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