pt.c 170 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 29 30 31 32 33 34 35 36

#include "config.h"
#include <stdio.h>
#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 41 42 43 44
#include "except.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
mrs's avatar
mrs committed
45 46 47 48 49 50 51

extern struct obstack permanent_obstack;

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

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

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

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

mrs's avatar
mrs committed
61
int minimal_parse_mode;
62

63
int processing_specialization;
64
int processing_explicit_instantiation;
65 66
static int template_header_count;

67 68
static tree saved_trees;

mrs's avatar
mrs committed
69 70 71
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free

72
static int unify PROTO((tree, tree *, int, tree, tree, int));
73 74 75
static void add_pending_template PROTO((tree));
static int push_tinst_level PROTO((tree));
static tree classtype_mangled_name PROTO((tree));
76
static char *mangle_class_name_for_template PROTO((char *, tree, tree, tree));
77
static tree tsubst_expr_values PROTO((tree, tree));
mrs's avatar
mrs committed
78
static int comp_template_args PROTO((tree, tree));
79
static int list_eq PROTO((tree, tree));
80
static tree get_class_bindings PROTO((tree, tree, tree, tree));
81
static tree coerce_template_parms PROTO((tree, tree, tree, int, int, int));
82
static tree tsubst_enum	PROTO((tree, tree, tree *));
83
static tree add_to_template_args PROTO((tree, tree));
84
static int  type_unification_real PROTO((tree, tree *, tree, tree,
85 86
					 int, int, int));
static void note_template_header PROTO((int));
87
static tree maybe_fold_nontype_arg PROTO((tree));
88 89 90 91 92 93 94 95 96 97 98 99 100
static tree convert_nontype_argument PROTO((tree, tree));

/* 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)
101
    end_template_decl ();
102
  else
103
    end_specialization ();
104

105 106 107 108
  if (decl == NULL_TREE || decl == void_type_node)
    return NULL_TREE;
  else if (TREE_CODE (decl) == TREE_LIST)
    {
jason's avatar
comment  
jason committed
109
      /* Assume that the class is the only declspec.  */
110
      decl = TREE_VALUE (decl);
111 112
      if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
	  && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
113 114 115 116 117
	{
	  tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
	  check_member_template (tmpl);
	  return tmpl;
	}
118
      return NULL_TREE;
119
    }
120
  else if (DECL_TEMPLATE_INFO (decl))
121
    {
122 123 124 125 126 127 128 129 130 131 132
      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);
	
133

134
  return error_mark_node;
jason's avatar
jason committed
135
}
136

jason's avatar
jason committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/* 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;
{
154
  int depth;
jason's avatar
jason committed
155

156 157 158 159 160 161
  for (depth = 0; type && TREE_CODE (type) != FUNCTION_DECL;
       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
162 163

  return depth;
164
}
165

166 167
/* Return the original template for this decl, disregarding any
   specializations.  */
168

169 170
static tree
original_template (decl)
171
     tree decl;
172
{
173 174 175 176
  while (DECL_TEMPLATE_INFO (decl))
    decl = DECL_TI_TEMPLATE (decl);
  return decl;
}
177

178 179 180 181 182 183 184 185 186
/* 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
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  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);
207

208
  ++processing_template_decl;
209
  current_template_parms
210 211
    = tree_cons (build_int_2 (0, processing_template_decl),
		 parms, current_template_parms);
212 213
  TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;

214
  pushlevel (0);
215 216
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) 
    {
217 218
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
219

220 221
      switch (TREE_CODE (parm))
	{
222
	case TYPE_DECL:
223
	case TEMPLATE_DECL:
224 225
	  pushdecl (parm);
	  break;
226 227 228 229 230 231 232 233 234

	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);
	  }
235
	  break;
236

237 238 239 240 241 242
	default:
	  my_friendly_abort (0);
	}
    }
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/* 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);
}

269 270 271
/* Undo the effects of begin_member_template_processing. */

void 
jason's avatar
jason committed
272 273
maybe_end_member_template_processing (decl)
     tree decl;
274 275 276 277
{
  if (! processing_template_decl)
    return;

278 279 280 281 282 283 284
  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);
    }
285 286
}

287
/* Returns non-zero iff T is a member template function.  We must be
jason's avatar
jason committed
288
   careful as in
289 290 291 292 293 294 295 296 297 298 299

     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.  */
300

301 302
int
is_member_template (t)
303 304
     tree t;
{
305 306
  if (TREE_CODE (t) != FUNCTION_DECL
      && !DECL_FUNCTION_TEMPLATE_P (t))
307
    /* Anything that isn't a function or a template function is
308 309 310
       certainly not a member template.  */
    return 0;

311 312 313 314 315
  /* A local class can't have member templates.  */
  if (hack_decl_function_context (t))
    return 0;

  if ((DECL_FUNCTION_MEMBER_P (t) 
316
       && !DECL_TEMPLATE_SPECIALIZATION (t))
jason's avatar
jason committed
317
      || (TREE_CODE (t) == TEMPLATE_DECL 
318
	  && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))))
319
    {
jason's avatar
jason committed
320
      tree tmpl;
321 322 323 324 325 326

      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
327 328 329
      else
	tmpl = NULL_TREE;

330
      if (tmpl
jason's avatar
jason committed
331 332 333
	  /* If there are more levels of template parameters than
	     there are template classes surrounding the declaration,
	     then we have a member template.  */
334 335
	  && (list_length (DECL_TEMPLATE_PARMS (tmpl)) > 
	      template_class_depth (DECL_CLASS_CONTEXT (t))))
jason's avatar
jason committed
336 337
	return 1;
    }
338

jason's avatar
jason committed
339 340
  return 0;
}
341 342 343 344

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

345
static tree
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
add_to_template_args (args, extra_args)
     tree args;
     tree extra_args;
{
  tree new_args;

  if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC)
    {
      new_args = make_tree_vec (2);
      TREE_VEC_ELT (new_args, 0) = args;
    }
  else 
    {
      int i;

361
      new_args = make_tree_vec (TREE_VEC_LENGTH (args) + 1);
362 363 364 365 366 367 368 369 370 371

      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
372 373 374

/* We've got a template header coming up; push to a new level for storing
   the parms.  */
mrs's avatar
mrs committed
375 376 377 378 379

void
begin_template_parm_list ()
{
  pushlevel (0);
mrs's avatar
mrs committed
380
  declare_pseudo_global_level ();
mrs's avatar
mrs committed
381
  ++processing_template_decl;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  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 <>.  */

419
static void 
420 421 422 423 424 425 426 427
note_template_header (specialization)
     int specialization;
{
  processing_specialization = specialization;
  template_header_count++;
}


428
/* We're beginning an explicit instantiation.  */
429

430 431
void
begin_explicit_instantiation ()
432
{
433 434
  ++processing_explicit_instantiation;
}
435 436


437 438 439 440 441 442
void
end_explicit_instantiation ()
{
  my_friendly_assert(processing_explicit_instantiation > 0, 0);
  --processing_explicit_instantiation;
}
443 444


445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
/* 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;
468 469 470
}


471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

/* 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));
}


540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
/* 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 = "               ";
    }
}

557
/* Returns the template (one of the functions given by TEMPLATE_ID)
558 559 560 561 562 563 564 565
   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.  */
566

567
tree
568
determine_specialization (template_id, decl, targs_out, 
569 570
			  need_member_template,
			  complain)
571
     tree template_id;
572
     tree decl;
573 574 575 576
     tree* targs_out;
     int need_member_template;
     int complain;
{
577 578
  tree fns = TREE_OPERAND (template_id, 0);
  tree targs_in = TREE_OPERAND (template_id, 1);
579
  tree templates = NULL_TREE;
580
  tree fn;
581 582
  int overloaded;
  int i;
583

584 585
  *targs_out = NULL_TREE;

586 587 588 589
  if (is_overloaded_fn (fns))
    fn = get_first_fn (fns);
  else
    fn = NULL_TREE;
590

591 592
  overloaded = really_overloaded_fn (fns);
  for (; fn != NULL_TREE; 
593 594
       fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE)
    {
595 596 597 598
      tree tmpl;

      if (!need_member_template 
	  && TREE_CODE (fn) == FUNCTION_DECL 
599
	  && DECL_FUNCTION_MEMBER_P (fn)
600 601
	  && DECL_USE_TEMPLATE (fn)
	  && DECL_TI_TEMPLATE (fn))
602 603 604 605 606
	/* 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.  */
607 608
	tmpl = DECL_TI_TEMPLATE (fn);
      else if (TREE_CODE (fn) != TEMPLATE_DECL
609
	       || (need_member_template && !is_member_template (fn)))
610
	continue;
611 612
      else
	tmpl = fn;
613

614
      if (list_length (targs_in) > DECL_NTPARMS (tmpl))
615 616
	continue;

617
      if (decl == NULL_TREE)
618
	{
619 620 621 622 623 624 625 626 627
	  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),
				&TREE_VEC_ELT (targs, 0),
				NULL_TREE,
				NULL_TREE,  
				targs_in,
628
				1, 1);
629 630 631 632
      
	  if (i == 0) 
	    /* Unification was successful.  */
	    templates = scratch_tree_cons (targs, tmpl, templates);
633
	}
634 635
      else
	templates = scratch_tree_cons (NULL_TREE, tmpl, templates);
636
    }
637
  
638
  if (decl != NULL_TREE)
639
    {
640 641 642 643 644 645 646 647 648 649 650 651 652 653
      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:
654
      if (complain)
655 656
	cp_error ("`%D' does not match any template declaration",
		  template_id);
657
      
658 659
      return NULL_TREE;
    }
660
  else if (TREE_CHAIN (templates) != NULL_TREE) 
661
    {
662
    ambiguous:
663 664
      if (complain)
	{
665 666
	  cp_error ("ambiguous template specialization `%D'",
		    template_id);
667
	  print_candidates (templates);
668 669 670 671 672
	}
      return NULL_TREE;
    }

  /* We have one, and exactly one, match. */
673 674
  *targs_out = TREE_PURPOSE (templates);
  return TREE_VALUE (templates);
mrs's avatar
mrs committed
675 676
}

677 678
	
/* Check to see if the function just declared, as indicated in
679 680 681 682
   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.

683 684
   Returns DECL, or an equivalent declaration that should be used
   instead. 
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
   
   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.  */
720

721
tree
722
check_explicit_specialization (declarator, decl, template_count, flags)
723 724 725 726 727
     tree declarator;
     tree decl;
     int template_count;
     int flags;
{
728 729 730 731
  int finish_member = flags & 1;
  int have_def = flags & 2;
  int is_friend = flags & 4;
  int specialization = 0;
732
  int explicit_instantiation = 0;
733 734 735 736
  int member_specialization = flags & 8;

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

738
  if (!finish_member)
739
    {
740 741 742 743 744 745 746 747 748 749 750 751 752 753
      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);
754

755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
	      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);
786
	      return decl;
787 788 789 790 791 792 793 794 795
	    } 
	}
      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");
796

797
	  explicit_instantiation = 1;
798
	}
799 800 801
      else if (ctype != NULL_TREE
	       && !TYPE_BEING_DEFINED (ctype)
	       && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
802
	{
803
	  /* This case catches outdated code that looks like this:
804

805
	     template <class T> struct S { void f(); };
806 807 808 809 810 811 812
	     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
813
	     the class is complete.  */
814

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
	  /* 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
833 834 835
	     template <> template <class T>
	     void f<int>();  */

836 837
	  cp_error ("template-id `%D' in declaration of primary template",
		    declarator);
838
	  return decl;
839
	}
840
    }
841

842 843 844 845 846 847 848 849 850 851 852 853
  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;
	  }
    }

854
  if (specialization || member_specialization || explicit_instantiation)
855 856 857 858 859
    {
      tree tmpl = NULL_TREE;
      tree targs = NULL_TREE;

      /* Make sure that the declarator is a TEMPLATE_ID_EXPR.  */
860 861 862 863 864 865 866
      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
867
	    fns = IDENTIFIER_NAMESPACE_VALUE (dname);
868 869 870
	  else
	    fns = dname;

871 872
	  declarator = 
	    lookup_template_function (fns, NULL_TREE);
873 874 875 876 877 878 879
	}

      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);
880
	  my_friendly_assert (!explicit_instantiation, 0);
881
	  SET_DECL_IMPLICIT_INSTANTIATION (decl);
882
	  return decl;
883 884
	} 

885 886 887 888 889 890
      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.  */
891 892 893 894 895 896 897 898 899 900
	  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;
901 902 903 904
	}
      else if (ctype != NULL_TREE 
	       && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
		   IDENTIFIER_NODE))
905
	{
906 907 908
	  /* Find the list of functions in ctype that have the same
	     name as the declared function.  */
	  tree name = TREE_OPERAND (declarator, 0);
909
	  tree fns;
910 911 912
	  
	  if (name == constructor_name (ctype) 
	      || name == constructor_name_full (ctype))
913
	    {
914 915 916 917 918 919
	      int is_constructor = DECL_CONSTRUCTOR_P (decl);
	      
	      if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
		  : !TYPE_HAS_DESTRUCTOR (ctype))
		{
		  /* From [temp.expl.spec]:
920
		       
921 922 923
		     If such an explicit specialization for the member
		     of a class template names an implicitly-declared
		     special member function (clause _special_), the
924 925 926
		     program is ill-formed.  

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

929
		  return decl;
930
		}
931

932 933 934 935 936 937
	      fns = TREE_VEC_ELT(CLASSTYPE_METHOD_VEC (ctype),
				 is_constructor ? 0 : 1);
	    }
	  else 
	    fns = lookup_fnfields (TYPE_BINFO (ctype), name,
				   1);
938 939 940
	  
	  if (fns == NULL_TREE) 
	    {
941 942
	      cp_error ("no member function `%s' declared in `%T'",
			IDENTIFIER_POINTER (name),
943
			ctype);
944
	      return decl;
945 946 947 948
	    }
	  else
	    TREE_OPERAND (declarator, 0) = fns;
	}
949
      
950 951 952 953 954 955 956 957 958
      /* Figure out what exactly is being specialized at this point.
	 Note that for an explicit instantiation, even one for a
	 member function, we cannot tell apriori whether the the
	 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, 
959 960
				       member_specialization,
				       1);
961 962 963
	    
      if (tmpl)
	{
964 965 966 967 968 969 970 971 972 973 974 975
	  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;
	    }
976 977 978
	  else if (DECL_STATIC_FUNCTION_P (tmpl)
		   && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
	    revert_static_member_fn (&decl, 0, 0);
979 980 981 982 983 984 985 986 987 988 989

	  /* 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)
990
	      && name_mangling_version >= 1)
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	    {
	      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);
1017
	      return decl;
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	    }

	  /* 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.	        

1035
	     So, we just leave the template info alone in this case.  */
1036 1037 1038
	  if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)))
	    DECL_TEMPLATE_INFO (decl)
	      = perm_tree_cons (tmpl, targs, NULL_TREE);
1039 1040 1041

	  register_specialization (decl, tmpl, targs);

1042
	  return decl;
1043 1044
	}
    }
1045
  
1046
  return decl;
1047
}
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102


/* 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
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
/* 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
1126
   TEMPLATE_PARM_LEVEL has been decreased by LEVELS.  If such a
jason's avatar
jason committed
1127 1128 1129 1130
   TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
   new one is created.  */

static tree 
1131
reduce_template_parm_level (index, type, levels)
jason's avatar
jason committed
1132 1133
     tree index;
     tree type;
1134
     int levels;
jason's avatar
jason committed
1135 1136 1137
{
  if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
      || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1138
	  != TEMPLATE_PARM_LEVEL (index) - levels))
jason's avatar
jason committed
1139 1140 1141 1142 1143 1144 1145
    {
      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),
1146
				     TEMPLATE_PARM_LEVEL (index) - levels,
jason's avatar
jason committed
1147 1148 1149
				     TEMPLATE_PARM_ORIG_LEVEL (index),
				     decl, type);
      TEMPLATE_PARM_DESCENDANTS (index) = t;
1150 1151 1152 1153

      /* Template template parameters need this.  */
      DECL_TEMPLATE_PARMS (decl)
	= DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
jason's avatar
jason committed
1154 1155 1156 1157 1158
    }

  return TEMPLATE_PARM_DESCENDANTS (index);
}

mrs's avatar
mrs committed
1159
/* Process information from new template parameter NEXT and append it to the
mrs's avatar
mrs committed
1160
   LIST being built.  */
mrs's avatar
mrs committed
1161

mrs's avatar
mrs committed
1162 1163 1164 1165 1166 1167
tree
process_template_parm (list, next)
     tree list, next;
{
  tree parm;
  tree decl = 0;
mrs's avatar
mrs committed
1168
  tree defval;
mrs's avatar
mrs committed
1169
  int is_type, idx;
jason's avatar
jason committed
1170

mrs's avatar
mrs committed
1171 1172
  parm = next;
  my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
mrs's avatar
mrs committed
1173 1174 1175
  defval = TREE_PURPOSE (parm);
  parm = TREE_VALUE (parm);
  is_type = TREE_PURPOSE (parm) == class_type_node;
mrs's avatar
mrs committed
1176 1177 1178 1179 1180 1181 1182

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

      if (TREE_CODE (p) == TYPE_DECL)
	idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1183 1184
      else if (TREE_CODE (p) == TEMPLATE_DECL)
	idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
mrs's avatar
mrs committed
1185
      else
jason's avatar
jason committed
1186
	idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
mrs's avatar
mrs committed
1187 1188 1189 1190 1191
      ++idx;
    }
  else
    idx = 0;

mrs's avatar
mrs committed
1192 1193
  if (!is_type)
    {
mrs's avatar
mrs committed
1194
      my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
mrs's avatar
mrs committed
1195
      /* is a const-param */
mrs's avatar
mrs committed
1196
      parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
mrs's avatar
merging  
mrs committed
1197
			     PARM, 0, NULL_TREE);
mrs's avatar
mrs committed
1198 1199
      /* A template parameter is not modifiable.  */
      TREE_READONLY (parm) = 1;
mrs's avatar
mrs committed
1200 1201
      if (IS_AGGR_TYPE (TREE_TYPE (parm))
	  && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
mrs's avatar
mrs committed
1202
	{
mrs's avatar
mrs committed
1203 1204 1205 1206
	  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
1207 1208
	  TREE_TYPE (parm) = void_type_node;
	}
mrs's avatar
mrs committed
1209 1210 1211
      else if (pedantic
	       && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
		   || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
mrs's avatar
mrs committed
1212 1213
	cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
		    TREE_TYPE (parm));
mrs's avatar
mrs committed
1214 1215 1216 1217 1218 1219
      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
1220 1221 1222 1223
      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
1224 1225 1226
    }
  else
    {
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
      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
1246
      CLASSTYPE_GOT_SEMICOLON (t) = 1;
mrs's avatar
mrs committed
1247 1248
      TYPE_NAME (t) = decl;
      TYPE_STUB_DECL (t) = decl;
mrs's avatar
mrs committed
1249
      parm = decl;
jason's avatar
jason committed
1250 1251 1252 1253
      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
1254
    }
mrs's avatar
mrs committed
1255
  SET_DECL_ARTIFICIAL (decl);
mrs's avatar
mrs committed
1256
  pushdecl (decl);
mrs's avatar
mrs committed
1257
  parm = build_tree_list (defval, parm);
mrs's avatar
mrs committed
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
  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
1270
  int nparms;
mrs's avatar
mrs committed
1271
  tree parm;
mrs's avatar
mrs committed
1272 1273 1274 1275 1276
  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
1277 1278

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

mrs's avatar
mrs committed
1281 1282 1283
  return saved_parmlist;
}

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

mrs's avatar
mrs committed
1286
void
mrs's avatar
mrs committed
1287
end_template_decl ()
mrs's avatar
mrs committed
1288
{
1289 1290
  reset_specialization ();

mrs's avatar
mrs committed
1291
  if (! processing_template_decl)
mrs's avatar
mrs committed
1292 1293
    return;

mrs's avatar
mrs committed
1294 1295
  /* This matches the pushlevel in begin_template_parm_list.  */
  poplevel (0, 0, 0);
mrs's avatar
mrs committed
1296

mrs's avatar
mrs committed
1297 1298 1299 1300
  --processing_template_decl;
  current_template_parms = TREE_CHAIN (current_template_parms);
  (void) get_pending_sizes ();	/* Why? */
}
mrs's avatar
mrs committed
1301

mrs's avatar
mrs committed
1302 1303 1304 1305
/* Generate a valid set of template args from current_template_parms.  */

tree
current_template_args ()
mrs's avatar
mrs committed
1306 1307
{
  tree header = current_template_parms;
1308 1309 1310 1311
  int length = list_length (header);
  tree args = make_tree_vec (length);
  int l = length;

mrs's avatar
mrs committed
1312
  while (header)
mrs's avatar
mrs committed
1313
    {
mrs's avatar
mrs committed
1314 1315 1316 1317 1318
      tree a = copy_node (TREE_VALUE (header));
      int i = TREE_VEC_LENGTH (a);
      TREE_TYPE (a) = NULL_TREE;
      while (i--)
	{
1319 1320 1321 1322 1323 1324 1325 1326 1327
	  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);
	      
1328 1329
	      if (TREE_CODE (t) == TYPE_DECL 
		  || TREE_CODE (t) == TEMPLATE_DECL)
1330 1331 1332 1333 1334
		t = TREE_TYPE (t);
	      else
		t = DECL_INITIAL (t);
	    }

mrs's avatar
mrs committed
1335 1336
	  TREE_VEC_ELT (a, i) = t;
	}
1337
      TREE_VEC_ELT (args, --l) = a;
mrs's avatar
mrs committed
1338
      header = TREE_CHAIN (header);
mrs's avatar
mrs committed
1339 1340
    }

mrs's avatar
mrs committed
1341 1342
  return args;
}
1343

1344 1345 1346 1347

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

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
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;
}

mrs's avatar
mrs committed
1366
  
1367
/* Creates a TEMPLATE_DECL for the indicated DECL using the template
jason's avatar
jason committed
1368 1369 1370
   parameters given by current_template_args, or reuses a
   previously existing one, if appropriate.  Returns the DECL, or an
   equivalent one, if it is replaced via a call to duplicate_decls.  */
1371 1372

tree
mrs's avatar
mrs committed
1373 1374 1375 1376
push_template_decl (decl)
     tree decl;
{
  tree tmpl;
jason's avatar
jason committed
1377
  tree args;
mrs's avatar
mrs committed
1378
  tree info;
jason's avatar
jason committed
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
  tree ctx;
  int primary;
  int is_friend = (TREE_CODE (decl) == FUNCTION_DECL
		   && DECL_FRIEND_P (decl));

  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);
  else if (DECL_REAL_CONTEXT (decl))
    /* 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;

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
  /* 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)
    info = DECL_CLASS_CONTEXT (decl);
  else
    info = ctx;

  if (info && TREE_CODE (info) == FUNCTION_DECL)
    primary = 0;
  else if (! info
	   || (TYPE_BEING_DEFINED (info) && template_header_count
	       && ! processing_specialization)
	   || (template_header_count > template_class_depth (info)))
mrs's avatar
mrs committed
1410
    primary = 1;
jason's avatar
jason committed
1411 1412
  else
    primary = 0;
mrs's avatar
mrs committed
1413

1414 1415 1416 1417 1418 1419 1420 1421
  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
1422
  /* Partial specialization.  */
mrs's avatar
mrs committed
1423
  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
mrs's avatar
mrs committed
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
      && 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);

      for (; spec; spec = TREE_CHAIN (spec))
	{
	  /* purpose: args to main template
	     value: spec template */
	  if (comp_template_args (TREE_PURPOSE (spec), mainargs))
1436
	    return decl;
mrs's avatar
mrs committed
1437 1438
	}

mrs's avatar
mrs committed
1439 1440 1441
      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
1442
      TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
1443
      return decl;
mrs's avatar
mrs committed
1444 1445
    }

mrs's avatar
mrs committed
1446 1447
  args = current_template_args ();

jason's avatar
jason committed
1448 1449 1450 1451
  if (!ctx 
      || TREE_CODE (ctx) == FUNCTION_DECL
      || TYPE_BEING_DEFINED (ctx)
      || (is_friend && !DECL_TEMPLATE_INFO (decl)))
mrs's avatar
mrs committed
1452
    {
1453
      if (DECL_LANG_SPECIFIC (decl)
jason's avatar
jason committed
1454 1455 1456 1457
	  && DECL_TEMPLATE_INFO (decl)
	  && DECL_TI_TEMPLATE (decl))
	tmpl = DECL_TI_TEMPLATE (decl);
      else
1458
	{
jason's avatar
jason committed
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
	  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;
	    }
1470
	}
mrs's avatar
mrs committed
1471 1472 1473
    }
  else
    {
mrs's avatar
mrs committed
1474
      tree t;
1475
      tree a;
mrs's avatar
mrs committed
1476

mrs's avatar
mrs committed
1477 1478 1479
      if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
	cp_error ("must specialize `%#T' before defining member `%#D'",
		  ctx, decl);
mrs's avatar
mrs committed
1480
      if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
mrs's avatar
mrs committed
1481
	tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
mrs's avatar
mrs committed
1482
      else if (! DECL_TEMPLATE_INFO (decl))
mrs's avatar
mrs committed
1483 1484
	{
	  cp_error ("template definition of non-template `%#D'", decl);
1485
	  return decl;
mrs's avatar
mrs committed
1486
	}
mrs's avatar
mrs committed
1487
      else
mrs's avatar
mrs committed
1488
	tmpl = DECL_TI_TEMPLATE (decl);
1489 1490 1491
      
      if (is_member_template (tmpl))
	{
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
	  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);
1514
	      return decl;
1515 1516
	    }
	  
1517 1518
	  a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
	  t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
jason's avatar
jason committed
1519
	  if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
	    {
	      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
1536 1537

      if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
	{
	  /* 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
1550

1551 1552
      if (t != NULL_TREE 
	  && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
mrs's avatar
mrs committed
1553 1554
	{
	  cp_error ("got %d template parameters for `%#D'",
1555
		    TREE_VEC_LENGTH (a), decl);
mrs's avatar
mrs committed
1556 1557
	  cp_error ("  but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
	}
mrs's avatar
mrs committed
1558
    }
1559
  /* Get the innermost set of template arguments. */
jason's avatar
jason committed
1560
  args = innermost_args (args, 0);
mrs's avatar
mrs committed
1561

mrs's avatar
mrs committed
1562 1563
  DECL_TEMPLATE_RESULT (tmpl) = decl;
  TREE_TYPE (tmpl) = TREE_TYPE (decl);
mrs's avatar
mrs committed
1564

jason's avatar
jason committed
1565 1566 1567 1568
  if (! ctx && primary)
    /* The check of PRIMARY ensures 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.  */
mrs's avatar
mrs committed
1569
    tmpl = pushdecl_top_level (tmpl);
mrs's avatar
mrs committed
1570

mrs's avatar
mrs committed
1571
  if (primary)
1572
    TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;
mrs's avatar
mrs committed
1573 1574 1575

  info = perm_tree_cons (tmpl, args, NULL_TREE);

mrs's avatar
mrs committed
1576
  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
mrs's avatar
mrs committed
1577
    {
mrs's avatar
mrs committed
1578
      CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
1579 1580
      if (!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
	DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
mrs's avatar
mrs committed
1581
    }
mrs's avatar
mrs committed
1582 1583
  else if (! DECL_LANG_SPECIFIC (decl))
    cp_error ("template declaration of `%#D'", decl);
mrs's avatar
mrs committed
1584
  else
mrs's avatar
mrs committed
1585
    DECL_TEMPLATE_INFO (decl) = info;
1586 1587

  return DECL_TEMPLATE_RESULT (tmpl);
mrs's avatar
mrs committed
1588 1589
}

1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601

/* 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
1602
convert_nontype_argument (type, expr)
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
     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
1618
     excluding non-static class members, expressed as id-expression;
1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
     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_.  */

1629 1630 1631 1632 1633
  /* 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);

1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
  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)
1645
    {
1646
      if (! TREE_CONSTANT (expr))
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 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
	{
	  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;

      if (TREE_CODE (expr) != ADDR_EXPR)
	{
	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;
	}

      referent = TREE_OPERAND (expr, 0);
      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;
	}
    }
1694
  else if (TREE_CODE (expr) == VAR_DECL)
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
    {
      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);

1720 1721 1722 1723 1724 1725 1726 1727
    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);

1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
    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;

1748
	    if (TREE_CODE (expr) == ADDR_EXPR)
1749 1750 1751 1752
	      fns = TREE_OPERAND (expr, 0);
	    else
	      fns = expr;

1753
	    fn = instantiate_type (type_pointed_to, fns, 0);
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781

	    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
1782 1783 1784 1785 1786
	       non-type template-parameter of pointer type.]  
	    
	       The call to decay_conversion performs the
	       array-to-pointer conversion, if appropriate.  */
	    expr = decay_conversion (expr);
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809

	    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;

1810
	    fn = instantiate_type (type_referred_to, fns, 0);
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883

	    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;

	my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 0);

	/* 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);
	
1884 1885
	fn = instantiate_type (TREE_TYPE (TREE_TYPE (type)), 
			       fns, 0);
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
	
	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
1907 1908
/* Convert all template arguments to their appropriate types, and return
   a vector containing the resulting values.  If any error occurs, return
1909 1910 1911 1912 1913 1914 1915
   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
1916 1917 1918 1919 1920
   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
1921

mrs's avatar
mrs committed
1922
static tree
1923 1924
coerce_template_parms (parms, arglist, in_decl,
		       complain,
1925 1926
		       require_all_arguments,
		       is_tmpl_parm)
mrs's avatar
mrs committed
1927 1928
     tree parms, arglist;
     tree in_decl;
1929 1930
     int complain;
     int require_all_arguments;
1931
     int is_tmpl_parm;
mrs's avatar
mrs committed
1932
{
mrs's avatar
mrs committed
1933
  int nparms, nargs, i, lost = 0;
1934
  tree vec = NULL_TREE;
mrs's avatar
mrs committed
1935

mrs's avatar
mrs committed
1936 1937 1938 1939
  if (arglist == NULL_TREE)
    nargs = 0;
  else if (TREE_CODE (arglist) == TREE_VEC)
    nargs = TREE_VEC_LENGTH (arglist);
mrs's avatar
mrs committed
1940
  else
mrs's avatar
mrs committed
1941 1942 1943 1944 1945 1946
    nargs = list_length (arglist);

  nparms = TREE_VEC_LENGTH (parms);

  if (nargs > nparms
      || (nargs < nparms
1947
	  && require_all_arguments
mrs's avatar
mrs committed
1948
	  && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
mrs's avatar
mrs committed
1949
    {
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
      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
1960 1961 1962
      return error_mark_node;
    }

1963 1964
  if (arglist && TREE_CODE (arglist) == TREE_VEC && nargs == nparms)
    vec = copy_node (arglist);
mrs's avatar
mrs committed
1965 1966 1967
  else
    {
      vec = make_tree_vec (nparms);
1968

mrs's avatar
mrs committed
1969 1970
      for (i = 0; i < nparms; i++)
	{
mrs's avatar
mrs committed
1971
	  tree arg;
1972
	  tree parm = TREE_VEC_ELT (parms, i);
mrs's avatar
mrs committed
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983

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

	      if (arg == error_mark_node)
		lost++;
	      else
		arg = TREE_VALUE (arg);
	    }
1984 1985 1986 1987 1988 1989
	  else if (is_tmpl_parm && i < nargs)
	    {
	      arg = TREE_VEC_ELT (arglist, i);
	      if (arg == error_mark_node)
		lost++;
	    }
1990 1991 1992 1993 1994 1995
	  else if (TREE_PURPOSE (parm) == NULL_TREE)
	    {
	      my_friendly_assert (!require_all_arguments, 0);
	      break;
	    }
	  else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
1996
	    arg = tsubst (TREE_PURPOSE (parm), vec, in_decl);
mrs's avatar
mrs committed
1997
	  else
1998
	    arg = tsubst_expr (TREE_PURPOSE (parm), vec, in_decl);
mrs's avatar
mrs committed
1999

mrs's avatar
mrs committed
2000 2001 2002 2003 2004 2005
	  TREE_VEC_ELT (vec, i) = arg;
	}
    }
  for (i = 0; i < nparms; i++)
    {
      tree arg = TREE_VEC_ELT (vec, i);
mrs's avatar
mrs committed
2006
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
mrs's avatar
mrs committed
2007
      tree val = 0;
2008
      int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
mrs's avatar
mrs committed
2009

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
      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);
	}

2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
      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;
	}

2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
      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;
	}
2046

2047 2048 2049 2050 2051 2052
      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.  */
2053 2054 2055
      is_tmpl_type = (TREE_CODE (arg) == TEMPLATE_DECL
		      && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
		     || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
2056 2057 2058 2059 2060 2061
			 && !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);
2062 2063
      if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
	arg = TYPE_STUB_DECL (arg);
2064 2065
      else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
	arg = CLASSTYPE_TI_TEMPLATE (arg);
2066 2067 2068 2069 2070

      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
2071 2072 2073 2074 2075 2076

      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);
2077

mrs's avatar
mrs committed
2078 2079 2080 2081
	  arg = make_typename_type (TREE_OPERAND (arg, 0),
				    TREE_OPERAND (arg, 1));
	  is_type = 1;
	}
mrs's avatar
mrs committed
2082 2083 2084
      if (is_type != requires_type)
	{
	  if (in_decl)
mrs's avatar
mrs committed
2085
	    {
2086 2087 2088 2089 2090 2091
	      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'",
2092 2093
			      TREE_TYPE (parm),
			      (is_tmpl_type ? DECL_NAME (arg) : arg));
2094 2095 2096
		  else
		    cp_error ("  expected a type, got `%E'", arg);
		}
mrs's avatar
mrs committed
2097
	    }
mrs's avatar
mrs committed
2098 2099 2100 2101
	  lost++;
	  TREE_VEC_ELT (vec, i) = error_mark_node;
	  continue;
	}
2102 2103 2104 2105 2106
      if (is_tmpl_type ^ requires_tmpl_type)
	{
	  if (in_decl)
	    {
	      cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2107
			i + 1, in_decl);
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
	      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
2128
      if (is_type)
mrs's avatar
mrs committed
2129
	{
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
	  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.  */

2143
	      val = coerce_template_parms (argparm, parmparm, in_decl, 1, 1, 1);
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
	      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
2154
	    {
2155 2156
	      val = groktypename (arg);
	      if (! processing_template_decl)
mrs's avatar
mrs committed
2157
		{
2158 2159 2160 2161 2162 2163 2164 2165 2166
		  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
2167 2168 2169
		}
	    }
	}
mrs's avatar
mrs committed
2170 2171
      else
	{
2172
	  tree t = tsubst (TREE_TYPE (parm), vec, in_decl);
2173

2174
	  if (processing_template_decl)
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
	    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.  */
2188
	    val = convert_nontype_argument (t, arg);
mrs's avatar
mrs committed
2189
	  else
2190 2191
	    val = arg;

2192 2193 2194 2195 2196
	  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
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
	}

      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
2209 2210 2211
/* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets
   of template arguments.  Returns 0 otherwise.  */

mrs's avatar
mrs committed
2212
static int
mrs's avatar
mrs committed
2213 2214 2215 2216 2217
comp_template_args (oldargs, newargs)
     tree oldargs, newargs;
{
  int i;

2218 2219 2220
  if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    return 0;

mrs's avatar
mrs committed
2221 2222 2223 2224 2225 2226 2227 2228 2229
  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;
2230 2231 2232 2233 2234 2235 2236
      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
2237 2238 2239 2240 2241
	{
	  if (comptypes (ot, nt, 1))
	    continue;
	}
      else if (cp_tree_equal (ot, nt) > 0)
mrs's avatar
mrs committed
2242 2243 2244 2245 2246 2247
	continue;
      return 0;
    }
  return 1;
}

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

mrs's avatar
mrs committed
2251
static char *
2252
mangle_class_name_for_template (name, parms, arglist, ctx)
mrs's avatar
mrs committed
2253 2254
     char *name;
     tree parms, arglist;
2255
     tree ctx;
mrs's avatar
mrs committed
2256 2257 2258 2259 2260 2261
{
  static struct obstack scratch_obstack;
  static char *scratch_firstobj;
  int i, nparms;

  if (!scratch_firstobj)
mrs's avatar
mrs committed
2262
    gcc_obstack_init (&scratch_obstack);
mrs's avatar
mrs committed
2263 2264
  else
    obstack_free (&scratch_obstack, scratch_firstobj);
mrs's avatar
mrs committed
2265
  scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
mrs's avatar
mrs committed
2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279

#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

2280 2281
  if (ctx)
    {
2282 2283 2284 2285 2286 2287 2288 2289
      char* s;

      if (TREE_CODE (ctx) == FUNCTION_DECL)
	s = fndecl_as_string(ctx, 0);
      else if (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
	s = type_as_string(ctx, 0);
      else
	my_friendly_abort (0);
2290 2291 2292
      cat (s);
      cat ("::");
    }
mrs's avatar
mrs committed
2293 2294 2295 2296 2297 2298
  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
2299 2300
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      tree arg = TREE_VEC_ELT (arglist, i);
mrs's avatar
mrs committed
2301 2302 2303 2304

      if (i)
	ccat (',');

mrs's avatar
mrs committed
2305
      if (TREE_CODE (parm) == TYPE_DECL)
mrs's avatar
mrs committed
2306 2307 2308 2309
	{
	  cat (type_as_string (arg, 0));
	  continue;
	}
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
      else if (TREE_CODE (parm) == TEMPLATE_DECL)
	{
	  if (TREE_CODE (arg) == TEMPLATE_DECL)
	    /* Already substituted with real template.  Just output 
	       the template name here */
	    cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
	  else
	    /* Output the parameter declaration */
	    cat (type_as_string (arg, 0));
	  continue;
	}
mrs's avatar
mrs committed
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
      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
2350
#if 0
mrs's avatar
mrs committed
2351
 too_long:
mrs's avatar
mrs committed
2352
#endif
mrs's avatar
mrs committed
2353 2354 2355 2356 2357
  fatal ("out of (preallocated) string space creating template instantiation name");
  /* NOTREACHED */
  return NULL;
}

mrs's avatar
mrs committed
2358
static tree
mrs's avatar
mrs committed
2359 2360 2361 2362 2363 2364 2365 2366 2367
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));
      char *mangled_name = mangle_class_name_for_template
	(IDENTIFIER_POINTER (name),
2368
	 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
2369
	 CLASSTYPE_TI_ARGS (t), DECL_CONTEXT (t));
mrs's avatar
mrs committed
2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
      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
2382 2383 2384 2385 2386 2387 2388
  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
2389
  if (TI_PENDING_TEMPLATE_FLAG (ti))
mrs's avatar
mrs committed
2390 2391 2392 2393 2394
    return;

  *template_tail = perm_tree_cons
    (current_function_decl, d, NULL_TREE);
  template_tail = &TREE_CHAIN (*template_tail);
mrs's avatar
mrs committed
2395
  TI_PENDING_TEMPLATE_FLAG (ti) = 1;
mrs's avatar
mrs committed
2396 2397
}

2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413

/* 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;
{
  if (fns == NULL_TREE)
    {
      cp_error ("non-template used as template");
      return error_mark_node;
    }

  if (arglist != NULL_TREE && !TREE_PERMANENT (arglist))
2414
    copy_to_permanent (arglist);
2415 2416 2417 2418 2419 2420 2421 2422

  return build_min (TEMPLATE_ID_EXPR,
		    TREE_TYPE (fns) 
		    ? TREE_TYPE (fns) : unknown_type_node, 
		    fns, arglist);  
}


mrs's avatar
mrs committed
2423 2424 2425 2426 2427 2428 2429 2430
/* 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
2431 2432 2433 2434 2435
   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
2436

mrs's avatar
mrs committed
2437
tree
2438
lookup_template_class (d1, arglist, in_decl, context)
mrs's avatar
mrs committed
2439 2440
     tree d1, arglist;
     tree in_decl;
2441
     tree context;
mrs's avatar
mrs committed
2442
{
2443
  tree template = NULL_TREE, parmlist;
mrs's avatar
mrs committed
2444
  char *mangled_name;
mrs's avatar
mrs committed
2445 2446 2447 2448
  tree id, t;

  if (TREE_CODE (d1) == IDENTIFIER_NODE)
    {
2449 2450 2451 2452 2453
      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
2454
	  template = IDENTIFIER_NAMESPACE_VALUE (d1); /* XXX */
2455 2456 2457
	  if (! template)
	    template = IDENTIFIER_CLASS_VALUE (d1);
	}
2458 2459
      if (template)
	context = DECL_CONTEXT (template);
mrs's avatar
mrs committed
2460
    }
mrs's avatar
mrs committed
2461 2462
  else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
    {
2463 2464
      if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)) == NULL_TREE)
	return error_mark_node;
mrs's avatar
mrs committed
2465 2466 2467
      template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
      d1 = DECL_NAME (template);
    }
mrs's avatar
mrs committed
2468 2469 2470 2471 2472
  else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
    {
      template = CLASSTYPE_TI_TEMPLATE (d1);
      d1 = DECL_NAME (template);
    }
2473 2474 2475 2476 2477 2478 2479
  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
2480 2481
  else
    my_friendly_abort (272);
mrs's avatar
mrs committed
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496

  /* 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;
  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;
    }

2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508
  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);

2509
      arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1, 0);
2510 2511 2512 2513 2514 2515 2516 2517 2518
      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;
    }
2519 2520 2521
  else if (PRIMARY_TEMPLATE_P (template)
	   || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template))) 
	       == FUNCTION_DECL))
mrs's avatar
mrs committed
2522
    {
2523
      parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
mrs's avatar
mrs committed
2524

2525
      arglist = coerce_template_parms (parmlist, arglist, template,
2526
				       1, 1, 0);
mrs's avatar
mrs committed
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555
      if (arglist == error_mark_node)
	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;
	    }
	}

2556
      /* FIXME avoid duplication.  */
mrs's avatar
mrs committed
2557
      mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
2558 2559
						     parmlist,
						     arglist,
2560
						     context);
mrs's avatar
mrs committed
2561
      id = get_identifier (mangled_name);
mrs's avatar
mrs committed
2562
      IDENTIFIER_TEMPLATE (id) = d1;
mrs's avatar
mrs committed
2563

mrs's avatar
mrs committed
2564
      maybe_push_to_top_level (uses_template_parms (arglist));
mrs's avatar
mrs committed
2565
      t = xref_tag_from_type (TREE_TYPE (template), id, 1);
2566

2567
      if (context != NULL_TREE)
2568 2569 2570 2571 2572 2573
	{
	  /* 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);

2574 2575
	  TYPE_CONTEXT (t) = context;
	  DECL_CONTEXT (type_decl) = context;
2576 2577 2578 2579 2580
	  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
2581
      pop_from_top_level ();
mrs's avatar
mrs committed
2582
    }
mrs's avatar
mrs committed
2583
  else
mrs's avatar
mrs committed
2584
    {
2585
      tree type_ctx = TYPE_CONTEXT (TREE_TYPE (template));
2586
      tree args = tsubst (CLASSTYPE_TI_ARGS (type_ctx), arglist, in_decl);
2587 2588
      tree ctx = lookup_template_class (type_ctx, args,
					in_decl, NULL_TREE);
mrs's avatar
mrs committed
2589 2590
      id = d1;
      arglist = CLASSTYPE_TI_ARGS (ctx);
mrs's avatar
mrs committed
2591

mrs's avatar
mrs committed
2592
      if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
mrs's avatar
mrs committed
2593
	{
mrs's avatar
mrs committed
2594 2595
	  int save_temp = processing_template_decl;
	  processing_template_decl = 0;
mrs's avatar
mrs committed
2596
	  t = xref_tag_from_type (TREE_TYPE (template), id, 0);
mrs's avatar
mrs committed
2597
	  processing_template_decl = save_temp;
mrs's avatar
mrs committed
2598 2599 2600
	}
      else
	{
mrs's avatar
mrs committed
2601 2602
	  t = lookup_nested_type_by_name (ctx, id);
	  my_friendly_assert (t != NULL_TREE, 42);
mrs's avatar
mrs committed
2603
	}
mrs's avatar
mrs committed
2604 2605
    }

mrs's avatar
mrs committed
2606
  /* Seems to be wanted.  */
mrs's avatar
mrs committed
2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620
  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
2621
      /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up.  */
mrs's avatar
mrs committed
2622
      DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
2623
      /* if (! uses_template_parms (arglist)) */
mrs's avatar
mrs committed
2624 2625 2626 2627 2628 2629
	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
2630
	add_pending_template (t);
mrs's avatar
mrs committed
2631 2632
    }

mrs's avatar
mrs committed
2633
  return t;
mrs's avatar
mrs committed
2634 2635
}

mrs's avatar
mrs committed
2636
/* Should be defined in parse.h.  */
mrs's avatar
mrs committed
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
extern int yychar;

int
uses_template_parms (t)
     tree t;
{
  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.  */
      return uses_template_parms (TREE_TYPE (t));

    case IDENTIFIER_NODE:
      if (!IDENTIFIER_TEMPLATE (t))
	return 0;
mrs's avatar
mrs committed
2657
      my_friendly_abort (42);
mrs's avatar
mrs committed
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678

      /* aggregates of tree nodes */
    case TREE_VEC:
      {
	int i = TREE_VEC_LENGTH (t);
	while (i--)
	  if (uses_template_parms (TREE_VEC_ELT (t, i)))
	    return 1;
	return 0;
      }
    case TREE_LIST:
      if (uses_template_parms (TREE_PURPOSE (t))
	  || uses_template_parms (TREE_VALUE (t)))
	return 1;
      return uses_template_parms (TREE_CHAIN (t));

      /* constructed type nodes */
    case POINTER_TYPE:
    case REFERENCE_TYPE:
      return uses_template_parms (TREE_TYPE (t));
    case RECORD_TYPE:
mrs's avatar
mrs committed
2679 2680
      if (TYPE_PTRMEMFUNC_FLAG (t))
	return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
mrs's avatar
mrs committed
2681
    case UNION_TYPE:
mrs's avatar
mrs committed
2682
      if (! CLASSTYPE_TEMPLATE_INFO (t))
mrs's avatar
mrs committed
2683
	return 0;
mrs's avatar
mrs committed
2684
      return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
mrs's avatar
mrs committed
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
    case FUNCTION_TYPE:
      if (uses_template_parms (TYPE_ARG_TYPES (t)))
	return 1;
      return uses_template_parms (TREE_TYPE (t));
    case ARRAY_TYPE:
      if (uses_template_parms (TYPE_DOMAIN (t)))
	return 1;
      return uses_template_parms (TREE_TYPE (t));
    case OFFSET_TYPE:
      if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
	return 1;
      return uses_template_parms (TREE_TYPE (t));
    case METHOD_TYPE:
2698
      if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
mrs's avatar
mrs committed
2699 2700 2701 2702 2703 2704 2705
	return 1;
      if (uses_template_parms (TYPE_ARG_TYPES (t)))
	return 1;
      return uses_template_parms (TREE_TYPE (t));

      /* decl nodes */
    case TYPE_DECL:
mrs's avatar
mrs committed
2706 2707
      return uses_template_parms (TREE_TYPE (t));

2708 2709 2710 2711 2712 2713 2714 2715 2716
    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;
      
2717 2718 2719 2720 2721
    case CONST_DECL:
      if (uses_template_parms (DECL_INITIAL (t)))
	return 1;
      goto check_type_and_context;

mrs's avatar
mrs committed
2722
    case FUNCTION_DECL:
mrs's avatar
mrs committed
2723 2724 2725 2726
    case VAR_DECL:
      /* ??? What about FIELD_DECLs?  */
      if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
	  && uses_template_parms (DECL_TI_ARGS (t)))
mrs's avatar
mrs committed
2727 2728 2729
	return 1;
      /* fall through */
    case PARM_DECL:
2730
    check_type_and_context:
mrs's avatar
mrs committed
2731 2732
      if (uses_template_parms (TREE_TYPE (t)))
	return 1;
mrs's avatar
mrs committed
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
      if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
	return 1;
      return 0;

    case CALL_EXPR:
      return uses_template_parms (TREE_TYPE (t));
    case ADDR_EXPR:
      return uses_template_parms (TREE_OPERAND (t, 0));

      /* template parm nodes */
    case TEMPLATE_TYPE_PARM:
2744
    case TEMPLATE_TEMPLATE_PARM:
jason's avatar
jason committed
2745
    case TEMPLATE_PARM_INDEX:
mrs's avatar
mrs committed
2746 2747 2748 2749 2750 2751 2752 2753 2754
      return 1;

      /* simple type nodes */
    case INTEGER_TYPE:
      if (uses_template_parms (TYPE_MIN_VALUE (t)))
	return 1;
      return uses_template_parms (TYPE_MAX_VALUE (t));

    case REAL_TYPE:
mrs's avatar
mrs committed
2755
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
2756
    case VOID_TYPE:
mrs's avatar
mrs committed
2757
    case BOOLEAN_TYPE:
mrs's avatar
mrs committed
2758 2759
      return 0;

2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
    case ENUMERAL_TYPE:
      {
	tree v;

	for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v))
	  if (uses_template_parms (TREE_VALUE (v)))
	    return 1;
      }
      return 0;

mrs's avatar
mrs committed
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
      /* 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
2782
    case LOOKUP_EXPR:
mrs's avatar
mrs committed
2783
    case TYPENAME_TYPE:
mrs's avatar
mrs committed
2784 2785
      return 1;

mrs's avatar
mrs committed
2786 2787 2788
    case SCOPE_REF:
      return uses_template_parms (TREE_OPERAND (t, 0));

mrs's avatar
mrs committed
2789 2790 2791
    case CONSTRUCTOR:
      if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
	return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
mrs's avatar
mrs committed
2792
      return uses_template_parms (TREE_OPERAND (t, 1));
mrs's avatar
mrs committed
2793

brendan's avatar
brendan committed
2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
    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;

2805 2806 2807 2808
    case SIZEOF_EXPR:
    case ALIGNOF_EXPR:
      return uses_template_parms (TREE_OPERAND (t, 0));

mrs's avatar
mrs committed
2809 2810 2811 2812 2813
    default:
      switch (TREE_CODE_CLASS (TREE_CODE (t)))
	{
	case '1':
	case '2':
mrs's avatar
mrs committed
2814
	case 'e':
mrs's avatar
mrs committed
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833
	case '<':
	  {
	    int i;
	    for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
	      if (uses_template_parms (TREE_OPERAND (t, i)))
		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;
    }
}

mrs's avatar
mrs committed
2834 2835 2836
static struct tinst_level *current_tinst_level = 0;
static struct tinst_level *free_tinst_level = 0;
static int tinst_depth = 0;
mrs's avatar
mrs committed
2837
extern int max_tinst_depth;
mrs's avatar
mrs committed
2838 2839 2840
#ifdef GATHER_STATISTICS
int depth_reached = 0;
#endif
mrs's avatar
mrs committed
2841

mrs's avatar
mrs committed
2842
static int
mrs's avatar
mrs committed
2843 2844
push_tinst_level (d)
     tree d;
mrs's avatar
mrs committed
2845 2846 2847
{
  struct tinst_level *new;

mrs's avatar
mrs committed
2848 2849
  if (tinst_depth >= max_tinst_depth)
    {
mrs's avatar
mrs committed
2850 2851 2852 2853
      struct tinst_level *p = current_tinst_level;
      int line = lineno;
      char *file = input_filename;

2854 2855 2856 2857 2858 2859
      /* 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;

mrs's avatar
mrs committed
2860 2861
      error ("template instantiation depth exceeds maximum of %d",
	     max_tinst_depth);
mrs's avatar
mrs committed
2862
      error (" (use -ftemplate-depth-NN to increase the maximum)");
mrs's avatar
mrs committed
2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
      cp_error ("  instantiating `%D'", d);

      for (; p; p = p->next)
	{
	  cp_error ("  instantiated from `%D'", p->decl);
	  lineno = p->line;
	  input_filename = p->file;
	}
      error ("  instantiated from here");

      lineno = line;
      input_filename = file;

mrs's avatar
mrs committed
2876 2877 2878
      return 0;
    }

mrs's avatar
mrs committed
2879 2880 2881 2882 2883 2884 2885 2886
  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
2887 2888 2889
  new->decl = d;
  new->line = lineno;
  new->file = input_filename;
mrs's avatar
mrs committed
2890 2891
  new->next = current_tinst_level;
  current_tinst_level = new;
mrs's avatar
mrs committed
2892

mrs's avatar
mrs committed
2893
  ++tinst_depth;
mrs's avatar
mrs committed
2894 2895 2896 2897 2898
#ifdef GATHER_STATISTICS
  if (tinst_depth > depth_reached)
    depth_reached = tinst_depth;
#endif

mrs's avatar
mrs committed
2899
  return 1;
mrs's avatar
mrs committed
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909
}

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
2910
  --tinst_depth;
mrs's avatar
mrs committed
2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
}

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
2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955

/* 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;
  
  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),
2956
						 args, NULL_TREE),
jason's avatar
jason committed
2957
				    tsubst (DECL_TI_ARGS (decl),
2958
					    args, NULL_TREE));
jason's avatar
jason committed
2959 2960 2961 2962 2963
      
      /* Temporarily remove the DECL_TEMPLATE_INFO so as not to
	 confuse tsubst.  */
      tinfo = DECL_TEMPLATE_INFO (decl);
      DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2964
      new_friend = tsubst (decl, args, NULL_TREE);
jason's avatar
jason committed
2965 2966 2967 2968 2969 2970 2971 2972 2973
      DECL_TEMPLATE_INFO (decl) = tinfo;

      tmpl = determine_specialization (template_id,
				       new_friend,
				       &new_args,
				       0, 1);
      return instantiate_template (tmpl, new_args);
    }
    else
2974
      new_friend = tsubst (decl, args, NULL_TREE);
jason's avatar
jason committed
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 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013
	
  /* 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;
  
  if (DECL_CONTEXT (new_friend) == NULL_TREE)
    {
      if (TREE_CODE (new_friend) == TEMPLATE_DECL)
	/* This declaration is a `primary' template.  */
	TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (new_friend))
	    = new_friend;

	new_friend = pushdecl_top_level (new_friend);
    }
  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;
    }

  return new_friend;
}


mrs's avatar
mrs committed
3014
tree
mrs's avatar
mrs committed
3015 3016
instantiate_class_template (type)
     tree type;
mrs's avatar
mrs committed
3017
{
mrs's avatar
mrs committed
3018
  tree template, template_info, args, pattern, t, *field_chain;
3019
  tree typedecl, outer_args;
mrs's avatar
mrs committed
3020

mrs's avatar
mrs committed
3021
  if (type == error_mark_node)
mrs's avatar
mrs committed
3022 3023
    return error_mark_node;

mrs's avatar
mrs committed
3024 3025 3026 3027 3028 3029 3030 3031
  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
3032

3033 3034
  if (DECL_TEMPLATE_INFO (template))
    {
3035
      outer_args = DECL_TI_ARGS (template);
3036 3037 3038
      while (DECL_TEMPLATE_INFO (template))
	template = DECL_TI_TEMPLATE (template);
    }
3039 3040
  else
    outer_args = NULL_TREE;
3041

mrs's avatar
mrs committed
3042
  t = most_specialized_class
3043
    (DECL_TEMPLATE_SPECIALIZATIONS (template), args, outer_args);
mrs's avatar
mrs committed
3044 3045 3046 3047 3048 3049 3050

  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))
	{
3051 3052
	  if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
				  args, outer_args))
mrs's avatar
mrs committed
3053 3054 3055 3056 3057 3058
	    {
	      cp_error_at ("%s %+#T", str, TREE_TYPE (t));
	      str = "               ";
	    }
	}
      TYPE_BEING_DEFINED (type) = 1;
mrs's avatar
mrs committed
3059
      return error_mark_node;
mrs's avatar
mrs committed
3060 3061 3062 3063 3064
    }
  else if (t)
    pattern = TREE_TYPE (t);
  else
    pattern = TREE_TYPE (template);
mrs's avatar
mrs committed
3065 3066

  if (TYPE_SIZE (pattern) == NULL_TREE)
mrs's avatar
mrs committed
3067
    return type;
mrs's avatar
mrs committed
3068

mrs's avatar
mrs committed
3069
  if (t)
3070 3071 3072
    args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
			       args, outer_args);

3073 3074 3075 3076 3077 3078 3079
  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;

mrs's avatar
mrs committed
3080 3081 3082 3083
  TYPE_BEING_DEFINED (type) = 1;

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

mrs's avatar
mrs committed
3085 3086 3087
  maybe_push_to_top_level (uses_template_parms (type));
  pushclass (type, 0);

3088 3089 3090
  if (outer_args)
    args = add_to_template_args (outer_args, args);

mrs's avatar
mrs committed
3091 3092 3093 3094 3095 3096 3097
  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)
3098 3099
	    = (! CLASSTYPE_INTERFACE_ONLY (type)
	       && CLASSTYPE_INTERFACE_KNOWN (type));
mrs's avatar
mrs committed
3100 3101 3102 3103 3104 3105 3106
	}
      else
	{
	  CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
	  SET_CLASSTYPE_INTERFACE_UNKNOWN_X
	    (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
	  CLASSTYPE_VTABLE_NEEDS_WRITING (type)
3107 3108
	    = (! CLASSTYPE_INTERFACE_ONLY (type)
	       && CLASSTYPE_INTERFACE_KNOWN (type));
mrs's avatar
mrs committed
3109 3110 3111
	}
    }
  else
mrs's avatar
mrs committed
3112
    {
mrs's avatar
mrs committed
3113 3114
      SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
      CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
mrs's avatar
mrs committed
3115 3116
    }

mrs's avatar
mrs committed
3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
  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_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (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);

3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157
  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
3158 3159 3160 3161 3162 3163 3164 3165 3166
  {
    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
3167
	bases = make_tree_vec (len);
mrs's avatar
mrs committed
3168 3169 3170 3171 3172
	for (i = 0; i < len; ++i)
	  {
	    tree elt;

	    TREE_VEC_ELT (bases, i) = elt
3173
	      = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE);
mrs's avatar
mrs committed
3174 3175
	    BINFO_INHERITANCE_CHAIN (elt) = binfo;

mrs's avatar
mrs committed
3176 3177 3178 3179
	    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);
3180
	    else if (TYPE_SIZE (complete_type (TREE_TYPE (elt))) == NULL_TREE)
mrs's avatar
mrs committed
3181 3182 3183
	      cp_error ("base class `%T' of `%T' has incomplete type",
			TREE_TYPE (elt), type);
	  }
mrs's avatar
mrs committed
3184 3185 3186
	/* Don't initialize this until the vector is filled out, or
	   lookups will crash.  */
	BINFO_BASETYPES (binfo) = bases;
mrs's avatar
mrs committed
3187 3188 3189 3190 3191 3192
      }
  }

  field_chain = &TYPE_FIELDS (type);

  for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
mrs's avatar
mrs committed
3193
    {
mrs's avatar
mrs committed
3194 3195
      tree tag = TREE_VALUE (t);

mrs's avatar
mrs committed
3196
      /* These will add themselves to CLASSTYPE_TAGS for the new type.  */
mrs's avatar
mrs committed
3197
      if (TREE_CODE (tag) == ENUMERAL_TYPE)
mrs's avatar
mrs committed
3198
	{
manfred's avatar
d  
manfred committed
3199
	  (void) tsubst_enum (tag, args, field_chain);
mrs's avatar
mrs committed
3200
	  while (*field_chain)
mrs's avatar
mrs committed
3201 3202 3203 3204
	    {
	      DECL_FIELD_CONTEXT (*field_chain) = type;
	      field_chain = &TREE_CHAIN (*field_chain);
	    }
mrs's avatar
mrs committed
3205
	}
mrs's avatar
mrs committed
3206
      else
3207
	tsubst (tag, args, NULL_TREE);
mrs's avatar
mrs committed
3208 3209
    }

mrs's avatar
mrs committed
3210 3211 3212 3213
  /* Don't replace enum constants here.  */
  for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
    if (TREE_CODE (t) != CONST_DECL)
      {
3214
	tree r = tsubst (t, args, NULL_TREE);
mrs's avatar
mrs committed
3215 3216
	if (TREE_CODE (r) == VAR_DECL)
	  {
3217 3218
	    pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
	    /* Perhaps we should do more of grokfield here.  */
mrs's avatar
mrs committed
3219 3220 3221 3222 3223 3224 3225 3226 3227
	    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
3228

mrs's avatar
mrs committed
3229
  TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
mrs's avatar
mrs committed
3230 3231 3232 3233 3234 3235 3236
  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
3237

3238 3239 3240 3241 3242
  /* 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
3243
    {
3244
      tree friends;
jason's avatar
jason committed
3245

3246 3247 3248
      DECL_FRIENDLIST (typedecl)
	= tree_cons (TREE_PURPOSE (t), NULL_TREE, 
		     DECL_FRIENDLIST (typedecl));
jason's avatar
jason committed
3249

3250 3251 3252 3253 3254
      for (friends = TREE_VALUE (t);
	   friends != NULL_TREE;
	   friends = TREE_CHAIN (friends))
	{
	  if (TREE_PURPOSE (friends) == error_mark_node)
jason's avatar
jason committed
3255
	    {
3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267
	      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
3268 3269 3270

	    }
	}
3271
    }
mrs's avatar
mrs committed
3272

3273 3274
  t = CLASSTYPE_FRIEND_CLASSES (type)
    = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args, NULL_TREE);
mrs's avatar
mrs committed
3275

3276 3277 3278
  /* This does injection for friend classes.  */
  for (; t; t = TREE_CHAIN (t))
    TREE_VALUE (t) = xref_tag_from_type (TREE_VALUE (t), NULL_TREE, 1);
mrs's avatar
mrs committed
3279

3280 3281 3282 3283
  /* This does injection for friend functions. */
  if (!processing_template_decl)
    {
      t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE);
mrs's avatar
mrs committed
3284

3285 3286 3287
      for (; t; t = TREE_CHAIN (t))
	{
	  tree d = TREE_VALUE (t);
mrs's avatar
mrs committed
3288

3289 3290 3291 3292 3293 3294
	  if (TREE_CODE (d) == TYPE_DECL)
	    /* Already injected.  */;
	  else
	    pushdecl (d);
	}
    } 
mrs's avatar
mrs committed
3295

3296 3297 3298 3299 3300 3301
  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
3302

3303 3304
  type = finish_struct_1 (type, 0);
  CLASSTYPE_GOT_SEMICOLON (type) = 1;
mrs's avatar
mrs committed
3305

3306 3307 3308
  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
3309

3310
 end:
mrs's avatar
mrs committed
3311 3312 3313 3314 3315 3316 3317
  TYPE_BEING_DEFINED (type) = 0;
  popclass (0);

  pop_from_top_level ();
  pop_tinst_level ();

  return type;
mrs's avatar
mrs committed
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
}

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
3336
tree 
mrs's avatar
mrs committed
3337 3338 3339 3340 3341
lookup_nested_type_by_name (ctype, name)
        tree ctype, name;
{
  tree t;

mrs's avatar
mrs committed
3342 3343
  complete_type (ctype);

mrs's avatar
mrs committed
3344 3345
  for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
    {
3346 3347 3348
      if (name == TREE_PURPOSE (t)
	  /* this catches typedef enum { foo } bar; */
	  || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
mrs's avatar
mrs committed
3349 3350
	return TREE_VALUE (t);
    }
mrs's avatar
mrs committed
3351 3352 3353
  return NULL_TREE;
}

3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
/* 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;
}

3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395
/* 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;
{
  if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
    return TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1 - is_spec);
  return args;
}
jason's avatar
jason committed
3396

3397
/* Take the tree structure T and replace template parameters used therein
3398 3399
   with the argument vector ARGS.  IN_DECL is an associated decl for
   diagnostics.
3400 3401 3402 3403

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

3404
tree
3405
tsubst (t, args, in_decl)
3406
     tree t, args;
mrs's avatar
mrs committed
3407 3408 3409 3410
     tree in_decl;
{
  tree type;

mrs's avatar
mrs committed
3411 3412 3413 3414
  if (t == NULL_TREE || t == error_mark_node
      || t == integer_type_node
      || t == void_type_node
      || t == char_type_node)
mrs's avatar
mrs committed
3415 3416 3417
    return t;

  type = TREE_TYPE (t);
mrs's avatar
mrs committed
3418 3419
  if (type == unknown_type_node)
    my_friendly_abort (42);
mrs's avatar
mrs committed
3420
  if (type && TREE_CODE (t) != FUNCTION_DECL
jason's avatar
jason committed
3421 3422
      && TREE_CODE (t) != TYPENAME_TYPE
      && TREE_CODE (t) != TEMPLATE_DECL)
3423
    type = tsubst (type, args, in_decl);
mrs's avatar
mrs committed
3424

mrs's avatar
mrs committed
3425 3426 3427 3428
  switch (TREE_CODE (t))
    {
    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_P (t))
mrs's avatar
mrs committed
3429 3430
	{
	  tree r = build_ptrmemfunc_type
3431
	    (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl));
mrs's avatar
mrs committed
3432 3433 3434 3435
	  return cp_build_type_variant (r, TYPE_READONLY (t),
					TYPE_VOLATILE (t));
	}

mrs's avatar
mrs committed
3436
      /* else fall through */
mrs's avatar
mrs committed
3437 3438 3439
    case UNION_TYPE:
      if (uses_template_parms (t))
	{
3440
	  tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl);
3441 3442 3443
	  tree context;
	  tree r;

3444 3445
	  context = 
	    TYPE_CONTEXT (t) 
3446
	    ? tsubst (TYPE_CONTEXT (t), args, in_decl) : NULL_TREE;
3447 3448 3449

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

mrs's avatar
mrs committed
3450 3451 3452
	  return cp_build_type_variant (r, TYPE_READONLY (t),
					TYPE_VOLATILE (t));
	}
mrs's avatar
mrs committed
3453

mrs's avatar
mrs committed
3454
      /* else fall through */
mrs's avatar
mrs committed
3455 3456 3457 3458 3459
    case ERROR_MARK:
    case IDENTIFIER_NODE:
    case OP_IDENTIFIER:
    case VOID_TYPE:
    case REAL_TYPE:
mrs's avatar
mrs committed
3460
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
3461
    case BOOLEAN_TYPE:
mrs's avatar
mrs committed
3462 3463 3464 3465 3466
    case INTEGER_CST:
    case REAL_CST:
    case STRING_CST:
      return t;

mrs's avatar
mrs committed
3467 3468
    case ENUMERAL_TYPE:
      {
3469
	tree ctx = tsubst (TYPE_CONTEXT (t), args, in_decl);
mrs's avatar
mrs committed
3470 3471
	if (ctx == NULL_TREE)
	  return t;
mrs's avatar
mrs committed
3472 3473
	else if (ctx == current_function_decl)
	  return lookup_name (TYPE_IDENTIFIER (t), 1);
mrs's avatar
mrs committed
3474 3475 3476 3477
	else
	  return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
      }

mrs's avatar
mrs committed
3478 3479 3480 3481 3482 3483 3484
    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
3485 3486

      {
mrs's avatar
mrs committed
3487
	tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
3488
	max = tsubst_expr (max, args, in_decl);
mrs's avatar
mrs committed
3489
	if (processing_template_decl)
mrs's avatar
mrs committed
3490 3491 3492
	  {
	    tree itype = make_node (INTEGER_TYPE);
	    TYPE_MIN_VALUE (itype) = size_zero_node;
mrs's avatar
mrs committed
3493 3494
	    TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
						integer_one_node);
mrs's avatar
mrs committed
3495 3496
	    return itype;
	  }
mrs's avatar
mrs committed
3497 3498

	max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
mrs's avatar
mrs committed
3499 3500
	return build_index_2_type (size_zero_node, max);
      }
mrs's avatar
mrs committed
3501 3502

    case TEMPLATE_TYPE_PARM:
3503
    case TEMPLATE_TEMPLATE_PARM:
jason's avatar
jason committed
3504
    case TEMPLATE_PARM_INDEX:
mrs's avatar
mrs committed
3505
      {
3506 3507
	int idx;
	int level;
3508
	int levels;
jason's avatar
jason committed
3509
	tree r = NULL_TREE;
3510

3511 3512
	if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
	    || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
3513 3514 3515 3516 3517 3518
	  {
	    idx = TEMPLATE_TYPE_IDX (t);
	    level = TEMPLATE_TYPE_LEVEL (t);
	  }
	else
	  {
jason's avatar
jason committed
3519 3520
	    idx = TEMPLATE_PARM_IDX (t);
	    level = TEMPLATE_PARM_LEVEL (t);
3521 3522
	  }

jason's avatar
jason committed
3523
	if (TREE_VEC_LENGTH (args) > 0)
3524 3525 3526 3527 3528
	  {
	    tree arg = NULL_TREE;

	    if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
	      {
3529 3530
		levels = TREE_VEC_LENGTH (args);
		if (level <= levels)
3531 3532 3533
		  arg = TREE_VEC_ELT
		    (TREE_VEC_ELT (args, level - 1), idx);
	      }
3534 3535 3536 3537 3538 3539
	    else
	      {
		levels = 1;
		if (level == 1)
		  arg = TREE_VEC_ELT (args, idx);
	      }
3540 3541 3542 3543 3544 3545 3546

	    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));
3547 3548 3549 3550 3551 3552
		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
3553
			tree argvec = tsubst (CLASSTYPE_TI_ARGS (t),
3554
					      args, in_decl);
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
			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), 
3566 3567
						   argvec, in_decl, 
						   DECL_CONTEXT (arg));
3568 3569 3570 3571 3572 3573 3574
			return cp_build_type_variant (r, TYPE_READONLY (t),
						      TYPE_VOLATILE (t));
		      }
		    else
		      /* We are processing a template argument list.  */ 
		      return arg;
		  }
3575 3576 3577 3578 3579 3580 3581
		else
		  return arg;
	      }
	  }

	/* If we get here, we must have been looking at a parm for a
	   more deeply nested template.  */
jason's avatar
jason committed
3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592
	my_friendly_assert(level > 1, 0);
	
	/* Make a new version of this template parameter, but with a
	   lower level.  */
	switch (TREE_CODE (t))
	  {
	  case TEMPLATE_TYPE_PARM:
	  case TEMPLATE_TEMPLATE_PARM:
	    r = copy_node (t);
	    TEMPLATE_TYPE_PARM_INDEX (r)
	      = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
3593
					    r, levels);
jason's avatar
jason committed
3594 3595 3596
	    TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
	    TYPE_MAIN_VARIANT (r) = r;
	    TYPE_POINTER_TO (r) = NULL_TREE;
3597
	    TYPE_REFERENCE_TO (r) = NULL_TREE;
jason's avatar
jason committed
3598 3599 3600
	    break;

	  case TEMPLATE_PARM_INDEX:
3601
	    r = reduce_template_parm_level (t, TREE_TYPE (t), levels);
jason's avatar
jason committed
3602 3603 3604 3605 3606 3607 3608
	    break;
	   
	  default:
	    my_friendly_abort (0);
	  }

	return r;
mrs's avatar
mrs committed
3609
      }
mrs's avatar
mrs committed
3610

3611 3612 3613 3614 3615 3616 3617
    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
3618
	tree* new_parms;
3619
	tree spec;
3620

3621 3622 3623 3624 3625
	if (TREE_CODE (decl) == TYPE_DECL
	    && TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
	  /* There is no tsubst'ing to be done in a template template
	     parameter.  */
	  return t;
jason's avatar
jason committed
3626

3627
	/* We might already have an instance of this template. */
3628 3629 3630
	spec = retrieve_specialization (t, args);
	if (spec != NULL_TREE)
	  return spec;
3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641

	/* 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);
	DECL_CHAIN (tmpl) = NULL_TREE;
	TREE_CHAIN (tmpl) = NULL_TREE;
jason's avatar
jason committed
3642
	DECL_CONTEXT (tmpl) = tsubst (DECL_CONTEXT (t),
3643
				      args, in_decl);
jason's avatar
jason committed
3644
	DECL_CLASS_CONTEXT (tmpl) = tsubst (DECL_CLASS_CONTEXT (t),
3645
					    args, in_decl);
3646
	DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662

	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);
	  }

3663
	DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE;
jason's avatar
jason committed
3664
	SET_DECL_IMPLICIT_INSTANTIATION (tmpl);
3665 3666 3667 3668

	/* 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
3669 3670
	for (new_parms = &DECL_TEMPLATE_PARMS (tmpl),
	       parms = DECL_TEMPLATE_PARMS (t);
3671
	     TREE_CHAIN (parms) != NULL_TREE;
jason's avatar
jason committed
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
	     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)
3687 3688
		  = build_tree_list (tsubst (default_value, args, in_decl),
				     tsubst (parm_decl, args, in_decl));
jason's avatar
jason committed
3689 3690 3691 3692 3693 3694 3695 3696 3697 3698
		  
	      }

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

3700 3701 3702
	if (PRIMARY_TEMPLATE_P (t))
	  TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;

3703
	/* We don't partially instantiate partial specializations.  */
3704 3705 3706
	if (TREE_CODE (decl) == TYPE_DECL)
	  return tmpl;

3707 3708 3709 3710 3711
	/* 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?  */ 
3712
	DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 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
	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;

3753
	    spec_args = tsubst (DECL_TI_ARGS (fn), args,
3754
				in_decl); 
3755
	    new_fn = tsubst (DECL_RESULT (fn), args,
3756 3757 3758 3759 3760 3761 3762 3763 3764
			     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);

3765 3766
	return tmpl;
      }
mrs's avatar
mrs committed
3767 3768 3769

    case FUNCTION_DECL:
      {
mrs's avatar
mrs committed
3770
	tree r = NULL_TREE;
3771
	tree ctx;
3772
	tree argvec;
jason's avatar
jason committed
3773
	tree tmpl = NULL_TREE;
mrs's avatar
mrs committed
3774 3775
	int member;

mrs's avatar
mrs committed
3776 3777 3778
	if (DECL_CONTEXT (t) != NULL_TREE
	    && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
	  {
mrs's avatar
mrs committed
3779 3780 3781 3782
	    if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
	      member = 2;
	    else
	      member = 1;
3783 3784
	    ctx = tsubst (DECL_CLASS_CONTEXT (t), args, t);
	    type = tsubst (type, args, in_decl);
mrs's avatar
mrs committed
3785 3786 3787 3788 3789
	  }
	else
	  {
	    member = 0;
	    ctx = NULL_TREE;
3790
	    type = tsubst (type, args, in_decl);
mrs's avatar
mrs committed
3791
	  }
mrs's avatar
mrs committed
3792

jason's avatar
jason committed
3793
	/* If we are instantiating a specialization, get the other args.  */
mrs's avatar
mrs committed
3794 3795
	if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
	  {
jason's avatar
jason committed
3796 3797 3798
	    tree spec;

	    tmpl = DECL_TI_TEMPLATE (t);
3799 3800

	    /* Start by getting the innermost args.  */
3801
	    argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
3802 3803 3804 3805 3806 3807

	    /* If tmpl is an instantiation of a member template, tack on
	       the args for the enclosing class.  NOTE: this will change
	       for member class templates.  The generalized procedure
	       is to grab the outer args, then tack on the current args,
	       then any specialized args.  */
jason's avatar
jason committed
3808
	    if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
3809 3810 3811 3812 3813 3814 3815 3816 3817
	      {
		if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
		  argvec = add_to_template_args (DECL_TI_ARGS (tmpl), argvec);
		else
		  /* In this case, we are instantiating a
		     specialization.  The innermost template args are
		     already given by the specialization.  */
		  argvec = add_to_template_args (argvec, DECL_TI_ARGS (tmpl));
	      }
mrs's avatar
mrs committed
3818

jason's avatar
jason committed
3819
	    /* Do we already have this instantiation?  */
3820
	    spec = retrieve_specialization (tmpl, argvec);
3821 3822
	    if (spec)
	      return spec;
mrs's avatar
mrs committed
3823 3824 3825 3826 3827
	  }

	/* 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
3828
           discrete model.  Instead, see add_maybe_template.  */
mrs's avatar
mrs committed
3829 3830 3831

	r = copy_node (t);
	copy_lang_decl (r);
3832
	DECL_USE_TEMPLATE (r) = 0;
mrs's avatar
mrs committed
3833 3834 3835
	TREE_TYPE (r) = type;

	DECL_CONTEXT (r)
3836
	  = tsubst (DECL_CONTEXT (t), args, t);
mrs's avatar
mrs committed
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857
	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
3858
	  }
3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874
	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
3875
	       member function of a template class.  */
3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892
	    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
3893
		tree tparms; 
3894 3895 3896 3897
		tree targs;

		if (!DECL_TEMPLATE_SPECIALIZATION (tmpl)) 
		  {
3898 3899 3900 3901 3902 3903
		    /* 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.  */
3904 3905 3906 3907
		    tparms = DECL_TEMPLATE_PARMS (tmpl);
		    while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
		      tparms = TREE_CHAIN (tparms);
		    
3908
		    targs = innermost_args (args, 0);
3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929
		  }
		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);
		  }
		    
3930 3931 3932 3933
		my_friendly_assert (tparms != NULL_TREE
				    && TREE_CODE (tparms) == TREE_LIST,
				    0);
		tparms = TREE_VALUE (tparms);
3934
		 
3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
		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)),
3945
		   tparms, targs, member);
3946 3947
	      }
	  }
mrs's avatar
mrs committed
3948 3949 3950
	DECL_RTL (r) = 0;
	make_decl_rtl (r, NULL_PTR, 1);

3951
	DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t);
mrs's avatar
mrs committed
3952 3953 3954 3955 3956
	DECL_MAIN_VARIANT (r) = r;
	DECL_RESULT (r) = NULL_TREE;
	DECL_INITIAL (r) = NULL_TREE;

	TREE_STATIC (r) = 0;
3957
	TREE_PUBLIC (r) = TREE_PUBLIC (t);
mrs's avatar
mrs committed
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970
	DECL_EXTERNAL (r) = 1;
	DECL_INTERFACE_KNOWN (r) = 0;
	DECL_DEFER_OUTPUT (r) = 0;
	TREE_CHAIN (r) = NULL_TREE;
	DECL_CHAIN (r) = NULL_TREE;

	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);

3971 3972 3973 3974 3975 3976 3977
	    /* 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
3978 3979 3980 3981
	      SET_DECL_TEMPLATE_SPECIALIZATION (r);
	    else
	      SET_DECL_IMPLICIT_INSTANTIATION (r);

3982
	    register_specialization (r, tmpl, argvec);
mrs's avatar
mrs committed
3983 3984
	  }

mrs's avatar
mrs committed
3985 3986 3987 3988 3989 3990
	/* 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)
	  IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;

mrs's avatar
mrs committed
3991 3992 3993 3994 3995
	return r;
      }

    case PARM_DECL:
      {
mrs's avatar
mrs committed
3996 3997
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
mrs's avatar
mrs committed
3998
	DECL_INITIAL (r) = TREE_TYPE (r);
mrs's avatar
mrs committed
3999
	DECL_CONTEXT (r) = NULL_TREE;
kenner's avatar
kenner committed
4000 4001 4002 4003 4004 4005
#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
4006
	if (TREE_CHAIN (t))
4007
	  TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t));
mrs's avatar
mrs committed
4008 4009 4010
	return r;
      }

mrs's avatar
mrs committed
4011 4012 4013 4014 4015 4016
    case FIELD_DECL:
      {
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
	copy_lang_decl (r);
#if 0
4017
	DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl);
mrs's avatar
mrs committed
4018
#endif
4019
	DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl);
mrs's avatar
mrs committed
4020 4021 4022 4023 4024 4025 4026 4027
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }

    case USING_DECL:
      {
	tree r = copy_node (t);
	DECL_INITIAL (r)
4028
	  = tsubst_copy (DECL_INITIAL (t), args, in_decl);
mrs's avatar
mrs committed
4029 4030 4031 4032 4033 4034 4035
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }

    case VAR_DECL:
      {
	tree r;
4036
	tree ctx = tsubst_copy (DECL_CONTEXT (t), args, in_decl);
mrs's avatar
mrs committed
4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054

	/* 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
4055 4056 4057 4058

	/* 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
4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072

	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);
4073
	    tree argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
mrs's avatar
mrs committed
4074 4075 4076 4077 4078 4079 4080 4081 4082 4083

	    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
4084 4085 4086
      if (t == TYPE_NAME (TREE_TYPE (t)))
	return TYPE_NAME (type);

mrs's avatar
mrs committed
4087 4088 4089
      {
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
mrs's avatar
mrs committed
4090
	DECL_CONTEXT (r) = current_class_type;
mrs's avatar
mrs committed
4091 4092 4093 4094
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }	  

mrs's avatar
mrs committed
4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108
    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)
4109
	  purpose = tsubst (purpose, args, in_decl);
mrs's avatar
mrs committed
4110 4111
	value = TREE_VALUE (t);
	if (value)
4112
	  value = tsubst (value, args, in_decl);
mrs's avatar
mrs committed
4113 4114
	chain = TREE_CHAIN (t);
	if (chain && chain != void_type_node)
4115
	  chain = tsubst (chain, args, in_decl);
mrs's avatar
mrs committed
4116 4117 4118 4119 4120 4121 4122 4123 4124 4125
	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
4126 4127
      if (type != NULL_TREE)
	{
4128 4129
	  /* A binfo node.  */

mrs's avatar
mrs committed
4130 4131 4132 4133 4134 4135
	  t = copy_node (t);

	  if (type == TREE_TYPE (t))
	    return t;

	  TREE_TYPE (t) = complete_type (type);
mrs's avatar
mrs committed
4136 4137 4138 4139 4140 4141 4142
	  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
4143 4144
	  return t;
	}
4145 4146

      /* Otherwise, a vector of template arguments.  */
mrs's avatar
mrs committed
4147 4148 4149
      {
	int len = TREE_VEC_LENGTH (t), need_new = 0, i;
	tree *elts = (tree *) alloca (len * sizeof (tree));
mrs's avatar
mrs committed
4150

4151
	bzero ((char *) elts, len * sizeof (tree));
mrs's avatar
mrs committed
4152 4153 4154

	for (i = 0; i < len; i++)
	  {
4155
	    elts[i] = maybe_fold_nontype_arg
4156
	      (tsubst_expr (TREE_VEC_ELT (t, i), args, in_decl));
4157

mrs's avatar
mrs committed
4158 4159 4160 4161 4162 4163 4164 4165 4166 4167
	    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];
mrs's avatar
mrs committed
4168
	
mrs's avatar
mrs committed
4169 4170 4171 4172 4173 4174 4175
	return t;
      }
    case POINTER_TYPE:
    case REFERENCE_TYPE:
      {
	tree r;
	enum tree_code code;
4176

mrs's avatar
mrs committed
4177 4178 4179 4180
	if (type == TREE_TYPE (t))
	  return t;

	code = TREE_CODE (t);
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
	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
4208 4209 4210
	  r = build_pointer_type (type);
	else
	  r = build_reference_type (type);
mrs's avatar
mrs committed
4211
	r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
4212

mrs's avatar
mrs committed
4213 4214 4215 4216
	/* Will this ever be needed for TYPE_..._TO values?  */
	layout_type (r);
	return r;
      }
mrs's avatar
mrs committed
4217 4218
    case OFFSET_TYPE:
      return build_offset_type
4219
	(tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl), type);
mrs's avatar
mrs committed
4220 4221 4222
    case FUNCTION_TYPE:
    case METHOD_TYPE:
      {
4223
	tree values = TYPE_ARG_TYPES (t);
mrs's avatar
mrs committed
4224
	tree context = TYPE_CONTEXT (t);
mrs's avatar
merging  
mrs committed
4225 4226
	tree raises = TYPE_RAISES_EXCEPTIONS (t);
	tree fntype;
mrs's avatar
mrs committed
4227 4228 4229

	/* Don't bother recursing if we know it won't change anything.	*/
	if (values != void_list_node)
mrs's avatar
mrs committed
4230 4231 4232 4233
	  {
	    /* This should probably be rewritten to use hash_tree_cons for
               the memory savings.  */
	    tree first = NULL_TREE;
4234
	    tree last = NULL_TREE;
mrs's avatar
mrs committed
4235 4236 4237 4238

	    for (; values && values != void_list_node;
		 values = TREE_CHAIN (values))
	      {
mrs's avatar
mrs committed
4239
		tree value = TYPE_MAIN_VARIANT (type_decays_to
4240
		  (tsubst (TREE_VALUE (values), args, in_decl)));
mrs's avatar
mrs committed
4241 4242 4243
		/* 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
4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257
		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
4258
	if (context)
4259
	  context = tsubst (context, args, in_decl);
mrs's avatar
mrs committed
4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
	/* 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
4273
	    fntype = build_function_type (type, values);
mrs's avatar
mrs committed
4274 4275 4276 4277
	  }
	else if (context == NULL_TREE)
	  {
	    tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
4278
				args, in_decl);
mrs's avatar
merging  
mrs committed
4279 4280
	    fntype = build_cplus_method_type (base, type,
					      TREE_CHAIN (values));
mrs's avatar
mrs committed
4281 4282 4283
	  }
	else
	  {
mrs's avatar
merging  
mrs committed
4284 4285 4286 4287 4288 4289 4290
	    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
4291
	    if (TYPE_METHOD_BASETYPE (t))
mrs's avatar
merging  
mrs committed
4292
	      TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
4293
						      args, in_decl);
mrs's avatar
mrs committed
4294 4295 4296
	    /* Need to generate hash value.  */
	    my_friendly_abort (84);
	  }
mrs's avatar
merging  
mrs committed
4297 4298 4299 4300 4301
	fntype = build_type_variant (fntype,
				     TYPE_READONLY (t),
				     TYPE_VOLATILE (t));
	if (raises)
	  {
4302
	    raises = tsubst (raises, args, in_decl);
mrs's avatar
merging  
mrs committed
4303 4304 4305
	    fntype = build_exception_variant (fntype, raises);
	  }
	return fntype;
mrs's avatar
mrs committed
4306 4307 4308
      }
    case ARRAY_TYPE:
      {
4309
	tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl);
mrs's avatar
mrs committed
4310 4311 4312 4313 4314 4315 4316 4317
	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
4318
    case MINUS_EXPR:
mrs's avatar
mrs committed
4319
      return fold (build (TREE_CODE (t), TREE_TYPE (t),
4320 4321
			  tsubst (TREE_OPERAND (t, 0), args, in_decl),
			  tsubst (TREE_OPERAND (t, 1), args, in_decl)));
mrs's avatar
mrs committed
4322 4323 4324 4325

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

mrs's avatar
mrs committed
4328 4329
    case TYPENAME_TYPE:
      {
4330
	tree ctx = tsubst (TYPE_CONTEXT (t), args, in_decl);
4331 4332
	tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl);
	f = make_typename_type (ctx, f);
mrs's avatar
mrs committed
4333 4334 4335 4336 4337 4338 4339
	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
4340
	(type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
mrs's avatar
mrs committed
4341 4342 4343
      
    case ADDR_EXPR:
      return make_reference_declarator
4344
	(type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
mrs's avatar
mrs committed
4345 4346 4347

    case ARRAY_REF:
      return build_parse_node
4348 4349
	(ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst_expr (TREE_OPERAND (t, 1), args, in_decl));
mrs's avatar
mrs committed
4350 4351

    case CALL_EXPR:
mrs's avatar
merging  
mrs committed
4352
      return make_call_declarator
4353 4354
	(tsubst (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst (TREE_OPERAND (t, 1), args, in_decl),
mrs's avatar
merging  
mrs committed
4355
	 TREE_OPERAND (t, 2),
4356
	 tsubst (TREE_TYPE (t), args, in_decl));
mrs's avatar
mrs committed
4357

mrs's avatar
mrs committed
4358 4359
    case SCOPE_REF:
      return build_parse_node
4360 4361
	(TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst (TREE_OPERAND (t, 1), args, in_decl));
mrs's avatar
mrs committed
4362

mrs's avatar
mrs committed
4363
    default:
mrs's avatar
mrs committed
4364
      sorry ("use of `%s' in template",
mrs's avatar
mrs committed
4365 4366 4367 4368 4369
	     tree_code_name [(int) TREE_CODE (t)]);
      return error_mark_node;
    }
}

mrs's avatar
mrs committed
4370 4371 4372 4373 4374 4375 4376 4377 4378 4379
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
4380
tree
mrs's avatar
mrs committed
4381
do_poplevel ()
mrs's avatar
mrs committed
4382
{
mrs's avatar
mrs committed
4383
  tree t;
4384
  int saved_warn_unused = 0;
mrs's avatar
mrs committed
4385

4386 4387 4388 4389 4390
  if (processing_template_decl)
    {
      saved_warn_unused = warn_unused;
      warn_unused = 0;
    }
4391
  expand_end_bindings (getdecls (), kept_level_p (), 0);
4392 4393
  if (processing_template_decl)
    warn_unused = saved_warn_unused;
mrs's avatar
mrs committed
4394 4395 4396 4397
  t = poplevel (kept_level_p (), 1, 0);
  pop_momentary ();
  return t;
}
mrs's avatar
mrs committed
4398

4399 4400 4401 4402
/* 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
4403
tree
4404
tsubst_copy (t, args, in_decl)
4405
     tree t, args;
mrs's avatar
mrs committed
4406 4407 4408
     tree in_decl;
{
  enum tree_code code;
mrs's avatar
mrs committed
4409

mrs's avatar
mrs committed
4410 4411 4412 4413
  if (t == NULL_TREE || t == error_mark_node)
    return t;

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

mrs's avatar
mrs committed
4415 4416 4417 4418 4419 4420 4421 4422 4423
  switch (code)
    {
    case PARM_DECL:
      return do_identifier (DECL_NAME (t), 0);

    case CONST_DECL:
    case FIELD_DECL:
      if (DECL_CONTEXT (t))
	{
4424 4425
	  tree ctx;
	  if (TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
mrs's avatar
mrs committed
4426
	    return lookup_name (DECL_NAME (t), 0);
4427 4428 4429

	  ctx = tsubst (DECL_CONTEXT (t), args, in_decl);
	  if (ctx != DECL_CONTEXT (t))
mrs's avatar
mrs committed
4430 4431 4432 4433 4434 4435 4436
	    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))
4437
	t = tsubst (t, args, in_decl);
mrs's avatar
mrs committed
4438 4439 4440
      mark_used (t);
      return t;

4441 4442
    case TEMPLATE_DECL:
      if (is_member_template (t))
4443
	return tsubst (t, args, in_decl);
4444 4445 4446
      else
	return t;

mrs's avatar
mrs committed
4447 4448 4449 4450 4451 4452 4453
#if 0
    case IDENTIFIER_NODE:
      return do_identifier (t, 0);
#endif
      
    case CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
mrs's avatar
mrs committed
4454 4455 4456
    case CONST_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
mrs's avatar
mrs committed
4457
      return build1
4458 4459
	(code, tsubst (TREE_TYPE (t), args, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
mrs's avatar
mrs committed
4460 4461 4462 4463 4464 4465 4466 4467

    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
4468
    case BIT_NOT_EXPR:
mrs's avatar
mrs committed
4469 4470 4471
    case ADDR_EXPR:
    case CONVERT_EXPR:      /* Unary + */
    case SIZEOF_EXPR:
4472
    case ALIGNOF_EXPR:
mrs's avatar
mrs committed
4473
    case ARROW_EXPR:
mrs's avatar
mrs committed
4474
    case THROW_EXPR:
mrs's avatar
mrs committed
4475
    case TYPEID_EXPR:
mrs's avatar
mrs committed
4476 4477
      return build1
	(code, NULL_TREE,
4478
	 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
mrs's avatar
mrs committed
4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516

    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
4517 4518
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
mrs's avatar
mrs committed
4519 4520 4521 4522

    case CALL_EXPR:
      {
	tree fn = TREE_OPERAND (t, 0);
jason's avatar
jason committed
4523
	if (is_overloaded_fn (fn))
4524
	  fn = tsubst_copy (get_first_fn (fn), args, in_decl);
mrs's avatar
mrs committed
4525
	else
jason's avatar
jason committed
4526
	  /* Sometimes FN is a LOOKUP_EXPR.  */
4527
	  fn = tsubst_copy (fn, args, in_decl);
mrs's avatar
mrs committed
4528
	return build_nt
4529
	  (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
mrs's avatar
mrs committed
4530 4531 4532 4533 4534 4535 4536 4537
	   NULL_TREE);
      }

    case METHOD_CALL_EXPR:
      {
	tree name = TREE_OPERAND (t, 0);
	if (TREE_CODE (name) == BIT_NOT_EXPR)
	  {
4538
	    name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
mrs's avatar
mrs committed
4539
	    name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
mrs's avatar
mrs committed
4540 4541 4542 4543
	  }
	else if (TREE_CODE (name) == SCOPE_REF
		 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
	  {
4544
	    tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
mrs's avatar
mrs committed
4545
	    name = TREE_OPERAND (name, 1);
4546
	    name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
4547 4548 4549
	    if (TREE_CODE (name) != IDENTIFIER_NODE)
	      name = TYPE_MAIN_VARIANT (name);
	    name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
mrs's avatar
mrs committed
4550 4551 4552
	    name = build_nt (SCOPE_REF, base, name);
	  }
	else
4553
	  name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
mrs's avatar
mrs committed
4554
	return build_nt
4555 4556
	  (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
4557 4558 4559
	   NULL_TREE);
      }

4560
    case BIND_EXPR:
mrs's avatar
mrs committed
4561 4562
    case COND_EXPR:
    case MODOP_EXPR:
4563 4564
      {
	tree r = build_nt
4565 4566 4567
	  (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));
4568 4569 4570 4571 4572 4573 4574 4575 4576 4577

	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.  */ 
	    tree rtl_exp = expand_start_stmt_expr();
4578
	    tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl);
4579 4580
	    rtl_exp = expand_end_stmt_expr (rtl_exp);
	    TREE_SIDE_EFFECTS (rtl_exp) = 1;
4581 4582 4583
	    r = build (BIND_EXPR, TREE_TYPE (rtl_exp), 
		       NULL_TREE, rtl_exp, block);
	    delete_block (block);
4584 4585 4586 4587
	  }

	return r;
      }
mrs's avatar
mrs committed
4588 4589 4590 4591

    case NEW_EXPR:
      {
	tree r = build_nt
4592 4593 4594
	(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
4595 4596 4597 4598 4599 4600 4601
	NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
	return r;
      }

    case DELETE_EXPR:
      {
	tree r = build_nt
4602 4603
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
mrs's avatar
mrs committed
4604 4605 4606 4607 4608
	DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
	DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
	return r;
      }

4609 4610
    case TEMPLATE_ID_EXPR:
      {
4611
        /* Substituted template arguments */
4612
	tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl);
4613 4614 4615 4616 4617
	tree chain;
	for (chain = targs; chain; chain = TREE_CHAIN (chain))
	  TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));

	return lookup_template_function
4618
	  (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs);
4619 4620
      }

mrs's avatar
mrs committed
4621 4622 4623 4624 4625 4626 4627 4628 4629
    case TREE_LIST:
      {
	tree purpose, value, chain;

	if (t == void_list_node)
	  return t;

	purpose = TREE_PURPOSE (t);
	if (purpose)
4630
	  purpose = tsubst_copy (purpose, args, in_decl);
mrs's avatar
mrs committed
4631 4632
	value = TREE_VALUE (t);
	if (value)
4633
	  value = tsubst_copy (value, args, in_decl);
mrs's avatar
mrs committed
4634 4635
	chain = TREE_CHAIN (t);
	if (chain && chain != void_type_node)
4636
	  chain = tsubst_copy (chain, args, in_decl);
mrs's avatar
mrs committed
4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648
	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:
4649
    case TEMPLATE_TEMPLATE_PARM:
jason's avatar
jason committed
4650
    case TEMPLATE_PARM_INDEX:
mrs's avatar
mrs committed
4651 4652 4653 4654 4655 4656 4657
    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
4658
    case TYPE_DECL:
4659
      return tsubst (t, args, in_decl);
mrs's avatar
mrs committed
4660

mrs's avatar
mrs committed
4661 4662 4663
    case IDENTIFIER_NODE:
      if (IDENTIFIER_TYPENAME_P (t))
	return build_typename_overload
4664
	  (tsubst (TREE_TYPE (t), args, in_decl));
mrs's avatar
mrs committed
4665 4666 4667
      else
	return t;

mrs's avatar
mrs committed
4668 4669
    case CONSTRUCTOR:
      return build
4670 4671
	(CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE,
	 tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl));
mrs's avatar
mrs committed
4672

mrs's avatar
mrs committed
4673 4674 4675 4676 4677
    default:
      return t;
    }
}

4678 4679
/* Like tsubst_copy, but also does semantic processing and RTL expansion.  */

mrs's avatar
mrs committed
4680
tree
4681
tsubst_expr (t, args, in_decl)
4682
     tree t, args;
mrs's avatar
mrs committed
4683 4684 4685 4686 4687
     tree in_decl;
{
  if (t == NULL_TREE || t == error_mark_node)
    return t;

mrs's avatar
mrs committed
4688
  if (processing_template_decl)
4689
    return tsubst_copy (t, args, in_decl);
mrs's avatar
mrs committed
4690 4691

  switch (TREE_CODE (t))
mrs's avatar
mrs committed
4692
    {
mrs's avatar
mrs committed
4693 4694
    case RETURN_STMT:
      lineno = TREE_COMPLEXITY (t);
4695 4696
      finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
				       args, in_decl));
mrs's avatar
mrs committed
4697 4698 4699 4700
      break;

    case EXPR_STMT:
      lineno = TREE_COMPLEXITY (t);
4701 4702
      finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
				     args, in_decl));
mrs's avatar
mrs committed
4703 4704 4705 4706 4707
      break;

    case DECL_STMT:
      {
	int i = suspend_momentary ();
mrs's avatar
mrs committed
4708
	tree dcl, init;
mrs's avatar
mrs committed
4709 4710 4711 4712

	lineno = TREE_COMPLEXITY (t);
	emit_line_note (input_filename, lineno);
	dcl = start_decl
4713 4714
	  (tsubst (TREE_OPERAND (t, 0), args, in_decl),
	   tsubst (TREE_OPERAND (t, 1), args, in_decl),
mrs's avatar
merging  
mrs committed
4715
	   TREE_OPERAND (t, 2) != 0);
4716
	init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl);
mrs's avatar
mrs committed
4717
	cp_finish_decl
mrs's avatar
mrs committed
4718
	  (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
mrs's avatar
mrs committed
4719 4720 4721
	resume_momentary (i);
	return dcl;
      }
mrs's avatar
mrs committed
4722

mrs's avatar
mrs committed
4723 4724 4725 4726 4727
    case FOR_STMT:
      {
	tree tmp;
	lineno = TREE_COMPLEXITY (t);

4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738
	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
4739 4740
      }
      break;
mrs's avatar
mrs committed
4741

mrs's avatar
mrs committed
4742 4743 4744
    case WHILE_STMT:
      {
	lineno = TREE_COMPLEXITY (t);
4745 4746 4747 4748 4749 4750
	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
4751 4752
      }
      break;
mrs's avatar
mrs committed
4753

mrs's avatar
mrs committed
4754 4755 4756
    case DO_STMT:
      {
	lineno = TREE_COMPLEXITY (t);
4757 4758 4759 4760 4761 4762
	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
4763 4764
      }
      break;
mrs's avatar
mrs committed
4765

mrs's avatar
mrs committed
4766
    case IF_STMT:
mrs's avatar
mrs committed
4767
      {
mrs's avatar
mrs committed
4768 4769 4770
	tree tmp;

	lineno = TREE_COMPLEXITY (t);
4771 4772 4773 4774
	begin_if_stmt ();
	finish_if_stmt_cond (tsubst_expr (IF_COND (t),
					  args, in_decl),
			     NULL_TREE);
mrs's avatar
mrs committed
4775

4776
	if (tmp = THEN_CLAUSE (t), tmp)
mrs's avatar
mrs committed
4777
	  {
4778
	    tsubst_expr (tmp, args, in_decl);
4779
	    finish_then_clause (NULL_TREE);
mrs's avatar
mrs committed
4780 4781
	  }

4782 4783 4784 4785 4786 4787
	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
4788

4789
	finish_if_stmt ();
mrs's avatar
mrs committed
4790
      }
mrs's avatar
mrs committed
4791
      break;
mrs's avatar
mrs committed
4792

mrs's avatar
mrs committed
4793 4794
    case COMPOUND_STMT:
      {
4795
	tree substmt;
mrs's avatar
mrs committed
4796

mrs's avatar
mrs committed
4797
	lineno = TREE_COMPLEXITY (t);
4798 4799 4800 4801
	begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
	for (substmt = COMPOUND_BODY (t); 
	     substmt != NULL_TREE;
	     substmt = TREE_CHAIN (substmt))
4802
	  tsubst_expr (substmt, args, in_decl);
4803 4804
	return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), 
				     NULL_TREE);
mrs's avatar
mrs committed
4805 4806
      }
      break;
mrs's avatar
mrs committed
4807

mrs's avatar
mrs committed
4808 4809
    case BREAK_STMT:
      lineno = TREE_COMPLEXITY (t);
4810
      finish_break_stmt ();
mrs's avatar
mrs committed
4811
      break;
mrs's avatar
mrs committed
4812

mrs's avatar
mrs committed
4813 4814
    case CONTINUE_STMT:
      lineno = TREE_COMPLEXITY (t);
4815
      finish_continue_stmt ();
mrs's avatar
mrs committed
4816 4817
      break;

mrs's avatar
mrs committed
4818 4819 4820 4821 4822
    case SWITCH_STMT:
      {
	tree val, tmp;

	lineno = TREE_COMPLEXITY (t);
4823 4824 4825
	begin_switch_stmt ();
	val = tsubst_expr (SWITCH_COND (t), args, in_decl);
	finish_switch_cond (val);
mrs's avatar
mrs committed
4826 4827
	
	if (tmp = TREE_OPERAND (t, 1), tmp)
4828
	  tsubst_expr (tmp, args, in_decl);
mrs's avatar
mrs committed
4829

4830
	finish_switch_stmt (val, NULL_TREE);
mrs's avatar
mrs committed
4831 4832 4833 4834
      }
      break;

    case CASE_LABEL:
4835 4836
      finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl),
			 tsubst_expr (CASE_HIGH (t), args, in_decl));
mrs's avatar
mrs committed
4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847
      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);
4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858
      finish_goto_stmt (tsubst_expr (GOTO_DESTINATION (t),
				     args, in_decl));
      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
4859
      break;
mrs's avatar
mrs committed
4860 4861 4862

    case TRY_BLOCK:
      lineno = TREE_COMPLEXITY (t);
4863 4864 4865
      begin_try_block ();
      tsubst_expr (TRY_STMTS (t), args, in_decl);
      finish_try_block (NULL_TREE);
mrs's avatar
mrs committed
4866
      {
4867
	tree handler = TRY_HANDLERS (t);
mrs's avatar
mrs committed
4868
	for (; handler; handler = TREE_CHAIN (handler))
4869
	  tsubst_expr (handler, args, in_decl);
mrs's avatar
mrs committed
4870
      }
4871
      finish_handler_sequence (NULL_TREE);
mrs's avatar
mrs committed
4872 4873 4874 4875
      break;

    case HANDLER:
      lineno = TREE_COMPLEXITY (t);
4876 4877
      begin_handler ();
      if (HANDLER_PARMS (t))
mrs's avatar
mrs committed
4878
	{
4879
	  tree d = HANDLER_PARMS (t);
mrs's avatar
mrs committed
4880
	  expand_start_catch_block
4881 4882
	    (tsubst (TREE_OPERAND (d, 1), args, in_decl),
	     tsubst (TREE_OPERAND (d, 0), args, in_decl));
mrs's avatar
mrs committed
4883 4884 4885
	}
      else
	expand_start_catch_block (NULL_TREE, NULL_TREE);
4886 4887 4888
      finish_handler_parms (NULL_TREE);
      tsubst_expr (HANDLER_BODY (t), args, in_decl);
      finish_handler (NULL_TREE);
mrs's avatar
mrs committed
4889 4890
      break;

mrs's avatar
mrs committed
4891 4892 4893 4894
    case TAG_DEFN:
      lineno = TREE_COMPLEXITY (t);
      t = TREE_TYPE (t);
      if (TREE_CODE (t) == ENUMERAL_TYPE)
4895
	tsubst_enum (t, args, NULL);
mrs's avatar
mrs committed
4896 4897
      break;

mrs's avatar
mrs committed
4898
    default:
4899
      return build_expr_from_tree (tsubst_copy (t, args, in_decl));
mrs's avatar
mrs committed
4900 4901
    }
  return NULL_TREE;
mrs's avatar
mrs committed
4902 4903
}

mrs's avatar
mrs committed
4904 4905
tree
instantiate_template (tmpl, targ_ptr)
4906
     tree tmpl, targ_ptr;
mrs's avatar
mrs committed
4907
{
mrs's avatar
mrs committed
4908 4909 4910 4911 4912
  tree fndecl;
  int i, len;
  struct obstack *old_fmp_obstack;
  extern struct obstack *function_maybepermanent_obstack;

4913 4914
  my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);

jason's avatar
jason committed
4915 4916
  /* FIXME this won't work with member templates; we only have one level
     of args here.  */
4917 4918
  if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    {
4919 4920
      /* Check to see if we already have this specialization.  */
      tree spec = retrieve_specialization (tmpl, targ_ptr);
4921
      
4922 4923
      if (spec != NULL_TREE)
	return spec;
4924 4925
    }

mrs's avatar
mrs committed
4926 4927 4928
  push_obstacks (&permanent_obstack, &permanent_obstack);
  old_fmp_obstack = function_maybepermanent_obstack;
  function_maybepermanent_obstack = &permanent_obstack;
mrs's avatar
mrs committed
4929

4930
  len = DECL_NTPARMS (tmpl);
mrs's avatar
mrs committed
4931

mrs's avatar
mrs committed
4932 4933
  i = len;
  while (i--)
mrs's avatar
mrs committed
4934
    {
4935
      tree t = TREE_VEC_ELT (targ_ptr, i);
mrs's avatar
mrs committed
4936 4937 4938
      if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
	{
	  tree nt = target_type (t);
mrs's avatar
mrs committed
4939
	  if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
mrs's avatar
mrs committed
4940 4941 4942 4943 4944 4945 4946
	    {
	      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;
	    }
	}
4947
      TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
mrs's avatar
mrs committed
4948
    }
4949
  targ_ptr = copy_to_permanent (targ_ptr);
mrs's avatar
mrs committed
4950

mrs's avatar
mrs committed
4951
  /* substitute template parameters */
4952
  fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, tmpl);
mrs's avatar
mrs committed
4953

mrs's avatar
mrs committed
4954 4955 4956
  if (flag_external_templates)
    add_pending_template (fndecl);

mrs's avatar
mrs committed
4957 4958 4959
 out:
  function_maybepermanent_obstack = old_fmp_obstack;
  pop_obstacks ();
mrs's avatar
mrs committed
4960

mrs's avatar
mrs committed
4961
  return fndecl;
mrs's avatar
mrs committed
4962
}
mrs's avatar
mrs committed
4963 4964

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

void
mrs's avatar
mrs committed
4967 4968
overload_template_name (type)
     tree type;
mrs's avatar
mrs committed
4969
{
mrs's avatar
mrs committed
4970 4971
  tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
  tree decl;
mrs's avatar
mrs committed
4972

mrs's avatar
mrs committed
4973 4974 4975
  if (IDENTIFIER_CLASS_VALUE (id)
      && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
    return;
mrs's avatar
mrs committed
4976

mrs's avatar
mrs committed
4977 4978 4979
  decl = build_decl (TYPE_DECL, id, type);
  SET_DECL_ARTIFICIAL (decl);
  pushdecl_class_level (decl);
mrs's avatar
mrs committed
4980 4981
}

4982

law's avatar
law committed
4983
/* Like type_unification but designed specially to handle conversion
4984 4985
   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.  */
4986 4987

int
4988 4989
fn_type_unification (fn, explicit_targs, targs, args, return_type,
		     strict, extra_fn_arg)
4990
     tree fn, explicit_targs, targs, args, return_type;
4991
     int strict;
4992
     tree extra_fn_arg;
4993
{
4994
  int i;
4995 4996 4997 4998 4999 5000 5001 5002 5003
  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.  */
5004
      fn_arg_types = scratch_tree_cons (NULL_TREE, 
5005 5006
					TREE_TYPE (TREE_TYPE (fn)),
					fn_arg_types);
5007
      decl_arg_types = scratch_tree_cons (NULL_TREE,
5008 5009
					  return_type,
					  decl_arg_types);
5010 5011
    }

5012 5013 5014 5015
  if (extra_fn_arg != NULL_TREE)
    fn_arg_types = scratch_tree_cons (NULL_TREE, extra_fn_arg,
				      fn_arg_types); 

5016 5017 5018 5019
  i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn), 
			&TREE_VEC_ELT (targs, 0), 
			fn_arg_types,
			decl_arg_types,
5020
			explicit_targs,
5021
			strict, 0);
5022 5023 5024 5025 5026

  return i;
}


mrs's avatar
mrs committed
5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044
/* 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
5045 5046 5047 5048
   out targs and don't fail on an incomplete match.

   If STRICT is 1, the match must be exact (for casts of overloaded
   addresses, explicit instantiation, and more_specialized).  */
mrs's avatar
mrs committed
5049 5050

int
5051
type_unification (tparms, targs, parms, args, targs_in,
5052 5053
		  strict, allow_incomplete)
     tree tparms, *targs, parms, args, targs_in;
5054
     int strict, allow_incomplete;
5055 5056
{
  int ntparms = TREE_VEC_LENGTH (tparms);
5057
  tree arg;
5058 5059 5060 5061 5062
  int i;
  int r;

  bzero ((char *) targs, sizeof (tree) * ntparms);

5063 5064 5065 5066
  if (targs_in != NULL_TREE)
    {
      tree arg_vec;
      arg_vec = coerce_template_parms (tparms, targs_in, NULL_TREE, 0,
5067
				       0, 0);
5068 5069 5070

      if (arg_vec == error_mark_node)
	return 1;
5071

5072 5073 5074 5075 5076 5077 5078 5079 5080 5081
      for (i = 0; 
	   i < TREE_VEC_LENGTH (arg_vec) 
	     && TREE_VEC_ELT (arg_vec, i) != NULL_TREE; 
	   ++i)
	/* Insert the template argument.  It is encoded as the operands
	   of NOP_EXPRs so that unify can tell that it is an explicit
	   arguments.  */
	targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VEC_ELT (arg_vec, i));
    }
  
5082
  r = type_unification_real (tparms, targs, parms, args, 0,
5083 5084
			     strict, allow_incomplete); 

5085 5086 5087
  for (i = 0, arg = targs_in; 
       arg != NULL_TREE; 
       arg = TREE_CHAIN (arg), ++i)
5088 5089 5090 5091 5092 5093 5094
    if (TREE_CODE (targs[i]) == NOP_EXPR)
      targs[i] = TREE_OPERAND (targs[i], 0);

  return r;
}


5095
static int
5096
type_unification_real (tparms, targs, parms, args, subr,
5097
		       strict, allow_incomplete)
mrs's avatar
mrs committed
5098
     tree tparms, *targs, parms, args;
5099
     int subr, strict, allow_incomplete;
mrs's avatar
mrs committed
5100 5101 5102 5103 5104 5105
{
  tree parm, arg;
  int i;
  int ntparms = TREE_VEC_LENGTH (tparms);

  my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
5106 5107
  my_friendly_assert (parms == NULL_TREE 
		      || TREE_CODE (parms) == TREE_LIST, 290);
mrs's avatar
mrs committed
5108
  /* ARGS could be NULL (via a call from parse.y to
mrs's avatar
mrs committed
5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127
     build_x_function_call).  */
  if (args)
    my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
  my_friendly_assert (ntparms > 0, 292);

  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
5128

5129 5130 5131 5132 5133
      /* 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
5134
	{
5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150
	  tree type;

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

	  if (strict)
	    {
	      if (comptypes (parm, type, 1))
		continue;
	    }
	  else
5151 5152 5153 5154
	    /* It might work; we shouldn't check now, because we might
	       get into infinite recursion.  Overload resolution will
	       handle it.  */
	    continue;
5155

mrs's avatar
mrs committed
5156 5157 5158
	  return 1;
	}
	
mrs's avatar
mrs committed
5159 5160 5161 5162 5163 5164 5165 5166 5167
#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);
mrs's avatar
mrs committed
5168 5169 5170 5171
	  if (TREE_CODE (arg) == TREE_LIST
	      && TREE_TYPE (arg) == unknown_type_node
	      && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
	    {
5172
	      int ntparms;
mrs's avatar
mrs committed
5173 5174 5175 5176
	      tree *targs;

	      /* Have to back unify here */
	      arg = TREE_VALUE (arg);
5177
	      ntparms = DECL_NTPARMS (arg);
mrs's avatar
mrs committed
5178
	      targs = (tree *) alloca (sizeof (tree) * ntparms);
5179
	      parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
5180 5181 5182 5183
	      return 
		type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg), 
				  targs,
				  TYPE_ARG_TYPES (TREE_TYPE (arg)),
5184
				  parm, NULL_TREE, strict,
5185
				  allow_incomplete); 
mrs's avatar
mrs committed
5186
	    }
mrs's avatar
mrs committed
5187 5188 5189
	  arg = TREE_TYPE (arg);
	}
#endif
5190 5191 5192 5193 5194 5195
      if (! flag_ansi && arg == TREE_TYPE (null_node))
	{
	  warning ("using type void* for NULL");
	  arg = ptr_type_node;
	}

mrs's avatar
mrs committed
5196
      if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
mrs's avatar
mrs committed
5197 5198
	arg = TREE_TYPE (arg);

mrs's avatar
mrs committed
5199
      if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
5200 5201 5202 5203 5204 5205 5206 5207 5208
	{
	  if (TREE_CODE (arg) == FUNCTION_TYPE
	      || TREE_CODE (arg) == METHOD_TYPE)
	    arg = build_pointer_type (arg);
	  else if (TREE_CODE (arg) == ARRAY_TYPE)
	    arg = build_pointer_type (TREE_TYPE (arg));
	  else
	    arg = TYPE_MAIN_VARIANT (arg);
	}
mrs's avatar
mrs committed
5209

5210
      switch (unify (tparms, targs, ntparms, parm, arg, strict))
mrs's avatar
mrs committed
5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230
	{
	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++)
      if (!targs[i])
	{
5231 5232
	  if (!allow_incomplete)
	    error ("incomplete type unification");
mrs's avatar
mrs committed
5233 5234 5235 5236 5237 5238
	  return 2;
	}
  return 0;
}

/* Tail recursion is your friend.  */
mrs's avatar
mrs committed
5239

mrs's avatar
mrs committed
5240
static int
5241
unify (tparms, targs, ntparms, parm, arg, strict)
mrs's avatar
mrs committed
5242
     tree tparms, *targs, parm, arg;
5243
     int ntparms, strict;
mrs's avatar
mrs committed
5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260
{
  int idx;

  /* 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;
  if (arg == parm)
    return 0;

5261 5262 5263 5264 5265 5266 5267
  /* We can't remove cv-quals when strict.  */
  if (strict && TREE_CODE (arg) == TREE_CODE (parm)
      && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
      && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
	  || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
    return 1;

mrs's avatar
mrs committed
5268 5269
  switch (TREE_CODE (parm))
    {
5270 5271 5272 5273 5274 5275
    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
5276 5277
    case TEMPLATE_TYPE_PARM:
      idx = TEMPLATE_TYPE_IDX (parm);
5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288
      /* Check for mixed types and values.  */
      if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
	return 1;

      if (!strict && targs[idx] != NULL_TREE && 
	  TREE_CODE (targs[idx]) == NOP_EXPR)
	/* 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;

mrs's avatar
mrs committed
5289 5290 5291
      if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
		     || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
	return 1;
mrs's avatar
mrs committed
5292
#if 0
mrs's avatar
mrs committed
5293 5294 5295 5296 5297 5298 5299
      /* Template type parameters cannot contain cv-quals; i.e.
         template <class T> void f (T& a, T& b) will not generate
	 void f (const int& a, const int& b).  */
      if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
	  || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
	return 1;
      arg = TYPE_MAIN_VARIANT (arg);
mrs's avatar
mrs committed
5300 5301 5302 5303 5304 5305 5306
#else
      {
	int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
	int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
	arg = cp_build_type_variant (arg, constp, volatilep);
      }
#endif
mrs's avatar
mrs committed
5307
      /* Simple cases: Value already set, does match or doesn't.  */
5308 5309 5310 5311
      if (targs[idx] == arg 
	  || (targs[idx] 
	      && TREE_CODE (targs[idx]) == NOP_EXPR 
	      && TREE_OPERAND (targs[idx], 0) == arg))
mrs's avatar
mrs committed
5312 5313 5314 5315 5316
	return 0;
      else if (targs[idx])
	return 1;
      targs[idx] = arg;
      return 0;
5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345

    case TEMPLATE_TEMPLATE_PARM:
      idx = TEMPLATE_TYPE_IDX (parm);
      /* Check for mixed types and values.  */
      if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TEMPLATE_DECL)
	return 1;

      if (!strict && targs[idx] != NULL_TREE && 
	  TREE_CODE (targs[idx]) == NOP_EXPR)
	/* 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;

      if (CLASSTYPE_TEMPLATE_INFO (parm))
	{
	  /* We arrive here when PARM does not involve template 
	     specialization.  */

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

	  {
	    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));
5346
	    int i;
5347 5348 5349 5350 5351 5352 5353 5354

	    /* 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.  */

5355
	    if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1, 0)
5356 5357 5358 5359 5360 5361 5362 5363 5364
		== error_mark_node)
	      return 1;
	  
	    /* 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
jason's avatar
jason committed
5365
		    && TREE_CODE (t) != TEMPLATE_PARM_INDEX)
5366 5367 5368 5369 5370
		  continue;

		/* This argument can be deduced.  */

		if (unify (tparms, targs, ntparms, t, 
5371
			   TREE_VEC_ELT (argvec, i), strict))
5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388
		  return 1;
	      }
	  }
	  arg = CLASSTYPE_TI_TEMPLATE (arg);
	}

      /* Simple cases: Value already set, does match or doesn't.  */
      if (targs[idx] == arg 
	  || (targs[idx] 
	      && TREE_CODE (targs[idx]) == NOP_EXPR 
	      && TREE_OPERAND (targs[idx], 0) == arg))
	return 0;
      else if (targs[idx])
	return 1;
      targs[idx] = arg;
      return 0;

jason's avatar
jason committed
5389 5390
    case TEMPLATE_PARM_INDEX:
      idx = TEMPLATE_PARM_IDX (parm);
mrs's avatar
mrs committed
5391
      if (targs[idx])
mrs's avatar
mrs committed
5392
	{
mrs's avatar
mrs committed
5393 5394 5395 5396 5397 5398 5399
	  int i = cp_tree_equal (targs[idx], arg);
	  if (i == 1)
	    return 0;
	  else if (i == 0)
	    return 1;
	  else
	    my_friendly_abort (42);
mrs's avatar
mrs committed
5400 5401 5402 5403 5404 5405
	}

      targs[idx] = copy_to_permanent (arg);
      return 0;

    case POINTER_TYPE:
mrs's avatar
mrs committed
5406 5407
      if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
	return unify (tparms, targs, ntparms, parm,
5408
		      TYPE_PTRMEMFUNC_FN_TYPE (arg), strict);
mrs's avatar
mrs committed
5409

mrs's avatar
mrs committed
5410 5411 5412
      if (TREE_CODE (arg) != POINTER_TYPE)
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
5413
		    strict);
mrs's avatar
mrs committed
5414 5415

    case REFERENCE_TYPE:
mrs's avatar
mrs committed
5416 5417
      if (TREE_CODE (arg) == REFERENCE_TYPE)
	arg = TREE_TYPE (arg);
mrs's avatar
mrs committed
5418
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
5419
		    strict);
mrs's avatar
mrs committed
5420 5421 5422 5423

    case ARRAY_TYPE:
      if (TREE_CODE (arg) != ARRAY_TYPE)
	return 1;
5424 5425 5426 5427 5428
      if ((TYPE_DOMAIN (parm) == NULL_TREE)
	  != (TYPE_DOMAIN (arg) == NULL_TREE))
	return 1;
      if (TYPE_DOMAIN (parm) != NULL_TREE
	  && unify (tparms, targs, ntparms, TYPE_DOMAIN (parm),
5429
		    TYPE_DOMAIN (arg), strict) != 0)
mrs's avatar
mrs committed
5430 5431
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
5432
		    strict);
mrs's avatar
mrs committed
5433 5434

    case REAL_TYPE:
mrs's avatar
mrs committed
5435
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
5436
    case INTEGER_TYPE:
brendan's avatar
brendan committed
5437
    case BOOLEAN_TYPE:
5438
    case VOID_TYPE:
mrs's avatar
mrs committed
5439 5440 5441 5442
      if (TREE_CODE (arg) != TREE_CODE (parm))
	return 1;

      if (TREE_CODE (parm) == INTEGER_TYPE)
mrs's avatar
mrs committed
5443 5444
	{
	  if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
mrs's avatar
mrs committed
5445
	      && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
5446
			TYPE_MIN_VALUE (arg), strict))
mrs's avatar
mrs committed
5447 5448
	    return 1;
	  if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
mrs's avatar
mrs committed
5449
	      && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
5450
			TYPE_MAX_VALUE (arg), strict))
mrs's avatar
mrs committed
5451 5452
	    return 1;
	}
5453 5454 5455 5456
      else if (TREE_CODE (parm) == REAL_TYPE
	       && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm))
	return 1;

mrs's avatar
mrs committed
5457 5458 5459 5460 5461
      /* 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
5462
      /* Type INTEGER_CST can come from ordinary constant template args.  */
mrs's avatar
mrs committed
5463
    case INTEGER_CST:
mrs's avatar
mrs committed
5464 5465 5466
      while (TREE_CODE (arg) == NOP_EXPR)
	arg = TREE_OPERAND (arg, 0);

mrs's avatar
mrs committed
5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477
      if (TREE_CODE (arg) != INTEGER_CST)
	return 1;
      return !tree_int_cst_equal (parm, arg);

    case MINUS_EXPR:
      {
	tree t1, t2;
	t1 = TREE_OPERAND (parm, 0);
	t2 = TREE_OPERAND (parm, 1);
	return unify (tparms, targs, ntparms, t1,
		      fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
5478
		      strict);
mrs's avatar
mrs committed
5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490
      }

    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--)
	  if (unify (tparms, targs, ntparms,
		     TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
5491
		     strict))
mrs's avatar
mrs committed
5492 5493 5494 5495 5496
	    return 1;
	return 0;
      }

    case RECORD_TYPE:
mrs's avatar
mrs committed
5497
      if (TYPE_PTRMEMFUNC_FLAG (parm))
mrs's avatar
mrs committed
5498
	return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
5499
		      arg, strict);
mrs's avatar
mrs committed
5500

mrs's avatar
mrs committed
5501
      /* Allow trivial conversions.  */
mrs's avatar
mrs committed
5502
      if (TREE_CODE (arg) != RECORD_TYPE
mrs's avatar
mrs committed
5503 5504 5505
	  || TYPE_READONLY (parm) < TYPE_READONLY (arg)
	  || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
	return 1;
mrs's avatar
mrs committed
5506

mrs's avatar
mrs committed
5507
      if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
mrs's avatar
mrs committed
5508
	{
mrs's avatar
mrs committed
5509
	  tree t = NULL_TREE;
5510
	  if (! strict)
mrs's avatar
mrs committed
5511
	    t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
mrs's avatar
mrs committed
5512 5513 5514
	  else if
	    (CLASSTYPE_TEMPLATE_INFO (arg)
	     && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
mrs's avatar
mrs committed
5515 5516
	    t = arg;
	  if (! t || t == error_mark_node)
mrs's avatar
mrs committed
5517
	    return 1;
mrs's avatar
mrs committed
5518

mrs's avatar
mrs committed
5519
	  return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
5520
			CLASSTYPE_TI_ARGS (t), strict);
mrs's avatar
mrs committed
5521 5522 5523
	}
      else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
	return 1;
mrs's avatar
mrs committed
5524
      return 0;
mrs's avatar
mrs committed
5525 5526 5527 5528 5529 5530 5531 5532 5533 5534

    case METHOD_TYPE:
      if (TREE_CODE (arg) != METHOD_TYPE)
	return 1;
      goto check_args;

    case FUNCTION_TYPE:
      if (TREE_CODE (arg) != FUNCTION_TYPE)
	return 1;
     check_args:
mrs's avatar
mrs committed
5535
      if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
5536
		 TREE_TYPE (arg), strict))
mrs's avatar
mrs committed
5537
	return 1;
5538
      return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
5539
				    TYPE_ARG_TYPES (arg), 1, 
5540
				    strict, 0);
mrs's avatar
mrs committed
5541 5542 5543 5544 5545

    case OFFSET_TYPE:
      if (TREE_CODE (arg) != OFFSET_TYPE)
	return 1;
      if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
5546
		 TYPE_OFFSET_BASETYPE (arg), strict))
mrs's avatar
mrs committed
5547 5548
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm),
5549
		    TREE_TYPE (arg), strict);
mrs's avatar
mrs committed
5550

jason's avatar
merge  
jason committed
5551 5552 5553 5554 5555
    case CONST_DECL:
      if (arg != decl_constant_value (parm))
	return 1;
      return 0;

5556 5557 5558 5559
    case TEMPLATE_DECL:
      /* Matched cases are handled by the ARG == PARM test above.  */
      return 1;

mrs's avatar
mrs committed
5560 5561 5562 5563 5564 5565 5566
    default:
      sorry ("use of `%s' in template type unification",
	     tree_code_name [(int) TREE_CODE (parm)]);
      return 1;
    }
}

mrs's avatar
mrs committed
5567
void
mrs's avatar
mrs committed
5568
mark_decl_instantiated (result, extern_p)
mrs's avatar
mrs committed
5569 5570 5571 5572 5573
     tree result;
     int extern_p;
{
  if (DECL_TEMPLATE_INSTANTIATION (result))
    SET_DECL_EXPLICIT_INSTANTIATION (result);
5574 5575 5576 5577 5578

  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
5579 5580 5581 5582 5583

  if (! extern_p)
    {
      DECL_INTERFACE_KNOWN (result) = 1;
      DECL_NOT_REALLY_EXTERN (result) = 1;
5584 5585 5586

      /* For WIN32 we also want to put explicit instantiations in
	 linkonce sections.  */
5587
      if (supports_one_only () && ! SUPPORTS_WEAK && TREE_PUBLIC (result))
5588
	make_decl_one_only (result);
mrs's avatar
mrs committed
5589
    }
mrs's avatar
mrs committed
5590 5591
  else if (TREE_CODE (result) == FUNCTION_DECL)
    mark_inline_for_output (result);
mrs's avatar
mrs committed
5592 5593
}

5594 5595
/* Given two function templates PAT1 and PAT2, and explicit template
   arguments EXPLICIT_ARGS return:
mrs's avatar
mrs committed
5596 5597 5598 5599 5600 5601

   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
5602 5603
more_specialized (pat1, pat2, explicit_args)
     tree pat1, pat2, explicit_args;
mrs's avatar
mrs committed
5604
{
5605
  tree targs;
mrs's avatar
mrs committed
5606
  int winner = 0;
mrs's avatar
mrs committed
5607

5608
  targs = get_bindings (pat1, pat2, explicit_args);
mrs's avatar
mrs committed
5609 5610 5611 5612
  if (targs)
    {
      --winner;
    }
mrs's avatar
mrs committed
5613

5614
  targs = get_bindings (pat2, pat1, explicit_args);
mrs's avatar
mrs committed
5615 5616 5617 5618
  if (targs)
    {
      ++winner;
    }
mrs's avatar
mrs committed
5619

mrs's avatar
mrs committed
5620 5621
  return winner;
}
mrs's avatar
mrs committed
5622

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

mrs's avatar
mrs committed
5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636
   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
5637 5638
    (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
     TREE_PURPOSE (pat2), NULL_TREE);
mrs's avatar
mrs committed
5639 5640 5641 5642
  if (targs)
    --winner;

  targs = get_class_bindings
5643 5644
    (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
     TREE_PURPOSE (pat1), NULL_TREE);
mrs's avatar
mrs committed
5645
  if (targs)
mrs's avatar
mrs committed
5646 5647 5648 5649
    ++winner;

  return winner;
}
mrs's avatar
mrs committed
5650 5651

/* Return the template arguments that will produce the function signature
5652 5653
   DECL from the function template FN, with the explicit template
   arguments EXPLICIT_ARGS.  */
mrs's avatar
mrs committed
5654

5655
tree 
5656 5657
get_bindings (fn, decl, explicit_args)
     tree fn, decl, explicit_args;
mrs's avatar
mrs committed
5658
{
5659
  int ntparms = DECL_NTPARMS (fn);
5660
  tree targs = make_scratch_vec (ntparms);
5661 5662
  tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
  tree extra_fn_arg = NULL_TREE;
5663 5664
  int i;

5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684
  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);
    }

5685
  i = fn_type_unification (fn, explicit_args, targs, 
5686
			   decl_arg_types,
5687
			   TREE_TYPE (TREE_TYPE (decl)),
5688 5689
			   1,
			   extra_fn_arg);
5690

mrs's avatar
mrs committed
5691
  if (i == 0)
5692 5693
    {
      /* Check to see that the resulting return type is also OK.  */
5694
      tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs, NULL_TREE);
5695

jason's avatar
jason committed
5696
      if (!comptypes (t, TREE_TYPE (TREE_TYPE (decl)), 1))
5697 5698 5699 5700 5701 5702
	return NULL_TREE;

      return targs;
    }

  return NULL_TREE;
mrs's avatar
mrs committed
5703 5704
}

mrs's avatar
mrs committed
5705
static tree
5706 5707
get_class_bindings (tparms, parms, args, outer_args)
     tree tparms, parms, args, outer_args;
mrs's avatar
mrs committed
5708
{
5709
  int i, ntparms = TREE_VEC_LENGTH (tparms);
mrs's avatar
mrs committed
5710 5711
  tree vec = make_temp_vec (ntparms);

5712 5713 5714 5715 5716 5717
  if (outer_args)
    {
      tparms = tsubst (tparms, outer_args, NULL_TREE);
      parms = tsubst (parms, outer_args, NULL_TREE);
    }

mrs's avatar
mrs committed
5718 5719 5720
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
    {
      switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
5721
		     TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i), 1))
mrs's avatar
mrs committed
5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737
	{
	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
5738 5739
   produce an instantiation matching DECL, given the explicit template
   arguments EXPLICIT_ARGS.  */
mrs's avatar
mrs committed
5740 5741

tree
5742 5743
most_specialized (fns, decl, explicit_args)
     tree fns, decl, explicit_args;
mrs's avatar
mrs committed
5744
{
5745
  tree fn, champ, args, *p;
mrs's avatar
mrs committed
5746 5747 5748 5749
  int fate;

  for (p = &fns; *p; )
    {
5750
      args = get_bindings (TREE_VALUE (*p), decl, explicit_args);
mrs's avatar
mrs committed
5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766
      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))
    {
5767
      fate = more_specialized (champ, TREE_VALUE (fn), explicit_args);
mrs's avatar
mrs committed
5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783
      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))
    {
5784
      fate = more_specialized (champ, TREE_VALUE (fn), explicit_args);
mrs's avatar
mrs committed
5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795
      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
5796 5797
most_specialized_class (specs, mainargs, outer_args)
     tree specs, mainargs, outer_args;
mrs's avatar
mrs committed
5798 5799 5800 5801 5802 5803
{
  tree list = NULL_TREE, t, args, champ;
  int fate;

  for (t = specs; t; t = TREE_CHAIN (t))
    {
5804 5805
      args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
				 mainargs, outer_args);
mrs's avatar
mrs committed
5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837
      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))
    {
5838
      fate = more_specialized_class (champ, t);
mrs's avatar
mrs committed
5839 5840 5841 5842 5843 5844 5845
      if (fate != 1)
	return error_mark_node;
    }

  return champ;
}

mrs's avatar
mrs committed
5846
/* called from the parser.  */
mrs's avatar
mrs committed
5847

mrs's avatar
mrs committed
5848
void
mrs's avatar
mrs committed
5849
do_decl_instantiation (declspecs, declarator, storage)
mrs's avatar
mrs committed
5850
     tree declspecs, declarator, storage;
mrs's avatar
mrs committed
5851
{
mrs's avatar
merging  
mrs committed
5852
  tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
mrs's avatar
mrs committed
5853
  tree result = NULL_TREE;
mrs's avatar
mrs committed
5854
  int extern_p = 0;
mrs's avatar
mrs committed
5855

mrs's avatar
mrs committed
5856 5857 5858 5859 5860 5861
  if (! DECL_LANG_SPECIFIC (decl))
    {
      cp_error ("explicit instantiation of non-template `%#D'", decl);
      return;
    }

mrs's avatar
mrs committed
5862
  /* If we've already seen this template instance, use it.  */
mrs's avatar
mrs committed
5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873
  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;
    }
5874 5875
  else if (DECL_TEMPLATE_INSTANTIATION (decl))
    result = decl;
5876

mrs's avatar
mrs committed
5877
  if (! result)
mrs's avatar
mrs committed
5878 5879 5880 5881
    {
      cp_error ("no matching template for `%D' found", decl);
      return;
    }
mrs's avatar
mrs committed
5882

mrs's avatar
mrs committed
5883 5884 5885 5886 5887 5888
  if (! DECL_TEMPLATE_INFO (result))
    {
      cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
      return;
    }

mrs's avatar
mrs committed
5889 5890 5891
  if (flag_external_templates)
    return;

mrs's avatar
mrs committed
5892
  if (storage == NULL_TREE)
mrs's avatar
mrs committed
5893
    ;
mrs's avatar
mrs committed
5894 5895
  else if (storage == ridpointers[(int) RID_EXTERN])
    extern_p = 1;
mrs's avatar
mrs committed
5896 5897 5898
  else
    cp_error ("storage class `%D' applied to template instantiation",
	      storage);
mrs's avatar
mrs committed
5899 5900

  mark_decl_instantiated (result, extern_p);
mrs's avatar
mrs committed
5901
  repo_template_instantiated (result, extern_p);
mrs's avatar
mrs committed
5902 5903
  if (! extern_p)
    instantiate_decl (result);
mrs's avatar
mrs committed
5904 5905
}

mrs's avatar
mrs committed
5906 5907 5908 5909 5910 5911
void
mark_class_instantiated (t, extern_p)
     tree t;
     int extern_p;
{
  SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
5912 5913
  SET_CLASSTYPE_INTERFACE_KNOWN (t);
  CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
mrs's avatar
mrs committed
5914 5915 5916 5917 5918 5919 5920 5921
  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
5922

mrs's avatar
mrs committed
5923
void
5924 5925
do_type_instantiation (t, storage)
     tree t, storage;
mrs's avatar
mrs committed
5926
{
mrs's avatar
mrs committed
5927 5928
  int extern_p = 0;
  int nomem_p = 0;
mrs's avatar
mrs committed
5929 5930
  int static_p = 0;

5931 5932 5933 5934 5935 5936 5937 5938 5939
  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
5940
  complete_type (t);
mrs's avatar
mrs committed
5941

mrs's avatar
mrs committed
5942 5943
  /* With -fexternal-templates, explicit instantiations are treated the same
     as implicit ones.  */
mrs's avatar
mrs committed
5944 5945 5946
  if (flag_external_templates)
    return;

mrs's avatar
mrs committed
5947 5948 5949 5950 5951 5952 5953 5954
  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
5955 5956 5957
    /* OK */;
  else if (storage == ridpointers[(int) RID_INLINE])
    nomem_p = 1;
mrs's avatar
mrs committed
5958 5959
  else if (storage == ridpointers[(int) RID_EXTERN])
    extern_p = 1;
mrs's avatar
mrs committed
5960 5961
  else if (storage == ridpointers[(int) RID_STATIC])
    static_p = 1;
mrs's avatar
mrs committed
5962 5963 5964 5965 5966 5967 5968
  else
    {
      cp_error ("storage class `%D' applied to template instantiation",
		storage);
      extern_p = 0;
    }

mrs's avatar
mrs committed
5969
  /* We've already instantiated this.  */
mrs's avatar
mrs committed
5970 5971 5972
  if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
      && extern_p)
    return;
mrs's avatar
mrs committed
5973

mrs's avatar
mrs committed
5974
  if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
mrs's avatar
mrs committed
5975 5976 5977 5978
    {
      mark_class_instantiated (t, extern_p);
      repo_template_instantiated (t, extern_p);
    }
mrs's avatar
mrs committed
5979 5980 5981 5982

  if (nomem_p)
    return;

mrs's avatar
mrs committed
5983
  {
mrs's avatar
mrs committed
5984
    tree tmp;
mrs's avatar
mrs committed
5985 5986 5987

    if (! static_p)
      for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
5988
	if (TREE_CODE (tmp) == FUNCTION_DECL
5989
	    && DECL_TEMPLATE_INSTANTIATION (tmp))
mrs's avatar
mrs committed
5990 5991 5992 5993 5994 5995 5996 5997 5998
	  {
	    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
5999
	{
mrs's avatar
mrs committed
6000
	  mark_decl_instantiated (tmp, extern_p);
mrs's avatar
mrs committed
6001
	  repo_template_instantiated (tmp, extern_p);
mrs's avatar
mrs committed
6002 6003
	  if (! extern_p)
	    instantiate_decl (tmp);
mrs's avatar
mrs committed
6004
	}
mrs's avatar
mrs committed
6005

mrs's avatar
mrs committed
6006
    for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
mrs's avatar
mrs committed
6007 6008
      if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
	do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
mrs's avatar
mrs committed
6009
  }
mrs's avatar
mrs committed
6010
}
mrs's avatar
mrs committed
6011

jason's avatar
jason committed
6012 6013
/* Produce the definition of D, a _DECL generated from a template.  */

mrs's avatar
mrs committed
6014
tree
mrs's avatar
mrs committed
6015 6016
instantiate_decl (d)
     tree d;
mrs's avatar
mrs committed
6017
{
mrs's avatar
mrs committed
6018 6019 6020
  tree ti = DECL_TEMPLATE_INFO (d);
  tree tmpl = TI_TEMPLATE (ti);
  tree args = TI_ARGS (ti);
6021
  tree td, temp;
jason's avatar
jason committed
6022
  tree decl_pattern, code_pattern;
mrs's avatar
mrs committed
6023 6024 6025 6026
  tree save_ti;
  int nested = in_function_p ();
  int d_defined;
  int pattern_defined;
mrs's avatar
mrs committed
6027 6028
  int line = lineno;
  char *file = input_filename;
mrs's avatar
mrs committed
6029

jason's avatar
jason committed
6030 6031 6032 6033 6034 6035 6036 6037 6038
  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
6039
    td = DECL_TI_TEMPLATE (td);
6040

jason's avatar
jason committed
6041 6042 6043 6044 6045
  /* 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);
6046

mrs's avatar
mrs committed
6047 6048 6049
  if (TREE_CODE (d) == FUNCTION_DECL)
    {
      d_defined = (DECL_INITIAL (d) != NULL_TREE);
jason's avatar
jason committed
6050
      pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
mrs's avatar
mrs committed
6051 6052 6053 6054
    }
  else
    {
      d_defined = ! DECL_IN_AGGR_P (d);
jason's avatar
jason committed
6055
      pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
mrs's avatar
mrs committed
6056 6057 6058 6059
    }

  if (d_defined)
    return d;
mrs's avatar
mrs committed
6060

6061 6062
  if (TREE_CODE (d) == FUNCTION_DECL) 
    {
6063 6064 6065 6066 6067
      tree spec = retrieve_specialization (tmpl, args);
      
      if (spec != NULL_TREE 
	  && DECL_TEMPLATE_SPECIALIZATION (spec))
	return spec;
6068 6069
    }

mrs's avatar
mrs committed
6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080
  /* 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);

  /* We need to set up DECL_INITIAL regardless of pattern_defined if the
     variable is a static const initialized in the class body.  */
  if (TREE_CODE (d) == VAR_DECL
jason's avatar
jason committed
6081
      && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern))
mrs's avatar
mrs committed
6082 6083
    {
      pushclass (DECL_CONTEXT (d), 2);
jason's avatar
jason committed
6084
      DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args,
6085
				      tmpl);
6086
      cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, LOOKUP_NORMAL);
mrs's avatar
mrs committed
6087 6088 6089
    }

  if (pattern_defined)
mrs's avatar
mrs committed
6090 6091 6092 6093 6094 6095 6096 6097 6098 6099
    {
      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
6100
	  else if (DECL_INTERFACE_KNOWN (code_pattern))
mrs's avatar
mrs committed
6101 6102
	    {
	      DECL_INTERFACE_KNOWN (d) = 1;
jason's avatar
jason committed
6103
	      DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
mrs's avatar
mrs committed
6104 6105
	    }
	  else
jason's avatar
jason committed
6106
	    warn_if_unknown_interface (code_pattern);
mrs's avatar
mrs committed
6107 6108
	}

mrs's avatar
mrs committed
6109
      if (at_eof)
mrs's avatar
mrs committed
6110 6111 6112
	import_export_decl (d);
    }

6113 6114 6115 6116 6117 6118 6119
  /* 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;

  /* Defer all templates except inline functions used in another function.  */
mrs's avatar
mrs committed
6120
  if (! pattern_defined
6121 6122
      || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
	  && ! at_eof))
mrs's avatar
mrs committed
6123 6124
    {
      add_pending_template (d);
mrs's avatar
mrs committed
6125
      goto out;
mrs's avatar
mrs committed
6126 6127
    }

mrs's avatar
mrs committed
6128 6129 6130
  lineno = DECL_SOURCE_LINE (d);
  input_filename = DECL_SOURCE_FILE (d);

mrs's avatar
mrs committed
6131
  /* Trick tsubst into giving us a new decl in case the template changed.  */
jason's avatar
jason committed
6132 6133
  save_ti = DECL_TEMPLATE_INFO (decl_pattern);
  DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE;
jason's avatar
comment  
jason committed
6134 6135
  /* decl_pattern has all but one level of template parms bound.  Only pass
     in that one level of args.  */
6136
  temp = innermost_args (args, DECL_TEMPLATE_SPECIALIZATION (decl_pattern));
6137
  td = tsubst (decl_pattern, temp, tmpl);
6138
  SET_DECL_IMPLICIT_INSTANTIATION (td);
jason's avatar
jason committed
6139
  DECL_TEMPLATE_INFO (decl_pattern) = save_ti;
mrs's avatar
mrs committed
6140

mrs's avatar
mrs committed
6141 6142
  /* And set up DECL_INITIAL, since tsubst doesn't.  */
  if (TREE_CODE (td) == VAR_DECL)
mrs's avatar
mrs committed
6143 6144
    {
      pushclass (DECL_CONTEXT (d), 2);
jason's avatar
jason committed
6145
      DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args,
6146
				       tmpl);
mrs's avatar
mrs committed
6147 6148
      popclass (1);
    }
mrs's avatar
mrs committed
6149

mrs's avatar
mrs committed
6150
  if (TREE_CODE (d) == FUNCTION_DECL)
6151 6152 6153 6154 6155 6156 6157 6158 6159
    {
      /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
	 new decl.  */ 
      DECL_INITIAL (td) = error_mark_node;

      if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td))
	/* Set up the information about what is being specialized. */
	DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d);
    }
mrs's avatar
mrs committed
6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177
  duplicate_decls (td, d);
  if (TREE_CODE (d) == FUNCTION_DECL)
    DECL_INITIAL (td) = 0;

  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
6178
      tree t = DECL_SAVED_TREE (code_pattern);
mrs's avatar
mrs committed
6179

mrs's avatar
merging  
mrs committed
6180
      start_function (NULL_TREE, d, NULL_TREE, 1);
mrs's avatar
mrs committed
6181 6182
      store_parm_decls ();

mrs's avatar
mrs committed
6183 6184 6185 6186
      if (t && TREE_CODE (t) == RETURN_INIT)
	{
	  store_return_init
	    (TREE_OPERAND (t, 0),
6187
	     tsubst_expr (TREE_OPERAND (t, 1), args, tmpl));
mrs's avatar
mrs committed
6188 6189 6190
	  t = TREE_CHAIN (t);
	}

mrs's avatar
mrs committed
6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201
      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
6202
	 pair of curly braces of a function.  These are needed
mrs's avatar
mrs committed
6203 6204 6205 6206
	 for correct operation of dwarfout.c.  */
      keep_next_level ();

      my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
6207
      tsubst_expr (t, args, tmpl);
mrs's avatar
mrs committed
6208

mrs's avatar
mrs committed
6209 6210 6211
      finish_function (lineno, 0, nested);
    }

mrs's avatar
mrs committed
6212
out:
mrs's avatar
mrs committed
6213 6214 6215
  lineno = line;
  input_filename = file;

mrs's avatar
mrs committed
6216 6217
  pop_from_top_level ();
  pop_tinst_level ();
mrs's avatar
mrs committed
6218 6219 6220

  return d;
}
mrs's avatar
mrs committed
6221 6222 6223 6224 6225 6226 6227

tree
tsubst_chain (t, argvec)
     tree t, argvec;
{
  if (t)
    {
6228
      tree first = tsubst (t, argvec, NULL_TREE);
mrs's avatar
mrs committed
6229 6230 6231 6232
      tree last = first;

      for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
	{
6233
	  tree x = tsubst (t, argvec, NULL_TREE);
mrs's avatar
mrs committed
6234 6235 6236 6237 6238 6239 6240 6241 6242
	  TREE_CHAIN (last) = x;
	  last = x;
	}

      return first;
    }
  return NULL_TREE;
}

mrs's avatar
mrs committed
6243
static tree
mrs's avatar
mrs committed
6244 6245 6246 6247 6248 6249 6250 6251
tsubst_expr_values (t, argvec)
     tree t, argvec;
{
  tree first = NULL_TREE;
  tree *p = &first;

  for (; t; t = TREE_CHAIN (t))
    {
6252 6253
      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
6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267
      *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
6268

6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286

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
6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300
/* 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;

6301
  t = most_specialized (fns, d, NULL_TREE);
mrs's avatar
mrs committed
6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313
  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
6314 6315 6316 6317 6318

/* Instantiate an enumerated type.  Used by instantiate_class_template and
   tsubst_expr.  */

static tree
6319
tsubst_enum (tag, args, field_chain)
6320
     tree tag, args;
6321
     tree * field_chain;
mrs's avatar
mrs committed
6322
{
6323 6324 6325
  extern tree current_local_enum;
  tree prev_local_enum = current_local_enum;

mrs's avatar
mrs committed
6326 6327 6328 6329 6330 6331 6332
  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,
6333
						NULL_TREE));
mrs's avatar
mrs committed
6334 6335 6336 6337 6338 6339
      TREE_CHAIN (elt) = values;
      values = elt;
    }

  finish_enum (newtag, values);

6340
  if (NULL != field_chain)
6341
    *field_chain = grok_enum_decls (NULL_TREE);
6342 6343 6344

  current_local_enum = prev_local_enum;

mrs's avatar
mrs committed
6345 6346
  return newtag;
}