]>
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" |
49c249e1 | 42 | |
050367a3 MM |
43 | /* The type of functions taking a tree, and some additional data, and |
44 | returning an int. */ | |
45 | typedef int (*tree_fn_t) PROTO((tree, void*)); | |
46 | ||
8d08fdba | 47 | extern struct obstack permanent_obstack; |
8d08fdba MS |
48 | |
49 | extern int lineno; | |
50 | extern char *input_filename; | |
51 | struct pending_inline *pending_template_expansions; | |
52 | ||
5566b478 MS |
53 | tree current_template_parms; |
54 | HOST_WIDE_INT processing_template_decl; | |
8d08fdba | 55 | |
5566b478 MS |
56 | tree pending_templates; |
57 | static tree *template_tail = &pending_templates; | |
58 | ||
73aad9b9 JM |
59 | tree maybe_templates; |
60 | static tree *maybe_template_tail = &maybe_templates; | |
61 | ||
5566b478 | 62 | int minimal_parse_mode; |
75b0bbce | 63 | |
92eca640 | 64 | int processing_specialization; |
75650646 | 65 | int processing_explicit_instantiation; |
67ffc812 | 66 | int processing_template_parmlist; |
386b8a85 JM |
67 | static int template_header_count; |
68 | ||
75650646 MM |
69 | static tree saved_trees; |
70 | ||
8d08fdba MS |
71 | #define obstack_chunk_alloc xmalloc |
72 | #define obstack_chunk_free free | |
73 | ||
830bfa74 MM |
74 | #define UNIFY_ALLOW_NONE 0 |
75 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
76 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
77 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 78 | #define UNIFY_ALLOW_INTEGER 8 |
830bfa74 | 79 | |
4393e105 MM |
80 | #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is |
81 | virtual, or a base class of a virtual | |
82 | base. */ | |
83 | #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current | |
84 | type with the desired type. */ | |
85 | ||
34016c81 | 86 | static int resolve_overloaded_unification PROTO((tree, tree, tree, tree, |
4393e105 | 87 | unification_kind_t, int)); |
e97e5263 | 88 | static int try_one_overload PROTO((tree, tree, tree, tree, tree, |
4393e105 MM |
89 | unification_kind_t, int)); |
90 | static int unify PROTO((tree, tree, tree, tree, int)); | |
49c249e1 JM |
91 | static void add_pending_template PROTO((tree)); |
92 | static int push_tinst_level PROTO((tree)); | |
93 | static tree classtype_mangled_name PROTO((tree)); | |
36a117a5 | 94 | static char *mangle_class_name_for_template PROTO((char *, tree, tree)); |
49c249e1 | 95 | static tree tsubst_expr_values PROTO((tree, tree)); |
49c249e1 | 96 | static int list_eq PROTO((tree, tree)); |
36a117a5 | 97 | static tree get_class_bindings PROTO((tree, tree, tree)); |
744fac59 | 98 | static tree coerce_template_parms PROTO((tree, tree, tree, int, int)); |
dbfe2124 | 99 | static void tsubst_enum PROTO((tree, tree, tree)); |
98c1c668 | 100 | static tree add_to_template_args PROTO((tree, tree)); |
36a117a5 | 101 | static tree add_outermost_template_args PROTO((tree, tree)); |
830bfa74 MM |
102 | static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*, |
103 | tree*)); | |
050367a3 | 104 | static int type_unification_real PROTO((tree, tree, tree, tree, |
4393e105 | 105 | int, unification_kind_t, int)); |
386b8a85 | 106 | static void note_template_header PROTO((int)); |
840b09b4 | 107 | static tree maybe_fold_nontype_arg PROTO((tree)); |
e1467ff2 | 108 | static tree convert_nontype_argument PROTO((tree, tree)); |
8b5b8b7c MM |
109 | static tree convert_template_argument PROTO ((tree, tree, tree, int, |
110 | int , tree)); | |
76b9a14d | 111 | static tree get_bindings_overload PROTO((tree, tree, tree)); |
050367a3 | 112 | static int for_each_template_parm PROTO((tree, tree_fn_t, void*)); |
4b580446 | 113 | static tree build_template_parm_index PROTO((int, int, int, tree, tree)); |
69ac77ce JL |
114 | static int inline_needs_template_parms PROTO((tree)); |
115 | static void push_inline_template_parms_recursive PROTO((tree, int)); | |
116 | static tree retrieve_specialization PROTO((tree, tree)); | |
36a117a5 MM |
117 | static tree register_specialization PROTO((tree, tree, tree)); |
118 | static int unregister_specialization PROTO((tree, tree)); | |
69ac77ce JL |
119 | static tree reduce_template_parm_level PROTO((tree, tree, int)); |
120 | static tree build_template_decl PROTO((tree, tree)); | |
121 | static int mark_template_parm PROTO((tree, void *)); | |
122 | static tree tsubst_friend_function PROTO((tree, tree)); | |
6757edfe | 123 | static tree tsubst_friend_class PROTO((tree, tree)); |
69ac77ce | 124 | static tree get_bindings_real PROTO((tree, tree, tree, int)); |
04a81d94 | 125 | static int template_decl_level PROTO((tree)); |
a2b60a0e | 126 | static tree maybe_get_template_decl_from_type_decl PROTO((tree)); |
830bfa74 | 127 | static int check_cv_quals_for_unify PROTO((int, tree, tree)); |
4393e105 MM |
128 | static tree tsubst_template_arg_vector PROTO((tree, tree, int)); |
129 | static tree tsubst_template_parms PROTO((tree, tree, int)); | |
830bfa74 | 130 | static void regenerate_decl_from_template PROTO((tree, tree)); |
36a117a5 MM |
131 | static tree most_specialized PROTO((tree, tree, tree)); |
132 | static tree most_specialized_class PROTO((tree, tree)); | |
133 | static tree most_general_template PROTO((tree)); | |
134 | static void set_mangled_name_for_template_decl PROTO((tree)); | |
39c01e4c | 135 | static int template_class_depth_real PROTO((tree, int)); |
4393e105 | 136 | static tree tsubst_aggr_type PROTO((tree, tree, int, tree, int)); |
ae58fa02 | 137 | static tree tsubst_decl PROTO((tree, tree, tree, tree)); |
4393e105 MM |
138 | static tree tsubst_arg_types PROTO((tree, tree, int, tree)); |
139 | static tree tsubst_function_type PROTO((tree, tree, int, tree)); | |
6c30752f MM |
140 | static void check_specialization_scope PROTO((void)); |
141 | static tree process_partial_specialization PROTO((tree)); | |
61a127b3 | 142 | static void set_current_access_from_decl PROTO((tree)); |
6ba89f8e | 143 | static void check_default_tmpl_args PROTO((tree, tree, int, int)); |
4393e105 MM |
144 | static tree tsubst_call_declarator_parms PROTO((tree, tree, int, tree)); |
145 | static tree get_template_base_recursive PROTO((tree, tree, | |
146 | tree, tree, tree, int)); | |
00aeceec | 147 | static tree get_template_base PROTO((tree, tree, tree, tree)); |
4393e105 MM |
148 | static tree try_class_unification PROTO((tree, tree, tree, tree)); |
149 | static int coerce_template_template_parms PROTO((tree, tree, int, | |
150 | tree, tree)); | |
bf8f3f93 | 151 | static tree determine_specialization PROTO((tree, tree, tree *, int)); |
d8e178a0 KG |
152 | static int template_args_equal PROTO((tree, tree)); |
153 | static void print_template_context PROTO((int)); | |
36a117a5 MM |
154 | |
155 | /* We use TREE_VECs to hold template arguments. If there is only one | |
156 | level of template arguments, then the TREE_VEC contains the | |
157 | arguments directly. If there is more than one level of template | |
158 | arguments, then each entry in the TREE_VEC is itself a TREE_VEC, | |
159 | containing the template arguments for a single level. The first | |
160 | entry in the outer TREE_VEC is the outermost level of template | |
161 | parameters; the last is the innermost. | |
162 | ||
163 | It is incorrect to ever form a template argument vector containing | |
164 | only one level of arguments, but which is a TREE_VEC containing as | |
165 | its only entry the TREE_VEC for that level. */ | |
166 | ||
36a117a5 MM |
167 | /* Non-zero if the template arguments is actually a vector of vectors, |
168 | rather than just a vector. */ | |
169 | #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ | |
170 | (NODE != NULL_TREE \ | |
171 | && TREE_CODE (NODE) == TREE_VEC \ | |
172 | && TREE_VEC_LENGTH (NODE) > 0 \ | |
173 | && TREE_VEC_ELT (NODE, 0) != NULL_TREE \ | |
67ffc812 MM |
174 | && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) |
175 | ||
e4a84209 MM |
176 | /* The depth of a template argument vector. When called directly by |
177 | the parser, we use a TREE_LIST rather than a TREE_VEC to represent | |
178 | template arguments. In fact, we may even see NULL_TREE if there | |
179 | are no template arguments. In both of those cases, there is only | |
180 | one level of template arguments. */ | |
181 | #define TMPL_ARGS_DEPTH(NODE) \ | |
182 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1) | |
183 | ||
36a117a5 MM |
184 | /* The LEVELth level of the template ARGS. Note that template |
185 | parameter levels are indexed from 1, not from 0. */ | |
186 | #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ | |
187 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ | |
188 | ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS) | |
189 | ||
190 | /* Set the LEVELth level of the template ARGS to VAL. This macro does | |
191 | not work with single-level argument vectors. */ | |
192 | #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \ | |
193 | (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL)) | |
194 | ||
195 | /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */ | |
196 | #define TMPL_ARG(ARGS, LEVEL, IDX) \ | |
197 | (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX)) | |
198 | ||
199 | /* Set the IDXth element in the LEVELth level of ARGS to VAL. This | |
200 | macro does not work with single-level argument vectors. */ | |
201 | #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \ | |
202 | (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL)) | |
203 | ||
e4a84209 MM |
204 | /* Given a single level of template arguments in NODE, return the |
205 | number of arguments. */ | |
206 | #define NUM_TMPL_ARGS(NODE) \ | |
207 | ((NODE) == NULL_TREE ? 0 \ | |
208 | : (TREE_CODE (NODE) == TREE_VEC \ | |
209 | ? TREE_VEC_LENGTH (NODE) : list_length (NODE))) | |
210 | ||
36a117a5 MM |
211 | /* The number of levels of template parameters given by NODE. */ |
212 | #define TMPL_PARMS_DEPTH(NODE) \ | |
213 | (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE))) | |
214 | ||
e1467ff2 MM |
215 | /* Do any processing required when DECL (a member template declaration |
216 | using TEMPLATE_PARAMETERS as its innermost parameter list) is | |
217 | finished. Returns the TEMPLATE_DECL corresponding to DECL, unless | |
218 | it is a specialization, in which case the DECL itself is returned. */ | |
219 | ||
220 | tree | |
61a127b3 | 221 | finish_member_template_decl (decl) |
e1467ff2 MM |
222 | tree decl; |
223 | { | |
93cdc044 JM |
224 | if (decl == NULL_TREE || decl == void_type_node) |
225 | return NULL_TREE; | |
3ddfb0e6 MM |
226 | else if (decl == error_mark_node) |
227 | /* By returning NULL_TREE, the parser will just ignore this | |
228 | declaration. We have already issued the error. */ | |
229 | return NULL_TREE; | |
93cdc044 JM |
230 | else if (TREE_CODE (decl) == TREE_LIST) |
231 | { | |
5f311aec | 232 | /* Assume that the class is the only declspec. */ |
93cdc044 | 233 | decl = TREE_VALUE (decl); |
8d019cef JM |
234 | if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) |
235 | && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) | |
93cdc044 JM |
236 | { |
237 | tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); | |
238 | check_member_template (tmpl); | |
239 | return tmpl; | |
240 | } | |
8d019cef | 241 | return NULL_TREE; |
93cdc044 | 242 | } |
a1da6cba | 243 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 244 | { |
a1da6cba MM |
245 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
246 | { | |
247 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
248 | return DECL_TI_TEMPLATE (decl); | |
249 | } | |
250 | else | |
251 | return decl; | |
252 | } | |
253 | else | |
8251199e | 254 | cp_error ("invalid member template declaration `%D'", decl); |
e1467ff2 | 255 | |
a1da6cba | 256 | return error_mark_node; |
f84b4be9 | 257 | } |
e1467ff2 | 258 | |
f84b4be9 JM |
259 | /* Returns the template nesting level of the indicated class TYPE. |
260 | ||
261 | For example, in: | |
262 | template <class T> | |
263 | struct A | |
264 | { | |
265 | template <class U> | |
266 | struct B {}; | |
267 | }; | |
268 | ||
39c01e4c MM |
269 | A<T>::B<U> has depth two, while A<T> has depth one. |
270 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
271 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
272 | specializations. | |
273 | ||
274 | This function is guaranteed to return 0 if passed NULL_TREE so | |
275 | that, for example, `template_class_depth (current_class_type)' is | |
276 | always safe. */ | |
f84b4be9 | 277 | |
e9659ab0 | 278 | static int |
39c01e4c | 279 | template_class_depth_real (type, count_specializations) |
f84b4be9 | 280 | tree type; |
39c01e4c | 281 | int count_specializations; |
f84b4be9 | 282 | { |
93cdc044 | 283 | int depth; |
f84b4be9 | 284 | |
2c73f9f5 | 285 | for (depth = 0; |
ed44da02 MM |
286 | type && TREE_CODE (type) != NAMESPACE_DECL; |
287 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
288 | ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type)) | |
289 | { | |
290 | if (TREE_CODE (type) != FUNCTION_DECL) | |
291 | { | |
292 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
293 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
294 | && ((count_specializations | |
295 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
296 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) | |
297 | ++depth; | |
298 | } | |
299 | else | |
300 | { | |
301 | if (DECL_TEMPLATE_INFO (type) | |
302 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
303 | && ((count_specializations | |
304 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
305 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
306 | ++depth; | |
307 | } | |
308 | } | |
f84b4be9 JM |
309 | |
310 | return depth; | |
e1467ff2 | 311 | } |
98c1c668 | 312 | |
39c01e4c MM |
313 | /* Returns the template nesting level of the indicated class TYPE. |
314 | Like template_class_depth_real, but instantiations do not count in | |
315 | the depth. */ | |
316 | ||
317 | int | |
318 | template_class_depth (type) | |
319 | tree type; | |
320 | { | |
321 | return template_class_depth_real (type, /*count_specializations=*/0); | |
322 | } | |
323 | ||
cae40af6 JM |
324 | /* Returns 1 if processing DECL as part of do_pending_inlines |
325 | needs us to push template parms. */ | |
326 | ||
327 | static int | |
328 | inline_needs_template_parms (decl) | |
329 | tree decl; | |
330 | { | |
331 | if (! DECL_TEMPLATE_INFO (decl)) | |
332 | return 0; | |
f84b4be9 | 333 | |
36a117a5 | 334 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
335 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
336 | } | |
337 | ||
338 | /* Subroutine of maybe_begin_member_template_processing. | |
339 | Push the template parms in PARMS, starting from LEVELS steps into the | |
340 | chain, and ending at the beginning, since template parms are listed | |
341 | innermost first. */ | |
342 | ||
343 | static void | |
344 | push_inline_template_parms_recursive (parmlist, levels) | |
345 | tree parmlist; | |
346 | int levels; | |
347 | { | |
348 | tree parms = TREE_VALUE (parmlist); | |
349 | int i; | |
350 | ||
351 | if (levels > 1) | |
352 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 353 | |
98c1c668 | 354 | ++processing_template_decl; |
cae40af6 | 355 | current_template_parms |
98c1c668 JM |
356 | = tree_cons (build_int_2 (0, processing_template_decl), |
357 | parms, current_template_parms); | |
cae40af6 JM |
358 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
359 | ||
786b5245 | 360 | pushlevel (0); |
98c1c668 JM |
361 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
362 | { | |
786b5245 MM |
363 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
364 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0); | |
cae40af6 | 365 | |
98c1c668 JM |
366 | switch (TREE_CODE (parm)) |
367 | { | |
786b5245 | 368 | case TYPE_DECL: |
73b0fce8 | 369 | case TEMPLATE_DECL: |
98c1c668 JM |
370 | pushdecl (parm); |
371 | break; | |
786b5245 MM |
372 | |
373 | case PARM_DECL: | |
374 | { | |
fc03edb3 MM |
375 | /* Make a CONST_DECL as is done in process_template_parm. |
376 | It is ugly that we recreate this here; the original | |
377 | version built in process_template_parm is no longer | |
378 | available. */ | |
786b5245 MM |
379 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
380 | TREE_TYPE (parm)); | |
fc03edb3 | 381 | SET_DECL_ARTIFICIAL (decl); |
786b5245 | 382 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
fc03edb3 | 383 | DECL_TEMPLATE_PARM_P (decl) = 1; |
786b5245 MM |
384 | pushdecl (decl); |
385 | } | |
cae40af6 | 386 | break; |
786b5245 | 387 | |
98c1c668 JM |
388 | default: |
389 | my_friendly_abort (0); | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
cae40af6 JM |
394 | /* Restore the template parameter context for a member template or |
395 | a friend template defined in a class definition. */ | |
396 | ||
397 | void | |
398 | maybe_begin_member_template_processing (decl) | |
399 | tree decl; | |
400 | { | |
401 | tree parms; | |
402 | int levels; | |
403 | ||
404 | if (! inline_needs_template_parms (decl)) | |
405 | return; | |
406 | ||
36a117a5 | 407 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); |
cae40af6 | 408 | |
36a117a5 | 409 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; |
cae40af6 JM |
410 | |
411 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) | |
412 | { | |
413 | --levels; | |
414 | parms = TREE_CHAIN (parms); | |
415 | } | |
416 | ||
417 | push_inline_template_parms_recursive (parms, levels); | |
418 | } | |
419 | ||
98c1c668 JM |
420 | /* Undo the effects of begin_member_template_processing. */ |
421 | ||
422 | void | |
b370501f | 423 | maybe_end_member_template_processing () |
98c1c668 JM |
424 | { |
425 | if (! processing_template_decl) | |
426 | return; | |
427 | ||
cae40af6 JM |
428 | while (current_template_parms |
429 | && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
430 | { | |
431 | --processing_template_decl; | |
432 | current_template_parms = TREE_CHAIN (current_template_parms); | |
433 | poplevel (0, 0, 0); | |
434 | } | |
98c1c668 JM |
435 | } |
436 | ||
cae40af6 | 437 | /* Returns non-zero iff T is a member template function. We must be |
f84b4be9 | 438 | careful as in |
75650646 MM |
439 | |
440 | template <class T> class C { void f(); } | |
441 | ||
442 | Here, f is a template function, and a member, but not a member | |
443 | template. This function does not concern itself with the origin of | |
444 | T, only its present state. So if we have | |
445 | ||
446 | template <class T> class C { template <class U> void f(U); } | |
447 | ||
448 | then neither C<int>::f<char> nor C<T>::f<double> is considered | |
f181d4ae MM |
449 | to be a member template. But, `template <class U> void |
450 | C<int>::f(U)' is considered a member template. */ | |
98c1c668 | 451 | |
cae40af6 JM |
452 | int |
453 | is_member_template (t) | |
98c1c668 JM |
454 | tree t; |
455 | { | |
f181d4ae | 456 | if (!DECL_FUNCTION_TEMPLATE_P (t)) |
75650646 | 457 | /* Anything that isn't a function or a template function is |
aa5f3bad MM |
458 | certainly not a member template. */ |
459 | return 0; | |
460 | ||
cae40af6 JM |
461 | /* A local class can't have member templates. */ |
462 | if (hack_decl_function_context (t)) | |
463 | return 0; | |
464 | ||
f181d4ae | 465 | return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) |
f84b4be9 JM |
466 | /* If there are more levels of template parameters than |
467 | there are template classes surrounding the declaration, | |
468 | then we have a member template. */ | |
f181d4ae MM |
469 | && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
470 | template_class_depth (DECL_CLASS_CONTEXT (t)))); | |
f84b4be9 | 471 | } |
98c1c668 | 472 | |
ba4f4e5d | 473 | #if 0 /* UNUSED */ |
2ee4e084 MM |
474 | /* Returns non-zero iff T is a member template class. See |
475 | is_member_template for a description of what precisely constitutes | |
476 | a member template. */ | |
477 | ||
478 | int | |
479 | is_member_template_class (t) | |
480 | tree t; | |
481 | { | |
482 | if (!DECL_CLASS_TEMPLATE_P (t)) | |
483 | /* Anything that isn't a class template, is certainly not a member | |
484 | template. */ | |
485 | return 0; | |
486 | ||
487 | if (!DECL_CLASS_SCOPE_P (t)) | |
488 | /* Anything whose context isn't a class type is surely not a | |
489 | member template. */ | |
490 | return 0; | |
491 | ||
492 | /* If there are more levels of template parameters than there are | |
493 | template classes surrounding the declaration, then we have a | |
494 | member template. */ | |
36a117a5 | 495 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
2ee4e084 MM |
496 | template_class_depth (DECL_CONTEXT (t))); |
497 | } | |
ba4f4e5d | 498 | #endif |
2ee4e084 | 499 | |
36a117a5 MM |
500 | /* Return a new template argument vector which contains all of ARGS, |
501 | but has as its innermost set of arguments the EXTRA_ARGS. The | |
502 | resulting vector will be built on a temporary obstack, and so must | |
503 | be explicitly copied to the permanent obstack, if required. */ | |
e6f1275f JM |
504 | |
505 | static tree | |
36a117a5 MM |
506 | add_to_template_args (args, extra_args) |
507 | tree args; | |
508 | tree extra_args; | |
e6f1275f | 509 | { |
36a117a5 MM |
510 | tree new_args; |
511 | int extra_depth; | |
512 | int i; | |
513 | int j; | |
e6f1275f | 514 | |
36a117a5 MM |
515 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
516 | new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth); | |
e6f1275f | 517 | |
36a117a5 MM |
518 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
519 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 520 | |
36a117a5 MM |
521 | for (j = 1; j <= extra_depth; ++j, ++i) |
522 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
523 | ||
e6f1275f JM |
524 | return new_args; |
525 | } | |
526 | ||
36a117a5 MM |
527 | /* Like add_to_template_args, but only the outermost ARGS are added to |
528 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
529 | (EXTRA_ARGS) levels are added. This function is used to combine | |
530 | the template arguments from a partial instantiation with the | |
531 | template arguments used to attain the full instantiation from the | |
532 | partial instantiation. */ | |
98c1c668 | 533 | |
4966381a | 534 | static tree |
36a117a5 | 535 | add_outermost_template_args (args, extra_args) |
98c1c668 JM |
536 | tree args; |
537 | tree extra_args; | |
538 | { | |
539 | tree new_args; | |
540 | ||
e4a84209 MM |
541 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
542 | something very fishy is going on. */ | |
543 | my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), | |
544 | 0); | |
545 | ||
546 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
547 | them. */ | |
548 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
549 | return extra_args; | |
550 | ||
36a117a5 MM |
551 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
552 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
553 | ||
554 | new_args = add_to_template_args (args, extra_args); | |
98c1c668 | 555 | |
36a117a5 MM |
556 | /* Now, we restore ARGS to its full dimensions. */ |
557 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
558 | |
559 | return new_args; | |
560 | } | |
5566b478 MS |
561 | |
562 | /* We've got a template header coming up; push to a new level for storing | |
563 | the parms. */ | |
8d08fdba | 564 | |
8d08fdba MS |
565 | void |
566 | begin_template_parm_list () | |
567 | { | |
6757edfe MM |
568 | /* We use a non-tag-transparent scope here, which causes pushtag to |
569 | put tags in this scope, rather than in the enclosing class or | |
570 | namespace scope. This is the right thing, since we want | |
571 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
572 | global template class, push_template_decl handles putting the | |
573 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
574 | e.g.: | |
575 | ||
576 | template <class T> struct S1 { | |
577 | template <class T> struct S2 {}; | |
578 | }; | |
579 | ||
580 | pushtag contains special code to call pushdecl_with_scope on the | |
581 | TEMPLATE_DECL for S2. */ | |
8d08fdba | 582 | pushlevel (0); |
5566b478 | 583 | declare_pseudo_global_level (); |
5156628f | 584 | ++processing_template_decl; |
67ffc812 | 585 | ++processing_template_parmlist; |
386b8a85 JM |
586 | note_template_header (0); |
587 | } | |
588 | ||
6c30752f MM |
589 | /* This routine is called when a specialization is declared. If it is |
590 | illegal to declare a specialization here, an error is reported. */ | |
591 | ||
e9659ab0 | 592 | static void |
6c30752f MM |
593 | check_specialization_scope () |
594 | { | |
595 | tree scope = current_scope (); | |
3ddfb0e6 | 596 | |
6c30752f MM |
597 | /* [temp.expl.spec] |
598 | ||
599 | An explicit specialization shall be declared in the namespace of | |
600 | which the template is a member, or, for member templates, in the | |
601 | namespace of which the enclosing class or enclosing class | |
602 | template is a member. An explicit specialization of a member | |
603 | function, member class or static data member of a class template | |
604 | shall be declared in the namespace of which the class template | |
605 | is a member. */ | |
606 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
8251199e | 607 | cp_error ("explicit specialization in non-namespace scope `%D'", |
6c30752f | 608 | scope); |
3ddfb0e6 | 609 | |
6c30752f MM |
610 | /* [temp.expl.spec] |
611 | ||
612 | In an explicit specialization declaration for a member of a class | |
613 | template or a member template that appears in namespace scope, | |
614 | the member template and some of its enclosing class templates may | |
615 | remain unspecialized, except that the declaration shall not | |
616 | explicitly specialize a class member template if its enclosing | |
617 | class templates are not explicitly specialized as well. */ | |
618 | if (current_template_parms) | |
fd4de5ff | 619 | cp_error ("enclosing class templates are not explicitly specialized"); |
6c30752f MM |
620 | } |
621 | ||
386b8a85 JM |
622 | /* We've just seen template <>. */ |
623 | ||
624 | void | |
625 | begin_specialization () | |
626 | { | |
627 | note_template_header (1); | |
6c30752f | 628 | check_specialization_scope (); |
386b8a85 JM |
629 | } |
630 | ||
386b8a85 JM |
631 | /* Called at then end of processing a declaration preceeded by |
632 | template<>. */ | |
633 | ||
634 | void | |
635 | end_specialization () | |
636 | { | |
637 | reset_specialization (); | |
638 | } | |
639 | ||
386b8a85 JM |
640 | /* Any template <>'s that we have seen thus far are not referring to a |
641 | function specialization. */ | |
642 | ||
643 | void | |
644 | reset_specialization () | |
645 | { | |
646 | processing_specialization = 0; | |
647 | template_header_count = 0; | |
648 | } | |
649 | ||
386b8a85 JM |
650 | /* We've just seen a template header. If SPECIALIZATION is non-zero, |
651 | it was of the form template <>. */ | |
652 | ||
4966381a | 653 | static void |
386b8a85 JM |
654 | note_template_header (specialization) |
655 | int specialization; | |
656 | { | |
657 | processing_specialization = specialization; | |
658 | template_header_count++; | |
659 | } | |
660 | ||
75650646 | 661 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 662 | |
75650646 MM |
663 | void |
664 | begin_explicit_instantiation () | |
386b8a85 | 665 | { |
75650646 MM |
666 | ++processing_explicit_instantiation; |
667 | } | |
386b8a85 | 668 | |
386b8a85 | 669 | |
75650646 MM |
670 | void |
671 | end_explicit_instantiation () | |
672 | { | |
673 | my_friendly_assert(processing_explicit_instantiation > 0, 0); | |
674 | --processing_explicit_instantiation; | |
675 | } | |
386b8a85 | 676 | |
36a117a5 MM |
677 | /* The TYPE is being declared. If it is a template type, that means it |
678 | is a partial specialization. Do appropriate error-checking. */ | |
679 | ||
680 | void | |
681 | maybe_process_partial_specialization (type) | |
682 | tree type; | |
683 | { | |
684 | if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type)) | |
685 | { | |
686 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) | |
687 | && TYPE_SIZE (type) == NULL_TREE) | |
688 | { | |
700466c2 JM |
689 | if (current_namespace |
690 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
691 | { | |
692 | cp_pedwarn ("specializing `%#T' in different namespace", type); | |
693 | cp_pedwarn_at (" from definition of `%#D'", | |
694 | CLASSTYPE_TI_TEMPLATE (type)); | |
695 | } | |
36a117a5 MM |
696 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
697 | if (processing_template_decl) | |
698 | push_template_decl (TYPE_MAIN_DECL (type)); | |
699 | } | |
700 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
8251199e | 701 | cp_error ("specialization of `%T' after instantiation", type); |
36a117a5 | 702 | } |
20496fa2 JM |
703 | else if (processing_specialization) |
704 | cp_error ("explicit specialization of non-template `%T'", type); | |
36a117a5 MM |
705 | } |
706 | ||
75650646 MM |
707 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
708 | specialization is either an instantiation or an explicit | |
709 | specialization) of TMPL for the given template ARGS. If there is | |
710 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
711 | arguments, or a vector of vectors of arguments, in the case of | |
712 | templates with more than one level of parameters. */ | |
713 | ||
714 | static tree | |
715 | retrieve_specialization (tmpl, args) | |
716 | tree tmpl; | |
717 | tree args; | |
718 | { | |
719 | tree s; | |
720 | ||
721 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
722 | ||
36a117a5 MM |
723 | /* There should be as many levels of arguments as there are |
724 | levels of parameters. */ | |
725 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
726 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
727 | 0); | |
728 | ||
75650646 MM |
729 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
730 | s != NULL_TREE; | |
731 | s = TREE_CHAIN (s)) | |
732 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
733 | return TREE_VALUE (s); | |
734 | ||
735 | return NULL_TREE; | |
386b8a85 JM |
736 | } |
737 | ||
6757edfe MM |
738 | /* Returns non-zero iff DECL is a specialization of TMPL. */ |
739 | ||
740 | int | |
741 | is_specialization_of (decl, tmpl) | |
742 | tree decl; | |
743 | tree tmpl; | |
744 | { | |
745 | tree t; | |
746 | ||
747 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
748 | { | |
749 | for (t = decl; | |
750 | t != NULL_TREE; | |
751 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
752 | if (t == tmpl) | |
753 | return 1; | |
754 | } | |
755 | else | |
756 | { | |
757 | my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); | |
758 | ||
759 | for (t = TREE_TYPE (decl); | |
760 | t != NULL_TREE; | |
761 | t = CLASSTYPE_USE_TEMPLATE (t) | |
762 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
3bfdc719 MM |
763 | if (same_type_p (TYPE_MAIN_VARIANT (t), |
764 | TYPE_MAIN_VARIANT (TREE_TYPE (tmpl)))) | |
6757edfe MM |
765 | return 1; |
766 | } | |
386b8a85 | 767 | |
6757edfe MM |
768 | return 0; |
769 | } | |
75650646 MM |
770 | |
771 | /* Register the specialization SPEC as a specialization of TMPL with | |
36a117a5 MM |
772 | the indicated ARGS. Returns SPEC, or an equivalent prior |
773 | declaration, if available. */ | |
75650646 | 774 | |
36a117a5 | 775 | static tree |
75650646 MM |
776 | register_specialization (spec, tmpl, args) |
777 | tree spec; | |
778 | tree tmpl; | |
779 | tree args; | |
780 | { | |
781 | tree s; | |
782 | ||
783 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
784 | ||
36a117a5 MM |
785 | if (TREE_CODE (spec) == FUNCTION_DECL |
786 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
787 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
788 | register it; we want the corresponding TEMPLATE_DECL instead. | |
789 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
790 | the more obvious `uses_template_parms (spec)' to avoid problems | |
791 | with default function arguments. In particular, given | |
792 | something like this: | |
793 | ||
794 | template <class T> void f(T t1, T t = T()) | |
795 | ||
796 | the default argument expression is not substituted for in an | |
797 | instantiation unless and until it is actually needed. */ | |
798 | return spec; | |
75650646 | 799 | |
36a117a5 MM |
800 | /* There should be as many levels of arguments as there are |
801 | levels of parameters. */ | |
802 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
803 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
804 | 0); | |
805 | ||
75650646 MM |
806 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
807 | s != NULL_TREE; | |
808 | s = TREE_CHAIN (s)) | |
809 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
810 | { | |
811 | tree fn = TREE_VALUE (s); | |
812 | ||
813 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
814 | { | |
815 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
816 | { | |
817 | if (TREE_USED (fn) | |
818 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
819 | { | |
8251199e | 820 | cp_error ("specialization of %D after instantiation", |
75650646 | 821 | fn); |
36a117a5 | 822 | return spec; |
75650646 MM |
823 | } |
824 | else | |
825 | { | |
826 | /* This situation should occur only if the first | |
827 | specialization is an implicit instantiation, | |
828 | the second is an explicit specialization, and | |
829 | the implicit instantiation has not yet been | |
830 | used. That situation can occur if we have | |
36a117a5 MM |
831 | implicitly instantiated a member function and |
832 | then specialized it later. | |
833 | ||
834 | We can also wind up here if a friend | |
835 | declaration that looked like an instantiation | |
836 | turns out to be a specialization: | |
837 | ||
838 | template <class T> void foo(T); | |
839 | class S { friend void foo<>(int) }; | |
840 | template <> void foo(int); | |
841 | ||
842 | We transform the existing DECL in place so that | |
843 | any pointers to it become pointers to the | |
fbf1c34b MM |
844 | updated declaration. |
845 | ||
846 | If there was a definition for the template, but | |
847 | not for the specialization, we want this to | |
848 | look as if there is no definition, and vice | |
849 | versa. */ | |
850 | DECL_INITIAL (fn) = NULL_TREE; | |
851 | duplicate_decls (spec, fn); | |
852 | ||
853 | return fn; | |
75650646 MM |
854 | } |
855 | } | |
856 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
857 | { | |
fbf1c34b MM |
858 | duplicate_decls (spec, fn); |
859 | return fn; | |
75650646 MM |
860 | } |
861 | } | |
862 | } | |
863 | ||
864 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
865 | = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
866 | |
867 | return spec; | |
868 | } | |
869 | ||
870 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
871 | Returns nonzero if the SPEC was listed as a specialization of | |
872 | TMPL. */ | |
873 | ||
874 | static int | |
875 | unregister_specialization (spec, tmpl) | |
876 | tree spec; | |
877 | tree tmpl; | |
878 | { | |
879 | tree* s; | |
880 | ||
881 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
882 | *s != NULL_TREE; | |
883 | s = &TREE_CHAIN (*s)) | |
884 | if (TREE_VALUE (*s) == spec) | |
885 | { | |
886 | *s = TREE_CHAIN (*s); | |
887 | return 1; | |
888 | } | |
889 | ||
890 | return 0; | |
75650646 MM |
891 | } |
892 | ||
e1467ff2 MM |
893 | /* Print the list of candidate FNS in an error message. */ |
894 | ||
104bf76a | 895 | void |
e1467ff2 MM |
896 | print_candidates (fns) |
897 | tree fns; | |
898 | { | |
899 | tree fn; | |
900 | ||
d8e178a0 | 901 | const char *str = "candidates are:"; |
e1467ff2 MM |
902 | |
903 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
904 | { | |
d6479fe7 MM |
905 | tree f; |
906 | ||
907 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
908 | cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); | |
e1467ff2 MM |
909 | str = " "; |
910 | } | |
911 | } | |
912 | ||
75650646 | 913 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 914 | which can be specialized to match the indicated DECL with the |
03017874 MM |
915 | explicit template args given in TEMPLATE_ID. The DECL may be |
916 | NULL_TREE if none is available. In that case, the functions in | |
917 | TEMPLATE_ID are non-members. | |
918 | ||
919 | If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a | |
920 | specialization of a member template. | |
921 | ||
922 | The template args (those explicitly specified and those deduced) | |
923 | are output in a newly created vector *TARGS_OUT. | |
924 | ||
925 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 926 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 927 | |
e9659ab0 | 928 | static tree |
e1467ff2 | 929 | determine_specialization (template_id, decl, targs_out, |
bf8f3f93 | 930 | need_member_template) |
386b8a85 | 931 | tree template_id; |
e1467ff2 | 932 | tree decl; |
386b8a85 JM |
933 | tree* targs_out; |
934 | int need_member_template; | |
386b8a85 | 935 | { |
386b8a85 | 936 | tree fn; |
03017874 MM |
937 | tree fns; |
938 | tree targs; | |
939 | tree explicit_targs; | |
940 | tree candidates = NULL_TREE; | |
941 | tree templates = NULL_TREE; | |
386b8a85 | 942 | |
e1467ff2 MM |
943 | *targs_out = NULL_TREE; |
944 | ||
f2e48b67 BK |
945 | if (template_id == error_mark_node) |
946 | return error_mark_node; | |
947 | ||
948 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 949 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 950 | |
27fafc8d JM |
951 | if (fns == error_mark_node) |
952 | return error_mark_node; | |
953 | ||
2c73f9f5 ML |
954 | /* Check for baselinks. */ |
955 | if (TREE_CODE (fns) == TREE_LIST) | |
956 | fns = TREE_VALUE (fns); | |
386b8a85 | 957 | |
2c73f9f5 | 958 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 959 | { |
75650646 MM |
960 | tree tmpl; |
961 | ||
2c73f9f5 | 962 | fn = OVL_CURRENT (fns); |
03017874 MM |
963 | |
964 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
965 | /* DECL might be a specialization of FN. */ | |
966 | tmpl = fn; | |
967 | else if (need_member_template) | |
968 | /* FN is an ordinary member function, and we need a | |
969 | specialization of a member template. */ | |
970 | continue; | |
971 | else if (TREE_CODE (fn) != FUNCTION_DECL) | |
972 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
973 | cases. */ | |
974 | continue; | |
975 | else if (!DECL_FUNCTION_MEMBER_P (fn)) | |
976 | /* This is just an ordinary non-member function. Nothing can | |
977 | be a specialization of that. */ | |
978 | continue; | |
75650646 | 979 | else |
03017874 MM |
980 | { |
981 | tree decl_arg_types; | |
386b8a85 | 982 | |
03017874 MM |
983 | /* This is an ordinary member function. However, since |
984 | we're here, we can assume it's enclosing class is a | |
985 | template class. For example, | |
986 | ||
987 | template <typename T> struct S { void f(); }; | |
988 | template <> void S<int>::f() {} | |
989 | ||
990 | Here, S<int>::f is a non-template, but S<int> is a | |
991 | template class. If FN has the same type as DECL, we | |
992 | might be in business. */ | |
993 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), | |
994 | TREE_TYPE (TREE_TYPE (fn)))) | |
995 | /* The return types differ. */ | |
996 | continue; | |
997 | ||
998 | /* Adjust the type of DECL in case FN is a static member. */ | |
999 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1000 | if (DECL_STATIC_FUNCTION_P (fn) | |
1001 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1002 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1003 | ||
1004 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1005 | decl_arg_types)) | |
1006 | /* They match! */ | |
1007 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1008 | ||
1009 | continue; | |
1010 | } | |
386b8a85 | 1011 | |
bf8f3f93 MM |
1012 | /* See whether this function might be a specialization of this |
1013 | template. */ | |
1014 | targs = get_bindings (tmpl, decl, explicit_targs); | |
03017874 MM |
1015 | |
1016 | if (!targs) | |
bf8f3f93 | 1017 | /* We cannot deduce template arguments that when used to |
03017874 MM |
1018 | specialize TMPL will produce DECL. */ |
1019 | continue; | |
1020 | ||
1021 | /* Save this template, and the arguments deduced. */ | |
1022 | templates = scratch_tree_cons (targs, tmpl, templates); | |
386b8a85 | 1023 | } |
03017874 | 1024 | |
bf8f3f93 | 1025 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1026 | { |
03017874 MM |
1027 | /* We have: |
1028 | ||
1029 | [temp.expl.spec] | |
1030 | ||
1031 | It is possible for a specialization with a given function | |
1032 | signature to be instantiated from more than one function | |
1033 | template. In such cases, explicit specification of the | |
1034 | template arguments must be used to uniquely identify the | |
1035 | function template specialization being specialized. | |
1036 | ||
1037 | Note that here, there's no suggestion that we're supposed to | |
1038 | determine which of the candidate templates is most | |
1039 | specialized. However, we, also have: | |
1040 | ||
1041 | [temp.func.order] | |
1042 | ||
1043 | Partial ordering of overloaded function template | |
1044 | declarations is used in the following contexts to select | |
1045 | the function template to which a function template | |
1046 | specialization refers: | |
1047 | ||
1048 | -- when an explicit specialization refers to a function | |
1049 | template. | |
1050 | ||
1051 | So, we do use the partial ordering rules, at least for now. | |
1052 | This extension can only serve to make illegal programs legal, | |
1053 | so it's safe. And, there is strong anecdotal evidence that | |
1054 | the committee intended the partial ordering rules to apply; | |
1055 | the EDG front-end has that behavior, and John Spicer claims | |
1056 | that the committee simply forgot to delete the wording in | |
1057 | [temp.expl.spec]. */ | |
1058 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1059 | if (tmpl && tmpl != error_mark_node) | |
1060 | { | |
1061 | targs = get_bindings (tmpl, decl, explicit_targs); | |
1062 | templates = scratch_tree_cons (targs, tmpl, NULL_TREE); | |
1063 | } | |
e1467ff2 MM |
1064 | } |
1065 | ||
03017874 | 1066 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1067 | { |
bf8f3f93 MM |
1068 | cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", |
1069 | template_id, decl); | |
03017874 | 1070 | return error_mark_node; |
386b8a85 | 1071 | } |
03017874 | 1072 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1073 | || (candidates && TREE_CHAIN (candidates)) |
1074 | || (templates && candidates)) | |
386b8a85 | 1075 | { |
bf8f3f93 MM |
1076 | cp_error_at ("ambiguous template specialization `%D' for `%+D'", |
1077 | template_id, decl); | |
1078 | chainon (candidates, templates); | |
1079 | print_candidates (candidates); | |
03017874 | 1080 | return error_mark_node; |
386b8a85 JM |
1081 | } |
1082 | ||
1083 | /* We have one, and exactly one, match. */ | |
03017874 MM |
1084 | if (candidates) |
1085 | { | |
1086 | /* It was a specialization of an ordinary member function in a | |
1087 | template class. */ | |
1088 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1089 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1090 | } | |
1091 | ||
1092 | /* It was a specialization of a template. */ | |
1093 | targs = DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates))); | |
1094 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) | |
1095 | { | |
1096 | *targs_out = copy_node (targs); | |
1097 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1098 | TMPL_ARGS_DEPTH (*targs_out), | |
1099 | TREE_PURPOSE (templates)); | |
1100 | } | |
1101 | else | |
1102 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1103 | return TREE_VALUE (templates); |
8d08fdba | 1104 | } |
6757edfe | 1105 | |
386b8a85 | 1106 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1107 | DECLARATOR, and in DECL, is a specialization of a function |
1108 | template. We may also discover that the declaration is an explicit | |
1109 | instantiation at this point. | |
1110 | ||
e1467ff2 | 1111 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1112 | instead if all goes well. Issues an error message if something is |
1113 | amiss. Returns error_mark_node if the error is not easily | |
1114 | recoverable. | |
75650646 | 1115 | |
75650646 MM |
1116 | FLAGS is a bitmask consisting of the following flags: |
1117 | ||
75650646 MM |
1118 | 2: The function has a definition. |
1119 | 4: The function is a friend. | |
75650646 MM |
1120 | |
1121 | The TEMPLATE_COUNT is the number of references to qualifying | |
1122 | template classes that appeared in the name of the function. For | |
1123 | example, in | |
1124 | ||
1125 | template <class T> struct S { void f(); }; | |
1126 | void S<int>::f(); | |
1127 | ||
1128 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1129 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1130 | ||
1131 | template <class T> struct S {}; | |
1132 | template <> struct S<int> { void f(); } | |
36a117a5 | 1133 | template <> void S<int>::f(); |
75650646 MM |
1134 | |
1135 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
1136 | illegal; there should be no template <>.) | |
1137 | ||
1138 | If the function is a specialization, it is marked as such via | |
1139 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1140 | is set up correctly, and it is added to the list of specializations | |
1141 | for that template. */ | |
386b8a85 | 1142 | |
e1467ff2 | 1143 | tree |
27bb8339 | 1144 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
1145 | tree declarator; |
1146 | tree decl; | |
1147 | int template_count; | |
1148 | int flags; | |
1149 | { | |
75650646 MM |
1150 | int have_def = flags & 2; |
1151 | int is_friend = flags & 4; | |
1152 | int specialization = 0; | |
e1467ff2 | 1153 | int explicit_instantiation = 0; |
fd4de5ff | 1154 | int member_specialization = 0; |
75650646 MM |
1155 | |
1156 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
1157 | tree dname = DECL_NAME (decl); | |
386b8a85 | 1158 | |
fd4de5ff | 1159 | if (processing_specialization) |
386b8a85 | 1160 | { |
fd4de5ff | 1161 | /* The last template header was of the form template <>. */ |
75650646 | 1162 | |
fd4de5ff MM |
1163 | if (template_header_count > template_count) |
1164 | { | |
1165 | /* There were more template headers than qualifying template | |
1166 | classes. */ | |
1167 | if (template_header_count - template_count > 1) | |
03017874 MM |
1168 | /* There shouldn't be that many template parameter lists. |
1169 | There can be at most one parameter list for every | |
1170 | qualifying class, plus one for the function itself. */ | |
fd4de5ff | 1171 | cp_error ("too many template parameter lists in declaration of `%D'", decl); |
75650646 | 1172 | |
fd4de5ff MM |
1173 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
1174 | if (ctype) | |
1175 | member_specialization = 1; | |
1176 | else | |
1177 | specialization = 1; | |
75650646 | 1178 | } |
fd4de5ff | 1179 | else if (template_header_count == template_count) |
75650646 | 1180 | { |
fd4de5ff MM |
1181 | /* The counts are equal. So, this might be a |
1182 | specialization, but it is not a specialization of a | |
1183 | member template. It might be something like | |
1184 | ||
1185 | template <class T> struct S { | |
1186 | void f(int i); | |
1187 | }; | |
1188 | template <> | |
1189 | void S<int>::f(int i) {} */ | |
1190 | specialization = 1; | |
1191 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1192 | } |
fd4de5ff | 1193 | else |
386b8a85 | 1194 | { |
fd4de5ff MM |
1195 | /* This cannot be an explicit specialization. There are not |
1196 | enough headers for all of the qualifying classes. For | |
1197 | example, we might have: | |
1198 | ||
1199 | template <> | |
1200 | void S<int>::T<char>::f(); | |
75650646 | 1201 | |
fd4de5ff MM |
1202 | But, we're missing another template <>. */ |
1203 | cp_error("too few template parameter lists in declaration of `%D'", decl); | |
1204 | return decl; | |
1205 | } | |
1206 | } | |
1207 | else if (processing_explicit_instantiation) | |
1208 | { | |
1209 | if (template_header_count) | |
1210 | cp_error ("template parameter list used in explicit instantiation"); | |
1211 | ||
1212 | if (have_def) | |
1213 | cp_error ("definition provided for explicit instantiation"); | |
75650646 | 1214 | |
fd4de5ff MM |
1215 | explicit_instantiation = 1; |
1216 | } | |
1217 | else if (ctype != NULL_TREE | |
1218 | && !TYPE_BEING_DEFINED (ctype) | |
1219 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) | |
1220 | && !is_friend) | |
1221 | { | |
1222 | /* This case catches outdated code that looks like this: | |
1223 | ||
1224 | template <class T> struct S { void f(); }; | |
1225 | void S<int>::f() {} // Missing template <> | |
1226 | ||
1227 | We disable this check when the type is being defined to | |
1228 | avoid complaining about default compiler-generated | |
1229 | constructors, destructors, and assignment operators. | |
1230 | Since the type is an instantiation, not a specialization, | |
1231 | these are the only functions that can be defined before | |
1232 | the class is complete. */ | |
75650646 | 1233 | |
c6f2ed0d JM |
1234 | /* If they said |
1235 | template <class T> void S<int>::f() {} | |
1236 | that's bogus. */ | |
fd4de5ff MM |
1237 | if (template_header_count) |
1238 | { | |
1239 | cp_error ("template parameters specified in specialization"); | |
1240 | return decl; | |
c6f2ed0d | 1241 | } |
fd4de5ff MM |
1242 | |
1243 | if (pedantic) | |
1244 | cp_pedwarn | |
1245 | ("explicit specialization not preceded by `template <>'"); | |
1246 | specialization = 1; | |
1247 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1248 | } | |
1249 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
1250 | { | |
1251 | if (is_friend) | |
1252 | /* This could be something like: | |
1253 | ||
1254 | template <class T> void f(T); | |
1255 | class S { friend void f<>(int); } */ | |
1256 | specialization = 1; | |
1257 | else | |
c6f2ed0d | 1258 | { |
fd4de5ff MM |
1259 | /* This case handles bogus declarations like template <> |
1260 | template <class T> void f<int>(); */ | |
75650646 | 1261 | |
fd4de5ff MM |
1262 | cp_error ("template-id `%D' in declaration of primary template", |
1263 | declarator); | |
1264 | return decl; | |
386b8a85 | 1265 | } |
75650646 | 1266 | } |
386b8a85 | 1267 | |
670960ac JM |
1268 | if (specialization || member_specialization) |
1269 | { | |
1270 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1271 | for (; t; t = TREE_CHAIN (t)) | |
1272 | if (TREE_PURPOSE (t)) | |
1273 | { | |
8251199e JM |
1274 | cp_pedwarn |
1275 | ("default argument specified in explicit specialization"); | |
670960ac JM |
1276 | break; |
1277 | } | |
f3400fe2 JM |
1278 | if (current_lang_name == lang_name_c) |
1279 | cp_error ("template specialization with C linkage"); | |
670960ac JM |
1280 | } |
1281 | ||
e1467ff2 | 1282 | if (specialization || member_specialization || explicit_instantiation) |
75650646 | 1283 | { |
36a117a5 | 1284 | tree gen_tmpl; |
75650646 MM |
1285 | tree tmpl = NULL_TREE; |
1286 | tree targs = NULL_TREE; | |
75650646 MM |
1287 | |
1288 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1289 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1290 | { | |
1291 | tree fns; | |
1292 | ||
1293 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1294 | 0); | |
1295 | if (!ctype) | |
30394414 | 1296 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); |
386b8a85 JM |
1297 | else |
1298 | fns = dname; | |
1299 | ||
75650646 MM |
1300 | declarator = |
1301 | lookup_template_function (fns, NULL_TREE); | |
386b8a85 JM |
1302 | } |
1303 | ||
f2e48b67 BK |
1304 | if (declarator == error_mark_node) |
1305 | return error_mark_node; | |
1306 | ||
1307 | if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) | |
386b8a85 JM |
1308 | { |
1309 | /* A friend declaration. We can't do much, because we don't | |
1310 | know what this resolves to, yet. */ | |
1311 | my_friendly_assert (is_friend != 0, 0); | |
e1467ff2 | 1312 | my_friendly_assert (!explicit_instantiation, 0); |
386b8a85 | 1313 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
e1467ff2 | 1314 | return decl; |
386b8a85 JM |
1315 | } |
1316 | ||
75650646 MM |
1317 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1318 | { | |
8ec2ac34 | 1319 | if (!explicit_instantiation) |
6c30752f MM |
1320 | /* A specialization in class scope. This is illegal, |
1321 | but the error will already have been flagged by | |
1322 | check_specialization_scope. */ | |
1323 | return error_mark_node; | |
8ec2ac34 | 1324 | else |
b370501f KG |
1325 | { |
1326 | /* It's not legal to write an explicit instantiation in | |
1327 | class scope, e.g.: | |
8ec2ac34 | 1328 | |
b370501f | 1329 | class C { template void f(); } |
8ec2ac34 | 1330 | |
b370501f KG |
1331 | This case is caught by the parser. However, on |
1332 | something like: | |
8ec2ac34 | 1333 | |
b370501f | 1334 | template class C { void f(); }; |
8ec2ac34 | 1335 | |
b370501f KG |
1336 | (which is illegal) we can get here. The error will be |
1337 | issued later. */ | |
1338 | ; | |
1339 | } | |
8ec2ac34 | 1340 | |
e1467ff2 | 1341 | return decl; |
75650646 MM |
1342 | } |
1343 | else if (ctype != NULL_TREE | |
1344 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1345 | IDENTIFIER_NODE)) | |
386b8a85 | 1346 | { |
75650646 MM |
1347 | /* Find the list of functions in ctype that have the same |
1348 | name as the declared function. */ | |
1349 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
1350 | tree fns = NULL_TREE; |
1351 | int idx; | |
1352 | ||
75650646 MM |
1353 | if (name == constructor_name (ctype) |
1354 | || name == constructor_name_full (ctype)) | |
386b8a85 | 1355 | { |
75650646 MM |
1356 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1357 | ||
1358 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1359 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1360 | { | |
1361 | /* From [temp.expl.spec]: | |
e1467ff2 | 1362 | |
75650646 MM |
1363 | If such an explicit specialization for the member |
1364 | of a class template names an implicitly-declared | |
1365 | special member function (clause _special_), the | |
e1467ff2 MM |
1366 | program is ill-formed. |
1367 | ||
1368 | Similar language is found in [temp.explicit]. */ | |
8251199e | 1369 | cp_error ("specialization of implicitly-declared special member function"); |
03017874 | 1370 | return error_mark_node; |
75650646 | 1371 | } |
386b8a85 | 1372 | |
42da2fd8 | 1373 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1374 | } |
42da2fd8 | 1375 | |
03017874 MM |
1376 | if (!IDENTIFIER_TYPENAME_P (name)) |
1377 | { | |
1378 | idx = lookup_fnfields_1 (ctype, name); | |
1379 | if (idx >= 0) | |
1380 | fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); | |
1381 | } | |
1382 | else | |
1383 | { | |
1384 | tree methods; | |
1385 | ||
1386 | /* For a type-conversion operator, we cannot do a | |
1387 | name-based lookup. We might be looking for `operator | |
1388 | int' which will be a specialization of `operator T'. | |
1389 | So, we find *all* the conversion operators, and then | |
1390 | select from them. */ | |
1391 | fns = NULL_TREE; | |
1392 | ||
1393 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1394 | if (methods) | |
1395 | for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx) | |
1396 | { | |
1397 | tree ovl = TREE_VEC_ELT (methods, idx); | |
1398 | ||
1399 | if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) | |
1400 | /* There are no more conversion functions. */ | |
1401 | break; | |
1402 | ||
1403 | /* Glue all these conversion functions together | |
1404 | with those we already have. */ | |
1405 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1406 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1407 | } | |
1408 | } | |
1409 | ||
386b8a85 JM |
1410 | if (fns == NULL_TREE) |
1411 | { | |
8251199e | 1412 | cp_error ("no member function `%s' declared in `%T'", |
75650646 | 1413 | IDENTIFIER_POINTER (name), |
386b8a85 | 1414 | ctype); |
03017874 | 1415 | return error_mark_node; |
386b8a85 JM |
1416 | } |
1417 | else | |
1418 | TREE_OPERAND (declarator, 0) = fns; | |
1419 | } | |
75650646 | 1420 | |
e1467ff2 MM |
1421 | /* Figure out what exactly is being specialized at this point. |
1422 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1423 | member function, we cannot tell apriori whether the |
e1467ff2 | 1424 | instantiation is for a member template, or just a member |
36a117a5 MM |
1425 | function of a template class. Even if a member template is |
1426 | being instantiated, the member template arguments may be | |
1427 | elided if they can be deduced from the rest of the | |
1428 | declaration. */ | |
e1467ff2 MM |
1429 | tmpl = determine_specialization (declarator, decl, |
1430 | &targs, | |
bf8f3f93 | 1431 | member_specialization); |
386b8a85 | 1432 | |
03017874 MM |
1433 | if (!tmpl || tmpl == error_mark_node) |
1434 | /* We couldn't figure out what this declaration was | |
1435 | specializing. */ | |
1436 | return error_mark_node; | |
1437 | else | |
386b8a85 | 1438 | { |
36a117a5 MM |
1439 | gen_tmpl = most_general_template (tmpl); |
1440 | ||
e1467ff2 MM |
1441 | if (explicit_instantiation) |
1442 | { | |
03d0f4af MM |
1443 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
1444 | is done by do_decl_instantiation later. */ | |
36a117a5 | 1445 | decl = instantiate_template (tmpl, innermost_args (targs)); |
e1467ff2 MM |
1446 | return decl; |
1447 | } | |
36a117a5 MM |
1448 | |
1449 | /* If we though that the DECL was a member function, but it | |
1450 | turns out to be specializing a static member function, | |
1451 | make DECL a static member function as well. */ | |
1452 | if (DECL_STATIC_FUNCTION_P (tmpl) | |
1453 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
366c0f1e MM |
1454 | { |
1455 | revert_static_member_fn (&decl, 0, 0); | |
1456 | last_function_parms = TREE_CHAIN (last_function_parms); | |
1457 | } | |
e1467ff2 | 1458 | |
36a117a5 MM |
1459 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
1460 | DECL_TEMPLATE_INFO (decl) | |
1461 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
1462 | ||
e1467ff2 MM |
1463 | /* Mangle the function name appropriately. Note that we do |
1464 | not mangle specializations of non-template member | |
1465 | functions of template classes, e.g. with | |
36a117a5 | 1466 | |
e1467ff2 | 1467 | template <class T> struct S { void f(); } |
36a117a5 | 1468 | |
e1467ff2 | 1469 | and given the specialization |
36a117a5 | 1470 | |
e1467ff2 | 1471 | template <> void S<int>::f() {} |
36a117a5 | 1472 | |
e1467ff2 MM |
1473 | we do not mangle S<int>::f() here. That's because it's |
1474 | just an ordinary member function and doesn't need special | |
36a117a5 MM |
1475 | treatment. We do this here so that the ordinary, |
1476 | non-template, name-mangling algorith will not be used | |
1477 | later. */ | |
e1467ff2 | 1478 | if ((is_member_template (tmpl) || ctype == NULL_TREE) |
75650646 | 1479 | && name_mangling_version >= 1) |
36a117a5 | 1480 | set_mangled_name_for_template_decl (decl); |
386b8a85 JM |
1481 | |
1482 | if (is_friend && !have_def) | |
36a117a5 MM |
1483 | /* This is not really a declaration of a specialization. |
1484 | It's just the name of an instantiation. But, it's not | |
1485 | a request for an instantiation, either. */ | |
fbf1c34b | 1486 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
75650646 | 1487 | |
36a117a5 MM |
1488 | /* Register this specialization so that we can find it |
1489 | again. */ | |
1490 | decl = register_specialization (decl, gen_tmpl, targs); | |
386b8a85 JM |
1491 | } |
1492 | } | |
75650646 | 1493 | |
e1467ff2 | 1494 | return decl; |
386b8a85 | 1495 | } |
75650646 | 1496 | |
39c01e4c MM |
1497 | /* TYPE is being declared. Verify that the use of template headers |
1498 | and such is reasonable. Issue error messages if not. */ | |
1499 | ||
1500 | void | |
1501 | maybe_check_template_type (type) | |
1502 | tree type; | |
1503 | { | |
1504 | if (template_header_count) | |
1505 | { | |
1506 | /* We are in the scope of some `template <...>' header. */ | |
1507 | ||
1508 | int context_depth | |
1509 | = template_class_depth_real (TYPE_CONTEXT (type), | |
1510 | /*count_specializations=*/1); | |
1511 | ||
1512 | if (template_header_count <= context_depth) | |
1513 | /* This is OK; the template headers are for the context. We | |
1514 | are actually too lenient here; like | |
1515 | check_explicit_specialization we should consider the number | |
1516 | of template types included in the actual declaration. For | |
1517 | example, | |
1518 | ||
1519 | template <class T> struct S { | |
1520 | template <class U> template <class V> | |
1521 | struct I {}; | |
1522 | }; | |
1523 | ||
1524 | is illegal, but: | |
1525 | ||
1526 | template <class T> struct S { | |
1527 | template <class U> struct I; | |
1528 | }; | |
1529 | ||
1530 | template <class T> template <class U. | |
1531 | struct S<T>::I {}; | |
1532 | ||
1533 | is not. */ | |
1534 | ; | |
1535 | else if (template_header_count > context_depth + 1) | |
1536 | /* There are two many template parameter lists. */ | |
8251199e | 1537 | cp_error ("too many template parameter lists in declaration of `%T'", type); |
39c01e4c MM |
1538 | } |
1539 | } | |
1540 | ||
75650646 MM |
1541 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
1542 | parameters. These are represented in the same format used for | |
1543 | DECL_TEMPLATE_PARMS. */ | |
1544 | ||
1545 | int comp_template_parms (parms1, parms2) | |
1546 | tree parms1; | |
1547 | tree parms2; | |
1548 | { | |
1549 | tree p1; | |
1550 | tree p2; | |
1551 | ||
1552 | if (parms1 == parms2) | |
1553 | return 1; | |
1554 | ||
1555 | for (p1 = parms1, p2 = parms2; | |
1556 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1557 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1558 | { | |
1559 | tree t1 = TREE_VALUE (p1); | |
1560 | tree t2 = TREE_VALUE (p2); | |
1561 | int i; | |
1562 | ||
1563 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1564 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1565 | ||
1566 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1567 | return 0; | |
1568 | ||
1569 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1570 | { | |
1571 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1572 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1573 | ||
1574 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1575 | return 0; | |
1576 | ||
1577 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1578 | continue; | |
3bfdc719 | 1579 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
1580 | return 0; |
1581 | } | |
1582 | } | |
1583 | ||
1584 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1585 | /* One set of parameters has more parameters lists than the | |
1586 | other. */ | |
1587 | return 0; | |
1588 | ||
1589 | return 1; | |
1590 | } | |
1591 | ||
f3400fe2 JM |
1592 | /* Complain if DECL shadows a template parameter. |
1593 | ||
1594 | [temp.local]: A template-parameter shall not be redeclared within its | |
1595 | scope (including nested scopes). */ | |
1596 | ||
1597 | void | |
1598 | check_template_shadow (decl) | |
1599 | tree decl; | |
1600 | { | |
f181d4ae | 1601 | tree olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); |
f3400fe2 | 1602 | |
f181d4ae MM |
1603 | if (current_template_parms && olddecl) |
1604 | { | |
f3400fe2 JM |
1605 | /* We check for decl != olddecl to avoid bogus errors for using a |
1606 | name inside a class. We check TPFI to avoid duplicate errors for | |
1607 | inline member templates. */ | |
fc03edb3 | 1608 | if (decl != olddecl && DECL_TEMPLATE_PARM_P (olddecl) |
f3400fe2 JM |
1609 | && ! TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
1610 | { | |
1611 | cp_error_at ("declaration of `%#D'", decl); | |
1612 | cp_error_at (" shadows template parm `%#D'", olddecl); | |
1613 | } | |
1614 | } | |
1615 | } | |
22a7be53 | 1616 | |
f3400fe2 | 1617 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
1618 | ORIG_LEVEL, DECL, and TYPE. */ |
1619 | ||
1620 | static tree | |
1621 | build_template_parm_index (index, level, orig_level, decl, type) | |
1622 | int index; | |
1623 | int level; | |
1624 | int orig_level; | |
1625 | tree decl; | |
1626 | tree type; | |
1627 | { | |
1628 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1629 | TEMPLATE_PARM_IDX (t) = index; | |
1630 | TEMPLATE_PARM_LEVEL (t) = level; | |
1631 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1632 | TEMPLATE_PARM_DECL (t) = decl; | |
1633 | TREE_TYPE (t) = type; | |
1634 | ||
1635 | return t; | |
1636 | } | |
1637 | ||
f84b4be9 | 1638 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 1639 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
1640 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
1641 | new one is created. */ | |
1642 | ||
1643 | static tree | |
93cdc044 | 1644 | reduce_template_parm_level (index, type, levels) |
f84b4be9 JM |
1645 | tree index; |
1646 | tree type; | |
93cdc044 | 1647 | int levels; |
f84b4be9 JM |
1648 | { |
1649 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1650 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 1651 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 JM |
1652 | { |
1653 | tree decl | |
1654 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
1655 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
1656 | type); | |
1657 | tree t | |
1658 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 1659 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
1660 | TEMPLATE_PARM_ORIG_LEVEL (index), |
1661 | decl, type); | |
1662 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
1663 | |
1664 | /* Template template parameters need this. */ | |
1665 | DECL_TEMPLATE_PARMS (decl) | |
1666 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
1667 | } |
1668 | ||
1669 | return TEMPLATE_PARM_DESCENDANTS (index); | |
1670 | } | |
1671 | ||
8d08fdba | 1672 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 1673 | LIST being built. */ |
e92cc029 | 1674 | |
8d08fdba MS |
1675 | tree |
1676 | process_template_parm (list, next) | |
1677 | tree list, next; | |
1678 | { | |
1679 | tree parm; | |
1680 | tree decl = 0; | |
a292b002 | 1681 | tree defval; |
5566b478 | 1682 | int is_type, idx; |
f84b4be9 | 1683 | |
8d08fdba MS |
1684 | parm = next; |
1685 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
1686 | defval = TREE_PURPOSE (parm); |
1687 | parm = TREE_VALUE (parm); | |
1688 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
1689 | |
1690 | if (list) | |
1691 | { | |
1692 | tree p = TREE_VALUE (tree_last (list)); | |
1693 | ||
1694 | if (TREE_CODE (p) == TYPE_DECL) | |
1695 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
73b0fce8 KL |
1696 | else if (TREE_CODE (p) == TEMPLATE_DECL) |
1697 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p))); | |
5566b478 | 1698 | else |
f84b4be9 | 1699 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
1700 | ++idx; |
1701 | } | |
1702 | else | |
1703 | idx = 0; | |
1704 | ||
8d08fdba MS |
1705 | if (!is_type) |
1706 | { | |
a292b002 | 1707 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 1708 | /* is a const-param */ |
a292b002 | 1709 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 1710 | PARM, 0, NULL_TREE); |
d490621d MM |
1711 | |
1712 | /* [temp.param] | |
1713 | ||
1714 | The top-level cv-qualifiers on the template-parameter are | |
1715 | ignored when determining its type. */ | |
1716 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
1717 | ||
8d08fdba MS |
1718 | /* A template parameter is not modifiable. */ |
1719 | TREE_READONLY (parm) = 1; | |
c91a56d2 | 1720 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
410c2fba KL |
1721 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM |
1722 | && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE) | |
8d08fdba | 1723 | { |
8251199e | 1724 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
c91a56d2 MS |
1725 | TREE_TYPE (parm)); |
1726 | if (DECL_NAME (parm) == NULL_TREE) | |
8251199e | 1727 | error (" a template type parameter must begin with `class' or `typename'"); |
8d08fdba MS |
1728 | TREE_TYPE (parm) = void_type_node; |
1729 | } | |
37c46b43 MS |
1730 | else if (pedantic |
1731 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
1732 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
8251199e | 1733 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
eb66be0e | 1734 | TREE_TYPE (parm)); |
8d08fdba MS |
1735 | if (TREE_PERMANENT (parm) == 0) |
1736 | { | |
1737 | parm = copy_node (parm); | |
1738 | TREE_PERMANENT (parm) = 1; | |
1739 | } | |
8d08fdba | 1740 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
f84b4be9 JM |
1741 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
1742 | = build_template_parm_index (idx, processing_template_decl, | |
1743 | processing_template_decl, | |
1744 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
1745 | } |
1746 | else | |
1747 | { | |
73b0fce8 KL |
1748 | tree t; |
1749 | parm = TREE_VALUE (parm); | |
1750 | ||
1751 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
1752 | { | |
1753 | t = make_lang_type (TEMPLATE_TEMPLATE_PARM); | |
1754 | /* This is for distinguishing between real templates and template | |
1755 | template parameters */ | |
1756 | TREE_TYPE (parm) = t; | |
1757 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
1758 | decl = parm; | |
1759 | } | |
1760 | else | |
1761 | { | |
1762 | t = make_lang_type (TEMPLATE_TYPE_PARM); | |
1763 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ | |
1764 | decl = build_decl (TYPE_DECL, parm, t); | |
1765 | } | |
1766 | ||
d2e5ee5c MS |
1767 | TYPE_NAME (t) = decl; |
1768 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 1769 | parm = decl; |
f84b4be9 JM |
1770 | TEMPLATE_TYPE_PARM_INDEX (t) |
1771 | = build_template_parm_index (idx, processing_template_decl, | |
1772 | processing_template_decl, | |
1773 | decl, TREE_TYPE (parm)); | |
8d08fdba | 1774 | } |
8ccc31eb | 1775 | SET_DECL_ARTIFICIAL (decl); |
fc03edb3 | 1776 | DECL_TEMPLATE_PARM_P (decl) = 1; |
8d08fdba | 1777 | pushdecl (decl); |
a292b002 | 1778 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
1779 | return chainon (list, parm); |
1780 | } | |
1781 | ||
1782 | /* The end of a template parameter list has been reached. Process the | |
1783 | tree list into a parameter vector, converting each parameter into a more | |
1784 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
1785 | as PARM_DECLs. */ | |
1786 | ||
1787 | tree | |
1788 | end_template_parm_list (parms) | |
1789 | tree parms; | |
1790 | { | |
5566b478 | 1791 | int nparms; |
8d08fdba | 1792 | tree parm; |
5566b478 MS |
1793 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
1794 | ||
5566b478 MS |
1795 | current_template_parms |
1796 | = tree_cons (build_int_2 (0, processing_template_decl), | |
1797 | saved_parmlist, current_template_parms); | |
8d08fdba MS |
1798 | |
1799 | for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++) | |
5566b478 | 1800 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 1801 | |
67ffc812 MM |
1802 | --processing_template_parmlist; |
1803 | ||
8d08fdba MS |
1804 | return saved_parmlist; |
1805 | } | |
1806 | ||
5566b478 MS |
1807 | /* end_template_decl is called after a template declaration is seen. */ |
1808 | ||
8d08fdba | 1809 | void |
5566b478 | 1810 | end_template_decl () |
8d08fdba | 1811 | { |
386b8a85 JM |
1812 | reset_specialization (); |
1813 | ||
5156628f | 1814 | if (! processing_template_decl) |
73aad9b9 JM |
1815 | return; |
1816 | ||
5566b478 MS |
1817 | /* This matches the pushlevel in begin_template_parm_list. */ |
1818 | poplevel (0, 0, 0); | |
8d08fdba | 1819 | |
5566b478 MS |
1820 | --processing_template_decl; |
1821 | current_template_parms = TREE_CHAIN (current_template_parms); | |
1822 | (void) get_pending_sizes (); /* Why? */ | |
1823 | } | |
8d08fdba | 1824 | |
36a117a5 MM |
1825 | /* Given a template argument vector containing the template PARMS. |
1826 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
1827 | |
1828 | tree | |
1829 | current_template_args () | |
5566b478 | 1830 | { |
36a117a5 | 1831 | tree header; |
b370501f | 1832 | tree args = NULL_TREE; |
36a117a5 | 1833 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
1834 | int l = length; |
1835 | ||
36a117a5 MM |
1836 | /* If there is only one level of template parameters, we do not |
1837 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
1838 | TREE_VEC containing the arguments. */ | |
1839 | if (length > 1) | |
1840 | args = make_tree_vec (length); | |
1841 | ||
1842 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 1843 | { |
5566b478 | 1844 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
1845 | int i; |
1846 | ||
5566b478 | 1847 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 1848 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 1849 | { |
98c1c668 JM |
1850 | tree t = TREE_VEC_ELT (a, i); |
1851 | ||
36a117a5 | 1852 | /* T will be a list if we are called from within a |
98c1c668 JM |
1853 | begin/end_template_parm_list pair, but a vector directly |
1854 | if within a begin/end_member_template_processing pair. */ | |
1855 | if (TREE_CODE (t) == TREE_LIST) | |
1856 | { | |
1857 | t = TREE_VALUE (t); | |
1858 | ||
73b0fce8 KL |
1859 | if (TREE_CODE (t) == TYPE_DECL |
1860 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
1861 | t = TREE_TYPE (t); |
1862 | else | |
1863 | t = DECL_INITIAL (t); | |
36a117a5 | 1864 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 1865 | } |
5566b478 | 1866 | } |
36a117a5 MM |
1867 | |
1868 | if (length > 1) | |
1869 | TREE_VEC_ELT (args, --l) = a; | |
1870 | else | |
1871 | args = a; | |
8d08fdba MS |
1872 | } |
1873 | ||
9a3b49ac MS |
1874 | return args; |
1875 | } | |
75650646 | 1876 | |
e1467ff2 MM |
1877 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
1878 | template PARMS. Used by push_template_decl below. */ | |
1879 | ||
75650646 MM |
1880 | static tree |
1881 | build_template_decl (decl, parms) | |
1882 | tree decl; | |
1883 | tree parms; | |
1884 | { | |
1885 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
1886 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
1887 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
1888 | if (DECL_LANG_SPECIFIC (decl)) | |
1889 | { | |
1890 | DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); | |
1891 | DECL_STATIC_FUNCTION_P (tmpl) = | |
1892 | DECL_STATIC_FUNCTION_P (decl); | |
1893 | } | |
1894 | ||
1895 | return tmpl; | |
1896 | } | |
1897 | ||
050367a3 MM |
1898 | struct template_parm_data |
1899 | { | |
6c30752f MM |
1900 | /* The level of the template parameters we are currently |
1901 | processing. */ | |
050367a3 | 1902 | int level; |
6c30752f MM |
1903 | |
1904 | /* The index of the specialization argument we are currently | |
1905 | processing. */ | |
1906 | int current_arg; | |
1907 | ||
1908 | /* An array whose size is the number of template parameters. The | |
1909 | elements are non-zero if the parameter has been used in any one | |
1910 | of the arguments processed so far. */ | |
050367a3 | 1911 | int* parms; |
6c30752f MM |
1912 | |
1913 | /* An array whose size is the number of template arguments. The | |
1914 | elements are non-zero if the argument makes use of template | |
1915 | parameters of this level. */ | |
1916 | int* arg_uses_template_parms; | |
050367a3 MM |
1917 | }; |
1918 | ||
1919 | /* Subroutine of push_template_decl used to see if each template | |
1920 | parameter in a partial specialization is used in the explicit | |
1921 | argument list. If T is of the LEVEL given in DATA (which is | |
1922 | treated as a template_parm_data*), then DATA->PARMS is marked | |
1923 | appropriately. */ | |
1924 | ||
1925 | static int | |
1926 | mark_template_parm (t, data) | |
1927 | tree t; | |
1928 | void* data; | |
1929 | { | |
1930 | int level; | |
1931 | int idx; | |
1932 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
1933 | ||
1934 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
1935 | { | |
1936 | level = TEMPLATE_PARM_LEVEL (t); | |
1937 | idx = TEMPLATE_PARM_IDX (t); | |
1938 | } | |
1939 | else | |
1940 | { | |
1941 | level = TEMPLATE_TYPE_LEVEL (t); | |
1942 | idx = TEMPLATE_TYPE_IDX (t); | |
1943 | } | |
1944 | ||
1945 | if (level == tpd->level) | |
6c30752f MM |
1946 | { |
1947 | tpd->parms[idx] = 1; | |
1948 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
1949 | } | |
050367a3 MM |
1950 | |
1951 | /* Return zero so that for_each_template_parm will continue the | |
1952 | traversal of the tree; we want to mark *every* template parm. */ | |
1953 | return 0; | |
1954 | } | |
1955 | ||
6c30752f MM |
1956 | /* Process the partial specialization DECL. */ |
1957 | ||
e9659ab0 | 1958 | static tree |
6c30752f MM |
1959 | process_partial_specialization (decl) |
1960 | tree decl; | |
1961 | { | |
1962 | tree type = TREE_TYPE (decl); | |
1963 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
1964 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
1965 | tree inner_args = innermost_args (specargs); | |
1966 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
1967 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
1968 | int nargs = TREE_VEC_LENGTH (inner_args); | |
1969 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
1970 | int i; | |
1971 | int did_error_intro = 0; | |
6c30752f MM |
1972 | struct template_parm_data tpd; |
1973 | struct template_parm_data tpd2; | |
1974 | ||
6c30752f MM |
1975 | /* We check that each of the template parameters given in the |
1976 | partial specialization is used in the argument list to the | |
1977 | specialization. For example: | |
1978 | ||
1979 | template <class T> struct S; | |
1980 | template <class T> struct S<T*>; | |
1981 | ||
1982 | The second declaration is OK because `T*' uses the template | |
1983 | parameter T, whereas | |
1984 | ||
1985 | template <class T> struct S<int>; | |
1986 | ||
1987 | is no good. Even trickier is: | |
1988 | ||
1989 | template <class T> | |
1990 | struct S1 | |
1991 | { | |
1992 | template <class U> | |
1993 | struct S2; | |
1994 | template <class U> | |
1995 | struct S2<T>; | |
1996 | }; | |
1997 | ||
1998 | The S2<T> declaration is actually illegal; it is a | |
1999 | full-specialization. Of course, | |
2000 | ||
2001 | template <class U> | |
2002 | struct S2<T (*)(U)>; | |
2003 | ||
2004 | or some such would have been OK. */ | |
2005 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2006 | tpd.parms = alloca (sizeof (int) * ntparms); | |
1ddb2906 | 2007 | bzero ((PTR) tpd.parms, sizeof (int) * ntparms); |
6c30752f MM |
2008 | |
2009 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
1ddb2906 | 2010 | bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs); |
6c30752f MM |
2011 | for (i = 0; i < nargs; ++i) |
2012 | { | |
2013 | tpd.current_arg = i; | |
2014 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2015 | &mark_template_parm, | |
2016 | &tpd); | |
2017 | } | |
2018 | for (i = 0; i < ntparms; ++i) | |
2019 | if (tpd.parms[i] == 0) | |
2020 | { | |
2021 | /* One of the template parms was not used in the | |
2022 | specialization. */ | |
2023 | if (!did_error_intro) | |
2024 | { | |
8251199e | 2025 | cp_error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2026 | did_error_intro = 1; |
2027 | } | |
2028 | ||
8251199e | 2029 | cp_error (" `%D'", |
6c30752f MM |
2030 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
2031 | } | |
2032 | ||
2033 | /* [temp.class.spec] | |
2034 | ||
2035 | The argument list of the specialization shall not be identical to | |
2036 | the implicit argument list of the primary template. */ | |
2037 | if (comp_template_args (inner_args, | |
2038 | innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2039 | (maintmpl))))) | |
8251199e | 2040 | cp_error ("partial specialization `%T' does not specialize any template arguments", type); |
6c30752f MM |
2041 | |
2042 | /* [temp.class.spec] | |
2043 | ||
2044 | A partially specialized non-type argument expression shall not | |
2045 | involve template parameters of the partial specialization except | |
2046 | when the argument expression is a simple identifier. | |
2047 | ||
2048 | The type of a template parameter corresponding to a specialized | |
2049 | non-type argument shall not be dependent on a parameter of the | |
2050 | specialization. */ | |
2051 | my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); | |
2052 | tpd2.parms = 0; | |
2053 | for (i = 0; i < nargs; ++i) | |
2054 | { | |
2055 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2056 | if (/* These first two lines are the `non-type' bit. */ | |
2057 | TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
2058 | && TREE_CODE (arg) != TEMPLATE_DECL | |
2059 | /* This next line is the `argument expression is not just a | |
2060 | simple identifier' condition and also the `specialized | |
2061 | non-type argument' bit. */ | |
2062 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2063 | { | |
2064 | if (tpd.arg_uses_template_parms[i]) | |
8251199e | 2065 | cp_error ("template argument `%E' involves template parameter(s)", arg); |
6c30752f MM |
2066 | else |
2067 | { | |
2068 | /* Look at the corresponding template parameter, | |
2069 | marking which template parameters its type depends | |
2070 | upon. */ | |
2071 | tree type = | |
2072 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2073 | i))); | |
2074 | ||
2075 | if (!tpd2.parms) | |
2076 | { | |
2077 | /* We haven't yet initialized TPD2. Do so now. */ | |
2078 | tpd2.arg_uses_template_parms | |
2079 | = (int*) alloca (sizeof (int) * nargs); | |
104bf76a | 2080 | /* The number of parameters here is the number in the |
76a83782 RH |
2081 | main template, which, as checked in the assertion |
2082 | above, is NARGS. */ | |
6c30752f MM |
2083 | tpd2.parms = (int*) alloca (sizeof (int) * nargs); |
2084 | tpd2.level = | |
2085 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2086 | } | |
2087 | ||
104bf76a | 2088 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2089 | looking for the template parameters of the main |
2090 | template, not in the specialization. */ | |
2091 | tpd2.current_arg = i; | |
2092 | tpd2.arg_uses_template_parms[i] = 0; | |
1ddb2906 | 2093 | bzero ((PTR) tpd2.parms, sizeof (int) * nargs); |
6c30752f MM |
2094 | for_each_template_parm (type, |
2095 | &mark_template_parm, | |
2096 | &tpd2); | |
2097 | ||
2098 | if (tpd2.arg_uses_template_parms [i]) | |
2099 | { | |
2100 | /* The type depended on some template parameters. | |
2101 | If they are fully specialized in the | |
2102 | specialization, that's OK. */ | |
2103 | int j; | |
2104 | for (j = 0; j < nargs; ++j) | |
2105 | if (tpd2.parms[j] != 0 | |
2106 | && tpd.arg_uses_template_parms [j]) | |
2107 | { | |
104bf76a | 2108 | cp_error ("type `%T' of template argument `%E' depends on template parameter(s)", |
6c30752f MM |
2109 | type, |
2110 | arg); | |
2111 | break; | |
2112 | } | |
2113 | } | |
2114 | } | |
2115 | } | |
2116 | } | |
2117 | ||
2118 | if (retrieve_specialization (maintmpl, specargs)) | |
2119 | /* We've already got this specialization. */ | |
2120 | return decl; | |
2121 | ||
2122 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) | |
2123 | = perm_tree_cons (inner_args, inner_parms, | |
2124 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
2125 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; | |
2126 | return decl; | |
2127 | } | |
2128 | ||
6ba89f8e MM |
2129 | /* Check that a template declaration's use of default arguments is not |
2130 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
2131 | non-zero if DECL is the thing declared by a primary template. | |
2132 | IS_PARTIAL is non-zero if DECL is a partial specialization. */ | |
2133 | ||
2134 | static void | |
2135 | check_default_tmpl_args (decl, parms, is_primary, is_partial) | |
2136 | tree decl; | |
2137 | tree parms; | |
2138 | int is_primary; | |
2139 | int is_partial; | |
2140 | { | |
d8e178a0 | 2141 | const char *msg; |
6ba89f8e MM |
2142 | int last_level_to_check; |
2143 | ||
2144 | /* [temp.param] | |
2145 | ||
2146 | A default template-argument shall not be specified in a | |
2147 | function template declaration or a function template definition, nor | |
2148 | in the template-parameter-list of the definition of a member of a | |
2149 | class template. */ | |
2150 | ||
2151 | if (current_class_type | |
2152 | && !TYPE_BEING_DEFINED (current_class_type) | |
2153 | && DECL_REAL_CONTEXT (decl) == current_class_type | |
2154 | && DECL_DEFINED_IN_CLASS_P (decl)) | |
2155 | /* We already checked these parameters when the template was | |
2156 | declared, so there's no need to do it again now. This is an | |
2157 | inline member function definition. */ | |
2158 | return; | |
2159 | ||
2160 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) | |
2161 | /* For an ordinary class template, default template arguments are | |
2162 | allowed at the innermost level, e.g.: | |
2163 | template <class T = int> | |
2164 | struct S {}; | |
2165 | but, in a partial specialization, they're not allowed even | |
2166 | there, as we have in [temp.class.spec]: | |
2167 | ||
2168 | The template parameter list of a specialization shall not | |
2169 | contain default template argument values. | |
2170 | ||
2171 | So, for a partial specialization, or for a function template, | |
2172 | we look at all of them. */ | |
2173 | ; | |
2174 | else | |
2175 | /* But, for a primary class template that is not a partial | |
2176 | specialization we look at all template parameters except the | |
2177 | innermost ones. */ | |
2178 | parms = TREE_CHAIN (parms); | |
2179 | ||
2180 | /* Figure out what error message to issue. */ | |
2181 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2182 | msg = "default argument for template parameter in function template `%D'"; | |
2183 | else if (is_partial) | |
2184 | msg = "default argument in partial specialization `%D'"; | |
2185 | else | |
2186 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2187 | ||
2188 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2189 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2190 | examine the parameters to the class itself. On the one |
6ba89f8e MM |
2191 | hand, they will be checked when the class is defined, and, |
2192 | on the other, default arguments are legal in things like: | |
2193 | template <class T = double> | |
2194 | struct S { template <class U> void f(U); }; | |
2195 | Here the default argument for `S' has no bearing on the | |
2196 | declaration of `f'. */ | |
2197 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2198 | else | |
2199 | /* Check everything. */ | |
2200 | last_level_to_check = 0; | |
2201 | ||
2202 | for (; parms && TMPL_PARMS_DEPTH (parms) >= last_level_to_check; | |
2203 | parms = TREE_CHAIN (parms)) | |
2204 | { | |
2205 | tree inner_parms = TREE_VALUE (parms); | |
2206 | int i, ntparms; | |
2207 | ||
2208 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2209 | for (i = 0; i < ntparms; ++i) | |
2210 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2211 | { | |
2212 | if (msg) | |
2213 | { | |
2214 | cp_error (msg, decl); | |
2215 | msg = 0; | |
2216 | } | |
2217 | ||
2218 | /* Clear out the default argument so that we are not | |
2219 | confused later. */ | |
2220 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2221 | } | |
2222 | ||
2223 | /* At this point, if we're still interested in issuing messages, | |
2224 | they must apply to classes surrounding the object declared. */ | |
2225 | if (msg) | |
2226 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2227 | } | |
2228 | } | |
2229 | ||
3ac3d9ea | 2230 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2231 | parameters given by current_template_args, or reuses a |
2232 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2233 | equivalent one, if it is replaced via a call to duplicate_decls. |
2234 | ||
2235 | If IS_FRIEND is non-zero, DECL is a friend declaration. */ | |
3ac3d9ea MM |
2236 | |
2237 | tree | |
6757edfe | 2238 | push_template_decl_real (decl, is_friend) |
9a3b49ac | 2239 | tree decl; |
6757edfe | 2240 | int is_friend; |
9a3b49ac MS |
2241 | { |
2242 | tree tmpl; | |
f84b4be9 | 2243 | tree args; |
9a3b49ac | 2244 | tree info; |
f84b4be9 JM |
2245 | tree ctx; |
2246 | int primary; | |
6ba89f8e MM |
2247 | int is_partial; |
2248 | ||
2249 | /* See if this is a partial specialization. */ | |
2250 | is_partial = (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl) | |
2251 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE | |
2252 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); | |
6757edfe MM |
2253 | |
2254 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2255 | |
2256 | if (is_friend) | |
2257 | /* For a friend, we want the context of the friend function, not | |
2258 | the type of which it is a friend. */ | |
2259 | ctx = DECL_CONTEXT (decl); | |
2c73f9f5 ML |
2260 | else if (DECL_REAL_CONTEXT (decl) |
2261 | && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2262 | /* In the case of a virtual function, we want the class in which |
2263 | it is defined. */ | |
2264 | ctx = DECL_REAL_CONTEXT (decl); | |
2265 | else | |
2266 | /* Otherwise, if we're currently definining some class, the DECL | |
2267 | is assumed to be a member of the class. */ | |
2268 | ctx = current_class_type; | |
2269 | ||
2c73f9f5 ML |
2270 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2271 | ctx = NULL_TREE; | |
2272 | ||
2273 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2274 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2275 | |
93cdc044 JM |
2276 | /* For determining whether this is a primary template or not, we're really |
2277 | interested in the lexical context, not the true context. */ | |
2278 | if (is_friend) | |
36a117a5 | 2279 | info = current_class_type; |
93cdc044 JM |
2280 | else |
2281 | info = ctx; | |
2282 | ||
6ba89f8e | 2283 | /* See if this is a primary template. */ |
93cdc044 JM |
2284 | if (info && TREE_CODE (info) == FUNCTION_DECL) |
2285 | primary = 0; | |
6757edfe MM |
2286 | /* Note that template_class_depth returns 0 if given NULL_TREE, so |
2287 | this next line works even when we are at global scope. */ | |
2288 | else if (processing_template_decl > template_class_depth (info)) | |
9a3b49ac | 2289 | primary = 1; |
f84b4be9 JM |
2290 | else |
2291 | primary = 0; | |
9a3b49ac | 2292 | |
83566abf JM |
2293 | if (primary) |
2294 | { | |
2295 | if (current_lang_name == lang_name_c) | |
8251199e | 2296 | cp_error ("template with C linkage"); |
83566abf | 2297 | if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl))) |
8251199e | 2298 | cp_error ("template class without a name"); |
ed44da02 MM |
2299 | if (TREE_CODE (decl) == TYPE_DECL |
2300 | && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
8251199e | 2301 | cp_error ("template declaration of `%#T'", TREE_TYPE (decl)); |
83566abf JM |
2302 | } |
2303 | ||
6ba89f8e MM |
2304 | /* Check to see that the rules regarding the use of default |
2305 | arguments are not being violated. */ | |
2306 | check_default_tmpl_args (decl, current_template_parms, | |
2307 | primary, is_partial); | |
73aad9b9 | 2308 | |
6ba89f8e MM |
2309 | if (is_partial) |
2310 | return process_partial_specialization (decl); | |
d32789d8 | 2311 | |
9a3b49ac MS |
2312 | args = current_template_args (); |
2313 | ||
f84b4be9 JM |
2314 | if (!ctx |
2315 | || TREE_CODE (ctx) == FUNCTION_DECL | |
2316 | || TYPE_BEING_DEFINED (ctx) | |
2317 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
8d08fdba | 2318 | { |
75650646 | 2319 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
2320 | && DECL_TEMPLATE_INFO (decl) |
2321 | && DECL_TI_TEMPLATE (decl)) | |
2322 | tmpl = DECL_TI_TEMPLATE (decl); | |
2323 | else | |
786b5245 | 2324 | { |
f84b4be9 JM |
2325 | tmpl = build_template_decl (decl, current_template_parms); |
2326 | ||
2327 | if (DECL_LANG_SPECIFIC (decl) | |
2328 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2329 | { | |
2330 | /* A specialization of a member template of a template | |
2331 | class. */ | |
2332 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
2333 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
2334 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
2335 | } | |
786b5245 | 2336 | } |
8d08fdba MS |
2337 | } |
2338 | else | |
2339 | { | |
e1a5ccf7 | 2340 | tree a, t, current, parms; |
ba4f4e5d | 2341 | int i; |
6633d636 | 2342 | |
73aad9b9 | 2343 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
8251199e | 2344 | cp_error ("must specialize `%#T' before defining member `%#D'", |
73aad9b9 | 2345 | ctx, decl); |
6b9ab5cc MM |
2346 | if (TREE_CODE (decl) == TYPE_DECL) |
2347 | { | |
ed44da02 MM |
2348 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
2349 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
2350 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2351 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2352 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
2353 | else |
2354 | { | |
8251199e | 2355 | cp_error ("`%D' does not declare a template type", decl); |
6b9ab5cc MM |
2356 | return decl; |
2357 | } | |
2358 | } | |
fc378698 | 2359 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 2360 | { |
8251199e | 2361 | cp_error ("template definition of non-template `%#D'", decl); |
3ac3d9ea | 2362 | return decl; |
c91a56d2 | 2363 | } |
8d08fdba | 2364 | else |
5566b478 | 2365 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 2366 | |
e1a5ccf7 JM |
2367 | if (is_member_template (tmpl) |
2368 | && DECL_FUNCTION_TEMPLATE_P (tmpl) | |
2369 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) | |
2370 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
98c1c668 | 2371 | { |
e1a5ccf7 JM |
2372 | tree new_tmpl; |
2373 | ||
2374 | /* The declaration is a specialization of a member | |
2375 | template, declared outside the class. Therefore, the | |
2376 | innermost template arguments will be NULL, so we | |
2377 | replace them with the arguments determined by the | |
2378 | earlier call to check_explicit_specialization. */ | |
2379 | args = DECL_TI_ARGS (decl); | |
2380 | ||
2381 | new_tmpl | |
2382 | = build_template_decl (decl, current_template_parms); | |
2383 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
2384 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
2385 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
2386 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
2387 | DECL_TEMPLATE_INFO (new_tmpl) = | |
2388 | perm_tree_cons (tmpl, args, NULL_TREE); | |
2389 | ||
2390 | register_specialization (new_tmpl, tmpl, args); | |
2391 | return decl; | |
98c1c668 | 2392 | } |
98c1c668 | 2393 | |
e1a5ccf7 | 2394 | /* Make sure the template headers we got make sense. */ |
6633d636 | 2395 | |
e1a5ccf7 JM |
2396 | parms = DECL_TEMPLATE_PARMS (tmpl); |
2397 | i = TMPL_PARMS_DEPTH (parms); | |
2398 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 2399 | { |
e1a5ccf7 JM |
2400 | cp_error ("expected %d levels of template parms for `%#D', got %d", |
2401 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 2402 | } |
e1a5ccf7 JM |
2403 | else |
2404 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
2405 | { | |
2406 | a = TMPL_ARGS_LEVEL (args, i); | |
2407 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
2408 | ||
2409 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
2410 | { | |
2411 | if (current == decl) | |
2412 | cp_error ("got %d template parameters for `%#D'", | |
2413 | TREE_VEC_LENGTH (a), decl); | |
2414 | else | |
2415 | cp_error ("got %d template parameters for `%#T'", | |
2416 | TREE_VEC_LENGTH (a), current); | |
2417 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
2418 | } | |
98c1c668 | 2419 | |
e1a5ccf7 JM |
2420 | /* Perhaps we should also check that the parms are used in the |
2421 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 2422 | |
e1a5ccf7 JM |
2423 | if (current == decl) |
2424 | current = ctx; | |
2425 | else | |
2426 | current = TYPE_CONTEXT (current); | |
2427 | } | |
5566b478 | 2428 | } |
8d08fdba | 2429 | |
5566b478 MS |
2430 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
2431 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 2432 | |
36a117a5 MM |
2433 | /* Push template declarations for global functions and types. Note |
2434 | that we do not try to push a global template friend declared in a | |
2435 | template class; such a thing may well depend on the template | |
39c01e4c | 2436 | parameters of the class. */ |
36a117a5 MM |
2437 | if (! ctx |
2438 | && !(is_friend && template_class_depth (current_class_type) > 0)) | |
2c73f9f5 | 2439 | tmpl = pushdecl_namespace_level (tmpl); |
8d08fdba | 2440 | |
5566b478 | 2441 | if (primary) |
6757edfe | 2442 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
5566b478 MS |
2443 | |
2444 | info = perm_tree_cons (tmpl, args, NULL_TREE); | |
2445 | ||
824b9a4c | 2446 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
8d08fdba | 2447 | { |
ed44da02 MM |
2448 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2449 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
2450 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE) | |
75650646 | 2451 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 2452 | } |
ec255269 | 2453 | else if (! DECL_LANG_SPECIFIC (decl)) |
8251199e | 2454 | cp_error ("template declaration of `%#D'", decl); |
51c184be | 2455 | else |
5566b478 | 2456 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
2457 | |
2458 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
2459 | } |
2460 | ||
6757edfe MM |
2461 | tree |
2462 | push_template_decl (decl) | |
2463 | tree decl; | |
2464 | { | |
2465 | return push_template_decl_real (decl, 0); | |
2466 | } | |
2467 | ||
2468 | /* Called when a class template TYPE is redeclared with the indicated | |
2469 | template PARMS, e.g.: | |
7fe6899f MM |
2470 | |
2471 | template <class T> struct S; | |
2472 | template <class T> struct S {}; */ | |
2473 | ||
2474 | void | |
6757edfe | 2475 | redeclare_class_template (type, parms) |
7fe6899f | 2476 | tree type; |
6757edfe | 2477 | tree parms; |
7fe6899f MM |
2478 | { |
2479 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); | |
6757edfe | 2480 | tree tmpl_parms; |
7fe6899f MM |
2481 | int i; |
2482 | ||
2483 | if (!PRIMARY_TEMPLATE_P (tmpl)) | |
2484 | /* The type is nested in some template class. Nothing to worry | |
2485 | about here; there are no new template parameters for the nested | |
2486 | type. */ | |
2487 | return; | |
2488 | ||
6757edfe MM |
2489 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
2490 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
2491 | ||
7fe6899f MM |
2492 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
2493 | { | |
8251199e JM |
2494 | cp_error_at ("previous declaration `%D'", tmpl); |
2495 | cp_error ("used %d template parameter%s instead of %d", | |
7fe6899f MM |
2496 | TREE_VEC_LENGTH (tmpl_parms), |
2497 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
2498 | TREE_VEC_LENGTH (parms)); | |
2499 | return; | |
2500 | } | |
2501 | ||
2502 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
2503 | { | |
2504 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
2505 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
2506 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
2507 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
2508 | ||
2509 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
2510 | { | |
8251199e JM |
2511 | cp_error_at ("template parameter `%#D'", tmpl_parm); |
2512 | cp_error ("redeclared here as `%#D'", parm); | |
7fe6899f MM |
2513 | return; |
2514 | } | |
2515 | ||
2516 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
2517 | { | |
2518 | /* We have in [temp.param]: | |
2519 | ||
2520 | A template-parameter may not be given default arguments | |
2521 | by two different declarations in the same scope. */ | |
8251199e JM |
2522 | cp_error ("redefinition of default argument for `%#D'", parm); |
2523 | cp_error_at (" original definition appeared here", tmpl_parm); | |
7fe6899f MM |
2524 | return; |
2525 | } | |
2526 | ||
2527 | if (parm_default != NULL_TREE) | |
2528 | /* Update the previous template parameters (which are the ones | |
2529 | that will really count) with the new default value. */ | |
2530 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
2531 | } | |
2532 | } | |
75650646 | 2533 | |
75650646 MM |
2534 | /* Attempt to convert the non-type template parameter EXPR to the |
2535 | indicated TYPE. If the conversion is successful, return the | |
2536 | converted value. If the conversion is unsuccesful, return | |
2537 | NULL_TREE if we issued an error message, or error_mark_node if we | |
2538 | did not. We issue error messages for out-and-out bad template | |
2539 | parameters, but not simply because the conversion failed, since we | |
2540 | might be just trying to do argument deduction. By the time this | |
2541 | function is called, neither TYPE nor EXPR may make use of template | |
2542 | parameters. */ | |
2543 | ||
2544 | static tree | |
e1467ff2 | 2545 | convert_nontype_argument (type, expr) |
75650646 MM |
2546 | tree type; |
2547 | tree expr; | |
2548 | { | |
2549 | tree expr_type = TREE_TYPE (expr); | |
2550 | ||
2551 | /* A template-argument for a non-type, non-template | |
2552 | template-parameter shall be one of: | |
2553 | ||
2554 | --an integral constant-expression of integral or enumeration | |
2555 | type; or | |
2556 | ||
2557 | --the name of a non-type template-parameter; or | |
2558 | ||
2559 | --the name of an object or function with external linkage, | |
2560 | including function templates and function template-ids but | |
86052cc3 | 2561 | excluding non-static class members, expressed as id-expression; |
75650646 MM |
2562 | or |
2563 | ||
2564 | --the address of an object or function with external linkage, | |
2565 | including function templates and function template-ids but | |
2566 | excluding non-static class members, expressed as & id-expression | |
2567 | where the & is optional if the name refers to a function or | |
2568 | array; or | |
2569 | ||
2570 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
2571 | ||
86052cc3 JM |
2572 | /* An integral constant-expression can include const variables |
2573 | or enumerators. */ | |
2574 | if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr)) | |
2575 | expr = decl_constant_value (expr); | |
2576 | ||
7bf2682f MM |
2577 | if (is_overloaded_fn (expr)) |
2578 | /* OK for now. We'll check that it has external linkage later. | |
2579 | Check this first since if expr_type is the unknown_type_node | |
2580 | we would otherwise complain below. */ | |
2581 | ; | |
e08a8f45 MM |
2582 | else if (TYPE_PTRMEM_P (expr_type) |
2583 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2584 | { | |
2585 | if (TREE_CODE (expr) != PTRMEM_CST) | |
2586 | goto bad_argument; | |
2587 | } | |
f3400fe2 | 2588 | else if (TYPE_PTR_P (expr_type) |
e08a8f45 | 2589 | || TYPE_PTRMEM_P (expr_type) |
f3400fe2 JM |
2590 | || TREE_CODE (expr_type) == ARRAY_TYPE |
2591 | || TREE_CODE (type) == REFERENCE_TYPE | |
75650646 MM |
2592 | /* If expr is the address of an overloaded function, we |
2593 | will get the unknown_type_node at this point. */ | |
2594 | || expr_type == unknown_type_node) | |
2595 | { | |
2596 | tree referent; | |
49bf0d6f MM |
2597 | tree e = expr; |
2598 | STRIP_NOPS (e); | |
75650646 | 2599 | |
f3400fe2 JM |
2600 | if (TREE_CODE (type) == REFERENCE_TYPE |
2601 | || TREE_CODE (expr_type) == ARRAY_TYPE) | |
2602 | referent = e; | |
2603 | else | |
75650646 | 2604 | { |
f3400fe2 JM |
2605 | if (TREE_CODE (e) != ADDR_EXPR) |
2606 | { | |
2607 | bad_argument: | |
2608 | cp_error ("`%E' is not a valid template argument", expr); | |
e08a8f45 MM |
2609 | if (TYPE_PTR_P (expr_type)) |
2610 | { | |
2611 | if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) | |
2612 | cp_error ("it must be the address of a function with external linkage"); | |
2613 | else | |
2614 | cp_error ("it must be the address of an object with external linkage"); | |
2615 | } | |
2616 | else if (TYPE_PTRMEM_P (expr_type) | |
2617 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
2618 | cp_error ("it must be a pointer-to-member of the form `&X::Y'"); | |
2619 | ||
f3400fe2 JM |
2620 | return NULL_TREE; |
2621 | } | |
2622 | ||
2623 | referent = TREE_OPERAND (e, 0); | |
2624 | STRIP_NOPS (referent); | |
75650646 MM |
2625 | } |
2626 | ||
75650646 MM |
2627 | if (TREE_CODE (referent) == STRING_CST) |
2628 | { | |
8251199e | 2629 | cp_error ("string literal %E is not a valid template argument", |
75650646 | 2630 | referent); |
8251199e | 2631 | error ("because it is the address of an object with static linkage"); |
75650646 MM |
2632 | return NULL_TREE; |
2633 | } | |
2634 | ||
2635 | if (is_overloaded_fn (referent)) | |
2636 | /* We'll check that it has external linkage later. */ | |
2637 | ; | |
2638 | else if (TREE_CODE (referent) != VAR_DECL) | |
2639 | goto bad_argument; | |
2640 | else if (!TREE_PUBLIC (referent)) | |
2641 | { | |
8251199e | 2642 | cp_error ("address of non-extern `%E' cannot be used as template argument", referent); |
75650646 MM |
2643 | return error_mark_node; |
2644 | } | |
2645 | } | |
f3400fe2 JM |
2646 | else if (INTEGRAL_TYPE_P (expr_type) |
2647 | || TYPE_PTRMEM_P (expr_type) | |
2648 | || TYPE_PTRMEMFUNC_P (expr_type) | |
2649 | /* The next two are g++ extensions. */ | |
2650 | || TREE_CODE (expr_type) == REAL_TYPE | |
2651 | || TREE_CODE (expr_type) == COMPLEX_TYPE) | |
75650646 | 2652 | { |
f3400fe2 JM |
2653 | if (! TREE_CONSTANT (expr)) |
2654 | { | |
2655 | non_constant: | |
2656 | cp_error ("non-constant `%E' cannot be used as template argument", | |
2657 | expr); | |
2658 | return NULL_TREE; | |
2659 | } | |
75650646 | 2660 | } |
75650646 MM |
2661 | else |
2662 | { | |
8251199e | 2663 | cp_error ("object `%E' cannot be used as template argument", expr); |
75650646 MM |
2664 | return NULL_TREE; |
2665 | } | |
2666 | ||
2667 | switch (TREE_CODE (type)) | |
2668 | { | |
2669 | case INTEGER_TYPE: | |
2670 | case BOOLEAN_TYPE: | |
2671 | case ENUMERAL_TYPE: | |
2672 | /* For a non-type template-parameter of integral or enumeration | |
2673 | type, integral promotions (_conv.prom_) and integral | |
2674 | conversions (_conv.integral_) are applied. */ | |
2675 | if (!INTEGRAL_TYPE_P (expr_type)) | |
2676 | return error_mark_node; | |
2677 | ||
2678 | /* It's safe to call digest_init in this case; we know we're | |
2679 | just converting one integral constant expression to another. */ | |
db02b6b9 | 2680 | expr = digest_init (type, expr, (tree*) 0); |
75650646 | 2681 | |
db02b6b9 | 2682 | if (TREE_CODE (expr) != INTEGER_CST) |
f3400fe2 | 2683 | /* Curiously, some TREE_CONSTANT integral expressions do not |
db02b6b9 MM |
2684 | simplify to integer constants. For example, `3 % 0', |
2685 | remains a TRUNC_MOD_EXPR. */ | |
2686 | goto non_constant; | |
2687 | ||
2688 | return expr; | |
2689 | ||
abff8e06 JM |
2690 | case REAL_TYPE: |
2691 | case COMPLEX_TYPE: | |
2692 | /* These are g++ extensions. */ | |
2693 | if (TREE_CODE (expr_type) != TREE_CODE (type)) | |
2694 | return error_mark_node; | |
2695 | ||
db02b6b9 MM |
2696 | expr = digest_init (type, expr, (tree*) 0); |
2697 | ||
2698 | if (TREE_CODE (expr) != REAL_CST) | |
2699 | goto non_constant; | |
2700 | ||
2701 | return expr; | |
abff8e06 | 2702 | |
75650646 MM |
2703 | case POINTER_TYPE: |
2704 | { | |
2705 | tree type_pointed_to = TREE_TYPE (type); | |
2706 | ||
2707 | if (TYPE_PTRMEM_P (type)) | |
93aa462d MM |
2708 | { |
2709 | tree e; | |
2710 | ||
2711 | /* For a non-type template-parameter of type pointer to data | |
2712 | member, qualification conversions (_conv.qual_) are | |
2713 | applied. */ | |
2714 | e = perform_qualification_conversions (type, expr); | |
2715 | if (TREE_CODE (e) == NOP_EXPR) | |
2716 | { | |
2717 | /* The call to perform_qualification_conversions will | |
2718 | insert a NOP_EXPR over EXPR to do express | |
2719 | conversion, if necessary. But, that will confuse | |
2720 | us if we use this (converted) template parameter to | |
2721 | instantiate another template; then the thing will | |
2722 | not look like a valid template argument. So, just | |
2723 | make a new constant, of the appropriate type. */ | |
2724 | e = make_node (PTRMEM_CST); | |
2725 | TREE_TYPE (e) = type; | |
2726 | PTRMEM_CST_MEMBER (e) = PTRMEM_CST_MEMBER (expr); | |
2727 | } | |
2728 | return e; | |
2729 | } | |
75650646 MM |
2730 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) |
2731 | { | |
2732 | /* For a non-type template-parameter of type pointer to | |
2733 | function, only the function-to-pointer conversion | |
2734 | (_conv.func_) is applied. If the template-argument | |
2735 | represents a set of overloaded functions (or a pointer to | |
2736 | such), the matching function is selected from the set | |
2737 | (_over.over_). */ | |
2738 | tree fns; | |
2739 | tree fn; | |
2740 | ||
7bf2682f | 2741 | if (TREE_CODE (expr) == ADDR_EXPR) |
75650646 MM |
2742 | fns = TREE_OPERAND (expr, 0); |
2743 | else | |
2744 | fns = expr; | |
2745 | ||
a05273d4 | 2746 | fn = instantiate_type (type_pointed_to, fns, 0); |
75650646 MM |
2747 | |
2748 | if (fn == error_mark_node) | |
2749 | return error_mark_node; | |
2750 | ||
2751 | if (!TREE_PUBLIC (fn)) | |
2752 | { | |
2753 | if (really_overloaded_fn (fns)) | |
2754 | return error_mark_node; | |
2755 | else | |
2756 | goto bad_argument; | |
2757 | } | |
2758 | ||
2759 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2760 | ||
3bfdc719 | 2761 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2762 | 0); |
2763 | return expr; | |
2764 | } | |
2765 | else | |
2766 | { | |
2767 | /* For a non-type template-parameter of type pointer to | |
2768 | object, qualification conversions (_conv.qual_) and the | |
2769 | array-to-pointer conversion (_conv.array_) are applied. | |
2770 | [Note: In particular, neither the null pointer conversion | |
2771 | (_conv.ptr_) nor the derived-to-base conversion | |
2772 | (_conv.ptr_) are applied. Although 0 is a valid | |
2773 | template-argument for a non-type template-parameter of | |
2774 | integral type, it is not a valid template-argument for a | |
e1467ff2 MM |
2775 | non-type template-parameter of pointer type.] |
2776 | ||
2777 | The call to decay_conversion performs the | |
2778 | array-to-pointer conversion, if appropriate. */ | |
2779 | expr = decay_conversion (expr); | |
75650646 MM |
2780 | |
2781 | if (expr == error_mark_node) | |
2782 | return error_mark_node; | |
2783 | else | |
2784 | return perform_qualification_conversions (type, expr); | |
2785 | } | |
2786 | } | |
2787 | break; | |
2788 | ||
2789 | case REFERENCE_TYPE: | |
2790 | { | |
2791 | tree type_referred_to = TREE_TYPE (type); | |
2792 | ||
2793 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
2794 | { | |
2795 | /* For a non-type template-parameter of type reference to | |
39c01e4c MM |
2796 | function, no conversions apply. If the |
2797 | template-argument represents a set of overloaded | |
2798 | functions, the matching function is selected from the | |
2799 | set (_over.over_). */ | |
75650646 MM |
2800 | tree fns = expr; |
2801 | tree fn; | |
2802 | ||
a05273d4 | 2803 | fn = instantiate_type (type_referred_to, fns, 0); |
e6e174e5 JM |
2804 | |
2805 | if (fn == error_mark_node) | |
2806 | return error_mark_node; | |
75650646 MM |
2807 | |
2808 | if (!TREE_PUBLIC (fn)) | |
2809 | { | |
2810 | if (really_overloaded_fn (fns)) | |
2811 | /* Don't issue an error here; we might get a different | |
2812 | function if the overloading had worked out | |
2813 | differently. */ | |
2814 | return error_mark_node; | |
2815 | else | |
2816 | goto bad_argument; | |
2817 | } | |
2818 | ||
3bfdc719 MM |
2819 | my_friendly_assert (same_type_p (type_referred_to, |
2820 | TREE_TYPE (fn)), | |
75650646 MM |
2821 | 0); |
2822 | ||
2823 | return fn; | |
2824 | } | |
2825 | else | |
2826 | { | |
2827 | /* For a non-type template-parameter of type reference to | |
2828 | object, no conversions apply. The type referred to by the | |
2829 | reference may be more cv-qualified than the (otherwise | |
2830 | identical) type of the template-argument. The | |
2831 | template-parameter is bound directly to the | |
2832 | template-argument, which must be an lvalue. */ | |
ff350acd JM |
2833 | if ((TYPE_MAIN_VARIANT (expr_type) |
2834 | != TYPE_MAIN_VARIANT (type_referred_to)) | |
91063b51 MM |
2835 | || !at_least_as_qualified_p (type_referred_to, |
2836 | expr_type) | |
75650646 MM |
2837 | || !real_lvalue_p (expr)) |
2838 | return error_mark_node; | |
2839 | else | |
2840 | return expr; | |
2841 | } | |
2842 | } | |
2843 | break; | |
2844 | ||
2845 | case RECORD_TYPE: | |
2846 | { | |
410c2fba KL |
2847 | if (!TYPE_PTRMEMFUNC_P (type)) |
2848 | /* This handles templates like | |
2849 | template<class T, T t> void f(); | |
2850 | when T is substituted with any class. The second template | |
2851 | parameter becomes invalid and the template candidate is | |
2852 | rejected. */ | |
2853 | return error_mark_node; | |
75650646 MM |
2854 | |
2855 | /* For a non-type template-parameter of type pointer to member | |
2856 | function, no conversions apply. If the template-argument | |
2857 | represents a set of overloaded member functions, the | |
2858 | matching member function is selected from the set | |
2859 | (_over.over_). */ | |
2860 | ||
2861 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
2862 | expr_type != unknown_type_node) | |
2863 | return error_mark_node; | |
2864 | ||
e08a8f45 | 2865 | if (TREE_CODE (expr) == PTRMEM_CST) |
75650646 MM |
2866 | { |
2867 | /* A ptr-to-member constant. */ | |
3bfdc719 | 2868 | if (!same_type_p (type, expr_type)) |
75650646 MM |
2869 | return error_mark_node; |
2870 | else | |
2871 | return expr; | |
2872 | } | |
2873 | ||
2874 | if (TREE_CODE (expr) != ADDR_EXPR) | |
2875 | return error_mark_node; | |
2876 | ||
d8f8dca1 | 2877 | expr = instantiate_type (type, expr, 0); |
75650646 | 2878 | |
d8f8dca1 | 2879 | if (expr == error_mark_node) |
75650646 MM |
2880 | return error_mark_node; |
2881 | ||
3bfdc719 | 2882 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2883 | 0); |
2884 | return expr; | |
2885 | } | |
2886 | break; | |
2887 | ||
2888 | default: | |
2889 | /* All non-type parameters must have one of these types. */ | |
2890 | my_friendly_abort (0); | |
2891 | break; | |
2892 | } | |
2893 | ||
2894 | return error_mark_node; | |
2895 | } | |
2896 | ||
744fac59 KL |
2897 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
2898 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
2899 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
2900 | or PARM_DECL. | |
2901 | ||
2902 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
2903 | the case, then extra parameters must have default arguments. | |
2904 | ||
2905 | Consider the example: | |
2906 | template <class T, class Allocator = allocator> class vector; | |
2907 | template<template <class U> class TT> class C; | |
2908 | ||
2909 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
2910 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
2911 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
2912 | substitute the TT parameter. */ | |
2913 | ||
2914 | static int | |
4393e105 MM |
2915 | coerce_template_template_parms (parm_parms, arg_parms, complain, |
2916 | in_decl, outer_args) | |
2917 | tree parm_parms, arg_parms; | |
2918 | int complain; | |
2919 | tree in_decl, outer_args; | |
744fac59 KL |
2920 | { |
2921 | int nparms, nargs, i; | |
2922 | tree parm, arg; | |
2923 | ||
2924 | my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); | |
2925 | my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); | |
2926 | ||
2927 | nparms = TREE_VEC_LENGTH (parm_parms); | |
2928 | nargs = TREE_VEC_LENGTH (arg_parms); | |
2929 | ||
2930 | /* The rule here is opposite of coerce_template_parms. */ | |
2931 | if (nargs < nparms | |
2932 | || (nargs > nparms | |
2933 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
2934 | return 0; | |
2935 | ||
2936 | for (i = 0; i < nparms; ++i) | |
2937 | { | |
2938 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
2939 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
2940 | ||
2941 | if (arg == NULL_TREE || arg == error_mark_node | |
2942 | || parm == NULL_TREE || parm == error_mark_node) | |
2943 | return 0; | |
2944 | ||
2945 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
2946 | return 0; | |
2947 | ||
2948 | switch (TREE_CODE (parm)) | |
2949 | { | |
2950 | case TYPE_DECL: | |
2951 | break; | |
2952 | ||
2953 | case TEMPLATE_DECL: | |
2954 | /* We encounter instantiations of templates like | |
2955 | template <template <template <class> class> class TT> | |
2956 | class C; */ | |
700466c2 JM |
2957 | { |
2958 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
2959 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
2960 | ||
2961 | if (!coerce_template_template_parms (parmparm, argparm, | |
4393e105 MM |
2962 | complain, in_decl, |
2963 | outer_args)) | |
700466c2 JM |
2964 | return 0; |
2965 | } | |
2966 | break; | |
744fac59 KL |
2967 | |
2968 | case PARM_DECL: | |
2969 | /* The tsubst call is used to handle cases such as | |
2970 | template <class T, template <T> class TT> class D; | |
2971 | i.e. the parameter list of TT depends on earlier parameters. */ | |
4393e105 MM |
2972 | if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, |
2973 | complain, in_decl), | |
3bfdc719 | 2974 | TREE_TYPE (arg))) |
744fac59 KL |
2975 | return 0; |
2976 | break; | |
2977 | ||
2978 | default: | |
2979 | my_friendly_abort (0); | |
2980 | } | |
2981 | } | |
2982 | return 1; | |
2983 | } | |
2984 | ||
8b5b8b7c MM |
2985 | /* Convert the indicated template ARG as necessary to match the |
2986 | indicated template PARM. Returns the converted ARG, or | |
2987 | error_mark_node if the conversion was unsuccessful. Error messages | |
2988 | are issued if COMPLAIN is non-zero. This conversion is for the Ith | |
2989 | parameter in the parameter list. ARGS is the full set of template | |
2990 | arguments deduced so far. */ | |
2991 | ||
2992 | static tree | |
2993 | convert_template_argument (parm, arg, args, complain, i, in_decl) | |
2994 | tree parm; | |
2995 | tree arg; | |
2996 | tree args; | |
2997 | int complain; | |
2998 | int i; | |
2999 | tree in_decl; | |
3000 | { | |
3001 | tree val; | |
3002 | tree inner_args; | |
3003 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
3004 | ||
3005 | inner_args = innermost_args (args); | |
3006 | ||
3007 | if (TREE_CODE (arg) == TREE_LIST | |
3008 | && TREE_TYPE (arg) != NULL_TREE | |
3009 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
3010 | { | |
3011 | /* The template argument was the name of some | |
3012 | member function. That's usually | |
3013 | illegal, but static members are OK. In any | |
3014 | case, grab the underlying fields/functions | |
3015 | and issue an error later if required. */ | |
3016 | arg = TREE_VALUE (arg); | |
3017 | TREE_TYPE (arg) = unknown_type_node; | |
3018 | } | |
3019 | ||
3020 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3021 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3022 | || requires_tmpl_type); | |
3023 | ||
3024 | /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, | |
3025 | we also accept implicitly created TYPE_DECL as a valid argument. | |
3026 | This is necessary to handle the case where we pass a template name | |
3027 | to a template template parameter in a scope where we've derived from | |
3028 | in instantiation of that template, so the template name refers to that | |
3029 | instantiation. We really ought to handle this better. */ | |
3030 | is_tmpl_type | |
3031 | = ((TREE_CODE (arg) == TEMPLATE_DECL | |
3032 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3033 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 3034 | && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg)) |
8b5b8b7c MM |
3035 | || (TREE_CODE (arg) == RECORD_TYPE |
3036 | && CLASSTYPE_TEMPLATE_INFO (arg) | |
3037 | && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL | |
3038 | && DECL_ARTIFICIAL (TYPE_NAME (arg)) | |
3039 | && requires_tmpl_type | |
b54ccf71 | 3040 | && is_base_of_enclosing_class (arg, current_class_type))); |
8b5b8b7c MM |
3041 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
3042 | arg = TYPE_STUB_DECL (arg); | |
3043 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) | |
3044 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
3045 | ||
3046 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type; | |
3047 | ||
3048 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3049 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3050 | { | |
8251199e JM |
3051 | cp_pedwarn ("to refer to a type member of a template parameter,"); |
3052 | cp_pedwarn (" use `typename %E'", arg); | |
8b5b8b7c MM |
3053 | |
3054 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3055 | TREE_OPERAND (arg, 1)); | |
3056 | is_type = 1; | |
3057 | } | |
3058 | if (is_type != requires_type) | |
3059 | { | |
3060 | if (in_decl) | |
3061 | { | |
3062 | if (complain) | |
3063 | { | |
8251199e | 3064 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3065 | i + 1, in_decl); |
3066 | if (is_type) | |
8251199e | 3067 | cp_error (" expected a constant of type `%T', got `%T'", |
8b5b8b7c MM |
3068 | TREE_TYPE (parm), |
3069 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
3070 | else | |
8251199e | 3071 | cp_error (" expected a type, got `%E'", arg); |
8b5b8b7c MM |
3072 | } |
3073 | } | |
3074 | return error_mark_node; | |
3075 | } | |
3076 | if (is_tmpl_type ^ requires_tmpl_type) | |
3077 | { | |
3078 | if (in_decl && complain) | |
3079 | { | |
8251199e | 3080 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3081 | i + 1, in_decl); |
3082 | if (is_tmpl_type) | |
8251199e | 3083 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); |
8b5b8b7c | 3084 | else |
8251199e | 3085 | cp_error (" expected a class template, got `%T'", arg); |
8b5b8b7c MM |
3086 | } |
3087 | return error_mark_node; | |
3088 | } | |
3089 | ||
3090 | if (is_type) | |
3091 | { | |
3092 | if (requires_tmpl_type) | |
3093 | { | |
3094 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3095 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3096 | ||
4393e105 | 3097 | if (coerce_template_template_parms (parmparm, argparm, complain, |
8b5b8b7c MM |
3098 | in_decl, inner_args)) |
3099 | { | |
3100 | val = arg; | |
3101 | ||
3102 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3103 | TEMPLATE_DECL. */ | |
3104 | if (val != error_mark_node | |
3105 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3106 | val = TREE_TYPE (val); | |
3107 | } | |
3108 | else | |
3109 | { | |
3110 | if (in_decl && complain) | |
3111 | { | |
8251199e | 3112 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c | 3113 | i + 1, in_decl); |
8251199e | 3114 | cp_error (" expected a template of type `%D', got `%D'", parm, arg); |
8b5b8b7c MM |
3115 | } |
3116 | ||
3117 | val = error_mark_node; | |
3118 | } | |
3119 | } | |
3120 | else | |
3121 | { | |
3122 | val = groktypename (arg); | |
3123 | if (! processing_template_decl) | |
3124 | { | |
3125 | /* [basic.link]: A name with no linkage (notably, the | |
3126 | name of a class or enumeration declared in a local | |
3127 | scope) shall not be used to declare an entity with | |
3128 | linkage. This implies that names with no linkage | |
3129 | cannot be used as template arguments. */ | |
3130 | tree t = no_linkage_check (val); | |
3131 | if (t) | |
3132 | { | |
3133 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
8251199e JM |
3134 | cp_pedwarn |
3135 | ("template-argument `%T' uses anonymous type", val); | |
8b5b8b7c | 3136 | else |
8251199e JM |
3137 | cp_error |
3138 | ("template-argument `%T' uses local type `%T'", | |
8b5b8b7c MM |
3139 | val, t); |
3140 | return error_mark_node; | |
3141 | } | |
3142 | } | |
3143 | } | |
3144 | } | |
3145 | else | |
3146 | { | |
4393e105 | 3147 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c MM |
3148 | |
3149 | if (processing_template_decl) | |
3150 | arg = maybe_fold_nontype_arg (arg); | |
3151 | ||
3152 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
3153 | /* We used to call digest_init here. However, digest_init | |
3154 | will report errors, which we don't want when complain | |
3155 | is zero. More importantly, digest_init will try too | |
3156 | hard to convert things: for example, `0' should not be | |
3157 | converted to pointer type at this point according to | |
3158 | the standard. Accepting this is not merely an | |
3159 | extension, since deciding whether or not these | |
3160 | conversions can occur is part of determining which | |
3161 | function template to call, or whether a given epxlicit | |
3162 | argument specification is legal. */ | |
3163 | val = convert_nontype_argument (t, arg); | |
3164 | else | |
3165 | val = arg; | |
3166 | ||
3167 | if (val == NULL_TREE) | |
3168 | val = error_mark_node; | |
3169 | else if (val == error_mark_node && complain) | |
8251199e | 3170 | cp_error ("could not convert template argument `%E' to `%T'", |
8b5b8b7c MM |
3171 | arg, t); |
3172 | } | |
3173 | ||
3174 | return val; | |
3175 | } | |
3176 | ||
3177 | /* Convert all template arguments to their appropriate types, and | |
3178 | return a vector containing the innermost resulting template | |
3179 | arguments. If any error occurs, return error_mark_node, and, if | |
3180 | COMPLAIN is non-zero, issue an error message. Some error messages | |
3181 | are issued even if COMPLAIN is zero; for instance, if a template | |
3182 | argument is composed from a local class. | |
75650646 MM |
3183 | |
3184 | If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be | |
3185 | provided in ARGLIST, or else trailing parameters must have default | |
3186 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
cabc336a MM |
3187 | deduction for any unspecified trailing arguments. |
3188 | ||
3189 | The resulting TREE_VEC is allocated on a temporary obstack, and | |
3190 | must be explicitly copied if it will be permanent. */ | |
de575009 | 3191 | |
8d08fdba | 3192 | static tree |
8b5b8b7c | 3193 | coerce_template_parms (parms, args, in_decl, |
75650646 | 3194 | complain, |
744fac59 | 3195 | require_all_arguments) |
8b5b8b7c | 3196 | tree parms, args; |
8d08fdba | 3197 | tree in_decl; |
75650646 MM |
3198 | int complain; |
3199 | int require_all_arguments; | |
8d08fdba | 3200 | { |
a292b002 | 3201 | int nparms, nargs, i, lost = 0; |
e4a84209 | 3202 | tree inner_args; |
8b5b8b7c MM |
3203 | tree new_args; |
3204 | tree new_inner_args; | |
a292b002 | 3205 | |
8b5b8b7c | 3206 | inner_args = innermost_args (args); |
e4a84209 | 3207 | nargs = NUM_TMPL_ARGS (inner_args); |
a292b002 MS |
3208 | nparms = TREE_VEC_LENGTH (parms); |
3209 | ||
3210 | if (nargs > nparms | |
3211 | || (nargs < nparms | |
75650646 | 3212 | && require_all_arguments |
a292b002 | 3213 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3214 | { |
75650646 MM |
3215 | if (complain) |
3216 | { | |
8251199e | 3217 | cp_error ("wrong number of template arguments (%d, should be %d)", |
e4a84209 | 3218 | nargs, nparms); |
75650646 MM |
3219 | |
3220 | if (in_decl) | |
8251199e | 3221 | cp_error_at ("provided for `%D'", in_decl); |
75650646 MM |
3222 | } |
3223 | ||
8d08fdba MS |
3224 | return error_mark_node; |
3225 | } | |
3226 | ||
cabc336a | 3227 | new_inner_args = make_temp_vec (nparms); |
8b5b8b7c MM |
3228 | new_args = add_outermost_template_args (args, new_inner_args); |
3229 | for (i = 0; i < nparms; i++) | |
8d08fdba | 3230 | { |
8b5b8b7c MM |
3231 | tree arg; |
3232 | tree parm; | |
e4a84209 | 3233 | |
8b5b8b7c MM |
3234 | /* Get the Ith template parameter. */ |
3235 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 3236 | |
8b5b8b7c MM |
3237 | /* Calculate the Ith argument. */ |
3238 | if (inner_args && TREE_CODE (inner_args) == TREE_LIST) | |
8d08fdba | 3239 | { |
8b5b8b7c MM |
3240 | arg = TREE_VALUE (inner_args); |
3241 | inner_args = TREE_CHAIN (inner_args); | |
8d08fdba | 3242 | } |
8b5b8b7c MM |
3243 | else if (i < nargs) |
3244 | arg = TREE_VEC_ELT (inner_args, i); | |
3245 | /* If no template argument was supplied, look for a default | |
3246 | value. */ | |
3247 | else if (TREE_PURPOSE (parm) == NULL_TREE) | |
3248 | { | |
3249 | /* There was no default value. */ | |
3250 | my_friendly_assert (!require_all_arguments, 0); | |
3251 | break; | |
3252 | } | |
3253 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
4393e105 | 3254 | arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); |
8b5b8b7c | 3255 | else |
4393e105 MM |
3256 | arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, |
3257 | in_decl); | |
e4a84209 | 3258 | |
8b5b8b7c | 3259 | /* Now, convert the Ith argument, as necessary. */ |
75650646 MM |
3260 | if (arg == NULL_TREE) |
3261 | /* We're out of arguments. */ | |
3262 | { | |
3263 | my_friendly_assert (!require_all_arguments, 0); | |
3264 | break; | |
3265 | } | |
8b5b8b7c | 3266 | else if (arg == error_mark_node) |
75650646 | 3267 | { |
8251199e | 3268 | cp_error ("template argument %d is invalid", i + 1); |
8b5b8b7c | 3269 | arg = error_mark_node; |
8d08fdba | 3270 | } |
8b5b8b7c MM |
3271 | else |
3272 | arg = convert_template_argument (TREE_VALUE (parm), | |
3273 | arg, new_args, complain, i, | |
3274 | in_decl); | |
3275 | ||
3276 | if (arg == error_mark_node) | |
8d08fdba | 3277 | lost++; |
8b5b8b7c | 3278 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 3279 | } |
8b5b8b7c | 3280 | |
8d08fdba MS |
3281 | if (lost) |
3282 | return error_mark_node; | |
8b5b8b7c MM |
3283 | |
3284 | return new_inner_args; | |
8d08fdba MS |
3285 | } |
3286 | ||
34016c81 JM |
3287 | /* Returns 1 if template args OT and NT are equivalent. */ |
3288 | ||
d8e178a0 | 3289 | static int |
34016c81 JM |
3290 | template_args_equal (ot, nt) |
3291 | tree ot, nt; | |
3292 | { | |
3293 | if (nt == ot) | |
3294 | return 1; | |
3295 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
3296 | return 0; | |
3297 | if (TREE_CODE (nt) == TREE_VEC) | |
3298 | /* For member templates */ | |
3299 | return comp_template_args (ot, nt); | |
3300 | else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') | |
3bfdc719 | 3301 | return same_type_p (ot, nt); |
34016c81 JM |
3302 | else |
3303 | return (cp_tree_equal (ot, nt) > 0); | |
3304 | } | |
3305 | ||
3306 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
3307 | of template arguments. Returns 0 otherwise. */ |
3308 | ||
6757edfe | 3309 | int |
5566b478 MS |
3310 | comp_template_args (oldargs, newargs) |
3311 | tree oldargs, newargs; | |
3312 | { | |
3313 | int i; | |
3314 | ||
386b8a85 JM |
3315 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
3316 | return 0; | |
3317 | ||
5566b478 MS |
3318 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
3319 | { | |
3320 | tree nt = TREE_VEC_ELT (newargs, i); | |
3321 | tree ot = TREE_VEC_ELT (oldargs, i); | |
3322 | ||
34016c81 | 3323 | if (! template_args_equal (ot, nt)) |
61a127b3 | 3324 | return 0; |
5566b478 MS |
3325 | } |
3326 | return 1; | |
3327 | } | |
3328 | ||
8d08fdba MS |
3329 | /* Given class template name and parameter list, produce a user-friendly name |
3330 | for the instantiation. */ | |
e92cc029 | 3331 | |
8d08fdba | 3332 | static char * |
36a117a5 | 3333 | mangle_class_name_for_template (name, parms, arglist) |
8d08fdba MS |
3334 | char *name; |
3335 | tree parms, arglist; | |
3336 | { | |
3337 | static struct obstack scratch_obstack; | |
3338 | static char *scratch_firstobj; | |
3339 | int i, nparms; | |
8d08fdba MS |
3340 | |
3341 | if (!scratch_firstobj) | |
fc378698 | 3342 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
3343 | else |
3344 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 3345 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 3346 | |
8d08fdba | 3347 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); |
8d08fdba | 3348 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) |
8d08fdba MS |
3349 | |
3350 | cat (name); | |
3351 | ccat ('<'); | |
3352 | nparms = TREE_VEC_LENGTH (parms); | |
36a117a5 | 3353 | arglist = innermost_args (arglist); |
8d08fdba MS |
3354 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); |
3355 | for (i = 0; i < nparms; i++) | |
3356 | { | |
a292b002 MS |
3357 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
3358 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
3359 | |
3360 | if (i) | |
3361 | ccat (','); | |
3362 | ||
a292b002 | 3363 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 3364 | { |
294471c3 | 3365 | cat (type_as_string_real (arg, 0, 1)); |
8d08fdba MS |
3366 | continue; |
3367 | } | |
73b0fce8 KL |
3368 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
3369 | { | |
3370 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
3371 | { |
3372 | /* Already substituted with real template. Just output | |
3373 | the template name here */ | |
cb0dbb9a JM |
3374 | tree context = DECL_CONTEXT (arg); |
3375 | if (context) | |
2c73f9f5 | 3376 | { |
cb0dbb9a | 3377 | my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422); |
2c73f9f5 ML |
3378 | cat(decl_as_string (DECL_CONTEXT (arg), 0)); |
3379 | cat("::"); | |
3380 | } | |
3381 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3382 | } | |
73b0fce8 KL |
3383 | else |
3384 | /* Output the parameter declaration */ | |
294471c3 | 3385 | cat (type_as_string_real (arg, 0, 1)); |
73b0fce8 KL |
3386 | continue; |
3387 | } | |
8d08fdba MS |
3388 | else |
3389 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
3390 | ||
3391 | if (TREE_CODE (arg) == TREE_LIST) | |
3392 | { | |
3393 | /* New list cell was built because old chain link was in | |
3394 | use. */ | |
3395 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
3396 | arg = TREE_VALUE (arg); | |
3397 | } | |
3398 | /* No need to check arglist against parmlist here; we did that | |
3399 | in coerce_template_parms, called from lookup_template_class. */ | |
3400 | cat (expr_as_string (arg, 0)); | |
3401 | } | |
3402 | { | |
3403 | char *bufp = obstack_next_free (&scratch_obstack); | |
3404 | int offset = 0; | |
3405 | while (bufp[offset - 1] == ' ') | |
3406 | offset--; | |
3407 | obstack_blank_fast (&scratch_obstack, offset); | |
3408 | ||
3409 | /* B<C<char> >, not B<C<char>> */ | |
3410 | if (bufp[offset - 1] == '>') | |
3411 | ccat (' '); | |
3412 | } | |
3413 | ccat ('>'); | |
3414 | ccat ('\0'); | |
3415 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
3416 | } |
3417 | ||
bd6dd845 | 3418 | static tree |
5566b478 MS |
3419 | classtype_mangled_name (t) |
3420 | tree t; | |
3421 | { | |
3422 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
3423 | /* Specializations have already had their names set up in |
3424 | lookup_template_class. */ | |
9fbf56f7 MM |
3425 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
3426 | { | |
3427 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
3428 | ||
36a117a5 MM |
3429 | /* For non-primary templates, the template parameters are |
3430 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
3431 | if (PRIMARY_TEMPLATE_P (tmpl)) |
3432 | { | |
3433 | tree name = DECL_NAME (tmpl); | |
3434 | char *mangled_name = mangle_class_name_for_template | |
3435 | (IDENTIFIER_POINTER (name), | |
3436 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
3437 | CLASSTYPE_TI_ARGS (t)); | |
3438 | tree id = get_identifier (mangled_name); | |
3439 | IDENTIFIER_TEMPLATE (id) = name; | |
3440 | return id; | |
3441 | } | |
5566b478 | 3442 | } |
9fbf56f7 MM |
3443 | |
3444 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
3445 | } |
3446 | ||
3447 | static void | |
3448 | add_pending_template (d) | |
3449 | tree d; | |
3450 | { | |
e92cc029 MS |
3451 | tree ti; |
3452 | ||
3453 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
3454 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
3455 | else | |
3456 | ti = DECL_TEMPLATE_INFO (d); | |
3457 | ||
824b9a4c | 3458 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
3459 | return; |
3460 | ||
3461 | *template_tail = perm_tree_cons | |
1139b3d8 | 3462 | (build_srcloc_here (), d, NULL_TREE); |
5566b478 | 3463 | template_tail = &TREE_CHAIN (*template_tail); |
824b9a4c | 3464 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
3465 | } |
3466 | ||
386b8a85 JM |
3467 | |
3468 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
3469 | may be either a _DECL or an overloaded function or an | |
3470 | IDENTIFIER_NODE), and ARGLIST. */ | |
3471 | ||
3472 | tree | |
3473 | lookup_template_function (fns, arglist) | |
3474 | tree fns, arglist; | |
3475 | { | |
2c73f9f5 | 3476 | tree type; |
050367a3 | 3477 | |
386b8a85 JM |
3478 | if (fns == NULL_TREE) |
3479 | { | |
8251199e | 3480 | cp_error ("non-template used as template"); |
386b8a85 JM |
3481 | return error_mark_node; |
3482 | } | |
3483 | ||
2c73f9f5 ML |
3484 | type = TREE_TYPE (fns); |
3485 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
3486 | type = unknown_type_node; | |
3487 | ||
672476cb MM |
3488 | if (processing_template_decl) |
3489 | return build_min (TEMPLATE_ID_EXPR, type, fns, arglist); | |
3490 | else | |
3491 | return build (TEMPLATE_ID_EXPR, type, fns, arglist); | |
386b8a85 JM |
3492 | } |
3493 | ||
a2b60a0e MM |
3494 | /* Within the scope of a template class S<T>, the name S gets bound |
3495 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
3496 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
3497 | or one of its enclosing classes, and that type is a template, | |
3498 | return the associated TEMPLATE_DECL. Otherwise, the original | |
3499 | DECL is returned. */ | |
3500 | ||
e9659ab0 | 3501 | static tree |
a2b60a0e MM |
3502 | maybe_get_template_decl_from_type_decl (decl) |
3503 | tree decl; | |
3504 | { | |
3505 | return (decl != NULL_TREE | |
3506 | && TREE_CODE (decl) == TYPE_DECL | |
3507 | && DECL_ARTIFICIAL (decl) | |
3508 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) | |
3509 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
3510 | } | |
386b8a85 | 3511 | |
8d08fdba MS |
3512 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
3513 | parameters, find the desired type. | |
3514 | ||
3515 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
36a117a5 MM |
3516 | (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will |
3517 | be a TREE_LIST if called directly from the parser, and a TREE_VEC | |
3518 | otherwise.) Since ARGLIST is build on the decl_obstack, we must | |
3519 | copy it here to keep it from being reclaimed when the decl storage | |
3520 | is reclaimed. | |
8d08fdba MS |
3521 | |
3522 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
3523 | instantiate. |
3524 | ||
36a117a5 MM |
3525 | If ENTERING_SCOPE is non-zero, we are about to enter the scope of |
3526 | the class we are looking up. | |
3527 | ||
75650646 MM |
3528 | If the template class is really a local class in a template |
3529 | function, then the FUNCTION_CONTEXT is the function in which it is | |
3530 | being instantiated. */ | |
e92cc029 | 3531 | |
8d08fdba | 3532 | tree |
36a117a5 | 3533 | lookup_template_class (d1, arglist, in_decl, context, entering_scope) |
8d08fdba MS |
3534 | tree d1, arglist; |
3535 | tree in_decl; | |
e1467ff2 | 3536 | tree context; |
36a117a5 | 3537 | int entering_scope; |
8d08fdba | 3538 | { |
a703fb38 | 3539 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 3540 | tree t; |
5566b478 MS |
3541 | |
3542 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
3543 | { | |
f181d4ae MM |
3544 | if (IDENTIFIER_VALUE (d1) |
3545 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) | |
3546 | template = IDENTIFIER_VALUE (d1); | |
73b0fce8 KL |
3547 | else |
3548 | { | |
2c73f9f5 ML |
3549 | if (context) |
3550 | push_decl_namespace (context); | |
67ffc812 MM |
3551 | if (current_class_type != NULL_TREE) |
3552 | template = | |
3553 | maybe_get_template_decl_from_type_decl | |
3554 | (IDENTIFIER_CLASS_VALUE (d1)); | |
a2b60a0e | 3555 | if (template == NULL_TREE) |
2c73f9f5 ML |
3556 | template = lookup_name_nonclass (d1); |
3557 | if (context) | |
3558 | pop_decl_namespace (); | |
73b0fce8 | 3559 | } |
8d019cef JM |
3560 | if (template) |
3561 | context = DECL_CONTEXT (template); | |
5566b478 | 3562 | } |
c91a56d2 MS |
3563 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
3564 | { | |
f3400fe2 JM |
3565 | if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1))) |
3566 | { | |
3567 | template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1)); | |
3568 | d1 = DECL_NAME (template); | |
3569 | } | |
c91a56d2 | 3570 | } |
ed44da02 MM |
3571 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
3572 | || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' | |
3573 | && IS_AGGR_TYPE (d1))) | |
5566b478 | 3574 | { |
ed44da02 | 3575 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
3576 | d1 = DECL_NAME (template); |
3577 | } | |
93cdc044 JM |
3578 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
3579 | && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL) | |
3580 | { | |
3581 | template = d1; | |
3582 | d1 = DECL_NAME (template); | |
3583 | context = DECL_CONTEXT (template); | |
3584 | } | |
5566b478 MS |
3585 | else |
3586 | my_friendly_abort (272); | |
8d08fdba | 3587 | |
8d08fdba | 3588 | /* With something like `template <class T> class X class X { ... };' |
f181d4ae MM |
3589 | we could end up with D1 having nothing but an IDENTIFIER_VALUE. |
3590 | We don't want to do that, but we have to deal with the situation, | |
3591 | so let's give them some syntax errors to chew on instead of a | |
3592 | crash. */ | |
8d08fdba | 3593 | if (! template) |
f3400fe2 JM |
3594 | { |
3595 | cp_error ("`%T' is not a template", d1); | |
3596 | return error_mark_node; | |
3597 | } | |
2c73f9f5 | 3598 | |
c3baf4b5 JM |
3599 | if (context == NULL_TREE) |
3600 | context = global_namespace; | |
2c73f9f5 | 3601 | |
8d08fdba MS |
3602 | if (TREE_CODE (template) != TEMPLATE_DECL) |
3603 | { | |
8251199e | 3604 | cp_error ("non-template type `%T' used as a template", d1); |
8d08fdba | 3605 | if (in_decl) |
8251199e | 3606 | cp_error_at ("for template declaration `%D'", in_decl); |
8d08fdba MS |
3607 | return error_mark_node; |
3608 | } | |
8d08fdba | 3609 | |
73b0fce8 KL |
3610 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
3611 | { | |
3612 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
3613 | template arguments */ | |
3614 | ||
3615 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
3616 | tree template2 = TYPE_STUB_DECL (parm); | |
3617 | tree arglist2; | |
3618 | ||
73b0fce8 KL |
3619 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
3620 | ||
744fac59 | 3621 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1); |
73b0fce8 KL |
3622 | if (arglist2 == error_mark_node) |
3623 | return error_mark_node; | |
3624 | ||
3625 | arglist2 = copy_to_permanent (arglist2); | |
7ddedda4 | 3626 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm) |
73b0fce8 KL |
3627 | = perm_tree_cons (template2, arglist2, NULL_TREE); |
3628 | TYPE_SIZE (parm) = 0; | |
3629 | return parm; | |
3630 | } | |
36a117a5 | 3631 | else |
8d08fdba | 3632 | { |
36a117a5 MM |
3633 | tree template_type = TREE_TYPE (template); |
3634 | tree type_decl; | |
3635 | tree found = NULL_TREE; | |
3636 | int arg_depth; | |
3637 | int parm_depth; | |
dbfe2124 | 3638 | int is_partial_instantiation; |
830bfa74 | 3639 | |
36a117a5 MM |
3640 | template = most_general_template (template); |
3641 | parmlist = DECL_TEMPLATE_PARMS (template); | |
3642 | parm_depth = TMPL_PARMS_DEPTH (parmlist); | |
3643 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3644 | ||
7ddedda4 MM |
3645 | /* We build up the coerced arguments and such on the |
3646 | momentary_obstack. */ | |
3647 | push_momentary (); | |
3648 | ||
36a117a5 MM |
3649 | if (arg_depth == 1 && parm_depth > 1) |
3650 | { | |
39c01e4c | 3651 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
3652 | For example, given: |
3653 | ||
3654 | template <class T> struct S1 { | |
3655 | template <class U> struct S2 {}; | |
3656 | template <class U> struct S2<U*> {}; | |
3657 | }; | |
3658 | ||
3659 | we will be called with an ARGLIST of `U*', but the | |
3660 | TEMPLATE will be `template <class T> template | |
3661 | <class U> struct S1<T>::S2'. We must fill in the missing | |
3662 | arguments. */ | |
3663 | my_friendly_assert (context != NULL_TREE, 0); | |
39c01e4c MM |
3664 | while (!IS_AGGR_TYPE_CODE (TREE_CODE (context)) |
3665 | && context != global_namespace) | |
36a117a5 | 3666 | context = DECL_REAL_CONTEXT (context); |
39c01e4c MM |
3667 | |
3668 | if (context == global_namespace) | |
3669 | /* This is bad. We cannot get enough arguments, even from | |
3670 | the surrounding context, to resolve this class. One | |
3671 | case where this might happen is (illegal) code like: | |
3672 | ||
3673 | template <class U> | |
3674 | template <class T> | |
3675 | struct S { | |
3676 | A(const A<T>& a) {} | |
3677 | }; | |
3678 | ||
3679 | We should catch this error sooner (at the opening curly | |
3680 | for `S', but it is better to be safe than sorry here. */ | |
3681 | { | |
8251199e | 3682 | cp_error ("invalid use of `%D'", template); |
39c01e4c MM |
3683 | return error_mark_node; |
3684 | } | |
3685 | ||
ed44da02 | 3686 | arglist = add_to_template_args (TYPE_TI_ARGS (context), |
36a117a5 MM |
3687 | arglist); |
3688 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3689 | } | |
5566b478 | 3690 | |
36a117a5 MM |
3691 | my_friendly_assert (parm_depth == arg_depth, 0); |
3692 | ||
3693 | /* Calculate the BOUND_ARGS. These will be the args that are | |
3694 | actually tsubst'd into the definition to create the | |
3695 | instantiation. */ | |
3696 | if (parm_depth > 1) | |
830bfa74 MM |
3697 | { |
3698 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 3699 | int i; |
e4a84209 | 3700 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 3701 | |
cabc336a | 3702 | tree bound_args = make_temp_vec (parm_depth); |
830bfa74 | 3703 | |
e4a84209 | 3704 | for (i = saved_depth, |
830bfa74 | 3705 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 3706 | i > 0 && t != NULL_TREE; |
830bfa74 | 3707 | --i, t = TREE_CHAIN (t)) |
e4a84209 MM |
3708 | { |
3709 | tree a = coerce_template_parms (TREE_VALUE (t), | |
3710 | arglist, template, 1, 1); | |
3711 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); | |
3712 | ||
3713 | /* We temporarily reduce the length of the ARGLIST so | |
3714 | that coerce_template_parms will see only the arguments | |
3715 | corresponding to the template parameters it is | |
3716 | examining. */ | |
3717 | TREE_VEC_LENGTH (arglist)--; | |
3718 | } | |
3719 | ||
3720 | /* Restore the ARGLIST to its full size. */ | |
3721 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
3722 | ||
36a117a5 | 3723 | arglist = bound_args; |
830bfa74 MM |
3724 | } |
3725 | else | |
36a117a5 MM |
3726 | arglist |
3727 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
3728 | innermost_args (arglist), | |
3729 | template, 1, 1); | |
3730 | ||
3731 | if (arglist == error_mark_node) | |
3732 | /* We were unable to bind the arguments. */ | |
5566b478 | 3733 | return error_mark_node; |
5566b478 | 3734 | |
36a117a5 MM |
3735 | /* In the scope of a template class, explicit references to the |
3736 | template class refer to the type of the template, not any | |
3737 | instantiation of it. For example, in: | |
3738 | ||
3739 | template <class T> class C { void f(C<T>); } | |
3740 | ||
3741 | the `C<T>' is just the same as `C'. Outside of the | |
3742 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 3743 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
3744 | arglist)) |
3745 | { | |
3746 | found = template_type; | |
3747 | ||
3748 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 3749 | { |
36a117a5 MM |
3750 | tree ctx; |
3751 | ||
3752 | /* Note that we use DECL_CONTEXT, rather than | |
3753 | CP_DECL_CONTEXT, so that the termination test is | |
3754 | always just `ctx'. We're not interested in namepace | |
3755 | scopes. */ | |
3756 | for (ctx = current_class_type; | |
3757 | ctx; | |
3758 | ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't') | |
3759 | ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) | |
3bfdc719 | 3760 | if (same_type_p (ctx, template_type)) |
36a117a5 MM |
3761 | break; |
3762 | ||
3763 | if (!ctx) | |
3764 | /* We're not in the scope of the class, so the | |
3765 | TEMPLATE_TYPE is not the type we want after | |
3766 | all. */ | |
3767 | found = NULL_TREE; | |
5566b478 MS |
3768 | } |
3769 | } | |
36a117a5 MM |
3770 | |
3771 | if (!found) | |
75650646 | 3772 | { |
36a117a5 MM |
3773 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); |
3774 | found; found = TREE_CHAIN (found)) | |
3775 | if (comp_template_args (TREE_PURPOSE (found), arglist)) | |
3776 | break; | |
75650646 | 3777 | |
36a117a5 MM |
3778 | if (found) |
3779 | found = TREE_VALUE (found); | |
8d08fdba | 3780 | } |
36a117a5 MM |
3781 | |
3782 | if (found) | |
8d08fdba | 3783 | { |
7ddedda4 | 3784 | pop_momentary (); |
36a117a5 | 3785 | return found; |
8d08fdba | 3786 | } |
5566b478 | 3787 | |
36a117a5 MM |
3788 | /* Since we didn't find the type, we'll have to create it. |
3789 | Since we'll be saving this type on the | |
3790 | DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */ | |
3791 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
3792 | ||
dbfe2124 | 3793 | /* This type is a "partial instantiation" if any of the template |
486e4077 MM |
3794 | arguments still inolve template parameters. Note that we set |
3795 | IS_PARTIAL_INSTANTIATION for partial specializations as | |
3796 | well. */ | |
dbfe2124 MM |
3797 | is_partial_instantiation = uses_template_parms (arglist); |
3798 | ||
36a117a5 | 3799 | /* Create the type. */ |
ed44da02 MM |
3800 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
3801 | { | |
dbfe2124 | 3802 | if (!is_partial_instantiation) |
61a127b3 | 3803 | t = start_enum (TYPE_IDENTIFIER (template_type)); |
ed44da02 | 3804 | else |
dbfe2124 | 3805 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
3806 | the values for the enumeration constants may involve |
3807 | template parameters. And, no one should be interested | |
3808 | in the enumeration constants for such a type. */ | |
3809 | t = make_node (ENUMERAL_TYPE); | |
3810 | } | |
3811 | else | |
3812 | { | |
3813 | t = make_lang_type (TREE_CODE (template_type)); | |
3814 | CLASSTYPE_DECLARED_CLASS (t) | |
3815 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
3816 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
3817 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
f668f160 | 3818 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ed44da02 MM |
3819 | } |
3820 | ||
61a127b3 | 3821 | /* If we called start_enum above, this information will already |
ed44da02 MM |
3822 | be set up. */ |
3823 | if (!TYPE_NAME (t)) | |
3824 | { | |
3825 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 3826 | |
ed44da02 MM |
3827 | /* Create a stub TYPE_DECL for it. */ |
3828 | type_decl = build_decl (TYPE_DECL, DECL_NAME (template), t); | |
3829 | SET_DECL_ARTIFICIAL (type_decl); | |
3830 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); | |
3831 | DECL_SOURCE_FILE (type_decl) | |
3832 | = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type)); | |
3833 | DECL_SOURCE_LINE (type_decl) | |
3834 | = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type)); | |
3835 | TYPE_STUB_DECL (t) = TYPE_NAME (t) = type_decl; | |
3836 | } | |
3837 | else | |
3838 | type_decl = TYPE_NAME (t); | |
36a117a5 | 3839 | |
9fbf56f7 MM |
3840 | /* Set up the template information. We have to figure out which |
3841 | template is the immediate parent if this is a full | |
3842 | instantiation. */ | |
3843 | if (parm_depth == 1 || is_partial_instantiation | |
3844 | || !PRIMARY_TEMPLATE_P (template)) | |
3845 | /* This case is easy; there are no member templates involved. */ | |
3846 | found = template; | |
3847 | else | |
3848 | { | |
3849 | /* This is a full instantiation of a member template. There | |
3850 | should be some partial instantiation of which this is an | |
3851 | instance. */ | |
3852 | ||
3853 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
3854 | found; found = TREE_CHAIN (found)) | |
3855 | { | |
3856 | int success; | |
3857 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
3858 | ||
3859 | /* We only want partial instantiations, here, not | |
3860 | specializations or full instantiations. */ | |
3861 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) | |
3862 | || !uses_template_parms (TREE_VALUE (found))) | |
3863 | continue; | |
3864 | ||
3865 | /* Temporarily reduce by one the number of levels in the | |
3866 | ARGLIST and in FOUND so as to avoid comparing the | |
3867 | last set of arguments. */ | |
3868 | TREE_VEC_LENGTH (arglist)--; | |
3869 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
3870 | ||
3871 | /* See if the arguments match. If they do, then TMPL is | |
3872 | the partial instantiation we want. */ | |
3873 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
3874 | ||
3875 | /* Restore the argument vectors to their full size. */ | |
3876 | TREE_VEC_LENGTH (arglist)++; | |
3877 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
3878 | ||
3879 | if (success) | |
3880 | { | |
3881 | found = tmpl; | |
3882 | break; | |
3883 | } | |
3884 | } | |
3885 | ||
3886 | if (!found) | |
3887 | my_friendly_abort (0); | |
3888 | } | |
3889 | ||
5566b478 | 3890 | arglist = copy_to_permanent (arglist); |
ed44da02 | 3891 | SET_TYPE_TEMPLATE_INFO (t, |
9fbf56f7 | 3892 | tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
3893 | DECL_TEMPLATE_INSTANTIATIONS (template) |
3894 | = tree_cons (arglist, t, | |
3895 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
3896 | ||
3897 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
3898 | && !is_partial_instantiation) | |
61a127b3 MM |
3899 | /* Now that the type has been registered on the instantiations |
3900 | list, we set up the enumerators. Because the enumeration | |
3901 | constants may involve the enumeration type itself, we make | |
3902 | sure to register the type first, and then create the | |
3903 | constants. That way, doing tsubst_expr for the enumeration | |
3904 | constants won't result in recursive calls here; we'll find | |
3905 | the instantiation and exit above. */ | |
3906 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 MM |
3907 | |
3908 | /* We're done with the permanent obstack, now. */ | |
3909 | pop_obstacks (); | |
7ddedda4 MM |
3910 | /* We're also done with the momentary allocation we started |
3911 | above. */ | |
3912 | pop_momentary (); | |
5566b478 | 3913 | |
36a117a5 MM |
3914 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
3915 | is set up. */ | |
ed44da02 MM |
3916 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
3917 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
36a117a5 | 3918 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
dbfe2124 | 3919 | if (!is_partial_instantiation) |
36a117a5 MM |
3920 | { |
3921 | DECL_ASSEMBLER_NAME (type_decl) | |
3922 | = get_identifier (build_overload_name (t, 1, 1)); | |
50a6dbd7 JM |
3923 | |
3924 | /* For backwards compatibility; code that uses | |
3925 | -fexternal-templates expects looking up a template to | |
3926 | instantiate it. I think DDD still relies on this. | |
3927 | (jason 8/20/1998) */ | |
ed44da02 MM |
3928 | if (TREE_CODE (t) != ENUMERAL_TYPE |
3929 | && flag_external_templates | |
36a117a5 MM |
3930 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) |
3931 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
3932 | add_pending_template (t); | |
3933 | } | |
3934 | else | |
077e7015 MM |
3935 | /* If the type makes use of template parameters, the |
3936 | code that generates debugging information will crash. */ | |
3937 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 3938 | |
36a117a5 MM |
3939 | return t; |
3940 | } | |
8d08fdba MS |
3941 | } |
3942 | \f | |
050367a3 MM |
3943 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or |
3944 | TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA. | |
3945 | If FN returns non-zero, the iteration is terminated, and | |
3946 | for_each_template_parm returns 1. Otherwise, the iteration | |
3947 | continues. If FN never returns a non-zero value, the value | |
3948 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
3949 | considered to be the function which always returns 1. */ | |
3950 | ||
e9659ab0 | 3951 | static int |
050367a3 | 3952 | for_each_template_parm (t, fn, data) |
8d08fdba | 3953 | tree t; |
050367a3 MM |
3954 | tree_fn_t fn; |
3955 | void* data; | |
8d08fdba MS |
3956 | { |
3957 | if (!t) | |
3958 | return 0; | |
581d38d0 MM |
3959 | |
3960 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't' | |
3961 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data)) | |
3962 | return 1; | |
3963 | ||
8d08fdba MS |
3964 | switch (TREE_CODE (t)) |
3965 | { | |
3966 | case INDIRECT_REF: | |
3967 | case COMPONENT_REF: | |
3968 | /* We assume that the object must be instantiated in order to build | |
3969 | the COMPONENT_REF, so we test only whether the type of the | |
3970 | COMPONENT_REF uses template parms. */ | |
050367a3 | 3971 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3972 | |
297e73d8 MM |
3973 | case ARRAY_REF: |
3974 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) | |
3975 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
3976 | ||
8d08fdba MS |
3977 | case IDENTIFIER_NODE: |
3978 | if (!IDENTIFIER_TEMPLATE (t)) | |
3979 | return 0; | |
5566b478 | 3980 | my_friendly_abort (42); |
8d08fdba MS |
3981 | |
3982 | /* aggregates of tree nodes */ | |
3983 | case TREE_VEC: | |
3984 | { | |
3985 | int i = TREE_VEC_LENGTH (t); | |
3986 | while (i--) | |
050367a3 | 3987 | if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data)) |
8d08fdba MS |
3988 | return 1; |
3989 | return 0; | |
3990 | } | |
3991 | case TREE_LIST: | |
050367a3 MM |
3992 | if (for_each_template_parm (TREE_PURPOSE (t), fn, data) |
3993 | || for_each_template_parm (TREE_VALUE (t), fn, data)) | |
8d08fdba | 3994 | return 1; |
050367a3 | 3995 | return for_each_template_parm (TREE_CHAIN (t), fn, data); |
8d08fdba | 3996 | |
2c73f9f5 ML |
3997 | case OVERLOAD: |
3998 | if (for_each_template_parm (OVL_FUNCTION (t), fn, data)) | |
3999 | return 1; | |
4000 | return for_each_template_parm (OVL_CHAIN (t), fn, data); | |
4001 | ||
8d08fdba MS |
4002 | /* constructed type nodes */ |
4003 | case POINTER_TYPE: | |
4004 | case REFERENCE_TYPE: | |
050367a3 | 4005 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
ed44da02 | 4006 | |
8d08fdba | 4007 | case RECORD_TYPE: |
b7484fbe | 4008 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
050367a3 MM |
4009 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t), |
4010 | fn, data); | |
ed44da02 MM |
4011 | /* Fall through. */ |
4012 | ||
8d08fdba | 4013 | case UNION_TYPE: |
ed44da02 MM |
4014 | case ENUMERAL_TYPE: |
4015 | if (! TYPE_TEMPLATE_INFO (t)) | |
8d08fdba | 4016 | return 0; |
050367a3 | 4017 | return for_each_template_parm (TREE_VALUE |
ed44da02 | 4018 | (TYPE_TEMPLATE_INFO (t)), |
050367a3 | 4019 | fn, data); |
588c2d1c MM |
4020 | case METHOD_TYPE: |
4021 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data)) | |
8d08fdba | 4022 | return 1; |
588c2d1c MM |
4023 | /* Fall through. */ |
4024 | ||
4025 | case FUNCTION_TYPE: | |
4026 | /* Check the parameter types. Since default arguments are not | |
4027 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4028 | contain expressions that involve template parameters. But, | |
4029 | no-one should be looking at them yet. And, once they're | |
4030 | instantiated, they don't contain template parameters, so | |
4031 | there's no point in looking at them then, either. */ | |
4032 | { | |
4033 | tree parm; | |
4034 | ||
4035 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4036 | if (for_each_template_parm (TREE_VALUE (parm), fn, data)) | |
4037 | return 1; | |
4038 | } | |
4039 | ||
4040 | /* Check the return type, too. */ | |
050367a3 | 4041 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
588c2d1c | 4042 | |
8d08fdba | 4043 | case ARRAY_TYPE: |
050367a3 | 4044 | if (for_each_template_parm (TYPE_DOMAIN (t), fn, data)) |
8d08fdba | 4045 | return 1; |
050367a3 | 4046 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 4047 | case OFFSET_TYPE: |
050367a3 | 4048 | if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data)) |
8d08fdba | 4049 | return 1; |
050367a3 | 4050 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
4051 | |
4052 | /* decl nodes */ | |
4053 | case TYPE_DECL: | |
050367a3 | 4054 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
5566b478 | 4055 | |
73b0fce8 KL |
4056 | case TEMPLATE_DECL: |
4057 | /* A template template parameter is encountered */ | |
4058 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
744fac59 KL |
4059 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
4060 | /* Already substituted template template parameter */ | |
73b0fce8 KL |
4061 | return 0; |
4062 | ||
3ac3d9ea | 4063 | case CONST_DECL: |
050367a3 | 4064 | if (for_each_template_parm (DECL_INITIAL (t), fn, data)) |
3ac3d9ea MM |
4065 | return 1; |
4066 | goto check_type_and_context; | |
4067 | ||
8d08fdba | 4068 | case FUNCTION_DECL: |
5566b478 MS |
4069 | case VAR_DECL: |
4070 | /* ??? What about FIELD_DECLs? */ | |
4071 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) | |
050367a3 | 4072 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data)) |
8d08fdba MS |
4073 | return 1; |
4074 | /* fall through */ | |
8d08fdba | 4075 | case PARM_DECL: |
3ac3d9ea | 4076 | check_type_and_context: |
050367a3 | 4077 | if (for_each_template_parm (TREE_TYPE (t), fn, data)) |
5566b478 | 4078 | return 1; |
050367a3 MM |
4079 | if (DECL_CONTEXT (t) |
4080 | && for_each_template_parm (DECL_CONTEXT (t), fn, data)) | |
8d08fdba | 4081 | return 1; |
8d08fdba MS |
4082 | return 0; |
4083 | ||
4084 | case CALL_EXPR: | |
297e73d8 MM |
4085 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) |
4086 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4087 | ||
8d08fdba | 4088 | case ADDR_EXPR: |
050367a3 | 4089 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
8d08fdba MS |
4090 | |
4091 | /* template parm nodes */ | |
73b0fce8 | 4092 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4093 | /* Record template parameters such as `T' inside `TT<T>'. */ |
7ddedda4 MM |
4094 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t) |
4095 | && for_each_template_parm (TYPE_TI_ARGS (t), fn, data)) | |
744fac59 KL |
4096 | return 1; |
4097 | case TEMPLATE_TYPE_PARM: | |
f84b4be9 | 4098 | case TEMPLATE_PARM_INDEX: |
050367a3 MM |
4099 | if (fn) |
4100 | return (*fn)(t, data); | |
4101 | else | |
4102 | return 1; | |
8d08fdba MS |
4103 | |
4104 | /* simple type nodes */ | |
4105 | case INTEGER_TYPE: | |
050367a3 | 4106 | if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data)) |
8d08fdba | 4107 | return 1; |
050367a3 | 4108 | return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data); |
8d08fdba MS |
4109 | |
4110 | case REAL_TYPE: | |
37c46b43 | 4111 | case COMPLEX_TYPE: |
8d08fdba | 4112 | case VOID_TYPE: |
2986ae00 | 4113 | case BOOLEAN_TYPE: |
2c73f9f5 | 4114 | case NAMESPACE_DECL: |
8d08fdba MS |
4115 | return 0; |
4116 | ||
4117 | /* constants */ | |
4118 | case INTEGER_CST: | |
4119 | case REAL_CST: | |
4120 | case STRING_CST: | |
4121 | return 0; | |
4122 | ||
4123 | case ERROR_MARK: | |
4124 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
4125 | my_friendly_assert (t == error_mark_node, 274); | |
4126 | /* NOTREACHED */ | |
4127 | return 0; | |
4128 | ||
ec255269 | 4129 | case LOOKUP_EXPR: |
5566b478 | 4130 | case TYPENAME_TYPE: |
8d08fdba MS |
4131 | return 1; |
4132 | ||
61a127b3 MM |
4133 | case PTRMEM_CST: |
4134 | return for_each_template_parm (TREE_TYPE (t), fn, data); | |
4135 | ||
5156628f | 4136 | case SCOPE_REF: |
050367a3 | 4137 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
5156628f | 4138 | |
db5ae43f MS |
4139 | case CONSTRUCTOR: |
4140 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
050367a3 MM |
4141 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4142 | (TREE_TYPE (t)), fn, data); | |
4143 | return for_each_template_parm (TREE_OPERAND (t, 1), fn, data); | |
db5ae43f | 4144 | |
42976354 BK |
4145 | case MODOP_EXPR: |
4146 | case CAST_EXPR: | |
4147 | case REINTERPRET_CAST_EXPR: | |
4148 | case CONST_CAST_EXPR: | |
4149 | case STATIC_CAST_EXPR: | |
4150 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
4151 | case ARROW_EXPR: |
4152 | case DOTSTAR_EXPR: | |
4153 | case TYPEID_EXPR: | |
4154 | return 1; | |
4155 | ||
abff8e06 JM |
4156 | case SIZEOF_EXPR: |
4157 | case ALIGNOF_EXPR: | |
050367a3 | 4158 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
abff8e06 | 4159 | |
8d08fdba MS |
4160 | default: |
4161 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
4162 | { | |
4163 | case '1': | |
4164 | case '2': | |
ec255269 | 4165 | case 'e': |
8d08fdba MS |
4166 | case '<': |
4167 | { | |
4168 | int i; | |
bbc06b4b | 4169 | for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;) |
050367a3 | 4170 | if (for_each_template_parm (TREE_OPERAND (t, i), fn, data)) |
8d08fdba MS |
4171 | return 1; |
4172 | return 0; | |
4173 | } | |
4174 | default: | |
4175 | break; | |
4176 | } | |
4177 | sorry ("testing %s for template parms", | |
4178 | tree_code_name [(int) TREE_CODE (t)]); | |
4179 | my_friendly_abort (82); | |
4180 | /* NOTREACHED */ | |
4181 | return 0; | |
4182 | } | |
4183 | } | |
4184 | ||
050367a3 MM |
4185 | int |
4186 | uses_template_parms (t) | |
4187 | tree t; | |
4188 | { | |
4189 | return for_each_template_parm (t, 0, 0); | |
4190 | } | |
4191 | ||
27fafc8d JM |
4192 | static struct tinst_level *current_tinst_level; |
4193 | static struct tinst_level *free_tinst_level; | |
4194 | static int tinst_depth; | |
e9f32eb5 | 4195 | extern int max_tinst_depth; |
5566b478 | 4196 | #ifdef GATHER_STATISTICS |
27fafc8d | 4197 | int depth_reached; |
5566b478 | 4198 | #endif |
27fafc8d JM |
4199 | int tinst_level_tick; |
4200 | int last_template_error_tick; | |
8d08fdba | 4201 | |
7bae46f4 | 4202 | /* Print out all the template instantiations that we are currently |
27fafc8d JM |
4203 | working on. If ERR, we are being called from cp_thing, so do |
4204 | the right thing for an error message. */ | |
7bae46f4 | 4205 | |
27fafc8d JM |
4206 | static void |
4207 | print_template_context (err) | |
4208 | int err; | |
7bae46f4 JM |
4209 | { |
4210 | struct tinst_level *p = current_tinst_level; | |
4211 | int line = lineno; | |
4212 | char *file = input_filename; | |
4213 | ||
49e04385 | 4214 | if (err && p) |
27fafc8d | 4215 | { |
49e04385 MM |
4216 | if (current_function_decl != p->decl |
4217 | && current_function_decl != NULL_TREE) | |
4218 | /* We can get here during the processing of some synthesized | |
4219 | method. Then, p->decl will be the function that's causing | |
4220 | the synthesis. */ | |
4221 | ; | |
27fafc8d | 4222 | else |
27fafc8d | 4223 | { |
49e04385 MM |
4224 | if (current_function_decl == p->decl) |
4225 | /* Avoid redundancy with the the "In function" line. */; | |
4226 | else | |
4227 | fprintf (stderr, "%s: In instantiation of `%s':\n", | |
4228 | file, decl_as_string (p->decl, 0)); | |
4229 | ||
038fb86f JM |
4230 | line = p->line; |
4231 | file = p->file; | |
27fafc8d JM |
4232 | p = p->next; |
4233 | } | |
4234 | } | |
4235 | ||
7bae46f4 JM |
4236 | for (; p; p = p->next) |
4237 | { | |
038fb86f JM |
4238 | fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line, |
4239 | decl_as_string (p->decl, 0)); | |
4240 | line = p->line; | |
4241 | file = p->file; | |
7bae46f4 | 4242 | } |
038fb86f | 4243 | fprintf (stderr, "%s:%d: instantiated from here\n", file, line); |
7bae46f4 JM |
4244 | } |
4245 | ||
27fafc8d JM |
4246 | /* Called from cp_thing to print the template context for an error. */ |
4247 | ||
4248 | void | |
4249 | maybe_print_template_context () | |
4250 | { | |
4251 | if (last_template_error_tick == tinst_level_tick | |
4252 | || current_tinst_level == 0) | |
4253 | return; | |
4254 | ||
4255 | last_template_error_tick = tinst_level_tick; | |
4256 | print_template_context (1); | |
4257 | } | |
4258 | ||
bd6dd845 | 4259 | static int |
5566b478 MS |
4260 | push_tinst_level (d) |
4261 | tree d; | |
8d08fdba MS |
4262 | { |
4263 | struct tinst_level *new; | |
8d08fdba | 4264 | |
7215f9a0 MS |
4265 | if (tinst_depth >= max_tinst_depth) |
4266 | { | |
8adf5b5e JM |
4267 | /* If the instantiation in question still has unbound template parms, |
4268 | we don't really care if we can't instantiate it, so just return. | |
4269 | This happens with base instantiation for implicit `typename'. */ | |
4270 | if (uses_template_parms (d)) | |
4271 | return 0; | |
4272 | ||
1139b3d8 | 4273 | last_template_error_tick = tinst_level_tick; |
8251199e | 4274 | error ("template instantiation depth exceeds maximum of %d", |
7215f9a0 | 4275 | max_tinst_depth); |
8251199e JM |
4276 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
4277 | cp_error (" instantiating `%D'", d); | |
5566b478 | 4278 | |
27fafc8d | 4279 | print_template_context (0); |
5566b478 | 4280 | |
7215f9a0 MS |
4281 | return 0; |
4282 | } | |
4283 | ||
8d08fdba MS |
4284 | if (free_tinst_level) |
4285 | { | |
4286 | new = free_tinst_level; | |
4287 | free_tinst_level = new->next; | |
4288 | } | |
4289 | else | |
4290 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
4291 | ||
5566b478 MS |
4292 | new->decl = d; |
4293 | new->line = lineno; | |
4294 | new->file = input_filename; | |
8d08fdba MS |
4295 | new->next = current_tinst_level; |
4296 | current_tinst_level = new; | |
5566b478 | 4297 | |
7215f9a0 | 4298 | ++tinst_depth; |
5566b478 MS |
4299 | #ifdef GATHER_STATISTICS |
4300 | if (tinst_depth > depth_reached) | |
4301 | depth_reached = tinst_depth; | |
4302 | #endif | |
4303 | ||
27fafc8d | 4304 | ++tinst_level_tick; |
7215f9a0 | 4305 | return 1; |
8d08fdba MS |
4306 | } |
4307 | ||
4308 | void | |
4309 | pop_tinst_level () | |
4310 | { | |
4311 | struct tinst_level *old = current_tinst_level; | |
4312 | ||
ae58fa02 MM |
4313 | /* Restore the filename and line number stashed away when we started |
4314 | this instantiation. */ | |
4315 | lineno = old->line; | |
4316 | input_filename = old->file; | |
5f2c99c4 | 4317 | extract_interface_info (); |
ae58fa02 | 4318 | |
8d08fdba MS |
4319 | current_tinst_level = old->next; |
4320 | old->next = free_tinst_level; | |
4321 | free_tinst_level = old; | |
7215f9a0 | 4322 | --tinst_depth; |
27fafc8d | 4323 | ++tinst_level_tick; |
8d08fdba MS |
4324 | } |
4325 | ||
4326 | struct tinst_level * | |
4327 | tinst_for_decl () | |
4328 | { | |
4329 | struct tinst_level *p = current_tinst_level; | |
4330 | ||
4331 | if (p) | |
4332 | for (; p->next ; p = p->next ) | |
4333 | ; | |
4334 | return p; | |
4335 | } | |
4336 | ||
f84b4be9 JM |
4337 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
4338 | vector of template arguments, as for tsubst. | |
4339 | ||
4340 | Returns an appropriate tsbust'd friend declaration. */ | |
4341 | ||
4342 | static tree | |
4343 | tsubst_friend_function (decl, args) | |
4344 | tree decl; | |
4345 | tree args; | |
4346 | { | |
4347 | tree new_friend; | |
27fafc8d JM |
4348 | int line = lineno; |
4349 | char *file = input_filename; | |
4350 | ||
4351 | lineno = DECL_SOURCE_LINE (decl); | |
4352 | input_filename = DECL_SOURCE_FILE (decl); | |
4353 | ||
f84b4be9 JM |
4354 | if (TREE_CODE (decl) == FUNCTION_DECL |
4355 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
4356 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
4357 | /* This was a friend declared with an explicit template | |
4358 | argument list, e.g.: | |
4359 | ||
4360 | friend void f<>(T); | |
4361 | ||
4362 | to indicate that f was a template instantiation, not a new | |
4363 | function declaration. Now, we have to figure out what | |
4364 | instantiation of what template. */ | |
4365 | { | |
4366 | tree template_id; | |
4367 | tree new_args; | |
4368 | tree tmpl; | |
f84b4be9 JM |
4369 | |
4370 | template_id | |
4371 | = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl), | |
4393e105 MM |
4372 | args, /*complain=*/1, |
4373 | NULL_TREE), | |
f84b4be9 | 4374 | tsubst (DECL_TI_ARGS (decl), |
4393e105 MM |
4375 | args, /*complain=*/1, |
4376 | NULL_TREE)); | |
39c01e4c MM |
4377 | /* FIXME: The decl we create via the next tsubst could be |
4378 | created on a temporary obstack. */ | |
4393e105 | 4379 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
4380 | tmpl = determine_specialization (template_id, new_friend, |
4381 | &new_args, | |
bf8f3f93 | 4382 | /*need_member_template=*/0); |
27fafc8d JM |
4383 | new_friend = instantiate_template (tmpl, new_args); |
4384 | goto done; | |
f84b4be9 | 4385 | } |
36a117a5 | 4386 | |
4393e105 | 4387 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
f84b4be9 | 4388 | |
36a117a5 | 4389 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
4390 | compiler, but is not an instantiation from the point of view of |
4391 | the language. For example, we might have had: | |
4392 | ||
4393 | template <class T> struct S { | |
4394 | template <class U> friend void f(T, U); | |
4395 | }; | |
4396 | ||
4397 | Then, in S<int>, template <class U> void f(int, U) is not an | |
4398 | instantiation of anything. */ | |
4399 | DECL_USE_TEMPLATE (new_friend) = 0; | |
4400 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
4401 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
36a117a5 MM |
4402 | |
4403 | /* The mangled name for the NEW_FRIEND is incorrect. The call to | |
4404 | tsubst will have resulted in a call to | |
4405 | set_mangled_name_for_template_decl. But, the function is not a | |
4406 | template instantiation and should not be mangled like one. | |
4407 | Therefore, we remangle the function name. We don't have to do | |
4408 | this if the NEW_FRIEND is a template since | |
4409 | set_mangled_name_for_template_decl doesn't do anything if the | |
4410 | function declaration still uses template arguments. */ | |
4411 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) | |
4412 | { | |
4413 | set_mangled_name_for_decl (new_friend); | |
4414 | DECL_RTL (new_friend) = 0; | |
4415 | make_decl_rtl (new_friend, NULL_PTR, 1); | |
4416 | } | |
4417 | ||
6eb3bb27 | 4418 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 4419 | { |
36a117a5 | 4420 | tree old_decl; |
fbf1c34b MM |
4421 | tree new_friend_template_info; |
4422 | tree new_friend_result_template_info; | |
4423 | int new_friend_is_defn; | |
4424 | ||
4425 | /* We must save some information from NEW_FRIEND before calling | |
4426 | duplicate decls since that function will free NEW_FRIEND if | |
4427 | possible. */ | |
4428 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
f84b4be9 | 4429 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
4430 | { |
4431 | /* This declaration is a `primary' template. */ | |
4432 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
4433 | ||
4434 | new_friend_is_defn | |
4435 | = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE; | |
4436 | new_friend_result_template_info | |
4437 | = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend)); | |
4438 | } | |
4439 | else | |
4440 | { | |
4441 | new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; | |
4442 | new_friend_result_template_info = NULL_TREE; | |
4443 | } | |
36a117a5 | 4444 | |
36a117a5 MM |
4445 | old_decl = pushdecl_namespace_level (new_friend); |
4446 | ||
4447 | if (old_decl != new_friend) | |
4448 | { | |
4449 | /* This new friend declaration matched an existing | |
4450 | declaration. For example, given: | |
4451 | ||
4452 | template <class T> void f(T); | |
4453 | template <class U> class C { | |
4454 | template <class T> friend void f(T) {} | |
4455 | }; | |
4456 | ||
4457 | the friend declaration actually provides the definition | |
4458 | of `f', once C has been instantiated for some type. So, | |
4459 | old_decl will be the out-of-class template declaration, | |
4460 | while new_friend is the in-class definition. | |
4461 | ||
4462 | But, if `f' was called before this point, the | |
4463 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
4464 | to `T' but not to `U', references to which might appear | |
4465 | in the definition of `f'. Previously, the most general | |
4466 | template for an instantiation of `f' was the out-of-class | |
4467 | version; now it is the in-class version. Therefore, we | |
4468 | run through all specialization of `f', adding to their | |
4469 | DECL_TI_ARGS appropriately. In particular, they need a | |
4470 | new set of outer arguments, corresponding to the | |
4471 | arguments for this class instantiation. | |
4472 | ||
4473 | The same situation can arise with something like this: | |
4474 | ||
4475 | friend void f(int); | |
4476 | template <class T> class C { | |
4477 | friend void f(T) {} | |
4478 | }; | |
4479 | ||
4480 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
4481 | in the class. */ | |
4482 | ||
fbf1c34b MM |
4483 | if (!new_friend_is_defn) |
4484 | /* On the other hand, if the in-class declaration does | |
4485 | *not* provide a definition, then we don't want to alter | |
4486 | existing definitions. We can just leave everything | |
4487 | alone. */ | |
36a117a5 | 4488 | ; |
fbf1c34b | 4489 | else |
36a117a5 | 4490 | { |
fbf1c34b MM |
4491 | /* Overwrite whatever template info was there before, if |
4492 | any, with the new template information pertaining to | |
4493 | the declaration. */ | |
4494 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
4495 | ||
4496 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
4497 | /* duplicate_decls will take care of this case. */ | |
4498 | ; | |
4499 | else | |
36a117a5 | 4500 | { |
fbf1c34b MM |
4501 | tree t; |
4502 | tree new_friend_args; | |
4503 | ||
4504 | DECL_TEMPLATE_INFO (DECL_RESULT (old_decl)) | |
4505 | = new_friend_result_template_info; | |
4506 | ||
4507 | new_friend_args = TI_ARGS (new_friend_template_info); | |
4508 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
4509 | t != NULL_TREE; | |
4510 | t = TREE_CHAIN (t)) | |
4511 | { | |
4512 | tree spec = TREE_VALUE (t); | |
36a117a5 | 4513 | |
fbf1c34b MM |
4514 | DECL_TI_ARGS (spec) |
4515 | = add_outermost_template_args (new_friend_args, | |
4516 | DECL_TI_ARGS (spec)); | |
4517 | DECL_TI_ARGS (spec) | |
4518 | = copy_to_permanent (DECL_TI_ARGS (spec)); | |
4519 | } | |
4520 | ||
4521 | /* Now, since specializations are always supposed to | |
4522 | hang off of the most general template, we must move | |
4523 | them. */ | |
4524 | t = most_general_template (old_decl); | |
4525 | if (t != old_decl) | |
4526 | { | |
4527 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
4528 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
4529 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
4530 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
4531 | } | |
36a117a5 MM |
4532 | } |
4533 | } | |
4534 | ||
4535 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
4536 | by duplicate_decls. */ | |
4537 | new_friend = old_decl; | |
4538 | } | |
f84b4be9 JM |
4539 | } |
4540 | else if (TYPE_SIZE (DECL_CONTEXT (new_friend))) | |
4541 | { | |
4542 | /* Check to see that the declaration is really present, and, | |
4543 | possibly obtain an improved declaration. */ | |
4544 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
4545 | new_friend); | |
4546 | ||
4547 | if (fn) | |
4548 | new_friend = fn; | |
4549 | } | |
4550 | ||
27fafc8d JM |
4551 | done: |
4552 | lineno = line; | |
4553 | input_filename = file; | |
f84b4be9 JM |
4554 | return new_friend; |
4555 | } | |
4556 | ||
1aed5355 MM |
4557 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
4558 | template arguments, as for tsubst. | |
6757edfe MM |
4559 | |
4560 | Returns an appropriate tsbust'd friend type. */ | |
4561 | ||
4562 | static tree | |
1aed5355 MM |
4563 | tsubst_friend_class (friend_tmpl, args) |
4564 | tree friend_tmpl; | |
6757edfe MM |
4565 | tree args; |
4566 | { | |
1aed5355 | 4567 | tree friend_type; |
f9c3243c | 4568 | tree tmpl = lookup_name (DECL_NAME (friend_tmpl), 1); |
6757edfe MM |
4569 | |
4570 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
4571 | ||
4572 | if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl)) | |
4573 | { | |
4574 | /* The friend template has already been declared. Just | |
36a117a5 MM |
4575 | check to see that the declarations match, and install any new |
4576 | default parameters. We must tsubst the default parameters, | |
4577 | of course. We only need the innermost template parameters | |
4578 | because that is all that redeclare_class_template will look | |
4579 | at. */ | |
4580 | tree parms | |
4581 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
4393e105 | 4582 | args, /*complain=*/1); |
36a117a5 | 4583 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
6757edfe MM |
4584 | friend_type = TREE_TYPE (tmpl); |
4585 | } | |
4586 | else | |
4587 | { | |
4588 | /* The friend template has not already been declared. In this | |
4589 | case, the instantiation of the template class will cause the | |
4590 | injection of this template into the global scope. */ | |
4393e105 | 4591 | tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE); |
6757edfe MM |
4592 | |
4593 | /* The new TMPL is not an instantiation of anything, so we | |
4594 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
4595 | the new type because that is supposed to be the corresponding | |
4596 | template decl, i.e., TMPL. */ | |
4597 | DECL_USE_TEMPLATE (tmpl) = 0; | |
4598 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
4599 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
4600 | ||
4601 | /* Inject this template into the global scope. */ | |
4602 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
4603 | } | |
4604 | ||
4605 | return friend_type; | |
4606 | } | |
f84b4be9 | 4607 | |
8d08fdba | 4608 | tree |
5566b478 MS |
4609 | instantiate_class_template (type) |
4610 | tree type; | |
8d08fdba | 4611 | { |
61a127b3 | 4612 | tree template, args, pattern, t; |
36a117a5 | 4613 | tree typedecl; |
8d08fdba | 4614 | |
5566b478 | 4615 | if (type == error_mark_node) |
8d08fdba MS |
4616 | return error_mark_node; |
4617 | ||
5566b478 MS |
4618 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) |
4619 | return type; | |
4620 | ||
7ddedda4 MM |
4621 | /* We want to allocate temporary vectors of template arguments and |
4622 | template argument expressions on the momentary obstack, not on | |
4623 | the expression obstack. Otherwise, all the space allocated in | |
4624 | argument coercion and such is simply lost. */ | |
4625 | push_momentary (); | |
4626 | ||
6bdb985f | 4627 | /* Figure out which template is being instantiated. */ |
36a117a5 | 4628 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
5566b478 | 4629 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); |
73aad9b9 | 4630 | |
6bdb985f MM |
4631 | /* Figure out which arguments are being used to do the |
4632 | instantiation. */ | |
4633 | args = CLASSTYPE_TI_ARGS (type); | |
4c571114 | 4634 | PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args); |
6bdb985f | 4635 | |
4c571114 MM |
4636 | if (pedantic && PARTIAL_INSTANTIATION_P (type)) |
4637 | /* If this is a partial instantiation, then we can't instantiate | |
4638 | the type; there's no telling whether or not one of the | |
4639 | template parameters might eventually be instantiated to some | |
4640 | value that results in a specialization being used. For | |
4641 | example, consider: | |
4642 | ||
4643 | template <class T> | |
4644 | struct S {}; | |
4645 | ||
4646 | template <class U> | |
4647 | void f(S<U>); | |
4648 | ||
4649 | template <> | |
4650 | struct S<int> {}; | |
4651 | ||
4652 | Now, the `S<U>' in `f<int>' is the specialization, not an | |
5db698f6 | 4653 | instantiation of the original template. */ |
4c571114 MM |
4654 | goto end; |
4655 | ||
4656 | /* Determine what specialization of the original template to | |
4657 | instantiate. */ | |
4658 | if (PARTIAL_INSTANTIATION_P (type)) | |
6bdb985f MM |
4659 | /* There's no telling which specialization is appropriate at this |
4660 | point. Since all peeking at the innards of this partial | |
4661 | instantiation are extensions (like the "implicit typename" | |
4662 | extension, which allows users to omit the keyword `typename' on | |
4663 | names that are declared as types in template base classes), we | |
4664 | are free to do what we please. | |
4665 | ||
4666 | Trying to figure out which partial instantiation to use can | |
4667 | cause a crash. (Some of the template arguments don't even have | |
4668 | types.) So, we just use the most general version. */ | |
4669 | t = NULL_TREE; | |
4670 | else | |
73aad9b9 | 4671 | { |
6bdb985f MM |
4672 | t = most_specialized_class (template, args); |
4673 | ||
4674 | if (t == error_mark_node) | |
73aad9b9 | 4675 | { |
d8e178a0 | 4676 | const char *str = "candidates are:"; |
6bdb985f MM |
4677 | cp_error ("ambiguous class template instantiation for `%#T'", type); |
4678 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; | |
4679 | t = TREE_CHAIN (t)) | |
73aad9b9 | 4680 | { |
6bdb985f MM |
4681 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
4682 | args)) | |
4683 | { | |
4684 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
4685 | str = " "; | |
4686 | } | |
73aad9b9 | 4687 | } |
6bdb985f MM |
4688 | TYPE_BEING_DEFINED (type) = 1; |
4689 | type = error_mark_node; | |
4690 | goto end; | |
73aad9b9 | 4691 | } |
73aad9b9 | 4692 | } |
6bdb985f MM |
4693 | |
4694 | if (t) | |
73aad9b9 JM |
4695 | pattern = TREE_TYPE (t); |
4696 | else | |
4697 | pattern = TREE_TYPE (template); | |
5566b478 | 4698 | |
4c571114 MM |
4699 | /* If the template we're instantiating is incomplete, then clearly |
4700 | there's nothing we can do. */ | |
5566b478 | 4701 | if (TYPE_SIZE (pattern) == NULL_TREE) |
7ddedda4 | 4702 | goto end; |
5566b478 | 4703 | |
4c571114 MM |
4704 | /* If this is a partial instantiation, don't tsubst anything. We will |
4705 | only use this type for implicit typename, so the actual contents don't | |
4706 | matter. All that matters is whether a particular name is a type. */ | |
4707 | if (PARTIAL_INSTANTIATION_P (type)) | |
4708 | { | |
4709 | /* The fields set here must be kept in sync with those cleared | |
4710 | in begin_class_definition. */ | |
4711 | TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); | |
4712 | TYPE_FIELDS (type) = TYPE_FIELDS (pattern); | |
4713 | TYPE_METHODS (type) = TYPE_METHODS (pattern); | |
4714 | CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern); | |
4715 | /* Pretend that the type is complete, so that we will look | |
4716 | inside it during name lookup and such. */ | |
4717 | TYPE_SIZE (type) = integer_zero_node; | |
4718 | goto end; | |
4719 | } | |
4720 | ||
4721 | /* If we've recursively instantiated too many templates, stop. */ | |
4722 | if (! push_tinst_level (type)) | |
4723 | goto end; | |
4724 | ||
4725 | /* Now we're really doing the instantiation. Mark the type as in | |
4726 | the process of being defined. */ | |
4727 | TYPE_BEING_DEFINED (type) = 1; | |
4728 | ||
4729 | maybe_push_to_top_level (uses_template_parms (type)); | |
4730 | pushclass (type, 0); | |
4731 | ||
73aad9b9 | 4732 | if (t) |
36a117a5 MM |
4733 | { |
4734 | /* This TYPE is actually a instantiation of of a partial | |
4735 | specialization. We replace the innermost set of ARGS with | |
4736 | the arguments appropriate for substitution. For example, | |
4737 | given: | |
4738 | ||
4739 | template <class T> struct S {}; | |
4740 | template <class T> struct S<T*> {}; | |
4741 | ||
4742 | and supposing that we are instantiating S<int*>, ARGS will | |
4743 | present be {int*} but we need {int}. */ | |
4744 | tree inner_args | |
4745 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
4746 | args); | |
4747 | ||
4748 | /* If there were multiple levels in ARGS, replacing the | |
4749 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
4750 | want, so we make a copy first. */ | |
4751 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
4752 | { | |
4753 | args = copy_node (args); | |
4754 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
4755 | } | |
4756 | else | |
4757 | args = inner_args; | |
4758 | } | |
8d019cef | 4759 | |
5566b478 MS |
4760 | if (flag_external_templates) |
4761 | { | |
4762 | if (flag_alt_external_templates) | |
4763 | { | |
4764 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
4765 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
4766 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4767 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4768 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4769 | } |
4770 | else | |
4771 | { | |
4772 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
4773 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
4774 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
4775 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4776 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4777 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4778 | } |
4779 | } | |
4780 | else | |
8d08fdba | 4781 | { |
5566b478 MS |
4782 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
4783 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
4784 | } |
4785 | ||
f7da6097 MS |
4786 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
4787 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
f7da6097 MS |
4788 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); |
4789 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
4790 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
4791 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
4792 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
4793 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
4794 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
4795 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
4796 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
4797 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
4798 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
4799 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
4800 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
4801 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
4802 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
4803 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
4804 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
4805 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
4806 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
4807 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
4808 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
eff71ab0 | 4809 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
f7da6097 | 4810 | |
4c571114 MM |
4811 | /* We must copy the arguments to the permanent obstack since |
4812 | during the tsubst'ing below they may wind up in the | |
4813 | DECL_TI_ARGS of some instantiated member template. */ | |
4814 | args = copy_to_permanent (args); | |
2604412d | 4815 | |
3fd71a52 MM |
4816 | if (TYPE_BINFO_BASETYPES (pattern)) |
4817 | { | |
4818 | tree base_list = NULL_TREE; | |
4819 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
4820 | int i; | |
5566b478 | 4821 | |
3fd71a52 MM |
4822 | /* Substitute into each of the bases to determine the actual |
4823 | basetypes. */ | |
4824 | for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) | |
4825 | { | |
4826 | tree base; | |
4827 | tree access; | |
4828 | tree pbase; | |
5566b478 | 4829 | |
3fd71a52 | 4830 | pbase = TREE_VEC_ELT (pbases, i); |
711734a9 | 4831 | |
3fd71a52 MM |
4832 | /* Substitue to figure out the base class. */ |
4833 | base = tsubst (BINFO_TYPE (pbase), args, | |
4834 | /*complain=*/1, NULL_TREE); | |
4835 | if (base == error_mark_node) | |
4836 | continue; | |
711734a9 | 4837 | |
3fd71a52 MM |
4838 | /* Calculate the correct access node. */ |
4839 | if (TREE_VIA_VIRTUAL (pbase)) | |
4840 | { | |
4841 | if (TREE_VIA_PUBLIC (pbase)) | |
4842 | access = access_public_virtual_node; | |
4843 | else if (TREE_VIA_PROTECTED (pbase)) | |
4844 | access = access_protected_virtual_node; | |
d6479fe7 | 4845 | else |
3fd71a52 MM |
4846 | access = access_private_virtual_node; |
4847 | } | |
4848 | else | |
4849 | { | |
4850 | if (TREE_VIA_PUBLIC (pbase)) | |
4851 | access = access_public_node; | |
4852 | else if (TREE_VIA_PROTECTED (pbase)) | |
4853 | access = access_protected_node; | |
d6479fe7 | 4854 | else |
3fd71a52 MM |
4855 | access = access_private_node; |
4856 | } | |
dfbcd65a | 4857 | |
3fd71a52 MM |
4858 | base_list = tree_cons (access, base, base_list); |
4859 | } | |
dfbcd65a | 4860 | |
3fd71a52 MM |
4861 | /* The list is now in reverse order; correct that. */ |
4862 | base_list = nreverse (base_list); | |
4863 | ||
4864 | /* Now call xref_basetypes to set up all the base-class | |
4865 | information. */ | |
4866 | xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE | |
4867 | ? (CLASSTYPE_DECLARED_CLASS (pattern) | |
4868 | ? class_type_node : record_type_node) | |
4869 | : union_type_node, | |
4870 | DECL_NAME (TYPE_NAME (pattern)), | |
4871 | type, | |
4872 | base_list); | |
4873 | } | |
5566b478 | 4874 | |
5566b478 | 4875 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) |
8d08fdba | 4876 | { |
5566b478 | 4877 | tree tag = TREE_VALUE (t); |
36a117a5 MM |
4878 | tree name = TYPE_IDENTIFIER (tag); |
4879 | tree newtag; | |
5566b478 | 4880 | |
4393e105 | 4881 | newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE); |
61a127b3 | 4882 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
ed44da02 | 4883 | { |
486e4077 MM |
4884 | if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) |
4885 | /* Unfortunately, lookup_template_class sets | |
4886 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
4887 | instantiation (i.e., for the type of a member template | |
4888 | class nested within a template class.) This behavior is | |
4889 | required for maybe_process_partial_specialization to work | |
4890 | correctly, but is not accurate in this case; the TAG is not | |
4891 | an instantiation of anything. (The corresponding | |
4892 | TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ | |
4893 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; | |
4894 | ||
ed44da02 MM |
4895 | /* Now, we call pushtag to put this NEWTAG into the scope of |
4896 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
4897 | pushtag calling push_template_decl. We don't have to do | |
4898 | this for enums because it will already have been done in | |
4899 | tsubst_enum. */ | |
4900 | if (name) | |
4901 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
4902 | pushtag (name, newtag, /*globalize=*/0); | |
4903 | } | |
8d08fdba MS |
4904 | } |
4905 | ||
5566b478 MS |
4906 | /* Don't replace enum constants here. */ |
4907 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
4908 | if (TREE_CODE (t) != CONST_DECL) | |
4909 | { | |
ae58fa02 MM |
4910 | tree r; |
4911 | ||
4912 | /* The the file and line for this declaration, to assist in | |
4913 | error message reporting. Since we called push_tinst_level | |
4914 | above, we don't need to restore these. */ | |
4915 | lineno = DECL_SOURCE_LINE (t); | |
4916 | input_filename = DECL_SOURCE_FILE (t); | |
4917 | ||
4393e105 | 4918 | r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
5566b478 MS |
4919 | if (TREE_CODE (r) == VAR_DECL) |
4920 | { | |
2604412d JM |
4921 | pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics); |
4922 | /* Perhaps we should do more of grokfield here. */ | |
94c82a77 JM |
4923 | if (DECL_DEFINED_IN_CLASS_P (r)) |
4924 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
4925 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, | |
4393e105 | 4926 | /*complain=*/1, NULL_TREE); |
5566b478 MS |
4927 | start_decl_1 (r); |
4928 | DECL_IN_AGGR_P (r) = 1; | |
4929 | DECL_EXTERNAL (r) = 1; | |
4930 | cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0); | |
6ba89f8e MM |
4931 | if (DECL_DEFINED_IN_CLASS_P (r)) |
4932 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5566b478 | 4933 | } |
61a127b3 MM |
4934 | |
4935 | /* R will have a TREE_CHAIN if and only if it has already been | |
4936 | processed by finish_member_declaration. This can happen | |
4937 | if, for example, it is a TYPE_DECL for a class-scoped | |
4938 | ENUMERAL_TYPE; such a thing will already have been added to | |
4939 | the field list by tsubst_enum above. */ | |
4940 | if (!TREE_CHAIN (r)) | |
4941 | { | |
4942 | set_current_access_from_decl (r); | |
4943 | finish_member_declaration (r); | |
4944 | } | |
5566b478 | 4945 | } |
8d08fdba | 4946 | |
61a127b3 MM |
4947 | /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC) |
4948 | for this instantiation. */ | |
4949 | for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t)) | |
4950 | { | |
4393e105 | 4951 | tree r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
61a127b3 MM |
4952 | set_current_access_from_decl (r); |
4953 | finish_member_declaration (r); | |
4954 | } | |
8d08fdba | 4955 | |
2604412d JM |
4956 | /* Construct the DECL_FRIENDLIST for the new class type. */ |
4957 | typedecl = TYPE_MAIN_DECL (type); | |
4958 | for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)); | |
4959 | t != NULL_TREE; | |
4960 | t = TREE_CHAIN (t)) | |
f84b4be9 | 4961 | { |
2604412d | 4962 | tree friends; |
f84b4be9 | 4963 | |
2604412d JM |
4964 | for (friends = TREE_VALUE (t); |
4965 | friends != NULL_TREE; | |
4966 | friends = TREE_CHAIN (friends)) | |
0c0aac2f MM |
4967 | if (TREE_PURPOSE (friends) == error_mark_node) |
4968 | add_friend (type, | |
4969 | tsubst_friend_function (TREE_VALUE (friends), | |
4970 | args)); | |
4971 | else | |
4972 | add_friends (type, | |
4973 | tsubst_copy (TREE_PURPOSE (t), args, | |
4974 | /*complain=*/1, NULL_TREE), | |
4975 | tsubst (TREE_PURPOSE (friends), args, | |
4976 | /*complain=*/1, NULL_TREE)); | |
2604412d | 4977 | } |
5566b478 | 4978 | |
6757edfe MM |
4979 | for (t = CLASSTYPE_FRIEND_CLASSES (pattern); |
4980 | t != NULL_TREE; | |
4981 | t = TREE_CHAIN (t)) | |
4982 | { | |
4983 | tree friend_type = TREE_VALUE (t); | |
1aed5355 | 4984 | tree new_friend_type; |
6757edfe | 4985 | |
36a117a5 MM |
4986 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
4987 | new_friend_type = tsubst_friend_class (friend_type, args); | |
4988 | else if (uses_template_parms (friend_type)) | |
4393e105 MM |
4989 | new_friend_type = tsubst (friend_type, args, /*complain=*/1, |
4990 | NULL_TREE); | |
36a117a5 | 4991 | else |
6757edfe MM |
4992 | /* The call to xref_tag_from_type does injection for friend |
4993 | classes. */ | |
1aed5355 | 4994 | new_friend_type = |
36a117a5 MM |
4995 | xref_tag_from_type (friend_type, NULL_TREE, 1); |
4996 | ||
1aed5355 MM |
4997 | |
4998 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
4999 | /* Trick make_friend_class into realizing that the friend | |
5000 | we're adding is a template, not an ordinary class. It's | |
5001 | important that we use make_friend_class since it will | |
5002 | perform some error-checking and output cross-reference | |
5003 | information. */ | |
5004 | ++processing_template_decl; | |
5005 | ||
5006 | make_friend_class (type, new_friend_type); | |
fc378698 | 5007 | |
1aed5355 MM |
5008 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
5009 | --processing_template_decl; | |
6757edfe | 5010 | } |
fc378698 | 5011 | |
2604412d JM |
5012 | /* This does injection for friend functions. */ |
5013 | if (!processing_template_decl) | |
5014 | { | |
4393e105 MM |
5015 | t = tsubst (DECL_TEMPLATE_INJECT (template), args, |
5016 | /*complain=*/1, NULL_TREE); | |
5566b478 | 5017 | |
2604412d JM |
5018 | for (; t; t = TREE_CHAIN (t)) |
5019 | { | |
5020 | tree d = TREE_VALUE (t); | |
fc378698 | 5021 | |
2604412d JM |
5022 | if (TREE_CODE (d) == TYPE_DECL) |
5023 | /* Already injected. */; | |
5024 | else | |
5025 | pushdecl (d); | |
5026 | } | |
5027 | } | |
5566b478 | 5028 | |
2604412d JM |
5029 | for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
5030 | if (TREE_CODE (t) == FIELD_DECL) | |
5031 | { | |
5032 | TREE_TYPE (t) = complete_type (TREE_TYPE (t)); | |
5033 | require_complete_type (t); | |
5034 | } | |
5566b478 | 5035 | |
61a127b3 MM |
5036 | /* Set the file and line number information to whatever is given for |
5037 | the class itself. This puts error messages involving generated | |
5038 | implicit functions at a predictable point, and the same point | |
5039 | that would be used for non-template classes. */ | |
5040 | lineno = DECL_SOURCE_LINE (typedecl); | |
5041 | input_filename = DECL_SOURCE_FILE (typedecl); | |
5042 | ||
5043 | unreverse_member_declarations (type); | |
2604412d JM |
5044 | type = finish_struct_1 (type, 0); |
5045 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
e92cc029 | 5046 | |
5524676d JM |
5047 | /* Clear this now so repo_template_used is happy. */ |
5048 | TYPE_BEING_DEFINED (type) = 0; | |
2604412d | 5049 | repo_template_used (type); |
8d08fdba | 5050 | |
5566b478 | 5051 | popclass (0); |
5566b478 MS |
5052 | pop_from_top_level (); |
5053 | pop_tinst_level (); | |
5054 | ||
7ddedda4 MM |
5055 | end: |
5056 | pop_momentary (); | |
5057 | ||
5566b478 | 5058 | return type; |
8d08fdba MS |
5059 | } |
5060 | ||
5061 | static int | |
5062 | list_eq (t1, t2) | |
5063 | tree t1, t2; | |
5064 | { | |
5065 | if (t1 == NULL_TREE) | |
5066 | return t2 == NULL_TREE; | |
5067 | if (t2 == NULL_TREE) | |
5068 | return 0; | |
5069 | /* Don't care if one declares its arg const and the other doesn't -- the | |
5070 | main variant of the arg type is all that matters. */ | |
5071 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
5072 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
5073 | return 0; | |
5074 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
5075 | } | |
5076 | ||
00d3396f JM |
5077 | /* If arg is a non-type template parameter that does not depend on template |
5078 | arguments, fold it like we weren't in the body of a template. */ | |
5079 | ||
5080 | static tree | |
5081 | maybe_fold_nontype_arg (arg) | |
5082 | tree arg; | |
5083 | { | |
5084 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
5085 | && !uses_template_parms (arg)) | |
5086 | { | |
5087 | /* Sometimes, one of the args was an expression involving a | |
5088 | template constant parameter, like N - 1. Now that we've | |
5089 | tsubst'd, we might have something like 2 - 1. This will | |
5090 | confuse lookup_template_class, so we do constant folding | |
5091 | here. We have to unset processing_template_decl, to | |
5092 | fool build_expr_from_tree() into building an actual | |
5093 | tree. */ | |
5094 | ||
5095 | int saved_processing_template_decl = processing_template_decl; | |
5096 | processing_template_decl = 0; | |
5097 | arg = fold (build_expr_from_tree (arg)); | |
5098 | processing_template_decl = saved_processing_template_decl; | |
5099 | } | |
5100 | return arg; | |
5101 | } | |
5102 | ||
a221d52f | 5103 | /* Return the TREE_VEC with the arguments for the innermost template header, |
36a117a5 MM |
5104 | where ARGS is either that or the VEC of VECs for all the |
5105 | arguments. */ | |
a221d52f JM |
5106 | |
5107 | tree | |
36a117a5 | 5108 | innermost_args (args) |
a221d52f | 5109 | tree args; |
a221d52f | 5110 | { |
36a117a5 | 5111 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
a221d52f | 5112 | } |
f84b4be9 | 5113 | |
830bfa74 MM |
5114 | /* Substitute ARGS into the vector of template arguments T. */ |
5115 | ||
e9659ab0 | 5116 | static tree |
4393e105 | 5117 | tsubst_template_arg_vector (t, args, complain) |
830bfa74 MM |
5118 | tree t; |
5119 | tree args; | |
4393e105 | 5120 | int complain; |
830bfa74 MM |
5121 | { |
5122 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
5123 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5124 | ||
5125 | bzero ((char *) elts, len * sizeof (tree)); | |
5126 | ||
5127 | for (i = 0; i < len; i++) | |
5128 | { | |
5129 | if (TREE_VEC_ELT (t, i) != NULL_TREE | |
5130 | && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) | |
4393e105 MM |
5131 | elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), |
5132 | args, complain); | |
830bfa74 MM |
5133 | else |
5134 | elts[i] = maybe_fold_nontype_arg | |
4393e105 MM |
5135 | (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, |
5136 | NULL_TREE)); | |
830bfa74 MM |
5137 | |
5138 | if (elts[i] != TREE_VEC_ELT (t, i)) | |
5139 | need_new = 1; | |
5140 | } | |
5141 | ||
5142 | if (!need_new) | |
5143 | return t; | |
5144 | ||
7ddedda4 | 5145 | t = make_temp_vec (len); |
830bfa74 MM |
5146 | for (i = 0; i < len; i++) |
5147 | TREE_VEC_ELT (t, i) = elts[i]; | |
5148 | ||
5149 | return t; | |
5150 | } | |
5151 | ||
36a117a5 MM |
5152 | /* Return the result of substituting ARGS into the template parameters |
5153 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5154 | PARMS, then the result will contain n levels of PARMS. For | |
5155 | example, if PARMS is `template <class T> template <class U> | |
5156 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5157 | result will be `template <int*, double, class V>'. */ | |
5158 | ||
e9659ab0 | 5159 | static tree |
4393e105 | 5160 | tsubst_template_parms (parms, args, complain) |
36a117a5 MM |
5161 | tree parms; |
5162 | tree args; | |
4393e105 | 5163 | int complain; |
36a117a5 MM |
5164 | { |
5165 | tree r; | |
5166 | tree* new_parms = &r; | |
5167 | ||
5168 | for (new_parms = &r; | |
5169 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5170 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5171 | parms = TREE_CHAIN (parms)) | |
5172 | { | |
5173 | tree new_vec = | |
5174 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5175 | int i; | |
5176 | ||
5177 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5178 | { | |
5179 | tree default_value = | |
5180 | TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5181 | tree parm_decl = | |
5182 | TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5183 | ||
5184 | TREE_VEC_ELT (new_vec, i) | |
4393e105 MM |
5185 | = build_tree_list (tsubst (default_value, args, complain, |
5186 | NULL_TREE), | |
5187 | tsubst (parm_decl, args, complain, | |
5188 | NULL_TREE)); | |
36a117a5 MM |
5189 | } |
5190 | ||
5191 | *new_parms = | |
5192 | tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms) | |
5193 | - TMPL_ARGS_DEPTH (args))), | |
5194 | new_vec, NULL_TREE); | |
5195 | } | |
5196 | ||
5197 | return r; | |
5198 | } | |
5199 | ||
ed44da02 MM |
5200 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
5201 | type T. If T is not an aggregate or enumeration type, it is | |
5202 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
5203 | ENTERING_SCOPE is non-zero, T is the context for a template which | |
5204 | we are presently tsubst'ing. Return the subsituted value. */ | |
36a117a5 | 5205 | |
e9659ab0 | 5206 | static tree |
4393e105 | 5207 | tsubst_aggr_type (t, args, complain, in_decl, entering_scope) |
36a117a5 MM |
5208 | tree t; |
5209 | tree args; | |
4393e105 | 5210 | int complain; |
36a117a5 MM |
5211 | tree in_decl; |
5212 | int entering_scope; | |
5213 | { | |
5214 | if (t == NULL_TREE) | |
5215 | return NULL_TREE; | |
5216 | ||
5217 | switch (TREE_CODE (t)) | |
5218 | { | |
5219 | case RECORD_TYPE: | |
5220 | if (TYPE_PTRMEMFUNC_P (t)) | |
5221 | { | |
5222 | tree r = build_ptrmemfunc_type | |
4393e105 | 5223 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl)); |
91063b51 | 5224 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
36a117a5 MM |
5225 | } |
5226 | ||
5227 | /* else fall through */ | |
ed44da02 | 5228 | case ENUMERAL_TYPE: |
36a117a5 | 5229 | case UNION_TYPE: |
5db698f6 | 5230 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
5231 | { |
5232 | tree argvec; | |
5233 | tree context; | |
5234 | tree r; | |
5235 | ||
5236 | /* First, determine the context for the type we are looking | |
5237 | up. */ | |
5238 | if (TYPE_CONTEXT (t) != NULL_TREE) | |
5239 | context = tsubst_aggr_type (TYPE_CONTEXT (t), args, | |
4393e105 | 5240 | complain, |
36a117a5 MM |
5241 | in_decl, /*entering_scope=*/1); |
5242 | else | |
5243 | context = NULL_TREE; | |
5244 | ||
5245 | /* Then, figure out what arguments are appropriate for the | |
5246 | type we are trying to find. For example, given: | |
5247 | ||
5248 | template <class T> struct S; | |
5249 | template <class T, class U> void f(T, U) { S<U> su; } | |
5250 | ||
5251 | and supposing that we are instantiating f<int, double>, | |
5252 | then our ARGS will be {int, double}, but, when looking up | |
5253 | S we only want {double}. */ | |
7ddedda4 | 5254 | push_momentary (); |
4393e105 MM |
5255 | argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, |
5256 | complain); | |
36a117a5 MM |
5257 | |
5258 | r = lookup_template_class (t, argvec, in_decl, context, | |
5259 | entering_scope); | |
7ddedda4 | 5260 | pop_momentary (); |
36a117a5 | 5261 | |
91063b51 | 5262 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
36a117a5 MM |
5263 | } |
5264 | else | |
5265 | /* This is not a template type, so there's nothing to do. */ | |
5266 | return t; | |
5267 | ||
5268 | default: | |
4393e105 | 5269 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
5270 | } |
5271 | } | |
5272 | ||
ae58fa02 MM |
5273 | /* Substitute the ARGS into the T, which is a _DECL. TYPE is the |
5274 | (already computed) substitution of ARGS into TREE_TYPE (T), if | |
5275 | appropriate. Return the result of the substitution. IN_DECL is as | |
5276 | for tsubst. */ | |
00d3396f | 5277 | |
e9659ab0 | 5278 | static tree |
ae58fa02 MM |
5279 | tsubst_decl (t, args, type, in_decl) |
5280 | tree t; | |
5281 | tree args; | |
5282 | tree type; | |
8d08fdba MS |
5283 | tree in_decl; |
5284 | { | |
ae58fa02 MM |
5285 | int saved_lineno; |
5286 | char* saved_filename; | |
b370501f | 5287 | tree r = NULL_TREE; |
830bfa74 | 5288 | |
ae58fa02 MM |
5289 | /* Set the filename and linenumber to improve error-reporting. */ |
5290 | saved_lineno = lineno; | |
5291 | saved_filename = input_filename; | |
5292 | lineno = DECL_SOURCE_LINE (t); | |
5293 | input_filename = DECL_SOURCE_FILE (t); | |
b7484fbe | 5294 | |
8d08fdba MS |
5295 | switch (TREE_CODE (t)) |
5296 | { | |
98c1c668 JM |
5297 | case TEMPLATE_DECL: |
5298 | { | |
5299 | /* We can get here when processing a member template function | |
5300 | of a template class. */ | |
98c1c668 | 5301 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 5302 | tree spec; |
db2767b6 | 5303 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); |
98c1c668 | 5304 | |
db2767b6 MM |
5305 | if (!is_template_template_parm) |
5306 | { | |
36a117a5 MM |
5307 | /* We might already have an instance of this template. |
5308 | The ARGS are for the surrounding class type, so the | |
5309 | full args contain the tsubst'd args for the context, | |
5310 | plus the innermost args from the template decl. */ | |
5311 | tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
5312 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
5313 | : DECL_TI_ARGS (DECL_RESULT (t)); | |
7ddedda4 MM |
5314 | tree full_args; |
5315 | ||
5316 | push_momentary (); | |
4393e105 MM |
5317 | full_args = tsubst_template_arg_vector (tmpl_args, args, |
5318 | /*complain=*/1); | |
36a117a5 MM |
5319 | |
5320 | /* tsubst_template_arg_vector doesn't copy the vector if | |
5321 | nothing changed. But, *something* should have | |
5322 | changed. */ | |
5323 | my_friendly_assert (full_args != tmpl_args, 0); | |
5324 | ||
5325 | spec = retrieve_specialization (t, full_args); | |
7ddedda4 | 5326 | pop_momentary (); |
db2767b6 | 5327 | if (spec != NULL_TREE) |
ae58fa02 MM |
5328 | { |
5329 | r = spec; | |
5330 | break; | |
5331 | } | |
db2767b6 | 5332 | } |
98c1c668 JM |
5333 | |
5334 | /* Make a new template decl. It will be similar to the | |
5335 | original, but will record the current template arguments. | |
5336 | We also create a new function declaration, which is just | |
5337 | like the old one, but points to this new template, rather | |
5338 | than the old one. */ | |
ae58fa02 MM |
5339 | r = copy_node (t); |
5340 | copy_lang_decl (r); | |
5341 | my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); | |
5342 | TREE_CHAIN (r) = NULL_TREE; | |
db2767b6 MM |
5343 | |
5344 | if (is_template_template_parm) | |
5345 | { | |
4393e105 | 5346 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5347 | DECL_RESULT (r) = new_decl; |
5348 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5349 | break; | |
db2767b6 MM |
5350 | } |
5351 | ||
ae58fa02 | 5352 | DECL_CONTEXT (r) |
4393e105 MM |
5353 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, |
5354 | in_decl, /*entering_scope=*/1); | |
ae58fa02 | 5355 | DECL_CLASS_CONTEXT (r) |
4393e105 MM |
5356 | = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5357 | /*complain=*/1, in_decl, | |
5358 | /*entering_scope=*/1); | |
ae58fa02 | 5359 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
5360 | |
5361 | if (TREE_CODE (decl) == TYPE_DECL) | |
5362 | { | |
4393e105 MM |
5363 | tree new_type = tsubst (TREE_TYPE (t), args, |
5364 | /*complain=*/1, in_decl); | |
ae58fa02 MM |
5365 | TREE_TYPE (r) = new_type; |
5366 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
5367 | DECL_RESULT (r) = TYPE_MAIN_DECL (new_type); | |
5368 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); | |
93cdc044 JM |
5369 | } |
5370 | else | |
5371 | { | |
4393e105 | 5372 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5373 | DECL_RESULT (r) = new_decl; |
5374 | DECL_TI_TEMPLATE (new_decl) = r; | |
5375 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5376 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
5377 | } |
5378 | ||
ae58fa02 MM |
5379 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5380 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
5381 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
5382 | |
5383 | /* The template parameters for this new template are all the | |
5384 | template parameters for the old template, except the | |
5385 | outermost level of parameters. */ | |
ae58fa02 | 5386 | DECL_TEMPLATE_PARMS (r) |
4393e105 MM |
5387 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
5388 | /*complain=*/1); | |
98c1c668 | 5389 | |
93cdc044 | 5390 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 5391 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 5392 | |
8d019cef | 5393 | /* We don't partially instantiate partial specializations. */ |
93cdc044 | 5394 | if (TREE_CODE (decl) == TYPE_DECL) |
ae58fa02 | 5395 | break; |
93cdc044 | 5396 | |
75650646 MM |
5397 | for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t); |
5398 | spec != NULL_TREE; | |
5399 | spec = TREE_CHAIN (spec)) | |
5400 | { | |
5401 | /* It helps to consider example here. Consider: | |
5402 | ||
5403 | template <class T> | |
5404 | struct S { | |
5405 | template <class U> | |
5406 | void f(U u); | |
5407 | ||
5408 | template <> | |
5409 | void f(T* t) {} | |
5410 | }; | |
5411 | ||
5412 | Now, for example, we are instantiating S<int>::f(U u). | |
5413 | We want to make a template: | |
5414 | ||
5415 | template <class U> | |
5416 | void S<int>::f(U); | |
5417 | ||
5418 | It will have a specialization, for the case U = int*, of | |
5419 | the form: | |
5420 | ||
5421 | template <> | |
5422 | void S<int>::f<int*>(int*); | |
5423 | ||
5424 | This specialization will be an instantiation of | |
5425 | the specialization given in the declaration of S, with | |
5426 | argument list int*. */ | |
5427 | ||
5428 | tree fn = TREE_VALUE (spec); | |
5429 | tree spec_args; | |
5430 | tree new_fn; | |
5431 | ||
5432 | if (!DECL_TEMPLATE_SPECIALIZATION (fn)) | |
5433 | /* Instantiations are on the same list, but they're of | |
5434 | no concern to us. */ | |
5435 | continue; | |
5436 | ||
36a117a5 MM |
5437 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
5438 | /* A full specialization. There's no need to record | |
5439 | that here. */ | |
5440 | continue; | |
5441 | ||
4393e105 MM |
5442 | spec_args = tsubst (DECL_TI_ARGS (fn), args, |
5443 | /*complain=*/1, in_decl); | |
36a117a5 | 5444 | new_fn = tsubst (DECL_RESULT (most_general_template (fn)), |
4393e105 | 5445 | spec_args, /*complain=*/1, in_decl); |
36a117a5 | 5446 | DECL_TI_TEMPLATE (new_fn) = fn; |
ae58fa02 | 5447 | register_specialization (new_fn, r, |
36a117a5 | 5448 | innermost_args (spec_args)); |
75650646 MM |
5449 | } |
5450 | ||
5451 | /* Record this partial instantiation. */ | |
ae58fa02 MM |
5452 | register_specialization (r, t, |
5453 | DECL_TI_ARGS (DECL_RESULT (r))); | |
75650646 | 5454 | |
98c1c668 | 5455 | } |
ae58fa02 | 5456 | break; |
8d08fdba MS |
5457 | |
5458 | case FUNCTION_DECL: | |
5459 | { | |
386b8a85 | 5460 | tree ctx; |
87603ed0 | 5461 | tree argvec = NULL_TREE; |
cf38f48a | 5462 | tree *friends; |
36a117a5 | 5463 | tree gen_tmpl; |
5566b478 | 5464 | int member; |
d8c4447d MM |
5465 | int args_depth; |
5466 | int parms_depth; | |
5566b478 | 5467 | |
36a117a5 MM |
5468 | /* Nobody should be tsubst'ing into non-template functions. */ |
5469 | my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5470 | ||
5471 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
5472 | { | |
5473 | tree spec; | |
5474 | ||
7ddedda4 MM |
5475 | /* Allocate template arguments on the momentary obstack, |
5476 | in case we don't need to keep them. */ | |
5477 | push_momentary (); | |
5478 | ||
36a117a5 MM |
5479 | /* Calculate the most general template of which R is a |
5480 | specialization, and the complete set of arguments used to | |
5481 | specialize R. */ | |
5482 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
7ddedda4 MM |
5483 | argvec |
5484 | = tsubst_template_arg_vector (DECL_TI_ARGS | |
5485 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
4393e105 | 5486 | args, /*complain=*/1); |
36a117a5 MM |
5487 | |
5488 | /* Check to see if we already have this specialization. */ | |
5489 | spec = retrieve_specialization (gen_tmpl, argvec); | |
7ddedda4 | 5490 | |
36a117a5 | 5491 | if (spec) |
ae58fa02 MM |
5492 | { |
5493 | r = spec; | |
7ddedda4 | 5494 | pop_momentary (); |
ae58fa02 MM |
5495 | break; |
5496 | } | |
d8c4447d | 5497 | |
7ddedda4 MM |
5498 | /* We're going to need to keep the ARGVEC, so we copy it |
5499 | here. */ | |
5500 | argvec = copy_to_permanent (argvec); | |
5501 | pop_momentary (); | |
5502 | ||
d8c4447d MM |
5503 | /* Here, we deal with the peculiar case: |
5504 | ||
5505 | template <class T> struct S { | |
5506 | template <class U> friend void f(); | |
5507 | }; | |
5508 | template <class U> friend void f() {} | |
5509 | template S<int>; | |
5510 | template void f<double>(); | |
5511 | ||
5512 | Here, the ARGS for the instantiation of will be {int, | |
5513 | double}. But, we only need as many ARGS as there are | |
5514 | levels of template parameters in CODE_PATTERN. We are | |
5515 | careful not to get fooled into reducing the ARGS in | |
5516 | situations like: | |
5517 | ||
5518 | template <class T> struct S { template <class U> void f(U); } | |
5519 | template <class T> template <> void S<T>::f(int) {} | |
5520 | ||
5521 | which we can spot because the pattern will be a | |
5522 | specialization in this case. */ | |
5523 | args_depth = TMPL_ARGS_DEPTH (args); | |
5524 | parms_depth = | |
5525 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
5526 | if (args_depth > parms_depth | |
5527 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
5528 | { | |
5529 | my_friendly_assert (DECL_FRIEND_P (t), 0); | |
5530 | ||
5531 | if (parms_depth > 1) | |
5532 | { | |
5533 | int i; | |
5534 | ||
5535 | args = make_temp_vec (parms_depth); | |
5536 | for (i = 0; i < parms_depth; ++i) | |
5537 | TREE_VEC_ELT (args, i) = | |
5538 | TREE_VEC_ELT (args, i + (args_depth - parms_depth)); | |
5539 | } | |
5540 | else | |
5541 | args = TREE_VEC_ELT (args, args_depth - parms_depth); | |
5542 | } | |
36a117a5 MM |
5543 | } |
5544 | else | |
5545 | { | |
5546 | /* This special case arises when we have something like this: | |
5547 | ||
5548 | template <class T> struct S { | |
5549 | friend void f<int>(int, double); | |
5550 | }; | |
5551 | ||
5552 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
39c01e4c MM |
5553 | will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are |
5554 | being called from tsubst_friend_function, and we want | |
5555 | only to create a new decl (R) with appropriate types so | |
5556 | that we can call determine_specialization. */ | |
5557 | my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5558 | == LOOKUP_EXPR) | |
5559 | || (TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5560 | == IDENTIFIER_NODE), 0); | |
36a117a5 MM |
5561 | gen_tmpl = NULL_TREE; |
5562 | } | |
5563 | ||
6eb3bb27 | 5564 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 5565 | { |
5566b478 MS |
5566 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
5567 | member = 2; | |
5568 | else | |
5569 | member = 1; | |
4393e105 MM |
5570 | ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5571 | /*complain=*/1, t, | |
36a117a5 | 5572 | /*entering_scope=*/1); |
5566b478 MS |
5573 | } |
5574 | else | |
5575 | { | |
5576 | member = 0; | |
5577 | ctx = NULL_TREE; | |
5566b478 | 5578 | } |
4393e105 | 5579 | type = tsubst (type, args, /*complain=*/1, in_decl); |
8d08fdba | 5580 | |
5566b478 MS |
5581 | /* We do NOT check for matching decls pushed separately at this |
5582 | point, as they may not represent instantiations of this | |
5583 | template, and in any case are considered separate under the | |
312e7d50 | 5584 | discrete model. Instead, see add_maybe_template. */ |
5566b478 MS |
5585 | |
5586 | r = copy_node (t); | |
5587 | copy_lang_decl (r); | |
e1467ff2 | 5588 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 MS |
5589 | TREE_TYPE (r) = type; |
5590 | ||
5591 | DECL_CONTEXT (r) | |
4393e105 MM |
5592 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, t, |
5593 | /*entering_scope=*/1); | |
5566b478 MS |
5594 | DECL_CLASS_CONTEXT (r) = ctx; |
5595 | ||
4c571114 | 5596 | if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r))) |
f181d4ae MM |
5597 | /* Type-conversion operator. Reconstruct the name, in |
5598 | case it's the name of one of the template's parameters. */ | |
5599 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
5566b478 | 5600 | |
4393e105 MM |
5601 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
5602 | /*complain=*/1, t); | |
711734a9 JM |
5603 | DECL_MAIN_VARIANT (r) = r; |
5604 | DECL_RESULT (r) = NULL_TREE; | |
711734a9 JM |
5605 | |
5606 | TREE_STATIC (r) = 0; | |
5607 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
5608 | DECL_EXTERNAL (r) = 1; | |
5609 | DECL_INTERFACE_KNOWN (r) = 0; | |
5610 | DECL_DEFER_OUTPUT (r) = 0; | |
5611 | TREE_CHAIN (r) = NULL_TREE; | |
5612 | DECL_PENDING_INLINE_INFO (r) = 0; | |
5613 | TREE_USED (r) = 0; | |
5614 | ||
36a117a5 MM |
5615 | /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled |
5616 | name. There's no need to do this in the special friend | |
5617 | case mentioned above where GEN_TMPL is NULL. */ | |
5618 | if (gen_tmpl) | |
386b8a85 | 5619 | { |
7ddedda4 MM |
5620 | /* The ARGVEC was built on the momentary obstack. Make it |
5621 | permanent now. */ | |
5622 | argvec = copy_to_permanent (argvec); | |
36a117a5 MM |
5623 | DECL_TEMPLATE_INFO (r) |
5624 | = perm_tree_cons (gen_tmpl, argvec, NULL_TREE); | |
5625 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5626 | register_specialization (r, gen_tmpl, argvec); | |
5627 | ||
5628 | /* Set the mangled name for R. */ | |
5629 | if (DECL_DESTRUCTOR_P (t)) | |
5630 | DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx); | |
5631 | else | |
386b8a85 | 5632 | { |
36a117a5 MM |
5633 | /* Instantiations of template functions must be mangled |
5634 | specially, in order to conform to 14.5.5.1 | |
5635 | [temp.over.link]. */ | |
5636 | tree tmpl = DECL_TI_TEMPLATE (t); | |
386b8a85 | 5637 | |
36a117a5 MM |
5638 | /* TMPL will be NULL if this is a specialization of a |
5639 | member function of a template class. */ | |
5640 | if (name_mangling_version < 1 | |
5641 | || tmpl == NULL_TREE | |
5642 | || (member && !is_member_template (tmpl) | |
5643 | && !DECL_TEMPLATE_INFO (tmpl))) | |
5644 | set_mangled_name_for_decl (r); | |
75650646 | 5645 | else |
36a117a5 | 5646 | set_mangled_name_for_template_decl (r); |
386b8a85 | 5647 | } |
36a117a5 MM |
5648 | |
5649 | DECL_RTL (r) = 0; | |
5650 | make_decl_rtl (r, NULL_PTR, 1); | |
5651 | ||
5652 | /* Like grokfndecl. If we don't do this, pushdecl will | |
5653 | mess up our TREE_CHAIN because it doesn't find a | |
5654 | previous decl. Sigh. */ | |
5655 | if (member | |
c36ae80f | 5656 | && ! uses_template_parms (r) |
36a117a5 MM |
5657 | && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) |
5658 | == NULL_TREE)) | |
5659 | SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r); | |
386b8a85 | 5660 | } |
f181d4ae | 5661 | |
cf38f48a MM |
5662 | /* Copy the list of befriending classes. */ |
5663 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
5664 | *friends; | |
5665 | friends = &TREE_CHAIN (*friends)) | |
5666 | { | |
5667 | *friends = copy_node (*friends); | |
5668 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
5669 | args, /*complain=*/1, | |
5670 | in_decl); | |
5671 | } | |
5672 | ||
f181d4ae MM |
5673 | if (DECL_CONSTRUCTOR_P (r)) |
5674 | { | |
5675 | maybe_retrofit_in_chrg (r); | |
5676 | grok_ctor_properties (ctx, r); | |
5677 | } | |
5678 | if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) | |
5679 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); | |
8d08fdba | 5680 | } |
ae58fa02 | 5681 | break; |
8d08fdba MS |
5682 | |
5683 | case PARM_DECL: | |
5684 | { | |
ae58fa02 | 5685 | r = copy_node (t); |
5566b478 | 5686 | TREE_TYPE (r) = type; |
db2767b6 MM |
5687 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
5688 | DECL_INITIAL (r) = TREE_TYPE (r); | |
5689 | else | |
4393e105 MM |
5690 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, |
5691 | /*complain=*/1, in_decl); | |
db2767b6 | 5692 | |
5566b478 | 5693 | DECL_CONTEXT (r) = NULL_TREE; |
f83b0cb6 JM |
5694 | #ifdef PROMOTE_PROTOTYPES |
5695 | if ((TREE_CODE (type) == INTEGER_TYPE | |
5696 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
5697 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) | |
5698 | DECL_ARG_TYPE (r) = integer_type_node; | |
5699 | #endif | |
8d08fdba | 5700 | if (TREE_CHAIN (t)) |
4393e105 MM |
5701 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
5702 | /*complain=*/1, TREE_CHAIN (t)); | |
8d08fdba | 5703 | } |
ae58fa02 | 5704 | break; |
8d08fdba | 5705 | |
5566b478 MS |
5706 | case FIELD_DECL: |
5707 | { | |
ae58fa02 | 5708 | r = copy_node (t); |
5566b478 MS |
5709 | TREE_TYPE (r) = type; |
5710 | copy_lang_decl (r); | |
5711 | #if 0 | |
4393e105 MM |
5712 | DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, |
5713 | /*complain=*/1, in_decl); | |
5566b478 | 5714 | #endif |
4393e105 MM |
5715 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, |
5716 | /*complain=*/1, in_decl); | |
5566b478 | 5717 | TREE_CHAIN (r) = NULL_TREE; |
4393e105 | 5718 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 5719 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5720 | } |
ae58fa02 | 5721 | break; |
5566b478 MS |
5722 | |
5723 | case USING_DECL: | |
5724 | { | |
ae58fa02 | 5725 | r = copy_node (t); |
5566b478 | 5726 | DECL_INITIAL (r) |
4393e105 | 5727 | = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl); |
5566b478 | 5728 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 5729 | } |
ae58fa02 | 5730 | break; |
5566b478 MS |
5731 | |
5732 | case VAR_DECL: | |
5733 | { | |
36a117a5 MM |
5734 | tree argvec; |
5735 | tree gen_tmpl; | |
5736 | tree spec; | |
5737 | tree tmpl; | |
4393e105 MM |
5738 | tree ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
5739 | /*complain=*/1, | |
5740 | in_decl, /*entering_scope=*/1); | |
36a117a5 MM |
5741 | |
5742 | /* Nobody should be tsubst'ing into non-template variables. */ | |
5743 | my_friendly_assert (DECL_LANG_SPECIFIC (t) | |
5744 | && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5566b478 | 5745 | |
36a117a5 MM |
5746 | /* Check to see if we already have this specialization. */ |
5747 | tmpl = DECL_TI_TEMPLATE (t); | |
5748 | gen_tmpl = most_general_template (tmpl); | |
4393e105 | 5749 | argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl); |
36a117a5 MM |
5750 | spec = retrieve_specialization (gen_tmpl, argvec); |
5751 | ||
5752 | if (spec) | |
ae58fa02 MM |
5753 | { |
5754 | r = spec; | |
5755 | break; | |
5756 | } | |
5566b478 MS |
5757 | |
5758 | r = copy_node (t); | |
5759 | TREE_TYPE (r) = type; | |
5760 | DECL_CONTEXT (r) = ctx; | |
5761 | if (TREE_STATIC (r)) | |
5762 | DECL_ASSEMBLER_NAME (r) | |
5763 | = build_static_name (DECL_CONTEXT (r), DECL_NAME (r)); | |
d11ad92e MS |
5764 | |
5765 | /* Don't try to expand the initializer until someone tries to use | |
5766 | this variable; otherwise we run into circular dependencies. */ | |
5767 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 MS |
5768 | DECL_RTL (r) = 0; |
5769 | DECL_SIZE (r) = 0; | |
36a117a5 MM |
5770 | copy_lang_decl (r); |
5771 | DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r); | |
5566b478 | 5772 | |
36a117a5 MM |
5773 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); |
5774 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5775 | register_specialization (r, gen_tmpl, argvec); | |
5566b478 | 5776 | |
5566b478 | 5777 | TREE_CHAIN (r) = NULL_TREE; |
519ebd1e | 5778 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 5779 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5780 | } |
ae58fa02 | 5781 | break; |
5566b478 MS |
5782 | |
5783 | case TYPE_DECL: | |
d2e5ee5c | 5784 | if (t == TYPE_NAME (TREE_TYPE (t))) |
ae58fa02 MM |
5785 | r = TYPE_NAME (type); |
5786 | else | |
5787 | { | |
5788 | r = copy_node (t); | |
5789 | TREE_TYPE (r) = type; | |
5790 | DECL_CONTEXT (r) = current_class_type; | |
5791 | TREE_CHAIN (r) = NULL_TREE; | |
5792 | } | |
5793 | break; | |
5794 | ||
5795 | default: | |
5796 | my_friendly_abort (0); | |
5797 | } | |
5798 | ||
5799 | /* Restore the file and line information. */ | |
5800 | lineno = saved_lineno; | |
5801 | input_filename = saved_filename; | |
5802 | ||
5803 | return r; | |
5804 | } | |
5805 | ||
cabc336a MM |
5806 | /* Substitue into the ARG_TYPES of a function type. */ |
5807 | ||
e9659ab0 | 5808 | static tree |
4393e105 | 5809 | tsubst_arg_types (arg_types, args, complain, in_decl) |
cabc336a MM |
5810 | tree arg_types; |
5811 | tree args; | |
4393e105 | 5812 | int complain; |
cabc336a MM |
5813 | tree in_decl; |
5814 | { | |
5815 | tree remaining_arg_types; | |
cabc336a MM |
5816 | tree type; |
5817 | ||
5818 | if (!arg_types || arg_types == void_list_node) | |
5819 | return arg_types; | |
5820 | ||
5821 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
4393e105 MM |
5822 | args, complain, in_decl); |
5823 | if (remaining_arg_types == error_mark_node) | |
5824 | return error_mark_node; | |
5825 | ||
5826 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
5827 | if (type == error_mark_node) | |
5828 | return error_mark_node; | |
cabc336a | 5829 | |
4393e105 MM |
5830 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
5831 | top-level qualifiers as required. */ | |
5832 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a MM |
5833 | |
5834 | /* Note that we do not substitute into default arguments here. The | |
5835 | standard mandates that they be instantiated only when needed, | |
5836 | which is done in build_over_call. */ | |
5837 | return hash_tree_cons_simple (TREE_PURPOSE (arg_types), type, | |
5838 | remaining_arg_types); | |
5839 | ||
5840 | } | |
5841 | ||
4393e105 MM |
5842 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
5843 | *not* handle the exception-specification for FNTYPE, because the | |
5844 | initial substitution of explicitly provided template parameters | |
5845 | during argument deduction forbids substitution into the | |
5846 | exception-specification: | |
5847 | ||
5848 | [temp.deduct] | |
5849 | ||
5850 | All references in the function type of the function template to the | |
5851 | corresponding template parameters are replaced by the specified tem- | |
5852 | plate argument values. If a substitution in a template parameter or | |
5853 | in the function type of the function template results in an invalid | |
5854 | type, type deduction fails. [Note: The equivalent substitution in | |
5855 | exception specifications is done only when the function is instanti- | |
5856 | ated, at which point a program is ill-formed if the substitution | |
5857 | results in an invalid type.] */ | |
5858 | ||
5859 | static tree | |
5860 | tsubst_function_type (t, args, complain, in_decl) | |
5861 | tree t; | |
5862 | tree args; | |
5863 | int complain; | |
5864 | tree in_decl; | |
5865 | { | |
5866 | tree return_type; | |
5867 | tree arg_types; | |
5868 | tree fntype; | |
5869 | ||
5870 | /* The TYPE_CONTEXT is not used for function/method types. */ | |
5871 | my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); | |
5872 | ||
5873 | /* Substitue the return type. */ | |
5874 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
5875 | if (return_type == error_mark_node) | |
5876 | return error_mark_node; | |
5877 | ||
5878 | /* Substitue the argument types. */ | |
5879 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, | |
5880 | complain, in_decl); | |
5881 | if (arg_types == error_mark_node) | |
5882 | return error_mark_node; | |
5883 | ||
5884 | /* Construct a new type node and return it. */ | |
5885 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
5886 | fntype = build_function_type (return_type, arg_types); | |
5887 | else | |
5888 | { | |
5889 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
5890 | if (! IS_AGGR_TYPE (r)) | |
5891 | { | |
5892 | /* [temp.deduct] | |
5893 | ||
5894 | Type deduction may fail for any of the following | |
5895 | reasons: | |
5896 | ||
5897 | -- Attempting to create "pointer to member of T" when T | |
5898 | is not a class type. */ | |
5899 | if (complain) | |
5900 | cp_error ("creating pointer to member function of non-class type `%T'", | |
5901 | r); | |
5902 | return error_mark_node; | |
5903 | } | |
5904 | ||
5905 | fntype = build_cplus_method_type (r, return_type, TREE_CHAIN | |
5906 | (arg_types)); | |
5907 | } | |
5908 | fntype = build_qualified_type (fntype, TYPE_QUALS (t)); | |
5909 | ||
5910 | return fntype; | |
5911 | } | |
5912 | ||
297e73d8 MM |
5913 | /* Substitute into the PARMS of a call-declarator. */ |
5914 | ||
e9659ab0 | 5915 | static tree |
4393e105 | 5916 | tsubst_call_declarator_parms (parms, args, complain, in_decl) |
297e73d8 MM |
5917 | tree parms; |
5918 | tree args; | |
4393e105 | 5919 | int complain; |
297e73d8 MM |
5920 | tree in_decl; |
5921 | { | |
5922 | tree new_parms; | |
5923 | tree type; | |
5924 | tree defarg; | |
5925 | ||
5926 | if (!parms || parms == void_list_node) | |
5927 | return parms; | |
5928 | ||
5929 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
4393e105 | 5930 | args, complain, in_decl); |
297e73d8 MM |
5931 | |
5932 | /* Figure out the type of this parameter. */ | |
4393e105 | 5933 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); |
297e73d8 MM |
5934 | |
5935 | /* Figure out the default argument as well. Note that we use | |
4393e105 MM |
5936 | tsubst_expr since the default argument is really an expression. */ |
5937 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
297e73d8 MM |
5938 | |
5939 | /* Chain this parameter on to the front of those we have already | |
5940 | processed. We don't use hash_tree_cons because that function | |
5941 | doesn't check TREE_PARMLIST. */ | |
5942 | new_parms = tree_cons (defarg, type, new_parms); | |
5943 | ||
5944 | /* And note that these are parameters. */ | |
5945 | TREE_PARMLIST (new_parms) = 1; | |
5946 | ||
5947 | return new_parms; | |
5948 | } | |
5949 | ||
4393e105 MM |
5950 | /* Take the tree structure T and replace template parameters used |
5951 | therein with the argument vector ARGS. IN_DECL is an associated | |
5952 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
5953 | An appropriate error message is issued only if COMPLAIN is | |
5954 | non-zero. Note that we must be relatively non-tolerant of | |
5955 | extensions here, in order to preserve conformance; if we allow | |
5956 | substitutions that should not be allowed, we may allow argument | |
5957 | deductions that should not succeed, and therefore report ambiguous | |
5958 | overload situations where there are none. In theory, we could | |
5959 | allow the substitution, but indicate that it should have failed, | |
5960 | and allow our caller to make sure that the right thing happens, but | |
5961 | we don't try to do this yet. | |
5962 | ||
5963 | This function is used for dealing with types, decls and the like; | |
5964 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 MM |
5965 | |
5966 | tree | |
4393e105 | 5967 | tsubst (t, args, complain, in_decl) |
ae58fa02 | 5968 | tree t, args; |
4393e105 | 5969 | int complain; |
ae58fa02 MM |
5970 | tree in_decl; |
5971 | { | |
0ecfe0b4 | 5972 | tree type, r; |
ae58fa02 MM |
5973 | |
5974 | if (t == NULL_TREE || t == error_mark_node | |
5975 | || t == integer_type_node | |
5976 | || t == void_type_node | |
5977 | || t == char_type_node | |
5978 | || TREE_CODE (t) == NAMESPACE_DECL) | |
5979 | return t; | |
5980 | ||
5981 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
5982 | type = IDENTIFIER_TYPE_VALUE (t); | |
5983 | else | |
5984 | type = TREE_TYPE (t); | |
5985 | if (type == unknown_type_node) | |
5986 | my_friendly_abort (42); | |
5987 | ||
5988 | if (type && TREE_CODE (t) != FUNCTION_DECL | |
5989 | && TREE_CODE (t) != TYPENAME_TYPE | |
5990 | && TREE_CODE (t) != TEMPLATE_DECL | |
4393e105 MM |
5991 | && TREE_CODE (t) != IDENTIFIER_NODE |
5992 | && TREE_CODE (t) != FUNCTION_TYPE | |
5993 | && TREE_CODE (t) != METHOD_TYPE) | |
5994 | type = tsubst (type, args, complain, in_decl); | |
5995 | if (type == error_mark_node) | |
5996 | return error_mark_node; | |
ae58fa02 MM |
5997 | |
5998 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd') | |
5999 | return tsubst_decl (t, args, type, in_decl); | |
6000 | ||
6001 | switch (TREE_CODE (t)) | |
6002 | { | |
6003 | case RECORD_TYPE: | |
6004 | case UNION_TYPE: | |
6005 | case ENUMERAL_TYPE: | |
4393e105 MM |
6006 | return tsubst_aggr_type (t, args, complain, in_decl, |
6007 | /*entering_scope=*/0); | |
ae58fa02 MM |
6008 | |
6009 | case ERROR_MARK: | |
6010 | case IDENTIFIER_NODE: | |
6011 | case OP_IDENTIFIER: | |
6012 | case VOID_TYPE: | |
6013 | case REAL_TYPE: | |
6014 | case COMPLEX_TYPE: | |
6015 | case BOOLEAN_TYPE: | |
6016 | case INTEGER_CST: | |
6017 | case REAL_CST: | |
6018 | case STRING_CST: | |
6019 | return t; | |
6020 | ||
6021 | case INTEGER_TYPE: | |
6022 | if (t == integer_type_node) | |
6023 | return t; | |
6024 | ||
6025 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
6026 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
6027 | return t; | |
d2e5ee5c | 6028 | |
5566b478 | 6029 | { |
ddce3528 | 6030 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 6031 | |
ddce3528 | 6032 | max = tsubst_expr (omax, args, complain, in_decl); |
4393e105 MM |
6033 | if (max == error_mark_node) |
6034 | return error_mark_node; | |
6035 | ||
ae58fa02 MM |
6036 | if (processing_template_decl) |
6037 | { | |
6038 | tree itype = make_node (INTEGER_TYPE); | |
6039 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
6040 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, | |
6041 | integer_one_node); | |
6042 | return itype; | |
6043 | } | |
6044 | ||
ddce3528 JM |
6045 | if (integer_zerop (omax)) |
6046 | { | |
6047 | /* Still allow an explicit array of size zero. */ | |
6048 | if (pedantic) | |
6049 | pedwarn ("creating array with size zero"); | |
6050 | } | |
6051 | else if (integer_zerop (max) || INT_CST_LT (max, integer_zero_node)) | |
0ecfe0b4 | 6052 | { |
4393e105 MM |
6053 | /* [temp.deduct] |
6054 | ||
6055 | Type deduction may fail for any of the following | |
6056 | reasons: | |
6057 | ||
bf8f3f93 MM |
6058 | Attempting to create an array with a size that is |
6059 | zero or negative. */ | |
4393e105 MM |
6060 | if (complain) |
6061 | cp_error ("creating array with size `%E'", max); | |
6062 | ||
6063 | return error_mark_node; | |
0ecfe0b4 JM |
6064 | } |
6065 | ||
ae58fa02 | 6066 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1)); |
7ddedda4 MM |
6067 | if (!TREE_PERMANENT (max) && !allocation_temporary_p ()) |
6068 | max = copy_to_permanent (max); | |
120ecb8a | 6069 | return build_index_type (max); |
ae58fa02 MM |
6070 | } |
6071 | ||
6072 | case TEMPLATE_TYPE_PARM: | |
6073 | case TEMPLATE_TEMPLATE_PARM: | |
6074 | case TEMPLATE_PARM_INDEX: | |
6075 | { | |
6076 | int idx; | |
6077 | int level; | |
6078 | int levels; | |
0ecfe0b4 JM |
6079 | |
6080 | r = NULL_TREE; | |
ae58fa02 MM |
6081 | |
6082 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
6083 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6084 | { | |
6085 | idx = TEMPLATE_TYPE_IDX (t); | |
6086 | level = TEMPLATE_TYPE_LEVEL (t); | |
6087 | } | |
6088 | else | |
6089 | { | |
6090 | idx = TEMPLATE_PARM_IDX (t); | |
6091 | level = TEMPLATE_PARM_LEVEL (t); | |
6092 | } | |
6093 | ||
6094 | if (TREE_VEC_LENGTH (args) > 0) | |
6095 | { | |
6096 | tree arg = NULL_TREE; | |
6097 | ||
6098 | levels = TMPL_ARGS_DEPTH (args); | |
6099 | if (level <= levels) | |
6100 | arg = TMPL_ARG (args, level, idx); | |
6101 | ||
8b5b8b7c MM |
6102 | if (arg == error_mark_node) |
6103 | return error_mark_node; | |
6104 | else if (arg != NULL_TREE) | |
ae58fa02 MM |
6105 | { |
6106 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
6107 | { | |
6108 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg)) | |
6109 | == 't', 0); | |
91063b51 MM |
6110 | return cp_build_qualified_type |
6111 | (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t)); | |
ae58fa02 MM |
6112 | } |
6113 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6114 | { | |
7ddedda4 | 6115 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 MM |
6116 | { |
6117 | /* We are processing a type constructed from | |
6118 | a template template parameter */ | |
7ddedda4 | 6119 | tree argvec = tsubst (TYPE_TI_ARGS (t), |
4393e105 MM |
6120 | args, complain, in_decl); |
6121 | if (argvec == error_mark_node) | |
6122 | return error_mark_node; | |
6123 | ||
ae58fa02 MM |
6124 | /* We can get a TEMPLATE_TEMPLATE_PARM here when |
6125 | we are resolving nested-types in the signature of | |
6126 | a member function templates. | |
6127 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
6128 | template to be instantiated. */ | |
6129 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
6130 | arg = TYPE_NAME (arg); | |
6131 | ||
6132 | r = lookup_template_class (DECL_NAME (arg), | |
6133 | argvec, in_decl, | |
6134 | DECL_CONTEXT (arg), | |
6135 | /*entering_scope=*/0); | |
91063b51 | 6136 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
ae58fa02 MM |
6137 | } |
6138 | else | |
6139 | /* We are processing a template argument list. */ | |
6140 | return arg; | |
6141 | } | |
6142 | else | |
6143 | return arg; | |
6144 | } | |
6145 | } | |
51924768 JM |
6146 | else |
6147 | my_friendly_abort (981018); | |
ae58fa02 MM |
6148 | |
6149 | if (level == 1) | |
6150 | /* This can happen during the attempted tsubst'ing in | |
6151 | unify. This means that we don't yet have any information | |
6152 | about the template parameter in question. */ | |
6153 | return t; | |
6154 | ||
6155 | /* If we get here, we must have been looking at a parm for a | |
6156 | more deeply nested template. Make a new version of this | |
6157 | template parameter, but with a lower level. */ | |
6158 | switch (TREE_CODE (t)) | |
6159 | { | |
6160 | case TEMPLATE_TYPE_PARM: | |
6161 | case TEMPLATE_TEMPLATE_PARM: | |
6162 | r = copy_node (t); | |
6163 | TEMPLATE_TYPE_PARM_INDEX (r) | |
6164 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
6165 | r, levels); | |
6166 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
6167 | TYPE_MAIN_VARIANT (r) = r; | |
6168 | TYPE_POINTER_TO (r) = NULL_TREE; | |
6169 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
6170 | ||
6171 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 6172 | && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 | 6173 | { |
4393e105 MM |
6174 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, |
6175 | complain, in_decl); | |
6176 | if (argvec == error_mark_node) | |
6177 | return error_mark_node; | |
6178 | ||
7ddedda4 | 6179 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
ae58fa02 MM |
6180 | = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE); |
6181 | } | |
6182 | break; | |
6183 | ||
6184 | case TEMPLATE_PARM_INDEX: | |
6185 | r = reduce_template_parm_level (t, type, levels); | |
6186 | break; | |
6187 | ||
6188 | default: | |
6189 | my_friendly_abort (0); | |
6190 | } | |
6191 | ||
5566b478 | 6192 | return r; |
ae58fa02 | 6193 | } |
5566b478 | 6194 | |
8d08fdba MS |
6195 | case TREE_LIST: |
6196 | { | |
6197 | tree purpose, value, chain, result; | |
6198 | int via_public, via_virtual, via_protected; | |
6199 | ||
6200 | if (t == void_list_node) | |
6201 | return t; | |
6202 | ||
6203 | via_public = TREE_VIA_PUBLIC (t); | |
6204 | via_protected = TREE_VIA_PROTECTED (t); | |
6205 | via_virtual = TREE_VIA_VIRTUAL (t); | |
6206 | ||
6207 | purpose = TREE_PURPOSE (t); | |
6208 | if (purpose) | |
4393e105 MM |
6209 | { |
6210 | purpose = tsubst (purpose, args, complain, in_decl); | |
6211 | if (purpose == error_mark_node) | |
6212 | return error_mark_node; | |
6213 | } | |
8d08fdba MS |
6214 | value = TREE_VALUE (t); |
6215 | if (value) | |
4393e105 MM |
6216 | { |
6217 | value = tsubst (value, args, complain, in_decl); | |
6218 | if (value == error_mark_node) | |
6219 | return error_mark_node; | |
6220 | } | |
8d08fdba MS |
6221 | chain = TREE_CHAIN (t); |
6222 | if (chain && chain != void_type_node) | |
4393e105 MM |
6223 | { |
6224 | chain = tsubst (chain, args, complain, in_decl); | |
6225 | if (chain == error_mark_node) | |
6226 | return error_mark_node; | |
6227 | } | |
8d08fdba MS |
6228 | if (purpose == TREE_PURPOSE (t) |
6229 | && value == TREE_VALUE (t) | |
6230 | && chain == TREE_CHAIN (t)) | |
6231 | return t; | |
6232 | result = hash_tree_cons (via_public, via_virtual, via_protected, | |
6233 | purpose, value, chain); | |
6234 | TREE_PARMLIST (result) = TREE_PARMLIST (t); | |
6235 | return result; | |
6236 | } | |
6237 | case TREE_VEC: | |
5566b478 MS |
6238 | if (type != NULL_TREE) |
6239 | { | |
1ceaad38 JM |
6240 | /* A binfo node. We always need to make a copy, of the node |
6241 | itself and of its BINFO_BASETYPES. */ | |
85b71cf2 | 6242 | |
5566b478 MS |
6243 | t = copy_node (t); |
6244 | ||
c7449f2b JM |
6245 | /* Make sure type isn't a typedef copy. */ |
6246 | type = BINFO_TYPE (TYPE_BINFO (type)); | |
6247 | ||
5566b478 | 6248 | TREE_TYPE (t) = complete_type (type); |
6633d636 MS |
6249 | if (IS_AGGR_TYPE (type)) |
6250 | { | |
6251 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
6252 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
6253 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
6254 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
6255 | } | |
5566b478 MS |
6256 | return t; |
6257 | } | |
85b71cf2 JM |
6258 | |
6259 | /* Otherwise, a vector of template arguments. */ | |
4393e105 | 6260 | return tsubst_template_arg_vector (t, args, complain); |
8d08fdba | 6261 | |
8d08fdba MS |
6262 | case POINTER_TYPE: |
6263 | case REFERENCE_TYPE: | |
6264 | { | |
8d08fdba | 6265 | enum tree_code code; |
79a7c7fa | 6266 | |
8d08fdba MS |
6267 | if (type == TREE_TYPE (t)) |
6268 | return t; | |
6269 | ||
6270 | code = TREE_CODE (t); | |
4393e105 MM |
6271 | |
6272 | ||
6273 | /* [temp.deduct] | |
6274 | ||
6275 | Type deduction may fail for any of the following | |
6276 | reasons: | |
6277 | ||
6278 | -- Attempting to create a pointer to reference type. | |
6279 | -- Attempting to create a reference to a reference type or | |
6280 | a reference to void. */ | |
0ecfe0b4 JM |
6281 | if (TREE_CODE (type) == REFERENCE_TYPE |
6282 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa JM |
6283 | { |
6284 | static int last_line = 0; | |
6285 | static char* last_file = 0; | |
6286 | ||
6287 | /* We keep track of the last time we issued this error | |
6288 | message to avoid spewing a ton of messages during a | |
6289 | single bad template instantiation. */ | |
4393e105 MM |
6290 | if (complain && (last_line != lineno || |
6291 | last_file != input_filename)) | |
79a7c7fa | 6292 | { |
0ecfe0b4 JM |
6293 | if (TREE_CODE (type) == VOID_TYPE) |
6294 | cp_error ("forming reference to void"); | |
6295 | else | |
6296 | cp_error ("forming %s to reference type `%T'", | |
6297 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
6298 | type); | |
79a7c7fa JM |
6299 | last_line = lineno; |
6300 | last_file = input_filename; | |
6301 | } | |
6302 | ||
4393e105 | 6303 | return error_mark_node; |
79a7c7fa JM |
6304 | } |
6305 | else if (code == POINTER_TYPE) | |
8d08fdba MS |
6306 | r = build_pointer_type (type); |
6307 | else | |
6308 | r = build_reference_type (type); | |
91063b51 | 6309 | r = cp_build_qualified_type (r, TYPE_QUALS (t)); |
79a7c7fa | 6310 | |
8d08fdba MS |
6311 | /* Will this ever be needed for TYPE_..._TO values? */ |
6312 | layout_type (r); | |
6313 | return r; | |
6314 | } | |
a4443a08 | 6315 | case OFFSET_TYPE: |
0ecfe0b4 | 6316 | { |
4393e105 MM |
6317 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
6318 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
6319 | { | |
6320 | /* [temp.deduct] | |
6321 | ||
6322 | Type deduction may fail for any of the following | |
6323 | reasons: | |
6324 | ||
6325 | -- Attempting to create "pointer to member of T" when T | |
6326 | is not a class type. */ | |
6327 | if (complain) | |
6328 | cp_error ("creating pointer to member of non-class type `%T'", | |
6329 | r); | |
6330 | return error_mark_node; | |
6331 | } | |
0ecfe0b4 JM |
6332 | return build_offset_type (r, type); |
6333 | } | |
8d08fdba MS |
6334 | case FUNCTION_TYPE: |
6335 | case METHOD_TYPE: | |
6336 | { | |
c11b6f21 | 6337 | tree fntype; |
4393e105 | 6338 | tree raises; |
8d08fdba | 6339 | |
4393e105 MM |
6340 | fntype = tsubst_function_type (t, args, complain, in_decl); |
6341 | if (fntype == error_mark_node) | |
6342 | return error_mark_node; | |
cabc336a MM |
6343 | |
6344 | /* Substitue the exception specification. */ | |
6345 | raises = TYPE_RAISES_EXCEPTIONS (t); | |
c11b6f21 MS |
6346 | if (raises) |
6347 | { | |
4393e105 MM |
6348 | raises = tsubst (raises, args, complain, in_decl); |
6349 | if (raises == error_mark_node) | |
6350 | return raises; | |
c11b6f21 MS |
6351 | fntype = build_exception_variant (fntype, raises); |
6352 | } | |
6353 | return fntype; | |
8d08fdba MS |
6354 | } |
6355 | case ARRAY_TYPE: | |
6356 | { | |
4393e105 MM |
6357 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
6358 | if (domain == error_mark_node) | |
6359 | return error_mark_node; | |
6360 | ||
6361 | /* As an optimization, we avoid regenerating the array type if | |
6362 | it will obviously be the same as T. */ | |
8d08fdba MS |
6363 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
6364 | return t; | |
0ecfe0b4 | 6365 | |
4393e105 MM |
6366 | /* These checks should match the ones in grokdeclarator. |
6367 | ||
6368 | [temp.deduct] | |
6369 | ||
6370 | The deduction may fail for any of the following reasons: | |
6371 | ||
6372 | -- Attempting to create an array with an element type that | |
6373 | is void, a function type, or a reference type. */ | |
6374 | if (TREE_CODE (type) == VOID_TYPE | |
6375 | || TREE_CODE (type) == FUNCTION_TYPE | |
6376 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 6377 | { |
4393e105 MM |
6378 | if (complain) |
6379 | cp_error ("creating array of `%T'", type); | |
6380 | return error_mark_node; | |
0ecfe0b4 JM |
6381 | } |
6382 | ||
8d08fdba MS |
6383 | r = build_cplus_array_type (type, domain); |
6384 | return r; | |
6385 | } | |
6386 | ||
8d08fdba | 6387 | case PLUS_EXPR: |
5566b478 | 6388 | case MINUS_EXPR: |
4393e105 MM |
6389 | { |
6390 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6391 | in_decl); | |
6392 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, | |
6393 | in_decl); | |
6394 | ||
6395 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6396 | return error_mark_node; | |
6397 | ||
6398 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); | |
6399 | } | |
8d08fdba MS |
6400 | |
6401 | case NEGATE_EXPR: | |
6402 | case NOP_EXPR: | |
4393e105 MM |
6403 | { |
6404 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6405 | in_decl); | |
6406 | if (e == error_mark_node) | |
6407 | return error_mark_node; | |
6408 | ||
6409 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); | |
6410 | } | |
8d08fdba | 6411 | |
5566b478 MS |
6412 | case TYPENAME_TYPE: |
6413 | { | |
4393e105 MM |
6414 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
6415 | in_decl, /*entering_scope=*/1); | |
6416 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
6417 | complain, in_decl); | |
6418 | ||
6419 | if (ctx == error_mark_node || f == error_mark_node) | |
6420 | return error_mark_node; | |
ae58fa02 | 6421 | |
bf8f3f93 MM |
6422 | if (!IS_AGGR_TYPE (ctx)) |
6423 | { | |
6424 | if (complain) | |
6425 | cp_error ("`%T' is not a class, struct, or union type", | |
6426 | ctx); | |
6427 | return error_mark_node; | |
6428 | } | |
6429 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
6430 | { | |
6431 | /* Normally, make_typename_type does not require that the CTX | |
6432 | have complete type in order to allow things like: | |
ae58fa02 | 6433 | |
bf8f3f93 | 6434 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 6435 | |
bf8f3f93 MM |
6436 | But, such constructs have already been resolved by this |
6437 | point, so here CTX really should have complete type, unless | |
6438 | it's a partial instantiation. */ | |
4393e105 MM |
6439 | ctx = complete_type (ctx); |
6440 | if (!TYPE_SIZE (ctx)) | |
6441 | { | |
6442 | if (complain) | |
6443 | incomplete_type_error (NULL_TREE, ctx); | |
6444 | return error_mark_node; | |
6445 | } | |
6446 | } | |
ae58fa02 | 6447 | |
b2b7d40a | 6448 | f = make_typename_type (ctx, f); |
91063b51 MM |
6449 | return cp_build_qualified_type (f, |
6450 | CP_TYPE_QUALS (f) | |
6451 | | CP_TYPE_QUALS (t)); | |
5566b478 MS |
6452 | } |
6453 | ||
6454 | case INDIRECT_REF: | |
4393e105 MM |
6455 | { |
6456 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6457 | in_decl); | |
6458 | if (e == error_mark_node) | |
6459 | return error_mark_node; | |
6460 | return make_pointer_declarator (type, e); | |
6461 | } | |
6462 | ||
5566b478 | 6463 | case ADDR_EXPR: |
4393e105 MM |
6464 | { |
6465 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6466 | in_decl); | |
6467 | if (e == error_mark_node) | |
6468 | return error_mark_node; | |
6469 | return make_reference_declarator (type, e); | |
6470 | } | |
5566b478 MS |
6471 | |
6472 | case ARRAY_REF: | |
4393e105 MM |
6473 | { |
6474 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6475 | in_decl); | |
6fb2c05a JM |
6476 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, |
6477 | in_decl); | |
4393e105 MM |
6478 | if (e1 == error_mark_node || e2 == error_mark_node) |
6479 | return error_mark_node; | |
6480 | ||
6481 | return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr); | |
6482 | } | |
5566b478 MS |
6483 | |
6484 | case CALL_EXPR: | |
4393e105 MM |
6485 | { |
6486 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6487 | in_decl); | |
6488 | tree e2 = tsubst_call_declarator_parms (TREE_OPERAND (t, 1), args, | |
6489 | complain, in_decl); | |
6490 | tree e3 = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6491 | ||
6492 | if (e1 == error_mark_node || e2 == error_mark_node | |
6493 | || e3 == error_mark_node) | |
6494 | return error_mark_node; | |
6495 | ||
6496 | return make_call_declarator (e1, e2, TREE_OPERAND (t, 2), e3); | |
6497 | } | |
5566b478 | 6498 | |
fc378698 | 6499 | case SCOPE_REF: |
4393e105 MM |
6500 | { |
6501 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6502 | in_decl); | |
6503 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
6504 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6505 | return error_mark_node; | |
6506 | ||
6507 | return build_parse_node (TREE_CODE (t), e1, e2); | |
6508 | } | |
fc378698 | 6509 | |
b894fc05 | 6510 | case TYPEOF_TYPE: |
4393e105 MM |
6511 | { |
6512 | tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, | |
6513 | in_decl); | |
6514 | if (e1 == error_mark_node) | |
6515 | return error_mark_node; | |
6516 | ||
6517 | return TREE_TYPE (e1); | |
6518 | } | |
b894fc05 | 6519 | |
8d08fdba | 6520 | default: |
5566b478 | 6521 | sorry ("use of `%s' in template", |
8d08fdba MS |
6522 | tree_code_name [(int) TREE_CODE (t)]); |
6523 | return error_mark_node; | |
6524 | } | |
6525 | } | |
6526 | ||
5566b478 MS |
6527 | void |
6528 | do_pushlevel () | |
6529 | { | |
6530 | emit_line_note (input_filename, lineno); | |
6531 | pushlevel (0); | |
6532 | clear_last_expr (); | |
6533 | push_momentary (); | |
6534 | expand_start_bindings (0); | |
6535 | } | |
6536 | ||
8d08fdba | 6537 | tree |
5566b478 | 6538 | do_poplevel () |
8d08fdba | 6539 | { |
5566b478 | 6540 | tree t; |
a703fb38 | 6541 | int saved_warn_unused = 0; |
8d08fdba | 6542 | |
85b71cf2 JM |
6543 | if (processing_template_decl) |
6544 | { | |
6545 | saved_warn_unused = warn_unused; | |
6546 | warn_unused = 0; | |
6547 | } | |
8baa713c | 6548 | expand_end_bindings (getdecls (), kept_level_p (), 0); |
85b71cf2 JM |
6549 | if (processing_template_decl) |
6550 | warn_unused = saved_warn_unused; | |
5566b478 MS |
6551 | t = poplevel (kept_level_p (), 1, 0); |
6552 | pop_momentary (); | |
6553 | return t; | |
6554 | } | |
8d08fdba | 6555 | |
00d3396f JM |
6556 | /* Like tsubst, but deals with expressions. This function just replaces |
6557 | template parms; to finish processing the resultant expression, use | |
6558 | tsubst_expr. */ | |
6559 | ||
5566b478 | 6560 | tree |
4393e105 | 6561 | tsubst_copy (t, args, complain, in_decl) |
98c1c668 | 6562 | tree t, args; |
4393e105 | 6563 | int complain; |
5566b478 MS |
6564 | tree in_decl; |
6565 | { | |
6566 | enum tree_code code; | |
8452b1d3 | 6567 | tree r; |
8d08fdba | 6568 | |
5566b478 MS |
6569 | if (t == NULL_TREE || t == error_mark_node) |
6570 | return t; | |
6571 | ||
6572 | code = TREE_CODE (t); | |
b7484fbe | 6573 | |
5566b478 MS |
6574 | switch (code) |
6575 | { | |
6576 | case PARM_DECL: | |
a759e627 | 6577 | return do_identifier (DECL_NAME (t), 0, NULL_TREE); |
5566b478 MS |
6578 | |
6579 | case CONST_DECL: | |
ed44da02 MM |
6580 | { |
6581 | tree enum_type; | |
6582 | tree v; | |
6583 | ||
6584 | if (!DECL_CONTEXT (t)) | |
6585 | /* This is a global enumeration constant. */ | |
6586 | return t; | |
6587 | ||
6588 | /* Unfortunately, we cannot just call lookup_name here. | |
6589 | Consider: | |
6590 | ||
6591 | template <int I> int f() { | |
6592 | enum E { a = I }; | |
6593 | struct S { void g() { E e = a; } }; | |
6594 | }; | |
6595 | ||
6596 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
6597 | clever enough to find f<7>::a. */ | |
6598 | enum_type | |
4393e105 | 6599 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
ed44da02 MM |
6600 | /*entering_scope=*/0); |
6601 | ||
6602 | for (v = TYPE_VALUES (enum_type); | |
6603 | v != NULL_TREE; | |
6604 | v = TREE_CHAIN (v)) | |
6605 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
6606 | return TREE_VALUE (v); | |
6607 | ||
6608 | /* We didn't find the name. That should never happen; if | |
6609 | name-lookup found it during preliminary parsing, we | |
6610 | should find it again here during instantiation. */ | |
6611 | my_friendly_abort (0); | |
6612 | } | |
db04386f | 6613 | return t; |
ed44da02 | 6614 | |
5566b478 MS |
6615 | case FIELD_DECL: |
6616 | if (DECL_CONTEXT (t)) | |
6617 | { | |
0978790f | 6618 | tree ctx; |
0978790f | 6619 | |
4393e105 | 6620 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 6621 | /*entering_scope=*/1); |
0978790f | 6622 | if (ctx != DECL_CONTEXT (t)) |
5566b478 MS |
6623 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
6624 | } | |
6625 | return t; | |
6626 | ||
6627 | case VAR_DECL: | |
6628 | case FUNCTION_DECL: | |
6629 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
4393e105 | 6630 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
6631 | mark_used (t); |
6632 | return t; | |
6633 | ||
98c1c668 JM |
6634 | case TEMPLATE_DECL: |
6635 | if (is_member_template (t)) | |
4393e105 | 6636 | return tsubst (t, args, complain, in_decl); |
98c1c668 JM |
6637 | else |
6638 | return t; | |
6639 | ||
672476cb MM |
6640 | case LOOKUP_EXPR: |
6641 | { | |
6642 | /* We must tsbust into a LOOKUP_EXPR in case the names to | |
6643 | which it refers is a conversion operator; in that case the | |
6644 | name will change. We avoid making unnecessary copies, | |
6645 | however. */ | |
6646 | ||
4393e105 | 6647 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
672476cb MM |
6648 | |
6649 | if (id != TREE_OPERAND (t, 0)) | |
6650 | { | |
8452b1d3 | 6651 | r = build_nt (LOOKUP_EXPR, id); |
672476cb MM |
6652 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); |
6653 | t = r; | |
6654 | } | |
6655 | ||
6656 | return t; | |
6657 | } | |
6658 | ||
5566b478 MS |
6659 | case CAST_EXPR: |
6660 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
6661 | case CONST_CAST_EXPR: |
6662 | case STATIC_CAST_EXPR: | |
6663 | case DYNAMIC_CAST_EXPR: | |
51924768 | 6664 | case NOP_EXPR: |
5566b478 | 6665 | return build1 |
4393e105 MM |
6666 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6667 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
6668 | |
6669 | case INDIRECT_REF: | |
6670 | case PREDECREMENT_EXPR: | |
6671 | case PREINCREMENT_EXPR: | |
6672 | case POSTDECREMENT_EXPR: | |
6673 | case POSTINCREMENT_EXPR: | |
6674 | case NEGATE_EXPR: | |
6675 | case TRUTH_NOT_EXPR: | |
b87692e5 | 6676 | case BIT_NOT_EXPR: |
5566b478 MS |
6677 | case ADDR_EXPR: |
6678 | case CONVERT_EXPR: /* Unary + */ | |
6679 | case SIZEOF_EXPR: | |
abff8e06 | 6680 | case ALIGNOF_EXPR: |
5566b478 | 6681 | case ARROW_EXPR: |
fc378698 | 6682 | case THROW_EXPR: |
5156628f | 6683 | case TYPEID_EXPR: |
5566b478 | 6684 | return build1 |
6a629cac | 6685 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 6686 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 MS |
6687 | |
6688 | case PLUS_EXPR: | |
6689 | case MINUS_EXPR: | |
6690 | case MULT_EXPR: | |
6691 | case TRUNC_DIV_EXPR: | |
6692 | case CEIL_DIV_EXPR: | |
6693 | case FLOOR_DIV_EXPR: | |
6694 | case ROUND_DIV_EXPR: | |
6695 | case EXACT_DIV_EXPR: | |
6696 | case BIT_AND_EXPR: | |
6697 | case BIT_ANDTC_EXPR: | |
6698 | case BIT_IOR_EXPR: | |
6699 | case BIT_XOR_EXPR: | |
6700 | case TRUNC_MOD_EXPR: | |
6701 | case FLOOR_MOD_EXPR: | |
6702 | case TRUTH_ANDIF_EXPR: | |
6703 | case TRUTH_ORIF_EXPR: | |
6704 | case TRUTH_AND_EXPR: | |
6705 | case TRUTH_OR_EXPR: | |
6706 | case RSHIFT_EXPR: | |
6707 | case LSHIFT_EXPR: | |
6708 | case RROTATE_EXPR: | |
6709 | case LROTATE_EXPR: | |
6710 | case EQ_EXPR: | |
6711 | case NE_EXPR: | |
6712 | case MAX_EXPR: | |
6713 | case MIN_EXPR: | |
6714 | case LE_EXPR: | |
6715 | case GE_EXPR: | |
6716 | case LT_EXPR: | |
6717 | case GT_EXPR: | |
6718 | case COMPONENT_REF: | |
6719 | case ARRAY_REF: | |
6720 | case COMPOUND_EXPR: | |
6721 | case SCOPE_REF: | |
6722 | case DOTSTAR_EXPR: | |
6723 | case MEMBER_REF: | |
6724 | return build_nt | |
4393e105 MM |
6725 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6726 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
6727 | |
6728 | case CALL_EXPR: | |
6729 | { | |
6730 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 6731 | if (is_overloaded_fn (fn)) |
4393e105 | 6732 | fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl); |
5566b478 | 6733 | else |
f84b4be9 | 6734 | /* Sometimes FN is a LOOKUP_EXPR. */ |
4393e105 | 6735 | fn = tsubst_copy (fn, args, complain, in_decl); |
5566b478 | 6736 | return build_nt |
4393e105 MM |
6737 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
6738 | in_decl), | |
5566b478 MS |
6739 | NULL_TREE); |
6740 | } | |
6741 | ||
6742 | case METHOD_CALL_EXPR: | |
6743 | { | |
6744 | tree name = TREE_OPERAND (t, 0); | |
6745 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
6746 | { | |
4393e105 MM |
6747 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
6748 | complain, in_decl); | |
7bae46f4 | 6749 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6750 | } |
6751 | else if (TREE_CODE (name) == SCOPE_REF | |
6752 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
6753 | { | |
4393e105 MM |
6754 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, |
6755 | complain, in_decl); | |
5566b478 | 6756 | name = TREE_OPERAND (name, 1); |
4393e105 MM |
6757 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
6758 | complain, in_decl); | |
11686454 | 6759 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6760 | name = build_nt (SCOPE_REF, base, name); |
6761 | } | |
6762 | else | |
4393e105 | 6763 | name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
5566b478 | 6764 | return build_nt |
4393e105 MM |
6765 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, |
6766 | complain, in_decl), | |
6767 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), | |
5566b478 MS |
6768 | NULL_TREE); |
6769 | } | |
6770 | ||
67da3287 | 6771 | case BIND_EXPR: |
5566b478 MS |
6772 | case COND_EXPR: |
6773 | case MODOP_EXPR: | |
67da3287 | 6774 | { |
8452b1d3 | 6775 | r = build_nt |
4393e105 MM |
6776 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6777 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
6778 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
67da3287 MM |
6779 | |
6780 | if (code == BIND_EXPR && !processing_template_decl) | |
6781 | { | |
61cd552e | 6782 | /* This processing should really occur in tsubst_expr, |
67da3287 MM |
6783 | However, tsubst_expr does not recurse into expressions, |
6784 | since it assumes that there aren't any statements | |
6785 | inside them. Instead, it simply calls | |
6786 | build_expr_from_tree. So, we need to expand the | |
6787 | BIND_EXPR here. */ | |
b4c4a9ec | 6788 | tree rtl_expr = begin_stmt_expr (); |
4393e105 MM |
6789 | tree block = tsubst_expr (TREE_OPERAND (r, 1), args, |
6790 | complain, in_decl); | |
b4c4a9ec | 6791 | r = finish_stmt_expr (rtl_expr, block); |
67da3287 MM |
6792 | } |
6793 | ||
6794 | return r; | |
6795 | } | |
5566b478 MS |
6796 | |
6797 | case NEW_EXPR: | |
6798 | { | |
8452b1d3 | 6799 | r = build_nt |
4393e105 MM |
6800 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6801 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
6802 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
6803 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
6804 | return r; | |
6805 | } | |
6806 | ||
6807 | case DELETE_EXPR: | |
6808 | { | |
8452b1d3 | 6809 | r = build_nt |
4393e105 MM |
6810 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6811 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
6812 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
6813 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
6814 | return r; | |
6815 | } | |
6816 | ||
386b8a85 JM |
6817 | case TEMPLATE_ID_EXPR: |
6818 | { | |
00d3396f | 6819 | /* Substituted template arguments */ |
4393e105 MM |
6820 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
6821 | in_decl); | |
00d3396f JM |
6822 | tree chain; |
6823 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
6824 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
6825 | ||
6826 | return lookup_template_function | |
4393e105 | 6827 | (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); |
386b8a85 JM |
6828 | } |
6829 | ||
5566b478 MS |
6830 | case TREE_LIST: |
6831 | { | |
6832 | tree purpose, value, chain; | |
6833 | ||
6834 | if (t == void_list_node) | |
6835 | return t; | |
6836 | ||
6837 | purpose = TREE_PURPOSE (t); | |
6838 | if (purpose) | |
4393e105 | 6839 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
6840 | value = TREE_VALUE (t); |
6841 | if (value) | |
4393e105 | 6842 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
6843 | chain = TREE_CHAIN (t); |
6844 | if (chain && chain != void_type_node) | |
4393e105 | 6845 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
6846 | if (purpose == TREE_PURPOSE (t) |
6847 | && value == TREE_VALUE (t) | |
6848 | && chain == TREE_CHAIN (t)) | |
6849 | return t; | |
6850 | return tree_cons (purpose, value, chain); | |
6851 | } | |
6852 | ||
6853 | case RECORD_TYPE: | |
6854 | case UNION_TYPE: | |
6855 | case ENUMERAL_TYPE: | |
6856 | case INTEGER_TYPE: | |
6857 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 6858 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 6859 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
6860 | case POINTER_TYPE: |
6861 | case REFERENCE_TYPE: | |
6862 | case OFFSET_TYPE: | |
6863 | case FUNCTION_TYPE: | |
6864 | case METHOD_TYPE: | |
6865 | case ARRAY_TYPE: | |
6866 | case TYPENAME_TYPE: | |
f84b4be9 | 6867 | case TYPE_DECL: |
4393e105 | 6868 | return tsubst (t, args, complain, in_decl); |
5566b478 | 6869 | |
e92cc029 | 6870 | case IDENTIFIER_NODE: |
8e710788 BK |
6871 | if (IDENTIFIER_TYPENAME_P (t) |
6872 | /* Make sure it's not just a variable named `__opr', for instance, | |
6873 | which can occur in some existing code. */ | |
6874 | && TREE_TYPE (t)) | |
e92cc029 | 6875 | return build_typename_overload |
4393e105 | 6876 | (tsubst (TREE_TYPE (t), args, complain, in_decl)); |
e92cc029 MS |
6877 | else |
6878 | return t; | |
6879 | ||
5156628f | 6880 | case CONSTRUCTOR: |
8452b1d3 JM |
6881 | { |
6882 | r = build | |
4393e105 MM |
6883 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6884 | NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, | |
6885 | complain, in_decl)); | |
8452b1d3 JM |
6886 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
6887 | return r; | |
6888 | } | |
5156628f | 6889 | |
5566b478 MS |
6890 | default: |
6891 | return t; | |
6892 | } | |
6893 | } | |
6894 | ||
00d3396f JM |
6895 | /* Like tsubst_copy, but also does semantic processing and RTL expansion. */ |
6896 | ||
5566b478 | 6897 | tree |
4393e105 | 6898 | tsubst_expr (t, args, complain, in_decl) |
98c1c668 | 6899 | tree t, args; |
4393e105 | 6900 | int complain; |
5566b478 MS |
6901 | tree in_decl; |
6902 | { | |
6903 | if (t == NULL_TREE || t == error_mark_node) | |
6904 | return t; | |
6905 | ||
5156628f | 6906 | if (processing_template_decl) |
4393e105 | 6907 | return tsubst_copy (t, args, complain, in_decl); |
5566b478 MS |
6908 | |
6909 | switch (TREE_CODE (t)) | |
8d08fdba | 6910 | { |
5566b478 MS |
6911 | case RETURN_STMT: |
6912 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6913 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
4393e105 | 6914 | args, complain, in_decl)); |
5566b478 MS |
6915 | break; |
6916 | ||
6917 | case EXPR_STMT: | |
6918 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6919 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
4393e105 | 6920 | args, complain, in_decl)); |
5566b478 MS |
6921 | break; |
6922 | ||
6923 | case DECL_STMT: | |
6924 | { | |
6925 | int i = suspend_momentary (); | |
67d743fe | 6926 | tree dcl, init; |
5566b478 MS |
6927 | |
6928 | lineno = TREE_COMPLEXITY (t); | |
6929 | emit_line_note (input_filename, lineno); | |
6930 | dcl = start_decl | |
4393e105 MM |
6931 | (tsubst (TREE_OPERAND (t, 0), args, complain, in_decl), |
6932 | tsubst (TREE_OPERAND (t, 1), args, complain, in_decl), | |
a1774733 | 6933 | TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE); |
4393e105 | 6934 | init = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl); |
5566b478 | 6935 | cp_finish_decl |
a0128b67 | 6936 | (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0); |
5566b478 MS |
6937 | resume_momentary (i); |
6938 | return dcl; | |
6939 | } | |
8d08fdba | 6940 | |
5566b478 MS |
6941 | case FOR_STMT: |
6942 | { | |
6943 | tree tmp; | |
5566b478 | 6944 | lineno = TREE_COMPLEXITY (t); |
5566b478 | 6945 | |
ad321293 MM |
6946 | begin_for_stmt (); |
6947 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) | |
4393e105 | 6948 | tsubst_expr (tmp, args, complain, in_decl); |
ad321293 MM |
6949 | finish_for_init_stmt (NULL_TREE); |
6950 | finish_for_cond (tsubst_expr (FOR_COND (t), args, | |
4393e105 | 6951 | complain, in_decl), |
ad321293 | 6952 | NULL_TREE); |
4393e105 | 6953 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); |
ad321293 | 6954 | finish_for_expr (tmp, NULL_TREE); |
4393e105 | 6955 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); |
ad321293 | 6956 | finish_for_stmt (tmp, NULL_TREE); |
5566b478 MS |
6957 | } |
6958 | break; | |
8d08fdba | 6959 | |
5566b478 MS |
6960 | case WHILE_STMT: |
6961 | { | |
5566b478 | 6962 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6963 | begin_while_stmt (); |
6964 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), | |
4393e105 | 6965 | args, complain, in_decl), |
ad321293 | 6966 | NULL_TREE); |
4393e105 | 6967 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); |
ad321293 | 6968 | finish_while_stmt (NULL_TREE); |
5566b478 MS |
6969 | } |
6970 | break; | |
8d08fdba | 6971 | |
5566b478 MS |
6972 | case DO_STMT: |
6973 | { | |
5566b478 | 6974 | lineno = TREE_COMPLEXITY (t); |
ad321293 | 6975 | begin_do_stmt (); |
4393e105 | 6976 | tsubst_expr (DO_BODY (t), args, complain, in_decl); |
ad321293 MM |
6977 | finish_do_body (NULL_TREE); |
6978 | finish_do_stmt (tsubst_expr (DO_COND (t), args, | |
4393e105 | 6979 | complain, in_decl), |
ad321293 | 6980 | NULL_TREE); |
5566b478 MS |
6981 | } |
6982 | break; | |
a0a33927 | 6983 | |
5566b478 | 6984 | case IF_STMT: |
8d08fdba | 6985 | { |
5566b478 | 6986 | tree tmp; |
5566b478 MS |
6987 | |
6988 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6989 | begin_if_stmt (); |
6990 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), | |
4393e105 | 6991 | args, complain, in_decl), |
ad321293 | 6992 | NULL_TREE); |
db5ae43f | 6993 | |
ad321293 | 6994 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 6995 | { |
4393e105 | 6996 | tsubst_expr (tmp, args, complain, in_decl); |
ad321293 | 6997 | finish_then_clause (NULL_TREE); |
db5ae43f MS |
6998 | } |
6999 | ||
ad321293 MM |
7000 | if (tmp = ELSE_CLAUSE (t), tmp) |
7001 | { | |
7002 | begin_else_clause (); | |
4393e105 | 7003 | tsubst_expr (tmp, args, complain, in_decl); |
ad321293 MM |
7004 | finish_else_clause (NULL_TREE); |
7005 | } | |
8d08fdba | 7006 | |
ad321293 | 7007 | finish_if_stmt (); |
8d08fdba | 7008 | } |
5566b478 | 7009 | break; |
8d08fdba | 7010 | |
5566b478 MS |
7011 | case COMPOUND_STMT: |
7012 | { | |
ad321293 | 7013 | tree substmt; |
8d08fdba | 7014 | |
5566b478 | 7015 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
7016 | begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
7017 | for (substmt = COMPOUND_BODY (t); | |
7018 | substmt != NULL_TREE; | |
7019 | substmt = TREE_CHAIN (substmt)) | |
4393e105 | 7020 | tsubst_expr (substmt, args, complain, in_decl); |
ad321293 MM |
7021 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), |
7022 | NULL_TREE); | |
5566b478 MS |
7023 | } |
7024 | break; | |
8d08fdba | 7025 | |
5566b478 MS |
7026 | case BREAK_STMT: |
7027 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 7028 | finish_break_stmt (); |
5566b478 | 7029 | break; |
8d08fdba | 7030 | |
6467930b MS |
7031 | case CONTINUE_STMT: |
7032 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 7033 | finish_continue_stmt (); |
6467930b MS |
7034 | break; |
7035 | ||
5566b478 MS |
7036 | case SWITCH_STMT: |
7037 | { | |
7038 | tree val, tmp; | |
5566b478 MS |
7039 | |
7040 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 7041 | begin_switch_stmt (); |
4393e105 | 7042 | val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); |
ad321293 | 7043 | finish_switch_cond (val); |
5566b478 MS |
7044 | |
7045 | if (tmp = TREE_OPERAND (t, 1), tmp) | |
4393e105 | 7046 | tsubst_expr (tmp, args, complain, in_decl); |
8d08fdba | 7047 | |
ad321293 | 7048 | finish_switch_stmt (val, NULL_TREE); |
5566b478 MS |
7049 | } |
7050 | break; | |
7051 | ||
7052 | case CASE_LABEL: | |
4393e105 MM |
7053 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), |
7054 | tsubst_expr (CASE_HIGH (t), args, complain, in_decl)); | |
5566b478 MS |
7055 | break; |
7056 | ||
7057 | case LABEL_DECL: | |
7058 | t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t), | |
7059 | DECL_NAME (t)); | |
7060 | if (t) | |
7061 | expand_label (t); | |
7062 | break; | |
7063 | ||
7064 | case GOTO_STMT: | |
7065 | lineno = TREE_COMPLEXITY (t); | |
aa09da44 MM |
7066 | t = GOTO_DESTINATION (t); |
7067 | if (TREE_CODE (t) != IDENTIFIER_NODE) | |
7068 | /* Computed goto's must be tsubst'd into. On the other hand, | |
7069 | non-computed gotos must not be; the identifier in question | |
7070 | will have no binding. */ | |
4393e105 | 7071 | t = tsubst_expr (t, args, complain, in_decl); |
aa09da44 | 7072 | finish_goto_stmt (t); |
ad321293 MM |
7073 | break; |
7074 | ||
7075 | case ASM_STMT: | |
7076 | lineno = TREE_COMPLEXITY (t); | |
4393e105 MM |
7077 | finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, complain, in_decl), |
7078 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), | |
7079 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
7080 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
7081 | tsubst_expr (ASM_CLOBBERS (t), args, complain, | |
7082 | in_decl)); | |
5566b478 | 7083 | break; |
faf5394a MS |
7084 | |
7085 | case TRY_BLOCK: | |
7086 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 7087 | begin_try_block (); |
4393e105 | 7088 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); |
ad321293 | 7089 | finish_try_block (NULL_TREE); |
faf5394a | 7090 | { |
ad321293 | 7091 | tree handler = TRY_HANDLERS (t); |
faf5394a | 7092 | for (; handler; handler = TREE_CHAIN (handler)) |
4393e105 | 7093 | tsubst_expr (handler, args, complain, in_decl); |
faf5394a | 7094 | } |
ad321293 | 7095 | finish_handler_sequence (NULL_TREE); |
faf5394a MS |
7096 | break; |
7097 | ||
7098 | case HANDLER: | |
7099 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
7100 | begin_handler (); |
7101 | if (HANDLER_PARMS (t)) | |
faf5394a | 7102 | { |
ad321293 | 7103 | tree d = HANDLER_PARMS (t); |
faf5394a | 7104 | expand_start_catch_block |
4393e105 MM |
7105 | (tsubst (TREE_OPERAND (d, 1), args, complain, in_decl), |
7106 | tsubst (TREE_OPERAND (d, 0), args, complain, in_decl)); | |
faf5394a MS |
7107 | } |
7108 | else | |
7109 | expand_start_catch_block (NULL_TREE, NULL_TREE); | |
ad321293 | 7110 | finish_handler_parms (NULL_TREE); |
4393e105 | 7111 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); |
ad321293 | 7112 | finish_handler (NULL_TREE); |
faf5394a MS |
7113 | break; |
7114 | ||
b87692e5 MS |
7115 | case TAG_DEFN: |
7116 | lineno = TREE_COMPLEXITY (t); | |
7117 | t = TREE_TYPE (t); | |
7118 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
4393e105 | 7119 | tsubst (t, args, complain, NULL_TREE); |
b87692e5 MS |
7120 | break; |
7121 | ||
5566b478 | 7122 | default: |
4393e105 | 7123 | return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl)); |
5566b478 MS |
7124 | } |
7125 | return NULL_TREE; | |
8d08fdba MS |
7126 | } |
7127 | ||
6ba89f8e | 7128 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
7129 | the template arguments in TARG_PTR. */ |
7130 | ||
5566b478 MS |
7131 | tree |
7132 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 7133 | tree tmpl, targ_ptr; |
8d08fdba | 7134 | { |
5566b478 | 7135 | tree fndecl; |
36a117a5 MM |
7136 | tree gen_tmpl; |
7137 | tree spec; | |
5566b478 MS |
7138 | int i, len; |
7139 | struct obstack *old_fmp_obstack; | |
7140 | extern struct obstack *function_maybepermanent_obstack; | |
36a117a5 | 7141 | tree inner_args; |
5566b478 | 7142 | |
27fafc8d JM |
7143 | if (tmpl == error_mark_node) |
7144 | return error_mark_node; | |
7145 | ||
386b8a85 JM |
7146 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
7147 | ||
36a117a5 MM |
7148 | /* Check to see if we already have this specialization. */ |
7149 | spec = retrieve_specialization (tmpl, targ_ptr); | |
7150 | if (spec != NULL_TREE) | |
7151 | return spec; | |
7152 | ||
7153 | if (DECL_TEMPLATE_INFO (tmpl)) | |
386b8a85 | 7154 | { |
36a117a5 MM |
7155 | /* The TMPL is a partial instantiation. To get a full set of |
7156 | arguments we must add the arguments used to perform the | |
7157 | partial instantiation. */ | |
7158 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
7159 | targ_ptr); | |
7160 | gen_tmpl = most_general_template (tmpl); | |
7161 | ||
7162 | /* Check to see if we already have this specialization. */ | |
7163 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
75650646 MM |
7164 | if (spec != NULL_TREE) |
7165 | return spec; | |
386b8a85 | 7166 | } |
36a117a5 MM |
7167 | else |
7168 | gen_tmpl = tmpl; | |
386b8a85 | 7169 | |
5566b478 MS |
7170 | push_obstacks (&permanent_obstack, &permanent_obstack); |
7171 | old_fmp_obstack = function_maybepermanent_obstack; | |
7172 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 7173 | |
36a117a5 MM |
7174 | len = DECL_NTPARMS (gen_tmpl); |
7175 | inner_args = innermost_args (targ_ptr); | |
5566b478 MS |
7176 | i = len; |
7177 | while (i--) | |
8d08fdba | 7178 | { |
36a117a5 | 7179 | tree t = TREE_VEC_ELT (inner_args, i); |
5566b478 MS |
7180 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
7181 | { | |
7182 | tree nt = target_type (t); | |
ec255269 | 7183 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 | 7184 | { |
8251199e JM |
7185 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); |
7186 | cp_error (" trying to instantiate `%D'", gen_tmpl); | |
5566b478 MS |
7187 | fndecl = error_mark_node; |
7188 | goto out; | |
7189 | } | |
7190 | } | |
8d08fdba | 7191 | } |
e66d884e | 7192 | targ_ptr = copy_to_permanent (targ_ptr); |
8d08fdba | 7193 | |
5566b478 | 7194 | /* substitute template parameters */ |
4393e105 | 7195 | fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, /*complain=*/1, gen_tmpl); |
36a117a5 MM |
7196 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
7197 | template, not the most general template. */ | |
7198 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 7199 | |
824b9a4c MS |
7200 | if (flag_external_templates) |
7201 | add_pending_template (fndecl); | |
7202 | ||
5566b478 MS |
7203 | out: |
7204 | function_maybepermanent_obstack = old_fmp_obstack; | |
7205 | pop_obstacks (); | |
8d08fdba | 7206 | |
5566b478 | 7207 | return fndecl; |
8d08fdba | 7208 | } |
5566b478 MS |
7209 | |
7210 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
7211 | |
7212 | void | |
5566b478 MS |
7213 | overload_template_name (type) |
7214 | tree type; | |
8d08fdba | 7215 | { |
5566b478 MS |
7216 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
7217 | tree decl; | |
8d08fdba | 7218 | |
5566b478 MS |
7219 | if (IDENTIFIER_CLASS_VALUE (id) |
7220 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
7221 | return; | |
8d08fdba | 7222 | |
5566b478 MS |
7223 | decl = build_decl (TYPE_DECL, id, type); |
7224 | SET_DECL_ARTIFICIAL (decl); | |
7225 | pushdecl_class_level (decl); | |
8d08fdba MS |
7226 | } |
7227 | ||
4393e105 MM |
7228 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
7229 | arguments that are being used when calling it. TARGS is a vector | |
7230 | into which the deduced template arguments are placed. | |
8d08fdba MS |
7231 | |
7232 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
7233 | all the types, and 1 for complete failure. An error message will be | |
7234 | printed only for an incomplete match. | |
7235 | ||
4393e105 MM |
7236 | If FN is a conversion operator, RETURN_TYPE is the type desired as |
7237 | the result of the conversion operator. | |
8d08fdba | 7238 | |
4393e105 | 7239 | TPARMS is a vector of template parameters. |
9f54c803 MM |
7240 | |
7241 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
7242 | template-id. | |
6467930b | 7243 | |
830bfa74 MM |
7244 | The parameter STRICT is one of: |
7245 | ||
7246 | DEDUCE_CALL: | |
7247 | We are deducing arguments for a function call, as in | |
7248 | [temp.deduct.call]. | |
7249 | ||
7250 | DEDUCE_CONV: | |
7251 | We are deducing arguments for a conversion function, as in | |
7252 | [temp.deduct.conv]. | |
7253 | ||
7254 | DEDUCE_EXACT: | |
7255 | We are deducing arguments when calculating the partial | |
7256 | ordering between specializations of function or class | |
7257 | templates, as in [temp.func.order] and [temp.class.order], | |
7258 | when doing an explicit instantiation as in [temp.explicit], | |
9f54c803 | 7259 | when determining an explicit specialization as in |
830bfa74 | 7260 | [temp.expl.spec], or when taking the address of a function |
4393e105 MM |
7261 | template, as in [temp.deduct.funcaddr]. |
7262 | ||
4393e105 | 7263 | The other arguments are as for type_unification. */ |
8d08fdba MS |
7264 | |
7265 | int | |
4393e105 | 7266 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
03017874 | 7267 | strict) |
4393e105 | 7268 | tree fn, explicit_targs, targs, args, return_type; |
830bfa74 | 7269 | unification_kind_t strict; |
386b8a85 | 7270 | { |
4393e105 MM |
7271 | tree parms; |
7272 | tree fntype; | |
386b8a85 | 7273 | |
4393e105 MM |
7274 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); |
7275 | ||
7276 | fntype = TREE_TYPE (fn); | |
7277 | if (explicit_targs) | |
75650646 | 7278 | { |
4393e105 MM |
7279 | /* [temp.deduct] |
7280 | ||
7281 | The specified template arguments must match the template | |
7282 | parameters in kind (i.e., type, nontype, template), and there | |
7283 | must not be more arguments than there are parameters; | |
7284 | otherwise type deduction fails. | |
7285 | ||
7286 | Nontype arguments must match the types of the corresponding | |
7287 | nontype template parameters, or must be convertible to the | |
7288 | types of the corresponding nontype parameters as specified in | |
7289 | _temp.arg.nontype_, otherwise type deduction fails. | |
7290 | ||
7291 | All references in the function type of the function template | |
7292 | to the corresponding template parameters are replaced by the | |
7293 | specified template argument values. If a substitution in a | |
7294 | template parameter or in the function type of the function | |
7295 | template results in an invalid type, type deduction fails. */ | |
7296 | int i; | |
7297 | tree converted_args; | |
75650646 | 7298 | |
4393e105 MM |
7299 | converted_args |
7300 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
7301 | explicit_targs, NULL_TREE, /*complain=*/0, | |
7302 | /*require_all_arguments=*/0)); | |
7303 | if (converted_args == error_mark_node) | |
75650646 | 7304 | return 1; |
386b8a85 | 7305 | |
4393e105 MM |
7306 | fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE); |
7307 | if (fntype == error_mark_node) | |
7308 | return 1; | |
050367a3 | 7309 | |
4393e105 MM |
7310 | /* Place the explicitly specified arguments in TARGS. */ |
7311 | for (i = 0; i < TREE_VEC_LENGTH (targs); i++) | |
7312 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); | |
75650646 | 7313 | } |
4393e105 MM |
7314 | |
7315 | parms = TYPE_ARG_TYPES (fntype); | |
386b8a85 | 7316 | |
4393e105 MM |
7317 | if (DECL_CONV_FN_P (fn)) |
7318 | { | |
7319 | /* This is a template conversion operator. Use the return types | |
7320 | as well as the argument types. */ | |
7321 | parms = scratch_tree_cons (NULL_TREE, TREE_TYPE (fntype), | |
7322 | parms); | |
7323 | args = scratch_tree_cons (NULL_TREE, return_type, args); | |
7324 | } | |
7325 | ||
4393e105 MM |
7326 | /* We allow incomplete unification without an error message here |
7327 | because the standard doesn't seem to explicitly prohibit it. Our | |
7328 | callers must be ready to deal with unification failures in any | |
7329 | event. */ | |
7330 | return type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
7331 | targs, parms, args, /*subr=*/0, | |
7332 | strict, /*allow_incomplete*/1); | |
830bfa74 MM |
7333 | } |
7334 | ||
7335 | /* Adjust types before performing type deduction, as described in | |
7336 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
7337 | sections are symmetric. PARM is the type of a function parameter |
7338 | or the return type of the conversion function. ARG is the type of | |
7339 | the argument passed to the call, or the type of the value | |
7340 | intialized with the result of the conversion function. */ | |
386b8a85 | 7341 | |
e9659ab0 | 7342 | static void |
830bfa74 MM |
7343 | maybe_adjust_types_for_deduction (strict, parm, arg) |
7344 | unification_kind_t strict; | |
7345 | tree* parm; | |
7346 | tree* arg; | |
7347 | { | |
7348 | switch (strict) | |
7349 | { | |
7350 | case DEDUCE_CALL: | |
7351 | break; | |
7352 | ||
7353 | case DEDUCE_CONV: | |
7354 | { | |
4c7d0dff MM |
7355 | /* Swap PARM and ARG throughout the remainder of this |
7356 | function; the handling is precisely symmetric since PARM | |
7357 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
7358 | tree* temp = parm; |
7359 | parm = arg; | |
7360 | arg = temp; | |
7361 | break; | |
7362 | } | |
7363 | ||
7364 | case DEDUCE_EXACT: | |
7365 | /* There is nothing to do in this case. */ | |
7366 | return; | |
7367 | ||
7368 | default: | |
7369 | my_friendly_abort (0); | |
7370 | } | |
7371 | ||
7372 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
7373 | { | |
7374 | /* [temp.deduct.call] | |
7375 | ||
7376 | If P is not a reference type: | |
7377 | ||
7378 | --If A is an array type, the pointer type produced by the | |
7379 | array-to-pointer standard conversion (_conv.array_) is | |
7380 | used in place of A for type deduction; otherwise, | |
7381 | ||
7382 | --If A is a function type, the pointer type produced by | |
7383 | the function-to-pointer standard conversion | |
7384 | (_conv.func_) is used in place of A for type deduction; | |
7385 | otherwise, | |
7386 | ||
7387 | --If A is a cv-qualified type, the top level | |
7388 | cv-qualifiers of A's type are ignored for type | |
7389 | deduction. */ | |
7390 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
7391 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 7392 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
7393 | *arg = build_pointer_type (*arg); |
7394 | else | |
7395 | *arg = TYPE_MAIN_VARIANT (*arg); | |
7396 | } | |
7397 | ||
7398 | /* [temp.deduct.call] | |
7399 | ||
7400 | If P is a cv-qualified type, the top level cv-qualifiers | |
7401 | of P's type are ignored for type deduction. If P is a | |
7402 | reference type, the type referred to by P is used for | |
7403 | type deduction. */ | |
7404 | *parm = TYPE_MAIN_VARIANT (*parm); | |
7405 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
7406 | *parm = TREE_TYPE (*parm); | |
386b8a85 JM |
7407 | } |
7408 | ||
4393e105 | 7409 | /* Like type_unfication. |
9f54c803 MM |
7410 | |
7411 | If SUBR is 1, we're being called recursively (to unify the | |
7412 | arguments of a function or method parameter of a function | |
7413 | template). */ | |
386b8a85 | 7414 | |
4966381a | 7415 | static int |
3b3ba9f0 | 7416 | type_unification_real (tparms, targs, parms, args, subr, |
4393e105 | 7417 | strict, allow_incomplete) |
050367a3 | 7418 | tree tparms, targs, parms, args; |
830bfa74 MM |
7419 | int subr; |
7420 | unification_kind_t strict; | |
7421 | int allow_incomplete; | |
8d08fdba MS |
7422 | { |
7423 | tree parm, arg; | |
7424 | int i; | |
7425 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 7426 | int sub_strict; |
8d08fdba MS |
7427 | |
7428 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
7429 | my_friendly_assert (parms == NULL_TREE |
7430 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 7431 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
7432 | build_x_function_call). */ |
7433 | if (args) | |
7434 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
7435 | my_friendly_assert (ntparms > 0, 292); | |
7436 | ||
830bfa74 MM |
7437 | switch (strict) |
7438 | { | |
7439 | case DEDUCE_CALL: | |
7440 | sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED; | |
7441 | break; | |
7442 | ||
7443 | case DEDUCE_CONV: | |
7444 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
7445 | break; | |
7446 | ||
7447 | case DEDUCE_EXACT: | |
7448 | sub_strict = UNIFY_ALLOW_NONE; | |
7449 | break; | |
7450 | ||
7451 | default: | |
7452 | my_friendly_abort (0); | |
7453 | } | |
7454 | ||
8d08fdba MS |
7455 | while (parms |
7456 | && parms != void_list_node | |
7457 | && args | |
7458 | && args != void_list_node) | |
7459 | { | |
7460 | parm = TREE_VALUE (parms); | |
7461 | parms = TREE_CHAIN (parms); | |
7462 | arg = TREE_VALUE (args); | |
7463 | args = TREE_CHAIN (args); | |
7464 | ||
7465 | if (arg == error_mark_node) | |
7466 | return 1; | |
7467 | if (arg == unknown_type_node) | |
34016c81 JM |
7468 | /* We can't deduce anything from this, but we might get all the |
7469 | template args from other function args. */ | |
7470 | continue; | |
b7484fbe | 7471 | |
03e70705 JM |
7472 | /* Conversions will be performed on a function argument that |
7473 | corresponds with a function parameter that contains only | |
7474 | non-deducible template parameters and explicitly specified | |
7475 | template parameters. */ | |
7476 | if (! uses_template_parms (parm)) | |
b7484fbe | 7477 | { |
03e70705 JM |
7478 | tree type; |
7479 | ||
7480 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
7481 | type = TREE_TYPE (arg); | |
7482 | else | |
7483 | { | |
7484 | type = arg; | |
7485 | arg = NULL_TREE; | |
7486 | } | |
7487 | ||
830bfa74 | 7488 | if (strict == DEDUCE_EXACT) |
03e70705 | 7489 | { |
3bfdc719 | 7490 | if (same_type_p (parm, type)) |
03e70705 JM |
7491 | continue; |
7492 | } | |
03e70705 | 7493 | else |
343c89cd JM |
7494 | /* It might work; we shouldn't check now, because we might |
7495 | get into infinite recursion. Overload resolution will | |
7496 | handle it. */ | |
7497 | continue; | |
03e70705 | 7498 | |
b7484fbe MS |
7499 | return 1; |
7500 | } | |
7501 | ||
8d08fdba MS |
7502 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') |
7503 | { | |
7504 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
34016c81 | 7505 | if (type_unknown_p (arg)) |
28cbf42c | 7506 | { |
34016c81 JM |
7507 | /* [temp.deduct.type] A template-argument can be deduced from |
7508 | a pointer to function or pointer to member function | |
7509 | argument if the set of overloaded functions does not | |
7510 | contain function templates and at most one of a set of | |
7511 | overloaded functions provides a unique match. */ | |
7512 | ||
7513 | if (resolve_overloaded_unification | |
4393e105 | 7514 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
7515 | != 0) |
7516 | return 1; | |
7517 | continue; | |
28cbf42c | 7518 | } |
8d08fdba MS |
7519 | arg = TREE_TYPE (arg); |
7520 | } | |
4393e105 | 7521 | |
830bfa74 MM |
7522 | if (!subr) |
7523 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
db5ae43f | 7524 | |
4393e105 | 7525 | switch (unify (tparms, targs, parm, arg, sub_strict)) |
8d08fdba MS |
7526 | { |
7527 | case 0: | |
7528 | break; | |
7529 | case 1: | |
7530 | return 1; | |
7531 | } | |
7532 | } | |
7533 | /* Fail if we've reached the end of the parm list, and more args | |
7534 | are present, and the parm list isn't variadic. */ | |
7535 | if (args && args != void_list_node && parms == void_list_node) | |
7536 | return 1; | |
7537 | /* Fail if parms are left and they don't have default values. */ | |
7538 | if (parms | |
7539 | && parms != void_list_node | |
7540 | && TREE_PURPOSE (parms) == NULL_TREE) | |
7541 | return 1; | |
7542 | if (!subr) | |
7543 | for (i = 0; i < ntparms; i++) | |
050367a3 | 7544 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 7545 | { |
386b8a85 | 7546 | if (!allow_incomplete) |
8251199e | 7547 | error ("incomplete type unification"); |
8d08fdba MS |
7548 | return 2; |
7549 | } | |
7550 | return 0; | |
7551 | } | |
7552 | ||
34016c81 JM |
7553 | /* Subroutine of type_unification_real. Args are like the variables at the |
7554 | call site. ARG is an overloaded function (or template-id); we try | |
7555 | deducing template args from each of the overloads, and if only one | |
7556 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
7557 | ||
7558 | static int | |
7559 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
4393e105 | 7560 | sub_strict) |
34016c81 JM |
7561 | tree tparms, targs, parm, arg; |
7562 | unification_kind_t strict; | |
7563 | int sub_strict; | |
34016c81 JM |
7564 | { |
7565 | tree tempargs = copy_node (targs); | |
7566 | int good = 0; | |
7567 | ||
7568 | if (TREE_CODE (arg) == ADDR_EXPR) | |
7569 | arg = TREE_OPERAND (arg, 0); | |
9f3d9e46 | 7570 | |
d8f8dca1 MM |
7571 | if (TREE_CODE (arg) == COMPONENT_REF) |
7572 | /* Handle `&x' where `x' is some static or non-static member | |
7573 | function name. */ | |
7574 | arg = TREE_OPERAND (arg, 1); | |
7575 | ||
9f3d9e46 JM |
7576 | /* Strip baselink information. */ |
7577 | while (TREE_CODE (arg) == TREE_LIST) | |
7578 | arg = TREE_VALUE (arg); | |
7579 | ||
34016c81 JM |
7580 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
7581 | { | |
7582 | /* If we got some explicit template args, we need to plug them into | |
7583 | the affected templates before we try to unify, in case the | |
7584 | explicit args will completely resolve the templates in question. */ | |
7585 | ||
7586 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
7587 | arg = TREE_OPERAND (arg, 0); | |
7588 | ||
7589 | for (; arg; arg = OVL_NEXT (arg)) | |
7590 | { | |
7591 | tree fn = OVL_CURRENT (arg); | |
7592 | tree subargs, elem; | |
7593 | ||
7594 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
7595 | continue; | |
7596 | ||
7597 | subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs); | |
7598 | if (subargs) | |
7599 | { | |
4393e105 MM |
7600 | elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0, |
7601 | NULL_TREE); | |
d8f8dca1 MM |
7602 | if (TREE_CODE (elem) == METHOD_TYPE) |
7603 | elem = build_ptrmemfunc_type (build_pointer_type (elem)); | |
e97e5263 | 7604 | good += try_one_overload (tparms, targs, tempargs, parm, elem, |
4393e105 | 7605 | strict, sub_strict); |
34016c81 JM |
7606 | } |
7607 | } | |
7608 | } | |
7609 | else if (TREE_CODE (arg) == OVERLOAD) | |
7610 | { | |
7611 | for (; arg; arg = OVL_NEXT (arg)) | |
d8f8dca1 MM |
7612 | { |
7613 | tree type = TREE_TYPE (OVL_CURRENT (arg)); | |
7614 | if (TREE_CODE (type) == METHOD_TYPE) | |
7615 | type = build_ptrmemfunc_type (build_pointer_type (type)); | |
7616 | good += try_one_overload (tparms, targs, tempargs, parm, | |
7617 | type, | |
4393e105 | 7618 | strict, sub_strict); |
d8f8dca1 | 7619 | } |
34016c81 JM |
7620 | } |
7621 | else | |
7622 | my_friendly_abort (981006); | |
7623 | ||
7624 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7625 | to function or pointer to member function argument if the set of | |
7626 | overloaded functions does not contain function templates and at most | |
7627 | one of a set of overloaded functions provides a unique match. | |
7628 | ||
7629 | So if we found multiple possibilities, we return success but don't | |
7630 | deduce anything. */ | |
7631 | ||
7632 | if (good == 1) | |
7633 | { | |
7634 | int i = TREE_VEC_LENGTH (targs); | |
7635 | for (; i--; ) | |
7636 | if (TREE_VEC_ELT (tempargs, i)) | |
7637 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
7638 | } | |
7639 | if (good) | |
7640 | return 0; | |
7641 | ||
7642 | return 1; | |
7643 | } | |
7644 | ||
7645 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
7646 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
7647 | different overloads deduce different arguments for a given parm. | |
7648 | Returns 1 on success. */ | |
7649 | ||
7650 | static int | |
e97e5263 | 7651 | try_one_overload (tparms, orig_targs, targs, parm, arg, strict, |
4393e105 | 7652 | sub_strict) |
e97e5263 | 7653 | tree tparms, orig_targs, targs, parm, arg; |
34016c81 JM |
7654 | unification_kind_t strict; |
7655 | int sub_strict; | |
34016c81 JM |
7656 | { |
7657 | int nargs; | |
7658 | tree tempargs; | |
7659 | int i; | |
7660 | ||
7661 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7662 | to function or pointer to member function argument if the set of | |
7663 | overloaded functions does not contain function templates and at most | |
7664 | one of a set of overloaded functions provides a unique match. | |
7665 | ||
7666 | So if this is a template, just return success. */ | |
7667 | ||
7668 | if (uses_template_parms (arg)) | |
7669 | return 1; | |
7670 | ||
7671 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
7672 | ||
7673 | /* We don't copy orig_targs for this because if we have already deduced | |
7674 | some template args from previous args, unify would complain when we | |
7675 | try to deduce a template parameter for the same argument, even though | |
7676 | there isn't really a conflict. */ | |
7677 | nargs = TREE_VEC_LENGTH (targs); | |
7678 | tempargs = make_scratch_vec (nargs); | |
7679 | ||
4393e105 | 7680 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
7681 | return 0; |
7682 | ||
7683 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 7684 | explicitly specified args. */ |
34016c81 JM |
7685 | for (i = nargs; i--; ) |
7686 | { | |
7687 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 7688 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 JM |
7689 | |
7690 | if (elt == NULL_TREE) | |
7691 | continue; | |
7692 | else if (uses_template_parms (elt)) | |
7693 | { | |
7694 | /* Since we're unifying against ourselves, we will fill in template | |
7695 | args used in the function parm list with our own template parms. | |
7696 | Discard them. */ | |
7697 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
7698 | continue; | |
7699 | } | |
7700 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
7701 | return 0; | |
7702 | } | |
7703 | ||
7704 | for (i = nargs; i--; ) | |
7705 | { | |
7706 | tree elt = TREE_VEC_ELT (tempargs, i); | |
7707 | ||
7708 | if (elt) | |
7709 | TREE_VEC_ELT (targs, i) = elt; | |
7710 | } | |
7711 | ||
7712 | return 1; | |
7713 | } | |
7714 | ||
4393e105 MM |
7715 | /* PARM is a template class (perhaps with unbound template |
7716 | parameters). ARG is a fully instantiated type. If ARG can be | |
7717 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
7718 | TARGS are as for unify. */ | |
fcfb9f96 MM |
7719 | |
7720 | static tree | |
4393e105 | 7721 | try_class_unification (tparms, targs, parm, arg) |
fcfb9f96 MM |
7722 | tree tparms; |
7723 | tree targs; | |
4393e105 MM |
7724 | tree parm; |
7725 | tree arg; | |
7726 | { | |
7727 | int i; | |
7728 | tree copy_of_targs; | |
7729 | ||
7730 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
7731 | || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) | |
7732 | return NULL_TREE; | |
7733 | ||
7734 | /* We need to make a new template argument vector for the call to | |
7735 | unify. If we used TARGS, we'd clutter it up with the result of | |
7736 | the attempted unification, even if this class didn't work out. | |
7737 | We also don't want to commit ourselves to all the unifications | |
7738 | we've already done, since unification is supposed to be done on | |
7739 | an argument-by-argument basis. In other words, consider the | |
7740 | following pathological case: | |
7741 | ||
7742 | template <int I, int J, int K> | |
7743 | struct S {}; | |
7744 | ||
7745 | template <int I, int J> | |
7746 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
7747 | ||
7748 | template <int I, int J, int K> | |
7749 | void f(S<I, J, K>, S<I, I, I>); | |
7750 | ||
7751 | void g() { | |
7752 | S<0, 0, 0> s0; | |
7753 | S<0, 1, 2> s2; | |
7754 | ||
7755 | f(s0, s2); | |
7756 | } | |
7757 | ||
7758 | Now, by the time we consider the unification involving `s2', we | |
7759 | already know that we must have `f<0, 0, 0>'. But, even though | |
7760 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal | |
7761 | because there are two ways to unify base classes of S<0, 1, 2> | |
7762 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
7763 | would reject the possibility I=1. */ | |
7764 | push_momentary (); | |
7765 | copy_of_targs = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
7766 | i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), | |
7767 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE); | |
7768 | pop_momentary (); | |
7769 | ||
7770 | /* If unification failed, we're done. */ | |
7771 | if (i != 0) | |
7772 | return NULL_TREE; | |
7773 | else | |
7774 | return arg; | |
7775 | } | |
7776 | ||
7777 | /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we | |
7778 | have alreay discovered to be satisfactory. ARG_BINFO is the binfo | |
7779 | for the base class of ARG that we are currently examining. */ | |
7780 | ||
7781 | static tree | |
7782 | get_template_base_recursive (tparms, targs, parm, | |
7783 | arg_binfo, rval, flags) | |
7784 | tree tparms; | |
7785 | tree targs; | |
7786 | tree arg_binfo; | |
fcfb9f96 | 7787 | tree rval; |
4393e105 MM |
7788 | tree parm; |
7789 | int flags; | |
fcfb9f96 MM |
7790 | { |
7791 | tree binfos; | |
7792 | int i, n_baselinks; | |
4393e105 | 7793 | tree arg = BINFO_TYPE (arg_binfo); |
fcfb9f96 | 7794 | |
4393e105 | 7795 | if (!(flags & GTB_IGNORE_TYPE)) |
fcfb9f96 | 7796 | { |
4393e105 MM |
7797 | tree r = try_class_unification (tparms, targs, |
7798 | parm, arg); | |
fcfb9f96 | 7799 | |
4393e105 | 7800 | /* If there is more than one satisfactory baseclass, then: |
fcfb9f96 | 7801 | |
4393e105 MM |
7802 | [temp.deduct.call] |
7803 | ||
7804 | If they yield more than one possible deduced A, the type | |
7805 | deduction fails. | |
7806 | ||
7807 | applies. */ | |
7808 | if (r && rval && !same_type_p (r, rval)) | |
7809 | return error_mark_node; | |
7810 | else if (r) | |
7811 | rval = r; | |
fcfb9f96 MM |
7812 | } |
7813 | ||
4393e105 | 7814 | binfos = BINFO_BASETYPES (arg_binfo); |
fcfb9f96 MM |
7815 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; |
7816 | ||
7817 | /* Process base types. */ | |
7818 | for (i = 0; i < n_baselinks; i++) | |
7819 | { | |
7820 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
4393e105 | 7821 | int this_virtual; |
fcfb9f96 | 7822 | |
4393e105 MM |
7823 | /* Skip this base, if we've already seen it. */ |
7824 | if (BINFO_MARKED (base_binfo)) | |
7825 | continue; | |
7826 | ||
7827 | this_virtual = | |
7828 | (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); | |
7829 | ||
7830 | /* When searching for a non-virtual, we cannot mark virtually | |
7831 | found binfos. */ | |
7832 | if (! this_virtual) | |
7833 | SET_BINFO_MARKED (base_binfo); | |
7834 | ||
7835 | rval = get_template_base_recursive (tparms, targs, | |
7836 | parm, | |
7837 | base_binfo, | |
7838 | rval, | |
7839 | GTB_VIA_VIRTUAL * this_virtual); | |
7840 | ||
7841 | /* If we discovered more than one matching base class, we can | |
7842 | stop now. */ | |
7843 | if (rval == error_mark_node) | |
7844 | return error_mark_node; | |
fcfb9f96 MM |
7845 | } |
7846 | ||
7847 | return rval; | |
7848 | } | |
7849 | ||
4393e105 MM |
7850 | /* Given a template type PARM and a class type ARG, find the unique |
7851 | base type in ARG that is an instance of PARM. We do not examine | |
7852 | ARG itself; only its base-classes. If there is no appropriate base | |
7853 | class, return NULL_TREE. If there is more than one, return | |
7854 | error_mark_node. PARM may be the type of a partial specialization, | |
7855 | as well as a plain template type. Used by unify. */ | |
fcfb9f96 MM |
7856 | |
7857 | static tree | |
4393e105 | 7858 | get_template_base (tparms, targs, parm, arg) |
fcfb9f96 MM |
7859 | tree tparms; |
7860 | tree targs; | |
4393e105 MM |
7861 | tree parm; |
7862 | tree arg; | |
fcfb9f96 | 7863 | { |
4393e105 MM |
7864 | tree rval; |
7865 | tree arg_binfo; | |
fcfb9f96 | 7866 | |
4393e105 MM |
7867 | my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); |
7868 | ||
7869 | arg_binfo = TYPE_BINFO (complete_type (arg)); | |
7870 | rval = get_template_base_recursive (tparms, targs, | |
7871 | parm, arg_binfo, | |
7872 | NULL_TREE, | |
7873 | GTB_IGNORE_TYPE); | |
fcfb9f96 | 7874 | |
4393e105 MM |
7875 | /* Since get_template_base_recursive marks the bases classes, we |
7876 | must unmark them here. */ | |
d6479fe7 | 7877 | dfs_walk (arg_binfo, dfs_unmark, markedp, 0); |
fcfb9f96 MM |
7878 | |
7879 | return rval; | |
7880 | } | |
7881 | ||
db2767b6 MM |
7882 | /* Returns the level of DECL, which declares a template parameter. */ |
7883 | ||
e9659ab0 | 7884 | static int |
db2767b6 MM |
7885 | template_decl_level (decl) |
7886 | tree decl; | |
7887 | { | |
7888 | switch (TREE_CODE (decl)) | |
7889 | { | |
7890 | case TYPE_DECL: | |
7891 | case TEMPLATE_DECL: | |
7892 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
7893 | ||
7894 | case PARM_DECL: | |
7895 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
7896 | ||
7897 | default: | |
7898 | my_friendly_abort (0); | |
5ffe581d | 7899 | return 0; |
db2767b6 MM |
7900 | } |
7901 | } | |
7902 | ||
830bfa74 MM |
7903 | /* Decide whether ARG can be unified with PARM, considering only the |
7904 | cv-qualifiers of each type, given STRICT as documented for unify. | |
7905 | Returns non-zero iff the unification is OK on that basis.*/ | |
e92cc029 | 7906 | |
e9659ab0 | 7907 | static int |
830bfa74 MM |
7908 | check_cv_quals_for_unify (strict, arg, parm) |
7909 | int strict; | |
7910 | tree arg; | |
7911 | tree parm; | |
7912 | { | |
7913 | return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL) | |
91063b51 | 7914 | && !at_least_as_qualified_p (arg, parm)) |
830bfa74 | 7915 | || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL) |
91063b51 | 7916 | && (!at_least_as_qualified_p (parm, arg)))); |
830bfa74 MM |
7917 | } |
7918 | ||
7919 | /* Takes parameters as for type_unification. Returns 0 if the | |
7920 | type deduction suceeds, 1 otherwise. The parameter STRICT is a | |
7921 | bitwise or of the following flags: | |
7922 | ||
7923 | UNIFY_ALLOW_NONE: | |
7924 | Require an exact match between PARM and ARG. | |
7925 | UNIFY_ALLOW_MORE_CV_QUAL: | |
7926 | Allow the deduced ARG to be more cv-qualified than ARG. | |
7927 | UNIFY_ALLOW_LESS_CV_QUAL: | |
7928 | Allow the deduced ARG to be less cv-qualified than ARG. | |
7929 | UNIFY_ALLOW_DERIVED: | |
7930 | Allow the deduced ARG to be a template base class of ARG, | |
7931 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
7932 | ARG. |
7933 | UNIFY_ALLOW_INTEGER: | |
7934 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
7935 | case for more information. */ | |
830bfa74 | 7936 | |
e9659ab0 | 7937 | static int |
4393e105 | 7938 | unify (tparms, targs, parm, arg, strict) |
050367a3 | 7939 | tree tparms, targs, parm, arg; |
830bfa74 | 7940 | int strict; |
8d08fdba MS |
7941 | { |
7942 | int idx; | |
050367a3 | 7943 | tree targ; |
db2767b6 | 7944 | tree tparm; |
8d08fdba MS |
7945 | |
7946 | /* I don't think this will do the right thing with respect to types. | |
7947 | But the only case I've seen it in so far has been array bounds, where | |
7948 | signedness is the only information lost, and I think that will be | |
7949 | okay. */ | |
7950 | while (TREE_CODE (parm) == NOP_EXPR) | |
7951 | parm = TREE_OPERAND (parm, 0); | |
7952 | ||
7953 | if (arg == error_mark_node) | |
7954 | return 1; | |
7955 | if (arg == unknown_type_node) | |
34016c81 JM |
7956 | /* We can't deduce anything from this, but we might get all the |
7957 | template args from other function args. */ | |
7958 | return 0; | |
7959 | ||
db2767b6 | 7960 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 7961 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
7962 | template parameters. We might need them if we're trying to |
7963 | figure out which of two things is more specialized. */ | |
7964 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
7965 | return 0; |
7966 | ||
830bfa74 MM |
7967 | /* Immediately reject some pairs that won't unify because of |
7968 | cv-qualification mismatches. */ | |
7969 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
49432171 | 7970 | && TREE_CODE_CLASS (TREE_CODE (arg)) == 't' |
830bfa74 MM |
7971 | /* We check the cv-qualifiers when unifying with template type |
7972 | parameters below. We want to allow ARG `const T' to unify with | |
7973 | PARM `T' for example, when computing which of two templates | |
7974 | is more specialized, for example. */ | |
7975 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
7976 | && !check_cv_quals_for_unify (strict, arg, parm)) | |
49432171 JM |
7977 | return 1; |
7978 | ||
8d08fdba MS |
7979 | switch (TREE_CODE (parm)) |
7980 | { | |
2ca340ae JM |
7981 | case TYPENAME_TYPE: |
7982 | /* In a type which contains a nested-name-specifier, template | |
7983 | argument values cannot be deduced for template parameters used | |
7984 | within the nested-name-specifier. */ | |
7985 | return 0; | |
7986 | ||
8d08fdba | 7987 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 7988 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
7989 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
7990 | ||
7991 | if (TEMPLATE_TYPE_LEVEL (parm) | |
7992 | != template_decl_level (tparm)) | |
7993 | /* The PARM is not one we're trying to unify. Just check | |
7994 | to see if it matches ARG. */ | |
7995 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 7996 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 7997 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 7998 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 7999 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 8000 | |
73b0fce8 | 8001 | /* Check for mixed types and values. */ |
db2767b6 MM |
8002 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
8003 | && TREE_CODE (tparm) != TYPE_DECL) | |
8004 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
8005 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
8006 | return 1; |
8007 | ||
db2767b6 | 8008 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 8009 | { |
7ddedda4 | 8010 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)) |
db2767b6 MM |
8011 | { |
8012 | /* We arrive here when PARM does not involve template | |
8013 | specialization. */ | |
73b0fce8 | 8014 | |
db2767b6 MM |
8015 | /* ARG must be constructed from a template class. */ |
8016 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
8017 | return 1; | |
73b0fce8 | 8018 | |
db2767b6 | 8019 | { |
7ddedda4 MM |
8020 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); |
8021 | tree parmvec = TYPE_TI_ARGS (parm); | |
db2767b6 MM |
8022 | tree argvec = CLASSTYPE_TI_ARGS (arg); |
8023 | tree argtmplvec | |
8024 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
8025 | int i; | |
8026 | ||
8027 | /* The parameter and argument roles have to be switched here | |
8028 | in order to handle default arguments properly. For example, | |
8029 | template<template <class> class TT> void f(TT<int>) | |
8030 | should be able to accept vector<int> which comes from | |
744fac59 | 8031 | template <class T, class Allocator = allocator> |
73b0fce8 KL |
8032 | class vector. */ |
8033 | ||
fcfcdfc8 | 8034 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) |
db2767b6 MM |
8035 | == error_mark_node) |
8036 | return 1; | |
73b0fce8 | 8037 | |
6b9b6b15 JM |
8038 | /* Deduce arguments T, i from TT<T> or TT<i>. |
8039 | We check each element of PARMVEC and ARGVEC individually | |
8040 | rather than the whole TREE_VEC since they can have | |
8041 | different number of elements. */ | |
8042 | ||
db2767b6 MM |
8043 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
8044 | { | |
8045 | tree t = TREE_VEC_ELT (parmvec, i); | |
73b0fce8 | 8046 | |
830bfa74 MM |
8047 | if (unify (tparms, targs, t, |
8048 | TREE_VEC_ELT (argvec, i), | |
4393e105 | 8049 | UNIFY_ALLOW_NONE)) |
db2767b6 MM |
8050 | return 1; |
8051 | } | |
73b0fce8 | 8052 | } |
db2767b6 MM |
8053 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
8054 | } | |
8055 | } | |
8056 | else | |
8057 | { | |
830bfa74 MM |
8058 | /* If PARM is `const T' and ARG is only `int', we don't have |
8059 | a match unless we are allowing additional qualification. | |
8060 | If ARG is `const int' and PARM is just `T' that's OK; | |
8061 | that binds `const int' to `T'. */ | |
8062 | if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, | |
8063 | arg, parm)) | |
db2767b6 MM |
8064 | return 1; |
8065 | ||
830bfa74 MM |
8066 | /* Consider the case where ARG is `const volatile int' and |
8067 | PARM is `const T'. Then, T should be `volatile int'. */ | |
8068 | arg = | |
91063b51 MM |
8069 | cp_build_qualified_type (arg, |
8070 | CP_TYPE_QUALS (arg) | |
8071 | & ~CP_TYPE_QUALS (parm)); | |
73b0fce8 KL |
8072 | } |
8073 | ||
8074 | /* Simple cases: Value already set, does match or doesn't. */ | |
4393e105 | 8075 | if (targ != NULL_TREE && same_type_p (targ, arg)) |
73b0fce8 | 8076 | return 0; |
050367a3 | 8077 | else if (targ) |
73b0fce8 | 8078 | return 1; |
61cd552e MM |
8079 | |
8080 | /* Make sure that ARG is not a variable-sized array. (Note that | |
8081 | were talking about variable-sized arrays (like `int[n]'), | |
8082 | rather than arrays of unknown size (like `int[]').) We'll | |
8083 | get very confused by such a type since the bound of the array | |
8084 | will not be computable in an instantiation. Besides, such | |
8085 | types are not allowed in ISO C++, so we can do as we please | |
8086 | here. */ | |
8087 | if (TREE_CODE (arg) == ARRAY_TYPE | |
8088 | && !uses_template_parms (arg) | |
8089 | && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg))) | |
8090 | != INTEGER_CST)) | |
8091 | return 1; | |
8092 | ||
050367a3 | 8093 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
8094 | return 0; |
8095 | ||
f84b4be9 | 8096 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
8097 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8098 | ||
8099 | if (TEMPLATE_PARM_LEVEL (parm) | |
8100 | != template_decl_level (tparm)) | |
8101 | /* The PARM is not one we're trying to unify. Just check | |
8102 | to see if it matches ARG. */ | |
8103 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
67ffc812 | 8104 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; |
db2767b6 | 8105 | |
f84b4be9 | 8106 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 8107 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8108 | |
050367a3 | 8109 | if (targ) |
8d08fdba | 8110 | { |
67ffc812 | 8111 | int i = (cp_tree_equal (targ, arg) > 0); |
312e7d50 JM |
8112 | if (i == 1) |
8113 | return 0; | |
8114 | else if (i == 0) | |
8115 | return 1; | |
8116 | else | |
8117 | my_friendly_abort (42); | |
8d08fdba | 8118 | } |
8d08fdba | 8119 | |
161c12b0 JM |
8120 | /* [temp.deduct.type] If, in the declaration of a function template |
8121 | with a non-type template-parameter, the non-type | |
8122 | template-parameter is used in an expression in the function | |
8123 | parameter-list and, if the corresponding template-argument is | |
8124 | deduced, the template-argument type shall match the type of the | |
8125 | template-parameter exactly, except that a template-argument | |
8126 | deduced from an array bound may be of any integral type. */ | |
8127 | if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm))) | |
8128 | /* OK */; | |
8129 | else if ((strict & UNIFY_ALLOW_INTEGER) | |
8130 | && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE | |
8131 | || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE)) | |
8132 | /* OK */; | |
8133 | else | |
8134 | return 1; | |
8135 | ||
050367a3 | 8136 | TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg); |
8d08fdba MS |
8137 | return 0; |
8138 | ||
8139 | case POINTER_TYPE: | |
830bfa74 MM |
8140 | { |
8141 | int sub_strict; | |
4ac14744 | 8142 | |
830bfa74 MM |
8143 | if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg)) |
8144 | return (unify (tparms, targs, parm, | |
4393e105 | 8145 | TYPE_PTRMEMFUNC_FN_TYPE (arg), strict)); |
830bfa74 MM |
8146 | |
8147 | if (TREE_CODE (arg) != POINTER_TYPE) | |
8148 | return 1; | |
8149 | ||
8150 | /* [temp.deduct.call] | |
8151 | ||
8152 | A can be another pointer or pointer to member type that can | |
8153 | be converted to the deduced A via a qualification | |
8154 | conversion (_conv.qual_). | |
8155 | ||
8156 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
8157 | This will allow for additional cv-qualification of the | |
8158 | pointed-to types if appropriate. In general, this is a bit | |
8159 | too generous; we are only supposed to allow qualification | |
8160 | conversions and this method will allow an ARG of char** and | |
8161 | a deduced ARG of const char**. However, overload | |
8162 | resolution will subsequently invalidate the candidate, so | |
8163 | this is probably OK. */ | |
8164 | sub_strict = strict; | |
8165 | ||
8166 | if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE | |
8167 | || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg))) | |
8168 | /* The derived-to-base conversion only persists through one | |
8169 | level of pointers. */ | |
8170 | sub_strict &= ~UNIFY_ALLOW_DERIVED; | |
8171 | ||
8172 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE | |
4393e105 | 8173 | (arg), sub_strict); |
830bfa74 | 8174 | } |
8d08fdba MS |
8175 | |
8176 | case REFERENCE_TYPE: | |
830bfa74 MM |
8177 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
8178 | return 1; | |
8179 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
4393e105 | 8180 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8181 | |
8182 | case ARRAY_TYPE: | |
8183 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
8184 | return 1; | |
3042d5be MM |
8185 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
8186 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
8187 | return 1; | |
8188 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
830bfa74 | 8189 | && unify (tparms, targs, TYPE_DOMAIN (parm), |
4393e105 | 8190 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) |
8d08fdba | 8191 | return 1; |
830bfa74 | 8192 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8193 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8194 | |
8195 | case REAL_TYPE: | |
37c46b43 | 8196 | case COMPLEX_TYPE: |
8d08fdba | 8197 | case INTEGER_TYPE: |
42976354 | 8198 | case BOOLEAN_TYPE: |
5ad5a526 | 8199 | case VOID_TYPE: |
f376e137 MS |
8200 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8201 | return 1; | |
8202 | ||
514a1f18 JM |
8203 | if (TREE_CODE (parm) == INTEGER_TYPE |
8204 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
8d08fdba MS |
8205 | { |
8206 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
830bfa74 | 8207 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), |
4393e105 | 8208 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8209 | return 1; |
8210 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
830bfa74 | 8211 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), |
4393e105 | 8212 | TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8213 | return 1; |
8214 | } | |
514a1f18 JM |
8215 | /* We use the TYPE_MAIN_VARIANT since we have already |
8216 | checked cv-qualification at the top of the | |
8217 | function. */ | |
3bfdc719 MM |
8218 | else if (!same_type_p (TYPE_MAIN_VARIANT (arg), |
8219 | TYPE_MAIN_VARIANT (parm))) | |
ca79f85d JM |
8220 | return 1; |
8221 | ||
8d08fdba MS |
8222 | /* As far as unification is concerned, this wins. Later checks |
8223 | will invalidate it if necessary. */ | |
8224 | return 0; | |
8225 | ||
8226 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 8227 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 8228 | case INTEGER_CST: |
bd6dd845 MS |
8229 | while (TREE_CODE (arg) == NOP_EXPR) |
8230 | arg = TREE_OPERAND (arg, 0); | |
8231 | ||
8d08fdba MS |
8232 | if (TREE_CODE (arg) != INTEGER_CST) |
8233 | return 1; | |
8234 | return !tree_int_cst_equal (parm, arg); | |
8235 | ||
8d08fdba MS |
8236 | case TREE_VEC: |
8237 | { | |
8238 | int i; | |
8239 | if (TREE_CODE (arg) != TREE_VEC) | |
8240 | return 1; | |
8241 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
8242 | return 1; | |
8243 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
830bfa74 | 8244 | if (unify (tparms, targs, |
8d08fdba | 8245 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 8246 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
8247 | return 1; |
8248 | return 0; | |
8249 | } | |
8250 | ||
8d08fdba | 8251 | case RECORD_TYPE: |
f181d4ae | 8252 | case UNION_TYPE: |
db5ae43f | 8253 | if (TYPE_PTRMEMFUNC_FLAG (parm)) |
830bfa74 | 8254 | return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm), |
4393e105 | 8255 | arg, strict); |
8d08fdba | 8256 | |
f181d4ae | 8257 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 8258 | return 1; |
830bfa74 | 8259 | |
5db698f6 | 8260 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 8261 | { |
6467930b | 8262 | tree t = NULL_TREE; |
4393e105 | 8263 | |
830bfa74 | 8264 | if (strict & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
8265 | { |
8266 | /* First, we try to unify the PARM and ARG directly. */ | |
8267 | t = try_class_unification (tparms, targs, | |
8268 | parm, arg); | |
8269 | ||
8270 | if (!t) | |
8271 | { | |
8272 | /* Fallback to the special case allowed in | |
8273 | [temp.deduct.call]: | |
8274 | ||
8275 | If P is a class, and P has the form | |
8276 | template-id, then A can be a derived class of | |
8277 | the deduced A. Likewise, if P is a pointer to | |
8278 | a class of the form template-id, A can be a | |
8279 | pointer to a derived class pointed to by the | |
8280 | deduced A. */ | |
8281 | t = get_template_base (tparms, targs, | |
8282 | parm, arg); | |
8283 | ||
8284 | if (! t || t == error_mark_node) | |
8285 | return 1; | |
8286 | } | |
8287 | } | |
6df47b06 | 8288 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
8289 | && (CLASSTYPE_TI_TEMPLATE (parm) |
8290 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
8291 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
8292 | Then, we should unify `int' and `U'. */ | |
6467930b | 8293 | t = arg; |
4393e105 MM |
8294 | else |
8295 | /* There's no chance of unication succeeding. */ | |
5566b478 | 8296 | return 1; |
6467930b | 8297 | |
830bfa74 | 8298 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 8299 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 8300 | } |
3bfdc719 MM |
8301 | else if (!same_type_p (TYPE_MAIN_VARIANT (parm), |
8302 | TYPE_MAIN_VARIANT (arg))) | |
5566b478 | 8303 | return 1; |
a4443a08 | 8304 | return 0; |
8d08fdba MS |
8305 | |
8306 | case METHOD_TYPE: | |
8d08fdba | 8307 | case FUNCTION_TYPE: |
830bfa74 | 8308 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 8309 | return 1; |
830bfa74 MM |
8310 | |
8311 | if (unify (tparms, targs, TREE_TYPE (parm), | |
4393e105 | 8312 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 8313 | return 1; |
386b8a85 | 8314 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 8315 | TYPE_ARG_TYPES (arg), 1, |
4393e105 | 8316 | DEDUCE_EXACT, 0); |
a4443a08 MS |
8317 | |
8318 | case OFFSET_TYPE: | |
8319 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
8320 | return 1; | |
830bfa74 | 8321 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 8322 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 8323 | return 1; |
830bfa74 | 8324 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8325 | strict); |
a4443a08 | 8326 | |
f62dbf03 | 8327 | case CONST_DECL: |
830bfa74 | 8328 | if (arg != decl_constant_value (parm)) |
f62dbf03 JM |
8329 | return 1; |
8330 | return 0; | |
8331 | ||
027905b4 KL |
8332 | case TEMPLATE_DECL: |
8333 | /* Matched cases are handled by the ARG == PARM test above. */ | |
8334 | return 1; | |
8335 | ||
53929c47 JM |
8336 | case MINUS_EXPR: |
8337 | if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST) | |
8338 | { | |
8339 | /* We handle this case specially, since it comes up with | |
8340 | arrays. In particular, something like: | |
8341 | ||
8342 | template <int N> void f(int (&x)[N]); | |
8343 | ||
8344 | Here, we are trying to unify the range type, which | |
8345 | looks like [0 ... (N - 1)]. */ | |
8346 | tree t, t1, t2; | |
8347 | t1 = TREE_OPERAND (parm, 0); | |
8348 | t2 = TREE_OPERAND (parm, 1); | |
8349 | ||
8350 | /* Should this be a regular fold? */ | |
8351 | t = maybe_fold_nontype_arg (build (PLUS_EXPR, | |
8352 | integer_type_node, | |
8353 | arg, t2)); | |
8354 | ||
4393e105 | 8355 | return unify (tparms, targs, t1, t, strict); |
53929c47 JM |
8356 | } |
8357 | /* else fall through */ | |
8358 | ||
8d08fdba | 8359 | default: |
050367a3 | 8360 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
4393e105 MM |
8361 | /* We're looking at an expression. This can happen with |
8362 | something like: | |
8363 | ||
8364 | template <int I> | |
8365 | void foo(S<I>, S<I + 2>); | |
050367a3 | 8366 | |
4393e105 | 8367 | This is a "nondeduced context": |
050367a3 | 8368 | |
4393e105 MM |
8369 | [deduct.type] |
8370 | ||
8371 | The nondeduced contexts are: | |
050367a3 | 8372 | |
4393e105 MM |
8373 | --A type that is a template-id in which one or more of |
8374 | the template-arguments is an expression that references | |
8375 | a template-parameter. | |
050367a3 | 8376 | |
4393e105 MM |
8377 | In these cases, we assume deduction succeeded, but don't |
8378 | actually infer any unifications. */ | |
8379 | return 0; | |
050367a3 | 8380 | else |
b4c4a9ec MM |
8381 | sorry ("use of `%s' in template type unification", |
8382 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 8383 | |
8d08fdba MS |
8384 | return 1; |
8385 | } | |
8386 | } | |
8d08fdba | 8387 | \f |
03d0f4af MM |
8388 | /* Called if RESULT is explicitly instantiated, or is a member of an |
8389 | explicitly instantiated class, or if using -frepo and the | |
8390 | instantiation of RESULT has been assigned to this file. */ | |
8391 | ||
faae18ab | 8392 | void |
5566b478 | 8393 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
8394 | tree result; |
8395 | int extern_p; | |
8396 | { | |
75650646 MM |
8397 | if (TREE_CODE (result) != FUNCTION_DECL) |
8398 | /* The TREE_PUBLIC flag for function declarations will have been | |
8399 | set correctly by tsubst. */ | |
8400 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
8401 | |
8402 | if (! extern_p) | |
8403 | { | |
8404 | DECL_INTERFACE_KNOWN (result) = 1; | |
8405 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 | 8406 | |
1a408d07 JM |
8407 | /* Always make artificials weak. */ |
8408 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
8409 | comdat_linkage (result); | |
a7d87521 JM |
8410 | /* For WIN32 we also want to put explicit instantiations in |
8411 | linkonce sections. */ | |
1a408d07 | 8412 | else if (TREE_PUBLIC (result)) |
b385c841 | 8413 | maybe_make_one_only (result); |
faae18ab | 8414 | } |
f49422da MS |
8415 | else if (TREE_CODE (result) == FUNCTION_DECL) |
8416 | mark_inline_for_output (result); | |
faae18ab MS |
8417 | } |
8418 | ||
e1467ff2 MM |
8419 | /* Given two function templates PAT1 and PAT2, and explicit template |
8420 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
8421 | |
8422 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
8423 | -1 if PAT2 is more specialized than PAT1. | |
8424 | 0 if neither is more specialized. */ | |
8425 | ||
8426 | int | |
e1467ff2 MM |
8427 | more_specialized (pat1, pat2, explicit_args) |
8428 | tree pat1, pat2, explicit_args; | |
6467930b | 8429 | { |
98c1c668 | 8430 | tree targs; |
73aad9b9 | 8431 | int winner = 0; |
6467930b | 8432 | |
4393e105 | 8433 | targs = get_bindings_overload (pat1, DECL_RESULT (pat2), explicit_args); |
73aad9b9 | 8434 | if (targs) |
36a117a5 | 8435 | --winner; |
6467930b | 8436 | |
4393e105 | 8437 | targs = get_bindings_overload (pat2, DECL_RESULT (pat1), explicit_args); |
73aad9b9 | 8438 | if (targs) |
36a117a5 | 8439 | ++winner; |
6467930b | 8440 | |
73aad9b9 JM |
8441 | return winner; |
8442 | } | |
6467930b | 8443 | |
73aad9b9 | 8444 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 8445 | |
73aad9b9 JM |
8446 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
8447 | -1 if PAT2 is more specialized than PAT1. | |
8448 | 0 if neither is more specialized. */ | |
8449 | ||
8450 | int | |
8451 | more_specialized_class (pat1, pat2) | |
8452 | tree pat1, pat2; | |
8453 | { | |
8454 | tree targs; | |
8455 | int winner = 0; | |
8456 | ||
36a117a5 MM |
8457 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
8458 | TREE_PURPOSE (pat2)); | |
73aad9b9 JM |
8459 | if (targs) |
8460 | --winner; | |
8461 | ||
36a117a5 MM |
8462 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
8463 | TREE_PURPOSE (pat1)); | |
73aad9b9 | 8464 | if (targs) |
6467930b MS |
8465 | ++winner; |
8466 | ||
8467 | return winner; | |
8468 | } | |
73aad9b9 JM |
8469 | |
8470 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 8471 | DECL from the function template FN, with the explicit template |
76b9a14d | 8472 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
4393e105 MM |
8473 | also match. Return NULL_TREE if no satisfactory arguments could be |
8474 | found. */ | |
73aad9b9 | 8475 | |
76b9a14d JM |
8476 | static tree |
8477 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 8478 | tree fn, decl, explicit_args; |
76b9a14d | 8479 | int check_rettype; |
73aad9b9 | 8480 | { |
98c1c668 | 8481 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 8482 | tree targs = make_scratch_vec (ntparms); |
4393e105 | 8483 | tree decl_type; |
03017874 | 8484 | tree decl_arg_types; |
98c1c668 JM |
8485 | int i; |
8486 | ||
4393e105 MM |
8487 | /* Substitute the explicit template arguments into the type of DECL. |
8488 | The call to fn_type_unification will handle substitution into the | |
8489 | FN. */ | |
8490 | decl_type = TREE_TYPE (decl); | |
8491 | if (explicit_args && uses_template_parms (decl_type)) | |
8492 | { | |
8493 | tree tmpl; | |
8494 | tree converted_args; | |
8495 | ||
8496 | if (DECL_TEMPLATE_INFO (decl)) | |
8497 | tmpl = DECL_TI_TEMPLATE (decl); | |
8498 | else | |
8499 | /* We can get here for some illegal specializations. */ | |
8500 | return NULL_TREE; | |
8501 | ||
8502 | converted_args | |
8503 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
8504 | explicit_args, NULL_TREE, | |
8505 | /*complain=*/0, | |
8506 | /*require_all_arguments=*/0)); | |
8507 | if (converted_args == error_mark_node) | |
8508 | return NULL_TREE; | |
8509 | ||
8510 | decl_type = tsubst (decl_type, converted_args, /*complain=*/0, | |
8511 | NULL_TREE); | |
8512 | if (decl_type == error_mark_node) | |
8513 | return NULL_TREE; | |
8514 | } | |
8515 | ||
03017874 MM |
8516 | /* If FN is a static member function, adjust the type of DECL |
8517 | appropriately. */ | |
4393e105 | 8518 | decl_arg_types = TYPE_ARG_TYPES (decl_type); |
d7684f2d MM |
8519 | if (DECL_STATIC_FUNCTION_P (fn) |
8520 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 8521 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 8522 | |
e1467ff2 | 8523 | i = fn_type_unification (fn, explicit_args, targs, |
03017874 MM |
8524 | decl_arg_types, |
8525 | TREE_TYPE (decl_type), | |
8526 | DEDUCE_EXACT); | |
98c1c668 | 8527 | |
76b9a14d JM |
8528 | if (i != 0) |
8529 | return NULL_TREE; | |
8530 | ||
8531 | if (check_rettype) | |
e1467ff2 MM |
8532 | { |
8533 | /* Check to see that the resulting return type is also OK. */ | |
36a117a5 | 8534 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs, |
4393e105 | 8535 | /*complain=*/0, NULL_TREE); |
e1467ff2 | 8536 | |
3bfdc719 | 8537 | if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl)))) |
e1467ff2 | 8538 | return NULL_TREE; |
e1467ff2 MM |
8539 | } |
8540 | ||
76b9a14d JM |
8541 | return targs; |
8542 | } | |
8543 | ||
8544 | /* For most uses, we want to check the return type. */ | |
8545 | ||
8546 | tree | |
8547 | get_bindings (fn, decl, explicit_args) | |
8548 | tree fn, decl, explicit_args; | |
8549 | { | |
8550 | return get_bindings_real (fn, decl, explicit_args, 1); | |
8551 | } | |
8552 | ||
8553 | /* But for more_specialized, we only care about the parameter types. */ | |
8554 | ||
8555 | static tree | |
8556 | get_bindings_overload (fn, decl, explicit_args) | |
8557 | tree fn, decl, explicit_args; | |
8558 | { | |
8559 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
8560 | } |
8561 | ||
36a117a5 MM |
8562 | /* Return the innermost template arguments that, when applied to a |
8563 | template specialization whose innermost template parameters are | |
8564 | TPARMS, and whose specialization arguments are ARGS, yield the | |
8565 | ARGS. | |
8566 | ||
8567 | For example, suppose we have: | |
8568 | ||
8569 | template <class T, class U> struct S {}; | |
8570 | template <class T> struct S<T*, int> {}; | |
8571 | ||
8572 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
8573 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
8574 | int}. The resulting vector will be {double}, indicating that `T' | |
8575 | is bound to `double'. */ | |
8576 | ||
bd6dd845 | 8577 | static tree |
36a117a5 MM |
8578 | get_class_bindings (tparms, parms, args) |
8579 | tree tparms, parms, args; | |
73aad9b9 | 8580 | { |
3b3ba9f0 | 8581 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
73aad9b9 JM |
8582 | tree vec = make_temp_vec (ntparms); |
8583 | ||
36a117a5 | 8584 | args = innermost_args (args); |
8d019cef | 8585 | |
4393e105 | 8586 | if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE)) |
fcfb9f96 | 8587 | return NULL_TREE; |
73aad9b9 JM |
8588 | |
8589 | for (i = 0; i < ntparms; ++i) | |
8590 | if (! TREE_VEC_ELT (vec, i)) | |
8591 | return NULL_TREE; | |
8592 | ||
8593 | return vec; | |
8594 | } | |
8595 | ||
104bf76a MM |
8596 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. |
8597 | Pick the most specialized template, and return the corresponding | |
8598 | instantiation, or if there is no corresponding instantiation, the | |
8599 | template itself. EXPLICIT_ARGS is any template arguments explicity | |
8600 | mentioned in a template-id. If there is no most specialized | |
8601 | tempalte, error_mark_node is returned. If there are no templates | |
8602 | at all, NULL_TREE is returned. */ | |
73aad9b9 JM |
8603 | |
8604 | tree | |
104bf76a MM |
8605 | most_specialized_instantiation (instantiations, explicit_args) |
8606 | tree instantiations; | |
8607 | tree explicit_args; | |
73aad9b9 | 8608 | { |
104bf76a | 8609 | tree fn, champ; |
73aad9b9 JM |
8610 | int fate; |
8611 | ||
104bf76a | 8612 | if (!instantiations) |
73aad9b9 JM |
8613 | return NULL_TREE; |
8614 | ||
104bf76a MM |
8615 | champ = instantiations; |
8616 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 8617 | { |
104bf76a MM |
8618 | fate = more_specialized (TREE_VALUE (champ), |
8619 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
8620 | if (fate == 1) |
8621 | ; | |
8622 | else | |
8623 | { | |
8624 | if (fate == 0) | |
8625 | { | |
8626 | fn = TREE_CHAIN (fn); | |
8627 | if (! fn) | |
8628 | return error_mark_node; | |
8629 | } | |
104bf76a | 8630 | champ = fn; |
73aad9b9 JM |
8631 | } |
8632 | } | |
8633 | ||
104bf76a | 8634 | for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 8635 | { |
104bf76a MM |
8636 | fate = more_specialized (TREE_VALUE (champ), |
8637 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
8638 | if (fate != 1) |
8639 | return error_mark_node; | |
8640 | } | |
8641 | ||
104bf76a MM |
8642 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); |
8643 | } | |
8644 | ||
8645 | /* Return the most specialized of the list of templates in FNS that can | |
8646 | produce an instantiation matching DECL, given the explicit template | |
8647 | arguments EXPLICIT_ARGS. */ | |
8648 | ||
e9659ab0 | 8649 | static tree |
104bf76a MM |
8650 | most_specialized (fns, decl, explicit_args) |
8651 | tree fns, decl, explicit_args; | |
8652 | { | |
8653 | tree candidates = NULL_TREE; | |
8654 | tree fn, args; | |
8655 | ||
8656 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
8657 | { | |
8658 | tree candidate = TREE_VALUE (fn); | |
8659 | ||
8660 | args = get_bindings (candidate, decl, explicit_args); | |
8661 | if (args) | |
8662 | candidates = scratch_tree_cons (NULL_TREE, candidate, | |
8663 | candidates); | |
8664 | } | |
8665 | ||
8666 | return most_specialized_instantiation (candidates, explicit_args); | |
73aad9b9 JM |
8667 | } |
8668 | ||
36a117a5 MM |
8669 | /* If DECL is a specialization of some template, return the most |
8670 | general such template. For example, given: | |
8671 | ||
8672 | template <class T> struct S { template <class U> void f(U); }; | |
8673 | ||
8674 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
8675 | the full template. This function will not trace past partial | |
8676 | specializations, however. For example, given in addition: | |
8677 | ||
8678 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
8679 | ||
8680 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
8681 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 8682 | |
e9659ab0 | 8683 | static tree |
36a117a5 MM |
8684 | most_general_template (decl) |
8685 | tree decl; | |
73aad9b9 | 8686 | { |
36a117a5 MM |
8687 | while (DECL_TEMPLATE_INFO (decl)) |
8688 | decl = DECL_TI_TEMPLATE (decl); | |
8689 | ||
8690 | return decl; | |
8691 | } | |
8692 | ||
8693 | /* Return the most specialized of the class template specializations | |
8694 | of TMPL which can produce an instantiation matching ARGS, or | |
8695 | error_mark_node if the choice is ambiguous. */ | |
8696 | ||
e9659ab0 | 8697 | static tree |
36a117a5 MM |
8698 | most_specialized_class (tmpl, args) |
8699 | tree tmpl; | |
8700 | tree args; | |
8701 | { | |
8702 | tree list = NULL_TREE; | |
8703 | tree t; | |
8704 | tree champ; | |
73aad9b9 JM |
8705 | int fate; |
8706 | ||
36a117a5 MM |
8707 | tmpl = most_general_template (tmpl); |
8708 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 8709 | { |
36a117a5 MM |
8710 | tree spec_args |
8711 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
8712 | if (spec_args) | |
73aad9b9 JM |
8713 | { |
8714 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
8715 | TREE_TYPE (list) = TREE_TYPE (t); | |
8716 | } | |
8717 | } | |
8718 | ||
8719 | if (! list) | |
8720 | return NULL_TREE; | |
8721 | ||
8722 | t = list; | |
8723 | champ = t; | |
8724 | t = TREE_CHAIN (t); | |
8725 | for (; t; t = TREE_CHAIN (t)) | |
8726 | { | |
8727 | fate = more_specialized_class (champ, t); | |
8728 | if (fate == 1) | |
8729 | ; | |
8730 | else | |
8731 | { | |
8732 | if (fate == 0) | |
8733 | { | |
8734 | t = TREE_CHAIN (t); | |
8735 | if (! t) | |
8736 | return error_mark_node; | |
8737 | } | |
8738 | champ = t; | |
8739 | } | |
8740 | } | |
8741 | ||
8742 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
8743 | { | |
85b71cf2 | 8744 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
8745 | if (fate != 1) |
8746 | return error_mark_node; | |
8747 | } | |
8748 | ||
8749 | return champ; | |
8750 | } | |
8751 | ||
8d08fdba | 8752 | /* called from the parser. */ |
e92cc029 | 8753 | |
8d08fdba | 8754 | void |
6633d636 | 8755 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 8756 | tree declspecs, declarator, storage; |
8d08fdba | 8757 | { |
c11b6f21 | 8758 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 8759 | tree result = NULL_TREE; |
faae18ab | 8760 | int extern_p = 0; |
e8abc66f | 8761 | |
ec255269 MS |
8762 | if (! DECL_LANG_SPECIFIC (decl)) |
8763 | { | |
8251199e | 8764 | cp_error ("explicit instantiation of non-template `%#D'", decl); |
ec255269 MS |
8765 | return; |
8766 | } | |
03d0f4af | 8767 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 8768 | { |
03d0f4af MM |
8769 | /* There is an asymmetry here in the way VAR_DECLs and |
8770 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
8771 | the latter, the DECL we get back will be marked as a | |
8772 | template instantiation, and the appropriate | |
8773 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
8774 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
8775 | should handle VAR_DECLs as it currently handles | |
8776 | FUNCTION_DECLs. */ | |
6633d636 MS |
8777 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); |
8778 | if (result && TREE_CODE (result) != VAR_DECL) | |
03d0f4af | 8779 | { |
8251199e | 8780 | cp_error ("no matching template for `%D' found", result); |
03d0f4af MM |
8781 | return; |
8782 | } | |
6633d636 MS |
8783 | } |
8784 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
8785 | { | |
8251199e | 8786 | cp_error ("explicit instantiation of `%#D'", decl); |
6633d636 MS |
8787 | return; |
8788 | } | |
03d0f4af MM |
8789 | else |
8790 | result = decl; | |
672476cb | 8791 | |
03d0f4af MM |
8792 | /* Check for various error cases. Note that if the explicit |
8793 | instantiation is legal the RESULT will currently be marked as an | |
8794 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
8795 | until we get here. */ | |
8796 | ||
8797 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 8798 | { |
03d0f4af MM |
8799 | /* [temp.spec] |
8800 | ||
8801 | No program shall both explicitly instantiate and explicitly | |
8802 | specialize a template. */ | |
8251199e JM |
8803 | cp_error ("explicit instantiation of `%#D' after", result); |
8804 | cp_error_at ("explicit specialization here", result); | |
672476cb MM |
8805 | return; |
8806 | } | |
03d0f4af MM |
8807 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
8808 | { | |
8809 | /* [temp.spec] | |
98c1c668 | 8810 | |
03d0f4af MM |
8811 | No program shall explicitly instantiate any template more |
8812 | than once. | |
8813 | ||
8814 | We check DECL_INTERFACE_KNOWN so as not to complain when the | |
8815 | first instantiation was `extern' and the second is not, and | |
8816 | EXTERN_P for the opposite case. */ | |
8817 | if (DECL_INTERFACE_KNOWN (result) && !extern_p) | |
8251199e | 8818 | cp_error ("duplicate explicit instantiation of `%#D'", result); |
03d0f4af MM |
8819 | |
8820 | /* If we've already instantiated the template, just return now. */ | |
8821 | if (DECL_INTERFACE_KNOWN (result)) | |
8822 | return; | |
8823 | } | |
8824 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 8825 | { |
8251199e | 8826 | cp_error ("no matching template for `%D' found", result); |
faae18ab MS |
8827 | return; |
8828 | } | |
03d0f4af | 8829 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 8830 | { |
8251199e | 8831 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); |
6633d636 MS |
8832 | return; |
8833 | } | |
8834 | ||
a0a33927 MS |
8835 | if (flag_external_templates) |
8836 | return; | |
8837 | ||
f0e01782 | 8838 | if (storage == NULL_TREE) |
00595019 | 8839 | ; |
faae18ab | 8840 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af MM |
8841 | { |
8842 | if (pedantic) | |
8251199e | 8843 | cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations"); |
03d0f4af MM |
8844 | extern_p = 1; |
8845 | } | |
f0e01782 | 8846 | else |
8251199e | 8847 | cp_error ("storage class `%D' applied to template instantiation", |
f0e01782 | 8848 | storage); |
5566b478 | 8849 | |
03d0f4af | 8850 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
5566b478 | 8851 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 8852 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
8853 | if (! extern_p) |
8854 | instantiate_decl (result); | |
7177d104 MS |
8855 | } |
8856 | ||
faae18ab MS |
8857 | void |
8858 | mark_class_instantiated (t, extern_p) | |
8859 | tree t; | |
8860 | int extern_p; | |
8861 | { | |
8862 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
8863 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
8864 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
8865 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
8866 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
8867 | if (! extern_p) | |
8868 | { | |
8869 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
8870 | rest_of_type_compilation (t, 1); | |
8871 | } | |
8872 | } | |
e8abc66f | 8873 | |
7177d104 | 8874 | void |
ca79f85d JM |
8875 | do_type_instantiation (t, storage) |
8876 | tree t, storage; | |
7177d104 | 8877 | { |
e8abc66f MS |
8878 | int extern_p = 0; |
8879 | int nomem_p = 0; | |
5566b478 MS |
8880 | int static_p = 0; |
8881 | ||
ca79f85d JM |
8882 | if (TREE_CODE (t) == TYPE_DECL) |
8883 | t = TREE_TYPE (t); | |
8884 | ||
7ddedda4 | 8885 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 8886 | { |
8251199e | 8887 | cp_error ("explicit instantiation of non-template type `%T'", t); |
ca79f85d JM |
8888 | return; |
8889 | } | |
8890 | ||
5566b478 | 8891 | complete_type (t); |
7177d104 | 8892 | |
a292b002 MS |
8893 | /* With -fexternal-templates, explicit instantiations are treated the same |
8894 | as implicit ones. */ | |
a0a33927 MS |
8895 | if (flag_external_templates) |
8896 | return; | |
8897 | ||
f0e01782 MS |
8898 | if (TYPE_SIZE (t) == NULL_TREE) |
8899 | { | |
8251199e | 8900 | cp_error ("explicit instantiation of `%#T' before definition of template", |
f0e01782 MS |
8901 | t); |
8902 | return; | |
8903 | } | |
8904 | ||
03d0f4af | 8905 | if (storage != NULL_TREE) |
f0e01782 | 8906 | { |
03d0f4af | 8907 | if (pedantic) |
8251199e JM |
8908 | cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations", |
8909 | IDENTIFIER_POINTER (storage)); | |
03d0f4af MM |
8910 | |
8911 | if (storage == ridpointers[(int) RID_INLINE]) | |
8912 | nomem_p = 1; | |
8913 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
8914 | extern_p = 1; | |
8915 | else if (storage == ridpointers[(int) RID_STATIC]) | |
8916 | static_p = 1; | |
8917 | else | |
8918 | { | |
8251199e | 8919 | cp_error ("storage class `%D' applied to template instantiation", |
03d0f4af MM |
8920 | storage); |
8921 | extern_p = 0; | |
8922 | } | |
f0e01782 MS |
8923 | } |
8924 | ||
03d0f4af MM |
8925 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
8926 | { | |
8927 | /* [temp.spec] | |
a292b002 | 8928 | |
03d0f4af MM |
8929 | No program shall both explicitly instantiate and explicitly |
8930 | specialize a template. */ | |
8251199e JM |
8931 | cp_error ("explicit instantiation of `%#T' after", t); |
8932 | cp_error_at ("explicit specialization here", t); | |
03d0f4af MM |
8933 | return; |
8934 | } | |
8935 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 8936 | { |
03d0f4af MM |
8937 | /* [temp.spec] |
8938 | ||
8939 | No program shall explicitly instantiate any template more | |
8940 | than once. | |
8941 | ||
8942 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit | |
8943 | instantiation was `extern', and if EXTERN_P then the second | |
8944 | is. Both cases are OK. */ | |
8945 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p) | |
8251199e | 8946 | cp_error ("duplicate explicit instantiation of `%#T'", t); |
03d0f4af MM |
8947 | |
8948 | /* If we've already instantiated the template, just return now. */ | |
8949 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
8950 | return; | |
44a8d0b3 | 8951 | } |
e8abc66f | 8952 | |
03d0f4af MM |
8953 | mark_class_instantiated (t, extern_p); |
8954 | repo_template_instantiated (t, extern_p); | |
8955 | ||
e8abc66f MS |
8956 | if (nomem_p) |
8957 | return; | |
8958 | ||
7177d104 | 8959 | { |
db5ae43f | 8960 | tree tmp; |
5566b478 | 8961 | |
03d0f4af MM |
8962 | /* In contrast to implicit instantiation, where only the |
8963 | declarations, and not the definitions, of members are | |
8964 | instantiated, we have here: | |
8965 | ||
8966 | [temp.explicit] | |
8967 | ||
8968 | The explicit instantiation of a class template specialization | |
8969 | implies the instantiation of all of its members not | |
8970 | previously explicitly specialized in the translation unit | |
8971 | containing the explicit instantiation. | |
8972 | ||
8973 | Of course, we can't instantiate member template classes, since | |
8974 | we don't have any arguments for them. Note that the standard | |
8975 | is unclear on whether the instatiation of the members are | |
8976 | *explicit* instantiations or not. We choose to be generous, | |
8977 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
8978 | the explicit instantiation of a class where some of the members | |
8979 | have no definition in the current translation unit. */ | |
8980 | ||
5566b478 MS |
8981 | if (! static_p) |
8982 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 8983 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 8984 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
8985 | { |
8986 | mark_decl_instantiated (tmp, extern_p); | |
8987 | repo_template_instantiated (tmp, extern_p); | |
8988 | if (! extern_p) | |
8989 | instantiate_decl (tmp); | |
8990 | } | |
8991 | ||
8992 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
8993 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 8994 | { |
5566b478 | 8995 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 8996 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
8997 | if (! extern_p) |
8998 | instantiate_decl (tmp); | |
863adfc0 | 8999 | } |
7177d104 | 9000 | |
a292b002 | 9001 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
e4a84209 MM |
9002 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) |
9003 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
f376e137 | 9004 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); |
a292b002 | 9005 | } |
8d08fdba | 9006 | } |
a28e3c7f | 9007 | |
36a117a5 MM |
9008 | /* Given a function DECL, which is a specialization of TMPL, modify |
9009 | DECL to be a re-instantiation of TMPL with the same template | |
9010 | arguments. TMPL should be the template into which tsubst'ing | |
9011 | should occur for DECL, not the most general template. | |
830bfa74 MM |
9012 | |
9013 | One reason for doing this is a scenario like this: | |
9014 | ||
9015 | template <class T> | |
9016 | void f(const T&, int i); | |
9017 | ||
9018 | void g() { f(3, 7); } | |
9019 | ||
9020 | template <class T> | |
9021 | void f(const T& t, const int i) { } | |
9022 | ||
9023 | Note that when the template is first instantiated, with | |
9024 | instantiate_template, the resulting DECL will have no name for the | |
9025 | first parameter, and the wrong type for the second. So, when we go | |
9026 | to instantiate the DECL, we regenerate it. */ | |
9027 | ||
e9659ab0 | 9028 | static void |
830bfa74 MM |
9029 | regenerate_decl_from_template (decl, tmpl) |
9030 | tree decl; | |
9031 | tree tmpl; | |
9032 | { | |
9033 | tree args; | |
830bfa74 MM |
9034 | tree code_pattern; |
9035 | tree new_decl; | |
36a117a5 MM |
9036 | tree gen_tmpl; |
9037 | int unregistered; | |
830bfa74 MM |
9038 | |
9039 | args = DECL_TI_ARGS (decl); | |
9040 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
9041 | ||
8251199e JM |
9042 | /* Unregister the specialization so that when we tsubst we will not |
9043 | just return DECL. We don't have to unregister DECL from TMPL | |
9044 | because if would only be registered there if it were a partial | |
9045 | instantiation of a specialization, which it isn't: it's a full | |
9046 | instantiation. */ | |
36a117a5 MM |
9047 | gen_tmpl = most_general_template (tmpl); |
9048 | unregistered = unregister_specialization (decl, gen_tmpl); | |
9049 | ||
9050 | /* If the DECL was not unregistered then something peculiar is | |
9051 | happening: we created a specialization but did not call | |
9052 | register_specialization for it. */ | |
9053 | my_friendly_assert (unregistered, 0); | |
9054 | ||
61a127b3 MM |
9055 | if (TREE_CODE (decl) == VAR_DECL) |
9056 | /* Make sure that we can see identifiers, and compute access | |
9057 | correctly, for the class members used in the declaration of | |
9058 | this static variable. */ | |
9059 | pushclass (DECL_CONTEXT (decl), 2); | |
9060 | ||
d8c4447d | 9061 | /* Do the substitution to get the new declaration. */ |
4393e105 | 9062 | new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE); |
830bfa74 MM |
9063 | |
9064 | if (TREE_CODE (decl) == VAR_DECL) | |
9065 | { | |
9066 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
830bfa74 | 9067 | DECL_INITIAL (new_decl) = |
d8c4447d | 9068 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
4393e105 | 9069 | /*complain=*/1, DECL_TI_TEMPLATE (decl)); |
61a127b3 | 9070 | /* Pop the class context we pushed above. */ |
830bfa74 MM |
9071 | popclass (1); |
9072 | } | |
2b0a63a3 | 9073 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
fbf1c34b MM |
9074 | { |
9075 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
9076 | new decl. */ | |
9077 | DECL_INITIAL (new_decl) = error_mark_node; | |
9078 | /* And don't complain about a duplicate definition. */ | |
9079 | DECL_INITIAL (decl) = NULL_TREE; | |
9080 | } | |
36a117a5 MM |
9081 | |
9082 | /* The immediate parent of the new template is still whatever it was | |
9083 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
9084 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
9085 | tsubst always calculates the name as if the function in question | |
9086 | were really a template instance, and sometimes, with friend | |
9087 | functions, this is not so. See tsubst_friend_function for | |
9088 | details. */ | |
9089 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
9090 | DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl); | |
9091 | DECL_RTL (new_decl) = DECL_RTL (decl); | |
9092 | ||
9093 | /* Call duplicate decls to merge the old and new declarations. */ | |
830bfa74 MM |
9094 | duplicate_decls (new_decl, decl); |
9095 | ||
36a117a5 MM |
9096 | /* Now, re-register the specialization. */ |
9097 | register_specialization (decl, gen_tmpl, args); | |
830bfa74 MM |
9098 | } |
9099 | ||
f84b4be9 JM |
9100 | /* Produce the definition of D, a _DECL generated from a template. */ |
9101 | ||
a28e3c7f | 9102 | tree |
5566b478 MS |
9103 | instantiate_decl (d) |
9104 | tree d; | |
a28e3c7f | 9105 | { |
36a117a5 MM |
9106 | tree tmpl = DECL_TI_TEMPLATE (d); |
9107 | tree args = DECL_TI_ARGS (d); | |
830bfa74 | 9108 | tree td; |
36a117a5 MM |
9109 | tree code_pattern; |
9110 | tree spec; | |
9111 | tree gen_tmpl; | |
5566b478 | 9112 | int nested = in_function_p (); |
5566b478 | 9113 | int pattern_defined; |
c27be9b9 JM |
9114 | int line = lineno; |
9115 | char *file = input_filename; | |
5566b478 | 9116 | |
36a117a5 MM |
9117 | /* This function should only be used to instantiate templates for |
9118 | functions and static member variables. */ | |
9119 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
9120 | || TREE_CODE (d) == VAR_DECL, 0); | |
9121 | ||
fbf1c34b | 9122 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af MM |
9123 | /* D has already been instantiated. It might seem reasonable to |
9124 | check whether or not D is an explict instantiation, and, if so, | |
9125 | stop here. But when an explicit instantiation is deferred | |
9126 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
9127 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
9128 | return d; |
9129 | ||
9130 | /* If we already have a specialization of this declaration, then | |
9131 | there's no reason to instantiate it. Note that | |
9132 | retrieve_specialization gives us both instantiations and | |
9133 | specializations, so we must explicitly check | |
9134 | DECL_TEMPLATE_SPECIALIZATION. */ | |
9135 | gen_tmpl = most_general_template (tmpl); | |
9136 | spec = retrieve_specialization (gen_tmpl, args); | |
9137 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
9138 | return spec; | |
9139 | ||
9140 | /* This needs to happen before any tsubsting. */ | |
9141 | if (! push_tinst_level (d)) | |
9142 | return d; | |
9143 | ||
4d85e00e MM |
9144 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
9145 | for the instantiation. This is not always the most general | |
9146 | template. Consider, for example: | |
9147 | ||
9148 | template <class T> | |
9149 | struct S { template <class U> void f(); | |
9150 | template <> void f<int>(); }; | |
9151 | ||
9152 | and an instantiation of S<double>::f<int>. We want TD to be the | |
9153 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
9154 | td = tmpl; | |
9155 | for (td = tmpl; | |
9156 | /* An instantiation cannot have a definition, so we need a | |
9157 | more general template. */ | |
9158 | DECL_TEMPLATE_INSTANTIATION (td) | |
9159 | /* We must also deal with friend templates. Given: | |
9160 | ||
9161 | template <class T> struct S { | |
9162 | template <class U> friend void f() {}; | |
9163 | }; | |
9164 | ||
9165 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
9166 | so far as the language is concerned, but that's still | |
9167 | where we get the pattern for the instantiation from. On | |
9168 | ther hand, if the definition comes outside the class, say: | |
9169 | ||
9170 | template <class T> struct S { | |
9171 | template <class U> friend void f(); | |
9172 | }; | |
9173 | template <class U> friend void f() {} | |
9174 | ||
9175 | we don't need to look any further. That's what the check for | |
9176 | DECL_INITIAL is for. */ | |
9177 | || (TREE_CODE (d) == FUNCTION_DECL | |
61289ca3 | 9178 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) |
4d85e00e | 9179 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))); |
f84b4be9 | 9180 | ) |
4d85e00e MM |
9181 | { |
9182 | /* The present template, TD, should not be a definition. If it | |
9183 | were a definition, we should be using it! Note that we | |
9184 | cannot restructure the loop to just keep going until we find | |
9185 | a template with a definition, since that might go too far if | |
9186 | a specialization was declared, but not defined. */ | |
4d85e00e MM |
9187 | my_friendly_assert (!(TREE_CODE (d) == VAR_DECL |
9188 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), | |
9189 | 0); | |
9190 | ||
9191 | /* Fetch the more general template. */ | |
9192 | td = DECL_TI_TEMPLATE (td); | |
9193 | } | |
27bb8339 | 9194 | |
fee23f54 | 9195 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 9196 | |
5566b478 | 9197 | if (TREE_CODE (d) == FUNCTION_DECL) |
36a117a5 | 9198 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 | 9199 | else |
36a117a5 | 9200 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
de22184b MS |
9201 | |
9202 | push_to_top_level (); | |
9203 | lineno = DECL_SOURCE_LINE (d); | |
9204 | input_filename = DECL_SOURCE_FILE (d); | |
9205 | ||
de22184b | 9206 | if (pattern_defined) |
5566b478 MS |
9207 | { |
9208 | repo_template_used (d); | |
9209 | ||
9210 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
9211 | { | |
9212 | if (flag_alt_external_templates) | |
9213 | { | |
9214 | if (interface_unknown) | |
9215 | warn_if_unknown_interface (d); | |
9216 | } | |
fee23f54 | 9217 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
9218 | { |
9219 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 9220 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
9221 | } |
9222 | else | |
fee23f54 | 9223 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
9224 | } |
9225 | ||
e92cc029 | 9226 | if (at_eof) |
5566b478 MS |
9227 | import_export_decl (d); |
9228 | } | |
9229 | ||
c4ae3f91 JM |
9230 | /* Reject all external templates except inline functions. */ |
9231 | if (DECL_INTERFACE_KNOWN (d) | |
9232 | && ! DECL_NOT_REALLY_EXTERN (d) | |
9233 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
9234 | goto out; | |
9235 | ||
67ffc812 | 9236 | if (TREE_CODE (d) == VAR_DECL |
711734a9 | 9237 | && TREE_READONLY (d) |
67ffc812 MM |
9238 | && DECL_INITIAL (d) == NULL_TREE |
9239 | && DECL_INITIAL (code_pattern) != NULL_TREE) | |
9240 | /* We need to set up DECL_INITIAL regardless of pattern_defined if | |
9241 | the variable is a static const initialized in the class body. */; | |
9242 | else if (! pattern_defined | |
9243 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) | |
9244 | && ! at_eof)) | |
5566b478 | 9245 | { |
67ffc812 | 9246 | /* Defer all templates except inline functions used in another |
c27be9b9 JM |
9247 | function. We restore the source position here because it's used |
9248 | by add_pending_template. */ | |
9249 | lineno = line; | |
9250 | input_filename = file; | |
9251 | ||
03d0f4af MM |
9252 | if (at_eof && !pattern_defined |
9253 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
9254 | /* [temp.explicit] | |
9255 | ||
9256 | The definition of a non-exported function template, a | |
9257 | non-exported member function template, or a non-exported | |
9258 | member function or static data member of a class template | |
9259 | shall be present in every translation unit in which it is | |
9260 | explicitly instantiated. */ | |
8251199e | 9261 | cp_error ("explicit instantiation of `%D' but no definition available", |
03d0f4af MM |
9262 | d); |
9263 | ||
5566b478 | 9264 | add_pending_template (d); |
de22184b | 9265 | goto out; |
5566b478 MS |
9266 | } |
9267 | ||
2b0a63a3 MM |
9268 | /* We're now committed to instantiating this template. Mark it as |
9269 | instantiated so that recursive calls to instantiate_decl do not | |
9270 | try to instantiate it again. */ | |
fbf1c34b | 9271 | DECL_TEMPLATE_INSTANTIATED (d) = 1; |
120722ac | 9272 | |
2b0a63a3 MM |
9273 | /* Regenerate the declaration in case the template has been modified |
9274 | by a subsequent redeclaration. */ | |
9275 | regenerate_decl_from_template (d, td); | |
9276 | ||
120722ac MM |
9277 | /* We already set the file and line above. Reset them now in case |
9278 | they changed as a result of calling regenerate_decl_from_template. */ | |
5156628f MS |
9279 | lineno = DECL_SOURCE_LINE (d); |
9280 | input_filename = DECL_SOURCE_FILE (d); | |
9281 | ||
5566b478 MS |
9282 | if (TREE_CODE (d) == VAR_DECL) |
9283 | { | |
9284 | DECL_IN_AGGR_P (d) = 0; | |
9285 | if (DECL_INTERFACE_KNOWN (d)) | |
9286 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
9287 | else | |
9288 | { | |
9289 | DECL_EXTERNAL (d) = 1; | |
9290 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
9291 | } | |
9292 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
9293 | } | |
9294 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
9295 | { | |
fee23f54 | 9296 | tree t = DECL_SAVED_TREE (code_pattern); |
5566b478 | 9297 | |
c11b6f21 | 9298 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
9299 | store_parm_decls (); |
9300 | ||
e76a2646 MS |
9301 | if (t && TREE_CODE (t) == RETURN_INIT) |
9302 | { | |
9303 | store_return_init | |
9304 | (TREE_OPERAND (t, 0), | |
4393e105 | 9305 | tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, tmpl)); |
e76a2646 MS |
9306 | t = TREE_CHAIN (t); |
9307 | } | |
9308 | ||
5566b478 MS |
9309 | if (t && TREE_CODE (t) == CTOR_INITIALIZER) |
9310 | { | |
9311 | current_member_init_list | |
9312 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
9313 | current_base_init_list | |
9314 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
9315 | t = TREE_CHAIN (t); | |
9316 | } | |
9317 | ||
9318 | setup_vtbl_ptr (); | |
9319 | /* Always keep the BLOCK node associated with the outermost | |
956d6950 | 9320 | pair of curly braces of a function. These are needed |
5566b478 MS |
9321 | for correct operation of dwarfout.c. */ |
9322 | keep_next_level (); | |
9323 | ||
9324 | my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42); | |
4393e105 | 9325 | tsubst_expr (t, args, /*complain=*/1, tmpl); |
a28e3c7f | 9326 | |
5566b478 | 9327 | finish_function (lineno, 0, nested); |
5566b478 MS |
9328 | } |
9329 | ||
de22184b | 9330 | out: |
c27be9b9 JM |
9331 | lineno = line; |
9332 | input_filename = file; | |
9333 | ||
5566b478 | 9334 | pop_from_top_level (); |
5566b478 | 9335 | pop_tinst_level (); |
a28e3c7f | 9336 | |
a28e3c7f MS |
9337 | return d; |
9338 | } | |
5566b478 | 9339 | |
4393e105 MM |
9340 | /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it |
9341 | is an initializer list: the TREE_PURPOSEs are DECLs, and the | |
9342 | TREE_VALUEs are initializer values. Used by instantiate_decl. */ | |
9343 | ||
824b9a4c | 9344 | static tree |
5566b478 MS |
9345 | tsubst_expr_values (t, argvec) |
9346 | tree t, argvec; | |
9347 | { | |
9348 | tree first = NULL_TREE; | |
9349 | tree *p = &first; | |
9350 | ||
9351 | for (; t; t = TREE_CHAIN (t)) | |
9352 | { | |
4393e105 MM |
9353 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, |
9354 | /*complain=*/1, NULL_TREE); | |
9355 | tree val = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1, | |
9356 | NULL_TREE); | |
5566b478 MS |
9357 | *p = build_tree_list (pur, val); |
9358 | p = &TREE_CHAIN (*p); | |
9359 | } | |
9360 | return first; | |
9361 | } | |
9362 | ||
9363 | tree last_tree; | |
9364 | ||
9365 | void | |
9366 | add_tree (t) | |
9367 | tree t; | |
9368 | { | |
9369 | last_tree = TREE_CHAIN (last_tree) = t; | |
9370 | } | |
73aad9b9 | 9371 | |
75650646 MM |
9372 | |
9373 | void | |
9374 | begin_tree () | |
9375 | { | |
9376 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
9377 | last_tree = NULL_TREE; | |
9378 | } | |
9379 | ||
9380 | ||
9381 | void | |
9382 | end_tree () | |
9383 | { | |
9384 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
9385 | ||
9386 | last_tree = TREE_VALUE (saved_trees); | |
9387 | saved_trees = TREE_CHAIN (saved_trees); | |
9388 | } | |
9389 | ||
73aad9b9 JM |
9390 | /* D is an undefined function declaration in the presence of templates with |
9391 | the same name, listed in FNS. If one of them can produce D as an | |
9392 | instantiation, remember this so we can instantiate it at EOF if D has | |
9393 | not been defined by that time. */ | |
9394 | ||
9395 | void | |
9396 | add_maybe_template (d, fns) | |
9397 | tree d, fns; | |
9398 | { | |
9399 | tree t; | |
9400 | ||
9401 | if (DECL_MAYBE_TEMPLATE (d)) | |
9402 | return; | |
9403 | ||
e1467ff2 | 9404 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
9405 | if (! t) |
9406 | return; | |
9407 | if (t == error_mark_node) | |
9408 | { | |
8251199e | 9409 | cp_error ("ambiguous template instantiation for `%D'", d); |
73aad9b9 JM |
9410 | return; |
9411 | } | |
9412 | ||
9413 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
9414 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
9415 | DECL_MAYBE_TEMPLATE (d) = 1; | |
9416 | } | |
b87692e5 | 9417 | |
61a127b3 MM |
9418 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
9419 | ||
9420 | static void | |
9421 | set_current_access_from_decl (decl) | |
9422 | tree decl; | |
9423 | { | |
9424 | if (TREE_PRIVATE (decl)) | |
9425 | current_access_specifier = access_private_node; | |
9426 | else if (TREE_PROTECTED (decl)) | |
9427 | current_access_specifier = access_protected_node; | |
9428 | else | |
9429 | current_access_specifier = access_public_node; | |
9430 | } | |
9431 | ||
dbfe2124 MM |
9432 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
9433 | is the instantiation (which should have been created with | |
9434 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 9435 | |
dbfe2124 MM |
9436 | static void |
9437 | tsubst_enum (tag, newtag, args) | |
9438 | tree tag; | |
9439 | tree newtag; | |
9440 | tree args; | |
b87692e5 | 9441 | { |
dbfe2124 | 9442 | tree e; |
b87692e5 MS |
9443 | |
9444 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
9445 | { | |
61a127b3 MM |
9446 | tree value; |
9447 | tree elt; | |
9448 | ||
9449 | /* Note that in a template enum, the TREE_VALUE is the | |
9450 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
9451 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
4393e105 MM |
9452 | args, /*complain=*/1, |
9453 | NULL_TREE); | |
61a127b3 MM |
9454 | |
9455 | /* Give this enumeration constant the correct access. */ | |
9456 | set_current_access_from_decl (TREE_VALUE (e)); | |
9457 | ||
9458 | /* Actually build the enumerator itself. */ | |
9459 | elt = build_enumerator (TREE_PURPOSE (e), value, newtag); | |
b87692e5 | 9460 | |
dbfe2124 MM |
9461 | /* We save the enumerators we have built so far in the |
9462 | TYPE_VALUES so that if the enumeration constants for | |
9463 | subsequent enumerators involve those for previous ones, | |
9464 | tsubst_copy will be able to find them. */ | |
9465 | TREE_CHAIN (elt) = TYPE_VALUES (newtag); | |
9466 | TYPE_VALUES (newtag) = elt; | |
9467 | } | |
b3d5a58b | 9468 | |
dbfe2124 | 9469 | finish_enum (newtag); |
b87692e5 | 9470 | } |
36a117a5 MM |
9471 | |
9472 | /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that | |
9473 | is either an instantiation or specialization of a template | |
9474 | function. */ | |
9475 | ||
9476 | static void | |
9477 | set_mangled_name_for_template_decl (decl) | |
9478 | tree decl; | |
9479 | { | |
9480 | tree saved_namespace; | |
b370501f | 9481 | tree context = NULL_TREE; |
36a117a5 MM |
9482 | tree fn_type; |
9483 | tree ret_type; | |
9484 | tree parm_types; | |
9485 | tree tparms; | |
9486 | tree targs; | |
9487 | tree tmpl; | |
9488 | int parm_depth; | |
9489 | ||
9490 | my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); | |
9491 | my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0); | |
9492 | ||
9493 | /* The names of template functions must be mangled so as to indicate | |
9494 | what template is being specialized with what template arguments. | |
9495 | For example, each of the following three functions must get | |
9496 | different mangled names: | |
9497 | ||
9498 | void f(int); | |
9499 | template <> void f<7>(int); | |
9500 | template <> void f<8>(int); */ | |
9501 | ||
9502 | targs = DECL_TI_ARGS (decl); | |
9503 | if (uses_template_parms (targs)) | |
9504 | /* This DECL is for a partial instantiation. There's no need to | |
9505 | mangle the name of such an entity. */ | |
9506 | return; | |
9507 | ||
9508 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
9509 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
9510 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
9511 | ||
9512 | /* There should be as many levels of arguments as there are levels | |
9513 | of parameters. */ | |
9514 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
9515 | ||
9516 | /* We now compute the PARMS and RET_TYPE to give to | |
9517 | build_decl_overload_real. The PARMS and RET_TYPE are the | |
9518 | parameter and return types of the template, after all but the | |
9519 | innermost template arguments have been substituted, not the | |
9520 | parameter and return types of the function DECL. For example, | |
9521 | given: | |
9522 | ||
9523 | template <class T> T f(T); | |
9524 | ||
9525 | both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'. | |
9526 | A more subtle example is: | |
9527 | ||
9528 | template <class T> struct S { template <class U> void f(T, U); } | |
9529 | ||
9530 | Here, if DECL is `void S<int>::f(int, double)', PARMS should be | |
9531 | {int, U}. Thus, the args that we want to subsitute into the | |
9532 | return and parameter type for the function are those in TARGS, | |
9533 | with the innermost level omitted. */ | |
9534 | fn_type = TREE_TYPE (tmpl); | |
9535 | if (DECL_STATIC_FUNCTION_P (decl)) | |
61a127b3 | 9536 | context = DECL_CLASS_CONTEXT (decl); |
36a117a5 MM |
9537 | |
9538 | if (parm_depth == 1) | |
9539 | /* No substitution is necessary. */ | |
9540 | ; | |
9541 | else | |
9542 | { | |
9543 | int i; | |
9544 | tree partial_args; | |
9545 | ||
9546 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
9547 | let tsubst know not to subsitute for those parameters. */ | |
9548 | partial_args = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
9549 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
9550 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
9551 | TMPL_ARGS_LEVEL (targs, i)); | |
9552 | SET_TMPL_ARGS_LEVEL (partial_args, | |
9553 | TMPL_ARGS_DEPTH (targs), | |
9554 | make_temp_vec (DECL_NTPARMS (tmpl))); | |
9555 | ||
9556 | /* Now, do the (partial) substitution to figure out the | |
9557 | appropriate function type. */ | |
4393e105 | 9558 | fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 | 9559 | if (DECL_STATIC_FUNCTION_P (decl)) |
4393e105 | 9560 | context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
9561 | |
9562 | /* Substitute into the template parameters to obtain the real | |
9563 | innermost set of parameters. This step is important if the | |
9564 | innermost set of template parameters contains value | |
9565 | parameters whose types depend on outer template parameters. */ | |
9566 | TREE_VEC_LENGTH (partial_args)--; | |
4393e105 | 9567 | tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1); |
36a117a5 MM |
9568 | } |
9569 | ||
9570 | /* Now, get the innermost parameters and arguments, and figure out | |
9571 | the parameter and return types. */ | |
9572 | tparms = INNERMOST_TEMPLATE_PARMS (tparms); | |
9573 | targs = innermost_args (targs); | |
9574 | ret_type = TREE_TYPE (fn_type); | |
9575 | parm_types = TYPE_ARG_TYPES (fn_type); | |
9576 | ||
9577 | /* For a static member function, we generate a fake `this' pointer, | |
9578 | for the purposes of mangling. This indicates of which class the | |
9579 | function is a member. Because of: | |
9580 | ||
9581 | [class.static] | |
9582 | ||
9583 | There shall not be a static and a nonstatic member function | |
9584 | with the same name and the same parameter types | |
9585 | ||
9586 | we don't have to worry that this will result in a clash with a | |
9587 | non-static member function. */ | |
9588 | if (DECL_STATIC_FUNCTION_P (decl)) | |
9589 | parm_types = hash_tree_chain (build_pointer_type (context), parm_types); | |
9590 | ||
9591 | /* There should be the same number of template parameters as | |
9592 | template arguments. */ | |
9593 | my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs), | |
9594 | 0); | |
9595 | ||
9596 | /* If the template is in a namespace, we need to put that into the | |
9597 | mangled name. Unfortunately, build_decl_overload_real does not | |
9598 | get the decl to mangle, so it relies on the current | |
9599 | namespace. Therefore, we set that here temporarily. */ | |
9600 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702); | |
9601 | saved_namespace = current_namespace; | |
9602 | current_namespace = CP_DECL_CONTEXT (decl); | |
9603 | ||
9604 | /* Actually set the DCL_ASSEMBLER_NAME. */ | |
9605 | DECL_ASSEMBLER_NAME (decl) | |
9606 | = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type, | |
9607 | tparms, targs, | |
9608 | DECL_FUNCTION_MEMBER_P (decl) | |
9609 | + DECL_CONSTRUCTOR_P (decl)); | |
9610 | ||
9611 | /* Restore the previously active namespace. */ | |
9612 | current_namespace = saved_namespace; | |
9613 | } |