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". */
44 extern struct obstack permanent_obstack
;
47 extern char *input_filename
;
48 struct pending_inline
*pending_template_expansions
;
50 tree current_template_parms
;
51 HOST_WIDE_INT processing_template_decl
;
53 tree pending_templates
;
54 static tree
*template_tail
= &pending_templates
;
57 static tree
*maybe_template_tail
= &maybe_templates
;
59 int minimal_parse_mode
;
61 #define obstack_chunk_alloc xmalloc
62 #define obstack_chunk_free free
66 void pop_template_decls ();
68 static tree
classtype_mangled_name ();
69 static char * mangle_class_name_for_template ();
70 static tree
tsubst_expr_values ();
71 static int comp_template_args
PROTO((tree
, tree
));
72 tree most_specialized_class
PROTO((tree
, tree
));
73 static tree get_class_bindings
PROTO((tree
, tree
, tree
));
74 tree make_temp_vec
PROTO((int));
75 static tree tsubst_enum
PROTO((tree
, tree
*, int));
77 /* We've got a template header coming up; push to a new level for storing
81 begin_template_parm_list ()
84 declare_pseudo_global_level ();
85 ++processing_template_decl
;
88 /* Process information from new template parameter NEXT and append it to the
92 process_template_parm (list
, next
)
100 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
101 defval
= TREE_PURPOSE (parm
);
102 parm
= TREE_VALUE (parm
);
103 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
107 tree p
= TREE_VALUE (tree_last (list
));
109 if (TREE_CODE (p
) == TYPE_DECL
)
110 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
112 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
121 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
122 /* is a const-param */
123 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
125 /* A template parameter is not modifiable. */
126 TREE_READONLY (parm
) = 1;
127 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
128 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
130 cp_error ("`%#T' is not a valid type for a template constant parameter",
132 if (DECL_NAME (parm
) == NULL_TREE
)
133 error (" a template type parameter must begin with `class' or `typename'");
134 TREE_TYPE (parm
) = void_type_node
;
137 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
138 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
139 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
141 tinfo
= make_node (TEMPLATE_CONST_PARM
);
142 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
143 if (TREE_PERMANENT (parm
) == 0)
145 parm
= copy_node (parm
);
146 TREE_PERMANENT (parm
) = 1;
148 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
149 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
150 DECL_INITIAL (decl
) = tinfo
;
151 DECL_INITIAL (parm
) = tinfo
;
152 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
);
156 tree t
= make_lang_type (TEMPLATE_TYPE_PARM
);
157 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
158 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
159 TYPE_NAME (t
) = decl
;
160 TYPE_STUB_DECL (t
) = decl
;
162 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
);
164 SET_DECL_ARTIFICIAL (decl
);
166 parm
= build_tree_list (defval
, parm
);
167 return chainon (list
, parm
);
170 /* The end of a template parameter list has been reached. Process the
171 tree list into a parameter vector, converting each parameter into a more
172 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
176 end_template_parm_list (parms
)
181 tree saved_parmlist
= make_tree_vec (list_length (parms
));
183 current_template_parms
184 = tree_cons (build_int_2 (0, processing_template_decl
),
185 saved_parmlist
, current_template_parms
);
187 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
188 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
190 return saved_parmlist
;
193 /* end_template_decl is called after a template declaration is seen. */
198 if (! processing_template_decl
)
201 /* This matches the pushlevel in begin_template_parm_list. */
204 --processing_template_decl
;
205 current_template_parms
= TREE_CHAIN (current_template_parms
);
206 (void) get_pending_sizes (); /* Why? */
209 /* Generate a valid set of template args from current_template_parms. */
212 current_template_args ()
214 tree header
= current_template_parms
;
215 tree args
= NULL_TREE
;
218 tree a
= copy_node (TREE_VALUE (header
));
219 int i
= TREE_VEC_LENGTH (a
);
220 TREE_TYPE (a
) = NULL_TREE
;
223 tree t
= TREE_VALUE (TREE_VEC_ELT (a
, i
));
224 if (TREE_CODE (t
) == TYPE_DECL
)
227 t
= DECL_INITIAL (t
);
228 TREE_VEC_ELT (a
, i
) = t
;
230 args
= tree_cons (TREE_PURPOSE (header
), a
, args
);
231 header
= TREE_CHAIN (header
);
233 args
= nreverse (args
);
235 /* FIXME Remove this when we support member templates. */
236 args
= TREE_VALUE (args
);
242 push_template_decl (decl
)
246 tree args
= NULL_TREE
;
248 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
252 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
253 && DECL_CLASS_CONTEXT (decl
))
255 /* Note that this template is a "primary template" */
256 else if (! ctx
|| ! CLASSTYPE_TEMPLATE_INFO (ctx
)
257 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
260 /* Partial specialization. */
261 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
262 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
264 tree type
= TREE_TYPE (decl
);
265 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
266 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
267 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
269 for (; spec
; spec
= TREE_CHAIN (spec
))
271 /* purpose: args to main template
272 value: spec template */
273 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
277 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
278 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
279 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
280 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
284 args
= current_template_args ();
286 if (! ctx
|| TYPE_BEING_DEFINED (ctx
))
288 tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
289 DECL_TEMPLATE_PARMS (tmpl
) = TREE_VALUE (current_template_parms
);
290 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
296 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
297 cp_error ("must specialize `%#T' before defining member `%#D'",
299 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
300 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
301 else if (! DECL_TEMPLATE_INFO (decl
))
303 cp_error ("template definition of non-template `%#D'", decl
);
307 tmpl
= DECL_TI_TEMPLATE (decl
);
309 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
310 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
312 t
= DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
314 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (args
))
316 cp_error ("got %d template parameters for `%#D'",
317 TREE_VEC_LENGTH (args
), decl
);
318 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
322 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
323 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
326 tmpl
= pushdecl_top_level (tmpl
);
329 TREE_TYPE (DECL_TEMPLATE_PARMS (tmpl
)) = tmpl
;
331 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
333 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
335 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
336 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
338 else if (! DECL_LANG_SPECIFIC (decl
))
339 cp_error ("template declaration of `%#D'", decl
);
341 DECL_TEMPLATE_INFO (decl
) = info
;
344 tree tsubst
PROTO ((tree
, tree
*, int, tree
));
346 /* Convert all template arguments to their appropriate types, and return
347 a vector containing the resulting values. If any error occurs, return
351 coerce_template_parms (parms
, arglist
, in_decl
)
355 int nparms
, nargs
, i
, lost
= 0;
358 if (arglist
== NULL_TREE
)
360 else if (TREE_CODE (arglist
) == TREE_VEC
)
361 nargs
= TREE_VEC_LENGTH (arglist
);
363 nargs
= list_length (arglist
);
365 nparms
= TREE_VEC_LENGTH (parms
);
369 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
371 error ("incorrect number of parameters (%d, should be %d)",
374 cp_error_at ("in template expansion for decl `%D'", in_decl
);
375 return error_mark_node
;
378 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
379 vec
= copy_node (arglist
);
382 vec
= make_tree_vec (nparms
);
383 for (i
= 0; i
< nparms
; i
++)
390 arglist
= TREE_CHAIN (arglist
);
392 if (arg
== error_mark_node
)
395 arg
= TREE_VALUE (arg
);
397 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
399 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
400 &TREE_VEC_ELT (vec
, 0), i
, in_decl
);
402 arg
= tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
403 &TREE_VEC_ELT (vec
, 0), i
, in_decl
);
405 TREE_VEC_ELT (vec
, i
) = arg
;
408 for (i
= 0; i
< nparms
; i
++)
410 tree arg
= TREE_VEC_ELT (vec
, i
);
411 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
413 int is_type
, requires_type
;
415 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
416 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
418 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
419 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
421 cp_pedwarn ("to refer to a type member of a template parameter,");
422 cp_pedwarn (" use `typename %E'", arg
);
423 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
424 TREE_OPERAND (arg
, 1));
427 if (is_type
!= requires_type
)
431 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
434 cp_error (" expected a constant of type `%T', got `%T'",
435 TREE_TYPE (parm
), arg
);
437 cp_error (" expected a type, got `%E'", arg
);
440 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
445 val
= groktypename (arg
);
446 if (! processing_template_decl
)
448 tree t
= target_type (val
);
450 && decl_function_context (TYPE_MAIN_DECL (t
)))
452 cp_error ("type `%T' composed from a local class is not a valid template-argument", val
);
453 return error_mark_node
;
459 tree t
= tsubst (TREE_TYPE (parm
), &TREE_VEC_ELT (vec
, 0),
460 TREE_VEC_LENGTH (vec
), in_decl
);
461 if (processing_template_decl
)
464 val
= digest_init (t
, arg
, (tree
*) 0);
466 if (val
== error_mark_node
|| processing_template_decl
)
469 /* 14.2: Other template-arguments must be constant-expressions,
470 addresses of objects or functions with external linkage, or of
471 static class members. */
472 else if (IS_AGGR_TYPE (TREE_TYPE (val
)))
474 cp_error ("object `%E' cannot be used as template argument", arg
);
475 val
= error_mark_node
;
477 else if (!TREE_CONSTANT (val
))
479 cp_error ("non-const `%E' cannot be used as template argument",
481 val
= error_mark_node
;
483 else if (POINTER_TYPE_P (TREE_TYPE (val
))
484 && ! integer_zerop (val
)
485 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
486 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
490 if (TREE_CODE (t
) == ADDR_EXPR
)
492 tree a
= TREE_OPERAND (t
, 0);
494 if (TREE_CODE (a
) == STRING_CST
)
496 cp_error ("string literal %E is not a valid template argument", a
);
497 error ("because it is the address of an object with static linkage");
498 val
= error_mark_node
;
500 else if (TREE_CODE (a
) != VAR_DECL
501 && TREE_CODE (a
) != FUNCTION_DECL
)
503 else if (! DECL_PUBLIC (a
))
505 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
506 val
= error_mark_node
;
512 cp_error ("`%E' is not a valid template argument", t
);
513 error ("it must be %s%s with external linkage",
514 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
515 ? "a pointer to " : "",
516 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
517 ? "a function" : "an object");
518 val
= error_mark_node
;
523 if (val
== error_mark_node
)
526 TREE_VEC_ELT (vec
, i
) = val
;
529 return error_mark_node
;
534 comp_template_args (oldargs
, newargs
)
535 tree oldargs
, newargs
;
539 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
541 tree nt
= TREE_VEC_ELT (newargs
, i
);
542 tree ot
= TREE_VEC_ELT (oldargs
, i
);
546 if (TREE_CODE (nt
) != TREE_CODE (ot
))
548 if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
550 if (comptypes (ot
, nt
, 1))
553 else if (cp_tree_equal (ot
, nt
) > 0)
560 /* Given class template name and parameter list, produce a user-friendly name
561 for the instantiation. */
564 mangle_class_name_for_template (name
, parms
, arglist
)
568 static struct obstack scratch_obstack
;
569 static char *scratch_firstobj
;
572 if (!scratch_firstobj
)
573 gcc_obstack_init (&scratch_obstack
);
575 obstack_free (&scratch_obstack
, scratch_firstobj
);
576 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
579 #define buflen sizeof(buf)
580 #define check if (bufp >= buf+buflen-1) goto too_long
581 #define ccat(c) *bufp++=(c); check
582 #define advance bufp+=strlen(bufp); check
583 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
586 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
588 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
593 nparms
= TREE_VEC_LENGTH (parms
);
594 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
595 for (i
= 0; i
< nparms
; i
++)
597 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
598 tree arg
= TREE_VEC_ELT (arglist
, i
);
603 if (TREE_CODE (parm
) == TYPE_DECL
)
605 cat (type_as_string (arg
, 0));
609 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
611 if (TREE_CODE (arg
) == TREE_LIST
)
613 /* New list cell was built because old chain link was in
615 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
616 arg
= TREE_VALUE (arg
);
618 /* No need to check arglist against parmlist here; we did that
619 in coerce_template_parms, called from lookup_template_class. */
620 cat (expr_as_string (arg
, 0));
623 char *bufp
= obstack_next_free (&scratch_obstack
);
625 while (bufp
[offset
- 1] == ' ')
627 obstack_blank_fast (&scratch_obstack
, offset
);
629 /* B<C<char> >, not B<C<char>> */
630 if (bufp
[offset
- 1] == '>')
635 return (char *) obstack_base (&scratch_obstack
);
640 fatal ("out of (preallocated) string space creating template instantiation name");
646 classtype_mangled_name (t
)
649 if (CLASSTYPE_TEMPLATE_INFO (t
)
650 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
652 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
653 char *mangled_name
= mangle_class_name_for_template
654 (IDENTIFIER_POINTER (name
),
655 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
656 CLASSTYPE_TI_ARGS (t
));
657 tree id
= get_identifier (mangled_name
);
658 IDENTIFIER_TEMPLATE (id
) = name
;
662 return TYPE_IDENTIFIER (t
);
666 add_pending_template (d
)
671 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
672 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
674 ti
= DECL_TEMPLATE_INFO (d
);
676 if (TI_PENDING_TEMPLATE_FLAG (ti
))
679 *template_tail
= perm_tree_cons
680 (current_function_decl
, d
, NULL_TREE
);
681 template_tail
= &TREE_CHAIN (*template_tail
);
682 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
685 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
686 parameters, find the desired type.
688 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
689 Since ARGLIST is build on the decl_obstack, we must copy it here
690 to keep it from being reclaimed when the decl storage is reclaimed.
692 IN_DECL, if non-NULL, is the template declaration we are trying to
696 lookup_template_class (d1
, arglist
, in_decl
)
700 tree
template, parmlist
;
704 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
706 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
708 template = IDENTIFIER_CLASS_VALUE (d1
);
710 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
712 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
713 d1
= DECL_NAME (template);
715 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
717 template = CLASSTYPE_TI_TEMPLATE (d1
);
718 d1
= DECL_NAME (template);
721 my_friendly_abort (272);
723 /* With something like `template <class T> class X class X { ... };'
724 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
725 We don't want to do that, but we have to deal with the situation, so
726 let's give them some syntax errors to chew on instead of a crash. */
728 return error_mark_node
;
729 if (TREE_CODE (template) != TEMPLATE_DECL
)
731 cp_error ("non-template type `%T' used as a template", d1
);
733 cp_error_at ("for template declaration `%D'", in_decl
);
734 return error_mark_node
;
737 if (PRIMARY_TEMPLATE_P (template))
739 parmlist
= DECL_TEMPLATE_PARMS (template);
741 arglist
= coerce_template_parms (parmlist
, arglist
, template);
742 if (arglist
== error_mark_node
)
743 return error_mark_node
;
744 if (uses_template_parms (arglist
))
747 if (comp_template_args
748 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
749 found
= TREE_TYPE (template);
752 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
753 found
; found
= TREE_CHAIN (found
))
755 if (TI_USES_TEMPLATE_PARMS (found
)
756 && comp_template_args (TREE_PURPOSE (found
), arglist
))
760 found
= TREE_VALUE (found
);
765 if (can_free (&permanent_obstack
, arglist
))
766 obstack_free (&permanent_obstack
, arglist
);
771 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
773 id
= get_identifier (mangled_name
);
774 IDENTIFIER_TEMPLATE (id
) = d1
;
776 maybe_push_to_top_level (uses_template_parms (arglist
));
777 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
778 pop_from_top_level ();
782 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
785 arglist
= CLASSTYPE_TI_ARGS (ctx
);
787 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
789 int save_temp
= processing_template_decl
;
790 processing_template_decl
= 0;
791 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
792 processing_template_decl
= save_temp
;
796 t
= lookup_nested_type_by_name (ctx
, id
);
797 my_friendly_assert (t
!= NULL_TREE
, 42);
801 /* Seems to be wanted. */
802 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
804 if (! CLASSTYPE_TEMPLATE_INFO (t
))
806 arglist
= copy_to_permanent (arglist
);
807 CLASSTYPE_TEMPLATE_INFO (t
)
808 = perm_tree_cons (template, arglist
, NULL_TREE
);
809 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
810 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
811 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
812 = uses_template_parms (arglist
);
814 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
816 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
817 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
818 if (! uses_template_parms (arglist
))
819 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
820 = get_identifier (build_overload_name (t
, 1, 1));
822 if (flag_external_templates
&& ! uses_template_parms (arglist
)
823 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
824 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
825 add_pending_template (t
);
831 /* Should be defined in parse.h. */
835 uses_template_parms (t
)
840 switch (TREE_CODE (t
))
844 /* We assume that the object must be instantiated in order to build
845 the COMPONENT_REF, so we test only whether the type of the
846 COMPONENT_REF uses template parms. */
847 return uses_template_parms (TREE_TYPE (t
));
849 case IDENTIFIER_NODE
:
850 if (!IDENTIFIER_TEMPLATE (t
))
852 my_friendly_abort (42);
854 /* aggregates of tree nodes */
857 int i
= TREE_VEC_LENGTH (t
);
859 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
864 if (uses_template_parms (TREE_PURPOSE (t
))
865 || uses_template_parms (TREE_VALUE (t
)))
867 return uses_template_parms (TREE_CHAIN (t
));
869 /* constructed type nodes */
872 return uses_template_parms (TREE_TYPE (t
));
874 if (TYPE_PTRMEMFUNC_FLAG (t
))
875 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
877 if (! CLASSTYPE_TEMPLATE_INFO (t
))
879 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
881 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
883 return uses_template_parms (TREE_TYPE (t
));
885 if (uses_template_parms (TYPE_DOMAIN (t
)))
887 return uses_template_parms (TREE_TYPE (t
));
889 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
891 return uses_template_parms (TREE_TYPE (t
));
893 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
895 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
897 return uses_template_parms (TREE_TYPE (t
));
901 return uses_template_parms (TREE_TYPE (t
));
905 /* ??? What about FIELD_DECLs? */
906 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
907 && uses_template_parms (DECL_TI_ARGS (t
)))
912 if (uses_template_parms (TREE_TYPE (t
)))
914 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
919 return uses_template_parms (TREE_TYPE (t
));
921 return uses_template_parms (TREE_OPERAND (t
, 0));
923 /* template parm nodes */
924 case TEMPLATE_TYPE_PARM
:
925 case TEMPLATE_CONST_PARM
:
928 /* simple type nodes */
930 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
932 return uses_template_parms (TYPE_MAX_VALUE (t
));
948 /* Non-error_mark_node ERROR_MARKs are bad things. */
949 my_friendly_assert (t
== error_mark_node
, 274);
958 return uses_template_parms (TREE_OPERAND (t
, 0));
961 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
962 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
963 return uses_template_parms (TREE_OPERAND (t
, 1));
967 case REINTERPRET_CAST_EXPR
:
968 case CONST_CAST_EXPR
:
969 case STATIC_CAST_EXPR
:
970 case DYNAMIC_CAST_EXPR
:
978 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
986 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
987 if (uses_template_parms (TREE_OPERAND (t
, i
)))
994 sorry ("testing %s for template parms",
995 tree_code_name
[(int) TREE_CODE (t
)]);
996 my_friendly_abort (82);
1002 static struct tinst_level
*current_tinst_level
= 0;
1003 static struct tinst_level
*free_tinst_level
= 0;
1004 static int tinst_depth
= 0;
1005 extern int max_tinst_depth
;
1006 #ifdef GATHER_STATISTICS
1007 int depth_reached
= 0;
1011 push_tinst_level (d
)
1014 struct tinst_level
*new;
1016 if (tinst_depth
>= max_tinst_depth
)
1018 struct tinst_level
*p
= current_tinst_level
;
1020 char *file
= input_filename
;
1022 error ("template instantiation depth exceeds maximum of %d",
1024 error (" (use -ftemplate-depth-NN to increase the maximum)");
1025 cp_error (" instantiating `%D'", d
);
1027 for (; p
; p
= p
->next
)
1029 cp_error (" instantiated from `%D'", p
->decl
);
1031 input_filename
= p
->file
;
1033 error (" instantiated from here");
1036 input_filename
= file
;
1041 if (free_tinst_level
)
1043 new = free_tinst_level
;
1044 free_tinst_level
= new->next
;
1047 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
1051 new->file
= input_filename
;
1052 new->next
= current_tinst_level
;
1053 current_tinst_level
= new;
1056 #ifdef GATHER_STATISTICS
1057 if (tinst_depth
> depth_reached
)
1058 depth_reached
= tinst_depth
;
1067 struct tinst_level
*old
= current_tinst_level
;
1069 current_tinst_level
= old
->next
;
1070 old
->next
= free_tinst_level
;
1071 free_tinst_level
= old
;
1075 struct tinst_level
*
1078 struct tinst_level
*p
= current_tinst_level
;
1081 for (; p
->next
; p
= p
->next
)
1087 instantiate_class_template (type
)
1090 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
1092 if (type
== error_mark_node
)
1093 return error_mark_node
;
1095 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
1097 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
1100 template = TI_TEMPLATE (template_info
);
1101 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
1102 args
= TI_ARGS (template_info
);
1104 t
= most_specialized_class
1105 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
1107 if (t
== error_mark_node
)
1109 char *str
= "candidates are:";
1110 cp_error ("ambiguous class template instantiation for `%#T'", type
);
1111 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
1113 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
1115 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
1119 TYPE_BEING_DEFINED (type
) = 1;
1120 return error_mark_node
;
1123 pattern
= TREE_TYPE (t
);
1125 pattern
= TREE_TYPE (template);
1127 if (TYPE_SIZE (pattern
) == NULL_TREE
)
1131 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
1133 TYPE_BEING_DEFINED (type
) = 1;
1135 if (! push_tinst_level (type
))
1138 maybe_push_to_top_level (uses_template_parms (type
));
1139 pushclass (type
, 0);
1141 if (flag_external_templates
)
1143 if (flag_alt_external_templates
)
1145 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1146 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1147 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1148 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1149 && CLASSTYPE_INTERFACE_KNOWN (type
);
1153 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1154 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1155 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1156 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1157 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1158 && CLASSTYPE_INTERFACE_KNOWN (type
);
1163 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1164 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1167 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1168 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1169 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1170 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1171 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1172 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1173 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1174 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1175 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1176 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1177 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1178 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1179 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1180 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1181 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1182 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1183 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1184 TYPE_USES_COMPLEX_INHERITANCE (type
)
1185 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1186 TYPE_USES_MULTIPLE_INHERITANCE (type
)
1187 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
1188 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1189 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1190 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
1191 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
1194 tree binfo
= TYPE_BINFO (type
);
1195 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1201 int len
= TREE_VEC_LENGTH (pbases
);
1202 bases
= make_tree_vec (len
);
1203 for (i
= 0; i
< len
; ++i
)
1207 TREE_VEC_ELT (bases
, i
) = elt
1208 = tsubst (TREE_VEC_ELT (pbases
, i
), &TREE_VEC_ELT (args
, 0),
1209 TREE_VEC_LENGTH (args
), NULL_TREE
);
1210 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1212 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
1214 ("base type `%T' of `%T' fails to be a struct or class type",
1215 TREE_TYPE (elt
), type
);
1216 else if (! uses_template_parms (type
)
1217 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
1219 cp_error ("base class `%T' of `%T' has incomplete type",
1220 TREE_TYPE (elt
), type
);
1222 /* Don't initialize this until the vector is filled out, or
1223 lookups will crash. */
1224 BINFO_BASETYPES (binfo
) = bases
;
1228 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1230 field_chain
= &TYPE_FIELDS (type
);
1232 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1234 tree name
= TREE_PURPOSE (t
);
1235 tree tag
= TREE_VALUE (t
);
1237 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1238 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1240 tree e
, newtag
= tsubst_enum (tag
, &TREE_VEC_ELT (args
, 0),
1241 TREE_VEC_LENGTH (args
));
1243 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
1244 while (*field_chain
)
1246 DECL_FIELD_CONTEXT (*field_chain
) = type
;
1247 field_chain
= &TREE_CHAIN (*field_chain
);
1251 tsubst (tag
, &TREE_VEC_ELT (args
, 0),
1252 TREE_VEC_LENGTH (args
), NULL_TREE
);
1255 /* Don't replace enum constants here. */
1256 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1257 if (TREE_CODE (t
) != CONST_DECL
)
1259 tree r
= tsubst (t
, &TREE_VEC_ELT (args
, 0),
1260 TREE_VEC_LENGTH (args
), NULL_TREE
);
1261 if (TREE_CODE (r
) == VAR_DECL
)
1263 if (! uses_template_parms (r
))
1264 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1265 /* Perhaps I should do more of grokfield here. */
1267 DECL_IN_AGGR_P (r
) = 1;
1268 DECL_EXTERNAL (r
) = 1;
1269 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1273 field_chain
= &TREE_CHAIN (r
);
1276 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1277 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
1279 if (DECL_CONSTRUCTOR_P (t
))
1280 grok_ctor_properties (type
, t
);
1281 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
1282 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
1285 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1286 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
1287 &TREE_VEC_ELT (args
, 0), TREE_VEC_LENGTH (args
), NULL_TREE
);
1290 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
1291 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), &TREE_VEC_ELT (args
, 0),
1292 TREE_VEC_LENGTH (args
), NULL_TREE
);
1294 /* This does injection for friend classes. */
1295 for (; d
; d
= TREE_CHAIN (d
))
1296 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
1298 d
= tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args
, 0),
1299 TREE_VEC_LENGTH (args
), NULL_TREE
);
1301 for (; d
; d
= TREE_CHAIN (d
))
1303 tree t
= TREE_VALUE (d
);
1305 if (TREE_CODE (t
) == TYPE_DECL
)
1306 /* Already injected. */;
1312 if (! uses_template_parms (type
))
1315 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1316 if (TREE_CODE (tmp
) == FIELD_DECL
)
1318 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
1319 require_complete_type (tmp
);
1322 type
= finish_struct_1 (type
, 0);
1323 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
1324 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
1325 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
1327 repo_template_used (type
);
1331 TYPE_SIZE (type
) = integer_zero_node
;
1332 CLASSTYPE_METHOD_VEC (type
)
1333 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1336 TYPE_BEING_DEFINED (type
) = 0;
1339 pop_from_top_level ();
1349 if (t1
== NULL_TREE
)
1350 return t2
== NULL_TREE
;
1351 if (t2
== NULL_TREE
)
1353 /* Don't care if one declares its arg const and the other doesn't -- the
1354 main variant of the arg type is all that matters. */
1355 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
1356 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
1358 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1362 lookup_nested_type_by_name (ctype
, name
)
1367 complete_type (ctype
);
1369 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1371 if (name
== TREE_PURPOSE (t
))
1372 return TREE_VALUE (t
);
1378 tsubst (t
, args
, nargs
, in_decl
)
1385 if (t
== NULL_TREE
|| t
== error_mark_node
1386 || t
== integer_type_node
1387 || t
== void_type_node
1388 || t
== char_type_node
)
1391 type
= TREE_TYPE (t
);
1392 if (type
== unknown_type_node
)
1393 my_friendly_abort (42);
1394 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
1395 && TREE_CODE (t
) != TYPENAME_TYPE
)
1396 type
= tsubst (type
, args
, nargs
, in_decl
);
1398 switch (TREE_CODE (t
))
1401 if (TYPE_PTRMEMFUNC_P (t
))
1403 tree r
= build_ptrmemfunc_type
1404 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1405 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1409 /* else fall through */
1411 if (uses_template_parms (t
))
1413 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
1414 tree r
= lookup_template_class (t
, argvec
, in_decl
);
1415 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1419 /* else fall through */
1421 case IDENTIFIER_NODE
:
1434 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1435 if (ctx
== NULL_TREE
)
1437 else if (ctx
== current_function_decl
)
1438 return lookup_name (TYPE_IDENTIFIER (t
), 1);
1440 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
1444 if (t
== integer_type_node
)
1447 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1448 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
1452 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
1453 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
1454 if (processing_template_decl
)
1456 tree itype
= make_node (INTEGER_TYPE
);
1457 TYPE_MIN_VALUE (itype
) = size_zero_node
;
1458 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
1463 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
1464 return build_index_2_type (size_zero_node
, max
);
1467 case TEMPLATE_TYPE_PARM
:
1469 tree arg
= args
[TEMPLATE_TYPE_IDX (t
)];
1470 return cp_build_type_variant
1471 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
1472 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
1475 case TEMPLATE_CONST_PARM
:
1476 return args
[TEMPLATE_CONST_IDX (t
)];
1481 tree arg_types
, ctx
;
1485 if (DECL_CONTEXT (t
) != NULL_TREE
1486 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
1488 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
1492 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1493 type
= tsubst (type
, args
, nargs
, in_decl
);
1499 type
= tsubst (type
, args
, nargs
, in_decl
);
1502 if (type
== TREE_TYPE (t
)
1503 && (! member
|| ctx
== DECL_CLASS_CONTEXT (t
)))
1510 /* Do we already have this instantiation? */
1511 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1513 tree tmpl
= TREE_PURPOSE (DECL_TEMPLATE_INFO (t
));
1514 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1516 for (; decls
; decls
= TREE_CHAIN (decls
))
1517 if (TREE_TYPE (TREE_VALUE (decls
)) == type
1518 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1519 return TREE_VALUE (decls
);
1522 /* We do NOT check for matching decls pushed separately at this
1523 point, as they may not represent instantiations of this
1524 template, and in any case are considered separate under the
1525 discrete model. Instead, see add_maybe_template. */
1529 TREE_TYPE (r
) = type
;
1532 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
1533 DECL_CLASS_CONTEXT (r
) = ctx
;
1535 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
1536 IDENTIFIER_POINTER (DECL_NAME (r
)),
1537 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
1539 /* Type-conversion operator. Reconstruct the name, in
1540 case it's the name of one of the template's parameters. */
1541 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
1544 arg_types
= TYPE_VALUES (type
);
1546 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
1547 arg_types
= hash_tree_chain
1548 (build_pointer_type (DECL_CONTEXT (r
)), arg_types
);
1550 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
1552 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
1553 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
1554 buf
= (char *) alloca (strlen (dbuf
)
1555 + sizeof (DESTRUCTOR_DECL_PREFIX
));
1556 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
1559 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
1562 DECL_ASSEMBLER_NAME (r
)
1563 = build_decl_overload (DECL_NAME (r
), arg_types
, member
);
1565 make_decl_rtl (r
, NULL_PTR
, 1);
1567 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1568 DECL_MAIN_VARIANT (r
) = r
;
1569 DECL_RESULT (r
) = NULL_TREE
;
1570 DECL_INITIAL (r
) = NULL_TREE
;
1572 TREE_STATIC (r
) = 0;
1573 TREE_PUBLIC (r
) = 1;
1574 DECL_EXTERNAL (r
) = 1;
1575 DECL_INTERFACE_KNOWN (r
) = 0;
1576 DECL_DEFER_OUTPUT (r
) = 0;
1577 TREE_CHAIN (r
) = NULL_TREE
;
1578 DECL_CHAIN (r
) = NULL_TREE
;
1580 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
1581 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
1583 /* Look for matching decls for the moment. */
1584 if (! member
&& ! flag_ansi_overloading
)
1586 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
1589 if (decls
== NULL_TREE
)
1591 else if (is_overloaded_fn (decls
))
1592 for (decls
= get_first_fn (decls
); decls
;
1593 decls
= DECL_CHAIN (decls
))
1595 if (TREE_CODE (decls
) == FUNCTION_DECL
1596 && TREE_TYPE (decls
) == type
)
1605 int dcl_only
= ! DECL_INITIAL (d
);
1607 DECL_INITIAL (r
) = error_mark_node
;
1608 duplicate_decls (r
, d
);
1611 DECL_INITIAL (r
) = 0;
1615 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1617 tree tmpl
= DECL_TI_TEMPLATE (t
);
1618 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1619 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1620 args
, nargs
, in_decl
);
1622 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1623 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1625 /* If we have a preexisting version of this function, don't expand
1626 the template version, use the other instead. */
1627 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
1628 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
1630 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1632 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1633 = tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1636 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
1637 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
1639 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
1640 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
1647 tree r
= copy_node (t
);
1648 TREE_TYPE (r
) = type
;
1649 DECL_INITIAL (r
) = TREE_TYPE (r
);
1650 DECL_CONTEXT (r
) = NULL_TREE
;
1651 #ifdef PROMOTE_PROTOTYPES
1652 if ((TREE_CODE (type
) == INTEGER_TYPE
1653 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1654 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
1655 DECL_ARG_TYPE (r
) = integer_type_node
;
1658 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1664 tree r
= copy_node (t
);
1665 TREE_TYPE (r
) = type
;
1668 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
1670 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1671 TREE_CHAIN (r
) = NULL_TREE
;
1677 tree r
= copy_node (t
);
1679 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1680 TREE_CHAIN (r
) = NULL_TREE
;
1687 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1689 /* Do we already have this instantiation? */
1690 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1692 tree tmpl
= DECL_TI_TEMPLATE (t
);
1693 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1695 for (; decls
; decls
= TREE_CHAIN (decls
))
1696 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1697 return TREE_VALUE (decls
);
1701 TREE_TYPE (r
) = type
;
1702 DECL_CONTEXT (r
) = ctx
;
1703 if (TREE_STATIC (r
))
1704 DECL_ASSEMBLER_NAME (r
)
1705 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
1707 /* Don't try to expand the initializer until someone tries to use
1708 this variable; otherwise we run into circular dependencies. */
1709 DECL_INITIAL (r
) = NULL_TREE
;
1714 if (DECL_LANG_SPECIFIC (r
))
1717 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
1720 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1722 tree tmpl
= DECL_TI_TEMPLATE (t
);
1723 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1724 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1725 args
, nargs
, in_decl
);
1727 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1728 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1729 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1731 TREE_CHAIN (r
) = NULL_TREE
;
1736 if (t
== TYPE_NAME (TREE_TYPE (t
)))
1737 return TYPE_NAME (type
);
1740 tree r
= copy_node (t
);
1741 TREE_TYPE (r
) = type
;
1742 DECL_CONTEXT (r
) = current_class_type
;
1743 TREE_CHAIN (r
) = NULL_TREE
;
1749 tree purpose
, value
, chain
, result
;
1750 int via_public
, via_virtual
, via_protected
;
1752 if (t
== void_list_node
)
1755 via_public
= TREE_VIA_PUBLIC (t
);
1756 via_protected
= TREE_VIA_PROTECTED (t
);
1757 via_virtual
= TREE_VIA_VIRTUAL (t
);
1759 purpose
= TREE_PURPOSE (t
);
1761 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
1762 value
= TREE_VALUE (t
);
1764 value
= tsubst (value
, args
, nargs
, in_decl
);
1765 chain
= TREE_CHAIN (t
);
1766 if (chain
&& chain
!= void_type_node
)
1767 chain
= tsubst (chain
, args
, nargs
, in_decl
);
1768 if (purpose
== TREE_PURPOSE (t
)
1769 && value
== TREE_VALUE (t
)
1770 && chain
== TREE_CHAIN (t
))
1772 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
1773 purpose
, value
, chain
);
1774 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
1778 if (type
!= NULL_TREE
)
1782 if (type
== TREE_TYPE (t
))
1785 TREE_TYPE (t
) = complete_type (type
);
1786 if (IS_AGGR_TYPE (type
))
1788 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
1789 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
1790 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
1791 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
1796 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
1797 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
1799 bzero ((char *) elts
, len
* sizeof (tree
));
1801 for (i
= 0; i
< len
; i
++)
1803 elts
[i
] = tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
1804 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
1811 t
= make_tree_vec (len
);
1812 for (i
= 0; i
< len
; i
++)
1813 TREE_VEC_ELT (t
, i
) = elts
[i
];
1818 case REFERENCE_TYPE
:
1821 enum tree_code code
;
1822 if (type
== TREE_TYPE (t
))
1825 code
= TREE_CODE (t
);
1826 if (code
== POINTER_TYPE
)
1827 r
= build_pointer_type (type
);
1829 r
= build_reference_type (type
);
1830 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1831 /* Will this ever be needed for TYPE_..._TO values? */
1836 return build_offset_type
1837 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
1841 tree values
= TYPE_ARG_TYPES (t
);
1842 tree context
= TYPE_CONTEXT (t
);
1843 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
1846 /* Don't bother recursing if we know it won't change anything. */
1847 if (values
!= void_list_node
)
1849 /* This should probably be rewritten to use hash_tree_cons for
1850 the memory savings. */
1851 tree first
= NULL_TREE
;
1854 for (; values
&& values
!= void_list_node
;
1855 values
= TREE_CHAIN (values
))
1857 tree value
= TYPE_MAIN_VARIANT (type_decays_to
1858 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
1859 /* Don't instantiate default args unless they are used.
1860 Handle it in build_over_call instead. */
1861 tree purpose
= TREE_PURPOSE (values
);
1862 tree x
= build_tree_list (purpose
, value
);
1865 TREE_CHAIN (last
) = x
;
1871 if (values
== void_list_node
)
1872 TREE_CHAIN (last
) = void_list_node
;
1877 context
= tsubst (context
, args
, nargs
, in_decl
);
1878 /* Could also optimize cases where return value and
1879 values have common elements (e.g., T min(const &T, const T&). */
1881 /* If the above parameters haven't changed, just return the type. */
1882 if (type
== TREE_TYPE (t
)
1883 && values
== TYPE_VALUES (t
)
1884 && context
== TYPE_CONTEXT (t
))
1887 /* Construct a new type node and return it. */
1888 if (TREE_CODE (t
) == FUNCTION_TYPE
1889 && context
== NULL_TREE
)
1891 fntype
= build_function_type (type
, values
);
1893 else if (context
== NULL_TREE
)
1895 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
1896 args
, nargs
, in_decl
);
1897 fntype
= build_cplus_method_type (base
, type
,
1898 TREE_CHAIN (values
));
1902 fntype
= make_node (TREE_CODE (t
));
1903 TREE_TYPE (fntype
) = type
;
1904 TYPE_CONTEXT (fntype
) = context
;
1905 TYPE_VALUES (fntype
) = values
;
1906 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
1907 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
1908 TYPE_MODE (fntype
) = TYPE_MODE (t
);
1909 if (TYPE_METHOD_BASETYPE (t
))
1910 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
1911 args
, nargs
, in_decl
);
1912 /* Need to generate hash value. */
1913 my_friendly_abort (84);
1915 fntype
= build_type_variant (fntype
,
1920 raises
= tsubst (raises
, args
, nargs
, in_decl
);
1921 fntype
= build_exception_variant (fntype
, raises
);
1927 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
1929 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
1931 r
= build_cplus_array_type (type
, domain
);
1937 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1938 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1939 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
1943 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
1944 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
1948 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1949 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
1950 return cp_build_type_variant
1951 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
1952 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
1956 return make_pointer_declarator
1957 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1960 return make_reference_declarator
1961 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1964 return build_parse_node
1965 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1966 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1969 return make_call_declarator
1970 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1971 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1972 TREE_OPERAND (t
, 2),
1973 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
1976 return build_parse_node
1977 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1978 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1981 sorry ("use of `%s' in template",
1982 tree_code_name
[(int) TREE_CODE (t
)]);
1983 return error_mark_node
;
1990 emit_line_note (input_filename
, lineno
);
1994 expand_start_bindings (0);
2002 expand_end_bindings (getdecls (), kept_level_p (), 1);
2003 t
= poplevel (kept_level_p (), 1, 0);
2009 tsubst_copy (t
, args
, nargs
, in_decl
)
2014 enum tree_code code
;
2016 if (t
== NULL_TREE
|| t
== error_mark_node
)
2019 code
= TREE_CODE (t
);
2024 return do_identifier (DECL_NAME (t
), 0);
2028 if (DECL_CONTEXT (t
))
2030 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2031 if (ctx
== current_function_decl
)
2032 return lookup_name (DECL_NAME (t
), 0);
2033 else if (ctx
!= DECL_CONTEXT (t
))
2034 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
2040 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2041 t
= tsubst (t
, args
, nargs
, in_decl
);
2046 case IDENTIFIER_NODE
:
2047 return do_identifier (t
, 0);
2051 case REINTERPRET_CAST_EXPR
:
2052 case CONST_CAST_EXPR
:
2053 case STATIC_CAST_EXPR
:
2054 case DYNAMIC_CAST_EXPR
:
2056 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
2057 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2060 case PREDECREMENT_EXPR
:
2061 case PREINCREMENT_EXPR
:
2062 case POSTDECREMENT_EXPR
:
2063 case POSTINCREMENT_EXPR
:
2065 case TRUTH_NOT_EXPR
:
2068 case CONVERT_EXPR
: /* Unary + */
2075 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2080 case TRUNC_DIV_EXPR
:
2082 case FLOOR_DIV_EXPR
:
2083 case ROUND_DIV_EXPR
:
2084 case EXACT_DIV_EXPR
:
2086 case BIT_ANDTC_EXPR
:
2089 case TRUNC_MOD_EXPR
:
2090 case FLOOR_MOD_EXPR
:
2091 case TRUTH_ANDIF_EXPR
:
2092 case TRUTH_ORIF_EXPR
:
2093 case TRUTH_AND_EXPR
:
2114 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2115 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2119 tree fn
= TREE_OPERAND (t
, 0);
2120 if (really_overloaded_fn (fn
))
2121 fn
= tsubst_copy (TREE_VALUE (fn
), args
, nargs
, in_decl
);
2123 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
2125 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2129 case METHOD_CALL_EXPR
:
2131 tree name
= TREE_OPERAND (t
, 0);
2132 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2134 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2135 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
2137 else if (TREE_CODE (name
) == SCOPE_REF
2138 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
2140 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2141 name
= TREE_OPERAND (name
, 1);
2142 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2143 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
2144 name
= build_nt (SCOPE_REF
, base
, name
);
2147 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2149 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2150 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
2157 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2158 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2159 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
2164 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2165 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2166 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
2167 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
2174 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2175 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2176 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
2177 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
2183 tree purpose
, value
, chain
;
2185 if (t
== void_list_node
)
2188 purpose
= TREE_PURPOSE (t
);
2190 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
2191 value
= TREE_VALUE (t
);
2193 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
2194 chain
= TREE_CHAIN (t
);
2195 if (chain
&& chain
!= void_type_node
)
2196 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
2197 if (purpose
== TREE_PURPOSE (t
)
2198 && value
== TREE_VALUE (t
)
2199 && chain
== TREE_CHAIN (t
))
2201 return tree_cons (purpose
, value
, chain
);
2208 case TEMPLATE_TYPE_PARM
:
2209 case TEMPLATE_CONST_PARM
:
2211 case REFERENCE_TYPE
:
2217 return tsubst (t
, args
, nargs
, in_decl
);
2219 case IDENTIFIER_NODE
:
2220 if (IDENTIFIER_TYPENAME_P (t
))
2221 return build_typename_overload
2222 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
2228 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
2229 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
2237 tsubst_expr (t
, args
, nargs
, in_decl
)
2242 if (t
== NULL_TREE
|| t
== error_mark_node
)
2245 if (processing_template_decl
)
2246 return tsubst_copy (t
, args
, nargs
, in_decl
);
2248 switch (TREE_CODE (t
))
2251 lineno
= TREE_COMPLEXITY (t
);
2252 emit_line_note (input_filename
, lineno
);
2254 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2259 lineno
= TREE_COMPLEXITY (t
);
2260 emit_line_note (input_filename
, lineno
);
2261 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2262 /* Do default conversion if safe and possibly important,
2263 in case within ({...}). */
2264 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
2265 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
2266 t
= default_conversion (t
);
2267 cplus_expand_expr_stmt (t
);
2274 int i
= suspend_momentary ();
2277 lineno
= TREE_COMPLEXITY (t
);
2278 emit_line_note (input_filename
, lineno
);
2280 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2281 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2282 TREE_OPERAND (t
, 2) != 0);
2283 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
2285 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
2286 resume_momentary (i
);
2293 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
2294 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2295 int cond_scope
= (TREE_OPERAND (t
, 1)
2296 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
2298 lineno
= TREE_COMPLEXITY (t
);
2299 emit_line_note (input_filename
, lineno
);
2302 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
2303 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2305 emit_line_note (input_filename
, lineno
);
2306 expand_start_loop_continue_elsewhere (1);
2310 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2311 emit_line_note (input_filename
, lineno
);
2313 expand_exit_loop_if_false (0, condition_conversion (tmp
));
2317 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
2320 emit_line_note (input_filename
, lineno
);
2321 expand_loop_continue_here ();
2322 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
2324 cplus_expand_expr_stmt (tmp
);
2337 lineno
= TREE_COMPLEXITY (t
);
2339 emit_line_note (input_filename
, lineno
);
2340 expand_start_loop (1);
2342 cond
= TREE_OPERAND (t
, 0);
2343 if (TREE_CODE (cond
) == DECL_STMT
)
2345 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
2346 emit_line_note (input_filename
, lineno
);
2347 expand_exit_loop_if_false (0, condition_conversion (cond
));
2349 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
2351 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2363 lineno
= TREE_COMPLEXITY (t
);
2365 emit_line_note (input_filename
, lineno
);
2366 expand_start_loop_continue_elsewhere (1);
2368 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2369 expand_loop_continue_here ();
2371 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2372 emit_line_note (input_filename
, lineno
);
2373 expand_exit_loop_if_false (0, condition_conversion (cond
));
2384 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2386 lineno
= TREE_COMPLEXITY (t
);
2389 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2390 emit_line_note (input_filename
, lineno
);
2391 expand_start_cond (condition_conversion (tmp
), 0);
2393 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2394 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2396 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
2398 expand_start_else ();
2399 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2413 tree substmt
= TREE_OPERAND (t
, 0);
2415 lineno
= TREE_COMPLEXITY (t
);
2417 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2420 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
2421 tsubst_expr (substmt
, args
, nargs
, in_decl
);
2423 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2429 lineno
= TREE_COMPLEXITY (t
);
2430 emit_line_note (input_filename
, lineno
);
2431 if (! expand_exit_something ())
2432 error ("break statement not within loop or switch");
2436 lineno
= TREE_COMPLEXITY (t
);
2437 emit_line_note (input_filename
, lineno
);
2438 if (! expand_continue_loop (0))
2439 error ("continue statement not within a loop");
2445 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2447 lineno
= TREE_COMPLEXITY (t
);
2450 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2451 emit_line_note (input_filename
, lineno
);
2452 c_expand_start_case (val
);
2455 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2456 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2458 expand_end_case (val
);
2469 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2470 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2474 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
2481 lineno
= TREE_COMPLEXITY (t
);
2482 emit_line_note (input_filename
, lineno
);
2483 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
2485 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
2486 TREE_USED (decl
) = 1;
2490 expand_computed_goto
2491 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2495 lineno
= TREE_COMPLEXITY (t
);
2496 emit_line_note (input_filename
, lineno
);
2497 expand_start_try_stmts ();
2498 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2499 expand_start_all_catch ();
2501 tree handler
= TREE_OPERAND (t
, 1);
2502 for (; handler
; handler
= TREE_CHAIN (handler
))
2503 tsubst_expr (handler
, args
, nargs
, in_decl
);
2505 expand_end_all_catch ();
2509 lineno
= TREE_COMPLEXITY (t
);
2511 if (TREE_OPERAND (t
, 0))
2513 tree d
= TREE_OPERAND (t
, 0);
2514 expand_start_catch_block
2515 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
2516 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
2519 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
2520 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2521 expand_end_catch_block ();
2526 lineno
= TREE_COMPLEXITY (t
);
2528 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
2529 tsubst_enum (t
, args
, nargs
);
2533 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
2539 instantiate_template (tmpl
, targ_ptr
)
2540 tree tmpl
, *targ_ptr
;
2544 struct obstack
*old_fmp_obstack
;
2545 extern struct obstack
*function_maybepermanent_obstack
;
2547 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2548 old_fmp_obstack
= function_maybepermanent_obstack
;
2549 function_maybepermanent_obstack
= &permanent_obstack
;
2551 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
2552 len
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
2557 tree t
= targ_ptr
[i
];
2558 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
2560 tree nt
= target_type (t
);
2561 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
2563 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
2564 cp_error (" trying to instantiate `%D'", tmpl
);
2565 fndecl
= error_mark_node
;
2569 targ_ptr
[i
] = copy_to_permanent (t
);
2572 /* substitute template parameters */
2573 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
2575 if (flag_external_templates
)
2576 add_pending_template (fndecl
);
2579 function_maybepermanent_obstack
= old_fmp_obstack
;
2585 /* Push the name of the class template into the scope of the instantiation. */
2588 overload_template_name (type
)
2591 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
2594 if (IDENTIFIER_CLASS_VALUE (id
)
2595 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
2598 decl
= build_decl (TYPE_DECL
, id
, type
);
2599 SET_DECL_ARTIFICIAL (decl
);
2600 pushdecl_class_level (decl
);
2603 /* Type unification.
2605 We have a function template signature with one or more references to
2606 template parameters, and a parameter list we wish to fit to this
2607 template. If possible, produce a list of parameters for the template
2608 which will cause it to fit the supplied parameter list.
2610 Return zero for success, 2 for an incomplete match that doesn't resolve
2611 all the types, and 1 for complete failure. An error message will be
2612 printed only for an incomplete match.
2614 TPARMS[NTPARMS] is an array of template parameter types;
2615 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2616 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2617 and ARGS is the argument list we're trying to match against it.
2619 If SUBR is 1, we're being called recursively (to unify the arguments of
2620 a function or method parameter of a function template), so don't zero
2621 out targs and don't fail on an incomplete match.
2623 If STRICT is 1, the match must be exact (for casts of overloaded
2624 addresses, explicit instantiation, and more_specialized). */
2627 type_unification (tparms
, targs
, parms
, args
, nsubsts
, subr
, strict
)
2628 tree tparms
, *targs
, parms
, args
;
2629 int *nsubsts
, subr
, strict
;
2633 int ntparms
= TREE_VEC_LENGTH (tparms
);
2635 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
2636 my_friendly_assert (TREE_CODE (parms
) == TREE_LIST
, 290);
2637 /* ARGS could be NULL (via a call from parse.y to
2638 build_x_function_call). */
2640 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
2641 my_friendly_assert (ntparms
> 0, 292);
2644 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
2647 && parms
!= void_list_node
2649 && args
!= void_list_node
)
2651 parm
= TREE_VALUE (parms
);
2652 parms
= TREE_CHAIN (parms
);
2653 arg
= TREE_VALUE (args
);
2654 args
= TREE_CHAIN (args
);
2656 if (arg
== error_mark_node
)
2658 if (arg
== unknown_type_node
)
2661 if (! uses_template_parms (parm
)
2662 && TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2664 if (can_convert_arg (parm
, TREE_TYPE (arg
), arg
))
2670 if (TREE_CODE (arg
) == VAR_DECL
)
2671 arg
= TREE_TYPE (arg
);
2672 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
2673 arg
= TREE_TYPE (arg
);
2675 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2677 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
2678 if (TREE_CODE (arg
) == TREE_LIST
2679 && TREE_TYPE (arg
) == unknown_type_node
2680 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
2682 int nsubsts
, ntparms
;
2685 /* Have to back unify here */
2686 arg
= TREE_VALUE (arg
);
2688 ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg
));
2689 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2690 parm
= tree_cons (NULL_TREE
, parm
, NULL_TREE
);
2691 return type_unification (DECL_TEMPLATE_PARMS (arg
), targs
,
2692 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
2693 parm
, &nsubsts
, 0, strict
);
2695 arg
= TREE_TYPE (arg
);
2698 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
2699 arg
= TREE_TYPE (arg
);
2701 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
2703 if (TREE_CODE (arg
) == FUNCTION_TYPE
2704 || TREE_CODE (arg
) == METHOD_TYPE
)
2705 arg
= build_pointer_type (arg
);
2706 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
2707 arg
= build_pointer_type (TREE_TYPE (arg
));
2709 arg
= TYPE_MAIN_VARIANT (arg
);
2712 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
2720 /* Fail if we've reached the end of the parm list, and more args
2721 are present, and the parm list isn't variadic. */
2722 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
2724 /* Fail if parms are left and they don't have default values. */
2726 && parms
!= void_list_node
2727 && TREE_PURPOSE (parms
) == NULL_TREE
)
2730 for (i
= 0; i
< ntparms
; i
++)
2733 error ("incomplete type unification");
2739 /* Tail recursion is your friend. */
2742 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
2743 tree tparms
, *targs
, parm
, arg
;
2744 int *nsubsts
, ntparms
, strict
;
2748 /* I don't think this will do the right thing with respect to types.
2749 But the only case I've seen it in so far has been array bounds, where
2750 signedness is the only information lost, and I think that will be
2752 while (TREE_CODE (parm
) == NOP_EXPR
)
2753 parm
= TREE_OPERAND (parm
, 0);
2755 if (arg
== error_mark_node
)
2757 if (arg
== unknown_type_node
)
2762 switch (TREE_CODE (parm
))
2764 case TEMPLATE_TYPE_PARM
:
2766 idx
= TEMPLATE_TYPE_IDX (parm
);
2767 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
2768 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
2771 /* Template type parameters cannot contain cv-quals; i.e.
2772 template <class T> void f (T& a, T& b) will not generate
2773 void f (const int& a, const int& b). */
2774 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
2775 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
2777 arg
= TYPE_MAIN_VARIANT (arg
);
2780 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
2781 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
2782 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
2785 /* Simple cases: Value already set, does match or doesn't. */
2786 if (targs
[idx
] == arg
)
2788 else if (targs
[idx
])
2790 /* Check for mixed types and values. */
2791 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
2795 case TEMPLATE_CONST_PARM
:
2797 idx
= TEMPLATE_CONST_IDX (parm
);
2800 int i
= cp_tree_equal (targs
[idx
], arg
);
2806 my_friendly_abort (42);
2809 targs
[idx
] = copy_to_permanent (arg
);
2813 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
2814 return unify (tparms
, targs
, ntparms
, parm
,
2815 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
2817 if (TREE_CODE (arg
) != POINTER_TYPE
)
2819 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2822 case REFERENCE_TYPE
:
2823 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2824 arg
= TREE_TYPE (arg
);
2825 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
2829 if (TREE_CODE (arg
) != ARRAY_TYPE
)
2831 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
2832 nsubsts
, strict
) != 0)
2834 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2841 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2844 if (TREE_CODE (parm
) == INTEGER_TYPE
)
2846 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
2847 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
2848 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
2850 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
2851 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
2852 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
2855 /* As far as unification is concerned, this wins. Later checks
2856 will invalidate it if necessary. */
2859 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2860 /* Type INTEGER_CST can come from ordinary constant template args. */
2862 while (TREE_CODE (arg
) == NOP_EXPR
)
2863 arg
= TREE_OPERAND (arg
, 0);
2865 if (TREE_CODE (arg
) != INTEGER_CST
)
2867 return !tree_int_cst_equal (parm
, arg
);
2872 t1
= TREE_OPERAND (parm
, 0);
2873 t2
= TREE_OPERAND (parm
, 1);
2874 return unify (tparms
, targs
, ntparms
, t1
,
2875 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
2882 if (TREE_CODE (arg
) != TREE_VEC
)
2884 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
2886 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
2887 if (unify (tparms
, targs
, ntparms
,
2888 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
2895 if (TYPE_PTRMEMFUNC_FLAG (parm
))
2896 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
2897 arg
, nsubsts
, strict
);
2899 /* Allow trivial conversions. */
2900 if (TREE_CODE (arg
) != RECORD_TYPE
2901 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
2902 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
2905 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
2908 if (flag_ansi_overloading
&& ! strict
)
2909 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
2911 (CLASSTYPE_TEMPLATE_INFO (arg
)
2912 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
2914 if (! t
|| t
== error_mark_node
)
2917 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
2918 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
2920 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
2925 if (TREE_CODE (arg
) != METHOD_TYPE
)
2930 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
2933 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2934 TREE_TYPE (arg
), nsubsts
, strict
))
2936 return type_unification (tparms
, targs
, TYPE_ARG_TYPES (parm
),
2937 TYPE_ARG_TYPES (arg
), nsubsts
, 1, strict
);
2940 if (TREE_CODE (arg
) != OFFSET_TYPE
)
2942 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
2943 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
2945 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2946 TREE_TYPE (arg
), nsubsts
, strict
);
2949 sorry ("use of `%s' in template type unification",
2950 tree_code_name
[(int) TREE_CODE (parm
)]);
2956 mark_decl_instantiated (result
, extern_p
)
2960 if (DECL_TEMPLATE_INSTANTIATION (result
))
2961 SET_DECL_EXPLICIT_INSTANTIATION (result
);
2962 TREE_PUBLIC (result
) = 1;
2966 DECL_INTERFACE_KNOWN (result
) = 1;
2967 DECL_NOT_REALLY_EXTERN (result
) = 1;
2969 else if (TREE_CODE (result
) == FUNCTION_DECL
)
2970 mark_inline_for_output (result
);
2973 /* Given two function templates PAT1 and PAT2, return:
2975 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
2976 -1 if PAT2 is more specialized than PAT1.
2977 0 if neither is more specialized. */
2980 more_specialized (pat1
, pat2
)
2986 targs
= get_bindings (pat1
, pat2
);
2993 targs
= get_bindings (pat2
, pat1
);
3003 /* Given two class template specialization list nodes PAT1 and PAT2, return:
3005 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
3006 -1 if PAT2 is more specialized than PAT1.
3007 0 if neither is more specialized. */
3010 more_specialized_class (pat1
, pat2
)
3016 targs
= get_class_bindings
3017 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
3021 targs
= get_class_bindings
3022 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
3029 /* Return the template arguments that will produce the function signature
3030 DECL from the function template FN. */
3033 get_bindings (fn
, decl
)
3036 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn
));
3037 tree
*targs
= (tree
*) malloc (sizeof (tree
) * ntparms
);
3039 i
= type_unification (DECL_TEMPLATE_PARMS (fn
), targs
,
3040 TYPE_ARG_TYPES (TREE_TYPE (fn
)),
3041 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
3050 get_class_bindings (tparms
, parms
, args
)
3051 tree tparms
, parms
, args
;
3053 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
3054 tree vec
= make_temp_vec (ntparms
);
3056 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
3058 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
3059 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
3069 for (i
= 0; i
< ntparms
; ++i
)
3070 if (! TREE_VEC_ELT (vec
, i
))
3076 /* Return the most specialized of the list of templates in FNS that can
3077 produce an instantiation matching DECL. */
3080 most_specialized (fns
, decl
)
3083 tree fn
, champ
, *args
, *p
;
3086 for (p
= &fns
; *p
; )
3088 args
= get_bindings (TREE_VALUE (*p
), decl
);
3092 p
= &TREE_CHAIN (*p
);
3095 *p
= TREE_CHAIN (*p
);
3102 champ
= TREE_VALUE (fn
);
3103 fn
= TREE_CHAIN (fn
);
3104 for (; fn
; fn
= TREE_CHAIN (fn
))
3106 fate
= more_specialized (champ
, TREE_VALUE (fn
));
3113 fn
= TREE_CHAIN (fn
);
3115 return error_mark_node
;
3117 champ
= TREE_VALUE (fn
);
3121 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
3123 fate
= more_specialized (champ
, TREE_VALUE (fn
));
3125 return error_mark_node
;
3131 /* Return the most specialized of the class template specializations in
3132 SPECS that can produce an instantiation matching ARGS. */
3135 most_specialized_class (specs
, mainargs
)
3136 tree specs
, mainargs
;
3138 tree list
= NULL_TREE
, t
, args
, champ
;
3141 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
3143 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
3146 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
3147 TREE_TYPE (list
) = TREE_TYPE (t
);
3157 for (; t
; t
= TREE_CHAIN (t
))
3159 fate
= more_specialized_class (champ
, t
);
3168 return error_mark_node
;
3174 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
3176 fate
= more_specialized (champ
, t
);
3178 return error_mark_node
;
3184 /* called from the parser. */
3187 do_decl_instantiation (declspecs
, declarator
, storage
)
3188 tree declspecs
, declarator
, storage
;
3190 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
3193 tree result
= NULL_TREE
;
3196 if (! DECL_LANG_SPECIFIC (decl
))
3198 cp_error ("explicit instantiation of non-template `%#D'", decl
);
3202 /* If we've already seen this template instance, use it. */
3203 if (TREE_CODE (decl
) == VAR_DECL
)
3205 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
3206 if (result
&& TREE_CODE (result
) != VAR_DECL
)
3209 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
3211 cp_error ("explicit instantiation of `%#D'", decl
);
3214 else if (DECL_FUNCTION_MEMBER_P (decl
))
3216 if (DECL_TEMPLATE_INSTANTIATION (decl
))
3218 else if (name
= DECL_ASSEMBLER_NAME (decl
),
3219 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
3220 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
3223 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
3225 tree templates
= NULL_TREE
;
3226 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
3227 if (decls_match (fn
, decl
)
3228 && DECL_DEFER_OUTPUT (fn
))
3233 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
3234 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
3239 result
= most_specialized (templates
, decl
);
3240 if (result
== error_mark_node
)
3242 char *str
= "candidates are:";
3243 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
3244 for (fn
= templates
; fn
; fn
= TREE_CHAIN (fn
))
3246 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
3253 args
= get_bindings (result
, decl
);
3254 result
= instantiate_template (result
, args
);
3261 cp_error ("no matching template for `%D' found", decl
);
3265 if (! DECL_TEMPLATE_INFO (result
))
3267 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
3271 if (flag_external_templates
)
3274 if (storage
== NULL_TREE
)
3276 else if (storage
== ridpointers
[(int) RID_EXTERN
])
3279 cp_error ("storage class `%D' applied to template instantiation",
3282 mark_decl_instantiated (result
, extern_p
);
3283 repo_template_instantiated (result
, extern_p
);
3285 instantiate_decl (result
);
3289 mark_class_instantiated (t
, extern_p
)
3293 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
3294 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
3295 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
3296 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
3297 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
3300 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
3301 rest_of_type_compilation (t
, 1);
3306 do_type_instantiation (name
, storage
)
3309 tree t
= TREE_TYPE (name
);
3316 /* With -fexternal-templates, explicit instantiations are treated the same
3317 as implicit ones. */
3318 if (flag_external_templates
)
3321 if (TYPE_SIZE (t
) == NULL_TREE
)
3323 cp_error ("explicit instantiation of `%#T' before definition of template",
3328 if (storage
== NULL_TREE
)
3330 else if (storage
== ridpointers
[(int) RID_INLINE
])
3332 else if (storage
== ridpointers
[(int) RID_EXTERN
])
3334 else if (storage
== ridpointers
[(int) RID_STATIC
])
3338 cp_error ("storage class `%D' applied to template instantiation",
3343 /* We've already instantiated this. */
3344 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
3348 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
3350 mark_class_instantiated (t
, extern_p
);
3351 repo_template_instantiated (t
, extern_p
);
3361 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
3362 if (DECL_TEMPLATE_INSTANTIATION (tmp
))
3364 mark_decl_instantiated (tmp
, extern_p
);
3365 repo_template_instantiated (tmp
, extern_p
);
3367 instantiate_decl (tmp
);
3370 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
3371 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
3373 mark_decl_instantiated (tmp
, extern_p
);
3374 repo_template_instantiated (tmp
, extern_p
);
3376 instantiate_decl (tmp
);
3379 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
3380 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
3381 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
3386 instantiate_decl (d
)
3389 tree ti
= DECL_TEMPLATE_INFO (d
);
3390 tree tmpl
= TI_TEMPLATE (ti
);
3391 tree args
= TI_ARGS (ti
);
3393 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
3395 int nested
= in_function_p ();
3397 int pattern_defined
;
3399 char *file
= input_filename
;
3401 if (TREE_CODE (d
) == FUNCTION_DECL
)
3403 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
3404 pattern_defined
= (DECL_INITIAL (pattern
) != NULL_TREE
);
3408 d_defined
= ! DECL_IN_AGGR_P (d
);
3409 pattern_defined
= ! DECL_IN_AGGR_P (pattern
);
3415 /* This needs to happen before any tsubsting. */
3416 if (! push_tinst_level (d
))
3419 push_to_top_level ();
3420 lineno
= DECL_SOURCE_LINE (d
);
3421 input_filename
= DECL_SOURCE_FILE (d
);
3423 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
3424 variable is a static const initialized in the class body. */
3425 if (TREE_CODE (d
) == VAR_DECL
3426 && ! DECL_INITIAL (d
) && DECL_INITIAL (pattern
))
3428 pushclass (DECL_CONTEXT (d
), 2);
3429 DECL_INITIAL (d
) = tsubst_expr
3430 (DECL_INITIAL (pattern
), &TREE_VEC_ELT (args
, 0),
3431 TREE_VEC_LENGTH (args
), tmpl
);
3435 /* import_export_decl has to happen after DECL_INITIAL is set up. */
3436 if (pattern_defined
)
3438 repo_template_used (d
);
3440 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
3442 if (flag_alt_external_templates
)
3444 if (interface_unknown
)
3445 warn_if_unknown_interface (d
);
3447 else if (DECL_INTERFACE_KNOWN (pattern
))
3449 DECL_INTERFACE_KNOWN (d
) = 1;
3450 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (pattern
);
3453 warn_if_unknown_interface (pattern
);
3457 import_export_decl (d
);
3460 if (! pattern_defined
3461 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! DECL_INLINE (d
)
3462 && (! DECL_INTERFACE_KNOWN (d
)
3463 || ! DECL_NOT_REALLY_EXTERN (d
)))
3464 /* Kludge: if we compile a constructor in the middle of processing a
3465 toplevel declaration, we blow away the declspecs in
3466 temp_decl_obstack when we call permanent_allocation in
3467 finish_function. So don't compile it yet. */
3468 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! nested
&& ! at_eof
))
3470 add_pending_template (d
);
3474 lineno
= DECL_SOURCE_LINE (d
);
3475 input_filename
= DECL_SOURCE_FILE (d
);
3477 /* Trick tsubst into giving us a new decl in case the template changed. */
3478 save_ti
= DECL_TEMPLATE_INFO (pattern
);
3479 DECL_TEMPLATE_INFO (pattern
) = NULL_TREE
;
3480 td
= tsubst (pattern
, &TREE_VEC_ELT (args
, 0), TREE_VEC_LENGTH (args
), tmpl
);
3481 DECL_TEMPLATE_INFO (pattern
) = save_ti
;
3483 /* And set up DECL_INITIAL, since tsubst doesn't. */
3484 if (TREE_CODE (td
) == VAR_DECL
)
3486 pushclass (DECL_CONTEXT (d
), 2);
3487 DECL_INITIAL (td
) = tsubst_expr
3488 (DECL_INITIAL (pattern
), &TREE_VEC_ELT (args
, 0),
3489 TREE_VEC_LENGTH (args
), tmpl
);
3493 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
3494 if (TREE_CODE (d
) == FUNCTION_DECL
)
3495 DECL_INITIAL (td
) = error_mark_node
;
3496 duplicate_decls (td
, d
);
3497 if (TREE_CODE (d
) == FUNCTION_DECL
)
3498 DECL_INITIAL (td
) = 0;
3500 if (TREE_CODE (d
) == VAR_DECL
)
3502 DECL_IN_AGGR_P (d
) = 0;
3503 if (DECL_INTERFACE_KNOWN (d
))
3504 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
3507 DECL_EXTERNAL (d
) = 1;
3508 DECL_NOT_REALLY_EXTERN (d
) = 1;
3510 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
3512 else if (TREE_CODE (d
) == FUNCTION_DECL
)
3514 tree t
= DECL_SAVED_TREE (pattern
);
3516 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
3517 store_parm_decls ();
3519 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
3522 (TREE_OPERAND (t
, 0),
3523 tsubst_expr (TREE_OPERAND (t
, 1), &TREE_VEC_ELT (args
, 0),
3524 TREE_VEC_LENGTH (args
), tmpl
));
3528 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
3530 current_member_init_list
3531 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
3532 current_base_init_list
3533 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
3538 /* Always keep the BLOCK node associated with the outermost
3539 pair of curley braces of a function. These are needed
3540 for correct operation of dwarfout.c. */
3543 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
3544 tsubst_expr (t
, &TREE_VEC_ELT (args
, 0),
3545 TREE_VEC_LENGTH (args
), tmpl
);
3547 finish_function (lineno
, 0, nested
);
3552 input_filename
= file
;
3554 pop_from_top_level ();
3561 tsubst_chain (t
, argvec
)
3566 tree first
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3567 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3570 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
3572 tree x
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3573 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3574 TREE_CHAIN (last
) = x
;
3584 tsubst_expr_values (t
, argvec
)
3587 tree first
= NULL_TREE
;
3590 for (; t
; t
= TREE_CHAIN (t
))
3592 tree pur
= tsubst_copy (TREE_PURPOSE (t
), &TREE_VEC_ELT (argvec
, 0),
3593 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3594 tree val
= tsubst_expr (TREE_VALUE (t
), &TREE_VEC_ELT (argvec
, 0),
3595 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3596 *p
= build_tree_list (pur
, val
);
3597 p
= &TREE_CHAIN (*p
);
3608 last_tree
= TREE_CHAIN (last_tree
) = t
;
3611 /* D is an undefined function declaration in the presence of templates with
3612 the same name, listed in FNS. If one of them can produce D as an
3613 instantiation, remember this so we can instantiate it at EOF if D has
3614 not been defined by that time. */
3617 add_maybe_template (d
, fns
)
3622 if (DECL_MAYBE_TEMPLATE (d
))
3625 t
= most_specialized (fns
, d
);
3628 if (t
== error_mark_node
)
3630 cp_error ("ambiguous template instantiation for `%D'", d
);
3634 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
3635 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
3636 DECL_MAYBE_TEMPLATE (d
) = 1;
3639 /* Instantiate an enumerated type. Used by instantiate_class_template and
3643 tsubst_enum (tag
, args
, nargs
)
3647 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
3648 tree e
, values
= NULL_TREE
;
3650 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
3652 tree elt
= build_enumerator (TREE_PURPOSE (e
),
3653 tsubst_expr (TREE_VALUE (e
), args
,
3655 TREE_CHAIN (elt
) = values
;
3659 finish_enum (newtag
, values
);