]>
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)) | |
845 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
846 | { | |
847 | tree fn = TREE_VALUE (s); | |
848 | ||
849 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
850 | { | |
851 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
852 | { | |
853 | if (TREE_USED (fn) | |
854 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
855 | { | |
8251199e | 856 | cp_error ("specialization of %D after instantiation", |
75650646 | 857 | fn); |
36a117a5 | 858 | return spec; |
75650646 MM |
859 | } |
860 | else | |
861 | { | |
862 | /* This situation should occur only if the first | |
863 | specialization is an implicit instantiation, | |
864 | the second is an explicit specialization, and | |
865 | the implicit instantiation has not yet been | |
866 | used. That situation can occur if we have | |
36a117a5 MM |
867 | implicitly instantiated a member function and |
868 | then specialized it later. | |
869 | ||
870 | We can also wind up here if a friend | |
871 | declaration that looked like an instantiation | |
872 | turns out to be a specialization: | |
873 | ||
874 | template <class T> void foo(T); | |
875 | class S { friend void foo<>(int) }; | |
876 | template <> void foo(int); | |
877 | ||
878 | We transform the existing DECL in place so that | |
879 | any pointers to it become pointers to the | |
fbf1c34b MM |
880 | updated declaration. |
881 | ||
882 | If there was a definition for the template, but | |
883 | not for the specialization, we want this to | |
884 | look as if there is no definition, and vice | |
885 | versa. */ | |
886 | DECL_INITIAL (fn) = NULL_TREE; | |
887 | duplicate_decls (spec, fn); | |
888 | ||
889 | return fn; | |
75650646 MM |
890 | } |
891 | } | |
892 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
893 | { | |
fbf1c34b MM |
894 | duplicate_decls (spec, fn); |
895 | return fn; | |
75650646 MM |
896 | } |
897 | } | |
898 | } | |
899 | ||
900 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
901 | = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
902 | |
903 | return spec; | |
904 | } | |
905 | ||
906 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
907 | Returns nonzero if the SPEC was listed as a specialization of | |
908 | TMPL. */ | |
909 | ||
910 | static int | |
911 | unregister_specialization (spec, tmpl) | |
912 | tree spec; | |
913 | tree tmpl; | |
914 | { | |
915 | tree* s; | |
916 | ||
917 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
918 | *s != NULL_TREE; | |
919 | s = &TREE_CHAIN (*s)) | |
920 | if (TREE_VALUE (*s) == spec) | |
921 | { | |
922 | *s = TREE_CHAIN (*s); | |
923 | return 1; | |
924 | } | |
925 | ||
926 | return 0; | |
75650646 MM |
927 | } |
928 | ||
9188c363 MM |
929 | /* Like register_specialization, but for local declarations. FN is |
930 | the function in which we are registering SPEC, an instantiation of | |
931 | TMPL. */ | |
932 | ||
933 | static tree | |
934 | register_local_specialization (spec, tmpl, fn) | |
935 | tree spec; | |
936 | tree tmpl; | |
937 | tree fn; | |
938 | { | |
939 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
940 | = perm_tree_cons (fn, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
941 | ||
942 | return spec; | |
943 | } | |
944 | ||
e1467ff2 MM |
945 | /* Print the list of candidate FNS in an error message. */ |
946 | ||
104bf76a | 947 | void |
e1467ff2 MM |
948 | print_candidates (fns) |
949 | tree fns; | |
950 | { | |
951 | tree fn; | |
952 | ||
d8e178a0 | 953 | const char *str = "candidates are:"; |
e1467ff2 MM |
954 | |
955 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
956 | { | |
d6479fe7 MM |
957 | tree f; |
958 | ||
959 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
960 | cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); | |
e1467ff2 MM |
961 | str = " "; |
962 | } | |
963 | } | |
964 | ||
75650646 | 965 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 966 | which can be specialized to match the indicated DECL with the |
03017874 MM |
967 | explicit template args given in TEMPLATE_ID. The DECL may be |
968 | NULL_TREE if none is available. In that case, the functions in | |
969 | TEMPLATE_ID are non-members. | |
970 | ||
971 | If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a | |
972 | specialization of a member template. | |
973 | ||
974 | The template args (those explicitly specified and those deduced) | |
975 | are output in a newly created vector *TARGS_OUT. | |
976 | ||
977 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 978 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 979 | |
e9659ab0 | 980 | static tree |
e1467ff2 | 981 | determine_specialization (template_id, decl, targs_out, |
bf8f3f93 | 982 | need_member_template) |
386b8a85 | 983 | tree template_id; |
e1467ff2 | 984 | tree decl; |
386b8a85 JM |
985 | tree* targs_out; |
986 | int need_member_template; | |
386b8a85 | 987 | { |
03017874 MM |
988 | tree fns; |
989 | tree targs; | |
990 | tree explicit_targs; | |
991 | tree candidates = NULL_TREE; | |
992 | tree templates = NULL_TREE; | |
386b8a85 | 993 | |
e1467ff2 MM |
994 | *targs_out = NULL_TREE; |
995 | ||
f2e48b67 BK |
996 | if (template_id == error_mark_node) |
997 | return error_mark_node; | |
998 | ||
999 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1000 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1001 | |
27fafc8d JM |
1002 | if (fns == error_mark_node) |
1003 | return error_mark_node; | |
1004 | ||
2c73f9f5 | 1005 | /* Check for baselinks. */ |
91e490ab | 1006 | if (BASELINK_P (fns)) |
2c73f9f5 | 1007 | fns = TREE_VALUE (fns); |
386b8a85 | 1008 | |
91e490ab MM |
1009 | if (!is_overloaded_fn (fns)) |
1010 | { | |
9dfce8fd | 1011 | cp_error ("`%D' is not a function template", fns); |
91e490ab MM |
1012 | return error_mark_node; |
1013 | } | |
1014 | ||
2c73f9f5 | 1015 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1016 | { |
75650646 MM |
1017 | tree tmpl; |
1018 | ||
9dfce8fd | 1019 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1020 | |
1021 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1022 | /* DECL might be a specialization of FN. */ | |
1023 | tmpl = fn; | |
1024 | else if (need_member_template) | |
1025 | /* FN is an ordinary member function, and we need a | |
1026 | specialization of a member template. */ | |
1027 | continue; | |
1028 | else if (TREE_CODE (fn) != FUNCTION_DECL) | |
1029 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1030 | cases. */ | |
1031 | continue; | |
1032 | else if (!DECL_FUNCTION_MEMBER_P (fn)) | |
1033 | /* This is just an ordinary non-member function. Nothing can | |
1034 | be a specialization of that. */ | |
1035 | continue; | |
75650646 | 1036 | else |
03017874 MM |
1037 | { |
1038 | tree decl_arg_types; | |
386b8a85 | 1039 | |
03017874 MM |
1040 | /* This is an ordinary member function. However, since |
1041 | we're here, we can assume it's enclosing class is a | |
1042 | template class. For example, | |
1043 | ||
1044 | template <typename T> struct S { void f(); }; | |
1045 | template <> void S<int>::f() {} | |
1046 | ||
1047 | Here, S<int>::f is a non-template, but S<int> is a | |
1048 | template class. If FN has the same type as DECL, we | |
1049 | might be in business. */ | |
1050 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), | |
1051 | TREE_TYPE (TREE_TYPE (fn)))) | |
1052 | /* The return types differ. */ | |
1053 | continue; | |
1054 | ||
1055 | /* Adjust the type of DECL in case FN is a static member. */ | |
1056 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1057 | if (DECL_STATIC_FUNCTION_P (fn) | |
1058 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1059 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1060 | ||
1061 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1062 | decl_arg_types)) | |
1063 | /* They match! */ | |
1064 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1065 | ||
1066 | continue; | |
1067 | } | |
386b8a85 | 1068 | |
bf8f3f93 MM |
1069 | /* See whether this function might be a specialization of this |
1070 | template. */ | |
1071 | targs = get_bindings (tmpl, decl, explicit_targs); | |
03017874 MM |
1072 | |
1073 | if (!targs) | |
bf8f3f93 | 1074 | /* We cannot deduce template arguments that when used to |
03017874 MM |
1075 | specialize TMPL will produce DECL. */ |
1076 | continue; | |
1077 | ||
1078 | /* Save this template, and the arguments deduced. */ | |
1079 | templates = scratch_tree_cons (targs, tmpl, templates); | |
386b8a85 | 1080 | } |
03017874 | 1081 | |
bf8f3f93 | 1082 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1083 | { |
03017874 MM |
1084 | /* We have: |
1085 | ||
1086 | [temp.expl.spec] | |
1087 | ||
1088 | It is possible for a specialization with a given function | |
1089 | signature to be instantiated from more than one function | |
1090 | template. In such cases, explicit specification of the | |
1091 | template arguments must be used to uniquely identify the | |
1092 | function template specialization being specialized. | |
1093 | ||
1094 | Note that here, there's no suggestion that we're supposed to | |
1095 | determine which of the candidate templates is most | |
1096 | specialized. However, we, also have: | |
1097 | ||
1098 | [temp.func.order] | |
1099 | ||
1100 | Partial ordering of overloaded function template | |
1101 | declarations is used in the following contexts to select | |
1102 | the function template to which a function template | |
1103 | specialization refers: | |
1104 | ||
1105 | -- when an explicit specialization refers to a function | |
1106 | template. | |
1107 | ||
1108 | So, we do use the partial ordering rules, at least for now. | |
1109 | This extension can only serve to make illegal programs legal, | |
1110 | so it's safe. And, there is strong anecdotal evidence that | |
1111 | the committee intended the partial ordering rules to apply; | |
1112 | the EDG front-end has that behavior, and John Spicer claims | |
1113 | that the committee simply forgot to delete the wording in | |
1114 | [temp.expl.spec]. */ | |
1115 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1116 | if (tmpl && tmpl != error_mark_node) | |
1117 | { | |
1118 | targs = get_bindings (tmpl, decl, explicit_targs); | |
1119 | templates = scratch_tree_cons (targs, tmpl, NULL_TREE); | |
1120 | } | |
e1467ff2 MM |
1121 | } |
1122 | ||
03017874 | 1123 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1124 | { |
bf8f3f93 MM |
1125 | cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", |
1126 | template_id, decl); | |
03017874 | 1127 | return error_mark_node; |
386b8a85 | 1128 | } |
03017874 | 1129 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1130 | || (candidates && TREE_CHAIN (candidates)) |
1131 | || (templates && candidates)) | |
386b8a85 | 1132 | { |
bf8f3f93 MM |
1133 | cp_error_at ("ambiguous template specialization `%D' for `%+D'", |
1134 | template_id, decl); | |
1135 | chainon (candidates, templates); | |
1136 | print_candidates (candidates); | |
03017874 | 1137 | return error_mark_node; |
386b8a85 JM |
1138 | } |
1139 | ||
1140 | /* We have one, and exactly one, match. */ | |
03017874 MM |
1141 | if (candidates) |
1142 | { | |
1143 | /* It was a specialization of an ordinary member function in a | |
1144 | template class. */ | |
1145 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1146 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1147 | } | |
1148 | ||
1149 | /* It was a specialization of a template. */ | |
1150 | targs = DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates))); | |
1151 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) | |
1152 | { | |
1153 | *targs_out = copy_node (targs); | |
1154 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1155 | TMPL_ARGS_DEPTH (*targs_out), | |
1156 | TREE_PURPOSE (templates)); | |
1157 | } | |
1158 | else | |
1159 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1160 | return TREE_VALUE (templates); |
8d08fdba | 1161 | } |
6757edfe | 1162 | |
386b8a85 | 1163 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1164 | DECLARATOR, and in DECL, is a specialization of a function |
1165 | template. We may also discover that the declaration is an explicit | |
1166 | instantiation at this point. | |
1167 | ||
e1467ff2 | 1168 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1169 | instead if all goes well. Issues an error message if something is |
1170 | amiss. Returns error_mark_node if the error is not easily | |
1171 | recoverable. | |
75650646 | 1172 | |
75650646 MM |
1173 | FLAGS is a bitmask consisting of the following flags: |
1174 | ||
75650646 MM |
1175 | 2: The function has a definition. |
1176 | 4: The function is a friend. | |
75650646 MM |
1177 | |
1178 | The TEMPLATE_COUNT is the number of references to qualifying | |
1179 | template classes that appeared in the name of the function. For | |
1180 | example, in | |
1181 | ||
1182 | template <class T> struct S { void f(); }; | |
1183 | void S<int>::f(); | |
1184 | ||
1185 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1186 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1187 | ||
1188 | template <class T> struct S {}; | |
1189 | template <> struct S<int> { void f(); } | |
36a117a5 | 1190 | template <> void S<int>::f(); |
75650646 MM |
1191 | |
1192 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
1193 | illegal; there should be no template <>.) | |
1194 | ||
1195 | If the function is a specialization, it is marked as such via | |
1196 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1197 | is set up correctly, and it is added to the list of specializations | |
1198 | for that template. */ | |
386b8a85 | 1199 | |
e1467ff2 | 1200 | tree |
27bb8339 | 1201 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
1202 | tree declarator; |
1203 | tree decl; | |
1204 | int template_count; | |
1205 | int flags; | |
1206 | { | |
75650646 MM |
1207 | int have_def = flags & 2; |
1208 | int is_friend = flags & 4; | |
1209 | int specialization = 0; | |
e1467ff2 | 1210 | int explicit_instantiation = 0; |
fd4de5ff | 1211 | int member_specialization = 0; |
75650646 MM |
1212 | |
1213 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
1214 | tree dname = DECL_NAME (decl); | |
386b8a85 | 1215 | |
fd4de5ff | 1216 | if (processing_specialization) |
386b8a85 | 1217 | { |
fd4de5ff | 1218 | /* The last template header was of the form template <>. */ |
75650646 | 1219 | |
fd4de5ff MM |
1220 | if (template_header_count > template_count) |
1221 | { | |
1222 | /* There were more template headers than qualifying template | |
1223 | classes. */ | |
1224 | if (template_header_count - template_count > 1) | |
03017874 MM |
1225 | /* There shouldn't be that many template parameter lists. |
1226 | There can be at most one parameter list for every | |
1227 | qualifying class, plus one for the function itself. */ | |
fd4de5ff | 1228 | cp_error ("too many template parameter lists in declaration of `%D'", decl); |
75650646 | 1229 | |
fd4de5ff MM |
1230 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
1231 | if (ctype) | |
1232 | member_specialization = 1; | |
1233 | else | |
1234 | specialization = 1; | |
75650646 | 1235 | } |
fd4de5ff | 1236 | else if (template_header_count == template_count) |
75650646 | 1237 | { |
fd4de5ff MM |
1238 | /* The counts are equal. So, this might be a |
1239 | specialization, but it is not a specialization of a | |
1240 | member template. It might be something like | |
1241 | ||
1242 | template <class T> struct S { | |
1243 | void f(int i); | |
1244 | }; | |
1245 | template <> | |
1246 | void S<int>::f(int i) {} */ | |
1247 | specialization = 1; | |
1248 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1249 | } |
fd4de5ff | 1250 | else |
386b8a85 | 1251 | { |
fd4de5ff MM |
1252 | /* This cannot be an explicit specialization. There are not |
1253 | enough headers for all of the qualifying classes. For | |
1254 | example, we might have: | |
1255 | ||
1256 | template <> | |
1257 | void S<int>::T<char>::f(); | |
75650646 | 1258 | |
fd4de5ff MM |
1259 | But, we're missing another template <>. */ |
1260 | cp_error("too few template parameter lists in declaration of `%D'", decl); | |
1261 | return decl; | |
1262 | } | |
1263 | } | |
1264 | else if (processing_explicit_instantiation) | |
1265 | { | |
1266 | if (template_header_count) | |
1267 | cp_error ("template parameter list used in explicit instantiation"); | |
1268 | ||
1269 | if (have_def) | |
1270 | cp_error ("definition provided for explicit instantiation"); | |
75650646 | 1271 | |
fd4de5ff MM |
1272 | explicit_instantiation = 1; |
1273 | } | |
1274 | else if (ctype != NULL_TREE | |
1275 | && !TYPE_BEING_DEFINED (ctype) | |
1276 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) | |
1277 | && !is_friend) | |
1278 | { | |
1279 | /* This case catches outdated code that looks like this: | |
1280 | ||
1281 | template <class T> struct S { void f(); }; | |
1282 | void S<int>::f() {} // Missing template <> | |
1283 | ||
1284 | We disable this check when the type is being defined to | |
1285 | avoid complaining about default compiler-generated | |
1286 | constructors, destructors, and assignment operators. | |
1287 | Since the type is an instantiation, not a specialization, | |
1288 | these are the only functions that can be defined before | |
1289 | the class is complete. */ | |
75650646 | 1290 | |
c6f2ed0d JM |
1291 | /* If they said |
1292 | template <class T> void S<int>::f() {} | |
1293 | that's bogus. */ | |
fd4de5ff MM |
1294 | if (template_header_count) |
1295 | { | |
1296 | cp_error ("template parameters specified in specialization"); | |
1297 | return decl; | |
c6f2ed0d | 1298 | } |
fd4de5ff MM |
1299 | |
1300 | if (pedantic) | |
1301 | cp_pedwarn | |
1302 | ("explicit specialization not preceded by `template <>'"); | |
1303 | specialization = 1; | |
1304 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1305 | } | |
1306 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
1307 | { | |
1308 | if (is_friend) | |
1309 | /* This could be something like: | |
1310 | ||
1311 | template <class T> void f(T); | |
1312 | class S { friend void f<>(int); } */ | |
1313 | specialization = 1; | |
1314 | else | |
c6f2ed0d | 1315 | { |
fd4de5ff MM |
1316 | /* This case handles bogus declarations like template <> |
1317 | template <class T> void f<int>(); */ | |
75650646 | 1318 | |
fd4de5ff MM |
1319 | cp_error ("template-id `%D' in declaration of primary template", |
1320 | declarator); | |
1321 | return decl; | |
386b8a85 | 1322 | } |
75650646 | 1323 | } |
386b8a85 | 1324 | |
670960ac JM |
1325 | if (specialization || member_specialization) |
1326 | { | |
1327 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1328 | for (; t; t = TREE_CHAIN (t)) | |
1329 | if (TREE_PURPOSE (t)) | |
1330 | { | |
8251199e JM |
1331 | cp_pedwarn |
1332 | ("default argument specified in explicit specialization"); | |
670960ac JM |
1333 | break; |
1334 | } | |
f3400fe2 JM |
1335 | if (current_lang_name == lang_name_c) |
1336 | cp_error ("template specialization with C linkage"); | |
670960ac JM |
1337 | } |
1338 | ||
e1467ff2 | 1339 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1340 | { |
1341 | tree tmpl = NULL_TREE; | |
1342 | tree targs = NULL_TREE; | |
75650646 MM |
1343 | |
1344 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1345 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1346 | { | |
1347 | tree fns; | |
1348 | ||
1349 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1350 | 0); | |
1351 | if (!ctype) | |
30394414 | 1352 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); |
386b8a85 JM |
1353 | else |
1354 | fns = dname; | |
1355 | ||
75650646 MM |
1356 | declarator = |
1357 | lookup_template_function (fns, NULL_TREE); | |
386b8a85 JM |
1358 | } |
1359 | ||
f2e48b67 BK |
1360 | if (declarator == error_mark_node) |
1361 | return error_mark_node; | |
1362 | ||
75650646 MM |
1363 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1364 | { | |
8ec2ac34 | 1365 | if (!explicit_instantiation) |
6c30752f MM |
1366 | /* A specialization in class scope. This is illegal, |
1367 | but the error will already have been flagged by | |
1368 | check_specialization_scope. */ | |
1369 | return error_mark_node; | |
8ec2ac34 | 1370 | else |
b370501f KG |
1371 | { |
1372 | /* It's not legal to write an explicit instantiation in | |
1373 | class scope, e.g.: | |
8ec2ac34 | 1374 | |
b370501f | 1375 | class C { template void f(); } |
8ec2ac34 | 1376 | |
b370501f KG |
1377 | This case is caught by the parser. However, on |
1378 | something like: | |
8ec2ac34 | 1379 | |
b370501f | 1380 | template class C { void f(); }; |
8ec2ac34 | 1381 | |
b370501f KG |
1382 | (which is illegal) we can get here. The error will be |
1383 | issued later. */ | |
1384 | ; | |
1385 | } | |
8ec2ac34 | 1386 | |
e1467ff2 | 1387 | return decl; |
75650646 | 1388 | } |
8f032717 MM |
1389 | else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) |
1390 | { | |
1391 | /* A friend declaration. We can't do much, because we don't | |
1392 | know what this resolves to, yet. */ | |
1393 | my_friendly_assert (is_friend != 0, 0); | |
1394 | my_friendly_assert (!explicit_instantiation, 0); | |
1395 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
1396 | return decl; | |
1397 | } | |
75650646 MM |
1398 | else if (ctype != NULL_TREE |
1399 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1400 | IDENTIFIER_NODE)) | |
386b8a85 | 1401 | { |
75650646 MM |
1402 | /* Find the list of functions in ctype that have the same |
1403 | name as the declared function. */ | |
1404 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
1405 | tree fns = NULL_TREE; |
1406 | int idx; | |
1407 | ||
75650646 MM |
1408 | if (name == constructor_name (ctype) |
1409 | || name == constructor_name_full (ctype)) | |
386b8a85 | 1410 | { |
75650646 MM |
1411 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1412 | ||
1413 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1414 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1415 | { | |
1416 | /* From [temp.expl.spec]: | |
e1467ff2 | 1417 | |
75650646 MM |
1418 | If such an explicit specialization for the member |
1419 | of a class template names an implicitly-declared | |
1420 | special member function (clause _special_), the | |
e1467ff2 MM |
1421 | program is ill-formed. |
1422 | ||
1423 | Similar language is found in [temp.explicit]. */ | |
8251199e | 1424 | cp_error ("specialization of implicitly-declared special member function"); |
03017874 | 1425 | return error_mark_node; |
75650646 | 1426 | } |
386b8a85 | 1427 | |
42da2fd8 | 1428 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1429 | } |
42da2fd8 | 1430 | |
03017874 MM |
1431 | if (!IDENTIFIER_TYPENAME_P (name)) |
1432 | { | |
1433 | idx = lookup_fnfields_1 (ctype, name); | |
1434 | if (idx >= 0) | |
1435 | fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); | |
1436 | } | |
1437 | else | |
1438 | { | |
1439 | tree methods; | |
1440 | ||
1441 | /* For a type-conversion operator, we cannot do a | |
1442 | name-based lookup. We might be looking for `operator | |
1443 | int' which will be a specialization of `operator T'. | |
1444 | So, we find *all* the conversion operators, and then | |
1445 | select from them. */ | |
1446 | fns = NULL_TREE; | |
1447 | ||
1448 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1449 | if (methods) | |
1450 | for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx) | |
1451 | { | |
1452 | tree ovl = TREE_VEC_ELT (methods, idx); | |
1453 | ||
1454 | if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) | |
1455 | /* There are no more conversion functions. */ | |
1456 | break; | |
1457 | ||
1458 | /* Glue all these conversion functions together | |
1459 | with those we already have. */ | |
1460 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1461 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1462 | } | |
1463 | } | |
1464 | ||
386b8a85 JM |
1465 | if (fns == NULL_TREE) |
1466 | { | |
b3f738da MM |
1467 | cp_error ("no member function `%D' declared in `%T'", |
1468 | name, ctype); | |
03017874 | 1469 | return error_mark_node; |
386b8a85 JM |
1470 | } |
1471 | else | |
1472 | TREE_OPERAND (declarator, 0) = fns; | |
1473 | } | |
75650646 | 1474 | |
e1467ff2 MM |
1475 | /* Figure out what exactly is being specialized at this point. |
1476 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1477 | member function, we cannot tell apriori whether the |
e1467ff2 | 1478 | instantiation is for a member template, or just a member |
36a117a5 MM |
1479 | function of a template class. Even if a member template is |
1480 | being instantiated, the member template arguments may be | |
1481 | elided if they can be deduced from the rest of the | |
1482 | declaration. */ | |
e1467ff2 MM |
1483 | tmpl = determine_specialization (declarator, decl, |
1484 | &targs, | |
bf8f3f93 | 1485 | member_specialization); |
386b8a85 | 1486 | |
03017874 MM |
1487 | if (!tmpl || tmpl == error_mark_node) |
1488 | /* We couldn't figure out what this declaration was | |
1489 | specializing. */ | |
1490 | return error_mark_node; | |
1491 | else | |
386b8a85 | 1492 | { |
25aab5d0 | 1493 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 1494 | |
e1467ff2 MM |
1495 | if (explicit_instantiation) |
1496 | { | |
03d0f4af | 1497 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
25aab5d0 MM |
1498 | is done by do_decl_instantiation later. */ |
1499 | ||
1500 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
1501 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
1502 | ||
1503 | if (arg_depth > parm_depth) | |
1504 | { | |
1505 | /* If TMPL is not the most general template (for | |
1506 | example, if TMPL is a friend template that is | |
1507 | injected into namespace scope), then there will | |
1508 | be too many levels fo TARGS. Remove some of them | |
1509 | here. */ | |
1510 | int i; | |
1511 | tree new_targs; | |
1512 | ||
1513 | new_targs = make_temp_vec (parm_depth); | |
1514 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) | |
1515 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
1516 | = TREE_VEC_ELT (targs, i); | |
1517 | targs = new_targs; | |
1518 | } | |
1519 | ||
1520 | decl = instantiate_template (tmpl, targs); | |
e1467ff2 MM |
1521 | return decl; |
1522 | } | |
36a117a5 MM |
1523 | |
1524 | /* If we though that the DECL was a member function, but it | |
1525 | turns out to be specializing a static member function, | |
1526 | make DECL a static member function as well. */ | |
1527 | if (DECL_STATIC_FUNCTION_P (tmpl) | |
1528 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
366c0f1e MM |
1529 | { |
1530 | revert_static_member_fn (&decl, 0, 0); | |
1531 | last_function_parms = TREE_CHAIN (last_function_parms); | |
1532 | } | |
e1467ff2 | 1533 | |
36a117a5 MM |
1534 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
1535 | DECL_TEMPLATE_INFO (decl) | |
1536 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
1537 | ||
e1467ff2 MM |
1538 | /* Mangle the function name appropriately. Note that we do |
1539 | not mangle specializations of non-template member | |
1540 | functions of template classes, e.g. with | |
36a117a5 | 1541 | |
e1467ff2 | 1542 | template <class T> struct S { void f(); } |
36a117a5 | 1543 | |
e1467ff2 | 1544 | and given the specialization |
36a117a5 | 1545 | |
e1467ff2 | 1546 | template <> void S<int>::f() {} |
36a117a5 | 1547 | |
e1467ff2 MM |
1548 | we do not mangle S<int>::f() here. That's because it's |
1549 | just an ordinary member function and doesn't need special | |
36a117a5 MM |
1550 | treatment. We do this here so that the ordinary, |
1551 | non-template, name-mangling algorith will not be used | |
1552 | later. */ | |
e1467ff2 | 1553 | if ((is_member_template (tmpl) || ctype == NULL_TREE) |
75650646 | 1554 | && name_mangling_version >= 1) |
36a117a5 | 1555 | set_mangled_name_for_template_decl (decl); |
386b8a85 JM |
1556 | |
1557 | if (is_friend && !have_def) | |
36a117a5 MM |
1558 | /* This is not really a declaration of a specialization. |
1559 | It's just the name of an instantiation. But, it's not | |
1560 | a request for an instantiation, either. */ | |
fbf1c34b | 1561 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
75650646 | 1562 | |
36a117a5 MM |
1563 | /* Register this specialization so that we can find it |
1564 | again. */ | |
1565 | decl = register_specialization (decl, gen_tmpl, targs); | |
386b8a85 JM |
1566 | } |
1567 | } | |
75650646 | 1568 | |
e1467ff2 | 1569 | return decl; |
386b8a85 | 1570 | } |
75650646 | 1571 | |
39c01e4c MM |
1572 | /* TYPE is being declared. Verify that the use of template headers |
1573 | and such is reasonable. Issue error messages if not. */ | |
1574 | ||
1575 | void | |
1576 | maybe_check_template_type (type) | |
1577 | tree type; | |
1578 | { | |
1579 | if (template_header_count) | |
1580 | { | |
1581 | /* We are in the scope of some `template <...>' header. */ | |
1582 | ||
1583 | int context_depth | |
1584 | = template_class_depth_real (TYPE_CONTEXT (type), | |
1585 | /*count_specializations=*/1); | |
1586 | ||
1587 | if (template_header_count <= context_depth) | |
1588 | /* This is OK; the template headers are for the context. We | |
1589 | are actually too lenient here; like | |
1590 | check_explicit_specialization we should consider the number | |
1591 | of template types included in the actual declaration. For | |
1592 | example, | |
1593 | ||
1594 | template <class T> struct S { | |
1595 | template <class U> template <class V> | |
1596 | struct I {}; | |
1597 | }; | |
1598 | ||
1599 | is illegal, but: | |
1600 | ||
1601 | template <class T> struct S { | |
1602 | template <class U> struct I; | |
1603 | }; | |
1604 | ||
1605 | template <class T> template <class U. | |
1606 | struct S<T>::I {}; | |
1607 | ||
1608 | is not. */ | |
1609 | ; | |
1610 | else if (template_header_count > context_depth + 1) | |
1611 | /* There are two many template parameter lists. */ | |
8251199e | 1612 | cp_error ("too many template parameter lists in declaration of `%T'", type); |
39c01e4c MM |
1613 | } |
1614 | } | |
1615 | ||
75650646 MM |
1616 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
1617 | parameters. These are represented in the same format used for | |
1618 | DECL_TEMPLATE_PARMS. */ | |
1619 | ||
1620 | int comp_template_parms (parms1, parms2) | |
1621 | tree parms1; | |
1622 | tree parms2; | |
1623 | { | |
1624 | tree p1; | |
1625 | tree p2; | |
1626 | ||
1627 | if (parms1 == parms2) | |
1628 | return 1; | |
1629 | ||
1630 | for (p1 = parms1, p2 = parms2; | |
1631 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1632 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1633 | { | |
1634 | tree t1 = TREE_VALUE (p1); | |
1635 | tree t2 = TREE_VALUE (p2); | |
1636 | int i; | |
1637 | ||
1638 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1639 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1640 | ||
1641 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1642 | return 0; | |
1643 | ||
1644 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1645 | { | |
1646 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1647 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1648 | ||
1649 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1650 | return 0; | |
1651 | ||
1652 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1653 | continue; | |
3bfdc719 | 1654 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
1655 | return 0; |
1656 | } | |
1657 | } | |
1658 | ||
1659 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1660 | /* One set of parameters has more parameters lists than the | |
1661 | other. */ | |
1662 | return 0; | |
1663 | ||
1664 | return 1; | |
1665 | } | |
1666 | ||
f3400fe2 JM |
1667 | /* Complain if DECL shadows a template parameter. |
1668 | ||
1669 | [temp.local]: A template-parameter shall not be redeclared within its | |
1670 | scope (including nested scopes). */ | |
1671 | ||
1672 | void | |
1673 | check_template_shadow (decl) | |
1674 | tree decl; | |
1675 | { | |
8f032717 MM |
1676 | tree olddecl; |
1677 | ||
b5d9b9ab MM |
1678 | /* If we're not in a template, we can't possibly shadow a template |
1679 | parameter. */ | |
1680 | if (!current_template_parms) | |
1681 | return; | |
1682 | ||
1683 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
1684 | if (TREE_CODE (decl) == OVERLOAD) |
1685 | decl = OVL_CURRENT (decl); | |
1686 | olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); | |
f3400fe2 | 1687 | |
b5d9b9ab MM |
1688 | /* If there's no previous binding for this name, we're not shadowing |
1689 | anything, let alone a template parameter. */ | |
1690 | if (!olddecl) | |
1691 | return; | |
1692 | ||
1693 | /* If we're not shadowing a template parameter, we're done. Note | |
1694 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
1695 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
1696 | node. */ | |
1697 | if (TREE_CODE_CLASS (TREE_CODE (olddecl)) != 'd' | |
1698 | || !DECL_TEMPLATE_PARM_P (olddecl)) | |
1699 | return; | |
1700 | ||
1701 | /* We check for decl != olddecl to avoid bogus errors for using a | |
1702 | name inside a class. We check TPFI to avoid duplicate errors for | |
1703 | inline member templates. */ | |
1704 | if (decl == olddecl | |
1705 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
1706 | return; | |
1707 | ||
1708 | cp_error_at ("declaration of `%#D'", decl); | |
1709 | cp_error_at (" shadows template parm `%#D'", olddecl); | |
f3400fe2 | 1710 | } |
22a7be53 | 1711 | |
f3400fe2 | 1712 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
1713 | ORIG_LEVEL, DECL, and TYPE. */ |
1714 | ||
1715 | static tree | |
1716 | build_template_parm_index (index, level, orig_level, decl, type) | |
1717 | int index; | |
1718 | int level; | |
1719 | int orig_level; | |
1720 | tree decl; | |
1721 | tree type; | |
1722 | { | |
1723 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1724 | TEMPLATE_PARM_IDX (t) = index; | |
1725 | TEMPLATE_PARM_LEVEL (t) = level; | |
1726 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1727 | TEMPLATE_PARM_DECL (t) = decl; | |
1728 | TREE_TYPE (t) = type; | |
1729 | ||
1730 | return t; | |
1731 | } | |
1732 | ||
f84b4be9 | 1733 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 1734 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
1735 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
1736 | new one is created. */ | |
1737 | ||
1738 | static tree | |
93cdc044 | 1739 | reduce_template_parm_level (index, type, levels) |
f84b4be9 JM |
1740 | tree index; |
1741 | tree type; | |
93cdc044 | 1742 | int levels; |
f84b4be9 JM |
1743 | { |
1744 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1745 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 1746 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 JM |
1747 | { |
1748 | tree decl | |
1749 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
1750 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
1751 | type); | |
1752 | tree t | |
1753 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 1754 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
1755 | TEMPLATE_PARM_ORIG_LEVEL (index), |
1756 | decl, type); | |
1757 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
1758 | |
1759 | /* Template template parameters need this. */ | |
1760 | DECL_TEMPLATE_PARMS (decl) | |
1761 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
1762 | } |
1763 | ||
1764 | return TEMPLATE_PARM_DESCENDANTS (index); | |
1765 | } | |
1766 | ||
8d08fdba | 1767 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 1768 | LIST being built. */ |
e92cc029 | 1769 | |
8d08fdba MS |
1770 | tree |
1771 | process_template_parm (list, next) | |
1772 | tree list, next; | |
1773 | { | |
1774 | tree parm; | |
1775 | tree decl = 0; | |
a292b002 | 1776 | tree defval; |
5566b478 | 1777 | int is_type, idx; |
f84b4be9 | 1778 | |
8d08fdba MS |
1779 | parm = next; |
1780 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
1781 | defval = TREE_PURPOSE (parm); |
1782 | parm = TREE_VALUE (parm); | |
1783 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
1784 | |
1785 | if (list) | |
1786 | { | |
1787 | tree p = TREE_VALUE (tree_last (list)); | |
1788 | ||
1789 | if (TREE_CODE (p) == TYPE_DECL) | |
1790 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
73b0fce8 KL |
1791 | else if (TREE_CODE (p) == TEMPLATE_DECL) |
1792 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p))); | |
5566b478 | 1793 | else |
f84b4be9 | 1794 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
1795 | ++idx; |
1796 | } | |
1797 | else | |
1798 | idx = 0; | |
1799 | ||
8d08fdba MS |
1800 | if (!is_type) |
1801 | { | |
a292b002 | 1802 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 1803 | /* is a const-param */ |
a292b002 | 1804 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 1805 | PARM, 0, NULL_TREE); |
d490621d MM |
1806 | |
1807 | /* [temp.param] | |
1808 | ||
1809 | The top-level cv-qualifiers on the template-parameter are | |
1810 | ignored when determining its type. */ | |
1811 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
1812 | ||
8d08fdba MS |
1813 | /* A template parameter is not modifiable. */ |
1814 | TREE_READONLY (parm) = 1; | |
c91a56d2 | 1815 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
410c2fba KL |
1816 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM |
1817 | && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE) | |
8d08fdba | 1818 | { |
8251199e | 1819 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
c91a56d2 MS |
1820 | TREE_TYPE (parm)); |
1821 | if (DECL_NAME (parm) == NULL_TREE) | |
8251199e | 1822 | error (" a template type parameter must begin with `class' or `typename'"); |
8d08fdba MS |
1823 | TREE_TYPE (parm) = void_type_node; |
1824 | } | |
37c46b43 MS |
1825 | else if (pedantic |
1826 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
1827 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
8251199e | 1828 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
eb66be0e | 1829 | TREE_TYPE (parm)); |
8d08fdba MS |
1830 | if (TREE_PERMANENT (parm) == 0) |
1831 | { | |
1832 | parm = copy_node (parm); | |
1833 | TREE_PERMANENT (parm) = 1; | |
1834 | } | |
8d08fdba | 1835 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
f84b4be9 JM |
1836 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
1837 | = build_template_parm_index (idx, processing_template_decl, | |
1838 | processing_template_decl, | |
1839 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
1840 | } |
1841 | else | |
1842 | { | |
73b0fce8 KL |
1843 | tree t; |
1844 | parm = TREE_VALUE (parm); | |
1845 | ||
1846 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
1847 | { | |
1848 | t = make_lang_type (TEMPLATE_TEMPLATE_PARM); | |
1849 | /* This is for distinguishing between real templates and template | |
1850 | template parameters */ | |
1851 | TREE_TYPE (parm) = t; | |
1852 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
1853 | decl = parm; | |
1854 | } | |
1855 | else | |
1856 | { | |
1857 | t = make_lang_type (TEMPLATE_TYPE_PARM); | |
1858 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ | |
1859 | decl = build_decl (TYPE_DECL, parm, t); | |
1860 | } | |
1861 | ||
d2e5ee5c MS |
1862 | TYPE_NAME (t) = decl; |
1863 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 1864 | parm = decl; |
f84b4be9 JM |
1865 | TEMPLATE_TYPE_PARM_INDEX (t) |
1866 | = build_template_parm_index (idx, processing_template_decl, | |
1867 | processing_template_decl, | |
1868 | decl, TREE_TYPE (parm)); | |
8d08fdba | 1869 | } |
8ccc31eb | 1870 | SET_DECL_ARTIFICIAL (decl); |
fc03edb3 | 1871 | DECL_TEMPLATE_PARM_P (decl) = 1; |
8d08fdba | 1872 | pushdecl (decl); |
a292b002 | 1873 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
1874 | return chainon (list, parm); |
1875 | } | |
1876 | ||
1877 | /* The end of a template parameter list has been reached. Process the | |
1878 | tree list into a parameter vector, converting each parameter into a more | |
1879 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
1880 | as PARM_DECLs. */ | |
1881 | ||
1882 | tree | |
1883 | end_template_parm_list (parms) | |
1884 | tree parms; | |
1885 | { | |
5566b478 | 1886 | int nparms; |
8d08fdba | 1887 | tree parm; |
5566b478 MS |
1888 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
1889 | ||
5566b478 MS |
1890 | current_template_parms |
1891 | = tree_cons (build_int_2 (0, processing_template_decl), | |
1892 | saved_parmlist, current_template_parms); | |
8d08fdba | 1893 | |
91e490ab MM |
1894 | for (parm = parms, nparms = 0; |
1895 | parm; | |
1896 | parm = TREE_CHAIN (parm), nparms++) | |
66191c20 | 1897 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 1898 | |
67ffc812 MM |
1899 | --processing_template_parmlist; |
1900 | ||
8d08fdba MS |
1901 | return saved_parmlist; |
1902 | } | |
1903 | ||
5566b478 MS |
1904 | /* end_template_decl is called after a template declaration is seen. */ |
1905 | ||
8d08fdba | 1906 | void |
5566b478 | 1907 | end_template_decl () |
8d08fdba | 1908 | { |
386b8a85 JM |
1909 | reset_specialization (); |
1910 | ||
5156628f | 1911 | if (! processing_template_decl) |
73aad9b9 JM |
1912 | return; |
1913 | ||
5566b478 MS |
1914 | /* This matches the pushlevel in begin_template_parm_list. */ |
1915 | poplevel (0, 0, 0); | |
8d08fdba | 1916 | |
5566b478 MS |
1917 | --processing_template_decl; |
1918 | current_template_parms = TREE_CHAIN (current_template_parms); | |
1919 | (void) get_pending_sizes (); /* Why? */ | |
1920 | } | |
8d08fdba | 1921 | |
36a117a5 MM |
1922 | /* Given a template argument vector containing the template PARMS. |
1923 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
1924 | |
1925 | tree | |
1926 | current_template_args () | |
5566b478 | 1927 | { |
36a117a5 | 1928 | tree header; |
b370501f | 1929 | tree args = NULL_TREE; |
36a117a5 | 1930 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
1931 | int l = length; |
1932 | ||
36a117a5 MM |
1933 | /* If there is only one level of template parameters, we do not |
1934 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
1935 | TREE_VEC containing the arguments. */ | |
1936 | if (length > 1) | |
1937 | args = make_tree_vec (length); | |
1938 | ||
1939 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 1940 | { |
5566b478 | 1941 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
1942 | int i; |
1943 | ||
5566b478 | 1944 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 1945 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 1946 | { |
98c1c668 JM |
1947 | tree t = TREE_VEC_ELT (a, i); |
1948 | ||
36a117a5 | 1949 | /* T will be a list if we are called from within a |
98c1c668 JM |
1950 | begin/end_template_parm_list pair, but a vector directly |
1951 | if within a begin/end_member_template_processing pair. */ | |
1952 | if (TREE_CODE (t) == TREE_LIST) | |
1953 | { | |
1954 | t = TREE_VALUE (t); | |
1955 | ||
73b0fce8 KL |
1956 | if (TREE_CODE (t) == TYPE_DECL |
1957 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
1958 | t = TREE_TYPE (t); |
1959 | else | |
1960 | t = DECL_INITIAL (t); | |
36a117a5 | 1961 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 1962 | } |
5566b478 | 1963 | } |
36a117a5 MM |
1964 | |
1965 | if (length > 1) | |
1966 | TREE_VEC_ELT (args, --l) = a; | |
1967 | else | |
1968 | args = a; | |
8d08fdba MS |
1969 | } |
1970 | ||
9a3b49ac MS |
1971 | return args; |
1972 | } | |
75650646 | 1973 | |
e1467ff2 MM |
1974 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
1975 | template PARMS. Used by push_template_decl below. */ | |
1976 | ||
75650646 MM |
1977 | static tree |
1978 | build_template_decl (decl, parms) | |
1979 | tree decl; | |
1980 | tree parms; | |
1981 | { | |
1982 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
1983 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
1984 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
1985 | if (DECL_LANG_SPECIFIC (decl)) | |
1986 | { | |
1987 | DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); | |
8f032717 MM |
1988 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
1989 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
11f98788 | 1990 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
75650646 MM |
1991 | } |
1992 | ||
1993 | return tmpl; | |
1994 | } | |
1995 | ||
050367a3 MM |
1996 | struct template_parm_data |
1997 | { | |
6c30752f MM |
1998 | /* The level of the template parameters we are currently |
1999 | processing. */ | |
050367a3 | 2000 | int level; |
6c30752f MM |
2001 | |
2002 | /* The index of the specialization argument we are currently | |
2003 | processing. */ | |
2004 | int current_arg; | |
2005 | ||
2006 | /* An array whose size is the number of template parameters. The | |
2007 | elements are non-zero if the parameter has been used in any one | |
2008 | of the arguments processed so far. */ | |
050367a3 | 2009 | int* parms; |
6c30752f MM |
2010 | |
2011 | /* An array whose size is the number of template arguments. The | |
2012 | elements are non-zero if the argument makes use of template | |
2013 | parameters of this level. */ | |
2014 | int* arg_uses_template_parms; | |
050367a3 MM |
2015 | }; |
2016 | ||
2017 | /* Subroutine of push_template_decl used to see if each template | |
2018 | parameter in a partial specialization is used in the explicit | |
2019 | argument list. If T is of the LEVEL given in DATA (which is | |
2020 | treated as a template_parm_data*), then DATA->PARMS is marked | |
2021 | appropriately. */ | |
2022 | ||
2023 | static int | |
2024 | mark_template_parm (t, data) | |
2025 | tree t; | |
2026 | void* data; | |
2027 | { | |
2028 | int level; | |
2029 | int idx; | |
2030 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
2031 | ||
2032 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2033 | { | |
2034 | level = TEMPLATE_PARM_LEVEL (t); | |
2035 | idx = TEMPLATE_PARM_IDX (t); | |
2036 | } | |
2037 | else | |
2038 | { | |
2039 | level = TEMPLATE_TYPE_LEVEL (t); | |
2040 | idx = TEMPLATE_TYPE_IDX (t); | |
2041 | } | |
2042 | ||
2043 | if (level == tpd->level) | |
6c30752f MM |
2044 | { |
2045 | tpd->parms[idx] = 1; | |
2046 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
2047 | } | |
050367a3 MM |
2048 | |
2049 | /* Return zero so that for_each_template_parm will continue the | |
2050 | traversal of the tree; we want to mark *every* template parm. */ | |
2051 | return 0; | |
2052 | } | |
2053 | ||
6c30752f MM |
2054 | /* Process the partial specialization DECL. */ |
2055 | ||
e9659ab0 | 2056 | static tree |
6c30752f MM |
2057 | process_partial_specialization (decl) |
2058 | tree decl; | |
2059 | { | |
2060 | tree type = TREE_TYPE (decl); | |
2061 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
2062 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
2063 | tree inner_args = innermost_args (specargs); | |
2064 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
2065 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
2066 | int nargs = TREE_VEC_LENGTH (inner_args); | |
2067 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2068 | int i; | |
2069 | int did_error_intro = 0; | |
6c30752f MM |
2070 | struct template_parm_data tpd; |
2071 | struct template_parm_data tpd2; | |
2072 | ||
6c30752f MM |
2073 | /* We check that each of the template parameters given in the |
2074 | partial specialization is used in the argument list to the | |
2075 | specialization. For example: | |
2076 | ||
2077 | template <class T> struct S; | |
2078 | template <class T> struct S<T*>; | |
2079 | ||
2080 | The second declaration is OK because `T*' uses the template | |
2081 | parameter T, whereas | |
2082 | ||
2083 | template <class T> struct S<int>; | |
2084 | ||
2085 | is no good. Even trickier is: | |
2086 | ||
2087 | template <class T> | |
2088 | struct S1 | |
2089 | { | |
2090 | template <class U> | |
2091 | struct S2; | |
2092 | template <class U> | |
2093 | struct S2<T>; | |
2094 | }; | |
2095 | ||
2096 | The S2<T> declaration is actually illegal; it is a | |
2097 | full-specialization. Of course, | |
2098 | ||
2099 | template <class U> | |
2100 | struct S2<T (*)(U)>; | |
2101 | ||
2102 | or some such would have been OK. */ | |
2103 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2104 | tpd.parms = alloca (sizeof (int) * ntparms); | |
1ddb2906 | 2105 | bzero ((PTR) tpd.parms, sizeof (int) * ntparms); |
6c30752f MM |
2106 | |
2107 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
1ddb2906 | 2108 | bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs); |
6c30752f MM |
2109 | for (i = 0; i < nargs; ++i) |
2110 | { | |
2111 | tpd.current_arg = i; | |
2112 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2113 | &mark_template_parm, | |
2114 | &tpd); | |
2115 | } | |
2116 | for (i = 0; i < ntparms; ++i) | |
2117 | if (tpd.parms[i] == 0) | |
2118 | { | |
2119 | /* One of the template parms was not used in the | |
2120 | specialization. */ | |
2121 | if (!did_error_intro) | |
2122 | { | |
8251199e | 2123 | cp_error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2124 | did_error_intro = 1; |
2125 | } | |
2126 | ||
8251199e | 2127 | cp_error (" `%D'", |
6c30752f MM |
2128 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
2129 | } | |
2130 | ||
2131 | /* [temp.class.spec] | |
2132 | ||
2133 | The argument list of the specialization shall not be identical to | |
2134 | the implicit argument list of the primary template. */ | |
2135 | if (comp_template_args (inner_args, | |
2136 | innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2137 | (maintmpl))))) | |
8251199e | 2138 | cp_error ("partial specialization `%T' does not specialize any template arguments", type); |
6c30752f MM |
2139 | |
2140 | /* [temp.class.spec] | |
2141 | ||
2142 | A partially specialized non-type argument expression shall not | |
2143 | involve template parameters of the partial specialization except | |
2144 | when the argument expression is a simple identifier. | |
2145 | ||
2146 | The type of a template parameter corresponding to a specialized | |
2147 | non-type argument shall not be dependent on a parameter of the | |
2148 | specialization. */ | |
2149 | my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); | |
2150 | tpd2.parms = 0; | |
2151 | for (i = 0; i < nargs; ++i) | |
2152 | { | |
2153 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2154 | if (/* These first two lines are the `non-type' bit. */ | |
2155 | TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
2156 | && TREE_CODE (arg) != TEMPLATE_DECL | |
2157 | /* This next line is the `argument expression is not just a | |
2158 | simple identifier' condition and also the `specialized | |
2159 | non-type argument' bit. */ | |
2160 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2161 | { | |
2162 | if (tpd.arg_uses_template_parms[i]) | |
8251199e | 2163 | cp_error ("template argument `%E' involves template parameter(s)", arg); |
6c30752f MM |
2164 | else |
2165 | { | |
2166 | /* Look at the corresponding template parameter, | |
2167 | marking which template parameters its type depends | |
2168 | upon. */ | |
2169 | tree type = | |
2170 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2171 | i))); | |
2172 | ||
2173 | if (!tpd2.parms) | |
2174 | { | |
2175 | /* We haven't yet initialized TPD2. Do so now. */ | |
2176 | tpd2.arg_uses_template_parms | |
2177 | = (int*) alloca (sizeof (int) * nargs); | |
104bf76a | 2178 | /* The number of parameters here is the number in the |
76a83782 RH |
2179 | main template, which, as checked in the assertion |
2180 | above, is NARGS. */ | |
6c30752f MM |
2181 | tpd2.parms = (int*) alloca (sizeof (int) * nargs); |
2182 | tpd2.level = | |
2183 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2184 | } | |
2185 | ||
104bf76a | 2186 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2187 | looking for the template parameters of the main |
2188 | template, not in the specialization. */ | |
2189 | tpd2.current_arg = i; | |
2190 | tpd2.arg_uses_template_parms[i] = 0; | |
1ddb2906 | 2191 | bzero ((PTR) tpd2.parms, sizeof (int) * nargs); |
6c30752f MM |
2192 | for_each_template_parm (type, |
2193 | &mark_template_parm, | |
2194 | &tpd2); | |
2195 | ||
2196 | if (tpd2.arg_uses_template_parms [i]) | |
2197 | { | |
2198 | /* The type depended on some template parameters. | |
2199 | If they are fully specialized in the | |
2200 | specialization, that's OK. */ | |
2201 | int j; | |
2202 | for (j = 0; j < nargs; ++j) | |
2203 | if (tpd2.parms[j] != 0 | |
2204 | && tpd.arg_uses_template_parms [j]) | |
2205 | { | |
104bf76a | 2206 | cp_error ("type `%T' of template argument `%E' depends on template parameter(s)", |
6c30752f MM |
2207 | type, |
2208 | arg); | |
2209 | break; | |
2210 | } | |
2211 | } | |
2212 | } | |
2213 | } | |
2214 | } | |
2215 | ||
2216 | if (retrieve_specialization (maintmpl, specargs)) | |
2217 | /* We've already got this specialization. */ | |
2218 | return decl; | |
2219 | ||
2220 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) | |
2221 | = perm_tree_cons (inner_args, inner_parms, | |
2222 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
2223 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; | |
2224 | return decl; | |
2225 | } | |
2226 | ||
6ba89f8e MM |
2227 | /* Check that a template declaration's use of default arguments is not |
2228 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
2229 | non-zero if DECL is the thing declared by a primary template. | |
2230 | IS_PARTIAL is non-zero if DECL is a partial specialization. */ | |
2231 | ||
2232 | static void | |
2233 | check_default_tmpl_args (decl, parms, is_primary, is_partial) | |
2234 | tree decl; | |
2235 | tree parms; | |
2236 | int is_primary; | |
2237 | int is_partial; | |
2238 | { | |
d8e178a0 | 2239 | const char *msg; |
66191c20 MM |
2240 | int last_level_to_check; |
2241 | tree parm_level; | |
6ba89f8e MM |
2242 | |
2243 | /* [temp.param] | |
2244 | ||
2245 | A default template-argument shall not be specified in a | |
2246 | function template declaration or a function template definition, nor | |
2247 | in the template-parameter-list of the definition of a member of a | |
2248 | class template. */ | |
2249 | ||
e0942dcd MM |
2250 | if (TREE_CODE (DECL_REAL_CONTEXT (decl)) == FUNCTION_DECL) |
2251 | /* You can't have a function template declaration in a local | |
2252 | scope, nor you can you define a member of a class template in a | |
2253 | local scope. */ | |
2254 | return; | |
2255 | ||
6ba89f8e MM |
2256 | if (current_class_type |
2257 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 2258 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
2259 | /* If this is either a friend defined in the scope of the class |
2260 | or a member function. */ | |
2261 | && DECL_CLASS_CONTEXT (decl) == current_class_type | |
2262 | /* And, if it was a member function, it really was defined in | |
2263 | the scope of the class. */ | |
2264 | && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_DEFINED_IN_CLASS_P (decl))) | |
6ba89f8e | 2265 | /* We already checked these parameters when the template was |
5937a6f9 MM |
2266 | declared, so there's no need to do it again now. This function |
2267 | was defined in class scope, but we're processing it's body now | |
2268 | that the class is complete. */ | |
6ba89f8e MM |
2269 | return; |
2270 | ||
66191c20 MM |
2271 | /* [temp.param] |
2272 | ||
2273 | If a template-parameter has a default template-argument, all | |
2274 | subsequent template-parameters shall have a default | |
2275 | template-argument supplied. */ | |
2276 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
2277 | { | |
2278 | tree inner_parms = TREE_VALUE (parm_level); | |
2279 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2280 | int seen_def_arg_p = 0; | |
2281 | int i; | |
2282 | ||
2283 | for (i = 0; i < ntparms; ++i) | |
2284 | { | |
2285 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
2286 | if (TREE_PURPOSE (parm)) | |
2287 | seen_def_arg_p = 1; | |
2288 | else if (seen_def_arg_p) | |
2289 | { | |
2290 | cp_error ("no default argument for `%D'", TREE_VALUE (parm)); | |
2291 | /* For better subsequent error-recovery, we indicate that | |
2292 | there should have been a default argument. */ | |
2293 | TREE_PURPOSE (parm) = error_mark_node; | |
2294 | } | |
2295 | } | |
2296 | } | |
2297 | ||
6ba89f8e MM |
2298 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) |
2299 | /* For an ordinary class template, default template arguments are | |
2300 | allowed at the innermost level, e.g.: | |
2301 | template <class T = int> | |
2302 | struct S {}; | |
2303 | but, in a partial specialization, they're not allowed even | |
2304 | there, as we have in [temp.class.spec]: | |
2305 | ||
2306 | The template parameter list of a specialization shall not | |
2307 | contain default template argument values. | |
2308 | ||
2309 | So, for a partial specialization, or for a function template, | |
2310 | we look at all of them. */ | |
2311 | ; | |
2312 | else | |
2313 | /* But, for a primary class template that is not a partial | |
2314 | specialization we look at all template parameters except the | |
2315 | innermost ones. */ | |
2316 | parms = TREE_CHAIN (parms); | |
2317 | ||
2318 | /* Figure out what error message to issue. */ | |
2319 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2320 | msg = "default argument for template parameter in function template `%D'"; | |
2321 | else if (is_partial) | |
2322 | msg = "default argument in partial specialization `%D'"; | |
2323 | else | |
2324 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2325 | ||
2326 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2327 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2328 | examine the parameters to the class itself. On the one |
6ba89f8e MM |
2329 | hand, they will be checked when the class is defined, and, |
2330 | on the other, default arguments are legal in things like: | |
2331 | template <class T = double> | |
2332 | struct S { template <class U> void f(U); }; | |
2333 | Here the default argument for `S' has no bearing on the | |
2334 | declaration of `f'. */ | |
2335 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2336 | else | |
2337 | /* Check everything. */ | |
2338 | last_level_to_check = 0; | |
2339 | ||
66191c20 MM |
2340 | for (parm_level = parms; |
2341 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
2342 | parm_level = TREE_CHAIN (parm_level)) | |
6ba89f8e | 2343 | { |
66191c20 MM |
2344 | tree inner_parms = TREE_VALUE (parm_level); |
2345 | int i; | |
2346 | int ntparms; | |
6ba89f8e MM |
2347 | |
2348 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2349 | for (i = 0; i < ntparms; ++i) | |
2350 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2351 | { | |
2352 | if (msg) | |
2353 | { | |
2354 | cp_error (msg, decl); | |
2355 | msg = 0; | |
2356 | } | |
2357 | ||
2358 | /* Clear out the default argument so that we are not | |
2359 | confused later. */ | |
2360 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2361 | } | |
2362 | ||
2363 | /* At this point, if we're still interested in issuing messages, | |
2364 | they must apply to classes surrounding the object declared. */ | |
2365 | if (msg) | |
2366 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2367 | } | |
2368 | } | |
2369 | ||
3ac3d9ea | 2370 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2371 | parameters given by current_template_args, or reuses a |
2372 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2373 | equivalent one, if it is replaced via a call to duplicate_decls. |
2374 | ||
2375 | If IS_FRIEND is non-zero, DECL is a friend declaration. */ | |
3ac3d9ea MM |
2376 | |
2377 | tree | |
6757edfe | 2378 | push_template_decl_real (decl, is_friend) |
9a3b49ac | 2379 | tree decl; |
6757edfe | 2380 | int is_friend; |
9a3b49ac MS |
2381 | { |
2382 | tree tmpl; | |
f84b4be9 | 2383 | tree args; |
9a3b49ac | 2384 | tree info; |
f84b4be9 JM |
2385 | tree ctx; |
2386 | int primary; | |
6ba89f8e | 2387 | int is_partial; |
cfe507be | 2388 | int new_template_p = 0; |
6ba89f8e MM |
2389 | |
2390 | /* See if this is a partial specialization. */ | |
9188c363 | 2391 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e MM |
2392 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
2393 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); | |
6757edfe MM |
2394 | |
2395 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2396 | |
2397 | if (is_friend) | |
2398 | /* For a friend, we want the context of the friend function, not | |
2399 | the type of which it is a friend. */ | |
2400 | ctx = DECL_CONTEXT (decl); | |
2c73f9f5 ML |
2401 | else if (DECL_REAL_CONTEXT (decl) |
2402 | && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2403 | /* In the case of a virtual function, we want the class in which |
2404 | it is defined. */ | |
2405 | ctx = DECL_REAL_CONTEXT (decl); | |
2406 | else | |
2407 | /* Otherwise, if we're currently definining some class, the DECL | |
2408 | is assumed to be a member of the class. */ | |
9188c363 | 2409 | ctx = current_scope (); |
f84b4be9 | 2410 | |
2c73f9f5 ML |
2411 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2412 | ctx = NULL_TREE; | |
2413 | ||
2414 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2415 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2416 | |
6ba89f8e | 2417 | /* See if this is a primary template. */ |
9188c363 | 2418 | primary = pseudo_global_level_p (); |
9a3b49ac | 2419 | |
83566abf JM |
2420 | if (primary) |
2421 | { | |
2422 | if (current_lang_name == lang_name_c) | |
8251199e | 2423 | cp_error ("template with C linkage"); |
4ce3d537 MM |
2424 | else if (TREE_CODE (decl) == TYPE_DECL |
2425 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
8251199e | 2426 | cp_error ("template class without a name"); |
2aaf816d JM |
2427 | else if ((DECL_IMPLICIT_TYPEDEF_P (decl) |
2428 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2429 | || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)) | |
2430 | || TREE_CODE (decl) == FUNCTION_DECL) | |
2431 | /* OK */; | |
2432 | else | |
4ce3d537 | 2433 | cp_error ("template declaration of `%#D'", decl); |
83566abf JM |
2434 | } |
2435 | ||
6ba89f8e MM |
2436 | /* Check to see that the rules regarding the use of default |
2437 | arguments are not being violated. */ | |
2438 | check_default_tmpl_args (decl, current_template_parms, | |
2439 | primary, is_partial); | |
73aad9b9 | 2440 | |
6ba89f8e MM |
2441 | if (is_partial) |
2442 | return process_partial_specialization (decl); | |
d32789d8 | 2443 | |
9a3b49ac MS |
2444 | args = current_template_args (); |
2445 | ||
f84b4be9 JM |
2446 | if (!ctx |
2447 | || TREE_CODE (ctx) == FUNCTION_DECL | |
2448 | || TYPE_BEING_DEFINED (ctx) | |
2449 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
8d08fdba | 2450 | { |
75650646 | 2451 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
2452 | && DECL_TEMPLATE_INFO (decl) |
2453 | && DECL_TI_TEMPLATE (decl)) | |
2454 | tmpl = DECL_TI_TEMPLATE (decl); | |
2455 | else | |
786b5245 | 2456 | { |
f84b4be9 | 2457 | tmpl = build_template_decl (decl, current_template_parms); |
cfe507be MM |
2458 | new_template_p = 1; |
2459 | ||
f84b4be9 JM |
2460 | if (DECL_LANG_SPECIFIC (decl) |
2461 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2462 | { | |
2463 | /* A specialization of a member template of a template | |
2464 | class. */ | |
2465 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
2466 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
2467 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
2468 | } | |
786b5245 | 2469 | } |
8d08fdba MS |
2470 | } |
2471 | else | |
2472 | { | |
e1a5ccf7 | 2473 | tree a, t, current, parms; |
ba4f4e5d | 2474 | int i; |
6633d636 | 2475 | |
73aad9b9 | 2476 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
8251199e | 2477 | cp_error ("must specialize `%#T' before defining member `%#D'", |
73aad9b9 | 2478 | ctx, decl); |
6b9ab5cc MM |
2479 | if (TREE_CODE (decl) == TYPE_DECL) |
2480 | { | |
ed44da02 MM |
2481 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
2482 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
2483 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2484 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2485 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
2486 | else |
2487 | { | |
8251199e | 2488 | cp_error ("`%D' does not declare a template type", decl); |
6b9ab5cc MM |
2489 | return decl; |
2490 | } | |
2491 | } | |
fc378698 | 2492 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 2493 | { |
8251199e | 2494 | cp_error ("template definition of non-template `%#D'", decl); |
3ac3d9ea | 2495 | return decl; |
c91a56d2 | 2496 | } |
8d08fdba | 2497 | else |
5566b478 | 2498 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 2499 | |
e1a5ccf7 JM |
2500 | if (is_member_template (tmpl) |
2501 | && DECL_FUNCTION_TEMPLATE_P (tmpl) | |
2502 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) | |
2503 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
98c1c668 | 2504 | { |
e1a5ccf7 JM |
2505 | tree new_tmpl; |
2506 | ||
2507 | /* The declaration is a specialization of a member | |
2508 | template, declared outside the class. Therefore, the | |
2509 | innermost template arguments will be NULL, so we | |
2510 | replace them with the arguments determined by the | |
2511 | earlier call to check_explicit_specialization. */ | |
2512 | args = DECL_TI_ARGS (decl); | |
2513 | ||
2514 | new_tmpl | |
2515 | = build_template_decl (decl, current_template_parms); | |
2516 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
2517 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
2518 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
2519 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
2520 | DECL_TEMPLATE_INFO (new_tmpl) = | |
2521 | perm_tree_cons (tmpl, args, NULL_TREE); | |
2522 | ||
2523 | register_specialization (new_tmpl, tmpl, args); | |
2524 | return decl; | |
98c1c668 | 2525 | } |
98c1c668 | 2526 | |
e1a5ccf7 | 2527 | /* Make sure the template headers we got make sense. */ |
6633d636 | 2528 | |
e1a5ccf7 JM |
2529 | parms = DECL_TEMPLATE_PARMS (tmpl); |
2530 | i = TMPL_PARMS_DEPTH (parms); | |
2531 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 2532 | { |
e1a5ccf7 JM |
2533 | cp_error ("expected %d levels of template parms for `%#D', got %d", |
2534 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 2535 | } |
e1a5ccf7 JM |
2536 | else |
2537 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
2538 | { | |
2539 | a = TMPL_ARGS_LEVEL (args, i); | |
2540 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
2541 | ||
2542 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
2543 | { | |
2544 | if (current == decl) | |
2545 | cp_error ("got %d template parameters for `%#D'", | |
2546 | TREE_VEC_LENGTH (a), decl); | |
2547 | else | |
2548 | cp_error ("got %d template parameters for `%#T'", | |
2549 | TREE_VEC_LENGTH (a), current); | |
2550 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
2551 | } | |
98c1c668 | 2552 | |
e1a5ccf7 JM |
2553 | /* Perhaps we should also check that the parms are used in the |
2554 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 2555 | |
e1a5ccf7 JM |
2556 | if (current == decl) |
2557 | current = ctx; | |
2558 | else | |
2559 | current = TYPE_CONTEXT (current); | |
2560 | } | |
5566b478 | 2561 | } |
8d08fdba | 2562 | |
5566b478 MS |
2563 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
2564 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 2565 | |
36a117a5 MM |
2566 | /* Push template declarations for global functions and types. Note |
2567 | that we do not try to push a global template friend declared in a | |
2568 | template class; such a thing may well depend on the template | |
39c01e4c | 2569 | parameters of the class. */ |
cfe507be | 2570 | if (new_template_p && !ctx |
36a117a5 | 2571 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
2c73f9f5 | 2572 | tmpl = pushdecl_namespace_level (tmpl); |
8d08fdba | 2573 | |
5566b478 | 2574 | if (primary) |
6757edfe | 2575 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
5566b478 MS |
2576 | |
2577 | info = perm_tree_cons (tmpl, args, NULL_TREE); | |
2578 | ||
9188c363 | 2579 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
8d08fdba | 2580 | { |
ed44da02 MM |
2581 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2582 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
2583 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE) | |
75650646 | 2584 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 2585 | } |
2aaf816d | 2586 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 2587 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
2588 | |
2589 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
2590 | } |
2591 | ||
6757edfe MM |
2592 | tree |
2593 | push_template_decl (decl) | |
2594 | tree decl; | |
2595 | { | |
2596 | return push_template_decl_real (decl, 0); | |
2597 | } | |
2598 | ||
2599 | /* Called when a class template TYPE is redeclared with the indicated | |
2600 | template PARMS, e.g.: | |
7fe6899f MM |
2601 | |
2602 | template <class T> struct S; | |
2603 | template <class T> struct S {}; */ | |
2604 | ||
2605 | void | |
6757edfe | 2606 | redeclare_class_template (type, parms) |
7fe6899f | 2607 | tree type; |
6757edfe | 2608 | tree parms; |
7fe6899f | 2609 | { |
3d7de1fa | 2610 | tree tmpl; |
6757edfe | 2611 | tree tmpl_parms; |
7fe6899f MM |
2612 | int i; |
2613 | ||
3d7de1fa MM |
2614 | if (!TYPE_TEMPLATE_INFO (type)) |
2615 | { | |
2616 | cp_error ("`%T' is not a template type", type); | |
2617 | return; | |
2618 | } | |
2619 | ||
2620 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
2621 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
2622 | /* The type is nested in some template class. Nothing to worry | |
2623 | about here; there are no new template parameters for the nested | |
2624 | type. */ | |
2625 | return; | |
2626 | ||
6757edfe MM |
2627 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
2628 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
2629 | ||
7fe6899f MM |
2630 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
2631 | { | |
8251199e JM |
2632 | cp_error_at ("previous declaration `%D'", tmpl); |
2633 | cp_error ("used %d template parameter%s instead of %d", | |
7fe6899f MM |
2634 | TREE_VEC_LENGTH (tmpl_parms), |
2635 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
2636 | TREE_VEC_LENGTH (parms)); | |
2637 | return; | |
2638 | } | |
2639 | ||
2640 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
2641 | { | |
2642 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
2643 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
2644 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
2645 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
2646 | ||
2647 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
2648 | { | |
8251199e JM |
2649 | cp_error_at ("template parameter `%#D'", tmpl_parm); |
2650 | cp_error ("redeclared here as `%#D'", parm); | |
7fe6899f MM |
2651 | return; |
2652 | } | |
2653 | ||
2654 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
2655 | { | |
2656 | /* We have in [temp.param]: | |
2657 | ||
2658 | A template-parameter may not be given default arguments | |
2659 | by two different declarations in the same scope. */ | |
8251199e JM |
2660 | cp_error ("redefinition of default argument for `%#D'", parm); |
2661 | cp_error_at (" original definition appeared here", tmpl_parm); | |
7fe6899f MM |
2662 | return; |
2663 | } | |
2664 | ||
2665 | if (parm_default != NULL_TREE) | |
2666 | /* Update the previous template parameters (which are the ones | |
2667 | that will really count) with the new default value. */ | |
2668 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
2669 | } | |
2670 | } | |
75650646 | 2671 | |
75650646 MM |
2672 | /* Attempt to convert the non-type template parameter EXPR to the |
2673 | indicated TYPE. If the conversion is successful, return the | |
2674 | converted value. If the conversion is unsuccesful, return | |
2675 | NULL_TREE if we issued an error message, or error_mark_node if we | |
2676 | did not. We issue error messages for out-and-out bad template | |
2677 | parameters, but not simply because the conversion failed, since we | |
2678 | might be just trying to do argument deduction. By the time this | |
2679 | function is called, neither TYPE nor EXPR may make use of template | |
2680 | parameters. */ | |
2681 | ||
2682 | static tree | |
e1467ff2 | 2683 | convert_nontype_argument (type, expr) |
75650646 MM |
2684 | tree type; |
2685 | tree expr; | |
2686 | { | |
2687 | tree expr_type = TREE_TYPE (expr); | |
2688 | ||
2689 | /* A template-argument for a non-type, non-template | |
2690 | template-parameter shall be one of: | |
2691 | ||
2692 | --an integral constant-expression of integral or enumeration | |
2693 | type; or | |
2694 | ||
2695 | --the name of a non-type template-parameter; or | |
2696 | ||
2697 | --the name of an object or function with external linkage, | |
2698 | including function templates and function template-ids but | |
86052cc3 | 2699 | excluding non-static class members, expressed as id-expression; |
75650646 MM |
2700 | or |
2701 | ||
2702 | --the address of an object or function with external linkage, | |
2703 | including function templates and function template-ids but | |
2704 | excluding non-static class members, expressed as & id-expression | |
2705 | where the & is optional if the name refers to a function or | |
2706 | array; or | |
2707 | ||
2708 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
2709 | ||
638dd8fc MM |
2710 | /* An integral constant-expression can include const variables or |
2711 | enumerators. Simplify things by folding them to their values, | |
2712 | unless we're about to bind the declaration to a reference | |
2713 | parameter. */ | |
2714 | if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr) | |
2715 | && TREE_CODE (type) != REFERENCE_TYPE) | |
86052cc3 JM |
2716 | expr = decl_constant_value (expr); |
2717 | ||
7bf2682f MM |
2718 | if (is_overloaded_fn (expr)) |
2719 | /* OK for now. We'll check that it has external linkage later. | |
2720 | Check this first since if expr_type is the unknown_type_node | |
2721 | we would otherwise complain below. */ | |
2722 | ; | |
e08a8f45 MM |
2723 | else if (TYPE_PTRMEM_P (expr_type) |
2724 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2725 | { | |
2726 | if (TREE_CODE (expr) != PTRMEM_CST) | |
2727 | goto bad_argument; | |
2728 | } | |
f3400fe2 | 2729 | else if (TYPE_PTR_P (expr_type) |
e08a8f45 | 2730 | || TYPE_PTRMEM_P (expr_type) |
f3400fe2 JM |
2731 | || TREE_CODE (expr_type) == ARRAY_TYPE |
2732 | || TREE_CODE (type) == REFERENCE_TYPE | |
75650646 MM |
2733 | /* If expr is the address of an overloaded function, we |
2734 | will get the unknown_type_node at this point. */ | |
2735 | || expr_type == unknown_type_node) | |
2736 | { | |
2737 | tree referent; | |
49bf0d6f MM |
2738 | tree e = expr; |
2739 | STRIP_NOPS (e); | |
75650646 | 2740 | |
f3400fe2 JM |
2741 | if (TREE_CODE (type) == REFERENCE_TYPE |
2742 | || TREE_CODE (expr_type) == ARRAY_TYPE) | |
2743 | referent = e; | |
2744 | else | |
75650646 | 2745 | { |
f3400fe2 JM |
2746 | if (TREE_CODE (e) != ADDR_EXPR) |
2747 | { | |
2748 | bad_argument: | |
2749 | cp_error ("`%E' is not a valid template argument", expr); | |
e08a8f45 MM |
2750 | if (TYPE_PTR_P (expr_type)) |
2751 | { | |
2752 | if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) | |
2753 | cp_error ("it must be the address of a function with external linkage"); | |
2754 | else | |
2755 | cp_error ("it must be the address of an object with external linkage"); | |
2756 | } | |
2757 | else if (TYPE_PTRMEM_P (expr_type) | |
2758 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2759 | cp_error ("it must be a pointer-to-member of the form `&X::Y'"); | |
2760 | ||
f3400fe2 JM |
2761 | return NULL_TREE; |
2762 | } | |
2763 | ||
2764 | referent = TREE_OPERAND (e, 0); | |
2765 | STRIP_NOPS (referent); | |
75650646 MM |
2766 | } |
2767 | ||
75650646 MM |
2768 | if (TREE_CODE (referent) == STRING_CST) |
2769 | { | |
8251199e | 2770 | cp_error ("string literal %E is not a valid template argument", |
75650646 | 2771 | referent); |
8251199e | 2772 | error ("because it is the address of an object with static linkage"); |
75650646 MM |
2773 | return NULL_TREE; |
2774 | } | |
2775 | ||
2776 | if (is_overloaded_fn (referent)) | |
2777 | /* We'll check that it has external linkage later. */ | |
2778 | ; | |
2779 | else if (TREE_CODE (referent) != VAR_DECL) | |
2780 | goto bad_argument; | |
2781 | else if (!TREE_PUBLIC (referent)) | |
2782 | { | |
8251199e | 2783 | cp_error ("address of non-extern `%E' cannot be used as template argument", referent); |
75650646 MM |
2784 | return error_mark_node; |
2785 | } | |
2786 | } | |
f3400fe2 JM |
2787 | else if (INTEGRAL_TYPE_P (expr_type) |
2788 | || TYPE_PTRMEM_P (expr_type) | |
2789 | || TYPE_PTRMEMFUNC_P (expr_type) | |
2790 | /* The next two are g++ extensions. */ | |
2791 | || TREE_CODE (expr_type) == REAL_TYPE | |
2792 | || TREE_CODE (expr_type) == COMPLEX_TYPE) | |
75650646 | 2793 | { |
f3400fe2 JM |
2794 | if (! TREE_CONSTANT (expr)) |
2795 | { | |
2796 | non_constant: | |
2797 | cp_error ("non-constant `%E' cannot be used as template argument", | |
2798 | expr); | |
2799 | return NULL_TREE; | |
2800 | } | |
75650646 | 2801 | } |
75650646 MM |
2802 | else |
2803 | { | |
8251199e | 2804 | cp_error ("object `%E' cannot be used as template argument", expr); |
75650646 MM |
2805 | return NULL_TREE; |
2806 | } | |
2807 | ||
2808 | switch (TREE_CODE (type)) | |
2809 | { | |
2810 | case INTEGER_TYPE: | |
2811 | case BOOLEAN_TYPE: | |
2812 | case ENUMERAL_TYPE: | |
2813 | /* For a non-type template-parameter of integral or enumeration | |
2814 | type, integral promotions (_conv.prom_) and integral | |
2815 | conversions (_conv.integral_) are applied. */ | |
2816 | if (!INTEGRAL_TYPE_P (expr_type)) | |
2817 | return error_mark_node; | |
2818 | ||
2819 | /* It's safe to call digest_init in this case; we know we're | |
2820 | just converting one integral constant expression to another. */ | |
db02b6b9 | 2821 | expr = digest_init (type, expr, (tree*) 0); |
75650646 | 2822 | |
db02b6b9 | 2823 | if (TREE_CODE (expr) != INTEGER_CST) |
f3400fe2 | 2824 | /* Curiously, some TREE_CONSTANT integral expressions do not |
db02b6b9 MM |
2825 | simplify to integer constants. For example, `3 % 0', |
2826 | remains a TRUNC_MOD_EXPR. */ | |
2827 | goto non_constant; | |
2828 | ||
2829 | return expr; | |
2830 | ||
abff8e06 JM |
2831 | case REAL_TYPE: |
2832 | case COMPLEX_TYPE: | |
2833 | /* These are g++ extensions. */ | |
2834 | if (TREE_CODE (expr_type) != TREE_CODE (type)) | |
2835 | return error_mark_node; | |
2836 | ||
db02b6b9 MM |
2837 | expr = digest_init (type, expr, (tree*) 0); |
2838 | ||
2839 | if (TREE_CODE (expr) != REAL_CST) | |
2840 | goto non_constant; | |
2841 | ||
2842 | return expr; | |
abff8e06 | 2843 | |
75650646 MM |
2844 | case POINTER_TYPE: |
2845 | { | |
2846 | tree type_pointed_to = TREE_TYPE (type); | |
2847 | ||
2848 | if (TYPE_PTRMEM_P (type)) | |
93aa462d MM |
2849 | { |
2850 | tree e; | |
2851 | ||
2852 | /* For a non-type template-parameter of type pointer to data | |
2853 | member, qualification conversions (_conv.qual_) are | |
2854 | applied. */ | |
2855 | e = perform_qualification_conversions (type, expr); | |
2856 | if (TREE_CODE (e) == NOP_EXPR) | |
87533b37 MM |
2857 | /* The call to perform_qualification_conversions will |
2858 | insert a NOP_EXPR over EXPR to do express conversion, | |
2859 | if necessary. But, that will confuse us if we use | |
2860 | this (converted) template parameter to instantiate | |
2861 | another template; then the thing will not look like a | |
2862 | valid template argument. So, just make a new | |
2863 | constant, of the appropriate type. */ | |
2864 | e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); | |
93aa462d MM |
2865 | return e; |
2866 | } | |
75650646 MM |
2867 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) |
2868 | { | |
2869 | /* For a non-type template-parameter of type pointer to | |
2870 | function, only the function-to-pointer conversion | |
2871 | (_conv.func_) is applied. If the template-argument | |
2872 | represents a set of overloaded functions (or a pointer to | |
2873 | such), the matching function is selected from the set | |
2874 | (_over.over_). */ | |
2875 | tree fns; | |
2876 | tree fn; | |
2877 | ||
7bf2682f | 2878 | if (TREE_CODE (expr) == ADDR_EXPR) |
75650646 MM |
2879 | fns = TREE_OPERAND (expr, 0); |
2880 | else | |
2881 | fns = expr; | |
2882 | ||
a05273d4 | 2883 | fn = instantiate_type (type_pointed_to, fns, 0); |
75650646 MM |
2884 | |
2885 | if (fn == error_mark_node) | |
2886 | return error_mark_node; | |
2887 | ||
2888 | if (!TREE_PUBLIC (fn)) | |
2889 | { | |
2890 | if (really_overloaded_fn (fns)) | |
2891 | return error_mark_node; | |
2892 | else | |
2893 | goto bad_argument; | |
2894 | } | |
2895 | ||
2896 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2897 | ||
3bfdc719 | 2898 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2899 | 0); |
2900 | return expr; | |
2901 | } | |
2902 | else | |
2903 | { | |
2904 | /* For a non-type template-parameter of type pointer to | |
2905 | object, qualification conversions (_conv.qual_) and the | |
2906 | array-to-pointer conversion (_conv.array_) are applied. | |
2907 | [Note: In particular, neither the null pointer conversion | |
2908 | (_conv.ptr_) nor the derived-to-base conversion | |
2909 | (_conv.ptr_) are applied. Although 0 is a valid | |
2910 | template-argument for a non-type template-parameter of | |
2911 | integral type, it is not a valid template-argument for a | |
e1467ff2 MM |
2912 | non-type template-parameter of pointer type.] |
2913 | ||
2914 | The call to decay_conversion performs the | |
2915 | array-to-pointer conversion, if appropriate. */ | |
2916 | expr = decay_conversion (expr); | |
75650646 MM |
2917 | |
2918 | if (expr == error_mark_node) | |
2919 | return error_mark_node; | |
2920 | else | |
2921 | return perform_qualification_conversions (type, expr); | |
2922 | } | |
2923 | } | |
2924 | break; | |
2925 | ||
2926 | case REFERENCE_TYPE: | |
2927 | { | |
2928 | tree type_referred_to = TREE_TYPE (type); | |
2929 | ||
2930 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
2931 | { | |
2932 | /* For a non-type template-parameter of type reference to | |
39c01e4c MM |
2933 | function, no conversions apply. If the |
2934 | template-argument represents a set of overloaded | |
2935 | functions, the matching function is selected from the | |
2936 | set (_over.over_). */ | |
75650646 MM |
2937 | tree fns = expr; |
2938 | tree fn; | |
2939 | ||
a05273d4 | 2940 | fn = instantiate_type (type_referred_to, fns, 0); |
e6e174e5 JM |
2941 | |
2942 | if (fn == error_mark_node) | |
2943 | return error_mark_node; | |
75650646 MM |
2944 | |
2945 | if (!TREE_PUBLIC (fn)) | |
2946 | { | |
2947 | if (really_overloaded_fn (fns)) | |
2948 | /* Don't issue an error here; we might get a different | |
2949 | function if the overloading had worked out | |
2950 | differently. */ | |
2951 | return error_mark_node; | |
2952 | else | |
2953 | goto bad_argument; | |
2954 | } | |
2955 | ||
3bfdc719 MM |
2956 | my_friendly_assert (same_type_p (type_referred_to, |
2957 | TREE_TYPE (fn)), | |
75650646 MM |
2958 | 0); |
2959 | ||
2960 | return fn; | |
2961 | } | |
2962 | else | |
2963 | { | |
2964 | /* For a non-type template-parameter of type reference to | |
2965 | object, no conversions apply. The type referred to by the | |
2966 | reference may be more cv-qualified than the (otherwise | |
2967 | identical) type of the template-argument. The | |
2968 | template-parameter is bound directly to the | |
2969 | template-argument, which must be an lvalue. */ | |
ff350acd JM |
2970 | if ((TYPE_MAIN_VARIANT (expr_type) |
2971 | != TYPE_MAIN_VARIANT (type_referred_to)) | |
91063b51 MM |
2972 | || !at_least_as_qualified_p (type_referred_to, |
2973 | expr_type) | |
75650646 MM |
2974 | || !real_lvalue_p (expr)) |
2975 | return error_mark_node; | |
2976 | else | |
2977 | return expr; | |
2978 | } | |
2979 | } | |
2980 | break; | |
2981 | ||
2982 | case RECORD_TYPE: | |
2983 | { | |
410c2fba KL |
2984 | if (!TYPE_PTRMEMFUNC_P (type)) |
2985 | /* This handles templates like | |
2986 | template<class T, T t> void f(); | |
2987 | when T is substituted with any class. The second template | |
2988 | parameter becomes invalid and the template candidate is | |
2989 | rejected. */ | |
2990 | return error_mark_node; | |
75650646 MM |
2991 | |
2992 | /* For a non-type template-parameter of type pointer to member | |
2993 | function, no conversions apply. If the template-argument | |
2994 | represents a set of overloaded member functions, the | |
2995 | matching member function is selected from the set | |
2996 | (_over.over_). */ | |
2997 | ||
2998 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
2999 | expr_type != unknown_type_node) | |
3000 | return error_mark_node; | |
3001 | ||
e08a8f45 | 3002 | if (TREE_CODE (expr) == PTRMEM_CST) |
75650646 MM |
3003 | { |
3004 | /* A ptr-to-member constant. */ | |
3bfdc719 | 3005 | if (!same_type_p (type, expr_type)) |
75650646 MM |
3006 | return error_mark_node; |
3007 | else | |
3008 | return expr; | |
3009 | } | |
3010 | ||
3011 | if (TREE_CODE (expr) != ADDR_EXPR) | |
3012 | return error_mark_node; | |
3013 | ||
d8f8dca1 | 3014 | expr = instantiate_type (type, expr, 0); |
75650646 | 3015 | |
d8f8dca1 | 3016 | if (expr == error_mark_node) |
75650646 MM |
3017 | return error_mark_node; |
3018 | ||
3bfdc719 | 3019 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
3020 | 0); |
3021 | return expr; | |
3022 | } | |
3023 | break; | |
3024 | ||
3025 | default: | |
3026 | /* All non-type parameters must have one of these types. */ | |
3027 | my_friendly_abort (0); | |
3028 | break; | |
3029 | } | |
3030 | ||
3031 | return error_mark_node; | |
3032 | } | |
3033 | ||
744fac59 KL |
3034 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
3035 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
3036 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
3037 | or PARM_DECL. | |
3038 | ||
3039 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
3040 | the case, then extra parameters must have default arguments. | |
3041 | ||
3042 | Consider the example: | |
3043 | template <class T, class Allocator = allocator> class vector; | |
3044 | template<template <class U> class TT> class C; | |
3045 | ||
3046 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
3047 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
3048 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
3049 | substitute the TT parameter. */ | |
3050 | ||
3051 | static int | |
4393e105 MM |
3052 | coerce_template_template_parms (parm_parms, arg_parms, complain, |
3053 | in_decl, outer_args) | |
3054 | tree parm_parms, arg_parms; | |
3055 | int complain; | |
3056 | tree in_decl, outer_args; | |
744fac59 KL |
3057 | { |
3058 | int nparms, nargs, i; | |
3059 | tree parm, arg; | |
3060 | ||
3061 | my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); | |
3062 | my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); | |
3063 | ||
3064 | nparms = TREE_VEC_LENGTH (parm_parms); | |
3065 | nargs = TREE_VEC_LENGTH (arg_parms); | |
3066 | ||
3067 | /* The rule here is opposite of coerce_template_parms. */ | |
3068 | if (nargs < nparms | |
3069 | || (nargs > nparms | |
3070 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
3071 | return 0; | |
3072 | ||
3073 | for (i = 0; i < nparms; ++i) | |
3074 | { | |
3075 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
3076 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
3077 | ||
3078 | if (arg == NULL_TREE || arg == error_mark_node | |
3079 | || parm == NULL_TREE || parm == error_mark_node) | |
3080 | return 0; | |
3081 | ||
3082 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
3083 | return 0; | |
3084 | ||
3085 | switch (TREE_CODE (parm)) | |
3086 | { | |
3087 | case TYPE_DECL: | |
3088 | break; | |
3089 | ||
3090 | case TEMPLATE_DECL: | |
3091 | /* We encounter instantiations of templates like | |
3092 | template <template <template <class> class> class TT> | |
3093 | class C; */ | |
700466c2 JM |
3094 | { |
3095 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3096 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3097 | ||
3098 | if (!coerce_template_template_parms (parmparm, argparm, | |
4393e105 MM |
3099 | complain, in_decl, |
3100 | outer_args)) | |
700466c2 JM |
3101 | return 0; |
3102 | } | |
3103 | break; | |
744fac59 KL |
3104 | |
3105 | case PARM_DECL: | |
3106 | /* The tsubst call is used to handle cases such as | |
3107 | template <class T, template <T> class TT> class D; | |
3108 | i.e. the parameter list of TT depends on earlier parameters. */ | |
4393e105 MM |
3109 | if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, |
3110 | complain, in_decl), | |
3bfdc719 | 3111 | TREE_TYPE (arg))) |
744fac59 KL |
3112 | return 0; |
3113 | break; | |
3114 | ||
3115 | default: | |
3116 | my_friendly_abort (0); | |
3117 | } | |
3118 | } | |
3119 | return 1; | |
3120 | } | |
3121 | ||
8b5b8b7c MM |
3122 | /* Convert the indicated template ARG as necessary to match the |
3123 | indicated template PARM. Returns the converted ARG, or | |
3124 | error_mark_node if the conversion was unsuccessful. Error messages | |
3125 | are issued if COMPLAIN is non-zero. This conversion is for the Ith | |
3126 | parameter in the parameter list. ARGS is the full set of template | |
3127 | arguments deduced so far. */ | |
3128 | ||
3129 | static tree | |
3130 | convert_template_argument (parm, arg, args, complain, i, in_decl) | |
3131 | tree parm; | |
3132 | tree arg; | |
3133 | tree args; | |
3134 | int complain; | |
3135 | int i; | |
3136 | tree in_decl; | |
3137 | { | |
3138 | tree val; | |
3139 | tree inner_args; | |
3140 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
3141 | ||
3142 | inner_args = innermost_args (args); | |
3143 | ||
3144 | if (TREE_CODE (arg) == TREE_LIST | |
3145 | && TREE_TYPE (arg) != NULL_TREE | |
3146 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
3147 | { | |
3148 | /* The template argument was the name of some | |
3149 | member function. That's usually | |
3150 | illegal, but static members are OK. In any | |
3151 | case, grab the underlying fields/functions | |
3152 | and issue an error later if required. */ | |
3153 | arg = TREE_VALUE (arg); | |
3154 | TREE_TYPE (arg) = unknown_type_node; | |
3155 | } | |
3156 | ||
3157 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3158 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3159 | || requires_tmpl_type); | |
3160 | ||
3161 | /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, | |
3162 | we also accept implicitly created TYPE_DECL as a valid argument. | |
3163 | This is necessary to handle the case where we pass a template name | |
3164 | to a template template parameter in a scope where we've derived from | |
3165 | in instantiation of that template, so the template name refers to that | |
3166 | instantiation. We really ought to handle this better. */ | |
3167 | is_tmpl_type | |
3168 | = ((TREE_CODE (arg) == TEMPLATE_DECL | |
3169 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3170 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 3171 | && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg)) |
8b5b8b7c MM |
3172 | || (TREE_CODE (arg) == RECORD_TYPE |
3173 | && CLASSTYPE_TEMPLATE_INFO (arg) | |
3174 | && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL | |
3175 | && DECL_ARTIFICIAL (TYPE_NAME (arg)) | |
3176 | && requires_tmpl_type | |
b54ccf71 | 3177 | && is_base_of_enclosing_class (arg, current_class_type))); |
8b5b8b7c MM |
3178 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
3179 | arg = TYPE_STUB_DECL (arg); | |
3180 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) | |
3181 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
3182 | ||
3183 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type; | |
3184 | ||
3185 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3186 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3187 | { | |
8251199e JM |
3188 | cp_pedwarn ("to refer to a type member of a template parameter,"); |
3189 | cp_pedwarn (" use `typename %E'", arg); | |
8b5b8b7c MM |
3190 | |
3191 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3192 | TREE_OPERAND (arg, 1)); | |
3193 | is_type = 1; | |
3194 | } | |
3195 | if (is_type != requires_type) | |
3196 | { | |
3197 | if (in_decl) | |
3198 | { | |
3199 | if (complain) | |
3200 | { | |
8251199e | 3201 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3202 | i + 1, in_decl); |
3203 | if (is_type) | |
8251199e | 3204 | cp_error (" expected a constant of type `%T', got `%T'", |
8b5b8b7c MM |
3205 | TREE_TYPE (parm), |
3206 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
3207 | else | |
8251199e | 3208 | cp_error (" expected a type, got `%E'", arg); |
8b5b8b7c MM |
3209 | } |
3210 | } | |
3211 | return error_mark_node; | |
3212 | } | |
3213 | if (is_tmpl_type ^ requires_tmpl_type) | |
3214 | { | |
3215 | if (in_decl && complain) | |
3216 | { | |
8251199e | 3217 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3218 | i + 1, in_decl); |
3219 | if (is_tmpl_type) | |
8251199e | 3220 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); |
8b5b8b7c | 3221 | else |
8251199e | 3222 | cp_error (" expected a class template, got `%T'", arg); |
8b5b8b7c MM |
3223 | } |
3224 | return error_mark_node; | |
3225 | } | |
3226 | ||
3227 | if (is_type) | |
3228 | { | |
3229 | if (requires_tmpl_type) | |
3230 | { | |
3231 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3232 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3233 | ||
4393e105 | 3234 | if (coerce_template_template_parms (parmparm, argparm, complain, |
8b5b8b7c MM |
3235 | in_decl, inner_args)) |
3236 | { | |
3237 | val = arg; | |
3238 | ||
3239 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3240 | TEMPLATE_DECL. */ | |
3241 | if (val != error_mark_node | |
3242 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3243 | val = TREE_TYPE (val); | |
3244 | } | |
3245 | else | |
3246 | { | |
3247 | if (in_decl && complain) | |
3248 | { | |
8251199e | 3249 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c | 3250 | i + 1, in_decl); |
8251199e | 3251 | cp_error (" expected a template of type `%D', got `%D'", parm, arg); |
8b5b8b7c MM |
3252 | } |
3253 | ||
3254 | val = error_mark_node; | |
3255 | } | |
3256 | } | |
3257 | else | |
3258 | { | |
3259 | val = groktypename (arg); | |
3260 | if (! processing_template_decl) | |
3261 | { | |
3262 | /* [basic.link]: A name with no linkage (notably, the | |
3263 | name of a class or enumeration declared in a local | |
3264 | scope) shall not be used to declare an entity with | |
3265 | linkage. This implies that names with no linkage | |
3266 | cannot be used as template arguments. */ | |
3267 | tree t = no_linkage_check (val); | |
3268 | if (t) | |
3269 | { | |
3270 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
8251199e JM |
3271 | cp_pedwarn |
3272 | ("template-argument `%T' uses anonymous type", val); | |
8b5b8b7c | 3273 | else |
8251199e JM |
3274 | cp_error |
3275 | ("template-argument `%T' uses local type `%T'", | |
8b5b8b7c MM |
3276 | val, t); |
3277 | return error_mark_node; | |
3278 | } | |
3279 | } | |
3280 | } | |
3281 | } | |
3282 | else | |
3283 | { | |
4393e105 | 3284 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c MM |
3285 | |
3286 | if (processing_template_decl) | |
3287 | arg = maybe_fold_nontype_arg (arg); | |
3288 | ||
3289 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
3290 | /* We used to call digest_init here. However, digest_init | |
3291 | will report errors, which we don't want when complain | |
3292 | is zero. More importantly, digest_init will try too | |
3293 | hard to convert things: for example, `0' should not be | |
3294 | converted to pointer type at this point according to | |
3295 | the standard. Accepting this is not merely an | |
3296 | extension, since deciding whether or not these | |
3297 | conversions can occur is part of determining which | |
3298 | function template to call, or whether a given epxlicit | |
3299 | argument specification is legal. */ | |
3300 | val = convert_nontype_argument (t, arg); | |
3301 | else | |
3302 | val = arg; | |
3303 | ||
3304 | if (val == NULL_TREE) | |
3305 | val = error_mark_node; | |
3306 | else if (val == error_mark_node && complain) | |
8251199e | 3307 | cp_error ("could not convert template argument `%E' to `%T'", |
8b5b8b7c MM |
3308 | arg, t); |
3309 | } | |
3310 | ||
3311 | return val; | |
3312 | } | |
3313 | ||
3314 | /* Convert all template arguments to their appropriate types, and | |
3315 | return a vector containing the innermost resulting template | |
3316 | arguments. If any error occurs, return error_mark_node, and, if | |
3317 | COMPLAIN is non-zero, issue an error message. Some error messages | |
3318 | are issued even if COMPLAIN is zero; for instance, if a template | |
3319 | argument is composed from a local class. | |
75650646 MM |
3320 | |
3321 | If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be | |
3322 | provided in ARGLIST, or else trailing parameters must have default | |
3323 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
cabc336a MM |
3324 | deduction for any unspecified trailing arguments. |
3325 | ||
3326 | The resulting TREE_VEC is allocated on a temporary obstack, and | |
3327 | must be explicitly copied if it will be permanent. */ | |
de575009 | 3328 | |
8d08fdba | 3329 | static tree |
8b5b8b7c | 3330 | coerce_template_parms (parms, args, in_decl, |
75650646 | 3331 | complain, |
744fac59 | 3332 | require_all_arguments) |
8b5b8b7c | 3333 | tree parms, args; |
8d08fdba | 3334 | tree in_decl; |
75650646 MM |
3335 | int complain; |
3336 | int require_all_arguments; | |
8d08fdba | 3337 | { |
a292b002 | 3338 | int nparms, nargs, i, lost = 0; |
e4a84209 | 3339 | tree inner_args; |
8b5b8b7c MM |
3340 | tree new_args; |
3341 | tree new_inner_args; | |
a292b002 | 3342 | |
8b5b8b7c | 3343 | inner_args = innermost_args (args); |
e4a84209 | 3344 | nargs = NUM_TMPL_ARGS (inner_args); |
a292b002 MS |
3345 | nparms = TREE_VEC_LENGTH (parms); |
3346 | ||
3347 | if (nargs > nparms | |
3348 | || (nargs < nparms | |
75650646 | 3349 | && require_all_arguments |
a292b002 | 3350 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3351 | { |
75650646 MM |
3352 | if (complain) |
3353 | { | |
8251199e | 3354 | cp_error ("wrong number of template arguments (%d, should be %d)", |
e4a84209 | 3355 | nargs, nparms); |
75650646 MM |
3356 | |
3357 | if (in_decl) | |
8251199e | 3358 | cp_error_at ("provided for `%D'", in_decl); |
75650646 MM |
3359 | } |
3360 | ||
8d08fdba MS |
3361 | return error_mark_node; |
3362 | } | |
3363 | ||
cabc336a | 3364 | new_inner_args = make_temp_vec (nparms); |
8b5b8b7c MM |
3365 | new_args = add_outermost_template_args (args, new_inner_args); |
3366 | for (i = 0; i < nparms; i++) | |
8d08fdba | 3367 | { |
8b5b8b7c MM |
3368 | tree arg; |
3369 | tree parm; | |
e4a84209 | 3370 | |
8b5b8b7c MM |
3371 | /* Get the Ith template parameter. */ |
3372 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 3373 | |
8b5b8b7c MM |
3374 | /* Calculate the Ith argument. */ |
3375 | if (inner_args && TREE_CODE (inner_args) == TREE_LIST) | |
8d08fdba | 3376 | { |
8b5b8b7c MM |
3377 | arg = TREE_VALUE (inner_args); |
3378 | inner_args = TREE_CHAIN (inner_args); | |
8d08fdba | 3379 | } |
8b5b8b7c MM |
3380 | else if (i < nargs) |
3381 | arg = TREE_VEC_ELT (inner_args, i); | |
3382 | /* If no template argument was supplied, look for a default | |
3383 | value. */ | |
3384 | else if (TREE_PURPOSE (parm) == NULL_TREE) | |
3385 | { | |
3386 | /* There was no default value. */ | |
3387 | my_friendly_assert (!require_all_arguments, 0); | |
3388 | break; | |
3389 | } | |
3390 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
4393e105 | 3391 | arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); |
8b5b8b7c | 3392 | else |
4393e105 MM |
3393 | arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, |
3394 | in_decl); | |
e4a84209 | 3395 | |
8b5b8b7c | 3396 | /* Now, convert the Ith argument, as necessary. */ |
75650646 MM |
3397 | if (arg == NULL_TREE) |
3398 | /* We're out of arguments. */ | |
3399 | { | |
3400 | my_friendly_assert (!require_all_arguments, 0); | |
3401 | break; | |
3402 | } | |
8b5b8b7c | 3403 | else if (arg == error_mark_node) |
75650646 | 3404 | { |
8251199e | 3405 | cp_error ("template argument %d is invalid", i + 1); |
8b5b8b7c | 3406 | arg = error_mark_node; |
8d08fdba | 3407 | } |
8b5b8b7c MM |
3408 | else |
3409 | arg = convert_template_argument (TREE_VALUE (parm), | |
3410 | arg, new_args, complain, i, | |
3411 | in_decl); | |
3412 | ||
3413 | if (arg == error_mark_node) | |
8d08fdba | 3414 | lost++; |
8b5b8b7c | 3415 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 3416 | } |
8b5b8b7c | 3417 | |
8d08fdba MS |
3418 | if (lost) |
3419 | return error_mark_node; | |
8b5b8b7c MM |
3420 | |
3421 | return new_inner_args; | |
8d08fdba MS |
3422 | } |
3423 | ||
34016c81 JM |
3424 | /* Returns 1 if template args OT and NT are equivalent. */ |
3425 | ||
d8e178a0 | 3426 | static int |
34016c81 JM |
3427 | template_args_equal (ot, nt) |
3428 | tree ot, nt; | |
3429 | { | |
3430 | if (nt == ot) | |
3431 | return 1; | |
3432 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
3433 | return 0; | |
3434 | if (TREE_CODE (nt) == TREE_VEC) | |
3435 | /* For member templates */ | |
3436 | return comp_template_args (ot, nt); | |
3437 | else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') | |
3bfdc719 | 3438 | return same_type_p (ot, nt); |
34016c81 JM |
3439 | else |
3440 | return (cp_tree_equal (ot, nt) > 0); | |
3441 | } | |
3442 | ||
3443 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
3444 | of template arguments. Returns 0 otherwise. */ |
3445 | ||
6757edfe | 3446 | int |
5566b478 MS |
3447 | comp_template_args (oldargs, newargs) |
3448 | tree oldargs, newargs; | |
3449 | { | |
3450 | int i; | |
3451 | ||
386b8a85 JM |
3452 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
3453 | return 0; | |
3454 | ||
5566b478 MS |
3455 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
3456 | { | |
3457 | tree nt = TREE_VEC_ELT (newargs, i); | |
3458 | tree ot = TREE_VEC_ELT (oldargs, i); | |
3459 | ||
34016c81 | 3460 | if (! template_args_equal (ot, nt)) |
61a127b3 | 3461 | return 0; |
5566b478 MS |
3462 | } |
3463 | return 1; | |
3464 | } | |
3465 | ||
8d08fdba MS |
3466 | /* Given class template name and parameter list, produce a user-friendly name |
3467 | for the instantiation. */ | |
e92cc029 | 3468 | |
8d08fdba | 3469 | static char * |
36a117a5 | 3470 | mangle_class_name_for_template (name, parms, arglist) |
8d08fdba MS |
3471 | char *name; |
3472 | tree parms, arglist; | |
3473 | { | |
3474 | static struct obstack scratch_obstack; | |
3475 | static char *scratch_firstobj; | |
3476 | int i, nparms; | |
8d08fdba MS |
3477 | |
3478 | if (!scratch_firstobj) | |
fc378698 | 3479 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
3480 | else |
3481 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 3482 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 3483 | |
8d08fdba | 3484 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); |
8d08fdba | 3485 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) |
8d08fdba MS |
3486 | |
3487 | cat (name); | |
3488 | ccat ('<'); | |
3489 | nparms = TREE_VEC_LENGTH (parms); | |
36a117a5 | 3490 | arglist = innermost_args (arglist); |
8d08fdba MS |
3491 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); |
3492 | for (i = 0; i < nparms; i++) | |
3493 | { | |
a292b002 MS |
3494 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
3495 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
3496 | |
3497 | if (i) | |
3498 | ccat (','); | |
3499 | ||
a292b002 | 3500 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 3501 | { |
294471c3 | 3502 | cat (type_as_string_real (arg, 0, 1)); |
8d08fdba MS |
3503 | continue; |
3504 | } | |
73b0fce8 KL |
3505 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
3506 | { | |
3507 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
3508 | { |
3509 | /* Already substituted with real template. Just output | |
3510 | the template name here */ | |
cb0dbb9a JM |
3511 | tree context = DECL_CONTEXT (arg); |
3512 | if (context) | |
2c73f9f5 | 3513 | { |
cb0dbb9a | 3514 | my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422); |
2c73f9f5 ML |
3515 | cat(decl_as_string (DECL_CONTEXT (arg), 0)); |
3516 | cat("::"); | |
3517 | } | |
3518 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3519 | } | |
73b0fce8 KL |
3520 | else |
3521 | /* Output the parameter declaration */ | |
294471c3 | 3522 | cat (type_as_string_real (arg, 0, 1)); |
73b0fce8 KL |
3523 | continue; |
3524 | } | |
8d08fdba MS |
3525 | else |
3526 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
3527 | ||
3528 | if (TREE_CODE (arg) == TREE_LIST) | |
3529 | { | |
3530 | /* New list cell was built because old chain link was in | |
3531 | use. */ | |
3532 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
3533 | arg = TREE_VALUE (arg); | |
3534 | } | |
3535 | /* No need to check arglist against parmlist here; we did that | |
3536 | in coerce_template_parms, called from lookup_template_class. */ | |
3537 | cat (expr_as_string (arg, 0)); | |
3538 | } | |
3539 | { | |
3540 | char *bufp = obstack_next_free (&scratch_obstack); | |
3541 | int offset = 0; | |
3542 | while (bufp[offset - 1] == ' ') | |
3543 | offset--; | |
3544 | obstack_blank_fast (&scratch_obstack, offset); | |
3545 | ||
3546 | /* B<C<char> >, not B<C<char>> */ | |
3547 | if (bufp[offset - 1] == '>') | |
3548 | ccat (' '); | |
3549 | } | |
3550 | ccat ('>'); | |
3551 | ccat ('\0'); | |
3552 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
3553 | } |
3554 | ||
bd6dd845 | 3555 | static tree |
5566b478 MS |
3556 | classtype_mangled_name (t) |
3557 | tree t; | |
3558 | { | |
3559 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
3560 | /* Specializations have already had their names set up in |
3561 | lookup_template_class. */ | |
9fbf56f7 MM |
3562 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
3563 | { | |
3564 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
3565 | ||
36a117a5 MM |
3566 | /* For non-primary templates, the template parameters are |
3567 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
3568 | if (PRIMARY_TEMPLATE_P (tmpl)) |
3569 | { | |
3570 | tree name = DECL_NAME (tmpl); | |
3571 | char *mangled_name = mangle_class_name_for_template | |
3572 | (IDENTIFIER_POINTER (name), | |
3573 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
3574 | CLASSTYPE_TI_ARGS (t)); | |
3575 | tree id = get_identifier (mangled_name); | |
3576 | IDENTIFIER_TEMPLATE (id) = name; | |
3577 | return id; | |
3578 | } | |
5566b478 | 3579 | } |
9fbf56f7 MM |
3580 | |
3581 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
3582 | } |
3583 | ||
3584 | static void | |
3585 | add_pending_template (d) | |
3586 | tree d; | |
3587 | { | |
e92cc029 MS |
3588 | tree ti; |
3589 | ||
3590 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
3591 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
3592 | else | |
3593 | ti = DECL_TEMPLATE_INFO (d); | |
3594 | ||
824b9a4c | 3595 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
3596 | return; |
3597 | ||
3598 | *template_tail = perm_tree_cons | |
1139b3d8 | 3599 | (build_srcloc_here (), d, NULL_TREE); |
5566b478 | 3600 | template_tail = &TREE_CHAIN (*template_tail); |
824b9a4c | 3601 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
3602 | } |
3603 | ||
386b8a85 JM |
3604 | |
3605 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
3606 | may be either a _DECL or an overloaded function or an | |
3607 | IDENTIFIER_NODE), and ARGLIST. */ | |
3608 | ||
3609 | tree | |
3610 | lookup_template_function (fns, arglist) | |
3611 | tree fns, arglist; | |
3612 | { | |
2c73f9f5 | 3613 | tree type; |
050367a3 | 3614 | |
386b8a85 JM |
3615 | if (fns == NULL_TREE) |
3616 | { | |
8251199e | 3617 | cp_error ("non-template used as template"); |
386b8a85 JM |
3618 | return error_mark_node; |
3619 | } | |
3620 | ||
2c73f9f5 ML |
3621 | type = TREE_TYPE (fns); |
3622 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
3623 | type = unknown_type_node; | |
3624 | ||
672476cb MM |
3625 | if (processing_template_decl) |
3626 | return build_min (TEMPLATE_ID_EXPR, type, fns, arglist); | |
3627 | else | |
3628 | return build (TEMPLATE_ID_EXPR, type, fns, arglist); | |
386b8a85 JM |
3629 | } |
3630 | ||
a2b60a0e MM |
3631 | /* Within the scope of a template class S<T>, the name S gets bound |
3632 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
3633 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
3634 | or one of its enclosing classes, and that type is a template, | |
3635 | return the associated TEMPLATE_DECL. Otherwise, the original | |
3636 | DECL is returned. */ | |
3637 | ||
e9659ab0 | 3638 | static tree |
a2b60a0e MM |
3639 | maybe_get_template_decl_from_type_decl (decl) |
3640 | tree decl; | |
3641 | { | |
3642 | return (decl != NULL_TREE | |
3643 | && TREE_CODE (decl) == TYPE_DECL | |
3644 | && DECL_ARTIFICIAL (decl) | |
511b60ff | 3645 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
a2b60a0e MM |
3646 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
3647 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
3648 | } | |
386b8a85 | 3649 | |
8d08fdba MS |
3650 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
3651 | parameters, find the desired type. | |
3652 | ||
3653 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
36a117a5 MM |
3654 | (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will |
3655 | be a TREE_LIST if called directly from the parser, and a TREE_VEC | |
3656 | otherwise.) Since ARGLIST is build on the decl_obstack, we must | |
3657 | copy it here to keep it from being reclaimed when the decl storage | |
3658 | is reclaimed. | |
8d08fdba MS |
3659 | |
3660 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
3661 | instantiate. |
3662 | ||
36a117a5 MM |
3663 | If ENTERING_SCOPE is non-zero, we are about to enter the scope of |
3664 | the class we are looking up. | |
3665 | ||
75650646 MM |
3666 | If the template class is really a local class in a template |
3667 | function, then the FUNCTION_CONTEXT is the function in which it is | |
3668 | being instantiated. */ | |
e92cc029 | 3669 | |
8d08fdba | 3670 | tree |
36a117a5 | 3671 | lookup_template_class (d1, arglist, in_decl, context, entering_scope) |
8d08fdba MS |
3672 | tree d1, arglist; |
3673 | tree in_decl; | |
e1467ff2 | 3674 | tree context; |
36a117a5 | 3675 | int entering_scope; |
8d08fdba | 3676 | { |
a703fb38 | 3677 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 3678 | tree t; |
5566b478 MS |
3679 | |
3680 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
3681 | { | |
f181d4ae MM |
3682 | if (IDENTIFIER_VALUE (d1) |
3683 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) | |
3684 | template = IDENTIFIER_VALUE (d1); | |
73b0fce8 KL |
3685 | else |
3686 | { | |
2c73f9f5 ML |
3687 | if (context) |
3688 | push_decl_namespace (context); | |
3ebc5c52 MM |
3689 | template = lookup_name (d1, /*prefer_type=*/0); |
3690 | template = maybe_get_template_decl_from_type_decl (template); | |
2c73f9f5 ML |
3691 | if (context) |
3692 | pop_decl_namespace (); | |
73b0fce8 | 3693 | } |
8d019cef JM |
3694 | if (template) |
3695 | context = DECL_CONTEXT (template); | |
5566b478 | 3696 | } |
c91a56d2 MS |
3697 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
3698 | { | |
802dbc34 JM |
3699 | tree type = TREE_TYPE (d1); |
3700 | ||
3701 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
3702 | an implicit typename for the second A. Deal with it. */ | |
3703 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
3704 | type = TREE_TYPE (type); | |
3705 | ||
3706 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
f3400fe2 | 3707 | { |
802dbc34 | 3708 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
3709 | d1 = DECL_NAME (template); |
3710 | } | |
c91a56d2 | 3711 | } |
ed44da02 MM |
3712 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
3713 | || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' | |
3714 | && IS_AGGR_TYPE (d1))) | |
5566b478 | 3715 | { |
ed44da02 | 3716 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
3717 | d1 = DECL_NAME (template); |
3718 | } | |
93cdc044 JM |
3719 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
3720 | && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL) | |
3721 | { | |
3722 | template = d1; | |
3723 | d1 = DECL_NAME (template); | |
3724 | context = DECL_CONTEXT (template); | |
3725 | } | |
5566b478 MS |
3726 | else |
3727 | my_friendly_abort (272); | |
8d08fdba | 3728 | |
8d08fdba | 3729 | /* With something like `template <class T> class X class X { ... };' |
f181d4ae MM |
3730 | we could end up with D1 having nothing but an IDENTIFIER_VALUE. |
3731 | We don't want to do that, but we have to deal with the situation, | |
3732 | so let's give them some syntax errors to chew on instead of a | |
3733 | crash. */ | |
8d08fdba | 3734 | if (! template) |
f3400fe2 JM |
3735 | { |
3736 | cp_error ("`%T' is not a template", d1); | |
3737 | return error_mark_node; | |
3738 | } | |
2c73f9f5 | 3739 | |
c3baf4b5 JM |
3740 | if (context == NULL_TREE) |
3741 | context = global_namespace; | |
2c73f9f5 | 3742 | |
8d08fdba MS |
3743 | if (TREE_CODE (template) != TEMPLATE_DECL) |
3744 | { | |
8251199e | 3745 | cp_error ("non-template type `%T' used as a template", d1); |
8d08fdba | 3746 | if (in_decl) |
8251199e | 3747 | cp_error_at ("for template declaration `%D'", in_decl); |
8d08fdba MS |
3748 | return error_mark_node; |
3749 | } | |
8d08fdba | 3750 | |
73b0fce8 KL |
3751 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
3752 | { | |
3753 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
3754 | template arguments */ | |
3755 | ||
3756 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
3757 | tree template2 = TYPE_STUB_DECL (parm); | |
3758 | tree arglist2; | |
3759 | ||
73b0fce8 KL |
3760 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
3761 | ||
744fac59 | 3762 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1); |
73b0fce8 KL |
3763 | if (arglist2 == error_mark_node) |
3764 | return error_mark_node; | |
3765 | ||
3766 | arglist2 = copy_to_permanent (arglist2); | |
7ddedda4 | 3767 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm) |
73b0fce8 KL |
3768 | = perm_tree_cons (template2, arglist2, NULL_TREE); |
3769 | TYPE_SIZE (parm) = 0; | |
3770 | return parm; | |
3771 | } | |
36a117a5 | 3772 | else |
8d08fdba | 3773 | { |
36a117a5 | 3774 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 3775 | tree gen_tmpl; |
36a117a5 MM |
3776 | tree type_decl; |
3777 | tree found = NULL_TREE; | |
3778 | int arg_depth; | |
3779 | int parm_depth; | |
dbfe2124 | 3780 | int is_partial_instantiation; |
830bfa74 | 3781 | |
7ac7b28f MM |
3782 | gen_tmpl = most_general_template (template); |
3783 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
3784 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
3785 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3786 | ||
7ddedda4 MM |
3787 | /* We build up the coerced arguments and such on the |
3788 | momentary_obstack. */ | |
3789 | push_momentary (); | |
3790 | ||
36a117a5 MM |
3791 | if (arg_depth == 1 && parm_depth > 1) |
3792 | { | |
39c01e4c | 3793 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
3794 | For example, given: |
3795 | ||
3796 | template <class T> struct S1 { | |
3797 | template <class U> struct S2 {}; | |
3798 | template <class U> struct S2<U*> {}; | |
3799 | }; | |
3800 | ||
3801 | we will be called with an ARGLIST of `U*', but the | |
3802 | TEMPLATE will be `template <class T> template | |
3803 | <class U> struct S1<T>::S2'. We must fill in the missing | |
3804 | arguments. */ | |
7ac7b28f MM |
3805 | arglist |
3806 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), | |
3807 | arglist); | |
36a117a5 MM |
3808 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
3809 | } | |
5566b478 | 3810 | |
7ac7b28f | 3811 | /* Now we should enough arguments. */ |
36a117a5 MM |
3812 | my_friendly_assert (parm_depth == arg_depth, 0); |
3813 | ||
7ac7b28f MM |
3814 | /* From here on, we're only interested in the most general |
3815 | template. */ | |
3816 | template = gen_tmpl; | |
3817 | ||
36a117a5 MM |
3818 | /* Calculate the BOUND_ARGS. These will be the args that are |
3819 | actually tsubst'd into the definition to create the | |
3820 | instantiation. */ | |
3821 | if (parm_depth > 1) | |
830bfa74 MM |
3822 | { |
3823 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 3824 | int i; |
e4a84209 | 3825 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 3826 | |
cabc336a | 3827 | tree bound_args = make_temp_vec (parm_depth); |
830bfa74 | 3828 | |
e4a84209 | 3829 | for (i = saved_depth, |
830bfa74 | 3830 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 3831 | i > 0 && t != NULL_TREE; |
830bfa74 | 3832 | --i, t = TREE_CHAIN (t)) |
e4a84209 MM |
3833 | { |
3834 | tree a = coerce_template_parms (TREE_VALUE (t), | |
3835 | arglist, template, 1, 1); | |
3836 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); | |
3837 | ||
3838 | /* We temporarily reduce the length of the ARGLIST so | |
3839 | that coerce_template_parms will see only the arguments | |
3840 | corresponding to the template parameters it is | |
3841 | examining. */ | |
3842 | TREE_VEC_LENGTH (arglist)--; | |
3843 | } | |
3844 | ||
3845 | /* Restore the ARGLIST to its full size. */ | |
3846 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
3847 | ||
36a117a5 | 3848 | arglist = bound_args; |
830bfa74 MM |
3849 | } |
3850 | else | |
36a117a5 MM |
3851 | arglist |
3852 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
3853 | innermost_args (arglist), | |
3854 | template, 1, 1); | |
3855 | ||
3856 | if (arglist == error_mark_node) | |
3857 | /* We were unable to bind the arguments. */ | |
5566b478 | 3858 | return error_mark_node; |
5566b478 | 3859 | |
36a117a5 MM |
3860 | /* In the scope of a template class, explicit references to the |
3861 | template class refer to the type of the template, not any | |
3862 | instantiation of it. For example, in: | |
3863 | ||
3864 | template <class T> class C { void f(C<T>); } | |
3865 | ||
3866 | the `C<T>' is just the same as `C'. Outside of the | |
3867 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 3868 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
3869 | arglist)) |
3870 | { | |
3871 | found = template_type; | |
3872 | ||
3873 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 3874 | { |
36a117a5 MM |
3875 | tree ctx; |
3876 | ||
3877 | /* Note that we use DECL_CONTEXT, rather than | |
3878 | CP_DECL_CONTEXT, so that the termination test is | |
3879 | always just `ctx'. We're not interested in namepace | |
3880 | scopes. */ | |
3881 | for (ctx = current_class_type; | |
3882 | ctx; | |
3883 | ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't') | |
3884 | ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) | |
3bfdc719 | 3885 | if (same_type_p (ctx, template_type)) |
36a117a5 MM |
3886 | break; |
3887 | ||
3888 | if (!ctx) | |
3889 | /* We're not in the scope of the class, so the | |
3890 | TEMPLATE_TYPE is not the type we want after | |
3891 | all. */ | |
3892 | found = NULL_TREE; | |
5566b478 MS |
3893 | } |
3894 | } | |
36a117a5 MM |
3895 | |
3896 | if (!found) | |
75650646 | 3897 | { |
36a117a5 MM |
3898 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); |
3899 | found; found = TREE_CHAIN (found)) | |
3900 | if (comp_template_args (TREE_PURPOSE (found), arglist)) | |
3901 | break; | |
75650646 | 3902 | |
36a117a5 MM |
3903 | if (found) |
3904 | found = TREE_VALUE (found); | |
8d08fdba | 3905 | } |
3ebc5c52 | 3906 | |
36a117a5 | 3907 | if (found) |
8d08fdba | 3908 | { |
7ddedda4 | 3909 | pop_momentary (); |
36a117a5 | 3910 | return found; |
8d08fdba | 3911 | } |
5566b478 | 3912 | |
dbfe2124 | 3913 | /* This type is a "partial instantiation" if any of the template |
486e4077 MM |
3914 | arguments still inolve template parameters. Note that we set |
3915 | IS_PARTIAL_INSTANTIATION for partial specializations as | |
3916 | well. */ | |
dbfe2124 MM |
3917 | is_partial_instantiation = uses_template_parms (arglist); |
3918 | ||
3ebc5c52 MM |
3919 | if (!is_partial_instantiation |
3920 | && !PRIMARY_TEMPLATE_P (template) | |
3921 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
3922 | { | |
3923 | pop_momentary (); | |
3924 | found = xref_tag_from_type (TREE_TYPE (template), | |
3925 | DECL_NAME (template), | |
3926 | /*globalize=*/1); | |
3927 | return found; | |
3928 | } | |
3929 | ||
3930 | /* Since we didn't find the type, we'll have to create it. | |
3931 | Since we'll be saving this type on the | |
3932 | DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */ | |
3933 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
3934 | ||
36a117a5 | 3935 | /* Create the type. */ |
ed44da02 MM |
3936 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
3937 | { | |
dbfe2124 | 3938 | if (!is_partial_instantiation) |
61a127b3 | 3939 | t = start_enum (TYPE_IDENTIFIER (template_type)); |
ed44da02 | 3940 | else |
dbfe2124 | 3941 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
3942 | the values for the enumeration constants may involve |
3943 | template parameters. And, no one should be interested | |
3944 | in the enumeration constants for such a type. */ | |
3945 | t = make_node (ENUMERAL_TYPE); | |
3946 | } | |
3947 | else | |
3948 | { | |
3949 | t = make_lang_type (TREE_CODE (template_type)); | |
3950 | CLASSTYPE_DECLARED_CLASS (t) | |
3951 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
3952 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
3953 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
f668f160 | 3954 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ed44da02 MM |
3955 | } |
3956 | ||
61a127b3 | 3957 | /* If we called start_enum above, this information will already |
ed44da02 MM |
3958 | be set up. */ |
3959 | if (!TYPE_NAME (t)) | |
3960 | { | |
3961 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 3962 | |
9188c363 | 3963 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 3964 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 3965 | TYPE_STUB_DECL (t) = type_decl; |
ed44da02 MM |
3966 | DECL_SOURCE_FILE (type_decl) |
3967 | = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type)); | |
3968 | DECL_SOURCE_LINE (type_decl) | |
3969 | = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type)); | |
ed44da02 MM |
3970 | } |
3971 | else | |
3972 | type_decl = TYPE_NAME (t); | |
36a117a5 | 3973 | |
9fbf56f7 MM |
3974 | /* Set up the template information. We have to figure out which |
3975 | template is the immediate parent if this is a full | |
3976 | instantiation. */ | |
3977 | if (parm_depth == 1 || is_partial_instantiation | |
3978 | || !PRIMARY_TEMPLATE_P (template)) | |
3979 | /* This case is easy; there are no member templates involved. */ | |
3980 | found = template; | |
3981 | else | |
3982 | { | |
3983 | /* This is a full instantiation of a member template. There | |
3984 | should be some partial instantiation of which this is an | |
3985 | instance. */ | |
3986 | ||
3987 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
3988 | found; found = TREE_CHAIN (found)) | |
3989 | { | |
3990 | int success; | |
3991 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
3992 | ||
3993 | /* We only want partial instantiations, here, not | |
3994 | specializations or full instantiations. */ | |
3995 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) | |
3996 | || !uses_template_parms (TREE_VALUE (found))) | |
3997 | continue; | |
3998 | ||
3999 | /* Temporarily reduce by one the number of levels in the | |
4000 | ARGLIST and in FOUND so as to avoid comparing the | |
4001 | last set of arguments. */ | |
4002 | TREE_VEC_LENGTH (arglist)--; | |
4003 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
4004 | ||
4005 | /* See if the arguments match. If they do, then TMPL is | |
4006 | the partial instantiation we want. */ | |
4007 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
4008 | ||
4009 | /* Restore the argument vectors to their full size. */ | |
4010 | TREE_VEC_LENGTH (arglist)++; | |
4011 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
4012 | ||
4013 | if (success) | |
4014 | { | |
4015 | found = tmpl; | |
4016 | break; | |
4017 | } | |
4018 | } | |
4019 | ||
4020 | if (!found) | |
4021 | my_friendly_abort (0); | |
4022 | } | |
4023 | ||
5566b478 | 4024 | arglist = copy_to_permanent (arglist); |
ed44da02 | 4025 | SET_TYPE_TEMPLATE_INFO (t, |
9fbf56f7 | 4026 | tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
4027 | DECL_TEMPLATE_INSTANTIATIONS (template) |
4028 | = tree_cons (arglist, t, | |
4029 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
4030 | ||
4031 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
4032 | && !is_partial_instantiation) | |
61a127b3 MM |
4033 | /* Now that the type has been registered on the instantiations |
4034 | list, we set up the enumerators. Because the enumeration | |
4035 | constants may involve the enumeration type itself, we make | |
4036 | sure to register the type first, and then create the | |
4037 | constants. That way, doing tsubst_expr for the enumeration | |
4038 | constants won't result in recursive calls here; we'll find | |
4039 | the instantiation and exit above. */ | |
4040 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 MM |
4041 | |
4042 | /* We're done with the permanent obstack, now. */ | |
4043 | pop_obstacks (); | |
7ddedda4 MM |
4044 | /* We're also done with the momentary allocation we started |
4045 | above. */ | |
4046 | pop_momentary (); | |
5566b478 | 4047 | |
36a117a5 MM |
4048 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
4049 | is set up. */ | |
ed44da02 MM |
4050 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
4051 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
36a117a5 | 4052 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
dbfe2124 | 4053 | if (!is_partial_instantiation) |
36a117a5 MM |
4054 | { |
4055 | DECL_ASSEMBLER_NAME (type_decl) | |
4056 | = get_identifier (build_overload_name (t, 1, 1)); | |
50a6dbd7 JM |
4057 | |
4058 | /* For backwards compatibility; code that uses | |
4059 | -fexternal-templates expects looking up a template to | |
4060 | instantiate it. I think DDD still relies on this. | |
4061 | (jason 8/20/1998) */ | |
ed44da02 MM |
4062 | if (TREE_CODE (t) != ENUMERAL_TYPE |
4063 | && flag_external_templates | |
36a117a5 MM |
4064 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) |
4065 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
4066 | add_pending_template (t); | |
4067 | } | |
4068 | else | |
077e7015 MM |
4069 | /* If the type makes use of template parameters, the |
4070 | code that generates debugging information will crash. */ | |
4071 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 4072 | |
36a117a5 MM |
4073 | return t; |
4074 | } | |
8d08fdba MS |
4075 | } |
4076 | \f | |
050367a3 MM |
4077 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or |
4078 | TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA. | |
4079 | If FN returns non-zero, the iteration is terminated, and | |
4080 | for_each_template_parm returns 1. Otherwise, the iteration | |
4081 | continues. If FN never returns a non-zero value, the value | |
4082 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
4083 | considered to be the function which always returns 1. */ | |
4084 | ||
e9659ab0 | 4085 | static int |
050367a3 | 4086 | for_each_template_parm (t, fn, data) |
8d08fdba | 4087 | tree t; |
050367a3 MM |
4088 | tree_fn_t fn; |
4089 | void* data; | |
8d08fdba MS |
4090 | { |
4091 | if (!t) | |
4092 | return 0; | |
581d38d0 MM |
4093 | |
4094 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't' | |
4095 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data)) | |
4096 | return 1; | |
4097 | ||
8d08fdba MS |
4098 | switch (TREE_CODE (t)) |
4099 | { | |
297e73d8 | 4100 | case ARRAY_REF: |
05e0b2f4 | 4101 | case OFFSET_REF: |
297e73d8 MM |
4102 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) |
4103 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4104 | ||
8d08fdba MS |
4105 | case IDENTIFIER_NODE: |
4106 | if (!IDENTIFIER_TEMPLATE (t)) | |
4107 | return 0; | |
5566b478 | 4108 | my_friendly_abort (42); |
8d08fdba MS |
4109 | |
4110 | /* aggregates of tree nodes */ | |
4111 | case TREE_VEC: | |
4112 | { | |
4113 | int i = TREE_VEC_LENGTH (t); | |
4114 | while (i--) | |
050367a3 | 4115 | if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data)) |
8d08fdba MS |
4116 | return 1; |
4117 | return 0; | |
4118 | } | |
4119 | case TREE_LIST: | |
050367a3 MM |
4120 | if (for_each_template_parm (TREE_PURPOSE (t), fn, data) |
4121 | || for_each_template_parm (TREE_VALUE (t), fn, data)) | |
8d08fdba | 4122 | return 1; |
050367a3 | 4123 | return for_each_template_parm (TREE_CHAIN (t), fn, data); |
8d08fdba | 4124 | |
2c73f9f5 ML |
4125 | case OVERLOAD: |
4126 | if (for_each_template_parm (OVL_FUNCTION (t), fn, data)) | |
4127 | return 1; | |
4128 | return for_each_template_parm (OVL_CHAIN (t), fn, data); | |
4129 | ||
8d08fdba MS |
4130 | /* constructed type nodes */ |
4131 | case POINTER_TYPE: | |
4132 | case REFERENCE_TYPE: | |
050367a3 | 4133 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
ed44da02 | 4134 | |
8d08fdba | 4135 | case RECORD_TYPE: |
b7484fbe | 4136 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
050367a3 MM |
4137 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t), |
4138 | fn, data); | |
ed44da02 MM |
4139 | /* Fall through. */ |
4140 | ||
8d08fdba | 4141 | case UNION_TYPE: |
ed44da02 MM |
4142 | case ENUMERAL_TYPE: |
4143 | if (! TYPE_TEMPLATE_INFO (t)) | |
8d08fdba | 4144 | return 0; |
050367a3 | 4145 | return for_each_template_parm (TREE_VALUE |
ed44da02 | 4146 | (TYPE_TEMPLATE_INFO (t)), |
050367a3 | 4147 | fn, data); |
588c2d1c MM |
4148 | case METHOD_TYPE: |
4149 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data)) | |
8d08fdba | 4150 | return 1; |
588c2d1c MM |
4151 | /* Fall through. */ |
4152 | ||
4153 | case FUNCTION_TYPE: | |
4154 | /* Check the parameter types. Since default arguments are not | |
4155 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4156 | contain expressions that involve template parameters. But, | |
4157 | no-one should be looking at them yet. And, once they're | |
4158 | instantiated, they don't contain template parameters, so | |
4159 | there's no point in looking at them then, either. */ | |
4160 | { | |
4161 | tree parm; | |
4162 | ||
4163 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4164 | if (for_each_template_parm (TREE_VALUE (parm), fn, data)) | |
4165 | return 1; | |
4166 | } | |
4167 | ||
4168 | /* Check the return type, too. */ | |
050367a3 | 4169 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
588c2d1c | 4170 | |
8d08fdba | 4171 | case ARRAY_TYPE: |
050367a3 | 4172 | if (for_each_template_parm (TYPE_DOMAIN (t), fn, data)) |
8d08fdba | 4173 | return 1; |
050367a3 | 4174 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 4175 | case OFFSET_TYPE: |
050367a3 | 4176 | if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data)) |
8d08fdba | 4177 | return 1; |
050367a3 | 4178 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
4179 | |
4180 | /* decl nodes */ | |
4181 | case TYPE_DECL: | |
050367a3 | 4182 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
5566b478 | 4183 | |
73b0fce8 KL |
4184 | case TEMPLATE_DECL: |
4185 | /* A template template parameter is encountered */ | |
4186 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
744fac59 KL |
4187 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
4188 | /* Already substituted template template parameter */ | |
73b0fce8 KL |
4189 | return 0; |
4190 | ||
3ac3d9ea | 4191 | case CONST_DECL: |
050367a3 | 4192 | if (for_each_template_parm (DECL_INITIAL (t), fn, data)) |
3ac3d9ea MM |
4193 | return 1; |
4194 | goto check_type_and_context; | |
4195 | ||
8d08fdba | 4196 | case FUNCTION_DECL: |
5566b478 | 4197 | case VAR_DECL: |
5566b478 | 4198 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
050367a3 | 4199 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data)) |
8d08fdba MS |
4200 | return 1; |
4201 | /* fall through */ | |
8d08fdba | 4202 | case PARM_DECL: |
3ac3d9ea | 4203 | check_type_and_context: |
050367a3 | 4204 | if (for_each_template_parm (TREE_TYPE (t), fn, data)) |
5566b478 | 4205 | return 1; |
050367a3 MM |
4206 | if (DECL_CONTEXT (t) |
4207 | && for_each_template_parm (DECL_CONTEXT (t), fn, data)) | |
8d08fdba | 4208 | return 1; |
8d08fdba MS |
4209 | return 0; |
4210 | ||
4211 | case CALL_EXPR: | |
297e73d8 MM |
4212 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) |
4213 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4214 | ||
8d08fdba | 4215 | case ADDR_EXPR: |
050367a3 | 4216 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
8d08fdba MS |
4217 | |
4218 | /* template parm nodes */ | |
73b0fce8 | 4219 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4220 | /* Record template parameters such as `T' inside `TT<T>'. */ |
7ddedda4 MM |
4221 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t) |
4222 | && for_each_template_parm (TYPE_TI_ARGS (t), fn, data)) | |
744fac59 KL |
4223 | return 1; |
4224 | case TEMPLATE_TYPE_PARM: | |
f84b4be9 | 4225 | case TEMPLATE_PARM_INDEX: |
050367a3 MM |
4226 | if (fn) |
4227 | return (*fn)(t, data); | |
4228 | else | |
4229 | return 1; | |
8d08fdba MS |
4230 | |
4231 | /* simple type nodes */ | |
4232 | case INTEGER_TYPE: | |
050367a3 | 4233 | if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data)) |
8d08fdba | 4234 | return 1; |
050367a3 | 4235 | return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data); |
8d08fdba MS |
4236 | |
4237 | case REAL_TYPE: | |
37c46b43 | 4238 | case COMPLEX_TYPE: |
8d08fdba | 4239 | case VOID_TYPE: |
2986ae00 | 4240 | case BOOLEAN_TYPE: |
2c73f9f5 | 4241 | case NAMESPACE_DECL: |
4699c561 | 4242 | case FIELD_DECL: |
8d08fdba MS |
4243 | return 0; |
4244 | ||
4245 | /* constants */ | |
4246 | case INTEGER_CST: | |
4247 | case REAL_CST: | |
4248 | case STRING_CST: | |
4249 | return 0; | |
4250 | ||
4251 | case ERROR_MARK: | |
4252 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
4253 | my_friendly_assert (t == error_mark_node, 274); | |
4254 | /* NOTREACHED */ | |
4255 | return 0; | |
4256 | ||
61a127b3 MM |
4257 | case PTRMEM_CST: |
4258 | return for_each_template_parm (TREE_TYPE (t), fn, data); | |
4259 | ||
5156628f | 4260 | case SCOPE_REF: |
050367a3 | 4261 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
5156628f | 4262 | |
db5ae43f MS |
4263 | case CONSTRUCTOR: |
4264 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
050367a3 MM |
4265 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4266 | (TREE_TYPE (t)), fn, data); | |
4267 | return for_each_template_parm (TREE_OPERAND (t, 1), fn, data); | |
db5ae43f | 4268 | |
b8865407 MM |
4269 | case SIZEOF_EXPR: |
4270 | case ALIGNOF_EXPR: | |
4271 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); | |
4272 | ||
4699c561 MM |
4273 | case TYPENAME_TYPE: |
4274 | if (!fn) | |
4275 | return 1; | |
4276 | return (for_each_template_parm (TYPE_CONTEXT (t), fn, data) | |
4277 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), | |
4278 | fn, data)); | |
4279 | ||
b8865407 MM |
4280 | case INDIRECT_REF: |
4281 | case COMPONENT_REF: | |
4699c561 | 4282 | /* If there's no type, then this thing must be some expression |
b8865407 | 4283 | involving template parameters. */ |
4699c561 MM |
4284 | if (!fn && !TREE_TYPE (t)) |
4285 | return 1; | |
4286 | if (TREE_CODE (t) == COMPONENT_REF) | |
4287 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) | |
4288 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4289 | else | |
4290 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); | |
b8865407 | 4291 | |
42976354 BK |
4292 | case MODOP_EXPR: |
4293 | case CAST_EXPR: | |
4294 | case REINTERPRET_CAST_EXPR: | |
4295 | case CONST_CAST_EXPR: | |
4296 | case STATIC_CAST_EXPR: | |
4297 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
4298 | case ARROW_EXPR: |
4299 | case DOTSTAR_EXPR: | |
4300 | case TYPEID_EXPR: | |
b8865407 | 4301 | case LOOKUP_EXPR: |
40242ccf | 4302 | case PSEUDO_DTOR_EXPR: |
b8865407 MM |
4303 | if (!fn) |
4304 | return 1; | |
4305 | /* Fall through. */ | |
abff8e06 | 4306 | |
8d08fdba MS |
4307 | default: |
4308 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
4309 | { | |
4310 | case '1': | |
4311 | case '2': | |
ec255269 | 4312 | case 'e': |
8d08fdba MS |
4313 | case '<': |
4314 | { | |
4315 | int i; | |
bbc06b4b | 4316 | for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;) |
050367a3 | 4317 | if (for_each_template_parm (TREE_OPERAND (t, i), fn, data)) |
8d08fdba MS |
4318 | return 1; |
4319 | return 0; | |
4320 | } | |
4321 | default: | |
4322 | break; | |
4323 | } | |
4324 | sorry ("testing %s for template parms", | |
4325 | tree_code_name [(int) TREE_CODE (t)]); | |
4326 | my_friendly_abort (82); | |
4327 | /* NOTREACHED */ | |
4328 | return 0; | |
4329 | } | |
4330 | } | |
4331 | ||
050367a3 MM |
4332 | int |
4333 | uses_template_parms (t) | |
4334 | tree t; | |
4335 | { | |
4336 | return for_each_template_parm (t, 0, 0); | |
4337 | } | |
4338 | ||
27fafc8d JM |
4339 | static struct tinst_level *current_tinst_level; |
4340 | static struct tinst_level *free_tinst_level; | |
4341 | static int tinst_depth; | |
e9f32eb5 | 4342 | extern int max_tinst_depth; |
5566b478 | 4343 | #ifdef GATHER_STATISTICS |
27fafc8d | 4344 | int depth_reached; |
5566b478 | 4345 | #endif |
27fafc8d JM |
4346 | int tinst_level_tick; |
4347 | int last_template_error_tick; | |
8d08fdba | 4348 | |
7bae46f4 | 4349 | /* Print out all the template instantiations that we are currently |
27fafc8d JM |
4350 | working on. If ERR, we are being called from cp_thing, so do |
4351 | the right thing for an error message. */ | |
7bae46f4 | 4352 | |
27fafc8d JM |
4353 | static void |
4354 | print_template_context (err) | |
4355 | int err; | |
7bae46f4 JM |
4356 | { |
4357 | struct tinst_level *p = current_tinst_level; | |
4358 | int line = lineno; | |
4359 | char *file = input_filename; | |
4360 | ||
49e04385 | 4361 | if (err && p) |
27fafc8d | 4362 | { |
49e04385 MM |
4363 | if (current_function_decl != p->decl |
4364 | && current_function_decl != NULL_TREE) | |
4365 | /* We can get here during the processing of some synthesized | |
4366 | method. Then, p->decl will be the function that's causing | |
4367 | the synthesis. */ | |
4368 | ; | |
27fafc8d | 4369 | else |
27fafc8d | 4370 | { |
49e04385 MM |
4371 | if (current_function_decl == p->decl) |
4372 | /* Avoid redundancy with the the "In function" line. */; | |
4373 | else | |
4374 | fprintf (stderr, "%s: In instantiation of `%s':\n", | |
4375 | file, decl_as_string (p->decl, 0)); | |
4376 | ||
038fb86f JM |
4377 | line = p->line; |
4378 | file = p->file; | |
27fafc8d JM |
4379 | p = p->next; |
4380 | } | |
4381 | } | |
4382 | ||
7bae46f4 JM |
4383 | for (; p; p = p->next) |
4384 | { | |
038fb86f JM |
4385 | fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line, |
4386 | decl_as_string (p->decl, 0)); | |
4387 | line = p->line; | |
4388 | file = p->file; | |
7bae46f4 | 4389 | } |
038fb86f | 4390 | fprintf (stderr, "%s:%d: instantiated from here\n", file, line); |
7bae46f4 JM |
4391 | } |
4392 | ||
27fafc8d JM |
4393 | /* Called from cp_thing to print the template context for an error. */ |
4394 | ||
4395 | void | |
4396 | maybe_print_template_context () | |
4397 | { | |
4398 | if (last_template_error_tick == tinst_level_tick | |
4399 | || current_tinst_level == 0) | |
4400 | return; | |
4401 | ||
4402 | last_template_error_tick = tinst_level_tick; | |
4403 | print_template_context (1); | |
4404 | } | |
4405 | ||
bd6dd845 | 4406 | static int |
5566b478 MS |
4407 | push_tinst_level (d) |
4408 | tree d; | |
8d08fdba MS |
4409 | { |
4410 | struct tinst_level *new; | |
8d08fdba | 4411 | |
7215f9a0 MS |
4412 | if (tinst_depth >= max_tinst_depth) |
4413 | { | |
8adf5b5e JM |
4414 | /* If the instantiation in question still has unbound template parms, |
4415 | we don't really care if we can't instantiate it, so just return. | |
4416 | This happens with base instantiation for implicit `typename'. */ | |
4417 | if (uses_template_parms (d)) | |
4418 | return 0; | |
4419 | ||
1139b3d8 | 4420 | last_template_error_tick = tinst_level_tick; |
8251199e | 4421 | error ("template instantiation depth exceeds maximum of %d", |
7215f9a0 | 4422 | max_tinst_depth); |
8251199e JM |
4423 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
4424 | cp_error (" instantiating `%D'", d); | |
5566b478 | 4425 | |
27fafc8d | 4426 | print_template_context (0); |
5566b478 | 4427 | |
7215f9a0 MS |
4428 | return 0; |
4429 | } | |
4430 | ||
8d08fdba MS |
4431 | if (free_tinst_level) |
4432 | { | |
4433 | new = free_tinst_level; | |
4434 | free_tinst_level = new->next; | |
4435 | } | |
4436 | else | |
4437 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
4438 | ||
5566b478 MS |
4439 | new->decl = d; |
4440 | new->line = lineno; | |
4441 | new->file = input_filename; | |
8d08fdba MS |
4442 | new->next = current_tinst_level; |
4443 | current_tinst_level = new; | |
5566b478 | 4444 | |
7215f9a0 | 4445 | ++tinst_depth; |
5566b478 MS |
4446 | #ifdef GATHER_STATISTICS |
4447 | if (tinst_depth > depth_reached) | |
4448 | depth_reached = tinst_depth; | |
4449 | #endif | |
4450 | ||
27fafc8d | 4451 | ++tinst_level_tick; |
7215f9a0 | 4452 | return 1; |
8d08fdba MS |
4453 | } |
4454 | ||
4455 | void | |
4456 | pop_tinst_level () | |
4457 | { | |
4458 | struct tinst_level *old = current_tinst_level; | |
4459 | ||
ae58fa02 MM |
4460 | /* Restore the filename and line number stashed away when we started |
4461 | this instantiation. */ | |
4462 | lineno = old->line; | |
4463 | input_filename = old->file; | |
5f2c99c4 | 4464 | extract_interface_info (); |
ae58fa02 | 4465 | |
8d08fdba MS |
4466 | current_tinst_level = old->next; |
4467 | old->next = free_tinst_level; | |
4468 | free_tinst_level = old; | |
7215f9a0 | 4469 | --tinst_depth; |
27fafc8d | 4470 | ++tinst_level_tick; |
8d08fdba MS |
4471 | } |
4472 | ||
4473 | struct tinst_level * | |
4474 | tinst_for_decl () | |
4475 | { | |
4476 | struct tinst_level *p = current_tinst_level; | |
4477 | ||
4478 | if (p) | |
4479 | for (; p->next ; p = p->next ) | |
4480 | ; | |
4481 | return p; | |
4482 | } | |
4483 | ||
f84b4be9 JM |
4484 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
4485 | vector of template arguments, as for tsubst. | |
4486 | ||
4487 | Returns an appropriate tsbust'd friend declaration. */ | |
4488 | ||
4489 | static tree | |
4490 | tsubst_friend_function (decl, args) | |
4491 | tree decl; | |
4492 | tree args; | |
4493 | { | |
4494 | tree new_friend; | |
27fafc8d JM |
4495 | int line = lineno; |
4496 | char *file = input_filename; | |
4497 | ||
4498 | lineno = DECL_SOURCE_LINE (decl); | |
4499 | input_filename = DECL_SOURCE_FILE (decl); | |
4500 | ||
f84b4be9 JM |
4501 | if (TREE_CODE (decl) == FUNCTION_DECL |
4502 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
4503 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
4504 | /* This was a friend declared with an explicit template | |
4505 | argument list, e.g.: | |
4506 | ||
4507 | friend void f<>(T); | |
4508 | ||
4509 | to indicate that f was a template instantiation, not a new | |
4510 | function declaration. Now, we have to figure out what | |
4511 | instantiation of what template. */ | |
4512 | { | |
4513 | tree template_id; | |
4514 | tree new_args; | |
4515 | tree tmpl; | |
f84b4be9 JM |
4516 | |
4517 | template_id | |
4518 | = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl), | |
4393e105 MM |
4519 | args, /*complain=*/1, |
4520 | NULL_TREE), | |
f84b4be9 | 4521 | tsubst (DECL_TI_ARGS (decl), |
4393e105 MM |
4522 | args, /*complain=*/1, |
4523 | NULL_TREE)); | |
39c01e4c MM |
4524 | /* FIXME: The decl we create via the next tsubst could be |
4525 | created on a temporary obstack. */ | |
4393e105 | 4526 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
4527 | tmpl = determine_specialization (template_id, new_friend, |
4528 | &new_args, | |
bf8f3f93 | 4529 | /*need_member_template=*/0); |
27fafc8d JM |
4530 | new_friend = instantiate_template (tmpl, new_args); |
4531 | goto done; | |
f84b4be9 | 4532 | } |
36a117a5 | 4533 | |
4393e105 | 4534 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
f84b4be9 | 4535 | |
36a117a5 | 4536 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
4537 | compiler, but is not an instantiation from the point of view of |
4538 | the language. For example, we might have had: | |
4539 | ||
4540 | template <class T> struct S { | |
4541 | template <class U> friend void f(T, U); | |
4542 | }; | |
4543 | ||
4544 | Then, in S<int>, template <class U> void f(int, U) is not an | |
4545 | instantiation of anything. */ | |
4546 | DECL_USE_TEMPLATE (new_friend) = 0; | |
4547 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
4548 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
36a117a5 MM |
4549 | |
4550 | /* The mangled name for the NEW_FRIEND is incorrect. The call to | |
4551 | tsubst will have resulted in a call to | |
4552 | set_mangled_name_for_template_decl. But, the function is not a | |
4553 | template instantiation and should not be mangled like one. | |
4554 | Therefore, we remangle the function name. We don't have to do | |
4555 | this if the NEW_FRIEND is a template since | |
4556 | set_mangled_name_for_template_decl doesn't do anything if the | |
4557 | function declaration still uses template arguments. */ | |
4558 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) | |
4559 | { | |
4560 | set_mangled_name_for_decl (new_friend); | |
4561 | DECL_RTL (new_friend) = 0; | |
4562 | make_decl_rtl (new_friend, NULL_PTR, 1); | |
4563 | } | |
4564 | ||
6eb3bb27 | 4565 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 4566 | { |
36a117a5 | 4567 | tree old_decl; |
fbf1c34b MM |
4568 | tree new_friend_template_info; |
4569 | tree new_friend_result_template_info; | |
92da7074 | 4570 | tree ns; |
fbf1c34b MM |
4571 | int new_friend_is_defn; |
4572 | ||
4573 | /* We must save some information from NEW_FRIEND before calling | |
4574 | duplicate decls since that function will free NEW_FRIEND if | |
4575 | possible. */ | |
4576 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
f84b4be9 | 4577 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
4578 | { |
4579 | /* This declaration is a `primary' template. */ | |
4580 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
4581 | ||
4582 | new_friend_is_defn | |
4583 | = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE; | |
4584 | new_friend_result_template_info | |
4585 | = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend)); | |
4586 | } | |
4587 | else | |
4588 | { | |
4589 | new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; | |
4590 | new_friend_result_template_info = NULL_TREE; | |
4591 | } | |
36a117a5 | 4592 | |
92da7074 ML |
4593 | /* Inside pushdecl_namespace_level, we will push into the |
4594 | current namespace. However, the friend function should | |
4595 | tyically go into the namespace of the template. */ | |
4596 | ns = decl_namespace_context (new_friend); | |
4597 | push_nested_namespace (ns); | |
36a117a5 | 4598 | old_decl = pushdecl_namespace_level (new_friend); |
92da7074 | 4599 | pop_nested_namespace (ns); |
36a117a5 MM |
4600 | |
4601 | if (old_decl != new_friend) | |
4602 | { | |
4603 | /* This new friend declaration matched an existing | |
4604 | declaration. For example, given: | |
4605 | ||
4606 | template <class T> void f(T); | |
4607 | template <class U> class C { | |
4608 | template <class T> friend void f(T) {} | |
4609 | }; | |
4610 | ||
4611 | the friend declaration actually provides the definition | |
4612 | of `f', once C has been instantiated for some type. So, | |
4613 | old_decl will be the out-of-class template declaration, | |
4614 | while new_friend is the in-class definition. | |
4615 | ||
4616 | But, if `f' was called before this point, the | |
4617 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
4618 | to `T' but not to `U', references to which might appear | |
4619 | in the definition of `f'. Previously, the most general | |
4620 | template for an instantiation of `f' was the out-of-class | |
4621 | version; now it is the in-class version. Therefore, we | |
4622 | run through all specialization of `f', adding to their | |
4623 | DECL_TI_ARGS appropriately. In particular, they need a | |
4624 | new set of outer arguments, corresponding to the | |
4625 | arguments for this class instantiation. | |
4626 | ||
4627 | The same situation can arise with something like this: | |
4628 | ||
4629 | friend void f(int); | |
4630 | template <class T> class C { | |
4631 | friend void f(T) {} | |
4632 | }; | |
4633 | ||
4634 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
4635 | in the class. */ | |
4636 | ||
fbf1c34b MM |
4637 | if (!new_friend_is_defn) |
4638 | /* On the other hand, if the in-class declaration does | |
4639 | *not* provide a definition, then we don't want to alter | |
4640 | existing definitions. We can just leave everything | |
4641 | alone. */ | |
36a117a5 | 4642 | ; |
fbf1c34b | 4643 | else |
36a117a5 | 4644 | { |
fbf1c34b MM |
4645 | /* Overwrite whatever template info was there before, if |
4646 | any, with the new template information pertaining to | |
4647 | the declaration. */ | |
4648 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
4649 | ||
4650 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
4651 | /* duplicate_decls will take care of this case. */ | |
4652 | ; | |
4653 | else | |
36a117a5 | 4654 | { |
fbf1c34b MM |
4655 | tree t; |
4656 | tree new_friend_args; | |
4657 | ||
4658 | DECL_TEMPLATE_INFO (DECL_RESULT (old_decl)) | |
4659 | = new_friend_result_template_info; | |
4660 | ||
4661 | new_friend_args = TI_ARGS (new_friend_template_info); | |
4662 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
4663 | t != NULL_TREE; | |
4664 | t = TREE_CHAIN (t)) | |
4665 | { | |
4666 | tree spec = TREE_VALUE (t); | |
36a117a5 | 4667 | |
fbf1c34b MM |
4668 | DECL_TI_ARGS (spec) |
4669 | = add_outermost_template_args (new_friend_args, | |
4670 | DECL_TI_ARGS (spec)); | |
4671 | DECL_TI_ARGS (spec) | |
4672 | = copy_to_permanent (DECL_TI_ARGS (spec)); | |
4673 | } | |
4674 | ||
4675 | /* Now, since specializations are always supposed to | |
4676 | hang off of the most general template, we must move | |
4677 | them. */ | |
4678 | t = most_general_template (old_decl); | |
4679 | if (t != old_decl) | |
4680 | { | |
4681 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
4682 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
4683 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
4684 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
4685 | } | |
36a117a5 MM |
4686 | } |
4687 | } | |
4688 | ||
4689 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
4690 | by duplicate_decls. */ | |
4691 | new_friend = old_decl; | |
4692 | } | |
f84b4be9 JM |
4693 | } |
4694 | else if (TYPE_SIZE (DECL_CONTEXT (new_friend))) | |
4695 | { | |
4696 | /* Check to see that the declaration is really present, and, | |
4697 | possibly obtain an improved declaration. */ | |
4698 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
4699 | new_friend); | |
4700 | ||
4701 | if (fn) | |
4702 | new_friend = fn; | |
4703 | } | |
4704 | ||
27fafc8d JM |
4705 | done: |
4706 | lineno = line; | |
4707 | input_filename = file; | |
f84b4be9 JM |
4708 | return new_friend; |
4709 | } | |
4710 | ||
1aed5355 MM |
4711 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
4712 | template arguments, as for tsubst. | |
6757edfe MM |
4713 | |
4714 | Returns an appropriate tsbust'd friend type. */ | |
4715 | ||
4716 | static tree | |
1aed5355 MM |
4717 | tsubst_friend_class (friend_tmpl, args) |
4718 | tree friend_tmpl; | |
6757edfe MM |
4719 | tree args; |
4720 | { | |
1aed5355 | 4721 | tree friend_type; |
25aab5d0 | 4722 | tree tmpl; |
6757edfe | 4723 | |
25aab5d0 MM |
4724 | /* First, we look for a class template. */ |
4725 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); | |
4726 | ||
4727 | /* But, if we don't find one, it might be because we're in a | |
4728 | situation like this: | |
4729 | ||
4730 | template <class T> | |
4731 | struct S { | |
4732 | template <class U> | |
4733 | friend struct S; | |
4734 | }; | |
4735 | ||
4736 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
4737 | for `S<int>', not the TEMPLATE_DECL. */ | |
bc639f90 | 4738 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) |
25aab5d0 MM |
4739 | { |
4740 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); | |
4741 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
4742 | } | |
6757edfe | 4743 | |
25aab5d0 | 4744 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
4745 | { |
4746 | /* The friend template has already been declared. Just | |
36a117a5 MM |
4747 | check to see that the declarations match, and install any new |
4748 | default parameters. We must tsubst the default parameters, | |
4749 | of course. We only need the innermost template parameters | |
4750 | because that is all that redeclare_class_template will look | |
4751 | at. */ | |
4752 | tree parms | |
4753 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
4393e105 | 4754 | args, /*complain=*/1); |
36a117a5 | 4755 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
6757edfe MM |
4756 | friend_type = TREE_TYPE (tmpl); |
4757 | } | |
4758 | else | |
4759 | { | |
4760 | /* The friend template has not already been declared. In this | |
4761 | case, the instantiation of the template class will cause the | |
4762 | injection of this template into the global scope. */ | |
4393e105 | 4763 | tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE); |
6757edfe MM |
4764 | |
4765 | /* The new TMPL is not an instantiation of anything, so we | |
4766 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
4767 | the new type because that is supposed to be the corresponding | |
4768 | template decl, i.e., TMPL. */ | |
4769 | DECL_USE_TEMPLATE (tmpl) = 0; | |
4770 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
4771 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
4772 | ||
4773 | /* Inject this template into the global scope. */ | |
4774 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
4775 | } | |
4776 | ||
4777 | return friend_type; | |
4778 | } | |
f84b4be9 | 4779 | |
8d08fdba | 4780 | tree |
5566b478 MS |
4781 | instantiate_class_template (type) |
4782 | tree type; | |
8d08fdba | 4783 | { |
61a127b3 | 4784 | tree template, args, pattern, t; |
36a117a5 | 4785 | tree typedecl; |
8d08fdba | 4786 | |
5566b478 | 4787 | if (type == error_mark_node) |
8d08fdba MS |
4788 | return error_mark_node; |
4789 | ||
5566b478 MS |
4790 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) |
4791 | return type; | |
4792 | ||
7ddedda4 MM |
4793 | /* We want to allocate temporary vectors of template arguments and |
4794 | template argument expressions on the momentary obstack, not on | |
4795 | the expression obstack. Otherwise, all the space allocated in | |
4796 | argument coercion and such is simply lost. */ | |
4797 | push_momentary (); | |
4798 | ||
6bdb985f | 4799 | /* Figure out which template is being instantiated. */ |
36a117a5 | 4800 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
5566b478 | 4801 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); |
73aad9b9 | 4802 | |
6bdb985f MM |
4803 | /* Figure out which arguments are being used to do the |
4804 | instantiation. */ | |
4805 | args = CLASSTYPE_TI_ARGS (type); | |
4c571114 | 4806 | PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args); |
6bdb985f | 4807 | |
4c571114 MM |
4808 | if (pedantic && PARTIAL_INSTANTIATION_P (type)) |
4809 | /* If this is a partial instantiation, then we can't instantiate | |
4810 | the type; there's no telling whether or not one of the | |
4811 | template parameters might eventually be instantiated to some | |
4812 | value that results in a specialization being used. For | |
4813 | example, consider: | |
4814 | ||
4815 | template <class T> | |
4816 | struct S {}; | |
4817 | ||
4818 | template <class U> | |
4819 | void f(S<U>); | |
4820 | ||
4821 | template <> | |
4822 | struct S<int> {}; | |
4823 | ||
4824 | Now, the `S<U>' in `f<int>' is the specialization, not an | |
5db698f6 | 4825 | instantiation of the original template. */ |
4c571114 MM |
4826 | goto end; |
4827 | ||
4828 | /* Determine what specialization of the original template to | |
4829 | instantiate. */ | |
4830 | if (PARTIAL_INSTANTIATION_P (type)) | |
6bdb985f MM |
4831 | /* There's no telling which specialization is appropriate at this |
4832 | point. Since all peeking at the innards of this partial | |
4833 | instantiation are extensions (like the "implicit typename" | |
4834 | extension, which allows users to omit the keyword `typename' on | |
4835 | names that are declared as types in template base classes), we | |
4836 | are free to do what we please. | |
4837 | ||
4838 | Trying to figure out which partial instantiation to use can | |
4839 | cause a crash. (Some of the template arguments don't even have | |
4840 | types.) So, we just use the most general version. */ | |
4841 | t = NULL_TREE; | |
4842 | else | |
73aad9b9 | 4843 | { |
6bdb985f MM |
4844 | t = most_specialized_class (template, args); |
4845 | ||
4846 | if (t == error_mark_node) | |
73aad9b9 | 4847 | { |
d8e178a0 | 4848 | const char *str = "candidates are:"; |
6bdb985f MM |
4849 | cp_error ("ambiguous class template instantiation for `%#T'", type); |
4850 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; | |
4851 | t = TREE_CHAIN (t)) | |
73aad9b9 | 4852 | { |
6bdb985f MM |
4853 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
4854 | args)) | |
4855 | { | |
4856 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
4857 | str = " "; | |
4858 | } | |
73aad9b9 | 4859 | } |
6bdb985f MM |
4860 | TYPE_BEING_DEFINED (type) = 1; |
4861 | type = error_mark_node; | |
4862 | goto end; | |
73aad9b9 | 4863 | } |
73aad9b9 | 4864 | } |
6bdb985f MM |
4865 | |
4866 | if (t) | |
73aad9b9 JM |
4867 | pattern = TREE_TYPE (t); |
4868 | else | |
4869 | pattern = TREE_TYPE (template); | |
5566b478 | 4870 | |
4c571114 MM |
4871 | /* If the template we're instantiating is incomplete, then clearly |
4872 | there's nothing we can do. */ | |
5566b478 | 4873 | if (TYPE_SIZE (pattern) == NULL_TREE) |
7ddedda4 | 4874 | goto end; |
5566b478 | 4875 | |
4c571114 MM |
4876 | /* If this is a partial instantiation, don't tsubst anything. We will |
4877 | only use this type for implicit typename, so the actual contents don't | |
4878 | matter. All that matters is whether a particular name is a type. */ | |
4879 | if (PARTIAL_INSTANTIATION_P (type)) | |
4880 | { | |
4881 | /* The fields set here must be kept in sync with those cleared | |
4882 | in begin_class_definition. */ | |
4883 | TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); | |
4884 | TYPE_FIELDS (type) = TYPE_FIELDS (pattern); | |
4885 | TYPE_METHODS (type) = TYPE_METHODS (pattern); | |
4886 | CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern); | |
4887 | /* Pretend that the type is complete, so that we will look | |
4888 | inside it during name lookup and such. */ | |
4889 | TYPE_SIZE (type) = integer_zero_node; | |
4890 | goto end; | |
4891 | } | |
4892 | ||
4893 | /* If we've recursively instantiated too many templates, stop. */ | |
4894 | if (! push_tinst_level (type)) | |
4895 | goto end; | |
4896 | ||
4897 | /* Now we're really doing the instantiation. Mark the type as in | |
4898 | the process of being defined. */ | |
4899 | TYPE_BEING_DEFINED (type) = 1; | |
4900 | ||
4901 | maybe_push_to_top_level (uses_template_parms (type)); | |
4c571114 | 4902 | |
73aad9b9 | 4903 | if (t) |
36a117a5 MM |
4904 | { |
4905 | /* This TYPE is actually a instantiation of of a partial | |
4906 | specialization. We replace the innermost set of ARGS with | |
4907 | the arguments appropriate for substitution. For example, | |
4908 | given: | |
4909 | ||
4910 | template <class T> struct S {}; | |
4911 | template <class T> struct S<T*> {}; | |
4912 | ||
4913 | and supposing that we are instantiating S<int*>, ARGS will | |
4914 | present be {int*} but we need {int}. */ | |
4915 | tree inner_args | |
4916 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
4917 | args); | |
4918 | ||
4919 | /* If there were multiple levels in ARGS, replacing the | |
4920 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
4921 | want, so we make a copy first. */ | |
4922 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
4923 | { | |
4924 | args = copy_node (args); | |
4925 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
4926 | } | |
4927 | else | |
4928 | args = inner_args; | |
4929 | } | |
8d019cef | 4930 | |
5566b478 MS |
4931 | if (flag_external_templates) |
4932 | { | |
4933 | if (flag_alt_external_templates) | |
4934 | { | |
4935 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
4936 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
4937 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4938 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4939 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4940 | } |
4941 | else | |
4942 | { | |
4943 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
4944 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
4945 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
4946 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4947 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4948 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4949 | } |
4950 | } | |
4951 | else | |
8d08fdba | 4952 | { |
5566b478 MS |
4953 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
4954 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
4955 | } |
4956 | ||
f7da6097 MS |
4957 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
4958 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
f7da6097 MS |
4959 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); |
4960 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
4961 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
4962 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
4963 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
4964 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
4965 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
4966 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
4967 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
4968 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
4969 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
4970 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
4971 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
4972 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
4973 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
4974 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
4975 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
4976 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
4977 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
4978 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
4979 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
eff71ab0 | 4980 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
4981 | if (ANON_AGGR_TYPE_P (pattern)) |
4982 | SET_ANON_AGGR_TYPE_P (type); | |
f7da6097 | 4983 | |
4c571114 MM |
4984 | /* We must copy the arguments to the permanent obstack since |
4985 | during the tsubst'ing below they may wind up in the | |
4986 | DECL_TI_ARGS of some instantiated member template. */ | |
4987 | args = copy_to_permanent (args); | |
2604412d | 4988 | |
3fd71a52 MM |
4989 | if (TYPE_BINFO_BASETYPES (pattern)) |
4990 | { | |
4991 | tree base_list = NULL_TREE; | |
4992 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
4993 | int i; | |
5566b478 | 4994 | |
3fd71a52 MM |
4995 | /* Substitute into each of the bases to determine the actual |
4996 | basetypes. */ | |
4997 | for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) | |
4998 | { | |
4999 | tree base; | |
5000 | tree access; | |
5001 | tree pbase; | |
5566b478 | 5002 | |
3fd71a52 | 5003 | pbase = TREE_VEC_ELT (pbases, i); |
711734a9 | 5004 | |
3fd71a52 MM |
5005 | /* Substitue to figure out the base class. */ |
5006 | base = tsubst (BINFO_TYPE (pbase), args, | |
5007 | /*complain=*/1, NULL_TREE); | |
5008 | if (base == error_mark_node) | |
5009 | continue; | |
711734a9 | 5010 | |
3fd71a52 MM |
5011 | /* Calculate the correct access node. */ |
5012 | if (TREE_VIA_VIRTUAL (pbase)) | |
5013 | { | |
5014 | if (TREE_VIA_PUBLIC (pbase)) | |
5015 | access = access_public_virtual_node; | |
5016 | else if (TREE_VIA_PROTECTED (pbase)) | |
5017 | access = access_protected_virtual_node; | |
d6479fe7 | 5018 | else |
3fd71a52 MM |
5019 | access = access_private_virtual_node; |
5020 | } | |
5021 | else | |
5022 | { | |
5023 | if (TREE_VIA_PUBLIC (pbase)) | |
5024 | access = access_public_node; | |
5025 | else if (TREE_VIA_PROTECTED (pbase)) | |
5026 | access = access_protected_node; | |
d6479fe7 | 5027 | else |
3fd71a52 MM |
5028 | access = access_private_node; |
5029 | } | |
dfbcd65a | 5030 | |
3fd71a52 MM |
5031 | base_list = tree_cons (access, base, base_list); |
5032 | } | |
dfbcd65a | 5033 | |
3fd71a52 MM |
5034 | /* The list is now in reverse order; correct that. */ |
5035 | base_list = nreverse (base_list); | |
5036 | ||
5037 | /* Now call xref_basetypes to set up all the base-class | |
5038 | information. */ | |
5039 | xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE | |
5040 | ? (CLASSTYPE_DECLARED_CLASS (pattern) | |
5041 | ? class_type_node : record_type_node) | |
5042 | : union_type_node, | |
5043 | DECL_NAME (TYPE_NAME (pattern)), | |
5044 | type, | |
5045 | base_list); | |
5046 | } | |
5566b478 | 5047 | |
b74a0560 MM |
5048 | /* Now that our base classes are set up, enter the scope of the |
5049 | class, so that name lookups into base classes, etc. will work | |
5050 | corectly. This is precisely analagous to what we do in | |
5051 | begin_class_definition when defining an ordinary non-template | |
5052 | class. */ | |
5053 | pushclass (type, 1); | |
5054 | ||
5566b478 | 5055 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) |
8d08fdba | 5056 | { |
5566b478 | 5057 | tree tag = TREE_VALUE (t); |
36a117a5 MM |
5058 | tree name = TYPE_IDENTIFIER (tag); |
5059 | tree newtag; | |
5566b478 | 5060 | |
4393e105 | 5061 | newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE); |
61a127b3 | 5062 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
ed44da02 | 5063 | { |
486e4077 MM |
5064 | if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) |
5065 | /* Unfortunately, lookup_template_class sets | |
5066 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
5067 | instantiation (i.e., for the type of a member template | |
5068 | class nested within a template class.) This behavior is | |
5069 | required for maybe_process_partial_specialization to work | |
5070 | correctly, but is not accurate in this case; the TAG is not | |
5071 | an instantiation of anything. (The corresponding | |
5072 | TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ | |
5073 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; | |
5074 | ||
ed44da02 MM |
5075 | /* Now, we call pushtag to put this NEWTAG into the scope of |
5076 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
5077 | pushtag calling push_template_decl. We don't have to do | |
5078 | this for enums because it will already have been done in | |
5079 | tsubst_enum. */ | |
5080 | if (name) | |
5081 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
5082 | pushtag (name, newtag, /*globalize=*/0); | |
5083 | } | |
8d08fdba MS |
5084 | } |
5085 | ||
5566b478 MS |
5086 | /* Don't replace enum constants here. */ |
5087 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
8dd3f57a | 5088 | if (TREE_CODE (t) != CONST_DECL) |
5566b478 | 5089 | { |
ae58fa02 MM |
5090 | tree r; |
5091 | ||
5092 | /* The the file and line for this declaration, to assist in | |
5093 | error message reporting. Since we called push_tinst_level | |
5094 | above, we don't need to restore these. */ | |
5095 | lineno = DECL_SOURCE_LINE (t); | |
5096 | input_filename = DECL_SOURCE_FILE (t); | |
5097 | ||
4393e105 | 5098 | r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
5566b478 MS |
5099 | if (TREE_CODE (r) == VAR_DECL) |
5100 | { | |
fa8d6e85 MM |
5101 | tree init; |
5102 | ||
94c82a77 | 5103 | if (DECL_DEFINED_IN_CLASS_P (r)) |
fa8d6e85 MM |
5104 | init = tsubst_expr (DECL_INITIAL (t), args, |
5105 | /*complain=*/1, NULL_TREE); | |
5106 | else | |
5107 | init = NULL_TREE; | |
5108 | ||
5109 | finish_static_data_member_decl (r, init, | |
5110 | /*asmspec_tree=*/NULL_TREE, | |
5111 | /*need_pop=*/0, | |
5112 | /*flags=*/0); | |
5113 | ||
6ba89f8e MM |
5114 | if (DECL_DEFINED_IN_CLASS_P (r)) |
5115 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5566b478 | 5116 | } |
61a127b3 MM |
5117 | |
5118 | /* R will have a TREE_CHAIN if and only if it has already been | |
5119 | processed by finish_member_declaration. This can happen | |
5120 | if, for example, it is a TYPE_DECL for a class-scoped | |
5121 | ENUMERAL_TYPE; such a thing will already have been added to | |
5122 | the field list by tsubst_enum above. */ | |
5123 | if (!TREE_CHAIN (r)) | |
5124 | { | |
5125 | set_current_access_from_decl (r); | |
5126 | finish_member_declaration (r); | |
5127 | } | |
5566b478 | 5128 | } |
8d08fdba | 5129 | |
61a127b3 MM |
5130 | /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC) |
5131 | for this instantiation. */ | |
5132 | for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t)) | |
5133 | { | |
4393e105 | 5134 | tree r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
61a127b3 MM |
5135 | set_current_access_from_decl (r); |
5136 | finish_member_declaration (r); | |
5137 | } | |
8d08fdba | 5138 | |
2604412d JM |
5139 | /* Construct the DECL_FRIENDLIST for the new class type. */ |
5140 | typedecl = TYPE_MAIN_DECL (type); | |
5141 | for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)); | |
5142 | t != NULL_TREE; | |
5143 | t = TREE_CHAIN (t)) | |
f84b4be9 | 5144 | { |
2604412d | 5145 | tree friends; |
f84b4be9 | 5146 | |
2604412d JM |
5147 | for (friends = TREE_VALUE (t); |
5148 | friends != NULL_TREE; | |
5149 | friends = TREE_CHAIN (friends)) | |
0c0aac2f MM |
5150 | if (TREE_PURPOSE (friends) == error_mark_node) |
5151 | add_friend (type, | |
5152 | tsubst_friend_function (TREE_VALUE (friends), | |
5153 | args)); | |
5154 | else | |
5155 | add_friends (type, | |
5156 | tsubst_copy (TREE_PURPOSE (t), args, | |
5157 | /*complain=*/1, NULL_TREE), | |
5158 | tsubst (TREE_PURPOSE (friends), args, | |
5159 | /*complain=*/1, NULL_TREE)); | |
2604412d | 5160 | } |
5566b478 | 5161 | |
6757edfe MM |
5162 | for (t = CLASSTYPE_FRIEND_CLASSES (pattern); |
5163 | t != NULL_TREE; | |
5164 | t = TREE_CHAIN (t)) | |
5165 | { | |
5166 | tree friend_type = TREE_VALUE (t); | |
1aed5355 | 5167 | tree new_friend_type; |
6757edfe | 5168 | |
36a117a5 MM |
5169 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5170 | new_friend_type = tsubst_friend_class (friend_type, args); | |
5171 | else if (uses_template_parms (friend_type)) | |
4393e105 MM |
5172 | new_friend_type = tsubst (friend_type, args, /*complain=*/1, |
5173 | NULL_TREE); | |
36a117a5 | 5174 | else |
61fc8c9e MM |
5175 | { |
5176 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); | |
36a117a5 | 5177 | |
61fc8c9e MM |
5178 | /* The call to xref_tag_from_type does injection for friend |
5179 | classes. */ | |
5180 | push_nested_namespace (ns); | |
5181 | new_friend_type = | |
5182 | xref_tag_from_type (friend_type, NULL_TREE, 1); | |
5183 | pop_nested_namespace (ns); | |
5184 | } | |
1aed5355 MM |
5185 | |
5186 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5187 | /* Trick make_friend_class into realizing that the friend | |
5188 | we're adding is a template, not an ordinary class. It's | |
5189 | important that we use make_friend_class since it will | |
5190 | perform some error-checking and output cross-reference | |
5191 | information. */ | |
5192 | ++processing_template_decl; | |
5193 | ||
5194 | make_friend_class (type, new_friend_type); | |
fc378698 | 5195 | |
1aed5355 MM |
5196 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5197 | --processing_template_decl; | |
6757edfe | 5198 | } |
fc378698 | 5199 | |
2604412d JM |
5200 | for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
5201 | if (TREE_CODE (t) == FIELD_DECL) | |
5202 | { | |
5203 | TREE_TYPE (t) = complete_type (TREE_TYPE (t)); | |
5204 | require_complete_type (t); | |
5205 | } | |
5566b478 | 5206 | |
61a127b3 MM |
5207 | /* Set the file and line number information to whatever is given for |
5208 | the class itself. This puts error messages involving generated | |
5209 | implicit functions at a predictable point, and the same point | |
5210 | that would be used for non-template classes. */ | |
5211 | lineno = DECL_SOURCE_LINE (typedecl); | |
5212 | input_filename = DECL_SOURCE_FILE (typedecl); | |
5213 | ||
5214 | unreverse_member_declarations (type); | |
9f33663b | 5215 | finish_struct_1 (type); |
2604412d | 5216 | CLASSTYPE_GOT_SEMICOLON (type) = 1; |
e92cc029 | 5217 | |
5524676d JM |
5218 | /* Clear this now so repo_template_used is happy. */ |
5219 | TYPE_BEING_DEFINED (type) = 0; | |
2604412d | 5220 | repo_template_used (type); |
8d08fdba | 5221 | |
9188c363 MM |
5222 | /* Now that the class is complete, instantiate default arguments for |
5223 | any member functions. We don't do this earlier because the | |
5224 | default arguments may reference members of the class. */ | |
5225 | if (!PRIMARY_TEMPLATE_P (template)) | |
5226 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
5227 | if (TREE_CODE (t) == FUNCTION_DECL | |
5228 | /* Implicitly generated member functions will not have tmplate | |
5229 | information; they are not instantiations, but instead are | |
5230 | created "fresh" for each instantiation. */ | |
5231 | && DECL_TEMPLATE_INFO (t)) | |
5232 | tsubst_default_arguments (t); | |
5233 | ||
b74a0560 | 5234 | popclass (); |
5566b478 MS |
5235 | pop_from_top_level (); |
5236 | pop_tinst_level (); | |
5237 | ||
7ddedda4 MM |
5238 | end: |
5239 | pop_momentary (); | |
5240 | ||
5566b478 | 5241 | return type; |
8d08fdba MS |
5242 | } |
5243 | ||
5244 | static int | |
5245 | list_eq (t1, t2) | |
5246 | tree t1, t2; | |
5247 | { | |
5248 | if (t1 == NULL_TREE) | |
5249 | return t2 == NULL_TREE; | |
5250 | if (t2 == NULL_TREE) | |
5251 | return 0; | |
5252 | /* Don't care if one declares its arg const and the other doesn't -- the | |
5253 | main variant of the arg type is all that matters. */ | |
5254 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
5255 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
5256 | return 0; | |
5257 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
5258 | } | |
5259 | ||
00d3396f JM |
5260 | /* If arg is a non-type template parameter that does not depend on template |
5261 | arguments, fold it like we weren't in the body of a template. */ | |
5262 | ||
5263 | static tree | |
5264 | maybe_fold_nontype_arg (arg) | |
5265 | tree arg; | |
5266 | { | |
5267 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
5268 | && !uses_template_parms (arg)) | |
5269 | { | |
5270 | /* Sometimes, one of the args was an expression involving a | |
5271 | template constant parameter, like N - 1. Now that we've | |
5272 | tsubst'd, we might have something like 2 - 1. This will | |
5273 | confuse lookup_template_class, so we do constant folding | |
5274 | here. We have to unset processing_template_decl, to | |
5275 | fool build_expr_from_tree() into building an actual | |
5276 | tree. */ | |
5277 | ||
5278 | int saved_processing_template_decl = processing_template_decl; | |
5279 | processing_template_decl = 0; | |
5280 | arg = fold (build_expr_from_tree (arg)); | |
5281 | processing_template_decl = saved_processing_template_decl; | |
5282 | } | |
5283 | return arg; | |
5284 | } | |
5285 | ||
a221d52f | 5286 | /* Return the TREE_VEC with the arguments for the innermost template header, |
36a117a5 MM |
5287 | where ARGS is either that or the VEC of VECs for all the |
5288 | arguments. */ | |
a221d52f JM |
5289 | |
5290 | tree | |
36a117a5 | 5291 | innermost_args (args) |
a221d52f | 5292 | tree args; |
a221d52f | 5293 | { |
36a117a5 | 5294 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
a221d52f | 5295 | } |
f84b4be9 | 5296 | |
830bfa74 MM |
5297 | /* Substitute ARGS into the vector of template arguments T. */ |
5298 | ||
e9659ab0 | 5299 | static tree |
4393e105 | 5300 | tsubst_template_arg_vector (t, args, complain) |
830bfa74 MM |
5301 | tree t; |
5302 | tree args; | |
4393e105 | 5303 | int complain; |
830bfa74 MM |
5304 | { |
5305 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
5306 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5307 | ||
5308 | bzero ((char *) elts, len * sizeof (tree)); | |
5309 | ||
5310 | for (i = 0; i < len; i++) | |
5311 | { | |
5312 | if (TREE_VEC_ELT (t, i) != NULL_TREE | |
5313 | && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) | |
4393e105 MM |
5314 | elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), |
5315 | args, complain); | |
830bfa74 MM |
5316 | else |
5317 | elts[i] = maybe_fold_nontype_arg | |
4393e105 MM |
5318 | (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, |
5319 | NULL_TREE)); | |
830bfa74 MM |
5320 | |
5321 | if (elts[i] != TREE_VEC_ELT (t, i)) | |
5322 | need_new = 1; | |
5323 | } | |
5324 | ||
5325 | if (!need_new) | |
5326 | return t; | |
5327 | ||
7ddedda4 | 5328 | t = make_temp_vec (len); |
830bfa74 MM |
5329 | for (i = 0; i < len; i++) |
5330 | TREE_VEC_ELT (t, i) = elts[i]; | |
5331 | ||
5332 | return t; | |
5333 | } | |
5334 | ||
36a117a5 MM |
5335 | /* Return the result of substituting ARGS into the template parameters |
5336 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5337 | PARMS, then the result will contain n levels of PARMS. For | |
5338 | example, if PARMS is `template <class T> template <class U> | |
5339 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5340 | result will be `template <int*, double, class V>'. */ | |
5341 | ||
e9659ab0 | 5342 | static tree |
4393e105 | 5343 | tsubst_template_parms (parms, args, complain) |
36a117a5 MM |
5344 | tree parms; |
5345 | tree args; | |
4393e105 | 5346 | int complain; |
36a117a5 | 5347 | { |
f71f87f9 MM |
5348 | tree r = NULL_TREE; |
5349 | tree* new_parms; | |
36a117a5 MM |
5350 | |
5351 | for (new_parms = &r; | |
5352 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5353 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5354 | parms = TREE_CHAIN (parms)) | |
5355 | { | |
5356 | tree new_vec = | |
5357 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5358 | int i; | |
5359 | ||
5360 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5361 | { | |
5362 | tree default_value = | |
5363 | TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5364 | tree parm_decl = | |
5365 | TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5366 | ||
5367 | TREE_VEC_ELT (new_vec, i) | |
4393e105 MM |
5368 | = build_tree_list (tsubst (default_value, args, complain, |
5369 | NULL_TREE), | |
5370 | tsubst (parm_decl, args, complain, | |
5371 | NULL_TREE)); | |
36a117a5 MM |
5372 | } |
5373 | ||
5374 | *new_parms = | |
5375 | tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms) | |
5376 | - TMPL_ARGS_DEPTH (args))), | |
5377 | new_vec, NULL_TREE); | |
5378 | } | |
5379 | ||
5380 | return r; | |
5381 | } | |
5382 | ||
ed44da02 MM |
5383 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
5384 | type T. If T is not an aggregate or enumeration type, it is | |
5385 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
5386 | ENTERING_SCOPE is non-zero, T is the context for a template which | |
5387 | we are presently tsubst'ing. Return the subsituted value. */ | |
36a117a5 | 5388 | |
e9659ab0 | 5389 | static tree |
4393e105 | 5390 | tsubst_aggr_type (t, args, complain, in_decl, entering_scope) |
36a117a5 MM |
5391 | tree t; |
5392 | tree args; | |
4393e105 | 5393 | int complain; |
36a117a5 MM |
5394 | tree in_decl; |
5395 | int entering_scope; | |
5396 | { | |
5397 | if (t == NULL_TREE) | |
5398 | return NULL_TREE; | |
5399 | ||
5400 | switch (TREE_CODE (t)) | |
5401 | { | |
5402 | case RECORD_TYPE: | |
5403 | if (TYPE_PTRMEMFUNC_P (t)) | |
5404 | { | |
5405 | tree r = build_ptrmemfunc_type | |
4393e105 | 5406 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl)); |
adecb3f4 MM |
5407 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), |
5408 | complain); | |
36a117a5 MM |
5409 | } |
5410 | ||
5411 | /* else fall through */ | |
ed44da02 | 5412 | case ENUMERAL_TYPE: |
36a117a5 | 5413 | case UNION_TYPE: |
5db698f6 | 5414 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
5415 | { |
5416 | tree argvec; | |
5417 | tree context; | |
5418 | tree r; | |
5419 | ||
5420 | /* First, determine the context for the type we are looking | |
5421 | up. */ | |
5422 | if (TYPE_CONTEXT (t) != NULL_TREE) | |
5423 | context = tsubst_aggr_type (TYPE_CONTEXT (t), args, | |
4393e105 | 5424 | complain, |
36a117a5 MM |
5425 | in_decl, /*entering_scope=*/1); |
5426 | else | |
5427 | context = NULL_TREE; | |
5428 | ||
5429 | /* Then, figure out what arguments are appropriate for the | |
5430 | type we are trying to find. For example, given: | |
5431 | ||
5432 | template <class T> struct S; | |
5433 | template <class T, class U> void f(T, U) { S<U> su; } | |
5434 | ||
5435 | and supposing that we are instantiating f<int, double>, | |
5436 | then our ARGS will be {int, double}, but, when looking up | |
5437 | S we only want {double}. */ | |
7ddedda4 | 5438 | push_momentary (); |
4393e105 MM |
5439 | argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, |
5440 | complain); | |
36a117a5 MM |
5441 | |
5442 | r = lookup_template_class (t, argvec, in_decl, context, | |
5443 | entering_scope); | |
7ddedda4 | 5444 | pop_momentary (); |
36a117a5 | 5445 | |
adecb3f4 MM |
5446 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), |
5447 | complain); | |
36a117a5 MM |
5448 | } |
5449 | else | |
5450 | /* This is not a template type, so there's nothing to do. */ | |
5451 | return t; | |
5452 | ||
5453 | default: | |
4393e105 | 5454 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
5455 | } |
5456 | } | |
5457 | ||
9188c363 MM |
5458 | /* Substitute into the default argument ARG (a default argument for |
5459 | FN), which has the indicated TYPE. */ | |
5460 | ||
5461 | tree | |
5462 | tsubst_default_argument (fn, type, arg) | |
5463 | tree fn; | |
5464 | tree type; | |
5465 | tree arg; | |
5466 | { | |
5467 | /* This default argument came from a template. Instantiate the | |
5468 | default argument here, not in tsubst. In the case of | |
5469 | something like: | |
5470 | ||
5471 | template <class T> | |
5472 | struct S { | |
5473 | static T t(); | |
5474 | void f(T = t()); | |
5475 | }; | |
5476 | ||
5477 | we must be careful to do name lookup in the scope of S<T>, | |
5478 | rather than in the current class. */ | |
5479 | if (DECL_CLASS_SCOPE_P (fn)) | |
5480 | pushclass (DECL_REAL_CONTEXT (fn), 2); | |
5481 | ||
5482 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE); | |
5483 | ||
5484 | if (DECL_CLASS_SCOPE_P (fn)) | |
5485 | popclass (); | |
5486 | ||
5487 | /* Make sure the default argument is reasonable. */ | |
5488 | arg = check_default_argument (type, arg); | |
5489 | ||
5490 | return arg; | |
5491 | } | |
5492 | ||
5493 | /* Substitute into all the default arguments for FN. */ | |
5494 | ||
5495 | static void | |
5496 | tsubst_default_arguments (fn) | |
5497 | tree fn; | |
5498 | { | |
5499 | tree arg; | |
5500 | tree tmpl_args; | |
5501 | ||
5502 | tmpl_args = DECL_TI_ARGS (fn); | |
5503 | ||
5504 | /* If this function is not yet instantiated, we certainly don't need | |
5505 | its default arguments. */ | |
5506 | if (uses_template_parms (tmpl_args)) | |
5507 | return; | |
5508 | ||
5509 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
5510 | arg; | |
5511 | arg = TREE_CHAIN (arg)) | |
5512 | if (TREE_PURPOSE (arg)) | |
5513 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, | |
5514 | TREE_VALUE (arg), | |
5515 | TREE_PURPOSE (arg)); | |
5516 | } | |
5517 | ||
ae58fa02 MM |
5518 | /* Substitute the ARGS into the T, which is a _DECL. TYPE is the |
5519 | (already computed) substitution of ARGS into TREE_TYPE (T), if | |
5520 | appropriate. Return the result of the substitution. IN_DECL is as | |
5521 | for tsubst. */ | |
00d3396f | 5522 | |
e9659ab0 | 5523 | static tree |
ae58fa02 MM |
5524 | tsubst_decl (t, args, type, in_decl) |
5525 | tree t; | |
5526 | tree args; | |
5527 | tree type; | |
8d08fdba MS |
5528 | tree in_decl; |
5529 | { | |
ae58fa02 MM |
5530 | int saved_lineno; |
5531 | char* saved_filename; | |
b370501f | 5532 | tree r = NULL_TREE; |
830bfa74 | 5533 | |
ae58fa02 MM |
5534 | /* Set the filename and linenumber to improve error-reporting. */ |
5535 | saved_lineno = lineno; | |
5536 | saved_filename = input_filename; | |
5537 | lineno = DECL_SOURCE_LINE (t); | |
5538 | input_filename = DECL_SOURCE_FILE (t); | |
b7484fbe | 5539 | |
8d08fdba MS |
5540 | switch (TREE_CODE (t)) |
5541 | { | |
98c1c668 JM |
5542 | case TEMPLATE_DECL: |
5543 | { | |
5544 | /* We can get here when processing a member template function | |
5545 | of a template class. */ | |
98c1c668 | 5546 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 5547 | tree spec; |
db2767b6 | 5548 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); |
98c1c668 | 5549 | |
db2767b6 MM |
5550 | if (!is_template_template_parm) |
5551 | { | |
36a117a5 MM |
5552 | /* We might already have an instance of this template. |
5553 | The ARGS are for the surrounding class type, so the | |
5554 | full args contain the tsubst'd args for the context, | |
5555 | plus the innermost args from the template decl. */ | |
5556 | tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
5557 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
5558 | : DECL_TI_ARGS (DECL_RESULT (t)); | |
7ddedda4 MM |
5559 | tree full_args; |
5560 | ||
5561 | push_momentary (); | |
4393e105 MM |
5562 | full_args = tsubst_template_arg_vector (tmpl_args, args, |
5563 | /*complain=*/1); | |
36a117a5 MM |
5564 | |
5565 | /* tsubst_template_arg_vector doesn't copy the vector if | |
5566 | nothing changed. But, *something* should have | |
5567 | changed. */ | |
5568 | my_friendly_assert (full_args != tmpl_args, 0); | |
5569 | ||
5570 | spec = retrieve_specialization (t, full_args); | |
7ddedda4 | 5571 | pop_momentary (); |
db2767b6 | 5572 | if (spec != NULL_TREE) |
ae58fa02 MM |
5573 | { |
5574 | r = spec; | |
5575 | break; | |
5576 | } | |
db2767b6 | 5577 | } |
98c1c668 JM |
5578 | |
5579 | /* Make a new template decl. It will be similar to the | |
5580 | original, but will record the current template arguments. | |
5581 | We also create a new function declaration, which is just | |
5582 | like the old one, but points to this new template, rather | |
5583 | than the old one. */ | |
ae58fa02 MM |
5584 | r = copy_node (t); |
5585 | copy_lang_decl (r); | |
5586 | my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); | |
5587 | TREE_CHAIN (r) = NULL_TREE; | |
db2767b6 MM |
5588 | |
5589 | if (is_template_template_parm) | |
5590 | { | |
4393e105 | 5591 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5592 | DECL_RESULT (r) = new_decl; |
5593 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5594 | break; | |
db2767b6 MM |
5595 | } |
5596 | ||
ae58fa02 | 5597 | DECL_CONTEXT (r) |
4393e105 MM |
5598 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, |
5599 | in_decl, /*entering_scope=*/1); | |
ae58fa02 | 5600 | DECL_CLASS_CONTEXT (r) |
4393e105 MM |
5601 | = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5602 | /*complain=*/1, in_decl, | |
5603 | /*entering_scope=*/1); | |
ae58fa02 | 5604 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
5605 | |
5606 | if (TREE_CODE (decl) == TYPE_DECL) | |
5607 | { | |
4393e105 MM |
5608 | tree new_type = tsubst (TREE_TYPE (t), args, |
5609 | /*complain=*/1, in_decl); | |
ae58fa02 MM |
5610 | TREE_TYPE (r) = new_type; |
5611 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
5612 | DECL_RESULT (r) = TYPE_MAIN_DECL (new_type); | |
5613 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); | |
93cdc044 JM |
5614 | } |
5615 | else | |
5616 | { | |
4393e105 | 5617 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5618 | DECL_RESULT (r) = new_decl; |
5619 | DECL_TI_TEMPLATE (new_decl) = r; | |
5620 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5621 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
5622 | } |
5623 | ||
ae58fa02 MM |
5624 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5625 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
5626 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
5627 | |
5628 | /* The template parameters for this new template are all the | |
5629 | template parameters for the old template, except the | |
5630 | outermost level of parameters. */ | |
ae58fa02 | 5631 | DECL_TEMPLATE_PARMS (r) |
4393e105 MM |
5632 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
5633 | /*complain=*/1); | |
98c1c668 | 5634 | |
93cdc044 | 5635 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 5636 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 5637 | |
8d019cef | 5638 | /* We don't partially instantiate partial specializations. */ |
93cdc044 | 5639 | if (TREE_CODE (decl) == TYPE_DECL) |
ae58fa02 | 5640 | break; |
93cdc044 | 5641 | |
75650646 MM |
5642 | for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t); |
5643 | spec != NULL_TREE; | |
5644 | spec = TREE_CHAIN (spec)) | |
5645 | { | |
5646 | /* It helps to consider example here. Consider: | |
5647 | ||
5648 | template <class T> | |
5649 | struct S { | |
5650 | template <class U> | |
5651 | void f(U u); | |
5652 | ||
5653 | template <> | |
5654 | void f(T* t) {} | |
5655 | }; | |
5656 | ||
5657 | Now, for example, we are instantiating S<int>::f(U u). | |
5658 | We want to make a template: | |
5659 | ||
5660 | template <class U> | |
5661 | void S<int>::f(U); | |
5662 | ||
5663 | It will have a specialization, for the case U = int*, of | |
5664 | the form: | |
5665 | ||
5666 | template <> | |
5667 | void S<int>::f<int*>(int*); | |
5668 | ||
5669 | This specialization will be an instantiation of | |
5670 | the specialization given in the declaration of S, with | |
5671 | argument list int*. */ | |
5672 | ||
5673 | tree fn = TREE_VALUE (spec); | |
5674 | tree spec_args; | |
5675 | tree new_fn; | |
5676 | ||
5677 | if (!DECL_TEMPLATE_SPECIALIZATION (fn)) | |
5678 | /* Instantiations are on the same list, but they're of | |
5679 | no concern to us. */ | |
5680 | continue; | |
5681 | ||
36a117a5 MM |
5682 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
5683 | /* A full specialization. There's no need to record | |
5684 | that here. */ | |
5685 | continue; | |
5686 | ||
4393e105 MM |
5687 | spec_args = tsubst (DECL_TI_ARGS (fn), args, |
5688 | /*complain=*/1, in_decl); | |
36a117a5 | 5689 | new_fn = tsubst (DECL_RESULT (most_general_template (fn)), |
4393e105 | 5690 | spec_args, /*complain=*/1, in_decl); |
36a117a5 | 5691 | DECL_TI_TEMPLATE (new_fn) = fn; |
ae58fa02 | 5692 | register_specialization (new_fn, r, |
36a117a5 | 5693 | innermost_args (spec_args)); |
75650646 MM |
5694 | } |
5695 | ||
5696 | /* Record this partial instantiation. */ | |
ae58fa02 MM |
5697 | register_specialization (r, t, |
5698 | DECL_TI_ARGS (DECL_RESULT (r))); | |
75650646 | 5699 | |
98c1c668 | 5700 | } |
ae58fa02 | 5701 | break; |
8d08fdba MS |
5702 | |
5703 | case FUNCTION_DECL: | |
5704 | { | |
386b8a85 | 5705 | tree ctx; |
87603ed0 | 5706 | tree argvec = NULL_TREE; |
cf38f48a | 5707 | tree *friends; |
36a117a5 | 5708 | tree gen_tmpl; |
5566b478 | 5709 | int member; |
d8c4447d MM |
5710 | int args_depth; |
5711 | int parms_depth; | |
5566b478 | 5712 | |
36a117a5 MM |
5713 | /* Nobody should be tsubst'ing into non-template functions. */ |
5714 | my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5715 | ||
5716 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
5717 | { | |
5718 | tree spec; | |
5719 | ||
7ddedda4 MM |
5720 | /* Allocate template arguments on the momentary obstack, |
5721 | in case we don't need to keep them. */ | |
5722 | push_momentary (); | |
5723 | ||
36a117a5 MM |
5724 | /* Calculate the most general template of which R is a |
5725 | specialization, and the complete set of arguments used to | |
5726 | specialize R. */ | |
5727 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
7ddedda4 MM |
5728 | argvec |
5729 | = tsubst_template_arg_vector (DECL_TI_ARGS | |
5730 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
4393e105 | 5731 | args, /*complain=*/1); |
36a117a5 MM |
5732 | |
5733 | /* Check to see if we already have this specialization. */ | |
5734 | spec = retrieve_specialization (gen_tmpl, argvec); | |
7ddedda4 | 5735 | |
36a117a5 | 5736 | if (spec) |
ae58fa02 MM |
5737 | { |
5738 | r = spec; | |
7ddedda4 | 5739 | pop_momentary (); |
ae58fa02 MM |
5740 | break; |
5741 | } | |
d8c4447d | 5742 | |
7ddedda4 MM |
5743 | /* We're going to need to keep the ARGVEC, so we copy it |
5744 | here. */ | |
5745 | argvec = copy_to_permanent (argvec); | |
5746 | pop_momentary (); | |
5747 | ||
d8c4447d MM |
5748 | /* Here, we deal with the peculiar case: |
5749 | ||
5750 | template <class T> struct S { | |
5751 | template <class U> friend void f(); | |
5752 | }; | |
5753 | template <class U> friend void f() {} | |
5754 | template S<int>; | |
5755 | template void f<double>(); | |
5756 | ||
5757 | Here, the ARGS for the instantiation of will be {int, | |
5758 | double}. But, we only need as many ARGS as there are | |
5759 | levels of template parameters in CODE_PATTERN. We are | |
5760 | careful not to get fooled into reducing the ARGS in | |
5761 | situations like: | |
5762 | ||
5763 | template <class T> struct S { template <class U> void f(U); } | |
5764 | template <class T> template <> void S<T>::f(int) {} | |
5765 | ||
5766 | which we can spot because the pattern will be a | |
5767 | specialization in this case. */ | |
5768 | args_depth = TMPL_ARGS_DEPTH (args); | |
5769 | parms_depth = | |
5770 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
5771 | if (args_depth > parms_depth | |
5772 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
5773 | { | |
5774 | my_friendly_assert (DECL_FRIEND_P (t), 0); | |
5775 | ||
5776 | if (parms_depth > 1) | |
5777 | { | |
5778 | int i; | |
5779 | ||
5780 | args = make_temp_vec (parms_depth); | |
5781 | for (i = 0; i < parms_depth; ++i) | |
5782 | TREE_VEC_ELT (args, i) = | |
5783 | TREE_VEC_ELT (args, i + (args_depth - parms_depth)); | |
5784 | } | |
5785 | else | |
5786 | args = TREE_VEC_ELT (args, args_depth - parms_depth); | |
5787 | } | |
36a117a5 MM |
5788 | } |
5789 | else | |
5790 | { | |
5791 | /* This special case arises when we have something like this: | |
5792 | ||
5793 | template <class T> struct S { | |
5794 | friend void f<int>(int, double); | |
5795 | }; | |
5796 | ||
5797 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
39c01e4c MM |
5798 | will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are |
5799 | being called from tsubst_friend_function, and we want | |
5800 | only to create a new decl (R) with appropriate types so | |
5801 | that we can call determine_specialization. */ | |
5802 | my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5803 | == LOOKUP_EXPR) | |
5804 | || (TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5805 | == IDENTIFIER_NODE), 0); | |
36a117a5 MM |
5806 | gen_tmpl = NULL_TREE; |
5807 | } | |
5808 | ||
6eb3bb27 | 5809 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 5810 | { |
5566b478 MS |
5811 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
5812 | member = 2; | |
5813 | else | |
5814 | member = 1; | |
4393e105 MM |
5815 | ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5816 | /*complain=*/1, t, | |
36a117a5 | 5817 | /*entering_scope=*/1); |
5566b478 MS |
5818 | } |
5819 | else | |
5820 | { | |
5821 | member = 0; | |
5822 | ctx = NULL_TREE; | |
5566b478 | 5823 | } |
4393e105 | 5824 | type = tsubst (type, args, /*complain=*/1, in_decl); |
8d08fdba | 5825 | |
5566b478 MS |
5826 | /* We do NOT check for matching decls pushed separately at this |
5827 | point, as they may not represent instantiations of this | |
5828 | template, and in any case are considered separate under the | |
312e7d50 | 5829 | discrete model. Instead, see add_maybe_template. */ |
5566b478 MS |
5830 | |
5831 | r = copy_node (t); | |
5832 | copy_lang_decl (r); | |
e1467ff2 | 5833 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 MS |
5834 | TREE_TYPE (r) = type; |
5835 | ||
5836 | DECL_CONTEXT (r) | |
4393e105 MM |
5837 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, t, |
5838 | /*entering_scope=*/1); | |
5566b478 MS |
5839 | DECL_CLASS_CONTEXT (r) = ctx; |
5840 | ||
4c571114 | 5841 | if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r))) |
f181d4ae MM |
5842 | /* Type-conversion operator. Reconstruct the name, in |
5843 | case it's the name of one of the template's parameters. */ | |
5844 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
5566b478 | 5845 | |
4393e105 MM |
5846 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
5847 | /*complain=*/1, t); | |
711734a9 JM |
5848 | DECL_MAIN_VARIANT (r) = r; |
5849 | DECL_RESULT (r) = NULL_TREE; | |
711734a9 JM |
5850 | |
5851 | TREE_STATIC (r) = 0; | |
5852 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
5853 | DECL_EXTERNAL (r) = 1; | |
5854 | DECL_INTERFACE_KNOWN (r) = 0; | |
5855 | DECL_DEFER_OUTPUT (r) = 0; | |
5856 | TREE_CHAIN (r) = NULL_TREE; | |
5857 | DECL_PENDING_INLINE_INFO (r) = 0; | |
5858 | TREE_USED (r) = 0; | |
5859 | ||
36a117a5 MM |
5860 | /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled |
5861 | name. There's no need to do this in the special friend | |
5862 | case mentioned above where GEN_TMPL is NULL. */ | |
5863 | if (gen_tmpl) | |
386b8a85 | 5864 | { |
7ddedda4 MM |
5865 | /* The ARGVEC was built on the momentary obstack. Make it |
5866 | permanent now. */ | |
5867 | argvec = copy_to_permanent (argvec); | |
36a117a5 MM |
5868 | DECL_TEMPLATE_INFO (r) |
5869 | = perm_tree_cons (gen_tmpl, argvec, NULL_TREE); | |
5870 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5871 | register_specialization (r, gen_tmpl, argvec); | |
5872 | ||
5873 | /* Set the mangled name for R. */ | |
5874 | if (DECL_DESTRUCTOR_P (t)) | |
5875 | DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx); | |
5876 | else | |
386b8a85 | 5877 | { |
36a117a5 MM |
5878 | /* Instantiations of template functions must be mangled |
5879 | specially, in order to conform to 14.5.5.1 | |
5880 | [temp.over.link]. */ | |
5881 | tree tmpl = DECL_TI_TEMPLATE (t); | |
386b8a85 | 5882 | |
36a117a5 MM |
5883 | /* TMPL will be NULL if this is a specialization of a |
5884 | member function of a template class. */ | |
5885 | if (name_mangling_version < 1 | |
5886 | || tmpl == NULL_TREE | |
5887 | || (member && !is_member_template (tmpl) | |
5888 | && !DECL_TEMPLATE_INFO (tmpl))) | |
5889 | set_mangled_name_for_decl (r); | |
75650646 | 5890 | else |
36a117a5 | 5891 | set_mangled_name_for_template_decl (r); |
386b8a85 | 5892 | } |
36a117a5 MM |
5893 | |
5894 | DECL_RTL (r) = 0; | |
5895 | make_decl_rtl (r, NULL_PTR, 1); | |
5896 | ||
5897 | /* Like grokfndecl. If we don't do this, pushdecl will | |
5898 | mess up our TREE_CHAIN because it doesn't find a | |
5899 | previous decl. Sigh. */ | |
5900 | if (member | |
c36ae80f | 5901 | && ! uses_template_parms (r) |
36a117a5 MM |
5902 | && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) |
5903 | == NULL_TREE)) | |
5904 | SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r); | |
9188c363 MM |
5905 | |
5906 | /* We're not supposed to instantiate default arguments | |
5907 | until they are called, for a template. But, for a | |
5908 | declaration like: | |
5909 | ||
5910 | template <class T> void f () | |
5911 | { extern void g(int i = T()); } | |
5912 | ||
5913 | we should do the substitution when the template is | |
5914 | instantiated. We handle the member function case in | |
5915 | instantiate_class_template since the default arguments | |
5916 | might refer to other members of the class. */ | |
5917 | if (!member | |
5918 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
5919 | && !uses_template_parms (argvec)) | |
5920 | tsubst_default_arguments (r); | |
386b8a85 | 5921 | } |
f181d4ae | 5922 | |
cf38f48a MM |
5923 | /* Copy the list of befriending classes. */ |
5924 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
5925 | *friends; | |
5926 | friends = &TREE_CHAIN (*friends)) | |
5927 | { | |
5928 | *friends = copy_node (*friends); | |
5929 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
5930 | args, /*complain=*/1, | |
5931 | in_decl); | |
5932 | } | |
5933 | ||
f181d4ae MM |
5934 | if (DECL_CONSTRUCTOR_P (r)) |
5935 | { | |
5936 | maybe_retrofit_in_chrg (r); | |
5937 | grok_ctor_properties (ctx, r); | |
5938 | } | |
f71f87f9 | 5939 | else if (DECL_OVERLOADED_OPERATOR_P (r)) |
f181d4ae | 5940 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); |
8d08fdba | 5941 | } |
ae58fa02 | 5942 | break; |
8d08fdba MS |
5943 | |
5944 | case PARM_DECL: | |
5945 | { | |
ae58fa02 | 5946 | r = copy_node (t); |
5566b478 | 5947 | TREE_TYPE (r) = type; |
1b8899d1 MM |
5948 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); |
5949 | ||
db2767b6 MM |
5950 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
5951 | DECL_INITIAL (r) = TREE_TYPE (r); | |
5952 | else | |
4393e105 MM |
5953 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, |
5954 | /*complain=*/1, in_decl); | |
db2767b6 | 5955 | |
5566b478 | 5956 | DECL_CONTEXT (r) = NULL_TREE; |
fa56377d JJ |
5957 | if (PROMOTE_PROTOTYPES |
5958 | && (TREE_CODE (type) == INTEGER_TYPE | |
5959 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
f83b0cb6 JM |
5960 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
5961 | DECL_ARG_TYPE (r) = integer_type_node; | |
8d08fdba | 5962 | if (TREE_CHAIN (t)) |
4393e105 MM |
5963 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
5964 | /*complain=*/1, TREE_CHAIN (t)); | |
8d08fdba | 5965 | } |
ae58fa02 | 5966 | break; |
8d08fdba | 5967 | |
5566b478 MS |
5968 | case FIELD_DECL: |
5969 | { | |
ae58fa02 | 5970 | r = copy_node (t); |
5566b478 | 5971 | copy_lang_decl (r); |
1b8899d1 MM |
5972 | TREE_TYPE (r) = type; |
5973 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); | |
5974 | ||
5975 | /* We don't have to set DECL_CONTEXT here; it is set by | |
5976 | finish_member_declaration. */ | |
4393e105 MM |
5977 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, |
5978 | /*complain=*/1, in_decl); | |
5566b478 | 5979 | TREE_CHAIN (r) = NULL_TREE; |
4393e105 | 5980 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 5981 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5982 | } |
ae58fa02 | 5983 | break; |
5566b478 MS |
5984 | |
5985 | case USING_DECL: | |
5986 | { | |
ae58fa02 | 5987 | r = copy_node (t); |
5566b478 | 5988 | DECL_INITIAL (r) |
4393e105 | 5989 | = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl); |
5566b478 | 5990 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 5991 | } |
ae58fa02 | 5992 | break; |
5566b478 | 5993 | |
9188c363 MM |
5994 | case TYPE_DECL: |
5995 | if (DECL_IMPLICIT_TYPEDEF_P (t)) | |
5996 | { | |
5997 | /* For an implicit typedef, we just want the implicit | |
5998 | typedef for the tsubst'd type. We've already got the | |
5999 | tsubst'd type, as TYPE, so we just need it's associated | |
6000 | declaration. */ | |
6001 | r = TYPE_NAME (type); | |
6002 | break; | |
6003 | } | |
6004 | else if (!DECL_LANG_SPECIFIC (t)) | |
6005 | { | |
6006 | /* For a template type parameter, we don't have to do | |
6007 | anything special. */ | |
6008 | r= TYPE_NAME (type); | |
6009 | break; | |
6010 | } | |
6011 | ||
6012 | /* Fall through. */ | |
6013 | ||
5566b478 MS |
6014 | case VAR_DECL: |
6015 | { | |
36a117a5 MM |
6016 | tree argvec; |
6017 | tree gen_tmpl; | |
6018 | tree spec; | |
6019 | tree tmpl; | |
9188c363 MM |
6020 | tree ctx; |
6021 | ||
8dd3f57a MM |
6022 | /* Nobody should be tsubst'ing into non-template variables. */ |
6023 | my_friendly_assert (DECL_LANG_SPECIFIC (t) | |
6024 | && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5566b478 | 6025 | |
9188c363 MM |
6026 | if (TYPE_P (DECL_CONTEXT (t))) |
6027 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, | |
6028 | /*complain=*/1, | |
6029 | in_decl, /*entering_scope=*/1); | |
6030 | else | |
6031 | /* Subsequent calls to pushdecl will fill this in. */ | |
6032 | ctx = NULL_TREE; | |
6033 | ||
36a117a5 MM |
6034 | /* Check to see if we already have this specialization. */ |
6035 | tmpl = DECL_TI_TEMPLATE (t); | |
6036 | gen_tmpl = most_general_template (tmpl); | |
4393e105 | 6037 | argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl); |
9188c363 MM |
6038 | if (ctx) |
6039 | spec = retrieve_specialization (gen_tmpl, argvec); | |
6040 | else | |
6041 | spec = retrieve_local_specialization (gen_tmpl, | |
6042 | current_function_decl); | |
6043 | ||
36a117a5 | 6044 | if (spec) |
ae58fa02 MM |
6045 | { |
6046 | r = spec; | |
6047 | break; | |
6048 | } | |
5566b478 | 6049 | |
e0942dcd MM |
6050 | /* This declaration is going to have to be around for a while, |
6051 | so me make sure it is on a saveable obstack. */ | |
6052 | push_obstacks_nochange (); | |
6053 | saveable_allocation (); | |
5566b478 | 6054 | r = copy_node (t); |
e0942dcd MM |
6055 | pop_obstacks (); |
6056 | ||
5566b478 | 6057 | TREE_TYPE (r) = type; |
1b8899d1 | 6058 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r); |
5566b478 | 6059 | DECL_CONTEXT (r) = ctx; |
d11ad92e MS |
6060 | |
6061 | /* Don't try to expand the initializer until someone tries to use | |
6062 | this variable; otherwise we run into circular dependencies. */ | |
6063 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 MS |
6064 | DECL_RTL (r) = 0; |
6065 | DECL_SIZE (r) = 0; | |
36a117a5 MM |
6066 | copy_lang_decl (r); |
6067 | DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r); | |
5566b478 | 6068 | |
e0942dcd MM |
6069 | /* Even if the original location is out of scope, the newly |
6070 | substituted one is not. */ | |
6071 | if (TREE_CODE (r) == VAR_DECL) | |
6072 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
6073 | ||
fa8d6e85 MM |
6074 | /* A static data member declaration is always marked external |
6075 | when it is declared in-class, even if an initializer is | |
6076 | present. We mimic the non-template processing here. */ | |
e0942dcd MM |
6077 | if (ctx) |
6078 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 6079 | |
36a117a5 MM |
6080 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); |
6081 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
9188c363 MM |
6082 | if (ctx) |
6083 | register_specialization (r, gen_tmpl, argvec); | |
6084 | else | |
6085 | register_local_specialization (r, gen_tmpl, | |
6086 | current_function_decl); | |
5566b478 | 6087 | |
5566b478 | 6088 | TREE_CHAIN (r) = NULL_TREE; |
9188c363 | 6089 | if (TREE_CODE (r) == VAR_DECL && TREE_CODE (type) == VOID_TYPE) |
8251199e | 6090 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 6091 | } |
ae58fa02 | 6092 | break; |
5566b478 | 6093 | |
ae58fa02 MM |
6094 | default: |
6095 | my_friendly_abort (0); | |
6096 | } | |
6097 | ||
6098 | /* Restore the file and line information. */ | |
6099 | lineno = saved_lineno; | |
6100 | input_filename = saved_filename; | |
6101 | ||
6102 | return r; | |
6103 | } | |
6104 | ||
cabc336a MM |
6105 | /* Substitue into the ARG_TYPES of a function type. */ |
6106 | ||
e9659ab0 | 6107 | static tree |
4393e105 | 6108 | tsubst_arg_types (arg_types, args, complain, in_decl) |
cabc336a MM |
6109 | tree arg_types; |
6110 | tree args; | |
4393e105 | 6111 | int complain; |
cabc336a MM |
6112 | tree in_decl; |
6113 | { | |
6114 | tree remaining_arg_types; | |
cabc336a MM |
6115 | tree type; |
6116 | ||
6117 | if (!arg_types || arg_types == void_list_node) | |
6118 | return arg_types; | |
6119 | ||
6120 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
4393e105 MM |
6121 | args, complain, in_decl); |
6122 | if (remaining_arg_types == error_mark_node) | |
6123 | return error_mark_node; | |
6124 | ||
6125 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
6126 | if (type == error_mark_node) | |
6127 | return error_mark_node; | |
cabc336a | 6128 | |
4393e105 MM |
6129 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
6130 | top-level qualifiers as required. */ | |
6131 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a MM |
6132 | |
6133 | /* Note that we do not substitute into default arguments here. The | |
6134 | standard mandates that they be instantiated only when needed, | |
6135 | which is done in build_over_call. */ | |
51632249 JM |
6136 | return hash_tree_cons (TREE_PURPOSE (arg_types), type, |
6137 | remaining_arg_types); | |
cabc336a MM |
6138 | |
6139 | } | |
6140 | ||
4393e105 MM |
6141 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
6142 | *not* handle the exception-specification for FNTYPE, because the | |
6143 | initial substitution of explicitly provided template parameters | |
6144 | during argument deduction forbids substitution into the | |
6145 | exception-specification: | |
6146 | ||
6147 | [temp.deduct] | |
6148 | ||
6149 | All references in the function type of the function template to the | |
6150 | corresponding template parameters are replaced by the specified tem- | |
6151 | plate argument values. If a substitution in a template parameter or | |
6152 | in the function type of the function template results in an invalid | |
6153 | type, type deduction fails. [Note: The equivalent substitution in | |
6154 | exception specifications is done only when the function is instanti- | |
6155 | ated, at which point a program is ill-formed if the substitution | |
6156 | results in an invalid type.] */ | |
6157 | ||
6158 | static tree | |
6159 | tsubst_function_type (t, args, complain, in_decl) | |
6160 | tree t; | |
6161 | tree args; | |
6162 | int complain; | |
6163 | tree in_decl; | |
6164 | { | |
6165 | tree return_type; | |
6166 | tree arg_types; | |
6167 | tree fntype; | |
6168 | ||
8dd3f57a | 6169 | /* The TYPE_CONTEXT is not used for function/method types. */ |
4393e105 MM |
6170 | my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); |
6171 | ||
6172 | /* Substitue the return type. */ | |
6173 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6174 | if (return_type == error_mark_node) | |
6175 | return error_mark_node; | |
6176 | ||
6177 | /* Substitue the argument types. */ | |
6178 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, | |
6179 | complain, in_decl); | |
6180 | if (arg_types == error_mark_node) | |
6181 | return error_mark_node; | |
6182 | ||
6183 | /* Construct a new type node and return it. */ | |
6184 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
6185 | fntype = build_function_type (return_type, arg_types); | |
6186 | else | |
6187 | { | |
6188 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
6189 | if (! IS_AGGR_TYPE (r)) | |
6190 | { | |
6191 | /* [temp.deduct] | |
6192 | ||
6193 | Type deduction may fail for any of the following | |
6194 | reasons: | |
6195 | ||
6196 | -- Attempting to create "pointer to member of T" when T | |
6197 | is not a class type. */ | |
6198 | if (complain) | |
6199 | cp_error ("creating pointer to member function of non-class type `%T'", | |
6200 | r); | |
6201 | return error_mark_node; | |
6202 | } | |
6203 | ||
6204 | fntype = build_cplus_method_type (r, return_type, TREE_CHAIN | |
6205 | (arg_types)); | |
6206 | } | |
6207 | fntype = build_qualified_type (fntype, TYPE_QUALS (t)); | |
ada846eb | 6208 | fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
4393e105 MM |
6209 | |
6210 | return fntype; | |
6211 | } | |
6212 | ||
297e73d8 MM |
6213 | /* Substitute into the PARMS of a call-declarator. */ |
6214 | ||
e9659ab0 | 6215 | static tree |
4393e105 | 6216 | tsubst_call_declarator_parms (parms, args, complain, in_decl) |
297e73d8 MM |
6217 | tree parms; |
6218 | tree args; | |
4393e105 | 6219 | int complain; |
297e73d8 MM |
6220 | tree in_decl; |
6221 | { | |
6222 | tree new_parms; | |
6223 | tree type; | |
6224 | tree defarg; | |
6225 | ||
6226 | if (!parms || parms == void_list_node) | |
6227 | return parms; | |
6228 | ||
6229 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
4393e105 | 6230 | args, complain, in_decl); |
297e73d8 MM |
6231 | |
6232 | /* Figure out the type of this parameter. */ | |
4393e105 | 6233 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); |
297e73d8 MM |
6234 | |
6235 | /* Figure out the default argument as well. Note that we use | |
4393e105 MM |
6236 | tsubst_expr since the default argument is really an expression. */ |
6237 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
297e73d8 MM |
6238 | |
6239 | /* Chain this parameter on to the front of those we have already | |
6240 | processed. We don't use hash_tree_cons because that function | |
6241 | doesn't check TREE_PARMLIST. */ | |
6242 | new_parms = tree_cons (defarg, type, new_parms); | |
6243 | ||
6244 | /* And note that these are parameters. */ | |
6245 | TREE_PARMLIST (new_parms) = 1; | |
6246 | ||
6247 | return new_parms; | |
6248 | } | |
6249 | ||
4393e105 MM |
6250 | /* Take the tree structure T and replace template parameters used |
6251 | therein with the argument vector ARGS. IN_DECL is an associated | |
6252 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
6253 | An appropriate error message is issued only if COMPLAIN is | |
6254 | non-zero. Note that we must be relatively non-tolerant of | |
6255 | extensions here, in order to preserve conformance; if we allow | |
6256 | substitutions that should not be allowed, we may allow argument | |
6257 | deductions that should not succeed, and therefore report ambiguous | |
6258 | overload situations where there are none. In theory, we could | |
6259 | allow the substitution, but indicate that it should have failed, | |
6260 | and allow our caller to make sure that the right thing happens, but | |
6261 | we don't try to do this yet. | |
6262 | ||
6263 | This function is used for dealing with types, decls and the like; | |
6264 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 MM |
6265 | |
6266 | tree | |
4393e105 | 6267 | tsubst (t, args, complain, in_decl) |
ae58fa02 | 6268 | tree t, args; |
4393e105 | 6269 | int complain; |
ae58fa02 MM |
6270 | tree in_decl; |
6271 | { | |
0ecfe0b4 | 6272 | tree type, r; |
ae58fa02 MM |
6273 | |
6274 | if (t == NULL_TREE || t == error_mark_node | |
6275 | || t == integer_type_node | |
6276 | || t == void_type_node | |
6277 | || t == char_type_node | |
6278 | || TREE_CODE (t) == NAMESPACE_DECL) | |
6279 | return t; | |
6280 | ||
6281 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
6282 | type = IDENTIFIER_TYPE_VALUE (t); | |
6283 | else | |
6284 | type = TREE_TYPE (t); | |
6285 | if (type == unknown_type_node) | |
6286 | my_friendly_abort (42); | |
6287 | ||
6288 | if (type && TREE_CODE (t) != FUNCTION_DECL | |
6289 | && TREE_CODE (t) != TYPENAME_TYPE | |
6290 | && TREE_CODE (t) != TEMPLATE_DECL | |
4393e105 MM |
6291 | && TREE_CODE (t) != IDENTIFIER_NODE |
6292 | && TREE_CODE (t) != FUNCTION_TYPE | |
6293 | && TREE_CODE (t) != METHOD_TYPE) | |
6294 | type = tsubst (type, args, complain, in_decl); | |
6295 | if (type == error_mark_node) | |
6296 | return error_mark_node; | |
ae58fa02 MM |
6297 | |
6298 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd') | |
6299 | return tsubst_decl (t, args, type, in_decl); | |
6300 | ||
6301 | switch (TREE_CODE (t)) | |
6302 | { | |
6303 | case RECORD_TYPE: | |
6304 | case UNION_TYPE: | |
6305 | case ENUMERAL_TYPE: | |
4393e105 MM |
6306 | return tsubst_aggr_type (t, args, complain, in_decl, |
6307 | /*entering_scope=*/0); | |
ae58fa02 MM |
6308 | |
6309 | case ERROR_MARK: | |
6310 | case IDENTIFIER_NODE: | |
6311 | case OP_IDENTIFIER: | |
6312 | case VOID_TYPE: | |
6313 | case REAL_TYPE: | |
6314 | case COMPLEX_TYPE: | |
6315 | case BOOLEAN_TYPE: | |
6316 | case INTEGER_CST: | |
6317 | case REAL_CST: | |
6318 | case STRING_CST: | |
6319 | return t; | |
6320 | ||
6321 | case INTEGER_TYPE: | |
6322 | if (t == integer_type_node) | |
6323 | return t; | |
6324 | ||
6325 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
6326 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
6327 | return t; | |
d2e5ee5c | 6328 | |
5566b478 | 6329 | { |
ddce3528 | 6330 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 6331 | |
ddce3528 | 6332 | max = tsubst_expr (omax, args, complain, in_decl); |
4393e105 MM |
6333 | if (max == error_mark_node) |
6334 | return error_mark_node; | |
6335 | ||
8dd3f57a MM |
6336 | /* See if we can reduce this expression to something simpler. */ |
6337 | max = maybe_fold_nontype_arg (max); | |
6338 | if (!processing_template_decl && TREE_READONLY_DECL_P (max)) | |
6339 | max = decl_constant_value (max); | |
6340 | ||
856216bb MM |
6341 | if (processing_template_decl |
6342 | /* When providing explicit arguments to a template | |
6343 | function, but leaving some arguments for subsequent | |
6344 | deduction, MAX may be template-dependent even if we're | |
6345 | not PROCESSING_TEMPLATE_DECL. */ | |
6346 | || TREE_CODE (max) != INTEGER_CST) | |
ae58fa02 | 6347 | { |
8dd3f57a MM |
6348 | tree itype = make_node (INTEGER_TYPE); |
6349 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
6350 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, | |
6351 | integer_one_node); | |
6352 | return itype; | |
ae58fa02 MM |
6353 | } |
6354 | ||
ddce3528 JM |
6355 | if (integer_zerop (omax)) |
6356 | { | |
6357 | /* Still allow an explicit array of size zero. */ | |
6358 | if (pedantic) | |
6359 | pedwarn ("creating array with size zero"); | |
6360 | } | |
6361 | else if (integer_zerop (max) || INT_CST_LT (max, integer_zero_node)) | |
0ecfe0b4 | 6362 | { |
4393e105 MM |
6363 | /* [temp.deduct] |
6364 | ||
6365 | Type deduction may fail for any of the following | |
6366 | reasons: | |
6367 | ||
bf8f3f93 MM |
6368 | Attempting to create an array with a size that is |
6369 | zero or negative. */ | |
4393e105 MM |
6370 | if (complain) |
6371 | cp_error ("creating array with size `%E'", max); | |
6372 | ||
6373 | return error_mark_node; | |
0ecfe0b4 JM |
6374 | } |
6375 | ||
337c90cc | 6376 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node)); |
7ddedda4 MM |
6377 | if (!TREE_PERMANENT (max) && !allocation_temporary_p ()) |
6378 | max = copy_to_permanent (max); | |
120ecb8a | 6379 | return build_index_type (max); |
ae58fa02 MM |
6380 | } |
6381 | ||
6382 | case TEMPLATE_TYPE_PARM: | |
6383 | case TEMPLATE_TEMPLATE_PARM: | |
6384 | case TEMPLATE_PARM_INDEX: | |
6385 | { | |
6386 | int idx; | |
6387 | int level; | |
6388 | int levels; | |
0ecfe0b4 JM |
6389 | |
6390 | r = NULL_TREE; | |
ae58fa02 MM |
6391 | |
6392 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
6393 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6394 | { | |
6395 | idx = TEMPLATE_TYPE_IDX (t); | |
6396 | level = TEMPLATE_TYPE_LEVEL (t); | |
6397 | } | |
6398 | else | |
6399 | { | |
6400 | idx = TEMPLATE_PARM_IDX (t); | |
6401 | level = TEMPLATE_PARM_LEVEL (t); | |
6402 | } | |
6403 | ||
6404 | if (TREE_VEC_LENGTH (args) > 0) | |
6405 | { | |
6406 | tree arg = NULL_TREE; | |
6407 | ||
6408 | levels = TMPL_ARGS_DEPTH (args); | |
6409 | if (level <= levels) | |
6410 | arg = TMPL_ARG (args, level, idx); | |
6411 | ||
8b5b8b7c MM |
6412 | if (arg == error_mark_node) |
6413 | return error_mark_node; | |
6414 | else if (arg != NULL_TREE) | |
ae58fa02 MM |
6415 | { |
6416 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
6417 | { | |
6418 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg)) | |
6419 | == 't', 0); | |
adecb3f4 MM |
6420 | return cp_build_qualified_type_real |
6421 | (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t), | |
6422 | complain); | |
ae58fa02 MM |
6423 | } |
6424 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6425 | { | |
7ddedda4 | 6426 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 MM |
6427 | { |
6428 | /* We are processing a type constructed from | |
6429 | a template template parameter */ | |
7ddedda4 | 6430 | tree argvec = tsubst (TYPE_TI_ARGS (t), |
4393e105 MM |
6431 | args, complain, in_decl); |
6432 | if (argvec == error_mark_node) | |
6433 | return error_mark_node; | |
6434 | ||
ae58fa02 MM |
6435 | /* We can get a TEMPLATE_TEMPLATE_PARM here when |
6436 | we are resolving nested-types in the signature of | |
6437 | a member function templates. | |
6438 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
6439 | template to be instantiated. */ | |
6440 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
6441 | arg = TYPE_NAME (arg); | |
6442 | ||
6443 | r = lookup_template_class (DECL_NAME (arg), | |
6444 | argvec, in_decl, | |
6445 | DECL_CONTEXT (arg), | |
6446 | /*entering_scope=*/0); | |
adecb3f4 MM |
6447 | return cp_build_qualified_type_real (r, |
6448 | TYPE_QUALS (t), | |
6449 | complain); | |
ae58fa02 MM |
6450 | } |
6451 | else | |
6452 | /* We are processing a template argument list. */ | |
6453 | return arg; | |
6454 | } | |
6455 | else | |
6456 | return arg; | |
6457 | } | |
6458 | } | |
51924768 JM |
6459 | else |
6460 | my_friendly_abort (981018); | |
ae58fa02 MM |
6461 | |
6462 | if (level == 1) | |
6463 | /* This can happen during the attempted tsubst'ing in | |
6464 | unify. This means that we don't yet have any information | |
6465 | about the template parameter in question. */ | |
6466 | return t; | |
6467 | ||
6468 | /* If we get here, we must have been looking at a parm for a | |
6469 | more deeply nested template. Make a new version of this | |
6470 | template parameter, but with a lower level. */ | |
6471 | switch (TREE_CODE (t)) | |
6472 | { | |
6473 | case TEMPLATE_TYPE_PARM: | |
6474 | case TEMPLATE_TEMPLATE_PARM: | |
6475 | r = copy_node (t); | |
6476 | TEMPLATE_TYPE_PARM_INDEX (r) | |
6477 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
6478 | r, levels); | |
6479 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
6480 | TYPE_MAIN_VARIANT (r) = r; | |
6481 | TYPE_POINTER_TO (r) = NULL_TREE; | |
6482 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
6483 | ||
6484 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 6485 | && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 | 6486 | { |
4393e105 MM |
6487 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, |
6488 | complain, in_decl); | |
6489 | if (argvec == error_mark_node) | |
6490 | return error_mark_node; | |
6491 | ||
7ddedda4 | 6492 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
ae58fa02 MM |
6493 | = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE); |
6494 | } | |
6495 | break; | |
6496 | ||
6497 | case TEMPLATE_PARM_INDEX: | |
6498 | r = reduce_template_parm_level (t, type, levels); | |
6499 | break; | |
6500 | ||
6501 | default: | |
6502 | my_friendly_abort (0); | |
6503 | } | |
6504 | ||
5566b478 | 6505 | return r; |
ae58fa02 | 6506 | } |
5566b478 | 6507 | |
8d08fdba MS |
6508 | case TREE_LIST: |
6509 | { | |
6510 | tree purpose, value, chain, result; | |
8d08fdba MS |
6511 | |
6512 | if (t == void_list_node) | |
6513 | return t; | |
6514 | ||
8d08fdba MS |
6515 | purpose = TREE_PURPOSE (t); |
6516 | if (purpose) | |
4393e105 MM |
6517 | { |
6518 | purpose = tsubst (purpose, args, complain, in_decl); | |
6519 | if (purpose == error_mark_node) | |
6520 | return error_mark_node; | |
6521 | } | |
8d08fdba MS |
6522 | value = TREE_VALUE (t); |
6523 | if (value) | |
4393e105 MM |
6524 | { |
6525 | value = tsubst (value, args, complain, in_decl); | |
6526 | if (value == error_mark_node) | |
6527 | return error_mark_node; | |
6528 | } | |
8d08fdba MS |
6529 | chain = TREE_CHAIN (t); |
6530 | if (chain && chain != void_type_node) | |
4393e105 MM |
6531 | { |
6532 | chain = tsubst (chain, args, complain, in_decl); | |
6533 | if (chain == error_mark_node) | |
6534 | return error_mark_node; | |
6535 | } | |
8d08fdba MS |
6536 | if (purpose == TREE_PURPOSE (t) |
6537 | && value == TREE_VALUE (t) | |
6538 | && chain == TREE_CHAIN (t)) | |
6539 | return t; | |
51632249 | 6540 | result = hash_tree_cons (purpose, value, chain); |
8d08fdba MS |
6541 | TREE_PARMLIST (result) = TREE_PARMLIST (t); |
6542 | return result; | |
6543 | } | |
6544 | case TREE_VEC: | |
5566b478 MS |
6545 | if (type != NULL_TREE) |
6546 | { | |
1ceaad38 JM |
6547 | /* A binfo node. We always need to make a copy, of the node |
6548 | itself and of its BINFO_BASETYPES. */ | |
85b71cf2 | 6549 | |
5566b478 MS |
6550 | t = copy_node (t); |
6551 | ||
c7449f2b JM |
6552 | /* Make sure type isn't a typedef copy. */ |
6553 | type = BINFO_TYPE (TYPE_BINFO (type)); | |
6554 | ||
5566b478 | 6555 | TREE_TYPE (t) = complete_type (type); |
6633d636 MS |
6556 | if (IS_AGGR_TYPE (type)) |
6557 | { | |
6558 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
6559 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
6560 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
6561 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
6562 | } | |
5566b478 MS |
6563 | return t; |
6564 | } | |
85b71cf2 JM |
6565 | |
6566 | /* Otherwise, a vector of template arguments. */ | |
4393e105 | 6567 | return tsubst_template_arg_vector (t, args, complain); |
8d08fdba | 6568 | |
8d08fdba MS |
6569 | case POINTER_TYPE: |
6570 | case REFERENCE_TYPE: | |
6571 | { | |
8d08fdba | 6572 | enum tree_code code; |
79a7c7fa | 6573 | |
8d08fdba MS |
6574 | if (type == TREE_TYPE (t)) |
6575 | return t; | |
6576 | ||
6577 | code = TREE_CODE (t); | |
4393e105 MM |
6578 | |
6579 | ||
6580 | /* [temp.deduct] | |
6581 | ||
6582 | Type deduction may fail for any of the following | |
6583 | reasons: | |
6584 | ||
6585 | -- Attempting to create a pointer to reference type. | |
6586 | -- Attempting to create a reference to a reference type or | |
6587 | a reference to void. */ | |
0ecfe0b4 JM |
6588 | if (TREE_CODE (type) == REFERENCE_TYPE |
6589 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa JM |
6590 | { |
6591 | static int last_line = 0; | |
6592 | static char* last_file = 0; | |
6593 | ||
6594 | /* We keep track of the last time we issued this error | |
6595 | message to avoid spewing a ton of messages during a | |
6596 | single bad template instantiation. */ | |
4393e105 MM |
6597 | if (complain && (last_line != lineno || |
6598 | last_file != input_filename)) | |
79a7c7fa | 6599 | { |
0ecfe0b4 JM |
6600 | if (TREE_CODE (type) == VOID_TYPE) |
6601 | cp_error ("forming reference to void"); | |
6602 | else | |
6603 | cp_error ("forming %s to reference type `%T'", | |
6604 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
6605 | type); | |
79a7c7fa JM |
6606 | last_line = lineno; |
6607 | last_file = input_filename; | |
6608 | } | |
6609 | ||
4393e105 | 6610 | return error_mark_node; |
79a7c7fa JM |
6611 | } |
6612 | else if (code == POINTER_TYPE) | |
8d08fdba MS |
6613 | r = build_pointer_type (type); |
6614 | else | |
6615 | r = build_reference_type (type); | |
adecb3f4 | 6616 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 6617 | |
8d08fdba MS |
6618 | /* Will this ever be needed for TYPE_..._TO values? */ |
6619 | layout_type (r); | |
6620 | return r; | |
6621 | } | |
a4443a08 | 6622 | case OFFSET_TYPE: |
0ecfe0b4 | 6623 | { |
4393e105 MM |
6624 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
6625 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
6626 | { | |
6627 | /* [temp.deduct] | |
6628 | ||
6629 | Type deduction may fail for any of the following | |
6630 | reasons: | |
6631 | ||
6632 | -- Attempting to create "pointer to member of T" when T | |
6633 | is not a class type. */ | |
6634 | if (complain) | |
6635 | cp_error ("creating pointer to member of non-class type `%T'", | |
6636 | r); | |
6637 | return error_mark_node; | |
6638 | } | |
0ecfe0b4 JM |
6639 | return build_offset_type (r, type); |
6640 | } | |
8d08fdba MS |
6641 | case FUNCTION_TYPE: |
6642 | case METHOD_TYPE: | |
6643 | { | |
c11b6f21 | 6644 | tree fntype; |
4393e105 | 6645 | tree raises; |
8d08fdba | 6646 | |
4393e105 MM |
6647 | fntype = tsubst_function_type (t, args, complain, in_decl); |
6648 | if (fntype == error_mark_node) | |
6649 | return error_mark_node; | |
cabc336a MM |
6650 | |
6651 | /* Substitue the exception specification. */ | |
6652 | raises = TYPE_RAISES_EXCEPTIONS (t); | |
c11b6f21 MS |
6653 | if (raises) |
6654 | { | |
4cc1d462 NS |
6655 | tree list = NULL_TREE; |
6656 | ||
6657 | if (! TREE_VALUE (raises)) | |
6658 | list = raises; | |
6659 | else | |
6660 | for (; raises != NULL_TREE; raises = TREE_CHAIN (raises)) | |
6661 | { | |
6662 | tree spec = TREE_VALUE (raises); | |
6663 | ||
6664 | spec = tsubst (spec, args, complain, in_decl); | |
6665 | if (spec == error_mark_node) | |
6666 | return spec; | |
6667 | list = add_exception_specifier (list, spec, complain); | |
6668 | } | |
6669 | fntype = build_exception_variant (fntype, list); | |
c11b6f21 MS |
6670 | } |
6671 | return fntype; | |
8d08fdba MS |
6672 | } |
6673 | case ARRAY_TYPE: | |
6674 | { | |
4393e105 MM |
6675 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
6676 | if (domain == error_mark_node) | |
6677 | return error_mark_node; | |
6678 | ||
6679 | /* As an optimization, we avoid regenerating the array type if | |
6680 | it will obviously be the same as T. */ | |
8d08fdba MS |
6681 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
6682 | return t; | |
0ecfe0b4 | 6683 | |
4393e105 MM |
6684 | /* These checks should match the ones in grokdeclarator. |
6685 | ||
6686 | [temp.deduct] | |
6687 | ||
6688 | The deduction may fail for any of the following reasons: | |
6689 | ||
6690 | -- Attempting to create an array with an element type that | |
6691 | is void, a function type, or a reference type. */ | |
6692 | if (TREE_CODE (type) == VOID_TYPE | |
6693 | || TREE_CODE (type) == FUNCTION_TYPE | |
6694 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 6695 | { |
4393e105 MM |
6696 | if (complain) |
6697 | cp_error ("creating array of `%T'", type); | |
6698 | return error_mark_node; | |
0ecfe0b4 JM |
6699 | } |
6700 | ||
8d08fdba MS |
6701 | r = build_cplus_array_type (type, domain); |
6702 | return r; | |
6703 | } | |
6704 | ||
8d08fdba | 6705 | case PLUS_EXPR: |
5566b478 | 6706 | case MINUS_EXPR: |
4393e105 MM |
6707 | { |
6708 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6709 | in_decl); | |
6710 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, | |
6711 | in_decl); | |
6712 | ||
6713 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6714 | return error_mark_node; | |
6715 | ||
6716 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); | |
6717 | } | |
8d08fdba MS |
6718 | |
6719 | case NEGATE_EXPR: | |
6720 | case NOP_EXPR: | |
4393e105 MM |
6721 | { |
6722 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6723 | in_decl); | |
6724 | if (e == error_mark_node) | |
6725 | return error_mark_node; | |
6726 | ||
6727 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); | |
6728 | } | |
8d08fdba | 6729 | |
5566b478 MS |
6730 | case TYPENAME_TYPE: |
6731 | { | |
4393e105 MM |
6732 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
6733 | in_decl, /*entering_scope=*/1); | |
6734 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
6735 | complain, in_decl); | |
6736 | ||
6737 | if (ctx == error_mark_node || f == error_mark_node) | |
6738 | return error_mark_node; | |
ae58fa02 | 6739 | |
bf8f3f93 MM |
6740 | if (!IS_AGGR_TYPE (ctx)) |
6741 | { | |
6742 | if (complain) | |
6743 | cp_error ("`%T' is not a class, struct, or union type", | |
6744 | ctx); | |
6745 | return error_mark_node; | |
6746 | } | |
6747 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
6748 | { | |
6749 | /* Normally, make_typename_type does not require that the CTX | |
6750 | have complete type in order to allow things like: | |
ae58fa02 | 6751 | |
bf8f3f93 | 6752 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 6753 | |
bf8f3f93 MM |
6754 | But, such constructs have already been resolved by this |
6755 | point, so here CTX really should have complete type, unless | |
6756 | it's a partial instantiation. */ | |
4393e105 MM |
6757 | ctx = complete_type (ctx); |
6758 | if (!TYPE_SIZE (ctx)) | |
6759 | { | |
6760 | if (complain) | |
6761 | incomplete_type_error (NULL_TREE, ctx); | |
6762 | return error_mark_node; | |
6763 | } | |
6764 | } | |
ae58fa02 | 6765 | |
b2b7d40a | 6766 | f = make_typename_type (ctx, f); |
f0bcd168 MM |
6767 | if (f == error_mark_node) |
6768 | return f; | |
adecb3f4 MM |
6769 | return cp_build_qualified_type_real (f, |
6770 | CP_TYPE_QUALS (f) | |
6771 | | CP_TYPE_QUALS (t), | |
6772 | complain); | |
5566b478 MS |
6773 | } |
6774 | ||
6775 | case INDIRECT_REF: | |
4393e105 MM |
6776 | { |
6777 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6778 | in_decl); | |
6779 | if (e == error_mark_node) | |
6780 | return error_mark_node; | |
6781 | return make_pointer_declarator (type, e); | |
6782 | } | |
6783 | ||
5566b478 | 6784 | case ADDR_EXPR: |
4393e105 MM |
6785 | { |
6786 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6787 | in_decl); | |
6788 | if (e == error_mark_node) | |
6789 | return error_mark_node; | |
6790 | return make_reference_declarator (type, e); | |
6791 | } | |
5566b478 MS |
6792 | |
6793 | case ARRAY_REF: | |
4393e105 MM |
6794 | { |
6795 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6796 | in_decl); | |
6fb2c05a JM |
6797 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, |
6798 | in_decl); | |
4393e105 MM |
6799 | if (e1 == error_mark_node || e2 == error_mark_node) |
6800 | return error_mark_node; | |
6801 | ||
6802 | return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr); | |
6803 | } | |
5566b478 MS |
6804 | |
6805 | case CALL_EXPR: | |
4393e105 MM |
6806 | { |
6807 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6808 | in_decl); | |
43f887f9 MM |
6809 | tree e2 = (tsubst_call_declarator_parms |
6810 | (CALL_DECLARATOR_PARMS (t), args, complain, in_decl)); | |
6811 | tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args, | |
6812 | complain, in_decl); | |
4393e105 MM |
6813 | |
6814 | if (e1 == error_mark_node || e2 == error_mark_node | |
6815 | || e3 == error_mark_node) | |
6816 | return error_mark_node; | |
6817 | ||
43f887f9 | 6818 | return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3); |
4393e105 | 6819 | } |
5566b478 | 6820 | |
fc378698 | 6821 | case SCOPE_REF: |
4393e105 MM |
6822 | { |
6823 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6824 | in_decl); | |
6825 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
6826 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6827 | return error_mark_node; | |
6828 | ||
6829 | return build_parse_node (TREE_CODE (t), e1, e2); | |
6830 | } | |
fc378698 | 6831 | |
b894fc05 | 6832 | case TYPEOF_TYPE: |
4393e105 MM |
6833 | { |
6834 | tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, | |
6835 | in_decl); | |
6836 | if (e1 == error_mark_node) | |
6837 | return error_mark_node; | |
6838 | ||
6839 | return TREE_TYPE (e1); | |
6840 | } | |
b894fc05 | 6841 | |
8d08fdba | 6842 | default: |
5566b478 | 6843 | sorry ("use of `%s' in template", |
8d08fdba MS |
6844 | tree_code_name [(int) TREE_CODE (t)]); |
6845 | return error_mark_node; | |
6846 | } | |
6847 | } | |
6848 | ||
00d3396f JM |
6849 | /* Like tsubst, but deals with expressions. This function just replaces |
6850 | template parms; to finish processing the resultant expression, use | |
6851 | tsubst_expr. */ | |
6852 | ||
5566b478 | 6853 | tree |
4393e105 | 6854 | tsubst_copy (t, args, complain, in_decl) |
98c1c668 | 6855 | tree t, args; |
4393e105 | 6856 | int complain; |
5566b478 MS |
6857 | tree in_decl; |
6858 | { | |
6859 | enum tree_code code; | |
8452b1d3 | 6860 | tree r; |
8d08fdba | 6861 | |
5566b478 MS |
6862 | if (t == NULL_TREE || t == error_mark_node) |
6863 | return t; | |
6864 | ||
6865 | code = TREE_CODE (t); | |
b7484fbe | 6866 | |
5566b478 MS |
6867 | switch (code) |
6868 | { | |
6869 | case PARM_DECL: | |
a759e627 | 6870 | return do_identifier (DECL_NAME (t), 0, NULL_TREE); |
5566b478 MS |
6871 | |
6872 | case CONST_DECL: | |
ed44da02 MM |
6873 | { |
6874 | tree enum_type; | |
6875 | tree v; | |
6876 | ||
6877 | if (!DECL_CONTEXT (t)) | |
6878 | /* This is a global enumeration constant. */ | |
6879 | return t; | |
6880 | ||
6881 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 MM |
6882 | Consider: |
6883 | ||
6884 | template <int I> int f() { | |
6885 | enum E { a = I }; | |
6886 | struct S { void g() { E e = a; } }; | |
6887 | }; | |
6888 | ||
6889 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
6890 | clever enough to find f<7>::a. */ | |
ed44da02 | 6891 | enum_type |
4393e105 | 6892 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
ed44da02 MM |
6893 | /*entering_scope=*/0); |
6894 | ||
6895 | for (v = TYPE_VALUES (enum_type); | |
6896 | v != NULL_TREE; | |
6897 | v = TREE_CHAIN (v)) | |
6898 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
6899 | return TREE_VALUE (v); | |
6900 | ||
6901 | /* We didn't find the name. That should never happen; if | |
6902 | name-lookup found it during preliminary parsing, we | |
6903 | should find it again here during instantiation. */ | |
6904 | my_friendly_abort (0); | |
6905 | } | |
db04386f | 6906 | return t; |
ed44da02 | 6907 | |
5566b478 MS |
6908 | case FIELD_DECL: |
6909 | if (DECL_CONTEXT (t)) | |
6910 | { | |
0978790f | 6911 | tree ctx; |
0978790f | 6912 | |
4393e105 | 6913 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 6914 | /*entering_scope=*/1); |
0978790f | 6915 | if (ctx != DECL_CONTEXT (t)) |
5566b478 MS |
6916 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
6917 | } | |
6918 | return t; | |
6919 | ||
6920 | case VAR_DECL: | |
6921 | case FUNCTION_DECL: | |
6922 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
4393e105 | 6923 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
6924 | mark_used (t); |
6925 | return t; | |
6926 | ||
98c1c668 JM |
6927 | case TEMPLATE_DECL: |
6928 | if (is_member_template (t)) | |
4393e105 | 6929 | return tsubst (t, args, complain, in_decl); |
98c1c668 JM |
6930 | else |
6931 | return t; | |
6932 | ||
672476cb MM |
6933 | case LOOKUP_EXPR: |
6934 | { | |
6935 | /* We must tsbust into a LOOKUP_EXPR in case the names to | |
6936 | which it refers is a conversion operator; in that case the | |
6937 | name will change. We avoid making unnecessary copies, | |
6938 | however. */ | |
6939 | ||
4393e105 | 6940 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
672476cb MM |
6941 | |
6942 | if (id != TREE_OPERAND (t, 0)) | |
6943 | { | |
8452b1d3 | 6944 | r = build_nt (LOOKUP_EXPR, id); |
672476cb MM |
6945 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); |
6946 | t = r; | |
6947 | } | |
6948 | ||
6949 | return t; | |
6950 | } | |
6951 | ||
5566b478 MS |
6952 | case CAST_EXPR: |
6953 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
6954 | case CONST_CAST_EXPR: |
6955 | case STATIC_CAST_EXPR: | |
6956 | case DYNAMIC_CAST_EXPR: | |
51924768 | 6957 | case NOP_EXPR: |
5566b478 | 6958 | return build1 |
4393e105 MM |
6959 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6960 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
6961 | |
6962 | case INDIRECT_REF: | |
6963 | case PREDECREMENT_EXPR: | |
6964 | case PREINCREMENT_EXPR: | |
6965 | case POSTDECREMENT_EXPR: | |
6966 | case POSTINCREMENT_EXPR: | |
6967 | case NEGATE_EXPR: | |
6968 | case TRUTH_NOT_EXPR: | |
b87692e5 | 6969 | case BIT_NOT_EXPR: |
5566b478 MS |
6970 | case ADDR_EXPR: |
6971 | case CONVERT_EXPR: /* Unary + */ | |
6972 | case SIZEOF_EXPR: | |
abff8e06 | 6973 | case ALIGNOF_EXPR: |
5566b478 | 6974 | case ARROW_EXPR: |
fc378698 | 6975 | case THROW_EXPR: |
5156628f | 6976 | case TYPEID_EXPR: |
5566b478 | 6977 | return build1 |
6a629cac | 6978 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 6979 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 MS |
6980 | |
6981 | case PLUS_EXPR: | |
6982 | case MINUS_EXPR: | |
6983 | case MULT_EXPR: | |
6984 | case TRUNC_DIV_EXPR: | |
6985 | case CEIL_DIV_EXPR: | |
6986 | case FLOOR_DIV_EXPR: | |
6987 | case ROUND_DIV_EXPR: | |
6988 | case EXACT_DIV_EXPR: | |
6989 | case BIT_AND_EXPR: | |
6990 | case BIT_ANDTC_EXPR: | |
6991 | case BIT_IOR_EXPR: | |
6992 | case BIT_XOR_EXPR: | |
6993 | case TRUNC_MOD_EXPR: | |
6994 | case FLOOR_MOD_EXPR: | |
6995 | case TRUTH_ANDIF_EXPR: | |
6996 | case TRUTH_ORIF_EXPR: | |
6997 | case TRUTH_AND_EXPR: | |
6998 | case TRUTH_OR_EXPR: | |
6999 | case RSHIFT_EXPR: | |
7000 | case LSHIFT_EXPR: | |
7001 | case RROTATE_EXPR: | |
7002 | case LROTATE_EXPR: | |
7003 | case EQ_EXPR: | |
7004 | case NE_EXPR: | |
7005 | case MAX_EXPR: | |
7006 | case MIN_EXPR: | |
7007 | case LE_EXPR: | |
7008 | case GE_EXPR: | |
7009 | case LT_EXPR: | |
7010 | case GT_EXPR: | |
7011 | case COMPONENT_REF: | |
7012 | case ARRAY_REF: | |
7013 | case COMPOUND_EXPR: | |
7014 | case SCOPE_REF: | |
7015 | case DOTSTAR_EXPR: | |
7016 | case MEMBER_REF: | |
7017 | return build_nt | |
4393e105 MM |
7018 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7019 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
7020 | |
7021 | case CALL_EXPR: | |
7022 | { | |
7023 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 7024 | if (is_overloaded_fn (fn)) |
4393e105 | 7025 | fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl); |
5566b478 | 7026 | else |
f84b4be9 | 7027 | /* Sometimes FN is a LOOKUP_EXPR. */ |
4393e105 | 7028 | fn = tsubst_copy (fn, args, complain, in_decl); |
5566b478 | 7029 | return build_nt |
4393e105 MM |
7030 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
7031 | in_decl), | |
5566b478 MS |
7032 | NULL_TREE); |
7033 | } | |
7034 | ||
7035 | case METHOD_CALL_EXPR: | |
7036 | { | |
7037 | tree name = TREE_OPERAND (t, 0); | |
7038 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
7039 | { | |
4393e105 MM |
7040 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
7041 | complain, in_decl); | |
7bae46f4 | 7042 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
7043 | } |
7044 | else if (TREE_CODE (name) == SCOPE_REF | |
7045 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
7046 | { | |
4393e105 MM |
7047 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, |
7048 | complain, in_decl); | |
5566b478 | 7049 | name = TREE_OPERAND (name, 1); |
4393e105 MM |
7050 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
7051 | complain, in_decl); | |
11686454 | 7052 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
7053 | name = build_nt (SCOPE_REF, base, name); |
7054 | } | |
7055 | else | |
4393e105 | 7056 | name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
5566b478 | 7057 | return build_nt |
4393e105 MM |
7058 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, |
7059 | complain, in_decl), | |
7060 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), | |
5566b478 MS |
7061 | NULL_TREE); |
7062 | } | |
7063 | ||
558475f0 MM |
7064 | case STMT_EXPR: |
7065 | /* This processing should really occur in tsubst_expr, However, | |
7066 | tsubst_expr does not recurse into expressions, since it | |
7067 | assumes that there aren't any statements inside them. | |
7068 | Instead, it simply calls build_expr_from_tree. So, we need | |
7069 | to expand the STMT_EXPR here. */ | |
7070 | if (!processing_template_decl) | |
7071 | { | |
7072 | tree rtl_expr = begin_stmt_expr (); | |
7073 | tree block = tsubst_expr (STMT_EXPR_STMT (t), args, | |
7074 | complain, in_decl); | |
7075 | r = finish_stmt_expr (rtl_expr, block); | |
7076 | } | |
7077 | return r; | |
7078 | ||
5566b478 MS |
7079 | case COND_EXPR: |
7080 | case MODOP_EXPR: | |
40242ccf | 7081 | case PSEUDO_DTOR_EXPR: |
67da3287 | 7082 | { |
8452b1d3 | 7083 | r = build_nt |
4393e105 MM |
7084 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7085 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7086 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
67da3287 MM |
7087 | return r; |
7088 | } | |
5566b478 MS |
7089 | |
7090 | case NEW_EXPR: | |
7091 | { | |
8452b1d3 | 7092 | r = build_nt |
4393e105 MM |
7093 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7094 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7095 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
7096 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
7097 | return r; | |
7098 | } | |
7099 | ||
7100 | case DELETE_EXPR: | |
7101 | { | |
8452b1d3 | 7102 | r = build_nt |
4393e105 MM |
7103 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7104 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
7105 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
7106 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
7107 | return r; | |
7108 | } | |
7109 | ||
386b8a85 JM |
7110 | case TEMPLATE_ID_EXPR: |
7111 | { | |
00d3396f | 7112 | /* Substituted template arguments */ |
4393e105 MM |
7113 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
7114 | in_decl); | |
856216bb MM |
7115 | |
7116 | if (targs && TREE_CODE (targs) == TREE_LIST) | |
7117 | { | |
7118 | tree chain; | |
7119 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
7120 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
7121 | } | |
7122 | else if (targs) | |
7123 | { | |
7124 | int i; | |
7125 | for (i = 0; i < TREE_VEC_LENGTH (targs); ++i) | |
7126 | TREE_VEC_ELT (targs, i) | |
7127 | = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i)); | |
7128 | } | |
00d3396f JM |
7129 | |
7130 | return lookup_template_function | |
4393e105 | 7131 | (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); |
386b8a85 JM |
7132 | } |
7133 | ||
5566b478 MS |
7134 | case TREE_LIST: |
7135 | { | |
7136 | tree purpose, value, chain; | |
7137 | ||
7138 | if (t == void_list_node) | |
7139 | return t; | |
7140 | ||
7141 | purpose = TREE_PURPOSE (t); | |
7142 | if (purpose) | |
4393e105 | 7143 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
7144 | value = TREE_VALUE (t); |
7145 | if (value) | |
4393e105 | 7146 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
7147 | chain = TREE_CHAIN (t); |
7148 | if (chain && chain != void_type_node) | |
4393e105 | 7149 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
7150 | if (purpose == TREE_PURPOSE (t) |
7151 | && value == TREE_VALUE (t) | |
7152 | && chain == TREE_CHAIN (t)) | |
7153 | return t; | |
7154 | return tree_cons (purpose, value, chain); | |
7155 | } | |
7156 | ||
7157 | case RECORD_TYPE: | |
7158 | case UNION_TYPE: | |
7159 | case ENUMERAL_TYPE: | |
7160 | case INTEGER_TYPE: | |
7161 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 7162 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 7163 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
7164 | case POINTER_TYPE: |
7165 | case REFERENCE_TYPE: | |
7166 | case OFFSET_TYPE: | |
7167 | case FUNCTION_TYPE: | |
7168 | case METHOD_TYPE: | |
7169 | case ARRAY_TYPE: | |
7170 | case TYPENAME_TYPE: | |
f84b4be9 | 7171 | case TYPE_DECL: |
4393e105 | 7172 | return tsubst (t, args, complain, in_decl); |
5566b478 | 7173 | |
e92cc029 | 7174 | case IDENTIFIER_NODE: |
8e710788 BK |
7175 | if (IDENTIFIER_TYPENAME_P (t) |
7176 | /* Make sure it's not just a variable named `__opr', for instance, | |
7177 | which can occur in some existing code. */ | |
7178 | && TREE_TYPE (t)) | |
e92cc029 | 7179 | return build_typename_overload |
4393e105 | 7180 | (tsubst (TREE_TYPE (t), args, complain, in_decl)); |
e92cc029 MS |
7181 | else |
7182 | return t; | |
7183 | ||
5156628f | 7184 | case CONSTRUCTOR: |
8452b1d3 JM |
7185 | { |
7186 | r = build | |
4393e105 MM |
7187 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), |
7188 | NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, | |
7189 | complain, in_decl)); | |
8452b1d3 JM |
7190 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
7191 | return r; | |
7192 | } | |
5156628f | 7193 | |
5566b478 MS |
7194 | default: |
7195 | return t; | |
7196 | } | |
7197 | } | |
7198 | ||
558475f0 | 7199 | /* Like tsubst_copy, but also does semantic processing. */ |
00d3396f | 7200 | |
5566b478 | 7201 | tree |
4393e105 | 7202 | tsubst_expr (t, args, complain, in_decl) |
98c1c668 | 7203 | tree t, args; |
4393e105 | 7204 | int complain; |
5566b478 MS |
7205 | tree in_decl; |
7206 | { | |
558475f0 MM |
7207 | tree stmt; |
7208 | ||
5566b478 MS |
7209 | if (t == NULL_TREE || t == error_mark_node) |
7210 | return t; | |
7211 | ||
5156628f | 7212 | if (processing_template_decl) |
4393e105 | 7213 | return tsubst_copy (t, args, complain, in_decl); |
5566b478 MS |
7214 | |
7215 | switch (TREE_CODE (t)) | |
8d08fdba | 7216 | { |
558475f0 MM |
7217 | case RETURN_INIT: |
7218 | finish_named_return_value | |
7219 | (TREE_OPERAND (t, 0), | |
7220 | tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, in_decl)); | |
7221 | tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7222 | break; | |
7223 | ||
7224 | case CTOR_INITIALIZER: | |
7225 | current_member_init_list | |
7226 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
7227 | current_base_init_list | |
7228 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
7229 | setup_vtbl_ptr (); | |
7230 | tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7231 | break; | |
7232 | ||
5566b478 | 7233 | case RETURN_STMT: |
f71f87f9 | 7234 | lineno = STMT_LINENO (t); |
ad321293 | 7235 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
4393e105 | 7236 | args, complain, in_decl)); |
5566b478 MS |
7237 | break; |
7238 | ||
7239 | case EXPR_STMT: | |
f71f87f9 | 7240 | lineno = STMT_LINENO (t); |
ad321293 | 7241 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
4393e105 | 7242 | args, complain, in_decl)); |
5566b478 MS |
7243 | break; |
7244 | ||
7245 | case DECL_STMT: | |
7246 | { | |
7247 | int i = suspend_momentary (); | |
e0942dcd MM |
7248 | tree decl; |
7249 | tree init; | |
5566b478 | 7250 | |
f71f87f9 | 7251 | lineno = STMT_LINENO (t); |
e0942dcd MM |
7252 | decl = DECL_STMT_DECL (t); |
7253 | init = DECL_INITIAL (decl); | |
7254 | decl = tsubst (decl, args, complain, in_decl); | |
7255 | init = tsubst_expr (init, args, complain, in_decl); | |
7256 | DECL_INITIAL (decl) = init; | |
558475f0 MM |
7257 | /* By marking the declaration as instantiated, we avoid trying |
7258 | to instantiate it. Since instantiate_decl can't handle | |
7259 | local variables, and since we've already done all that | |
7260 | needs to be done, that's the right thing to do. */ | |
e0942dcd MM |
7261 | if (TREE_CODE (decl) == VAR_DECL) |
7262 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
558475f0 MM |
7263 | maybe_push_decl (decl); |
7264 | add_decl_stmt (decl); | |
5566b478 | 7265 | resume_momentary (i); |
e0942dcd | 7266 | return decl; |
5566b478 | 7267 | } |
8d08fdba | 7268 | |
5566b478 MS |
7269 | case FOR_STMT: |
7270 | { | |
7271 | tree tmp; | |
f71f87f9 | 7272 | lineno = STMT_LINENO (t); |
5566b478 | 7273 | |
558475f0 | 7274 | stmt = begin_for_stmt (); |
ad321293 | 7275 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) |
4393e105 | 7276 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7277 | finish_for_init_stmt (stmt); |
ad321293 | 7278 | finish_for_cond (tsubst_expr (FOR_COND (t), args, |
4393e105 | 7279 | complain, in_decl), |
558475f0 | 7280 | stmt); |
4393e105 | 7281 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); |
558475f0 | 7282 | finish_for_expr (tmp, stmt); |
4393e105 | 7283 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); |
558475f0 | 7284 | finish_for_stmt (tmp, stmt); |
5566b478 MS |
7285 | } |
7286 | break; | |
8d08fdba | 7287 | |
5566b478 MS |
7288 | case WHILE_STMT: |
7289 | { | |
f71f87f9 | 7290 | lineno = STMT_LINENO (t); |
558475f0 | 7291 | stmt = begin_while_stmt (); |
ad321293 | 7292 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), |
4393e105 | 7293 | args, complain, in_decl), |
558475f0 | 7294 | stmt); |
4393e105 | 7295 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); |
558475f0 | 7296 | finish_while_stmt (stmt); |
5566b478 MS |
7297 | } |
7298 | break; | |
8d08fdba | 7299 | |
5566b478 MS |
7300 | case DO_STMT: |
7301 | { | |
f71f87f9 | 7302 | lineno = STMT_LINENO (t); |
558475f0 | 7303 | stmt = begin_do_stmt (); |
4393e105 | 7304 | tsubst_expr (DO_BODY (t), args, complain, in_decl); |
558475f0 | 7305 | finish_do_body (stmt); |
ad321293 | 7306 | finish_do_stmt (tsubst_expr (DO_COND (t), args, |
4393e105 | 7307 | complain, in_decl), |
558475f0 | 7308 | stmt); |
5566b478 MS |
7309 | } |
7310 | break; | |
a0a33927 | 7311 | |
5566b478 | 7312 | case IF_STMT: |
8d08fdba | 7313 | { |
5566b478 | 7314 | tree tmp; |
5566b478 | 7315 | |
f71f87f9 | 7316 | lineno = STMT_LINENO (t); |
558475f0 | 7317 | stmt = begin_if_stmt (); |
ad321293 | 7318 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), |
4393e105 | 7319 | args, complain, in_decl), |
558475f0 | 7320 | stmt); |
db5ae43f | 7321 | |
ad321293 | 7322 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 7323 | { |
4393e105 | 7324 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7325 | finish_then_clause (stmt); |
db5ae43f MS |
7326 | } |
7327 | ||
ad321293 MM |
7328 | if (tmp = ELSE_CLAUSE (t), tmp) |
7329 | { | |
7330 | begin_else_clause (); | |
4393e105 | 7331 | tsubst_expr (tmp, args, complain, in_decl); |
558475f0 | 7332 | finish_else_clause (stmt); |
ad321293 | 7333 | } |
8d08fdba | 7334 | |
ad321293 | 7335 | finish_if_stmt (); |
8d08fdba | 7336 | } |
5566b478 | 7337 | break; |
8d08fdba | 7338 | |
5566b478 MS |
7339 | case COMPOUND_STMT: |
7340 | { | |
ad321293 | 7341 | tree substmt; |
8d08fdba | 7342 | |
f71f87f9 | 7343 | lineno = STMT_LINENO (t); |
558475f0 | 7344 | stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
ad321293 MM |
7345 | for (substmt = COMPOUND_BODY (t); |
7346 | substmt != NULL_TREE; | |
7347 | substmt = TREE_CHAIN (substmt)) | |
4393e105 | 7348 | tsubst_expr (substmt, args, complain, in_decl); |
558475f0 | 7349 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt); |
5566b478 MS |
7350 | } |
7351 | break; | |
8d08fdba | 7352 | |
5566b478 | 7353 | case BREAK_STMT: |
f71f87f9 | 7354 | lineno = STMT_LINENO (t); |
ad321293 | 7355 | finish_break_stmt (); |
5566b478 | 7356 | break; |
8d08fdba | 7357 | |
6467930b | 7358 | case CONTINUE_STMT: |
f71f87f9 | 7359 | lineno = STMT_LINENO (t); |
ad321293 | 7360 | finish_continue_stmt (); |
6467930b MS |
7361 | break; |
7362 | ||
5566b478 MS |
7363 | case SWITCH_STMT: |
7364 | { | |
558475f0 | 7365 | tree val; |
5566b478 | 7366 | |
f71f87f9 | 7367 | lineno = STMT_LINENO (t); |
ad321293 | 7368 | begin_switch_stmt (); |
4393e105 | 7369 | val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); |
558475f0 MM |
7370 | stmt = finish_switch_cond (val); |
7371 | tsubst_expr (SWITCH_BODY (t), args, complain, in_decl); | |
7372 | finish_switch_stmt (val, stmt); | |
5566b478 MS |
7373 | } |
7374 | break; | |
7375 | ||
7376 | case CASE_LABEL: | |
4393e105 MM |
7377 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), |
7378 | tsubst_expr (CASE_HIGH (t), args, complain, in_decl)); | |
5566b478 MS |
7379 | break; |
7380 | ||
7381 | case LABEL_DECL: | |
f01b0acb MM |
7382 | lineno = DECL_SOURCE_LINE (t); |
7383 | input_filename = DECL_SOURCE_FILE (t); | |
7384 | finish_label_stmt (DECL_NAME (t)); | |
5566b478 MS |
7385 | break; |
7386 | ||
7387 | case GOTO_STMT: | |
f71f87f9 | 7388 | lineno = STMT_LINENO (t); |
aa09da44 MM |
7389 | t = GOTO_DESTINATION (t); |
7390 | if (TREE_CODE (t) != IDENTIFIER_NODE) | |
7391 | /* Computed goto's must be tsubst'd into. On the other hand, | |
7392 | non-computed gotos must not be; the identifier in question | |
7393 | will have no binding. */ | |
4393e105 | 7394 | t = tsubst_expr (t, args, complain, in_decl); |
aa09da44 | 7395 | finish_goto_stmt (t); |
ad321293 MM |
7396 | break; |
7397 | ||
7398 | case ASM_STMT: | |
f71f87f9 | 7399 | lineno = STMT_LINENO (t); |
3ebc5c52 | 7400 | finish_asm_stmt (ASM_CV_QUAL (t), |
4393e105 MM |
7401 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), |
7402 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
7403 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
7404 | tsubst_expr (ASM_CLOBBERS (t), args, complain, | |
7405 | in_decl)); | |
5566b478 | 7406 | break; |
faf5394a MS |
7407 | |
7408 | case TRY_BLOCK: | |
f71f87f9 | 7409 | lineno = STMT_LINENO (t); |
558475f0 | 7410 | stmt = begin_try_block (); |
4393e105 | 7411 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); |
558475f0 | 7412 | finish_try_block (stmt); |
faf5394a | 7413 | { |
ad321293 | 7414 | tree handler = TRY_HANDLERS (t); |
faf5394a | 7415 | for (; handler; handler = TREE_CHAIN (handler)) |
4393e105 | 7416 | tsubst_expr (handler, args, complain, in_decl); |
faf5394a | 7417 | } |
558475f0 | 7418 | finish_handler_sequence (stmt); |
faf5394a MS |
7419 | break; |
7420 | ||
7421 | case HANDLER: | |
f71f87f9 | 7422 | lineno = STMT_LINENO (t); |
558475f0 | 7423 | stmt = begin_handler (); |
ad321293 | 7424 | if (HANDLER_PARMS (t)) |
faf5394a | 7425 | { |
ad321293 | 7426 | tree d = HANDLER_PARMS (t); |
faf5394a | 7427 | expand_start_catch_block |
4393e105 MM |
7428 | (tsubst (TREE_OPERAND (d, 1), args, complain, in_decl), |
7429 | tsubst (TREE_OPERAND (d, 0), args, complain, in_decl)); | |
faf5394a MS |
7430 | } |
7431 | else | |
7432 | expand_start_catch_block (NULL_TREE, NULL_TREE); | |
558475f0 | 7433 | finish_handler_parms (stmt); |
4393e105 | 7434 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); |
558475f0 | 7435 | finish_handler (stmt); |
faf5394a MS |
7436 | break; |
7437 | ||
b87692e5 | 7438 | case TAG_DEFN: |
f71f87f9 | 7439 | lineno = STMT_LINENO (t); |
b87692e5 MS |
7440 | t = TREE_TYPE (t); |
7441 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
4393e105 | 7442 | tsubst (t, args, complain, NULL_TREE); |
b87692e5 MS |
7443 | break; |
7444 | ||
5566b478 | 7445 | default: |
4393e105 | 7446 | return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl)); |
5566b478 MS |
7447 | } |
7448 | return NULL_TREE; | |
8d08fdba MS |
7449 | } |
7450 | ||
6ba89f8e | 7451 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
7452 | the template arguments in TARG_PTR. */ |
7453 | ||
5566b478 MS |
7454 | tree |
7455 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 7456 | tree tmpl, targ_ptr; |
8d08fdba | 7457 | { |
5566b478 | 7458 | tree fndecl; |
36a117a5 MM |
7459 | tree gen_tmpl; |
7460 | tree spec; | |
5566b478 MS |
7461 | int i, len; |
7462 | struct obstack *old_fmp_obstack; | |
7463 | extern struct obstack *function_maybepermanent_obstack; | |
36a117a5 | 7464 | tree inner_args; |
5566b478 | 7465 | |
27fafc8d JM |
7466 | if (tmpl == error_mark_node) |
7467 | return error_mark_node; | |
7468 | ||
386b8a85 JM |
7469 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
7470 | ||
36a117a5 MM |
7471 | /* Check to see if we already have this specialization. */ |
7472 | spec = retrieve_specialization (tmpl, targ_ptr); | |
7473 | if (spec != NULL_TREE) | |
7474 | return spec; | |
7475 | ||
7476 | if (DECL_TEMPLATE_INFO (tmpl)) | |
386b8a85 | 7477 | { |
36a117a5 MM |
7478 | /* The TMPL is a partial instantiation. To get a full set of |
7479 | arguments we must add the arguments used to perform the | |
7480 | partial instantiation. */ | |
7481 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
7482 | targ_ptr); | |
7483 | gen_tmpl = most_general_template (tmpl); | |
7484 | ||
7485 | /* Check to see if we already have this specialization. */ | |
7486 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
75650646 MM |
7487 | if (spec != NULL_TREE) |
7488 | return spec; | |
386b8a85 | 7489 | } |
36a117a5 MM |
7490 | else |
7491 | gen_tmpl = tmpl; | |
386b8a85 | 7492 | |
5566b478 MS |
7493 | push_obstacks (&permanent_obstack, &permanent_obstack); |
7494 | old_fmp_obstack = function_maybepermanent_obstack; | |
7495 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 7496 | |
36a117a5 MM |
7497 | len = DECL_NTPARMS (gen_tmpl); |
7498 | inner_args = innermost_args (targ_ptr); | |
5566b478 MS |
7499 | i = len; |
7500 | while (i--) | |
8d08fdba | 7501 | { |
36a117a5 | 7502 | tree t = TREE_VEC_ELT (inner_args, i); |
5566b478 MS |
7503 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
7504 | { | |
7505 | tree nt = target_type (t); | |
ec255269 | 7506 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 | 7507 | { |
8251199e JM |
7508 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); |
7509 | cp_error (" trying to instantiate `%D'", gen_tmpl); | |
5566b478 MS |
7510 | fndecl = error_mark_node; |
7511 | goto out; | |
7512 | } | |
7513 | } | |
8d08fdba | 7514 | } |
e66d884e | 7515 | targ_ptr = copy_to_permanent (targ_ptr); |
8d08fdba | 7516 | |
5566b478 | 7517 | /* substitute template parameters */ |
4393e105 | 7518 | fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, /*complain=*/1, gen_tmpl); |
36a117a5 MM |
7519 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
7520 | template, not the most general template. */ | |
7521 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 7522 | |
824b9a4c MS |
7523 | if (flag_external_templates) |
7524 | add_pending_template (fndecl); | |
7525 | ||
5566b478 MS |
7526 | out: |
7527 | function_maybepermanent_obstack = old_fmp_obstack; | |
7528 | pop_obstacks (); | |
8d08fdba | 7529 | |
5566b478 | 7530 | return fndecl; |
8d08fdba | 7531 | } |
5566b478 MS |
7532 | |
7533 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
7534 | |
7535 | void | |
5566b478 MS |
7536 | overload_template_name (type) |
7537 | tree type; | |
8d08fdba | 7538 | { |
5566b478 MS |
7539 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
7540 | tree decl; | |
8d08fdba | 7541 | |
5566b478 MS |
7542 | if (IDENTIFIER_CLASS_VALUE (id) |
7543 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
7544 | return; | |
8d08fdba | 7545 | |
5566b478 MS |
7546 | decl = build_decl (TYPE_DECL, id, type); |
7547 | SET_DECL_ARTIFICIAL (decl); | |
7548 | pushdecl_class_level (decl); | |
8d08fdba MS |
7549 | } |
7550 | ||
4393e105 MM |
7551 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
7552 | arguments that are being used when calling it. TARGS is a vector | |
7553 | into which the deduced template arguments are placed. | |
8d08fdba MS |
7554 | |
7555 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
7556 | all the types, and 1 for complete failure. An error message will be | |
7557 | printed only for an incomplete match. | |
7558 | ||
4393e105 MM |
7559 | If FN is a conversion operator, RETURN_TYPE is the type desired as |
7560 | the result of the conversion operator. | |
8d08fdba | 7561 | |
4393e105 | 7562 | TPARMS is a vector of template parameters. |
9f54c803 MM |
7563 | |
7564 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
7565 | template-id. | |
6467930b | 7566 | |
830bfa74 MM |
7567 | The parameter STRICT is one of: |
7568 | ||
7569 | DEDUCE_CALL: | |
7570 | We are deducing arguments for a function call, as in | |
7571 | [temp.deduct.call]. | |
7572 | ||
7573 | DEDUCE_CONV: | |
7574 | We are deducing arguments for a conversion function, as in | |
7575 | [temp.deduct.conv]. | |
7576 | ||
7577 | DEDUCE_EXACT: | |
7578 | We are deducing arguments when calculating the partial | |
7579 | ordering between specializations of function or class | |
7580 | templates, as in [temp.func.order] and [temp.class.order], | |
7581 | when doing an explicit instantiation as in [temp.explicit], | |
9f54c803 | 7582 | when determining an explicit specialization as in |
830bfa74 | 7583 | [temp.expl.spec], or when taking the address of a function |
4393e105 MM |
7584 | template, as in [temp.deduct.funcaddr]. |
7585 | ||
4393e105 | 7586 | The other arguments are as for type_unification. */ |
8d08fdba MS |
7587 | |
7588 | int | |
4393e105 | 7589 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
03017874 | 7590 | strict) |
4393e105 | 7591 | tree fn, explicit_targs, targs, args, return_type; |
830bfa74 | 7592 | unification_kind_t strict; |
386b8a85 | 7593 | { |
4393e105 MM |
7594 | tree parms; |
7595 | tree fntype; | |
adecb3f4 | 7596 | int result; |
386b8a85 | 7597 | |
4393e105 MM |
7598 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); |
7599 | ||
7600 | fntype = TREE_TYPE (fn); | |
7601 | if (explicit_targs) | |
75650646 | 7602 | { |
4393e105 MM |
7603 | /* [temp.deduct] |
7604 | ||
7605 | The specified template arguments must match the template | |
7606 | parameters in kind (i.e., type, nontype, template), and there | |
7607 | must not be more arguments than there are parameters; | |
7608 | otherwise type deduction fails. | |
7609 | ||
7610 | Nontype arguments must match the types of the corresponding | |
7611 | nontype template parameters, or must be convertible to the | |
7612 | types of the corresponding nontype parameters as specified in | |
7613 | _temp.arg.nontype_, otherwise type deduction fails. | |
7614 | ||
7615 | All references in the function type of the function template | |
7616 | to the corresponding template parameters are replaced by the | |
7617 | specified template argument values. If a substitution in a | |
7618 | template parameter or in the function type of the function | |
7619 | template results in an invalid type, type deduction fails. */ | |
7620 | int i; | |
7621 | tree converted_args; | |
75650646 | 7622 | |
4393e105 MM |
7623 | converted_args |
7624 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
7625 | explicit_targs, NULL_TREE, /*complain=*/0, | |
7626 | /*require_all_arguments=*/0)); | |
7627 | if (converted_args == error_mark_node) | |
75650646 | 7628 | return 1; |
386b8a85 | 7629 | |
4393e105 MM |
7630 | fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE); |
7631 | if (fntype == error_mark_node) | |
7632 | return 1; | |
050367a3 | 7633 | |
4393e105 MM |
7634 | /* Place the explicitly specified arguments in TARGS. */ |
7635 | for (i = 0; i < TREE_VEC_LENGTH (targs); i++) | |
7636 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); | |
75650646 | 7637 | } |
4393e105 MM |
7638 | |
7639 | parms = TYPE_ARG_TYPES (fntype); | |
386b8a85 | 7640 | |
4393e105 MM |
7641 | if (DECL_CONV_FN_P (fn)) |
7642 | { | |
7643 | /* This is a template conversion operator. Use the return types | |
edef8288 JM |
7644 | as well as the argument types. We use it instead of 'this', since |
7645 | we could be comparing conversions from different classes. */ | |
4393e105 | 7646 | parms = scratch_tree_cons (NULL_TREE, TREE_TYPE (fntype), |
edef8288 JM |
7647 | TREE_CHAIN (parms)); |
7648 | args = scratch_tree_cons (NULL_TREE, return_type, TREE_CHAIN (args)); | |
4393e105 MM |
7649 | } |
7650 | ||
4393e105 MM |
7651 | /* We allow incomplete unification without an error message here |
7652 | because the standard doesn't seem to explicitly prohibit it. Our | |
7653 | callers must be ready to deal with unification failures in any | |
7654 | event. */ | |
adecb3f4 MM |
7655 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
7656 | targs, parms, args, /*subr=*/0, | |
7657 | strict, /*allow_incomplete*/1); | |
7658 | ||
7659 | if (result == 0) | |
7660 | /* All is well so far. Now, check: | |
7661 | ||
7662 | [temp.deduct] | |
7663 | ||
7664 | When all template arguments have been deduced, all uses of | |
7665 | template parameters in nondeduced contexts are replaced with | |
7666 | the corresponding deduced argument values. If the | |
7667 | substitution results in an invalid type, as described above, | |
7668 | type deduction fails. */ | |
7669 | if (tsubst (TREE_TYPE (fn), targs, /*complain=*/0, NULL_TREE) | |
7670 | == error_mark_node) | |
7671 | return 1; | |
7672 | ||
7673 | return result; | |
830bfa74 MM |
7674 | } |
7675 | ||
7676 | /* Adjust types before performing type deduction, as described in | |
7677 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
7678 | sections are symmetric. PARM is the type of a function parameter |
7679 | or the return type of the conversion function. ARG is the type of | |
7680 | the argument passed to the call, or the type of the value | |
7681 | intialized with the result of the conversion function. */ | |
386b8a85 | 7682 | |
e9659ab0 | 7683 | static void |
830bfa74 MM |
7684 | maybe_adjust_types_for_deduction (strict, parm, arg) |
7685 | unification_kind_t strict; | |
7686 | tree* parm; | |
7687 | tree* arg; | |
7688 | { | |
7689 | switch (strict) | |
7690 | { | |
7691 | case DEDUCE_CALL: | |
7692 | break; | |
7693 | ||
7694 | case DEDUCE_CONV: | |
7695 | { | |
4c7d0dff MM |
7696 | /* Swap PARM and ARG throughout the remainder of this |
7697 | function; the handling is precisely symmetric since PARM | |
7698 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
7699 | tree* temp = parm; |
7700 | parm = arg; | |
7701 | arg = temp; | |
7702 | break; | |
7703 | } | |
7704 | ||
7705 | case DEDUCE_EXACT: | |
7706 | /* There is nothing to do in this case. */ | |
7707 | return; | |
7708 | ||
7709 | default: | |
7710 | my_friendly_abort (0); | |
7711 | } | |
7712 | ||
7713 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
7714 | { | |
7715 | /* [temp.deduct.call] | |
7716 | ||
7717 | If P is not a reference type: | |
7718 | ||
7719 | --If A is an array type, the pointer type produced by the | |
7720 | array-to-pointer standard conversion (_conv.array_) is | |
7721 | used in place of A for type deduction; otherwise, | |
7722 | ||
7723 | --If A is a function type, the pointer type produced by | |
7724 | the function-to-pointer standard conversion | |
7725 | (_conv.func_) is used in place of A for type deduction; | |
7726 | otherwise, | |
7727 | ||
7728 | --If A is a cv-qualified type, the top level | |
7729 | cv-qualifiers of A's type are ignored for type | |
7730 | deduction. */ | |
7731 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
7732 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 7733 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
7734 | *arg = build_pointer_type (*arg); |
7735 | else | |
7736 | *arg = TYPE_MAIN_VARIANT (*arg); | |
7737 | } | |
7738 | ||
7739 | /* [temp.deduct.call] | |
7740 | ||
7741 | If P is a cv-qualified type, the top level cv-qualifiers | |
7742 | of P's type are ignored for type deduction. If P is a | |
7743 | reference type, the type referred to by P is used for | |
7744 | type deduction. */ | |
7745 | *parm = TYPE_MAIN_VARIANT (*parm); | |
7746 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
7747 | *parm = TREE_TYPE (*parm); | |
386b8a85 JM |
7748 | } |
7749 | ||
4393e105 | 7750 | /* Like type_unfication. |
9f54c803 MM |
7751 | |
7752 | If SUBR is 1, we're being called recursively (to unify the | |
7753 | arguments of a function or method parameter of a function | |
7754 | template). */ | |
386b8a85 | 7755 | |
4966381a | 7756 | static int |
3b3ba9f0 | 7757 | type_unification_real (tparms, targs, parms, args, subr, |
4393e105 | 7758 | strict, allow_incomplete) |
050367a3 | 7759 | tree tparms, targs, parms, args; |
830bfa74 MM |
7760 | int subr; |
7761 | unification_kind_t strict; | |
7762 | int allow_incomplete; | |
8d08fdba MS |
7763 | { |
7764 | tree parm, arg; | |
7765 | int i; | |
7766 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 7767 | int sub_strict; |
8d08fdba MS |
7768 | |
7769 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
7770 | my_friendly_assert (parms == NULL_TREE |
7771 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 7772 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
7773 | build_x_function_call). */ |
7774 | if (args) | |
7775 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
7776 | my_friendly_assert (ntparms > 0, 292); | |
7777 | ||
830bfa74 MM |
7778 | switch (strict) |
7779 | { | |
7780 | case DEDUCE_CALL: | |
7781 | sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED; | |
7782 | break; | |
7783 | ||
7784 | case DEDUCE_CONV: | |
7785 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
7786 | break; | |
7787 | ||
7788 | case DEDUCE_EXACT: | |
7789 | sub_strict = UNIFY_ALLOW_NONE; | |
7790 | break; | |
7791 | ||
7792 | default: | |
7793 | my_friendly_abort (0); | |
7794 | } | |
7795 | ||
8d08fdba MS |
7796 | while (parms |
7797 | && parms != void_list_node | |
7798 | && args | |
7799 | && args != void_list_node) | |
7800 | { | |
7801 | parm = TREE_VALUE (parms); | |
7802 | parms = TREE_CHAIN (parms); | |
7803 | arg = TREE_VALUE (args); | |
7804 | args = TREE_CHAIN (args); | |
7805 | ||
7806 | if (arg == error_mark_node) | |
7807 | return 1; | |
7808 | if (arg == unknown_type_node) | |
34016c81 JM |
7809 | /* We can't deduce anything from this, but we might get all the |
7810 | template args from other function args. */ | |
7811 | continue; | |
b7484fbe | 7812 | |
03e70705 JM |
7813 | /* Conversions will be performed on a function argument that |
7814 | corresponds with a function parameter that contains only | |
7815 | non-deducible template parameters and explicitly specified | |
7816 | template parameters. */ | |
7817 | if (! uses_template_parms (parm)) | |
b7484fbe | 7818 | { |
03e70705 JM |
7819 | tree type; |
7820 | ||
7821 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
7822 | type = TREE_TYPE (arg); | |
7823 | else | |
7824 | { | |
7825 | type = arg; | |
7826 | arg = NULL_TREE; | |
7827 | } | |
7828 | ||
830bfa74 | 7829 | if (strict == DEDUCE_EXACT) |
03e70705 | 7830 | { |
3bfdc719 | 7831 | if (same_type_p (parm, type)) |
03e70705 JM |
7832 | continue; |
7833 | } | |
03e70705 | 7834 | else |
343c89cd JM |
7835 | /* It might work; we shouldn't check now, because we might |
7836 | get into infinite recursion. Overload resolution will | |
7837 | handle it. */ | |
7838 | continue; | |
03e70705 | 7839 | |
b7484fbe MS |
7840 | return 1; |
7841 | } | |
7842 | ||
8d08fdba MS |
7843 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') |
7844 | { | |
7845 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
34016c81 | 7846 | if (type_unknown_p (arg)) |
28cbf42c | 7847 | { |
34016c81 JM |
7848 | /* [temp.deduct.type] A template-argument can be deduced from |
7849 | a pointer to function or pointer to member function | |
7850 | argument if the set of overloaded functions does not | |
7851 | contain function templates and at most one of a set of | |
7852 | overloaded functions provides a unique match. */ | |
7853 | ||
7854 | if (resolve_overloaded_unification | |
4393e105 | 7855 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
7856 | != 0) |
7857 | return 1; | |
7858 | continue; | |
28cbf42c | 7859 | } |
8d08fdba MS |
7860 | arg = TREE_TYPE (arg); |
7861 | } | |
4393e105 | 7862 | |
830bfa74 MM |
7863 | if (!subr) |
7864 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
db5ae43f | 7865 | |
4393e105 | 7866 | switch (unify (tparms, targs, parm, arg, sub_strict)) |
8d08fdba MS |
7867 | { |
7868 | case 0: | |
7869 | break; | |
7870 | case 1: | |
7871 | return 1; | |
7872 | } | |
7873 | } | |
7874 | /* Fail if we've reached the end of the parm list, and more args | |
7875 | are present, and the parm list isn't variadic. */ | |
7876 | if (args && args != void_list_node && parms == void_list_node) | |
7877 | return 1; | |
7878 | /* Fail if parms are left and they don't have default values. */ | |
7879 | if (parms | |
7880 | && parms != void_list_node | |
7881 | && TREE_PURPOSE (parms) == NULL_TREE) | |
7882 | return 1; | |
7883 | if (!subr) | |
7884 | for (i = 0; i < ntparms; i++) | |
050367a3 | 7885 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 7886 | { |
386b8a85 | 7887 | if (!allow_incomplete) |
8251199e | 7888 | error ("incomplete type unification"); |
8d08fdba MS |
7889 | return 2; |
7890 | } | |
7891 | return 0; | |
7892 | } | |
7893 | ||
34016c81 JM |
7894 | /* Subroutine of type_unification_real. Args are like the variables at the |
7895 | call site. ARG is an overloaded function (or template-id); we try | |
7896 | deducing template args from each of the overloads, and if only one | |
7897 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
7898 | ||
7899 | static int | |
7900 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
4393e105 | 7901 | sub_strict) |
34016c81 JM |
7902 | tree tparms, targs, parm, arg; |
7903 | unification_kind_t strict; | |
7904 | int sub_strict; | |
34016c81 JM |
7905 | { |
7906 | tree tempargs = copy_node (targs); | |
7907 | int good = 0; | |
7908 | ||
7909 | if (TREE_CODE (arg) == ADDR_EXPR) | |
7910 | arg = TREE_OPERAND (arg, 0); | |
9f3d9e46 | 7911 | |
d8f8dca1 MM |
7912 | if (TREE_CODE (arg) == COMPONENT_REF) |
7913 | /* Handle `&x' where `x' is some static or non-static member | |
7914 | function name. */ | |
7915 | arg = TREE_OPERAND (arg, 1); | |
7916 | ||
05e0b2f4 JM |
7917 | if (TREE_CODE (arg) == OFFSET_REF) |
7918 | arg = TREE_OPERAND (arg, 1); | |
7919 | ||
9f3d9e46 JM |
7920 | /* Strip baselink information. */ |
7921 | while (TREE_CODE (arg) == TREE_LIST) | |
7922 | arg = TREE_VALUE (arg); | |
7923 | ||
34016c81 JM |
7924 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
7925 | { | |
7926 | /* If we got some explicit template args, we need to plug them into | |
7927 | the affected templates before we try to unify, in case the | |
7928 | explicit args will completely resolve the templates in question. */ | |
7929 | ||
7930 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
7931 | arg = TREE_OPERAND (arg, 0); | |
7932 | ||
7933 | for (; arg; arg = OVL_NEXT (arg)) | |
7934 | { | |
7935 | tree fn = OVL_CURRENT (arg); | |
7936 | tree subargs, elem; | |
7937 | ||
7938 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
7939 | continue; | |
7940 | ||
7941 | subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs); | |
7942 | if (subargs) | |
7943 | { | |
4393e105 MM |
7944 | elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0, |
7945 | NULL_TREE); | |
d8f8dca1 MM |
7946 | if (TREE_CODE (elem) == METHOD_TYPE) |
7947 | elem = build_ptrmemfunc_type (build_pointer_type (elem)); | |
e97e5263 | 7948 | good += try_one_overload (tparms, targs, tempargs, parm, elem, |
4393e105 | 7949 | strict, sub_strict); |
34016c81 JM |
7950 | } |
7951 | } | |
7952 | } | |
7953 | else if (TREE_CODE (arg) == OVERLOAD) | |
7954 | { | |
7955 | for (; arg; arg = OVL_NEXT (arg)) | |
d8f8dca1 MM |
7956 | { |
7957 | tree type = TREE_TYPE (OVL_CURRENT (arg)); | |
7958 | if (TREE_CODE (type) == METHOD_TYPE) | |
7959 | type = build_ptrmemfunc_type (build_pointer_type (type)); | |
7960 | good += try_one_overload (tparms, targs, tempargs, parm, | |
7961 | type, | |
4393e105 | 7962 | strict, sub_strict); |
d8f8dca1 | 7963 | } |
34016c81 JM |
7964 | } |
7965 | else | |
7966 | my_friendly_abort (981006); | |
7967 | ||
7968 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7969 | to function or pointer to member function argument if the set of | |
7970 | overloaded functions does not contain function templates and at most | |
7971 | one of a set of overloaded functions provides a unique match. | |
7972 | ||
7973 | So if we found multiple possibilities, we return success but don't | |
7974 | deduce anything. */ | |
7975 | ||
7976 | if (good == 1) | |
7977 | { | |
7978 | int i = TREE_VEC_LENGTH (targs); | |
7979 | for (; i--; ) | |
7980 | if (TREE_VEC_ELT (tempargs, i)) | |
7981 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
7982 | } | |
7983 | if (good) | |
7984 | return 0; | |
7985 | ||
7986 | return 1; | |
7987 | } | |
7988 | ||
7989 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
7990 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
7991 | different overloads deduce different arguments for a given parm. | |
7992 | Returns 1 on success. */ | |
7993 | ||
7994 | static int | |
e97e5263 | 7995 | try_one_overload (tparms, orig_targs, targs, parm, arg, strict, |
4393e105 | 7996 | sub_strict) |
e97e5263 | 7997 | tree tparms, orig_targs, targs, parm, arg; |
34016c81 JM |
7998 | unification_kind_t strict; |
7999 | int sub_strict; | |
34016c81 JM |
8000 | { |
8001 | int nargs; | |
8002 | tree tempargs; | |
8003 | int i; | |
8004 | ||
8005 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
8006 | to function or pointer to member function argument if the set of | |
8007 | overloaded functions does not contain function templates and at most | |
8008 | one of a set of overloaded functions provides a unique match. | |
8009 | ||
8010 | So if this is a template, just return success. */ | |
8011 | ||
8012 | if (uses_template_parms (arg)) | |
8013 | return 1; | |
8014 | ||
8015 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
8016 | ||
8017 | /* We don't copy orig_targs for this because if we have already deduced | |
8018 | some template args from previous args, unify would complain when we | |
8019 | try to deduce a template parameter for the same argument, even though | |
8020 | there isn't really a conflict. */ | |
8021 | nargs = TREE_VEC_LENGTH (targs); | |
8022 | tempargs = make_scratch_vec (nargs); | |
8023 | ||
4393e105 | 8024 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
8025 | return 0; |
8026 | ||
8027 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 8028 | explicitly specified args. */ |
34016c81 JM |
8029 | for (i = nargs; i--; ) |
8030 | { | |
8031 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 8032 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 JM |
8033 | |
8034 | if (elt == NULL_TREE) | |
8035 | continue; | |
8036 | else if (uses_template_parms (elt)) | |
8037 | { | |
8038 | /* Since we're unifying against ourselves, we will fill in template | |
8039 | args used in the function parm list with our own template parms. | |
8040 | Discard them. */ | |
8041 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
8042 | continue; | |
8043 | } | |
8044 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
8045 | return 0; | |
8046 | } | |
8047 | ||
8048 | for (i = nargs; i--; ) | |
8049 | { | |
8050 | tree elt = TREE_VEC_ELT (tempargs, i); | |
8051 | ||
8052 | if (elt) | |
8053 | TREE_VEC_ELT (targs, i) = elt; | |
8054 | } | |
8055 | ||
8056 | return 1; | |
8057 | } | |
8058 | ||
4393e105 MM |
8059 | /* PARM is a template class (perhaps with unbound template |
8060 | parameters). ARG is a fully instantiated type. If ARG can be | |
8061 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
8062 | TARGS are as for unify. */ | |
fcfb9f96 MM |
8063 | |
8064 | static tree | |
4393e105 | 8065 | try_class_unification (tparms, targs, parm, arg) |
fcfb9f96 MM |
8066 | tree tparms; |
8067 | tree targs; | |
4393e105 MM |
8068 | tree parm; |
8069 | tree arg; | |
8070 | { | |
8071 | int i; | |
8072 | tree copy_of_targs; | |
8073 | ||
8074 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
8075 | || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) | |
8076 | return NULL_TREE; | |
8077 | ||
8078 | /* We need to make a new template argument vector for the call to | |
8079 | unify. If we used TARGS, we'd clutter it up with the result of | |
8080 | the attempted unification, even if this class didn't work out. | |
8081 | We also don't want to commit ourselves to all the unifications | |
8082 | we've already done, since unification is supposed to be done on | |
8083 | an argument-by-argument basis. In other words, consider the | |
8084 | following pathological case: | |
8085 | ||
8086 | template <int I, int J, int K> | |
8087 | struct S {}; | |
8088 | ||
8089 | template <int I, int J> | |
8090 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
8091 | ||
8092 | template <int I, int J, int K> | |
8093 | void f(S<I, J, K>, S<I, I, I>); | |
8094 | ||
8095 | void g() { | |
8096 | S<0, 0, 0> s0; | |
8097 | S<0, 1, 2> s2; | |
8098 | ||
8099 | f(s0, s2); | |
8100 | } | |
8101 | ||
8102 | Now, by the time we consider the unification involving `s2', we | |
8103 | already know that we must have `f<0, 0, 0>'. But, even though | |
8104 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal | |
8105 | because there are two ways to unify base classes of S<0, 1, 2> | |
8106 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
8107 | would reject the possibility I=1. */ | |
8108 | push_momentary (); | |
8109 | copy_of_targs = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
8110 | i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), | |
8111 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE); | |
8112 | pop_momentary (); | |
8113 | ||
8114 | /* If unification failed, we're done. */ | |
8115 | if (i != 0) | |
8116 | return NULL_TREE; | |
8117 | else | |
8118 | return arg; | |
8119 | } | |
8120 | ||
8121 | /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we | |
8122 | have alreay discovered to be satisfactory. ARG_BINFO is the binfo | |
8123 | for the base class of ARG that we are currently examining. */ | |
8124 | ||
8125 | static tree | |
8126 | get_template_base_recursive (tparms, targs, parm, | |
8127 | arg_binfo, rval, flags) | |
8128 | tree tparms; | |
8129 | tree targs; | |
8130 | tree arg_binfo; | |
fcfb9f96 | 8131 | tree rval; |
4393e105 MM |
8132 | tree parm; |
8133 | int flags; | |
fcfb9f96 MM |
8134 | { |
8135 | tree binfos; | |
8136 | int i, n_baselinks; | |
4393e105 | 8137 | tree arg = BINFO_TYPE (arg_binfo); |
fcfb9f96 | 8138 | |
4393e105 | 8139 | if (!(flags & GTB_IGNORE_TYPE)) |
fcfb9f96 | 8140 | { |
4393e105 MM |
8141 | tree r = try_class_unification (tparms, targs, |
8142 | parm, arg); | |
fcfb9f96 | 8143 | |
4393e105 | 8144 | /* If there is more than one satisfactory baseclass, then: |
fcfb9f96 | 8145 | |
4393e105 MM |
8146 | [temp.deduct.call] |
8147 | ||
8148 | If they yield more than one possible deduced A, the type | |
8149 | deduction fails. | |
8150 | ||
8151 | applies. */ | |
8152 | if (r && rval && !same_type_p (r, rval)) | |
8153 | return error_mark_node; | |
8154 | else if (r) | |
8155 | rval = r; | |
fcfb9f96 MM |
8156 | } |
8157 | ||
4393e105 | 8158 | binfos = BINFO_BASETYPES (arg_binfo); |
fcfb9f96 MM |
8159 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; |
8160 | ||
8161 | /* Process base types. */ | |
8162 | for (i = 0; i < n_baselinks; i++) | |
8163 | { | |
8164 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
4393e105 | 8165 | int this_virtual; |
fcfb9f96 | 8166 | |
4393e105 MM |
8167 | /* Skip this base, if we've already seen it. */ |
8168 | if (BINFO_MARKED (base_binfo)) | |
8169 | continue; | |
8170 | ||
8171 | this_virtual = | |
8172 | (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); | |
8173 | ||
8174 | /* When searching for a non-virtual, we cannot mark virtually | |
8175 | found binfos. */ | |
8176 | if (! this_virtual) | |
8177 | SET_BINFO_MARKED (base_binfo); | |
8178 | ||
8179 | rval = get_template_base_recursive (tparms, targs, | |
8180 | parm, | |
8181 | base_binfo, | |
8182 | rval, | |
8183 | GTB_VIA_VIRTUAL * this_virtual); | |
8184 | ||
8185 | /* If we discovered more than one matching base class, we can | |
8186 | stop now. */ | |
8187 | if (rval == error_mark_node) | |
8188 | return error_mark_node; | |
fcfb9f96 MM |
8189 | } |
8190 | ||
8191 | return rval; | |
8192 | } | |
8193 | ||
4393e105 MM |
8194 | /* Given a template type PARM and a class type ARG, find the unique |
8195 | base type in ARG that is an instance of PARM. We do not examine | |
8196 | ARG itself; only its base-classes. If there is no appropriate base | |
8197 | class, return NULL_TREE. If there is more than one, return | |
8198 | error_mark_node. PARM may be the type of a partial specialization, | |
8199 | as well as a plain template type. Used by unify. */ | |
fcfb9f96 MM |
8200 | |
8201 | static tree | |
4393e105 | 8202 | get_template_base (tparms, targs, parm, arg) |
fcfb9f96 MM |
8203 | tree tparms; |
8204 | tree targs; | |
4393e105 MM |
8205 | tree parm; |
8206 | tree arg; | |
fcfb9f96 | 8207 | { |
4393e105 MM |
8208 | tree rval; |
8209 | tree arg_binfo; | |
fcfb9f96 | 8210 | |
4393e105 MM |
8211 | my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); |
8212 | ||
8213 | arg_binfo = TYPE_BINFO (complete_type (arg)); | |
8214 | rval = get_template_base_recursive (tparms, targs, | |
8215 | parm, arg_binfo, | |
8216 | NULL_TREE, | |
8217 | GTB_IGNORE_TYPE); | |
fcfb9f96 | 8218 | |
4393e105 MM |
8219 | /* Since get_template_base_recursive marks the bases classes, we |
8220 | must unmark them here. */ | |
d6479fe7 | 8221 | dfs_walk (arg_binfo, dfs_unmark, markedp, 0); |
fcfb9f96 MM |
8222 | |
8223 | return rval; | |
8224 | } | |
8225 | ||
db2767b6 MM |
8226 | /* Returns the level of DECL, which declares a template parameter. */ |
8227 | ||
e9659ab0 | 8228 | static int |
db2767b6 MM |
8229 | template_decl_level (decl) |
8230 | tree decl; | |
8231 | { | |
8232 | switch (TREE_CODE (decl)) | |
8233 | { | |
8234 | case TYPE_DECL: | |
8235 | case TEMPLATE_DECL: | |
8236 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
8237 | ||
8238 | case PARM_DECL: | |
8239 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
8240 | ||
8241 | default: | |
8242 | my_friendly_abort (0); | |
5ffe581d | 8243 | return 0; |
db2767b6 MM |
8244 | } |
8245 | } | |
8246 | ||
830bfa74 MM |
8247 | /* Decide whether ARG can be unified with PARM, considering only the |
8248 | cv-qualifiers of each type, given STRICT as documented for unify. | |
8249 | Returns non-zero iff the unification is OK on that basis.*/ | |
e92cc029 | 8250 | |
e9659ab0 | 8251 | static int |
830bfa74 MM |
8252 | check_cv_quals_for_unify (strict, arg, parm) |
8253 | int strict; | |
8254 | tree arg; | |
8255 | tree parm; | |
8256 | { | |
ef637255 MM |
8257 | if (!(strict & UNIFY_ALLOW_MORE_CV_QUAL) |
8258 | && !at_least_as_qualified_p (arg, parm)) | |
8259 | return 0; | |
8260 | ||
8261 | if (!(strict & UNIFY_ALLOW_LESS_CV_QUAL) | |
8262 | && !at_least_as_qualified_p (parm, arg)) | |
8263 | return 0; | |
8264 | ||
ef637255 | 8265 | return 1; |
830bfa74 MM |
8266 | } |
8267 | ||
8268 | /* Takes parameters as for type_unification. Returns 0 if the | |
8269 | type deduction suceeds, 1 otherwise. The parameter STRICT is a | |
8270 | bitwise or of the following flags: | |
8271 | ||
8272 | UNIFY_ALLOW_NONE: | |
8273 | Require an exact match between PARM and ARG. | |
8274 | UNIFY_ALLOW_MORE_CV_QUAL: | |
8275 | Allow the deduced ARG to be more cv-qualified than ARG. | |
8276 | UNIFY_ALLOW_LESS_CV_QUAL: | |
8277 | Allow the deduced ARG to be less cv-qualified than ARG. | |
8278 | UNIFY_ALLOW_DERIVED: | |
8279 | Allow the deduced ARG to be a template base class of ARG, | |
8280 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
8281 | ARG. |
8282 | UNIFY_ALLOW_INTEGER: | |
8283 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
8284 | case for more information. */ | |
830bfa74 | 8285 | |
e9659ab0 | 8286 | static int |
4393e105 | 8287 | unify (tparms, targs, parm, arg, strict) |
050367a3 | 8288 | tree tparms, targs, parm, arg; |
830bfa74 | 8289 | int strict; |
8d08fdba MS |
8290 | { |
8291 | int idx; | |
050367a3 | 8292 | tree targ; |
db2767b6 | 8293 | tree tparm; |
8d08fdba MS |
8294 | |
8295 | /* I don't think this will do the right thing with respect to types. | |
8296 | But the only case I've seen it in so far has been array bounds, where | |
8297 | signedness is the only information lost, and I think that will be | |
8298 | okay. */ | |
8299 | while (TREE_CODE (parm) == NOP_EXPR) | |
8300 | parm = TREE_OPERAND (parm, 0); | |
8301 | ||
8302 | if (arg == error_mark_node) | |
8303 | return 1; | |
8304 | if (arg == unknown_type_node) | |
34016c81 JM |
8305 | /* We can't deduce anything from this, but we might get all the |
8306 | template args from other function args. */ | |
8307 | return 0; | |
8308 | ||
db2767b6 | 8309 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 8310 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
8311 | template parameters. We might need them if we're trying to |
8312 | figure out which of two things is more specialized. */ | |
8313 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
8314 | return 0; |
8315 | ||
830bfa74 MM |
8316 | /* Immediately reject some pairs that won't unify because of |
8317 | cv-qualification mismatches. */ | |
8318 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
49432171 | 8319 | && TREE_CODE_CLASS (TREE_CODE (arg)) == 't' |
830bfa74 MM |
8320 | /* We check the cv-qualifiers when unifying with template type |
8321 | parameters below. We want to allow ARG `const T' to unify with | |
8322 | PARM `T' for example, when computing which of two templates | |
8323 | is more specialized, for example. */ | |
8324 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
8325 | && !check_cv_quals_for_unify (strict, arg, parm)) | |
49432171 JM |
8326 | return 1; |
8327 | ||
8d08fdba MS |
8328 | switch (TREE_CODE (parm)) |
8329 | { | |
2ca340ae JM |
8330 | case TYPENAME_TYPE: |
8331 | /* In a type which contains a nested-name-specifier, template | |
8332 | argument values cannot be deduced for template parameters used | |
8333 | within the nested-name-specifier. */ | |
8334 | return 0; | |
8335 | ||
8d08fdba | 8336 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 8337 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
8338 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8339 | ||
8340 | if (TEMPLATE_TYPE_LEVEL (parm) | |
8341 | != template_decl_level (tparm)) | |
8342 | /* The PARM is not one we're trying to unify. Just check | |
8343 | to see if it matches ARG. */ | |
8344 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 8345 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 8346 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 8347 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8348 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 8349 | |
73b0fce8 | 8350 | /* Check for mixed types and values. */ |
db2767b6 MM |
8351 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
8352 | && TREE_CODE (tparm) != TYPE_DECL) | |
8353 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
8354 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
8355 | return 1; |
8356 | ||
db2767b6 | 8357 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 8358 | { |
7ddedda4 | 8359 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)) |
db2767b6 MM |
8360 | { |
8361 | /* We arrive here when PARM does not involve template | |
8362 | specialization. */ | |
73b0fce8 | 8363 | |
db2767b6 MM |
8364 | /* ARG must be constructed from a template class. */ |
8365 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
8366 | return 1; | |
73b0fce8 | 8367 | |
db2767b6 | 8368 | { |
7ddedda4 MM |
8369 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); |
8370 | tree parmvec = TYPE_TI_ARGS (parm); | |
db2767b6 MM |
8371 | tree argvec = CLASSTYPE_TI_ARGS (arg); |
8372 | tree argtmplvec | |
8373 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
8374 | int i; | |
8375 | ||
8376 | /* The parameter and argument roles have to be switched here | |
8377 | in order to handle default arguments properly. For example, | |
8378 | template<template <class> class TT> void f(TT<int>) | |
8379 | should be able to accept vector<int> which comes from | |
744fac59 | 8380 | template <class T, class Allocator = allocator> |
73b0fce8 KL |
8381 | class vector. */ |
8382 | ||
fcfcdfc8 | 8383 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) |
db2767b6 MM |
8384 | == error_mark_node) |
8385 | return 1; | |
73b0fce8 | 8386 | |
6b9b6b15 JM |
8387 | /* Deduce arguments T, i from TT<T> or TT<i>. |
8388 | We check each element of PARMVEC and ARGVEC individually | |
8389 | rather than the whole TREE_VEC since they can have | |
8390 | different number of elements. */ | |
8391 | ||
db2767b6 MM |
8392 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
8393 | { | |
8394 | tree t = TREE_VEC_ELT (parmvec, i); | |
73b0fce8 | 8395 | |
830bfa74 MM |
8396 | if (unify (tparms, targs, t, |
8397 | TREE_VEC_ELT (argvec, i), | |
4393e105 | 8398 | UNIFY_ALLOW_NONE)) |
db2767b6 MM |
8399 | return 1; |
8400 | } | |
73b0fce8 | 8401 | } |
db2767b6 MM |
8402 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
8403 | } | |
8404 | } | |
8405 | else | |
8406 | { | |
830bfa74 MM |
8407 | /* If PARM is `const T' and ARG is only `int', we don't have |
8408 | a match unless we are allowing additional qualification. | |
8409 | If ARG is `const int' and PARM is just `T' that's OK; | |
8410 | that binds `const int' to `T'. */ | |
8411 | if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, | |
8412 | arg, parm)) | |
db2767b6 MM |
8413 | return 1; |
8414 | ||
830bfa74 MM |
8415 | /* Consider the case where ARG is `const volatile int' and |
8416 | PARM is `const T'. Then, T should be `volatile int'. */ | |
8417 | arg = | |
adecb3f4 MM |
8418 | cp_build_qualified_type_real (arg, |
8419 | CP_TYPE_QUALS (arg) | |
8420 | & ~CP_TYPE_QUALS (parm), | |
8421 | /*complain=*/0); | |
8422 | if (arg == error_mark_node) | |
8423 | return 1; | |
73b0fce8 KL |
8424 | } |
8425 | ||
8426 | /* Simple cases: Value already set, does match or doesn't. */ | |
4393e105 | 8427 | if (targ != NULL_TREE && same_type_p (targ, arg)) |
73b0fce8 | 8428 | return 0; |
050367a3 | 8429 | else if (targ) |
73b0fce8 | 8430 | return 1; |
61cd552e MM |
8431 | |
8432 | /* Make sure that ARG is not a variable-sized array. (Note that | |
8433 | were talking about variable-sized arrays (like `int[n]'), | |
8434 | rather than arrays of unknown size (like `int[]').) We'll | |
8435 | get very confused by such a type since the bound of the array | |
8436 | will not be computable in an instantiation. Besides, such | |
8437 | types are not allowed in ISO C++, so we can do as we please | |
8438 | here. */ | |
8439 | if (TREE_CODE (arg) == ARRAY_TYPE | |
8440 | && !uses_template_parms (arg) | |
8441 | && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg))) | |
8442 | != INTEGER_CST)) | |
8443 | return 1; | |
8444 | ||
050367a3 | 8445 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
8446 | return 0; |
8447 | ||
f84b4be9 | 8448 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
8449 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8450 | ||
8451 | if (TEMPLATE_PARM_LEVEL (parm) | |
8452 | != template_decl_level (tparm)) | |
8453 | /* The PARM is not one we're trying to unify. Just check | |
8454 | to see if it matches ARG. */ | |
8455 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
67ffc812 | 8456 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; |
db2767b6 | 8457 | |
f84b4be9 | 8458 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 8459 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8460 | |
050367a3 | 8461 | if (targ) |
8d08fdba | 8462 | { |
67ffc812 | 8463 | int i = (cp_tree_equal (targ, arg) > 0); |
312e7d50 JM |
8464 | if (i == 1) |
8465 | return 0; | |
8466 | else if (i == 0) | |
8467 | return 1; | |
8468 | else | |
8469 | my_friendly_abort (42); | |
8d08fdba | 8470 | } |
8d08fdba | 8471 | |
161c12b0 JM |
8472 | /* [temp.deduct.type] If, in the declaration of a function template |
8473 | with a non-type template-parameter, the non-type | |
8474 | template-parameter is used in an expression in the function | |
8475 | parameter-list and, if the corresponding template-argument is | |
8476 | deduced, the template-argument type shall match the type of the | |
8477 | template-parameter exactly, except that a template-argument | |
8478 | deduced from an array bound may be of any integral type. */ | |
8479 | if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm))) | |
8480 | /* OK */; | |
8481 | else if ((strict & UNIFY_ALLOW_INTEGER) | |
8482 | && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE | |
8483 | || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE)) | |
8484 | /* OK */; | |
8485 | else | |
8486 | return 1; | |
8487 | ||
050367a3 | 8488 | TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg); |
8d08fdba MS |
8489 | return 0; |
8490 | ||
8491 | case POINTER_TYPE: | |
830bfa74 MM |
8492 | { |
8493 | int sub_strict; | |
4ac14744 | 8494 | |
830bfa74 MM |
8495 | if (TREE_CODE (arg) != POINTER_TYPE) |
8496 | return 1; | |
8497 | ||
8498 | /* [temp.deduct.call] | |
8499 | ||
8500 | A can be another pointer or pointer to member type that can | |
8501 | be converted to the deduced A via a qualification | |
8502 | conversion (_conv.qual_). | |
8503 | ||
8504 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
8505 | This will allow for additional cv-qualification of the | |
8506 | pointed-to types if appropriate. In general, this is a bit | |
8507 | too generous; we are only supposed to allow qualification | |
8508 | conversions and this method will allow an ARG of char** and | |
8509 | a deduced ARG of const char**. However, overload | |
8510 | resolution will subsequently invalidate the candidate, so | |
8511 | this is probably OK. */ | |
8512 | sub_strict = strict; | |
8513 | ||
a7a64a77 | 8514 | if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE) |
830bfa74 MM |
8515 | /* The derived-to-base conversion only persists through one |
8516 | level of pointers. */ | |
8517 | sub_strict &= ~UNIFY_ALLOW_DERIVED; | |
8518 | ||
a7a64a77 MM |
8519 | return unify (tparms, targs, TREE_TYPE (parm), |
8520 | TREE_TYPE (arg), sub_strict); | |
830bfa74 | 8521 | } |
8d08fdba MS |
8522 | |
8523 | case REFERENCE_TYPE: | |
830bfa74 MM |
8524 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
8525 | return 1; | |
8526 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
4393e105 | 8527 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8528 | |
8529 | case ARRAY_TYPE: | |
8530 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
8531 | return 1; | |
3042d5be MM |
8532 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
8533 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
8534 | return 1; | |
8535 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
830bfa74 | 8536 | && unify (tparms, targs, TYPE_DOMAIN (parm), |
4393e105 | 8537 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) |
8d08fdba | 8538 | return 1; |
830bfa74 | 8539 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8540 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8541 | |
8542 | case REAL_TYPE: | |
37c46b43 | 8543 | case COMPLEX_TYPE: |
8d08fdba | 8544 | case INTEGER_TYPE: |
42976354 | 8545 | case BOOLEAN_TYPE: |
5ad5a526 | 8546 | case VOID_TYPE: |
f376e137 MS |
8547 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8548 | return 1; | |
8549 | ||
514a1f18 JM |
8550 | if (TREE_CODE (parm) == INTEGER_TYPE |
8551 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
8d08fdba MS |
8552 | { |
8553 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
830bfa74 | 8554 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), |
4393e105 | 8555 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8556 | return 1; |
8557 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
830bfa74 | 8558 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), |
4393e105 | 8559 | TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8560 | return 1; |
8561 | } | |
514a1f18 JM |
8562 | /* We use the TYPE_MAIN_VARIANT since we have already |
8563 | checked cv-qualification at the top of the | |
8564 | function. */ | |
3bfdc719 MM |
8565 | else if (!same_type_p (TYPE_MAIN_VARIANT (arg), |
8566 | TYPE_MAIN_VARIANT (parm))) | |
ca79f85d JM |
8567 | return 1; |
8568 | ||
8d08fdba MS |
8569 | /* As far as unification is concerned, this wins. Later checks |
8570 | will invalidate it if necessary. */ | |
8571 | return 0; | |
8572 | ||
8573 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 8574 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 8575 | case INTEGER_CST: |
bd6dd845 MS |
8576 | while (TREE_CODE (arg) == NOP_EXPR) |
8577 | arg = TREE_OPERAND (arg, 0); | |
8578 | ||
8d08fdba MS |
8579 | if (TREE_CODE (arg) != INTEGER_CST) |
8580 | return 1; | |
8581 | return !tree_int_cst_equal (parm, arg); | |
8582 | ||
8d08fdba MS |
8583 | case TREE_VEC: |
8584 | { | |
8585 | int i; | |
8586 | if (TREE_CODE (arg) != TREE_VEC) | |
8587 | return 1; | |
8588 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
8589 | return 1; | |
8590 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
830bfa74 | 8591 | if (unify (tparms, targs, |
8d08fdba | 8592 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 8593 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
8594 | return 1; |
8595 | return 0; | |
8596 | } | |
8597 | ||
8d08fdba | 8598 | case RECORD_TYPE: |
f181d4ae | 8599 | case UNION_TYPE: |
f181d4ae | 8600 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 8601 | return 1; |
830bfa74 | 8602 | |
a7a64a77 MM |
8603 | if (TYPE_PTRMEMFUNC_P (parm)) |
8604 | { | |
8605 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
8606 | return 1; | |
8607 | ||
8608 | return unify (tparms, targs, | |
8609 | TYPE_PTRMEMFUNC_FN_TYPE (parm), | |
8610 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
8611 | strict); | |
8612 | } | |
8613 | ||
5db698f6 | 8614 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 8615 | { |
6467930b | 8616 | tree t = NULL_TREE; |
4393e105 | 8617 | |
830bfa74 | 8618 | if (strict & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
8619 | { |
8620 | /* First, we try to unify the PARM and ARG directly. */ | |
8621 | t = try_class_unification (tparms, targs, | |
8622 | parm, arg); | |
8623 | ||
8624 | if (!t) | |
8625 | { | |
8626 | /* Fallback to the special case allowed in | |
8627 | [temp.deduct.call]: | |
8628 | ||
8629 | If P is a class, and P has the form | |
8630 | template-id, then A can be a derived class of | |
8631 | the deduced A. Likewise, if P is a pointer to | |
8632 | a class of the form template-id, A can be a | |
8633 | pointer to a derived class pointed to by the | |
8634 | deduced A. */ | |
8635 | t = get_template_base (tparms, targs, | |
8636 | parm, arg); | |
8637 | ||
8638 | if (! t || t == error_mark_node) | |
8639 | return 1; | |
8640 | } | |
8641 | } | |
6df47b06 | 8642 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
8643 | && (CLASSTYPE_TI_TEMPLATE (parm) |
8644 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
8645 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
8646 | Then, we should unify `int' and `U'. */ | |
6467930b | 8647 | t = arg; |
4393e105 MM |
8648 | else |
8649 | /* There's no chance of unication succeeding. */ | |
5566b478 | 8650 | return 1; |
6467930b | 8651 | |
830bfa74 | 8652 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 8653 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 8654 | } |
3bfdc719 MM |
8655 | else if (!same_type_p (TYPE_MAIN_VARIANT (parm), |
8656 | TYPE_MAIN_VARIANT (arg))) | |
5566b478 | 8657 | return 1; |
a4443a08 | 8658 | return 0; |
8d08fdba MS |
8659 | |
8660 | case METHOD_TYPE: | |
8d08fdba | 8661 | case FUNCTION_TYPE: |
830bfa74 | 8662 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 8663 | return 1; |
830bfa74 MM |
8664 | |
8665 | if (unify (tparms, targs, TREE_TYPE (parm), | |
4393e105 | 8666 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 8667 | return 1; |
386b8a85 | 8668 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 8669 | TYPE_ARG_TYPES (arg), 1, |
4393e105 | 8670 | DEDUCE_EXACT, 0); |
a4443a08 MS |
8671 | |
8672 | case OFFSET_TYPE: | |
8673 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
8674 | return 1; | |
830bfa74 | 8675 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 8676 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 8677 | return 1; |
830bfa74 | 8678 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8679 | strict); |
a4443a08 | 8680 | |
f62dbf03 | 8681 | case CONST_DECL: |
830bfa74 | 8682 | if (arg != decl_constant_value (parm)) |
f62dbf03 JM |
8683 | return 1; |
8684 | return 0; | |
8685 | ||
027905b4 KL |
8686 | case TEMPLATE_DECL: |
8687 | /* Matched cases are handled by the ARG == PARM test above. */ | |
8688 | return 1; | |
8689 | ||
53929c47 JM |
8690 | case MINUS_EXPR: |
8691 | if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST) | |
8692 | { | |
8693 | /* We handle this case specially, since it comes up with | |
8694 | arrays. In particular, something like: | |
8695 | ||
8696 | template <int N> void f(int (&x)[N]); | |
8697 | ||
8698 | Here, we are trying to unify the range type, which | |
8699 | looks like [0 ... (N - 1)]. */ | |
8700 | tree t, t1, t2; | |
8701 | t1 = TREE_OPERAND (parm, 0); | |
8702 | t2 = TREE_OPERAND (parm, 1); | |
8703 | ||
8704 | /* Should this be a regular fold? */ | |
8705 | t = maybe_fold_nontype_arg (build (PLUS_EXPR, | |
8706 | integer_type_node, | |
8707 | arg, t2)); | |
8708 | ||
4393e105 | 8709 | return unify (tparms, targs, t1, t, strict); |
53929c47 JM |
8710 | } |
8711 | /* else fall through */ | |
8712 | ||
8d08fdba | 8713 | default: |
050367a3 | 8714 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
4393e105 MM |
8715 | /* We're looking at an expression. This can happen with |
8716 | something like: | |
8717 | ||
8718 | template <int I> | |
8719 | void foo(S<I>, S<I + 2>); | |
050367a3 | 8720 | |
4393e105 | 8721 | This is a "nondeduced context": |
050367a3 | 8722 | |
4393e105 MM |
8723 | [deduct.type] |
8724 | ||
8725 | The nondeduced contexts are: | |
050367a3 | 8726 | |
4393e105 MM |
8727 | --A type that is a template-id in which one or more of |
8728 | the template-arguments is an expression that references | |
8729 | a template-parameter. | |
050367a3 | 8730 | |
4393e105 MM |
8731 | In these cases, we assume deduction succeeded, but don't |
8732 | actually infer any unifications. */ | |
8733 | return 0; | |
050367a3 | 8734 | else |
b4c4a9ec MM |
8735 | sorry ("use of `%s' in template type unification", |
8736 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 8737 | |
8d08fdba MS |
8738 | return 1; |
8739 | } | |
8740 | } | |
8d08fdba | 8741 | \f |
03d0f4af MM |
8742 | /* Called if RESULT is explicitly instantiated, or is a member of an |
8743 | explicitly instantiated class, or if using -frepo and the | |
8744 | instantiation of RESULT has been assigned to this file. */ | |
8745 | ||
faae18ab | 8746 | void |
5566b478 | 8747 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
8748 | tree result; |
8749 | int extern_p; | |
8750 | { | |
75650646 MM |
8751 | if (TREE_CODE (result) != FUNCTION_DECL) |
8752 | /* The TREE_PUBLIC flag for function declarations will have been | |
8753 | set correctly by tsubst. */ | |
8754 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
8755 | |
8756 | if (! extern_p) | |
8757 | { | |
8758 | DECL_INTERFACE_KNOWN (result) = 1; | |
8759 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 | 8760 | |
1a408d07 JM |
8761 | /* Always make artificials weak. */ |
8762 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
8763 | comdat_linkage (result); | |
a7d87521 JM |
8764 | /* For WIN32 we also want to put explicit instantiations in |
8765 | linkonce sections. */ | |
1a408d07 | 8766 | else if (TREE_PUBLIC (result)) |
b385c841 | 8767 | maybe_make_one_only (result); |
faae18ab | 8768 | } |
f49422da MS |
8769 | else if (TREE_CODE (result) == FUNCTION_DECL) |
8770 | mark_inline_for_output (result); | |
faae18ab MS |
8771 | } |
8772 | ||
e1467ff2 MM |
8773 | /* Given two function templates PAT1 and PAT2, and explicit template |
8774 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
8775 | |
8776 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
8777 | -1 if PAT2 is more specialized than PAT1. | |
8778 | 0 if neither is more specialized. */ | |
8779 | ||
8780 | int | |
e1467ff2 MM |
8781 | more_specialized (pat1, pat2, explicit_args) |
8782 | tree pat1, pat2, explicit_args; | |
6467930b | 8783 | { |
98c1c668 | 8784 | tree targs; |
73aad9b9 | 8785 | int winner = 0; |
6467930b | 8786 | |
4393e105 | 8787 | targs = get_bindings_overload (pat1, DECL_RESULT (pat2), explicit_args); |
73aad9b9 | 8788 | if (targs) |
36a117a5 | 8789 | --winner; |
6467930b | 8790 | |
4393e105 | 8791 | targs = get_bindings_overload (pat2, DECL_RESULT (pat1), explicit_args); |
73aad9b9 | 8792 | if (targs) |
36a117a5 | 8793 | ++winner; |
6467930b | 8794 | |
73aad9b9 JM |
8795 | return winner; |
8796 | } | |
6467930b | 8797 | |
73aad9b9 | 8798 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 8799 | |
73aad9b9 JM |
8800 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
8801 | -1 if PAT2 is more specialized than PAT1. | |
8802 | 0 if neither is more specialized. */ | |
8803 | ||
8804 | int | |
8805 | more_specialized_class (pat1, pat2) | |
8806 | tree pat1, pat2; | |
8807 | { | |
8808 | tree targs; | |
8809 | int winner = 0; | |
8810 | ||
36a117a5 MM |
8811 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
8812 | TREE_PURPOSE (pat2)); | |
73aad9b9 JM |
8813 | if (targs) |
8814 | --winner; | |
8815 | ||
36a117a5 MM |
8816 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
8817 | TREE_PURPOSE (pat1)); | |
73aad9b9 | 8818 | if (targs) |
6467930b MS |
8819 | ++winner; |
8820 | ||
8821 | return winner; | |
8822 | } | |
73aad9b9 JM |
8823 | |
8824 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 8825 | DECL from the function template FN, with the explicit template |
76b9a14d | 8826 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
4393e105 MM |
8827 | also match. Return NULL_TREE if no satisfactory arguments could be |
8828 | found. */ | |
73aad9b9 | 8829 | |
76b9a14d JM |
8830 | static tree |
8831 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 8832 | tree fn, decl, explicit_args; |
76b9a14d | 8833 | int check_rettype; |
73aad9b9 | 8834 | { |
98c1c668 | 8835 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 8836 | tree targs = make_scratch_vec (ntparms); |
4393e105 | 8837 | tree decl_type; |
03017874 | 8838 | tree decl_arg_types; |
98c1c668 JM |
8839 | int i; |
8840 | ||
4393e105 MM |
8841 | /* Substitute the explicit template arguments into the type of DECL. |
8842 | The call to fn_type_unification will handle substitution into the | |
8843 | FN. */ | |
8844 | decl_type = TREE_TYPE (decl); | |
8845 | if (explicit_args && uses_template_parms (decl_type)) | |
8846 | { | |
8847 | tree tmpl; | |
8848 | tree converted_args; | |
8849 | ||
8850 | if (DECL_TEMPLATE_INFO (decl)) | |
8851 | tmpl = DECL_TI_TEMPLATE (decl); | |
8852 | else | |
8853 | /* We can get here for some illegal specializations. */ | |
8854 | return NULL_TREE; | |
8855 | ||
8856 | converted_args | |
8857 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
8858 | explicit_args, NULL_TREE, | |
8859 | /*complain=*/0, | |
8860 | /*require_all_arguments=*/0)); | |
8861 | if (converted_args == error_mark_node) | |
8862 | return NULL_TREE; | |
8863 | ||
8864 | decl_type = tsubst (decl_type, converted_args, /*complain=*/0, | |
8865 | NULL_TREE); | |
8866 | if (decl_type == error_mark_node) | |
8867 | return NULL_TREE; | |
8868 | } | |
8869 | ||
03017874 MM |
8870 | /* If FN is a static member function, adjust the type of DECL |
8871 | appropriately. */ | |
4393e105 | 8872 | decl_arg_types = TYPE_ARG_TYPES (decl_type); |
d7684f2d MM |
8873 | if (DECL_STATIC_FUNCTION_P (fn) |
8874 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 8875 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 8876 | |
e1467ff2 | 8877 | i = fn_type_unification (fn, explicit_args, targs, |
03017874 MM |
8878 | decl_arg_types, |
8879 | TREE_TYPE (decl_type), | |
8880 | DEDUCE_EXACT); | |
98c1c668 | 8881 | |
76b9a14d JM |
8882 | if (i != 0) |
8883 | return NULL_TREE; | |
8884 | ||
8885 | if (check_rettype) | |
e1467ff2 MM |
8886 | { |
8887 | /* Check to see that the resulting return type is also OK. */ | |
36a117a5 | 8888 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs, |
4393e105 | 8889 | /*complain=*/0, NULL_TREE); |
e1467ff2 | 8890 | |
3bfdc719 | 8891 | if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl)))) |
e1467ff2 | 8892 | return NULL_TREE; |
e1467ff2 MM |
8893 | } |
8894 | ||
76b9a14d JM |
8895 | return targs; |
8896 | } | |
8897 | ||
8898 | /* For most uses, we want to check the return type. */ | |
8899 | ||
8900 | tree | |
8901 | get_bindings (fn, decl, explicit_args) | |
8902 | tree fn, decl, explicit_args; | |
8903 | { | |
8904 | return get_bindings_real (fn, decl, explicit_args, 1); | |
8905 | } | |
8906 | ||
8907 | /* But for more_specialized, we only care about the parameter types. */ | |
8908 | ||
8909 | static tree | |
8910 | get_bindings_overload (fn, decl, explicit_args) | |
8911 | tree fn, decl, explicit_args; | |
8912 | { | |
8913 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
8914 | } |
8915 | ||
36a117a5 MM |
8916 | /* Return the innermost template arguments that, when applied to a |
8917 | template specialization whose innermost template parameters are | |
8918 | TPARMS, and whose specialization arguments are ARGS, yield the | |
8919 | ARGS. | |
8920 | ||
8921 | For example, suppose we have: | |
8922 | ||
8923 | template <class T, class U> struct S {}; | |
8924 | template <class T> struct S<T*, int> {}; | |
8925 | ||
8926 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
8927 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
8928 | int}. The resulting vector will be {double}, indicating that `T' | |
8929 | is bound to `double'. */ | |
8930 | ||
bd6dd845 | 8931 | static tree |
36a117a5 MM |
8932 | get_class_bindings (tparms, parms, args) |
8933 | tree tparms, parms, args; | |
73aad9b9 | 8934 | { |
3b3ba9f0 | 8935 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
73aad9b9 JM |
8936 | tree vec = make_temp_vec (ntparms); |
8937 | ||
36a117a5 | 8938 | args = innermost_args (args); |
8d019cef | 8939 | |
4393e105 | 8940 | if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE)) |
fcfb9f96 | 8941 | return NULL_TREE; |
73aad9b9 JM |
8942 | |
8943 | for (i = 0; i < ntparms; ++i) | |
8944 | if (! TREE_VEC_ELT (vec, i)) | |
8945 | return NULL_TREE; | |
8946 | ||
8947 | return vec; | |
8948 | } | |
8949 | ||
104bf76a MM |
8950 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. |
8951 | Pick the most specialized template, and return the corresponding | |
8952 | instantiation, or if there is no corresponding instantiation, the | |
8953 | template itself. EXPLICIT_ARGS is any template arguments explicity | |
8954 | mentioned in a template-id. If there is no most specialized | |
8955 | tempalte, error_mark_node is returned. If there are no templates | |
8956 | at all, NULL_TREE is returned. */ | |
73aad9b9 JM |
8957 | |
8958 | tree | |
104bf76a MM |
8959 | most_specialized_instantiation (instantiations, explicit_args) |
8960 | tree instantiations; | |
8961 | tree explicit_args; | |
73aad9b9 | 8962 | { |
104bf76a | 8963 | tree fn, champ; |
73aad9b9 JM |
8964 | int fate; |
8965 | ||
104bf76a | 8966 | if (!instantiations) |
73aad9b9 JM |
8967 | return NULL_TREE; |
8968 | ||
104bf76a MM |
8969 | champ = instantiations; |
8970 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 8971 | { |
104bf76a MM |
8972 | fate = more_specialized (TREE_VALUE (champ), |
8973 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
8974 | if (fate == 1) |
8975 | ; | |
8976 | else | |
8977 | { | |
8978 | if (fate == 0) | |
8979 | { | |
8980 | fn = TREE_CHAIN (fn); | |
8981 | if (! fn) | |
8982 | return error_mark_node; | |
8983 | } | |
104bf76a | 8984 | champ = fn; |
73aad9b9 JM |
8985 | } |
8986 | } | |
8987 | ||
104bf76a | 8988 | for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 8989 | { |
104bf76a MM |
8990 | fate = more_specialized (TREE_VALUE (champ), |
8991 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
8992 | if (fate != 1) |
8993 | return error_mark_node; | |
8994 | } | |
8995 | ||
104bf76a MM |
8996 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); |
8997 | } | |
8998 | ||
8999 | /* Return the most specialized of the list of templates in FNS that can | |
9000 | produce an instantiation matching DECL, given the explicit template | |
9001 | arguments EXPLICIT_ARGS. */ | |
9002 | ||
e9659ab0 | 9003 | static tree |
104bf76a MM |
9004 | most_specialized (fns, decl, explicit_args) |
9005 | tree fns, decl, explicit_args; | |
9006 | { | |
9007 | tree candidates = NULL_TREE; | |
9008 | tree fn, args; | |
9009 | ||
9010 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
9011 | { | |
9012 | tree candidate = TREE_VALUE (fn); | |
9013 | ||
9014 | args = get_bindings (candidate, decl, explicit_args); | |
9015 | if (args) | |
9016 | candidates = scratch_tree_cons (NULL_TREE, candidate, | |
9017 | candidates); | |
9018 | } | |
9019 | ||
9020 | return most_specialized_instantiation (candidates, explicit_args); | |
73aad9b9 JM |
9021 | } |
9022 | ||
36a117a5 MM |
9023 | /* If DECL is a specialization of some template, return the most |
9024 | general such template. For example, given: | |
9025 | ||
9026 | template <class T> struct S { template <class U> void f(U); }; | |
9027 | ||
9028 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
9029 | the full template. This function will not trace past partial | |
9030 | specializations, however. For example, given in addition: | |
9031 | ||
9032 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
9033 | ||
9034 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
9035 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 9036 | |
e9659ab0 | 9037 | static tree |
36a117a5 MM |
9038 | most_general_template (decl) |
9039 | tree decl; | |
73aad9b9 | 9040 | { |
36a117a5 MM |
9041 | while (DECL_TEMPLATE_INFO (decl)) |
9042 | decl = DECL_TI_TEMPLATE (decl); | |
9043 | ||
9044 | return decl; | |
9045 | } | |
9046 | ||
9047 | /* Return the most specialized of the class template specializations | |
9048 | of TMPL which can produce an instantiation matching ARGS, or | |
9049 | error_mark_node if the choice is ambiguous. */ | |
9050 | ||
e9659ab0 | 9051 | static tree |
36a117a5 MM |
9052 | most_specialized_class (tmpl, args) |
9053 | tree tmpl; | |
9054 | tree args; | |
9055 | { | |
9056 | tree list = NULL_TREE; | |
9057 | tree t; | |
9058 | tree champ; | |
73aad9b9 JM |
9059 | int fate; |
9060 | ||
36a117a5 MM |
9061 | tmpl = most_general_template (tmpl); |
9062 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 9063 | { |
36a117a5 MM |
9064 | tree spec_args |
9065 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
9066 | if (spec_args) | |
73aad9b9 JM |
9067 | { |
9068 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
9069 | TREE_TYPE (list) = TREE_TYPE (t); | |
9070 | } | |
9071 | } | |
9072 | ||
9073 | if (! list) | |
9074 | return NULL_TREE; | |
9075 | ||
9076 | t = list; | |
9077 | champ = t; | |
9078 | t = TREE_CHAIN (t); | |
9079 | for (; t; t = TREE_CHAIN (t)) | |
9080 | { | |
9081 | fate = more_specialized_class (champ, t); | |
9082 | if (fate == 1) | |
9083 | ; | |
9084 | else | |
9085 | { | |
9086 | if (fate == 0) | |
9087 | { | |
9088 | t = TREE_CHAIN (t); | |
9089 | if (! t) | |
9090 | return error_mark_node; | |
9091 | } | |
9092 | champ = t; | |
9093 | } | |
9094 | } | |
9095 | ||
9096 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
9097 | { | |
85b71cf2 | 9098 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
9099 | if (fate != 1) |
9100 | return error_mark_node; | |
9101 | } | |
9102 | ||
9103 | return champ; | |
9104 | } | |
9105 | ||
8d08fdba | 9106 | /* called from the parser. */ |
e92cc029 | 9107 | |
8d08fdba | 9108 | void |
6633d636 | 9109 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 9110 | tree declspecs, declarator, storage; |
8d08fdba | 9111 | { |
c11b6f21 | 9112 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 9113 | tree result = NULL_TREE; |
faae18ab | 9114 | int extern_p = 0; |
e8abc66f | 9115 | |
ec255269 MS |
9116 | if (! DECL_LANG_SPECIFIC (decl)) |
9117 | { | |
8251199e | 9118 | cp_error ("explicit instantiation of non-template `%#D'", decl); |
ec255269 MS |
9119 | return; |
9120 | } | |
03d0f4af | 9121 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 9122 | { |
03d0f4af MM |
9123 | /* There is an asymmetry here in the way VAR_DECLs and |
9124 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
9125 | the latter, the DECL we get back will be marked as a | |
9126 | template instantiation, and the appropriate | |
9127 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
9128 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
9129 | should handle VAR_DECLs as it currently handles | |
9130 | FUNCTION_DECLs. */ | |
6633d636 MS |
9131 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); |
9132 | if (result && TREE_CODE (result) != VAR_DECL) | |
03d0f4af | 9133 | { |
8251199e | 9134 | cp_error ("no matching template for `%D' found", result); |
03d0f4af MM |
9135 | return; |
9136 | } | |
6633d636 MS |
9137 | } |
9138 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
9139 | { | |
8251199e | 9140 | cp_error ("explicit instantiation of `%#D'", decl); |
6633d636 MS |
9141 | return; |
9142 | } | |
03d0f4af MM |
9143 | else |
9144 | result = decl; | |
672476cb | 9145 | |
03d0f4af MM |
9146 | /* Check for various error cases. Note that if the explicit |
9147 | instantiation is legal the RESULT will currently be marked as an | |
9148 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
9149 | until we get here. */ | |
9150 | ||
9151 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 9152 | { |
03d0f4af MM |
9153 | /* [temp.spec] |
9154 | ||
9155 | No program shall both explicitly instantiate and explicitly | |
9156 | specialize a template. */ | |
6f1b4c42 JM |
9157 | cp_pedwarn ("explicit instantiation of `%#D' after", result); |
9158 | cp_pedwarn_at ("explicit specialization here", result); | |
672476cb MM |
9159 | return; |
9160 | } | |
03d0f4af MM |
9161 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
9162 | { | |
9163 | /* [temp.spec] | |
98c1c668 | 9164 | |
03d0f4af MM |
9165 | No program shall explicitly instantiate any template more |
9166 | than once. | |
9167 | ||
2aaf816d JM |
9168 | We check DECL_INTERFACE_KNOWN so as not to complain when the first |
9169 | instantiation was `extern' and the second is not, and EXTERN_P for | |
9170 | the opposite case. If -frepo, chances are we already got marked | |
9171 | as an explicit instantion because of the repo file. */ | |
9172 | if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository) | |
6f1b4c42 | 9173 | cp_pedwarn ("duplicate explicit instantiation of `%#D'", result); |
03d0f4af MM |
9174 | |
9175 | /* If we've already instantiated the template, just return now. */ | |
9176 | if (DECL_INTERFACE_KNOWN (result)) | |
9177 | return; | |
9178 | } | |
9179 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 9180 | { |
8251199e | 9181 | cp_error ("no matching template for `%D' found", result); |
faae18ab MS |
9182 | return; |
9183 | } | |
03d0f4af | 9184 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 9185 | { |
8251199e | 9186 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); |
6633d636 MS |
9187 | return; |
9188 | } | |
9189 | ||
a0a33927 MS |
9190 | if (flag_external_templates) |
9191 | return; | |
9192 | ||
f0e01782 | 9193 | if (storage == NULL_TREE) |
00595019 | 9194 | ; |
faae18ab | 9195 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af MM |
9196 | { |
9197 | if (pedantic) | |
8251199e | 9198 | cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations"); |
03d0f4af MM |
9199 | extern_p = 1; |
9200 | } | |
f0e01782 | 9201 | else |
8251199e | 9202 | cp_error ("storage class `%D' applied to template instantiation", |
f0e01782 | 9203 | storage); |
5566b478 | 9204 | |
03d0f4af | 9205 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
5566b478 | 9206 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 9207 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
9208 | if (! extern_p) |
9209 | instantiate_decl (result); | |
7177d104 MS |
9210 | } |
9211 | ||
faae18ab MS |
9212 | void |
9213 | mark_class_instantiated (t, extern_p) | |
9214 | tree t; | |
9215 | int extern_p; | |
9216 | { | |
9217 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
9218 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
9219 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
9220 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
9221 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
9222 | if (! extern_p) | |
9223 | { | |
9224 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
9225 | rest_of_type_compilation (t, 1); | |
9226 | } | |
9227 | } | |
e8abc66f | 9228 | |
7177d104 | 9229 | void |
ca79f85d JM |
9230 | do_type_instantiation (t, storage) |
9231 | tree t, storage; | |
7177d104 | 9232 | { |
e8abc66f MS |
9233 | int extern_p = 0; |
9234 | int nomem_p = 0; | |
5566b478 MS |
9235 | int static_p = 0; |
9236 | ||
ca79f85d JM |
9237 | if (TREE_CODE (t) == TYPE_DECL) |
9238 | t = TREE_TYPE (t); | |
9239 | ||
7ddedda4 | 9240 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 9241 | { |
8251199e | 9242 | cp_error ("explicit instantiation of non-template type `%T'", t); |
ca79f85d JM |
9243 | return; |
9244 | } | |
9245 | ||
5566b478 | 9246 | complete_type (t); |
7177d104 | 9247 | |
a292b002 MS |
9248 | /* With -fexternal-templates, explicit instantiations are treated the same |
9249 | as implicit ones. */ | |
a0a33927 MS |
9250 | if (flag_external_templates) |
9251 | return; | |
9252 | ||
f0e01782 MS |
9253 | if (TYPE_SIZE (t) == NULL_TREE) |
9254 | { | |
8251199e | 9255 | cp_error ("explicit instantiation of `%#T' before definition of template", |
f0e01782 MS |
9256 | t); |
9257 | return; | |
9258 | } | |
9259 | ||
03d0f4af | 9260 | if (storage != NULL_TREE) |
f0e01782 | 9261 | { |
03d0f4af | 9262 | if (pedantic) |
8251199e JM |
9263 | cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations", |
9264 | IDENTIFIER_POINTER (storage)); | |
03d0f4af MM |
9265 | |
9266 | if (storage == ridpointers[(int) RID_INLINE]) | |
9267 | nomem_p = 1; | |
9268 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
9269 | extern_p = 1; | |
9270 | else if (storage == ridpointers[(int) RID_STATIC]) | |
9271 | static_p = 1; | |
9272 | else | |
9273 | { | |
8251199e | 9274 | cp_error ("storage class `%D' applied to template instantiation", |
03d0f4af MM |
9275 | storage); |
9276 | extern_p = 0; | |
9277 | } | |
f0e01782 MS |
9278 | } |
9279 | ||
03d0f4af MM |
9280 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
9281 | { | |
9282 | /* [temp.spec] | |
a292b002 | 9283 | |
03d0f4af MM |
9284 | No program shall both explicitly instantiate and explicitly |
9285 | specialize a template. */ | |
8251199e JM |
9286 | cp_error ("explicit instantiation of `%#T' after", t); |
9287 | cp_error_at ("explicit specialization here", t); | |
03d0f4af MM |
9288 | return; |
9289 | } | |
9290 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 9291 | { |
03d0f4af MM |
9292 | /* [temp.spec] |
9293 | ||
9294 | No program shall explicitly instantiate any template more | |
9295 | than once. | |
9296 | ||
2aaf816d JM |
9297 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation |
9298 | was `extern'. If EXTERN_P then the second is. If -frepo, chances | |
9299 | are we already got marked as an explicit instantion because of the | |
9300 | repo file. All these cases are OK. */ | |
9301 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository) | |
9302 | cp_pedwarn ("duplicate explicit instantiation of `%#T'", t); | |
03d0f4af MM |
9303 | |
9304 | /* If we've already instantiated the template, just return now. */ | |
9305 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
9306 | return; | |
44a8d0b3 | 9307 | } |
e8abc66f | 9308 | |
03d0f4af MM |
9309 | mark_class_instantiated (t, extern_p); |
9310 | repo_template_instantiated (t, extern_p); | |
9311 | ||
e8abc66f MS |
9312 | if (nomem_p) |
9313 | return; | |
9314 | ||
7177d104 | 9315 | { |
db5ae43f | 9316 | tree tmp; |
5566b478 | 9317 | |
03d0f4af MM |
9318 | /* In contrast to implicit instantiation, where only the |
9319 | declarations, and not the definitions, of members are | |
9320 | instantiated, we have here: | |
9321 | ||
9322 | [temp.explicit] | |
9323 | ||
9324 | The explicit instantiation of a class template specialization | |
9325 | implies the instantiation of all of its members not | |
9326 | previously explicitly specialized in the translation unit | |
9327 | containing the explicit instantiation. | |
9328 | ||
9329 | Of course, we can't instantiate member template classes, since | |
9330 | we don't have any arguments for them. Note that the standard | |
9331 | is unclear on whether the instatiation of the members are | |
9332 | *explicit* instantiations or not. We choose to be generous, | |
9333 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
9334 | the explicit instantiation of a class where some of the members | |
9335 | have no definition in the current translation unit. */ | |
9336 | ||
5566b478 MS |
9337 | if (! static_p) |
9338 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 9339 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 9340 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
9341 | { |
9342 | mark_decl_instantiated (tmp, extern_p); | |
9343 | repo_template_instantiated (tmp, extern_p); | |
9344 | if (! extern_p) | |
9345 | instantiate_decl (tmp); | |
9346 | } | |
9347 | ||
9348 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
9349 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 9350 | { |
5566b478 | 9351 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 9352 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
9353 | if (! extern_p) |
9354 | instantiate_decl (tmp); | |
863adfc0 | 9355 | } |
7177d104 | 9356 | |
a292b002 | 9357 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
e4a84209 MM |
9358 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) |
9359 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
f376e137 | 9360 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); |
a292b002 | 9361 | } |
8d08fdba | 9362 | } |
a28e3c7f | 9363 | |
36a117a5 MM |
9364 | /* Given a function DECL, which is a specialization of TMPL, modify |
9365 | DECL to be a re-instantiation of TMPL with the same template | |
9366 | arguments. TMPL should be the template into which tsubst'ing | |
9367 | should occur for DECL, not the most general template. | |
830bfa74 MM |
9368 | |
9369 | One reason for doing this is a scenario like this: | |
9370 | ||
9371 | template <class T> | |
9372 | void f(const T&, int i); | |
9373 | ||
9374 | void g() { f(3, 7); } | |
9375 | ||
9376 | template <class T> | |
9377 | void f(const T& t, const int i) { } | |
9378 | ||
9379 | Note that when the template is first instantiated, with | |
9380 | instantiate_template, the resulting DECL will have no name for the | |
9381 | first parameter, and the wrong type for the second. So, when we go | |
9382 | to instantiate the DECL, we regenerate it. */ | |
9383 | ||
e9659ab0 | 9384 | static void |
830bfa74 MM |
9385 | regenerate_decl_from_template (decl, tmpl) |
9386 | tree decl; | |
9387 | tree tmpl; | |
9388 | { | |
9389 | tree args; | |
830bfa74 MM |
9390 | tree code_pattern; |
9391 | tree new_decl; | |
36a117a5 MM |
9392 | tree gen_tmpl; |
9393 | int unregistered; | |
830bfa74 MM |
9394 | |
9395 | args = DECL_TI_ARGS (decl); | |
9396 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
9397 | ||
8251199e JM |
9398 | /* Unregister the specialization so that when we tsubst we will not |
9399 | just return DECL. We don't have to unregister DECL from TMPL | |
9400 | because if would only be registered there if it were a partial | |
9401 | instantiation of a specialization, which it isn't: it's a full | |
9402 | instantiation. */ | |
36a117a5 MM |
9403 | gen_tmpl = most_general_template (tmpl); |
9404 | unregistered = unregister_specialization (decl, gen_tmpl); | |
9405 | ||
9406 | /* If the DECL was not unregistered then something peculiar is | |
9407 | happening: we created a specialization but did not call | |
9408 | register_specialization for it. */ | |
9409 | my_friendly_assert (unregistered, 0); | |
9410 | ||
61a127b3 MM |
9411 | if (TREE_CODE (decl) == VAR_DECL) |
9412 | /* Make sure that we can see identifiers, and compute access | |
9413 | correctly, for the class members used in the declaration of | |
9414 | this static variable. */ | |
9415 | pushclass (DECL_CONTEXT (decl), 2); | |
9416 | ||
d8c4447d | 9417 | /* Do the substitution to get the new declaration. */ |
4393e105 | 9418 | new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE); |
830bfa74 MM |
9419 | |
9420 | if (TREE_CODE (decl) == VAR_DECL) | |
9421 | { | |
9422 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
830bfa74 | 9423 | DECL_INITIAL (new_decl) = |
d8c4447d | 9424 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
4393e105 | 9425 | /*complain=*/1, DECL_TI_TEMPLATE (decl)); |
61a127b3 | 9426 | /* Pop the class context we pushed above. */ |
b74a0560 | 9427 | popclass (); |
830bfa74 | 9428 | } |
2b0a63a3 | 9429 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
fbf1c34b MM |
9430 | { |
9431 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
9432 | new decl. */ | |
9433 | DECL_INITIAL (new_decl) = error_mark_node; | |
9434 | /* And don't complain about a duplicate definition. */ | |
9435 | DECL_INITIAL (decl) = NULL_TREE; | |
9436 | } | |
36a117a5 MM |
9437 | |
9438 | /* The immediate parent of the new template is still whatever it was | |
9439 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
9440 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
9441 | tsubst always calculates the name as if the function in question | |
9442 | were really a template instance, and sometimes, with friend | |
9443 | functions, this is not so. See tsubst_friend_function for | |
9444 | details. */ | |
9445 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
9446 | DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl); | |
9447 | DECL_RTL (new_decl) = DECL_RTL (decl); | |
9448 | ||
9449 | /* Call duplicate decls to merge the old and new declarations. */ | |
830bfa74 MM |
9450 | duplicate_decls (new_decl, decl); |
9451 | ||
36a117a5 MM |
9452 | /* Now, re-register the specialization. */ |
9453 | register_specialization (decl, gen_tmpl, args); | |
830bfa74 MM |
9454 | } |
9455 | ||
f84b4be9 JM |
9456 | /* Produce the definition of D, a _DECL generated from a template. */ |
9457 | ||
a28e3c7f | 9458 | tree |
5566b478 MS |
9459 | instantiate_decl (d) |
9460 | tree d; | |
a28e3c7f | 9461 | { |
36a117a5 MM |
9462 | tree tmpl = DECL_TI_TEMPLATE (d); |
9463 | tree args = DECL_TI_ARGS (d); | |
830bfa74 | 9464 | tree td; |
36a117a5 MM |
9465 | tree code_pattern; |
9466 | tree spec; | |
9467 | tree gen_tmpl; | |
5566b478 | 9468 | int nested = in_function_p (); |
5566b478 | 9469 | int pattern_defined; |
c27be9b9 JM |
9470 | int line = lineno; |
9471 | char *file = input_filename; | |
5566b478 | 9472 | |
36a117a5 MM |
9473 | /* This function should only be used to instantiate templates for |
9474 | functions and static member variables. */ | |
9475 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
9476 | || TREE_CODE (d) == VAR_DECL, 0); | |
9477 | ||
fbf1c34b | 9478 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af MM |
9479 | /* D has already been instantiated. It might seem reasonable to |
9480 | check whether or not D is an explict instantiation, and, if so, | |
9481 | stop here. But when an explicit instantiation is deferred | |
9482 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
9483 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
9484 | return d; |
9485 | ||
9486 | /* If we already have a specialization of this declaration, then | |
9487 | there's no reason to instantiate it. Note that | |
9488 | retrieve_specialization gives us both instantiations and | |
9489 | specializations, so we must explicitly check | |
9490 | DECL_TEMPLATE_SPECIALIZATION. */ | |
9491 | gen_tmpl = most_general_template (tmpl); | |
9492 | spec = retrieve_specialization (gen_tmpl, args); | |
9493 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
9494 | return spec; | |
9495 | ||
9496 | /* This needs to happen before any tsubsting. */ | |
9497 | if (! push_tinst_level (d)) | |
9498 | return d; | |
9499 | ||
4d85e00e MM |
9500 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
9501 | for the instantiation. This is not always the most general | |
9502 | template. Consider, for example: | |
9503 | ||
9504 | template <class T> | |
9505 | struct S { template <class U> void f(); | |
9506 | template <> void f<int>(); }; | |
9507 | ||
9508 | and an instantiation of S<double>::f<int>. We want TD to be the | |
9509 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
9510 | td = tmpl; | |
9511 | for (td = tmpl; | |
9512 | /* An instantiation cannot have a definition, so we need a | |
9513 | more general template. */ | |
9514 | DECL_TEMPLATE_INSTANTIATION (td) | |
9515 | /* We must also deal with friend templates. Given: | |
9516 | ||
9517 | template <class T> struct S { | |
9518 | template <class U> friend void f() {}; | |
9519 | }; | |
9520 | ||
9521 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
9522 | so far as the language is concerned, but that's still | |
9523 | where we get the pattern for the instantiation from. On | |
9524 | ther hand, if the definition comes outside the class, say: | |
9525 | ||
9526 | template <class T> struct S { | |
9527 | template <class U> friend void f(); | |
9528 | }; | |
9529 | template <class U> friend void f() {} | |
9530 | ||
9531 | we don't need to look any further. That's what the check for | |
9532 | DECL_INITIAL is for. */ | |
9533 | || (TREE_CODE (d) == FUNCTION_DECL | |
61289ca3 | 9534 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) |
4d85e00e | 9535 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))); |
f84b4be9 | 9536 | ) |
4d85e00e MM |
9537 | { |
9538 | /* The present template, TD, should not be a definition. If it | |
9539 | were a definition, we should be using it! Note that we | |
9540 | cannot restructure the loop to just keep going until we find | |
9541 | a template with a definition, since that might go too far if | |
9542 | a specialization was declared, but not defined. */ | |
4d85e00e MM |
9543 | my_friendly_assert (!(TREE_CODE (d) == VAR_DECL |
9544 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), | |
9545 | 0); | |
9546 | ||
9547 | /* Fetch the more general template. */ | |
9548 | td = DECL_TI_TEMPLATE (td); | |
9549 | } | |
27bb8339 | 9550 | |
fee23f54 | 9551 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 9552 | |
5566b478 | 9553 | if (TREE_CODE (d) == FUNCTION_DECL) |
36a117a5 | 9554 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 | 9555 | else |
36a117a5 | 9556 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
de22184b MS |
9557 | |
9558 | push_to_top_level (); | |
9559 | lineno = DECL_SOURCE_LINE (d); | |
9560 | input_filename = DECL_SOURCE_FILE (d); | |
9561 | ||
de22184b | 9562 | if (pattern_defined) |
5566b478 MS |
9563 | { |
9564 | repo_template_used (d); | |
9565 | ||
9566 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
9567 | { | |
9568 | if (flag_alt_external_templates) | |
9569 | { | |
9570 | if (interface_unknown) | |
9571 | warn_if_unknown_interface (d); | |
9572 | } | |
fee23f54 | 9573 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
9574 | { |
9575 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 9576 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
9577 | } |
9578 | else | |
fee23f54 | 9579 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
9580 | } |
9581 | ||
e92cc029 | 9582 | if (at_eof) |
5566b478 MS |
9583 | import_export_decl (d); |
9584 | } | |
9585 | ||
c4ae3f91 JM |
9586 | /* Reject all external templates except inline functions. */ |
9587 | if (DECL_INTERFACE_KNOWN (d) | |
9588 | && ! DECL_NOT_REALLY_EXTERN (d) | |
9589 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
9590 | goto out; | |
9591 | ||
67ffc812 | 9592 | if (TREE_CODE (d) == VAR_DECL |
711734a9 | 9593 | && TREE_READONLY (d) |
67ffc812 MM |
9594 | && DECL_INITIAL (d) == NULL_TREE |
9595 | && DECL_INITIAL (code_pattern) != NULL_TREE) | |
9596 | /* We need to set up DECL_INITIAL regardless of pattern_defined if | |
9597 | the variable is a static const initialized in the class body. */; | |
9598 | else if (! pattern_defined | |
9599 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) | |
9600 | && ! at_eof)) | |
5566b478 | 9601 | { |
67ffc812 | 9602 | /* Defer all templates except inline functions used in another |
c27be9b9 JM |
9603 | function. We restore the source position here because it's used |
9604 | by add_pending_template. */ | |
9605 | lineno = line; | |
9606 | input_filename = file; | |
9607 | ||
03d0f4af MM |
9608 | if (at_eof && !pattern_defined |
9609 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
9610 | /* [temp.explicit] | |
9611 | ||
9612 | The definition of a non-exported function template, a | |
9613 | non-exported member function template, or a non-exported | |
9614 | member function or static data member of a class template | |
9615 | shall be present in every translation unit in which it is | |
9616 | explicitly instantiated. */ | |
8251199e | 9617 | cp_error ("explicit instantiation of `%D' but no definition available", |
03d0f4af MM |
9618 | d); |
9619 | ||
5566b478 | 9620 | add_pending_template (d); |
de22184b | 9621 | goto out; |
5566b478 MS |
9622 | } |
9623 | ||
2b0a63a3 MM |
9624 | /* We're now committed to instantiating this template. Mark it as |
9625 | instantiated so that recursive calls to instantiate_decl do not | |
9626 | try to instantiate it again. */ | |
fbf1c34b | 9627 | DECL_TEMPLATE_INSTANTIATED (d) = 1; |
120722ac | 9628 | |
2b0a63a3 MM |
9629 | /* Regenerate the declaration in case the template has been modified |
9630 | by a subsequent redeclaration. */ | |
9631 | regenerate_decl_from_template (d, td); | |
9632 | ||
120722ac MM |
9633 | /* We already set the file and line above. Reset them now in case |
9634 | they changed as a result of calling regenerate_decl_from_template. */ | |
5156628f MS |
9635 | lineno = DECL_SOURCE_LINE (d); |
9636 | input_filename = DECL_SOURCE_FILE (d); | |
9637 | ||
5566b478 MS |
9638 | if (TREE_CODE (d) == VAR_DECL) |
9639 | { | |
9640 | DECL_IN_AGGR_P (d) = 0; | |
9641 | if (DECL_INTERFACE_KNOWN (d)) | |
9642 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
9643 | else | |
9644 | { | |
9645 | DECL_EXTERNAL (d) = 1; | |
9646 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
9647 | } | |
9648 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
9649 | } | |
9650 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
9651 | { | |
558475f0 MM |
9652 | extern struct obstack *saveable_obstack; |
9653 | extern struct obstack *rtl_obstack; | |
9654 | int saved_expanding_p = expanding_p; | |
9655 | ||
9656 | /* We're not expanding all the way to RTL here. */ | |
9657 | expanding_p = 0; | |
5566b478 | 9658 | |
558475f0 | 9659 | /* Set up context. */ |
c11b6f21 | 9660 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
9661 | store_parm_decls (); |
9662 | ||
558475f0 MM |
9663 | /* Anything we might |
9664 | want to save is going to have to be saved forever. Note that | |
9665 | we don't want to save all kinds of temporary clutter that | |
9666 | might end up on the temporary obstack so we don't want to | |
9667 | call push_permanent_obstack. */ | |
9668 | push_obstacks_nochange (); | |
9669 | saveable_obstack = &permanent_obstack; | |
9670 | /* We only need this because of the cases where we generate | |
9671 | RTL_EXPRs. We should really be generating RTL_EXPRs until | |
9672 | final expansion time; when that is fixed, this can go. */ | |
9673 | rtl_obstack = &permanent_obstack; | |
9674 | /* Substitute into the body of the function. */ | |
9675 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
9676 | /*complain=*/1, tmpl); | |
9677 | ||
9678 | /* Clean up. */ | |
9679 | pop_obstacks (); | |
5566b478 | 9680 | finish_function (lineno, 0, nested); |
558475f0 MM |
9681 | expanding_p = saved_expanding_p; |
9682 | ||
9683 | /* Now, generate RTL for the function. */ | |
9684 | expand_body (d); | |
5566b478 MS |
9685 | } |
9686 | ||
de22184b | 9687 | out: |
c27be9b9 JM |
9688 | lineno = line; |
9689 | input_filename = file; | |
9690 | ||
5566b478 | 9691 | pop_from_top_level (); |
5566b478 | 9692 | pop_tinst_level (); |
a28e3c7f | 9693 | |
a28e3c7f MS |
9694 | return d; |
9695 | } | |
5566b478 | 9696 | |
0aafb128 MM |
9697 | /* Run through the list of templates that we wish we could |
9698 | instantiate, and instantiate any we can. */ | |
9699 | ||
9700 | int | |
9701 | instantiate_pending_templates () | |
9702 | { | |
9703 | tree *t; | |
9704 | int instantiated_something = 0; | |
9705 | int reconsider; | |
9706 | ||
9707 | do | |
9708 | { | |
9709 | reconsider = 0; | |
9710 | ||
9711 | t = &pending_templates; | |
9712 | while (*t) | |
9713 | { | |
9714 | tree srcloc = TREE_PURPOSE (*t); | |
9715 | tree instantiation = TREE_VALUE (*t); | |
9716 | ||
9717 | input_filename = SRCLOC_FILE (srcloc); | |
9718 | lineno = SRCLOC_LINE (srcloc); | |
9719 | ||
9720 | if (TREE_CODE_CLASS (TREE_CODE (instantiation)) == 't') | |
9721 | { | |
9722 | tree fn; | |
9723 | ||
9724 | if (!TYPE_SIZE (instantiation)) | |
9725 | { | |
9726 | instantiate_class_template (instantiation); | |
9727 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
9728 | for (fn = TYPE_METHODS (instantiation); | |
9729 | fn; | |
9730 | fn = TREE_CHAIN (fn)) | |
9731 | if (! DECL_ARTIFICIAL (fn)) | |
9732 | instantiate_decl (fn); | |
9733 | if (TYPE_SIZE (instantiation)) | |
9734 | { | |
9735 | instantiated_something = 1; | |
9736 | reconsider = 1; | |
9737 | } | |
9738 | } | |
9739 | ||
9740 | if (TYPE_SIZE (instantiation)) | |
9741 | /* If INSTANTIATION has been instantiated, then we don't | |
9742 | need to consider it again in the future. */ | |
9743 | *t = TREE_CHAIN (*t); | |
9744 | else | |
9745 | t = &TREE_CHAIN (*t); | |
9746 | } | |
9747 | else | |
9748 | { | |
9749 | if (DECL_TEMPLATE_INSTANTIATION (instantiation) | |
9750 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9751 | { | |
9752 | instantiation = instantiate_decl (instantiation); | |
9753 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9754 | { | |
9755 | instantiated_something = 1; | |
9756 | reconsider = 1; | |
9757 | } | |
9758 | } | |
9759 | ||
9760 | if (!DECL_TEMPLATE_INSTANTIATION (instantiation) | |
9761 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
9762 | /* If INSTANTIATION has been instantiated, then we don't | |
9763 | need to consider it again in the future. */ | |
9764 | *t = TREE_CHAIN (*t); | |
9765 | else | |
9766 | t = &TREE_CHAIN (*t); | |
9767 | } | |
9768 | } | |
9769 | template_tail = t; | |
9770 | ||
9771 | /* Go through the things that are template instantiations if we are | |
9772 | using guiding declarations. */ | |
9773 | t = &maybe_templates; | |
9774 | while (*t) | |
9775 | { | |
9776 | tree template; | |
9777 | tree fn; | |
9778 | tree args; | |
9779 | ||
9780 | fn = TREE_VALUE (*t); | |
9781 | ||
9782 | if (DECL_INITIAL (fn)) | |
9783 | /* If the FN is already defined, then it was either already | |
9784 | instantiated or, even though guiding declarations were | |
9785 | allowed, a non-template definition was provided. */ | |
9786 | ; | |
9787 | else | |
9788 | { | |
9789 | template = TREE_PURPOSE (*t); | |
9790 | args = get_bindings (template, fn, NULL_TREE); | |
9791 | fn = instantiate_template (template, args); | |
9792 | instantiate_decl (fn); | |
9793 | reconsider = 1; | |
9794 | } | |
9795 | ||
9796 | /* Remove this entry from the chain. */ | |
9797 | *t = TREE_CHAIN (*t); | |
9798 | } | |
9799 | maybe_template_tail = t; | |
9800 | } | |
9801 | while (reconsider); | |
9802 | ||
9803 | return instantiated_something; | |
9804 | } | |
9805 | ||
4393e105 MM |
9806 | /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it |
9807 | is an initializer list: the TREE_PURPOSEs are DECLs, and the | |
9808 | TREE_VALUEs are initializer values. Used by instantiate_decl. */ | |
9809 | ||
824b9a4c | 9810 | static tree |
5566b478 MS |
9811 | tsubst_expr_values (t, argvec) |
9812 | tree t, argvec; | |
9813 | { | |
9814 | tree first = NULL_TREE; | |
9815 | tree *p = &first; | |
9816 | ||
9817 | for (; t; t = TREE_CHAIN (t)) | |
9818 | { | |
4393e105 MM |
9819 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, |
9820 | /*complain=*/1, NULL_TREE); | |
9821 | tree val = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1, | |
9822 | NULL_TREE); | |
5566b478 MS |
9823 | *p = build_tree_list (pur, val); |
9824 | p = &TREE_CHAIN (*p); | |
9825 | } | |
9826 | return first; | |
9827 | } | |
9828 | ||
9829 | tree last_tree; | |
9830 | ||
9831 | void | |
9832 | add_tree (t) | |
9833 | tree t; | |
9834 | { | |
9835 | last_tree = TREE_CHAIN (last_tree) = t; | |
9836 | } | |
73aad9b9 | 9837 | |
75650646 MM |
9838 | |
9839 | void | |
9840 | begin_tree () | |
9841 | { | |
9842 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
9843 | last_tree = NULL_TREE; | |
9844 | } | |
9845 | ||
9846 | ||
9847 | void | |
9848 | end_tree () | |
9849 | { | |
9850 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
9851 | ||
9852 | last_tree = TREE_VALUE (saved_trees); | |
9853 | saved_trees = TREE_CHAIN (saved_trees); | |
9854 | } | |
9855 | ||
73aad9b9 JM |
9856 | /* D is an undefined function declaration in the presence of templates with |
9857 | the same name, listed in FNS. If one of them can produce D as an | |
9858 | instantiation, remember this so we can instantiate it at EOF if D has | |
9859 | not been defined by that time. */ | |
9860 | ||
9861 | void | |
9862 | add_maybe_template (d, fns) | |
9863 | tree d, fns; | |
9864 | { | |
9865 | tree t; | |
9866 | ||
9867 | if (DECL_MAYBE_TEMPLATE (d)) | |
9868 | return; | |
9869 | ||
e1467ff2 | 9870 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
9871 | if (! t) |
9872 | return; | |
9873 | if (t == error_mark_node) | |
9874 | { | |
8251199e | 9875 | cp_error ("ambiguous template instantiation for `%D'", d); |
73aad9b9 JM |
9876 | return; |
9877 | } | |
9878 | ||
9879 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
9880 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
9881 | DECL_MAYBE_TEMPLATE (d) = 1; | |
9882 | } | |
b87692e5 | 9883 | |
61a127b3 MM |
9884 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
9885 | ||
9886 | static void | |
9887 | set_current_access_from_decl (decl) | |
9888 | tree decl; | |
9889 | { | |
9890 | if (TREE_PRIVATE (decl)) | |
9891 | current_access_specifier = access_private_node; | |
9892 | else if (TREE_PROTECTED (decl)) | |
9893 | current_access_specifier = access_protected_node; | |
9894 | else | |
9895 | current_access_specifier = access_public_node; | |
9896 | } | |
9897 | ||
dbfe2124 MM |
9898 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
9899 | is the instantiation (which should have been created with | |
9900 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 9901 | |
dbfe2124 MM |
9902 | static void |
9903 | tsubst_enum (tag, newtag, args) | |
9904 | tree tag; | |
9905 | tree newtag; | |
9906 | tree args; | |
b87692e5 | 9907 | { |
dbfe2124 | 9908 | tree e; |
b87692e5 MS |
9909 | |
9910 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
9911 | { | |
61a127b3 MM |
9912 | tree value; |
9913 | tree elt; | |
9914 | ||
9915 | /* Note that in a template enum, the TREE_VALUE is the | |
9916 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
9917 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
4393e105 MM |
9918 | args, /*complain=*/1, |
9919 | NULL_TREE); | |
61a127b3 MM |
9920 | |
9921 | /* Give this enumeration constant the correct access. */ | |
9922 | set_current_access_from_decl (TREE_VALUE (e)); | |
9923 | ||
9924 | /* Actually build the enumerator itself. */ | |
9925 | elt = build_enumerator (TREE_PURPOSE (e), value, newtag); | |
b87692e5 | 9926 | |
dbfe2124 MM |
9927 | /* We save the enumerators we have built so far in the |
9928 | TYPE_VALUES so that if the enumeration constants for | |
9929 | subsequent enumerators involve those for previous ones, | |
9930 | tsubst_copy will be able to find them. */ | |
9931 | TREE_CHAIN (elt) = TYPE_VALUES (newtag); | |
9932 | TYPE_VALUES (newtag) = elt; | |
9933 | } | |
b3d5a58b | 9934 | |
dbfe2124 | 9935 | finish_enum (newtag); |
b87692e5 | 9936 | } |
36a117a5 MM |
9937 | |
9938 | /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that | |
9939 | is either an instantiation or specialization of a template | |
9940 | function. */ | |
9941 | ||
9942 | static void | |
9943 | set_mangled_name_for_template_decl (decl) | |
9944 | tree decl; | |
9945 | { | |
9946 | tree saved_namespace; | |
b370501f | 9947 | tree context = NULL_TREE; |
36a117a5 MM |
9948 | tree fn_type; |
9949 | tree ret_type; | |
9950 | tree parm_types; | |
9951 | tree tparms; | |
9952 | tree targs; | |
9953 | tree tmpl; | |
9954 | int parm_depth; | |
9955 | ||
9956 | my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); | |
9957 | my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0); | |
9958 | ||
9959 | /* The names of template functions must be mangled so as to indicate | |
9960 | what template is being specialized with what template arguments. | |
9961 | For example, each of the following three functions must get | |
9962 | different mangled names: | |
9963 | ||
9964 | void f(int); | |
9965 | template <> void f<7>(int); | |
9966 | template <> void f<8>(int); */ | |
9967 | ||
9968 | targs = DECL_TI_ARGS (decl); | |
9969 | if (uses_template_parms (targs)) | |
9970 | /* This DECL is for a partial instantiation. There's no need to | |
9971 | mangle the name of such an entity. */ | |
9972 | return; | |
9973 | ||
9974 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
9975 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
9976 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
9977 | ||
9978 | /* There should be as many levels of arguments as there are levels | |
9979 | of parameters. */ | |
9980 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
9981 | ||
9982 | /* We now compute the PARMS and RET_TYPE to give to | |
9983 | build_decl_overload_real. The PARMS and RET_TYPE are the | |
9984 | parameter and return types of the template, after all but the | |
9985 | innermost template arguments have been substituted, not the | |
9986 | parameter and return types of the function DECL. For example, | |
9987 | given: | |
9988 | ||
9989 | template <class T> T f(T); | |
9990 | ||
9991 | both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'. | |
9992 | A more subtle example is: | |
9993 | ||
9994 | template <class T> struct S { template <class U> void f(T, U); } | |
9995 | ||
9996 | Here, if DECL is `void S<int>::f(int, double)', PARMS should be | |
9997 | {int, U}. Thus, the args that we want to subsitute into the | |
9998 | return and parameter type for the function are those in TARGS, | |
9999 | with the innermost level omitted. */ | |
10000 | fn_type = TREE_TYPE (tmpl); | |
10001 | if (DECL_STATIC_FUNCTION_P (decl)) | |
61a127b3 | 10002 | context = DECL_CLASS_CONTEXT (decl); |
36a117a5 MM |
10003 | |
10004 | if (parm_depth == 1) | |
10005 | /* No substitution is necessary. */ | |
10006 | ; | |
10007 | else | |
10008 | { | |
10009 | int i; | |
10010 | tree partial_args; | |
10011 | ||
10012 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
10013 | let tsubst know not to subsitute for those parameters. */ | |
10014 | partial_args = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
10015 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
10016 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
10017 | TMPL_ARGS_LEVEL (targs, i)); | |
10018 | SET_TMPL_ARGS_LEVEL (partial_args, | |
10019 | TMPL_ARGS_DEPTH (targs), | |
10020 | make_temp_vec (DECL_NTPARMS (tmpl))); | |
10021 | ||
10022 | /* Now, do the (partial) substitution to figure out the | |
10023 | appropriate function type. */ | |
4393e105 | 10024 | fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 | 10025 | if (DECL_STATIC_FUNCTION_P (decl)) |
4393e105 | 10026 | context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
10027 | |
10028 | /* Substitute into the template parameters to obtain the real | |
10029 | innermost set of parameters. This step is important if the | |
10030 | innermost set of template parameters contains value | |
10031 | parameters whose types depend on outer template parameters. */ | |
10032 | TREE_VEC_LENGTH (partial_args)--; | |
4393e105 | 10033 | tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1); |
36a117a5 MM |
10034 | } |
10035 | ||
10036 | /* Now, get the innermost parameters and arguments, and figure out | |
10037 | the parameter and return types. */ | |
10038 | tparms = INNERMOST_TEMPLATE_PARMS (tparms); | |
10039 | targs = innermost_args (targs); | |
10040 | ret_type = TREE_TYPE (fn_type); | |
10041 | parm_types = TYPE_ARG_TYPES (fn_type); | |
10042 | ||
10043 | /* For a static member function, we generate a fake `this' pointer, | |
10044 | for the purposes of mangling. This indicates of which class the | |
10045 | function is a member. Because of: | |
10046 | ||
10047 | [class.static] | |
10048 | ||
10049 | There shall not be a static and a nonstatic member function | |
10050 | with the same name and the same parameter types | |
10051 | ||
10052 | we don't have to worry that this will result in a clash with a | |
10053 | non-static member function. */ | |
10054 | if (DECL_STATIC_FUNCTION_P (decl)) | |
10055 | parm_types = hash_tree_chain (build_pointer_type (context), parm_types); | |
10056 | ||
10057 | /* There should be the same number of template parameters as | |
10058 | template arguments. */ | |
10059 | my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs), | |
10060 | 0); | |
10061 | ||
10062 | /* If the template is in a namespace, we need to put that into the | |
10063 | mangled name. Unfortunately, build_decl_overload_real does not | |
10064 | get the decl to mangle, so it relies on the current | |
10065 | namespace. Therefore, we set that here temporarily. */ | |
10066 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702); | |
10067 | saved_namespace = current_namespace; | |
10068 | current_namespace = CP_DECL_CONTEXT (decl); | |
10069 | ||
10070 | /* Actually set the DCL_ASSEMBLER_NAME. */ | |
10071 | DECL_ASSEMBLER_NAME (decl) | |
10072 | = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type, | |
10073 | tparms, targs, | |
10074 | DECL_FUNCTION_MEMBER_P (decl) | |
10075 | + DECL_CONSTRUCTOR_P (decl)); | |
10076 | ||
10077 | /* Restore the previously active namespace. */ | |
10078 | current_namespace = saved_namespace; | |
10079 | } |