35th Cygnus<->FSF merge
[gcc.git] / gcc / cp / pt.c
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
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the 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
41 extern struct obstack permanent_obstack;
42 extern tree grokdeclarator ();
43
44 extern int lineno;
45 extern char *input_filename;
46 struct pending_inline *pending_template_expansions;
47
48 int processing_template_decl;
49 int processing_template_defn;
50
51 #define obstack_chunk_alloc xmalloc
52 #define obstack_chunk_free free
53
54 static int unify ();
55 static void add_pending_template ();
56
57 void 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.) */
63 void
64 begin_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. */
77 tree
78 process_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 parm = TREE_PURPOSE (parm);
91 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 260);
92 parm = TREE_VALUE (parm);
93 /* is a const-param */
94 parm = grokdeclarator (TREE_VALUE (next), TREE_PURPOSE (next),
95 PARM, 0, NULL_TREE);
96 /* A template parameter is not modifiable. */
97 TREE_READONLY (parm) = 1;
98 if (TREE_CODE (TREE_TYPE (parm)) == RECORD_TYPE
99 || TREE_CODE (TREE_TYPE (parm)) == UNION_TYPE)
100 {
101 sorry ("aggregate template parameter types");
102 TREE_TYPE (parm) = void_type_node;
103 }
104 tinfo = make_node (TEMPLATE_CONST_PARM);
105 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
106 if (TREE_PERMANENT (parm) == 0)
107 {
108 parm = copy_node (parm);
109 TREE_PERMANENT (parm) = 1;
110 }
111 TREE_TYPE (tinfo) = TREE_TYPE (parm);
112 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
113 DECL_INITIAL (decl) = tinfo;
114 DECL_INITIAL (parm) = tinfo;
115 }
116 else
117 {
118 tree t = make_node (TEMPLATE_TYPE_PARM);
119 decl = build_lang_decl (TYPE_DECL, TREE_PURPOSE (parm), t);
120 TYPE_NAME (t) = decl;
121 TREE_VALUE (parm) = t;
122 }
123 pushdecl (decl);
124 return chainon (list, parm);
125 }
126
127 /* The end of a template parameter list has been reached. Process the
128 tree list into a parameter vector, converting each parameter into a more
129 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
130 as PARM_DECLs. */
131
132 tree
133 end_template_parm_list (parms)
134 tree parms;
135 {
136 int nparms = 0;
137 tree saved_parmlist;
138 tree parm;
139 for (parm = parms; parm; parm = TREE_CHAIN (parm))
140 nparms++;
141 saved_parmlist = make_tree_vec (nparms);
142
143 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
144 {
145 tree p = parm;
146 if (TREE_CODE (p) == TREE_LIST)
147 {
148 tree t = TREE_VALUE (p);
149 TREE_VALUE (p) = NULL_TREE;
150 p = TREE_PURPOSE (p);
151 my_friendly_assert (TREE_CODE (p) == IDENTIFIER_NODE, 261);
152 TEMPLATE_TYPE_SET_INFO (t, saved_parmlist, nparms);
153 }
154 else
155 {
156 tree tinfo = DECL_INITIAL (p);
157 DECL_INITIAL (p) = NULL_TREE;
158 TEMPLATE_CONST_SET_INFO (tinfo, saved_parmlist, nparms);
159 }
160 TREE_VEC_ELT (saved_parmlist, nparms) = p;
161 }
162 set_current_level_tags_transparency (1);
163 processing_template_decl++;
164 return saved_parmlist;
165 }
166
167 /* end_template_decl is called after a template declaration is seen.
168 D1 is template header; D2 is class_head_sans_basetype or a
169 TEMPLATE_DECL with its DECL_RESULT field set. */
170 void
171 end_template_decl (d1, d2, is_class, defn)
172 tree d1, d2, is_class;
173 int defn;
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;
257
258 /* So that duplicate_decls can do the right thing. */
259 if (defn)
260 DECL_INITIAL (decl) = error_mark_node;
261
262 /* If context of decl is non-null (i.e., method template), add it
263 to the appropriate class template, and pop the binding levels. */
264 if (! DECL_TEMPLATE_IS_CLASS (decl)
265 && DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl)) != NULL_TREE)
266 {
267 tree ctx = DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl));
268 tree tmpl;
269 my_friendly_assert (TREE_CODE (ctx) == UNINSTANTIATED_P_TYPE, 266);
270 tmpl = UPT_TEMPLATE (ctx);
271 DECL_TEMPLATE_MEMBERS (tmpl) =
272 perm_tree_cons (DECL_NAME (decl), decl,
273 DECL_TEMPLATE_MEMBERS (tmpl));
274 poplevel (0, 0, 0);
275 poplevel (0, 0, 0);
276 }
277 /* Otherwise, go back to top level first, and push the template decl
278 again there. */
279 else
280 {
281 poplevel (0, 0, 0);
282 poplevel (0, 0, 0);
283 pushdecl (decl);
284 }
285 lose:
286 #if 0 /* It happens sometimes, with syntactic or semantic errors.
287
288 One specific case:
289 template <class A, int X, int Y> class Foo { ... };
290 template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
291 Note the missing "A" in the class containing "method". */
292 my_friendly_assert (global_bindings_p (), 267);
293 #else
294 while (! global_bindings_p ())
295 poplevel (0, 0, 0);
296 #endif
297 pop_obstacks ();
298 processing_template_decl--;
299 (void) get_pending_sizes ();
300 }
301
302 /* If TYPE contains a template parm type, then substitute that type
303 with its actual type that is found in TVEC. */
304 static void
305 grok_template_type (tvec, type)
306 tree tvec;
307 tree* type;
308 {
309 switch (TREE_CODE (*type))
310 {
311 case TEMPLATE_TYPE_PARM:
312 if (*type != TYPE_MAIN_VARIANT (*type))
313 {
314 /* we are here for cases like const T* etc. */
315 grok_template_type (tvec, &TYPE_MAIN_VARIANT (*type));
316 *type = build_type_variant (TYPE_MAIN_VARIANT (*type),
317 TYPE_READONLY (*type),
318 TYPE_VOLATILE (*type));
319 }
320 else
321 *type = TREE_VEC_ELT (tvec, TEMPLATE_TYPE_IDX (*type));
322 return;
323 case POINTER_TYPE:
324 case REFERENCE_TYPE:
325 grok_template_type (tvec, &TREE_TYPE (*type));
326 return;
327 case FUNCTION_TYPE:
328 {
329 tree p;
330
331 /* take care of function's return type first */
332 grok_template_type (tvec, &TREE_TYPE (*type));
333
334 /* take care of function's arguments */
335 for (p = TYPE_ARG_TYPES (*type); p; p = TREE_CHAIN (p))
336 grok_template_type (tvec, &TREE_VALUE (p));
337 return;
338 }
339 default:
340 break;
341 }
342 return;
343 }
344
345 /* Convert all template arguments to their appropriate types, and return
346 a vector containing the resulting values. If any error occurs, return
347 error_mark_node. */
348 static tree
349 coerce_template_parms (parms, arglist, in_decl)
350 tree parms, arglist;
351 tree in_decl;
352 {
353 int nparms, i, lost = 0;
354 tree vec;
355
356 if (TREE_CODE (arglist) == TREE_VEC)
357 nparms = TREE_VEC_LENGTH (arglist);
358 else
359 nparms = list_length (arglist);
360 if (nparms != TREE_VEC_LENGTH (parms))
361 {
362 error ("incorrect number of parameters (%d, should be %d)",
363 nparms, TREE_VEC_LENGTH (parms));
364 if (in_decl)
365 cp_error_at ("in template expansion for decl `%D'", in_decl);
366 return error_mark_node;
367 }
368
369 if (TREE_CODE (arglist) == TREE_VEC)
370 vec = copy_node (arglist);
371 else
372 {
373 vec = make_tree_vec (nparms);
374 for (i = 0; i < nparms; i++)
375 {
376 tree arg = arglist;
377 arglist = TREE_CHAIN (arglist);
378 if (arg == error_mark_node)
379 lost++;
380 else
381 arg = TREE_VALUE (arg);
382 TREE_VEC_ELT (vec, i) = arg;
383 }
384 }
385 for (i = 0; i < nparms; i++)
386 {
387 tree arg = TREE_VEC_ELT (vec, i);
388 tree parm = TREE_VEC_ELT (parms, i);
389 tree val = 0;
390 int is_type, requires_type;
391
392 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
393 requires_type = TREE_CODE (parm) == IDENTIFIER_NODE;
394 if (is_type != requires_type)
395 {
396 if (in_decl)
397 cp_error_at ("type/value mismatch in template parameter list for `%D'", in_decl);
398 lost++;
399 TREE_VEC_ELT (vec, i) = error_mark_node;
400 continue;
401 }
402 if (is_type)
403 val = groktypename (arg);
404 else if (TREE_CODE (arg) == STRING_CST)
405 {
406 cp_error ("string literal %E is not a valid template argument", arg);
407 error ("because it is the address of an object with static linkage");
408 val = error_mark_node;
409 }
410 else
411 {
412 grok_template_type (vec, &TREE_TYPE (parm));
413 val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
414
415 if (val == error_mark_node)
416 ;
417
418 /* 14.2: Other template-arguments must be constant-expressions,
419 addresses of objects or functions with external linkage, or of
420 static class members. */
421 else if (!TREE_CONSTANT (val))
422 {
423 cp_error ("non-const `%E' cannot be used as template argument",
424 arg);
425 val = error_mark_node;
426 }
427 else if (TREE_CODE (val) == ADDR_EXPR)
428 {
429 tree a = TREE_OPERAND (val, 0);
430 if ((TREE_CODE (a) == VAR_DECL
431 || TREE_CODE (a) == FUNCTION_DECL)
432 && !TREE_PUBLIC (a))
433 {
434 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
435 val = error_mark_node;
436 }
437 }
438 }
439
440 if (val == error_mark_node)
441 lost++;
442
443 TREE_VEC_ELT (vec, i) = val;
444 }
445 if (lost)
446 return error_mark_node;
447 return vec;
448 }
449
450 /* Given class template name and parameter list, produce a user-friendly name
451 for the instantiation. */
452 static char *
453 mangle_class_name_for_template (name, parms, arglist)
454 char *name;
455 tree parms, arglist;
456 {
457 static struct obstack scratch_obstack;
458 static char *scratch_firstobj;
459 int i, nparms;
460
461 if (!scratch_firstobj)
462 {
463 gcc_obstack_init (&scratch_obstack);
464 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
465 }
466 else
467 obstack_free (&scratch_obstack, scratch_firstobj);
468
469 #if 0
470 #define buflen sizeof(buf)
471 #define check if (bufp >= buf+buflen-1) goto too_long
472 #define ccat(c) *bufp++=(c); check
473 #define advance bufp+=strlen(bufp); check
474 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
475 #else
476 #define check
477 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
478 #define advance
479 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
480 #endif
481
482 cat (name);
483 ccat ('<');
484 nparms = TREE_VEC_LENGTH (parms);
485 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
486 for (i = 0; i < nparms; i++)
487 {
488 tree parm = TREE_VEC_ELT (parms, i), arg = TREE_VEC_ELT (arglist, i);
489
490 if (i)
491 ccat (',');
492
493 if (TREE_CODE (parm) == IDENTIFIER_NODE)
494 {
495 cat (type_as_string (arg, 0));
496 continue;
497 }
498 else
499 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
500
501 if (TREE_CODE (arg) == TREE_LIST)
502 {
503 /* New list cell was built because old chain link was in
504 use. */
505 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
506 arg = TREE_VALUE (arg);
507 }
508 /* No need to check arglist against parmlist here; we did that
509 in coerce_template_parms, called from lookup_template_class. */
510 cat (expr_as_string (arg, 0));
511 }
512 {
513 char *bufp = obstack_next_free (&scratch_obstack);
514 int offset = 0;
515 while (bufp[offset - 1] == ' ')
516 offset--;
517 obstack_blank_fast (&scratch_obstack, offset);
518
519 /* B<C<char> >, not B<C<char>> */
520 if (bufp[offset - 1] == '>')
521 ccat (' ');
522 }
523 ccat ('>');
524 ccat ('\0');
525 return (char *) obstack_base (&scratch_obstack);
526
527 #if 0
528 too_long:
529 #endif
530 fatal ("out of (preallocated) string space creating template instantiation name");
531 /* NOTREACHED */
532 return NULL;
533 }
534
535 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
536 parameters, find the desired type.
537
538 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
539 Since ARGLIST is build on the decl_obstack, we must copy it here
540 to keep it from being reclaimed when the decl storage is reclaimed.
541
542 IN_DECL, if non-NULL, is the template declaration we are trying to
543 instantiate. */
544 tree
545 lookup_template_class (d1, arglist, in_decl)
546 tree d1, arglist;
547 tree in_decl;
548 {
549 tree template, parmlist;
550 char *mangled_name;
551 tree id;
552
553 my_friendly_assert (TREE_CODE (d1) == IDENTIFIER_NODE, 272);
554 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
555 if (! template)
556 template = IDENTIFIER_CLASS_VALUE (d1);
557 /* With something like `template <class T> class X class X { ... };'
558 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
559 We don't want to do that, but we have to deal with the situation, so
560 let's give them some syntax errors to chew on instead of a crash. */
561 if (! template)
562 return error_mark_node;
563 if (TREE_CODE (template) != TEMPLATE_DECL)
564 {
565 cp_error ("non-template type `%T' used as a template", d1);
566 if (in_decl)
567 cp_error_at ("for template declaration `%D'", in_decl);
568 return error_mark_node;
569 }
570 parmlist = DECL_TEMPLATE_PARMS (template);
571
572 arglist = coerce_template_parms (parmlist, arglist, in_decl);
573 if (arglist == error_mark_node)
574 return error_mark_node;
575 if (uses_template_parms (arglist))
576 {
577 tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
578 tree d;
579 id = make_anon_name ();
580 d = build_lang_decl (TYPE_DECL, id, t);
581 TYPE_NAME (t) = d;
582 TYPE_VALUES (t) = build_tree_list (template, arglist);
583 pushdecl_top_level (d);
584 }
585 else
586 {
587 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
588 parmlist, arglist);
589 id = get_identifier (mangled_name);
590 }
591 if (!IDENTIFIER_TEMPLATE (id))
592 {
593 arglist = copy_to_permanent (arglist);
594 IDENTIFIER_TEMPLATE (id) = perm_tree_cons (template, arglist, NULL_TREE);
595 }
596 return id;
597 }
598 \f
599 void
600 push_template_decls (parmlist, arglist, class_level)
601 tree parmlist, arglist;
602 int class_level;
603 {
604 int i, nparms;
605
606 /* Don't want to push values into global context. */
607 if (!class_level)
608 {
609 pushlevel (1);
610 declare_pseudo_global_level ();
611 }
612
613 nparms = TREE_VEC_LENGTH (parmlist);
614
615 for (i = 0; i < nparms; i++)
616 {
617 int requires_type, is_type;
618 tree parm = TREE_VEC_ELT (parmlist, i);
619 tree arg = TREE_VEC_ELT (arglist, i);
620 tree decl = 0;
621
622 requires_type = TREE_CODE (parm) == IDENTIFIER_NODE;
623 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
624 if (is_type)
625 {
626 /* add typename to namespace */
627 if (!requires_type)
628 {
629 error ("template use error: type provided where value needed");
630 continue;
631 }
632 decl = arg;
633 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 't', 273);
634 decl = build_lang_decl (TYPE_DECL, parm, decl);
635 }
636 else
637 {
638 /* add const decl to namespace */
639 tree val;
640 if (requires_type)
641 {
642 error ("template use error: value provided where type needed");
643 continue;
644 }
645 val = digest_init (TREE_TYPE (parm), arg, (tree *) 0);
646 if (val != error_mark_node)
647 {
648 decl = build_decl (VAR_DECL, DECL_NAME (parm), TREE_TYPE (parm));
649 DECL_INITIAL (decl) = val;
650 TREE_READONLY (decl) = 1;
651 }
652 }
653 if (decl != 0)
654 {
655 layout_decl (decl, 0);
656 if (class_level)
657 pushdecl_class_level (decl);
658 else
659 pushdecl (decl);
660 }
661 }
662 }
663
664 void
665 pop_template_decls (parmlist, arglist, class_level)
666 tree parmlist, arglist;
667 int class_level;
668 {
669 if (!class_level)
670 poplevel (0, 0, 0);
671 }
672 \f
673 /* Should be defined in parse.h. */
674 extern int yychar;
675
676 int
677 uses_template_parms (t)
678 tree t;
679 {
680 if (!t)
681 return 0;
682 switch (TREE_CODE (t))
683 {
684 case INDIRECT_REF:
685 case COMPONENT_REF:
686 /* We assume that the object must be instantiated in order to build
687 the COMPONENT_REF, so we test only whether the type of the
688 COMPONENT_REF uses template parms. */
689 return uses_template_parms (TREE_TYPE (t));
690
691 case IDENTIFIER_NODE:
692 if (!IDENTIFIER_TEMPLATE (t))
693 return 0;
694 return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t)));
695
696 /* aggregates of tree nodes */
697 case TREE_VEC:
698 {
699 int i = TREE_VEC_LENGTH (t);
700 while (i--)
701 if (uses_template_parms (TREE_VEC_ELT (t, i)))
702 return 1;
703 return 0;
704 }
705 case TREE_LIST:
706 if (uses_template_parms (TREE_PURPOSE (t))
707 || uses_template_parms (TREE_VALUE (t)))
708 return 1;
709 return uses_template_parms (TREE_CHAIN (t));
710
711 /* constructed type nodes */
712 case POINTER_TYPE:
713 case REFERENCE_TYPE:
714 return uses_template_parms (TREE_TYPE (t));
715 case RECORD_TYPE:
716 case UNION_TYPE:
717 if (!TYPE_NAME (t))
718 return 0;
719 if (!TYPE_IDENTIFIER (t))
720 return 0;
721 return uses_template_parms (TYPE_IDENTIFIER (t));
722 case FUNCTION_TYPE:
723 if (uses_template_parms (TYPE_ARG_TYPES (t)))
724 return 1;
725 return uses_template_parms (TREE_TYPE (t));
726 case ARRAY_TYPE:
727 if (uses_template_parms (TYPE_DOMAIN (t)))
728 return 1;
729 return uses_template_parms (TREE_TYPE (t));
730 case OFFSET_TYPE:
731 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
732 return 1;
733 return uses_template_parms (TREE_TYPE (t));
734 case METHOD_TYPE:
735 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
736 return 1;
737 if (uses_template_parms (TYPE_ARG_TYPES (t)))
738 return 1;
739 return uses_template_parms (TREE_TYPE (t));
740
741 /* decl nodes */
742 case TYPE_DECL:
743 return uses_template_parms (DECL_NAME (t));
744 case FUNCTION_DECL:
745 if (uses_template_parms (TREE_TYPE (t)))
746 return 1;
747 /* fall through */
748 case VAR_DECL:
749 case PARM_DECL:
750 /* ??? What about FIELD_DECLs? */
751 /* The type of a decl can't use template parms if the name of the
752 variable doesn't, because it's impossible to resolve them. So
753 ignore the type field for now. */
754 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
755 return 1;
756 if (uses_template_parms (TREE_TYPE (t)))
757 {
758 error ("template parms used where they can't be resolved");
759 }
760 return 0;
761
762 case CALL_EXPR:
763 return uses_template_parms (TREE_TYPE (t));
764 case ADDR_EXPR:
765 return uses_template_parms (TREE_OPERAND (t, 0));
766
767 /* template parm nodes */
768 case TEMPLATE_TYPE_PARM:
769 case TEMPLATE_CONST_PARM:
770 return 1;
771
772 /* simple type nodes */
773 case INTEGER_TYPE:
774 if (uses_template_parms (TYPE_MIN_VALUE (t)))
775 return 1;
776 return uses_template_parms (TYPE_MAX_VALUE (t));
777
778 case REAL_TYPE:
779 case VOID_TYPE:
780 case ENUMERAL_TYPE:
781 return 0;
782
783 /* constants */
784 case INTEGER_CST:
785 case REAL_CST:
786 case STRING_CST:
787 return 0;
788
789 case ERROR_MARK:
790 /* Non-error_mark_node ERROR_MARKs are bad things. */
791 my_friendly_assert (t == error_mark_node, 274);
792 /* NOTREACHED */
793 return 0;
794
795 case UNINSTANTIATED_P_TYPE:
796 return 1;
797
798 default:
799 switch (TREE_CODE_CLASS (TREE_CODE (t)))
800 {
801 case '1':
802 case '2':
803 case '3':
804 case '<':
805 {
806 int i;
807 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
808 if (uses_template_parms (TREE_OPERAND (t, i)))
809 return 1;
810 return 0;
811 }
812 default:
813 break;
814 }
815 sorry ("testing %s for template parms",
816 tree_code_name [(int) TREE_CODE (t)]);
817 my_friendly_abort (82);
818 /* NOTREACHED */
819 return 0;
820 }
821 }
822
823 void
824 instantiate_member_templates (classname)
825 tree classname;
826 {
827 tree t;
828 tree id = classname;
829 tree members = DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id)));
830
831 for (t = members; t; t = TREE_CHAIN (t))
832 {
833 tree parmvec, type, classparms, tdecl, t2;
834 int nparms, xxx = 0, i;
835
836 my_friendly_assert (TREE_VALUE (t) != NULL_TREE, 275);
837 my_friendly_assert (TREE_CODE (TREE_VALUE (t)) == TEMPLATE_DECL, 276);
838 /* @@ Should verify that class parm list is a list of
839 distinct template parameters, and covers all the template
840 parameters. */
841 tdecl = TREE_VALUE (t);
842 type = DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl));
843 classparms = UPT_PARMS (type);
844 nparms = TREE_VEC_LENGTH (classparms);
845 parmvec = make_tree_vec (nparms);
846 for (i = 0; i < nparms; i++)
847 TREE_VEC_ELT (parmvec, i) = NULL_TREE;
848 switch (unify (DECL_TEMPLATE_PARMS (tdecl),
849 &TREE_VEC_ELT (parmvec, 0), nparms,
850 type, IDENTIFIER_TYPE_VALUE (classname),
851 &xxx))
852 {
853 case 0:
854 /* Success -- well, no inconsistency, at least. */
855 for (i = 0; i < nparms; i++)
856 if (TREE_VEC_ELT (parmvec, i) == NULL_TREE)
857 goto failure;
858 t2 = instantiate_template (tdecl,
859 &TREE_VEC_ELT (parmvec, 0));
860 type = IDENTIFIER_TYPE_VALUE (id);
861 my_friendly_assert (type != 0, 277);
862 if (CLASSTYPE_INTERFACE_UNKNOWN (type))
863 {
864 DECL_EXTERNAL (t2) = 0;
865 TREE_PUBLIC (t2) = 0;
866 }
867 else
868 {
869 DECL_EXTERNAL (t2) = CLASSTYPE_INTERFACE_ONLY (type);
870 TREE_PUBLIC (t2) = 1;
871 }
872 break;
873 case 1:
874 /* Failure. */
875 failure:
876 cp_error ("type unification error instantiating %T::%D",
877 classname, tdecl);
878 cp_error_at ("for template declaration `%D'", tdecl);
879
880 continue /* loop of members */;
881 default:
882 /* Eek, a bug. */
883 my_friendly_abort (83);
884 }
885 }
886 }
887
888 struct tinst_level *current_tinst_level = 0;
889 struct tinst_level *free_tinst_level = 0;
890
891 void
892 push_tinst_level (name)
893 tree name;
894 {
895 struct tinst_level *new;
896 tree global = IDENTIFIER_GLOBAL_VALUE (name);
897
898 if (free_tinst_level)
899 {
900 new = free_tinst_level;
901 free_tinst_level = new->next;
902 }
903 else
904 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
905
906 new->classname = name;
907 if (global)
908 {
909 new->line = DECL_SOURCE_LINE (global);
910 new->file = DECL_SOURCE_FILE (global);
911 }
912 else
913 {
914 new->line = lineno;
915 new->file = input_filename;
916 }
917 new->next = current_tinst_level;
918 current_tinst_level = new;
919 }
920
921 void
922 pop_tinst_level ()
923 {
924 struct tinst_level *old = current_tinst_level;
925
926 current_tinst_level = old->next;
927 old->next = free_tinst_level;
928 free_tinst_level = old;
929 }
930
931 struct tinst_level *
932 tinst_for_decl ()
933 {
934 struct tinst_level *p = current_tinst_level;
935
936 if (p)
937 for (; p->next ; p = p->next )
938 ;
939 return p;
940 }
941
942 tree
943 instantiate_class_template (classname, setup_parse)
944 tree classname;
945 int setup_parse;
946 {
947 struct template_info *template_info;
948 tree template, t1;
949
950 if (classname == error_mark_node)
951 return error_mark_node;
952
953 my_friendly_assert (TREE_CODE (classname) == IDENTIFIER_NODE, 278);
954 template = IDENTIFIER_TEMPLATE (classname);
955
956 if (IDENTIFIER_HAS_TYPE_VALUE (classname))
957 {
958 tree type = IDENTIFIER_TYPE_VALUE (classname);
959 if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
960 return type;
961 if (TYPE_BEING_DEFINED (type)
962 || TYPE_SIZE (type)
963 || CLASSTYPE_USE_TEMPLATE (type) != 0)
964 return type;
965 }
966
967 /* If IDENTIFIER_LOCAL_VALUE is already set on this template classname
968 (it's something like `foo<int>'), that means we're already working on
969 the instantiation for it. Normally, a classname comes in with nothing
970 but its IDENTIFIER_TEMPLATE slot set. If we were to try to instantiate
971 this again, we'd get a redeclaration error. Since we're already working
972 on it, we'll pass back this classname's TYPE_DECL (it's the value of
973 the classname's IDENTIFIER_LOCAL_VALUE). Only do this if we're setting
974 things up for the parser, though---if we're just trying to instantiate
975 it (e.g., via tsubst) we can trip up cuz it may not have an
976 IDENTIFIER_TYPE_VALUE when it will need one. */
977 if (setup_parse && IDENTIFIER_LOCAL_VALUE (classname))
978 return IDENTIFIER_LOCAL_VALUE (classname);
979
980 if (uses_template_parms (classname))
981 {
982 if (!TREE_TYPE (classname))
983 {
984 tree t = make_lang_type (RECORD_TYPE);
985 tree d = build_lang_decl (TYPE_DECL, classname, t);
986 DECL_NAME (d) = classname;
987 TYPE_NAME (t) = d;
988 pushdecl (d);
989 }
990 return NULL_TREE;
991 }
992
993 t1 = TREE_PURPOSE (template);
994 my_friendly_assert (TREE_CODE (t1) == TEMPLATE_DECL, 279);
995
996 /* If a template is declared but not defined, accept it; don't crash.
997 Later uses requiring the definition will be flagged as errors by
998 other code. Thanks to niklas@appli.se for this bug fix. */
999 if (DECL_TEMPLATE_INFO (t1)->text == 0)
1000 setup_parse = 0;
1001
1002 push_to_top_level ();
1003 template_info = DECL_TEMPLATE_INFO (t1);
1004 if (setup_parse)
1005 {
1006 push_tinst_level (classname);
1007 push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
1008 TREE_VALUE (template), 0);
1009 set_current_level_tags_transparency (1);
1010 feed_input (template_info->text, template_info->length, (struct obstack *)0);
1011 lineno = template_info->lineno;
1012 input_filename = template_info->filename;
1013 /* Get interface/implementation back in sync. */
1014 extract_interface_info ();
1015 overload_template_name (classname, 0);
1016 /* Kludge so that we don't get screwed by our own base classes. */
1017 TYPE_BEING_DEFINED (TREE_TYPE (classname)) = 1;
1018 yychar = PRE_PARSED_CLASS_DECL;
1019 yylval.ttype = classname;
1020 processing_template_defn++;
1021 if (!flag_external_templates)
1022 interface_unknown++;
1023 }
1024 else
1025 {
1026 tree t, decl, id, tmpl;
1027
1028 id = classname;
1029 tmpl = TREE_PURPOSE (IDENTIFIER_TEMPLATE (id));
1030 t = xref_tag (DECL_TEMPLATE_INFO (tmpl)->aggr, id, NULL_TREE, 0);
1031 my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
1032 || TREE_CODE (t) == UNION_TYPE, 280);
1033
1034 /* Now, put a copy of the decl in global scope, to avoid
1035 * recursive expansion. */
1036 decl = IDENTIFIER_LOCAL_VALUE (id);
1037 if (!decl)
1038 decl = IDENTIFIER_CLASS_VALUE (id);
1039 if (decl)
1040 {
1041 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 281);
1042 /* We'd better make sure we're on the permanent obstack or else
1043 * we'll get a "friendly" abort 124 in pushdecl. Perhaps a
1044 * copy_to_permanent would be sufficient here, but then a
1045 * sharing problem might occur. I don't know -- niklas@appli.se */
1046 push_obstacks (&permanent_obstack, &permanent_obstack);
1047 pushdecl_top_level (copy_node (decl));
1048 pop_obstacks ();
1049 }
1050 pop_from_top_level ();
1051 }
1052
1053 return NULL_TREE;
1054 }
1055
1056 static int
1057 list_eq (t1, t2)
1058 tree t1, t2;
1059 {
1060 if (t1 == NULL_TREE)
1061 return t2 == NULL_TREE;
1062 if (t2 == NULL_TREE)
1063 return 0;
1064 /* Don't care if one declares its arg const and the other doesn't -- the
1065 main variant of the arg type is all that matters. */
1066 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1067 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1068 return 0;
1069 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1070 }
1071
1072 static tree
1073 lookup_nested_type_by_name (ctype, name)
1074 tree ctype, name;
1075 {
1076 tree t;
1077
1078 t = TREE_VALUE(CLASSTYPE_TAGS(ctype));
1079 while (t)
1080 {
1081 if (strcmp(IDENTIFIER_POINTER(name), IDENTIFIER_POINTER(TYPE_IDENTIFIER(t)))
1082 == 0)
1083 return t;
1084 else
1085 t = TREE_CHAIN(t);
1086 }
1087 return NULL_TREE;
1088 }
1089
1090 static tree
1091 search_nested_type_in_tmpl (tmpl, type)
1092 tree tmpl, type;
1093 {
1094 tree t;
1095
1096 if (tmpl == NULL || TYPE_CONTEXT(type) == NULL)
1097 return tmpl;
1098 t = search_nested_type_in_tmpl (tmpl, TYPE_CONTEXT(type));
1099 if (t == NULL) return t;
1100 t = lookup_nested_type_by_name(t, DECL_NAME(TYPE_NAME(type)));
1101 return t;
1102 }
1103
1104 static tree
1105 tsubst (t, args, nargs, in_decl)
1106 tree t, *args;
1107 int nargs;
1108 tree in_decl;
1109 {
1110 tree type;
1111
1112 if (t == NULL_TREE || t == error_mark_node)
1113 return t;
1114
1115 type = TREE_TYPE (t);
1116 if (type
1117 /* Minor optimization.
1118 ?? Are these really the most frequent cases? Is the savings
1119 significant? */
1120 && type != integer_type_node
1121 && type != void_type_node
1122 && type != char_type_node)
1123 type = build_type_variant (tsubst (type, args, nargs, in_decl),
1124 TYPE_READONLY (type),
1125 TYPE_VOLATILE (type));
1126 switch (TREE_CODE (t))
1127 {
1128 case RECORD_TYPE:
1129 if (TYPE_PTRMEMFUNC_P (t))
1130 return build_ptrmemfunc_type
1131 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1132
1133 /* else fall through */
1134
1135 case ERROR_MARK:
1136 case IDENTIFIER_NODE:
1137 case OP_IDENTIFIER:
1138 case VOID_TYPE:
1139 case REAL_TYPE:
1140 case ENUMERAL_TYPE:
1141 case INTEGER_CST:
1142 case REAL_CST:
1143 case STRING_CST:
1144 case UNION_TYPE:
1145 return t;
1146
1147 case INTEGER_TYPE:
1148 if (t == integer_type_node)
1149 return t;
1150
1151 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1152 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1153 return t;
1154 return build_index_2_type
1155 (tsubst (TYPE_MIN_VALUE (t), args, nargs, in_decl),
1156 tsubst (TYPE_MAX_VALUE (t), args, nargs, in_decl));
1157
1158 case TEMPLATE_TYPE_PARM:
1159 return build_type_variant (args[TEMPLATE_TYPE_IDX (t)],
1160 TYPE_READONLY (t),
1161 TYPE_VOLATILE (t));
1162
1163 case TEMPLATE_CONST_PARM:
1164 return args[TEMPLATE_CONST_IDX (t)];
1165
1166 case FUNCTION_DECL:
1167 {
1168 tree r;
1169 tree fnargs, result;
1170
1171 if (type == TREE_TYPE (t)
1172 && (DECL_CONTEXT (t) == NULL_TREE
1173 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) != 't'))
1174 return t;
1175 fnargs = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1176 result = tsubst (DECL_RESULT (t), args, nargs, t);
1177 if (DECL_CONTEXT (t) != NULL_TREE
1178 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1179 {
1180 /* Look it up in that class, and return the decl node there,
1181 instead of creating a new one. */
1182 tree ctx, methods, name, method;
1183 int n_methods;
1184 int i, found = 0;
1185
1186 name = DECL_NAME (t);
1187 ctx = tsubst (DECL_CONTEXT (t), args, nargs, t);
1188 methods = CLASSTYPE_METHOD_VEC (ctx);
1189 if (methods == NULL_TREE)
1190 /* No methods at all -- no way this one can match. */
1191 goto no_match;
1192 n_methods = TREE_VEC_LENGTH (methods);
1193
1194 r = NULL_TREE;
1195
1196 if (!strncmp (OPERATOR_TYPENAME_FORMAT,
1197 IDENTIFIER_POINTER (name),
1198 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1199 {
1200 /* Type-conversion operator. Reconstruct the name, in
1201 case it's the name of one of the template's parameters. */
1202 name = build_typename_overload (TREE_TYPE (type));
1203 }
1204
1205 if (DECL_CONTEXT (t) != NULL_TREE
1206 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't'
1207 && constructor_name (DECL_CONTEXT (t)) == DECL_NAME (t))
1208 name = constructor_name (ctx);
1209 #if 0
1210 fprintf (stderr, "\nfor function %s in class %s:\n",
1211 IDENTIFIER_POINTER (name),
1212 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
1213 #endif
1214 for (i = 0; i < n_methods; i++)
1215 {
1216 int pass;
1217
1218 method = TREE_VEC_ELT (methods, i);
1219 if (method == NULL_TREE || DECL_NAME (method) != name)
1220 continue;
1221
1222 pass = 0;
1223 maybe_error:
1224 for (; method; method = DECL_CHAIN (method))
1225 {
1226 my_friendly_assert (TREE_CODE (method) == FUNCTION_DECL,
1227 282);
1228 if (! comptypes (type, TREE_TYPE (method), 1))
1229 {
1230 tree mtype = TREE_TYPE (method);
1231 tree t1, t2;
1232
1233 /* Keep looking for a method that matches
1234 perfectly. This takes care of the problem
1235 where destructors (which have implicit int args)
1236 look like constructors which have an int arg. */
1237 if (pass == 0)
1238 continue;
1239
1240 t1 = TYPE_ARG_TYPES (mtype);
1241 t2 = TYPE_ARG_TYPES (type);
1242 if (TREE_CODE (mtype) == FUNCTION_TYPE)
1243 t2 = TREE_CHAIN (t2);
1244
1245 if (list_eq (t1, t2))
1246 {
1247 if (TREE_CODE (mtype) == FUNCTION_TYPE)
1248 {
1249 tree newtype;
1250 newtype = build_function_type (TREE_TYPE (type),
1251 TYPE_ARG_TYPES (type));
1252 newtype = build_type_variant (newtype,
1253 TYPE_READONLY (type),
1254 TYPE_VOLATILE (type));
1255 type = newtype;
1256 if (TREE_TYPE (type) != TREE_TYPE (mtype))
1257 goto maybe_bad_return_type;
1258 }
1259 else if (TYPE_METHOD_BASETYPE (mtype)
1260 == TYPE_METHOD_BASETYPE (type))
1261 {
1262 /* Types didn't match, but arg types and
1263 `this' do match, so the return type is
1264 all that should be messing it up. */
1265 maybe_bad_return_type:
1266 if (TREE_TYPE (type) != TREE_TYPE (mtype))
1267 error ("inconsistent return types for method `%s' in class `%s'",
1268 IDENTIFIER_POINTER (name),
1269 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx)));
1270 }
1271 r = method;
1272 break;
1273 }
1274 found = 1;
1275 continue;
1276 }
1277 #if 0
1278 fprintf (stderr, "\tfound %s\n\n",
1279 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method)));
1280 #endif
1281
1282 if (DECL_ARGUMENTS (method)
1283 && ! TREE_PERMANENT (DECL_ARGUMENTS (method)))
1284 /* @@ Is this early enough? Might we want to do
1285 this instead while processing the expansion? */
1286 DECL_ARGUMENTS (method)
1287 = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1288 r = method;
1289 break;
1290 }
1291 if (r == NULL_TREE && pass == 0)
1292 {
1293 pass = 1;
1294 method = TREE_VEC_ELT (methods, i);
1295 goto maybe_error;
1296 }
1297 }
1298 if (r == NULL_TREE)
1299 {
1300 no_match:
1301 cp_error
1302 (found
1303 ? "template for method `%D' doesn't match any in class `%T'"
1304 : "method `%D' not found in class `%T'", name, ctx);
1305 if (in_decl)
1306 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl);
1307 return error_mark_node;
1308 }
1309 }
1310 else
1311 {
1312 r = DECL_NAME (t);
1313 {
1314 tree decls;
1315 int got_it = 0;
1316
1317 decls = lookup_name (r, 0);
1318 if (decls == NULL_TREE)
1319 /* no match */;
1320 else if (TREE_CODE (decls) == TREE_LIST)
1321 for (decls = TREE_VALUE (decls); decls ;
1322 decls = DECL_CHAIN (decls))
1323 {
1324 if (TREE_CODE (decls) == FUNCTION_DECL
1325 && TREE_TYPE (decls) == type)
1326 {
1327 got_it = 1;
1328 r = decls;
1329 break;
1330 }
1331 }
1332 else
1333 {
1334 tree val = decls;
1335 decls = NULL_TREE;
1336 if (TREE_CODE (val) == FUNCTION_DECL
1337 && TREE_TYPE (val) == type)
1338 {
1339 got_it = 1;
1340 r = val;
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
1575 tree
1576 instantiate_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 (&DECL_RESULT (tmpl), NULL, NULL);
1628 /* Chop off the this pointer that grokclassfn so kindly added
1629 for us (it didn't know yet if the fn was static or not). */
1630 DECL_ARGUMENTS (olddecl) = TREE_CHAIN (DECL_ARGUMENTS (olddecl));
1631 DECL_ARGUMENTS (fndecl) = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1632 }
1633
1634 t = DECL_TEMPLATE_INFO (tmpl);
1635 if (t->text)
1636 {
1637 p = (struct pending_inline *) permalloc (sizeof (struct pending_inline));
1638 p->parm_vec = t->parm_vec;
1639 p->bindings = targs;
1640 p->can_free = 0;
1641 p->deja_vu = 0;
1642 p->buf = t->text;
1643 p->len = t->length;
1644 p->fndecl = fndecl;
1645 {
1646 int l = lineno;
1647 char * f = input_filename;
1648
1649 lineno = p->lineno = t->lineno;
1650 input_filename = p->filename = t->filename;
1651
1652 extract_interface_info ();
1653
1654 if (interface_unknown && flag_external_templates && ! DECL_IN_SYSTEM_HEADER (tmpl))
1655 warn_if_unknown_interface ();
1656 if (interface_unknown || !flag_external_templates)
1657 p->interface = 1; /* unknown */
1658 else
1659 p->interface = interface_only ? 0 : 2;
1660
1661 lineno = l;
1662 input_filename = f;
1663
1664 extract_interface_info ();
1665 }
1666 }
1667 else
1668 p = (struct pending_inline *)0;
1669
1670 DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
1671 tree_cons (targs, fndecl, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1672
1673 function_maybepermanent_obstack = old_fmp_obstack;
1674 pop_obstacks ();
1675
1676 if (fndecl == error_mark_node || p == (struct pending_inline *)0)
1677 {
1678 /* do nothing */
1679 }
1680 else if (DECL_INLINE (fndecl))
1681 {
1682 DECL_PENDING_INLINE_INFO (fndecl) = p;
1683 p->next = pending_inlines;
1684 pending_inlines = p;
1685 }
1686 else
1687 {
1688 p->next = pending_template_expansions;
1689 pending_template_expansions = p;
1690 }
1691 return fndecl;
1692 }
1693
1694 /* classlevel should now never be true. jason 4/12/94 */
1695 void
1696 undo_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
1716 /* classlevel should now never be true. jason 4/12/94 */
1717 void
1718 overload_template_name (id, classlevel)
1719 tree id;
1720 int classlevel;
1721 {
1722 tree template, t, decl;
1723 struct template_info *tinfo;
1724
1725 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 284);
1726 template = IDENTIFIER_TEMPLATE (id);
1727 if (!template)
1728 return;
1729
1730 template = TREE_PURPOSE (template);
1731 tinfo = DECL_TEMPLATE_INFO (template);
1732 template = DECL_NAME (template);
1733 my_friendly_assert (template != NULL_TREE, 285);
1734
1735 #if 1 /* XXX */
1736 /* This was a botch... names of templates do not get their own private
1737 scopes. Rather, they should go into the binding level already created
1738 by push_template_decls. Except that there isn't one of those for
1739 specializations. */
1740 if (!classlevel)
1741 {
1742 pushlevel (1);
1743 declare_pseudo_global_level ();
1744 }
1745 #endif
1746
1747 t = xref_tag (tinfo->aggr, id, NULL_TREE, 0);
1748 my_friendly_assert (TREE_CODE (t) == RECORD_TYPE
1749 || TREE_CODE (t) == UNION_TYPE
1750 || TREE_CODE (t) == UNINSTANTIATED_P_TYPE, 286);
1751
1752 decl = build_decl (TYPE_DECL, template, t);
1753
1754 #if 0 /* fix this later */
1755 /* We don't want to call here if the work has already been done. */
1756 t = (classlevel
1757 ? IDENTIFIER_CLASS_VALUE (template)
1758 : IDENTIFIER_LOCAL_VALUE (template));
1759 if (t
1760 && TREE_CODE (t) == TYPE_DECL
1761 && TREE_TYPE (t) == t)
1762 my_friendly_abort (85);
1763 #endif
1764
1765 if (classlevel)
1766 pushdecl_class_level (decl);
1767 else
1768 pushdecl (decl);
1769
1770 #if 0 /* This seems bogus to me; if it isn't, explain why. (jason) */
1771 /* Fake this for now, just to make dwarfout.c happy. It will have to
1772 be done in a proper way later on. */
1773 DECL_CONTEXT (decl) = t;
1774 #endif
1775 }
1776
1777 /* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
1778 void
1779 end_template_instantiation (name)
1780 tree name;
1781 {
1782 extern struct pending_input *to_be_restored;
1783 tree t, decl;
1784
1785 processing_template_defn--;
1786 if (!flag_external_templates)
1787 interface_unknown--;
1788
1789 /* Restore the old parser input state. */
1790 if (yychar == YYEMPTY)
1791 yychar = yylex ();
1792 if (yychar != END_OF_SAVED_INPUT)
1793 error ("parse error at end of class template");
1794 else
1795 {
1796 restore_pending_input (to_be_restored);
1797 to_be_restored = 0;
1798 }
1799
1800 /* Our declarations didn't get stored in the global slot, since
1801 there was a (supposedly tags-transparent) scope in between. */
1802 t = IDENTIFIER_TYPE_VALUE (name);
1803 my_friendly_assert (t != NULL_TREE
1804 && TREE_CODE_CLASS (TREE_CODE (t)) == 't',
1805 287);
1806 CLASSTYPE_USE_TEMPLATE (t) = 2;
1807 /* Make methods of template classes static, unless
1808 -fexternal-templates is given. */
1809 if (!flag_external_templates)
1810 SET_CLASSTYPE_INTERFACE_UNKNOWN (t);
1811 decl = IDENTIFIER_GLOBAL_VALUE (name);
1812 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 288);
1813
1814 undo_template_name_overload (name, 0);
1815 t = IDENTIFIER_TEMPLATE (name);
1816 pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t)), TREE_VALUE (t),
1817 0);
1818 /* This will fix up the type-value field. */
1819 pushdecl (decl);
1820 pop_from_top_level ();
1821
1822 #ifdef DWARF_DEBUGGING_INFO
1823 if (write_symbols == DWARF_DEBUG && TREE_CODE (decl) == TYPE_DECL)
1824 {
1825 /* We just completed the definition of a new file-scope type,
1826 so we can go ahead and output debug-info for it now. */
1827 TYPE_STUB_DECL (TREE_TYPE (decl)) = decl;
1828 rest_of_type_compilation (TREE_TYPE (decl), 1);
1829 }
1830 #endif /* DWARF_DEBUGGING_INFO */
1831
1832 /* Restore interface/implementation settings. */
1833 extract_interface_info ();
1834 }
1835 \f
1836 /* Store away the text of an template. */
1837
1838 void
1839 reinit_parse_for_template (yychar, d1, d2)
1840 int yychar;
1841 tree d1, d2;
1842 {
1843 struct template_info *template_info;
1844 extern struct obstack inline_text_obstack; /* see comment in lex.c */
1845
1846 if (d2 == NULL_TREE || d2 == error_mark_node)
1847 {
1848 lose:
1849 /* @@ Should use temp obstack, and discard results. */
1850 reinit_parse_for_block (yychar, &inline_text_obstack, 1);
1851 return;
1852 }
1853
1854 if (TREE_CODE (d2) == IDENTIFIER_NODE)
1855 d2 = IDENTIFIER_GLOBAL_VALUE (d2);
1856 if (!d2)
1857 goto lose;
1858 template_info = DECL_TEMPLATE_INFO (d2);
1859 if (!template_info)
1860 {
1861 template_info = (struct template_info *) permalloc (sizeof (struct template_info));
1862 bzero (template_info, sizeof (struct template_info));
1863 DECL_TEMPLATE_INFO (d2) = template_info;
1864 }
1865 template_info->filename = input_filename;
1866 template_info->lineno = lineno;
1867 reinit_parse_for_block (yychar, &inline_text_obstack, 1);
1868 template_info->text = obstack_base (&inline_text_obstack);
1869 template_info->length = obstack_object_size (&inline_text_obstack);
1870 obstack_finish (&inline_text_obstack);
1871 template_info->parm_vec = d1;
1872 }
1873
1874 /* Type unification.
1875
1876 We have a function template signature with one or more references to
1877 template parameters, and a parameter list we wish to fit to this
1878 template. If possible, produce a list of parameters for the template
1879 which will cause it to fit the supplied parameter list.
1880
1881 Return zero for success, 2 for an incomplete match that doesn't resolve
1882 all the types, and 1 for complete failure. An error message will be
1883 printed only for an incomplete match.
1884
1885 TPARMS[NTPARMS] is an array of template parameter types;
1886 TARGS[NTPARMS] is the array of template parameter values. PARMS is
1887 the function template's signature (using TEMPLATE_PARM_IDX nodes),
1888 and ARGS is the argument list we're trying to match against it.
1889
1890 If SUBR is 1, we're being called recursively (to unify the arguments of
1891 a function or method parameter of a function template), so don't zero
1892 out targs and don't fail on an incomplete match. */
1893
1894 int
1895 type_unification (tparms, targs, parms, args, nsubsts, subr)
1896 tree tparms, *targs, parms, args;
1897 int *nsubsts, subr;
1898 {
1899 tree parm, arg;
1900 int i;
1901 int ntparms = TREE_VEC_LENGTH (tparms);
1902
1903 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
1904 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
1905 /* ARGS could be NULL (via a call from parse.y to
1906 build_x_function_call). */
1907 if (args)
1908 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
1909 my_friendly_assert (ntparms > 0, 292);
1910
1911 if (!subr)
1912 bzero (targs, sizeof (tree) * ntparms);
1913
1914 while (parms
1915 && parms != void_list_node
1916 && args
1917 && args != void_list_node)
1918 {
1919 parm = TREE_VALUE (parms);
1920 parms = TREE_CHAIN (parms);
1921 arg = TREE_VALUE (args);
1922 args = TREE_CHAIN (args);
1923
1924 if (arg == error_mark_node)
1925 return 1;
1926 if (arg == unknown_type_node)
1927 return 1;
1928 #if 0
1929 if (TREE_CODE (arg) == VAR_DECL)
1930 arg = TREE_TYPE (arg);
1931 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
1932 arg = TREE_TYPE (arg);
1933 #else
1934 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
1935 {
1936 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
1937 arg = TREE_TYPE (arg);
1938 }
1939 #endif
1940 if (TREE_CODE (arg) == FUNCTION_TYPE
1941 || TREE_CODE (arg) == METHOD_TYPE)
1942 arg = build_pointer_type (arg);
1943
1944 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts))
1945 {
1946 case 0:
1947 break;
1948 case 1:
1949 return 1;
1950 }
1951 }
1952 /* Fail if we've reached the end of the parm list, and more args
1953 are present, and the parm list isn't variadic. */
1954 if (args && args != void_list_node && parms == void_list_node)
1955 return 1;
1956 /* Fail if parms are left and they don't have default values. */
1957 if (parms
1958 && parms != void_list_node
1959 && TREE_PURPOSE (parms) == NULL_TREE)
1960 return 1;
1961 if (!subr)
1962 for (i = 0; i < ntparms; i++)
1963 if (!targs[i])
1964 {
1965 error ("incomplete type unification");
1966 return 2;
1967 }
1968 return 0;
1969 }
1970
1971 /* Tail recursion is your friend. */
1972 static int
1973 unify (tparms, targs, ntparms, parm, arg, nsubsts)
1974 tree tparms, *targs, parm, arg;
1975 int *nsubsts, ntparms;
1976 {
1977 int idx;
1978
1979 /* I don't think this will do the right thing with respect to types.
1980 But the only case I've seen it in so far has been array bounds, where
1981 signedness is the only information lost, and I think that will be
1982 okay. */
1983 while (TREE_CODE (parm) == NOP_EXPR)
1984 parm = TREE_OPERAND (parm, 0);
1985
1986 if (arg == error_mark_node)
1987 return 1;
1988 if (arg == unknown_type_node)
1989 return 1;
1990 if (arg == parm)
1991 return 0;
1992
1993 if (TREE_CODE (arg) == REFERENCE_TYPE)
1994 arg = TREE_TYPE (arg);
1995
1996 switch (TREE_CODE (parm))
1997 {
1998 case TEMPLATE_TYPE_PARM:
1999 (*nsubsts)++;
2000 if (TEMPLATE_TYPE_TPARMLIST (parm) != tparms)
2001 {
2002 error ("mixed template headers?!");
2003 my_friendly_abort (86);
2004 return 1;
2005 }
2006 idx = TEMPLATE_TYPE_IDX (parm);
2007 /* Simple cases: Value already set, does match or doesn't. */
2008 if (targs[idx] == arg)
2009 return 0;
2010 else if (targs[idx])
2011 return 1;
2012 /* Check for mixed types and values. */
2013 if (TREE_CODE (TREE_VEC_ELT (tparms, idx)) != IDENTIFIER_NODE)
2014 return 1;
2015 targs[idx] = arg;
2016 return 0;
2017 case TEMPLATE_CONST_PARM:
2018 (*nsubsts)++;
2019 idx = TEMPLATE_CONST_IDX (parm);
2020 if (targs[idx] == arg)
2021 return 0;
2022 else if (targs[idx])
2023 {
2024 tree t = targs[idx];
2025 if (TREE_CODE (t) == TREE_CODE (arg))
2026 switch (TREE_CODE (arg))
2027 {
2028 case INTEGER_CST:
2029 if (tree_int_cst_equal (t, arg))
2030 return 0;
2031 break;
2032 case REAL_CST:
2033 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
2034 return 0;
2035 break;
2036 /* STRING_CST values are not valid template const parms. */
2037 default:
2038 ;
2039 }
2040 my_friendly_abort (87);
2041 return 1;
2042 }
2043 /* else if (typeof arg != tparms[idx])
2044 return 1;*/
2045
2046 targs[idx] = copy_to_permanent (arg);
2047 return 0;
2048
2049 case POINTER_TYPE:
2050 if (TREE_CODE (arg) != POINTER_TYPE)
2051 return 1;
2052 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2053 nsubsts);
2054
2055 case REFERENCE_TYPE:
2056 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, nsubsts);
2057
2058 case ARRAY_TYPE:
2059 if (TREE_CODE (arg) != ARRAY_TYPE)
2060 return 1;
2061 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2062 nsubsts) != 0)
2063 return 1;
2064 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2065 nsubsts);
2066
2067 case REAL_TYPE:
2068 case INTEGER_TYPE:
2069 if (TREE_CODE (parm) == INTEGER_TYPE && TREE_CODE (arg) == INTEGER_TYPE)
2070 {
2071 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2072 && unify (tparms, targs, ntparms,
2073 TYPE_MIN_VALUE (parm), TYPE_MIN_VALUE (arg), nsubsts))
2074 return 1;
2075 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2076 && unify (tparms, targs, ntparms,
2077 TYPE_MAX_VALUE (parm), TYPE_MAX_VALUE (arg), nsubsts))
2078 return 1;
2079 }
2080 /* As far as unification is concerned, this wins. Later checks
2081 will invalidate it if necessary. */
2082 return 0;
2083
2084 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2085 case INTEGER_CST:
2086 if (TREE_CODE (arg) != INTEGER_CST)
2087 return 1;
2088 return !tree_int_cst_equal (parm, arg);
2089
2090 case MINUS_EXPR:
2091 {
2092 tree t1, t2;
2093 t1 = TREE_OPERAND (parm, 0);
2094 t2 = TREE_OPERAND (parm, 1);
2095 if (TREE_CODE (t1) != TEMPLATE_CONST_PARM)
2096 return 1;
2097 return unify (tparms, targs, ntparms, t1,
2098 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2099 nsubsts);
2100 }
2101
2102 case TREE_VEC:
2103 {
2104 int i;
2105 if (TREE_CODE (arg) != TREE_VEC)
2106 return 1;
2107 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2108 return 1;
2109 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2110 if (unify (tparms, targs, ntparms,
2111 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2112 nsubsts))
2113 return 1;
2114 return 0;
2115 }
2116
2117 case UNINSTANTIATED_P_TYPE:
2118 {
2119 tree a;
2120 /* Unification of something that is not a template fails. (mrs) */
2121 if (TYPE_NAME (arg) == 0)
2122 return 1;
2123 a = IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg));
2124 /* Unification of something that is not a template fails. (mrs) */
2125 if (a == 0)
2126 return 1;
2127 if (UPT_TEMPLATE (parm) != TREE_PURPOSE (a))
2128 /* different templates */
2129 return 1;
2130 return unify (tparms, targs, ntparms, UPT_PARMS (parm), TREE_VALUE (a),
2131 nsubsts);
2132 }
2133
2134 case RECORD_TYPE:
2135 if (TYPE_PTRMEMFUNC_P (parm))
2136 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2137 arg, nsubsts);
2138
2139 /* Unification of something that is not a template fails. (mrs) */
2140 return 1;
2141
2142 case METHOD_TYPE:
2143 if (TREE_CODE (arg) != METHOD_TYPE)
2144 return 1;
2145 goto check_args;
2146
2147 case FUNCTION_TYPE:
2148 if (TREE_CODE (arg) != FUNCTION_TYPE)
2149 return 1;
2150 check_args:
2151 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2152 TYPE_ARG_TYPES (arg), nsubsts, 1);
2153
2154 default:
2155 sorry ("use of `%s' in template type unification",
2156 tree_code_name [(int) TREE_CODE (parm)]);
2157 return 1;
2158 }
2159 }
2160
2161 \f
2162 #undef DEBUG
2163
2164 int
2165 do_pending_expansions ()
2166 {
2167 struct pending_inline *i, *new_list = 0;
2168
2169 if (!pending_template_expansions)
2170 return 0;
2171
2172 #ifdef DEBUG
2173 fprintf (stderr, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
2174 #endif
2175
2176 i = pending_template_expansions;
2177 while (i)
2178 {
2179 tree context;
2180
2181 struct pending_inline *next = i->next;
2182 tree t = i->fndecl;
2183
2184 int decision = 0;
2185 #define DECIDE(N) do {decision=(N); goto decided;} while(0)
2186
2187 my_friendly_assert (TREE_CODE (t) == FUNCTION_DECL
2188 || TREE_CODE (t) == VAR_DECL, 294);
2189 if (TREE_ASM_WRITTEN (t))
2190 DECIDE (0);
2191
2192 if (DECL_EXPLICITLY_INSTANTIATED (t))
2193 DECIDE (1);
2194
2195 /* If it's a method, let the class type decide it.
2196 @@ What if the method template is in a separate file?
2197 Maybe both file contexts should be taken into account?
2198 Maybe only do this if i->interface == 1 (unknown)? */
2199 context = DECL_CONTEXT (t);
2200 if (context != NULL_TREE
2201 && TREE_CODE_CLASS (TREE_CODE (context)) == 't')
2202 {
2203 /* I'm interested in the context of this version of the function,
2204 not the original virtual declaration. */
2205 context = DECL_CLASS_CONTEXT (t);
2206
2207 /* If `unknown', we might want a static copy.
2208 If `implementation', we want a global one.
2209 If `interface', ext ref. */
2210 if (CLASSTYPE_INTERFACE_KNOWN (context))
2211 DECIDE (!CLASSTYPE_INTERFACE_ONLY (context));
2212 #if 0 /* This doesn't get us stuff needed only by the file initializer. */
2213 DECIDE (TREE_USED (t));
2214 #else /* This compiles too much stuff, but that's probably better in
2215 most cases than never compiling the stuff we need. */
2216 DECIDE (1);
2217 #endif
2218 }
2219
2220 if (i->interface == 1)
2221 DECIDE (TREE_USED (t));
2222 else
2223 DECIDE (i->interface);
2224
2225 decided:
2226 #ifdef DEBUG
2227 print_node_brief (stderr, decision ? "yes: " : "no: ", t, 0);
2228 fprintf (stderr, "\t%s\n",
2229 (DECL_ASSEMBLER_NAME (t)
2230 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t))
2231 : ""));
2232 #endif
2233 if (decision)
2234 {
2235 i->next = pending_inlines;
2236 pending_inlines = i;
2237 }
2238 else
2239 {
2240 i->next = new_list;
2241 new_list = i;
2242 }
2243 i = next;
2244 }
2245 pending_template_expansions = new_list;
2246 if (!pending_inlines)
2247 return 0;
2248 do_pending_inlines ();
2249 return 1;
2250 }
2251
2252 \f
2253 struct pending_template {
2254 struct pending_template *next;
2255 tree id;
2256 };
2257
2258 static struct pending_template* pending_templates;
2259
2260 void
2261 do_pending_templates ()
2262 {
2263 struct pending_template* t;
2264
2265 for ( t = pending_templates; t; t = t->next)
2266 {
2267 instantiate_class_template (t->id, 1);
2268 }
2269
2270 for ( t = pending_templates; t; t = pending_templates)
2271 {
2272 pending_templates = t->next;
2273 free(t);
2274 }
2275 }
2276
2277 static void
2278 add_pending_template (pt)
2279 tree pt;
2280 {
2281 struct pending_template *p;
2282
2283 p = (struct pending_template *) malloc (sizeof (struct pending_template));
2284 p->next = pending_templates;
2285 pending_templates = p;
2286 p->id = pt;
2287 }
2288
2289 /* called from the parser. */
2290 void
2291 do_function_instantiation (declspecs, declarator)
2292 tree declspecs, declarator;
2293 {
2294 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, 0);
2295 tree name = DECL_NAME (decl);
2296 tree fn = IDENTIFIER_GLOBAL_VALUE (name);
2297 tree result = NULL_TREE;
2298 if (fn)
2299 {
2300 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
2301 if (TREE_CODE (fn) == TEMPLATE_DECL)
2302 {
2303 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
2304 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
2305 int i, dummy;
2306 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
2307 TYPE_ARG_TYPES (TREE_TYPE (fn)),
2308 TYPE_ARG_TYPES (TREE_TYPE (decl)),
2309 &dummy, 0);
2310 if (i == 0)
2311 {
2312 if (result)
2313 cp_error ("ambiguous template instantiation for `%D' requested", decl);
2314 else
2315 result = instantiate_template (fn, targs);
2316 }
2317 }
2318 }
2319 if (! result)
2320 cp_error ("no matching template for `%D' found", decl);
2321
2322 DECL_EXPLICITLY_INSTANTIATED (result) = 1;
2323 }
2324
2325 void
2326 do_type_instantiation (name)
2327 tree name;
2328 {
2329 tree t = TREE_TYPE (name);
2330
2331 CLASSTYPE_EXPLICITLY_INSTANTIATED (t) = 1;
2332 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = 1;
2333
2334 /* this should really be done by instantiate_member_templates */
2335 {
2336 tree method = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2337 for (; method; method = TREE_CHAIN (method))
2338 DECL_EXPLICITLY_INSTANTIATED (method) = 1;
2339 }
2340
2341 /* and data member templates, too */
2342 }
2343
2344 tree
2345 create_nested_upt (scope, name)
2346 tree scope, name;
2347 {
2348 tree t = make_lang_type (UNINSTANTIATED_P_TYPE);
2349 tree d = build_lang_decl (TYPE_DECL, name, t);
2350
2351 TYPE_NAME (t) = d;
2352 TYPE_VALUES (t) = TYPE_VALUES (scope);
2353 TYPE_CONTEXT (t) = scope;
2354
2355 pushdecl (d);
2356 return d;
2357 }