pt.c 123 KB
Newer Older
mrs's avatar
mrs committed
1
/* Handle parameterized types (templates) for GNU C++.
mrs's avatar
mrs committed
2
   Copyright (C) 1992, 93, 94, 95, 1996 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 65
static int template_header_count;

mrs's avatar
mrs committed
66 67 68
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free

69 70 71 72 73 74
static int unify PROTO((tree, tree *, int, tree, tree, int *, int));
static void add_pending_template PROTO((tree));
static int push_tinst_level PROTO((tree));
static tree classtype_mangled_name PROTO((tree));
static char *mangle_class_name_for_template PROTO((char *, tree, tree));
static tree tsubst_expr_values PROTO((tree, tree));
mrs's avatar
mrs committed
75
static int comp_template_args PROTO((tree, tree));
76
static int list_eq PROTO((tree, tree));
mrs's avatar
mrs committed
77
static tree get_class_bindings PROTO((tree, tree, tree));
78
static tree coerce_template_parms PROTO((tree, tree, tree));
79
static tree tsubst_enum	PROTO((tree, tree, int, tree *));
80
static tree add_to_template_args PROTO((tree, tree));
81 82 83 84
static int  type_unification_real PROTO((tree, tree *, tree, tree, int*,
					 int, int, int));
static int processing_explicit_specialization PROTO((int));
static void note_template_header PROTO((int));
85 86 87 88

/* Restore the template parameter context. */

void 
89 90
begin_member_template_processing (decl)
     tree decl;
91
{
92
  tree parms;
93 94
  int i;

95 96
  parms = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));

97 98 99 100
  ++processing_template_decl;
  current_template_parms 
    = tree_cons (build_int_2 (0, processing_template_decl),
		 parms, current_template_parms);
101
  pushlevel (0);
102 103
  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) 
    {
104 105 106
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
      
107 108
      switch (TREE_CODE (parm))
	{
109
	case TYPE_DECL:
110 111
	  pushdecl (parm);
	  break;
112 113 114 115 116 117 118 119 120 121 122

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

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	default:
	  my_friendly_abort (0);
	}
    }
}

/* Undo the effects of begin_member_template_processing. */

void 
end_member_template_processing ()
{
  if (! processing_template_decl)
    return;

  --processing_template_decl;
  current_template_parms = TREE_CHAIN (current_template_parms);
139
  poplevel (0, 0, 0);
140 141 142 143 144 145 146 147 148 149 150
}

/* Returns non-zero iff T is a member template function.  Works if T
   is either a FUNCTION_DECL or a TEMPLATE_DECL.  */

int
is_member_template (t)
     tree t;
{
  int r = 0;

151 152 153 154 155 156
  if (TREE_CODE (t) != FUNCTION_DECL
      && !DECL_FUNCTION_TEMPLATE_P (t))
    /* Anything that isn't a template or a template functon is
       certainly not a member template.  */
    return 0;

157 158 159 160
  if ((DECL_FUNCTION_MEMBER_P (t) 
       && !DECL_TEMPLATE_SPECIALIZATION (t))
      || (TREE_CODE (t) == TEMPLATE_DECL && 
	  DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))))
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    {
      tree tmpl = NULL_TREE;

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

      if (tmpl) 
	{
	  tree parms = DECL_TEMPLATE_PARMS (tmpl);
	  int parm_levels = list_length (parms);
	  int template_class_levels = 0;
	  tree ctx = DECL_CLASS_CONTEXT (t);

	  if (CLASSTYPE_TEMPLATE_INFO (ctx))
	    {
	      tree args;

	      /* Here, we should really count the number of levels
		 deep ctx is, making sure not to count any levels that
		 are just specializations.  Since there are no member
		 template classes yet, we don't have to do all that.  */

	      if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
		template_class_levels = 1;
	      else
		{
		  int i;

		  args = CLASSTYPE_TI_ARGS (ctx);

		  if (args == NULL_TREE)
		    template_class_levels = 1;
		  else 
		    for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
		      if (uses_template_parms (TREE_VEC_ELT (args, i)))
			{
			  template_class_levels++;
			  break;
			}
		}
	    }

	  if (parm_levels > template_class_levels)
	    r = 1;
	}
    }

  return r;
}

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

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

      new_args = make_tree_vec (TREE_VEC_LENGTH (args) - 1);

      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
244 245 246

/* We've got a template header coming up; push to a new level for storing
   the parms.  */
mrs's avatar
mrs committed
247 248 249 250 251

void
begin_template_parm_list ()
{
  pushlevel (0);
mrs's avatar
mrs committed
252
  declare_pseudo_global_level ();
mrs's avatar
mrs committed
253
  ++processing_template_decl;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  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 <>.  */

void 
note_template_header (specialization)
     int specialization;
{
  processing_specialization = specialization;
  template_header_count++;
}


/* Returns non-zero iff a declarator, in which the number of template
   types that appeared was TEMPLATE_COUNT, is an explicit
   specialization.  */

int
processing_explicit_specialization(template_count)
     int template_count;
{
  /* A function declaration is an explicit specialization of a member
     template if all of the following conditions hold:
     
     o There was a template <...> preceeding the declaration.
     o The last template <...> was in fact template <>.
     o The number of template <...>'s preceeding the declaration, less
       the number of template classes with arguments specified used to
       qualify the function name, is 1.

     For example:

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

     The first of these is not a specialization of S<int>::foo() (it
     is instead a specialization of S<T>::foo), while the next two are
     specializations of member template functions.  */

  return processing_specialization 
    && template_header_count > template_count; 
}

/* Returns the template function specialized by TEMPLATE_ID, or
   NULL_TREE if there is none.

   The TEMPLATE_ID is a TEMPLATE_ID_EXPR.  The TYPE is
   the type it has been declared to have.  Return the TEMPLATE_DECL
   that is being specialized, and put the specialization arguments in
   *TARGS.  If no appropriate specialization can be found, NULL_TREE is
   returned, and *TARGS is assigned NULL_TREE.  If complain is
   non-zero, error messages are printed where appropriate.  */
   
tree
determine_explicit_specialization (template_id, type, targs_out,
				   need_member_template,
				   complain)
     tree template_id;
     tree type;
     tree* targs_out;
     int need_member_template;
     int complain;
{
  int i;
  int overloaded;
  tree fns;
  tree matching_fns = NULL_TREE;
  tree name = NULL_TREE;
  tree result;
  tree fn;

  my_friendly_assert (TREE_CODE (template_id) == TEMPLATE_ID_EXPR,
		      0); 
		      
  fns = TREE_OPERAND (template_id, 0);

  overloaded = fns != NULL_TREE && really_overloaded_fn (fns);

  for (fn = (fns != NULL_TREE) ? get_first_fn (fns) : NULL_TREE; 
       fn != NULL_TREE; 
       fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE)
    {
370
      int dummy = 0;
371 372 373 374 375 376 377 378 379 380 381 382
      tree targs;

      if (name == NULL_TREE)
	name = DECL_NAME (fn);

      if (TREE_CODE (fn) != TEMPLATE_DECL
	  || (need_member_template && !is_member_template (fn)))
	continue;

      if (list_length (TREE_OPERAND (template_id, 1)) > DECL_NTPARMS (fn))
	continue;

383
      targs = make_scratch_vec (DECL_NTPARMS (fn));
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 419

      /* We allow incomplete unification here, because we are going to
	 check all the functions. */
      i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
			    &TREE_VEC_ELT (targs, 0),
			    type 
			    ? TYPE_ARG_TYPES (TREE_TYPE (fn)) : NULL_TREE, 
			    type ? TYPE_ARG_TYPES (type) : NULL_TREE,
			    TREE_OPERAND (template_id, 1),
			    &dummy, 1, 1);
      
      if (i == 0) 
	{
	  /* Unification was successful.  See if the return types
	     match. */
	  if (type != NULL_TREE)
	    {
	      tree tmpl_return_type = tsubst (TREE_TYPE (TREE_TYPE (fn)),
					      targs,
					      DECL_NTPARMS (fn),
					      NULL_TREE);
	      
	      if (tmpl_return_type != TREE_TYPE (type))
		{
		  /* Always complain about this.  With ambiguity, some
		     other context, might resolve things.  But, a
		     non-matching return type will always be a
		     problem.  */
		  cp_error ("Return type of explicit specialization of");
		  cp_error ("`%D' is `%T', but should be `%T'.", 
			    fn, TREE_TYPE (type), tmpl_return_type);
		  *targs_out = NULL_TREE;
		  return NULL_TREE;
		}
	    }

420
	  matching_fns = scratch_tree_cons (fn, targs, matching_fns);
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	}
    }

  if (matching_fns == NULL_TREE)
    {
      if (complain)
	cp_error ("Specialization of `%s' does not match any template "
		  "declaration.", IDENTIFIER_POINTER (name));
      *targs_out = NULL_TREE;
      return NULL_TREE;
    }

  if (TREE_CHAIN (matching_fns) != NULL_TREE) 
    {
      if (complain)
	{
	  tree fn;
	  
	  cp_error ("Ambiguous explicit specialization.  Candidates are:");
	  for (fn = matching_fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
	    cp_error ("    %D", TREE_PURPOSE (fn));
	}

      *targs_out = NULL_TREE;
      return NULL_TREE;
    }

  /* We have one, and exactly one, match. */
  *targs_out = TREE_VALUE (matching_fns);
  return TREE_PURPOSE (matching_fns);
mrs's avatar
mrs committed
451 452
}

453 454 455 456 457 458 459 460 461 462 463 464
	
/* Check to see if the function just declared, as indicated in
   DECLARATOR, and in DECL, is a specialization.  Check that the
   specialization is OK.  If FLAGS == 1, we are being called by
   finish_struct_methods.  If FLAGS == 2, we are being called by
   grokfndecl, and the function has a definition, or is a friend.  If
   FLAGS == 3, this is a friend declaration.
   Returns 0 if the decl is not an explicit specialization or
   instantiation, 1 if it is an explicit specialization, and 2 if it
   is an explicit instantiation.  */

int
465
check_explicit_specialization (declarator, decl, template_count, flags)
466 467 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 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
     tree declarator;
     tree decl;
     int template_count;
     int flags;
{
  int finish_member = flags == 1;
  int have_def = flags == 2;
  int is_friend = flags == 3;

  if (processing_explicit_specialization (template_count)
      || finish_member
      || TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
    {
      tree tmpl = NULL_TREE;
      tree dname = DECL_NAME (decl);
      tree ctype = DECL_CLASS_CONTEXT (decl);
      tree targs;

      /* We've come across a declarator that looks like: U f<T1,
	 T2, ...>(A1, A2, ..).  This is an explicit template
	 specialization.  Check that: 
	       
	 o The explicitly specified parameters together with those
	 that can be deduced by template argument deduction
	 uniquely determine a particular specialization.  
	       
	 See [temp.expl.spec].  */

      if (!finish_member
	  && TREE_CODE (declarator) == TEMPLATE_ID_EXPR
	  && !processing_explicit_specialization (template_count)
	  && !is_friend)
	{
	  if (!have_def)
	    /* This is not an explicit specialization.  It must be
	       an explicit instantiation.  */
	    return 2;
	  else if (pedantic)
	    pedwarn ("Explicit specialization not preceeded by "
		     "`template <>'");
	}

      if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
	{
	  tree fns;

	  my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, 
			      0);
	  if (!ctype)
	    fns = IDENTIFIER_GLOBAL_VALUE (dname);
	  else
	    fns = dname;

	  declarator = lookup_template_function (fns, NULL_TREE);
	}

      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);
	  SET_DECL_IMPLICIT_INSTANTIATION (decl);
	  return 1;
	} 

      if (ctype 
	  && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE)
	{
	  tree fns;
 
	  if (TYPE_BEING_DEFINED (ctype) && !finish_member)
	    {
	      /* 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_methods fix this up later.  */
	      SET_DECL_TEMPLATE_SPECIALIZATION (decl);
	      DECL_TEMPLATE_INFO (decl) 
		= perm_tree_cons (NULL_TREE, 
				  TREE_OPERAND (declarator, 1),
				  NULL_TREE);
	      return 1;
	    }

	  fns = lookup_fnfields (TYPE_BINFO (ctype), 
				 TREE_OPERAND (declarator, 0),
				 1);
	  
	  if (fns == NULL_TREE) 
	    {
	      cp_error ("No member template `%s' declared in `%T'",
			IDENTIFIER_POINTER (TREE_OPERAND (declarator,
							  0)),
			ctype);
	      return 1;
	    }
	  else
	    TREE_OPERAND (declarator, 0) = fns;
	}

      tmpl = 
	determine_explicit_specialization 
	(declarator, TREE_TYPE (decl), &targs, 
	 TREE_CODE (decl) == TEMPLATE_DECL, 1);
	    
      if (tmpl)
	{
	  /* Mangle the function name appropriately.  */
	  if (name_mangling_version >= 1)
	    {
	      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);
	      return 1;
	    }

	  /* This function declaration is a template specialization.
	     Record that fact.  */
	  SET_DECL_TEMPLATE_SPECIALIZATION (decl);
	  DECL_TEMPLATE_SPECIALIZATIONS (tmpl) 
	    = perm_tree_cons (targs, decl, 
			      DECL_TEMPLATE_SPECIALIZATIONS
			      (tmpl));
	  /* 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.	        

626
	     So, we just leave the template info alone in this case.  */
627 628 629 630 631 632 633 634 635 636
	  if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)))
	    DECL_TEMPLATE_INFO (decl)
	      = perm_tree_cons (tmpl, targs, NULL_TREE);
	  return 1;
	}
    }

  return 0;
}
			   
mrs's avatar
mrs committed
637
/* Process information from new template parameter NEXT and append it to the
mrs's avatar
mrs committed
638
   LIST being built.  */
mrs's avatar
mrs committed
639

mrs's avatar
mrs committed
640 641 642 643 644 645
tree
process_template_parm (list, next)
     tree list, next;
{
  tree parm;
  tree decl = 0;
mrs's avatar
mrs committed
646
  tree defval;
mrs's avatar
mrs committed
647
  int is_type, idx;
mrs's avatar
mrs committed
648 649
  parm = next;
  my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
mrs's avatar
mrs committed
650 651 652
  defval = TREE_PURPOSE (parm);
  parm = TREE_VALUE (parm);
  is_type = TREE_PURPOSE (parm) == class_type_node;
mrs's avatar
mrs committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666

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

      if (TREE_CODE (p) == TYPE_DECL)
	idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
      else
	idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p));
      ++idx;
    }
  else
    idx = 0;

mrs's avatar
mrs committed
667 668 669
  if (!is_type)
    {
      tree tinfo = 0;
mrs's avatar
mrs committed
670
      my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
mrs's avatar
mrs committed
671
      /* is a const-param */
mrs's avatar
mrs committed
672
      parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
mrs's avatar
merging  
mrs committed
673
			     PARM, 0, NULL_TREE);
mrs's avatar
mrs committed
674 675
      /* A template parameter is not modifiable.  */
      TREE_READONLY (parm) = 1;
mrs's avatar
mrs committed
676 677
      if (IS_AGGR_TYPE (TREE_TYPE (parm))
	  && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
mrs's avatar
mrs committed
678
	{
mrs's avatar
mrs committed
679 680 681 682
	  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
683 684
	  TREE_TYPE (parm) = void_type_node;
	}
mrs's avatar
mrs committed
685 686 687
      else if (pedantic
	       && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
		   || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
mrs's avatar
mrs committed
688 689
	cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
		    TREE_TYPE (parm));
mrs's avatar
mrs committed
690 691 692 693 694 695 696 697 698 699 700
      tinfo = make_node (TEMPLATE_CONST_PARM);
      my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
      if (TREE_PERMANENT (parm) == 0)
        {
	  parm = copy_node (parm);
	  TREE_PERMANENT (parm) = 1;
        }
      TREE_TYPE (tinfo) = TREE_TYPE (parm);
      decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
      DECL_INITIAL (decl) = tinfo;
      DECL_INITIAL (parm) = tinfo;
mrs's avatar
mrs committed
701
      TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl);
mrs's avatar
mrs committed
702 703 704
    }
  else
    {
mrs's avatar
mrs committed
705 706
      tree t = make_lang_type (TEMPLATE_TYPE_PARM);
      CLASSTYPE_GOT_SEMICOLON (t) = 1;
mrs's avatar
mrs committed
707
      decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
mrs's avatar
mrs committed
708 709
      TYPE_NAME (t) = decl;
      TYPE_STUB_DECL (t) = decl;
mrs's avatar
mrs committed
710
      parm = decl;
mrs's avatar
mrs committed
711
      TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl);
mrs's avatar
mrs committed
712
    }
mrs's avatar
mrs committed
713
  SET_DECL_ARTIFICIAL (decl);
mrs's avatar
mrs committed
714
  pushdecl (decl);
mrs's avatar
mrs committed
715
  parm = build_tree_list (defval, parm);
mrs's avatar
mrs committed
716 717 718 719 720 721 722 723 724 725 726 727
  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
728
  int nparms;
mrs's avatar
mrs committed
729
  tree parm;
mrs's avatar
mrs committed
730 731 732 733 734
  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
735 736

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

mrs's avatar
mrs committed
739 740 741
  return saved_parmlist;
}

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

mrs's avatar
mrs committed
744
void
mrs's avatar
mrs committed
745
end_template_decl ()
mrs's avatar
mrs committed
746
{
747 748
  reset_specialization ();

mrs's avatar
mrs committed
749
  if (! processing_template_decl)
mrs's avatar
mrs committed
750 751
    return;

mrs's avatar
mrs committed
752 753
  /* This matches the pushlevel in begin_template_parm_list.  */
  poplevel (0, 0, 0);
mrs's avatar
mrs committed
754

mrs's avatar
mrs committed
755 756 757 758
  --processing_template_decl;
  current_template_parms = TREE_CHAIN (current_template_parms);
  (void) get_pending_sizes ();	/* Why? */
}
mrs's avatar
mrs committed
759

mrs's avatar
mrs committed
760 761 762 763
/* Generate a valid set of template args from current_template_parms.  */

tree
current_template_args ()
mrs's avatar
mrs committed
764 765
{
  tree header = current_template_parms;
766 767 768 769
  int length = list_length (header);
  tree args = make_tree_vec (length);
  int l = length;

mrs's avatar
mrs committed
770
  while (header)
mrs's avatar
mrs committed
771
    {
mrs's avatar
mrs committed
772 773 774 775 776
      tree a = copy_node (TREE_VALUE (header));
      int i = TREE_VEC_LENGTH (a);
      TREE_TYPE (a) = NULL_TREE;
      while (i--)
	{
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	  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);
	      
	      if (TREE_CODE (t) == TYPE_DECL)
		t = TREE_TYPE (t);
	      else
		t = DECL_INITIAL (t);
	    }

mrs's avatar
mrs committed
792 793
	  TREE_VEC_ELT (a, i) = t;
	}
794
      TREE_VEC_ELT (args, --l) = a;
mrs's avatar
mrs committed
795
      header = TREE_CHAIN (header);
mrs's avatar
mrs committed
796 797
    }

mrs's avatar
mrs committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
  return args;
}
  
void
push_template_decl (decl)
     tree decl;
{
  tree tmpl;
  tree args = NULL_TREE;
  tree info;
  tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
  int primary = 0;

  /* Kludge! */
  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
      && DECL_CLASS_CONTEXT (decl))
    ;
  /* Note that this template is a "primary template" */
  else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
      /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
    primary = 1;

mrs's avatar
mrs committed
820
  /* Partial specialization.  */
mrs's avatar
mrs committed
821
  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
mrs's avatar
mrs committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
      && 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))
	    return;
	}

mrs's avatar
mrs committed
837 838 839
      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
840 841 842 843
      TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
      return;
    }

mrs's avatar
mrs committed
844 845
  args = current_template_args ();

mrs's avatar
mrs committed
846
  if (! ctx || TYPE_BEING_DEFINED (ctx))
mrs's avatar
mrs committed
847
    {
mrs's avatar
mrs committed
848
      tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
849
      DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
mrs's avatar
mrs committed
850
      DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
851
      if (DECL_LANG_SPECIFIC (decl))
852 853 854 855
	{
	  DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
	  DECL_STATIC_FUNCTION_P (tmpl) = 
	    DECL_STATIC_FUNCTION_P (decl);
856 857 858 859 860 861 862 863 864

	  if (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;
	    }
865
	}
mrs's avatar
mrs committed
866 867 868
    }
  else
    {
mrs's avatar
mrs committed
869
      tree t;
870
      tree a;
mrs's avatar
mrs committed
871

mrs's avatar
mrs committed
872 873 874
      if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
	cp_error ("must specialize `%#T' before defining member `%#D'",
		  ctx, decl);
mrs's avatar
mrs committed
875
      if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
mrs's avatar
mrs committed
876
	tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
mrs's avatar
mrs committed
877
      else if (! DECL_TEMPLATE_INFO (decl))
mrs's avatar
mrs committed
878 879 880 881
	{
	  cp_error ("template definition of non-template `%#D'", decl);
	  return;
	}
mrs's avatar
mrs committed
882
      else
mrs's avatar
mrs committed
883
	tmpl = DECL_TI_TEMPLATE (decl);
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
      
      if (is_member_template (tmpl))
	{
	  a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
	  t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
	  if (TREE_VEC_LENGTH (t) 
	      != TREE_VEC_LENGTH (a))
	    {
	      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
907 908

      if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
909 910 911 912 913 914 915 916 917 918 919 920
	{
	  /* 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
921

922 923
      if (t != NULL_TREE 
	  && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
mrs's avatar
mrs committed
924 925
	{
	  cp_error ("got %d template parameters for `%#D'",
926
		    TREE_VEC_LENGTH (a), decl);
mrs's avatar
mrs committed
927 928
	  cp_error ("  but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
	}
mrs's avatar
mrs committed
929
    }
930 931
  /* Get the innermost set of template arguments. */
  args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
mrs's avatar
mrs committed
932

mrs's avatar
mrs committed
933 934
  DECL_TEMPLATE_RESULT (tmpl) = decl;
  TREE_TYPE (tmpl) = TREE_TYPE (decl);
mrs's avatar
mrs committed
935

mrs's avatar
mrs committed
936 937
  if (! ctx)
    tmpl = pushdecl_top_level (tmpl);
mrs's avatar
mrs committed
938

mrs's avatar
mrs committed
939
  if (primary)
940
    TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;
mrs's avatar
mrs committed
941 942 943

  info = perm_tree_cons (tmpl, args, NULL_TREE);

mrs's avatar
mrs committed
944
  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
mrs's avatar
mrs committed
945
    {
mrs's avatar
mrs committed
946 947
      CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
      DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
mrs's avatar
mrs committed
948
    }
mrs's avatar
mrs committed
949 950
  else if (! DECL_LANG_SPECIFIC (decl))
    cp_error ("template declaration of `%#D'", decl);
mrs's avatar
mrs committed
951
  else
mrs's avatar
mrs committed
952
    DECL_TEMPLATE_INFO (decl) = info;
mrs's avatar
mrs committed
953 954 955 956 957
}

/* Convert all template arguments to their appropriate types, and return
   a vector containing the resulting values.  If any error occurs, return
   error_mark_node.  */
mrs's avatar
mrs committed
958

mrs's avatar
mrs committed
959 960 961 962 963
static tree
coerce_template_parms (parms, arglist, in_decl)
     tree parms, arglist;
     tree in_decl;
{
mrs's avatar
mrs committed
964
  int nparms, nargs, i, lost = 0;
mrs's avatar
mrs committed
965 966
  tree vec;

mrs's avatar
mrs committed
967 968 969 970
  if (arglist == NULL_TREE)
    nargs = 0;
  else if (TREE_CODE (arglist) == TREE_VEC)
    nargs = TREE_VEC_LENGTH (arglist);
mrs's avatar
mrs committed
971
  else
mrs's avatar
mrs committed
972 973 974 975 976 977 978
    nargs = list_length (arglist);

  nparms = TREE_VEC_LENGTH (parms);

  if (nargs > nparms
      || (nargs < nparms
	  && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
mrs's avatar
mrs committed
979 980
    {
      error ("incorrect number of parameters (%d, should be %d)",
mrs's avatar
mrs committed
981
	     nargs, nparms);
mrs's avatar
mrs committed
982 983 984 985 986
      if (in_decl)
	cp_error_at ("in template expansion for decl `%D'", in_decl);
      return error_mark_node;
    }

mrs's avatar
mrs committed
987
  if (arglist && TREE_CODE (arglist) == TREE_VEC)
mrs's avatar
mrs committed
988 989 990 991 992 993
    vec = copy_node (arglist);
  else
    {
      vec = make_tree_vec (nparms);
      for (i = 0; i < nparms; i++)
	{
mrs's avatar
mrs committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	  tree arg;

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

	      if (arg == error_mark_node)
		lost++;
	      else
		arg = TREE_VALUE (arg);
	    }
mrs's avatar
mrs committed
1006 1007
	  else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i)))
		   == TYPE_DECL)
mrs's avatar
mrs committed
1008
	    arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
1009
			  vec, i, in_decl);
mrs's avatar
mrs committed
1010 1011
	  else
	    arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
1012
			       vec, i, in_decl);
mrs's avatar
mrs committed
1013

mrs's avatar
mrs committed
1014 1015 1016 1017 1018 1019
	  TREE_VEC_ELT (vec, i) = arg;
	}
    }
  for (i = 0; i < nparms; i++)
    {
      tree arg = TREE_VEC_ELT (vec, i);
mrs's avatar
mrs committed
1020
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
mrs's avatar
mrs committed
1021 1022 1023 1024
      tree val = 0;
      int is_type, requires_type;

      is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
mrs's avatar
mrs committed
1025
      requires_type = TREE_CODE (parm) == TYPE_DECL;
mrs's avatar
mrs committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035

      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);
	  arg = make_typename_type (TREE_OPERAND (arg, 0),
				    TREE_OPERAND (arg, 1));
	  is_type = 1;
	}
mrs's avatar
mrs committed
1036 1037 1038
      if (is_type != requires_type)
	{
	  if (in_decl)
mrs's avatar
mrs committed
1039 1040 1041 1042 1043 1044 1045 1046 1047
	    {
	      cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
			i, in_decl);
	      if (is_type)
		cp_error ("  expected a constant of type `%T', got `%T'",
			  TREE_TYPE (parm), arg);
	      else
		cp_error ("  expected a type, got `%E'", arg);
	    }
mrs's avatar
mrs committed
1048 1049 1050 1051 1052
	  lost++;
	  TREE_VEC_ELT (vec, i) = error_mark_node;
	  continue;
	}
      if (is_type)
mrs's avatar
mrs committed
1053 1054
	{
	  val = groktypename (arg);
mrs's avatar
mrs committed
1055
	  if (! processing_template_decl)
mrs's avatar
mrs committed
1056 1057
	    {
	      tree t = target_type (val);
1058 1059
	      if (TREE_CODE (t) != TYPENAME_TYPE 
		  && IS_AGGR_TYPE (t)
mrs's avatar
mrs committed
1060 1061 1062 1063 1064 1065 1066
		  && 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
1067 1068
      else
	{
1069
	  tree t = tsubst (TREE_TYPE (parm), vec,
1070
			   TREE_VEC_LENGTH (vec), in_decl);
mrs's avatar
mrs committed
1071
	  if (processing_template_decl)
mrs's avatar
mrs committed
1072 1073 1074
	    val = arg;
	  else
	    val = digest_init (t, arg, (tree *) 0);
mrs's avatar
mrs committed
1075

mrs's avatar
mrs committed
1076
	  if (val == error_mark_node || processing_template_decl)
mrs's avatar
mrs committed
1077 1078 1079 1080 1081
	    ;

	  /* 14.2: Other template-arguments must be constant-expressions,
	     addresses of objects or functions with external linkage, or of
	     static class members.  */
mrs's avatar
mrs committed
1082 1083 1084 1085 1086
	  else if (IS_AGGR_TYPE (TREE_TYPE (val)))
	    {
	      cp_error ("object `%E' cannot be used as template argument", arg);
	      val = error_mark_node;
	    }
mrs's avatar
mrs committed
1087 1088 1089 1090 1091 1092
	  else if (!TREE_CONSTANT (val))
	    {
	      cp_error ("non-const `%E' cannot be used as template argument",
			arg);
	      val = error_mark_node;
	    }
mrs's avatar
mrs committed
1093 1094 1095 1096
	  else if (POINTER_TYPE_P (TREE_TYPE (val))
		   && ! integer_zerop (val)
		   && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
		   && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
mrs's avatar
mrs committed
1097
	    {
mrs's avatar
mrs committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
	      t = val;
	      STRIP_NOPS (t);
	      if (TREE_CODE (t) == ADDR_EXPR)
		{
		  tree a = TREE_OPERAND (t, 0);
		  STRIP_NOPS (a);
		  if (TREE_CODE (a) == STRING_CST)
		    {
		      cp_error ("string literal %E is not a valid template argument", a);
		      error ("because it is the address of an object with static linkage");
		      val = error_mark_node;
		    }
		  else if (TREE_CODE (a) != VAR_DECL
			   && TREE_CODE (a) != FUNCTION_DECL)
		    goto bad;
jason's avatar
jason committed
1113
		  else if (! TREE_PUBLIC (a))
mrs's avatar
mrs committed
1114 1115 1116 1117 1118 1119
		    {
		      cp_error ("address of non-extern `%E' cannot be used as template argument", a);
		      val = error_mark_node;
		    }
		}
	      else
mrs's avatar
mrs committed
1120
		{
mrs's avatar
mrs committed
1121 1122 1123 1124 1125 1126 1127
		bad:
		  cp_error ("`%E' is not a valid template argument", t);
		  error ("it must be %s%s with external linkage",
			 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
			 ? "a pointer to " : "",
			 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
			 ? "a function" : "an object");
mrs's avatar
mrs committed
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
		  val = error_mark_node;
		}
	    }
	}

      if (val == error_mark_node)
	lost++;

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

mrs's avatar
mrs committed
1143
static int
mrs's avatar
mrs committed
1144 1145 1146 1147 1148
comp_template_args (oldargs, newargs)
     tree oldargs, newargs;
{
  int i;

1149 1150 1151
  if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
    return 0;

mrs's avatar
mrs committed
1152 1153 1154 1155 1156 1157 1158 1159 1160
  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;
mrs's avatar
mrs committed
1161 1162 1163 1164 1165 1166
      if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
	{
	  if (comptypes (ot, nt, 1))
	    continue;
	}
      else if (cp_tree_equal (ot, nt) > 0)
mrs's avatar
mrs committed
1167 1168 1169 1170 1171 1172
	continue;
      return 0;
    }
  return 1;
}

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

mrs's avatar
mrs committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
static char *
mangle_class_name_for_template (name, parms, arglist)
     char *name;
     tree parms, arglist;
{
  static struct obstack scratch_obstack;
  static char *scratch_firstobj;
  int i, nparms;

  if (!scratch_firstobj)
mrs's avatar
mrs committed
1186
    gcc_obstack_init (&scratch_obstack);
mrs's avatar
mrs committed
1187 1188
  else
    obstack_free (&scratch_obstack, scratch_firstobj);
mrs's avatar
mrs committed
1189
  scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
mrs's avatar
mrs committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

#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

  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
1210 1211
      tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
      tree arg = TREE_VEC_ELT (arglist, i);
mrs's avatar
mrs committed
1212 1213 1214 1215

      if (i)
	ccat (',');

mrs's avatar
mrs committed
1216
      if (TREE_CODE (parm) == TYPE_DECL)
mrs's avatar
mrs committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
	{
	  cat (type_as_string (arg, 0));
	  continue;
	}
      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
1250
#if 0
mrs's avatar
mrs committed
1251
 too_long:
mrs's avatar
mrs committed
1252
#endif
mrs's avatar
mrs committed
1253 1254 1255 1256 1257
  fatal ("out of (preallocated) string space creating template instantiation name");
  /* NOTREACHED */
  return NULL;
}

mrs's avatar
mrs committed
1258
static tree
mrs's avatar
mrs committed
1259 1260 1261 1262 1263 1264 1265 1266 1267
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),
1268
	 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
mrs's avatar
mrs committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	 CLASSTYPE_TI_ARGS (t));
      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
1282 1283 1284 1285 1286 1287 1288
  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
1289
  if (TI_PENDING_TEMPLATE_FLAG (ti))
mrs's avatar
mrs committed
1290 1291 1292 1293 1294
    return;

  *template_tail = perm_tree_cons
    (current_function_decl, d, NULL_TREE);
  template_tail = &TREE_CHAIN (*template_tail);
mrs's avatar
mrs committed
1295
  TI_PENDING_TEMPLATE_FLAG (ti) = 1;
mrs's avatar
mrs committed
1296 1297
}

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

/* 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))
    {
      push_obstacks (&permanent_obstack, &permanent_obstack);
      arglist = copy_list (arglist);
      pop_obstacks ();
    }

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


mrs's avatar
mrs committed
1327 1328 1329 1330 1331 1332 1333 1334 1335
/* 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
   instantiate.  */
mrs's avatar
mrs committed
1336

mrs's avatar
mrs committed
1337 1338 1339 1340 1341 1342 1343
tree
lookup_template_class (d1, arglist, in_decl)
     tree d1, arglist;
     tree in_decl;
{
  tree template, parmlist;
  char *mangled_name;
mrs's avatar
mrs committed
1344 1345 1346 1347 1348 1349 1350 1351
  tree id, t;

  if (TREE_CODE (d1) == IDENTIFIER_NODE)
    {
      template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
      if (! template)
	template = IDENTIFIER_CLASS_VALUE (d1);
    }
mrs's avatar
mrs committed
1352 1353 1354 1355 1356
  else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
    {
      template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
      d1 = DECL_NAME (template);
    }
mrs's avatar
mrs committed
1357 1358 1359 1360 1361 1362 1363
  else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
    {
      template = CLASSTYPE_TI_TEMPLATE (d1);
      d1 = DECL_NAME (template);
    }
  else
    my_friendly_abort (272);
mrs's avatar
mrs committed
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378

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

mrs's avatar
mrs committed
1379
  if (PRIMARY_TEMPLATE_P (template))
mrs's avatar
mrs committed
1380
    {
1381
      parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
mrs's avatar
mrs committed
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412

      arglist = coerce_template_parms (parmlist, arglist, template);
      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;
	    }
	}

mrs's avatar
mrs committed
1413 1414 1415
      mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
						     parmlist, arglist);
      id = get_identifier (mangled_name);
mrs's avatar
mrs committed
1416
      IDENTIFIER_TEMPLATE (id) = d1;
mrs's avatar
mrs committed
1417

mrs's avatar
mrs committed
1418
      maybe_push_to_top_level (uses_template_parms (arglist));
mrs's avatar
mrs committed
1419
      t = xref_tag_from_type (TREE_TYPE (template), id, 1);
mrs's avatar
mrs committed
1420
      pop_from_top_level ();
mrs's avatar
mrs committed
1421
    }
mrs's avatar
mrs committed
1422
  else
mrs's avatar
mrs committed
1423
    {
mrs's avatar
mrs committed
1424 1425 1426 1427
      tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
					arglist, in_decl);
      id = d1;
      arglist = CLASSTYPE_TI_ARGS (ctx);
mrs's avatar
mrs committed
1428

mrs's avatar
mrs committed
1429
      if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
mrs's avatar
mrs committed
1430
	{
mrs's avatar
mrs committed
1431 1432
	  int save_temp = processing_template_decl;
	  processing_template_decl = 0;
mrs's avatar
mrs committed
1433
	  t = xref_tag_from_type (TREE_TYPE (template), id, 0);
mrs's avatar
mrs committed
1434
	  processing_template_decl = save_temp;
mrs's avatar
mrs committed
1435 1436 1437
	}
      else
	{
mrs's avatar
mrs committed
1438 1439
	  t = lookup_nested_type_by_name (ctx, id);
	  my_friendly_assert (t != NULL_TREE, 42);
mrs's avatar
mrs committed
1440
	}
mrs's avatar
mrs committed
1441 1442
    }

mrs's avatar
mrs committed
1443
  /* Seems to be wanted.  */
mrs's avatar
mrs committed
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
  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
1458
      /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up.  */
mrs's avatar
mrs committed
1459
      DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
1460
      /* if (! uses_template_parms (arglist)) */
mrs's avatar
mrs committed
1461 1462 1463 1464 1465 1466
	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
1467
	add_pending_template (t);
mrs's avatar
mrs committed
1468 1469
    }

mrs's avatar
mrs committed
1470
  return t;
mrs's avatar
mrs committed
1471 1472
}

mrs's avatar
mrs committed
1473
/* Should be defined in parse.h.  */
mrs's avatar
mrs committed
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
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
1494
      my_friendly_abort (42);
mrs's avatar
mrs committed
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515

      /* 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
1516 1517
      if (TYPE_PTRMEMFUNC_FLAG (t))
	return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
mrs's avatar
mrs committed
1518
    case UNION_TYPE:
mrs's avatar
mrs committed
1519
      if (! CLASSTYPE_TEMPLATE_INFO (t))
mrs's avatar
mrs committed
1520
	return 0;
mrs's avatar
mrs committed
1521
      return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
mrs's avatar
mrs committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
    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:
1535
      if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
mrs's avatar
mrs committed
1536 1537 1538 1539 1540 1541 1542
	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
1543 1544
      return uses_template_parms (TREE_TYPE (t));

mrs's avatar
mrs committed
1545
    case FUNCTION_DECL:
mrs's avatar
mrs committed
1546 1547 1548 1549
    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
1550 1551
	return 1;
      /* fall through */
mrs's avatar
mrs committed
1552
    case CONST_DECL:
mrs's avatar
mrs committed
1553
    case PARM_DECL:
mrs's avatar
mrs committed
1554 1555
      if (uses_template_parms (TREE_TYPE (t)))
	return 1;
mrs's avatar
mrs committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
      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:
    case TEMPLATE_CONST_PARM:
      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
1577
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
1578
    case VOID_TYPE:
mrs's avatar
mrs committed
1579
    case BOOLEAN_TYPE:
mrs's avatar
mrs committed
1580 1581
      return 0;

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
    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
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
      /* 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
1604
    case LOOKUP_EXPR:
mrs's avatar
mrs committed
1605
    case TYPENAME_TYPE:
mrs's avatar
mrs committed
1606 1607
      return 1;

mrs's avatar
mrs committed
1608 1609 1610
    case SCOPE_REF:
      return uses_template_parms (TREE_OPERAND (t, 0));

mrs's avatar
mrs committed
1611 1612 1613
    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
1614
      return uses_template_parms (TREE_OPERAND (t, 1));
mrs's avatar
mrs committed
1615

brendan's avatar
brendan committed
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
    case MODOP_EXPR:
    case CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
    case CONST_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
    case SIZEOF_EXPR:
    case ARROW_EXPR:
    case DOTSTAR_EXPR:
    case TYPEID_EXPR:
      return 1;

mrs's avatar
mrs committed
1628 1629 1630 1631 1632
    default:
      switch (TREE_CODE_CLASS (TREE_CODE (t)))
	{
	case '1':
	case '2':
mrs's avatar
mrs committed
1633
	case 'e':
mrs's avatar
mrs committed
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
	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
1653 1654 1655
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
1656
extern int max_tinst_depth;
mrs's avatar
mrs committed
1657 1658 1659
#ifdef GATHER_STATISTICS
int depth_reached = 0;
#endif
mrs's avatar
mrs committed
1660

mrs's avatar
mrs committed
1661
static int
mrs's avatar
mrs committed
1662 1663
push_tinst_level (d)
     tree d;
mrs's avatar
mrs committed
1664 1665 1666
{
  struct tinst_level *new;

mrs's avatar
mrs committed
1667 1668
  if (tinst_depth >= max_tinst_depth)
    {
mrs's avatar
mrs committed
1669 1670 1671 1672
      struct tinst_level *p = current_tinst_level;
      int line = lineno;
      char *file = input_filename;

mrs's avatar
mrs committed
1673 1674
      error ("template instantiation depth exceeds maximum of %d",
	     max_tinst_depth);
mrs's avatar
mrs committed
1675
      error (" (use -ftemplate-depth-NN to increase the maximum)");
mrs's avatar
mrs committed
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
      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
1689 1690 1691
      return 0;
    }

mrs's avatar
mrs committed
1692 1693 1694 1695 1696 1697 1698 1699
  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
1700 1701 1702
  new->decl = d;
  new->line = lineno;
  new->file = input_filename;
mrs's avatar
mrs committed
1703 1704
  new->next = current_tinst_level;
  current_tinst_level = new;
mrs's avatar
mrs committed
1705

mrs's avatar
mrs committed
1706
  ++tinst_depth;
mrs's avatar
mrs committed
1707 1708 1709 1710 1711
#ifdef GATHER_STATISTICS
  if (tinst_depth > depth_reached)
    depth_reached = tinst_depth;
#endif

mrs's avatar
mrs committed
1712
  return 1;
mrs's avatar
mrs committed
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
}

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
1723
  --tinst_depth;
mrs's avatar
mrs committed
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
}

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

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

tree
mrs's avatar
mrs committed
1738 1739
instantiate_class_template (type)
     tree type;
mrs's avatar
mrs committed
1740
{
mrs's avatar
mrs committed
1741
  tree template, template_info, args, pattern, t, *field_chain;
mrs's avatar
mrs committed
1742

mrs's avatar
mrs committed
1743
  if (type == error_mark_node)
mrs's avatar
mrs committed
1744 1745
    return error_mark_node;

mrs's avatar
mrs committed
1746 1747 1748 1749 1750 1751 1752 1753
  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
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770

  t = most_specialized_class
    (DECL_TEMPLATE_SPECIALIZATIONS (template), args);

  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))
	{
	  if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
	    {
	      cp_error_at ("%s %+#T", str, TREE_TYPE (t));
	      str = "               ";
	    }
	}
      TYPE_BEING_DEFINED (type) = 1;
mrs's avatar
mrs committed
1771
      return error_mark_node;
mrs's avatar
mrs committed
1772 1773 1774 1775 1776
    }
  else if (t)
    pattern = TREE_TYPE (t);
  else
    pattern = TREE_TYPE (template);
mrs's avatar
mrs committed
1777 1778

  if (TYPE_SIZE (pattern) == NULL_TREE)
mrs's avatar
mrs committed
1779
    return type;
mrs's avatar
mrs committed
1780

mrs's avatar
mrs committed
1781 1782 1783
  if (t)
    args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);

mrs's avatar
mrs committed
1784 1785 1786 1787
  TYPE_BEING_DEFINED (type) = 1;

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

mrs's avatar
mrs committed
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
  maybe_push_to_top_level (uses_template_parms (type));
  pushclass (type, 0);

  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)
	    = ! CLASSTYPE_INTERFACE_ONLY (type)
	      && CLASSTYPE_INTERFACE_KNOWN (type);
	}
      else
	{
	  CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
	  SET_CLASSTYPE_INTERFACE_UNKNOWN_X
	    (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
	  CLASSTYPE_VTABLE_NEEDS_WRITING (type)
	    = ! CLASSTYPE_INTERFACE_ONLY (type)
	      && CLASSTYPE_INTERFACE_KNOWN (type);
	}
    }
  else
mrs's avatar
mrs committed
1813
    {
mrs's avatar
mrs committed
1814 1815
      SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
      CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
mrs's avatar
mrs committed
1816 1817
    }

mrs's avatar
mrs committed
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
  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);

mrs's avatar
mrs committed
1844 1845 1846 1847 1848 1849 1850 1851 1852
  {
    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
1853
	bases = make_tree_vec (len);
mrs's avatar
mrs committed
1854 1855 1856 1857 1858
	for (i = 0; i < len; ++i)
	  {
	    tree elt;

	    TREE_VEC_ELT (bases, i) = elt
1859
	      = tsubst (TREE_VEC_ELT (pbases, i), args,
mrs's avatar
mrs committed
1860 1861 1862
			TREE_VEC_LENGTH (args), NULL_TREE);
	    BINFO_INHERITANCE_CHAIN (elt) = binfo;

mrs's avatar
mrs committed
1863 1864 1865 1866 1867 1868 1869
	    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);
	    else if (! uses_template_parms (type)
		     && (TYPE_SIZE (complete_type (TREE_TYPE (elt)))
			 == NULL_TREE))
mrs's avatar
mrs committed
1870 1871 1872
	      cp_error ("base class `%T' of `%T' has incomplete type",
			TREE_TYPE (elt), type);
	  }
mrs's avatar
mrs committed
1873 1874 1875
	/* Don't initialize this until the vector is filled out, or
	   lookups will crash.  */
	BINFO_BASETYPES (binfo) = bases;
mrs's avatar
mrs committed
1876 1877 1878 1879 1880 1881 1882 1883
      }
  }

  CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);

  field_chain = &TYPE_FIELDS (type);

  for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
mrs's avatar
mrs committed
1884
    {
mrs's avatar
mrs committed
1885 1886 1887
      tree name = TREE_PURPOSE (t);
      tree tag = TREE_VALUE (t);

mrs's avatar
mrs committed
1888
      /* These will add themselves to CLASSTYPE_TAGS for the new type.  */
mrs's avatar
mrs committed
1889
      if (TREE_CODE (tag) == ENUMERAL_TYPE)
mrs's avatar
mrs committed
1890
	{
1891
	  tree e, newtag = tsubst_enum (tag, args, 
1892
					TREE_VEC_LENGTH (args), field_chain);
mrs's avatar
mrs committed
1893 1894

	  while (*field_chain)
mrs's avatar
mrs committed
1895 1896 1897 1898
	    {
	      DECL_FIELD_CONTEXT (*field_chain) = type;
	      field_chain = &TREE_CHAIN (*field_chain);
	    }
mrs's avatar
mrs committed
1899
	}
mrs's avatar
mrs committed
1900
      else
1901
	tsubst (tag, args,
mrs's avatar
mrs committed
1902
		TREE_VEC_LENGTH (args), NULL_TREE);
mrs's avatar
mrs committed
1903 1904
    }

mrs's avatar
mrs committed
1905 1906 1907 1908
  /* Don't replace enum constants here.  */
  for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
    if (TREE_CODE (t) != CONST_DECL)
      {
1909
	tree r = tsubst (t, args,
mrs's avatar
mrs committed
1910 1911 1912 1913 1914
			 TREE_VEC_LENGTH (args), NULL_TREE);
	if (TREE_CODE (r) == VAR_DECL)
	  {
	    if (! uses_template_parms (r))
	      pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
mrs's avatar
mrs committed
1915
	    /* Perhaps I should do more of grokfield here.  */
mrs's avatar
mrs committed
1916 1917 1918 1919 1920 1921 1922 1923 1924
	    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
1925

mrs's avatar
mrs committed
1926
  TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
mrs's avatar
mrs committed
1927 1928 1929 1930 1931 1932 1933
  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
1934

mrs's avatar
mrs committed
1935
  DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
mrs's avatar
mrs committed
1936
    = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1937
	      args, TREE_VEC_LENGTH (args), NULL_TREE);
mrs's avatar
mrs committed
1938 1939

  {
mrs's avatar
mrs committed
1940
    tree d = CLASSTYPE_FRIEND_CLASSES (type)
1941
      = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args,
mrs's avatar
mrs committed
1942
		TREE_VEC_LENGTH (args), NULL_TREE);
mrs's avatar
mrs committed
1943 1944 1945 1946 1947

    /* This does injection for friend classes.  */
    for (; d; d = TREE_CHAIN (d))
      TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);

1948
    d = tsubst (DECL_TEMPLATE_INJECT (template), args,
1949
		TREE_VEC_LENGTH (args), NULL_TREE);
mrs's avatar
mrs committed
1950 1951

    for (; d; d = TREE_CHAIN (d))
mrs's avatar
mrs committed
1952 1953 1954 1955 1956 1957 1958 1959
      {
	tree t = TREE_VALUE (d);

	if (TREE_CODE (t) == TYPE_DECL)
	  /* Already injected.  */;
	else
	  pushdecl (t);
      }
mrs's avatar
mrs committed
1960 1961 1962
  }

  if (! uses_template_parms (type))
mrs's avatar
mrs committed
1963
    {
mrs's avatar
mrs committed
1964 1965
      tree tmp;
      for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
mrs's avatar
mrs committed
1966
	if (TREE_CODE (tmp) == FIELD_DECL)
mrs's avatar
mrs committed
1967 1968 1969 1970
	  {
	    TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
	    require_complete_type (tmp);
	  }
mrs's avatar
mrs committed
1971

mrs's avatar
mrs committed
1972
      type = finish_struct_1 (type, 0);
mrs's avatar
mrs committed
1973
      CLASSTYPE_GOT_SEMICOLON (type) = 1;
mrs's avatar
mrs committed
1974 1975

      repo_template_used (type);
1976 1977
      if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
	finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
mrs's avatar
mrs committed
1978 1979 1980
    }
  else
    {
mrs's avatar
mrs committed
1981 1982 1983
      TYPE_SIZE (type) = integer_zero_node;
      CLASSTYPE_METHOD_VEC (type)
	= finish_struct_methods (type, TYPE_METHODS (type), 1);
mrs's avatar
mrs committed
1984 1985
    }

mrs's avatar
mrs committed
1986 1987 1988 1989 1990 1991 1992
  TYPE_BEING_DEFINED (type) = 0;
  popclass (0);

  pop_from_top_level ();
  pop_tinst_level ();

  return type;
mrs's avatar
mrs committed
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
}

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
2011
tree 
mrs's avatar
mrs committed
2012 2013 2014 2015 2016
lookup_nested_type_by_name (ctype, name)
        tree ctype, name;
{
  tree t;

mrs's avatar
mrs committed
2017 2018
  complete_type (ctype);

mrs's avatar
mrs committed
2019 2020
  for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
    {
2021 2022 2023
      if (name == TREE_PURPOSE (t)
	  /* this catches typedef enum { foo } bar; */
	  || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
mrs's avatar
mrs committed
2024 2025
	return TREE_VALUE (t);
    }
mrs's avatar
mrs committed
2026 2027 2028
  return NULL_TREE;
}

2029
tree
mrs's avatar
mrs committed
2030
tsubst (t, args, nargs, in_decl)
2031
     tree t, args;
mrs's avatar
mrs committed
2032 2033 2034 2035 2036
     int nargs;
     tree in_decl;
{
  tree type;

mrs's avatar
mrs committed
2037 2038 2039 2040
  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
2041 2042 2043
    return t;

  type = TREE_TYPE (t);
mrs's avatar
mrs committed
2044 2045
  if (type == unknown_type_node)
    my_friendly_abort (42);
mrs's avatar
mrs committed
2046 2047
  if (type && TREE_CODE (t) != FUNCTION_DECL
      && TREE_CODE (t) != TYPENAME_TYPE)
mrs's avatar
mrs committed
2048 2049
    type = tsubst (type, args, nargs, in_decl);

mrs's avatar
mrs committed
2050 2051 2052 2053
  switch (TREE_CODE (t))
    {
    case RECORD_TYPE:
      if (TYPE_PTRMEMFUNC_P (t))
mrs's avatar
mrs committed
2054 2055 2056 2057 2058 2059 2060
	{
	  tree r = build_ptrmemfunc_type
	    (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
	  return cp_build_type_variant (r, TYPE_READONLY (t),
					TYPE_VOLATILE (t));
	}

mrs's avatar
mrs committed
2061
      /* else fall through */
mrs's avatar
mrs committed
2062 2063 2064 2065 2066 2067 2068 2069
    case UNION_TYPE:
      if (uses_template_parms (t))
	{
	  tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
	  tree r = lookup_template_class (t, argvec, in_decl);
	  return cp_build_type_variant (r, TYPE_READONLY (t),
					TYPE_VOLATILE (t));
	}
mrs's avatar
mrs committed
2070

mrs's avatar
mrs committed
2071
      /* else fall through */
mrs's avatar
mrs committed
2072 2073 2074 2075 2076
    case ERROR_MARK:
    case IDENTIFIER_NODE:
    case OP_IDENTIFIER:
    case VOID_TYPE:
    case REAL_TYPE:
mrs's avatar
mrs committed
2077
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
2078
    case BOOLEAN_TYPE:
mrs's avatar
mrs committed
2079 2080 2081 2082 2083
    case INTEGER_CST:
    case REAL_CST:
    case STRING_CST:
      return t;

mrs's avatar
mrs committed
2084 2085 2086 2087 2088
    case ENUMERAL_TYPE:
      {
	tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
	if (ctx == NULL_TREE)
	  return t;
mrs's avatar
mrs committed
2089 2090
	else if (ctx == current_function_decl)
	  return lookup_name (TYPE_IDENTIFIER (t), 1);
mrs's avatar
mrs committed
2091 2092 2093 2094
	else
	  return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
      }

mrs's avatar
mrs committed
2095 2096 2097 2098 2099 2100 2101
    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
2102 2103

      {
mrs's avatar
mrs committed
2104 2105
	tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
	max = tsubst_expr (max, args, nargs, in_decl);
mrs's avatar
mrs committed
2106
	if (processing_template_decl)
mrs's avatar
mrs committed
2107 2108 2109
	  {
	    tree itype = make_node (INTEGER_TYPE);
	    TYPE_MIN_VALUE (itype) = size_zero_node;
mrs's avatar
mrs committed
2110 2111
	    TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
						integer_one_node);
mrs's avatar
mrs committed
2112 2113
	    return itype;
	  }
mrs's avatar
mrs committed
2114 2115

	max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
mrs's avatar
mrs committed
2116 2117
	return build_index_2_type (size_zero_node, max);
      }
mrs's avatar
mrs committed
2118 2119

    case TEMPLATE_TYPE_PARM:
2120
    case TEMPLATE_CONST_PARM:
mrs's avatar
mrs committed
2121
      {
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
	int idx;
	int level;

	if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
	  {
	    idx = TEMPLATE_TYPE_IDX (t);
	    level = TEMPLATE_TYPE_LEVEL (t);
	  }
	else
	  {
	    idx = TEMPLATE_CONST_IDX (t);
	    level = TEMPLATE_CONST_LEVEL (t);
	  }

	if (TREE_VEC_LENGTH (args) > 0) 
	  {
	    tree arg = NULL_TREE;

	    if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
	      {
		if (TREE_VEC_LENGTH (args) >= level - 1)
		  arg = TREE_VEC_ELT
		    (TREE_VEC_ELT (args, level - 1), idx);
	      }
	    else if (level == 1)
	      arg = TREE_VEC_ELT (args, idx);

	    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));
		else
		  return arg;
	      }
	  }

	/* If we get here, we must have been looking at a parm for a
	   more deeply nested template.  */
	my_friendly_assert((TREE_CODE (t) == TEMPLATE_CONST_PARM 
			    && TEMPLATE_CONST_LEVEL (t) > 1) 
			   || (TREE_CODE (t) == TEMPLATE_TYPE_PARM
			       && TEMPLATE_TYPE_LEVEL (t) > 1),
			   0);
	return t;
mrs's avatar
mrs committed
2168
      }
mrs's avatar
mrs committed
2169

2170 2171 2172 2173 2174 2175 2176 2177
    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 new_decl;
	tree parms;
2178
	tree spec;
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
	int i;

	/* We might already have an instance of this template. */
	tree instances = DECL_TEMPLATE_INSTANTIATIONS (t);
	tree ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, in_decl); 
	  
	for (; instances; instances = TREE_CHAIN (instances))
	  if (DECL_CLASS_CONTEXT (TREE_VALUE (instances)) == ctx)
	    return TREE_VALUE (instances);

	/* 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;
	DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
	new_decl = tsubst (decl, args, nargs, in_decl);
	DECL_RESULT (tmpl) = new_decl;
	DECL_TI_TEMPLATE (new_decl) = tmpl;
	TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
2204
	DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE;
jason's avatar
jason committed
2205
	SET_DECL_IMPLICIT_INSTANTIATION (tmpl);
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220

	/* The template parameters for this new template are all the
	   template parameters for the old template, except the
	   outermost level of parameters. */
	DECL_TEMPLATE_PARMS (tmpl)
	  = copy_node (DECL_TEMPLATE_PARMS (tmpl));
	for (parms = DECL_TEMPLATE_PARMS (tmpl);
	     TREE_CHAIN (parms) != NULL_TREE;
	     parms = TREE_CHAIN (parms))
	  TREE_CHAIN (parms) = copy_node (TREE_CHAIN (parms));

	/* Record this partial instantiation. */
	DECL_TEMPLATE_INSTANTIATIONS (t)
	  = perm_tree_cons (NULL_TREE, tmpl,
			    DECL_TEMPLATE_INSTANTIATIONS (t));
2221 2222

	DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
2223 2224
	return tmpl;
      }
mrs's avatar
mrs committed
2225 2226 2227

    case FUNCTION_DECL:
      {
mrs's avatar
mrs committed
2228
	tree r = NULL_TREE;
2229
	tree ctx;
mrs's avatar
mrs committed
2230 2231 2232

	int member;

mrs's avatar
mrs committed
2233 2234 2235
	if (DECL_CONTEXT (t) != NULL_TREE
	    && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
	  {
mrs's avatar
mrs committed
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
	    if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
	      member = 2;
	    else
	      member = 1;
	    ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
	    type = tsubst (type, args, nargs, in_decl);
	  }
	else
	  {
	    member = 0;
	    ctx = NULL_TREE;
	    type = tsubst (type, args, nargs, in_decl);
	  }
mrs's avatar
mrs committed
2249

mrs's avatar
mrs committed
2250 2251
	if (type == TREE_TYPE (t)
	    && (! member || ctx == DECL_CLASS_CONTEXT (t)))
mrs's avatar
mrs committed
2252 2253 2254 2255 2256
	  {
	    t = copy_node (t);
	    copy_lang_decl (t);
	    return t;
	  }
mrs's avatar
mrs committed
2257

mrs's avatar
mrs committed
2258 2259 2260
	/* Do we already have this instantiation?  */
	if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
	  {
2261
	    tree tmpl = DECL_TI_TEMPLATE (t);
mrs's avatar
mrs committed
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272
	    tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);

	    for (; decls; decls = TREE_CHAIN (decls))
	      if (TREE_TYPE (TREE_VALUE (decls)) == type
		  && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx)
		return TREE_VALUE (decls);
	  }

	/* 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
2273
           discrete model.  Instead, see add_maybe_template.  */
mrs's avatar
mrs committed
2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301

	r = copy_node (t);
	copy_lang_decl (r);
	TREE_TYPE (r) = type;

	DECL_CONTEXT (r)
	  = tsubst (DECL_CONTEXT (t), args, nargs, t);
	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
2302
	  }
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
	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
	       member template of a template class.  */
	    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
	      {
		/* 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.  */
jason's avatar
jason committed
2342 2343 2344 2345 2346 2347 2348
		/* FIXME The messed up thing here is that we get here with
		   full args and only one level of parms.  This is necessary
		   because when we partially instantiate a member template,
		   even though there's really only one level of parms left
		   we re-use the parms from the original template, which
		   have level 2.  When this is fixed we can remove the
		   add_to_template_args from instantiate_template.  */
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
		tree tparms = DECL_TEMPLATE_PARMS (tmpl);

		while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
		  tparms = TREE_CHAIN (tparms);

		my_friendly_assert (tparms != NULL_TREE
				    && TREE_CODE (tparms) == TREE_LIST,
				    0);
		tparms = TREE_VALUE (tparms);

		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)),
		   tparms,
		   TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC 
		   ? TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1) :
		   args, 
		   member);
	      }
	  }
mrs's avatar
mrs committed
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
	DECL_RTL (r) = 0;
	make_decl_rtl (r, NULL_PTR, 1);

	DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
	DECL_MAIN_VARIANT (r) = r;
	DECL_RESULT (r) = NULL_TREE;
	DECL_INITIAL (r) = NULL_TREE;

	TREE_STATIC (r) = 0;
	TREE_PUBLIC (r) = 1;
	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));

	/* Look for matching decls for the moment.  */
mrs's avatar
mrs committed
2396
	if (! member && ! flag_ansi_overloading)
mrs's avatar
mrs committed
2397
	  {
mrs's avatar
mrs committed
2398 2399 2400 2401 2402 2403 2404 2405
	    tree decls = lookup_name_nonclass (DECL_NAME (t));
	    tree d = NULL_TREE;
    
	    if (decls == NULL_TREE)
	      /* no match */;
	    else if (is_overloaded_fn (decls))
	      for (decls = get_first_fn (decls); decls;
		   decls = DECL_CHAIN (decls))
mrs's avatar
mrs committed
2406
		{
mrs's avatar
mrs committed
2407 2408
		  if (TREE_CODE (decls) == FUNCTION_DECL
		      && TREE_TYPE (decls) == type)
mrs's avatar
mrs committed
2409
		    {
mrs's avatar
mrs committed
2410 2411
		      d = decls;
		      break;
mrs's avatar
mrs committed
2412 2413 2414
		    }
		}

mrs's avatar
mrs committed
2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
	    if (d)
	      {
		int dcl_only = ! DECL_INITIAL (d);
		if (dcl_only)
		  DECL_INITIAL (r) = error_mark_node;
		duplicate_decls (r, d);
		r = d;
		if (dcl_only)
		  DECL_INITIAL (r) = 0;
	      }
	  }

	if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
	  {
	    tree tmpl = DECL_TI_TEMPLATE (t);
	    tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2431 2432 2433 2434
	    tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);

	    if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
	      argvec = add_to_template_args (DECL_TI_ARGS (tmpl), argvec);
mrs's avatar
mrs committed
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445

	    DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
	    *declsp = perm_tree_cons (argvec, r, *declsp);

	    /* If we have a preexisting version of this function, don't expand
	       the template version, use the other instead.  */
	    if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
	      SET_DECL_TEMPLATE_SPECIALIZATION (r);
	    else
	      SET_DECL_IMPLICIT_INSTANTIATION (r);

mrs's avatar
mrs committed
2446 2447
	    DECL_TEMPLATE_INSTANTIATIONS (tmpl)
	      = tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
mrs's avatar
mrs committed
2448 2449
	  }

mrs's avatar
mrs committed
2450 2451 2452 2453 2454 2455
	/* 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
2456 2457 2458 2459 2460
	return r;
      }

    case PARM_DECL:
      {
mrs's avatar
mrs committed
2461 2462
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
mrs's avatar
mrs committed
2463
	DECL_INITIAL (r) = TREE_TYPE (r);
mrs's avatar
mrs committed
2464
	DECL_CONTEXT (r) = NULL_TREE;
kenner's avatar
kenner committed
2465 2466 2467 2468 2469 2470
#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
2471 2472 2473 2474 2475
	if (TREE_CHAIN (t))
	  TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
	return r;
      }

mrs's avatar
mrs committed
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519
    case FIELD_DECL:
      {
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
	copy_lang_decl (r);
#if 0
	DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
#endif
	DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }

    case USING_DECL:
      {
	tree r = copy_node (t);
	DECL_INITIAL (r)
	  = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }

    case VAR_DECL:
      {
	tree r;
	tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);

	/* 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
2520 2521 2522 2523

	/* 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
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537

	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);
2538
	    tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
mrs's avatar
mrs committed
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548

	    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
2549 2550 2551
      if (t == TYPE_NAME (TREE_TYPE (t)))
	return TYPE_NAME (type);

mrs's avatar
mrs committed
2552 2553 2554
      {
	tree r = copy_node (t);
	TREE_TYPE (r) = type;
mrs's avatar
mrs committed
2555
	DECL_CONTEXT (r) = current_class_type;
mrs's avatar
mrs committed
2556 2557 2558 2559
	TREE_CHAIN (r) = NULL_TREE;
	return r;
      }	  

mrs's avatar
mrs committed
2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590
    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)
	  purpose = tsubst (purpose, args, nargs, in_decl);
	value = TREE_VALUE (t);
	if (value)
	  value = tsubst (value, args, nargs, in_decl);
	chain = TREE_CHAIN (t);
	if (chain && chain != void_type_node)
	  chain = tsubst (chain, args, nargs, in_decl);
	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
2591 2592
      if (type != NULL_TREE)
	{
2593 2594
	  /* A binfo node.  */

mrs's avatar
mrs committed
2595 2596 2597 2598 2599 2600
	  t = copy_node (t);

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

	  TREE_TYPE (t) = complete_type (type);
mrs's avatar
mrs committed
2601 2602 2603 2604 2605 2606 2607
	  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
2608 2609
	  return t;
	}
2610 2611

      /* Otherwise, a vector of template arguments.  */
mrs's avatar
mrs committed
2612 2613 2614
      {
	int len = TREE_VEC_LENGTH (t), need_new = 0, i;
	tree *elts = (tree *) alloca (len * sizeof (tree));
mrs's avatar
mrs committed
2615

2616
	bzero ((char *) elts, len * sizeof (tree));
mrs's avatar
mrs committed
2617 2618 2619

	for (i = 0; i < len; i++)
	  {
mrs's avatar
mrs committed
2620
	    elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl);
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638

	    if (TREE_CODE_CLASS (TREE_CODE (elts[i])) != 't'
		&& !uses_template_parms (elts[i]))
	      {
		/* 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;
		elts[i] = fold (build_expr_from_tree (elts[i]));
		processing_template_decl = saved_processing_template_decl; 
	      }

mrs's avatar
mrs committed
2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
	    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
2649
	
mrs's avatar
mrs committed
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
	return t;
      }
    case POINTER_TYPE:
    case REFERENCE_TYPE:
      {
	tree r;
	enum tree_code code;
	if (type == TREE_TYPE (t))
	  return t;

	code = TREE_CODE (t);
	if (code == POINTER_TYPE)
	  r = build_pointer_type (type);
	else
	  r = build_reference_type (type);
mrs's avatar
mrs committed
2665
	r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
mrs's avatar
mrs committed
2666 2667 2668 2669
	/* Will this ever be needed for TYPE_..._TO values?  */
	layout_type (r);
	return r;
      }
mrs's avatar
mrs committed
2670 2671 2672
    case OFFSET_TYPE:
      return build_offset_type
	(tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
mrs's avatar
mrs committed
2673 2674 2675
    case FUNCTION_TYPE:
    case METHOD_TYPE:
      {
2676
	tree values = TYPE_ARG_TYPES (t);
mrs's avatar
mrs committed
2677
	tree context = TYPE_CONTEXT (t);
mrs's avatar
merging  
mrs committed
2678 2679
	tree raises = TYPE_RAISES_EXCEPTIONS (t);
	tree fntype;
mrs's avatar
mrs committed
2680 2681 2682

	/* Don't bother recursing if we know it won't change anything.	*/
	if (values != void_list_node)
mrs's avatar
mrs committed
2683 2684 2685 2686 2687 2688 2689 2690 2691
	  {
	    /* This should probably be rewritten to use hash_tree_cons for
               the memory savings.  */
	    tree first = NULL_TREE;
	    tree last;

	    for (; values && values != void_list_node;
		 values = TREE_CHAIN (values))
	      {
mrs's avatar
mrs committed
2692 2693
		tree value = TYPE_MAIN_VARIANT (type_decays_to
		  (tsubst (TREE_VALUE (values), args, nargs, in_decl)));
mrs's avatar
mrs committed
2694 2695 2696
		/* 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
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
		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
2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
	if (context)
	  context = tsubst (context, args, nargs, in_decl);
	/* 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
2726
	    fntype = build_function_type (type, values);
mrs's avatar
mrs committed
2727 2728 2729 2730 2731
	  }
	else if (context == NULL_TREE)
	  {
	    tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
				args, nargs, in_decl);
mrs's avatar
merging  
mrs committed
2732 2733
	    fntype = build_cplus_method_type (base, type,
					      TREE_CHAIN (values));
mrs's avatar
mrs committed
2734 2735 2736
	  }
	else
	  {
mrs's avatar
merging  
mrs committed
2737 2738 2739 2740 2741 2742 2743
	    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
2744
	    if (TYPE_METHOD_BASETYPE (t))
mrs's avatar
merging  
mrs committed
2745 2746
	      TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
						      args, nargs, in_decl);
mrs's avatar
mrs committed
2747 2748 2749
	    /* Need to generate hash value.  */
	    my_friendly_abort (84);
	  }
mrs's avatar
merging  
mrs committed
2750 2751 2752 2753 2754 2755 2756 2757 2758
	fntype = build_type_variant (fntype,
				     TYPE_READONLY (t),
				     TYPE_VOLATILE (t));
	if (raises)
	  {
	    raises = tsubst (raises, args, nargs, in_decl);
	    fntype = build_exception_variant (fntype, raises);
	  }
	return fntype;
mrs's avatar
mrs committed
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
      }
    case ARRAY_TYPE:
      {
	tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
	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
2771
    case MINUS_EXPR:
mrs's avatar
mrs committed
2772 2773 2774 2775 2776 2777 2778 2779 2780
      return fold (build (TREE_CODE (t), TREE_TYPE (t),
			  tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
			  tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));

    case NEGATE_EXPR:
    case NOP_EXPR:
      return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
			   tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));

mrs's avatar
mrs committed
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800
    case TYPENAME_TYPE:
      {
	tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
	tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
	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
	(type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
      
    case ADDR_EXPR:
      return make_reference_declarator
	(type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));

    case ARRAY_REF:
      return build_parse_node
	(ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
mrs's avatar
mrs committed
2801
	 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
mrs's avatar
mrs committed
2802 2803

    case CALL_EXPR:
mrs's avatar
merging  
mrs committed
2804 2805 2806 2807 2808
      return make_call_declarator
	(tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
	 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
	 TREE_OPERAND (t, 2),
	 tsubst (TREE_TYPE (t), args, nargs, in_decl));
mrs's avatar
mrs committed
2809

mrs's avatar
mrs committed
2810 2811 2812 2813 2814
    case SCOPE_REF:
      return build_parse_node
	(TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
	 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));

mrs's avatar
mrs committed
2815
    default:
mrs's avatar
mrs committed
2816
      sorry ("use of `%s' in template",
mrs's avatar
mrs committed
2817 2818 2819 2820 2821
	     tree_code_name [(int) TREE_CODE (t)]);
      return error_mark_node;
    }
}

mrs's avatar
mrs committed
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
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
2832
tree
mrs's avatar
mrs committed
2833
do_poplevel ()
mrs's avatar
mrs committed
2834
{
mrs's avatar
mrs committed
2835
  tree t;
2836
  int saved_warn_unused;
mrs's avatar
mrs committed
2837

2838 2839 2840 2841 2842
  if (processing_template_decl)
    {
      saved_warn_unused = warn_unused;
      warn_unused = 0;
    }
mrs's avatar
mrs committed
2843
  expand_end_bindings (getdecls (), kept_level_p (), 1);
2844 2845
  if (processing_template_decl)
    warn_unused = saved_warn_unused;
mrs's avatar
mrs committed
2846 2847 2848 2849
  t = poplevel (kept_level_p (), 1, 0);
  pop_momentary ();
  return t;
}
mrs's avatar
mrs committed
2850

mrs's avatar
mrs committed
2851 2852
tree
tsubst_copy (t, args, nargs, in_decl)
2853
     tree t, args;
mrs's avatar
mrs committed
2854 2855 2856 2857
     int nargs;
     tree in_decl;
{
  enum tree_code code;
mrs's avatar
mrs committed
2858

mrs's avatar
mrs committed
2859 2860 2861 2862
  if (t == NULL_TREE || t == error_mark_node)
    return t;

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

mrs's avatar
mrs committed
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
  switch (code)
    {
    case PARM_DECL:
      return do_identifier (DECL_NAME (t), 0);

    case CONST_DECL:
    case FIELD_DECL:
      if (DECL_CONTEXT (t))
	{
	  tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
mrs's avatar
mrs committed
2874 2875 2876
	  if (ctx == current_function_decl)
	    return lookup_name (DECL_NAME (t), 0);
	  else if (ctx != DECL_CONTEXT (t))
mrs's avatar
mrs committed
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
	    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))
	t = tsubst (t, args, nargs, in_decl);
      mark_used (t);
      return t;

2888 2889 2890 2891 2892 2893
    case TEMPLATE_DECL:
      if (is_member_template (t))
	return tsubst (t, args, nargs, in_decl);
      else
	return t;

mrs's avatar
mrs committed
2894 2895 2896 2897 2898 2899 2900
#if 0
    case IDENTIFIER_NODE:
      return do_identifier (t, 0);
#endif
      
    case CAST_EXPR:
    case REINTERPRET_CAST_EXPR:
mrs's avatar
mrs committed
2901 2902 2903
    case CONST_CAST_EXPR:
    case STATIC_CAST_EXPR:
    case DYNAMIC_CAST_EXPR:
mrs's avatar
mrs committed
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
      return build1
	(code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));

    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
2915
    case BIT_NOT_EXPR:
mrs's avatar
mrs committed
2916 2917 2918 2919
    case ADDR_EXPR:
    case CONVERT_EXPR:      /* Unary + */
    case SIZEOF_EXPR:
    case ARROW_EXPR:
mrs's avatar
mrs committed
2920
    case THROW_EXPR:
mrs's avatar
mrs committed
2921
    case TYPEID_EXPR:
mrs's avatar
mrs committed
2922 2923 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 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
      return build1
	(code, NULL_TREE,
	 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));

    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
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));

    case CALL_EXPR:
      {
	tree fn = TREE_OPERAND (t, 0);
	if (really_overloaded_fn (fn))
	  fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl);
	else
	  fn = tsubst_copy (fn, args, nargs, in_decl);
	return build_nt
	  (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
	   NULL_TREE);
      }

    case METHOD_CALL_EXPR:
      {
	tree name = TREE_OPERAND (t, 0);
	if (TREE_CODE (name) == BIT_NOT_EXPR)
	  {
	    name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
mrs's avatar
mrs committed
2984
	    name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
mrs's avatar
mrs committed
2985 2986 2987 2988 2989 2990 2991
	  }
	else if (TREE_CODE (name) == SCOPE_REF
		 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
	  {
	    tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
	    name = TREE_OPERAND (name, 1);
	    name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
mrs's avatar
mrs committed
2992
	    name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
mrs's avatar
mrs committed
2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
	    name = build_nt (SCOPE_REF, base, name);
	  }
	else
	  name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
	return build_nt
	  (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
	   tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
	   NULL_TREE);
      }

    case COND_EXPR:
    case MODOP_EXPR:
      return build_nt
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));

    case NEW_EXPR:
      {
	tree r = build_nt
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
	NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
	return r;
      }

    case DELETE_EXPR:
      {
	tree r = build_nt
	(code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
	 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
	DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
	DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
	return r;
      }

3030 3031 3032 3033 3034 3035 3036 3037
    case TEMPLATE_ID_EXPR:
      {
	tree r = lookup_template_function
	  (tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
	   tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
	return r;
      }

mrs's avatar
mrs committed
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075
    case TREE_LIST:
      {
	tree purpose, value, chain;

	if (t == void_list_node)
	  return t;

	purpose = TREE_PURPOSE (t);
	if (purpose)
	  purpose = tsubst_copy (purpose, args, nargs, in_decl);
	value = TREE_VALUE (t);
	if (value)
	  value = tsubst_copy (value, args, nargs, in_decl);
	chain = TREE_CHAIN (t);
	if (chain && chain != void_type_node)
	  chain = tsubst_copy (chain, args, nargs, in_decl);
	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:
    case TEMPLATE_CONST_PARM:
    case POINTER_TYPE:
    case REFERENCE_TYPE:
    case OFFSET_TYPE:
    case FUNCTION_TYPE:
    case METHOD_TYPE:
    case ARRAY_TYPE:
    case TYPENAME_TYPE:
      return tsubst (t, args, nargs, in_decl);

mrs's avatar
mrs committed
3076 3077 3078 3079 3080 3081 3082
    case IDENTIFIER_NODE:
      if (IDENTIFIER_TYPENAME_P (t))
	return build_typename_overload
	  (tsubst (TREE_TYPE (t), args, nargs, in_decl));
      else
	return t;

mrs's avatar
mrs committed
3083 3084 3085 3086 3087
    case CONSTRUCTOR:
      return build
	(CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
	 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));

mrs's avatar
mrs committed
3088 3089 3090 3091 3092 3093 3094
    default:
      return t;
    }
}

tree
tsubst_expr (t, args, nargs, in_decl)
3095
     tree t, args;
mrs's avatar
mrs committed
3096 3097 3098 3099 3100 3101
     int nargs;
     tree in_decl;
{
  if (t == NULL_TREE || t == error_mark_node)
    return t;

mrs's avatar
mrs committed
3102
  if (processing_template_decl)
mrs's avatar
mrs committed
3103 3104 3105
    return tsubst_copy (t, args, nargs, in_decl);

  switch (TREE_CODE (t))
mrs's avatar
mrs committed
3106
    {
mrs's avatar
mrs committed
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131
    case RETURN_STMT:
      lineno = TREE_COMPLEXITY (t);
      emit_line_note (input_filename, lineno);
      c_expand_return
	(tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
      finish_stmt ();
      break;

    case EXPR_STMT:
      lineno = TREE_COMPLEXITY (t);
      emit_line_note (input_filename, lineno);
      t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
      /* Do default conversion if safe and possibly important,
	 in case within ({...}).  */
      if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
	  || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
	t = default_conversion (t);
      cplus_expand_expr_stmt (t);
      clear_momentary ();
      finish_stmt ();
      break;

    case DECL_STMT:
      {
	int i = suspend_momentary ();
mrs's avatar
mrs committed
3132
	tree dcl, init;
mrs's avatar
mrs committed
3133 3134 3135 3136 3137 3138

	lineno = TREE_COMPLEXITY (t);
	emit_line_note (input_filename, lineno);
	dcl = start_decl
	  (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
	   tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
mrs's avatar
merging  
mrs committed
3139 3140
	   TREE_OPERAND (t, 2) != 0);
	init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
mrs's avatar
mrs committed
3141
	cp_finish_decl
mrs's avatar
mrs committed
3142
	  (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
mrs's avatar
mrs committed
3143 3144 3145
	resume_momentary (i);
	return dcl;
      }
mrs's avatar
mrs committed
3146

mrs's avatar
mrs committed
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158
    case FOR_STMT:
      {
	tree tmp;
	int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
			  && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
	int cond_scope = (TREE_OPERAND (t, 1)
			  && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);

	lineno = TREE_COMPLEXITY (t);
	emit_line_note (input_filename, lineno);
	if (init_scope)
	  do_pushlevel ();
mrs's avatar
mrs committed
3159 3160
	for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
	  tsubst_expr (tmp, args, nargs, in_decl);
mrs's avatar
mrs committed
3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188
	emit_nop ();
	emit_line_note (input_filename, lineno);
	expand_start_loop_continue_elsewhere (1); 

	if (cond_scope)
	  do_pushlevel ();
	tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
	emit_line_note (input_filename, lineno);
	if (tmp)
	  expand_exit_loop_if_false (0, condition_conversion (tmp));

	if (! cond_scope)
	  do_pushlevel ();
	tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
	do_poplevel ();

	emit_line_note (input_filename, lineno);
	expand_loop_continue_here ();
	tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
	if (tmp)
	  cplus_expand_expr_stmt (tmp);

	expand_end_loop ();
	if (init_scope)
	  do_poplevel ();
	finish_stmt ();
      }
      break;
mrs's avatar
mrs committed
3189

mrs's avatar
mrs committed
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214
    case WHILE_STMT:
      {
	tree cond;

	lineno = TREE_COMPLEXITY (t);
	emit_nop ();
	emit_line_note (input_filename, lineno);
	expand_start_loop (1); 

	cond = TREE_OPERAND (t, 0);
	if (TREE_CODE (cond) == DECL_STMT)
	  do_pushlevel ();
	cond = tsubst_expr (cond, args, nargs, in_decl);
	emit_line_note (input_filename, lineno);
	expand_exit_loop_if_false (0, condition_conversion (cond));

	if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
	  do_pushlevel ();
	tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
	do_poplevel ();

	expand_end_loop ();
	finish_stmt ();
      }
      break;
mrs's avatar
mrs committed
3215

mrs's avatar
mrs committed
3216 3217 3218
    case DO_STMT:
      {
	tree cond;
mrs's avatar
mrs committed
3219

mrs's avatar
mrs committed
3220 3221 3222 3223
	lineno = TREE_COMPLEXITY (t);
	emit_nop ();
	emit_line_note (input_filename, lineno);
	expand_start_loop_continue_elsewhere (1); 
mrs's avatar
mrs committed
3224

mrs's avatar
mrs committed
3225 3226
	tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
	expand_loop_continue_here ();
mrs's avatar
mrs committed
3227

mrs's avatar
mrs committed
3228 3229 3230 3231
	cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
	emit_line_note (input_filename, lineno);
	expand_exit_loop_if_false (0, condition_conversion (cond));
	expand_end_loop ();
mrs's avatar
mrs committed
3232

mrs's avatar
mrs committed
3233 3234 3235 3236
	clear_momentary ();
	finish_stmt ();
      }
      break;
mrs's avatar
mrs committed
3237

mrs's avatar
mrs committed
3238
    case IF_STMT:
mrs's avatar
mrs committed
3239
      {
mrs's avatar
mrs committed
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
	tree tmp;
	int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);

	lineno = TREE_COMPLEXITY (t);
	if (cond_scope)
	  do_pushlevel ();
	tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
	emit_line_note (input_filename, lineno);
	expand_start_cond (condition_conversion (tmp), 0);
	
	if (tmp = TREE_OPERAND (t, 1), tmp)
	  tsubst_expr (tmp, args, nargs, in_decl);
mrs's avatar
mrs committed
3252

mrs's avatar
mrs committed
3253
	if (tmp = TREE_OPERAND (t, 2), tmp)
mrs's avatar
mrs committed
3254
	  {
mrs's avatar
mrs committed
3255 3256
	    expand_start_else ();
	    tsubst_expr (tmp, args, nargs, in_decl);
mrs's avatar
mrs committed
3257 3258
	  }

mrs's avatar
mrs committed
3259
	expand_end_cond ();
mrs's avatar
mrs committed
3260

mrs's avatar
mrs committed
3261 3262
	if (cond_scope)
	  do_poplevel ();
mrs's avatar
mrs committed
3263

mrs's avatar
mrs committed
3264
	finish_stmt ();
mrs's avatar
mrs committed
3265
      }
mrs's avatar
mrs committed
3266
      break;
mrs's avatar
mrs committed
3267

mrs's avatar
mrs committed
3268 3269 3270
    case COMPOUND_STMT:
      {
	tree substmt = TREE_OPERAND (t, 0);
mrs's avatar
mrs committed
3271

mrs's avatar
mrs committed
3272
	lineno = TREE_COMPLEXITY (t);
mrs's avatar
mrs committed
3273

mrs's avatar
mrs committed
3274 3275
	if (COMPOUND_STMT_NO_SCOPE (t) == 0)
	  do_pushlevel ();
mrs's avatar
mrs committed
3276

mrs's avatar
mrs committed
3277 3278
	for (; substmt; substmt = TREE_CHAIN (substmt))
	  tsubst_expr (substmt, args, nargs, in_decl);
mrs's avatar
mrs committed
3279

mrs's avatar
mrs committed
3280 3281 3282 3283
	if (COMPOUND_STMT_NO_SCOPE (t) == 0)
	  do_poplevel ();
      }
      break;
mrs's avatar
mrs committed
3284

mrs's avatar
mrs committed
3285 3286 3287 3288 3289 3290
    case BREAK_STMT:
      lineno = TREE_COMPLEXITY (t);
      emit_line_note (input_filename, lineno);
      if (! expand_exit_something ())
	error ("break statement not within loop or switch");
      break;
mrs's avatar
mrs committed
3291

mrs's avatar
mrs committed
3292 3293 3294 3295 3296 3297 3298
    case CONTINUE_STMT:
      lineno = TREE_COMPLEXITY (t);
      emit_line_note (input_filename, lineno);
      if (! expand_continue_loop (0))
	error ("continue statement not within a loop");
      break;

mrs's avatar
mrs committed
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313
    case SWITCH_STMT:
      {
	tree val, tmp;
	int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);

	lineno = TREE_COMPLEXITY (t);
	if (cond_scope)
	  do_pushlevel ();
	val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
	emit_line_note (input_filename, lineno);
	c_expand_start_case (val);
	push_switch ();
	
	if (tmp = TREE_OPERAND (t, 1), tmp)
	  tsubst_expr (tmp, args, nargs, in_decl);
mrs's avatar
mrs committed
3314

mrs's avatar
mrs committed
3315 3316
	expand_end_case (val);
	pop_switch ();
mrs's avatar
mrs committed
3317

mrs's avatar
mrs committed
3318 3319
	if (cond_scope)
	  do_poplevel ();
mrs's avatar
mrs committed
3320

mrs's avatar
mrs committed
3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349
	finish_stmt ();
      }
      break;

    case CASE_LABEL:
      do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
	       tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
      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);
      emit_line_note (input_filename, lineno);
      if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
	{
	  tree decl = lookup_label (TREE_OPERAND (t, 0));
	  TREE_USED (decl) = 1;
	  expand_goto (decl);
	}
      else
	expand_computed_goto
	  (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
      break;
mrs's avatar
mrs committed
3350 3351 3352 3353 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 3380 3381

    case TRY_BLOCK:
      lineno = TREE_COMPLEXITY (t);
      emit_line_note (input_filename, lineno);
      expand_start_try_stmts ();
      tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
      expand_start_all_catch ();
      {
	tree handler = TREE_OPERAND (t, 1);
	for (; handler; handler = TREE_CHAIN (handler))
	  tsubst_expr (handler, args, nargs, in_decl);
      }
      expand_end_all_catch ();
      break;

    case HANDLER:
      lineno = TREE_COMPLEXITY (t);
      do_pushlevel ();
      if (TREE_OPERAND (t, 0))
	{
	  tree d = TREE_OPERAND (t, 0);
	  expand_start_catch_block
	    (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
	     tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
	}
      else
	expand_start_catch_block (NULL_TREE, NULL_TREE);
      tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
      expand_end_catch_block ();
      do_poplevel ();
      break;

mrs's avatar
mrs committed
3382 3383 3384 3385
    case TAG_DEFN:
      lineno = TREE_COMPLEXITY (t);
      t = TREE_TYPE (t);
      if (TREE_CODE (t) == ENUMERAL_TYPE)
3386
	tsubst_enum (t, args, nargs, NULL);
mrs's avatar
mrs committed
3387 3388
      break;

mrs's avatar
mrs committed
3389 3390 3391 3392
    default:
      return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
    }
  return NULL_TREE;
mrs's avatar
mrs committed
3393 3394
}

mrs's avatar
mrs committed
3395 3396
tree
instantiate_template (tmpl, targ_ptr)
3397
     tree tmpl, targ_ptr;
mrs's avatar
mrs committed
3398
{
mrs's avatar
mrs committed
3399 3400 3401 3402 3403
  tree fndecl;
  int i, len;
  struct obstack *old_fmp_obstack;
  extern struct obstack *function_maybepermanent_obstack;

3404 3405 3406 3407 3408 3409 3410 3411 3412 3413
  my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);

  if (DECL_FUNCTION_TEMPLATE_P (tmpl))
    {
      tree specs;
      
      /* Check to see if there is a matching specialization. */
      for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
	   specs != NULL_TREE;
	   specs = TREE_CHAIN (specs))
jason's avatar
jason committed
3414
	if (comp_template_args (TREE_PURPOSE (specs), targ_ptr))
3415 3416 3417
	  return TREE_VALUE (specs);
    }

mrs's avatar
mrs committed
3418 3419 3420
  push_obstacks (&permanent_obstack, &permanent_obstack);
  old_fmp_obstack = function_maybepermanent_obstack;
  function_maybepermanent_obstack = &permanent_obstack;
mrs's avatar
mrs committed
3421

3422
  len = DECL_NTPARMS (tmpl);
mrs's avatar
mrs committed
3423

mrs's avatar
mrs committed
3424 3425
  i = len;
  while (i--)
mrs's avatar
mrs committed
3426
    {
3427
      tree t = TREE_VEC_ELT (targ_ptr, i);
mrs's avatar
mrs committed
3428 3429 3430
      if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
	{
	  tree nt = target_type (t);
mrs's avatar
mrs committed
3431
	  if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
mrs's avatar
mrs committed
3432 3433 3434 3435 3436 3437 3438
	    {
	      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;
	    }
	}
3439
      TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
mrs's avatar
mrs committed
3440
    }
3441
  targ_ptr = copy_to_permanent (targ_ptr);
mrs's avatar
mrs committed
3442

3443 3444 3445
  if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
    targ_ptr = add_to_template_args (DECL_TI_ARGS (tmpl), targ_ptr);

mrs's avatar
mrs committed
3446 3447
  /* substitute template parameters */
  fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
mrs's avatar
mrs committed
3448

mrs's avatar
mrs committed
3449 3450 3451
  if (flag_external_templates)
    add_pending_template (fndecl);

mrs's avatar
mrs committed
3452 3453 3454
 out:
  function_maybepermanent_obstack = old_fmp_obstack;
  pop_obstacks ();
mrs's avatar
mrs committed
3455

mrs's avatar
mrs committed
3456
  return fndecl;
mrs's avatar
mrs committed
3457
}
mrs's avatar
mrs committed
3458 3459

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

void
mrs's avatar
mrs committed
3462 3463
overload_template_name (type)
     tree type;
mrs's avatar
mrs committed
3464
{
mrs's avatar
mrs committed
3465 3466
  tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
  tree decl;
mrs's avatar
mrs committed
3467

mrs's avatar
mrs committed
3468 3469 3470
  if (IDENTIFIER_CLASS_VALUE (id)
      && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
    return;
mrs's avatar
mrs committed
3471

mrs's avatar
mrs committed
3472 3473 3474
  decl = build_decl (TYPE_DECL, id, type);
  SET_DECL_ARTIFICIAL (decl);
  pushdecl_class_level (decl);
mrs's avatar
mrs committed
3475 3476
}

3477 3478 3479 3480
/* Like type_unfication but designed specially to handle conversion
   operators.  */

int
3481 3482
fn_type_unification (fn, explicit_targs, targs, args, return_type, strict)
     tree fn, explicit_targs, targs, args, return_type;
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494
     int strict;
{
  int i, dummy = 0;
  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.  */
3495
      fn_arg_types = scratch_tree_cons (NULL_TREE, 
3496 3497
				TREE_TYPE (TREE_TYPE (fn)),
				fn_arg_types);
3498
      decl_arg_types = scratch_tree_cons (NULL_TREE,
3499 3500 3501 3502 3503 3504 3505 3506
				  return_type,
				  decl_arg_types);
    }

  i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn), 
			&TREE_VEC_ELT (targs, 0), 
			fn_arg_types,
			decl_arg_types,
3507 3508
			explicit_targs,
			&dummy, strict, 0);
3509 3510 3511 3512 3513

  return i;
}


mrs's avatar
mrs committed
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531
/* 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
3532 3533 3534 3535
   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
3536 3537

int
3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569
type_unification (tparms, targs, parms, args, targs_in, nsubsts,
		  strict, allow_incomplete)
     tree tparms, *targs, parms, args, targs_in;
     int *nsubsts, strict, allow_incomplete;
{
  int ntparms = TREE_VEC_LENGTH (tparms);
  tree t;
  int i;
  int r;

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

  /* Insert any explicit template arguments.  They are encoded as the
     operands of NOP_EXPRs so that unify can tell that they are
     explicit arguments.  */
  for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
    targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VALUE (t));

  r = type_unification_real (tparms, targs, parms, args, nsubsts, 0,
			     strict, allow_incomplete); 

  for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
    if (TREE_CODE (targs[i]) == NOP_EXPR)
      targs[i] = TREE_OPERAND (targs[i], 0);

  return r;
}


int
type_unification_real (tparms, targs, parms, args, nsubsts, subr,
		       strict, allow_incomplete)
mrs's avatar
mrs committed
3570
     tree tparms, *targs, parms, args;
3571
     int *nsubsts, subr, strict, allow_incomplete;
mrs's avatar
mrs committed
3572 3573 3574 3575 3576 3577
{
  tree parm, arg;
  int i;
  int ntparms = TREE_VEC_LENGTH (tparms);

  my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
3578 3579
  my_friendly_assert (parms == NULL_TREE 
		      || TREE_CODE (parms) == TREE_LIST, 290);
mrs's avatar
mrs committed
3580
  /* ARGS could be NULL (via a call from parse.y to
mrs's avatar
mrs committed
3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599
     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
3600

3601 3602 3603 3604 3605
      /* 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
3606
	{
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632
	  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 if (arg)
	    {
	      if (can_convert_arg (parm, type, arg))
		continue;
	    }
	  else
	    {
	      if (can_convert (parm, type))
		continue;
	    }

mrs's avatar
mrs committed
3633 3634 3635
	  return 1;
	}
	
mrs's avatar
mrs committed
3636 3637 3638 3639 3640 3641 3642 3643 3644
#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
3645 3646 3647 3648 3649 3650 3651 3652 3653 3654
	  if (TREE_CODE (arg) == TREE_LIST
	      && TREE_TYPE (arg) == unknown_type_node
	      && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
	    {
	      int nsubsts, ntparms;
	      tree *targs;

	      /* Have to back unify here */
	      arg = TREE_VALUE (arg);
	      nsubsts = 0;
3655
	      ntparms = DECL_NTPARMS (arg);
mrs's avatar
mrs committed
3656
	      targs = (tree *) alloca (sizeof (tree) * ntparms);
3657
	      parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
3658 3659 3660 3661 3662 3663
	      return 
		type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg), 
				  targs,
				  TYPE_ARG_TYPES (TREE_TYPE (arg)),
				  parm, NULL_TREE, &nsubsts, strict,
				  allow_incomplete); 
mrs's avatar
mrs committed
3664
	    }
mrs's avatar
mrs committed
3665 3666 3667
	  arg = TREE_TYPE (arg);
	}
#endif
mrs's avatar
mrs committed
3668
      if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
mrs's avatar
mrs committed
3669 3670
	arg = TREE_TYPE (arg);

mrs's avatar
mrs committed
3671
      if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
3672 3673 3674 3675 3676 3677 3678 3679 3680
	{
	  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
3681

mrs's avatar
mrs committed
3682
      switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
mrs's avatar
mrs committed
3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702
	{
	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])
	{
3703 3704
	  if (!allow_incomplete)
	    error ("incomplete type unification");
mrs's avatar
mrs committed
3705 3706 3707 3708 3709 3710
	  return 2;
	}
  return 0;
}

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

mrs's avatar
mrs committed
3712
static int
mrs's avatar
mrs committed
3713
unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
mrs's avatar
mrs committed
3714
     tree tparms, *targs, parm, arg;
mrs's avatar
mrs committed
3715
     int *nsubsts, ntparms, strict;
mrs's avatar
mrs committed
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734
{
  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;

  switch (TREE_CODE (parm))
    {
3735 3736 3737 3738 3739 3740
    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
3741 3742 3743
    case TEMPLATE_TYPE_PARM:
      (*nsubsts)++;
      idx = TEMPLATE_TYPE_IDX (parm);
3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754
      /* 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
3755 3756 3757
      if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
		     || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
	return 1;
mrs's avatar
mrs committed
3758
#if 0
mrs's avatar
mrs committed
3759 3760 3761 3762 3763 3764 3765
      /* 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
3766 3767 3768 3769 3770 3771 3772
#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
3773
      /* Simple cases: Value already set, does match or doesn't.  */
3774 3775 3776 3777
      if (targs[idx] == arg 
	  || (targs[idx] 
	      && TREE_CODE (targs[idx]) == NOP_EXPR 
	      && TREE_OPERAND (targs[idx], 0) == arg))
mrs's avatar
mrs committed
3778 3779 3780 3781 3782 3783 3784 3785
	return 0;
      else if (targs[idx])
	return 1;
      targs[idx] = arg;
      return 0;
    case TEMPLATE_CONST_PARM:
      (*nsubsts)++;
      idx = TEMPLATE_CONST_IDX (parm);
mrs's avatar
mrs committed
3786
      if (targs[idx])
mrs's avatar
mrs committed
3787
	{
mrs's avatar
mrs committed
3788 3789 3790 3791 3792 3793 3794
	  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
3795 3796 3797 3798 3799 3800
	}

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

    case POINTER_TYPE:
mrs's avatar
mrs committed
3801 3802
      if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
	return unify (tparms, targs, ntparms, parm,
mrs's avatar
mrs committed
3803
		      TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
mrs's avatar
mrs committed
3804

mrs's avatar
mrs committed
3805 3806 3807
      if (TREE_CODE (arg) != POINTER_TYPE)
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
mrs's avatar
mrs committed
3808
		    nsubsts, strict);
mrs's avatar
mrs committed
3809 3810

    case REFERENCE_TYPE:
mrs's avatar
mrs committed
3811 3812
      if (TREE_CODE (arg) == REFERENCE_TYPE)
	arg = TREE_TYPE (arg);
mrs's avatar
mrs committed
3813 3814
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
		    nsubsts, strict);
mrs's avatar
mrs committed
3815 3816 3817 3818 3819

    case ARRAY_TYPE:
      if (TREE_CODE (arg) != ARRAY_TYPE)
	return 1;
      if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
mrs's avatar
mrs committed
3820
		 nsubsts, strict) != 0)
mrs's avatar
mrs committed
3821 3822
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
mrs's avatar
mrs committed
3823
		    nsubsts, strict);
mrs's avatar
mrs committed
3824 3825

    case REAL_TYPE:
mrs's avatar
mrs committed
3826
    case COMPLEX_TYPE:
mrs's avatar
mrs committed
3827
    case INTEGER_TYPE:
brendan's avatar
brendan committed
3828
    case BOOLEAN_TYPE:
mrs's avatar
mrs committed
3829 3830 3831 3832
      if (TREE_CODE (arg) != TREE_CODE (parm))
	return 1;

      if (TREE_CODE (parm) == INTEGER_TYPE)
mrs's avatar
mrs committed
3833 3834
	{
	  if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
mrs's avatar
mrs committed
3835 3836
	      && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
			TYPE_MIN_VALUE (arg), nsubsts, strict))
mrs's avatar
mrs committed
3837 3838
	    return 1;
	  if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
mrs's avatar
mrs committed
3839 3840
	      && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
			TYPE_MAX_VALUE (arg), nsubsts, strict))
mrs's avatar
mrs committed
3841 3842
	    return 1;
	}
3843 3844 3845 3846
      else if (TREE_CODE (parm) == REAL_TYPE
	       && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm))
	return 1;

mrs's avatar
mrs committed
3847 3848 3849 3850 3851
      /* 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
3852
      /* Type INTEGER_CST can come from ordinary constant template args.  */
mrs's avatar
mrs committed
3853
    case INTEGER_CST:
mrs's avatar
mrs committed
3854 3855 3856
      while (TREE_CODE (arg) == NOP_EXPR)
	arg = TREE_OPERAND (arg, 0);

mrs's avatar
mrs committed
3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867
      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)),
mrs's avatar
mrs committed
3868
		      nsubsts, strict);
mrs's avatar
mrs committed
3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880
      }

    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),
mrs's avatar
mrs committed
3881
		     nsubsts, strict))
mrs's avatar
mrs committed
3882 3883 3884 3885 3886
	    return 1;
	return 0;
      }

    case RECORD_TYPE:
mrs's avatar
mrs committed
3887
      if (TYPE_PTRMEMFUNC_FLAG (parm))
mrs's avatar
mrs committed
3888
	return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
mrs's avatar
mrs committed
3889
		      arg, nsubsts, strict);
mrs's avatar
mrs committed
3890

mrs's avatar
mrs committed
3891
      /* Allow trivial conversions.  */
mrs's avatar
mrs committed
3892
      if (TREE_CODE (arg) != RECORD_TYPE
mrs's avatar
mrs committed
3893 3894 3895
	  || TYPE_READONLY (parm) < TYPE_READONLY (arg)
	  || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
	return 1;
mrs's avatar
mrs committed
3896

mrs's avatar
mrs committed
3897
      if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
mrs's avatar
mrs committed
3898
	{
mrs's avatar
mrs committed
3899
	  tree t = NULL_TREE;
mrs's avatar
mrs committed
3900
	  if (flag_ansi_overloading && ! strict)
mrs's avatar
mrs committed
3901
	    t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
mrs's avatar
mrs committed
3902 3903 3904
	  else if
	    (CLASSTYPE_TEMPLATE_INFO (arg)
	     && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
mrs's avatar
mrs committed
3905 3906
	    t = arg;
	  if (! t || t == error_mark_node)
mrs's avatar
mrs committed
3907
	    return 1;
mrs's avatar
mrs committed
3908

mrs's avatar
mrs committed
3909
	  return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
mrs's avatar
mrs committed
3910
			CLASSTYPE_TI_ARGS (t), nsubsts, strict);
mrs's avatar
mrs committed
3911 3912 3913
	}
      else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
	return 1;
mrs's avatar
mrs committed
3914
      return 0;
mrs's avatar
mrs committed
3915 3916 3917 3918 3919 3920 3921 3922 3923 3924

    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
3925
      if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
mrs's avatar
mrs committed
3926
		 TREE_TYPE (arg), nsubsts, strict))
mrs's avatar
mrs committed
3927
	return 1;
3928 3929 3930
      return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
				    TYPE_ARG_TYPES (arg), nsubsts, 1, 
				    strict, 0);
mrs's avatar
mrs committed
3931 3932 3933 3934 3935

    case OFFSET_TYPE:
      if (TREE_CODE (arg) != OFFSET_TYPE)
	return 1;
      if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
mrs's avatar
mrs committed
3936
		 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
mrs's avatar
mrs committed
3937 3938
	return 1;
      return unify (tparms, targs, ntparms, TREE_TYPE (parm),
mrs's avatar
mrs committed
3939
		    TREE_TYPE (arg), nsubsts, strict);
mrs's avatar
mrs committed
3940

jason's avatar
merge  
jason committed
3941 3942 3943 3944 3945
    case CONST_DECL:
      if (arg != decl_constant_value (parm))
	return 1;
      return 0;

mrs's avatar
mrs committed
3946 3947 3948 3949 3950 3951 3952
    default:
      sorry ("use of `%s' in template type unification",
	     tree_code_name [(int) TREE_CODE (parm)]);
      return 1;
    }
}

mrs's avatar
mrs committed
3953
void
mrs's avatar
mrs committed
3954
mark_decl_instantiated (result, extern_p)
mrs's avatar
mrs committed
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966
     tree result;
     int extern_p;
{
  if (DECL_TEMPLATE_INSTANTIATION (result))
    SET_DECL_EXPLICIT_INSTANTIATION (result);
  TREE_PUBLIC (result) = 1;

  if (! extern_p)
    {
      DECL_INTERFACE_KNOWN (result) = 1;
      DECL_NOT_REALLY_EXTERN (result) = 1;
    }
mrs's avatar
mrs committed
3967 3968
  else if (TREE_CODE (result) == FUNCTION_DECL)
    mark_inline_for_output (result);
mrs's avatar
mrs committed
3969 3970
}

mrs's avatar
mrs committed
3971 3972 3973 3974 3975 3976 3977 3978 3979 3980
/* Given two function templates PAT1 and PAT2, return:

   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
more_specialized (pat1, pat2)
     tree pat1, pat2;
{
3981
  tree targs;
mrs's avatar
mrs committed
3982
  int winner = 0;
mrs's avatar
mrs committed
3983

mrs's avatar
mrs committed
3984 3985 3986 3987 3988
  targs = get_bindings (pat1, pat2);
  if (targs)
    {
      --winner;
    }
mrs's avatar
mrs committed
3989

mrs's avatar
mrs committed
3990 3991 3992 3993 3994
  targs = get_bindings (pat2, pat1);
  if (targs)
    {
      ++winner;
    }
mrs's avatar
mrs committed
3995

mrs's avatar
mrs committed
3996 3997
  return winner;
}
mrs's avatar
mrs committed
3998

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

mrs's avatar
mrs committed
4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019
   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
    (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
  if (targs)
    --winner;

  targs = get_class_bindings
    (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
  if (targs)
mrs's avatar
mrs committed
4020 4021 4022 4023
    ++winner;

  return winner;
}
mrs's avatar
mrs committed
4024 4025 4026 4027

/* Return the template arguments that will produce the function signature
   DECL from the function template FN.  */

4028
tree 
mrs's avatar
mrs committed
4029 4030 4031
get_bindings (fn, decl)
     tree fn, decl;
{
4032
  int ntparms = DECL_NTPARMS (fn);
4033
  tree targs = make_scratch_vec (ntparms);
4034 4035
  int i;

4036
  i = fn_type_unification (fn, NULL_TREE, targs, 
4037 4038 4039 4040
			   TYPE_ARG_TYPES (TREE_TYPE (decl)), 
			   TREE_TYPE (TREE_TYPE (decl)),
			   1);

mrs's avatar
mrs committed
4041 4042 4043 4044 4045
  if (i == 0)
    return targs;
  return 0;
}

mrs's avatar
mrs committed
4046
static tree
mrs's avatar
mrs committed
4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
get_class_bindings (tparms, parms, args)
     tree tparms, parms, args;
{
  int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
  tree vec = make_temp_vec (ntparms);

  for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
    {
      switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
		     TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
		     &dummy, 1))
	{
	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
   produce an instantiation matching DECL.  */

tree
most_specialized (fns, decl)
     tree fns, decl;
{
4080
  tree fn, champ, args, *p;
mrs's avatar
mrs committed
4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171
  int fate;

  for (p = &fns; *p; )
    {
      args = get_bindings (TREE_VALUE (*p), decl);
      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))
    {
      fate = more_specialized (champ, TREE_VALUE (fn));
      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))
    {
      fate = more_specialized (champ, TREE_VALUE (fn));
      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
most_specialized_class (specs, mainargs)
     tree specs, mainargs;
{
  tree list = NULL_TREE, t, args, champ;
  int fate;

  for (t = specs; t; t = TREE_CHAIN (t))
    {
      args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
      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))
    {
4172
      fate = more_specialized_class (champ, t);
mrs's avatar
mrs committed
4173 4174 4175 4176 4177 4178 4179
      if (fate != 1)
	return error_mark_node;
    }

  return champ;
}

mrs's avatar
mrs committed
4180
/* called from the parser.  */
mrs's avatar
mrs committed
4181

mrs's avatar
mrs committed
4182
void
mrs's avatar
mrs committed
4183
do_decl_instantiation (declspecs, declarator, storage)
mrs's avatar
mrs committed
4184
     tree declspecs, declarator, storage;
mrs's avatar
mrs committed
4185
{
mrs's avatar
merging  
mrs committed
4186
  tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
mrs's avatar
mrs committed
4187 4188
  tree name;
  tree fn;
mrs's avatar
mrs committed
4189
  tree result = NULL_TREE;
mrs's avatar
mrs committed
4190
  int extern_p = 0;
4191
  tree templates = NULL_TREE;
mrs's avatar
mrs committed
4192

mrs's avatar
mrs committed
4193 4194 4195 4196 4197 4198
  if (! DECL_LANG_SPECIFIC (decl))
    {
      cp_error ("explicit instantiation of non-template `%#D'", decl);
      return;
    }

mrs's avatar
mrs committed
4199
  /* If we've already seen this template instance, use it.  */
mrs's avatar
mrs committed
4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211
  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;
    }
  else if (DECL_FUNCTION_MEMBER_P (decl))
mrs's avatar
mrs committed
4212 4213 4214 4215 4216 4217 4218
    {
      if (DECL_TEMPLATE_INSTANTIATION (decl))
	result = decl;
      else if (name = DECL_ASSEMBLER_NAME (decl),
	       fn = IDENTIFIER_GLOBAL_VALUE (name),
	       fn && DECL_TEMPLATE_INSTANTIATION (fn))
	result = fn;
4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230
      else 
	{
	  /* Maybe this is an instantiation of a member template
	     function.  */
	  tree ctype = DECL_CONTEXT (decl);

	  name = DECL_NAME (decl);
	  fn = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
	  if (fn)
	    fn = TREE_VALUE (fn);

	  for (; fn; fn = DECL_CHAIN (fn))
4231
	    if (TREE_CODE (fn) == TEMPLATE_DECL)
4232 4233
	      templates = decl_tree_cons (NULL_TREE, fn, templates);
	}
mrs's avatar
mrs committed
4234
    }
mrs's avatar
mrs committed
4235
  else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
mrs's avatar
mrs committed
4236 4237
    {
      for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
4238
	if (TREE_CODE (fn) == TEMPLATE_DECL)
mrs's avatar
mrs committed
4239
	  templates = decl_tree_cons (NULL_TREE, fn, templates);
4240
    }
mrs's avatar
mrs committed
4241

4242 4243 4244 4245 4246
  if (templates && !result)
    {
      tree args;
      result = most_specialized (templates, decl);
      if (result == error_mark_node)
mrs's avatar
mrs committed
4247
	{
4248 4249 4250
	  char *str = "candidates are:";
	  cp_error ("ambiguous template instantiation for `%D' requested", decl);
	  for (fn = templates; fn; fn = TREE_CHAIN (fn))
mrs's avatar
mrs committed
4251
	    {
4252 4253
	      cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
	      str = "               ";
mrs's avatar
mrs committed
4254
	    }
4255 4256 4257 4258 4259 4260
	  return;
	}
      else if (result)
	{
	  args = get_bindings (result, decl);
	  result = instantiate_template (result, args);
mrs's avatar
mrs committed
4261
	}
mrs's avatar
mrs committed
4262
    }
4263

mrs's avatar
mrs committed
4264
  if (! result)
mrs's avatar
mrs committed
4265 4266 4267 4268
    {
      cp_error ("no matching template for `%D' found", decl);
      return;
    }
mrs's avatar
mrs committed
4269

mrs's avatar
mrs committed
4270 4271 4272 4273 4274 4275
  if (! DECL_TEMPLATE_INFO (result))
    {
      cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
      return;
    }

mrs's avatar
mrs committed
4276 4277 4278
  if (flag_external_templates)
    return;

mrs's avatar
mrs committed
4279
  if (storage == NULL_TREE)
mrs's avatar
mrs committed
4280
    ;
mrs's avatar
mrs committed
4281 4282
  else if (storage == ridpointers[(int) RID_EXTERN])
    extern_p = 1;
mrs's avatar
mrs committed
4283 4284 4285
  else
    cp_error ("storage class `%D' applied to template instantiation",
	      storage);
mrs's avatar
mrs committed
4286 4287

  mark_decl_instantiated (result, extern_p);
mrs's avatar
mrs committed
4288
  repo_template_instantiated (result, extern_p);
mrs's avatar
mrs committed
4289 4290
  if (! extern_p)
    instantiate_decl (result);
mrs's avatar
mrs committed
4291 4292
}

mrs's avatar
mrs committed
4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308
void
mark_class_instantiated (t, extern_p)
     tree t;
     int extern_p;
{
  SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
  SET_CLASSTYPE_INTERFACE_KNOWN (t);
  CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
  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
4309

mrs's avatar
mrs committed
4310
void
4311 4312
do_type_instantiation (t, storage)
     tree t, storage;
mrs's avatar
mrs committed
4313
{
mrs's avatar
mrs committed
4314 4315
  int extern_p = 0;
  int nomem_p = 0;
mrs's avatar
mrs committed
4316 4317
  int static_p = 0;

4318 4319 4320 4321 4322 4323 4324 4325 4326
  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
4327
  complete_type (t);
mrs's avatar
mrs committed
4328

mrs's avatar
mrs committed
4329 4330
  /* With -fexternal-templates, explicit instantiations are treated the same
     as implicit ones.  */
mrs's avatar
mrs committed
4331 4332 4333
  if (flag_external_templates)
    return;

mrs's avatar
mrs committed
4334 4335 4336 4337 4338 4339 4340 4341
  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
4342 4343 4344
    /* OK */;
  else if (storage == ridpointers[(int) RID_INLINE])
    nomem_p = 1;
mrs's avatar
mrs committed
4345 4346
  else if (storage == ridpointers[(int) RID_EXTERN])
    extern_p = 1;
mrs's avatar
mrs committed
4347 4348
  else if (storage == ridpointers[(int) RID_STATIC])
    static_p = 1;
mrs's avatar
mrs committed
4349 4350 4351 4352 4353 4354 4355
  else
    {
      cp_error ("storage class `%D' applied to template instantiation",
		storage);
      extern_p = 0;
    }

mrs's avatar
mrs committed
4356
  /* We've already instantiated this.  */
mrs's avatar
mrs committed
4357 4358 4359
  if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
      && extern_p)
    return;
mrs's avatar
mrs committed
4360

mrs's avatar
mrs committed
4361
  if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
mrs's avatar
mrs committed
4362 4363 4364 4365
    {
      mark_class_instantiated (t, extern_p);
      repo_template_instantiated (t, extern_p);
    }
mrs's avatar
mrs committed
4366 4367 4368 4369

  if (nomem_p)
    return;

mrs's avatar
mrs committed
4370
  {
mrs's avatar
mrs committed
4371
    tree tmp;
mrs's avatar
mrs committed
4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384

    if (! static_p)
      for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
	if (DECL_TEMPLATE_INSTANTIATION (tmp))
	  {
	    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
4385
	{
mrs's avatar
mrs committed
4386
	  mark_decl_instantiated (tmp, extern_p);
mrs's avatar
mrs committed
4387
	  repo_template_instantiated (tmp, extern_p);
mrs's avatar
mrs committed
4388 4389
	  if (! extern_p)
	    instantiate_decl (tmp);
mrs's avatar
mrs committed
4390
	}
mrs's avatar
mrs committed
4391

mrs's avatar
mrs committed
4392
    for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
mrs's avatar
mrs committed
4393 4394
      if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
	do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
mrs's avatar
mrs committed
4395
  }
mrs's avatar
mrs committed
4396
}
mrs's avatar
mrs committed
4397 4398

tree
mrs's avatar
mrs committed
4399 4400
instantiate_decl (d)
     tree d;
mrs's avatar
mrs committed
4401
{
mrs's avatar
mrs committed
4402 4403 4404 4405
  tree ti = DECL_TEMPLATE_INFO (d);
  tree tmpl = TI_TEMPLATE (ti);
  tree args = TI_ARGS (ti);
  tree td;
jason's avatar
jason committed
4406
  tree decl_pattern, code_pattern;
mrs's avatar
mrs committed
4407 4408 4409 4410
  tree save_ti;
  int nested = in_function_p ();
  int d_defined;
  int pattern_defined;
mrs's avatar
mrs committed
4411 4412
  int line = lineno;
  char *file = input_filename;
mrs's avatar
mrs committed
4413

jason's avatar
jason committed
4414 4415
  for (td = tmpl; DECL_TEMPLATE_INSTANTIATION (td); )
    td = DECL_TI_TEMPLATE (td);
4416

jason's avatar
jason committed
4417 4418 4419 4420 4421
  /* 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);
4422

mrs's avatar
mrs committed
4423 4424 4425
  if (TREE_CODE (d) == FUNCTION_DECL)
    {
      d_defined = (DECL_INITIAL (d) != NULL_TREE);
jason's avatar
jason committed
4426
      pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
mrs's avatar
mrs committed
4427 4428 4429 4430
    }
  else
    {
      d_defined = ! DECL_IN_AGGR_P (d);
jason's avatar
jason committed
4431
      pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
mrs's avatar
mrs committed
4432 4433 4434 4435
    }

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

4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448
  if (TREE_CODE (d) == FUNCTION_DECL) 
    {
      tree specs;

      /* Check to see if there is a matching specialization. */
      for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
	   specs != NULL_TREE;
	   specs = TREE_CHAIN (specs))
	if (comp_template_args (TREE_PURPOSE (specs), args))
	  return TREE_VALUE (specs);
    }

mrs's avatar
mrs committed
4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459
  /* 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
4460
      && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern))
mrs's avatar
mrs committed
4461 4462
    {
      pushclass (DECL_CONTEXT (d), 2);
jason's avatar
jason committed
4463 4464
      DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args,
				      TREE_VEC_LENGTH (args), tmpl);
mrs's avatar
mrs committed
4465 4466 4467 4468 4469
      popclass (1);
    }

  /* import_export_decl has to happen after DECL_INITIAL is set up.  */
  if (pattern_defined)
mrs's avatar
mrs committed
4470 4471 4472 4473 4474 4475 4476 4477 4478 4479
    {
      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
4480
	  else if (DECL_INTERFACE_KNOWN (code_pattern))
mrs's avatar
mrs committed
4481 4482
	    {
	      DECL_INTERFACE_KNOWN (d) = 1;
jason's avatar
jason committed
4483
	      DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
mrs's avatar
mrs committed
4484 4485
	    }
	  else
jason's avatar
jason committed
4486
	    warn_if_unknown_interface (code_pattern);
mrs's avatar
mrs committed
4487 4488
	}

mrs's avatar
mrs committed
4489
      if (at_eof)
mrs's avatar
mrs committed
4490 4491 4492 4493 4494 4495
	import_export_decl (d);
    }

  if (! pattern_defined
      || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
	  && (! DECL_INTERFACE_KNOWN (d)
mrs's avatar
mrs committed
4496 4497 4498 4499 4500 4501
	      || ! DECL_NOT_REALLY_EXTERN (d)))
      /* Kludge: if we compile a constructor in the middle of processing a
         toplevel declaration, we blow away the declspecs in
         temp_decl_obstack when we call permanent_allocation in
         finish_function.  So don't compile it yet.  */
      || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
mrs's avatar
mrs committed
4502 4503
    {
      add_pending_template (d);
mrs's avatar
mrs committed
4504
      goto out;
mrs's avatar
mrs committed
4505 4506
    }

mrs's avatar
mrs committed
4507 4508 4509
  lineno = DECL_SOURCE_LINE (d);
  input_filename = DECL_SOURCE_FILE (d);

mrs's avatar
mrs committed
4510
  /* Trick tsubst into giving us a new decl in case the template changed.  */
jason's avatar
jason committed
4511 4512 4513 4514
  save_ti = DECL_TEMPLATE_INFO (decl_pattern);
  DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE;
  td = tsubst (decl_pattern, args, TREE_VEC_LENGTH (args), tmpl);
  DECL_TEMPLATE_INFO (decl_pattern) = save_ti;
mrs's avatar
mrs committed
4515

mrs's avatar
mrs committed
4516 4517
  /* And set up DECL_INITIAL, since tsubst doesn't.  */
  if (TREE_CODE (td) == VAR_DECL)
mrs's avatar
mrs committed
4518 4519
    {
      pushclass (DECL_CONTEXT (d), 2);
jason's avatar
jason committed
4520 4521
      DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args,
				       TREE_VEC_LENGTH (args), tmpl);
mrs's avatar
mrs committed
4522 4523
      popclass (1);
    }
mrs's avatar
mrs committed
4524

mrs's avatar
mrs committed
4525
  if (TREE_CODE (d) == FUNCTION_DECL)
4526 4527 4528 4529 4530 4531 4532 4533 4534
    {
      /* 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
4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552
  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
4553
      tree t = DECL_SAVED_TREE (code_pattern);
mrs's avatar
mrs committed
4554

mrs's avatar
merging  
mrs committed
4555
      start_function (NULL_TREE, d, NULL_TREE, 1);
mrs's avatar
mrs committed
4556 4557
      store_parm_decls ();

mrs's avatar
mrs committed
4558 4559 4560 4561
      if (t && TREE_CODE (t) == RETURN_INIT)
	{
	  store_return_init
	    (TREE_OPERAND (t, 0),
4562
	     tsubst_expr (TREE_OPERAND (t, 1), args,
mrs's avatar
mrs committed
4563 4564 4565 4566
			  TREE_VEC_LENGTH (args), tmpl));
	  t = TREE_CHAIN (t);
	}

mrs's avatar
mrs committed
4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582
      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
	 pair of curley braces of a function.  These are needed
	 for correct operation of dwarfout.c.  */
      keep_next_level ();

      my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
4583
      tsubst_expr (t, args, TREE_VEC_LENGTH (args), tmpl);
mrs's avatar
mrs committed
4584

mrs's avatar
mrs committed
4585 4586 4587
      finish_function (lineno, 0, nested);
    }

mrs's avatar
mrs committed
4588
out:
mrs's avatar
mrs committed
4589 4590 4591
  lineno = line;
  input_filename = file;

mrs's avatar
mrs committed
4592 4593
  pop_from_top_level ();
  pop_tinst_level ();
mrs's avatar
mrs committed
4594 4595 4596

  return d;
}
mrs's avatar
mrs committed
4597 4598 4599 4600 4601 4602 4603

tree
tsubst_chain (t, argvec)
     tree t, argvec;
{
  if (t)
    {
4604
      tree first = tsubst (t, argvec,
mrs's avatar
mrs committed
4605 4606 4607 4608 4609
			   TREE_VEC_LENGTH (argvec), NULL_TREE);
      tree last = first;

      for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
	{
4610
	  tree x = tsubst (t, argvec, TREE_VEC_LENGTH (argvec), NULL_TREE);
mrs's avatar
mrs committed
4611 4612 4613 4614 4615 4616 4617 4618 4619
	  TREE_CHAIN (last) = x;
	  last = x;
	}

      return first;
    }
  return NULL_TREE;
}

mrs's avatar
mrs committed
4620
static tree
mrs's avatar
mrs committed
4621 4622 4623 4624 4625 4626 4627 4628
tsubst_expr_values (t, argvec)
     tree t, argvec;
{
  tree first = NULL_TREE;
  tree *p = &first;

  for (; t; t = TREE_CHAIN (t))
    {
4629
      tree pur = tsubst_copy (TREE_PURPOSE (t), argvec,
mrs's avatar
mrs committed
4630
			      TREE_VEC_LENGTH (argvec), NULL_TREE);
4631
      tree val = tsubst_expr (TREE_VALUE (t), argvec,
mrs's avatar
mrs committed
4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646
			      TREE_VEC_LENGTH (argvec), NULL_TREE);
      *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
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674

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

  t = most_specialized (fns, d);
  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
4675 4676 4677 4678 4679

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

static tree
4680
tsubst_enum (tag, args, nargs, field_chain)
4681
     tree tag, args;
mrs's avatar
mrs committed
4682
     int nargs;
4683
     tree * field_chain;
mrs's avatar
mrs committed
4684
{
4685 4686 4687
  extern tree current_local_enum;
  tree prev_local_enum = current_local_enum;

mrs's avatar
mrs committed
4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701
  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,
						nargs, NULL_TREE));
      TREE_CHAIN (elt) = values;
      values = elt;
    }

  finish_enum (newtag, values);

4702 4703 4704 4705 4706
  if (NULL != field_chain)
    *field_chain = grok_enum_decls (newtag, NULL_TREE);

  current_local_enum = prev_local_enum;

mrs's avatar
mrs committed
4707 4708
  return newtag;
}