1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Known bugs or deficiencies include:
23 * templates for class static data don't work (methods only)
24 * duplicated method templates can crash the compiler
25 * interface/impl data is taken from file defining the template
26 * all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win"
28 * method templates must be seen before the expansion of the
29 class template is done
45 extern struct obstack permanent_obstack
;
48 extern char *input_filename
;
49 struct pending_inline
*pending_template_expansions
;
51 int processing_template_decl
;
52 int processing_template_defn
;
54 /* This is a kludge to handle instantiation of template methods that are
55 used before their definition. It should not be necessary after the
57 static tree template_classes
;
59 #define obstack_chunk_alloc xmalloc
60 #define obstack_chunk_free free
63 static void add_pending_template ();
65 void overload_template_name (), pop_template_decls ();
67 /* We've got a template header coming up; set obstacks up to save the
68 nodes created permanently. (There might be cases with nested templates
69 where we don't have to do this, but they aren't implemented, and it
70 probably wouldn't be worth the effort.) */
72 begin_template_parm_list ()
75 push_obstacks (&permanent_obstack
, &permanent_obstack
);
79 /* Process information from new template parameter NEXT and append it to the
80 LIST being built. The rules for use of a template parameter type name
81 by later parameters are not well-defined for us just yet. However, the
82 only way to avoid having to parse expressions of unknown complexity (and
83 with tokens of unknown types) is to disallow it completely. So for now,
84 that is what is assumed. */
86 process_template_parm (list
, next
)
94 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
95 defval
= TREE_PURPOSE (parm
);
96 parm
= TREE_VALUE (parm
);
97 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
101 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
102 /* is a const-param */
103 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
104 PARM
, 0, NULL_TREE
, NULL_TREE
);
105 /* A template parameter is not modifiable. */
106 TREE_READONLY (parm
) = 1;
107 if (IS_AGGR_TYPE (TREE_TYPE (parm
)))
109 sorry ("aggregate template parameter types");
110 TREE_TYPE (parm
) = void_type_node
;
112 tinfo
= make_node (TEMPLATE_CONST_PARM
);
113 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
114 if (TREE_PERMANENT (parm
) == 0)
116 parm
= copy_node (parm
);
117 TREE_PERMANENT (parm
) = 1;
119 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
120 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
121 DECL_INITIAL (decl
) = tinfo
;
122 DECL_INITIAL (parm
) = tinfo
;
126 tree t
= make_node (TEMPLATE_TYPE_PARM
);
127 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
128 TYPE_MAIN_DECL (t
) = decl
;
132 if (IDENTIFIER_HAS_TYPE_VALUE (defval
))
133 defval
= IDENTIFIER_TYPE_VALUE (defval
);
135 defval
= TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (defval
));
138 SET_DECL_ARTIFICIAL (decl
);
140 parm
= build_tree_list (defval
, parm
);
141 return chainon (list
, parm
);
144 /* The end of a template parameter list has been reached. Process the
145 tree list into a parameter vector, converting each parameter into a more
146 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
150 end_template_parm_list (parms
)
157 for (parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
159 saved_parmlist
= make_tree_vec (nparms
);
161 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
163 tree p
= TREE_VALUE (parm
);
164 if (TREE_PURPOSE (parm
))
166 else if (saw_default
)
168 error ("if a default argument is given for one template parameter");
169 error ("default arguments must be given for all subsequent");
170 error ("parameters as well");
173 if (TREE_CODE (p
) == TYPE_DECL
)
175 tree t
= TREE_TYPE (p
);
176 TEMPLATE_TYPE_SET_INFO (t
, saved_parmlist
, nparms
);
180 tree tinfo
= DECL_INITIAL (p
);
181 DECL_INITIAL (p
) = NULL_TREE
;
182 TEMPLATE_CONST_SET_INFO (tinfo
, saved_parmlist
, nparms
);
184 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
186 set_current_level_tags_transparency (1);
187 processing_template_decl
++;
188 return saved_parmlist
;
191 /* end_template_decl is called after a template declaration is seen.
192 D1 is template header; D2 is class_head_sans_basetype or a
193 TEMPLATE_DECL with its DECL_RESULT field set. */
195 end_template_decl (d1
, d2
, is_class
, defn
)
196 tree d1
, d2
, is_class
;
200 struct template_info
*tmpl
;
202 tmpl
= (struct template_info
*) obstack_alloc (&permanent_obstack
,
203 sizeof (struct template_info
));
206 tmpl
->aggr
= is_class
;
208 /* cloned from reinit_parse_for_template */
209 tmpl
->filename
= input_filename
;
210 tmpl
->lineno
= lineno
;
211 tmpl
->parm_vec
= d1
; /* [eichin:19911015.2306EST] */
213 if (d2
== NULL_TREE
|| d2
== error_mark_node
)
221 decl
= build_lang_decl (TEMPLATE_DECL
, d2
, NULL_TREE
);
222 GNU_xref_decl (current_function_decl
, decl
);
226 if (TREE_CODE (d2
) == TEMPLATE_DECL
)
230 /* Class destructor templates and operator templates are
231 slipping past as non-template nodes. Process them here, since
232 I haven't figured out where to catch them earlier. I could
233 go do that, but it's a choice between getting that done and
234 staying only N months behind schedule. Sorry.... */
236 my_friendly_assert (TREE_CODE (d2
) == CALL_EXPR
, 263);
237 code
= TREE_CODE (TREE_OPERAND (d2
, 0));
238 my_friendly_assert (code
== BIT_NOT_EXPR
239 || code
== OP_IDENTIFIER
240 || code
== SCOPE_REF
, 264);
241 d2
= grokdeclarator (d2
, NULL_TREE
, MEMFUNCDEF
, 0, NULL_TREE
, NULL_TREE
);
242 decl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (d2
),
244 DECL_TEMPLATE_RESULT (decl
) = d2
;
245 DECL_CONTEXT (decl
) = DECL_CONTEXT (d2
);
246 DECL_CLASS_CONTEXT (decl
) = DECL_CLASS_CONTEXT (d2
);
247 DECL_NAME (decl
) = DECL_NAME (d2
);
248 TREE_TYPE (decl
) = TREE_TYPE (d2
);
249 if (interface_unknown
&& flag_external_templates
250 && ! flag_alt_external_templates
251 && ! DECL_IN_SYSTEM_HEADER (decl
))
252 warn_if_unknown_interface (decl
);
253 TREE_PUBLIC (decl
) = TREE_PUBLIC (d2
) = flag_external_templates
&& !interface_unknown
;
254 DECL_EXTERNAL (decl
) = (DECL_EXTERNAL (d2
)
255 && !(DECL_CLASS_CONTEXT (d2
)
256 && !DECL_THIS_EXTERN (d2
)));
259 /* All routines creating TEMPLATE_DECL nodes should now be using
260 build_lang_decl, which will have set this up already. */
261 my_friendly_assert (DECL_LANG_SPECIFIC (decl
) != 0, 265);
263 /* @@ Somewhere, permanent allocation isn't being used. */
264 if (! DECL_TEMPLATE_IS_CLASS (decl
)
265 && TREE_CODE (DECL_TEMPLATE_RESULT (decl
)) == FUNCTION_DECL
)
267 tree result
= DECL_TEMPLATE_RESULT (decl
);
268 /* Will do nothing if allocation was already permanent. */
269 DECL_ARGUMENTS (result
) = copy_to_permanent (DECL_ARGUMENTS (result
));
272 /* If this is for a method, there's an extra binding level here. */
273 if (DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl
)) != NULL_TREE
)
275 /* @@ Find out where this should be getting set! */
276 tree r
= DECL_TEMPLATE_RESULT (decl
);
277 if (DECL_LANG_SPECIFIC (r
) && DECL_CLASS_CONTEXT (r
) == NULL_TREE
)
278 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
281 DECL_TEMPLATE_INFO (decl
) = tmpl
;
282 DECL_TEMPLATE_PARMS (decl
) = d1
;
284 /* So that duplicate_decls can do the right thing. */
286 DECL_INITIAL (decl
) = error_mark_node
;
288 /* If context of decl is non-null (i.e., method template), add it
289 to the appropriate class template, and pop the binding levels. */
290 if (! is_class
&& DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl
)) != NULL_TREE
)
292 tree ctx
= DECL_CONTEXT (DECL_TEMPLATE_RESULT (decl
));
294 my_friendly_assert (TREE_CODE (ctx
) == UNINSTANTIATED_P_TYPE
, 266);
295 tmpl
= UPT_TEMPLATE (ctx
);
296 for (t
= DECL_TEMPLATE_MEMBERS (tmpl
); t
; t
= TREE_CHAIN (t
))
297 if (TREE_PURPOSE (t
) == DECL_NAME (decl
)
298 && duplicate_decls (decl
, TREE_VALUE (t
)))
300 DECL_TEMPLATE_MEMBERS (tmpl
) =
301 perm_tree_cons (DECL_NAME (decl
), decl
, DECL_TEMPLATE_MEMBERS (tmpl
));
306 /* Otherwise, go back to top level first, and push the template decl
315 #if 0 /* It happens sometimes, with syntactic or semantic errors.
318 template <class A, int X, int Y> class Foo { ... };
319 template <class A, int X, int y> Foo<X,Y>::method (Foo& x) { ... }
320 Note the missing "A" in the class containing "method". */
321 my_friendly_assert (global_bindings_p (), 267);
323 while (! global_bindings_p ())
327 processing_template_decl
--;
328 (void) get_pending_sizes ();
331 tree tsubst
PROTO ((tree
, tree
*, int, tree
));
333 /* Convert all template arguments to their appropriate types, and return
334 a vector containing the resulting values. If any error occurs, return
337 coerce_template_parms (parms
, arglist
, in_decl
)
341 int nparms
, nargs
, i
, lost
= 0;
344 if (arglist
== NULL_TREE
)
346 else if (TREE_CODE (arglist
) == TREE_VEC
)
347 nargs
= TREE_VEC_LENGTH (arglist
);
349 nargs
= list_length (arglist
);
351 nparms
= TREE_VEC_LENGTH (parms
);
355 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
357 error ("incorrect number of parameters (%d, should be %d)",
360 cp_error_at ("in template expansion for decl `%D'", in_decl
);
361 return error_mark_node
;
364 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
365 vec
= copy_node (arglist
);
368 vec
= make_tree_vec (nparms
);
369 for (i
= 0; i
< nparms
; i
++)
376 arglist
= TREE_CHAIN (arglist
);
378 if (arg
== error_mark_node
)
381 arg
= TREE_VALUE (arg
);
384 arg
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
386 TREE_VEC_ELT (vec
, i
) = arg
;
389 for (i
= 0; i
< nparms
; i
++)
391 tree arg
= TREE_VEC_ELT (vec
, i
);
392 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
394 int is_type
, requires_type
;
396 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
397 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
398 if (is_type
!= requires_type
)
401 cp_error ("type/value mismatch in template parameter list for `%D'",
404 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
408 val
= groktypename (arg
);
411 tree t
= tsubst (TREE_TYPE (parm
), &TREE_VEC_ELT (vec
, 0),
412 TREE_VEC_LENGTH (vec
), in_decl
);
413 val
= digest_init (t
, arg
, (tree
*) 0);
415 if (val
== error_mark_node
)
418 /* 14.2: Other template-arguments must be constant-expressions,
419 addresses of objects or functions with external linkage, or of
420 static class members. */
421 else if (!TREE_CONSTANT (val
))
423 cp_error ("non-const `%E' cannot be used as template argument",
425 val
= error_mark_node
;
427 else if (POINTER_TYPE_P (TREE_TYPE (val
))
428 && ! integer_zerop (val
)
429 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
430 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
434 if (TREE_CODE (t
) == ADDR_EXPR
)
436 tree a
= TREE_OPERAND (t
, 0);
438 if (TREE_CODE (a
) == STRING_CST
)
440 cp_error ("string literal %E is not a valid template argument", a
);
441 error ("because it is the address of an object with static linkage");
442 val
= error_mark_node
;
444 else if (TREE_CODE (a
) != VAR_DECL
445 && TREE_CODE (a
) != FUNCTION_DECL
)
447 else if (! DECL_PUBLIC (a
))
449 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
450 val
= error_mark_node
;
456 cp_error ("`%E' is not a valid template argument", t
);
457 error ("it must be %s%s with external linkage",
458 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
459 ? "a pointer to " : "",
460 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
461 ? "a function" : "an object");
462 val
= error_mark_node
;
467 if (val
== error_mark_node
)
470 TREE_VEC_ELT (vec
, i
) = val
;
473 return error_mark_node
;
477 /* Given class template name and parameter list, produce a user-friendly name
478 for the instantiation. */
480 mangle_class_name_for_template (name
, parms
, arglist
)
484 static struct obstack scratch_obstack
;
485 static char *scratch_firstobj
;
488 if (!scratch_firstobj
)
490 gcc_obstack_init (&scratch_obstack
);
491 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
494 obstack_free (&scratch_obstack
, scratch_firstobj
);
497 #define buflen sizeof(buf)
498 #define check if (bufp >= buf+buflen-1) goto too_long
499 #define ccat(c) *bufp++=(c); check
500 #define advance bufp+=strlen(bufp); check
501 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
504 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
506 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
511 nparms
= TREE_VEC_LENGTH (parms
);
512 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
513 for (i
= 0; i
< nparms
; i
++)
515 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
516 tree arg
= TREE_VEC_ELT (arglist
, i
);
521 if (TREE_CODE (parm
) == TYPE_DECL
)
523 cat (type_as_string (arg
, 0));
527 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
529 if (TREE_CODE (arg
) == TREE_LIST
)
531 /* New list cell was built because old chain link was in
533 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
534 arg
= TREE_VALUE (arg
);
536 /* No need to check arglist against parmlist here; we did that
537 in coerce_template_parms, called from lookup_template_class. */
538 cat (expr_as_string (arg
, 0));
541 char *bufp
= obstack_next_free (&scratch_obstack
);
543 while (bufp
[offset
- 1] == ' ')
545 obstack_blank_fast (&scratch_obstack
, offset
);
547 /* B<C<char> >, not B<C<char>> */
548 if (bufp
[offset
- 1] == '>')
553 return (char *) obstack_base (&scratch_obstack
);
558 fatal ("out of (preallocated) string space creating template instantiation name");
563 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
564 parameters, find the desired type.
566 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
567 Since ARGLIST is build on the decl_obstack, we must copy it here
568 to keep it from being reclaimed when the decl storage is reclaimed.
570 IN_DECL, if non-NULL, is the template declaration we are trying to
573 lookup_template_class (d1
, arglist
, in_decl
)
577 tree
template, parmlist
;
581 my_friendly_assert (TREE_CODE (d1
) == IDENTIFIER_NODE
, 272);
582 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
584 template = IDENTIFIER_CLASS_VALUE (d1
);
585 /* With something like `template <class T> class X class X { ... };'
586 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
587 We don't want to do that, but we have to deal with the situation, so
588 let's give them some syntax errors to chew on instead of a crash. */
590 return error_mark_node
;
591 if (TREE_CODE (template) != TEMPLATE_DECL
)
593 cp_error ("non-template type `%T' used as a template", d1
);
595 cp_error_at ("for template declaration `%D'", in_decl
);
596 return error_mark_node
;
598 parmlist
= DECL_TEMPLATE_PARMS (template);
600 arglist
= coerce_template_parms (parmlist
, arglist
, template);
601 if (arglist
== error_mark_node
)
602 return error_mark_node
;
603 if (uses_template_parms (arglist
))
605 tree t
= make_lang_type (UNINSTANTIATED_P_TYPE
);
607 id
= make_anon_name ();
608 d
= build_decl (TYPE_DECL
, id
, t
);
610 TYPE_VALUES (t
) = build_tree_list (template, arglist
);
611 pushdecl_top_level (d
);
615 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
617 id
= get_identifier (mangled_name
);
619 if (!IDENTIFIER_TEMPLATE (id
))
621 arglist
= copy_to_permanent (arglist
);
622 IDENTIFIER_TEMPLATE (id
) = perm_tree_cons (template, arglist
, NULL_TREE
);
628 push_template_decls (parmlist
, arglist
, class_level
)
629 tree parmlist
, arglist
;
634 /* Don't want to push values into global context. */
638 declare_pseudo_global_level ();
641 nparms
= TREE_VEC_LENGTH (parmlist
);
643 for (i
= 0; i
< nparms
; i
++)
645 int requires_type
, is_type
;
646 tree parm
= TREE_VALUE (TREE_VEC_ELT (parmlist
, i
));
647 tree arg
= TREE_VEC_ELT (arglist
, i
);
650 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
651 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
654 /* add typename to namespace */
657 error ("template use error: type provided where value needed");
661 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl
)) == 't', 273);
662 decl
= build_decl (TYPE_DECL
, DECL_NAME (parm
), decl
);
666 /* add const decl to namespace */
671 error ("template use error: value provided where type needed");
674 parmtype
= tsubst (TREE_TYPE (parm
), &TREE_VEC_ELT (arglist
, 0),
675 TREE_VEC_LENGTH (arglist
), NULL_TREE
);
676 val
= digest_init (parmtype
, arg
, (tree
*) 0);
677 if (val
!= error_mark_node
)
679 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
681 DECL_INITIAL (decl
) = val
;
682 TREE_READONLY (decl
) = 1;
687 SET_DECL_ARTIFICIAL (decl
);
688 layout_decl (decl
, 0);
690 pushdecl_class_level (decl
);
698 pop_template_decls (parmlist
, arglist
, class_level
)
699 tree parmlist
, arglist
;
706 /* Should be defined in parse.h. */
710 uses_template_parms (t
)
715 switch (TREE_CODE (t
))
719 /* We assume that the object must be instantiated in order to build
720 the COMPONENT_REF, so we test only whether the type of the
721 COMPONENT_REF uses template parms. */
722 return uses_template_parms (TREE_TYPE (t
));
724 case IDENTIFIER_NODE
:
725 if (!IDENTIFIER_TEMPLATE (t
))
727 return uses_template_parms (TREE_VALUE (IDENTIFIER_TEMPLATE (t
)));
729 /* aggregates of tree nodes */
732 int i
= TREE_VEC_LENGTH (t
);
734 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
739 if (uses_template_parms (TREE_PURPOSE (t
))
740 || uses_template_parms (TREE_VALUE (t
)))
742 return uses_template_parms (TREE_CHAIN (t
));
744 /* constructed type nodes */
747 return uses_template_parms (TREE_TYPE (t
));
749 if (TYPE_PTRMEMFUNC_FLAG (t
))
750 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
754 if (!TYPE_IDENTIFIER (t
))
756 return uses_template_parms (TYPE_IDENTIFIER (t
));
758 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
760 return uses_template_parms (TREE_TYPE (t
));
762 if (uses_template_parms (TYPE_DOMAIN (t
)))
764 return uses_template_parms (TREE_TYPE (t
));
766 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
768 return uses_template_parms (TREE_TYPE (t
));
770 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
772 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
774 return uses_template_parms (TREE_TYPE (t
));
778 return uses_template_parms (DECL_NAME (t
));
780 if (uses_template_parms (TREE_TYPE (t
)))
785 /* ??? What about FIELD_DECLs? */
786 /* The type of a decl can't use template parms if the name of the
787 variable doesn't, because it's impossible to resolve them. So
788 ignore the type field for now. */
789 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
791 if (uses_template_parms (TREE_TYPE (t
)))
793 error ("template parms used where they can't be resolved");
798 return uses_template_parms (TREE_TYPE (t
));
800 return uses_template_parms (TREE_OPERAND (t
, 0));
802 /* template parm nodes */
803 case TEMPLATE_TYPE_PARM
:
804 case TEMPLATE_CONST_PARM
:
807 /* simple type nodes */
809 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
811 return uses_template_parms (TYPE_MAX_VALUE (t
));
826 /* Non-error_mark_node ERROR_MARKs are bad things. */
827 my_friendly_assert (t
== error_mark_node
, 274);
831 case UNINSTANTIATED_P_TYPE
:
835 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
836 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
837 /* else fall through */
840 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
848 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
849 if (uses_template_parms (TREE_OPERAND (t
, i
)))
856 sorry ("testing %s for template parms",
857 tree_code_name
[(int) TREE_CODE (t
)]);
858 my_friendly_abort (82);
865 instantiate_member_templates (classname
)
870 tree members
= DECL_TEMPLATE_MEMBERS (TREE_PURPOSE (IDENTIFIER_TEMPLATE (id
)));
872 for (t
= members
; t
; t
= TREE_CHAIN (t
))
874 tree parmvec
, type
, classparms
, tdecl
, t2
;
875 int nparms
, xxx
= 0, i
;
877 my_friendly_assert (TREE_VALUE (t
) != NULL_TREE
, 275);
878 my_friendly_assert (TREE_CODE (TREE_VALUE (t
)) == TEMPLATE_DECL
, 276);
879 /* @@ Should verify that class parm list is a list of
880 distinct template parameters, and covers all the template
882 tdecl
= TREE_VALUE (t
);
883 type
= DECL_CONTEXT (DECL_TEMPLATE_RESULT (tdecl
));
884 classparms
= UPT_PARMS (type
);
885 nparms
= TREE_VEC_LENGTH (classparms
);
886 parmvec
= make_tree_vec (nparms
);
887 for (i
= 0; i
< nparms
; i
++)
888 TREE_VEC_ELT (parmvec
, i
) = NULL_TREE
;
889 switch (unify (DECL_TEMPLATE_PARMS (tdecl
),
890 &TREE_VEC_ELT (parmvec
, 0), nparms
,
891 type
, IDENTIFIER_TYPE_VALUE (classname
),
895 /* Success -- well, no inconsistency, at least. */
896 for (i
= 0; i
< nparms
; i
++)
897 if (TREE_VEC_ELT (parmvec
, i
) == NULL_TREE
)
899 t2
= instantiate_template (tdecl
,
900 &TREE_VEC_ELT (parmvec
, 0));
901 type
= IDENTIFIER_TYPE_VALUE (id
);
902 my_friendly_assert (type
!= 0, 277);
907 cp_error_at ("type unification error instantiating `%D'", tdecl
);
908 cp_error ("while instantiating members of `%T'", classname
);
910 continue /* loop of members */;
913 my_friendly_abort (83);
918 static struct tinst_level
*current_tinst_level
= 0;
919 static struct tinst_level
*free_tinst_level
= 0;
920 static int tinst_depth
= 0;
921 int max_tinst_depth
= 17;
924 push_tinst_level (name
)
927 struct tinst_level
*new;
928 tree global
= IDENTIFIER_GLOBAL_VALUE (name
);
930 if (tinst_depth
>= max_tinst_depth
)
932 error ("template instantiation depth exceeds maximum of %d",
934 cp_error (" instantiating `%D'", name
);
938 if (free_tinst_level
)
940 new = free_tinst_level
;
941 free_tinst_level
= new->next
;
944 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
946 new->classname
= name
;
949 new->line
= DECL_SOURCE_LINE (global
);
950 new->file
= DECL_SOURCE_FILE (global
);
955 new->file
= input_filename
;
957 new->next
= current_tinst_level
;
958 current_tinst_level
= new;
966 struct tinst_level
*old
= current_tinst_level
;
968 current_tinst_level
= old
->next
;
969 old
->next
= free_tinst_level
;
970 free_tinst_level
= old
;
977 struct tinst_level
*p
= current_tinst_level
;
980 for (; p
->next
; p
= p
->next
)
986 instantiate_class_template (classname
, setup_parse
)
990 struct template_info
*template_info
;
993 if (classname
== error_mark_node
)
994 return error_mark_node
;
996 my_friendly_assert (TREE_CODE (classname
) == IDENTIFIER_NODE
, 278);
997 template = IDENTIFIER_TEMPLATE (classname
);
999 if (IDENTIFIER_HAS_TYPE_VALUE (classname
))
1001 tree type
= IDENTIFIER_TYPE_VALUE (classname
);
1002 if (TREE_CODE (type
) == UNINSTANTIATED_P_TYPE
)
1004 if (TYPE_BEING_DEFINED (type
)
1006 || CLASSTYPE_USE_TEMPLATE (type
) != 0)
1010 /* If IDENTIFIER_LOCAL_VALUE is already set on this template classname
1011 (it's something like `foo<int>'), that means we're already working on
1012 the instantiation for it. Normally, a classname comes in with nothing
1013 but its IDENTIFIER_TEMPLATE slot set. If we were to try to instantiate
1014 this again, we'd get a redeclaration error. Since we're already working
1015 on it, we'll pass back this classname's TYPE_DECL (it's the value of
1016 the classname's IDENTIFIER_LOCAL_VALUE). Only do this if we're setting
1017 things up for the parser, though---if we're just trying to instantiate
1018 it (e.g., via tsubst) we can trip up cuz it may not have an
1019 IDENTIFIER_TYPE_VALUE when it will need one. */
1020 if (setup_parse
&& IDENTIFIER_LOCAL_VALUE (classname
))
1021 return IDENTIFIER_LOCAL_VALUE (classname
);
1023 if (uses_template_parms (classname
))
1025 if (!TREE_TYPE (classname
))
1027 tree t
= make_lang_type (RECORD_TYPE
);
1028 tree d
= build_decl (TYPE_DECL
, classname
, t
);
1029 DECL_NAME (d
) = classname
;
1036 t1
= TREE_PURPOSE (template);
1037 my_friendly_assert (TREE_CODE (t1
) == TEMPLATE_DECL
, 279);
1039 /* If a template is declared but not defined, accept it; don't crash.
1040 Later uses requiring the definition will be flagged as errors by
1041 other code. Thanks to niklas@appli.se for this bug fix. */
1042 if (DECL_TEMPLATE_INFO (t1
)->text
== 0)
1045 push_to_top_level ();
1046 template_info
= DECL_TEMPLATE_INFO (t1
);
1047 if (setup_parse
&& push_tinst_level (classname
))
1049 push_template_decls (DECL_TEMPLATE_PARMS (TREE_PURPOSE (template)),
1050 TREE_VALUE (template), 0);
1051 set_current_level_tags_transparency (1);
1052 feed_input (template_info
->text
, template_info
->length
, (struct obstack
*)0);
1053 lineno
= template_info
->lineno
;
1054 input_filename
= template_info
->filename
;
1055 /* Get interface/implementation back in sync. */
1056 extract_interface_info ();
1057 overload_template_name (classname
, 0);
1058 /* Kludge so that we don't get screwed by our own base classes. */
1059 TYPE_BEING_DEFINED (TREE_TYPE (classname
)) = 1;
1060 yychar
= PRE_PARSED_CLASS_DECL
;
1061 yylval
.ttype
= classname
;
1062 processing_template_defn
++;
1063 if (!flag_external_templates
)
1064 interface_unknown
++;
1066 = perm_tree_cons (classname
, NULL_TREE
, template_classes
);
1070 tree t
, decl
, id
, tmpl
;
1073 tmpl
= TREE_PURPOSE (IDENTIFIER_TEMPLATE (id
));
1074 t
= xref_tag (DECL_TEMPLATE_INFO (tmpl
)->aggr
, id
, NULL_TREE
, 0);
1075 my_friendly_assert (TREE_CODE (t
) == RECORD_TYPE
1076 || TREE_CODE (t
) == UNION_TYPE
, 280);
1078 /* Now, put a copy of the decl in global scope, to avoid
1079 * recursive expansion. */
1080 decl
= IDENTIFIER_LOCAL_VALUE (id
);
1082 decl
= IDENTIFIER_CLASS_VALUE (id
);
1085 my_friendly_assert (TREE_CODE (decl
) == TYPE_DECL
, 281);
1086 /* We'd better make sure we're on the permanent obstack or else
1087 * we'll get a "friendly" abort 124 in pushdecl. Perhaps a
1088 * copy_to_permanent would be sufficient here, but then a
1089 * sharing problem might occur. I don't know -- niklas@appli.se */
1090 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1091 pushdecl_top_level (copy_node (decl
));
1094 pop_from_top_level ();
1104 if (t1
== NULL_TREE
)
1105 return t2
== NULL_TREE
;
1106 if (t2
== NULL_TREE
)
1108 /* Don't care if one declares its arg const and the other doesn't -- the
1109 main variant of the arg type is all that matters. */
1110 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
1111 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
1113 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1117 lookup_nested_type_by_name (ctype
, name
)
1122 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1124 if (name
== TREE_PURPOSE (t
))
1125 return TREE_VALUE (t
);
1131 search_nested_type_in_tmpl (tmpl
, type
)
1136 if (tmpl
== NULL
|| TYPE_CONTEXT(type
) == NULL
)
1138 t
= search_nested_type_in_tmpl (tmpl
, TYPE_CONTEXT(type
));
1139 if (t
== NULL
) return t
;
1140 t
= lookup_nested_type_by_name(t
, DECL_NAME(TYPE_NAME(type
)));
1145 tsubst (t
, args
, nargs
, in_decl
)
1152 if (t
== NULL_TREE
|| t
== error_mark_node
)
1155 type
= TREE_TYPE (t
);
1157 /* Minor optimization.
1158 ?? Are these really the most frequent cases? Is the savings
1160 && type
!= integer_type_node
1161 && type
!= void_type_node
1162 && type
!= char_type_node
)
1163 type
= tsubst (type
, args
, nargs
, in_decl
);
1165 switch (TREE_CODE (t
))
1168 if (TYPE_PTRMEMFUNC_P (t
))
1169 return build_ptrmemfunc_type
1170 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1172 /* else fall through */
1175 case IDENTIFIER_NODE
:
1188 if (t
== integer_type_node
)
1191 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1192 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
1194 return build_index_2_type
1195 (tsubst (TYPE_MIN_VALUE (t
), args
, nargs
, in_decl
),
1196 tsubst (TYPE_MAX_VALUE (t
), args
, nargs
, in_decl
));
1198 case TEMPLATE_TYPE_PARM
:
1200 tree arg
= args
[TEMPLATE_TYPE_IDX (t
)];
1201 return cp_build_type_variant
1202 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
1203 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
1206 case TEMPLATE_CONST_PARM
:
1207 return args
[TEMPLATE_CONST_IDX (t
)];
1212 tree fnargs
, result
;
1214 if (type
== TREE_TYPE (t
)
1215 && (DECL_CONTEXT (t
) == NULL_TREE
1216 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) != 't'))
1218 fnargs
= tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1219 result
= tsubst (DECL_RESULT (t
), args
, nargs
, t
);
1220 if (DECL_CONTEXT (t
) != NULL_TREE
1221 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
1223 /* Look it up in that class, and return the decl node there,
1224 instead of creating a new one. */
1225 tree ctx
, methods
, name
, method
;
1229 name
= DECL_NAME (t
);
1230 ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
1231 methods
= CLASSTYPE_METHOD_VEC (ctx
);
1232 if (methods
== NULL_TREE
)
1233 /* No methods at all -- no way this one can match. */
1235 n_methods
= TREE_VEC_LENGTH (methods
);
1239 if (!strncmp (OPERATOR_TYPENAME_FORMAT
,
1240 IDENTIFIER_POINTER (name
),
1241 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
1243 /* Type-conversion operator. Reconstruct the name, in
1244 case it's the name of one of the template's parameters. */
1245 name
= build_typename_overload (TREE_TYPE (type
));
1248 if (DECL_CONTEXT (t
) != NULL_TREE
1249 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't'
1250 && constructor_name (DECL_CONTEXT (t
)) == DECL_NAME (t
))
1251 name
= constructor_name (ctx
);
1253 if (DECL_CONSTRUCTOR_P (t
) && TYPE_USES_VIRTUAL_BASECLASSES (ctx
))
1255 /* Since we didn't know that this class had virtual bases until after
1256 we instantiated it, we have to recreate the arguments to this
1257 constructor, as otherwise it would miss the __in_chrg parameter. */
1259 tree parms
= TREE_CHAIN (TYPE_ARG_TYPES (type
));
1260 parms
= hash_tree_chain (integer_type_node
, parms
);
1261 newtype
= build_cplus_method_type (ctx
,
1264 newtype
= build_type_variant (newtype
,
1265 TYPE_READONLY (type
),
1266 TYPE_VOLATILE (type
));
1269 fnargs
= copy_node (DECL_ARGUMENTS (t
));
1270 TREE_CHAIN (fnargs
) = TREE_CHAIN (DECL_ARGUMENTS (t
));
1272 /* In this case we need "in-charge" flag saying whether
1273 this constructor is responsible for initialization
1274 of virtual baseclasses or not. */
1275 parm
= build_decl (PARM_DECL
, in_charge_identifier
, integer_type_node
);
1276 /* Mark the artificial `__in_chrg' parameter as "artificial". */
1277 SET_DECL_ARTIFICIAL (parm
);
1278 DECL_ARG_TYPE (parm
) = integer_type_node
;
1279 DECL_REGISTER (parm
) = 1;
1280 TREE_CHAIN (parm
) = TREE_CHAIN (fnargs
);
1281 TREE_CHAIN (fnargs
) = parm
;
1283 fnargs
= tsubst (fnargs
, args
, nargs
, t
);
1286 fprintf (stderr
, "\nfor function %s in class %s:\n",
1287 IDENTIFIER_POINTER (name
),
1288 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx
)));
1290 for (i
= 0; i
< n_methods
; i
++)
1294 method
= TREE_VEC_ELT (methods
, i
);
1295 if (method
== NULL_TREE
|| DECL_NAME (method
) != name
)
1300 for (; method
; method
= DECL_CHAIN (method
))
1302 my_friendly_assert (TREE_CODE (method
) == FUNCTION_DECL
,
1304 if (! comptypes (type
, TREE_TYPE (method
), 1))
1306 tree mtype
= TREE_TYPE (method
);
1309 /* Keep looking for a method that matches
1310 perfectly. This takes care of the problem
1311 where destructors (which have implicit int args)
1312 look like constructors which have an int arg. */
1316 t1
= TYPE_ARG_TYPES (mtype
);
1317 t2
= TYPE_ARG_TYPES (type
);
1318 if (TREE_CODE (mtype
) == FUNCTION_TYPE
)
1319 t2
= TREE_CHAIN (t2
);
1321 if (list_eq (t1
, t2
))
1323 if (TREE_CODE (mtype
) == FUNCTION_TYPE
)
1326 newtype
= build_function_type (TREE_TYPE (type
),
1327 TYPE_ARG_TYPES (type
));
1328 newtype
= build_type_variant (newtype
,
1329 TYPE_READONLY (type
),
1330 TYPE_VOLATILE (type
));
1332 if (TREE_TYPE (type
) != TREE_TYPE (mtype
))
1333 goto maybe_bad_return_type
;
1335 else if (TYPE_METHOD_BASETYPE (mtype
)
1336 == TYPE_METHOD_BASETYPE (type
))
1338 /* Types didn't match, but arg types and
1339 `this' do match, so the return type is
1340 all that should be messing it up. */
1341 maybe_bad_return_type
:
1342 if (TREE_TYPE (type
) != TREE_TYPE (mtype
))
1343 error ("inconsistent return types for method `%s' in class `%s'",
1344 IDENTIFIER_POINTER (name
),
1345 IDENTIFIER_POINTER (TYPE_IDENTIFIER (ctx
)));
1354 fprintf (stderr
, "\tfound %s\n\n",
1355 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method
)));
1357 if (DECL_ARTIFICIAL (method
))
1359 cp_error ("template for method `%D' which has default implementation in class `%T'", name
, ctx
);
1361 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl
);
1362 return error_mark_node
;
1365 if (DECL_ARGUMENTS (method
)
1366 && ! TREE_PERMANENT (DECL_ARGUMENTS (method
)))
1367 /* @@ Is this early enough? Might we want to do
1368 this instead while processing the expansion? */
1369 DECL_ARGUMENTS (method
)
1370 = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1374 if (r
== NULL_TREE
&& pass
== 0)
1377 method
= TREE_VEC_ELT (methods
, i
);
1386 ? "template for method `%D' doesn't match any in class `%T'"
1387 : "method `%D' not found in class `%T'", name
, ctx
);
1389 cp_error_at ("in attempt to instantiate `%D' declared at this point in file", in_decl
);
1390 return error_mark_node
;
1400 decls
= lookup_name_nonclass (r
);
1401 if (decls
== NULL_TREE
)
1403 else if (TREE_CODE (decls
) == TREE_LIST
)
1404 for (decls
= TREE_VALUE (decls
); decls
;
1405 decls
= DECL_CHAIN (decls
))
1407 if (TREE_CODE (decls
) == FUNCTION_DECL
1408 && TREE_TYPE (decls
) == type
)
1419 if (TREE_CODE (val
) == FUNCTION_DECL
1420 && TREE_TYPE (val
) == type
)
1429 tree a
= build_decl_overload (r
, TYPE_VALUES (type
),
1430 DECL_CONTEXT (t
) != NULL_TREE
);
1431 r
= build_lang_decl (FUNCTION_DECL
, r
, type
);
1432 DECL_ASSEMBLER_NAME (r
) = a
;
1434 else if (TREE_STATIC (r
))
1436 /* This overrides the template version, use it. */
1441 TREE_PUBLIC (r
) = 1;
1442 DECL_EXTERNAL (r
) = 1;
1443 TREE_STATIC (r
) = 0;
1444 DECL_INTERFACE_KNOWN (r
) = 0;
1445 DECL_INLINE (r
) = DECL_INLINE (t
);
1446 DECL_THIS_INLINE (r
) = DECL_THIS_INLINE (t
);
1447 TREE_READONLY (r
) = TREE_READONLY (t
);
1448 TREE_THIS_VOLATILE (r
) = TREE_THIS_VOLATILE (t
);
1450 #if 0 /* Maybe later. -jason */
1451 struct tinst_level
*til
= tinst_for_decl();
1453 /* should always be true under new approach */
1456 DECL_SOURCE_FILE (r
) = til
->file
;
1457 DECL_SOURCE_LINE (r
) = til
->line
;
1462 DECL_SOURCE_FILE (r
) = DECL_SOURCE_FILE (t
);
1463 DECL_SOURCE_LINE (r
) = DECL_SOURCE_LINE (t
);
1466 DECL_CLASS_CONTEXT (r
) = tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1467 make_decl_rtl (r
, NULL_PTR
, 1);
1468 DECL_ARGUMENTS (r
) = fnargs
;
1469 DECL_RESULT (r
) = result
;
1471 if (DECL_CONTEXT (t
) == NULL_TREE
1472 || TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) != 't')
1473 push_overloaded_decl_top_level (r
, 0);
1481 r
= build_decl (PARM_DECL
, DECL_NAME (t
), type
);
1482 DECL_INITIAL (r
) = TREE_TYPE (r
);
1483 DECL_ARTIFICIAL (r
) = DECL_ARTIFICIAL (t
);
1484 #ifdef PROMOTE_PROTOTYPES
1485 if ((TREE_CODE (type
) == INTEGER_TYPE
1486 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1487 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
1488 DECL_ARG_TYPE (r
) = integer_type_node
;
1491 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1497 tree purpose
, value
, chain
, result
;
1498 int via_public
, via_virtual
, via_protected
;
1500 if (t
== void_list_node
)
1503 via_public
= TREE_VIA_PUBLIC (t
);
1504 via_protected
= TREE_VIA_PROTECTED (t
);
1505 via_virtual
= TREE_VIA_VIRTUAL (t
);
1507 purpose
= TREE_PURPOSE (t
);
1509 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
1510 value
= TREE_VALUE (t
);
1512 value
= tsubst (value
, args
, nargs
, in_decl
);
1513 chain
= TREE_CHAIN (t
);
1514 if (chain
&& chain
!= void_type_node
)
1515 chain
= tsubst (chain
, args
, nargs
, in_decl
);
1516 if (purpose
== TREE_PURPOSE (t
)
1517 && value
== TREE_VALUE (t
)
1518 && chain
== TREE_CHAIN (t
))
1520 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
1521 purpose
, value
, chain
);
1522 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
1527 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
1528 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
1529 bzero ((char *) elts
, len
* sizeof (tree
));
1531 for (i
= 0; i
< len
; i
++)
1533 elts
[i
] = tsubst (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
1534 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
1541 t
= make_tree_vec (len
);
1542 for (i
= 0; i
< len
; i
++)
1543 TREE_VEC_ELT (t
, i
) = elts
[i
];
1547 case REFERENCE_TYPE
:
1550 enum tree_code code
;
1551 if (type
== TREE_TYPE (t
))
1554 code
= TREE_CODE (t
);
1555 if (code
== POINTER_TYPE
)
1556 r
= build_pointer_type (type
);
1558 r
= build_reference_type (type
);
1559 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1560 /* Will this ever be needed for TYPE_..._TO values? */
1565 return build_offset_type
1566 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
1570 tree values
= TYPE_ARG_TYPES (t
);
1571 tree context
= TYPE_CONTEXT (t
);
1574 /* Don't bother recursing if we know it won't change anything. */
1575 if (values
!= void_list_node
)
1576 values
= tsubst (values
, args
, nargs
, in_decl
);
1578 context
= tsubst (context
, args
, nargs
, in_decl
);
1579 /* Could also optimize cases where return value and
1580 values have common elements (e.g., T min(const &T, const T&). */
1582 /* If the above parameters haven't changed, just return the type. */
1583 if (type
== TREE_TYPE (t
)
1584 && values
== TYPE_VALUES (t
)
1585 && context
== TYPE_CONTEXT (t
))
1588 /* Construct a new type node and return it. */
1589 if (TREE_CODE (t
) == FUNCTION_TYPE
1590 && context
== NULL_TREE
)
1592 new_value
= build_function_type (type
, values
);
1594 else if (context
== NULL_TREE
)
1596 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
1597 args
, nargs
, in_decl
);
1598 new_value
= build_cplus_method_type (base
, type
,
1599 TREE_CHAIN (values
));
1603 new_value
= make_node (TREE_CODE (t
));
1604 TREE_TYPE (new_value
) = type
;
1605 TYPE_CONTEXT (new_value
) = context
;
1606 TYPE_VALUES (new_value
) = values
;
1607 TYPE_SIZE (new_value
) = TYPE_SIZE (t
);
1608 TYPE_ALIGN (new_value
) = TYPE_ALIGN (t
);
1609 TYPE_MODE (new_value
) = TYPE_MODE (t
);
1610 if (TYPE_METHOD_BASETYPE (t
))
1611 TYPE_METHOD_BASETYPE (new_value
) = tsubst (TYPE_METHOD_BASETYPE (t
),
1612 args
, nargs
, in_decl
);
1613 /* Need to generate hash value. */
1614 my_friendly_abort (84);
1616 new_value
= build_type_variant (new_value
,
1623 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
1625 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
1627 r
= build_cplus_array_type (type
, domain
);
1631 case UNINSTANTIATED_P_TYPE
:
1633 int nparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (UPT_TEMPLATE (t
)));
1634 tree argvec
= make_tree_vec (nparms
);
1635 tree parmvec
= UPT_PARMS (t
);
1638 for (i
= 0; i
< nparms
; i
++)
1639 TREE_VEC_ELT (argvec
, i
) = tsubst (TREE_VEC_ELT (parmvec
, i
),
1640 args
, nargs
, in_decl
);
1641 id
= lookup_template_class (DECL_NAME (UPT_TEMPLATE (t
)), argvec
, NULL_TREE
);
1642 if (! IDENTIFIER_HAS_TYPE_VALUE (id
)) {
1643 instantiate_class_template(id
, 0);
1644 /* set up pending_classes */
1645 add_pending_template (id
);
1647 TYPE_MAIN_VARIANT (IDENTIFIER_TYPE_VALUE (id
)) =
1648 IDENTIFIER_TYPE_VALUE (id
);
1650 rt
= IDENTIFIER_TYPE_VALUE (id
);
1652 /* kung: this part handles nested type in template definition */
1654 if ( !ANON_AGGRNAME_P (DECL_NAME(TYPE_NAME(t
))))
1656 rt
= search_nested_type_in_tmpl (rt
, t
);
1659 return build_type_variant (rt
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1664 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1665 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1666 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
1670 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
1671 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
1674 sorry ("use of `%s' in function template",
1675 tree_code_name
[(int) TREE_CODE (t
)]);
1676 return error_mark_node
;
1681 instantiate_template (tmpl
, targ_ptr
)
1682 tree tmpl
, *targ_ptr
;
1686 struct pending_inline
*p
;
1687 struct template_info
*t
;
1688 struct obstack
*old_fmp_obstack
;
1689 extern struct obstack
*function_maybepermanent_obstack
;
1691 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1692 old_fmp_obstack
= function_maybepermanent_obstack
;
1693 function_maybepermanent_obstack
= &permanent_obstack
;
1695 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
1696 len
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
1700 targ_ptr
[i
] = copy_to_permanent (targ_ptr
[i
]);
1702 for (fndecl
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1703 fndecl
; fndecl
= TREE_CHAIN (fndecl
))
1705 tree
*t1
= &TREE_VEC_ELT (TREE_PURPOSE (fndecl
), 0);
1706 for (i
= len
- 1; i
>= 0; i
--)
1707 if (simple_cst_equal (t1
[i
], targ_ptr
[i
]) <= 0)
1710 /* Here, we have a match. */
1711 fndecl
= TREE_VALUE (fndecl
);
1718 targs
= make_tree_vec (len
);
1721 TREE_VEC_ELT (targs
, i
) = targ_ptr
[i
];
1723 /* substitute template parameters */
1724 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
,
1725 TREE_VEC_LENGTH (targs
), tmpl
);
1727 if (fndecl
== error_mark_node
)
1730 assemble_external (fndecl
);
1732 /* If it's a static member fn in the template, we need to change it
1733 into a FUNCTION_TYPE and chop off its this pointer. */
1734 if (TREE_CODE (TREE_TYPE (DECL_RESULT (tmpl
))) == METHOD_TYPE
1735 && DECL_STATIC_FUNCTION_P (fndecl
))
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 (fndecl
) = TREE_CHAIN (DECL_ARGUMENTS (fndecl
));
1743 t
= DECL_TEMPLATE_INFO (tmpl
);
1745 /* If we have a preexisting version of this function, don't expand
1746 the template version, use the other instead. */
1747 if (TREE_STATIC (fndecl
) || DECL_TEMPLATE_SPECIALIZATION (fndecl
))
1749 SET_DECL_TEMPLATE_SPECIALIZATION (fndecl
);
1750 p
= (struct pending_inline
*)0;
1754 SET_DECL_IMPLICIT_INSTANTIATION (fndecl
);
1755 repo_template_used (fndecl
);
1756 p
= (struct pending_inline
*) permalloc (sizeof (struct pending_inline
));
1757 p
->parm_vec
= t
->parm_vec
;
1758 p
->bindings
= targs
;
1766 char * f
= input_filename
;
1768 lineno
= p
->lineno
= t
->lineno
;
1769 input_filename
= p
->filename
= t
->filename
;
1771 extract_interface_info ();
1773 if (interface_unknown
&& flag_external_templates
)
1775 if (DECL_CLASS_CONTEXT (fndecl
)
1776 && CLASSTYPE_INTERFACE_KNOWN (DECL_CLASS_CONTEXT (fndecl
)))
1778 interface_unknown
= 0;
1780 = CLASSTYPE_INTERFACE_ONLY (DECL_CLASS_CONTEXT (fndecl
));
1782 else if (! DECL_IN_SYSTEM_HEADER (tmpl
))
1783 warn_if_unknown_interface (tmpl
);
1786 if (interface_unknown
|| ! flag_external_templates
)
1787 p
->interface
= 1; /* unknown */
1789 p
->interface
= interface_only
? 0 : 2;
1794 extract_interface_info ();
1798 p
= (struct pending_inline
*)0;
1800 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) =
1801 tree_cons (targs
, fndecl
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1803 if (p
== (struct pending_inline
*)0)
1807 else if (DECL_INLINE (fndecl
))
1809 DECL_PENDING_INLINE_INFO (fndecl
) = p
;
1810 p
->next
= pending_inlines
;
1811 pending_inlines
= p
;
1815 p
->next
= pending_template_expansions
;
1816 pending_template_expansions
= p
;
1819 function_maybepermanent_obstack
= old_fmp_obstack
;
1825 /* classlevel should now never be true. jason 4/12/94 */
1827 undo_template_name_overload (id
, classlevel
)
1833 template = IDENTIFIER_TEMPLATE (id
);
1837 #if 0 /* not yet, should get fixed properly later */
1841 /* This was a botch... See `overload_template_name' just below. */
1847 /* classlevel should now never be true. jason 4/12/94 */
1849 overload_template_name (id
, classlevel
)
1853 tree
template, t
, decl
;
1854 struct template_info
*tinfo
;
1856 my_friendly_assert (TREE_CODE (id
) == IDENTIFIER_NODE
, 284);
1857 template = IDENTIFIER_TEMPLATE (id
);
1861 template = TREE_PURPOSE (template);
1862 tinfo
= DECL_TEMPLATE_INFO (template);
1863 template = DECL_NAME (template);
1864 my_friendly_assert (template != NULL_TREE
, 285);
1867 /* This was a botch... names of templates do not get their own private
1868 scopes. Rather, they should go into the binding level already created
1869 by push_template_decls. Except that there isn't one of those for
1874 declare_pseudo_global_level ();
1878 t
= xref_tag (tinfo
->aggr
, id
, NULL_TREE
, 1);
1879 my_friendly_assert (TREE_CODE (t
) == RECORD_TYPE
1880 || TREE_CODE (t
) == UNION_TYPE
1881 || TREE_CODE (t
) == UNINSTANTIATED_P_TYPE
, 286);
1883 decl
= build_decl (TYPE_DECL
, template, t
);
1884 SET_DECL_ARTIFICIAL (decl
);
1886 #if 0 /* fix this later */
1887 /* We don't want to call here if the work has already been done. */
1889 ? IDENTIFIER_CLASS_VALUE (template)
1890 : IDENTIFIER_LOCAL_VALUE (template));
1892 && TREE_CODE (t
) == TYPE_DECL
1893 && TREE_TYPE (t
) == t
)
1894 my_friendly_abort (85);
1898 pushdecl_class_level (decl
);
1902 #if 0 /* This seems bogus to me; if it isn't, explain why. (jason) */
1903 /* Fake this for now, just to make dwarfout.c happy. It will have to
1904 be done in a proper way later on. */
1905 DECL_CONTEXT (decl
) = t
;
1909 extern struct pending_input
*to_be_restored
;
1911 /* NAME is the IDENTIFIER value of a PRE_PARSED_CLASS_DECL. */
1913 end_template_instantiation (name
)
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;
2368 for (t
= template_classes
; t
; t
= TREE_CHAIN (t
))
2369 instantiate_member_templates (TREE_PURPOSE (t
));
2372 if (!pending_template_expansions
)
2376 fprintf (stderr
, "\n\n\t\t IN DO_PENDING_EXPANSIONS\n\n");
2379 i
= pending_template_expansions
;
2384 struct pending_inline
*next
= i
->next
;
2388 #define DECIDE(N) do {decision=(N); goto decided;} while(0)
2390 my_friendly_assert (TREE_CODE (t
) == FUNCTION_DECL
2391 || TREE_CODE (t
) == VAR_DECL
, 294);
2392 if (TREE_ASM_WRITTEN (t
))
2395 if (DECL_EXPLICIT_INSTANTIATION (t
))
2396 DECIDE (DECL_NOT_REALLY_EXTERN (t
));
2397 else if (! flag_implicit_templates
)
2400 if (i
->interface
== 1)
2401 /* OK, it was an implicit instantiation. */
2406 TREE_PUBLIC (t
) = 0;
2409 /* If it's a method, let the class type decide it.
2410 @@ What if the method template is in a separate file?
2411 Maybe both file contexts should be taken into account?
2412 Maybe only do this if i->interface == 1 (unknown)? */
2413 context
= DECL_CONTEXT (t
);
2414 if (context
!= NULL_TREE
2415 && TREE_CODE_CLASS (TREE_CODE (context
)) == 't')
2417 /* I'm interested in the context of this version of the function,
2418 not the original virtual declaration. */
2419 context
= DECL_CLASS_CONTEXT (t
);
2421 /* If `unknown', we might want a static copy.
2422 If `implementation', we want a global one.
2423 If `interface', ext ref. */
2424 if (CLASSTYPE_INTERFACE_KNOWN (context
))
2425 DECIDE (!CLASSTYPE_INTERFACE_ONLY (context
));
2426 #if 1 /* This doesn't get us stuff needed only by the file initializer. */
2427 DECIDE (TREE_USED (t
));
2428 #else /* This compiles too much stuff, but that's probably better in
2429 most cases than never compiling the stuff we need. */
2434 if (i
->interface
== 1)
2435 DECIDE (TREE_USED (t
));
2437 DECIDE (i
->interface
);
2441 print_node_brief (stderr
, decision
? "yes: " : "no: ", t
, 0);
2442 fprintf (stderr
, "\t%s\n",
2443 (DECL_ASSEMBLER_NAME (t
)
2444 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t
))
2449 i
->next
= pending_inlines
;
2450 pending_inlines
= i
;
2459 pending_template_expansions
= new_list
;
2460 if (!pending_inlines
)
2462 do_pending_inlines ();
2467 struct pending_template
{
2468 struct pending_template
*next
;
2472 static struct pending_template
* pending_templates
;
2475 do_pending_templates ()
2477 struct pending_template
* t
;
2479 for ( t
= pending_templates
; t
; t
= t
->next
)
2481 instantiate_class_template (t
->id
, 1);
2484 for ( t
= pending_templates
; t
; t
= pending_templates
)
2486 pending_templates
= t
->next
;
2492 add_pending_template (pt
)
2495 struct pending_template
*p
;
2497 p
= (struct pending_template
*) malloc (sizeof (struct pending_template
));
2498 p
->next
= pending_templates
;
2499 pending_templates
= p
;
2504 mark_function_instantiated (result
, extern_p
)
2508 if (DECL_TEMPLATE_INSTANTIATION (result
))
2509 SET_DECL_EXPLICIT_INSTANTIATION (result
);
2510 TREE_PUBLIC (result
) = 1;
2514 DECL_INTERFACE_KNOWN (result
) = 1;
2515 DECL_NOT_REALLY_EXTERN (result
) = 1;
2519 /* called from the parser. */
2521 do_function_instantiation (declspecs
, declarator
, storage
)
2522 tree declspecs
, declarator
, storage
;
2524 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0,
2525 NULL_TREE
, NULL_TREE
);
2528 tree result
= NULL_TREE
;
2531 /* If we've already seen this template instance, use it. */
2532 if (name
= DECL_ASSEMBLER_NAME (decl
),
2533 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
2534 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
2536 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
2538 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
2539 if (decls_match (fn
, decl
)
2540 && DECL_DEFER_OUTPUT (fn
))
2545 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2547 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn
));
2548 tree
*targs
= (tree
*) malloc (sizeof (tree
) * ntparms
);
2550 i
= type_unification (DECL_TEMPLATE_PARMS (fn
), targs
,
2551 TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2552 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
2557 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
2559 result
= instantiate_template (fn
, targs
);
2566 cp_error ("no matching template for `%D' found", decl
);
2570 if (flag_external_templates
)
2573 if (storage
== NULL_TREE
)
2575 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2578 cp_error ("storage class `%D' applied to template instantiation",
2580 mark_function_instantiated (result
, extern_p
);
2581 repo_template_instantiated (result
, extern_p
);
2585 mark_class_instantiated (t
, extern_p
)
2589 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
2590 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
2591 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
2592 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
2593 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
2596 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2597 rest_of_type_compilation (t
, 1);
2602 do_type_instantiation (name
, storage
)
2605 tree t
= TREE_TYPE (name
);
2609 /* With -fexternal-templates, explicit instantiations are treated the same
2610 as implicit ones. */
2611 if (flag_external_templates
)
2614 if (TYPE_SIZE (t
) == NULL_TREE
)
2616 cp_error ("explicit instantiation of `%#T' before definition of template",
2621 if (storage
== NULL_TREE
)
2623 else if (storage
== ridpointers
[(int) RID_INLINE
])
2625 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2629 cp_error ("storage class `%D' applied to template instantiation",
2634 /* We've already instantiated this. */
2635 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
2639 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
2641 mark_class_instantiated (t
, extern_p
);
2642 repo_template_instantiated (t
, extern_p
);
2650 /* Classes nested in template classes currently don't have an
2651 IDENTIFIER_TEMPLATE--their out-of-line members are handled
2652 by the enclosing template class. Note that there are name
2653 conflict bugs with this approach. */
2654 tmp
= TYPE_IDENTIFIER (t
);
2655 if (IDENTIFIER_TEMPLATE (tmp
))
2656 instantiate_member_templates (tmp
);
2658 /* this should really be done by instantiate_member_templates */
2659 tmp
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
2660 for (; tmp
; tmp
= TREE_CHAIN (tmp
))
2661 if (DECL_TEMPLATE_INSTANTIATION (tmp
))
2663 mark_function_instantiated (tmp
, extern_p
);
2664 repo_template_instantiated (tmp
, extern_p
);
2668 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2670 if (TREE_CODE (tmp
) == VAR_DECL
)
2671 /* eventually do something */;
2675 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2676 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
2677 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
2682 create_nested_upt (scope
, name
)
2685 tree t
= make_lang_type (UNINSTANTIATED_P_TYPE
);
2686 tree d
= build_decl (TYPE_DECL
, name
, t
);
2689 TYPE_VALUES (t
) = TYPE_VALUES (scope
);
2690 TYPE_CONTEXT (t
) = scope
;