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
43 extern struct obstack permanent_obstack
;
44 extern tree
grokdeclarator ();
47 extern char *input_filename
;
48 struct pending_inline
*pending_template_expansions
;
50 int processing_template_decl
;
51 int processing_template_defn
;
53 #define obstack_chunk_alloc xmalloc
54 #define obstack_chunk_free free
57 static void add_pending_template ();
59 void overload_template_name (), pop_template_decls ();
61 /* We've got a template header coming up; set obstacks up to save the
62 nodes created permanently. (There might be cases with nested templates
63 where we don't have to do this, but they aren't implemented, and it
64 probably wouldn't be worth the effort.) */
66 begin_template_parm_list ()
69 push_obstacks (&permanent_obstack
, &permanent_obstack
);
73 /* Process information from new template parameter NEXT and append it to the
74 LIST being built. The rules for use of a template parameter type name
75 by later parameters are not well-defined for us just yet. However, the
76 only way to avoid having to parse expressions of unknown complexity (and
77 with tokens of unknown types) is to disallow it completely. So for now,
78 that is what is assumed. */
80 process_template_parm (list
, next
)
88 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
89 defval
= TREE_PURPOSE (parm
);
90 parm
= TREE_VALUE (parm
);
91 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
95 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
96 /* is a const-param */
97 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
99 /* A template parameter is not modifiable. */
100 TREE_READONLY (parm
) = 1;
101 if (IS_AGGR_TYPE (TREE_TYPE (parm
)))
103 sorry ("aggregate template parameter types");
104 TREE_TYPE (parm
) = void_type_node
;
106 tinfo
= make_node (TEMPLATE_CONST_PARM
);
107 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
108 if (TREE_PERMANENT (parm
) == 0)
110 parm
= copy_node (parm
);
111 TREE_PERMANENT (parm
) = 1;
113 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
114 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
115 DECL_INITIAL (decl
) = tinfo
;
116 DECL_INITIAL (parm
) = tinfo
;
120 tree t
= make_node (TEMPLATE_TYPE_PARM
);
121 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
122 TYPE_MAIN_DECL (t
) = decl
;
126 if (IDENTIFIER_HAS_TYPE_VALUE (defval
))
127 defval
= IDENTIFIER_TYPE_VALUE (defval
);
129 defval
= TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (defval
));
132 SET_DECL_ARTIFICIAL (decl
);
134 parm
= build_tree_list (defval
, parm
);
135 return chainon (list
, parm
);
138 /* The end of a template parameter list has been reached. Process the
139 tree list into a parameter vector, converting each parameter into a more
140 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
144 end_template_parm_list (parms
)
151 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
153 saved_parmlist
= make_tree_vec (nparms
);
155 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
157 tree p
= TREE_VALUE (parm
);
158 if (TREE_PURPOSE (parm
))
160 else if (saw_default
)
162 error ("if a default argument is given for one template parameter");
163 error ("default arguments must be given for all subsequent");
164 error ("parameters as well");
167 if (TREE_CODE (p
) == TYPE_DECL
)
169 tree t
= TREE_TYPE (p
);
170 TEMPLATE_TYPE_SET_INFO (t
, saved_parmlist
, nparms
);
174 tree tinfo
= DECL_INITIAL (p
);
175 DECL_INITIAL (p
) = NULL_TREE
;
176 TEMPLATE_CONST_SET_INFO (tinfo
, saved_parmlist
, nparms
);
178 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
180 set_current_level_tags_transparency (1);
181 processing_template_decl
++;
182 return saved_parmlist
;
185 /* end_template_decl is called after a template declaration is seen.
186 D1 is template header; D2 is class_head_sans_basetype or a
187 TEMPLATE_DECL with its DECL_RESULT field set. */
189 end_template_decl (d1
, d2
, is_class
, defn
)
190 tree d1
, d2
, is_class
;
194 struct template_info
*tmpl
;
196 tmpl
= (struct template_info
*) obstack_alloc (&permanent_obstack
,
197 sizeof (struct template_info
));
200 tmpl
->aggr
= is_class
;
202 /* cloned from reinit_parse_for_template */
203 tmpl
->filename
= input_filename
;
204 tmpl
->lineno
= lineno
;
205 tmpl
->parm_vec
= d1
; /* [eichin:19911015.2306EST] */
207 if (d2
== NULL_TREE
|| d2
== error_mark_node
)
215 decl
= build_lang_decl (TEMPLATE_DECL
, d2
, NULL_TREE
);
216 GNU_xref_decl (current_function_decl
, decl
);
220 if (TREE_CODE (d2
) == TEMPLATE_DECL
)
224 /* Class destructor templates and operator templates are
225 slipping past as non-template nodes. Process them here, since
226 I haven't figured out where to catch them earlier. I could
227 go do that, but it's a choice between getting that done and
228 staying only N months behind schedule. Sorry.... */
230 my_friendly_assert (TREE_CODE (d2
) == CALL_EXPR
, 263);
231 code
= TREE_CODE (TREE_OPERAND (d2
, 0));
232 my_friendly_assert (code
== BIT_NOT_EXPR
233 || code
== OP_IDENTIFIER
234 || code
== SCOPE_REF
, 264);
235 d2
= grokdeclarator (d2
, NULL_TREE
, MEMFUNCDEF
, 0, NULL_TREE
);
236 decl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (d2
),
238 DECL_TEMPLATE_RESULT (decl
) = d2
;
239 DECL_CONTEXT (decl
) = DECL_CONTEXT (d2
);
240 DECL_CLASS_CONTEXT (decl
) = DECL_CLASS_CONTEXT (d2
);
241 DECL_NAME (decl
) = DECL_NAME (d2
);
242 TREE_TYPE (decl
) = TREE_TYPE (d2
);
243 if (interface_unknown
&& flag_external_templates
244 && ! flag_alt_external_templates
245 && ! DECL_IN_SYSTEM_HEADER (decl
))
246 warn_if_unknown_interface (decl
);
247 TREE_PUBLIC (decl
) = TREE_PUBLIC (d2
) = flag_external_templates
&& !interface_unknown
;
248 DECL_EXTERNAL (decl
) = (DECL_EXTERNAL (d2
)
249 && !(DECL_CLASS_CONTEXT (d2
)
250 && !DECL_THIS_EXTERN (d2
)));
253 /* All routines creating TEMPLATE_DECL nodes should now be using
254 build_lang_decl, which will have set this up already. */
255 my_friendly_assert (DECL_LANG_SPECIFIC (decl
) != 0, 265);
257 /* @@ Somewhere, permanent allocation isn't being used. */
258 if (! DECL_TEMPLATE_IS_CLASS (decl
)
259 && TREE_CODE (DECL_TEMPLATE_RESULT (decl
)) == FUNCTION_DECL
)
261 tree result
= DECL_TEMPLATE_RESULT (decl
);
262 /* Will do nothing if allocation was already permanent. */
263 DECL_ARGUMENTS (result
) = copy_to_permanent (DECL_ARGUMENTS (result
));
266 /* If this is for a method, there's an extra binding level here. */
267 if (DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl
)) != NULL_TREE
)
269 /* @@ Find out where this should be getting set! */
270 tree r
= DECL_TEMPLATE_RESULT (decl
);
271 if (DECL_LANG_SPECIFIC (r
) && DECL_CLASS_CONTEXT (r
) == NULL_TREE
)
272 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
275 DECL_TEMPLATE_INFO (decl
) = tmpl
;
276 DECL_TEMPLATE_PARMS (decl
) = d1
;
278 /* So that duplicate_decls can do the right thing. */
280 DECL_INITIAL (decl
) = error_mark_node
;
282 /* If context of decl is non-null (i.e., method template), add it
283 to the appropriate class template, and pop the binding levels. */
284 if (! is_class
&& DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl
)) != NULL_TREE
)
286 tree ctx
= DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl
));
288 my_friendly_assert (TREE_CODE (ctx
) == UNINSTANTIATED_P_TYPE
, 266);
289 tmpl
= UPT_TEMPLATE (ctx
);
290 for (t
= DECL_TEMPLATE_MEMBERS (tmpl
); t
; t
= TREE_CHAIN (t
))
291 if (TREE_PURPOSE (t
) == DECL_NAME (decl
)
292 && duplicate_decls (decl
, TREE_VALUE (t
)))
294 DECL_TEMPLATE_MEMBERS (tmpl
) =
295 perm_tree_cons (DECL_NAME (decl
), decl
, DECL_TEMPLATE_MEMBERS (tmpl
));
300 /* Otherwise, go back to top level first, and push the template decl
309 #if 0 /* It happens sometimes, with syntactic or semantic errors.
312 template <class A, int X, int Y> class Foo { ... };
313 template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
314 Note the missing "A" in the class containing "method". */
315 my_friendly_assert (global_bindings_p (), 267);
317 while (! global_bindings_p ())
321 processing_template_decl
--;
322 (void) get_pending_sizes ();
325 /* If TYPE contains a template parm type, then substitute that type
326 with its actual type that is found in TVEC. */
328 grok_template_type (tvec
, type
)
332 switch (TREE_CODE (*type
))
334 case TEMPLATE_TYPE_PARM
:
335 if (*type
!= TYPE_MAIN_VARIANT (*type
))
337 /* we are here for cases like const T* etc. */
338 grok_template_type (tvec
, &TYPE_MAIN_VARIANT (*type
));
339 *type
= cp_build_type_variant (TYPE_MAIN_VARIANT (*type
),
340 TYPE_READONLY (*type
),
341 TYPE_VOLATILE (*type
));
344 *type
= TREE_VEC_ELT (tvec
, TEMPLATE_TYPE_IDX (*type
));
348 grok_template_type (tvec
, &TREE_TYPE (*type
));
354 /* take care of function's return type first */
355 grok_template_type (tvec
, &TREE_TYPE (*type
));
357 /* take care of function's arguments */
358 for (p
= TYPE_ARG_TYPES (*type
); p
; p
= TREE_CHAIN (p
))
359 grok_template_type (tvec
, &TREE_VALUE (p
));
368 /* Convert all template arguments to their appropriate types, and return
369 a vector containing the resulting values. If any error occurs, return
372 coerce_template_parms (parms
, arglist
, in_decl
)
376 int nparms
, nargs
, i
, lost
= 0;
379 if (arglist
== NULL_TREE
)
381 else if (TREE_CODE (arglist
) == TREE_VEC
)
382 nargs
= TREE_VEC_LENGTH (arglist
);
384 nargs
= list_length (arglist
);
386 nparms
= TREE_VEC_LENGTH (parms
);
390 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
392 error ("incorrect number of parameters (%d, should be %d)",
395 cp_error_at ("in template expansion for decl `%D'", in_decl
);
396 return error_mark_node
;
399 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
400 vec
= copy_node (arglist
);
403 vec
= make_tree_vec (nparms
);
404 for (i
= 0; i
< nparms
; i
++)
411 arglist
= TREE_CHAIN (arglist
);
413 if (arg
== error_mark_node
)
416 arg
= TREE_VALUE (arg
);
419 arg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
421 TREE_VEC_ELT (vec
, i
) = arg
;
424 for (i
= 0; i
< nparms
; i
++)
426 tree arg
= TREE_VEC_ELT (vec
, i
);
427 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
429 int is_type
, requires_type
;
431 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
432 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
433 if (is_type
!= requires_type
)
436 cp_error ("type/value mismatch in template parameter list for `%D'",
439 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
443 val
= groktypename (arg
);
444 else if (TREE_CODE (arg
) == STRING_CST
)
446 cp_error ("string literal %E is not a valid template argument", arg
);
447 error ("because it is the address of an object with static linkage");
448 val
= error_mark_node
;
452 grok_template_type (vec
, &TREE_TYPE (parm
));
453 val
= digest_init (TREE_TYPE (parm
), arg
, (tree
*) 0);
455 if (val
== error_mark_node
)
458 /* 14.2: Other template-arguments must be constant-expressions,
459 addresses of objects or functions with external linkage, or of
460 static class members. */
461 else if (!TREE_CONSTANT (val
))
463 cp_error ("non-const `%E' cannot be used as template argument",
465 val
= error_mark_node
;
467 else if (TREE_CODE (val
) == ADDR_EXPR
)
469 tree a
= TREE_OPERAND (val
, 0);
470 if ((TREE_CODE (a
) == VAR_DECL
471 || TREE_CODE (a
) == FUNCTION_DECL
)
472 && ! DECL_PUBLIC (a
))
474 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
475 val
= error_mark_node
;
480 if (val
== error_mark_node
)
483 TREE_VEC_ELT (vec
, i
) = val
;
486 return error_mark_node
;
490 /* Given class template name and parameter list, produce a user-friendly name
491 for the instantiation. */
493 mangle_class_name_for_template (name
, parms
, arglist
)
497 static struct obstack scratch_obstack
;
498 static char *scratch_firstobj
;
501 if (!scratch_firstobj
)
503 gcc_obstack_init (&scratch_obstack
);
504 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
507 obstack_free (&scratch_obstack
, scratch_firstobj
);
510 #define buflen sizeof(buf)
511 #define check if (bufp >= buf+buflen-1) goto too_long
512 #define ccat(c) *bufp++=(c); check
513 #define advance bufp+=strlen(bufp); check
514 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
517 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
519 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
524 nparms
= TREE_VEC_LENGTH (parms
);
525 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
526 for (i
= 0; i
< nparms
; i
++)
528 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
529 tree arg
= TREE_VEC_ELT (arglist
, i
);
534 if (TREE_CODE (parm
) == TYPE_DECL
)
536 cat (type_as_string (arg
, 0));
540 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
542 if (TREE_CODE (arg
) == TREE_LIST
)
544 /* New list cell was built because old chain link was in
546 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
547 arg
= TREE_VALUE (arg
);
549 /* No need to check arglist against parmlist here; we did that
550 in coerce_template_parms, called from lookup_template_class. */
551 cat (expr_as_string (arg
, 0));
554 char *bufp
= obstack_next_free (&scratch_obstack
);
556 while (bufp
[offset
- 1] == ' ')
558 obstack_blank_fast (&scratch_obstack
, offset
);
560 /* B<C<char> >, not B<C<char>> */
561 if (bufp
[offset
- 1] == '>')
566 return (char *) obstack_base (&scratch_obstack
);
571 fatal ("out of (preallocated) string space creating template instantiation name");
576 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
577 parameters, find the desired type.
579 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
580 Since ARGLIST is build on the decl_obstack, we must copy it here
581 to keep it from being reclaimed when the decl storage is reclaimed.
583 IN_DECL, if non-NULL, is the template declaration we are trying to
586 lookup_template_class (d1
, arglist
, in_decl
)
590 tree
template, parmlist
;
594 my_friendly_assert (TREE_CODE (d1
) == IDENTIFIER_NODE
, 272);
595 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
597 template = IDENTIFIER_CLASS_VALUE (d1
);
598 /* With something like `template <class T> class X class X { ... };'
599 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
600 We don't want to do that, but we have to deal with the situation, so
601 let's give them some syntax errors to chew on instead of a crash. */
603 return error_mark_node
;
604 if (TREE_CODE (template) != TEMPLATE_DECL
)
606 cp_error ("non-template type `%T' used as a template", d1
);
608 cp_error_at ("for template declaration `%D'", in_decl
);
609 return error_mark_node
;
611 parmlist
= DECL_TEMPLATE_PARMS (template);
613 arglist
= coerce_template_parms (parmlist
, arglist
, template);
614 if (arglist
== error_mark_node
)
615 return error_mark_node
;
616 if (uses_template_parms (arglist
))
618 tree t
= make_lang_type (UNINSTANTIATED_P_TYPE
);
620 id
= make_anon_name ();
621 d
= build_decl (TYPE_DECL
, id
, t
);
623 TYPE_VALUES (t
) = build_tree_list (template, arglist
);
624 pushdecl_top_level (d
);
628 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
630 id
= get_identifier (mangled_name
);
632 if (!IDENTIFIER_TEMPLATE (id
))
634 arglist
= copy_to_permanent (arglist
);
635 IDENTIFIER_TEMPLATE (id
) = perm_tree_cons (template, arglist
, NULL_TREE
);
641 push_template_decls (parmlist
, arglist
, class_level
)
642 tree parmlist
, arglist
;
647 /* Don't want to push values into global context. */
651 declare_pseudo_global_level ();
654 nparms
= TREE_VEC_LENGTH (parmlist
);
656 for (i
= 0; i
< nparms
; i
++)
658 int requires_type
, is_type
;
659 tree parm
= TREE_VALUE (TREE_VEC_ELT (parmlist
, i
));
660 tree arg
= TREE_VEC_ELT (arglist
, i
);
663 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
664 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
667 /* add typename to namespace */
670 error ("template use error: type provided where value needed");
674 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 't', 273);
675 decl
= build_decl (TYPE_DECL
, DECL_NAME (parm
), decl
);
679 /* add const decl to namespace */
683 error ("template use error: value provided where type needed");
686 val
= digest_init (TREE_TYPE (parm
), arg
, (tree
*) 0);
687 if (val
!= error_mark_node
)
689 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
691 DECL_INITIAL (decl
) = val
;
692 TREE_READONLY (decl
) = 1;
697 SET_DECL_ARTIFICIAL (decl
);
698 layout_decl (decl
, 0);
700 pushdecl_class_level (decl
);
708 pop_template_decls (parmlist
, arglist
, class_level
)
709 tree parmlist
, arglist
;
716 /* Should be defined in parse.h. */
720 uses_template_parms (t
)
725 switch (TREE_CODE (t
))
729 /* We assume that the object must be instantiated in order to build
730 the COMPONENT_REF, so we test only whether the type of the
731 COMPONENT_REF uses template parms. */
732 return uses_template_parms (TREE_TYPE (t
));
734 case IDENTIFIER_NODE
:
735 if (!IDENTIFIER_TEMPLATE (t
))
737 return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t
)));
739 /* aggregates of tree nodes */
742 int i
= TREE_VEC_LENGTH (t
);
744 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
749 if (uses_template_parms (TREE_PURPOSE (t
))
750 || uses_template_parms (TREE_VALUE (t
)))
752 return uses_template_parms (TREE_CHAIN (t
));
754 /* constructed type nodes */
757 return uses_template_parms (TREE_TYPE (t
));
759 if (TYPE_PTRMEMFUNC_FLAG (t
))
760 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
764 if (!TYPE_IDENTIFIER (t
))
766 return uses_template_parms (TYPE_IDENTIFIER (t
));
768 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
770 return uses_template_parms (TREE_TYPE (t
));
772 if (uses_template_parms (TYPE_DOMAIN (t
)))
774 return uses_template_parms (TREE_TYPE (t
));
776 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
778 return uses_template_parms (TREE_TYPE (t
));
780 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
782 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
784 return uses_template_parms (TREE_TYPE (t
));
788 return uses_template_parms (DECL_NAME (t
));
790 if (uses_template_parms (TREE_TYPE (t
)))
795 /* ??? What about FIELD_DECLs? */
796 /* The type of a decl can't use template parms if the name of the
797 variable doesn't, because it's impossible to resolve them. So
798 ignore the type field for now. */
799 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
801 if (uses_template_parms (TREE_TYPE (t
)))
803 error ("template parms used where they can't be resolved");
808 return uses_template_parms (TREE_TYPE (t
));
810 return uses_template_parms (TREE_OPERAND (t
, 0));
812 /* template parm nodes */
813 case TEMPLATE_TYPE_PARM
:
814 case TEMPLATE_CONST_PARM
:
817 /* simple type nodes */
819 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
821 return uses_template_parms (TYPE_MAX_VALUE (t
));
836 /* Non-error_mark_node ERROR_MARKs are bad things. */
837 my_friendly_assert (t
== error_mark_node
, 274);
841 case UNINSTANTIATED_P_TYPE
:
845 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
846 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
847 /* else fall through */
850 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
858 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
859 if (uses_template_parms (TREE_OPERAND (t
, i
)))
866 sorry ("testing %s for template parms",
867 tree_code_name
[(int) TREE_CODE (t
)]);
868 my_friendly_abort (82);
875 instantiate_member_templates (classname
)
880 tree members
= DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id
)));
882 for (t
= members
; t
; t
= TREE_CHAIN (t
))
884 tree parmvec
, type
, classparms
, tdecl
, t2
;
885 int nparms
, xxx
= 0, i
;
887 my_friendly_assert (TREE_VALUE (t
) != NULL_TREE
, 275);
888 my_friendly_assert (TREE_CODE (TREE_VALUE (t
)) == TEMPLATE_DECL
, 276);
889 /* @@ Should verify that class parm list is a list of
890 distinct template parameters, and covers all the template
892 tdecl
= TREE_VALUE (t
);
893 type
= DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl
));
894 classparms
= UPT_PARMS (type
);
895 nparms
= TREE_VEC_LENGTH (classparms
);
896 parmvec
= make_tree_vec (nparms
);
897 for (i
= 0; i
< nparms
; i
++)
898 TREE_VEC_ELT (parmvec
, i
) = NULL_TREE
;
899 switch (unify (DECL_TEMPLATE_PARMS (tdecl
),
900 &TREE_VEC_ELT (parmvec
, 0), nparms
,
901 type
, IDENTIFIER_TYPE_VALUE (classname
),
905 /* Success -- well, no inconsistency, at least. */
906 for (i
= 0; i
< nparms
; i
++)
907 if (TREE_VEC_ELT (parmvec
, i
) == NULL_TREE
)
909 t2
= instantiate_template (tdecl
,
910 &TREE_VEC_ELT (parmvec
, 0));
911 type
= IDENTIFIER_TYPE_VALUE (id
);
912 my_friendly_assert (type
!= 0, 277);
917 cp_error_at ("type unification error instantiating `%D'", tdecl
);
918 cp_error ("while instantiating members of `%T'", classname
);
920 continue /* loop of members */;
923 my_friendly_abort (83);
928 static struct tinst_level
*current_tinst_level
= 0;
929 static struct tinst_level
*free_tinst_level
= 0;
930 static int tinst_depth
= 0;
931 int max_tinst_depth
= 17;
934 push_tinst_level (name
)
937 struct tinst_level
*new;
938 tree global
= IDENTIFIER_GLOBAL_VALUE (name
);
940 if (tinst_depth
>= max_tinst_depth
)
942 error ("template instantiation depth exceeds maximum of %d",
944 cp_error (" instantiating `%D'", name
);
948 if (free_tinst_level
)
950 new = free_tinst_level
;
951 free_tinst_level
= new->next
;
954 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
956 new->classname
= name
;
959 new->line
= DECL_SOURCE_LINE (global
);
960 new->file
= DECL_SOURCE_FILE (global
);
965 new->file
= input_filename
;
967 new->next
= current_tinst_level
;
968 current_tinst_level
= new;
976 struct tinst_level
*old
= current_tinst_level
;
978 current_tinst_level
= old
->next
;
979 old
->next
= free_tinst_level
;
980 free_tinst_level
= old
;
987 struct tinst_level
*p
= current_tinst_level
;
990 for (; p
->next
; p
= p
->next
)
996 instantiate_class_template (classname
, setup_parse
)
1000 struct template_info
*template_info
;
1003 if (classname
== error_mark_node
)
1004 return error_mark_node
;
1006 my_friendly_assert (TREE_CODE (classname
) == IDENTIFIER_NODE
, 278);
1007 template = IDENTIFIER_TEMPLATE (classname
);
1009 if (IDENTIFIER_HAS_TYPE_VALUE (classname
))
1011 tree type
= IDENTIFIER_TYPE_VALUE (classname
);
1012 if (TREE_CODE (type
) == UNINSTANTIATED_P_TYPE
)
1014 if (TYPE_BEING_DEFINED (type
)
1016 || CLASSTYPE_USE_TEMPLATE (type
) != 0)
1020 /* If IDENTIFIER_LOCAL_VALUE is already set on this template classname
1021 (it's something like `foo<int>'), that means we're already working on
1022 the instantiation for it. Normally, a classname comes in with nothing
1023 but its IDENTIFIER_TEMPLATE slot set. If we were to try to instantiate
1024 this again, we'd get a redeclaration error. Since we're already working
1025 on it, we'll pass back this classname's TYPE_DECL (it's the value of
1026 the classname's IDENTIFIER_LOCAL_VALUE). Only do this if we're setting
1027 things up for the parser, though---if we're just trying to instantiate
1028 it (e.g., via tsubst) we can trip up cuz it may not have an
1029 IDENTIFIER_TYPE_VALUE when it will need one. */
1030 if (setup_parse
&& IDENTIFIER_LOCAL_VALUE (classname
))
1031 return IDENTIFIER_LOCAL_VALUE (classname
);
1033 if (uses_template_parms (classname
))
1035 if (!TREE_TYPE (classname
))
1037 tree t
= make_lang_type (RECORD_TYPE
);
1038 tree d
= build_decl (TYPE_DECL
, classname
, t
);
1039 DECL_NAME (d
) = classname
;
1046 t1
= TREE_PURPOSE (template);
1047 my_friendly_assert (TREE_CODE (t1
) == TEMPLATE_DECL
, 279);
1049 /* If a template is declared but not defined, accept it; don't crash.
1050 Later uses requiring the definition will be flagged as errors by
1051 other code. Thanks to niklas@appli.se for this bug fix. */
1052 if (DECL_TEMPLATE_INFO (t1
)->text
== 0)
1055 push_to_top_level ();
1056 template_info
= DECL_TEMPLATE_INFO (t1
);
1057 if (setup_parse
&& push_tinst_level (classname
))
1059 push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
1060 TREE_VALUE (template), 0);
1061 set_current_level_tags_transparency (1);
1062 feed_input (template_info
->text
, template_info
->length
, (struct obstack
*)0);
1063 lineno
= template_info
->lineno
;
1064 input_filename
= template_info
->filename
;
1065 /* Get interface/implementation back in sync. */
1066 extract_interface_info ();
1067 overload_template_name (classname
, 0);
1068 /* Kludge so that we don't get screwed by our own base classes. */
1069 TYPE_BEING_DEFINED (TREE_TYPE (classname
)) = 1;
1070 yychar
= PRE_PARSED_CLASS_DECL
;
1071 yylval
.ttype
= classname
;
1072 processing_template_defn
++;
1073 if (!flag_external_templates
)
1074 interface_unknown
++;
1078 tree t
, decl
, id
, tmpl
;
1081 tmpl
= TREE_PURPOSE (IDENTIFIER_TEMPLATE (id
));
1082 t
= xref_tag (DECL_TEMPLATE_INFO (tmpl
)->aggr
, id
, NULL_TREE
, 0);
1083 my_friendly_assert (TREE_CODE (t
) == RECORD_TYPE
1084 || TREE_CODE (t
) == UNION_TYPE
, 280);
1086 /* Now, put a copy of the decl in global scope, to avoid
1087 * recursive expansion. */
1088 decl
= IDENTIFIER_LOCAL_VALUE (id
);
1090 decl
= IDENTIFIER_CLASS_VALUE (id
);
1093 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 281);
1094 /* We'd better make sure we're on the permanent obstack or else
1095 * we'll get a "friendly" abort 124 in pushdecl. Perhaps a
1096 * copy_to_permanent would be sufficient here, but then a
1097 * sharing problem might occur. I don't know -- niklas@appli.se */
1098 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1099 pushdecl_top_level (copy_node (decl
));
1102 pop_from_top_level ();
1112 if (t1
== NULL_TREE
)
1113 return t2
== NULL_TREE
;
1114 if (t2
== NULL_TREE
)
1116 /* Don't care if one declares its arg const and the other doesn't -- the
1117 main variant of the arg type is all that matters. */
1118 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
1119 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
1121 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1125 lookup_nested_type_by_name (ctype
, name
)
1130 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1132 if (name
== TREE_PURPOSE (t
))
1133 return TREE_VALUE (t
);
1139 search_nested_type_in_tmpl (tmpl
, type
)
1144 if (tmpl
== NULL
|| TYPE_CONTEXT(type
) == NULL
)
1146 t
= search_nested_type_in_tmpl (tmpl
, TYPE_CONTEXT(type
));
1147 if (t
== NULL
) return t
;
1148 t
= lookup_nested_type_by_name(t
, DECL_NAME(TYPE_NAME(type
)));
1153 tsubst (t
, args
, nargs
, in_decl
)
1160 if (t
== NULL_TREE
|| t
== error_mark_node
)
1163 type
= TREE_TYPE (t
);
1165 /* Minor optimization.
1166 ?? Are these really the most frequent cases? Is the savings
1168 && type
!= integer_type_node
1169 && type
!= void_type_node
1170 && type
!= char_type_node
)
1171 type
= tsubst (type
, args
, nargs
, in_decl
);
1173 switch (TREE_CODE (t
))
1176 if (TYPE_PTRMEMFUNC_P (t
))
1177 return build_ptrmemfunc_type
1178 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1180 /* else fall through */
1183 case IDENTIFIER_NODE
:
1196 if (t
== integer_type_node
)
1199 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1200 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
1202 return build_index_2_type
1203 (tsubst (TYPE_MIN_VALUE (t
), args
, nargs
, in_decl
),
1204 tsubst (TYPE_MAX_VALUE (t
), args
, nargs
, in_decl
));
1206 case TEMPLATE_TYPE_PARM
:
1208 tree arg
= args
[TEMPLATE_TYPE_IDX (t
)];
1209 return cp_build_type_variant
1210 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
1211 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
1214 case TEMPLATE_CONST_PARM
:
1215 return args
[TEMPLATE_CONST_IDX (t
)];
1220 tree fnargs
, result
;
1222 if (type
== TREE_TYPE (t
)
1223 && (DECL_CONTEXT (t
) == NULL_TREE
1224 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) != 't'))
1226 fnargs
= tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1227 result
= tsubst (DECL_RESULT (t
), args
, nargs
, t
);
1228 if (DECL_CONTEXT (t
) != NULL_TREE
1229 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
1231 /* Look it up in that class, and return the decl node there,
1232 instead of creating a new one. */
1233 tree ctx
, methods
, name
, method
;
1237 name
= DECL_NAME (t
);
1238 ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
1239 methods
= CLASSTYPE_METHOD_VEC (ctx
);
1240 if (methods
== NULL_TREE
)
1241 /* No methods at all -- no way this one can match. */
1243 n_methods
= TREE_VEC_LENGTH (methods
);
1247 if (!strncmp (OPERATOR_TYPENAME_FORMAT
,
1248 IDENTIFIER_POINTER (name
),
1249 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
1251 /* Type-conversion operator. Reconstruct the name, in
1252 case it's the name of one of the template's parameters. */
1253 name
= build_typename_overload (TREE_TYPE (type
));
1256 if (DECL_CONTEXT (t
) != NULL_TREE
1257 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't'
1258 && constructor_name (DECL_CONTEXT (t
)) == DECL_NAME (t
))
1259 name
= constructor_name (ctx
);
1261 if (DECL_CONSTRUCTOR_P (t
) && TYPE_USES_VIRTUAL_BASECLASSES (ctx
))
1263 /* Since we didn't know that this class had virtual bases until after
1264 we instantiated it, we have to recreate the arguments to this
1265 constructor, as otherwise it would miss the __in_chrg parameter. */
1267 tree parms
= TREE_CHAIN (TYPE_ARG_TYPES (type
));
1268 parms
= hash_tree_chain (integer_type_node
, parms
);
1269 newtype
= build_cplus_method_type (ctx
,
1272 newtype
= build_type_variant (newtype
,
1273 TYPE_READONLY (type
),
1274 TYPE_VOLATILE (type
));
1277 fnargs
= copy_node (DECL_ARGUMENTS (t
));
1278 TREE_CHAIN (fnargs
) = TREE_CHAIN (DECL_ARGUMENTS (t
));
1280 /* In this case we need "in-charge" flag saying whether
1281 this constructor is responsible for initialization
1282 of virtual baseclasses or not. */
1283 parm
= build_decl (PARM_DECL
, in_charge_identifier
, integer_type_node
);
1284 /* Mark the artificial `__in_chrg' parameter as "artificial". */
1285 SET_DECL_ARTIFICIAL (parm
);
1286 DECL_ARG_TYPE (parm
) = integer_type_node
;
1287 DECL_REGISTER (parm
) = 1;
1288 TREE_CHAIN (parm
) = TREE_CHAIN (fnargs
);
1289 TREE_CHAIN (fnargs
) = parm
;
1291 fnargs
= tsubst (fnargs
, args
, nargs
, t
);
1294 fprintf (stderr
, "\nfor function %s in class %s:\n",
1295 IDENTIFIER_POINTER (name
),
1296 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx
)));
1298 for (i
= 0; i
< n_methods
; i
++)
1302 method
= TREE_VEC_ELT (methods
, i
);
1303 if (method
== NULL_TREE
|| DECL_NAME (method
) != name
)
1308 for (; method
; method
= DECL_CHAIN (method
))
1310 my_friendly_assert (TREE_CODE (method
) == FUNCTION_DECL
,
1312 if (! comptypes (type
, TREE_TYPE (method
), 1))
1314 tree mtype
= TREE_TYPE (method
);
1317 /* Keep looking for a method that matches
1318 perfectly. This takes care of the problem
1319 where destructors (which have implicit int args)
1320 look like constructors which have an int arg. */
1324 t1
= TYPE_ARG_TYPES (mtype
);
1325 t2
= TYPE_ARG_TYPES (type
);
1326 if (TREE_CODE (mtype
) == FUNCTION_TYPE
)
1327 t2
= TREE_CHAIN (t2
);
1329 if (list_eq (t1
, t2
))
1331 if (TREE_CODE (mtype
) == FUNCTION_TYPE
)
1334 newtype
= build_function_type (TREE_TYPE (type
),
1335 TYPE_ARG_TYPES (type
));
1336 newtype
= build_type_variant (newtype
,
1337 TYPE_READONLY (type
),
1338 TYPE_VOLATILE (type
));
1340 if (TREE_TYPE (type
) != TREE_TYPE (mtype
))
1341 goto maybe_bad_return_type
;
1343 else if (TYPE_METHOD_BASETYPE (mtype
)
1344 == TYPE_METHOD_BASETYPE (type
))
1346 /* Types didn't match, but arg types and
1347 `this' do match, so the return type is
1348 all that should be messing it up. */
1349 maybe_bad_return_type
:
1350 if (TREE_TYPE (type
) != TREE_TYPE (mtype
))
1351 error ("inconsistent return types for method `%s' in class `%s'",
1352 IDENTIFIER_POINTER (name
),
1353 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx
)));
1362 fprintf (stderr
, "\tfound %s\n\n",
1363 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method
)));
1365 if (DECL_ARTIFICIAL (method
))
1367 cp_error ("template for method `%D' which has default implementation in class `%T'", name
, ctx
);
1369 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl
);
1370 return error_mark_node
;
1373 if (DECL_ARGUMENTS (method
)
1374 && ! TREE_PERMANENT (DECL_ARGUMENTS (method
)))
1375 /* @@ Is this early enough? Might we want to do
1376 this instead while processing the expansion? */
1377 DECL_ARGUMENTS (method
)
1378 = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1382 if (r
== NULL_TREE
&& pass
== 0)
1385 method
= TREE_VEC_ELT (methods
, i
);
1394 ? "template for method `%D' doesn't match any in class `%T'"
1395 : "method `%D' not found in class `%T'", name
, ctx
);
1397 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl
);
1398 return error_mark_node
;
1408 decls
= lookup_name_nonclass (r
);
1409 if (decls
== NULL_TREE
)
1411 else if (TREE_CODE (decls
) == TREE_LIST
)
1412 for (decls
= TREE_VALUE (decls
); decls
;
1413 decls
= DECL_CHAIN (decls
))
1415 if (TREE_CODE (decls
) == FUNCTION_DECL
1416 && TREE_TYPE (decls
) == type
)
1427 if (TREE_CODE (val
) == FUNCTION_DECL
1428 && TREE_TYPE (val
) == type
)
1437 tree a
= build_decl_overload (r
, TYPE_VALUES (type
),
1438 DECL_CONTEXT (t
) != NULL_TREE
);
1439 r
= build_lang_decl (FUNCTION_DECL
, r
, type
);
1440 DECL_ASSEMBLER_NAME (r
) = a
;
1442 else if (TREE_STATIC (r
))
1444 /* This overrides the template version, use it. */
1449 TREE_PUBLIC (r
) = 1;
1450 DECL_EXTERNAL (r
) = 1;
1451 TREE_STATIC (r
) = 0;
1452 DECL_INTERFACE_KNOWN (r
) = 0;
1453 DECL_INLINE (r
) = DECL_INLINE (t
);
1454 DECL_THIS_INLINE (r
) = DECL_THIS_INLINE (t
);
1456 #if 0 /* Maybe later. -jason */
1457 struct tinst_level
*til
= tinst_for_decl();
1459 /* should always be true under new approach */
1462 DECL_SOURCE_FILE (r
) = til
->file
;
1463 DECL_SOURCE_LINE (r
) = til
->line
;
1468 DECL_SOURCE_FILE (r
) = DECL_SOURCE_FILE (t
);
1469 DECL_SOURCE_LINE (r
) = DECL_SOURCE_LINE (t
);
1472 DECL_CLASS_CONTEXT (r
) = tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1473 make_decl_rtl (r
, NULL_PTR
, 1);
1474 DECL_ARGUMENTS (r
) = fnargs
;
1475 DECL_RESULT (r
) = result
;
1477 if (DECL_CONTEXT (t
) == NULL_TREE
1478 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) != 't')
1479 push_overloaded_decl_top_level (r
, 0);
1487 r
= build_decl (PARM_DECL
, DECL_NAME (t
), type
);
1488 DECL_INITIAL (r
) = TREE_TYPE (r
);
1490 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1496 tree purpose
, value
, chain
, result
;
1497 int via_public
, via_virtual
, via_protected
;
1499 if (t
== void_list_node
)
1502 via_public
= TREE_VIA_PUBLIC (t
);
1503 via_protected
= TREE_VIA_PROTECTED (t
);
1504 via_virtual
= TREE_VIA_VIRTUAL (t
);
1506 purpose
= TREE_PURPOSE (t
);
1508 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
1509 value
= TREE_VALUE (t
);
1511 value
= tsubst (value
, args
, nargs
, in_decl
);
1512 chain
= TREE_CHAIN (t
);
1513 if (chain
&& chain
!= void_type_node
)
1514 chain
= tsubst (chain
, args
, nargs
, in_decl
);
1515 if (purpose
== TREE_PURPOSE (t
)
1516 && value
== TREE_VALUE (t
)
1517 && chain
== TREE_CHAIN (t
))
1519 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
1520 purpose
, value
, chain
);
1521 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
1526 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
1527 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
1528 bzero ((char *) elts
, len
* sizeof (tree
));
1530 for (i
= 0; i
< len
; i
++)
1532 elts
[i
] = tsubst (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
1533 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
1540 t
= make_tree_vec (len
);
1541 for (i
= 0; i
< len
; i
++)
1542 TREE_VEC_ELT (t
, i
) = elts
[i
];
1546 case REFERENCE_TYPE
:
1549 enum tree_code code
;
1550 if (type
== TREE_TYPE (t
))
1553 code
= TREE_CODE (t
);
1554 if (code
== POINTER_TYPE
)
1555 r
= build_pointer_type (type
);
1557 r
= build_reference_type (type
);
1558 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1559 /* Will this ever be needed for TYPE_..._TO values? */
1564 return build_offset_type
1565 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
1569 tree values
= TYPE_VALUES (t
); /* same as TYPE_ARG_TYPES */
1570 tree context
= TYPE_CONTEXT (t
);
1573 /* Don't bother recursing if we know it won't change anything. */
1574 if (values
!= void_list_node
)
1575 values
= tsubst (values
, args
, nargs
, in_decl
);
1577 context
= tsubst (context
, args
, nargs
, in_decl
);
1578 /* Could also optimize cases where return value and
1579 values have common elements (e.g., T min(const &T, const T&). */
1581 /* If the above parameters haven't changed, just return the type. */
1582 if (type
== TREE_TYPE (t
)
1583 && values
== TYPE_VALUES (t
)
1584 && context
== TYPE_CONTEXT (t
))
1587 /* Construct a new type node and return it. */
1588 if (TREE_CODE (t
) == FUNCTION_TYPE
1589 && context
== NULL_TREE
)
1591 new_value
= build_function_type (type
, values
);
1593 else if (context
== NULL_TREE
)
1595 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
1596 args
, nargs
, in_decl
);
1597 new_value
= build_cplus_method_type (base
, type
,
1598 TREE_CHAIN (values
));
1602 new_value
= make_node (TREE_CODE (t
));
1603 TREE_TYPE (new_value
) = type
;
1604 TYPE_CONTEXT (new_value
) = context
;
1605 TYPE_VALUES (new_value
) = values
;
1606 TYPE_SIZE (new_value
) = TYPE_SIZE (t
);
1607 TYPE_ALIGN (new_value
) = TYPE_ALIGN (t
);
1608 TYPE_MODE (new_value
) = TYPE_MODE (t
);
1609 if (TYPE_METHOD_BASETYPE (t
))
1610 TYPE_METHOD_BASETYPE (new_value
) = tsubst (TYPE_METHOD_BASETYPE (t
),
1611 args
, nargs
, in_decl
);
1612 /* Need to generate hash value. */
1613 my_friendly_abort (84);
1615 new_value
= build_type_variant (new_value
,
1622 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
1624 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
1626 r
= build_cplus_array_type (type
, domain
);
1630 case UNINSTANTIATED_P_TYPE
:
1632 int nparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t
)));
1633 tree argvec
= make_tree_vec (nparms
);
1634 tree parmvec
= UPT_PARMS (t
);
1637 for (i
= 0; i
< nparms
; i
++)
1638 TREE_VEC_ELT (argvec
, i
) = tsubst (TREE_VEC_ELT (parmvec
, i
),
1639 args
, nargs
, in_decl
);
1640 id
= lookup_template_class (DECL_NAME (UPT_TEMPLATE (t
)), argvec
, NULL_TREE
);
1641 if (! IDENTIFIER_HAS_TYPE_VALUE (id
)) {
1642 instantiate_class_template(id
, 0);
1643 /* set up pending_classes */
1644 add_pending_template (id
);
1646 TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id
)) =
1647 IDENTIFIER_TYPE_VALUE (id
);
1649 rt
= IDENTIFIER_TYPE_VALUE (id
);
1651 /* kung: this part handles nested type in template definition */
1653 if ( !ANON_AGGRNAME_P (DECL_NAME(TYPE_NAME(t
))))
1655 rt
= search_nested_type_in_tmpl (rt
, t
);
1658 return build_type_variant (rt
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1663 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1664 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1665 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
1669 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
1670 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
1673 sorry ("use of `%s' in function template",
1674 tree_code_name
[(int) TREE_CODE (t
)]);
1675 return error_mark_node
;
1680 instantiate_template (tmpl
, targ_ptr
)
1681 tree tmpl
, *targ_ptr
;
1685 struct pending_inline
*p
;
1686 struct template_info
*t
;
1687 struct obstack
*old_fmp_obstack
;
1688 extern struct obstack
*function_maybepermanent_obstack
;
1690 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1691 old_fmp_obstack
= function_maybepermanent_obstack
;
1692 function_maybepermanent_obstack
= &permanent_obstack
;
1694 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
1695 len
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
1699 targ_ptr
[i
] = copy_to_permanent (targ_ptr
[i
]);
1701 for (fndecl
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1702 fndecl
; fndecl
= TREE_CHAIN (fndecl
))
1704 tree
*t1
= &TREE_VEC_ELT (TREE_PURPOSE (fndecl
), 0);
1705 for (i
= len
- 1; i
>= 0; i
--)
1706 if (simple_cst_equal (t1
[i
], targ_ptr
[i
]) <= 0)
1709 /* Here, we have a match. */
1710 fndecl
= TREE_VALUE (fndecl
);
1717 targs
= make_tree_vec (len
);
1720 TREE_VEC_ELT (targs
, i
) = targ_ptr
[i
];
1722 /* substitute template parameters */
1723 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
,
1724 TREE_VEC_LENGTH (targs
), tmpl
);
1726 if (fndecl
== error_mark_node
)
1729 assemble_external (fndecl
);
1731 /* If it's a static member fn in the template, we need to change it
1732 into a FUNCTION_TYPE and chop off its this pointer. */
1733 if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl
))) == METHOD_TYPE
1734 && DECL_STATIC_FUNCTION_P (fndecl
))
1736 tree olddecl
= DECL_RESULT (tmpl
);
1737 revert_static_member_fn (&DECL_RESULT (tmpl
), NULL
, NULL
);
1738 /* Chop off the this pointer that grokclassfn so kindly added
1739 for us (it didn't know yet if the fn was static or not). */
1740 DECL_ARGUMENTS (olddecl
) = TREE_CHAIN (DECL_ARGUMENTS (olddecl
));
1741 DECL_ARGUMENTS (fndecl
) = TREE_CHAIN (DECL_ARGUMENTS (fndecl
));
1744 t
= DECL_TEMPLATE_INFO (tmpl
);
1746 /* If we have a preexisting version of this function, don't expand
1747 the template version, use the other instead. */
1748 if (TREE_STATIC (fndecl
))
1750 SET_DECL_TEMPLATE_SPECIALIZATION (fndecl
);
1751 p
= (struct pending_inline
*)0;
1755 SET_DECL_IMPLICIT_INSTANTIATION (fndecl
);
1756 repo_template_used (fndecl
);
1757 p
= (struct pending_inline
*) permalloc (sizeof (struct pending_inline
));
1758 p
->parm_vec
= t
->parm_vec
;
1759 p
->bindings
= targs
;
1767 char * f
= input_filename
;
1769 lineno
= p
->lineno
= t
->lineno
;
1770 input_filename
= p
->filename
= t
->filename
;
1772 extract_interface_info ();
1774 if (interface_unknown
&& flag_external_templates
)
1776 if (DECL_CLASS_CONTEXT (fndecl
)
1777 && CLASSTYPE_INTERFACE_KNOWN (DECL_CLASS_CONTEXT (fndecl
)))
1779 interface_unknown
= 0;
1781 = CLASSTYPE_INTERFACE_ONLY (DECL_CLASS_CONTEXT (fndecl
));
1783 else if (! DECL_IN_SYSTEM_HEADER (tmpl
))
1784 warn_if_unknown_interface (tmpl
);
1787 if (interface_unknown
|| ! flag_external_templates
)
1788 p
->interface
= 1; /* unknown */
1790 p
->interface
= interface_only
? 0 : 2;
1795 extract_interface_info ();
1799 p
= (struct pending_inline
*)0;
1801 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) =
1802 tree_cons (targs
, fndecl
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1804 if (p
== (struct pending_inline
*)0)
1808 else if (DECL_INLINE (fndecl
))
1810 DECL_PENDING_INLINE_INFO (fndecl
) = p
;
1811 p
->next
= pending_inlines
;
1812 pending_inlines
= p
;
1816 p
->next
= pending_template_expansions
;
1817 pending_template_expansions
= p
;
1820 function_maybepermanent_obstack
= old_fmp_obstack
;
1826 /* classlevel should now never be true. jason 4/12/94 */
1828 undo_template_name_overload (id
, classlevel
)
1834 template = IDENTIFIER_TEMPLATE (id
);
1838 #if 0 /* not yet, should get fixed properly later */
1842 /* This was a botch... See `overload_template_name' just below. */
1848 /* classlevel should now never be true. jason 4/12/94 */
1850 overload_template_name (id
, classlevel
)
1854 tree
template, t
, decl
;
1855 struct template_info
*tinfo
;
1857 my_friendly_assert (TREE_CODE (id
) == IDENTIFIER_NODE
, 284);
1858 template = IDENTIFIER_TEMPLATE (id
);
1862 template = TREE_PURPOSE (template);
1863 tinfo
= DECL_TEMPLATE_INFO (template);
1864 template = DECL_NAME (template);
1865 my_friendly_assert (template != NULL_TREE
, 285);
1868 /* This was a botch... names of templates do not get their own private
1869 scopes. Rather, they should go into the binding level already created
1870 by push_template_decls. Except that there isn't one of those for
1875 declare_pseudo_global_level ();
1879 t
= xref_tag (tinfo
->aggr
, id
, NULL_TREE
, 1);
1880 my_friendly_assert (TREE_CODE (t
) == RECORD_TYPE
1881 || TREE_CODE (t
) == UNION_TYPE
1882 || TREE_CODE (t
) == UNINSTANTIATED_P_TYPE
, 286);
1884 decl
= build_decl (TYPE_DECL
, template, t
);
1885 SET_DECL_ARTIFICIAL (decl
);
1887 #if 0 /* fix this later */
1888 /* We don't want to call here if the work has already been done. */
1890 ? IDENTIFIER_CLASS_VALUE (template)
1891 : IDENTIFIER_LOCAL_VALUE (template));
1893 && TREE_CODE (t
) == TYPE_DECL
1894 && TREE_TYPE (t
) == t
)
1895 my_friendly_abort (85);
1899 pushdecl_class_level (decl
);
1903 #if 0 /* This seems bogus to me; if it isn't, explain why. (jason) */
1904 /* Fake this for now, just to make dwarfout.c happy. It will have to
1905 be done in a proper way later on. */
1906 DECL_CONTEXT (decl
) = t
;
1910 /* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
1912 end_template_instantiation (name
)
1915 extern struct pending_input
*to_be_restored
;
1918 processing_template_defn
--;
1919 if (!flag_external_templates
)
1920 interface_unknown
--;
1922 /* Restore the old parser input state. */
1923 if (yychar
== YYEMPTY
)
1925 if (yychar
!= END_OF_SAVED_INPUT
)
1926 error ("parse error at end of class template");
1929 restore_pending_input (to_be_restored
);
1933 /* Our declarations didn't get stored in the global slot, since
1934 there was a (supposedly tags-transparent) scope in between. */
1935 t
= IDENTIFIER_TYPE_VALUE (name
);
1936 my_friendly_assert (t
!= NULL_TREE
1937 && TREE_CODE_CLASS (TREE_CODE (t
)) == 't',
1939 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
1940 /* Make methods of template classes static, unless
1941 -fexternal-templates is given. */
1942 if (!flag_external_templates
)
1943 SET_CLASSTYPE_INTERFACE_UNKNOWN (t
);
1944 decl
= IDENTIFIER_GLOBAL_VALUE (name
);
1945 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 288);
1947 undo_template_name_overload (name
, 0);
1948 t
= IDENTIFIER_TEMPLATE (name
);
1949 pop_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (t
)), TREE_VALUE (t
),
1951 /* This will fix up the type-value field. */
1953 pop_from_top_level ();
1955 #ifdef DWARF_DEBUGGING_INFO
1956 if (write_symbols
== DWARF_DEBUG
&& TREE_CODE (decl
) == TYPE_DECL
)
1958 /* We just completed the definition of a new file-scope type,
1959 so we can go ahead and output debug-info for it now. */
1960 TYPE_STUB_DECL (TREE_TYPE (decl
)) = decl
;
1961 rest_of_type_compilation (TREE_TYPE (decl
), 1);
1963 #endif /* DWARF_DEBUGGING_INFO */
1965 /* Restore interface/implementation settings. */
1966 extract_interface_info ();
1969 /* Store away the text of an template. */
1972 reinit_parse_for_template (yychar
, d1
, d2
)
1976 struct template_info
*template_info
;
1977 extern struct obstack inline_text_obstack
; /* see comment in lex.c */
1979 if (d2
== NULL_TREE
|| d2
== error_mark_node
)
1982 /* @@ Should use temp obstack, and discard results. */
1983 reinit_parse_for_block (yychar
, &inline_text_obstack
, 1);
1987 if (TREE_CODE (d2
) == IDENTIFIER_NODE
)
1988 d2
= IDENTIFIER_GLOBAL_VALUE (d2
);
1991 template_info
= DECL_TEMPLATE_INFO (d2
);
1994 template_info
= (struct template_info
*) permalloc (sizeof (struct template_info
));
1995 bzero ((char *) template_info
, sizeof (struct template_info
));
1996 DECL_TEMPLATE_INFO (d2
) = template_info
;
1998 template_info
->filename
= input_filename
;
1999 template_info
->lineno
= lineno
;
2000 reinit_parse_for_block (yychar
, &inline_text_obstack
, 1);
2001 template_info
->text
= obstack_base (&inline_text_obstack
);
2002 template_info
->length
= obstack_object_size (&inline_text_obstack
);
2003 obstack_finish (&inline_text_obstack
);
2004 template_info
->parm_vec
= d1
;
2007 /* Type unification.
2009 We have a function template signature with one or more references to
2010 template parameters, and a parameter list we wish to fit to this
2011 template. If possible, produce a list of parameters for the template
2012 which will cause it to fit the supplied parameter list.
2014 Return zero for success, 2 for an incomplete match that doesn't resolve
2015 all the types, and 1 for complete failure. An error message will be
2016 printed only for an incomplete match.
2018 TPARMS[NTPARMS] is an array of template parameter types;
2019 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2020 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2021 and ARGS is the argument list we're trying to match against it.
2023 If SUBR is 1, we're being called recursively (to unify the arguments of
2024 a function or method parameter of a function template), so don't zero
2025 out targs and don't fail on an incomplete match. */
2028 type_unification (tparms
, targs
, parms
, args
, nsubsts
, subr
)
2029 tree tparms
, *targs
, parms
, args
;
2034 int ntparms
= TREE_VEC_LENGTH (tparms
);
2036 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
2037 my_friendly_assert (TREE_CODE (parms
) == TREE_LIST
, 290);
2038 /* ARGS could be NULL (via a call from parse.y to
2039 build_x_function_call). */
2041 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
2042 my_friendly_assert (ntparms
> 0, 292);
2045 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
2048 && parms
!= void_list_node
2050 && args
!= void_list_node
)
2052 parm
= TREE_VALUE (parms
);
2053 parms
= TREE_CHAIN (parms
);
2054 arg
= TREE_VALUE (args
);
2055 args
= TREE_CHAIN (args
);
2057 if (arg
== error_mark_node
)
2059 if (arg
== unknown_type_node
)
2062 if (! uses_template_parms (parm
)
2063 && TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2065 if (can_convert_arg (parm
, TREE_TYPE (arg
), arg
))
2071 if (TREE_CODE (arg
) == VAR_DECL
)
2072 arg
= TREE_TYPE (arg
);
2073 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
2074 arg
= TREE_TYPE (arg
);
2076 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2078 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
2079 if (TREE_CODE (arg
) == TREE_LIST
2080 && TREE_TYPE (arg
) == unknown_type_node
2081 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
2083 int nsubsts
, ntparms
;
2086 /* Have to back unify here */
2087 arg
= TREE_VALUE (arg
);
2089 ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg
));
2090 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2091 parm
= tree_cons (NULL_TREE
, parm
, NULL_TREE
);
2092 return type_unification (DECL_TEMPLATE_PARMS (arg
), targs
,
2093 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
2096 arg
= TREE_TYPE (arg
);
2099 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2100 arg
= TREE_TYPE (arg
);
2102 if (TREE_CODE (parm
) != REFERENCE_TYPE
)
2104 if (TREE_CODE (arg
) == FUNCTION_TYPE
2105 || TREE_CODE (arg
) == METHOD_TYPE
)
2106 arg
= build_pointer_type (arg
);
2107 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
2108 arg
= build_pointer_type (TREE_TYPE (arg
));
2110 arg
= TYPE_MAIN_VARIANT (arg
);
2113 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
))
2121 /* Fail if we've reached the end of the parm list, and more args
2122 are present, and the parm list isn't variadic. */
2123 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
2125 /* Fail if parms are left and they don't have default values. */
2127 && parms
!= void_list_node
2128 && TREE_PURPOSE (parms
) == NULL_TREE
)
2131 for (i
= 0; i
< ntparms
; i
++)
2134 error ("incomplete type unification");
2140 /* Tail recursion is your friend. */
2142 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
)
2143 tree tparms
, *targs
, parm
, arg
;
2144 int *nsubsts
, ntparms
;
2148 /* I don't think this will do the right thing with respect to types.
2149 But the only case I've seen it in so far has been array bounds, where
2150 signedness is the only information lost, and I think that will be
2152 while (TREE_CODE (parm
) == NOP_EXPR
)
2153 parm
= TREE_OPERAND (parm
, 0);
2155 if (arg
== error_mark_node
)
2157 if (arg
== unknown_type_node
)
2162 switch (TREE_CODE (parm
))
2164 case TEMPLATE_TYPE_PARM
:
2166 if (TEMPLATE_TYPE_TPARMLIST (parm
) != tparms
)
2168 error ("mixed template headers?!");
2169 my_friendly_abort (86);
2172 idx
= TEMPLATE_TYPE_IDX (parm
);
2174 /* Template type parameters cannot contain cv-quals; i.e.
2175 template <class T> void f (T& a, T& b) will not generate
2176 void f (const int& a, const int& b). */
2177 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
2178 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
2180 arg
= TYPE_MAIN_VARIANT (arg
);
2183 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
2184 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
2185 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
2188 /* Simple cases: Value already set, does match or doesn't. */
2189 if (targs
[idx
] == arg
)
2191 else if (targs
[idx
])
2193 /* Check for mixed types and values. */
2194 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
2198 case TEMPLATE_CONST_PARM
:
2200 idx
= TEMPLATE_CONST_IDX (parm
);
2201 if (targs
[idx
] == arg
)
2203 else if (targs
[idx
])
2205 tree t
= targs
[idx
];
2206 if (TREE_CODE (t
) == TREE_CODE (arg
))
2207 switch (TREE_CODE (arg
))
2210 if (tree_int_cst_equal (t
, arg
))
2214 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t
), TREE_REAL_CST (arg
)))
2217 /* STRING_CST values are not valid template const parms. */
2221 my_friendly_abort (87);
2224 /* else if (typeof arg != tparms[idx])
2227 targs
[idx
] = copy_to_permanent (arg
);
2231 if (TREE_CODE (arg
) != POINTER_TYPE
)
2233 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2236 case REFERENCE_TYPE
:
2237 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2238 arg
= TREE_TYPE (arg
);
2239 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
, nsubsts
);
2242 if (TREE_CODE (arg
) != ARRAY_TYPE
)
2244 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
2247 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2252 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2255 if (TREE_CODE (parm
) == INTEGER_TYPE
)
2257 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
2258 && unify (tparms
, targs
, ntparms
,
2259 TYPE_MIN_VALUE (parm
), TYPE_MIN_VALUE (arg
), nsubsts
))
2261 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
2262 && unify (tparms
, targs
, ntparms
,
2263 TYPE_MAX_VALUE (parm
), TYPE_MAX_VALUE (arg
), nsubsts
))
2266 /* As far as unification is concerned, this wins. Later checks
2267 will invalidate it if necessary. */
2270 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2272 if (TREE_CODE (arg
) != INTEGER_CST
)
2274 return !tree_int_cst_equal (parm
, arg
);
2279 t1
= TREE_OPERAND (parm
, 0);
2280 t2
= TREE_OPERAND (parm
, 1);
2281 return unify (tparms
, targs
, ntparms
, t1
,
2282 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
2289 if (TREE_CODE (arg
) != TREE_VEC
)
2291 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
2293 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
2294 if (unify (tparms
, targs
, ntparms
,
2295 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
2301 case UNINSTANTIATED_P_TYPE
:
2304 /* Unification of something that is not a class fails. */
2305 if (! IS_AGGR_TYPE (arg
))
2307 a
= IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (arg
));
2308 if (a
&& UPT_TEMPLATE (parm
) == TREE_PURPOSE (a
))
2309 return unify (tparms
, targs
, ntparms
, UPT_PARMS (parm
),
2310 TREE_VALUE (a
), nsubsts
);
2311 /* FIXME: Should check base conversions here. */
2316 if (TYPE_PTRMEMFUNC_FLAG (parm
))
2317 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
2320 /* Allow trivial conversions. */
2321 if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
)
2322 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
2323 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
2328 if (TREE_CODE (arg
) != METHOD_TYPE
)
2333 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
2336 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2337 TREE_TYPE (arg
), nsubsts
))
2339 return type_unification (tparms
, targs
, TYPE_ARG_TYPES (parm
),
2340 TYPE_ARG_TYPES (arg
), nsubsts
, 1);
2343 if (TREE_CODE (arg
) != OFFSET_TYPE
)
2345 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
2346 TYPE_OFFSET_BASETYPE (arg
), nsubsts
))
2348 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2349 TREE_TYPE (arg
), nsubsts
);
2352 sorry ("use of `%s' in template type unification",
2353 tree_code_name
[(int) TREE_CODE (parm
)]);
2362 do_pending_expansions ()
2364 struct pending_inline
*i
, *new_list
= 0;
2366 if (!pending_template_expansions
)
2370 fprintf (stderr
, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
2373 i
= pending_template_expansions
;
2378 struct pending_inline
*next
= i
->next
;
2382 #define DECIDE(N) do {decision=(N); goto decided;} while(0)
2384 my_friendly_assert (TREE_CODE (t
) == FUNCTION_DECL
2385 || TREE_CODE (t
) == VAR_DECL
, 294);
2386 if (TREE_ASM_WRITTEN (t
))
2389 if (DECL_EXPLICIT_INSTANTIATION (t
))
2390 DECIDE (DECL_NOT_REALLY_EXTERN (t
));
2391 else if (! flag_implicit_templates
)
2394 if (i
->interface
== 1)
2395 /* OK, it was an implicit instantiation. */
2396 TREE_PUBLIC (t
) = 0;
2398 /* If it's a method, let the class type decide it.
2399 @@ What if the method template is in a separate file?
2400 Maybe both file contexts should be taken into account?
2401 Maybe only do this if i->interface == 1 (unknown)? */
2402 context
= DECL_CONTEXT (t
);
2403 if (context
!= NULL_TREE
2404 && TREE_CODE_CLASS (TREE_CODE (context
)) == 't')
2406 /* I'm interested in the context of this version of the function,
2407 not the original virtual declaration. */
2408 context
= DECL_CLASS_CONTEXT (t
);
2410 /* If `unknown', we might want a static copy.
2411 If `implementation', we want a global one.
2412 If `interface', ext ref. */
2413 if (CLASSTYPE_INTERFACE_KNOWN (context
))
2414 DECIDE (!CLASSTYPE_INTERFACE_ONLY (context
));
2415 #if 0 /* This doesn't get us stuff needed only by the file initializer. */
2416 DECIDE (TREE_USED (t
));
2417 #else /* This compiles too much stuff, but that's probably better in
2418 most cases than never compiling the stuff we need. */
2423 if (i
->interface
== 1)
2424 DECIDE (TREE_USED (t
));
2426 DECIDE (i
->interface
);
2430 print_node_brief (stderr
, decision
? "yes: " : "no: ", t
, 0);
2431 fprintf (stderr
, "\t%s\n",
2432 (DECL_ASSEMBLER_NAME (t
)
2433 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t
))
2438 i
->next
= pending_inlines
;
2439 pending_inlines
= i
;
2448 pending_template_expansions
= new_list
;
2449 if (!pending_inlines
)
2451 do_pending_inlines ();
2456 struct pending_template
{
2457 struct pending_template
*next
;
2461 static struct pending_template
* pending_templates
;
2464 do_pending_templates ()
2466 struct pending_template
* t
;
2468 for ( t
= pending_templates
; t
; t
= t
->next
)
2470 instantiate_class_template (t
->id
, 1);
2473 for ( t
= pending_templates
; t
; t
= pending_templates
)
2475 pending_templates
= t
->next
;
2481 add_pending_template (pt
)
2484 struct pending_template
*p
;
2486 p
= (struct pending_template
*) malloc (sizeof (struct pending_template
));
2487 p
->next
= pending_templates
;
2488 pending_templates
= p
;
2493 mark_function_instantiated (result
, extern_p
)
2497 if (DECL_TEMPLATE_INSTANTIATION (result
))
2498 SET_DECL_EXPLICIT_INSTANTIATION (result
);
2499 TREE_PUBLIC (result
) = 1;
2503 DECL_INTERFACE_KNOWN (result
) = 1;
2504 DECL_NOT_REALLY_EXTERN (result
) = 1;
2508 /* called from the parser. */
2510 do_function_instantiation (declspecs
, declarator
, storage
)
2511 tree declspecs
, declarator
, storage
;
2513 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, 0);
2516 tree result
= NULL_TREE
;
2519 /* If we've already seen this template instance, use it. */
2520 if (name
= DECL_ASSEMBLER_NAME (decl
),
2521 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
2522 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
2524 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
2526 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
2527 if (decls_match (fn
, decl
)
2528 && DECL_DEFER_OUTPUT (fn
))
2533 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2535 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn
));
2536 tree
*targs
= (tree
*) malloc (sizeof (tree
) * ntparms
);
2538 i
= type_unification (DECL_TEMPLATE_PARMS (fn
), targs
,
2539 TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2540 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
2545 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
2547 result
= instantiate_template (fn
, targs
);
2554 cp_error ("no matching template for `%D' found", decl
);
2558 if (flag_external_templates
)
2561 if (storage
== NULL_TREE
)
2563 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2566 cp_error ("storage class `%D' applied to template instantiation",
2568 mark_function_instantiated (result
, extern_p
);
2569 repo_template_instantiated (result
, extern_p
);
2573 mark_class_instantiated (t
, extern_p
)
2577 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
2578 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
2579 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
2580 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
2581 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
2584 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2585 rest_of_type_compilation (t
, 1);
2590 do_type_instantiation (name
, storage
)
2593 tree t
= TREE_TYPE (name
);
2597 /* With -fexternal-templates, explicit instantiations are treated the same
2598 as implicit ones. */
2599 if (flag_external_templates
)
2602 if (TYPE_SIZE (t
) == NULL_TREE
)
2604 cp_error ("explicit instantiation of `%#T' before definition of template",
2609 if (storage
== NULL_TREE
)
2611 else if (storage
== ridpointers
[(int) RID_INLINE
])
2613 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2617 cp_error ("storage class `%D' applied to template instantiation",
2622 /* We've already instantiated this. */
2623 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
2627 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
2629 mark_class_instantiated (t
, extern_p
);
2630 repo_template_instantiated (t
, extern_p
);
2638 /* Classes nested in template classes currently don't have an
2639 IDENTIFIER_TEMPLATE--their out-of-line members are handled
2640 by the enclosing template class. Note that there are name
2641 conflict bugs with this approach. */
2642 tmp
= TYPE_IDENTIFIER (t
);
2643 if (IDENTIFIER_TEMPLATE (tmp
))
2644 instantiate_member_templates (tmp
);
2646 /* this should really be done by instantiate_member_templates */
2647 tmp
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
2648 for (; tmp
; tmp
= TREE_CHAIN (tmp
))
2650 mark_function_instantiated (tmp
, extern_p
);
2651 repo_template_instantiated (tmp
, extern_p
);
2655 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2657 if (TREE_CODE (tmp
) == VAR_DECL
)
2658 /* eventually do something */;
2662 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2663 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
2664 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
2669 create_nested_upt (scope
, name
)
2672 tree t
= make_lang_type (UNINSTANTIATED_P_TYPE
);
2673 tree d
= build_decl (TYPE_DECL
, name
, t
);
2676 TYPE_VALUES (t
) = TYPE_VALUES (scope
);
2677 TYPE_CONTEXT (t
) = scope
;