1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 templates for class static data don't work (methods only),
26 duplicated method templates can crash the compiler,
27 interface/impl data is taken from file defining the template,
28 all methods must be provided in header files; can't use a source
29 file that contains only the method templates and "just win". */
49 extern struct obstack permanent_obstack
;
52 extern char *input_filename
;
53 struct pending_inline
*pending_template_expansions
;
55 tree current_template_parms
;
56 HOST_WIDE_INT processing_template_decl
;
58 tree pending_templates
;
59 static tree
*template_tail
= &pending_templates
;
62 static tree
*maybe_template_tail
= &maybe_templates
;
64 int minimal_parse_mode
;
66 #define obstack_chunk_alloc xmalloc
67 #define obstack_chunk_free free
69 static int unify
PROTO((tree
, tree
*, int, tree
, tree
, int *, int));
70 static void add_pending_template
PROTO((tree
));
71 static int push_tinst_level
PROTO((tree
));
72 static tree classtype_mangled_name
PROTO((tree
));
73 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
));
74 static tree tsubst_expr_values
PROTO((tree
, tree
));
75 static int comp_template_args
PROTO((tree
, tree
));
76 static int list_eq
PROTO((tree
, tree
));
77 static tree get_class_bindings
PROTO((tree
, tree
, tree
));
78 static tree coerce_template_parms
PROTO((tree
, tree
, tree
));
79 static tree tsubst_enum
PROTO((tree
, tree
, int));
80 static tree add_to_template_args
PROTO((tree
, tree
));
82 /* Restore the template parameter context. */
85 begin_member_template_processing (parms
)
90 ++processing_template_decl
;
91 current_template_parms
92 = tree_cons (build_int_2 (0, processing_template_decl
),
93 parms
, current_template_parms
);
94 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
96 tree parm
= TREE_VEC_ELT (parms
, i
);
98 switch (TREE_CODE (parm
))
100 case TEMPLATE_TYPE_PARM
:
101 pushdecl (TYPE_NAME (parm
));
104 case TEMPLATE_CONST_PARM
:
109 my_friendly_abort (0);
114 /* Undo the effects of begin_member_template_processing. */
117 end_member_template_processing ()
119 if (! processing_template_decl
)
122 --processing_template_decl
;
123 current_template_parms
= TREE_CHAIN (current_template_parms
);
126 /* Returns non-zero iff T is a member template function. Works if T
127 is either a FUNCTION_DECL or a TEMPLATE_DECL. */
130 is_member_template (t
)
135 if (DECL_FUNCTION_MEMBER_P (t
) ||
136 (TREE_CODE (t
) == TEMPLATE_DECL
&&
137 DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))))
139 tree tmpl
= NULL_TREE
;
141 if (DECL_FUNCTION_TEMPLATE_P (t
))
143 else if (DECL_TEMPLATE_INFO (t
)
144 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
145 tmpl
= DECL_TI_TEMPLATE (t
);
149 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
150 int parm_levels
= list_length (parms
);
151 int template_class_levels
= 0;
152 tree ctx
= DECL_CLASS_CONTEXT (t
);
154 if (CLASSTYPE_TEMPLATE_INFO (ctx
))
158 /* Here, we should really count the number of levels
159 deep ctx is, making sure not to count any levels that
160 are just specializations. Since there are no member
161 template classes yet, we don't have to do all that. */
163 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
164 template_class_levels
= 1;
169 args
= CLASSTYPE_TI_ARGS (ctx
);
171 if (args
== NULL_TREE
)
172 template_class_levels
= 1;
174 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
175 if (uses_template_parms (TREE_VEC_ELT (args
, i
)))
177 template_class_levels
++;
183 if (parm_levels
> template_class_levels
)
191 /* Return a new template argument vector which contains all of ARGS,
192 but has as its innermost set of arguments the EXTRA_ARGS. */
195 add_to_template_args (args
, extra_args
)
201 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
203 new_args
= make_tree_vec (2);
204 TREE_VEC_ELT (new_args
, 0) = args
;
210 new_args
= make_tree_vec (TREE_VEC_LENGTH (args
) - 1);
212 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
213 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
216 TREE_VEC_ELT (new_args
,
217 TREE_VEC_LENGTH (new_args
) - 1) = extra_args
;
222 /* We've got a template header coming up; push to a new level for storing
226 begin_template_parm_list ()
229 declare_pseudo_global_level ();
230 ++processing_template_decl
;
233 /* Process information from new template parameter NEXT and append it to the
237 process_template_parm (list
, next
)
245 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
246 defval
= TREE_PURPOSE (parm
);
247 parm
= TREE_VALUE (parm
);
248 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
252 tree p
= TREE_VALUE (tree_last (list
));
254 if (TREE_CODE (p
) == TYPE_DECL
)
255 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
257 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
266 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
267 /* is a const-param */
268 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
270 /* A template parameter is not modifiable. */
271 TREE_READONLY (parm
) = 1;
272 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
273 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
275 cp_error ("`%#T' is not a valid type for a template constant parameter",
277 if (DECL_NAME (parm
) == NULL_TREE
)
278 error (" a template type parameter must begin with `class' or `typename'");
279 TREE_TYPE (parm
) = void_type_node
;
282 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
283 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
284 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
286 tinfo
= make_node (TEMPLATE_CONST_PARM
);
287 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
288 if (TREE_PERMANENT (parm
) == 0)
290 parm
= copy_node (parm
);
291 TREE_PERMANENT (parm
) = 1;
293 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
294 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
295 DECL_INITIAL (decl
) = tinfo
;
296 DECL_INITIAL (parm
) = tinfo
;
297 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
);
301 tree t
= make_lang_type (TEMPLATE_TYPE_PARM
);
302 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
303 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
304 TYPE_NAME (t
) = decl
;
305 TYPE_STUB_DECL (t
) = decl
;
307 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
);
309 SET_DECL_ARTIFICIAL (decl
);
311 parm
= build_tree_list (defval
, parm
);
312 return chainon (list
, parm
);
315 /* The end of a template parameter list has been reached. Process the
316 tree list into a parameter vector, converting each parameter into a more
317 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
321 end_template_parm_list (parms
)
326 tree saved_parmlist
= make_tree_vec (list_length (parms
));
328 current_template_parms
329 = tree_cons (build_int_2 (0, processing_template_decl
),
330 saved_parmlist
, current_template_parms
);
332 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
333 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
335 return saved_parmlist
;
338 /* end_template_decl is called after a template declaration is seen. */
343 if (! processing_template_decl
)
346 /* This matches the pushlevel in begin_template_parm_list. */
349 --processing_template_decl
;
350 current_template_parms
= TREE_CHAIN (current_template_parms
);
351 (void) get_pending_sizes (); /* Why? */
354 /* Generate a valid set of template args from current_template_parms. */
357 current_template_args ()
359 tree header
= current_template_parms
;
360 int length
= list_length (header
);
361 tree args
= make_tree_vec (length
);
366 tree a
= copy_node (TREE_VALUE (header
));
367 int i
= TREE_VEC_LENGTH (a
);
368 TREE_TYPE (a
) = NULL_TREE
;
371 tree t
= TREE_VEC_ELT (a
, i
);
373 /* t will be a list if we are called from within a
374 begin/end_template_parm_list pair, but a vector directly
375 if within a begin/end_member_template_processing pair. */
376 if (TREE_CODE (t
) == TREE_LIST
)
380 if (TREE_CODE (t
) == TYPE_DECL
)
383 t
= DECL_INITIAL (t
);
386 TREE_VEC_ELT (a
, i
) = t
;
388 TREE_VEC_ELT (args
, --l
) = a
;
389 header
= TREE_CHAIN (header
);
396 push_template_decl (decl
)
400 tree args
= NULL_TREE
;
402 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
406 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
407 && DECL_CLASS_CONTEXT (decl
))
409 /* Note that this template is a "primary template" */
410 else if (! ctx
|| ! CLASSTYPE_TEMPLATE_INFO (ctx
)
411 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
414 /* Partial specialization. */
415 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
416 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
418 tree type
= TREE_TYPE (decl
);
419 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
420 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
421 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
423 for (; spec
; spec
= TREE_CHAIN (spec
))
425 /* purpose: args to main template
426 value: spec template */
427 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
431 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
432 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
433 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
434 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
438 args
= current_template_args ();
440 if (! ctx
|| TYPE_BEING_DEFINED (ctx
))
442 tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
443 DECL_TEMPLATE_PARMS (tmpl
) = current_template_parms
;
444 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
445 if (DECL_LANG_SPECIFIC (decl
))
446 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
453 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
454 cp_error ("must specialize `%#T' before defining member `%#D'",
456 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
457 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
458 else if (! DECL_TEMPLATE_INFO (decl
))
460 cp_error ("template definition of non-template `%#D'", decl
);
464 tmpl
= DECL_TI_TEMPLATE (decl
);
466 if (is_member_template (tmpl
))
468 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
469 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
470 if (TREE_VEC_LENGTH (t
)
471 != TREE_VEC_LENGTH (a
))
473 cp_error ("got %d template parameters for `%#D'",
474 TREE_VEC_LENGTH (a
), decl
);
475 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
477 if (TREE_VEC_LENGTH (args
) > 1)
478 /* Get the template parameters for the enclosing template
480 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
485 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
489 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
491 /* When processing an inline member template of a
492 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
493 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
494 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
496 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
497 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
499 /* There should be template arguments if and only if there is a
501 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
504 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
506 cp_error ("got %d template parameters for `%#D'",
507 TREE_VEC_LENGTH (a
), decl
);
508 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
511 /* Get the innermost set of template arguments. */
512 args
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
514 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
515 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
518 tmpl
= pushdecl_top_level (tmpl
);
521 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
523 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
525 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
527 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
528 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
530 else if (! DECL_LANG_SPECIFIC (decl
))
531 cp_error ("template declaration of `%#D'", decl
);
533 DECL_TEMPLATE_INFO (decl
) = info
;
536 /* Convert all template arguments to their appropriate types, and return
537 a vector containing the resulting values. If any error occurs, return
541 coerce_template_parms (parms
, arglist
, in_decl
)
545 int nparms
, nargs
, i
, lost
= 0;
548 if (arglist
== NULL_TREE
)
550 else if (TREE_CODE (arglist
) == TREE_VEC
)
551 nargs
= TREE_VEC_LENGTH (arglist
);
553 nargs
= list_length (arglist
);
555 nparms
= TREE_VEC_LENGTH (parms
);
559 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
561 error ("incorrect number of parameters (%d, should be %d)",
564 cp_error_at ("in template expansion for decl `%D'", in_decl
);
565 return error_mark_node
;
568 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
569 vec
= copy_node (arglist
);
572 vec
= make_tree_vec (nparms
);
573 for (i
= 0; i
< nparms
; i
++)
580 arglist
= TREE_CHAIN (arglist
);
582 if (arg
== error_mark_node
)
585 arg
= TREE_VALUE (arg
);
587 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
589 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
592 arg
= tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
595 TREE_VEC_ELT (vec
, i
) = arg
;
598 for (i
= 0; i
< nparms
; i
++)
600 tree arg
= TREE_VEC_ELT (vec
, i
);
601 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
603 int is_type
, requires_type
;
605 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
606 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
608 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
609 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
611 cp_pedwarn ("to refer to a type member of a template parameter,");
612 cp_pedwarn (" use `typename %E'", arg
);
613 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
614 TREE_OPERAND (arg
, 1));
617 if (is_type
!= requires_type
)
621 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
624 cp_error (" expected a constant of type `%T', got `%T'",
625 TREE_TYPE (parm
), arg
);
627 cp_error (" expected a type, got `%E'", arg
);
630 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
635 val
= groktypename (arg
);
636 if (! processing_template_decl
)
638 tree t
= target_type (val
);
640 && decl_function_context (TYPE_MAIN_DECL (t
)))
642 cp_error ("type `%T' composed from a local class is not a valid template-argument", val
);
643 return error_mark_node
;
649 tree t
= tsubst (TREE_TYPE (parm
), vec
,
650 TREE_VEC_LENGTH (vec
), in_decl
);
651 if (processing_template_decl
)
654 val
= digest_init (t
, arg
, (tree
*) 0);
656 if (val
== error_mark_node
|| processing_template_decl
)
659 /* 14.2: Other template-arguments must be constant-expressions,
660 addresses of objects or functions with external linkage, or of
661 static class members. */
662 else if (IS_AGGR_TYPE (TREE_TYPE (val
)))
664 cp_error ("object `%E' cannot be used as template argument", arg
);
665 val
= error_mark_node
;
667 else if (!TREE_CONSTANT (val
))
669 cp_error ("non-const `%E' cannot be used as template argument",
671 val
= error_mark_node
;
673 else if (POINTER_TYPE_P (TREE_TYPE (val
))
674 && ! integer_zerop (val
)
675 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
676 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
680 if (TREE_CODE (t
) == ADDR_EXPR
)
682 tree a
= TREE_OPERAND (t
, 0);
684 if (TREE_CODE (a
) == STRING_CST
)
686 cp_error ("string literal %E is not a valid template argument", a
);
687 error ("because it is the address of an object with static linkage");
688 val
= error_mark_node
;
690 else if (TREE_CODE (a
) != VAR_DECL
691 && TREE_CODE (a
) != FUNCTION_DECL
)
693 else if (! DECL_PUBLIC (a
))
695 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
696 val
= error_mark_node
;
702 cp_error ("`%E' is not a valid template argument", t
);
703 error ("it must be %s%s with external linkage",
704 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
705 ? "a pointer to " : "",
706 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
707 ? "a function" : "an object");
708 val
= error_mark_node
;
713 if (val
== error_mark_node
)
716 TREE_VEC_ELT (vec
, i
) = val
;
719 return error_mark_node
;
724 comp_template_args (oldargs
, newargs
)
725 tree oldargs
, newargs
;
729 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
731 tree nt
= TREE_VEC_ELT (newargs
, i
);
732 tree ot
= TREE_VEC_ELT (oldargs
, i
);
736 if (TREE_CODE (nt
) != TREE_CODE (ot
))
738 if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
740 if (comptypes (ot
, nt
, 1))
743 else if (cp_tree_equal (ot
, nt
) > 0)
750 /* Given class template name and parameter list, produce a user-friendly name
751 for the instantiation. */
754 mangle_class_name_for_template (name
, parms
, arglist
)
758 static struct obstack scratch_obstack
;
759 static char *scratch_firstobj
;
762 if (!scratch_firstobj
)
763 gcc_obstack_init (&scratch_obstack
);
765 obstack_free (&scratch_obstack
, scratch_firstobj
);
766 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
769 #define buflen sizeof(buf)
770 #define check if (bufp >= buf+buflen-1) goto too_long
771 #define ccat(c) *bufp++=(c); check
772 #define advance bufp+=strlen(bufp); check
773 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
776 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
778 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
783 nparms
= TREE_VEC_LENGTH (parms
);
784 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
785 for (i
= 0; i
< nparms
; i
++)
787 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
788 tree arg
= TREE_VEC_ELT (arglist
, i
);
793 if (TREE_CODE (parm
) == TYPE_DECL
)
795 cat (type_as_string (arg
, 0));
799 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
801 if (TREE_CODE (arg
) == TREE_LIST
)
803 /* New list cell was built because old chain link was in
805 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
806 arg
= TREE_VALUE (arg
);
808 /* No need to check arglist against parmlist here; we did that
809 in coerce_template_parms, called from lookup_template_class. */
810 cat (expr_as_string (arg
, 0));
813 char *bufp
= obstack_next_free (&scratch_obstack
);
815 while (bufp
[offset
- 1] == ' ')
817 obstack_blank_fast (&scratch_obstack
, offset
);
819 /* B<C<char> >, not B<C<char>> */
820 if (bufp
[offset
- 1] == '>')
825 return (char *) obstack_base (&scratch_obstack
);
830 fatal ("out of (preallocated) string space creating template instantiation name");
836 classtype_mangled_name (t
)
839 if (CLASSTYPE_TEMPLATE_INFO (t
)
840 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
842 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
843 char *mangled_name
= mangle_class_name_for_template
844 (IDENTIFIER_POINTER (name
),
845 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
846 CLASSTYPE_TI_ARGS (t
));
847 tree id
= get_identifier (mangled_name
);
848 IDENTIFIER_TEMPLATE (id
) = name
;
852 return TYPE_IDENTIFIER (t
);
856 add_pending_template (d
)
861 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
862 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
864 ti
= DECL_TEMPLATE_INFO (d
);
866 if (TI_PENDING_TEMPLATE_FLAG (ti
))
869 *template_tail
= perm_tree_cons
870 (current_function_decl
, d
, NULL_TREE
);
871 template_tail
= &TREE_CHAIN (*template_tail
);
872 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
875 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
876 parameters, find the desired type.
878 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
879 Since ARGLIST is build on the decl_obstack, we must copy it here
880 to keep it from being reclaimed when the decl storage is reclaimed.
882 IN_DECL, if non-NULL, is the template declaration we are trying to
886 lookup_template_class (d1
, arglist
, in_decl
)
890 tree
template, parmlist
;
894 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
896 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
898 template = IDENTIFIER_CLASS_VALUE (d1
);
900 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
902 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
903 d1
= DECL_NAME (template);
905 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
907 template = CLASSTYPE_TI_TEMPLATE (d1
);
908 d1
= DECL_NAME (template);
911 my_friendly_abort (272);
913 /* With something like `template <class T> class X class X { ... };'
914 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
915 We don't want to do that, but we have to deal with the situation, so
916 let's give them some syntax errors to chew on instead of a crash. */
918 return error_mark_node
;
919 if (TREE_CODE (template) != TEMPLATE_DECL
)
921 cp_error ("non-template type `%T' used as a template", d1
);
923 cp_error_at ("for template declaration `%D'", in_decl
);
924 return error_mark_node
;
927 if (PRIMARY_TEMPLATE_P (template))
929 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
931 arglist
= coerce_template_parms (parmlist
, arglist
, template);
932 if (arglist
== error_mark_node
)
933 return error_mark_node
;
934 if (uses_template_parms (arglist
))
937 if (comp_template_args
938 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
939 found
= TREE_TYPE (template);
942 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
943 found
; found
= TREE_CHAIN (found
))
945 if (TI_USES_TEMPLATE_PARMS (found
)
946 && comp_template_args (TREE_PURPOSE (found
), arglist
))
950 found
= TREE_VALUE (found
);
955 if (can_free (&permanent_obstack
, arglist
))
956 obstack_free (&permanent_obstack
, arglist
);
961 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
963 id
= get_identifier (mangled_name
);
964 IDENTIFIER_TEMPLATE (id
) = d1
;
966 maybe_push_to_top_level (uses_template_parms (arglist
));
967 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
968 pop_from_top_level ();
972 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
975 arglist
= CLASSTYPE_TI_ARGS (ctx
);
977 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
979 int save_temp
= processing_template_decl
;
980 processing_template_decl
= 0;
981 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
982 processing_template_decl
= save_temp
;
986 t
= lookup_nested_type_by_name (ctx
, id
);
987 my_friendly_assert (t
!= NULL_TREE
, 42);
991 /* Seems to be wanted. */
992 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
994 if (! CLASSTYPE_TEMPLATE_INFO (t
))
996 arglist
= copy_to_permanent (arglist
);
997 CLASSTYPE_TEMPLATE_INFO (t
)
998 = perm_tree_cons (template, arglist
, NULL_TREE
);
999 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1000 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
1001 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1002 = uses_template_parms (arglist
);
1004 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
1006 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1007 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
1008 if (! uses_template_parms (arglist
))
1009 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
1010 = get_identifier (build_overload_name (t
, 1, 1));
1012 if (flag_external_templates
&& ! uses_template_parms (arglist
)
1013 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1014 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1015 add_pending_template (t
);
1021 /* Should be defined in parse.h. */
1025 uses_template_parms (t
)
1030 switch (TREE_CODE (t
))
1034 /* We assume that the object must be instantiated in order to build
1035 the COMPONENT_REF, so we test only whether the type of the
1036 COMPONENT_REF uses template parms. */
1037 return uses_template_parms (TREE_TYPE (t
));
1039 case IDENTIFIER_NODE
:
1040 if (!IDENTIFIER_TEMPLATE (t
))
1042 my_friendly_abort (42);
1044 /* aggregates of tree nodes */
1047 int i
= TREE_VEC_LENGTH (t
);
1049 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
1054 if (uses_template_parms (TREE_PURPOSE (t
))
1055 || uses_template_parms (TREE_VALUE (t
)))
1057 return uses_template_parms (TREE_CHAIN (t
));
1059 /* constructed type nodes */
1061 case REFERENCE_TYPE
:
1062 return uses_template_parms (TREE_TYPE (t
));
1064 if (TYPE_PTRMEMFUNC_FLAG (t
))
1065 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1067 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1069 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
1071 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1073 return uses_template_parms (TREE_TYPE (t
));
1075 if (uses_template_parms (TYPE_DOMAIN (t
)))
1077 return uses_template_parms (TREE_TYPE (t
));
1079 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
1081 return uses_template_parms (TREE_TYPE (t
));
1083 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
1085 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1087 return uses_template_parms (TREE_TYPE (t
));
1091 return uses_template_parms (TREE_TYPE (t
));
1095 /* ??? What about FIELD_DECLs? */
1096 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
1097 && uses_template_parms (DECL_TI_ARGS (t
)))
1102 if (uses_template_parms (TREE_TYPE (t
)))
1104 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
1109 return uses_template_parms (TREE_TYPE (t
));
1111 return uses_template_parms (TREE_OPERAND (t
, 0));
1113 /* template parm nodes */
1114 case TEMPLATE_TYPE_PARM
:
1115 case TEMPLATE_CONST_PARM
:
1118 /* simple type nodes */
1120 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
1122 return uses_template_parms (TYPE_MAX_VALUE (t
));
1138 /* Non-error_mark_node ERROR_MARKs are bad things. */
1139 my_friendly_assert (t
== error_mark_node
, 274);
1148 return uses_template_parms (TREE_OPERAND (t
, 0));
1151 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
1152 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
1153 return uses_template_parms (TREE_OPERAND (t
, 1));
1157 case REINTERPRET_CAST_EXPR
:
1158 case CONST_CAST_EXPR
:
1159 case STATIC_CAST_EXPR
:
1160 case DYNAMIC_CAST_EXPR
:
1168 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
1176 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
1177 if (uses_template_parms (TREE_OPERAND (t
, i
)))
1184 sorry ("testing %s for template parms",
1185 tree_code_name
[(int) TREE_CODE (t
)]);
1186 my_friendly_abort (82);
1192 static struct tinst_level
*current_tinst_level
= 0;
1193 static struct tinst_level
*free_tinst_level
= 0;
1194 static int tinst_depth
= 0;
1195 extern int max_tinst_depth
;
1196 #ifdef GATHER_STATISTICS
1197 int depth_reached
= 0;
1201 push_tinst_level (d
)
1204 struct tinst_level
*new;
1206 if (tinst_depth
>= max_tinst_depth
)
1208 struct tinst_level
*p
= current_tinst_level
;
1210 char *file
= input_filename
;
1212 error ("template instantiation depth exceeds maximum of %d",
1214 error (" (use -ftemplate-depth-NN to increase the maximum)");
1215 cp_error (" instantiating `%D'", d
);
1217 for (; p
; p
= p
->next
)
1219 cp_error (" instantiated from `%D'", p
->decl
);
1221 input_filename
= p
->file
;
1223 error (" instantiated from here");
1226 input_filename
= file
;
1231 if (free_tinst_level
)
1233 new = free_tinst_level
;
1234 free_tinst_level
= new->next
;
1237 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
1241 new->file
= input_filename
;
1242 new->next
= current_tinst_level
;
1243 current_tinst_level
= new;
1246 #ifdef GATHER_STATISTICS
1247 if (tinst_depth
> depth_reached
)
1248 depth_reached
= tinst_depth
;
1257 struct tinst_level
*old
= current_tinst_level
;
1259 current_tinst_level
= old
->next
;
1260 old
->next
= free_tinst_level
;
1261 free_tinst_level
= old
;
1265 struct tinst_level
*
1268 struct tinst_level
*p
= current_tinst_level
;
1271 for (; p
->next
; p
= p
->next
)
1277 instantiate_class_template (type
)
1280 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
1282 if (type
== error_mark_node
)
1283 return error_mark_node
;
1285 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
1287 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
1290 template = TI_TEMPLATE (template_info
);
1291 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
1292 args
= TI_ARGS (template_info
);
1294 t
= most_specialized_class
1295 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
1297 if (t
== error_mark_node
)
1299 char *str
= "candidates are:";
1300 cp_error ("ambiguous class template instantiation for `%#T'", type
);
1301 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
1303 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
1305 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
1309 TYPE_BEING_DEFINED (type
) = 1;
1310 return error_mark_node
;
1313 pattern
= TREE_TYPE (t
);
1315 pattern
= TREE_TYPE (template);
1317 if (TYPE_SIZE (pattern
) == NULL_TREE
)
1321 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
1323 TYPE_BEING_DEFINED (type
) = 1;
1325 if (! push_tinst_level (type
))
1328 maybe_push_to_top_level (uses_template_parms (type
));
1329 pushclass (type
, 0);
1331 if (flag_external_templates
)
1333 if (flag_alt_external_templates
)
1335 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1336 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1337 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1338 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1339 && CLASSTYPE_INTERFACE_KNOWN (type
);
1343 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1344 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1345 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1346 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1347 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1348 && CLASSTYPE_INTERFACE_KNOWN (type
);
1353 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1354 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1357 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1358 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1359 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1360 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1361 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1362 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1363 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1364 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1365 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1366 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1367 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1368 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1369 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1370 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1371 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1372 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1373 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1374 TYPE_USES_COMPLEX_INHERITANCE (type
)
1375 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1376 TYPE_USES_MULTIPLE_INHERITANCE (type
)
1377 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
1378 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1379 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1380 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
1381 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
1384 tree binfo
= TYPE_BINFO (type
);
1385 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1391 int len
= TREE_VEC_LENGTH (pbases
);
1392 bases
= make_tree_vec (len
);
1393 for (i
= 0; i
< len
; ++i
)
1397 TREE_VEC_ELT (bases
, i
) = elt
1398 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
1399 TREE_VEC_LENGTH (args
), NULL_TREE
);
1400 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1402 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
1404 ("base type `%T' of `%T' fails to be a struct or class type",
1405 TREE_TYPE (elt
), type
);
1406 else if (! uses_template_parms (type
)
1407 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
1409 cp_error ("base class `%T' of `%T' has incomplete type",
1410 TREE_TYPE (elt
), type
);
1412 /* Don't initialize this until the vector is filled out, or
1413 lookups will crash. */
1414 BINFO_BASETYPES (binfo
) = bases
;
1418 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1420 field_chain
= &TYPE_FIELDS (type
);
1422 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1424 tree name
= TREE_PURPOSE (t
);
1425 tree tag
= TREE_VALUE (t
);
1427 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1428 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1430 tree e
, newtag
= tsubst_enum (tag
, args
,
1431 TREE_VEC_LENGTH (args
));
1433 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
1434 while (*field_chain
)
1436 DECL_FIELD_CONTEXT (*field_chain
) = type
;
1437 field_chain
= &TREE_CHAIN (*field_chain
);
1442 TREE_VEC_LENGTH (args
), NULL_TREE
);
1445 /* Don't replace enum constants here. */
1446 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1447 if (TREE_CODE (t
) != CONST_DECL
)
1449 tree r
= tsubst (t
, args
,
1450 TREE_VEC_LENGTH (args
), NULL_TREE
);
1451 if (TREE_CODE (r
) == VAR_DECL
)
1453 if (! uses_template_parms (r
))
1454 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1455 /* Perhaps I should do more of grokfield here. */
1457 DECL_IN_AGGR_P (r
) = 1;
1458 DECL_EXTERNAL (r
) = 1;
1459 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1463 field_chain
= &TREE_CHAIN (r
);
1466 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1467 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
1469 if (DECL_CONSTRUCTOR_P (t
))
1470 grok_ctor_properties (type
, t
);
1471 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
1472 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
1475 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1476 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
1477 args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
1480 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
1481 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
1482 TREE_VEC_LENGTH (args
), NULL_TREE
);
1484 /* This does injection for friend classes. */
1485 for (; d
; d
= TREE_CHAIN (d
))
1486 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
1488 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
1489 TREE_VEC_LENGTH (args
), NULL_TREE
);
1491 for (; d
; d
= TREE_CHAIN (d
))
1493 tree t
= TREE_VALUE (d
);
1495 if (TREE_CODE (t
) == TYPE_DECL
)
1496 /* Already injected. */;
1502 if (! uses_template_parms (type
))
1505 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1506 if (TREE_CODE (tmp
) == FIELD_DECL
)
1508 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
1509 require_complete_type (tmp
);
1512 type
= finish_struct_1 (type
, 0);
1513 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
1515 repo_template_used (type
);
1516 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
1517 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
1521 TYPE_SIZE (type
) = integer_zero_node
;
1522 CLASSTYPE_METHOD_VEC (type
)
1523 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1526 TYPE_BEING_DEFINED (type
) = 0;
1529 pop_from_top_level ();
1539 if (t1
== NULL_TREE
)
1540 return t2
== NULL_TREE
;
1541 if (t2
== NULL_TREE
)
1543 /* Don't care if one declares its arg const and the other doesn't -- the
1544 main variant of the arg type is all that matters. */
1545 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
1546 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
1548 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1552 lookup_nested_type_by_name (ctype
, name
)
1557 complete_type (ctype
);
1559 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1561 if (name
== TREE_PURPOSE (t
)
1562 /* this catches typedef enum { foo } bar; */
1563 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
1564 return TREE_VALUE (t
);
1570 tsubst (t
, args
, nargs
, in_decl
)
1577 if (t
== NULL_TREE
|| t
== error_mark_node
1578 || t
== integer_type_node
1579 || t
== void_type_node
1580 || t
== char_type_node
)
1583 type
= TREE_TYPE (t
);
1584 if (type
== unknown_type_node
)
1585 my_friendly_abort (42);
1586 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
1587 && TREE_CODE (t
) != TYPENAME_TYPE
)
1588 type
= tsubst (type
, args
, nargs
, in_decl
);
1590 switch (TREE_CODE (t
))
1593 if (TYPE_PTRMEMFUNC_P (t
))
1595 tree r
= build_ptrmemfunc_type
1596 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1597 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1601 /* else fall through */
1603 if (uses_template_parms (t
))
1605 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
1606 tree r
= lookup_template_class (t
, argvec
, in_decl
);
1607 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1611 /* else fall through */
1613 case IDENTIFIER_NODE
:
1626 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1627 if (ctx
== NULL_TREE
)
1629 else if (ctx
== current_function_decl
)
1630 return lookup_name (TYPE_IDENTIFIER (t
), 1);
1632 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
1636 if (t
== integer_type_node
)
1639 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1640 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
1644 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
1645 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
1646 if (processing_template_decl
)
1648 tree itype
= make_node (INTEGER_TYPE
);
1649 TYPE_MIN_VALUE (itype
) = size_zero_node
;
1650 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
1655 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
1656 return build_index_2_type (size_zero_node
, max
);
1659 case TEMPLATE_TYPE_PARM
:
1660 case TEMPLATE_CONST_PARM
:
1665 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
1667 idx
= TEMPLATE_TYPE_IDX (t
);
1668 level
= TEMPLATE_TYPE_LEVEL (t
);
1672 idx
= TEMPLATE_CONST_IDX (t
);
1673 level
= TEMPLATE_CONST_LEVEL (t
);
1676 if (TREE_VEC_LENGTH (args
) > 0)
1678 tree arg
= NULL_TREE
;
1680 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
1682 if (TREE_VEC_LENGTH (args
) >= level
- 1)
1684 (TREE_VEC_ELT (args
, level
- 1), idx
);
1686 else if (level
== 1)
1687 arg
= TREE_VEC_ELT (args
, idx
);
1689 if (arg
!= NULL_TREE
)
1691 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
1692 return cp_build_type_variant
1693 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
1694 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
1700 /* If we get here, we must have been looking at a parm for a
1701 more deeply nested template. */
1702 my_friendly_assert((TREE_CODE (t
) == TEMPLATE_CONST_PARM
1703 && TEMPLATE_CONST_LEVEL (t
) > 1)
1704 || (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
1705 && TEMPLATE_TYPE_LEVEL (t
) > 1),
1712 /* We can get here when processing a member template function
1713 of a template class. */
1715 tree decl
= DECL_TEMPLATE_RESULT (t
);
1720 /* We might already have an instance of this template. */
1721 tree instances
= DECL_TEMPLATE_INSTANTIATIONS (t
);
1722 tree ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, in_decl
);
1724 for (; instances
; instances
= TREE_CHAIN (instances
))
1725 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances
)) == ctx
)
1726 return TREE_VALUE (instances
);
1728 /* Make a new template decl. It will be similar to the
1729 original, but will record the current template arguments.
1730 We also create a new function declaration, which is just
1731 like the old one, but points to this new template, rather
1732 than the old one. */
1733 tmpl
= copy_node (t
);
1734 copy_lang_decl (tmpl
);
1735 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
1736 DECL_CHAIN (tmpl
) = NULL_TREE
;
1737 TREE_CHAIN (tmpl
) = NULL_TREE
;
1738 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
1739 new_decl
= tsubst (decl
, args
, nargs
, in_decl
);
1740 DECL_RESULT (tmpl
) = new_decl
;
1741 DECL_INITIAL (new_decl
) = DECL_INITIAL (decl
);
1742 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
1743 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
1744 DECL_TEMPLATE_INSTANTIATIONS(tmpl
) = NULL_TREE
;
1746 /* The template parameters for this new template are all the
1747 template parameters for the old template, except the
1748 outermost level of parameters. */
1749 DECL_TEMPLATE_PARMS (tmpl
)
1750 = copy_node (DECL_TEMPLATE_PARMS (tmpl
));
1751 for (parms
= DECL_TEMPLATE_PARMS (tmpl
);
1752 TREE_CHAIN (parms
) != NULL_TREE
;
1753 parms
= TREE_CHAIN (parms
))
1754 TREE_CHAIN (parms
) = copy_node (TREE_CHAIN (parms
));
1756 /* Record this partial instantiation. */
1757 DECL_TEMPLATE_INSTANTIATIONS (t
)
1758 = perm_tree_cons (NULL_TREE
, tmpl
,
1759 DECL_TEMPLATE_INSTANTIATIONS (t
));
1766 tree arg_types
, ctx
;
1770 if (DECL_CONTEXT (t
) != NULL_TREE
1771 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
1773 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
1777 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1778 type
= tsubst (type
, args
, nargs
, in_decl
);
1784 type
= tsubst (type
, args
, nargs
, in_decl
);
1787 if (type
== TREE_TYPE (t
)
1788 && (! member
|| ctx
== DECL_CLASS_CONTEXT (t
)))
1795 /* Do we already have this instantiation? */
1796 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1798 tree tmpl
= DECL_TI_TEMPLATE (t
);
1799 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1801 for (; decls
; decls
= TREE_CHAIN (decls
))
1802 if (TREE_TYPE (TREE_VALUE (decls
)) == type
1803 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1804 return TREE_VALUE (decls
);
1807 /* We do NOT check for matching decls pushed separately at this
1808 point, as they may not represent instantiations of this
1809 template, and in any case are considered separate under the
1810 discrete model. Instead, see add_maybe_template. */
1814 TREE_TYPE (r
) = type
;
1817 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
1818 DECL_CLASS_CONTEXT (r
) = ctx
;
1820 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
1821 IDENTIFIER_POINTER (DECL_NAME (r
)),
1822 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
1824 /* Type-conversion operator. Reconstruct the name, in
1825 case it's the name of one of the template's parameters. */
1826 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
1829 arg_types
= TYPE_VALUES (type
);
1831 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
1832 arg_types
= hash_tree_chain
1833 (build_pointer_type (DECL_CONTEXT (r
)), arg_types
);
1835 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
1837 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
1838 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
1839 buf
= (char *) alloca (strlen (dbuf
)
1840 + sizeof (DESTRUCTOR_DECL_PREFIX
));
1841 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
1844 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
1847 DECL_ASSEMBLER_NAME (r
)
1848 = build_decl_overload (DECL_NAME (r
), arg_types
, member
);
1850 make_decl_rtl (r
, NULL_PTR
, 1);
1852 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1853 DECL_MAIN_VARIANT (r
) = r
;
1854 DECL_RESULT (r
) = NULL_TREE
;
1855 DECL_INITIAL (r
) = NULL_TREE
;
1857 TREE_STATIC (r
) = 0;
1858 TREE_PUBLIC (r
) = 1;
1859 DECL_EXTERNAL (r
) = 1;
1860 DECL_INTERFACE_KNOWN (r
) = 0;
1861 DECL_DEFER_OUTPUT (r
) = 0;
1862 TREE_CHAIN (r
) = NULL_TREE
;
1863 DECL_CHAIN (r
) = NULL_TREE
;
1865 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
1866 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
1868 /* Look for matching decls for the moment. */
1869 if (! member
&& ! flag_ansi_overloading
)
1871 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
1874 if (decls
== NULL_TREE
)
1876 else if (is_overloaded_fn (decls
))
1877 for (decls
= get_first_fn (decls
); decls
;
1878 decls
= DECL_CHAIN (decls
))
1880 if (TREE_CODE (decls
) == FUNCTION_DECL
1881 && TREE_TYPE (decls
) == type
)
1890 int dcl_only
= ! DECL_INITIAL (d
);
1892 DECL_INITIAL (r
) = error_mark_node
;
1893 duplicate_decls (r
, d
);
1896 DECL_INITIAL (r
) = 0;
1900 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1902 tree tmpl
= DECL_TI_TEMPLATE (t
);
1903 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1904 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
1906 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
1907 argvec
= add_to_template_args (DECL_TI_ARGS (tmpl
), argvec
);
1909 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1910 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1912 /* If we have a preexisting version of this function, don't expand
1913 the template version, use the other instead. */
1914 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
1915 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
1917 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1919 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1920 = tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1923 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
1924 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
1926 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
1927 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
1934 tree r
= copy_node (t
);
1935 TREE_TYPE (r
) = type
;
1936 DECL_INITIAL (r
) = TREE_TYPE (r
);
1937 DECL_CONTEXT (r
) = NULL_TREE
;
1938 #ifdef PROMOTE_PROTOTYPES
1939 if ((TREE_CODE (type
) == INTEGER_TYPE
1940 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1941 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
1942 DECL_ARG_TYPE (r
) = integer_type_node
;
1945 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1951 tree r
= copy_node (t
);
1952 TREE_TYPE (r
) = type
;
1955 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
1957 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1958 TREE_CHAIN (r
) = NULL_TREE
;
1964 tree r
= copy_node (t
);
1966 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1967 TREE_CHAIN (r
) = NULL_TREE
;
1974 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1976 /* Do we already have this instantiation? */
1977 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1979 tree tmpl
= DECL_TI_TEMPLATE (t
);
1980 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1982 for (; decls
; decls
= TREE_CHAIN (decls
))
1983 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1984 return TREE_VALUE (decls
);
1988 TREE_TYPE (r
) = type
;
1989 DECL_CONTEXT (r
) = ctx
;
1990 if (TREE_STATIC (r
))
1991 DECL_ASSEMBLER_NAME (r
)
1992 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
1994 /* Don't try to expand the initializer until someone tries to use
1995 this variable; otherwise we run into circular dependencies. */
1996 DECL_INITIAL (r
) = NULL_TREE
;
2001 if (DECL_LANG_SPECIFIC (r
))
2004 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
2007 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2009 tree tmpl
= DECL_TI_TEMPLATE (t
);
2010 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2011 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2013 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2014 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2015 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2017 TREE_CHAIN (r
) = NULL_TREE
;
2022 if (t
== TYPE_NAME (TREE_TYPE (t
)))
2023 return TYPE_NAME (type
);
2026 tree r
= copy_node (t
);
2027 TREE_TYPE (r
) = type
;
2028 DECL_CONTEXT (r
) = current_class_type
;
2029 TREE_CHAIN (r
) = NULL_TREE
;
2035 tree purpose
, value
, chain
, result
;
2036 int via_public
, via_virtual
, via_protected
;
2038 if (t
== void_list_node
)
2041 via_public
= TREE_VIA_PUBLIC (t
);
2042 via_protected
= TREE_VIA_PROTECTED (t
);
2043 via_virtual
= TREE_VIA_VIRTUAL (t
);
2045 purpose
= TREE_PURPOSE (t
);
2047 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
2048 value
= TREE_VALUE (t
);
2050 value
= tsubst (value
, args
, nargs
, in_decl
);
2051 chain
= TREE_CHAIN (t
);
2052 if (chain
&& chain
!= void_type_node
)
2053 chain
= tsubst (chain
, args
, nargs
, in_decl
);
2054 if (purpose
== TREE_PURPOSE (t
)
2055 && value
== TREE_VALUE (t
)
2056 && chain
== TREE_CHAIN (t
))
2058 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
2059 purpose
, value
, chain
);
2060 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
2064 if (type
!= NULL_TREE
)
2068 if (type
== TREE_TYPE (t
))
2071 TREE_TYPE (t
) = complete_type (type
);
2072 if (IS_AGGR_TYPE (type
))
2074 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
2075 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
2076 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
2077 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
2082 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
2083 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
2085 bzero ((char *) elts
, len
* sizeof (tree
));
2087 for (i
= 0; i
< len
; i
++)
2089 elts
[i
] = tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
2090 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
2097 t
= make_tree_vec (len
);
2098 for (i
= 0; i
< len
; i
++)
2099 TREE_VEC_ELT (t
, i
) = elts
[i
];
2104 case REFERENCE_TYPE
:
2107 enum tree_code code
;
2108 if (type
== TREE_TYPE (t
))
2111 code
= TREE_CODE (t
);
2112 if (code
== POINTER_TYPE
)
2113 r
= build_pointer_type (type
);
2115 r
= build_reference_type (type
);
2116 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
2117 /* Will this ever be needed for TYPE_..._TO values? */
2122 return build_offset_type
2123 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
2127 tree values
= TYPE_ARG_TYPES (t
);
2128 tree context
= TYPE_CONTEXT (t
);
2129 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
2132 /* Don't bother recursing if we know it won't change anything. */
2133 if (values
!= void_list_node
)
2135 /* This should probably be rewritten to use hash_tree_cons for
2136 the memory savings. */
2137 tree first
= NULL_TREE
;
2140 for (; values
&& values
!= void_list_node
;
2141 values
= TREE_CHAIN (values
))
2143 tree value
= TYPE_MAIN_VARIANT (type_decays_to
2144 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
2145 /* Don't instantiate default args unless they are used.
2146 Handle it in build_over_call instead. */
2147 tree purpose
= TREE_PURPOSE (values
);
2148 tree x
= build_tree_list (purpose
, value
);
2151 TREE_CHAIN (last
) = x
;
2157 if (values
== void_list_node
)
2158 TREE_CHAIN (last
) = void_list_node
;
2163 context
= tsubst (context
, args
, nargs
, in_decl
);
2164 /* Could also optimize cases where return value and
2165 values have common elements (e.g., T min(const &T, const T&). */
2167 /* If the above parameters haven't changed, just return the type. */
2168 if (type
== TREE_TYPE (t
)
2169 && values
== TYPE_VALUES (t
)
2170 && context
== TYPE_CONTEXT (t
))
2173 /* Construct a new type node and return it. */
2174 if (TREE_CODE (t
) == FUNCTION_TYPE
2175 && context
== NULL_TREE
)
2177 fntype
= build_function_type (type
, values
);
2179 else if (context
== NULL_TREE
)
2181 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
2182 args
, nargs
, in_decl
);
2183 fntype
= build_cplus_method_type (base
, type
,
2184 TREE_CHAIN (values
));
2188 fntype
= make_node (TREE_CODE (t
));
2189 TREE_TYPE (fntype
) = type
;
2190 TYPE_CONTEXT (fntype
) = context
;
2191 TYPE_VALUES (fntype
) = values
;
2192 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
2193 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
2194 TYPE_MODE (fntype
) = TYPE_MODE (t
);
2195 if (TYPE_METHOD_BASETYPE (t
))
2196 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
2197 args
, nargs
, in_decl
);
2198 /* Need to generate hash value. */
2199 my_friendly_abort (84);
2201 fntype
= build_type_variant (fntype
,
2206 raises
= tsubst (raises
, args
, nargs
, in_decl
);
2207 fntype
= build_exception_variant (fntype
, raises
);
2213 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
2215 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
2217 r
= build_cplus_array_type (type
, domain
);
2223 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
2224 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2225 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
2229 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
2230 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
2234 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2235 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
2236 return cp_build_type_variant
2237 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
2238 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
2242 return make_pointer_declarator
2243 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2246 return make_reference_declarator
2247 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2250 return build_parse_node
2251 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2252 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2255 return make_call_declarator
2256 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2257 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2258 TREE_OPERAND (t
, 2),
2259 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
2262 return build_parse_node
2263 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2264 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2267 sorry ("use of `%s' in template",
2268 tree_code_name
[(int) TREE_CODE (t
)]);
2269 return error_mark_node
;
2276 emit_line_note (input_filename
, lineno
);
2280 expand_start_bindings (0);
2288 expand_end_bindings (getdecls (), kept_level_p (), 1);
2289 t
= poplevel (kept_level_p (), 1, 0);
2295 tsubst_copy (t
, args
, nargs
, in_decl
)
2300 enum tree_code code
;
2302 if (t
== NULL_TREE
|| t
== error_mark_node
)
2305 code
= TREE_CODE (t
);
2310 return do_identifier (DECL_NAME (t
), 0);
2314 if (DECL_CONTEXT (t
))
2316 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2317 if (ctx
== current_function_decl
)
2318 return lookup_name (DECL_NAME (t
), 0);
2319 else if (ctx
!= DECL_CONTEXT (t
))
2320 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
2326 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2327 t
= tsubst (t
, args
, nargs
, in_decl
);
2332 if (is_member_template (t
))
2333 return tsubst (t
, args
, nargs
, in_decl
);
2338 case IDENTIFIER_NODE
:
2339 return do_identifier (t
, 0);
2343 case REINTERPRET_CAST_EXPR
:
2344 case CONST_CAST_EXPR
:
2345 case STATIC_CAST_EXPR
:
2346 case DYNAMIC_CAST_EXPR
:
2348 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
2349 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2352 case PREDECREMENT_EXPR
:
2353 case PREINCREMENT_EXPR
:
2354 case POSTDECREMENT_EXPR
:
2355 case POSTINCREMENT_EXPR
:
2357 case TRUTH_NOT_EXPR
:
2360 case CONVERT_EXPR
: /* Unary + */
2367 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2372 case TRUNC_DIV_EXPR
:
2374 case FLOOR_DIV_EXPR
:
2375 case ROUND_DIV_EXPR
:
2376 case EXACT_DIV_EXPR
:
2378 case BIT_ANDTC_EXPR
:
2381 case TRUNC_MOD_EXPR
:
2382 case FLOOR_MOD_EXPR
:
2383 case TRUTH_ANDIF_EXPR
:
2384 case TRUTH_ORIF_EXPR
:
2385 case TRUTH_AND_EXPR
:
2406 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2407 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2411 tree fn
= TREE_OPERAND (t
, 0);
2412 if (really_overloaded_fn (fn
))
2413 fn
= tsubst_copy (TREE_VALUE (fn
), args
, nargs
, in_decl
);
2415 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
2417 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2421 case METHOD_CALL_EXPR
:
2423 tree name
= TREE_OPERAND (t
, 0);
2424 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2426 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2427 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
2429 else if (TREE_CODE (name
) == SCOPE_REF
2430 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
2432 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2433 name
= TREE_OPERAND (name
, 1);
2434 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2435 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
2436 name
= build_nt (SCOPE_REF
, base
, name
);
2439 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2441 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2442 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
2449 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2450 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2451 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
2456 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2457 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2458 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
2459 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
2466 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2467 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2468 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
2469 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
2475 tree purpose
, value
, chain
;
2477 if (t
== void_list_node
)
2480 purpose
= TREE_PURPOSE (t
);
2482 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
2483 value
= TREE_VALUE (t
);
2485 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
2486 chain
= TREE_CHAIN (t
);
2487 if (chain
&& chain
!= void_type_node
)
2488 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
2489 if (purpose
== TREE_PURPOSE (t
)
2490 && value
== TREE_VALUE (t
)
2491 && chain
== TREE_CHAIN (t
))
2493 return tree_cons (purpose
, value
, chain
);
2500 case TEMPLATE_TYPE_PARM
:
2501 case TEMPLATE_CONST_PARM
:
2503 case REFERENCE_TYPE
:
2509 return tsubst (t
, args
, nargs
, in_decl
);
2511 case IDENTIFIER_NODE
:
2512 if (IDENTIFIER_TYPENAME_P (t
))
2513 return build_typename_overload
2514 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
2520 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
2521 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
2529 tsubst_expr (t
, args
, nargs
, in_decl
)
2534 if (t
== NULL_TREE
|| t
== error_mark_node
)
2537 if (processing_template_decl
)
2538 return tsubst_copy (t
, args
, nargs
, in_decl
);
2540 switch (TREE_CODE (t
))
2543 lineno
= TREE_COMPLEXITY (t
);
2544 emit_line_note (input_filename
, lineno
);
2546 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2551 lineno
= TREE_COMPLEXITY (t
);
2552 emit_line_note (input_filename
, lineno
);
2553 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2554 /* Do default conversion if safe and possibly important,
2555 in case within ({...}). */
2556 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
2557 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
2558 t
= default_conversion (t
);
2559 cplus_expand_expr_stmt (t
);
2566 int i
= suspend_momentary ();
2569 lineno
= TREE_COMPLEXITY (t
);
2570 emit_line_note (input_filename
, lineno
);
2572 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2573 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2574 TREE_OPERAND (t
, 2) != 0);
2575 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
2577 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
2578 resume_momentary (i
);
2585 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
2586 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2587 int cond_scope
= (TREE_OPERAND (t
, 1)
2588 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
2590 lineno
= TREE_COMPLEXITY (t
);
2591 emit_line_note (input_filename
, lineno
);
2594 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
2595 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2597 emit_line_note (input_filename
, lineno
);
2598 expand_start_loop_continue_elsewhere (1);
2602 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2603 emit_line_note (input_filename
, lineno
);
2605 expand_exit_loop_if_false (0, condition_conversion (tmp
));
2609 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
2612 emit_line_note (input_filename
, lineno
);
2613 expand_loop_continue_here ();
2614 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
2616 cplus_expand_expr_stmt (tmp
);
2629 lineno
= TREE_COMPLEXITY (t
);
2631 emit_line_note (input_filename
, lineno
);
2632 expand_start_loop (1);
2634 cond
= TREE_OPERAND (t
, 0);
2635 if (TREE_CODE (cond
) == DECL_STMT
)
2637 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
2638 emit_line_note (input_filename
, lineno
);
2639 expand_exit_loop_if_false (0, condition_conversion (cond
));
2641 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
2643 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2655 lineno
= TREE_COMPLEXITY (t
);
2657 emit_line_note (input_filename
, lineno
);
2658 expand_start_loop_continue_elsewhere (1);
2660 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2661 expand_loop_continue_here ();
2663 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2664 emit_line_note (input_filename
, lineno
);
2665 expand_exit_loop_if_false (0, condition_conversion (cond
));
2676 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2678 lineno
= TREE_COMPLEXITY (t
);
2681 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2682 emit_line_note (input_filename
, lineno
);
2683 expand_start_cond (condition_conversion (tmp
), 0);
2685 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2686 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2688 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
2690 expand_start_else ();
2691 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2705 tree substmt
= TREE_OPERAND (t
, 0);
2707 lineno
= TREE_COMPLEXITY (t
);
2709 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2712 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
2713 tsubst_expr (substmt
, args
, nargs
, in_decl
);
2715 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2721 lineno
= TREE_COMPLEXITY (t
);
2722 emit_line_note (input_filename
, lineno
);
2723 if (! expand_exit_something ())
2724 error ("break statement not within loop or switch");
2728 lineno
= TREE_COMPLEXITY (t
);
2729 emit_line_note (input_filename
, lineno
);
2730 if (! expand_continue_loop (0))
2731 error ("continue statement not within a loop");
2737 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2739 lineno
= TREE_COMPLEXITY (t
);
2742 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2743 emit_line_note (input_filename
, lineno
);
2744 c_expand_start_case (val
);
2747 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2748 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2750 expand_end_case (val
);
2761 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2762 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2766 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
2773 lineno
= TREE_COMPLEXITY (t
);
2774 emit_line_note (input_filename
, lineno
);
2775 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
2777 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
2778 TREE_USED (decl
) = 1;
2782 expand_computed_goto
2783 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2787 lineno
= TREE_COMPLEXITY (t
);
2788 emit_line_note (input_filename
, lineno
);
2789 expand_start_try_stmts ();
2790 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2791 expand_start_all_catch ();
2793 tree handler
= TREE_OPERAND (t
, 1);
2794 for (; handler
; handler
= TREE_CHAIN (handler
))
2795 tsubst_expr (handler
, args
, nargs
, in_decl
);
2797 expand_end_all_catch ();
2801 lineno
= TREE_COMPLEXITY (t
);
2803 if (TREE_OPERAND (t
, 0))
2805 tree d
= TREE_OPERAND (t
, 0);
2806 expand_start_catch_block
2807 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
2808 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
2811 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
2812 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2813 expand_end_catch_block ();
2818 lineno
= TREE_COMPLEXITY (t
);
2820 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
2821 tsubst_enum (t
, args
, nargs
);
2825 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
2831 instantiate_template (tmpl
, targ_ptr
)
2832 tree tmpl
, targ_ptr
;
2836 struct obstack
*old_fmp_obstack
;
2837 extern struct obstack
*function_maybepermanent_obstack
;
2839 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2840 old_fmp_obstack
= function_maybepermanent_obstack
;
2841 function_maybepermanent_obstack
= &permanent_obstack
;
2843 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
2844 len
= DECL_NTPARMS (tmpl
);
2849 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
2850 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
2852 tree nt
= target_type (t
);
2853 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
2855 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
2856 cp_error (" trying to instantiate `%D'", tmpl
);
2857 fndecl
= error_mark_node
;
2861 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
2864 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
2865 targ_ptr
= add_to_template_args (DECL_TI_ARGS (tmpl
), targ_ptr
);
2867 /* substitute template parameters */
2868 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
2870 if (flag_external_templates
)
2871 add_pending_template (fndecl
);
2874 function_maybepermanent_obstack
= old_fmp_obstack
;
2880 /* Push the name of the class template into the scope of the instantiation. */
2883 overload_template_name (type
)
2886 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
2889 if (IDENTIFIER_CLASS_VALUE (id
)
2890 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
2893 decl
= build_decl (TYPE_DECL
, id
, type
);
2894 SET_DECL_ARTIFICIAL (decl
);
2895 pushdecl_class_level (decl
);
2898 /* Like type_unfication but designed specially to handle conversion
2902 fn_type_unification (fn
, targs
, args
, return_type
, strict
)
2903 tree fn
, targs
, args
, return_type
;
2907 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2908 tree decl_arg_types
= args
;
2910 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
2912 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
2914 /* This is a template conversion operator. Use the return types
2915 as well as the argument types. */
2916 fn_arg_types
= tree_cons (NULL_TREE
,
2917 TREE_TYPE (TREE_TYPE (fn
)),
2919 decl_arg_types
= tree_cons (NULL_TREE
,
2924 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
2925 &TREE_VEC_ELT (targs
, 0),
2934 /* Type unification.
2936 We have a function template signature with one or more references to
2937 template parameters, and a parameter list we wish to fit to this
2938 template. If possible, produce a list of parameters for the template
2939 which will cause it to fit the supplied parameter list.
2941 Return zero for success, 2 for an incomplete match that doesn't resolve
2942 all the types, and 1 for complete failure. An error message will be
2943 printed only for an incomplete match.
2945 TPARMS[NTPARMS] is an array of template parameter types;
2946 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2947 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2948 and ARGS is the argument list we're trying to match against it.
2950 If SUBR is 1, we're being called recursively (to unify the arguments of
2951 a function or method parameter of a function template), so don't zero
2952 out targs and don't fail on an incomplete match.
2954 If STRICT is 1, the match must be exact (for casts of overloaded
2955 addresses, explicit instantiation, and more_specialized). */
2958 type_unification (tparms
, targs
, parms
, args
, nsubsts
, subr
, strict
)
2959 tree tparms
, *targs
, parms
, args
;
2960 int *nsubsts
, subr
, strict
;
2964 int ntparms
= TREE_VEC_LENGTH (tparms
);
2966 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
2967 my_friendly_assert (TREE_CODE (parms
) == TREE_LIST
, 290);
2968 /* ARGS could be NULL (via a call from parse.y to
2969 build_x_function_call). */
2971 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
2972 my_friendly_assert (ntparms
> 0, 292);
2975 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
2978 && parms
!= void_list_node
2980 && args
!= void_list_node
)
2982 parm
= TREE_VALUE (parms
);
2983 parms
= TREE_CHAIN (parms
);
2984 arg
= TREE_VALUE (args
);
2985 args
= TREE_CHAIN (args
);
2987 if (arg
== error_mark_node
)
2989 if (arg
== unknown_type_node
)
2992 /* Conversions will be performed on a function argument that
2993 corresponds with a function parameter that contains only
2994 non-deducible template parameters and explicitly specified
2995 template parameters. */
2996 if (! uses_template_parms (parm
))
3000 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
3001 type
= TREE_TYPE (arg
);
3010 if (comptypes (parm
, type
, 1))
3015 if (can_convert_arg (parm
, type
, arg
))
3020 if (can_convert (parm
, type
))
3028 if (TREE_CODE (arg
) == VAR_DECL
)
3029 arg
= TREE_TYPE (arg
);
3030 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
3031 arg
= TREE_TYPE (arg
);
3033 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
3035 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
3036 if (TREE_CODE (arg
) == TREE_LIST
3037 && TREE_TYPE (arg
) == unknown_type_node
3038 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
3040 int nsubsts
, ntparms
;
3043 /* Have to back unify here */
3044 arg
= TREE_VALUE (arg
);
3046 ntparms
= DECL_NTPARMS (arg
);
3047 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
3048 parm
= tree_cons (NULL_TREE
, parm
, NULL_TREE
);
3049 return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
3051 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
3052 parm
, &nsubsts
, 0, strict
);
3054 arg
= TREE_TYPE (arg
);
3057 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
3058 arg
= TREE_TYPE (arg
);
3060 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
3062 if (TREE_CODE (arg
) == FUNCTION_TYPE
3063 || TREE_CODE (arg
) == METHOD_TYPE
)
3064 arg
= build_pointer_type (arg
);
3065 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
3066 arg
= build_pointer_type (TREE_TYPE (arg
));
3068 arg
= TYPE_MAIN_VARIANT (arg
);
3071 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
3079 /* Fail if we've reached the end of the parm list, and more args
3080 are present, and the parm list isn't variadic. */
3081 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
3083 /* Fail if parms are left and they don't have default values. */
3085 && parms
!= void_list_node
3086 && TREE_PURPOSE (parms
) == NULL_TREE
)
3089 for (i
= 0; i
< ntparms
; i
++)
3092 error ("incomplete type unification");
3098 /* Tail recursion is your friend. */
3101 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
3102 tree tparms
, *targs
, parm
, arg
;
3103 int *nsubsts
, ntparms
, strict
;
3107 /* I don't think this will do the right thing with respect to types.
3108 But the only case I've seen it in so far has been array bounds, where
3109 signedness is the only information lost, and I think that will be
3111 while (TREE_CODE (parm
) == NOP_EXPR
)
3112 parm
= TREE_OPERAND (parm
, 0);
3114 if (arg
== error_mark_node
)
3116 if (arg
== unknown_type_node
)
3121 switch (TREE_CODE (parm
))
3123 case TEMPLATE_TYPE_PARM
:
3125 idx
= TEMPLATE_TYPE_IDX (parm
);
3126 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
3127 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
3130 /* Template type parameters cannot contain cv-quals; i.e.
3131 template <class T> void f (T& a, T& b) will not generate
3132 void f (const int& a, const int& b). */
3133 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
3134 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
3136 arg
= TYPE_MAIN_VARIANT (arg
);
3139 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
3140 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
3141 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
3144 /* Simple cases: Value already set, does match or doesn't. */
3145 if (targs
[idx
] == arg
)
3147 else if (targs
[idx
])
3149 /* Check for mixed types and values. */
3150 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
3154 case TEMPLATE_CONST_PARM
:
3156 idx
= TEMPLATE_CONST_IDX (parm
);
3159 int i
= cp_tree_equal (targs
[idx
], arg
);
3165 my_friendly_abort (42);
3168 targs
[idx
] = copy_to_permanent (arg
);
3172 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
3173 return unify (tparms
, targs
, ntparms
, parm
,
3174 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
3176 if (TREE_CODE (arg
) != POINTER_TYPE
)
3178 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
3181 case REFERENCE_TYPE
:
3182 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
3183 arg
= TREE_TYPE (arg
);
3184 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
3188 if (TREE_CODE (arg
) != ARRAY_TYPE
)
3190 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
3191 nsubsts
, strict
) != 0)
3193 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
3200 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3203 if (TREE_CODE (parm
) == INTEGER_TYPE
)
3205 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
3206 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
3207 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
3209 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
3210 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
3211 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
3214 else if (TREE_CODE (parm
) == REAL_TYPE
3215 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
3218 /* As far as unification is concerned, this wins. Later checks
3219 will invalidate it if necessary. */
3222 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
3223 /* Type INTEGER_CST can come from ordinary constant template args. */
3225 while (TREE_CODE (arg
) == NOP_EXPR
)
3226 arg
= TREE_OPERAND (arg
, 0);
3228 if (TREE_CODE (arg
) != INTEGER_CST
)
3230 return !tree_int_cst_equal (parm
, arg
);
3235 t1
= TREE_OPERAND (parm
, 0);
3236 t2
= TREE_OPERAND (parm
, 1);
3237 return unify (tparms
, targs
, ntparms
, t1
,
3238 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
3245 if (TREE_CODE (arg
) != TREE_VEC
)
3247 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
3249 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
3250 if (unify (tparms
, targs
, ntparms
,
3251 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
3258 if (TYPE_PTRMEMFUNC_FLAG (parm
))
3259 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
3260 arg
, nsubsts
, strict
);
3262 /* Allow trivial conversions. */
3263 if (TREE_CODE (arg
) != RECORD_TYPE
3264 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
3265 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
3268 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
3271 if (flag_ansi_overloading
&& ! strict
)
3272 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
3274 (CLASSTYPE_TEMPLATE_INFO (arg
)
3275 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
3277 if (! t
|| t
== error_mark_node
)
3280 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
3281 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
3283 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
3288 if (TREE_CODE (arg
) != METHOD_TYPE
)
3293 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
3296 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
3297 TREE_TYPE (arg
), nsubsts
, strict
))
3299 return type_unification (tparms
, targs
, TYPE_ARG_TYPES (parm
),
3300 TYPE_ARG_TYPES (arg
), nsubsts
, 1, strict
);
3303 if (TREE_CODE (arg
) != OFFSET_TYPE
)
3305 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
3306 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
3308 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
3309 TREE_TYPE (arg
), nsubsts
, strict
);
3312 if (arg
!= decl_constant_value (parm
))
3317 sorry ("use of `%s' in template type unification",
3318 tree_code_name
[(int) TREE_CODE (parm
)]);
3324 mark_decl_instantiated (result
, extern_p
)
3328 if (DECL_TEMPLATE_INSTANTIATION (result
))
3329 SET_DECL_EXPLICIT_INSTANTIATION (result
);
3330 TREE_PUBLIC (result
) = 1;
3334 DECL_INTERFACE_KNOWN (result
) = 1;
3335 DECL_NOT_REALLY_EXTERN (result
) = 1;
3337 else if (TREE_CODE (result
) == FUNCTION_DECL
)
3338 mark_inline_for_output (result
);
3341 /* Given two function templates PAT1 and PAT2, return:
3343 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
3344 -1 if PAT2 is more specialized than PAT1.
3345 0 if neither is more specialized. */
3348 more_specialized (pat1
, pat2
)
3354 targs
= get_bindings (pat1
, pat2
);
3360 targs
= get_bindings (pat2
, pat1
);
3369 /* Given two class template specialization list nodes PAT1 and PAT2, return:
3371 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
3372 -1 if PAT2 is more specialized than PAT1.
3373 0 if neither is more specialized. */
3376 more_specialized_class (pat1
, pat2
)
3382 targs
= get_class_bindings
3383 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
3387 targs
= get_class_bindings
3388 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
3395 /* Return the template arguments that will produce the function signature
3396 DECL from the function template FN. */
3399 get_bindings (fn
, decl
)
3402 int ntparms
= DECL_NTPARMS (fn
);
3403 tree targs
= make_tree_vec (ntparms
);
3406 i
= fn_type_unification (fn
, targs
,
3407 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
3408 TREE_TYPE (TREE_TYPE (decl
)),
3417 get_class_bindings (tparms
, parms
, args
)
3418 tree tparms
, parms
, args
;
3420 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
3421 tree vec
= make_temp_vec (ntparms
);
3423 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
3425 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
3426 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
3436 for (i
= 0; i
< ntparms
; ++i
)
3437 if (! TREE_VEC_ELT (vec
, i
))
3443 /* Return the most specialized of the list of templates in FNS that can
3444 produce an instantiation matching DECL. */
3447 most_specialized (fns
, decl
)
3450 tree fn
, champ
, args
, *p
;
3453 for (p
= &fns
; *p
; )
3455 args
= get_bindings (TREE_VALUE (*p
), decl
);
3458 p
= &TREE_CHAIN (*p
);
3461 *p
= TREE_CHAIN (*p
);
3468 champ
= TREE_VALUE (fn
);
3469 fn
= TREE_CHAIN (fn
);
3470 for (; fn
; fn
= TREE_CHAIN (fn
))
3472 fate
= more_specialized (champ
, TREE_VALUE (fn
));
3479 fn
= TREE_CHAIN (fn
);
3481 return error_mark_node
;
3483 champ
= TREE_VALUE (fn
);
3487 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
3489 fate
= more_specialized (champ
, TREE_VALUE (fn
));
3491 return error_mark_node
;
3497 /* Return the most specialized of the class template specializations in
3498 SPECS that can produce an instantiation matching ARGS. */
3501 most_specialized_class (specs
, mainargs
)
3502 tree specs
, mainargs
;
3504 tree list
= NULL_TREE
, t
, args
, champ
;
3507 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
3509 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
3512 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
3513 TREE_TYPE (list
) = TREE_TYPE (t
);
3523 for (; t
; t
= TREE_CHAIN (t
))
3525 fate
= more_specialized_class (champ
, t
);
3534 return error_mark_node
;
3540 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
3542 fate
= more_specialized (champ
, t
);
3544 return error_mark_node
;
3550 /* called from the parser. */
3553 do_decl_instantiation (declspecs
, declarator
, storage
)
3554 tree declspecs
, declarator
, storage
;
3556 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
3559 tree result
= NULL_TREE
;
3561 tree templates
= NULL_TREE
;
3563 if (! DECL_LANG_SPECIFIC (decl
))
3565 cp_error ("explicit instantiation of non-template `%#D'", decl
);
3569 /* If we've already seen this template instance, use it. */
3570 if (TREE_CODE (decl
) == VAR_DECL
)
3572 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
3573 if (result
&& TREE_CODE (result
) != VAR_DECL
)
3576 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
3578 cp_error ("explicit instantiation of `%#D'", decl
);
3581 else if (DECL_FUNCTION_MEMBER_P (decl
))
3583 if (DECL_TEMPLATE_INSTANTIATION (decl
))
3585 else if (name
= DECL_ASSEMBLER_NAME (decl
),
3586 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
3587 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
3591 /* Maybe this is an instantiation of a member template
3593 tree ctype
= DECL_CONTEXT (decl
);
3595 name
= DECL_NAME (decl
);
3596 fn
= lookup_fnfields (TYPE_BINFO (ctype
), name
, 1);
3598 fn
= TREE_VALUE (fn
);
3600 for (; fn
; fn
= DECL_CHAIN (fn
))
3601 if (decls_match (fn
, decl
) && DECL_DEFER_OUTPUT (fn
))
3606 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3607 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
3610 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
3612 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
3613 if (decls_match (fn
, decl
) && DECL_DEFER_OUTPUT (fn
))
3618 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3619 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
3622 if (templates
&& !result
)
3625 result
= most_specialized (templates
, decl
);
3626 if (result
== error_mark_node
)
3628 char *str
= "candidates are:";
3629 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
3630 for (fn
= templates
; fn
; fn
= TREE_CHAIN (fn
))
3632 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
3639 args
= get_bindings (result
, decl
);
3640 result
= instantiate_template (result
, args
);
3646 cp_error ("no matching template for `%D' found", decl
);
3650 if (! DECL_TEMPLATE_INFO (result
))
3652 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
3656 if (flag_external_templates
)
3659 if (storage
== NULL_TREE
)
3661 else if (storage
== ridpointers
[(int) RID_EXTERN
])
3664 cp_error ("storage class `%D' applied to template instantiation",
3667 mark_decl_instantiated (result
, extern_p
);
3668 repo_template_instantiated (result
, extern_p
);
3670 instantiate_decl (result
);
3674 mark_class_instantiated (t
, extern_p
)
3678 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
3679 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
3680 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
3681 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
3682 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
3685 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
3686 rest_of_type_compilation (t
, 1);
3691 do_type_instantiation (t
, storage
)
3698 if (TREE_CODE (t
) == TYPE_DECL
)
3701 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
3703 cp_error ("explicit instantiation of non-template type `%T'", t
);
3709 /* With -fexternal-templates, explicit instantiations are treated the same
3710 as implicit ones. */
3711 if (flag_external_templates
)
3714 if (TYPE_SIZE (t
) == NULL_TREE
)
3716 cp_error ("explicit instantiation of `%#T' before definition of template",
3721 if (storage
== NULL_TREE
)
3723 else if (storage
== ridpointers
[(int) RID_INLINE
])
3725 else if (storage
== ridpointers
[(int) RID_EXTERN
])
3727 else if (storage
== ridpointers
[(int) RID_STATIC
])
3731 cp_error ("storage class `%D' applied to template instantiation",
3736 /* We've already instantiated this. */
3737 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
3741 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3743 mark_class_instantiated (t
, extern_p
);
3744 repo_template_instantiated (t
, extern_p
);
3754 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
3755 if (DECL_TEMPLATE_INSTANTIATION (tmp
))
3757 mark_decl_instantiated (tmp
, extern_p
);
3758 repo_template_instantiated (tmp
, extern_p
);
3760 instantiate_decl (tmp
);
3763 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
3764 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
3766 mark_decl_instantiated (tmp
, extern_p
);
3767 repo_template_instantiated (tmp
, extern_p
);
3769 instantiate_decl (tmp
);
3772 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
3773 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
3774 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
3779 instantiate_decl (d
)
3782 tree ti
= DECL_TEMPLATE_INFO (d
);
3783 tree tmpl
= TI_TEMPLATE (ti
);
3784 tree args
= TI_ARGS (ti
);
3786 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
3788 int nested
= in_function_p ();
3790 int pattern_defined
;
3792 char *file
= input_filename
;
3794 if (TREE_CODE (d
) == FUNCTION_DECL
)
3796 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
3797 pattern_defined
= (DECL_INITIAL (pattern
) != NULL_TREE
);
3801 d_defined
= ! DECL_IN_AGGR_P (d
);
3802 pattern_defined
= ! DECL_IN_AGGR_P (pattern
);
3808 /* This needs to happen before any tsubsting. */
3809 if (! push_tinst_level (d
))
3812 push_to_top_level ();
3813 lineno
= DECL_SOURCE_LINE (d
);
3814 input_filename
= DECL_SOURCE_FILE (d
);
3816 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
3817 variable is a static const initialized in the class body. */
3818 if (TREE_CODE (d
) == VAR_DECL
3819 && ! DECL_INITIAL (d
) && DECL_INITIAL (pattern
))
3821 pushclass (DECL_CONTEXT (d
), 2);
3822 DECL_INITIAL (d
) = tsubst_expr
3823 (DECL_INITIAL (pattern
), args
,
3824 TREE_VEC_LENGTH (args
), tmpl
);
3828 /* import_export_decl has to happen after DECL_INITIAL is set up. */
3829 if (pattern_defined
)
3831 repo_template_used (d
);
3833 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
3835 if (flag_alt_external_templates
)
3837 if (interface_unknown
)
3838 warn_if_unknown_interface (d
);
3840 else if (DECL_INTERFACE_KNOWN (pattern
))
3842 DECL_INTERFACE_KNOWN (d
) = 1;
3843 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (pattern
);
3846 warn_if_unknown_interface (pattern
);
3850 import_export_decl (d
);
3853 if (! pattern_defined
3854 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! DECL_INLINE (d
)
3855 && (! DECL_INTERFACE_KNOWN (d
)
3856 || ! DECL_NOT_REALLY_EXTERN (d
)))
3857 /* Kludge: if we compile a constructor in the middle of processing a
3858 toplevel declaration, we blow away the declspecs in
3859 temp_decl_obstack when we call permanent_allocation in
3860 finish_function. So don't compile it yet. */
3861 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! nested
&& ! at_eof
))
3863 add_pending_template (d
);
3867 lineno
= DECL_SOURCE_LINE (d
);
3868 input_filename
= DECL_SOURCE_FILE (d
);
3870 /* Trick tsubst into giving us a new decl in case the template changed. */
3871 save_ti
= DECL_TEMPLATE_INFO (pattern
);
3872 DECL_TEMPLATE_INFO (pattern
) = NULL_TREE
;
3873 td
= tsubst (pattern
, args
, TREE_VEC_LENGTH (args
), tmpl
);
3874 DECL_TEMPLATE_INFO (pattern
) = save_ti
;
3876 /* And set up DECL_INITIAL, since tsubst doesn't. */
3877 if (TREE_CODE (td
) == VAR_DECL
)
3879 pushclass (DECL_CONTEXT (d
), 2);
3880 DECL_INITIAL (td
) = tsubst_expr
3881 (DECL_INITIAL (pattern
), args
,
3882 TREE_VEC_LENGTH (args
), tmpl
);
3886 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
3887 if (TREE_CODE (d
) == FUNCTION_DECL
)
3888 DECL_INITIAL (td
) = error_mark_node
;
3889 duplicate_decls (td
, d
);
3890 if (TREE_CODE (d
) == FUNCTION_DECL
)
3891 DECL_INITIAL (td
) = 0;
3893 if (TREE_CODE (d
) == VAR_DECL
)
3895 DECL_IN_AGGR_P (d
) = 0;
3896 if (DECL_INTERFACE_KNOWN (d
))
3897 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
3900 DECL_EXTERNAL (d
) = 1;
3901 DECL_NOT_REALLY_EXTERN (d
) = 1;
3903 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
3905 else if (TREE_CODE (d
) == FUNCTION_DECL
)
3907 tree t
= DECL_SAVED_TREE (pattern
);
3909 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
3910 store_parm_decls ();
3912 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
3915 (TREE_OPERAND (t
, 0),
3916 tsubst_expr (TREE_OPERAND (t
, 1), args
,
3917 TREE_VEC_LENGTH (args
), tmpl
));
3921 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
3923 current_member_init_list
3924 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
3925 current_base_init_list
3926 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
3931 /* Always keep the BLOCK node associated with the outermost
3932 pair of curley braces of a function. These are needed
3933 for correct operation of dwarfout.c. */
3936 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
3937 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
3939 finish_function (lineno
, 0, nested
);
3944 input_filename
= file
;
3946 pop_from_top_level ();
3953 tsubst_chain (t
, argvec
)
3958 tree first
= tsubst (t
, argvec
,
3959 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3962 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
3964 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3965 TREE_CHAIN (last
) = x
;
3975 tsubst_expr_values (t
, argvec
)
3978 tree first
= NULL_TREE
;
3981 for (; t
; t
= TREE_CHAIN (t
))
3983 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
3984 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3985 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
3986 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3987 *p
= build_tree_list (pur
, val
);
3988 p
= &TREE_CHAIN (*p
);
3999 last_tree
= TREE_CHAIN (last_tree
) = t
;
4002 /* D is an undefined function declaration in the presence of templates with
4003 the same name, listed in FNS. If one of them can produce D as an
4004 instantiation, remember this so we can instantiate it at EOF if D has
4005 not been defined by that time. */
4008 add_maybe_template (d
, fns
)
4013 if (DECL_MAYBE_TEMPLATE (d
))
4016 t
= most_specialized (fns
, d
);
4019 if (t
== error_mark_node
)
4021 cp_error ("ambiguous template instantiation for `%D'", d
);
4025 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
4026 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
4027 DECL_MAYBE_TEMPLATE (d
) = 1;
4030 /* Instantiate an enumerated type. Used by instantiate_class_template and
4034 tsubst_enum (tag
, args
, nargs
)
4038 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
4039 tree e
, values
= NULL_TREE
;
4041 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
4043 tree elt
= build_enumerator (TREE_PURPOSE (e
),
4044 tsubst_expr (TREE_VALUE (e
), args
,
4046 TREE_CHAIN (elt
) = values
;
4050 finish_enum (newtag
, values
);