pt.c 201 KB
Newer Older
mrs's avatar
mrs committed
1
/* Handle parameterized types (templates) for GNU C++.
law's avatar
law committed
2
   Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
mrs's avatar
mrs committed
3
   Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
mrs's avatar
mrs committed
4
   Rewritten by Jason Merrill (jason@cygnus.com).
mrs's avatar
mrs committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

This file is part of GNU CC.

GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING.  If not, write to
kenner's avatar
kenner committed
20 21
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
mrs's avatar
mrs committed
22 23

/* Known bugs or deficiencies include:
mrs's avatar
mrs committed
24 25 26

     all methods must be provided in header files; can't use a source
     file that contains only the method templates and "just win".  */
mrs's avatar
mrs committed
27 28

#include "config.h"
29
#include "system.h"
mrs's avatar
mrs committed
30 31 32 33 34 35 36
#include "obstack.h"

#include "tree.h"
#include "flags.h"
#include "cp-tree.h"
#include "decl.h"
#include "parse.h"
mrs's avatar
mrs committed
37
#include "lex.h"
mrs's avatar
mrs committed
38
#include "output.h"
mrs's avatar
mrs committed
39
#include "defaults.h"
40
#include "except.h"
41
#include "toplev.h"
42

43 44 45 46
/* The type of functions taking a tree, and some additional data, and
   returning an int.  */
typedef int (*tree_fn_t) PROTO((tree, void*));

mrs's avatar
mrs committed
47 48 49 50 51 52
extern struct obstack permanent_obstack;

extern int lineno;
extern char *input_filename;
struct pending_inline *pending_template_expansions;

mrs's avatar
mrs committed
53 54
tree current_template_parms;
HOST_WIDE_INT processing_template_decl;
mrs's avatar
mrs committed
55

mrs's avatar
mrs committed
56 57 58
tree pending_templates;
static tree *template_tail = &pending_templates;

mrs's avatar
mrs committed
59 60 61
tree maybe_templates;
static tree *maybe_template_tail = &maybe_templates;

mrs's avatar
mrs committed
62
int minimal_parse_mode;
63

64
int processing_specialization;
65
int processing_explicit_instantiation;
66
int processing_template_parmlist;
67 68
static int template_header_count;

69 70
static tree saved_trees;

mrs's avatar
mrs committed
71 72 73
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free

74 75 76 77 78 79
#define UNIFY_ALLOW_NONE 0
#define UNIFY_ALLOW_MORE_CV_QUAL 1
#define UNIFY_ALLOW_LESS_CV_QUAL 2
#define UNIFY_ALLOW_DERIVED 4

static int unify PROTO((tree, tree, tree, tree, int, int*));
80 81 82
static void add_pending_template PROTO((tree));
static int push_tinst_level PROTO((tree));
static tree classtype_mangled_name PROTO((tree));
83
static char *mangle_class_name_for_template PROTO((char *, tree, tree, tree));
84 85
static tree tsubst_expr_values PROTO((tree, tree));
static int list_eq PROTO((tree, tree));
86
static tree get_class_bindings PROTO((tree, tree, tree, tree));
87
static tree coerce_template_parms PROTO((tree, tree, tree, int, int, int));
88
static tree tsubst_enum	PROTO((tree, tree, tree *));
89
static tree add_to_template_args PROTO((tree, tree));
90 91
static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*,
						    tree*)); 
92
static int  type_unification_real PROTO((tree, tree, tree, tree,
93
					 int, unification_kind_t, int, int*));
94
static tree complete_template_args PROTO((tree, tree, int));
95
static void note_template_header PROTO((int));
96
static tree maybe_fold_nontype_arg PROTO((tree));
97
static tree convert_nontype_argument PROTO((tree, tree));
98
static tree get_bindings_overload PROTO((tree, tree, tree));
99
static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
law's avatar
8  
law committed
100
static tree build_template_parm_index PROTO((int, int, int, tree, tree));
101 102 103 104 105 106 107 108 109 110
static tree original_template PROTO((tree));
static int inline_needs_template_parms PROTO((tree));
static void push_inline_template_parms_recursive PROTO((tree, int));
static tree retrieve_specialization PROTO((tree, tree));
static void register_specialization PROTO((tree, tree, tree));
static void print_candidates PROTO((tree));
static tree reduce_template_parm_level PROTO((tree, tree, int));
static tree build_template_decl PROTO((tree, tree));
static int mark_template_parm PROTO((tree, void *));
static tree tsubst_friend_function PROTO((tree, tree));
111
static tree tsubst_friend_class PROTO((tree, tree));
112
static tree get_bindings_real PROTO((tree, tree, tree, int));
mmitchel's avatar
mmitchel committed
113
static int template_decl_level PROTO((tree));
114
static tree maybe_get_template_decl_from_type_decl PROTO((tree));
115 116 117
static int check_cv_quals_for_unify PROTO((int, tree, tree));
static tree tsubst_template_arg_vector PROTO((tree, tree));
static void regenerate_decl_from_template PROTO((tree, tree));
118

119 120 121 122 123 124 125 126
/* Nonzero if ARGVEC contains multiple levels of template arguments.  */
#define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) 		\
  (NODE != NULL_TREE 					\
   && TREE_CODE (NODE) == TREE_VEC 			\
   && TREE_VEC_LENGTH (NODE) > 0 			\
   && TREE_VEC_ELT (NODE, 0) != NULL_TREE               \
   && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)

127 128 129 130 131 132 133 134 135 136 137
/* Do any processing required when DECL (a member template declaration
   using TEMPLATE_PARAMETERS as its innermost parameter list) is
   finished.  Returns the TEMPLATE_DECL corresponding to DECL, unless
   it is a specialization, in which case the DECL itself is returned.  */

tree
finish_member_template_decl (template_parameters, decl)
  tree template_parameters;
  tree decl;
{
  if (template_parameters)
138
    end_template_decl ();
139
  else
140
    end_specialization ();
141

142 143 144 145
  if (decl == NULL_TREE || decl == void_type_node)
    return NULL_TREE;
  else if (TREE_CODE (decl) == TREE_LIST)
    {
jason's avatar
comment  
jason committed
146
      /* Assume that the class is the only declspec.  */
147
      decl = TREE_VALUE (decl);
148 149
      if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
	  && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
150 151 152 153 154
	{
	  tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
	  check_member_template (tmpl);
	  return tmpl;
	}
155
      return NULL_TREE;
156
    }
157
  else if (DECL_TEMPLATE_INFO (decl))
158
    {
159 160 161 162 163 164 165 166 167 168 169
      if (!DECL_TEMPLATE_SPECIALIZATION (decl))
	{
	  check_member_template (DECL_TI_TEMPLATE (decl));
	  return DECL_TI_TEMPLATE (decl);
	}
      else
	return decl;
    } 
  else
    cp_error ("invalid member template declaration `%D'", decl);
	
170

171
  return error_mark_node;
jason's avatar
jason committed
172
}
173

jason's avatar
jason committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/* Returns the template nesting level of the indicated class TYPE.
   
   For example, in:
     template <class T>
     struct A
     {
       template <class U>
       struct B {};
     };

   A<T>::B<U> has depth two, while A<T> has depth one.  Also,
   both A<T>::B<int> and A<int>::B<U> have depth one.  */

int 
template_class_depth (type)
     tree type;
{
191
  int depth;
jason's avatar
jason committed
192

jason's avatar
jason committed
193 194 195
  for (depth = 0; 
       type && TREE_CODE (type) != FUNCTION_DECL 
	 && TREE_CODE (type) != NAMESPACE_DECL;
196 197 198 199 200
       type = TYPE_CONTEXT (type))
    if (CLASSTYPE_TEMPLATE_INFO (type)
	&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
	&& uses_template_parms (CLASSTYPE_TI_ARGS (type)))
      ++depth;
jason's avatar
jason committed
201 202

  return depth;
203
}
204

205 206
/* Return the original template for this decl, disregarding any
   specializations.  */
207

208 209
static tree
original_template (decl)
210
     tree decl;
211
{
212 213 214 215
  while (DECL_TEMPLATE_INFO (decl))
    decl = DECL_TI_TEMPLATE (decl);
  return decl;
}
216

217 218 219 220 221 222 223 224 225
/* Returns 1 if processing DECL as part of do_pending_inlines
   needs us to push template parms.  */

static int
inline_needs_template_parms (decl)
     tree decl;
{
  if (! DECL_TEMPLATE_INFO (decl))
    return 0;
jason's avatar
jason committed
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  return (list_length (DECL_TEMPLATE_PARMS (original_template (decl)))
	  > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
}

/* Subroutine of maybe_begin_member_template_processing.
   Push the template parms in PARMS, starting from LEVELS steps into the
   chain, and ending at the beginning, since template parms are listed
   innermost first.  */

static void
push_inline_template_parms_recursive (parmlist, levels)
     tree parmlist;
     int levels;
{
  tree parms = TREE_VALUE (parmlist);
  int i;

  if (levels > 1)
    push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
246

247
  ++processing_template_decl;
248
  current_template_parms
249 250
    = tree_cons (build_int_2 (0, processing_template_decl),
		 parms, current_template_parms);
251 252
  TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;

253
  pushlevel (0);
254 255
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) 
    {
256 257
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
258

259 260
      switch (TREE_CODE (parm))
	{
261
	case TYPE_DECL:
262
	case TEMPLATE_DECL:
263 264
	  pushdecl (parm);
	  break;
265 266 267 268 269 270 271 272 273

	case PARM_DECL:
	  {
	    /* Make a CONST_DECL as is done in process_template_parm. */
	    tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
				    TREE_TYPE (parm));
	    DECL_INITIAL (decl) = DECL_INITIAL (parm);
	    pushdecl (decl);
	  }
274
	  break;
275

276 277 278 279 280 281
	default:
	  my_friendly_abort (0);
	}
    }
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
/* Restore the template parameter context for a member template or
   a friend template defined in a class definition.  */

void
maybe_begin_member_template_processing (decl)
     tree decl;
{
  tree parms;
  int levels;

  if (! inline_needs_template_parms (decl))
    return;

  parms = DECL_TEMPLATE_PARMS (original_template (decl));

  levels = list_length (parms) - processing_template_decl;

  if (DECL_TEMPLATE_SPECIALIZATION (decl))
    {
      --levels;
      parms = TREE_CHAIN (parms);
    }

  push_inline_template_parms_recursive (parms, levels);
}

308 309 310
/* Undo the effects of begin_member_template_processing. */

void 
jason's avatar
jason committed
311 312
maybe_end_member_template_processing (decl)
     tree decl;
313 314 315 316
{
  if (! processing_template_decl)
    return;

317 318 319 320 321 322 323
  while (current_template_parms
	 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
    {
      --processing_template_decl;
      current_template_parms = TREE_CHAIN (current_template_parms);
      poplevel (0, 0, 0);
    }
324 325
}

326
/* Returns non-zero iff T is a member template function.  We must be
jason's avatar
jason committed
327
   careful as in
328 329 330 331 332 333 334 335 336 337 338

     template <class T> class C { void f(); }

   Here, f is a template function, and a member, but not a member
   template.  This function does not concern itself with the origin of
   T, only its present state.  So if we have 

     template <class T> class C { template <class U> void f(U); }

   then neither C<int>::f<char> nor C<T>::f<double> is considered
   to be a member template.  */
339

340 341
int
is_member_template (t)
342 343
     tree t;
{
344 345
  if (TREE_CODE (t) != FUNCTION_DECL
      && !DECL_FUNCTION_TEMPLATE_P (t))
346
    /* Anything that isn't a function or a template function is
347 348 349
       certainly not a member template.  */
    return 0;

350 351 352 353 354
  /* A local class can't have member templates.  */
  if (hack_decl_function_context (t))
    return 0;

  if ((DECL_FUNCTION_MEMBER_P (t) 
355
       && !DECL_TEMPLATE_SPECIALIZATION (t))
jason's avatar
jason committed
356
      || (TREE_CODE (t) == TEMPLATE_DECL 
357
	  && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))))
358
    {
jason's avatar
jason committed
359
      tree tmpl;
360 361 362 363 364 365

      if (DECL_FUNCTION_TEMPLATE_P (t))
	tmpl = t;
      else if (DECL_TEMPLATE_INFO (t) 
	       && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
	tmpl = DECL_TI_TEMPLATE (t);
jason's avatar
jason committed
366 367 368
      else
	tmpl = NULL_TREE;

369
      if (tmpl
jason's avatar
jason committed
370 371 372
	  /* If there are more levels of template parameters than
	     there are template classes surrounding the declaration,
	     then we have a member template.  */
373 374
	  && (list_length (DECL_TEMPLATE_PARMS (tmpl)) > 
	      template_class_depth (DECL_CLASS_CONTEXT (t))))
jason's avatar
jason committed
375 376
	return 1;
    }
377

jason's avatar
jason committed
378 379
  return 0;
}
380

381
/* Return a new template argument vector which contains all of ARGS
382
   for all outer templates TMPL is contained in, but has as its 
383 384 385 386 387 388 389 390 391 392 393 394
   innermost set of arguments the EXTRA_ARGS.  If UNBOUND_ONLY, we
   are only interested in unbound template arguments, not arguments from
   enclosing templates that have been instantiated already.  */

static tree
complete_template_args (tmpl, extra_args, unbound_only)
     tree tmpl, extra_args;
     int unbound_only;
{
  /* depth is the number of levels of enclosing args we're adding.  */
  int depth, i;
  tree args, new_args, spec_args = NULL_TREE;
395 396
  int extra_arg_depth;

397 398 399
  my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
  my_friendly_assert (TREE_CODE (extra_args) == TREE_VEC, 0);

400
  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (extra_args))
401 402 403 404
    extra_arg_depth = TREE_VEC_LENGTH (extra_args);
  else
    extra_arg_depth = 1;

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
  if (DECL_TEMPLATE_INFO (tmpl) && !unbound_only)
    {
      /* A specialization of a member template of a template class shows up
	 as a TEMPLATE_DECL with DECL_TEMPLATE_SPECIALIZATION set.
	 DECL_TI_ARGS is the specialization args, and DECL_TI_TEMPLATE
	 is the template being specialized.  */
      if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
	{
	  spec_args = DECL_TI_ARGS (tmpl);
	  tmpl = DECL_TI_TEMPLATE (tmpl);
	}

      if (DECL_TEMPLATE_INFO (tmpl))
	{
	  /* A partial instantiation of a member template shows up as a
	     TEMPLATE_DECL with DECL_TEMPLATE_INFO.  DECL_TI_ARGS is
	     all the bound template arguments.  */
	  args = DECL_TI_ARGS (tmpl);
423
	  if (!TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
424 425 426 427 428 429 430 431 432
	    depth = 1;
	  else
	    depth = TREE_VEC_LENGTH (args);
	}
      else
	/* If we are a specialization, we might have no previously bound
	   template args.  */
	depth = 0;

433
      new_args = make_tree_vec (depth + extra_arg_depth + (!!spec_args));
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

      if (depth == 1)
	TREE_VEC_ELT (new_args, 0) = args;
      else
	for (i = 0; i < depth; ++i)
	  TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i);
    }
  else
    {
      tree type;
      int skip;

      /* For unbound args, we have to do more work.  We are getting bindings
	 for the innermost args from extra_args, so we start from our
	 context and work out until we've seen all the args.  We need to
	 do it this way to handle partial specialization.  */

      depth = list_length (DECL_TEMPLATE_PARMS (tmpl)) - 1;
      if (depth == 0)
	return extra_args;

455
      new_args = make_tree_vec (depth + extra_arg_depth);
456

457 458 459
      /* If this isn't a member template, extra_args is for the innermost
	 template class, so skip over it.  */
      skip = (! is_member_template (tmpl));
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

      type = DECL_REAL_CONTEXT (tmpl);
      for (i = depth; i; type = TYPE_CONTEXT (type))
	if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
	  {
	    if (skip)
	      skip = 0;
	    else
	      {
		--i;
		TREE_VEC_ELT (new_args, i) = CLASSTYPE_TI_ARGS (type);
	      }
	  }
    }

475 476 477 478 479
  if (extra_arg_depth == 1)
    TREE_VEC_ELT (new_args, depth++) = extra_args;
  else
    for (i = 0; i < extra_arg_depth; ++i)
      TREE_VEC_ELT (new_args, depth++) = TREE_VEC_ELT (extra_args, i);
480 481

  if (spec_args)
482
    TREE_VEC_ELT (new_args, depth) = spec_args;
483 484 485 486

  return new_args;
}

487 488 489
/* Return a new template argument vector which contains all of ARGS,
   but has as its innermost set of arguments the EXTRA_ARGS.  */

490
static tree
491 492 493 494 495 496
add_to_template_args (args, extra_args)
     tree args;
     tree extra_args;
{
  tree new_args;

497
  if (!TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
498 499 500 501 502 503 504 505
    {
      new_args = make_tree_vec (2);
      TREE_VEC_ELT (new_args, 0) = args;
    }
  else 
    {
      int i;

506
      new_args = make_tree_vec (TREE_VEC_LENGTH (args) + 1);
507 508 509 510 511 512 513 514 515 516

      for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
	TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i);
    }
	  
  TREE_VEC_ELT (new_args, 
		TREE_VEC_LENGTH (new_args) - 1) = extra_args;

  return new_args;
}
mrs's avatar
mrs committed
517 518 519

/* We've got a template header coming up; push to a new level for storing
   the parms.  */
mrs's avatar
mrs committed
520 521 522 523

void
begin_template_parm_list ()
{
524 525 526 527 528 529 530 531 532 533 534 535 536 537
  /* We use a non-tag-transparent scope here, which causes pushtag to
     put tags in this scope, rather than in the enclosing class or
     namespace scope.  This is the right thing, since we want
     TEMPLATE_DECLS, and not TYPE_DECLS for template classes.  For a
     global template class, push_template_decl handles putting the
     TEMPLATE_DECL into top-level scope.  For a nested template class,
     e.g.:

       template <class T> struct S1 {
         template <class T> struct S2 {}; 
       };

     pushtag contains special code to call pushdecl_with_scope on the
     TEMPLATE_DECL for S2.  */
mrs's avatar
mrs committed
538
  pushlevel (0);
mrs's avatar
mrs committed
539
  declare_pseudo_global_level ();
mrs's avatar
mrs committed
540
  ++processing_template_decl;
541
  ++processing_template_parmlist;
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  note_template_header (0);
}

/* We've just seen template <>. */

void
begin_specialization ()
{
  note_template_header (1);
}

/* Called at then end of processing a declaration preceeded by
   template<>.  */

void 
end_specialization ()
{
  reset_specialization ();
}

/* Any template <>'s that we have seen thus far are not referring to a
   function specialization. */

void
reset_specialization ()
{
  processing_specialization = 0;
  template_header_count = 0;
}

/* We've just seen a template header.  If SPECIALIZATION is non-zero,
   it was of the form template <>.  */

575
static void 
576 577 578 579 580 581 582
note_template_header (specialization)
     int specialization;
{
  processing_specialization = specialization;
  template_header_count++;
}

583
/* We're beginning an explicit instantiation.  */
584

585 586
void
begin_explicit_instantiation ()
587
{
588 589
  ++processing_explicit_instantiation;
}
590 591


592 593 594 595 596 597
void
end_explicit_instantiation ()
{
  my_friendly_assert(processing_explicit_instantiation > 0, 0);
  --processing_explicit_instantiation;
}
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
/* Retrieve the specialization (in the sense of [temp.spec] - a
   specialization is either an instantiation or an explicit
   specialization) of TMPL for the given template ARGS.  If there is
   no such specialization, return NULL_TREE.  The ARGS are a vector of
   arguments, or a vector of vectors of arguments, in the case of
   templates with more than one level of parameters.  */
   
static tree
retrieve_specialization (tmpl, args)
     tree tmpl;
     tree args;
{
  tree s;

  my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);

  for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
       s != NULL_TREE;
       s = TREE_CHAIN (s))
    if (comp_template_args (TREE_PURPOSE (s), args))
      return TREE_VALUE (s);

  return NULL_TREE;
622 623
}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
/* Returns non-zero iff DECL is a specialization of TMPL.  */

int
is_specialization_of (decl, tmpl)
     tree decl;
     tree tmpl;
{
  tree t;

  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
      for (t = decl; 
	   t != NULL_TREE;
	   t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
	if (t == tmpl)
	  return 1;
    }
  else 
    {
      my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);

      for (t = TREE_TYPE (decl);
	   t != NULL_TREE;
	   t = CLASSTYPE_USE_TEMPLATE (t)
	     ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
	if (comptypes (TYPE_MAIN_VARIANT (t), 
		       TYPE_MAIN_VARIANT (TREE_TYPE (tmpl)), 1))
	  return 1;
    }  
653

654 655
  return 0;
}
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

/* Register the specialization SPEC as a specialization of TMPL with
   the indicated ARGS.  */

static void
register_specialization (spec, tmpl, args)
     tree spec;
     tree tmpl;
     tree args;
{
  tree s;

  my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);

  if (TREE_CODE (spec) != TEMPLATE_DECL
      && list_length (DECL_TEMPLATE_PARMS (tmpl)) > 1)
    /* Avoid registering function declarations as
       specializations of member templates, as would otherwise
       happen with out-of-class specializations of member
       templates.  */
    return;
    
  for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
       s != NULL_TREE;
       s = TREE_CHAIN (s))
    if (comp_template_args (TREE_PURPOSE (s), args))
      {
	tree fn = TREE_VALUE (s);

	if (DECL_TEMPLATE_SPECIALIZATION (spec))
	  {
	    if (DECL_TEMPLATE_INSTANTIATION (fn))
	      {
		if (TREE_USED (fn) 
		    || DECL_EXPLICIT_INSTANTIATION (fn))
		  {
		    cp_error ("specialization of %D after instantiation",
			      fn);
		    return;
		  }
		else
		  {
		    /* This situation should occur only if the first
		       specialization is an implicit instantiation,
		       the second is an explicit specialization, and
		       the implicit instantiation has not yet been
		       used.  That situation can occur if we have
		       implicitly instantiated a member function of
		       class type, and then specialized it later.  */
		    TREE_VALUE (s) = spec;
		    return;
		  }
	      }
	    else if (DECL_TEMPLATE_SPECIALIZATION (fn))
	      {
		if (DECL_INITIAL (fn))
		  cp_error ("duplicate specialization of %D", fn);

		TREE_VALUE (s) = spec;
		return;
	      }
	  }
      }

  DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
     = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
}

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
/* Print the list of candidate FNS in an error message.  */

static void
print_candidates (fns)
     tree fns;
{
  tree fn;

  char* str = "candidates are:";

  for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
    {
      cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
      str = "               ";
    }
}

741
/* Returns the template (one of the functions given by TEMPLATE_ID)
742 743 744 745 746 747 748 749
   which can be specialized to match the indicated DECL with the
   explicit template args given in TEMPLATE_ID.  If
   NEED_MEMBER_TEMPLATE is true the function is a specialization of a
   member template.  The template args (those explicitly specified and
   those deduced) are output in a newly created vector *TARGS_OUT.  If
   it is impossible to determine the result, an error message is
   issued, unless COMPLAIN is 0.  The DECL may be NULL_TREE if none is
   available.  */
750

751
tree
752
determine_specialization (template_id, decl, targs_out, 
753 754
			  need_member_template,
			  complain)
755
     tree template_id;
756
     tree decl;
757 758 759 760
     tree* targs_out;
     int need_member_template;
     int complain;
{
761 762
  tree fns = TREE_OPERAND (template_id, 0);
  tree targs_in = TREE_OPERAND (template_id, 1);
763
  tree templates = NULL_TREE;
764
  tree fn;
765
  int i;
766

767 768
  *targs_out = NULL_TREE;

769 770 771
  if (fns == error_mark_node)
    return error_mark_node;

jason's avatar
jason committed
772 773 774
  /* Check for baselinks. */
  if (TREE_CODE (fns) == TREE_LIST)
    fns = TREE_VALUE (fns);
775

jason's avatar
jason committed
776
  for (; fns; fns = OVL_NEXT (fns))
777
    {
778 779
      tree tmpl;

jason's avatar
jason committed
780
      fn = OVL_CURRENT (fns);
781 782
      if (!need_member_template 
	  && TREE_CODE (fn) == FUNCTION_DECL 
783
	  && DECL_FUNCTION_MEMBER_P (fn)
784 785
	  && DECL_USE_TEMPLATE (fn)
	  && DECL_TI_TEMPLATE (fn))
786 787 788 789 790
	/* We can get here when processing something like:
	     template <class T> class X { void f(); }
	     template <> void X<int>::f() {}
	   We're specializing a member function, but not a member
	   template.  */
791 792
	tmpl = DECL_TI_TEMPLATE (fn);
      else if (TREE_CODE (fn) != TEMPLATE_DECL
793
	       || (need_member_template && !is_member_template (fn)))
794
	continue;
795 796
      else
	tmpl = fn;
797

798
      if (list_length (targs_in) > DECL_NTPARMS (tmpl))
799 800
	continue;

801
      if (decl == NULL_TREE)
802
	{
803 804 805 806 807
	  tree targs = make_scratch_vec (DECL_NTPARMS (tmpl));

	  /* We allow incomplete unification here, because we are going to
	     check all the functions. */
	  i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
808
				targs,
809 810 811
				NULL_TREE,
				NULL_TREE,  
				targs_in,
812
				DEDUCE_EXACT, 1);
813 814 815 816
      
	  if (i == 0) 
	    /* Unification was successful.  */
	    templates = scratch_tree_cons (targs, tmpl, templates);
817
	}
818 819
      else
	templates = scratch_tree_cons (NULL_TREE, tmpl, templates);
820
    }
821
  
822
  if (decl != NULL_TREE)
823
    {
824 825 826 827 828 829 830 831 832 833 834 835 836 837
      tree tmpl = most_specialized (templates, decl, targs_in);

      if (tmpl == error_mark_node) 
	goto ambiguous;
      else if (tmpl == NULL_TREE)
	goto no_match;

      *targs_out = get_bindings (tmpl, decl, targs_in);
      return tmpl;
    }

  if (templates == NULL_TREE)
    {
    no_match:
838
      if (complain)
839 840 841 842 843
	{
	  cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
		       template_id, decl);
	  return error_mark_node;
	}
844 845
      return NULL_TREE;
    }
846
  else if (TREE_CHAIN (templates) != NULL_TREE) 
847
    {
848
    ambiguous:
849 850
      if (complain)
	{
851 852
	  cp_error_at ("ambiguous template specialization `%D' for `%+D'",
		       template_id, decl);
853
	  print_candidates (templates);
854
	  return error_mark_node;
855 856 857 858 859
	}
      return NULL_TREE;
    }

  /* We have one, and exactly one, match. */
860 861
  *targs_out = TREE_PURPOSE (templates);
  return TREE_VALUE (templates);
mrs's avatar
mrs committed
862
}
863
      
864
/* Check to see if the function just declared, as indicated in
865 866 867 868
   DECLARATOR, and in DECL, is a specialization of a function
   template.  We may also discover that the declaration is an explicit
   instantiation at this point.

869 870
   Returns DECL, or an equivalent declaration that should be used
   instead. 
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
   
   FLAGS is a bitmask consisting of the following flags: 

   1: We are being called by finish_struct.  (We are unable to
      determine what template is specialized by an in-class
      declaration until the class definition is complete, so
      finish_struct_methods calls this function again later to finish
      the job.)
   2: The function has a definition.
   4: The function is a friend.
   8: The function is known to be a specialization of a member
      template. 

   The TEMPLATE_COUNT is the number of references to qualifying
   template classes that appeared in the name of the function.  For
   example, in

     template <class T> struct S { void f(); };
     void S<int>::f();
     
   the TEMPLATE_COUNT would be 1.  However, explicitly specialized
   classes are not counted in the TEMPLATE_COUNT, so that in

     template <class T> struct S {};
     template <> struct S<int> { void f(); }
     template <>
     void S<int>::f();

   the TEMPLATE_COUNT would be 0.  (Note that this declaration is
   illegal; there should be no template <>.)

   If the function is a specialization, it is marked as such via
   DECL_TEMPLATE_SPECIALIZATION.  Furthermore, its DECL_TEMPLATE_INFO
   is set up correctly, and it is added to the list of specializations 
   for that template.  */
906

907
tree
908
check_explicit_specialization (declarator, decl, template_count, flags)
909 910 911 912 913
     tree declarator;
     tree decl;
     int template_count;
     int flags;
{
914 915 916 917
  int finish_member = flags & 1;
  int have_def = flags & 2;
  int is_friend = flags & 4;
  int specialization = 0;
918
  int explicit_instantiation = 0;
919 920 921 922
  int member_specialization = flags & 8;

  tree ctype = DECL_CLASS_CONTEXT (decl);
  tree dname = DECL_NAME (decl);
923

924
  if (!finish_member)
925
    {
926 927 928 929 930 931 932 933 934 935 936 937 938 939
      if (processing_specialization) 
	{
	  /* The last template header was of the form template <>.  */
	  
	  if (template_header_count > template_count) 
	    {
	      /* There were more template headers than qualifying template
		 classes.  */
	      if (template_header_count - template_count > 1)
		/* There shouldn't be that many template parameter
		   lists.  There can be at most one parameter list for
		   every qualifying class, plus one for the function
		   itself.  */
		cp_error ("too many template parameter lists in declaration of `%D'", decl);
940

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
	      SET_DECL_TEMPLATE_SPECIALIZATION (decl);
	      if (ctype)
		member_specialization = 1;
	      else
		specialization = 1;
	    }
	  else if (template_header_count == template_count)
	    {
	      /* The counts are equal.  So, this might be a
		 specialization, but it is not a specialization of a
		 member template.  It might be something like
		 
		 template <class T> struct S { 
	         void f(int i); 
		 };
		 template <>
		 void S<int>::f(int i) {}  */
	      specialization = 1;
	      SET_DECL_TEMPLATE_SPECIALIZATION (decl);
	    }
	  else 
	    {
	      /* This cannot be an explicit specialization.  There are not
		 enough headers for all of the qualifying classes.  For
		 example, we might have:
	     
		 template <>
		 void S<int>::T<char>::f();

		 But, we're missing another template <>.  */
	      cp_error("too few template parameter lists in declaration of `%D'", decl);
972
	      return decl;
973 974 975 976 977 978 979 980 981
	    } 
	}
      else if (processing_explicit_instantiation)
	{
	  if (template_header_count)
	    cp_error ("template parameter list used in explicit instantiation");
	  
	  if (have_def)
	    cp_error ("definition provided for explicit instantiation");
982

983
	  explicit_instantiation = 1;
984
	}
985 986 987
      else if (ctype != NULL_TREE
	       && !TYPE_BEING_DEFINED (ctype)
	       && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
988
	{
989
	  /* This case catches outdated code that looks like this:
990

991
	     template <class T> struct S { void f(); };
992 993 994 995 996 997 998
	     void S<int>::f() {} // Missing template <>

	     We disable this check when the type is being defined to
	     avoid complaining about default compiler-generated
	     constructors, destructors, and assignment operators.
	     Since the type is an instantiation, not a specialization,
	     these are the only functions that can be defined before
999
	     the class is complete.  */
1000

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
	  /* If they said
	       template <class T> void S<int>::f() {}
	     that's bogus.  */
	  if (template_header_count)
	    {
	      cp_error ("template parameters specified in specialization");
	      return decl;
	    }

	  if (pedantic)
	    cp_pedwarn
	      ("explicit specialization not preceded by `template <>'");
	  specialization = 1;
	  SET_DECL_TEMPLATE_SPECIALIZATION (decl);
	}
      else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
	{
	  /* This case handles bogus declarations like
1019 1020 1021
	     template <> template <class T>
	     void f<int>();  */

1022 1023
	  cp_error ("template-id `%D' in declaration of primary template",
		    declarator);
1024
	  return decl;
1025
	}
1026
    }
1027

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  if (specialization || member_specialization)
    {
      tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
      for (; t; t = TREE_CHAIN (t))
	if (TREE_PURPOSE (t))
	  {
	    cp_pedwarn
	      ("default argument specified in explicit specialization");
	    break;
	  }
    }

1040
  if (specialization || member_specialization || explicit_instantiation)
1041 1042 1043 1044 1045
    {
      tree tmpl = NULL_TREE;
      tree targs = NULL_TREE;

      /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
1046 1047 1048 1049 1050 1051 1052
      if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
	{
	  tree fns;

	  my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, 
			      0);
	  if (!ctype)
jason's avatar
jason committed
1053
	    fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1054 1055 1056
	  else
	    fns = dname;

1057 1058
	  declarator = 
	    lookup_template_function (fns, NULL_TREE);
1059 1060 1061 1062 1063 1064 1065
	}

      if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) 
	{
	  /* A friend declaration.  We can't do much, because we don't
	   know what this resolves to, yet.  */
	  my_friendly_assert (is_friend != 0, 0);
1066
	  my_friendly_assert (!explicit_instantiation, 0);
1067
	  SET_DECL_IMPLICIT_INSTANTIATION (decl);
1068
	  return decl;
1069 1070
	} 

1071 1072 1073 1074 1075 1076
      if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
	{
	  /* Since finish_struct_1 has not been called yet, we
	     can't call lookup_fnfields.  We note that this
	     template is a specialization, and proceed, letting
	     finish_struct fix this up later.  */
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	  tree ti = perm_tree_cons (NULL_TREE, 
				    TREE_OPERAND (declarator, 1),
				    NULL_TREE);
	  TI_PENDING_SPECIALIZATION_FLAG (ti) = 1;
	  DECL_TEMPLATE_INFO (decl) = ti;
	  /* This should not be an instantiation; explicit
	     instantiation directives can only occur at the top
	     level.  */
	  my_friendly_assert (!explicit_instantiation, 0);
	  return decl;
1087 1088 1089 1090
	}
      else if (ctype != NULL_TREE 
	       && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
		   IDENTIFIER_NODE))
1091
	{
1092 1093 1094
	  /* Find the list of functions in ctype that have the same
	     name as the declared function.  */
	  tree name = TREE_OPERAND (declarator, 0);
1095
	  tree fns;
1096 1097 1098
	  
	  if (name == constructor_name (ctype) 
	      || name == constructor_name_full (ctype))
1099
	    {
1100 1101 1102 1103 1104 1105
	      int is_constructor = DECL_CONSTRUCTOR_P (decl);
	      
	      if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
		  : !TYPE_HAS_DESTRUCTOR (ctype))
		{
		  /* From [temp.expl.spec]:
1106
		       
1107 1108 1109
		     If such an explicit specialization for the member
		     of a class template names an implicitly-declared
		     special member function (clause _special_), the
1110 1111 1112
		     program is ill-formed.  

		     Similar language is found in [temp.explicit].  */
1113 1114
		  cp_error ("specialization of implicitly-declared special member function");

1115
		  return decl;
1116
		}
1117

1118
	      name = is_constructor ? ctor_identifier : dtor_identifier;
1119
	    }
1120 1121

	  fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
1122 1123 1124
	  
	  if (fns == NULL_TREE) 
	    {
1125 1126
	      cp_error ("no member function `%s' declared in `%T'",
			IDENTIFIER_POINTER (name),
1127
			ctype);
1128
	      return decl;
1129 1130 1131 1132
	    }
	  else
	    TREE_OPERAND (declarator, 0) = fns;
	}
1133
      
1134 1135
      /* Figure out what exactly is being specialized at this point.
	 Note that for an explicit instantiation, even one for a
law's avatar
law committed
1136
	 member function, we cannot tell apriori whether the
1137 1138 1139 1140 1141 1142
	 instantiation is for a member template, or just a member
	 function of a template class.  In particular, even in if the
	 instantiation is for a member template, the template
	 arguments could be deduced from the declaration.  */
      tmpl = determine_specialization (declarator, decl,
				       &targs, 
1143 1144
				       member_specialization,
				       1);
1145
	    
1146
      if (tmpl && tmpl != error_mark_node)
1147
	{
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	  if (explicit_instantiation)
	    {
	      decl = instantiate_template (tmpl, targs);
	      if (!DECL_TEMPLATE_SPECIALIZATION (decl))
		/* There doesn't seem to be anything in the draft to
		   prevent a specialization from being explicitly
		   instantiated.  We're careful not to destroy the
		   information indicating that this is a
		   specialization here.  */
		SET_DECL_EXPLICIT_INSTANTIATION (decl);
	      return decl;
	    }
1160 1161
	  else if (DECL_STATIC_FUNCTION_P (tmpl)
		   && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1162 1163 1164 1165
	    {
	      revert_static_member_fn (&decl, 0, 0);
	      last_function_parms = TREE_CHAIN (last_function_parms);
	    }
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

	  /* Mangle the function name appropriately.  Note that we do
	     not mangle specializations of non-template member
	     functions of template classes, e.g. with
	       template <class T> struct S { void f(); }
	     and given the specialization 
	       template <> void S<int>::f() {}
	     we do not mangle S<int>::f() here.  That's because it's
	     just an ordinary member function and doesn't need special
	     treatment.  */
	  if ((is_member_template (tmpl) || ctype == NULL_TREE)
1177
	      && name_mangling_version >= 1)
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
	    {
	      tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));

	      if (ctype 
		  && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE)
		arg_types = 
		  hash_tree_chain (build_pointer_type (ctype),
				   arg_types);

	      DECL_ASSEMBLER_NAME (decl) 
		= build_template_decl_overload 
		(DECL_NAME (decl), 
		 arg_types,
		 TREE_TYPE (TREE_TYPE (tmpl)),
		 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
		 targs, ctype != NULL_TREE);
	    }

	  if (is_friend && !have_def)
	    {
	      /* This is not really a declaration of a specialization.
		 It's just the name of an instantiation.  But, it's not
		 a request for an instantiation, either.  */
	      SET_DECL_IMPLICIT_INSTANTIATION (decl);
	      DECL_TEMPLATE_INFO (decl) 
		= perm_tree_cons (tmpl, targs, NULL_TREE);
1204
	      return decl;
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
	    }

	  /* If DECL_TI_TEMPLATE (decl), the decl is an
	     instantiation of a specialization of a member template.
	     (In other words, there was a member template, in a
	     class template.  That member template was specialized.
	     We then instantiated the class, so there is now an
	     instance of that specialization.)  

	     According to the CD2,

	     14.7.3.13 [tmpl.expl.spec]
	       
	     A specialization  of  a member function template or
	     member class template of a non-specialized class
	     template is itself a template.	        

1222
	     So, we just leave the template info alone in this case.  */
1223 1224 1225
	  if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)))
	    DECL_TEMPLATE_INFO (decl)
	      = perm_tree_cons (tmpl, targs, NULL_TREE);
1226 1227 1228

	  register_specialization (decl, tmpl, targs);

1229
	  return decl;
1230 1231
	}
    }
1232
  
1233
  return decl;
1234
}
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
   parameters.  These are represented in the same format used for
   DECL_TEMPLATE_PARMS.  */

int comp_template_parms (parms1, parms2)
     tree parms1;
     tree parms2;
{
  tree p1;
  tree p2;

  if (parms1 == parms2)
    return 1;

  for (p1 = parms1, p2 = parms2; 
       p1 != NULL_TREE && p2 != NULL_TREE;
       p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
    {
      tree t1 = TREE_VALUE (p1);
      tree t2 = TREE_VALUE (p2);
      int i;

      my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
      my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);

      if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
	return 0;

      for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) 
	{
	  tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
	  tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));

	  if (TREE_CODE (parm1) != TREE_CODE (parm2))
	    return 0;

	  if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
	    continue;
	  else if (!comptypes (TREE_TYPE (parm1), 
			       TREE_TYPE (parm2), 1))
	    return 0;
	}
    }

  if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
    /* One set of parameters has more parameters lists than the
       other.  */
    return 0;

  return 1;
}

jason's avatar
jason committed
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
   ORIG_LEVEL, DECL, and TYPE.  */

static tree
build_template_parm_index (index, level, orig_level, decl, type)
     int index;
     int level;
     int orig_level;
     tree decl;
     tree type;
{
  tree t = make_node (TEMPLATE_PARM_INDEX);
  TEMPLATE_PARM_IDX (t) = index;
  TEMPLATE_PARM_LEVEL (t) = level;
  TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
  TEMPLATE_PARM_DECL (t) = decl;
  TREE_TYPE (t) = type;

  return t;
}

/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1310
   TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
jason's avatar
jason committed
1311 1312 1313 1314
   TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
   new one is created.  */

static tree 
1315
reduce_template_parm_level (index, type, levels)
jason's avatar
jason committed
1316 1317
     tree index;
     tree type;
1318
     int levels;
jason's avatar
jason committed
1319 1320 1321
{
  if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
      || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1322
	  != TEMPLATE_PARM_LEVEL (index) - levels))
jason's avatar
jason committed
1323 1324 1325 1326 1327 1328 1329
    {
      tree decl 
	= build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
		      DECL_NAME (TEMPLATE_PARM_DECL (index)),
		      type);
      tree t
	= build_template_parm_index (TEMPLATE_PARM_IDX (index),
1330
				     TEMPLATE_PARM_LEVEL (index) - levels,
jason's avatar
jason committed
1331 1332 1333
				     TEMPLATE_PARM_ORIG_LEVEL (index),
				     decl, type);
      TEMPLATE_PARM_DESCENDANTS (index) = t;
1334 1335 1336 1337

      /* Template template parameters need this.  */
      DECL_TEMPLATE_PARMS (decl)
	= DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
jason's avatar
jason committed
1338 1339 1340 1341 1342
    }

  return TEMPLATE_PARM_DESCENDANTS (index);
}

mrs's avatar
mrs committed
1343
/* Process information from new template parameter NEXT and append it to the
mrs's avatar
mrs committed
1344
   LIST being built.  */
mrs's avatar
mrs committed
1345

mrs's avatar
mrs committed
1346 1347 1348 1349 1350 1351
tree
process_template_parm (list, next)
     tree list, next;
{
  tree parm;
  tree decl = 0;
mrs's avatar
mrs committed
1352
  tree defval;
mrs's avatar
mrs committed
1353
  int is_type, idx;
jason's avatar
jason committed
1354

mrs's avatar
mrs committed
1355 1356
  parm = next;
  my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
mrs's avatar
mrs committed
1357 1358 1359
  defval = TREE_PURPOSE (parm);
  parm = TREE_VALUE (parm);
  is_type = TREE_PURPOSE (parm) == class_type_node;
mrs's avatar
mrs committed
1360 1361 1362 1363 1364 1365 1366

  if (list)
    {
      tree p = TREE_VALUE (tree_last (list));

      if (TREE_CODE (p) == TYPE_DECL)
	idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1367 1368
      else if (TREE_CODE (p) == TEMPLATE_DECL)
	idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
mrs's avatar
mrs committed
1369
      else
jason's avatar
jason committed
1370
	idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
mrs's avatar
mrs committed
1371 1372 1373 1374 1375
      ++idx;
    }
  else
    idx = 0;

mrs's avatar
mrs committed
1376 1377
  if (!is_type)
    {
mrs's avatar
mrs committed
1378
      my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
mrs's avatar
mrs committed
1379
      /* is a const-param */
mrs's avatar
mrs committed
1380
      parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
mrs's avatar
merging  
mrs committed
1381
			     PARM, 0, NULL_TREE);
mrs's avatar
mrs committed
1382 1383
      /* A template parameter is not modifiable.  */
      TREE_READONLY (parm) = 1;
mrs's avatar
mrs committed
1384
      if (IS_AGGR_TYPE (TREE_TYPE (parm))
1385 1386
	  && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM
	  && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE)
mrs's avatar
mrs committed
1387
	{
mrs's avatar
mrs committed
1388 1389 1390 1391
	  cp_error ("`%#T' is not a valid type for a template constant parameter",
		    TREE_TYPE (parm));
	  if (DECL_NAME (parm) == NULL_TREE)
	    error ("  a template type parameter must begin with `class' or `typename'");
mrs's avatar
mrs committed
1392 1393
	  TREE_TYPE (parm) = void_type_node;
	}
mrs's avatar
mrs committed
1394 1395 1396
      else if (pedantic
	       && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
		   || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
mrs's avatar
mrs committed
1397 1398
	cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
		    TREE_TYPE (parm));
mrs's avatar
mrs committed
1399 1400 1401 1402 1403 1404
      if (TREE_PERMANENT (parm) == 0)
        {
	  parm = copy_node (parm);
	  TREE_PERMANENT (parm) = 1;
        }
      decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
jason's avatar
jason committed
1405 1406 1407 1408
      DECL_INITIAL (parm) = DECL_INITIAL (decl) 
	= build_template_parm_index (idx, processing_template_decl,
				     processing_template_decl,
				     decl, TREE_TYPE (parm));
mrs's avatar
mrs committed
1409 1410 1411
    }
  else
    {
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
      tree t;
      parm = TREE_VALUE (parm);
      
      if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
	{
	  t = make_lang_type (TEMPLATE_TEMPLATE_PARM);
	  /* This is for distinguishing between real templates and template 
	     template parameters */
	  TREE_TYPE (parm) = t;
	  TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
	  decl = parm;
	}
      else
	{
	  t = make_lang_type (TEMPLATE_TYPE_PARM);
	  /* parm is either IDENTIFIER_NODE or NULL_TREE */
	  decl = build_decl (TYPE_DECL, parm, t);
	}
        
mrs's avatar
mrs committed
1431
      CLASSTYPE_GOT_SEMICOLON (t) = 1;
mrs's avatar
mrs committed
1432 1433
      TYPE_NAME (t) = decl;
      TYPE_STUB_DECL (t) = decl;
mrs's avatar
mrs committed
1434
      parm = decl;
jason's avatar
jason committed
1435 1436 1437 1438
      TEMPLATE_TYPE_PARM_INDEX (t)
	= build_template_parm_index (idx, processing_template_decl, 
				     processing_template_decl,
				     decl, TREE_TYPE (parm));
mrs's avatar
mrs committed
1439
    }
mrs's avatar
mrs committed
1440
  SET_DECL_ARTIFICIAL (decl);
mrs's avatar
mrs committed
1441
  pushdecl (decl);
mrs's avatar
mrs committed
1442
  parm = build_tree_list (defval, parm);
mrs's avatar
mrs committed
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
  return chainon (list, parm);
}

/* The end of a template parameter list has been reached.  Process the
   tree list into a parameter vector, converting each parameter into a more
   useful form.	 Type parameters are saved as IDENTIFIER_NODEs, and others
   as PARM_DECLs.  */

tree
end_template_parm_list (parms)
     tree parms;
{
mrs's avatar
mrs committed
1455
  int nparms;
mrs's avatar
mrs committed
1456
  tree parm;
mrs's avatar
mrs committed
1457 1458 1459 1460 1461
  tree saved_parmlist = make_tree_vec (list_length (parms));

  current_template_parms
    = tree_cons (build_int_2 (0, processing_template_decl),
		 saved_parmlist, current_template_parms);
mrs's avatar
mrs committed
1462 1463

  for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
mrs's avatar
mrs committed
1464
    TREE_VEC_ELT (saved_parmlist, nparms) = parm;
mrs's avatar
mrs committed
1465

1466 1467
  --processing_template_parmlist;

mrs's avatar
mrs committed
1468 1469 1470
  return saved_parmlist;
}

mrs's avatar
mrs committed
1471 1472
/* end_template_decl is called after a template declaration is seen.  */

mrs's avatar
mrs committed
1473
void
mrs's avatar
mrs committed
1474
end_template_decl ()
mrs's avatar
mrs committed
1475
{
1476 1477
  reset_specialization ();

mrs's avatar
mrs committed
1478
  if (! processing_template_decl)
mrs's avatar
mrs committed
1479 1480
    return;

mrs's avatar
mrs committed
1481 1482
  /* This matches the pushlevel in begin_template_parm_list.  */
  poplevel (0, 0, 0);
mrs's avatar
mrs committed
1483

mrs's avatar
mrs committed
1484 1485 1486 1487
  --processing_template_decl;
  current_template_parms = TREE_CHAIN (current_template_parms);
  (void) get_pending_sizes ();	/* Why? */
}
mrs's avatar
mrs committed
1488

mrs's avatar
mrs committed
1489 1490 1491 1492
/* Generate a valid set of template args from current_template_parms.  */

tree
current_template_args ()
mrs's avatar
mrs committed
1493 1494
{
  tree header = current_template_parms;
1495 1496 1497 1498
  int length = list_length (header);
  tree args = make_tree_vec (length);
  int l = length;

mrs's avatar
mrs committed
1499
  while (header)
mrs's avatar
mrs committed
1500
    {
mrs's avatar
mrs committed
1501 1502 1503 1504 1505
      tree a = copy_node (TREE_VALUE (header));
      int i = TREE_VEC_LENGTH (a);
      TREE_TYPE (a) = NULL_TREE;
      while (i--)
	{
1506 1507 1508 1509 1510 1511 1512 1513 1514
	  tree t = TREE_VEC_ELT (a, i);

	  /* t will be a list if we are called from within a
	     begin/end_template_parm_list pair, but a vector directly
	     if within a begin/end_member_template_processing pair.  */
	  if (TREE_CODE (t) == TREE_LIST) 
	    {
	      t = TREE_VALUE (t);
	      
1515 1516
	      if (TREE_CODE (t) == TYPE_DECL 
		  || TREE_CODE (t) == TEMPLATE_DECL)
1517 1518 1519 1520 1521
		t = TREE_TYPE (t);
	      else
		t = DECL_INITIAL (t);
	    }

mrs's avatar
mrs committed
1522 1523
	  TREE_VEC_ELT (a, i) = t;
	}
1524
      TREE_VEC_ELT (args, --l) = a;
mrs's avatar
mrs committed
1525
      header = TREE_CHAIN (header);
mrs's avatar
mrs committed
1526 1527
    }

mrs's avatar
mrs committed
1528 1529
  return args;
}
1530

1531 1532 1533 1534

/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
   template PARMS.  Used by push_template_decl below.  */

1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
static tree
build_template_decl (decl, parms)
     tree decl;
     tree parms;
{
  tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
  DECL_TEMPLATE_PARMS (tmpl) = parms;
  DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
  if (DECL_LANG_SPECIFIC (decl))
    {
      DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
      DECL_STATIC_FUNCTION_P (tmpl) = 
	DECL_STATIC_FUNCTION_P (decl);
    }

  return tmpl;
}

1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
struct template_parm_data
{
  int level;
  int* parms;
};

/* Subroutine of push_template_decl used to see if each template
   parameter in a partial specialization is used in the explicit
   argument list.  If T is of the LEVEL given in DATA (which is
   treated as a template_parm_data*), then DATA->PARMS is marked
   appropriately.  */

static int
mark_template_parm (t, data)
     tree t;
     void* data;
{
  int level;
  int idx;
  struct template_parm_data* tpd = (struct template_parm_data*) data;

  if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
    {
      level = TEMPLATE_PARM_LEVEL (t);
      idx = TEMPLATE_PARM_IDX (t);
    }
  else
    {
      level = TEMPLATE_TYPE_LEVEL (t);
      idx = TEMPLATE_TYPE_IDX (t);
    }

  if (level == tpd->level)
    tpd->parms[idx] = 1;

  /* Return zero so that for_each_template_parm will continue the
     traversal of the tree; we want to mark *every* template parm.  */
  return 0;
}

1593
/* Creates a TEMPLATE_DECL for the indicated DECL using the template
jason's avatar
jason committed
1594 1595
   parameters given by current_template_args, or reuses a
   previously existing one, if appropriate.  Returns the DECL, or an
1596 1597 1598
   equivalent one, if it is replaced via a call to duplicate_decls.  

   If IS_FRIEND is non-zero, DECL is a friend declaration.  */
1599 1600

tree
1601
push_template_decl_real (decl, is_friend)
mrs's avatar
mrs committed
1602
     tree decl;
1603
     int is_friend;
mrs's avatar
mrs committed
1604 1605
{
  tree tmpl;
jason's avatar
jason committed
1606
  tree args;
mrs's avatar
mrs committed
1607
  tree info;
jason's avatar
jason committed
1608 1609
  tree ctx;
  int primary;
1610 1611

  is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
jason's avatar
jason committed
1612 1613 1614 1615 1616

  if (is_friend)
    /* For a friend, we want the context of the friend function, not
       the type of which it is a friend.  */
    ctx = DECL_CONTEXT (decl);
jason's avatar
jason committed
1617 1618
  else if (DECL_REAL_CONTEXT (decl)
	   && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL)
jason's avatar
jason committed
1619 1620 1621 1622 1623 1624 1625 1626
    /* In the case of a virtual function, we want the class in which
       it is defined.  */
    ctx = DECL_REAL_CONTEXT (decl);
  else
    /* Otherwise, if we're currently definining some class, the DECL
       is assumed to be a member of the class.  */
    ctx = current_class_type;

jason's avatar
jason committed
1627 1628 1629 1630 1631 1632
  if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
    ctx = NULL_TREE;

  if (!DECL_CONTEXT (decl))
    DECL_CONTEXT (decl) = current_namespace;

1633 1634 1635
  /* For determining whether this is a primary template or not, we're really
     interested in the lexical context, not the true context.  */
  if (is_friend)
1636 1637 1638
    /* For a TYPE_DECL, there is no DECL_CLASS_CONTEXT.  */
    info = TREE_CODE (decl) == FUNCTION_DECL 
      ? DECL_CLASS_CONTEXT (decl) : current_class_type;
1639 1640 1641 1642 1643
  else
    info = ctx;

  if (info && TREE_CODE (info) == FUNCTION_DECL)
    primary = 0;
1644 1645 1646
  /* Note that template_class_depth returns 0 if given NULL_TREE, so
     this next line works even when we are at global scope.  */
  else if (processing_template_decl > template_class_depth (info))
mrs's avatar
mrs committed
1647
    primary = 1;
jason's avatar
jason committed
1648 1649
  else
    primary = 0;
mrs's avatar
mrs committed
1650

1651 1652 1653 1654 1655 1656 1657 1658
  if (primary)
    {
      if (current_lang_name == lang_name_c)
	cp_error ("template with C linkage");
      if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl)))
	cp_error ("template class without a name");
    }

mrs's avatar
mrs committed
1659
  /* Partial specialization.  */
mrs's avatar
mrs committed
1660
  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
mrs's avatar
mrs committed
1661 1662 1663 1664 1665 1666 1667
      && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
    {
      tree type = TREE_TYPE (decl);
      tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
      tree mainargs = CLASSTYPE_TI_ARGS (type);
      tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
      /* We check that each of the template parameters given in the
	 partial specialization is used in the argument list to the
	 specialization.  For example:
	 
	   template <class T> struct S;
	   template <class T> struct S<T*>;

	 The second declaration is OK because `T*' uses the template
	 parameter T, whereas
       
           template <class T> struct S<int>;

	 is no good.  Even trickier is:

	   template <class T>
	   struct S1
	   {
	      template <class U>
	      struct S2;
	      template <class U>
	      struct S2<T>;
	   };
	   
	 The S2<T> declaration is actually illegal; it is a
	 full-specialization.  Of course, 

              template <class U>
              struct S2<T (*)(U)>;

         or some such would have been OK.  */
      int  i;
      struct template_parm_data tpd;
      int ntparms = TREE_VEC_LENGTH (TREE_VALUE (current_template_parms));
      int did_error_intro = 0;

      tpd.level = TREE_INT_CST_HIGH (TREE_PURPOSE (current_template_parms));
      tpd.parms = alloca (sizeof (int) * ntparms);
      for (i = 0; i < ntparms; ++i)
	tpd.parms[i] = 0;
      for (i = 0; i < TREE_VEC_LENGTH (mainargs); ++i)
	for_each_template_parm (TREE_VEC_ELT (mainargs, i),
				&mark_template_parm,
				&tpd);
      for (i = 0; i < ntparms; ++i)
	if (tpd.parms[i] == 0)
	  {
	    /* One of the template parms was not used in the
	       specialization.  */
	    if (!did_error_intro)
	      {
		cp_error ("template parameters not used in partial specialization:");
		did_error_intro = 1;
	      }

	    cp_error ("        `%D'", 
		      TREE_VALUE (TREE_VEC_ELT 
				  (TREE_VALUE (current_template_parms),
				   i)));
	  }

mrs's avatar
mrs committed
1728 1729 1730 1731 1732
      for (; spec; spec = TREE_CHAIN (spec))
	{
	  /* purpose: args to main template
	     value: spec template */
	  if (comp_template_args (TREE_PURPOSE (spec), mainargs))
1733
	    return decl;
mrs's avatar
mrs committed
1734 1735
	}

mrs's avatar
mrs committed
1736 1737 1738
      DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
	= perm_tree_cons (mainargs, TREE_VALUE (current_template_parms),
			  DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
mrs's avatar
mrs committed
1739
      TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
1740
      return decl;
mrs's avatar
mrs committed
1741 1742
    }

mrs's avatar
mrs committed
1743 1744
  args = current_template_args ();

jason's avatar
jason committed
1745 1746 1747 1748
  if (!ctx 
      || TREE_CODE (ctx) == FUNCTION_DECL
      || TYPE_BEING_DEFINED (ctx)
      || (is_friend && !DECL_TEMPLATE_INFO (decl)))
mrs's avatar
mrs committed
1749
    {
1750
      if (DECL_LANG_SPECIFIC (decl)
jason's avatar
jason committed
1751 1752 1753 1754
	  && DECL_TEMPLATE_INFO (decl)
	  && DECL_TI_TEMPLATE (decl))
	tmpl = DECL_TI_TEMPLATE (decl);
      else
1755
	{
jason's avatar
jason committed
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
	  tmpl = build_template_decl (decl, current_template_parms);
	  
	  if (DECL_LANG_SPECIFIC (decl)
	      && DECL_TEMPLATE_SPECIALIZATION (decl))
	    {
	      /* A specialization of a member template of a template
		 class. */
	      SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
	      DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
	      DECL_TEMPLATE_INFO (decl) = NULL_TREE;
	    }
1767
	}
mrs's avatar
mrs committed
1768 1769 1770
    }
  else
    {
mrs's avatar
mrs committed
1771
      tree t;
1772
      tree a;
mrs's avatar
mrs committed
1773

mrs's avatar
mrs committed
1774 1775 1776
      if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
	cp_error ("must specialize `%#T' before defining member `%#D'",
		  ctx, decl);
mrs's avatar
mrs committed
1777
      if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
mrs's avatar
mrs committed
1778
	tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
mrs's avatar
mrs committed
1779
      else if (! DECL_TEMPLATE_INFO (decl))
mrs's avatar
mrs committed
1780 1781
	{
	  cp_error ("template definition of non-template `%#D'", decl);
1782
	  return decl;
mrs's avatar
mrs committed
1783
	}
mrs's avatar
mrs committed
1784
      else
mrs's avatar
mrs committed
1785
	tmpl = DECL_TI_TEMPLATE (decl);
1786 1787 1788
      
      if (is_member_template (tmpl))
	{
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
	  if (DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) 
	      && DECL_TEMPLATE_SPECIALIZATION (decl))
	    {
	      tree new_tmpl;

	      /* The declaration is a specialization of a member
		 template, declared outside the class.  Therefore, the
		 innermost template arguments will be NULL, so we
		 replace them with the arguments determined by the
		 earlier call to check_explicit_specialization.  */
	      args = DECL_TI_ARGS (decl);

	      new_tmpl 
		= build_template_decl (decl, current_template_parms);
	      DECL_TEMPLATE_RESULT (new_tmpl) = decl;
	      TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
	      DECL_TI_TEMPLATE (decl) = new_tmpl;
	      SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
	      DECL_TEMPLATE_INFO (new_tmpl) = 
		perm_tree_cons (tmpl, args, NULL_TREE);

	      register_specialization (new_tmpl, tmpl, args);
1811
	      return decl;
1812 1813
	    }
	  
1814 1815
	  a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
	  t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
jason's avatar
jason committed
1816
	  if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	    {
	      cp_error ("got %d template parameters for `%#D'",
			TREE_VEC_LENGTH (a), decl);
	      cp_error ("  but %d required", TREE_VEC_LENGTH (t));
	    }
	  if (TREE_VEC_LENGTH (args) > 1)
	    /* Get the template parameters for the enclosing template
	       class.  */ 
	    a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2);
	  else
	    a = NULL_TREE;
	}
      else 
	a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);

      t = NULL_TREE;
mrs's avatar
mrs committed
1833 1834

      if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
	{
	  /* When processing an inline member template of a
	     specialized class, there is no CLASSTYPE_TI_SPEC_INFO.  */
	  if (CLASSTYPE_TI_SPEC_INFO (ctx))
	    t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx));
	}
      else if (CLASSTYPE_TEMPLATE_INFO (ctx))
	t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx));

      /* There should be template arguments if and only if there is a
	 template class.  */
      my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0);
mrs's avatar
mrs committed
1847

1848 1849
      if (t != NULL_TREE 
	  && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
mrs's avatar
mrs committed
1850 1851
	{
	  cp_error ("got %d template parameters for `%#D'",
1852
		    TREE_VEC_LENGTH (a), decl);
mrs's avatar
mrs committed
1853 1854
	  cp_error ("  but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
	}
mrs's avatar
mrs committed
1855
    }
1856 1857 1858 1859 1860 1861 1862 1863 1864
  /* Get the innermost set of template arguments.  We don't do this
     for a non-template member function of a nested template class
     because there we will never get a `partial instantiation' of the
     function containing the outer arguments, and so we must save all
     of the arguments here.  */
  if (TREE_CODE (decl) != FUNCTION_DECL 
      || template_class_depth (ctx) <= 1
      || primary)
    args = innermost_args (args, 0);
mrs's avatar
mrs committed
1865

mrs's avatar
mrs committed
1866 1867
  DECL_TEMPLATE_RESULT (tmpl) = decl;
  TREE_TYPE (tmpl) = TREE_TYPE (decl);
mrs's avatar
mrs committed
1868

1869 1870 1871 1872
  if (! ctx && !(is_friend && template_class_depth (info) > 0))
    /* Note that we do not try to push a global template friend
       declared in a template class; such a thing may well depend on
       the template parameters of the class.  */
jason's avatar
jason committed
1873
    tmpl = pushdecl_namespace_level (tmpl);
mrs's avatar
mrs committed
1874

mrs's avatar
mrs committed
1875
  if (primary)
1876
    DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
mrs's avatar
mrs committed
1877 1878 1879

  info = perm_tree_cons (tmpl, args, NULL_TREE);

mrs's avatar
mrs committed
1880
  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
mrs's avatar
mrs committed
1881
    {
mrs's avatar
mrs committed
1882
      CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
1883 1884
      if (!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
	DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
mrs's avatar
mrs committed
1885
    }
mrs's avatar
mrs committed
1886 1887
  else if (! DECL_LANG_SPECIFIC (decl))
    cp_error ("template declaration of `%#D'", decl);
mrs's avatar
mrs committed
1888
  else
mrs's avatar
mrs committed
1889
    DECL_TEMPLATE_INFO (decl) = info;
1890 1891

  return DECL_TEMPLATE_RESULT (tmpl);
mrs's avatar
mrs committed
1892 1893
}

1894 1895 1896 1897 1898 1899 1900 1901 1902
tree
push_template_decl (decl)
     tree decl;
{
  return push_template_decl_real (decl, 0);
}

/* Called when a class template TYPE is redeclared with the indicated
   template PARMS, e.g.:
1903 1904 1905 1906 1907

     template <class T> struct S;
     template <class T> struct S {};  */

void 
1908
redeclare_class_template (type, parms)
1909
     tree type;
1910
     tree parms;
1911 1912
{
  tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1913
  tree tmpl_parms;
1914 1915 1916 1917 1918 1919 1920 1921
  int i;

  if (!PRIMARY_TEMPLATE_P (tmpl))
    /* The type is nested in some template class.  Nothing to worry
       about here; there are no new template parameters for the nested
       type.  */
    return;

1922 1923 1924
  parms = INNERMOST_TEMPLATE_PARMS (parms);
  tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);

1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
  if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
    {
      cp_error_at ("previous declaration `%D'", tmpl);
      cp_error ("used %d template parameter%s instead of %d",
		TREE_VEC_LENGTH (tmpl_parms), 
		TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
		TREE_VEC_LENGTH (parms));
      return;
    }

  for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
    {
      tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
      tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));

      if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
	{
	  cp_error_at ("template parameter `%#D'", tmpl_parm);
	  cp_error ("redeclared here as `%#D'", parm);
	  return;
	}

      if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
	{
	  /* We have in [temp.param]:

	     A template-parameter may not be given default arguments
	     by two different declarations in the same scope.  */
	  cp_error ("redefinition of default argument for `%#D'", parm);
1956
	  cp_error_at ("  original definition appeared here", tmpl_parm);
1957 1958 1959 1960 1961 1962 1963 1964 1965
	  return;
	}

      if (parm_default != NULL_TREE)
	/* Update the previous template parameters (which are the ones
	   that will really count) with the new default value.  */
	TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
    }
}
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977

/* Attempt to convert the non-type template parameter EXPR to the
   indicated TYPE.  If the conversion is successful, return the
   converted value.  If the conversion is unsuccesful, return
   NULL_TREE if we issued an error message, or error_mark_node if we
   did not.  We issue error messages for out-and-out bad template
   parameters, but not simply because the conversion failed, since we
   might be just trying to do argument deduction.  By the time this
   function is called, neither TYPE nor EXPR may make use of template
   parameters.  */

static tree
1978
convert_nontype_argument (type, expr)
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
     tree type;
     tree expr;
{
  tree expr_type = TREE_TYPE (expr);

  /* A template-argument for a non-type, non-template
     template-parameter shall be one of:

     --an integral constant-expression of integral or enumeration
     type; or
     
     --the name of a non-type template-parameter; or
     
     --the name of an object or function with external linkage,
     including function templates and function template-ids but
1994
     excluding non-static class members, expressed as id-expression;
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
     or
     
     --the address of an object or function with external linkage,
     including function templates and function template-ids but
     excluding non-static class members, expressed as & id-expression
     where the & is optional if the name refers to a function or
     array; or
     
     --a pointer to member expressed as described in _expr.unary.op_.  */

2005 2006 2007 2008 2009
  /* An integral constant-expression can include const variables
     or enumerators.  */
  if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr))
    expr = decl_constant_value (expr);

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
  if (is_overloaded_fn (expr))
    /* OK for now.  We'll check that it has external linkage later.
       Check this first since if expr_type is the unknown_type_node
       we would otherwise complain below.  */
    ;
  else if (INTEGRAL_TYPE_P (expr_type) 
	   || TYPE_PTRMEM_P (expr_type) 
	   || TYPE_PTRMEMFUNC_P (expr_type)
	   /* The next two are g++ extensions.  */
	   || TREE_CODE (expr_type) == REAL_TYPE
	   || TREE_CODE (expr_type) == COMPLEX_TYPE)
2021
    {
2022
      if (! TREE_CONSTANT (expr))
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
	{
	  cp_error ("non-constant `%E' cannot be used as template argument",
		    expr);
	  return NULL_TREE;
	}
    }
  else if (TYPE_PTR_P (expr_type) 
	   /* If expr is the address of an overloaded function, we
	      will get the unknown_type_node at this point.  */
	   || expr_type == unknown_type_node)
    {
      tree referent;
2035 2036
      tree e = expr;
      STRIP_NOPS (e);
2037

2038
      if (TREE_CODE (e) != ADDR_EXPR)
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
	{
	bad_argument:
	  cp_error ("`%E' is not a valid template argument", expr);
	  error ("it must be %s%s with external linkage",
		 TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
		 ? "a pointer to " : "",
		 TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE
		 ? "a function" : "an object");
	  return NULL_TREE;
	}

2050
      referent = TREE_OPERAND (e, 0);
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
      STRIP_NOPS (referent);
      
      if (TREE_CODE (referent) == STRING_CST)
	{
	  cp_error ("string literal %E is not a valid template argument", 
		    referent);
	  error ("because it is the address of an object with static linkage");
	  return NULL_TREE;
	}

      if (is_overloaded_fn (referent))
	/* We'll check that it has external linkage later.  */
	;
      else if (TREE_CODE (referent) != VAR_DECL)
	goto bad_argument;
      else if (!TREE_PUBLIC (referent))
	{
	  cp_error ("address of non-extern `%E' cannot be used as template argument", referent); 
	  return error_mark_node;
	}
    }
2072
  else if (TREE_CODE (expr) == VAR_DECL)
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
    {
      if (!TREE_PUBLIC (expr))
	goto bad_argument;
    }
  else 
    {
      cp_error ("object `%E' cannot be used as template argument", expr);
      return NULL_TREE;
    }

  switch (TREE_CODE (type))
    {
    case INTEGER_TYPE:
    case BOOLEAN_TYPE:
    case ENUMERAL_TYPE:
      /* For a non-type template-parameter of integral or enumeration
         type, integral promotions (_conv.prom_) and integral
         conversions (_conv.integral_) are applied. */
      if (!INTEGRAL_TYPE_P (expr_type))
	return error_mark_node;
      
      /* It's safe to call digest_init in this case; we know we're
	 just converting one integral constant expression to another.  */
      return digest_init (type, expr, (tree*) 0);

2098 2099 2100 2101 2102 2103 2104 2105
    case REAL_TYPE:
    case COMPLEX_TYPE:
      /* These are g++ extensions.  */
      if (TREE_CODE (expr_type) != TREE_CODE (type))
	return error_mark_node;

      return digest_init (type, expr, (tree*) 0);

2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
    case POINTER_TYPE:
      {
	tree type_pointed_to = TREE_TYPE (type);
 
	if (TYPE_PTRMEM_P (type))
	  /* For a non-type template-parameter of type pointer to data
	     member, qualification conversions (_conv.qual_) are
	     applied.  */
	  return perform_qualification_conversions (type, expr);
	else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
	  { 
	    /* For a non-type template-parameter of type pointer to
	       function, only the function-to-pointer conversion
	       (_conv.func_) is applied.  If the template-argument
	       represents a set of overloaded functions (or a pointer to
	       such), the matching function is selected from the set
	       (_over.over_).  */
	    tree fns;
	    tree fn;

2126
	    if (TREE_CODE (expr) == ADDR_EXPR)
2127 2128 2129 2130
	      fns = TREE_OPERAND (expr, 0);
	    else
	      fns = expr;

2131
	    fn = instantiate_type (type_pointed_to, fns, 0);
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159

	    if (fn == error_mark_node)
	      return error_mark_node;

	    if (!TREE_PUBLIC (fn))
	      {
		if (really_overloaded_fn (fns))
		  return error_mark_node;
		else
		  goto bad_argument;
	      }

	    expr = build_unary_op (ADDR_EXPR, fn, 0);

	    my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1), 
				0);
	    return expr;
	  }
	else 
	  {
	    /* For a non-type template-parameter of type pointer to
	       object, qualification conversions (_conv.qual_) and the
	       array-to-pointer conversion (_conv.array_) are applied.
	       [Note: In particular, neither the null pointer conversion
	       (_conv.ptr_) nor the derived-to-base conversion
	       (_conv.ptr_) are applied.  Although 0 is a valid
	       template-argument for a non-type template-parameter of
	       integral type, it is not a valid template-argument for a
2160 2161 2162 2163 2164
	       non-type template-parameter of pointer type.]  
	    
	       The call to decay_conversion performs the
	       array-to-pointer conversion, if appropriate.  */
	    expr = decay_conversion (expr);
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187

	    if (expr == error_mark_node)
	      return error_mark_node;
	    else
	      return perform_qualification_conversions (type, expr);
	  }
      }
      break;

    case REFERENCE_TYPE:
      {
	tree type_referred_to = TREE_TYPE (type);

	if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
	  {
	    /* For a non-type template-parameter of type reference to
	      function, no conversions apply.  If the
	      template-argument represents a set of overloaded
	      functions, the matching function is selected from the
	      set (_over.over_).  */
	    tree fns = expr;
	    tree fn;

2188
	    fn = instantiate_type (type_referred_to, fns, 0);
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235

	    if (!TREE_PUBLIC (fn))
	      {
		if (really_overloaded_fn (fns))
		  /* Don't issue an error here; we might get a different
		     function if the overloading had worked out
		     differently.  */
		  return error_mark_node;
		else
		  goto bad_argument;
	      }

	    if (fn == error_mark_node)
	      return error_mark_node;

	    my_friendly_assert (comptypes (type, TREE_TYPE (fn), 1),
				0);

	    return fn;
	  }
	else
	  {
	    /* For a non-type template-parameter of type reference to
	       object, no conversions apply.  The type referred to by the
	       reference may be more cv-qualified than the (otherwise
	       identical) type of the template-argument.  The
	       template-parameter is bound directly to the
	       template-argument, which must be an lvalue.  */
	    if (!comptypes (TYPE_MAIN_VARIANT (expr_type),
			    TYPE_MAIN_VARIANT (type), 1)
		|| (TYPE_READONLY (expr_type) >
		    TYPE_READONLY (type_referred_to))
		|| (TYPE_VOLATILE (expr_type) >
		    TYPE_VOLATILE (type_referred_to))
		|| !real_lvalue_p (expr))
	      return error_mark_node;
	    else
	      return expr;
	  }
      }
      break;

    case RECORD_TYPE:
      {
	tree fns;
	tree fn;

2236 2237 2238 2239 2240 2241 2242
	if (!TYPE_PTRMEMFUNC_P (type))
	  /* This handles templates like
	       template<class T, T t> void f();
	     when T is substituted with any class.  The second template
	     parameter becomes invalid and the template candidate is
	     rejected.  */
	  return error_mark_node;
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267

	/* For a non-type template-parameter of type pointer to member
	   function, no conversions apply.  If the template-argument
	   represents a set of overloaded member functions, the
	   matching member function is selected from the set
	   (_over.over_).  */

	if (!TYPE_PTRMEMFUNC_P (expr_type) && 
	    expr_type != unknown_type_node)
	  return error_mark_node;

	if (TREE_CODE (expr) == CONSTRUCTOR)
	  {
	    /* A ptr-to-member constant.  */
	    if (!comptypes (type, expr_type, 1))
	      return error_mark_node;
	    else 
	      return expr;
	  }

	if (TREE_CODE (expr) != ADDR_EXPR)
	  return error_mark_node;

	fns = TREE_OPERAND (expr, 0);
	
2268 2269
	fn = instantiate_type (TREE_TYPE (TREE_TYPE (type)), 
			       fns, 0);
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
	
	if (fn == error_mark_node)
	  return error_mark_node;

	expr = build_unary_op (ADDR_EXPR, fn, 0);
	
	my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1), 
			    0);
	return expr;
      }
      break;

    default:
      /* All non-type parameters must have one of these types.  */
      my_friendly_abort (0);
      break;
    }

  return error_mark_node;
}

mrs's avatar
mrs committed
2291 2292
/* Convert all template arguments to their appropriate types, and return
   a vector containing the resulting values.  If any error occurs, return
2293 2294 2295 2296 2297 2298 2299
   error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
   Some error messages are issued even if COMPLAIN is zero; for
   instance, if a template argument is composed from a local class. 

   If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
   provided in ARGLIST, or else trailing parameters must have default
   values.  If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
2300 2301 2302 2303 2304
   deduction for any unspecified trailing arguments.
   
   If IS_TMPL_PARM is non-zero,  we will coercing parameters of template 
   template arguments.  In this case, ARGLIST is a chain of TREE_LIST
   nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL.  */
mrs's avatar
mrs committed
2305

mrs's avatar
mrs committed
2306
static tree
2307 2308
coerce_template_parms (parms, arglist, in_decl,
		       complain,
2309 2310
		       require_all_arguments,
		       is_tmpl_parm)
mrs's avatar
mrs committed
2311 2312
     tree parms, arglist;
     tree in_decl;
2313 2314
     int complain;
     int require_all_arguments;
2315
     int is_tmpl_parm;
mrs's avatar
mrs committed
2316
{
mrs's avatar
mrs committed
2317
  int nparms, nargs, i, lost = 0;
2318
  tree vec = NULL_TREE;
mrs's avatar
mrs committed
2319

mrs's avatar
mrs committed
2320 2321 2322 2323
  if (arglist == NULL_TREE)
    nargs = 0;
  else if (TREE_CODE (arglist) == TREE_VEC)
    nargs = TREE_VEC_LENGTH (arglist);
mrs's avatar
mrs committed
2324
  else
mrs's avatar
mrs committed
2325 2326 2327 2328 2329 2330
    nargs = list_length (arglist);

  nparms = TREE_VEC_LENGTH (parms);

  if (nargs > nparms
      || (nargs < nparms
2331
	  && require_all_arguments
mrs's avatar
mrs committed
2332
	  && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
mrs's avatar
mrs committed
2333
    {
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
      if (complain) 
	{
	  error ("incorrect number of parameters (%d, should be %d)",
		 nargs, nparms);
	  
	  if (in_decl)
	    cp_error_at ("in template expansion for decl `%D'",
			 in_decl);
	}

mrs's avatar
mrs committed
2344 2345 2346
      return error_mark_node;
    }

2347 2348
  if (arglist && TREE_CODE (arglist) == TREE_VEC && nargs == nparms)
    vec = copy_node (arglist);
mrs's avatar
mrs committed
2349 2350 2351
  else
    {
      vec = make_tree_vec (nparms);
2352

mrs's avatar
mrs committed
2353 2354
      for (i = 0; i < nparms; i++)
	{
mrs's avatar
mrs committed
2355
	  tree arg;
2356
	  tree parm = TREE_VEC_ELT (parms, i);
mrs's avatar
mrs committed
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367

	  if (arglist)
	    {
	      arg = arglist;
	      arglist = TREE_CHAIN (arglist);

	      if (arg == error_mark_node)
		lost++;
	      else
		arg = TREE_VALUE (arg);
	    }
2368 2369 2370 2371 2372 2373
	  else if (is_tmpl_parm && i < nargs)
	    {
	      arg = TREE_VEC_ELT (arglist, i);
	      if (arg == error_mark_node)
		lost++;
	    }
2374 2375 2376 2377 2378 2379
	  else if (TREE_PURPOSE (parm) == NULL_TREE)
	    {
	      my_friendly_assert (!require_all_arguments, 0);
	      break;
	    }
	  else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
2380
	    arg = tsubst (TREE_PURPOSE (parm), vec, in_decl);
mrs's avatar
mrs committed
2381
	  else
2382
	    arg = tsubst_expr (TREE_PURPOSE (parm), vec, in_decl);
mrs's avatar
mrs committed
2383

mrs's avatar
mrs committed
2384 2385 2386 2387 2388 2389
	  TREE_VEC_ELT (vec, i) = arg;
	}
    }
  for (i = 0; i < nparms; i++)
    {
      tree arg = TREE_VEC_ELT (vec, i);
mrs's avatar
mrs committed
2390
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
mrs's avatar
mrs committed
2391
      tree val = 0;
2392
      int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
mrs's avatar
mrs committed
2393

2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
      if (is_tmpl_parm && i < nargs)
	{
	  /* In case we are checking arguments inside a template template
	     parameter, ARG that does not come from default argument is 
	     also a TREE_LIST node.  Note that ARG can also be a TREE_LIST
	     in other cases such as overloaded functions.  */
	  if (arg != NULL_TREE && arg != error_mark_node)
	    arg = TREE_VALUE (arg);
	}

2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
      if (arg == NULL_TREE)
	/* We're out of arguments.  */
	{
	  my_friendly_assert (!require_all_arguments, 0);
	  break;
	}

      if (arg == error_mark_node)
	{
	  cp_error ("template argument %d is invalid", i + 1);
	  lost++;
	  continue;
	}

2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
      if (TREE_CODE (arg) == TREE_LIST 
	  && TREE_TYPE (arg) != NULL_TREE
	  && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
	{  
	  /* The template argument was the name of some
	     member function.  That's usually
	     illegal, but static members are OK.  In any
	     case, grab the underlying fields/functions
	     and issue an error later if required.  */
	  arg = TREE_VALUE (arg);
	  TREE_TYPE (arg) = unknown_type_node;
	}
2430

2431 2432 2433 2434 2435 2436
      requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
      requires_type = TREE_CODE (parm) == TYPE_DECL
		      || requires_tmpl_type;

      /* Check if it is a class template.  If REQUIRES_TMPL_TYPE is true,
	 we also accept implicitly created TYPE_DECL as a valid argument.  */
2437 2438 2439
      is_tmpl_type = (TREE_CODE (arg) == TEMPLATE_DECL
		      && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
		     || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
2440 2441 2442 2443 2444 2445
			 && !CLASSTYPE_TEMPLATE_INFO (arg))
		     || (TREE_CODE (arg) == RECORD_TYPE
		         && CLASSTYPE_TEMPLATE_INFO (arg)
		         && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
			 && DECL_ARTIFICIAL (TYPE_NAME (arg))
			 && requires_tmpl_type);
2446 2447
      if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
	arg = TYPE_STUB_DECL (arg);
2448 2449
      else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
	arg = CLASSTYPE_TI_TEMPLATE (arg);
2450 2451 2452 2453 2454

      if (is_tmpl_parm && i < nargs)
	is_type = TREE_CODE (arg) == TYPE_DECL || is_tmpl_type;
      else
	is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type;
mrs's avatar
mrs committed
2455 2456 2457 2458 2459 2460

      if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
	  && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
	{
	  cp_pedwarn ("to refer to a type member of a template parameter,");
	  cp_pedwarn ("  use `typename %E'", arg);
2461

mrs's avatar
mrs committed
2462 2463 2464 2465
	  arg = make_typename_type (TREE_OPERAND (arg, 0),
				    TREE_OPERAND (arg, 1));
	  is_type = 1;
	}
mrs's avatar
mrs committed
2466 2467 2468
      if (is_type != requires_type)
	{
	  if (in_decl)
mrs's avatar
mrs committed
2469
	    {
2470 2471 2472 2473 2474 2475
	      if (complain)
		{
		  cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
			    i + 1, in_decl);
		  if (is_type)
		    cp_error ("  expected a constant of type `%T', got `%T'",
2476 2477
			      TREE_TYPE (parm),
			      (is_tmpl_type ? DECL_NAME (arg) : arg));
2478 2479 2480
		  else
		    cp_error ("  expected a type, got `%E'", arg);
		}
mrs's avatar
mrs committed
2481
	    }
mrs's avatar
mrs committed
2482 2483 2484 2485
	  lost++;
	  TREE_VEC_ELT (vec, i) = error_mark_node;
	  continue;
	}
2486 2487 2488 2489 2490
      if (is_tmpl_type ^ requires_tmpl_type)
	{
	  if (in_decl)
	    {
	      cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2491
			i + 1, in_decl);
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511
	      if (is_tmpl_type)
		cp_error ("  expected a type, got `%T'", DECL_NAME (arg));
	      else
		cp_error ("  expected a class template, got `%T'", arg);
	    }
	  lost++;
	  TREE_VEC_ELT (vec, i) = error_mark_node;
	  continue;
	}
      if (is_tmpl_parm)
	{
	  if (requires_tmpl_type)
	    {
	      cp_error ("nested template template parameter not implemented");
	      lost++;
	      TREE_VEC_ELT (vec, i) = error_mark_node;
	    }
	  continue;
	}
        
mrs's avatar
mrs committed
2512
      if (is_type)
mrs's avatar
mrs committed
2513
	{
2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526
	  if (requires_tmpl_type)
	    {
	      tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
	      tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);

	      /* The parameter and argument roles have to be switched 
		 here in order to handle default arguments properly.  
		 For example, 
		   template<template <class> class TT> void f(TT<int>) 
		 should be able to accept vector<int> which comes from 
		   template <class T, class Allcator = allocator> 
		   class vector.  */

2527
	      val = coerce_template_parms (argparm, parmparm, in_decl, 1, 1, 1);
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
	      if (val != error_mark_node)
		val = arg;
		    
	      /* TEMPLATE_TEMPLATE_PARM node is preferred over 
		 TEMPLATE_DECL.  */
	      if (val != error_mark_node 
		  && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
		val = TREE_TYPE (val);
	    }
	  else
mrs's avatar
mrs committed
2538
	    {
2539 2540
	      val = groktypename (arg);
	      if (! processing_template_decl)
mrs's avatar
mrs committed
2541
		{
2542 2543 2544 2545 2546 2547 2548 2549 2550
		  tree t = target_type (val);
		  if (TREE_CODE (t) != TYPENAME_TYPE 
		      && IS_AGGR_TYPE (t)
		      && decl_function_context (TYPE_MAIN_DECL (t)))
		    {
		      cp_error ("type `%T' composed from a local class is not a valid template-argument",
				val);
		      return error_mark_node;
		    }
mrs's avatar
mrs committed
2551 2552 2553
		}
	    }
	}
mrs's avatar
mrs committed
2554 2555
      else
	{
2556
	  tree t = tsubst (TREE_TYPE (parm), vec, in_decl);
2557

2558
	  if (processing_template_decl)
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571
	    arg = maybe_fold_nontype_arg (arg);

	  if (!uses_template_parms (arg) && !uses_template_parms (t))
	    /* We used to call digest_init here.  However, digest_init
	       will report errors, which we don't want when complain
	       is zero.  More importantly, digest_init will try too
	       hard to convert things: for example, `0' should not be
	       converted to pointer type at this point according to
	       the standard.  Accepting this is not merely an
	       extension, since deciding whether or not these
	       conversions can occur is part of determining which
	       function template to call, or whether a given epxlicit
	       argument specification is legal.  */
2572
	    val = convert_nontype_argument (t, arg);
mrs's avatar
mrs committed
2573
	  else
2574 2575
	    val = arg;

2576 2577 2578 2579 2580
	  if (val == NULL_TREE)
	    val = error_mark_node;
	  else if (val == error_mark_node && complain)
	    cp_error ("could not convert template argument `%E' to `%T'", 
		      arg, t);
mrs's avatar
mrs committed
2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
	}

      if (val == error_mark_node)
	lost++;

      TREE_VEC_ELT (vec, i) = val;
    }
  if (lost)
    return error_mark_node;
  return vec;
}

jason's avatar
jason committed
2593 2594 2595
/* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets
   of template arguments.  Returns 0 otherwise.  */

2596
int
mrs's avatar
mrs committed
2597 2598 2599 2600 2601
comp_template_args (oldargs, newargs)
     tree oldargs, newargs;
{
  int i;

2602 2603 2604
  if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    return 0;

mrs's avatar
mrs committed
2605 2606 2607 2608 2609 2610 2611 2612 2613
  for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
    {
      tree nt = TREE_VEC_ELT (newargs, i);
      tree ot = TREE_VEC_ELT (oldargs, i);

      if (nt == ot)
	continue;
      if (TREE_CODE (nt) != TREE_CODE (ot))
	return 0;
2614 2615 2616 2617 2618 2619 2620
      if (TREE_CODE (nt) == TREE_VEC)
        {
          /* For member templates */
	  if (comp_template_args (nt, ot))
	    continue;
        }
      else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
mrs's avatar
mrs committed
2621 2622 2623 2624 2625
	{
	  if (comptypes (ot, nt, 1))
	    continue;
	}
      else if (cp_tree_equal (ot, nt) > 0)
mrs's avatar
mrs committed
2626 2627 2628 2629 2630 2631
	continue;
      return 0;
    }
  return 1;
}

mrs's avatar
mrs committed
2632 2633
/* Given class template name and parameter list, produce a user-friendly name
   for the instantiation.  */
mrs's avatar
mrs committed
2634

mrs's avatar
mrs committed
2635
static char *
2636
mangle_class_name_for_template (name, parms, arglist, ctx)
mrs's avatar
mrs committed
2637 2638
     char *name;
     tree parms, arglist;
2639
     tree ctx;
mrs's avatar
mrs committed
2640 2641 2642 2643 2644 2645
{
  static struct obstack scratch_obstack;
  static char *scratch_firstobj;
  int i, nparms;

  if (!scratch_firstobj)
mrs's avatar
mrs committed
2646
    gcc_obstack_init (&scratch_obstack);
mrs's avatar
mrs committed
2647 2648
  else
    obstack_free (&scratch_obstack, scratch_firstobj);
mrs's avatar
mrs committed
2649
  scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
mrs's avatar
mrs committed
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663

#if 0
#define buflen	sizeof(buf)
#define check	if (bufp >= buf+buflen-1) goto too_long
#define ccat(c) *bufp++=(c); check
#define advance	bufp+=strlen(bufp); check
#define cat(s)	strncpy(bufp, s, buf+buflen-bufp-1); advance
#else
#define check
#define ccat(c)	obstack_1grow (&scratch_obstack, (c));
#define advance
#define cat(s)	obstack_grow (&scratch_obstack, (s), strlen (s))
#endif

jason's avatar
jason committed
2664
  if (ctx && ctx != global_namespace)
2665
    {
2666 2667 2668
      char* s;

      if (TREE_CODE (ctx) == FUNCTION_DECL)
2669
	s = fndecl_as_string (ctx, 0);
2670
      else if (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
2671
	s = type_as_string_real (ctx, 0, 1);
jason's avatar
jason committed
2672 2673
      else if (TREE_CODE (ctx) == NAMESPACE_DECL)
	s = decl_as_string (ctx, 0);
2674 2675
      else
	my_friendly_abort (0);
2676 2677 2678
      cat (s);
      cat ("::");
    }
mrs's avatar
mrs committed
2679 2680 2681 2682 2683 2684
  cat (name);
  ccat ('<');
  nparms = TREE_VEC_LENGTH (parms);
  my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
  for (i = 0; i < nparms; i++)
    {
mrs's avatar
mrs committed
2685 2686
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      tree arg = TREE_VEC_ELT (arglist, i);
mrs's avatar
mrs committed
2687 2688 2689 2690

      if (i)
	ccat (',');

mrs's avatar
mrs committed
2691
      if (TREE_CODE (parm) == TYPE_DECL)
mrs's avatar
mrs committed
2692
	{
2693
	  cat (type_as_string_real (arg, 0, 1));
mrs's avatar
mrs committed
2694 2695
	  continue;
	}
2696 2697 2698
      else if (TREE_CODE (parm) == TEMPLATE_DECL)
	{
	  if (TREE_CODE (arg) == TEMPLATE_DECL)
jason's avatar
jason committed
2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
	    {
	      /* Already substituted with real template.  Just output 
		 the template name here */
	      my_friendly_assert (TREE_CODE (DECL_CONTEXT (arg)) 
				  == NAMESPACE_DECL, 980422);
	      if (DECL_CONTEXT (arg) != global_namespace)
		{
		  cat(decl_as_string (DECL_CONTEXT (arg), 0));
		  cat("::");
		}
	      cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
	    }
2711 2712
	  else
	    /* Output the parameter declaration */
2713
	    cat (type_as_string_real (arg, 0, 1));
2714 2715
	  continue;
	}
mrs's avatar
mrs committed
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
      else
	my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);

      if (TREE_CODE (arg) == TREE_LIST)
	{
	  /* New list cell was built because old chain link was in
	     use.  */
	  my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
	  arg = TREE_VALUE (arg);
	}
      /* No need to check arglist against parmlist here; we did that
	 in coerce_template_parms, called from lookup_template_class.  */
      cat (expr_as_string (arg, 0));
    }
  {
    char *bufp = obstack_next_free (&scratch_obstack);
    int offset = 0;
    while (bufp[offset - 1] == ' ')
      offset--;
    obstack_blank_fast (&scratch_obstack, offset);

    /* B<C<char> >, not B<C<char>> */
    if (bufp[offset - 1] == '>')
      ccat (' ');
  }
  ccat ('>');
  ccat ('\0');
  return (char *) obstack_base (&scratch_obstack);

mrs's avatar
mrs committed
2745
#if 0
mrs's avatar
mrs committed
2746
 too_long:
mrs's avatar
mrs committed
2747
#endif
mrs's avatar
mrs committed
2748 2749 2750 2751 2752
  fatal ("out of (preallocated) string space creating template instantiation name");
  /* NOTREACHED */
  return NULL;
}

mrs's avatar
mrs committed
2753
static tree
mrs's avatar
mrs committed
2754 2755 2756 2757 2758 2759 2760
classtype_mangled_name (t)
     tree t;
{
  if (CLASSTYPE_TEMPLATE_INFO (t)
      && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
    {
      tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
2761 2762 2763 2764 2765 2766 2767 2768 2769
      /* We do not pass in the context here since that is only needed
	 when mangling the name of instantiations, not the primary
	 template declaration.  In reality, it should not be needed
	 then either, but the way lookup_template_class operates
	 requires the context for the moment.  In the long run,
	 lookup_template_class should not be looking for existing
	 instantiations by matching mangled names, but rather by
	 matching the templates, and then scanning the instantiation
	 list.  */
mrs's avatar
mrs committed
2770 2771
      char *mangled_name = mangle_class_name_for_template
	(IDENTIFIER_POINTER (name),
2772
	 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
2773
	 CLASSTYPE_TI_ARGS (t), NULL_TREE);
mrs's avatar
mrs committed
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785
      tree id = get_identifier (mangled_name);
      IDENTIFIER_TEMPLATE (id) = name;
      return id;
    }
  else
    return TYPE_IDENTIFIER (t);
}

static void
add_pending_template (d)
     tree d;
{
mrs's avatar
mrs committed
2786 2787 2788 2789 2790 2791 2792
  tree ti;

  if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
    ti = CLASSTYPE_TEMPLATE_INFO (d);
  else
    ti = DECL_TEMPLATE_INFO (d);

mrs's avatar
mrs committed
2793
  if (TI_PENDING_TEMPLATE_FLAG (ti))
mrs's avatar
mrs committed
2794 2795 2796
    return;

  *template_tail = perm_tree_cons
jason's avatar
jason committed
2797
    (build_srcloc_here (), d, NULL_TREE);
mrs's avatar
mrs committed
2798
  template_tail = &TREE_CHAIN (*template_tail);
mrs's avatar
mrs committed
2799
  TI_PENDING_TEMPLATE_FLAG (ti) = 1;
mrs's avatar
mrs committed
2800 2801
}

2802 2803 2804 2805 2806 2807 2808 2809 2810

/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
   may be either a _DECL or an overloaded function or an
   IDENTIFIER_NODE), and ARGLIST.  */

tree
lookup_template_function (fns, arglist)
     tree fns, arglist;
{
jason's avatar
jason committed
2811
  tree type;
2812

2813 2814 2815 2816 2817 2818 2819
  if (fns == NULL_TREE)
    {
      cp_error ("non-template used as template");
      return error_mark_node;
    }

  if (arglist != NULL_TREE && !TREE_PERMANENT (arglist))
2820
    copy_to_permanent (arglist);
2821

jason's avatar
jason committed
2822 2823 2824 2825 2826
  type = TREE_TYPE (fns);
  if (TREE_CODE (fns) == OVERLOAD || !type)
    type = unknown_type_node;

  return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);  
2827 2828
}

2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
/* Within the scope of a template class S<T>, the name S gets bound
   (in build_self_reference) to a TYPE_DECL for the class, not a
   TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
   or one of its enclosing classes, and that type is a template,
   return the associated TEMPLATE_DECL.  Otherwise, the original
   DECL is returned.  */

tree
maybe_get_template_decl_from_type_decl (decl)
     tree decl;
{
  return (decl != NULL_TREE
	  && TREE_CODE (decl) == TYPE_DECL 
	  && DECL_ARTIFICIAL (decl)
	  && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) 
    ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
}
2846

mrs's avatar
mrs committed
2847 2848 2849 2850 2851 2852 2853 2854
/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
   parameters, find the desired type.

   D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
   Since ARGLIST is build on the decl_obstack, we must copy it here
   to keep it from being reclaimed when the decl storage is reclaimed.

   IN_DECL, if non-NULL, is the template declaration we are trying to
2855 2856 2857 2858 2859
   instantiate.  

   If the template class is really a local class in a template
   function, then the FUNCTION_CONTEXT is the function in which it is
   being instantiated.  */
mrs's avatar
mrs committed
2860

mrs's avatar
mrs committed
2861
tree
2862
lookup_template_class (d1, arglist, in_decl, context)
mrs's avatar
mrs committed
2863 2864
     tree d1, arglist;
     tree in_decl;
2865
     tree context;
mrs's avatar
mrs committed
2866
{
2867
  tree template = NULL_TREE, parmlist;
mrs's avatar
mrs committed
2868
  char *mangled_name;
jason's avatar
jason committed
2869
  tree id, t, id_context;
mrs's avatar
mrs committed
2870 2871 2872

  if (TREE_CODE (d1) == IDENTIFIER_NODE)
    {
2873 2874 2875 2876 2877
      if (IDENTIFIER_LOCAL_VALUE (d1) 
	  && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1)))
	template = IDENTIFIER_LOCAL_VALUE (d1);
      else
	{
jason's avatar
jason committed
2878 2879
	  if (context)
	    push_decl_namespace (context);
2880 2881 2882 2883
	  if (current_class_type != NULL_TREE)
	    template = 
	      maybe_get_template_decl_from_type_decl
	      (IDENTIFIER_CLASS_VALUE (d1));
2884
	  if (template == NULL_TREE)
jason's avatar
jason committed
2885 2886 2887
	    template = lookup_name_nonclass (d1);
	  if (context)
	    pop_decl_namespace ();
2888
	}
2889 2890
      if (template)
	context = DECL_CONTEXT (template);
mrs's avatar
mrs committed
2891
    }
mrs's avatar
mrs committed
2892 2893
  else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
    {
2894 2895
      if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)) == NULL_TREE)
	return error_mark_node;
mrs's avatar
mrs committed
2896 2897 2898
      template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
      d1 = DECL_NAME (template);
    }
mrs's avatar
mrs committed
2899 2900 2901 2902 2903
  else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
    {
      template = CLASSTYPE_TI_TEMPLATE (d1);
      d1 = DECL_NAME (template);
    }
2904 2905 2906 2907 2908 2909 2910
  else if (TREE_CODE (d1) == TEMPLATE_DECL
	   && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
    {
      template = d1;
      d1 = DECL_NAME (template);
      context = DECL_CONTEXT (template);
    }
mrs's avatar
mrs committed
2911 2912
  else
    my_friendly_abort (272);
mrs's avatar
mrs committed
2913

jason's avatar
jason committed
2914 2915 2916 2917 2918 2919
  /* A namespace is used as context only for mangling.
     Template IDs with namespace context are found 
     in the global binding level.  */
  if (context != NULL_TREE && TREE_CODE (context) == NAMESPACE_DECL)
    context = global_namespace;

mrs's avatar
mrs committed
2920 2921 2922 2923 2924 2925
  /* With something like `template <class T> class X class X { ... };'
     we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
     We don't want to do that, but we have to deal with the situation, so
     let's give them some syntax errors to chew on instead of a crash.  */
  if (! template)
    return error_mark_node;
jason's avatar
jason committed
2926 2927 2928 2929 2930 2931 2932 2933 2934 2935

  /* We need an id_context to get the mangling right. If this is a
     nested template, use the context. If it is global, retrieve the
     context from the template.  */
  if (context && TREE_CODE (context) != NAMESPACE_DECL)
    id_context = context;
  else
    id_context = DECL_CONTEXT (template);
  my_friendly_assert (id_context != NULL_TREE, 980410);

mrs's avatar
mrs committed
2936 2937 2938 2939 2940 2941 2942 2943
  if (TREE_CODE (template) != TEMPLATE_DECL)
    {
      cp_error ("non-template type `%T' used as a template", d1);
      if (in_decl)
	cp_error_at ("for template declaration `%D'", in_decl);
      return error_mark_node;
    }

2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
  if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
    {
      /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
         template arguments */

      tree parm = copy_template_template_parm (TREE_TYPE (template));
      tree template2 = TYPE_STUB_DECL (parm);
      tree arglist2;

      CLASSTYPE_GOT_SEMICOLON (parm) = 1;
      parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);

2956
      arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1, 0);
2957 2958 2959 2960 2961 2962 2963 2964 2965
      if (arglist2 == error_mark_node)
	return error_mark_node;

      arglist2 = copy_to_permanent (arglist2);
      CLASSTYPE_TEMPLATE_INFO (parm)
	= perm_tree_cons (template2, arglist2, NULL_TREE);
      TYPE_SIZE (parm) = 0;
      return parm;
    }
2966 2967 2968
  else if (PRIMARY_TEMPLATE_P (template)
	   || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template))) 
	       == FUNCTION_DECL))
mrs's avatar
mrs committed
2969
    {
2970 2971
      tree arglist_for_mangling;

2972
      parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
mrs's avatar
mrs committed
2973

2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999
      if (/* ARGLIST can be NULL_TREE if there are default arguments.  */
	  arglist != NULL_TREE
	  && TREE_CODE (arglist) == TREE_VEC 
	  && TREE_VEC_LENGTH (arglist) > 1
	  && list_length (DECL_TEMPLATE_PARMS (template)) > 1)
	{
	  /* We have multiple levels of arguments to coerce, at once.  */
	  tree new_args = 
	    make_tree_vec (list_length (DECL_TEMPLATE_PARMS (template)));
	  int i;
	  
	  for (i = TREE_VEC_LENGTH (arglist) - 1, 
		 t = DECL_TEMPLATE_PARMS (template); 
	       i >= 0 && t != NULL_TREE;
	       --i, t = TREE_CHAIN (t))
	    TREE_VEC_ELT (new_args, i) =
	      coerce_template_parms (TREE_VALUE (t),
				     TREE_VEC_ELT (arglist, i),
				     template, 1, 1, 0);
	  arglist = new_args;
	}
      else
	arglist = coerce_template_parms (parmlist, 
					 innermost_args (arglist, 0),
					 template, 1, 1, 0);
     if (arglist == error_mark_node)
mrs's avatar
mrs committed
3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
	return error_mark_node;
      if (uses_template_parms (arglist))
	{
	  tree found;
	  if (comp_template_args
	      (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
	    found = TREE_TYPE (template);
	  else
	    {
	      for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
		   found; found = TREE_CHAIN (found))
		{
		  if (TI_USES_TEMPLATE_PARMS (found)
		      && comp_template_args (TREE_PURPOSE (found), arglist))
		    break;
		}
	      if (found)
		found = TREE_VALUE (found);
	    }

	  if (found)
	    {
	      if (can_free (&permanent_obstack, arglist))
		obstack_free (&permanent_obstack, arglist);
	      return found;
	    }
	}

3028 3029 3030 3031 3032
      if (TREE_CODE (arglist) == TREE_VEC)
	arglist_for_mangling = innermost_args (arglist, 0);
      else
	arglist_for_mangling = arglist;

3033
      /* FIXME avoid duplication.  */
mrs's avatar
mrs committed
3034
      mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
3035
						     parmlist,
3036
						     arglist_for_mangling,
jason's avatar
jason committed
3037
						     id_context);
mrs's avatar
mrs committed
3038
      id = get_identifier (mangled_name);
mrs's avatar
mrs committed
3039
      IDENTIFIER_TEMPLATE (id) = d1;
mrs's avatar
mrs committed
3040

mrs's avatar
mrs committed
3041
      maybe_push_to_top_level (uses_template_parms (arglist));
mrs's avatar
mrs committed
3042
      t = xref_tag_from_type (TREE_TYPE (template), id, 1);
3043

3044
      if (context != NULL_TREE)
3045 3046 3047 3048 3049 3050
	{
	  /* Set up the context for the type_decl correctly.  Note
	     that we must clear DECL_ASSEMBLER_NAME to fool
	     build_overload_name into creating a new name.  */
	  tree type_decl = TYPE_STUB_DECL (t);

3051 3052
	  TYPE_CONTEXT (t) = context;
	  DECL_CONTEXT (type_decl) = context;
3053 3054 3055 3056 3057
	  DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
	  DECL_ASSEMBLER_NAME (type_decl) = 
	    get_identifier (build_overload_name (t, 1, 1));
	}

mrs's avatar
mrs committed
3058
      pop_from_top_level ();
mrs's avatar
mrs committed
3059
    }
mrs's avatar
mrs committed
3060
  else
mrs's avatar
mrs committed
3061
    {
3062
      tree type_ctx = TYPE_CONTEXT (TREE_TYPE (template));
3063
      tree args = tsubst (CLASSTYPE_TI_ARGS (type_ctx), arglist, in_decl);
3064 3065
      tree ctx = lookup_template_class (type_ctx, args,
					in_decl, NULL_TREE);
mrs's avatar
mrs committed
3066 3067
      id = d1;
      arglist = CLASSTYPE_TI_ARGS (ctx);
mrs's avatar
mrs committed
3068

mrs's avatar
mrs committed
3069
      if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
mrs's avatar
mrs committed
3070
	{
mrs's avatar
mrs committed
3071 3072
	  int save_temp = processing_template_decl;
	  processing_template_decl = 0;
mrs's avatar
mrs committed
3073
	  t = xref_tag_from_type (TREE_TYPE (template), id, 0);
mrs's avatar
mrs committed
3074
	  processing_template_decl = save_temp;
mrs's avatar
mrs committed
3075 3076 3077
	}
      else
	{
mrs's avatar
mrs committed
3078 3079
	  t = lookup_nested_type_by_name (ctx, id);
	  my_friendly_assert (t != NULL_TREE, 42);
mrs's avatar
mrs committed
3080
	}
mrs's avatar
mrs committed
3081 3082
    }

mrs's avatar
mrs committed
3083
  /* Seems to be wanted.  */
mrs's avatar
mrs committed
3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
  CLASSTYPE_GOT_SEMICOLON (t) = 1;

  if (! CLASSTYPE_TEMPLATE_INFO (t))
    {
      arglist = copy_to_permanent (arglist);
      CLASSTYPE_TEMPLATE_INFO (t)
	= perm_tree_cons (template, arglist, NULL_TREE);
      DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
	(arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
      TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
	= uses_template_parms (arglist);

      SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);

mrs's avatar
mrs committed
3098
      /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up.  */
mrs's avatar
mrs committed
3099
      DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
mmitchel's avatar
 
mmitchel committed
3100
      if (! uses_template_parms (arglist))
mrs's avatar
mrs committed
3101 3102 3103 3104 3105 3106
	DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) 
	  = get_identifier (build_overload_name (t, 1, 1));

      if (flag_external_templates && ! uses_template_parms (arglist)
	  && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
	  && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
mrs's avatar
mrs committed
3107
	add_pending_template (t);
mrs's avatar
mrs committed
3108 3109
    }

mrs's avatar
mrs committed
3110
  return t;
mrs's avatar
mrs committed
3111 3112
}

mrs's avatar
mrs committed
3113
/* Should be defined in parse.h.  */
mrs's avatar
mrs committed
3114 3115
extern int yychar;

3116 3117 3118 3119 3120 3121 3122 3123
/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
   TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
   If FN returns non-zero, the iteration is terminated, and
   for_each_template_parm returns 1.  Otherwise, the iteration
   continues.  If FN never returns a non-zero value, the value
   returned by for_each_template_parm is 0.  If FN is NULL, it is
   considered to be the function which always returns 1.  */

mrs's avatar
mrs committed
3124
int
3125
for_each_template_parm (t, fn, data)
mrs's avatar
mrs committed
3126
     tree t;
3127 3128
     tree_fn_t fn;
     void* data;
mrs's avatar
mrs committed
3129 3130 3131 3132 3133 3134 3135 3136 3137 3138
{
  if (!t)
    return 0;
  switch (TREE_CODE (t))
    {
    case INDIRECT_REF:
    case COMPONENT_REF:
      /* We assume that the object must be instantiated in order to build
	 the COMPONENT_REF, so we test only whether the type of the
	 COMPONENT_REF uses template parms.  */
3139
      return for_each_template_parm (TREE_TYPE (t), fn, data);
mrs's avatar
mrs committed
3140 3141 3142 3143

    case IDENTIFIER_NODE:
      if (!IDENTIFIER_TEMPLATE (t))
	return 0;
mrs's avatar
mrs committed
3144
      my_friendly_abort (42);
mrs's avatar
mrs committed
3145 3146 3147 3148 3149 3150

      /* aggregates of tree nodes */
    case TREE_VEC:
      {
	int i = TREE_VEC_LENGTH (t);
	while (i--)
3151
	  if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
mrs's avatar
mrs committed
3152 3153 3154 3155
	    return 1;
	return 0;
      }
    case TREE_LIST:
3156 3157
      if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
	  || for_each_template_parm (TREE_VALUE (t), fn, data))
mrs's avatar
mrs committed
3158
	return 1;
3159
      return for_each_template_parm (TREE_CHAIN (t), fn, data);
mrs's avatar
mrs committed
3160

jason's avatar
jason committed
3161 3162 3163 3164 3165
    case OVERLOAD:
      if (for_each_template_parm (OVL_FUNCTION (t), fn, data))
	return 1;
      return for_each_template_parm (OVL_CHAIN (t), fn, data);

mrs's avatar
mrs committed
3166 3167 3168
      /* constructed type nodes */
    case POINTER_TYPE:
    case REFERENCE_TYPE:
3169
      return for_each_template_parm (TREE_TYPE (t), fn, data);
mrs's avatar
mrs committed
3170
    case RECORD_TYPE:
mrs's avatar
mrs committed
3171
      if (TYPE_PTRMEMFUNC_FLAG (t))
3172 3173
	return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
				       fn, data);
mrs's avatar
mrs committed
3174
    case UNION_TYPE:
mrs's avatar
mrs committed
3175
      if (! CLASSTYPE_TEMPLATE_INFO (t))
mrs's avatar
mrs committed
3176
	return 0;
3177 3178 3179
      return for_each_template_parm (TREE_VALUE
				     (CLASSTYPE_TEMPLATE_INFO (t)),
				     fn, data);
mrs's avatar
mrs committed
3180
    case FUNCTION_TYPE:
3181
      if (for_each_template_parm (TYPE_ARG_TYPES (t), fn, data))
mrs's avatar
mrs committed
3182
	return 1;
3183
      return for_each_template_parm (TREE_TYPE (t), fn, data);
mrs's avatar
mrs committed
3184
    case ARRAY_TYPE:
3185
      if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
mrs's avatar
mrs committed
3186
	return 1;
3187
      return for_each_template_parm (TREE_TYPE (t), fn, data);
mrs's avatar
mrs committed
3188
    case OFFSET_TYPE:
3189
      if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
mrs's avatar
mrs committed
3190
	return 1;
3191
      return for_each_template_parm (TREE_TYPE (t), fn, data);
mrs's avatar
mrs committed
3192
    case METHOD_TYPE:
3193
      if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
mrs's avatar
mrs committed
3194
	return 1;
3195
      if (for_each_template_parm (TYPE_ARG_TYPES (t), fn, data))
mrs's avatar
mrs committed
3196
	return 1;
3197
      return for_each_template_parm (TREE_TYPE (t), fn, data);
mrs's avatar
mrs committed
3198 3199 3200

      /* decl nodes */
    case TYPE_DECL:
3201
      return for_each_template_parm (TREE_TYPE (t), fn, data);
mrs's avatar
mrs committed
3202

3203 3204 3205 3206 3207 3208 3209 3210 3211
    case TEMPLATE_DECL:
      /* A template template parameter is encountered */
      if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
        /* We are parsing a template declaration */
        return 1;
      /* We are instantiating templates with template template
         parameter */
      return 0;
      
3212
    case CONST_DECL:
3213
      if (for_each_template_parm (DECL_INITIAL (t), fn, data))
3214 3215 3216
	return 1;
      goto check_type_and_context;

mrs's avatar
mrs committed
3217
    case FUNCTION_DECL:
mrs's avatar
mrs committed
3218 3219 3220
    case VAR_DECL:
      /* ??? What about FIELD_DECLs?  */
      if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
3221
	  && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
mrs's avatar
mrs committed
3222 3223 3224
	return 1;
      /* fall through */
    case PARM_DECL:
3225
    check_type_and_context:
3226
      if (for_each_template_parm (TREE_TYPE (t), fn, data))
mrs's avatar
mrs committed
3227
	return 1;
3228 3229
      if (DECL_CONTEXT (t) 
	  && for_each_template_parm (DECL_CONTEXT (t), fn, data))
mrs's avatar
mrs committed
3230 3231 3232 3233
	return 1;
      return 0;

    case CALL_EXPR:
3234
      return for_each_template_parm (TREE_TYPE (t), fn, data);
mrs's avatar
mrs committed
3235
    case ADDR_EXPR:
3236
      return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
mrs's avatar
mrs committed
3237 3238 3239

      /* template parm nodes */
    case TEMPLATE_TYPE_PARM:
3240
    case TEMPLATE_TEMPLATE_PARM:
jason's avatar
jason committed
3241
    case TEMPLATE_PARM_INDEX:
3242 3243 3244 3245
      if (fn)
	return (*fn)(t, data);
      else
	return 1;
mrs's avatar
mrs committed
3246 3247 3248

      /* simple type nodes */
    case INTEGER_TYPE:
3249
      if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
mrs's avatar
mrs committed
3250
	return 1;
3251
      return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
mrs's avatar
mrs committed
3252 3253

    case REAL_TYPE:
mrs's avatar
mrs committed
3254
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
3255
    case VOID_TYPE:
mrs's avatar
mrs committed
3256
    case BOOLEAN_TYPE:
jason's avatar
jason committed
3257
    case NAMESPACE_DECL:
mrs's avatar
mrs committed
3258 3259
      return 0;

3260 3261 3262 3263 3264
    case ENUMERAL_TYPE:
      {
	tree v;

	for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v))
3265
	  if (for_each_template_parm (TREE_VALUE (v), fn, data))
3266 3267 3268 3269
	    return 1;
      }
      return 0;

mrs's avatar
mrs committed
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281
      /* constants */
    case INTEGER_CST:
    case REAL_CST:
    case STRING_CST:
      return 0;

    case ERROR_MARK:
      /* Non-error_mark_node ERROR_MARKs are bad things.  */
      my_friendly_assert (t == error_mark_node, 274);
      /* NOTREACHED */
      return 0;

mrs's avatar
mrs committed
3282
    case LOOKUP_EXPR:
mrs's avatar
mrs committed
3283
    case TYPENAME_TYPE:
mrs's avatar
mrs committed
3284 3285
      return 1;

mrs's avatar
mrs committed
3286
    case SCOPE_REF:
3287
      return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
mrs's avatar
mrs committed
3288

mrs's avatar
mrs committed
3289 3290
    case CONSTRUCTOR:
      if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3291 3292 3293
	return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
				       (TREE_TYPE (t)), fn, data);
      return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
mrs's avatar
mrs committed
3294

brendan's avatar
brendan committed
3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
    case MODOP_EXPR:
    case CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case CONST_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case ARROW_EXPR:
    case DOTSTAR_EXPR:
    case TYPEID_EXPR:
      return 1;

3306 3307
    case SIZEOF_EXPR:
    case ALIGNOF_EXPR:
3308
      return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3309

mrs's avatar
mrs committed
3310 3311 3312 3313 3314
    default:
      switch (TREE_CODE_CLASS (TREE_CODE (t)))
	{
	case '1':
	case '2':
mrs's avatar
mrs committed
3315
	case 'e':
mrs's avatar
mrs committed
3316 3317 3318 3319
	case '<':
	  {
	    int i;
	    for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
3320
	      if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
mrs's avatar
mrs committed
3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334
		return 1;
	    return 0;
	  }
	default:
	  break;
	}
      sorry ("testing %s for template parms",
	     tree_code_name [(int) TREE_CODE (t)]);
      my_friendly_abort (82);
      /* NOTREACHED */
      return 0;
    }
}

3335 3336 3337 3338 3339 3340 3341
int
uses_template_parms (t)
     tree t;
{
  return for_each_template_parm (t, 0, 0);
}

3342 3343 3344
static struct tinst_level *current_tinst_level;
static struct tinst_level *free_tinst_level;
static int tinst_depth;
mrs's avatar
mrs committed
3345
extern int max_tinst_depth;
mrs's avatar
mrs committed
3346
#ifdef GATHER_STATISTICS
3347
int depth_reached;
mrs's avatar
mrs committed
3348
#endif
3349 3350
int tinst_level_tick;
int last_template_error_tick;
mrs's avatar
mrs committed
3351

3352
/* Print out all the template instantiations that we are currently
3353 3354
   working on.  If ERR, we are being called from cp_thing, so do
   the right thing for an error message.  */
3355

3356 3357 3358
static void
print_template_context (err)
     int err;
3359 3360 3361 3362 3363
{
  struct tinst_level *p = current_tinst_level;
  int line = lineno;
  char *file = input_filename;

3364 3365 3366 3367 3368
  if (err)
    {
      if (current_function_decl == p->decl)
	/* Avoid redundancy with the the "In function" line.  */;
      else if (current_function_decl == NULL_TREE)
jason's avatar
jason committed
3369 3370
	fprintf (stderr, "%s: In instantiation of `%s':\n",
		 file, decl_as_string (p->decl, 0));
3371 3372 3373 3374 3375
      else
	my_friendly_abort (980521);

      if (p)
	{
3376 3377
	  line = p->line;
	  file = p->file;
3378 3379 3380 3381 3382
	  p = p->next;
	}
    }

 next:
3383 3384
  for (; p; p = p->next)
    {
3385 3386 3387 3388
      fprintf (stderr, "%s:%d:   instantiated from `%s'\n", file, line,
	       decl_as_string (p->decl, 0));
      line = p->line;
      file = p->file;
3389
    }
3390
  fprintf (stderr, "%s:%d:   instantiated from here\n", file, line);
3391 3392
}

3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405
/* Called from cp_thing to print the template context for an error.  */

void
maybe_print_template_context ()
{
  if (last_template_error_tick == tinst_level_tick
      || current_tinst_level == 0)
    return;

  last_template_error_tick = tinst_level_tick;
  print_template_context (1);
}

mrs's avatar
mrs committed
3406
static int
mrs's avatar
mrs committed
3407 3408
push_tinst_level (d)
     tree d;
mrs's avatar
mrs committed
3409 3410 3411
{
  struct tinst_level *new;

mrs's avatar
mrs committed
3412 3413
  if (tinst_depth >= max_tinst_depth)
    {
3414 3415 3416 3417 3418 3419
      /* If the instantiation in question still has unbound template parms,
	 we don't really care if we can't instantiate it, so just return.
         This happens with base instantiation for implicit `typename'.  */
      if (uses_template_parms (d))
	return 0;

jason's avatar
jason committed
3420
      last_template_error_tick = tinst_level_tick;
mrs's avatar
mrs committed
3421 3422
      error ("template instantiation depth exceeds maximum of %d",
	     max_tinst_depth);
mrs's avatar
mrs committed
3423
      error (" (use -ftemplate-depth-NN to increase the maximum)");
mrs's avatar
mrs committed
3424 3425
      cp_error ("  instantiating `%D'", d);

3426
      print_template_context (0);
mrs's avatar
mrs committed
3427

mrs's avatar
mrs committed
3428 3429 3430
      return 0;
    }

mrs's avatar
mrs committed
3431 3432 3433 3434 3435 3436 3437 3438
  if (free_tinst_level)
    {
      new = free_tinst_level;
      free_tinst_level = new->next;
    }
  else
    new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));

mrs's avatar
mrs committed
3439 3440 3441
  new->decl = d;
  new->line = lineno;
  new->file = input_filename;
mrs's avatar
mrs committed
3442 3443
  new->next = current_tinst_level;
  current_tinst_level = new;
mrs's avatar
mrs committed
3444

mrs's avatar
mrs committed
3445
  ++tinst_depth;
mrs's avatar
mrs committed
3446 3447 3448 3449 3450
#ifdef GATHER_STATISTICS
  if (tinst_depth > depth_reached)
    depth_reached = tinst_depth;
#endif

3451
  ++tinst_level_tick;
mrs's avatar
mrs committed
3452
  return 1;
mrs's avatar
mrs committed
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462
}

void
pop_tinst_level ()
{
  struct tinst_level *old = current_tinst_level;

  current_tinst_level = old->next;
  old->next = free_tinst_level;
  free_tinst_level = old;
mrs's avatar
mrs committed
3463
  --tinst_depth;
3464
  ++tinst_level_tick;
mrs's avatar
mrs committed
3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
}

struct tinst_level *
tinst_for_decl ()
{
  struct tinst_level *p = current_tinst_level;

  if (p)
    for (; p->next ; p = p->next )
      ;
  return p;
}

jason's avatar
jason committed
3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488
/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL.  ARGS is the
   vector of template arguments, as for tsubst.

   Returns an appropriate tsbust'd friend declaration.  */

static tree
tsubst_friend_function (decl, args)
     tree decl;
     tree args;
{
  tree new_friend;
3489 3490 3491 3492 3493 3494
  int line = lineno;
  char *file = input_filename;

  lineno = DECL_SOURCE_LINE (decl);
  input_filename = DECL_SOURCE_FILE (decl);

jason's avatar
jason committed
3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513
  if (TREE_CODE (decl) == FUNCTION_DECL 
      && DECL_TEMPLATE_INSTANTIATION (decl)
      && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
    /* This was a friend declared with an explicit template
       argument list, e.g.:
       
       friend void f<>(T);
       
       to indicate that f was a template instantiation, not a new
       function declaration.  Now, we have to figure out what
       instantiation of what template.  */
    {
      tree template_id;
      tree new_args;
      tree tmpl;
      tree tinfo;

      template_id
	= lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
3514
						 args, NULL_TREE),
jason's avatar
jason committed
3515
				    tsubst (DECL_TI_ARGS (decl),
3516
					    args, NULL_TREE));
jason's avatar
jason committed
3517 3518 3519 3520 3521
      
      /* Temporarily remove the DECL_TEMPLATE_INFO so as not to
	 confuse tsubst.  */
      tinfo = DECL_TEMPLATE_INFO (decl);
      DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3522
      new_friend = tsubst (decl, args, NULL_TREE);
jason's avatar
jason committed
3523 3524 3525 3526 3527 3528
      DECL_TEMPLATE_INFO (decl) = tinfo;

      tmpl = determine_specialization (template_id,
				       new_friend,
				       &new_args,
				       0, 1);
3529 3530
      new_friend = instantiate_template (tmpl, new_args);
      goto done;
jason's avatar
jason committed
3531
    }
3532 3533
  else
    new_friend = tsubst (decl, args, NULL_TREE);
jason's avatar
jason committed
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548
	
  /* The new_friend will look like an instantiation, to the
     compiler, but is not an instantiation from the point of view of
     the language.  For example, we might have had:
     
     template <class T> struct S {
       template <class U> friend void f(T, U);
     };
     
     Then, in S<int>, template <class U> void f(int, U) is not an
     instantiation of anything.  */
  DECL_USE_TEMPLATE (new_friend) = 0;
  if (TREE_CODE (decl) == TEMPLATE_DECL)
    DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
  
3549
  if (DECL_NAMESPACE_SCOPE_P (new_friend))
jason's avatar
jason committed
3550 3551 3552 3553 3554 3555
    {
      if (TREE_CODE (new_friend) == TEMPLATE_DECL)
	/* This declaration is a `primary' template.  */
	TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (new_friend))
	    = new_friend;

jason's avatar
jason committed
3556
	new_friend = pushdecl_namespace_level (new_friend);
jason's avatar
jason committed
3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568
    }
  else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
    {
      /* Check to see that the declaration is really present, and,
	 possibly obtain an improved declaration.  */
      tree fn = check_classfn (DECL_CONTEXT (new_friend),
			       new_friend);
      
      if (fn)
	new_friend = fn;
    }

3569 3570 3571
 done:
  lineno = line;
  input_filename = file;
jason's avatar
jason committed
3572 3573 3574
  return new_friend;
}

3575 3576
/* FRIEND_TMPL is a friend TEMPLATE_DECL.  ARGS is the vector of
   template arguments, as for tsubst.
3577 3578 3579 3580

   Returns an appropriate tsbust'd friend type.  */

static tree
3581 3582
tsubst_friend_class (friend_tmpl, args)
     tree friend_tmpl;
3583 3584
     tree args;
{
3585
  tree friend_type;
3586
  tree tmpl = lookup_name (DECL_NAME (friend_tmpl), 1); 
3587 3588 3589 3590 3591 3592 3593

  tmpl = maybe_get_template_decl_from_type_decl (tmpl);

  if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl))
    {
      /* The friend template has already been declared.  Just
	 check to see that the declarations match.  */
3594 3595
      redeclare_class_template (TREE_TYPE (tmpl),
				DECL_TEMPLATE_PARMS (friend_tmpl));
3596 3597 3598 3599 3600 3601 3602
      friend_type = TREE_TYPE (tmpl);
    }
  else
    {
      /* The friend template has not already been declared.  In this
	 case, the instantiation of the template class will cause the
	 injection of this template into the global scope.  */
3603
      tmpl = tsubst (friend_tmpl, args, NULL_TREE);
3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618

      /* The new TMPL is not an instantiation of anything, so we
 	 forget its origins.  We don't reset CLASSTYPE_TI_TEMPLATE for
	 the new type because that is supposed to be the corresponding
	 template decl, i.e., TMPL.  */
      DECL_USE_TEMPLATE (tmpl) = 0;
      DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
      CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;

      /* Inject this template into the global scope.  */
      friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
    }

  return friend_type;
}
jason's avatar
jason committed
3619

mrs's avatar
mrs committed
3620
tree
mrs's avatar
mrs committed
3621 3622
instantiate_class_template (type)
     tree type;
mrs's avatar
mrs committed
3623
{
mrs's avatar
mrs committed
3624
  tree template, template_info, args, pattern, t, *field_chain;
3625
  tree typedecl, outer_args;
mrs's avatar
mrs committed
3626

mrs's avatar
mrs committed
3627
  if (type == error_mark_node)
mrs's avatar
mrs committed
3628 3629
    return error_mark_node;

mrs's avatar
mrs committed
3630 3631 3632 3633 3634 3635 3636 3637
  template_info = CLASSTYPE_TEMPLATE_INFO (type);

  if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
    return type;

  template = TI_TEMPLATE (template_info);
  my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
  args = TI_ARGS (template_info);
mrs's avatar
mrs committed
3638

3639 3640
  if (DECL_TEMPLATE_INFO (template))
    {
3641
      outer_args = DECL_TI_ARGS (template);
3642 3643 3644
      while (DECL_TEMPLATE_INFO (template))
	template = DECL_TI_TEMPLATE (template);
    }
3645 3646
  else
    outer_args = NULL_TREE;
3647

mrs's avatar
mrs committed
3648
  t = most_specialized_class
3649
    (DECL_TEMPLATE_SPECIALIZATIONS (template), args, outer_args);
mrs's avatar
mrs committed
3650 3651 3652 3653 3654 3655 3656

  if (t == error_mark_node)
    {
      char *str = "candidates are:";
      cp_error ("ambiguous class template instantiation for `%#T'", type);
      for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
	{
3657 3658
	  if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
				  args, outer_args))
mrs's avatar
mrs committed
3659 3660 3661 3662 3663 3664
	    {
	      cp_error_at ("%s %+#T", str, TREE_TYPE (t));
	      str = "               ";
	    }
	}
      TYPE_BEING_DEFINED (type) = 1;
mrs's avatar
mrs committed
3665
      return error_mark_node;
mrs's avatar
mrs committed
3666 3667 3668 3669 3670
    }
  else if (t)
    pattern = TREE_TYPE (t);
  else
    pattern = TREE_TYPE (template);
mrs's avatar
mrs committed
3671 3672

  if (TYPE_SIZE (pattern) == NULL_TREE)
mrs's avatar
mrs committed
3673
    return type;
mrs's avatar
mrs committed
3674

mrs's avatar
mrs committed
3675
  if (t)
3676 3677 3678
    args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
			       args, outer_args);

3679 3680 3681 3682 3683 3684 3685
  if (pedantic && uses_template_parms (args))
    /* If there are still template parameters amongst the args, then
       we can't instantiate the type; there's no telling whether or not one
       of the template parameters might eventually be instantiated to some
       value that results in a specialization being used.  */
    return type;

3686 3687 3688 3689 3690
  /* We must copy the arguments to the permanent obstack since
     during the tsubst'ing below they may wind up in the
     DECL_TI_ARGS of some instantiated member template.  */
  args = copy_to_permanent (args);

mrs's avatar
mrs committed
3691 3692 3693 3694
  TYPE_BEING_DEFINED (type) = 1;

  if (! push_tinst_level (type))
    return type;
mrs's avatar
mrs committed
3695

mrs's avatar
mrs committed
3696 3697 3698
  maybe_push_to_top_level (uses_template_parms (type));
  pushclass (type, 0);

3699 3700 3701
  if (outer_args)
    args = add_to_template_args (outer_args, args);

mrs's avatar
mrs committed
3702 3703 3704 3705 3706 3707 3708
  if (flag_external_templates)
    {
      if (flag_alt_external_templates)
	{
	  CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
	  SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
	  CLASSTYPE_VTABLE_NEEDS_WRITING (type)
3709 3710
	    = (! CLASSTYPE_INTERFACE_ONLY (type)
	       && CLASSTYPE_INTERFACE_KNOWN (type));
mrs's avatar
mrs committed
3711 3712 3713 3714 3715 3716 3717
	}
      else
	{
	  CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
	  SET_CLASSTYPE_INTERFACE_UNKNOWN_X
	    (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
	  CLASSTYPE_VTABLE_NEEDS_WRITING (type)
3718 3719
	    = (! CLASSTYPE_INTERFACE_ONLY (type)
	       && CLASSTYPE_INTERFACE_KNOWN (type));
mrs's avatar
mrs committed
3720 3721 3722
	}
    }
  else
mrs's avatar
mrs committed
3723
    {
mrs's avatar
mrs committed
3724 3725
      SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
      CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
mrs's avatar
mrs committed
3726 3727
    }

mrs's avatar
mrs committed
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752
  TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
  TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
  TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
  TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
  TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
  TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
  TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
  TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
  TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
  TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
  TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
  TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
  TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
  TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
  TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
  TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
  TYPE_USES_COMPLEX_INHERITANCE (type)
    = TYPE_USES_COMPLEX_INHERITANCE (pattern);
  TYPE_USES_MULTIPLE_INHERITANCE (type)
    = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
  TYPE_USES_VIRTUAL_BASECLASSES (type)
    = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
  TYPE_PACKED (type) = TYPE_PACKED (pattern);
  TYPE_ALIGN (type) = TYPE_ALIGN (pattern);

3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767
  CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);

  /* If this is a partial instantiation, don't tsubst anything.  We will
     only use this type for implicit typename, so the actual contents don't
     matter.  All that matters is whether a particular name is a type.  */
  if (uses_template_parms (type))
    {
      TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
      TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
      TYPE_METHODS (type) = TYPE_METHODS (pattern);
      CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
      TYPE_SIZE (type) = integer_zero_node;
      goto end;
    }

mrs's avatar
mrs committed
3768 3769 3770 3771 3772 3773 3774 3775 3776
  {
    tree binfo = TYPE_BINFO (type);
    tree pbases = TYPE_BINFO_BASETYPES (pattern);

    if (pbases)
      {
	tree bases;
	int i;
	int len = TREE_VEC_LENGTH (pbases);
mrs's avatar
mrs committed
3777
	bases = make_tree_vec (len);
mrs's avatar
mrs committed
3778 3779 3780 3781 3782
	for (i = 0; i < len; ++i)
	  {
	    tree elt;

	    TREE_VEC_ELT (bases, i) = elt
3783
	      = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE);
mrs's avatar
mrs committed
3784 3785
	    BINFO_INHERITANCE_CHAIN (elt) = binfo;

mrs's avatar
mrs committed
3786 3787 3788 3789
	    if (! IS_AGGR_TYPE (TREE_TYPE (elt)))
	      cp_error
		("base type `%T' of `%T' fails to be a struct or class type",
		 TREE_TYPE (elt), type);
3790
	    else if (TYPE_SIZE (complete_type (TREE_TYPE (elt))) == NULL_TREE)
mrs's avatar
mrs committed
3791 3792 3793
	      cp_error ("base class `%T' of `%T' has incomplete type",
			TREE_TYPE (elt), type);
	  }
mrs's avatar
mrs committed
3794 3795 3796
	/* Don't initialize this until the vector is filled out, or
	   lookups will crash.  */
	BINFO_BASETYPES (binfo) = bases;
mrs's avatar
mrs committed
3797 3798 3799 3800 3801 3802
      }
  }

  field_chain = &TYPE_FIELDS (type);

  for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
mrs's avatar
mrs committed
3803
    {
mrs's avatar
mrs committed
3804 3805
      tree tag = TREE_VALUE (t);

mrs's avatar
mrs committed
3806
      /* These will add themselves to CLASSTYPE_TAGS for the new type.  */
mrs's avatar
mrs committed
3807
      if (TREE_CODE (tag) == ENUMERAL_TYPE)
mrs's avatar
mrs committed
3808
	{
manfred's avatar
d  
manfred committed
3809
	  (void) tsubst_enum (tag, args, field_chain);
mrs's avatar
mrs committed
3810
	  while (*field_chain)
mrs's avatar
mrs committed
3811 3812 3813 3814
	    {
	      DECL_FIELD_CONTEXT (*field_chain) = type;
	      field_chain = &TREE_CHAIN (*field_chain);
	    }
mrs's avatar
mrs committed
3815
	}
mrs's avatar
mrs committed
3816
      else
3817
	tsubst (tag, args, NULL_TREE);
mrs's avatar
mrs committed
3818 3819
    }

mrs's avatar
mrs committed
3820 3821 3822 3823
  /* Don't replace enum constants here.  */
  for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
    if (TREE_CODE (t) != CONST_DECL)
      {
3824
	tree r = tsubst (t, args, NULL_TREE);
mrs's avatar
mrs committed
3825 3826
	if (TREE_CODE (r) == VAR_DECL)
	  {
3827 3828
	    pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
	    /* Perhaps we should do more of grokfield here.  */
mrs's avatar
mrs committed
3829 3830 3831 3832 3833 3834 3835 3836 3837
	    start_decl_1 (r);
	    DECL_IN_AGGR_P (r) = 1;
	    DECL_EXTERNAL (r) = 1;
	    cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
	  }

	*field_chain = r;
	field_chain = &TREE_CHAIN (r);
      }
mrs's avatar
mrs committed
3838

mrs's avatar
mrs committed
3839
  TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
mrs's avatar
mrs committed
3840 3841 3842 3843 3844 3845 3846
  for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
    {
      if (DECL_CONSTRUCTOR_P (t))
	grok_ctor_properties (type, t);
      else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
	grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
    }
mrs's avatar
mrs committed
3847

3848 3849 3850 3851 3852
  /* Construct the DECL_FRIENDLIST for the new class type.  */
  typedecl = TYPE_MAIN_DECL (type);
  for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
       t != NULL_TREE;
       t = TREE_CHAIN (t))
jason's avatar
jason committed
3853
    {
3854
      tree friends;
jason's avatar
jason committed
3855

3856 3857 3858
      DECL_FRIENDLIST (typedecl)
	= tree_cons (TREE_PURPOSE (t), NULL_TREE, 
		     DECL_FRIENDLIST (typedecl));
jason's avatar
jason committed
3859

3860 3861 3862 3863 3864
      for (friends = TREE_VALUE (t);
	   friends != NULL_TREE;
	   friends = TREE_CHAIN (friends))
	{
	  if (TREE_PURPOSE (friends) == error_mark_node)
jason's avatar
jason committed
3865
	    {
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877
	      TREE_VALUE (DECL_FRIENDLIST (typedecl))
		= tree_cons (error_mark_node, 
			     tsubst_friend_function (TREE_VALUE (friends),
						     args),
			     TREE_VALUE (DECL_FRIENDLIST (typedecl)));
	    }
	  else
	    {
	      TREE_VALUE (DECL_FRIENDLIST (typedecl))
		= tree_cons (tsubst (TREE_PURPOSE (friends), args, NULL_TREE),
			     NULL_TREE,
			     TREE_VALUE (DECL_FRIENDLIST (typedecl)));
jason's avatar
jason committed
3878 3879 3880

	    }
	}
3881
    }
mrs's avatar
mrs committed
3882

3883 3884 3885 3886 3887
  for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
       t != NULL_TREE;
       t = TREE_CHAIN (t))
    {
      tree friend_type = TREE_VALUE (t);
3888
      tree new_friend_type;
3889

3890
      if (TREE_CODE (friend_type) != TEMPLATE_DECL)
3891 3892
	/* The call to xref_tag_from_type does injection for friend
	   classes.  */
3893
	new_friend_type = 
3894 3895 3896
	  xref_tag_from_type (tsubst (friend_type, args, NULL_TREE),
			      NULL_TREE, 1);
      else
3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907
	new_friend_type = tsubst_friend_class (friend_type, args);

      if (TREE_CODE (friend_type) == TEMPLATE_DECL)
	/* Trick make_friend_class into realizing that the friend
	   we're adding is a template, not an ordinary class.  It's
	   important that we use make_friend_class since it will
	   perform some error-checking and output cross-reference
	   information.  */
	++processing_template_decl;

      make_friend_class (type, new_friend_type);
mrs's avatar
mrs committed
3908

3909 3910
      if (TREE_CODE (friend_type) == TEMPLATE_DECL)
	--processing_template_decl;
3911
    }
mrs's avatar
mrs committed
3912

3913 3914 3915 3916
  /* This does injection for friend functions. */
  if (!processing_template_decl)
    {
      t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE);
mrs's avatar
mrs committed
3917

3918 3919 3920
      for (; t; t = TREE_CHAIN (t))
	{
	  tree d = TREE_VALUE (t);
mrs's avatar
mrs committed
3921

3922 3923 3924 3925 3926 3927
	  if (TREE_CODE (d) == TYPE_DECL)
	    /* Already injected.  */;
	  else
	    pushdecl (d);
	}
    } 
mrs's avatar
mrs committed
3928

3929 3930 3931 3932 3933 3934
  for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
    if (TREE_CODE (t) == FIELD_DECL)
      {
	TREE_TYPE (t) = complete_type (TREE_TYPE (t));
	require_complete_type (t);
      }
mrs's avatar
mrs committed
3935

3936 3937
  type = finish_struct_1 (type, 0);
  CLASSTYPE_GOT_SEMICOLON (type) = 1;
mrs's avatar
mrs committed
3938

3939 3940 3941
  repo_template_used (type);
  if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
    finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
mrs's avatar
mrs committed
3942

3943
 end:
mrs's avatar
mrs committed
3944 3945 3946 3947 3948 3949 3950
  TYPE_BEING_DEFINED (type) = 0;
  popclass (0);

  pop_from_top_level ();
  pop_tinst_level ();

  return type;
mrs's avatar
mrs committed
3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968
}

static int
list_eq (t1, t2)
     tree t1, t2;
{
  if (t1 == NULL_TREE)
    return t2 == NULL_TREE;
  if (t2 == NULL_TREE)
    return 0;
  /* Don't care if one declares its arg const and the other doesn't -- the
     main variant of the arg type is all that matters.  */
  if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
      != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
    return 0;
  return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
}

mrs's avatar
mrs committed
3969
tree 
mrs's avatar
mrs committed
3970 3971 3972 3973 3974
lookup_nested_type_by_name (ctype, name)
        tree ctype, name;
{
  tree t;

mrs's avatar
mrs committed
3975 3976
  complete_type (ctype);

mrs's avatar
mrs committed
3977 3978
  for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
    {
3979 3980 3981
      if (name == TREE_PURPOSE (t)
	  /* this catches typedef enum { foo } bar; */
	  || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
mrs's avatar
mrs committed
3982 3983
	return TREE_VALUE (t);
    }
mrs's avatar
mrs committed
3984 3985 3986
  return NULL_TREE;
}

3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012
/* If arg is a non-type template parameter that does not depend on template
   arguments, fold it like we weren't in the body of a template.  */

static tree
maybe_fold_nontype_arg (arg)
     tree arg;
{
  if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
      && !uses_template_parms (arg))
    {
      /* Sometimes, one of the args was an expression involving a
	 template constant parameter, like N - 1.  Now that we've
	 tsubst'd, we might have something like 2 - 1.  This will
	 confuse lookup_template_class, so we do constant folding
	 here.  We have to unset processing_template_decl, to
	 fool build_expr_from_tree() into building an actual
	 tree.  */

      int saved_processing_template_decl = processing_template_decl; 
      processing_template_decl = 0;
      arg = fold (build_expr_from_tree (arg));
      processing_template_decl = saved_processing_template_decl; 
    }
  return arg;
}

4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024
/* Return the TREE_VEC with the arguments for the innermost template header,
   where ARGS is either that or the VEC of VECs for all the arguments.

   If is_spec, then we are dealing with a specialization of a member
   template, and want the second-innermost args, the innermost ones that
   are instantiated.  */

tree
innermost_args (args, is_spec)
     tree args;
     int is_spec;
{
4025
  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4026 4027 4028
    return TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1 - is_spec);
  return args;
}
jason's avatar
jason committed
4029

4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064
/* Substitute ARGS into the vector of template arguments T.  */

tree
tsubst_template_arg_vector (t, args)
     tree t;
     tree args;
{
  int len = TREE_VEC_LENGTH (t), need_new = 0, i;
  tree *elts = (tree *) alloca (len * sizeof (tree));
  
  bzero ((char *) elts, len * sizeof (tree));
  
  for (i = 0; i < len; i++)
    {
      if (TREE_VEC_ELT (t, i) != NULL_TREE
	  && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
	elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), args);
      else
	elts[i] = maybe_fold_nontype_arg
	  (tsubst_expr (TREE_VEC_ELT (t, i), args, NULL_TREE));
      
      if (elts[i] != TREE_VEC_ELT (t, i))
	need_new = 1;
    }
  
  if (!need_new)
    return t;
  
  t = make_tree_vec (len);
  for (i = 0; i < len; i++)
    TREE_VEC_ELT (t, i) = elts[i];
  
  return t;
}

4065
/* Take the tree structure T and replace template parameters used therein
4066 4067
   with the argument vector ARGS.  IN_DECL is an associated decl for
   diagnostics.
4068 4069 4070 4071

   tsubst is used for dealing with types, decls and the like; for
   expressions, use tsubst_expr or tsubst_copy.  */

4072
tree
4073
tsubst (t, args, in_decl)
4074
     tree t, args;
mrs's avatar
mrs committed
4075 4076 4077 4078
     tree in_decl;
{
  tree type;

mrs's avatar
mrs committed
4079 4080 4081
  if (t == NULL_TREE || t == error_mark_node
      || t == integer_type_node
      || t == void_type_node
jason's avatar
jason committed
4082 4083
      || t == char_type_node
      || TREE_CODE (t) == NAMESPACE_DECL)
mrs's avatar
mrs committed
4084 4085
    return t;

jason's avatar
jason committed
4086 4087 4088 4089
  if (TREE_CODE (t) == IDENTIFIER_NODE)
    type = IDENTIFIER_TYPE_VALUE (t);
  else
    type = TREE_TYPE (t);
mrs's avatar
mrs committed
4090 4091
  if (type == unknown_type_node)
    my_friendly_abort (42);
4092

mrs's avatar
mrs committed
4093
  if (type && TREE_CODE (t) != FUNCTION_DECL
jason's avatar
jason committed
4094
      && TREE_CODE (t) != TYPENAME_TYPE
4095 4096
      && TREE_CODE (t) != TEMPLATE_DECL
      && TREE_CODE (t) != IDENTIFIER_NODE)
4097
    type = tsubst (type, args, in_decl);
mrs's avatar
mrs committed
4098

mrs's avatar
mrs committed
4099 4100 4101 4102
  switch (TREE_CODE (t))
    {
    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_P (t))
mrs's avatar
mrs committed
4103 4104
	{
	  tree r = build_ptrmemfunc_type
4105
	    (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl));
mrs's avatar
mrs committed
4106 4107 4108 4109
	  return cp_build_type_variant (r, TYPE_READONLY (t),
					TYPE_VOLATILE (t));
	}

mrs's avatar
mrs committed
4110
      /* else fall through */
mrs's avatar
mrs committed
4111 4112 4113
    case UNION_TYPE:
      if (uses_template_parms (t))
	{
4114
	  tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl);
4115 4116 4117
	  tree context;
	  tree r;

4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139
	  if (TYPE_CONTEXT (t) != NULL_TREE)
	    {
	      context = tsubst (TYPE_CONTEXT (t), args, in_decl);
	  
	      if (TREE_CODE (context) != FUNCTION_DECL
		  && TREE_CODE (context) != NAMESPACE_DECL)
		{
		  /* For a member class template, we need all the
		     template arguments.  */
		  if (CLASSTYPE_IS_TEMPLATE (TYPE_CONTEXT (t)))
		    argvec = 
		      add_to_template_args (CLASSTYPE_TI_ARGS (context),
					    argvec);

		  if (CLASSTYPE_TEMPLATE_INFO (context))
		    argvec = 
		      complete_template_args (CLASSTYPE_TI_TEMPLATE (context),
					      argvec, 0);
		}
	    }
	  else
	    context = NULL_TREE;
4140 4141 4142

	  r = lookup_template_class (t, argvec, in_decl, context);

mrs's avatar
mrs committed
4143
	  return cp_build_type_variant (r, TYPE_READONLY (t),
4144
					TYPE_VOLATILE (t));
mrs's avatar
mrs committed
4145
	}
mrs's avatar
mrs committed
4146

mrs's avatar
mrs committed
4147
      /* else fall through */
mrs's avatar
mrs committed
4148 4149 4150 4151 4152
    case ERROR_MARK:
    case IDENTIFIER_NODE:
    case OP_IDENTIFIER:
    case VOID_TYPE:
    case REAL_TYPE:
mrs's avatar
mrs committed
4153
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
4154
    case BOOLEAN_TYPE:
mrs's avatar
mrs committed
4155 4156 4157
    case INTEGER_CST:
    case REAL_CST:
    case STRING_CST:
jason's avatar
jason committed
4158
    case NAMESPACE_DECL:
mrs's avatar
mrs committed
4159 4160
      return t;

mrs's avatar
mrs committed
4161 4162
    case ENUMERAL_TYPE:
      {
4163
	tree ctx = tsubst (TYPE_CONTEXT (t), args, in_decl);
jason's avatar
jason committed
4164
	if (ctx == NULL_TREE || TREE_CODE (ctx) == NAMESPACE_DECL)
mrs's avatar
mrs committed
4165
	  return t;
mrs's avatar
mrs committed
4166 4167
	else if (ctx == current_function_decl)
	  return lookup_name (TYPE_IDENTIFIER (t), 1);
mrs's avatar
mrs committed
4168 4169 4170 4171
	else
	  return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
      }

mrs's avatar
mrs committed
4172 4173 4174 4175 4176 4177 4178
    case INTEGER_TYPE:
      if (t == integer_type_node)
	return t;

      if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
	  && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
	return t;
mrs's avatar
mrs committed
4179 4180

      {
mrs's avatar
mrs committed
4181
	tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
4182
	max = tsubst_expr (max, args, in_decl);
mrs's avatar
mrs committed
4183
	if (processing_template_decl)
mrs's avatar
mrs committed
4184 4185 4186
	  {
	    tree itype = make_node (INTEGER_TYPE);
	    TYPE_MIN_VALUE (itype) = size_zero_node;
mrs's avatar
mrs committed
4187 4188
	    TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
						integer_one_node);
mrs's avatar
mrs committed
4189 4190
	    return itype;
	  }
mrs's avatar
mrs committed
4191 4192

	max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
mrs's avatar
mrs committed
4193 4194
	return build_index_2_type (size_zero_node, max);
      }
mrs's avatar
mrs committed
4195 4196

    case TEMPLATE_TYPE_PARM:
4197
    case TEMPLATE_TEMPLATE_PARM:
jason's avatar
jason committed
4198
    case TEMPLATE_PARM_INDEX:
mrs's avatar
mrs committed
4199
      {
4200 4201
	int idx;
	int level;
4202
	int levels;
jason's avatar
jason committed
4203
	tree r = NULL_TREE;
4204

4205 4206
	if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
	    || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4207 4208 4209 4210 4211 4212
	  {
	    idx = TEMPLATE_TYPE_IDX (t);
	    level = TEMPLATE_TYPE_LEVEL (t);
	  }
	else
	  {
jason's avatar
jason committed
4213 4214
	    idx = TEMPLATE_PARM_IDX (t);
	    level = TEMPLATE_PARM_LEVEL (t);
4215 4216
	  }

jason's avatar
jason committed
4217
	if (TREE_VEC_LENGTH (args) > 0)
4218 4219 4220
	  {
	    tree arg = NULL_TREE;

4221
	    if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4222
	      {
4223 4224
		levels = TREE_VEC_LENGTH (args);
		if (level <= levels)
4225 4226 4227
		  arg = TREE_VEC_ELT
		    (TREE_VEC_ELT (args, level - 1), idx);
	      }
4228 4229 4230 4231 4232 4233
	    else
	      {
		levels = 1;
		if (level == 1)
		  arg = TREE_VEC_ELT (args, idx);
	      }
4234 4235 4236 4237 4238 4239 4240

	    if (arg != NULL_TREE)
	      {
		if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
		  return cp_build_type_variant
		    (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
		     TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
4241 4242 4243 4244 4245 4246
		else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
		  {
		    if (CLASSTYPE_TEMPLATE_INFO (t))
		      {
			/* We are processing a type constructed from
			   a template template parameter */
jason's avatar
tweak  
jason committed
4247
			tree argvec = tsubst (CLASSTYPE_TI_ARGS (t),
4248
					      args, in_decl);
4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259
			tree r;

			/* We can get a TEMPLATE_TEMPLATE_PARM here when 
			   we are resolving nested-types in the signature of 
			   a member function templates.
			   Otherwise ARG is a TEMPLATE_DECL and is the real 
			   template to be instantiated.  */
			if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
			  arg = TYPE_NAME (arg);

			r = lookup_template_class (DECL_NAME (arg), 
4260 4261
						   argvec, in_decl, 
						   DECL_CONTEXT (arg));
4262 4263 4264 4265 4266 4267 4268
			return cp_build_type_variant (r, TYPE_READONLY (t),
						      TYPE_VOLATILE (t));
		      }
		    else
		      /* We are processing a template argument list.  */ 
		      return arg;
		  }
4269 4270 4271 4272 4273
		else
		  return arg;
	      }
	  }

4274 4275 4276 4277 4278 4279
	if (level == 1)
	  /* This can happen during the attempted tsubst'ing in
	     unify.  This means that we don't yet have any information
	     about the template parameter in question.  */
	  return t;

4280
	/* If we get here, we must have been looking at a parm for a
4281 4282
	   more deeply nested template.  Make a new version of this
	   template parameter, but with a lower level.  */
jason's avatar
jason committed
4283 4284 4285 4286
	switch (TREE_CODE (t))
	  {
	  case TEMPLATE_TYPE_PARM:
	  case TEMPLATE_TEMPLATE_PARM:
4287
	    r = copy_node (t);
jason's avatar
jason committed
4288 4289
	    TEMPLATE_TYPE_PARM_INDEX (r)
	      = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
4290
					    r, levels);
jason's avatar
jason committed
4291 4292 4293
	    TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
	    TYPE_MAIN_VARIANT (r) = r;
	    TYPE_POINTER_TO (r) = NULL_TREE;
4294
	    TYPE_REFERENCE_TO (r) = NULL_TREE;
4295 4296 4297 4298 4299 4300 4301 4302

	    if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
		&& CLASSTYPE_TEMPLATE_INFO (t))
	      {
		tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl);
		CLASSTYPE_TEMPLATE_INFO (r)
		  = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
	      }
jason's avatar
jason committed
4303 4304 4305
	    break;

	  case TEMPLATE_PARM_INDEX:
4306
	    r = reduce_template_parm_level (t, type, levels);
jason's avatar
jason committed
4307 4308 4309 4310 4311 4312 4313
	    break;
	   
	  default:
	    my_friendly_abort (0);
	  }

	return r;
mrs's avatar
mrs committed
4314
      }
mrs's avatar
mrs committed
4315

4316 4317 4318 4319 4320 4321 4322
    case TEMPLATE_DECL:
      {
	/* We can get here when processing a member template function
	   of a template class.  */
	tree tmpl;
	tree decl = DECL_TEMPLATE_RESULT (t);
	tree parms;
jason's avatar
jason committed
4323
	tree* new_parms;
4324
	tree spec;
4325
	int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
4326

4327 4328 4329 4330 4331 4332 4333
	if (!is_template_template_parm)
	  {
	    /* We might already have an instance of this template. */
	    spec = retrieve_specialization (t, args);
	    if (spec != NULL_TREE)
	      return spec;
	  }
4334 4335 4336 4337 4338 4339 4340 4341 4342 4343

	/* Make a new template decl.  It will be similar to the
	   original, but will record the current template arguments. 
	   We also create a new function declaration, which is just
	   like the old one, but points to this new template, rather
	   than the old one.  */
	tmpl = copy_node (t);
	copy_lang_decl (tmpl);
	my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0);
	TREE_CHAIN (tmpl) = NULL_TREE;
4344 4345 4346 4347 4348 4349 4350 4351 4352

	if (is_template_template_parm)
	  {
	    tree new_decl = tsubst (decl, args, in_decl);
	    DECL_RESULT (tmpl) = new_decl;
	    TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
	    return tmpl;
	  }

jason's avatar
jason committed
4353
	DECL_CONTEXT (tmpl) = tsubst (DECL_CONTEXT (t),
4354
				      args, in_decl);
jason's avatar
jason committed
4355
	DECL_CLASS_CONTEXT (tmpl) = tsubst (DECL_CLASS_CONTEXT (t),
4356
					    args, in_decl);
4357
	DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373

	if (TREE_CODE (decl) == TYPE_DECL)
	  {
	    tree new_type = tsubst (TREE_TYPE (t), args, in_decl);
	    TREE_TYPE (tmpl) = new_type;
	    CLASSTYPE_TI_TEMPLATE (new_type) = tmpl;
	    DECL_RESULT (tmpl) = TYPE_MAIN_DECL (new_type);
	  }
	else
	  {
	    tree new_decl = tsubst (decl, args, in_decl);
	    DECL_RESULT (tmpl) = new_decl;
	    DECL_TI_TEMPLATE (new_decl) = tmpl;
	    TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
	  }

4374
	DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE;
jason's avatar
jason committed
4375
	SET_DECL_IMPLICIT_INSTANTIATION (tmpl);
4376 4377 4378 4379

	/* The template parameters for this new template are all the
	   template parameters for the old template, except the
	   outermost level of parameters. */
jason's avatar
jason committed
4380 4381
	for (new_parms = &DECL_TEMPLATE_PARMS (tmpl),
	       parms = DECL_TEMPLATE_PARMS (t);
4382
	     TREE_CHAIN (parms) != NULL_TREE;
jason's avatar
jason committed
4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397
	     new_parms = &(TREE_CHAIN (*new_parms)),
	       parms = TREE_CHAIN (parms))
	  {
	    tree new_vec = 
	      make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
	    int i;

	    for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
	      {
		tree default_value =
		  TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
		tree parm_decl = 
		  TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
		  
		TREE_VEC_ELT (new_vec, i)
4398 4399
		  = build_tree_list (tsubst (default_value, args, in_decl),
				     tsubst (parm_decl, args, in_decl));
jason's avatar
jason committed
4400 4401 4402 4403 4404 4405 4406 4407 4408 4409
		  
	      }

	    *new_parms = 
	      tree_cons (build_int_2 (0, 
				      TREE_INT_CST_HIGH 
				      (TREE_PURPOSE (parms)) - 1),
			 new_vec,
			 NULL_TREE);
	  }
4410

4411
	if (PRIMARY_TEMPLATE_P (t))
4412
	  DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4413

4414
	/* We don't partially instantiate partial specializations.  */
4415 4416 4417
	if (TREE_CODE (decl) == TYPE_DECL)
	  return tmpl;

4418 4419 4420 4421 4422
	/* What should we do with the specializations of this member
	   template?  Are they specializations of this new template,
	   or instantiations of the templates they previously were?
	   this new template?  And where should their
	   DECL_TI_TEMPLATES point?  */ 
4423
	DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
	for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
	     spec != NULL_TREE;
	     spec = TREE_CHAIN (spec))
	  {
	    /* It helps to consider example here.  Consider:

	       template <class T>
	       struct S {
	         template <class U>
		 void f(U u);

		 template <>
		 void f(T* t) {}
	       };
	       
	       Now, for example, we are instantiating S<int>::f(U u).  
	       We want to make a template:

	       template <class U>
	       void S<int>::f(U);

	       It will have a specialization, for the case U = int*, of
	       the form:

	       template <>
	       void S<int>::f<int*>(int*);

	       This specialization will be an instantiation of
	       the specialization given in the declaration of S, with
	       argument list int*.  */

	    tree fn = TREE_VALUE (spec);
	    tree spec_args;
	    tree new_fn;

	    if (!DECL_TEMPLATE_SPECIALIZATION (fn))
	      /* Instantiations are on the same list, but they're of
		 no concern to us.  */
	      continue;

4464
	    spec_args = tsubst (DECL_TI_ARGS (fn), args,
4465
				in_decl); 
4466
	    new_fn = tsubst (DECL_RESULT (fn), args,
4467 4468 4469 4470 4471 4472 4473 4474 4475
			     in_decl); 
	    DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = 
	      perm_tree_cons (spec_args, new_fn, 
			      DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
	  }

	/* Record this partial instantiation.  */
	register_specialization (tmpl, t, args);

4476 4477
	return tmpl;
      }
mrs's avatar
mrs committed
4478 4479 4480

    case FUNCTION_DECL:
      {
mrs's avatar
mrs committed
4481
	tree r = NULL_TREE;
4482
	tree ctx;
4483
	tree argvec;
jason's avatar
jason committed
4484
	tree tmpl = NULL_TREE;
mrs's avatar
mrs committed
4485 4486
	int member;

4487
	if (DECL_CLASS_SCOPE_P (t))
mrs's avatar
mrs committed
4488
	  {
mrs's avatar
mrs committed
4489 4490 4491 4492
	    if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
	      member = 2;
	    else
	      member = 1;
4493 4494
	    ctx = tsubst (DECL_CLASS_CONTEXT (t), args, t);
	    type = tsubst (type, args, in_decl);
mrs's avatar
mrs committed
4495 4496 4497 4498 4499
	  }
	else
	  {
	    member = 0;
	    ctx = NULL_TREE;
4500
	    type = tsubst (type, args, in_decl);
mrs's avatar
mrs committed
4501
	  }
mrs's avatar
mrs committed
4502

jason's avatar
jason committed
4503
	/* If we are instantiating a specialization, get the other args.  */
mrs's avatar
mrs committed
4504 4505
	if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
	  {
jason's avatar
jason committed
4506 4507 4508
	    tree spec;

	    tmpl = DECL_TI_TEMPLATE (t);
4509 4510

	    /* Start by getting the innermost args.  */
4511
	    argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
4512

4513 4514
	    if (DECL_TEMPLATE_INFO (tmpl))
	      argvec = complete_template_args (tmpl, argvec, 0);
mrs's avatar
mrs committed
4515

jason's avatar
jason committed
4516
	    /* Do we already have this instantiation?  */
4517
	    spec = retrieve_specialization (tmpl, argvec);
4518 4519
	    if (spec)
	      return spec;
mrs's avatar
mrs committed
4520 4521 4522 4523 4524
	  }

	/* We do NOT check for matching decls pushed separately at this
           point, as they may not represent instantiations of this
           template, and in any case are considered separate under the
mrs's avatar
mrs committed
4525
           discrete model.  Instead, see add_maybe_template.  */
mrs's avatar
mrs committed
4526 4527 4528

	r = copy_node (t);
	copy_lang_decl (r);
4529
	DECL_USE_TEMPLATE (r) = 0;
mrs's avatar
mrs committed
4530 4531 4532
	TREE_TYPE (r) = type;

	DECL_CONTEXT (r)
4533
	  = tsubst (DECL_CONTEXT (t), args, t);
mrs's avatar
mrs committed
4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554
	DECL_CLASS_CONTEXT (r) = ctx;

	if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
				IDENTIFIER_POINTER (DECL_NAME (r)),
				sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
	  {
	    /* Type-conversion operator.  Reconstruct the name, in
	       case it's the name of one of the template's parameters.  */
	    DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
	  }

	if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
	  {
	    char *buf, *dbuf = build_overload_name (ctx, 1, 1);
	    int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
	    buf = (char *) alloca (strlen (dbuf)
				   + sizeof (DESTRUCTOR_DECL_PREFIX));
	    bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
	    buf[len] = '\0';
	    strcat (buf, dbuf);
	    DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
mrs's avatar
mrs committed
4555
	  }
4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571
	else 
	  {
	    /* Instantiations of template functions must be mangled
	       specially, in order to conform to 14.5.5.1
	       [temp.over.link].  We use in_decl below rather than
	       DECL_TI_TEMPLATE (r) because the latter is set to
	       NULL_TREE in instantiate_decl.  */
	    tree tmpl;
	    tree arg_types;

	    if (DECL_TEMPLATE_INFO (r))
	      tmpl = DECL_TI_TEMPLATE (r);
	    else
	      tmpl = in_decl;

	    /* tmpl will be NULL if this is a specialization of a
4572
	       member function of a template class.  */
4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589
	    if (name_mangling_version < 1
		|| tmpl == NULL_TREE
		|| (member && !is_member_template (tmpl)
		    && !DECL_TEMPLATE_INFO (tmpl)))
	      {
		arg_types = TYPE_ARG_TYPES (type);
		if (member && TREE_CODE (type) == FUNCTION_TYPE)
		  arg_types = hash_tree_chain 
		    (build_pointer_type (DECL_CONTEXT (r)),
		     arg_types); 
		
		DECL_ASSEMBLER_NAME (r) 
		  = build_decl_overload (DECL_NAME (r), arg_types,
					 member);
	      }
	    else
	      {
jason's avatar
jason committed
4590
		tree tparms; 
4591 4592 4593 4594
		tree targs;

		if (!DECL_TEMPLATE_SPECIALIZATION (tmpl)) 
		  {
4595 4596 4597 4598 4599 4600
		    /* We pass the outermost template parameters to
		       build_template_decl_overload, since the innermost
		       template parameters are still just template
		       parameters; there are no corresponding subsitution
		       arguments.  Levels of parms that have been bound
		       before are not represented in DECL_TEMPLATE_PARMS.  */
4601 4602 4603 4604
		    tparms = DECL_TEMPLATE_PARMS (tmpl);
		    while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
		      tparms = TREE_CHAIN (tparms);
		    
4605
		    targs = innermost_args (args, 0);
4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626
		  }
		else
		  {
		    /* If the template is a specialization, then it is
		       a member template specialization.  We have
		       something like:

		       template <class T> struct S {
		         template <int i> void f();
			 template <> void f<7>();
		       };

		       and now we are forming S<double>::f<7>.
		       Therefore, the template parameters of interest
		       are those that are specialized by the template
		       (i.e., the int), not those we are using to
		       instantiate the template, i.e. the double.  */
		    tparms = DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl));
		    targs = DECL_TI_ARGS (tmpl);
		  }
		    
4627 4628 4629 4630
		my_friendly_assert (tparms != NULL_TREE
				    && TREE_CODE (tparms) == TREE_LIST,
				    0);
		tparms = TREE_VALUE (tparms);
4631
		 
4632 4633 4634 4635 4636 4637 4638 4639 4640 4641
		arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
		if (member && TREE_CODE (type) == FUNCTION_TYPE)
		  arg_types = hash_tree_chain 
		    (build_pointer_type (DECL_CONTEXT (r)),
		     arg_types); 

		DECL_ASSEMBLER_NAME (r)
		  = build_template_decl_overload 
		  (DECL_NAME (r), arg_types, 
		   TREE_TYPE (TREE_TYPE (tmpl)),
4642
		   tparms, targs, member);
4643 4644
	      }
	  }
mrs's avatar
mrs committed
4645 4646 4647
	DECL_RTL (r) = 0;
	make_decl_rtl (r, NULL_PTR, 1);

4648
	DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t);
mrs's avatar
mrs committed
4649 4650 4651 4652 4653
	DECL_MAIN_VARIANT (r) = r;
	DECL_RESULT (r) = NULL_TREE;
	DECL_INITIAL (r) = NULL_TREE;

	TREE_STATIC (r) = 0;
4654
	TREE_PUBLIC (r) = TREE_PUBLIC (t);
mrs's avatar
mrs committed
4655 4656 4657 4658
	DECL_EXTERNAL (r) = 1;
	DECL_INTERFACE_KNOWN (r) = 0;
	DECL_DEFER_OUTPUT (r) = 0;
	TREE_CHAIN (r) = NULL_TREE;
4659
	DECL_PENDING_INLINE_INFO (r) = 0;
4660
	TREE_USED (r) = 0;
mrs's avatar
mrs committed
4661 4662 4663 4664 4665 4666 4667 4668

	if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
	  grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));

	if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
	  {
	    DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);

4669 4670 4671 4672 4673 4674 4675
	    /* If we're not using ANSI overloading, then we might have
	       called duplicate_decls above, and gotten back an
	       preexisting version of this function.  We treat such a
	       function as a specialization.  Otherwise, we cleared
	       both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so
	       this condition will be false.  */
	    if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
mrs's avatar
mrs committed
4676 4677 4678 4679
	      SET_DECL_TEMPLATE_SPECIALIZATION (r);
	    else
	      SET_DECL_IMPLICIT_INSTANTIATION (r);

4680
	    register_specialization (r, tmpl, argvec);
mrs's avatar
mrs committed
4681 4682
	  }

mrs's avatar
mrs committed
4683 4684 4685 4686
	/* Like grokfndecl.  If we don't do this, pushdecl will mess up our
	   TREE_CHAIN because it doesn't find a previous decl.  Sigh.  */
	if (member
	    && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
jason's avatar
jason committed
4687
	  SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r);
mrs's avatar
mrs committed
4688

mrs's avatar
mrs committed
4689 4690 4691 4692 4693
	return r;
      }

    case PARM_DECL:
      {
mrs's avatar
mrs committed
4694 4695
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
4696 4697 4698 4699 4700
	if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
	  DECL_INITIAL (r) = TREE_TYPE (r);
	else
	  DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, in_decl);

mrs's avatar
mrs committed
4701
	DECL_CONTEXT (r) = NULL_TREE;
kenner's avatar
kenner committed
4702 4703 4704 4705 4706 4707
#ifdef PROMOTE_PROTOTYPES
	if ((TREE_CODE (type) == INTEGER_TYPE
	     || TREE_CODE (type) == ENUMERAL_TYPE)
	    && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
	  DECL_ARG_TYPE (r) = integer_type_node;
#endif
mrs's avatar
mrs committed
4708
	if (TREE_CHAIN (t))
4709
	  TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t));
mrs's avatar
mrs committed
4710 4711 4712
	return r;
      }

mrs's avatar
mrs committed
4713 4714 4715 4716 4717 4718
    case FIELD_DECL:
      {
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
	copy_lang_decl (r);
#if 0
4719
	DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl);
mrs's avatar
mrs committed
4720
#endif
4721
	DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl);
mrs's avatar
mrs committed
4722 4723 4724 4725 4726 4727 4728 4729
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }

    case USING_DECL:
      {
	tree r = copy_node (t);
	DECL_INITIAL (r)
4730
	  = tsubst_copy (DECL_INITIAL (t), args, in_decl);
mrs's avatar
mrs committed
4731 4732 4733 4734 4735 4736 4737
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }

    case VAR_DECL:
      {
	tree r;
4738
	tree ctx = tsubst_copy (DECL_CONTEXT (t), args, in_decl);
mrs's avatar
mrs committed
4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756

	/* Do we already have this instantiation?  */
	if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
	  {
	    tree tmpl = DECL_TI_TEMPLATE (t);
	    tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);

	    for (; decls; decls = TREE_CHAIN (decls))
	      if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
		return TREE_VALUE (decls);
	  }

	r = copy_node (t);
	TREE_TYPE (r) = type;
	DECL_CONTEXT (r) = ctx;
	if (TREE_STATIC (r))
	  DECL_ASSEMBLER_NAME (r)
	    = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
mrs's avatar
mrs committed
4757 4758 4759 4760

	/* Don't try to expand the initializer until someone tries to use
	   this variable; otherwise we run into circular dependencies.  */
	DECL_INITIAL (r) = NULL_TREE;
mrs's avatar
mrs committed
4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774

	DECL_RTL (r) = 0;
	DECL_SIZE (r) = 0;

	if (DECL_LANG_SPECIFIC (r))
	  {
	    copy_lang_decl (r);
	    DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
	  }

	if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
	  {
	    tree tmpl = DECL_TI_TEMPLATE (t);
	    tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
4775
	    tree argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
mrs's avatar
mrs committed
4776 4777 4778 4779 4780 4781 4782 4783 4784 4785

	    DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
	    *declsp = perm_tree_cons (argvec, r, *declsp);
	    SET_DECL_IMPLICIT_INSTANTIATION (r);
	  }
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }

    case TYPE_DECL:
mrs's avatar
mrs committed
4786 4787 4788
      if (t == TYPE_NAME (TREE_TYPE (t)))
	return TYPE_NAME (type);

mrs's avatar
mrs committed
4789 4790 4791
      {
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
mrs's avatar
mrs committed
4792
	DECL_CONTEXT (r) = current_class_type;
mrs's avatar
mrs committed
4793 4794 4795 4796
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }	  

mrs's avatar
mrs committed
4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810
    case TREE_LIST:
      {
	tree purpose, value, chain, result;
	int via_public, via_virtual, via_protected;

	if (t == void_list_node)
	  return t;

	via_public = TREE_VIA_PUBLIC (t);
	via_protected = TREE_VIA_PROTECTED (t);
	via_virtual = TREE_VIA_VIRTUAL (t);

	purpose = TREE_PURPOSE (t);
	if (purpose)
4811
	  purpose = tsubst (purpose, args, in_decl);
mrs's avatar
mrs committed
4812 4813
	value = TREE_VALUE (t);
	if (value)
4814
	  value = tsubst (value, args, in_decl);
mrs's avatar
mrs committed
4815 4816
	chain = TREE_CHAIN (t);
	if (chain && chain != void_type_node)
4817
	  chain = tsubst (chain, args, in_decl);
mrs's avatar
mrs committed
4818 4819 4820 4821 4822 4823 4824 4825 4826 4827
	if (purpose == TREE_PURPOSE (t)
	    && value == TREE_VALUE (t)
	    && chain == TREE_CHAIN (t))
	  return t;
	result = hash_tree_cons (via_public, via_virtual, via_protected,
				 purpose, value, chain);
	TREE_PARMLIST (result) = TREE_PARMLIST (t);
	return result;
      }
    case TREE_VEC:
mrs's avatar
mrs committed
4828 4829
      if (type != NULL_TREE)
	{
4830 4831
	  /* A binfo node.  */

mrs's avatar
mrs committed
4832 4833
	  t = copy_node (t);

4834 4835 4836
	  /* Make sure type isn't a typedef copy.  */
	  type = BINFO_TYPE (TYPE_BINFO (type));

mrs's avatar
mrs committed
4837 4838 4839 4840
	  if (type == TREE_TYPE (t))
	    return t;

	  TREE_TYPE (t) = complete_type (type);
mrs's avatar
mrs committed
4841 4842 4843 4844 4845 4846 4847
	  if (IS_AGGR_TYPE (type))
	    {
	      BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
	      BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
	      if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
		BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
	    }
mrs's avatar
mrs committed
4848 4849
	  return t;
	}
4850 4851

      /* Otherwise, a vector of template arguments.  */
4852
      return tsubst_template_arg_vector (t, args);
mrs's avatar
mrs committed
4853 4854 4855 4856 4857 4858

    case POINTER_TYPE:
    case REFERENCE_TYPE:
      {
	tree r;
	enum tree_code code;
4859

mrs's avatar
mrs committed
4860 4861 4862 4863
	if (type == TREE_TYPE (t))
	  return t;

	code = TREE_CODE (t);
4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890
	if (TREE_CODE (type) == REFERENCE_TYPE) 
	  {
	    static int   last_line = 0;
	    static char* last_file = 0;

	    /* We keep track of the last time we issued this error
	       message to avoid spewing a ton of messages during a
	       single bad template instantiation.  */
	    if (last_line != lineno ||
		last_file != input_filename)
	      {
		cp_error ("cannot form type %s to reference type %T during template instantiation",
			  (code == POINTER_TYPE) ? "pointer" : "reference",
			  type);
		last_line = lineno;
		last_file = input_filename;
	      }

	    /* Use the underlying type in an attempt at error
	       recovery; maybe the user meant vector<int> and wrote
	       vector<int&>, or some such.  */
	    if (code == REFERENCE_TYPE)
	      r = type;
	    else
	      r = build_pointer_type (TREE_TYPE (type));
	  }
	else if (code == POINTER_TYPE)
mrs's avatar
mrs committed
4891 4892 4893
	  r = build_pointer_type (type);
	else
	  r = build_reference_type (type);
mrs's avatar
mrs committed
4894
	r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
4895

mrs's avatar
mrs committed
4896 4897 4898 4899
	/* Will this ever be needed for TYPE_..._TO values?  */
	layout_type (r);
	return r;
      }
mrs's avatar
mrs committed
4900 4901
    case OFFSET_TYPE:
      return build_offset_type
4902
	(tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl), type);
mrs's avatar
mrs committed
4903 4904 4905
    case FUNCTION_TYPE:
    case METHOD_TYPE:
      {
4906
	tree values = TYPE_ARG_TYPES (t);
mrs's avatar
mrs committed
4907
	tree context = TYPE_CONTEXT (t);
mrs's avatar
merging  
mrs committed
4908 4909
	tree raises = TYPE_RAISES_EXCEPTIONS (t);
	tree fntype;
mrs's avatar
mrs committed
4910 4911 4912

	/* Don't bother recursing if we know it won't change anything.	*/
	if (values != void_list_node)
mrs's avatar
mrs committed
4913 4914 4915 4916
	  {
	    /* This should probably be rewritten to use hash_tree_cons for
               the memory savings.  */
	    tree first = NULL_TREE;
4917
	    tree last = NULL_TREE;
mrs's avatar
mrs committed
4918 4919 4920 4921

	    for (; values && values != void_list_node;
		 values = TREE_CHAIN (values))
	      {
mrs's avatar
mrs committed
4922
		tree value = TYPE_MAIN_VARIANT (type_decays_to
4923
		  (tsubst (TREE_VALUE (values), args, in_decl)));
mrs's avatar
mrs committed
4924 4925 4926
		/* Don't instantiate default args unless they are used.
		   Handle it in build_over_call instead.  */
		tree purpose = TREE_PURPOSE (values);
mrs's avatar
mrs committed
4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940
		tree x = build_tree_list (purpose, value);

		if (first)
		  TREE_CHAIN (last) = x;
		else
		  first = x;
		last = x;
	      }

	    if (values == void_list_node)
	      TREE_CHAIN (last) = void_list_node;

	    values = first;
	  }
mrs's avatar
mrs committed
4941
	if (context)
4942
	  context = tsubst (context, args, in_decl);
mrs's avatar
mrs committed
4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955
	/* Could also optimize cases where return value and
	   values have common elements (e.g., T min(const &T, const T&).  */

	/* If the above parameters haven't changed, just return the type.  */
	if (type == TREE_TYPE (t)
	    && values == TYPE_VALUES (t)
	    && context == TYPE_CONTEXT (t))
	  return t;

	/* Construct a new type node and return it.  */
	if (TREE_CODE (t) == FUNCTION_TYPE
	    && context == NULL_TREE)
	  {
mrs's avatar
merging  
mrs committed
4956
	    fntype = build_function_type (type, values);
mrs's avatar
mrs committed
4957 4958 4959 4960
	  }
	else if (context == NULL_TREE)
	  {
	    tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
4961
				args, in_decl);
mrs's avatar
merging  
mrs committed
4962 4963
	    fntype = build_cplus_method_type (base, type,
					      TREE_CHAIN (values));
mrs's avatar
mrs committed
4964 4965 4966
	  }
	else
	  {
mrs's avatar
merging  
mrs committed
4967 4968 4969 4970 4971 4972 4973
	    fntype = make_node (TREE_CODE (t));
	    TREE_TYPE (fntype) = type;
	    TYPE_CONTEXT (fntype) = context;
	    TYPE_VALUES (fntype) = values;
	    TYPE_SIZE (fntype) = TYPE_SIZE (t);
	    TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
	    TYPE_MODE (fntype) = TYPE_MODE (t);
mrs's avatar
mrs committed
4974
	    if (TYPE_METHOD_BASETYPE (t))
mrs's avatar
merging  
mrs committed
4975
	      TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
4976
						      args, in_decl);
mrs's avatar
mrs committed
4977 4978 4979
	    /* Need to generate hash value.  */
	    my_friendly_abort (84);
	  }
mrs's avatar
merging  
mrs committed
4980 4981 4982 4983 4984
	fntype = build_type_variant (fntype,
				     TYPE_READONLY (t),
				     TYPE_VOLATILE (t));
	if (raises)
	  {
4985
	    raises = tsubst (raises, args, in_decl);
mrs's avatar
merging  
mrs committed
4986 4987 4988
	    fntype = build_exception_variant (fntype, raises);
	  }
	return fntype;
mrs's avatar
mrs committed
4989 4990 4991
      }
    case ARRAY_TYPE:
      {
4992
	tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl);
mrs's avatar
mrs committed
4993 4994 4995 4996 4997 4998 4999 5000
	tree r;
	if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
	  return t;
	r = build_cplus_array_type (type, domain);
	return r;
      }

    case PLUS_EXPR:
mrs's avatar
mrs committed
5001
    case MINUS_EXPR:
mrs's avatar
mrs committed
5002
      return fold (build (TREE_CODE (t), TREE_TYPE (t),
5003 5004
			  tsubst (TREE_OPERAND (t, 0), args, in_decl),
			  tsubst (TREE_OPERAND (t, 1), args, in_decl)));
mrs's avatar
mrs committed
5005 5006 5007 5008

    case NEGATE_EXPR:
    case NOP_EXPR:
      return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
5009
			   tsubst (TREE_OPERAND (t, 0), args, in_decl)));
mrs's avatar
mrs committed
5010

mrs's avatar
mrs committed
5011 5012
    case TYPENAME_TYPE:
      {
5013
	tree ctx = tsubst (TYPE_CONTEXT (t), args, in_decl);
5014 5015
	tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl);
	f = make_typename_type (ctx, f);
mrs's avatar
mrs committed
5016 5017 5018 5019 5020 5021 5022
	return cp_build_type_variant
	  (f, TYPE_READONLY (f) || TYPE_READONLY (t),
	   TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
      }

    case INDIRECT_REF:
      return make_pointer_declarator
5023
	(type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
mrs's avatar
mrs committed
5024 5025 5026
      
    case ADDR_EXPR:
      return make_reference_declarator
5027
	(type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
mrs's avatar
mrs committed
5028 5029 5030

    case ARRAY_REF:
      return build_parse_node
5031 5032
	(ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst_expr (TREE_OPERAND (t, 1), args, in_decl));
mrs's avatar
mrs committed
5033 5034

    case CALL_EXPR:
mrs's avatar
merging  
mrs committed
5035
      return make_call_declarator
5036 5037
	(tsubst (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst (TREE_OPERAND (t, 1), args, in_decl),
mrs's avatar
merging  
mrs committed
5038
	 TREE_OPERAND (t, 2),
5039
	 tsubst (TREE_TYPE (t), args, in_decl));
mrs's avatar
mrs committed
5040

mrs's avatar
mrs committed
5041 5042
    case SCOPE_REF:
      return build_parse_node
5043 5044
	(TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst (TREE_OPERAND (t, 1), args, in_decl));
mrs's avatar
mrs committed
5045

mrs's avatar
mrs committed
5046
    default:
mrs's avatar
mrs committed
5047
      sorry ("use of `%s' in template",
mrs's avatar
mrs committed
5048 5049 5050 5051 5052
	     tree_code_name [(int) TREE_CODE (t)]);
      return error_mark_node;
    }
}

mrs's avatar
mrs committed
5053 5054 5055 5056 5057 5058 5059 5060 5061 5062
void
do_pushlevel ()
{
  emit_line_note (input_filename, lineno);
  pushlevel (0);
  clear_last_expr ();
  push_momentary ();
  expand_start_bindings (0);
}  

mrs's avatar
mrs committed
5063
tree
mrs's avatar
mrs committed
5064
do_poplevel ()
mrs's avatar
mrs committed
5065
{
mrs's avatar
mrs committed
5066
  tree t;
5067
  int saved_warn_unused = 0;
mrs's avatar
mrs committed
5068

5069 5070 5071 5072 5073
  if (processing_template_decl)
    {
      saved_warn_unused = warn_unused;
      warn_unused = 0;
    }
5074
  expand_end_bindings (getdecls (), kept_level_p (), 0);
5075 5076
  if (processing_template_decl)
    warn_unused = saved_warn_unused;
mrs's avatar
mrs committed
5077 5078 5079 5080
  t = poplevel (kept_level_p (), 1, 0);
  pop_momentary ();
  return t;
}
mrs's avatar
mrs committed
5081

5082 5083 5084 5085
/* Like tsubst, but deals with expressions.  This function just replaces
   template parms; to finish processing the resultant expression, use
   tsubst_expr.  */

mrs's avatar
mrs committed
5086
tree
5087
tsubst_copy (t, args, in_decl)
5088
     tree t, args;
mrs's avatar
mrs committed
5089 5090 5091
     tree in_decl;
{
  enum tree_code code;
mrs's avatar
mrs committed
5092

mrs's avatar
mrs committed
5093 5094 5095 5096
  if (t == NULL_TREE || t == error_mark_node)
    return t;

  code = TREE_CODE (t);
mrs's avatar
mrs committed
5097

mrs's avatar
mrs committed
5098 5099 5100 5101 5102 5103 5104 5105 5106
  switch (code)
    {
    case PARM_DECL:
      return do_identifier (DECL_NAME (t), 0);

    case CONST_DECL:
    case FIELD_DECL:
      if (DECL_CONTEXT (t))
	{
5107 5108
	  tree ctx;
	  if (TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
mrs's avatar
mrs committed
5109
	    return lookup_name (DECL_NAME (t), 0);
5110 5111 5112

	  ctx = tsubst (DECL_CONTEXT (t), args, in_decl);
	  if (ctx != DECL_CONTEXT (t))
mrs's avatar
mrs committed
5113 5114 5115 5116 5117 5118 5119
	    return lookup_field (ctx, DECL_NAME (t), 0, 0);
	}
      return t;

    case VAR_DECL:
    case FUNCTION_DECL:
      if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
5120
	t = tsubst (t, args, in_decl);
mrs's avatar
mrs committed
5121 5122 5123
      mark_used (t);
      return t;

5124 5125
    case TEMPLATE_DECL:
      if (is_member_template (t))
5126
	return tsubst (t, args, in_decl);
5127 5128 5129
      else
	return t;

mrs's avatar
mrs committed
5130 5131 5132 5133 5134 5135 5136
#if 0
    case IDENTIFIER_NODE:
      return do_identifier (t, 0);
#endif
      
    case CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
mrs's avatar
mrs committed
5137 5138 5139
    case CONST_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
mrs's avatar
mrs committed
5140
      return build1
5141 5142
	(code, tsubst (TREE_TYPE (t), args, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
mrs's avatar
mrs committed
5143 5144 5145 5146 5147 5148 5149 5150

    case INDIRECT_REF:
    case PREDECREMENT_EXPR:
    case PREINCREMENT_EXPR:
    case POSTDECREMENT_EXPR:
    case POSTINCREMENT_EXPR:
    case NEGATE_EXPR:
    case TRUTH_NOT_EXPR:
mrs's avatar
mrs committed
5151
    case BIT_NOT_EXPR:
mrs's avatar
mrs committed
5152 5153 5154
    case ADDR_EXPR:
    case CONVERT_EXPR:      /* Unary + */
    case SIZEOF_EXPR:
5155
    case ALIGNOF_EXPR:
mrs's avatar
mrs committed
5156
    case ARROW_EXPR:
mrs's avatar
mrs committed
5157
    case THROW_EXPR:
mrs's avatar
mrs committed
5158
    case TYPEID_EXPR:
mrs's avatar
mrs committed
5159 5160
      return build1
	(code, NULL_TREE,
5161
	 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
mrs's avatar
mrs committed
5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199

    case PLUS_EXPR:
    case MINUS_EXPR:
    case MULT_EXPR:
    case TRUNC_DIV_EXPR:
    case CEIL_DIV_EXPR:
    case FLOOR_DIV_EXPR:
    case ROUND_DIV_EXPR:
    case EXACT_DIV_EXPR:
    case BIT_AND_EXPR:
    case BIT_ANDTC_EXPR:
    case BIT_IOR_EXPR:
    case BIT_XOR_EXPR:
    case TRUNC_MOD_EXPR:
    case FLOOR_MOD_EXPR:
    case TRUTH_ANDIF_EXPR:
    case TRUTH_ORIF_EXPR:
    case TRUTH_AND_EXPR:
    case TRUTH_OR_EXPR:
    case RSHIFT_EXPR:
    case LSHIFT_EXPR:
    case RROTATE_EXPR:
    case LROTATE_EXPR:
    case EQ_EXPR:
    case NE_EXPR:
    case MAX_EXPR:
    case MIN_EXPR:
    case LE_EXPR:
    case GE_EXPR:
    case LT_EXPR:
    case GT_EXPR:
    case COMPONENT_REF:
    case ARRAY_REF:
    case COMPOUND_EXPR:
    case SCOPE_REF:
    case DOTSTAR_EXPR:
    case MEMBER_REF:
      return build_nt
5200 5201
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
mrs's avatar
mrs committed
5202 5203 5204 5205

    case CALL_EXPR:
      {
	tree fn = TREE_OPERAND (t, 0);
jason's avatar
jason committed
5206
	if (is_overloaded_fn (fn))
5207
	  fn = tsubst_copy (get_first_fn (fn), args, in_decl);
mrs's avatar
mrs committed
5208
	else
jason's avatar
jason committed
5209
	  /* Sometimes FN is a LOOKUP_EXPR.  */
5210
	  fn = tsubst_copy (fn, args, in_decl);
mrs's avatar
mrs committed
5211
	return build_nt
5212
	  (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
mrs's avatar
mrs committed
5213 5214 5215 5216 5217 5218 5219 5220
	   NULL_TREE);
      }

    case METHOD_CALL_EXPR:
      {
	tree name = TREE_OPERAND (t, 0);
	if (TREE_CODE (name) == BIT_NOT_EXPR)
	  {
5221
	    name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
5222 5223 5224
	    if (TREE_CODE (name) != IDENTIFIER_NODE)
	      name = TYPE_MAIN_VARIANT (name);
	    name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
mrs's avatar
mrs committed
5225 5226 5227 5228
	  }
	else if (TREE_CODE (name) == SCOPE_REF
		 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
	  {
5229
	    tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
mrs's avatar
mrs committed
5230
	    name = TREE_OPERAND (name, 1);
5231
	    name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
5232 5233 5234
	    if (TREE_CODE (name) != IDENTIFIER_NODE)
	      name = TYPE_MAIN_VARIANT (name);
	    name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
mrs's avatar
mrs committed
5235 5236 5237
	    name = build_nt (SCOPE_REF, base, name);
	  }
	else
5238
	  name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
mrs's avatar
mrs committed
5239
	return build_nt
5240 5241
	  (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
	   tsubst_copy (TREE_OPERAND (t, 2), args, in_decl),
mrs's avatar
mrs committed
5242 5243 5244
	   NULL_TREE);
      }

5245
    case BIND_EXPR:
mrs's avatar
mrs committed
5246 5247
    case COND_EXPR:
    case MODOP_EXPR:
5248 5249
      {
	tree r = build_nt
5250 5251 5252
	  (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
	   tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
	   tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
5253 5254 5255 5256 5257 5258 5259 5260 5261

	if (code == BIND_EXPR && !processing_template_decl)
	  {
	    /* This processing  should really occur in tsubst_expr,
	       However, tsubst_expr does not recurse into expressions,
	       since it assumes that there aren't any statements
	       inside them.  Instead, it simply calls
	       build_expr_from_tree.  So, we need to expand the
	       BIND_EXPR here.  */ 
5262
	    tree rtl_expr = begin_stmt_expr ();
5263
	    tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl);
5264
	    r = finish_stmt_expr (rtl_expr, block);
5265 5266 5267 5268
	  }

	return r;
      }
mrs's avatar
mrs committed
5269 5270 5271 5272

    case NEW_EXPR:
      {
	tree r = build_nt
5273 5274 5275
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
mrs's avatar
mrs committed
5276 5277 5278 5279 5280 5281 5282
	NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
	return r;
      }

    case DELETE_EXPR:
      {
	tree r = build_nt
5283 5284
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
mrs's avatar
mrs committed
5285 5286 5287 5288 5289
	DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
	DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
	return r;
      }

5290 5291
    case TEMPLATE_ID_EXPR:
      {
5292
        /* Substituted template arguments */
5293
	tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl);
5294 5295 5296 5297 5298
	tree chain;
	for (chain = targs; chain; chain = TREE_CHAIN (chain))
	  TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));

	return lookup_template_function
5299
	  (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs);
5300 5301
      }

mrs's avatar
mrs committed
5302 5303 5304 5305 5306 5307 5308 5309 5310
    case TREE_LIST:
      {
	tree purpose, value, chain;

	if (t == void_list_node)
	  return t;

	purpose = TREE_PURPOSE (t);
	if (purpose)
5311
	  purpose = tsubst_copy (purpose, args, in_decl);
mrs's avatar
mrs committed
5312 5313
	value = TREE_VALUE (t);
	if (value)
5314
	  value = tsubst_copy (value, args, in_decl);
mrs's avatar
mrs committed
5315 5316
	chain = TREE_CHAIN (t);
	if (chain && chain != void_type_node)
5317
	  chain = tsubst_copy (chain, args, in_decl);
mrs's avatar
mrs committed
5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329
	if (purpose == TREE_PURPOSE (t)
	    && value == TREE_VALUE (t)
	    && chain == TREE_CHAIN (t))
	  return t;
	return tree_cons (purpose, value, chain);
      }

    case RECORD_TYPE:
    case UNION_TYPE:
    case ENUMERAL_TYPE:
    case INTEGER_TYPE:
    case TEMPLATE_TYPE_PARM:
5330
    case TEMPLATE_TEMPLATE_PARM:
jason's avatar
jason committed
5331
    case TEMPLATE_PARM_INDEX:
mrs's avatar
mrs committed
5332 5333 5334 5335 5336 5337 5338
    case POINTER_TYPE:
    case REFERENCE_TYPE:
    case OFFSET_TYPE:
    case FUNCTION_TYPE:
    case METHOD_TYPE:
    case ARRAY_TYPE:
    case TYPENAME_TYPE:
jason's avatar
jason committed
5339
    case TYPE_DECL:
5340
      return tsubst (t, args, in_decl);
mrs's avatar
mrs committed
5341

mrs's avatar
mrs committed
5342 5343 5344
    case IDENTIFIER_NODE:
      if (IDENTIFIER_TYPENAME_P (t))
	return build_typename_overload
5345
	  (tsubst (TREE_TYPE (t), args, in_decl));
mrs's avatar
mrs committed
5346 5347 5348
      else
	return t;

mrs's avatar
mrs committed
5349 5350
    case CONSTRUCTOR:
      return build
5351 5352
	(CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE,
	 tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl));
mrs's avatar
mrs committed
5353

mrs's avatar
mrs committed
5354 5355 5356 5357 5358
    default:
      return t;
    }
}

5359 5360
/* Like tsubst_copy, but also does semantic processing and RTL expansion.  */

mrs's avatar
mrs committed
5361
tree
5362
tsubst_expr (t, args, in_decl)
5363
     tree t, args;
mrs's avatar
mrs committed
5364 5365 5366 5367 5368
     tree in_decl;
{
  if (t == NULL_TREE || t == error_mark_node)
    return t;

mrs's avatar
mrs committed
5369
  if (processing_template_decl)
5370
    return tsubst_copy (t, args, in_decl);
mrs's avatar
mrs committed
5371 5372

  switch (TREE_CODE (t))
mrs's avatar
mrs committed
5373
    {
mrs's avatar
mrs committed
5374 5375
    case RETURN_STMT:
      lineno = TREE_COMPLEXITY (t);
5376 5377
      finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
				       args, in_decl));
mrs's avatar
mrs committed
5378 5379 5380 5381
      break;

    case EXPR_STMT:
      lineno = TREE_COMPLEXITY (t);
5382 5383
      finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
				     args, in_decl));
mrs's avatar
mrs committed
5384 5385 5386 5387 5388
      break;

    case DECL_STMT:
      {
	int i = suspend_momentary ();
mrs's avatar
mrs committed
5389
	tree dcl, init;
mrs's avatar
mrs committed
5390 5391 5392 5393

	lineno = TREE_COMPLEXITY (t);
	emit_line_note (input_filename, lineno);
	dcl = start_decl
5394 5395
	  (tsubst (TREE_OPERAND (t, 0), args, in_decl),
	   tsubst (TREE_OPERAND (t, 1), args, in_decl),
bkoz's avatar
d  
bkoz committed
5396
	   TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE);
5397
	init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl);
mrs's avatar
mrs committed
5398
	cp_finish_decl
mrs's avatar
mrs committed
5399
	  (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
mrs's avatar
mrs committed
5400 5401 5402
	resume_momentary (i);
	return dcl;
      }
mrs's avatar
mrs committed
5403

mrs's avatar
mrs committed
5404 5405 5406 5407 5408
    case FOR_STMT:
      {
	tree tmp;
	lineno = TREE_COMPLEXITY (t);

5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419
	begin_for_stmt ();
	for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
	  tsubst_expr (tmp, args, in_decl);
	finish_for_init_stmt (NULL_TREE);
	finish_for_cond (tsubst_expr (FOR_COND (t), args,
				      in_decl),
			 NULL_TREE);
	tmp = tsubst_expr (FOR_EXPR (t), args, in_decl);
	finish_for_expr (tmp, NULL_TREE);
	tsubst_expr (FOR_BODY (t), args, in_decl);
	finish_for_stmt (tmp, NULL_TREE);
mrs's avatar
mrs committed
5420 5421
      }
      break;
mrs's avatar
mrs committed
5422

mrs's avatar
mrs committed
5423 5424 5425
    case WHILE_STMT:
      {
	lineno = TREE_COMPLEXITY (t);
5426 5427 5428 5429 5430 5431
	begin_while_stmt ();
	finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
					     args, in_decl),
				NULL_TREE);
	tsubst_expr (WHILE_BODY (t), args, in_decl);
	finish_while_stmt (NULL_TREE);
mrs's avatar
mrs committed
5432 5433
      }
      break;
mrs's avatar
mrs committed
5434

mrs's avatar
mrs committed
5435 5436 5437
    case DO_STMT:
      {
	lineno = TREE_COMPLEXITY (t);
5438 5439 5440 5441 5442 5443
	begin_do_stmt ();
	tsubst_expr (DO_BODY (t), args, in_decl);
	finish_do_body (NULL_TREE);
	finish_do_stmt (tsubst_expr (DO_COND (t), args,
				     in_decl),
			NULL_TREE);
mrs's avatar
mrs committed
5444 5445
      }
      break;
mrs's avatar
mrs committed
5446

mrs's avatar
mrs committed
5447
    case IF_STMT:
mrs's avatar
mrs committed
5448
      {
mrs's avatar
mrs committed
5449 5450 5451
	tree tmp;

	lineno = TREE_COMPLEXITY (t);
5452 5453 5454 5455
	begin_if_stmt ();
	finish_if_stmt_cond (tsubst_expr (IF_COND (t),
					  args, in_decl),
			     NULL_TREE);
mrs's avatar
mrs committed
5456

5457
	if (tmp = THEN_CLAUSE (t), tmp)
mrs's avatar
mrs committed
5458
	  {
5459
	    tsubst_expr (tmp, args, in_decl);
5460
	    finish_then_clause (NULL_TREE);
mrs's avatar
mrs committed
5461 5462
	  }

5463 5464 5465 5466 5467 5468
	if (tmp = ELSE_CLAUSE (t), tmp)
	  {
	    begin_else_clause ();
	    tsubst_expr (tmp, args, in_decl);
	    finish_else_clause (NULL_TREE);
	  }
mrs's avatar
mrs committed
5469

5470
	finish_if_stmt ();
mrs's avatar
mrs committed
5471
      }
mrs's avatar
mrs committed
5472
      break;
mrs's avatar
mrs committed
5473

mrs's avatar
mrs committed
5474 5475
    case COMPOUND_STMT:
      {
5476
	tree substmt;
mrs's avatar
mrs committed
5477

mrs's avatar
mrs committed
5478
	lineno = TREE_COMPLEXITY (t);
5479 5480 5481 5482
	begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
	for (substmt = COMPOUND_BODY (t); 
	     substmt != NULL_TREE;
	     substmt = TREE_CHAIN (substmt))
5483
	  tsubst_expr (substmt, args, in_decl);
5484 5485
	return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), 
				     NULL_TREE);
mrs's avatar
mrs committed
5486 5487
      }
      break;
mrs's avatar
mrs committed
5488

mrs's avatar
mrs committed
5489 5490
    case BREAK_STMT:
      lineno = TREE_COMPLEXITY (t);
5491
      finish_break_stmt ();
mrs's avatar
mrs committed
5492
      break;
mrs's avatar
mrs committed
5493

mrs's avatar
mrs committed
5494 5495
    case CONTINUE_STMT:
      lineno = TREE_COMPLEXITY (t);
5496
      finish_continue_stmt ();
mrs's avatar
mrs committed
5497 5498
      break;

mrs's avatar
mrs committed
5499 5500 5501 5502 5503
    case SWITCH_STMT:
      {
	tree val, tmp;

	lineno = TREE_COMPLEXITY (t);
5504 5505 5506
	begin_switch_stmt ();
	val = tsubst_expr (SWITCH_COND (t), args, in_decl);
	finish_switch_cond (val);
mrs's avatar
mrs committed
5507 5508
	
	if (tmp = TREE_OPERAND (t, 1), tmp)
5509
	  tsubst_expr (tmp, args, in_decl);
mrs's avatar
mrs committed
5510

5511
	finish_switch_stmt (val, NULL_TREE);
mrs's avatar
mrs committed
5512 5513 5514 5515
      }
      break;

    case CASE_LABEL:
5516 5517
      finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl),
			 tsubst_expr (CASE_HIGH (t), args, in_decl));
mrs's avatar
mrs committed
5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528
      break;

    case LABEL_DECL:
      t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
			DECL_NAME (t));
      if (t)
	expand_label (t);
      break;

    case GOTO_STMT:
      lineno = TREE_COMPLEXITY (t);
mmitchel's avatar
 
mmitchel committed
5529 5530 5531 5532 5533 5534 5535
      t = GOTO_DESTINATION (t);
      if (TREE_CODE (t) != IDENTIFIER_NODE)
	/* Computed goto's must be tsubst'd into.  On the other hand,
	   non-computed gotos must not be; the identifier in question
	   will have no binding.  */
	t = tsubst_expr (t, args, in_decl);
      finish_goto_stmt (t);
5536 5537 5538 5539 5540 5541 5542 5543 5544
      break;

    case ASM_STMT:
      lineno = TREE_COMPLEXITY (t);
      finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl),
		       tsubst_expr (ASM_STRING (t), args, in_decl),
		       tsubst_expr (ASM_OUTPUTS (t), args, in_decl),
		       tsubst_expr (ASM_INPUTS (t), args, in_decl), 
		       tsubst_expr (ASM_CLOBBERS (t), args, in_decl));
mrs's avatar
mrs committed
5545
      break;
mrs's avatar
mrs committed
5546 5547 5548

    case TRY_BLOCK:
      lineno = TREE_COMPLEXITY (t);
5549 5550 5551
      begin_try_block ();
      tsubst_expr (TRY_STMTS (t), args, in_decl);
      finish_try_block (NULL_TREE);
mrs's avatar
mrs committed
5552
      {
5553
	tree handler = TRY_HANDLERS (t);
mrs's avatar
mrs committed
5554
	for (; handler; handler = TREE_CHAIN (handler))
5555
	  tsubst_expr (handler, args, in_decl);
mrs's avatar
mrs committed
5556
      }
5557
      finish_handler_sequence (NULL_TREE);
mrs's avatar
mrs committed
5558 5559 5560 5561
      break;

    case HANDLER:
      lineno = TREE_COMPLEXITY (t);
5562 5563
      begin_handler ();
      if (HANDLER_PARMS (t))
mrs's avatar
mrs committed
5564
	{
5565
	  tree d = HANDLER_PARMS (t);
mrs's avatar
mrs committed
5566
	  expand_start_catch_block
5567 5568
	    (tsubst (TREE_OPERAND (d, 1), args, in_decl),
	     tsubst (TREE_OPERAND (d, 0), args, in_decl));
mrs's avatar
mrs committed
5569 5570 5571
	}
      else
	expand_start_catch_block (NULL_TREE, NULL_TREE);
5572 5573 5574
      finish_handler_parms (NULL_TREE);
      tsubst_expr (HANDLER_BODY (t), args, in_decl);
      finish_handler (NULL_TREE);
mrs's avatar
mrs committed
5575 5576
      break;

mrs's avatar
mrs committed
5577 5578 5579 5580
    case TAG_DEFN:
      lineno = TREE_COMPLEXITY (t);
      t = TREE_TYPE (t);
      if (TREE_CODE (t) == ENUMERAL_TYPE)
5581
	tsubst_enum (t, args, NULL);
mrs's avatar
mrs committed
5582 5583
      break;

mrs's avatar
mrs committed
5584
    default:
5585
      return build_expr_from_tree (tsubst_copy (t, args, in_decl));
mrs's avatar
mrs committed
5586 5587
    }
  return NULL_TREE;
mrs's avatar
mrs committed
5588 5589
}

mrs's avatar
mrs committed
5590 5591
tree
instantiate_template (tmpl, targ_ptr)
5592
     tree tmpl, targ_ptr;
mrs's avatar
mrs committed
5593
{
mrs's avatar
mrs committed
5594 5595 5596 5597 5598
  tree fndecl;
  int i, len;
  struct obstack *old_fmp_obstack;
  extern struct obstack *function_maybepermanent_obstack;

5599 5600 5601
  if (tmpl == error_mark_node)
    return error_mark_node;

5602 5603
  my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);

jason's avatar
jason committed
5604 5605
  /* FIXME this won't work with member templates; we only have one level
     of args here.  */
5606 5607
  if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    {
5608 5609
      /* Check to see if we already have this specialization.  */
      tree spec = retrieve_specialization (tmpl, targ_ptr);
5610
      
5611 5612
      if (spec != NULL_TREE)
	return spec;
5613 5614
    }

mrs's avatar
mrs committed
5615 5616 5617
  push_obstacks (&permanent_obstack, &permanent_obstack);
  old_fmp_obstack = function_maybepermanent_obstack;
  function_maybepermanent_obstack = &permanent_obstack;
mrs's avatar
mrs committed
5618

5619
  len = DECL_NTPARMS (tmpl);
mrs's avatar
mrs committed
5620

mrs's avatar
mrs committed
5621 5622
  i = len;
  while (i--)
mrs's avatar
mrs committed
5623
    {
5624
      tree t = TREE_VEC_ELT (targ_ptr, i);
mrs's avatar
mrs committed
5625 5626 5627
      if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
	{
	  tree nt = target_type (t);
mrs's avatar
mrs committed
5628
	  if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
mrs's avatar
mrs committed
5629 5630 5631 5632 5633 5634 5635
	    {
	      cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
	      cp_error ("  trying to instantiate `%D'", tmpl);
	      fndecl = error_mark_node;
	      goto out;
	    }
	}
5636
      TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
mrs's avatar
mrs committed
5637
    }
5638
  targ_ptr = copy_to_permanent (targ_ptr);
mrs's avatar
mrs committed
5639

mrs's avatar
mrs committed
5640
  /* substitute template parameters */
5641
  fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, tmpl);
mrs's avatar
mrs committed
5642

mrs's avatar
mrs committed
5643 5644 5645
  if (flag_external_templates)
    add_pending_template (fndecl);

mrs's avatar
mrs committed
5646 5647 5648
 out:
  function_maybepermanent_obstack = old_fmp_obstack;
  pop_obstacks ();
mrs's avatar
mrs committed
5649

mrs's avatar
mrs committed
5650
  return fndecl;
mrs's avatar
mrs committed
5651
}
mrs's avatar
mrs committed
5652 5653

/* Push the name of the class template into the scope of the instantiation.  */
mrs's avatar
mrs committed
5654 5655

void
mrs's avatar
mrs committed
5656 5657
overload_template_name (type)
     tree type;
mrs's avatar
mrs committed
5658
{
mrs's avatar
mrs committed
5659 5660
  tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
  tree decl;
mrs's avatar
mrs committed
5661

mrs's avatar
mrs committed
5662 5663 5664
  if (IDENTIFIER_CLASS_VALUE (id)
      && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
    return;
mrs's avatar
mrs committed
5665

mrs's avatar
mrs committed
5666 5667 5668
  decl = build_decl (TYPE_DECL, id, type);
  SET_DECL_ARTIFICIAL (decl);
  pushdecl_class_level (decl);
mrs's avatar
mrs committed
5669 5670
}

5671

law's avatar
law committed
5672
/* Like type_unification but designed specially to handle conversion
5673 5674
   operators.  The EXTRA_FN_ARG, if any, is the type of an additional
   parameter to be added to the beginning of FN's parameter list.  */
5675 5676

int
5677 5678
fn_type_unification (fn, explicit_targs, targs, args, return_type,
		     strict, extra_fn_arg)
5679
     tree fn, explicit_targs, targs, args, return_type;
5680
     unification_kind_t strict;
5681
     tree extra_fn_arg;
5682
{
5683
  int i;
5684 5685 5686 5687 5688 5689 5690 5691 5692
  tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
  tree decl_arg_types = args;

  my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);

  if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn))) 
    {
      /* This is a template conversion operator.  Use the return types
         as well as the argument types.  */
5693
      fn_arg_types = scratch_tree_cons (NULL_TREE, 
5694 5695
					TREE_TYPE (TREE_TYPE (fn)),
					fn_arg_types);
5696
      decl_arg_types = scratch_tree_cons (NULL_TREE,
5697 5698
					  return_type,
					  decl_arg_types);
5699 5700
    }

5701 5702 5703 5704
  if (extra_fn_arg != NULL_TREE)
    fn_arg_types = scratch_tree_cons (NULL_TREE, extra_fn_arg,
				      fn_arg_types); 

5705 5706 5707 5708
  /* We allow incomplete unification without an error message here
     because the standard doesn't seem to explicitly prohibit it.  Our
     callers must be ready to deal with unification failures in any
     event.  */
5709
  i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn), 
5710
			targs,
5711 5712
			fn_arg_types,
			decl_arg_types,
5713
			explicit_targs,
5714
			strict, 1);
5715 5716 5717 5718 5719

  return i;
}


mrs's avatar
mrs committed
5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737
/* Type unification.

   We have a function template signature with one or more references to
   template parameters, and a parameter list we wish to fit to this
   template.  If possible, produce a list of parameters for the template
   which will cause it to fit the supplied parameter list.

   Return zero for success, 2 for an incomplete match that doesn't resolve
   all the types, and 1 for complete failure.  An error message will be
   printed only for an incomplete match.

   TPARMS[NTPARMS] is an array of template parameter types;
   TARGS[NTPARMS] is the array of template parameter values.  PARMS is
   the function template's signature (using TEMPLATE_PARM_IDX nodes),
   and ARGS is the argument list we're trying to match against it.

   If SUBR is 1, we're being called recursively (to unify the arguments of
   a function or method parameter of a function template), so don't zero
mrs's avatar
mrs committed
5738 5739
   out targs and don't fail on an incomplete match.

5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757
   The parameter STRICT is one of:

   DEDUCE_CALL: 
     We are deducing arguments for a function call, as in
     [temp.deduct.call].

   DEDUCE_CONV:
     We are deducing arguments for a conversion function, as in 
     [temp.deduct.conv].

   DEDUCE_EXACT:
     We are deducing arguments when calculating the partial
     ordering between specializations of function or class
     templates, as in [temp.func.order] and [temp.class.order],
     when doing an explicit instantiation as in [temp.explicit],
     when determiningan explicit specialization as in
     [temp.expl.spec], or when taking the address of a function
     template, as in [temp.deduct.funcaddr].  */
mrs's avatar
mrs committed
5758 5759

int
5760
type_unification (tparms, targs, parms, args, targs_in,
5761
		  strict, allow_incomplete)
5762
     tree tparms, targs, parms, args, targs_in;
5763 5764
     unification_kind_t strict;
     int allow_incomplete;
5765
{
5766
  int* explicit_mask;
5767 5768
  int i;

5769
  for (i = 0; i < TREE_VEC_LENGTH (tparms); i++)
5770
    TREE_VEC_ELT (targs, i) = NULL_TREE;
5771

5772 5773 5774 5775
  if (targs_in != NULL_TREE)
    {
      tree arg_vec;
      arg_vec = coerce_template_parms (tparms, targs_in, NULL_TREE, 0,
5776
				       0, 0);
5777 5778 5779

      if (arg_vec == error_mark_node)
	return 1;
5780

5781
      explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs));
ghazi's avatar
ghazi committed
5782
      bzero ((char *) explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs));
5783

5784 5785
      for (i = 0; 
	   i < TREE_VEC_LENGTH (arg_vec) 
5786
	     && TREE_VEC_ELT (arg_vec, i) != NULL_TREE;  
5787
	   ++i)
5788 5789 5790 5791 5792
	{
	  TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i);
	  /* Let unify know that this argument was explicit.  */
	  explicit_mask [i] = 1;
	}
5793
    }
5794 5795
  else
    explicit_mask = 0;
5796

5797 5798 5799 5800 5801 5802 5803
  return 
    type_unification_real (tparms, targs, parms, args, 0,
			   strict, allow_incomplete, explicit_mask); 
}

/* Adjust types before performing type deduction, as described in
   [temp.deduct.call] and [temp.deduct.conv].  The rules in these two
mmitchel's avatar
mmitchel committed
5804 5805 5806 5807
   sections are symmetric.  PARM is the type of a function parameter
   or the return type of the conversion function.  ARG is the type of
   the argument passed to the call, or the type of the value
   intialized with the result of the conversion function.  */
5808

5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821
void
maybe_adjust_types_for_deduction (strict, parm, arg)
     unification_kind_t strict;
     tree* parm;
     tree* arg;
{
  switch (strict)
    {
    case DEDUCE_CALL:
      break;

    case DEDUCE_CONV:
      {
mmitchel's avatar
mmitchel committed
5822 5823 5824
	/* Swap PARM and ARG throughout the remainder of this
	   function; the handling is precisely symmetric since PARM
	   will initialize ARG rather than vice versa.  */
5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874
	tree* temp = parm;
	parm = arg;
	arg = temp;
	break;
      }

    case DEDUCE_EXACT:
      /* There is nothing to do in this case.  */
      return;

    default:
      my_friendly_abort (0);
    }

  if (TREE_CODE (*parm) != REFERENCE_TYPE)
    {
      /* [temp.deduct.call]
	 
	 If P is not a reference type:
	 
	 --If A is an array type, the pointer type produced by the
	 array-to-pointer standard conversion (_conv.array_) is
	 used in place of A for type deduction; otherwise,
	 
	 --If A is a function type, the pointer type produced by
	 the function-to-pointer standard conversion
	 (_conv.func_) is used in place of A for type deduction;
	 otherwise,
	 
	 --If A is a cv-qualified type, the top level
	 cv-qualifiers of A's type are ignored for type
	 deduction.  */
      if (TREE_CODE (*arg) == ARRAY_TYPE)
	*arg = build_pointer_type (TREE_TYPE (*arg));
      else if (TREE_CODE (*arg) == FUNCTION_TYPE
	  || TREE_CODE (*arg) == METHOD_TYPE)
	*arg = build_pointer_type (*arg);
      else
	*arg = TYPE_MAIN_VARIANT (*arg);
    }
  
  /* [temp.deduct.call]
     
     If P is a cv-qualified type, the top level cv-qualifiers
     of P's type are ignored for type deduction.  If P is a
     reference type, the type referred to by P is used for
     type deduction.  */
  *parm = TYPE_MAIN_VARIANT (*parm);
  if (TREE_CODE (*parm) == REFERENCE_TYPE)
    *parm = TREE_TYPE (*parm);
5875 5876
}

5877 5878 5879
/* Like type_unfication.  EXPLICIT_MASK, if non-NULL, is an array of
   integers, with ones in positions corresponding to arguments in
   targs that were provided explicitly, and zeros elsewhere.  */
5880

5881
static int
5882
type_unification_real (tparms, targs, parms, args, subr,
5883 5884
		       strict, allow_incomplete, explicit_mask)
     tree tparms, targs, parms, args;
5885 5886 5887
     int subr;
     unification_kind_t strict;
     int allow_incomplete;
5888
     int* explicit_mask;
mrs's avatar
mrs committed
5889 5890 5891 5892
{
  tree parm, arg;
  int i;
  int ntparms = TREE_VEC_LENGTH (tparms);
5893
  int sub_strict;
mrs's avatar
mrs committed
5894 5895

  my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
5896 5897
  my_friendly_assert (parms == NULL_TREE 
		      || TREE_CODE (parms) == TREE_LIST, 290);
mrs's avatar
mrs committed
5898
  /* ARGS could be NULL (via a call from parse.y to
mrs's avatar
mrs committed
5899 5900 5901 5902 5903
     build_x_function_call).  */
  if (args)
    my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
  my_friendly_assert (ntparms > 0, 292);

5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921
  switch (strict)
    {
    case DEDUCE_CALL:
      sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED;
      break;
      
    case DEDUCE_CONV:
      sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
      break;

    case DEDUCE_EXACT:
      sub_strict = UNIFY_ALLOW_NONE;
      break;
      
    default:
      my_friendly_abort (0);
    }

mrs's avatar
mrs committed
5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935
  while (parms
	 && parms != void_list_node
	 && args
	 && args != void_list_node)
    {
      parm = TREE_VALUE (parms);
      parms = TREE_CHAIN (parms);
      arg = TREE_VALUE (args);
      args = TREE_CHAIN (args);

      if (arg == error_mark_node)
	return 1;
      if (arg == unknown_type_node)
	return 1;
mrs's avatar
mrs committed
5936

5937 5938 5939 5940 5941
      /* Conversions will be performed on a function argument that
	 corresponds with a function parameter that contains only
	 non-deducible template parameters and explicitly specified
	 template parameters.  */
      if (! uses_template_parms (parm))
mrs's avatar
mrs committed
5942
	{
5943 5944 5945 5946 5947 5948 5949 5950 5951 5952
	  tree type;

	  if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
	    type = TREE_TYPE (arg);
	  else
	    {
	      type = arg;
	      arg = NULL_TREE;
	    }

5953
	  if (strict == DEDUCE_EXACT)
5954 5955 5956 5957 5958
	    {
	      if (comptypes (parm, type, 1))
		continue;
	    }
	  else
5959 5960 5961 5962
	    /* It might work; we shouldn't check now, because we might
	       get into infinite recursion.  Overload resolution will
	       handle it.  */
	    continue;
5963

mrs's avatar
mrs committed
5964 5965 5966
	  return 1;
	}
	
mrs's avatar
mrs committed
5967 5968 5969 5970 5971 5972 5973 5974 5975
#if 0
      if (TREE_CODE (arg) == VAR_DECL)
	arg = TREE_TYPE (arg);
      else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
	arg = TREE_TYPE (arg);
#else
      if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
	{
	  my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
jason's avatar
jason committed
5976 5977
	  if (TREE_CODE (arg) == OVERLOAD
	      && TREE_CODE (OVL_FUNCTION (arg)) == TEMPLATE_DECL)
mrs's avatar
mrs committed
5978
	    {
5979
	      tree targs;
5980
	      tree arg_type;
mrs's avatar
mrs committed
5981 5982

	      /* Have to back unify here */
jason's avatar
jason committed
5983
	      arg = OVL_FUNCTION (arg);
5984 5985 5986
	      targs = make_scratch_vec (DECL_NTPARMS (arg));
	      arg_type = TREE_TYPE (arg);
	      maybe_adjust_types_for_deduction (strict, &parm, &arg_type);
5987
	      parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
5988
	      arg_type = scratch_tree_cons (NULL_TREE, arg_type, NULL_TREE);
5989 5990
	      return 
		type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg), 
5991 5992
				  targs, arg_type, parm, NULL_TREE,
				  DEDUCE_EXACT, allow_incomplete); 
mrs's avatar
mrs committed
5993
	    }
mrs's avatar
mrs committed
5994 5995 5996
	  arg = TREE_TYPE (arg);
	}
#endif
5997 5998 5999 6000 6001 6002
      if (! flag_ansi && arg == TREE_TYPE (null_node))
	{
	  warning ("using type void* for NULL");
	  arg = ptr_type_node;
	}

6003 6004
      if (!subr)
	maybe_adjust_types_for_deduction (strict, &parm, &arg);
mrs's avatar
mrs committed
6005

6006
      switch (unify (tparms, targs, parm, arg, sub_strict,
6007
		     explicit_mask)) 
mrs's avatar
mrs committed
6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025
	{
	case 0:
	  break;
	case 1:
	  return 1;
	}
    }
  /* Fail if we've reached the end of the parm list, and more args
     are present, and the parm list isn't variadic.  */
  if (args && args != void_list_node && parms == void_list_node)
    return 1;
  /* Fail if parms are left and they don't have default values.	 */
  if (parms
      && parms != void_list_node
      && TREE_PURPOSE (parms) == NULL_TREE)
    return 1;
  if (!subr)
    for (i = 0; i < ntparms; i++)
6026
      if (TREE_VEC_ELT (targs, i) == NULL_TREE)
mrs's avatar
mrs committed
6027
	{
6028 6029
	  if (!allow_incomplete)
	    error ("incomplete type unification");
mrs's avatar
mrs committed
6030 6031 6032 6033 6034
	  return 2;
	}
  return 0;
}

6035 6036
/* Returns the level of DECL, which declares a template parameter.  */

mmitchel's avatar
mmitchel committed
6037
int
6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051
template_decl_level (decl)
     tree decl;
{
  switch (TREE_CODE (decl))
    {
    case TYPE_DECL:
    case TEMPLATE_DECL:
      return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));

    case PARM_DECL:
      return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));

    default:
      my_friendly_abort (0);
6052
      return 0;
6053 6054 6055
    }
}

6056 6057 6058
/* Decide whether ARG can be unified with PARM, considering only the
   cv-qualifiers of each type, given STRICT as documented for unify.
   Returns non-zero iff the unification is OK on that basis.*/
mrs's avatar
mrs committed
6059

6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090
int
check_cv_quals_for_unify (strict, arg, parm)
     int strict;
     tree arg;
     tree parm;
{
  return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL)
	    && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
		|| TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
	   || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL)
	       && (TYPE_READONLY (arg) > TYPE_READONLY (parm)
		   || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))));
}

/* Takes parameters as for type_unification.  Returns 0 if the
   type deduction suceeds, 1 otherwise.  The parameter STRICT is a
   bitwise or of the following flags:

     UNIFY_ALLOW_NONE:
       Require an exact match between PARM and ARG.
     UNIFY_ALLOW_MORE_CV_QUAL:
       Allow the deduced ARG to be more cv-qualified than ARG.
     UNIFY_ALLOW_LESS_CV_QUAL:
       Allow the deduced ARG to be less cv-qualified than ARG.
     UNIFY_ALLOW_DERIVED:
       Allow the deduced ARG to be a template base class of ARG,
       or a pointer to a template base class of the type pointed to by
       ARG.  */

int
unify (tparms, targs, parm, arg, strict, explicit_mask)
6091
     tree tparms, targs, parm, arg;
6092
     int strict;
6093
     int* explicit_mask;
mrs's avatar
mrs committed
6094 6095
{
  int idx;
6096
  tree targ;
6097
  tree tparm;
mrs's avatar
mrs committed
6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109

  /* I don't think this will do the right thing with respect to types.
     But the only case I've seen it in so far has been array bounds, where
     signedness is the only information lost, and I think that will be
     okay.  */
  while (TREE_CODE (parm) == NOP_EXPR)
    parm = TREE_OPERAND (parm, 0);

  if (arg == error_mark_node)
    return 1;
  if (arg == unknown_type_node)
    return 1;
6110 6111 6112 6113 6114
  /* If PARM uses template parameters, then we can't bail out here,
     even in ARG == PARM, since we won't record unifications for the
     template parameters.  We might need them if we're trying to
     figure out which of two things is more specialized.  */
  if (arg == parm && !uses_template_parms (parm))
mrs's avatar
mrs committed
6115 6116
    return 0;

6117 6118 6119
  /* Immediately reject some pairs that won't unify because of
     cv-qualification mismatches.  */
  if (TREE_CODE (arg) == TREE_CODE (parm)
6120
      && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
6121 6122 6123 6124 6125 6126
      /* We check the cv-qualifiers when unifying with template type
	 parameters below.  We want to allow ARG `const T' to unify with
	 PARM `T' for example, when computing which of two templates
	 is more specialized, for example.  */
      && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
      && !check_cv_quals_for_unify (strict, arg, parm))
6127 6128
    return 1;

mrs's avatar
mrs committed
6129 6130
  switch (TREE_CODE (parm))
    {
6131 6132 6133 6134 6135 6136
    case TYPENAME_TYPE:
      /* In a type which contains a nested-name-specifier, template
	 argument values cannot be deduced for template parameters used
	 within the nested-name-specifier.  */
      return 0;

mrs's avatar
mrs committed
6137
    case TEMPLATE_TYPE_PARM:
6138
    case TEMPLATE_TEMPLATE_PARM:
6139 6140 6141 6142 6143 6144 6145
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));

      if (TEMPLATE_TYPE_LEVEL (parm)
	  != template_decl_level (tparm))
	/* The PARM is not one we're trying to unify.  Just check
	   to see if it matches ARG.  */
	return (TREE_CODE (arg) == TREE_CODE (parm)
6146
		&& comptypes (parm, arg, 1)) ? 0 : 1;
6147
      idx = TEMPLATE_TYPE_IDX (parm);
6148
      targ = TREE_VEC_ELT (targs, idx);
6149
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
6150

6151
      /* Check for mixed types and values.  */
6152 6153 6154 6155
      if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
	   && TREE_CODE (tparm) != TYPE_DECL)
	  || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM 
	      && TREE_CODE (tparm) != TEMPLATE_DECL))
6156 6157
	return 1;

6158 6159
      if (!strict && targ != NULL_TREE 
	  && explicit_mask && explicit_mask[idx])
6160 6161 6162 6163 6164
	/* An explicit template argument.  Don't even try to match
	   here; the overload resolution code will manage check to
	   see whether the call is legal.  */ 
	return 0;

6165
      if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
6166
	{
6167 6168 6169 6170
	  if (CLASSTYPE_TEMPLATE_INFO (parm))
	    {
	      /* We arrive here when PARM does not involve template 
		 specialization.  */
6171

6172 6173 6174
	      /* ARG must be constructed from a template class.  */
	      if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
		return 1;
6175

6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188
	      {
		tree parmtmpl = CLASSTYPE_TI_TEMPLATE (parm);
		tree parmvec = CLASSTYPE_TI_ARGS (parm);
		tree argvec = CLASSTYPE_TI_ARGS (arg);
		tree argtmplvec
		  = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
		int i;

		/* The parameter and argument roles have to be switched here 
		   in order to handle default arguments properly.  For example, 
		   template<template <class> class TT> void f(TT<int>) 
		   should be able to accept vector<int> which comes from 
		   template <class T, class Allcator = allocator> 
6189 6190
		   class vector.  */

6191 6192 6193
		if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1, 0)
		    == error_mark_node)
		  return 1;
6194
	  
6195 6196 6197 6198 6199 6200 6201 6202
		/* Deduce arguments T, i from TT<T> or TT<i>.  */
		for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
		  {
		    tree t = TREE_VEC_ELT (parmvec, i);
		    if (TREE_CODE (t) != TEMPLATE_TYPE_PARM
			&& TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM
			&& TREE_CODE (t) != TEMPLATE_PARM_INDEX)
		      continue;
6203

6204
		    /* This argument can be deduced.  */
6205

6206 6207 6208
		    if (unify (tparms, targs, t, 
			       TREE_VEC_ELT (argvec, i), 
			       UNIFY_ALLOW_NONE, explicit_mask))
6209 6210
		      return 1;
		  }
6211
	      }
6212 6213 6214 6215 6216
	      arg = CLASSTYPE_TI_TEMPLATE (arg);
	    }
	}
      else
	{
6217 6218 6219 6220 6221 6222
	  /* If PARM is `const T' and ARG is only `int', we don't have
	     a match unless we are allowing additional qualification.
	     If ARG is `const int' and PARM is just `T' that's OK;
	     that binds `const int' to `T'.  */
	  if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, 
					 arg, parm))
6223 6224
	    return 1;

6225 6226 6227 6228 6229 6230
	  /* Consider the case where ARG is `const volatile int' and
	     PARM is `const T'.  Then, T should be `volatile int'.  */
	  arg = 
	    cp_build_type_variant (arg, 
				   TYPE_READONLY (arg) > TYPE_READONLY (parm),
				   TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm));
6231 6232 6233
	}

      /* Simple cases: Value already set, does match or doesn't.  */
6234 6235 6236
      if (targ != NULL_TREE 
	  && (comptypes (targ, arg, 1)
	      || (explicit_mask && explicit_mask[idx])))
6237
	return 0;
6238
      else if (targ)
6239
	return 1;
6240
      TREE_VEC_ELT (targs, idx) = arg;
6241 6242
      return 0;

jason's avatar
jason committed
6243
    case TEMPLATE_PARM_INDEX:
6244 6245 6246 6247 6248 6249 6250
      tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));

      if (TEMPLATE_PARM_LEVEL (parm) 
	  != template_decl_level (tparm))
	/* The PARM is not one we're trying to unify.  Just check
	   to see if it matches ARG.  */
	return (TREE_CODE (arg) == TREE_CODE (parm)
6251
		&& cp_tree_equal (parm, arg) > 0) ? 0 : 1;
6252

jason's avatar
jason committed
6253
      idx = TEMPLATE_PARM_IDX (parm);
6254
      targ = TREE_VEC_ELT (targs, idx);
6255

6256
      if (targ)
mrs's avatar
mrs committed
6257
	{
6258
	  int i = (cp_tree_equal (targ, arg) > 0);
mrs's avatar
mrs committed
6259 6260 6261 6262 6263 6264
	  if (i == 1)
	    return 0;
	  else if (i == 0)
	    return 1;
	  else
	    my_friendly_abort (42);
mrs's avatar
mrs committed
6265 6266
	}

6267
      TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg);
mrs's avatar
mrs committed
6268 6269 6270
      return 0;

    case POINTER_TYPE:
6271 6272
      {
	int sub_strict;
mrs's avatar
mrs committed
6273

6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306
	if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
	  return (unify (tparms, targs, parm, 
			 TYPE_PTRMEMFUNC_FN_TYPE (arg), strict,
			 explicit_mask)); 
	
	if (TREE_CODE (arg) != POINTER_TYPE)
	  return 1;
	
	/* [temp.deduct.call]

	   A can be another pointer or pointer to member type that can
	   be converted to the deduced A via a qualification
	   conversion (_conv.qual_).

	   We pass down STRICT here rather than UNIFY_ALLOW_NONE.
	   This will allow for additional cv-qualification of the
	   pointed-to types if appropriate.  In general, this is a bit
	   too generous; we are only supposed to allow qualification
	   conversions and this method will allow an ARG of char** and
	   a deduced ARG of const char**.  However, overload
	   resolution will subsequently invalidate the candidate, so
	   this is probably OK.  */
	sub_strict = strict;
	
	if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE
	    || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg)))
	  /* The derived-to-base conversion only persists through one
	     level of pointers.  */
	  sub_strict &= ~UNIFY_ALLOW_DERIVED;

	return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE
		      (arg), sub_strict,  explicit_mask);
      }
mrs's avatar
mrs committed
6307 6308

    case REFERENCE_TYPE:
6309 6310 6311 6312
      if (TREE_CODE (arg) != REFERENCE_TYPE)
	return 1;
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
		    UNIFY_ALLOW_NONE, explicit_mask);
mrs's avatar
mrs committed
6313 6314 6315 6316

    case ARRAY_TYPE:
      if (TREE_CODE (arg) != ARRAY_TYPE)
	return 1;
6317 6318 6319 6320
      if ((TYPE_DOMAIN (parm) == NULL_TREE)
	  != (TYPE_DOMAIN (arg) == NULL_TREE))
	return 1;
      if (TYPE_DOMAIN (parm) != NULL_TREE
6321 6322
	  && unify (tparms, targs, TYPE_DOMAIN (parm),
		    TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE, explicit_mask) != 0)
mrs's avatar
mrs committed
6323
	return 1;
6324 6325
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
		    UNIFY_ALLOW_NONE, explicit_mask);
mrs's avatar
mrs committed
6326 6327

    case REAL_TYPE:
mrs's avatar
mrs committed
6328
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
6329
    case INTEGER_TYPE:
brendan's avatar
brendan committed
6330
    case BOOLEAN_TYPE:
6331
    case VOID_TYPE:
mrs's avatar
mrs committed
6332 6333 6334 6335
      if (TREE_CODE (arg) != TREE_CODE (parm))
	return 1;

      if (TREE_CODE (parm) == INTEGER_TYPE)
mrs's avatar
mrs committed
6336 6337
	{
	  if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
6338 6339
	      && unify (tparms, targs, TYPE_MIN_VALUE (parm),
			TYPE_MIN_VALUE (arg), UNIFY_ALLOW_NONE, explicit_mask))
mrs's avatar
mrs committed
6340 6341
	    return 1;
	  if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
6342 6343
	      && unify (tparms, targs, TYPE_MAX_VALUE (parm),
			TYPE_MAX_VALUE (arg), UNIFY_ALLOW_NONE, explicit_mask))
mrs's avatar
mrs committed
6344 6345
	    return 1;
	}
6346
      else if (TREE_CODE (parm) == REAL_TYPE
6347 6348 6349 6350 6351
	       /* We use the TYPE_MAIN_VARIANT since we have already
		  checked cv-qualification at the top of the
		  function.  */
	       && !comptypes (TYPE_MAIN_VARIANT (arg),
			      TYPE_MAIN_VARIANT (parm), 1))
6352 6353
	return 1;

mrs's avatar
mrs committed
6354 6355 6356 6357 6358
      /* As far as unification is concerned, this wins.	 Later checks
	 will invalidate it if necessary.  */
      return 0;

      /* Types INTEGER_CST and MINUS_EXPR can come from array bounds.  */
mrs's avatar
mrs committed
6359
      /* Type INTEGER_CST can come from ordinary constant template args.  */
mrs's avatar
mrs committed
6360
    case INTEGER_CST:
mrs's avatar
mrs committed
6361 6362 6363
      while (TREE_CODE (arg) == NOP_EXPR)
	arg = TREE_OPERAND (arg, 0);

mrs's avatar
mrs committed
6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375
      if (TREE_CODE (arg) != INTEGER_CST)
	return 1;
      return !tree_int_cst_equal (parm, arg);

    case TREE_VEC:
      {
	int i;
	if (TREE_CODE (arg) != TREE_VEC)
	  return 1;
	if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
	  return 1;
	for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
6376
	  if (unify (tparms, targs,
mrs's avatar
mrs committed
6377
		     TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
6378
		     UNIFY_ALLOW_NONE, explicit_mask))
mrs's avatar
mrs committed
6379 6380 6381 6382 6383
	    return 1;
	return 0;
      }

    case RECORD_TYPE:
mrs's avatar
mrs committed
6384
      if (TYPE_PTRMEMFUNC_FLAG (parm))
6385
	return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm),
6386
		      arg, strict, explicit_mask);
mrs's avatar
mrs committed
6387

6388
      if (TREE_CODE (arg) != RECORD_TYPE)
mrs's avatar
mrs committed
6389
	return 1;
6390
  
mrs's avatar
mrs committed
6391
      if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
mrs's avatar
mrs committed
6392
	{
mrs's avatar
mrs committed
6393
	  tree t = NULL_TREE;
6394 6395 6396 6397 6398 6399 6400 6401
	  if (strict & UNIFY_ALLOW_DERIVED)
	    /* [temp.deduct.call]

	       If P is a class, and P has the form template-id, then A
	       can be a derived class of the deduced A.  Likewise, if
	       P is a pointer to a class of the form template-id, A
	       can be a pointer to a derived class pointed to by the
	       deduced A.  */
mrs's avatar
mrs committed
6402
	    t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
mrs's avatar
mrs committed
6403 6404 6405
	  else if
	    (CLASSTYPE_TEMPLATE_INFO (arg)
	     && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
mrs's avatar
mrs committed
6406 6407
	    t = arg;
	  if (! t || t == error_mark_node)
mrs's avatar
mrs committed
6408
	    return 1;
mrs's avatar
mrs committed
6409

6410 6411 6412
	  return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
			CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE,
			explicit_mask);
mrs's avatar
mrs committed
6413
	}
6414 6415
      else if (!comptypes (TYPE_MAIN_VARIANT (parm),
			   TYPE_MAIN_VARIANT (arg), 1))
mrs's avatar
mrs committed
6416
	return 1;
mrs's avatar
mrs committed
6417
      return 0;
mrs's avatar
mrs committed
6418 6419 6420

    case METHOD_TYPE:
    case FUNCTION_TYPE:
6421
      if (TREE_CODE (arg) != TREE_CODE (parm))
mrs's avatar
mrs committed
6422
	return 1;
6423 6424 6425

      if (unify (tparms, targs, TREE_TYPE (parm),
		 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
mrs's avatar
mrs committed
6426
	return 1;
6427
      return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
6428
				    TYPE_ARG_TYPES (arg), 1, 
6429
				    DEDUCE_EXACT, 0, explicit_mask);
mrs's avatar
mrs committed
6430 6431 6432 6433

    case OFFSET_TYPE:
      if (TREE_CODE (arg) != OFFSET_TYPE)
	return 1;
6434 6435
      if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
		 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
mrs's avatar
mrs committed
6436
	return 1;
6437 6438
      return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
		    UNIFY_ALLOW_NONE, explicit_mask);
mrs's avatar
mrs committed
6439

jason's avatar
merge  
jason committed
6440
    case CONST_DECL:
6441
      if (arg != decl_constant_value (parm)) 
jason's avatar
merge  
jason committed
6442 6443 6444
	return 1;
      return 0;

6445 6446 6447 6448
    case TEMPLATE_DECL:
      /* Matched cases are handled by the ARG == PARM test above.  */
      return 1;

mrs's avatar
mrs committed
6449
    default:
6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488
      if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
	{
	  /* We're looking at an expression.  This can happen with
	     something like:

	       template <int I>
	       void foo(S<I>, S<I + 2>);

             If the call looked like:

               foo(S<2>(), S<4>());

	     we would have already matched `I' with `2'.  Now, we'd
	     like to know if `4' matches `I + 2'.  So, we substitute
	     into that expression, and fold constants, in the hope of
	     figuring it out.  */
	  tree t = 
	    maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE)); 
	  enum tree_code tc = TREE_CODE (t);

	  if (tc == MINUS_EXPR 
	      && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX
	      && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
	    {
	      /* We handle this case specially, since it comes up with
		 arrays.  In particular, something like:

		 template <int N> void f(int (&x)[N]);

		 Here, we are trying to unify the range type, which
		 looks like [0 ... (N - 1)].  */
	      tree t1, t2;
	      t1 = TREE_OPERAND (parm, 0);
	      t2 = TREE_OPERAND (parm, 1);

	      t = maybe_fold_nontype_arg (build (PLUS_EXPR,
						 integer_type_node,
						 arg, t2));

6489 6490
	      return unify (tparms, targs, t1, t, UNIFY_ALLOW_NONE,
			    explicit_mask);
6491 6492 6493 6494
	    }

	  if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (tc)))
	    /* Good, we mangaged to simplify the exression.  */
6495
	    return unify (tparms, targs, t, arg, UNIFY_ALLOW_NONE,
6496 6497 6498 6499 6500 6501 6502
			  explicit_mask);
	  else
	    /* Bad, we couldn't simplify this.  Assume it doesn't
	       unify.  */
	    return 1;
	}
      else
6503 6504
	sorry ("use of `%s' in template type unification",
	       tree_code_name [(int) TREE_CODE (parm)]);
6505

mrs's avatar
mrs committed
6506 6507 6508 6509
      return 1;
    }
}

mrs's avatar
mrs committed
6510
void
mrs's avatar
mrs committed
6511
mark_decl_instantiated (result, extern_p)
mrs's avatar
mrs committed
6512 6513 6514 6515 6516
     tree result;
     int extern_p;
{
  if (DECL_TEMPLATE_INSTANTIATION (result))
    SET_DECL_EXPLICIT_INSTANTIATION (result);
6517 6518 6519 6520 6521

  if (TREE_CODE (result) != FUNCTION_DECL)
    /* The TREE_PUBLIC flag for function declarations will have been
       set correctly by tsubst.  */
    TREE_PUBLIC (result) = 1;
mrs's avatar
mrs committed
6522 6523 6524 6525 6526

  if (! extern_p)
    {
      DECL_INTERFACE_KNOWN (result) = 1;
      DECL_NOT_REALLY_EXTERN (result) = 1;
6527 6528 6529

      /* For WIN32 we also want to put explicit instantiations in
	 linkonce sections.  */
6530 6531
      if (TREE_PUBLIC (result))
	maybe_make_one_only (result);
mrs's avatar
mrs committed
6532
    }
mrs's avatar
mrs committed
6533 6534
  else if (TREE_CODE (result) == FUNCTION_DECL)
    mark_inline_for_output (result);
mrs's avatar
mrs committed
6535 6536
}

6537 6538
/* Given two function templates PAT1 and PAT2, and explicit template
   arguments EXPLICIT_ARGS return:
mrs's avatar
mrs committed
6539 6540 6541 6542 6543 6544

   1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
   -1 if PAT2 is more specialized than PAT1.
   0 if neither is more specialized.  */
   
int
6545 6546
more_specialized (pat1, pat2, explicit_args)
     tree pat1, pat2, explicit_args;
mrs's avatar
mrs committed
6547
{
6548
  tree targs;
mrs's avatar
mrs committed
6549
  int winner = 0;
mrs's avatar
mrs committed
6550

6551
  targs = get_bindings_overload (pat1, pat2, explicit_args);
mrs's avatar
mrs committed
6552 6553 6554 6555
  if (targs)
    {
      --winner;
    }
mrs's avatar
mrs committed
6556

6557
  targs = get_bindings_overload (pat2, pat1, explicit_args);
mrs's avatar
mrs committed
6558 6559 6560 6561
  if (targs)
    {
      ++winner;
    }
mrs's avatar
mrs committed
6562

mrs's avatar
mrs committed
6563 6564
  return winner;
}
mrs's avatar
mrs committed
6565

mrs's avatar
mrs committed
6566
/* Given two class template specialization list nodes PAT1 and PAT2, return:
mrs's avatar
mrs committed
6567

mrs's avatar
mrs committed
6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579
   1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
   -1 if PAT2 is more specialized than PAT1.
   0 if neither is more specialized.  */
   
int
more_specialized_class (pat1, pat2)
     tree pat1, pat2;
{
  tree targs;
  int winner = 0;

  targs = get_class_bindings
6580 6581
    (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
     TREE_PURPOSE (pat2), NULL_TREE);
mrs's avatar
mrs committed
6582 6583 6584 6585
  if (targs)
    --winner;

  targs = get_class_bindings
6586 6587
    (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
     TREE_PURPOSE (pat1), NULL_TREE);
mrs's avatar
mrs committed
6588
  if (targs)
mrs's avatar
mrs committed
6589 6590 6591 6592
    ++winner;

  return winner;
}
mrs's avatar
mrs committed
6593 6594

/* Return the template arguments that will produce the function signature
6595
   DECL from the function template FN, with the explicit template
6596 6597
   arguments EXPLICIT_ARGS.  If CHECK_RETTYPE is 1, the return type must
   also match.  */
mrs's avatar
mrs committed
6598

6599 6600
static tree
get_bindings_real (fn, decl, explicit_args, check_rettype)
6601
     tree fn, decl, explicit_args;
6602
     int check_rettype;
mrs's avatar
mrs committed
6603
{
6604
  int ntparms = DECL_NTPARMS (fn);
6605
  tree targs = make_scratch_vec (ntparms);
6606 6607
  tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
  tree extra_fn_arg = NULL_TREE;
6608 6609
  int i;

6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629
  if (DECL_STATIC_FUNCTION_P (fn) 
      && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
    {
      /* Sometimes we are trying to figure out what's being
	 specialized by a declaration that looks like a method, and it
	 turns out to be a static member function.  */
      if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn))
	  && !is_member_template (fn))
	/* The natural thing to do here seems to be to remove the
	   spurious `this' parameter from the DECL, but that prevents
	   unification from making use of the class type.  So,
	   instead, we have fn_type_unification add to the parameters
	   for FN.  */
	extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn));
      else
	/* In this case, though, adding the extra_fn_arg can confuse
	   things, so we remove from decl_arg_types instead.  */
	decl_arg_types = TREE_CHAIN (decl_arg_types);
    }

6630
  i = fn_type_unification (fn, explicit_args, targs, 
6631
			   decl_arg_types,
6632
			   TREE_TYPE (TREE_TYPE (decl)),
6633
			   DEDUCE_EXACT,
6634
			   extra_fn_arg);
6635

6636 6637 6638 6639
  if (i != 0)
    return NULL_TREE;

  if (check_rettype)
6640 6641
    {
      /* Check to see that the resulting return type is also OK.  */
6642 6643 6644
      tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)),
		       complete_template_args (fn, targs, 1),
		       NULL_TREE);
6645

jason's avatar
jason committed
6646
      if (!comptypes (t, TREE_TYPE (TREE_TYPE (decl)), 1))
6647 6648 6649
	return NULL_TREE;
    }

6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668
  return targs;
}

/* For most uses, we want to check the return type.  */

tree 
get_bindings (fn, decl, explicit_args)
     tree fn, decl, explicit_args;
{
  return get_bindings_real (fn, decl, explicit_args, 1);
}

/* But for more_specialized, we only care about the parameter types.  */

static tree
get_bindings_overload (fn, decl, explicit_args)
     tree fn, decl, explicit_args;
{
  return get_bindings_real (fn, decl, explicit_args, 0);
mrs's avatar
mrs committed
6669 6670
}

mrs's avatar
mrs committed
6671
static tree
6672 6673
get_class_bindings (tparms, parms, args, outer_args)
     tree tparms, parms, args, outer_args;
mrs's avatar
mrs committed
6674
{
6675
  int i, ntparms = TREE_VEC_LENGTH (tparms);
mrs's avatar
mrs committed
6676 6677
  tree vec = make_temp_vec (ntparms);

6678 6679 6680 6681 6682 6683
  if (outer_args)
    {
      tparms = tsubst (tparms, outer_args, NULL_TREE);
      parms = tsubst (parms, outer_args, NULL_TREE);
    }

mrs's avatar
mrs committed
6684 6685
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
    {
6686
      switch (unify (tparms, vec, 
6687
		     TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
6688
		     UNIFY_ALLOW_NONE, 0))
mrs's avatar
mrs committed
6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704
	{
	case 0:
	  break;
	case 1:
	  return NULL_TREE;
	}
    }

  for (i =  0; i < ntparms; ++i)
    if (! TREE_VEC_ELT (vec, i))
      return NULL_TREE;

  return vec;
}

/* Return the most specialized of the list of templates in FNS that can
6705 6706
   produce an instantiation matching DECL, given the explicit template
   arguments EXPLICIT_ARGS.  */
mrs's avatar
mrs committed
6707 6708

tree
6709 6710
most_specialized (fns, decl, explicit_args)
     tree fns, decl, explicit_args;
mrs's avatar
mrs committed
6711
{
6712
  tree fn, champ, args, *p;
mrs's avatar
mrs committed
6713 6714 6715 6716
  int fate;

  for (p = &fns; *p; )
    {
6717
      args = get_bindings (TREE_VALUE (*p), decl, explicit_args);
mrs's avatar
mrs committed
6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733
      if (args)
	{
	  p = &TREE_CHAIN (*p);
	}
      else
	*p = TREE_CHAIN (*p);
    }

  if (! fns)
    return NULL_TREE;

  fn = fns;
  champ = TREE_VALUE (fn);
  fn = TREE_CHAIN (fn);
  for (; fn; fn = TREE_CHAIN (fn))
    {
6734
      fate = more_specialized (champ, TREE_VALUE (fn), explicit_args);
mrs's avatar
mrs committed
6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750
      if (fate == 1)
	;
      else
	{
	  if (fate == 0)
	    {
	      fn = TREE_CHAIN (fn);
	      if (! fn)
		return error_mark_node;
	    }
	  champ = TREE_VALUE (fn);
	}
    }

  for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
    {
6751
      fate = more_specialized (champ, TREE_VALUE (fn), explicit_args);
mrs's avatar
mrs committed
6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762
      if (fate != 1)
	return error_mark_node;
    }

  return champ;
}

/* Return the most specialized of the class template specializations in
   SPECS that can produce an instantiation matching ARGS.  */

tree
6763 6764
most_specialized_class (specs, mainargs, outer_args)
     tree specs, mainargs, outer_args;
mrs's avatar
mrs committed
6765 6766 6767 6768 6769 6770
{
  tree list = NULL_TREE, t, args, champ;
  int fate;

  for (t = specs; t; t = TREE_CHAIN (t))
    {
6771 6772
      args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
				 mainargs, outer_args);
mrs's avatar
mrs committed
6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804
      if (args)
	{
	  list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
	  TREE_TYPE (list) = TREE_TYPE (t);
	}
    }

  if (! list)
    return NULL_TREE;

  t = list;
  champ = t;
  t = TREE_CHAIN (t);
  for (; t; t = TREE_CHAIN (t))
    {
      fate = more_specialized_class (champ, t);
      if (fate == 1)
	;
      else
	{
	  if (fate == 0)
	    {
	      t = TREE_CHAIN (t);
	      if (! t)
		return error_mark_node;
	    }
	  champ = t;
	}
    }

  for (t = list; t && t != champ; t = TREE_CHAIN (t))
    {
6805
      fate = more_specialized_class (champ, t);
mrs's avatar
mrs committed
6806 6807 6808 6809 6810 6811 6812
      if (fate != 1)
	return error_mark_node;
    }

  return champ;
}

mrs's avatar
mrs committed
6813
/* called from the parser.  */
mrs's avatar
mrs committed
6814

mrs's avatar
mrs committed
6815
void
mrs's avatar
mrs committed
6816
do_decl_instantiation (declspecs, declarator, storage)
mrs's avatar
mrs committed
6817
     tree declspecs, declarator, storage;
mrs's avatar
mrs committed
6818
{
mrs's avatar
merging  
mrs committed
6819
  tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
mrs's avatar
mrs committed
6820
  tree result = NULL_TREE;
mrs's avatar
mrs committed
6821
  int extern_p = 0;
mrs's avatar
mrs committed
6822

mrs's avatar
mrs committed
6823 6824 6825 6826 6827 6828
  if (! DECL_LANG_SPECIFIC (decl))
    {
      cp_error ("explicit instantiation of non-template `%#D'", decl);
      return;
    }

mrs's avatar
mrs committed
6829
  /* If we've already seen this template instance, use it.  */
mrs's avatar
mrs committed
6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840
  if (TREE_CODE (decl) == VAR_DECL)
    {
      result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
      if (result && TREE_CODE (result) != VAR_DECL)
	result = NULL_TREE;
    }
  else if (TREE_CODE (decl) != FUNCTION_DECL)
    {
      cp_error ("explicit instantiation of `%#D'", decl);
      return;
    }
6841 6842
  else if (DECL_TEMPLATE_INSTANTIATION (decl))
    result = decl;
6843

mrs's avatar
mrs committed
6844
  if (! result)
mrs's avatar
mrs committed
6845 6846 6847 6848
    {
      cp_error ("no matching template for `%D' found", decl);
      return;
    }
mrs's avatar
mrs committed
6849

mrs's avatar
mrs committed
6850 6851 6852 6853 6854 6855
  if (! DECL_TEMPLATE_INFO (result))
    {
      cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
      return;
    }

mrs's avatar
mrs committed
6856 6857 6858
  if (flag_external_templates)
    return;

mrs's avatar
mrs committed
6859
  if (storage == NULL_TREE)
mrs's avatar
mrs committed
6860
    ;
mrs's avatar
mrs committed
6861 6862
  else if (storage == ridpointers[(int) RID_EXTERN])
    extern_p = 1;
mrs's avatar
mrs committed
6863 6864 6865
  else
    cp_error ("storage class `%D' applied to template instantiation",
	      storage);
mrs's avatar
mrs committed
6866 6867

  mark_decl_instantiated (result, extern_p);
mrs's avatar
mrs committed
6868
  repo_template_instantiated (result, extern_p);
mrs's avatar
mrs committed
6869 6870
  if (! extern_p)
    instantiate_decl (result);
mrs's avatar
mrs committed
6871 6872
}

mrs's avatar
mrs committed
6873 6874 6875 6876 6877 6878
void
mark_class_instantiated (t, extern_p)
     tree t;
     int extern_p;
{
  SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
6879 6880
  SET_CLASSTYPE_INTERFACE_KNOWN (t);
  CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
mrs's avatar
mrs committed
6881 6882 6883 6884 6885 6886 6887 6888
  CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
  TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
  if (! extern_p)
    {
      CLASSTYPE_DEBUG_REQUESTED (t) = 1;
      rest_of_type_compilation (t, 1);
    }
}     
mrs's avatar
mrs committed
6889

mrs's avatar
mrs committed
6890
void
6891 6892
do_type_instantiation (t, storage)
     tree t, storage;
mrs's avatar
mrs committed
6893
{
mrs's avatar
mrs committed
6894 6895
  int extern_p = 0;
  int nomem_p = 0;
mrs's avatar
mrs committed
6896 6897
  int static_p = 0;

6898 6899 6900 6901 6902 6903 6904 6905 6906
  if (TREE_CODE (t) == TYPE_DECL)
    t = TREE_TYPE (t);

  if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
    {
      cp_error ("explicit instantiation of non-template type `%T'", t);
      return;
    }

mrs's avatar
mrs committed
6907
  complete_type (t);
mrs's avatar
mrs committed
6908

mrs's avatar
mrs committed
6909 6910
  /* With -fexternal-templates, explicit instantiations are treated the same
     as implicit ones.  */
mrs's avatar
mrs committed
6911 6912 6913
  if (flag_external_templates)
    return;

mrs's avatar
mrs committed
6914 6915 6916 6917 6918 6919 6920 6921
  if (TYPE_SIZE (t) == NULL_TREE)
    {
      cp_error ("explicit instantiation of `%#T' before definition of template",
		t);
      return;
    }

  if (storage == NULL_TREE)
mrs's avatar
mrs committed
6922 6923 6924
    /* OK */;
  else if (storage == ridpointers[(int) RID_INLINE])
    nomem_p = 1;
mrs's avatar
mrs committed
6925 6926
  else if (storage == ridpointers[(int) RID_EXTERN])
    extern_p = 1;
mrs's avatar
mrs committed
6927 6928
  else if (storage == ridpointers[(int) RID_STATIC])
    static_p = 1;
mrs's avatar
mrs committed
6929 6930 6931 6932 6933 6934 6935
  else
    {
      cp_error ("storage class `%D' applied to template instantiation",
		storage);
      extern_p = 0;
    }

mrs's avatar
mrs committed
6936
  /* We've already instantiated this.  */
mrs's avatar
mrs committed
6937 6938 6939
  if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
      && extern_p)
    return;
mrs's avatar
mrs committed
6940

mrs's avatar
mrs committed
6941
  if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
mrs's avatar
mrs committed
6942 6943 6944 6945
    {
      mark_class_instantiated (t, extern_p);
      repo_template_instantiated (t, extern_p);
    }
mrs's avatar
mrs committed
6946 6947 6948 6949

  if (nomem_p)
    return;

mrs's avatar
mrs committed
6950
  {
mrs's avatar
mrs committed
6951
    tree tmp;
mrs's avatar
mrs committed
6952 6953 6954

    if (! static_p)
      for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
6955
	if (TREE_CODE (tmp) == FUNCTION_DECL
6956
	    && DECL_TEMPLATE_INSTANTIATION (tmp))
mrs's avatar
mrs committed
6957 6958 6959 6960 6961 6962 6963 6964 6965
	  {
	    mark_decl_instantiated (tmp, extern_p);
	    repo_template_instantiated (tmp, extern_p);
	    if (! extern_p)
	      instantiate_decl (tmp);
	  }

    for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
      if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
mrs's avatar
mrs committed
6966
	{
mrs's avatar
mrs committed
6967
	  mark_decl_instantiated (tmp, extern_p);
mrs's avatar
mrs committed
6968
	  repo_template_instantiated (tmp, extern_p);
mrs's avatar
mrs committed
6969 6970
	  if (! extern_p)
	    instantiate_decl (tmp);
mrs's avatar
mrs committed
6971
	}
mrs's avatar
mrs committed
6972

mrs's avatar
mrs committed
6973
    for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
mrs's avatar
mrs committed
6974 6975
      if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
	do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
mrs's avatar
mrs committed
6976
  }
mrs's avatar
mrs committed
6977
}
mrs's avatar
mrs committed
6978

6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047
/* Given a function DECL, which is a specialization of TEMP, modify
   DECL to be a re-instantiation of TEMPL with the same template
   arguments.

   One reason for doing this is a scenario like this:

     template <class T>
     void f(const T&, int i);

     void g() { f(3, 7); }

     template <class T>
     void f(const T& t, const int i) { }

   Note that when the template is first instantiated, with
   instantiate_template, the resulting DECL will have no name for the
   first parameter, and the wrong type for the second.  So, when we go
   to instantiate the DECL, we regenerate it.  */

void
regenerate_decl_from_template (decl, tmpl)
     tree decl;
     tree tmpl;
{
  tree args;
  tree save_ti;
  tree code_pattern;
  tree new_decl;

  args = DECL_TI_ARGS (decl);
  code_pattern = DECL_TEMPLATE_RESULT (tmpl);

  /* Trick tsubst into giving us a new decl.  CODE_PATTERN must be the
     most distant ancestor of DECL, since that's the one that will
     actually be altered by a redefinition.  */
  save_ti = DECL_TEMPLATE_INFO (code_pattern);
  DECL_TEMPLATE_INFO (code_pattern) = NULL_TREE;
  new_decl = tsubst (code_pattern, args, NULL_TREE);
  SET_DECL_IMPLICIT_INSTANTIATION (new_decl);
  DECL_TEMPLATE_INFO (code_pattern) = save_ti;

  if (TREE_CODE (decl) == VAR_DECL)
    {
      /* Set up DECL_INITIAL, since tsubst doesn't.  */
      pushclass (DECL_CONTEXT (decl), 2);
      DECL_INITIAL (new_decl) = 
	tsubst_expr (DECL_INITIAL (code_pattern), args, 
		     DECL_TI_TEMPLATE (decl));
      popclass (1);
    }

  if (TREE_CODE (decl) == FUNCTION_DECL)
    {
      /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
	 new decl.  */ 
      DECL_INITIAL (new_decl) = error_mark_node;

      if (DECL_TEMPLATE_SPECIALIZATION (new_decl) 
	  && !DECL_TEMPLATE_INFO (new_decl))
	/* Set up the information about what is being specialized. */
	DECL_TEMPLATE_INFO (new_decl) = DECL_TEMPLATE_INFO (decl);
    }

  duplicate_decls (new_decl, decl);

  if (TREE_CODE (decl) == FUNCTION_DECL)
    DECL_INITIAL (new_decl) = NULL_TREE;
}

jason's avatar
jason committed
7048 7049
/* Produce the definition of D, a _DECL generated from a template.  */

mrs's avatar
mrs committed
7050
tree
mrs's avatar
mrs committed
7051 7052
instantiate_decl (d)
     tree d;
mrs's avatar
mrs committed
7053
{
mrs's avatar
mrs committed
7054 7055 7056
  tree ti = DECL_TEMPLATE_INFO (d);
  tree tmpl = TI_TEMPLATE (ti);
  tree args = TI_ARGS (ti);
7057
  tree td;
jason's avatar
jason committed
7058
  tree decl_pattern, code_pattern;
mrs's avatar
mrs committed
7059 7060 7061
  int nested = in_function_p ();
  int d_defined;
  int pattern_defined;
mrs's avatar
mrs committed
7062 7063
  int line = lineno;
  char *file = input_filename;
mrs's avatar
mrs committed
7064

jason's avatar
jason committed
7065 7066 7067 7068 7069 7070 7071 7072 7073
  for (td = tmpl; 
       DECL_TEMPLATE_INSTANTIATION (td) 
	 /* This next clause handles friend templates defined inside
	    class templates.  The friend templates are not really
	    instantiations from the point of view of the language, but
	    they are instantiations from the point of view of the
	    compiler.  */
	 || (DECL_TEMPLATE_INFO (td) && !DECL_TEMPLATE_SPECIALIZATION (td)); 
       )
jason's avatar
jason committed
7074
    td = DECL_TI_TEMPLATE (td);
7075

jason's avatar
jason committed
7076 7077 7078 7079 7080
  /* In the case of a member template, decl_pattern is the partially
     instantiated declaration (in the instantiated class), and code_pattern
     is the original template definition.  */
  decl_pattern = DECL_TEMPLATE_RESULT (tmpl);
  code_pattern = DECL_TEMPLATE_RESULT (td);
7081

mrs's avatar
mrs committed
7082 7083 7084
  if (TREE_CODE (d) == FUNCTION_DECL)
    {
      d_defined = (DECL_INITIAL (d) != NULL_TREE);
jason's avatar
jason committed
7085
      pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
mrs's avatar
mrs committed
7086 7087 7088 7089
    }
  else
    {
      d_defined = ! DECL_IN_AGGR_P (d);
jason's avatar
jason committed
7090
      pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
mrs's avatar
mrs committed
7091 7092 7093 7094
    }

  if (d_defined)
    return d;
mrs's avatar
mrs committed
7095

7096 7097
  if (TREE_CODE (d) == FUNCTION_DECL) 
    {
7098 7099 7100 7101 7102
      tree spec = retrieve_specialization (tmpl, args);
      
      if (spec != NULL_TREE 
	  && DECL_TEMPLATE_SPECIALIZATION (spec))
	return spec;
7103 7104
    }

mrs's avatar
mrs committed
7105 7106 7107 7108 7109 7110 7111 7112 7113
  /* This needs to happen before any tsubsting.  */
  if (! push_tinst_level (d))
    return d;

  push_to_top_level ();
  lineno = DECL_SOURCE_LINE (d);
  input_filename = DECL_SOURCE_FILE (d);

  if (pattern_defined)
mrs's avatar
mrs committed
7114 7115 7116 7117 7118 7119 7120 7121 7122 7123
    {
      repo_template_used (d);

      if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
	{
	  if (flag_alt_external_templates)
	    {
	      if (interface_unknown)
		warn_if_unknown_interface (d);
	    }
jason's avatar
jason committed
7124
	  else if (DECL_INTERFACE_KNOWN (code_pattern))
mrs's avatar
mrs committed
7125 7126
	    {
	      DECL_INTERFACE_KNOWN (d) = 1;
jason's avatar
jason committed
7127
	      DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
mrs's avatar
mrs committed
7128 7129
	    }
	  else
jason's avatar
jason committed
7130
	    warn_if_unknown_interface (code_pattern);
mrs's avatar
mrs committed
7131 7132
	}

mrs's avatar
mrs committed
7133
      if (at_eof)
mrs's avatar
mrs committed
7134 7135 7136
	import_export_decl (d);
    }

7137 7138 7139 7140 7141 7142
  /* Reject all external templates except inline functions.  */
  if (DECL_INTERFACE_KNOWN (d)
      && ! DECL_NOT_REALLY_EXTERN (d)
      && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
    goto out;

7143 7144 7145 7146 7147 7148 7149 7150
  if (TREE_CODE (d) == VAR_DECL 
      && DECL_INITIAL (d) == NULL_TREE
      && DECL_INITIAL (code_pattern) != NULL_TREE)
    /* We need to set up DECL_INITIAL regardless of pattern_defined if
	 the variable is a static const initialized in the class body.  */;
  else if (! pattern_defined
	   || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
	       && ! at_eof))
mrs's avatar
mrs committed
7151
    {
7152 7153
      /* Defer all templates except inline functions used in another
         function.  */
jason's avatar
jason committed
7154 7155 7156
      lineno = line;
      input_filename = file;

mrs's avatar
mrs committed
7157
      add_pending_template (d);
mrs's avatar
mrs committed
7158
      goto out;
mrs's avatar
mrs committed
7159 7160
    }

7161 7162 7163 7164
  regenerate_decl_from_template (d, td);

  /* We already set the file and line above.  Reset them now in case
     they changed as a result of calling regenerate_decl_from_template.  */
mrs's avatar
mrs committed
7165 7166 7167
  lineno = DECL_SOURCE_LINE (d);
  input_filename = DECL_SOURCE_FILE (d);

mrs's avatar
mrs committed
7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181
  if (TREE_CODE (d) == VAR_DECL)
    {
      DECL_IN_AGGR_P (d) = 0;
      if (DECL_INTERFACE_KNOWN (d))
	DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
      else
	{
	  DECL_EXTERNAL (d) = 1;
	  DECL_NOT_REALLY_EXTERN (d) = 1;
	}
      cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
    }
  else if (TREE_CODE (d) == FUNCTION_DECL)
    {
jason's avatar
jason committed
7182
      tree t = DECL_SAVED_TREE (code_pattern);
mrs's avatar
mrs committed
7183

mrs's avatar
merging  
mrs committed
7184
      start_function (NULL_TREE, d, NULL_TREE, 1);
mrs's avatar
mrs committed
7185 7186
      store_parm_decls ();

mrs's avatar
mrs committed
7187 7188 7189 7190
      if (t && TREE_CODE (t) == RETURN_INIT)
	{
	  store_return_init
	    (TREE_OPERAND (t, 0),
7191
	     tsubst_expr (TREE_OPERAND (t, 1), args, tmpl));
mrs's avatar
mrs committed
7192 7193 7194
	  t = TREE_CHAIN (t);
	}

mrs's avatar
mrs committed
7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205
      if (t && TREE_CODE (t) == CTOR_INITIALIZER)
	{
	  current_member_init_list
	    = tsubst_expr_values (TREE_OPERAND (t, 0), args);
	  current_base_init_list
	    = tsubst_expr_values (TREE_OPERAND (t, 1), args);
	  t = TREE_CHAIN (t);
	}

      setup_vtbl_ptr ();
      /* Always keep the BLOCK node associated with the outermost
law's avatar
law committed
7206
	 pair of curly braces of a function.  These are needed
mrs's avatar
mrs committed
7207 7208 7209 7210
	 for correct operation of dwarfout.c.  */
      keep_next_level ();

      my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
7211
      tsubst_expr (t, args, tmpl);
mrs's avatar
mrs committed
7212

mrs's avatar
mrs committed
7213 7214 7215
      finish_function (lineno, 0, nested);
    }

mrs's avatar
mrs committed
7216
out:
mrs's avatar
mrs committed
7217 7218 7219
  lineno = line;
  input_filename = file;

mrs's avatar
mrs committed
7220 7221
  pop_from_top_level ();
  pop_tinst_level ();
mrs's avatar
mrs committed
7222 7223 7224

  return d;
}
mrs's avatar
mrs committed
7225 7226 7227 7228 7229 7230 7231

tree
tsubst_chain (t, argvec)
     tree t, argvec;
{
  if (t)
    {
7232
      tree first = tsubst (t, argvec, NULL_TREE);
mrs's avatar
mrs committed
7233 7234 7235 7236
      tree last = first;

      for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
	{
7237
	  tree x = tsubst (t, argvec, NULL_TREE);
mrs's avatar
mrs committed
7238 7239 7240 7241 7242 7243 7244 7245 7246
	  TREE_CHAIN (last) = x;
	  last = x;
	}

      return first;
    }
  return NULL_TREE;
}

mrs's avatar
mrs committed
7247
static tree
mrs's avatar
mrs committed
7248 7249 7250 7251 7252 7253 7254 7255
tsubst_expr_values (t, argvec)
     tree t, argvec;
{
  tree first = NULL_TREE;
  tree *p = &first;

  for (; t; t = TREE_CHAIN (t))
    {
7256 7257
      tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE);
      tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE);
mrs's avatar
mrs committed
7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271
      *p = build_tree_list (pur, val);
      p = &TREE_CHAIN (*p);
    }
  return first;
}

tree last_tree;

void
add_tree (t)
     tree t;
{
  last_tree = TREE_CHAIN (last_tree) = t;
}
mrs's avatar
mrs committed
7272

7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290

void
begin_tree ()
{
  saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
  last_tree = NULL_TREE;
}


void 
end_tree ()
{
  my_friendly_assert (saved_trees != NULL_TREE, 0);

  last_tree = TREE_VALUE (saved_trees);
  saved_trees = TREE_CHAIN (saved_trees);
}

mrs's avatar
mrs committed
7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304
/* D is an undefined function declaration in the presence of templates with
   the same name, listed in FNS.  If one of them can produce D as an
   instantiation, remember this so we can instantiate it at EOF if D has
   not been defined by that time.  */

void
add_maybe_template (d, fns)
     tree d, fns;
{
  tree t;

  if (DECL_MAYBE_TEMPLATE (d))
    return;

7305
  t = most_specialized (fns, d, NULL_TREE);
mrs's avatar
mrs committed
7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317
  if (! t)
    return;
  if (t == error_mark_node)
    {
      cp_error ("ambiguous template instantiation for `%D'", d);
      return;
    }

  *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
  maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
  DECL_MAYBE_TEMPLATE (d) = 1;
}
mrs's avatar
mrs committed
7318 7319 7320 7321 7322

/* Instantiate an enumerated type.  Used by instantiate_class_template and
   tsubst_expr.  */

static tree
7323
tsubst_enum (tag, args, field_chain)
7324
     tree tag, args;
7325
     tree * field_chain;
mrs's avatar
mrs committed
7326
{
7327 7328 7329
  extern tree current_local_enum;
  tree prev_local_enum = current_local_enum;

mrs's avatar
mrs committed
7330 7331 7332 7333 7334 7335 7336
  tree newtag = start_enum (TYPE_IDENTIFIER (tag));
  tree e, values = NULL_TREE;

  for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
    {
      tree elt = build_enumerator (TREE_PURPOSE (e),
				   tsubst_expr (TREE_VALUE (e), args,
7337
						NULL_TREE));
mrs's avatar
mrs committed
7338 7339 7340 7341 7342 7343
      TREE_CHAIN (elt) = values;
      values = elt;
    }

  finish_enum (newtag, values);

7344
  if (NULL != field_chain)
7345
    *field_chain = grok_enum_decls (NULL_TREE);
7346 7347 7348

  current_local_enum = prev_local_enum;

mrs's avatar
mrs committed
7349 7350
  return newtag;
}