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