]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/pt.c
62nd Cygnus<->FSF merge
[thirdparty/gcc.git] / gcc / cp / pt.c
CommitLineData
471086d6 1/* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
19the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21/* Known bugs or deficiencies include:
22 * templates for class static data don't work (methods only)
23 * duplicated method templates can crash the compiler
24 * interface/impl data is taken from file defining the template
25 * all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win"
27 * method templates must be seen before the expansion of the
28 class template is done
29 */
30
31#include "config.h"
32#include <stdio.h>
33#include "obstack.h"
34
35#include "tree.h"
36#include "flags.h"
37#include "cp-tree.h"
38#include "decl.h"
39#include "parse.h"
617abf06 40#include "lex.h"
471086d6 41
42extern struct obstack permanent_obstack;
43extern tree grokdeclarator ();
44
45extern int lineno;
46extern char *input_filename;
47struct pending_inline *pending_template_expansions;
48
49int processing_template_decl;
50int processing_template_defn;
51
52#define obstack_chunk_alloc xmalloc
53#define obstack_chunk_free free
54
55static int unify ();
56static void add_pending_template ();
57
58void overload_template_name (), pop_template_decls ();
59
60/* We've got a template header coming up; set obstacks up to save the
61 nodes created permanently. (There might be cases with nested templates
62 where we don't have to do this, but they aren't implemented, and it
63 probably wouldn't be worth the effort.) */
64void
65begin_template_parm_list ()
66{
67 pushlevel (0);
68 push_obstacks (&permanent_obstack, &permanent_obstack);
69 pushlevel (0);
70}
71
72/* Process information from new template parameter NEXT and append it to the
73 LIST being built. The rules for use of a template parameter type name
74 by later parameters are not well-defined for us just yet. However, the
75 only way to avoid having to parse expressions of unknown complexity (and
76 with tokens of unknown types) is to disallow it completely. So for now,
77 that is what is assumed. */
78tree
79process_template_parm (list, next)
80 tree list, next;
81{
82 tree parm;
83 tree decl = 0;
f3ba5c6a 84 tree defval;
471086d6 85 int is_type;
86 parm = next;
87 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
f3ba5c6a 88 defval = TREE_PURPOSE (parm);
89 parm = TREE_VALUE (parm);
90 is_type = TREE_PURPOSE (parm) == class_type_node;
471086d6 91 if (!is_type)
92 {
93 tree tinfo = 0;
f3ba5c6a 94 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
471086d6 95 /* is a const-param */
f3ba5c6a 96 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
471086d6 97 PARM, 0, NULL_TREE);
98 /* A template parameter is not modifiable. */
99 TREE_READONLY (parm) = 1;
c25194fd 100 if (IS_AGGR_TYPE (TREE_TYPE (parm)))
471086d6 101 {
102 sorry ("aggregate template parameter types");
103 TREE_TYPE (parm) = void_type_node;
104 }
105 tinfo = make_node (TEMPLATE_CONST_PARM);
106 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
107 if (TREE_PERMANENT (parm) == 0)
108 {
109 parm = copy_node (parm);
110 TREE_PERMANENT (parm) = 1;
111 }
112 TREE_TYPE (tinfo) = TREE_TYPE (parm);
113 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
114 DECL_INITIAL (decl) = tinfo;
115 DECL_INITIAL (parm) = tinfo;
116 }
117 else
118 {
119 tree t = make_node (TEMPLATE_TYPE_PARM);
f3ba5c6a 120 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
121 TYPE_MAIN_DECL (t) = decl;
122 parm = decl;
123 if (defval)
124 {
125 if (IDENTIFIER_HAS_TYPE_VALUE (defval))
126 defval = IDENTIFIER_TYPE_VALUE (defval);
127 else
128 defval = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (defval));
129 }
471086d6 130 }
1a3f833b 131 SET_DECL_ARTIFICIAL (decl);
471086d6 132 pushdecl (decl);
f3ba5c6a 133 parm = build_tree_list (defval, parm);
471086d6 134 return chainon (list, parm);
135}
136
137/* The end of a template parameter list has been reached. Process the
138 tree list into a parameter vector, converting each parameter into a more
139 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
140 as PARM_DECLs. */
141
142tree
143end_template_parm_list (parms)
144 tree parms;
145{
146 int nparms = 0;
f3ba5c6a 147 int saw_default = 0;
471086d6 148 tree saved_parmlist;
149 tree parm;
150 for (parm = parms; parm; parm = TREE_CHAIN (parm))
151 nparms++;
152 saved_parmlist = make_tree_vec (nparms);
153
154 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
155 {
f3ba5c6a 156 tree p = TREE_VALUE (parm);
157 if (TREE_PURPOSE (parm))
158 saw_default = 1;
159 else if (saw_default)
160 {
161 error ("if a default argument is given for one template parameter");
162 error ("default arguments must be given for all subsequent");
163 error ("parameters as well");
164 }
165
166 if (TREE_CODE (p) == TYPE_DECL)
471086d6 167 {
f3ba5c6a 168 tree t = TREE_TYPE (p);
471086d6 169 TEMPLATE_TYPE_SET_INFO (t, saved_parmlist, nparms);
170 }
171 else
172 {
173 tree tinfo = DECL_INITIAL (p);
174 DECL_INITIAL (p) = NULL_TREE;
175 TEMPLATE_CONST_SET_INFO (tinfo, saved_parmlist, nparms);
176 }
f3ba5c6a 177 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
471086d6 178 }
179 set_current_level_tags_transparency (1);
180 processing_template_decl++;
181 return saved_parmlist;
182}
183
184/* end_template_decl is called after a template declaration is seen.
185 D1 is template header; D2 is class_head_sans_basetype or a
186 TEMPLATE_DECL with its DECL_RESULT field set. */
187void
ac9386a0 188end_template_decl (d1, d2, is_class, defn)
471086d6 189 tree d1, d2, is_class;
ac9386a0 190 int defn;
471086d6 191{
192 tree decl;
193 struct template_info *tmpl;
194
195 tmpl = (struct template_info *) obstack_alloc (&permanent_obstack,
196 sizeof (struct template_info));
197 tmpl->text = 0;
198 tmpl->length = 0;
199 tmpl->aggr = is_class;
200
201 /* cloned from reinit_parse_for_template */
202 tmpl->filename = input_filename;
203 tmpl->lineno = lineno;
204 tmpl->parm_vec = d1; /* [eichin:19911015.2306EST] */
205
206 if (d2 == NULL_TREE || d2 == error_mark_node)
207 {
208 decl = 0;
209 goto lose;
210 }
211
212 if (is_class)
213 {
214 decl = build_lang_decl (TEMPLATE_DECL, d2, NULL_TREE);
617abf06 215 GNU_xref_decl (current_function_decl, decl);
471086d6 216 }
217 else
218 {
219 if (TREE_CODE (d2) == TEMPLATE_DECL)
220 decl = d2;
221 else
222 {
223 /* Class destructor templates and operator templates are
224 slipping past as non-template nodes. Process them here, since
225 I haven't figured out where to catch them earlier. I could
226 go do that, but it's a choice between getting that done and
227 staying only N months behind schedule. Sorry.... */
228 enum tree_code code;
229 my_friendly_assert (TREE_CODE (d2) == CALL_EXPR, 263);
230 code = TREE_CODE (TREE_OPERAND (d2, 0));
231 my_friendly_assert (code == BIT_NOT_EXPR
232 || code == OP_IDENTIFIER
233 || code == SCOPE_REF, 264);
234 d2 = grokdeclarator (d2, NULL_TREE, MEMFUNCDEF, 0, NULL_TREE);
235 decl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (d2),
236 TREE_TYPE (d2));
237 DECL_TEMPLATE_RESULT (decl) = d2;
238 DECL_CONTEXT (decl) = DECL_CONTEXT (d2);
239 DECL_CLASS_CONTEXT (decl) = DECL_CLASS_CONTEXT (d2);
240 DECL_NAME (decl) = DECL_NAME (d2);
241 TREE_TYPE (decl) = TREE_TYPE (d2);
1a3f833b 242 if (interface_unknown && flag_external_templates
243 && ! flag_alt_external_templates
244 && ! DECL_IN_SYSTEM_HEADER (decl))
245 warn_if_unknown_interface (decl);
471086d6 246 TREE_PUBLIC (decl) = TREE_PUBLIC (d2) = flag_external_templates && !interface_unknown;
247 DECL_EXTERNAL (decl) = (DECL_EXTERNAL (d2)
248 && !(DECL_CLASS_CONTEXT (d2)
249 && !DECL_THIS_EXTERN (d2)));
250 }
251
252 /* All routines creating TEMPLATE_DECL nodes should now be using
253 build_lang_decl, which will have set this up already. */
254 my_friendly_assert (DECL_LANG_SPECIFIC (decl) != 0, 265);
255
256 /* @@ Somewhere, permanent allocation isn't being used. */
257 if (! DECL_TEMPLATE_IS_CLASS (decl)
258 && TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == FUNCTION_DECL)
259 {
260 tree result = DECL_TEMPLATE_RESULT (decl);
261 /* Will do nothing if allocation was already permanent. */
262 DECL_ARGUMENTS (result) = copy_to_permanent (DECL_ARGUMENTS (result));
263 }
264
265 /* If this is for a method, there's an extra binding level here. */
617abf06 266 if (DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
471086d6 267 {
268 /* @@ Find out where this should be getting set! */
269 tree r = DECL_TEMPLATE_RESULT (decl);
617abf06 270 if (DECL_LANG_SPECIFIC (r) && DECL_CLASS_CONTEXT (r) == NULL_TREE)
471086d6 271 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
272 }
273 }
274 DECL_TEMPLATE_INFO (decl) = tmpl;
275 DECL_TEMPLATE_PARMS (decl) = d1;
ac9386a0 276
277 /* So that duplicate_decls can do the right thing. */
278 if (defn)
279 DECL_INITIAL (decl) = error_mark_node;
280
281 /* If context of decl is non-null (i.e., method template), add it
282 to the appropriate class template, and pop the binding levels. */
617abf06 283 if (! is_class && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
471086d6 284 {
ac9386a0 285 tree ctx = DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl));
d81e00a4 286 tree tmpl, t;
ac9386a0 287 my_friendly_assert (TREE_CODE (ctx) == UNINSTANTIATED_P_TYPE, 266);
288 tmpl = UPT_TEMPLATE (ctx);
d81e00a4 289 for (t = DECL_TEMPLATE_MEMBERS (tmpl); t; t = TREE_CHAIN (t))
290 if (TREE_PURPOSE (t) == DECL_NAME (decl)
291 && duplicate_decls (decl, TREE_VALUE (t)))
292 goto already_there;
ac9386a0 293 DECL_TEMPLATE_MEMBERS (tmpl) =
d81e00a4 294 perm_tree_cons (DECL_NAME (decl), decl, DECL_TEMPLATE_MEMBERS (tmpl));
295 already_there:
ac9386a0 296 poplevel (0, 0, 0);
297 poplevel (0, 0, 0);
298 }
299 /* Otherwise, go back to top level first, and push the template decl
300 again there. */
301 else
302 {
303 poplevel (0, 0, 0);
304 poplevel (0, 0, 0);
0543e7a9 305 pushdecl (decl);
471086d6 306 }
ac9386a0 307 lose:
471086d6 308#if 0 /* It happens sometimes, with syntactic or semantic errors.
309
310 One specific case:
311 template <class A, int X, int Y> class Foo { ... };
312 template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
313 Note the missing "A" in the class containing "method". */
314 my_friendly_assert (global_bindings_p (), 267);
315#else
316 while (! global_bindings_p ())
317 poplevel (0, 0, 0);
318#endif
319 pop_obstacks ();
320 processing_template_decl--;
321 (void) get_pending_sizes ();
322}
323
324/* If TYPE contains a template parm type, then substitute that type
325 with its actual type that is found in TVEC. */
326static void
327grok_template_type (tvec, type)
328 tree tvec;
329 tree* type;
330{
331 switch (TREE_CODE (*type))
332 {
333 case TEMPLATE_TYPE_PARM:
334 if (*type != TYPE_MAIN_VARIANT (*type))
335 {
336 /* we are here for cases like const T* etc. */
337 grok_template_type (tvec, &TYPE_MAIN_VARIANT (*type));
c38086bd 338 *type = cp_build_type_variant (TYPE_MAIN_VARIANT (*type),
c07b1ad1 339 TYPE_READONLY (*type),
340 TYPE_VOLATILE (*type));
471086d6 341 }
342 else
343 *type = TREE_VEC_ELT (tvec, TEMPLATE_TYPE_IDX (*type));
344 return;
345 case POINTER_TYPE:
346 case REFERENCE_TYPE:
347 grok_template_type (tvec, &TREE_TYPE (*type));
348 return;
349 case FUNCTION_TYPE:
350 {
351 tree p;
352
353 /* take care of function's return type first */
354 grok_template_type (tvec, &TREE_TYPE (*type));
355
356 /* take care of function's arguments */
357 for (p = TYPE_ARG_TYPES (*type); p; p = TREE_CHAIN (p))
358 grok_template_type (tvec, &TREE_VALUE (p));
359 return;
360 }
361 default:
362 break;
363 }
364 return;
365}
366
367/* Convert all template arguments to their appropriate types, and return
368 a vector containing the resulting values. If any error occurs, return
369 error_mark_node. */
370static tree
371coerce_template_parms (parms, arglist, in_decl)
372 tree parms, arglist;
373 tree in_decl;
374{
f3ba5c6a 375 int nparms, nargs, i, lost = 0;
471086d6 376 tree vec;
377
f3ba5c6a 378 if (arglist == NULL_TREE)
379 nargs = 0;
380 else if (TREE_CODE (arglist) == TREE_VEC)
381 nargs = TREE_VEC_LENGTH (arglist);
471086d6 382 else
f3ba5c6a 383 nargs = list_length (arglist);
384
385 nparms = TREE_VEC_LENGTH (parms);
386
387 if (nargs > nparms
388 || (nargs < nparms
389 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
471086d6 390 {
391 error ("incorrect number of parameters (%d, should be %d)",
f3ba5c6a 392 nargs, nparms);
471086d6 393 if (in_decl)
394 cp_error_at ("in template expansion for decl `%D'", in_decl);
395 return error_mark_node;
396 }
397
f3ba5c6a 398 if (arglist && TREE_CODE (arglist) == TREE_VEC)
471086d6 399 vec = copy_node (arglist);
400 else
401 {
402 vec = make_tree_vec (nparms);
403 for (i = 0; i < nparms; i++)
404 {
f3ba5c6a 405 tree arg;
406
407 if (arglist)
408 {
409 arg = arglist;
410 arglist = TREE_CHAIN (arglist);
411
412 if (arg == error_mark_node)
413 lost++;
414 else
415 arg = TREE_VALUE (arg);
416 }
471086d6 417 else
f3ba5c6a 418 arg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
419
471086d6 420 TREE_VEC_ELT (vec, i) = arg;
421 }
422 }
423 for (i = 0; i < nparms; i++)
424 {
425 tree arg = TREE_VEC_ELT (vec, i);
f3ba5c6a 426 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
471086d6 427 tree val = 0;
428 int is_type, requires_type;
429
430 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
f3ba5c6a 431 requires_type = TREE_CODE (parm) == TYPE_DECL;
471086d6 432 if (is_type != requires_type)
433 {
434 if (in_decl)
c38086bd 435 cp_error ("type/value mismatch in template parameter list for `%D'",
436 in_decl);
471086d6 437 lost++;
438 TREE_VEC_ELT (vec, i) = error_mark_node;
439 continue;
440 }
441 if (is_type)
442 val = groktypename (arg);
443 else if (TREE_CODE (arg) == STRING_CST)
444 {
445 cp_error ("string literal %E is not a valid template argument", arg);
446 error ("because it is the address of an object with static linkage");
447 val = error_mark_node;
448 }
449 else
450 {
451 grok_template_type (vec, &TREE_TYPE (parm));
452 val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
f3ba5c6a 453
471086d6 454 if (val == error_mark_node)
455 ;
456
457 /* 14.2: Other template-arguments must be constant-expressions,
458 addresses of objects or functions with external linkage, or of
459 static class members. */
460 else if (!TREE_CONSTANT (val))
461 {
462 cp_error ("non-const `%E' cannot be used as template argument",
463 arg);
464 val = error_mark_node;
465 }
466 else if (TREE_CODE (val) == ADDR_EXPR)
467 {
468 tree a = TREE_OPERAND (val, 0);
469 if ((TREE_CODE (a) == VAR_DECL
470 || TREE_CODE (a) == FUNCTION_DECL)
c25194fd 471 && ! DECL_PUBLIC (a))
471086d6 472 {
473 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
474 val = error_mark_node;
475 }
476 }
477 }
478
479 if (val == error_mark_node)
480 lost++;
481
482 TREE_VEC_ELT (vec, i) = val;
483 }
484 if (lost)
485 return error_mark_node;
486 return vec;
487}
488
489/* Given class template name and parameter list, produce a user-friendly name
490 for the instantiation. */
491static char *
492mangle_class_name_for_template (name, parms, arglist)
493 char *name;
494 tree parms, arglist;
495{
496 static struct obstack scratch_obstack;
497 static char *scratch_firstobj;
498 int i, nparms;
471086d6 499
500 if (!scratch_firstobj)
501 {
502 gcc_obstack_init (&scratch_obstack);
503 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
504 }
505 else
506 obstack_free (&scratch_obstack, scratch_firstobj);
507
508#if 0
509#define buflen sizeof(buf)
510#define check if (bufp >= buf+buflen-1) goto too_long
511#define ccat(c) *bufp++=(c); check
512#define advance bufp+=strlen(bufp); check
513#define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
514#else
515#define check
516#define ccat(c) obstack_1grow (&scratch_obstack, (c));
517#define advance
518#define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
519#endif
471086d6 520
521 cat (name);
522 ccat ('<');
523 nparms = TREE_VEC_LENGTH (parms);
524 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
525 for (i = 0; i < nparms; i++)
526 {
f3ba5c6a 527 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
528 tree arg = TREE_VEC_ELT (arglist, i);
471086d6 529
530 if (i)
531 ccat (',');
532
f3ba5c6a 533 if (TREE_CODE (parm) == TYPE_DECL)
471086d6 534 {
535 cat (type_as_string (arg, 0));
536 continue;
537 }
538 else
539 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
540
541 if (TREE_CODE (arg) == TREE_LIST)
542 {
543 /* New list cell was built because old chain link was in
544 use. */
545 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
546 arg = TREE_VALUE (arg);
547 }
548 /* No need to check arglist against parmlist here; we did that
549 in coerce_template_parms, called from lookup_template_class. */
550 cat (expr_as_string (arg, 0));
551 }
552 {
553 char *bufp = obstack_next_free (&scratch_obstack);
554 int offset = 0;
555 while (bufp[offset - 1] == ' ')
556 offset--;
557 obstack_blank_fast (&scratch_obstack, offset);
558
559 /* B<C<char> >, not B<C<char>> */
560 if (bufp[offset - 1] == '>')
561 ccat (' ');
562 }
563 ccat ('>');
564 ccat ('\0');
565 return (char *) obstack_base (&scratch_obstack);
566
0543e7a9 567#if 0
471086d6 568 too_long:
0543e7a9 569#endif
471086d6 570 fatal ("out of (preallocated) string space creating template instantiation name");
571 /* NOTREACHED */
572 return NULL;
573}
574
575/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
576 parameters, find the desired type.
577
578 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
579 Since ARGLIST is build on the decl_obstack, we must copy it here
580 to keep it from being reclaimed when the decl storage is reclaimed.
581
582 IN_DECL, if non-NULL, is the template declaration we are trying to
583 instantiate. */
584tree
585lookup_template_class (d1, arglist, in_decl)
586 tree d1, arglist;
587 tree in_decl;
588{
589 tree template, parmlist;
590 char *mangled_name;
591 tree id;
592
593 my_friendly_assert (TREE_CODE (d1) == IDENTIFIER_NODE, 272);
594 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
595 if (! template)
596 template = IDENTIFIER_CLASS_VALUE (d1);
597 /* With something like `template <class T> class X class X { ... };'
598 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
599 We don't want to do that, but we have to deal with the situation, so
600 let's give them some syntax errors to chew on instead of a crash. */
601 if (! template)
602 return error_mark_node;
603 if (TREE_CODE (template) != TEMPLATE_DECL)
604 {
605 cp_error ("non-template type `%T' used as a template", d1);
606 if (in_decl)
607 cp_error_at ("for template declaration `%D'", in_decl);
608 return error_mark_node;
609 }
610 parmlist = DECL_TEMPLATE_PARMS (template);
611
f3ba5c6a 612 arglist = coerce_template_parms (parmlist, arglist, template);
471086d6 613 if (arglist == error_mark_node)
614 return error_mark_node;
615 if (uses_template_parms (arglist))
616 {
617 tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
618 tree d;
619 id = make_anon_name ();
ddb9bca7 620 d = build_decl (TYPE_DECL, id, t);
471086d6 621 TYPE_NAME (t) = d;
622 TYPE_VALUES (t) = build_tree_list (template, arglist);
623 pushdecl_top_level (d);
624 }
625 else
626 {
627 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
628 parmlist, arglist);
629 id = get_identifier (mangled_name);
630 }
631 if (!IDENTIFIER_TEMPLATE (id))
632 {
633 arglist = copy_to_permanent (arglist);
634 IDENTIFIER_TEMPLATE (id) = perm_tree_cons (template, arglist, NULL_TREE);
635 }
636 return id;
637}
638\f
639void
640push_template_decls (parmlist, arglist, class_level)
641 tree parmlist, arglist;
642 int class_level;
643{
644 int i, nparms;
645
646 /* Don't want to push values into global context. */
647 if (!class_level)
0543e7a9 648 {
649 pushlevel (1);
650 declare_pseudo_global_level ();
651 }
652
471086d6 653 nparms = TREE_VEC_LENGTH (parmlist);
654
655 for (i = 0; i < nparms; i++)
656 {
657 int requires_type, is_type;
f3ba5c6a 658 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
471086d6 659 tree arg = TREE_VEC_ELT (arglist, i);
660 tree decl = 0;
661
f3ba5c6a 662 requires_type = TREE_CODE (parm) == TYPE_DECL;
471086d6 663 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
664 if (is_type)
665 {
666 /* add typename to namespace */
667 if (!requires_type)
668 {
669 error ("template use error: type provided where value needed");
670 continue;
671 }
672 decl = arg;
673 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 't', 273);
f3ba5c6a 674 decl = build_decl (TYPE_DECL, DECL_NAME (parm), decl);
471086d6 675 }
676 else
677 {
678 /* add const decl to namespace */
679 tree val;
680 if (requires_type)
681 {
682 error ("template use error: value provided where type needed");
683 continue;
684 }
685 val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
686 if (val != error_mark_node)
687 {
c25194fd 688 decl = build_decl (CONST_DECL, DECL_NAME (parm),
689 TREE_TYPE (parm));
471086d6 690 DECL_INITIAL (decl) = val;
691 TREE_READONLY (decl) = 1;
692 }
693 }
694 if (decl != 0)
695 {
1a3f833b 696 SET_DECL_ARTIFICIAL (decl);
471086d6 697 layout_decl (decl, 0);
698 if (class_level)
699 pushdecl_class_level (decl);
700 else
701 pushdecl (decl);
702 }
703 }
471086d6 704}
705
706void
707pop_template_decls (parmlist, arglist, class_level)
708 tree parmlist, arglist;
709 int class_level;
710{
711 if (!class_level)
712 poplevel (0, 0, 0);
713}
714\f
ac9386a0 715/* Should be defined in parse.h. */
471086d6 716extern int yychar;
717
718int
719uses_template_parms (t)
720 tree t;
721{
722 if (!t)
723 return 0;
724 switch (TREE_CODE (t))
725 {
726 case INDIRECT_REF:
727 case COMPONENT_REF:
728 /* We assume that the object must be instantiated in order to build
729 the COMPONENT_REF, so we test only whether the type of the
730 COMPONENT_REF uses template parms. */
731 return uses_template_parms (TREE_TYPE (t));
732
733 case IDENTIFIER_NODE:
734 if (!IDENTIFIER_TEMPLATE (t))
735 return 0;
736 return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t)));
737
738 /* aggregates of tree nodes */
739 case TREE_VEC:
740 {
741 int i = TREE_VEC_LENGTH (t);
742 while (i--)
743 if (uses_template_parms (TREE_VEC_ELT (t, i)))
744 return 1;
745 return 0;
746 }
747 case TREE_LIST:
748 if (uses_template_parms (TREE_PURPOSE (t))
749 || uses_template_parms (TREE_VALUE (t)))
750 return 1;
751 return uses_template_parms (TREE_CHAIN (t));
752
753 /* constructed type nodes */
754 case POINTER_TYPE:
755 case REFERENCE_TYPE:
756 return uses_template_parms (TREE_TYPE (t));
757 case RECORD_TYPE:
1e66592c 758 if (TYPE_PTRMEMFUNC_FLAG (t))
759 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
471086d6 760 case UNION_TYPE:
761 if (!TYPE_NAME (t))
762 return 0;
763 if (!TYPE_IDENTIFIER (t))
764 return 0;
765 return uses_template_parms (TYPE_IDENTIFIER (t));
766 case FUNCTION_TYPE:
767 if (uses_template_parms (TYPE_ARG_TYPES (t)))
768 return 1;
769 return uses_template_parms (TREE_TYPE (t));
770 case ARRAY_TYPE:
771 if (uses_template_parms (TYPE_DOMAIN (t)))
772 return 1;
773 return uses_template_parms (TREE_TYPE (t));
774 case OFFSET_TYPE:
775 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
776 return 1;
777 return uses_template_parms (TREE_TYPE (t));
778 case METHOD_TYPE:
779 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
780 return 1;
781 if (uses_template_parms (TYPE_ARG_TYPES (t)))
782 return 1;
783 return uses_template_parms (TREE_TYPE (t));
784
785 /* decl nodes */
786 case TYPE_DECL:
787 return uses_template_parms (DECL_NAME (t));
788 case FUNCTION_DECL:
789 if (uses_template_parms (TREE_TYPE (t)))
790 return 1;
791 /* fall through */
792 case VAR_DECL:
793 case PARM_DECL:
794 /* ??? What about FIELD_DECLs? */
795 /* The type of a decl can't use template parms if the name of the
796 variable doesn't, because it's impossible to resolve them. So
797 ignore the type field for now. */
798 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
799 return 1;
800 if (uses_template_parms (TREE_TYPE (t)))
801 {
802 error ("template parms used where they can't be resolved");
803 }
804 return 0;
805
806 case CALL_EXPR:
807 return uses_template_parms (TREE_TYPE (t));
808 case ADDR_EXPR:
809 return uses_template_parms (TREE_OPERAND (t, 0));
810
811 /* template parm nodes */
812 case TEMPLATE_TYPE_PARM:
813 case TEMPLATE_CONST_PARM:
814 return 1;
815
816 /* simple type nodes */
817 case INTEGER_TYPE:
818 if (uses_template_parms (TYPE_MIN_VALUE (t)))
819 return 1;
820 return uses_template_parms (TYPE_MAX_VALUE (t));
821
822 case REAL_TYPE:
823 case VOID_TYPE:
824 case ENUMERAL_TYPE:
bb0726a1 825 case BOOLEAN_TYPE:
471086d6 826 return 0;
827
828 /* constants */
829 case INTEGER_CST:
830 case REAL_CST:
831 case STRING_CST:
832 return 0;
833
834 case ERROR_MARK:
835 /* Non-error_mark_node ERROR_MARKs are bad things. */
836 my_friendly_assert (t == error_mark_node, 274);
837 /* NOTREACHED */
838 return 0;
839
840 case UNINSTANTIATED_P_TYPE:
841 return 1;
842
c25194fd 843 case CONSTRUCTOR:
844 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
845 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
846 /* else fall through */
847
471086d6 848 default:
849 switch (TREE_CODE_CLASS (TREE_CODE (t)))
850 {
851 case '1':
852 case '2':
853 case '3':
854 case '<':
855 {
856 int i;
857 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
858 if (uses_template_parms (TREE_OPERAND (t, i)))
859 return 1;
860 return 0;
861 }
862 default:
863 break;
864 }
865 sorry ("testing %s for template parms",
866 tree_code_name [(int) TREE_CODE (t)]);
867 my_friendly_abort (82);
868 /* NOTREACHED */
869 return 0;
870 }
871}
872
873void
874instantiate_member_templates (classname)
875 tree classname;
876{
877 tree t;
878 tree id = classname;
879 tree members = DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id)));
880
881 for (t = members; t; t = TREE_CHAIN (t))
882 {
883 tree parmvec, type, classparms, tdecl, t2;
884 int nparms, xxx = 0, i;
885
886 my_friendly_assert (TREE_VALUE (t) != NULL_TREE, 275);
887 my_friendly_assert (TREE_CODE (TREE_VALUE (t)) == TEMPLATE_DECL, 276);
888 /* @@ Should verify that class parm list is a list of
889 distinct template parameters, and covers all the template
890 parameters. */
891 tdecl = TREE_VALUE (t);
892 type = DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl));
893 classparms = UPT_PARMS (type);
894 nparms = TREE_VEC_LENGTH (classparms);
895 parmvec = make_tree_vec (nparms);
896 for (i = 0; i < nparms; i++)
897 TREE_VEC_ELT (parmvec, i) = NULL_TREE;
898 switch (unify (DECL_TEMPLATE_PARMS (tdecl),
899 &TREE_VEC_ELT (parmvec, 0), nparms,
900 type, IDENTIFIER_TYPE_VALUE (classname),
901 &xxx))
902 {
903 case 0:
904 /* Success -- well, no inconsistency, at least. */
905 for (i = 0; i < nparms; i++)
906 if (TREE_VEC_ELT (parmvec, i) == NULL_TREE)
907 goto failure;
908 t2 = instantiate_template (tdecl,
909 &TREE_VEC_ELT (parmvec, 0));
910 type = IDENTIFIER_TYPE_VALUE (id);
911 my_friendly_assert (type != 0, 277);
471086d6 912 break;
913 case 1:
914 /* Failure. */
915 failure:
f3ba5c6a 916 cp_error_at ("type unification error instantiating `%D'", tdecl);
917 cp_error ("while instantiating members of `%T'", classname);
471086d6 918
919 continue /* loop of members */;
920 default:
921 /* Eek, a bug. */
922 my_friendly_abort (83);
923 }
924 }
925}
926
dd2b9e17 927static struct tinst_level *current_tinst_level = 0;
928static struct tinst_level *free_tinst_level = 0;
929static int tinst_depth = 0;
930int max_tinst_depth = 17;
471086d6 931
dd2b9e17 932int
471086d6 933push_tinst_level (name)
934 tree name;
935{
936 struct tinst_level *new;
937 tree global = IDENTIFIER_GLOBAL_VALUE (name);
938
dd2b9e17 939 if (tinst_depth >= max_tinst_depth)
940 {
941 error ("template instantiation depth exceeds maximum of %d",
942 max_tinst_depth);
943 cp_error (" instantiating `%D'", name);
944 return 0;
945 }
946
471086d6 947 if (free_tinst_level)
948 {
949 new = free_tinst_level;
950 free_tinst_level = new->next;
951 }
952 else
953 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
954
955 new->classname = name;
956 if (global)
957 {
958 new->line = DECL_SOURCE_LINE (global);
959 new->file = DECL_SOURCE_FILE (global);
960 }
961 else
962 {
963 new->line = lineno;
964 new->file = input_filename;
965 }
966 new->next = current_tinst_level;
967 current_tinst_level = new;
dd2b9e17 968 ++tinst_depth;
969 return 1;
471086d6 970}
971
972void
973pop_tinst_level ()
974{
975 struct tinst_level *old = current_tinst_level;
976
977 current_tinst_level = old->next;
978 old->next = free_tinst_level;
979 free_tinst_level = old;
dd2b9e17 980 --tinst_depth;
471086d6 981}
982
983struct tinst_level *
984tinst_for_decl ()
985{
986 struct tinst_level *p = current_tinst_level;
987
988 if (p)
989 for (; p->next ; p = p->next )
990 ;
991 return p;
992}
993
994tree
995instantiate_class_template (classname, setup_parse)
996 tree classname;
997 int setup_parse;
998{
999 struct template_info *template_info;
1000 tree template, t1;
1001
1002 if (classname == error_mark_node)
1003 return error_mark_node;
1004
1005 my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 278);
1006 template = IDENTIFIER_TEMPLATE (classname);
1007
1008 if (IDENTIFIER_HAS_TYPE_VALUE (classname))
1009 {
1010 tree type = IDENTIFIER_TYPE_VALUE (classname);
1011 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
1012 return type;
1013 if (TYPE_BEING_DEFINED (type)
1014 || TYPE_SIZE (type)
1015 || CLASSTYPE_USE_TEMPLATE (type) != 0)
1016 return type;
1017 }
1018
1019 /* If IDENTIFIER_LOCAL_VALUE is already set on this template classname
1020 (it's something like `foo<int>'), that means we're already working on
1021 the instantiation for it. Normally, a classname comes in with nothing
1022 but its IDENTIFIER_TEMPLATE slot set. If we were to try to instantiate
1023 this again, we'd get a redeclaration error. Since we're already working
1024 on it, we'll pass back this classname's TYPE_DECL (it's the value of
1025 the classname's IDENTIFIER_LOCAL_VALUE). Only do this if we're setting
1026 things up for the parser, though---if we're just trying to instantiate
1027 it (e.g., via tsubst) we can trip up cuz it may not have an
1028 IDENTIFIER_TYPE_VALUE when it will need one. */
1029 if (setup_parse && IDENTIFIER_LOCAL_VALUE (classname))
1030 return IDENTIFIER_LOCAL_VALUE (classname);
1031
1032 if (uses_template_parms (classname))
1033 {
1034 if (!TREE_TYPE (classname))
1035 {
1036 tree t = make_lang_type (RECORD_TYPE);
ddb9bca7 1037 tree d = build_decl (TYPE_DECL, classname, t);
471086d6 1038 DECL_NAME (d) = classname;
1039 TYPE_NAME (t) = d;
1040 pushdecl (d);
1041 }
1042 return NULL_TREE;
1043 }
1044
1045 t1 = TREE_PURPOSE (template);
1046 my_friendly_assert (TREE_CODE (t1) == TEMPLATE_DECL, 279);
1047
1048 /* If a template is declared but not defined, accept it; don't crash.
1049 Later uses requiring the definition will be flagged as errors by
1050 other code. Thanks to niklas@appli.se for this bug fix. */
1051 if (DECL_TEMPLATE_INFO (t1)->text == 0)
1052 setup_parse = 0;
1053
1054 push_to_top_level ();
1055 template_info = DECL_TEMPLATE_INFO (t1);
dd2b9e17 1056 if (setup_parse && push_tinst_level (classname))
471086d6 1057 {
471086d6 1058 push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
1059 TREE_VALUE (template), 0);
1060 set_current_level_tags_transparency (1);
1061 feed_input (template_info->text, template_info->length, (struct obstack *)0);
1062 lineno = template_info->lineno;
1063 input_filename = template_info->filename;
1064 /* Get interface/implementation back in sync. */
1065 extract_interface_info ();
1066 overload_template_name (classname, 0);
b0722fac 1067 /* Kludge so that we don't get screwed by our own base classes. */
1068 TYPE_BEING_DEFINED (TREE_TYPE (classname)) = 1;
471086d6 1069 yychar = PRE_PARSED_CLASS_DECL;
1070 yylval.ttype = classname;
1071 processing_template_defn++;
1072 if (!flag_external_templates)
1073 interface_unknown++;
1074 }
1075 else
1076 {
1077 tree t, decl, id, tmpl;
1078
1079 id = classname;
1080 tmpl = TREE_PURPOSE (IDENTIFIER_TEMPLATE (id));
1081 t = xref_tag (DECL_TEMPLATE_INFO (tmpl)->aggr, id, NULL_TREE, 0);
1082 my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
1083 || TREE_CODE (t) == UNION_TYPE, 280);
1084
1085 /* Now, put a copy of the decl in global scope, to avoid
1086 * recursive expansion. */
1087 decl = IDENTIFIER_LOCAL_VALUE (id);
1088 if (!decl)
1089 decl = IDENTIFIER_CLASS_VALUE (id);
1090 if (decl)
1091 {
1092 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 281);
1093 /* We'd better make sure we're on the permanent obstack or else
1094 * we'll get a "friendly" abort 124 in pushdecl. Perhaps a
1095 * copy_to_permanent would be sufficient here, but then a
1096 * sharing problem might occur. I don't know -- niklas@appli.se */
1097 push_obstacks (&permanent_obstack, &permanent_obstack);
1098 pushdecl_top_level (copy_node (decl));
1099 pop_obstacks ();
1100 }
1101 pop_from_top_level ();
1102 }
1103
1104 return NULL_TREE;
1105}
1106
1107static int
1108list_eq (t1, t2)
1109 tree t1, t2;
1110{
1111 if (t1 == NULL_TREE)
1112 return t2 == NULL_TREE;
1113 if (t2 == NULL_TREE)
1114 return 0;
1115 /* Don't care if one declares its arg const and the other doesn't -- the
1116 main variant of the arg type is all that matters. */
1117 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1118 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1119 return 0;
1120 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1121}
1122
1123static tree
1124lookup_nested_type_by_name (ctype, name)
1125 tree ctype, name;
1126{
1127 tree t;
1128
c25194fd 1129 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
1130 {
1131 if (name == TREE_PURPOSE (t))
1132 return TREE_VALUE (t);
1133 }
471086d6 1134 return NULL_TREE;
1135}
1136
1137static tree
1138search_nested_type_in_tmpl (tmpl, type)
1139 tree tmpl, type;
1140{
1141 tree t;
1142
1143 if (tmpl == NULL || TYPE_CONTEXT(type) == NULL)
1144 return tmpl;
1145 t = search_nested_type_in_tmpl (tmpl, TYPE_CONTEXT(type));
1146 if (t == NULL) return t;
1147 t = lookup_nested_type_by_name(t, DECL_NAME(TYPE_NAME(type)));
1148 return t;
1149}
1150
1151static tree
1152tsubst (t, args, nargs, in_decl)
1153 tree t, *args;
1154 int nargs;
1155 tree in_decl;
1156{
1157 tree type;
1158
1159 if (t == NULL_TREE || t == error_mark_node)
1160 return t;
1161
1162 type = TREE_TYPE (t);
1163 if (type
1164 /* Minor optimization.
1165 ?? Are these really the most frequent cases? Is the savings
1166 significant? */
1167 && type != integer_type_node
1168 && type != void_type_node
1169 && type != char_type_node)
1e66592c 1170 type = tsubst (type, args, nargs, in_decl);
1171
471086d6 1172 switch (TREE_CODE (t))
1173 {
1174 case RECORD_TYPE:
1175 if (TYPE_PTRMEMFUNC_P (t))
1176 return build_ptrmemfunc_type
1177 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1178
1179 /* else fall through */
1180
1181 case ERROR_MARK:
1182 case IDENTIFIER_NODE:
1183 case OP_IDENTIFIER:
1184 case VOID_TYPE:
1185 case REAL_TYPE:
1186 case ENUMERAL_TYPE:
bb0726a1 1187 case BOOLEAN_TYPE:
471086d6 1188 case INTEGER_CST:
1189 case REAL_CST:
1190 case STRING_CST:
1191 case UNION_TYPE:
1192 return t;
1193
1194 case INTEGER_TYPE:
1195 if (t == integer_type_node)
1196 return t;
1197
1198 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1199 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1200 return t;
1201 return build_index_2_type
1202 (tsubst (TYPE_MIN_VALUE (t), args, nargs, in_decl),
1203 tsubst (TYPE_MAX_VALUE (t), args, nargs, in_decl));
1204
1205 case TEMPLATE_TYPE_PARM:
c25194fd 1206 {
1207 tree arg = args[TEMPLATE_TYPE_IDX (t)];
1208 return cp_build_type_variant
1209 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
1210 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
1211 }
471086d6 1212
1213 case TEMPLATE_CONST_PARM:
1214 return args[TEMPLATE_CONST_IDX (t)];
1215
1216 case FUNCTION_DECL:
1217 {
1218 tree r;
1219 tree fnargs, result;
1220
1221 if (type == TREE_TYPE (t)
1222 && (DECL_CONTEXT (t) == NULL_TREE
1223 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't'))
1224 return t;
1225 fnargs = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1226 result = tsubst (DECL_RESULT (t), args, nargs, t);
1227 if (DECL_CONTEXT (t) != NULL_TREE
1228 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1229 {
1230 /* Look it up in that class, and return the decl node there,
1231 instead of creating a new one. */
1232 tree ctx, methods, name, method;
1233 int n_methods;
1234 int i, found = 0;
1235
1236 name = DECL_NAME (t);
1237 ctx = tsubst (DECL_CONTEXT (t), args, nargs, t);
1238 methods = CLASSTYPE_METHOD_VEC (ctx);
1239 if (methods == NULL_TREE)
1240 /* No methods at all -- no way this one can match. */
1241 goto no_match;
1242 n_methods = TREE_VEC_LENGTH (methods);
1243
1244 r = NULL_TREE;
1245
1246 if (!strncmp (OPERATOR_TYPENAME_FORMAT,
1247 IDENTIFIER_POINTER (name),
1248 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1249 {
1250 /* Type-conversion operator. Reconstruct the name, in
1251 case it's the name of one of the template's parameters. */
1252 name = build_typename_overload (TREE_TYPE (type));
1253 }
1254
1255 if (DECL_CONTEXT (t) != NULL_TREE
1256 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't'
1257 && constructor_name (DECL_CONTEXT (t)) == DECL_NAME (t))
1258 name = constructor_name (ctx);
05910319 1259
1260 if (DECL_CONSTRUCTOR_P (t) && TYPE_USES_VIRTUAL_BASECLASSES (ctx))
1261 {
1262 /* Since we didn't know that this class had virtual bases until after
1263 we instantiated it, we have to recreate the arguments to this
1264 constructor, as otherwise it would miss the __in_chrg parameter. */
1265 tree newtype, parm;
1266 tree parms = TREE_CHAIN (TYPE_ARG_TYPES (type));
1267 parms = hash_tree_chain (integer_type_node, parms);
1268 newtype = build_cplus_method_type (ctx,
1269 TREE_TYPE (type),
1270 parms);
1271 newtype = build_type_variant (newtype,
1272 TYPE_READONLY (type),
1273 TYPE_VOLATILE (type));
1274 type = newtype;
1275
1276 fnargs = copy_node (DECL_ARGUMENTS (t));
bcf789d7 1277 TREE_CHAIN (fnargs) = TREE_CHAIN (DECL_ARGUMENTS (t));
1278
05910319 1279 /* In this case we need "in-charge" flag saying whether
1280 this constructor is responsible for initialization
1281 of virtual baseclasses or not. */
1282 parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
1283 /* Mark the artificial `__in_chrg' parameter as "artificial". */
1284 SET_DECL_ARTIFICIAL (parm);
1285 DECL_ARG_TYPE (parm) = integer_type_node;
1286 DECL_REGISTER (parm) = 1;
1287 TREE_CHAIN (parm) = TREE_CHAIN (fnargs);
1288 TREE_CHAIN (fnargs) = parm;
1289
1290 fnargs = tsubst (fnargs, args, nargs, t);
1291 }
471086d6 1292#if 0
1293 fprintf (stderr, "\nfor function %s in class %s:\n",
1294 IDENTIFIER_POINTER (name),
1295 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
1296#endif
1297 for (i = 0; i < n_methods; i++)
1298 {
1299 int pass;
1300
1301 method = TREE_VEC_ELT (methods, i);
1302 if (method == NULL_TREE || DECL_NAME (method) != name)
1303 continue;
1304
1305 pass = 0;
1306 maybe_error:
1307 for (; method; method = DECL_CHAIN (method))
1308 {
1309 my_friendly_assert (TREE_CODE (method) == FUNCTION_DECL,
1310 282);
0543e7a9 1311 if (! comptypes (type, TREE_TYPE (method), 1))
471086d6 1312 {
1313 tree mtype = TREE_TYPE (method);
1314 tree t1, t2;
1315
1316 /* Keep looking for a method that matches
1317 perfectly. This takes care of the problem
1318 where destructors (which have implicit int args)
1319 look like constructors which have an int arg. */
1320 if (pass == 0)
1321 continue;
1322
1323 t1 = TYPE_ARG_TYPES (mtype);
1324 t2 = TYPE_ARG_TYPES (type);
1325 if (TREE_CODE (mtype) == FUNCTION_TYPE)
1326 t2 = TREE_CHAIN (t2);
1327
1328 if (list_eq (t1, t2))
1329 {
1330 if (TREE_CODE (mtype) == FUNCTION_TYPE)
1331 {
1332 tree newtype;
1333 newtype = build_function_type (TREE_TYPE (type),
1334 TYPE_ARG_TYPES (type));
1335 newtype = build_type_variant (newtype,
1336 TYPE_READONLY (type),
1337 TYPE_VOLATILE (type));
1338 type = newtype;
1339 if (TREE_TYPE (type) != TREE_TYPE (mtype))
1340 goto maybe_bad_return_type;
1341 }
1342 else if (TYPE_METHOD_BASETYPE (mtype)
1343 == TYPE_METHOD_BASETYPE (type))
1344 {
1345 /* Types didn't match, but arg types and
1346 `this' do match, so the return type is
1347 all that should be messing it up. */
1348 maybe_bad_return_type:
1349 if (TREE_TYPE (type) != TREE_TYPE (mtype))
1350 error ("inconsistent return types for method `%s' in class `%s'",
1351 IDENTIFIER_POINTER (name),
1352 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
1353 }
1354 r = method;
1355 break;
1356 }
1357 found = 1;
1358 continue;
1359 }
1360#if 0
1361 fprintf (stderr, "\tfound %s\n\n",
1362 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method)));
1363#endif
617abf06 1364 if (DECL_ARTIFICIAL (method))
1365 {
1366 cp_error ("template for method `%D' which has default implementation in class `%T'", name, ctx);
1367 if (in_decl)
1368 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
1369 return error_mark_node;
1370 }
471086d6 1371
1372 if (DECL_ARGUMENTS (method)
1373 && ! TREE_PERMANENT (DECL_ARGUMENTS (method)))
1374 /* @@ Is this early enough? Might we want to do
1375 this instead while processing the expansion? */
1376 DECL_ARGUMENTS (method)
1377 = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1378 r = method;
1379 break;
1380 }
1381 if (r == NULL_TREE && pass == 0)
1382 {
1383 pass = 1;
1384 method = TREE_VEC_ELT (methods, i);
1385 goto maybe_error;
1386 }
1387 }
1388 if (r == NULL_TREE)
1389 {
1390 no_match:
1391 cp_error
1392 (found
1393 ? "template for method `%D' doesn't match any in class `%T'"
1394 : "method `%D' not found in class `%T'", name, ctx);
1395 if (in_decl)
1396 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
1397 return error_mark_node;
1398 }
1399 }
1400 else
1401 {
1402 r = DECL_NAME (t);
1403 {
1404 tree decls;
1405 int got_it = 0;
1406
ddb9bca7 1407 decls = lookup_name_nonclass (r);
471086d6 1408 if (decls == NULL_TREE)
1409 /* no match */;
1410 else if (TREE_CODE (decls) == TREE_LIST)
1411 for (decls = TREE_VALUE (decls); decls ;
1412 decls = DECL_CHAIN (decls))
1413 {
1414 if (TREE_CODE (decls) == FUNCTION_DECL
1415 && TREE_TYPE (decls) == type)
1416 {
1417 got_it = 1;
1418 r = decls;
1419 break;
1420 }
1421 }
1422 else
1423 {
1424 tree val = decls;
1425 decls = NULL_TREE;
1426 if (TREE_CODE (val) == FUNCTION_DECL
1427 && TREE_TYPE (val) == type)
1428 {
1429 got_it = 1;
1430 r = val;
471086d6 1431 }
1432 }
1433
1434 if (!got_it)
1435 {
c38086bd 1436 tree a = build_decl_overload (r, TYPE_VALUES (type),
1437 DECL_CONTEXT (t) != NULL_TREE);
471086d6 1438 r = build_lang_decl (FUNCTION_DECL, r, type);
c38086bd 1439 DECL_ASSEMBLER_NAME (r) = a;
471086d6 1440 }
bcf789d7 1441 else if (TREE_STATIC (r))
bb0726a1 1442 {
1443 /* This overrides the template version, use it. */
1444 return r;
1445 }
471086d6 1446 }
1447 }
c25194fd 1448 TREE_PUBLIC (r) = 1;
1449 DECL_EXTERNAL (r) = 1;
1450 TREE_STATIC (r) = 0;
1451 DECL_INTERFACE_KNOWN (r) = 0;
471086d6 1452 DECL_INLINE (r) = DECL_INLINE (t);
bb09dca5 1453 DECL_THIS_INLINE (r) = DECL_THIS_INLINE (t);
471086d6 1454 {
1455#if 0 /* Maybe later. -jason */
1456 struct tinst_level *til = tinst_for_decl();
1457
1458 /* should always be true under new approach */
1459 if (til)
1460 {
1461 DECL_SOURCE_FILE (r) = til->file;
1462 DECL_SOURCE_LINE (r) = til->line;
1463 }
1464 else
1465#endif
1466 {
1467 DECL_SOURCE_FILE (r) = DECL_SOURCE_FILE (t);
1468 DECL_SOURCE_LINE (r) = DECL_SOURCE_LINE (t);
1469 }
1470 }
1471 DECL_CLASS_CONTEXT (r) = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
1472 make_decl_rtl (r, NULL_PTR, 1);
1473 DECL_ARGUMENTS (r) = fnargs;
1474 DECL_RESULT (r) = result;
c38086bd 1475#if 0
471086d6 1476 if (DECL_CONTEXT (t) == NULL_TREE
1477 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't')
1478 push_overloaded_decl_top_level (r, 0);
c38086bd 1479#endif
471086d6 1480 return r;
1481 }
1482
1483 case PARM_DECL:
1484 {
1485 tree r;
1486 r = build_decl (PARM_DECL, DECL_NAME (t), type);
1487 DECL_INITIAL (r) = TREE_TYPE (r);
1488 if (TREE_CHAIN (t))
1489 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
1490 return r;
1491 }
1492
1493 case TREE_LIST:
1494 {
1495 tree purpose, value, chain, result;
1496 int via_public, via_virtual, via_protected;
1497
1498 if (t == void_list_node)
1499 return t;
1500
1501 via_public = TREE_VIA_PUBLIC (t);
1502 via_protected = TREE_VIA_PROTECTED (t);
1503 via_virtual = TREE_VIA_VIRTUAL (t);
1504
1505 purpose = TREE_PURPOSE (t);
1506 if (purpose)
1507 purpose = tsubst (purpose, args, nargs, in_decl);
1508 value = TREE_VALUE (t);
1509 if (value)
1510 value = tsubst (value, args, nargs, in_decl);
1511 chain = TREE_CHAIN (t);
1512 if (chain && chain != void_type_node)
1513 chain = tsubst (chain, args, nargs, in_decl);
1514 if (purpose == TREE_PURPOSE (t)
1515 && value == TREE_VALUE (t)
1516 && chain == TREE_CHAIN (t))
1517 return t;
1518 result = hash_tree_cons (via_public, via_virtual, via_protected,
1519 purpose, value, chain);
1520 TREE_PARMLIST (result) = TREE_PARMLIST (t);
1521 return result;
1522 }
1523 case TREE_VEC:
1524 {
1525 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
1526 tree *elts = (tree *) alloca (len * sizeof (tree));
ca613dde 1527 bzero ((char *) elts, len * sizeof (tree));
471086d6 1528
1529 for (i = 0; i < len; i++)
1530 {
1531 elts[i] = tsubst (TREE_VEC_ELT (t, i), args, nargs, in_decl);
1532 if (elts[i] != TREE_VEC_ELT (t, i))
1533 need_new = 1;
1534 }
1535
1536 if (!need_new)
1537 return t;
1538
1539 t = make_tree_vec (len);
1540 for (i = 0; i < len; i++)
1541 TREE_VEC_ELT (t, i) = elts[i];
1542 return t;
1543 }
1544 case POINTER_TYPE:
1545 case REFERENCE_TYPE:
1546 {
1547 tree r;
1548 enum tree_code code;
1549 if (type == TREE_TYPE (t))
1550 return t;
1551
1552 code = TREE_CODE (t);
1553 if (code == POINTER_TYPE)
1554 r = build_pointer_type (type);
1555 else
1556 r = build_reference_type (type);
c38086bd 1557 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
471086d6 1558 /* Will this ever be needed for TYPE_..._TO values? */
1559 layout_type (r);
1560 return r;
1561 }
d81e00a4 1562 case OFFSET_TYPE:
1563 return build_offset_type
1564 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
471086d6 1565 case FUNCTION_TYPE:
1566 case METHOD_TYPE:
1567 {
1568 tree values = TYPE_VALUES (t); /* same as TYPE_ARG_TYPES */
1569 tree context = TYPE_CONTEXT (t);
1570 tree new_value;
1571
1572 /* Don't bother recursing if we know it won't change anything. */
1573 if (values != void_list_node)
1574 values = tsubst (values, args, nargs, in_decl);
1575 if (context)
1576 context = tsubst (context, args, nargs, in_decl);
1577 /* Could also optimize cases where return value and
1578 values have common elements (e.g., T min(const &T, const T&). */
1579
1580 /* If the above parameters haven't changed, just return the type. */
1581 if (type == TREE_TYPE (t)
1582 && values == TYPE_VALUES (t)
1583 && context == TYPE_CONTEXT (t))
1584 return t;
1585
1586 /* Construct a new type node and return it. */
1587 if (TREE_CODE (t) == FUNCTION_TYPE
1588 && context == NULL_TREE)
1589 {
1590 new_value = build_function_type (type, values);
1591 }
1592 else if (context == NULL_TREE)
1593 {
1594 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
1595 args, nargs, in_decl);
1596 new_value = build_cplus_method_type (base, type,
1597 TREE_CHAIN (values));
1598 }
1599 else
1600 {
1601 new_value = make_node (TREE_CODE (t));
1602 TREE_TYPE (new_value) = type;
1603 TYPE_CONTEXT (new_value) = context;
1604 TYPE_VALUES (new_value) = values;
1605 TYPE_SIZE (new_value) = TYPE_SIZE (t);
1606 TYPE_ALIGN (new_value) = TYPE_ALIGN (t);
1607 TYPE_MODE (new_value) = TYPE_MODE (t);
1608 if (TYPE_METHOD_BASETYPE (t))
1609 TYPE_METHOD_BASETYPE (new_value) = tsubst (TYPE_METHOD_BASETYPE (t),
1610 args, nargs, in_decl);
1611 /* Need to generate hash value. */
1612 my_friendly_abort (84);
1613 }
1614 new_value = build_type_variant (new_value,
1615 TYPE_READONLY (t),
1616 TYPE_VOLATILE (t));
1617 return new_value;
1618 }
1619 case ARRAY_TYPE:
1620 {
1621 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
1622 tree r;
1623 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
1624 return t;
1625 r = build_cplus_array_type (type, domain);
1626 return r;
1627 }
1628
1629 case UNINSTANTIATED_P_TYPE:
1630 {
1631 int nparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t)));
1632 tree argvec = make_tree_vec (nparms);
1633 tree parmvec = UPT_PARMS (t);
1634 int i;
1635 tree id, rt;
1636 for (i = 0; i < nparms; i++)
1637 TREE_VEC_ELT (argvec, i) = tsubst (TREE_VEC_ELT (parmvec, i),
1638 args, nargs, in_decl);
1639 id = lookup_template_class (DECL_NAME (UPT_TEMPLATE (t)), argvec, NULL_TREE);
1640 if (! IDENTIFIER_HAS_TYPE_VALUE (id)) {
1641 instantiate_class_template(id, 0);
1642 /* set up pending_classes */
1643 add_pending_template (id);
1644
1645 TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id)) =
1646 IDENTIFIER_TYPE_VALUE (id);
1647 }
1648 rt = IDENTIFIER_TYPE_VALUE (id);
1649
1650 /* kung: this part handles nested type in template definition */
1651
1652 if ( !ANON_AGGRNAME_P (DECL_NAME(TYPE_NAME(t))))
1653 {
1654 rt = search_nested_type_in_tmpl (rt, t);
1655 }
1656
1657 return build_type_variant (rt, TYPE_READONLY (t), TYPE_VOLATILE (t));
1658 }
1659
1660 case MINUS_EXPR:
1661 case PLUS_EXPR:
1662 return fold (build (TREE_CODE (t), TREE_TYPE (t),
1663 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1664 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
1665
1666 case NEGATE_EXPR:
1667 case NOP_EXPR:
1668 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
1669 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
1670
1671 default:
1672 sorry ("use of `%s' in function template",
1673 tree_code_name [(int) TREE_CODE (t)]);
1674 return error_mark_node;
1675 }
1676}
1677
1678tree
1679instantiate_template (tmpl, targ_ptr)
1680 tree tmpl, *targ_ptr;
1681{
1682 tree targs, fndecl;
1683 int i, len;
1684 struct pending_inline *p;
1685 struct template_info *t;
1686 struct obstack *old_fmp_obstack;
1687 extern struct obstack *function_maybepermanent_obstack;
1688
1689 push_obstacks (&permanent_obstack, &permanent_obstack);
1690 old_fmp_obstack = function_maybepermanent_obstack;
1691 function_maybepermanent_obstack = &permanent_obstack;
1692
1693 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
1694 len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
1695
1e66592c 1696 i = len;
1697 while (i--)
1698 targ_ptr[i] = copy_to_permanent (targ_ptr[i]);
1699
471086d6 1700 for (fndecl = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1701 fndecl; fndecl = TREE_CHAIN (fndecl))
1702 {
1703 tree *t1 = &TREE_VEC_ELT (TREE_PURPOSE (fndecl), 0);
1704 for (i = len - 1; i >= 0; i--)
1e66592c 1705 if (simple_cst_equal (t1[i], targ_ptr[i]) <= 0)
471086d6 1706 goto no_match;
1707
1708 /* Here, we have a match. */
1709 fndecl = TREE_VALUE (fndecl);
617abf06 1710 goto exit;
471086d6 1711
1712 no_match:
1713 ;
1714 }
1715
1716 targs = make_tree_vec (len);
1717 i = len;
1718 while (i--)
1719 TREE_VEC_ELT (targs, i) = targ_ptr[i];
1720
1721 /* substitute template parameters */
1722 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr,
1723 TREE_VEC_LENGTH (targs), tmpl);
1724
617abf06 1725 if (fndecl == error_mark_node)
1726 goto exit;
1727
bea7d742 1728 assemble_external (fndecl);
1729
471086d6 1730 /* If it's a static member fn in the template, we need to change it
1731 into a FUNCTION_TYPE and chop off its this pointer. */
1732 if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl))) == METHOD_TYPE
471086d6 1733 && DECL_STATIC_FUNCTION_P (fndecl))
1734 {
1735 tree olddecl = DECL_RESULT (tmpl);
94f3b32d 1736 revert_static_member_fn (&DECL_RESULT (tmpl), NULL, NULL);
471086d6 1737 /* Chop off the this pointer that grokclassfn so kindly added
1738 for us (it didn't know yet if the fn was static or not). */
1739 DECL_ARGUMENTS (olddecl) = TREE_CHAIN (DECL_ARGUMENTS (olddecl));
1740 DECL_ARGUMENTS (fndecl) = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1741 }
1742
ddb9bca7 1743 t = DECL_TEMPLATE_INFO (tmpl);
1744
bb0726a1 1745 /* If we have a preexisting version of this function, don't expand
1746 the template version, use the other instead. */
bcf789d7 1747 if (TREE_STATIC (fndecl))
471086d6 1748 {
ddb9bca7 1749 SET_DECL_TEMPLATE_SPECIALIZATION (fndecl);
1750 p = (struct pending_inline *)0;
1751 }
1752 else if (t->text)
1753 {
1754 SET_DECL_IMPLICIT_INSTANTIATION (fndecl);
bb09dca5 1755 repo_template_used (fndecl);
471086d6 1756 p = (struct pending_inline *) permalloc (sizeof (struct pending_inline));
1757 p->parm_vec = t->parm_vec;
1758 p->bindings = targs;
1759 p->can_free = 0;
1760 p->deja_vu = 0;
1761 p->buf = t->text;
1762 p->len = t->length;
1763 p->fndecl = fndecl;
1764 {
1765 int l = lineno;
1766 char * f = input_filename;
1767
1768 lineno = p->lineno = t->lineno;
1769 input_filename = p->filename = t->filename;
1770
1771 extract_interface_info ();
c25194fd 1772
1773 if (interface_unknown && flag_external_templates)
1774 {
1775 if (DECL_CLASS_CONTEXT (fndecl)
1776 && CLASSTYPE_INTERFACE_KNOWN (DECL_CLASS_CONTEXT (fndecl)))
1777 {
1778 interface_unknown = 0;
1779 interface_only
1780 = CLASSTYPE_INTERFACE_ONLY (DECL_CLASS_CONTEXT (fndecl));
1781 }
1782 else if (! DECL_IN_SYSTEM_HEADER (tmpl))
1a3f833b 1783 warn_if_unknown_interface (tmpl);
c25194fd 1784 }
1785
1786 if (interface_unknown || ! flag_external_templates)
471086d6 1787 p->interface = 1; /* unknown */
1788 else
1789 p->interface = interface_only ? 0 : 2;
1790
1791 lineno = l;
1792 input_filename = f;
1793
1794 extract_interface_info ();
1795 }
1796 }
1797 else
1798 p = (struct pending_inline *)0;
1799
1800 DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
1801 tree_cons (targs, fndecl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1802
617abf06 1803 if (p == (struct pending_inline *)0)
471086d6 1804 {
1805 /* do nothing */
1806 }
1807 else if (DECL_INLINE (fndecl))
1808 {
1809 DECL_PENDING_INLINE_INFO (fndecl) = p;
1810 p->next = pending_inlines;
1811 pending_inlines = p;
1812 }
1813 else
1814 {
1815 p->next = pending_template_expansions;
1816 pending_template_expansions = p;
1817 }
617abf06 1818 exit:
1819 function_maybepermanent_obstack = old_fmp_obstack;
1820 pop_obstacks ();
1821
471086d6 1822 return fndecl;
1823}
1824
e581f478 1825/* classlevel should now never be true. jason 4/12/94 */
471086d6 1826void
1827undo_template_name_overload (id, classlevel)
1828 tree id;
1829 int classlevel;
1830{
1831 tree template;
1832
1833 template = IDENTIFIER_TEMPLATE (id);
1834 if (!template)
1835 return;
1836
1837#if 0 /* not yet, should get fixed properly later */
1838 poplevel (0, 0, 0);
1839#endif
1840#if 1 /* XXX */
1841 /* This was a botch... See `overload_template_name' just below. */
1842 if (!classlevel)
1843 poplevel (0, 0, 0);
1844#endif
1845}
1846
e581f478 1847/* classlevel should now never be true. jason 4/12/94 */
471086d6 1848void
1849overload_template_name (id, classlevel)
1850 tree id;
1851 int classlevel;
1852{
1853 tree template, t, decl;
1854 struct template_info *tinfo;
1855
1856 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 284);
1857 template = IDENTIFIER_TEMPLATE (id);
1858 if (!template)
1859 return;
1860
1861 template = TREE_PURPOSE (template);
1862 tinfo = DECL_TEMPLATE_INFO (template);
1863 template = DECL_NAME (template);
1864 my_friendly_assert (template != NULL_TREE, 285);
1865
1866#if 1 /* XXX */
1867 /* This was a botch... names of templates do not get their own private
e581f478 1868 scopes. Rather, they should go into the binding level already created
1869 by push_template_decls. Except that there isn't one of those for
1870 specializations. */
471086d6 1871 if (!classlevel)
1872 {
1873 pushlevel (1);
1874 declare_pseudo_global_level ();
1875 }
1876#endif
1877
1a3f833b 1878 t = xref_tag (tinfo->aggr, id, NULL_TREE, 1);
471086d6 1879 my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
1880 || TREE_CODE (t) == UNION_TYPE
1881 || TREE_CODE (t) == UNINSTANTIATED_P_TYPE, 286);
1882
1883 decl = build_decl (TYPE_DECL, template, t);
f3ba5c6a 1884 SET_DECL_ARTIFICIAL (decl);
471086d6 1885
1886#if 0 /* fix this later */
1887 /* We don't want to call here if the work has already been done. */
1888 t = (classlevel
1889 ? IDENTIFIER_CLASS_VALUE (template)
1890 : IDENTIFIER_LOCAL_VALUE (template));
1891 if (t
1892 && TREE_CODE (t) == TYPE_DECL
1893 && TREE_TYPE (t) == t)
1894 my_friendly_abort (85);
1895#endif
1896
1897 if (classlevel)
1898 pushdecl_class_level (decl);
1899 else
471086d6 1900 pushdecl (decl);
471086d6 1901
e581f478 1902#if 0 /* This seems bogus to me; if it isn't, explain why. (jason) */
471086d6 1903 /* Fake this for now, just to make dwarfout.c happy. It will have to
1904 be done in a proper way later on. */
1905 DECL_CONTEXT (decl) = t;
e581f478 1906#endif
471086d6 1907}
1908
1909/* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
1910void
1911end_template_instantiation (name)
1912 tree name;
1913{
1914 extern struct pending_input *to_be_restored;
1915 tree t, decl;
1916
1917 processing_template_defn--;
1918 if (!flag_external_templates)
1919 interface_unknown--;
1920
1921 /* Restore the old parser input state. */
1922 if (yychar == YYEMPTY)
1923 yychar = yylex ();
1924 if (yychar != END_OF_SAVED_INPUT)
1925 error ("parse error at end of class template");
1926 else
1927 {
1928 restore_pending_input (to_be_restored);
1929 to_be_restored = 0;
1930 }
1931
1932 /* Our declarations didn't get stored in the global slot, since
1933 there was a (supposedly tags-transparent) scope in between. */
1934 t = IDENTIFIER_TYPE_VALUE (name);
1935 my_friendly_assert (t != NULL_TREE
1936 && TREE_CODE_CLASS (TREE_CODE (t)) == 't',
1937 287);
ddb9bca7 1938 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
471086d6 1939 /* Make methods of template classes static, unless
1940 -fexternal-templates is given. */
1941 if (!flag_external_templates)
1942 SET_CLASSTYPE_INTERFACE_UNKNOWN (t);
1943 decl = IDENTIFIER_GLOBAL_VALUE (name);
1944 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 288);
1945
1946 undo_template_name_overload (name, 0);
1947 t = IDENTIFIER_TEMPLATE (name);
1948 pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t)), TREE_VALUE (t),
1949 0);
1950 /* This will fix up the type-value field. */
1951 pushdecl (decl);
1952 pop_from_top_level ();
1953
1954#ifdef DWARF_DEBUGGING_INFO
1955 if (write_symbols == DWARF_DEBUG && TREE_CODE (decl) == TYPE_DECL)
1956 {
1957 /* We just completed the definition of a new file-scope type,
1958 so we can go ahead and output debug-info for it now. */
1959 TYPE_STUB_DECL (TREE_TYPE (decl)) = decl;
1960 rest_of_type_compilation (TREE_TYPE (decl), 1);
1961 }
1962#endif /* DWARF_DEBUGGING_INFO */
1963
1964 /* Restore interface/implementation settings. */
1965 extract_interface_info ();
1966}
1967\f
ac9386a0 1968/* Store away the text of an template. */
471086d6 1969
1970void
1971reinit_parse_for_template (yychar, d1, d2)
1972 int yychar;
1973 tree d1, d2;
1974{
1975 struct template_info *template_info;
ac9386a0 1976 extern struct obstack inline_text_obstack; /* see comment in lex.c */
471086d6 1977
1978 if (d2 == NULL_TREE || d2 == error_mark_node)
1979 {
1980 lose:
1981 /* @@ Should use temp obstack, and discard results. */
1982 reinit_parse_for_block (yychar, &inline_text_obstack, 1);
1983 return;
1984 }
1985
1986 if (TREE_CODE (d2) == IDENTIFIER_NODE)
1987 d2 = IDENTIFIER_GLOBAL_VALUE (d2);
1988 if (!d2)
1989 goto lose;
1990 template_info = DECL_TEMPLATE_INFO (d2);
1991 if (!template_info)
1992 {
1993 template_info = (struct template_info *) permalloc (sizeof (struct template_info));
ca613dde 1994 bzero ((char *) template_info, sizeof (struct template_info));
471086d6 1995 DECL_TEMPLATE_INFO (d2) = template_info;
1996 }
1997 template_info->filename = input_filename;
1998 template_info->lineno = lineno;
1999 reinit_parse_for_block (yychar, &inline_text_obstack, 1);
2000 template_info->text = obstack_base (&inline_text_obstack);
2001 template_info->length = obstack_object_size (&inline_text_obstack);
2002 obstack_finish (&inline_text_obstack);
2003 template_info->parm_vec = d1;
2004}
2005
2006/* Type unification.
2007
2008 We have a function template signature with one or more references to
2009 template parameters, and a parameter list we wish to fit to this
2010 template. If possible, produce a list of parameters for the template
2011 which will cause it to fit the supplied parameter list.
2012
2013 Return zero for success, 2 for an incomplete match that doesn't resolve
2014 all the types, and 1 for complete failure. An error message will be
2015 printed only for an incomplete match.
2016
2017 TPARMS[NTPARMS] is an array of template parameter types;
2018 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2019 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2020 and ARGS is the argument list we're trying to match against it.
2021
2022 If SUBR is 1, we're being called recursively (to unify the arguments of
2023 a function or method parameter of a function template), so don't zero
2024 out targs and don't fail on an incomplete match. */
2025
2026int
2027type_unification (tparms, targs, parms, args, nsubsts, subr)
2028 tree tparms, *targs, parms, args;
2029 int *nsubsts, subr;
2030{
2031 tree parm, arg;
2032 int i;
2033 int ntparms = TREE_VEC_LENGTH (tparms);
2034
2035 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
2036 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
ac9386a0 2037 /* ARGS could be NULL (via a call from parse.y to
471086d6 2038 build_x_function_call). */
2039 if (args)
2040 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
2041 my_friendly_assert (ntparms > 0, 292);
2042
2043 if (!subr)
ca613dde 2044 bzero ((char *) targs, sizeof (tree) * ntparms);
471086d6 2045
2046 while (parms
2047 && parms != void_list_node
2048 && args
2049 && args != void_list_node)
2050 {
2051 parm = TREE_VALUE (parms);
2052 parms = TREE_CHAIN (parms);
2053 arg = TREE_VALUE (args);
2054 args = TREE_CHAIN (args);
2055
2056 if (arg == error_mark_node)
2057 return 1;
2058 if (arg == unknown_type_node)
2059 return 1;
1e66592c 2060
2061 if (! uses_template_parms (parm)
2062 && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2063 {
2064 if (can_convert_arg (parm, TREE_TYPE (arg), arg))
2065 continue;
2066 return 1;
2067 }
2068
471086d6 2069#if 0
2070 if (TREE_CODE (arg) == VAR_DECL)
2071 arg = TREE_TYPE (arg);
2072 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
2073 arg = TREE_TYPE (arg);
2074#else
2075 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2076 {
2077 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
bea7d742 2078 if (TREE_CODE (arg) == TREE_LIST
2079 && TREE_TYPE (arg) == unknown_type_node
2080 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
2081 {
2082 int nsubsts, ntparms;
2083 tree *targs;
2084
2085 /* Have to back unify here */
2086 arg = TREE_VALUE (arg);
2087 nsubsts = 0;
2088 ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
2089 targs = (tree *) alloca (sizeof (tree) * ntparms);
2090 parm = tree_cons (NULL_TREE, parm, NULL_TREE);
2091 return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
2092 TYPE_ARG_TYPES (TREE_TYPE (arg)),
2093 parm, &nsubsts, 0);
2094 }
471086d6 2095 arg = TREE_TYPE (arg);
2096 }
2097#endif
c25194fd 2098 if (TREE_CODE (arg) == REFERENCE_TYPE)
2099 arg = TREE_TYPE (arg);
2100
c45a8a47 2101 if (TREE_CODE (parm) != REFERENCE_TYPE)
2102 {
2103 if (TREE_CODE (arg) == FUNCTION_TYPE
2104 || TREE_CODE (arg) == METHOD_TYPE)
2105 arg = build_pointer_type (arg);
2106 else if (TREE_CODE (arg) == ARRAY_TYPE)
2107 arg = build_pointer_type (TREE_TYPE (arg));
2108 else
2109 arg = TYPE_MAIN_VARIANT (arg);
2110 }
471086d6 2111
2112 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts))
2113 {
2114 case 0:
2115 break;
2116 case 1:
2117 return 1;
2118 }
2119 }
2120 /* Fail if we've reached the end of the parm list, and more args
2121 are present, and the parm list isn't variadic. */
2122 if (args && args != void_list_node && parms == void_list_node)
2123 return 1;
2124 /* Fail if parms are left and they don't have default values. */
2125 if (parms
2126 && parms != void_list_node
2127 && TREE_PURPOSE (parms) == NULL_TREE)
2128 return 1;
2129 if (!subr)
2130 for (i = 0; i < ntparms; i++)
2131 if (!targs[i])
2132 {
2133 error ("incomplete type unification");
2134 return 2;
2135 }
2136 return 0;
2137}
2138
2139/* Tail recursion is your friend. */
2140static int
2141unify (tparms, targs, ntparms, parm, arg, nsubsts)
2142 tree tparms, *targs, parm, arg;
2143 int *nsubsts, ntparms;
2144{
2145 int idx;
2146
2147 /* I don't think this will do the right thing with respect to types.
2148 But the only case I've seen it in so far has been array bounds, where
2149 signedness is the only information lost, and I think that will be
2150 okay. */
2151 while (TREE_CODE (parm) == NOP_EXPR)
2152 parm = TREE_OPERAND (parm, 0);
2153
2154 if (arg == error_mark_node)
2155 return 1;
2156 if (arg == unknown_type_node)
2157 return 1;
2158 if (arg == parm)
2159 return 0;
2160
471086d6 2161 switch (TREE_CODE (parm))
2162 {
2163 case TEMPLATE_TYPE_PARM:
2164 (*nsubsts)++;
2165 if (TEMPLATE_TYPE_TPARMLIST (parm) != tparms)
2166 {
2167 error ("mixed template headers?!");
2168 my_friendly_abort (86);
2169 return 1;
2170 }
2171 idx = TEMPLATE_TYPE_IDX (parm);
c25194fd 2172#if 0
f3ba5c6a 2173 /* Template type parameters cannot contain cv-quals; i.e.
2174 template <class T> void f (T& a, T& b) will not generate
2175 void f (const int& a, const int& b). */
2176 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
2177 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
2178 return 1;
2179 arg = TYPE_MAIN_VARIANT (arg);
c25194fd 2180#else
2181 {
2182 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
2183 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
2184 arg = cp_build_type_variant (arg, constp, volatilep);
2185 }
2186#endif
471086d6 2187 /* Simple cases: Value already set, does match or doesn't. */
2188 if (targs[idx] == arg)
2189 return 0;
2190 else if (targs[idx])
471086d6 2191 return 1;
f3ba5c6a 2192 /* Check for mixed types and values. */
2193 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
d81e00a4 2194 return 1;
471086d6 2195 targs[idx] = arg;
2196 return 0;
2197 case TEMPLATE_CONST_PARM:
2198 (*nsubsts)++;
2199 idx = TEMPLATE_CONST_IDX (parm);
2200 if (targs[idx] == arg)
2201 return 0;
2202 else if (targs[idx])
2203 {
e581f478 2204 tree t = targs[idx];
2205 if (TREE_CODE (t) == TREE_CODE (arg))
2206 switch (TREE_CODE (arg))
2207 {
2208 case INTEGER_CST:
2209 if (tree_int_cst_equal (t, arg))
2210 return 0;
2211 break;
2212 case REAL_CST:
2213 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
2214 return 0;
2215 break;
2216 /* STRING_CST values are not valid template const parms. */
2217 default:
2218 ;
2219 }
471086d6 2220 my_friendly_abort (87);
2221 return 1;
2222 }
2223/* else if (typeof arg != tparms[idx])
2224 return 1;*/
2225
2226 targs[idx] = copy_to_permanent (arg);
2227 return 0;
2228
2229 case POINTER_TYPE:
2230 if (TREE_CODE (arg) != POINTER_TYPE)
2231 return 1;
2232 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2233 nsubsts);
2234
2235 case REFERENCE_TYPE:
bea7d742 2236 if (TREE_CODE (arg) == REFERENCE_TYPE)
2237 arg = TREE_TYPE (arg);
471086d6 2238 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, nsubsts);
2239
2240 case ARRAY_TYPE:
2241 if (TREE_CODE (arg) != ARRAY_TYPE)
2242 return 1;
2243 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2244 nsubsts) != 0)
2245 return 1;
2246 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2247 nsubsts);
2248
2249 case REAL_TYPE:
2250 case INTEGER_TYPE:
c38086bd 2251 if (TREE_CODE (arg) != TREE_CODE (parm))
2252 return 1;
2253
2254 if (TREE_CODE (parm) == INTEGER_TYPE)
471086d6 2255 {
2256 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2257 && unify (tparms, targs, ntparms,
2258 TYPE_MIN_VALUE (parm), TYPE_MIN_VALUE (arg), nsubsts))
2259 return 1;
2260 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2261 && unify (tparms, targs, ntparms,
2262 TYPE_MAX_VALUE (parm), TYPE_MAX_VALUE (arg), nsubsts))
2263 return 1;
2264 }
2265 /* As far as unification is concerned, this wins. Later checks
2266 will invalidate it if necessary. */
2267 return 0;
2268
2269 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2270 case INTEGER_CST:
2271 if (TREE_CODE (arg) != INTEGER_CST)
2272 return 1;
2273 return !tree_int_cst_equal (parm, arg);
2274
2275 case MINUS_EXPR:
2276 {
2277 tree t1, t2;
2278 t1 = TREE_OPERAND (parm, 0);
2279 t2 = TREE_OPERAND (parm, 1);
471086d6 2280 return unify (tparms, targs, ntparms, t1,
2281 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2282 nsubsts);
2283 }
2284
2285 case TREE_VEC:
2286 {
2287 int i;
2288 if (TREE_CODE (arg) != TREE_VEC)
2289 return 1;
2290 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2291 return 1;
2292 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2293 if (unify (tparms, targs, ntparms,
2294 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2295 nsubsts))
2296 return 1;
2297 return 0;
2298 }
2299
2300 case UNINSTANTIATED_P_TYPE:
2301 {
2302 tree a;
c25194fd 2303 /* Unification of something that is not a class fails. */
2304 if (! IS_AGGR_TYPE (arg))
471086d6 2305 return 1;
2306 a = IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg));
c25194fd 2307 if (a && UPT_TEMPLATE (parm) == TREE_PURPOSE (a))
2308 return unify (tparms, targs, ntparms, UPT_PARMS (parm),
2309 TREE_VALUE (a), nsubsts);
2310 /* FIXME: Should check base conversions here. */
2311 return 1;
471086d6 2312 }
2313
2314 case RECORD_TYPE:
c25194fd 2315 if (TYPE_PTRMEMFUNC_FLAG (parm))
471086d6 2316 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2317 arg, nsubsts);
2318
d81e00a4 2319 /* Allow trivial conversions. */
2320 if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg)
2321 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
2322 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2323 return 1;
2324 return 0;
471086d6 2325
2326 case METHOD_TYPE:
2327 if (TREE_CODE (arg) != METHOD_TYPE)
2328 return 1;
2329 goto check_args;
2330
2331 case FUNCTION_TYPE:
2332 if (TREE_CODE (arg) != FUNCTION_TYPE)
2333 return 1;
2334 check_args:
bea7d742 2335 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
2336 TREE_TYPE (arg), nsubsts))
2337 return 1;
471086d6 2338 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2339 TYPE_ARG_TYPES (arg), nsubsts, 1);
d81e00a4 2340
2341 case OFFSET_TYPE:
2342 if (TREE_CODE (arg) != OFFSET_TYPE)
2343 return 1;
2344 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
2345 TYPE_OFFSET_BASETYPE (arg), nsubsts))
2346 return 1;
2347 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
2348 TREE_TYPE (arg), nsubsts);
2349
471086d6 2350 default:
2351 sorry ("use of `%s' in template type unification",
2352 tree_code_name [(int) TREE_CODE (parm)]);
2353 return 1;
2354 }
2355}
2356
2357\f
2358#undef DEBUG
2359
2360int
2361do_pending_expansions ()
2362{
2363 struct pending_inline *i, *new_list = 0;
2364
2365 if (!pending_template_expansions)
2366 return 0;
2367
2368#ifdef DEBUG
2369 fprintf (stderr, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
2370#endif
2371
2372 i = pending_template_expansions;
2373 while (i)
2374 {
2375 tree context;
2376
2377 struct pending_inline *next = i->next;
2378 tree t = i->fndecl;
2379
2380 int decision = 0;
0543e7a9 2381#define DECIDE(N) do {decision=(N); goto decided;} while(0)
471086d6 2382
2383 my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
2384 || TREE_CODE (t) == VAR_DECL, 294);
2385 if (TREE_ASM_WRITTEN (t))
2386 DECIDE (0);
b0722fac 2387
ddb9bca7 2388 if (DECL_EXPLICIT_INSTANTIATION (t))
bb09dca5 2389 DECIDE (DECL_NOT_REALLY_EXTERN (t));
ddb9bca7 2390 else if (! flag_implicit_templates)
2391 DECIDE (0);
b0722fac 2392
a74e8896 2393 if (i->interface == 1)
2394 /* OK, it was an implicit instantiation. */
2395 TREE_PUBLIC (t) = 0;
c25194fd 2396
471086d6 2397 /* If it's a method, let the class type decide it.
2398 @@ What if the method template is in a separate file?
2399 Maybe both file contexts should be taken into account?
2400 Maybe only do this if i->interface == 1 (unknown)? */
2401 context = DECL_CONTEXT (t);
2402 if (context != NULL_TREE
2403 && TREE_CODE_CLASS (TREE_CODE (context)) == 't')
2404 {
2405 /* I'm interested in the context of this version of the function,
2406 not the original virtual declaration. */
2407 context = DECL_CLASS_CONTEXT (t);
2408
2409 /* If `unknown', we might want a static copy.
2410 If `implementation', we want a global one.
2411 If `interface', ext ref. */
2412 if (CLASSTYPE_INTERFACE_KNOWN (context))
2413 DECIDE (!CLASSTYPE_INTERFACE_ONLY (context));
2414#if 0 /* This doesn't get us stuff needed only by the file initializer. */
2415 DECIDE (TREE_USED (t));
2416#else /* This compiles too much stuff, but that's probably better in
2417 most cases than never compiling the stuff we need. */
2418 DECIDE (1);
2419#endif
2420 }
2421
2422 if (i->interface == 1)
2423 DECIDE (TREE_USED (t));
2424 else
2425 DECIDE (i->interface);
2426
2427 decided:
2428#ifdef DEBUG
2429 print_node_brief (stderr, decision ? "yes: " : "no: ", t, 0);
2430 fprintf (stderr, "\t%s\n",
2431 (DECL_ASSEMBLER_NAME (t)
2432 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t))
2433 : ""));
2434#endif
2435 if (decision)
2436 {
2437 i->next = pending_inlines;
2438 pending_inlines = i;
2439 }
2440 else
2441 {
2442 i->next = new_list;
2443 new_list = i;
2444 }
2445 i = next;
2446 }
2447 pending_template_expansions = new_list;
2448 if (!pending_inlines)
2449 return 0;
2450 do_pending_inlines ();
2451 return 1;
2452}
2453
2454\f
2455struct pending_template {
2456 struct pending_template *next;
2457 tree id;
2458};
2459
2460static struct pending_template* pending_templates;
2461
2462void
2463do_pending_templates ()
2464{
2465 struct pending_template* t;
2466
2467 for ( t = pending_templates; t; t = t->next)
2468 {
2469 instantiate_class_template (t->id, 1);
2470 }
2471
2472 for ( t = pending_templates; t; t = pending_templates)
2473 {
2474 pending_templates = t->next;
2475 free(t);
2476 }
2477}
2478
2479static void
2480add_pending_template (pt)
2481 tree pt;
2482{
2483 struct pending_template *p;
2484
2485 p = (struct pending_template *) malloc (sizeof (struct pending_template));
2486 p->next = pending_templates;
2487 pending_templates = p;
2488 p->id = pt;
2489}
2490
bb09dca5 2491void
2492mark_function_instantiated (result, extern_p)
2493 tree result;
2494 int extern_p;
2495{
2496 if (DECL_TEMPLATE_INSTANTIATION (result))
2497 SET_DECL_EXPLICIT_INSTANTIATION (result);
2498 TREE_PUBLIC (result) = 1;
2499
2500 if (! extern_p)
2501 {
2502 DECL_INTERFACE_KNOWN (result) = 1;
2503 DECL_NOT_REALLY_EXTERN (result) = 1;
2504 }
2505}
2506
471086d6 2507/* called from the parser. */
2508void
617abf06 2509do_function_instantiation (declspecs, declarator, storage)
2510 tree declspecs, declarator, storage;
471086d6 2511{
2512 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, 0);
2513 tree name = DECL_NAME (decl);
2514 tree fn = IDENTIFIER_GLOBAL_VALUE (name);
2515 tree result = NULL_TREE;
bb09dca5 2516 int extern_p = 0;
471086d6 2517 if (fn)
2518 {
2519 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
bb09dca5 2520 if (decls_match (fn, decl)
2521 && DECL_DEFER_OUTPUT (fn))
2522 {
2523 result = fn;
2524 break;
2525 }
2526 else if (TREE_CODE (fn) == TEMPLATE_DECL)
471086d6 2527 {
2528 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
2529 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
bcf789d7 2530 int i, dummy = 0;
471086d6 2531 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
2532 TYPE_ARG_TYPES (TREE_TYPE (fn)),
2533 TYPE_ARG_TYPES (TREE_TYPE (decl)),
2534 &dummy, 0);
2535 if (i == 0)
2536 {
2537 if (result)
2538 cp_error ("ambiguous template instantiation for `%D' requested", decl);
2539 else
2540 result = instantiate_template (fn, targs);
2541 }
bcf789d7 2542 free (targs);
471086d6 2543 }
2544 }
b0722fac 2545 if (! result)
bb09dca5 2546 {
2547 cp_error ("no matching template for `%D' found", decl);
2548 return;
2549 }
b0722fac 2550
ddb9bca7 2551 if (flag_external_templates)
2552 return;
2553
617abf06 2554 if (storage == NULL_TREE)
6495357a 2555 ;
bb09dca5 2556 else if (storage == ridpointers[(int) RID_EXTERN])
2557 extern_p = 1;
617abf06 2558 else
2559 cp_error ("storage class `%D' applied to template instantiation",
2560 storage);
bb09dca5 2561 mark_function_instantiated (result, extern_p);
b0722fac 2562}
2563
bb09dca5 2564void
2565mark_class_instantiated (t, extern_p)
2566 tree t;
2567 int extern_p;
2568{
2569 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
2570 SET_CLASSTYPE_INTERFACE_KNOWN (t);
2571 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
2572 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
2573 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
2574 if (! extern_p)
2575 {
2576 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2577 rest_of_type_compilation (t, 1);
2578 }
2579}
b0722fac 2580void
617abf06 2581do_type_instantiation (name, storage)
2582 tree name, storage;
b0722fac 2583{
2584 tree t = TREE_TYPE (name);
617abf06 2585 int extern_p;
b0722fac 2586
f3ba5c6a 2587 /* With -fexternal-templates, explicit instantiations are treated the same
2588 as implicit ones. */
ddb9bca7 2589 if (flag_external_templates)
2590 return;
2591
617abf06 2592 if (TYPE_SIZE (t) == NULL_TREE)
2593 {
2594 cp_error ("explicit instantiation of `%#T' before definition of template",
2595 t);
2596 return;
2597 }
2598
2599 if (storage == NULL_TREE)
2600 extern_p = 0;
2601 else if (storage == ridpointers[(int) RID_EXTERN])
2602 extern_p = 1;
2603 else
2604 {
2605 cp_error ("storage class `%D' applied to template instantiation",
2606 storage);
2607 extern_p = 0;
2608 }
2609
f3ba5c6a 2610 /* We've already instantiated this. */
c25194fd 2611 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t))
f3ba5c6a 2612 {
2613 if (! extern_p)
2614 cp_pedwarn ("multiple explicit instantiation of `%#T'", t);
2615 return;
2616 }
2617
c38086bd 2618 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
bb09dca5 2619 mark_class_instantiated (t, extern_p);
c25194fd 2620
b0722fac 2621 {
c25194fd 2622 tree tmp;
2623 /* Classes nested in template classes currently don't have an
2624 IDENTIFIER_TEMPLATE--their out-of-line members are handled
2625 by the enclosing template class. Note that there are name
2626 conflict bugs with this approach. */
2627 tmp = TYPE_IDENTIFIER (t);
2628 if (IDENTIFIER_TEMPLATE (tmp))
2629 instantiate_member_templates (tmp);
2630
2631 /* this should really be done by instantiate_member_templates */
2632 tmp = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
f3ba5c6a 2633 for (; tmp; tmp = TREE_CHAIN (tmp))
ddb9bca7 2634 {
c38086bd 2635 if (DECL_TEMPLATE_SPECIALIZATION (tmp)
2636 || (DECL_USE_TEMPLATE (tmp) == 0
2637 && CLASSTYPE_TEMPLATE_SPECIALIZATION (t)))
2638 continue;
2639
f3ba5c6a 2640 SET_DECL_EXPLICIT_INSTANTIATION (tmp);
c25194fd 2641 TREE_PUBLIC (tmp) = 1;
6495357a 2642 if (! extern_p)
2643 {
c25194fd 2644 DECL_INTERFACE_KNOWN (tmp) = 1;
bb09dca5 2645 DECL_NOT_REALLY_EXTERN (tmp) = 1;
6495357a 2646 }
ddb9bca7 2647 }
b0722fac 2648
f3ba5c6a 2649#if 0
2650 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
2651 {
2652 if (TREE_CODE (tmp) == VAR_DECL)
2653 /* eventually do something */;
2654 }
2655#endif
2656
2657 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
c38086bd 2658 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
2659 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
f3ba5c6a 2660 }
471086d6 2661}
e581f478 2662
2663tree
2664create_nested_upt (scope, name)
2665 tree scope, name;
2666{
2667 tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
ddb9bca7 2668 tree d = build_decl (TYPE_DECL, name, t);
e581f478 2669
2670 TYPE_NAME (t) = d;
2671 TYPE_VALUES (t) = TYPE_VALUES (scope);
2672 TYPE_CONTEXT (t) = scope;
2673
2674 pushdecl (d);
2675 return d;
2676}