]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle parameterized types (templates) for GNU C++. |
4c571114 | 2 | Copyright (C) 1992, 93-97, 1998, 1999 Free Software Foundation, Inc. |
8d08fdba | 3 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
fc378698 | 4 | Rewritten by Jason Merrill (jason@cygnus.com). |
8d08fdba MS |
5 | |
6 | This file is part of GNU CC. | |
7 | ||
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) | |
11 | any later version. | |
12 | ||
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. | |
17 | ||
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 | |
e9fa0c7c RK |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
22 | |
23 | /* Known bugs or deficiencies include: | |
e92cc029 | 24 | |
e92cc029 MS |
25 | all methods must be provided in header files; can't use a source |
26 | file that contains only the method templates and "just win". */ | |
8d08fdba MS |
27 | |
28 | #include "config.h" | |
8d052bc7 | 29 | #include "system.h" |
8d08fdba MS |
30 | #include "obstack.h" |
31 | ||
32 | #include "tree.h" | |
33 | #include "flags.h" | |
34 | #include "cp-tree.h" | |
35 | #include "decl.h" | |
36 | #include "parse.h" | |
f0e01782 | 37 | #include "lex.h" |
e8abc66f | 38 | #include "output.h" |
a9aedbc2 | 39 | #include "defaults.h" |
49c249e1 | 40 | #include "except.h" |
54f92bfb | 41 | #include "toplev.h" |
3dcaad8b | 42 | #include "rtl.h" |
091f9839 | 43 | #include "defaults.h" |
9cd64686 | 44 | #include "ggc.h" |
49c249e1 | 45 | |
050367a3 MM |
46 | /* The type of functions taking a tree, and some additional data, and |
47 | returning an int. */ | |
48 | typedef int (*tree_fn_t) PROTO((tree, void*)); | |
49 | ||
8d08fdba | 50 | extern struct obstack permanent_obstack; |
8d08fdba MS |
51 | |
52 | extern int lineno; | |
53 | extern char *input_filename; | |
8d08fdba | 54 | |
0aafb128 MM |
55 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
56 | instantiations have been deferred, either because their definitions | |
57 | were not yet available, or because we were putting off doing the | |
58 | work. The TREE_PURPOSE of each entry is a SRCLOC indicating where | |
59 | the instantiate request occurred; the TREE_VALUE is a either a DECL | |
60 | (for a function or static data member), or a TYPE (for a class) | |
61 | indicating what we are hoping to instantiate. */ | |
62 | static tree pending_templates; | |
5566b478 MS |
63 | static tree *template_tail = &pending_templates; |
64 | ||
0aafb128 | 65 | static tree maybe_templates; |
73aad9b9 JM |
66 | static tree *maybe_template_tail = &maybe_templates; |
67 | ||
67ffc812 | 68 | int processing_template_parmlist; |
386b8a85 JM |
69 | static int template_header_count; |
70 | ||
75650646 | 71 | static tree saved_trees; |
3dcaad8b MM |
72 | static varray_type inline_parm_levels; |
73 | static size_t inline_parm_levels_used; | |
75650646 | 74 | |
8d08fdba MS |
75 | #define obstack_chunk_alloc xmalloc |
76 | #define obstack_chunk_free free | |
77 | ||
830bfa74 MM |
78 | #define UNIFY_ALLOW_NONE 0 |
79 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
80 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
81 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 82 | #define UNIFY_ALLOW_INTEGER 8 |
830bfa74 | 83 | |
4393e105 MM |
84 | #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is |
85 | virtual, or a base class of a virtual | |
86 | base. */ | |
87 | #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current | |
88 | type with the desired type. */ | |
89 | ||
34016c81 | 90 | static int resolve_overloaded_unification PROTO((tree, tree, tree, tree, |
4393e105 | 91 | unification_kind_t, int)); |
e97e5263 | 92 | static int try_one_overload PROTO((tree, tree, tree, tree, tree, |
4393e105 MM |
93 | unification_kind_t, int)); |
94 | static int unify PROTO((tree, tree, tree, tree, int)); | |
49c249e1 JM |
95 | static void add_pending_template PROTO((tree)); |
96 | static int push_tinst_level PROTO((tree)); | |
97 | static tree classtype_mangled_name PROTO((tree)); | |
36a117a5 | 98 | static char *mangle_class_name_for_template PROTO((char *, tree, tree)); |
49c249e1 | 99 | static tree tsubst_expr_values PROTO((tree, tree)); |
49c249e1 | 100 | static int list_eq PROTO((tree, tree)); |
36a117a5 | 101 | static tree get_class_bindings PROTO((tree, tree, tree)); |
744fac59 | 102 | static tree coerce_template_parms PROTO((tree, tree, tree, int, int)); |
dbfe2124 | 103 | static void tsubst_enum PROTO((tree, tree, tree)); |
98c1c668 | 104 | static tree add_to_template_args PROTO((tree, tree)); |
36a117a5 | 105 | static tree add_outermost_template_args PROTO((tree, tree)); |
830bfa74 MM |
106 | static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*, |
107 | tree*)); | |
050367a3 | 108 | static int type_unification_real PROTO((tree, tree, tree, tree, |
4393e105 | 109 | int, unification_kind_t, int)); |
386b8a85 | 110 | static void note_template_header PROTO((int)); |
840b09b4 | 111 | static tree maybe_fold_nontype_arg PROTO((tree)); |
e1467ff2 | 112 | static tree convert_nontype_argument PROTO((tree, tree)); |
8b5b8b7c MM |
113 | static tree convert_template_argument PROTO ((tree, tree, tree, int, |
114 | int , tree)); | |
76b9a14d | 115 | static tree get_bindings_overload PROTO((tree, tree, tree)); |
050367a3 | 116 | static int for_each_template_parm PROTO((tree, tree_fn_t, void*)); |
4b580446 | 117 | static tree build_template_parm_index PROTO((int, int, int, tree, tree)); |
69ac77ce JL |
118 | static int inline_needs_template_parms PROTO((tree)); |
119 | static void push_inline_template_parms_recursive PROTO((tree, int)); | |
120 | static tree retrieve_specialization PROTO((tree, tree)); | |
9188c363 | 121 | static tree retrieve_local_specialization PROTO((tree, tree)); |
36a117a5 | 122 | static tree register_specialization PROTO((tree, tree, tree)); |
9188c363 | 123 | static tree register_local_specialization PROTO((tree, tree, tree)); |
36a117a5 | 124 | static int unregister_specialization PROTO((tree, tree)); |
69ac77ce JL |
125 | static tree reduce_template_parm_level PROTO((tree, tree, int)); |
126 | static tree build_template_decl PROTO((tree, tree)); | |
127 | static int mark_template_parm PROTO((tree, void *)); | |
128 | static tree tsubst_friend_function PROTO((tree, tree)); | |
6757edfe | 129 | static tree tsubst_friend_class PROTO((tree, tree)); |
69ac77ce | 130 | static tree get_bindings_real PROTO((tree, tree, tree, int)); |
04a81d94 | 131 | static int template_decl_level PROTO((tree)); |
a2b60a0e | 132 | static tree maybe_get_template_decl_from_type_decl PROTO((tree)); |
830bfa74 | 133 | static int check_cv_quals_for_unify PROTO((int, tree, tree)); |
4393e105 MM |
134 | static tree tsubst_template_arg_vector PROTO((tree, tree, int)); |
135 | static tree tsubst_template_parms PROTO((tree, tree, int)); | |
830bfa74 | 136 | static void regenerate_decl_from_template PROTO((tree, tree)); |
36a117a5 MM |
137 | static tree most_specialized PROTO((tree, tree, tree)); |
138 | static tree most_specialized_class PROTO((tree, tree)); | |
139 | static tree most_general_template PROTO((tree)); | |
140 | static void set_mangled_name_for_template_decl PROTO((tree)); | |
39c01e4c | 141 | static int template_class_depth_real PROTO((tree, int)); |
4393e105 | 142 | static tree tsubst_aggr_type PROTO((tree, tree, int, tree, int)); |
ae58fa02 | 143 | static tree tsubst_decl PROTO((tree, tree, tree, tree)); |
4393e105 MM |
144 | static tree tsubst_arg_types PROTO((tree, tree, int, tree)); |
145 | static tree tsubst_function_type PROTO((tree, tree, int, tree)); | |
6c30752f MM |
146 | static void check_specialization_scope PROTO((void)); |
147 | static tree process_partial_specialization PROTO((tree)); | |
61a127b3 | 148 | static void set_current_access_from_decl PROTO((tree)); |
6ba89f8e | 149 | static void check_default_tmpl_args PROTO((tree, tree, int, int)); |
4393e105 MM |
150 | static tree tsubst_call_declarator_parms PROTO((tree, tree, int, tree)); |
151 | static tree get_template_base_recursive PROTO((tree, tree, | |
152 | tree, tree, tree, int)); | |
00aeceec | 153 | static tree get_template_base PROTO((tree, tree, tree, tree)); |
4393e105 MM |
154 | static tree try_class_unification PROTO((tree, tree, tree, tree)); |
155 | static int coerce_template_template_parms PROTO((tree, tree, int, | |
156 | tree, tree)); | |
bf8f3f93 | 157 | static tree determine_specialization PROTO((tree, tree, tree *, int)); |
d8e178a0 KG |
158 | static int template_args_equal PROTO((tree, tree)); |
159 | static void print_template_context PROTO((int)); | |
9188c363 | 160 | static void tsubst_default_arguments PROTO((tree)); |
36a117a5 MM |
161 | |
162 | /* We use TREE_VECs to hold template arguments. If there is only one | |
163 | level of template arguments, then the TREE_VEC contains the | |
164 | arguments directly. If there is more than one level of template | |
165 | arguments, then each entry in the TREE_VEC is itself a TREE_VEC, | |
166 | containing the template arguments for a single level. The first | |
167 | entry in the outer TREE_VEC is the outermost level of template | |
168 | parameters; the last is the innermost. | |
169 | ||
170 | It is incorrect to ever form a template argument vector containing | |
171 | only one level of arguments, but which is a TREE_VEC containing as | |
172 | its only entry the TREE_VEC for that level. */ | |
173 | ||
36a117a5 MM |
174 | /* Non-zero if the template arguments is actually a vector of vectors, |
175 | rather than just a vector. */ | |
176 | #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ | |
177 | (NODE != NULL_TREE \ | |
178 | && TREE_CODE (NODE) == TREE_VEC \ | |
179 | && TREE_VEC_LENGTH (NODE) > 0 \ | |
180 | && TREE_VEC_ELT (NODE, 0) != NULL_TREE \ | |
67ffc812 MM |
181 | && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) |
182 | ||
e4a84209 MM |
183 | /* The depth of a template argument vector. When called directly by |
184 | the parser, we use a TREE_LIST rather than a TREE_VEC to represent | |
185 | template arguments. In fact, we may even see NULL_TREE if there | |
186 | are no template arguments. In both of those cases, there is only | |
187 | one level of template arguments. */ | |
188 | #define TMPL_ARGS_DEPTH(NODE) \ | |
189 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1) | |
190 | ||
36a117a5 MM |
191 | /* The LEVELth level of the template ARGS. Note that template |
192 | parameter levels are indexed from 1, not from 0. */ | |
193 | #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ | |
194 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ | |
195 | ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS) | |
196 | ||
197 | /* Set the LEVELth level of the template ARGS to VAL. This macro does | |
198 | not work with single-level argument vectors. */ | |
199 | #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \ | |
200 | (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL)) | |
201 | ||
202 | /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */ | |
203 | #define TMPL_ARG(ARGS, LEVEL, IDX) \ | |
204 | (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX)) | |
205 | ||
206 | /* Set the IDXth element in the LEVELth level of ARGS to VAL. This | |
207 | macro does not work with single-level argument vectors. */ | |
208 | #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \ | |
209 | (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL)) | |
210 | ||
e4a84209 MM |
211 | /* Given a single level of template arguments in NODE, return the |
212 | number of arguments. */ | |
213 | #define NUM_TMPL_ARGS(NODE) \ | |
214 | ((NODE) == NULL_TREE ? 0 \ | |
215 | : (TREE_CODE (NODE) == TREE_VEC \ | |
216 | ? TREE_VEC_LENGTH (NODE) : list_length (NODE))) | |
217 | ||
36a117a5 MM |
218 | /* The number of levels of template parameters given by NODE. */ |
219 | #define TMPL_PARMS_DEPTH(NODE) \ | |
220 | (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE))) | |
221 | ||
9cd64686 MM |
222 | /* Called once to initialize pt.c. */ |
223 | ||
224 | void | |
225 | init_pt () | |
226 | { | |
227 | ggc_add_tree_root (&pending_templates, 1); | |
228 | ggc_add_tree_root (&maybe_templates, 1); | |
229 | ggc_add_tree_root (&saved_trees, 1); | |
230 | } | |
231 | ||
e1467ff2 MM |
232 | /* Do any processing required when DECL (a member template declaration |
233 | using TEMPLATE_PARAMETERS as its innermost parameter list) is | |
234 | finished. Returns the TEMPLATE_DECL corresponding to DECL, unless | |
235 | it is a specialization, in which case the DECL itself is returned. */ | |
236 | ||
237 | tree | |
61a127b3 | 238 | finish_member_template_decl (decl) |
e1467ff2 MM |
239 | tree decl; |
240 | { | |
93cdc044 JM |
241 | if (decl == NULL_TREE || decl == void_type_node) |
242 | return NULL_TREE; | |
3ddfb0e6 MM |
243 | else if (decl == error_mark_node) |
244 | /* By returning NULL_TREE, the parser will just ignore this | |
245 | declaration. We have already issued the error. */ | |
246 | return NULL_TREE; | |
93cdc044 JM |
247 | else if (TREE_CODE (decl) == TREE_LIST) |
248 | { | |
5f311aec | 249 | /* Assume that the class is the only declspec. */ |
93cdc044 | 250 | decl = TREE_VALUE (decl); |
8d019cef JM |
251 | if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) |
252 | && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) | |
93cdc044 JM |
253 | { |
254 | tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); | |
255 | check_member_template (tmpl); | |
256 | return tmpl; | |
257 | } | |
8d019cef | 258 | return NULL_TREE; |
93cdc044 | 259 | } |
a1da6cba | 260 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 261 | { |
a1da6cba MM |
262 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
263 | { | |
264 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
265 | return DECL_TI_TEMPLATE (decl); | |
266 | } | |
267 | else | |
268 | return decl; | |
269 | } | |
270 | else | |
8251199e | 271 | cp_error ("invalid member template declaration `%D'", decl); |
e1467ff2 | 272 | |
a1da6cba | 273 | return error_mark_node; |
f84b4be9 | 274 | } |
e1467ff2 | 275 | |
f84b4be9 JM |
276 | /* Returns the template nesting level of the indicated class TYPE. |
277 | ||
278 | For example, in: | |
279 | template <class T> | |
280 | struct A | |
281 | { | |
282 | template <class U> | |
283 | struct B {}; | |
284 | }; | |
285 | ||
39c01e4c MM |
286 | A<T>::B<U> has depth two, while A<T> has depth one. |
287 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
288 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
289 | specializations. | |
290 | ||
291 | This function is guaranteed to return 0 if passed NULL_TREE so | |
292 | that, for example, `template_class_depth (current_class_type)' is | |
293 | always safe. */ | |
f84b4be9 | 294 | |
e9659ab0 | 295 | static int |
39c01e4c | 296 | template_class_depth_real (type, count_specializations) |
f84b4be9 | 297 | tree type; |
39c01e4c | 298 | int count_specializations; |
f84b4be9 | 299 | { |
93cdc044 | 300 | int depth; |
f84b4be9 | 301 | |
2c73f9f5 | 302 | for (depth = 0; |
ed44da02 MM |
303 | type && TREE_CODE (type) != NAMESPACE_DECL; |
304 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
305 | ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type)) | |
306 | { | |
307 | if (TREE_CODE (type) != FUNCTION_DECL) | |
308 | { | |
309 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
310 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
311 | && ((count_specializations | |
312 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
313 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) | |
314 | ++depth; | |
315 | } | |
316 | else | |
317 | { | |
318 | if (DECL_TEMPLATE_INFO (type) | |
319 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
320 | && ((count_specializations | |
321 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
322 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
323 | ++depth; | |
324 | } | |
325 | } | |
f84b4be9 JM |
326 | |
327 | return depth; | |
e1467ff2 | 328 | } |
98c1c668 | 329 | |
39c01e4c MM |
330 | /* Returns the template nesting level of the indicated class TYPE. |
331 | Like template_class_depth_real, but instantiations do not count in | |
332 | the depth. */ | |
333 | ||
334 | int | |
335 | template_class_depth (type) | |
336 | tree type; | |
337 | { | |
338 | return template_class_depth_real (type, /*count_specializations=*/0); | |
339 | } | |
340 | ||
cae40af6 JM |
341 | /* Returns 1 if processing DECL as part of do_pending_inlines |
342 | needs us to push template parms. */ | |
343 | ||
344 | static int | |
345 | inline_needs_template_parms (decl) | |
346 | tree decl; | |
347 | { | |
348 | if (! DECL_TEMPLATE_INFO (decl)) | |
349 | return 0; | |
f84b4be9 | 350 | |
36a117a5 | 351 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
352 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
353 | } | |
354 | ||
355 | /* Subroutine of maybe_begin_member_template_processing. | |
356 | Push the template parms in PARMS, starting from LEVELS steps into the | |
357 | chain, and ending at the beginning, since template parms are listed | |
358 | innermost first. */ | |
359 | ||
360 | static void | |
361 | push_inline_template_parms_recursive (parmlist, levels) | |
362 | tree parmlist; | |
363 | int levels; | |
364 | { | |
365 | tree parms = TREE_VALUE (parmlist); | |
366 | int i; | |
367 | ||
368 | if (levels > 1) | |
369 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 370 | |
98c1c668 | 371 | ++processing_template_decl; |
cae40af6 | 372 | current_template_parms |
98c1c668 JM |
373 | = tree_cons (build_int_2 (0, processing_template_decl), |
374 | parms, current_template_parms); | |
cae40af6 JM |
375 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
376 | ||
786b5245 | 377 | pushlevel (0); |
98c1c668 JM |
378 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
379 | { | |
786b5245 MM |
380 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
381 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0); | |
cae40af6 | 382 | |
98c1c668 JM |
383 | switch (TREE_CODE (parm)) |
384 | { | |
786b5245 | 385 | case TYPE_DECL: |
73b0fce8 | 386 | case TEMPLATE_DECL: |
98c1c668 JM |
387 | pushdecl (parm); |
388 | break; | |
786b5245 MM |
389 | |
390 | case PARM_DECL: | |
391 | { | |
fc03edb3 MM |
392 | /* Make a CONST_DECL as is done in process_template_parm. |
393 | It is ugly that we recreate this here; the original | |
394 | version built in process_template_parm is no longer | |
395 | available. */ | |
786b5245 MM |
396 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
397 | TREE_TYPE (parm)); | |
fc03edb3 | 398 | SET_DECL_ARTIFICIAL (decl); |
786b5245 | 399 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
fc03edb3 | 400 | DECL_TEMPLATE_PARM_P (decl) = 1; |
786b5245 MM |
401 | pushdecl (decl); |
402 | } | |
cae40af6 | 403 | break; |
786b5245 | 404 | |
98c1c668 JM |
405 | default: |
406 | my_friendly_abort (0); | |
407 | } | |
408 | } | |
409 | } | |
410 | ||
cae40af6 JM |
411 | /* Restore the template parameter context for a member template or |
412 | a friend template defined in a class definition. */ | |
413 | ||
414 | void | |
415 | maybe_begin_member_template_processing (decl) | |
416 | tree decl; | |
417 | { | |
418 | tree parms; | |
3dcaad8b | 419 | int levels = 0; |
cae40af6 | 420 | |
3dcaad8b MM |
421 | if (inline_needs_template_parms (decl)) |
422 | { | |
423 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
424 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 425 | |
3dcaad8b MM |
426 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
427 | { | |
428 | --levels; | |
429 | parms = TREE_CHAIN (parms); | |
430 | } | |
cae40af6 | 431 | |
3dcaad8b | 432 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
433 | } |
434 | ||
3dcaad8b MM |
435 | /* Remember how many levels of template parameters we pushed so that |
436 | we can pop them later. */ | |
437 | if (!inline_parm_levels) | |
438 | VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); | |
439 | if (inline_parm_levels_used == inline_parm_levels->num_elements) | |
440 | VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); | |
441 | VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; | |
442 | ++inline_parm_levels_used; | |
cae40af6 JM |
443 | } |
444 | ||
98c1c668 JM |
445 | /* Undo the effects of begin_member_template_processing. */ |
446 | ||
447 | void | |
b370501f | 448 | maybe_end_member_template_processing () |
98c1c668 | 449 | { |
3dcaad8b MM |
450 | int i; |
451 | ||
452 | if (!inline_parm_levels_used) | |
98c1c668 JM |
453 | return; |
454 | ||
3dcaad8b MM |
455 | --inline_parm_levels_used; |
456 | for (i = 0; | |
457 | i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); | |
458 | ++i) | |
cae40af6 JM |
459 | { |
460 | --processing_template_decl; | |
461 | current_template_parms = TREE_CHAIN (current_template_parms); | |
462 | poplevel (0, 0, 0); | |
463 | } | |
98c1c668 JM |
464 | } |
465 | ||
cae40af6 | 466 | /* Returns non-zero iff T is a member template function. We must be |
f84b4be9 | 467 | careful as in |
75650646 MM |
468 | |
469 | template <class T> class C { void f(); } | |
470 | ||
471 | Here, f is a template function, and a member, but not a member | |
472 | template. This function does not concern itself with the origin of | |
473 | T, only its present state. So if we have | |
474 | ||
475 | template <class T> class C { template <class U> void f(U); } | |
476 | ||
477 | then neither C<int>::f<char> nor C<T>::f<double> is considered | |
f181d4ae MM |
478 | to be a member template. But, `template <class U> void |
479 | C<int>::f(U)' is considered a member template. */ | |
98c1c668 | 480 | |
cae40af6 JM |
481 | int |
482 | is_member_template (t) | |
98c1c668 JM |
483 | tree t; |
484 | { | |
f181d4ae | 485 | if (!DECL_FUNCTION_TEMPLATE_P (t)) |
75650646 | 486 | /* Anything that isn't a function or a template function is |
aa5f3bad MM |
487 | certainly not a member template. */ |
488 | return 0; | |
489 | ||
cae40af6 JM |
490 | /* A local class can't have member templates. */ |
491 | if (hack_decl_function_context (t)) | |
492 | return 0; | |
493 | ||
f181d4ae | 494 | return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) |
f84b4be9 JM |
495 | /* If there are more levels of template parameters than |
496 | there are template classes surrounding the declaration, | |
497 | then we have a member template. */ | |
f181d4ae MM |
498 | && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
499 | template_class_depth (DECL_CLASS_CONTEXT (t)))); | |
f84b4be9 | 500 | } |
98c1c668 | 501 | |
ba4f4e5d | 502 | #if 0 /* UNUSED */ |
2ee4e084 MM |
503 | /* Returns non-zero iff T is a member template class. See |
504 | is_member_template for a description of what precisely constitutes | |
505 | a member template. */ | |
506 | ||
507 | int | |
508 | is_member_template_class (t) | |
509 | tree t; | |
510 | { | |
511 | if (!DECL_CLASS_TEMPLATE_P (t)) | |
512 | /* Anything that isn't a class template, is certainly not a member | |
513 | template. */ | |
514 | return 0; | |
515 | ||
516 | if (!DECL_CLASS_SCOPE_P (t)) | |
517 | /* Anything whose context isn't a class type is surely not a | |
518 | member template. */ | |
519 | return 0; | |
520 | ||
521 | /* If there are more levels of template parameters than there are | |
522 | template classes surrounding the declaration, then we have a | |
523 | member template. */ | |
36a117a5 | 524 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
2ee4e084 MM |
525 | template_class_depth (DECL_CONTEXT (t))); |
526 | } | |
ba4f4e5d | 527 | #endif |
2ee4e084 | 528 | |
36a117a5 MM |
529 | /* Return a new template argument vector which contains all of ARGS, |
530 | but has as its innermost set of arguments the EXTRA_ARGS. The | |
531 | resulting vector will be built on a temporary obstack, and so must | |
532 | be explicitly copied to the permanent obstack, if required. */ | |
e6f1275f JM |
533 | |
534 | static tree | |
36a117a5 MM |
535 | add_to_template_args (args, extra_args) |
536 | tree args; | |
537 | tree extra_args; | |
e6f1275f | 538 | { |
36a117a5 MM |
539 | tree new_args; |
540 | int extra_depth; | |
541 | int i; | |
542 | int j; | |
e6f1275f | 543 | |
36a117a5 MM |
544 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
545 | new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth); | |
e6f1275f | 546 | |
36a117a5 MM |
547 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
548 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 549 | |
36a117a5 MM |
550 | for (j = 1; j <= extra_depth; ++j, ++i) |
551 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
552 | ||
e6f1275f JM |
553 | return new_args; |
554 | } | |
555 | ||
36a117a5 MM |
556 | /* Like add_to_template_args, but only the outermost ARGS are added to |
557 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
558 | (EXTRA_ARGS) levels are added. This function is used to combine | |
559 | the template arguments from a partial instantiation with the | |
560 | template arguments used to attain the full instantiation from the | |
561 | partial instantiation. */ | |
98c1c668 | 562 | |
4966381a | 563 | static tree |
36a117a5 | 564 | add_outermost_template_args (args, extra_args) |
98c1c668 JM |
565 | tree args; |
566 | tree extra_args; | |
567 | { | |
568 | tree new_args; | |
569 | ||
e4a84209 MM |
570 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
571 | something very fishy is going on. */ | |
572 | my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), | |
573 | 0); | |
574 | ||
575 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
576 | them. */ | |
577 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
578 | return extra_args; | |
579 | ||
36a117a5 MM |
580 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
581 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
582 | ||
583 | new_args = add_to_template_args (args, extra_args); | |
98c1c668 | 584 | |
36a117a5 MM |
585 | /* Now, we restore ARGS to its full dimensions. */ |
586 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
587 | |
588 | return new_args; | |
589 | } | |
5566b478 MS |
590 | |
591 | /* We've got a template header coming up; push to a new level for storing | |
592 | the parms. */ | |
8d08fdba | 593 | |
8d08fdba MS |
594 | void |
595 | begin_template_parm_list () | |
596 | { | |
6757edfe MM |
597 | /* We use a non-tag-transparent scope here, which causes pushtag to |
598 | put tags in this scope, rather than in the enclosing class or | |
599 | namespace scope. This is the right thing, since we want | |
600 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
601 | global template class, push_template_decl handles putting the | |
602 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
603 | e.g.: | |
604 | ||
605 | template <class T> struct S1 { | |
606 | template <class T> struct S2 {}; | |
607 | }; | |
608 | ||
609 | pushtag contains special code to call pushdecl_with_scope on the | |
610 | TEMPLATE_DECL for S2. */ | |
8d08fdba | 611 | pushlevel (0); |
5566b478 | 612 | declare_pseudo_global_level (); |
5156628f | 613 | ++processing_template_decl; |
67ffc812 | 614 | ++processing_template_parmlist; |
386b8a85 JM |
615 | note_template_header (0); |
616 | } | |
617 | ||
6c30752f MM |
618 | /* This routine is called when a specialization is declared. If it is |
619 | illegal to declare a specialization here, an error is reported. */ | |
620 | ||
e9659ab0 | 621 | static void |
6c30752f MM |
622 | check_specialization_scope () |
623 | { | |
624 | tree scope = current_scope (); | |
3ddfb0e6 | 625 | |
6c30752f MM |
626 | /* [temp.expl.spec] |
627 | ||
628 | An explicit specialization shall be declared in the namespace of | |
629 | which the template is a member, or, for member templates, in the | |
630 | namespace of which the enclosing class or enclosing class | |
631 | template is a member. An explicit specialization of a member | |
632 | function, member class or static data member of a class template | |
633 | shall be declared in the namespace of which the class template | |
634 | is a member. */ | |
635 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
8251199e | 636 | cp_error ("explicit specialization in non-namespace scope `%D'", |
6c30752f | 637 | scope); |
3ddfb0e6 | 638 | |
6c30752f MM |
639 | /* [temp.expl.spec] |
640 | ||
641 | In an explicit specialization declaration for a member of a class | |
642 | template or a member template that appears in namespace scope, | |
643 | the member template and some of its enclosing class templates may | |
644 | remain unspecialized, except that the declaration shall not | |
645 | explicitly specialize a class member template if its enclosing | |
646 | class templates are not explicitly specialized as well. */ | |
647 | if (current_template_parms) | |
fd4de5ff | 648 | cp_error ("enclosing class templates are not explicitly specialized"); |
6c30752f MM |
649 | } |
650 | ||
386b8a85 JM |
651 | /* We've just seen template <>. */ |
652 | ||
653 | void | |
654 | begin_specialization () | |
655 | { | |
656 | note_template_header (1); | |
6c30752f | 657 | check_specialization_scope (); |
386b8a85 JM |
658 | } |
659 | ||
386b8a85 JM |
660 | /* Called at then end of processing a declaration preceeded by |
661 | template<>. */ | |
662 | ||
663 | void | |
664 | end_specialization () | |
665 | { | |
666 | reset_specialization (); | |
667 | } | |
668 | ||
386b8a85 JM |
669 | /* Any template <>'s that we have seen thus far are not referring to a |
670 | function specialization. */ | |
671 | ||
672 | void | |
673 | reset_specialization () | |
674 | { | |
675 | processing_specialization = 0; | |
676 | template_header_count = 0; | |
677 | } | |
678 | ||
386b8a85 JM |
679 | /* We've just seen a template header. If SPECIALIZATION is non-zero, |
680 | it was of the form template <>. */ | |
681 | ||
4966381a | 682 | static void |
386b8a85 JM |
683 | note_template_header (specialization) |
684 | int specialization; | |
685 | { | |
686 | processing_specialization = specialization; | |
687 | template_header_count++; | |
688 | } | |
689 | ||
75650646 | 690 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 691 | |
75650646 MM |
692 | void |
693 | begin_explicit_instantiation () | |
386b8a85 | 694 | { |
75650646 MM |
695 | ++processing_explicit_instantiation; |
696 | } | |
386b8a85 | 697 | |
386b8a85 | 698 | |
75650646 MM |
699 | void |
700 | end_explicit_instantiation () | |
701 | { | |
702 | my_friendly_assert(processing_explicit_instantiation > 0, 0); | |
703 | --processing_explicit_instantiation; | |
704 | } | |
386b8a85 | 705 | |
36a117a5 MM |
706 | /* The TYPE is being declared. If it is a template type, that means it |
707 | is a partial specialization. Do appropriate error-checking. */ | |
708 | ||
709 | void | |
710 | maybe_process_partial_specialization (type) | |
711 | tree type; | |
712 | { | |
713 | if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type)) | |
714 | { | |
715 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) | |
716 | && TYPE_SIZE (type) == NULL_TREE) | |
717 | { | |
700466c2 JM |
718 | if (current_namespace |
719 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
720 | { | |
721 | cp_pedwarn ("specializing `%#T' in different namespace", type); | |
722 | cp_pedwarn_at (" from definition of `%#D'", | |
723 | CLASSTYPE_TI_TEMPLATE (type)); | |
724 | } | |
36a117a5 MM |
725 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
726 | if (processing_template_decl) | |
727 | push_template_decl (TYPE_MAIN_DECL (type)); | |
728 | } | |
729 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
8251199e | 730 | cp_error ("specialization of `%T' after instantiation", type); |
36a117a5 | 731 | } |
20496fa2 JM |
732 | else if (processing_specialization) |
733 | cp_error ("explicit specialization of non-template `%T'", type); | |
36a117a5 MM |
734 | } |
735 | ||
75650646 MM |
736 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
737 | specialization is either an instantiation or an explicit | |
738 | specialization) of TMPL for the given template ARGS. If there is | |
739 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
740 | arguments, or a vector of vectors of arguments, in the case of | |
741 | templates with more than one level of parameters. */ | |
742 | ||
743 | static tree | |
744 | retrieve_specialization (tmpl, args) | |
745 | tree tmpl; | |
746 | tree args; | |
747 | { | |
748 | tree s; | |
749 | ||
750 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
751 | ||
36a117a5 MM |
752 | /* There should be as many levels of arguments as there are |
753 | levels of parameters. */ | |
754 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
755 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
756 | 0); | |
757 | ||
75650646 MM |
758 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
759 | s != NULL_TREE; | |
760 | s = TREE_CHAIN (s)) | |
761 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
762 | return TREE_VALUE (s); | |
763 | ||
764 | return NULL_TREE; | |
386b8a85 JM |
765 | } |
766 | ||
9188c363 MM |
767 | /* Like retrieve_speciailization, but for local declarations. FN is |
768 | the function in which we are looking for an instantiation. */ | |
769 | ||
770 | static tree | |
771 | retrieve_local_specialization (tmpl, fn) | |
772 | tree tmpl; | |
773 | tree fn; | |
774 | { | |
775 | tree s = purpose_member (fn, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
776 | return s ? TREE_VALUE (s) : NULL_TREE; | |
777 | } | |
778 | ||
6757edfe MM |
779 | /* Returns non-zero iff DECL is a specialization of TMPL. */ |
780 | ||
781 | int | |
782 | is_specialization_of (decl, tmpl) | |
783 | tree decl; | |
784 | tree tmpl; | |
785 | { | |
786 | tree t; | |
787 | ||
788 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
789 | { | |
790 | for (t = decl; | |
791 | t != NULL_TREE; | |
792 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
793 | if (t == tmpl) | |
794 | return 1; | |
795 | } | |
796 | else | |
797 | { | |
798 | my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); | |
799 | ||
800 | for (t = TREE_TYPE (decl); | |
801 | t != NULL_TREE; | |
802 | t = CLASSTYPE_USE_TEMPLATE (t) | |
803 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
3bfdc719 MM |
804 | if (same_type_p (TYPE_MAIN_VARIANT (t), |
805 | TYPE_MAIN_VARIANT (TREE_TYPE (tmpl)))) | |
6757edfe MM |
806 | return 1; |
807 | } | |
386b8a85 | 808 | |
6757edfe MM |
809 | return 0; |
810 | } | |
75650646 MM |
811 | |
812 | /* Register the specialization SPEC as a specialization of TMPL with | |
36a117a5 MM |
813 | the indicated ARGS. Returns SPEC, or an equivalent prior |
814 | declaration, if available. */ | |
75650646 | 815 | |
36a117a5 | 816 | static tree |
75650646 MM |
817 | register_specialization (spec, tmpl, args) |
818 | tree spec; | |
819 | tree tmpl; | |
820 | tree args; | |
821 | { | |
822 | tree s; | |
823 | ||
824 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
825 | ||
36a117a5 MM |
826 | if (TREE_CODE (spec) == FUNCTION_DECL |
827 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
828 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
829 | register it; we want the corresponding TEMPLATE_DECL instead. | |
830 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
831 | the more obvious `uses_template_parms (spec)' to avoid problems | |
832 | with default function arguments. In particular, given | |
833 | something like this: | |
834 | ||
835 | template <class T> void f(T t1, T t = T()) | |
836 | ||
837 | the default argument expression is not substituted for in an | |
838 | instantiation unless and until it is actually needed. */ | |
839 | return spec; | |
75650646 | 840 | |
36a117a5 MM |
841 | /* There should be as many levels of arguments as there are |
842 | levels of parameters. */ | |
843 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
844 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
845 | 0); | |
846 | ||
75650646 MM |
847 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
848 | s != NULL_TREE; | |
849 | s = TREE_CHAIN (s)) | |
22e9174f MM |
850 | { |
851 | tree fn = TREE_VALUE (s); | |
852 | ||
853 | /* We can sometimes try to re-register a specialization that we've | |
854 | already got. In particular, regenerate_decl_from_template | |
855 | calls duplicate_decls which will update the specialization | |
856 | list. But, we'll still get called again here anyhow. It's | |
857 | more convenient to simply allow this than to try to prevent it. */ | |
858 | if (fn == spec) | |
859 | return spec; | |
860 | else if (comp_template_args (TREE_PURPOSE (s), args)) | |
861 | { | |
862 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
863 | { | |
864 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
865 | { | |
866 | if (TREE_USED (fn) | |
867 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
868 | { | |
869 | cp_error ("specialization of %D after instantiation", | |
870 | fn); | |
871 | return spec; | |
872 | } | |
873 | else | |
874 | { | |
875 | /* This situation should occur only if the first | |
876 | specialization is an implicit instantiation, | |
877 | the second is an explicit specialization, and | |
878 | the implicit instantiation has not yet been | |
879 | used. That situation can occur if we have | |
880 | implicitly instantiated a member function and | |
881 | then specialized it later. | |
882 | ||
883 | We can also wind up here if a friend | |
884 | declaration that looked like an instantiation | |
885 | turns out to be a specialization: | |
886 | ||
887 | template <class T> void foo(T); | |
888 | class S { friend void foo<>(int) }; | |
889 | template <> void foo(int); | |
890 | ||
891 | We transform the existing DECL in place so that | |
892 | any pointers to it become pointers to the | |
893 | updated declaration. | |
894 | ||
895 | If there was a definition for the template, but | |
896 | not for the specialization, we want this to | |
897 | look as if there is no definition, and vice | |
898 | versa. */ | |
899 | DECL_INITIAL (fn) = NULL_TREE; | |
900 | duplicate_decls (spec, fn); | |
901 | ||
902 | return fn; | |
903 | } | |
904 | } | |
905 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
906 | { | |
907 | duplicate_decls (spec, fn); | |
908 | return fn; | |
909 | } | |
910 | } | |
911 | } | |
75650646 MM |
912 | } |
913 | ||
914 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
e1b3e07d | 915 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); |
36a117a5 MM |
916 | |
917 | return spec; | |
918 | } | |
919 | ||
920 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
921 | Returns nonzero if the SPEC was listed as a specialization of | |
922 | TMPL. */ | |
923 | ||
924 | static int | |
925 | unregister_specialization (spec, tmpl) | |
926 | tree spec; | |
927 | tree tmpl; | |
928 | { | |
929 | tree* s; | |
930 | ||
931 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
932 | *s != NULL_TREE; | |
933 | s = &TREE_CHAIN (*s)) | |
934 | if (TREE_VALUE (*s) == spec) | |
935 | { | |
936 | *s = TREE_CHAIN (*s); | |
937 | return 1; | |
938 | } | |
939 | ||
940 | return 0; | |
75650646 MM |
941 | } |
942 | ||
9188c363 MM |
943 | /* Like register_specialization, but for local declarations. FN is |
944 | the function in which we are registering SPEC, an instantiation of | |
945 | TMPL. */ | |
946 | ||
947 | static tree | |
948 | register_local_specialization (spec, tmpl, fn) | |
949 | tree spec; | |
950 | tree tmpl; | |
951 | tree fn; | |
952 | { | |
953 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
e1b3e07d | 954 | = tree_cons (fn, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); |
9188c363 MM |
955 | |
956 | return spec; | |
957 | } | |
958 | ||
e1467ff2 MM |
959 | /* Print the list of candidate FNS in an error message. */ |
960 | ||
104bf76a | 961 | void |
e1467ff2 MM |
962 | print_candidates (fns) |
963 | tree fns; | |
964 | { | |
965 | tree fn; | |
966 | ||
d8e178a0 | 967 | const char *str = "candidates are:"; |
e1467ff2 MM |
968 | |
969 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
970 | { | |
d6479fe7 MM |
971 | tree f; |
972 | ||
973 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
974 | cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); | |
e1467ff2 MM |
975 | str = " "; |
976 | } | |
977 | } | |
978 | ||
75650646 | 979 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 980 | which can be specialized to match the indicated DECL with the |
03017874 MM |
981 | explicit template args given in TEMPLATE_ID. The DECL may be |
982 | NULL_TREE if none is available. In that case, the functions in | |
983 | TEMPLATE_ID are non-members. | |
984 | ||
985 | If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a | |
986 | specialization of a member template. | |
987 | ||
988 | The template args (those explicitly specified and those deduced) | |
989 | are output in a newly created vector *TARGS_OUT. | |
990 | ||
991 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 992 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 993 | |
e9659ab0 | 994 | static tree |
e1467ff2 | 995 | determine_specialization (template_id, decl, targs_out, |
bf8f3f93 | 996 | need_member_template) |
386b8a85 | 997 | tree template_id; |
e1467ff2 | 998 | tree decl; |
386b8a85 JM |
999 | tree* targs_out; |
1000 | int need_member_template; | |
386b8a85 | 1001 | { |
03017874 MM |
1002 | tree fns; |
1003 | tree targs; | |
1004 | tree explicit_targs; | |
1005 | tree candidates = NULL_TREE; | |
1006 | tree templates = NULL_TREE; | |
386b8a85 | 1007 | |
e1467ff2 MM |
1008 | *targs_out = NULL_TREE; |
1009 | ||
f2e48b67 BK |
1010 | if (template_id == error_mark_node) |
1011 | return error_mark_node; | |
1012 | ||
1013 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1014 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1015 | |
27fafc8d JM |
1016 | if (fns == error_mark_node) |
1017 | return error_mark_node; | |
1018 | ||
2c73f9f5 | 1019 | /* Check for baselinks. */ |
91e490ab | 1020 | if (BASELINK_P (fns)) |
2c73f9f5 | 1021 | fns = TREE_VALUE (fns); |
386b8a85 | 1022 | |
91e490ab MM |
1023 | if (!is_overloaded_fn (fns)) |
1024 | { | |
9dfce8fd | 1025 | cp_error ("`%D' is not a function template", fns); |
91e490ab MM |
1026 | return error_mark_node; |
1027 | } | |
1028 | ||
2c73f9f5 | 1029 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1030 | { |
75650646 MM |
1031 | tree tmpl; |
1032 | ||
9dfce8fd | 1033 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1034 | |
1035 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1036 | /* DECL might be a specialization of FN. */ | |
1037 | tmpl = fn; | |
1038 | else if (need_member_template) | |
1039 | /* FN is an ordinary member function, and we need a | |
1040 | specialization of a member template. */ | |
1041 | continue; | |
1042 | else if (TREE_CODE (fn) != FUNCTION_DECL) | |
1043 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1044 | cases. */ | |
1045 | continue; | |
1046 | else if (!DECL_FUNCTION_MEMBER_P (fn)) | |
1047 | /* This is just an ordinary non-member function. Nothing can | |
1048 | be a specialization of that. */ | |
1049 | continue; | |
75650646 | 1050 | else |
03017874 MM |
1051 | { |
1052 | tree decl_arg_types; | |
386b8a85 | 1053 | |
03017874 MM |
1054 | /* This is an ordinary member function. However, since |
1055 | we're here, we can assume it's enclosing class is a | |
1056 | template class. For example, | |
1057 | ||
1058 | template <typename T> struct S { void f(); }; | |
1059 | template <> void S<int>::f() {} | |
1060 | ||
1061 | Here, S<int>::f is a non-template, but S<int> is a | |
1062 | template class. If FN has the same type as DECL, we | |
1063 | might be in business. */ | |
1064 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), | |
1065 | TREE_TYPE (TREE_TYPE (fn)))) | |
1066 | /* The return types differ. */ | |
1067 | continue; | |
1068 | ||
1069 | /* Adjust the type of DECL in case FN is a static member. */ | |
1070 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1071 | if (DECL_STATIC_FUNCTION_P (fn) | |
1072 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1073 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1074 | ||
1075 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1076 | decl_arg_types)) | |
1077 | /* They match! */ | |
1078 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1079 | ||
1080 | continue; | |
1081 | } | |
386b8a85 | 1082 | |
bf8f3f93 MM |
1083 | /* See whether this function might be a specialization of this |
1084 | template. */ | |
1085 | targs = get_bindings (tmpl, decl, explicit_targs); | |
03017874 MM |
1086 | |
1087 | if (!targs) | |
bf8f3f93 | 1088 | /* We cannot deduce template arguments that when used to |
03017874 MM |
1089 | specialize TMPL will produce DECL. */ |
1090 | continue; | |
1091 | ||
1092 | /* Save this template, and the arguments deduced. */ | |
e1b3e07d | 1093 | templates = tree_cons (targs, tmpl, templates); |
386b8a85 | 1094 | } |
03017874 | 1095 | |
bf8f3f93 | 1096 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1097 | { |
03017874 MM |
1098 | /* We have: |
1099 | ||
1100 | [temp.expl.spec] | |
1101 | ||
1102 | It is possible for a specialization with a given function | |
1103 | signature to be instantiated from more than one function | |
1104 | template. In such cases, explicit specification of the | |
1105 | template arguments must be used to uniquely identify the | |
1106 | function template specialization being specialized. | |
1107 | ||
1108 | Note that here, there's no suggestion that we're supposed to | |
1109 | determine which of the candidate templates is most | |
1110 | specialized. However, we, also have: | |
1111 | ||
1112 | [temp.func.order] | |
1113 | ||
1114 | Partial ordering of overloaded function template | |
1115 | declarations is used in the following contexts to select | |
1116 | the function template to which a function template | |
1117 | specialization refers: | |
1118 | ||
1119 | -- when an explicit specialization refers to a function | |
1120 | template. | |
1121 | ||
1122 | So, we do use the partial ordering rules, at least for now. | |
1123 | This extension can only serve to make illegal programs legal, | |
1124 | so it's safe. And, there is strong anecdotal evidence that | |
1125 | the committee intended the partial ordering rules to apply; | |
1126 | the EDG front-end has that behavior, and John Spicer claims | |
1127 | that the committee simply forgot to delete the wording in | |
1128 | [temp.expl.spec]. */ | |
1129 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1130 | if (tmpl && tmpl != error_mark_node) | |
1131 | { | |
1132 | targs = get_bindings (tmpl, decl, explicit_targs); | |
e1b3e07d | 1133 | templates = tree_cons (targs, tmpl, NULL_TREE); |
03017874 | 1134 | } |
e1467ff2 MM |
1135 | } |
1136 | ||
03017874 | 1137 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1138 | { |
bf8f3f93 MM |
1139 | cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", |
1140 | template_id, decl); | |
03017874 | 1141 | return error_mark_node; |
386b8a85 | 1142 | } |
03017874 | 1143 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1144 | || (candidates && TREE_CHAIN (candidates)) |
1145 | || (templates && candidates)) | |
386b8a85 | 1146 | { |
bf8f3f93 MM |
1147 | cp_error_at ("ambiguous template specialization `%D' for `%+D'", |
1148 | template_id, decl); | |
1149 | chainon (candidates, templates); | |
1150 | print_candidates (candidates); | |
03017874 | 1151 | return error_mark_node; |
386b8a85 JM |
1152 | } |
1153 | ||
1154 | /* We have one, and exactly one, match. */ | |
03017874 MM |
1155 | if (candidates) |
1156 | { | |
1157 | /* It was a specialization of an ordinary member function in a | |
1158 | template class. */ | |
1159 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1160 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1161 | } | |
1162 | ||
1163 | /* It was a specialization of a template. */ | |
1164 | targs = DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates))); | |
1165 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) | |
1166 | { | |
1167 | *targs_out = copy_node (targs); | |
1168 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1169 | TMPL_ARGS_DEPTH (*targs_out), | |
1170 | TREE_PURPOSE (templates)); | |
1171 | } | |
1172 | else | |
1173 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1174 | return TREE_VALUE (templates); |
8d08fdba | 1175 | } |
6757edfe | 1176 | |
386b8a85 | 1177 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1178 | DECLARATOR, and in DECL, is a specialization of a function |
1179 | template. We may also discover that the declaration is an explicit | |
1180 | instantiation at this point. | |
1181 | ||
e1467ff2 | 1182 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1183 | instead if all goes well. Issues an error message if something is |
1184 | amiss. Returns error_mark_node if the error is not easily | |
1185 | recoverable. | |
75650646 | 1186 | |
75650646 MM |
1187 | FLAGS is a bitmask consisting of the following flags: |
1188 | ||
75650646 MM |
1189 | 2: The function has a definition. |
1190 | 4: The function is a friend. | |
75650646 MM |
1191 | |
1192 | The TEMPLATE_COUNT is the number of references to qualifying | |
1193 | template classes that appeared in the name of the function. For | |
1194 | example, in | |
1195 | ||
1196 | template <class T> struct S { void f(); }; | |
1197 | void S<int>::f(); | |
1198 | ||
1199 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1200 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1201 | ||
1202 | template <class T> struct S {}; | |
1203 | template <> struct S<int> { void f(); } | |
36a117a5 | 1204 | template <> void S<int>::f(); |
75650646 MM |
1205 | |
1206 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
1207 | illegal; there should be no template <>.) | |
1208 | ||
1209 | If the function is a specialization, it is marked as such via | |
1210 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1211 | is set up correctly, and it is added to the list of specializations | |
1212 | for that template. */ | |
386b8a85 | 1213 | |
e1467ff2 | 1214 | tree |
27bb8339 | 1215 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
1216 | tree declarator; |
1217 | tree decl; | |
1218 | int template_count; | |
1219 | int flags; | |
1220 | { | |
75650646 MM |
1221 | int have_def = flags & 2; |
1222 | int is_friend = flags & 4; | |
1223 | int specialization = 0; | |
e1467ff2 | 1224 | int explicit_instantiation = 0; |
fd4de5ff | 1225 | int member_specialization = 0; |
75650646 MM |
1226 | |
1227 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
1228 | tree dname = DECL_NAME (decl); | |
386b8a85 | 1229 | |
fd4de5ff | 1230 | if (processing_specialization) |
386b8a85 | 1231 | { |
fd4de5ff | 1232 | /* The last template header was of the form template <>. */ |
75650646 | 1233 | |
fd4de5ff MM |
1234 | if (template_header_count > template_count) |
1235 | { | |
1236 | /* There were more template headers than qualifying template | |
1237 | classes. */ | |
1238 | if (template_header_count - template_count > 1) | |
03017874 MM |
1239 | /* There shouldn't be that many template parameter lists. |
1240 | There can be at most one parameter list for every | |
1241 | qualifying class, plus one for the function itself. */ | |
fd4de5ff | 1242 | cp_error ("too many template parameter lists in declaration of `%D'", decl); |
75650646 | 1243 | |
fd4de5ff MM |
1244 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
1245 | if (ctype) | |
1246 | member_specialization = 1; | |
1247 | else | |
1248 | specialization = 1; | |
75650646 | 1249 | } |
fd4de5ff | 1250 | else if (template_header_count == template_count) |
75650646 | 1251 | { |
fd4de5ff MM |
1252 | /* The counts are equal. So, this might be a |
1253 | specialization, but it is not a specialization of a | |
1254 | member template. It might be something like | |
1255 | ||
1256 | template <class T> struct S { | |
1257 | void f(int i); | |
1258 | }; | |
1259 | template <> | |
1260 | void S<int>::f(int i) {} */ | |
1261 | specialization = 1; | |
1262 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1263 | } |
fd4de5ff | 1264 | else |
386b8a85 | 1265 | { |
fd4de5ff MM |
1266 | /* This cannot be an explicit specialization. There are not |
1267 | enough headers for all of the qualifying classes. For | |
1268 | example, we might have: | |
1269 | ||
1270 | template <> | |
1271 | void S<int>::T<char>::f(); | |
75650646 | 1272 | |
fd4de5ff MM |
1273 | But, we're missing another template <>. */ |
1274 | cp_error("too few template parameter lists in declaration of `%D'", decl); | |
1275 | return decl; | |
1276 | } | |
1277 | } | |
1278 | else if (processing_explicit_instantiation) | |
1279 | { | |
1280 | if (template_header_count) | |
1281 | cp_error ("template parameter list used in explicit instantiation"); | |
1282 | ||
1283 | if (have_def) | |
1284 | cp_error ("definition provided for explicit instantiation"); | |
75650646 | 1285 | |
fd4de5ff MM |
1286 | explicit_instantiation = 1; |
1287 | } | |
1288 | else if (ctype != NULL_TREE | |
1289 | && !TYPE_BEING_DEFINED (ctype) | |
1290 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) | |
1291 | && !is_friend) | |
1292 | { | |
1293 | /* This case catches outdated code that looks like this: | |
1294 | ||
1295 | template <class T> struct S { void f(); }; | |
1296 | void S<int>::f() {} // Missing template <> | |
1297 | ||
1298 | We disable this check when the type is being defined to | |
1299 | avoid complaining about default compiler-generated | |
1300 | constructors, destructors, and assignment operators. | |
1301 | Since the type is an instantiation, not a specialization, | |
1302 | these are the only functions that can be defined before | |
1303 | the class is complete. */ | |
75650646 | 1304 | |
c6f2ed0d JM |
1305 | /* If they said |
1306 | template <class T> void S<int>::f() {} | |
1307 | that's bogus. */ | |
fd4de5ff MM |
1308 | if (template_header_count) |
1309 | { | |
1310 | cp_error ("template parameters specified in specialization"); | |
1311 | return decl; | |
c6f2ed0d | 1312 | } |
fd4de5ff MM |
1313 | |
1314 | if (pedantic) | |
1315 | cp_pedwarn | |
1316 | ("explicit specialization not preceded by `template <>'"); | |
1317 | specialization = 1; | |
1318 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1319 | } | |
1320 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
1321 | { | |
1322 | if (is_friend) | |
1323 | /* This could be something like: | |
1324 | ||
1325 | template <class T> void f(T); | |
1326 | class S { friend void f<>(int); } */ | |
1327 | specialization = 1; | |
1328 | else | |
c6f2ed0d | 1329 | { |
fd4de5ff MM |
1330 | /* This case handles bogus declarations like template <> |
1331 | template <class T> void f<int>(); */ | |
75650646 | 1332 | |
fd4de5ff MM |
1333 | cp_error ("template-id `%D' in declaration of primary template", |
1334 | declarator); | |
1335 | return decl; | |
386b8a85 | 1336 | } |
75650646 | 1337 | } |
386b8a85 | 1338 | |
670960ac JM |
1339 | if (specialization || member_specialization) |
1340 | { | |
1341 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1342 | for (; t; t = TREE_CHAIN (t)) | |
1343 | if (TREE_PURPOSE (t)) | |
1344 | { | |
8251199e JM |
1345 | cp_pedwarn |
1346 | ("default argument specified in explicit specialization"); | |
670960ac JM |
1347 | break; |
1348 | } | |
f3400fe2 JM |
1349 | if (current_lang_name == lang_name_c) |
1350 | cp_error ("template specialization with C linkage"); | |
670960ac JM |
1351 | } |
1352 | ||
e1467ff2 | 1353 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1354 | { |
1355 | tree tmpl = NULL_TREE; | |
1356 | tree targs = NULL_TREE; | |
75650646 MM |
1357 | |
1358 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1359 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1360 | { | |
1361 | tree fns; | |
1362 | ||
1363 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1364 | 0); | |
1365 | if (!ctype) | |
30394414 | 1366 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); |
386b8a85 JM |
1367 | else |
1368 | fns = dname; | |
1369 | ||
75650646 MM |
1370 | declarator = |
1371 | lookup_template_function (fns, NULL_TREE); | |
386b8a85 JM |
1372 | } |
1373 | ||
f2e48b67 BK |
1374 | if (declarator == error_mark_node) |
1375 | return error_mark_node; | |
1376 | ||
75650646 MM |
1377 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1378 | { | |
8ec2ac34 | 1379 | if (!explicit_instantiation) |
6c30752f MM |
1380 | /* A specialization in class scope. This is illegal, |
1381 | but the error will already have been flagged by | |
1382 | check_specialization_scope. */ | |
1383 | return error_mark_node; | |
8ec2ac34 | 1384 | else |
b370501f KG |
1385 | { |
1386 | /* It's not legal to write an explicit instantiation in | |
1387 | class scope, e.g.: | |
8ec2ac34 | 1388 | |
b370501f | 1389 | class C { template void f(); } |
8ec2ac34 | 1390 | |
b370501f KG |
1391 | This case is caught by the parser. However, on |
1392 | something like: | |
8ec2ac34 | 1393 | |
b370501f | 1394 | template class C { void f(); }; |
8ec2ac34 | 1395 | |
b370501f KG |
1396 | (which is illegal) we can get here. The error will be |
1397 | issued later. */ | |
1398 | ; | |
1399 | } | |
8ec2ac34 | 1400 | |
e1467ff2 | 1401 | return decl; |
75650646 | 1402 | } |
8f032717 MM |
1403 | else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) |
1404 | { | |
1405 | /* A friend declaration. We can't do much, because we don't | |
1406 | know what this resolves to, yet. */ | |
1407 | my_friendly_assert (is_friend != 0, 0); | |
1408 | my_friendly_assert (!explicit_instantiation, 0); | |
1409 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
1410 | return decl; | |
1411 | } | |
75650646 MM |
1412 | else if (ctype != NULL_TREE |
1413 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1414 | IDENTIFIER_NODE)) | |
386b8a85 | 1415 | { |
75650646 MM |
1416 | /* Find the list of functions in ctype that have the same |
1417 | name as the declared function. */ | |
1418 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
1419 | tree fns = NULL_TREE; |
1420 | int idx; | |
1421 | ||
75650646 MM |
1422 | if (name == constructor_name (ctype) |
1423 | || name == constructor_name_full (ctype)) | |
386b8a85 | 1424 | { |
75650646 MM |
1425 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1426 | ||
1427 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1428 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1429 | { | |
1430 | /* From [temp.expl.spec]: | |
e1467ff2 | 1431 | |
75650646 MM |
1432 | If such an explicit specialization for the member |
1433 | of a class template names an implicitly-declared | |
1434 | special member function (clause _special_), the | |
e1467ff2 MM |
1435 | program is ill-formed. |
1436 | ||
1437 | Similar language is found in [temp.explicit]. */ | |
8251199e | 1438 | cp_error ("specialization of implicitly-declared special member function"); |
03017874 | 1439 | return error_mark_node; |
75650646 | 1440 | } |
386b8a85 | 1441 | |
42da2fd8 | 1442 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1443 | } |
42da2fd8 | 1444 | |
03017874 MM |
1445 | if (!IDENTIFIER_TYPENAME_P (name)) |
1446 | { | |
1447 | idx = lookup_fnfields_1 (ctype, name); | |
1448 | if (idx >= 0) | |
1449 | fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); | |
1450 | } | |
1451 | else | |
1452 | { | |
1453 | tree methods; | |
1454 | ||
1455 | /* For a type-conversion operator, we cannot do a | |
1456 | name-based lookup. We might be looking for `operator | |
1457 | int' which will be a specialization of `operator T'. | |
1458 | So, we find *all* the conversion operators, and then | |
1459 | select from them. */ | |
1460 | fns = NULL_TREE; | |
1461 | ||
1462 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1463 | if (methods) | |
1464 | for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx) | |
1465 | { | |
1466 | tree ovl = TREE_VEC_ELT (methods, idx); | |
1467 | ||
1468 | if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) | |
1469 | /* There are no more conversion functions. */ | |
1470 | break; | |
1471 | ||
1472 | /* Glue all these conversion functions together | |
1473 | with those we already have. */ | |
1474 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1475 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1476 | } | |
1477 | } | |
1478 | ||
386b8a85 JM |
1479 | if (fns == NULL_TREE) |
1480 | { | |
b3f738da MM |
1481 | cp_error ("no member function `%D' declared in `%T'", |
1482 | name, ctype); | |
03017874 | 1483 | return error_mark_node; |
386b8a85 JM |
1484 | } |
1485 | else | |
1486 | TREE_OPERAND (declarator, 0) = fns; | |
1487 | } | |
75650646 | 1488 | |
e1467ff2 MM |
1489 | /* Figure out what exactly is being specialized at this point. |
1490 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1491 | member function, we cannot tell apriori whether the |
e1467ff2 | 1492 | instantiation is for a member template, or just a member |
36a117a5 MM |
1493 | function of a template class. Even if a member template is |
1494 | being instantiated, the member template arguments may be | |
1495 | elided if they can be deduced from the rest of the | |
1496 | declaration. */ | |
e1467ff2 MM |
1497 | tmpl = determine_specialization (declarator, decl, |
1498 | &targs, | |
bf8f3f93 | 1499 | member_specialization); |
386b8a85 | 1500 | |
03017874 MM |
1501 | if (!tmpl || tmpl == error_mark_node) |
1502 | /* We couldn't figure out what this declaration was | |
1503 | specializing. */ | |
1504 | return error_mark_node; | |
1505 | else | |
386b8a85 | 1506 | { |
25aab5d0 | 1507 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 1508 | |
e1467ff2 MM |
1509 | if (explicit_instantiation) |
1510 | { | |
03d0f4af | 1511 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
25aab5d0 MM |
1512 | is done by do_decl_instantiation later. */ |
1513 | ||
1514 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
1515 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
1516 | ||
1517 | if (arg_depth > parm_depth) | |
1518 | { | |
1519 | /* If TMPL is not the most general template (for | |
1520 | example, if TMPL is a friend template that is | |
1521 | injected into namespace scope), then there will | |
1522 | be too many levels fo TARGS. Remove some of them | |
1523 | here. */ | |
1524 | int i; | |
1525 | tree new_targs; | |
1526 | ||
1527 | new_targs = make_temp_vec (parm_depth); | |
1528 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) | |
1529 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
1530 | = TREE_VEC_ELT (targs, i); | |
1531 | targs = new_targs; | |
1532 | } | |
1533 | ||
1534 | decl = instantiate_template (tmpl, targs); | |
e1467ff2 MM |
1535 | return decl; |
1536 | } | |
36a117a5 MM |
1537 | |
1538 | /* If we though that the DECL was a member function, but it | |
1539 | turns out to be specializing a static member function, | |
1540 | make DECL a static member function as well. */ | |
1541 | if (DECL_STATIC_FUNCTION_P (tmpl) | |
1542 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
366c0f1e MM |
1543 | { |
1544 | revert_static_member_fn (&decl, 0, 0); | |
1545 | last_function_parms = TREE_CHAIN (last_function_parms); | |
1546 | } | |
e1467ff2 | 1547 | |
36a117a5 | 1548 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 1549 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 1550 | |
e1467ff2 MM |
1551 | /* Mangle the function name appropriately. Note that we do |
1552 | not mangle specializations of non-template member | |
1553 | functions of template classes, e.g. with | |
36a117a5 | 1554 | |
e1467ff2 | 1555 | template <class T> struct S { void f(); } |
36a117a5 | 1556 | |
e1467ff2 | 1557 | and given the specialization |
36a117a5 | 1558 | |
e1467ff2 | 1559 | template <> void S<int>::f() {} |
36a117a5 | 1560 | |
e1467ff2 MM |
1561 | we do not mangle S<int>::f() here. That's because it's |
1562 | just an ordinary member function and doesn't need special | |
36a117a5 MM |
1563 | treatment. We do this here so that the ordinary, |
1564 | non-template, name-mangling algorith will not be used | |
1565 | later. */ | |
e1467ff2 | 1566 | if ((is_member_template (tmpl) || ctype == NULL_TREE) |
75650646 | 1567 | && name_mangling_version >= 1) |
36a117a5 | 1568 | set_mangled_name_for_template_decl (decl); |
386b8a85 JM |
1569 | |
1570 | if (is_friend && !have_def) | |
36a117a5 MM |
1571 | /* This is not really a declaration of a specialization. |
1572 | It's just the name of an instantiation. But, it's not | |
1573 | a request for an instantiation, either. */ | |
fbf1c34b | 1574 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
75650646 | 1575 | |
36a117a5 MM |
1576 | /* Register this specialization so that we can find it |
1577 | again. */ | |
1578 | decl = register_specialization (decl, gen_tmpl, targs); | |
386b8a85 JM |
1579 | } |
1580 | } | |
75650646 | 1581 | |
e1467ff2 | 1582 | return decl; |
386b8a85 | 1583 | } |
75650646 | 1584 | |
39c01e4c MM |
1585 | /* TYPE is being declared. Verify that the use of template headers |
1586 | and such is reasonable. Issue error messages if not. */ | |
1587 | ||
1588 | void | |
1589 | maybe_check_template_type (type) | |
1590 | tree type; | |
1591 | { | |
1592 | if (template_header_count) | |
1593 | { | |
1594 | /* We are in the scope of some `template <...>' header. */ | |
1595 | ||
1596 | int context_depth | |
1597 | = template_class_depth_real (TYPE_CONTEXT (type), | |
1598 | /*count_specializations=*/1); | |
1599 | ||
1600 | if (template_header_count <= context_depth) | |
1601 | /* This is OK; the template headers are for the context. We | |
1602 | are actually too lenient here; like | |
1603 | check_explicit_specialization we should consider the number | |
1604 | of template types included in the actual declaration. For | |
1605 | example, | |
1606 | ||
1607 | template <class T> struct S { | |
1608 | template <class U> template <class V> | |
1609 | struct I {}; | |
1610 | }; | |
1611 | ||
1612 | is illegal, but: | |
1613 | ||
1614 | template <class T> struct S { | |
1615 | template <class U> struct I; | |
1616 | }; | |
1617 | ||
1618 | template <class T> template <class U. | |
1619 | struct S<T>::I {}; | |
1620 | ||
1621 | is not. */ | |
1622 | ; | |
1623 | else if (template_header_count > context_depth + 1) | |
1624 | /* There are two many template parameter lists. */ | |
8251199e | 1625 | cp_error ("too many template parameter lists in declaration of `%T'", type); |
39c01e4c MM |
1626 | } |
1627 | } | |
1628 | ||
75650646 MM |
1629 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
1630 | parameters. These are represented in the same format used for | |
1631 | DECL_TEMPLATE_PARMS. */ | |
1632 | ||
1633 | int comp_template_parms (parms1, parms2) | |
1634 | tree parms1; | |
1635 | tree parms2; | |
1636 | { | |
1637 | tree p1; | |
1638 | tree p2; | |
1639 | ||
1640 | if (parms1 == parms2) | |
1641 | return 1; | |
1642 | ||
1643 | for (p1 = parms1, p2 = parms2; | |
1644 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1645 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1646 | { | |
1647 | tree t1 = TREE_VALUE (p1); | |
1648 | tree t2 = TREE_VALUE (p2); | |
1649 | int i; | |
1650 | ||
1651 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1652 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1653 | ||
1654 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1655 | return 0; | |
1656 | ||
1657 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1658 | { | |
1659 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1660 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1661 | ||
1662 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1663 | return 0; | |
1664 | ||
1665 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1666 | continue; | |
3bfdc719 | 1667 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
1668 | return 0; |
1669 | } | |
1670 | } | |
1671 | ||
1672 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1673 | /* One set of parameters has more parameters lists than the | |
1674 | other. */ | |
1675 | return 0; | |
1676 | ||
1677 | return 1; | |
1678 | } | |
1679 | ||
f3400fe2 JM |
1680 | /* Complain if DECL shadows a template parameter. |
1681 | ||
1682 | [temp.local]: A template-parameter shall not be redeclared within its | |
1683 | scope (including nested scopes). */ | |
1684 | ||
1685 | void | |
1686 | check_template_shadow (decl) | |
1687 | tree decl; | |
1688 | { | |
8f032717 MM |
1689 | tree olddecl; |
1690 | ||
b5d9b9ab MM |
1691 | /* If we're not in a template, we can't possibly shadow a template |
1692 | parameter. */ | |
1693 | if (!current_template_parms) | |
1694 | return; | |
1695 | ||
1696 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
1697 | if (TREE_CODE (decl) == OVERLOAD) |
1698 | decl = OVL_CURRENT (decl); | |
1699 | olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); | |
f3400fe2 | 1700 | |
b5d9b9ab MM |
1701 | /* If there's no previous binding for this name, we're not shadowing |
1702 | anything, let alone a template parameter. */ | |
1703 | if (!olddecl) | |
1704 | return; | |
1705 | ||
1706 | /* If we're not shadowing a template parameter, we're done. Note | |
1707 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
1708 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
1709 | node. */ | |
1710 | if (TREE_CODE_CLASS (TREE_CODE (olddecl)) != 'd' | |
1711 | || !DECL_TEMPLATE_PARM_P (olddecl)) | |
1712 | return; | |
1713 | ||
1714 | /* We check for decl != olddecl to avoid bogus errors for using a | |
1715 | name inside a class. We check TPFI to avoid duplicate errors for | |
1716 | inline member templates. */ | |
1717 | if (decl == olddecl | |
1718 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
1719 | return; | |
1720 | ||
1721 | cp_error_at ("declaration of `%#D'", decl); | |
1722 | cp_error_at (" shadows template parm `%#D'", olddecl); | |
f3400fe2 | 1723 | } |
22a7be53 | 1724 | |
f3400fe2 | 1725 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
1726 | ORIG_LEVEL, DECL, and TYPE. */ |
1727 | ||
1728 | static tree | |
1729 | build_template_parm_index (index, level, orig_level, decl, type) | |
1730 | int index; | |
1731 | int level; | |
1732 | int orig_level; | |
1733 | tree decl; | |
1734 | tree type; | |
1735 | { | |
1736 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1737 | TEMPLATE_PARM_IDX (t) = index; | |
1738 | TEMPLATE_PARM_LEVEL (t) = level; | |
1739 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1740 | TEMPLATE_PARM_DECL (t) = decl; | |
1741 | TREE_TYPE (t) = type; | |
1742 | ||
1743 | return t; | |
1744 | } | |
1745 | ||
f84b4be9 | 1746 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 1747 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
1748 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
1749 | new one is created. */ | |
1750 | ||
1751 | static tree | |
93cdc044 | 1752 | reduce_template_parm_level (index, type, levels) |
f84b4be9 JM |
1753 | tree index; |
1754 | tree type; | |
93cdc044 | 1755 | int levels; |
f84b4be9 JM |
1756 | { |
1757 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1758 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 1759 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 JM |
1760 | { |
1761 | tree decl | |
1762 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
1763 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
1764 | type); | |
1765 | tree t | |
1766 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 1767 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
1768 | TEMPLATE_PARM_ORIG_LEVEL (index), |
1769 | decl, type); | |
1770 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
1771 | |
1772 | /* Template template parameters need this. */ | |
1773 | DECL_TEMPLATE_PARMS (decl) | |
1774 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
1775 | } |
1776 | ||
1777 | return TEMPLATE_PARM_DESCENDANTS (index); | |
1778 | } | |
1779 | ||
8d08fdba | 1780 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 1781 | LIST being built. */ |
e92cc029 | 1782 | |
8d08fdba MS |
1783 | tree |
1784 | process_template_parm (list, next) | |
1785 | tree list, next; | |
1786 | { | |
1787 | tree parm; | |
1788 | tree decl = 0; | |
a292b002 | 1789 | tree defval; |
5566b478 | 1790 | int is_type, idx; |
f84b4be9 | 1791 | |
8d08fdba MS |
1792 | parm = next; |
1793 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
1794 | defval = TREE_PURPOSE (parm); |
1795 | parm = TREE_VALUE (parm); | |
1796 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
1797 | |
1798 | if (list) | |
1799 | { | |
1800 | tree p = TREE_VALUE (tree_last (list)); | |
1801 | ||
1802 | if (TREE_CODE (p) == TYPE_DECL) | |
1803 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
73b0fce8 KL |
1804 | else if (TREE_CODE (p) == TEMPLATE_DECL) |
1805 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p))); | |
5566b478 | 1806 | else |
f84b4be9 | 1807 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
1808 | ++idx; |
1809 | } | |
1810 | else | |
1811 | idx = 0; | |
1812 | ||
8d08fdba MS |
1813 | if (!is_type) |
1814 | { | |
a292b002 | 1815 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 1816 | /* is a const-param */ |
a292b002 | 1817 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 1818 | PARM, 0, NULL_TREE); |
d490621d MM |
1819 | |
1820 | /* [temp.param] | |
1821 | ||
1822 | The top-level cv-qualifiers on the template-parameter are | |
1823 | ignored when determining its type. */ | |
1824 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
1825 | ||
8d08fdba MS |
1826 | /* A template parameter is not modifiable. */ |
1827 | TREE_READONLY (parm) = 1; | |
c91a56d2 | 1828 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
410c2fba KL |
1829 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM |
1830 | && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE) | |
8d08fdba | 1831 | { |
8251199e | 1832 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
c91a56d2 MS |
1833 | TREE_TYPE (parm)); |
1834 | if (DECL_NAME (parm) == NULL_TREE) | |
8251199e | 1835 | error (" a template type parameter must begin with `class' or `typename'"); |
8d08fdba MS |
1836 | TREE_TYPE (parm) = void_type_node; |
1837 | } | |
37c46b43 MS |
1838 | else if (pedantic |
1839 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
1840 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
8251199e | 1841 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
eb66be0e | 1842 | TREE_TYPE (parm)); |
8d08fdba MS |
1843 | if (TREE_PERMANENT (parm) == 0) |
1844 | { | |
1845 | parm = copy_node (parm); | |
1846 | TREE_PERMANENT (parm) = 1; | |
1847 | } | |
8d08fdba | 1848 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
f84b4be9 JM |
1849 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
1850 | = build_template_parm_index (idx, processing_template_decl, | |
1851 | processing_template_decl, | |
1852 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
1853 | } |
1854 | else | |
1855 | { | |
73b0fce8 KL |
1856 | tree t; |
1857 | parm = TREE_VALUE (parm); | |
1858 | ||
1859 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
1860 | { | |
1861 | t = make_lang_type (TEMPLATE_TEMPLATE_PARM); | |
1862 | /* This is for distinguishing between real templates and template | |
1863 | template parameters */ | |
1864 | TREE_TYPE (parm) = t; | |
1865 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
1866 | decl = parm; | |
1867 | } | |
1868 | else | |
1869 | { | |
1870 | t = make_lang_type (TEMPLATE_TYPE_PARM); | |
1871 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ | |
1872 | decl = build_decl (TYPE_DECL, parm, t); | |
1873 | } | |
1874 | ||
d2e5ee5c MS |
1875 | TYPE_NAME (t) = decl; |
1876 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 1877 | parm = decl; |
f84b4be9 JM |
1878 | TEMPLATE_TYPE_PARM_INDEX (t) |
1879 | = build_template_parm_index (idx, processing_template_decl, | |
1880 | processing_template_decl, | |
1881 | decl, TREE_TYPE (parm)); | |
8d08fdba | 1882 | } |
8ccc31eb | 1883 | SET_DECL_ARTIFICIAL (decl); |
fc03edb3 | 1884 | DECL_TEMPLATE_PARM_P (decl) = 1; |
8d08fdba | 1885 | pushdecl (decl); |
a292b002 | 1886 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
1887 | return chainon (list, parm); |
1888 | } | |
1889 | ||
1890 | /* The end of a template parameter list has been reached. Process the | |
1891 | tree list into a parameter vector, converting each parameter into a more | |
1892 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
1893 | as PARM_DECLs. */ | |
1894 | ||
1895 | tree | |
1896 | end_template_parm_list (parms) | |
1897 | tree parms; | |
1898 | { | |
5566b478 | 1899 | int nparms; |
8d08fdba | 1900 | tree parm; |
5566b478 MS |
1901 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
1902 | ||
5566b478 MS |
1903 | current_template_parms |
1904 | = tree_cons (build_int_2 (0, processing_template_decl), | |
1905 | saved_parmlist, current_template_parms); | |
8d08fdba | 1906 | |
91e490ab MM |
1907 | for (parm = parms, nparms = 0; |
1908 | parm; | |
1909 | parm = TREE_CHAIN (parm), nparms++) | |
66191c20 | 1910 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 1911 | |
67ffc812 MM |
1912 | --processing_template_parmlist; |
1913 | ||
8d08fdba MS |
1914 | return saved_parmlist; |
1915 | } | |
1916 | ||
5566b478 MS |
1917 | /* end_template_decl is called after a template declaration is seen. */ |
1918 | ||
8d08fdba | 1919 | void |
5566b478 | 1920 | end_template_decl () |
8d08fdba | 1921 | { |
386b8a85 JM |
1922 | reset_specialization (); |
1923 | ||
5156628f | 1924 | if (! processing_template_decl) |
73aad9b9 JM |
1925 | return; |
1926 | ||
5566b478 MS |
1927 | /* This matches the pushlevel in begin_template_parm_list. */ |
1928 | poplevel (0, 0, 0); | |
8d08fdba | 1929 | |
5566b478 MS |
1930 | --processing_template_decl; |
1931 | current_template_parms = TREE_CHAIN (current_template_parms); | |
1932 | (void) get_pending_sizes (); /* Why? */ | |
1933 | } | |
8d08fdba | 1934 | |
36a117a5 MM |
1935 | /* Given a template argument vector containing the template PARMS. |
1936 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
1937 | |
1938 | tree | |
1939 | current_template_args () | |
5566b478 | 1940 | { |
36a117a5 | 1941 | tree header; |
b370501f | 1942 | tree args = NULL_TREE; |
36a117a5 | 1943 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
1944 | int l = length; |
1945 | ||
36a117a5 MM |
1946 | /* If there is only one level of template parameters, we do not |
1947 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
1948 | TREE_VEC containing the arguments. */ | |
1949 | if (length > 1) | |
1950 | args = make_tree_vec (length); | |
1951 | ||
1952 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 1953 | { |
5566b478 | 1954 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
1955 | int i; |
1956 | ||
5566b478 | 1957 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 1958 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 1959 | { |
98c1c668 JM |
1960 | tree t = TREE_VEC_ELT (a, i); |
1961 | ||
36a117a5 | 1962 | /* T will be a list if we are called from within a |
98c1c668 JM |
1963 | begin/end_template_parm_list pair, but a vector directly |
1964 | if within a begin/end_member_template_processing pair. */ | |
1965 | if (TREE_CODE (t) == TREE_LIST) | |
1966 | { | |
1967 | t = TREE_VALUE (t); | |
1968 | ||
73b0fce8 KL |
1969 | if (TREE_CODE (t) == TYPE_DECL |
1970 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
1971 | t = TREE_TYPE (t); |
1972 | else | |
1973 | t = DECL_INITIAL (t); | |
36a117a5 | 1974 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 1975 | } |
5566b478 | 1976 | } |
36a117a5 MM |
1977 | |
1978 | if (length > 1) | |
1979 | TREE_VEC_ELT (args, --l) = a; | |
1980 | else | |
1981 | args = a; | |
8d08fdba MS |
1982 | } |
1983 | ||
9a3b49ac MS |
1984 | return args; |
1985 | } | |
75650646 | 1986 | |
e1467ff2 MM |
1987 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
1988 | template PARMS. Used by push_template_decl below. */ | |
1989 | ||
75650646 MM |
1990 | static tree |
1991 | build_template_decl (decl, parms) | |
1992 | tree decl; | |
1993 | tree parms; | |
1994 | { | |
1995 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
1996 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
1997 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
1998 | if (DECL_LANG_SPECIFIC (decl)) | |
1999 | { | |
2000 | DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); | |
8f032717 MM |
2001 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
2002 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
11f98788 | 2003 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
75650646 MM |
2004 | } |
2005 | ||
2006 | return tmpl; | |
2007 | } | |
2008 | ||
050367a3 MM |
2009 | struct template_parm_data |
2010 | { | |
6c30752f MM |
2011 | /* The level of the template parameters we are currently |
2012 | processing. */ | |
050367a3 | 2013 | int level; |
6c30752f MM |
2014 | |
2015 | /* The index of the specialization argument we are currently | |
2016 | processing. */ | |
2017 | int current_arg; | |
2018 | ||
2019 | /* An array whose size is the number of template parameters. The | |
2020 | elements are non-zero if the parameter has been used in any one | |
2021 | of the arguments processed so far. */ | |
050367a3 | 2022 | int* parms; |
6c30752f MM |
2023 | |
2024 | /* An array whose size is the number of template arguments. The | |
2025 | elements are non-zero if the argument makes use of template | |
2026 | parameters of this level. */ | |
2027 | int* arg_uses_template_parms; | |
050367a3 MM |
2028 | }; |
2029 | ||
2030 | /* Subroutine of push_template_decl used to see if each template | |
2031 | parameter in a partial specialization is used in the explicit | |
2032 | argument list. If T is of the LEVEL given in DATA (which is | |
2033 | treated as a template_parm_data*), then DATA->PARMS is marked | |
2034 | appropriately. */ | |
2035 | ||
2036 | static int | |
2037 | mark_template_parm (t, data) | |
2038 | tree t; | |
2039 | void* data; | |
2040 | { | |
2041 | int level; | |
2042 | int idx; | |
2043 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
2044 | ||
2045 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2046 | { | |
2047 | level = TEMPLATE_PARM_LEVEL (t); | |
2048 | idx = TEMPLATE_PARM_IDX (t); | |
2049 | } | |
2050 | else | |
2051 | { | |
2052 | level = TEMPLATE_TYPE_LEVEL (t); | |
2053 | idx = TEMPLATE_TYPE_IDX (t); | |
2054 | } | |
2055 | ||
2056 | if (level == tpd->level) | |
6c30752f MM |
2057 | { |
2058 | tpd->parms[idx] = 1; | |
2059 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
2060 | } | |
050367a3 MM |
2061 | |
2062 | /* Return zero so that for_each_template_parm will continue the | |
2063 | traversal of the tree; we want to mark *every* template parm. */ | |
2064 | return 0; | |
2065 | } | |
2066 | ||
6c30752f MM |
2067 | /* Process the partial specialization DECL. */ |
2068 | ||
e9659ab0 | 2069 | static tree |
6c30752f MM |
2070 | process_partial_specialization (decl) |
2071 | tree decl; | |
2072 | { | |
2073 | tree type = TREE_TYPE (decl); | |
2074 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
2075 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
2076 | tree inner_args = innermost_args (specargs); | |
2077 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
2078 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
2079 | int nargs = TREE_VEC_LENGTH (inner_args); | |
2080 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2081 | int i; | |
2082 | int did_error_intro = 0; | |
6c30752f MM |
2083 | struct template_parm_data tpd; |
2084 | struct template_parm_data tpd2; | |
2085 | ||
6c30752f MM |
2086 | /* We check that each of the template parameters given in the |
2087 | partial specialization is used in the argument list to the | |
2088 | specialization. For example: | |
2089 | ||
2090 | template <class T> struct S; | |
2091 | template <class T> struct S<T*>; | |
2092 | ||
2093 | The second declaration is OK because `T*' uses the template | |
2094 | parameter T, whereas | |
2095 | ||
2096 | template <class T> struct S<int>; | |
2097 | ||
2098 | is no good. Even trickier is: | |
2099 | ||
2100 | template <class T> | |
2101 | struct S1 | |
2102 | { | |
2103 | template <class U> | |
2104 | struct S2; | |
2105 | template <class U> | |
2106 | struct S2<T>; | |
2107 | }; | |
2108 | ||
2109 | The S2<T> declaration is actually illegal; it is a | |
2110 | full-specialization. Of course, | |
2111 | ||
2112 | template <class U> | |
2113 | struct S2<T (*)(U)>; | |
2114 | ||
2115 | or some such would have been OK. */ | |
2116 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2117 | tpd.parms = alloca (sizeof (int) * ntparms); | |
1ddb2906 | 2118 | bzero ((PTR) tpd.parms, sizeof (int) * ntparms); |
6c30752f MM |
2119 | |
2120 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
1ddb2906 | 2121 | bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs); |
6c30752f MM |
2122 | for (i = 0; i < nargs; ++i) |
2123 | { | |
2124 | tpd.current_arg = i; | |
2125 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2126 | &mark_template_parm, | |
2127 | &tpd); | |
2128 | } | |
2129 | for (i = 0; i < ntparms; ++i) | |
2130 | if (tpd.parms[i] == 0) | |
2131 | { | |
2132 | /* One of the template parms was not used in the | |
2133 | specialization. */ | |
2134 | if (!did_error_intro) | |
2135 | { | |
8251199e | 2136 | cp_error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2137 | did_error_intro = 1; |
2138 | } | |
2139 | ||
8251199e | 2140 | cp_error (" `%D'", |
6c30752f MM |
2141 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
2142 | } | |
2143 | ||
2144 | /* [temp.class.spec] | |
2145 | ||
2146 | The argument list of the specialization shall not be identical to | |
2147 | the implicit argument list of the primary template. */ | |
2148 | if (comp_template_args (inner_args, | |
2149 | innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2150 | (maintmpl))))) | |
8251199e | 2151 | cp_error ("partial specialization `%T' does not specialize any template arguments", type); |
6c30752f MM |
2152 | |
2153 | /* [temp.class.spec] | |
2154 | ||
2155 | A partially specialized non-type argument expression shall not | |
2156 | involve template parameters of the partial specialization except | |
2157 | when the argument expression is a simple identifier. | |
2158 | ||
2159 | The type of a template parameter corresponding to a specialized | |
2160 | non-type argument shall not be dependent on a parameter of the | |
2161 | specialization. */ | |
2162 | my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); | |
2163 | tpd2.parms = 0; | |
2164 | for (i = 0; i < nargs; ++i) | |
2165 | { | |
2166 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2167 | if (/* These first two lines are the `non-type' bit. */ | |
2168 | TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
2169 | && TREE_CODE (arg) != TEMPLATE_DECL | |
2170 | /* This next line is the `argument expression is not just a | |
2171 | simple identifier' condition and also the `specialized | |
2172 | non-type argument' bit. */ | |
2173 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2174 | { | |
2175 | if (tpd.arg_uses_template_parms[i]) | |
8251199e | 2176 | cp_error ("template argument `%E' involves template parameter(s)", arg); |
6c30752f MM |
2177 | else |
2178 | { | |
2179 | /* Look at the corresponding template parameter, | |
2180 | marking which template parameters its type depends | |
2181 | upon. */ | |
2182 | tree type = | |
2183 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2184 | i))); | |
2185 | ||
2186 | if (!tpd2.parms) | |
2187 | { | |
2188 | /* We haven't yet initialized TPD2. Do so now. */ | |
2189 | tpd2.arg_uses_template_parms | |
2190 | = (int*) alloca (sizeof (int) * nargs); | |
104bf76a | 2191 | /* The number of parameters here is the number in the |
76a83782 RH |
2192 | main template, which, as checked in the assertion |
2193 | above, is NARGS. */ | |
6c30752f MM |
2194 | tpd2.parms = (int*) alloca (sizeof (int) * nargs); |
2195 | tpd2.level = | |
2196 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2197 | } | |
2198 | ||
104bf76a | 2199 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2200 | looking for the template parameters of the main |
2201 | template, not in the specialization. */ | |
2202 | tpd2.current_arg = i; | |
2203 | tpd2.arg_uses_template_parms[i] = 0; | |
1ddb2906 | 2204 | bzero ((PTR) tpd2.parms, sizeof (int) * nargs); |
6c30752f MM |
2205 | for_each_template_parm (type, |
2206 | &mark_template_parm, | |
2207 | &tpd2); | |
2208 | ||
2209 | if (tpd2.arg_uses_template_parms [i]) | |
2210 | { | |
2211 | /* The type depended on some template parameters. | |
2212 | If they are fully specialized in the | |
2213 | specialization, that's OK. */ | |
2214 | int j; | |
2215 | for (j = 0; j < nargs; ++j) | |
2216 | if (tpd2.parms[j] != 0 | |
2217 | && tpd.arg_uses_template_parms [j]) | |
2218 | { | |
104bf76a | 2219 | cp_error ("type `%T' of template argument `%E' depends on template parameter(s)", |
6c30752f MM |
2220 | type, |
2221 | arg); | |
2222 | break; | |
2223 | } | |
2224 | } | |
2225 | } | |
2226 | } | |
2227 | } | |
2228 | ||
2229 | if (retrieve_specialization (maintmpl, specargs)) | |
2230 | /* We've already got this specialization. */ | |
2231 | return decl; | |
2232 | ||
2233 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) | |
e1b3e07d MM |
2234 | = tree_cons (inner_args, inner_parms, |
2235 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
6c30752f MM |
2236 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
2237 | return decl; | |
2238 | } | |
2239 | ||
6ba89f8e MM |
2240 | /* Check that a template declaration's use of default arguments is not |
2241 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
2242 | non-zero if DECL is the thing declared by a primary template. | |
2243 | IS_PARTIAL is non-zero if DECL is a partial specialization. */ | |
2244 | ||
2245 | static void | |
2246 | check_default_tmpl_args (decl, parms, is_primary, is_partial) | |
2247 | tree decl; | |
2248 | tree parms; | |
2249 | int is_primary; | |
2250 | int is_partial; | |
2251 | { | |
d8e178a0 | 2252 | const char *msg; |
66191c20 MM |
2253 | int last_level_to_check; |
2254 | tree parm_level; | |
6ba89f8e MM |
2255 | |
2256 | /* [temp.param] | |
2257 | ||
2258 | A default template-argument shall not be specified in a | |
2259 | function template declaration or a function template definition, nor | |
2260 | in the template-parameter-list of the definition of a member of a | |
2261 | class template. */ | |
2262 | ||
e0942dcd MM |
2263 | if (TREE_CODE (DECL_REAL_CONTEXT (decl)) == FUNCTION_DECL) |
2264 | /* You can't have a function template declaration in a local | |
2265 | scope, nor you can you define a member of a class template in a | |
2266 | local scope. */ | |
2267 | return; | |
2268 | ||
6ba89f8e MM |
2269 | if (current_class_type |
2270 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 2271 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
2272 | /* If this is either a friend defined in the scope of the class |
2273 | or a member function. */ | |
2274 | && DECL_CLASS_CONTEXT (decl) == current_class_type | |
2275 | /* And, if it was a member function, it really was defined in | |
2276 | the scope of the class. */ | |
2277 | && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_DEFINED_IN_CLASS_P (decl))) | |
6ba89f8e | 2278 | /* We already checked these parameters when the template was |
5937a6f9 MM |
2279 | declared, so there's no need to do it again now. This function |
2280 | was defined in class scope, but we're processing it's body now | |
2281 | that the class is complete. */ | |
6ba89f8e MM |
2282 | return; |
2283 | ||
66191c20 MM |
2284 | /* [temp.param] |
2285 | ||
2286 | If a template-parameter has a default template-argument, all | |
2287 | subsequent template-parameters shall have a default | |
2288 | template-argument supplied. */ | |
2289 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
2290 | { | |
2291 | tree inner_parms = TREE_VALUE (parm_level); | |
2292 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2293 | int seen_def_arg_p = 0; | |
2294 | int i; | |
2295 | ||
2296 | for (i = 0; i < ntparms; ++i) | |
2297 | { | |
2298 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
2299 | if (TREE_PURPOSE (parm)) | |
2300 | seen_def_arg_p = 1; | |
2301 | else if (seen_def_arg_p) | |
2302 | { | |
2303 | cp_error ("no default argument for `%D'", TREE_VALUE (parm)); | |
2304 | /* For better subsequent error-recovery, we indicate that | |
2305 | there should have been a default argument. */ | |
2306 | TREE_PURPOSE (parm) = error_mark_node; | |
2307 | } | |
2308 | } | |
2309 | } | |
2310 | ||
6ba89f8e MM |
2311 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) |
2312 | /* For an ordinary class template, default template arguments are | |
2313 | allowed at the innermost level, e.g.: | |
2314 | template <class T = int> | |
2315 | struct S {}; | |
2316 | but, in a partial specialization, they're not allowed even | |
2317 | there, as we have in [temp.class.spec]: | |
2318 | ||
2319 | The template parameter list of a specialization shall not | |
2320 | contain default template argument values. | |
2321 | ||
2322 | So, for a partial specialization, or for a function template, | |
2323 | we look at all of them. */ | |
2324 | ; | |
2325 | else | |
2326 | /* But, for a primary class template that is not a partial | |
2327 | specialization we look at all template parameters except the | |
2328 | innermost ones. */ | |
2329 | parms = TREE_CHAIN (parms); | |
2330 | ||
2331 | /* Figure out what error message to issue. */ | |
2332 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2333 | msg = "default argument for template parameter in function template `%D'"; | |
2334 | else if (is_partial) | |
2335 | msg = "default argument in partial specialization `%D'"; | |
2336 | else | |
2337 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2338 | ||
2339 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2340 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2341 | examine the parameters to the class itself. On the one |
6ba89f8e MM |
2342 | hand, they will be checked when the class is defined, and, |
2343 | on the other, default arguments are legal in things like: | |
2344 | template <class T = double> | |
2345 | struct S { template <class U> void f(U); }; | |
2346 | Here the default argument for `S' has no bearing on the | |
2347 | declaration of `f'. */ | |
2348 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2349 | else | |
2350 | /* Check everything. */ | |
2351 | last_level_to_check = 0; | |
2352 | ||
66191c20 MM |
2353 | for (parm_level = parms; |
2354 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
2355 | parm_level = TREE_CHAIN (parm_level)) | |
6ba89f8e | 2356 | { |
66191c20 MM |
2357 | tree inner_parms = TREE_VALUE (parm_level); |
2358 | int i; | |
2359 | int ntparms; | |
6ba89f8e MM |
2360 | |
2361 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2362 | for (i = 0; i < ntparms; ++i) | |
2363 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2364 | { | |
2365 | if (msg) | |
2366 | { | |
2367 | cp_error (msg, decl); | |
2368 | msg = 0; | |
2369 | } | |
2370 | ||
2371 | /* Clear out the default argument so that we are not | |
2372 | confused later. */ | |
2373 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2374 | } | |
2375 | ||
2376 | /* At this point, if we're still interested in issuing messages, | |
2377 | they must apply to classes surrounding the object declared. */ | |
2378 | if (msg) | |
2379 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2380 | } | |
2381 | } | |
2382 | ||
3ac3d9ea | 2383 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2384 | parameters given by current_template_args, or reuses a |
2385 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2386 | equivalent one, if it is replaced via a call to duplicate_decls. |
2387 | ||
2388 | If IS_FRIEND is non-zero, DECL is a friend declaration. */ | |
3ac3d9ea MM |
2389 | |
2390 | tree | |
6757edfe | 2391 | push_template_decl_real (decl, is_friend) |
9a3b49ac | 2392 | tree decl; |
6757edfe | 2393 | int is_friend; |
9a3b49ac MS |
2394 | { |
2395 | tree tmpl; | |
f84b4be9 | 2396 | tree args; |
9a3b49ac | 2397 | tree info; |
f84b4be9 JM |
2398 | tree ctx; |
2399 | int primary; | |
6ba89f8e | 2400 | int is_partial; |
cfe507be | 2401 | int new_template_p = 0; |
6ba89f8e MM |
2402 | |
2403 | /* See if this is a partial specialization. */ | |
9188c363 | 2404 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e MM |
2405 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
2406 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); | |
6757edfe MM |
2407 | |
2408 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2409 | |
2410 | if (is_friend) | |
2411 | /* For a friend, we want the context of the friend function, not | |
2412 | the type of which it is a friend. */ | |
2413 | ctx = DECL_CONTEXT (decl); | |
2c73f9f5 ML |
2414 | else if (DECL_REAL_CONTEXT (decl) |
2415 | && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2416 | /* In the case of a virtual function, we want the class in which |
2417 | it is defined. */ | |
2418 | ctx = DECL_REAL_CONTEXT (decl); | |
2419 | else | |
2420 | /* Otherwise, if we're currently definining some class, the DECL | |
2421 | is assumed to be a member of the class. */ | |
9188c363 | 2422 | ctx = current_scope (); |
f84b4be9 | 2423 | |
2c73f9f5 ML |
2424 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2425 | ctx = NULL_TREE; | |
2426 | ||
2427 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2428 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2429 | |
6ba89f8e | 2430 | /* See if this is a primary template. */ |
9188c363 | 2431 | primary = pseudo_global_level_p (); |
9a3b49ac | 2432 | |
83566abf JM |
2433 | if (primary) |
2434 | { | |
2435 | if (current_lang_name == lang_name_c) | |
8251199e | 2436 | cp_error ("template with C linkage"); |
4ce3d537 MM |
2437 | else if (TREE_CODE (decl) == TYPE_DECL |
2438 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
8251199e | 2439 | cp_error ("template class without a name"); |
2aaf816d JM |
2440 | else if ((DECL_IMPLICIT_TYPEDEF_P (decl) |
2441 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2442 | || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)) | |
2443 | || TREE_CODE (decl) == FUNCTION_DECL) | |
2444 | /* OK */; | |
2445 | else | |
4ce3d537 | 2446 | cp_error ("template declaration of `%#D'", decl); |
83566abf JM |
2447 | } |
2448 | ||
6ba89f8e MM |
2449 | /* Check to see that the rules regarding the use of default |
2450 | arguments are not being violated. */ | |
2451 | check_default_tmpl_args (decl, current_template_parms, | |
2452 | primary, is_partial); | |
73aad9b9 | 2453 | |
6ba89f8e MM |
2454 | if (is_partial) |
2455 | return process_partial_specialization (decl); | |
d32789d8 | 2456 | |
9a3b49ac MS |
2457 | args = current_template_args (); |
2458 | ||
f84b4be9 JM |
2459 | if (!ctx |
2460 | || TREE_CODE (ctx) == FUNCTION_DECL | |
2461 | || TYPE_BEING_DEFINED (ctx) | |
2462 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
8d08fdba | 2463 | { |
75650646 | 2464 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
2465 | && DECL_TEMPLATE_INFO (decl) |
2466 | && DECL_TI_TEMPLATE (decl)) | |
2467 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
2468 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
2469 | be DECL_LANG_SPECIFIC. The information equivalent to | |
2470 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
2471 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) | |
2472 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2473 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2474 | { | |
2475 | /* Since a template declaration already existed for this | |
2476 | class-type, we must be redeclaring it here. Make sure | |
2477 | that the redeclaration is legal. */ | |
2478 | redeclare_class_template (TREE_TYPE (decl), | |
2479 | current_template_parms); | |
2480 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
2481 | one we already had. */ | |
2482 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
2483 | } | |
f84b4be9 | 2484 | else |
786b5245 | 2485 | { |
f84b4be9 | 2486 | tmpl = build_template_decl (decl, current_template_parms); |
cfe507be MM |
2487 | new_template_p = 1; |
2488 | ||
f84b4be9 JM |
2489 | if (DECL_LANG_SPECIFIC (decl) |
2490 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2491 | { | |
2492 | /* A specialization of a member template of a template | |
2493 | class. */ | |
2494 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
2495 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
2496 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
2497 | } | |
786b5245 | 2498 | } |
8d08fdba MS |
2499 | } |
2500 | else | |
2501 | { | |
e1a5ccf7 | 2502 | tree a, t, current, parms; |
ba4f4e5d | 2503 | int i; |
6633d636 | 2504 | |
73aad9b9 | 2505 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
8251199e | 2506 | cp_error ("must specialize `%#T' before defining member `%#D'", |
73aad9b9 | 2507 | ctx, decl); |
6b9ab5cc MM |
2508 | if (TREE_CODE (decl) == TYPE_DECL) |
2509 | { | |
ed44da02 MM |
2510 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
2511 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
2512 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2513 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2514 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
2515 | else |
2516 | { | |
8251199e | 2517 | cp_error ("`%D' does not declare a template type", decl); |
6b9ab5cc MM |
2518 | return decl; |
2519 | } | |
2520 | } | |
fc378698 | 2521 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 2522 | { |
8251199e | 2523 | cp_error ("template definition of non-template `%#D'", decl); |
3ac3d9ea | 2524 | return decl; |
c91a56d2 | 2525 | } |
8d08fdba | 2526 | else |
5566b478 | 2527 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 2528 | |
e1a5ccf7 JM |
2529 | if (is_member_template (tmpl) |
2530 | && DECL_FUNCTION_TEMPLATE_P (tmpl) | |
2531 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) | |
2532 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
98c1c668 | 2533 | { |
e1a5ccf7 JM |
2534 | tree new_tmpl; |
2535 | ||
2536 | /* The declaration is a specialization of a member | |
2537 | template, declared outside the class. Therefore, the | |
2538 | innermost template arguments will be NULL, so we | |
2539 | replace them with the arguments determined by the | |
2540 | earlier call to check_explicit_specialization. */ | |
2541 | args = DECL_TI_ARGS (decl); | |
2542 | ||
2543 | new_tmpl | |
2544 | = build_template_decl (decl, current_template_parms); | |
2545 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
2546 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
2547 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
2548 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
e1b3e07d MM |
2549 | DECL_TEMPLATE_INFO (new_tmpl) |
2550 | = tree_cons (tmpl, args, NULL_TREE); | |
e1a5ccf7 JM |
2551 | |
2552 | register_specialization (new_tmpl, tmpl, args); | |
2553 | return decl; | |
98c1c668 | 2554 | } |
98c1c668 | 2555 | |
e1a5ccf7 | 2556 | /* Make sure the template headers we got make sense. */ |
6633d636 | 2557 | |
e1a5ccf7 JM |
2558 | parms = DECL_TEMPLATE_PARMS (tmpl); |
2559 | i = TMPL_PARMS_DEPTH (parms); | |
2560 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 2561 | { |
e1a5ccf7 JM |
2562 | cp_error ("expected %d levels of template parms for `%#D', got %d", |
2563 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 2564 | } |
e1a5ccf7 JM |
2565 | else |
2566 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
2567 | { | |
2568 | a = TMPL_ARGS_LEVEL (args, i); | |
2569 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
2570 | ||
2571 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
2572 | { | |
2573 | if (current == decl) | |
2574 | cp_error ("got %d template parameters for `%#D'", | |
2575 | TREE_VEC_LENGTH (a), decl); | |
2576 | else | |
2577 | cp_error ("got %d template parameters for `%#T'", | |
2578 | TREE_VEC_LENGTH (a), current); | |
2579 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
2580 | } | |
98c1c668 | 2581 | |
e1a5ccf7 JM |
2582 | /* Perhaps we should also check that the parms are used in the |
2583 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 2584 | |
e1a5ccf7 JM |
2585 | if (current == decl) |
2586 | current = ctx; | |
2587 | else | |
2588 | current = TYPE_CONTEXT (current); | |
2589 | } | |
5566b478 | 2590 | } |
8d08fdba | 2591 | |
5566b478 MS |
2592 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
2593 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 2594 | |
36a117a5 MM |
2595 | /* Push template declarations for global functions and types. Note |
2596 | that we do not try to push a global template friend declared in a | |
2597 | template class; such a thing may well depend on the template | |
39c01e4c | 2598 | parameters of the class. */ |
cfe507be | 2599 | if (new_template_p && !ctx |
36a117a5 | 2600 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
2c73f9f5 | 2601 | tmpl = pushdecl_namespace_level (tmpl); |
8d08fdba | 2602 | |
5566b478 | 2603 | if (primary) |
6757edfe | 2604 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
5566b478 | 2605 | |
e1b3e07d | 2606 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 2607 | |
9188c363 | 2608 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
8d08fdba | 2609 | { |
ed44da02 MM |
2610 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2611 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
2612 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE) | |
75650646 | 2613 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 2614 | } |
2aaf816d | 2615 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 2616 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
2617 | |
2618 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
2619 | } |
2620 | ||
6757edfe MM |
2621 | tree |
2622 | push_template_decl (decl) | |
2623 | tree decl; | |
2624 | { | |
2625 | return push_template_decl_real (decl, 0); | |
2626 | } | |
2627 | ||
2628 | /* Called when a class template TYPE is redeclared with the indicated | |
2629 | template PARMS, e.g.: | |
7fe6899f MM |
2630 | |
2631 | template <class T> struct S; | |
2632 | template <class T> struct S {}; */ | |
2633 | ||
2634 | void | |
6757edfe | 2635 | redeclare_class_template (type, parms) |
7fe6899f | 2636 | tree type; |
6757edfe | 2637 | tree parms; |
7fe6899f | 2638 | { |
3d7de1fa | 2639 | tree tmpl; |
6757edfe | 2640 | tree tmpl_parms; |
7fe6899f MM |
2641 | int i; |
2642 | ||
3d7de1fa MM |
2643 | if (!TYPE_TEMPLATE_INFO (type)) |
2644 | { | |
2645 | cp_error ("`%T' is not a template type", type); | |
2646 | return; | |
2647 | } | |
2648 | ||
2649 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
2650 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
2651 | /* The type is nested in some template class. Nothing to worry | |
2652 | about here; there are no new template parameters for the nested | |
2653 | type. */ | |
2654 | return; | |
2655 | ||
6757edfe MM |
2656 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
2657 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
2658 | ||
7fe6899f MM |
2659 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
2660 | { | |
8251199e JM |
2661 | cp_error_at ("previous declaration `%D'", tmpl); |
2662 | cp_error ("used %d template parameter%s instead of %d", | |
7fe6899f MM |
2663 | TREE_VEC_LENGTH (tmpl_parms), |
2664 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
2665 | TREE_VEC_LENGTH (parms)); | |
2666 | return; | |
2667 | } | |
2668 | ||
2669 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
2670 | { | |
2671 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
2672 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
2673 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
2674 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
2675 | ||
2676 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
2677 | { | |
8251199e JM |
2678 | cp_error_at ("template parameter `%#D'", tmpl_parm); |
2679 | cp_error ("redeclared here as `%#D'", parm); | |
7fe6899f MM |
2680 | return; |
2681 | } | |
2682 | ||
2683 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
2684 | { | |
2685 | /* We have in [temp.param]: | |
2686 | ||
2687 | A template-parameter may not be given default arguments | |
2688 | by two different declarations in the same scope. */ | |
8251199e JM |
2689 | cp_error ("redefinition of default argument for `%#D'", parm); |
2690 | cp_error_at (" original definition appeared here", tmpl_parm); | |
7fe6899f MM |
2691 | return; |
2692 | } | |
2693 | ||
2694 | if (parm_default != NULL_TREE) | |
2695 | /* Update the previous template parameters (which are the ones | |
2696 | that will really count) with the new default value. */ | |
2697 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
2698 | else if (tmpl_default != NULL_TREE) |
2699 | /* Update the new parameters, too; they'll be used as the | |
2700 | parameters for any members. */ | |
2701 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f MM |
2702 | } |
2703 | } | |
75650646 | 2704 | |
75650646 MM |
2705 | /* Attempt to convert the non-type template parameter EXPR to the |
2706 | indicated TYPE. If the conversion is successful, return the | |
2707 | converted value. If the conversion is unsuccesful, return | |
2708 | NULL_TREE if we issued an error message, or error_mark_node if we | |
2709 | did not. We issue error messages for out-and-out bad template | |
2710 | parameters, but not simply because the conversion failed, since we | |
2711 | might be just trying to do argument deduction. By the time this | |
2712 | function is called, neither TYPE nor EXPR may make use of template | |
2713 | parameters. */ | |
2714 | ||
2715 | static tree | |
e1467ff2 | 2716 | convert_nontype_argument (type, expr) |
75650646 MM |
2717 | tree type; |
2718 | tree expr; | |
2719 | { | |
2720 | tree expr_type = TREE_TYPE (expr); | |
2721 | ||
2722 | /* A template-argument for a non-type, non-template | |
2723 | template-parameter shall be one of: | |
2724 | ||
2725 | --an integral constant-expression of integral or enumeration | |
2726 | type; or | |
2727 | ||
2728 | --the name of a non-type template-parameter; or | |
2729 | ||
2730 | --the name of an object or function with external linkage, | |
2731 | including function templates and function template-ids but | |
86052cc3 | 2732 | excluding non-static class members, expressed as id-expression; |
75650646 MM |
2733 | or |
2734 | ||
2735 | --the address of an object or function with external linkage, | |
2736 | including function templates and function template-ids but | |
2737 | excluding non-static class members, expressed as & id-expression | |
2738 | where the & is optional if the name refers to a function or | |
2739 | array; or | |
2740 | ||
2741 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
2742 | ||
638dd8fc MM |
2743 | /* An integral constant-expression can include const variables or |
2744 | enumerators. Simplify things by folding them to their values, | |
2745 | unless we're about to bind the declaration to a reference | |
2746 | parameter. */ | |
2747 | if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr) | |
2748 | && TREE_CODE (type) != REFERENCE_TYPE) | |
86052cc3 JM |
2749 | expr = decl_constant_value (expr); |
2750 | ||
7bf2682f MM |
2751 | if (is_overloaded_fn (expr)) |
2752 | /* OK for now. We'll check that it has external linkage later. | |
2753 | Check this first since if expr_type is the unknown_type_node | |
2754 | we would otherwise complain below. */ | |
2755 | ; | |
e08a8f45 MM |
2756 | else if (TYPE_PTRMEM_P (expr_type) |
2757 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2758 | { | |
2759 | if (TREE_CODE (expr) != PTRMEM_CST) | |
2760 | goto bad_argument; | |
2761 | } | |
f3400fe2 | 2762 | else if (TYPE_PTR_P (expr_type) |
e08a8f45 | 2763 | || TYPE_PTRMEM_P (expr_type) |
f3400fe2 JM |
2764 | || TREE_CODE (expr_type) == ARRAY_TYPE |
2765 | || TREE_CODE (type) == REFERENCE_TYPE | |
75650646 MM |
2766 | /* If expr is the address of an overloaded function, we |
2767 | will get the unknown_type_node at this point. */ | |
2768 | || expr_type == unknown_type_node) | |
2769 | { | |
2770 | tree referent; | |
49bf0d6f MM |
2771 | tree e = expr; |
2772 | STRIP_NOPS (e); | |
75650646 | 2773 | |
f3400fe2 JM |
2774 | if (TREE_CODE (type) == REFERENCE_TYPE |
2775 | || TREE_CODE (expr_type) == ARRAY_TYPE) | |
2776 | referent = e; | |
2777 | else | |
75650646 | 2778 | { |
f3400fe2 JM |
2779 | if (TREE_CODE (e) != ADDR_EXPR) |
2780 | { | |
2781 | bad_argument: | |
2782 | cp_error ("`%E' is not a valid template argument", expr); | |
e08a8f45 MM |
2783 | if (TYPE_PTR_P (expr_type)) |
2784 | { | |
2785 | if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) | |
2786 | cp_error ("it must be the address of a function with external linkage"); | |
2787 | else | |
2788 | cp_error ("it must be the address of an object with external linkage"); | |
2789 | } | |
2790 | else if (TYPE_PTRMEM_P (expr_type) | |
2791 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2792 | cp_error ("it must be a pointer-to-member of the form `&X::Y'"); | |
2793 | ||
f3400fe2 JM |
2794 | return NULL_TREE; |
2795 | } | |
2796 | ||
2797 | referent = TREE_OPERAND (e, 0); | |
2798 | STRIP_NOPS (referent); | |
75650646 MM |
2799 | } |
2800 | ||
75650646 MM |
2801 | if (TREE_CODE (referent) == STRING_CST) |
2802 | { | |
8251199e | 2803 | cp_error ("string literal %E is not a valid template argument", |
75650646 | 2804 | referent); |
8251199e | 2805 | error ("because it is the address of an object with static linkage"); |
75650646 MM |
2806 | return NULL_TREE; |
2807 | } | |
2808 | ||
2809 | if (is_overloaded_fn (referent)) | |
2810 | /* We'll check that it has external linkage later. */ | |
2811 | ; | |
2812 | else if (TREE_CODE (referent) != VAR_DECL) | |
2813 | goto bad_argument; | |
2814 | else if (!TREE_PUBLIC (referent)) | |
2815 | { | |
8251199e | 2816 | cp_error ("address of non-extern `%E' cannot be used as template argument", referent); |
75650646 MM |
2817 | return error_mark_node; |
2818 | } | |
2819 | } | |
f3400fe2 JM |
2820 | else if (INTEGRAL_TYPE_P (expr_type) |
2821 | || TYPE_PTRMEM_P (expr_type) | |
2822 | || TYPE_PTRMEMFUNC_P (expr_type) | |
2823 | /* The next two are g++ extensions. */ | |
2824 | || TREE_CODE (expr_type) == REAL_TYPE | |
2825 | || TREE_CODE (expr_type) == COMPLEX_TYPE) | |
75650646 | 2826 | { |
f3400fe2 JM |
2827 | if (! TREE_CONSTANT (expr)) |
2828 | { | |
2829 | non_constant: | |
2830 | cp_error ("non-constant `%E' cannot be used as template argument", | |
2831 | expr); | |
2832 | return NULL_TREE; | |
2833 | } | |
75650646 | 2834 | } |
75650646 MM |
2835 | else |
2836 | { | |
8251199e | 2837 | cp_error ("object `%E' cannot be used as template argument", expr); |
75650646 MM |
2838 | return NULL_TREE; |
2839 | } | |
2840 | ||
2841 | switch (TREE_CODE (type)) | |
2842 | { | |
2843 | case INTEGER_TYPE: | |
2844 | case BOOLEAN_TYPE: | |
2845 | case ENUMERAL_TYPE: | |
2846 | /* For a non-type template-parameter of integral or enumeration | |
2847 | type, integral promotions (_conv.prom_) and integral | |
2848 | conversions (_conv.integral_) are applied. */ | |
2849 | if (!INTEGRAL_TYPE_P (expr_type)) | |
2850 | return error_mark_node; | |
2851 | ||
2852 | /* It's safe to call digest_init in this case; we know we're | |
2853 | just converting one integral constant expression to another. */ | |
db02b6b9 | 2854 | expr = digest_init (type, expr, (tree*) 0); |
75650646 | 2855 | |
db02b6b9 | 2856 | if (TREE_CODE (expr) != INTEGER_CST) |
f3400fe2 | 2857 | /* Curiously, some TREE_CONSTANT integral expressions do not |
db02b6b9 MM |
2858 | simplify to integer constants. For example, `3 % 0', |
2859 | remains a TRUNC_MOD_EXPR. */ | |
2860 | goto non_constant; | |
2861 | ||
2862 | return expr; | |
2863 | ||
abff8e06 JM |
2864 | case REAL_TYPE: |
2865 | case COMPLEX_TYPE: | |
2866 | /* These are g++ extensions. */ | |
2867 | if (TREE_CODE (expr_type) != TREE_CODE (type)) | |
2868 | return error_mark_node; | |
2869 | ||
db02b6b9 MM |
2870 | expr = digest_init (type, expr, (tree*) 0); |
2871 | ||
2872 | if (TREE_CODE (expr) != REAL_CST) | |
2873 | goto non_constant; | |
2874 | ||
2875 | return expr; | |
abff8e06 | 2876 | |
75650646 MM |
2877 | case POINTER_TYPE: |
2878 | { | |
2879 | tree type_pointed_to = TREE_TYPE (type); | |
2880 | ||
2881 | if (TYPE_PTRMEM_P (type)) | |
93aa462d MM |
2882 | { |
2883 | tree e; | |
2884 | ||
2885 | /* For a non-type template-parameter of type pointer to data | |
2886 | member, qualification conversions (_conv.qual_) are | |
2887 | applied. */ | |
2888 | e = perform_qualification_conversions (type, expr); | |
2889 | if (TREE_CODE (e) == NOP_EXPR) | |
87533b37 MM |
2890 | /* The call to perform_qualification_conversions will |
2891 | insert a NOP_EXPR over EXPR to do express conversion, | |
2892 | if necessary. But, that will confuse us if we use | |
2893 | this (converted) template parameter to instantiate | |
2894 | another template; then the thing will not look like a | |
2895 | valid template argument. So, just make a new | |
2896 | constant, of the appropriate type. */ | |
2897 | e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); | |
93aa462d MM |
2898 | return e; |
2899 | } | |
75650646 MM |
2900 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) |
2901 | { | |
2902 | /* For a non-type template-parameter of type pointer to | |
2903 | function, only the function-to-pointer conversion | |
2904 | (_conv.func_) is applied. If the template-argument | |
2905 | represents a set of overloaded functions (or a pointer to | |
2906 | such), the matching function is selected from the set | |
2907 | (_over.over_). */ | |
2908 | tree fns; | |
2909 | tree fn; | |
2910 | ||
7bf2682f | 2911 | if (TREE_CODE (expr) == ADDR_EXPR) |
75650646 MM |
2912 | fns = TREE_OPERAND (expr, 0); |
2913 | else | |
2914 | fns = expr; | |
2915 | ||
a05273d4 | 2916 | fn = instantiate_type (type_pointed_to, fns, 0); |
75650646 MM |
2917 | |
2918 | if (fn == error_mark_node) | |
2919 | return error_mark_node; | |
2920 | ||
2921 | if (!TREE_PUBLIC (fn)) | |
2922 | { | |
2923 | if (really_overloaded_fn (fns)) | |
2924 | return error_mark_node; | |
2925 | else | |
2926 | goto bad_argument; | |
2927 | } | |
2928 | ||
2929 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2930 | ||
3bfdc719 | 2931 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2932 | 0); |
2933 | return expr; | |
2934 | } | |
2935 | else | |
2936 | { | |
2937 | /* For a non-type template-parameter of type pointer to | |
2938 | object, qualification conversions (_conv.qual_) and the | |
2939 | array-to-pointer conversion (_conv.array_) are applied. | |
2940 | [Note: In particular, neither the null pointer conversion | |
2941 | (_conv.ptr_) nor the derived-to-base conversion | |
2942 | (_conv.ptr_) are applied. Although 0 is a valid | |
2943 | template-argument for a non-type template-parameter of | |
2944 | integral type, it is not a valid template-argument for a | |
e1467ff2 MM |
2945 | non-type template-parameter of pointer type.] |
2946 | ||
2947 | The call to decay_conversion performs the | |
2948 | array-to-pointer conversion, if appropriate. */ | |
2949 | expr = decay_conversion (expr); | |
75650646 MM |
2950 | |
2951 | if (expr == error_mark_node) | |
2952 | return error_mark_node; | |
2953 | else | |
2954 | return perform_qualification_conversions (type, expr); | |
2955 | } | |
2956 | } | |
2957 | break; | |
2958 | ||
2959 | case REFERENCE_TYPE: | |
2960 | { | |
2961 | tree type_referred_to = TREE_TYPE (type); | |
2962 | ||
2963 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
2964 | { | |
2965 | /* For a non-type template-parameter of type reference to | |
39c01e4c MM |
2966 | function, no conversions apply. If the |
2967 | template-argument represents a set of overloaded | |
2968 | functions, the matching function is selected from the | |
2969 | set (_over.over_). */ | |
75650646 MM |
2970 | tree fns = expr; |
2971 | tree fn; | |
2972 | ||
a05273d4 | 2973 | fn = instantiate_type (type_referred_to, fns, 0); |
e6e174e5 JM |
2974 | |
2975 | if (fn == error_mark_node) | |
2976 | return error_mark_node; | |
75650646 MM |
2977 | |
2978 | if (!TREE_PUBLIC (fn)) | |
2979 | { | |
2980 | if (really_overloaded_fn (fns)) | |
2981 | /* Don't issue an error here; we might get a different | |
2982 | function if the overloading had worked out | |
2983 | differently. */ | |
2984 | return error_mark_node; | |
2985 | else | |
2986 | goto bad_argument; | |
2987 | } | |
2988 | ||
3bfdc719 MM |
2989 | my_friendly_assert (same_type_p (type_referred_to, |
2990 | TREE_TYPE (fn)), | |
75650646 MM |
2991 | 0); |
2992 | ||
2993 | return fn; | |
2994 | } | |
2995 | else | |
2996 | { | |
2997 | /* For a non-type template-parameter of type reference to | |
2998 | object, no conversions apply. The type referred to by the | |
2999 | reference may be more cv-qualified than the (otherwise | |
3000 | identical) type of the template-argument. The | |
3001 | template-parameter is bound directly to the | |
3002 | template-argument, which must be an lvalue. */ | |
ff350acd JM |
3003 | if ((TYPE_MAIN_VARIANT (expr_type) |
3004 | != TYPE_MAIN_VARIANT (type_referred_to)) | |
91063b51 MM |
3005 | || !at_least_as_qualified_p (type_referred_to, |
3006 | expr_type) | |
75650646 MM |
3007 | || !real_lvalue_p (expr)) |
3008 | return error_mark_node; | |
3009 | else | |
3010 | return expr; | |
3011 | } | |
3012 | } | |
3013 | break; | |
3014 | ||
3015 | case RECORD_TYPE: | |
3016 | { | |
410c2fba KL |
3017 | if (!TYPE_PTRMEMFUNC_P (type)) |
3018 | /* This handles templates like | |
3019 | template<class T, T t> void f(); | |
3020 | when T is substituted with any class. The second template | |
3021 | parameter becomes invalid and the template candidate is | |
3022 | rejected. */ | |
3023 | return error_mark_node; | |
75650646 MM |
3024 | |
3025 | /* For a non-type template-parameter of type pointer to member | |
3026 | function, no conversions apply. If the template-argument | |
3027 | represents a set of overloaded member functions, the | |
3028 | matching member function is selected from the set | |
3029 | (_over.over_). */ | |
3030 | ||
3031 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
3032 | expr_type != unknown_type_node) | |
3033 | return error_mark_node; | |
3034 | ||
e08a8f45 | 3035 | if (TREE_CODE (expr) == PTRMEM_CST) |
75650646 MM |
3036 | { |
3037 | /* A ptr-to-member constant. */ | |
3bfdc719 | 3038 | if (!same_type_p (type, expr_type)) |
75650646 MM |
3039 | return error_mark_node; |
3040 | else | |
3041 | return expr; | |
3042 | } | |
3043 | ||
3044 | if (TREE_CODE (expr) != ADDR_EXPR) | |
3045 | return error_mark_node; | |
3046 | ||
d8f8dca1 | 3047 | expr = instantiate_type (type, expr, 0); |
75650646 | 3048 | |
d8f8dca1 | 3049 | if (expr == error_mark_node) |
75650646 MM |
3050 | return error_mark_node; |
3051 | ||
3bfdc719 | 3052 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
3053 | 0); |
3054 | return expr; | |
3055 | } | |
3056 | break; | |
3057 | ||
3058 | default: | |
3059 | /* All non-type parameters must have one of these types. */ | |
3060 | my_friendly_abort (0); | |
3061 | break; | |
3062 | } | |
3063 | ||
3064 | return error_mark_node; | |
3065 | } | |
3066 | ||
744fac59 KL |
3067 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
3068 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
3069 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
3070 | or PARM_DECL. | |
3071 | ||
3072 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
3073 | the case, then extra parameters must have default arguments. | |
3074 | ||
3075 | Consider the example: | |
3076 | template <class T, class Allocator = allocator> class vector; | |
3077 | template<template <class U> class TT> class C; | |
3078 | ||
3079 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
3080 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
3081 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
3082 | substitute the TT parameter. */ | |
3083 | ||
3084 | static int | |
4393e105 MM |
3085 | coerce_template_template_parms (parm_parms, arg_parms, complain, |
3086 | in_decl, outer_args) | |
3087 | tree parm_parms, arg_parms; | |
3088 | int complain; | |
3089 | tree in_decl, outer_args; | |
744fac59 KL |
3090 | { |
3091 | int nparms, nargs, i; | |
3092 | tree parm, arg; | |
3093 | ||
3094 | my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); | |
3095 | my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); | |
3096 | ||
3097 | nparms = TREE_VEC_LENGTH (parm_parms); | |
3098 | nargs = TREE_VEC_LENGTH (arg_parms); | |
3099 | ||
3100 | /* The rule here is opposite of coerce_template_parms. */ | |
3101 | if (nargs < nparms | |
3102 | || (nargs > nparms | |
3103 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
3104 | return 0; | |
3105 | ||
3106 | for (i = 0; i < nparms; ++i) | |
3107 | { | |
3108 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
3109 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
3110 | ||
3111 | if (arg == NULL_TREE || arg == error_mark_node | |
3112 | || parm == NULL_TREE || parm == error_mark_node) | |
3113 | return 0; | |
3114 | ||
3115 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
3116 | return 0; | |
3117 | ||
3118 | switch (TREE_CODE (parm)) | |
3119 | { | |
3120 | case TYPE_DECL: | |
3121 | break; | |
3122 | ||
3123 | case TEMPLATE_DECL: | |
3124 | /* We encounter instantiations of templates like | |
3125 | template <template <template <class> class> class TT> | |
3126 | class C; */ | |
700466c2 JM |
3127 | { |
3128 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3129 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3130 | ||
3131 | if (!coerce_template_template_parms (parmparm, argparm, | |
4393e105 MM |
3132 | complain, in_decl, |
3133 | outer_args)) | |
700466c2 JM |
3134 | return 0; |
3135 | } | |
3136 | break; | |
744fac59 KL |
3137 | |
3138 | case PARM_DECL: | |
3139 | /* The tsubst call is used to handle cases such as | |
3140 | template <class T, template <T> class TT> class D; | |
3141 | i.e. the parameter list of TT depends on earlier parameters. */ | |
4393e105 MM |
3142 | if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, |
3143 | complain, in_decl), | |
3bfdc719 | 3144 | TREE_TYPE (arg))) |
744fac59 KL |
3145 | return 0; |
3146 | break; | |
3147 | ||
3148 | default: | |
3149 | my_friendly_abort (0); | |
3150 | } | |
3151 | } | |
3152 | return 1; | |
3153 | } | |
3154 | ||
8b5b8b7c MM |
3155 | /* Convert the indicated template ARG as necessary to match the |
3156 | indicated template PARM. Returns the converted ARG, or | |
3157 | error_mark_node if the conversion was unsuccessful. Error messages | |
3158 | are issued if COMPLAIN is non-zero. This conversion is for the Ith | |
3159 | parameter in the parameter list. ARGS is the full set of template | |
3160 | arguments deduced so far. */ | |
3161 | ||
3162 | static tree | |
3163 | convert_template_argument (parm, arg, args, complain, i, in_decl) | |
3164 | tree parm; | |
3165 | tree arg; | |
3166 | tree args; | |
3167 | int complain; | |
3168 | int i; | |
3169 | tree in_decl; | |
3170 | { | |
3171 | tree val; | |
3172 | tree inner_args; | |
3173 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
3174 | ||
3175 | inner_args = innermost_args (args); | |
3176 | ||
3177 | if (TREE_CODE (arg) == TREE_LIST | |
3178 | && TREE_TYPE (arg) != NULL_TREE | |
3179 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
3180 | { | |
3181 | /* The template argument was the name of some | |
3182 | member function. That's usually | |
3183 | illegal, but static members are OK. In any | |
3184 | case, grab the underlying fields/functions | |
3185 | and issue an error later if required. */ | |
3186 | arg = TREE_VALUE (arg); | |
3187 | TREE_TYPE (arg) = unknown_type_node; | |
3188 | } | |
3189 | ||
3190 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3191 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3192 | || requires_tmpl_type); | |
3193 | ||
3194 | /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, | |
3195 | we also accept implicitly created TYPE_DECL as a valid argument. | |
3196 | This is necessary to handle the case where we pass a template name | |
3197 | to a template template parameter in a scope where we've derived from | |
3198 | in instantiation of that template, so the template name refers to that | |
3199 | instantiation. We really ought to handle this better. */ | |
3200 | is_tmpl_type | |
3201 | = ((TREE_CODE (arg) == TEMPLATE_DECL | |
3202 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3203 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 3204 | && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg)) |
8b5b8b7c MM |
3205 | || (TREE_CODE (arg) == RECORD_TYPE |
3206 | && CLASSTYPE_TEMPLATE_INFO (arg) | |
3207 | && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL | |
3208 | && DECL_ARTIFICIAL (TYPE_NAME (arg)) | |
3209 | && requires_tmpl_type | |
b54ccf71 | 3210 | && is_base_of_enclosing_class (arg, current_class_type))); |
8b5b8b7c MM |
3211 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
3212 | arg = TYPE_STUB_DECL (arg); | |
3213 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) | |
3214 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
3215 | ||
3216 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type; | |
3217 | ||
3218 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3219 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3220 | { | |
8251199e JM |
3221 | cp_pedwarn ("to refer to a type member of a template parameter,"); |
3222 | cp_pedwarn (" use `typename %E'", arg); | |
8b5b8b7c MM |
3223 | |
3224 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3225 | TREE_OPERAND (arg, 1)); | |
3226 | is_type = 1; | |
3227 | } | |
3228 | if (is_type != requires_type) | |
3229 | { | |
3230 | if (in_decl) | |
3231 | { | |
3232 | if (complain) | |
3233 | { | |
8251199e | 3234 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3235 | i + 1, in_decl); |
3236 | if (is_type) | |
8251199e | 3237 | cp_error (" expected a constant of type `%T', got `%T'", |
8b5b8b7c MM |
3238 | TREE_TYPE (parm), |
3239 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
3240 | else | |
8251199e | 3241 | cp_error (" expected a type, got `%E'", arg); |
8b5b8b7c MM |
3242 | } |
3243 | } | |
3244 | return error_mark_node; | |
3245 | } | |
3246 | if (is_tmpl_type ^ requires_tmpl_type) | |
3247 | { | |
3248 | if (in_decl && complain) | |
3249 | { | |
8251199e | 3250 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3251 | i + 1, in_decl); |
3252 | if (is_tmpl_type) | |
8251199e | 3253 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); |
8b5b8b7c | 3254 | else |
8251199e | 3255 | cp_error (" expected a class template, got `%T'", arg); |
8b5b8b7c MM |
3256 | } |
3257 | return error_mark_node; | |
3258 | } | |
3259 | ||
3260 | if (is_type) | |
3261 | { | |
3262 | if (requires_tmpl_type) | |
3263 | { | |
3264 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3265 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3266 | ||
4393e105 | 3267 | if (coerce_template_template_parms (parmparm, argparm, complain, |
8b5b8b7c MM |
3268 | in_decl, inner_args)) |
3269 | { | |
3270 | val = arg; | |
3271 | ||
3272 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3273 | TEMPLATE_DECL. */ | |
3274 | if (val != error_mark_node | |
3275 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3276 | val = TREE_TYPE (val); | |
3277 | } | |
3278 | else | |
3279 | { | |
3280 | if (in_decl && complain) | |
3281 | { | |
8251199e | 3282 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c | 3283 | i + 1, in_decl); |
8251199e | 3284 | cp_error (" expected a template of type `%D', got `%D'", parm, arg); |
8b5b8b7c MM |
3285 | } |
3286 | ||
3287 | val = error_mark_node; | |
3288 | } | |
3289 | } | |
3290 | else | |
3291 | { | |
3292 | val = groktypename (arg); | |
3293 | if (! processing_template_decl) | |
3294 | { | |
3295 | /* [basic.link]: A name with no linkage (notably, the | |
3296 | name of a class or enumeration declared in a local | |
3297 | scope) shall not be used to declare an entity with | |
3298 | linkage. This implies that names with no linkage | |
3299 | cannot be used as template arguments. */ | |
3300 | tree t = no_linkage_check (val); | |
3301 | if (t) | |
3302 | { | |
3303 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
8251199e JM |
3304 | cp_pedwarn |
3305 | ("template-argument `%T' uses anonymous type", val); | |
8b5b8b7c | 3306 | else |
8251199e JM |
3307 | cp_error |
3308 | ("template-argument `%T' uses local type `%T'", | |
8b5b8b7c MM |
3309 | val, t); |
3310 | return error_mark_node; | |
3311 | } | |
3312 | } | |
3313 | } | |
3314 | } | |
3315 | else | |
3316 | { | |
4393e105 | 3317 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c MM |
3318 | |
3319 | if (processing_template_decl) | |
3320 | arg = maybe_fold_nontype_arg (arg); | |
3321 | ||
3322 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
3323 | /* We used to call digest_init here. However, digest_init | |
3324 | will report errors, which we don't want when complain | |
3325 | is zero. More importantly, digest_init will try too | |
3326 | hard to convert things: for example, `0' should not be | |
3327 | converted to pointer type at this point according to | |
3328 | the standard. Accepting this is not merely an | |
3329 | extension, since deciding whether or not these | |
3330 | conversions can occur is part of determining which | |
3331 | function template to call, or whether a given epxlicit | |
3332 | argument specification is legal. */ | |
3333 | val = convert_nontype_argument (t, arg); | |
3334 | else | |
3335 | val = arg; | |
3336 | ||
3337 | if (val == NULL_TREE) | |
3338 | val = error_mark_node; | |
3339 | else if (val == error_mark_node && complain) | |
8251199e | 3340 | cp_error ("could not convert template argument `%E' to `%T'", |
8b5b8b7c MM |
3341 | arg, t); |
3342 | } | |
3343 | ||
3344 | return val; | |
3345 | } | |
3346 | ||
3347 | /* Convert all template arguments to their appropriate types, and | |
3348 | return a vector containing the innermost resulting template | |
3349 | arguments. If any error occurs, return error_mark_node, and, if | |
3350 | COMPLAIN is non-zero, issue an error message. Some error messages | |
3351 | are issued even if COMPLAIN is zero; for instance, if a template | |
3352 | argument is composed from a local class. | |
75650646 MM |
3353 | |
3354 | If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be | |
3355 | provided in ARGLIST, or else trailing parameters must have default | |
3356 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
cabc336a MM |
3357 | deduction for any unspecified trailing arguments. |
3358 | ||
3359 | The resulting TREE_VEC is allocated on a temporary obstack, and | |
3360 | must be explicitly copied if it will be permanent. */ | |
de575009 | 3361 | |
8d08fdba | 3362 | static tree |
8b5b8b7c | 3363 | coerce_template_parms (parms, args, in_decl, |
75650646 | 3364 | complain, |
744fac59 | 3365 | require_all_arguments) |
8b5b8b7c | 3366 | tree parms, args; |
8d08fdba | 3367 | tree in_decl; |
75650646 MM |
3368 | int complain; |
3369 | int require_all_arguments; | |
8d08fdba | 3370 | { |
a292b002 | 3371 | int nparms, nargs, i, lost = 0; |
e4a84209 | 3372 | tree inner_args; |
8b5b8b7c MM |
3373 | tree new_args; |
3374 | tree new_inner_args; | |
a292b002 | 3375 | |
8b5b8b7c | 3376 | inner_args = innermost_args (args); |
e4a84209 | 3377 | nargs = NUM_TMPL_ARGS (inner_args); |
a292b002 MS |
3378 | nparms = TREE_VEC_LENGTH (parms); |
3379 | ||
3380 | if (nargs > nparms | |
3381 | || (nargs < nparms | |
75650646 | 3382 | && require_all_arguments |
a292b002 | 3383 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3384 | { |
75650646 MM |
3385 | if (complain) |
3386 | { | |
8251199e | 3387 | cp_error ("wrong number of template arguments (%d, should be %d)", |
e4a84209 | 3388 | nargs, nparms); |
75650646 MM |
3389 | |
3390 | if (in_decl) | |
8251199e | 3391 | cp_error_at ("provided for `%D'", in_decl); |
75650646 MM |
3392 | } |
3393 | ||
8d08fdba MS |
3394 | return error_mark_node; |
3395 | } | |
3396 | ||
cabc336a | 3397 | new_inner_args = make_temp_vec (nparms); |
8b5b8b7c MM |
3398 | new_args = add_outermost_template_args (args, new_inner_args); |
3399 | for (i = 0; i < nparms; i++) | |
8d08fdba | 3400 | { |
8b5b8b7c MM |
3401 | tree arg; |
3402 | tree parm; | |
e4a84209 | 3403 | |
8b5b8b7c MM |
3404 | /* Get the Ith template parameter. */ |
3405 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 3406 | |
8b5b8b7c MM |
3407 | /* Calculate the Ith argument. */ |
3408 | if (inner_args && TREE_CODE (inner_args) == TREE_LIST) | |
8d08fdba | 3409 | { |
8b5b8b7c MM |
3410 | arg = TREE_VALUE (inner_args); |
3411 | inner_args = TREE_CHAIN (inner_args); | |
8d08fdba | 3412 | } |
8b5b8b7c MM |
3413 | else if (i < nargs) |
3414 | arg = TREE_VEC_ELT (inner_args, i); | |
3415 | /* If no template argument was supplied, look for a default | |
3416 | value. */ | |
3417 | else if (TREE_PURPOSE (parm) == NULL_TREE) | |
3418 | { | |
3419 | /* There was no default value. */ | |
3420 | my_friendly_assert (!require_all_arguments, 0); | |
3421 | break; | |
3422 | } | |
3423 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
4393e105 | 3424 | arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); |
8b5b8b7c | 3425 | else |
4393e105 MM |
3426 | arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, |
3427 | in_decl); | |
e4a84209 | 3428 | |
8b5b8b7c | 3429 | /* Now, convert the Ith argument, as necessary. */ |
75650646 MM |
3430 | if (arg == NULL_TREE) |
3431 | /* We're out of arguments. */ | |
3432 | { | |
3433 | my_friendly_assert (!require_all_arguments, 0); | |
3434 | break; | |
3435 | } | |
8b5b8b7c | 3436 | else if (arg == error_mark_node) |
75650646 | 3437 | { |
8251199e | 3438 | cp_error ("template argument %d is invalid", i + 1); |
8b5b8b7c | 3439 | arg = error_mark_node; |
8d08fdba | 3440 | } |
8b5b8b7c MM |
3441 | else |
3442 | arg = convert_template_argument (TREE_VALUE (parm), | |
3443 | arg, new_args, complain, i, | |
3444 | in_decl); | |
3445 | ||
3446 | if (arg == error_mark_node) | |
8d08fdba | 3447 | lost++; |
8b5b8b7c | 3448 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 3449 | } |
8b5b8b7c | 3450 | |
8d08fdba MS |
3451 | if (lost) |
3452 | return error_mark_node; | |
8b5b8b7c MM |
3453 | |
3454 | return new_inner_args; | |
8d08fdba MS |
3455 | } |
3456 | ||
34016c81 JM |
3457 | /* Returns 1 if template args OT and NT are equivalent. */ |
3458 | ||
d8e178a0 | 3459 | static int |
34016c81 JM |
3460 | template_args_equal (ot, nt) |
3461 | tree ot, nt; | |
3462 | { | |
3463 | if (nt == ot) | |
3464 | return 1; | |
3465 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
3466 | return 0; | |
3467 | if (TREE_CODE (nt) == TREE_VEC) | |
3468 | /* For member templates */ | |
3469 | return comp_template_args (ot, nt); | |
3470 | else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') | |
3bfdc719 | 3471 | return same_type_p (ot, nt); |
34016c81 JM |
3472 | else |
3473 | return (cp_tree_equal (ot, nt) > 0); | |
3474 | } | |
3475 | ||
3476 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
3477 | of template arguments. Returns 0 otherwise. */ |
3478 | ||
6757edfe | 3479 | int |
5566b478 MS |
3480 | comp_template_args (oldargs, newargs) |
3481 | tree oldargs, newargs; | |
3482 | { | |
3483 | int i; | |
3484 | ||
386b8a85 JM |
3485 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
3486 | return 0; | |
3487 | ||
5566b478 MS |
3488 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
3489 | { | |
3490 | tree nt = TREE_VEC_ELT (newargs, i); | |
3491 | tree ot = TREE_VEC_ELT (oldargs, i); | |
3492 | ||
34016c81 | 3493 | if (! template_args_equal (ot, nt)) |
61a127b3 | 3494 | return 0; |
5566b478 MS |
3495 | } |
3496 | return 1; | |
3497 | } | |
3498 | ||
8d08fdba MS |
3499 | /* Given class template name and parameter list, produce a user-friendly name |
3500 | for the instantiation. */ | |
e92cc029 | 3501 | |
8d08fdba | 3502 | static char * |
36a117a5 | 3503 | mangle_class_name_for_template (name, parms, arglist) |
8d08fdba MS |
3504 | char *name; |
3505 | tree parms, arglist; | |
3506 | { | |
3507 | static struct obstack scratch_obstack; | |
3508 | static char *scratch_firstobj; | |
3509 | int i, nparms; | |
8d08fdba MS |
3510 | |
3511 | if (!scratch_firstobj) | |
fc378698 | 3512 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
3513 | else |
3514 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 3515 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 3516 | |
8d08fdba | 3517 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); |
8d08fdba | 3518 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) |
8d08fdba MS |
3519 | |
3520 | cat (name); | |
3521 | ccat ('<'); | |
3522 | nparms = TREE_VEC_LENGTH (parms); | |
36a117a5 | 3523 | arglist = innermost_args (arglist); |
8d08fdba MS |
3524 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); |
3525 | for (i = 0; i < nparms; i++) | |
3526 | { | |
a292b002 MS |
3527 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
3528 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
3529 | |
3530 | if (i) | |
3531 | ccat (','); | |
3532 | ||
a292b002 | 3533 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 3534 | { |
294471c3 | 3535 | cat (type_as_string_real (arg, 0, 1)); |
8d08fdba MS |
3536 | continue; |
3537 | } | |
73b0fce8 KL |
3538 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
3539 | { | |
3540 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
3541 | { |
3542 | /* Already substituted with real template. Just output | |
3543 | the template name here */ | |
cb0dbb9a | 3544 | tree context = DECL_CONTEXT (arg); |
ec4f972f AS |
3545 | if (context) |
3546 | { | |
3547 | /* The template may be defined in a namespace, or | |
3548 | may be a member template. */ | |
3549 | my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL | |
3550 | || CLASS_TYPE_P (context), | |
3551 | 980422); | |
2c73f9f5 ML |
3552 | cat(decl_as_string (DECL_CONTEXT (arg), 0)); |
3553 | cat("::"); | |
3554 | } | |
3555 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3556 | } | |
73b0fce8 KL |
3557 | else |
3558 | /* Output the parameter declaration */ | |
294471c3 | 3559 | cat (type_as_string_real (arg, 0, 1)); |
73b0fce8 KL |
3560 | continue; |
3561 | } | |
8d08fdba MS |
3562 | else |
3563 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
3564 | ||
3565 | if (TREE_CODE (arg) == TREE_LIST) | |
3566 | { | |
3567 | /* New list cell was built because old chain link was in | |
3568 | use. */ | |
3569 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
3570 | arg = TREE_VALUE (arg); | |
3571 | } | |
3572 | /* No need to check arglist against parmlist here; we did that | |
3573 | in coerce_template_parms, called from lookup_template_class. */ | |
3574 | cat (expr_as_string (arg, 0)); | |
3575 | } | |
3576 | { | |
3577 | char *bufp = obstack_next_free (&scratch_obstack); | |
3578 | int offset = 0; | |
3579 | while (bufp[offset - 1] == ' ') | |
3580 | offset--; | |
3581 | obstack_blank_fast (&scratch_obstack, offset); | |
3582 | ||
3583 | /* B<C<char> >, not B<C<char>> */ | |
3584 | if (bufp[offset - 1] == '>') | |
3585 | ccat (' '); | |
3586 | } | |
3587 | ccat ('>'); | |
3588 | ccat ('\0'); | |
3589 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
3590 | } |
3591 | ||
bd6dd845 | 3592 | static tree |
5566b478 MS |
3593 | classtype_mangled_name (t) |
3594 | tree t; | |
3595 | { | |
3596 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
3597 | /* Specializations have already had their names set up in |
3598 | lookup_template_class. */ | |
9fbf56f7 MM |
3599 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
3600 | { | |
3601 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
3602 | ||
36a117a5 MM |
3603 | /* For non-primary templates, the template parameters are |
3604 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
3605 | if (PRIMARY_TEMPLATE_P (tmpl)) |
3606 | { | |
3607 | tree name = DECL_NAME (tmpl); | |
3608 | char *mangled_name = mangle_class_name_for_template | |
3609 | (IDENTIFIER_POINTER (name), | |
3610 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
3611 | CLASSTYPE_TI_ARGS (t)); | |
3612 | tree id = get_identifier (mangled_name); | |
3613 | IDENTIFIER_TEMPLATE (id) = name; | |
3614 | return id; | |
3615 | } | |
5566b478 | 3616 | } |
9fbf56f7 MM |
3617 | |
3618 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
3619 | } |
3620 | ||
3621 | static void | |
3622 | add_pending_template (d) | |
3623 | tree d; | |
3624 | { | |
e92cc029 MS |
3625 | tree ti; |
3626 | ||
3627 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
3628 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
3629 | else | |
3630 | ti = DECL_TEMPLATE_INFO (d); | |
3631 | ||
824b9a4c | 3632 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
3633 | return; |
3634 | ||
e1b3e07d | 3635 | *template_tail = tree_cons (build_srcloc_here (), d, NULL_TREE); |
5566b478 | 3636 | template_tail = &TREE_CHAIN (*template_tail); |
824b9a4c | 3637 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
3638 | } |
3639 | ||
386b8a85 JM |
3640 | |
3641 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
3642 | may be either a _DECL or an overloaded function or an | |
3643 | IDENTIFIER_NODE), and ARGLIST. */ | |
3644 | ||
3645 | tree | |
3646 | lookup_template_function (fns, arglist) | |
3647 | tree fns, arglist; | |
3648 | { | |
2c73f9f5 | 3649 | tree type; |
050367a3 | 3650 | |
386b8a85 JM |
3651 | if (fns == NULL_TREE) |
3652 | { | |
8251199e | 3653 | cp_error ("non-template used as template"); |
386b8a85 JM |
3654 | return error_mark_node; |
3655 | } | |
3656 | ||
2c73f9f5 ML |
3657 | type = TREE_TYPE (fns); |
3658 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
3659 | type = unknown_type_node; | |
3660 | ||
672476cb MM |
3661 | if (processing_template_decl) |
3662 | return build_min (TEMPLATE_ID_EXPR, type, fns, arglist); | |
3663 | else | |
3664 | return build (TEMPLATE_ID_EXPR, type, fns, arglist); | |
386b8a85 JM |
3665 | } |
3666 | ||
a2b60a0e MM |
3667 | /* Within the scope of a template class S<T>, the name S gets bound |
3668 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
3669 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
3670 | or one of its enclosing classes, and that type is a template, | |
3671 | return the associated TEMPLATE_DECL. Otherwise, the original | |
3672 | DECL is returned. */ | |
3673 | ||
e9659ab0 | 3674 | static tree |
a2b60a0e MM |
3675 | maybe_get_template_decl_from_type_decl (decl) |
3676 | tree decl; | |
3677 | { | |
3678 | return (decl != NULL_TREE | |
3679 | && TREE_CODE (decl) == TYPE_DECL | |
3680 | && DECL_ARTIFICIAL (decl) | |
511b60ff | 3681 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
a2b60a0e MM |
3682 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
3683 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
3684 | } | |
386b8a85 | 3685 | |
8d08fdba MS |
3686 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
3687 | parameters, find the desired type. | |
3688 | ||
3689 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
36a117a5 MM |
3690 | (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will |
3691 | be a TREE_LIST if called directly from the parser, and a TREE_VEC | |
3692 | otherwise.) Since ARGLIST is build on the decl_obstack, we must | |
3693 | copy it here to keep it from being reclaimed when the decl storage | |
3694 | is reclaimed. | |
8d08fdba MS |
3695 | |
3696 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
3697 | instantiate. |
3698 | ||
36a117a5 MM |
3699 | If ENTERING_SCOPE is non-zero, we are about to enter the scope of |
3700 | the class we are looking up. | |
3701 | ||
75650646 MM |
3702 | If the template class is really a local class in a template |
3703 | function, then the FUNCTION_CONTEXT is the function in which it is | |
3704 | being instantiated. */ | |
e92cc029 | 3705 | |
8d08fdba | 3706 | tree |
36a117a5 | 3707 | lookup_template_class (d1, arglist, in_decl, context, entering_scope) |
8d08fdba MS |
3708 | tree d1, arglist; |
3709 | tree in_decl; | |
e1467ff2 | 3710 | tree context; |
36a117a5 | 3711 | int entering_scope; |
8d08fdba | 3712 | { |
a703fb38 | 3713 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 3714 | tree t; |
5566b478 MS |
3715 | |
3716 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
3717 | { | |
f181d4ae MM |
3718 | if (IDENTIFIER_VALUE (d1) |
3719 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) | |
3720 | template = IDENTIFIER_VALUE (d1); | |
73b0fce8 KL |
3721 | else |
3722 | { | |
2c73f9f5 ML |
3723 | if (context) |
3724 | push_decl_namespace (context); | |
3ebc5c52 MM |
3725 | template = lookup_name (d1, /*prefer_type=*/0); |
3726 | template = maybe_get_template_decl_from_type_decl (template); | |
2c73f9f5 ML |
3727 | if (context) |
3728 | pop_decl_namespace (); | |
73b0fce8 | 3729 | } |
8d019cef JM |
3730 | if (template) |
3731 | context = DECL_CONTEXT (template); | |
5566b478 | 3732 | } |
c91a56d2 MS |
3733 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
3734 | { | |
802dbc34 JM |
3735 | tree type = TREE_TYPE (d1); |
3736 | ||
3737 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
3738 | an implicit typename for the second A. Deal with it. */ | |
3739 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
3740 | type = TREE_TYPE (type); | |
3741 | ||
3742 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
f3400fe2 | 3743 | { |
802dbc34 | 3744 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
3745 | d1 = DECL_NAME (template); |
3746 | } | |
c91a56d2 | 3747 | } |
ed44da02 MM |
3748 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
3749 | || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' | |
3750 | && IS_AGGR_TYPE (d1))) | |
5566b478 | 3751 | { |
ed44da02 | 3752 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
3753 | d1 = DECL_NAME (template); |
3754 | } | |
93cdc044 JM |
3755 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
3756 | && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL) | |
3757 | { | |
3758 | template = d1; | |
3759 | d1 = DECL_NAME (template); | |
3760 | context = DECL_CONTEXT (template); | |
3761 | } | |
5566b478 MS |
3762 | else |
3763 | my_friendly_abort (272); | |
8d08fdba | 3764 | |
8d08fdba | 3765 | /* With something like `template <class T> class X class X { ... };' |
f181d4ae MM |
3766 | we could end up with D1 having nothing but an IDENTIFIER_VALUE. |
3767 | We don't want to do that, but we have to deal with the situation, | |
3768 | so let's give them some syntax errors to chew on instead of a | |
3769 | crash. */ | |
8d08fdba | 3770 | if (! template) |
f3400fe2 JM |
3771 | { |
3772 | cp_error ("`%T' is not a template", d1); | |
3773 | return error_mark_node; | |
3774 | } | |
2c73f9f5 | 3775 | |
c3baf4b5 JM |
3776 | if (context == NULL_TREE) |
3777 | context = global_namespace; | |
2c73f9f5 | 3778 | |
8d08fdba MS |
3779 | if (TREE_CODE (template) != TEMPLATE_DECL) |
3780 | { | |
8251199e | 3781 | cp_error ("non-template type `%T' used as a template", d1); |
8d08fdba | 3782 | if (in_decl) |
8251199e | 3783 | cp_error_at ("for template declaration `%D'", in_decl); |
8d08fdba MS |
3784 | return error_mark_node; |
3785 | } | |
8d08fdba | 3786 | |
73b0fce8 KL |
3787 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
3788 | { | |
3789 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
3790 | template arguments */ | |
3791 | ||
3792 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
3793 | tree template2 = TYPE_STUB_DECL (parm); | |
3794 | tree arglist2; | |
3795 | ||
73b0fce8 KL |
3796 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
3797 | ||
744fac59 | 3798 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1); |
73b0fce8 KL |
3799 | if (arglist2 == error_mark_node) |
3800 | return error_mark_node; | |
3801 | ||
7ddedda4 | 3802 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm) |
e1b3e07d | 3803 | = tree_cons (template2, arglist2, NULL_TREE); |
73b0fce8 KL |
3804 | TYPE_SIZE (parm) = 0; |
3805 | return parm; | |
3806 | } | |
36a117a5 | 3807 | else |
8d08fdba | 3808 | { |
36a117a5 | 3809 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 3810 | tree gen_tmpl; |
36a117a5 MM |
3811 | tree type_decl; |
3812 | tree found = NULL_TREE; | |
3813 | int arg_depth; | |
3814 | int parm_depth; | |
dbfe2124 | 3815 | int is_partial_instantiation; |
830bfa74 | 3816 | |
7ac7b28f MM |
3817 | gen_tmpl = most_general_template (template); |
3818 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
3819 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
3820 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3821 | ||
7ddedda4 MM |
3822 | /* We build up the coerced arguments and such on the |
3823 | momentary_obstack. */ | |
3824 | push_momentary (); | |
3825 | ||
36a117a5 MM |
3826 | if (arg_depth == 1 && parm_depth > 1) |
3827 | { | |
39c01e4c | 3828 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
3829 | For example, given: |
3830 | ||
3831 | template <class T> struct S1 { | |
3832 | template <class U> struct S2 {}; | |
3833 | template <class U> struct S2<U*> {}; | |
3834 | }; | |
3835 | ||
3836 | we will be called with an ARGLIST of `U*', but the | |
3837 | TEMPLATE will be `template <class T> template | |
3838 | <class U> struct S1<T>::S2'. We must fill in the missing | |
3839 | arguments. */ | |
7ac7b28f MM |
3840 | arglist |
3841 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), | |
3842 | arglist); | |
36a117a5 MM |
3843 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
3844 | } | |
5566b478 | 3845 | |
7ac7b28f | 3846 | /* Now we should enough arguments. */ |
36a117a5 MM |
3847 | my_friendly_assert (parm_depth == arg_depth, 0); |
3848 | ||
7ac7b28f MM |
3849 | /* From here on, we're only interested in the most general |
3850 | template. */ | |
3851 | template = gen_tmpl; | |
3852 | ||
36a117a5 MM |
3853 | /* Calculate the BOUND_ARGS. These will be the args that are |
3854 | actually tsubst'd into the definition to create the | |
3855 | instantiation. */ | |
3856 | if (parm_depth > 1) | |
830bfa74 MM |
3857 | { |
3858 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 3859 | int i; |
e4a84209 | 3860 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 3861 | |
cabc336a | 3862 | tree bound_args = make_temp_vec (parm_depth); |
830bfa74 | 3863 | |
e4a84209 | 3864 | for (i = saved_depth, |
830bfa74 | 3865 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 3866 | i > 0 && t != NULL_TREE; |
830bfa74 | 3867 | --i, t = TREE_CHAIN (t)) |
e4a84209 MM |
3868 | { |
3869 | tree a = coerce_template_parms (TREE_VALUE (t), | |
3870 | arglist, template, 1, 1); | |
3871 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); | |
3872 | ||
3873 | /* We temporarily reduce the length of the ARGLIST so | |
3874 | that coerce_template_parms will see only the arguments | |
3875 | corresponding to the template parameters it is | |
3876 | examining. */ | |
3877 | TREE_VEC_LENGTH (arglist)--; | |
3878 | } | |
3879 | ||
3880 | /* Restore the ARGLIST to its full size. */ | |
3881 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
3882 | ||
36a117a5 | 3883 | arglist = bound_args; |
830bfa74 MM |
3884 | } |
3885 | else | |
36a117a5 MM |
3886 | arglist |
3887 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
3888 | innermost_args (arglist), | |
3889 | template, 1, 1); | |
3890 | ||
3891 | if (arglist == error_mark_node) | |
3892 | /* We were unable to bind the arguments. */ | |
5566b478 | 3893 | return error_mark_node; |
5566b478 | 3894 | |
36a117a5 MM |
3895 | /* In the scope of a template class, explicit references to the |
3896 | template class refer to the type of the template, not any | |
3897 | instantiation of it. For example, in: | |
3898 | ||
3899 | template <class T> class C { void f(C<T>); } | |
3900 | ||
3901 | the `C<T>' is just the same as `C'. Outside of the | |
3902 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 3903 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
3904 | arglist)) |
3905 | { | |
3906 | found = template_type; | |
3907 | ||
3908 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 3909 | { |
36a117a5 MM |
3910 | tree ctx; |
3911 | ||
3912 | /* Note that we use DECL_CONTEXT, rather than | |
3913 | CP_DECL_CONTEXT, so that the termination test is | |
3914 | always just `ctx'. We're not interested in namepace | |
3915 | scopes. */ | |
3916 | for (ctx = current_class_type; | |
3917 | ctx; | |
3918 | ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't') | |
3919 | ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) | |
3bfdc719 | 3920 | if (same_type_p (ctx, template_type)) |
36a117a5 MM |
3921 | break; |
3922 | ||
3923 | if (!ctx) | |
3924 | /* We're not in the scope of the class, so the | |
3925 | TEMPLATE_TYPE is not the type we want after | |
3926 | all. */ | |
3927 | found = NULL_TREE; | |
5566b478 MS |
3928 | } |
3929 | } | |
36a117a5 MM |
3930 | |
3931 | if (!found) | |
75650646 | 3932 | { |
36a117a5 MM |
3933 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); |
3934 | found; found = TREE_CHAIN (found)) | |
3935 | if (comp_template_args (TREE_PURPOSE (found), arglist)) | |
3936 | break; | |
75650646 | 3937 | |
36a117a5 MM |
3938 | if (found) |
3939 | found = TREE_VALUE (found); | |
8d08fdba | 3940 | } |
3ebc5c52 | 3941 | |
36a117a5 | 3942 | if (found) |
8d08fdba | 3943 | { |
7ddedda4 | 3944 | pop_momentary (); |
36a117a5 | 3945 | return found; |
8d08fdba | 3946 | } |
5566b478 | 3947 | |
dbfe2124 | 3948 | /* This type is a "partial instantiation" if any of the template |
486e4077 MM |
3949 | arguments still inolve template parameters. Note that we set |
3950 | IS_PARTIAL_INSTANTIATION for partial specializations as | |
3951 | well. */ | |
dbfe2124 MM |
3952 | is_partial_instantiation = uses_template_parms (arglist); |
3953 | ||
3ebc5c52 MM |
3954 | if (!is_partial_instantiation |
3955 | && !PRIMARY_TEMPLATE_P (template) | |
3956 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
3957 | { | |
3958 | pop_momentary (); | |
3959 | found = xref_tag_from_type (TREE_TYPE (template), | |
3960 | DECL_NAME (template), | |
3961 | /*globalize=*/1); | |
3962 | return found; | |
3963 | } | |
3964 | ||
3965 | /* Since we didn't find the type, we'll have to create it. | |
3966 | Since we'll be saving this type on the | |
3967 | DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */ | |
3968 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
3969 | ||
36a117a5 | 3970 | /* Create the type. */ |
ed44da02 MM |
3971 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
3972 | { | |
dbfe2124 | 3973 | if (!is_partial_instantiation) |
61a127b3 | 3974 | t = start_enum (TYPE_IDENTIFIER (template_type)); |
ed44da02 | 3975 | else |
dbfe2124 | 3976 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
3977 | the values for the enumeration constants may involve |
3978 | template parameters. And, no one should be interested | |
3979 | in the enumeration constants for such a type. */ | |
3980 | t = make_node (ENUMERAL_TYPE); | |
3981 | } | |
3982 | else | |
3983 | { | |
3984 | t = make_lang_type (TREE_CODE (template_type)); | |
3985 | CLASSTYPE_DECLARED_CLASS (t) | |
3986 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
3987 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
3988 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
f668f160 | 3989 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ed44da02 MM |
3990 | } |
3991 | ||
61a127b3 | 3992 | /* If we called start_enum above, this information will already |
ed44da02 MM |
3993 | be set up. */ |
3994 | if (!TYPE_NAME (t)) | |
3995 | { | |
3996 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 3997 | |
9188c363 | 3998 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 3999 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 4000 | TYPE_STUB_DECL (t) = type_decl; |
ed44da02 MM |
4001 | DECL_SOURCE_FILE (type_decl) |
4002 | = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type)); | |
4003 | DECL_SOURCE_LINE (type_decl) | |
4004 | = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type)); | |
ed44da02 MM |
4005 | } |
4006 | else | |
4007 | type_decl = TYPE_NAME (t); | |
36a117a5 | 4008 | |
9fbf56f7 MM |
4009 | /* Set up the template information. We have to figure out which |
4010 | template is the immediate parent if this is a full | |
4011 | instantiation. */ | |
4012 | if (parm_depth == 1 || is_partial_instantiation | |
4013 | || !PRIMARY_TEMPLATE_P (template)) | |
4014 | /* This case is easy; there are no member templates involved. */ | |
4015 | found = template; | |
4016 | else | |
4017 | { | |
4018 | /* This is a full instantiation of a member template. There | |
4019 | should be some partial instantiation of which this is an | |
4020 | instance. */ | |
4021 | ||
4022 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
4023 | found; found = TREE_CHAIN (found)) | |
4024 | { | |
4025 | int success; | |
4026 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
4027 | ||
4028 | /* We only want partial instantiations, here, not | |
4029 | specializations or full instantiations. */ | |
4030 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) | |
4031 | || !uses_template_parms (TREE_VALUE (found))) | |
4032 | continue; | |
4033 | ||
4034 | /* Temporarily reduce by one the number of levels in the | |
4035 | ARGLIST and in FOUND so as to avoid comparing the | |
4036 | last set of arguments. */ | |
4037 | TREE_VEC_LENGTH (arglist)--; | |
4038 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
4039 | ||
4040 | /* See if the arguments match. If they do, then TMPL is | |
4041 | the partial instantiation we want. */ | |
4042 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
4043 | ||
4044 | /* Restore the argument vectors to their full size. */ | |
4045 | TREE_VEC_LENGTH (arglist)++; | |
4046 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
4047 | ||
4048 | if (success) | |
4049 | { | |
4050 | found = tmpl; | |
4051 | break; | |
4052 | } | |
4053 | } | |
4054 | ||
4055 | if (!found) | |
4056 | my_friendly_abort (0); | |
4057 | } | |
4058 | ||
ed44da02 | 4059 | SET_TYPE_TEMPLATE_INFO (t, |
9fbf56f7 | 4060 | tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
4061 | DECL_TEMPLATE_INSTANTIATIONS (template) |
4062 | = tree_cons (arglist, t, | |
4063 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
4064 | ||
4065 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
4066 | && !is_partial_instantiation) | |
61a127b3 MM |
4067 | /* Now that the type has been registered on the instantiations |
4068 | list, we set up the enumerators. Because the enumeration | |
4069 | constants may involve the enumeration type itself, we make | |
4070 | sure to register the type first, and then create the | |
4071 | constants. That way, doing tsubst_expr for the enumeration | |
4072 | constants won't result in recursive calls here; we'll find | |
4073 | the instantiation and exit above. */ | |
4074 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 MM |
4075 | |
4076 | /* We're done with the permanent obstack, now. */ | |
4077 | pop_obstacks (); | |
7ddedda4 MM |
4078 | /* We're also done with the momentary allocation we started |
4079 | above. */ | |
4080 | pop_momentary (); | |
5566b478 | 4081 | |
36a117a5 MM |
4082 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
4083 | is set up. */ | |
ed44da02 MM |
4084 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
4085 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
36a117a5 | 4086 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
dbfe2124 | 4087 | if (!is_partial_instantiation) |
36a117a5 MM |
4088 | { |
4089 | DECL_ASSEMBLER_NAME (type_decl) | |
4090 | = get_identifier (build_overload_name (t, 1, 1)); | |
50a6dbd7 JM |
4091 | |
4092 | /* For backwards compatibility; code that uses | |
4093 | -fexternal-templates expects looking up a template to | |
4094 | instantiate it. I think DDD still relies on this. | |
4095 | (jason 8/20/1998) */ | |
ed44da02 MM |
4096 | if (TREE_CODE (t) != ENUMERAL_TYPE |
4097 | && flag_external_templates | |
36a117a5 MM |
4098 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) |
4099 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
4100 | add_pending_template (t); | |
4101 | } | |
4102 | else | |
077e7015 MM |
4103 | /* If the type makes use of template parameters, the |
4104 | code that generates debugging information will crash. */ | |
4105 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 4106 | |
36a117a5 MM |
4107 | return t; |
4108 | } | |
8d08fdba MS |
4109 | } |
4110 | \f | |
050367a3 MM |
4111 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or |
4112 | TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA. | |
4113 | If FN returns non-zero, the iteration is terminated, and | |
4114 | for_each_template_parm returns 1. Otherwise, the iteration | |
4115 | continues. If FN never returns a non-zero value, the value | |
4116 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
4117 | considered to be the function which always returns 1. */ | |
4118 | ||
e9659ab0 | 4119 | static int |
050367a3 | 4120 | for_each_template_parm (t, fn, data) |
8d08fdba | 4121 | tree t; |
050367a3 MM |
4122 | tree_fn_t fn; |
4123 | void* data; | |
8d08fdba MS |
4124 | { |
4125 | if (!t) | |
4126 | return 0; | |
581d38d0 MM |
4127 | |
4128 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't' | |
4129 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data)) | |
4130 | return 1; | |
4131 | ||
8d08fdba MS |
4132 | switch (TREE_CODE (t)) |
4133 | { | |
297e73d8 | 4134 | case ARRAY_REF: |
05e0b2f4 | 4135 | case OFFSET_REF: |
297e73d8 MM |
4136 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) |
4137 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4138 | ||
8d08fdba MS |
4139 | case IDENTIFIER_NODE: |
4140 | if (!IDENTIFIER_TEMPLATE (t)) | |
4141 | return 0; | |
5566b478 | 4142 | my_friendly_abort (42); |
8d08fdba MS |
4143 | |
4144 | /* aggregates of tree nodes */ | |
4145 | case TREE_VEC: | |
4146 | { | |
4147 | int i = TREE_VEC_LENGTH (t); | |
4148 | while (i--) | |
050367a3 | 4149 | if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data)) |
8d08fdba MS |
4150 | return 1; |
4151 | return 0; | |
4152 | } | |
4153 | case TREE_LIST: | |
050367a3 MM |
4154 | if (for_each_template_parm (TREE_PURPOSE (t), fn, data) |
4155 | || for_each_template_parm (TREE_VALUE (t), fn, data)) | |
8d08fdba | 4156 | return 1; |
050367a3 | 4157 | return for_each_template_parm (TREE_CHAIN (t), fn, data); |
8d08fdba | 4158 | |
2c73f9f5 ML |
4159 | case OVERLOAD: |
4160 | if (for_each_template_parm (OVL_FUNCTION (t), fn, data)) | |
4161 | return 1; | |
4162 | return for_each_template_parm (OVL_CHAIN (t), fn, data); | |
4163 | ||
8d08fdba MS |
4164 | /* constructed type nodes */ |
4165 | case POINTER_TYPE: | |
4166 | case REFERENCE_TYPE: | |
050367a3 | 4167 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
ed44da02 | 4168 | |
8d08fdba | 4169 | case RECORD_TYPE: |
b7484fbe | 4170 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
050367a3 MM |
4171 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t), |
4172 | fn, data); | |
ed44da02 MM |
4173 | /* Fall through. */ |
4174 | ||
8d08fdba | 4175 | case UNION_TYPE: |
ed44da02 MM |
4176 | case ENUMERAL_TYPE: |
4177 | if (! TYPE_TEMPLATE_INFO (t)) | |
8d08fdba | 4178 | return 0; |
050367a3 | 4179 | return for_each_template_parm (TREE_VALUE |
ed44da02 | 4180 | (TYPE_TEMPLATE_INFO (t)), |
050367a3 | 4181 | fn, data); |
588c2d1c MM |
4182 | case METHOD_TYPE: |
4183 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data)) | |
8d08fdba | 4184 | return 1; |
588c2d1c MM |
4185 | /* Fall through. */ |
4186 | ||
4187 | case FUNCTION_TYPE: | |
4188 | /* Check the parameter types. Since default arguments are not | |
4189 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4190 | contain expressions that involve template parameters. But, | |
4191 | no-one should be looking at them yet. And, once they're | |
4192 | instantiated, they don't contain template parameters, so | |
4193 | there's no point in looking at them then, either. */ | |
4194 | { | |
4195 | tree parm; | |
4196 | ||
4197 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4198 | if (for_each_template_parm (TREE_VALUE (parm), fn, data)) | |
4199 | return 1; | |
4200 | } | |
4201 | ||
4202 | /* Check the return type, too. */ | |
050367a3 | 4203 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
588c2d1c | 4204 | |
8d08fdba | 4205 | case ARRAY_TYPE: |
050367a3 | 4206 | if (for_each_template_parm (TYPE_DOMAIN (t), fn, data)) |
8d08fdba | 4207 | return 1; |
050367a3 | 4208 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 4209 | case OFFSET_TYPE: |
050367a3 | 4210 | if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data)) |
8d08fdba | 4211 | return 1; |
050367a3 | 4212 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
4213 | |
4214 | /* decl nodes */ | |
4215 | case TYPE_DECL: | |
050367a3 | 4216 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
5566b478 | 4217 | |
73b0fce8 KL |
4218 | case TEMPLATE_DECL: |
4219 | /* A template template parameter is encountered */ | |
4220 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
744fac59 KL |
4221 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
4222 | /* Already substituted template template parameter */ | |
73b0fce8 KL |
4223 | return 0; |
4224 | ||
3ac3d9ea | 4225 | case CONST_DECL: |
050367a3 | 4226 | if (for_each_template_parm (DECL_INITIAL (t), fn, data)) |
3ac3d9ea MM |
4227 | return 1; |
4228 | goto check_type_and_context; | |
4229 | ||
8d08fdba | 4230 | case FUNCTION_DECL: |
5566b478 | 4231 | case VAR_DECL: |
5566b478 | 4232 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
050367a3 | 4233 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data)) |
8d08fdba MS |
4234 | return 1; |
4235 | /* fall through */ | |
8d08fdba | 4236 | case PARM_DECL: |
3ac3d9ea | 4237 | check_type_and_context: |
050367a3 | 4238 | if (for_each_template_parm (TREE_TYPE (t), fn, data)) |
5566b478 | 4239 | return 1; |
050367a3 MM |
4240 | if (DECL_CONTEXT (t) |
4241 | && for_each_template_parm (DECL_CONTEXT (t), fn, data)) | |
8d08fdba | 4242 | return 1; |
8d08fdba MS |
4243 | return 0; |
4244 | ||
4245 | case CALL_EXPR: | |
297e73d8 MM |
4246 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) |
4247 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4248 | ||
8d08fdba | 4249 | case ADDR_EXPR: |
050367a3 | 4250 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
8d08fdba MS |
4251 | |
4252 | /* template parm nodes */ | |
73b0fce8 | 4253 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4254 | /* Record template parameters such as `T' inside `TT<T>'. */ |
7ddedda4 MM |
4255 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t) |
4256 | && for_each_template_parm (TYPE_TI_ARGS (t), fn, data)) | |
744fac59 KL |
4257 | return 1; |
4258 | case TEMPLATE_TYPE_PARM: | |
f84b4be9 | 4259 | case TEMPLATE_PARM_INDEX: |
050367a3 MM |
4260 | if (fn) |
4261 | return (*fn)(t, data); | |
4262 | else | |
4263 | return 1; | |
8d08fdba MS |
4264 | |
4265 | /* simple type nodes */ | |
4266 | case INTEGER_TYPE: | |
050367a3 | 4267 | if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data)) |
8d08fdba | 4268 | return 1; |
050367a3 | 4269 | return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data); |
8d08fdba MS |
4270 | |
4271 | case REAL_TYPE: | |
37c46b43 | 4272 | case COMPLEX_TYPE: |
8d08fdba | 4273 | case VOID_TYPE: |
2986ae00 | 4274 | case BOOLEAN_TYPE: |
2c73f9f5 | 4275 | case NAMESPACE_DECL: |
4699c561 | 4276 | case FIELD_DECL: |
8d08fdba MS |
4277 | return 0; |
4278 | ||
4279 | /* constants */ | |
4280 | case INTEGER_CST: | |
4281 | case REAL_CST: | |
4282 | case STRING_CST: | |
4283 | return 0; | |
4284 | ||
4285 | case ERROR_MARK: | |
4286 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
4287 | my_friendly_assert (t == error_mark_node, 274); | |
4288 | /* NOTREACHED */ | |
4289 | return 0; | |
4290 | ||
61a127b3 MM |
4291 | case PTRMEM_CST: |
4292 | return for_each_template_parm (TREE_TYPE (t), fn, data); | |
4293 | ||
5156628f | 4294 | case SCOPE_REF: |
050367a3 | 4295 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
5156628f | 4296 | |
db5ae43f MS |
4297 | case CONSTRUCTOR: |
4298 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
050367a3 MM |
4299 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4300 | (TREE_TYPE (t)), fn, data); | |
4301 | return for_each_template_parm (TREE_OPERAND (t, 1), fn, data); | |
db5ae43f | 4302 | |
b8865407 MM |
4303 | case SIZEOF_EXPR: |
4304 | case ALIGNOF_EXPR: | |
4305 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); | |
4306 | ||
4699c561 MM |
4307 | case TYPENAME_TYPE: |
4308 | if (!fn) | |
4309 | return 1; | |
4310 | return (for_each_template_parm (TYPE_CONTEXT (t), fn, data) | |
4311 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), | |
4312 | fn, data)); | |
4313 | ||
b8865407 MM |
4314 | case INDIRECT_REF: |
4315 | case COMPONENT_REF: | |
4699c561 | 4316 | /* If there's no type, then this thing must be some expression |
b8865407 | 4317 | involving template parameters. */ |
4699c561 MM |
4318 | if (!fn && !TREE_TYPE (t)) |
4319 | return 1; | |
4320 | if (TREE_CODE (t) == COMPONENT_REF) | |
4321 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) | |
4322 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4323 | else | |
4324 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); | |
b8865407 | 4325 | |
42976354 BK |
4326 | case MODOP_EXPR: |
4327 | case CAST_EXPR: | |
4328 | case REINTERPRET_CAST_EXPR: | |
4329 | case CONST_CAST_EXPR: | |
4330 | case STATIC_CAST_EXPR: | |
4331 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
4332 | case ARROW_EXPR: |
4333 | case DOTSTAR_EXPR: | |
4334 | case TYPEID_EXPR: | |
b8865407 | 4335 | case LOOKUP_EXPR: |
40242ccf | 4336 | case PSEUDO_DTOR_EXPR: |
b8865407 MM |
4337 | if (!fn) |
4338 | return 1; | |
4339 | /* Fall through. */ | |
abff8e06 | 4340 | |
8d08fdba MS |
4341 | default: |
4342 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
4343 | { | |
4344 | case '1': | |
4345 | case '2': | |
ec255269 | 4346 | case 'e': |
8d08fdba MS |
4347 | case '<': |
4348 | { | |
4349 | int i; | |
bbc06b4b | 4350 | for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;) |
050367a3 | 4351 | if (for_each_template_parm (TREE_OPERAND (t, i), fn, data)) |
8d08fdba MS |
4352 | return 1; |
4353 | return 0; | |
4354 | } | |
4355 | default: | |
4356 | break; | |
4357 | } | |
4358 | sorry ("testing %s for template parms", | |
4359 | tree_code_name [(int) TREE_CODE (t)]); | |
4360 | my_friendly_abort (82); | |
4361 | /* NOTREACHED */ | |
4362 | return 0; | |
4363 | } | |
4364 | } | |
4365 | ||
050367a3 MM |
4366 | int |
4367 | uses_template_parms (t) | |
4368 | tree t; | |
4369 | { | |
4370 | return for_each_template_parm (t, 0, 0); | |
4371 | } | |
4372 | ||
27fafc8d JM |
4373 | static struct tinst_level *current_tinst_level; |
4374 | static struct tinst_level *free_tinst_level; | |
4375 | static int tinst_depth; | |
e9f32eb5 | 4376 | extern int max_tinst_depth; |
5566b478 | 4377 | #ifdef GATHER_STATISTICS |
27fafc8d | 4378 | int depth_reached; |
5566b478 | 4379 | #endif |
27fafc8d JM |
4380 | int tinst_level_tick; |
4381 | int last_template_error_tick; | |
8d08fdba | 4382 | |
7bae46f4 | 4383 | /* Print out all the template instantiations that we are currently |
27fafc8d JM |
4384 | working on. If ERR, we are being called from cp_thing, so do |
4385 | the right thing for an error message. */ | |
7bae46f4 | 4386 | |
27fafc8d JM |
4387 | static void |
4388 | print_template_context (err) | |
4389 | int err; | |
7bae46f4 JM |
4390 | { |
4391 | struct tinst_level *p = current_tinst_level; | |
4392 | int line = lineno; | |
4393 | char *file = input_filename; | |
4394 | ||
49e04385 | 4395 | if (err && p) |
27fafc8d | 4396 | { |
49e04385 MM |
4397 | if (current_function_decl != p->decl |
4398 | && current_function_decl != NULL_TREE) | |
4399 | /* We can get here during the processing of some synthesized | |
4400 | method. Then, p->decl will be the function that's causing | |
4401 | the synthesis. */ | |
4402 | ; | |
27fafc8d | 4403 | else |
27fafc8d | 4404 | { |
49e04385 MM |
4405 | if (current_function_decl == p->decl) |
4406 | /* Avoid redundancy with the the "In function" line. */; | |
4407 | else | |
4408 | fprintf (stderr, "%s: In instantiation of `%s':\n", | |
4409 | file, decl_as_string (p->decl, 0)); | |
4410 | ||
038fb86f JM |
4411 | line = p->line; |
4412 | file = p->file; | |
27fafc8d JM |
4413 | p = p->next; |
4414 | } | |
4415 | } | |
4416 | ||
7bae46f4 JM |
4417 | for (; p; p = p->next) |
4418 | { | |
038fb86f JM |
4419 | fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line, |
4420 | decl_as_string (p->decl, 0)); | |
4421 | line = p->line; | |
4422 | file = p->file; | |
7bae46f4 | 4423 | } |
038fb86f | 4424 | fprintf (stderr, "%s:%d: instantiated from here\n", file, line); |
7bae46f4 JM |
4425 | } |
4426 | ||
27fafc8d JM |
4427 | /* Called from cp_thing to print the template context for an error. */ |
4428 | ||
4429 | void | |
4430 | maybe_print_template_context () | |
4431 | { | |
4432 | if (last_template_error_tick == tinst_level_tick | |
4433 | || current_tinst_level == 0) | |
4434 | return; | |
4435 | ||
4436 | last_template_error_tick = tinst_level_tick; | |
4437 | print_template_context (1); | |
4438 | } | |
4439 | ||
bd6dd845 | 4440 | static int |
5566b478 MS |
4441 | push_tinst_level (d) |
4442 | tree d; | |
8d08fdba MS |
4443 | { |
4444 | struct tinst_level *new; | |
8d08fdba | 4445 | |
7215f9a0 MS |
4446 | if (tinst_depth >= max_tinst_depth) |
4447 | { | |
8adf5b5e JM |
4448 | /* If the instantiation in question still has unbound template parms, |
4449 | we don't really care if we can't instantiate it, so just return. | |
4450 | This happens with base instantiation for implicit `typename'. */ | |
4451 | if (uses_template_parms (d)) | |
4452 | return 0; | |
4453 | ||
1139b3d8 | 4454 | last_template_error_tick = tinst_level_tick; |
8251199e | 4455 | error ("template instantiation depth exceeds maximum of %d", |
7215f9a0 | 4456 | max_tinst_depth); |
8251199e JM |
4457 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
4458 | cp_error (" instantiating `%D'", d); | |
5566b478 | 4459 | |
27fafc8d | 4460 | print_template_context (0); |
5566b478 | 4461 | |
7215f9a0 MS |
4462 | return 0; |
4463 | } | |
4464 | ||
8d08fdba MS |
4465 | if (free_tinst_level) |
4466 | { | |
4467 | new = free_tinst_level; | |
4468 | free_tinst_level = new->next; | |
4469 | } | |
4470 | else | |
4471 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
4472 | ||
5566b478 MS |
4473 | new->decl = d; |
4474 | new->line = lineno; | |
4475 | new->file = input_filename; | |
8d08fdba MS |
4476 | new->next = current_tinst_level; |
4477 | current_tinst_level = new; | |
5566b478 | 4478 | |
7215f9a0 | 4479 | ++tinst_depth; |
5566b478 MS |
4480 | #ifdef GATHER_STATISTICS |
4481 | if (tinst_depth > depth_reached) | |
4482 | depth_reached = tinst_depth; | |
4483 | #endif | |
4484 | ||
27fafc8d | 4485 | ++tinst_level_tick; |
7215f9a0 | 4486 | return 1; |
8d08fdba MS |
4487 | } |
4488 | ||
4489 | void | |
4490 | pop_tinst_level () | |
4491 | { | |
4492 | struct tinst_level *old = current_tinst_level; | |
4493 | ||
ae58fa02 MM |
4494 | /* Restore the filename and line number stashed away when we started |
4495 | this instantiation. */ | |
4496 | lineno = old->line; | |
4497 | input_filename = old->file; | |
5f2c99c4 | 4498 | extract_interface_info (); |
ae58fa02 | 4499 | |
8d08fdba MS |
4500 | current_tinst_level = old->next; |
4501 | old->next = free_tinst_level; | |
4502 | free_tinst_level = old; | |
7215f9a0 | 4503 | --tinst_depth; |
27fafc8d | 4504 | ++tinst_level_tick; |
8d08fdba MS |
4505 | } |
4506 | ||
4507 | struct tinst_level * | |
4508 | tinst_for_decl () | |
4509 | { | |
4510 | struct tinst_level *p = current_tinst_level; | |
4511 | ||
4512 | if (p) | |
4513 | for (; p->next ; p = p->next ) | |
4514 | ; | |
4515 | return p; | |
4516 | } | |
4517 | ||
f84b4be9 JM |
4518 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
4519 | vector of template arguments, as for tsubst. | |
4520 | ||
4521 | Returns an appropriate tsbust'd friend declaration. */ | |
4522 | ||
4523 | static tree | |
4524 | tsubst_friend_function (decl, args) | |
4525 | tree decl; | |
4526 | tree args; | |
4527 | { | |
4528 | tree new_friend; | |
27fafc8d JM |
4529 | int line = lineno; |
4530 | char *file = input_filename; | |
4531 | ||
4532 | lineno = DECL_SOURCE_LINE (decl); | |
4533 | input_filename = DECL_SOURCE_FILE (decl); | |
4534 | ||
f84b4be9 JM |
4535 | if (TREE_CODE (decl) == FUNCTION_DECL |
4536 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
4537 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
4538 | /* This was a friend declared with an explicit template | |
4539 | argument list, e.g.: | |
4540 | ||
4541 | friend void f<>(T); | |
4542 | ||
4543 | to indicate that f was a template instantiation, not a new | |
4544 | function declaration. Now, we have to figure out what | |
4545 | instantiation of what template. */ | |
4546 | { | |
4547 | tree template_id; | |
4548 | tree new_args; | |
4549 | tree tmpl; | |
f84b4be9 JM |
4550 | |
4551 | template_id | |
4552 | = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl), | |
4393e105 MM |
4553 | args, /*complain=*/1, |
4554 | NULL_TREE), | |
f84b4be9 | 4555 | tsubst (DECL_TI_ARGS (decl), |
4393e105 MM |
4556 | args, /*complain=*/1, |
4557 | NULL_TREE)); | |
39c01e4c MM |
4558 | /* FIXME: The decl we create via the next tsubst could be |
4559 | created on a temporary obstack. */ | |
4393e105 | 4560 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
4561 | tmpl = determine_specialization (template_id, new_friend, |
4562 | &new_args, | |
bf8f3f93 | 4563 | /*need_member_template=*/0); |
27fafc8d JM |
4564 | new_friend = instantiate_template (tmpl, new_args); |
4565 | goto done; | |
f84b4be9 | 4566 | } |
36a117a5 | 4567 | |
4393e105 | 4568 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
f84b4be9 | 4569 | |
36a117a5 | 4570 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
4571 | compiler, but is not an instantiation from the point of view of |
4572 | the language. For example, we might have had: | |
4573 | ||
4574 | template <class T> struct S { | |
4575 | template <class U> friend void f(T, U); | |
4576 | }; | |
4577 | ||
4578 | Then, in S<int>, template <class U> void f(int, U) is not an | |
4579 | instantiation of anything. */ | |
4580 | DECL_USE_TEMPLATE (new_friend) = 0; | |
4581 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
4582 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
36a117a5 MM |
4583 | |
4584 | /* The mangled name for the NEW_FRIEND is incorrect. The call to | |
4585 | tsubst will have resulted in a call to | |
4586 | set_mangled_name_for_template_decl. But, the function is not a | |
4587 | template instantiation and should not be mangled like one. | |
4588 | Therefore, we remangle the function name. We don't have to do | |
4589 | this if the NEW_FRIEND is a template since | |
4590 | set_mangled_name_for_template_decl doesn't do anything if the | |
4591 | function declaration still uses template arguments. */ | |
4592 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) | |
4593 | { | |
4594 | set_mangled_name_for_decl (new_friend); | |
4595 | DECL_RTL (new_friend) = 0; | |
4596 | make_decl_rtl (new_friend, NULL_PTR, 1); | |
4597 | } | |
4598 | ||
6eb3bb27 | 4599 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 4600 | { |
36a117a5 | 4601 | tree old_decl; |
fbf1c34b MM |
4602 | tree new_friend_template_info; |
4603 | tree new_friend_result_template_info; | |
92da7074 | 4604 | tree ns; |
fbf1c34b MM |
4605 | int new_friend_is_defn; |
4606 | ||
4607 | /* We must save some information from NEW_FRIEND before calling | |
4608 | duplicate decls since that function will free NEW_FRIEND if | |
4609 | possible. */ | |
4610 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
f84b4be9 | 4611 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
4612 | { |
4613 | /* This declaration is a `primary' template. */ | |
4614 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
4615 | ||
4616 | new_friend_is_defn | |
4617 | = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE; | |
4618 | new_friend_result_template_info | |
4619 | = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend)); | |
4620 | } | |
4621 | else | |
4622 | { | |
4623 | new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; | |
4624 | new_friend_result_template_info = NULL_TREE; | |
4625 | } | |
36a117a5 | 4626 | |
92da7074 ML |
4627 | /* Inside pushdecl_namespace_level, we will push into the |
4628 | current namespace. However, the friend function should | |
4629 | tyically go into the namespace of the template. */ | |
4630 | ns = decl_namespace_context (new_friend); | |
4631 | push_nested_namespace (ns); | |
36a117a5 | 4632 | old_decl = pushdecl_namespace_level (new_friend); |
92da7074 | 4633 | pop_nested_namespace (ns); |
36a117a5 MM |
4634 | |
4635 | if (old_decl != new_friend) | |
4636 | { | |
4637 | /* This new friend declaration matched an existing | |
4638 | declaration. For example, given: | |
4639 | ||
4640 | template <class T> void f(T); | |
4641 | template <class U> class C { | |
4642 | template <class T> friend void f(T) {} | |
4643 | }; | |
4644 | ||
4645 | the friend declaration actually provides the definition | |
4646 | of `f', once C has been instantiated for some type. So, | |
4647 | old_decl will be the out-of-class template declaration, | |
4648 | while new_friend is the in-class definition. | |
4649 | ||
4650 | But, if `f' was called before this point, the | |
4651 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
4652 | to `T' but not to `U', references to which might appear | |
4653 | in the definition of `f'. Previously, the most general | |
4654 | template for an instantiation of `f' was the out-of-class | |
4655 | version; now it is the in-class version. Therefore, we | |
4656 | run through all specialization of `f', adding to their | |
4657 | DECL_TI_ARGS appropriately. In particular, they need a | |
4658 | new set of outer arguments, corresponding to the | |
4659 | arguments for this class instantiation. | |
4660 | ||
4661 | The same situation can arise with something like this: | |
4662 | ||
4663 | friend void f(int); | |
4664 | template <class T> class C { | |
4665 | friend void f(T) {} | |
4666 | }; | |
4667 | ||
4668 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
4669 | in the class. */ | |
4670 | ||
fbf1c34b MM |
4671 | if (!new_friend_is_defn) |
4672 | /* On the other hand, if the in-class declaration does | |
4673 | *not* provide a definition, then we don't want to alter | |
4674 | existing definitions. We can just leave everything | |
4675 | alone. */ | |
36a117a5 | 4676 | ; |
fbf1c34b | 4677 | else |
36a117a5 | 4678 | { |
fbf1c34b MM |
4679 | /* Overwrite whatever template info was there before, if |
4680 | any, with the new template information pertaining to | |
4681 | the declaration. */ | |
4682 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
4683 | ||
4684 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
4685 | /* duplicate_decls will take care of this case. */ | |
4686 | ; | |
4687 | else | |
36a117a5 | 4688 | { |
fbf1c34b MM |
4689 | tree t; |
4690 | tree new_friend_args; | |
4691 | ||
4692 | DECL_TEMPLATE_INFO (DECL_RESULT (old_decl)) | |
4693 | = new_friend_result_template_info; | |
4694 | ||
4695 | new_friend_args = TI_ARGS (new_friend_template_info); | |
4696 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
4697 | t != NULL_TREE; | |
4698 | t = TREE_CHAIN (t)) | |
4699 | { | |
4700 | tree spec = TREE_VALUE (t); | |
36a117a5 | 4701 | |
fbf1c34b MM |
4702 | DECL_TI_ARGS (spec) |
4703 | = add_outermost_template_args (new_friend_args, | |
4704 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
4705 | } |
4706 | ||
4707 | /* Now, since specializations are always supposed to | |
4708 | hang off of the most general template, we must move | |
4709 | them. */ | |
4710 | t = most_general_template (old_decl); | |
4711 | if (t != old_decl) | |
4712 | { | |
4713 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
4714 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
4715 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
4716 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
4717 | } | |
36a117a5 MM |
4718 | } |
4719 | } | |
4720 | ||
4721 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
4722 | by duplicate_decls. */ | |
4723 | new_friend = old_decl; | |
4724 | } | |
f84b4be9 JM |
4725 | } |
4726 | else if (TYPE_SIZE (DECL_CONTEXT (new_friend))) | |
4727 | { | |
4728 | /* Check to see that the declaration is really present, and, | |
4729 | possibly obtain an improved declaration. */ | |
4730 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
4731 | new_friend); | |
4732 | ||
4733 | if (fn) | |
4734 | new_friend = fn; | |
4735 | } | |
4736 | ||
27fafc8d JM |
4737 | done: |
4738 | lineno = line; | |
4739 | input_filename = file; | |
f84b4be9 JM |
4740 | return new_friend; |
4741 | } | |
4742 | ||
1aed5355 MM |
4743 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
4744 | template arguments, as for tsubst. | |
6757edfe MM |
4745 | |
4746 | Returns an appropriate tsbust'd friend type. */ | |
4747 | ||
4748 | static tree | |
1aed5355 MM |
4749 | tsubst_friend_class (friend_tmpl, args) |
4750 | tree friend_tmpl; | |
6757edfe MM |
4751 | tree args; |
4752 | { | |
1aed5355 | 4753 | tree friend_type; |
25aab5d0 | 4754 | tree tmpl; |
6757edfe | 4755 | |
25aab5d0 MM |
4756 | /* First, we look for a class template. */ |
4757 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); | |
4758 | ||
4759 | /* But, if we don't find one, it might be because we're in a | |
4760 | situation like this: | |
4761 | ||
4762 | template <class T> | |
4763 | struct S { | |
4764 | template <class U> | |
4765 | friend struct S; | |
4766 | }; | |
4767 | ||
4768 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
4769 | for `S<int>', not the TEMPLATE_DECL. */ | |
bc639f90 | 4770 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) |
25aab5d0 MM |
4771 | { |
4772 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); | |
4773 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
4774 | } | |
6757edfe | 4775 | |
25aab5d0 | 4776 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
4777 | { |
4778 | /* The friend template has already been declared. Just | |
36a117a5 MM |
4779 | check to see that the declarations match, and install any new |
4780 | default parameters. We must tsubst the default parameters, | |
4781 | of course. We only need the innermost template parameters | |
4782 | because that is all that redeclare_class_template will look | |
4783 | at. */ | |
4784 | tree parms | |
4785 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
4393e105 | 4786 | args, /*complain=*/1); |
36a117a5 | 4787 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
6757edfe MM |
4788 | friend_type = TREE_TYPE (tmpl); |
4789 | } | |
4790 | else | |
4791 | { | |
4792 | /* The friend template has not already been declared. In this | |
4793 | case, the instantiation of the template class will cause the | |
4794 | injection of this template into the global scope. */ | |
4393e105 | 4795 | tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE); |
6757edfe MM |
4796 | |
4797 | /* The new TMPL is not an instantiation of anything, so we | |
4798 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
4799 | the new type because that is supposed to be the corresponding | |
4800 | template decl, i.e., TMPL. */ | |
4801 | DECL_USE_TEMPLATE (tmpl) = 0; | |
4802 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
4803 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
4804 | ||
4805 | /* Inject this template into the global scope. */ | |
4806 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
4807 | } | |
4808 | ||
4809 | return friend_type; | |
4810 | } | |
f84b4be9 | 4811 | |
8d08fdba | 4812 | tree |
5566b478 MS |
4813 | instantiate_class_template (type) |
4814 | tree type; | |
8d08fdba | 4815 | { |
61a127b3 | 4816 | tree template, args, pattern, t; |
36a117a5 | 4817 | tree typedecl; |
8d08fdba | 4818 | |
5566b478 | 4819 | if (type == error_mark_node) |
8d08fdba MS |
4820 | return error_mark_node; |
4821 | ||
5566b478 MS |
4822 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) |
4823 | return type; | |
4824 | ||
7ddedda4 MM |
4825 | /* We want to allocate temporary vectors of template arguments and |
4826 | template argument expressions on the momentary obstack, not on | |
4827 | the expression obstack. Otherwise, all the space allocated in | |
4828 | argument coercion and such is simply lost. */ | |
4829 | push_momentary (); | |
4830 | ||
6bdb985f | 4831 | /* Figure out which template is being instantiated. */ |
36a117a5 | 4832 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
5566b478 | 4833 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); |
73aad9b9 | 4834 | |
6bdb985f MM |
4835 | /* Figure out which arguments are being used to do the |
4836 | instantiation. */ | |
4837 | args = CLASSTYPE_TI_ARGS (type); | |
4c571114 | 4838 | PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args); |
6bdb985f | 4839 | |
4c571114 MM |
4840 | if (pedantic && PARTIAL_INSTANTIATION_P (type)) |
4841 | /* If this is a partial instantiation, then we can't instantiate | |
4842 | the type; there's no telling whether or not one of the | |
4843 | template parameters might eventually be instantiated to some | |
4844 | value that results in a specialization being used. For | |
4845 | example, consider: | |
4846 | ||
4847 | template <class T> | |
4848 | struct S {}; | |
4849 | ||
4850 | template <class U> | |
4851 | void f(S<U>); | |
4852 | ||
4853 | template <> | |
4854 | struct S<int> {}; | |
4855 | ||
4856 | Now, the `S<U>' in `f<int>' is the specialization, not an | |
5db698f6 | 4857 | instantiation of the original template. */ |
4c571114 MM |
4858 | goto end; |
4859 | ||
4860 | /* Determine what specialization of the original template to | |
4861 | instantiate. */ | |
4862 | if (PARTIAL_INSTANTIATION_P (type)) | |
6bdb985f MM |
4863 | /* There's no telling which specialization is appropriate at this |
4864 | point. Since all peeking at the innards of this partial | |
4865 | instantiation are extensions (like the "implicit typename" | |
4866 | extension, which allows users to omit the keyword `typename' on | |
4867 | names that are declared as types in template base classes), we | |
4868 | are free to do what we please. | |
4869 | ||
4870 | Trying to figure out which partial instantiation to use can | |
4871 | cause a crash. (Some of the template arguments don't even have | |
4872 | types.) So, we just use the most general version. */ | |
4873 | t = NULL_TREE; | |
4874 | else | |
73aad9b9 | 4875 | { |
6bdb985f MM |
4876 | t = most_specialized_class (template, args); |
4877 | ||
4878 | if (t == error_mark_node) | |
73aad9b9 | 4879 | { |
d8e178a0 | 4880 | const char *str = "candidates are:"; |
6bdb985f MM |
4881 | cp_error ("ambiguous class template instantiation for `%#T'", type); |
4882 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; | |
4883 | t = TREE_CHAIN (t)) | |
73aad9b9 | 4884 | { |
6bdb985f MM |
4885 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
4886 | args)) | |
4887 | { | |
4888 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
4889 | str = " "; | |
4890 | } | |
73aad9b9 | 4891 | } |
6bdb985f MM |
4892 | TYPE_BEING_DEFINED (type) = 1; |
4893 | type = error_mark_node; | |
4894 | goto end; | |
73aad9b9 | 4895 | } |
73aad9b9 | 4896 | } |
6bdb985f MM |
4897 | |
4898 | if (t) | |
73aad9b9 JM |
4899 | pattern = TREE_TYPE (t); |
4900 | else | |
4901 | pattern = TREE_TYPE (template); | |
5566b478 | 4902 | |
4c571114 MM |
4903 | /* If the template we're instantiating is incomplete, then clearly |
4904 | there's nothing we can do. */ | |
5566b478 | 4905 | if (TYPE_SIZE (pattern) == NULL_TREE) |
7ddedda4 | 4906 | goto end; |
5566b478 | 4907 | |
4c571114 MM |
4908 | /* If this is a partial instantiation, don't tsubst anything. We will |
4909 | only use this type for implicit typename, so the actual contents don't | |
4910 | matter. All that matters is whether a particular name is a type. */ | |
4911 | if (PARTIAL_INSTANTIATION_P (type)) | |
4912 | { | |
4913 | /* The fields set here must be kept in sync with those cleared | |
4914 | in begin_class_definition. */ | |
4915 | TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); | |
4916 | TYPE_FIELDS (type) = TYPE_FIELDS (pattern); | |
4917 | TYPE_METHODS (type) = TYPE_METHODS (pattern); | |
4918 | CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern); | |
4919 | /* Pretend that the type is complete, so that we will look | |
4920 | inside it during name lookup and such. */ | |
4921 | TYPE_SIZE (type) = integer_zero_node; | |
4922 | goto end; | |
4923 | } | |
4924 | ||
4925 | /* If we've recursively instantiated too many templates, stop. */ | |
4926 | if (! push_tinst_level (type)) | |
4927 | goto end; | |
4928 | ||
4929 | /* Now we're really doing the instantiation. Mark the type as in | |
4930 | the process of being defined. */ | |
4931 | TYPE_BEING_DEFINED (type) = 1; | |
4932 | ||
4933 | maybe_push_to_top_level (uses_template_parms (type)); | |
4c571114 | 4934 | |
73aad9b9 | 4935 | if (t) |
36a117a5 MM |
4936 | { |
4937 | /* This TYPE is actually a instantiation of of a partial | |
4938 | specialization. We replace the innermost set of ARGS with | |
4939 | the arguments appropriate for substitution. For example, | |
4940 | given: | |
4941 | ||
4942 | template <class T> struct S {}; | |
4943 | template <class T> struct S<T*> {}; | |
4944 | ||
4945 | and supposing that we are instantiating S<int*>, ARGS will | |
4946 | present be {int*} but we need {int}. */ | |
4947 | tree inner_args | |
4948 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
4949 | args); | |
4950 | ||
4951 | /* If there were multiple levels in ARGS, replacing the | |
4952 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
4953 | want, so we make a copy first. */ | |
4954 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
4955 | { | |
4956 | args = copy_node (args); | |
4957 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
4958 | } | |
4959 | else | |
4960 | args = inner_args; | |
4961 | } | |
8d019cef | 4962 | |
5566b478 MS |
4963 | if (flag_external_templates) |
4964 | { | |
4965 | if (flag_alt_external_templates) | |
4966 | { | |
4967 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
4968 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
4969 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4970 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4971 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4972 | } |
4973 | else | |
4974 | { | |
4975 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
4976 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
4977 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
4978 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4979 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4980 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4981 | } |
4982 | } | |
4983 | else | |
8d08fdba | 4984 | { |
5566b478 MS |
4985 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
4986 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
4987 | } |
4988 | ||
f7da6097 MS |
4989 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
4990 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
f7da6097 MS |
4991 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); |
4992 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
4993 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
4994 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
4995 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
4996 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
4997 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
4998 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
4999 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
5000 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
5001 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
5002 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
5003 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
5004 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
5005 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
5006 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
5007 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
5008 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
5009 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
5010 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
5011 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
eff71ab0 | 5012 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
5013 | if (ANON_AGGR_TYPE_P (pattern)) |
5014 | SET_ANON_AGGR_TYPE_P (type); | |
f7da6097 | 5015 | |
3fd71a52 MM |
5016 | if (TYPE_BINFO_BASETYPES (pattern)) |
5017 | { | |
5018 | tree base_list = NULL_TREE; | |
5019 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
5020 | int i; | |
5566b478 | 5021 | |
3fd71a52 MM |
5022 | /* Substitute into each of the bases to determine the actual |
5023 | basetypes. */ | |
5024 | for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) | |
5025 | { | |
5026 | tree base; | |
5027 | tree access; | |
5028 | tree pbase; | |
5566b478 | 5029 | |
3fd71a52 | 5030 | pbase = TREE_VEC_ELT (pbases, i); |
711734a9 | 5031 | |
3fd71a52 MM |
5032 | /* Substitue to figure out the base class. */ |
5033 | base = tsubst (BINFO_TYPE (pbase), args, | |
5034 | /*complain=*/1, NULL_TREE); | |
5035 | if (base == error_mark_node) | |
5036 | continue; | |
711734a9 | 5037 | |
3fd71a52 MM |
5038 | /* Calculate the correct access node. */ |
5039 | if (TREE_VIA_VIRTUAL (pbase)) | |
5040 | { | |
5041 | if (TREE_VIA_PUBLIC (pbase)) | |
5042 | access = access_public_virtual_node; | |
5043 | else if (TREE_VIA_PROTECTED (pbase)) | |
5044 | access = access_protected_virtual_node; | |
d6479fe7 | 5045 | else |
3fd71a52 MM |
5046 | access = access_private_virtual_node; |
5047 | } | |
5048 | else | |
5049 | { | |
5050 | if (TREE_VIA_PUBLIC (pbase)) | |
5051 | access = access_public_node; | |
5052 | else if (TREE_VIA_PROTECTED (pbase)) | |
5053 | access = access_protected_node; | |
d6479fe7 | 5054 | else |
3fd71a52 MM |
5055 | access = access_private_node; |
5056 | } | |
dfbcd65a | 5057 | |
3fd71a52 MM |
5058 | base_list = tree_cons (access, base, base_list); |
5059 | } | |
dfbcd65a | 5060 | |
3fd71a52 MM |
5061 | /* The list is now in reverse order; correct that. */ |
5062 | base_list = nreverse (base_list); | |
5063 | ||
5064 | /* Now call xref_basetypes to set up all the base-class | |
5065 | information. */ | |
5066 | xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE | |
5067 | ? (CLASSTYPE_DECLARED_CLASS (pattern) | |
5068 | ? class_type_node : record_type_node) | |
5069 | : union_type_node, | |
5070 | DECL_NAME (TYPE_NAME (pattern)), | |
5071 | type, | |
5072 | base_list); | |
5073 | } | |
5566b478 | 5074 | |
b74a0560 MM |
5075 | /* Now that our base classes are set up, enter the scope of the |
5076 | class, so that name lookups into base classes, etc. will work | |
5077 | corectly. This is precisely analagous to what we do in | |
5078 | begin_class_definition when defining an ordinary non-template | |
5079 | class. */ | |
5080 | pushclass (type, 1); | |
5081 | ||
5566b478 | 5082 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) |
8d08fdba | 5083 | { |
5566b478 | 5084 | tree tag = TREE_VALUE (t); |
36a117a5 MM |
5085 | tree name = TYPE_IDENTIFIER (tag); |
5086 | tree newtag; | |
5566b478 | 5087 | |
4393e105 | 5088 | newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE); |
61a127b3 | 5089 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
ed44da02 | 5090 | { |
486e4077 MM |
5091 | if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) |
5092 | /* Unfortunately, lookup_template_class sets | |
5093 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
5094 | instantiation (i.e., for the type of a member template | |
5095 | class nested within a template class.) This behavior is | |
5096 | required for maybe_process_partial_specialization to work | |
5097 | correctly, but is not accurate in this case; the TAG is not | |
5098 | an instantiation of anything. (The corresponding | |
5099 | TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ | |
5100 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; | |
5101 | ||
ed44da02 MM |
5102 | /* Now, we call pushtag to put this NEWTAG into the scope of |
5103 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
5104 | pushtag calling push_template_decl. We don't have to do | |
5105 | this for enums because it will already have been done in | |
5106 | tsubst_enum. */ | |
5107 | if (name) | |
5108 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
5109 | pushtag (name, newtag, /*globalize=*/0); | |
5110 | } | |
8d08fdba MS |
5111 | } |
5112 | ||
5566b478 MS |
5113 | /* Don't replace enum constants here. */ |
5114 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
8dd3f57a | 5115 | if (TREE_CODE (t) != CONST_DECL) |
5566b478 | 5116 | { |
ae58fa02 MM |
5117 | tree r; |
5118 | ||
5119 | /* The the file and line for this declaration, to assist in | |
5120 | error message reporting. Since we called push_tinst_level | |
5121 | above, we don't need to restore these. */ | |
5122 | lineno = DECL_SOURCE_LINE (t); | |
5123 | input_filename = DECL_SOURCE_FILE (t); | |
5124 | ||
4393e105 | 5125 | r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
5566b478 MS |
5126 | if (TREE_CODE (r) == VAR_DECL) |
5127 | { | |
fa8d6e85 MM |
5128 | tree init; |
5129 | ||
94c82a77 | 5130 | if (DECL_DEFINED_IN_CLASS_P (r)) |
fa8d6e85 MM |
5131 | init = tsubst_expr (DECL_INITIAL (t), args, |
5132 | /*complain=*/1, NULL_TREE); | |
5133 | else | |
5134 | init = NULL_TREE; | |
5135 | ||
5136 | finish_static_data_member_decl (r, init, | |
5137 | /*asmspec_tree=*/NULL_TREE, | |
5138 | /*need_pop=*/0, | |
5139 | /*flags=*/0); | |
5140 | ||
6ba89f8e MM |
5141 | if (DECL_DEFINED_IN_CLASS_P (r)) |
5142 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5566b478 | 5143 | } |
61a127b3 MM |
5144 | |
5145 | /* R will have a TREE_CHAIN if and only if it has already been | |
5146 | processed by finish_member_declaration. This can happen | |
5147 | if, for example, it is a TYPE_DECL for a class-scoped | |
5148 | ENUMERAL_TYPE; such a thing will already have been added to | |
5149 | the field list by tsubst_enum above. */ | |
5150 | if (!TREE_CHAIN (r)) | |
5151 | { | |
5152 | set_current_access_from_decl (r); | |
5153 | finish_member_declaration (r); | |
5154 | } | |
5566b478 | 5155 | } |
8d08fdba | 5156 | |
61a127b3 MM |
5157 | /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC) |
5158 | for this instantiation. */ | |
5159 | for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t)) | |
5160 | { | |
4393e105 | 5161 | tree r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
61a127b3 MM |
5162 | set_current_access_from_decl (r); |
5163 | finish_member_declaration (r); | |
5164 | } | |
8d08fdba | 5165 | |
2604412d JM |
5166 | /* Construct the DECL_FRIENDLIST for the new class type. */ |
5167 | typedecl = TYPE_MAIN_DECL (type); | |
5168 | for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)); | |
5169 | t != NULL_TREE; | |
5170 | t = TREE_CHAIN (t)) | |
f84b4be9 | 5171 | { |
2604412d | 5172 | tree friends; |
f84b4be9 | 5173 | |
2604412d JM |
5174 | for (friends = TREE_VALUE (t); |
5175 | friends != NULL_TREE; | |
5176 | friends = TREE_CHAIN (friends)) | |
0c0aac2f MM |
5177 | if (TREE_PURPOSE (friends) == error_mark_node) |
5178 | add_friend (type, | |
5179 | tsubst_friend_function (TREE_VALUE (friends), | |
5180 | args)); | |
5181 | else | |
5182 | add_friends (type, | |
5183 | tsubst_copy (TREE_PURPOSE (t), args, | |
5184 | /*complain=*/1, NULL_TREE), | |
5185 | tsubst (TREE_PURPOSE (friends), args, | |
5186 | /*complain=*/1, NULL_TREE)); | |
2604412d | 5187 | } |
5566b478 | 5188 | |
6757edfe MM |
5189 | for (t = CLASSTYPE_FRIEND_CLASSES (pattern); |
5190 | t != NULL_TREE; | |
5191 | t = TREE_CHAIN (t)) | |
5192 | { | |
5193 | tree friend_type = TREE_VALUE (t); | |
1aed5355 | 5194 | tree new_friend_type; |
6757edfe | 5195 | |
36a117a5 MM |
5196 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5197 | new_friend_type = tsubst_friend_class (friend_type, args); | |
5198 | else if (uses_template_parms (friend_type)) | |
4393e105 MM |
5199 | new_friend_type = tsubst (friend_type, args, /*complain=*/1, |
5200 | NULL_TREE); | |
36a117a5 | 5201 | else |
61fc8c9e MM |
5202 | { |
5203 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); | |
36a117a5 | 5204 | |
61fc8c9e MM |
5205 | /* The call to xref_tag_from_type does injection for friend |
5206 | classes. */ | |
5207 | push_nested_namespace (ns); | |
5208 | new_friend_type = | |
5209 | xref_tag_from_type (friend_type, NULL_TREE, 1); | |
5210 | pop_nested_namespace (ns); | |
5211 | } | |
1aed5355 MM |
5212 | |
5213 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5214 | /* Trick make_friend_class into realizing that the friend | |
5215 | we're adding is a template, not an ordinary class. It's | |
5216 | important that we use make_friend_class since it will | |
5217 | perform some error-checking and output cross-reference | |
5218 | information. */ | |
5219 | ++processing_template_decl; | |
5220 | ||
5221 | make_friend_class (type, new_friend_type); | |
fc378698 | 5222 | |
1aed5355 MM |
5223 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5224 | --processing_template_decl; | |
6757edfe | 5225 | } |
fc378698 | 5226 | |
2604412d JM |
5227 | for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
5228 | if (TREE_CODE (t) == FIELD_DECL) | |
5229 | { | |
5230 | TREE_TYPE (t) = complete_type (TREE_TYPE (t)); | |
5231 | require_complete_type (t); | |
5232 | } | |
5566b478 | 5233 | |
61a127b3 MM |
5234 | /* Set the file and line number information to whatever is given for |
5235 | the class itself. This puts error messages involving generated | |
5236 | implicit functions at a predictable point, and the same point | |
5237 | that would be used for non-template classes. */ | |
5238 | lineno = DECL_SOURCE_LINE (typedecl); | |
5239 | input_filename = DECL_SOURCE_FILE (typedecl); | |
5240 | ||
5241 | unreverse_member_declarations (type); | |
9f33663b | 5242 | finish_struct_1 (type); |
2604412d | 5243 | CLASSTYPE_GOT_SEMICOLON (type) = 1; |
e92cc029 | 5244 | |
5524676d JM |
5245 | /* Clear this now so repo_template_used is happy. */ |
5246 | TYPE_BEING_DEFINED (type) = 0; | |
2604412d | 5247 | repo_template_used (type); |
8d08fdba | 5248 | |
9188c363 MM |
5249 | /* Now that the class is complete, instantiate default arguments for |
5250 | any member functions. We don't do this earlier because the | |
5251 | default arguments may reference members of the class. */ | |
5252 | if (!PRIMARY_TEMPLATE_P (template)) | |
5253 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
5254 | if (TREE_CODE (t) == FUNCTION_DECL | |
5255 | /* Implicitly generated member functions will not have tmplate | |
5256 | information; they are not instantiations, but instead are | |
5257 | created "fresh" for each instantiation. */ | |
5258 | && DECL_TEMPLATE_INFO (t)) | |
5259 | tsubst_default_arguments (t); | |
5260 | ||
b74a0560 | 5261 | popclass (); |
5566b478 MS |
5262 | pop_from_top_level (); |
5263 | pop_tinst_level (); | |
5264 | ||
7ddedda4 MM |
5265 | end: |
5266 | pop_momentary (); | |
5267 | ||
5566b478 | 5268 | return type; |
8d08fdba MS |
5269 | } |
5270 | ||
5271 | static int | |
5272 | list_eq (t1, t2) | |
5273 | tree t1, t2; | |
5274 | { | |
5275 | if (t1 == NULL_TREE) | |
5276 | return t2 == NULL_TREE; | |
5277 | if (t2 == NULL_TREE) | |
5278 | return 0; | |
5279 | /* Don't care if one declares its arg const and the other doesn't -- the | |
5280 | main variant of the arg type is all that matters. */ | |
5281 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
5282 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
5283 | return 0; | |
5284 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
5285 | } | |
5286 | ||
00d3396f JM |
5287 | /* If arg is a non-type template parameter that does not depend on template |
5288 | arguments, fold it like we weren't in the body of a template. */ | |
5289 | ||
5290 | static tree | |
5291 | maybe_fold_nontype_arg (arg) | |
5292 | tree arg; | |
5293 | { | |
5294 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
5295 | && !uses_template_parms (arg)) | |
5296 | { | |
5297 | /* Sometimes, one of the args was an expression involving a | |
5298 | template constant parameter, like N - 1. Now that we've | |
5299 | tsubst'd, we might have something like 2 - 1. This will | |
5300 | confuse lookup_template_class, so we do constant folding | |
5301 | here. We have to unset processing_template_decl, to | |
5302 | fool build_expr_from_tree() into building an actual | |
5303 | tree. */ | |
5304 | ||
5305 | int saved_processing_template_decl = processing_template_decl; | |
5306 | processing_template_decl = 0; | |
5307 | arg = fold (build_expr_from_tree (arg)); | |
5308 | processing_template_decl = saved_processing_template_decl; | |
5309 | } | |
5310 | return arg; | |
5311 | } | |
5312 | ||
a221d52f | 5313 | /* Return the TREE_VEC with the arguments for the innermost template header, |
36a117a5 MM |
5314 | where ARGS is either that or the VEC of VECs for all the |
5315 | arguments. */ | |
a221d52f JM |
5316 | |
5317 | tree | |
36a117a5 | 5318 | innermost_args (args) |
a221d52f | 5319 | tree args; |
a221d52f | 5320 | { |
36a117a5 | 5321 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
a221d52f | 5322 | } |
f84b4be9 | 5323 | |
830bfa74 MM |
5324 | /* Substitute ARGS into the vector of template arguments T. */ |
5325 | ||
e9659ab0 | 5326 | static tree |
4393e105 | 5327 | tsubst_template_arg_vector (t, args, complain) |
830bfa74 MM |
5328 | tree t; |
5329 | tree args; | |
4393e105 | 5330 | int complain; |
830bfa74 MM |
5331 | { |
5332 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
5333 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5334 | ||
5335 | bzero ((char *) elts, len * sizeof (tree)); | |
5336 | ||
5337 | for (i = 0; i < len; i++) | |
5338 | { | |
5339 | if (TREE_VEC_ELT (t, i) != NULL_TREE | |
5340 | && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) | |
4393e105 MM |
5341 | elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), |
5342 | args, complain); | |
830bfa74 MM |
5343 | else |
5344 | elts[i] = maybe_fold_nontype_arg | |
4393e105 MM |
5345 | (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, |
5346 | NULL_TREE)); | |
830bfa74 MM |
5347 | |
5348 | if (elts[i] != TREE_VEC_ELT (t, i)) | |
5349 | need_new = 1; | |
5350 | } | |
5351 | ||
5352 | if (!need_new) | |
5353 | return t; | |
5354 | ||
7ddedda4 | 5355 | t = make_temp_vec (len); |
830bfa74 MM |
5356 | for (i = 0; i < len; i++) |
5357 | TREE_VEC_ELT (t, i) = elts[i]; | |
5358 | ||
5359 | return t; | |
5360 | } | |
5361 | ||
36a117a5 MM |
5362 | /* Return the result of substituting ARGS into the template parameters |
5363 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5364 | PARMS, then the result will contain n levels of PARMS. For | |
5365 | example, if PARMS is `template <class T> template <class U> | |
5366 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5367 | result will be `template <int*, double, class V>'. */ | |
5368 | ||
e9659ab0 | 5369 | static tree |
4393e105 | 5370 | tsubst_template_parms (parms, args, complain) |
36a117a5 MM |
5371 | tree parms; |
5372 | tree args; | |
4393e105 | 5373 | int complain; |
36a117a5 | 5374 | { |
f71f87f9 MM |
5375 | tree r = NULL_TREE; |
5376 | tree* new_parms; | |
36a117a5 MM |
5377 | |
5378 | for (new_parms = &r; | |
5379 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5380 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5381 | parms = TREE_CHAIN (parms)) | |
5382 | { | |
5383 | tree new_vec = | |
5384 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5385 | int i; | |
5386 | ||
5387 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5388 | { | |
5389 | tree default_value = | |
5390 | TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5391 | tree parm_decl = | |
5392 | TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5393 | ||
5394 | TREE_VEC_ELT (new_vec, i) | |
4393e105 MM |
5395 | = build_tree_list (tsubst (default_value, args, complain, |
5396 | NULL_TREE), | |
5397 | tsubst (parm_decl, args, complain, | |
5398 | NULL_TREE)); | |
36a117a5 MM |
5399 | } |
5400 | ||
5401 | *new_parms = | |
5402 | tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms) | |
5403 | - TMPL_ARGS_DEPTH (args))), | |
5404 | new_vec, NULL_TREE); | |
5405 | } | |
5406 | ||
5407 | return r; | |
5408 | } | |
5409 | ||
ed44da02 MM |
5410 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
5411 | type T. If T is not an aggregate or enumeration type, it is | |
5412 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
5413 | ENTERING_SCOPE is non-zero, T is the context for a template which | |
5414 | we are presently tsubst'ing. Return the subsituted value. */ | |
36a117a5 | 5415 | |
e9659ab0 | 5416 | static tree |
4393e105 | 5417 | tsubst_aggr_type (t, args, complain, in_decl, entering_scope) |
36a117a5 MM |
5418 | tree t; |
5419 | tree args; | |
4393e105 | 5420 | int complain; |
36a117a5 MM |
5421 | tree in_decl; |
5422 | int entering_scope; | |
5423 | { | |
5424 | if (t == NULL_TREE) | |
5425 | return NULL_TREE; | |
5426 | ||
5427 | switch (TREE_CODE (t)) | |
5428 | { | |
5429 | case RECORD_TYPE: | |
5430 | if (TYPE_PTRMEMFUNC_P (t)) | |
5431 | { | |
5432 | tree r = build_ptrmemfunc_type | |
4393e105 | 5433 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl)); |
adecb3f4 MM |
5434 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), |
5435 | complain); | |
36a117a5 MM |
5436 | } |
5437 | ||
5438 | /* else fall through */ | |
ed44da02 | 5439 | case ENUMERAL_TYPE: |
36a117a5 | 5440 | case UNION_TYPE: |
5db698f6 | 5441 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
5442 | { |
5443 | tree argvec; | |
5444 | tree context; | |
5445 | tree r; | |
5446 | ||
5447 | /* First, determine the context for the type we are looking | |
5448 | up. */ | |
5449 | if (TYPE_CONTEXT (t) != NULL_TREE) | |
5450 | context = tsubst_aggr_type (TYPE_CONTEXT (t), args, | |
4393e105 | 5451 | complain, |
36a117a5 MM |
5452 | in_decl, /*entering_scope=*/1); |
5453 | else | |
5454 | context = NULL_TREE; | |
5455 | ||
5456 | /* Then, figure out what arguments are appropriate for the | |
5457 | type we are trying to find. For example, given: | |
5458 | ||
5459 | template <class T> struct S; | |
5460 | template <class T, class U> void f(T, U) { S<U> su; } | |
5461 | ||
5462 | and supposing that we are instantiating f<int, double>, | |
5463 | then our ARGS will be {int, double}, but, when looking up | |
5464 | S we only want {double}. */ | |
7ddedda4 | 5465 | push_momentary (); |
4393e105 MM |
5466 | argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, |
5467 | complain); | |
36a117a5 MM |
5468 | |
5469 | r = lookup_template_class (t, argvec, in_decl, context, | |
5470 | entering_scope); | |
7ddedda4 | 5471 | pop_momentary (); |
36a117a5 | 5472 | |
adecb3f4 MM |
5473 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), |
5474 | complain); | |
36a117a5 MM |
5475 | } |
5476 | else | |
5477 | /* This is not a template type, so there's nothing to do. */ | |
5478 | return t; | |
5479 | ||
5480 | default: | |
4393e105 | 5481 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
5482 | } |
5483 | } | |
5484 | ||
9188c363 MM |
5485 | /* Substitute into the default argument ARG (a default argument for |
5486 | FN), which has the indicated TYPE. */ | |
5487 | ||
5488 | tree | |
5489 | tsubst_default_argument (fn, type, arg) | |
5490 | tree fn; | |
5491 | tree type; | |
5492 | tree arg; | |
5493 | { | |
5494 | /* This default argument came from a template. Instantiate the | |
5495 | default argument here, not in tsubst. In the case of | |
5496 | something like: | |
5497 | ||
5498 | template <class T> | |
5499 | struct S { | |
5500 | static T t(); | |
5501 | void f(T = t()); | |
5502 | }; | |
5503 | ||
5504 | we must be careful to do name lookup in the scope of S<T>, | |
5505 | rather than in the current class. */ | |
5506 | if (DECL_CLASS_SCOPE_P (fn)) | |
5507 | pushclass (DECL_REAL_CONTEXT (fn), 2); | |
5508 | ||
5509 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE); | |
5510 | ||
5511 | if (DECL_CLASS_SCOPE_P (fn)) | |
5512 | popclass (); | |
5513 | ||
5514 | /* Make sure the default argument is reasonable. */ | |
5515 | arg = check_default_argument (type, arg); | |
5516 | ||
5517 | return arg; | |
5518 | } | |
5519 | ||
5520 | /* Substitute into all the default arguments for FN. */ | |
5521 | ||
5522 | static void | |
5523 | tsubst_default_arguments (fn) | |
5524 | tree fn; | |
5525 | { | |
5526 | tree arg; | |
5527 | tree tmpl_args; | |
5528 | ||
5529 | tmpl_args = DECL_TI_ARGS (fn); | |
5530 | ||
5531 | /* If this function is not yet instantiated, we certainly don't need | |
5532 | its default arguments. */ | |
5533 | if (uses_template_parms (tmpl_args)) | |
5534 | return; | |
5535 | ||
5536 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
5537 | arg; | |
5538 | arg = TREE_CHAIN (arg)) | |
5539 | if (TREE_PURPOSE (arg)) | |
5540 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, | |
5541 | TREE_VALUE (arg), | |
5542 | TREE_PURPOSE (arg)); | |
5543 | } | |
5544 | ||
ae58fa02 MM |
5545 | /* Substitute the ARGS into the T, which is a _DECL. TYPE is the |
5546 | (already computed) substitution of ARGS into TREE_TYPE (T), if | |
5547 | appropriate. Return the result of the substitution. IN_DECL is as | |
5548 | for tsubst. */ | |
00d3396f | 5549 | |
e9659ab0 | 5550 | static tree |
ae58fa02 MM |
5551 | tsubst_decl (t, args, type, in_decl) |
5552 | tree t; | |
5553 | tree args; | |
5554 | tree type; | |
8d08fdba MS |
5555 | tree in_decl; |
5556 | { | |
ae58fa02 MM |
5557 | int saved_lineno; |
5558 | char* saved_filename; | |
b370501f | 5559 | tree r = NULL_TREE; |
830bfa74 | 5560 | |
ae58fa02 MM |
5561 | /* Set the filename and linenumber to improve error-reporting. */ |
5562 | saved_lineno = lineno; | |
5563 | saved_filename = input_filename; | |
5564 | lineno = DECL_SOURCE_LINE (t); | |
5565 | input_filename = DECL_SOURCE_FILE (t); | |
b7484fbe | 5566 | |
8d08fdba MS |
5567 | switch (TREE_CODE (t)) |
5568 | { | |
98c1c668 JM |
5569 | case TEMPLATE_DECL: |
5570 | { | |
5571 | /* We can get here when processing a member template function | |
5572 | of a template class. */ | |
98c1c668 | 5573 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 5574 | tree spec; |
db2767b6 | 5575 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); |
98c1c668 | 5576 | |
db2767b6 MM |
5577 | if (!is_template_template_parm) |
5578 | { | |
36a117a5 MM |
5579 | /* We might already have an instance of this template. |
5580 | The ARGS are for the surrounding class type, so the | |
5581 | full args contain the tsubst'd args for the context, | |
5582 | plus the innermost args from the template decl. */ | |
5583 | tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
5584 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
5585 | : DECL_TI_ARGS (DECL_RESULT (t)); | |
7ddedda4 MM |
5586 | tree full_args; |
5587 | ||
5588 | push_momentary (); | |
4393e105 MM |
5589 | full_args = tsubst_template_arg_vector (tmpl_args, args, |
5590 | /*complain=*/1); | |
36a117a5 MM |
5591 | |
5592 | /* tsubst_template_arg_vector doesn't copy the vector if | |
5593 | nothing changed. But, *something* should have | |
5594 | changed. */ | |
5595 | my_friendly_assert (full_args != tmpl_args, 0); | |
5596 | ||
5597 | spec = retrieve_specialization (t, full_args); | |
7ddedda4 | 5598 | pop_momentary (); |
db2767b6 | 5599 | if (spec != NULL_TREE) |
ae58fa02 MM |
5600 | { |
5601 | r = spec; | |
5602 | break; | |
5603 | } | |
db2767b6 | 5604 | } |
98c1c668 JM |
5605 | |
5606 | /* Make a new template decl. It will be similar to the | |
5607 | original, but will record the current template arguments. | |
5608 | We also create a new function declaration, which is just | |
5609 | like the old one, but points to this new template, rather | |
5610 | than the old one. */ | |
ae58fa02 MM |
5611 | r = copy_node (t); |
5612 | copy_lang_decl (r); | |
5613 | my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); | |
5614 | TREE_CHAIN (r) = NULL_TREE; | |
db2767b6 MM |
5615 | |
5616 | if (is_template_template_parm) | |
5617 | { | |
4393e105 | 5618 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5619 | DECL_RESULT (r) = new_decl; |
5620 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5621 | break; | |
db2767b6 MM |
5622 | } |
5623 | ||
ae58fa02 | 5624 | DECL_CONTEXT (r) |
4393e105 MM |
5625 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, |
5626 | in_decl, /*entering_scope=*/1); | |
ae58fa02 | 5627 | DECL_CLASS_CONTEXT (r) |
4393e105 MM |
5628 | = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5629 | /*complain=*/1, in_decl, | |
5630 | /*entering_scope=*/1); | |
ae58fa02 | 5631 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
5632 | |
5633 | if (TREE_CODE (decl) == TYPE_DECL) | |
5634 | { | |
4393e105 MM |
5635 | tree new_type = tsubst (TREE_TYPE (t), args, |
5636 | /*complain=*/1, in_decl); | |
ae58fa02 MM |
5637 | TREE_TYPE (r) = new_type; |
5638 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
5639 | DECL_RESULT (r) = TYPE_MAIN_DECL (new_type); | |
5640 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); | |
93cdc044 JM |
5641 | } |
5642 | else | |
5643 | { | |
4393e105 | 5644 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5645 | DECL_RESULT (r) = new_decl; |
5646 | DECL_TI_TEMPLATE (new_decl) = r; | |
5647 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5648 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
5649 | } |
5650 | ||
ae58fa02 MM |
5651 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5652 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
5653 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
5654 | |
5655 | /* The template parameters for this new template are all the | |
5656 | template parameters for the old template, except the | |
5657 | outermost level of parameters. */ | |
ae58fa02 | 5658 | DECL_TEMPLATE_PARMS (r) |
4393e105 MM |
5659 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
5660 | /*complain=*/1); | |
98c1c668 | 5661 | |
93cdc044 | 5662 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 5663 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 5664 | |
8d019cef | 5665 | /* We don't partially instantiate partial specializations. */ |
93cdc044 | 5666 | if (TREE_CODE (decl) == TYPE_DECL) |
ae58fa02 | 5667 | break; |
93cdc044 | 5668 | |
75650646 MM |
5669 | for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t); |
5670 | spec != NULL_TREE; | |
5671 | spec = TREE_CHAIN (spec)) | |
5672 | { | |
5673 | /* It helps to consider example here. Consider: | |
5674 | ||
5675 | template <class T> | |
5676 | struct S { | |
5677 | template <class U> | |
5678 | void f(U u); | |
5679 | ||
5680 | template <> | |
5681 | void f(T* t) {} | |
5682 | }; | |
5683 | ||
5684 | Now, for example, we are instantiating S<int>::f(U u). | |
5685 | We want to make a template: | |
5686 | ||
5687 | template <class U> | |
5688 | void S<int>::f(U); | |
5689 | ||
5690 | It will have a specialization, for the case U = int*, of | |
5691 | the form: | |
5692 | ||
5693 | template <> | |
5694 | void S<int>::f<int*>(int*); | |
5695 | ||
5696 | This specialization will be an instantiation of | |
5697 | the specialization given in the declaration of S, with | |
5698 | argument list int*. */ | |
5699 | ||
5700 | tree fn = TREE_VALUE (spec); | |
5701 | tree spec_args; | |
5702 | tree new_fn; | |
5703 | ||
5704 | if (!DECL_TEMPLATE_SPECIALIZATION (fn)) | |
5705 | /* Instantiations are on the same list, but they're of | |
5706 | no concern to us. */ | |
5707 | continue; | |
5708 | ||
36a117a5 MM |
5709 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
5710 | /* A full specialization. There's no need to record | |
5711 | that here. */ | |
5712 | continue; | |
5713 | ||
4393e105 MM |
5714 | spec_args = tsubst (DECL_TI_ARGS (fn), args, |
5715 | /*complain=*/1, in_decl); | |
36a117a5 | 5716 | new_fn = tsubst (DECL_RESULT (most_general_template (fn)), |
4393e105 | 5717 | spec_args, /*complain=*/1, in_decl); |
36a117a5 | 5718 | DECL_TI_TEMPLATE (new_fn) = fn; |
ae58fa02 | 5719 | register_specialization (new_fn, r, |
36a117a5 | 5720 | innermost_args (spec_args)); |
75650646 MM |
5721 | } |
5722 | ||
5723 | /* Record this partial instantiation. */ | |
ae58fa02 MM |
5724 | register_specialization (r, t, |
5725 | DECL_TI_ARGS (DECL_RESULT (r))); | |
75650646 | 5726 | |
98c1c668 | 5727 | } |
ae58fa02 | 5728 | break; |
8d08fdba MS |
5729 | |
5730 | case FUNCTION_DECL: | |
5731 | { | |
386b8a85 | 5732 | tree ctx; |
87603ed0 | 5733 | tree argvec = NULL_TREE; |
cf38f48a | 5734 | tree *friends; |
36a117a5 | 5735 | tree gen_tmpl; |
5566b478 | 5736 | int member; |
d8c4447d MM |
5737 | int args_depth; |
5738 | int parms_depth; | |
5566b478 | 5739 | |
36a117a5 MM |
5740 | /* Nobody should be tsubst'ing into non-template functions. */ |
5741 | my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5742 | ||
5743 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
5744 | { | |
5745 | tree spec; | |
5746 | ||
7ddedda4 MM |
5747 | /* Allocate template arguments on the momentary obstack, |
5748 | in case we don't need to keep them. */ | |
5749 | push_momentary (); | |
5750 | ||
36a117a5 MM |
5751 | /* Calculate the most general template of which R is a |
5752 | specialization, and the complete set of arguments used to | |
5753 | specialize R. */ | |
5754 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
7ddedda4 MM |
5755 | argvec |
5756 | = tsubst_template_arg_vector (DECL_TI_ARGS | |
5757 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
4393e105 | 5758 | args, /*complain=*/1); |
36a117a5 MM |
5759 | |
5760 | /* Check to see if we already have this specialization. */ | |
5761 | spec = retrieve_specialization (gen_tmpl, argvec); | |
7ddedda4 | 5762 | |
36a117a5 | 5763 | if (spec) |
ae58fa02 MM |
5764 | { |
5765 | r = spec; | |
7ddedda4 | 5766 | pop_momentary (); |
ae58fa02 MM |
5767 | break; |
5768 | } | |
d8c4447d | 5769 | |
7ddedda4 MM |
5770 | pop_momentary (); |
5771 | ||
d8c4447d MM |
5772 | /* Here, we deal with the peculiar case: |
5773 | ||
5774 | template <class T> struct S { | |
5775 | template <class U> friend void f(); | |
5776 | }; | |
5777 | template <class U> friend void f() {} | |
5778 | template S<int>; | |
5779 | template void f<double>(); | |
5780 | ||
5781 | Here, the ARGS for the instantiation of will be {int, | |
5782 | double}. But, we only need as many ARGS as there are | |
5783 | levels of template parameters in CODE_PATTERN. We are | |
5784 | careful not to get fooled into reducing the ARGS in | |
5785 | situations like: | |
5786 | ||
5787 | template <class T> struct S { template <class U> void f(U); } | |
5788 | template <class T> template <> void S<T>::f(int) {} | |
5789 | ||
5790 | which we can spot because the pattern will be a | |
5791 | specialization in this case. */ | |
5792 | args_depth = TMPL_ARGS_DEPTH (args); | |
5793 | parms_depth = | |
5794 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
5795 | if (args_depth > parms_depth | |
5796 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
5797 | { | |
5798 | my_friendly_assert (DECL_FRIEND_P (t), 0); | |
5799 | ||
5800 | if (parms_depth > 1) | |
5801 | { | |
5802 | int i; | |
5803 | ||
5804 | args = make_temp_vec (parms_depth); | |
5805 | for (i = 0; i < parms_depth; ++i) | |
5806 | TREE_VEC_ELT (args, i) = | |
5807 | TREE_VEC_ELT (args, i + (args_depth - parms_depth)); | |
5808 | } | |
5809 | else | |
5810 | args = TREE_VEC_ELT (args, args_depth - parms_depth); | |
5811 | } | |
36a117a5 MM |
5812 | } |
5813 | else | |
5814 | { | |
5815 | /* This special case arises when we have something like this: | |
5816 | ||
5817 | template <class T> struct S { | |
5818 | friend void f<int>(int, double); | |
5819 | }; | |
5820 | ||
5821 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
39c01e4c MM |
5822 | will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are |
5823 | being called from tsubst_friend_function, and we want | |
5824 | only to create a new decl (R) with appropriate types so | |
5825 | that we can call determine_specialization. */ | |
5826 | my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5827 | == LOOKUP_EXPR) | |
5828 | || (TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5829 | == IDENTIFIER_NODE), 0); | |
36a117a5 MM |
5830 | gen_tmpl = NULL_TREE; |
5831 | } | |
5832 | ||
6eb3bb27 | 5833 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 5834 | { |
5566b478 MS |
5835 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
5836 | member = 2; | |
5837 | else | |
5838 | member = 1; | |
4393e105 MM |
5839 | ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5840 | /*complain=*/1, t, | |
36a117a5 | 5841 | /*entering_scope=*/1); |
5566b478 MS |
5842 | } |
5843 | else | |
5844 | { | |
5845 | member = 0; | |
5846 | ctx = NULL_TREE; | |
5566b478 | 5847 | } |
4393e105 | 5848 | type = tsubst (type, args, /*complain=*/1, in_decl); |
8d08fdba | 5849 | |
5566b478 MS |
5850 | /* We do NOT check for matching decls pushed separately at this |
5851 | point, as they may not represent instantiations of this | |
5852 | template, and in any case are considered separate under the | |
312e7d50 | 5853 | discrete model. Instead, see add_maybe_template. */ |
5566b478 MS |
5854 | |
5855 | r = copy_node (t); | |
5856 | copy_lang_decl (r); | |
e1467ff2 | 5857 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 MS |
5858 | TREE_TYPE (r) = type; |
5859 | ||
5860 | DECL_CONTEXT (r) | |
4393e105 MM |
5861 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, t, |
5862 | /*entering_scope=*/1); | |
5566b478 MS |
5863 | DECL_CLASS_CONTEXT (r) = ctx; |
5864 | ||
4c571114 | 5865 | if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r))) |
f181d4ae MM |
5866 | /* Type-conversion operator. Reconstruct the name, in |
5867 | case it's the name of one of the template's parameters. */ | |
5868 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
5566b478 | 5869 | |
4393e105 MM |
5870 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
5871 | /*complain=*/1, t); | |
711734a9 JM |
5872 | DECL_MAIN_VARIANT (r) = r; |
5873 | DECL_RESULT (r) = NULL_TREE; | |
711734a9 JM |
5874 | |
5875 | TREE_STATIC (r) = 0; | |
5876 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
5877 | DECL_EXTERNAL (r) = 1; | |
5878 | DECL_INTERFACE_KNOWN (r) = 0; | |
5879 | DECL_DEFER_OUTPUT (r) = 0; | |
5880 | TREE_CHAIN (r) = NULL_TREE; | |
5881 | DECL_PENDING_INLINE_INFO (r) = 0; | |
5882 | TREE_USED (r) = 0; | |
5883 | ||
36a117a5 MM |
5884 | /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled |
5885 | name. There's no need to do this in the special friend | |
5886 | case mentioned above where GEN_TMPL is NULL. */ | |
5887 | if (gen_tmpl) | |
386b8a85 | 5888 | { |
36a117a5 | 5889 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 5890 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 MM |
5891 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5892 | register_specialization (r, gen_tmpl, argvec); | |
5893 | ||
5894 | /* Set the mangled name for R. */ | |
5895 | if (DECL_DESTRUCTOR_P (t)) | |
5896 | DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx); | |
5897 | else | |
386b8a85 | 5898 | { |
36a117a5 MM |
5899 | /* Instantiations of template functions must be mangled |
5900 | specially, in order to conform to 14.5.5.1 | |
5901 | [temp.over.link]. */ | |
5902 | tree tmpl = DECL_TI_TEMPLATE (t); | |
386b8a85 | 5903 | |
36a117a5 MM |
5904 | /* TMPL will be NULL if this is a specialization of a |
5905 | member function of a template class. */ | |
5906 | if (name_mangling_version < 1 | |
5907 | || tmpl == NULL_TREE | |
5908 | || (member && !is_member_template (tmpl) | |
5909 | && !DECL_TEMPLATE_INFO (tmpl))) | |
5910 | set_mangled_name_for_decl (r); | |
75650646 | 5911 | else |
36a117a5 | 5912 | set_mangled_name_for_template_decl (r); |
386b8a85 | 5913 | } |
36a117a5 MM |
5914 | |
5915 | DECL_RTL (r) = 0; | |
5916 | make_decl_rtl (r, NULL_PTR, 1); | |
5917 | ||
5918 | /* Like grokfndecl. If we don't do this, pushdecl will | |
5919 | mess up our TREE_CHAIN because it doesn't find a | |
5920 | previous decl. Sigh. */ | |
5921 | if (member | |
c36ae80f | 5922 | && ! uses_template_parms (r) |
36a117a5 MM |
5923 | && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) |
5924 | == NULL_TREE)) | |
5925 | SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r); | |
9188c363 MM |
5926 | |
5927 | /* We're not supposed to instantiate default arguments | |
5928 | until they are called, for a template. But, for a | |
5929 | declaration like: | |
5930 | ||
5931 | template <class T> void f () | |
5932 | { extern void g(int i = T()); } | |
5933 | ||
5934 | we should do the substitution when the template is | |
5935 | instantiated. We handle the member function case in | |
5936 | instantiate_class_template since the default arguments | |
5937 | might refer to other members of the class. */ | |
5938 | if (!member | |
5939 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
5940 | && !uses_template_parms (argvec)) | |
5941 | tsubst_default_arguments (r); | |
386b8a85 | 5942 | } |
f181d4ae | 5943 | |
cf38f48a MM |
5944 | /* Copy the list of befriending classes. */ |
5945 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
5946 | *friends; | |
5947 | friends = &TREE_CHAIN (*friends)) | |
5948 | { | |
5949 | *friends = copy_node (*friends); | |
5950 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
5951 | args, /*complain=*/1, | |
5952 | in_decl); | |
5953 | } | |
5954 | ||
f181d4ae MM |
5955 | if (DECL_CONSTRUCTOR_P (r)) |
5956 | { | |
5957 | maybe_retrofit_in_chrg (r); | |
5958 | grok_ctor_properties (ctx, r); | |
5959 | } | |
f71f87f9 | 5960 | else if (DECL_OVERLOADED_OPERATOR_P (r)) |
f181d4ae | 5961 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); |
8d08fdba | 5962 | } |
ae58fa02 | 5963 | break; |
8d08fdba MS |
5964 | |
5965 | case PARM_DECL: | |
5966 | { | |
ae58fa02 | 5967 | r = copy_node (t); |
5566b478 | 5968 | TREE_TYPE (r) = type; |
1b8899d1 MM |
5969 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); |
5970 | ||
db2767b6 MM |
5971 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
5972 | DECL_INITIAL (r) = TREE_TYPE (r); | |
5973 | else | |
4393e105 MM |
5974 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, |
5975 | /*complain=*/1, in_decl); | |
db2767b6 | 5976 | |
5566b478 | 5977 | DECL_CONTEXT (r) = NULL_TREE; |
fa56377d JJ |
5978 | if (PROMOTE_PROTOTYPES |
5979 | && (TREE_CODE (type) == INTEGER_TYPE | |
5980 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
f83b0cb6 JM |
5981 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
5982 | DECL_ARG_TYPE (r) = integer_type_node; | |
8d08fdba | 5983 | if (TREE_CHAIN (t)) |
4393e105 MM |
5984 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
5985 | /*complain=*/1, TREE_CHAIN (t)); | |
8d08fdba | 5986 | } |
ae58fa02 | 5987 | break; |
8d08fdba | 5988 | |
5566b478 MS |
5989 | case FIELD_DECL: |
5990 | { | |
ae58fa02 | 5991 | r = copy_node (t); |
5566b478 | 5992 | copy_lang_decl (r); |
1b8899d1 MM |
5993 | TREE_TYPE (r) = type; |
5994 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); | |
5995 | ||
5996 | /* We don't have to set DECL_CONTEXT here; it is set by | |
5997 | finish_member_declaration. */ | |
4393e105 MM |
5998 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, |
5999 | /*complain=*/1, in_decl); | |
5566b478 | 6000 | TREE_CHAIN (r) = NULL_TREE; |
4393e105 | 6001 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 6002 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 6003 | } |
ae58fa02 | 6004 | break; |
5566b478 MS |
6005 | |
6006 | case USING_DECL: | |
6007 | { | |
ae58fa02 | 6008 | r = copy_node (t); |
5566b478 | 6009 | DECL_INITIAL (r) |
4393e105 | 6010 | = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl); |
5566b478 | 6011 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 6012 | } |
ae58fa02 | 6013 | break; |
5566b478 | 6014 | |
9188c363 MM |
6015 | case TYPE_DECL: |
6016 | if (DECL_IMPLICIT_TYPEDEF_P (t)) | |
6017 | { | |
6018 | /* For an implicit typedef, we just want the implicit | |
6019 | typedef for the tsubst'd type. We've already got the | |
6020 | tsubst'd type, as TYPE, so we just need it's associated | |
6021 | declaration. */ | |
6022 | r = TYPE_NAME (type); | |
6023 | break; | |
6024 | } | |
6025 | else if (!DECL_LANG_SPECIFIC (t)) | |
6026 | { | |
6027 | /* For a template type parameter, we don't have to do | |
6028 | anything special. */ | |
6029 | r= TYPE_NAME (type); | |
6030 | break; | |
6031 | } | |
6032 | ||
6033 | /* Fall through. */ | |
6034 | ||
5566b478 MS |
6035 | case VAR_DECL: |
6036 | { | |
36a117a5 MM |
6037 | tree argvec; |
6038 | tree gen_tmpl; | |
6039 | tree spec; | |
6040 | tree tmpl; | |
9188c363 MM |
6041 | tree ctx; |
6042 | ||
8dd3f57a MM |
6043 | /* Nobody should be tsubst'ing into non-template variables. */ |
6044 | my_friendly_assert (DECL_LANG_SPECIFIC (t) | |
6045 | && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5566b478 | 6046 | |
9188c363 MM |
6047 | if (TYPE_P (DECL_CONTEXT (t))) |
6048 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, | |
6049 | /*complain=*/1, | |
6050 | in_decl, /*entering_scope=*/1); | |
6051 | else | |
6052 | /* Subsequent calls to pushdecl will fill this in. */ | |
6053 | ctx = NULL_TREE; | |
6054 | ||
36a117a5 MM |
6055 | /* Check to see if we already have this specialization. */ |
6056 | tmpl = DECL_TI_TEMPLATE (t); | |
6057 | gen_tmpl = most_general_template (tmpl); | |
4393e105 | 6058 | argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl); |
9188c363 MM |
6059 | if (ctx) |
6060 | spec = retrieve_specialization (gen_tmpl, argvec); | |
6061 | else | |
6062 | spec = retrieve_local_specialization (gen_tmpl, | |
6063 | current_function_decl); | |
6064 | ||
36a117a5 | 6065 | if (spec) |
ae58fa02 MM |
6066 | { |
6067 | r = spec; | |
6068 | break; | |
6069 | } | |
5566b478 | 6070 | |
e0942dcd MM |
6071 | /* This declaration is going to have to be around for a while, |
6072 | so me make sure it is on a saveable obstack. */ | |
6073 | push_obstacks_nochange (); | |
6074 | saveable_allocation (); | |
5566b478 | 6075 | r = copy_node (t); |
e0942dcd MM |
6076 | pop_obstacks (); |
6077 | ||
5566b478 | 6078 | TREE_TYPE (r) = type; |
1b8899d1 | 6079 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); |
5566b478 | 6080 | DECL_CONTEXT (r) = ctx; |
d11ad92e MS |
6081 | |
6082 | /* Don't try to expand the initializer until someone tries to use | |
6083 | this variable; otherwise we run into circular dependencies. */ | |
6084 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 MS |
6085 | DECL_RTL (r) = 0; |
6086 | DECL_SIZE (r) = 0; | |
36a117a5 MM |
6087 | copy_lang_decl (r); |
6088 | DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r); | |
5566b478 | 6089 | |
e0942dcd MM |
6090 | /* Even if the original location is out of scope, the newly |
6091 | substituted one is not. */ | |
6092 | if (TREE_CODE (r) == VAR_DECL) | |
6093 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
6094 | ||
fa8d6e85 MM |
6095 | /* A static data member declaration is always marked external |
6096 | when it is declared in-class, even if an initializer is | |
6097 | present. We mimic the non-template processing here. */ | |
e0942dcd MM |
6098 | if (ctx) |
6099 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 6100 | |
e1b3e07d | 6101 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); |
36a117a5 | 6102 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
9188c363 MM |
6103 | if (ctx) |
6104 | register_specialization (r, gen_tmpl, argvec); | |
6105 | else | |
6106 | register_local_specialization (r, gen_tmpl, | |
6107 | current_function_decl); | |
5566b478 | 6108 | |
5566b478 | 6109 | TREE_CHAIN (r) = NULL_TREE; |
9188c363 | 6110 | if (TREE_CODE (r) == VAR_DECL && TREE_CODE (type) == VOID_TYPE) |
8251199e | 6111 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 6112 | } |
ae58fa02 | 6113 | break; |
5566b478 | 6114 | |
ae58fa02 MM |
6115 | default: |
6116 | my_friendly_abort (0); | |
6117 | } | |
6118 | ||
6119 | /* Restore the file and line information. */ | |
6120 | lineno = saved_lineno; | |
6121 | input_filename = saved_filename; | |
6122 | ||
6123 | return r; | |
6124 | } | |
6125 | ||
cabc336a MM |
6126 | /* Substitue into the ARG_TYPES of a function type. */ |
6127 | ||
e9659ab0 | 6128 | static tree |
4393e105 | 6129 | tsubst_arg_types (arg_types, args, complain, in_decl) |
cabc336a MM |
6130 | tree arg_types; |
6131 | tree args; | |
4393e105 | 6132 | int complain; |
cabc336a MM |
6133 | tree in_decl; |
6134 | { | |
6135 | tree remaining_arg_types; | |
cabc336a MM |
6136 | tree type; |
6137 | ||
6138 | if (!arg_types || arg_types == void_list_node) | |
6139 | return arg_types; | |
6140 | ||
6141 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
4393e105 MM |
6142 | args, complain, in_decl); |
6143 | if (remaining_arg_types == error_mark_node) | |
6144 | return error_mark_node; | |
6145 | ||
6146 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
6147 | if (type == error_mark_node) | |
6148 | return error_mark_node; | |
cabc336a | 6149 | |
4393e105 MM |
6150 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
6151 | top-level qualifiers as required. */ | |
6152 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a MM |
6153 | |
6154 | /* Note that we do not substitute into default arguments here. The | |
6155 | standard mandates that they be instantiated only when needed, | |
6156 | which is done in build_over_call. */ | |
51632249 JM |
6157 | return hash_tree_cons (TREE_PURPOSE (arg_types), type, |
6158 | remaining_arg_types); | |
cabc336a MM |
6159 | |
6160 | } | |
6161 | ||
4393e105 MM |
6162 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
6163 | *not* handle the exception-specification for FNTYPE, because the | |
6164 | initial substitution of explicitly provided template parameters | |
6165 | during argument deduction forbids substitution into the | |
6166 | exception-specification: | |
6167 | ||
6168 | [temp.deduct] | |
6169 | ||
6170 | All references in the function type of the function template to the | |
6171 | corresponding template parameters are replaced by the specified tem- | |
6172 | plate argument values. If a substitution in a template parameter or | |
6173 | in the function type of the function template results in an invalid | |
6174 | type, type deduction fails. [Note: The equivalent substitution in | |
6175 | exception specifications is done only when the function is instanti- | |
6176 | ated, at which point a program is ill-formed if the substitution | |
6177 | results in an invalid type.] */ | |
6178 | ||
6179 | static tree | |
6180 | tsubst_function_type (t, args, complain, in_decl) | |
6181 | tree t; | |
6182 | tree args; | |
6183 | int complain; | |
6184 | tree in_decl; | |
6185 | { | |
6186 | tree return_type; | |
6187 | tree arg_types; | |
6188 | tree fntype; | |
6189 | ||
8dd3f57a | 6190 | /* The TYPE_CONTEXT is not used for function/method types. */ |
4393e105 MM |
6191 | my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); |
6192 | ||
6193 | /* Substitue the return type. */ | |
6194 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6195 | if (return_type == error_mark_node) | |
6196 | return error_mark_node; | |
6197 | ||
6198 | /* Substitue the argument types. */ | |
6199 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, | |
6200 | complain, in_decl); | |
6201 | if (arg_types == error_mark_node) | |
6202 | return error_mark_node; | |
6203 | ||
6204 | /* Construct a new type node and return it. */ | |
6205 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
6206 | fntype = build_function_type (return_type, arg_types); | |
6207 | else | |
6208 | { | |
6209 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
6210 | if (! IS_AGGR_TYPE (r)) | |
6211 | { | |
6212 | /* [temp.deduct] | |
6213 | ||
6214 | Type deduction may fail for any of the following | |
6215 | reasons: | |
6216 | ||
6217 | -- Attempting to create "pointer to member of T" when T | |
6218 | is not a class type. */ | |
6219 | if (complain) | |
6220 | cp_error ("creating pointer to member function of non-class type `%T'", | |
6221 | r); | |
6222 | return error_mark_node; | |
6223 | } | |
6224 | ||
6225 | fntype = build_cplus_method_type (r, return_type, TREE_CHAIN | |
6226 | (arg_types)); | |
6227 | } | |
6228 | fntype = build_qualified_type (fntype, TYPE_QUALS (t)); | |
ada846eb | 6229 | fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
4393e105 MM |
6230 | |
6231 | return fntype; | |
6232 | } | |
6233 | ||
297e73d8 MM |
6234 | /* Substitute into the PARMS of a call-declarator. */ |
6235 | ||
e9659ab0 | 6236 | static tree |
4393e105 | 6237 | tsubst_call_declarator_parms (parms, args, complain, in_decl) |
297e73d8 MM |
6238 | tree parms; |
6239 | tree args; | |
4393e105 | 6240 | int complain; |
297e73d8 MM |
6241 | tree in_decl; |
6242 | { | |
6243 | tree new_parms; | |
6244 | tree type; | |
6245 | tree defarg; | |
6246 | ||
6247 | if (!parms || parms == void_list_node) | |
6248 | return parms; | |
6249 | ||
6250 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
4393e105 | 6251 | args, complain, in_decl); |
297e73d8 MM |
6252 | |
6253 | /* Figure out the type of this parameter. */ | |
4393e105 | 6254 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); |
297e73d8 MM |
6255 | |
6256 | /* Figure out the default argument as well. Note that we use | |
4393e105 MM |
6257 | tsubst_expr since the default argument is really an expression. */ |
6258 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
297e73d8 MM |
6259 | |
6260 | /* Chain this parameter on to the front of those we have already | |
6261 | processed. We don't use hash_tree_cons because that function | |
6262 | doesn't check TREE_PARMLIST. */ | |
6263 | new_parms = tree_cons (defarg, type, new_parms); | |
6264 | ||
6265 | /* And note that these are parameters. */ | |
6266 | TREE_PARMLIST (new_parms) = 1; | |
6267 | ||
6268 | return new_parms; | |
6269 | } | |
6270 | ||
4393e105 MM |
6271 | /* Take the tree structure T and replace template parameters used |
6272 | therein with the argument vector ARGS. IN_DECL is an associated | |
6273 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
6274 | An appropriate error message is issued only if COMPLAIN is | |
6275 | non-zero. Note that we must be relatively non-tolerant of | |
6276 | extensions here, in order to preserve conformance; if we allow | |
6277 | substitutions that should not be allowed, we may allow argument | |
6278 | deductions that should not succeed, and therefore report ambiguous | |
6279 | overload situations where there are none. In theory, we could | |
6280 | allow the substitution, but indicate that it should have failed, | |
6281 | and allow our caller to make sure that the right thing happens, but | |
6282 | we don't try to do this yet. | |
6283 | ||
6284 | This function is used for dealing with types, decls and the like; | |
6285 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 MM |
6286 | |
6287 | tree | |
4393e105 | 6288 | tsubst (t, args, complain, in_decl) |
ae58fa02 | 6289 | tree t, args; |
4393e105 | 6290 | int complain; |
ae58fa02 MM |
6291 | tree in_decl; |
6292 | { | |
0ecfe0b4 | 6293 | tree type, r; |
ae58fa02 MM |
6294 | |
6295 | if (t == NULL_TREE || t == error_mark_node | |
6296 | || t == integer_type_node | |
6297 | || t == void_type_node | |
6298 | || t == char_type_node | |
6299 | || TREE_CODE (t) == NAMESPACE_DECL) | |
6300 | return t; | |
6301 | ||
6302 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
6303 | type = IDENTIFIER_TYPE_VALUE (t); | |
6304 | else | |
6305 | type = TREE_TYPE (t); | |
6306 | if (type == unknown_type_node) | |
6307 | my_friendly_abort (42); | |
6308 | ||
6309 | if (type && TREE_CODE (t) != FUNCTION_DECL | |
6310 | && TREE_CODE (t) != TYPENAME_TYPE | |
6311 | && TREE_CODE (t) != TEMPLATE_DECL | |
4393e105 MM |
6312 | && TREE_CODE (t) != IDENTIFIER_NODE |
6313 | && TREE_CODE (t) != FUNCTION_TYPE | |
6314 | && TREE_CODE (t) != METHOD_TYPE) | |
6315 | type = tsubst (type, args, complain, in_decl); | |
6316 | if (type == error_mark_node) | |
6317 | return error_mark_node; | |
ae58fa02 MM |
6318 | |
6319 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd') | |
6320 | return tsubst_decl (t, args, type, in_decl); | |
6321 | ||
6322 | switch (TREE_CODE (t)) | |
6323 | { | |
6324 | case RECORD_TYPE: | |
6325 | case UNION_TYPE: | |
6326 | case ENUMERAL_TYPE: | |
4393e105 MM |
6327 | return tsubst_aggr_type (t, args, complain, in_decl, |
6328 | /*entering_scope=*/0); | |
ae58fa02 MM |
6329 | |
6330 | case ERROR_MARK: | |
6331 | case IDENTIFIER_NODE: | |
6332 | case OP_IDENTIFIER: | |
6333 | case VOID_TYPE: | |
6334 | case REAL_TYPE: | |
6335 | case COMPLEX_TYPE: | |
6336 | case BOOLEAN_TYPE: | |
6337 | case INTEGER_CST: | |
6338 | case REAL_CST: | |
6339 | case STRING_CST: | |
6340 | return t; | |
6341 | ||
6342 | case INTEGER_TYPE: | |
6343 | if (t == integer_type_node) | |
6344 | return t; | |
6345 | ||
6346 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
6347 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
6348 | return t; | |
d2e5ee5c | 6349 | |
5566b478 | 6350 | { |
ddce3528 | 6351 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 6352 | |
ddce3528 | 6353 | max = tsubst_expr (omax, args, complain, in_decl); |
4393e105 MM |
6354 | if (max == error_mark_node) |
6355 | return error_mark_node; | |
6356 | ||
8dd3f57a MM |
6357 | /* See if we can reduce this expression to something simpler. */ |
6358 | max = maybe_fold_nontype_arg (max); | |
6359 | if (!processing_template_decl && TREE_READONLY_DECL_P (max)) | |
6360 | max = decl_constant_value (max); | |
6361 | ||
856216bb MM |
6362 | if (processing_template_decl |
6363 | /* When providing explicit arguments to a template | |
6364 | function, but leaving some arguments for subsequent | |
6365 | deduction, MAX may be template-dependent even if we're | |
6366 | not PROCESSING_TEMPLATE_DECL. */ | |
6367 | || TREE_CODE (max) != INTEGER_CST) | |
ae58fa02 | 6368 | { |
8dd3f57a MM |
6369 | tree itype = make_node (INTEGER_TYPE); |
6370 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
6371 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, | |
6372 | integer_one_node); | |
6373 | return itype; | |
ae58fa02 MM |
6374 | } |
6375 | ||
ddce3528 JM |
6376 | if (integer_zerop (omax)) |
6377 | { | |
6378 | /* Still allow an explicit array of size zero. */ | |
6379 | if (pedantic) | |
6380 | pedwarn ("creating array with size zero"); | |
6381 | } | |
6382 | else if (integer_zerop (max) || INT_CST_LT (max, integer_zero_node)) | |
0ecfe0b4 | 6383 | { |
4393e105 MM |
6384 | /* [temp.deduct] |
6385 | ||
6386 | Type deduction may fail for any of the following | |
6387 | reasons: | |
6388 | ||
bf8f3f93 MM |
6389 | Attempting to create an array with a size that is |
6390 | zero or negative. */ | |
4393e105 MM |
6391 | if (complain) |
6392 | cp_error ("creating array with size `%E'", max); | |
6393 | ||
6394 | return error_mark_node; | |
0ecfe0b4 JM |
6395 | } |
6396 | ||
337c90cc | 6397 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node)); |
120ecb8a | 6398 | return build_index_type (max); |
ae58fa02 MM |
6399 | } |
6400 | ||
6401 | case TEMPLATE_TYPE_PARM: | |
6402 | case TEMPLATE_TEMPLATE_PARM: | |
6403 | case TEMPLATE_PARM_INDEX: | |
6404 | { | |
6405 | int idx; | |
6406 | int level; | |
6407 | int levels; | |
0ecfe0b4 JM |
6408 | |
6409 | r = NULL_TREE; | |
ae58fa02 MM |
6410 | |
6411 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
6412 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6413 | { | |
6414 | idx = TEMPLATE_TYPE_IDX (t); | |
6415 | level = TEMPLATE_TYPE_LEVEL (t); | |
6416 | } | |
6417 | else | |
6418 | { | |
6419 | idx = TEMPLATE_PARM_IDX (t); | |
6420 | level = TEMPLATE_PARM_LEVEL (t); | |
6421 | } | |
6422 | ||
6423 | if (TREE_VEC_LENGTH (args) > 0) | |
6424 | { | |
6425 | tree arg = NULL_TREE; | |
6426 | ||
6427 | levels = TMPL_ARGS_DEPTH (args); | |
6428 | if (level <= levels) | |
6429 | arg = TMPL_ARG (args, level, idx); | |
6430 | ||
8b5b8b7c MM |
6431 | if (arg == error_mark_node) |
6432 | return error_mark_node; | |
6433 | else if (arg != NULL_TREE) | |
ae58fa02 MM |
6434 | { |
6435 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
6436 | { | |
6437 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg)) | |
6438 | == 't', 0); | |
adecb3f4 MM |
6439 | return cp_build_qualified_type_real |
6440 | (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t), | |
6441 | complain); | |
ae58fa02 MM |
6442 | } |
6443 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6444 | { | |
7ddedda4 | 6445 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 MM |
6446 | { |
6447 | /* We are processing a type constructed from | |
6448 | a template template parameter */ | |
7ddedda4 | 6449 | tree argvec = tsubst (TYPE_TI_ARGS (t), |
4393e105 MM |
6450 | args, complain, in_decl); |
6451 | if (argvec == error_mark_node) | |
6452 | return error_mark_node; | |
6453 | ||
ae58fa02 MM |
6454 | /* We can get a TEMPLATE_TEMPLATE_PARM here when |
6455 | we are resolving nested-types in the signature of | |
6456 | a member function templates. | |
6457 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
6458 | template to be instantiated. */ | |
6459 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
6460 | arg = TYPE_NAME (arg); | |
6461 | ||
6462 | r = lookup_template_class (DECL_NAME (arg), | |
6463 | argvec, in_decl, | |
6464 | DECL_CONTEXT (arg), | |
6465 | /*entering_scope=*/0); | |
adecb3f4 MM |
6466 | return cp_build_qualified_type_real (r, |
6467 | TYPE_QUALS (t), | |
6468 | complain); | |
ae58fa02 MM |
6469 | } |
6470 | else | |
6471 | /* We are processing a template argument list. */ | |
6472 | return arg; | |
6473 | } | |
6474 | else | |
6475 | return arg; | |
6476 | } | |
6477 | } | |
51924768 JM |
6478 | else |
6479 | my_friendly_abort (981018); | |
ae58fa02 MM |
6480 | |
6481 | if (level == 1) | |
6482 | /* This can happen during the attempted tsubst'ing in | |
6483 | unify. This means that we don't yet have any information | |
6484 | about the template parameter in question. */ | |
6485 | return t; | |
6486 | ||
6487 | /* If we get here, we must have been looking at a parm for a | |
6488 | more deeply nested template. Make a new version of this | |
6489 | template parameter, but with a lower level. */ | |
6490 | switch (TREE_CODE (t)) | |
6491 | { | |
6492 | case TEMPLATE_TYPE_PARM: | |
6493 | case TEMPLATE_TEMPLATE_PARM: | |
6494 | r = copy_node (t); | |
6495 | TEMPLATE_TYPE_PARM_INDEX (r) | |
6496 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
6497 | r, levels); | |
6498 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
6499 | TYPE_MAIN_VARIANT (r) = r; | |
6500 | TYPE_POINTER_TO (r) = NULL_TREE; | |
6501 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
6502 | ||
6503 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 6504 | && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 | 6505 | { |
4393e105 MM |
6506 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, |
6507 | complain, in_decl); | |
6508 | if (argvec == error_mark_node) | |
6509 | return error_mark_node; | |
6510 | ||
7ddedda4 | 6511 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
e1b3e07d | 6512 | = tree_cons (TYPE_NAME (t), argvec, NULL_TREE); |
ae58fa02 MM |
6513 | } |
6514 | break; | |
6515 | ||
6516 | case TEMPLATE_PARM_INDEX: | |
6517 | r = reduce_template_parm_level (t, type, levels); | |
6518 | break; | |
6519 | ||
6520 | default: | |
6521 | my_friendly_abort (0); | |
6522 | } | |
6523 | ||
5566b478 | 6524 | return r; |
ae58fa02 | 6525 | } |
5566b478 | 6526 | |
8d08fdba MS |
6527 | case TREE_LIST: |
6528 | { | |
6529 | tree purpose, value, chain, result; | |
8d08fdba MS |
6530 | |
6531 | if (t == void_list_node) | |
6532 | return t; | |
6533 | ||
8d08fdba MS |
6534 | purpose = TREE_PURPOSE (t); |
6535 | if (purpose) | |
4393e105 MM |
6536 | { |
6537 | purpose = tsubst (purpose, args, complain, in_decl); | |
6538 | if (purpose == error_mark_node) | |
6539 | return error_mark_node; | |
6540 | } | |
8d08fdba MS |
6541 | value = TREE_VALUE (t); |
6542 | if (value) | |
4393e105 MM |
6543 | { |
6544 | value = tsubst (value, args, complain, in_decl); | |
6545 | if (value == error_mark_node) | |
6546 | return error_mark_node; | |
6547 | } | |
8d08fdba MS |
6548 | chain = TREE_CHAIN (t); |
6549 | if (chain && chain != void_type_node) | |
4393e105 MM |
6550 | { |
6551 | chain = tsubst (chain, args, complain, in_decl); | |
6552 | if (chain == error_mark_node) | |
6553 | return error_mark_node; | |
6554 | } | |
8d08fdba MS |
6555 | if (purpose == TREE_PURPOSE (t) |
6556 | && value == TREE_VALUE (t) | |
6557 | && chain == TREE_CHAIN (t)) | |
6558 | return t; | |
51632249 | 6559 | result = hash_tree_cons (purpose, value, chain); |
8d08fdba MS |
6560 | TREE_PARMLIST (result) = TREE_PARMLIST (t); |
6561 | return result; | |
6562 | } | |
6563 | case TREE_VEC: | |
5566b478 MS |
6564 | if (type != NULL_TREE) |
6565 | { | |
1ceaad38 JM |
6566 | /* A binfo node. We always need to make a copy, of the node |
6567 | itself and of its BINFO_BASETYPES. */ | |
85b71cf2 | 6568 | |
5566b478 MS |
6569 | t = copy_node (t); |
6570 | ||
c7449f2b JM |
6571 | /* Make sure type isn't a typedef copy. */ |
6572 | type = BINFO_TYPE (TYPE_BINFO (type)); | |
6573 | ||
5566b478 | 6574 | TREE_TYPE (t) = complete_type (type); |
6633d636 MS |
6575 | if (IS_AGGR_TYPE (type)) |
6576 | { | |
6577 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
6578 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
6579 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
6580 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
6581 | } | |
5566b478 MS |
6582 | return t; |
6583 | } | |
85b71cf2 JM |
6584 | |
6585 | /* Otherwise, a vector of template arguments. */ | |
4393e105 | 6586 | return tsubst_template_arg_vector (t, args, complain); |
8d08fdba | 6587 | |
8d08fdba MS |
6588 | case POINTER_TYPE: |
6589 | case REFERENCE_TYPE: | |
6590 | { | |
8d08fdba | 6591 | enum tree_code code; |
79a7c7fa | 6592 | |
8d08fdba MS |
6593 | if (type == TREE_TYPE (t)) |
6594 | return t; | |
6595 | ||
6596 | code = TREE_CODE (t); | |
4393e105 MM |
6597 | |
6598 | ||
6599 | /* [temp.deduct] | |
6600 | ||
6601 | Type deduction may fail for any of the following | |
6602 | reasons: | |
6603 | ||
6604 | -- Attempting to create a pointer to reference type. | |
6605 | -- Attempting to create a reference to a reference type or | |
6606 | a reference to void. */ | |
0ecfe0b4 JM |
6607 | if (TREE_CODE (type) == REFERENCE_TYPE |
6608 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa JM |
6609 | { |
6610 | static int last_line = 0; | |
6611 | static char* last_file = 0; | |
6612 | ||
6613 | /* We keep track of the last time we issued this error | |
6614 | message to avoid spewing a ton of messages during a | |
6615 | single bad template instantiation. */ | |
4393e105 MM |
6616 | if (complain && (last_line != lineno || |
6617 | last_file != input_filename)) | |
79a7c7fa | 6618 | { |
0ecfe0b4 JM |
6619 | if (TREE_CODE (type) == VOID_TYPE) |
6620 | cp_error ("forming reference to void"); | |
6621 | else | |
6622 | cp_error ("forming %s to reference type `%T'", | |
6623 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
6624 | type); | |
79a7c7fa JM |
6625 | last_line = lineno; |
6626 | last_file = input_filename; | |
6627 | } | |
6628 | ||
4393e105 | 6629 | return error_mark_node; |
79a7c7fa JM |
6630 | } |
6631 | else if (code == POINTER_TYPE) | |
8d08fdba MS |
6632 | r = build_pointer_type (type); |
6633 | else | |
6634 | r = build_reference_type (type); | |
adecb3f4 | 6635 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 6636 | |
8d08fdba MS |
6637 | /* Will this ever be needed for TYPE_..._TO values? */ |
6638 | layout_type (r); | |
6639 | return r; | |
6640 | } | |
a4443a08 | 6641 | case OFFSET_TYPE: |
0ecfe0b4 | 6642 | { |
4393e105 MM |
6643 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
6644 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
6645 | { | |
6646 | /* [temp.deduct] | |
6647 | ||
6648 | Type deduction may fail for any of the following | |
6649 | reasons: | |
6650 | ||
6651 | -- Attempting to create "pointer to member of T" when T | |
6652 | is not a class type. */ | |
6653 | if (complain) | |
6654 | cp_error ("creating pointer to member of non-class type `%T'", | |
6655 | r); | |
6656 | return error_mark_node; | |
6657 | } | |
0ecfe0b4 JM |
6658 | return build_offset_type (r, type); |
6659 | } | |
8d08fdba MS |
6660 | case FUNCTION_TYPE: |
6661 | case METHOD_TYPE: | |
6662 | { | |
c11b6f21 | 6663 | tree fntype; |
4393e105 | 6664 | tree raises; |
8d08fdba | 6665 | |
4393e105 MM |
6666 | fntype = tsubst_function_type (t, args, complain, in_decl); |
6667 | if (fntype == error_mark_node) | |
6668 | return error_mark_node; | |
cabc336a MM |
6669 | |
6670 | /* Substitue the exception specification. */ | |
6671 | raises = TYPE_RAISES_EXCEPTIONS (t); | |
c11b6f21 MS |
6672 | if (raises) |
6673 | { | |
4cc1d462 NS |
6674 | tree list = NULL_TREE; |
6675 | ||
6676 | if (! TREE_VALUE (raises)) | |
6677 | list = raises; | |
6678 | else | |
6679 | for (; raises != NULL_TREE; raises = TREE_CHAIN (raises)) | |
6680 | { | |
6681 | tree spec = TREE_VALUE (raises); | |
6682 | ||
6683 | spec = tsubst (spec, args, complain, in_decl); | |
6684 | if (spec == error_mark_node) | |
6685 | return spec; | |
6686 | list = add_exception_specifier (list, spec, complain); | |
6687 | } | |
6688 | fntype = build_exception_variant (fntype, list); | |
c11b6f21 MS |
6689 | } |
6690 | return fntype; | |
8d08fdba MS |
6691 | } |
6692 | case ARRAY_TYPE: | |
6693 | { | |
4393e105 MM |
6694 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
6695 | if (domain == error_mark_node) | |
6696 | return error_mark_node; | |
6697 | ||
6698 | /* As an optimization, we avoid regenerating the array type if | |
6699 | it will obviously be the same as T. */ | |
8d08fdba MS |
6700 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
6701 | return t; | |
0ecfe0b4 | 6702 | |
4393e105 MM |
6703 | /* These checks should match the ones in grokdeclarator. |
6704 | ||
6705 | [temp.deduct] | |
6706 | ||
6707 | The deduction may fail for any of the following reasons: | |
6708 | ||
6709 | -- Attempting to create an array with an element type that | |
6710 | is void, a function type, or a reference type. */ | |
6711 | if (TREE_CODE (type) == VOID_TYPE | |
6712 | || TREE_CODE (type) == FUNCTION_TYPE | |
6713 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 6714 | { |
4393e105 MM |
6715 | if (complain) |
6716 | cp_error ("creating array of `%T'", type); | |
6717 | return error_mark_node; | |
0ecfe0b4 JM |
6718 | } |
6719 | ||
8d08fdba MS |
6720 | r = build_cplus_array_type (type, domain); |
6721 | return r; | |
6722 | } | |
6723 | ||
8d08fdba | 6724 | case PLUS_EXPR: |
5566b478 | 6725 | case MINUS_EXPR: |
4393e105 MM |
6726 | { |
6727 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6728 | in_decl); | |
6729 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, | |
6730 | in_decl); | |
6731 | ||
6732 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6733 | return error_mark_node; | |
6734 | ||
6735 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); | |
6736 | } | |
8d08fdba MS |
6737 | |
6738 | case NEGATE_EXPR: | |
6739 | case NOP_EXPR: | |
4393e105 MM |
6740 | { |
6741 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6742 | in_decl); | |
6743 | if (e == error_mark_node) | |
6744 | return error_mark_node; | |
6745 | ||
6746 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); | |
6747 | } | |
8d08fdba | 6748 | |
5566b478 MS |
6749 | case TYPENAME_TYPE: |
6750 | { | |
4393e105 MM |
6751 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
6752 | in_decl, /*entering_scope=*/1); | |
6753 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
6754 | complain, in_decl); | |
6755 | ||
6756 | if (ctx == error_mark_node || f == error_mark_node) | |
6757 | return error_mark_node; | |
ae58fa02 | 6758 | |
bf8f3f93 MM |
6759 | if (!IS_AGGR_TYPE (ctx)) |
6760 | { | |
6761 | if (complain) | |
6762 | cp_error ("`%T' is not a class, struct, or union type", | |
6763 | ctx); | |
6764 | return error_mark_node; | |
6765 | } | |
6766 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
6767 | { | |
6768 | /* Normally, make_typename_type does not require that the CTX | |
6769 | have complete type in order to allow things like: | |
ae58fa02 | 6770 | |
bf8f3f93 | 6771 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 6772 | |
bf8f3f93 MM |
6773 | But, such constructs have already been resolved by this |
6774 | point, so here CTX really should have complete type, unless | |
6775 | it's a partial instantiation. */ | |
4393e105 MM |
6776 | ctx = complete_type (ctx); |
6777 | if (!TYPE_SIZE (ctx)) | |
6778 | { | |
6779 | if (complain) | |
6780 | incomplete_type_error (NULL_TREE, ctx); | |
6781 | return error_mark_node; | |
6782 | } | |
6783 | } | |
ae58fa02 | 6784 | |
b2b7d40a | 6785 | f = make_typename_type (ctx, f); |
f0bcd168 MM |
6786 | if (f == error_mark_node) |
6787 | return f; | |
adecb3f4 MM |
6788 | return cp_build_qualified_type_real (f, |
6789 | CP_TYPE_QUALS (f) | |
6790 | | CP_TYPE_QUALS (t), | |
6791 | complain); | |
5566b478 MS |
6792 | } |
6793 | ||
6794 | case INDIRECT_REF: | |
4393e105 MM |
6795 | { |
6796 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6797 | in_decl); | |
6798 | if (e == error_mark_node) | |
6799 | return error_mark_node; | |
6800 | return make_pointer_declarator (type, e); | |
6801 | } | |
6802 | ||
5566b478 | 6803 | case ADDR_EXPR: |
4393e105 MM |
6804 | { |
6805 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6806 | in_decl); | |
6807 | if (e == error_mark_node) | |
6808 | return error_mark_node; | |
6809 | return make_reference_declarator (type, e); | |
6810 | } | |
5566b478 MS |
6811 | |
6812 | case ARRAY_REF: | |
4393e105 MM |
6813 | { |
6814 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6815 | in_decl); | |
6fb2c05a JM |
6816 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, |
6817 | in_decl); | |
4393e105 MM |
6818 | if (e1 == error_mark_node || e2 == error_mark_node) |
6819 | return error_mark_node; | |
6820 | ||
6821 | return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr); | |
6822 | } | |
5566b478 MS |
6823 | |
6824 | case CALL_EXPR: | |
4393e105 MM |
6825 | { |
6826 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6827 | in_decl); | |
43f887f9 MM |
6828 | tree e2 = (tsubst_call_declarator_parms |
6829 | (CALL_DECLARATOR_PARMS (t), args, complain, in_decl)); | |
6830 | tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args, | |
6831 | complain, in_decl); | |
4393e105 MM |
6832 | |
6833 | if (e1 == error_mark_node || e2 == error_mark_node | |
6834 | || e3 == error_mark_node) | |
6835 | return error_mark_node; | |
6836 | ||
43f887f9 | 6837 | return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3); |
4393e105 | 6838 | } |
5566b478 | 6839 | |
fc378698 | 6840 | case SCOPE_REF: |
4393e105 MM |
6841 | { |
6842 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6843 | in_decl); | |
6844 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
6845 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6846 | return error_mark_node; | |
6847 | ||
6848 | return build_parse_node (TREE_CODE (t), e1, e2); | |
6849 | } | |
fc378698 | 6850 | |
b894fc05 | 6851 | case TYPEOF_TYPE: |
4393e105 MM |
6852 | { |
6853 | tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, | |
6854 | in_decl); | |
6855 | if (e1 == error_mark_node) | |
6856 | return error_mark_node; | |
6857 | ||
6858 | return TREE_TYPE (e1); | |
6859 | } | |
b894fc05 | 6860 | |
8d08fdba | 6861 | default: |
5566b478 | 6862 | sorry ("use of `%s' in template", |
8d08fdba MS |
6863 | tree_code_name [(int) TREE_CODE (t)]); |
6864 | return error_mark_node; | |
6865 | } | |
6866 | } | |
6867 | ||
00d3396f JM |
6868 | /* Like tsubst, but deals with expressions. This function just replaces |
6869 | template parms; to finish processing the resultant expression, use | |
6870 | tsubst_expr. */ | |
6871 | ||
5566b478 | 6872 | tree |
4393e105 | 6873 | tsubst_copy (t, args, complain, in_decl) |
98c1c668 | 6874 | tree t, args; |
4393e105 | 6875 | int complain; |
5566b478 MS |
6876 | tree in_decl; |
6877 | { | |
6878 | enum tree_code code; | |
8452b1d3 | 6879 | tree r; |
8d08fdba | 6880 | |
5566b478 MS |
6881 | if (t == NULL_TREE || t == error_mark_node) |
6882 | return t; | |
6883 | ||
6884 | code = TREE_CODE (t); | |
b7484fbe | 6885 | |
5566b478 MS |
6886 | switch (code) |
6887 | { | |
6888 | case PARM_DECL: | |
a759e627 | 6889 | return do_identifier (DECL_NAME (t), 0, NULL_TREE); |
5566b478 MS |
6890 | |
6891 | case CONST_DECL: | |
ed44da02 MM |
6892 | { |
6893 | tree enum_type; | |
6894 | tree v; | |
6895 | ||
6896 | if (!DECL_CONTEXT (t)) | |
6897 | /* This is a global enumeration constant. */ | |
6898 | return t; | |
6899 | ||
6900 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 MM |
6901 | Consider: |
6902 | ||
6903 | template <int I> int f() { | |
6904 | enum E { a = I }; | |
6905 | struct S { void g() { E e = a; } }; | |
6906 | }; | |
6907 | ||
6908 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
6909 | clever enough to find f<7>::a. */ | |
ed44da02 | 6910 | enum_type |
4393e105 | 6911 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
ed44da02 MM |
6912 | /*entering_scope=*/0); |
6913 | ||
6914 | for (v = TYPE_VALUES (enum_type); | |
6915 | v != NULL_TREE; | |
6916 | v = TREE_CHAIN (v)) | |
6917 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
6918 | return TREE_VALUE (v); | |
6919 | ||
6920 | /* We didn't find the name. That should never happen; if | |
6921 | name-lookup found it during preliminary parsing, we | |
6922 | should find it again here during instantiation. */ | |
6923 | my_friendly_abort (0); | |
6924 | } | |
db04386f | 6925 | return t; |
ed44da02 | 6926 | |
5566b478 MS |
6927 | case FIELD_DECL: |
6928 | if (DECL_CONTEXT (t)) | |
6929 | { | |
0978790f | 6930 | tree ctx; |
0978790f | 6931 | |
4393e105 | 6932 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 6933 | /*entering_scope=*/1); |
0978790f | 6934 | if (ctx != DECL_CONTEXT (t)) |
5566b478 MS |
6935 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
6936 | } | |
6937 | return t; | |
6938 | ||
6939 | case VAR_DECL: | |
6940 | case FUNCTION_DECL: | |
6941 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
4393e105 | 6942 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
6943 | mark_used (t); |
6944 | return t; | |
6945 | ||
98c1c668 JM |
6946 | case TEMPLATE_DECL: |
6947 | if (is_member_template (t)) | |
4393e105 | 6948 | return tsubst (t, args, complain, in_decl); |
98c1c668 JM |
6949 | else |
6950 | return t; | |
6951 | ||
672476cb MM |
6952 | case LOOKUP_EXPR: |
6953 | { | |
6954 | /* We must tsbust into a LOOKUP_EXPR in case the names to | |
6955 | which it refers is a conversion operator; in that case the | |
6956 | name will change. We avoid making unnecessary copies, | |
6957 | however. */ | |
6958 | ||
4393e105 | 6959 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
672476cb MM |
6960 | |
6961 | if (id != TREE_OPERAND (t, 0)) | |
6962 | { | |
8452b1d3 | 6963 | r = build_nt (LOOKUP_EXPR, id); |
672476cb MM |
6964 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); |
6965 | t = r; | |
6966 | } | |
6967 | ||
6968 | return t; | |
6969 | } | |
6970 | ||
5566b478 MS |
6971 | case CAST_EXPR: |
6972 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
6973 | case CONST_CAST_EXPR: |
6974 | case STATIC_CAST_EXPR: | |
6975 | case DYNAMIC_CAST_EXPR: | |
51924768 | 6976 | case NOP_EXPR: |
5566b478 | 6977 | return build1 |
4393e105 MM |
6978 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6979 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
6980 | |
6981 | case INDIRECT_REF: | |
6982 | case PREDECREMENT_EXPR: | |
6983 | case PREINCREMENT_EXPR: | |
6984 | case POSTDECREMENT_EXPR: | |
6985 | case POSTINCREMENT_EXPR: | |
6986 | case NEGATE_EXPR: | |
6987 | case TRUTH_NOT_EXPR: | |
b87692e5 | 6988 | case BIT_NOT_EXPR: |
5566b478 MS |
6989 | case ADDR_EXPR: |
6990 | case CONVERT_EXPR: /* Unary + */ | |
6991 | case SIZEOF_EXPR: | |
abff8e06 | 6992 | case ALIGNOF_EXPR: |
5566b478 | 6993 | case ARROW_EXPR: |
fc378698 | 6994 | case THROW_EXPR: |
5156628f | 6995 | case TYPEID_EXPR: |
5566b478 | 6996 | return build1 |
6a629cac | 6997 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 6998 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 MS |
6999 | |
7000 | case PLUS_EXPR: | |
7001 | case MINUS_EXPR: | |
7002 | case MULT_EXPR: | |
7003 | case TRUNC_DIV_EXPR: | |
7004 | case CEIL_DIV_EXPR: | |
7005 | case FLOOR_DIV_EXPR: | |
7006 | case ROUND_DIV_EXPR: | |
7007 | case EXACT_DIV_EXPR: | |
7008 | case BIT_AND_EXPR: | |
7009 | case BIT_ANDTC_EXPR: | |
7010 | case BIT_IOR_EXPR: | |
7011 | case BIT_XOR_EXPR: | |
7012 | case TRUNC_MOD_EXPR: | |
7013 | case FLOOR_MOD_EXPR: | |
7014 | case TRUTH_ANDIF_EXPR: | |
7015 | case TRUTH_ORIF_EXPR: | |
7016 | case TRUTH_AND_EXPR: | |
7017 | case TRUTH_OR_EXPR: | |
7018 | case RSHIFT_EXPR: | |
7019 | case LSHIFT_EXPR: | |
7020 | case RROTATE_EXPR: | |
7021 | case LROTATE_EXPR: | |
7022 | case EQ_EXPR: | |
7023 | case NE_EXPR: | |
7024 | case MAX_EXPR: | |
7025 | case MIN_EXPR: | |
7026 | case LE_EXPR: | |
7027 | case GE_EXPR: | |
7028 | case LT_EXPR: | |
7029 | case GT_EXPR: | |
7030 | case COMPONENT_REF: | |
7031 | case ARRAY_REF: | |
7032 | case COMPOUND_EXPR: | |
7033 | case SCOPE_REF: | |
7034 | case DOTSTAR_EXPR: | |
7035 | case MEMBER_REF: | |
7036 | return build_nt | |
4393e105 MM |
7037 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7038 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
7039 | |
7040 | case CALL_EXPR: | |
7041 | { | |
7042 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 7043 | if (is_overloaded_fn (fn)) |
4393e105 | 7044 | fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl); |
5566b478 | 7045 | else |
f84b4be9 | 7046 | /* Sometimes FN is a LOOKUP_EXPR. */ |
4393e105 | 7047 | fn = tsubst_copy (fn, args, complain, in_decl); |
5566b478 | 7048 | return build_nt |
4393e105 MM |
7049 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
7050 | in_decl), | |
5566b478 MS |
7051 | NULL_TREE); |
7052 | } | |
7053 | ||
7054 | case METHOD_CALL_EXPR: | |
7055 | { | |
7056 | tree name = TREE_OPERAND (t, 0); | |
7057 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
7058 | { | |
4393e105 MM |
7059 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
7060 | complain, in_decl); | |
7bae46f4 | 7061 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
7062 | } |
7063 | else if (TREE_CODE (name) == SCOPE_REF | |
7064 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
7065 | { | |
4393e105 MM |
7066 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, |
7067 | complain, in_decl); | |
5566b478 | 7068 | name = TREE_OPERAND (name, 1); |
4393e105 MM |
7069 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
7070 | complain, in_decl); | |
11686454 | 7071 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
7072 | name = build_nt (SCOPE_REF, base, name); |
7073 | } | |
7074 | else | |
4393e105 | 7075 | name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
5566b478 | 7076 | return build_nt |
4393e105 MM |
7077 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, |
7078 | complain, in_decl), | |
7079 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), | |
5566b478 MS |
7080 | NULL_TREE); |
7081 | } | |
7082 | ||
558475f0 MM |
7083 | case STMT_EXPR: |
7084 | /* This processing should really occur in tsubst_expr, However, | |
7085 | tsubst_expr does not recurse into expressions, since it | |
7086 | assumes that there aren't any statements inside them. | |
7087 | Instead, it simply calls build_expr_from_tree. So, we need | |
7088 | to expand the STMT_EXPR here. */ | |
7089 | if (!processing_template_decl) | |
7090 | { | |
7091 | tree rtl_expr = begin_stmt_expr (); | |
7092 | tree block = tsubst_expr (STMT_EXPR_STMT (t), args, | |
7093 | complain, in_decl); | |
9d9169de | 7094 | return finish_stmt_expr (rtl_expr, block); |
558475f0 | 7095 | } |
9d9169de MM |
7096 | |
7097 | return t; | |
558475f0 | 7098 | |
5566b478 MS |
7099 | case COND_EXPR: |
7100 | case MODOP_EXPR: | |
40242ccf | 7101 | case PSEUDO_DTOR_EXPR: |
67da3287 | 7102 | { |
8452b1d3 | 7103 | r = build_nt |
4393e105 MM |
7104 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7105 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7106 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
67da3287 MM |
7107 | return r; |
7108 | } | |
5566b478 MS |
7109 | |
7110 | case NEW_EXPR: | |
7111 | { | |
8452b1d3 | 7112 | r = build_nt |
4393e105 MM |
7113 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7114 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7115 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
7116 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
7117 | return r; | |
7118 | } | |
7119 | ||
7120 | case DELETE_EXPR: | |
7121 | { | |
8452b1d3 | 7122 | r = build_nt |
4393e105 MM |
7123 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7124 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
7125 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
7126 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
7127 | return r; | |
7128 | } | |
7129 | ||
386b8a85 JM |
7130 | case TEMPLATE_ID_EXPR: |
7131 | { | |
00d3396f | 7132 | /* Substituted template arguments */ |
4393e105 MM |
7133 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
7134 | in_decl); | |
856216bb MM |
7135 | |
7136 | if (targs && TREE_CODE (targs) == TREE_LIST) | |
7137 | { | |
7138 | tree chain; | |
7139 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
7140 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
7141 | } | |
7142 | else if (targs) | |
7143 | { | |
7144 | int i; | |
7145 | for (i = 0; i < TREE_VEC_LENGTH (targs); ++i) | |
7146 | TREE_VEC_ELT (targs, i) | |
7147 | = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i)); | |
7148 | } | |
00d3396f JM |
7149 | |
7150 | return lookup_template_function | |
4393e105 | 7151 | (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); |
386b8a85 JM |
7152 | } |
7153 | ||
5566b478 MS |
7154 | case TREE_LIST: |
7155 | { | |
7156 | tree purpose, value, chain; | |
7157 | ||
7158 | if (t == void_list_node) | |
7159 | return t; | |
7160 | ||
7161 | purpose = TREE_PURPOSE (t); | |
7162 | if (purpose) | |
4393e105 | 7163 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
7164 | value = TREE_VALUE (t); |
7165 | if (value) | |
4393e105 | 7166 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
7167 | chain = TREE_CHAIN (t); |
7168 | if (chain && chain != void_type_node) | |
4393e105 | 7169 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
7170 | if (purpose == TREE_PURPOSE (t) |
7171 | && value == TREE_VALUE (t) | |
7172 | && chain == TREE_CHAIN (t)) | |
7173 | return t; | |
7174 | return tree_cons (purpose, value, chain); | |
7175 | } | |
7176 | ||
7177 | case RECORD_TYPE: | |
7178 | case UNION_TYPE: | |
7179 | case ENUMERAL_TYPE: | |
7180 | case INTEGER_TYPE: | |
7181 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 7182 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 7183 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
7184 | case POINTER_TYPE: |
7185 | case REFERENCE_TYPE: | |
7186 | case OFFSET_TYPE: | |
7187 | case FUNCTION_TYPE: | |
7188 | case METHOD_TYPE: | |
7189 | case ARRAY_TYPE: | |
7190 | case TYPENAME_TYPE: | |
f84b4be9 | 7191 | case TYPE_DECL: |
4393e105 | 7192 | return tsubst (t, args, complain, in_decl); |
5566b478 | 7193 | |
e92cc029 | 7194 | case IDENTIFIER_NODE: |
8e710788 BK |
7195 | if (IDENTIFIER_TYPENAME_P (t) |
7196 | /* Make sure it's not just a variable named `__opr', for instance, | |
7197 | which can occur in some existing code. */ | |
7198 | && TREE_TYPE (t)) | |
e92cc029 | 7199 | return build_typename_overload |
4393e105 | 7200 | (tsubst (TREE_TYPE (t), args, complain, in_decl)); |
e92cc029 MS |
7201 | else |
7202 | return t; | |
7203 | ||
5156628f | 7204 | case CONSTRUCTOR: |
8452b1d3 JM |
7205 | { |
7206 | r = build | |
4393e105 MM |
7207 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), |
7208 | NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, | |
7209 | complain, in_decl)); | |
8452b1d3 JM |
7210 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
7211 | return r; | |
7212 | } | |
5156628f | 7213 | |
5566b478 MS |
7214 | default: |
7215 | return t; | |
7216 | } | |
7217 | } | |
7218 | ||
558475f0 | 7219 | /* Like tsubst_copy, but also does semantic processing. */ |
00d3396f | 7220 | |
5566b478 | 7221 | tree |
4393e105 | 7222 | tsubst_expr (t, args, complain, in_decl) |
98c1c668 | 7223 | tree t, args; |
4393e105 | 7224 | int complain; |
5566b478 MS |
7225 | tree in_decl; |
7226 | { | |
558475f0 MM |
7227 | tree stmt; |
7228 | ||
5566b478 MS |
7229 | if (t == NULL_TREE || t == error_mark_node) |
7230 | return t; | |
7231 | ||
5156628f | 7232 | if (processing_template_decl) |
4393e105 | 7233 | return tsubst_copy (t, args, complain, in_decl); |
5566b478 MS |
7234 | |
7235 | switch (TREE_CODE (t)) | |
8d08fdba | 7236 | { |
558475f0 | 7237 | case RETURN_INIT: |
a7e4cfa0 | 7238 | prep_stmt (t); |
558475f0 MM |
7239 | finish_named_return_value |
7240 | (TREE_OPERAND (t, 0), | |
7241 | tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, in_decl)); | |
7242 | tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7243 | break; | |
7244 | ||
7245 | case CTOR_INITIALIZER: | |
a7e4cfa0 | 7246 | prep_stmt (t); |
558475f0 MM |
7247 | current_member_init_list |
7248 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
7249 | current_base_init_list | |
7250 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
7251 | setup_vtbl_ptr (); | |
7252 | tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7253 | break; | |
7254 | ||
5566b478 | 7255 | case RETURN_STMT: |
a7e4cfa0 | 7256 | prep_stmt (t); |
ad321293 | 7257 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
4393e105 | 7258 | args, complain, in_decl)); |
5566b478 MS |
7259 | break; |
7260 | ||
7261 | case EXPR_STMT: | |
a7e4cfa0 MM |
7262 | prep_stmt (t); |
7263 | /* If we're doing tsubst'ing, then we should not yet have done | |
7264 | semantic analysisy, so we should not know that this statement | |
7265 | assigns to the `this' pointer. */ | |
7266 | my_friendly_assert (EXPR_STMT_ASSIGNS_THIS (t) == 0, | |
7267 | 19990831); | |
ad321293 | 7268 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
4393e105 | 7269 | args, complain, in_decl)); |
5566b478 MS |
7270 | break; |
7271 | ||
7272 | case DECL_STMT: | |
7273 | { | |
7274 | int i = suspend_momentary (); | |
e0942dcd MM |
7275 | tree decl; |
7276 | tree init; | |
5566b478 | 7277 | |
a7e4cfa0 | 7278 | prep_stmt (t); |
e0942dcd | 7279 | decl = DECL_STMT_DECL (t); |
acef433b MM |
7280 | if (TREE_CODE (decl) == LABEL_DECL) |
7281 | finish_label_decl (DECL_NAME (decl)); | |
7282 | else | |
7283 | { | |
7284 | init = DECL_INITIAL (decl); | |
7285 | decl = tsubst (decl, args, complain, in_decl); | |
7286 | init = tsubst_expr (init, args, complain, in_decl); | |
7287 | DECL_INITIAL (decl) = init; | |
b7b8bcd2 MM |
7288 | /* By marking the declaration as instantiated, we avoid |
7289 | trying to instantiate it. Since instantiate_decl can't | |
7290 | handle local variables, and since we've already done | |
7291 | all that needs to be done, that's the right thing to | |
7292 | do. */ | |
acef433b MM |
7293 | if (TREE_CODE (decl) == VAR_DECL) |
7294 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
7295 | maybe_push_decl (decl); | |
b7b8bcd2 | 7296 | cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0); |
acef433b MM |
7297 | add_decl_stmt (decl); |
7298 | } | |
5566b478 | 7299 | resume_momentary (i); |
e0942dcd | 7300 | return decl; |
5566b478 | 7301 | } |
8d08fdba | 7302 | |
5566b478 MS |
7303 | case FOR_STMT: |
7304 | { | |
7305 | tree tmp; | |
a7e4cfa0 | 7306 | prep_stmt (t); |
5566b478 | 7307 | |
558475f0 | 7308 | stmt = begin_for_stmt (); |
ad321293 | 7309 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) |
4393e105 | 7310 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7311 | finish_for_init_stmt (stmt); |
ad321293 | 7312 | finish_for_cond (tsubst_expr (FOR_COND (t), args, |
4393e105 | 7313 | complain, in_decl), |
558475f0 | 7314 | stmt); |
4393e105 | 7315 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); |
558475f0 | 7316 | finish_for_expr (tmp, stmt); |
4393e105 | 7317 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); |
558475f0 | 7318 | finish_for_stmt (tmp, stmt); |
5566b478 MS |
7319 | } |
7320 | break; | |
8d08fdba | 7321 | |
5566b478 MS |
7322 | case WHILE_STMT: |
7323 | { | |
a7e4cfa0 | 7324 | prep_stmt (t); |
558475f0 | 7325 | stmt = begin_while_stmt (); |
ad321293 | 7326 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), |
4393e105 | 7327 | args, complain, in_decl), |
558475f0 | 7328 | stmt); |
4393e105 | 7329 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); |
558475f0 | 7330 | finish_while_stmt (stmt); |
5566b478 MS |
7331 | } |
7332 | break; | |
8d08fdba | 7333 | |
5566b478 MS |
7334 | case DO_STMT: |
7335 | { | |
a7e4cfa0 | 7336 | prep_stmt (t); |
558475f0 | 7337 | stmt = begin_do_stmt (); |
4393e105 | 7338 | tsubst_expr (DO_BODY (t), args, complain, in_decl); |
558475f0 | 7339 | finish_do_body (stmt); |
ad321293 | 7340 | finish_do_stmt (tsubst_expr (DO_COND (t), args, |
4393e105 | 7341 | complain, in_decl), |
558475f0 | 7342 | stmt); |
5566b478 MS |
7343 | } |
7344 | break; | |
a0a33927 | 7345 | |
5566b478 | 7346 | case IF_STMT: |
8d08fdba | 7347 | { |
5566b478 | 7348 | tree tmp; |
5566b478 | 7349 | |
a7e4cfa0 | 7350 | prep_stmt (t); |
558475f0 | 7351 | stmt = begin_if_stmt (); |
ad321293 | 7352 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), |
4393e105 | 7353 | args, complain, in_decl), |
558475f0 | 7354 | stmt); |
db5ae43f | 7355 | |
ad321293 | 7356 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 7357 | { |
4393e105 | 7358 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7359 | finish_then_clause (stmt); |
db5ae43f MS |
7360 | } |
7361 | ||
ad321293 MM |
7362 | if (tmp = ELSE_CLAUSE (t), tmp) |
7363 | { | |
7364 | begin_else_clause (); | |
4393e105 | 7365 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7366 | finish_else_clause (stmt); |
ad321293 | 7367 | } |
8d08fdba | 7368 | |
ad321293 | 7369 | finish_if_stmt (); |
8d08fdba | 7370 | } |
5566b478 | 7371 | break; |
8d08fdba | 7372 | |
5566b478 MS |
7373 | case COMPOUND_STMT: |
7374 | { | |
ad321293 | 7375 | tree substmt; |
8d08fdba | 7376 | |
a7e4cfa0 | 7377 | prep_stmt (t); |
558475f0 | 7378 | stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
ad321293 MM |
7379 | for (substmt = COMPOUND_BODY (t); |
7380 | substmt != NULL_TREE; | |
7381 | substmt = TREE_CHAIN (substmt)) | |
4393e105 | 7382 | tsubst_expr (substmt, args, complain, in_decl); |
558475f0 | 7383 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt); |
5566b478 MS |
7384 | } |
7385 | break; | |
8d08fdba | 7386 | |
5566b478 | 7387 | case BREAK_STMT: |
a7e4cfa0 | 7388 | prep_stmt (t); |
ad321293 | 7389 | finish_break_stmt (); |
5566b478 | 7390 | break; |
8d08fdba | 7391 | |
6467930b | 7392 | case CONTINUE_STMT: |
a7e4cfa0 | 7393 | prep_stmt (t); |
ad321293 | 7394 | finish_continue_stmt (); |
6467930b MS |
7395 | break; |
7396 | ||
5566b478 MS |
7397 | case SWITCH_STMT: |
7398 | { | |
558475f0 | 7399 | tree val; |
5566b478 | 7400 | |
a7e4cfa0 | 7401 | prep_stmt (t); |
527f0080 | 7402 | stmt = begin_switch_stmt (); |
4393e105 | 7403 | val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); |
527f0080 | 7404 | finish_switch_cond (val, stmt); |
558475f0 MM |
7405 | tsubst_expr (SWITCH_BODY (t), args, complain, in_decl); |
7406 | finish_switch_stmt (val, stmt); | |
5566b478 MS |
7407 | } |
7408 | break; | |
7409 | ||
7410 | case CASE_LABEL: | |
a7e4cfa0 | 7411 | prep_stmt (t); |
4393e105 MM |
7412 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), |
7413 | tsubst_expr (CASE_HIGH (t), args, complain, in_decl)); | |
5566b478 MS |
7414 | break; |
7415 | ||
acef433b MM |
7416 | case LABEL_STMT: |
7417 | lineno = STMT_LINENO (t); | |
7418 | finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t))); | |
5566b478 MS |
7419 | break; |
7420 | ||
7421 | case GOTO_STMT: | |
a7e4cfa0 | 7422 | prep_stmt (t); |
aa09da44 | 7423 | t = GOTO_DESTINATION (t); |
3fa56191 | 7424 | if (TREE_CODE (t) != LABEL_DECL) |
aa09da44 MM |
7425 | /* Computed goto's must be tsubst'd into. On the other hand, |
7426 | non-computed gotos must not be; the identifier in question | |
7427 | will have no binding. */ | |
4393e105 | 7428 | t = tsubst_expr (t, args, complain, in_decl); |
3fa56191 MM |
7429 | else |
7430 | t = DECL_NAME (t); | |
aa09da44 | 7431 | finish_goto_stmt (t); |
ad321293 MM |
7432 | break; |
7433 | ||
7434 | case ASM_STMT: | |
a7e4cfa0 | 7435 | prep_stmt (t); |
3ebc5c52 | 7436 | finish_asm_stmt (ASM_CV_QUAL (t), |
4393e105 MM |
7437 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), |
7438 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
7439 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
7440 | tsubst_expr (ASM_CLOBBERS (t), args, complain, | |
7441 | in_decl)); | |
5566b478 | 7442 | break; |
faf5394a MS |
7443 | |
7444 | case TRY_BLOCK: | |
a7e4cfa0 | 7445 | prep_stmt (t); |
f1dedc31 | 7446 | if (CLEANUP_P (t)) |
62409b39 MM |
7447 | { |
7448 | begin_try_block (); | |
7449 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); | |
7450 | finish_cleanup_try_block (stmt); | |
7451 | finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args, | |
7452 | complain, in_decl), | |
7453 | stmt); | |
7454 | } | |
f1dedc31 MM |
7455 | else |
7456 | { | |
62409b39 MM |
7457 | tree handler; |
7458 | ||
7459 | if (FN_TRY_BLOCK_P (t)) | |
7460 | stmt = begin_function_try_block (); | |
7461 | else | |
7462 | stmt = begin_try_block (); | |
7463 | ||
7464 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); | |
7465 | ||
7466 | if (FN_TRY_BLOCK_P (t)) | |
7467 | finish_function_try_block (stmt); | |
7468 | else | |
7469 | finish_try_block (stmt); | |
7470 | ||
7471 | handler = TRY_HANDLERS (t); | |
f1dedc31 MM |
7472 | for (; handler; handler = TREE_CHAIN (handler)) |
7473 | tsubst_expr (handler, args, complain, in_decl); | |
7474 | finish_handler_sequence (stmt); | |
7475 | } | |
faf5394a MS |
7476 | break; |
7477 | ||
7478 | case HANDLER: | |
a7e4cfa0 | 7479 | prep_stmt (t); |
558475f0 | 7480 | stmt = begin_handler (); |
ad321293 | 7481 | if (HANDLER_PARMS (t)) |
3c5c0849 MM |
7482 | expand_start_catch_block |
7483 | (tsubst (DECL_STMT_DECL (HANDLER_PARMS (t)), | |
7484 | args, complain, in_decl)); | |
faf5394a | 7485 | else |
3c5c0849 | 7486 | expand_start_catch_block (NULL_TREE); |
558475f0 | 7487 | finish_handler_parms (stmt); |
4393e105 | 7488 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); |
558475f0 | 7489 | finish_handler (stmt); |
faf5394a MS |
7490 | break; |
7491 | ||
b87692e5 | 7492 | case TAG_DEFN: |
a7e4cfa0 | 7493 | prep_stmt (t); |
b87692e5 MS |
7494 | t = TREE_TYPE (t); |
7495 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
4393e105 | 7496 | tsubst (t, args, complain, NULL_TREE); |
b87692e5 MS |
7497 | break; |
7498 | ||
5566b478 | 7499 | default: |
4393e105 | 7500 | return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl)); |
5566b478 MS |
7501 | } |
7502 | return NULL_TREE; | |
8d08fdba MS |
7503 | } |
7504 | ||
6ba89f8e | 7505 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
7506 | the template arguments in TARG_PTR. */ |
7507 | ||
5566b478 MS |
7508 | tree |
7509 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 7510 | tree tmpl, targ_ptr; |
8d08fdba | 7511 | { |
5566b478 | 7512 | tree fndecl; |
36a117a5 MM |
7513 | tree gen_tmpl; |
7514 | tree spec; | |
5566b478 MS |
7515 | int i, len; |
7516 | struct obstack *old_fmp_obstack; | |
7517 | extern struct obstack *function_maybepermanent_obstack; | |
36a117a5 | 7518 | tree inner_args; |
5566b478 | 7519 | |
27fafc8d JM |
7520 | if (tmpl == error_mark_node) |
7521 | return error_mark_node; | |
7522 | ||
386b8a85 JM |
7523 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
7524 | ||
36a117a5 MM |
7525 | /* Check to see if we already have this specialization. */ |
7526 | spec = retrieve_specialization (tmpl, targ_ptr); | |
7527 | if (spec != NULL_TREE) | |
7528 | return spec; | |
7529 | ||
7530 | if (DECL_TEMPLATE_INFO (tmpl)) | |
386b8a85 | 7531 | { |
36a117a5 MM |
7532 | /* The TMPL is a partial instantiation. To get a full set of |
7533 | arguments we must add the arguments used to perform the | |
7534 | partial instantiation. */ | |
7535 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
7536 | targ_ptr); | |
7537 | gen_tmpl = most_general_template (tmpl); | |
7538 | ||
7539 | /* Check to see if we already have this specialization. */ | |
7540 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
75650646 MM |
7541 | if (spec != NULL_TREE) |
7542 | return spec; | |
386b8a85 | 7543 | } |
36a117a5 MM |
7544 | else |
7545 | gen_tmpl = tmpl; | |
386b8a85 | 7546 | |
5566b478 MS |
7547 | push_obstacks (&permanent_obstack, &permanent_obstack); |
7548 | old_fmp_obstack = function_maybepermanent_obstack; | |
7549 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 7550 | |
36a117a5 MM |
7551 | len = DECL_NTPARMS (gen_tmpl); |
7552 | inner_args = innermost_args (targ_ptr); | |
5566b478 MS |
7553 | i = len; |
7554 | while (i--) | |
8d08fdba | 7555 | { |
36a117a5 | 7556 | tree t = TREE_VEC_ELT (inner_args, i); |
5566b478 MS |
7557 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
7558 | { | |
7559 | tree nt = target_type (t); | |
ec255269 | 7560 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 | 7561 | { |
8251199e JM |
7562 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); |
7563 | cp_error (" trying to instantiate `%D'", gen_tmpl); | |
5566b478 MS |
7564 | fndecl = error_mark_node; |
7565 | goto out; | |
7566 | } | |
7567 | } | |
8d08fdba MS |
7568 | } |
7569 | ||
5566b478 | 7570 | /* substitute template parameters */ |
4393e105 | 7571 | fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, /*complain=*/1, gen_tmpl); |
36a117a5 MM |
7572 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
7573 | template, not the most general template. */ | |
7574 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 7575 | |
824b9a4c MS |
7576 | if (flag_external_templates) |
7577 | add_pending_template (fndecl); | |
7578 | ||
5566b478 MS |
7579 | out: |
7580 | function_maybepermanent_obstack = old_fmp_obstack; | |
7581 | pop_obstacks (); | |
8d08fdba | 7582 | |
5566b478 | 7583 | return fndecl; |
8d08fdba | 7584 | } |
5566b478 MS |
7585 | |
7586 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
7587 | |
7588 | void | |
5566b478 MS |
7589 | overload_template_name (type) |
7590 | tree type; | |
8d08fdba | 7591 | { |
5566b478 MS |
7592 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
7593 | tree decl; | |
8d08fdba | 7594 | |
5566b478 MS |
7595 | if (IDENTIFIER_CLASS_VALUE (id) |
7596 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
7597 | return; | |
8d08fdba | 7598 | |
5566b478 MS |
7599 | decl = build_decl (TYPE_DECL, id, type); |
7600 | SET_DECL_ARTIFICIAL (decl); | |
7601 | pushdecl_class_level (decl); | |
8d08fdba MS |
7602 | } |
7603 | ||
4393e105 MM |
7604 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
7605 | arguments that are being used when calling it. TARGS is a vector | |
7606 | into which the deduced template arguments are placed. | |
8d08fdba MS |
7607 | |
7608 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
7609 | all the types, and 1 for complete failure. An error message will be | |
7610 | printed only for an incomplete match. | |
7611 | ||
4393e105 MM |
7612 | If FN is a conversion operator, RETURN_TYPE is the type desired as |
7613 | the result of the conversion operator. | |
8d08fdba | 7614 | |
4393e105 | 7615 | TPARMS is a vector of template parameters. |
9f54c803 MM |
7616 | |
7617 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
7618 | template-id. | |
6467930b | 7619 | |
830bfa74 MM |
7620 | The parameter STRICT is one of: |
7621 | ||
7622 | DEDUCE_CALL: | |
7623 | We are deducing arguments for a function call, as in | |
7624 | [temp.deduct.call]. | |
7625 | ||
7626 | DEDUCE_CONV: | |
7627 | We are deducing arguments for a conversion function, as in | |
7628 | [temp.deduct.conv]. | |
7629 | ||
7630 | DEDUCE_EXACT: | |
7631 | We are deducing arguments when calculating the partial | |
7632 | ordering between specializations of function or class | |
7633 | templates, as in [temp.func.order] and [temp.class.order], | |
7634 | when doing an explicit instantiation as in [temp.explicit], | |
9f54c803 | 7635 | when determining an explicit specialization as in |
830bfa74 | 7636 | [temp.expl.spec], or when taking the address of a function |
4393e105 MM |
7637 | template, as in [temp.deduct.funcaddr]. |
7638 | ||
4393e105 | 7639 | The other arguments are as for type_unification. */ |
8d08fdba MS |
7640 | |
7641 | int | |
4393e105 | 7642 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
03017874 | 7643 | strict) |
4393e105 | 7644 | tree fn, explicit_targs, targs, args, return_type; |
830bfa74 | 7645 | unification_kind_t strict; |
386b8a85 | 7646 | { |
4393e105 MM |
7647 | tree parms; |
7648 | tree fntype; | |
adecb3f4 | 7649 | int result; |
386b8a85 | 7650 | |
4393e105 MM |
7651 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); |
7652 | ||
7653 | fntype = TREE_TYPE (fn); | |
7654 | if (explicit_targs) | |
75650646 | 7655 | { |
4393e105 MM |
7656 | /* [temp.deduct] |
7657 | ||
7658 | The specified template arguments must match the template | |
7659 | parameters in kind (i.e., type, nontype, template), and there | |
7660 | must not be more arguments than there are parameters; | |
7661 | otherwise type deduction fails. | |
7662 | ||
7663 | Nontype arguments must match the types of the corresponding | |
7664 | nontype template parameters, or must be convertible to the | |
7665 | types of the corresponding nontype parameters as specified in | |
7666 | _temp.arg.nontype_, otherwise type deduction fails. | |
7667 | ||
7668 | All references in the function type of the function template | |
7669 | to the corresponding template parameters are replaced by the | |
7670 | specified template argument values. If a substitution in a | |
7671 | template parameter or in the function type of the function | |
7672 | template results in an invalid type, type deduction fails. */ | |
7673 | int i; | |
7674 | tree converted_args; | |
75650646 | 7675 | |
4393e105 MM |
7676 | converted_args |
7677 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
7678 | explicit_targs, NULL_TREE, /*complain=*/0, | |
7679 | /*require_all_arguments=*/0)); | |
7680 | if (converted_args == error_mark_node) | |
75650646 | 7681 | return 1; |
386b8a85 | 7682 | |
4393e105 MM |
7683 | fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE); |
7684 | if (fntype == error_mark_node) | |
7685 | return 1; | |
050367a3 | 7686 | |
4393e105 MM |
7687 | /* Place the explicitly specified arguments in TARGS. */ |
7688 | for (i = 0; i < TREE_VEC_LENGTH (targs); i++) | |
7689 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); | |
75650646 | 7690 | } |
4393e105 MM |
7691 | |
7692 | parms = TYPE_ARG_TYPES (fntype); | |
386b8a85 | 7693 | |
4393e105 MM |
7694 | if (DECL_CONV_FN_P (fn)) |
7695 | { | |
7696 | /* This is a template conversion operator. Use the return types | |
edef8288 JM |
7697 | as well as the argument types. We use it instead of 'this', since |
7698 | we could be comparing conversions from different classes. */ | |
e1b3e07d MM |
7699 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), |
7700 | TREE_CHAIN (parms)); | |
7701 | args = tree_cons (NULL_TREE, return_type, TREE_CHAIN (args)); | |
4393e105 MM |
7702 | } |
7703 | ||
4393e105 MM |
7704 | /* We allow incomplete unification without an error message here |
7705 | because the standard doesn't seem to explicitly prohibit it. Our | |
7706 | callers must be ready to deal with unification failures in any | |
7707 | event. */ | |
adecb3f4 MM |
7708 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
7709 | targs, parms, args, /*subr=*/0, | |
7710 | strict, /*allow_incomplete*/1); | |
7711 | ||
7712 | if (result == 0) | |
7713 | /* All is well so far. Now, check: | |
7714 | ||
7715 | [temp.deduct] | |
7716 | ||
7717 | When all template arguments have been deduced, all uses of | |
7718 | template parameters in nondeduced contexts are replaced with | |
7719 | the corresponding deduced argument values. If the | |
7720 | substitution results in an invalid type, as described above, | |
7721 | type deduction fails. */ | |
7722 | if (tsubst (TREE_TYPE (fn), targs, /*complain=*/0, NULL_TREE) | |
7723 | == error_mark_node) | |
7724 | return 1; | |
7725 | ||
7726 | return result; | |
830bfa74 MM |
7727 | } |
7728 | ||
7729 | /* Adjust types before performing type deduction, as described in | |
7730 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
7731 | sections are symmetric. PARM is the type of a function parameter |
7732 | or the return type of the conversion function. ARG is the type of | |
7733 | the argument passed to the call, or the type of the value | |
7734 | intialized with the result of the conversion function. */ | |
386b8a85 | 7735 | |
e9659ab0 | 7736 | static void |
830bfa74 MM |
7737 | maybe_adjust_types_for_deduction (strict, parm, arg) |
7738 | unification_kind_t strict; | |
7739 | tree* parm; | |
7740 | tree* arg; | |
7741 | { | |
7742 | switch (strict) | |
7743 | { | |
7744 | case DEDUCE_CALL: | |
7745 | break; | |
7746 | ||
7747 | case DEDUCE_CONV: | |
7748 | { | |
4c7d0dff MM |
7749 | /* Swap PARM and ARG throughout the remainder of this |
7750 | function; the handling is precisely symmetric since PARM | |
7751 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
7752 | tree* temp = parm; |
7753 | parm = arg; | |
7754 | arg = temp; | |
7755 | break; | |
7756 | } | |
7757 | ||
7758 | case DEDUCE_EXACT: | |
7759 | /* There is nothing to do in this case. */ | |
7760 | return; | |
7761 | ||
7762 | default: | |
7763 | my_friendly_abort (0); | |
7764 | } | |
7765 | ||
7766 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
7767 | { | |
7768 | /* [temp.deduct.call] | |
7769 | ||
7770 | If P is not a reference type: | |
7771 | ||
7772 | --If A is an array type, the pointer type produced by the | |
7773 | array-to-pointer standard conversion (_conv.array_) is | |
7774 | used in place of A for type deduction; otherwise, | |
7775 | ||
7776 | --If A is a function type, the pointer type produced by | |
7777 | the function-to-pointer standard conversion | |
7778 | (_conv.func_) is used in place of A for type deduction; | |
7779 | otherwise, | |
7780 | ||
7781 | --If A is a cv-qualified type, the top level | |
7782 | cv-qualifiers of A's type are ignored for type | |
7783 | deduction. */ | |
7784 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
7785 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 7786 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
7787 | *arg = build_pointer_type (*arg); |
7788 | else | |
7789 | *arg = TYPE_MAIN_VARIANT (*arg); | |
7790 | } | |
7791 | ||
7792 | /* [temp.deduct.call] | |
7793 | ||
7794 | If P is a cv-qualified type, the top level cv-qualifiers | |
7795 | of P's type are ignored for type deduction. If P is a | |
7796 | reference type, the type referred to by P is used for | |
7797 | type deduction. */ | |
7798 | *parm = TYPE_MAIN_VARIANT (*parm); | |
7799 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
7800 | *parm = TREE_TYPE (*parm); | |
386b8a85 JM |
7801 | } |
7802 | ||
4393e105 | 7803 | /* Like type_unfication. |
9f54c803 MM |
7804 | |
7805 | If SUBR is 1, we're being called recursively (to unify the | |
7806 | arguments of a function or method parameter of a function | |
7807 | template). */ | |
386b8a85 | 7808 | |
4966381a | 7809 | static int |
3b3ba9f0 | 7810 | type_unification_real (tparms, targs, parms, args, subr, |
4393e105 | 7811 | strict, allow_incomplete) |
050367a3 | 7812 | tree tparms, targs, parms, args; |
830bfa74 MM |
7813 | int subr; |
7814 | unification_kind_t strict; | |
7815 | int allow_incomplete; | |
8d08fdba MS |
7816 | { |
7817 | tree parm, arg; | |
7818 | int i; | |
7819 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 7820 | int sub_strict; |
8d08fdba MS |
7821 | |
7822 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
7823 | my_friendly_assert (parms == NULL_TREE |
7824 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 7825 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
7826 | build_x_function_call). */ |
7827 | if (args) | |
7828 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
7829 | my_friendly_assert (ntparms > 0, 292); | |
7830 | ||
830bfa74 MM |
7831 | switch (strict) |
7832 | { | |
7833 | case DEDUCE_CALL: | |
7834 | sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED; | |
7835 | break; | |
7836 | ||
7837 | case DEDUCE_CONV: | |
7838 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
7839 | break; | |
7840 | ||
7841 | case DEDUCE_EXACT: | |
7842 | sub_strict = UNIFY_ALLOW_NONE; | |
7843 | break; | |
7844 | ||
7845 | default: | |
7846 | my_friendly_abort (0); | |
7847 | } | |
7848 | ||
8d08fdba MS |
7849 | while (parms |
7850 | && parms != void_list_node | |
7851 | && args | |
7852 | && args != void_list_node) | |
7853 | { | |
7854 | parm = TREE_VALUE (parms); | |
7855 | parms = TREE_CHAIN (parms); | |
7856 | arg = TREE_VALUE (args); | |
7857 | args = TREE_CHAIN (args); | |
7858 | ||
7859 | if (arg == error_mark_node) | |
7860 | return 1; | |
7861 | if (arg == unknown_type_node) | |
34016c81 JM |
7862 | /* We can't deduce anything from this, but we might get all the |
7863 | template args from other function args. */ | |
7864 | continue; | |
b7484fbe | 7865 | |
03e70705 JM |
7866 | /* Conversions will be performed on a function argument that |
7867 | corresponds with a function parameter that contains only | |
7868 | non-deducible template parameters and explicitly specified | |
7869 | template parameters. */ | |
7870 | if (! uses_template_parms (parm)) | |
b7484fbe | 7871 | { |
03e70705 JM |
7872 | tree type; |
7873 | ||
7874 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
7875 | type = TREE_TYPE (arg); | |
7876 | else | |
7877 | { | |
7878 | type = arg; | |
7879 | arg = NULL_TREE; | |
7880 | } | |
7881 | ||
830bfa74 | 7882 | if (strict == DEDUCE_EXACT) |
03e70705 | 7883 | { |
3bfdc719 | 7884 | if (same_type_p (parm, type)) |
03e70705 JM |
7885 | continue; |
7886 | } | |
03e70705 | 7887 | else |
343c89cd JM |
7888 | /* It might work; we shouldn't check now, because we might |
7889 | get into infinite recursion. Overload resolution will | |
7890 | handle it. */ | |
7891 | continue; | |
03e70705 | 7892 | |
b7484fbe MS |
7893 | return 1; |
7894 | } | |
7895 | ||
8d08fdba MS |
7896 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') |
7897 | { | |
7898 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
34016c81 | 7899 | if (type_unknown_p (arg)) |
28cbf42c | 7900 | { |
34016c81 JM |
7901 | /* [temp.deduct.type] A template-argument can be deduced from |
7902 | a pointer to function or pointer to member function | |
7903 | argument if the set of overloaded functions does not | |
7904 | contain function templates and at most one of a set of | |
7905 | overloaded functions provides a unique match. */ | |
7906 | ||
7907 | if (resolve_overloaded_unification | |
4393e105 | 7908 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
7909 | != 0) |
7910 | return 1; | |
7911 | continue; | |
28cbf42c | 7912 | } |
8d08fdba MS |
7913 | arg = TREE_TYPE (arg); |
7914 | } | |
4393e105 | 7915 | |
830bfa74 MM |
7916 | if (!subr) |
7917 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
db5ae43f | 7918 | |
4393e105 | 7919 | switch (unify (tparms, targs, parm, arg, sub_strict)) |
8d08fdba MS |
7920 | { |
7921 | case 0: | |
7922 | break; | |
7923 | case 1: | |
7924 | return 1; | |
7925 | } | |
7926 | } | |
7927 | /* Fail if we've reached the end of the parm list, and more args | |
7928 | are present, and the parm list isn't variadic. */ | |
7929 | if (args && args != void_list_node && parms == void_list_node) | |
7930 | return 1; | |
7931 | /* Fail if parms are left and they don't have default values. */ | |
7932 | if (parms | |
7933 | && parms != void_list_node | |
7934 | && TREE_PURPOSE (parms) == NULL_TREE) | |
7935 | return 1; | |
7936 | if (!subr) | |
7937 | for (i = 0; i < ntparms; i++) | |
050367a3 | 7938 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 7939 | { |
386b8a85 | 7940 | if (!allow_incomplete) |
8251199e | 7941 | error ("incomplete type unification"); |
8d08fdba MS |
7942 | return 2; |
7943 | } | |
7944 | return 0; | |
7945 | } | |
7946 | ||
34016c81 JM |
7947 | /* Subroutine of type_unification_real. Args are like the variables at the |
7948 | call site. ARG is an overloaded function (or template-id); we try | |
7949 | deducing template args from each of the overloads, and if only one | |
7950 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
7951 | ||
7952 | static int | |
7953 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
4393e105 | 7954 | sub_strict) |
34016c81 JM |
7955 | tree tparms, targs, parm, arg; |
7956 | unification_kind_t strict; | |
7957 | int sub_strict; | |
34016c81 JM |
7958 | { |
7959 | tree tempargs = copy_node (targs); | |
7960 | int good = 0; | |
7961 | ||
7962 | if (TREE_CODE (arg) == ADDR_EXPR) | |
7963 | arg = TREE_OPERAND (arg, 0); | |
9f3d9e46 | 7964 | |
d8f8dca1 MM |
7965 | if (TREE_CODE (arg) == COMPONENT_REF) |
7966 | /* Handle `&x' where `x' is some static or non-static member | |
7967 | function name. */ | |
7968 | arg = TREE_OPERAND (arg, 1); | |
7969 | ||
05e0b2f4 JM |
7970 | if (TREE_CODE (arg) == OFFSET_REF) |
7971 | arg = TREE_OPERAND (arg, 1); | |
7972 | ||
9f3d9e46 JM |
7973 | /* Strip baselink information. */ |
7974 | while (TREE_CODE (arg) == TREE_LIST) | |
7975 | arg = TREE_VALUE (arg); | |
7976 | ||
34016c81 JM |
7977 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
7978 | { | |
7979 | /* If we got some explicit template args, we need to plug them into | |
7980 | the affected templates before we try to unify, in case the | |
7981 | explicit args will completely resolve the templates in question. */ | |
7982 | ||
7983 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
7984 | arg = TREE_OPERAND (arg, 0); | |
7985 | ||
7986 | for (; arg; arg = OVL_NEXT (arg)) | |
7987 | { | |
7988 | tree fn = OVL_CURRENT (arg); | |
7989 | tree subargs, elem; | |
7990 | ||
7991 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
7992 | continue; | |
7993 | ||
7994 | subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs); | |
7995 | if (subargs) | |
7996 | { | |
4393e105 MM |
7997 | elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0, |
7998 | NULL_TREE); | |
d8f8dca1 MM |
7999 | if (TREE_CODE (elem) == METHOD_TYPE) |
8000 | elem = build_ptrmemfunc_type (build_pointer_type (elem)); | |
e97e5263 | 8001 | good += try_one_overload (tparms, targs, tempargs, parm, elem, |
4393e105 | 8002 | strict, sub_strict); |
34016c81 JM |
8003 | } |
8004 | } | |
8005 | } | |
8006 | else if (TREE_CODE (arg) == OVERLOAD) | |
8007 | { | |
8008 | for (; arg; arg = OVL_NEXT (arg)) | |
d8f8dca1 MM |
8009 | { |
8010 | tree type = TREE_TYPE (OVL_CURRENT (arg)); | |
8011 | if (TREE_CODE (type) == METHOD_TYPE) | |
8012 | type = build_ptrmemfunc_type (build_pointer_type (type)); | |
8013 | good += try_one_overload (tparms, targs, tempargs, parm, | |
8014 | type, | |
4393e105 | 8015 | strict, sub_strict); |
d8f8dca1 | 8016 | } |
34016c81 JM |
8017 | } |
8018 | else | |
8019 | my_friendly_abort (981006); | |
8020 | ||
8021 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
8022 | to function or pointer to member function argument if the set of | |
8023 | overloaded functions does not contain function templates and at most | |
8024 | one of a set of overloaded functions provides a unique match. | |
8025 | ||
8026 | So if we found multiple possibilities, we return success but don't | |
8027 | deduce anything. */ | |
8028 | ||
8029 | if (good == 1) | |
8030 | { | |
8031 | int i = TREE_VEC_LENGTH (targs); | |
8032 | for (; i--; ) | |
8033 | if (TREE_VEC_ELT (tempargs, i)) | |
8034 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
8035 | } | |
8036 | if (good) | |
8037 | return 0; | |
8038 | ||
8039 | return 1; | |
8040 | } | |
8041 | ||
8042 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
8043 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
8044 | different overloads deduce different arguments for a given parm. | |
8045 | Returns 1 on success. */ | |
8046 | ||
8047 | static int | |
e97e5263 | 8048 | try_one_overload (tparms, orig_targs, targs, parm, arg, strict, |
4393e105 | 8049 | sub_strict) |
e97e5263 | 8050 | tree tparms, orig_targs, targs, parm, arg; |
34016c81 JM |
8051 | unification_kind_t strict; |
8052 | int sub_strict; | |
34016c81 JM |
8053 | { |
8054 | int nargs; | |
8055 | tree tempargs; | |
8056 | int i; | |
8057 | ||
8058 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
8059 | to function or pointer to member function argument if the set of | |
8060 | overloaded functions does not contain function templates and at most | |
8061 | one of a set of overloaded functions provides a unique match. | |
8062 | ||
8063 | So if this is a template, just return success. */ | |
8064 | ||
8065 | if (uses_template_parms (arg)) | |
8066 | return 1; | |
8067 | ||
8068 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
8069 | ||
8070 | /* We don't copy orig_targs for this because if we have already deduced | |
8071 | some template args from previous args, unify would complain when we | |
8072 | try to deduce a template parameter for the same argument, even though | |
8073 | there isn't really a conflict. */ | |
8074 | nargs = TREE_VEC_LENGTH (targs); | |
8075 | tempargs = make_scratch_vec (nargs); | |
8076 | ||
4393e105 | 8077 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
8078 | return 0; |
8079 | ||
8080 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 8081 | explicitly specified args. */ |
34016c81 JM |
8082 | for (i = nargs; i--; ) |
8083 | { | |
8084 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 8085 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 JM |
8086 | |
8087 | if (elt == NULL_TREE) | |
8088 | continue; | |
8089 | else if (uses_template_parms (elt)) | |
8090 | { | |
8091 | /* Since we're unifying against ourselves, we will fill in template | |
8092 | args used in the function parm list with our own template parms. | |
8093 | Discard them. */ | |
8094 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
8095 | continue; | |
8096 | } | |
8097 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
8098 | return 0; | |
8099 | } | |
8100 | ||
8101 | for (i = nargs; i--; ) | |
8102 | { | |
8103 | tree elt = TREE_VEC_ELT (tempargs, i); | |
8104 | ||
8105 | if (elt) | |
8106 | TREE_VEC_ELT (targs, i) = elt; | |
8107 | } | |
8108 | ||
8109 | return 1; | |
8110 | } | |
8111 | ||
4393e105 MM |
8112 | /* PARM is a template class (perhaps with unbound template |
8113 | parameters). ARG is a fully instantiated type. If ARG can be | |
8114 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
8115 | TARGS are as for unify. */ | |
fcfb9f96 MM |
8116 | |
8117 | static tree | |
4393e105 | 8118 | try_class_unification (tparms, targs, parm, arg) |
fcfb9f96 MM |
8119 | tree tparms; |
8120 | tree targs; | |
4393e105 MM |
8121 | tree parm; |
8122 | tree arg; | |
8123 | { | |
8124 | int i; | |
8125 | tree copy_of_targs; | |
8126 | ||
8127 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
8128 | || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) | |
8129 | return NULL_TREE; | |
8130 | ||
8131 | /* We need to make a new template argument vector for the call to | |
8132 | unify. If we used TARGS, we'd clutter it up with the result of | |
8133 | the attempted unification, even if this class didn't work out. | |
8134 | We also don't want to commit ourselves to all the unifications | |
8135 | we've already done, since unification is supposed to be done on | |
8136 | an argument-by-argument basis. In other words, consider the | |
8137 | following pathological case: | |
8138 | ||
8139 | template <int I, int J, int K> | |
8140 | struct S {}; | |
8141 | ||
8142 | template <int I, int J> | |
8143 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
8144 | ||
8145 | template <int I, int J, int K> | |
8146 | void f(S<I, J, K>, S<I, I, I>); | |
8147 | ||
8148 | void g() { | |
8149 | S<0, 0, 0> s0; | |
8150 | S<0, 1, 2> s2; | |
8151 | ||
8152 | f(s0, s2); | |
8153 | } | |
8154 | ||
8155 | Now, by the time we consider the unification involving `s2', we | |
8156 | already know that we must have `f<0, 0, 0>'. But, even though | |
8157 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal | |
8158 | because there are two ways to unify base classes of S<0, 1, 2> | |
8159 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
8160 | would reject the possibility I=1. */ | |
8161 | push_momentary (); | |
8162 | copy_of_targs = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
8163 | i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), | |
8164 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE); | |
8165 | pop_momentary (); | |
8166 | ||
8167 | /* If unification failed, we're done. */ | |
8168 | if (i != 0) | |
8169 | return NULL_TREE; | |
8170 | else | |
8171 | return arg; | |
8172 | } | |
8173 | ||
8174 | /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we | |
8175 | have alreay discovered to be satisfactory. ARG_BINFO is the binfo | |
8176 | for the base class of ARG that we are currently examining. */ | |
8177 | ||
8178 | static tree | |
8179 | get_template_base_recursive (tparms, targs, parm, | |
8180 | arg_binfo, rval, flags) | |
8181 | tree tparms; | |
8182 | tree targs; | |
8183 | tree arg_binfo; | |
fcfb9f96 | 8184 | tree rval; |
4393e105 MM |
8185 | tree parm; |
8186 | int flags; | |
fcfb9f96 MM |
8187 | { |
8188 | tree binfos; | |
8189 | int i, n_baselinks; | |
4393e105 | 8190 | tree arg = BINFO_TYPE (arg_binfo); |
fcfb9f96 | 8191 | |
4393e105 | 8192 | if (!(flags & GTB_IGNORE_TYPE)) |
fcfb9f96 | 8193 | { |
4393e105 MM |
8194 | tree r = try_class_unification (tparms, targs, |
8195 | parm, arg); | |
fcfb9f96 | 8196 | |
4393e105 | 8197 | /* If there is more than one satisfactory baseclass, then: |
fcfb9f96 | 8198 | |
4393e105 MM |
8199 | [temp.deduct.call] |
8200 | ||
8201 | If they yield more than one possible deduced A, the type | |
8202 | deduction fails. | |
8203 | ||
8204 | applies. */ | |
8205 | if (r && rval && !same_type_p (r, rval)) | |
8206 | return error_mark_node; | |
8207 | else if (r) | |
8208 | rval = r; | |
fcfb9f96 MM |
8209 | } |
8210 | ||
4393e105 | 8211 | binfos = BINFO_BASETYPES (arg_binfo); |
fcfb9f96 MM |
8212 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; |
8213 | ||
8214 | /* Process base types. */ | |
8215 | for (i = 0; i < n_baselinks; i++) | |
8216 | { | |
8217 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
4393e105 | 8218 | int this_virtual; |
fcfb9f96 | 8219 | |
4393e105 MM |
8220 | /* Skip this base, if we've already seen it. */ |
8221 | if (BINFO_MARKED (base_binfo)) | |
8222 | continue; | |
8223 | ||
8224 | this_virtual = | |
8225 | (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); | |
8226 | ||
8227 | /* When searching for a non-virtual, we cannot mark virtually | |
8228 | found binfos. */ | |
8229 | if (! this_virtual) | |
8230 | SET_BINFO_MARKED (base_binfo); | |
8231 | ||
8232 | rval = get_template_base_recursive (tparms, targs, | |
8233 | parm, | |
8234 | base_binfo, | |
8235 | rval, | |
8236 | GTB_VIA_VIRTUAL * this_virtual); | |
8237 | ||
8238 | /* If we discovered more than one matching base class, we can | |
8239 | stop now. */ | |
8240 | if (rval == error_mark_node) | |
8241 | return error_mark_node; | |
fcfb9f96 MM |
8242 | } |
8243 | ||
8244 | return rval; | |
8245 | } | |
8246 | ||
4393e105 MM |
8247 | /* Given a template type PARM and a class type ARG, find the unique |
8248 | base type in ARG that is an instance of PARM. We do not examine | |
8249 | ARG itself; only its base-classes. If there is no appropriate base | |
8250 | class, return NULL_TREE. If there is more than one, return | |
8251 | error_mark_node. PARM may be the type of a partial specialization, | |
8252 | as well as a plain template type. Used by unify. */ | |
fcfb9f96 MM |
8253 | |
8254 | static tree | |
4393e105 | 8255 | get_template_base (tparms, targs, parm, arg) |
fcfb9f96 MM |
8256 | tree tparms; |
8257 | tree targs; | |
4393e105 MM |
8258 | tree parm; |
8259 | tree arg; | |
fcfb9f96 | 8260 | { |
4393e105 MM |
8261 | tree rval; |
8262 | tree arg_binfo; | |
fcfb9f96 | 8263 | |
4393e105 MM |
8264 | my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); |
8265 | ||
8266 | arg_binfo = TYPE_BINFO (complete_type (arg)); | |
8267 | rval = get_template_base_recursive (tparms, targs, | |
8268 | parm, arg_binfo, | |
8269 | NULL_TREE, | |
8270 | GTB_IGNORE_TYPE); | |
fcfb9f96 | 8271 | |
4393e105 MM |
8272 | /* Since get_template_base_recursive marks the bases classes, we |
8273 | must unmark them here. */ | |
d6479fe7 | 8274 | dfs_walk (arg_binfo, dfs_unmark, markedp, 0); |
fcfb9f96 MM |
8275 | |
8276 | return rval; | |
8277 | } | |
8278 | ||
db2767b6 MM |
8279 | /* Returns the level of DECL, which declares a template parameter. */ |
8280 | ||
e9659ab0 | 8281 | static int |
db2767b6 MM |
8282 | template_decl_level (decl) |
8283 | tree decl; | |
8284 | { | |
8285 | switch (TREE_CODE (decl)) | |
8286 | { | |
8287 | case TYPE_DECL: | |
8288 | case TEMPLATE_DECL: | |
8289 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
8290 | ||
8291 | case PARM_DECL: | |
8292 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
8293 | ||
8294 | default: | |
8295 | my_friendly_abort (0); | |
5ffe581d | 8296 | return 0; |
db2767b6 MM |
8297 | } |
8298 | } | |
8299 | ||
830bfa74 MM |
8300 | /* Decide whether ARG can be unified with PARM, considering only the |
8301 | cv-qualifiers of each type, given STRICT as documented for unify. | |
8302 | Returns non-zero iff the unification is OK on that basis.*/ | |
e92cc029 | 8303 | |
e9659ab0 | 8304 | static int |
830bfa74 MM |
8305 | check_cv_quals_for_unify (strict, arg, parm) |
8306 | int strict; | |
8307 | tree arg; | |
8308 | tree parm; | |
8309 | { | |
ef637255 MM |
8310 | if (!(strict & UNIFY_ALLOW_MORE_CV_QUAL) |
8311 | && !at_least_as_qualified_p (arg, parm)) | |
8312 | return 0; | |
8313 | ||
8314 | if (!(strict & UNIFY_ALLOW_LESS_CV_QUAL) | |
8315 | && !at_least_as_qualified_p (parm, arg)) | |
8316 | return 0; | |
8317 | ||
ef637255 | 8318 | return 1; |
830bfa74 MM |
8319 | } |
8320 | ||
8321 | /* Takes parameters as for type_unification. Returns 0 if the | |
8322 | type deduction suceeds, 1 otherwise. The parameter STRICT is a | |
8323 | bitwise or of the following flags: | |
8324 | ||
8325 | UNIFY_ALLOW_NONE: | |
8326 | Require an exact match between PARM and ARG. | |
8327 | UNIFY_ALLOW_MORE_CV_QUAL: | |
8328 | Allow the deduced ARG to be more cv-qualified than ARG. | |
8329 | UNIFY_ALLOW_LESS_CV_QUAL: | |
8330 | Allow the deduced ARG to be less cv-qualified than ARG. | |
8331 | UNIFY_ALLOW_DERIVED: | |
8332 | Allow the deduced ARG to be a template base class of ARG, | |
8333 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
8334 | ARG. |
8335 | UNIFY_ALLOW_INTEGER: | |
8336 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
8337 | case for more information. */ | |
830bfa74 | 8338 | |
e9659ab0 | 8339 | static int |
4393e105 | 8340 | unify (tparms, targs, parm, arg, strict) |
050367a3 | 8341 | tree tparms, targs, parm, arg; |
830bfa74 | 8342 | int strict; |
8d08fdba MS |
8343 | { |
8344 | int idx; | |
050367a3 | 8345 | tree targ; |
db2767b6 | 8346 | tree tparm; |
8d08fdba MS |
8347 | |
8348 | /* I don't think this will do the right thing with respect to types. | |
8349 | But the only case I've seen it in so far has been array bounds, where | |
8350 | signedness is the only information lost, and I think that will be | |
8351 | okay. */ | |
8352 | while (TREE_CODE (parm) == NOP_EXPR) | |
8353 | parm = TREE_OPERAND (parm, 0); | |
8354 | ||
8355 | if (arg == error_mark_node) | |
8356 | return 1; | |
8357 | if (arg == unknown_type_node) | |
34016c81 JM |
8358 | /* We can't deduce anything from this, but we might get all the |
8359 | template args from other function args. */ | |
8360 | return 0; | |
8361 | ||
db2767b6 | 8362 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 8363 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
8364 | template parameters. We might need them if we're trying to |
8365 | figure out which of two things is more specialized. */ | |
8366 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
8367 | return 0; |
8368 | ||
830bfa74 MM |
8369 | /* Immediately reject some pairs that won't unify because of |
8370 | cv-qualification mismatches. */ | |
8371 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
49432171 | 8372 | && TREE_CODE_CLASS (TREE_CODE (arg)) == 't' |
830bfa74 MM |
8373 | /* We check the cv-qualifiers when unifying with template type |
8374 | parameters below. We want to allow ARG `const T' to unify with | |
8375 | PARM `T' for example, when computing which of two templates | |
8376 | is more specialized, for example. */ | |
8377 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
8378 | && !check_cv_quals_for_unify (strict, arg, parm)) | |
49432171 JM |
8379 | return 1; |
8380 | ||
8d08fdba MS |
8381 | switch (TREE_CODE (parm)) |
8382 | { | |
2ca340ae JM |
8383 | case TYPENAME_TYPE: |
8384 | /* In a type which contains a nested-name-specifier, template | |
8385 | argument values cannot be deduced for template parameters used | |
8386 | within the nested-name-specifier. */ | |
8387 | return 0; | |
8388 | ||
8d08fdba | 8389 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 8390 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
8391 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8392 | ||
8393 | if (TEMPLATE_TYPE_LEVEL (parm) | |
8394 | != template_decl_level (tparm)) | |
8395 | /* The PARM is not one we're trying to unify. Just check | |
8396 | to see if it matches ARG. */ | |
8397 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 8398 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 8399 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 8400 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8401 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 8402 | |
73b0fce8 | 8403 | /* Check for mixed types and values. */ |
db2767b6 MM |
8404 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
8405 | && TREE_CODE (tparm) != TYPE_DECL) | |
8406 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
8407 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
8408 | return 1; |
8409 | ||
db2767b6 | 8410 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 8411 | { |
7ddedda4 | 8412 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)) |
db2767b6 MM |
8413 | { |
8414 | /* We arrive here when PARM does not involve template | |
8415 | specialization. */ | |
73b0fce8 | 8416 | |
db2767b6 MM |
8417 | /* ARG must be constructed from a template class. */ |
8418 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
8419 | return 1; | |
73b0fce8 | 8420 | |
db2767b6 | 8421 | { |
7ddedda4 MM |
8422 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); |
8423 | tree parmvec = TYPE_TI_ARGS (parm); | |
db2767b6 MM |
8424 | tree argvec = CLASSTYPE_TI_ARGS (arg); |
8425 | tree argtmplvec | |
8426 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
8427 | int i; | |
8428 | ||
8429 | /* The parameter and argument roles have to be switched here | |
8430 | in order to handle default arguments properly. For example, | |
8431 | template<template <class> class TT> void f(TT<int>) | |
8432 | should be able to accept vector<int> which comes from | |
744fac59 | 8433 | template <class T, class Allocator = allocator> |
73b0fce8 KL |
8434 | class vector. */ |
8435 | ||
fcfcdfc8 | 8436 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) |
db2767b6 MM |
8437 | == error_mark_node) |
8438 | return 1; | |
73b0fce8 | 8439 | |
6b9b6b15 JM |
8440 | /* Deduce arguments T, i from TT<T> or TT<i>. |
8441 | We check each element of PARMVEC and ARGVEC individually | |
8442 | rather than the whole TREE_VEC since they can have | |
8443 | different number of elements. */ | |
8444 | ||
db2767b6 MM |
8445 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
8446 | { | |
8447 | tree t = TREE_VEC_ELT (parmvec, i); | |
73b0fce8 | 8448 | |
830bfa74 MM |
8449 | if (unify (tparms, targs, t, |
8450 | TREE_VEC_ELT (argvec, i), | |
4393e105 | 8451 | UNIFY_ALLOW_NONE)) |
db2767b6 MM |
8452 | return 1; |
8453 | } | |
73b0fce8 | 8454 | } |
db2767b6 MM |
8455 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
8456 | } | |
8457 | } | |
8458 | else | |
8459 | { | |
830bfa74 MM |
8460 | /* If PARM is `const T' and ARG is only `int', we don't have |
8461 | a match unless we are allowing additional qualification. | |
8462 | If ARG is `const int' and PARM is just `T' that's OK; | |
8463 | that binds `const int' to `T'. */ | |
8464 | if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, | |
8465 | arg, parm)) | |
db2767b6 MM |
8466 | return 1; |
8467 | ||
830bfa74 MM |
8468 | /* Consider the case where ARG is `const volatile int' and |
8469 | PARM is `const T'. Then, T should be `volatile int'. */ | |
8470 | arg = | |
adecb3f4 MM |
8471 | cp_build_qualified_type_real (arg, |
8472 | CP_TYPE_QUALS (arg) | |
8473 | & ~CP_TYPE_QUALS (parm), | |
8474 | /*complain=*/0); | |
8475 | if (arg == error_mark_node) | |
8476 | return 1; | |
73b0fce8 KL |
8477 | } |
8478 | ||
8479 | /* Simple cases: Value already set, does match or doesn't. */ | |
4393e105 | 8480 | if (targ != NULL_TREE && same_type_p (targ, arg)) |
73b0fce8 | 8481 | return 0; |
050367a3 | 8482 | else if (targ) |
73b0fce8 | 8483 | return 1; |
61cd552e MM |
8484 | |
8485 | /* Make sure that ARG is not a variable-sized array. (Note that | |
8486 | were talking about variable-sized arrays (like `int[n]'), | |
8487 | rather than arrays of unknown size (like `int[]').) We'll | |
8488 | get very confused by such a type since the bound of the array | |
8489 | will not be computable in an instantiation. Besides, such | |
8490 | types are not allowed in ISO C++, so we can do as we please | |
8491 | here. */ | |
8492 | if (TREE_CODE (arg) == ARRAY_TYPE | |
8493 | && !uses_template_parms (arg) | |
8494 | && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg))) | |
8495 | != INTEGER_CST)) | |
8496 | return 1; | |
8497 | ||
050367a3 | 8498 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
8499 | return 0; |
8500 | ||
f84b4be9 | 8501 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
8502 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8503 | ||
8504 | if (TEMPLATE_PARM_LEVEL (parm) | |
8505 | != template_decl_level (tparm)) | |
8506 | /* The PARM is not one we're trying to unify. Just check | |
8507 | to see if it matches ARG. */ | |
8508 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
67ffc812 | 8509 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; |
db2767b6 | 8510 | |
f84b4be9 | 8511 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 8512 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8513 | |
050367a3 | 8514 | if (targ) |
8d08fdba | 8515 | { |
67ffc812 | 8516 | int i = (cp_tree_equal (targ, arg) > 0); |
312e7d50 JM |
8517 | if (i == 1) |
8518 | return 0; | |
8519 | else if (i == 0) | |
8520 | return 1; | |
8521 | else | |
8522 | my_friendly_abort (42); | |
8d08fdba | 8523 | } |
8d08fdba | 8524 | |
161c12b0 JM |
8525 | /* [temp.deduct.type] If, in the declaration of a function template |
8526 | with a non-type template-parameter, the non-type | |
8527 | template-parameter is used in an expression in the function | |
8528 | parameter-list and, if the corresponding template-argument is | |
8529 | deduced, the template-argument type shall match the type of the | |
8530 | template-parameter exactly, except that a template-argument | |
8531 | deduced from an array bound may be of any integral type. */ | |
8532 | if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm))) | |
8533 | /* OK */; | |
8534 | else if ((strict & UNIFY_ALLOW_INTEGER) | |
8535 | && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE | |
8536 | || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE)) | |
8537 | /* OK */; | |
8538 | else | |
8539 | return 1; | |
8540 | ||
2a1e9fdd | 8541 | TREE_VEC_ELT (targs, idx) = arg; |
8d08fdba MS |
8542 | return 0; |
8543 | ||
8544 | case POINTER_TYPE: | |
830bfa74 MM |
8545 | { |
8546 | int sub_strict; | |
4ac14744 | 8547 | |
830bfa74 MM |
8548 | if (TREE_CODE (arg) != POINTER_TYPE) |
8549 | return 1; | |
8550 | ||
8551 | /* [temp.deduct.call] | |
8552 | ||
8553 | A can be another pointer or pointer to member type that can | |
8554 | be converted to the deduced A via a qualification | |
8555 | conversion (_conv.qual_). | |
8556 | ||
8557 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
8558 | This will allow for additional cv-qualification of the | |
8559 | pointed-to types if appropriate. In general, this is a bit | |
8560 | too generous; we are only supposed to allow qualification | |
8561 | conversions and this method will allow an ARG of char** and | |
8562 | a deduced ARG of const char**. However, overload | |
8563 | resolution will subsequently invalidate the candidate, so | |
8564 | this is probably OK. */ | |
8565 | sub_strict = strict; | |
8566 | ||
a7a64a77 | 8567 | if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE) |
830bfa74 MM |
8568 | /* The derived-to-base conversion only persists through one |
8569 | level of pointers. */ | |
8570 | sub_strict &= ~UNIFY_ALLOW_DERIVED; | |
8571 | ||
a7a64a77 MM |
8572 | return unify (tparms, targs, TREE_TYPE (parm), |
8573 | TREE_TYPE (arg), sub_strict); | |
830bfa74 | 8574 | } |
8d08fdba MS |
8575 | |
8576 | case REFERENCE_TYPE: | |
830bfa74 MM |
8577 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
8578 | return 1; | |
8579 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
4393e105 | 8580 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8581 | |
8582 | case ARRAY_TYPE: | |
8583 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
8584 | return 1; | |
3042d5be MM |
8585 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
8586 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
8587 | return 1; | |
8588 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
830bfa74 | 8589 | && unify (tparms, targs, TYPE_DOMAIN (parm), |
4393e105 | 8590 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) |
8d08fdba | 8591 | return 1; |
830bfa74 | 8592 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8593 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8594 | |
8595 | case REAL_TYPE: | |
37c46b43 | 8596 | case COMPLEX_TYPE: |
8d08fdba | 8597 | case INTEGER_TYPE: |
42976354 | 8598 | case BOOLEAN_TYPE: |
5ad5a526 | 8599 | case VOID_TYPE: |
f376e137 MS |
8600 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8601 | return 1; | |
8602 | ||
514a1f18 JM |
8603 | if (TREE_CODE (parm) == INTEGER_TYPE |
8604 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
8d08fdba MS |
8605 | { |
8606 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
830bfa74 | 8607 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), |
4393e105 | 8608 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8609 | return 1; |
8610 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
830bfa74 | 8611 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), |
4393e105 | 8612 | TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8613 | return 1; |
8614 | } | |
514a1f18 JM |
8615 | /* We use the TYPE_MAIN_VARIANT since we have already |
8616 | checked cv-qualification at the top of the | |
8617 | function. */ | |
3bfdc719 MM |
8618 | else if (!same_type_p (TYPE_MAIN_VARIANT (arg), |
8619 | TYPE_MAIN_VARIANT (parm))) | |
ca79f85d JM |
8620 | return 1; |
8621 | ||
8d08fdba MS |
8622 | /* As far as unification is concerned, this wins. Later checks |
8623 | will invalidate it if necessary. */ | |
8624 | return 0; | |
8625 | ||
8626 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 8627 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 8628 | case INTEGER_CST: |
bd6dd845 MS |
8629 | while (TREE_CODE (arg) == NOP_EXPR) |
8630 | arg = TREE_OPERAND (arg, 0); | |
8631 | ||
8d08fdba MS |
8632 | if (TREE_CODE (arg) != INTEGER_CST) |
8633 | return 1; | |
8634 | return !tree_int_cst_equal (parm, arg); | |
8635 | ||
8d08fdba MS |
8636 | case TREE_VEC: |
8637 | { | |
8638 | int i; | |
8639 | if (TREE_CODE (arg) != TREE_VEC) | |
8640 | return 1; | |
8641 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
8642 | return 1; | |
8643 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
830bfa74 | 8644 | if (unify (tparms, targs, |
8d08fdba | 8645 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 8646 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
8647 | return 1; |
8648 | return 0; | |
8649 | } | |
8650 | ||
8d08fdba | 8651 | case RECORD_TYPE: |
f181d4ae | 8652 | case UNION_TYPE: |
f181d4ae | 8653 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 8654 | return 1; |
830bfa74 | 8655 | |
a7a64a77 MM |
8656 | if (TYPE_PTRMEMFUNC_P (parm)) |
8657 | { | |
8658 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
8659 | return 1; | |
8660 | ||
8661 | return unify (tparms, targs, | |
8662 | TYPE_PTRMEMFUNC_FN_TYPE (parm), | |
8663 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
8664 | strict); | |
8665 | } | |
8666 | ||
5db698f6 | 8667 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 8668 | { |
6467930b | 8669 | tree t = NULL_TREE; |
4393e105 | 8670 | |
830bfa74 | 8671 | if (strict & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
8672 | { |
8673 | /* First, we try to unify the PARM and ARG directly. */ | |
8674 | t = try_class_unification (tparms, targs, | |
8675 | parm, arg); | |
8676 | ||
8677 | if (!t) | |
8678 | { | |
8679 | /* Fallback to the special case allowed in | |
8680 | [temp.deduct.call]: | |
8681 | ||
8682 | If P is a class, and P has the form | |
8683 | template-id, then A can be a derived class of | |
8684 | the deduced A. Likewise, if P is a pointer to | |
8685 | a class of the form template-id, A can be a | |
8686 | pointer to a derived class pointed to by the | |
8687 | deduced A. */ | |
8688 | t = get_template_base (tparms, targs, | |
8689 | parm, arg); | |
8690 | ||
8691 | if (! t || t == error_mark_node) | |
8692 | return 1; | |
8693 | } | |
8694 | } | |
6df47b06 | 8695 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
8696 | && (CLASSTYPE_TI_TEMPLATE (parm) |
8697 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
8698 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
8699 | Then, we should unify `int' and `U'. */ | |
6467930b | 8700 | t = arg; |
4393e105 MM |
8701 | else |
8702 | /* There's no chance of unication succeeding. */ | |
5566b478 | 8703 | return 1; |
6467930b | 8704 | |
830bfa74 | 8705 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 8706 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 8707 | } |
3bfdc719 MM |
8708 | else if (!same_type_p (TYPE_MAIN_VARIANT (parm), |
8709 | TYPE_MAIN_VARIANT (arg))) | |
5566b478 | 8710 | return 1; |
a4443a08 | 8711 | return 0; |
8d08fdba MS |
8712 | |
8713 | case METHOD_TYPE: | |
8d08fdba | 8714 | case FUNCTION_TYPE: |
830bfa74 | 8715 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 8716 | return 1; |
830bfa74 MM |
8717 | |
8718 | if (unify (tparms, targs, TREE_TYPE (parm), | |
4393e105 | 8719 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 8720 | return 1; |
386b8a85 | 8721 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 8722 | TYPE_ARG_TYPES (arg), 1, |
4393e105 | 8723 | DEDUCE_EXACT, 0); |
a4443a08 MS |
8724 | |
8725 | case OFFSET_TYPE: | |
8726 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
8727 | return 1; | |
830bfa74 | 8728 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 8729 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 8730 | return 1; |
830bfa74 | 8731 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8732 | strict); |
a4443a08 | 8733 | |
f62dbf03 | 8734 | case CONST_DECL: |
830bfa74 | 8735 | if (arg != decl_constant_value (parm)) |
f62dbf03 JM |
8736 | return 1; |
8737 | return 0; | |
8738 | ||
027905b4 KL |
8739 | case TEMPLATE_DECL: |
8740 | /* Matched cases are handled by the ARG == PARM test above. */ | |
8741 | return 1; | |
8742 | ||
53929c47 JM |
8743 | case MINUS_EXPR: |
8744 | if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST) | |
8745 | { | |
8746 | /* We handle this case specially, since it comes up with | |
8747 | arrays. In particular, something like: | |
8748 | ||
8749 | template <int N> void f(int (&x)[N]); | |
8750 | ||
8751 | Here, we are trying to unify the range type, which | |
8752 | looks like [0 ... (N - 1)]. */ | |
8753 | tree t, t1, t2; | |
8754 | t1 = TREE_OPERAND (parm, 0); | |
8755 | t2 = TREE_OPERAND (parm, 1); | |
8756 | ||
8757 | /* Should this be a regular fold? */ | |
8758 | t = maybe_fold_nontype_arg (build (PLUS_EXPR, | |
8759 | integer_type_node, | |
8760 | arg, t2)); | |
8761 | ||
4393e105 | 8762 | return unify (tparms, targs, t1, t, strict); |
53929c47 JM |
8763 | } |
8764 | /* else fall through */ | |
8765 | ||
8d08fdba | 8766 | default: |
050367a3 | 8767 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
4393e105 MM |
8768 | /* We're looking at an expression. This can happen with |
8769 | something like: | |
8770 | ||
8771 | template <int I> | |
8772 | void foo(S<I>, S<I + 2>); | |
050367a3 | 8773 | |
4393e105 | 8774 | This is a "nondeduced context": |
050367a3 | 8775 | |
4393e105 MM |
8776 | [deduct.type] |
8777 | ||
8778 | The nondeduced contexts are: | |
050367a3 | 8779 | |
4393e105 MM |
8780 | --A type that is a template-id in which one or more of |
8781 | the template-arguments is an expression that references | |
8782 | a template-parameter. | |
050367a3 | 8783 | |
4393e105 MM |
8784 | In these cases, we assume deduction succeeded, but don't |
8785 | actually infer any unifications. */ | |
8786 | return 0; | |
050367a3 | 8787 | else |
b4c4a9ec MM |
8788 | sorry ("use of `%s' in template type unification", |
8789 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 8790 | |
8d08fdba MS |
8791 | return 1; |
8792 | } | |
8793 | } | |
8d08fdba | 8794 | \f |
03d0f4af MM |
8795 | /* Called if RESULT is explicitly instantiated, or is a member of an |
8796 | explicitly instantiated class, or if using -frepo and the | |
8797 | instantiation of RESULT has been assigned to this file. */ | |
8798 | ||
faae18ab | 8799 | void |
5566b478 | 8800 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
8801 | tree result; |
8802 | int extern_p; | |
8803 | { | |
75650646 MM |
8804 | if (TREE_CODE (result) != FUNCTION_DECL) |
8805 | /* The TREE_PUBLIC flag for function declarations will have been | |
8806 | set correctly by tsubst. */ | |
8807 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
8808 | |
8809 | if (! extern_p) | |
8810 | { | |
8811 | DECL_INTERFACE_KNOWN (result) = 1; | |
8812 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 | 8813 | |
1a408d07 JM |
8814 | /* Always make artificials weak. */ |
8815 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
8816 | comdat_linkage (result); | |
a7d87521 JM |
8817 | /* For WIN32 we also want to put explicit instantiations in |
8818 | linkonce sections. */ | |
1a408d07 | 8819 | else if (TREE_PUBLIC (result)) |
b385c841 | 8820 | maybe_make_one_only (result); |
faae18ab | 8821 | } |
f49422da MS |
8822 | else if (TREE_CODE (result) == FUNCTION_DECL) |
8823 | mark_inline_for_output (result); | |
faae18ab MS |
8824 | } |
8825 | ||
e1467ff2 MM |
8826 | /* Given two function templates PAT1 and PAT2, and explicit template |
8827 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
8828 | |
8829 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
8830 | -1 if PAT2 is more specialized than PAT1. | |
8831 | 0 if neither is more specialized. */ | |
8832 | ||
8833 | int | |
e1467ff2 MM |
8834 | more_specialized (pat1, pat2, explicit_args) |
8835 | tree pat1, pat2, explicit_args; | |
6467930b | 8836 | { |
98c1c668 | 8837 | tree targs; |
73aad9b9 | 8838 | int winner = 0; |
6467930b | 8839 | |
4393e105 | 8840 | targs = get_bindings_overload (pat1, DECL_RESULT (pat2), explicit_args); |
73aad9b9 | 8841 | if (targs) |
36a117a5 | 8842 | --winner; |
6467930b | 8843 | |
4393e105 | 8844 | targs = get_bindings_overload (pat2, DECL_RESULT (pat1), explicit_args); |
73aad9b9 | 8845 | if (targs) |
36a117a5 | 8846 | ++winner; |
6467930b | 8847 | |
73aad9b9 JM |
8848 | return winner; |
8849 | } | |
6467930b | 8850 | |
73aad9b9 | 8851 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 8852 | |
73aad9b9 JM |
8853 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
8854 | -1 if PAT2 is more specialized than PAT1. | |
8855 | 0 if neither is more specialized. */ | |
8856 | ||
8857 | int | |
8858 | more_specialized_class (pat1, pat2) | |
8859 | tree pat1, pat2; | |
8860 | { | |
8861 | tree targs; | |
8862 | int winner = 0; | |
8863 | ||
36a117a5 MM |
8864 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
8865 | TREE_PURPOSE (pat2)); | |
73aad9b9 JM |
8866 | if (targs) |
8867 | --winner; | |
8868 | ||
36a117a5 MM |
8869 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
8870 | TREE_PURPOSE (pat1)); | |
73aad9b9 | 8871 | if (targs) |
6467930b MS |
8872 | ++winner; |
8873 | ||
8874 | return winner; | |
8875 | } | |
73aad9b9 JM |
8876 | |
8877 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 8878 | DECL from the function template FN, with the explicit template |
76b9a14d | 8879 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
4393e105 MM |
8880 | also match. Return NULL_TREE if no satisfactory arguments could be |
8881 | found. */ | |
73aad9b9 | 8882 | |
76b9a14d JM |
8883 | static tree |
8884 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 8885 | tree fn, decl, explicit_args; |
76b9a14d | 8886 | int check_rettype; |
73aad9b9 | 8887 | { |
98c1c668 | 8888 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 8889 | tree targs = make_scratch_vec (ntparms); |
4393e105 | 8890 | tree decl_type; |
03017874 | 8891 | tree decl_arg_types; |
98c1c668 JM |
8892 | int i; |
8893 | ||
4393e105 MM |
8894 | /* Substitute the explicit template arguments into the type of DECL. |
8895 | The call to fn_type_unification will handle substitution into the | |
8896 | FN. */ | |
8897 | decl_type = TREE_TYPE (decl); | |
8898 | if (explicit_args && uses_template_parms (decl_type)) | |
8899 | { | |
8900 | tree tmpl; | |
8901 | tree converted_args; | |
8902 | ||
8903 | if (DECL_TEMPLATE_INFO (decl)) | |
8904 | tmpl = DECL_TI_TEMPLATE (decl); | |
8905 | else | |
8906 | /* We can get here for some illegal specializations. */ | |
8907 | return NULL_TREE; | |
8908 | ||
8909 | converted_args | |
8910 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
8911 | explicit_args, NULL_TREE, | |
8912 | /*complain=*/0, | |
8913 | /*require_all_arguments=*/0)); | |
8914 | if (converted_args == error_mark_node) | |
8915 | return NULL_TREE; | |
8916 | ||
8917 | decl_type = tsubst (decl_type, converted_args, /*complain=*/0, | |
8918 | NULL_TREE); | |
8919 | if (decl_type == error_mark_node) | |
8920 | return NULL_TREE; | |
8921 | } | |
8922 | ||
03017874 MM |
8923 | /* If FN is a static member function, adjust the type of DECL |
8924 | appropriately. */ | |
4393e105 | 8925 | decl_arg_types = TYPE_ARG_TYPES (decl_type); |
d7684f2d MM |
8926 | if (DECL_STATIC_FUNCTION_P (fn) |
8927 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 8928 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 8929 | |
e1467ff2 | 8930 | i = fn_type_unification (fn, explicit_args, targs, |
03017874 MM |
8931 | decl_arg_types, |
8932 | TREE_TYPE (decl_type), | |
8933 | DEDUCE_EXACT); | |
98c1c668 | 8934 | |
76b9a14d JM |
8935 | if (i != 0) |
8936 | return NULL_TREE; | |
8937 | ||
8938 | if (check_rettype) | |
e1467ff2 MM |
8939 | { |
8940 | /* Check to see that the resulting return type is also OK. */ | |
36a117a5 | 8941 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs, |
4393e105 | 8942 | /*complain=*/0, NULL_TREE); |
e1467ff2 | 8943 | |
3bfdc719 | 8944 | if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl)))) |
e1467ff2 | 8945 | return NULL_TREE; |
e1467ff2 MM |
8946 | } |
8947 | ||
76b9a14d JM |
8948 | return targs; |
8949 | } | |
8950 | ||
8951 | /* For most uses, we want to check the return type. */ | |
8952 | ||
8953 | tree | |
8954 | get_bindings (fn, decl, explicit_args) | |
8955 | tree fn, decl, explicit_args; | |
8956 | { | |
8957 | return get_bindings_real (fn, decl, explicit_args, 1); | |
8958 | } | |
8959 | ||
8960 | /* But for more_specialized, we only care about the parameter types. */ | |
8961 | ||
8962 | static tree | |
8963 | get_bindings_overload (fn, decl, explicit_args) | |
8964 | tree fn, decl, explicit_args; | |
8965 | { | |
8966 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
8967 | } |
8968 | ||
36a117a5 MM |
8969 | /* Return the innermost template arguments that, when applied to a |
8970 | template specialization whose innermost template parameters are | |
8971 | TPARMS, and whose specialization arguments are ARGS, yield the | |
8972 | ARGS. | |
8973 | ||
8974 | For example, suppose we have: | |
8975 | ||
8976 | template <class T, class U> struct S {}; | |
8977 | template <class T> struct S<T*, int> {}; | |
8978 | ||
8979 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
8980 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
8981 | int}. The resulting vector will be {double}, indicating that `T' | |
8982 | is bound to `double'. */ | |
8983 | ||
bd6dd845 | 8984 | static tree |
36a117a5 MM |
8985 | get_class_bindings (tparms, parms, args) |
8986 | tree tparms, parms, args; | |
73aad9b9 | 8987 | { |
3b3ba9f0 | 8988 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
73aad9b9 JM |
8989 | tree vec = make_temp_vec (ntparms); |
8990 | ||
36a117a5 | 8991 | args = innermost_args (args); |
8d019cef | 8992 | |
4393e105 | 8993 | if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE)) |
fcfb9f96 | 8994 | return NULL_TREE; |
73aad9b9 JM |
8995 | |
8996 | for (i = 0; i < ntparms; ++i) | |
8997 | if (! TREE_VEC_ELT (vec, i)) | |
8998 | return NULL_TREE; | |
8999 | ||
9000 | return vec; | |
9001 | } | |
9002 | ||
104bf76a MM |
9003 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. |
9004 | Pick the most specialized template, and return the corresponding | |
9005 | instantiation, or if there is no corresponding instantiation, the | |
9006 | template itself. EXPLICIT_ARGS is any template arguments explicity | |
9007 | mentioned in a template-id. If there is no most specialized | |
9008 | tempalte, error_mark_node is returned. If there are no templates | |
9009 | at all, NULL_TREE is returned. */ | |
73aad9b9 JM |
9010 | |
9011 | tree | |
104bf76a MM |
9012 | most_specialized_instantiation (instantiations, explicit_args) |
9013 | tree instantiations; | |
9014 | tree explicit_args; | |
73aad9b9 | 9015 | { |
104bf76a | 9016 | tree fn, champ; |
73aad9b9 JM |
9017 | int fate; |
9018 | ||
104bf76a | 9019 | if (!instantiations) |
73aad9b9 JM |
9020 | return NULL_TREE; |
9021 | ||
104bf76a MM |
9022 | champ = instantiations; |
9023 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 9024 | { |
104bf76a MM |
9025 | fate = more_specialized (TREE_VALUE (champ), |
9026 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
9027 | if (fate == 1) |
9028 | ; | |
9029 | else | |
9030 | { | |
9031 | if (fate == 0) | |
9032 | { | |
9033 | fn = TREE_CHAIN (fn); | |
9034 | if (! fn) | |
9035 | return error_mark_node; | |
9036 | } | |
104bf76a | 9037 | champ = fn; |
73aad9b9 JM |
9038 | } |
9039 | } | |
9040 | ||
104bf76a | 9041 | for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 9042 | { |
104bf76a MM |
9043 | fate = more_specialized (TREE_VALUE (champ), |
9044 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
9045 | if (fate != 1) |
9046 | return error_mark_node; | |
9047 | } | |
9048 | ||
104bf76a MM |
9049 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); |
9050 | } | |
9051 | ||
9052 | /* Return the most specialized of the list of templates in FNS that can | |
9053 | produce an instantiation matching DECL, given the explicit template | |
9054 | arguments EXPLICIT_ARGS. */ | |
9055 | ||
e9659ab0 | 9056 | static tree |
104bf76a MM |
9057 | most_specialized (fns, decl, explicit_args) |
9058 | tree fns, decl, explicit_args; | |
9059 | { | |
9060 | tree candidates = NULL_TREE; | |
9061 | tree fn, args; | |
9062 | ||
9063 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
9064 | { | |
9065 | tree candidate = TREE_VALUE (fn); | |
9066 | ||
9067 | args = get_bindings (candidate, decl, explicit_args); | |
9068 | if (args) | |
e1b3e07d | 9069 | candidates = tree_cons (NULL_TREE, candidate, candidates); |
104bf76a MM |
9070 | } |
9071 | ||
9072 | return most_specialized_instantiation (candidates, explicit_args); | |
73aad9b9 JM |
9073 | } |
9074 | ||
36a117a5 MM |
9075 | /* If DECL is a specialization of some template, return the most |
9076 | general such template. For example, given: | |
9077 | ||
9078 | template <class T> struct S { template <class U> void f(U); }; | |
9079 | ||
9080 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
9081 | the full template. This function will not trace past partial | |
9082 | specializations, however. For example, given in addition: | |
9083 | ||
9084 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
9085 | ||
9086 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
9087 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 9088 | |
e9659ab0 | 9089 | static tree |
36a117a5 MM |
9090 | most_general_template (decl) |
9091 | tree decl; | |
73aad9b9 | 9092 | { |
36a117a5 MM |
9093 | while (DECL_TEMPLATE_INFO (decl)) |
9094 | decl = DECL_TI_TEMPLATE (decl); | |
9095 | ||
9096 | return decl; | |
9097 | } | |
9098 | ||
9099 | /* Return the most specialized of the class template specializations | |
9100 | of TMPL which can produce an instantiation matching ARGS, or | |
9101 | error_mark_node if the choice is ambiguous. */ | |
9102 | ||
e9659ab0 | 9103 | static tree |
36a117a5 MM |
9104 | most_specialized_class (tmpl, args) |
9105 | tree tmpl; | |
9106 | tree args; | |
9107 | { | |
9108 | tree list = NULL_TREE; | |
9109 | tree t; | |
9110 | tree champ; | |
73aad9b9 JM |
9111 | int fate; |
9112 | ||
36a117a5 MM |
9113 | tmpl = most_general_template (tmpl); |
9114 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 9115 | { |
36a117a5 MM |
9116 | tree spec_args |
9117 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
9118 | if (spec_args) | |
73aad9b9 JM |
9119 | { |
9120 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
9121 | TREE_TYPE (list) = TREE_TYPE (t); | |
9122 | } | |
9123 | } | |
9124 | ||
9125 | if (! list) | |
9126 | return NULL_TREE; | |
9127 | ||
9128 | t = list; | |
9129 | champ = t; | |
9130 | t = TREE_CHAIN (t); | |
9131 | for (; t; t = TREE_CHAIN (t)) | |
9132 | { | |
9133 | fate = more_specialized_class (champ, t); | |
9134 | if (fate == 1) | |
9135 | ; | |
9136 | else | |
9137 | { | |
9138 | if (fate == 0) | |
9139 | { | |
9140 | t = TREE_CHAIN (t); | |
9141 | if (! t) | |
9142 | return error_mark_node; | |
9143 | } | |
9144 | champ = t; | |
9145 | } | |
9146 | } | |
9147 | ||
9148 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
9149 | { | |
85b71cf2 | 9150 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
9151 | if (fate != 1) |
9152 | return error_mark_node; | |
9153 | } | |
9154 | ||
9155 | return champ; | |
9156 | } | |
9157 | ||
8d08fdba | 9158 | /* called from the parser. */ |
e92cc029 | 9159 | |
8d08fdba | 9160 | void |
6633d636 | 9161 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 9162 | tree declspecs, declarator, storage; |
8d08fdba | 9163 | { |
c11b6f21 | 9164 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 9165 | tree result = NULL_TREE; |
faae18ab | 9166 | int extern_p = 0; |
e8abc66f | 9167 | |
3fa56191 MM |
9168 | if (!decl) |
9169 | /* An error ocurred, for which grokdeclarator has already issued | |
9170 | an appropriate message. */ | |
9171 | return; | |
9172 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 9173 | { |
8251199e | 9174 | cp_error ("explicit instantiation of non-template `%#D'", decl); |
ec255269 MS |
9175 | return; |
9176 | } | |
03d0f4af | 9177 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 9178 | { |
03d0f4af MM |
9179 | /* There is an asymmetry here in the way VAR_DECLs and |
9180 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
9181 | the latter, the DECL we get back will be marked as a | |
9182 | template instantiation, and the appropriate | |
9183 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
9184 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
9185 | should handle VAR_DECLs as it currently handles | |
9186 | FUNCTION_DECLs. */ | |
6633d636 MS |
9187 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); |
9188 | if (result && TREE_CODE (result) != VAR_DECL) | |
03d0f4af | 9189 | { |
8251199e | 9190 | cp_error ("no matching template for `%D' found", result); |
03d0f4af MM |
9191 | return; |
9192 | } | |
6633d636 MS |
9193 | } |
9194 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
9195 | { | |
8251199e | 9196 | cp_error ("explicit instantiation of `%#D'", decl); |
6633d636 MS |
9197 | return; |
9198 | } | |
03d0f4af MM |
9199 | else |
9200 | result = decl; | |
672476cb | 9201 | |
03d0f4af MM |
9202 | /* Check for various error cases. Note that if the explicit |
9203 | instantiation is legal the RESULT will currently be marked as an | |
9204 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
9205 | until we get here. */ | |
9206 | ||
9207 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 9208 | { |
03d0f4af MM |
9209 | /* [temp.spec] |
9210 | ||
9211 | No program shall both explicitly instantiate and explicitly | |
9212 | specialize a template. */ | |
6f1b4c42 JM |
9213 | cp_pedwarn ("explicit instantiation of `%#D' after", result); |
9214 | cp_pedwarn_at ("explicit specialization here", result); | |
672476cb MM |
9215 | return; |
9216 | } | |
03d0f4af MM |
9217 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
9218 | { | |
9219 | /* [temp.spec] | |
98c1c668 | 9220 | |
03d0f4af MM |
9221 | No program shall explicitly instantiate any template more |
9222 | than once. | |
9223 | ||
2aaf816d JM |
9224 | We check DECL_INTERFACE_KNOWN so as not to complain when the first |
9225 | instantiation was `extern' and the second is not, and EXTERN_P for | |
9226 | the opposite case. If -frepo, chances are we already got marked | |
9227 | as an explicit instantion because of the repo file. */ | |
9228 | if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository) | |
6f1b4c42 | 9229 | cp_pedwarn ("duplicate explicit instantiation of `%#D'", result); |
03d0f4af MM |
9230 | |
9231 | /* If we've already instantiated the template, just return now. */ | |
9232 | if (DECL_INTERFACE_KNOWN (result)) | |
9233 | return; | |
9234 | } | |
9235 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 9236 | { |
8251199e | 9237 | cp_error ("no matching template for `%D' found", result); |
faae18ab MS |
9238 | return; |
9239 | } | |
03d0f4af | 9240 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 9241 | { |
8251199e | 9242 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); |
6633d636 MS |
9243 | return; |
9244 | } | |
9245 | ||
a0a33927 MS |
9246 | if (flag_external_templates) |
9247 | return; | |
9248 | ||
f0e01782 | 9249 | if (storage == NULL_TREE) |
00595019 | 9250 | ; |
faae18ab | 9251 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af MM |
9252 | { |
9253 | if (pedantic) | |
8251199e | 9254 | cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations"); |
03d0f4af MM |
9255 | extern_p = 1; |
9256 | } | |
f0e01782 | 9257 | else |
8251199e | 9258 | cp_error ("storage class `%D' applied to template instantiation", |
f0e01782 | 9259 | storage); |
5566b478 | 9260 | |
03d0f4af | 9261 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
5566b478 | 9262 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 9263 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
9264 | if (! extern_p) |
9265 | instantiate_decl (result); | |
7177d104 MS |
9266 | } |
9267 | ||
faae18ab MS |
9268 | void |
9269 | mark_class_instantiated (t, extern_p) | |
9270 | tree t; | |
9271 | int extern_p; | |
9272 | { | |
9273 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
9274 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
9275 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
9276 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
9277 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
9278 | if (! extern_p) | |
9279 | { | |
9280 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
9281 | rest_of_type_compilation (t, 1); | |
9282 | } | |
9283 | } | |
e8abc66f | 9284 | |
7177d104 | 9285 | void |
ca79f85d JM |
9286 | do_type_instantiation (t, storage) |
9287 | tree t, storage; | |
7177d104 | 9288 | { |
e8abc66f MS |
9289 | int extern_p = 0; |
9290 | int nomem_p = 0; | |
5566b478 MS |
9291 | int static_p = 0; |
9292 | ||
ca79f85d JM |
9293 | if (TREE_CODE (t) == TYPE_DECL) |
9294 | t = TREE_TYPE (t); | |
9295 | ||
7ddedda4 | 9296 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 9297 | { |
8251199e | 9298 | cp_error ("explicit instantiation of non-template type `%T'", t); |
ca79f85d JM |
9299 | return; |
9300 | } | |
9301 | ||
5566b478 | 9302 | complete_type (t); |
7177d104 | 9303 | |
a292b002 MS |
9304 | /* With -fexternal-templates, explicit instantiations are treated the same |
9305 | as implicit ones. */ | |
a0a33927 MS |
9306 | if (flag_external_templates) |
9307 | return; | |
9308 | ||
f0e01782 MS |
9309 | if (TYPE_SIZE (t) == NULL_TREE) |
9310 | { | |
8251199e | 9311 | cp_error ("explicit instantiation of `%#T' before definition of template", |
f0e01782 MS |
9312 | t); |
9313 | return; | |
9314 | } | |
9315 | ||
03d0f4af | 9316 | if (storage != NULL_TREE) |
f0e01782 | 9317 | { |
03d0f4af | 9318 | if (pedantic) |
8251199e JM |
9319 | cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations", |
9320 | IDENTIFIER_POINTER (storage)); | |
03d0f4af MM |
9321 | |
9322 | if (storage == ridpointers[(int) RID_INLINE]) | |
9323 | nomem_p = 1; | |
9324 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
9325 | extern_p = 1; | |
9326 | else if (storage == ridpointers[(int) RID_STATIC]) | |
9327 | static_p = 1; | |
9328 | else | |
9329 | { | |
8251199e | 9330 | cp_error ("storage class `%D' applied to template instantiation", |
03d0f4af MM |
9331 | storage); |
9332 | extern_p = 0; | |
9333 | } | |
f0e01782 MS |
9334 | } |
9335 | ||
03d0f4af MM |
9336 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
9337 | { | |
9338 | /* [temp.spec] | |
a292b002 | 9339 | |
03d0f4af MM |
9340 | No program shall both explicitly instantiate and explicitly |
9341 | specialize a template. */ | |
8251199e JM |
9342 | cp_error ("explicit instantiation of `%#T' after", t); |
9343 | cp_error_at ("explicit specialization here", t); | |
03d0f4af MM |
9344 | return; |
9345 | } | |
9346 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 9347 | { |
03d0f4af MM |
9348 | /* [temp.spec] |
9349 | ||
9350 | No program shall explicitly instantiate any template more | |
9351 | than once. | |
9352 | ||
2aaf816d JM |
9353 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation |
9354 | was `extern'. If EXTERN_P then the second is. If -frepo, chances | |
9355 | are we already got marked as an explicit instantion because of the | |
9356 | repo file. All these cases are OK. */ | |
9357 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository) | |
9358 | cp_pedwarn ("duplicate explicit instantiation of `%#T'", t); | |
03d0f4af MM |
9359 | |
9360 | /* If we've already instantiated the template, just return now. */ | |
9361 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
9362 | return; | |
44a8d0b3 | 9363 | } |
e8abc66f | 9364 | |
03d0f4af MM |
9365 | mark_class_instantiated (t, extern_p); |
9366 | repo_template_instantiated (t, extern_p); | |
9367 | ||
e8abc66f MS |
9368 | if (nomem_p) |
9369 | return; | |
9370 | ||
7177d104 | 9371 | { |
db5ae43f | 9372 | tree tmp; |
5566b478 | 9373 | |
03d0f4af MM |
9374 | /* In contrast to implicit instantiation, where only the |
9375 | declarations, and not the definitions, of members are | |
9376 | instantiated, we have here: | |
9377 | ||
9378 | [temp.explicit] | |
9379 | ||
9380 | The explicit instantiation of a class template specialization | |
9381 | implies the instantiation of all of its members not | |
9382 | previously explicitly specialized in the translation unit | |
9383 | containing the explicit instantiation. | |
9384 | ||
9385 | Of course, we can't instantiate member template classes, since | |
9386 | we don't have any arguments for them. Note that the standard | |
9387 | is unclear on whether the instatiation of the members are | |
9388 | *explicit* instantiations or not. We choose to be generous, | |
9389 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
9390 | the explicit instantiation of a class where some of the members | |
9391 | have no definition in the current translation unit. */ | |
9392 | ||
5566b478 MS |
9393 | if (! static_p) |
9394 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 9395 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 9396 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
9397 | { |
9398 | mark_decl_instantiated (tmp, extern_p); | |
9399 | repo_template_instantiated (tmp, extern_p); | |
9400 | if (! extern_p) | |
9401 | instantiate_decl (tmp); | |
9402 | } | |
9403 | ||
9404 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
9405 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 9406 | { |
5566b478 | 9407 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 9408 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
9409 | if (! extern_p) |
9410 | instantiate_decl (tmp); | |
863adfc0 | 9411 | } |
7177d104 | 9412 | |
a292b002 | 9413 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
e4a84209 MM |
9414 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) |
9415 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
f376e137 | 9416 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); |
a292b002 | 9417 | } |
8d08fdba | 9418 | } |
a28e3c7f | 9419 | |
36a117a5 MM |
9420 | /* Given a function DECL, which is a specialization of TMPL, modify |
9421 | DECL to be a re-instantiation of TMPL with the same template | |
9422 | arguments. TMPL should be the template into which tsubst'ing | |
9423 | should occur for DECL, not the most general template. | |
830bfa74 MM |
9424 | |
9425 | One reason for doing this is a scenario like this: | |
9426 | ||
9427 | template <class T> | |
9428 | void f(const T&, int i); | |
9429 | ||
9430 | void g() { f(3, 7); } | |
9431 | ||
9432 | template <class T> | |
9433 | void f(const T& t, const int i) { } | |
9434 | ||
9435 | Note that when the template is first instantiated, with | |
9436 | instantiate_template, the resulting DECL will have no name for the | |
9437 | first parameter, and the wrong type for the second. So, when we go | |
9438 | to instantiate the DECL, we regenerate it. */ | |
9439 | ||
e9659ab0 | 9440 | static void |
830bfa74 MM |
9441 | regenerate_decl_from_template (decl, tmpl) |
9442 | tree decl; | |
9443 | tree tmpl; | |
9444 | { | |
9445 | tree args; | |
830bfa74 MM |
9446 | tree code_pattern; |
9447 | tree new_decl; | |
36a117a5 MM |
9448 | tree gen_tmpl; |
9449 | int unregistered; | |
830bfa74 MM |
9450 | |
9451 | args = DECL_TI_ARGS (decl); | |
9452 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
9453 | ||
8251199e JM |
9454 | /* Unregister the specialization so that when we tsubst we will not |
9455 | just return DECL. We don't have to unregister DECL from TMPL | |
9456 | because if would only be registered there if it were a partial | |
9457 | instantiation of a specialization, which it isn't: it's a full | |
9458 | instantiation. */ | |
36a117a5 MM |
9459 | gen_tmpl = most_general_template (tmpl); |
9460 | unregistered = unregister_specialization (decl, gen_tmpl); | |
9461 | ||
9462 | /* If the DECL was not unregistered then something peculiar is | |
9463 | happening: we created a specialization but did not call | |
9464 | register_specialization for it. */ | |
9465 | my_friendly_assert (unregistered, 0); | |
9466 | ||
61a127b3 MM |
9467 | if (TREE_CODE (decl) == VAR_DECL) |
9468 | /* Make sure that we can see identifiers, and compute access | |
9469 | correctly, for the class members used in the declaration of | |
9470 | this static variable. */ | |
9471 | pushclass (DECL_CONTEXT (decl), 2); | |
9472 | ||
d8c4447d | 9473 | /* Do the substitution to get the new declaration. */ |
4393e105 | 9474 | new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE); |
830bfa74 MM |
9475 | |
9476 | if (TREE_CODE (decl) == VAR_DECL) | |
9477 | { | |
9478 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
830bfa74 | 9479 | DECL_INITIAL (new_decl) = |
d8c4447d | 9480 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
4393e105 | 9481 | /*complain=*/1, DECL_TI_TEMPLATE (decl)); |
61a127b3 | 9482 | /* Pop the class context we pushed above. */ |
b74a0560 | 9483 | popclass (); |
830bfa74 | 9484 | } |
2b0a63a3 | 9485 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
fbf1c34b MM |
9486 | { |
9487 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
9488 | new decl. */ | |
9489 | DECL_INITIAL (new_decl) = error_mark_node; | |
9490 | /* And don't complain about a duplicate definition. */ | |
9491 | DECL_INITIAL (decl) = NULL_TREE; | |
9492 | } | |
36a117a5 MM |
9493 | |
9494 | /* The immediate parent of the new template is still whatever it was | |
9495 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
9496 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
9497 | tsubst always calculates the name as if the function in question | |
9498 | were really a template instance, and sometimes, with friend | |
9499 | functions, this is not so. See tsubst_friend_function for | |
9500 | details. */ | |
9501 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
9502 | DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl); | |
9503 | DECL_RTL (new_decl) = DECL_RTL (decl); | |
9504 | ||
9505 | /* Call duplicate decls to merge the old and new declarations. */ | |
830bfa74 MM |
9506 | duplicate_decls (new_decl, decl); |
9507 | ||
36a117a5 MM |
9508 | /* Now, re-register the specialization. */ |
9509 | register_specialization (decl, gen_tmpl, args); | |
830bfa74 MM |
9510 | } |
9511 | ||
f84b4be9 JM |
9512 | /* Produce the definition of D, a _DECL generated from a template. */ |
9513 | ||
a28e3c7f | 9514 | tree |
5566b478 MS |
9515 | instantiate_decl (d) |
9516 | tree d; | |
a28e3c7f | 9517 | { |
36a117a5 MM |
9518 | tree tmpl = DECL_TI_TEMPLATE (d); |
9519 | tree args = DECL_TI_ARGS (d); | |
830bfa74 | 9520 | tree td; |
36a117a5 MM |
9521 | tree code_pattern; |
9522 | tree spec; | |
9523 | tree gen_tmpl; | |
5566b478 | 9524 | int nested = in_function_p (); |
5566b478 | 9525 | int pattern_defined; |
c27be9b9 JM |
9526 | int line = lineno; |
9527 | char *file = input_filename; | |
5566b478 | 9528 | |
36a117a5 MM |
9529 | /* This function should only be used to instantiate templates for |
9530 | functions and static member variables. */ | |
9531 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
9532 | || TREE_CODE (d) == VAR_DECL, 0); | |
9533 | ||
fbf1c34b | 9534 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af MM |
9535 | /* D has already been instantiated. It might seem reasonable to |
9536 | check whether or not D is an explict instantiation, and, if so, | |
9537 | stop here. But when an explicit instantiation is deferred | |
9538 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
9539 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
9540 | return d; |
9541 | ||
9542 | /* If we already have a specialization of this declaration, then | |
9543 | there's no reason to instantiate it. Note that | |
9544 | retrieve_specialization gives us both instantiations and | |
9545 | specializations, so we must explicitly check | |
9546 | DECL_TEMPLATE_SPECIALIZATION. */ | |
9547 | gen_tmpl = most_general_template (tmpl); | |
9548 | spec = retrieve_specialization (gen_tmpl, args); | |
9549 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
9550 | return spec; | |
9551 | ||
9552 | /* This needs to happen before any tsubsting. */ | |
9553 | if (! push_tinst_level (d)) | |
9554 | return d; | |
9555 | ||
4d85e00e MM |
9556 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
9557 | for the instantiation. This is not always the most general | |
9558 | template. Consider, for example: | |
9559 | ||
9560 | template <class T> | |
9561 | struct S { template <class U> void f(); | |
9562 | template <> void f<int>(); }; | |
9563 | ||
9564 | and an instantiation of S<double>::f<int>. We want TD to be the | |
9565 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
9566 | td = tmpl; | |
9567 | for (td = tmpl; | |
9568 | /* An instantiation cannot have a definition, so we need a | |
9569 | more general template. */ | |
9570 | DECL_TEMPLATE_INSTANTIATION (td) | |
9571 | /* We must also deal with friend templates. Given: | |
9572 | ||
9573 | template <class T> struct S { | |
9574 | template <class U> friend void f() {}; | |
9575 | }; | |
9576 | ||
9577 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
9578 | so far as the language is concerned, but that's still | |
9579 | where we get the pattern for the instantiation from. On | |
9580 | ther hand, if the definition comes outside the class, say: | |
9581 | ||
9582 | template <class T> struct S { | |
9583 | template <class U> friend void f(); | |
9584 | }; | |
9585 | template <class U> friend void f() {} | |
9586 | ||
9587 | we don't need to look any further. That's what the check for | |
9588 | DECL_INITIAL is for. */ | |
9589 | || (TREE_CODE (d) == FUNCTION_DECL | |
61289ca3 | 9590 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) |
4d85e00e | 9591 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))); |
f84b4be9 | 9592 | ) |
4d85e00e MM |
9593 | { |
9594 | /* The present template, TD, should not be a definition. If it | |
9595 | were a definition, we should be using it! Note that we | |
9596 | cannot restructure the loop to just keep going until we find | |
9597 | a template with a definition, since that might go too far if | |
9598 | a specialization was declared, but not defined. */ | |
4d85e00e MM |
9599 | my_friendly_assert (!(TREE_CODE (d) == VAR_DECL |
9600 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), | |
9601 | 0); | |
9602 | ||
9603 | /* Fetch the more general template. */ | |
9604 | td = DECL_TI_TEMPLATE (td); | |
9605 | } | |
27bb8339 | 9606 | |
fee23f54 | 9607 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 9608 | |
5566b478 | 9609 | if (TREE_CODE (d) == FUNCTION_DECL) |
36a117a5 | 9610 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 | 9611 | else |
36a117a5 | 9612 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
de22184b MS |
9613 | |
9614 | push_to_top_level (); | |
9615 | lineno = DECL_SOURCE_LINE (d); | |
9616 | input_filename = DECL_SOURCE_FILE (d); | |
9617 | ||
de22184b | 9618 | if (pattern_defined) |
5566b478 MS |
9619 | { |
9620 | repo_template_used (d); | |
9621 | ||
9622 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
9623 | { | |
9624 | if (flag_alt_external_templates) | |
9625 | { | |
9626 | if (interface_unknown) | |
9627 | warn_if_unknown_interface (d); | |
9628 | } | |
fee23f54 | 9629 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
9630 | { |
9631 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 9632 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
9633 | } |
9634 | else | |
fee23f54 | 9635 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
9636 | } |
9637 | ||
e92cc029 | 9638 | if (at_eof) |
5566b478 MS |
9639 | import_export_decl (d); |
9640 | } | |
9641 | ||
c4ae3f91 JM |
9642 | /* Reject all external templates except inline functions. */ |
9643 | if (DECL_INTERFACE_KNOWN (d) | |
9644 | && ! DECL_NOT_REALLY_EXTERN (d) | |
9645 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
9646 | goto out; | |
9647 | ||
67ffc812 | 9648 | if (TREE_CODE (d) == VAR_DECL |
711734a9 | 9649 | && TREE_READONLY (d) |
67ffc812 MM |
9650 | && DECL_INITIAL (d) == NULL_TREE |
9651 | && DECL_INITIAL (code_pattern) != NULL_TREE) | |
9652 | /* We need to set up DECL_INITIAL regardless of pattern_defined if | |
9653 | the variable is a static const initialized in the class body. */; | |
9654 | else if (! pattern_defined | |
9655 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) | |
9656 | && ! at_eof)) | |
5566b478 | 9657 | { |
67ffc812 | 9658 | /* Defer all templates except inline functions used in another |
c27be9b9 JM |
9659 | function. We restore the source position here because it's used |
9660 | by add_pending_template. */ | |
9661 | lineno = line; | |
9662 | input_filename = file; | |
9663 | ||
03d0f4af MM |
9664 | if (at_eof && !pattern_defined |
9665 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
9666 | /* [temp.explicit] | |
9667 | ||
9668 | The definition of a non-exported function template, a | |
9669 | non-exported member function template, or a non-exported | |
9670 | member function or static data member of a class template | |
9671 | shall be present in every translation unit in which it is | |
9672 | explicitly instantiated. */ | |
8251199e | 9673 | cp_error ("explicit instantiation of `%D' but no definition available", |
03d0f4af MM |
9674 | d); |
9675 | ||
5566b478 | 9676 | add_pending_template (d); |
de22184b | 9677 | goto out; |
5566b478 MS |
9678 | } |
9679 | ||
2b0a63a3 MM |
9680 | /* We're now committed to instantiating this template. Mark it as |
9681 | instantiated so that recursive calls to instantiate_decl do not | |
9682 | try to instantiate it again. */ | |
fbf1c34b | 9683 | DECL_TEMPLATE_INSTANTIATED (d) = 1; |
120722ac | 9684 | |
2b0a63a3 MM |
9685 | /* Regenerate the declaration in case the template has been modified |
9686 | by a subsequent redeclaration. */ | |
9687 | regenerate_decl_from_template (d, td); | |
9688 | ||
120722ac MM |
9689 | /* We already set the file and line above. Reset them now in case |
9690 | they changed as a result of calling regenerate_decl_from_template. */ | |
5156628f MS |
9691 | lineno = DECL_SOURCE_LINE (d); |
9692 | input_filename = DECL_SOURCE_FILE (d); | |
9693 | ||
5566b478 MS |
9694 | if (TREE_CODE (d) == VAR_DECL) |
9695 | { | |
9696 | DECL_IN_AGGR_P (d) = 0; | |
9697 | if (DECL_INTERFACE_KNOWN (d)) | |
9698 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
9699 | else | |
9700 | { | |
9701 | DECL_EXTERNAL (d) = 1; | |
9702 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
9703 | } | |
9704 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
9705 | } | |
9706 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
9707 | { | |
558475f0 | 9708 | /* Set up context. */ |
a8f73d4b | 9709 | start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED); |
5566b478 MS |
9710 | store_parm_decls (); |
9711 | ||
558475f0 MM |
9712 | /* Substitute into the body of the function. */ |
9713 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
9714 | /*complain=*/1, tmpl); | |
9715 | ||
4d6abc1c MM |
9716 | /* Finish the function. */ |
9717 | expand_body (finish_function (lineno, 0)); | |
5566b478 MS |
9718 | } |
9719 | ||
de22184b | 9720 | out: |
c27be9b9 JM |
9721 | lineno = line; |
9722 | input_filename = file; | |
9723 | ||
5566b478 | 9724 | pop_from_top_level (); |
5566b478 | 9725 | pop_tinst_level (); |
a28e3c7f | 9726 | |
a28e3c7f MS |
9727 | return d; |
9728 | } | |
5566b478 | 9729 | |
0aafb128 MM |
9730 | /* Run through the list of templates that we wish we could |
9731 | instantiate, and instantiate any we can. */ | |
9732 | ||
9733 | int | |
9734 | instantiate_pending_templates () | |
9735 | { | |
9736 | tree *t; | |
9737 | int instantiated_something = 0; | |
9738 | int reconsider; | |
9739 | ||
9740 | do | |
9741 | { | |
9742 | reconsider = 0; | |
9743 | ||
9744 | t = &pending_templates; | |
9745 | while (*t) | |
9746 | { | |
9747 | tree srcloc = TREE_PURPOSE (*t); | |
9748 | tree instantiation = TREE_VALUE (*t); | |
9749 | ||
9750 | input_filename = SRCLOC_FILE (srcloc); | |
9751 | lineno = SRCLOC_LINE (srcloc); | |
9752 | ||
9753 | if (TREE_CODE_CLASS (TREE_CODE (instantiation)) == 't') | |
9754 | { | |
9755 | tree fn; | |
9756 | ||
9757 | if (!TYPE_SIZE (instantiation)) | |
9758 | { | |
9759 | instantiate_class_template (instantiation); | |
9760 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
9761 | for (fn = TYPE_METHODS (instantiation); | |
9762 | fn; | |
9763 | fn = TREE_CHAIN (fn)) | |
9764 | if (! DECL_ARTIFICIAL (fn)) | |
9765 | instantiate_decl (fn); | |
9766 | if (TYPE_SIZE (instantiation)) | |
9767 | { | |
9768 | instantiated_something = 1; | |
9769 | reconsider = 1; | |
9770 | } | |
9771 | } | |
9772 | ||
9773 | if (TYPE_SIZE (instantiation)) | |
9774 | /* If INSTANTIATION has been instantiated, then we don't | |
9775 | need to consider it again in the future. */ | |
9776 | *t = TREE_CHAIN (*t); | |
9777 | else | |
9778 | t = &TREE_CHAIN (*t); | |
9779 | } | |
9780 | else | |
9781 | { | |
9782 | if (DECL_TEMPLATE_INSTANTIATION (instantiation) | |
9783 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9784 | { | |
9785 | instantiation = instantiate_decl (instantiation); | |
9786 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9787 | { | |
9788 | instantiated_something = 1; | |
9789 | reconsider = 1; | |
9790 | } | |
9791 | } | |
9792 | ||
9793 | if (!DECL_TEMPLATE_INSTANTIATION (instantiation) | |
9794 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9795 | /* If INSTANTIATION has been instantiated, then we don't | |
9796 | need to consider it again in the future. */ | |
9797 | *t = TREE_CHAIN (*t); | |
9798 | else | |
9799 | t = &TREE_CHAIN (*t); | |
9800 | } | |
9801 | } | |
9802 | template_tail = t; | |
9803 | ||
9804 | /* Go through the things that are template instantiations if we are | |
9805 | using guiding declarations. */ | |
9806 | t = &maybe_templates; | |
9807 | while (*t) | |
9808 | { | |
9809 | tree template; | |
9810 | tree fn; | |
9811 | tree args; | |
9812 | ||
9813 | fn = TREE_VALUE (*t); | |
9814 | ||
9815 | if (DECL_INITIAL (fn)) | |
9816 | /* If the FN is already defined, then it was either already | |
9817 | instantiated or, even though guiding declarations were | |
9818 | allowed, a non-template definition was provided. */ | |
9819 | ; | |
9820 | else | |
9821 | { | |
9822 | template = TREE_PURPOSE (*t); | |
9823 | args = get_bindings (template, fn, NULL_TREE); | |
9824 | fn = instantiate_template (template, args); | |
9825 | instantiate_decl (fn); | |
9826 | reconsider = 1; | |
9827 | } | |
9828 | ||
9829 | /* Remove this entry from the chain. */ | |
9830 | *t = TREE_CHAIN (*t); | |
9831 | } | |
9832 | maybe_template_tail = t; | |
9833 | } | |
9834 | while (reconsider); | |
9835 | ||
9836 | return instantiated_something; | |
9837 | } | |
9838 | ||
4393e105 MM |
9839 | /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it |
9840 | is an initializer list: the TREE_PURPOSEs are DECLs, and the | |
9841 | TREE_VALUEs are initializer values. Used by instantiate_decl. */ | |
9842 | ||
824b9a4c | 9843 | static tree |
5566b478 MS |
9844 | tsubst_expr_values (t, argvec) |
9845 | tree t, argvec; | |
9846 | { | |
9847 | tree first = NULL_TREE; | |
9848 | tree *p = &first; | |
9849 | ||
9850 | for (; t; t = TREE_CHAIN (t)) | |
9851 | { | |
4393e105 MM |
9852 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, |
9853 | /*complain=*/1, NULL_TREE); | |
9854 | tree val = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1, | |
9855 | NULL_TREE); | |
5566b478 MS |
9856 | *p = build_tree_list (pur, val); |
9857 | p = &TREE_CHAIN (*p); | |
9858 | } | |
9859 | return first; | |
9860 | } | |
9861 | ||
75650646 MM |
9862 | void |
9863 | begin_tree () | |
9864 | { | |
a8f73d4b MM |
9865 | if (current_function) |
9866 | { | |
9867 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
9868 | last_tree = NULL_TREE; | |
9869 | } | |
9870 | else | |
9871 | saved_trees = tree_cons (NULL_TREE, NULL_TREE, saved_trees); | |
75650646 MM |
9872 | } |
9873 | ||
9874 | ||
9875 | void | |
9876 | end_tree () | |
9877 | { | |
9878 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
9879 | ||
a8f73d4b MM |
9880 | if (current_function) |
9881 | last_tree = TREE_VALUE (saved_trees); | |
75650646 MM |
9882 | saved_trees = TREE_CHAIN (saved_trees); |
9883 | } | |
9884 | ||
73aad9b9 JM |
9885 | /* D is an undefined function declaration in the presence of templates with |
9886 | the same name, listed in FNS. If one of them can produce D as an | |
9887 | instantiation, remember this so we can instantiate it at EOF if D has | |
9888 | not been defined by that time. */ | |
9889 | ||
9890 | void | |
9891 | add_maybe_template (d, fns) | |
9892 | tree d, fns; | |
9893 | { | |
9894 | tree t; | |
9895 | ||
9896 | if (DECL_MAYBE_TEMPLATE (d)) | |
9897 | return; | |
9898 | ||
e1467ff2 | 9899 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
9900 | if (! t) |
9901 | return; | |
9902 | if (t == error_mark_node) | |
9903 | { | |
8251199e | 9904 | cp_error ("ambiguous template instantiation for `%D'", d); |
73aad9b9 JM |
9905 | return; |
9906 | } | |
9907 | ||
e1b3e07d | 9908 | *maybe_template_tail = tree_cons (t, d, NULL_TREE); |
73aad9b9 JM |
9909 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); |
9910 | DECL_MAYBE_TEMPLATE (d) = 1; | |
9911 | } | |
b87692e5 | 9912 | |
61a127b3 MM |
9913 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
9914 | ||
9915 | static void | |
9916 | set_current_access_from_decl (decl) | |
9917 | tree decl; | |
9918 | { | |
9919 | if (TREE_PRIVATE (decl)) | |
9920 | current_access_specifier = access_private_node; | |
9921 | else if (TREE_PROTECTED (decl)) | |
9922 | current_access_specifier = access_protected_node; | |
9923 | else | |
9924 | current_access_specifier = access_public_node; | |
9925 | } | |
9926 | ||
dbfe2124 MM |
9927 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
9928 | is the instantiation (which should have been created with | |
9929 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 9930 | |
dbfe2124 MM |
9931 | static void |
9932 | tsubst_enum (tag, newtag, args) | |
9933 | tree tag; | |
9934 | tree newtag; | |
9935 | tree args; | |
b87692e5 | 9936 | { |
dbfe2124 | 9937 | tree e; |
b87692e5 MS |
9938 | |
9939 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
9940 | { | |
61a127b3 MM |
9941 | tree value; |
9942 | tree elt; | |
9943 | ||
9944 | /* Note that in a template enum, the TREE_VALUE is the | |
9945 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
9946 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
4393e105 MM |
9947 | args, /*complain=*/1, |
9948 | NULL_TREE); | |
61a127b3 MM |
9949 | |
9950 | /* Give this enumeration constant the correct access. */ | |
9951 | set_current_access_from_decl (TREE_VALUE (e)); | |
9952 | ||
9953 | /* Actually build the enumerator itself. */ | |
9954 | elt = build_enumerator (TREE_PURPOSE (e), value, newtag); | |
b87692e5 | 9955 | |
dbfe2124 MM |
9956 | /* We save the enumerators we have built so far in the |
9957 | TYPE_VALUES so that if the enumeration constants for | |
9958 | subsequent enumerators involve those for previous ones, | |
9959 | tsubst_copy will be able to find them. */ | |
9960 | TREE_CHAIN (elt) = TYPE_VALUES (newtag); | |
9961 | TYPE_VALUES (newtag) = elt; | |
9962 | } | |
b3d5a58b | 9963 | |
dbfe2124 | 9964 | finish_enum (newtag); |
b87692e5 | 9965 | } |
36a117a5 MM |
9966 | |
9967 | /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that | |
9968 | is either an instantiation or specialization of a template | |
9969 | function. */ | |
9970 | ||
9971 | static void | |
9972 | set_mangled_name_for_template_decl (decl) | |
9973 | tree decl; | |
9974 | { | |
9975 | tree saved_namespace; | |
b370501f | 9976 | tree context = NULL_TREE; |
36a117a5 MM |
9977 | tree fn_type; |
9978 | tree ret_type; | |
9979 | tree parm_types; | |
9980 | tree tparms; | |
9981 | tree targs; | |
9982 | tree tmpl; | |
9983 | int parm_depth; | |
9984 | ||
9985 | my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); | |
9986 | my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0); | |
9987 | ||
9988 | /* The names of template functions must be mangled so as to indicate | |
9989 | what template is being specialized with what template arguments. | |
9990 | For example, each of the following three functions must get | |
9991 | different mangled names: | |
9992 | ||
9993 | void f(int); | |
9994 | template <> void f<7>(int); | |
9995 | template <> void f<8>(int); */ | |
9996 | ||
9997 | targs = DECL_TI_ARGS (decl); | |
9998 | if (uses_template_parms (targs)) | |
9999 | /* This DECL is for a partial instantiation. There's no need to | |
10000 | mangle the name of such an entity. */ | |
10001 | return; | |
10002 | ||
10003 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
10004 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
10005 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
10006 | ||
10007 | /* There should be as many levels of arguments as there are levels | |
10008 | of parameters. */ | |
10009 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
10010 | ||
10011 | /* We now compute the PARMS and RET_TYPE to give to | |
10012 | build_decl_overload_real. The PARMS and RET_TYPE are the | |
10013 | parameter and return types of the template, after all but the | |
10014 | innermost template arguments have been substituted, not the | |
10015 | parameter and return types of the function DECL. For example, | |
10016 | given: | |
10017 | ||
10018 | template <class T> T f(T); | |
10019 | ||
10020 | both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'. | |
10021 | A more subtle example is: | |
10022 | ||
10023 | template <class T> struct S { template <class U> void f(T, U); } | |
10024 | ||
10025 | Here, if DECL is `void S<int>::f(int, double)', PARMS should be | |
10026 | {int, U}. Thus, the args that we want to subsitute into the | |
10027 | return and parameter type for the function are those in TARGS, | |
10028 | with the innermost level omitted. */ | |
10029 | fn_type = TREE_TYPE (tmpl); | |
10030 | if (DECL_STATIC_FUNCTION_P (decl)) | |
61a127b3 | 10031 | context = DECL_CLASS_CONTEXT (decl); |
36a117a5 MM |
10032 | |
10033 | if (parm_depth == 1) | |
10034 | /* No substitution is necessary. */ | |
10035 | ; | |
10036 | else | |
10037 | { | |
10038 | int i; | |
10039 | tree partial_args; | |
10040 | ||
10041 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
10042 | let tsubst know not to subsitute for those parameters. */ | |
10043 | partial_args = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
10044 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
10045 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
10046 | TMPL_ARGS_LEVEL (targs, i)); | |
10047 | SET_TMPL_ARGS_LEVEL (partial_args, | |
10048 | TMPL_ARGS_DEPTH (targs), | |
10049 | make_temp_vec (DECL_NTPARMS (tmpl))); | |
10050 | ||
10051 | /* Now, do the (partial) substitution to figure out the | |
10052 | appropriate function type. */ | |
4393e105 | 10053 | fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 | 10054 | if (DECL_STATIC_FUNCTION_P (decl)) |
4393e105 | 10055 | context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
10056 | |
10057 | /* Substitute into the template parameters to obtain the real | |
10058 | innermost set of parameters. This step is important if the | |
10059 | innermost set of template parameters contains value | |
10060 | parameters whose types depend on outer template parameters. */ | |
10061 | TREE_VEC_LENGTH (partial_args)--; | |
4393e105 | 10062 | tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1); |
36a117a5 MM |
10063 | } |
10064 | ||
10065 | /* Now, get the innermost parameters and arguments, and figure out | |
10066 | the parameter and return types. */ | |
10067 | tparms = INNERMOST_TEMPLATE_PARMS (tparms); | |
10068 | targs = innermost_args (targs); | |
10069 | ret_type = TREE_TYPE (fn_type); | |
10070 | parm_types = TYPE_ARG_TYPES (fn_type); | |
10071 | ||
10072 | /* For a static member function, we generate a fake `this' pointer, | |
10073 | for the purposes of mangling. This indicates of which class the | |
10074 | function is a member. Because of: | |
10075 | ||
10076 | [class.static] | |
10077 | ||
10078 | There shall not be a static and a nonstatic member function | |
10079 | with the same name and the same parameter types | |
10080 | ||
10081 | we don't have to worry that this will result in a clash with a | |
10082 | non-static member function. */ | |
10083 | if (DECL_STATIC_FUNCTION_P (decl)) | |
10084 | parm_types = hash_tree_chain (build_pointer_type (context), parm_types); | |
10085 | ||
10086 | /* There should be the same number of template parameters as | |
10087 | template arguments. */ | |
10088 | my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs), | |
10089 | 0); | |
10090 | ||
10091 | /* If the template is in a namespace, we need to put that into the | |
10092 | mangled name. Unfortunately, build_decl_overload_real does not | |
10093 | get the decl to mangle, so it relies on the current | |
10094 | namespace. Therefore, we set that here temporarily. */ | |
10095 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702); | |
10096 | saved_namespace = current_namespace; | |
10097 | current_namespace = CP_DECL_CONTEXT (decl); | |
10098 | ||
10099 | /* Actually set the DCL_ASSEMBLER_NAME. */ | |
10100 | DECL_ASSEMBLER_NAME (decl) | |
10101 | = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type, | |
10102 | tparms, targs, | |
10103 | DECL_FUNCTION_MEMBER_P (decl) | |
10104 | + DECL_CONSTRUCTOR_P (decl)); | |
10105 | ||
10106 | /* Restore the previously active namespace. */ | |
10107 | current_namespace = saved_namespace; | |
10108 | } |