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