]>
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 MM |
42 | #include "rtl.h" |
43 | #include "varray.h" | |
091f9839 | 44 | #include "defaults.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 | |
5566b478 MS |
55 | tree current_template_parms; |
56 | HOST_WIDE_INT processing_template_decl; | |
8d08fdba | 57 | |
0aafb128 MM |
58 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
59 | instantiations have been deferred, either because their definitions | |
60 | were not yet available, or because we were putting off doing the | |
61 | work. The TREE_PURPOSE of each entry is a SRCLOC indicating where | |
62 | the instantiate request occurred; the TREE_VALUE is a either a DECL | |
63 | (for a function or static data member), or a TYPE (for a class) | |
64 | indicating what we are hoping to instantiate. */ | |
65 | static tree pending_templates; | |
5566b478 MS |
66 | static tree *template_tail = &pending_templates; |
67 | ||
0aafb128 | 68 | static tree maybe_templates; |
73aad9b9 JM |
69 | static tree *maybe_template_tail = &maybe_templates; |
70 | ||
92eca640 | 71 | int processing_specialization; |
75650646 | 72 | int processing_explicit_instantiation; |
67ffc812 | 73 | int processing_template_parmlist; |
386b8a85 JM |
74 | static int template_header_count; |
75 | ||
75650646 | 76 | static tree saved_trees; |
3dcaad8b MM |
77 | static varray_type inline_parm_levels; |
78 | static size_t inline_parm_levels_used; | |
75650646 | 79 | |
8d08fdba MS |
80 | #define obstack_chunk_alloc xmalloc |
81 | #define obstack_chunk_free free | |
82 | ||
830bfa74 MM |
83 | #define UNIFY_ALLOW_NONE 0 |
84 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
85 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
86 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 87 | #define UNIFY_ALLOW_INTEGER 8 |
830bfa74 | 88 | |
4393e105 MM |
89 | #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is |
90 | virtual, or a base class of a virtual | |
91 | base. */ | |
92 | #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current | |
93 | type with the desired type. */ | |
94 | ||
34016c81 | 95 | static int resolve_overloaded_unification PROTO((tree, tree, tree, tree, |
4393e105 | 96 | unification_kind_t, int)); |
e97e5263 | 97 | static int try_one_overload PROTO((tree, tree, tree, tree, tree, |
4393e105 MM |
98 | unification_kind_t, int)); |
99 | static int unify PROTO((tree, tree, tree, tree, int)); | |
49c249e1 JM |
100 | static void add_pending_template PROTO((tree)); |
101 | static int push_tinst_level PROTO((tree)); | |
102 | static tree classtype_mangled_name PROTO((tree)); | |
36a117a5 | 103 | static char *mangle_class_name_for_template PROTO((char *, tree, tree)); |
49c249e1 | 104 | static tree tsubst_expr_values PROTO((tree, tree)); |
49c249e1 | 105 | static int list_eq PROTO((tree, tree)); |
36a117a5 | 106 | static tree get_class_bindings PROTO((tree, tree, tree)); |
744fac59 | 107 | static tree coerce_template_parms PROTO((tree, tree, tree, int, int)); |
dbfe2124 | 108 | static void tsubst_enum PROTO((tree, tree, tree)); |
98c1c668 | 109 | static tree add_to_template_args PROTO((tree, tree)); |
36a117a5 | 110 | static tree add_outermost_template_args PROTO((tree, tree)); |
830bfa74 MM |
111 | static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*, |
112 | tree*)); | |
050367a3 | 113 | static int type_unification_real PROTO((tree, tree, tree, tree, |
4393e105 | 114 | int, unification_kind_t, int)); |
386b8a85 | 115 | static void note_template_header PROTO((int)); |
840b09b4 | 116 | static tree maybe_fold_nontype_arg PROTO((tree)); |
e1467ff2 | 117 | static tree convert_nontype_argument PROTO((tree, tree)); |
8b5b8b7c MM |
118 | static tree convert_template_argument PROTO ((tree, tree, tree, int, |
119 | int , tree)); | |
76b9a14d | 120 | static tree get_bindings_overload PROTO((tree, tree, tree)); |
050367a3 | 121 | static int for_each_template_parm PROTO((tree, tree_fn_t, void*)); |
4b580446 | 122 | static tree build_template_parm_index PROTO((int, int, int, tree, tree)); |
69ac77ce JL |
123 | static int inline_needs_template_parms PROTO((tree)); |
124 | static void push_inline_template_parms_recursive PROTO((tree, int)); | |
125 | static tree retrieve_specialization PROTO((tree, tree)); | |
9188c363 | 126 | static tree retrieve_local_specialization PROTO((tree, tree)); |
36a117a5 | 127 | static tree register_specialization PROTO((tree, tree, tree)); |
9188c363 | 128 | static tree register_local_specialization PROTO((tree, tree, tree)); |
36a117a5 | 129 | static int unregister_specialization PROTO((tree, tree)); |
69ac77ce JL |
130 | static tree reduce_template_parm_level PROTO((tree, tree, int)); |
131 | static tree build_template_decl PROTO((tree, tree)); | |
132 | static int mark_template_parm PROTO((tree, void *)); | |
133 | static tree tsubst_friend_function PROTO((tree, tree)); | |
6757edfe | 134 | static tree tsubst_friend_class PROTO((tree, tree)); |
69ac77ce | 135 | static tree get_bindings_real PROTO((tree, tree, tree, int)); |
04a81d94 | 136 | static int template_decl_level PROTO((tree)); |
a2b60a0e | 137 | static tree maybe_get_template_decl_from_type_decl PROTO((tree)); |
830bfa74 | 138 | static int check_cv_quals_for_unify PROTO((int, tree, tree)); |
4393e105 MM |
139 | static tree tsubst_template_arg_vector PROTO((tree, tree, int)); |
140 | static tree tsubst_template_parms PROTO((tree, tree, int)); | |
830bfa74 | 141 | static void regenerate_decl_from_template PROTO((tree, tree)); |
36a117a5 MM |
142 | static tree most_specialized PROTO((tree, tree, tree)); |
143 | static tree most_specialized_class PROTO((tree, tree)); | |
144 | static tree most_general_template PROTO((tree)); | |
145 | static void set_mangled_name_for_template_decl PROTO((tree)); | |
39c01e4c | 146 | static int template_class_depth_real PROTO((tree, int)); |
4393e105 | 147 | static tree tsubst_aggr_type PROTO((tree, tree, int, tree, int)); |
ae58fa02 | 148 | static tree tsubst_decl PROTO((tree, tree, tree, tree)); |
4393e105 MM |
149 | static tree tsubst_arg_types PROTO((tree, tree, int, tree)); |
150 | static tree tsubst_function_type PROTO((tree, tree, int, tree)); | |
6c30752f MM |
151 | static void check_specialization_scope PROTO((void)); |
152 | static tree process_partial_specialization PROTO((tree)); | |
61a127b3 | 153 | static void set_current_access_from_decl PROTO((tree)); |
6ba89f8e | 154 | static void check_default_tmpl_args PROTO((tree, tree, int, int)); |
4393e105 MM |
155 | static tree tsubst_call_declarator_parms PROTO((tree, tree, int, tree)); |
156 | static tree get_template_base_recursive PROTO((tree, tree, | |
157 | tree, tree, tree, int)); | |
00aeceec | 158 | static tree get_template_base PROTO((tree, tree, tree, tree)); |
4393e105 MM |
159 | static tree try_class_unification PROTO((tree, tree, tree, tree)); |
160 | static int coerce_template_template_parms PROTO((tree, tree, int, | |
161 | tree, tree)); | |
bf8f3f93 | 162 | static tree determine_specialization PROTO((tree, tree, tree *, int)); |
d8e178a0 KG |
163 | static int template_args_equal PROTO((tree, tree)); |
164 | static void print_template_context PROTO((int)); | |
9188c363 | 165 | static void tsubst_default_arguments PROTO((tree)); |
36a117a5 MM |
166 | |
167 | /* We use TREE_VECs to hold template arguments. If there is only one | |
168 | level of template arguments, then the TREE_VEC contains the | |
169 | arguments directly. If there is more than one level of template | |
170 | arguments, then each entry in the TREE_VEC is itself a TREE_VEC, | |
171 | containing the template arguments for a single level. The first | |
172 | entry in the outer TREE_VEC is the outermost level of template | |
173 | parameters; the last is the innermost. | |
174 | ||
175 | It is incorrect to ever form a template argument vector containing | |
176 | only one level of arguments, but which is a TREE_VEC containing as | |
177 | its only entry the TREE_VEC for that level. */ | |
178 | ||
36a117a5 MM |
179 | /* Non-zero if the template arguments is actually a vector of vectors, |
180 | rather than just a vector. */ | |
181 | #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ | |
182 | (NODE != NULL_TREE \ | |
183 | && TREE_CODE (NODE) == TREE_VEC \ | |
184 | && TREE_VEC_LENGTH (NODE) > 0 \ | |
185 | && TREE_VEC_ELT (NODE, 0) != NULL_TREE \ | |
67ffc812 MM |
186 | && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) |
187 | ||
e4a84209 MM |
188 | /* The depth of a template argument vector. When called directly by |
189 | the parser, we use a TREE_LIST rather than a TREE_VEC to represent | |
190 | template arguments. In fact, we may even see NULL_TREE if there | |
191 | are no template arguments. In both of those cases, there is only | |
192 | one level of template arguments. */ | |
193 | #define TMPL_ARGS_DEPTH(NODE) \ | |
194 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1) | |
195 | ||
36a117a5 MM |
196 | /* The LEVELth level of the template ARGS. Note that template |
197 | parameter levels are indexed from 1, not from 0. */ | |
198 | #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ | |
199 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ | |
200 | ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS) | |
201 | ||
202 | /* Set the LEVELth level of the template ARGS to VAL. This macro does | |
203 | not work with single-level argument vectors. */ | |
204 | #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \ | |
205 | (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL)) | |
206 | ||
207 | /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */ | |
208 | #define TMPL_ARG(ARGS, LEVEL, IDX) \ | |
209 | (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX)) | |
210 | ||
211 | /* Set the IDXth element in the LEVELth level of ARGS to VAL. This | |
212 | macro does not work with single-level argument vectors. */ | |
213 | #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \ | |
214 | (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL)) | |
215 | ||
e4a84209 MM |
216 | /* Given a single level of template arguments in NODE, return the |
217 | number of arguments. */ | |
218 | #define NUM_TMPL_ARGS(NODE) \ | |
219 | ((NODE) == NULL_TREE ? 0 \ | |
220 | : (TREE_CODE (NODE) == TREE_VEC \ | |
221 | ? TREE_VEC_LENGTH (NODE) : list_length (NODE))) | |
222 | ||
36a117a5 MM |
223 | /* The number of levels of template parameters given by NODE. */ |
224 | #define TMPL_PARMS_DEPTH(NODE) \ | |
225 | (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE))) | |
226 | ||
e1467ff2 MM |
227 | /* Do any processing required when DECL (a member template declaration |
228 | using TEMPLATE_PARAMETERS as its innermost parameter list) is | |
229 | finished. Returns the TEMPLATE_DECL corresponding to DECL, unless | |
230 | it is a specialization, in which case the DECL itself is returned. */ | |
231 | ||
232 | tree | |
61a127b3 | 233 | finish_member_template_decl (decl) |
e1467ff2 MM |
234 | tree decl; |
235 | { | |
93cdc044 JM |
236 | if (decl == NULL_TREE || decl == void_type_node) |
237 | return NULL_TREE; | |
3ddfb0e6 MM |
238 | else if (decl == error_mark_node) |
239 | /* By returning NULL_TREE, the parser will just ignore this | |
240 | declaration. We have already issued the error. */ | |
241 | return NULL_TREE; | |
93cdc044 JM |
242 | else if (TREE_CODE (decl) == TREE_LIST) |
243 | { | |
5f311aec | 244 | /* Assume that the class is the only declspec. */ |
93cdc044 | 245 | decl = TREE_VALUE (decl); |
8d019cef JM |
246 | if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) |
247 | && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) | |
93cdc044 JM |
248 | { |
249 | tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); | |
250 | check_member_template (tmpl); | |
251 | return tmpl; | |
252 | } | |
8d019cef | 253 | return NULL_TREE; |
93cdc044 | 254 | } |
a1da6cba | 255 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 256 | { |
a1da6cba MM |
257 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
258 | { | |
259 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
260 | return DECL_TI_TEMPLATE (decl); | |
261 | } | |
262 | else | |
263 | return decl; | |
264 | } | |
265 | else | |
8251199e | 266 | cp_error ("invalid member template declaration `%D'", decl); |
e1467ff2 | 267 | |
a1da6cba | 268 | return error_mark_node; |
f84b4be9 | 269 | } |
e1467ff2 | 270 | |
f84b4be9 JM |
271 | /* Returns the template nesting level of the indicated class TYPE. |
272 | ||
273 | For example, in: | |
274 | template <class T> | |
275 | struct A | |
276 | { | |
277 | template <class U> | |
278 | struct B {}; | |
279 | }; | |
280 | ||
39c01e4c MM |
281 | A<T>::B<U> has depth two, while A<T> has depth one. |
282 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
283 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
284 | specializations. | |
285 | ||
286 | This function is guaranteed to return 0 if passed NULL_TREE so | |
287 | that, for example, `template_class_depth (current_class_type)' is | |
288 | always safe. */ | |
f84b4be9 | 289 | |
e9659ab0 | 290 | static int |
39c01e4c | 291 | template_class_depth_real (type, count_specializations) |
f84b4be9 | 292 | tree type; |
39c01e4c | 293 | int count_specializations; |
f84b4be9 | 294 | { |
93cdc044 | 295 | int depth; |
f84b4be9 | 296 | |
2c73f9f5 | 297 | for (depth = 0; |
ed44da02 MM |
298 | type && TREE_CODE (type) != NAMESPACE_DECL; |
299 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
300 | ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type)) | |
301 | { | |
302 | if (TREE_CODE (type) != FUNCTION_DECL) | |
303 | { | |
304 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
305 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
306 | && ((count_specializations | |
307 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
308 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) | |
309 | ++depth; | |
310 | } | |
311 | else | |
312 | { | |
313 | if (DECL_TEMPLATE_INFO (type) | |
314 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
315 | && ((count_specializations | |
316 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
317 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
318 | ++depth; | |
319 | } | |
320 | } | |
f84b4be9 JM |
321 | |
322 | return depth; | |
e1467ff2 | 323 | } |
98c1c668 | 324 | |
39c01e4c MM |
325 | /* Returns the template nesting level of the indicated class TYPE. |
326 | Like template_class_depth_real, but instantiations do not count in | |
327 | the depth. */ | |
328 | ||
329 | int | |
330 | template_class_depth (type) | |
331 | tree type; | |
332 | { | |
333 | return template_class_depth_real (type, /*count_specializations=*/0); | |
334 | } | |
335 | ||
cae40af6 JM |
336 | /* Returns 1 if processing DECL as part of do_pending_inlines |
337 | needs us to push template parms. */ | |
338 | ||
339 | static int | |
340 | inline_needs_template_parms (decl) | |
341 | tree decl; | |
342 | { | |
343 | if (! DECL_TEMPLATE_INFO (decl)) | |
344 | return 0; | |
f84b4be9 | 345 | |
36a117a5 | 346 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
347 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
348 | } | |
349 | ||
350 | /* Subroutine of maybe_begin_member_template_processing. | |
351 | Push the template parms in PARMS, starting from LEVELS steps into the | |
352 | chain, and ending at the beginning, since template parms are listed | |
353 | innermost first. */ | |
354 | ||
355 | static void | |
356 | push_inline_template_parms_recursive (parmlist, levels) | |
357 | tree parmlist; | |
358 | int levels; | |
359 | { | |
360 | tree parms = TREE_VALUE (parmlist); | |
361 | int i; | |
362 | ||
363 | if (levels > 1) | |
364 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 365 | |
98c1c668 | 366 | ++processing_template_decl; |
cae40af6 | 367 | current_template_parms |
98c1c668 JM |
368 | = tree_cons (build_int_2 (0, processing_template_decl), |
369 | parms, current_template_parms); | |
cae40af6 JM |
370 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
371 | ||
786b5245 | 372 | pushlevel (0); |
98c1c668 JM |
373 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
374 | { | |
786b5245 MM |
375 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
376 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0); | |
cae40af6 | 377 | |
98c1c668 JM |
378 | switch (TREE_CODE (parm)) |
379 | { | |
786b5245 | 380 | case TYPE_DECL: |
73b0fce8 | 381 | case TEMPLATE_DECL: |
98c1c668 JM |
382 | pushdecl (parm); |
383 | break; | |
786b5245 MM |
384 | |
385 | case PARM_DECL: | |
386 | { | |
fc03edb3 MM |
387 | /* Make a CONST_DECL as is done in process_template_parm. |
388 | It is ugly that we recreate this here; the original | |
389 | version built in process_template_parm is no longer | |
390 | available. */ | |
786b5245 MM |
391 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
392 | TREE_TYPE (parm)); | |
fc03edb3 | 393 | SET_DECL_ARTIFICIAL (decl); |
786b5245 | 394 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
fc03edb3 | 395 | DECL_TEMPLATE_PARM_P (decl) = 1; |
786b5245 MM |
396 | pushdecl (decl); |
397 | } | |
cae40af6 | 398 | break; |
786b5245 | 399 | |
98c1c668 JM |
400 | default: |
401 | my_friendly_abort (0); | |
402 | } | |
403 | } | |
404 | } | |
405 | ||
cae40af6 JM |
406 | /* Restore the template parameter context for a member template or |
407 | a friend template defined in a class definition. */ | |
408 | ||
409 | void | |
410 | maybe_begin_member_template_processing (decl) | |
411 | tree decl; | |
412 | { | |
413 | tree parms; | |
3dcaad8b | 414 | int levels = 0; |
cae40af6 | 415 | |
3dcaad8b MM |
416 | if (inline_needs_template_parms (decl)) |
417 | { | |
418 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
419 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 420 | |
3dcaad8b MM |
421 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
422 | { | |
423 | --levels; | |
424 | parms = TREE_CHAIN (parms); | |
425 | } | |
cae40af6 | 426 | |
3dcaad8b | 427 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
428 | } |
429 | ||
3dcaad8b MM |
430 | /* Remember how many levels of template parameters we pushed so that |
431 | we can pop them later. */ | |
432 | if (!inline_parm_levels) | |
433 | VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); | |
434 | if (inline_parm_levels_used == inline_parm_levels->num_elements) | |
435 | VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); | |
436 | VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; | |
437 | ++inline_parm_levels_used; | |
cae40af6 JM |
438 | } |
439 | ||
98c1c668 JM |
440 | /* Undo the effects of begin_member_template_processing. */ |
441 | ||
442 | void | |
b370501f | 443 | maybe_end_member_template_processing () |
98c1c668 | 444 | { |
3dcaad8b MM |
445 | int i; |
446 | ||
447 | if (!inline_parm_levels_used) | |
98c1c668 JM |
448 | return; |
449 | ||
3dcaad8b MM |
450 | --inline_parm_levels_used; |
451 | for (i = 0; | |
452 | i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); | |
453 | ++i) | |
cae40af6 JM |
454 | { |
455 | --processing_template_decl; | |
456 | current_template_parms = TREE_CHAIN (current_template_parms); | |
457 | poplevel (0, 0, 0); | |
458 | } | |
98c1c668 JM |
459 | } |
460 | ||
cae40af6 | 461 | /* Returns non-zero iff T is a member template function. We must be |
f84b4be9 | 462 | careful as in |
75650646 MM |
463 | |
464 | template <class T> class C { void f(); } | |
465 | ||
466 | Here, f is a template function, and a member, but not a member | |
467 | template. This function does not concern itself with the origin of | |
468 | T, only its present state. So if we have | |
469 | ||
470 | template <class T> class C { template <class U> void f(U); } | |
471 | ||
472 | then neither C<int>::f<char> nor C<T>::f<double> is considered | |
f181d4ae MM |
473 | to be a member template. But, `template <class U> void |
474 | C<int>::f(U)' is considered a member template. */ | |
98c1c668 | 475 | |
cae40af6 JM |
476 | int |
477 | is_member_template (t) | |
98c1c668 JM |
478 | tree t; |
479 | { | |
f181d4ae | 480 | if (!DECL_FUNCTION_TEMPLATE_P (t)) |
75650646 | 481 | /* Anything that isn't a function or a template function is |
aa5f3bad MM |
482 | certainly not a member template. */ |
483 | return 0; | |
484 | ||
cae40af6 JM |
485 | /* A local class can't have member templates. */ |
486 | if (hack_decl_function_context (t)) | |
487 | return 0; | |
488 | ||
f181d4ae | 489 | return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) |
f84b4be9 JM |
490 | /* If there are more levels of template parameters than |
491 | there are template classes surrounding the declaration, | |
492 | then we have a member template. */ | |
f181d4ae MM |
493 | && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
494 | template_class_depth (DECL_CLASS_CONTEXT (t)))); | |
f84b4be9 | 495 | } |
98c1c668 | 496 | |
ba4f4e5d | 497 | #if 0 /* UNUSED */ |
2ee4e084 MM |
498 | /* Returns non-zero iff T is a member template class. See |
499 | is_member_template for a description of what precisely constitutes | |
500 | a member template. */ | |
501 | ||
502 | int | |
503 | is_member_template_class (t) | |
504 | tree t; | |
505 | { | |
506 | if (!DECL_CLASS_TEMPLATE_P (t)) | |
507 | /* Anything that isn't a class template, is certainly not a member | |
508 | template. */ | |
509 | return 0; | |
510 | ||
511 | if (!DECL_CLASS_SCOPE_P (t)) | |
512 | /* Anything whose context isn't a class type is surely not a | |
513 | member template. */ | |
514 | return 0; | |
515 | ||
516 | /* If there are more levels of template parameters than there are | |
517 | template classes surrounding the declaration, then we have a | |
518 | member template. */ | |
36a117a5 | 519 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
2ee4e084 MM |
520 | template_class_depth (DECL_CONTEXT (t))); |
521 | } | |
ba4f4e5d | 522 | #endif |
2ee4e084 | 523 | |
36a117a5 MM |
524 | /* Return a new template argument vector which contains all of ARGS, |
525 | but has as its innermost set of arguments the EXTRA_ARGS. The | |
526 | resulting vector will be built on a temporary obstack, and so must | |
527 | be explicitly copied to the permanent obstack, if required. */ | |
e6f1275f JM |
528 | |
529 | static tree | |
36a117a5 MM |
530 | add_to_template_args (args, extra_args) |
531 | tree args; | |
532 | tree extra_args; | |
e6f1275f | 533 | { |
36a117a5 MM |
534 | tree new_args; |
535 | int extra_depth; | |
536 | int i; | |
537 | int j; | |
e6f1275f | 538 | |
36a117a5 MM |
539 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
540 | new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth); | |
e6f1275f | 541 | |
36a117a5 MM |
542 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
543 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 544 | |
36a117a5 MM |
545 | for (j = 1; j <= extra_depth; ++j, ++i) |
546 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
547 | ||
e6f1275f JM |
548 | return new_args; |
549 | } | |
550 | ||
36a117a5 MM |
551 | /* Like add_to_template_args, but only the outermost ARGS are added to |
552 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
553 | (EXTRA_ARGS) levels are added. This function is used to combine | |
554 | the template arguments from a partial instantiation with the | |
555 | template arguments used to attain the full instantiation from the | |
556 | partial instantiation. */ | |
98c1c668 | 557 | |
4966381a | 558 | static tree |
36a117a5 | 559 | add_outermost_template_args (args, extra_args) |
98c1c668 JM |
560 | tree args; |
561 | tree extra_args; | |
562 | { | |
563 | tree new_args; | |
564 | ||
e4a84209 MM |
565 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
566 | something very fishy is going on. */ | |
567 | my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), | |
568 | 0); | |
569 | ||
570 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
571 | them. */ | |
572 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
573 | return extra_args; | |
574 | ||
36a117a5 MM |
575 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
576 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
577 | ||
578 | new_args = add_to_template_args (args, extra_args); | |
98c1c668 | 579 | |
36a117a5 MM |
580 | /* Now, we restore ARGS to its full dimensions. */ |
581 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
582 | |
583 | return new_args; | |
584 | } | |
5566b478 MS |
585 | |
586 | /* We've got a template header coming up; push to a new level for storing | |
587 | the parms. */ | |
8d08fdba | 588 | |
8d08fdba MS |
589 | void |
590 | begin_template_parm_list () | |
591 | { | |
6757edfe MM |
592 | /* We use a non-tag-transparent scope here, which causes pushtag to |
593 | put tags in this scope, rather than in the enclosing class or | |
594 | namespace scope. This is the right thing, since we want | |
595 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
596 | global template class, push_template_decl handles putting the | |
597 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
598 | e.g.: | |
599 | ||
600 | template <class T> struct S1 { | |
601 | template <class T> struct S2 {}; | |
602 | }; | |
603 | ||
604 | pushtag contains special code to call pushdecl_with_scope on the | |
605 | TEMPLATE_DECL for S2. */ | |
8d08fdba | 606 | pushlevel (0); |
5566b478 | 607 | declare_pseudo_global_level (); |
5156628f | 608 | ++processing_template_decl; |
67ffc812 | 609 | ++processing_template_parmlist; |
386b8a85 JM |
610 | note_template_header (0); |
611 | } | |
612 | ||
6c30752f MM |
613 | /* This routine is called when a specialization is declared. If it is |
614 | illegal to declare a specialization here, an error is reported. */ | |
615 | ||
e9659ab0 | 616 | static void |
6c30752f MM |
617 | check_specialization_scope () |
618 | { | |
619 | tree scope = current_scope (); | |
3ddfb0e6 | 620 | |
6c30752f MM |
621 | /* [temp.expl.spec] |
622 | ||
623 | An explicit specialization shall be declared in the namespace of | |
624 | which the template is a member, or, for member templates, in the | |
625 | namespace of which the enclosing class or enclosing class | |
626 | template is a member. An explicit specialization of a member | |
627 | function, member class or static data member of a class template | |
628 | shall be declared in the namespace of which the class template | |
629 | is a member. */ | |
630 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
8251199e | 631 | cp_error ("explicit specialization in non-namespace scope `%D'", |
6c30752f | 632 | scope); |
3ddfb0e6 | 633 | |
6c30752f MM |
634 | /* [temp.expl.spec] |
635 | ||
636 | In an explicit specialization declaration for a member of a class | |
637 | template or a member template that appears in namespace scope, | |
638 | the member template and some of its enclosing class templates may | |
639 | remain unspecialized, except that the declaration shall not | |
640 | explicitly specialize a class member template if its enclosing | |
641 | class templates are not explicitly specialized as well. */ | |
642 | if (current_template_parms) | |
fd4de5ff | 643 | cp_error ("enclosing class templates are not explicitly specialized"); |
6c30752f MM |
644 | } |
645 | ||
386b8a85 JM |
646 | /* We've just seen template <>. */ |
647 | ||
648 | void | |
649 | begin_specialization () | |
650 | { | |
651 | note_template_header (1); | |
6c30752f | 652 | check_specialization_scope (); |
386b8a85 JM |
653 | } |
654 | ||
386b8a85 JM |
655 | /* Called at then end of processing a declaration preceeded by |
656 | template<>. */ | |
657 | ||
658 | void | |
659 | end_specialization () | |
660 | { | |
661 | reset_specialization (); | |
662 | } | |
663 | ||
386b8a85 JM |
664 | /* Any template <>'s that we have seen thus far are not referring to a |
665 | function specialization. */ | |
666 | ||
667 | void | |
668 | reset_specialization () | |
669 | { | |
670 | processing_specialization = 0; | |
671 | template_header_count = 0; | |
672 | } | |
673 | ||
386b8a85 JM |
674 | /* We've just seen a template header. If SPECIALIZATION is non-zero, |
675 | it was of the form template <>. */ | |
676 | ||
4966381a | 677 | static void |
386b8a85 JM |
678 | note_template_header (specialization) |
679 | int specialization; | |
680 | { | |
681 | processing_specialization = specialization; | |
682 | template_header_count++; | |
683 | } | |
684 | ||
75650646 | 685 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 686 | |
75650646 MM |
687 | void |
688 | begin_explicit_instantiation () | |
386b8a85 | 689 | { |
75650646 MM |
690 | ++processing_explicit_instantiation; |
691 | } | |
386b8a85 | 692 | |
386b8a85 | 693 | |
75650646 MM |
694 | void |
695 | end_explicit_instantiation () | |
696 | { | |
697 | my_friendly_assert(processing_explicit_instantiation > 0, 0); | |
698 | --processing_explicit_instantiation; | |
699 | } | |
386b8a85 | 700 | |
36a117a5 MM |
701 | /* The TYPE is being declared. If it is a template type, that means it |
702 | is a partial specialization. Do appropriate error-checking. */ | |
703 | ||
704 | void | |
705 | maybe_process_partial_specialization (type) | |
706 | tree type; | |
707 | { | |
708 | if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type)) | |
709 | { | |
710 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) | |
711 | && TYPE_SIZE (type) == NULL_TREE) | |
712 | { | |
700466c2 JM |
713 | if (current_namespace |
714 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
715 | { | |
716 | cp_pedwarn ("specializing `%#T' in different namespace", type); | |
717 | cp_pedwarn_at (" from definition of `%#D'", | |
718 | CLASSTYPE_TI_TEMPLATE (type)); | |
719 | } | |
36a117a5 MM |
720 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
721 | if (processing_template_decl) | |
722 | push_template_decl (TYPE_MAIN_DECL (type)); | |
723 | } | |
724 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
8251199e | 725 | cp_error ("specialization of `%T' after instantiation", type); |
36a117a5 | 726 | } |
20496fa2 JM |
727 | else if (processing_specialization) |
728 | cp_error ("explicit specialization of non-template `%T'", type); | |
36a117a5 MM |
729 | } |
730 | ||
75650646 MM |
731 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
732 | specialization is either an instantiation or an explicit | |
733 | specialization) of TMPL for the given template ARGS. If there is | |
734 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
735 | arguments, or a vector of vectors of arguments, in the case of | |
736 | templates with more than one level of parameters. */ | |
737 | ||
738 | static tree | |
739 | retrieve_specialization (tmpl, args) | |
740 | tree tmpl; | |
741 | tree args; | |
742 | { | |
743 | tree s; | |
744 | ||
745 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
746 | ||
36a117a5 MM |
747 | /* There should be as many levels of arguments as there are |
748 | levels of parameters. */ | |
749 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
750 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
751 | 0); | |
752 | ||
75650646 MM |
753 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
754 | s != NULL_TREE; | |
755 | s = TREE_CHAIN (s)) | |
756 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
757 | return TREE_VALUE (s); | |
758 | ||
759 | return NULL_TREE; | |
386b8a85 JM |
760 | } |
761 | ||
9188c363 MM |
762 | /* Like retrieve_speciailization, but for local declarations. FN is |
763 | the function in which we are looking for an instantiation. */ | |
764 | ||
765 | static tree | |
766 | retrieve_local_specialization (tmpl, fn) | |
767 | tree tmpl; | |
768 | tree fn; | |
769 | { | |
770 | tree s = purpose_member (fn, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
771 | return s ? TREE_VALUE (s) : NULL_TREE; | |
772 | } | |
773 | ||
6757edfe MM |
774 | /* Returns non-zero iff DECL is a specialization of TMPL. */ |
775 | ||
776 | int | |
777 | is_specialization_of (decl, tmpl) | |
778 | tree decl; | |
779 | tree tmpl; | |
780 | { | |
781 | tree t; | |
782 | ||
783 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
784 | { | |
785 | for (t = decl; | |
786 | t != NULL_TREE; | |
787 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
788 | if (t == tmpl) | |
789 | return 1; | |
790 | } | |
791 | else | |
792 | { | |
793 | my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); | |
794 | ||
795 | for (t = TREE_TYPE (decl); | |
796 | t != NULL_TREE; | |
797 | t = CLASSTYPE_USE_TEMPLATE (t) | |
798 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
3bfdc719 MM |
799 | if (same_type_p (TYPE_MAIN_VARIANT (t), |
800 | TYPE_MAIN_VARIANT (TREE_TYPE (tmpl)))) | |
6757edfe MM |
801 | return 1; |
802 | } | |
386b8a85 | 803 | |
6757edfe MM |
804 | return 0; |
805 | } | |
75650646 MM |
806 | |
807 | /* Register the specialization SPEC as a specialization of TMPL with | |
36a117a5 MM |
808 | the indicated ARGS. Returns SPEC, or an equivalent prior |
809 | declaration, if available. */ | |
75650646 | 810 | |
36a117a5 | 811 | static tree |
75650646 MM |
812 | register_specialization (spec, tmpl, args) |
813 | tree spec; | |
814 | tree tmpl; | |
815 | tree args; | |
816 | { | |
817 | tree s; | |
818 | ||
819 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
820 | ||
36a117a5 MM |
821 | if (TREE_CODE (spec) == FUNCTION_DECL |
822 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
823 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
824 | register it; we want the corresponding TEMPLATE_DECL instead. | |
825 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
826 | the more obvious `uses_template_parms (spec)' to avoid problems | |
827 | with default function arguments. In particular, given | |
828 | something like this: | |
829 | ||
830 | template <class T> void f(T t1, T t = T()) | |
831 | ||
832 | the default argument expression is not substituted for in an | |
833 | instantiation unless and until it is actually needed. */ | |
834 | return spec; | |
75650646 | 835 | |
36a117a5 MM |
836 | /* There should be as many levels of arguments as there are |
837 | levels of parameters. */ | |
838 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
839 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
840 | 0); | |
841 | ||
75650646 MM |
842 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
843 | s != NULL_TREE; | |
844 | s = TREE_CHAIN (s)) | |
22e9174f MM |
845 | { |
846 | tree fn = TREE_VALUE (s); | |
847 | ||
848 | /* We can sometimes try to re-register a specialization that we've | |
849 | already got. In particular, regenerate_decl_from_template | |
850 | calls duplicate_decls which will update the specialization | |
851 | list. But, we'll still get called again here anyhow. It's | |
852 | more convenient to simply allow this than to try to prevent it. */ | |
853 | if (fn == spec) | |
854 | return spec; | |
855 | else if (comp_template_args (TREE_PURPOSE (s), args)) | |
856 | { | |
857 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
858 | { | |
859 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
860 | { | |
861 | if (TREE_USED (fn) | |
862 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
863 | { | |
864 | cp_error ("specialization of %D after instantiation", | |
865 | fn); | |
866 | return spec; | |
867 | } | |
868 | else | |
869 | { | |
870 | /* This situation should occur only if the first | |
871 | specialization is an implicit instantiation, | |
872 | the second is an explicit specialization, and | |
873 | the implicit instantiation has not yet been | |
874 | used. That situation can occur if we have | |
875 | implicitly instantiated a member function and | |
876 | then specialized it later. | |
877 | ||
878 | We can also wind up here if a friend | |
879 | declaration that looked like an instantiation | |
880 | turns out to be a specialization: | |
881 | ||
882 | template <class T> void foo(T); | |
883 | class S { friend void foo<>(int) }; | |
884 | template <> void foo(int); | |
885 | ||
886 | We transform the existing DECL in place so that | |
887 | any pointers to it become pointers to the | |
888 | updated declaration. | |
889 | ||
890 | If there was a definition for the template, but | |
891 | not for the specialization, we want this to | |
892 | look as if there is no definition, and vice | |
893 | versa. */ | |
894 | DECL_INITIAL (fn) = NULL_TREE; | |
895 | duplicate_decls (spec, fn); | |
896 | ||
897 | return fn; | |
898 | } | |
899 | } | |
900 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
901 | { | |
902 | duplicate_decls (spec, fn); | |
903 | return fn; | |
904 | } | |
905 | } | |
906 | } | |
75650646 MM |
907 | } |
908 | ||
909 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
910 | = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
911 | |
912 | return spec; | |
913 | } | |
914 | ||
915 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
916 | Returns nonzero if the SPEC was listed as a specialization of | |
917 | TMPL. */ | |
918 | ||
919 | static int | |
920 | unregister_specialization (spec, tmpl) | |
921 | tree spec; | |
922 | tree tmpl; | |
923 | { | |
924 | tree* s; | |
925 | ||
926 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
927 | *s != NULL_TREE; | |
928 | s = &TREE_CHAIN (*s)) | |
929 | if (TREE_VALUE (*s) == spec) | |
930 | { | |
931 | *s = TREE_CHAIN (*s); | |
932 | return 1; | |
933 | } | |
934 | ||
935 | return 0; | |
75650646 MM |
936 | } |
937 | ||
9188c363 MM |
938 | /* Like register_specialization, but for local declarations. FN is |
939 | the function in which we are registering SPEC, an instantiation of | |
940 | TMPL. */ | |
941 | ||
942 | static tree | |
943 | register_local_specialization (spec, tmpl, fn) | |
944 | tree spec; | |
945 | tree tmpl; | |
946 | tree fn; | |
947 | { | |
948 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
949 | = perm_tree_cons (fn, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
950 | ||
951 | return spec; | |
952 | } | |
953 | ||
e1467ff2 MM |
954 | /* Print the list of candidate FNS in an error message. */ |
955 | ||
104bf76a | 956 | void |
e1467ff2 MM |
957 | print_candidates (fns) |
958 | tree fns; | |
959 | { | |
960 | tree fn; | |
961 | ||
d8e178a0 | 962 | const char *str = "candidates are:"; |
e1467ff2 MM |
963 | |
964 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
965 | { | |
d6479fe7 MM |
966 | tree f; |
967 | ||
968 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
969 | cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); | |
e1467ff2 MM |
970 | str = " "; |
971 | } | |
972 | } | |
973 | ||
75650646 | 974 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 975 | which can be specialized to match the indicated DECL with the |
03017874 MM |
976 | explicit template args given in TEMPLATE_ID. The DECL may be |
977 | NULL_TREE if none is available. In that case, the functions in | |
978 | TEMPLATE_ID are non-members. | |
979 | ||
980 | If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a | |
981 | specialization of a member template. | |
982 | ||
983 | The template args (those explicitly specified and those deduced) | |
984 | are output in a newly created vector *TARGS_OUT. | |
985 | ||
986 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 987 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 988 | |
e9659ab0 | 989 | static tree |
e1467ff2 | 990 | determine_specialization (template_id, decl, targs_out, |
bf8f3f93 | 991 | need_member_template) |
386b8a85 | 992 | tree template_id; |
e1467ff2 | 993 | tree decl; |
386b8a85 JM |
994 | tree* targs_out; |
995 | int need_member_template; | |
386b8a85 | 996 | { |
03017874 MM |
997 | tree fns; |
998 | tree targs; | |
999 | tree explicit_targs; | |
1000 | tree candidates = NULL_TREE; | |
1001 | tree templates = NULL_TREE; | |
386b8a85 | 1002 | |
e1467ff2 MM |
1003 | *targs_out = NULL_TREE; |
1004 | ||
f2e48b67 BK |
1005 | if (template_id == error_mark_node) |
1006 | return error_mark_node; | |
1007 | ||
1008 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1009 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1010 | |
27fafc8d JM |
1011 | if (fns == error_mark_node) |
1012 | return error_mark_node; | |
1013 | ||
2c73f9f5 | 1014 | /* Check for baselinks. */ |
91e490ab | 1015 | if (BASELINK_P (fns)) |
2c73f9f5 | 1016 | fns = TREE_VALUE (fns); |
386b8a85 | 1017 | |
91e490ab MM |
1018 | if (!is_overloaded_fn (fns)) |
1019 | { | |
9dfce8fd | 1020 | cp_error ("`%D' is not a function template", fns); |
91e490ab MM |
1021 | return error_mark_node; |
1022 | } | |
1023 | ||
2c73f9f5 | 1024 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1025 | { |
75650646 MM |
1026 | tree tmpl; |
1027 | ||
9dfce8fd | 1028 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1029 | |
1030 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1031 | /* DECL might be a specialization of FN. */ | |
1032 | tmpl = fn; | |
1033 | else if (need_member_template) | |
1034 | /* FN is an ordinary member function, and we need a | |
1035 | specialization of a member template. */ | |
1036 | continue; | |
1037 | else if (TREE_CODE (fn) != FUNCTION_DECL) | |
1038 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1039 | cases. */ | |
1040 | continue; | |
1041 | else if (!DECL_FUNCTION_MEMBER_P (fn)) | |
1042 | /* This is just an ordinary non-member function. Nothing can | |
1043 | be a specialization of that. */ | |
1044 | continue; | |
75650646 | 1045 | else |
03017874 MM |
1046 | { |
1047 | tree decl_arg_types; | |
386b8a85 | 1048 | |
03017874 MM |
1049 | /* This is an ordinary member function. However, since |
1050 | we're here, we can assume it's enclosing class is a | |
1051 | template class. For example, | |
1052 | ||
1053 | template <typename T> struct S { void f(); }; | |
1054 | template <> void S<int>::f() {} | |
1055 | ||
1056 | Here, S<int>::f is a non-template, but S<int> is a | |
1057 | template class. If FN has the same type as DECL, we | |
1058 | might be in business. */ | |
1059 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), | |
1060 | TREE_TYPE (TREE_TYPE (fn)))) | |
1061 | /* The return types differ. */ | |
1062 | continue; | |
1063 | ||
1064 | /* Adjust the type of DECL in case FN is a static member. */ | |
1065 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1066 | if (DECL_STATIC_FUNCTION_P (fn) | |
1067 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1068 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1069 | ||
1070 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1071 | decl_arg_types)) | |
1072 | /* They match! */ | |
1073 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1074 | ||
1075 | continue; | |
1076 | } | |
386b8a85 | 1077 | |
bf8f3f93 MM |
1078 | /* See whether this function might be a specialization of this |
1079 | template. */ | |
1080 | targs = get_bindings (tmpl, decl, explicit_targs); | |
03017874 MM |
1081 | |
1082 | if (!targs) | |
bf8f3f93 | 1083 | /* We cannot deduce template arguments that when used to |
03017874 MM |
1084 | specialize TMPL will produce DECL. */ |
1085 | continue; | |
1086 | ||
1087 | /* Save this template, and the arguments deduced. */ | |
1088 | templates = scratch_tree_cons (targs, tmpl, templates); | |
386b8a85 | 1089 | } |
03017874 | 1090 | |
bf8f3f93 | 1091 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1092 | { |
03017874 MM |
1093 | /* We have: |
1094 | ||
1095 | [temp.expl.spec] | |
1096 | ||
1097 | It is possible for a specialization with a given function | |
1098 | signature to be instantiated from more than one function | |
1099 | template. In such cases, explicit specification of the | |
1100 | template arguments must be used to uniquely identify the | |
1101 | function template specialization being specialized. | |
1102 | ||
1103 | Note that here, there's no suggestion that we're supposed to | |
1104 | determine which of the candidate templates is most | |
1105 | specialized. However, we, also have: | |
1106 | ||
1107 | [temp.func.order] | |
1108 | ||
1109 | Partial ordering of overloaded function template | |
1110 | declarations is used in the following contexts to select | |
1111 | the function template to which a function template | |
1112 | specialization refers: | |
1113 | ||
1114 | -- when an explicit specialization refers to a function | |
1115 | template. | |
1116 | ||
1117 | So, we do use the partial ordering rules, at least for now. | |
1118 | This extension can only serve to make illegal programs legal, | |
1119 | so it's safe. And, there is strong anecdotal evidence that | |
1120 | the committee intended the partial ordering rules to apply; | |
1121 | the EDG front-end has that behavior, and John Spicer claims | |
1122 | that the committee simply forgot to delete the wording in | |
1123 | [temp.expl.spec]. */ | |
1124 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1125 | if (tmpl && tmpl != error_mark_node) | |
1126 | { | |
1127 | targs = get_bindings (tmpl, decl, explicit_targs); | |
1128 | templates = scratch_tree_cons (targs, tmpl, NULL_TREE); | |
1129 | } | |
e1467ff2 MM |
1130 | } |
1131 | ||
03017874 | 1132 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1133 | { |
bf8f3f93 MM |
1134 | cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", |
1135 | template_id, decl); | |
03017874 | 1136 | return error_mark_node; |
386b8a85 | 1137 | } |
03017874 | 1138 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1139 | || (candidates && TREE_CHAIN (candidates)) |
1140 | || (templates && candidates)) | |
386b8a85 | 1141 | { |
bf8f3f93 MM |
1142 | cp_error_at ("ambiguous template specialization `%D' for `%+D'", |
1143 | template_id, decl); | |
1144 | chainon (candidates, templates); | |
1145 | print_candidates (candidates); | |
03017874 | 1146 | return error_mark_node; |
386b8a85 JM |
1147 | } |
1148 | ||
1149 | /* We have one, and exactly one, match. */ | |
03017874 MM |
1150 | if (candidates) |
1151 | { | |
1152 | /* It was a specialization of an ordinary member function in a | |
1153 | template class. */ | |
1154 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1155 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1156 | } | |
1157 | ||
1158 | /* It was a specialization of a template. */ | |
1159 | targs = DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates))); | |
1160 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) | |
1161 | { | |
1162 | *targs_out = copy_node (targs); | |
1163 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1164 | TMPL_ARGS_DEPTH (*targs_out), | |
1165 | TREE_PURPOSE (templates)); | |
1166 | } | |
1167 | else | |
1168 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1169 | return TREE_VALUE (templates); |
8d08fdba | 1170 | } |
6757edfe | 1171 | |
386b8a85 | 1172 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1173 | DECLARATOR, and in DECL, is a specialization of a function |
1174 | template. We may also discover that the declaration is an explicit | |
1175 | instantiation at this point. | |
1176 | ||
e1467ff2 | 1177 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1178 | instead if all goes well. Issues an error message if something is |
1179 | amiss. Returns error_mark_node if the error is not easily | |
1180 | recoverable. | |
75650646 | 1181 | |
75650646 MM |
1182 | FLAGS is a bitmask consisting of the following flags: |
1183 | ||
75650646 MM |
1184 | 2: The function has a definition. |
1185 | 4: The function is a friend. | |
75650646 MM |
1186 | |
1187 | The TEMPLATE_COUNT is the number of references to qualifying | |
1188 | template classes that appeared in the name of the function. For | |
1189 | example, in | |
1190 | ||
1191 | template <class T> struct S { void f(); }; | |
1192 | void S<int>::f(); | |
1193 | ||
1194 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1195 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1196 | ||
1197 | template <class T> struct S {}; | |
1198 | template <> struct S<int> { void f(); } | |
36a117a5 | 1199 | template <> void S<int>::f(); |
75650646 MM |
1200 | |
1201 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
1202 | illegal; there should be no template <>.) | |
1203 | ||
1204 | If the function is a specialization, it is marked as such via | |
1205 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1206 | is set up correctly, and it is added to the list of specializations | |
1207 | for that template. */ | |
386b8a85 | 1208 | |
e1467ff2 | 1209 | tree |
27bb8339 | 1210 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
1211 | tree declarator; |
1212 | tree decl; | |
1213 | int template_count; | |
1214 | int flags; | |
1215 | { | |
75650646 MM |
1216 | int have_def = flags & 2; |
1217 | int is_friend = flags & 4; | |
1218 | int specialization = 0; | |
e1467ff2 | 1219 | int explicit_instantiation = 0; |
fd4de5ff | 1220 | int member_specialization = 0; |
75650646 MM |
1221 | |
1222 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
1223 | tree dname = DECL_NAME (decl); | |
386b8a85 | 1224 | |
fd4de5ff | 1225 | if (processing_specialization) |
386b8a85 | 1226 | { |
fd4de5ff | 1227 | /* The last template header was of the form template <>. */ |
75650646 | 1228 | |
fd4de5ff MM |
1229 | if (template_header_count > template_count) |
1230 | { | |
1231 | /* There were more template headers than qualifying template | |
1232 | classes. */ | |
1233 | if (template_header_count - template_count > 1) | |
03017874 MM |
1234 | /* There shouldn't be that many template parameter lists. |
1235 | There can be at most one parameter list for every | |
1236 | qualifying class, plus one for the function itself. */ | |
fd4de5ff | 1237 | cp_error ("too many template parameter lists in declaration of `%D'", decl); |
75650646 | 1238 | |
fd4de5ff MM |
1239 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
1240 | if (ctype) | |
1241 | member_specialization = 1; | |
1242 | else | |
1243 | specialization = 1; | |
75650646 | 1244 | } |
fd4de5ff | 1245 | else if (template_header_count == template_count) |
75650646 | 1246 | { |
fd4de5ff MM |
1247 | /* The counts are equal. So, this might be a |
1248 | specialization, but it is not a specialization of a | |
1249 | member template. It might be something like | |
1250 | ||
1251 | template <class T> struct S { | |
1252 | void f(int i); | |
1253 | }; | |
1254 | template <> | |
1255 | void S<int>::f(int i) {} */ | |
1256 | specialization = 1; | |
1257 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1258 | } |
fd4de5ff | 1259 | else |
386b8a85 | 1260 | { |
fd4de5ff MM |
1261 | /* This cannot be an explicit specialization. There are not |
1262 | enough headers for all of the qualifying classes. For | |
1263 | example, we might have: | |
1264 | ||
1265 | template <> | |
1266 | void S<int>::T<char>::f(); | |
75650646 | 1267 | |
fd4de5ff MM |
1268 | But, we're missing another template <>. */ |
1269 | cp_error("too few template parameter lists in declaration of `%D'", decl); | |
1270 | return decl; | |
1271 | } | |
1272 | } | |
1273 | else if (processing_explicit_instantiation) | |
1274 | { | |
1275 | if (template_header_count) | |
1276 | cp_error ("template parameter list used in explicit instantiation"); | |
1277 | ||
1278 | if (have_def) | |
1279 | cp_error ("definition provided for explicit instantiation"); | |
75650646 | 1280 | |
fd4de5ff MM |
1281 | explicit_instantiation = 1; |
1282 | } | |
1283 | else if (ctype != NULL_TREE | |
1284 | && !TYPE_BEING_DEFINED (ctype) | |
1285 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) | |
1286 | && !is_friend) | |
1287 | { | |
1288 | /* This case catches outdated code that looks like this: | |
1289 | ||
1290 | template <class T> struct S { void f(); }; | |
1291 | void S<int>::f() {} // Missing template <> | |
1292 | ||
1293 | We disable this check when the type is being defined to | |
1294 | avoid complaining about default compiler-generated | |
1295 | constructors, destructors, and assignment operators. | |
1296 | Since the type is an instantiation, not a specialization, | |
1297 | these are the only functions that can be defined before | |
1298 | the class is complete. */ | |
75650646 | 1299 | |
c6f2ed0d JM |
1300 | /* If they said |
1301 | template <class T> void S<int>::f() {} | |
1302 | that's bogus. */ | |
fd4de5ff MM |
1303 | if (template_header_count) |
1304 | { | |
1305 | cp_error ("template parameters specified in specialization"); | |
1306 | return decl; | |
c6f2ed0d | 1307 | } |
fd4de5ff MM |
1308 | |
1309 | if (pedantic) | |
1310 | cp_pedwarn | |
1311 | ("explicit specialization not preceded by `template <>'"); | |
1312 | specialization = 1; | |
1313 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1314 | } | |
1315 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
1316 | { | |
1317 | if (is_friend) | |
1318 | /* This could be something like: | |
1319 | ||
1320 | template <class T> void f(T); | |
1321 | class S { friend void f<>(int); } */ | |
1322 | specialization = 1; | |
1323 | else | |
c6f2ed0d | 1324 | { |
fd4de5ff MM |
1325 | /* This case handles bogus declarations like template <> |
1326 | template <class T> void f<int>(); */ | |
75650646 | 1327 | |
fd4de5ff MM |
1328 | cp_error ("template-id `%D' in declaration of primary template", |
1329 | declarator); | |
1330 | return decl; | |
386b8a85 | 1331 | } |
75650646 | 1332 | } |
386b8a85 | 1333 | |
670960ac JM |
1334 | if (specialization || member_specialization) |
1335 | { | |
1336 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1337 | for (; t; t = TREE_CHAIN (t)) | |
1338 | if (TREE_PURPOSE (t)) | |
1339 | { | |
8251199e JM |
1340 | cp_pedwarn |
1341 | ("default argument specified in explicit specialization"); | |
670960ac JM |
1342 | break; |
1343 | } | |
f3400fe2 JM |
1344 | if (current_lang_name == lang_name_c) |
1345 | cp_error ("template specialization with C linkage"); | |
670960ac JM |
1346 | } |
1347 | ||
e1467ff2 | 1348 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1349 | { |
1350 | tree tmpl = NULL_TREE; | |
1351 | tree targs = NULL_TREE; | |
75650646 MM |
1352 | |
1353 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1354 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1355 | { | |
1356 | tree fns; | |
1357 | ||
1358 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1359 | 0); | |
1360 | if (!ctype) | |
30394414 | 1361 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); |
386b8a85 JM |
1362 | else |
1363 | fns = dname; | |
1364 | ||
75650646 MM |
1365 | declarator = |
1366 | lookup_template_function (fns, NULL_TREE); | |
386b8a85 JM |
1367 | } |
1368 | ||
f2e48b67 BK |
1369 | if (declarator == error_mark_node) |
1370 | return error_mark_node; | |
1371 | ||
75650646 MM |
1372 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1373 | { | |
8ec2ac34 | 1374 | if (!explicit_instantiation) |
6c30752f MM |
1375 | /* A specialization in class scope. This is illegal, |
1376 | but the error will already have been flagged by | |
1377 | check_specialization_scope. */ | |
1378 | return error_mark_node; | |
8ec2ac34 | 1379 | else |
b370501f KG |
1380 | { |
1381 | /* It's not legal to write an explicit instantiation in | |
1382 | class scope, e.g.: | |
8ec2ac34 | 1383 | |
b370501f | 1384 | class C { template void f(); } |
8ec2ac34 | 1385 | |
b370501f KG |
1386 | This case is caught by the parser. However, on |
1387 | something like: | |
8ec2ac34 | 1388 | |
b370501f | 1389 | template class C { void f(); }; |
8ec2ac34 | 1390 | |
b370501f KG |
1391 | (which is illegal) we can get here. The error will be |
1392 | issued later. */ | |
1393 | ; | |
1394 | } | |
8ec2ac34 | 1395 | |
e1467ff2 | 1396 | return decl; |
75650646 | 1397 | } |
8f032717 MM |
1398 | else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) |
1399 | { | |
1400 | /* A friend declaration. We can't do much, because we don't | |
1401 | know what this resolves to, yet. */ | |
1402 | my_friendly_assert (is_friend != 0, 0); | |
1403 | my_friendly_assert (!explicit_instantiation, 0); | |
1404 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
1405 | return decl; | |
1406 | } | |
75650646 MM |
1407 | else if (ctype != NULL_TREE |
1408 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1409 | IDENTIFIER_NODE)) | |
386b8a85 | 1410 | { |
75650646 MM |
1411 | /* Find the list of functions in ctype that have the same |
1412 | name as the declared function. */ | |
1413 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
1414 | tree fns = NULL_TREE; |
1415 | int idx; | |
1416 | ||
75650646 MM |
1417 | if (name == constructor_name (ctype) |
1418 | || name == constructor_name_full (ctype)) | |
386b8a85 | 1419 | { |
75650646 MM |
1420 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1421 | ||
1422 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1423 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1424 | { | |
1425 | /* From [temp.expl.spec]: | |
e1467ff2 | 1426 | |
75650646 MM |
1427 | If such an explicit specialization for the member |
1428 | of a class template names an implicitly-declared | |
1429 | special member function (clause _special_), the | |
e1467ff2 MM |
1430 | program is ill-formed. |
1431 | ||
1432 | Similar language is found in [temp.explicit]. */ | |
8251199e | 1433 | cp_error ("specialization of implicitly-declared special member function"); |
03017874 | 1434 | return error_mark_node; |
75650646 | 1435 | } |
386b8a85 | 1436 | |
42da2fd8 | 1437 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1438 | } |
42da2fd8 | 1439 | |
03017874 MM |
1440 | if (!IDENTIFIER_TYPENAME_P (name)) |
1441 | { | |
1442 | idx = lookup_fnfields_1 (ctype, name); | |
1443 | if (idx >= 0) | |
1444 | fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); | |
1445 | } | |
1446 | else | |
1447 | { | |
1448 | tree methods; | |
1449 | ||
1450 | /* For a type-conversion operator, we cannot do a | |
1451 | name-based lookup. We might be looking for `operator | |
1452 | int' which will be a specialization of `operator T'. | |
1453 | So, we find *all* the conversion operators, and then | |
1454 | select from them. */ | |
1455 | fns = NULL_TREE; | |
1456 | ||
1457 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1458 | if (methods) | |
1459 | for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx) | |
1460 | { | |
1461 | tree ovl = TREE_VEC_ELT (methods, idx); | |
1462 | ||
1463 | if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) | |
1464 | /* There are no more conversion functions. */ | |
1465 | break; | |
1466 | ||
1467 | /* Glue all these conversion functions together | |
1468 | with those we already have. */ | |
1469 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1470 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1471 | } | |
1472 | } | |
1473 | ||
386b8a85 JM |
1474 | if (fns == NULL_TREE) |
1475 | { | |
b3f738da MM |
1476 | cp_error ("no member function `%D' declared in `%T'", |
1477 | name, ctype); | |
03017874 | 1478 | return error_mark_node; |
386b8a85 JM |
1479 | } |
1480 | else | |
1481 | TREE_OPERAND (declarator, 0) = fns; | |
1482 | } | |
75650646 | 1483 | |
e1467ff2 MM |
1484 | /* Figure out what exactly is being specialized at this point. |
1485 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1486 | member function, we cannot tell apriori whether the |
e1467ff2 | 1487 | instantiation is for a member template, or just a member |
36a117a5 MM |
1488 | function of a template class. Even if a member template is |
1489 | being instantiated, the member template arguments may be | |
1490 | elided if they can be deduced from the rest of the | |
1491 | declaration. */ | |
e1467ff2 MM |
1492 | tmpl = determine_specialization (declarator, decl, |
1493 | &targs, | |
bf8f3f93 | 1494 | member_specialization); |
386b8a85 | 1495 | |
03017874 MM |
1496 | if (!tmpl || tmpl == error_mark_node) |
1497 | /* We couldn't figure out what this declaration was | |
1498 | specializing. */ | |
1499 | return error_mark_node; | |
1500 | else | |
386b8a85 | 1501 | { |
25aab5d0 | 1502 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 1503 | |
e1467ff2 MM |
1504 | if (explicit_instantiation) |
1505 | { | |
03d0f4af | 1506 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
25aab5d0 MM |
1507 | is done by do_decl_instantiation later. */ |
1508 | ||
1509 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
1510 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
1511 | ||
1512 | if (arg_depth > parm_depth) | |
1513 | { | |
1514 | /* If TMPL is not the most general template (for | |
1515 | example, if TMPL is a friend template that is | |
1516 | injected into namespace scope), then there will | |
1517 | be too many levels fo TARGS. Remove some of them | |
1518 | here. */ | |
1519 | int i; | |
1520 | tree new_targs; | |
1521 | ||
1522 | new_targs = make_temp_vec (parm_depth); | |
1523 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) | |
1524 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
1525 | = TREE_VEC_ELT (targs, i); | |
1526 | targs = new_targs; | |
1527 | } | |
1528 | ||
1529 | decl = instantiate_template (tmpl, targs); | |
e1467ff2 MM |
1530 | return decl; |
1531 | } | |
36a117a5 MM |
1532 | |
1533 | /* If we though that the DECL was a member function, but it | |
1534 | turns out to be specializing a static member function, | |
1535 | make DECL a static member function as well. */ | |
1536 | if (DECL_STATIC_FUNCTION_P (tmpl) | |
1537 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
366c0f1e MM |
1538 | { |
1539 | revert_static_member_fn (&decl, 0, 0); | |
1540 | last_function_parms = TREE_CHAIN (last_function_parms); | |
1541 | } | |
e1467ff2 | 1542 | |
36a117a5 MM |
1543 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
1544 | DECL_TEMPLATE_INFO (decl) | |
1545 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
1546 | ||
e1467ff2 MM |
1547 | /* Mangle the function name appropriately. Note that we do |
1548 | not mangle specializations of non-template member | |
1549 | functions of template classes, e.g. with | |
36a117a5 | 1550 | |
e1467ff2 | 1551 | template <class T> struct S { void f(); } |
36a117a5 | 1552 | |
e1467ff2 | 1553 | and given the specialization |
36a117a5 | 1554 | |
e1467ff2 | 1555 | template <> void S<int>::f() {} |
36a117a5 | 1556 | |
e1467ff2 MM |
1557 | we do not mangle S<int>::f() here. That's because it's |
1558 | just an ordinary member function and doesn't need special | |
36a117a5 MM |
1559 | treatment. We do this here so that the ordinary, |
1560 | non-template, name-mangling algorith will not be used | |
1561 | later. */ | |
e1467ff2 | 1562 | if ((is_member_template (tmpl) || ctype == NULL_TREE) |
75650646 | 1563 | && name_mangling_version >= 1) |
36a117a5 | 1564 | set_mangled_name_for_template_decl (decl); |
386b8a85 JM |
1565 | |
1566 | if (is_friend && !have_def) | |
36a117a5 MM |
1567 | /* This is not really a declaration of a specialization. |
1568 | It's just the name of an instantiation. But, it's not | |
1569 | a request for an instantiation, either. */ | |
fbf1c34b | 1570 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
75650646 | 1571 | |
36a117a5 MM |
1572 | /* Register this specialization so that we can find it |
1573 | again. */ | |
1574 | decl = register_specialization (decl, gen_tmpl, targs); | |
386b8a85 JM |
1575 | } |
1576 | } | |
75650646 | 1577 | |
e1467ff2 | 1578 | return decl; |
386b8a85 | 1579 | } |
75650646 | 1580 | |
39c01e4c MM |
1581 | /* TYPE is being declared. Verify that the use of template headers |
1582 | and such is reasonable. Issue error messages if not. */ | |
1583 | ||
1584 | void | |
1585 | maybe_check_template_type (type) | |
1586 | tree type; | |
1587 | { | |
1588 | if (template_header_count) | |
1589 | { | |
1590 | /* We are in the scope of some `template <...>' header. */ | |
1591 | ||
1592 | int context_depth | |
1593 | = template_class_depth_real (TYPE_CONTEXT (type), | |
1594 | /*count_specializations=*/1); | |
1595 | ||
1596 | if (template_header_count <= context_depth) | |
1597 | /* This is OK; the template headers are for the context. We | |
1598 | are actually too lenient here; like | |
1599 | check_explicit_specialization we should consider the number | |
1600 | of template types included in the actual declaration. For | |
1601 | example, | |
1602 | ||
1603 | template <class T> struct S { | |
1604 | template <class U> template <class V> | |
1605 | struct I {}; | |
1606 | }; | |
1607 | ||
1608 | is illegal, but: | |
1609 | ||
1610 | template <class T> struct S { | |
1611 | template <class U> struct I; | |
1612 | }; | |
1613 | ||
1614 | template <class T> template <class U. | |
1615 | struct S<T>::I {}; | |
1616 | ||
1617 | is not. */ | |
1618 | ; | |
1619 | else if (template_header_count > context_depth + 1) | |
1620 | /* There are two many template parameter lists. */ | |
8251199e | 1621 | cp_error ("too many template parameter lists in declaration of `%T'", type); |
39c01e4c MM |
1622 | } |
1623 | } | |
1624 | ||
75650646 MM |
1625 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
1626 | parameters. These are represented in the same format used for | |
1627 | DECL_TEMPLATE_PARMS. */ | |
1628 | ||
1629 | int comp_template_parms (parms1, parms2) | |
1630 | tree parms1; | |
1631 | tree parms2; | |
1632 | { | |
1633 | tree p1; | |
1634 | tree p2; | |
1635 | ||
1636 | if (parms1 == parms2) | |
1637 | return 1; | |
1638 | ||
1639 | for (p1 = parms1, p2 = parms2; | |
1640 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1641 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1642 | { | |
1643 | tree t1 = TREE_VALUE (p1); | |
1644 | tree t2 = TREE_VALUE (p2); | |
1645 | int i; | |
1646 | ||
1647 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1648 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1649 | ||
1650 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1651 | return 0; | |
1652 | ||
1653 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1654 | { | |
1655 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1656 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1657 | ||
1658 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1659 | return 0; | |
1660 | ||
1661 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1662 | continue; | |
3bfdc719 | 1663 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
1664 | return 0; |
1665 | } | |
1666 | } | |
1667 | ||
1668 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1669 | /* One set of parameters has more parameters lists than the | |
1670 | other. */ | |
1671 | return 0; | |
1672 | ||
1673 | return 1; | |
1674 | } | |
1675 | ||
f3400fe2 JM |
1676 | /* Complain if DECL shadows a template parameter. |
1677 | ||
1678 | [temp.local]: A template-parameter shall not be redeclared within its | |
1679 | scope (including nested scopes). */ | |
1680 | ||
1681 | void | |
1682 | check_template_shadow (decl) | |
1683 | tree decl; | |
1684 | { | |
8f032717 MM |
1685 | tree olddecl; |
1686 | ||
b5d9b9ab MM |
1687 | /* If we're not in a template, we can't possibly shadow a template |
1688 | parameter. */ | |
1689 | if (!current_template_parms) | |
1690 | return; | |
1691 | ||
1692 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
1693 | if (TREE_CODE (decl) == OVERLOAD) |
1694 | decl = OVL_CURRENT (decl); | |
1695 | olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); | |
f3400fe2 | 1696 | |
b5d9b9ab MM |
1697 | /* If there's no previous binding for this name, we're not shadowing |
1698 | anything, let alone a template parameter. */ | |
1699 | if (!olddecl) | |
1700 | return; | |
1701 | ||
1702 | /* If we're not shadowing a template parameter, we're done. Note | |
1703 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
1704 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
1705 | node. */ | |
1706 | if (TREE_CODE_CLASS (TREE_CODE (olddecl)) != 'd' | |
1707 | || !DECL_TEMPLATE_PARM_P (olddecl)) | |
1708 | return; | |
1709 | ||
1710 | /* We check for decl != olddecl to avoid bogus errors for using a | |
1711 | name inside a class. We check TPFI to avoid duplicate errors for | |
1712 | inline member templates. */ | |
1713 | if (decl == olddecl | |
1714 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
1715 | return; | |
1716 | ||
1717 | cp_error_at ("declaration of `%#D'", decl); | |
1718 | cp_error_at (" shadows template parm `%#D'", olddecl); | |
f3400fe2 | 1719 | } |
22a7be53 | 1720 | |
f3400fe2 | 1721 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
1722 | ORIG_LEVEL, DECL, and TYPE. */ |
1723 | ||
1724 | static tree | |
1725 | build_template_parm_index (index, level, orig_level, decl, type) | |
1726 | int index; | |
1727 | int level; | |
1728 | int orig_level; | |
1729 | tree decl; | |
1730 | tree type; | |
1731 | { | |
1732 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1733 | TEMPLATE_PARM_IDX (t) = index; | |
1734 | TEMPLATE_PARM_LEVEL (t) = level; | |
1735 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1736 | TEMPLATE_PARM_DECL (t) = decl; | |
1737 | TREE_TYPE (t) = type; | |
1738 | ||
1739 | return t; | |
1740 | } | |
1741 | ||
f84b4be9 | 1742 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 1743 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
1744 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
1745 | new one is created. */ | |
1746 | ||
1747 | static tree | |
93cdc044 | 1748 | reduce_template_parm_level (index, type, levels) |
f84b4be9 JM |
1749 | tree index; |
1750 | tree type; | |
93cdc044 | 1751 | int levels; |
f84b4be9 JM |
1752 | { |
1753 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1754 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 1755 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 JM |
1756 | { |
1757 | tree decl | |
1758 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
1759 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
1760 | type); | |
1761 | tree t | |
1762 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 1763 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
1764 | TEMPLATE_PARM_ORIG_LEVEL (index), |
1765 | decl, type); | |
1766 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
1767 | |
1768 | /* Template template parameters need this. */ | |
1769 | DECL_TEMPLATE_PARMS (decl) | |
1770 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
1771 | } |
1772 | ||
1773 | return TEMPLATE_PARM_DESCENDANTS (index); | |
1774 | } | |
1775 | ||
8d08fdba | 1776 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 1777 | LIST being built. */ |
e92cc029 | 1778 | |
8d08fdba MS |
1779 | tree |
1780 | process_template_parm (list, next) | |
1781 | tree list, next; | |
1782 | { | |
1783 | tree parm; | |
1784 | tree decl = 0; | |
a292b002 | 1785 | tree defval; |
5566b478 | 1786 | int is_type, idx; |
f84b4be9 | 1787 | |
8d08fdba MS |
1788 | parm = next; |
1789 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
1790 | defval = TREE_PURPOSE (parm); |
1791 | parm = TREE_VALUE (parm); | |
1792 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
1793 | |
1794 | if (list) | |
1795 | { | |
1796 | tree p = TREE_VALUE (tree_last (list)); | |
1797 | ||
1798 | if (TREE_CODE (p) == TYPE_DECL) | |
1799 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
73b0fce8 KL |
1800 | else if (TREE_CODE (p) == TEMPLATE_DECL) |
1801 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p))); | |
5566b478 | 1802 | else |
f84b4be9 | 1803 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
1804 | ++idx; |
1805 | } | |
1806 | else | |
1807 | idx = 0; | |
1808 | ||
8d08fdba MS |
1809 | if (!is_type) |
1810 | { | |
a292b002 | 1811 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 1812 | /* is a const-param */ |
a292b002 | 1813 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 1814 | PARM, 0, NULL_TREE); |
d490621d MM |
1815 | |
1816 | /* [temp.param] | |
1817 | ||
1818 | The top-level cv-qualifiers on the template-parameter are | |
1819 | ignored when determining its type. */ | |
1820 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
1821 | ||
8d08fdba MS |
1822 | /* A template parameter is not modifiable. */ |
1823 | TREE_READONLY (parm) = 1; | |
c91a56d2 | 1824 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
410c2fba KL |
1825 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM |
1826 | && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE) | |
8d08fdba | 1827 | { |
8251199e | 1828 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
c91a56d2 MS |
1829 | TREE_TYPE (parm)); |
1830 | if (DECL_NAME (parm) == NULL_TREE) | |
8251199e | 1831 | error (" a template type parameter must begin with `class' or `typename'"); |
8d08fdba MS |
1832 | TREE_TYPE (parm) = void_type_node; |
1833 | } | |
37c46b43 MS |
1834 | else if (pedantic |
1835 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
1836 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
8251199e | 1837 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
eb66be0e | 1838 | TREE_TYPE (parm)); |
8d08fdba MS |
1839 | if (TREE_PERMANENT (parm) == 0) |
1840 | { | |
1841 | parm = copy_node (parm); | |
1842 | TREE_PERMANENT (parm) = 1; | |
1843 | } | |
8d08fdba | 1844 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
f84b4be9 JM |
1845 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
1846 | = build_template_parm_index (idx, processing_template_decl, | |
1847 | processing_template_decl, | |
1848 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
1849 | } |
1850 | else | |
1851 | { | |
73b0fce8 KL |
1852 | tree t; |
1853 | parm = TREE_VALUE (parm); | |
1854 | ||
1855 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
1856 | { | |
1857 | t = make_lang_type (TEMPLATE_TEMPLATE_PARM); | |
1858 | /* This is for distinguishing between real templates and template | |
1859 | template parameters */ | |
1860 | TREE_TYPE (parm) = t; | |
1861 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
1862 | decl = parm; | |
1863 | } | |
1864 | else | |
1865 | { | |
1866 | t = make_lang_type (TEMPLATE_TYPE_PARM); | |
1867 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ | |
1868 | decl = build_decl (TYPE_DECL, parm, t); | |
1869 | } | |
1870 | ||
d2e5ee5c MS |
1871 | TYPE_NAME (t) = decl; |
1872 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 1873 | parm = decl; |
f84b4be9 JM |
1874 | TEMPLATE_TYPE_PARM_INDEX (t) |
1875 | = build_template_parm_index (idx, processing_template_decl, | |
1876 | processing_template_decl, | |
1877 | decl, TREE_TYPE (parm)); | |
8d08fdba | 1878 | } |
8ccc31eb | 1879 | SET_DECL_ARTIFICIAL (decl); |
fc03edb3 | 1880 | DECL_TEMPLATE_PARM_P (decl) = 1; |
8d08fdba | 1881 | pushdecl (decl); |
a292b002 | 1882 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
1883 | return chainon (list, parm); |
1884 | } | |
1885 | ||
1886 | /* The end of a template parameter list has been reached. Process the | |
1887 | tree list into a parameter vector, converting each parameter into a more | |
1888 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
1889 | as PARM_DECLs. */ | |
1890 | ||
1891 | tree | |
1892 | end_template_parm_list (parms) | |
1893 | tree parms; | |
1894 | { | |
5566b478 | 1895 | int nparms; |
8d08fdba | 1896 | tree parm; |
5566b478 MS |
1897 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
1898 | ||
5566b478 MS |
1899 | current_template_parms |
1900 | = tree_cons (build_int_2 (0, processing_template_decl), | |
1901 | saved_parmlist, current_template_parms); | |
8d08fdba | 1902 | |
91e490ab MM |
1903 | for (parm = parms, nparms = 0; |
1904 | parm; | |
1905 | parm = TREE_CHAIN (parm), nparms++) | |
66191c20 | 1906 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 1907 | |
67ffc812 MM |
1908 | --processing_template_parmlist; |
1909 | ||
8d08fdba MS |
1910 | return saved_parmlist; |
1911 | } | |
1912 | ||
5566b478 MS |
1913 | /* end_template_decl is called after a template declaration is seen. */ |
1914 | ||
8d08fdba | 1915 | void |
5566b478 | 1916 | end_template_decl () |
8d08fdba | 1917 | { |
386b8a85 JM |
1918 | reset_specialization (); |
1919 | ||
5156628f | 1920 | if (! processing_template_decl) |
73aad9b9 JM |
1921 | return; |
1922 | ||
5566b478 MS |
1923 | /* This matches the pushlevel in begin_template_parm_list. */ |
1924 | poplevel (0, 0, 0); | |
8d08fdba | 1925 | |
5566b478 MS |
1926 | --processing_template_decl; |
1927 | current_template_parms = TREE_CHAIN (current_template_parms); | |
1928 | (void) get_pending_sizes (); /* Why? */ | |
1929 | } | |
8d08fdba | 1930 | |
36a117a5 MM |
1931 | /* Given a template argument vector containing the template PARMS. |
1932 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
1933 | |
1934 | tree | |
1935 | current_template_args () | |
5566b478 | 1936 | { |
36a117a5 | 1937 | tree header; |
b370501f | 1938 | tree args = NULL_TREE; |
36a117a5 | 1939 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
1940 | int l = length; |
1941 | ||
36a117a5 MM |
1942 | /* If there is only one level of template parameters, we do not |
1943 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
1944 | TREE_VEC containing the arguments. */ | |
1945 | if (length > 1) | |
1946 | args = make_tree_vec (length); | |
1947 | ||
1948 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 1949 | { |
5566b478 | 1950 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
1951 | int i; |
1952 | ||
5566b478 | 1953 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 1954 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 1955 | { |
98c1c668 JM |
1956 | tree t = TREE_VEC_ELT (a, i); |
1957 | ||
36a117a5 | 1958 | /* T will be a list if we are called from within a |
98c1c668 JM |
1959 | begin/end_template_parm_list pair, but a vector directly |
1960 | if within a begin/end_member_template_processing pair. */ | |
1961 | if (TREE_CODE (t) == TREE_LIST) | |
1962 | { | |
1963 | t = TREE_VALUE (t); | |
1964 | ||
73b0fce8 KL |
1965 | if (TREE_CODE (t) == TYPE_DECL |
1966 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
1967 | t = TREE_TYPE (t); |
1968 | else | |
1969 | t = DECL_INITIAL (t); | |
36a117a5 | 1970 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 1971 | } |
5566b478 | 1972 | } |
36a117a5 MM |
1973 | |
1974 | if (length > 1) | |
1975 | TREE_VEC_ELT (args, --l) = a; | |
1976 | else | |
1977 | args = a; | |
8d08fdba MS |
1978 | } |
1979 | ||
9a3b49ac MS |
1980 | return args; |
1981 | } | |
75650646 | 1982 | |
e1467ff2 MM |
1983 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
1984 | template PARMS. Used by push_template_decl below. */ | |
1985 | ||
75650646 MM |
1986 | static tree |
1987 | build_template_decl (decl, parms) | |
1988 | tree decl; | |
1989 | tree parms; | |
1990 | { | |
1991 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
1992 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
1993 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
1994 | if (DECL_LANG_SPECIFIC (decl)) | |
1995 | { | |
1996 | DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); | |
8f032717 MM |
1997 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
1998 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
11f98788 | 1999 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
75650646 MM |
2000 | } |
2001 | ||
2002 | return tmpl; | |
2003 | } | |
2004 | ||
050367a3 MM |
2005 | struct template_parm_data |
2006 | { | |
6c30752f MM |
2007 | /* The level of the template parameters we are currently |
2008 | processing. */ | |
050367a3 | 2009 | int level; |
6c30752f MM |
2010 | |
2011 | /* The index of the specialization argument we are currently | |
2012 | processing. */ | |
2013 | int current_arg; | |
2014 | ||
2015 | /* An array whose size is the number of template parameters. The | |
2016 | elements are non-zero if the parameter has been used in any one | |
2017 | of the arguments processed so far. */ | |
050367a3 | 2018 | int* parms; |
6c30752f MM |
2019 | |
2020 | /* An array whose size is the number of template arguments. The | |
2021 | elements are non-zero if the argument makes use of template | |
2022 | parameters of this level. */ | |
2023 | int* arg_uses_template_parms; | |
050367a3 MM |
2024 | }; |
2025 | ||
2026 | /* Subroutine of push_template_decl used to see if each template | |
2027 | parameter in a partial specialization is used in the explicit | |
2028 | argument list. If T is of the LEVEL given in DATA (which is | |
2029 | treated as a template_parm_data*), then DATA->PARMS is marked | |
2030 | appropriately. */ | |
2031 | ||
2032 | static int | |
2033 | mark_template_parm (t, data) | |
2034 | tree t; | |
2035 | void* data; | |
2036 | { | |
2037 | int level; | |
2038 | int idx; | |
2039 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
2040 | ||
2041 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2042 | { | |
2043 | level = TEMPLATE_PARM_LEVEL (t); | |
2044 | idx = TEMPLATE_PARM_IDX (t); | |
2045 | } | |
2046 | else | |
2047 | { | |
2048 | level = TEMPLATE_TYPE_LEVEL (t); | |
2049 | idx = TEMPLATE_TYPE_IDX (t); | |
2050 | } | |
2051 | ||
2052 | if (level == tpd->level) | |
6c30752f MM |
2053 | { |
2054 | tpd->parms[idx] = 1; | |
2055 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
2056 | } | |
050367a3 MM |
2057 | |
2058 | /* Return zero so that for_each_template_parm will continue the | |
2059 | traversal of the tree; we want to mark *every* template parm. */ | |
2060 | return 0; | |
2061 | } | |
2062 | ||
6c30752f MM |
2063 | /* Process the partial specialization DECL. */ |
2064 | ||
e9659ab0 | 2065 | static tree |
6c30752f MM |
2066 | process_partial_specialization (decl) |
2067 | tree decl; | |
2068 | { | |
2069 | tree type = TREE_TYPE (decl); | |
2070 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
2071 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
2072 | tree inner_args = innermost_args (specargs); | |
2073 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
2074 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
2075 | int nargs = TREE_VEC_LENGTH (inner_args); | |
2076 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2077 | int i; | |
2078 | int did_error_intro = 0; | |
6c30752f MM |
2079 | struct template_parm_data tpd; |
2080 | struct template_parm_data tpd2; | |
2081 | ||
6c30752f MM |
2082 | /* We check that each of the template parameters given in the |
2083 | partial specialization is used in the argument list to the | |
2084 | specialization. For example: | |
2085 | ||
2086 | template <class T> struct S; | |
2087 | template <class T> struct S<T*>; | |
2088 | ||
2089 | The second declaration is OK because `T*' uses the template | |
2090 | parameter T, whereas | |
2091 | ||
2092 | template <class T> struct S<int>; | |
2093 | ||
2094 | is no good. Even trickier is: | |
2095 | ||
2096 | template <class T> | |
2097 | struct S1 | |
2098 | { | |
2099 | template <class U> | |
2100 | struct S2; | |
2101 | template <class U> | |
2102 | struct S2<T>; | |
2103 | }; | |
2104 | ||
2105 | The S2<T> declaration is actually illegal; it is a | |
2106 | full-specialization. Of course, | |
2107 | ||
2108 | template <class U> | |
2109 | struct S2<T (*)(U)>; | |
2110 | ||
2111 | or some such would have been OK. */ | |
2112 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2113 | tpd.parms = alloca (sizeof (int) * ntparms); | |
1ddb2906 | 2114 | bzero ((PTR) tpd.parms, sizeof (int) * ntparms); |
6c30752f MM |
2115 | |
2116 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
1ddb2906 | 2117 | bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs); |
6c30752f MM |
2118 | for (i = 0; i < nargs; ++i) |
2119 | { | |
2120 | tpd.current_arg = i; | |
2121 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2122 | &mark_template_parm, | |
2123 | &tpd); | |
2124 | } | |
2125 | for (i = 0; i < ntparms; ++i) | |
2126 | if (tpd.parms[i] == 0) | |
2127 | { | |
2128 | /* One of the template parms was not used in the | |
2129 | specialization. */ | |
2130 | if (!did_error_intro) | |
2131 | { | |
8251199e | 2132 | cp_error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2133 | did_error_intro = 1; |
2134 | } | |
2135 | ||
8251199e | 2136 | cp_error (" `%D'", |
6c30752f MM |
2137 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
2138 | } | |
2139 | ||
2140 | /* [temp.class.spec] | |
2141 | ||
2142 | The argument list of the specialization shall not be identical to | |
2143 | the implicit argument list of the primary template. */ | |
2144 | if (comp_template_args (inner_args, | |
2145 | innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2146 | (maintmpl))))) | |
8251199e | 2147 | cp_error ("partial specialization `%T' does not specialize any template arguments", type); |
6c30752f MM |
2148 | |
2149 | /* [temp.class.spec] | |
2150 | ||
2151 | A partially specialized non-type argument expression shall not | |
2152 | involve template parameters of the partial specialization except | |
2153 | when the argument expression is a simple identifier. | |
2154 | ||
2155 | The type of a template parameter corresponding to a specialized | |
2156 | non-type argument shall not be dependent on a parameter of the | |
2157 | specialization. */ | |
2158 | my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); | |
2159 | tpd2.parms = 0; | |
2160 | for (i = 0; i < nargs; ++i) | |
2161 | { | |
2162 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2163 | if (/* These first two lines are the `non-type' bit. */ | |
2164 | TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
2165 | && TREE_CODE (arg) != TEMPLATE_DECL | |
2166 | /* This next line is the `argument expression is not just a | |
2167 | simple identifier' condition and also the `specialized | |
2168 | non-type argument' bit. */ | |
2169 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2170 | { | |
2171 | if (tpd.arg_uses_template_parms[i]) | |
8251199e | 2172 | cp_error ("template argument `%E' involves template parameter(s)", arg); |
6c30752f MM |
2173 | else |
2174 | { | |
2175 | /* Look at the corresponding template parameter, | |
2176 | marking which template parameters its type depends | |
2177 | upon. */ | |
2178 | tree type = | |
2179 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2180 | i))); | |
2181 | ||
2182 | if (!tpd2.parms) | |
2183 | { | |
2184 | /* We haven't yet initialized TPD2. Do so now. */ | |
2185 | tpd2.arg_uses_template_parms | |
2186 | = (int*) alloca (sizeof (int) * nargs); | |
104bf76a | 2187 | /* The number of parameters here is the number in the |
76a83782 RH |
2188 | main template, which, as checked in the assertion |
2189 | above, is NARGS. */ | |
6c30752f MM |
2190 | tpd2.parms = (int*) alloca (sizeof (int) * nargs); |
2191 | tpd2.level = | |
2192 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2193 | } | |
2194 | ||
104bf76a | 2195 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2196 | looking for the template parameters of the main |
2197 | template, not in the specialization. */ | |
2198 | tpd2.current_arg = i; | |
2199 | tpd2.arg_uses_template_parms[i] = 0; | |
1ddb2906 | 2200 | bzero ((PTR) tpd2.parms, sizeof (int) * nargs); |
6c30752f MM |
2201 | for_each_template_parm (type, |
2202 | &mark_template_parm, | |
2203 | &tpd2); | |
2204 | ||
2205 | if (tpd2.arg_uses_template_parms [i]) | |
2206 | { | |
2207 | /* The type depended on some template parameters. | |
2208 | If they are fully specialized in the | |
2209 | specialization, that's OK. */ | |
2210 | int j; | |
2211 | for (j = 0; j < nargs; ++j) | |
2212 | if (tpd2.parms[j] != 0 | |
2213 | && tpd.arg_uses_template_parms [j]) | |
2214 | { | |
104bf76a | 2215 | cp_error ("type `%T' of template argument `%E' depends on template parameter(s)", |
6c30752f MM |
2216 | type, |
2217 | arg); | |
2218 | break; | |
2219 | } | |
2220 | } | |
2221 | } | |
2222 | } | |
2223 | } | |
2224 | ||
2225 | if (retrieve_specialization (maintmpl, specargs)) | |
2226 | /* We've already got this specialization. */ | |
2227 | return decl; | |
2228 | ||
2229 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) | |
2230 | = perm_tree_cons (inner_args, inner_parms, | |
2231 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
2232 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; | |
2233 | return decl; | |
2234 | } | |
2235 | ||
6ba89f8e MM |
2236 | /* Check that a template declaration's use of default arguments is not |
2237 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
2238 | non-zero if DECL is the thing declared by a primary template. | |
2239 | IS_PARTIAL is non-zero if DECL is a partial specialization. */ | |
2240 | ||
2241 | static void | |
2242 | check_default_tmpl_args (decl, parms, is_primary, is_partial) | |
2243 | tree decl; | |
2244 | tree parms; | |
2245 | int is_primary; | |
2246 | int is_partial; | |
2247 | { | |
d8e178a0 | 2248 | const char *msg; |
66191c20 MM |
2249 | int last_level_to_check; |
2250 | tree parm_level; | |
6ba89f8e MM |
2251 | |
2252 | /* [temp.param] | |
2253 | ||
2254 | A default template-argument shall not be specified in a | |
2255 | function template declaration or a function template definition, nor | |
2256 | in the template-parameter-list of the definition of a member of a | |
2257 | class template. */ | |
2258 | ||
e0942dcd MM |
2259 | if (TREE_CODE (DECL_REAL_CONTEXT (decl)) == FUNCTION_DECL) |
2260 | /* You can't have a function template declaration in a local | |
2261 | scope, nor you can you define a member of a class template in a | |
2262 | local scope. */ | |
2263 | return; | |
2264 | ||
6ba89f8e MM |
2265 | if (current_class_type |
2266 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 2267 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
2268 | /* If this is either a friend defined in the scope of the class |
2269 | or a member function. */ | |
2270 | && DECL_CLASS_CONTEXT (decl) == current_class_type | |
2271 | /* And, if it was a member function, it really was defined in | |
2272 | the scope of the class. */ | |
2273 | && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_DEFINED_IN_CLASS_P (decl))) | |
6ba89f8e | 2274 | /* We already checked these parameters when the template was |
5937a6f9 MM |
2275 | declared, so there's no need to do it again now. This function |
2276 | was defined in class scope, but we're processing it's body now | |
2277 | that the class is complete. */ | |
6ba89f8e MM |
2278 | return; |
2279 | ||
66191c20 MM |
2280 | /* [temp.param] |
2281 | ||
2282 | If a template-parameter has a default template-argument, all | |
2283 | subsequent template-parameters shall have a default | |
2284 | template-argument supplied. */ | |
2285 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
2286 | { | |
2287 | tree inner_parms = TREE_VALUE (parm_level); | |
2288 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2289 | int seen_def_arg_p = 0; | |
2290 | int i; | |
2291 | ||
2292 | for (i = 0; i < ntparms; ++i) | |
2293 | { | |
2294 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
2295 | if (TREE_PURPOSE (parm)) | |
2296 | seen_def_arg_p = 1; | |
2297 | else if (seen_def_arg_p) | |
2298 | { | |
2299 | cp_error ("no default argument for `%D'", TREE_VALUE (parm)); | |
2300 | /* For better subsequent error-recovery, we indicate that | |
2301 | there should have been a default argument. */ | |
2302 | TREE_PURPOSE (parm) = error_mark_node; | |
2303 | } | |
2304 | } | |
2305 | } | |
2306 | ||
6ba89f8e MM |
2307 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) |
2308 | /* For an ordinary class template, default template arguments are | |
2309 | allowed at the innermost level, e.g.: | |
2310 | template <class T = int> | |
2311 | struct S {}; | |
2312 | but, in a partial specialization, they're not allowed even | |
2313 | there, as we have in [temp.class.spec]: | |
2314 | ||
2315 | The template parameter list of a specialization shall not | |
2316 | contain default template argument values. | |
2317 | ||
2318 | So, for a partial specialization, or for a function template, | |
2319 | we look at all of them. */ | |
2320 | ; | |
2321 | else | |
2322 | /* But, for a primary class template that is not a partial | |
2323 | specialization we look at all template parameters except the | |
2324 | innermost ones. */ | |
2325 | parms = TREE_CHAIN (parms); | |
2326 | ||
2327 | /* Figure out what error message to issue. */ | |
2328 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2329 | msg = "default argument for template parameter in function template `%D'"; | |
2330 | else if (is_partial) | |
2331 | msg = "default argument in partial specialization `%D'"; | |
2332 | else | |
2333 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2334 | ||
2335 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2336 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2337 | examine the parameters to the class itself. On the one |
6ba89f8e MM |
2338 | hand, they will be checked when the class is defined, and, |
2339 | on the other, default arguments are legal in things like: | |
2340 | template <class T = double> | |
2341 | struct S { template <class U> void f(U); }; | |
2342 | Here the default argument for `S' has no bearing on the | |
2343 | declaration of `f'. */ | |
2344 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2345 | else | |
2346 | /* Check everything. */ | |
2347 | last_level_to_check = 0; | |
2348 | ||
66191c20 MM |
2349 | for (parm_level = parms; |
2350 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
2351 | parm_level = TREE_CHAIN (parm_level)) | |
6ba89f8e | 2352 | { |
66191c20 MM |
2353 | tree inner_parms = TREE_VALUE (parm_level); |
2354 | int i; | |
2355 | int ntparms; | |
6ba89f8e MM |
2356 | |
2357 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2358 | for (i = 0; i < ntparms; ++i) | |
2359 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2360 | { | |
2361 | if (msg) | |
2362 | { | |
2363 | cp_error (msg, decl); | |
2364 | msg = 0; | |
2365 | } | |
2366 | ||
2367 | /* Clear out the default argument so that we are not | |
2368 | confused later. */ | |
2369 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2370 | } | |
2371 | ||
2372 | /* At this point, if we're still interested in issuing messages, | |
2373 | they must apply to classes surrounding the object declared. */ | |
2374 | if (msg) | |
2375 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2376 | } | |
2377 | } | |
2378 | ||
3ac3d9ea | 2379 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2380 | parameters given by current_template_args, or reuses a |
2381 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2382 | equivalent one, if it is replaced via a call to duplicate_decls. |
2383 | ||
2384 | If IS_FRIEND is non-zero, DECL is a friend declaration. */ | |
3ac3d9ea MM |
2385 | |
2386 | tree | |
6757edfe | 2387 | push_template_decl_real (decl, is_friend) |
9a3b49ac | 2388 | tree decl; |
6757edfe | 2389 | int is_friend; |
9a3b49ac MS |
2390 | { |
2391 | tree tmpl; | |
f84b4be9 | 2392 | tree args; |
9a3b49ac | 2393 | tree info; |
f84b4be9 JM |
2394 | tree ctx; |
2395 | int primary; | |
6ba89f8e | 2396 | int is_partial; |
cfe507be | 2397 | int new_template_p = 0; |
6ba89f8e MM |
2398 | |
2399 | /* See if this is a partial specialization. */ | |
9188c363 | 2400 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e MM |
2401 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
2402 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); | |
6757edfe MM |
2403 | |
2404 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2405 | |
2406 | if (is_friend) | |
2407 | /* For a friend, we want the context of the friend function, not | |
2408 | the type of which it is a friend. */ | |
2409 | ctx = DECL_CONTEXT (decl); | |
2c73f9f5 ML |
2410 | else if (DECL_REAL_CONTEXT (decl) |
2411 | && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2412 | /* In the case of a virtual function, we want the class in which |
2413 | it is defined. */ | |
2414 | ctx = DECL_REAL_CONTEXT (decl); | |
2415 | else | |
2416 | /* Otherwise, if we're currently definining some class, the DECL | |
2417 | is assumed to be a member of the class. */ | |
9188c363 | 2418 | ctx = current_scope (); |
f84b4be9 | 2419 | |
2c73f9f5 ML |
2420 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2421 | ctx = NULL_TREE; | |
2422 | ||
2423 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2424 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2425 | |
6ba89f8e | 2426 | /* See if this is a primary template. */ |
9188c363 | 2427 | primary = pseudo_global_level_p (); |
9a3b49ac | 2428 | |
83566abf JM |
2429 | if (primary) |
2430 | { | |
2431 | if (current_lang_name == lang_name_c) | |
8251199e | 2432 | cp_error ("template with C linkage"); |
4ce3d537 MM |
2433 | else if (TREE_CODE (decl) == TYPE_DECL |
2434 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
8251199e | 2435 | cp_error ("template class without a name"); |
2aaf816d JM |
2436 | else if ((DECL_IMPLICIT_TYPEDEF_P (decl) |
2437 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2438 | || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)) | |
2439 | || TREE_CODE (decl) == FUNCTION_DECL) | |
2440 | /* OK */; | |
2441 | else | |
4ce3d537 | 2442 | cp_error ("template declaration of `%#D'", decl); |
83566abf JM |
2443 | } |
2444 | ||
6ba89f8e MM |
2445 | /* Check to see that the rules regarding the use of default |
2446 | arguments are not being violated. */ | |
2447 | check_default_tmpl_args (decl, current_template_parms, | |
2448 | primary, is_partial); | |
73aad9b9 | 2449 | |
6ba89f8e MM |
2450 | if (is_partial) |
2451 | return process_partial_specialization (decl); | |
d32789d8 | 2452 | |
9a3b49ac MS |
2453 | args = current_template_args (); |
2454 | ||
f84b4be9 JM |
2455 | if (!ctx |
2456 | || TREE_CODE (ctx) == FUNCTION_DECL | |
2457 | || TYPE_BEING_DEFINED (ctx) | |
2458 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
8d08fdba | 2459 | { |
75650646 | 2460 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
2461 | && DECL_TEMPLATE_INFO (decl) |
2462 | && DECL_TI_TEMPLATE (decl)) | |
2463 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
2464 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
2465 | be DECL_LANG_SPECIFIC. The information equivalent to | |
2466 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
2467 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) | |
2468 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2469 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2470 | { | |
2471 | /* Since a template declaration already existed for this | |
2472 | class-type, we must be redeclaring it here. Make sure | |
2473 | that the redeclaration is legal. */ | |
2474 | redeclare_class_template (TREE_TYPE (decl), | |
2475 | current_template_parms); | |
2476 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
2477 | one we already had. */ | |
2478 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
2479 | } | |
f84b4be9 | 2480 | else |
786b5245 | 2481 | { |
f84b4be9 | 2482 | tmpl = build_template_decl (decl, current_template_parms); |
cfe507be MM |
2483 | new_template_p = 1; |
2484 | ||
f84b4be9 JM |
2485 | if (DECL_LANG_SPECIFIC (decl) |
2486 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2487 | { | |
2488 | /* A specialization of a member template of a template | |
2489 | class. */ | |
2490 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
2491 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
2492 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
2493 | } | |
786b5245 | 2494 | } |
8d08fdba MS |
2495 | } |
2496 | else | |
2497 | { | |
e1a5ccf7 | 2498 | tree a, t, current, parms; |
ba4f4e5d | 2499 | int i; |
6633d636 | 2500 | |
73aad9b9 | 2501 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
8251199e | 2502 | cp_error ("must specialize `%#T' before defining member `%#D'", |
73aad9b9 | 2503 | ctx, decl); |
6b9ab5cc MM |
2504 | if (TREE_CODE (decl) == TYPE_DECL) |
2505 | { | |
ed44da02 MM |
2506 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
2507 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
2508 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2509 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2510 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
2511 | else |
2512 | { | |
8251199e | 2513 | cp_error ("`%D' does not declare a template type", decl); |
6b9ab5cc MM |
2514 | return decl; |
2515 | } | |
2516 | } | |
fc378698 | 2517 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 2518 | { |
8251199e | 2519 | cp_error ("template definition of non-template `%#D'", decl); |
3ac3d9ea | 2520 | return decl; |
c91a56d2 | 2521 | } |
8d08fdba | 2522 | else |
5566b478 | 2523 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 2524 | |
e1a5ccf7 JM |
2525 | if (is_member_template (tmpl) |
2526 | && DECL_FUNCTION_TEMPLATE_P (tmpl) | |
2527 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) | |
2528 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
98c1c668 | 2529 | { |
e1a5ccf7 JM |
2530 | tree new_tmpl; |
2531 | ||
2532 | /* The declaration is a specialization of a member | |
2533 | template, declared outside the class. Therefore, the | |
2534 | innermost template arguments will be NULL, so we | |
2535 | replace them with the arguments determined by the | |
2536 | earlier call to check_explicit_specialization. */ | |
2537 | args = DECL_TI_ARGS (decl); | |
2538 | ||
2539 | new_tmpl | |
2540 | = build_template_decl (decl, current_template_parms); | |
2541 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
2542 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
2543 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
2544 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
2545 | DECL_TEMPLATE_INFO (new_tmpl) = | |
2546 | perm_tree_cons (tmpl, args, NULL_TREE); | |
2547 | ||
2548 | register_specialization (new_tmpl, tmpl, args); | |
2549 | return decl; | |
98c1c668 | 2550 | } |
98c1c668 | 2551 | |
e1a5ccf7 | 2552 | /* Make sure the template headers we got make sense. */ |
6633d636 | 2553 | |
e1a5ccf7 JM |
2554 | parms = DECL_TEMPLATE_PARMS (tmpl); |
2555 | i = TMPL_PARMS_DEPTH (parms); | |
2556 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 2557 | { |
e1a5ccf7 JM |
2558 | cp_error ("expected %d levels of template parms for `%#D', got %d", |
2559 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 2560 | } |
e1a5ccf7 JM |
2561 | else |
2562 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
2563 | { | |
2564 | a = TMPL_ARGS_LEVEL (args, i); | |
2565 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
2566 | ||
2567 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
2568 | { | |
2569 | if (current == decl) | |
2570 | cp_error ("got %d template parameters for `%#D'", | |
2571 | TREE_VEC_LENGTH (a), decl); | |
2572 | else | |
2573 | cp_error ("got %d template parameters for `%#T'", | |
2574 | TREE_VEC_LENGTH (a), current); | |
2575 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
2576 | } | |
98c1c668 | 2577 | |
e1a5ccf7 JM |
2578 | /* Perhaps we should also check that the parms are used in the |
2579 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 2580 | |
e1a5ccf7 JM |
2581 | if (current == decl) |
2582 | current = ctx; | |
2583 | else | |
2584 | current = TYPE_CONTEXT (current); | |
2585 | } | |
5566b478 | 2586 | } |
8d08fdba | 2587 | |
5566b478 MS |
2588 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
2589 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 2590 | |
36a117a5 MM |
2591 | /* Push template declarations for global functions and types. Note |
2592 | that we do not try to push a global template friend declared in a | |
2593 | template class; such a thing may well depend on the template | |
39c01e4c | 2594 | parameters of the class. */ |
cfe507be | 2595 | if (new_template_p && !ctx |
36a117a5 | 2596 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
2c73f9f5 | 2597 | tmpl = pushdecl_namespace_level (tmpl); |
8d08fdba | 2598 | |
5566b478 | 2599 | if (primary) |
6757edfe | 2600 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
5566b478 | 2601 | |
3c5c0849 | 2602 | info = perm_tree_cons (tmpl, copy_to_permanent (args), NULL_TREE); |
5566b478 | 2603 | |
9188c363 | 2604 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
8d08fdba | 2605 | { |
ed44da02 MM |
2606 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2607 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
2608 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE) | |
75650646 | 2609 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 2610 | } |
2aaf816d | 2611 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 2612 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
2613 | |
2614 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
2615 | } |
2616 | ||
6757edfe MM |
2617 | tree |
2618 | push_template_decl (decl) | |
2619 | tree decl; | |
2620 | { | |
2621 | return push_template_decl_real (decl, 0); | |
2622 | } | |
2623 | ||
2624 | /* Called when a class template TYPE is redeclared with the indicated | |
2625 | template PARMS, e.g.: | |
7fe6899f MM |
2626 | |
2627 | template <class T> struct S; | |
2628 | template <class T> struct S {}; */ | |
2629 | ||
2630 | void | |
6757edfe | 2631 | redeclare_class_template (type, parms) |
7fe6899f | 2632 | tree type; |
6757edfe | 2633 | tree parms; |
7fe6899f | 2634 | { |
3d7de1fa | 2635 | tree tmpl; |
6757edfe | 2636 | tree tmpl_parms; |
7fe6899f MM |
2637 | int i; |
2638 | ||
3d7de1fa MM |
2639 | if (!TYPE_TEMPLATE_INFO (type)) |
2640 | { | |
2641 | cp_error ("`%T' is not a template type", type); | |
2642 | return; | |
2643 | } | |
2644 | ||
2645 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
2646 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
2647 | /* The type is nested in some template class. Nothing to worry | |
2648 | about here; there are no new template parameters for the nested | |
2649 | type. */ | |
2650 | return; | |
2651 | ||
6757edfe MM |
2652 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
2653 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
2654 | ||
7fe6899f MM |
2655 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
2656 | { | |
8251199e JM |
2657 | cp_error_at ("previous declaration `%D'", tmpl); |
2658 | cp_error ("used %d template parameter%s instead of %d", | |
7fe6899f MM |
2659 | TREE_VEC_LENGTH (tmpl_parms), |
2660 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
2661 | TREE_VEC_LENGTH (parms)); | |
2662 | return; | |
2663 | } | |
2664 | ||
2665 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
2666 | { | |
2667 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
2668 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
2669 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
2670 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
2671 | ||
2672 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
2673 | { | |
8251199e JM |
2674 | cp_error_at ("template parameter `%#D'", tmpl_parm); |
2675 | cp_error ("redeclared here as `%#D'", parm); | |
7fe6899f MM |
2676 | return; |
2677 | } | |
2678 | ||
2679 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
2680 | { | |
2681 | /* We have in [temp.param]: | |
2682 | ||
2683 | A template-parameter may not be given default arguments | |
2684 | by two different declarations in the same scope. */ | |
8251199e JM |
2685 | cp_error ("redefinition of default argument for `%#D'", parm); |
2686 | cp_error_at (" original definition appeared here", tmpl_parm); | |
7fe6899f MM |
2687 | return; |
2688 | } | |
2689 | ||
2690 | if (parm_default != NULL_TREE) | |
2691 | /* Update the previous template parameters (which are the ones | |
2692 | that will really count) with the new default value. */ | |
2693 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
2694 | else if (tmpl_default != NULL_TREE) |
2695 | /* Update the new parameters, too; they'll be used as the | |
2696 | parameters for any members. */ | |
2697 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f MM |
2698 | } |
2699 | } | |
75650646 | 2700 | |
75650646 MM |
2701 | /* Attempt to convert the non-type template parameter EXPR to the |
2702 | indicated TYPE. If the conversion is successful, return the | |
2703 | converted value. If the conversion is unsuccesful, return | |
2704 | NULL_TREE if we issued an error message, or error_mark_node if we | |
2705 | did not. We issue error messages for out-and-out bad template | |
2706 | parameters, but not simply because the conversion failed, since we | |
2707 | might be just trying to do argument deduction. By the time this | |
2708 | function is called, neither TYPE nor EXPR may make use of template | |
2709 | parameters. */ | |
2710 | ||
2711 | static tree | |
e1467ff2 | 2712 | convert_nontype_argument (type, expr) |
75650646 MM |
2713 | tree type; |
2714 | tree expr; | |
2715 | { | |
2716 | tree expr_type = TREE_TYPE (expr); | |
2717 | ||
2718 | /* A template-argument for a non-type, non-template | |
2719 | template-parameter shall be one of: | |
2720 | ||
2721 | --an integral constant-expression of integral or enumeration | |
2722 | type; or | |
2723 | ||
2724 | --the name of a non-type template-parameter; or | |
2725 | ||
2726 | --the name of an object or function with external linkage, | |
2727 | including function templates and function template-ids but | |
86052cc3 | 2728 | excluding non-static class members, expressed as id-expression; |
75650646 MM |
2729 | or |
2730 | ||
2731 | --the address of an object or function with external linkage, | |
2732 | including function templates and function template-ids but | |
2733 | excluding non-static class members, expressed as & id-expression | |
2734 | where the & is optional if the name refers to a function or | |
2735 | array; or | |
2736 | ||
2737 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
2738 | ||
638dd8fc MM |
2739 | /* An integral constant-expression can include const variables or |
2740 | enumerators. Simplify things by folding them to their values, | |
2741 | unless we're about to bind the declaration to a reference | |
2742 | parameter. */ | |
2743 | if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr) | |
2744 | && TREE_CODE (type) != REFERENCE_TYPE) | |
86052cc3 JM |
2745 | expr = decl_constant_value (expr); |
2746 | ||
7bf2682f MM |
2747 | if (is_overloaded_fn (expr)) |
2748 | /* OK for now. We'll check that it has external linkage later. | |
2749 | Check this first since if expr_type is the unknown_type_node | |
2750 | we would otherwise complain below. */ | |
2751 | ; | |
e08a8f45 MM |
2752 | else if (TYPE_PTRMEM_P (expr_type) |
2753 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2754 | { | |
2755 | if (TREE_CODE (expr) != PTRMEM_CST) | |
2756 | goto bad_argument; | |
2757 | } | |
f3400fe2 | 2758 | else if (TYPE_PTR_P (expr_type) |
e08a8f45 | 2759 | || TYPE_PTRMEM_P (expr_type) |
f3400fe2 JM |
2760 | || TREE_CODE (expr_type) == ARRAY_TYPE |
2761 | || TREE_CODE (type) == REFERENCE_TYPE | |
75650646 MM |
2762 | /* If expr is the address of an overloaded function, we |
2763 | will get the unknown_type_node at this point. */ | |
2764 | || expr_type == unknown_type_node) | |
2765 | { | |
2766 | tree referent; | |
49bf0d6f MM |
2767 | tree e = expr; |
2768 | STRIP_NOPS (e); | |
75650646 | 2769 | |
f3400fe2 JM |
2770 | if (TREE_CODE (type) == REFERENCE_TYPE |
2771 | || TREE_CODE (expr_type) == ARRAY_TYPE) | |
2772 | referent = e; | |
2773 | else | |
75650646 | 2774 | { |
f3400fe2 JM |
2775 | if (TREE_CODE (e) != ADDR_EXPR) |
2776 | { | |
2777 | bad_argument: | |
2778 | cp_error ("`%E' is not a valid template argument", expr); | |
e08a8f45 MM |
2779 | if (TYPE_PTR_P (expr_type)) |
2780 | { | |
2781 | if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) | |
2782 | cp_error ("it must be the address of a function with external linkage"); | |
2783 | else | |
2784 | cp_error ("it must be the address of an object with external linkage"); | |
2785 | } | |
2786 | else if (TYPE_PTRMEM_P (expr_type) | |
2787 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2788 | cp_error ("it must be a pointer-to-member of the form `&X::Y'"); | |
2789 | ||
f3400fe2 JM |
2790 | return NULL_TREE; |
2791 | } | |
2792 | ||
2793 | referent = TREE_OPERAND (e, 0); | |
2794 | STRIP_NOPS (referent); | |
75650646 MM |
2795 | } |
2796 | ||
75650646 MM |
2797 | if (TREE_CODE (referent) == STRING_CST) |
2798 | { | |
8251199e | 2799 | cp_error ("string literal %E is not a valid template argument", |
75650646 | 2800 | referent); |
8251199e | 2801 | error ("because it is the address of an object with static linkage"); |
75650646 MM |
2802 | return NULL_TREE; |
2803 | } | |
2804 | ||
2805 | if (is_overloaded_fn (referent)) | |
2806 | /* We'll check that it has external linkage later. */ | |
2807 | ; | |
2808 | else if (TREE_CODE (referent) != VAR_DECL) | |
2809 | goto bad_argument; | |
2810 | else if (!TREE_PUBLIC (referent)) | |
2811 | { | |
8251199e | 2812 | cp_error ("address of non-extern `%E' cannot be used as template argument", referent); |
75650646 MM |
2813 | return error_mark_node; |
2814 | } | |
2815 | } | |
f3400fe2 JM |
2816 | else if (INTEGRAL_TYPE_P (expr_type) |
2817 | || TYPE_PTRMEM_P (expr_type) | |
2818 | || TYPE_PTRMEMFUNC_P (expr_type) | |
2819 | /* The next two are g++ extensions. */ | |
2820 | || TREE_CODE (expr_type) == REAL_TYPE | |
2821 | || TREE_CODE (expr_type) == COMPLEX_TYPE) | |
75650646 | 2822 | { |
f3400fe2 JM |
2823 | if (! TREE_CONSTANT (expr)) |
2824 | { | |
2825 | non_constant: | |
2826 | cp_error ("non-constant `%E' cannot be used as template argument", | |
2827 | expr); | |
2828 | return NULL_TREE; | |
2829 | } | |
75650646 | 2830 | } |
75650646 MM |
2831 | else |
2832 | { | |
8251199e | 2833 | cp_error ("object `%E' cannot be used as template argument", expr); |
75650646 MM |
2834 | return NULL_TREE; |
2835 | } | |
2836 | ||
2837 | switch (TREE_CODE (type)) | |
2838 | { | |
2839 | case INTEGER_TYPE: | |
2840 | case BOOLEAN_TYPE: | |
2841 | case ENUMERAL_TYPE: | |
2842 | /* For a non-type template-parameter of integral or enumeration | |
2843 | type, integral promotions (_conv.prom_) and integral | |
2844 | conversions (_conv.integral_) are applied. */ | |
2845 | if (!INTEGRAL_TYPE_P (expr_type)) | |
2846 | return error_mark_node; | |
2847 | ||
2848 | /* It's safe to call digest_init in this case; we know we're | |
2849 | just converting one integral constant expression to another. */ | |
db02b6b9 | 2850 | expr = digest_init (type, expr, (tree*) 0); |
75650646 | 2851 | |
db02b6b9 | 2852 | if (TREE_CODE (expr) != INTEGER_CST) |
f3400fe2 | 2853 | /* Curiously, some TREE_CONSTANT integral expressions do not |
db02b6b9 MM |
2854 | simplify to integer constants. For example, `3 % 0', |
2855 | remains a TRUNC_MOD_EXPR. */ | |
2856 | goto non_constant; | |
2857 | ||
2858 | return expr; | |
2859 | ||
abff8e06 JM |
2860 | case REAL_TYPE: |
2861 | case COMPLEX_TYPE: | |
2862 | /* These are g++ extensions. */ | |
2863 | if (TREE_CODE (expr_type) != TREE_CODE (type)) | |
2864 | return error_mark_node; | |
2865 | ||
db02b6b9 MM |
2866 | expr = digest_init (type, expr, (tree*) 0); |
2867 | ||
2868 | if (TREE_CODE (expr) != REAL_CST) | |
2869 | goto non_constant; | |
2870 | ||
2871 | return expr; | |
abff8e06 | 2872 | |
75650646 MM |
2873 | case POINTER_TYPE: |
2874 | { | |
2875 | tree type_pointed_to = TREE_TYPE (type); | |
2876 | ||
2877 | if (TYPE_PTRMEM_P (type)) | |
93aa462d MM |
2878 | { |
2879 | tree e; | |
2880 | ||
2881 | /* For a non-type template-parameter of type pointer to data | |
2882 | member, qualification conversions (_conv.qual_) are | |
2883 | applied. */ | |
2884 | e = perform_qualification_conversions (type, expr); | |
2885 | if (TREE_CODE (e) == NOP_EXPR) | |
87533b37 MM |
2886 | /* The call to perform_qualification_conversions will |
2887 | insert a NOP_EXPR over EXPR to do express conversion, | |
2888 | if necessary. But, that will confuse us if we use | |
2889 | this (converted) template parameter to instantiate | |
2890 | another template; then the thing will not look like a | |
2891 | valid template argument. So, just make a new | |
2892 | constant, of the appropriate type. */ | |
2893 | e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); | |
93aa462d MM |
2894 | return e; |
2895 | } | |
75650646 MM |
2896 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) |
2897 | { | |
2898 | /* For a non-type template-parameter of type pointer to | |
2899 | function, only the function-to-pointer conversion | |
2900 | (_conv.func_) is applied. If the template-argument | |
2901 | represents a set of overloaded functions (or a pointer to | |
2902 | such), the matching function is selected from the set | |
2903 | (_over.over_). */ | |
2904 | tree fns; | |
2905 | tree fn; | |
2906 | ||
7bf2682f | 2907 | if (TREE_CODE (expr) == ADDR_EXPR) |
75650646 MM |
2908 | fns = TREE_OPERAND (expr, 0); |
2909 | else | |
2910 | fns = expr; | |
2911 | ||
a05273d4 | 2912 | fn = instantiate_type (type_pointed_to, fns, 0); |
75650646 MM |
2913 | |
2914 | if (fn == error_mark_node) | |
2915 | return error_mark_node; | |
2916 | ||
2917 | if (!TREE_PUBLIC (fn)) | |
2918 | { | |
2919 | if (really_overloaded_fn (fns)) | |
2920 | return error_mark_node; | |
2921 | else | |
2922 | goto bad_argument; | |
2923 | } | |
2924 | ||
2925 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2926 | ||
3bfdc719 | 2927 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2928 | 0); |
2929 | return expr; | |
2930 | } | |
2931 | else | |
2932 | { | |
2933 | /* For a non-type template-parameter of type pointer to | |
2934 | object, qualification conversions (_conv.qual_) and the | |
2935 | array-to-pointer conversion (_conv.array_) are applied. | |
2936 | [Note: In particular, neither the null pointer conversion | |
2937 | (_conv.ptr_) nor the derived-to-base conversion | |
2938 | (_conv.ptr_) are applied. Although 0 is a valid | |
2939 | template-argument for a non-type template-parameter of | |
2940 | integral type, it is not a valid template-argument for a | |
e1467ff2 MM |
2941 | non-type template-parameter of pointer type.] |
2942 | ||
2943 | The call to decay_conversion performs the | |
2944 | array-to-pointer conversion, if appropriate. */ | |
2945 | expr = decay_conversion (expr); | |
75650646 MM |
2946 | |
2947 | if (expr == error_mark_node) | |
2948 | return error_mark_node; | |
2949 | else | |
2950 | return perform_qualification_conversions (type, expr); | |
2951 | } | |
2952 | } | |
2953 | break; | |
2954 | ||
2955 | case REFERENCE_TYPE: | |
2956 | { | |
2957 | tree type_referred_to = TREE_TYPE (type); | |
2958 | ||
2959 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
2960 | { | |
2961 | /* For a non-type template-parameter of type reference to | |
39c01e4c MM |
2962 | function, no conversions apply. If the |
2963 | template-argument represents a set of overloaded | |
2964 | functions, the matching function is selected from the | |
2965 | set (_over.over_). */ | |
75650646 MM |
2966 | tree fns = expr; |
2967 | tree fn; | |
2968 | ||
a05273d4 | 2969 | fn = instantiate_type (type_referred_to, fns, 0); |
e6e174e5 JM |
2970 | |
2971 | if (fn == error_mark_node) | |
2972 | return error_mark_node; | |
75650646 MM |
2973 | |
2974 | if (!TREE_PUBLIC (fn)) | |
2975 | { | |
2976 | if (really_overloaded_fn (fns)) | |
2977 | /* Don't issue an error here; we might get a different | |
2978 | function if the overloading had worked out | |
2979 | differently. */ | |
2980 | return error_mark_node; | |
2981 | else | |
2982 | goto bad_argument; | |
2983 | } | |
2984 | ||
3bfdc719 MM |
2985 | my_friendly_assert (same_type_p (type_referred_to, |
2986 | TREE_TYPE (fn)), | |
75650646 MM |
2987 | 0); |
2988 | ||
2989 | return fn; | |
2990 | } | |
2991 | else | |
2992 | { | |
2993 | /* For a non-type template-parameter of type reference to | |
2994 | object, no conversions apply. The type referred to by the | |
2995 | reference may be more cv-qualified than the (otherwise | |
2996 | identical) type of the template-argument. The | |
2997 | template-parameter is bound directly to the | |
2998 | template-argument, which must be an lvalue. */ | |
ff350acd JM |
2999 | if ((TYPE_MAIN_VARIANT (expr_type) |
3000 | != TYPE_MAIN_VARIANT (type_referred_to)) | |
91063b51 MM |
3001 | || !at_least_as_qualified_p (type_referred_to, |
3002 | expr_type) | |
75650646 MM |
3003 | || !real_lvalue_p (expr)) |
3004 | return error_mark_node; | |
3005 | else | |
3006 | return expr; | |
3007 | } | |
3008 | } | |
3009 | break; | |
3010 | ||
3011 | case RECORD_TYPE: | |
3012 | { | |
410c2fba KL |
3013 | if (!TYPE_PTRMEMFUNC_P (type)) |
3014 | /* This handles templates like | |
3015 | template<class T, T t> void f(); | |
3016 | when T is substituted with any class. The second template | |
3017 | parameter becomes invalid and the template candidate is | |
3018 | rejected. */ | |
3019 | return error_mark_node; | |
75650646 MM |
3020 | |
3021 | /* For a non-type template-parameter of type pointer to member | |
3022 | function, no conversions apply. If the template-argument | |
3023 | represents a set of overloaded member functions, the | |
3024 | matching member function is selected from the set | |
3025 | (_over.over_). */ | |
3026 | ||
3027 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
3028 | expr_type != unknown_type_node) | |
3029 | return error_mark_node; | |
3030 | ||
e08a8f45 | 3031 | if (TREE_CODE (expr) == PTRMEM_CST) |
75650646 MM |
3032 | { |
3033 | /* A ptr-to-member constant. */ | |
3bfdc719 | 3034 | if (!same_type_p (type, expr_type)) |
75650646 MM |
3035 | return error_mark_node; |
3036 | else | |
3037 | return expr; | |
3038 | } | |
3039 | ||
3040 | if (TREE_CODE (expr) != ADDR_EXPR) | |
3041 | return error_mark_node; | |
3042 | ||
d8f8dca1 | 3043 | expr = instantiate_type (type, expr, 0); |
75650646 | 3044 | |
d8f8dca1 | 3045 | if (expr == error_mark_node) |
75650646 MM |
3046 | return error_mark_node; |
3047 | ||
3bfdc719 | 3048 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
3049 | 0); |
3050 | return expr; | |
3051 | } | |
3052 | break; | |
3053 | ||
3054 | default: | |
3055 | /* All non-type parameters must have one of these types. */ | |
3056 | my_friendly_abort (0); | |
3057 | break; | |
3058 | } | |
3059 | ||
3060 | return error_mark_node; | |
3061 | } | |
3062 | ||
744fac59 KL |
3063 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
3064 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
3065 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
3066 | or PARM_DECL. | |
3067 | ||
3068 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
3069 | the case, then extra parameters must have default arguments. | |
3070 | ||
3071 | Consider the example: | |
3072 | template <class T, class Allocator = allocator> class vector; | |
3073 | template<template <class U> class TT> class C; | |
3074 | ||
3075 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
3076 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
3077 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
3078 | substitute the TT parameter. */ | |
3079 | ||
3080 | static int | |
4393e105 MM |
3081 | coerce_template_template_parms (parm_parms, arg_parms, complain, |
3082 | in_decl, outer_args) | |
3083 | tree parm_parms, arg_parms; | |
3084 | int complain; | |
3085 | tree in_decl, outer_args; | |
744fac59 KL |
3086 | { |
3087 | int nparms, nargs, i; | |
3088 | tree parm, arg; | |
3089 | ||
3090 | my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); | |
3091 | my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); | |
3092 | ||
3093 | nparms = TREE_VEC_LENGTH (parm_parms); | |
3094 | nargs = TREE_VEC_LENGTH (arg_parms); | |
3095 | ||
3096 | /* The rule here is opposite of coerce_template_parms. */ | |
3097 | if (nargs < nparms | |
3098 | || (nargs > nparms | |
3099 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
3100 | return 0; | |
3101 | ||
3102 | for (i = 0; i < nparms; ++i) | |
3103 | { | |
3104 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
3105 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
3106 | ||
3107 | if (arg == NULL_TREE || arg == error_mark_node | |
3108 | || parm == NULL_TREE || parm == error_mark_node) | |
3109 | return 0; | |
3110 | ||
3111 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
3112 | return 0; | |
3113 | ||
3114 | switch (TREE_CODE (parm)) | |
3115 | { | |
3116 | case TYPE_DECL: | |
3117 | break; | |
3118 | ||
3119 | case TEMPLATE_DECL: | |
3120 | /* We encounter instantiations of templates like | |
3121 | template <template <template <class> class> class TT> | |
3122 | class C; */ | |
700466c2 JM |
3123 | { |
3124 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3125 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3126 | ||
3127 | if (!coerce_template_template_parms (parmparm, argparm, | |
4393e105 MM |
3128 | complain, in_decl, |
3129 | outer_args)) | |
700466c2 JM |
3130 | return 0; |
3131 | } | |
3132 | break; | |
744fac59 KL |
3133 | |
3134 | case PARM_DECL: | |
3135 | /* The tsubst call is used to handle cases such as | |
3136 | template <class T, template <T> class TT> class D; | |
3137 | i.e. the parameter list of TT depends on earlier parameters. */ | |
4393e105 MM |
3138 | if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, |
3139 | complain, in_decl), | |
3bfdc719 | 3140 | TREE_TYPE (arg))) |
744fac59 KL |
3141 | return 0; |
3142 | break; | |
3143 | ||
3144 | default: | |
3145 | my_friendly_abort (0); | |
3146 | } | |
3147 | } | |
3148 | return 1; | |
3149 | } | |
3150 | ||
8b5b8b7c MM |
3151 | /* Convert the indicated template ARG as necessary to match the |
3152 | indicated template PARM. Returns the converted ARG, or | |
3153 | error_mark_node if the conversion was unsuccessful. Error messages | |
3154 | are issued if COMPLAIN is non-zero. This conversion is for the Ith | |
3155 | parameter in the parameter list. ARGS is the full set of template | |
3156 | arguments deduced so far. */ | |
3157 | ||
3158 | static tree | |
3159 | convert_template_argument (parm, arg, args, complain, i, in_decl) | |
3160 | tree parm; | |
3161 | tree arg; | |
3162 | tree args; | |
3163 | int complain; | |
3164 | int i; | |
3165 | tree in_decl; | |
3166 | { | |
3167 | tree val; | |
3168 | tree inner_args; | |
3169 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
3170 | ||
3171 | inner_args = innermost_args (args); | |
3172 | ||
3173 | if (TREE_CODE (arg) == TREE_LIST | |
3174 | && TREE_TYPE (arg) != NULL_TREE | |
3175 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
3176 | { | |
3177 | /* The template argument was the name of some | |
3178 | member function. That's usually | |
3179 | illegal, but static members are OK. In any | |
3180 | case, grab the underlying fields/functions | |
3181 | and issue an error later if required. */ | |
3182 | arg = TREE_VALUE (arg); | |
3183 | TREE_TYPE (arg) = unknown_type_node; | |
3184 | } | |
3185 | ||
3186 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3187 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3188 | || requires_tmpl_type); | |
3189 | ||
3190 | /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, | |
3191 | we also accept implicitly created TYPE_DECL as a valid argument. | |
3192 | This is necessary to handle the case where we pass a template name | |
3193 | to a template template parameter in a scope where we've derived from | |
3194 | in instantiation of that template, so the template name refers to that | |
3195 | instantiation. We really ought to handle this better. */ | |
3196 | is_tmpl_type | |
3197 | = ((TREE_CODE (arg) == TEMPLATE_DECL | |
3198 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3199 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 3200 | && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg)) |
8b5b8b7c MM |
3201 | || (TREE_CODE (arg) == RECORD_TYPE |
3202 | && CLASSTYPE_TEMPLATE_INFO (arg) | |
3203 | && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL | |
3204 | && DECL_ARTIFICIAL (TYPE_NAME (arg)) | |
3205 | && requires_tmpl_type | |
b54ccf71 | 3206 | && is_base_of_enclosing_class (arg, current_class_type))); |
8b5b8b7c MM |
3207 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
3208 | arg = TYPE_STUB_DECL (arg); | |
3209 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) | |
3210 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
3211 | ||
3212 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type; | |
3213 | ||
3214 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3215 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3216 | { | |
8251199e JM |
3217 | cp_pedwarn ("to refer to a type member of a template parameter,"); |
3218 | cp_pedwarn (" use `typename %E'", arg); | |
8b5b8b7c MM |
3219 | |
3220 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3221 | TREE_OPERAND (arg, 1)); | |
3222 | is_type = 1; | |
3223 | } | |
3224 | if (is_type != requires_type) | |
3225 | { | |
3226 | if (in_decl) | |
3227 | { | |
3228 | if (complain) | |
3229 | { | |
8251199e | 3230 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3231 | i + 1, in_decl); |
3232 | if (is_type) | |
8251199e | 3233 | cp_error (" expected a constant of type `%T', got `%T'", |
8b5b8b7c MM |
3234 | TREE_TYPE (parm), |
3235 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
3236 | else | |
8251199e | 3237 | cp_error (" expected a type, got `%E'", arg); |
8b5b8b7c MM |
3238 | } |
3239 | } | |
3240 | return error_mark_node; | |
3241 | } | |
3242 | if (is_tmpl_type ^ requires_tmpl_type) | |
3243 | { | |
3244 | if (in_decl && complain) | |
3245 | { | |
8251199e | 3246 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3247 | i + 1, in_decl); |
3248 | if (is_tmpl_type) | |
8251199e | 3249 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); |
8b5b8b7c | 3250 | else |
8251199e | 3251 | cp_error (" expected a class template, got `%T'", arg); |
8b5b8b7c MM |
3252 | } |
3253 | return error_mark_node; | |
3254 | } | |
3255 | ||
3256 | if (is_type) | |
3257 | { | |
3258 | if (requires_tmpl_type) | |
3259 | { | |
3260 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3261 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3262 | ||
4393e105 | 3263 | if (coerce_template_template_parms (parmparm, argparm, complain, |
8b5b8b7c MM |
3264 | in_decl, inner_args)) |
3265 | { | |
3266 | val = arg; | |
3267 | ||
3268 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3269 | TEMPLATE_DECL. */ | |
3270 | if (val != error_mark_node | |
3271 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3272 | val = TREE_TYPE (val); | |
3273 | } | |
3274 | else | |
3275 | { | |
3276 | if (in_decl && complain) | |
3277 | { | |
8251199e | 3278 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c | 3279 | i + 1, in_decl); |
8251199e | 3280 | cp_error (" expected a template of type `%D', got `%D'", parm, arg); |
8b5b8b7c MM |
3281 | } |
3282 | ||
3283 | val = error_mark_node; | |
3284 | } | |
3285 | } | |
3286 | else | |
3287 | { | |
3288 | val = groktypename (arg); | |
3289 | if (! processing_template_decl) | |
3290 | { | |
3291 | /* [basic.link]: A name with no linkage (notably, the | |
3292 | name of a class or enumeration declared in a local | |
3293 | scope) shall not be used to declare an entity with | |
3294 | linkage. This implies that names with no linkage | |
3295 | cannot be used as template arguments. */ | |
3296 | tree t = no_linkage_check (val); | |
3297 | if (t) | |
3298 | { | |
3299 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
8251199e JM |
3300 | cp_pedwarn |
3301 | ("template-argument `%T' uses anonymous type", val); | |
8b5b8b7c | 3302 | else |
8251199e JM |
3303 | cp_error |
3304 | ("template-argument `%T' uses local type `%T'", | |
8b5b8b7c MM |
3305 | val, t); |
3306 | return error_mark_node; | |
3307 | } | |
3308 | } | |
3309 | } | |
3310 | } | |
3311 | else | |
3312 | { | |
4393e105 | 3313 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c MM |
3314 | |
3315 | if (processing_template_decl) | |
3316 | arg = maybe_fold_nontype_arg (arg); | |
3317 | ||
3318 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
3319 | /* We used to call digest_init here. However, digest_init | |
3320 | will report errors, which we don't want when complain | |
3321 | is zero. More importantly, digest_init will try too | |
3322 | hard to convert things: for example, `0' should not be | |
3323 | converted to pointer type at this point according to | |
3324 | the standard. Accepting this is not merely an | |
3325 | extension, since deciding whether or not these | |
3326 | conversions can occur is part of determining which | |
3327 | function template to call, or whether a given epxlicit | |
3328 | argument specification is legal. */ | |
3329 | val = convert_nontype_argument (t, arg); | |
3330 | else | |
3331 | val = arg; | |
3332 | ||
3333 | if (val == NULL_TREE) | |
3334 | val = error_mark_node; | |
3335 | else if (val == error_mark_node && complain) | |
8251199e | 3336 | cp_error ("could not convert template argument `%E' to `%T'", |
8b5b8b7c MM |
3337 | arg, t); |
3338 | } | |
3339 | ||
3340 | return val; | |
3341 | } | |
3342 | ||
3343 | /* Convert all template arguments to their appropriate types, and | |
3344 | return a vector containing the innermost resulting template | |
3345 | arguments. If any error occurs, return error_mark_node, and, if | |
3346 | COMPLAIN is non-zero, issue an error message. Some error messages | |
3347 | are issued even if COMPLAIN is zero; for instance, if a template | |
3348 | argument is composed from a local class. | |
75650646 MM |
3349 | |
3350 | If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be | |
3351 | provided in ARGLIST, or else trailing parameters must have default | |
3352 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
cabc336a MM |
3353 | deduction for any unspecified trailing arguments. |
3354 | ||
3355 | The resulting TREE_VEC is allocated on a temporary obstack, and | |
3356 | must be explicitly copied if it will be permanent. */ | |
de575009 | 3357 | |
8d08fdba | 3358 | static tree |
8b5b8b7c | 3359 | coerce_template_parms (parms, args, in_decl, |
75650646 | 3360 | complain, |
744fac59 | 3361 | require_all_arguments) |
8b5b8b7c | 3362 | tree parms, args; |
8d08fdba | 3363 | tree in_decl; |
75650646 MM |
3364 | int complain; |
3365 | int require_all_arguments; | |
8d08fdba | 3366 | { |
a292b002 | 3367 | int nparms, nargs, i, lost = 0; |
e4a84209 | 3368 | tree inner_args; |
8b5b8b7c MM |
3369 | tree new_args; |
3370 | tree new_inner_args; | |
a292b002 | 3371 | |
8b5b8b7c | 3372 | inner_args = innermost_args (args); |
e4a84209 | 3373 | nargs = NUM_TMPL_ARGS (inner_args); |
a292b002 MS |
3374 | nparms = TREE_VEC_LENGTH (parms); |
3375 | ||
3376 | if (nargs > nparms | |
3377 | || (nargs < nparms | |
75650646 | 3378 | && require_all_arguments |
a292b002 | 3379 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3380 | { |
75650646 MM |
3381 | if (complain) |
3382 | { | |
8251199e | 3383 | cp_error ("wrong number of template arguments (%d, should be %d)", |
e4a84209 | 3384 | nargs, nparms); |
75650646 MM |
3385 | |
3386 | if (in_decl) | |
8251199e | 3387 | cp_error_at ("provided for `%D'", in_decl); |
75650646 MM |
3388 | } |
3389 | ||
8d08fdba MS |
3390 | return error_mark_node; |
3391 | } | |
3392 | ||
cabc336a | 3393 | new_inner_args = make_temp_vec (nparms); |
8b5b8b7c MM |
3394 | new_args = add_outermost_template_args (args, new_inner_args); |
3395 | for (i = 0; i < nparms; i++) | |
8d08fdba | 3396 | { |
8b5b8b7c MM |
3397 | tree arg; |
3398 | tree parm; | |
e4a84209 | 3399 | |
8b5b8b7c MM |
3400 | /* Get the Ith template parameter. */ |
3401 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 3402 | |
8b5b8b7c MM |
3403 | /* Calculate the Ith argument. */ |
3404 | if (inner_args && TREE_CODE (inner_args) == TREE_LIST) | |
8d08fdba | 3405 | { |
8b5b8b7c MM |
3406 | arg = TREE_VALUE (inner_args); |
3407 | inner_args = TREE_CHAIN (inner_args); | |
8d08fdba | 3408 | } |
8b5b8b7c MM |
3409 | else if (i < nargs) |
3410 | arg = TREE_VEC_ELT (inner_args, i); | |
3411 | /* If no template argument was supplied, look for a default | |
3412 | value. */ | |
3413 | else if (TREE_PURPOSE (parm) == NULL_TREE) | |
3414 | { | |
3415 | /* There was no default value. */ | |
3416 | my_friendly_assert (!require_all_arguments, 0); | |
3417 | break; | |
3418 | } | |
3419 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
4393e105 | 3420 | arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); |
8b5b8b7c | 3421 | else |
4393e105 MM |
3422 | arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, |
3423 | in_decl); | |
e4a84209 | 3424 | |
8b5b8b7c | 3425 | /* Now, convert the Ith argument, as necessary. */ |
75650646 MM |
3426 | if (arg == NULL_TREE) |
3427 | /* We're out of arguments. */ | |
3428 | { | |
3429 | my_friendly_assert (!require_all_arguments, 0); | |
3430 | break; | |
3431 | } | |
8b5b8b7c | 3432 | else if (arg == error_mark_node) |
75650646 | 3433 | { |
8251199e | 3434 | cp_error ("template argument %d is invalid", i + 1); |
8b5b8b7c | 3435 | arg = error_mark_node; |
8d08fdba | 3436 | } |
8b5b8b7c MM |
3437 | else |
3438 | arg = convert_template_argument (TREE_VALUE (parm), | |
3439 | arg, new_args, complain, i, | |
3440 | in_decl); | |
3441 | ||
3442 | if (arg == error_mark_node) | |
8d08fdba | 3443 | lost++; |
8b5b8b7c | 3444 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 3445 | } |
8b5b8b7c | 3446 | |
8d08fdba MS |
3447 | if (lost) |
3448 | return error_mark_node; | |
8b5b8b7c MM |
3449 | |
3450 | return new_inner_args; | |
8d08fdba MS |
3451 | } |
3452 | ||
34016c81 JM |
3453 | /* Returns 1 if template args OT and NT are equivalent. */ |
3454 | ||
d8e178a0 | 3455 | static int |
34016c81 JM |
3456 | template_args_equal (ot, nt) |
3457 | tree ot, nt; | |
3458 | { | |
3459 | if (nt == ot) | |
3460 | return 1; | |
3461 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
3462 | return 0; | |
3463 | if (TREE_CODE (nt) == TREE_VEC) | |
3464 | /* For member templates */ | |
3465 | return comp_template_args (ot, nt); | |
3466 | else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') | |
3bfdc719 | 3467 | return same_type_p (ot, nt); |
34016c81 JM |
3468 | else |
3469 | return (cp_tree_equal (ot, nt) > 0); | |
3470 | } | |
3471 | ||
3472 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
3473 | of template arguments. Returns 0 otherwise. */ |
3474 | ||
6757edfe | 3475 | int |
5566b478 MS |
3476 | comp_template_args (oldargs, newargs) |
3477 | tree oldargs, newargs; | |
3478 | { | |
3479 | int i; | |
3480 | ||
386b8a85 JM |
3481 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
3482 | return 0; | |
3483 | ||
5566b478 MS |
3484 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
3485 | { | |
3486 | tree nt = TREE_VEC_ELT (newargs, i); | |
3487 | tree ot = TREE_VEC_ELT (oldargs, i); | |
3488 | ||
34016c81 | 3489 | if (! template_args_equal (ot, nt)) |
61a127b3 | 3490 | return 0; |
5566b478 MS |
3491 | } |
3492 | return 1; | |
3493 | } | |
3494 | ||
8d08fdba MS |
3495 | /* Given class template name and parameter list, produce a user-friendly name |
3496 | for the instantiation. */ | |
e92cc029 | 3497 | |
8d08fdba | 3498 | static char * |
36a117a5 | 3499 | mangle_class_name_for_template (name, parms, arglist) |
8d08fdba MS |
3500 | char *name; |
3501 | tree parms, arglist; | |
3502 | { | |
3503 | static struct obstack scratch_obstack; | |
3504 | static char *scratch_firstobj; | |
3505 | int i, nparms; | |
8d08fdba MS |
3506 | |
3507 | if (!scratch_firstobj) | |
fc378698 | 3508 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
3509 | else |
3510 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 3511 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 3512 | |
8d08fdba | 3513 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); |
8d08fdba | 3514 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) |
8d08fdba MS |
3515 | |
3516 | cat (name); | |
3517 | ccat ('<'); | |
3518 | nparms = TREE_VEC_LENGTH (parms); | |
36a117a5 | 3519 | arglist = innermost_args (arglist); |
8d08fdba MS |
3520 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); |
3521 | for (i = 0; i < nparms; i++) | |
3522 | { | |
a292b002 MS |
3523 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
3524 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
3525 | |
3526 | if (i) | |
3527 | ccat (','); | |
3528 | ||
a292b002 | 3529 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 3530 | { |
294471c3 | 3531 | cat (type_as_string_real (arg, 0, 1)); |
8d08fdba MS |
3532 | continue; |
3533 | } | |
73b0fce8 KL |
3534 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
3535 | { | |
3536 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
3537 | { |
3538 | /* Already substituted with real template. Just output | |
3539 | the template name here */ | |
cb0dbb9a JM |
3540 | tree context = DECL_CONTEXT (arg); |
3541 | if (context) | |
2c73f9f5 | 3542 | { |
cb0dbb9a | 3543 | my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422); |
2c73f9f5 ML |
3544 | cat(decl_as_string (DECL_CONTEXT (arg), 0)); |
3545 | cat("::"); | |
3546 | } | |
3547 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3548 | } | |
73b0fce8 KL |
3549 | else |
3550 | /* Output the parameter declaration */ | |
294471c3 | 3551 | cat (type_as_string_real (arg, 0, 1)); |
73b0fce8 KL |
3552 | continue; |
3553 | } | |
8d08fdba MS |
3554 | else |
3555 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
3556 | ||
3557 | if (TREE_CODE (arg) == TREE_LIST) | |
3558 | { | |
3559 | /* New list cell was built because old chain link was in | |
3560 | use. */ | |
3561 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
3562 | arg = TREE_VALUE (arg); | |
3563 | } | |
3564 | /* No need to check arglist against parmlist here; we did that | |
3565 | in coerce_template_parms, called from lookup_template_class. */ | |
3566 | cat (expr_as_string (arg, 0)); | |
3567 | } | |
3568 | { | |
3569 | char *bufp = obstack_next_free (&scratch_obstack); | |
3570 | int offset = 0; | |
3571 | while (bufp[offset - 1] == ' ') | |
3572 | offset--; | |
3573 | obstack_blank_fast (&scratch_obstack, offset); | |
3574 | ||
3575 | /* B<C<char> >, not B<C<char>> */ | |
3576 | if (bufp[offset - 1] == '>') | |
3577 | ccat (' '); | |
3578 | } | |
3579 | ccat ('>'); | |
3580 | ccat ('\0'); | |
3581 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
3582 | } |
3583 | ||
bd6dd845 | 3584 | static tree |
5566b478 MS |
3585 | classtype_mangled_name (t) |
3586 | tree t; | |
3587 | { | |
3588 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
3589 | /* Specializations have already had their names set up in |
3590 | lookup_template_class. */ | |
9fbf56f7 MM |
3591 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
3592 | { | |
3593 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
3594 | ||
36a117a5 MM |
3595 | /* For non-primary templates, the template parameters are |
3596 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
3597 | if (PRIMARY_TEMPLATE_P (tmpl)) |
3598 | { | |
3599 | tree name = DECL_NAME (tmpl); | |
3600 | char *mangled_name = mangle_class_name_for_template | |
3601 | (IDENTIFIER_POINTER (name), | |
3602 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
3603 | CLASSTYPE_TI_ARGS (t)); | |
3604 | tree id = get_identifier (mangled_name); | |
3605 | IDENTIFIER_TEMPLATE (id) = name; | |
3606 | return id; | |
3607 | } | |
5566b478 | 3608 | } |
9fbf56f7 MM |
3609 | |
3610 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
3611 | } |
3612 | ||
3613 | static void | |
3614 | add_pending_template (d) | |
3615 | tree d; | |
3616 | { | |
e92cc029 MS |
3617 | tree ti; |
3618 | ||
3619 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
3620 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
3621 | else | |
3622 | ti = DECL_TEMPLATE_INFO (d); | |
3623 | ||
824b9a4c | 3624 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
3625 | return; |
3626 | ||
3627 | *template_tail = perm_tree_cons | |
1139b3d8 | 3628 | (build_srcloc_here (), d, NULL_TREE); |
5566b478 | 3629 | template_tail = &TREE_CHAIN (*template_tail); |
824b9a4c | 3630 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
3631 | } |
3632 | ||
386b8a85 JM |
3633 | |
3634 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
3635 | may be either a _DECL or an overloaded function or an | |
3636 | IDENTIFIER_NODE), and ARGLIST. */ | |
3637 | ||
3638 | tree | |
3639 | lookup_template_function (fns, arglist) | |
3640 | tree fns, arglist; | |
3641 | { | |
2c73f9f5 | 3642 | tree type; |
050367a3 | 3643 | |
386b8a85 JM |
3644 | if (fns == NULL_TREE) |
3645 | { | |
8251199e | 3646 | cp_error ("non-template used as template"); |
386b8a85 JM |
3647 | return error_mark_node; |
3648 | } | |
3649 | ||
2c73f9f5 ML |
3650 | type = TREE_TYPE (fns); |
3651 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
3652 | type = unknown_type_node; | |
3653 | ||
672476cb MM |
3654 | if (processing_template_decl) |
3655 | return build_min (TEMPLATE_ID_EXPR, type, fns, arglist); | |
3656 | else | |
3657 | return build (TEMPLATE_ID_EXPR, type, fns, arglist); | |
386b8a85 JM |
3658 | } |
3659 | ||
a2b60a0e MM |
3660 | /* Within the scope of a template class S<T>, the name S gets bound |
3661 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
3662 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
3663 | or one of its enclosing classes, and that type is a template, | |
3664 | return the associated TEMPLATE_DECL. Otherwise, the original | |
3665 | DECL is returned. */ | |
3666 | ||
e9659ab0 | 3667 | static tree |
a2b60a0e MM |
3668 | maybe_get_template_decl_from_type_decl (decl) |
3669 | tree decl; | |
3670 | { | |
3671 | return (decl != NULL_TREE | |
3672 | && TREE_CODE (decl) == TYPE_DECL | |
3673 | && DECL_ARTIFICIAL (decl) | |
511b60ff | 3674 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
a2b60a0e MM |
3675 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
3676 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
3677 | } | |
386b8a85 | 3678 | |
8d08fdba MS |
3679 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
3680 | parameters, find the desired type. | |
3681 | ||
3682 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
36a117a5 MM |
3683 | (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will |
3684 | be a TREE_LIST if called directly from the parser, and a TREE_VEC | |
3685 | otherwise.) Since ARGLIST is build on the decl_obstack, we must | |
3686 | copy it here to keep it from being reclaimed when the decl storage | |
3687 | is reclaimed. | |
8d08fdba MS |
3688 | |
3689 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
3690 | instantiate. |
3691 | ||
36a117a5 MM |
3692 | If ENTERING_SCOPE is non-zero, we are about to enter the scope of |
3693 | the class we are looking up. | |
3694 | ||
75650646 MM |
3695 | If the template class is really a local class in a template |
3696 | function, then the FUNCTION_CONTEXT is the function in which it is | |
3697 | being instantiated. */ | |
e92cc029 | 3698 | |
8d08fdba | 3699 | tree |
36a117a5 | 3700 | lookup_template_class (d1, arglist, in_decl, context, entering_scope) |
8d08fdba MS |
3701 | tree d1, arglist; |
3702 | tree in_decl; | |
e1467ff2 | 3703 | tree context; |
36a117a5 | 3704 | int entering_scope; |
8d08fdba | 3705 | { |
a703fb38 | 3706 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 3707 | tree t; |
5566b478 MS |
3708 | |
3709 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
3710 | { | |
f181d4ae MM |
3711 | if (IDENTIFIER_VALUE (d1) |
3712 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) | |
3713 | template = IDENTIFIER_VALUE (d1); | |
73b0fce8 KL |
3714 | else |
3715 | { | |
2c73f9f5 ML |
3716 | if (context) |
3717 | push_decl_namespace (context); | |
3ebc5c52 MM |
3718 | template = lookup_name (d1, /*prefer_type=*/0); |
3719 | template = maybe_get_template_decl_from_type_decl (template); | |
2c73f9f5 ML |
3720 | if (context) |
3721 | pop_decl_namespace (); | |
73b0fce8 | 3722 | } |
8d019cef JM |
3723 | if (template) |
3724 | context = DECL_CONTEXT (template); | |
5566b478 | 3725 | } |
c91a56d2 MS |
3726 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
3727 | { | |
802dbc34 JM |
3728 | tree type = TREE_TYPE (d1); |
3729 | ||
3730 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
3731 | an implicit typename for the second A. Deal with it. */ | |
3732 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
3733 | type = TREE_TYPE (type); | |
3734 | ||
3735 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
f3400fe2 | 3736 | { |
802dbc34 | 3737 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
3738 | d1 = DECL_NAME (template); |
3739 | } | |
c91a56d2 | 3740 | } |
ed44da02 MM |
3741 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
3742 | || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' | |
3743 | && IS_AGGR_TYPE (d1))) | |
5566b478 | 3744 | { |
ed44da02 | 3745 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
3746 | d1 = DECL_NAME (template); |
3747 | } | |
93cdc044 JM |
3748 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
3749 | && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL) | |
3750 | { | |
3751 | template = d1; | |
3752 | d1 = DECL_NAME (template); | |
3753 | context = DECL_CONTEXT (template); | |
3754 | } | |
5566b478 MS |
3755 | else |
3756 | my_friendly_abort (272); | |
8d08fdba | 3757 | |
8d08fdba | 3758 | /* With something like `template <class T> class X class X { ... };' |
f181d4ae MM |
3759 | we could end up with D1 having nothing but an IDENTIFIER_VALUE. |
3760 | We don't want to do that, but we have to deal with the situation, | |
3761 | so let's give them some syntax errors to chew on instead of a | |
3762 | crash. */ | |
8d08fdba | 3763 | if (! template) |
f3400fe2 JM |
3764 | { |
3765 | cp_error ("`%T' is not a template", d1); | |
3766 | return error_mark_node; | |
3767 | } | |
2c73f9f5 | 3768 | |
c3baf4b5 JM |
3769 | if (context == NULL_TREE) |
3770 | context = global_namespace; | |
2c73f9f5 | 3771 | |
8d08fdba MS |
3772 | if (TREE_CODE (template) != TEMPLATE_DECL) |
3773 | { | |
8251199e | 3774 | cp_error ("non-template type `%T' used as a template", d1); |
8d08fdba | 3775 | if (in_decl) |
8251199e | 3776 | cp_error_at ("for template declaration `%D'", in_decl); |
8d08fdba MS |
3777 | return error_mark_node; |
3778 | } | |
8d08fdba | 3779 | |
73b0fce8 KL |
3780 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
3781 | { | |
3782 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
3783 | template arguments */ | |
3784 | ||
3785 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
3786 | tree template2 = TYPE_STUB_DECL (parm); | |
3787 | tree arglist2; | |
3788 | ||
73b0fce8 KL |
3789 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
3790 | ||
744fac59 | 3791 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1); |
73b0fce8 KL |
3792 | if (arglist2 == error_mark_node) |
3793 | return error_mark_node; | |
3794 | ||
3795 | arglist2 = copy_to_permanent (arglist2); | |
7ddedda4 | 3796 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm) |
73b0fce8 KL |
3797 | = perm_tree_cons (template2, arglist2, NULL_TREE); |
3798 | TYPE_SIZE (parm) = 0; | |
3799 | return parm; | |
3800 | } | |
36a117a5 | 3801 | else |
8d08fdba | 3802 | { |
36a117a5 | 3803 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 3804 | tree gen_tmpl; |
36a117a5 MM |
3805 | tree type_decl; |
3806 | tree found = NULL_TREE; | |
3807 | int arg_depth; | |
3808 | int parm_depth; | |
dbfe2124 | 3809 | int is_partial_instantiation; |
830bfa74 | 3810 | |
7ac7b28f MM |
3811 | gen_tmpl = most_general_template (template); |
3812 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
3813 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
3814 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3815 | ||
7ddedda4 MM |
3816 | /* We build up the coerced arguments and such on the |
3817 | momentary_obstack. */ | |
3818 | push_momentary (); | |
3819 | ||
36a117a5 MM |
3820 | if (arg_depth == 1 && parm_depth > 1) |
3821 | { | |
39c01e4c | 3822 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
3823 | For example, given: |
3824 | ||
3825 | template <class T> struct S1 { | |
3826 | template <class U> struct S2 {}; | |
3827 | template <class U> struct S2<U*> {}; | |
3828 | }; | |
3829 | ||
3830 | we will be called with an ARGLIST of `U*', but the | |
3831 | TEMPLATE will be `template <class T> template | |
3832 | <class U> struct S1<T>::S2'. We must fill in the missing | |
3833 | arguments. */ | |
7ac7b28f MM |
3834 | arglist |
3835 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), | |
3836 | arglist); | |
36a117a5 MM |
3837 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
3838 | } | |
5566b478 | 3839 | |
7ac7b28f | 3840 | /* Now we should enough arguments. */ |
36a117a5 MM |
3841 | my_friendly_assert (parm_depth == arg_depth, 0); |
3842 | ||
7ac7b28f MM |
3843 | /* From here on, we're only interested in the most general |
3844 | template. */ | |
3845 | template = gen_tmpl; | |
3846 | ||
36a117a5 MM |
3847 | /* Calculate the BOUND_ARGS. These will be the args that are |
3848 | actually tsubst'd into the definition to create the | |
3849 | instantiation. */ | |
3850 | if (parm_depth > 1) | |
830bfa74 MM |
3851 | { |
3852 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 3853 | int i; |
e4a84209 | 3854 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 3855 | |
cabc336a | 3856 | tree bound_args = make_temp_vec (parm_depth); |
830bfa74 | 3857 | |
e4a84209 | 3858 | for (i = saved_depth, |
830bfa74 | 3859 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 3860 | i > 0 && t != NULL_TREE; |
830bfa74 | 3861 | --i, t = TREE_CHAIN (t)) |
e4a84209 MM |
3862 | { |
3863 | tree a = coerce_template_parms (TREE_VALUE (t), | |
3864 | arglist, template, 1, 1); | |
3865 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); | |
3866 | ||
3867 | /* We temporarily reduce the length of the ARGLIST so | |
3868 | that coerce_template_parms will see only the arguments | |
3869 | corresponding to the template parameters it is | |
3870 | examining. */ | |
3871 | TREE_VEC_LENGTH (arglist)--; | |
3872 | } | |
3873 | ||
3874 | /* Restore the ARGLIST to its full size. */ | |
3875 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
3876 | ||
36a117a5 | 3877 | arglist = bound_args; |
830bfa74 MM |
3878 | } |
3879 | else | |
36a117a5 MM |
3880 | arglist |
3881 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
3882 | innermost_args (arglist), | |
3883 | template, 1, 1); | |
3884 | ||
3885 | if (arglist == error_mark_node) | |
3886 | /* We were unable to bind the arguments. */ | |
5566b478 | 3887 | return error_mark_node; |
5566b478 | 3888 | |
36a117a5 MM |
3889 | /* In the scope of a template class, explicit references to the |
3890 | template class refer to the type of the template, not any | |
3891 | instantiation of it. For example, in: | |
3892 | ||
3893 | template <class T> class C { void f(C<T>); } | |
3894 | ||
3895 | the `C<T>' is just the same as `C'. Outside of the | |
3896 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 3897 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
3898 | arglist)) |
3899 | { | |
3900 | found = template_type; | |
3901 | ||
3902 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 3903 | { |
36a117a5 MM |
3904 | tree ctx; |
3905 | ||
3906 | /* Note that we use DECL_CONTEXT, rather than | |
3907 | CP_DECL_CONTEXT, so that the termination test is | |
3908 | always just `ctx'. We're not interested in namepace | |
3909 | scopes. */ | |
3910 | for (ctx = current_class_type; | |
3911 | ctx; | |
3912 | ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't') | |
3913 | ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) | |
3bfdc719 | 3914 | if (same_type_p (ctx, template_type)) |
36a117a5 MM |
3915 | break; |
3916 | ||
3917 | if (!ctx) | |
3918 | /* We're not in the scope of the class, so the | |
3919 | TEMPLATE_TYPE is not the type we want after | |
3920 | all. */ | |
3921 | found = NULL_TREE; | |
5566b478 MS |
3922 | } |
3923 | } | |
36a117a5 MM |
3924 | |
3925 | if (!found) | |
75650646 | 3926 | { |
36a117a5 MM |
3927 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); |
3928 | found; found = TREE_CHAIN (found)) | |
3929 | if (comp_template_args (TREE_PURPOSE (found), arglist)) | |
3930 | break; | |
75650646 | 3931 | |
36a117a5 MM |
3932 | if (found) |
3933 | found = TREE_VALUE (found); | |
8d08fdba | 3934 | } |
3ebc5c52 | 3935 | |
36a117a5 | 3936 | if (found) |
8d08fdba | 3937 | { |
7ddedda4 | 3938 | pop_momentary (); |
36a117a5 | 3939 | return found; |
8d08fdba | 3940 | } |
5566b478 | 3941 | |
dbfe2124 | 3942 | /* This type is a "partial instantiation" if any of the template |
486e4077 MM |
3943 | arguments still inolve template parameters. Note that we set |
3944 | IS_PARTIAL_INSTANTIATION for partial specializations as | |
3945 | well. */ | |
dbfe2124 MM |
3946 | is_partial_instantiation = uses_template_parms (arglist); |
3947 | ||
3ebc5c52 MM |
3948 | if (!is_partial_instantiation |
3949 | && !PRIMARY_TEMPLATE_P (template) | |
3950 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
3951 | { | |
3952 | pop_momentary (); | |
3953 | found = xref_tag_from_type (TREE_TYPE (template), | |
3954 | DECL_NAME (template), | |
3955 | /*globalize=*/1); | |
3956 | return found; | |
3957 | } | |
3958 | ||
3959 | /* Since we didn't find the type, we'll have to create it. | |
3960 | Since we'll be saving this type on the | |
3961 | DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */ | |
3962 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
3963 | ||
36a117a5 | 3964 | /* Create the type. */ |
ed44da02 MM |
3965 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
3966 | { | |
dbfe2124 | 3967 | if (!is_partial_instantiation) |
61a127b3 | 3968 | t = start_enum (TYPE_IDENTIFIER (template_type)); |
ed44da02 | 3969 | else |
dbfe2124 | 3970 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
3971 | the values for the enumeration constants may involve |
3972 | template parameters. And, no one should be interested | |
3973 | in the enumeration constants for such a type. */ | |
3974 | t = make_node (ENUMERAL_TYPE); | |
3975 | } | |
3976 | else | |
3977 | { | |
3978 | t = make_lang_type (TREE_CODE (template_type)); | |
3979 | CLASSTYPE_DECLARED_CLASS (t) | |
3980 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
3981 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
3982 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
f668f160 | 3983 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ed44da02 MM |
3984 | } |
3985 | ||
61a127b3 | 3986 | /* If we called start_enum above, this information will already |
ed44da02 MM |
3987 | be set up. */ |
3988 | if (!TYPE_NAME (t)) | |
3989 | { | |
3990 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 3991 | |
9188c363 | 3992 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 3993 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 3994 | TYPE_STUB_DECL (t) = type_decl; |
ed44da02 MM |
3995 | DECL_SOURCE_FILE (type_decl) |
3996 | = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type)); | |
3997 | DECL_SOURCE_LINE (type_decl) | |
3998 | = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type)); | |
ed44da02 MM |
3999 | } |
4000 | else | |
4001 | type_decl = TYPE_NAME (t); | |
36a117a5 | 4002 | |
9fbf56f7 MM |
4003 | /* Set up the template information. We have to figure out which |
4004 | template is the immediate parent if this is a full | |
4005 | instantiation. */ | |
4006 | if (parm_depth == 1 || is_partial_instantiation | |
4007 | || !PRIMARY_TEMPLATE_P (template)) | |
4008 | /* This case is easy; there are no member templates involved. */ | |
4009 | found = template; | |
4010 | else | |
4011 | { | |
4012 | /* This is a full instantiation of a member template. There | |
4013 | should be some partial instantiation of which this is an | |
4014 | instance. */ | |
4015 | ||
4016 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
4017 | found; found = TREE_CHAIN (found)) | |
4018 | { | |
4019 | int success; | |
4020 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
4021 | ||
4022 | /* We only want partial instantiations, here, not | |
4023 | specializations or full instantiations. */ | |
4024 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) | |
4025 | || !uses_template_parms (TREE_VALUE (found))) | |
4026 | continue; | |
4027 | ||
4028 | /* Temporarily reduce by one the number of levels in the | |
4029 | ARGLIST and in FOUND so as to avoid comparing the | |
4030 | last set of arguments. */ | |
4031 | TREE_VEC_LENGTH (arglist)--; | |
4032 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
4033 | ||
4034 | /* See if the arguments match. If they do, then TMPL is | |
4035 | the partial instantiation we want. */ | |
4036 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
4037 | ||
4038 | /* Restore the argument vectors to their full size. */ | |
4039 | TREE_VEC_LENGTH (arglist)++; | |
4040 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
4041 | ||
4042 | if (success) | |
4043 | { | |
4044 | found = tmpl; | |
4045 | break; | |
4046 | } | |
4047 | } | |
4048 | ||
4049 | if (!found) | |
4050 | my_friendly_abort (0); | |
4051 | } | |
4052 | ||
5566b478 | 4053 | arglist = copy_to_permanent (arglist); |
ed44da02 | 4054 | SET_TYPE_TEMPLATE_INFO (t, |
9fbf56f7 | 4055 | tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
4056 | DECL_TEMPLATE_INSTANTIATIONS (template) |
4057 | = tree_cons (arglist, t, | |
4058 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
4059 | ||
4060 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
4061 | && !is_partial_instantiation) | |
61a127b3 MM |
4062 | /* Now that the type has been registered on the instantiations |
4063 | list, we set up the enumerators. Because the enumeration | |
4064 | constants may involve the enumeration type itself, we make | |
4065 | sure to register the type first, and then create the | |
4066 | constants. That way, doing tsubst_expr for the enumeration | |
4067 | constants won't result in recursive calls here; we'll find | |
4068 | the instantiation and exit above. */ | |
4069 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 MM |
4070 | |
4071 | /* We're done with the permanent obstack, now. */ | |
4072 | pop_obstacks (); | |
7ddedda4 MM |
4073 | /* We're also done with the momentary allocation we started |
4074 | above. */ | |
4075 | pop_momentary (); | |
5566b478 | 4076 | |
36a117a5 MM |
4077 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
4078 | is set up. */ | |
ed44da02 MM |
4079 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
4080 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
36a117a5 | 4081 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
dbfe2124 | 4082 | if (!is_partial_instantiation) |
36a117a5 MM |
4083 | { |
4084 | DECL_ASSEMBLER_NAME (type_decl) | |
4085 | = get_identifier (build_overload_name (t, 1, 1)); | |
50a6dbd7 JM |
4086 | |
4087 | /* For backwards compatibility; code that uses | |
4088 | -fexternal-templates expects looking up a template to | |
4089 | instantiate it. I think DDD still relies on this. | |
4090 | (jason 8/20/1998) */ | |
ed44da02 MM |
4091 | if (TREE_CODE (t) != ENUMERAL_TYPE |
4092 | && flag_external_templates | |
36a117a5 MM |
4093 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) |
4094 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
4095 | add_pending_template (t); | |
4096 | } | |
4097 | else | |
077e7015 MM |
4098 | /* If the type makes use of template parameters, the |
4099 | code that generates debugging information will crash. */ | |
4100 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 4101 | |
36a117a5 MM |
4102 | return t; |
4103 | } | |
8d08fdba MS |
4104 | } |
4105 | \f | |
050367a3 MM |
4106 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or |
4107 | TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA. | |
4108 | If FN returns non-zero, the iteration is terminated, and | |
4109 | for_each_template_parm returns 1. Otherwise, the iteration | |
4110 | continues. If FN never returns a non-zero value, the value | |
4111 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
4112 | considered to be the function which always returns 1. */ | |
4113 | ||
e9659ab0 | 4114 | static int |
050367a3 | 4115 | for_each_template_parm (t, fn, data) |
8d08fdba | 4116 | tree t; |
050367a3 MM |
4117 | tree_fn_t fn; |
4118 | void* data; | |
8d08fdba MS |
4119 | { |
4120 | if (!t) | |
4121 | return 0; | |
581d38d0 MM |
4122 | |
4123 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't' | |
4124 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data)) | |
4125 | return 1; | |
4126 | ||
8d08fdba MS |
4127 | switch (TREE_CODE (t)) |
4128 | { | |
297e73d8 | 4129 | case ARRAY_REF: |
05e0b2f4 | 4130 | case OFFSET_REF: |
297e73d8 MM |
4131 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) |
4132 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4133 | ||
8d08fdba MS |
4134 | case IDENTIFIER_NODE: |
4135 | if (!IDENTIFIER_TEMPLATE (t)) | |
4136 | return 0; | |
5566b478 | 4137 | my_friendly_abort (42); |
8d08fdba MS |
4138 | |
4139 | /* aggregates of tree nodes */ | |
4140 | case TREE_VEC: | |
4141 | { | |
4142 | int i = TREE_VEC_LENGTH (t); | |
4143 | while (i--) | |
050367a3 | 4144 | if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data)) |
8d08fdba MS |
4145 | return 1; |
4146 | return 0; | |
4147 | } | |
4148 | case TREE_LIST: | |
050367a3 MM |
4149 | if (for_each_template_parm (TREE_PURPOSE (t), fn, data) |
4150 | || for_each_template_parm (TREE_VALUE (t), fn, data)) | |
8d08fdba | 4151 | return 1; |
050367a3 | 4152 | return for_each_template_parm (TREE_CHAIN (t), fn, data); |
8d08fdba | 4153 | |
2c73f9f5 ML |
4154 | case OVERLOAD: |
4155 | if (for_each_template_parm (OVL_FUNCTION (t), fn, data)) | |
4156 | return 1; | |
4157 | return for_each_template_parm (OVL_CHAIN (t), fn, data); | |
4158 | ||
8d08fdba MS |
4159 | /* constructed type nodes */ |
4160 | case POINTER_TYPE: | |
4161 | case REFERENCE_TYPE: | |
050367a3 | 4162 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
ed44da02 | 4163 | |
8d08fdba | 4164 | case RECORD_TYPE: |
b7484fbe | 4165 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
050367a3 MM |
4166 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t), |
4167 | fn, data); | |
ed44da02 MM |
4168 | /* Fall through. */ |
4169 | ||
8d08fdba | 4170 | case UNION_TYPE: |
ed44da02 MM |
4171 | case ENUMERAL_TYPE: |
4172 | if (! TYPE_TEMPLATE_INFO (t)) | |
8d08fdba | 4173 | return 0; |
050367a3 | 4174 | return for_each_template_parm (TREE_VALUE |
ed44da02 | 4175 | (TYPE_TEMPLATE_INFO (t)), |
050367a3 | 4176 | fn, data); |
588c2d1c MM |
4177 | case METHOD_TYPE: |
4178 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data)) | |
8d08fdba | 4179 | return 1; |
588c2d1c MM |
4180 | /* Fall through. */ |
4181 | ||
4182 | case FUNCTION_TYPE: | |
4183 | /* Check the parameter types. Since default arguments are not | |
4184 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4185 | contain expressions that involve template parameters. But, | |
4186 | no-one should be looking at them yet. And, once they're | |
4187 | instantiated, they don't contain template parameters, so | |
4188 | there's no point in looking at them then, either. */ | |
4189 | { | |
4190 | tree parm; | |
4191 | ||
4192 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4193 | if (for_each_template_parm (TREE_VALUE (parm), fn, data)) | |
4194 | return 1; | |
4195 | } | |
4196 | ||
4197 | /* Check the return type, too. */ | |
050367a3 | 4198 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
588c2d1c | 4199 | |
8d08fdba | 4200 | case ARRAY_TYPE: |
050367a3 | 4201 | if (for_each_template_parm (TYPE_DOMAIN (t), fn, data)) |
8d08fdba | 4202 | return 1; |
050367a3 | 4203 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 4204 | case OFFSET_TYPE: |
050367a3 | 4205 | if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data)) |
8d08fdba | 4206 | return 1; |
050367a3 | 4207 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
4208 | |
4209 | /* decl nodes */ | |
4210 | case TYPE_DECL: | |
050367a3 | 4211 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
5566b478 | 4212 | |
73b0fce8 KL |
4213 | case TEMPLATE_DECL: |
4214 | /* A template template parameter is encountered */ | |
4215 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
744fac59 KL |
4216 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
4217 | /* Already substituted template template parameter */ | |
73b0fce8 KL |
4218 | return 0; |
4219 | ||
3ac3d9ea | 4220 | case CONST_DECL: |
050367a3 | 4221 | if (for_each_template_parm (DECL_INITIAL (t), fn, data)) |
3ac3d9ea MM |
4222 | return 1; |
4223 | goto check_type_and_context; | |
4224 | ||
8d08fdba | 4225 | case FUNCTION_DECL: |
5566b478 | 4226 | case VAR_DECL: |
5566b478 | 4227 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
050367a3 | 4228 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data)) |
8d08fdba MS |
4229 | return 1; |
4230 | /* fall through */ | |
8d08fdba | 4231 | case PARM_DECL: |
3ac3d9ea | 4232 | check_type_and_context: |
050367a3 | 4233 | if (for_each_template_parm (TREE_TYPE (t), fn, data)) |
5566b478 | 4234 | return 1; |
050367a3 MM |
4235 | if (DECL_CONTEXT (t) |
4236 | && for_each_template_parm (DECL_CONTEXT (t), fn, data)) | |
8d08fdba | 4237 | return 1; |
8d08fdba MS |
4238 | return 0; |
4239 | ||
4240 | case CALL_EXPR: | |
297e73d8 MM |
4241 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) |
4242 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4243 | ||
8d08fdba | 4244 | case ADDR_EXPR: |
050367a3 | 4245 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
8d08fdba MS |
4246 | |
4247 | /* template parm nodes */ | |
73b0fce8 | 4248 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4249 | /* Record template parameters such as `T' inside `TT<T>'. */ |
7ddedda4 MM |
4250 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t) |
4251 | && for_each_template_parm (TYPE_TI_ARGS (t), fn, data)) | |
744fac59 KL |
4252 | return 1; |
4253 | case TEMPLATE_TYPE_PARM: | |
f84b4be9 | 4254 | case TEMPLATE_PARM_INDEX: |
050367a3 MM |
4255 | if (fn) |
4256 | return (*fn)(t, data); | |
4257 | else | |
4258 | return 1; | |
8d08fdba MS |
4259 | |
4260 | /* simple type nodes */ | |
4261 | case INTEGER_TYPE: | |
050367a3 | 4262 | if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data)) |
8d08fdba | 4263 | return 1; |
050367a3 | 4264 | return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data); |
8d08fdba MS |
4265 | |
4266 | case REAL_TYPE: | |
37c46b43 | 4267 | case COMPLEX_TYPE: |
8d08fdba | 4268 | case VOID_TYPE: |
2986ae00 | 4269 | case BOOLEAN_TYPE: |
2c73f9f5 | 4270 | case NAMESPACE_DECL: |
4699c561 | 4271 | case FIELD_DECL: |
8d08fdba MS |
4272 | return 0; |
4273 | ||
4274 | /* constants */ | |
4275 | case INTEGER_CST: | |
4276 | case REAL_CST: | |
4277 | case STRING_CST: | |
4278 | return 0; | |
4279 | ||
4280 | case ERROR_MARK: | |
4281 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
4282 | my_friendly_assert (t == error_mark_node, 274); | |
4283 | /* NOTREACHED */ | |
4284 | return 0; | |
4285 | ||
61a127b3 MM |
4286 | case PTRMEM_CST: |
4287 | return for_each_template_parm (TREE_TYPE (t), fn, data); | |
4288 | ||
5156628f | 4289 | case SCOPE_REF: |
050367a3 | 4290 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
5156628f | 4291 | |
db5ae43f MS |
4292 | case CONSTRUCTOR: |
4293 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
050367a3 MM |
4294 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4295 | (TREE_TYPE (t)), fn, data); | |
4296 | return for_each_template_parm (TREE_OPERAND (t, 1), fn, data); | |
db5ae43f | 4297 | |
b8865407 MM |
4298 | case SIZEOF_EXPR: |
4299 | case ALIGNOF_EXPR: | |
4300 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); | |
4301 | ||
4699c561 MM |
4302 | case TYPENAME_TYPE: |
4303 | if (!fn) | |
4304 | return 1; | |
4305 | return (for_each_template_parm (TYPE_CONTEXT (t), fn, data) | |
4306 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), | |
4307 | fn, data)); | |
4308 | ||
b8865407 MM |
4309 | case INDIRECT_REF: |
4310 | case COMPONENT_REF: | |
4699c561 | 4311 | /* If there's no type, then this thing must be some expression |
b8865407 | 4312 | involving template parameters. */ |
4699c561 MM |
4313 | if (!fn && !TREE_TYPE (t)) |
4314 | return 1; | |
4315 | if (TREE_CODE (t) == COMPONENT_REF) | |
4316 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) | |
4317 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4318 | else | |
4319 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); | |
b8865407 | 4320 | |
42976354 BK |
4321 | case MODOP_EXPR: |
4322 | case CAST_EXPR: | |
4323 | case REINTERPRET_CAST_EXPR: | |
4324 | case CONST_CAST_EXPR: | |
4325 | case STATIC_CAST_EXPR: | |
4326 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
4327 | case ARROW_EXPR: |
4328 | case DOTSTAR_EXPR: | |
4329 | case TYPEID_EXPR: | |
b8865407 | 4330 | case LOOKUP_EXPR: |
40242ccf | 4331 | case PSEUDO_DTOR_EXPR: |
b8865407 MM |
4332 | if (!fn) |
4333 | return 1; | |
4334 | /* Fall through. */ | |
abff8e06 | 4335 | |
8d08fdba MS |
4336 | default: |
4337 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
4338 | { | |
4339 | case '1': | |
4340 | case '2': | |
ec255269 | 4341 | case 'e': |
8d08fdba MS |
4342 | case '<': |
4343 | { | |
4344 | int i; | |
bbc06b4b | 4345 | for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;) |
050367a3 | 4346 | if (for_each_template_parm (TREE_OPERAND (t, i), fn, data)) |
8d08fdba MS |
4347 | return 1; |
4348 | return 0; | |
4349 | } | |
4350 | default: | |
4351 | break; | |
4352 | } | |
4353 | sorry ("testing %s for template parms", | |
4354 | tree_code_name [(int) TREE_CODE (t)]); | |
4355 | my_friendly_abort (82); | |
4356 | /* NOTREACHED */ | |
4357 | return 0; | |
4358 | } | |
4359 | } | |
4360 | ||
050367a3 MM |
4361 | int |
4362 | uses_template_parms (t) | |
4363 | tree t; | |
4364 | { | |
4365 | return for_each_template_parm (t, 0, 0); | |
4366 | } | |
4367 | ||
27fafc8d JM |
4368 | static struct tinst_level *current_tinst_level; |
4369 | static struct tinst_level *free_tinst_level; | |
4370 | static int tinst_depth; | |
e9f32eb5 | 4371 | extern int max_tinst_depth; |
5566b478 | 4372 | #ifdef GATHER_STATISTICS |
27fafc8d | 4373 | int depth_reached; |
5566b478 | 4374 | #endif |
27fafc8d JM |
4375 | int tinst_level_tick; |
4376 | int last_template_error_tick; | |
8d08fdba | 4377 | |
7bae46f4 | 4378 | /* Print out all the template instantiations that we are currently |
27fafc8d JM |
4379 | working on. If ERR, we are being called from cp_thing, so do |
4380 | the right thing for an error message. */ | |
7bae46f4 | 4381 | |
27fafc8d JM |
4382 | static void |
4383 | print_template_context (err) | |
4384 | int err; | |
7bae46f4 JM |
4385 | { |
4386 | struct tinst_level *p = current_tinst_level; | |
4387 | int line = lineno; | |
4388 | char *file = input_filename; | |
4389 | ||
49e04385 | 4390 | if (err && p) |
27fafc8d | 4391 | { |
49e04385 MM |
4392 | if (current_function_decl != p->decl |
4393 | && current_function_decl != NULL_TREE) | |
4394 | /* We can get here during the processing of some synthesized | |
4395 | method. Then, p->decl will be the function that's causing | |
4396 | the synthesis. */ | |
4397 | ; | |
27fafc8d | 4398 | else |
27fafc8d | 4399 | { |
49e04385 MM |
4400 | if (current_function_decl == p->decl) |
4401 | /* Avoid redundancy with the the "In function" line. */; | |
4402 | else | |
4403 | fprintf (stderr, "%s: In instantiation of `%s':\n", | |
4404 | file, decl_as_string (p->decl, 0)); | |
4405 | ||
038fb86f JM |
4406 | line = p->line; |
4407 | file = p->file; | |
27fafc8d JM |
4408 | p = p->next; |
4409 | } | |
4410 | } | |
4411 | ||
7bae46f4 JM |
4412 | for (; p; p = p->next) |
4413 | { | |
038fb86f JM |
4414 | fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line, |
4415 | decl_as_string (p->decl, 0)); | |
4416 | line = p->line; | |
4417 | file = p->file; | |
7bae46f4 | 4418 | } |
038fb86f | 4419 | fprintf (stderr, "%s:%d: instantiated from here\n", file, line); |
7bae46f4 JM |
4420 | } |
4421 | ||
27fafc8d JM |
4422 | /* Called from cp_thing to print the template context for an error. */ |
4423 | ||
4424 | void | |
4425 | maybe_print_template_context () | |
4426 | { | |
4427 | if (last_template_error_tick == tinst_level_tick | |
4428 | || current_tinst_level == 0) | |
4429 | return; | |
4430 | ||
4431 | last_template_error_tick = tinst_level_tick; | |
4432 | print_template_context (1); | |
4433 | } | |
4434 | ||
bd6dd845 | 4435 | static int |
5566b478 MS |
4436 | push_tinst_level (d) |
4437 | tree d; | |
8d08fdba MS |
4438 | { |
4439 | struct tinst_level *new; | |
8d08fdba | 4440 | |
7215f9a0 MS |
4441 | if (tinst_depth >= max_tinst_depth) |
4442 | { | |
8adf5b5e JM |
4443 | /* If the instantiation in question still has unbound template parms, |
4444 | we don't really care if we can't instantiate it, so just return. | |
4445 | This happens with base instantiation for implicit `typename'. */ | |
4446 | if (uses_template_parms (d)) | |
4447 | return 0; | |
4448 | ||
1139b3d8 | 4449 | last_template_error_tick = tinst_level_tick; |
8251199e | 4450 | error ("template instantiation depth exceeds maximum of %d", |
7215f9a0 | 4451 | max_tinst_depth); |
8251199e JM |
4452 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
4453 | cp_error (" instantiating `%D'", d); | |
5566b478 | 4454 | |
27fafc8d | 4455 | print_template_context (0); |
5566b478 | 4456 | |
7215f9a0 MS |
4457 | return 0; |
4458 | } | |
4459 | ||
8d08fdba MS |
4460 | if (free_tinst_level) |
4461 | { | |
4462 | new = free_tinst_level; | |
4463 | free_tinst_level = new->next; | |
4464 | } | |
4465 | else | |
4466 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
4467 | ||
5566b478 MS |
4468 | new->decl = d; |
4469 | new->line = lineno; | |
4470 | new->file = input_filename; | |
8d08fdba MS |
4471 | new->next = current_tinst_level; |
4472 | current_tinst_level = new; | |
5566b478 | 4473 | |
7215f9a0 | 4474 | ++tinst_depth; |
5566b478 MS |
4475 | #ifdef GATHER_STATISTICS |
4476 | if (tinst_depth > depth_reached) | |
4477 | depth_reached = tinst_depth; | |
4478 | #endif | |
4479 | ||
27fafc8d | 4480 | ++tinst_level_tick; |
7215f9a0 | 4481 | return 1; |
8d08fdba MS |
4482 | } |
4483 | ||
4484 | void | |
4485 | pop_tinst_level () | |
4486 | { | |
4487 | struct tinst_level *old = current_tinst_level; | |
4488 | ||
ae58fa02 MM |
4489 | /* Restore the filename and line number stashed away when we started |
4490 | this instantiation. */ | |
4491 | lineno = old->line; | |
4492 | input_filename = old->file; | |
5f2c99c4 | 4493 | extract_interface_info (); |
ae58fa02 | 4494 | |
8d08fdba MS |
4495 | current_tinst_level = old->next; |
4496 | old->next = free_tinst_level; | |
4497 | free_tinst_level = old; | |
7215f9a0 | 4498 | --tinst_depth; |
27fafc8d | 4499 | ++tinst_level_tick; |
8d08fdba MS |
4500 | } |
4501 | ||
4502 | struct tinst_level * | |
4503 | tinst_for_decl () | |
4504 | { | |
4505 | struct tinst_level *p = current_tinst_level; | |
4506 | ||
4507 | if (p) | |
4508 | for (; p->next ; p = p->next ) | |
4509 | ; | |
4510 | return p; | |
4511 | } | |
4512 | ||
f84b4be9 JM |
4513 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
4514 | vector of template arguments, as for tsubst. | |
4515 | ||
4516 | Returns an appropriate tsbust'd friend declaration. */ | |
4517 | ||
4518 | static tree | |
4519 | tsubst_friend_function (decl, args) | |
4520 | tree decl; | |
4521 | tree args; | |
4522 | { | |
4523 | tree new_friend; | |
27fafc8d JM |
4524 | int line = lineno; |
4525 | char *file = input_filename; | |
4526 | ||
4527 | lineno = DECL_SOURCE_LINE (decl); | |
4528 | input_filename = DECL_SOURCE_FILE (decl); | |
4529 | ||
f84b4be9 JM |
4530 | if (TREE_CODE (decl) == FUNCTION_DECL |
4531 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
4532 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
4533 | /* This was a friend declared with an explicit template | |
4534 | argument list, e.g.: | |
4535 | ||
4536 | friend void f<>(T); | |
4537 | ||
4538 | to indicate that f was a template instantiation, not a new | |
4539 | function declaration. Now, we have to figure out what | |
4540 | instantiation of what template. */ | |
4541 | { | |
4542 | tree template_id; | |
4543 | tree new_args; | |
4544 | tree tmpl; | |
f84b4be9 JM |
4545 | |
4546 | template_id | |
4547 | = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl), | |
4393e105 MM |
4548 | args, /*complain=*/1, |
4549 | NULL_TREE), | |
f84b4be9 | 4550 | tsubst (DECL_TI_ARGS (decl), |
4393e105 MM |
4551 | args, /*complain=*/1, |
4552 | NULL_TREE)); | |
39c01e4c MM |
4553 | /* FIXME: The decl we create via the next tsubst could be |
4554 | created on a temporary obstack. */ | |
4393e105 | 4555 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
4556 | tmpl = determine_specialization (template_id, new_friend, |
4557 | &new_args, | |
bf8f3f93 | 4558 | /*need_member_template=*/0); |
27fafc8d JM |
4559 | new_friend = instantiate_template (tmpl, new_args); |
4560 | goto done; | |
f84b4be9 | 4561 | } |
36a117a5 | 4562 | |
4393e105 | 4563 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
f84b4be9 | 4564 | |
36a117a5 | 4565 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
4566 | compiler, but is not an instantiation from the point of view of |
4567 | the language. For example, we might have had: | |
4568 | ||
4569 | template <class T> struct S { | |
4570 | template <class U> friend void f(T, U); | |
4571 | }; | |
4572 | ||
4573 | Then, in S<int>, template <class U> void f(int, U) is not an | |
4574 | instantiation of anything. */ | |
4575 | DECL_USE_TEMPLATE (new_friend) = 0; | |
4576 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
4577 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
36a117a5 MM |
4578 | |
4579 | /* The mangled name for the NEW_FRIEND is incorrect. The call to | |
4580 | tsubst will have resulted in a call to | |
4581 | set_mangled_name_for_template_decl. But, the function is not a | |
4582 | template instantiation and should not be mangled like one. | |
4583 | Therefore, we remangle the function name. We don't have to do | |
4584 | this if the NEW_FRIEND is a template since | |
4585 | set_mangled_name_for_template_decl doesn't do anything if the | |
4586 | function declaration still uses template arguments. */ | |
4587 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) | |
4588 | { | |
4589 | set_mangled_name_for_decl (new_friend); | |
4590 | DECL_RTL (new_friend) = 0; | |
4591 | make_decl_rtl (new_friend, NULL_PTR, 1); | |
4592 | } | |
4593 | ||
6eb3bb27 | 4594 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 4595 | { |
36a117a5 | 4596 | tree old_decl; |
fbf1c34b MM |
4597 | tree new_friend_template_info; |
4598 | tree new_friend_result_template_info; | |
92da7074 | 4599 | tree ns; |
fbf1c34b MM |
4600 | int new_friend_is_defn; |
4601 | ||
4602 | /* We must save some information from NEW_FRIEND before calling | |
4603 | duplicate decls since that function will free NEW_FRIEND if | |
4604 | possible. */ | |
4605 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
f84b4be9 | 4606 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
4607 | { |
4608 | /* This declaration is a `primary' template. */ | |
4609 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
4610 | ||
4611 | new_friend_is_defn | |
4612 | = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE; | |
4613 | new_friend_result_template_info | |
4614 | = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend)); | |
4615 | } | |
4616 | else | |
4617 | { | |
4618 | new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; | |
4619 | new_friend_result_template_info = NULL_TREE; | |
4620 | } | |
36a117a5 | 4621 | |
92da7074 ML |
4622 | /* Inside pushdecl_namespace_level, we will push into the |
4623 | current namespace. However, the friend function should | |
4624 | tyically go into the namespace of the template. */ | |
4625 | ns = decl_namespace_context (new_friend); | |
4626 | push_nested_namespace (ns); | |
36a117a5 | 4627 | old_decl = pushdecl_namespace_level (new_friend); |
92da7074 | 4628 | pop_nested_namespace (ns); |
36a117a5 MM |
4629 | |
4630 | if (old_decl != new_friend) | |
4631 | { | |
4632 | /* This new friend declaration matched an existing | |
4633 | declaration. For example, given: | |
4634 | ||
4635 | template <class T> void f(T); | |
4636 | template <class U> class C { | |
4637 | template <class T> friend void f(T) {} | |
4638 | }; | |
4639 | ||
4640 | the friend declaration actually provides the definition | |
4641 | of `f', once C has been instantiated for some type. So, | |
4642 | old_decl will be the out-of-class template declaration, | |
4643 | while new_friend is the in-class definition. | |
4644 | ||
4645 | But, if `f' was called before this point, the | |
4646 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
4647 | to `T' but not to `U', references to which might appear | |
4648 | in the definition of `f'. Previously, the most general | |
4649 | template for an instantiation of `f' was the out-of-class | |
4650 | version; now it is the in-class version. Therefore, we | |
4651 | run through all specialization of `f', adding to their | |
4652 | DECL_TI_ARGS appropriately. In particular, they need a | |
4653 | new set of outer arguments, corresponding to the | |
4654 | arguments for this class instantiation. | |
4655 | ||
4656 | The same situation can arise with something like this: | |
4657 | ||
4658 | friend void f(int); | |
4659 | template <class T> class C { | |
4660 | friend void f(T) {} | |
4661 | }; | |
4662 | ||
4663 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
4664 | in the class. */ | |
4665 | ||
fbf1c34b MM |
4666 | if (!new_friend_is_defn) |
4667 | /* On the other hand, if the in-class declaration does | |
4668 | *not* provide a definition, then we don't want to alter | |
4669 | existing definitions. We can just leave everything | |
4670 | alone. */ | |
36a117a5 | 4671 | ; |
fbf1c34b | 4672 | else |
36a117a5 | 4673 | { |
fbf1c34b MM |
4674 | /* Overwrite whatever template info was there before, if |
4675 | any, with the new template information pertaining to | |
4676 | the declaration. */ | |
4677 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
4678 | ||
4679 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
4680 | /* duplicate_decls will take care of this case. */ | |
4681 | ; | |
4682 | else | |
36a117a5 | 4683 | { |
fbf1c34b MM |
4684 | tree t; |
4685 | tree new_friend_args; | |
4686 | ||
4687 | DECL_TEMPLATE_INFO (DECL_RESULT (old_decl)) | |
4688 | = new_friend_result_template_info; | |
4689 | ||
4690 | new_friend_args = TI_ARGS (new_friend_template_info); | |
4691 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
4692 | t != NULL_TREE; | |
4693 | t = TREE_CHAIN (t)) | |
4694 | { | |
4695 | tree spec = TREE_VALUE (t); | |
36a117a5 | 4696 | |
fbf1c34b MM |
4697 | DECL_TI_ARGS (spec) |
4698 | = add_outermost_template_args (new_friend_args, | |
4699 | DECL_TI_ARGS (spec)); | |
4700 | DECL_TI_ARGS (spec) | |
4701 | = copy_to_permanent (DECL_TI_ARGS (spec)); | |
4702 | } | |
4703 | ||
4704 | /* Now, since specializations are always supposed to | |
4705 | hang off of the most general template, we must move | |
4706 | them. */ | |
4707 | t = most_general_template (old_decl); | |
4708 | if (t != old_decl) | |
4709 | { | |
4710 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
4711 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
4712 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
4713 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
4714 | } | |
36a117a5 MM |
4715 | } |
4716 | } | |
4717 | ||
4718 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
4719 | by duplicate_decls. */ | |
4720 | new_friend = old_decl; | |
4721 | } | |
f84b4be9 JM |
4722 | } |
4723 | else if (TYPE_SIZE (DECL_CONTEXT (new_friend))) | |
4724 | { | |
4725 | /* Check to see that the declaration is really present, and, | |
4726 | possibly obtain an improved declaration. */ | |
4727 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
4728 | new_friend); | |
4729 | ||
4730 | if (fn) | |
4731 | new_friend = fn; | |
4732 | } | |
4733 | ||
27fafc8d JM |
4734 | done: |
4735 | lineno = line; | |
4736 | input_filename = file; | |
f84b4be9 JM |
4737 | return new_friend; |
4738 | } | |
4739 | ||
1aed5355 MM |
4740 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
4741 | template arguments, as for tsubst. | |
6757edfe MM |
4742 | |
4743 | Returns an appropriate tsbust'd friend type. */ | |
4744 | ||
4745 | static tree | |
1aed5355 MM |
4746 | tsubst_friend_class (friend_tmpl, args) |
4747 | tree friend_tmpl; | |
6757edfe MM |
4748 | tree args; |
4749 | { | |
1aed5355 | 4750 | tree friend_type; |
25aab5d0 | 4751 | tree tmpl; |
6757edfe | 4752 | |
25aab5d0 MM |
4753 | /* First, we look for a class template. */ |
4754 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); | |
4755 | ||
4756 | /* But, if we don't find one, it might be because we're in a | |
4757 | situation like this: | |
4758 | ||
4759 | template <class T> | |
4760 | struct S { | |
4761 | template <class U> | |
4762 | friend struct S; | |
4763 | }; | |
4764 | ||
4765 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
4766 | for `S<int>', not the TEMPLATE_DECL. */ | |
bc639f90 | 4767 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) |
25aab5d0 MM |
4768 | { |
4769 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); | |
4770 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
4771 | } | |
6757edfe | 4772 | |
25aab5d0 | 4773 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
4774 | { |
4775 | /* The friend template has already been declared. Just | |
36a117a5 MM |
4776 | check to see that the declarations match, and install any new |
4777 | default parameters. We must tsubst the default parameters, | |
4778 | of course. We only need the innermost template parameters | |
4779 | because that is all that redeclare_class_template will look | |
4780 | at. */ | |
4781 | tree parms | |
4782 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
4393e105 | 4783 | args, /*complain=*/1); |
36a117a5 | 4784 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
6757edfe MM |
4785 | friend_type = TREE_TYPE (tmpl); |
4786 | } | |
4787 | else | |
4788 | { | |
4789 | /* The friend template has not already been declared. In this | |
4790 | case, the instantiation of the template class will cause the | |
4791 | injection of this template into the global scope. */ | |
4393e105 | 4792 | tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE); |
6757edfe MM |
4793 | |
4794 | /* The new TMPL is not an instantiation of anything, so we | |
4795 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
4796 | the new type because that is supposed to be the corresponding | |
4797 | template decl, i.e., TMPL. */ | |
4798 | DECL_USE_TEMPLATE (tmpl) = 0; | |
4799 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
4800 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
4801 | ||
4802 | /* Inject this template into the global scope. */ | |
4803 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
4804 | } | |
4805 | ||
4806 | return friend_type; | |
4807 | } | |
f84b4be9 | 4808 | |
8d08fdba | 4809 | tree |
5566b478 MS |
4810 | instantiate_class_template (type) |
4811 | tree type; | |
8d08fdba | 4812 | { |
61a127b3 | 4813 | tree template, args, pattern, t; |
36a117a5 | 4814 | tree typedecl; |
8d08fdba | 4815 | |
5566b478 | 4816 | if (type == error_mark_node) |
8d08fdba MS |
4817 | return error_mark_node; |
4818 | ||
5566b478 MS |
4819 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) |
4820 | return type; | |
4821 | ||
7ddedda4 MM |
4822 | /* We want to allocate temporary vectors of template arguments and |
4823 | template argument expressions on the momentary obstack, not on | |
4824 | the expression obstack. Otherwise, all the space allocated in | |
4825 | argument coercion and such is simply lost. */ | |
4826 | push_momentary (); | |
4827 | ||
6bdb985f | 4828 | /* Figure out which template is being instantiated. */ |
36a117a5 | 4829 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
5566b478 | 4830 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); |
73aad9b9 | 4831 | |
6bdb985f MM |
4832 | /* Figure out which arguments are being used to do the |
4833 | instantiation. */ | |
4834 | args = CLASSTYPE_TI_ARGS (type); | |
4c571114 | 4835 | PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args); |
6bdb985f | 4836 | |
4c571114 MM |
4837 | if (pedantic && PARTIAL_INSTANTIATION_P (type)) |
4838 | /* If this is a partial instantiation, then we can't instantiate | |
4839 | the type; there's no telling whether or not one of the | |
4840 | template parameters might eventually be instantiated to some | |
4841 | value that results in a specialization being used. For | |
4842 | example, consider: | |
4843 | ||
4844 | template <class T> | |
4845 | struct S {}; | |
4846 | ||
4847 | template <class U> | |
4848 | void f(S<U>); | |
4849 | ||
4850 | template <> | |
4851 | struct S<int> {}; | |
4852 | ||
4853 | Now, the `S<U>' in `f<int>' is the specialization, not an | |
5db698f6 | 4854 | instantiation of the original template. */ |
4c571114 MM |
4855 | goto end; |
4856 | ||
4857 | /* Determine what specialization of the original template to | |
4858 | instantiate. */ | |
4859 | if (PARTIAL_INSTANTIATION_P (type)) | |
6bdb985f MM |
4860 | /* There's no telling which specialization is appropriate at this |
4861 | point. Since all peeking at the innards of this partial | |
4862 | instantiation are extensions (like the "implicit typename" | |
4863 | extension, which allows users to omit the keyword `typename' on | |
4864 | names that are declared as types in template base classes), we | |
4865 | are free to do what we please. | |
4866 | ||
4867 | Trying to figure out which partial instantiation to use can | |
4868 | cause a crash. (Some of the template arguments don't even have | |
4869 | types.) So, we just use the most general version. */ | |
4870 | t = NULL_TREE; | |
4871 | else | |
73aad9b9 | 4872 | { |
6bdb985f MM |
4873 | t = most_specialized_class (template, args); |
4874 | ||
4875 | if (t == error_mark_node) | |
73aad9b9 | 4876 | { |
d8e178a0 | 4877 | const char *str = "candidates are:"; |
6bdb985f MM |
4878 | cp_error ("ambiguous class template instantiation for `%#T'", type); |
4879 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; | |
4880 | t = TREE_CHAIN (t)) | |
73aad9b9 | 4881 | { |
6bdb985f MM |
4882 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
4883 | args)) | |
4884 | { | |
4885 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
4886 | str = " "; | |
4887 | } | |
73aad9b9 | 4888 | } |
6bdb985f MM |
4889 | TYPE_BEING_DEFINED (type) = 1; |
4890 | type = error_mark_node; | |
4891 | goto end; | |
73aad9b9 | 4892 | } |
73aad9b9 | 4893 | } |
6bdb985f MM |
4894 | |
4895 | if (t) | |
73aad9b9 JM |
4896 | pattern = TREE_TYPE (t); |
4897 | else | |
4898 | pattern = TREE_TYPE (template); | |
5566b478 | 4899 | |
4c571114 MM |
4900 | /* If the template we're instantiating is incomplete, then clearly |
4901 | there's nothing we can do. */ | |
5566b478 | 4902 | if (TYPE_SIZE (pattern) == NULL_TREE) |
7ddedda4 | 4903 | goto end; |
5566b478 | 4904 | |
4c571114 MM |
4905 | /* If this is a partial instantiation, don't tsubst anything. We will |
4906 | only use this type for implicit typename, so the actual contents don't | |
4907 | matter. All that matters is whether a particular name is a type. */ | |
4908 | if (PARTIAL_INSTANTIATION_P (type)) | |
4909 | { | |
4910 | /* The fields set here must be kept in sync with those cleared | |
4911 | in begin_class_definition. */ | |
4912 | TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); | |
4913 | TYPE_FIELDS (type) = TYPE_FIELDS (pattern); | |
4914 | TYPE_METHODS (type) = TYPE_METHODS (pattern); | |
4915 | CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern); | |
4916 | /* Pretend that the type is complete, so that we will look | |
4917 | inside it during name lookup and such. */ | |
4918 | TYPE_SIZE (type) = integer_zero_node; | |
4919 | goto end; | |
4920 | } | |
4921 | ||
4922 | /* If we've recursively instantiated too many templates, stop. */ | |
4923 | if (! push_tinst_level (type)) | |
4924 | goto end; | |
4925 | ||
4926 | /* Now we're really doing the instantiation. Mark the type as in | |
4927 | the process of being defined. */ | |
4928 | TYPE_BEING_DEFINED (type) = 1; | |
4929 | ||
4930 | maybe_push_to_top_level (uses_template_parms (type)); | |
4c571114 | 4931 | |
73aad9b9 | 4932 | if (t) |
36a117a5 MM |
4933 | { |
4934 | /* This TYPE is actually a instantiation of of a partial | |
4935 | specialization. We replace the innermost set of ARGS with | |
4936 | the arguments appropriate for substitution. For example, | |
4937 | given: | |
4938 | ||
4939 | template <class T> struct S {}; | |
4940 | template <class T> struct S<T*> {}; | |
4941 | ||
4942 | and supposing that we are instantiating S<int*>, ARGS will | |
4943 | present be {int*} but we need {int}. */ | |
4944 | tree inner_args | |
4945 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
4946 | args); | |
4947 | ||
4948 | /* If there were multiple levels in ARGS, replacing the | |
4949 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
4950 | want, so we make a copy first. */ | |
4951 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
4952 | { | |
4953 | args = copy_node (args); | |
4954 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
4955 | } | |
4956 | else | |
4957 | args = inner_args; | |
4958 | } | |
8d019cef | 4959 | |
5566b478 MS |
4960 | if (flag_external_templates) |
4961 | { | |
4962 | if (flag_alt_external_templates) | |
4963 | { | |
4964 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
4965 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
4966 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4967 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4968 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4969 | } |
4970 | else | |
4971 | { | |
4972 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
4973 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
4974 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
4975 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4976 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4977 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4978 | } |
4979 | } | |
4980 | else | |
8d08fdba | 4981 | { |
5566b478 MS |
4982 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
4983 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
4984 | } |
4985 | ||
f7da6097 MS |
4986 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
4987 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
f7da6097 MS |
4988 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); |
4989 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
4990 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
4991 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
4992 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
4993 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
4994 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
4995 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
4996 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
4997 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
4998 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
4999 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
5000 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
5001 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
5002 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
5003 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
5004 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
5005 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
5006 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
5007 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
5008 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
eff71ab0 | 5009 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
5010 | if (ANON_AGGR_TYPE_P (pattern)) |
5011 | SET_ANON_AGGR_TYPE_P (type); | |
f7da6097 | 5012 | |
4c571114 MM |
5013 | /* We must copy the arguments to the permanent obstack since |
5014 | during the tsubst'ing below they may wind up in the | |
5015 | DECL_TI_ARGS of some instantiated member template. */ | |
5016 | args = copy_to_permanent (args); | |
2604412d | 5017 | |
3fd71a52 MM |
5018 | if (TYPE_BINFO_BASETYPES (pattern)) |
5019 | { | |
5020 | tree base_list = NULL_TREE; | |
5021 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
5022 | int i; | |
5566b478 | 5023 | |
3fd71a52 MM |
5024 | /* Substitute into each of the bases to determine the actual |
5025 | basetypes. */ | |
5026 | for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) | |
5027 | { | |
5028 | tree base; | |
5029 | tree access; | |
5030 | tree pbase; | |
5566b478 | 5031 | |
3fd71a52 | 5032 | pbase = TREE_VEC_ELT (pbases, i); |
711734a9 | 5033 | |
3fd71a52 MM |
5034 | /* Substitue to figure out the base class. */ |
5035 | base = tsubst (BINFO_TYPE (pbase), args, | |
5036 | /*complain=*/1, NULL_TREE); | |
5037 | if (base == error_mark_node) | |
5038 | continue; | |
711734a9 | 5039 | |
3fd71a52 MM |
5040 | /* Calculate the correct access node. */ |
5041 | if (TREE_VIA_VIRTUAL (pbase)) | |
5042 | { | |
5043 | if (TREE_VIA_PUBLIC (pbase)) | |
5044 | access = access_public_virtual_node; | |
5045 | else if (TREE_VIA_PROTECTED (pbase)) | |
5046 | access = access_protected_virtual_node; | |
d6479fe7 | 5047 | else |
3fd71a52 MM |
5048 | access = access_private_virtual_node; |
5049 | } | |
5050 | else | |
5051 | { | |
5052 | if (TREE_VIA_PUBLIC (pbase)) | |
5053 | access = access_public_node; | |
5054 | else if (TREE_VIA_PROTECTED (pbase)) | |
5055 | access = access_protected_node; | |
d6479fe7 | 5056 | else |
3fd71a52 MM |
5057 | access = access_private_node; |
5058 | } | |
dfbcd65a | 5059 | |
3fd71a52 MM |
5060 | base_list = tree_cons (access, base, base_list); |
5061 | } | |
dfbcd65a | 5062 | |
3fd71a52 MM |
5063 | /* The list is now in reverse order; correct that. */ |
5064 | base_list = nreverse (base_list); | |
5065 | ||
5066 | /* Now call xref_basetypes to set up all the base-class | |
5067 | information. */ | |
5068 | xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE | |
5069 | ? (CLASSTYPE_DECLARED_CLASS (pattern) | |
5070 | ? class_type_node : record_type_node) | |
5071 | : union_type_node, | |
5072 | DECL_NAME (TYPE_NAME (pattern)), | |
5073 | type, | |
5074 | base_list); | |
5075 | } | |
5566b478 | 5076 | |
b74a0560 MM |
5077 | /* Now that our base classes are set up, enter the scope of the |
5078 | class, so that name lookups into base classes, etc. will work | |
5079 | corectly. This is precisely analagous to what we do in | |
5080 | begin_class_definition when defining an ordinary non-template | |
5081 | class. */ | |
5082 | pushclass (type, 1); | |
5083 | ||
5566b478 | 5084 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) |
8d08fdba | 5085 | { |
5566b478 | 5086 | tree tag = TREE_VALUE (t); |
36a117a5 MM |
5087 | tree name = TYPE_IDENTIFIER (tag); |
5088 | tree newtag; | |
5566b478 | 5089 | |
4393e105 | 5090 | newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE); |
61a127b3 | 5091 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
ed44da02 | 5092 | { |
486e4077 MM |
5093 | if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) |
5094 | /* Unfortunately, lookup_template_class sets | |
5095 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
5096 | instantiation (i.e., for the type of a member template | |
5097 | class nested within a template class.) This behavior is | |
5098 | required for maybe_process_partial_specialization to work | |
5099 | correctly, but is not accurate in this case; the TAG is not | |
5100 | an instantiation of anything. (The corresponding | |
5101 | TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ | |
5102 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; | |
5103 | ||
ed44da02 MM |
5104 | /* Now, we call pushtag to put this NEWTAG into the scope of |
5105 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
5106 | pushtag calling push_template_decl. We don't have to do | |
5107 | this for enums because it will already have been done in | |
5108 | tsubst_enum. */ | |
5109 | if (name) | |
5110 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
5111 | pushtag (name, newtag, /*globalize=*/0); | |
5112 | } | |
8d08fdba MS |
5113 | } |
5114 | ||
5566b478 MS |
5115 | /* Don't replace enum constants here. */ |
5116 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
8dd3f57a | 5117 | if (TREE_CODE (t) != CONST_DECL) |
5566b478 | 5118 | { |
ae58fa02 MM |
5119 | tree r; |
5120 | ||
5121 | /* The the file and line for this declaration, to assist in | |
5122 | error message reporting. Since we called push_tinst_level | |
5123 | above, we don't need to restore these. */ | |
5124 | lineno = DECL_SOURCE_LINE (t); | |
5125 | input_filename = DECL_SOURCE_FILE (t); | |
5126 | ||
4393e105 | 5127 | r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
5566b478 MS |
5128 | if (TREE_CODE (r) == VAR_DECL) |
5129 | { | |
fa8d6e85 MM |
5130 | tree init; |
5131 | ||
94c82a77 | 5132 | if (DECL_DEFINED_IN_CLASS_P (r)) |
fa8d6e85 MM |
5133 | init = tsubst_expr (DECL_INITIAL (t), args, |
5134 | /*complain=*/1, NULL_TREE); | |
5135 | else | |
5136 | init = NULL_TREE; | |
5137 | ||
5138 | finish_static_data_member_decl (r, init, | |
5139 | /*asmspec_tree=*/NULL_TREE, | |
5140 | /*need_pop=*/0, | |
5141 | /*flags=*/0); | |
5142 | ||
6ba89f8e MM |
5143 | if (DECL_DEFINED_IN_CLASS_P (r)) |
5144 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5566b478 | 5145 | } |
61a127b3 MM |
5146 | |
5147 | /* R will have a TREE_CHAIN if and only if it has already been | |
5148 | processed by finish_member_declaration. This can happen | |
5149 | if, for example, it is a TYPE_DECL for a class-scoped | |
5150 | ENUMERAL_TYPE; such a thing will already have been added to | |
5151 | the field list by tsubst_enum above. */ | |
5152 | if (!TREE_CHAIN (r)) | |
5153 | { | |
5154 | set_current_access_from_decl (r); | |
5155 | finish_member_declaration (r); | |
5156 | } | |
5566b478 | 5157 | } |
8d08fdba | 5158 | |
61a127b3 MM |
5159 | /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC) |
5160 | for this instantiation. */ | |
5161 | for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t)) | |
5162 | { | |
4393e105 | 5163 | tree r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
61a127b3 MM |
5164 | set_current_access_from_decl (r); |
5165 | finish_member_declaration (r); | |
5166 | } | |
8d08fdba | 5167 | |
2604412d JM |
5168 | /* Construct the DECL_FRIENDLIST for the new class type. */ |
5169 | typedecl = TYPE_MAIN_DECL (type); | |
5170 | for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)); | |
5171 | t != NULL_TREE; | |
5172 | t = TREE_CHAIN (t)) | |
f84b4be9 | 5173 | { |
2604412d | 5174 | tree friends; |
f84b4be9 | 5175 | |
2604412d JM |
5176 | for (friends = TREE_VALUE (t); |
5177 | friends != NULL_TREE; | |
5178 | friends = TREE_CHAIN (friends)) | |
0c0aac2f MM |
5179 | if (TREE_PURPOSE (friends) == error_mark_node) |
5180 | add_friend (type, | |
5181 | tsubst_friend_function (TREE_VALUE (friends), | |
5182 | args)); | |
5183 | else | |
5184 | add_friends (type, | |
5185 | tsubst_copy (TREE_PURPOSE (t), args, | |
5186 | /*complain=*/1, NULL_TREE), | |
5187 | tsubst (TREE_PURPOSE (friends), args, | |
5188 | /*complain=*/1, NULL_TREE)); | |
2604412d | 5189 | } |
5566b478 | 5190 | |
6757edfe MM |
5191 | for (t = CLASSTYPE_FRIEND_CLASSES (pattern); |
5192 | t != NULL_TREE; | |
5193 | t = TREE_CHAIN (t)) | |
5194 | { | |
5195 | tree friend_type = TREE_VALUE (t); | |
1aed5355 | 5196 | tree new_friend_type; |
6757edfe | 5197 | |
36a117a5 MM |
5198 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5199 | new_friend_type = tsubst_friend_class (friend_type, args); | |
5200 | else if (uses_template_parms (friend_type)) | |
4393e105 MM |
5201 | new_friend_type = tsubst (friend_type, args, /*complain=*/1, |
5202 | NULL_TREE); | |
36a117a5 | 5203 | else |
61fc8c9e MM |
5204 | { |
5205 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); | |
36a117a5 | 5206 | |
61fc8c9e MM |
5207 | /* The call to xref_tag_from_type does injection for friend |
5208 | classes. */ | |
5209 | push_nested_namespace (ns); | |
5210 | new_friend_type = | |
5211 | xref_tag_from_type (friend_type, NULL_TREE, 1); | |
5212 | pop_nested_namespace (ns); | |
5213 | } | |
1aed5355 MM |
5214 | |
5215 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5216 | /* Trick make_friend_class into realizing that the friend | |
5217 | we're adding is a template, not an ordinary class. It's | |
5218 | important that we use make_friend_class since it will | |
5219 | perform some error-checking and output cross-reference | |
5220 | information. */ | |
5221 | ++processing_template_decl; | |
5222 | ||
5223 | make_friend_class (type, new_friend_type); | |
fc378698 | 5224 | |
1aed5355 MM |
5225 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5226 | --processing_template_decl; | |
6757edfe | 5227 | } |
fc378698 | 5228 | |
2604412d JM |
5229 | for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
5230 | if (TREE_CODE (t) == FIELD_DECL) | |
5231 | { | |
5232 | TREE_TYPE (t) = complete_type (TREE_TYPE (t)); | |
5233 | require_complete_type (t); | |
5234 | } | |
5566b478 | 5235 | |
61a127b3 MM |
5236 | /* Set the file and line number information to whatever is given for |
5237 | the class itself. This puts error messages involving generated | |
5238 | implicit functions at a predictable point, and the same point | |
5239 | that would be used for non-template classes. */ | |
5240 | lineno = DECL_SOURCE_LINE (typedecl); | |
5241 | input_filename = DECL_SOURCE_FILE (typedecl); | |
5242 | ||
5243 | unreverse_member_declarations (type); | |
9f33663b | 5244 | finish_struct_1 (type); |
2604412d | 5245 | CLASSTYPE_GOT_SEMICOLON (type) = 1; |
e92cc029 | 5246 | |
5524676d JM |
5247 | /* Clear this now so repo_template_used is happy. */ |
5248 | TYPE_BEING_DEFINED (type) = 0; | |
2604412d | 5249 | repo_template_used (type); |
8d08fdba | 5250 | |
9188c363 MM |
5251 | /* Now that the class is complete, instantiate default arguments for |
5252 | any member functions. We don't do this earlier because the | |
5253 | default arguments may reference members of the class. */ | |
5254 | if (!PRIMARY_TEMPLATE_P (template)) | |
5255 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
5256 | if (TREE_CODE (t) == FUNCTION_DECL | |
5257 | /* Implicitly generated member functions will not have tmplate | |
5258 | information; they are not instantiations, but instead are | |
5259 | created "fresh" for each instantiation. */ | |
5260 | && DECL_TEMPLATE_INFO (t)) | |
5261 | tsubst_default_arguments (t); | |
5262 | ||
b74a0560 | 5263 | popclass (); |
5566b478 MS |
5264 | pop_from_top_level (); |
5265 | pop_tinst_level (); | |
5266 | ||
7ddedda4 MM |
5267 | end: |
5268 | pop_momentary (); | |
5269 | ||
5566b478 | 5270 | return type; |
8d08fdba MS |
5271 | } |
5272 | ||
5273 | static int | |
5274 | list_eq (t1, t2) | |
5275 | tree t1, t2; | |
5276 | { | |
5277 | if (t1 == NULL_TREE) | |
5278 | return t2 == NULL_TREE; | |
5279 | if (t2 == NULL_TREE) | |
5280 | return 0; | |
5281 | /* Don't care if one declares its arg const and the other doesn't -- the | |
5282 | main variant of the arg type is all that matters. */ | |
5283 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
5284 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
5285 | return 0; | |
5286 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
5287 | } | |
5288 | ||
00d3396f JM |
5289 | /* If arg is a non-type template parameter that does not depend on template |
5290 | arguments, fold it like we weren't in the body of a template. */ | |
5291 | ||
5292 | static tree | |
5293 | maybe_fold_nontype_arg (arg) | |
5294 | tree arg; | |
5295 | { | |
5296 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
5297 | && !uses_template_parms (arg)) | |
5298 | { | |
5299 | /* Sometimes, one of the args was an expression involving a | |
5300 | template constant parameter, like N - 1. Now that we've | |
5301 | tsubst'd, we might have something like 2 - 1. This will | |
5302 | confuse lookup_template_class, so we do constant folding | |
5303 | here. We have to unset processing_template_decl, to | |
5304 | fool build_expr_from_tree() into building an actual | |
5305 | tree. */ | |
5306 | ||
5307 | int saved_processing_template_decl = processing_template_decl; | |
5308 | processing_template_decl = 0; | |
5309 | arg = fold (build_expr_from_tree (arg)); | |
5310 | processing_template_decl = saved_processing_template_decl; | |
5311 | } | |
5312 | return arg; | |
5313 | } | |
5314 | ||
a221d52f | 5315 | /* Return the TREE_VEC with the arguments for the innermost template header, |
36a117a5 MM |
5316 | where ARGS is either that or the VEC of VECs for all the |
5317 | arguments. */ | |
a221d52f JM |
5318 | |
5319 | tree | |
36a117a5 | 5320 | innermost_args (args) |
a221d52f | 5321 | tree args; |
a221d52f | 5322 | { |
36a117a5 | 5323 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
a221d52f | 5324 | } |
f84b4be9 | 5325 | |
830bfa74 MM |
5326 | /* Substitute ARGS into the vector of template arguments T. */ |
5327 | ||
e9659ab0 | 5328 | static tree |
4393e105 | 5329 | tsubst_template_arg_vector (t, args, complain) |
830bfa74 MM |
5330 | tree t; |
5331 | tree args; | |
4393e105 | 5332 | int complain; |
830bfa74 MM |
5333 | { |
5334 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
5335 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5336 | ||
5337 | bzero ((char *) elts, len * sizeof (tree)); | |
5338 | ||
5339 | for (i = 0; i < len; i++) | |
5340 | { | |
5341 | if (TREE_VEC_ELT (t, i) != NULL_TREE | |
5342 | && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) | |
4393e105 MM |
5343 | elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), |
5344 | args, complain); | |
830bfa74 MM |
5345 | else |
5346 | elts[i] = maybe_fold_nontype_arg | |
4393e105 MM |
5347 | (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, |
5348 | NULL_TREE)); | |
830bfa74 MM |
5349 | |
5350 | if (elts[i] != TREE_VEC_ELT (t, i)) | |
5351 | need_new = 1; | |
5352 | } | |
5353 | ||
5354 | if (!need_new) | |
5355 | return t; | |
5356 | ||
7ddedda4 | 5357 | t = make_temp_vec (len); |
830bfa74 MM |
5358 | for (i = 0; i < len; i++) |
5359 | TREE_VEC_ELT (t, i) = elts[i]; | |
5360 | ||
5361 | return t; | |
5362 | } | |
5363 | ||
36a117a5 MM |
5364 | /* Return the result of substituting ARGS into the template parameters |
5365 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5366 | PARMS, then the result will contain n levels of PARMS. For | |
5367 | example, if PARMS is `template <class T> template <class U> | |
5368 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5369 | result will be `template <int*, double, class V>'. */ | |
5370 | ||
e9659ab0 | 5371 | static tree |
4393e105 | 5372 | tsubst_template_parms (parms, args, complain) |
36a117a5 MM |
5373 | tree parms; |
5374 | tree args; | |
4393e105 | 5375 | int complain; |
36a117a5 | 5376 | { |
f71f87f9 MM |
5377 | tree r = NULL_TREE; |
5378 | tree* new_parms; | |
36a117a5 MM |
5379 | |
5380 | for (new_parms = &r; | |
5381 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5382 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5383 | parms = TREE_CHAIN (parms)) | |
5384 | { | |
5385 | tree new_vec = | |
5386 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5387 | int i; | |
5388 | ||
5389 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5390 | { | |
5391 | tree default_value = | |
5392 | TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5393 | tree parm_decl = | |
5394 | TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5395 | ||
5396 | TREE_VEC_ELT (new_vec, i) | |
4393e105 MM |
5397 | = build_tree_list (tsubst (default_value, args, complain, |
5398 | NULL_TREE), | |
5399 | tsubst (parm_decl, args, complain, | |
5400 | NULL_TREE)); | |
36a117a5 MM |
5401 | } |
5402 | ||
5403 | *new_parms = | |
5404 | tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms) | |
5405 | - TMPL_ARGS_DEPTH (args))), | |
5406 | new_vec, NULL_TREE); | |
5407 | } | |
5408 | ||
5409 | return r; | |
5410 | } | |
5411 | ||
ed44da02 MM |
5412 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
5413 | type T. If T is not an aggregate or enumeration type, it is | |
5414 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
5415 | ENTERING_SCOPE is non-zero, T is the context for a template which | |
5416 | we are presently tsubst'ing. Return the subsituted value. */ | |
36a117a5 | 5417 | |
e9659ab0 | 5418 | static tree |
4393e105 | 5419 | tsubst_aggr_type (t, args, complain, in_decl, entering_scope) |
36a117a5 MM |
5420 | tree t; |
5421 | tree args; | |
4393e105 | 5422 | int complain; |
36a117a5 MM |
5423 | tree in_decl; |
5424 | int entering_scope; | |
5425 | { | |
5426 | if (t == NULL_TREE) | |
5427 | return NULL_TREE; | |
5428 | ||
5429 | switch (TREE_CODE (t)) | |
5430 | { | |
5431 | case RECORD_TYPE: | |
5432 | if (TYPE_PTRMEMFUNC_P (t)) | |
5433 | { | |
5434 | tree r = build_ptrmemfunc_type | |
4393e105 | 5435 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl)); |
adecb3f4 MM |
5436 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), |
5437 | complain); | |
36a117a5 MM |
5438 | } |
5439 | ||
5440 | /* else fall through */ | |
ed44da02 | 5441 | case ENUMERAL_TYPE: |
36a117a5 | 5442 | case UNION_TYPE: |
5db698f6 | 5443 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
5444 | { |
5445 | tree argvec; | |
5446 | tree context; | |
5447 | tree r; | |
5448 | ||
5449 | /* First, determine the context for the type we are looking | |
5450 | up. */ | |
5451 | if (TYPE_CONTEXT (t) != NULL_TREE) | |
5452 | context = tsubst_aggr_type (TYPE_CONTEXT (t), args, | |
4393e105 | 5453 | complain, |
36a117a5 MM |
5454 | in_decl, /*entering_scope=*/1); |
5455 | else | |
5456 | context = NULL_TREE; | |
5457 | ||
5458 | /* Then, figure out what arguments are appropriate for the | |
5459 | type we are trying to find. For example, given: | |
5460 | ||
5461 | template <class T> struct S; | |
5462 | template <class T, class U> void f(T, U) { S<U> su; } | |
5463 | ||
5464 | and supposing that we are instantiating f<int, double>, | |
5465 | then our ARGS will be {int, double}, but, when looking up | |
5466 | S we only want {double}. */ | |
7ddedda4 | 5467 | push_momentary (); |
4393e105 MM |
5468 | argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, |
5469 | complain); | |
36a117a5 MM |
5470 | |
5471 | r = lookup_template_class (t, argvec, in_decl, context, | |
5472 | entering_scope); | |
7ddedda4 | 5473 | pop_momentary (); |
36a117a5 | 5474 | |
adecb3f4 MM |
5475 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), |
5476 | complain); | |
36a117a5 MM |
5477 | } |
5478 | else | |
5479 | /* This is not a template type, so there's nothing to do. */ | |
5480 | return t; | |
5481 | ||
5482 | default: | |
4393e105 | 5483 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
5484 | } |
5485 | } | |
5486 | ||
9188c363 MM |
5487 | /* Substitute into the default argument ARG (a default argument for |
5488 | FN), which has the indicated TYPE. */ | |
5489 | ||
5490 | tree | |
5491 | tsubst_default_argument (fn, type, arg) | |
5492 | tree fn; | |
5493 | tree type; | |
5494 | tree arg; | |
5495 | { | |
5496 | /* This default argument came from a template. Instantiate the | |
5497 | default argument here, not in tsubst. In the case of | |
5498 | something like: | |
5499 | ||
5500 | template <class T> | |
5501 | struct S { | |
5502 | static T t(); | |
5503 | void f(T = t()); | |
5504 | }; | |
5505 | ||
5506 | we must be careful to do name lookup in the scope of S<T>, | |
5507 | rather than in the current class. */ | |
5508 | if (DECL_CLASS_SCOPE_P (fn)) | |
5509 | pushclass (DECL_REAL_CONTEXT (fn), 2); | |
5510 | ||
5511 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE); | |
5512 | ||
5513 | if (DECL_CLASS_SCOPE_P (fn)) | |
5514 | popclass (); | |
5515 | ||
5516 | /* Make sure the default argument is reasonable. */ | |
5517 | arg = check_default_argument (type, arg); | |
5518 | ||
5519 | return arg; | |
5520 | } | |
5521 | ||
5522 | /* Substitute into all the default arguments for FN. */ | |
5523 | ||
5524 | static void | |
5525 | tsubst_default_arguments (fn) | |
5526 | tree fn; | |
5527 | { | |
5528 | tree arg; | |
5529 | tree tmpl_args; | |
5530 | ||
5531 | tmpl_args = DECL_TI_ARGS (fn); | |
5532 | ||
5533 | /* If this function is not yet instantiated, we certainly don't need | |
5534 | its default arguments. */ | |
5535 | if (uses_template_parms (tmpl_args)) | |
5536 | return; | |
5537 | ||
5538 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
5539 | arg; | |
5540 | arg = TREE_CHAIN (arg)) | |
5541 | if (TREE_PURPOSE (arg)) | |
5542 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, | |
5543 | TREE_VALUE (arg), | |
5544 | TREE_PURPOSE (arg)); | |
5545 | } | |
5546 | ||
ae58fa02 MM |
5547 | /* Substitute the ARGS into the T, which is a _DECL. TYPE is the |
5548 | (already computed) substitution of ARGS into TREE_TYPE (T), if | |
5549 | appropriate. Return the result of the substitution. IN_DECL is as | |
5550 | for tsubst. */ | |
00d3396f | 5551 | |
e9659ab0 | 5552 | static tree |
ae58fa02 MM |
5553 | tsubst_decl (t, args, type, in_decl) |
5554 | tree t; | |
5555 | tree args; | |
5556 | tree type; | |
8d08fdba MS |
5557 | tree in_decl; |
5558 | { | |
ae58fa02 MM |
5559 | int saved_lineno; |
5560 | char* saved_filename; | |
b370501f | 5561 | tree r = NULL_TREE; |
830bfa74 | 5562 | |
ae58fa02 MM |
5563 | /* Set the filename and linenumber to improve error-reporting. */ |
5564 | saved_lineno = lineno; | |
5565 | saved_filename = input_filename; | |
5566 | lineno = DECL_SOURCE_LINE (t); | |
5567 | input_filename = DECL_SOURCE_FILE (t); | |
b7484fbe | 5568 | |
8d08fdba MS |
5569 | switch (TREE_CODE (t)) |
5570 | { | |
98c1c668 JM |
5571 | case TEMPLATE_DECL: |
5572 | { | |
5573 | /* We can get here when processing a member template function | |
5574 | of a template class. */ | |
98c1c668 | 5575 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 5576 | tree spec; |
db2767b6 | 5577 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); |
98c1c668 | 5578 | |
db2767b6 MM |
5579 | if (!is_template_template_parm) |
5580 | { | |
36a117a5 MM |
5581 | /* We might already have an instance of this template. |
5582 | The ARGS are for the surrounding class type, so the | |
5583 | full args contain the tsubst'd args for the context, | |
5584 | plus the innermost args from the template decl. */ | |
5585 | tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
5586 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
5587 | : DECL_TI_ARGS (DECL_RESULT (t)); | |
7ddedda4 MM |
5588 | tree full_args; |
5589 | ||
5590 | push_momentary (); | |
4393e105 MM |
5591 | full_args = tsubst_template_arg_vector (tmpl_args, args, |
5592 | /*complain=*/1); | |
36a117a5 MM |
5593 | |
5594 | /* tsubst_template_arg_vector doesn't copy the vector if | |
5595 | nothing changed. But, *something* should have | |
5596 | changed. */ | |
5597 | my_friendly_assert (full_args != tmpl_args, 0); | |
5598 | ||
5599 | spec = retrieve_specialization (t, full_args); | |
7ddedda4 | 5600 | pop_momentary (); |
db2767b6 | 5601 | if (spec != NULL_TREE) |
ae58fa02 MM |
5602 | { |
5603 | r = spec; | |
5604 | break; | |
5605 | } | |
db2767b6 | 5606 | } |
98c1c668 JM |
5607 | |
5608 | /* Make a new template decl. It will be similar to the | |
5609 | original, but will record the current template arguments. | |
5610 | We also create a new function declaration, which is just | |
5611 | like the old one, but points to this new template, rather | |
5612 | than the old one. */ | |
ae58fa02 MM |
5613 | r = copy_node (t); |
5614 | copy_lang_decl (r); | |
5615 | my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); | |
5616 | TREE_CHAIN (r) = NULL_TREE; | |
db2767b6 MM |
5617 | |
5618 | if (is_template_template_parm) | |
5619 | { | |
4393e105 | 5620 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5621 | DECL_RESULT (r) = new_decl; |
5622 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5623 | break; | |
db2767b6 MM |
5624 | } |
5625 | ||
ae58fa02 | 5626 | DECL_CONTEXT (r) |
4393e105 MM |
5627 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, |
5628 | in_decl, /*entering_scope=*/1); | |
ae58fa02 | 5629 | DECL_CLASS_CONTEXT (r) |
4393e105 MM |
5630 | = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5631 | /*complain=*/1, in_decl, | |
5632 | /*entering_scope=*/1); | |
ae58fa02 | 5633 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
5634 | |
5635 | if (TREE_CODE (decl) == TYPE_DECL) | |
5636 | { | |
4393e105 MM |
5637 | tree new_type = tsubst (TREE_TYPE (t), args, |
5638 | /*complain=*/1, in_decl); | |
ae58fa02 MM |
5639 | TREE_TYPE (r) = new_type; |
5640 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
5641 | DECL_RESULT (r) = TYPE_MAIN_DECL (new_type); | |
5642 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); | |
93cdc044 JM |
5643 | } |
5644 | else | |
5645 | { | |
4393e105 | 5646 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5647 | DECL_RESULT (r) = new_decl; |
5648 | DECL_TI_TEMPLATE (new_decl) = r; | |
5649 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5650 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
5651 | } |
5652 | ||
ae58fa02 MM |
5653 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5654 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
5655 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
5656 | |
5657 | /* The template parameters for this new template are all the | |
5658 | template parameters for the old template, except the | |
5659 | outermost level of parameters. */ | |
ae58fa02 | 5660 | DECL_TEMPLATE_PARMS (r) |
4393e105 MM |
5661 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
5662 | /*complain=*/1); | |
98c1c668 | 5663 | |
93cdc044 | 5664 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 5665 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 5666 | |
8d019cef | 5667 | /* We don't partially instantiate partial specializations. */ |
93cdc044 | 5668 | if (TREE_CODE (decl) == TYPE_DECL) |
ae58fa02 | 5669 | break; |
93cdc044 | 5670 | |
75650646 MM |
5671 | for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t); |
5672 | spec != NULL_TREE; | |
5673 | spec = TREE_CHAIN (spec)) | |
5674 | { | |
5675 | /* It helps to consider example here. Consider: | |
5676 | ||
5677 | template <class T> | |
5678 | struct S { | |
5679 | template <class U> | |
5680 | void f(U u); | |
5681 | ||
5682 | template <> | |
5683 | void f(T* t) {} | |
5684 | }; | |
5685 | ||
5686 | Now, for example, we are instantiating S<int>::f(U u). | |
5687 | We want to make a template: | |
5688 | ||
5689 | template <class U> | |
5690 | void S<int>::f(U); | |
5691 | ||
5692 | It will have a specialization, for the case U = int*, of | |
5693 | the form: | |
5694 | ||
5695 | template <> | |
5696 | void S<int>::f<int*>(int*); | |
5697 | ||
5698 | This specialization will be an instantiation of | |
5699 | the specialization given in the declaration of S, with | |
5700 | argument list int*. */ | |
5701 | ||
5702 | tree fn = TREE_VALUE (spec); | |
5703 | tree spec_args; | |
5704 | tree new_fn; | |
5705 | ||
5706 | if (!DECL_TEMPLATE_SPECIALIZATION (fn)) | |
5707 | /* Instantiations are on the same list, but they're of | |
5708 | no concern to us. */ | |
5709 | continue; | |
5710 | ||
36a117a5 MM |
5711 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
5712 | /* A full specialization. There's no need to record | |
5713 | that here. */ | |
5714 | continue; | |
5715 | ||
4393e105 MM |
5716 | spec_args = tsubst (DECL_TI_ARGS (fn), args, |
5717 | /*complain=*/1, in_decl); | |
36a117a5 | 5718 | new_fn = tsubst (DECL_RESULT (most_general_template (fn)), |
4393e105 | 5719 | spec_args, /*complain=*/1, in_decl); |
36a117a5 | 5720 | DECL_TI_TEMPLATE (new_fn) = fn; |
ae58fa02 | 5721 | register_specialization (new_fn, r, |
36a117a5 | 5722 | innermost_args (spec_args)); |
75650646 MM |
5723 | } |
5724 | ||
5725 | /* Record this partial instantiation. */ | |
ae58fa02 MM |
5726 | register_specialization (r, t, |
5727 | DECL_TI_ARGS (DECL_RESULT (r))); | |
75650646 | 5728 | |
98c1c668 | 5729 | } |
ae58fa02 | 5730 | break; |
8d08fdba MS |
5731 | |
5732 | case FUNCTION_DECL: | |
5733 | { | |
386b8a85 | 5734 | tree ctx; |
87603ed0 | 5735 | tree argvec = NULL_TREE; |
cf38f48a | 5736 | tree *friends; |
36a117a5 | 5737 | tree gen_tmpl; |
5566b478 | 5738 | int member; |
d8c4447d MM |
5739 | int args_depth; |
5740 | int parms_depth; | |
5566b478 | 5741 | |
36a117a5 MM |
5742 | /* Nobody should be tsubst'ing into non-template functions. */ |
5743 | my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5744 | ||
5745 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
5746 | { | |
5747 | tree spec; | |
5748 | ||
7ddedda4 MM |
5749 | /* Allocate template arguments on the momentary obstack, |
5750 | in case we don't need to keep them. */ | |
5751 | push_momentary (); | |
5752 | ||
36a117a5 MM |
5753 | /* Calculate the most general template of which R is a |
5754 | specialization, and the complete set of arguments used to | |
5755 | specialize R. */ | |
5756 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
7ddedda4 MM |
5757 | argvec |
5758 | = tsubst_template_arg_vector (DECL_TI_ARGS | |
5759 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
4393e105 | 5760 | args, /*complain=*/1); |
36a117a5 MM |
5761 | |
5762 | /* Check to see if we already have this specialization. */ | |
5763 | spec = retrieve_specialization (gen_tmpl, argvec); | |
7ddedda4 | 5764 | |
36a117a5 | 5765 | if (spec) |
ae58fa02 MM |
5766 | { |
5767 | r = spec; | |
7ddedda4 | 5768 | pop_momentary (); |
ae58fa02 MM |
5769 | break; |
5770 | } | |
d8c4447d | 5771 | |
7ddedda4 MM |
5772 | /* We're going to need to keep the ARGVEC, so we copy it |
5773 | here. */ | |
5774 | argvec = copy_to_permanent (argvec); | |
5775 | pop_momentary (); | |
5776 | ||
d8c4447d MM |
5777 | /* Here, we deal with the peculiar case: |
5778 | ||
5779 | template <class T> struct S { | |
5780 | template <class U> friend void f(); | |
5781 | }; | |
5782 | template <class U> friend void f() {} | |
5783 | template S<int>; | |
5784 | template void f<double>(); | |
5785 | ||
5786 | Here, the ARGS for the instantiation of will be {int, | |
5787 | double}. But, we only need as many ARGS as there are | |
5788 | levels of template parameters in CODE_PATTERN. We are | |
5789 | careful not to get fooled into reducing the ARGS in | |
5790 | situations like: | |
5791 | ||
5792 | template <class T> struct S { template <class U> void f(U); } | |
5793 | template <class T> template <> void S<T>::f(int) {} | |
5794 | ||
5795 | which we can spot because the pattern will be a | |
5796 | specialization in this case. */ | |
5797 | args_depth = TMPL_ARGS_DEPTH (args); | |
5798 | parms_depth = | |
5799 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
5800 | if (args_depth > parms_depth | |
5801 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
5802 | { | |
5803 | my_friendly_assert (DECL_FRIEND_P (t), 0); | |
5804 | ||
5805 | if (parms_depth > 1) | |
5806 | { | |
5807 | int i; | |
5808 | ||
5809 | args = make_temp_vec (parms_depth); | |
5810 | for (i = 0; i < parms_depth; ++i) | |
5811 | TREE_VEC_ELT (args, i) = | |
5812 | TREE_VEC_ELT (args, i + (args_depth - parms_depth)); | |
5813 | } | |
5814 | else | |
5815 | args = TREE_VEC_ELT (args, args_depth - parms_depth); | |
5816 | } | |
36a117a5 MM |
5817 | } |
5818 | else | |
5819 | { | |
5820 | /* This special case arises when we have something like this: | |
5821 | ||
5822 | template <class T> struct S { | |
5823 | friend void f<int>(int, double); | |
5824 | }; | |
5825 | ||
5826 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
39c01e4c MM |
5827 | will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are |
5828 | being called from tsubst_friend_function, and we want | |
5829 | only to create a new decl (R) with appropriate types so | |
5830 | that we can call determine_specialization. */ | |
5831 | my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5832 | == LOOKUP_EXPR) | |
5833 | || (TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5834 | == IDENTIFIER_NODE), 0); | |
36a117a5 MM |
5835 | gen_tmpl = NULL_TREE; |
5836 | } | |
5837 | ||
6eb3bb27 | 5838 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 5839 | { |
5566b478 MS |
5840 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
5841 | member = 2; | |
5842 | else | |
5843 | member = 1; | |
4393e105 MM |
5844 | ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5845 | /*complain=*/1, t, | |
36a117a5 | 5846 | /*entering_scope=*/1); |
5566b478 MS |
5847 | } |
5848 | else | |
5849 | { | |
5850 | member = 0; | |
5851 | ctx = NULL_TREE; | |
5566b478 | 5852 | } |
4393e105 | 5853 | type = tsubst (type, args, /*complain=*/1, in_decl); |
8d08fdba | 5854 | |
5566b478 MS |
5855 | /* We do NOT check for matching decls pushed separately at this |
5856 | point, as they may not represent instantiations of this | |
5857 | template, and in any case are considered separate under the | |
312e7d50 | 5858 | discrete model. Instead, see add_maybe_template. */ |
5566b478 MS |
5859 | |
5860 | r = copy_node (t); | |
5861 | copy_lang_decl (r); | |
e1467ff2 | 5862 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 MS |
5863 | TREE_TYPE (r) = type; |
5864 | ||
5865 | DECL_CONTEXT (r) | |
4393e105 MM |
5866 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, t, |
5867 | /*entering_scope=*/1); | |
5566b478 MS |
5868 | DECL_CLASS_CONTEXT (r) = ctx; |
5869 | ||
4c571114 | 5870 | if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r))) |
f181d4ae MM |
5871 | /* Type-conversion operator. Reconstruct the name, in |
5872 | case it's the name of one of the template's parameters. */ | |
5873 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
5566b478 | 5874 | |
4393e105 MM |
5875 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
5876 | /*complain=*/1, t); | |
711734a9 JM |
5877 | DECL_MAIN_VARIANT (r) = r; |
5878 | DECL_RESULT (r) = NULL_TREE; | |
711734a9 JM |
5879 | |
5880 | TREE_STATIC (r) = 0; | |
5881 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
5882 | DECL_EXTERNAL (r) = 1; | |
5883 | DECL_INTERFACE_KNOWN (r) = 0; | |
5884 | DECL_DEFER_OUTPUT (r) = 0; | |
5885 | TREE_CHAIN (r) = NULL_TREE; | |
5886 | DECL_PENDING_INLINE_INFO (r) = 0; | |
5887 | TREE_USED (r) = 0; | |
5888 | ||
36a117a5 MM |
5889 | /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled |
5890 | name. There's no need to do this in the special friend | |
5891 | case mentioned above where GEN_TMPL is NULL. */ | |
5892 | if (gen_tmpl) | |
386b8a85 | 5893 | { |
7ddedda4 MM |
5894 | /* The ARGVEC was built on the momentary obstack. Make it |
5895 | permanent now. */ | |
5896 | argvec = copy_to_permanent (argvec); | |
36a117a5 MM |
5897 | DECL_TEMPLATE_INFO (r) |
5898 | = perm_tree_cons (gen_tmpl, argvec, NULL_TREE); | |
5899 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5900 | register_specialization (r, gen_tmpl, argvec); | |
5901 | ||
5902 | /* Set the mangled name for R. */ | |
5903 | if (DECL_DESTRUCTOR_P (t)) | |
5904 | DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx); | |
5905 | else | |
386b8a85 | 5906 | { |
36a117a5 MM |
5907 | /* Instantiations of template functions must be mangled |
5908 | specially, in order to conform to 14.5.5.1 | |
5909 | [temp.over.link]. */ | |
5910 | tree tmpl = DECL_TI_TEMPLATE (t); | |
386b8a85 | 5911 | |
36a117a5 MM |
5912 | /* TMPL will be NULL if this is a specialization of a |
5913 | member function of a template class. */ | |
5914 | if (name_mangling_version < 1 | |
5915 | || tmpl == NULL_TREE | |
5916 | || (member && !is_member_template (tmpl) | |
5917 | && !DECL_TEMPLATE_INFO (tmpl))) | |
5918 | set_mangled_name_for_decl (r); | |
75650646 | 5919 | else |
36a117a5 | 5920 | set_mangled_name_for_template_decl (r); |
386b8a85 | 5921 | } |
36a117a5 MM |
5922 | |
5923 | DECL_RTL (r) = 0; | |
5924 | make_decl_rtl (r, NULL_PTR, 1); | |
5925 | ||
5926 | /* Like grokfndecl. If we don't do this, pushdecl will | |
5927 | mess up our TREE_CHAIN because it doesn't find a | |
5928 | previous decl. Sigh. */ | |
5929 | if (member | |
c36ae80f | 5930 | && ! uses_template_parms (r) |
36a117a5 MM |
5931 | && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) |
5932 | == NULL_TREE)) | |
5933 | SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r); | |
9188c363 MM |
5934 | |
5935 | /* We're not supposed to instantiate default arguments | |
5936 | until they are called, for a template. But, for a | |
5937 | declaration like: | |
5938 | ||
5939 | template <class T> void f () | |
5940 | { extern void g(int i = T()); } | |
5941 | ||
5942 | we should do the substitution when the template is | |
5943 | instantiated. We handle the member function case in | |
5944 | instantiate_class_template since the default arguments | |
5945 | might refer to other members of the class. */ | |
5946 | if (!member | |
5947 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
5948 | && !uses_template_parms (argvec)) | |
5949 | tsubst_default_arguments (r); | |
386b8a85 | 5950 | } |
f181d4ae | 5951 | |
cf38f48a MM |
5952 | /* Copy the list of befriending classes. */ |
5953 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
5954 | *friends; | |
5955 | friends = &TREE_CHAIN (*friends)) | |
5956 | { | |
5957 | *friends = copy_node (*friends); | |
5958 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
5959 | args, /*complain=*/1, | |
5960 | in_decl); | |
5961 | } | |
5962 | ||
f181d4ae MM |
5963 | if (DECL_CONSTRUCTOR_P (r)) |
5964 | { | |
5965 | maybe_retrofit_in_chrg (r); | |
5966 | grok_ctor_properties (ctx, r); | |
5967 | } | |
f71f87f9 | 5968 | else if (DECL_OVERLOADED_OPERATOR_P (r)) |
f181d4ae | 5969 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); |
8d08fdba | 5970 | } |
ae58fa02 | 5971 | break; |
8d08fdba MS |
5972 | |
5973 | case PARM_DECL: | |
5974 | { | |
ae58fa02 | 5975 | r = copy_node (t); |
5566b478 | 5976 | TREE_TYPE (r) = type; |
1b8899d1 MM |
5977 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); |
5978 | ||
db2767b6 MM |
5979 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
5980 | DECL_INITIAL (r) = TREE_TYPE (r); | |
5981 | else | |
4393e105 MM |
5982 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, |
5983 | /*complain=*/1, in_decl); | |
db2767b6 | 5984 | |
5566b478 | 5985 | DECL_CONTEXT (r) = NULL_TREE; |
fa56377d JJ |
5986 | if (PROMOTE_PROTOTYPES |
5987 | && (TREE_CODE (type) == INTEGER_TYPE | |
5988 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
f83b0cb6 JM |
5989 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
5990 | DECL_ARG_TYPE (r) = integer_type_node; | |
8d08fdba | 5991 | if (TREE_CHAIN (t)) |
4393e105 MM |
5992 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
5993 | /*complain=*/1, TREE_CHAIN (t)); | |
8d08fdba | 5994 | } |
ae58fa02 | 5995 | break; |
8d08fdba | 5996 | |
5566b478 MS |
5997 | case FIELD_DECL: |
5998 | { | |
ae58fa02 | 5999 | r = copy_node (t); |
5566b478 | 6000 | copy_lang_decl (r); |
1b8899d1 MM |
6001 | TREE_TYPE (r) = type; |
6002 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); | |
6003 | ||
6004 | /* We don't have to set DECL_CONTEXT here; it is set by | |
6005 | finish_member_declaration. */ | |
4393e105 MM |
6006 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, |
6007 | /*complain=*/1, in_decl); | |
5566b478 | 6008 | TREE_CHAIN (r) = NULL_TREE; |
4393e105 | 6009 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 6010 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 6011 | } |
ae58fa02 | 6012 | break; |
5566b478 MS |
6013 | |
6014 | case USING_DECL: | |
6015 | { | |
ae58fa02 | 6016 | r = copy_node (t); |
5566b478 | 6017 | DECL_INITIAL (r) |
4393e105 | 6018 | = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl); |
5566b478 | 6019 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 6020 | } |
ae58fa02 | 6021 | break; |
5566b478 | 6022 | |
9188c363 MM |
6023 | case TYPE_DECL: |
6024 | if (DECL_IMPLICIT_TYPEDEF_P (t)) | |
6025 | { | |
6026 | /* For an implicit typedef, we just want the implicit | |
6027 | typedef for the tsubst'd type. We've already got the | |
6028 | tsubst'd type, as TYPE, so we just need it's associated | |
6029 | declaration. */ | |
6030 | r = TYPE_NAME (type); | |
6031 | break; | |
6032 | } | |
6033 | else if (!DECL_LANG_SPECIFIC (t)) | |
6034 | { | |
6035 | /* For a template type parameter, we don't have to do | |
6036 | anything special. */ | |
6037 | r= TYPE_NAME (type); | |
6038 | break; | |
6039 | } | |
6040 | ||
6041 | /* Fall through. */ | |
6042 | ||
5566b478 MS |
6043 | case VAR_DECL: |
6044 | { | |
36a117a5 MM |
6045 | tree argvec; |
6046 | tree gen_tmpl; | |
6047 | tree spec; | |
6048 | tree tmpl; | |
9188c363 MM |
6049 | tree ctx; |
6050 | ||
8dd3f57a MM |
6051 | /* Nobody should be tsubst'ing into non-template variables. */ |
6052 | my_friendly_assert (DECL_LANG_SPECIFIC (t) | |
6053 | && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5566b478 | 6054 | |
9188c363 MM |
6055 | if (TYPE_P (DECL_CONTEXT (t))) |
6056 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, | |
6057 | /*complain=*/1, | |
6058 | in_decl, /*entering_scope=*/1); | |
6059 | else | |
6060 | /* Subsequent calls to pushdecl will fill this in. */ | |
6061 | ctx = NULL_TREE; | |
6062 | ||
36a117a5 MM |
6063 | /* Check to see if we already have this specialization. */ |
6064 | tmpl = DECL_TI_TEMPLATE (t); | |
6065 | gen_tmpl = most_general_template (tmpl); | |
4393e105 | 6066 | argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl); |
9188c363 MM |
6067 | if (ctx) |
6068 | spec = retrieve_specialization (gen_tmpl, argvec); | |
6069 | else | |
6070 | spec = retrieve_local_specialization (gen_tmpl, | |
6071 | current_function_decl); | |
6072 | ||
36a117a5 | 6073 | if (spec) |
ae58fa02 MM |
6074 | { |
6075 | r = spec; | |
6076 | break; | |
6077 | } | |
5566b478 | 6078 | |
e0942dcd MM |
6079 | /* This declaration is going to have to be around for a while, |
6080 | so me make sure it is on a saveable obstack. */ | |
6081 | push_obstacks_nochange (); | |
6082 | saveable_allocation (); | |
5566b478 | 6083 | r = copy_node (t); |
e0942dcd MM |
6084 | pop_obstacks (); |
6085 | ||
5566b478 | 6086 | TREE_TYPE (r) = type; |
1b8899d1 | 6087 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); |
5566b478 | 6088 | DECL_CONTEXT (r) = ctx; |
d11ad92e MS |
6089 | |
6090 | /* Don't try to expand the initializer until someone tries to use | |
6091 | this variable; otherwise we run into circular dependencies. */ | |
6092 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 MS |
6093 | DECL_RTL (r) = 0; |
6094 | DECL_SIZE (r) = 0; | |
36a117a5 MM |
6095 | copy_lang_decl (r); |
6096 | DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r); | |
5566b478 | 6097 | |
e0942dcd MM |
6098 | /* Even if the original location is out of scope, the newly |
6099 | substituted one is not. */ | |
6100 | if (TREE_CODE (r) == VAR_DECL) | |
6101 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
6102 | ||
fa8d6e85 MM |
6103 | /* A static data member declaration is always marked external |
6104 | when it is declared in-class, even if an initializer is | |
6105 | present. We mimic the non-template processing here. */ | |
e0942dcd MM |
6106 | if (ctx) |
6107 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 6108 | |
36a117a5 MM |
6109 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); |
6110 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
9188c363 MM |
6111 | if (ctx) |
6112 | register_specialization (r, gen_tmpl, argvec); | |
6113 | else | |
6114 | register_local_specialization (r, gen_tmpl, | |
6115 | current_function_decl); | |
5566b478 | 6116 | |
5566b478 | 6117 | TREE_CHAIN (r) = NULL_TREE; |
9188c363 | 6118 | if (TREE_CODE (r) == VAR_DECL && TREE_CODE (type) == VOID_TYPE) |
8251199e | 6119 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 6120 | } |
ae58fa02 | 6121 | break; |
5566b478 | 6122 | |
ae58fa02 MM |
6123 | default: |
6124 | my_friendly_abort (0); | |
6125 | } | |
6126 | ||
6127 | /* Restore the file and line information. */ | |
6128 | lineno = saved_lineno; | |
6129 | input_filename = saved_filename; | |
6130 | ||
6131 | return r; | |
6132 | } | |
6133 | ||
cabc336a MM |
6134 | /* Substitue into the ARG_TYPES of a function type. */ |
6135 | ||
e9659ab0 | 6136 | static tree |
4393e105 | 6137 | tsubst_arg_types (arg_types, args, complain, in_decl) |
cabc336a MM |
6138 | tree arg_types; |
6139 | tree args; | |
4393e105 | 6140 | int complain; |
cabc336a MM |
6141 | tree in_decl; |
6142 | { | |
6143 | tree remaining_arg_types; | |
cabc336a MM |
6144 | tree type; |
6145 | ||
6146 | if (!arg_types || arg_types == void_list_node) | |
6147 | return arg_types; | |
6148 | ||
6149 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
4393e105 MM |
6150 | args, complain, in_decl); |
6151 | if (remaining_arg_types == error_mark_node) | |
6152 | return error_mark_node; | |
6153 | ||
6154 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
6155 | if (type == error_mark_node) | |
6156 | return error_mark_node; | |
cabc336a | 6157 | |
4393e105 MM |
6158 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
6159 | top-level qualifiers as required. */ | |
6160 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a MM |
6161 | |
6162 | /* Note that we do not substitute into default arguments here. The | |
6163 | standard mandates that they be instantiated only when needed, | |
6164 | which is done in build_over_call. */ | |
51632249 JM |
6165 | return hash_tree_cons (TREE_PURPOSE (arg_types), type, |
6166 | remaining_arg_types); | |
cabc336a MM |
6167 | |
6168 | } | |
6169 | ||
4393e105 MM |
6170 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
6171 | *not* handle the exception-specification for FNTYPE, because the | |
6172 | initial substitution of explicitly provided template parameters | |
6173 | during argument deduction forbids substitution into the | |
6174 | exception-specification: | |
6175 | ||
6176 | [temp.deduct] | |
6177 | ||
6178 | All references in the function type of the function template to the | |
6179 | corresponding template parameters are replaced by the specified tem- | |
6180 | plate argument values. If a substitution in a template parameter or | |
6181 | in the function type of the function template results in an invalid | |
6182 | type, type deduction fails. [Note: The equivalent substitution in | |
6183 | exception specifications is done only when the function is instanti- | |
6184 | ated, at which point a program is ill-formed if the substitution | |
6185 | results in an invalid type.] */ | |
6186 | ||
6187 | static tree | |
6188 | tsubst_function_type (t, args, complain, in_decl) | |
6189 | tree t; | |
6190 | tree args; | |
6191 | int complain; | |
6192 | tree in_decl; | |
6193 | { | |
6194 | tree return_type; | |
6195 | tree arg_types; | |
6196 | tree fntype; | |
6197 | ||
8dd3f57a | 6198 | /* The TYPE_CONTEXT is not used for function/method types. */ |
4393e105 MM |
6199 | my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); |
6200 | ||
6201 | /* Substitue the return type. */ | |
6202 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6203 | if (return_type == error_mark_node) | |
6204 | return error_mark_node; | |
6205 | ||
6206 | /* Substitue the argument types. */ | |
6207 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, | |
6208 | complain, in_decl); | |
6209 | if (arg_types == error_mark_node) | |
6210 | return error_mark_node; | |
6211 | ||
6212 | /* Construct a new type node and return it. */ | |
6213 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
6214 | fntype = build_function_type (return_type, arg_types); | |
6215 | else | |
6216 | { | |
6217 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
6218 | if (! IS_AGGR_TYPE (r)) | |
6219 | { | |
6220 | /* [temp.deduct] | |
6221 | ||
6222 | Type deduction may fail for any of the following | |
6223 | reasons: | |
6224 | ||
6225 | -- Attempting to create "pointer to member of T" when T | |
6226 | is not a class type. */ | |
6227 | if (complain) | |
6228 | cp_error ("creating pointer to member function of non-class type `%T'", | |
6229 | r); | |
6230 | return error_mark_node; | |
6231 | } | |
6232 | ||
6233 | fntype = build_cplus_method_type (r, return_type, TREE_CHAIN | |
6234 | (arg_types)); | |
6235 | } | |
6236 | fntype = build_qualified_type (fntype, TYPE_QUALS (t)); | |
ada846eb | 6237 | fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
4393e105 MM |
6238 | |
6239 | return fntype; | |
6240 | } | |
6241 | ||
297e73d8 MM |
6242 | /* Substitute into the PARMS of a call-declarator. */ |
6243 | ||
e9659ab0 | 6244 | static tree |
4393e105 | 6245 | tsubst_call_declarator_parms (parms, args, complain, in_decl) |
297e73d8 MM |
6246 | tree parms; |
6247 | tree args; | |
4393e105 | 6248 | int complain; |
297e73d8 MM |
6249 | tree in_decl; |
6250 | { | |
6251 | tree new_parms; | |
6252 | tree type; | |
6253 | tree defarg; | |
6254 | ||
6255 | if (!parms || parms == void_list_node) | |
6256 | return parms; | |
6257 | ||
6258 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
4393e105 | 6259 | args, complain, in_decl); |
297e73d8 MM |
6260 | |
6261 | /* Figure out the type of this parameter. */ | |
4393e105 | 6262 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); |
297e73d8 MM |
6263 | |
6264 | /* Figure out the default argument as well. Note that we use | |
4393e105 MM |
6265 | tsubst_expr since the default argument is really an expression. */ |
6266 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
297e73d8 MM |
6267 | |
6268 | /* Chain this parameter on to the front of those we have already | |
6269 | processed. We don't use hash_tree_cons because that function | |
6270 | doesn't check TREE_PARMLIST. */ | |
6271 | new_parms = tree_cons (defarg, type, new_parms); | |
6272 | ||
6273 | /* And note that these are parameters. */ | |
6274 | TREE_PARMLIST (new_parms) = 1; | |
6275 | ||
6276 | return new_parms; | |
6277 | } | |
6278 | ||
4393e105 MM |
6279 | /* Take the tree structure T and replace template parameters used |
6280 | therein with the argument vector ARGS. IN_DECL is an associated | |
6281 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
6282 | An appropriate error message is issued only if COMPLAIN is | |
6283 | non-zero. Note that we must be relatively non-tolerant of | |
6284 | extensions here, in order to preserve conformance; if we allow | |
6285 | substitutions that should not be allowed, we may allow argument | |
6286 | deductions that should not succeed, and therefore report ambiguous | |
6287 | overload situations where there are none. In theory, we could | |
6288 | allow the substitution, but indicate that it should have failed, | |
6289 | and allow our caller to make sure that the right thing happens, but | |
6290 | we don't try to do this yet. | |
6291 | ||
6292 | This function is used for dealing with types, decls and the like; | |
6293 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 MM |
6294 | |
6295 | tree | |
4393e105 | 6296 | tsubst (t, args, complain, in_decl) |
ae58fa02 | 6297 | tree t, args; |
4393e105 | 6298 | int complain; |
ae58fa02 MM |
6299 | tree in_decl; |
6300 | { | |
0ecfe0b4 | 6301 | tree type, r; |
ae58fa02 MM |
6302 | |
6303 | if (t == NULL_TREE || t == error_mark_node | |
6304 | || t == integer_type_node | |
6305 | || t == void_type_node | |
6306 | || t == char_type_node | |
6307 | || TREE_CODE (t) == NAMESPACE_DECL) | |
6308 | return t; | |
6309 | ||
6310 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
6311 | type = IDENTIFIER_TYPE_VALUE (t); | |
6312 | else | |
6313 | type = TREE_TYPE (t); | |
6314 | if (type == unknown_type_node) | |
6315 | my_friendly_abort (42); | |
6316 | ||
6317 | if (type && TREE_CODE (t) != FUNCTION_DECL | |
6318 | && TREE_CODE (t) != TYPENAME_TYPE | |
6319 | && TREE_CODE (t) != TEMPLATE_DECL | |
4393e105 MM |
6320 | && TREE_CODE (t) != IDENTIFIER_NODE |
6321 | && TREE_CODE (t) != FUNCTION_TYPE | |
6322 | && TREE_CODE (t) != METHOD_TYPE) | |
6323 | type = tsubst (type, args, complain, in_decl); | |
6324 | if (type == error_mark_node) | |
6325 | return error_mark_node; | |
ae58fa02 MM |
6326 | |
6327 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd') | |
6328 | return tsubst_decl (t, args, type, in_decl); | |
6329 | ||
6330 | switch (TREE_CODE (t)) | |
6331 | { | |
6332 | case RECORD_TYPE: | |
6333 | case UNION_TYPE: | |
6334 | case ENUMERAL_TYPE: | |
4393e105 MM |
6335 | return tsubst_aggr_type (t, args, complain, in_decl, |
6336 | /*entering_scope=*/0); | |
ae58fa02 MM |
6337 | |
6338 | case ERROR_MARK: | |
6339 | case IDENTIFIER_NODE: | |
6340 | case OP_IDENTIFIER: | |
6341 | case VOID_TYPE: | |
6342 | case REAL_TYPE: | |
6343 | case COMPLEX_TYPE: | |
6344 | case BOOLEAN_TYPE: | |
6345 | case INTEGER_CST: | |
6346 | case REAL_CST: | |
6347 | case STRING_CST: | |
6348 | return t; | |
6349 | ||
6350 | case INTEGER_TYPE: | |
6351 | if (t == integer_type_node) | |
6352 | return t; | |
6353 | ||
6354 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
6355 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
6356 | return t; | |
d2e5ee5c | 6357 | |
5566b478 | 6358 | { |
ddce3528 | 6359 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 6360 | |
ddce3528 | 6361 | max = tsubst_expr (omax, args, complain, in_decl); |
4393e105 MM |
6362 | if (max == error_mark_node) |
6363 | return error_mark_node; | |
6364 | ||
8dd3f57a MM |
6365 | /* See if we can reduce this expression to something simpler. */ |
6366 | max = maybe_fold_nontype_arg (max); | |
6367 | if (!processing_template_decl && TREE_READONLY_DECL_P (max)) | |
6368 | max = decl_constant_value (max); | |
6369 | ||
856216bb MM |
6370 | if (processing_template_decl |
6371 | /* When providing explicit arguments to a template | |
6372 | function, but leaving some arguments for subsequent | |
6373 | deduction, MAX may be template-dependent even if we're | |
6374 | not PROCESSING_TEMPLATE_DECL. */ | |
6375 | || TREE_CODE (max) != INTEGER_CST) | |
ae58fa02 | 6376 | { |
8dd3f57a MM |
6377 | tree itype = make_node (INTEGER_TYPE); |
6378 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
6379 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, | |
6380 | integer_one_node); | |
6381 | return itype; | |
ae58fa02 MM |
6382 | } |
6383 | ||
ddce3528 JM |
6384 | if (integer_zerop (omax)) |
6385 | { | |
6386 | /* Still allow an explicit array of size zero. */ | |
6387 | if (pedantic) | |
6388 | pedwarn ("creating array with size zero"); | |
6389 | } | |
6390 | else if (integer_zerop (max) || INT_CST_LT (max, integer_zero_node)) | |
0ecfe0b4 | 6391 | { |
4393e105 MM |
6392 | /* [temp.deduct] |
6393 | ||
6394 | Type deduction may fail for any of the following | |
6395 | reasons: | |
6396 | ||
bf8f3f93 MM |
6397 | Attempting to create an array with a size that is |
6398 | zero or negative. */ | |
4393e105 MM |
6399 | if (complain) |
6400 | cp_error ("creating array with size `%E'", max); | |
6401 | ||
6402 | return error_mark_node; | |
0ecfe0b4 JM |
6403 | } |
6404 | ||
337c90cc | 6405 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node)); |
7ddedda4 MM |
6406 | if (!TREE_PERMANENT (max) && !allocation_temporary_p ()) |
6407 | max = copy_to_permanent (max); | |
120ecb8a | 6408 | return build_index_type (max); |
ae58fa02 MM |
6409 | } |
6410 | ||
6411 | case TEMPLATE_TYPE_PARM: | |
6412 | case TEMPLATE_TEMPLATE_PARM: | |
6413 | case TEMPLATE_PARM_INDEX: | |
6414 | { | |
6415 | int idx; | |
6416 | int level; | |
6417 | int levels; | |
0ecfe0b4 JM |
6418 | |
6419 | r = NULL_TREE; | |
ae58fa02 MM |
6420 | |
6421 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
6422 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6423 | { | |
6424 | idx = TEMPLATE_TYPE_IDX (t); | |
6425 | level = TEMPLATE_TYPE_LEVEL (t); | |
6426 | } | |
6427 | else | |
6428 | { | |
6429 | idx = TEMPLATE_PARM_IDX (t); | |
6430 | level = TEMPLATE_PARM_LEVEL (t); | |
6431 | } | |
6432 | ||
6433 | if (TREE_VEC_LENGTH (args) > 0) | |
6434 | { | |
6435 | tree arg = NULL_TREE; | |
6436 | ||
6437 | levels = TMPL_ARGS_DEPTH (args); | |
6438 | if (level <= levels) | |
6439 | arg = TMPL_ARG (args, level, idx); | |
6440 | ||
8b5b8b7c MM |
6441 | if (arg == error_mark_node) |
6442 | return error_mark_node; | |
6443 | else if (arg != NULL_TREE) | |
ae58fa02 MM |
6444 | { |
6445 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
6446 | { | |
6447 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg)) | |
6448 | == 't', 0); | |
adecb3f4 MM |
6449 | return cp_build_qualified_type_real |
6450 | (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t), | |
6451 | complain); | |
ae58fa02 MM |
6452 | } |
6453 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6454 | { | |
7ddedda4 | 6455 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 MM |
6456 | { |
6457 | /* We are processing a type constructed from | |
6458 | a template template parameter */ | |
7ddedda4 | 6459 | tree argvec = tsubst (TYPE_TI_ARGS (t), |
4393e105 MM |
6460 | args, complain, in_decl); |
6461 | if (argvec == error_mark_node) | |
6462 | return error_mark_node; | |
6463 | ||
ae58fa02 MM |
6464 | /* We can get a TEMPLATE_TEMPLATE_PARM here when |
6465 | we are resolving nested-types in the signature of | |
6466 | a member function templates. | |
6467 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
6468 | template to be instantiated. */ | |
6469 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
6470 | arg = TYPE_NAME (arg); | |
6471 | ||
6472 | r = lookup_template_class (DECL_NAME (arg), | |
6473 | argvec, in_decl, | |
6474 | DECL_CONTEXT (arg), | |
6475 | /*entering_scope=*/0); | |
adecb3f4 MM |
6476 | return cp_build_qualified_type_real (r, |
6477 | TYPE_QUALS (t), | |
6478 | complain); | |
ae58fa02 MM |
6479 | } |
6480 | else | |
6481 | /* We are processing a template argument list. */ | |
6482 | return arg; | |
6483 | } | |
6484 | else | |
6485 | return arg; | |
6486 | } | |
6487 | } | |
51924768 JM |
6488 | else |
6489 | my_friendly_abort (981018); | |
ae58fa02 MM |
6490 | |
6491 | if (level == 1) | |
6492 | /* This can happen during the attempted tsubst'ing in | |
6493 | unify. This means that we don't yet have any information | |
6494 | about the template parameter in question. */ | |
6495 | return t; | |
6496 | ||
6497 | /* If we get here, we must have been looking at a parm for a | |
6498 | more deeply nested template. Make a new version of this | |
6499 | template parameter, but with a lower level. */ | |
6500 | switch (TREE_CODE (t)) | |
6501 | { | |
6502 | case TEMPLATE_TYPE_PARM: | |
6503 | case TEMPLATE_TEMPLATE_PARM: | |
6504 | r = copy_node (t); | |
6505 | TEMPLATE_TYPE_PARM_INDEX (r) | |
6506 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
6507 | r, levels); | |
6508 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
6509 | TYPE_MAIN_VARIANT (r) = r; | |
6510 | TYPE_POINTER_TO (r) = NULL_TREE; | |
6511 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
6512 | ||
6513 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 6514 | && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 | 6515 | { |
4393e105 MM |
6516 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, |
6517 | complain, in_decl); | |
6518 | if (argvec == error_mark_node) | |
6519 | return error_mark_node; | |
6520 | ||
7ddedda4 | 6521 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
ae58fa02 MM |
6522 | = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE); |
6523 | } | |
6524 | break; | |
6525 | ||
6526 | case TEMPLATE_PARM_INDEX: | |
6527 | r = reduce_template_parm_level (t, type, levels); | |
6528 | break; | |
6529 | ||
6530 | default: | |
6531 | my_friendly_abort (0); | |
6532 | } | |
6533 | ||
5566b478 | 6534 | return r; |
ae58fa02 | 6535 | } |
5566b478 | 6536 | |
8d08fdba MS |
6537 | case TREE_LIST: |
6538 | { | |
6539 | tree purpose, value, chain, result; | |
8d08fdba MS |
6540 | |
6541 | if (t == void_list_node) | |
6542 | return t; | |
6543 | ||
8d08fdba MS |
6544 | purpose = TREE_PURPOSE (t); |
6545 | if (purpose) | |
4393e105 MM |
6546 | { |
6547 | purpose = tsubst (purpose, args, complain, in_decl); | |
6548 | if (purpose == error_mark_node) | |
6549 | return error_mark_node; | |
6550 | } | |
8d08fdba MS |
6551 | value = TREE_VALUE (t); |
6552 | if (value) | |
4393e105 MM |
6553 | { |
6554 | value = tsubst (value, args, complain, in_decl); | |
6555 | if (value == error_mark_node) | |
6556 | return error_mark_node; | |
6557 | } | |
8d08fdba MS |
6558 | chain = TREE_CHAIN (t); |
6559 | if (chain && chain != void_type_node) | |
4393e105 MM |
6560 | { |
6561 | chain = tsubst (chain, args, complain, in_decl); | |
6562 | if (chain == error_mark_node) | |
6563 | return error_mark_node; | |
6564 | } | |
8d08fdba MS |
6565 | if (purpose == TREE_PURPOSE (t) |
6566 | && value == TREE_VALUE (t) | |
6567 | && chain == TREE_CHAIN (t)) | |
6568 | return t; | |
51632249 | 6569 | result = hash_tree_cons (purpose, value, chain); |
8d08fdba MS |
6570 | TREE_PARMLIST (result) = TREE_PARMLIST (t); |
6571 | return result; | |
6572 | } | |
6573 | case TREE_VEC: | |
5566b478 MS |
6574 | if (type != NULL_TREE) |
6575 | { | |
1ceaad38 JM |
6576 | /* A binfo node. We always need to make a copy, of the node |
6577 | itself and of its BINFO_BASETYPES. */ | |
85b71cf2 | 6578 | |
5566b478 MS |
6579 | t = copy_node (t); |
6580 | ||
c7449f2b JM |
6581 | /* Make sure type isn't a typedef copy. */ |
6582 | type = BINFO_TYPE (TYPE_BINFO (type)); | |
6583 | ||
5566b478 | 6584 | TREE_TYPE (t) = complete_type (type); |
6633d636 MS |
6585 | if (IS_AGGR_TYPE (type)) |
6586 | { | |
6587 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
6588 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
6589 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
6590 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
6591 | } | |
5566b478 MS |
6592 | return t; |
6593 | } | |
85b71cf2 JM |
6594 | |
6595 | /* Otherwise, a vector of template arguments. */ | |
4393e105 | 6596 | return tsubst_template_arg_vector (t, args, complain); |
8d08fdba | 6597 | |
8d08fdba MS |
6598 | case POINTER_TYPE: |
6599 | case REFERENCE_TYPE: | |
6600 | { | |
8d08fdba | 6601 | enum tree_code code; |
79a7c7fa | 6602 | |
8d08fdba MS |
6603 | if (type == TREE_TYPE (t)) |
6604 | return t; | |
6605 | ||
6606 | code = TREE_CODE (t); | |
4393e105 MM |
6607 | |
6608 | ||
6609 | /* [temp.deduct] | |
6610 | ||
6611 | Type deduction may fail for any of the following | |
6612 | reasons: | |
6613 | ||
6614 | -- Attempting to create a pointer to reference type. | |
6615 | -- Attempting to create a reference to a reference type or | |
6616 | a reference to void. */ | |
0ecfe0b4 JM |
6617 | if (TREE_CODE (type) == REFERENCE_TYPE |
6618 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa JM |
6619 | { |
6620 | static int last_line = 0; | |
6621 | static char* last_file = 0; | |
6622 | ||
6623 | /* We keep track of the last time we issued this error | |
6624 | message to avoid spewing a ton of messages during a | |
6625 | single bad template instantiation. */ | |
4393e105 MM |
6626 | if (complain && (last_line != lineno || |
6627 | last_file != input_filename)) | |
79a7c7fa | 6628 | { |
0ecfe0b4 JM |
6629 | if (TREE_CODE (type) == VOID_TYPE) |
6630 | cp_error ("forming reference to void"); | |
6631 | else | |
6632 | cp_error ("forming %s to reference type `%T'", | |
6633 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
6634 | type); | |
79a7c7fa JM |
6635 | last_line = lineno; |
6636 | last_file = input_filename; | |
6637 | } | |
6638 | ||
4393e105 | 6639 | return error_mark_node; |
79a7c7fa JM |
6640 | } |
6641 | else if (code == POINTER_TYPE) | |
8d08fdba MS |
6642 | r = build_pointer_type (type); |
6643 | else | |
6644 | r = build_reference_type (type); | |
adecb3f4 | 6645 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 6646 | |
8d08fdba MS |
6647 | /* Will this ever be needed for TYPE_..._TO values? */ |
6648 | layout_type (r); | |
6649 | return r; | |
6650 | } | |
a4443a08 | 6651 | case OFFSET_TYPE: |
0ecfe0b4 | 6652 | { |
4393e105 MM |
6653 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
6654 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
6655 | { | |
6656 | /* [temp.deduct] | |
6657 | ||
6658 | Type deduction may fail for any of the following | |
6659 | reasons: | |
6660 | ||
6661 | -- Attempting to create "pointer to member of T" when T | |
6662 | is not a class type. */ | |
6663 | if (complain) | |
6664 | cp_error ("creating pointer to member of non-class type `%T'", | |
6665 | r); | |
6666 | return error_mark_node; | |
6667 | } | |
0ecfe0b4 JM |
6668 | return build_offset_type (r, type); |
6669 | } | |
8d08fdba MS |
6670 | case FUNCTION_TYPE: |
6671 | case METHOD_TYPE: | |
6672 | { | |
c11b6f21 | 6673 | tree fntype; |
4393e105 | 6674 | tree raises; |
8d08fdba | 6675 | |
4393e105 MM |
6676 | fntype = tsubst_function_type (t, args, complain, in_decl); |
6677 | if (fntype == error_mark_node) | |
6678 | return error_mark_node; | |
cabc336a MM |
6679 | |
6680 | /* Substitue the exception specification. */ | |
6681 | raises = TYPE_RAISES_EXCEPTIONS (t); | |
c11b6f21 MS |
6682 | if (raises) |
6683 | { | |
4cc1d462 NS |
6684 | tree list = NULL_TREE; |
6685 | ||
6686 | if (! TREE_VALUE (raises)) | |
6687 | list = raises; | |
6688 | else | |
6689 | for (; raises != NULL_TREE; raises = TREE_CHAIN (raises)) | |
6690 | { | |
6691 | tree spec = TREE_VALUE (raises); | |
6692 | ||
6693 | spec = tsubst (spec, args, complain, in_decl); | |
6694 | if (spec == error_mark_node) | |
6695 | return spec; | |
6696 | list = add_exception_specifier (list, spec, complain); | |
6697 | } | |
6698 | fntype = build_exception_variant (fntype, list); | |
c11b6f21 MS |
6699 | } |
6700 | return fntype; | |
8d08fdba MS |
6701 | } |
6702 | case ARRAY_TYPE: | |
6703 | { | |
4393e105 MM |
6704 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
6705 | if (domain == error_mark_node) | |
6706 | return error_mark_node; | |
6707 | ||
6708 | /* As an optimization, we avoid regenerating the array type if | |
6709 | it will obviously be the same as T. */ | |
8d08fdba MS |
6710 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
6711 | return t; | |
0ecfe0b4 | 6712 | |
4393e105 MM |
6713 | /* These checks should match the ones in grokdeclarator. |
6714 | ||
6715 | [temp.deduct] | |
6716 | ||
6717 | The deduction may fail for any of the following reasons: | |
6718 | ||
6719 | -- Attempting to create an array with an element type that | |
6720 | is void, a function type, or a reference type. */ | |
6721 | if (TREE_CODE (type) == VOID_TYPE | |
6722 | || TREE_CODE (type) == FUNCTION_TYPE | |
6723 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 6724 | { |
4393e105 MM |
6725 | if (complain) |
6726 | cp_error ("creating array of `%T'", type); | |
6727 | return error_mark_node; | |
0ecfe0b4 JM |
6728 | } |
6729 | ||
8d08fdba MS |
6730 | r = build_cplus_array_type (type, domain); |
6731 | return r; | |
6732 | } | |
6733 | ||
8d08fdba | 6734 | case PLUS_EXPR: |
5566b478 | 6735 | case MINUS_EXPR: |
4393e105 MM |
6736 | { |
6737 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6738 | in_decl); | |
6739 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, | |
6740 | in_decl); | |
6741 | ||
6742 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6743 | return error_mark_node; | |
6744 | ||
6745 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); | |
6746 | } | |
8d08fdba MS |
6747 | |
6748 | case NEGATE_EXPR: | |
6749 | case NOP_EXPR: | |
4393e105 MM |
6750 | { |
6751 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6752 | in_decl); | |
6753 | if (e == error_mark_node) | |
6754 | return error_mark_node; | |
6755 | ||
6756 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); | |
6757 | } | |
8d08fdba | 6758 | |
5566b478 MS |
6759 | case TYPENAME_TYPE: |
6760 | { | |
4393e105 MM |
6761 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
6762 | in_decl, /*entering_scope=*/1); | |
6763 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
6764 | complain, in_decl); | |
6765 | ||
6766 | if (ctx == error_mark_node || f == error_mark_node) | |
6767 | return error_mark_node; | |
ae58fa02 | 6768 | |
bf8f3f93 MM |
6769 | if (!IS_AGGR_TYPE (ctx)) |
6770 | { | |
6771 | if (complain) | |
6772 | cp_error ("`%T' is not a class, struct, or union type", | |
6773 | ctx); | |
6774 | return error_mark_node; | |
6775 | } | |
6776 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
6777 | { | |
6778 | /* Normally, make_typename_type does not require that the CTX | |
6779 | have complete type in order to allow things like: | |
ae58fa02 | 6780 | |
bf8f3f93 | 6781 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 6782 | |
bf8f3f93 MM |
6783 | But, such constructs have already been resolved by this |
6784 | point, so here CTX really should have complete type, unless | |
6785 | it's a partial instantiation. */ | |
4393e105 MM |
6786 | ctx = complete_type (ctx); |
6787 | if (!TYPE_SIZE (ctx)) | |
6788 | { | |
6789 | if (complain) | |
6790 | incomplete_type_error (NULL_TREE, ctx); | |
6791 | return error_mark_node; | |
6792 | } | |
6793 | } | |
ae58fa02 | 6794 | |
b2b7d40a | 6795 | f = make_typename_type (ctx, f); |
f0bcd168 MM |
6796 | if (f == error_mark_node) |
6797 | return f; | |
adecb3f4 MM |
6798 | return cp_build_qualified_type_real (f, |
6799 | CP_TYPE_QUALS (f) | |
6800 | | CP_TYPE_QUALS (t), | |
6801 | complain); | |
5566b478 MS |
6802 | } |
6803 | ||
6804 | case INDIRECT_REF: | |
4393e105 MM |
6805 | { |
6806 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6807 | in_decl); | |
6808 | if (e == error_mark_node) | |
6809 | return error_mark_node; | |
6810 | return make_pointer_declarator (type, e); | |
6811 | } | |
6812 | ||
5566b478 | 6813 | case ADDR_EXPR: |
4393e105 MM |
6814 | { |
6815 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6816 | in_decl); | |
6817 | if (e == error_mark_node) | |
6818 | return error_mark_node; | |
6819 | return make_reference_declarator (type, e); | |
6820 | } | |
5566b478 MS |
6821 | |
6822 | case ARRAY_REF: | |
4393e105 MM |
6823 | { |
6824 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6825 | in_decl); | |
6fb2c05a JM |
6826 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, |
6827 | in_decl); | |
4393e105 MM |
6828 | if (e1 == error_mark_node || e2 == error_mark_node) |
6829 | return error_mark_node; | |
6830 | ||
6831 | return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr); | |
6832 | } | |
5566b478 MS |
6833 | |
6834 | case CALL_EXPR: | |
4393e105 MM |
6835 | { |
6836 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6837 | in_decl); | |
43f887f9 MM |
6838 | tree e2 = (tsubst_call_declarator_parms |
6839 | (CALL_DECLARATOR_PARMS (t), args, complain, in_decl)); | |
6840 | tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args, | |
6841 | complain, in_decl); | |
4393e105 MM |
6842 | |
6843 | if (e1 == error_mark_node || e2 == error_mark_node | |
6844 | || e3 == error_mark_node) | |
6845 | return error_mark_node; | |
6846 | ||
43f887f9 | 6847 | return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3); |
4393e105 | 6848 | } |
5566b478 | 6849 | |
fc378698 | 6850 | case SCOPE_REF: |
4393e105 MM |
6851 | { |
6852 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6853 | in_decl); | |
6854 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
6855 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6856 | return error_mark_node; | |
6857 | ||
6858 | return build_parse_node (TREE_CODE (t), e1, e2); | |
6859 | } | |
fc378698 | 6860 | |
b894fc05 | 6861 | case TYPEOF_TYPE: |
4393e105 MM |
6862 | { |
6863 | tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, | |
6864 | in_decl); | |
6865 | if (e1 == error_mark_node) | |
6866 | return error_mark_node; | |
6867 | ||
6868 | return TREE_TYPE (e1); | |
6869 | } | |
b894fc05 | 6870 | |
8d08fdba | 6871 | default: |
5566b478 | 6872 | sorry ("use of `%s' in template", |
8d08fdba MS |
6873 | tree_code_name [(int) TREE_CODE (t)]); |
6874 | return error_mark_node; | |
6875 | } | |
6876 | } | |
6877 | ||
00d3396f JM |
6878 | /* Like tsubst, but deals with expressions. This function just replaces |
6879 | template parms; to finish processing the resultant expression, use | |
6880 | tsubst_expr. */ | |
6881 | ||
5566b478 | 6882 | tree |
4393e105 | 6883 | tsubst_copy (t, args, complain, in_decl) |
98c1c668 | 6884 | tree t, args; |
4393e105 | 6885 | int complain; |
5566b478 MS |
6886 | tree in_decl; |
6887 | { | |
6888 | enum tree_code code; | |
8452b1d3 | 6889 | tree r; |
8d08fdba | 6890 | |
5566b478 MS |
6891 | if (t == NULL_TREE || t == error_mark_node) |
6892 | return t; | |
6893 | ||
6894 | code = TREE_CODE (t); | |
b7484fbe | 6895 | |
5566b478 MS |
6896 | switch (code) |
6897 | { | |
6898 | case PARM_DECL: | |
a759e627 | 6899 | return do_identifier (DECL_NAME (t), 0, NULL_TREE); |
5566b478 MS |
6900 | |
6901 | case CONST_DECL: | |
ed44da02 MM |
6902 | { |
6903 | tree enum_type; | |
6904 | tree v; | |
6905 | ||
6906 | if (!DECL_CONTEXT (t)) | |
6907 | /* This is a global enumeration constant. */ | |
6908 | return t; | |
6909 | ||
6910 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 MM |
6911 | Consider: |
6912 | ||
6913 | template <int I> int f() { | |
6914 | enum E { a = I }; | |
6915 | struct S { void g() { E e = a; } }; | |
6916 | }; | |
6917 | ||
6918 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
6919 | clever enough to find f<7>::a. */ | |
ed44da02 | 6920 | enum_type |
4393e105 | 6921 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
ed44da02 MM |
6922 | /*entering_scope=*/0); |
6923 | ||
6924 | for (v = TYPE_VALUES (enum_type); | |
6925 | v != NULL_TREE; | |
6926 | v = TREE_CHAIN (v)) | |
6927 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
6928 | return TREE_VALUE (v); | |
6929 | ||
6930 | /* We didn't find the name. That should never happen; if | |
6931 | name-lookup found it during preliminary parsing, we | |
6932 | should find it again here during instantiation. */ | |
6933 | my_friendly_abort (0); | |
6934 | } | |
db04386f | 6935 | return t; |
ed44da02 | 6936 | |
5566b478 MS |
6937 | case FIELD_DECL: |
6938 | if (DECL_CONTEXT (t)) | |
6939 | { | |
0978790f | 6940 | tree ctx; |
0978790f | 6941 | |
4393e105 | 6942 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 6943 | /*entering_scope=*/1); |
0978790f | 6944 | if (ctx != DECL_CONTEXT (t)) |
5566b478 MS |
6945 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
6946 | } | |
6947 | return t; | |
6948 | ||
6949 | case VAR_DECL: | |
6950 | case FUNCTION_DECL: | |
6951 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
4393e105 | 6952 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
6953 | mark_used (t); |
6954 | return t; | |
6955 | ||
98c1c668 JM |
6956 | case TEMPLATE_DECL: |
6957 | if (is_member_template (t)) | |
4393e105 | 6958 | return tsubst (t, args, complain, in_decl); |
98c1c668 JM |
6959 | else |
6960 | return t; | |
6961 | ||
672476cb MM |
6962 | case LOOKUP_EXPR: |
6963 | { | |
6964 | /* We must tsbust into a LOOKUP_EXPR in case the names to | |
6965 | which it refers is a conversion operator; in that case the | |
6966 | name will change. We avoid making unnecessary copies, | |
6967 | however. */ | |
6968 | ||
4393e105 | 6969 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
672476cb MM |
6970 | |
6971 | if (id != TREE_OPERAND (t, 0)) | |
6972 | { | |
8452b1d3 | 6973 | r = build_nt (LOOKUP_EXPR, id); |
672476cb MM |
6974 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); |
6975 | t = r; | |
6976 | } | |
6977 | ||
6978 | return t; | |
6979 | } | |
6980 | ||
5566b478 MS |
6981 | case CAST_EXPR: |
6982 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
6983 | case CONST_CAST_EXPR: |
6984 | case STATIC_CAST_EXPR: | |
6985 | case DYNAMIC_CAST_EXPR: | |
51924768 | 6986 | case NOP_EXPR: |
5566b478 | 6987 | return build1 |
4393e105 MM |
6988 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6989 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
6990 | |
6991 | case INDIRECT_REF: | |
6992 | case PREDECREMENT_EXPR: | |
6993 | case PREINCREMENT_EXPR: | |
6994 | case POSTDECREMENT_EXPR: | |
6995 | case POSTINCREMENT_EXPR: | |
6996 | case NEGATE_EXPR: | |
6997 | case TRUTH_NOT_EXPR: | |
b87692e5 | 6998 | case BIT_NOT_EXPR: |
5566b478 MS |
6999 | case ADDR_EXPR: |
7000 | case CONVERT_EXPR: /* Unary + */ | |
7001 | case SIZEOF_EXPR: | |
abff8e06 | 7002 | case ALIGNOF_EXPR: |
5566b478 | 7003 | case ARROW_EXPR: |
fc378698 | 7004 | case THROW_EXPR: |
5156628f | 7005 | case TYPEID_EXPR: |
5566b478 | 7006 | return build1 |
6a629cac | 7007 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 7008 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 MS |
7009 | |
7010 | case PLUS_EXPR: | |
7011 | case MINUS_EXPR: | |
7012 | case MULT_EXPR: | |
7013 | case TRUNC_DIV_EXPR: | |
7014 | case CEIL_DIV_EXPR: | |
7015 | case FLOOR_DIV_EXPR: | |
7016 | case ROUND_DIV_EXPR: | |
7017 | case EXACT_DIV_EXPR: | |
7018 | case BIT_AND_EXPR: | |
7019 | case BIT_ANDTC_EXPR: | |
7020 | case BIT_IOR_EXPR: | |
7021 | case BIT_XOR_EXPR: | |
7022 | case TRUNC_MOD_EXPR: | |
7023 | case FLOOR_MOD_EXPR: | |
7024 | case TRUTH_ANDIF_EXPR: | |
7025 | case TRUTH_ORIF_EXPR: | |
7026 | case TRUTH_AND_EXPR: | |
7027 | case TRUTH_OR_EXPR: | |
7028 | case RSHIFT_EXPR: | |
7029 | case LSHIFT_EXPR: | |
7030 | case RROTATE_EXPR: | |
7031 | case LROTATE_EXPR: | |
7032 | case EQ_EXPR: | |
7033 | case NE_EXPR: | |
7034 | case MAX_EXPR: | |
7035 | case MIN_EXPR: | |
7036 | case LE_EXPR: | |
7037 | case GE_EXPR: | |
7038 | case LT_EXPR: | |
7039 | case GT_EXPR: | |
7040 | case COMPONENT_REF: | |
7041 | case ARRAY_REF: | |
7042 | case COMPOUND_EXPR: | |
7043 | case SCOPE_REF: | |
7044 | case DOTSTAR_EXPR: | |
7045 | case MEMBER_REF: | |
7046 | return build_nt | |
4393e105 MM |
7047 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7048 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
7049 | |
7050 | case CALL_EXPR: | |
7051 | { | |
7052 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 7053 | if (is_overloaded_fn (fn)) |
4393e105 | 7054 | fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl); |
5566b478 | 7055 | else |
f84b4be9 | 7056 | /* Sometimes FN is a LOOKUP_EXPR. */ |
4393e105 | 7057 | fn = tsubst_copy (fn, args, complain, in_decl); |
5566b478 | 7058 | return build_nt |
4393e105 MM |
7059 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
7060 | in_decl), | |
5566b478 MS |
7061 | NULL_TREE); |
7062 | } | |
7063 | ||
7064 | case METHOD_CALL_EXPR: | |
7065 | { | |
7066 | tree name = TREE_OPERAND (t, 0); | |
7067 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
7068 | { | |
4393e105 MM |
7069 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
7070 | complain, in_decl); | |
7bae46f4 | 7071 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
7072 | } |
7073 | else if (TREE_CODE (name) == SCOPE_REF | |
7074 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
7075 | { | |
4393e105 MM |
7076 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, |
7077 | complain, in_decl); | |
5566b478 | 7078 | name = TREE_OPERAND (name, 1); |
4393e105 MM |
7079 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
7080 | complain, in_decl); | |
11686454 | 7081 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
7082 | name = build_nt (SCOPE_REF, base, name); |
7083 | } | |
7084 | else | |
4393e105 | 7085 | name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
5566b478 | 7086 | return build_nt |
4393e105 MM |
7087 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, |
7088 | complain, in_decl), | |
7089 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), | |
5566b478 MS |
7090 | NULL_TREE); |
7091 | } | |
7092 | ||
558475f0 MM |
7093 | case STMT_EXPR: |
7094 | /* This processing should really occur in tsubst_expr, However, | |
7095 | tsubst_expr does not recurse into expressions, since it | |
7096 | assumes that there aren't any statements inside them. | |
7097 | Instead, it simply calls build_expr_from_tree. So, we need | |
7098 | to expand the STMT_EXPR here. */ | |
7099 | if (!processing_template_decl) | |
7100 | { | |
7101 | tree rtl_expr = begin_stmt_expr (); | |
7102 | tree block = tsubst_expr (STMT_EXPR_STMT (t), args, | |
7103 | complain, in_decl); | |
9d9169de | 7104 | return finish_stmt_expr (rtl_expr, block); |
558475f0 | 7105 | } |
9d9169de MM |
7106 | |
7107 | return t; | |
558475f0 | 7108 | |
5566b478 MS |
7109 | case COND_EXPR: |
7110 | case MODOP_EXPR: | |
40242ccf | 7111 | case PSEUDO_DTOR_EXPR: |
67da3287 | 7112 | { |
8452b1d3 | 7113 | r = build_nt |
4393e105 MM |
7114 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7115 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7116 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
67da3287 MM |
7117 | return r; |
7118 | } | |
5566b478 MS |
7119 | |
7120 | case NEW_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), | |
7125 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
7126 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
7127 | return r; | |
7128 | } | |
7129 | ||
7130 | case DELETE_EXPR: | |
7131 | { | |
8452b1d3 | 7132 | r = build_nt |
4393e105 MM |
7133 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7134 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
7135 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
7136 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
7137 | return r; | |
7138 | } | |
7139 | ||
386b8a85 JM |
7140 | case TEMPLATE_ID_EXPR: |
7141 | { | |
00d3396f | 7142 | /* Substituted template arguments */ |
4393e105 MM |
7143 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
7144 | in_decl); | |
856216bb MM |
7145 | |
7146 | if (targs && TREE_CODE (targs) == TREE_LIST) | |
7147 | { | |
7148 | tree chain; | |
7149 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
7150 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
7151 | } | |
7152 | else if (targs) | |
7153 | { | |
7154 | int i; | |
7155 | for (i = 0; i < TREE_VEC_LENGTH (targs); ++i) | |
7156 | TREE_VEC_ELT (targs, i) | |
7157 | = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i)); | |
7158 | } | |
00d3396f JM |
7159 | |
7160 | return lookup_template_function | |
4393e105 | 7161 | (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); |
386b8a85 JM |
7162 | } |
7163 | ||
5566b478 MS |
7164 | case TREE_LIST: |
7165 | { | |
7166 | tree purpose, value, chain; | |
7167 | ||
7168 | if (t == void_list_node) | |
7169 | return t; | |
7170 | ||
7171 | purpose = TREE_PURPOSE (t); | |
7172 | if (purpose) | |
4393e105 | 7173 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
7174 | value = TREE_VALUE (t); |
7175 | if (value) | |
4393e105 | 7176 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
7177 | chain = TREE_CHAIN (t); |
7178 | if (chain && chain != void_type_node) | |
4393e105 | 7179 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
7180 | if (purpose == TREE_PURPOSE (t) |
7181 | && value == TREE_VALUE (t) | |
7182 | && chain == TREE_CHAIN (t)) | |
7183 | return t; | |
7184 | return tree_cons (purpose, value, chain); | |
7185 | } | |
7186 | ||
7187 | case RECORD_TYPE: | |
7188 | case UNION_TYPE: | |
7189 | case ENUMERAL_TYPE: | |
7190 | case INTEGER_TYPE: | |
7191 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 7192 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 7193 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
7194 | case POINTER_TYPE: |
7195 | case REFERENCE_TYPE: | |
7196 | case OFFSET_TYPE: | |
7197 | case FUNCTION_TYPE: | |
7198 | case METHOD_TYPE: | |
7199 | case ARRAY_TYPE: | |
7200 | case TYPENAME_TYPE: | |
f84b4be9 | 7201 | case TYPE_DECL: |
4393e105 | 7202 | return tsubst (t, args, complain, in_decl); |
5566b478 | 7203 | |
e92cc029 | 7204 | case IDENTIFIER_NODE: |
8e710788 BK |
7205 | if (IDENTIFIER_TYPENAME_P (t) |
7206 | /* Make sure it's not just a variable named `__opr', for instance, | |
7207 | which can occur in some existing code. */ | |
7208 | && TREE_TYPE (t)) | |
e92cc029 | 7209 | return build_typename_overload |
4393e105 | 7210 | (tsubst (TREE_TYPE (t), args, complain, in_decl)); |
e92cc029 MS |
7211 | else |
7212 | return t; | |
7213 | ||
5156628f | 7214 | case CONSTRUCTOR: |
8452b1d3 JM |
7215 | { |
7216 | r = build | |
4393e105 MM |
7217 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), |
7218 | NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, | |
7219 | complain, in_decl)); | |
8452b1d3 JM |
7220 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
7221 | return r; | |
7222 | } | |
5156628f | 7223 | |
5566b478 MS |
7224 | default: |
7225 | return t; | |
7226 | } | |
7227 | } | |
7228 | ||
558475f0 | 7229 | /* Like tsubst_copy, but also does semantic processing. */ |
00d3396f | 7230 | |
5566b478 | 7231 | tree |
4393e105 | 7232 | tsubst_expr (t, args, complain, in_decl) |
98c1c668 | 7233 | tree t, args; |
4393e105 | 7234 | int complain; |
5566b478 MS |
7235 | tree in_decl; |
7236 | { | |
558475f0 MM |
7237 | tree stmt; |
7238 | ||
5566b478 MS |
7239 | if (t == NULL_TREE || t == error_mark_node) |
7240 | return t; | |
7241 | ||
5156628f | 7242 | if (processing_template_decl) |
4393e105 | 7243 | return tsubst_copy (t, args, complain, in_decl); |
5566b478 MS |
7244 | |
7245 | switch (TREE_CODE (t)) | |
8d08fdba | 7246 | { |
558475f0 MM |
7247 | case RETURN_INIT: |
7248 | finish_named_return_value | |
7249 | (TREE_OPERAND (t, 0), | |
7250 | tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, in_decl)); | |
7251 | tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7252 | break; | |
7253 | ||
7254 | case CTOR_INITIALIZER: | |
7255 | current_member_init_list | |
7256 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
7257 | current_base_init_list | |
7258 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
7259 | setup_vtbl_ptr (); | |
7260 | tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7261 | break; | |
7262 | ||
5566b478 | 7263 | case RETURN_STMT: |
f71f87f9 | 7264 | lineno = STMT_LINENO (t); |
ad321293 | 7265 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
4393e105 | 7266 | args, complain, in_decl)); |
5566b478 MS |
7267 | break; |
7268 | ||
7269 | case EXPR_STMT: | |
f71f87f9 | 7270 | lineno = STMT_LINENO (t); |
ad321293 | 7271 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
4393e105 | 7272 | args, complain, in_decl)); |
5566b478 MS |
7273 | break; |
7274 | ||
7275 | case DECL_STMT: | |
7276 | { | |
7277 | int i = suspend_momentary (); | |
e0942dcd MM |
7278 | tree decl; |
7279 | tree init; | |
5566b478 | 7280 | |
f71f87f9 | 7281 | lineno = STMT_LINENO (t); |
e0942dcd | 7282 | decl = DECL_STMT_DECL (t); |
acef433b MM |
7283 | if (TREE_CODE (decl) == LABEL_DECL) |
7284 | finish_label_decl (DECL_NAME (decl)); | |
7285 | else | |
7286 | { | |
7287 | init = DECL_INITIAL (decl); | |
7288 | decl = tsubst (decl, args, complain, in_decl); | |
7289 | init = tsubst_expr (init, args, complain, in_decl); | |
7290 | DECL_INITIAL (decl) = init; | |
b7b8bcd2 MM |
7291 | /* By marking the declaration as instantiated, we avoid |
7292 | trying to instantiate it. Since instantiate_decl can't | |
7293 | handle local variables, and since we've already done | |
7294 | all that needs to be done, that's the right thing to | |
7295 | do. */ | |
acef433b MM |
7296 | if (TREE_CODE (decl) == VAR_DECL) |
7297 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
7298 | maybe_push_decl (decl); | |
b7b8bcd2 | 7299 | cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0); |
acef433b MM |
7300 | add_decl_stmt (decl); |
7301 | } | |
5566b478 | 7302 | resume_momentary (i); |
e0942dcd | 7303 | return decl; |
5566b478 | 7304 | } |
8d08fdba | 7305 | |
5566b478 MS |
7306 | case FOR_STMT: |
7307 | { | |
7308 | tree tmp; | |
f71f87f9 | 7309 | lineno = STMT_LINENO (t); |
5566b478 | 7310 | |
558475f0 | 7311 | stmt = begin_for_stmt (); |
ad321293 | 7312 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) |
4393e105 | 7313 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7314 | finish_for_init_stmt (stmt); |
ad321293 | 7315 | finish_for_cond (tsubst_expr (FOR_COND (t), args, |
4393e105 | 7316 | complain, in_decl), |
558475f0 | 7317 | stmt); |
4393e105 | 7318 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); |
558475f0 | 7319 | finish_for_expr (tmp, stmt); |
4393e105 | 7320 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); |
558475f0 | 7321 | finish_for_stmt (tmp, stmt); |
5566b478 MS |
7322 | } |
7323 | break; | |
8d08fdba | 7324 | |
5566b478 MS |
7325 | case WHILE_STMT: |
7326 | { | |
f71f87f9 | 7327 | lineno = STMT_LINENO (t); |
558475f0 | 7328 | stmt = begin_while_stmt (); |
ad321293 | 7329 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), |
4393e105 | 7330 | args, complain, in_decl), |
558475f0 | 7331 | stmt); |
4393e105 | 7332 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); |
558475f0 | 7333 | finish_while_stmt (stmt); |
5566b478 MS |
7334 | } |
7335 | break; | |
8d08fdba | 7336 | |
5566b478 MS |
7337 | case DO_STMT: |
7338 | { | |
f71f87f9 | 7339 | lineno = STMT_LINENO (t); |
558475f0 | 7340 | stmt = begin_do_stmt (); |
4393e105 | 7341 | tsubst_expr (DO_BODY (t), args, complain, in_decl); |
558475f0 | 7342 | finish_do_body (stmt); |
ad321293 | 7343 | finish_do_stmt (tsubst_expr (DO_COND (t), args, |
4393e105 | 7344 | complain, in_decl), |
558475f0 | 7345 | stmt); |
5566b478 MS |
7346 | } |
7347 | break; | |
a0a33927 | 7348 | |
5566b478 | 7349 | case IF_STMT: |
8d08fdba | 7350 | { |
5566b478 | 7351 | tree tmp; |
5566b478 | 7352 | |
f71f87f9 | 7353 | lineno = STMT_LINENO (t); |
558475f0 | 7354 | stmt = begin_if_stmt (); |
ad321293 | 7355 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), |
4393e105 | 7356 | args, complain, in_decl), |
558475f0 | 7357 | stmt); |
db5ae43f | 7358 | |
ad321293 | 7359 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 7360 | { |
4393e105 | 7361 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7362 | finish_then_clause (stmt); |
db5ae43f MS |
7363 | } |
7364 | ||
ad321293 MM |
7365 | if (tmp = ELSE_CLAUSE (t), tmp) |
7366 | { | |
7367 | begin_else_clause (); | |
4393e105 | 7368 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7369 | finish_else_clause (stmt); |
ad321293 | 7370 | } |
8d08fdba | 7371 | |
ad321293 | 7372 | finish_if_stmt (); |
8d08fdba | 7373 | } |
5566b478 | 7374 | break; |
8d08fdba | 7375 | |
5566b478 MS |
7376 | case COMPOUND_STMT: |
7377 | { | |
ad321293 | 7378 | tree substmt; |
8d08fdba | 7379 | |
f71f87f9 | 7380 | lineno = STMT_LINENO (t); |
558475f0 | 7381 | stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
ad321293 MM |
7382 | for (substmt = COMPOUND_BODY (t); |
7383 | substmt != NULL_TREE; | |
7384 | substmt = TREE_CHAIN (substmt)) | |
4393e105 | 7385 | tsubst_expr (substmt, args, complain, in_decl); |
558475f0 | 7386 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt); |
5566b478 MS |
7387 | } |
7388 | break; | |
8d08fdba | 7389 | |
5566b478 | 7390 | case BREAK_STMT: |
f71f87f9 | 7391 | lineno = STMT_LINENO (t); |
ad321293 | 7392 | finish_break_stmt (); |
5566b478 | 7393 | break; |
8d08fdba | 7394 | |
6467930b | 7395 | case CONTINUE_STMT: |
f71f87f9 | 7396 | lineno = STMT_LINENO (t); |
ad321293 | 7397 | finish_continue_stmt (); |
6467930b MS |
7398 | break; |
7399 | ||
5566b478 MS |
7400 | case SWITCH_STMT: |
7401 | { | |
558475f0 | 7402 | tree val; |
5566b478 | 7403 | |
f71f87f9 | 7404 | lineno = STMT_LINENO (t); |
ad321293 | 7405 | begin_switch_stmt (); |
4393e105 | 7406 | val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); |
558475f0 MM |
7407 | stmt = finish_switch_cond (val); |
7408 | tsubst_expr (SWITCH_BODY (t), args, complain, in_decl); | |
7409 | finish_switch_stmt (val, stmt); | |
5566b478 MS |
7410 | } |
7411 | break; | |
7412 | ||
7413 | case CASE_LABEL: | |
4393e105 MM |
7414 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), |
7415 | tsubst_expr (CASE_HIGH (t), args, complain, in_decl)); | |
5566b478 MS |
7416 | break; |
7417 | ||
acef433b MM |
7418 | case LABEL_STMT: |
7419 | lineno = STMT_LINENO (t); | |
7420 | finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t))); | |
5566b478 MS |
7421 | break; |
7422 | ||
7423 | case GOTO_STMT: | |
f71f87f9 | 7424 | lineno = STMT_LINENO (t); |
aa09da44 | 7425 | t = GOTO_DESTINATION (t); |
3fa56191 | 7426 | if (TREE_CODE (t) != LABEL_DECL) |
aa09da44 MM |
7427 | /* Computed goto's must be tsubst'd into. On the other hand, |
7428 | non-computed gotos must not be; the identifier in question | |
7429 | will have no binding. */ | |
4393e105 | 7430 | t = tsubst_expr (t, args, complain, in_decl); |
3fa56191 MM |
7431 | else |
7432 | t = DECL_NAME (t); | |
aa09da44 | 7433 | finish_goto_stmt (t); |
ad321293 MM |
7434 | break; |
7435 | ||
7436 | case ASM_STMT: | |
f71f87f9 | 7437 | lineno = STMT_LINENO (t); |
3ebc5c52 | 7438 | finish_asm_stmt (ASM_CV_QUAL (t), |
4393e105 MM |
7439 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), |
7440 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
7441 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
7442 | tsubst_expr (ASM_CLOBBERS (t), args, complain, | |
7443 | in_decl)); | |
5566b478 | 7444 | break; |
faf5394a MS |
7445 | |
7446 | case TRY_BLOCK: | |
f71f87f9 | 7447 | lineno = STMT_LINENO (t); |
558475f0 | 7448 | stmt = begin_try_block (); |
4393e105 | 7449 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); |
558475f0 | 7450 | finish_try_block (stmt); |
f1dedc31 MM |
7451 | if (CLEANUP_P (t)) |
7452 | finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args, | |
7453 | complain, in_decl), | |
7454 | stmt); | |
7455 | else | |
7456 | { | |
7457 | tree handler = TRY_HANDLERS (t); | |
7458 | for (; handler; handler = TREE_CHAIN (handler)) | |
7459 | tsubst_expr (handler, args, complain, in_decl); | |
7460 | finish_handler_sequence (stmt); | |
7461 | } | |
faf5394a MS |
7462 | break; |
7463 | ||
7464 | case HANDLER: | |
f71f87f9 | 7465 | lineno = STMT_LINENO (t); |
558475f0 | 7466 | stmt = begin_handler (); |
ad321293 | 7467 | if (HANDLER_PARMS (t)) |
3c5c0849 MM |
7468 | expand_start_catch_block |
7469 | (tsubst (DECL_STMT_DECL (HANDLER_PARMS (t)), | |
7470 | args, complain, in_decl)); | |
faf5394a | 7471 | else |
3c5c0849 | 7472 | expand_start_catch_block (NULL_TREE); |
558475f0 | 7473 | finish_handler_parms (stmt); |
4393e105 | 7474 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); |
558475f0 | 7475 | finish_handler (stmt); |
faf5394a MS |
7476 | break; |
7477 | ||
b87692e5 | 7478 | case TAG_DEFN: |
f71f87f9 | 7479 | lineno = STMT_LINENO (t); |
b87692e5 MS |
7480 | t = TREE_TYPE (t); |
7481 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
4393e105 | 7482 | tsubst (t, args, complain, NULL_TREE); |
b87692e5 MS |
7483 | break; |
7484 | ||
5566b478 | 7485 | default: |
4393e105 | 7486 | return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl)); |
5566b478 MS |
7487 | } |
7488 | return NULL_TREE; | |
8d08fdba MS |
7489 | } |
7490 | ||
6ba89f8e | 7491 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
7492 | the template arguments in TARG_PTR. */ |
7493 | ||
5566b478 MS |
7494 | tree |
7495 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 7496 | tree tmpl, targ_ptr; |
8d08fdba | 7497 | { |
5566b478 | 7498 | tree fndecl; |
36a117a5 MM |
7499 | tree gen_tmpl; |
7500 | tree spec; | |
5566b478 MS |
7501 | int i, len; |
7502 | struct obstack *old_fmp_obstack; | |
7503 | extern struct obstack *function_maybepermanent_obstack; | |
36a117a5 | 7504 | tree inner_args; |
5566b478 | 7505 | |
27fafc8d JM |
7506 | if (tmpl == error_mark_node) |
7507 | return error_mark_node; | |
7508 | ||
386b8a85 JM |
7509 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
7510 | ||
36a117a5 MM |
7511 | /* Check to see if we already have this specialization. */ |
7512 | spec = retrieve_specialization (tmpl, targ_ptr); | |
7513 | if (spec != NULL_TREE) | |
7514 | return spec; | |
7515 | ||
7516 | if (DECL_TEMPLATE_INFO (tmpl)) | |
386b8a85 | 7517 | { |
36a117a5 MM |
7518 | /* The TMPL is a partial instantiation. To get a full set of |
7519 | arguments we must add the arguments used to perform the | |
7520 | partial instantiation. */ | |
7521 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
7522 | targ_ptr); | |
7523 | gen_tmpl = most_general_template (tmpl); | |
7524 | ||
7525 | /* Check to see if we already have this specialization. */ | |
7526 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
75650646 MM |
7527 | if (spec != NULL_TREE) |
7528 | return spec; | |
386b8a85 | 7529 | } |
36a117a5 MM |
7530 | else |
7531 | gen_tmpl = tmpl; | |
386b8a85 | 7532 | |
5566b478 MS |
7533 | push_obstacks (&permanent_obstack, &permanent_obstack); |
7534 | old_fmp_obstack = function_maybepermanent_obstack; | |
7535 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 7536 | |
36a117a5 MM |
7537 | len = DECL_NTPARMS (gen_tmpl); |
7538 | inner_args = innermost_args (targ_ptr); | |
5566b478 MS |
7539 | i = len; |
7540 | while (i--) | |
8d08fdba | 7541 | { |
36a117a5 | 7542 | tree t = TREE_VEC_ELT (inner_args, i); |
5566b478 MS |
7543 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
7544 | { | |
7545 | tree nt = target_type (t); | |
ec255269 | 7546 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 | 7547 | { |
8251199e JM |
7548 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); |
7549 | cp_error (" trying to instantiate `%D'", gen_tmpl); | |
5566b478 MS |
7550 | fndecl = error_mark_node; |
7551 | goto out; | |
7552 | } | |
7553 | } | |
8d08fdba | 7554 | } |
e66d884e | 7555 | targ_ptr = copy_to_permanent (targ_ptr); |
8d08fdba | 7556 | |
5566b478 | 7557 | /* substitute template parameters */ |
4393e105 | 7558 | fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, /*complain=*/1, gen_tmpl); |
36a117a5 MM |
7559 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
7560 | template, not the most general template. */ | |
7561 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 7562 | |
824b9a4c MS |
7563 | if (flag_external_templates) |
7564 | add_pending_template (fndecl); | |
7565 | ||
5566b478 MS |
7566 | out: |
7567 | function_maybepermanent_obstack = old_fmp_obstack; | |
7568 | pop_obstacks (); | |
8d08fdba | 7569 | |
5566b478 | 7570 | return fndecl; |
8d08fdba | 7571 | } |
5566b478 MS |
7572 | |
7573 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
7574 | |
7575 | void | |
5566b478 MS |
7576 | overload_template_name (type) |
7577 | tree type; | |
8d08fdba | 7578 | { |
5566b478 MS |
7579 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
7580 | tree decl; | |
8d08fdba | 7581 | |
5566b478 MS |
7582 | if (IDENTIFIER_CLASS_VALUE (id) |
7583 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
7584 | return; | |
8d08fdba | 7585 | |
5566b478 MS |
7586 | decl = build_decl (TYPE_DECL, id, type); |
7587 | SET_DECL_ARTIFICIAL (decl); | |
7588 | pushdecl_class_level (decl); | |
8d08fdba MS |
7589 | } |
7590 | ||
4393e105 MM |
7591 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
7592 | arguments that are being used when calling it. TARGS is a vector | |
7593 | into which the deduced template arguments are placed. | |
8d08fdba MS |
7594 | |
7595 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
7596 | all the types, and 1 for complete failure. An error message will be | |
7597 | printed only for an incomplete match. | |
7598 | ||
4393e105 MM |
7599 | If FN is a conversion operator, RETURN_TYPE is the type desired as |
7600 | the result of the conversion operator. | |
8d08fdba | 7601 | |
4393e105 | 7602 | TPARMS is a vector of template parameters. |
9f54c803 MM |
7603 | |
7604 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
7605 | template-id. | |
6467930b | 7606 | |
830bfa74 MM |
7607 | The parameter STRICT is one of: |
7608 | ||
7609 | DEDUCE_CALL: | |
7610 | We are deducing arguments for a function call, as in | |
7611 | [temp.deduct.call]. | |
7612 | ||
7613 | DEDUCE_CONV: | |
7614 | We are deducing arguments for a conversion function, as in | |
7615 | [temp.deduct.conv]. | |
7616 | ||
7617 | DEDUCE_EXACT: | |
7618 | We are deducing arguments when calculating the partial | |
7619 | ordering between specializations of function or class | |
7620 | templates, as in [temp.func.order] and [temp.class.order], | |
7621 | when doing an explicit instantiation as in [temp.explicit], | |
9f54c803 | 7622 | when determining an explicit specialization as in |
830bfa74 | 7623 | [temp.expl.spec], or when taking the address of a function |
4393e105 MM |
7624 | template, as in [temp.deduct.funcaddr]. |
7625 | ||
4393e105 | 7626 | The other arguments are as for type_unification. */ |
8d08fdba MS |
7627 | |
7628 | int | |
4393e105 | 7629 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
03017874 | 7630 | strict) |
4393e105 | 7631 | tree fn, explicit_targs, targs, args, return_type; |
830bfa74 | 7632 | unification_kind_t strict; |
386b8a85 | 7633 | { |
4393e105 MM |
7634 | tree parms; |
7635 | tree fntype; | |
adecb3f4 | 7636 | int result; |
386b8a85 | 7637 | |
4393e105 MM |
7638 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); |
7639 | ||
7640 | fntype = TREE_TYPE (fn); | |
7641 | if (explicit_targs) | |
75650646 | 7642 | { |
4393e105 MM |
7643 | /* [temp.deduct] |
7644 | ||
7645 | The specified template arguments must match the template | |
7646 | parameters in kind (i.e., type, nontype, template), and there | |
7647 | must not be more arguments than there are parameters; | |
7648 | otherwise type deduction fails. | |
7649 | ||
7650 | Nontype arguments must match the types of the corresponding | |
7651 | nontype template parameters, or must be convertible to the | |
7652 | types of the corresponding nontype parameters as specified in | |
7653 | _temp.arg.nontype_, otherwise type deduction fails. | |
7654 | ||
7655 | All references in the function type of the function template | |
7656 | to the corresponding template parameters are replaced by the | |
7657 | specified template argument values. If a substitution in a | |
7658 | template parameter or in the function type of the function | |
7659 | template results in an invalid type, type deduction fails. */ | |
7660 | int i; | |
7661 | tree converted_args; | |
75650646 | 7662 | |
4393e105 MM |
7663 | converted_args |
7664 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
7665 | explicit_targs, NULL_TREE, /*complain=*/0, | |
7666 | /*require_all_arguments=*/0)); | |
7667 | if (converted_args == error_mark_node) | |
75650646 | 7668 | return 1; |
386b8a85 | 7669 | |
4393e105 MM |
7670 | fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE); |
7671 | if (fntype == error_mark_node) | |
7672 | return 1; | |
050367a3 | 7673 | |
4393e105 MM |
7674 | /* Place the explicitly specified arguments in TARGS. */ |
7675 | for (i = 0; i < TREE_VEC_LENGTH (targs); i++) | |
7676 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); | |
75650646 | 7677 | } |
4393e105 MM |
7678 | |
7679 | parms = TYPE_ARG_TYPES (fntype); | |
386b8a85 | 7680 | |
4393e105 MM |
7681 | if (DECL_CONV_FN_P (fn)) |
7682 | { | |
7683 | /* This is a template conversion operator. Use the return types | |
edef8288 JM |
7684 | as well as the argument types. We use it instead of 'this', since |
7685 | we could be comparing conversions from different classes. */ | |
4393e105 | 7686 | parms = scratch_tree_cons (NULL_TREE, TREE_TYPE (fntype), |
edef8288 JM |
7687 | TREE_CHAIN (parms)); |
7688 | args = scratch_tree_cons (NULL_TREE, return_type, TREE_CHAIN (args)); | |
4393e105 MM |
7689 | } |
7690 | ||
4393e105 MM |
7691 | /* We allow incomplete unification without an error message here |
7692 | because the standard doesn't seem to explicitly prohibit it. Our | |
7693 | callers must be ready to deal with unification failures in any | |
7694 | event. */ | |
adecb3f4 MM |
7695 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
7696 | targs, parms, args, /*subr=*/0, | |
7697 | strict, /*allow_incomplete*/1); | |
7698 | ||
7699 | if (result == 0) | |
7700 | /* All is well so far. Now, check: | |
7701 | ||
7702 | [temp.deduct] | |
7703 | ||
7704 | When all template arguments have been deduced, all uses of | |
7705 | template parameters in nondeduced contexts are replaced with | |
7706 | the corresponding deduced argument values. If the | |
7707 | substitution results in an invalid type, as described above, | |
7708 | type deduction fails. */ | |
7709 | if (tsubst (TREE_TYPE (fn), targs, /*complain=*/0, NULL_TREE) | |
7710 | == error_mark_node) | |
7711 | return 1; | |
7712 | ||
7713 | return result; | |
830bfa74 MM |
7714 | } |
7715 | ||
7716 | /* Adjust types before performing type deduction, as described in | |
7717 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
7718 | sections are symmetric. PARM is the type of a function parameter |
7719 | or the return type of the conversion function. ARG is the type of | |
7720 | the argument passed to the call, or the type of the value | |
7721 | intialized with the result of the conversion function. */ | |
386b8a85 | 7722 | |
e9659ab0 | 7723 | static void |
830bfa74 MM |
7724 | maybe_adjust_types_for_deduction (strict, parm, arg) |
7725 | unification_kind_t strict; | |
7726 | tree* parm; | |
7727 | tree* arg; | |
7728 | { | |
7729 | switch (strict) | |
7730 | { | |
7731 | case DEDUCE_CALL: | |
7732 | break; | |
7733 | ||
7734 | case DEDUCE_CONV: | |
7735 | { | |
4c7d0dff MM |
7736 | /* Swap PARM and ARG throughout the remainder of this |
7737 | function; the handling is precisely symmetric since PARM | |
7738 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
7739 | tree* temp = parm; |
7740 | parm = arg; | |
7741 | arg = temp; | |
7742 | break; | |
7743 | } | |
7744 | ||
7745 | case DEDUCE_EXACT: | |
7746 | /* There is nothing to do in this case. */ | |
7747 | return; | |
7748 | ||
7749 | default: | |
7750 | my_friendly_abort (0); | |
7751 | } | |
7752 | ||
7753 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
7754 | { | |
7755 | /* [temp.deduct.call] | |
7756 | ||
7757 | If P is not a reference type: | |
7758 | ||
7759 | --If A is an array type, the pointer type produced by the | |
7760 | array-to-pointer standard conversion (_conv.array_) is | |
7761 | used in place of A for type deduction; otherwise, | |
7762 | ||
7763 | --If A is a function type, the pointer type produced by | |
7764 | the function-to-pointer standard conversion | |
7765 | (_conv.func_) is used in place of A for type deduction; | |
7766 | otherwise, | |
7767 | ||
7768 | --If A is a cv-qualified type, the top level | |
7769 | cv-qualifiers of A's type are ignored for type | |
7770 | deduction. */ | |
7771 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
7772 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 7773 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
7774 | *arg = build_pointer_type (*arg); |
7775 | else | |
7776 | *arg = TYPE_MAIN_VARIANT (*arg); | |
7777 | } | |
7778 | ||
7779 | /* [temp.deduct.call] | |
7780 | ||
7781 | If P is a cv-qualified type, the top level cv-qualifiers | |
7782 | of P's type are ignored for type deduction. If P is a | |
7783 | reference type, the type referred to by P is used for | |
7784 | type deduction. */ | |
7785 | *parm = TYPE_MAIN_VARIANT (*parm); | |
7786 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
7787 | *parm = TREE_TYPE (*parm); | |
386b8a85 JM |
7788 | } |
7789 | ||
4393e105 | 7790 | /* Like type_unfication. |
9f54c803 MM |
7791 | |
7792 | If SUBR is 1, we're being called recursively (to unify the | |
7793 | arguments of a function or method parameter of a function | |
7794 | template). */ | |
386b8a85 | 7795 | |
4966381a | 7796 | static int |
3b3ba9f0 | 7797 | type_unification_real (tparms, targs, parms, args, subr, |
4393e105 | 7798 | strict, allow_incomplete) |
050367a3 | 7799 | tree tparms, targs, parms, args; |
830bfa74 MM |
7800 | int subr; |
7801 | unification_kind_t strict; | |
7802 | int allow_incomplete; | |
8d08fdba MS |
7803 | { |
7804 | tree parm, arg; | |
7805 | int i; | |
7806 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 7807 | int sub_strict; |
8d08fdba MS |
7808 | |
7809 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
7810 | my_friendly_assert (parms == NULL_TREE |
7811 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 7812 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
7813 | build_x_function_call). */ |
7814 | if (args) | |
7815 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
7816 | my_friendly_assert (ntparms > 0, 292); | |
7817 | ||
830bfa74 MM |
7818 | switch (strict) |
7819 | { | |
7820 | case DEDUCE_CALL: | |
7821 | sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED; | |
7822 | break; | |
7823 | ||
7824 | case DEDUCE_CONV: | |
7825 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
7826 | break; | |
7827 | ||
7828 | case DEDUCE_EXACT: | |
7829 | sub_strict = UNIFY_ALLOW_NONE; | |
7830 | break; | |
7831 | ||
7832 | default: | |
7833 | my_friendly_abort (0); | |
7834 | } | |
7835 | ||
8d08fdba MS |
7836 | while (parms |
7837 | && parms != void_list_node | |
7838 | && args | |
7839 | && args != void_list_node) | |
7840 | { | |
7841 | parm = TREE_VALUE (parms); | |
7842 | parms = TREE_CHAIN (parms); | |
7843 | arg = TREE_VALUE (args); | |
7844 | args = TREE_CHAIN (args); | |
7845 | ||
7846 | if (arg == error_mark_node) | |
7847 | return 1; | |
7848 | if (arg == unknown_type_node) | |
34016c81 JM |
7849 | /* We can't deduce anything from this, but we might get all the |
7850 | template args from other function args. */ | |
7851 | continue; | |
b7484fbe | 7852 | |
03e70705 JM |
7853 | /* Conversions will be performed on a function argument that |
7854 | corresponds with a function parameter that contains only | |
7855 | non-deducible template parameters and explicitly specified | |
7856 | template parameters. */ | |
7857 | if (! uses_template_parms (parm)) | |
b7484fbe | 7858 | { |
03e70705 JM |
7859 | tree type; |
7860 | ||
7861 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
7862 | type = TREE_TYPE (arg); | |
7863 | else | |
7864 | { | |
7865 | type = arg; | |
7866 | arg = NULL_TREE; | |
7867 | } | |
7868 | ||
830bfa74 | 7869 | if (strict == DEDUCE_EXACT) |
03e70705 | 7870 | { |
3bfdc719 | 7871 | if (same_type_p (parm, type)) |
03e70705 JM |
7872 | continue; |
7873 | } | |
03e70705 | 7874 | else |
343c89cd JM |
7875 | /* It might work; we shouldn't check now, because we might |
7876 | get into infinite recursion. Overload resolution will | |
7877 | handle it. */ | |
7878 | continue; | |
03e70705 | 7879 | |
b7484fbe MS |
7880 | return 1; |
7881 | } | |
7882 | ||
8d08fdba MS |
7883 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') |
7884 | { | |
7885 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
34016c81 | 7886 | if (type_unknown_p (arg)) |
28cbf42c | 7887 | { |
34016c81 JM |
7888 | /* [temp.deduct.type] A template-argument can be deduced from |
7889 | a pointer to function or pointer to member function | |
7890 | argument if the set of overloaded functions does not | |
7891 | contain function templates and at most one of a set of | |
7892 | overloaded functions provides a unique match. */ | |
7893 | ||
7894 | if (resolve_overloaded_unification | |
4393e105 | 7895 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
7896 | != 0) |
7897 | return 1; | |
7898 | continue; | |
28cbf42c | 7899 | } |
8d08fdba MS |
7900 | arg = TREE_TYPE (arg); |
7901 | } | |
4393e105 | 7902 | |
830bfa74 MM |
7903 | if (!subr) |
7904 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
db5ae43f | 7905 | |
4393e105 | 7906 | switch (unify (tparms, targs, parm, arg, sub_strict)) |
8d08fdba MS |
7907 | { |
7908 | case 0: | |
7909 | break; | |
7910 | case 1: | |
7911 | return 1; | |
7912 | } | |
7913 | } | |
7914 | /* Fail if we've reached the end of the parm list, and more args | |
7915 | are present, and the parm list isn't variadic. */ | |
7916 | if (args && args != void_list_node && parms == void_list_node) | |
7917 | return 1; | |
7918 | /* Fail if parms are left and they don't have default values. */ | |
7919 | if (parms | |
7920 | && parms != void_list_node | |
7921 | && TREE_PURPOSE (parms) == NULL_TREE) | |
7922 | return 1; | |
7923 | if (!subr) | |
7924 | for (i = 0; i < ntparms; i++) | |
050367a3 | 7925 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 7926 | { |
386b8a85 | 7927 | if (!allow_incomplete) |
8251199e | 7928 | error ("incomplete type unification"); |
8d08fdba MS |
7929 | return 2; |
7930 | } | |
7931 | return 0; | |
7932 | } | |
7933 | ||
34016c81 JM |
7934 | /* Subroutine of type_unification_real. Args are like the variables at the |
7935 | call site. ARG is an overloaded function (or template-id); we try | |
7936 | deducing template args from each of the overloads, and if only one | |
7937 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
7938 | ||
7939 | static int | |
7940 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
4393e105 | 7941 | sub_strict) |
34016c81 JM |
7942 | tree tparms, targs, parm, arg; |
7943 | unification_kind_t strict; | |
7944 | int sub_strict; | |
34016c81 JM |
7945 | { |
7946 | tree tempargs = copy_node (targs); | |
7947 | int good = 0; | |
7948 | ||
7949 | if (TREE_CODE (arg) == ADDR_EXPR) | |
7950 | arg = TREE_OPERAND (arg, 0); | |
9f3d9e46 | 7951 | |
d8f8dca1 MM |
7952 | if (TREE_CODE (arg) == COMPONENT_REF) |
7953 | /* Handle `&x' where `x' is some static or non-static member | |
7954 | function name. */ | |
7955 | arg = TREE_OPERAND (arg, 1); | |
7956 | ||
05e0b2f4 JM |
7957 | if (TREE_CODE (arg) == OFFSET_REF) |
7958 | arg = TREE_OPERAND (arg, 1); | |
7959 | ||
9f3d9e46 JM |
7960 | /* Strip baselink information. */ |
7961 | while (TREE_CODE (arg) == TREE_LIST) | |
7962 | arg = TREE_VALUE (arg); | |
7963 | ||
34016c81 JM |
7964 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
7965 | { | |
7966 | /* If we got some explicit template args, we need to plug them into | |
7967 | the affected templates before we try to unify, in case the | |
7968 | explicit args will completely resolve the templates in question. */ | |
7969 | ||
7970 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
7971 | arg = TREE_OPERAND (arg, 0); | |
7972 | ||
7973 | for (; arg; arg = OVL_NEXT (arg)) | |
7974 | { | |
7975 | tree fn = OVL_CURRENT (arg); | |
7976 | tree subargs, elem; | |
7977 | ||
7978 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
7979 | continue; | |
7980 | ||
7981 | subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs); | |
7982 | if (subargs) | |
7983 | { | |
4393e105 MM |
7984 | elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0, |
7985 | NULL_TREE); | |
d8f8dca1 MM |
7986 | if (TREE_CODE (elem) == METHOD_TYPE) |
7987 | elem = build_ptrmemfunc_type (build_pointer_type (elem)); | |
e97e5263 | 7988 | good += try_one_overload (tparms, targs, tempargs, parm, elem, |
4393e105 | 7989 | strict, sub_strict); |
34016c81 JM |
7990 | } |
7991 | } | |
7992 | } | |
7993 | else if (TREE_CODE (arg) == OVERLOAD) | |
7994 | { | |
7995 | for (; arg; arg = OVL_NEXT (arg)) | |
d8f8dca1 MM |
7996 | { |
7997 | tree type = TREE_TYPE (OVL_CURRENT (arg)); | |
7998 | if (TREE_CODE (type) == METHOD_TYPE) | |
7999 | type = build_ptrmemfunc_type (build_pointer_type (type)); | |
8000 | good += try_one_overload (tparms, targs, tempargs, parm, | |
8001 | type, | |
4393e105 | 8002 | strict, sub_strict); |
d8f8dca1 | 8003 | } |
34016c81 JM |
8004 | } |
8005 | else | |
8006 | my_friendly_abort (981006); | |
8007 | ||
8008 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
8009 | to function or pointer to member function argument if the set of | |
8010 | overloaded functions does not contain function templates and at most | |
8011 | one of a set of overloaded functions provides a unique match. | |
8012 | ||
8013 | So if we found multiple possibilities, we return success but don't | |
8014 | deduce anything. */ | |
8015 | ||
8016 | if (good == 1) | |
8017 | { | |
8018 | int i = TREE_VEC_LENGTH (targs); | |
8019 | for (; i--; ) | |
8020 | if (TREE_VEC_ELT (tempargs, i)) | |
8021 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
8022 | } | |
8023 | if (good) | |
8024 | return 0; | |
8025 | ||
8026 | return 1; | |
8027 | } | |
8028 | ||
8029 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
8030 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
8031 | different overloads deduce different arguments for a given parm. | |
8032 | Returns 1 on success. */ | |
8033 | ||
8034 | static int | |
e97e5263 | 8035 | try_one_overload (tparms, orig_targs, targs, parm, arg, strict, |
4393e105 | 8036 | sub_strict) |
e97e5263 | 8037 | tree tparms, orig_targs, targs, parm, arg; |
34016c81 JM |
8038 | unification_kind_t strict; |
8039 | int sub_strict; | |
34016c81 JM |
8040 | { |
8041 | int nargs; | |
8042 | tree tempargs; | |
8043 | int i; | |
8044 | ||
8045 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
8046 | to function or pointer to member function argument if the set of | |
8047 | overloaded functions does not contain function templates and at most | |
8048 | one of a set of overloaded functions provides a unique match. | |
8049 | ||
8050 | So if this is a template, just return success. */ | |
8051 | ||
8052 | if (uses_template_parms (arg)) | |
8053 | return 1; | |
8054 | ||
8055 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
8056 | ||
8057 | /* We don't copy orig_targs for this because if we have already deduced | |
8058 | some template args from previous args, unify would complain when we | |
8059 | try to deduce a template parameter for the same argument, even though | |
8060 | there isn't really a conflict. */ | |
8061 | nargs = TREE_VEC_LENGTH (targs); | |
8062 | tempargs = make_scratch_vec (nargs); | |
8063 | ||
4393e105 | 8064 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
8065 | return 0; |
8066 | ||
8067 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 8068 | explicitly specified args. */ |
34016c81 JM |
8069 | for (i = nargs; i--; ) |
8070 | { | |
8071 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 8072 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 JM |
8073 | |
8074 | if (elt == NULL_TREE) | |
8075 | continue; | |
8076 | else if (uses_template_parms (elt)) | |
8077 | { | |
8078 | /* Since we're unifying against ourselves, we will fill in template | |
8079 | args used in the function parm list with our own template parms. | |
8080 | Discard them. */ | |
8081 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
8082 | continue; | |
8083 | } | |
8084 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
8085 | return 0; | |
8086 | } | |
8087 | ||
8088 | for (i = nargs; i--; ) | |
8089 | { | |
8090 | tree elt = TREE_VEC_ELT (tempargs, i); | |
8091 | ||
8092 | if (elt) | |
8093 | TREE_VEC_ELT (targs, i) = elt; | |
8094 | } | |
8095 | ||
8096 | return 1; | |
8097 | } | |
8098 | ||
4393e105 MM |
8099 | /* PARM is a template class (perhaps with unbound template |
8100 | parameters). ARG is a fully instantiated type. If ARG can be | |
8101 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
8102 | TARGS are as for unify. */ | |
fcfb9f96 MM |
8103 | |
8104 | static tree | |
4393e105 | 8105 | try_class_unification (tparms, targs, parm, arg) |
fcfb9f96 MM |
8106 | tree tparms; |
8107 | tree targs; | |
4393e105 MM |
8108 | tree parm; |
8109 | tree arg; | |
8110 | { | |
8111 | int i; | |
8112 | tree copy_of_targs; | |
8113 | ||
8114 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
8115 | || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) | |
8116 | return NULL_TREE; | |
8117 | ||
8118 | /* We need to make a new template argument vector for the call to | |
8119 | unify. If we used TARGS, we'd clutter it up with the result of | |
8120 | the attempted unification, even if this class didn't work out. | |
8121 | We also don't want to commit ourselves to all the unifications | |
8122 | we've already done, since unification is supposed to be done on | |
8123 | an argument-by-argument basis. In other words, consider the | |
8124 | following pathological case: | |
8125 | ||
8126 | template <int I, int J, int K> | |
8127 | struct S {}; | |
8128 | ||
8129 | template <int I, int J> | |
8130 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
8131 | ||
8132 | template <int I, int J, int K> | |
8133 | void f(S<I, J, K>, S<I, I, I>); | |
8134 | ||
8135 | void g() { | |
8136 | S<0, 0, 0> s0; | |
8137 | S<0, 1, 2> s2; | |
8138 | ||
8139 | f(s0, s2); | |
8140 | } | |
8141 | ||
8142 | Now, by the time we consider the unification involving `s2', we | |
8143 | already know that we must have `f<0, 0, 0>'. But, even though | |
8144 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal | |
8145 | because there are two ways to unify base classes of S<0, 1, 2> | |
8146 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
8147 | would reject the possibility I=1. */ | |
8148 | push_momentary (); | |
8149 | copy_of_targs = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
8150 | i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), | |
8151 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE); | |
8152 | pop_momentary (); | |
8153 | ||
8154 | /* If unification failed, we're done. */ | |
8155 | if (i != 0) | |
8156 | return NULL_TREE; | |
8157 | else | |
8158 | return arg; | |
8159 | } | |
8160 | ||
8161 | /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we | |
8162 | have alreay discovered to be satisfactory. ARG_BINFO is the binfo | |
8163 | for the base class of ARG that we are currently examining. */ | |
8164 | ||
8165 | static tree | |
8166 | get_template_base_recursive (tparms, targs, parm, | |
8167 | arg_binfo, rval, flags) | |
8168 | tree tparms; | |
8169 | tree targs; | |
8170 | tree arg_binfo; | |
fcfb9f96 | 8171 | tree rval; |
4393e105 MM |
8172 | tree parm; |
8173 | int flags; | |
fcfb9f96 MM |
8174 | { |
8175 | tree binfos; | |
8176 | int i, n_baselinks; | |
4393e105 | 8177 | tree arg = BINFO_TYPE (arg_binfo); |
fcfb9f96 | 8178 | |
4393e105 | 8179 | if (!(flags & GTB_IGNORE_TYPE)) |
fcfb9f96 | 8180 | { |
4393e105 MM |
8181 | tree r = try_class_unification (tparms, targs, |
8182 | parm, arg); | |
fcfb9f96 | 8183 | |
4393e105 | 8184 | /* If there is more than one satisfactory baseclass, then: |
fcfb9f96 | 8185 | |
4393e105 MM |
8186 | [temp.deduct.call] |
8187 | ||
8188 | If they yield more than one possible deduced A, the type | |
8189 | deduction fails. | |
8190 | ||
8191 | applies. */ | |
8192 | if (r && rval && !same_type_p (r, rval)) | |
8193 | return error_mark_node; | |
8194 | else if (r) | |
8195 | rval = r; | |
fcfb9f96 MM |
8196 | } |
8197 | ||
4393e105 | 8198 | binfos = BINFO_BASETYPES (arg_binfo); |
fcfb9f96 MM |
8199 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; |
8200 | ||
8201 | /* Process base types. */ | |
8202 | for (i = 0; i < n_baselinks; i++) | |
8203 | { | |
8204 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
4393e105 | 8205 | int this_virtual; |
fcfb9f96 | 8206 | |
4393e105 MM |
8207 | /* Skip this base, if we've already seen it. */ |
8208 | if (BINFO_MARKED (base_binfo)) | |
8209 | continue; | |
8210 | ||
8211 | this_virtual = | |
8212 | (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); | |
8213 | ||
8214 | /* When searching for a non-virtual, we cannot mark virtually | |
8215 | found binfos. */ | |
8216 | if (! this_virtual) | |
8217 | SET_BINFO_MARKED (base_binfo); | |
8218 | ||
8219 | rval = get_template_base_recursive (tparms, targs, | |
8220 | parm, | |
8221 | base_binfo, | |
8222 | rval, | |
8223 | GTB_VIA_VIRTUAL * this_virtual); | |
8224 | ||
8225 | /* If we discovered more than one matching base class, we can | |
8226 | stop now. */ | |
8227 | if (rval == error_mark_node) | |
8228 | return error_mark_node; | |
fcfb9f96 MM |
8229 | } |
8230 | ||
8231 | return rval; | |
8232 | } | |
8233 | ||
4393e105 MM |
8234 | /* Given a template type PARM and a class type ARG, find the unique |
8235 | base type in ARG that is an instance of PARM. We do not examine | |
8236 | ARG itself; only its base-classes. If there is no appropriate base | |
8237 | class, return NULL_TREE. If there is more than one, return | |
8238 | error_mark_node. PARM may be the type of a partial specialization, | |
8239 | as well as a plain template type. Used by unify. */ | |
fcfb9f96 MM |
8240 | |
8241 | static tree | |
4393e105 | 8242 | get_template_base (tparms, targs, parm, arg) |
fcfb9f96 MM |
8243 | tree tparms; |
8244 | tree targs; | |
4393e105 MM |
8245 | tree parm; |
8246 | tree arg; | |
fcfb9f96 | 8247 | { |
4393e105 MM |
8248 | tree rval; |
8249 | tree arg_binfo; | |
fcfb9f96 | 8250 | |
4393e105 MM |
8251 | my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); |
8252 | ||
8253 | arg_binfo = TYPE_BINFO (complete_type (arg)); | |
8254 | rval = get_template_base_recursive (tparms, targs, | |
8255 | parm, arg_binfo, | |
8256 | NULL_TREE, | |
8257 | GTB_IGNORE_TYPE); | |
fcfb9f96 | 8258 | |
4393e105 MM |
8259 | /* Since get_template_base_recursive marks the bases classes, we |
8260 | must unmark them here. */ | |
d6479fe7 | 8261 | dfs_walk (arg_binfo, dfs_unmark, markedp, 0); |
fcfb9f96 MM |
8262 | |
8263 | return rval; | |
8264 | } | |
8265 | ||
db2767b6 MM |
8266 | /* Returns the level of DECL, which declares a template parameter. */ |
8267 | ||
e9659ab0 | 8268 | static int |
db2767b6 MM |
8269 | template_decl_level (decl) |
8270 | tree decl; | |
8271 | { | |
8272 | switch (TREE_CODE (decl)) | |
8273 | { | |
8274 | case TYPE_DECL: | |
8275 | case TEMPLATE_DECL: | |
8276 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
8277 | ||
8278 | case PARM_DECL: | |
8279 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
8280 | ||
8281 | default: | |
8282 | my_friendly_abort (0); | |
5ffe581d | 8283 | return 0; |
db2767b6 MM |
8284 | } |
8285 | } | |
8286 | ||
830bfa74 MM |
8287 | /* Decide whether ARG can be unified with PARM, considering only the |
8288 | cv-qualifiers of each type, given STRICT as documented for unify. | |
8289 | Returns non-zero iff the unification is OK on that basis.*/ | |
e92cc029 | 8290 | |
e9659ab0 | 8291 | static int |
830bfa74 MM |
8292 | check_cv_quals_for_unify (strict, arg, parm) |
8293 | int strict; | |
8294 | tree arg; | |
8295 | tree parm; | |
8296 | { | |
ef637255 MM |
8297 | if (!(strict & UNIFY_ALLOW_MORE_CV_QUAL) |
8298 | && !at_least_as_qualified_p (arg, parm)) | |
8299 | return 0; | |
8300 | ||
8301 | if (!(strict & UNIFY_ALLOW_LESS_CV_QUAL) | |
8302 | && !at_least_as_qualified_p (parm, arg)) | |
8303 | return 0; | |
8304 | ||
ef637255 | 8305 | return 1; |
830bfa74 MM |
8306 | } |
8307 | ||
8308 | /* Takes parameters as for type_unification. Returns 0 if the | |
8309 | type deduction suceeds, 1 otherwise. The parameter STRICT is a | |
8310 | bitwise or of the following flags: | |
8311 | ||
8312 | UNIFY_ALLOW_NONE: | |
8313 | Require an exact match between PARM and ARG. | |
8314 | UNIFY_ALLOW_MORE_CV_QUAL: | |
8315 | Allow the deduced ARG to be more cv-qualified than ARG. | |
8316 | UNIFY_ALLOW_LESS_CV_QUAL: | |
8317 | Allow the deduced ARG to be less cv-qualified than ARG. | |
8318 | UNIFY_ALLOW_DERIVED: | |
8319 | Allow the deduced ARG to be a template base class of ARG, | |
8320 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
8321 | ARG. |
8322 | UNIFY_ALLOW_INTEGER: | |
8323 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
8324 | case for more information. */ | |
830bfa74 | 8325 | |
e9659ab0 | 8326 | static int |
4393e105 | 8327 | unify (tparms, targs, parm, arg, strict) |
050367a3 | 8328 | tree tparms, targs, parm, arg; |
830bfa74 | 8329 | int strict; |
8d08fdba MS |
8330 | { |
8331 | int idx; | |
050367a3 | 8332 | tree targ; |
db2767b6 | 8333 | tree tparm; |
8d08fdba MS |
8334 | |
8335 | /* I don't think this will do the right thing with respect to types. | |
8336 | But the only case I've seen it in so far has been array bounds, where | |
8337 | signedness is the only information lost, and I think that will be | |
8338 | okay. */ | |
8339 | while (TREE_CODE (parm) == NOP_EXPR) | |
8340 | parm = TREE_OPERAND (parm, 0); | |
8341 | ||
8342 | if (arg == error_mark_node) | |
8343 | return 1; | |
8344 | if (arg == unknown_type_node) | |
34016c81 JM |
8345 | /* We can't deduce anything from this, but we might get all the |
8346 | template args from other function args. */ | |
8347 | return 0; | |
8348 | ||
db2767b6 | 8349 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 8350 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
8351 | template parameters. We might need them if we're trying to |
8352 | figure out which of two things is more specialized. */ | |
8353 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
8354 | return 0; |
8355 | ||
830bfa74 MM |
8356 | /* Immediately reject some pairs that won't unify because of |
8357 | cv-qualification mismatches. */ | |
8358 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
49432171 | 8359 | && TREE_CODE_CLASS (TREE_CODE (arg)) == 't' |
830bfa74 MM |
8360 | /* We check the cv-qualifiers when unifying with template type |
8361 | parameters below. We want to allow ARG `const T' to unify with | |
8362 | PARM `T' for example, when computing which of two templates | |
8363 | is more specialized, for example. */ | |
8364 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
8365 | && !check_cv_quals_for_unify (strict, arg, parm)) | |
49432171 JM |
8366 | return 1; |
8367 | ||
8d08fdba MS |
8368 | switch (TREE_CODE (parm)) |
8369 | { | |
2ca340ae JM |
8370 | case TYPENAME_TYPE: |
8371 | /* In a type which contains a nested-name-specifier, template | |
8372 | argument values cannot be deduced for template parameters used | |
8373 | within the nested-name-specifier. */ | |
8374 | return 0; | |
8375 | ||
8d08fdba | 8376 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 8377 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
8378 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8379 | ||
8380 | if (TEMPLATE_TYPE_LEVEL (parm) | |
8381 | != template_decl_level (tparm)) | |
8382 | /* The PARM is not one we're trying to unify. Just check | |
8383 | to see if it matches ARG. */ | |
8384 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 8385 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 8386 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 8387 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8388 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 8389 | |
73b0fce8 | 8390 | /* Check for mixed types and values. */ |
db2767b6 MM |
8391 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
8392 | && TREE_CODE (tparm) != TYPE_DECL) | |
8393 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
8394 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
8395 | return 1; |
8396 | ||
db2767b6 | 8397 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 8398 | { |
7ddedda4 | 8399 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)) |
db2767b6 MM |
8400 | { |
8401 | /* We arrive here when PARM does not involve template | |
8402 | specialization. */ | |
73b0fce8 | 8403 | |
db2767b6 MM |
8404 | /* ARG must be constructed from a template class. */ |
8405 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
8406 | return 1; | |
73b0fce8 | 8407 | |
db2767b6 | 8408 | { |
7ddedda4 MM |
8409 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); |
8410 | tree parmvec = TYPE_TI_ARGS (parm); | |
db2767b6 MM |
8411 | tree argvec = CLASSTYPE_TI_ARGS (arg); |
8412 | tree argtmplvec | |
8413 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
8414 | int i; | |
8415 | ||
8416 | /* The parameter and argument roles have to be switched here | |
8417 | in order to handle default arguments properly. For example, | |
8418 | template<template <class> class TT> void f(TT<int>) | |
8419 | should be able to accept vector<int> which comes from | |
744fac59 | 8420 | template <class T, class Allocator = allocator> |
73b0fce8 KL |
8421 | class vector. */ |
8422 | ||
fcfcdfc8 | 8423 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) |
db2767b6 MM |
8424 | == error_mark_node) |
8425 | return 1; | |
73b0fce8 | 8426 | |
6b9b6b15 JM |
8427 | /* Deduce arguments T, i from TT<T> or TT<i>. |
8428 | We check each element of PARMVEC and ARGVEC individually | |
8429 | rather than the whole TREE_VEC since they can have | |
8430 | different number of elements. */ | |
8431 | ||
db2767b6 MM |
8432 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
8433 | { | |
8434 | tree t = TREE_VEC_ELT (parmvec, i); | |
73b0fce8 | 8435 | |
830bfa74 MM |
8436 | if (unify (tparms, targs, t, |
8437 | TREE_VEC_ELT (argvec, i), | |
4393e105 | 8438 | UNIFY_ALLOW_NONE)) |
db2767b6 MM |
8439 | return 1; |
8440 | } | |
73b0fce8 | 8441 | } |
db2767b6 MM |
8442 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
8443 | } | |
8444 | } | |
8445 | else | |
8446 | { | |
830bfa74 MM |
8447 | /* If PARM is `const T' and ARG is only `int', we don't have |
8448 | a match unless we are allowing additional qualification. | |
8449 | If ARG is `const int' and PARM is just `T' that's OK; | |
8450 | that binds `const int' to `T'. */ | |
8451 | if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, | |
8452 | arg, parm)) | |
db2767b6 MM |
8453 | return 1; |
8454 | ||
830bfa74 MM |
8455 | /* Consider the case where ARG is `const volatile int' and |
8456 | PARM is `const T'. Then, T should be `volatile int'. */ | |
8457 | arg = | |
adecb3f4 MM |
8458 | cp_build_qualified_type_real (arg, |
8459 | CP_TYPE_QUALS (arg) | |
8460 | & ~CP_TYPE_QUALS (parm), | |
8461 | /*complain=*/0); | |
8462 | if (arg == error_mark_node) | |
8463 | return 1; | |
73b0fce8 KL |
8464 | } |
8465 | ||
8466 | /* Simple cases: Value already set, does match or doesn't. */ | |
4393e105 | 8467 | if (targ != NULL_TREE && same_type_p (targ, arg)) |
73b0fce8 | 8468 | return 0; |
050367a3 | 8469 | else if (targ) |
73b0fce8 | 8470 | return 1; |
61cd552e MM |
8471 | |
8472 | /* Make sure that ARG is not a variable-sized array. (Note that | |
8473 | were talking about variable-sized arrays (like `int[n]'), | |
8474 | rather than arrays of unknown size (like `int[]').) We'll | |
8475 | get very confused by such a type since the bound of the array | |
8476 | will not be computable in an instantiation. Besides, such | |
8477 | types are not allowed in ISO C++, so we can do as we please | |
8478 | here. */ | |
8479 | if (TREE_CODE (arg) == ARRAY_TYPE | |
8480 | && !uses_template_parms (arg) | |
8481 | && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg))) | |
8482 | != INTEGER_CST)) | |
8483 | return 1; | |
8484 | ||
050367a3 | 8485 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
8486 | return 0; |
8487 | ||
f84b4be9 | 8488 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
8489 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8490 | ||
8491 | if (TEMPLATE_PARM_LEVEL (parm) | |
8492 | != template_decl_level (tparm)) | |
8493 | /* The PARM is not one we're trying to unify. Just check | |
8494 | to see if it matches ARG. */ | |
8495 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
67ffc812 | 8496 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; |
db2767b6 | 8497 | |
f84b4be9 | 8498 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 8499 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8500 | |
050367a3 | 8501 | if (targ) |
8d08fdba | 8502 | { |
67ffc812 | 8503 | int i = (cp_tree_equal (targ, arg) > 0); |
312e7d50 JM |
8504 | if (i == 1) |
8505 | return 0; | |
8506 | else if (i == 0) | |
8507 | return 1; | |
8508 | else | |
8509 | my_friendly_abort (42); | |
8d08fdba | 8510 | } |
8d08fdba | 8511 | |
161c12b0 JM |
8512 | /* [temp.deduct.type] If, in the declaration of a function template |
8513 | with a non-type template-parameter, the non-type | |
8514 | template-parameter is used in an expression in the function | |
8515 | parameter-list and, if the corresponding template-argument is | |
8516 | deduced, the template-argument type shall match the type of the | |
8517 | template-parameter exactly, except that a template-argument | |
8518 | deduced from an array bound may be of any integral type. */ | |
8519 | if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm))) | |
8520 | /* OK */; | |
8521 | else if ((strict & UNIFY_ALLOW_INTEGER) | |
8522 | && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE | |
8523 | || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE)) | |
8524 | /* OK */; | |
8525 | else | |
8526 | return 1; | |
8527 | ||
050367a3 | 8528 | TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg); |
8d08fdba MS |
8529 | return 0; |
8530 | ||
8531 | case POINTER_TYPE: | |
830bfa74 MM |
8532 | { |
8533 | int sub_strict; | |
4ac14744 | 8534 | |
830bfa74 MM |
8535 | if (TREE_CODE (arg) != POINTER_TYPE) |
8536 | return 1; | |
8537 | ||
8538 | /* [temp.deduct.call] | |
8539 | ||
8540 | A can be another pointer or pointer to member type that can | |
8541 | be converted to the deduced A via a qualification | |
8542 | conversion (_conv.qual_). | |
8543 | ||
8544 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
8545 | This will allow for additional cv-qualification of the | |
8546 | pointed-to types if appropriate. In general, this is a bit | |
8547 | too generous; we are only supposed to allow qualification | |
8548 | conversions and this method will allow an ARG of char** and | |
8549 | a deduced ARG of const char**. However, overload | |
8550 | resolution will subsequently invalidate the candidate, so | |
8551 | this is probably OK. */ | |
8552 | sub_strict = strict; | |
8553 | ||
a7a64a77 | 8554 | if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE) |
830bfa74 MM |
8555 | /* The derived-to-base conversion only persists through one |
8556 | level of pointers. */ | |
8557 | sub_strict &= ~UNIFY_ALLOW_DERIVED; | |
8558 | ||
a7a64a77 MM |
8559 | return unify (tparms, targs, TREE_TYPE (parm), |
8560 | TREE_TYPE (arg), sub_strict); | |
830bfa74 | 8561 | } |
8d08fdba MS |
8562 | |
8563 | case REFERENCE_TYPE: | |
830bfa74 MM |
8564 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
8565 | return 1; | |
8566 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
4393e105 | 8567 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8568 | |
8569 | case ARRAY_TYPE: | |
8570 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
8571 | return 1; | |
3042d5be MM |
8572 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
8573 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
8574 | return 1; | |
8575 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
830bfa74 | 8576 | && unify (tparms, targs, TYPE_DOMAIN (parm), |
4393e105 | 8577 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) |
8d08fdba | 8578 | return 1; |
830bfa74 | 8579 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8580 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8581 | |
8582 | case REAL_TYPE: | |
37c46b43 | 8583 | case COMPLEX_TYPE: |
8d08fdba | 8584 | case INTEGER_TYPE: |
42976354 | 8585 | case BOOLEAN_TYPE: |
5ad5a526 | 8586 | case VOID_TYPE: |
f376e137 MS |
8587 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8588 | return 1; | |
8589 | ||
514a1f18 JM |
8590 | if (TREE_CODE (parm) == INTEGER_TYPE |
8591 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
8d08fdba MS |
8592 | { |
8593 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
830bfa74 | 8594 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), |
4393e105 | 8595 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8596 | return 1; |
8597 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
830bfa74 | 8598 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), |
4393e105 | 8599 | TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8600 | return 1; |
8601 | } | |
514a1f18 JM |
8602 | /* We use the TYPE_MAIN_VARIANT since we have already |
8603 | checked cv-qualification at the top of the | |
8604 | function. */ | |
3bfdc719 MM |
8605 | else if (!same_type_p (TYPE_MAIN_VARIANT (arg), |
8606 | TYPE_MAIN_VARIANT (parm))) | |
ca79f85d JM |
8607 | return 1; |
8608 | ||
8d08fdba MS |
8609 | /* As far as unification is concerned, this wins. Later checks |
8610 | will invalidate it if necessary. */ | |
8611 | return 0; | |
8612 | ||
8613 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 8614 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 8615 | case INTEGER_CST: |
bd6dd845 MS |
8616 | while (TREE_CODE (arg) == NOP_EXPR) |
8617 | arg = TREE_OPERAND (arg, 0); | |
8618 | ||
8d08fdba MS |
8619 | if (TREE_CODE (arg) != INTEGER_CST) |
8620 | return 1; | |
8621 | return !tree_int_cst_equal (parm, arg); | |
8622 | ||
8d08fdba MS |
8623 | case TREE_VEC: |
8624 | { | |
8625 | int i; | |
8626 | if (TREE_CODE (arg) != TREE_VEC) | |
8627 | return 1; | |
8628 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
8629 | return 1; | |
8630 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
830bfa74 | 8631 | if (unify (tparms, targs, |
8d08fdba | 8632 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 8633 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
8634 | return 1; |
8635 | return 0; | |
8636 | } | |
8637 | ||
8d08fdba | 8638 | case RECORD_TYPE: |
f181d4ae | 8639 | case UNION_TYPE: |
f181d4ae | 8640 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 8641 | return 1; |
830bfa74 | 8642 | |
a7a64a77 MM |
8643 | if (TYPE_PTRMEMFUNC_P (parm)) |
8644 | { | |
8645 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
8646 | return 1; | |
8647 | ||
8648 | return unify (tparms, targs, | |
8649 | TYPE_PTRMEMFUNC_FN_TYPE (parm), | |
8650 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
8651 | strict); | |
8652 | } | |
8653 | ||
5db698f6 | 8654 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 8655 | { |
6467930b | 8656 | tree t = NULL_TREE; |
4393e105 | 8657 | |
830bfa74 | 8658 | if (strict & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
8659 | { |
8660 | /* First, we try to unify the PARM and ARG directly. */ | |
8661 | t = try_class_unification (tparms, targs, | |
8662 | parm, arg); | |
8663 | ||
8664 | if (!t) | |
8665 | { | |
8666 | /* Fallback to the special case allowed in | |
8667 | [temp.deduct.call]: | |
8668 | ||
8669 | If P is a class, and P has the form | |
8670 | template-id, then A can be a derived class of | |
8671 | the deduced A. Likewise, if P is a pointer to | |
8672 | a class of the form template-id, A can be a | |
8673 | pointer to a derived class pointed to by the | |
8674 | deduced A. */ | |
8675 | t = get_template_base (tparms, targs, | |
8676 | parm, arg); | |
8677 | ||
8678 | if (! t || t == error_mark_node) | |
8679 | return 1; | |
8680 | } | |
8681 | } | |
6df47b06 | 8682 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
8683 | && (CLASSTYPE_TI_TEMPLATE (parm) |
8684 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
8685 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
8686 | Then, we should unify `int' and `U'. */ | |
6467930b | 8687 | t = arg; |
4393e105 MM |
8688 | else |
8689 | /* There's no chance of unication succeeding. */ | |
5566b478 | 8690 | return 1; |
6467930b | 8691 | |
830bfa74 | 8692 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 8693 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 8694 | } |
3bfdc719 MM |
8695 | else if (!same_type_p (TYPE_MAIN_VARIANT (parm), |
8696 | TYPE_MAIN_VARIANT (arg))) | |
5566b478 | 8697 | return 1; |
a4443a08 | 8698 | return 0; |
8d08fdba MS |
8699 | |
8700 | case METHOD_TYPE: | |
8d08fdba | 8701 | case FUNCTION_TYPE: |
830bfa74 | 8702 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 8703 | return 1; |
830bfa74 MM |
8704 | |
8705 | if (unify (tparms, targs, TREE_TYPE (parm), | |
4393e105 | 8706 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 8707 | return 1; |
386b8a85 | 8708 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 8709 | TYPE_ARG_TYPES (arg), 1, |
4393e105 | 8710 | DEDUCE_EXACT, 0); |
a4443a08 MS |
8711 | |
8712 | case OFFSET_TYPE: | |
8713 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
8714 | return 1; | |
830bfa74 | 8715 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 8716 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 8717 | return 1; |
830bfa74 | 8718 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8719 | strict); |
a4443a08 | 8720 | |
f62dbf03 | 8721 | case CONST_DECL: |
830bfa74 | 8722 | if (arg != decl_constant_value (parm)) |
f62dbf03 JM |
8723 | return 1; |
8724 | return 0; | |
8725 | ||
027905b4 KL |
8726 | case TEMPLATE_DECL: |
8727 | /* Matched cases are handled by the ARG == PARM test above. */ | |
8728 | return 1; | |
8729 | ||
53929c47 JM |
8730 | case MINUS_EXPR: |
8731 | if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST) | |
8732 | { | |
8733 | /* We handle this case specially, since it comes up with | |
8734 | arrays. In particular, something like: | |
8735 | ||
8736 | template <int N> void f(int (&x)[N]); | |
8737 | ||
8738 | Here, we are trying to unify the range type, which | |
8739 | looks like [0 ... (N - 1)]. */ | |
8740 | tree t, t1, t2; | |
8741 | t1 = TREE_OPERAND (parm, 0); | |
8742 | t2 = TREE_OPERAND (parm, 1); | |
8743 | ||
8744 | /* Should this be a regular fold? */ | |
8745 | t = maybe_fold_nontype_arg (build (PLUS_EXPR, | |
8746 | integer_type_node, | |
8747 | arg, t2)); | |
8748 | ||
4393e105 | 8749 | return unify (tparms, targs, t1, t, strict); |
53929c47 JM |
8750 | } |
8751 | /* else fall through */ | |
8752 | ||
8d08fdba | 8753 | default: |
050367a3 | 8754 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
4393e105 MM |
8755 | /* We're looking at an expression. This can happen with |
8756 | something like: | |
8757 | ||
8758 | template <int I> | |
8759 | void foo(S<I>, S<I + 2>); | |
050367a3 | 8760 | |
4393e105 | 8761 | This is a "nondeduced context": |
050367a3 | 8762 | |
4393e105 MM |
8763 | [deduct.type] |
8764 | ||
8765 | The nondeduced contexts are: | |
050367a3 | 8766 | |
4393e105 MM |
8767 | --A type that is a template-id in which one or more of |
8768 | the template-arguments is an expression that references | |
8769 | a template-parameter. | |
050367a3 | 8770 | |
4393e105 MM |
8771 | In these cases, we assume deduction succeeded, but don't |
8772 | actually infer any unifications. */ | |
8773 | return 0; | |
050367a3 | 8774 | else |
b4c4a9ec MM |
8775 | sorry ("use of `%s' in template type unification", |
8776 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 8777 | |
8d08fdba MS |
8778 | return 1; |
8779 | } | |
8780 | } | |
8d08fdba | 8781 | \f |
03d0f4af MM |
8782 | /* Called if RESULT is explicitly instantiated, or is a member of an |
8783 | explicitly instantiated class, or if using -frepo and the | |
8784 | instantiation of RESULT has been assigned to this file. */ | |
8785 | ||
faae18ab | 8786 | void |
5566b478 | 8787 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
8788 | tree result; |
8789 | int extern_p; | |
8790 | { | |
75650646 MM |
8791 | if (TREE_CODE (result) != FUNCTION_DECL) |
8792 | /* The TREE_PUBLIC flag for function declarations will have been | |
8793 | set correctly by tsubst. */ | |
8794 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
8795 | |
8796 | if (! extern_p) | |
8797 | { | |
8798 | DECL_INTERFACE_KNOWN (result) = 1; | |
8799 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 | 8800 | |
1a408d07 JM |
8801 | /* Always make artificials weak. */ |
8802 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
8803 | comdat_linkage (result); | |
a7d87521 JM |
8804 | /* For WIN32 we also want to put explicit instantiations in |
8805 | linkonce sections. */ | |
1a408d07 | 8806 | else if (TREE_PUBLIC (result)) |
b385c841 | 8807 | maybe_make_one_only (result); |
faae18ab | 8808 | } |
f49422da MS |
8809 | else if (TREE_CODE (result) == FUNCTION_DECL) |
8810 | mark_inline_for_output (result); | |
faae18ab MS |
8811 | } |
8812 | ||
e1467ff2 MM |
8813 | /* Given two function templates PAT1 and PAT2, and explicit template |
8814 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
8815 | |
8816 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
8817 | -1 if PAT2 is more specialized than PAT1. | |
8818 | 0 if neither is more specialized. */ | |
8819 | ||
8820 | int | |
e1467ff2 MM |
8821 | more_specialized (pat1, pat2, explicit_args) |
8822 | tree pat1, pat2, explicit_args; | |
6467930b | 8823 | { |
98c1c668 | 8824 | tree targs; |
73aad9b9 | 8825 | int winner = 0; |
6467930b | 8826 | |
4393e105 | 8827 | targs = get_bindings_overload (pat1, DECL_RESULT (pat2), explicit_args); |
73aad9b9 | 8828 | if (targs) |
36a117a5 | 8829 | --winner; |
6467930b | 8830 | |
4393e105 | 8831 | targs = get_bindings_overload (pat2, DECL_RESULT (pat1), explicit_args); |
73aad9b9 | 8832 | if (targs) |
36a117a5 | 8833 | ++winner; |
6467930b | 8834 | |
73aad9b9 JM |
8835 | return winner; |
8836 | } | |
6467930b | 8837 | |
73aad9b9 | 8838 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 8839 | |
73aad9b9 JM |
8840 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
8841 | -1 if PAT2 is more specialized than PAT1. | |
8842 | 0 if neither is more specialized. */ | |
8843 | ||
8844 | int | |
8845 | more_specialized_class (pat1, pat2) | |
8846 | tree pat1, pat2; | |
8847 | { | |
8848 | tree targs; | |
8849 | int winner = 0; | |
8850 | ||
36a117a5 MM |
8851 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
8852 | TREE_PURPOSE (pat2)); | |
73aad9b9 JM |
8853 | if (targs) |
8854 | --winner; | |
8855 | ||
36a117a5 MM |
8856 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
8857 | TREE_PURPOSE (pat1)); | |
73aad9b9 | 8858 | if (targs) |
6467930b MS |
8859 | ++winner; |
8860 | ||
8861 | return winner; | |
8862 | } | |
73aad9b9 JM |
8863 | |
8864 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 8865 | DECL from the function template FN, with the explicit template |
76b9a14d | 8866 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
4393e105 MM |
8867 | also match. Return NULL_TREE if no satisfactory arguments could be |
8868 | found. */ | |
73aad9b9 | 8869 | |
76b9a14d JM |
8870 | static tree |
8871 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 8872 | tree fn, decl, explicit_args; |
76b9a14d | 8873 | int check_rettype; |
73aad9b9 | 8874 | { |
98c1c668 | 8875 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 8876 | tree targs = make_scratch_vec (ntparms); |
4393e105 | 8877 | tree decl_type; |
03017874 | 8878 | tree decl_arg_types; |
98c1c668 JM |
8879 | int i; |
8880 | ||
4393e105 MM |
8881 | /* Substitute the explicit template arguments into the type of DECL. |
8882 | The call to fn_type_unification will handle substitution into the | |
8883 | FN. */ | |
8884 | decl_type = TREE_TYPE (decl); | |
8885 | if (explicit_args && uses_template_parms (decl_type)) | |
8886 | { | |
8887 | tree tmpl; | |
8888 | tree converted_args; | |
8889 | ||
8890 | if (DECL_TEMPLATE_INFO (decl)) | |
8891 | tmpl = DECL_TI_TEMPLATE (decl); | |
8892 | else | |
8893 | /* We can get here for some illegal specializations. */ | |
8894 | return NULL_TREE; | |
8895 | ||
8896 | converted_args | |
8897 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
8898 | explicit_args, NULL_TREE, | |
8899 | /*complain=*/0, | |
8900 | /*require_all_arguments=*/0)); | |
8901 | if (converted_args == error_mark_node) | |
8902 | return NULL_TREE; | |
8903 | ||
8904 | decl_type = tsubst (decl_type, converted_args, /*complain=*/0, | |
8905 | NULL_TREE); | |
8906 | if (decl_type == error_mark_node) | |
8907 | return NULL_TREE; | |
8908 | } | |
8909 | ||
03017874 MM |
8910 | /* If FN is a static member function, adjust the type of DECL |
8911 | appropriately. */ | |
4393e105 | 8912 | decl_arg_types = TYPE_ARG_TYPES (decl_type); |
d7684f2d MM |
8913 | if (DECL_STATIC_FUNCTION_P (fn) |
8914 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 8915 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 8916 | |
e1467ff2 | 8917 | i = fn_type_unification (fn, explicit_args, targs, |
03017874 MM |
8918 | decl_arg_types, |
8919 | TREE_TYPE (decl_type), | |
8920 | DEDUCE_EXACT); | |
98c1c668 | 8921 | |
76b9a14d JM |
8922 | if (i != 0) |
8923 | return NULL_TREE; | |
8924 | ||
8925 | if (check_rettype) | |
e1467ff2 MM |
8926 | { |
8927 | /* Check to see that the resulting return type is also OK. */ | |
36a117a5 | 8928 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs, |
4393e105 | 8929 | /*complain=*/0, NULL_TREE); |
e1467ff2 | 8930 | |
3bfdc719 | 8931 | if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl)))) |
e1467ff2 | 8932 | return NULL_TREE; |
e1467ff2 MM |
8933 | } |
8934 | ||
76b9a14d JM |
8935 | return targs; |
8936 | } | |
8937 | ||
8938 | /* For most uses, we want to check the return type. */ | |
8939 | ||
8940 | tree | |
8941 | get_bindings (fn, decl, explicit_args) | |
8942 | tree fn, decl, explicit_args; | |
8943 | { | |
8944 | return get_bindings_real (fn, decl, explicit_args, 1); | |
8945 | } | |
8946 | ||
8947 | /* But for more_specialized, we only care about the parameter types. */ | |
8948 | ||
8949 | static tree | |
8950 | get_bindings_overload (fn, decl, explicit_args) | |
8951 | tree fn, decl, explicit_args; | |
8952 | { | |
8953 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
8954 | } |
8955 | ||
36a117a5 MM |
8956 | /* Return the innermost template arguments that, when applied to a |
8957 | template specialization whose innermost template parameters are | |
8958 | TPARMS, and whose specialization arguments are ARGS, yield the | |
8959 | ARGS. | |
8960 | ||
8961 | For example, suppose we have: | |
8962 | ||
8963 | template <class T, class U> struct S {}; | |
8964 | template <class T> struct S<T*, int> {}; | |
8965 | ||
8966 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
8967 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
8968 | int}. The resulting vector will be {double}, indicating that `T' | |
8969 | is bound to `double'. */ | |
8970 | ||
bd6dd845 | 8971 | static tree |
36a117a5 MM |
8972 | get_class_bindings (tparms, parms, args) |
8973 | tree tparms, parms, args; | |
73aad9b9 | 8974 | { |
3b3ba9f0 | 8975 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
73aad9b9 JM |
8976 | tree vec = make_temp_vec (ntparms); |
8977 | ||
36a117a5 | 8978 | args = innermost_args (args); |
8d019cef | 8979 | |
4393e105 | 8980 | if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE)) |
fcfb9f96 | 8981 | return NULL_TREE; |
73aad9b9 JM |
8982 | |
8983 | for (i = 0; i < ntparms; ++i) | |
8984 | if (! TREE_VEC_ELT (vec, i)) | |
8985 | return NULL_TREE; | |
8986 | ||
8987 | return vec; | |
8988 | } | |
8989 | ||
104bf76a MM |
8990 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. |
8991 | Pick the most specialized template, and return the corresponding | |
8992 | instantiation, or if there is no corresponding instantiation, the | |
8993 | template itself. EXPLICIT_ARGS is any template arguments explicity | |
8994 | mentioned in a template-id. If there is no most specialized | |
8995 | tempalte, error_mark_node is returned. If there are no templates | |
8996 | at all, NULL_TREE is returned. */ | |
73aad9b9 JM |
8997 | |
8998 | tree | |
104bf76a MM |
8999 | most_specialized_instantiation (instantiations, explicit_args) |
9000 | tree instantiations; | |
9001 | tree explicit_args; | |
73aad9b9 | 9002 | { |
104bf76a | 9003 | tree fn, champ; |
73aad9b9 JM |
9004 | int fate; |
9005 | ||
104bf76a | 9006 | if (!instantiations) |
73aad9b9 JM |
9007 | return NULL_TREE; |
9008 | ||
104bf76a MM |
9009 | champ = instantiations; |
9010 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 9011 | { |
104bf76a MM |
9012 | fate = more_specialized (TREE_VALUE (champ), |
9013 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
9014 | if (fate == 1) |
9015 | ; | |
9016 | else | |
9017 | { | |
9018 | if (fate == 0) | |
9019 | { | |
9020 | fn = TREE_CHAIN (fn); | |
9021 | if (! fn) | |
9022 | return error_mark_node; | |
9023 | } | |
104bf76a | 9024 | champ = fn; |
73aad9b9 JM |
9025 | } |
9026 | } | |
9027 | ||
104bf76a | 9028 | for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 9029 | { |
104bf76a MM |
9030 | fate = more_specialized (TREE_VALUE (champ), |
9031 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
9032 | if (fate != 1) |
9033 | return error_mark_node; | |
9034 | } | |
9035 | ||
104bf76a MM |
9036 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); |
9037 | } | |
9038 | ||
9039 | /* Return the most specialized of the list of templates in FNS that can | |
9040 | produce an instantiation matching DECL, given the explicit template | |
9041 | arguments EXPLICIT_ARGS. */ | |
9042 | ||
e9659ab0 | 9043 | static tree |
104bf76a MM |
9044 | most_specialized (fns, decl, explicit_args) |
9045 | tree fns, decl, explicit_args; | |
9046 | { | |
9047 | tree candidates = NULL_TREE; | |
9048 | tree fn, args; | |
9049 | ||
9050 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
9051 | { | |
9052 | tree candidate = TREE_VALUE (fn); | |
9053 | ||
9054 | args = get_bindings (candidate, decl, explicit_args); | |
9055 | if (args) | |
9056 | candidates = scratch_tree_cons (NULL_TREE, candidate, | |
9057 | candidates); | |
9058 | } | |
9059 | ||
9060 | return most_specialized_instantiation (candidates, explicit_args); | |
73aad9b9 JM |
9061 | } |
9062 | ||
36a117a5 MM |
9063 | /* If DECL is a specialization of some template, return the most |
9064 | general such template. For example, given: | |
9065 | ||
9066 | template <class T> struct S { template <class U> void f(U); }; | |
9067 | ||
9068 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
9069 | the full template. This function will not trace past partial | |
9070 | specializations, however. For example, given in addition: | |
9071 | ||
9072 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
9073 | ||
9074 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
9075 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 9076 | |
e9659ab0 | 9077 | static tree |
36a117a5 MM |
9078 | most_general_template (decl) |
9079 | tree decl; | |
73aad9b9 | 9080 | { |
36a117a5 MM |
9081 | while (DECL_TEMPLATE_INFO (decl)) |
9082 | decl = DECL_TI_TEMPLATE (decl); | |
9083 | ||
9084 | return decl; | |
9085 | } | |
9086 | ||
9087 | /* Return the most specialized of the class template specializations | |
9088 | of TMPL which can produce an instantiation matching ARGS, or | |
9089 | error_mark_node if the choice is ambiguous. */ | |
9090 | ||
e9659ab0 | 9091 | static tree |
36a117a5 MM |
9092 | most_specialized_class (tmpl, args) |
9093 | tree tmpl; | |
9094 | tree args; | |
9095 | { | |
9096 | tree list = NULL_TREE; | |
9097 | tree t; | |
9098 | tree champ; | |
73aad9b9 JM |
9099 | int fate; |
9100 | ||
36a117a5 MM |
9101 | tmpl = most_general_template (tmpl); |
9102 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 9103 | { |
36a117a5 MM |
9104 | tree spec_args |
9105 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
9106 | if (spec_args) | |
73aad9b9 JM |
9107 | { |
9108 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
9109 | TREE_TYPE (list) = TREE_TYPE (t); | |
9110 | } | |
9111 | } | |
9112 | ||
9113 | if (! list) | |
9114 | return NULL_TREE; | |
9115 | ||
9116 | t = list; | |
9117 | champ = t; | |
9118 | t = TREE_CHAIN (t); | |
9119 | for (; t; t = TREE_CHAIN (t)) | |
9120 | { | |
9121 | fate = more_specialized_class (champ, t); | |
9122 | if (fate == 1) | |
9123 | ; | |
9124 | else | |
9125 | { | |
9126 | if (fate == 0) | |
9127 | { | |
9128 | t = TREE_CHAIN (t); | |
9129 | if (! t) | |
9130 | return error_mark_node; | |
9131 | } | |
9132 | champ = t; | |
9133 | } | |
9134 | } | |
9135 | ||
9136 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
9137 | { | |
85b71cf2 | 9138 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
9139 | if (fate != 1) |
9140 | return error_mark_node; | |
9141 | } | |
9142 | ||
9143 | return champ; | |
9144 | } | |
9145 | ||
8d08fdba | 9146 | /* called from the parser. */ |
e92cc029 | 9147 | |
8d08fdba | 9148 | void |
6633d636 | 9149 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 9150 | tree declspecs, declarator, storage; |
8d08fdba | 9151 | { |
c11b6f21 | 9152 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 9153 | tree result = NULL_TREE; |
faae18ab | 9154 | int extern_p = 0; |
e8abc66f | 9155 | |
3fa56191 MM |
9156 | if (!decl) |
9157 | /* An error ocurred, for which grokdeclarator has already issued | |
9158 | an appropriate message. */ | |
9159 | return; | |
9160 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 9161 | { |
8251199e | 9162 | cp_error ("explicit instantiation of non-template `%#D'", decl); |
ec255269 MS |
9163 | return; |
9164 | } | |
03d0f4af | 9165 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 9166 | { |
03d0f4af MM |
9167 | /* There is an asymmetry here in the way VAR_DECLs and |
9168 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
9169 | the latter, the DECL we get back will be marked as a | |
9170 | template instantiation, and the appropriate | |
9171 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
9172 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
9173 | should handle VAR_DECLs as it currently handles | |
9174 | FUNCTION_DECLs. */ | |
6633d636 MS |
9175 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); |
9176 | if (result && TREE_CODE (result) != VAR_DECL) | |
03d0f4af | 9177 | { |
8251199e | 9178 | cp_error ("no matching template for `%D' found", result); |
03d0f4af MM |
9179 | return; |
9180 | } | |
6633d636 MS |
9181 | } |
9182 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
9183 | { | |
8251199e | 9184 | cp_error ("explicit instantiation of `%#D'", decl); |
6633d636 MS |
9185 | return; |
9186 | } | |
03d0f4af MM |
9187 | else |
9188 | result = decl; | |
672476cb | 9189 | |
03d0f4af MM |
9190 | /* Check for various error cases. Note that if the explicit |
9191 | instantiation is legal the RESULT will currently be marked as an | |
9192 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
9193 | until we get here. */ | |
9194 | ||
9195 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 9196 | { |
03d0f4af MM |
9197 | /* [temp.spec] |
9198 | ||
9199 | No program shall both explicitly instantiate and explicitly | |
9200 | specialize a template. */ | |
6f1b4c42 JM |
9201 | cp_pedwarn ("explicit instantiation of `%#D' after", result); |
9202 | cp_pedwarn_at ("explicit specialization here", result); | |
672476cb MM |
9203 | return; |
9204 | } | |
03d0f4af MM |
9205 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
9206 | { | |
9207 | /* [temp.spec] | |
98c1c668 | 9208 | |
03d0f4af MM |
9209 | No program shall explicitly instantiate any template more |
9210 | than once. | |
9211 | ||
2aaf816d JM |
9212 | We check DECL_INTERFACE_KNOWN so as not to complain when the first |
9213 | instantiation was `extern' and the second is not, and EXTERN_P for | |
9214 | the opposite case. If -frepo, chances are we already got marked | |
9215 | as an explicit instantion because of the repo file. */ | |
9216 | if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository) | |
6f1b4c42 | 9217 | cp_pedwarn ("duplicate explicit instantiation of `%#D'", result); |
03d0f4af MM |
9218 | |
9219 | /* If we've already instantiated the template, just return now. */ | |
9220 | if (DECL_INTERFACE_KNOWN (result)) | |
9221 | return; | |
9222 | } | |
9223 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 9224 | { |
8251199e | 9225 | cp_error ("no matching template for `%D' found", result); |
faae18ab MS |
9226 | return; |
9227 | } | |
03d0f4af | 9228 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 9229 | { |
8251199e | 9230 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); |
6633d636 MS |
9231 | return; |
9232 | } | |
9233 | ||
a0a33927 MS |
9234 | if (flag_external_templates) |
9235 | return; | |
9236 | ||
f0e01782 | 9237 | if (storage == NULL_TREE) |
00595019 | 9238 | ; |
faae18ab | 9239 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af MM |
9240 | { |
9241 | if (pedantic) | |
8251199e | 9242 | cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations"); |
03d0f4af MM |
9243 | extern_p = 1; |
9244 | } | |
f0e01782 | 9245 | else |
8251199e | 9246 | cp_error ("storage class `%D' applied to template instantiation", |
f0e01782 | 9247 | storage); |
5566b478 | 9248 | |
03d0f4af | 9249 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
5566b478 | 9250 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 9251 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
9252 | if (! extern_p) |
9253 | instantiate_decl (result); | |
7177d104 MS |
9254 | } |
9255 | ||
faae18ab MS |
9256 | void |
9257 | mark_class_instantiated (t, extern_p) | |
9258 | tree t; | |
9259 | int extern_p; | |
9260 | { | |
9261 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
9262 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
9263 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
9264 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
9265 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
9266 | if (! extern_p) | |
9267 | { | |
9268 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
9269 | rest_of_type_compilation (t, 1); | |
9270 | } | |
9271 | } | |
e8abc66f | 9272 | |
7177d104 | 9273 | void |
ca79f85d JM |
9274 | do_type_instantiation (t, storage) |
9275 | tree t, storage; | |
7177d104 | 9276 | { |
e8abc66f MS |
9277 | int extern_p = 0; |
9278 | int nomem_p = 0; | |
5566b478 MS |
9279 | int static_p = 0; |
9280 | ||
ca79f85d JM |
9281 | if (TREE_CODE (t) == TYPE_DECL) |
9282 | t = TREE_TYPE (t); | |
9283 | ||
7ddedda4 | 9284 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 9285 | { |
8251199e | 9286 | cp_error ("explicit instantiation of non-template type `%T'", t); |
ca79f85d JM |
9287 | return; |
9288 | } | |
9289 | ||
5566b478 | 9290 | complete_type (t); |
7177d104 | 9291 | |
a292b002 MS |
9292 | /* With -fexternal-templates, explicit instantiations are treated the same |
9293 | as implicit ones. */ | |
a0a33927 MS |
9294 | if (flag_external_templates) |
9295 | return; | |
9296 | ||
f0e01782 MS |
9297 | if (TYPE_SIZE (t) == NULL_TREE) |
9298 | { | |
8251199e | 9299 | cp_error ("explicit instantiation of `%#T' before definition of template", |
f0e01782 MS |
9300 | t); |
9301 | return; | |
9302 | } | |
9303 | ||
03d0f4af | 9304 | if (storage != NULL_TREE) |
f0e01782 | 9305 | { |
03d0f4af | 9306 | if (pedantic) |
8251199e JM |
9307 | cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations", |
9308 | IDENTIFIER_POINTER (storage)); | |
03d0f4af MM |
9309 | |
9310 | if (storage == ridpointers[(int) RID_INLINE]) | |
9311 | nomem_p = 1; | |
9312 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
9313 | extern_p = 1; | |
9314 | else if (storage == ridpointers[(int) RID_STATIC]) | |
9315 | static_p = 1; | |
9316 | else | |
9317 | { | |
8251199e | 9318 | cp_error ("storage class `%D' applied to template instantiation", |
03d0f4af MM |
9319 | storage); |
9320 | extern_p = 0; | |
9321 | } | |
f0e01782 MS |
9322 | } |
9323 | ||
03d0f4af MM |
9324 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
9325 | { | |
9326 | /* [temp.spec] | |
a292b002 | 9327 | |
03d0f4af MM |
9328 | No program shall both explicitly instantiate and explicitly |
9329 | specialize a template. */ | |
8251199e JM |
9330 | cp_error ("explicit instantiation of `%#T' after", t); |
9331 | cp_error_at ("explicit specialization here", t); | |
03d0f4af MM |
9332 | return; |
9333 | } | |
9334 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 9335 | { |
03d0f4af MM |
9336 | /* [temp.spec] |
9337 | ||
9338 | No program shall explicitly instantiate any template more | |
9339 | than once. | |
9340 | ||
2aaf816d JM |
9341 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation |
9342 | was `extern'. If EXTERN_P then the second is. If -frepo, chances | |
9343 | are we already got marked as an explicit instantion because of the | |
9344 | repo file. All these cases are OK. */ | |
9345 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository) | |
9346 | cp_pedwarn ("duplicate explicit instantiation of `%#T'", t); | |
03d0f4af MM |
9347 | |
9348 | /* If we've already instantiated the template, just return now. */ | |
9349 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
9350 | return; | |
44a8d0b3 | 9351 | } |
e8abc66f | 9352 | |
03d0f4af MM |
9353 | mark_class_instantiated (t, extern_p); |
9354 | repo_template_instantiated (t, extern_p); | |
9355 | ||
e8abc66f MS |
9356 | if (nomem_p) |
9357 | return; | |
9358 | ||
7177d104 | 9359 | { |
db5ae43f | 9360 | tree tmp; |
5566b478 | 9361 | |
03d0f4af MM |
9362 | /* In contrast to implicit instantiation, where only the |
9363 | declarations, and not the definitions, of members are | |
9364 | instantiated, we have here: | |
9365 | ||
9366 | [temp.explicit] | |
9367 | ||
9368 | The explicit instantiation of a class template specialization | |
9369 | implies the instantiation of all of its members not | |
9370 | previously explicitly specialized in the translation unit | |
9371 | containing the explicit instantiation. | |
9372 | ||
9373 | Of course, we can't instantiate member template classes, since | |
9374 | we don't have any arguments for them. Note that the standard | |
9375 | is unclear on whether the instatiation of the members are | |
9376 | *explicit* instantiations or not. We choose to be generous, | |
9377 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
9378 | the explicit instantiation of a class where some of the members | |
9379 | have no definition in the current translation unit. */ | |
9380 | ||
5566b478 MS |
9381 | if (! static_p) |
9382 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 9383 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 9384 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
9385 | { |
9386 | mark_decl_instantiated (tmp, extern_p); | |
9387 | repo_template_instantiated (tmp, extern_p); | |
9388 | if (! extern_p) | |
9389 | instantiate_decl (tmp); | |
9390 | } | |
9391 | ||
9392 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
9393 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 9394 | { |
5566b478 | 9395 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 9396 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
9397 | if (! extern_p) |
9398 | instantiate_decl (tmp); | |
863adfc0 | 9399 | } |
7177d104 | 9400 | |
a292b002 | 9401 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
e4a84209 MM |
9402 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) |
9403 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
f376e137 | 9404 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); |
a292b002 | 9405 | } |
8d08fdba | 9406 | } |
a28e3c7f | 9407 | |
36a117a5 MM |
9408 | /* Given a function DECL, which is a specialization of TMPL, modify |
9409 | DECL to be a re-instantiation of TMPL with the same template | |
9410 | arguments. TMPL should be the template into which tsubst'ing | |
9411 | should occur for DECL, not the most general template. | |
830bfa74 MM |
9412 | |
9413 | One reason for doing this is a scenario like this: | |
9414 | ||
9415 | template <class T> | |
9416 | void f(const T&, int i); | |
9417 | ||
9418 | void g() { f(3, 7); } | |
9419 | ||
9420 | template <class T> | |
9421 | void f(const T& t, const int i) { } | |
9422 | ||
9423 | Note that when the template is first instantiated, with | |
9424 | instantiate_template, the resulting DECL will have no name for the | |
9425 | first parameter, and the wrong type for the second. So, when we go | |
9426 | to instantiate the DECL, we regenerate it. */ | |
9427 | ||
e9659ab0 | 9428 | static void |
830bfa74 MM |
9429 | regenerate_decl_from_template (decl, tmpl) |
9430 | tree decl; | |
9431 | tree tmpl; | |
9432 | { | |
9433 | tree args; | |
830bfa74 MM |
9434 | tree code_pattern; |
9435 | tree new_decl; | |
36a117a5 MM |
9436 | tree gen_tmpl; |
9437 | int unregistered; | |
830bfa74 MM |
9438 | |
9439 | args = DECL_TI_ARGS (decl); | |
9440 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
9441 | ||
8251199e JM |
9442 | /* Unregister the specialization so that when we tsubst we will not |
9443 | just return DECL. We don't have to unregister DECL from TMPL | |
9444 | because if would only be registered there if it were a partial | |
9445 | instantiation of a specialization, which it isn't: it's a full | |
9446 | instantiation. */ | |
36a117a5 MM |
9447 | gen_tmpl = most_general_template (tmpl); |
9448 | unregistered = unregister_specialization (decl, gen_tmpl); | |
9449 | ||
9450 | /* If the DECL was not unregistered then something peculiar is | |
9451 | happening: we created a specialization but did not call | |
9452 | register_specialization for it. */ | |
9453 | my_friendly_assert (unregistered, 0); | |
9454 | ||
61a127b3 MM |
9455 | if (TREE_CODE (decl) == VAR_DECL) |
9456 | /* Make sure that we can see identifiers, and compute access | |
9457 | correctly, for the class members used in the declaration of | |
9458 | this static variable. */ | |
9459 | pushclass (DECL_CONTEXT (decl), 2); | |
9460 | ||
d8c4447d | 9461 | /* Do the substitution to get the new declaration. */ |
4393e105 | 9462 | new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE); |
830bfa74 MM |
9463 | |
9464 | if (TREE_CODE (decl) == VAR_DECL) | |
9465 | { | |
9466 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
830bfa74 | 9467 | DECL_INITIAL (new_decl) = |
d8c4447d | 9468 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
4393e105 | 9469 | /*complain=*/1, DECL_TI_TEMPLATE (decl)); |
61a127b3 | 9470 | /* Pop the class context we pushed above. */ |
b74a0560 | 9471 | popclass (); |
830bfa74 | 9472 | } |
2b0a63a3 | 9473 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
fbf1c34b MM |
9474 | { |
9475 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
9476 | new decl. */ | |
9477 | DECL_INITIAL (new_decl) = error_mark_node; | |
9478 | /* And don't complain about a duplicate definition. */ | |
9479 | DECL_INITIAL (decl) = NULL_TREE; | |
9480 | } | |
36a117a5 MM |
9481 | |
9482 | /* The immediate parent of the new template is still whatever it was | |
9483 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
9484 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
9485 | tsubst always calculates the name as if the function in question | |
9486 | were really a template instance, and sometimes, with friend | |
9487 | functions, this is not so. See tsubst_friend_function for | |
9488 | details. */ | |
9489 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
9490 | DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl); | |
9491 | DECL_RTL (new_decl) = DECL_RTL (decl); | |
9492 | ||
9493 | /* Call duplicate decls to merge the old and new declarations. */ | |
830bfa74 MM |
9494 | duplicate_decls (new_decl, decl); |
9495 | ||
36a117a5 MM |
9496 | /* Now, re-register the specialization. */ |
9497 | register_specialization (decl, gen_tmpl, args); | |
830bfa74 MM |
9498 | } |
9499 | ||
f84b4be9 JM |
9500 | /* Produce the definition of D, a _DECL generated from a template. */ |
9501 | ||
a28e3c7f | 9502 | tree |
5566b478 MS |
9503 | instantiate_decl (d) |
9504 | tree d; | |
a28e3c7f | 9505 | { |
36a117a5 MM |
9506 | tree tmpl = DECL_TI_TEMPLATE (d); |
9507 | tree args = DECL_TI_ARGS (d); | |
830bfa74 | 9508 | tree td; |
36a117a5 MM |
9509 | tree code_pattern; |
9510 | tree spec; | |
9511 | tree gen_tmpl; | |
5566b478 | 9512 | int nested = in_function_p (); |
5566b478 | 9513 | int pattern_defined; |
c27be9b9 JM |
9514 | int line = lineno; |
9515 | char *file = input_filename; | |
5566b478 | 9516 | |
36a117a5 MM |
9517 | /* This function should only be used to instantiate templates for |
9518 | functions and static member variables. */ | |
9519 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
9520 | || TREE_CODE (d) == VAR_DECL, 0); | |
9521 | ||
fbf1c34b | 9522 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af MM |
9523 | /* D has already been instantiated. It might seem reasonable to |
9524 | check whether or not D is an explict instantiation, and, if so, | |
9525 | stop here. But when an explicit instantiation is deferred | |
9526 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
9527 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
9528 | return d; |
9529 | ||
9530 | /* If we already have a specialization of this declaration, then | |
9531 | there's no reason to instantiate it. Note that | |
9532 | retrieve_specialization gives us both instantiations and | |
9533 | specializations, so we must explicitly check | |
9534 | DECL_TEMPLATE_SPECIALIZATION. */ | |
9535 | gen_tmpl = most_general_template (tmpl); | |
9536 | spec = retrieve_specialization (gen_tmpl, args); | |
9537 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
9538 | return spec; | |
9539 | ||
9540 | /* This needs to happen before any tsubsting. */ | |
9541 | if (! push_tinst_level (d)) | |
9542 | return d; | |
9543 | ||
4d85e00e MM |
9544 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
9545 | for the instantiation. This is not always the most general | |
9546 | template. Consider, for example: | |
9547 | ||
9548 | template <class T> | |
9549 | struct S { template <class U> void f(); | |
9550 | template <> void f<int>(); }; | |
9551 | ||
9552 | and an instantiation of S<double>::f<int>. We want TD to be the | |
9553 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
9554 | td = tmpl; | |
9555 | for (td = tmpl; | |
9556 | /* An instantiation cannot have a definition, so we need a | |
9557 | more general template. */ | |
9558 | DECL_TEMPLATE_INSTANTIATION (td) | |
9559 | /* We must also deal with friend templates. Given: | |
9560 | ||
9561 | template <class T> struct S { | |
9562 | template <class U> friend void f() {}; | |
9563 | }; | |
9564 | ||
9565 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
9566 | so far as the language is concerned, but that's still | |
9567 | where we get the pattern for the instantiation from. On | |
9568 | ther hand, if the definition comes outside the class, say: | |
9569 | ||
9570 | template <class T> struct S { | |
9571 | template <class U> friend void f(); | |
9572 | }; | |
9573 | template <class U> friend void f() {} | |
9574 | ||
9575 | we don't need to look any further. That's what the check for | |
9576 | DECL_INITIAL is for. */ | |
9577 | || (TREE_CODE (d) == FUNCTION_DECL | |
61289ca3 | 9578 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) |
4d85e00e | 9579 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))); |
f84b4be9 | 9580 | ) |
4d85e00e MM |
9581 | { |
9582 | /* The present template, TD, should not be a definition. If it | |
9583 | were a definition, we should be using it! Note that we | |
9584 | cannot restructure the loop to just keep going until we find | |
9585 | a template with a definition, since that might go too far if | |
9586 | a specialization was declared, but not defined. */ | |
4d85e00e MM |
9587 | my_friendly_assert (!(TREE_CODE (d) == VAR_DECL |
9588 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), | |
9589 | 0); | |
9590 | ||
9591 | /* Fetch the more general template. */ | |
9592 | td = DECL_TI_TEMPLATE (td); | |
9593 | } | |
27bb8339 | 9594 | |
fee23f54 | 9595 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 9596 | |
5566b478 | 9597 | if (TREE_CODE (d) == FUNCTION_DECL) |
36a117a5 | 9598 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 | 9599 | else |
36a117a5 | 9600 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
de22184b MS |
9601 | |
9602 | push_to_top_level (); | |
9603 | lineno = DECL_SOURCE_LINE (d); | |
9604 | input_filename = DECL_SOURCE_FILE (d); | |
9605 | ||
de22184b | 9606 | if (pattern_defined) |
5566b478 MS |
9607 | { |
9608 | repo_template_used (d); | |
9609 | ||
9610 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
9611 | { | |
9612 | if (flag_alt_external_templates) | |
9613 | { | |
9614 | if (interface_unknown) | |
9615 | warn_if_unknown_interface (d); | |
9616 | } | |
fee23f54 | 9617 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
9618 | { |
9619 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 9620 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
9621 | } |
9622 | else | |
fee23f54 | 9623 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
9624 | } |
9625 | ||
e92cc029 | 9626 | if (at_eof) |
5566b478 MS |
9627 | import_export_decl (d); |
9628 | } | |
9629 | ||
c4ae3f91 JM |
9630 | /* Reject all external templates except inline functions. */ |
9631 | if (DECL_INTERFACE_KNOWN (d) | |
9632 | && ! DECL_NOT_REALLY_EXTERN (d) | |
9633 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
9634 | goto out; | |
9635 | ||
67ffc812 | 9636 | if (TREE_CODE (d) == VAR_DECL |
711734a9 | 9637 | && TREE_READONLY (d) |
67ffc812 MM |
9638 | && DECL_INITIAL (d) == NULL_TREE |
9639 | && DECL_INITIAL (code_pattern) != NULL_TREE) | |
9640 | /* We need to set up DECL_INITIAL regardless of pattern_defined if | |
9641 | the variable is a static const initialized in the class body. */; | |
9642 | else if (! pattern_defined | |
9643 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) | |
9644 | && ! at_eof)) | |
5566b478 | 9645 | { |
67ffc812 | 9646 | /* Defer all templates except inline functions used in another |
c27be9b9 JM |
9647 | function. We restore the source position here because it's used |
9648 | by add_pending_template. */ | |
9649 | lineno = line; | |
9650 | input_filename = file; | |
9651 | ||
03d0f4af MM |
9652 | if (at_eof && !pattern_defined |
9653 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
9654 | /* [temp.explicit] | |
9655 | ||
9656 | The definition of a non-exported function template, a | |
9657 | non-exported member function template, or a non-exported | |
9658 | member function or static data member of a class template | |
9659 | shall be present in every translation unit in which it is | |
9660 | explicitly instantiated. */ | |
8251199e | 9661 | cp_error ("explicit instantiation of `%D' but no definition available", |
03d0f4af MM |
9662 | d); |
9663 | ||
5566b478 | 9664 | add_pending_template (d); |
de22184b | 9665 | goto out; |
5566b478 MS |
9666 | } |
9667 | ||
2b0a63a3 MM |
9668 | /* We're now committed to instantiating this template. Mark it as |
9669 | instantiated so that recursive calls to instantiate_decl do not | |
9670 | try to instantiate it again. */ | |
fbf1c34b | 9671 | DECL_TEMPLATE_INSTANTIATED (d) = 1; |
120722ac | 9672 | |
2b0a63a3 MM |
9673 | /* Regenerate the declaration in case the template has been modified |
9674 | by a subsequent redeclaration. */ | |
9675 | regenerate_decl_from_template (d, td); | |
9676 | ||
120722ac MM |
9677 | /* We already set the file and line above. Reset them now in case |
9678 | they changed as a result of calling regenerate_decl_from_template. */ | |
5156628f MS |
9679 | lineno = DECL_SOURCE_LINE (d); |
9680 | input_filename = DECL_SOURCE_FILE (d); | |
9681 | ||
5566b478 MS |
9682 | if (TREE_CODE (d) == VAR_DECL) |
9683 | { | |
9684 | DECL_IN_AGGR_P (d) = 0; | |
9685 | if (DECL_INTERFACE_KNOWN (d)) | |
9686 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
9687 | else | |
9688 | { | |
9689 | DECL_EXTERNAL (d) = 1; | |
9690 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
9691 | } | |
9692 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
9693 | } | |
9694 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
9695 | { | |
558475f0 MM |
9696 | extern struct obstack *saveable_obstack; |
9697 | extern struct obstack *rtl_obstack; | |
9698 | int saved_expanding_p = expanding_p; | |
9699 | ||
9700 | /* We're not expanding all the way to RTL here. */ | |
9701 | expanding_p = 0; | |
5566b478 | 9702 | |
558475f0 | 9703 | /* Set up context. */ |
c11b6f21 | 9704 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
9705 | store_parm_decls (); |
9706 | ||
558475f0 MM |
9707 | /* Anything we might |
9708 | want to save is going to have to be saved forever. Note that | |
9709 | we don't want to save all kinds of temporary clutter that | |
9710 | might end up on the temporary obstack so we don't want to | |
9711 | call push_permanent_obstack. */ | |
9712 | push_obstacks_nochange (); | |
9713 | saveable_obstack = &permanent_obstack; | |
9714 | /* We only need this because of the cases where we generate | |
9715 | RTL_EXPRs. We should really be generating RTL_EXPRs until | |
9716 | final expansion time; when that is fixed, this can go. */ | |
9717 | rtl_obstack = &permanent_obstack; | |
9718 | /* Substitute into the body of the function. */ | |
9719 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
9720 | /*complain=*/1, tmpl); | |
9721 | ||
9722 | /* Clean up. */ | |
9723 | pop_obstacks (); | |
5566b478 | 9724 | finish_function (lineno, 0, nested); |
558475f0 MM |
9725 | expanding_p = saved_expanding_p; |
9726 | ||
9727 | /* Now, generate RTL for the function. */ | |
9728 | expand_body (d); | |
5566b478 MS |
9729 | } |
9730 | ||
de22184b | 9731 | out: |
c27be9b9 JM |
9732 | lineno = line; |
9733 | input_filename = file; | |
9734 | ||
5566b478 | 9735 | pop_from_top_level (); |
5566b478 | 9736 | pop_tinst_level (); |
a28e3c7f | 9737 | |
a28e3c7f MS |
9738 | return d; |
9739 | } | |
5566b478 | 9740 | |
0aafb128 MM |
9741 | /* Run through the list of templates that we wish we could |
9742 | instantiate, and instantiate any we can. */ | |
9743 | ||
9744 | int | |
9745 | instantiate_pending_templates () | |
9746 | { | |
9747 | tree *t; | |
9748 | int instantiated_something = 0; | |
9749 | int reconsider; | |
9750 | ||
9751 | do | |
9752 | { | |
9753 | reconsider = 0; | |
9754 | ||
9755 | t = &pending_templates; | |
9756 | while (*t) | |
9757 | { | |
9758 | tree srcloc = TREE_PURPOSE (*t); | |
9759 | tree instantiation = TREE_VALUE (*t); | |
9760 | ||
9761 | input_filename = SRCLOC_FILE (srcloc); | |
9762 | lineno = SRCLOC_LINE (srcloc); | |
9763 | ||
9764 | if (TREE_CODE_CLASS (TREE_CODE (instantiation)) == 't') | |
9765 | { | |
9766 | tree fn; | |
9767 | ||
9768 | if (!TYPE_SIZE (instantiation)) | |
9769 | { | |
9770 | instantiate_class_template (instantiation); | |
9771 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
9772 | for (fn = TYPE_METHODS (instantiation); | |
9773 | fn; | |
9774 | fn = TREE_CHAIN (fn)) | |
9775 | if (! DECL_ARTIFICIAL (fn)) | |
9776 | instantiate_decl (fn); | |
9777 | if (TYPE_SIZE (instantiation)) | |
9778 | { | |
9779 | instantiated_something = 1; | |
9780 | reconsider = 1; | |
9781 | } | |
9782 | } | |
9783 | ||
9784 | if (TYPE_SIZE (instantiation)) | |
9785 | /* If INSTANTIATION has been instantiated, then we don't | |
9786 | need to consider it again in the future. */ | |
9787 | *t = TREE_CHAIN (*t); | |
9788 | else | |
9789 | t = &TREE_CHAIN (*t); | |
9790 | } | |
9791 | else | |
9792 | { | |
9793 | if (DECL_TEMPLATE_INSTANTIATION (instantiation) | |
9794 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9795 | { | |
9796 | instantiation = instantiate_decl (instantiation); | |
9797 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9798 | { | |
9799 | instantiated_something = 1; | |
9800 | reconsider = 1; | |
9801 | } | |
9802 | } | |
9803 | ||
9804 | if (!DECL_TEMPLATE_INSTANTIATION (instantiation) | |
9805 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9806 | /* If INSTANTIATION has been instantiated, then we don't | |
9807 | need to consider it again in the future. */ | |
9808 | *t = TREE_CHAIN (*t); | |
9809 | else | |
9810 | t = &TREE_CHAIN (*t); | |
9811 | } | |
9812 | } | |
9813 | template_tail = t; | |
9814 | ||
9815 | /* Go through the things that are template instantiations if we are | |
9816 | using guiding declarations. */ | |
9817 | t = &maybe_templates; | |
9818 | while (*t) | |
9819 | { | |
9820 | tree template; | |
9821 | tree fn; | |
9822 | tree args; | |
9823 | ||
9824 | fn = TREE_VALUE (*t); | |
9825 | ||
9826 | if (DECL_INITIAL (fn)) | |
9827 | /* If the FN is already defined, then it was either already | |
9828 | instantiated or, even though guiding declarations were | |
9829 | allowed, a non-template definition was provided. */ | |
9830 | ; | |
9831 | else | |
9832 | { | |
9833 | template = TREE_PURPOSE (*t); | |
9834 | args = get_bindings (template, fn, NULL_TREE); | |
9835 | fn = instantiate_template (template, args); | |
9836 | instantiate_decl (fn); | |
9837 | reconsider = 1; | |
9838 | } | |
9839 | ||
9840 | /* Remove this entry from the chain. */ | |
9841 | *t = TREE_CHAIN (*t); | |
9842 | } | |
9843 | maybe_template_tail = t; | |
9844 | } | |
9845 | while (reconsider); | |
9846 | ||
9847 | return instantiated_something; | |
9848 | } | |
9849 | ||
4393e105 MM |
9850 | /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it |
9851 | is an initializer list: the TREE_PURPOSEs are DECLs, and the | |
9852 | TREE_VALUEs are initializer values. Used by instantiate_decl. */ | |
9853 | ||
824b9a4c | 9854 | static tree |
5566b478 MS |
9855 | tsubst_expr_values (t, argvec) |
9856 | tree t, argvec; | |
9857 | { | |
9858 | tree first = NULL_TREE; | |
9859 | tree *p = &first; | |
9860 | ||
9861 | for (; t; t = TREE_CHAIN (t)) | |
9862 | { | |
4393e105 MM |
9863 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, |
9864 | /*complain=*/1, NULL_TREE); | |
9865 | tree val = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1, | |
9866 | NULL_TREE); | |
5566b478 MS |
9867 | *p = build_tree_list (pur, val); |
9868 | p = &TREE_CHAIN (*p); | |
9869 | } | |
9870 | return first; | |
9871 | } | |
9872 | ||
9873 | tree last_tree; | |
9874 | ||
9875 | void | |
9876 | add_tree (t) | |
9877 | tree t; | |
9878 | { | |
9879 | last_tree = TREE_CHAIN (last_tree) = t; | |
9880 | } | |
73aad9b9 | 9881 | |
75650646 MM |
9882 | |
9883 | void | |
9884 | begin_tree () | |
9885 | { | |
9886 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
9887 | last_tree = NULL_TREE; | |
9888 | } | |
9889 | ||
9890 | ||
9891 | void | |
9892 | end_tree () | |
9893 | { | |
9894 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
9895 | ||
9896 | last_tree = TREE_VALUE (saved_trees); | |
9897 | saved_trees = TREE_CHAIN (saved_trees); | |
9898 | } | |
9899 | ||
73aad9b9 JM |
9900 | /* D is an undefined function declaration in the presence of templates with |
9901 | the same name, listed in FNS. If one of them can produce D as an | |
9902 | instantiation, remember this so we can instantiate it at EOF if D has | |
9903 | not been defined by that time. */ | |
9904 | ||
9905 | void | |
9906 | add_maybe_template (d, fns) | |
9907 | tree d, fns; | |
9908 | { | |
9909 | tree t; | |
9910 | ||
9911 | if (DECL_MAYBE_TEMPLATE (d)) | |
9912 | return; | |
9913 | ||
e1467ff2 | 9914 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
9915 | if (! t) |
9916 | return; | |
9917 | if (t == error_mark_node) | |
9918 | { | |
8251199e | 9919 | cp_error ("ambiguous template instantiation for `%D'", d); |
73aad9b9 JM |
9920 | return; |
9921 | } | |
9922 | ||
9923 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
9924 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
9925 | DECL_MAYBE_TEMPLATE (d) = 1; | |
9926 | } | |
b87692e5 | 9927 | |
61a127b3 MM |
9928 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
9929 | ||
9930 | static void | |
9931 | set_current_access_from_decl (decl) | |
9932 | tree decl; | |
9933 | { | |
9934 | if (TREE_PRIVATE (decl)) | |
9935 | current_access_specifier = access_private_node; | |
9936 | else if (TREE_PROTECTED (decl)) | |
9937 | current_access_specifier = access_protected_node; | |
9938 | else | |
9939 | current_access_specifier = access_public_node; | |
9940 | } | |
9941 | ||
dbfe2124 MM |
9942 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
9943 | is the instantiation (which should have been created with | |
9944 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 9945 | |
dbfe2124 MM |
9946 | static void |
9947 | tsubst_enum (tag, newtag, args) | |
9948 | tree tag; | |
9949 | tree newtag; | |
9950 | tree args; | |
b87692e5 | 9951 | { |
dbfe2124 | 9952 | tree e; |
b87692e5 MS |
9953 | |
9954 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
9955 | { | |
61a127b3 MM |
9956 | tree value; |
9957 | tree elt; | |
9958 | ||
9959 | /* Note that in a template enum, the TREE_VALUE is the | |
9960 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
9961 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
4393e105 MM |
9962 | args, /*complain=*/1, |
9963 | NULL_TREE); | |
61a127b3 MM |
9964 | |
9965 | /* Give this enumeration constant the correct access. */ | |
9966 | set_current_access_from_decl (TREE_VALUE (e)); | |
9967 | ||
9968 | /* Actually build the enumerator itself. */ | |
9969 | elt = build_enumerator (TREE_PURPOSE (e), value, newtag); | |
b87692e5 | 9970 | |
dbfe2124 MM |
9971 | /* We save the enumerators we have built so far in the |
9972 | TYPE_VALUES so that if the enumeration constants for | |
9973 | subsequent enumerators involve those for previous ones, | |
9974 | tsubst_copy will be able to find them. */ | |
9975 | TREE_CHAIN (elt) = TYPE_VALUES (newtag); | |
9976 | TYPE_VALUES (newtag) = elt; | |
9977 | } | |
b3d5a58b | 9978 | |
dbfe2124 | 9979 | finish_enum (newtag); |
b87692e5 | 9980 | } |
36a117a5 MM |
9981 | |
9982 | /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that | |
9983 | is either an instantiation or specialization of a template | |
9984 | function. */ | |
9985 | ||
9986 | static void | |
9987 | set_mangled_name_for_template_decl (decl) | |
9988 | tree decl; | |
9989 | { | |
9990 | tree saved_namespace; | |
b370501f | 9991 | tree context = NULL_TREE; |
36a117a5 MM |
9992 | tree fn_type; |
9993 | tree ret_type; | |
9994 | tree parm_types; | |
9995 | tree tparms; | |
9996 | tree targs; | |
9997 | tree tmpl; | |
9998 | int parm_depth; | |
9999 | ||
10000 | my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); | |
10001 | my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0); | |
10002 | ||
10003 | /* The names of template functions must be mangled so as to indicate | |
10004 | what template is being specialized with what template arguments. | |
10005 | For example, each of the following three functions must get | |
10006 | different mangled names: | |
10007 | ||
10008 | void f(int); | |
10009 | template <> void f<7>(int); | |
10010 | template <> void f<8>(int); */ | |
10011 | ||
10012 | targs = DECL_TI_ARGS (decl); | |
10013 | if (uses_template_parms (targs)) | |
10014 | /* This DECL is for a partial instantiation. There's no need to | |
10015 | mangle the name of such an entity. */ | |
10016 | return; | |
10017 | ||
10018 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
10019 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
10020 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
10021 | ||
10022 | /* There should be as many levels of arguments as there are levels | |
10023 | of parameters. */ | |
10024 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
10025 | ||
10026 | /* We now compute the PARMS and RET_TYPE to give to | |
10027 | build_decl_overload_real. The PARMS and RET_TYPE are the | |
10028 | parameter and return types of the template, after all but the | |
10029 | innermost template arguments have been substituted, not the | |
10030 | parameter and return types of the function DECL. For example, | |
10031 | given: | |
10032 | ||
10033 | template <class T> T f(T); | |
10034 | ||
10035 | both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'. | |
10036 | A more subtle example is: | |
10037 | ||
10038 | template <class T> struct S { template <class U> void f(T, U); } | |
10039 | ||
10040 | Here, if DECL is `void S<int>::f(int, double)', PARMS should be | |
10041 | {int, U}. Thus, the args that we want to subsitute into the | |
10042 | return and parameter type for the function are those in TARGS, | |
10043 | with the innermost level omitted. */ | |
10044 | fn_type = TREE_TYPE (tmpl); | |
10045 | if (DECL_STATIC_FUNCTION_P (decl)) | |
61a127b3 | 10046 | context = DECL_CLASS_CONTEXT (decl); |
36a117a5 MM |
10047 | |
10048 | if (parm_depth == 1) | |
10049 | /* No substitution is necessary. */ | |
10050 | ; | |
10051 | else | |
10052 | { | |
10053 | int i; | |
10054 | tree partial_args; | |
10055 | ||
10056 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
10057 | let tsubst know not to subsitute for those parameters. */ | |
10058 | partial_args = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
10059 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
10060 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
10061 | TMPL_ARGS_LEVEL (targs, i)); | |
10062 | SET_TMPL_ARGS_LEVEL (partial_args, | |
10063 | TMPL_ARGS_DEPTH (targs), | |
10064 | make_temp_vec (DECL_NTPARMS (tmpl))); | |
10065 | ||
10066 | /* Now, do the (partial) substitution to figure out the | |
10067 | appropriate function type. */ | |
4393e105 | 10068 | fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 | 10069 | if (DECL_STATIC_FUNCTION_P (decl)) |
4393e105 | 10070 | context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
10071 | |
10072 | /* Substitute into the template parameters to obtain the real | |
10073 | innermost set of parameters. This step is important if the | |
10074 | innermost set of template parameters contains value | |
10075 | parameters whose types depend on outer template parameters. */ | |
10076 | TREE_VEC_LENGTH (partial_args)--; | |
4393e105 | 10077 | tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1); |
36a117a5 MM |
10078 | } |
10079 | ||
10080 | /* Now, get the innermost parameters and arguments, and figure out | |
10081 | the parameter and return types. */ | |
10082 | tparms = INNERMOST_TEMPLATE_PARMS (tparms); | |
10083 | targs = innermost_args (targs); | |
10084 | ret_type = TREE_TYPE (fn_type); | |
10085 | parm_types = TYPE_ARG_TYPES (fn_type); | |
10086 | ||
10087 | /* For a static member function, we generate a fake `this' pointer, | |
10088 | for the purposes of mangling. This indicates of which class the | |
10089 | function is a member. Because of: | |
10090 | ||
10091 | [class.static] | |
10092 | ||
10093 | There shall not be a static and a nonstatic member function | |
10094 | with the same name and the same parameter types | |
10095 | ||
10096 | we don't have to worry that this will result in a clash with a | |
10097 | non-static member function. */ | |
10098 | if (DECL_STATIC_FUNCTION_P (decl)) | |
10099 | parm_types = hash_tree_chain (build_pointer_type (context), parm_types); | |
10100 | ||
10101 | /* There should be the same number of template parameters as | |
10102 | template arguments. */ | |
10103 | my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs), | |
10104 | 0); | |
10105 | ||
10106 | /* If the template is in a namespace, we need to put that into the | |
10107 | mangled name. Unfortunately, build_decl_overload_real does not | |
10108 | get the decl to mangle, so it relies on the current | |
10109 | namespace. Therefore, we set that here temporarily. */ | |
10110 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702); | |
10111 | saved_namespace = current_namespace; | |
10112 | current_namespace = CP_DECL_CONTEXT (decl); | |
10113 | ||
10114 | /* Actually set the DCL_ASSEMBLER_NAME. */ | |
10115 | DECL_ASSEMBLER_NAME (decl) | |
10116 | = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type, | |
10117 | tparms, targs, | |
10118 | DECL_FUNCTION_MEMBER_P (decl) | |
10119 | + DECL_CONSTRUCTOR_P (decl)); | |
10120 | ||
10121 | /* Restore the previously active namespace. */ | |
10122 | current_namespace = saved_namespace; | |
10123 | } |