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.
5 This file is part of GNU CC.
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)
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.
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. */
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
42 extern struct obstack permanent_obstack
;
43 extern tree
grokdeclarator ();
46 extern char *input_filename
;
47 struct pending_inline
*pending_template_expansions
;
49 int processing_template_decl
;
50 int processing_template_defn
;
52 #define obstack_chunk_alloc xmalloc
53 #define obstack_chunk_free free
56 static void add_pending_template ();
58 void overload_template_name (), pop_template_decls ();
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.) */
65 begin_template_parm_list ()
68 push_obstacks (&permanent_obstack
, &permanent_obstack
);
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. */
79 process_template_parm (list
, 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
;
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
),
98 /* A template parameter is not modifiable. */
99 TREE_READONLY (parm
) = 1;
100 if (IS_AGGR_TYPE (TREE_TYPE (parm
)))
102 sorry ("aggregate template parameter types");
103 TREE_TYPE (parm
) = void_type_node
;
105 tinfo
= make_node (TEMPLATE_CONST_PARM
);
106 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
107 if (TREE_PERMANENT (parm
) == 0)
109 parm
= copy_node (parm
);
110 TREE_PERMANENT (parm
) = 1;
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
;
119 tree t
= make_node (TEMPLATE_TYPE_PARM
);
120 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
121 TYPE_MAIN_DECL (t
) = decl
;
125 if (IDENTIFIER_HAS_TYPE_VALUE (defval
))
126 defval
= IDENTIFIER_TYPE_VALUE (defval
);
128 defval
= TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (defval
));
132 parm
= build_tree_list (defval
, parm
);
133 return chainon (list
, parm
);
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
142 end_template_parm_list (parms
)
149 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
151 saved_parmlist
= make_tree_vec (nparms
);
153 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
155 tree p
= TREE_VALUE (parm
);
156 if (TREE_PURPOSE (parm
))
158 else if (saw_default
)
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");
165 if (TREE_CODE (p
) == TYPE_DECL
)
167 tree t
= TREE_TYPE (p
);
168 TEMPLATE_TYPE_SET_INFO (t
, saved_parmlist
, nparms
);
172 tree tinfo
= DECL_INITIAL (p
);
173 DECL_INITIAL (p
) = NULL_TREE
;
174 TEMPLATE_CONST_SET_INFO (tinfo
, saved_parmlist
, nparms
);
176 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
178 set_current_level_tags_transparency (1);
179 processing_template_decl
++;
180 return saved_parmlist
;
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. */
187 end_template_decl (d1
, d2
, is_class
, defn
)
188 tree d1
, d2
, is_class
;
192 struct template_info
*tmpl
;
194 tmpl
= (struct template_info
*) obstack_alloc (&permanent_obstack
,
195 sizeof (struct template_info
));
198 tmpl
->aggr
= is_class
;
200 /* cloned from reinit_parse_for_template */
201 tmpl
->filename
= input_filename
;
202 tmpl
->lineno
= lineno
;
203 tmpl
->parm_vec
= d1
; /* [eichin:19911015.2306EST] */
205 if (d2
== NULL_TREE
|| d2
== error_mark_node
)
213 decl
= build_lang_decl (TEMPLATE_DECL
, d2
, NULL_TREE
);
214 GNU_xref_decl (current_function_decl
, decl
);
218 if (TREE_CODE (d2
) == TEMPLATE_DECL
)
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.... */
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
),
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
)));
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);
253 /* @@ Somewhere, permanent allocation isn't being used. */
254 if (! DECL_TEMPLATE_IS_CLASS (decl
)
255 && TREE_CODE (DECL_TEMPLATE_RESULT (decl
)) == FUNCTION_DECL
)
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
));
262 /* If this is for a method, there's an extra binding level here. */
263 if (DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl
)) != NULL_TREE
)
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
);
271 DECL_TEMPLATE_INFO (decl
) = tmpl
;
272 DECL_TEMPLATE_PARMS (decl
) = d1
;
274 /* So that duplicate_decls can do the right thing. */
276 DECL_INITIAL (decl
) = error_mark_node
;
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
)
282 tree ctx
= DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl
));
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
)))
290 DECL_TEMPLATE_MEMBERS (tmpl
) =
291 perm_tree_cons (DECL_NAME (decl
), decl
, DECL_TEMPLATE_MEMBERS (tmpl
));
296 /* Otherwise, go back to top level first, and push the template decl
305 #if 0 /* It happens sometimes, with syntactic or semantic errors.
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);
313 while (! global_bindings_p ())
317 processing_template_decl
--;
318 (void) get_pending_sizes ();
321 /* If TYPE contains a template parm type, then substitute that type
322 with its actual type that is found in TVEC. */
324 grok_template_type (tvec
, type
)
328 switch (TREE_CODE (*type
))
330 case TEMPLATE_TYPE_PARM
:
331 if (*type
!= TYPE_MAIN_VARIANT (*type
))
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
));
340 *type
= TREE_VEC_ELT (tvec
, TEMPLATE_TYPE_IDX (*type
));
344 grok_template_type (tvec
, &TREE_TYPE (*type
));
350 /* take care of function's return type first */
351 grok_template_type (tvec
, &TREE_TYPE (*type
));
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
));
364 /* Convert all template arguments to their appropriate types, and return
365 a vector containing the resulting values. If any error occurs, return
368 coerce_template_parms (parms
, arglist
, in_decl
)
372 int nparms
, nargs
, i
, lost
= 0;
375 if (arglist
== NULL_TREE
)
377 else if (TREE_CODE (arglist
) == TREE_VEC
)
378 nargs
= TREE_VEC_LENGTH (arglist
);
380 nargs
= list_length (arglist
);
382 nparms
= TREE_VEC_LENGTH (parms
);
386 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
388 error ("incorrect number of parameters (%d, should be %d)",
391 cp_error_at ("in template expansion for decl `%D'", in_decl
);
392 return error_mark_node
;
395 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
396 vec
= copy_node (arglist
);
399 vec
= make_tree_vec (nparms
);
400 for (i
= 0; i
< nparms
; i
++)
407 arglist
= TREE_CHAIN (arglist
);
409 if (arg
== error_mark_node
)
412 arg
= TREE_VALUE (arg
);
415 arg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
417 TREE_VEC_ELT (vec
, i
) = arg
;
420 for (i
= 0; i
< nparms
; i
++)
422 tree arg
= TREE_VEC_ELT (vec
, i
);
423 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
425 int is_type
, requires_type
;
427 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
428 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
429 if (is_type
!= requires_type
)
432 cp_error ("type/value mismatch in template parameter list for `%D'",
435 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
439 val
= groktypename (arg
);
440 else if (TREE_CODE (arg
) == STRING_CST
)
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
;
448 grok_template_type (vec
, &TREE_TYPE (parm
));
449 val
= digest_init (TREE_TYPE (parm
), arg
, (tree
*) 0);
451 if (val
== error_mark_node
)
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
))
459 cp_error ("non-const `%E' cannot be used as template argument",
461 val
= error_mark_node
;
463 else if (TREE_CODE (val
) == ADDR_EXPR
)
465 tree a
= TREE_OPERAND (val
, 0);
466 if ((TREE_CODE (a
) == VAR_DECL
467 || TREE_CODE (a
) == FUNCTION_DECL
)
468 && ! DECL_PUBLIC (a
))
470 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
471 val
= error_mark_node
;
476 if (val
== error_mark_node
)
479 TREE_VEC_ELT (vec
, i
) = val
;
482 return error_mark_node
;
486 /* Given class template name and parameter list, produce a user-friendly name
487 for the instantiation. */
489 mangle_class_name_for_template (name
, parms
, arglist
)
493 static struct obstack scratch_obstack
;
494 static char *scratch_firstobj
;
497 if (!scratch_firstobj
)
499 gcc_obstack_init (&scratch_obstack
);
500 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
503 obstack_free (&scratch_obstack
, scratch_firstobj
);
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
513 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
515 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
520 nparms
= TREE_VEC_LENGTH (parms
);
521 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
522 for (i
= 0; i
< nparms
; i
++)
524 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
525 tree arg
= TREE_VEC_ELT (arglist
, i
);
530 if (TREE_CODE (parm
) == TYPE_DECL
)
532 cat (type_as_string (arg
, 0));
536 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
538 if (TREE_CODE (arg
) == TREE_LIST
)
540 /* New list cell was built because old chain link was in
542 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
543 arg
= TREE_VALUE (arg
);
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));
550 char *bufp
= obstack_next_free (&scratch_obstack
);
552 while (bufp
[offset
- 1] == ' ')
554 obstack_blank_fast (&scratch_obstack
, offset
);
556 /* B<C<char> >, not B<C<char>> */
557 if (bufp
[offset
- 1] == '>')
562 return (char *) obstack_base (&scratch_obstack
);
567 fatal ("out of (preallocated) string space creating template instantiation name");
572 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
573 parameters, find the desired type.
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.
579 IN_DECL, if non-NULL, is the template declaration we are trying to
582 lookup_template_class (d1
, arglist
, in_decl
)
586 tree
template, parmlist
;
590 my_friendly_assert (TREE_CODE (d1
) == IDENTIFIER_NODE
, 272);
591 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
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. */
599 return error_mark_node
;
600 if (TREE_CODE (template) != TEMPLATE_DECL
)
602 cp_error ("non-template type `%T' used as a template", d1
);
604 cp_error_at ("for template declaration `%D'", in_decl
);
605 return error_mark_node
;
607 parmlist
= DECL_TEMPLATE_PARMS (template);
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
))
614 tree t
= make_lang_type (UNINSTANTIATED_P_TYPE
);
616 id
= make_anon_name ();
617 d
= build_decl (TYPE_DECL
, id
, t
);
619 TYPE_VALUES (t
) = build_tree_list (template, arglist
);
620 pushdecl_top_level (d
);
624 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
626 id
= get_identifier (mangled_name
);
628 if (!IDENTIFIER_TEMPLATE (id
))
630 arglist
= copy_to_permanent (arglist
);
631 IDENTIFIER_TEMPLATE (id
) = perm_tree_cons (template, arglist
, NULL_TREE
);
637 push_template_decls (parmlist
, arglist
, class_level
)
638 tree parmlist
, arglist
;
643 /* Don't want to push values into global context. */
647 declare_pseudo_global_level ();
650 nparms
= TREE_VEC_LENGTH (parmlist
);
652 for (i
= 0; i
< nparms
; i
++)
654 int requires_type
, is_type
;
655 tree parm
= TREE_VALUE (TREE_VEC_ELT (parmlist
, i
));
656 tree arg
= TREE_VEC_ELT (arglist
, i
);
659 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
660 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
663 /* add typename to namespace */
666 error ("template use error: type provided where value needed");
670 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 't', 273);
671 decl
= build_decl (TYPE_DECL
, DECL_NAME (parm
), decl
);
675 /* add const decl to namespace */
679 error ("template use error: value provided where type needed");
682 val
= digest_init (TREE_TYPE (parm
), arg
, (tree
*) 0);
683 if (val
!= error_mark_node
)
685 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
687 DECL_INITIAL (decl
) = val
;
688 TREE_READONLY (decl
) = 1;
693 layout_decl (decl
, 0);
695 pushdecl_class_level (decl
);
703 pop_template_decls (parmlist
, arglist
, class_level
)
704 tree parmlist
, arglist
;
711 /* Should be defined in parse.h. */
715 uses_template_parms (t
)
720 switch (TREE_CODE (t
))
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
));
729 case IDENTIFIER_NODE
:
730 if (!IDENTIFIER_TEMPLATE (t
))
732 return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t
)));
734 /* aggregates of tree nodes */
737 int i
= TREE_VEC_LENGTH (t
);
739 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
744 if (uses_template_parms (TREE_PURPOSE (t
))
745 || uses_template_parms (TREE_VALUE (t
)))
747 return uses_template_parms (TREE_CHAIN (t
));
749 /* constructed type nodes */
752 return uses_template_parms (TREE_TYPE (t
));
757 if (!TYPE_IDENTIFIER (t
))
759 return uses_template_parms (TYPE_IDENTIFIER (t
));
761 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
763 return uses_template_parms (TREE_TYPE (t
));
765 if (uses_template_parms (TYPE_DOMAIN (t
)))
767 return uses_template_parms (TREE_TYPE (t
));
769 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
771 return uses_template_parms (TREE_TYPE (t
));
773 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
775 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
777 return uses_template_parms (TREE_TYPE (t
));
781 return uses_template_parms (DECL_NAME (t
));
783 if (uses_template_parms (TREE_TYPE (t
)))
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
)))
794 if (uses_template_parms (TREE_TYPE (t
)))
796 error ("template parms used where they can't be resolved");
801 return uses_template_parms (TREE_TYPE (t
));
803 return uses_template_parms (TREE_OPERAND (t
, 0));
805 /* template parm nodes */
806 case TEMPLATE_TYPE_PARM
:
807 case TEMPLATE_CONST_PARM
:
810 /* simple type nodes */
812 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
814 return uses_template_parms (TYPE_MAX_VALUE (t
));
829 /* Non-error_mark_node ERROR_MARKs are bad things. */
830 my_friendly_assert (t
== error_mark_node
, 274);
834 case UNINSTANTIATED_P_TYPE
:
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 */
843 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
851 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
852 if (uses_template_parms (TREE_OPERAND (t
, i
)))
859 sorry ("testing %s for template parms",
860 tree_code_name
[(int) TREE_CODE (t
)]);
861 my_friendly_abort (82);
868 instantiate_member_templates (classname
)
873 tree members
= DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id
)));
875 for (t
= members
; t
; t
= TREE_CHAIN (t
))
877 tree parmvec
, type
, classparms
, tdecl
, t2
;
878 int nparms
, xxx
= 0, i
;
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
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
),
898 /* Success -- well, no inconsistency, at least. */
899 for (i
= 0; i
< nparms
; i
++)
900 if (TREE_VEC_ELT (parmvec
, i
) == NULL_TREE
)
902 t2
= instantiate_template (tdecl
,
903 &TREE_VEC_ELT (parmvec
, 0));
904 type
= IDENTIFIER_TYPE_VALUE (id
);
905 my_friendly_assert (type
!= 0, 277);
910 cp_error_at ("type unification error instantiating `%D'", tdecl
);
911 cp_error ("while instantiating members of `%T'", classname
);
913 continue /* loop of members */;
916 my_friendly_abort (83);
921 struct tinst_level
*current_tinst_level
= 0;
922 struct tinst_level
*free_tinst_level
= 0;
925 push_tinst_level (name
)
928 struct tinst_level
*new;
929 tree global
= IDENTIFIER_GLOBAL_VALUE (name
);
931 if (free_tinst_level
)
933 new = free_tinst_level
;
934 free_tinst_level
= new->next
;
937 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
939 new->classname
= name
;
942 new->line
= DECL_SOURCE_LINE (global
);
943 new->file
= DECL_SOURCE_FILE (global
);
948 new->file
= input_filename
;
950 new->next
= current_tinst_level
;
951 current_tinst_level
= new;
957 struct tinst_level
*old
= current_tinst_level
;
959 current_tinst_level
= old
->next
;
960 old
->next
= free_tinst_level
;
961 free_tinst_level
= old
;
967 struct tinst_level
*p
= current_tinst_level
;
970 for (; p
->next
; p
= p
->next
)
976 instantiate_class_template (classname
, setup_parse
)
980 struct template_info
*template_info
;
983 if (classname
== error_mark_node
)
984 return error_mark_node
;
986 my_friendly_assert (TREE_CODE (classname
) == IDENTIFIER_NODE
, 278);
987 template = IDENTIFIER_TEMPLATE (classname
);
989 if (IDENTIFIER_HAS_TYPE_VALUE (classname
))
991 tree type
= IDENTIFIER_TYPE_VALUE (classname
);
992 if (TREE_CODE (type
) == UNINSTANTIATED_P_TYPE
)
994 if (TYPE_BEING_DEFINED (type
)
996 || CLASSTYPE_USE_TEMPLATE (type
) != 0)
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
);
1013 if (uses_template_parms (classname
))
1015 if (!TREE_TYPE (classname
))
1017 tree t
= make_lang_type (RECORD_TYPE
);
1018 tree d
= build_decl (TYPE_DECL
, classname
, t
);
1019 DECL_NAME (d
) = classname
;
1026 t1
= TREE_PURPOSE (template);
1027 my_friendly_assert (TREE_CODE (t1
) == TEMPLATE_DECL
, 279);
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)
1035 push_to_top_level ();
1036 template_info
= DECL_TEMPLATE_INFO (t1
);
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
++;
1059 tree t
, decl
, id
, tmpl
;
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);
1067 /* Now, put a copy of the decl in global scope, to avoid
1068 * recursive expansion. */
1069 decl
= IDENTIFIER_LOCAL_VALUE (id
);
1071 decl
= IDENTIFIER_CLASS_VALUE (id
);
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
));
1083 pop_from_top_level ();
1093 if (t1
== NULL_TREE
)
1094 return t2
== NULL_TREE
;
1095 if (t2
== NULL_TREE
)
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
)))
1102 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1106 lookup_nested_type_by_name (ctype
, name
)
1111 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1113 if (name
== TREE_PURPOSE (t
))
1114 return TREE_VALUE (t
);
1120 search_nested_type_in_tmpl (tmpl
, type
)
1125 if (tmpl
== NULL
|| TYPE_CONTEXT(type
) == NULL
)
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
)));
1134 tsubst (t
, args
, nargs
, in_decl
)
1141 if (t
== NULL_TREE
|| t
== error_mark_node
)
1144 type
= TREE_TYPE (t
);
1146 /* Minor optimization.
1147 ?? Are these really the most frequent cases? Is the savings
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
))
1158 if (TYPE_PTRMEMFUNC_P (t
))
1159 return build_ptrmemfunc_type
1160 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1162 /* else fall through */
1165 case IDENTIFIER_NODE
:
1178 if (t
== integer_type_node
)
1181 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1182 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
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
));
1188 case TEMPLATE_TYPE_PARM
:
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
));
1196 case TEMPLATE_CONST_PARM
:
1197 return args
[TEMPLATE_CONST_IDX (t
)];
1202 tree fnargs
, result
;
1204 if (type
== TREE_TYPE (t
)
1205 && (DECL_CONTEXT (t
) == NULL_TREE
1206 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) != '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')
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
;
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. */
1225 n_methods
= TREE_VEC_LENGTH (methods
);
1229 if (!strncmp (OPERATOR_TYPENAME_FORMAT
,
1230 IDENTIFIER_POINTER (name
),
1231 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
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
));
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
);
1243 fprintf (stderr
, "\nfor function %s in class %s:\n",
1244 IDENTIFIER_POINTER (name
),
1245 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx
)));
1247 for (i
= 0; i
< n_methods
; i
++)
1251 method
= TREE_VEC_ELT (methods
, i
);
1252 if (method
== NULL_TREE
|| DECL_NAME (method
) != name
)
1257 for (; method
; method
= DECL_CHAIN (method
))
1259 my_friendly_assert (TREE_CODE (method
) == FUNCTION_DECL
,
1261 if (! comptypes (type
, TREE_TYPE (method
), 1))
1263 tree mtype
= TREE_TYPE (method
);
1266 /* Keep looking for a method that matches
1267 perfectly. This takes care of the problem
1268 where destructors (which have implicit int args)
1269 look like constructors which have an int arg. */
1273 t1
= TYPE_ARG_TYPES (mtype
);
1274 t2
= TYPE_ARG_TYPES (type
);
1275 if (TREE_CODE (mtype
) == FUNCTION_TYPE
)
1276 t2
= TREE_CHAIN (t2
);
1278 if (list_eq (t1
, t2
))
1280 if (TREE_CODE (mtype
) == FUNCTION_TYPE
)
1283 newtype
= build_function_type (TREE_TYPE (type
),
1284 TYPE_ARG_TYPES (type
));
1285 newtype
= build_type_variant (newtype
,
1286 TYPE_READONLY (type
),
1287 TYPE_VOLATILE (type
));
1289 if (TREE_TYPE (type
) != TREE_TYPE (mtype
))
1290 goto maybe_bad_return_type
;
1292 else if (TYPE_METHOD_BASETYPE (mtype
)
1293 == TYPE_METHOD_BASETYPE (type
))
1295 /* Types didn't match, but arg types and
1296 `this' do match, so the return type is
1297 all that should be messing it up. */
1298 maybe_bad_return_type
:
1299 if (TREE_TYPE (type
) != TREE_TYPE (mtype
))
1300 error ("inconsistent return types for method `%s' in class `%s'",
1301 IDENTIFIER_POINTER (name
),
1302 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx
)));
1311 fprintf (stderr
, "\tfound %s\n\n",
1312 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method
)));
1314 if (DECL_ARTIFICIAL (method
))
1316 cp_error ("template for method `%D' which has default implementation in class `%T'", name
, ctx
);
1318 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl
);
1319 return error_mark_node
;
1322 if (DECL_ARGUMENTS (method
)
1323 && ! TREE_PERMANENT (DECL_ARGUMENTS (method
)))
1324 /* @@ Is this early enough? Might we want to do
1325 this instead while processing the expansion? */
1326 DECL_ARGUMENTS (method
)
1327 = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1331 if (r
== NULL_TREE
&& pass
== 0)
1334 method
= TREE_VEC_ELT (methods
, i
);
1343 ? "template for method `%D' doesn't match any in class `%T'"
1344 : "method `%D' not found in class `%T'", name
, ctx
);
1346 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl
);
1347 return error_mark_node
;
1357 decls
= lookup_name_nonclass (r
);
1358 if (decls
== NULL_TREE
)
1360 else if (TREE_CODE (decls
) == TREE_LIST
)
1361 for (decls
= TREE_VALUE (decls
); decls
;
1362 decls
= DECL_CHAIN (decls
))
1364 if (TREE_CODE (decls
) == FUNCTION_DECL
1365 && TREE_TYPE (decls
) == type
)
1376 if (TREE_CODE (val
) == FUNCTION_DECL
1377 && TREE_TYPE (val
) == type
)
1386 tree a
= build_decl_overload (r
, TYPE_VALUES (type
),
1387 DECL_CONTEXT (t
) != NULL_TREE
);
1388 r
= build_lang_decl (FUNCTION_DECL
, r
, type
);
1389 DECL_ASSEMBLER_NAME (r
) = a
;
1391 else if (DECL_INLINE (r
) && DECL_SAVED_INSNS (r
))
1393 /* This overrides the template version, use it. */
1398 TREE_PUBLIC (r
) = 1;
1399 DECL_EXTERNAL (r
) = 1;
1400 TREE_STATIC (r
) = 0;
1401 DECL_INTERFACE_KNOWN (r
) = 0;
1402 DECL_INLINE (r
) = DECL_INLINE (t
);
1404 #if 0 /* Maybe later. -jason */
1405 struct tinst_level
*til
= tinst_for_decl();
1407 /* should always be true under new approach */
1410 DECL_SOURCE_FILE (r
) = til
->file
;
1411 DECL_SOURCE_LINE (r
) = til
->line
;
1416 DECL_SOURCE_FILE (r
) = DECL_SOURCE_FILE (t
);
1417 DECL_SOURCE_LINE (r
) = DECL_SOURCE_LINE (t
);
1420 DECL_CLASS_CONTEXT (r
) = tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1421 make_decl_rtl (r
, NULL_PTR
, 1);
1422 DECL_ARGUMENTS (r
) = fnargs
;
1423 DECL_RESULT (r
) = result
;
1425 if (DECL_CONTEXT (t
) == NULL_TREE
1426 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) != 't')
1427 push_overloaded_decl_top_level (r
, 0);
1435 r
= build_decl (PARM_DECL
, DECL_NAME (t
), type
);
1436 DECL_INITIAL (r
) = TREE_TYPE (r
);
1438 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1444 tree purpose
, value
, chain
, result
;
1445 int via_public
, via_virtual
, via_protected
;
1447 if (t
== void_list_node
)
1450 via_public
= TREE_VIA_PUBLIC (t
);
1451 via_protected
= TREE_VIA_PROTECTED (t
);
1452 via_virtual
= TREE_VIA_VIRTUAL (t
);
1454 purpose
= TREE_PURPOSE (t
);
1456 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
1457 value
= TREE_VALUE (t
);
1459 value
= tsubst (value
, args
, nargs
, in_decl
);
1460 chain
= TREE_CHAIN (t
);
1461 if (chain
&& chain
!= void_type_node
)
1462 chain
= tsubst (chain
, args
, nargs
, in_decl
);
1463 if (purpose
== TREE_PURPOSE (t
)
1464 && value
== TREE_VALUE (t
)
1465 && chain
== TREE_CHAIN (t
))
1467 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
1468 purpose
, value
, chain
);
1469 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
1474 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
1475 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
1476 bzero ((char *) elts
, len
* sizeof (tree
));
1478 for (i
= 0; i
< len
; i
++)
1480 elts
[i
] = tsubst (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
1481 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
1488 t
= make_tree_vec (len
);
1489 for (i
= 0; i
< len
; i
++)
1490 TREE_VEC_ELT (t
, i
) = elts
[i
];
1494 case REFERENCE_TYPE
:
1497 enum tree_code code
;
1498 if (type
== TREE_TYPE (t
))
1501 code
= TREE_CODE (t
);
1502 if (code
== POINTER_TYPE
)
1503 r
= build_pointer_type (type
);
1505 r
= build_reference_type (type
);
1506 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1507 /* Will this ever be needed for TYPE_..._TO values? */
1512 return build_offset_type
1513 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
1517 tree values
= TYPE_VALUES (t
); /* same as TYPE_ARG_TYPES */
1518 tree context
= TYPE_CONTEXT (t
);
1521 /* Don't bother recursing if we know it won't change anything. */
1522 if (values
!= void_list_node
)
1523 values
= tsubst (values
, args
, nargs
, in_decl
);
1525 context
= tsubst (context
, args
, nargs
, in_decl
);
1526 /* Could also optimize cases where return value and
1527 values have common elements (e.g., T min(const &T, const T&). */
1529 /* If the above parameters haven't changed, just return the type. */
1530 if (type
== TREE_TYPE (t
)
1531 && values
== TYPE_VALUES (t
)
1532 && context
== TYPE_CONTEXT (t
))
1535 /* Construct a new type node and return it. */
1536 if (TREE_CODE (t
) == FUNCTION_TYPE
1537 && context
== NULL_TREE
)
1539 new_value
= build_function_type (type
, values
);
1541 else if (context
== NULL_TREE
)
1543 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
1544 args
, nargs
, in_decl
);
1545 new_value
= build_cplus_method_type (base
, type
,
1546 TREE_CHAIN (values
));
1550 new_value
= make_node (TREE_CODE (t
));
1551 TREE_TYPE (new_value
) = type
;
1552 TYPE_CONTEXT (new_value
) = context
;
1553 TYPE_VALUES (new_value
) = values
;
1554 TYPE_SIZE (new_value
) = TYPE_SIZE (t
);
1555 TYPE_ALIGN (new_value
) = TYPE_ALIGN (t
);
1556 TYPE_MODE (new_value
) = TYPE_MODE (t
);
1557 if (TYPE_METHOD_BASETYPE (t
))
1558 TYPE_METHOD_BASETYPE (new_value
) = tsubst (TYPE_METHOD_BASETYPE (t
),
1559 args
, nargs
, in_decl
);
1560 /* Need to generate hash value. */
1561 my_friendly_abort (84);
1563 new_value
= build_type_variant (new_value
,
1570 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
1572 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
1574 r
= build_cplus_array_type (type
, domain
);
1578 case UNINSTANTIATED_P_TYPE
:
1580 int nparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t
)));
1581 tree argvec
= make_tree_vec (nparms
);
1582 tree parmvec
= UPT_PARMS (t
);
1585 for (i
= 0; i
< nparms
; i
++)
1586 TREE_VEC_ELT (argvec
, i
) = tsubst (TREE_VEC_ELT (parmvec
, i
),
1587 args
, nargs
, in_decl
);
1588 id
= lookup_template_class (DECL_NAME (UPT_TEMPLATE (t
)), argvec
, NULL_TREE
);
1589 if (! IDENTIFIER_HAS_TYPE_VALUE (id
)) {
1590 instantiate_class_template(id
, 0);
1591 /* set up pending_classes */
1592 add_pending_template (id
);
1594 TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id
)) =
1595 IDENTIFIER_TYPE_VALUE (id
);
1597 rt
= IDENTIFIER_TYPE_VALUE (id
);
1599 /* kung: this part handles nested type in template definition */
1601 if ( !ANON_AGGRNAME_P (DECL_NAME(TYPE_NAME(t
))))
1603 rt
= search_nested_type_in_tmpl (rt
, t
);
1606 return build_type_variant (rt
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1611 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1612 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1613 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
1617 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
1618 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
1621 sorry ("use of `%s' in function template",
1622 tree_code_name
[(int) TREE_CODE (t
)]);
1623 return error_mark_node
;
1628 instantiate_template (tmpl
, targ_ptr
)
1629 tree tmpl
, *targ_ptr
;
1633 struct pending_inline
*p
;
1634 struct template_info
*t
;
1635 struct obstack
*old_fmp_obstack
;
1636 extern struct obstack
*function_maybepermanent_obstack
;
1638 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1639 old_fmp_obstack
= function_maybepermanent_obstack
;
1640 function_maybepermanent_obstack
= &permanent_obstack
;
1642 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
1643 len
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
1645 for (fndecl
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1646 fndecl
; fndecl
= TREE_CHAIN (fndecl
))
1648 tree
*t1
= &TREE_VEC_ELT (TREE_PURPOSE (fndecl
), 0);
1649 for (i
= len
- 1; i
>= 0; i
--)
1650 if (t1
[i
] != targ_ptr
[i
])
1653 /* Here, we have a match. */
1654 fndecl
= TREE_VALUE (fndecl
);
1661 targs
= make_tree_vec (len
);
1664 TREE_VEC_ELT (targs
, i
) = targ_ptr
[i
];
1666 /* substitute template parameters */
1667 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
,
1668 TREE_VEC_LENGTH (targs
), tmpl
);
1670 if (fndecl
== error_mark_node
)
1673 /* If it's a static member fn in the template, we need to change it
1674 into a FUNCTION_TYPE and chop off its this pointer. */
1675 if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl
))) == METHOD_TYPE
1676 && DECL_STATIC_FUNCTION_P (fndecl
))
1678 tree olddecl
= DECL_RESULT (tmpl
);
1679 revert_static_member_fn (&DECL_RESULT (tmpl
), NULL
, NULL
);
1680 /* Chop off the this pointer that grokclassfn so kindly added
1681 for us (it didn't know yet if the fn was static or not). */
1682 DECL_ARGUMENTS (olddecl
) = TREE_CHAIN (DECL_ARGUMENTS (olddecl
));
1683 DECL_ARGUMENTS (fndecl
) = TREE_CHAIN (DECL_ARGUMENTS (fndecl
));
1686 t
= DECL_TEMPLATE_INFO (tmpl
);
1688 /* If we have a preexisting version of this function, don't expand
1689 the template version, use the other instead. */
1690 if (DECL_INLINE (fndecl
) && DECL_SAVED_INSNS (fndecl
))
1692 SET_DECL_TEMPLATE_SPECIALIZATION (fndecl
);
1693 p
= (struct pending_inline
*)0;
1697 SET_DECL_IMPLICIT_INSTANTIATION (fndecl
);
1698 p
= (struct pending_inline
*) permalloc (sizeof (struct pending_inline
));
1699 p
->parm_vec
= t
->parm_vec
;
1700 p
->bindings
= targs
;
1708 char * f
= input_filename
;
1710 lineno
= p
->lineno
= t
->lineno
;
1711 input_filename
= p
->filename
= t
->filename
;
1713 extract_interface_info ();
1715 if (interface_unknown
&& flag_external_templates
)
1717 if (DECL_CLASS_CONTEXT (fndecl
)
1718 && CLASSTYPE_INTERFACE_KNOWN (DECL_CLASS_CONTEXT (fndecl
)))
1720 interface_unknown
= 0;
1722 = CLASSTYPE_INTERFACE_ONLY (DECL_CLASS_CONTEXT (fndecl
));
1724 else if (! DECL_IN_SYSTEM_HEADER (tmpl
))
1725 warn_if_unknown_interface ();
1728 if (interface_unknown
|| ! flag_external_templates
)
1729 p
->interface
= 1; /* unknown */
1731 p
->interface
= interface_only
? 0 : 2;
1736 extract_interface_info ();
1740 p
= (struct pending_inline
*)0;
1742 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) =
1743 tree_cons (targs
, fndecl
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1745 if (p
== (struct pending_inline
*)0)
1749 else if (DECL_INLINE (fndecl
))
1751 DECL_PENDING_INLINE_INFO (fndecl
) = p
;
1752 p
->next
= pending_inlines
;
1753 pending_inlines
= p
;
1757 p
->next
= pending_template_expansions
;
1758 pending_template_expansions
= p
;
1761 function_maybepermanent_obstack
= old_fmp_obstack
;
1767 /* classlevel should now never be true. jason 4/12/94 */
1769 undo_template_name_overload (id
, classlevel
)
1775 template = IDENTIFIER_TEMPLATE (id
);
1779 #if 0 /* not yet, should get fixed properly later */
1783 /* This was a botch... See `overload_template_name' just below. */
1789 /* classlevel should now never be true. jason 4/12/94 */
1791 overload_template_name (id
, classlevel
)
1795 tree
template, t
, decl
;
1796 struct template_info
*tinfo
;
1798 my_friendly_assert (TREE_CODE (id
) == IDENTIFIER_NODE
, 284);
1799 template = IDENTIFIER_TEMPLATE (id
);
1803 template = TREE_PURPOSE (template);
1804 tinfo
= DECL_TEMPLATE_INFO (template);
1805 template = DECL_NAME (template);
1806 my_friendly_assert (template != NULL_TREE
, 285);
1809 /* This was a botch... names of templates do not get their own private
1810 scopes. Rather, they should go into the binding level already created
1811 by push_template_decls. Except that there isn't one of those for
1816 declare_pseudo_global_level ();
1820 t
= xref_tag (tinfo
->aggr
, id
, NULL_TREE
, 0);
1821 my_friendly_assert (TREE_CODE (t
) == RECORD_TYPE
1822 || TREE_CODE (t
) == UNION_TYPE
1823 || TREE_CODE (t
) == UNINSTANTIATED_P_TYPE
, 286);
1825 decl
= build_decl (TYPE_DECL
, template, t
);
1826 SET_DECL_ARTIFICIAL (decl
);
1828 #if 0 /* fix this later */
1829 /* We don't want to call here if the work has already been done. */
1831 ? IDENTIFIER_CLASS_VALUE (template)
1832 : IDENTIFIER_LOCAL_VALUE (template));
1834 && TREE_CODE (t
) == TYPE_DECL
1835 && TREE_TYPE (t
) == t
)
1836 my_friendly_abort (85);
1840 pushdecl_class_level (decl
);
1844 #if 0 /* This seems bogus to me; if it isn't, explain why. (jason) */
1845 /* Fake this for now, just to make dwarfout.c happy. It will have to
1846 be done in a proper way later on. */
1847 DECL_CONTEXT (decl
) = t
;
1851 /* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
1853 end_template_instantiation (name
)
1856 extern struct pending_input
*to_be_restored
;
1859 processing_template_defn
--;
1860 if (!flag_external_templates
)
1861 interface_unknown
--;
1863 /* Restore the old parser input state. */
1864 if (yychar
== YYEMPTY
)
1866 if (yychar
!= END_OF_SAVED_INPUT
)
1867 error ("parse error at end of class template");
1870 restore_pending_input (to_be_restored
);
1874 /* Our declarations didn't get stored in the global slot, since
1875 there was a (supposedly tags-transparent) scope in between. */
1876 t
= IDENTIFIER_TYPE_VALUE (name
);
1877 my_friendly_assert (t
!= NULL_TREE
1878 && TREE_CODE_CLASS (TREE_CODE (t
)) == 't',
1880 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
1881 /* Make methods of template classes static, unless
1882 -fexternal-templates is given. */
1883 if (!flag_external_templates
)
1884 SET_CLASSTYPE_INTERFACE_UNKNOWN (t
);
1885 decl
= IDENTIFIER_GLOBAL_VALUE (name
);
1886 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 288);
1888 undo_template_name_overload (name
, 0);
1889 t
= IDENTIFIER_TEMPLATE (name
);
1890 pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t
)), TREE_VALUE (t
),
1892 /* This will fix up the type-value field. */
1894 pop_from_top_level ();
1896 #ifdef DWARF_DEBUGGING_INFO
1897 if (write_symbols
== DWARF_DEBUG
&& TREE_CODE (decl
) == TYPE_DECL
)
1899 /* We just completed the definition of a new file-scope type,
1900 so we can go ahead and output debug-info for it now. */
1901 TYPE_STUB_DECL (TREE_TYPE (decl
)) = decl
;
1902 rest_of_type_compilation (TREE_TYPE (decl
), 1);
1904 #endif /* DWARF_DEBUGGING_INFO */
1906 /* Restore interface/implementation settings. */
1907 extract_interface_info ();
1910 /* Store away the text of an template. */
1913 reinit_parse_for_template (yychar
, d1
, d2
)
1917 struct template_info
*template_info
;
1918 extern struct obstack inline_text_obstack
; /* see comment in lex.c */
1920 if (d2
== NULL_TREE
|| d2
== error_mark_node
)
1923 /* @@ Should use temp obstack, and discard results. */
1924 reinit_parse_for_block (yychar
, &inline_text_obstack
, 1);
1928 if (TREE_CODE (d2
) == IDENTIFIER_NODE
)
1929 d2
= IDENTIFIER_GLOBAL_VALUE (d2
);
1932 template_info
= DECL_TEMPLATE_INFO (d2
);
1935 template_info
= (struct template_info
*) permalloc (sizeof (struct template_info
));
1936 bzero ((char *) template_info
, sizeof (struct template_info
));
1937 DECL_TEMPLATE_INFO (d2
) = template_info
;
1939 template_info
->filename
= input_filename
;
1940 template_info
->lineno
= lineno
;
1941 reinit_parse_for_block (yychar
, &inline_text_obstack
, 1);
1942 template_info
->text
= obstack_base (&inline_text_obstack
);
1943 template_info
->length
= obstack_object_size (&inline_text_obstack
);
1944 obstack_finish (&inline_text_obstack
);
1945 template_info
->parm_vec
= d1
;
1948 /* Type unification.
1950 We have a function template signature with one or more references to
1951 template parameters, and a parameter list we wish to fit to this
1952 template. If possible, produce a list of parameters for the template
1953 which will cause it to fit the supplied parameter list.
1955 Return zero for success, 2 for an incomplete match that doesn't resolve
1956 all the types, and 1 for complete failure. An error message will be
1957 printed only for an incomplete match.
1959 TPARMS[NTPARMS] is an array of template parameter types;
1960 TARGS[NTPARMS] is the array of template parameter values. PARMS is
1961 the function template's signature (using TEMPLATE_PARM_IDX nodes),
1962 and ARGS is the argument list we're trying to match against it.
1964 If SUBR is 1, we're being called recursively (to unify the arguments of
1965 a function or method parameter of a function template), so don't zero
1966 out targs and don't fail on an incomplete match. */
1969 type_unification (tparms
, targs
, parms
, args
, nsubsts
, subr
)
1970 tree tparms
, *targs
, parms
, args
;
1975 int ntparms
= TREE_VEC_LENGTH (tparms
);
1977 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
1978 my_friendly_assert (TREE_CODE (parms
) == TREE_LIST
, 290);
1979 /* ARGS could be NULL (via a call from parse.y to
1980 build_x_function_call). */
1982 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
1983 my_friendly_assert (ntparms
> 0, 292);
1986 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
1989 && parms
!= void_list_node
1991 && args
!= void_list_node
)
1993 parm
= TREE_VALUE (parms
);
1994 parms
= TREE_CHAIN (parms
);
1995 arg
= TREE_VALUE (args
);
1996 args
= TREE_CHAIN (args
);
1998 if (arg
== error_mark_node
)
2000 if (arg
== unknown_type_node
)
2003 if (TREE_CODE (arg
) == VAR_DECL
)
2004 arg
= TREE_TYPE (arg
);
2005 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
2006 arg
= TREE_TYPE (arg
);
2008 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2010 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
2011 arg
= TREE_TYPE (arg
);
2014 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2015 arg
= TREE_TYPE (arg
);
2017 if (TREE_CODE (parm
) != REFERENCE_TYPE
)
2019 if (TREE_CODE (arg
) == FUNCTION_TYPE
2020 || TREE_CODE (arg
) == METHOD_TYPE
)
2021 arg
= build_pointer_type (arg
);
2022 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
2023 arg
= build_pointer_type (TREE_TYPE (arg
));
2025 arg
= TYPE_MAIN_VARIANT (arg
);
2028 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
))
2036 /* Fail if we've reached the end of the parm list, and more args
2037 are present, and the parm list isn't variadic. */
2038 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
2040 /* Fail if parms are left and they don't have default values. */
2042 && parms
!= void_list_node
2043 && TREE_PURPOSE (parms
) == NULL_TREE
)
2046 for (i
= 0; i
< ntparms
; i
++)
2049 error ("incomplete type unification");
2055 /* Tail recursion is your friend. */
2057 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
)
2058 tree tparms
, *targs
, parm
, arg
;
2059 int *nsubsts
, ntparms
;
2063 /* I don't think this will do the right thing with respect to types.
2064 But the only case I've seen it in so far has been array bounds, where
2065 signedness is the only information lost, and I think that will be
2067 while (TREE_CODE (parm
) == NOP_EXPR
)
2068 parm
= TREE_OPERAND (parm
, 0);
2070 if (arg
== error_mark_node
)
2072 if (arg
== unknown_type_node
)
2077 switch (TREE_CODE (parm
))
2079 case TEMPLATE_TYPE_PARM
:
2081 if (TEMPLATE_TYPE_TPARMLIST (parm
) != tparms
)
2083 error ("mixed template headers?!");
2084 my_friendly_abort (86);
2087 idx
= TEMPLATE_TYPE_IDX (parm
);
2089 /* Template type parameters cannot contain cv-quals; i.e.
2090 template <class T> void f (T& a, T& b) will not generate
2091 void f (const int& a, const int& b). */
2092 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
2093 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
2095 arg
= TYPE_MAIN_VARIANT (arg
);
2098 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
2099 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
2100 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
2103 /* Simple cases: Value already set, does match or doesn't. */
2104 if (targs
[idx
] == arg
)
2106 else if (targs
[idx
])
2108 /* Check for mixed types and values. */
2109 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
2113 case TEMPLATE_CONST_PARM
:
2115 idx
= TEMPLATE_CONST_IDX (parm
);
2116 if (targs
[idx
] == arg
)
2118 else if (targs
[idx
])
2120 tree t
= targs
[idx
];
2121 if (TREE_CODE (t
) == TREE_CODE (arg
))
2122 switch (TREE_CODE (arg
))
2125 if (tree_int_cst_equal (t
, arg
))
2129 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t
), TREE_REAL_CST (arg
)))
2132 /* STRING_CST values are not valid template const parms. */
2136 my_friendly_abort (87);
2139 /* else if (typeof arg != tparms[idx])
2142 targs
[idx
] = copy_to_permanent (arg
);
2146 if (TREE_CODE (arg
) != POINTER_TYPE
)
2148 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2151 case REFERENCE_TYPE
:
2152 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
, nsubsts
);
2155 if (TREE_CODE (arg
) != ARRAY_TYPE
)
2157 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
2160 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2165 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2168 if (TREE_CODE (parm
) == INTEGER_TYPE
)
2170 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
2171 && unify (tparms
, targs
, ntparms
,
2172 TYPE_MIN_VALUE (parm
), TYPE_MIN_VALUE (arg
), nsubsts
))
2174 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
2175 && unify (tparms
, targs
, ntparms
,
2176 TYPE_MAX_VALUE (parm
), TYPE_MAX_VALUE (arg
), nsubsts
))
2179 /* As far as unification is concerned, this wins. Later checks
2180 will invalidate it if necessary. */
2183 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2185 if (TREE_CODE (arg
) != INTEGER_CST
)
2187 return !tree_int_cst_equal (parm
, arg
);
2192 t1
= TREE_OPERAND (parm
, 0);
2193 t2
= TREE_OPERAND (parm
, 1);
2194 if (TREE_CODE (t1
) != TEMPLATE_CONST_PARM
)
2196 return unify (tparms
, targs
, ntparms
, t1
,
2197 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
2204 if (TREE_CODE (arg
) != TREE_VEC
)
2206 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
2208 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
2209 if (unify (tparms
, targs
, ntparms
,
2210 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
2216 case UNINSTANTIATED_P_TYPE
:
2219 /* Unification of something that is not a class fails. */
2220 if (! IS_AGGR_TYPE (arg
))
2222 a
= IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg
));
2223 if (a
&& UPT_TEMPLATE (parm
) == TREE_PURPOSE (a
))
2224 return unify (tparms
, targs
, ntparms
, UPT_PARMS (parm
),
2225 TREE_VALUE (a
), nsubsts
);
2226 /* FIXME: Should check base conversions here. */
2231 if (TYPE_PTRMEMFUNC_FLAG (parm
))
2232 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
2235 /* Allow trivial conversions. */
2236 if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
)
2237 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
2238 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
2243 if (TREE_CODE (arg
) != METHOD_TYPE
)
2248 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
2251 return type_unification (tparms
, targs
, TYPE_ARG_TYPES (parm
),
2252 TYPE_ARG_TYPES (arg
), nsubsts
, 1);
2255 if (TREE_CODE (arg
) != OFFSET_TYPE
)
2257 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
2258 TYPE_OFFSET_BASETYPE (arg
), nsubsts
))
2260 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2261 TREE_TYPE (arg
), nsubsts
);
2264 sorry ("use of `%s' in template type unification",
2265 tree_code_name
[(int) TREE_CODE (parm
)]);
2274 do_pending_expansions ()
2276 struct pending_inline
*i
, *new_list
= 0;
2278 if (!pending_template_expansions
)
2282 fprintf (stderr
, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
2285 i
= pending_template_expansions
;
2290 struct pending_inline
*next
= i
->next
;
2294 #define DECIDE(N) do {decision=(N); goto decided;} while(0)
2296 my_friendly_assert (TREE_CODE (t
) == FUNCTION_DECL
2297 || TREE_CODE (t
) == VAR_DECL
, 294);
2298 if (TREE_ASM_WRITTEN (t
))
2301 if (DECL_EXPLICIT_INSTANTIATION (t
))
2302 DECIDE (! DECL_EXTERNAL (t
));
2303 else if (! flag_implicit_templates
)
2306 if (i
->interface
== 1)
2307 /* OK, it was an implicit instantiation. */
2308 TREE_PUBLIC (t
) = 0;
2310 /* If it's a method, let the class type decide it.
2311 @@ What if the method template is in a separate file?
2312 Maybe both file contexts should be taken into account?
2313 Maybe only do this if i->interface == 1 (unknown)? */
2314 context
= DECL_CONTEXT (t
);
2315 if (context
!= NULL_TREE
2316 && TREE_CODE_CLASS (TREE_CODE (context
)) == 't')
2318 /* I'm interested in the context of this version of the function,
2319 not the original virtual declaration. */
2320 context
= DECL_CLASS_CONTEXT (t
);
2322 /* If `unknown', we might want a static copy.
2323 If `implementation', we want a global one.
2324 If `interface', ext ref. */
2325 if (CLASSTYPE_INTERFACE_KNOWN (context
))
2326 DECIDE (!CLASSTYPE_INTERFACE_ONLY (context
));
2327 #if 0 /* This doesn't get us stuff needed only by the file initializer. */
2328 DECIDE (TREE_USED (t
));
2329 #else /* This compiles too much stuff, but that's probably better in
2330 most cases than never compiling the stuff we need. */
2335 if (i
->interface
== 1)
2336 DECIDE (TREE_USED (t
));
2338 DECIDE (i
->interface
);
2342 print_node_brief (stderr
, decision
? "yes: " : "no: ", t
, 0);
2343 fprintf (stderr
, "\t%s\n",
2344 (DECL_ASSEMBLER_NAME (t
)
2345 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t
))
2350 i
->next
= pending_inlines
;
2351 pending_inlines
= i
;
2360 pending_template_expansions
= new_list
;
2361 if (!pending_inlines
)
2363 do_pending_inlines ();
2368 struct pending_template
{
2369 struct pending_template
*next
;
2373 static struct pending_template
* pending_templates
;
2376 do_pending_templates ()
2378 struct pending_template
* t
;
2380 for ( t
= pending_templates
; t
; t
= t
->next
)
2382 instantiate_class_template (t
->id
, 1);
2385 for ( t
= pending_templates
; t
; t
= pending_templates
)
2387 pending_templates
= t
->next
;
2393 add_pending_template (pt
)
2396 struct pending_template
*p
;
2398 p
= (struct pending_template
*) malloc (sizeof (struct pending_template
));
2399 p
->next
= pending_templates
;
2400 pending_templates
= p
;
2404 /* called from the parser. */
2406 do_function_instantiation (declspecs
, declarator
, storage
)
2407 tree declspecs
, declarator
, storage
;
2409 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, 0);
2410 tree name
= DECL_NAME (decl
);
2411 tree fn
= IDENTIFIER_GLOBAL_VALUE (name
);
2412 tree result
= NULL_TREE
;
2415 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
2416 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2418 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn
));
2419 tree
*targs
= (tree
*) malloc (sizeof (tree
) * ntparms
);
2421 i
= type_unification (DECL_TEMPLATE_PARMS (fn
), targs
,
2422 TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2423 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
2428 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
2430 result
= instantiate_template (fn
, targs
);
2435 cp_error ("no matching template for `%D' found", decl
);
2437 if (flag_external_templates
)
2440 SET_DECL_EXPLICIT_INSTANTIATION (result
);
2441 TREE_PUBLIC (result
) = 1;
2443 if (storage
== NULL_TREE
)
2445 DECL_INTERFACE_KNOWN (result
) = 1;
2446 DECL_EXTERNAL (result
) = 0;
2447 TREE_STATIC (result
) = 1;
2449 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2452 cp_error ("storage class `%D' applied to template instantiation",
2457 do_type_instantiation (name
, storage
)
2460 tree t
= TREE_TYPE (name
);
2463 /* With -fexternal-templates, explicit instantiations are treated the same
2464 as implicit ones. */
2465 if (flag_external_templates
)
2468 if (TYPE_SIZE (t
) == NULL_TREE
)
2470 cp_error ("explicit instantiation of `%#T' before definition of template",
2475 if (storage
== NULL_TREE
)
2477 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2481 cp_error ("storage class `%D' applied to template instantiation",
2486 /* We've already instantiated this. */
2487 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
))
2490 cp_pedwarn ("multiple explicit instantiation of `%#T'", t
);
2494 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
2496 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
2497 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
2498 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
2499 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
2500 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
2503 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2504 rest_of_type_compilation (t
, 1);
2510 /* Classes nested in template classes currently don't have an
2511 IDENTIFIER_TEMPLATE--their out-of-line members are handled
2512 by the enclosing template class. Note that there are name
2513 conflict bugs with this approach. */
2514 tmp
= TYPE_IDENTIFIER (t
);
2515 if (IDENTIFIER_TEMPLATE (tmp
))
2516 instantiate_member_templates (tmp
);
2518 /* this should really be done by instantiate_member_templates */
2519 tmp
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
2520 for (; tmp
; tmp
= TREE_CHAIN (tmp
))
2522 if (DECL_TEMPLATE_SPECIALIZATION (tmp
)
2523 || (DECL_USE_TEMPLATE (tmp
) == 0
2524 && CLASSTYPE_TEMPLATE_SPECIALIZATION (t
)))
2527 SET_DECL_EXPLICIT_INSTANTIATION (tmp
);
2528 TREE_PUBLIC (tmp
) = 1;
2531 DECL_INTERFACE_KNOWN (tmp
) = 1;
2532 DECL_EXTERNAL (tmp
) = 0;
2533 TREE_STATIC (tmp
) = 1;
2538 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2540 if (TREE_CODE (tmp
) == VAR_DECL
)
2541 /* eventually do something */;
2545 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2546 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
2547 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
2552 create_nested_upt (scope
, name
)
2555 tree t
= make_lang_type (UNINSTANTIATED_P_TYPE
);
2556 tree d
= build_decl (TYPE_DECL
, name
, t
);
2559 TYPE_VALUES (t
) = TYPE_VALUES (scope
);
2560 TYPE_CONTEXT (t
) = scope
;