]>
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)); |
36a117a5 MM |
152 | |
153 | /* We use TREE_VECs to hold template arguments. If there is only one | |
154 | level of template arguments, then the TREE_VEC contains the | |
155 | arguments directly. If there is more than one level of template | |
156 | arguments, then each entry in the TREE_VEC is itself a TREE_VEC, | |
157 | containing the template arguments for a single level. The first | |
158 | entry in the outer TREE_VEC is the outermost level of template | |
159 | parameters; the last is the innermost. | |
160 | ||
161 | It is incorrect to ever form a template argument vector containing | |
162 | only one level of arguments, but which is a TREE_VEC containing as | |
163 | its only entry the TREE_VEC for that level. */ | |
164 | ||
36a117a5 MM |
165 | /* Non-zero if the template arguments is actually a vector of vectors, |
166 | rather than just a vector. */ | |
167 | #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ | |
168 | (NODE != NULL_TREE \ | |
169 | && TREE_CODE (NODE) == TREE_VEC \ | |
170 | && TREE_VEC_LENGTH (NODE) > 0 \ | |
171 | && TREE_VEC_ELT (NODE, 0) != NULL_TREE \ | |
67ffc812 MM |
172 | && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) |
173 | ||
e4a84209 MM |
174 | /* The depth of a template argument vector. When called directly by |
175 | the parser, we use a TREE_LIST rather than a TREE_VEC to represent | |
176 | template arguments. In fact, we may even see NULL_TREE if there | |
177 | are no template arguments. In both of those cases, there is only | |
178 | one level of template arguments. */ | |
179 | #define TMPL_ARGS_DEPTH(NODE) \ | |
180 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1) | |
181 | ||
36a117a5 MM |
182 | /* The LEVELth level of the template ARGS. Note that template |
183 | parameter levels are indexed from 1, not from 0. */ | |
184 | #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ | |
185 | (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ | |
186 | ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS) | |
187 | ||
188 | /* Set the LEVELth level of the template ARGS to VAL. This macro does | |
189 | not work with single-level argument vectors. */ | |
190 | #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \ | |
191 | (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL)) | |
192 | ||
193 | /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */ | |
194 | #define TMPL_ARG(ARGS, LEVEL, IDX) \ | |
195 | (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX)) | |
196 | ||
197 | /* Set the IDXth element in the LEVELth level of ARGS to VAL. This | |
198 | macro does not work with single-level argument vectors. */ | |
199 | #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \ | |
200 | (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL)) | |
201 | ||
e4a84209 MM |
202 | /* Given a single level of template arguments in NODE, return the |
203 | number of arguments. */ | |
204 | #define NUM_TMPL_ARGS(NODE) \ | |
205 | ((NODE) == NULL_TREE ? 0 \ | |
206 | : (TREE_CODE (NODE) == TREE_VEC \ | |
207 | ? TREE_VEC_LENGTH (NODE) : list_length (NODE))) | |
208 | ||
36a117a5 MM |
209 | /* The number of levels of template parameters given by NODE. */ |
210 | #define TMPL_PARMS_DEPTH(NODE) \ | |
211 | (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE))) | |
212 | ||
e1467ff2 MM |
213 | /* Do any processing required when DECL (a member template declaration |
214 | using TEMPLATE_PARAMETERS as its innermost parameter list) is | |
215 | finished. Returns the TEMPLATE_DECL corresponding to DECL, unless | |
216 | it is a specialization, in which case the DECL itself is returned. */ | |
217 | ||
218 | tree | |
61a127b3 | 219 | finish_member_template_decl (decl) |
e1467ff2 MM |
220 | tree decl; |
221 | { | |
93cdc044 JM |
222 | if (decl == NULL_TREE || decl == void_type_node) |
223 | return NULL_TREE; | |
3ddfb0e6 MM |
224 | else if (decl == error_mark_node) |
225 | /* By returning NULL_TREE, the parser will just ignore this | |
226 | declaration. We have already issued the error. */ | |
227 | return NULL_TREE; | |
93cdc044 JM |
228 | else if (TREE_CODE (decl) == TREE_LIST) |
229 | { | |
5f311aec | 230 | /* Assume that the class is the only declspec. */ |
93cdc044 | 231 | decl = TREE_VALUE (decl); |
8d019cef JM |
232 | if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) |
233 | && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) | |
93cdc044 JM |
234 | { |
235 | tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); | |
236 | check_member_template (tmpl); | |
237 | return tmpl; | |
238 | } | |
8d019cef | 239 | return NULL_TREE; |
93cdc044 | 240 | } |
a1da6cba | 241 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 242 | { |
a1da6cba MM |
243 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
244 | { | |
245 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
246 | return DECL_TI_TEMPLATE (decl); | |
247 | } | |
248 | else | |
249 | return decl; | |
250 | } | |
251 | else | |
8251199e | 252 | cp_error ("invalid member template declaration `%D'", decl); |
e1467ff2 | 253 | |
a1da6cba | 254 | return error_mark_node; |
f84b4be9 | 255 | } |
e1467ff2 | 256 | |
f84b4be9 JM |
257 | /* Returns the template nesting level of the indicated class TYPE. |
258 | ||
259 | For example, in: | |
260 | template <class T> | |
261 | struct A | |
262 | { | |
263 | template <class U> | |
264 | struct B {}; | |
265 | }; | |
266 | ||
39c01e4c MM |
267 | A<T>::B<U> has depth two, while A<T> has depth one. |
268 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
269 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
270 | specializations. | |
271 | ||
272 | This function is guaranteed to return 0 if passed NULL_TREE so | |
273 | that, for example, `template_class_depth (current_class_type)' is | |
274 | always safe. */ | |
f84b4be9 | 275 | |
e9659ab0 | 276 | static int |
39c01e4c | 277 | template_class_depth_real (type, count_specializations) |
f84b4be9 | 278 | tree type; |
39c01e4c | 279 | int count_specializations; |
f84b4be9 | 280 | { |
93cdc044 | 281 | int depth; |
f84b4be9 | 282 | |
2c73f9f5 | 283 | for (depth = 0; |
ed44da02 MM |
284 | type && TREE_CODE (type) != NAMESPACE_DECL; |
285 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
286 | ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type)) | |
287 | { | |
288 | if (TREE_CODE (type) != FUNCTION_DECL) | |
289 | { | |
290 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
291 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
292 | && ((count_specializations | |
293 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
294 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) | |
295 | ++depth; | |
296 | } | |
297 | else | |
298 | { | |
299 | if (DECL_TEMPLATE_INFO (type) | |
300 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
301 | && ((count_specializations | |
302 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
303 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
304 | ++depth; | |
305 | } | |
306 | } | |
f84b4be9 JM |
307 | |
308 | return depth; | |
e1467ff2 | 309 | } |
98c1c668 | 310 | |
39c01e4c MM |
311 | /* Returns the template nesting level of the indicated class TYPE. |
312 | Like template_class_depth_real, but instantiations do not count in | |
313 | the depth. */ | |
314 | ||
315 | int | |
316 | template_class_depth (type) | |
317 | tree type; | |
318 | { | |
319 | return template_class_depth_real (type, /*count_specializations=*/0); | |
320 | } | |
321 | ||
cae40af6 JM |
322 | /* Returns 1 if processing DECL as part of do_pending_inlines |
323 | needs us to push template parms. */ | |
324 | ||
325 | static int | |
326 | inline_needs_template_parms (decl) | |
327 | tree decl; | |
328 | { | |
329 | if (! DECL_TEMPLATE_INFO (decl)) | |
330 | return 0; | |
f84b4be9 | 331 | |
36a117a5 | 332 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
333 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
334 | } | |
335 | ||
336 | /* Subroutine of maybe_begin_member_template_processing. | |
337 | Push the template parms in PARMS, starting from LEVELS steps into the | |
338 | chain, and ending at the beginning, since template parms are listed | |
339 | innermost first. */ | |
340 | ||
341 | static void | |
342 | push_inline_template_parms_recursive (parmlist, levels) | |
343 | tree parmlist; | |
344 | int levels; | |
345 | { | |
346 | tree parms = TREE_VALUE (parmlist); | |
347 | int i; | |
348 | ||
349 | if (levels > 1) | |
350 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 351 | |
98c1c668 | 352 | ++processing_template_decl; |
cae40af6 | 353 | current_template_parms |
98c1c668 JM |
354 | = tree_cons (build_int_2 (0, processing_template_decl), |
355 | parms, current_template_parms); | |
cae40af6 JM |
356 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
357 | ||
786b5245 | 358 | pushlevel (0); |
98c1c668 JM |
359 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
360 | { | |
786b5245 MM |
361 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
362 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0); | |
cae40af6 | 363 | |
98c1c668 JM |
364 | switch (TREE_CODE (parm)) |
365 | { | |
786b5245 | 366 | case TYPE_DECL: |
73b0fce8 | 367 | case TEMPLATE_DECL: |
98c1c668 JM |
368 | pushdecl (parm); |
369 | break; | |
786b5245 MM |
370 | |
371 | case PARM_DECL: | |
372 | { | |
fc03edb3 MM |
373 | /* Make a CONST_DECL as is done in process_template_parm. |
374 | It is ugly that we recreate this here; the original | |
375 | version built in process_template_parm is no longer | |
376 | available. */ | |
786b5245 MM |
377 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
378 | TREE_TYPE (parm)); | |
fc03edb3 | 379 | SET_DECL_ARTIFICIAL (decl); |
786b5245 | 380 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
fc03edb3 | 381 | DECL_TEMPLATE_PARM_P (decl) = 1; |
786b5245 MM |
382 | pushdecl (decl); |
383 | } | |
cae40af6 | 384 | break; |
786b5245 | 385 | |
98c1c668 JM |
386 | default: |
387 | my_friendly_abort (0); | |
388 | } | |
389 | } | |
390 | } | |
391 | ||
cae40af6 JM |
392 | /* Restore the template parameter context for a member template or |
393 | a friend template defined in a class definition. */ | |
394 | ||
395 | void | |
396 | maybe_begin_member_template_processing (decl) | |
397 | tree decl; | |
398 | { | |
399 | tree parms; | |
400 | int levels; | |
401 | ||
402 | if (! inline_needs_template_parms (decl)) | |
403 | return; | |
404 | ||
36a117a5 | 405 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); |
cae40af6 | 406 | |
36a117a5 | 407 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; |
cae40af6 JM |
408 | |
409 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) | |
410 | { | |
411 | --levels; | |
412 | parms = TREE_CHAIN (parms); | |
413 | } | |
414 | ||
415 | push_inline_template_parms_recursive (parms, levels); | |
416 | } | |
417 | ||
98c1c668 JM |
418 | /* Undo the effects of begin_member_template_processing. */ |
419 | ||
420 | void | |
b370501f | 421 | maybe_end_member_template_processing () |
98c1c668 JM |
422 | { |
423 | if (! processing_template_decl) | |
424 | return; | |
425 | ||
cae40af6 JM |
426 | while (current_template_parms |
427 | && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
428 | { | |
429 | --processing_template_decl; | |
430 | current_template_parms = TREE_CHAIN (current_template_parms); | |
431 | poplevel (0, 0, 0); | |
432 | } | |
98c1c668 JM |
433 | } |
434 | ||
cae40af6 | 435 | /* Returns non-zero iff T is a member template function. We must be |
f84b4be9 | 436 | careful as in |
75650646 MM |
437 | |
438 | template <class T> class C { void f(); } | |
439 | ||
440 | Here, f is a template function, and a member, but not a member | |
441 | template. This function does not concern itself with the origin of | |
442 | T, only its present state. So if we have | |
443 | ||
444 | template <class T> class C { template <class U> void f(U); } | |
445 | ||
446 | then neither C<int>::f<char> nor C<T>::f<double> is considered | |
f181d4ae MM |
447 | to be a member template. But, `template <class U> void |
448 | C<int>::f(U)' is considered a member template. */ | |
98c1c668 | 449 | |
cae40af6 JM |
450 | int |
451 | is_member_template (t) | |
98c1c668 JM |
452 | tree t; |
453 | { | |
f181d4ae | 454 | if (!DECL_FUNCTION_TEMPLATE_P (t)) |
75650646 | 455 | /* Anything that isn't a function or a template function is |
aa5f3bad MM |
456 | certainly not a member template. */ |
457 | return 0; | |
458 | ||
cae40af6 JM |
459 | /* A local class can't have member templates. */ |
460 | if (hack_decl_function_context (t)) | |
461 | return 0; | |
462 | ||
f181d4ae | 463 | return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) |
f84b4be9 JM |
464 | /* If there are more levels of template parameters than |
465 | there are template classes surrounding the declaration, | |
466 | then we have a member template. */ | |
f181d4ae MM |
467 | && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
468 | template_class_depth (DECL_CLASS_CONTEXT (t)))); | |
f84b4be9 | 469 | } |
98c1c668 | 470 | |
ba4f4e5d | 471 | #if 0 /* UNUSED */ |
2ee4e084 MM |
472 | /* Returns non-zero iff T is a member template class. See |
473 | is_member_template for a description of what precisely constitutes | |
474 | a member template. */ | |
475 | ||
476 | int | |
477 | is_member_template_class (t) | |
478 | tree t; | |
479 | { | |
480 | if (!DECL_CLASS_TEMPLATE_P (t)) | |
481 | /* Anything that isn't a class template, is certainly not a member | |
482 | template. */ | |
483 | return 0; | |
484 | ||
485 | if (!DECL_CLASS_SCOPE_P (t)) | |
486 | /* Anything whose context isn't a class type is surely not a | |
487 | member template. */ | |
488 | return 0; | |
489 | ||
490 | /* If there are more levels of template parameters than there are | |
491 | template classes surrounding the declaration, then we have a | |
492 | member template. */ | |
36a117a5 | 493 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > |
2ee4e084 MM |
494 | template_class_depth (DECL_CONTEXT (t))); |
495 | } | |
ba4f4e5d | 496 | #endif |
2ee4e084 | 497 | |
36a117a5 MM |
498 | /* Return a new template argument vector which contains all of ARGS, |
499 | but has as its innermost set of arguments the EXTRA_ARGS. The | |
500 | resulting vector will be built on a temporary obstack, and so must | |
501 | be explicitly copied to the permanent obstack, if required. */ | |
e6f1275f JM |
502 | |
503 | static tree | |
36a117a5 MM |
504 | add_to_template_args (args, extra_args) |
505 | tree args; | |
506 | tree extra_args; | |
e6f1275f | 507 | { |
36a117a5 MM |
508 | tree new_args; |
509 | int extra_depth; | |
510 | int i; | |
511 | int j; | |
e6f1275f | 512 | |
36a117a5 MM |
513 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
514 | new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth); | |
e6f1275f | 515 | |
36a117a5 MM |
516 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
517 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 518 | |
36a117a5 MM |
519 | for (j = 1; j <= extra_depth; ++j, ++i) |
520 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
521 | ||
e6f1275f JM |
522 | return new_args; |
523 | } | |
524 | ||
36a117a5 MM |
525 | /* Like add_to_template_args, but only the outermost ARGS are added to |
526 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
527 | (EXTRA_ARGS) levels are added. This function is used to combine | |
528 | the template arguments from a partial instantiation with the | |
529 | template arguments used to attain the full instantiation from the | |
530 | partial instantiation. */ | |
98c1c668 | 531 | |
4966381a | 532 | static tree |
36a117a5 | 533 | add_outermost_template_args (args, extra_args) |
98c1c668 JM |
534 | tree args; |
535 | tree extra_args; | |
536 | { | |
537 | tree new_args; | |
538 | ||
e4a84209 MM |
539 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
540 | something very fishy is going on. */ | |
541 | my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), | |
542 | 0); | |
543 | ||
544 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
545 | them. */ | |
546 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
547 | return extra_args; | |
548 | ||
36a117a5 MM |
549 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
550 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
551 | ||
552 | new_args = add_to_template_args (args, extra_args); | |
98c1c668 | 553 | |
36a117a5 MM |
554 | /* Now, we restore ARGS to its full dimensions. */ |
555 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
556 | |
557 | return new_args; | |
558 | } | |
5566b478 MS |
559 | |
560 | /* We've got a template header coming up; push to a new level for storing | |
561 | the parms. */ | |
8d08fdba | 562 | |
8d08fdba MS |
563 | void |
564 | begin_template_parm_list () | |
565 | { | |
6757edfe MM |
566 | /* We use a non-tag-transparent scope here, which causes pushtag to |
567 | put tags in this scope, rather than in the enclosing class or | |
568 | namespace scope. This is the right thing, since we want | |
569 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
570 | global template class, push_template_decl handles putting the | |
571 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
572 | e.g.: | |
573 | ||
574 | template <class T> struct S1 { | |
575 | template <class T> struct S2 {}; | |
576 | }; | |
577 | ||
578 | pushtag contains special code to call pushdecl_with_scope on the | |
579 | TEMPLATE_DECL for S2. */ | |
8d08fdba | 580 | pushlevel (0); |
5566b478 | 581 | declare_pseudo_global_level (); |
5156628f | 582 | ++processing_template_decl; |
67ffc812 | 583 | ++processing_template_parmlist; |
386b8a85 JM |
584 | note_template_header (0); |
585 | } | |
586 | ||
6c30752f MM |
587 | /* This routine is called when a specialization is declared. If it is |
588 | illegal to declare a specialization here, an error is reported. */ | |
589 | ||
e9659ab0 | 590 | static void |
6c30752f MM |
591 | check_specialization_scope () |
592 | { | |
593 | tree scope = current_scope (); | |
3ddfb0e6 | 594 | |
6c30752f MM |
595 | /* [temp.expl.spec] |
596 | ||
597 | An explicit specialization shall be declared in the namespace of | |
598 | which the template is a member, or, for member templates, in the | |
599 | namespace of which the enclosing class or enclosing class | |
600 | template is a member. An explicit specialization of a member | |
601 | function, member class or static data member of a class template | |
602 | shall be declared in the namespace of which the class template | |
603 | is a member. */ | |
604 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
8251199e | 605 | cp_error ("explicit specialization in non-namespace scope `%D'", |
6c30752f | 606 | scope); |
3ddfb0e6 | 607 | |
6c30752f MM |
608 | /* [temp.expl.spec] |
609 | ||
610 | In an explicit specialization declaration for a member of a class | |
611 | template or a member template that appears in namespace scope, | |
612 | the member template and some of its enclosing class templates may | |
613 | remain unspecialized, except that the declaration shall not | |
614 | explicitly specialize a class member template if its enclosing | |
615 | class templates are not explicitly specialized as well. */ | |
616 | if (current_template_parms) | |
fd4de5ff | 617 | cp_error ("enclosing class templates are not explicitly specialized"); |
6c30752f MM |
618 | } |
619 | ||
386b8a85 JM |
620 | /* We've just seen template <>. */ |
621 | ||
622 | void | |
623 | begin_specialization () | |
624 | { | |
625 | note_template_header (1); | |
6c30752f | 626 | check_specialization_scope (); |
386b8a85 JM |
627 | } |
628 | ||
386b8a85 JM |
629 | /* Called at then end of processing a declaration preceeded by |
630 | template<>. */ | |
631 | ||
632 | void | |
633 | end_specialization () | |
634 | { | |
635 | reset_specialization (); | |
636 | } | |
637 | ||
386b8a85 JM |
638 | /* Any template <>'s that we have seen thus far are not referring to a |
639 | function specialization. */ | |
640 | ||
641 | void | |
642 | reset_specialization () | |
643 | { | |
644 | processing_specialization = 0; | |
645 | template_header_count = 0; | |
646 | } | |
647 | ||
386b8a85 JM |
648 | /* We've just seen a template header. If SPECIALIZATION is non-zero, |
649 | it was of the form template <>. */ | |
650 | ||
4966381a | 651 | static void |
386b8a85 JM |
652 | note_template_header (specialization) |
653 | int specialization; | |
654 | { | |
655 | processing_specialization = specialization; | |
656 | template_header_count++; | |
657 | } | |
658 | ||
75650646 | 659 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 660 | |
75650646 MM |
661 | void |
662 | begin_explicit_instantiation () | |
386b8a85 | 663 | { |
75650646 MM |
664 | ++processing_explicit_instantiation; |
665 | } | |
386b8a85 | 666 | |
386b8a85 | 667 | |
75650646 MM |
668 | void |
669 | end_explicit_instantiation () | |
670 | { | |
671 | my_friendly_assert(processing_explicit_instantiation > 0, 0); | |
672 | --processing_explicit_instantiation; | |
673 | } | |
386b8a85 | 674 | |
36a117a5 MM |
675 | /* The TYPE is being declared. If it is a template type, that means it |
676 | is a partial specialization. Do appropriate error-checking. */ | |
677 | ||
678 | void | |
679 | maybe_process_partial_specialization (type) | |
680 | tree type; | |
681 | { | |
682 | if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type)) | |
683 | { | |
684 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) | |
685 | && TYPE_SIZE (type) == NULL_TREE) | |
686 | { | |
700466c2 JM |
687 | if (current_namespace |
688 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
689 | { | |
690 | cp_pedwarn ("specializing `%#T' in different namespace", type); | |
691 | cp_pedwarn_at (" from definition of `%#D'", | |
692 | CLASSTYPE_TI_TEMPLATE (type)); | |
693 | } | |
36a117a5 MM |
694 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
695 | if (processing_template_decl) | |
696 | push_template_decl (TYPE_MAIN_DECL (type)); | |
697 | } | |
698 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
8251199e | 699 | cp_error ("specialization of `%T' after instantiation", type); |
36a117a5 | 700 | } |
20496fa2 JM |
701 | else if (processing_specialization) |
702 | cp_error ("explicit specialization of non-template `%T'", type); | |
36a117a5 MM |
703 | } |
704 | ||
75650646 MM |
705 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
706 | specialization is either an instantiation or an explicit | |
707 | specialization) of TMPL for the given template ARGS. If there is | |
708 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
709 | arguments, or a vector of vectors of arguments, in the case of | |
710 | templates with more than one level of parameters. */ | |
711 | ||
712 | static tree | |
713 | retrieve_specialization (tmpl, args) | |
714 | tree tmpl; | |
715 | tree args; | |
716 | { | |
717 | tree s; | |
718 | ||
719 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
720 | ||
36a117a5 MM |
721 | /* There should be as many levels of arguments as there are |
722 | levels of parameters. */ | |
723 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
724 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
725 | 0); | |
726 | ||
75650646 MM |
727 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
728 | s != NULL_TREE; | |
729 | s = TREE_CHAIN (s)) | |
730 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
731 | return TREE_VALUE (s); | |
732 | ||
733 | return NULL_TREE; | |
386b8a85 JM |
734 | } |
735 | ||
6757edfe MM |
736 | /* Returns non-zero iff DECL is a specialization of TMPL. */ |
737 | ||
738 | int | |
739 | is_specialization_of (decl, tmpl) | |
740 | tree decl; | |
741 | tree tmpl; | |
742 | { | |
743 | tree t; | |
744 | ||
745 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
746 | { | |
747 | for (t = decl; | |
748 | t != NULL_TREE; | |
749 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
750 | if (t == tmpl) | |
751 | return 1; | |
752 | } | |
753 | else | |
754 | { | |
755 | my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); | |
756 | ||
757 | for (t = TREE_TYPE (decl); | |
758 | t != NULL_TREE; | |
759 | t = CLASSTYPE_USE_TEMPLATE (t) | |
760 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
3bfdc719 MM |
761 | if (same_type_p (TYPE_MAIN_VARIANT (t), |
762 | TYPE_MAIN_VARIANT (TREE_TYPE (tmpl)))) | |
6757edfe MM |
763 | return 1; |
764 | } | |
386b8a85 | 765 | |
6757edfe MM |
766 | return 0; |
767 | } | |
75650646 MM |
768 | |
769 | /* Register the specialization SPEC as a specialization of TMPL with | |
36a117a5 MM |
770 | the indicated ARGS. Returns SPEC, or an equivalent prior |
771 | declaration, if available. */ | |
75650646 | 772 | |
36a117a5 | 773 | static tree |
75650646 MM |
774 | register_specialization (spec, tmpl, args) |
775 | tree spec; | |
776 | tree tmpl; | |
777 | tree args; | |
778 | { | |
779 | tree s; | |
780 | ||
781 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
782 | ||
36a117a5 MM |
783 | if (TREE_CODE (spec) == FUNCTION_DECL |
784 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
785 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
786 | register it; we want the corresponding TEMPLATE_DECL instead. | |
787 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
788 | the more obvious `uses_template_parms (spec)' to avoid problems | |
789 | with default function arguments. In particular, given | |
790 | something like this: | |
791 | ||
792 | template <class T> void f(T t1, T t = T()) | |
793 | ||
794 | the default argument expression is not substituted for in an | |
795 | instantiation unless and until it is actually needed. */ | |
796 | return spec; | |
75650646 | 797 | |
36a117a5 MM |
798 | /* There should be as many levels of arguments as there are |
799 | levels of parameters. */ | |
800 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
801 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
802 | 0); | |
803 | ||
75650646 MM |
804 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
805 | s != NULL_TREE; | |
806 | s = TREE_CHAIN (s)) | |
807 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
808 | { | |
809 | tree fn = TREE_VALUE (s); | |
810 | ||
811 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
812 | { | |
813 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
814 | { | |
815 | if (TREE_USED (fn) | |
816 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
817 | { | |
8251199e | 818 | cp_error ("specialization of %D after instantiation", |
75650646 | 819 | fn); |
36a117a5 | 820 | return spec; |
75650646 MM |
821 | } |
822 | else | |
823 | { | |
824 | /* This situation should occur only if the first | |
825 | specialization is an implicit instantiation, | |
826 | the second is an explicit specialization, and | |
827 | the implicit instantiation has not yet been | |
828 | used. That situation can occur if we have | |
36a117a5 MM |
829 | implicitly instantiated a member function and |
830 | then specialized it later. | |
831 | ||
832 | We can also wind up here if a friend | |
833 | declaration that looked like an instantiation | |
834 | turns out to be a specialization: | |
835 | ||
836 | template <class T> void foo(T); | |
837 | class S { friend void foo<>(int) }; | |
838 | template <> void foo(int); | |
839 | ||
840 | We transform the existing DECL in place so that | |
841 | any pointers to it become pointers to the | |
fbf1c34b MM |
842 | updated declaration. |
843 | ||
844 | If there was a definition for the template, but | |
845 | not for the specialization, we want this to | |
846 | look as if there is no definition, and vice | |
847 | versa. */ | |
848 | DECL_INITIAL (fn) = NULL_TREE; | |
849 | duplicate_decls (spec, fn); | |
850 | ||
851 | return fn; | |
75650646 MM |
852 | } |
853 | } | |
854 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
855 | { | |
fbf1c34b MM |
856 | duplicate_decls (spec, fn); |
857 | return fn; | |
75650646 MM |
858 | } |
859 | } | |
860 | } | |
861 | ||
862 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
863 | = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
864 | |
865 | return spec; | |
866 | } | |
867 | ||
868 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
869 | Returns nonzero if the SPEC was listed as a specialization of | |
870 | TMPL. */ | |
871 | ||
872 | static int | |
873 | unregister_specialization (spec, tmpl) | |
874 | tree spec; | |
875 | tree tmpl; | |
876 | { | |
877 | tree* s; | |
878 | ||
879 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
880 | *s != NULL_TREE; | |
881 | s = &TREE_CHAIN (*s)) | |
882 | if (TREE_VALUE (*s) == spec) | |
883 | { | |
884 | *s = TREE_CHAIN (*s); | |
885 | return 1; | |
886 | } | |
887 | ||
888 | return 0; | |
75650646 MM |
889 | } |
890 | ||
e1467ff2 MM |
891 | /* Print the list of candidate FNS in an error message. */ |
892 | ||
104bf76a | 893 | void |
e1467ff2 MM |
894 | print_candidates (fns) |
895 | tree fns; | |
896 | { | |
897 | tree fn; | |
898 | ||
899 | char* str = "candidates are:"; | |
900 | ||
901 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
902 | { | |
8251199e | 903 | cp_error_at ("%s %+#D", str, TREE_VALUE (fn)); |
e1467ff2 MM |
904 | str = " "; |
905 | } | |
906 | } | |
907 | ||
75650646 | 908 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 909 | which can be specialized to match the indicated DECL with the |
03017874 MM |
910 | explicit template args given in TEMPLATE_ID. The DECL may be |
911 | NULL_TREE if none is available. In that case, the functions in | |
912 | TEMPLATE_ID are non-members. | |
913 | ||
914 | If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a | |
915 | specialization of a member template. | |
916 | ||
917 | The template args (those explicitly specified and those deduced) | |
918 | are output in a newly created vector *TARGS_OUT. | |
919 | ||
920 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 921 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 922 | |
e9659ab0 | 923 | static tree |
e1467ff2 | 924 | determine_specialization (template_id, decl, targs_out, |
bf8f3f93 | 925 | need_member_template) |
386b8a85 | 926 | tree template_id; |
e1467ff2 | 927 | tree decl; |
386b8a85 JM |
928 | tree* targs_out; |
929 | int need_member_template; | |
386b8a85 | 930 | { |
386b8a85 | 931 | tree fn; |
03017874 MM |
932 | tree fns; |
933 | tree targs; | |
934 | tree explicit_targs; | |
935 | tree candidates = NULL_TREE; | |
936 | tree templates = NULL_TREE; | |
386b8a85 | 937 | |
e1467ff2 MM |
938 | *targs_out = NULL_TREE; |
939 | ||
f2e48b67 BK |
940 | if (template_id == error_mark_node) |
941 | return error_mark_node; | |
942 | ||
943 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 944 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 945 | |
27fafc8d JM |
946 | if (fns == error_mark_node) |
947 | return error_mark_node; | |
948 | ||
2c73f9f5 ML |
949 | /* Check for baselinks. */ |
950 | if (TREE_CODE (fns) == TREE_LIST) | |
951 | fns = TREE_VALUE (fns); | |
386b8a85 | 952 | |
2c73f9f5 | 953 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 954 | { |
75650646 MM |
955 | tree tmpl; |
956 | ||
2c73f9f5 | 957 | fn = OVL_CURRENT (fns); |
03017874 MM |
958 | |
959 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
960 | /* DECL might be a specialization of FN. */ | |
961 | tmpl = fn; | |
962 | else if (need_member_template) | |
963 | /* FN is an ordinary member function, and we need a | |
964 | specialization of a member template. */ | |
965 | continue; | |
966 | else if (TREE_CODE (fn) != FUNCTION_DECL) | |
967 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
968 | cases. */ | |
969 | continue; | |
970 | else if (!DECL_FUNCTION_MEMBER_P (fn)) | |
971 | /* This is just an ordinary non-member function. Nothing can | |
972 | be a specialization of that. */ | |
973 | continue; | |
75650646 | 974 | else |
03017874 MM |
975 | { |
976 | tree decl_arg_types; | |
386b8a85 | 977 | |
03017874 MM |
978 | /* This is an ordinary member function. However, since |
979 | we're here, we can assume it's enclosing class is a | |
980 | template class. For example, | |
981 | ||
982 | template <typename T> struct S { void f(); }; | |
983 | template <> void S<int>::f() {} | |
984 | ||
985 | Here, S<int>::f is a non-template, but S<int> is a | |
986 | template class. If FN has the same type as DECL, we | |
987 | might be in business. */ | |
988 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), | |
989 | TREE_TYPE (TREE_TYPE (fn)))) | |
990 | /* The return types differ. */ | |
991 | continue; | |
992 | ||
993 | /* Adjust the type of DECL in case FN is a static member. */ | |
994 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
995 | if (DECL_STATIC_FUNCTION_P (fn) | |
996 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
997 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
998 | ||
999 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1000 | decl_arg_types)) | |
1001 | /* They match! */ | |
1002 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1003 | ||
1004 | continue; | |
1005 | } | |
386b8a85 | 1006 | |
bf8f3f93 MM |
1007 | /* See whether this function might be a specialization of this |
1008 | template. */ | |
1009 | targs = get_bindings (tmpl, decl, explicit_targs); | |
03017874 MM |
1010 | |
1011 | if (!targs) | |
bf8f3f93 | 1012 | /* We cannot deduce template arguments that when used to |
03017874 MM |
1013 | specialize TMPL will produce DECL. */ |
1014 | continue; | |
1015 | ||
1016 | /* Save this template, and the arguments deduced. */ | |
1017 | templates = scratch_tree_cons (targs, tmpl, templates); | |
386b8a85 | 1018 | } |
03017874 | 1019 | |
bf8f3f93 | 1020 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1021 | { |
03017874 MM |
1022 | /* We have: |
1023 | ||
1024 | [temp.expl.spec] | |
1025 | ||
1026 | It is possible for a specialization with a given function | |
1027 | signature to be instantiated from more than one function | |
1028 | template. In such cases, explicit specification of the | |
1029 | template arguments must be used to uniquely identify the | |
1030 | function template specialization being specialized. | |
1031 | ||
1032 | Note that here, there's no suggestion that we're supposed to | |
1033 | determine which of the candidate templates is most | |
1034 | specialized. However, we, also have: | |
1035 | ||
1036 | [temp.func.order] | |
1037 | ||
1038 | Partial ordering of overloaded function template | |
1039 | declarations is used in the following contexts to select | |
1040 | the function template to which a function template | |
1041 | specialization refers: | |
1042 | ||
1043 | -- when an explicit specialization refers to a function | |
1044 | template. | |
1045 | ||
1046 | So, we do use the partial ordering rules, at least for now. | |
1047 | This extension can only serve to make illegal programs legal, | |
1048 | so it's safe. And, there is strong anecdotal evidence that | |
1049 | the committee intended the partial ordering rules to apply; | |
1050 | the EDG front-end has that behavior, and John Spicer claims | |
1051 | that the committee simply forgot to delete the wording in | |
1052 | [temp.expl.spec]. */ | |
1053 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1054 | if (tmpl && tmpl != error_mark_node) | |
1055 | { | |
1056 | targs = get_bindings (tmpl, decl, explicit_targs); | |
1057 | templates = scratch_tree_cons (targs, tmpl, NULL_TREE); | |
1058 | } | |
e1467ff2 MM |
1059 | } |
1060 | ||
03017874 | 1061 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1062 | { |
bf8f3f93 MM |
1063 | cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", |
1064 | template_id, decl); | |
03017874 | 1065 | return error_mark_node; |
386b8a85 | 1066 | } |
03017874 | 1067 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1068 | || (candidates && TREE_CHAIN (candidates)) |
1069 | || (templates && candidates)) | |
386b8a85 | 1070 | { |
bf8f3f93 MM |
1071 | cp_error_at ("ambiguous template specialization `%D' for `%+D'", |
1072 | template_id, decl); | |
1073 | chainon (candidates, templates); | |
1074 | print_candidates (candidates); | |
03017874 | 1075 | return error_mark_node; |
386b8a85 JM |
1076 | } |
1077 | ||
1078 | /* We have one, and exactly one, match. */ | |
03017874 MM |
1079 | if (candidates) |
1080 | { | |
1081 | /* It was a specialization of an ordinary member function in a | |
1082 | template class. */ | |
1083 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1084 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1085 | } | |
1086 | ||
1087 | /* It was a specialization of a template. */ | |
1088 | targs = DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates))); | |
1089 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) | |
1090 | { | |
1091 | *targs_out = copy_node (targs); | |
1092 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1093 | TMPL_ARGS_DEPTH (*targs_out), | |
1094 | TREE_PURPOSE (templates)); | |
1095 | } | |
1096 | else | |
1097 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1098 | return TREE_VALUE (templates); |
8d08fdba | 1099 | } |
6757edfe | 1100 | |
386b8a85 | 1101 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1102 | DECLARATOR, and in DECL, is a specialization of a function |
1103 | template. We may also discover that the declaration is an explicit | |
1104 | instantiation at this point. | |
1105 | ||
e1467ff2 | 1106 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1107 | instead if all goes well. Issues an error message if something is |
1108 | amiss. Returns error_mark_node if the error is not easily | |
1109 | recoverable. | |
75650646 | 1110 | |
75650646 MM |
1111 | FLAGS is a bitmask consisting of the following flags: |
1112 | ||
75650646 MM |
1113 | 2: The function has a definition. |
1114 | 4: The function is a friend. | |
75650646 MM |
1115 | |
1116 | The TEMPLATE_COUNT is the number of references to qualifying | |
1117 | template classes that appeared in the name of the function. For | |
1118 | example, in | |
1119 | ||
1120 | template <class T> struct S { void f(); }; | |
1121 | void S<int>::f(); | |
1122 | ||
1123 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1124 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1125 | ||
1126 | template <class T> struct S {}; | |
1127 | template <> struct S<int> { void f(); } | |
36a117a5 | 1128 | template <> void S<int>::f(); |
75650646 MM |
1129 | |
1130 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
1131 | illegal; there should be no template <>.) | |
1132 | ||
1133 | If the function is a specialization, it is marked as such via | |
1134 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1135 | is set up correctly, and it is added to the list of specializations | |
1136 | for that template. */ | |
386b8a85 | 1137 | |
e1467ff2 | 1138 | tree |
27bb8339 | 1139 | check_explicit_specialization (declarator, decl, template_count, flags) |
386b8a85 JM |
1140 | tree declarator; |
1141 | tree decl; | |
1142 | int template_count; | |
1143 | int flags; | |
1144 | { | |
75650646 MM |
1145 | int have_def = flags & 2; |
1146 | int is_friend = flags & 4; | |
1147 | int specialization = 0; | |
e1467ff2 | 1148 | int explicit_instantiation = 0; |
fd4de5ff | 1149 | int member_specialization = 0; |
75650646 MM |
1150 | |
1151 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
1152 | tree dname = DECL_NAME (decl); | |
386b8a85 | 1153 | |
fd4de5ff | 1154 | if (processing_specialization) |
386b8a85 | 1155 | { |
fd4de5ff | 1156 | /* The last template header was of the form template <>. */ |
75650646 | 1157 | |
fd4de5ff MM |
1158 | if (template_header_count > template_count) |
1159 | { | |
1160 | /* There were more template headers than qualifying template | |
1161 | classes. */ | |
1162 | if (template_header_count - template_count > 1) | |
03017874 MM |
1163 | /* There shouldn't be that many template parameter lists. |
1164 | There can be at most one parameter list for every | |
1165 | qualifying class, plus one for the function itself. */ | |
fd4de5ff | 1166 | cp_error ("too many template parameter lists in declaration of `%D'", decl); |
75650646 | 1167 | |
fd4de5ff MM |
1168 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
1169 | if (ctype) | |
1170 | member_specialization = 1; | |
1171 | else | |
1172 | specialization = 1; | |
75650646 | 1173 | } |
fd4de5ff | 1174 | else if (template_header_count == template_count) |
75650646 | 1175 | { |
fd4de5ff MM |
1176 | /* The counts are equal. So, this might be a |
1177 | specialization, but it is not a specialization of a | |
1178 | member template. It might be something like | |
1179 | ||
1180 | template <class T> struct S { | |
1181 | void f(int i); | |
1182 | }; | |
1183 | template <> | |
1184 | void S<int>::f(int i) {} */ | |
1185 | specialization = 1; | |
1186 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1187 | } |
fd4de5ff | 1188 | else |
386b8a85 | 1189 | { |
fd4de5ff MM |
1190 | /* This cannot be an explicit specialization. There are not |
1191 | enough headers for all of the qualifying classes. For | |
1192 | example, we might have: | |
1193 | ||
1194 | template <> | |
1195 | void S<int>::T<char>::f(); | |
75650646 | 1196 | |
fd4de5ff MM |
1197 | But, we're missing another template <>. */ |
1198 | cp_error("too few template parameter lists in declaration of `%D'", decl); | |
1199 | return decl; | |
1200 | } | |
1201 | } | |
1202 | else if (processing_explicit_instantiation) | |
1203 | { | |
1204 | if (template_header_count) | |
1205 | cp_error ("template parameter list used in explicit instantiation"); | |
1206 | ||
1207 | if (have_def) | |
1208 | cp_error ("definition provided for explicit instantiation"); | |
75650646 | 1209 | |
fd4de5ff MM |
1210 | explicit_instantiation = 1; |
1211 | } | |
1212 | else if (ctype != NULL_TREE | |
1213 | && !TYPE_BEING_DEFINED (ctype) | |
1214 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) | |
1215 | && !is_friend) | |
1216 | { | |
1217 | /* This case catches outdated code that looks like this: | |
1218 | ||
1219 | template <class T> struct S { void f(); }; | |
1220 | void S<int>::f() {} // Missing template <> | |
1221 | ||
1222 | We disable this check when the type is being defined to | |
1223 | avoid complaining about default compiler-generated | |
1224 | constructors, destructors, and assignment operators. | |
1225 | Since the type is an instantiation, not a specialization, | |
1226 | these are the only functions that can be defined before | |
1227 | the class is complete. */ | |
75650646 | 1228 | |
c6f2ed0d JM |
1229 | /* If they said |
1230 | template <class T> void S<int>::f() {} | |
1231 | that's bogus. */ | |
fd4de5ff MM |
1232 | if (template_header_count) |
1233 | { | |
1234 | cp_error ("template parameters specified in specialization"); | |
1235 | return decl; | |
c6f2ed0d | 1236 | } |
fd4de5ff MM |
1237 | |
1238 | if (pedantic) | |
1239 | cp_pedwarn | |
1240 | ("explicit specialization not preceded by `template <>'"); | |
1241 | specialization = 1; | |
1242 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1243 | } | |
1244 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
1245 | { | |
1246 | if (is_friend) | |
1247 | /* This could be something like: | |
1248 | ||
1249 | template <class T> void f(T); | |
1250 | class S { friend void f<>(int); } */ | |
1251 | specialization = 1; | |
1252 | else | |
c6f2ed0d | 1253 | { |
fd4de5ff MM |
1254 | /* This case handles bogus declarations like template <> |
1255 | template <class T> void f<int>(); */ | |
75650646 | 1256 | |
fd4de5ff MM |
1257 | cp_error ("template-id `%D' in declaration of primary template", |
1258 | declarator); | |
1259 | return decl; | |
386b8a85 | 1260 | } |
75650646 | 1261 | } |
386b8a85 | 1262 | |
670960ac JM |
1263 | if (specialization || member_specialization) |
1264 | { | |
1265 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1266 | for (; t; t = TREE_CHAIN (t)) | |
1267 | if (TREE_PURPOSE (t)) | |
1268 | { | |
8251199e JM |
1269 | cp_pedwarn |
1270 | ("default argument specified in explicit specialization"); | |
670960ac JM |
1271 | break; |
1272 | } | |
f3400fe2 JM |
1273 | if (current_lang_name == lang_name_c) |
1274 | cp_error ("template specialization with C linkage"); | |
670960ac JM |
1275 | } |
1276 | ||
e1467ff2 | 1277 | if (specialization || member_specialization || explicit_instantiation) |
75650646 | 1278 | { |
36a117a5 | 1279 | tree gen_tmpl; |
75650646 MM |
1280 | tree tmpl = NULL_TREE; |
1281 | tree targs = NULL_TREE; | |
75650646 MM |
1282 | |
1283 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1284 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1285 | { | |
1286 | tree fns; | |
1287 | ||
1288 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1289 | 0); | |
1290 | if (!ctype) | |
30394414 | 1291 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); |
386b8a85 JM |
1292 | else |
1293 | fns = dname; | |
1294 | ||
75650646 MM |
1295 | declarator = |
1296 | lookup_template_function (fns, NULL_TREE); | |
386b8a85 JM |
1297 | } |
1298 | ||
f2e48b67 BK |
1299 | if (declarator == error_mark_node) |
1300 | return error_mark_node; | |
1301 | ||
1302 | if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) | |
386b8a85 JM |
1303 | { |
1304 | /* A friend declaration. We can't do much, because we don't | |
1305 | know what this resolves to, yet. */ | |
1306 | my_friendly_assert (is_friend != 0, 0); | |
e1467ff2 | 1307 | my_friendly_assert (!explicit_instantiation, 0); |
386b8a85 | 1308 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
e1467ff2 | 1309 | return decl; |
386b8a85 JM |
1310 | } |
1311 | ||
75650646 MM |
1312 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1313 | { | |
8ec2ac34 | 1314 | if (!explicit_instantiation) |
6c30752f MM |
1315 | /* A specialization in class scope. This is illegal, |
1316 | but the error will already have been flagged by | |
1317 | check_specialization_scope. */ | |
1318 | return error_mark_node; | |
8ec2ac34 | 1319 | else |
b370501f KG |
1320 | { |
1321 | /* It's not legal to write an explicit instantiation in | |
1322 | class scope, e.g.: | |
8ec2ac34 | 1323 | |
b370501f | 1324 | class C { template void f(); } |
8ec2ac34 | 1325 | |
b370501f KG |
1326 | This case is caught by the parser. However, on |
1327 | something like: | |
8ec2ac34 | 1328 | |
b370501f | 1329 | template class C { void f(); }; |
8ec2ac34 | 1330 | |
b370501f KG |
1331 | (which is illegal) we can get here. The error will be |
1332 | issued later. */ | |
1333 | ; | |
1334 | } | |
8ec2ac34 | 1335 | |
e1467ff2 | 1336 | return decl; |
75650646 MM |
1337 | } |
1338 | else if (ctype != NULL_TREE | |
1339 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1340 | IDENTIFIER_NODE)) | |
386b8a85 | 1341 | { |
75650646 MM |
1342 | /* Find the list of functions in ctype that have the same |
1343 | name as the declared function. */ | |
1344 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
1345 | tree fns = NULL_TREE; |
1346 | int idx; | |
1347 | ||
75650646 MM |
1348 | if (name == constructor_name (ctype) |
1349 | || name == constructor_name_full (ctype)) | |
386b8a85 | 1350 | { |
75650646 MM |
1351 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1352 | ||
1353 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1354 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1355 | { | |
1356 | /* From [temp.expl.spec]: | |
e1467ff2 | 1357 | |
75650646 MM |
1358 | If such an explicit specialization for the member |
1359 | of a class template names an implicitly-declared | |
1360 | special member function (clause _special_), the | |
e1467ff2 MM |
1361 | program is ill-formed. |
1362 | ||
1363 | Similar language is found in [temp.explicit]. */ | |
8251199e | 1364 | cp_error ("specialization of implicitly-declared special member function"); |
03017874 | 1365 | return error_mark_node; |
75650646 | 1366 | } |
386b8a85 | 1367 | |
42da2fd8 | 1368 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1369 | } |
42da2fd8 | 1370 | |
03017874 MM |
1371 | if (!IDENTIFIER_TYPENAME_P (name)) |
1372 | { | |
1373 | idx = lookup_fnfields_1 (ctype, name); | |
1374 | if (idx >= 0) | |
1375 | fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); | |
1376 | } | |
1377 | else | |
1378 | { | |
1379 | tree methods; | |
1380 | ||
1381 | /* For a type-conversion operator, we cannot do a | |
1382 | name-based lookup. We might be looking for `operator | |
1383 | int' which will be a specialization of `operator T'. | |
1384 | So, we find *all* the conversion operators, and then | |
1385 | select from them. */ | |
1386 | fns = NULL_TREE; | |
1387 | ||
1388 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1389 | if (methods) | |
1390 | for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx) | |
1391 | { | |
1392 | tree ovl = TREE_VEC_ELT (methods, idx); | |
1393 | ||
1394 | if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) | |
1395 | /* There are no more conversion functions. */ | |
1396 | break; | |
1397 | ||
1398 | /* Glue all these conversion functions together | |
1399 | with those we already have. */ | |
1400 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1401 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1402 | } | |
1403 | } | |
1404 | ||
386b8a85 JM |
1405 | if (fns == NULL_TREE) |
1406 | { | |
8251199e | 1407 | cp_error ("no member function `%s' declared in `%T'", |
75650646 | 1408 | IDENTIFIER_POINTER (name), |
386b8a85 | 1409 | ctype); |
03017874 | 1410 | return error_mark_node; |
386b8a85 JM |
1411 | } |
1412 | else | |
1413 | TREE_OPERAND (declarator, 0) = fns; | |
1414 | } | |
75650646 | 1415 | |
e1467ff2 MM |
1416 | /* Figure out what exactly is being specialized at this point. |
1417 | Note that for an explicit instantiation, even one for a | |
38e01259 | 1418 | member function, we cannot tell apriori whether the |
e1467ff2 | 1419 | instantiation is for a member template, or just a member |
36a117a5 MM |
1420 | function of a template class. Even if a member template is |
1421 | being instantiated, the member template arguments may be | |
1422 | elided if they can be deduced from the rest of the | |
1423 | declaration. */ | |
e1467ff2 MM |
1424 | tmpl = determine_specialization (declarator, decl, |
1425 | &targs, | |
bf8f3f93 | 1426 | member_specialization); |
386b8a85 | 1427 | |
03017874 MM |
1428 | if (!tmpl || tmpl == error_mark_node) |
1429 | /* We couldn't figure out what this declaration was | |
1430 | specializing. */ | |
1431 | return error_mark_node; | |
1432 | else | |
386b8a85 | 1433 | { |
36a117a5 MM |
1434 | gen_tmpl = most_general_template (tmpl); |
1435 | ||
e1467ff2 MM |
1436 | if (explicit_instantiation) |
1437 | { | |
03d0f4af MM |
1438 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
1439 | is done by do_decl_instantiation later. */ | |
36a117a5 | 1440 | decl = instantiate_template (tmpl, innermost_args (targs)); |
e1467ff2 MM |
1441 | return decl; |
1442 | } | |
36a117a5 MM |
1443 | |
1444 | /* If we though that the DECL was a member function, but it | |
1445 | turns out to be specializing a static member function, | |
1446 | make DECL a static member function as well. */ | |
1447 | if (DECL_STATIC_FUNCTION_P (tmpl) | |
1448 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
366c0f1e MM |
1449 | { |
1450 | revert_static_member_fn (&decl, 0, 0); | |
1451 | last_function_parms = TREE_CHAIN (last_function_parms); | |
1452 | } | |
e1467ff2 | 1453 | |
36a117a5 MM |
1454 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
1455 | DECL_TEMPLATE_INFO (decl) | |
1456 | = perm_tree_cons (tmpl, targs, NULL_TREE); | |
1457 | ||
e1467ff2 MM |
1458 | /* Mangle the function name appropriately. Note that we do |
1459 | not mangle specializations of non-template member | |
1460 | functions of template classes, e.g. with | |
36a117a5 | 1461 | |
e1467ff2 | 1462 | template <class T> struct S { void f(); } |
36a117a5 | 1463 | |
e1467ff2 | 1464 | and given the specialization |
36a117a5 | 1465 | |
e1467ff2 | 1466 | template <> void S<int>::f() {} |
36a117a5 | 1467 | |
e1467ff2 MM |
1468 | we do not mangle S<int>::f() here. That's because it's |
1469 | just an ordinary member function and doesn't need special | |
36a117a5 MM |
1470 | treatment. We do this here so that the ordinary, |
1471 | non-template, name-mangling algorith will not be used | |
1472 | later. */ | |
e1467ff2 | 1473 | if ((is_member_template (tmpl) || ctype == NULL_TREE) |
75650646 | 1474 | && name_mangling_version >= 1) |
36a117a5 | 1475 | set_mangled_name_for_template_decl (decl); |
386b8a85 JM |
1476 | |
1477 | if (is_friend && !have_def) | |
36a117a5 MM |
1478 | /* This is not really a declaration of a specialization. |
1479 | It's just the name of an instantiation. But, it's not | |
1480 | a request for an instantiation, either. */ | |
fbf1c34b | 1481 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
75650646 | 1482 | |
36a117a5 MM |
1483 | /* Register this specialization so that we can find it |
1484 | again. */ | |
1485 | decl = register_specialization (decl, gen_tmpl, targs); | |
386b8a85 JM |
1486 | } |
1487 | } | |
75650646 | 1488 | |
e1467ff2 | 1489 | return decl; |
386b8a85 | 1490 | } |
75650646 | 1491 | |
39c01e4c MM |
1492 | /* TYPE is being declared. Verify that the use of template headers |
1493 | and such is reasonable. Issue error messages if not. */ | |
1494 | ||
1495 | void | |
1496 | maybe_check_template_type (type) | |
1497 | tree type; | |
1498 | { | |
1499 | if (template_header_count) | |
1500 | { | |
1501 | /* We are in the scope of some `template <...>' header. */ | |
1502 | ||
1503 | int context_depth | |
1504 | = template_class_depth_real (TYPE_CONTEXT (type), | |
1505 | /*count_specializations=*/1); | |
1506 | ||
1507 | if (template_header_count <= context_depth) | |
1508 | /* This is OK; the template headers are for the context. We | |
1509 | are actually too lenient here; like | |
1510 | check_explicit_specialization we should consider the number | |
1511 | of template types included in the actual declaration. For | |
1512 | example, | |
1513 | ||
1514 | template <class T> struct S { | |
1515 | template <class U> template <class V> | |
1516 | struct I {}; | |
1517 | }; | |
1518 | ||
1519 | is illegal, but: | |
1520 | ||
1521 | template <class T> struct S { | |
1522 | template <class U> struct I; | |
1523 | }; | |
1524 | ||
1525 | template <class T> template <class U. | |
1526 | struct S<T>::I {}; | |
1527 | ||
1528 | is not. */ | |
1529 | ; | |
1530 | else if (template_header_count > context_depth + 1) | |
1531 | /* There are two many template parameter lists. */ | |
8251199e | 1532 | cp_error ("too many template parameter lists in declaration of `%T'", type); |
39c01e4c MM |
1533 | } |
1534 | } | |
1535 | ||
75650646 MM |
1536 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
1537 | parameters. These are represented in the same format used for | |
1538 | DECL_TEMPLATE_PARMS. */ | |
1539 | ||
1540 | int comp_template_parms (parms1, parms2) | |
1541 | tree parms1; | |
1542 | tree parms2; | |
1543 | { | |
1544 | tree p1; | |
1545 | tree p2; | |
1546 | ||
1547 | if (parms1 == parms2) | |
1548 | return 1; | |
1549 | ||
1550 | for (p1 = parms1, p2 = parms2; | |
1551 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1552 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1553 | { | |
1554 | tree t1 = TREE_VALUE (p1); | |
1555 | tree t2 = TREE_VALUE (p2); | |
1556 | int i; | |
1557 | ||
1558 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1559 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1560 | ||
1561 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1562 | return 0; | |
1563 | ||
1564 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1565 | { | |
1566 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1567 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1568 | ||
1569 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1570 | return 0; | |
1571 | ||
1572 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1573 | continue; | |
3bfdc719 | 1574 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
1575 | return 0; |
1576 | } | |
1577 | } | |
1578 | ||
1579 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1580 | /* One set of parameters has more parameters lists than the | |
1581 | other. */ | |
1582 | return 0; | |
1583 | ||
1584 | return 1; | |
1585 | } | |
1586 | ||
f3400fe2 JM |
1587 | /* Complain if DECL shadows a template parameter. |
1588 | ||
1589 | [temp.local]: A template-parameter shall not be redeclared within its | |
1590 | scope (including nested scopes). */ | |
1591 | ||
1592 | void | |
1593 | check_template_shadow (decl) | |
1594 | tree decl; | |
1595 | { | |
f181d4ae | 1596 | tree olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); |
f3400fe2 | 1597 | |
f181d4ae MM |
1598 | if (current_template_parms && olddecl) |
1599 | { | |
f3400fe2 JM |
1600 | /* We check for decl != olddecl to avoid bogus errors for using a |
1601 | name inside a class. We check TPFI to avoid duplicate errors for | |
1602 | inline member templates. */ | |
fc03edb3 | 1603 | if (decl != olddecl && DECL_TEMPLATE_PARM_P (olddecl) |
f3400fe2 JM |
1604 | && ! TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
1605 | { | |
1606 | cp_error_at ("declaration of `%#D'", decl); | |
1607 | cp_error_at (" shadows template parm `%#D'", olddecl); | |
1608 | } | |
1609 | } | |
1610 | } | |
22a7be53 | 1611 | |
f3400fe2 | 1612 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
1613 | ORIG_LEVEL, DECL, and TYPE. */ |
1614 | ||
1615 | static tree | |
1616 | build_template_parm_index (index, level, orig_level, decl, type) | |
1617 | int index; | |
1618 | int level; | |
1619 | int orig_level; | |
1620 | tree decl; | |
1621 | tree type; | |
1622 | { | |
1623 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1624 | TEMPLATE_PARM_IDX (t) = index; | |
1625 | TEMPLATE_PARM_LEVEL (t) = level; | |
1626 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1627 | TEMPLATE_PARM_DECL (t) = decl; | |
1628 | TREE_TYPE (t) = type; | |
1629 | ||
1630 | return t; | |
1631 | } | |
1632 | ||
f84b4be9 | 1633 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 1634 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
1635 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
1636 | new one is created. */ | |
1637 | ||
1638 | static tree | |
93cdc044 | 1639 | reduce_template_parm_level (index, type, levels) |
f84b4be9 JM |
1640 | tree index; |
1641 | tree type; | |
93cdc044 | 1642 | int levels; |
f84b4be9 JM |
1643 | { |
1644 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1645 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 1646 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 JM |
1647 | { |
1648 | tree decl | |
1649 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
1650 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
1651 | type); | |
1652 | tree t | |
1653 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 1654 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
1655 | TEMPLATE_PARM_ORIG_LEVEL (index), |
1656 | decl, type); | |
1657 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
1658 | |
1659 | /* Template template parameters need this. */ | |
1660 | DECL_TEMPLATE_PARMS (decl) | |
1661 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
1662 | } |
1663 | ||
1664 | return TEMPLATE_PARM_DESCENDANTS (index); | |
1665 | } | |
1666 | ||
8d08fdba | 1667 | /* Process information from new template parameter NEXT and append it to the |
5566b478 | 1668 | LIST being built. */ |
e92cc029 | 1669 | |
8d08fdba MS |
1670 | tree |
1671 | process_template_parm (list, next) | |
1672 | tree list, next; | |
1673 | { | |
1674 | tree parm; | |
1675 | tree decl = 0; | |
a292b002 | 1676 | tree defval; |
5566b478 | 1677 | int is_type, idx; |
f84b4be9 | 1678 | |
8d08fdba MS |
1679 | parm = next; |
1680 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
a292b002 MS |
1681 | defval = TREE_PURPOSE (parm); |
1682 | parm = TREE_VALUE (parm); | |
1683 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
5566b478 MS |
1684 | |
1685 | if (list) | |
1686 | { | |
1687 | tree p = TREE_VALUE (tree_last (list)); | |
1688 | ||
1689 | if (TREE_CODE (p) == TYPE_DECL) | |
1690 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
73b0fce8 KL |
1691 | else if (TREE_CODE (p) == TEMPLATE_DECL) |
1692 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p))); | |
5566b478 | 1693 | else |
f84b4be9 | 1694 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
1695 | ++idx; |
1696 | } | |
1697 | else | |
1698 | idx = 0; | |
1699 | ||
8d08fdba MS |
1700 | if (!is_type) |
1701 | { | |
a292b002 | 1702 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); |
8d08fdba | 1703 | /* is a const-param */ |
a292b002 | 1704 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
c11b6f21 | 1705 | PARM, 0, NULL_TREE); |
d490621d MM |
1706 | |
1707 | /* [temp.param] | |
1708 | ||
1709 | The top-level cv-qualifiers on the template-parameter are | |
1710 | ignored when determining its type. */ | |
1711 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
1712 | ||
8d08fdba MS |
1713 | /* A template parameter is not modifiable. */ |
1714 | TREE_READONLY (parm) = 1; | |
c91a56d2 | 1715 | if (IS_AGGR_TYPE (TREE_TYPE (parm)) |
410c2fba KL |
1716 | && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM |
1717 | && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE) | |
8d08fdba | 1718 | { |
8251199e | 1719 | cp_error ("`%#T' is not a valid type for a template constant parameter", |
c91a56d2 MS |
1720 | TREE_TYPE (parm)); |
1721 | if (DECL_NAME (parm) == NULL_TREE) | |
8251199e | 1722 | error (" a template type parameter must begin with `class' or `typename'"); |
8d08fdba MS |
1723 | TREE_TYPE (parm) = void_type_node; |
1724 | } | |
37c46b43 MS |
1725 | else if (pedantic |
1726 | && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE | |
1727 | || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) | |
8251199e | 1728 | cp_pedwarn ("`%T' is not a valid type for a template constant parameter", |
eb66be0e | 1729 | TREE_TYPE (parm)); |
8d08fdba MS |
1730 | if (TREE_PERMANENT (parm) == 0) |
1731 | { | |
1732 | parm = copy_node (parm); | |
1733 | TREE_PERMANENT (parm) = 1; | |
1734 | } | |
8d08fdba | 1735 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
f84b4be9 JM |
1736 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
1737 | = build_template_parm_index (idx, processing_template_decl, | |
1738 | processing_template_decl, | |
1739 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
1740 | } |
1741 | else | |
1742 | { | |
73b0fce8 KL |
1743 | tree t; |
1744 | parm = TREE_VALUE (parm); | |
1745 | ||
1746 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
1747 | { | |
1748 | t = make_lang_type (TEMPLATE_TEMPLATE_PARM); | |
1749 | /* This is for distinguishing between real templates and template | |
1750 | template parameters */ | |
1751 | TREE_TYPE (parm) = t; | |
1752 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
1753 | decl = parm; | |
1754 | } | |
1755 | else | |
1756 | { | |
1757 | t = make_lang_type (TEMPLATE_TYPE_PARM); | |
1758 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ | |
1759 | decl = build_decl (TYPE_DECL, parm, t); | |
1760 | } | |
1761 | ||
d2e5ee5c MS |
1762 | TYPE_NAME (t) = decl; |
1763 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 1764 | parm = decl; |
f84b4be9 JM |
1765 | TEMPLATE_TYPE_PARM_INDEX (t) |
1766 | = build_template_parm_index (idx, processing_template_decl, | |
1767 | processing_template_decl, | |
1768 | decl, TREE_TYPE (parm)); | |
8d08fdba | 1769 | } |
8ccc31eb | 1770 | SET_DECL_ARTIFICIAL (decl); |
fc03edb3 | 1771 | DECL_TEMPLATE_PARM_P (decl) = 1; |
8d08fdba | 1772 | pushdecl (decl); |
a292b002 | 1773 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
1774 | return chainon (list, parm); |
1775 | } | |
1776 | ||
1777 | /* The end of a template parameter list has been reached. Process the | |
1778 | tree list into a parameter vector, converting each parameter into a more | |
1779 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
1780 | as PARM_DECLs. */ | |
1781 | ||
1782 | tree | |
1783 | end_template_parm_list (parms) | |
1784 | tree parms; | |
1785 | { | |
5566b478 | 1786 | int nparms; |
8d08fdba | 1787 | tree parm; |
5566b478 MS |
1788 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
1789 | ||
5566b478 MS |
1790 | current_template_parms |
1791 | = tree_cons (build_int_2 (0, processing_template_decl), | |
1792 | saved_parmlist, current_template_parms); | |
8d08fdba MS |
1793 | |
1794 | for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++) | |
5566b478 | 1795 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; |
a292b002 | 1796 | |
67ffc812 MM |
1797 | --processing_template_parmlist; |
1798 | ||
8d08fdba MS |
1799 | return saved_parmlist; |
1800 | } | |
1801 | ||
5566b478 MS |
1802 | /* end_template_decl is called after a template declaration is seen. */ |
1803 | ||
8d08fdba | 1804 | void |
5566b478 | 1805 | end_template_decl () |
8d08fdba | 1806 | { |
386b8a85 JM |
1807 | reset_specialization (); |
1808 | ||
5156628f | 1809 | if (! processing_template_decl) |
73aad9b9 JM |
1810 | return; |
1811 | ||
5566b478 MS |
1812 | /* This matches the pushlevel in begin_template_parm_list. */ |
1813 | poplevel (0, 0, 0); | |
8d08fdba | 1814 | |
5566b478 MS |
1815 | --processing_template_decl; |
1816 | current_template_parms = TREE_CHAIN (current_template_parms); | |
1817 | (void) get_pending_sizes (); /* Why? */ | |
1818 | } | |
8d08fdba | 1819 | |
36a117a5 MM |
1820 | /* Given a template argument vector containing the template PARMS. |
1821 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
1822 | |
1823 | tree | |
1824 | current_template_args () | |
5566b478 | 1825 | { |
36a117a5 | 1826 | tree header; |
b370501f | 1827 | tree args = NULL_TREE; |
36a117a5 | 1828 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
1829 | int l = length; |
1830 | ||
36a117a5 MM |
1831 | /* If there is only one level of template parameters, we do not |
1832 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
1833 | TREE_VEC containing the arguments. */ | |
1834 | if (length > 1) | |
1835 | args = make_tree_vec (length); | |
1836 | ||
1837 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 1838 | { |
5566b478 | 1839 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
1840 | int i; |
1841 | ||
5566b478 | 1842 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 1843 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 1844 | { |
98c1c668 JM |
1845 | tree t = TREE_VEC_ELT (a, i); |
1846 | ||
36a117a5 | 1847 | /* T will be a list if we are called from within a |
98c1c668 JM |
1848 | begin/end_template_parm_list pair, but a vector directly |
1849 | if within a begin/end_member_template_processing pair. */ | |
1850 | if (TREE_CODE (t) == TREE_LIST) | |
1851 | { | |
1852 | t = TREE_VALUE (t); | |
1853 | ||
73b0fce8 KL |
1854 | if (TREE_CODE (t) == TYPE_DECL |
1855 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
1856 | t = TREE_TYPE (t); |
1857 | else | |
1858 | t = DECL_INITIAL (t); | |
36a117a5 | 1859 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 1860 | } |
5566b478 | 1861 | } |
36a117a5 MM |
1862 | |
1863 | if (length > 1) | |
1864 | TREE_VEC_ELT (args, --l) = a; | |
1865 | else | |
1866 | args = a; | |
8d08fdba MS |
1867 | } |
1868 | ||
9a3b49ac MS |
1869 | return args; |
1870 | } | |
75650646 | 1871 | |
e1467ff2 MM |
1872 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
1873 | template PARMS. Used by push_template_decl below. */ | |
1874 | ||
75650646 MM |
1875 | static tree |
1876 | build_template_decl (decl, parms) | |
1877 | tree decl; | |
1878 | tree parms; | |
1879 | { | |
1880 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
1881 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
1882 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
1883 | if (DECL_LANG_SPECIFIC (decl)) | |
1884 | { | |
1885 | DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); | |
1886 | DECL_STATIC_FUNCTION_P (tmpl) = | |
1887 | DECL_STATIC_FUNCTION_P (decl); | |
1888 | } | |
1889 | ||
1890 | return tmpl; | |
1891 | } | |
1892 | ||
050367a3 MM |
1893 | struct template_parm_data |
1894 | { | |
6c30752f MM |
1895 | /* The level of the template parameters we are currently |
1896 | processing. */ | |
050367a3 | 1897 | int level; |
6c30752f MM |
1898 | |
1899 | /* The index of the specialization argument we are currently | |
1900 | processing. */ | |
1901 | int current_arg; | |
1902 | ||
1903 | /* An array whose size is the number of template parameters. The | |
1904 | elements are non-zero if the parameter has been used in any one | |
1905 | of the arguments processed so far. */ | |
050367a3 | 1906 | int* parms; |
6c30752f MM |
1907 | |
1908 | /* An array whose size is the number of template arguments. The | |
1909 | elements are non-zero if the argument makes use of template | |
1910 | parameters of this level. */ | |
1911 | int* arg_uses_template_parms; | |
050367a3 MM |
1912 | }; |
1913 | ||
1914 | /* Subroutine of push_template_decl used to see if each template | |
1915 | parameter in a partial specialization is used in the explicit | |
1916 | argument list. If T is of the LEVEL given in DATA (which is | |
1917 | treated as a template_parm_data*), then DATA->PARMS is marked | |
1918 | appropriately. */ | |
1919 | ||
1920 | static int | |
1921 | mark_template_parm (t, data) | |
1922 | tree t; | |
1923 | void* data; | |
1924 | { | |
1925 | int level; | |
1926 | int idx; | |
1927 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
1928 | ||
1929 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
1930 | { | |
1931 | level = TEMPLATE_PARM_LEVEL (t); | |
1932 | idx = TEMPLATE_PARM_IDX (t); | |
1933 | } | |
1934 | else | |
1935 | { | |
1936 | level = TEMPLATE_TYPE_LEVEL (t); | |
1937 | idx = TEMPLATE_TYPE_IDX (t); | |
1938 | } | |
1939 | ||
1940 | if (level == tpd->level) | |
6c30752f MM |
1941 | { |
1942 | tpd->parms[idx] = 1; | |
1943 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
1944 | } | |
050367a3 MM |
1945 | |
1946 | /* Return zero so that for_each_template_parm will continue the | |
1947 | traversal of the tree; we want to mark *every* template parm. */ | |
1948 | return 0; | |
1949 | } | |
1950 | ||
6c30752f MM |
1951 | /* Process the partial specialization DECL. */ |
1952 | ||
e9659ab0 | 1953 | static tree |
6c30752f MM |
1954 | process_partial_specialization (decl) |
1955 | tree decl; | |
1956 | { | |
1957 | tree type = TREE_TYPE (decl); | |
1958 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
1959 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
1960 | tree inner_args = innermost_args (specargs); | |
1961 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
1962 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
1963 | int nargs = TREE_VEC_LENGTH (inner_args); | |
1964 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
1965 | int i; | |
1966 | int did_error_intro = 0; | |
6c30752f MM |
1967 | struct template_parm_data tpd; |
1968 | struct template_parm_data tpd2; | |
1969 | ||
6c30752f MM |
1970 | /* We check that each of the template parameters given in the |
1971 | partial specialization is used in the argument list to the | |
1972 | specialization. For example: | |
1973 | ||
1974 | template <class T> struct S; | |
1975 | template <class T> struct S<T*>; | |
1976 | ||
1977 | The second declaration is OK because `T*' uses the template | |
1978 | parameter T, whereas | |
1979 | ||
1980 | template <class T> struct S<int>; | |
1981 | ||
1982 | is no good. Even trickier is: | |
1983 | ||
1984 | template <class T> | |
1985 | struct S1 | |
1986 | { | |
1987 | template <class U> | |
1988 | struct S2; | |
1989 | template <class U> | |
1990 | struct S2<T>; | |
1991 | }; | |
1992 | ||
1993 | The S2<T> declaration is actually illegal; it is a | |
1994 | full-specialization. Of course, | |
1995 | ||
1996 | template <class U> | |
1997 | struct S2<T (*)(U)>; | |
1998 | ||
1999 | or some such would have been OK. */ | |
2000 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2001 | tpd.parms = alloca (sizeof (int) * ntparms); | |
1ddb2906 | 2002 | bzero ((PTR) tpd.parms, sizeof (int) * ntparms); |
6c30752f MM |
2003 | |
2004 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
1ddb2906 | 2005 | bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs); |
6c30752f MM |
2006 | for (i = 0; i < nargs; ++i) |
2007 | { | |
2008 | tpd.current_arg = i; | |
2009 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2010 | &mark_template_parm, | |
2011 | &tpd); | |
2012 | } | |
2013 | for (i = 0; i < ntparms; ++i) | |
2014 | if (tpd.parms[i] == 0) | |
2015 | { | |
2016 | /* One of the template parms was not used in the | |
2017 | specialization. */ | |
2018 | if (!did_error_intro) | |
2019 | { | |
8251199e | 2020 | cp_error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2021 | did_error_intro = 1; |
2022 | } | |
2023 | ||
8251199e | 2024 | cp_error (" `%D'", |
6c30752f MM |
2025 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
2026 | } | |
2027 | ||
2028 | /* [temp.class.spec] | |
2029 | ||
2030 | The argument list of the specialization shall not be identical to | |
2031 | the implicit argument list of the primary template. */ | |
2032 | if (comp_template_args (inner_args, | |
2033 | innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2034 | (maintmpl))))) | |
8251199e | 2035 | cp_error ("partial specialization `%T' does not specialize any template arguments", type); |
6c30752f MM |
2036 | |
2037 | /* [temp.class.spec] | |
2038 | ||
2039 | A partially specialized non-type argument expression shall not | |
2040 | involve template parameters of the partial specialization except | |
2041 | when the argument expression is a simple identifier. | |
2042 | ||
2043 | The type of a template parameter corresponding to a specialized | |
2044 | non-type argument shall not be dependent on a parameter of the | |
2045 | specialization. */ | |
2046 | my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); | |
2047 | tpd2.parms = 0; | |
2048 | for (i = 0; i < nargs; ++i) | |
2049 | { | |
2050 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2051 | if (/* These first two lines are the `non-type' bit. */ | |
2052 | TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
2053 | && TREE_CODE (arg) != TEMPLATE_DECL | |
2054 | /* This next line is the `argument expression is not just a | |
2055 | simple identifier' condition and also the `specialized | |
2056 | non-type argument' bit. */ | |
2057 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2058 | { | |
2059 | if (tpd.arg_uses_template_parms[i]) | |
8251199e | 2060 | cp_error ("template argument `%E' involves template parameter(s)", arg); |
6c30752f MM |
2061 | else |
2062 | { | |
2063 | /* Look at the corresponding template parameter, | |
2064 | marking which template parameters its type depends | |
2065 | upon. */ | |
2066 | tree type = | |
2067 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2068 | i))); | |
2069 | ||
2070 | if (!tpd2.parms) | |
2071 | { | |
2072 | /* We haven't yet initialized TPD2. Do so now. */ | |
2073 | tpd2.arg_uses_template_parms | |
2074 | = (int*) alloca (sizeof (int) * nargs); | |
104bf76a | 2075 | /* The number of parameters here is the number in the |
76a83782 RH |
2076 | main template, which, as checked in the assertion |
2077 | above, is NARGS. */ | |
6c30752f MM |
2078 | tpd2.parms = (int*) alloca (sizeof (int) * nargs); |
2079 | tpd2.level = | |
2080 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2081 | } | |
2082 | ||
104bf76a | 2083 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2084 | looking for the template parameters of the main |
2085 | template, not in the specialization. */ | |
2086 | tpd2.current_arg = i; | |
2087 | tpd2.arg_uses_template_parms[i] = 0; | |
1ddb2906 | 2088 | bzero ((PTR) tpd2.parms, sizeof (int) * nargs); |
6c30752f MM |
2089 | for_each_template_parm (type, |
2090 | &mark_template_parm, | |
2091 | &tpd2); | |
2092 | ||
2093 | if (tpd2.arg_uses_template_parms [i]) | |
2094 | { | |
2095 | /* The type depended on some template parameters. | |
2096 | If they are fully specialized in the | |
2097 | specialization, that's OK. */ | |
2098 | int j; | |
2099 | for (j = 0; j < nargs; ++j) | |
2100 | if (tpd2.parms[j] != 0 | |
2101 | && tpd.arg_uses_template_parms [j]) | |
2102 | { | |
104bf76a | 2103 | cp_error ("type `%T' of template argument `%E' depends on template parameter(s)", |
6c30752f MM |
2104 | type, |
2105 | arg); | |
2106 | break; | |
2107 | } | |
2108 | } | |
2109 | } | |
2110 | } | |
2111 | } | |
2112 | ||
2113 | if (retrieve_specialization (maintmpl, specargs)) | |
2114 | /* We've already got this specialization. */ | |
2115 | return decl; | |
2116 | ||
2117 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) | |
2118 | = perm_tree_cons (inner_args, inner_parms, | |
2119 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
2120 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; | |
2121 | return decl; | |
2122 | } | |
2123 | ||
6ba89f8e MM |
2124 | /* Check that a template declaration's use of default arguments is not |
2125 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
2126 | non-zero if DECL is the thing declared by a primary template. | |
2127 | IS_PARTIAL is non-zero if DECL is a partial specialization. */ | |
2128 | ||
2129 | static void | |
2130 | check_default_tmpl_args (decl, parms, is_primary, is_partial) | |
2131 | tree decl; | |
2132 | tree parms; | |
2133 | int is_primary; | |
2134 | int is_partial; | |
2135 | { | |
2136 | char* msg; | |
2137 | int last_level_to_check; | |
2138 | ||
2139 | /* [temp.param] | |
2140 | ||
2141 | A default template-argument shall not be specified in a | |
2142 | function template declaration or a function template definition, nor | |
2143 | in the template-parameter-list of the definition of a member of a | |
2144 | class template. */ | |
2145 | ||
2146 | if (current_class_type | |
2147 | && !TYPE_BEING_DEFINED (current_class_type) | |
2148 | && DECL_REAL_CONTEXT (decl) == current_class_type | |
2149 | && DECL_DEFINED_IN_CLASS_P (decl)) | |
2150 | /* We already checked these parameters when the template was | |
2151 | declared, so there's no need to do it again now. This is an | |
2152 | inline member function definition. */ | |
2153 | return; | |
2154 | ||
2155 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) | |
2156 | /* For an ordinary class template, default template arguments are | |
2157 | allowed at the innermost level, e.g.: | |
2158 | template <class T = int> | |
2159 | struct S {}; | |
2160 | but, in a partial specialization, they're not allowed even | |
2161 | there, as we have in [temp.class.spec]: | |
2162 | ||
2163 | The template parameter list of a specialization shall not | |
2164 | contain default template argument values. | |
2165 | ||
2166 | So, for a partial specialization, or for a function template, | |
2167 | we look at all of them. */ | |
2168 | ; | |
2169 | else | |
2170 | /* But, for a primary class template that is not a partial | |
2171 | specialization we look at all template parameters except the | |
2172 | innermost ones. */ | |
2173 | parms = TREE_CHAIN (parms); | |
2174 | ||
2175 | /* Figure out what error message to issue. */ | |
2176 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2177 | msg = "default argument for template parameter in function template `%D'"; | |
2178 | else if (is_partial) | |
2179 | msg = "default argument in partial specialization `%D'"; | |
2180 | else | |
2181 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2182 | ||
2183 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2184 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2185 | examine the parameters to the class itself. On the one |
6ba89f8e MM |
2186 | hand, they will be checked when the class is defined, and, |
2187 | on the other, default arguments are legal in things like: | |
2188 | template <class T = double> | |
2189 | struct S { template <class U> void f(U); }; | |
2190 | Here the default argument for `S' has no bearing on the | |
2191 | declaration of `f'. */ | |
2192 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2193 | else | |
2194 | /* Check everything. */ | |
2195 | last_level_to_check = 0; | |
2196 | ||
2197 | for (; parms && TMPL_PARMS_DEPTH (parms) >= last_level_to_check; | |
2198 | parms = TREE_CHAIN (parms)) | |
2199 | { | |
2200 | tree inner_parms = TREE_VALUE (parms); | |
2201 | int i, ntparms; | |
2202 | ||
2203 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2204 | for (i = 0; i < ntparms; ++i) | |
2205 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2206 | { | |
2207 | if (msg) | |
2208 | { | |
2209 | cp_error (msg, decl); | |
2210 | msg = 0; | |
2211 | } | |
2212 | ||
2213 | /* Clear out the default argument so that we are not | |
2214 | confused later. */ | |
2215 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2216 | } | |
2217 | ||
2218 | /* At this point, if we're still interested in issuing messages, | |
2219 | they must apply to classes surrounding the object declared. */ | |
2220 | if (msg) | |
2221 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2222 | } | |
2223 | } | |
2224 | ||
3ac3d9ea | 2225 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2226 | parameters given by current_template_args, or reuses a |
2227 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2228 | equivalent one, if it is replaced via a call to duplicate_decls. |
2229 | ||
2230 | If IS_FRIEND is non-zero, DECL is a friend declaration. */ | |
3ac3d9ea MM |
2231 | |
2232 | tree | |
6757edfe | 2233 | push_template_decl_real (decl, is_friend) |
9a3b49ac | 2234 | tree decl; |
6757edfe | 2235 | int is_friend; |
9a3b49ac MS |
2236 | { |
2237 | tree tmpl; | |
f84b4be9 | 2238 | tree args; |
9a3b49ac | 2239 | tree info; |
f84b4be9 JM |
2240 | tree ctx; |
2241 | int primary; | |
6ba89f8e MM |
2242 | int is_partial; |
2243 | ||
2244 | /* See if this is a partial specialization. */ | |
2245 | is_partial = (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl) | |
2246 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE | |
2247 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); | |
6757edfe MM |
2248 | |
2249 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2250 | |
2251 | if (is_friend) | |
2252 | /* For a friend, we want the context of the friend function, not | |
2253 | the type of which it is a friend. */ | |
2254 | ctx = DECL_CONTEXT (decl); | |
2c73f9f5 ML |
2255 | else if (DECL_REAL_CONTEXT (decl) |
2256 | && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2257 | /* In the case of a virtual function, we want the class in which |
2258 | it is defined. */ | |
2259 | ctx = DECL_REAL_CONTEXT (decl); | |
2260 | else | |
2261 | /* Otherwise, if we're currently definining some class, the DECL | |
2262 | is assumed to be a member of the class. */ | |
2263 | ctx = current_class_type; | |
2264 | ||
2c73f9f5 ML |
2265 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2266 | ctx = NULL_TREE; | |
2267 | ||
2268 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2269 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2270 | |
93cdc044 JM |
2271 | /* For determining whether this is a primary template or not, we're really |
2272 | interested in the lexical context, not the true context. */ | |
2273 | if (is_friend) | |
36a117a5 | 2274 | info = current_class_type; |
93cdc044 JM |
2275 | else |
2276 | info = ctx; | |
2277 | ||
6ba89f8e | 2278 | /* See if this is a primary template. */ |
93cdc044 JM |
2279 | if (info && TREE_CODE (info) == FUNCTION_DECL) |
2280 | primary = 0; | |
6757edfe MM |
2281 | /* Note that template_class_depth returns 0 if given NULL_TREE, so |
2282 | this next line works even when we are at global scope. */ | |
2283 | else if (processing_template_decl > template_class_depth (info)) | |
9a3b49ac | 2284 | primary = 1; |
f84b4be9 JM |
2285 | else |
2286 | primary = 0; | |
9a3b49ac | 2287 | |
83566abf JM |
2288 | if (primary) |
2289 | { | |
2290 | if (current_lang_name == lang_name_c) | |
8251199e | 2291 | cp_error ("template with C linkage"); |
83566abf | 2292 | if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl))) |
8251199e | 2293 | cp_error ("template class without a name"); |
ed44da02 MM |
2294 | if (TREE_CODE (decl) == TYPE_DECL |
2295 | && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
8251199e | 2296 | cp_error ("template declaration of `%#T'", TREE_TYPE (decl)); |
83566abf JM |
2297 | } |
2298 | ||
6ba89f8e MM |
2299 | /* Check to see that the rules regarding the use of default |
2300 | arguments are not being violated. */ | |
2301 | check_default_tmpl_args (decl, current_template_parms, | |
2302 | primary, is_partial); | |
73aad9b9 | 2303 | |
6ba89f8e MM |
2304 | if (is_partial) |
2305 | return process_partial_specialization (decl); | |
d32789d8 | 2306 | |
9a3b49ac MS |
2307 | args = current_template_args (); |
2308 | ||
f84b4be9 JM |
2309 | if (!ctx |
2310 | || TREE_CODE (ctx) == FUNCTION_DECL | |
2311 | || TYPE_BEING_DEFINED (ctx) | |
2312 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
8d08fdba | 2313 | { |
75650646 | 2314 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
2315 | && DECL_TEMPLATE_INFO (decl) |
2316 | && DECL_TI_TEMPLATE (decl)) | |
2317 | tmpl = DECL_TI_TEMPLATE (decl); | |
2318 | else | |
786b5245 | 2319 | { |
f84b4be9 JM |
2320 | tmpl = build_template_decl (decl, current_template_parms); |
2321 | ||
2322 | if (DECL_LANG_SPECIFIC (decl) | |
2323 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2324 | { | |
2325 | /* A specialization of a member template of a template | |
2326 | class. */ | |
2327 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
2328 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
2329 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
2330 | } | |
786b5245 | 2331 | } |
8d08fdba MS |
2332 | } |
2333 | else | |
2334 | { | |
e1a5ccf7 | 2335 | tree a, t, current, parms; |
ba4f4e5d | 2336 | int i; |
6633d636 | 2337 | |
73aad9b9 | 2338 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) |
8251199e | 2339 | cp_error ("must specialize `%#T' before defining member `%#D'", |
73aad9b9 | 2340 | ctx, decl); |
6b9ab5cc MM |
2341 | if (TREE_CODE (decl) == TYPE_DECL) |
2342 | { | |
ed44da02 MM |
2343 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
2344 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
2345 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2346 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2347 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
2348 | else |
2349 | { | |
8251199e | 2350 | cp_error ("`%D' does not declare a template type", decl); |
6b9ab5cc MM |
2351 | return decl; |
2352 | } | |
2353 | } | |
fc378698 | 2354 | else if (! DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 2355 | { |
8251199e | 2356 | cp_error ("template definition of non-template `%#D'", decl); |
3ac3d9ea | 2357 | return decl; |
c91a56d2 | 2358 | } |
8d08fdba | 2359 | else |
5566b478 | 2360 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 2361 | |
e1a5ccf7 JM |
2362 | if (is_member_template (tmpl) |
2363 | && DECL_FUNCTION_TEMPLATE_P (tmpl) | |
2364 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) | |
2365 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
98c1c668 | 2366 | { |
e1a5ccf7 JM |
2367 | tree new_tmpl; |
2368 | ||
2369 | /* The declaration is a specialization of a member | |
2370 | template, declared outside the class. Therefore, the | |
2371 | innermost template arguments will be NULL, so we | |
2372 | replace them with the arguments determined by the | |
2373 | earlier call to check_explicit_specialization. */ | |
2374 | args = DECL_TI_ARGS (decl); | |
2375 | ||
2376 | new_tmpl | |
2377 | = build_template_decl (decl, current_template_parms); | |
2378 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
2379 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
2380 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
2381 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
2382 | DECL_TEMPLATE_INFO (new_tmpl) = | |
2383 | perm_tree_cons (tmpl, args, NULL_TREE); | |
2384 | ||
2385 | register_specialization (new_tmpl, tmpl, args); | |
2386 | return decl; | |
98c1c668 | 2387 | } |
98c1c668 | 2388 | |
e1a5ccf7 | 2389 | /* Make sure the template headers we got make sense. */ |
6633d636 | 2390 | |
e1a5ccf7 JM |
2391 | parms = DECL_TEMPLATE_PARMS (tmpl); |
2392 | i = TMPL_PARMS_DEPTH (parms); | |
2393 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 2394 | { |
e1a5ccf7 JM |
2395 | cp_error ("expected %d levels of template parms for `%#D', got %d", |
2396 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 2397 | } |
e1a5ccf7 JM |
2398 | else |
2399 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
2400 | { | |
2401 | a = TMPL_ARGS_LEVEL (args, i); | |
2402 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
2403 | ||
2404 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
2405 | { | |
2406 | if (current == decl) | |
2407 | cp_error ("got %d template parameters for `%#D'", | |
2408 | TREE_VEC_LENGTH (a), decl); | |
2409 | else | |
2410 | cp_error ("got %d template parameters for `%#T'", | |
2411 | TREE_VEC_LENGTH (a), current); | |
2412 | cp_error (" but %d required", TREE_VEC_LENGTH (t)); | |
2413 | } | |
98c1c668 | 2414 | |
e1a5ccf7 JM |
2415 | /* Perhaps we should also check that the parms are used in the |
2416 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 2417 | |
e1a5ccf7 JM |
2418 | if (current == decl) |
2419 | current = ctx; | |
2420 | else | |
2421 | current = TYPE_CONTEXT (current); | |
2422 | } | |
5566b478 | 2423 | } |
8d08fdba | 2424 | |
5566b478 MS |
2425 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
2426 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 2427 | |
36a117a5 MM |
2428 | /* Push template declarations for global functions and types. Note |
2429 | that we do not try to push a global template friend declared in a | |
2430 | template class; such a thing may well depend on the template | |
39c01e4c | 2431 | parameters of the class. */ |
36a117a5 MM |
2432 | if (! ctx |
2433 | && !(is_friend && template_class_depth (current_class_type) > 0)) | |
2c73f9f5 | 2434 | tmpl = pushdecl_namespace_level (tmpl); |
8d08fdba | 2435 | |
5566b478 | 2436 | if (primary) |
6757edfe | 2437 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
5566b478 MS |
2438 | |
2439 | info = perm_tree_cons (tmpl, args, NULL_TREE); | |
2440 | ||
824b9a4c | 2441 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
8d08fdba | 2442 | { |
ed44da02 MM |
2443 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
2444 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
2445 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE) | |
75650646 | 2446 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 2447 | } |
ec255269 | 2448 | else if (! DECL_LANG_SPECIFIC (decl)) |
8251199e | 2449 | cp_error ("template declaration of `%#D'", decl); |
51c184be | 2450 | else |
5566b478 | 2451 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
2452 | |
2453 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
2454 | } |
2455 | ||
6757edfe MM |
2456 | tree |
2457 | push_template_decl (decl) | |
2458 | tree decl; | |
2459 | { | |
2460 | return push_template_decl_real (decl, 0); | |
2461 | } | |
2462 | ||
2463 | /* Called when a class template TYPE is redeclared with the indicated | |
2464 | template PARMS, e.g.: | |
7fe6899f MM |
2465 | |
2466 | template <class T> struct S; | |
2467 | template <class T> struct S {}; */ | |
2468 | ||
2469 | void | |
6757edfe | 2470 | redeclare_class_template (type, parms) |
7fe6899f | 2471 | tree type; |
6757edfe | 2472 | tree parms; |
7fe6899f MM |
2473 | { |
2474 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); | |
6757edfe | 2475 | tree tmpl_parms; |
7fe6899f MM |
2476 | int i; |
2477 | ||
2478 | if (!PRIMARY_TEMPLATE_P (tmpl)) | |
2479 | /* The type is nested in some template class. Nothing to worry | |
2480 | about here; there are no new template parameters for the nested | |
2481 | type. */ | |
2482 | return; | |
2483 | ||
6757edfe MM |
2484 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
2485 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
2486 | ||
7fe6899f MM |
2487 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
2488 | { | |
8251199e JM |
2489 | cp_error_at ("previous declaration `%D'", tmpl); |
2490 | cp_error ("used %d template parameter%s instead of %d", | |
7fe6899f MM |
2491 | TREE_VEC_LENGTH (tmpl_parms), |
2492 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
2493 | TREE_VEC_LENGTH (parms)); | |
2494 | return; | |
2495 | } | |
2496 | ||
2497 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
2498 | { | |
2499 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
2500 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
2501 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
2502 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
2503 | ||
2504 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
2505 | { | |
8251199e JM |
2506 | cp_error_at ("template parameter `%#D'", tmpl_parm); |
2507 | cp_error ("redeclared here as `%#D'", parm); | |
7fe6899f MM |
2508 | return; |
2509 | } | |
2510 | ||
2511 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
2512 | { | |
2513 | /* We have in [temp.param]: | |
2514 | ||
2515 | A template-parameter may not be given default arguments | |
2516 | by two different declarations in the same scope. */ | |
8251199e JM |
2517 | cp_error ("redefinition of default argument for `%#D'", parm); |
2518 | cp_error_at (" original definition appeared here", tmpl_parm); | |
7fe6899f MM |
2519 | return; |
2520 | } | |
2521 | ||
2522 | if (parm_default != NULL_TREE) | |
2523 | /* Update the previous template parameters (which are the ones | |
2524 | that will really count) with the new default value. */ | |
2525 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
2526 | } | |
2527 | } | |
75650646 | 2528 | |
75650646 MM |
2529 | /* Attempt to convert the non-type template parameter EXPR to the |
2530 | indicated TYPE. If the conversion is successful, return the | |
2531 | converted value. If the conversion is unsuccesful, return | |
2532 | NULL_TREE if we issued an error message, or error_mark_node if we | |
2533 | did not. We issue error messages for out-and-out bad template | |
2534 | parameters, but not simply because the conversion failed, since we | |
2535 | might be just trying to do argument deduction. By the time this | |
2536 | function is called, neither TYPE nor EXPR may make use of template | |
2537 | parameters. */ | |
2538 | ||
2539 | static tree | |
e1467ff2 | 2540 | convert_nontype_argument (type, expr) |
75650646 MM |
2541 | tree type; |
2542 | tree expr; | |
2543 | { | |
2544 | tree expr_type = TREE_TYPE (expr); | |
2545 | ||
2546 | /* A template-argument for a non-type, non-template | |
2547 | template-parameter shall be one of: | |
2548 | ||
2549 | --an integral constant-expression of integral or enumeration | |
2550 | type; or | |
2551 | ||
2552 | --the name of a non-type template-parameter; or | |
2553 | ||
2554 | --the name of an object or function with external linkage, | |
2555 | including function templates and function template-ids but | |
86052cc3 | 2556 | excluding non-static class members, expressed as id-expression; |
75650646 MM |
2557 | or |
2558 | ||
2559 | --the address of an object or function with external linkage, | |
2560 | including function templates and function template-ids but | |
2561 | excluding non-static class members, expressed as & id-expression | |
2562 | where the & is optional if the name refers to a function or | |
2563 | array; or | |
2564 | ||
2565 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
2566 | ||
86052cc3 JM |
2567 | /* An integral constant-expression can include const variables |
2568 | or enumerators. */ | |
2569 | if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr)) | |
2570 | expr = decl_constant_value (expr); | |
2571 | ||
7bf2682f MM |
2572 | if (is_overloaded_fn (expr)) |
2573 | /* OK for now. We'll check that it has external linkage later. | |
2574 | Check this first since if expr_type is the unknown_type_node | |
2575 | we would otherwise complain below. */ | |
2576 | ; | |
f3400fe2 JM |
2577 | else if (TYPE_PTR_P (expr_type) |
2578 | || TREE_CODE (expr_type) == ARRAY_TYPE | |
2579 | || TREE_CODE (type) == REFERENCE_TYPE | |
75650646 MM |
2580 | /* If expr is the address of an overloaded function, we |
2581 | will get the unknown_type_node at this point. */ | |
2582 | || expr_type == unknown_type_node) | |
2583 | { | |
2584 | tree referent; | |
49bf0d6f MM |
2585 | tree e = expr; |
2586 | STRIP_NOPS (e); | |
75650646 | 2587 | |
f3400fe2 JM |
2588 | if (TREE_CODE (type) == REFERENCE_TYPE |
2589 | || TREE_CODE (expr_type) == ARRAY_TYPE) | |
2590 | referent = e; | |
2591 | else | |
75650646 | 2592 | { |
f3400fe2 JM |
2593 | if (TREE_CODE (e) != ADDR_EXPR) |
2594 | { | |
2595 | bad_argument: | |
2596 | cp_error ("`%E' is not a valid template argument", expr); | |
2597 | error ("it must be %s%s with external linkage", | |
2598 | TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE | |
2599 | ? "a pointer to " : "", | |
2600 | TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE | |
2601 | ? "a function" : "an object"); | |
2602 | return NULL_TREE; | |
2603 | } | |
2604 | ||
2605 | referent = TREE_OPERAND (e, 0); | |
2606 | STRIP_NOPS (referent); | |
75650646 MM |
2607 | } |
2608 | ||
75650646 MM |
2609 | if (TREE_CODE (referent) == STRING_CST) |
2610 | { | |
8251199e | 2611 | cp_error ("string literal %E is not a valid template argument", |
75650646 | 2612 | referent); |
8251199e | 2613 | error ("because it is the address of an object with static linkage"); |
75650646 MM |
2614 | return NULL_TREE; |
2615 | } | |
2616 | ||
2617 | if (is_overloaded_fn (referent)) | |
2618 | /* We'll check that it has external linkage later. */ | |
2619 | ; | |
2620 | else if (TREE_CODE (referent) != VAR_DECL) | |
2621 | goto bad_argument; | |
2622 | else if (!TREE_PUBLIC (referent)) | |
2623 | { | |
8251199e | 2624 | cp_error ("address of non-extern `%E' cannot be used as template argument", referent); |
75650646 MM |
2625 | return error_mark_node; |
2626 | } | |
2627 | } | |
f3400fe2 JM |
2628 | else if (INTEGRAL_TYPE_P (expr_type) |
2629 | || TYPE_PTRMEM_P (expr_type) | |
2630 | || TYPE_PTRMEMFUNC_P (expr_type) | |
2631 | /* The next two are g++ extensions. */ | |
2632 | || TREE_CODE (expr_type) == REAL_TYPE | |
2633 | || TREE_CODE (expr_type) == COMPLEX_TYPE) | |
75650646 | 2634 | { |
f3400fe2 JM |
2635 | if (! TREE_CONSTANT (expr)) |
2636 | { | |
2637 | non_constant: | |
2638 | cp_error ("non-constant `%E' cannot be used as template argument", | |
2639 | expr); | |
2640 | return NULL_TREE; | |
2641 | } | |
75650646 | 2642 | } |
75650646 MM |
2643 | else |
2644 | { | |
8251199e | 2645 | cp_error ("object `%E' cannot be used as template argument", expr); |
75650646 MM |
2646 | return NULL_TREE; |
2647 | } | |
2648 | ||
2649 | switch (TREE_CODE (type)) | |
2650 | { | |
2651 | case INTEGER_TYPE: | |
2652 | case BOOLEAN_TYPE: | |
2653 | case ENUMERAL_TYPE: | |
2654 | /* For a non-type template-parameter of integral or enumeration | |
2655 | type, integral promotions (_conv.prom_) and integral | |
2656 | conversions (_conv.integral_) are applied. */ | |
2657 | if (!INTEGRAL_TYPE_P (expr_type)) | |
2658 | return error_mark_node; | |
2659 | ||
2660 | /* It's safe to call digest_init in this case; we know we're | |
2661 | just converting one integral constant expression to another. */ | |
db02b6b9 | 2662 | expr = digest_init (type, expr, (tree*) 0); |
75650646 | 2663 | |
db02b6b9 | 2664 | if (TREE_CODE (expr) != INTEGER_CST) |
f3400fe2 | 2665 | /* Curiously, some TREE_CONSTANT integral expressions do not |
db02b6b9 MM |
2666 | simplify to integer constants. For example, `3 % 0', |
2667 | remains a TRUNC_MOD_EXPR. */ | |
2668 | goto non_constant; | |
2669 | ||
2670 | return expr; | |
2671 | ||
abff8e06 JM |
2672 | case REAL_TYPE: |
2673 | case COMPLEX_TYPE: | |
2674 | /* These are g++ extensions. */ | |
2675 | if (TREE_CODE (expr_type) != TREE_CODE (type)) | |
2676 | return error_mark_node; | |
2677 | ||
db02b6b9 MM |
2678 | expr = digest_init (type, expr, (tree*) 0); |
2679 | ||
2680 | if (TREE_CODE (expr) != REAL_CST) | |
2681 | goto non_constant; | |
2682 | ||
2683 | return expr; | |
abff8e06 | 2684 | |
75650646 MM |
2685 | case POINTER_TYPE: |
2686 | { | |
2687 | tree type_pointed_to = TREE_TYPE (type); | |
2688 | ||
2689 | if (TYPE_PTRMEM_P (type)) | |
2690 | /* For a non-type template-parameter of type pointer to data | |
2691 | member, qualification conversions (_conv.qual_) are | |
2692 | applied. */ | |
2693 | return perform_qualification_conversions (type, expr); | |
2694 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) | |
2695 | { | |
2696 | /* For a non-type template-parameter of type pointer to | |
2697 | function, only the function-to-pointer conversion | |
2698 | (_conv.func_) is applied. If the template-argument | |
2699 | represents a set of overloaded functions (or a pointer to | |
2700 | such), the matching function is selected from the set | |
2701 | (_over.over_). */ | |
2702 | tree fns; | |
2703 | tree fn; | |
2704 | ||
7bf2682f | 2705 | if (TREE_CODE (expr) == ADDR_EXPR) |
75650646 MM |
2706 | fns = TREE_OPERAND (expr, 0); |
2707 | else | |
2708 | fns = expr; | |
2709 | ||
a05273d4 | 2710 | fn = instantiate_type (type_pointed_to, fns, 0); |
75650646 MM |
2711 | |
2712 | if (fn == error_mark_node) | |
2713 | return error_mark_node; | |
2714 | ||
2715 | if (!TREE_PUBLIC (fn)) | |
2716 | { | |
2717 | if (really_overloaded_fn (fns)) | |
2718 | return error_mark_node; | |
2719 | else | |
2720 | goto bad_argument; | |
2721 | } | |
2722 | ||
2723 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
2724 | ||
3bfdc719 | 2725 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2726 | 0); |
2727 | return expr; | |
2728 | } | |
2729 | else | |
2730 | { | |
2731 | /* For a non-type template-parameter of type pointer to | |
2732 | object, qualification conversions (_conv.qual_) and the | |
2733 | array-to-pointer conversion (_conv.array_) are applied. | |
2734 | [Note: In particular, neither the null pointer conversion | |
2735 | (_conv.ptr_) nor the derived-to-base conversion | |
2736 | (_conv.ptr_) are applied. Although 0 is a valid | |
2737 | template-argument for a non-type template-parameter of | |
2738 | integral type, it is not a valid template-argument for a | |
e1467ff2 MM |
2739 | non-type template-parameter of pointer type.] |
2740 | ||
2741 | The call to decay_conversion performs the | |
2742 | array-to-pointer conversion, if appropriate. */ | |
2743 | expr = decay_conversion (expr); | |
75650646 MM |
2744 | |
2745 | if (expr == error_mark_node) | |
2746 | return error_mark_node; | |
2747 | else | |
2748 | return perform_qualification_conversions (type, expr); | |
2749 | } | |
2750 | } | |
2751 | break; | |
2752 | ||
2753 | case REFERENCE_TYPE: | |
2754 | { | |
2755 | tree type_referred_to = TREE_TYPE (type); | |
2756 | ||
2757 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
2758 | { | |
2759 | /* For a non-type template-parameter of type reference to | |
39c01e4c MM |
2760 | function, no conversions apply. If the |
2761 | template-argument represents a set of overloaded | |
2762 | functions, the matching function is selected from the | |
2763 | set (_over.over_). */ | |
75650646 MM |
2764 | tree fns = expr; |
2765 | tree fn; | |
2766 | ||
a05273d4 | 2767 | fn = instantiate_type (type_referred_to, fns, 0); |
e6e174e5 JM |
2768 | |
2769 | if (fn == error_mark_node) | |
2770 | return error_mark_node; | |
75650646 MM |
2771 | |
2772 | if (!TREE_PUBLIC (fn)) | |
2773 | { | |
2774 | if (really_overloaded_fn (fns)) | |
2775 | /* Don't issue an error here; we might get a different | |
2776 | function if the overloading had worked out | |
2777 | differently. */ | |
2778 | return error_mark_node; | |
2779 | else | |
2780 | goto bad_argument; | |
2781 | } | |
2782 | ||
3bfdc719 MM |
2783 | my_friendly_assert (same_type_p (type_referred_to, |
2784 | TREE_TYPE (fn)), | |
75650646 MM |
2785 | 0); |
2786 | ||
2787 | return fn; | |
2788 | } | |
2789 | else | |
2790 | { | |
2791 | /* For a non-type template-parameter of type reference to | |
2792 | object, no conversions apply. The type referred to by the | |
2793 | reference may be more cv-qualified than the (otherwise | |
2794 | identical) type of the template-argument. The | |
2795 | template-parameter is bound directly to the | |
2796 | template-argument, which must be an lvalue. */ | |
ff350acd JM |
2797 | if ((TYPE_MAIN_VARIANT (expr_type) |
2798 | != TYPE_MAIN_VARIANT (type_referred_to)) | |
91063b51 MM |
2799 | || !at_least_as_qualified_p (type_referred_to, |
2800 | expr_type) | |
75650646 MM |
2801 | || !real_lvalue_p (expr)) |
2802 | return error_mark_node; | |
2803 | else | |
2804 | return expr; | |
2805 | } | |
2806 | } | |
2807 | break; | |
2808 | ||
2809 | case RECORD_TYPE: | |
2810 | { | |
410c2fba KL |
2811 | if (!TYPE_PTRMEMFUNC_P (type)) |
2812 | /* This handles templates like | |
2813 | template<class T, T t> void f(); | |
2814 | when T is substituted with any class. The second template | |
2815 | parameter becomes invalid and the template candidate is | |
2816 | rejected. */ | |
2817 | return error_mark_node; | |
75650646 MM |
2818 | |
2819 | /* For a non-type template-parameter of type pointer to member | |
2820 | function, no conversions apply. If the template-argument | |
2821 | represents a set of overloaded member functions, the | |
2822 | matching member function is selected from the set | |
2823 | (_over.over_). */ | |
2824 | ||
2825 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
2826 | expr_type != unknown_type_node) | |
2827 | return error_mark_node; | |
2828 | ||
2829 | if (TREE_CODE (expr) == CONSTRUCTOR) | |
2830 | { | |
2831 | /* A ptr-to-member constant. */ | |
3bfdc719 | 2832 | if (!same_type_p (type, expr_type)) |
75650646 MM |
2833 | return error_mark_node; |
2834 | else | |
2835 | return expr; | |
2836 | } | |
2837 | ||
2838 | if (TREE_CODE (expr) != ADDR_EXPR) | |
2839 | return error_mark_node; | |
2840 | ||
d8f8dca1 | 2841 | expr = instantiate_type (type, expr, 0); |
75650646 | 2842 | |
d8f8dca1 | 2843 | if (expr == error_mark_node) |
75650646 MM |
2844 | return error_mark_node; |
2845 | ||
3bfdc719 | 2846 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), |
75650646 MM |
2847 | 0); |
2848 | return expr; | |
2849 | } | |
2850 | break; | |
2851 | ||
2852 | default: | |
2853 | /* All non-type parameters must have one of these types. */ | |
2854 | my_friendly_abort (0); | |
2855 | break; | |
2856 | } | |
2857 | ||
2858 | return error_mark_node; | |
2859 | } | |
2860 | ||
744fac59 KL |
2861 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
2862 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
2863 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
2864 | or PARM_DECL. | |
2865 | ||
2866 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
2867 | the case, then extra parameters must have default arguments. | |
2868 | ||
2869 | Consider the example: | |
2870 | template <class T, class Allocator = allocator> class vector; | |
2871 | template<template <class U> class TT> class C; | |
2872 | ||
2873 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
2874 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
2875 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
2876 | substitute the TT parameter. */ | |
2877 | ||
2878 | static int | |
4393e105 MM |
2879 | coerce_template_template_parms (parm_parms, arg_parms, complain, |
2880 | in_decl, outer_args) | |
2881 | tree parm_parms, arg_parms; | |
2882 | int complain; | |
2883 | tree in_decl, outer_args; | |
744fac59 KL |
2884 | { |
2885 | int nparms, nargs, i; | |
2886 | tree parm, arg; | |
2887 | ||
2888 | my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); | |
2889 | my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); | |
2890 | ||
2891 | nparms = TREE_VEC_LENGTH (parm_parms); | |
2892 | nargs = TREE_VEC_LENGTH (arg_parms); | |
2893 | ||
2894 | /* The rule here is opposite of coerce_template_parms. */ | |
2895 | if (nargs < nparms | |
2896 | || (nargs > nparms | |
2897 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
2898 | return 0; | |
2899 | ||
2900 | for (i = 0; i < nparms; ++i) | |
2901 | { | |
2902 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
2903 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
2904 | ||
2905 | if (arg == NULL_TREE || arg == error_mark_node | |
2906 | || parm == NULL_TREE || parm == error_mark_node) | |
2907 | return 0; | |
2908 | ||
2909 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
2910 | return 0; | |
2911 | ||
2912 | switch (TREE_CODE (parm)) | |
2913 | { | |
2914 | case TYPE_DECL: | |
2915 | break; | |
2916 | ||
2917 | case TEMPLATE_DECL: | |
2918 | /* We encounter instantiations of templates like | |
2919 | template <template <template <class> class> class TT> | |
2920 | class C; */ | |
700466c2 JM |
2921 | { |
2922 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
2923 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
2924 | ||
2925 | if (!coerce_template_template_parms (parmparm, argparm, | |
4393e105 MM |
2926 | complain, in_decl, |
2927 | outer_args)) | |
700466c2 JM |
2928 | return 0; |
2929 | } | |
2930 | break; | |
744fac59 KL |
2931 | |
2932 | case PARM_DECL: | |
2933 | /* The tsubst call is used to handle cases such as | |
2934 | template <class T, template <T> class TT> class D; | |
2935 | i.e. the parameter list of TT depends on earlier parameters. */ | |
4393e105 MM |
2936 | if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, |
2937 | complain, in_decl), | |
3bfdc719 | 2938 | TREE_TYPE (arg))) |
744fac59 KL |
2939 | return 0; |
2940 | break; | |
2941 | ||
2942 | default: | |
2943 | my_friendly_abort (0); | |
2944 | } | |
2945 | } | |
2946 | return 1; | |
2947 | } | |
2948 | ||
8b5b8b7c MM |
2949 | /* Convert the indicated template ARG as necessary to match the |
2950 | indicated template PARM. Returns the converted ARG, or | |
2951 | error_mark_node if the conversion was unsuccessful. Error messages | |
2952 | are issued if COMPLAIN is non-zero. This conversion is for the Ith | |
2953 | parameter in the parameter list. ARGS is the full set of template | |
2954 | arguments deduced so far. */ | |
2955 | ||
2956 | static tree | |
2957 | convert_template_argument (parm, arg, args, complain, i, in_decl) | |
2958 | tree parm; | |
2959 | tree arg; | |
2960 | tree args; | |
2961 | int complain; | |
2962 | int i; | |
2963 | tree in_decl; | |
2964 | { | |
2965 | tree val; | |
2966 | tree inner_args; | |
2967 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
2968 | ||
2969 | inner_args = innermost_args (args); | |
2970 | ||
2971 | if (TREE_CODE (arg) == TREE_LIST | |
2972 | && TREE_TYPE (arg) != NULL_TREE | |
2973 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
2974 | { | |
2975 | /* The template argument was the name of some | |
2976 | member function. That's usually | |
2977 | illegal, but static members are OK. In any | |
2978 | case, grab the underlying fields/functions | |
2979 | and issue an error later if required. */ | |
2980 | arg = TREE_VALUE (arg); | |
2981 | TREE_TYPE (arg) = unknown_type_node; | |
2982 | } | |
2983 | ||
2984 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
2985 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
2986 | || requires_tmpl_type); | |
2987 | ||
2988 | /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, | |
2989 | we also accept implicitly created TYPE_DECL as a valid argument. | |
2990 | This is necessary to handle the case where we pass a template name | |
2991 | to a template template parameter in a scope where we've derived from | |
2992 | in instantiation of that template, so the template name refers to that | |
2993 | instantiation. We really ought to handle this better. */ | |
2994 | is_tmpl_type | |
2995 | = ((TREE_CODE (arg) == TEMPLATE_DECL | |
2996 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
2997 | || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 2998 | && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg)) |
8b5b8b7c MM |
2999 | || (TREE_CODE (arg) == RECORD_TYPE |
3000 | && CLASSTYPE_TEMPLATE_INFO (arg) | |
3001 | && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL | |
3002 | && DECL_ARTIFICIAL (TYPE_NAME (arg)) | |
3003 | && requires_tmpl_type | |
b54ccf71 | 3004 | && is_base_of_enclosing_class (arg, current_class_type))); |
8b5b8b7c MM |
3005 | if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
3006 | arg = TYPE_STUB_DECL (arg); | |
3007 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) | |
3008 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
3009 | ||
3010 | is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type; | |
3011 | ||
3012 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3013 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3014 | { | |
8251199e JM |
3015 | cp_pedwarn ("to refer to a type member of a template parameter,"); |
3016 | cp_pedwarn (" use `typename %E'", arg); | |
8b5b8b7c MM |
3017 | |
3018 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3019 | TREE_OPERAND (arg, 1)); | |
3020 | is_type = 1; | |
3021 | } | |
3022 | if (is_type != requires_type) | |
3023 | { | |
3024 | if (in_decl) | |
3025 | { | |
3026 | if (complain) | |
3027 | { | |
8251199e | 3028 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3029 | i + 1, in_decl); |
3030 | if (is_type) | |
8251199e | 3031 | cp_error (" expected a constant of type `%T', got `%T'", |
8b5b8b7c MM |
3032 | TREE_TYPE (parm), |
3033 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
3034 | else | |
8251199e | 3035 | cp_error (" expected a type, got `%E'", arg); |
8b5b8b7c MM |
3036 | } |
3037 | } | |
3038 | return error_mark_node; | |
3039 | } | |
3040 | if (is_tmpl_type ^ requires_tmpl_type) | |
3041 | { | |
3042 | if (in_decl && complain) | |
3043 | { | |
8251199e | 3044 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c MM |
3045 | i + 1, in_decl); |
3046 | if (is_tmpl_type) | |
8251199e | 3047 | cp_error (" expected a type, got `%T'", DECL_NAME (arg)); |
8b5b8b7c | 3048 | else |
8251199e | 3049 | cp_error (" expected a class template, got `%T'", arg); |
8b5b8b7c MM |
3050 | } |
3051 | return error_mark_node; | |
3052 | } | |
3053 | ||
3054 | if (is_type) | |
3055 | { | |
3056 | if (requires_tmpl_type) | |
3057 | { | |
3058 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3059 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3060 | ||
4393e105 | 3061 | if (coerce_template_template_parms (parmparm, argparm, complain, |
8b5b8b7c MM |
3062 | in_decl, inner_args)) |
3063 | { | |
3064 | val = arg; | |
3065 | ||
3066 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3067 | TEMPLATE_DECL. */ | |
3068 | if (val != error_mark_node | |
3069 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3070 | val = TREE_TYPE (val); | |
3071 | } | |
3072 | else | |
3073 | { | |
3074 | if (in_decl && complain) | |
3075 | { | |
8251199e | 3076 | cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", |
8b5b8b7c | 3077 | i + 1, in_decl); |
8251199e | 3078 | cp_error (" expected a template of type `%D', got `%D'", parm, arg); |
8b5b8b7c MM |
3079 | } |
3080 | ||
3081 | val = error_mark_node; | |
3082 | } | |
3083 | } | |
3084 | else | |
3085 | { | |
3086 | val = groktypename (arg); | |
3087 | if (! processing_template_decl) | |
3088 | { | |
3089 | /* [basic.link]: A name with no linkage (notably, the | |
3090 | name of a class or enumeration declared in a local | |
3091 | scope) shall not be used to declare an entity with | |
3092 | linkage. This implies that names with no linkage | |
3093 | cannot be used as template arguments. */ | |
3094 | tree t = no_linkage_check (val); | |
3095 | if (t) | |
3096 | { | |
3097 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
8251199e JM |
3098 | cp_pedwarn |
3099 | ("template-argument `%T' uses anonymous type", val); | |
8b5b8b7c | 3100 | else |
8251199e JM |
3101 | cp_error |
3102 | ("template-argument `%T' uses local type `%T'", | |
8b5b8b7c MM |
3103 | val, t); |
3104 | return error_mark_node; | |
3105 | } | |
3106 | } | |
3107 | } | |
3108 | } | |
3109 | else | |
3110 | { | |
4393e105 | 3111 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c MM |
3112 | |
3113 | if (processing_template_decl) | |
3114 | arg = maybe_fold_nontype_arg (arg); | |
3115 | ||
3116 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
3117 | /* We used to call digest_init here. However, digest_init | |
3118 | will report errors, which we don't want when complain | |
3119 | is zero. More importantly, digest_init will try too | |
3120 | hard to convert things: for example, `0' should not be | |
3121 | converted to pointer type at this point according to | |
3122 | the standard. Accepting this is not merely an | |
3123 | extension, since deciding whether or not these | |
3124 | conversions can occur is part of determining which | |
3125 | function template to call, or whether a given epxlicit | |
3126 | argument specification is legal. */ | |
3127 | val = convert_nontype_argument (t, arg); | |
3128 | else | |
3129 | val = arg; | |
3130 | ||
3131 | if (val == NULL_TREE) | |
3132 | val = error_mark_node; | |
3133 | else if (val == error_mark_node && complain) | |
8251199e | 3134 | cp_error ("could not convert template argument `%E' to `%T'", |
8b5b8b7c MM |
3135 | arg, t); |
3136 | } | |
3137 | ||
3138 | return val; | |
3139 | } | |
3140 | ||
3141 | /* Convert all template arguments to their appropriate types, and | |
3142 | return a vector containing the innermost resulting template | |
3143 | arguments. If any error occurs, return error_mark_node, and, if | |
3144 | COMPLAIN is non-zero, issue an error message. Some error messages | |
3145 | are issued even if COMPLAIN is zero; for instance, if a template | |
3146 | argument is composed from a local class. | |
75650646 MM |
3147 | |
3148 | If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be | |
3149 | provided in ARGLIST, or else trailing parameters must have default | |
3150 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
cabc336a MM |
3151 | deduction for any unspecified trailing arguments. |
3152 | ||
3153 | The resulting TREE_VEC is allocated on a temporary obstack, and | |
3154 | must be explicitly copied if it will be permanent. */ | |
de575009 | 3155 | |
8d08fdba | 3156 | static tree |
8b5b8b7c | 3157 | coerce_template_parms (parms, args, in_decl, |
75650646 | 3158 | complain, |
744fac59 | 3159 | require_all_arguments) |
8b5b8b7c | 3160 | tree parms, args; |
8d08fdba | 3161 | tree in_decl; |
75650646 MM |
3162 | int complain; |
3163 | int require_all_arguments; | |
8d08fdba | 3164 | { |
a292b002 | 3165 | int nparms, nargs, i, lost = 0; |
e4a84209 | 3166 | tree inner_args; |
8b5b8b7c MM |
3167 | tree new_args; |
3168 | tree new_inner_args; | |
a292b002 | 3169 | |
8b5b8b7c | 3170 | inner_args = innermost_args (args); |
e4a84209 | 3171 | nargs = NUM_TMPL_ARGS (inner_args); |
a292b002 MS |
3172 | nparms = TREE_VEC_LENGTH (parms); |
3173 | ||
3174 | if (nargs > nparms | |
3175 | || (nargs < nparms | |
75650646 | 3176 | && require_all_arguments |
a292b002 | 3177 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3178 | { |
75650646 MM |
3179 | if (complain) |
3180 | { | |
8251199e | 3181 | cp_error ("wrong number of template arguments (%d, should be %d)", |
e4a84209 | 3182 | nargs, nparms); |
75650646 MM |
3183 | |
3184 | if (in_decl) | |
8251199e | 3185 | cp_error_at ("provided for `%D'", in_decl); |
75650646 MM |
3186 | } |
3187 | ||
8d08fdba MS |
3188 | return error_mark_node; |
3189 | } | |
3190 | ||
cabc336a | 3191 | new_inner_args = make_temp_vec (nparms); |
8b5b8b7c MM |
3192 | new_args = add_outermost_template_args (args, new_inner_args); |
3193 | for (i = 0; i < nparms; i++) | |
8d08fdba | 3194 | { |
8b5b8b7c MM |
3195 | tree arg; |
3196 | tree parm; | |
e4a84209 | 3197 | |
8b5b8b7c MM |
3198 | /* Get the Ith template parameter. */ |
3199 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 3200 | |
8b5b8b7c MM |
3201 | /* Calculate the Ith argument. */ |
3202 | if (inner_args && TREE_CODE (inner_args) == TREE_LIST) | |
8d08fdba | 3203 | { |
8b5b8b7c MM |
3204 | arg = TREE_VALUE (inner_args); |
3205 | inner_args = TREE_CHAIN (inner_args); | |
8d08fdba | 3206 | } |
8b5b8b7c MM |
3207 | else if (i < nargs) |
3208 | arg = TREE_VEC_ELT (inner_args, i); | |
3209 | /* If no template argument was supplied, look for a default | |
3210 | value. */ | |
3211 | else if (TREE_PURPOSE (parm) == NULL_TREE) | |
3212 | { | |
3213 | /* There was no default value. */ | |
3214 | my_friendly_assert (!require_all_arguments, 0); | |
3215 | break; | |
3216 | } | |
3217 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
4393e105 | 3218 | arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); |
8b5b8b7c | 3219 | else |
4393e105 MM |
3220 | arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, |
3221 | in_decl); | |
e4a84209 | 3222 | |
8b5b8b7c | 3223 | /* Now, convert the Ith argument, as necessary. */ |
75650646 MM |
3224 | if (arg == NULL_TREE) |
3225 | /* We're out of arguments. */ | |
3226 | { | |
3227 | my_friendly_assert (!require_all_arguments, 0); | |
3228 | break; | |
3229 | } | |
8b5b8b7c | 3230 | else if (arg == error_mark_node) |
75650646 | 3231 | { |
8251199e | 3232 | cp_error ("template argument %d is invalid", i + 1); |
8b5b8b7c | 3233 | arg = error_mark_node; |
8d08fdba | 3234 | } |
8b5b8b7c MM |
3235 | else |
3236 | arg = convert_template_argument (TREE_VALUE (parm), | |
3237 | arg, new_args, complain, i, | |
3238 | in_decl); | |
3239 | ||
3240 | if (arg == error_mark_node) | |
8d08fdba | 3241 | lost++; |
8b5b8b7c | 3242 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 3243 | } |
8b5b8b7c | 3244 | |
8d08fdba MS |
3245 | if (lost) |
3246 | return error_mark_node; | |
8b5b8b7c MM |
3247 | |
3248 | return new_inner_args; | |
8d08fdba MS |
3249 | } |
3250 | ||
34016c81 JM |
3251 | /* Returns 1 if template args OT and NT are equivalent. */ |
3252 | ||
3253 | int | |
3254 | template_args_equal (ot, nt) | |
3255 | tree ot, nt; | |
3256 | { | |
3257 | if (nt == ot) | |
3258 | return 1; | |
3259 | if (TREE_CODE (nt) != TREE_CODE (ot)) | |
3260 | return 0; | |
3261 | if (TREE_CODE (nt) == TREE_VEC) | |
3262 | /* For member templates */ | |
3263 | return comp_template_args (ot, nt); | |
3264 | else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't') | |
3bfdc719 | 3265 | return same_type_p (ot, nt); |
34016c81 JM |
3266 | else |
3267 | return (cp_tree_equal (ot, nt) > 0); | |
3268 | } | |
3269 | ||
3270 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
3271 | of template arguments. Returns 0 otherwise. */ |
3272 | ||
6757edfe | 3273 | int |
5566b478 MS |
3274 | comp_template_args (oldargs, newargs) |
3275 | tree oldargs, newargs; | |
3276 | { | |
3277 | int i; | |
3278 | ||
386b8a85 JM |
3279 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
3280 | return 0; | |
3281 | ||
5566b478 MS |
3282 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
3283 | { | |
3284 | tree nt = TREE_VEC_ELT (newargs, i); | |
3285 | tree ot = TREE_VEC_ELT (oldargs, i); | |
3286 | ||
34016c81 | 3287 | if (! template_args_equal (ot, nt)) |
61a127b3 | 3288 | return 0; |
5566b478 MS |
3289 | } |
3290 | return 1; | |
3291 | } | |
3292 | ||
8d08fdba MS |
3293 | /* Given class template name and parameter list, produce a user-friendly name |
3294 | for the instantiation. */ | |
e92cc029 | 3295 | |
8d08fdba | 3296 | static char * |
36a117a5 | 3297 | mangle_class_name_for_template (name, parms, arglist) |
8d08fdba MS |
3298 | char *name; |
3299 | tree parms, arglist; | |
3300 | { | |
3301 | static struct obstack scratch_obstack; | |
3302 | static char *scratch_firstobj; | |
3303 | int i, nparms; | |
8d08fdba MS |
3304 | |
3305 | if (!scratch_firstobj) | |
fc378698 | 3306 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
3307 | else |
3308 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 3309 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 3310 | |
8d08fdba | 3311 | #define ccat(c) obstack_1grow (&scratch_obstack, (c)); |
8d08fdba | 3312 | #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s)) |
8d08fdba MS |
3313 | |
3314 | cat (name); | |
3315 | ccat ('<'); | |
3316 | nparms = TREE_VEC_LENGTH (parms); | |
36a117a5 | 3317 | arglist = innermost_args (arglist); |
8d08fdba MS |
3318 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); |
3319 | for (i = 0; i < nparms; i++) | |
3320 | { | |
a292b002 MS |
3321 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
3322 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
3323 | |
3324 | if (i) | |
3325 | ccat (','); | |
3326 | ||
a292b002 | 3327 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 3328 | { |
294471c3 | 3329 | cat (type_as_string_real (arg, 0, 1)); |
8d08fdba MS |
3330 | continue; |
3331 | } | |
73b0fce8 KL |
3332 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
3333 | { | |
3334 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
3335 | { |
3336 | /* Already substituted with real template. Just output | |
3337 | the template name here */ | |
cb0dbb9a JM |
3338 | tree context = DECL_CONTEXT (arg); |
3339 | if (context) | |
2c73f9f5 | 3340 | { |
cb0dbb9a | 3341 | my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422); |
2c73f9f5 ML |
3342 | cat(decl_as_string (DECL_CONTEXT (arg), 0)); |
3343 | cat("::"); | |
3344 | } | |
3345 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3346 | } | |
73b0fce8 KL |
3347 | else |
3348 | /* Output the parameter declaration */ | |
294471c3 | 3349 | cat (type_as_string_real (arg, 0, 1)); |
73b0fce8 KL |
3350 | continue; |
3351 | } | |
8d08fdba MS |
3352 | else |
3353 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
3354 | ||
3355 | if (TREE_CODE (arg) == TREE_LIST) | |
3356 | { | |
3357 | /* New list cell was built because old chain link was in | |
3358 | use. */ | |
3359 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
3360 | arg = TREE_VALUE (arg); | |
3361 | } | |
3362 | /* No need to check arglist against parmlist here; we did that | |
3363 | in coerce_template_parms, called from lookup_template_class. */ | |
3364 | cat (expr_as_string (arg, 0)); | |
3365 | } | |
3366 | { | |
3367 | char *bufp = obstack_next_free (&scratch_obstack); | |
3368 | int offset = 0; | |
3369 | while (bufp[offset - 1] == ' ') | |
3370 | offset--; | |
3371 | obstack_blank_fast (&scratch_obstack, offset); | |
3372 | ||
3373 | /* B<C<char> >, not B<C<char>> */ | |
3374 | if (bufp[offset - 1] == '>') | |
3375 | ccat (' '); | |
3376 | } | |
3377 | ccat ('>'); | |
3378 | ccat ('\0'); | |
3379 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
3380 | } |
3381 | ||
bd6dd845 | 3382 | static tree |
5566b478 MS |
3383 | classtype_mangled_name (t) |
3384 | tree t; | |
3385 | { | |
3386 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
3387 | /* Specializations have already had their names set up in |
3388 | lookup_template_class. */ | |
9fbf56f7 MM |
3389 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
3390 | { | |
3391 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
3392 | ||
36a117a5 MM |
3393 | /* For non-primary templates, the template parameters are |
3394 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
3395 | if (PRIMARY_TEMPLATE_P (tmpl)) |
3396 | { | |
3397 | tree name = DECL_NAME (tmpl); | |
3398 | char *mangled_name = mangle_class_name_for_template | |
3399 | (IDENTIFIER_POINTER (name), | |
3400 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
3401 | CLASSTYPE_TI_ARGS (t)); | |
3402 | tree id = get_identifier (mangled_name); | |
3403 | IDENTIFIER_TEMPLATE (id) = name; | |
3404 | return id; | |
3405 | } | |
5566b478 | 3406 | } |
9fbf56f7 MM |
3407 | |
3408 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
3409 | } |
3410 | ||
3411 | static void | |
3412 | add_pending_template (d) | |
3413 | tree d; | |
3414 | { | |
e92cc029 MS |
3415 | tree ti; |
3416 | ||
3417 | if (TREE_CODE_CLASS (TREE_CODE (d)) == 't') | |
3418 | ti = CLASSTYPE_TEMPLATE_INFO (d); | |
3419 | else | |
3420 | ti = DECL_TEMPLATE_INFO (d); | |
3421 | ||
824b9a4c | 3422 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
3423 | return; |
3424 | ||
3425 | *template_tail = perm_tree_cons | |
1139b3d8 | 3426 | (build_srcloc_here (), d, NULL_TREE); |
5566b478 | 3427 | template_tail = &TREE_CHAIN (*template_tail); |
824b9a4c | 3428 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
5566b478 MS |
3429 | } |
3430 | ||
386b8a85 JM |
3431 | |
3432 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which | |
3433 | may be either a _DECL or an overloaded function or an | |
3434 | IDENTIFIER_NODE), and ARGLIST. */ | |
3435 | ||
3436 | tree | |
3437 | lookup_template_function (fns, arglist) | |
3438 | tree fns, arglist; | |
3439 | { | |
2c73f9f5 | 3440 | tree type; |
050367a3 | 3441 | |
386b8a85 JM |
3442 | if (fns == NULL_TREE) |
3443 | { | |
8251199e | 3444 | cp_error ("non-template used as template"); |
386b8a85 JM |
3445 | return error_mark_node; |
3446 | } | |
3447 | ||
2c73f9f5 ML |
3448 | type = TREE_TYPE (fns); |
3449 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
3450 | type = unknown_type_node; | |
3451 | ||
672476cb MM |
3452 | if (processing_template_decl) |
3453 | return build_min (TEMPLATE_ID_EXPR, type, fns, arglist); | |
3454 | else | |
3455 | return build (TEMPLATE_ID_EXPR, type, fns, arglist); | |
386b8a85 JM |
3456 | } |
3457 | ||
a2b60a0e MM |
3458 | /* Within the scope of a template class S<T>, the name S gets bound |
3459 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
3460 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
3461 | or one of its enclosing classes, and that type is a template, | |
3462 | return the associated TEMPLATE_DECL. Otherwise, the original | |
3463 | DECL is returned. */ | |
3464 | ||
e9659ab0 | 3465 | static tree |
a2b60a0e MM |
3466 | maybe_get_template_decl_from_type_decl (decl) |
3467 | tree decl; | |
3468 | { | |
3469 | return (decl != NULL_TREE | |
3470 | && TREE_CODE (decl) == TYPE_DECL | |
3471 | && DECL_ARTIFICIAL (decl) | |
3472 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) | |
3473 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
3474 | } | |
386b8a85 | 3475 | |
8d08fdba MS |
3476 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
3477 | parameters, find the desired type. | |
3478 | ||
3479 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
36a117a5 MM |
3480 | (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will |
3481 | be a TREE_LIST if called directly from the parser, and a TREE_VEC | |
3482 | otherwise.) Since ARGLIST is build on the decl_obstack, we must | |
3483 | copy it here to keep it from being reclaimed when the decl storage | |
3484 | is reclaimed. | |
8d08fdba MS |
3485 | |
3486 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
3487 | instantiate. |
3488 | ||
36a117a5 MM |
3489 | If ENTERING_SCOPE is non-zero, we are about to enter the scope of |
3490 | the class we are looking up. | |
3491 | ||
75650646 MM |
3492 | If the template class is really a local class in a template |
3493 | function, then the FUNCTION_CONTEXT is the function in which it is | |
3494 | being instantiated. */ | |
e92cc029 | 3495 | |
8d08fdba | 3496 | tree |
36a117a5 | 3497 | lookup_template_class (d1, arglist, in_decl, context, entering_scope) |
8d08fdba MS |
3498 | tree d1, arglist; |
3499 | tree in_decl; | |
e1467ff2 | 3500 | tree context; |
36a117a5 | 3501 | int entering_scope; |
8d08fdba | 3502 | { |
a703fb38 | 3503 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 3504 | tree t; |
5566b478 MS |
3505 | |
3506 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
3507 | { | |
f181d4ae MM |
3508 | if (IDENTIFIER_VALUE (d1) |
3509 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) | |
3510 | template = IDENTIFIER_VALUE (d1); | |
73b0fce8 KL |
3511 | else |
3512 | { | |
2c73f9f5 ML |
3513 | if (context) |
3514 | push_decl_namespace (context); | |
67ffc812 MM |
3515 | if (current_class_type != NULL_TREE) |
3516 | template = | |
3517 | maybe_get_template_decl_from_type_decl | |
3518 | (IDENTIFIER_CLASS_VALUE (d1)); | |
a2b60a0e | 3519 | if (template == NULL_TREE) |
2c73f9f5 ML |
3520 | template = lookup_name_nonclass (d1); |
3521 | if (context) | |
3522 | pop_decl_namespace (); | |
73b0fce8 | 3523 | } |
8d019cef JM |
3524 | if (template) |
3525 | context = DECL_CONTEXT (template); | |
5566b478 | 3526 | } |
c91a56d2 MS |
3527 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
3528 | { | |
f3400fe2 JM |
3529 | if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1))) |
3530 | { | |
3531 | template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1)); | |
3532 | d1 = DECL_NAME (template); | |
3533 | } | |
c91a56d2 | 3534 | } |
ed44da02 MM |
3535 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
3536 | || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' | |
3537 | && IS_AGGR_TYPE (d1))) | |
5566b478 | 3538 | { |
ed44da02 | 3539 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
3540 | d1 = DECL_NAME (template); |
3541 | } | |
93cdc044 JM |
3542 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
3543 | && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL) | |
3544 | { | |
3545 | template = d1; | |
3546 | d1 = DECL_NAME (template); | |
3547 | context = DECL_CONTEXT (template); | |
3548 | } | |
5566b478 MS |
3549 | else |
3550 | my_friendly_abort (272); | |
8d08fdba | 3551 | |
8d08fdba | 3552 | /* With something like `template <class T> class X class X { ... };' |
f181d4ae MM |
3553 | we could end up with D1 having nothing but an IDENTIFIER_VALUE. |
3554 | We don't want to do that, but we have to deal with the situation, | |
3555 | so let's give them some syntax errors to chew on instead of a | |
3556 | crash. */ | |
8d08fdba | 3557 | if (! template) |
f3400fe2 JM |
3558 | { |
3559 | cp_error ("`%T' is not a template", d1); | |
3560 | return error_mark_node; | |
3561 | } | |
2c73f9f5 | 3562 | |
c3baf4b5 JM |
3563 | if (context == NULL_TREE) |
3564 | context = global_namespace; | |
2c73f9f5 | 3565 | |
8d08fdba MS |
3566 | if (TREE_CODE (template) != TEMPLATE_DECL) |
3567 | { | |
8251199e | 3568 | cp_error ("non-template type `%T' used as a template", d1); |
8d08fdba | 3569 | if (in_decl) |
8251199e | 3570 | cp_error_at ("for template declaration `%D'", in_decl); |
8d08fdba MS |
3571 | return error_mark_node; |
3572 | } | |
8d08fdba | 3573 | |
73b0fce8 KL |
3574 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
3575 | { | |
3576 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
3577 | template arguments */ | |
3578 | ||
3579 | tree parm = copy_template_template_parm (TREE_TYPE (template)); | |
3580 | tree template2 = TYPE_STUB_DECL (parm); | |
3581 | tree arglist2; | |
3582 | ||
73b0fce8 KL |
3583 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
3584 | ||
744fac59 | 3585 | arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1); |
73b0fce8 KL |
3586 | if (arglist2 == error_mark_node) |
3587 | return error_mark_node; | |
3588 | ||
3589 | arglist2 = copy_to_permanent (arglist2); | |
7ddedda4 | 3590 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm) |
73b0fce8 KL |
3591 | = perm_tree_cons (template2, arglist2, NULL_TREE); |
3592 | TYPE_SIZE (parm) = 0; | |
3593 | return parm; | |
3594 | } | |
36a117a5 | 3595 | else |
8d08fdba | 3596 | { |
36a117a5 MM |
3597 | tree template_type = TREE_TYPE (template); |
3598 | tree type_decl; | |
3599 | tree found = NULL_TREE; | |
3600 | int arg_depth; | |
3601 | int parm_depth; | |
dbfe2124 | 3602 | int is_partial_instantiation; |
830bfa74 | 3603 | |
36a117a5 MM |
3604 | template = most_general_template (template); |
3605 | parmlist = DECL_TEMPLATE_PARMS (template); | |
3606 | parm_depth = TMPL_PARMS_DEPTH (parmlist); | |
3607 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3608 | ||
7ddedda4 MM |
3609 | /* We build up the coerced arguments and such on the |
3610 | momentary_obstack. */ | |
3611 | push_momentary (); | |
3612 | ||
36a117a5 MM |
3613 | if (arg_depth == 1 && parm_depth > 1) |
3614 | { | |
39c01e4c | 3615 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
3616 | For example, given: |
3617 | ||
3618 | template <class T> struct S1 { | |
3619 | template <class U> struct S2 {}; | |
3620 | template <class U> struct S2<U*> {}; | |
3621 | }; | |
3622 | ||
3623 | we will be called with an ARGLIST of `U*', but the | |
3624 | TEMPLATE will be `template <class T> template | |
3625 | <class U> struct S1<T>::S2'. We must fill in the missing | |
3626 | arguments. */ | |
3627 | my_friendly_assert (context != NULL_TREE, 0); | |
39c01e4c MM |
3628 | while (!IS_AGGR_TYPE_CODE (TREE_CODE (context)) |
3629 | && context != global_namespace) | |
36a117a5 | 3630 | context = DECL_REAL_CONTEXT (context); |
39c01e4c MM |
3631 | |
3632 | if (context == global_namespace) | |
3633 | /* This is bad. We cannot get enough arguments, even from | |
3634 | the surrounding context, to resolve this class. One | |
3635 | case where this might happen is (illegal) code like: | |
3636 | ||
3637 | template <class U> | |
3638 | template <class T> | |
3639 | struct S { | |
3640 | A(const A<T>& a) {} | |
3641 | }; | |
3642 | ||
3643 | We should catch this error sooner (at the opening curly | |
3644 | for `S', but it is better to be safe than sorry here. */ | |
3645 | { | |
8251199e | 3646 | cp_error ("invalid use of `%D'", template); |
39c01e4c MM |
3647 | return error_mark_node; |
3648 | } | |
3649 | ||
ed44da02 | 3650 | arglist = add_to_template_args (TYPE_TI_ARGS (context), |
36a117a5 MM |
3651 | arglist); |
3652 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
3653 | } | |
5566b478 | 3654 | |
36a117a5 MM |
3655 | my_friendly_assert (parm_depth == arg_depth, 0); |
3656 | ||
3657 | /* Calculate the BOUND_ARGS. These will be the args that are | |
3658 | actually tsubst'd into the definition to create the | |
3659 | instantiation. */ | |
3660 | if (parm_depth > 1) | |
830bfa74 MM |
3661 | { |
3662 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 3663 | int i; |
e4a84209 | 3664 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 3665 | |
cabc336a | 3666 | tree bound_args = make_temp_vec (parm_depth); |
830bfa74 | 3667 | |
e4a84209 | 3668 | for (i = saved_depth, |
830bfa74 | 3669 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 3670 | i > 0 && t != NULL_TREE; |
830bfa74 | 3671 | --i, t = TREE_CHAIN (t)) |
e4a84209 MM |
3672 | { |
3673 | tree a = coerce_template_parms (TREE_VALUE (t), | |
3674 | arglist, template, 1, 1); | |
3675 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); | |
3676 | ||
3677 | /* We temporarily reduce the length of the ARGLIST so | |
3678 | that coerce_template_parms will see only the arguments | |
3679 | corresponding to the template parameters it is | |
3680 | examining. */ | |
3681 | TREE_VEC_LENGTH (arglist)--; | |
3682 | } | |
3683 | ||
3684 | /* Restore the ARGLIST to its full size. */ | |
3685 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
3686 | ||
36a117a5 | 3687 | arglist = bound_args; |
830bfa74 MM |
3688 | } |
3689 | else | |
36a117a5 MM |
3690 | arglist |
3691 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
3692 | innermost_args (arglist), | |
3693 | template, 1, 1); | |
3694 | ||
3695 | if (arglist == error_mark_node) | |
3696 | /* We were unable to bind the arguments. */ | |
5566b478 | 3697 | return error_mark_node; |
5566b478 | 3698 | |
36a117a5 MM |
3699 | /* In the scope of a template class, explicit references to the |
3700 | template class refer to the type of the template, not any | |
3701 | instantiation of it. For example, in: | |
3702 | ||
3703 | template <class T> class C { void f(C<T>); } | |
3704 | ||
3705 | the `C<T>' is just the same as `C'. Outside of the | |
3706 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 3707 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
3708 | arglist)) |
3709 | { | |
3710 | found = template_type; | |
3711 | ||
3712 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 3713 | { |
36a117a5 MM |
3714 | tree ctx; |
3715 | ||
3716 | /* Note that we use DECL_CONTEXT, rather than | |
3717 | CP_DECL_CONTEXT, so that the termination test is | |
3718 | always just `ctx'. We're not interested in namepace | |
3719 | scopes. */ | |
3720 | for (ctx = current_class_type; | |
3721 | ctx; | |
3722 | ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't') | |
3723 | ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) | |
3bfdc719 | 3724 | if (same_type_p (ctx, template_type)) |
36a117a5 MM |
3725 | break; |
3726 | ||
3727 | if (!ctx) | |
3728 | /* We're not in the scope of the class, so the | |
3729 | TEMPLATE_TYPE is not the type we want after | |
3730 | all. */ | |
3731 | found = NULL_TREE; | |
5566b478 MS |
3732 | } |
3733 | } | |
36a117a5 MM |
3734 | |
3735 | if (!found) | |
75650646 | 3736 | { |
36a117a5 MM |
3737 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); |
3738 | found; found = TREE_CHAIN (found)) | |
3739 | if (comp_template_args (TREE_PURPOSE (found), arglist)) | |
3740 | break; | |
75650646 | 3741 | |
36a117a5 MM |
3742 | if (found) |
3743 | found = TREE_VALUE (found); | |
8d08fdba | 3744 | } |
36a117a5 MM |
3745 | |
3746 | if (found) | |
8d08fdba | 3747 | { |
7ddedda4 | 3748 | pop_momentary (); |
36a117a5 | 3749 | return found; |
8d08fdba | 3750 | } |
5566b478 | 3751 | |
36a117a5 MM |
3752 | /* Since we didn't find the type, we'll have to create it. |
3753 | Since we'll be saving this type on the | |
3754 | DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */ | |
3755 | push_obstacks (&permanent_obstack, &permanent_obstack); | |
3756 | ||
dbfe2124 | 3757 | /* This type is a "partial instantiation" if any of the template |
486e4077 MM |
3758 | arguments still inolve template parameters. Note that we set |
3759 | IS_PARTIAL_INSTANTIATION for partial specializations as | |
3760 | well. */ | |
dbfe2124 MM |
3761 | is_partial_instantiation = uses_template_parms (arglist); |
3762 | ||
36a117a5 | 3763 | /* Create the type. */ |
ed44da02 MM |
3764 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
3765 | { | |
dbfe2124 | 3766 | if (!is_partial_instantiation) |
61a127b3 | 3767 | t = start_enum (TYPE_IDENTIFIER (template_type)); |
ed44da02 | 3768 | else |
dbfe2124 | 3769 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
3770 | the values for the enumeration constants may involve |
3771 | template parameters. And, no one should be interested | |
3772 | in the enumeration constants for such a type. */ | |
3773 | t = make_node (ENUMERAL_TYPE); | |
3774 | } | |
3775 | else | |
3776 | { | |
3777 | t = make_lang_type (TREE_CODE (template_type)); | |
3778 | CLASSTYPE_DECLARED_CLASS (t) | |
3779 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
3780 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
3781 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
f668f160 | 3782 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ed44da02 MM |
3783 | } |
3784 | ||
61a127b3 | 3785 | /* If we called start_enum above, this information will already |
ed44da02 MM |
3786 | be set up. */ |
3787 | if (!TYPE_NAME (t)) | |
3788 | { | |
3789 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 3790 | |
ed44da02 MM |
3791 | /* Create a stub TYPE_DECL for it. */ |
3792 | type_decl = build_decl (TYPE_DECL, DECL_NAME (template), t); | |
3793 | SET_DECL_ARTIFICIAL (type_decl); | |
3794 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); | |
3795 | DECL_SOURCE_FILE (type_decl) | |
3796 | = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type)); | |
3797 | DECL_SOURCE_LINE (type_decl) | |
3798 | = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type)); | |
3799 | TYPE_STUB_DECL (t) = TYPE_NAME (t) = type_decl; | |
3800 | } | |
3801 | else | |
3802 | type_decl = TYPE_NAME (t); | |
36a117a5 | 3803 | |
9fbf56f7 MM |
3804 | /* Set up the template information. We have to figure out which |
3805 | template is the immediate parent if this is a full | |
3806 | instantiation. */ | |
3807 | if (parm_depth == 1 || is_partial_instantiation | |
3808 | || !PRIMARY_TEMPLATE_P (template)) | |
3809 | /* This case is easy; there are no member templates involved. */ | |
3810 | found = template; | |
3811 | else | |
3812 | { | |
3813 | /* This is a full instantiation of a member template. There | |
3814 | should be some partial instantiation of which this is an | |
3815 | instance. */ | |
3816 | ||
3817 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
3818 | found; found = TREE_CHAIN (found)) | |
3819 | { | |
3820 | int success; | |
3821 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
3822 | ||
3823 | /* We only want partial instantiations, here, not | |
3824 | specializations or full instantiations. */ | |
3825 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) | |
3826 | || !uses_template_parms (TREE_VALUE (found))) | |
3827 | continue; | |
3828 | ||
3829 | /* Temporarily reduce by one the number of levels in the | |
3830 | ARGLIST and in FOUND so as to avoid comparing the | |
3831 | last set of arguments. */ | |
3832 | TREE_VEC_LENGTH (arglist)--; | |
3833 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
3834 | ||
3835 | /* See if the arguments match. If they do, then TMPL is | |
3836 | the partial instantiation we want. */ | |
3837 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
3838 | ||
3839 | /* Restore the argument vectors to their full size. */ | |
3840 | TREE_VEC_LENGTH (arglist)++; | |
3841 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
3842 | ||
3843 | if (success) | |
3844 | { | |
3845 | found = tmpl; | |
3846 | break; | |
3847 | } | |
3848 | } | |
3849 | ||
3850 | if (!found) | |
3851 | my_friendly_abort (0); | |
3852 | } | |
3853 | ||
5566b478 | 3854 | arglist = copy_to_permanent (arglist); |
ed44da02 | 3855 | SET_TYPE_TEMPLATE_INFO (t, |
9fbf56f7 | 3856 | tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
3857 | DECL_TEMPLATE_INSTANTIATIONS (template) |
3858 | = tree_cons (arglist, t, | |
3859 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
3860 | ||
3861 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
3862 | && !is_partial_instantiation) | |
61a127b3 MM |
3863 | /* Now that the type has been registered on the instantiations |
3864 | list, we set up the enumerators. Because the enumeration | |
3865 | constants may involve the enumeration type itself, we make | |
3866 | sure to register the type first, and then create the | |
3867 | constants. That way, doing tsubst_expr for the enumeration | |
3868 | constants won't result in recursive calls here; we'll find | |
3869 | the instantiation and exit above. */ | |
3870 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 MM |
3871 | |
3872 | /* We're done with the permanent obstack, now. */ | |
3873 | pop_obstacks (); | |
7ddedda4 MM |
3874 | /* We're also done with the momentary allocation we started |
3875 | above. */ | |
3876 | pop_momentary (); | |
5566b478 | 3877 | |
36a117a5 MM |
3878 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
3879 | is set up. */ | |
ed44da02 MM |
3880 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
3881 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
36a117a5 | 3882 | DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl); |
dbfe2124 | 3883 | if (!is_partial_instantiation) |
36a117a5 MM |
3884 | { |
3885 | DECL_ASSEMBLER_NAME (type_decl) | |
3886 | = get_identifier (build_overload_name (t, 1, 1)); | |
50a6dbd7 JM |
3887 | |
3888 | /* For backwards compatibility; code that uses | |
3889 | -fexternal-templates expects looking up a template to | |
3890 | instantiate it. I think DDD still relies on this. | |
3891 | (jason 8/20/1998) */ | |
ed44da02 MM |
3892 | if (TREE_CODE (t) != ENUMERAL_TYPE |
3893 | && flag_external_templates | |
36a117a5 MM |
3894 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) |
3895 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
3896 | add_pending_template (t); | |
3897 | } | |
3898 | else | |
077e7015 MM |
3899 | /* If the type makes use of template parameters, the |
3900 | code that generates debugging information will crash. */ | |
3901 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 3902 | |
36a117a5 MM |
3903 | return t; |
3904 | } | |
8d08fdba MS |
3905 | } |
3906 | \f | |
050367a3 MM |
3907 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or |
3908 | TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA. | |
3909 | If FN returns non-zero, the iteration is terminated, and | |
3910 | for_each_template_parm returns 1. Otherwise, the iteration | |
3911 | continues. If FN never returns a non-zero value, the value | |
3912 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
3913 | considered to be the function which always returns 1. */ | |
3914 | ||
e9659ab0 | 3915 | static int |
050367a3 | 3916 | for_each_template_parm (t, fn, data) |
8d08fdba | 3917 | tree t; |
050367a3 MM |
3918 | tree_fn_t fn; |
3919 | void* data; | |
8d08fdba MS |
3920 | { |
3921 | if (!t) | |
3922 | return 0; | |
581d38d0 MM |
3923 | |
3924 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't' | |
3925 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data)) | |
3926 | return 1; | |
3927 | ||
8d08fdba MS |
3928 | switch (TREE_CODE (t)) |
3929 | { | |
3930 | case INDIRECT_REF: | |
3931 | case COMPONENT_REF: | |
3932 | /* We assume that the object must be instantiated in order to build | |
3933 | the COMPONENT_REF, so we test only whether the type of the | |
3934 | COMPONENT_REF uses template parms. */ | |
050367a3 | 3935 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 3936 | |
297e73d8 MM |
3937 | case ARRAY_REF: |
3938 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) | |
3939 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
3940 | ||
8d08fdba MS |
3941 | case IDENTIFIER_NODE: |
3942 | if (!IDENTIFIER_TEMPLATE (t)) | |
3943 | return 0; | |
5566b478 | 3944 | my_friendly_abort (42); |
8d08fdba MS |
3945 | |
3946 | /* aggregates of tree nodes */ | |
3947 | case TREE_VEC: | |
3948 | { | |
3949 | int i = TREE_VEC_LENGTH (t); | |
3950 | while (i--) | |
050367a3 | 3951 | if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data)) |
8d08fdba MS |
3952 | return 1; |
3953 | return 0; | |
3954 | } | |
3955 | case TREE_LIST: | |
050367a3 MM |
3956 | if (for_each_template_parm (TREE_PURPOSE (t), fn, data) |
3957 | || for_each_template_parm (TREE_VALUE (t), fn, data)) | |
8d08fdba | 3958 | return 1; |
050367a3 | 3959 | return for_each_template_parm (TREE_CHAIN (t), fn, data); |
8d08fdba | 3960 | |
2c73f9f5 ML |
3961 | case OVERLOAD: |
3962 | if (for_each_template_parm (OVL_FUNCTION (t), fn, data)) | |
3963 | return 1; | |
3964 | return for_each_template_parm (OVL_CHAIN (t), fn, data); | |
3965 | ||
8d08fdba MS |
3966 | /* constructed type nodes */ |
3967 | case POINTER_TYPE: | |
3968 | case REFERENCE_TYPE: | |
050367a3 | 3969 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
ed44da02 | 3970 | |
8d08fdba | 3971 | case RECORD_TYPE: |
b7484fbe | 3972 | if (TYPE_PTRMEMFUNC_FLAG (t)) |
050367a3 MM |
3973 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t), |
3974 | fn, data); | |
ed44da02 MM |
3975 | /* Fall through. */ |
3976 | ||
8d08fdba | 3977 | case UNION_TYPE: |
ed44da02 MM |
3978 | case ENUMERAL_TYPE: |
3979 | if (! TYPE_TEMPLATE_INFO (t)) | |
8d08fdba | 3980 | return 0; |
050367a3 | 3981 | return for_each_template_parm (TREE_VALUE |
ed44da02 | 3982 | (TYPE_TEMPLATE_INFO (t)), |
050367a3 | 3983 | fn, data); |
588c2d1c MM |
3984 | case METHOD_TYPE: |
3985 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data)) | |
8d08fdba | 3986 | return 1; |
588c2d1c MM |
3987 | /* Fall through. */ |
3988 | ||
3989 | case FUNCTION_TYPE: | |
3990 | /* Check the parameter types. Since default arguments are not | |
3991 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
3992 | contain expressions that involve template parameters. But, | |
3993 | no-one should be looking at them yet. And, once they're | |
3994 | instantiated, they don't contain template parameters, so | |
3995 | there's no point in looking at them then, either. */ | |
3996 | { | |
3997 | tree parm; | |
3998 | ||
3999 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4000 | if (for_each_template_parm (TREE_VALUE (parm), fn, data)) | |
4001 | return 1; | |
4002 | } | |
4003 | ||
4004 | /* Check the return type, too. */ | |
050367a3 | 4005 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
588c2d1c | 4006 | |
8d08fdba | 4007 | case ARRAY_TYPE: |
050367a3 | 4008 | if (for_each_template_parm (TYPE_DOMAIN (t), fn, data)) |
8d08fdba | 4009 | return 1; |
050367a3 | 4010 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba | 4011 | case OFFSET_TYPE: |
050367a3 | 4012 | if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data)) |
8d08fdba | 4013 | return 1; |
050367a3 | 4014 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
8d08fdba MS |
4015 | |
4016 | /* decl nodes */ | |
4017 | case TYPE_DECL: | |
050367a3 | 4018 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
5566b478 | 4019 | |
73b0fce8 KL |
4020 | case TEMPLATE_DECL: |
4021 | /* A template template parameter is encountered */ | |
4022 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
744fac59 KL |
4023 | return for_each_template_parm (TREE_TYPE (t), fn, data); |
4024 | /* Already substituted template template parameter */ | |
73b0fce8 KL |
4025 | return 0; |
4026 | ||
3ac3d9ea | 4027 | case CONST_DECL: |
050367a3 | 4028 | if (for_each_template_parm (DECL_INITIAL (t), fn, data)) |
3ac3d9ea MM |
4029 | return 1; |
4030 | goto check_type_and_context; | |
4031 | ||
8d08fdba | 4032 | case FUNCTION_DECL: |
5566b478 MS |
4033 | case VAR_DECL: |
4034 | /* ??? What about FIELD_DECLs? */ | |
4035 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) | |
050367a3 | 4036 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data)) |
8d08fdba MS |
4037 | return 1; |
4038 | /* fall through */ | |
8d08fdba | 4039 | case PARM_DECL: |
3ac3d9ea | 4040 | check_type_and_context: |
050367a3 | 4041 | if (for_each_template_parm (TREE_TYPE (t), fn, data)) |
5566b478 | 4042 | return 1; |
050367a3 MM |
4043 | if (DECL_CONTEXT (t) |
4044 | && for_each_template_parm (DECL_CONTEXT (t), fn, data)) | |
8d08fdba | 4045 | return 1; |
8d08fdba MS |
4046 | return 0; |
4047 | ||
4048 | case CALL_EXPR: | |
297e73d8 MM |
4049 | return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data) |
4050 | || for_each_template_parm (TREE_OPERAND (t, 1), fn, data)); | |
4051 | ||
8d08fdba | 4052 | case ADDR_EXPR: |
050367a3 | 4053 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
8d08fdba MS |
4054 | |
4055 | /* template parm nodes */ | |
73b0fce8 | 4056 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4057 | /* Record template parameters such as `T' inside `TT<T>'. */ |
7ddedda4 MM |
4058 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t) |
4059 | && for_each_template_parm (TYPE_TI_ARGS (t), fn, data)) | |
744fac59 KL |
4060 | return 1; |
4061 | case TEMPLATE_TYPE_PARM: | |
f84b4be9 | 4062 | case TEMPLATE_PARM_INDEX: |
050367a3 MM |
4063 | if (fn) |
4064 | return (*fn)(t, data); | |
4065 | else | |
4066 | return 1; | |
8d08fdba MS |
4067 | |
4068 | /* simple type nodes */ | |
4069 | case INTEGER_TYPE: | |
050367a3 | 4070 | if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data)) |
8d08fdba | 4071 | return 1; |
050367a3 | 4072 | return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data); |
8d08fdba MS |
4073 | |
4074 | case REAL_TYPE: | |
37c46b43 | 4075 | case COMPLEX_TYPE: |
8d08fdba | 4076 | case VOID_TYPE: |
2986ae00 | 4077 | case BOOLEAN_TYPE: |
2c73f9f5 | 4078 | case NAMESPACE_DECL: |
8d08fdba MS |
4079 | return 0; |
4080 | ||
4081 | /* constants */ | |
4082 | case INTEGER_CST: | |
4083 | case REAL_CST: | |
4084 | case STRING_CST: | |
4085 | return 0; | |
4086 | ||
4087 | case ERROR_MARK: | |
4088 | /* Non-error_mark_node ERROR_MARKs are bad things. */ | |
4089 | my_friendly_assert (t == error_mark_node, 274); | |
4090 | /* NOTREACHED */ | |
4091 | return 0; | |
4092 | ||
ec255269 | 4093 | case LOOKUP_EXPR: |
5566b478 | 4094 | case TYPENAME_TYPE: |
8d08fdba MS |
4095 | return 1; |
4096 | ||
61a127b3 MM |
4097 | case PTRMEM_CST: |
4098 | return for_each_template_parm (TREE_TYPE (t), fn, data); | |
4099 | ||
5156628f | 4100 | case SCOPE_REF: |
050367a3 | 4101 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
5156628f | 4102 | |
db5ae43f MS |
4103 | case CONSTRUCTOR: |
4104 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))) | |
050367a3 MM |
4105 | return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE |
4106 | (TREE_TYPE (t)), fn, data); | |
4107 | return for_each_template_parm (TREE_OPERAND (t, 1), fn, data); | |
db5ae43f | 4108 | |
42976354 BK |
4109 | case MODOP_EXPR: |
4110 | case CAST_EXPR: | |
4111 | case REINTERPRET_CAST_EXPR: | |
4112 | case CONST_CAST_EXPR: | |
4113 | case STATIC_CAST_EXPR: | |
4114 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
4115 | case ARROW_EXPR: |
4116 | case DOTSTAR_EXPR: | |
4117 | case TYPEID_EXPR: | |
4118 | return 1; | |
4119 | ||
abff8e06 JM |
4120 | case SIZEOF_EXPR: |
4121 | case ALIGNOF_EXPR: | |
050367a3 | 4122 | return for_each_template_parm (TREE_OPERAND (t, 0), fn, data); |
abff8e06 | 4123 | |
8d08fdba MS |
4124 | default: |
4125 | switch (TREE_CODE_CLASS (TREE_CODE (t))) | |
4126 | { | |
4127 | case '1': | |
4128 | case '2': | |
ec255269 | 4129 | case 'e': |
8d08fdba MS |
4130 | case '<': |
4131 | { | |
4132 | int i; | |
bbc06b4b | 4133 | for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;) |
050367a3 | 4134 | if (for_each_template_parm (TREE_OPERAND (t, i), fn, data)) |
8d08fdba MS |
4135 | return 1; |
4136 | return 0; | |
4137 | } | |
4138 | default: | |
4139 | break; | |
4140 | } | |
4141 | sorry ("testing %s for template parms", | |
4142 | tree_code_name [(int) TREE_CODE (t)]); | |
4143 | my_friendly_abort (82); | |
4144 | /* NOTREACHED */ | |
4145 | return 0; | |
4146 | } | |
4147 | } | |
4148 | ||
050367a3 MM |
4149 | int |
4150 | uses_template_parms (t) | |
4151 | tree t; | |
4152 | { | |
4153 | return for_each_template_parm (t, 0, 0); | |
4154 | } | |
4155 | ||
27fafc8d JM |
4156 | static struct tinst_level *current_tinst_level; |
4157 | static struct tinst_level *free_tinst_level; | |
4158 | static int tinst_depth; | |
e9f32eb5 | 4159 | extern int max_tinst_depth; |
5566b478 | 4160 | #ifdef GATHER_STATISTICS |
27fafc8d | 4161 | int depth_reached; |
5566b478 | 4162 | #endif |
27fafc8d JM |
4163 | int tinst_level_tick; |
4164 | int last_template_error_tick; | |
8d08fdba | 4165 | |
7bae46f4 | 4166 | /* Print out all the template instantiations that we are currently |
27fafc8d JM |
4167 | working on. If ERR, we are being called from cp_thing, so do |
4168 | the right thing for an error message. */ | |
7bae46f4 | 4169 | |
27fafc8d JM |
4170 | static void |
4171 | print_template_context (err) | |
4172 | int err; | |
7bae46f4 JM |
4173 | { |
4174 | struct tinst_level *p = current_tinst_level; | |
4175 | int line = lineno; | |
4176 | char *file = input_filename; | |
4177 | ||
49e04385 | 4178 | if (err && p) |
27fafc8d | 4179 | { |
49e04385 MM |
4180 | if (current_function_decl != p->decl |
4181 | && current_function_decl != NULL_TREE) | |
4182 | /* We can get here during the processing of some synthesized | |
4183 | method. Then, p->decl will be the function that's causing | |
4184 | the synthesis. */ | |
4185 | ; | |
27fafc8d | 4186 | else |
27fafc8d | 4187 | { |
49e04385 MM |
4188 | if (current_function_decl == p->decl) |
4189 | /* Avoid redundancy with the the "In function" line. */; | |
4190 | else | |
4191 | fprintf (stderr, "%s: In instantiation of `%s':\n", | |
4192 | file, decl_as_string (p->decl, 0)); | |
4193 | ||
038fb86f JM |
4194 | line = p->line; |
4195 | file = p->file; | |
27fafc8d JM |
4196 | p = p->next; |
4197 | } | |
4198 | } | |
4199 | ||
7bae46f4 JM |
4200 | for (; p; p = p->next) |
4201 | { | |
038fb86f JM |
4202 | fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line, |
4203 | decl_as_string (p->decl, 0)); | |
4204 | line = p->line; | |
4205 | file = p->file; | |
7bae46f4 | 4206 | } |
038fb86f | 4207 | fprintf (stderr, "%s:%d: instantiated from here\n", file, line); |
7bae46f4 JM |
4208 | } |
4209 | ||
27fafc8d JM |
4210 | /* Called from cp_thing to print the template context for an error. */ |
4211 | ||
4212 | void | |
4213 | maybe_print_template_context () | |
4214 | { | |
4215 | if (last_template_error_tick == tinst_level_tick | |
4216 | || current_tinst_level == 0) | |
4217 | return; | |
4218 | ||
4219 | last_template_error_tick = tinst_level_tick; | |
4220 | print_template_context (1); | |
4221 | } | |
4222 | ||
bd6dd845 | 4223 | static int |
5566b478 MS |
4224 | push_tinst_level (d) |
4225 | tree d; | |
8d08fdba MS |
4226 | { |
4227 | struct tinst_level *new; | |
8d08fdba | 4228 | |
7215f9a0 MS |
4229 | if (tinst_depth >= max_tinst_depth) |
4230 | { | |
8adf5b5e JM |
4231 | /* If the instantiation in question still has unbound template parms, |
4232 | we don't really care if we can't instantiate it, so just return. | |
4233 | This happens with base instantiation for implicit `typename'. */ | |
4234 | if (uses_template_parms (d)) | |
4235 | return 0; | |
4236 | ||
1139b3d8 | 4237 | last_template_error_tick = tinst_level_tick; |
8251199e | 4238 | error ("template instantiation depth exceeds maximum of %d", |
7215f9a0 | 4239 | max_tinst_depth); |
8251199e JM |
4240 | error (" (use -ftemplate-depth-NN to increase the maximum)"); |
4241 | cp_error (" instantiating `%D'", d); | |
5566b478 | 4242 | |
27fafc8d | 4243 | print_template_context (0); |
5566b478 | 4244 | |
7215f9a0 MS |
4245 | return 0; |
4246 | } | |
4247 | ||
8d08fdba MS |
4248 | if (free_tinst_level) |
4249 | { | |
4250 | new = free_tinst_level; | |
4251 | free_tinst_level = new->next; | |
4252 | } | |
4253 | else | |
4254 | new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level)); | |
4255 | ||
5566b478 MS |
4256 | new->decl = d; |
4257 | new->line = lineno; | |
4258 | new->file = input_filename; | |
8d08fdba MS |
4259 | new->next = current_tinst_level; |
4260 | current_tinst_level = new; | |
5566b478 | 4261 | |
7215f9a0 | 4262 | ++tinst_depth; |
5566b478 MS |
4263 | #ifdef GATHER_STATISTICS |
4264 | if (tinst_depth > depth_reached) | |
4265 | depth_reached = tinst_depth; | |
4266 | #endif | |
4267 | ||
27fafc8d | 4268 | ++tinst_level_tick; |
7215f9a0 | 4269 | return 1; |
8d08fdba MS |
4270 | } |
4271 | ||
4272 | void | |
4273 | pop_tinst_level () | |
4274 | { | |
4275 | struct tinst_level *old = current_tinst_level; | |
4276 | ||
ae58fa02 MM |
4277 | /* Restore the filename and line number stashed away when we started |
4278 | this instantiation. */ | |
4279 | lineno = old->line; | |
4280 | input_filename = old->file; | |
5f2c99c4 | 4281 | extract_interface_info (); |
ae58fa02 | 4282 | |
8d08fdba MS |
4283 | current_tinst_level = old->next; |
4284 | old->next = free_tinst_level; | |
4285 | free_tinst_level = old; | |
7215f9a0 | 4286 | --tinst_depth; |
27fafc8d | 4287 | ++tinst_level_tick; |
8d08fdba MS |
4288 | } |
4289 | ||
4290 | struct tinst_level * | |
4291 | tinst_for_decl () | |
4292 | { | |
4293 | struct tinst_level *p = current_tinst_level; | |
4294 | ||
4295 | if (p) | |
4296 | for (; p->next ; p = p->next ) | |
4297 | ; | |
4298 | return p; | |
4299 | } | |
4300 | ||
f84b4be9 JM |
4301 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
4302 | vector of template arguments, as for tsubst. | |
4303 | ||
4304 | Returns an appropriate tsbust'd friend declaration. */ | |
4305 | ||
4306 | static tree | |
4307 | tsubst_friend_function (decl, args) | |
4308 | tree decl; | |
4309 | tree args; | |
4310 | { | |
4311 | tree new_friend; | |
27fafc8d JM |
4312 | int line = lineno; |
4313 | char *file = input_filename; | |
4314 | ||
4315 | lineno = DECL_SOURCE_LINE (decl); | |
4316 | input_filename = DECL_SOURCE_FILE (decl); | |
4317 | ||
f84b4be9 JM |
4318 | if (TREE_CODE (decl) == FUNCTION_DECL |
4319 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
4320 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
4321 | /* This was a friend declared with an explicit template | |
4322 | argument list, e.g.: | |
4323 | ||
4324 | friend void f<>(T); | |
4325 | ||
4326 | to indicate that f was a template instantiation, not a new | |
4327 | function declaration. Now, we have to figure out what | |
4328 | instantiation of what template. */ | |
4329 | { | |
4330 | tree template_id; | |
4331 | tree new_args; | |
4332 | tree tmpl; | |
f84b4be9 JM |
4333 | |
4334 | template_id | |
4335 | = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl), | |
4393e105 MM |
4336 | args, /*complain=*/1, |
4337 | NULL_TREE), | |
f84b4be9 | 4338 | tsubst (DECL_TI_ARGS (decl), |
4393e105 MM |
4339 | args, /*complain=*/1, |
4340 | NULL_TREE)); | |
39c01e4c MM |
4341 | /* FIXME: The decl we create via the next tsubst could be |
4342 | created on a temporary obstack. */ | |
4393e105 | 4343 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
4344 | tmpl = determine_specialization (template_id, new_friend, |
4345 | &new_args, | |
bf8f3f93 | 4346 | /*need_member_template=*/0); |
27fafc8d JM |
4347 | new_friend = instantiate_template (tmpl, new_args); |
4348 | goto done; | |
f84b4be9 | 4349 | } |
36a117a5 | 4350 | |
4393e105 | 4351 | new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE); |
f84b4be9 | 4352 | |
36a117a5 | 4353 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
4354 | compiler, but is not an instantiation from the point of view of |
4355 | the language. For example, we might have had: | |
4356 | ||
4357 | template <class T> struct S { | |
4358 | template <class U> friend void f(T, U); | |
4359 | }; | |
4360 | ||
4361 | Then, in S<int>, template <class U> void f(int, U) is not an | |
4362 | instantiation of anything. */ | |
4363 | DECL_USE_TEMPLATE (new_friend) = 0; | |
4364 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
4365 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
36a117a5 MM |
4366 | |
4367 | /* The mangled name for the NEW_FRIEND is incorrect. The call to | |
4368 | tsubst will have resulted in a call to | |
4369 | set_mangled_name_for_template_decl. But, the function is not a | |
4370 | template instantiation and should not be mangled like one. | |
4371 | Therefore, we remangle the function name. We don't have to do | |
4372 | this if the NEW_FRIEND is a template since | |
4373 | set_mangled_name_for_template_decl doesn't do anything if the | |
4374 | function declaration still uses template arguments. */ | |
4375 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) | |
4376 | { | |
4377 | set_mangled_name_for_decl (new_friend); | |
4378 | DECL_RTL (new_friend) = 0; | |
4379 | make_decl_rtl (new_friend, NULL_PTR, 1); | |
4380 | } | |
4381 | ||
6eb3bb27 | 4382 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 4383 | { |
36a117a5 | 4384 | tree old_decl; |
fbf1c34b MM |
4385 | tree new_friend_template_info; |
4386 | tree new_friend_result_template_info; | |
4387 | int new_friend_is_defn; | |
4388 | ||
4389 | /* We must save some information from NEW_FRIEND before calling | |
4390 | duplicate decls since that function will free NEW_FRIEND if | |
4391 | possible. */ | |
4392 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
f84b4be9 | 4393 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
4394 | { |
4395 | /* This declaration is a `primary' template. */ | |
4396 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
4397 | ||
4398 | new_friend_is_defn | |
4399 | = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE; | |
4400 | new_friend_result_template_info | |
4401 | = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend)); | |
4402 | } | |
4403 | else | |
4404 | { | |
4405 | new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; | |
4406 | new_friend_result_template_info = NULL_TREE; | |
4407 | } | |
36a117a5 | 4408 | |
36a117a5 MM |
4409 | old_decl = pushdecl_namespace_level (new_friend); |
4410 | ||
4411 | if (old_decl != new_friend) | |
4412 | { | |
4413 | /* This new friend declaration matched an existing | |
4414 | declaration. For example, given: | |
4415 | ||
4416 | template <class T> void f(T); | |
4417 | template <class U> class C { | |
4418 | template <class T> friend void f(T) {} | |
4419 | }; | |
4420 | ||
4421 | the friend declaration actually provides the definition | |
4422 | of `f', once C has been instantiated for some type. So, | |
4423 | old_decl will be the out-of-class template declaration, | |
4424 | while new_friend is the in-class definition. | |
4425 | ||
4426 | But, if `f' was called before this point, the | |
4427 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
4428 | to `T' but not to `U', references to which might appear | |
4429 | in the definition of `f'. Previously, the most general | |
4430 | template for an instantiation of `f' was the out-of-class | |
4431 | version; now it is the in-class version. Therefore, we | |
4432 | run through all specialization of `f', adding to their | |
4433 | DECL_TI_ARGS appropriately. In particular, they need a | |
4434 | new set of outer arguments, corresponding to the | |
4435 | arguments for this class instantiation. | |
4436 | ||
4437 | The same situation can arise with something like this: | |
4438 | ||
4439 | friend void f(int); | |
4440 | template <class T> class C { | |
4441 | friend void f(T) {} | |
4442 | }; | |
4443 | ||
4444 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
4445 | in the class. */ | |
4446 | ||
fbf1c34b MM |
4447 | if (!new_friend_is_defn) |
4448 | /* On the other hand, if the in-class declaration does | |
4449 | *not* provide a definition, then we don't want to alter | |
4450 | existing definitions. We can just leave everything | |
4451 | alone. */ | |
36a117a5 | 4452 | ; |
fbf1c34b | 4453 | else |
36a117a5 | 4454 | { |
fbf1c34b MM |
4455 | /* Overwrite whatever template info was there before, if |
4456 | any, with the new template information pertaining to | |
4457 | the declaration. */ | |
4458 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
4459 | ||
4460 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
4461 | /* duplicate_decls will take care of this case. */ | |
4462 | ; | |
4463 | else | |
36a117a5 | 4464 | { |
fbf1c34b MM |
4465 | tree t; |
4466 | tree new_friend_args; | |
4467 | ||
4468 | DECL_TEMPLATE_INFO (DECL_RESULT (old_decl)) | |
4469 | = new_friend_result_template_info; | |
4470 | ||
4471 | new_friend_args = TI_ARGS (new_friend_template_info); | |
4472 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
4473 | t != NULL_TREE; | |
4474 | t = TREE_CHAIN (t)) | |
4475 | { | |
4476 | tree spec = TREE_VALUE (t); | |
36a117a5 | 4477 | |
fbf1c34b MM |
4478 | DECL_TI_ARGS (spec) |
4479 | = add_outermost_template_args (new_friend_args, | |
4480 | DECL_TI_ARGS (spec)); | |
4481 | DECL_TI_ARGS (spec) | |
4482 | = copy_to_permanent (DECL_TI_ARGS (spec)); | |
4483 | } | |
4484 | ||
4485 | /* Now, since specializations are always supposed to | |
4486 | hang off of the most general template, we must move | |
4487 | them. */ | |
4488 | t = most_general_template (old_decl); | |
4489 | if (t != old_decl) | |
4490 | { | |
4491 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
4492 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
4493 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
4494 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
4495 | } | |
36a117a5 MM |
4496 | } |
4497 | } | |
4498 | ||
4499 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
4500 | by duplicate_decls. */ | |
4501 | new_friend = old_decl; | |
4502 | } | |
f84b4be9 JM |
4503 | } |
4504 | else if (TYPE_SIZE (DECL_CONTEXT (new_friend))) | |
4505 | { | |
4506 | /* Check to see that the declaration is really present, and, | |
4507 | possibly obtain an improved declaration. */ | |
4508 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
4509 | new_friend); | |
4510 | ||
4511 | if (fn) | |
4512 | new_friend = fn; | |
4513 | } | |
4514 | ||
27fafc8d JM |
4515 | done: |
4516 | lineno = line; | |
4517 | input_filename = file; | |
f84b4be9 JM |
4518 | return new_friend; |
4519 | } | |
4520 | ||
1aed5355 MM |
4521 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
4522 | template arguments, as for tsubst. | |
6757edfe MM |
4523 | |
4524 | Returns an appropriate tsbust'd friend type. */ | |
4525 | ||
4526 | static tree | |
1aed5355 MM |
4527 | tsubst_friend_class (friend_tmpl, args) |
4528 | tree friend_tmpl; | |
6757edfe MM |
4529 | tree args; |
4530 | { | |
1aed5355 | 4531 | tree friend_type; |
f9c3243c | 4532 | tree tmpl = lookup_name (DECL_NAME (friend_tmpl), 1); |
6757edfe MM |
4533 | |
4534 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
4535 | ||
4536 | if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl)) | |
4537 | { | |
4538 | /* The friend template has already been declared. Just | |
36a117a5 MM |
4539 | check to see that the declarations match, and install any new |
4540 | default parameters. We must tsubst the default parameters, | |
4541 | of course. We only need the innermost template parameters | |
4542 | because that is all that redeclare_class_template will look | |
4543 | at. */ | |
4544 | tree parms | |
4545 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
4393e105 | 4546 | args, /*complain=*/1); |
36a117a5 | 4547 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
6757edfe MM |
4548 | friend_type = TREE_TYPE (tmpl); |
4549 | } | |
4550 | else | |
4551 | { | |
4552 | /* The friend template has not already been declared. In this | |
4553 | case, the instantiation of the template class will cause the | |
4554 | injection of this template into the global scope. */ | |
4393e105 | 4555 | tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE); |
6757edfe MM |
4556 | |
4557 | /* The new TMPL is not an instantiation of anything, so we | |
4558 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
4559 | the new type because that is supposed to be the corresponding | |
4560 | template decl, i.e., TMPL. */ | |
4561 | DECL_USE_TEMPLATE (tmpl) = 0; | |
4562 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
4563 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
4564 | ||
4565 | /* Inject this template into the global scope. */ | |
4566 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
4567 | } | |
4568 | ||
4569 | return friend_type; | |
4570 | } | |
f84b4be9 | 4571 | |
8d08fdba | 4572 | tree |
5566b478 MS |
4573 | instantiate_class_template (type) |
4574 | tree type; | |
8d08fdba | 4575 | { |
61a127b3 | 4576 | tree template, args, pattern, t; |
36a117a5 | 4577 | tree typedecl; |
8d08fdba | 4578 | |
5566b478 | 4579 | if (type == error_mark_node) |
8d08fdba MS |
4580 | return error_mark_node; |
4581 | ||
5566b478 MS |
4582 | if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type)) |
4583 | return type; | |
4584 | ||
7ddedda4 MM |
4585 | /* We want to allocate temporary vectors of template arguments and |
4586 | template argument expressions on the momentary obstack, not on | |
4587 | the expression obstack. Otherwise, all the space allocated in | |
4588 | argument coercion and such is simply lost. */ | |
4589 | push_momentary (); | |
4590 | ||
6bdb985f | 4591 | /* Figure out which template is being instantiated. */ |
36a117a5 | 4592 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
5566b478 | 4593 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); |
73aad9b9 | 4594 | |
6bdb985f MM |
4595 | /* Figure out which arguments are being used to do the |
4596 | instantiation. */ | |
4597 | args = CLASSTYPE_TI_ARGS (type); | |
4c571114 | 4598 | PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args); |
6bdb985f | 4599 | |
4c571114 MM |
4600 | if (pedantic && PARTIAL_INSTANTIATION_P (type)) |
4601 | /* If this is a partial instantiation, then we can't instantiate | |
4602 | the type; there's no telling whether or not one of the | |
4603 | template parameters might eventually be instantiated to some | |
4604 | value that results in a specialization being used. For | |
4605 | example, consider: | |
4606 | ||
4607 | template <class T> | |
4608 | struct S {}; | |
4609 | ||
4610 | template <class U> | |
4611 | void f(S<U>); | |
4612 | ||
4613 | template <> | |
4614 | struct S<int> {}; | |
4615 | ||
4616 | Now, the `S<U>' in `f<int>' is the specialization, not an | |
5db698f6 | 4617 | instantiation of the original template. */ |
4c571114 MM |
4618 | goto end; |
4619 | ||
4620 | /* Determine what specialization of the original template to | |
4621 | instantiate. */ | |
4622 | if (PARTIAL_INSTANTIATION_P (type)) | |
6bdb985f MM |
4623 | /* There's no telling which specialization is appropriate at this |
4624 | point. Since all peeking at the innards of this partial | |
4625 | instantiation are extensions (like the "implicit typename" | |
4626 | extension, which allows users to omit the keyword `typename' on | |
4627 | names that are declared as types in template base classes), we | |
4628 | are free to do what we please. | |
4629 | ||
4630 | Trying to figure out which partial instantiation to use can | |
4631 | cause a crash. (Some of the template arguments don't even have | |
4632 | types.) So, we just use the most general version. */ | |
4633 | t = NULL_TREE; | |
4634 | else | |
73aad9b9 | 4635 | { |
6bdb985f MM |
4636 | t = most_specialized_class (template, args); |
4637 | ||
4638 | if (t == error_mark_node) | |
73aad9b9 | 4639 | { |
6bdb985f MM |
4640 | char *str = "candidates are:"; |
4641 | cp_error ("ambiguous class template instantiation for `%#T'", type); | |
4642 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; | |
4643 | t = TREE_CHAIN (t)) | |
73aad9b9 | 4644 | { |
6bdb985f MM |
4645 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), |
4646 | args)) | |
4647 | { | |
4648 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
4649 | str = " "; | |
4650 | } | |
73aad9b9 | 4651 | } |
6bdb985f MM |
4652 | TYPE_BEING_DEFINED (type) = 1; |
4653 | type = error_mark_node; | |
4654 | goto end; | |
73aad9b9 | 4655 | } |
73aad9b9 | 4656 | } |
6bdb985f MM |
4657 | |
4658 | if (t) | |
73aad9b9 JM |
4659 | pattern = TREE_TYPE (t); |
4660 | else | |
4661 | pattern = TREE_TYPE (template); | |
5566b478 | 4662 | |
4c571114 MM |
4663 | /* If the template we're instantiating is incomplete, then clearly |
4664 | there's nothing we can do. */ | |
5566b478 | 4665 | if (TYPE_SIZE (pattern) == NULL_TREE) |
7ddedda4 | 4666 | goto end; |
5566b478 | 4667 | |
4c571114 MM |
4668 | /* If this is a partial instantiation, don't tsubst anything. We will |
4669 | only use this type for implicit typename, so the actual contents don't | |
4670 | matter. All that matters is whether a particular name is a type. */ | |
4671 | if (PARTIAL_INSTANTIATION_P (type)) | |
4672 | { | |
4673 | /* The fields set here must be kept in sync with those cleared | |
4674 | in begin_class_definition. */ | |
4675 | TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern); | |
4676 | TYPE_FIELDS (type) = TYPE_FIELDS (pattern); | |
4677 | TYPE_METHODS (type) = TYPE_METHODS (pattern); | |
4678 | CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern); | |
4679 | /* Pretend that the type is complete, so that we will look | |
4680 | inside it during name lookup and such. */ | |
4681 | TYPE_SIZE (type) = integer_zero_node; | |
4682 | goto end; | |
4683 | } | |
4684 | ||
4685 | /* If we've recursively instantiated too many templates, stop. */ | |
4686 | if (! push_tinst_level (type)) | |
4687 | goto end; | |
4688 | ||
4689 | /* Now we're really doing the instantiation. Mark the type as in | |
4690 | the process of being defined. */ | |
4691 | TYPE_BEING_DEFINED (type) = 1; | |
4692 | ||
4693 | maybe_push_to_top_level (uses_template_parms (type)); | |
4694 | pushclass (type, 0); | |
4695 | ||
73aad9b9 | 4696 | if (t) |
36a117a5 MM |
4697 | { |
4698 | /* This TYPE is actually a instantiation of of a partial | |
4699 | specialization. We replace the innermost set of ARGS with | |
4700 | the arguments appropriate for substitution. For example, | |
4701 | given: | |
4702 | ||
4703 | template <class T> struct S {}; | |
4704 | template <class T> struct S<T*> {}; | |
4705 | ||
4706 | and supposing that we are instantiating S<int*>, ARGS will | |
4707 | present be {int*} but we need {int}. */ | |
4708 | tree inner_args | |
4709 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
4710 | args); | |
4711 | ||
4712 | /* If there were multiple levels in ARGS, replacing the | |
4713 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
4714 | want, so we make a copy first. */ | |
4715 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
4716 | { | |
4717 | args = copy_node (args); | |
4718 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
4719 | } | |
4720 | else | |
4721 | args = inner_args; | |
4722 | } | |
8d019cef | 4723 | |
5566b478 MS |
4724 | if (flag_external_templates) |
4725 | { | |
4726 | if (flag_alt_external_templates) | |
4727 | { | |
4728 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
4729 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
4730 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4731 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4732 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4733 | } |
4734 | else | |
4735 | { | |
4736 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
4737 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
4738 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
4739 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) | |
2604412d JM |
4740 | = (! CLASSTYPE_INTERFACE_ONLY (type) |
4741 | && CLASSTYPE_INTERFACE_KNOWN (type)); | |
5566b478 MS |
4742 | } |
4743 | } | |
4744 | else | |
8d08fdba | 4745 | { |
5566b478 MS |
4746 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
4747 | CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1; | |
8d08fdba MS |
4748 | } |
4749 | ||
f7da6097 MS |
4750 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
4751 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
4752 | TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern); | |
4753 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); | |
4754 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
4755 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
4756 | TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern); | |
4757 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
4758 | TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern); | |
4759 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
4760 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
4761 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
4762 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
4763 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
4764 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
4765 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
4766 | TYPE_USES_COMPLEX_INHERITANCE (type) | |
4767 | = TYPE_USES_COMPLEX_INHERITANCE (pattern); | |
4768 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
4769 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
4770 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
4771 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
4772 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
4773 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
eff71ab0 | 4774 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
f7da6097 | 4775 | |
4c571114 MM |
4776 | /* We must copy the arguments to the permanent obstack since |
4777 | during the tsubst'ing below they may wind up in the | |
4778 | DECL_TI_ARGS of some instantiated member template. */ | |
4779 | args = copy_to_permanent (args); | |
2604412d | 4780 | |
3fd71a52 MM |
4781 | if (TYPE_BINFO_BASETYPES (pattern)) |
4782 | { | |
4783 | tree base_list = NULL_TREE; | |
4784 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
4785 | int i; | |
5566b478 | 4786 | |
3fd71a52 MM |
4787 | /* Substitute into each of the bases to determine the actual |
4788 | basetypes. */ | |
4789 | for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) | |
4790 | { | |
4791 | tree base; | |
4792 | tree access; | |
4793 | tree pbase; | |
5566b478 | 4794 | |
3fd71a52 | 4795 | pbase = TREE_VEC_ELT (pbases, i); |
711734a9 | 4796 | |
3fd71a52 MM |
4797 | /* Substitue to figure out the base class. */ |
4798 | base = tsubst (BINFO_TYPE (pbase), args, | |
4799 | /*complain=*/1, NULL_TREE); | |
4800 | if (base == error_mark_node) | |
4801 | continue; | |
711734a9 | 4802 | |
3fd71a52 MM |
4803 | /* Calculate the correct access node. */ |
4804 | if (TREE_VIA_VIRTUAL (pbase)) | |
4805 | { | |
4806 | if (TREE_VIA_PUBLIC (pbase)) | |
4807 | access = access_public_virtual_node; | |
4808 | else if (TREE_VIA_PROTECTED (pbase)) | |
4809 | access = access_protected_virtual_node; | |
4810 | else if (TREE_VIA_PRIVATE (pbase)) | |
4811 | access = access_private_virtual_node; | |
4812 | } | |
4813 | else | |
4814 | { | |
4815 | if (TREE_VIA_PUBLIC (pbase)) | |
4816 | access = access_public_node; | |
4817 | else if (TREE_VIA_PROTECTED (pbase)) | |
4818 | access = access_protected_node; | |
4819 | else if (TREE_VIA_PRIVATE (pbase)) | |
4820 | access = access_private_node; | |
4821 | } | |
dfbcd65a | 4822 | |
3fd71a52 MM |
4823 | base_list = tree_cons (access, base, base_list); |
4824 | } | |
dfbcd65a | 4825 | |
3fd71a52 MM |
4826 | /* The list is now in reverse order; correct that. */ |
4827 | base_list = nreverse (base_list); | |
4828 | ||
4829 | /* Now call xref_basetypes to set up all the base-class | |
4830 | information. */ | |
4831 | xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE | |
4832 | ? (CLASSTYPE_DECLARED_CLASS (pattern) | |
4833 | ? class_type_node : record_type_node) | |
4834 | : union_type_node, | |
4835 | DECL_NAME (TYPE_NAME (pattern)), | |
4836 | type, | |
4837 | base_list); | |
4838 | } | |
5566b478 | 4839 | |
5566b478 | 4840 | for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t)) |
8d08fdba | 4841 | { |
5566b478 | 4842 | tree tag = TREE_VALUE (t); |
36a117a5 MM |
4843 | tree name = TYPE_IDENTIFIER (tag); |
4844 | tree newtag; | |
5566b478 | 4845 | |
4393e105 | 4846 | newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE); |
61a127b3 | 4847 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
ed44da02 | 4848 | { |
486e4077 MM |
4849 | if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) |
4850 | /* Unfortunately, lookup_template_class sets | |
4851 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
4852 | instantiation (i.e., for the type of a member template | |
4853 | class nested within a template class.) This behavior is | |
4854 | required for maybe_process_partial_specialization to work | |
4855 | correctly, but is not accurate in this case; the TAG is not | |
4856 | an instantiation of anything. (The corresponding | |
4857 | TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ | |
4858 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; | |
4859 | ||
ed44da02 MM |
4860 | /* Now, we call pushtag to put this NEWTAG into the scope of |
4861 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
4862 | pushtag calling push_template_decl. We don't have to do | |
4863 | this for enums because it will already have been done in | |
4864 | tsubst_enum. */ | |
4865 | if (name) | |
4866 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
4867 | pushtag (name, newtag, /*globalize=*/0); | |
4868 | } | |
8d08fdba MS |
4869 | } |
4870 | ||
5566b478 MS |
4871 | /* Don't replace enum constants here. */ |
4872 | for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t)) | |
4873 | if (TREE_CODE (t) != CONST_DECL) | |
4874 | { | |
ae58fa02 MM |
4875 | tree r; |
4876 | ||
4877 | /* The the file and line for this declaration, to assist in | |
4878 | error message reporting. Since we called push_tinst_level | |
4879 | above, we don't need to restore these. */ | |
4880 | lineno = DECL_SOURCE_LINE (t); | |
4881 | input_filename = DECL_SOURCE_FILE (t); | |
4882 | ||
4393e105 | 4883 | r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
5566b478 MS |
4884 | if (TREE_CODE (r) == VAR_DECL) |
4885 | { | |
2604412d JM |
4886 | pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics); |
4887 | /* Perhaps we should do more of grokfield here. */ | |
94c82a77 JM |
4888 | if (DECL_DEFINED_IN_CLASS_P (r)) |
4889 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
4890 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, | |
4393e105 | 4891 | /*complain=*/1, NULL_TREE); |
5566b478 MS |
4892 | start_decl_1 (r); |
4893 | DECL_IN_AGGR_P (r) = 1; | |
4894 | DECL_EXTERNAL (r) = 1; | |
4895 | cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0); | |
6ba89f8e MM |
4896 | if (DECL_DEFINED_IN_CLASS_P (r)) |
4897 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5566b478 | 4898 | } |
61a127b3 MM |
4899 | |
4900 | /* R will have a TREE_CHAIN if and only if it has already been | |
4901 | processed by finish_member_declaration. This can happen | |
4902 | if, for example, it is a TYPE_DECL for a class-scoped | |
4903 | ENUMERAL_TYPE; such a thing will already have been added to | |
4904 | the field list by tsubst_enum above. */ | |
4905 | if (!TREE_CHAIN (r)) | |
4906 | { | |
4907 | set_current_access_from_decl (r); | |
4908 | finish_member_declaration (r); | |
4909 | } | |
5566b478 | 4910 | } |
8d08fdba | 4911 | |
61a127b3 MM |
4912 | /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC) |
4913 | for this instantiation. */ | |
4914 | for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t)) | |
4915 | { | |
4393e105 | 4916 | tree r = tsubst (t, args, /*complain=*/1, NULL_TREE); |
61a127b3 MM |
4917 | set_current_access_from_decl (r); |
4918 | finish_member_declaration (r); | |
4919 | } | |
8d08fdba | 4920 | |
2604412d JM |
4921 | /* Construct the DECL_FRIENDLIST for the new class type. */ |
4922 | typedecl = TYPE_MAIN_DECL (type); | |
4923 | for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)); | |
4924 | t != NULL_TREE; | |
4925 | t = TREE_CHAIN (t)) | |
f84b4be9 | 4926 | { |
2604412d | 4927 | tree friends; |
f84b4be9 | 4928 | |
2604412d JM |
4929 | DECL_FRIENDLIST (typedecl) |
4930 | = tree_cons (TREE_PURPOSE (t), NULL_TREE, | |
4931 | DECL_FRIENDLIST (typedecl)); | |
f84b4be9 | 4932 | |
2604412d JM |
4933 | for (friends = TREE_VALUE (t); |
4934 | friends != NULL_TREE; | |
4935 | friends = TREE_CHAIN (friends)) | |
4936 | { | |
4937 | if (TREE_PURPOSE (friends) == error_mark_node) | |
f84b4be9 | 4938 | { |
2604412d JM |
4939 | TREE_VALUE (DECL_FRIENDLIST (typedecl)) |
4940 | = tree_cons (error_mark_node, | |
4941 | tsubst_friend_function (TREE_VALUE (friends), | |
4942 | args), | |
4943 | TREE_VALUE (DECL_FRIENDLIST (typedecl))); | |
4944 | } | |
4945 | else | |
4946 | { | |
4947 | TREE_VALUE (DECL_FRIENDLIST (typedecl)) | |
4393e105 MM |
4948 | = tree_cons (tsubst (TREE_PURPOSE (friends), args, |
4949 | /*complain=*/1, NULL_TREE), | |
2604412d JM |
4950 | NULL_TREE, |
4951 | TREE_VALUE (DECL_FRIENDLIST (typedecl))); | |
f84b4be9 JM |
4952 | |
4953 | } | |
4954 | } | |
2604412d | 4955 | } |
5566b478 | 4956 | |
6757edfe MM |
4957 | for (t = CLASSTYPE_FRIEND_CLASSES (pattern); |
4958 | t != NULL_TREE; | |
4959 | t = TREE_CHAIN (t)) | |
4960 | { | |
4961 | tree friend_type = TREE_VALUE (t); | |
1aed5355 | 4962 | tree new_friend_type; |
6757edfe | 4963 | |
36a117a5 MM |
4964 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
4965 | new_friend_type = tsubst_friend_class (friend_type, args); | |
4966 | else if (uses_template_parms (friend_type)) | |
4393e105 MM |
4967 | new_friend_type = tsubst (friend_type, args, /*complain=*/1, |
4968 | NULL_TREE); | |
36a117a5 | 4969 | else |
6757edfe MM |
4970 | /* The call to xref_tag_from_type does injection for friend |
4971 | classes. */ | |
1aed5355 | 4972 | new_friend_type = |
36a117a5 MM |
4973 | xref_tag_from_type (friend_type, NULL_TREE, 1); |
4974 | ||
1aed5355 MM |
4975 | |
4976 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
4977 | /* Trick make_friend_class into realizing that the friend | |
4978 | we're adding is a template, not an ordinary class. It's | |
4979 | important that we use make_friend_class since it will | |
4980 | perform some error-checking and output cross-reference | |
4981 | information. */ | |
4982 | ++processing_template_decl; | |
4983 | ||
4984 | make_friend_class (type, new_friend_type); | |
fc378698 | 4985 | |
1aed5355 MM |
4986 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
4987 | --processing_template_decl; | |
6757edfe | 4988 | } |
fc378698 | 4989 | |
2604412d JM |
4990 | /* This does injection for friend functions. */ |
4991 | if (!processing_template_decl) | |
4992 | { | |
4393e105 MM |
4993 | t = tsubst (DECL_TEMPLATE_INJECT (template), args, |
4994 | /*complain=*/1, NULL_TREE); | |
5566b478 | 4995 | |
2604412d JM |
4996 | for (; t; t = TREE_CHAIN (t)) |
4997 | { | |
4998 | tree d = TREE_VALUE (t); | |
fc378698 | 4999 | |
2604412d JM |
5000 | if (TREE_CODE (d) == TYPE_DECL) |
5001 | /* Already injected. */; | |
5002 | else | |
5003 | pushdecl (d); | |
5004 | } | |
5005 | } | |
5566b478 | 5006 | |
2604412d JM |
5007 | for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
5008 | if (TREE_CODE (t) == FIELD_DECL) | |
5009 | { | |
5010 | TREE_TYPE (t) = complete_type (TREE_TYPE (t)); | |
5011 | require_complete_type (t); | |
5012 | } | |
5566b478 | 5013 | |
61a127b3 MM |
5014 | /* Set the file and line number information to whatever is given for |
5015 | the class itself. This puts error messages involving generated | |
5016 | implicit functions at a predictable point, and the same point | |
5017 | that would be used for non-template classes. */ | |
5018 | lineno = DECL_SOURCE_LINE (typedecl); | |
5019 | input_filename = DECL_SOURCE_FILE (typedecl); | |
5020 | ||
5021 | unreverse_member_declarations (type); | |
2604412d JM |
5022 | type = finish_struct_1 (type, 0); |
5023 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
e92cc029 | 5024 | |
5524676d JM |
5025 | /* Clear this now so repo_template_used is happy. */ |
5026 | TYPE_BEING_DEFINED (type) = 0; | |
2604412d | 5027 | repo_template_used (type); |
8d08fdba | 5028 | |
5566b478 | 5029 | popclass (0); |
5566b478 MS |
5030 | pop_from_top_level (); |
5031 | pop_tinst_level (); | |
5032 | ||
7ddedda4 MM |
5033 | end: |
5034 | pop_momentary (); | |
5035 | ||
5566b478 | 5036 | return type; |
8d08fdba MS |
5037 | } |
5038 | ||
5039 | static int | |
5040 | list_eq (t1, t2) | |
5041 | tree t1, t2; | |
5042 | { | |
5043 | if (t1 == NULL_TREE) | |
5044 | return t2 == NULL_TREE; | |
5045 | if (t2 == NULL_TREE) | |
5046 | return 0; | |
5047 | /* Don't care if one declares its arg const and the other doesn't -- the | |
5048 | main variant of the arg type is all that matters. */ | |
5049 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
5050 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
5051 | return 0; | |
5052 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
5053 | } | |
5054 | ||
00d3396f JM |
5055 | /* If arg is a non-type template parameter that does not depend on template |
5056 | arguments, fold it like we weren't in the body of a template. */ | |
5057 | ||
5058 | static tree | |
5059 | maybe_fold_nontype_arg (arg) | |
5060 | tree arg; | |
5061 | { | |
5062 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't' | |
5063 | && !uses_template_parms (arg)) | |
5064 | { | |
5065 | /* Sometimes, one of the args was an expression involving a | |
5066 | template constant parameter, like N - 1. Now that we've | |
5067 | tsubst'd, we might have something like 2 - 1. This will | |
5068 | confuse lookup_template_class, so we do constant folding | |
5069 | here. We have to unset processing_template_decl, to | |
5070 | fool build_expr_from_tree() into building an actual | |
5071 | tree. */ | |
5072 | ||
5073 | int saved_processing_template_decl = processing_template_decl; | |
5074 | processing_template_decl = 0; | |
5075 | arg = fold (build_expr_from_tree (arg)); | |
5076 | processing_template_decl = saved_processing_template_decl; | |
5077 | } | |
5078 | return arg; | |
5079 | } | |
5080 | ||
a221d52f | 5081 | /* Return the TREE_VEC with the arguments for the innermost template header, |
36a117a5 MM |
5082 | where ARGS is either that or the VEC of VECs for all the |
5083 | arguments. */ | |
a221d52f JM |
5084 | |
5085 | tree | |
36a117a5 | 5086 | innermost_args (args) |
a221d52f | 5087 | tree args; |
a221d52f | 5088 | { |
36a117a5 | 5089 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
a221d52f | 5090 | } |
f84b4be9 | 5091 | |
830bfa74 MM |
5092 | /* Substitute ARGS into the vector of template arguments T. */ |
5093 | ||
e9659ab0 | 5094 | static tree |
4393e105 | 5095 | tsubst_template_arg_vector (t, args, complain) |
830bfa74 MM |
5096 | tree t; |
5097 | tree args; | |
4393e105 | 5098 | int complain; |
830bfa74 MM |
5099 | { |
5100 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
5101 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5102 | ||
5103 | bzero ((char *) elts, len * sizeof (tree)); | |
5104 | ||
5105 | for (i = 0; i < len; i++) | |
5106 | { | |
5107 | if (TREE_VEC_ELT (t, i) != NULL_TREE | |
5108 | && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) | |
4393e105 MM |
5109 | elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), |
5110 | args, complain); | |
830bfa74 MM |
5111 | else |
5112 | elts[i] = maybe_fold_nontype_arg | |
4393e105 MM |
5113 | (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, |
5114 | NULL_TREE)); | |
830bfa74 MM |
5115 | |
5116 | if (elts[i] != TREE_VEC_ELT (t, i)) | |
5117 | need_new = 1; | |
5118 | } | |
5119 | ||
5120 | if (!need_new) | |
5121 | return t; | |
5122 | ||
7ddedda4 | 5123 | t = make_temp_vec (len); |
830bfa74 MM |
5124 | for (i = 0; i < len; i++) |
5125 | TREE_VEC_ELT (t, i) = elts[i]; | |
5126 | ||
5127 | return t; | |
5128 | } | |
5129 | ||
36a117a5 MM |
5130 | /* Return the result of substituting ARGS into the template parameters |
5131 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5132 | PARMS, then the result will contain n levels of PARMS. For | |
5133 | example, if PARMS is `template <class T> template <class U> | |
5134 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5135 | result will be `template <int*, double, class V>'. */ | |
5136 | ||
e9659ab0 | 5137 | static tree |
4393e105 | 5138 | tsubst_template_parms (parms, args, complain) |
36a117a5 MM |
5139 | tree parms; |
5140 | tree args; | |
4393e105 | 5141 | int complain; |
36a117a5 MM |
5142 | { |
5143 | tree r; | |
5144 | tree* new_parms = &r; | |
5145 | ||
5146 | for (new_parms = &r; | |
5147 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5148 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5149 | parms = TREE_CHAIN (parms)) | |
5150 | { | |
5151 | tree new_vec = | |
5152 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5153 | int i; | |
5154 | ||
5155 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5156 | { | |
5157 | tree default_value = | |
5158 | TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5159 | tree parm_decl = | |
5160 | TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i)); | |
5161 | ||
5162 | TREE_VEC_ELT (new_vec, i) | |
4393e105 MM |
5163 | = build_tree_list (tsubst (default_value, args, complain, |
5164 | NULL_TREE), | |
5165 | tsubst (parm_decl, args, complain, | |
5166 | NULL_TREE)); | |
36a117a5 MM |
5167 | } |
5168 | ||
5169 | *new_parms = | |
5170 | tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms) | |
5171 | - TMPL_ARGS_DEPTH (args))), | |
5172 | new_vec, NULL_TREE); | |
5173 | } | |
5174 | ||
5175 | return r; | |
5176 | } | |
5177 | ||
ed44da02 MM |
5178 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
5179 | type T. If T is not an aggregate or enumeration type, it is | |
5180 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
5181 | ENTERING_SCOPE is non-zero, T is the context for a template which | |
5182 | we are presently tsubst'ing. Return the subsituted value. */ | |
36a117a5 | 5183 | |
e9659ab0 | 5184 | static tree |
4393e105 | 5185 | tsubst_aggr_type (t, args, complain, in_decl, entering_scope) |
36a117a5 MM |
5186 | tree t; |
5187 | tree args; | |
4393e105 | 5188 | int complain; |
36a117a5 MM |
5189 | tree in_decl; |
5190 | int entering_scope; | |
5191 | { | |
5192 | if (t == NULL_TREE) | |
5193 | return NULL_TREE; | |
5194 | ||
5195 | switch (TREE_CODE (t)) | |
5196 | { | |
5197 | case RECORD_TYPE: | |
5198 | if (TYPE_PTRMEMFUNC_P (t)) | |
5199 | { | |
5200 | tree r = build_ptrmemfunc_type | |
4393e105 | 5201 | (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl)); |
91063b51 | 5202 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
36a117a5 MM |
5203 | } |
5204 | ||
5205 | /* else fall through */ | |
ed44da02 | 5206 | case ENUMERAL_TYPE: |
36a117a5 | 5207 | case UNION_TYPE: |
5db698f6 | 5208 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
5209 | { |
5210 | tree argvec; | |
5211 | tree context; | |
5212 | tree r; | |
5213 | ||
5214 | /* First, determine the context for the type we are looking | |
5215 | up. */ | |
5216 | if (TYPE_CONTEXT (t) != NULL_TREE) | |
5217 | context = tsubst_aggr_type (TYPE_CONTEXT (t), args, | |
4393e105 | 5218 | complain, |
36a117a5 MM |
5219 | in_decl, /*entering_scope=*/1); |
5220 | else | |
5221 | context = NULL_TREE; | |
5222 | ||
5223 | /* Then, figure out what arguments are appropriate for the | |
5224 | type we are trying to find. For example, given: | |
5225 | ||
5226 | template <class T> struct S; | |
5227 | template <class T, class U> void f(T, U) { S<U> su; } | |
5228 | ||
5229 | and supposing that we are instantiating f<int, double>, | |
5230 | then our ARGS will be {int, double}, but, when looking up | |
5231 | S we only want {double}. */ | |
7ddedda4 | 5232 | push_momentary (); |
4393e105 MM |
5233 | argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, |
5234 | complain); | |
36a117a5 MM |
5235 | |
5236 | r = lookup_template_class (t, argvec, in_decl, context, | |
5237 | entering_scope); | |
7ddedda4 | 5238 | pop_momentary (); |
36a117a5 | 5239 | |
91063b51 | 5240 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
36a117a5 MM |
5241 | } |
5242 | else | |
5243 | /* This is not a template type, so there's nothing to do. */ | |
5244 | return t; | |
5245 | ||
5246 | default: | |
4393e105 | 5247 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
5248 | } |
5249 | } | |
5250 | ||
ae58fa02 MM |
5251 | /* Substitute the ARGS into the T, which is a _DECL. TYPE is the |
5252 | (already computed) substitution of ARGS into TREE_TYPE (T), if | |
5253 | appropriate. Return the result of the substitution. IN_DECL is as | |
5254 | for tsubst. */ | |
00d3396f | 5255 | |
e9659ab0 | 5256 | static tree |
ae58fa02 MM |
5257 | tsubst_decl (t, args, type, in_decl) |
5258 | tree t; | |
5259 | tree args; | |
5260 | tree type; | |
8d08fdba MS |
5261 | tree in_decl; |
5262 | { | |
ae58fa02 MM |
5263 | int saved_lineno; |
5264 | char* saved_filename; | |
b370501f | 5265 | tree r = NULL_TREE; |
830bfa74 | 5266 | |
ae58fa02 MM |
5267 | /* Set the filename and linenumber to improve error-reporting. */ |
5268 | saved_lineno = lineno; | |
5269 | saved_filename = input_filename; | |
5270 | lineno = DECL_SOURCE_LINE (t); | |
5271 | input_filename = DECL_SOURCE_FILE (t); | |
b7484fbe | 5272 | |
8d08fdba MS |
5273 | switch (TREE_CODE (t)) |
5274 | { | |
98c1c668 JM |
5275 | case TEMPLATE_DECL: |
5276 | { | |
5277 | /* We can get here when processing a member template function | |
5278 | of a template class. */ | |
98c1c668 | 5279 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 5280 | tree spec; |
db2767b6 | 5281 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); |
98c1c668 | 5282 | |
db2767b6 MM |
5283 | if (!is_template_template_parm) |
5284 | { | |
36a117a5 MM |
5285 | /* We might already have an instance of this template. |
5286 | The ARGS are for the surrounding class type, so the | |
5287 | full args contain the tsubst'd args for the context, | |
5288 | plus the innermost args from the template decl. */ | |
5289 | tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
5290 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
5291 | : DECL_TI_ARGS (DECL_RESULT (t)); | |
7ddedda4 MM |
5292 | tree full_args; |
5293 | ||
5294 | push_momentary (); | |
4393e105 MM |
5295 | full_args = tsubst_template_arg_vector (tmpl_args, args, |
5296 | /*complain=*/1); | |
36a117a5 MM |
5297 | |
5298 | /* tsubst_template_arg_vector doesn't copy the vector if | |
5299 | nothing changed. But, *something* should have | |
5300 | changed. */ | |
5301 | my_friendly_assert (full_args != tmpl_args, 0); | |
5302 | ||
5303 | spec = retrieve_specialization (t, full_args); | |
7ddedda4 | 5304 | pop_momentary (); |
db2767b6 | 5305 | if (spec != NULL_TREE) |
ae58fa02 MM |
5306 | { |
5307 | r = spec; | |
5308 | break; | |
5309 | } | |
db2767b6 | 5310 | } |
98c1c668 JM |
5311 | |
5312 | /* Make a new template decl. It will be similar to the | |
5313 | original, but will record the current template arguments. | |
5314 | We also create a new function declaration, which is just | |
5315 | like the old one, but points to this new template, rather | |
5316 | than the old one. */ | |
ae58fa02 MM |
5317 | r = copy_node (t); |
5318 | copy_lang_decl (r); | |
5319 | my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); | |
5320 | TREE_CHAIN (r) = NULL_TREE; | |
db2767b6 MM |
5321 | |
5322 | if (is_template_template_parm) | |
5323 | { | |
4393e105 | 5324 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5325 | DECL_RESULT (r) = new_decl; |
5326 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5327 | break; | |
db2767b6 MM |
5328 | } |
5329 | ||
ae58fa02 | 5330 | DECL_CONTEXT (r) |
4393e105 MM |
5331 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, |
5332 | in_decl, /*entering_scope=*/1); | |
ae58fa02 | 5333 | DECL_CLASS_CONTEXT (r) |
4393e105 MM |
5334 | = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5335 | /*complain=*/1, in_decl, | |
5336 | /*entering_scope=*/1); | |
ae58fa02 | 5337 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
5338 | |
5339 | if (TREE_CODE (decl) == TYPE_DECL) | |
5340 | { | |
4393e105 MM |
5341 | tree new_type = tsubst (TREE_TYPE (t), args, |
5342 | /*complain=*/1, in_decl); | |
ae58fa02 MM |
5343 | TREE_TYPE (r) = new_type; |
5344 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
5345 | DECL_RESULT (r) = TYPE_MAIN_DECL (new_type); | |
5346 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); | |
93cdc044 JM |
5347 | } |
5348 | else | |
5349 | { | |
4393e105 | 5350 | tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl); |
ae58fa02 MM |
5351 | DECL_RESULT (r) = new_decl; |
5352 | DECL_TI_TEMPLATE (new_decl) = r; | |
5353 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5354 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
5355 | } |
5356 | ||
ae58fa02 MM |
5357 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
5358 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
5359 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
5360 | |
5361 | /* The template parameters for this new template are all the | |
5362 | template parameters for the old template, except the | |
5363 | outermost level of parameters. */ | |
ae58fa02 | 5364 | DECL_TEMPLATE_PARMS (r) |
4393e105 MM |
5365 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
5366 | /*complain=*/1); | |
98c1c668 | 5367 | |
93cdc044 | 5368 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 5369 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 5370 | |
8d019cef | 5371 | /* We don't partially instantiate partial specializations. */ |
93cdc044 | 5372 | if (TREE_CODE (decl) == TYPE_DECL) |
ae58fa02 | 5373 | break; |
93cdc044 | 5374 | |
75650646 MM |
5375 | for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t); |
5376 | spec != NULL_TREE; | |
5377 | spec = TREE_CHAIN (spec)) | |
5378 | { | |
5379 | /* It helps to consider example here. Consider: | |
5380 | ||
5381 | template <class T> | |
5382 | struct S { | |
5383 | template <class U> | |
5384 | void f(U u); | |
5385 | ||
5386 | template <> | |
5387 | void f(T* t) {} | |
5388 | }; | |
5389 | ||
5390 | Now, for example, we are instantiating S<int>::f(U u). | |
5391 | We want to make a template: | |
5392 | ||
5393 | template <class U> | |
5394 | void S<int>::f(U); | |
5395 | ||
5396 | It will have a specialization, for the case U = int*, of | |
5397 | the form: | |
5398 | ||
5399 | template <> | |
5400 | void S<int>::f<int*>(int*); | |
5401 | ||
5402 | This specialization will be an instantiation of | |
5403 | the specialization given in the declaration of S, with | |
5404 | argument list int*. */ | |
5405 | ||
5406 | tree fn = TREE_VALUE (spec); | |
5407 | tree spec_args; | |
5408 | tree new_fn; | |
5409 | ||
5410 | if (!DECL_TEMPLATE_SPECIALIZATION (fn)) | |
5411 | /* Instantiations are on the same list, but they're of | |
5412 | no concern to us. */ | |
5413 | continue; | |
5414 | ||
36a117a5 MM |
5415 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
5416 | /* A full specialization. There's no need to record | |
5417 | that here. */ | |
5418 | continue; | |
5419 | ||
4393e105 MM |
5420 | spec_args = tsubst (DECL_TI_ARGS (fn), args, |
5421 | /*complain=*/1, in_decl); | |
36a117a5 | 5422 | new_fn = tsubst (DECL_RESULT (most_general_template (fn)), |
4393e105 | 5423 | spec_args, /*complain=*/1, in_decl); |
36a117a5 | 5424 | DECL_TI_TEMPLATE (new_fn) = fn; |
ae58fa02 | 5425 | register_specialization (new_fn, r, |
36a117a5 | 5426 | innermost_args (spec_args)); |
75650646 MM |
5427 | } |
5428 | ||
5429 | /* Record this partial instantiation. */ | |
ae58fa02 MM |
5430 | register_specialization (r, t, |
5431 | DECL_TI_ARGS (DECL_RESULT (r))); | |
75650646 | 5432 | |
98c1c668 | 5433 | } |
ae58fa02 | 5434 | break; |
8d08fdba MS |
5435 | |
5436 | case FUNCTION_DECL: | |
5437 | { | |
386b8a85 | 5438 | tree ctx; |
87603ed0 | 5439 | tree argvec = NULL_TREE; |
36a117a5 | 5440 | tree gen_tmpl; |
5566b478 | 5441 | int member; |
d8c4447d MM |
5442 | int args_depth; |
5443 | int parms_depth; | |
5566b478 | 5444 | |
36a117a5 MM |
5445 | /* Nobody should be tsubst'ing into non-template functions. */ |
5446 | my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5447 | ||
5448 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
5449 | { | |
5450 | tree spec; | |
5451 | ||
7ddedda4 MM |
5452 | /* Allocate template arguments on the momentary obstack, |
5453 | in case we don't need to keep them. */ | |
5454 | push_momentary (); | |
5455 | ||
36a117a5 MM |
5456 | /* Calculate the most general template of which R is a |
5457 | specialization, and the complete set of arguments used to | |
5458 | specialize R. */ | |
5459 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
7ddedda4 MM |
5460 | argvec |
5461 | = tsubst_template_arg_vector (DECL_TI_ARGS | |
5462 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
4393e105 | 5463 | args, /*complain=*/1); |
36a117a5 MM |
5464 | |
5465 | /* Check to see if we already have this specialization. */ | |
5466 | spec = retrieve_specialization (gen_tmpl, argvec); | |
7ddedda4 | 5467 | |
36a117a5 | 5468 | if (spec) |
ae58fa02 MM |
5469 | { |
5470 | r = spec; | |
7ddedda4 | 5471 | pop_momentary (); |
ae58fa02 MM |
5472 | break; |
5473 | } | |
d8c4447d | 5474 | |
7ddedda4 MM |
5475 | /* We're going to need to keep the ARGVEC, so we copy it |
5476 | here. */ | |
5477 | argvec = copy_to_permanent (argvec); | |
5478 | pop_momentary (); | |
5479 | ||
d8c4447d MM |
5480 | /* Here, we deal with the peculiar case: |
5481 | ||
5482 | template <class T> struct S { | |
5483 | template <class U> friend void f(); | |
5484 | }; | |
5485 | template <class U> friend void f() {} | |
5486 | template S<int>; | |
5487 | template void f<double>(); | |
5488 | ||
5489 | Here, the ARGS for the instantiation of will be {int, | |
5490 | double}. But, we only need as many ARGS as there are | |
5491 | levels of template parameters in CODE_PATTERN. We are | |
5492 | careful not to get fooled into reducing the ARGS in | |
5493 | situations like: | |
5494 | ||
5495 | template <class T> struct S { template <class U> void f(U); } | |
5496 | template <class T> template <> void S<T>::f(int) {} | |
5497 | ||
5498 | which we can spot because the pattern will be a | |
5499 | specialization in this case. */ | |
5500 | args_depth = TMPL_ARGS_DEPTH (args); | |
5501 | parms_depth = | |
5502 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
5503 | if (args_depth > parms_depth | |
5504 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
5505 | { | |
5506 | my_friendly_assert (DECL_FRIEND_P (t), 0); | |
5507 | ||
5508 | if (parms_depth > 1) | |
5509 | { | |
5510 | int i; | |
5511 | ||
5512 | args = make_temp_vec (parms_depth); | |
5513 | for (i = 0; i < parms_depth; ++i) | |
5514 | TREE_VEC_ELT (args, i) = | |
5515 | TREE_VEC_ELT (args, i + (args_depth - parms_depth)); | |
5516 | } | |
5517 | else | |
5518 | args = TREE_VEC_ELT (args, args_depth - parms_depth); | |
5519 | } | |
36a117a5 MM |
5520 | } |
5521 | else | |
5522 | { | |
5523 | /* This special case arises when we have something like this: | |
5524 | ||
5525 | template <class T> struct S { | |
5526 | friend void f<int>(int, double); | |
5527 | }; | |
5528 | ||
5529 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
39c01e4c MM |
5530 | will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are |
5531 | being called from tsubst_friend_function, and we want | |
5532 | only to create a new decl (R) with appropriate types so | |
5533 | that we can call determine_specialization. */ | |
5534 | my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5535 | == LOOKUP_EXPR) | |
5536 | || (TREE_CODE (DECL_TI_TEMPLATE (t)) | |
5537 | == IDENTIFIER_NODE), 0); | |
36a117a5 MM |
5538 | gen_tmpl = NULL_TREE; |
5539 | } | |
5540 | ||
6eb3bb27 | 5541 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 5542 | { |
5566b478 MS |
5543 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
5544 | member = 2; | |
5545 | else | |
5546 | member = 1; | |
4393e105 MM |
5547 | ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, |
5548 | /*complain=*/1, t, | |
36a117a5 | 5549 | /*entering_scope=*/1); |
5566b478 MS |
5550 | } |
5551 | else | |
5552 | { | |
5553 | member = 0; | |
5554 | ctx = NULL_TREE; | |
5566b478 | 5555 | } |
4393e105 | 5556 | type = tsubst (type, args, /*complain=*/1, in_decl); |
8d08fdba | 5557 | |
5566b478 MS |
5558 | /* We do NOT check for matching decls pushed separately at this |
5559 | point, as they may not represent instantiations of this | |
5560 | template, and in any case are considered separate under the | |
312e7d50 | 5561 | discrete model. Instead, see add_maybe_template. */ |
5566b478 MS |
5562 | |
5563 | r = copy_node (t); | |
5564 | copy_lang_decl (r); | |
e1467ff2 | 5565 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 MS |
5566 | TREE_TYPE (r) = type; |
5567 | ||
5568 | DECL_CONTEXT (r) | |
4393e105 MM |
5569 | = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, t, |
5570 | /*entering_scope=*/1); | |
5566b478 MS |
5571 | DECL_CLASS_CONTEXT (r) = ctx; |
5572 | ||
4c571114 | 5573 | if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r))) |
f181d4ae MM |
5574 | /* Type-conversion operator. Reconstruct the name, in |
5575 | case it's the name of one of the template's parameters. */ | |
5576 | DECL_NAME (r) = build_typename_overload (TREE_TYPE (type)); | |
5566b478 | 5577 | |
4393e105 MM |
5578 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
5579 | /*complain=*/1, t); | |
711734a9 JM |
5580 | DECL_MAIN_VARIANT (r) = r; |
5581 | DECL_RESULT (r) = NULL_TREE; | |
711734a9 JM |
5582 | |
5583 | TREE_STATIC (r) = 0; | |
5584 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
5585 | DECL_EXTERNAL (r) = 1; | |
5586 | DECL_INTERFACE_KNOWN (r) = 0; | |
5587 | DECL_DEFER_OUTPUT (r) = 0; | |
5588 | TREE_CHAIN (r) = NULL_TREE; | |
5589 | DECL_PENDING_INLINE_INFO (r) = 0; | |
5590 | TREE_USED (r) = 0; | |
5591 | ||
36a117a5 MM |
5592 | /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled |
5593 | name. There's no need to do this in the special friend | |
5594 | case mentioned above where GEN_TMPL is NULL. */ | |
5595 | if (gen_tmpl) | |
386b8a85 | 5596 | { |
7ddedda4 MM |
5597 | /* The ARGVEC was built on the momentary obstack. Make it |
5598 | permanent now. */ | |
5599 | argvec = copy_to_permanent (argvec); | |
36a117a5 MM |
5600 | DECL_TEMPLATE_INFO (r) |
5601 | = perm_tree_cons (gen_tmpl, argvec, NULL_TREE); | |
5602 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5603 | register_specialization (r, gen_tmpl, argvec); | |
5604 | ||
5605 | /* Set the mangled name for R. */ | |
5606 | if (DECL_DESTRUCTOR_P (t)) | |
5607 | DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx); | |
5608 | else | |
386b8a85 | 5609 | { |
36a117a5 MM |
5610 | /* Instantiations of template functions must be mangled |
5611 | specially, in order to conform to 14.5.5.1 | |
5612 | [temp.over.link]. */ | |
5613 | tree tmpl = DECL_TI_TEMPLATE (t); | |
386b8a85 | 5614 | |
36a117a5 MM |
5615 | /* TMPL will be NULL if this is a specialization of a |
5616 | member function of a template class. */ | |
5617 | if (name_mangling_version < 1 | |
5618 | || tmpl == NULL_TREE | |
5619 | || (member && !is_member_template (tmpl) | |
5620 | && !DECL_TEMPLATE_INFO (tmpl))) | |
5621 | set_mangled_name_for_decl (r); | |
75650646 | 5622 | else |
36a117a5 | 5623 | set_mangled_name_for_template_decl (r); |
386b8a85 | 5624 | } |
36a117a5 MM |
5625 | |
5626 | DECL_RTL (r) = 0; | |
5627 | make_decl_rtl (r, NULL_PTR, 1); | |
5628 | ||
5629 | /* Like grokfndecl. If we don't do this, pushdecl will | |
5630 | mess up our TREE_CHAIN because it doesn't find a | |
5631 | previous decl. Sigh. */ | |
5632 | if (member | |
c36ae80f | 5633 | && ! uses_template_parms (r) |
36a117a5 MM |
5634 | && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) |
5635 | == NULL_TREE)) | |
5636 | SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r); | |
386b8a85 | 5637 | } |
f181d4ae MM |
5638 | |
5639 | if (DECL_CONSTRUCTOR_P (r)) | |
5640 | { | |
5641 | maybe_retrofit_in_chrg (r); | |
5642 | grok_ctor_properties (ctx, r); | |
5643 | } | |
5644 | if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) | |
5645 | grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r)); | |
8d08fdba | 5646 | } |
ae58fa02 | 5647 | break; |
8d08fdba MS |
5648 | |
5649 | case PARM_DECL: | |
5650 | { | |
ae58fa02 | 5651 | r = copy_node (t); |
5566b478 | 5652 | TREE_TYPE (r) = type; |
db2767b6 MM |
5653 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
5654 | DECL_INITIAL (r) = TREE_TYPE (r); | |
5655 | else | |
4393e105 MM |
5656 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, |
5657 | /*complain=*/1, in_decl); | |
db2767b6 | 5658 | |
5566b478 | 5659 | DECL_CONTEXT (r) = NULL_TREE; |
f83b0cb6 JM |
5660 | #ifdef PROMOTE_PROTOTYPES |
5661 | if ((TREE_CODE (type) == INTEGER_TYPE | |
5662 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
5663 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) | |
5664 | DECL_ARG_TYPE (r) = integer_type_node; | |
5665 | #endif | |
8d08fdba | 5666 | if (TREE_CHAIN (t)) |
4393e105 MM |
5667 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
5668 | /*complain=*/1, TREE_CHAIN (t)); | |
8d08fdba | 5669 | } |
ae58fa02 | 5670 | break; |
8d08fdba | 5671 | |
5566b478 MS |
5672 | case FIELD_DECL: |
5673 | { | |
ae58fa02 | 5674 | r = copy_node (t); |
5566b478 MS |
5675 | TREE_TYPE (r) = type; |
5676 | copy_lang_decl (r); | |
5677 | #if 0 | |
4393e105 MM |
5678 | DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, |
5679 | /*complain=*/1, in_decl); | |
5566b478 | 5680 | #endif |
4393e105 MM |
5681 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, |
5682 | /*complain=*/1, in_decl); | |
5566b478 | 5683 | TREE_CHAIN (r) = NULL_TREE; |
4393e105 | 5684 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 5685 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5686 | } |
ae58fa02 | 5687 | break; |
5566b478 MS |
5688 | |
5689 | case USING_DECL: | |
5690 | { | |
ae58fa02 | 5691 | r = copy_node (t); |
5566b478 | 5692 | DECL_INITIAL (r) |
4393e105 | 5693 | = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl); |
5566b478 | 5694 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 5695 | } |
ae58fa02 | 5696 | break; |
5566b478 MS |
5697 | |
5698 | case VAR_DECL: | |
5699 | { | |
36a117a5 MM |
5700 | tree argvec; |
5701 | tree gen_tmpl; | |
5702 | tree spec; | |
5703 | tree tmpl; | |
4393e105 MM |
5704 | tree ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
5705 | /*complain=*/1, | |
5706 | in_decl, /*entering_scope=*/1); | |
36a117a5 MM |
5707 | |
5708 | /* Nobody should be tsubst'ing into non-template variables. */ | |
5709 | my_friendly_assert (DECL_LANG_SPECIFIC (t) | |
5710 | && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5566b478 | 5711 | |
36a117a5 MM |
5712 | /* Check to see if we already have this specialization. */ |
5713 | tmpl = DECL_TI_TEMPLATE (t); | |
5714 | gen_tmpl = most_general_template (tmpl); | |
4393e105 | 5715 | argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl); |
36a117a5 MM |
5716 | spec = retrieve_specialization (gen_tmpl, argvec); |
5717 | ||
5718 | if (spec) | |
ae58fa02 MM |
5719 | { |
5720 | r = spec; | |
5721 | break; | |
5722 | } | |
5566b478 MS |
5723 | |
5724 | r = copy_node (t); | |
5725 | TREE_TYPE (r) = type; | |
5726 | DECL_CONTEXT (r) = ctx; | |
5727 | if (TREE_STATIC (r)) | |
5728 | DECL_ASSEMBLER_NAME (r) | |
5729 | = build_static_name (DECL_CONTEXT (r), DECL_NAME (r)); | |
d11ad92e MS |
5730 | |
5731 | /* Don't try to expand the initializer until someone tries to use | |
5732 | this variable; otherwise we run into circular dependencies. */ | |
5733 | DECL_INITIAL (r) = NULL_TREE; | |
5566b478 MS |
5734 | DECL_RTL (r) = 0; |
5735 | DECL_SIZE (r) = 0; | |
36a117a5 MM |
5736 | copy_lang_decl (r); |
5737 | DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r); | |
5566b478 | 5738 | |
36a117a5 MM |
5739 | DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE); |
5740 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5741 | register_specialization (r, gen_tmpl, argvec); | |
5566b478 | 5742 | |
5566b478 | 5743 | TREE_CHAIN (r) = NULL_TREE; |
519ebd1e | 5744 | if (TREE_CODE (type) == VOID_TYPE) |
8251199e | 5745 | cp_error_at ("instantiation of `%D' as type void", r); |
5566b478 | 5746 | } |
ae58fa02 | 5747 | break; |
5566b478 MS |
5748 | |
5749 | case TYPE_DECL: | |
d2e5ee5c | 5750 | if (t == TYPE_NAME (TREE_TYPE (t))) |
ae58fa02 MM |
5751 | r = TYPE_NAME (type); |
5752 | else | |
5753 | { | |
5754 | r = copy_node (t); | |
5755 | TREE_TYPE (r) = type; | |
5756 | DECL_CONTEXT (r) = current_class_type; | |
5757 | TREE_CHAIN (r) = NULL_TREE; | |
5758 | } | |
5759 | break; | |
5760 | ||
5761 | default: | |
5762 | my_friendly_abort (0); | |
5763 | } | |
5764 | ||
5765 | /* Restore the file and line information. */ | |
5766 | lineno = saved_lineno; | |
5767 | input_filename = saved_filename; | |
5768 | ||
5769 | return r; | |
5770 | } | |
5771 | ||
cabc336a MM |
5772 | /* Substitue into the ARG_TYPES of a function type. */ |
5773 | ||
e9659ab0 | 5774 | static tree |
4393e105 | 5775 | tsubst_arg_types (arg_types, args, complain, in_decl) |
cabc336a MM |
5776 | tree arg_types; |
5777 | tree args; | |
4393e105 | 5778 | int complain; |
cabc336a MM |
5779 | tree in_decl; |
5780 | { | |
5781 | tree remaining_arg_types; | |
cabc336a MM |
5782 | tree type; |
5783 | ||
5784 | if (!arg_types || arg_types == void_list_node) | |
5785 | return arg_types; | |
5786 | ||
5787 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
4393e105 MM |
5788 | args, complain, in_decl); |
5789 | if (remaining_arg_types == error_mark_node) | |
5790 | return error_mark_node; | |
5791 | ||
5792 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
5793 | if (type == error_mark_node) | |
5794 | return error_mark_node; | |
cabc336a | 5795 | |
4393e105 MM |
5796 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
5797 | top-level qualifiers as required. */ | |
5798 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a MM |
5799 | |
5800 | /* Note that we do not substitute into default arguments here. The | |
5801 | standard mandates that they be instantiated only when needed, | |
5802 | which is done in build_over_call. */ | |
5803 | return hash_tree_cons_simple (TREE_PURPOSE (arg_types), type, | |
5804 | remaining_arg_types); | |
5805 | ||
5806 | } | |
5807 | ||
4393e105 MM |
5808 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
5809 | *not* handle the exception-specification for FNTYPE, because the | |
5810 | initial substitution of explicitly provided template parameters | |
5811 | during argument deduction forbids substitution into the | |
5812 | exception-specification: | |
5813 | ||
5814 | [temp.deduct] | |
5815 | ||
5816 | All references in the function type of the function template to the | |
5817 | corresponding template parameters are replaced by the specified tem- | |
5818 | plate argument values. If a substitution in a template parameter or | |
5819 | in the function type of the function template results in an invalid | |
5820 | type, type deduction fails. [Note: The equivalent substitution in | |
5821 | exception specifications is done only when the function is instanti- | |
5822 | ated, at which point a program is ill-formed if the substitution | |
5823 | results in an invalid type.] */ | |
5824 | ||
5825 | static tree | |
5826 | tsubst_function_type (t, args, complain, in_decl) | |
5827 | tree t; | |
5828 | tree args; | |
5829 | int complain; | |
5830 | tree in_decl; | |
5831 | { | |
5832 | tree return_type; | |
5833 | tree arg_types; | |
5834 | tree fntype; | |
5835 | ||
5836 | /* The TYPE_CONTEXT is not used for function/method types. */ | |
5837 | my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); | |
5838 | ||
5839 | /* Substitue the return type. */ | |
5840 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
5841 | if (return_type == error_mark_node) | |
5842 | return error_mark_node; | |
5843 | ||
5844 | /* Substitue the argument types. */ | |
5845 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, | |
5846 | complain, in_decl); | |
5847 | if (arg_types == error_mark_node) | |
5848 | return error_mark_node; | |
5849 | ||
5850 | /* Construct a new type node and return it. */ | |
5851 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
5852 | fntype = build_function_type (return_type, arg_types); | |
5853 | else | |
5854 | { | |
5855 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
5856 | if (! IS_AGGR_TYPE (r)) | |
5857 | { | |
5858 | /* [temp.deduct] | |
5859 | ||
5860 | Type deduction may fail for any of the following | |
5861 | reasons: | |
5862 | ||
5863 | -- Attempting to create "pointer to member of T" when T | |
5864 | is not a class type. */ | |
5865 | if (complain) | |
5866 | cp_error ("creating pointer to member function of non-class type `%T'", | |
5867 | r); | |
5868 | return error_mark_node; | |
5869 | } | |
5870 | ||
5871 | fntype = build_cplus_method_type (r, return_type, TREE_CHAIN | |
5872 | (arg_types)); | |
5873 | } | |
5874 | fntype = build_qualified_type (fntype, TYPE_QUALS (t)); | |
5875 | ||
5876 | return fntype; | |
5877 | } | |
5878 | ||
297e73d8 MM |
5879 | /* Substitute into the PARMS of a call-declarator. */ |
5880 | ||
e9659ab0 | 5881 | static tree |
4393e105 | 5882 | tsubst_call_declarator_parms (parms, args, complain, in_decl) |
297e73d8 MM |
5883 | tree parms; |
5884 | tree args; | |
4393e105 | 5885 | int complain; |
297e73d8 MM |
5886 | tree in_decl; |
5887 | { | |
5888 | tree new_parms; | |
5889 | tree type; | |
5890 | tree defarg; | |
5891 | ||
5892 | if (!parms || parms == void_list_node) | |
5893 | return parms; | |
5894 | ||
5895 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
4393e105 | 5896 | args, complain, in_decl); |
297e73d8 MM |
5897 | |
5898 | /* Figure out the type of this parameter. */ | |
4393e105 | 5899 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); |
297e73d8 MM |
5900 | |
5901 | /* Figure out the default argument as well. Note that we use | |
4393e105 MM |
5902 | tsubst_expr since the default argument is really an expression. */ |
5903 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
297e73d8 MM |
5904 | |
5905 | /* Chain this parameter on to the front of those we have already | |
5906 | processed. We don't use hash_tree_cons because that function | |
5907 | doesn't check TREE_PARMLIST. */ | |
5908 | new_parms = tree_cons (defarg, type, new_parms); | |
5909 | ||
5910 | /* And note that these are parameters. */ | |
5911 | TREE_PARMLIST (new_parms) = 1; | |
5912 | ||
5913 | return new_parms; | |
5914 | } | |
5915 | ||
4393e105 MM |
5916 | /* Take the tree structure T and replace template parameters used |
5917 | therein with the argument vector ARGS. IN_DECL is an associated | |
5918 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
5919 | An appropriate error message is issued only if COMPLAIN is | |
5920 | non-zero. Note that we must be relatively non-tolerant of | |
5921 | extensions here, in order to preserve conformance; if we allow | |
5922 | substitutions that should not be allowed, we may allow argument | |
5923 | deductions that should not succeed, and therefore report ambiguous | |
5924 | overload situations where there are none. In theory, we could | |
5925 | allow the substitution, but indicate that it should have failed, | |
5926 | and allow our caller to make sure that the right thing happens, but | |
5927 | we don't try to do this yet. | |
5928 | ||
5929 | This function is used for dealing with types, decls and the like; | |
5930 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 MM |
5931 | |
5932 | tree | |
4393e105 | 5933 | tsubst (t, args, complain, in_decl) |
ae58fa02 | 5934 | tree t, args; |
4393e105 | 5935 | int complain; |
ae58fa02 MM |
5936 | tree in_decl; |
5937 | { | |
0ecfe0b4 | 5938 | tree type, r; |
ae58fa02 MM |
5939 | |
5940 | if (t == NULL_TREE || t == error_mark_node | |
5941 | || t == integer_type_node | |
5942 | || t == void_type_node | |
5943 | || t == char_type_node | |
5944 | || TREE_CODE (t) == NAMESPACE_DECL) | |
5945 | return t; | |
5946 | ||
5947 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
5948 | type = IDENTIFIER_TYPE_VALUE (t); | |
5949 | else | |
5950 | type = TREE_TYPE (t); | |
5951 | if (type == unknown_type_node) | |
5952 | my_friendly_abort (42); | |
5953 | ||
5954 | if (type && TREE_CODE (t) != FUNCTION_DECL | |
5955 | && TREE_CODE (t) != TYPENAME_TYPE | |
5956 | && TREE_CODE (t) != TEMPLATE_DECL | |
4393e105 MM |
5957 | && TREE_CODE (t) != IDENTIFIER_NODE |
5958 | && TREE_CODE (t) != FUNCTION_TYPE | |
5959 | && TREE_CODE (t) != METHOD_TYPE) | |
5960 | type = tsubst (type, args, complain, in_decl); | |
5961 | if (type == error_mark_node) | |
5962 | return error_mark_node; | |
ae58fa02 MM |
5963 | |
5964 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd') | |
5965 | return tsubst_decl (t, args, type, in_decl); | |
5966 | ||
5967 | switch (TREE_CODE (t)) | |
5968 | { | |
5969 | case RECORD_TYPE: | |
5970 | case UNION_TYPE: | |
5971 | case ENUMERAL_TYPE: | |
4393e105 MM |
5972 | return tsubst_aggr_type (t, args, complain, in_decl, |
5973 | /*entering_scope=*/0); | |
ae58fa02 MM |
5974 | |
5975 | case ERROR_MARK: | |
5976 | case IDENTIFIER_NODE: | |
5977 | case OP_IDENTIFIER: | |
5978 | case VOID_TYPE: | |
5979 | case REAL_TYPE: | |
5980 | case COMPLEX_TYPE: | |
5981 | case BOOLEAN_TYPE: | |
5982 | case INTEGER_CST: | |
5983 | case REAL_CST: | |
5984 | case STRING_CST: | |
5985 | return t; | |
5986 | ||
5987 | case INTEGER_TYPE: | |
5988 | if (t == integer_type_node) | |
5989 | return t; | |
5990 | ||
5991 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
5992 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
5993 | return t; | |
d2e5ee5c | 5994 | |
5566b478 | 5995 | { |
ddce3528 | 5996 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 5997 | |
ddce3528 | 5998 | max = tsubst_expr (omax, args, complain, in_decl); |
4393e105 MM |
5999 | if (max == error_mark_node) |
6000 | return error_mark_node; | |
6001 | ||
ae58fa02 MM |
6002 | if (processing_template_decl) |
6003 | { | |
6004 | tree itype = make_node (INTEGER_TYPE); | |
6005 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
6006 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, | |
6007 | integer_one_node); | |
6008 | return itype; | |
6009 | } | |
6010 | ||
ddce3528 JM |
6011 | if (integer_zerop (omax)) |
6012 | { | |
6013 | /* Still allow an explicit array of size zero. */ | |
6014 | if (pedantic) | |
6015 | pedwarn ("creating array with size zero"); | |
6016 | } | |
6017 | else if (integer_zerop (max) || INT_CST_LT (max, integer_zero_node)) | |
0ecfe0b4 | 6018 | { |
4393e105 MM |
6019 | /* [temp.deduct] |
6020 | ||
6021 | Type deduction may fail for any of the following | |
6022 | reasons: | |
6023 | ||
bf8f3f93 MM |
6024 | Attempting to create an array with a size that is |
6025 | zero or negative. */ | |
4393e105 MM |
6026 | if (complain) |
6027 | cp_error ("creating array with size `%E'", max); | |
6028 | ||
6029 | return error_mark_node; | |
0ecfe0b4 JM |
6030 | } |
6031 | ||
ae58fa02 | 6032 | max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1)); |
7ddedda4 MM |
6033 | if (!TREE_PERMANENT (max) && !allocation_temporary_p ()) |
6034 | max = copy_to_permanent (max); | |
120ecb8a | 6035 | return build_index_type (max); |
ae58fa02 MM |
6036 | } |
6037 | ||
6038 | case TEMPLATE_TYPE_PARM: | |
6039 | case TEMPLATE_TEMPLATE_PARM: | |
6040 | case TEMPLATE_PARM_INDEX: | |
6041 | { | |
6042 | int idx; | |
6043 | int level; | |
6044 | int levels; | |
0ecfe0b4 JM |
6045 | |
6046 | r = NULL_TREE; | |
ae58fa02 MM |
6047 | |
6048 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
6049 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6050 | { | |
6051 | idx = TEMPLATE_TYPE_IDX (t); | |
6052 | level = TEMPLATE_TYPE_LEVEL (t); | |
6053 | } | |
6054 | else | |
6055 | { | |
6056 | idx = TEMPLATE_PARM_IDX (t); | |
6057 | level = TEMPLATE_PARM_LEVEL (t); | |
6058 | } | |
6059 | ||
6060 | if (TREE_VEC_LENGTH (args) > 0) | |
6061 | { | |
6062 | tree arg = NULL_TREE; | |
6063 | ||
6064 | levels = TMPL_ARGS_DEPTH (args); | |
6065 | if (level <= levels) | |
6066 | arg = TMPL_ARG (args, level, idx); | |
6067 | ||
8b5b8b7c MM |
6068 | if (arg == error_mark_node) |
6069 | return error_mark_node; | |
6070 | else if (arg != NULL_TREE) | |
ae58fa02 MM |
6071 | { |
6072 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
6073 | { | |
6074 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg)) | |
6075 | == 't', 0); | |
91063b51 MM |
6076 | return cp_build_qualified_type |
6077 | (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t)); | |
ae58fa02 MM |
6078 | } |
6079 | else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) | |
6080 | { | |
7ddedda4 | 6081 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 MM |
6082 | { |
6083 | /* We are processing a type constructed from | |
6084 | a template template parameter */ | |
7ddedda4 | 6085 | tree argvec = tsubst (TYPE_TI_ARGS (t), |
4393e105 MM |
6086 | args, complain, in_decl); |
6087 | if (argvec == error_mark_node) | |
6088 | return error_mark_node; | |
6089 | ||
ae58fa02 MM |
6090 | /* We can get a TEMPLATE_TEMPLATE_PARM here when |
6091 | we are resolving nested-types in the signature of | |
6092 | a member function templates. | |
6093 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
6094 | template to be instantiated. */ | |
6095 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
6096 | arg = TYPE_NAME (arg); | |
6097 | ||
6098 | r = lookup_template_class (DECL_NAME (arg), | |
6099 | argvec, in_decl, | |
6100 | DECL_CONTEXT (arg), | |
6101 | /*entering_scope=*/0); | |
91063b51 | 6102 | return cp_build_qualified_type (r, TYPE_QUALS (t)); |
ae58fa02 MM |
6103 | } |
6104 | else | |
6105 | /* We are processing a template argument list. */ | |
6106 | return arg; | |
6107 | } | |
6108 | else | |
6109 | return arg; | |
6110 | } | |
6111 | } | |
51924768 JM |
6112 | else |
6113 | my_friendly_abort (981018); | |
ae58fa02 MM |
6114 | |
6115 | if (level == 1) | |
6116 | /* This can happen during the attempted tsubst'ing in | |
6117 | unify. This means that we don't yet have any information | |
6118 | about the template parameter in question. */ | |
6119 | return t; | |
6120 | ||
6121 | /* If we get here, we must have been looking at a parm for a | |
6122 | more deeply nested template. Make a new version of this | |
6123 | template parameter, but with a lower level. */ | |
6124 | switch (TREE_CODE (t)) | |
6125 | { | |
6126 | case TEMPLATE_TYPE_PARM: | |
6127 | case TEMPLATE_TEMPLATE_PARM: | |
6128 | r = copy_node (t); | |
6129 | TEMPLATE_TYPE_PARM_INDEX (r) | |
6130 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
6131 | r, levels); | |
6132 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
6133 | TYPE_MAIN_VARIANT (r) = r; | |
6134 | TYPE_POINTER_TO (r) = NULL_TREE; | |
6135 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
6136 | ||
6137 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
7ddedda4 | 6138 | && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)) |
ae58fa02 | 6139 | { |
4393e105 MM |
6140 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, |
6141 | complain, in_decl); | |
6142 | if (argvec == error_mark_node) | |
6143 | return error_mark_node; | |
6144 | ||
7ddedda4 | 6145 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
ae58fa02 MM |
6146 | = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE); |
6147 | } | |
6148 | break; | |
6149 | ||
6150 | case TEMPLATE_PARM_INDEX: | |
6151 | r = reduce_template_parm_level (t, type, levels); | |
6152 | break; | |
6153 | ||
6154 | default: | |
6155 | my_friendly_abort (0); | |
6156 | } | |
6157 | ||
5566b478 | 6158 | return r; |
ae58fa02 | 6159 | } |
5566b478 | 6160 | |
8d08fdba MS |
6161 | case TREE_LIST: |
6162 | { | |
6163 | tree purpose, value, chain, result; | |
6164 | int via_public, via_virtual, via_protected; | |
6165 | ||
6166 | if (t == void_list_node) | |
6167 | return t; | |
6168 | ||
6169 | via_public = TREE_VIA_PUBLIC (t); | |
6170 | via_protected = TREE_VIA_PROTECTED (t); | |
6171 | via_virtual = TREE_VIA_VIRTUAL (t); | |
6172 | ||
6173 | purpose = TREE_PURPOSE (t); | |
6174 | if (purpose) | |
4393e105 MM |
6175 | { |
6176 | purpose = tsubst (purpose, args, complain, in_decl); | |
6177 | if (purpose == error_mark_node) | |
6178 | return error_mark_node; | |
6179 | } | |
8d08fdba MS |
6180 | value = TREE_VALUE (t); |
6181 | if (value) | |
4393e105 MM |
6182 | { |
6183 | value = tsubst (value, args, complain, in_decl); | |
6184 | if (value == error_mark_node) | |
6185 | return error_mark_node; | |
6186 | } | |
8d08fdba MS |
6187 | chain = TREE_CHAIN (t); |
6188 | if (chain && chain != void_type_node) | |
4393e105 MM |
6189 | { |
6190 | chain = tsubst (chain, args, complain, in_decl); | |
6191 | if (chain == error_mark_node) | |
6192 | return error_mark_node; | |
6193 | } | |
8d08fdba MS |
6194 | if (purpose == TREE_PURPOSE (t) |
6195 | && value == TREE_VALUE (t) | |
6196 | && chain == TREE_CHAIN (t)) | |
6197 | return t; | |
6198 | result = hash_tree_cons (via_public, via_virtual, via_protected, | |
6199 | purpose, value, chain); | |
6200 | TREE_PARMLIST (result) = TREE_PARMLIST (t); | |
6201 | return result; | |
6202 | } | |
6203 | case TREE_VEC: | |
5566b478 MS |
6204 | if (type != NULL_TREE) |
6205 | { | |
1ceaad38 JM |
6206 | /* A binfo node. We always need to make a copy, of the node |
6207 | itself and of its BINFO_BASETYPES. */ | |
85b71cf2 | 6208 | |
5566b478 MS |
6209 | t = copy_node (t); |
6210 | ||
c7449f2b JM |
6211 | /* Make sure type isn't a typedef copy. */ |
6212 | type = BINFO_TYPE (TYPE_BINFO (type)); | |
6213 | ||
5566b478 | 6214 | TREE_TYPE (t) = complete_type (type); |
6633d636 MS |
6215 | if (IS_AGGR_TYPE (type)) |
6216 | { | |
6217 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
6218 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
6219 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
6220 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
6221 | } | |
5566b478 MS |
6222 | return t; |
6223 | } | |
85b71cf2 JM |
6224 | |
6225 | /* Otherwise, a vector of template arguments. */ | |
4393e105 | 6226 | return tsubst_template_arg_vector (t, args, complain); |
8d08fdba | 6227 | |
8d08fdba MS |
6228 | case POINTER_TYPE: |
6229 | case REFERENCE_TYPE: | |
6230 | { | |
8d08fdba | 6231 | enum tree_code code; |
79a7c7fa | 6232 | |
8d08fdba MS |
6233 | if (type == TREE_TYPE (t)) |
6234 | return t; | |
6235 | ||
6236 | code = TREE_CODE (t); | |
4393e105 MM |
6237 | |
6238 | ||
6239 | /* [temp.deduct] | |
6240 | ||
6241 | Type deduction may fail for any of the following | |
6242 | reasons: | |
6243 | ||
6244 | -- Attempting to create a pointer to reference type. | |
6245 | -- Attempting to create a reference to a reference type or | |
6246 | a reference to void. */ | |
0ecfe0b4 JM |
6247 | if (TREE_CODE (type) == REFERENCE_TYPE |
6248 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa JM |
6249 | { |
6250 | static int last_line = 0; | |
6251 | static char* last_file = 0; | |
6252 | ||
6253 | /* We keep track of the last time we issued this error | |
6254 | message to avoid spewing a ton of messages during a | |
6255 | single bad template instantiation. */ | |
4393e105 MM |
6256 | if (complain && (last_line != lineno || |
6257 | last_file != input_filename)) | |
79a7c7fa | 6258 | { |
0ecfe0b4 JM |
6259 | if (TREE_CODE (type) == VOID_TYPE) |
6260 | cp_error ("forming reference to void"); | |
6261 | else | |
6262 | cp_error ("forming %s to reference type `%T'", | |
6263 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
6264 | type); | |
79a7c7fa JM |
6265 | last_line = lineno; |
6266 | last_file = input_filename; | |
6267 | } | |
6268 | ||
4393e105 | 6269 | return error_mark_node; |
79a7c7fa JM |
6270 | } |
6271 | else if (code == POINTER_TYPE) | |
8d08fdba MS |
6272 | r = build_pointer_type (type); |
6273 | else | |
6274 | r = build_reference_type (type); | |
91063b51 | 6275 | r = cp_build_qualified_type (r, TYPE_QUALS (t)); |
79a7c7fa | 6276 | |
8d08fdba MS |
6277 | /* Will this ever be needed for TYPE_..._TO values? */ |
6278 | layout_type (r); | |
6279 | return r; | |
6280 | } | |
a4443a08 | 6281 | case OFFSET_TYPE: |
0ecfe0b4 | 6282 | { |
4393e105 MM |
6283 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
6284 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
6285 | { | |
6286 | /* [temp.deduct] | |
6287 | ||
6288 | Type deduction may fail for any of the following | |
6289 | reasons: | |
6290 | ||
6291 | -- Attempting to create "pointer to member of T" when T | |
6292 | is not a class type. */ | |
6293 | if (complain) | |
6294 | cp_error ("creating pointer to member of non-class type `%T'", | |
6295 | r); | |
6296 | return error_mark_node; | |
6297 | } | |
0ecfe0b4 JM |
6298 | return build_offset_type (r, type); |
6299 | } | |
8d08fdba MS |
6300 | case FUNCTION_TYPE: |
6301 | case METHOD_TYPE: | |
6302 | { | |
c11b6f21 | 6303 | tree fntype; |
4393e105 | 6304 | tree raises; |
8d08fdba | 6305 | |
4393e105 MM |
6306 | fntype = tsubst_function_type (t, args, complain, in_decl); |
6307 | if (fntype == error_mark_node) | |
6308 | return error_mark_node; | |
cabc336a MM |
6309 | |
6310 | /* Substitue the exception specification. */ | |
6311 | raises = TYPE_RAISES_EXCEPTIONS (t); | |
c11b6f21 MS |
6312 | if (raises) |
6313 | { | |
4393e105 MM |
6314 | raises = tsubst (raises, args, complain, in_decl); |
6315 | if (raises == error_mark_node) | |
6316 | return raises; | |
c11b6f21 MS |
6317 | fntype = build_exception_variant (fntype, raises); |
6318 | } | |
6319 | return fntype; | |
8d08fdba MS |
6320 | } |
6321 | case ARRAY_TYPE: | |
6322 | { | |
4393e105 MM |
6323 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
6324 | if (domain == error_mark_node) | |
6325 | return error_mark_node; | |
6326 | ||
6327 | /* As an optimization, we avoid regenerating the array type if | |
6328 | it will obviously be the same as T. */ | |
8d08fdba MS |
6329 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
6330 | return t; | |
0ecfe0b4 | 6331 | |
4393e105 MM |
6332 | /* These checks should match the ones in grokdeclarator. |
6333 | ||
6334 | [temp.deduct] | |
6335 | ||
6336 | The deduction may fail for any of the following reasons: | |
6337 | ||
6338 | -- Attempting to create an array with an element type that | |
6339 | is void, a function type, or a reference type. */ | |
6340 | if (TREE_CODE (type) == VOID_TYPE | |
6341 | || TREE_CODE (type) == FUNCTION_TYPE | |
6342 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 6343 | { |
4393e105 MM |
6344 | if (complain) |
6345 | cp_error ("creating array of `%T'", type); | |
6346 | return error_mark_node; | |
0ecfe0b4 JM |
6347 | } |
6348 | ||
8d08fdba MS |
6349 | r = build_cplus_array_type (type, domain); |
6350 | return r; | |
6351 | } | |
6352 | ||
8d08fdba | 6353 | case PLUS_EXPR: |
5566b478 | 6354 | case MINUS_EXPR: |
4393e105 MM |
6355 | { |
6356 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6357 | in_decl); | |
6358 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, | |
6359 | in_decl); | |
6360 | ||
6361 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6362 | return error_mark_node; | |
6363 | ||
6364 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); | |
6365 | } | |
8d08fdba MS |
6366 | |
6367 | case NEGATE_EXPR: | |
6368 | case NOP_EXPR: | |
4393e105 MM |
6369 | { |
6370 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6371 | in_decl); | |
6372 | if (e == error_mark_node) | |
6373 | return error_mark_node; | |
6374 | ||
6375 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); | |
6376 | } | |
8d08fdba | 6377 | |
5566b478 MS |
6378 | case TYPENAME_TYPE: |
6379 | { | |
4393e105 MM |
6380 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
6381 | in_decl, /*entering_scope=*/1); | |
6382 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
6383 | complain, in_decl); | |
6384 | ||
6385 | if (ctx == error_mark_node || f == error_mark_node) | |
6386 | return error_mark_node; | |
ae58fa02 | 6387 | |
bf8f3f93 MM |
6388 | if (!IS_AGGR_TYPE (ctx)) |
6389 | { | |
6390 | if (complain) | |
6391 | cp_error ("`%T' is not a class, struct, or union type", | |
6392 | ctx); | |
6393 | return error_mark_node; | |
6394 | } | |
6395 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
6396 | { | |
6397 | /* Normally, make_typename_type does not require that the CTX | |
6398 | have complete type in order to allow things like: | |
ae58fa02 | 6399 | |
bf8f3f93 | 6400 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 6401 | |
bf8f3f93 MM |
6402 | But, such constructs have already been resolved by this |
6403 | point, so here CTX really should have complete type, unless | |
6404 | it's a partial instantiation. */ | |
4393e105 MM |
6405 | ctx = complete_type (ctx); |
6406 | if (!TYPE_SIZE (ctx)) | |
6407 | { | |
6408 | if (complain) | |
6409 | incomplete_type_error (NULL_TREE, ctx); | |
6410 | return error_mark_node; | |
6411 | } | |
6412 | } | |
ae58fa02 | 6413 | |
b2b7d40a | 6414 | f = make_typename_type (ctx, f); |
91063b51 MM |
6415 | return cp_build_qualified_type (f, |
6416 | CP_TYPE_QUALS (f) | |
6417 | | CP_TYPE_QUALS (t)); | |
5566b478 MS |
6418 | } |
6419 | ||
6420 | case INDIRECT_REF: | |
4393e105 MM |
6421 | { |
6422 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6423 | in_decl); | |
6424 | if (e == error_mark_node) | |
6425 | return error_mark_node; | |
6426 | return make_pointer_declarator (type, e); | |
6427 | } | |
6428 | ||
5566b478 | 6429 | case ADDR_EXPR: |
4393e105 MM |
6430 | { |
6431 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6432 | in_decl); | |
6433 | if (e == error_mark_node) | |
6434 | return error_mark_node; | |
6435 | return make_reference_declarator (type, e); | |
6436 | } | |
5566b478 MS |
6437 | |
6438 | case ARRAY_REF: | |
4393e105 MM |
6439 | { |
6440 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6441 | in_decl); | |
6442 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, | |
6443 | in_decl); | |
6444 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6445 | return error_mark_node; | |
6446 | ||
6447 | return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr); | |
6448 | } | |
5566b478 MS |
6449 | |
6450 | case CALL_EXPR: | |
4393e105 MM |
6451 | { |
6452 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6453 | in_decl); | |
6454 | tree e2 = tsubst_call_declarator_parms (TREE_OPERAND (t, 1), args, | |
6455 | complain, in_decl); | |
6456 | tree e3 = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6457 | ||
6458 | if (e1 == error_mark_node || e2 == error_mark_node | |
6459 | || e3 == error_mark_node) | |
6460 | return error_mark_node; | |
6461 | ||
6462 | return make_call_declarator (e1, e2, TREE_OPERAND (t, 2), e3); | |
6463 | } | |
5566b478 | 6464 | |
fc378698 | 6465 | case SCOPE_REF: |
4393e105 MM |
6466 | { |
6467 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, | |
6468 | in_decl); | |
6469 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
6470 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6471 | return error_mark_node; | |
6472 | ||
6473 | return build_parse_node (TREE_CODE (t), e1, e2); | |
6474 | } | |
fc378698 | 6475 | |
b894fc05 | 6476 | case TYPEOF_TYPE: |
4393e105 MM |
6477 | { |
6478 | tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, | |
6479 | in_decl); | |
6480 | if (e1 == error_mark_node) | |
6481 | return error_mark_node; | |
6482 | ||
6483 | return TREE_TYPE (e1); | |
6484 | } | |
b894fc05 | 6485 | |
8d08fdba | 6486 | default: |
5566b478 | 6487 | sorry ("use of `%s' in template", |
8d08fdba MS |
6488 | tree_code_name [(int) TREE_CODE (t)]); |
6489 | return error_mark_node; | |
6490 | } | |
6491 | } | |
6492 | ||
5566b478 MS |
6493 | void |
6494 | do_pushlevel () | |
6495 | { | |
6496 | emit_line_note (input_filename, lineno); | |
6497 | pushlevel (0); | |
6498 | clear_last_expr (); | |
6499 | push_momentary (); | |
6500 | expand_start_bindings (0); | |
6501 | } | |
6502 | ||
8d08fdba | 6503 | tree |
5566b478 | 6504 | do_poplevel () |
8d08fdba | 6505 | { |
5566b478 | 6506 | tree t; |
a703fb38 | 6507 | int saved_warn_unused = 0; |
8d08fdba | 6508 | |
85b71cf2 JM |
6509 | if (processing_template_decl) |
6510 | { | |
6511 | saved_warn_unused = warn_unused; | |
6512 | warn_unused = 0; | |
6513 | } | |
8baa713c | 6514 | expand_end_bindings (getdecls (), kept_level_p (), 0); |
85b71cf2 JM |
6515 | if (processing_template_decl) |
6516 | warn_unused = saved_warn_unused; | |
5566b478 MS |
6517 | t = poplevel (kept_level_p (), 1, 0); |
6518 | pop_momentary (); | |
6519 | return t; | |
6520 | } | |
8d08fdba | 6521 | |
00d3396f JM |
6522 | /* Like tsubst, but deals with expressions. This function just replaces |
6523 | template parms; to finish processing the resultant expression, use | |
6524 | tsubst_expr. */ | |
6525 | ||
5566b478 | 6526 | tree |
4393e105 | 6527 | tsubst_copy (t, args, complain, in_decl) |
98c1c668 | 6528 | tree t, args; |
4393e105 | 6529 | int complain; |
5566b478 MS |
6530 | tree in_decl; |
6531 | { | |
6532 | enum tree_code code; | |
8452b1d3 | 6533 | tree r; |
8d08fdba | 6534 | |
5566b478 MS |
6535 | if (t == NULL_TREE || t == error_mark_node) |
6536 | return t; | |
6537 | ||
6538 | code = TREE_CODE (t); | |
b7484fbe | 6539 | |
5566b478 MS |
6540 | switch (code) |
6541 | { | |
6542 | case PARM_DECL: | |
a759e627 | 6543 | return do_identifier (DECL_NAME (t), 0, NULL_TREE); |
5566b478 MS |
6544 | |
6545 | case CONST_DECL: | |
ed44da02 MM |
6546 | { |
6547 | tree enum_type; | |
6548 | tree v; | |
6549 | ||
6550 | if (!DECL_CONTEXT (t)) | |
6551 | /* This is a global enumeration constant. */ | |
6552 | return t; | |
6553 | ||
6554 | /* Unfortunately, we cannot just call lookup_name here. | |
6555 | Consider: | |
6556 | ||
6557 | template <int I> int f() { | |
6558 | enum E { a = I }; | |
6559 | struct S { void g() { E e = a; } }; | |
6560 | }; | |
6561 | ||
6562 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
6563 | clever enough to find f<7>::a. */ | |
6564 | enum_type | |
4393e105 | 6565 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
ed44da02 MM |
6566 | /*entering_scope=*/0); |
6567 | ||
6568 | for (v = TYPE_VALUES (enum_type); | |
6569 | v != NULL_TREE; | |
6570 | v = TREE_CHAIN (v)) | |
6571 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
6572 | return TREE_VALUE (v); | |
6573 | ||
6574 | /* We didn't find the name. That should never happen; if | |
6575 | name-lookup found it during preliminary parsing, we | |
6576 | should find it again here during instantiation. */ | |
6577 | my_friendly_abort (0); | |
6578 | } | |
db04386f | 6579 | return t; |
ed44da02 | 6580 | |
5566b478 MS |
6581 | case FIELD_DECL: |
6582 | if (DECL_CONTEXT (t)) | |
6583 | { | |
0978790f | 6584 | tree ctx; |
0978790f | 6585 | |
4393e105 | 6586 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 6587 | /*entering_scope=*/1); |
0978790f | 6588 | if (ctx != DECL_CONTEXT (t)) |
5566b478 MS |
6589 | return lookup_field (ctx, DECL_NAME (t), 0, 0); |
6590 | } | |
6591 | return t; | |
6592 | ||
6593 | case VAR_DECL: | |
6594 | case FUNCTION_DECL: | |
6595 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
4393e105 | 6596 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
6597 | mark_used (t); |
6598 | return t; | |
6599 | ||
98c1c668 JM |
6600 | case TEMPLATE_DECL: |
6601 | if (is_member_template (t)) | |
4393e105 | 6602 | return tsubst (t, args, complain, in_decl); |
98c1c668 JM |
6603 | else |
6604 | return t; | |
6605 | ||
672476cb MM |
6606 | case LOOKUP_EXPR: |
6607 | { | |
6608 | /* We must tsbust into a LOOKUP_EXPR in case the names to | |
6609 | which it refers is a conversion operator; in that case the | |
6610 | name will change. We avoid making unnecessary copies, | |
6611 | however. */ | |
6612 | ||
4393e105 | 6613 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
672476cb MM |
6614 | |
6615 | if (id != TREE_OPERAND (t, 0)) | |
6616 | { | |
8452b1d3 | 6617 | r = build_nt (LOOKUP_EXPR, id); |
672476cb MM |
6618 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); |
6619 | t = r; | |
6620 | } | |
6621 | ||
6622 | return t; | |
6623 | } | |
6624 | ||
5566b478 MS |
6625 | case CAST_EXPR: |
6626 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
6627 | case CONST_CAST_EXPR: |
6628 | case STATIC_CAST_EXPR: | |
6629 | case DYNAMIC_CAST_EXPR: | |
51924768 | 6630 | case NOP_EXPR: |
5566b478 | 6631 | return build1 |
4393e105 MM |
6632 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6633 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
6634 | |
6635 | case INDIRECT_REF: | |
6636 | case PREDECREMENT_EXPR: | |
6637 | case PREINCREMENT_EXPR: | |
6638 | case POSTDECREMENT_EXPR: | |
6639 | case POSTINCREMENT_EXPR: | |
6640 | case NEGATE_EXPR: | |
6641 | case TRUTH_NOT_EXPR: | |
b87692e5 | 6642 | case BIT_NOT_EXPR: |
5566b478 MS |
6643 | case ADDR_EXPR: |
6644 | case CONVERT_EXPR: /* Unary + */ | |
6645 | case SIZEOF_EXPR: | |
abff8e06 | 6646 | case ALIGNOF_EXPR: |
5566b478 | 6647 | case ARROW_EXPR: |
fc378698 | 6648 | case THROW_EXPR: |
5156628f | 6649 | case TYPEID_EXPR: |
5566b478 MS |
6650 | return build1 |
6651 | (code, NULL_TREE, | |
4393e105 | 6652 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 MS |
6653 | |
6654 | case PLUS_EXPR: | |
6655 | case MINUS_EXPR: | |
6656 | case MULT_EXPR: | |
6657 | case TRUNC_DIV_EXPR: | |
6658 | case CEIL_DIV_EXPR: | |
6659 | case FLOOR_DIV_EXPR: | |
6660 | case ROUND_DIV_EXPR: | |
6661 | case EXACT_DIV_EXPR: | |
6662 | case BIT_AND_EXPR: | |
6663 | case BIT_ANDTC_EXPR: | |
6664 | case BIT_IOR_EXPR: | |
6665 | case BIT_XOR_EXPR: | |
6666 | case TRUNC_MOD_EXPR: | |
6667 | case FLOOR_MOD_EXPR: | |
6668 | case TRUTH_ANDIF_EXPR: | |
6669 | case TRUTH_ORIF_EXPR: | |
6670 | case TRUTH_AND_EXPR: | |
6671 | case TRUTH_OR_EXPR: | |
6672 | case RSHIFT_EXPR: | |
6673 | case LSHIFT_EXPR: | |
6674 | case RROTATE_EXPR: | |
6675 | case LROTATE_EXPR: | |
6676 | case EQ_EXPR: | |
6677 | case NE_EXPR: | |
6678 | case MAX_EXPR: | |
6679 | case MIN_EXPR: | |
6680 | case LE_EXPR: | |
6681 | case GE_EXPR: | |
6682 | case LT_EXPR: | |
6683 | case GT_EXPR: | |
6684 | case COMPONENT_REF: | |
6685 | case ARRAY_REF: | |
6686 | case COMPOUND_EXPR: | |
6687 | case SCOPE_REF: | |
6688 | case DOTSTAR_EXPR: | |
6689 | case MEMBER_REF: | |
6690 | return build_nt | |
4393e105 MM |
6691 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6692 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
6693 | |
6694 | case CALL_EXPR: | |
6695 | { | |
6696 | tree fn = TREE_OPERAND (t, 0); | |
f84b4be9 | 6697 | if (is_overloaded_fn (fn)) |
4393e105 | 6698 | fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl); |
5566b478 | 6699 | else |
f84b4be9 | 6700 | /* Sometimes FN is a LOOKUP_EXPR. */ |
4393e105 | 6701 | fn = tsubst_copy (fn, args, complain, in_decl); |
5566b478 | 6702 | return build_nt |
4393e105 MM |
6703 | (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
6704 | in_decl), | |
5566b478 MS |
6705 | NULL_TREE); |
6706 | } | |
6707 | ||
6708 | case METHOD_CALL_EXPR: | |
6709 | { | |
6710 | tree name = TREE_OPERAND (t, 0); | |
6711 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
6712 | { | |
4393e105 MM |
6713 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
6714 | complain, in_decl); | |
7bae46f4 | 6715 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6716 | } |
6717 | else if (TREE_CODE (name) == SCOPE_REF | |
6718 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
6719 | { | |
4393e105 MM |
6720 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, |
6721 | complain, in_decl); | |
5566b478 | 6722 | name = TREE_OPERAND (name, 1); |
4393e105 MM |
6723 | name = tsubst_copy (TREE_OPERAND (name, 0), args, |
6724 | complain, in_decl); | |
11686454 | 6725 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); |
5566b478 MS |
6726 | name = build_nt (SCOPE_REF, base, name); |
6727 | } | |
6728 | else | |
4393e105 | 6729 | name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); |
5566b478 | 6730 | return build_nt |
4393e105 MM |
6731 | (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, |
6732 | complain, in_decl), | |
6733 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), | |
5566b478 MS |
6734 | NULL_TREE); |
6735 | } | |
6736 | ||
67da3287 | 6737 | case BIND_EXPR: |
5566b478 MS |
6738 | case COND_EXPR: |
6739 | case MODOP_EXPR: | |
67da3287 | 6740 | { |
8452b1d3 | 6741 | r = build_nt |
4393e105 MM |
6742 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6743 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
6744 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
67da3287 MM |
6745 | |
6746 | if (code == BIND_EXPR && !processing_template_decl) | |
6747 | { | |
61cd552e | 6748 | /* This processing should really occur in tsubst_expr, |
67da3287 MM |
6749 | However, tsubst_expr does not recurse into expressions, |
6750 | since it assumes that there aren't any statements | |
6751 | inside them. Instead, it simply calls | |
6752 | build_expr_from_tree. So, we need to expand the | |
6753 | BIND_EXPR here. */ | |
b4c4a9ec | 6754 | tree rtl_expr = begin_stmt_expr (); |
4393e105 MM |
6755 | tree block = tsubst_expr (TREE_OPERAND (r, 1), args, |
6756 | complain, in_decl); | |
b4c4a9ec | 6757 | r = finish_stmt_expr (rtl_expr, block); |
67da3287 MM |
6758 | } |
6759 | ||
6760 | return r; | |
6761 | } | |
5566b478 MS |
6762 | |
6763 | case NEW_EXPR: | |
6764 | { | |
8452b1d3 | 6765 | r = build_nt |
4393e105 MM |
6766 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
6767 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
6768 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
6769 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
6770 | return r; | |
6771 | } | |
6772 | ||
6773 | case DELETE_EXPR: | |
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)); | |
5566b478 MS |
6778 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
6779 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
6780 | return r; | |
6781 | } | |
6782 | ||
386b8a85 JM |
6783 | case TEMPLATE_ID_EXPR: |
6784 | { | |
00d3396f | 6785 | /* Substituted template arguments */ |
4393e105 MM |
6786 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, |
6787 | in_decl); | |
00d3396f JM |
6788 | tree chain; |
6789 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
6790 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
6791 | ||
6792 | return lookup_template_function | |
4393e105 | 6793 | (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); |
386b8a85 JM |
6794 | } |
6795 | ||
5566b478 MS |
6796 | case TREE_LIST: |
6797 | { | |
6798 | tree purpose, value, chain; | |
6799 | ||
6800 | if (t == void_list_node) | |
6801 | return t; | |
6802 | ||
6803 | purpose = TREE_PURPOSE (t); | |
6804 | if (purpose) | |
4393e105 | 6805 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
6806 | value = TREE_VALUE (t); |
6807 | if (value) | |
4393e105 | 6808 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
6809 | chain = TREE_CHAIN (t); |
6810 | if (chain && chain != void_type_node) | |
4393e105 | 6811 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
6812 | if (purpose == TREE_PURPOSE (t) |
6813 | && value == TREE_VALUE (t) | |
6814 | && chain == TREE_CHAIN (t)) | |
6815 | return t; | |
6816 | return tree_cons (purpose, value, chain); | |
6817 | } | |
6818 | ||
6819 | case RECORD_TYPE: | |
6820 | case UNION_TYPE: | |
6821 | case ENUMERAL_TYPE: | |
6822 | case INTEGER_TYPE: | |
6823 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 6824 | case TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 6825 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
6826 | case POINTER_TYPE: |
6827 | case REFERENCE_TYPE: | |
6828 | case OFFSET_TYPE: | |
6829 | case FUNCTION_TYPE: | |
6830 | case METHOD_TYPE: | |
6831 | case ARRAY_TYPE: | |
6832 | case TYPENAME_TYPE: | |
f84b4be9 | 6833 | case TYPE_DECL: |
4393e105 | 6834 | return tsubst (t, args, complain, in_decl); |
5566b478 | 6835 | |
e92cc029 | 6836 | case IDENTIFIER_NODE: |
8e710788 BK |
6837 | if (IDENTIFIER_TYPENAME_P (t) |
6838 | /* Make sure it's not just a variable named `__opr', for instance, | |
6839 | which can occur in some existing code. */ | |
6840 | && TREE_TYPE (t)) | |
e92cc029 | 6841 | return build_typename_overload |
4393e105 | 6842 | (tsubst (TREE_TYPE (t), args, complain, in_decl)); |
e92cc029 MS |
6843 | else |
6844 | return t; | |
6845 | ||
5156628f | 6846 | case CONSTRUCTOR: |
8452b1d3 JM |
6847 | { |
6848 | r = build | |
4393e105 MM |
6849 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), |
6850 | NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, | |
6851 | complain, in_decl)); | |
8452b1d3 JM |
6852 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
6853 | return r; | |
6854 | } | |
5156628f | 6855 | |
5566b478 MS |
6856 | default: |
6857 | return t; | |
6858 | } | |
6859 | } | |
6860 | ||
00d3396f JM |
6861 | /* Like tsubst_copy, but also does semantic processing and RTL expansion. */ |
6862 | ||
5566b478 | 6863 | tree |
4393e105 | 6864 | tsubst_expr (t, args, complain, in_decl) |
98c1c668 | 6865 | tree t, args; |
4393e105 | 6866 | int complain; |
5566b478 MS |
6867 | tree in_decl; |
6868 | { | |
6869 | if (t == NULL_TREE || t == error_mark_node) | |
6870 | return t; | |
6871 | ||
5156628f | 6872 | if (processing_template_decl) |
4393e105 | 6873 | return tsubst_copy (t, args, complain, in_decl); |
5566b478 MS |
6874 | |
6875 | switch (TREE_CODE (t)) | |
8d08fdba | 6876 | { |
5566b478 MS |
6877 | case RETURN_STMT: |
6878 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6879 | finish_return_stmt (tsubst_expr (RETURN_EXPR (t), |
4393e105 | 6880 | args, complain, in_decl)); |
5566b478 MS |
6881 | break; |
6882 | ||
6883 | case EXPR_STMT: | |
6884 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6885 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), |
4393e105 | 6886 | args, complain, in_decl)); |
5566b478 MS |
6887 | break; |
6888 | ||
6889 | case DECL_STMT: | |
6890 | { | |
6891 | int i = suspend_momentary (); | |
67d743fe | 6892 | tree dcl, init; |
5566b478 MS |
6893 | |
6894 | lineno = TREE_COMPLEXITY (t); | |
6895 | emit_line_note (input_filename, lineno); | |
6896 | dcl = start_decl | |
4393e105 MM |
6897 | (tsubst (TREE_OPERAND (t, 0), args, complain, in_decl), |
6898 | tsubst (TREE_OPERAND (t, 1), args, complain, in_decl), | |
a1774733 | 6899 | TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE); |
4393e105 | 6900 | init = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl); |
5566b478 | 6901 | cp_finish_decl |
a0128b67 | 6902 | (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0); |
5566b478 MS |
6903 | resume_momentary (i); |
6904 | return dcl; | |
6905 | } | |
8d08fdba | 6906 | |
5566b478 MS |
6907 | case FOR_STMT: |
6908 | { | |
6909 | tree tmp; | |
5566b478 | 6910 | lineno = TREE_COMPLEXITY (t); |
5566b478 | 6911 | |
ad321293 MM |
6912 | begin_for_stmt (); |
6913 | for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp)) | |
4393e105 | 6914 | tsubst_expr (tmp, args, complain, in_decl); |
ad321293 MM |
6915 | finish_for_init_stmt (NULL_TREE); |
6916 | finish_for_cond (tsubst_expr (FOR_COND (t), args, | |
4393e105 | 6917 | complain, in_decl), |
ad321293 | 6918 | NULL_TREE); |
4393e105 | 6919 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); |
ad321293 | 6920 | finish_for_expr (tmp, NULL_TREE); |
4393e105 | 6921 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); |
ad321293 | 6922 | finish_for_stmt (tmp, NULL_TREE); |
5566b478 MS |
6923 | } |
6924 | break; | |
8d08fdba | 6925 | |
5566b478 MS |
6926 | case WHILE_STMT: |
6927 | { | |
5566b478 | 6928 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6929 | begin_while_stmt (); |
6930 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), | |
4393e105 | 6931 | args, complain, in_decl), |
ad321293 | 6932 | NULL_TREE); |
4393e105 | 6933 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); |
ad321293 | 6934 | finish_while_stmt (NULL_TREE); |
5566b478 MS |
6935 | } |
6936 | break; | |
8d08fdba | 6937 | |
5566b478 MS |
6938 | case DO_STMT: |
6939 | { | |
5566b478 | 6940 | lineno = TREE_COMPLEXITY (t); |
ad321293 | 6941 | begin_do_stmt (); |
4393e105 | 6942 | tsubst_expr (DO_BODY (t), args, complain, in_decl); |
ad321293 MM |
6943 | finish_do_body (NULL_TREE); |
6944 | finish_do_stmt (tsubst_expr (DO_COND (t), args, | |
4393e105 | 6945 | complain, in_decl), |
ad321293 | 6946 | NULL_TREE); |
5566b478 MS |
6947 | } |
6948 | break; | |
a0a33927 | 6949 | |
5566b478 | 6950 | case IF_STMT: |
8d08fdba | 6951 | { |
5566b478 | 6952 | tree tmp; |
5566b478 MS |
6953 | |
6954 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
6955 | begin_if_stmt (); |
6956 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), | |
4393e105 | 6957 | args, complain, in_decl), |
ad321293 | 6958 | NULL_TREE); |
db5ae43f | 6959 | |
ad321293 | 6960 | if (tmp = THEN_CLAUSE (t), tmp) |
db5ae43f | 6961 | { |
4393e105 | 6962 | tsubst_expr (tmp, args, complain, in_decl); |
ad321293 | 6963 | finish_then_clause (NULL_TREE); |
db5ae43f MS |
6964 | } |
6965 | ||
ad321293 MM |
6966 | if (tmp = ELSE_CLAUSE (t), tmp) |
6967 | { | |
6968 | begin_else_clause (); | |
4393e105 | 6969 | tsubst_expr (tmp, args, complain, in_decl); |
ad321293 MM |
6970 | finish_else_clause (NULL_TREE); |
6971 | } | |
8d08fdba | 6972 | |
ad321293 | 6973 | finish_if_stmt (); |
8d08fdba | 6974 | } |
5566b478 | 6975 | break; |
8d08fdba | 6976 | |
5566b478 MS |
6977 | case COMPOUND_STMT: |
6978 | { | |
ad321293 | 6979 | tree substmt; |
8d08fdba | 6980 | |
5566b478 | 6981 | lineno = TREE_COMPLEXITY (t); |
ad321293 MM |
6982 | begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); |
6983 | for (substmt = COMPOUND_BODY (t); | |
6984 | substmt != NULL_TREE; | |
6985 | substmt = TREE_CHAIN (substmt)) | |
4393e105 | 6986 | tsubst_expr (substmt, args, complain, in_decl); |
ad321293 MM |
6987 | return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), |
6988 | NULL_TREE); | |
5566b478 MS |
6989 | } |
6990 | break; | |
8d08fdba | 6991 | |
5566b478 MS |
6992 | case BREAK_STMT: |
6993 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6994 | finish_break_stmt (); |
5566b478 | 6995 | break; |
8d08fdba | 6996 | |
6467930b MS |
6997 | case CONTINUE_STMT: |
6998 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 6999 | finish_continue_stmt (); |
6467930b MS |
7000 | break; |
7001 | ||
5566b478 MS |
7002 | case SWITCH_STMT: |
7003 | { | |
7004 | tree val, tmp; | |
5566b478 MS |
7005 | |
7006 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 7007 | begin_switch_stmt (); |
4393e105 | 7008 | val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); |
ad321293 | 7009 | finish_switch_cond (val); |
5566b478 MS |
7010 | |
7011 | if (tmp = TREE_OPERAND (t, 1), tmp) | |
4393e105 | 7012 | tsubst_expr (tmp, args, complain, in_decl); |
8d08fdba | 7013 | |
ad321293 | 7014 | finish_switch_stmt (val, NULL_TREE); |
5566b478 MS |
7015 | } |
7016 | break; | |
7017 | ||
7018 | case CASE_LABEL: | |
4393e105 MM |
7019 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), |
7020 | tsubst_expr (CASE_HIGH (t), args, complain, in_decl)); | |
5566b478 MS |
7021 | break; |
7022 | ||
7023 | case LABEL_DECL: | |
7024 | t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t), | |
7025 | DECL_NAME (t)); | |
7026 | if (t) | |
7027 | expand_label (t); | |
7028 | break; | |
7029 | ||
7030 | case GOTO_STMT: | |
7031 | lineno = TREE_COMPLEXITY (t); | |
aa09da44 MM |
7032 | t = GOTO_DESTINATION (t); |
7033 | if (TREE_CODE (t) != IDENTIFIER_NODE) | |
7034 | /* Computed goto's must be tsubst'd into. On the other hand, | |
7035 | non-computed gotos must not be; the identifier in question | |
7036 | will have no binding. */ | |
4393e105 | 7037 | t = tsubst_expr (t, args, complain, in_decl); |
aa09da44 | 7038 | finish_goto_stmt (t); |
ad321293 MM |
7039 | break; |
7040 | ||
7041 | case ASM_STMT: | |
7042 | lineno = TREE_COMPLEXITY (t); | |
4393e105 MM |
7043 | finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, complain, in_decl), |
7044 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), | |
7045 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
7046 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
7047 | tsubst_expr (ASM_CLOBBERS (t), args, complain, | |
7048 | in_decl)); | |
5566b478 | 7049 | break; |
faf5394a MS |
7050 | |
7051 | case TRY_BLOCK: | |
7052 | lineno = TREE_COMPLEXITY (t); | |
ad321293 | 7053 | begin_try_block (); |
4393e105 | 7054 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); |
ad321293 | 7055 | finish_try_block (NULL_TREE); |
faf5394a | 7056 | { |
ad321293 | 7057 | tree handler = TRY_HANDLERS (t); |
faf5394a | 7058 | for (; handler; handler = TREE_CHAIN (handler)) |
4393e105 | 7059 | tsubst_expr (handler, args, complain, in_decl); |
faf5394a | 7060 | } |
ad321293 | 7061 | finish_handler_sequence (NULL_TREE); |
faf5394a MS |
7062 | break; |
7063 | ||
7064 | case HANDLER: | |
7065 | lineno = TREE_COMPLEXITY (t); | |
ad321293 MM |
7066 | begin_handler (); |
7067 | if (HANDLER_PARMS (t)) | |
faf5394a | 7068 | { |
ad321293 | 7069 | tree d = HANDLER_PARMS (t); |
faf5394a | 7070 | expand_start_catch_block |
4393e105 MM |
7071 | (tsubst (TREE_OPERAND (d, 1), args, complain, in_decl), |
7072 | tsubst (TREE_OPERAND (d, 0), args, complain, in_decl)); | |
faf5394a MS |
7073 | } |
7074 | else | |
7075 | expand_start_catch_block (NULL_TREE, NULL_TREE); | |
ad321293 | 7076 | finish_handler_parms (NULL_TREE); |
4393e105 | 7077 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); |
ad321293 | 7078 | finish_handler (NULL_TREE); |
faf5394a MS |
7079 | break; |
7080 | ||
b87692e5 MS |
7081 | case TAG_DEFN: |
7082 | lineno = TREE_COMPLEXITY (t); | |
7083 | t = TREE_TYPE (t); | |
7084 | if (TREE_CODE (t) == ENUMERAL_TYPE) | |
4393e105 | 7085 | tsubst (t, args, complain, NULL_TREE); |
b87692e5 MS |
7086 | break; |
7087 | ||
5566b478 | 7088 | default: |
4393e105 | 7089 | return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl)); |
5566b478 MS |
7090 | } |
7091 | return NULL_TREE; | |
8d08fdba MS |
7092 | } |
7093 | ||
6ba89f8e | 7094 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
7095 | the template arguments in TARG_PTR. */ |
7096 | ||
5566b478 MS |
7097 | tree |
7098 | instantiate_template (tmpl, targ_ptr) | |
98c1c668 | 7099 | tree tmpl, targ_ptr; |
8d08fdba | 7100 | { |
5566b478 | 7101 | tree fndecl; |
36a117a5 MM |
7102 | tree gen_tmpl; |
7103 | tree spec; | |
5566b478 MS |
7104 | int i, len; |
7105 | struct obstack *old_fmp_obstack; | |
7106 | extern struct obstack *function_maybepermanent_obstack; | |
36a117a5 | 7107 | tree inner_args; |
5566b478 | 7108 | |
27fafc8d JM |
7109 | if (tmpl == error_mark_node) |
7110 | return error_mark_node; | |
7111 | ||
386b8a85 JM |
7112 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); |
7113 | ||
36a117a5 MM |
7114 | /* Check to see if we already have this specialization. */ |
7115 | spec = retrieve_specialization (tmpl, targ_ptr); | |
7116 | if (spec != NULL_TREE) | |
7117 | return spec; | |
7118 | ||
7119 | if (DECL_TEMPLATE_INFO (tmpl)) | |
386b8a85 | 7120 | { |
36a117a5 MM |
7121 | /* The TMPL is a partial instantiation. To get a full set of |
7122 | arguments we must add the arguments used to perform the | |
7123 | partial instantiation. */ | |
7124 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
7125 | targ_ptr); | |
7126 | gen_tmpl = most_general_template (tmpl); | |
7127 | ||
7128 | /* Check to see if we already have this specialization. */ | |
7129 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
75650646 MM |
7130 | if (spec != NULL_TREE) |
7131 | return spec; | |
386b8a85 | 7132 | } |
36a117a5 MM |
7133 | else |
7134 | gen_tmpl = tmpl; | |
386b8a85 | 7135 | |
5566b478 MS |
7136 | push_obstacks (&permanent_obstack, &permanent_obstack); |
7137 | old_fmp_obstack = function_maybepermanent_obstack; | |
7138 | function_maybepermanent_obstack = &permanent_obstack; | |
8d08fdba | 7139 | |
36a117a5 MM |
7140 | len = DECL_NTPARMS (gen_tmpl); |
7141 | inner_args = innermost_args (targ_ptr); | |
5566b478 MS |
7142 | i = len; |
7143 | while (i--) | |
8d08fdba | 7144 | { |
36a117a5 | 7145 | tree t = TREE_VEC_ELT (inner_args, i); |
5566b478 MS |
7146 | if (TREE_CODE_CLASS (TREE_CODE (t)) == 't') |
7147 | { | |
7148 | tree nt = target_type (t); | |
ec255269 | 7149 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) |
5566b478 | 7150 | { |
8251199e JM |
7151 | cp_error ("type `%T' composed from a local class is not a valid template-argument", t); |
7152 | cp_error (" trying to instantiate `%D'", gen_tmpl); | |
5566b478 MS |
7153 | fndecl = error_mark_node; |
7154 | goto out; | |
7155 | } | |
7156 | } | |
8d08fdba | 7157 | } |
e66d884e | 7158 | targ_ptr = copy_to_permanent (targ_ptr); |
8d08fdba | 7159 | |
5566b478 | 7160 | /* substitute template parameters */ |
4393e105 | 7161 | fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, /*complain=*/1, gen_tmpl); |
36a117a5 MM |
7162 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
7163 | template, not the most general template. */ | |
7164 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 7165 | |
824b9a4c MS |
7166 | if (flag_external_templates) |
7167 | add_pending_template (fndecl); | |
7168 | ||
5566b478 MS |
7169 | out: |
7170 | function_maybepermanent_obstack = old_fmp_obstack; | |
7171 | pop_obstacks (); | |
8d08fdba | 7172 | |
5566b478 | 7173 | return fndecl; |
8d08fdba | 7174 | } |
5566b478 MS |
7175 | |
7176 | /* Push the name of the class template into the scope of the instantiation. */ | |
8d08fdba MS |
7177 | |
7178 | void | |
5566b478 MS |
7179 | overload_template_name (type) |
7180 | tree type; | |
8d08fdba | 7181 | { |
5566b478 MS |
7182 | tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); |
7183 | tree decl; | |
8d08fdba | 7184 | |
5566b478 MS |
7185 | if (IDENTIFIER_CLASS_VALUE (id) |
7186 | && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type) | |
7187 | return; | |
8d08fdba | 7188 | |
5566b478 MS |
7189 | decl = build_decl (TYPE_DECL, id, type); |
7190 | SET_DECL_ARTIFICIAL (decl); | |
7191 | pushdecl_class_level (decl); | |
8d08fdba MS |
7192 | } |
7193 | ||
4393e105 MM |
7194 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
7195 | arguments that are being used when calling it. TARGS is a vector | |
7196 | into which the deduced template arguments are placed. | |
8d08fdba MS |
7197 | |
7198 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
7199 | all the types, and 1 for complete failure. An error message will be | |
7200 | printed only for an incomplete match. | |
7201 | ||
4393e105 MM |
7202 | If FN is a conversion operator, RETURN_TYPE is the type desired as |
7203 | the result of the conversion operator. | |
8d08fdba | 7204 | |
4393e105 | 7205 | TPARMS is a vector of template parameters. |
9f54c803 MM |
7206 | |
7207 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
7208 | template-id. | |
6467930b | 7209 | |
830bfa74 MM |
7210 | The parameter STRICT is one of: |
7211 | ||
7212 | DEDUCE_CALL: | |
7213 | We are deducing arguments for a function call, as in | |
7214 | [temp.deduct.call]. | |
7215 | ||
7216 | DEDUCE_CONV: | |
7217 | We are deducing arguments for a conversion function, as in | |
7218 | [temp.deduct.conv]. | |
7219 | ||
7220 | DEDUCE_EXACT: | |
7221 | We are deducing arguments when calculating the partial | |
7222 | ordering between specializations of function or class | |
7223 | templates, as in [temp.func.order] and [temp.class.order], | |
7224 | when doing an explicit instantiation as in [temp.explicit], | |
9f54c803 | 7225 | when determining an explicit specialization as in |
830bfa74 | 7226 | [temp.expl.spec], or when taking the address of a function |
4393e105 MM |
7227 | template, as in [temp.deduct.funcaddr]. |
7228 | ||
4393e105 | 7229 | The other arguments are as for type_unification. */ |
8d08fdba MS |
7230 | |
7231 | int | |
4393e105 | 7232 | fn_type_unification (fn, explicit_targs, targs, args, return_type, |
03017874 | 7233 | strict) |
4393e105 | 7234 | tree fn, explicit_targs, targs, args, return_type; |
830bfa74 | 7235 | unification_kind_t strict; |
386b8a85 | 7236 | { |
4393e105 MM |
7237 | tree parms; |
7238 | tree fntype; | |
386b8a85 | 7239 | |
4393e105 MM |
7240 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); |
7241 | ||
7242 | fntype = TREE_TYPE (fn); | |
7243 | if (explicit_targs) | |
75650646 | 7244 | { |
4393e105 MM |
7245 | /* [temp.deduct] |
7246 | ||
7247 | The specified template arguments must match the template | |
7248 | parameters in kind (i.e., type, nontype, template), and there | |
7249 | must not be more arguments than there are parameters; | |
7250 | otherwise type deduction fails. | |
7251 | ||
7252 | Nontype arguments must match the types of the corresponding | |
7253 | nontype template parameters, or must be convertible to the | |
7254 | types of the corresponding nontype parameters as specified in | |
7255 | _temp.arg.nontype_, otherwise type deduction fails. | |
7256 | ||
7257 | All references in the function type of the function template | |
7258 | to the corresponding template parameters are replaced by the | |
7259 | specified template argument values. If a substitution in a | |
7260 | template parameter or in the function type of the function | |
7261 | template results in an invalid type, type deduction fails. */ | |
7262 | int i; | |
7263 | tree converted_args; | |
75650646 | 7264 | |
4393e105 MM |
7265 | converted_args |
7266 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
7267 | explicit_targs, NULL_TREE, /*complain=*/0, | |
7268 | /*require_all_arguments=*/0)); | |
7269 | if (converted_args == error_mark_node) | |
75650646 | 7270 | return 1; |
386b8a85 | 7271 | |
4393e105 MM |
7272 | fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE); |
7273 | if (fntype == error_mark_node) | |
7274 | return 1; | |
050367a3 | 7275 | |
4393e105 MM |
7276 | /* Place the explicitly specified arguments in TARGS. */ |
7277 | for (i = 0; i < TREE_VEC_LENGTH (targs); i++) | |
7278 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); | |
75650646 | 7279 | } |
4393e105 MM |
7280 | |
7281 | parms = TYPE_ARG_TYPES (fntype); | |
386b8a85 | 7282 | |
4393e105 MM |
7283 | if (DECL_CONV_FN_P (fn)) |
7284 | { | |
7285 | /* This is a template conversion operator. Use the return types | |
7286 | as well as the argument types. */ | |
7287 | parms = scratch_tree_cons (NULL_TREE, TREE_TYPE (fntype), | |
7288 | parms); | |
7289 | args = scratch_tree_cons (NULL_TREE, return_type, args); | |
7290 | } | |
7291 | ||
4393e105 MM |
7292 | /* We allow incomplete unification without an error message here |
7293 | because the standard doesn't seem to explicitly prohibit it. Our | |
7294 | callers must be ready to deal with unification failures in any | |
7295 | event. */ | |
7296 | return type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
7297 | targs, parms, args, /*subr=*/0, | |
7298 | strict, /*allow_incomplete*/1); | |
830bfa74 MM |
7299 | } |
7300 | ||
7301 | /* Adjust types before performing type deduction, as described in | |
7302 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
7303 | sections are symmetric. PARM is the type of a function parameter |
7304 | or the return type of the conversion function. ARG is the type of | |
7305 | the argument passed to the call, or the type of the value | |
7306 | intialized with the result of the conversion function. */ | |
386b8a85 | 7307 | |
e9659ab0 | 7308 | static void |
830bfa74 MM |
7309 | maybe_adjust_types_for_deduction (strict, parm, arg) |
7310 | unification_kind_t strict; | |
7311 | tree* parm; | |
7312 | tree* arg; | |
7313 | { | |
7314 | switch (strict) | |
7315 | { | |
7316 | case DEDUCE_CALL: | |
7317 | break; | |
7318 | ||
7319 | case DEDUCE_CONV: | |
7320 | { | |
4c7d0dff MM |
7321 | /* Swap PARM and ARG throughout the remainder of this |
7322 | function; the handling is precisely symmetric since PARM | |
7323 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
7324 | tree* temp = parm; |
7325 | parm = arg; | |
7326 | arg = temp; | |
7327 | break; | |
7328 | } | |
7329 | ||
7330 | case DEDUCE_EXACT: | |
7331 | /* There is nothing to do in this case. */ | |
7332 | return; | |
7333 | ||
7334 | default: | |
7335 | my_friendly_abort (0); | |
7336 | } | |
7337 | ||
7338 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
7339 | { | |
7340 | /* [temp.deduct.call] | |
7341 | ||
7342 | If P is not a reference type: | |
7343 | ||
7344 | --If A is an array type, the pointer type produced by the | |
7345 | array-to-pointer standard conversion (_conv.array_) is | |
7346 | used in place of A for type deduction; otherwise, | |
7347 | ||
7348 | --If A is a function type, the pointer type produced by | |
7349 | the function-to-pointer standard conversion | |
7350 | (_conv.func_) is used in place of A for type deduction; | |
7351 | otherwise, | |
7352 | ||
7353 | --If A is a cv-qualified type, the top level | |
7354 | cv-qualifiers of A's type are ignored for type | |
7355 | deduction. */ | |
7356 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
7357 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 7358 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
7359 | *arg = build_pointer_type (*arg); |
7360 | else | |
7361 | *arg = TYPE_MAIN_VARIANT (*arg); | |
7362 | } | |
7363 | ||
7364 | /* [temp.deduct.call] | |
7365 | ||
7366 | If P is a cv-qualified type, the top level cv-qualifiers | |
7367 | of P's type are ignored for type deduction. If P is a | |
7368 | reference type, the type referred to by P is used for | |
7369 | type deduction. */ | |
7370 | *parm = TYPE_MAIN_VARIANT (*parm); | |
7371 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
7372 | *parm = TREE_TYPE (*parm); | |
386b8a85 JM |
7373 | } |
7374 | ||
4393e105 | 7375 | /* Like type_unfication. |
9f54c803 MM |
7376 | |
7377 | If SUBR is 1, we're being called recursively (to unify the | |
7378 | arguments of a function or method parameter of a function | |
7379 | template). */ | |
386b8a85 | 7380 | |
4966381a | 7381 | static int |
3b3ba9f0 | 7382 | type_unification_real (tparms, targs, parms, args, subr, |
4393e105 | 7383 | strict, allow_incomplete) |
050367a3 | 7384 | tree tparms, targs, parms, args; |
830bfa74 MM |
7385 | int subr; |
7386 | unification_kind_t strict; | |
7387 | int allow_incomplete; | |
8d08fdba MS |
7388 | { |
7389 | tree parm, arg; | |
7390 | int i; | |
7391 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 7392 | int sub_strict; |
8d08fdba MS |
7393 | |
7394 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
386b8a85 JM |
7395 | my_friendly_assert (parms == NULL_TREE |
7396 | || TREE_CODE (parms) == TREE_LIST, 290); | |
51c184be | 7397 | /* ARGS could be NULL (via a call from parse.y to |
8d08fdba MS |
7398 | build_x_function_call). */ |
7399 | if (args) | |
7400 | my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291); | |
7401 | my_friendly_assert (ntparms > 0, 292); | |
7402 | ||
830bfa74 MM |
7403 | switch (strict) |
7404 | { | |
7405 | case DEDUCE_CALL: | |
7406 | sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED; | |
7407 | break; | |
7408 | ||
7409 | case DEDUCE_CONV: | |
7410 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
7411 | break; | |
7412 | ||
7413 | case DEDUCE_EXACT: | |
7414 | sub_strict = UNIFY_ALLOW_NONE; | |
7415 | break; | |
7416 | ||
7417 | default: | |
7418 | my_friendly_abort (0); | |
7419 | } | |
7420 | ||
8d08fdba MS |
7421 | while (parms |
7422 | && parms != void_list_node | |
7423 | && args | |
7424 | && args != void_list_node) | |
7425 | { | |
7426 | parm = TREE_VALUE (parms); | |
7427 | parms = TREE_CHAIN (parms); | |
7428 | arg = TREE_VALUE (args); | |
7429 | args = TREE_CHAIN (args); | |
7430 | ||
7431 | if (arg == error_mark_node) | |
7432 | return 1; | |
7433 | if (arg == unknown_type_node) | |
34016c81 JM |
7434 | /* We can't deduce anything from this, but we might get all the |
7435 | template args from other function args. */ | |
7436 | continue; | |
b7484fbe | 7437 | |
03e70705 JM |
7438 | /* Conversions will be performed on a function argument that |
7439 | corresponds with a function parameter that contains only | |
7440 | non-deducible template parameters and explicitly specified | |
7441 | template parameters. */ | |
7442 | if (! uses_template_parms (parm)) | |
b7484fbe | 7443 | { |
03e70705 JM |
7444 | tree type; |
7445 | ||
7446 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') | |
7447 | type = TREE_TYPE (arg); | |
7448 | else | |
7449 | { | |
7450 | type = arg; | |
7451 | arg = NULL_TREE; | |
7452 | } | |
7453 | ||
830bfa74 | 7454 | if (strict == DEDUCE_EXACT) |
03e70705 | 7455 | { |
3bfdc719 | 7456 | if (same_type_p (parm, type)) |
03e70705 JM |
7457 | continue; |
7458 | } | |
03e70705 | 7459 | else |
343c89cd JM |
7460 | /* It might work; we shouldn't check now, because we might |
7461 | get into infinite recursion. Overload resolution will | |
7462 | handle it. */ | |
7463 | continue; | |
03e70705 | 7464 | |
b7484fbe MS |
7465 | return 1; |
7466 | } | |
7467 | ||
8d08fdba MS |
7468 | if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't') |
7469 | { | |
7470 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
34016c81 | 7471 | if (type_unknown_p (arg)) |
28cbf42c | 7472 | { |
34016c81 JM |
7473 | /* [temp.deduct.type] A template-argument can be deduced from |
7474 | a pointer to function or pointer to member function | |
7475 | argument if the set of overloaded functions does not | |
7476 | contain function templates and at most one of a set of | |
7477 | overloaded functions provides a unique match. */ | |
7478 | ||
7479 | if (resolve_overloaded_unification | |
4393e105 | 7480 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
7481 | != 0) |
7482 | return 1; | |
7483 | continue; | |
28cbf42c | 7484 | } |
8d08fdba MS |
7485 | arg = TREE_TYPE (arg); |
7486 | } | |
4393e105 | 7487 | |
830bfa74 MM |
7488 | if (!subr) |
7489 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
db5ae43f | 7490 | |
4393e105 | 7491 | switch (unify (tparms, targs, parm, arg, sub_strict)) |
8d08fdba MS |
7492 | { |
7493 | case 0: | |
7494 | break; | |
7495 | case 1: | |
7496 | return 1; | |
7497 | } | |
7498 | } | |
7499 | /* Fail if we've reached the end of the parm list, and more args | |
7500 | are present, and the parm list isn't variadic. */ | |
7501 | if (args && args != void_list_node && parms == void_list_node) | |
7502 | return 1; | |
7503 | /* Fail if parms are left and they don't have default values. */ | |
7504 | if (parms | |
7505 | && parms != void_list_node | |
7506 | && TREE_PURPOSE (parms) == NULL_TREE) | |
7507 | return 1; | |
7508 | if (!subr) | |
7509 | for (i = 0; i < ntparms; i++) | |
050367a3 | 7510 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
8d08fdba | 7511 | { |
386b8a85 | 7512 | if (!allow_incomplete) |
8251199e | 7513 | error ("incomplete type unification"); |
8d08fdba MS |
7514 | return 2; |
7515 | } | |
7516 | return 0; | |
7517 | } | |
7518 | ||
34016c81 JM |
7519 | /* Subroutine of type_unification_real. Args are like the variables at the |
7520 | call site. ARG is an overloaded function (or template-id); we try | |
7521 | deducing template args from each of the overloads, and if only one | |
7522 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
7523 | ||
7524 | static int | |
7525 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
4393e105 | 7526 | sub_strict) |
34016c81 JM |
7527 | tree tparms, targs, parm, arg; |
7528 | unification_kind_t strict; | |
7529 | int sub_strict; | |
34016c81 JM |
7530 | { |
7531 | tree tempargs = copy_node (targs); | |
7532 | int good = 0; | |
7533 | ||
7534 | if (TREE_CODE (arg) == ADDR_EXPR) | |
7535 | arg = TREE_OPERAND (arg, 0); | |
9f3d9e46 | 7536 | |
d8f8dca1 MM |
7537 | if (TREE_CODE (arg) == COMPONENT_REF) |
7538 | /* Handle `&x' where `x' is some static or non-static member | |
7539 | function name. */ | |
7540 | arg = TREE_OPERAND (arg, 1); | |
7541 | ||
9f3d9e46 JM |
7542 | /* Strip baselink information. */ |
7543 | while (TREE_CODE (arg) == TREE_LIST) | |
7544 | arg = TREE_VALUE (arg); | |
7545 | ||
34016c81 JM |
7546 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
7547 | { | |
7548 | /* If we got some explicit template args, we need to plug them into | |
7549 | the affected templates before we try to unify, in case the | |
7550 | explicit args will completely resolve the templates in question. */ | |
7551 | ||
7552 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
7553 | arg = TREE_OPERAND (arg, 0); | |
7554 | ||
7555 | for (; arg; arg = OVL_NEXT (arg)) | |
7556 | { | |
7557 | tree fn = OVL_CURRENT (arg); | |
7558 | tree subargs, elem; | |
7559 | ||
7560 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
7561 | continue; | |
7562 | ||
7563 | subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs); | |
7564 | if (subargs) | |
7565 | { | |
4393e105 MM |
7566 | elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0, |
7567 | NULL_TREE); | |
d8f8dca1 MM |
7568 | if (TREE_CODE (elem) == METHOD_TYPE) |
7569 | elem = build_ptrmemfunc_type (build_pointer_type (elem)); | |
e97e5263 | 7570 | good += try_one_overload (tparms, targs, tempargs, parm, elem, |
4393e105 | 7571 | strict, sub_strict); |
34016c81 JM |
7572 | } |
7573 | } | |
7574 | } | |
7575 | else if (TREE_CODE (arg) == OVERLOAD) | |
7576 | { | |
7577 | for (; arg; arg = OVL_NEXT (arg)) | |
d8f8dca1 MM |
7578 | { |
7579 | tree type = TREE_TYPE (OVL_CURRENT (arg)); | |
7580 | if (TREE_CODE (type) == METHOD_TYPE) | |
7581 | type = build_ptrmemfunc_type (build_pointer_type (type)); | |
7582 | good += try_one_overload (tparms, targs, tempargs, parm, | |
7583 | type, | |
4393e105 | 7584 | strict, sub_strict); |
d8f8dca1 | 7585 | } |
34016c81 JM |
7586 | } |
7587 | else | |
7588 | my_friendly_abort (981006); | |
7589 | ||
7590 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7591 | to function or pointer to member function argument if the set of | |
7592 | overloaded functions does not contain function templates and at most | |
7593 | one of a set of overloaded functions provides a unique match. | |
7594 | ||
7595 | So if we found multiple possibilities, we return success but don't | |
7596 | deduce anything. */ | |
7597 | ||
7598 | if (good == 1) | |
7599 | { | |
7600 | int i = TREE_VEC_LENGTH (targs); | |
7601 | for (; i--; ) | |
7602 | if (TREE_VEC_ELT (tempargs, i)) | |
7603 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
7604 | } | |
7605 | if (good) | |
7606 | return 0; | |
7607 | ||
7608 | return 1; | |
7609 | } | |
7610 | ||
7611 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
7612 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
7613 | different overloads deduce different arguments for a given parm. | |
7614 | Returns 1 on success. */ | |
7615 | ||
7616 | static int | |
e97e5263 | 7617 | try_one_overload (tparms, orig_targs, targs, parm, arg, strict, |
4393e105 | 7618 | sub_strict) |
e97e5263 | 7619 | tree tparms, orig_targs, targs, parm, arg; |
34016c81 JM |
7620 | unification_kind_t strict; |
7621 | int sub_strict; | |
34016c81 JM |
7622 | { |
7623 | int nargs; | |
7624 | tree tempargs; | |
7625 | int i; | |
7626 | ||
7627 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
7628 | to function or pointer to member function argument if the set of | |
7629 | overloaded functions does not contain function templates and at most | |
7630 | one of a set of overloaded functions provides a unique match. | |
7631 | ||
7632 | So if this is a template, just return success. */ | |
7633 | ||
7634 | if (uses_template_parms (arg)) | |
7635 | return 1; | |
7636 | ||
7637 | maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
7638 | ||
7639 | /* We don't copy orig_targs for this because if we have already deduced | |
7640 | some template args from previous args, unify would complain when we | |
7641 | try to deduce a template parameter for the same argument, even though | |
7642 | there isn't really a conflict. */ | |
7643 | nargs = TREE_VEC_LENGTH (targs); | |
7644 | tempargs = make_scratch_vec (nargs); | |
7645 | ||
4393e105 | 7646 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
7647 | return 0; |
7648 | ||
7649 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 7650 | explicitly specified args. */ |
34016c81 JM |
7651 | for (i = nargs; i--; ) |
7652 | { | |
7653 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 7654 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 JM |
7655 | |
7656 | if (elt == NULL_TREE) | |
7657 | continue; | |
7658 | else if (uses_template_parms (elt)) | |
7659 | { | |
7660 | /* Since we're unifying against ourselves, we will fill in template | |
7661 | args used in the function parm list with our own template parms. | |
7662 | Discard them. */ | |
7663 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
7664 | continue; | |
7665 | } | |
7666 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
7667 | return 0; | |
7668 | } | |
7669 | ||
7670 | for (i = nargs; i--; ) | |
7671 | { | |
7672 | tree elt = TREE_VEC_ELT (tempargs, i); | |
7673 | ||
7674 | if (elt) | |
7675 | TREE_VEC_ELT (targs, i) = elt; | |
7676 | } | |
7677 | ||
7678 | return 1; | |
7679 | } | |
7680 | ||
4393e105 MM |
7681 | /* PARM is a template class (perhaps with unbound template |
7682 | parameters). ARG is a fully instantiated type. If ARG can be | |
7683 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
7684 | TARGS are as for unify. */ | |
fcfb9f96 MM |
7685 | |
7686 | static tree | |
4393e105 | 7687 | try_class_unification (tparms, targs, parm, arg) |
fcfb9f96 MM |
7688 | tree tparms; |
7689 | tree targs; | |
4393e105 MM |
7690 | tree parm; |
7691 | tree arg; | |
7692 | { | |
7693 | int i; | |
7694 | tree copy_of_targs; | |
7695 | ||
7696 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
7697 | || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) | |
7698 | return NULL_TREE; | |
7699 | ||
7700 | /* We need to make a new template argument vector for the call to | |
7701 | unify. If we used TARGS, we'd clutter it up with the result of | |
7702 | the attempted unification, even if this class didn't work out. | |
7703 | We also don't want to commit ourselves to all the unifications | |
7704 | we've already done, since unification is supposed to be done on | |
7705 | an argument-by-argument basis. In other words, consider the | |
7706 | following pathological case: | |
7707 | ||
7708 | template <int I, int J, int K> | |
7709 | struct S {}; | |
7710 | ||
7711 | template <int I, int J> | |
7712 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
7713 | ||
7714 | template <int I, int J, int K> | |
7715 | void f(S<I, J, K>, S<I, I, I>); | |
7716 | ||
7717 | void g() { | |
7718 | S<0, 0, 0> s0; | |
7719 | S<0, 1, 2> s2; | |
7720 | ||
7721 | f(s0, s2); | |
7722 | } | |
7723 | ||
7724 | Now, by the time we consider the unification involving `s2', we | |
7725 | already know that we must have `f<0, 0, 0>'. But, even though | |
7726 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal | |
7727 | because there are two ways to unify base classes of S<0, 1, 2> | |
7728 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
7729 | would reject the possibility I=1. */ | |
7730 | push_momentary (); | |
7731 | copy_of_targs = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
7732 | i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), | |
7733 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE); | |
7734 | pop_momentary (); | |
7735 | ||
7736 | /* If unification failed, we're done. */ | |
7737 | if (i != 0) | |
7738 | return NULL_TREE; | |
7739 | else | |
7740 | return arg; | |
7741 | } | |
7742 | ||
7743 | /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we | |
7744 | have alreay discovered to be satisfactory. ARG_BINFO is the binfo | |
7745 | for the base class of ARG that we are currently examining. */ | |
7746 | ||
7747 | static tree | |
7748 | get_template_base_recursive (tparms, targs, parm, | |
7749 | arg_binfo, rval, flags) | |
7750 | tree tparms; | |
7751 | tree targs; | |
7752 | tree arg_binfo; | |
fcfb9f96 | 7753 | tree rval; |
4393e105 MM |
7754 | tree parm; |
7755 | int flags; | |
fcfb9f96 MM |
7756 | { |
7757 | tree binfos; | |
7758 | int i, n_baselinks; | |
4393e105 | 7759 | tree arg = BINFO_TYPE (arg_binfo); |
fcfb9f96 | 7760 | |
4393e105 | 7761 | if (!(flags & GTB_IGNORE_TYPE)) |
fcfb9f96 | 7762 | { |
4393e105 MM |
7763 | tree r = try_class_unification (tparms, targs, |
7764 | parm, arg); | |
fcfb9f96 | 7765 | |
4393e105 | 7766 | /* If there is more than one satisfactory baseclass, then: |
fcfb9f96 | 7767 | |
4393e105 MM |
7768 | [temp.deduct.call] |
7769 | ||
7770 | If they yield more than one possible deduced A, the type | |
7771 | deduction fails. | |
7772 | ||
7773 | applies. */ | |
7774 | if (r && rval && !same_type_p (r, rval)) | |
7775 | return error_mark_node; | |
7776 | else if (r) | |
7777 | rval = r; | |
fcfb9f96 MM |
7778 | } |
7779 | ||
4393e105 | 7780 | binfos = BINFO_BASETYPES (arg_binfo); |
fcfb9f96 MM |
7781 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; |
7782 | ||
7783 | /* Process base types. */ | |
7784 | for (i = 0; i < n_baselinks; i++) | |
7785 | { | |
7786 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
4393e105 | 7787 | int this_virtual; |
fcfb9f96 | 7788 | |
4393e105 MM |
7789 | /* Skip this base, if we've already seen it. */ |
7790 | if (BINFO_MARKED (base_binfo)) | |
7791 | continue; | |
7792 | ||
7793 | this_virtual = | |
7794 | (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); | |
7795 | ||
7796 | /* When searching for a non-virtual, we cannot mark virtually | |
7797 | found binfos. */ | |
7798 | if (! this_virtual) | |
7799 | SET_BINFO_MARKED (base_binfo); | |
7800 | ||
7801 | rval = get_template_base_recursive (tparms, targs, | |
7802 | parm, | |
7803 | base_binfo, | |
7804 | rval, | |
7805 | GTB_VIA_VIRTUAL * this_virtual); | |
7806 | ||
7807 | /* If we discovered more than one matching base class, we can | |
7808 | stop now. */ | |
7809 | if (rval == error_mark_node) | |
7810 | return error_mark_node; | |
fcfb9f96 MM |
7811 | } |
7812 | ||
7813 | return rval; | |
7814 | } | |
7815 | ||
4393e105 MM |
7816 | /* Given a template type PARM and a class type ARG, find the unique |
7817 | base type in ARG that is an instance of PARM. We do not examine | |
7818 | ARG itself; only its base-classes. If there is no appropriate base | |
7819 | class, return NULL_TREE. If there is more than one, return | |
7820 | error_mark_node. PARM may be the type of a partial specialization, | |
7821 | as well as a plain template type. Used by unify. */ | |
fcfb9f96 MM |
7822 | |
7823 | static tree | |
4393e105 | 7824 | get_template_base (tparms, targs, parm, arg) |
fcfb9f96 MM |
7825 | tree tparms; |
7826 | tree targs; | |
4393e105 MM |
7827 | tree parm; |
7828 | tree arg; | |
fcfb9f96 | 7829 | { |
4393e105 MM |
7830 | tree rval; |
7831 | tree arg_binfo; | |
fcfb9f96 | 7832 | |
4393e105 MM |
7833 | my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); |
7834 | ||
7835 | arg_binfo = TYPE_BINFO (complete_type (arg)); | |
7836 | rval = get_template_base_recursive (tparms, targs, | |
7837 | parm, arg_binfo, | |
7838 | NULL_TREE, | |
7839 | GTB_IGNORE_TYPE); | |
fcfb9f96 | 7840 | |
4393e105 MM |
7841 | /* Since get_template_base_recursive marks the bases classes, we |
7842 | must unmark them here. */ | |
7843 | dfs_walk (arg_binfo, dfs_unmark, markedp); | |
fcfb9f96 MM |
7844 | |
7845 | return rval; | |
7846 | } | |
7847 | ||
db2767b6 MM |
7848 | /* Returns the level of DECL, which declares a template parameter. */ |
7849 | ||
e9659ab0 | 7850 | static int |
db2767b6 MM |
7851 | template_decl_level (decl) |
7852 | tree decl; | |
7853 | { | |
7854 | switch (TREE_CODE (decl)) | |
7855 | { | |
7856 | case TYPE_DECL: | |
7857 | case TEMPLATE_DECL: | |
7858 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
7859 | ||
7860 | case PARM_DECL: | |
7861 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
7862 | ||
7863 | default: | |
7864 | my_friendly_abort (0); | |
5ffe581d | 7865 | return 0; |
db2767b6 MM |
7866 | } |
7867 | } | |
7868 | ||
830bfa74 MM |
7869 | /* Decide whether ARG can be unified with PARM, considering only the |
7870 | cv-qualifiers of each type, given STRICT as documented for unify. | |
7871 | Returns non-zero iff the unification is OK on that basis.*/ | |
e92cc029 | 7872 | |
e9659ab0 | 7873 | static int |
830bfa74 MM |
7874 | check_cv_quals_for_unify (strict, arg, parm) |
7875 | int strict; | |
7876 | tree arg; | |
7877 | tree parm; | |
7878 | { | |
7879 | return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL) | |
91063b51 | 7880 | && !at_least_as_qualified_p (arg, parm)) |
830bfa74 | 7881 | || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL) |
91063b51 | 7882 | && (!at_least_as_qualified_p (parm, arg)))); |
830bfa74 MM |
7883 | } |
7884 | ||
7885 | /* Takes parameters as for type_unification. Returns 0 if the | |
7886 | type deduction suceeds, 1 otherwise. The parameter STRICT is a | |
7887 | bitwise or of the following flags: | |
7888 | ||
7889 | UNIFY_ALLOW_NONE: | |
7890 | Require an exact match between PARM and ARG. | |
7891 | UNIFY_ALLOW_MORE_CV_QUAL: | |
7892 | Allow the deduced ARG to be more cv-qualified than ARG. | |
7893 | UNIFY_ALLOW_LESS_CV_QUAL: | |
7894 | Allow the deduced ARG to be less cv-qualified than ARG. | |
7895 | UNIFY_ALLOW_DERIVED: | |
7896 | Allow the deduced ARG to be a template base class of ARG, | |
7897 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
7898 | ARG. |
7899 | UNIFY_ALLOW_INTEGER: | |
7900 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
7901 | case for more information. */ | |
830bfa74 | 7902 | |
e9659ab0 | 7903 | static int |
4393e105 | 7904 | unify (tparms, targs, parm, arg, strict) |
050367a3 | 7905 | tree tparms, targs, parm, arg; |
830bfa74 | 7906 | int strict; |
8d08fdba MS |
7907 | { |
7908 | int idx; | |
050367a3 | 7909 | tree targ; |
db2767b6 | 7910 | tree tparm; |
8d08fdba MS |
7911 | |
7912 | /* I don't think this will do the right thing with respect to types. | |
7913 | But the only case I've seen it in so far has been array bounds, where | |
7914 | signedness is the only information lost, and I think that will be | |
7915 | okay. */ | |
7916 | while (TREE_CODE (parm) == NOP_EXPR) | |
7917 | parm = TREE_OPERAND (parm, 0); | |
7918 | ||
7919 | if (arg == error_mark_node) | |
7920 | return 1; | |
7921 | if (arg == unknown_type_node) | |
34016c81 JM |
7922 | /* We can't deduce anything from this, but we might get all the |
7923 | template args from other function args. */ | |
7924 | return 0; | |
7925 | ||
db2767b6 | 7926 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 7927 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
7928 | template parameters. We might need them if we're trying to |
7929 | figure out which of two things is more specialized. */ | |
7930 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
7931 | return 0; |
7932 | ||
830bfa74 MM |
7933 | /* Immediately reject some pairs that won't unify because of |
7934 | cv-qualification mismatches. */ | |
7935 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
49432171 | 7936 | && TREE_CODE_CLASS (TREE_CODE (arg)) == 't' |
830bfa74 MM |
7937 | /* We check the cv-qualifiers when unifying with template type |
7938 | parameters below. We want to allow ARG `const T' to unify with | |
7939 | PARM `T' for example, when computing which of two templates | |
7940 | is more specialized, for example. */ | |
7941 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
7942 | && !check_cv_quals_for_unify (strict, arg, parm)) | |
49432171 JM |
7943 | return 1; |
7944 | ||
8d08fdba MS |
7945 | switch (TREE_CODE (parm)) |
7946 | { | |
2ca340ae JM |
7947 | case TYPENAME_TYPE: |
7948 | /* In a type which contains a nested-name-specifier, template | |
7949 | argument values cannot be deduced for template parameters used | |
7950 | within the nested-name-specifier. */ | |
7951 | return 0; | |
7952 | ||
8d08fdba | 7953 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 7954 | case TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
7955 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
7956 | ||
7957 | if (TEMPLATE_TYPE_LEVEL (parm) | |
7958 | != template_decl_level (tparm)) | |
7959 | /* The PARM is not one we're trying to unify. Just check | |
7960 | to see if it matches ARG. */ | |
7961 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 7962 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 7963 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 7964 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 7965 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 7966 | |
73b0fce8 | 7967 | /* Check for mixed types and values. */ |
db2767b6 MM |
7968 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
7969 | && TREE_CODE (tparm) != TYPE_DECL) | |
7970 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
7971 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
7972 | return 1; |
7973 | ||
db2767b6 | 7974 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 7975 | { |
7ddedda4 | 7976 | if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)) |
db2767b6 MM |
7977 | { |
7978 | /* We arrive here when PARM does not involve template | |
7979 | specialization. */ | |
73b0fce8 | 7980 | |
db2767b6 MM |
7981 | /* ARG must be constructed from a template class. */ |
7982 | if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)) | |
7983 | return 1; | |
73b0fce8 | 7984 | |
db2767b6 | 7985 | { |
7ddedda4 MM |
7986 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); |
7987 | tree parmvec = TYPE_TI_ARGS (parm); | |
db2767b6 MM |
7988 | tree argvec = CLASSTYPE_TI_ARGS (arg); |
7989 | tree argtmplvec | |
7990 | = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg)); | |
7991 | int i; | |
7992 | ||
7993 | /* The parameter and argument roles have to be switched here | |
7994 | in order to handle default arguments properly. For example, | |
7995 | template<template <class> class TT> void f(TT<int>) | |
7996 | should be able to accept vector<int> which comes from | |
744fac59 | 7997 | template <class T, class Allocator = allocator> |
73b0fce8 KL |
7998 | class vector. */ |
7999 | ||
fcfcdfc8 | 8000 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) |
db2767b6 MM |
8001 | == error_mark_node) |
8002 | return 1; | |
73b0fce8 | 8003 | |
6b9b6b15 JM |
8004 | /* Deduce arguments T, i from TT<T> or TT<i>. |
8005 | We check each element of PARMVEC and ARGVEC individually | |
8006 | rather than the whole TREE_VEC since they can have | |
8007 | different number of elements. */ | |
8008 | ||
db2767b6 MM |
8009 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
8010 | { | |
8011 | tree t = TREE_VEC_ELT (parmvec, i); | |
73b0fce8 | 8012 | |
830bfa74 MM |
8013 | if (unify (tparms, targs, t, |
8014 | TREE_VEC_ELT (argvec, i), | |
4393e105 | 8015 | UNIFY_ALLOW_NONE)) |
db2767b6 MM |
8016 | return 1; |
8017 | } | |
73b0fce8 | 8018 | } |
db2767b6 MM |
8019 | arg = CLASSTYPE_TI_TEMPLATE (arg); |
8020 | } | |
8021 | } | |
8022 | else | |
8023 | { | |
830bfa74 MM |
8024 | /* If PARM is `const T' and ARG is only `int', we don't have |
8025 | a match unless we are allowing additional qualification. | |
8026 | If ARG is `const int' and PARM is just `T' that's OK; | |
8027 | that binds `const int' to `T'. */ | |
8028 | if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL, | |
8029 | arg, parm)) | |
db2767b6 MM |
8030 | return 1; |
8031 | ||
830bfa74 MM |
8032 | /* Consider the case where ARG is `const volatile int' and |
8033 | PARM is `const T'. Then, T should be `volatile int'. */ | |
8034 | arg = | |
91063b51 MM |
8035 | cp_build_qualified_type (arg, |
8036 | CP_TYPE_QUALS (arg) | |
8037 | & ~CP_TYPE_QUALS (parm)); | |
73b0fce8 KL |
8038 | } |
8039 | ||
8040 | /* Simple cases: Value already set, does match or doesn't. */ | |
4393e105 | 8041 | if (targ != NULL_TREE && same_type_p (targ, arg)) |
73b0fce8 | 8042 | return 0; |
050367a3 | 8043 | else if (targ) |
73b0fce8 | 8044 | return 1; |
61cd552e MM |
8045 | |
8046 | /* Make sure that ARG is not a variable-sized array. (Note that | |
8047 | were talking about variable-sized arrays (like `int[n]'), | |
8048 | rather than arrays of unknown size (like `int[]').) We'll | |
8049 | get very confused by such a type since the bound of the array | |
8050 | will not be computable in an instantiation. Besides, such | |
8051 | types are not allowed in ISO C++, so we can do as we please | |
8052 | here. */ | |
8053 | if (TREE_CODE (arg) == ARRAY_TYPE | |
8054 | && !uses_template_parms (arg) | |
8055 | && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg))) | |
8056 | != INTEGER_CST)) | |
8057 | return 1; | |
8058 | ||
050367a3 | 8059 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
8060 | return 0; |
8061 | ||
f84b4be9 | 8062 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
8063 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
8064 | ||
8065 | if (TEMPLATE_PARM_LEVEL (parm) | |
8066 | != template_decl_level (tparm)) | |
8067 | /* The PARM is not one we're trying to unify. Just check | |
8068 | to see if it matches ARG. */ | |
8069 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
67ffc812 | 8070 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; |
db2767b6 | 8071 | |
f84b4be9 | 8072 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 8073 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 8074 | |
050367a3 | 8075 | if (targ) |
8d08fdba | 8076 | { |
67ffc812 | 8077 | int i = (cp_tree_equal (targ, arg) > 0); |
312e7d50 JM |
8078 | if (i == 1) |
8079 | return 0; | |
8080 | else if (i == 0) | |
8081 | return 1; | |
8082 | else | |
8083 | my_friendly_abort (42); | |
8d08fdba | 8084 | } |
8d08fdba | 8085 | |
161c12b0 JM |
8086 | /* [temp.deduct.type] If, in the declaration of a function template |
8087 | with a non-type template-parameter, the non-type | |
8088 | template-parameter is used in an expression in the function | |
8089 | parameter-list and, if the corresponding template-argument is | |
8090 | deduced, the template-argument type shall match the type of the | |
8091 | template-parameter exactly, except that a template-argument | |
8092 | deduced from an array bound may be of any integral type. */ | |
8093 | if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm))) | |
8094 | /* OK */; | |
8095 | else if ((strict & UNIFY_ALLOW_INTEGER) | |
8096 | && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE | |
8097 | || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE)) | |
8098 | /* OK */; | |
8099 | else | |
8100 | return 1; | |
8101 | ||
050367a3 | 8102 | TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg); |
8d08fdba MS |
8103 | return 0; |
8104 | ||
8105 | case POINTER_TYPE: | |
830bfa74 MM |
8106 | { |
8107 | int sub_strict; | |
4ac14744 | 8108 | |
830bfa74 MM |
8109 | if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg)) |
8110 | return (unify (tparms, targs, parm, | |
4393e105 | 8111 | TYPE_PTRMEMFUNC_FN_TYPE (arg), strict)); |
830bfa74 MM |
8112 | |
8113 | if (TREE_CODE (arg) != POINTER_TYPE) | |
8114 | return 1; | |
8115 | ||
8116 | /* [temp.deduct.call] | |
8117 | ||
8118 | A can be another pointer or pointer to member type that can | |
8119 | be converted to the deduced A via a qualification | |
8120 | conversion (_conv.qual_). | |
8121 | ||
8122 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
8123 | This will allow for additional cv-qualification of the | |
8124 | pointed-to types if appropriate. In general, this is a bit | |
8125 | too generous; we are only supposed to allow qualification | |
8126 | conversions and this method will allow an ARG of char** and | |
8127 | a deduced ARG of const char**. However, overload | |
8128 | resolution will subsequently invalidate the candidate, so | |
8129 | this is probably OK. */ | |
8130 | sub_strict = strict; | |
8131 | ||
8132 | if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE | |
8133 | || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg))) | |
8134 | /* The derived-to-base conversion only persists through one | |
8135 | level of pointers. */ | |
8136 | sub_strict &= ~UNIFY_ALLOW_DERIVED; | |
8137 | ||
8138 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE | |
4393e105 | 8139 | (arg), sub_strict); |
830bfa74 | 8140 | } |
8d08fdba MS |
8141 | |
8142 | case REFERENCE_TYPE: | |
830bfa74 MM |
8143 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
8144 | return 1; | |
8145 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
4393e105 | 8146 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8147 | |
8148 | case ARRAY_TYPE: | |
8149 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
8150 | return 1; | |
3042d5be MM |
8151 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
8152 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
8153 | return 1; | |
8154 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
830bfa74 | 8155 | && unify (tparms, targs, TYPE_DOMAIN (parm), |
4393e105 | 8156 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) |
8d08fdba | 8157 | return 1; |
830bfa74 | 8158 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8159 | UNIFY_ALLOW_NONE); |
8d08fdba MS |
8160 | |
8161 | case REAL_TYPE: | |
37c46b43 | 8162 | case COMPLEX_TYPE: |
8d08fdba | 8163 | case INTEGER_TYPE: |
42976354 | 8164 | case BOOLEAN_TYPE: |
5ad5a526 | 8165 | case VOID_TYPE: |
f376e137 MS |
8166 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8167 | return 1; | |
8168 | ||
514a1f18 JM |
8169 | if (TREE_CODE (parm) == INTEGER_TYPE |
8170 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
8d08fdba MS |
8171 | { |
8172 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
830bfa74 | 8173 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), |
4393e105 | 8174 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8175 | return 1; |
8176 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
830bfa74 | 8177 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), |
4393e105 | 8178 | TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER)) |
8d08fdba MS |
8179 | return 1; |
8180 | } | |
514a1f18 JM |
8181 | /* We use the TYPE_MAIN_VARIANT since we have already |
8182 | checked cv-qualification at the top of the | |
8183 | function. */ | |
3bfdc719 MM |
8184 | else if (!same_type_p (TYPE_MAIN_VARIANT (arg), |
8185 | TYPE_MAIN_VARIANT (parm))) | |
ca79f85d JM |
8186 | return 1; |
8187 | ||
8d08fdba MS |
8188 | /* As far as unification is concerned, this wins. Later checks |
8189 | will invalidate it if necessary. */ | |
8190 | return 0; | |
8191 | ||
8192 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 8193 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 8194 | case INTEGER_CST: |
bd6dd845 MS |
8195 | while (TREE_CODE (arg) == NOP_EXPR) |
8196 | arg = TREE_OPERAND (arg, 0); | |
8197 | ||
8d08fdba MS |
8198 | if (TREE_CODE (arg) != INTEGER_CST) |
8199 | return 1; | |
8200 | return !tree_int_cst_equal (parm, arg); | |
8201 | ||
8d08fdba MS |
8202 | case TREE_VEC: |
8203 | { | |
8204 | int i; | |
8205 | if (TREE_CODE (arg) != TREE_VEC) | |
8206 | return 1; | |
8207 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
8208 | return 1; | |
8209 | for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--) | |
830bfa74 | 8210 | if (unify (tparms, targs, |
8d08fdba | 8211 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 8212 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
8213 | return 1; |
8214 | return 0; | |
8215 | } | |
8216 | ||
8d08fdba | 8217 | case RECORD_TYPE: |
f181d4ae | 8218 | case UNION_TYPE: |
db5ae43f | 8219 | if (TYPE_PTRMEMFUNC_FLAG (parm)) |
830bfa74 | 8220 | return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm), |
4393e105 | 8221 | arg, strict); |
8d08fdba | 8222 | |
f181d4ae | 8223 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 8224 | return 1; |
830bfa74 | 8225 | |
5db698f6 | 8226 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 8227 | { |
6467930b | 8228 | tree t = NULL_TREE; |
4393e105 | 8229 | |
830bfa74 | 8230 | if (strict & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
8231 | { |
8232 | /* First, we try to unify the PARM and ARG directly. */ | |
8233 | t = try_class_unification (tparms, targs, | |
8234 | parm, arg); | |
8235 | ||
8236 | if (!t) | |
8237 | { | |
8238 | /* Fallback to the special case allowed in | |
8239 | [temp.deduct.call]: | |
8240 | ||
8241 | If P is a class, and P has the form | |
8242 | template-id, then A can be a derived class of | |
8243 | the deduced A. Likewise, if P is a pointer to | |
8244 | a class of the form template-id, A can be a | |
8245 | pointer to a derived class pointed to by the | |
8246 | deduced A. */ | |
8247 | t = get_template_base (tparms, targs, | |
8248 | parm, arg); | |
8249 | ||
8250 | if (! t || t == error_mark_node) | |
8251 | return 1; | |
8252 | } | |
8253 | } | |
6df47b06 | 8254 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
8255 | && (CLASSTYPE_TI_TEMPLATE (parm) |
8256 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
8257 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
8258 | Then, we should unify `int' and `U'. */ | |
6467930b | 8259 | t = arg; |
4393e105 MM |
8260 | else |
8261 | /* There's no chance of unication succeeding. */ | |
5566b478 | 8262 | return 1; |
6467930b | 8263 | |
830bfa74 | 8264 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 8265 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 8266 | } |
3bfdc719 MM |
8267 | else if (!same_type_p (TYPE_MAIN_VARIANT (parm), |
8268 | TYPE_MAIN_VARIANT (arg))) | |
5566b478 | 8269 | return 1; |
a4443a08 | 8270 | return 0; |
8d08fdba MS |
8271 | |
8272 | case METHOD_TYPE: | |
8d08fdba | 8273 | case FUNCTION_TYPE: |
830bfa74 | 8274 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 8275 | return 1; |
830bfa74 MM |
8276 | |
8277 | if (unify (tparms, targs, TREE_TYPE (parm), | |
4393e105 | 8278 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 8279 | return 1; |
386b8a85 | 8280 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 8281 | TYPE_ARG_TYPES (arg), 1, |
4393e105 | 8282 | DEDUCE_EXACT, 0); |
a4443a08 MS |
8283 | |
8284 | case OFFSET_TYPE: | |
8285 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
8286 | return 1; | |
830bfa74 | 8287 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 8288 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 8289 | return 1; |
830bfa74 | 8290 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 8291 | strict); |
a4443a08 | 8292 | |
f62dbf03 | 8293 | case CONST_DECL: |
830bfa74 | 8294 | if (arg != decl_constant_value (parm)) |
f62dbf03 JM |
8295 | return 1; |
8296 | return 0; | |
8297 | ||
027905b4 KL |
8298 | case TEMPLATE_DECL: |
8299 | /* Matched cases are handled by the ARG == PARM test above. */ | |
8300 | return 1; | |
8301 | ||
53929c47 JM |
8302 | case MINUS_EXPR: |
8303 | if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST) | |
8304 | { | |
8305 | /* We handle this case specially, since it comes up with | |
8306 | arrays. In particular, something like: | |
8307 | ||
8308 | template <int N> void f(int (&x)[N]); | |
8309 | ||
8310 | Here, we are trying to unify the range type, which | |
8311 | looks like [0 ... (N - 1)]. */ | |
8312 | tree t, t1, t2; | |
8313 | t1 = TREE_OPERAND (parm, 0); | |
8314 | t2 = TREE_OPERAND (parm, 1); | |
8315 | ||
8316 | /* Should this be a regular fold? */ | |
8317 | t = maybe_fold_nontype_arg (build (PLUS_EXPR, | |
8318 | integer_type_node, | |
8319 | arg, t2)); | |
8320 | ||
4393e105 | 8321 | return unify (tparms, targs, t1, t, strict); |
53929c47 JM |
8322 | } |
8323 | /* else fall through */ | |
8324 | ||
8d08fdba | 8325 | default: |
050367a3 | 8326 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) |
4393e105 MM |
8327 | /* We're looking at an expression. This can happen with |
8328 | something like: | |
8329 | ||
8330 | template <int I> | |
8331 | void foo(S<I>, S<I + 2>); | |
050367a3 | 8332 | |
4393e105 | 8333 | This is a "nondeduced context": |
050367a3 | 8334 | |
4393e105 MM |
8335 | [deduct.type] |
8336 | ||
8337 | The nondeduced contexts are: | |
050367a3 | 8338 | |
4393e105 MM |
8339 | --A type that is a template-id in which one or more of |
8340 | the template-arguments is an expression that references | |
8341 | a template-parameter. | |
050367a3 | 8342 | |
4393e105 MM |
8343 | In these cases, we assume deduction succeeded, but don't |
8344 | actually infer any unifications. */ | |
8345 | return 0; | |
050367a3 | 8346 | else |
b4c4a9ec MM |
8347 | sorry ("use of `%s' in template type unification", |
8348 | tree_code_name [(int) TREE_CODE (parm)]); | |
050367a3 | 8349 | |
8d08fdba MS |
8350 | return 1; |
8351 | } | |
8352 | } | |
8d08fdba | 8353 | \f |
03d0f4af MM |
8354 | /* Called if RESULT is explicitly instantiated, or is a member of an |
8355 | explicitly instantiated class, or if using -frepo and the | |
8356 | instantiation of RESULT has been assigned to this file. */ | |
8357 | ||
faae18ab | 8358 | void |
5566b478 | 8359 | mark_decl_instantiated (result, extern_p) |
faae18ab MS |
8360 | tree result; |
8361 | int extern_p; | |
8362 | { | |
75650646 MM |
8363 | if (TREE_CODE (result) != FUNCTION_DECL) |
8364 | /* The TREE_PUBLIC flag for function declarations will have been | |
8365 | set correctly by tsubst. */ | |
8366 | TREE_PUBLIC (result) = 1; | |
faae18ab MS |
8367 | |
8368 | if (! extern_p) | |
8369 | { | |
8370 | DECL_INTERFACE_KNOWN (result) = 1; | |
8371 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
a7d87521 | 8372 | |
1a408d07 JM |
8373 | /* Always make artificials weak. */ |
8374 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
8375 | comdat_linkage (result); | |
a7d87521 JM |
8376 | /* For WIN32 we also want to put explicit instantiations in |
8377 | linkonce sections. */ | |
1a408d07 | 8378 | else if (TREE_PUBLIC (result)) |
b385c841 | 8379 | maybe_make_one_only (result); |
faae18ab | 8380 | } |
f49422da MS |
8381 | else if (TREE_CODE (result) == FUNCTION_DECL) |
8382 | mark_inline_for_output (result); | |
faae18ab MS |
8383 | } |
8384 | ||
e1467ff2 MM |
8385 | /* Given two function templates PAT1 and PAT2, and explicit template |
8386 | arguments EXPLICIT_ARGS return: | |
6467930b MS |
8387 | |
8388 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
8389 | -1 if PAT2 is more specialized than PAT1. | |
8390 | 0 if neither is more specialized. */ | |
8391 | ||
8392 | int | |
e1467ff2 MM |
8393 | more_specialized (pat1, pat2, explicit_args) |
8394 | tree pat1, pat2, explicit_args; | |
6467930b | 8395 | { |
98c1c668 | 8396 | tree targs; |
73aad9b9 | 8397 | int winner = 0; |
6467930b | 8398 | |
4393e105 | 8399 | targs = get_bindings_overload (pat1, DECL_RESULT (pat2), explicit_args); |
73aad9b9 | 8400 | if (targs) |
36a117a5 | 8401 | --winner; |
6467930b | 8402 | |
4393e105 | 8403 | targs = get_bindings_overload (pat2, DECL_RESULT (pat1), explicit_args); |
73aad9b9 | 8404 | if (targs) |
36a117a5 | 8405 | ++winner; |
6467930b | 8406 | |
73aad9b9 JM |
8407 | return winner; |
8408 | } | |
6467930b | 8409 | |
73aad9b9 | 8410 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 8411 | |
73aad9b9 JM |
8412 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
8413 | -1 if PAT2 is more specialized than PAT1. | |
8414 | 0 if neither is more specialized. */ | |
8415 | ||
8416 | int | |
8417 | more_specialized_class (pat1, pat2) | |
8418 | tree pat1, pat2; | |
8419 | { | |
8420 | tree targs; | |
8421 | int winner = 0; | |
8422 | ||
36a117a5 MM |
8423 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
8424 | TREE_PURPOSE (pat2)); | |
73aad9b9 JM |
8425 | if (targs) |
8426 | --winner; | |
8427 | ||
36a117a5 MM |
8428 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
8429 | TREE_PURPOSE (pat1)); | |
73aad9b9 | 8430 | if (targs) |
6467930b MS |
8431 | ++winner; |
8432 | ||
8433 | return winner; | |
8434 | } | |
73aad9b9 JM |
8435 | |
8436 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 8437 | DECL from the function template FN, with the explicit template |
76b9a14d | 8438 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must |
4393e105 MM |
8439 | also match. Return NULL_TREE if no satisfactory arguments could be |
8440 | found. */ | |
73aad9b9 | 8441 | |
76b9a14d JM |
8442 | static tree |
8443 | get_bindings_real (fn, decl, explicit_args, check_rettype) | |
e1467ff2 | 8444 | tree fn, decl, explicit_args; |
76b9a14d | 8445 | int check_rettype; |
73aad9b9 | 8446 | { |
98c1c668 | 8447 | int ntparms = DECL_NTPARMS (fn); |
e66d884e | 8448 | tree targs = make_scratch_vec (ntparms); |
4393e105 | 8449 | tree decl_type; |
03017874 | 8450 | tree decl_arg_types; |
98c1c668 JM |
8451 | int i; |
8452 | ||
4393e105 MM |
8453 | /* Substitute the explicit template arguments into the type of DECL. |
8454 | The call to fn_type_unification will handle substitution into the | |
8455 | FN. */ | |
8456 | decl_type = TREE_TYPE (decl); | |
8457 | if (explicit_args && uses_template_parms (decl_type)) | |
8458 | { | |
8459 | tree tmpl; | |
8460 | tree converted_args; | |
8461 | ||
8462 | if (DECL_TEMPLATE_INFO (decl)) | |
8463 | tmpl = DECL_TI_TEMPLATE (decl); | |
8464 | else | |
8465 | /* We can get here for some illegal specializations. */ | |
8466 | return NULL_TREE; | |
8467 | ||
8468 | converted_args | |
8469 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
8470 | explicit_args, NULL_TREE, | |
8471 | /*complain=*/0, | |
8472 | /*require_all_arguments=*/0)); | |
8473 | if (converted_args == error_mark_node) | |
8474 | return NULL_TREE; | |
8475 | ||
8476 | decl_type = tsubst (decl_type, converted_args, /*complain=*/0, | |
8477 | NULL_TREE); | |
8478 | if (decl_type == error_mark_node) | |
8479 | return NULL_TREE; | |
8480 | } | |
8481 | ||
03017874 MM |
8482 | /* If FN is a static member function, adjust the type of DECL |
8483 | appropriately. */ | |
4393e105 | 8484 | decl_arg_types = TYPE_ARG_TYPES (decl_type); |
d7684f2d MM |
8485 | if (DECL_STATIC_FUNCTION_P (fn) |
8486 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 8487 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 8488 | |
e1467ff2 | 8489 | i = fn_type_unification (fn, explicit_args, targs, |
03017874 MM |
8490 | decl_arg_types, |
8491 | TREE_TYPE (decl_type), | |
8492 | DEDUCE_EXACT); | |
98c1c668 | 8493 | |
76b9a14d JM |
8494 | if (i != 0) |
8495 | return NULL_TREE; | |
8496 | ||
8497 | if (check_rettype) | |
e1467ff2 MM |
8498 | { |
8499 | /* Check to see that the resulting return type is also OK. */ | |
36a117a5 | 8500 | tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs, |
4393e105 | 8501 | /*complain=*/0, NULL_TREE); |
e1467ff2 | 8502 | |
3bfdc719 | 8503 | if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl)))) |
e1467ff2 | 8504 | return NULL_TREE; |
e1467ff2 MM |
8505 | } |
8506 | ||
76b9a14d JM |
8507 | return targs; |
8508 | } | |
8509 | ||
8510 | /* For most uses, we want to check the return type. */ | |
8511 | ||
8512 | tree | |
8513 | get_bindings (fn, decl, explicit_args) | |
8514 | tree fn, decl, explicit_args; | |
8515 | { | |
8516 | return get_bindings_real (fn, decl, explicit_args, 1); | |
8517 | } | |
8518 | ||
8519 | /* But for more_specialized, we only care about the parameter types. */ | |
8520 | ||
8521 | static tree | |
8522 | get_bindings_overload (fn, decl, explicit_args) | |
8523 | tree fn, decl, explicit_args; | |
8524 | { | |
8525 | return get_bindings_real (fn, decl, explicit_args, 0); | |
73aad9b9 JM |
8526 | } |
8527 | ||
36a117a5 MM |
8528 | /* Return the innermost template arguments that, when applied to a |
8529 | template specialization whose innermost template parameters are | |
8530 | TPARMS, and whose specialization arguments are ARGS, yield the | |
8531 | ARGS. | |
8532 | ||
8533 | For example, suppose we have: | |
8534 | ||
8535 | template <class T, class U> struct S {}; | |
8536 | template <class T> struct S<T*, int> {}; | |
8537 | ||
8538 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
8539 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
8540 | int}. The resulting vector will be {double}, indicating that `T' | |
8541 | is bound to `double'. */ | |
8542 | ||
bd6dd845 | 8543 | static tree |
36a117a5 MM |
8544 | get_class_bindings (tparms, parms, args) |
8545 | tree tparms, parms, args; | |
73aad9b9 | 8546 | { |
3b3ba9f0 | 8547 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
73aad9b9 JM |
8548 | tree vec = make_temp_vec (ntparms); |
8549 | ||
36a117a5 | 8550 | args = innermost_args (args); |
8d019cef | 8551 | |
4393e105 | 8552 | if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE)) |
fcfb9f96 | 8553 | return NULL_TREE; |
73aad9b9 JM |
8554 | |
8555 | for (i = 0; i < ntparms; ++i) | |
8556 | if (! TREE_VEC_ELT (vec, i)) | |
8557 | return NULL_TREE; | |
8558 | ||
8559 | return vec; | |
8560 | } | |
8561 | ||
104bf76a MM |
8562 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. |
8563 | Pick the most specialized template, and return the corresponding | |
8564 | instantiation, or if there is no corresponding instantiation, the | |
8565 | template itself. EXPLICIT_ARGS is any template arguments explicity | |
8566 | mentioned in a template-id. If there is no most specialized | |
8567 | tempalte, error_mark_node is returned. If there are no templates | |
8568 | at all, NULL_TREE is returned. */ | |
73aad9b9 JM |
8569 | |
8570 | tree | |
104bf76a MM |
8571 | most_specialized_instantiation (instantiations, explicit_args) |
8572 | tree instantiations; | |
8573 | tree explicit_args; | |
73aad9b9 | 8574 | { |
104bf76a | 8575 | tree fn, champ; |
73aad9b9 JM |
8576 | int fate; |
8577 | ||
104bf76a | 8578 | if (!instantiations) |
73aad9b9 JM |
8579 | return NULL_TREE; |
8580 | ||
104bf76a MM |
8581 | champ = instantiations; |
8582 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 8583 | { |
104bf76a MM |
8584 | fate = more_specialized (TREE_VALUE (champ), |
8585 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
8586 | if (fate == 1) |
8587 | ; | |
8588 | else | |
8589 | { | |
8590 | if (fate == 0) | |
8591 | { | |
8592 | fn = TREE_CHAIN (fn); | |
8593 | if (! fn) | |
8594 | return error_mark_node; | |
8595 | } | |
104bf76a | 8596 | champ = fn; |
73aad9b9 JM |
8597 | } |
8598 | } | |
8599 | ||
104bf76a | 8600 | for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) |
73aad9b9 | 8601 | { |
104bf76a MM |
8602 | fate = more_specialized (TREE_VALUE (champ), |
8603 | TREE_VALUE (fn), explicit_args); | |
73aad9b9 JM |
8604 | if (fate != 1) |
8605 | return error_mark_node; | |
8606 | } | |
8607 | ||
104bf76a MM |
8608 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); |
8609 | } | |
8610 | ||
8611 | /* Return the most specialized of the list of templates in FNS that can | |
8612 | produce an instantiation matching DECL, given the explicit template | |
8613 | arguments EXPLICIT_ARGS. */ | |
8614 | ||
e9659ab0 | 8615 | static tree |
104bf76a MM |
8616 | most_specialized (fns, decl, explicit_args) |
8617 | tree fns, decl, explicit_args; | |
8618 | { | |
8619 | tree candidates = NULL_TREE; | |
8620 | tree fn, args; | |
8621 | ||
8622 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
8623 | { | |
8624 | tree candidate = TREE_VALUE (fn); | |
8625 | ||
8626 | args = get_bindings (candidate, decl, explicit_args); | |
8627 | if (args) | |
8628 | candidates = scratch_tree_cons (NULL_TREE, candidate, | |
8629 | candidates); | |
8630 | } | |
8631 | ||
8632 | return most_specialized_instantiation (candidates, explicit_args); | |
73aad9b9 JM |
8633 | } |
8634 | ||
36a117a5 MM |
8635 | /* If DECL is a specialization of some template, return the most |
8636 | general such template. For example, given: | |
8637 | ||
8638 | template <class T> struct S { template <class U> void f(U); }; | |
8639 | ||
8640 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
8641 | the full template. This function will not trace past partial | |
8642 | specializations, however. For example, given in addition: | |
8643 | ||
8644 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
8645 | ||
8646 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
8647 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 8648 | |
e9659ab0 | 8649 | static tree |
36a117a5 MM |
8650 | most_general_template (decl) |
8651 | tree decl; | |
73aad9b9 | 8652 | { |
36a117a5 MM |
8653 | while (DECL_TEMPLATE_INFO (decl)) |
8654 | decl = DECL_TI_TEMPLATE (decl); | |
8655 | ||
8656 | return decl; | |
8657 | } | |
8658 | ||
8659 | /* Return the most specialized of the class template specializations | |
8660 | of TMPL which can produce an instantiation matching ARGS, or | |
8661 | error_mark_node if the choice is ambiguous. */ | |
8662 | ||
e9659ab0 | 8663 | static tree |
36a117a5 MM |
8664 | most_specialized_class (tmpl, args) |
8665 | tree tmpl; | |
8666 | tree args; | |
8667 | { | |
8668 | tree list = NULL_TREE; | |
8669 | tree t; | |
8670 | tree champ; | |
73aad9b9 JM |
8671 | int fate; |
8672 | ||
36a117a5 MM |
8673 | tmpl = most_general_template (tmpl); |
8674 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 8675 | { |
36a117a5 MM |
8676 | tree spec_args |
8677 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
8678 | if (spec_args) | |
73aad9b9 JM |
8679 | { |
8680 | list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
8681 | TREE_TYPE (list) = TREE_TYPE (t); | |
8682 | } | |
8683 | } | |
8684 | ||
8685 | if (! list) | |
8686 | return NULL_TREE; | |
8687 | ||
8688 | t = list; | |
8689 | champ = t; | |
8690 | t = TREE_CHAIN (t); | |
8691 | for (; t; t = TREE_CHAIN (t)) | |
8692 | { | |
8693 | fate = more_specialized_class (champ, t); | |
8694 | if (fate == 1) | |
8695 | ; | |
8696 | else | |
8697 | { | |
8698 | if (fate == 0) | |
8699 | { | |
8700 | t = TREE_CHAIN (t); | |
8701 | if (! t) | |
8702 | return error_mark_node; | |
8703 | } | |
8704 | champ = t; | |
8705 | } | |
8706 | } | |
8707 | ||
8708 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
8709 | { | |
85b71cf2 | 8710 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
8711 | if (fate != 1) |
8712 | return error_mark_node; | |
8713 | } | |
8714 | ||
8715 | return champ; | |
8716 | } | |
8717 | ||
8d08fdba | 8718 | /* called from the parser. */ |
e92cc029 | 8719 | |
8d08fdba | 8720 | void |
6633d636 | 8721 | do_decl_instantiation (declspecs, declarator, storage) |
f0e01782 | 8722 | tree declspecs, declarator, storage; |
8d08fdba | 8723 | { |
c11b6f21 | 8724 | tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE); |
8d08fdba | 8725 | tree result = NULL_TREE; |
faae18ab | 8726 | int extern_p = 0; |
e8abc66f | 8727 | |
ec255269 MS |
8728 | if (! DECL_LANG_SPECIFIC (decl)) |
8729 | { | |
8251199e | 8730 | cp_error ("explicit instantiation of non-template `%#D'", decl); |
ec255269 MS |
8731 | return; |
8732 | } | |
03d0f4af | 8733 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 8734 | { |
03d0f4af MM |
8735 | /* There is an asymmetry here in the way VAR_DECLs and |
8736 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
8737 | the latter, the DECL we get back will be marked as a | |
8738 | template instantiation, and the appropriate | |
8739 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
8740 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
8741 | should handle VAR_DECLs as it currently handles | |
8742 | FUNCTION_DECLs. */ | |
6633d636 MS |
8743 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0); |
8744 | if (result && TREE_CODE (result) != VAR_DECL) | |
03d0f4af | 8745 | { |
8251199e | 8746 | cp_error ("no matching template for `%D' found", result); |
03d0f4af MM |
8747 | return; |
8748 | } | |
6633d636 MS |
8749 | } |
8750 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
8751 | { | |
8251199e | 8752 | cp_error ("explicit instantiation of `%#D'", decl); |
6633d636 MS |
8753 | return; |
8754 | } | |
03d0f4af MM |
8755 | else |
8756 | result = decl; | |
672476cb | 8757 | |
03d0f4af MM |
8758 | /* Check for various error cases. Note that if the explicit |
8759 | instantiation is legal the RESULT will currently be marked as an | |
8760 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
8761 | until we get here. */ | |
8762 | ||
8763 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 8764 | { |
03d0f4af MM |
8765 | /* [temp.spec] |
8766 | ||
8767 | No program shall both explicitly instantiate and explicitly | |
8768 | specialize a template. */ | |
8251199e JM |
8769 | cp_error ("explicit instantiation of `%#D' after", result); |
8770 | cp_error_at ("explicit specialization here", result); | |
672476cb MM |
8771 | return; |
8772 | } | |
03d0f4af MM |
8773 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
8774 | { | |
8775 | /* [temp.spec] | |
98c1c668 | 8776 | |
03d0f4af MM |
8777 | No program shall explicitly instantiate any template more |
8778 | than once. | |
8779 | ||
8780 | We check DECL_INTERFACE_KNOWN so as not to complain when the | |
8781 | first instantiation was `extern' and the second is not, and | |
8782 | EXTERN_P for the opposite case. */ | |
8783 | if (DECL_INTERFACE_KNOWN (result) && !extern_p) | |
8251199e | 8784 | cp_error ("duplicate explicit instantiation of `%#D'", result); |
03d0f4af MM |
8785 | |
8786 | /* If we've already instantiated the template, just return now. */ | |
8787 | if (DECL_INTERFACE_KNOWN (result)) | |
8788 | return; | |
8789 | } | |
8790 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 8791 | { |
8251199e | 8792 | cp_error ("no matching template for `%D' found", result); |
faae18ab MS |
8793 | return; |
8794 | } | |
03d0f4af | 8795 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 8796 | { |
8251199e | 8797 | cp_pedwarn ("explicit instantiation of non-template `%#D'", result); |
6633d636 MS |
8798 | return; |
8799 | } | |
8800 | ||
a0a33927 MS |
8801 | if (flag_external_templates) |
8802 | return; | |
8803 | ||
f0e01782 | 8804 | if (storage == NULL_TREE) |
00595019 | 8805 | ; |
faae18ab | 8806 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af MM |
8807 | { |
8808 | if (pedantic) | |
8251199e | 8809 | cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations"); |
03d0f4af MM |
8810 | extern_p = 1; |
8811 | } | |
f0e01782 | 8812 | else |
8251199e | 8813 | cp_error ("storage class `%D' applied to template instantiation", |
f0e01782 | 8814 | storage); |
5566b478 | 8815 | |
03d0f4af | 8816 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
5566b478 | 8817 | mark_decl_instantiated (result, extern_p); |
44a8d0b3 | 8818 | repo_template_instantiated (result, extern_p); |
c91a56d2 MS |
8819 | if (! extern_p) |
8820 | instantiate_decl (result); | |
7177d104 MS |
8821 | } |
8822 | ||
faae18ab MS |
8823 | void |
8824 | mark_class_instantiated (t, extern_p) | |
8825 | tree t; | |
8826 | int extern_p; | |
8827 | { | |
8828 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
8829 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
8830 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
8831 | CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p; |
8832 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
8833 | if (! extern_p) | |
8834 | { | |
8835 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
8836 | rest_of_type_compilation (t, 1); | |
8837 | } | |
8838 | } | |
e8abc66f | 8839 | |
7177d104 | 8840 | void |
ca79f85d JM |
8841 | do_type_instantiation (t, storage) |
8842 | tree t, storage; | |
7177d104 | 8843 | { |
e8abc66f MS |
8844 | int extern_p = 0; |
8845 | int nomem_p = 0; | |
5566b478 MS |
8846 | int static_p = 0; |
8847 | ||
ca79f85d JM |
8848 | if (TREE_CODE (t) == TYPE_DECL) |
8849 | t = TREE_TYPE (t); | |
8850 | ||
7ddedda4 | 8851 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 8852 | { |
8251199e | 8853 | cp_error ("explicit instantiation of non-template type `%T'", t); |
ca79f85d JM |
8854 | return; |
8855 | } | |
8856 | ||
5566b478 | 8857 | complete_type (t); |
7177d104 | 8858 | |
a292b002 MS |
8859 | /* With -fexternal-templates, explicit instantiations are treated the same |
8860 | as implicit ones. */ | |
a0a33927 MS |
8861 | if (flag_external_templates) |
8862 | return; | |
8863 | ||
f0e01782 MS |
8864 | if (TYPE_SIZE (t) == NULL_TREE) |
8865 | { | |
8251199e | 8866 | cp_error ("explicit instantiation of `%#T' before definition of template", |
f0e01782 MS |
8867 | t); |
8868 | return; | |
8869 | } | |
8870 | ||
03d0f4af | 8871 | if (storage != NULL_TREE) |
f0e01782 | 8872 | { |
03d0f4af | 8873 | if (pedantic) |
8251199e JM |
8874 | cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations", |
8875 | IDENTIFIER_POINTER (storage)); | |
03d0f4af MM |
8876 | |
8877 | if (storage == ridpointers[(int) RID_INLINE]) | |
8878 | nomem_p = 1; | |
8879 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
8880 | extern_p = 1; | |
8881 | else if (storage == ridpointers[(int) RID_STATIC]) | |
8882 | static_p = 1; | |
8883 | else | |
8884 | { | |
8251199e | 8885 | cp_error ("storage class `%D' applied to template instantiation", |
03d0f4af MM |
8886 | storage); |
8887 | extern_p = 0; | |
8888 | } | |
f0e01782 MS |
8889 | } |
8890 | ||
03d0f4af MM |
8891 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
8892 | { | |
8893 | /* [temp.spec] | |
a292b002 | 8894 | |
03d0f4af MM |
8895 | No program shall both explicitly instantiate and explicitly |
8896 | specialize a template. */ | |
8251199e JM |
8897 | cp_error ("explicit instantiation of `%#T' after", t); |
8898 | cp_error_at ("explicit specialization here", t); | |
03d0f4af MM |
8899 | return; |
8900 | } | |
8901 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 8902 | { |
03d0f4af MM |
8903 | /* [temp.spec] |
8904 | ||
8905 | No program shall explicitly instantiate any template more | |
8906 | than once. | |
8907 | ||
8908 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit | |
8909 | instantiation was `extern', and if EXTERN_P then the second | |
8910 | is. Both cases are OK. */ | |
8911 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p) | |
8251199e | 8912 | cp_error ("duplicate explicit instantiation of `%#T'", t); |
03d0f4af MM |
8913 | |
8914 | /* If we've already instantiated the template, just return now. */ | |
8915 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
8916 | return; | |
44a8d0b3 | 8917 | } |
e8abc66f | 8918 | |
03d0f4af MM |
8919 | mark_class_instantiated (t, extern_p); |
8920 | repo_template_instantiated (t, extern_p); | |
8921 | ||
e8abc66f MS |
8922 | if (nomem_p) |
8923 | return; | |
8924 | ||
7177d104 | 8925 | { |
db5ae43f | 8926 | tree tmp; |
5566b478 | 8927 | |
03d0f4af MM |
8928 | /* In contrast to implicit instantiation, where only the |
8929 | declarations, and not the definitions, of members are | |
8930 | instantiated, we have here: | |
8931 | ||
8932 | [temp.explicit] | |
8933 | ||
8934 | The explicit instantiation of a class template specialization | |
8935 | implies the instantiation of all of its members not | |
8936 | previously explicitly specialized in the translation unit | |
8937 | containing the explicit instantiation. | |
8938 | ||
8939 | Of course, we can't instantiate member template classes, since | |
8940 | we don't have any arguments for them. Note that the standard | |
8941 | is unclear on whether the instatiation of the members are | |
8942 | *explicit* instantiations or not. We choose to be generous, | |
8943 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
8944 | the explicit instantiation of a class where some of the members | |
8945 | have no definition in the current translation unit. */ | |
8946 | ||
5566b478 MS |
8947 | if (! static_p) |
8948 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 8949 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 8950 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
5566b478 MS |
8951 | { |
8952 | mark_decl_instantiated (tmp, extern_p); | |
8953 | repo_template_instantiated (tmp, extern_p); | |
8954 | if (! extern_p) | |
8955 | instantiate_decl (tmp); | |
8956 | } | |
8957 | ||
8958 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
8959 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
863adfc0 | 8960 | { |
5566b478 | 8961 | mark_decl_instantiated (tmp, extern_p); |
863adfc0 | 8962 | repo_template_instantiated (tmp, extern_p); |
5566b478 MS |
8963 | if (! extern_p) |
8964 | instantiate_decl (tmp); | |
863adfc0 | 8965 | } |
7177d104 | 8966 | |
a292b002 | 8967 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) |
e4a84209 MM |
8968 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) |
8969 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
f376e137 | 8970 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage); |
a292b002 | 8971 | } |
8d08fdba | 8972 | } |
a28e3c7f | 8973 | |
36a117a5 MM |
8974 | /* Given a function DECL, which is a specialization of TMPL, modify |
8975 | DECL to be a re-instantiation of TMPL with the same template | |
8976 | arguments. TMPL should be the template into which tsubst'ing | |
8977 | should occur for DECL, not the most general template. | |
830bfa74 MM |
8978 | |
8979 | One reason for doing this is a scenario like this: | |
8980 | ||
8981 | template <class T> | |
8982 | void f(const T&, int i); | |
8983 | ||
8984 | void g() { f(3, 7); } | |
8985 | ||
8986 | template <class T> | |
8987 | void f(const T& t, const int i) { } | |
8988 | ||
8989 | Note that when the template is first instantiated, with | |
8990 | instantiate_template, the resulting DECL will have no name for the | |
8991 | first parameter, and the wrong type for the second. So, when we go | |
8992 | to instantiate the DECL, we regenerate it. */ | |
8993 | ||
e9659ab0 | 8994 | static void |
830bfa74 MM |
8995 | regenerate_decl_from_template (decl, tmpl) |
8996 | tree decl; | |
8997 | tree tmpl; | |
8998 | { | |
8999 | tree args; | |
830bfa74 MM |
9000 | tree code_pattern; |
9001 | tree new_decl; | |
36a117a5 MM |
9002 | tree gen_tmpl; |
9003 | int unregistered; | |
830bfa74 MM |
9004 | |
9005 | args = DECL_TI_ARGS (decl); | |
9006 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
9007 | ||
8251199e JM |
9008 | /* Unregister the specialization so that when we tsubst we will not |
9009 | just return DECL. We don't have to unregister DECL from TMPL | |
9010 | because if would only be registered there if it were a partial | |
9011 | instantiation of a specialization, which it isn't: it's a full | |
9012 | instantiation. */ | |
36a117a5 MM |
9013 | gen_tmpl = most_general_template (tmpl); |
9014 | unregistered = unregister_specialization (decl, gen_tmpl); | |
9015 | ||
9016 | /* If the DECL was not unregistered then something peculiar is | |
9017 | happening: we created a specialization but did not call | |
9018 | register_specialization for it. */ | |
9019 | my_friendly_assert (unregistered, 0); | |
9020 | ||
61a127b3 MM |
9021 | if (TREE_CODE (decl) == VAR_DECL) |
9022 | /* Make sure that we can see identifiers, and compute access | |
9023 | correctly, for the class members used in the declaration of | |
9024 | this static variable. */ | |
9025 | pushclass (DECL_CONTEXT (decl), 2); | |
9026 | ||
d8c4447d | 9027 | /* Do the substitution to get the new declaration. */ |
4393e105 | 9028 | new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE); |
830bfa74 MM |
9029 | |
9030 | if (TREE_CODE (decl) == VAR_DECL) | |
9031 | { | |
9032 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
830bfa74 | 9033 | DECL_INITIAL (new_decl) = |
d8c4447d | 9034 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
4393e105 | 9035 | /*complain=*/1, DECL_TI_TEMPLATE (decl)); |
61a127b3 | 9036 | /* Pop the class context we pushed above. */ |
830bfa74 MM |
9037 | popclass (1); |
9038 | } | |
2b0a63a3 | 9039 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
fbf1c34b MM |
9040 | { |
9041 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
9042 | new decl. */ | |
9043 | DECL_INITIAL (new_decl) = error_mark_node; | |
9044 | /* And don't complain about a duplicate definition. */ | |
9045 | DECL_INITIAL (decl) = NULL_TREE; | |
9046 | } | |
36a117a5 MM |
9047 | |
9048 | /* The immediate parent of the new template is still whatever it was | |
9049 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
9050 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
9051 | tsubst always calculates the name as if the function in question | |
9052 | were really a template instance, and sometimes, with friend | |
9053 | functions, this is not so. See tsubst_friend_function for | |
9054 | details. */ | |
9055 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
9056 | DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl); | |
9057 | DECL_RTL (new_decl) = DECL_RTL (decl); | |
9058 | ||
9059 | /* Call duplicate decls to merge the old and new declarations. */ | |
830bfa74 MM |
9060 | duplicate_decls (new_decl, decl); |
9061 | ||
36a117a5 MM |
9062 | /* Now, re-register the specialization. */ |
9063 | register_specialization (decl, gen_tmpl, args); | |
830bfa74 MM |
9064 | } |
9065 | ||
f84b4be9 JM |
9066 | /* Produce the definition of D, a _DECL generated from a template. */ |
9067 | ||
a28e3c7f | 9068 | tree |
5566b478 MS |
9069 | instantiate_decl (d) |
9070 | tree d; | |
a28e3c7f | 9071 | { |
36a117a5 MM |
9072 | tree tmpl = DECL_TI_TEMPLATE (d); |
9073 | tree args = DECL_TI_ARGS (d); | |
830bfa74 | 9074 | tree td; |
36a117a5 MM |
9075 | tree code_pattern; |
9076 | tree spec; | |
9077 | tree gen_tmpl; | |
5566b478 | 9078 | int nested = in_function_p (); |
5566b478 | 9079 | int pattern_defined; |
c27be9b9 JM |
9080 | int line = lineno; |
9081 | char *file = input_filename; | |
5566b478 | 9082 | |
36a117a5 MM |
9083 | /* This function should only be used to instantiate templates for |
9084 | functions and static member variables. */ | |
9085 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
9086 | || TREE_CODE (d) == VAR_DECL, 0); | |
9087 | ||
fbf1c34b | 9088 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af MM |
9089 | /* D has already been instantiated. It might seem reasonable to |
9090 | check whether or not D is an explict instantiation, and, if so, | |
9091 | stop here. But when an explicit instantiation is deferred | |
9092 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
9093 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
9094 | return d; |
9095 | ||
9096 | /* If we already have a specialization of this declaration, then | |
9097 | there's no reason to instantiate it. Note that | |
9098 | retrieve_specialization gives us both instantiations and | |
9099 | specializations, so we must explicitly check | |
9100 | DECL_TEMPLATE_SPECIALIZATION. */ | |
9101 | gen_tmpl = most_general_template (tmpl); | |
9102 | spec = retrieve_specialization (gen_tmpl, args); | |
9103 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
9104 | return spec; | |
9105 | ||
9106 | /* This needs to happen before any tsubsting. */ | |
9107 | if (! push_tinst_level (d)) | |
9108 | return d; | |
9109 | ||
4d85e00e MM |
9110 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
9111 | for the instantiation. This is not always the most general | |
9112 | template. Consider, for example: | |
9113 | ||
9114 | template <class T> | |
9115 | struct S { template <class U> void f(); | |
9116 | template <> void f<int>(); }; | |
9117 | ||
9118 | and an instantiation of S<double>::f<int>. We want TD to be the | |
9119 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
9120 | td = tmpl; | |
9121 | for (td = tmpl; | |
9122 | /* An instantiation cannot have a definition, so we need a | |
9123 | more general template. */ | |
9124 | DECL_TEMPLATE_INSTANTIATION (td) | |
9125 | /* We must also deal with friend templates. Given: | |
9126 | ||
9127 | template <class T> struct S { | |
9128 | template <class U> friend void f() {}; | |
9129 | }; | |
9130 | ||
9131 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
9132 | so far as the language is concerned, but that's still | |
9133 | where we get the pattern for the instantiation from. On | |
9134 | ther hand, if the definition comes outside the class, say: | |
9135 | ||
9136 | template <class T> struct S { | |
9137 | template <class U> friend void f(); | |
9138 | }; | |
9139 | template <class U> friend void f() {} | |
9140 | ||
9141 | we don't need to look any further. That's what the check for | |
9142 | DECL_INITIAL is for. */ | |
9143 | || (TREE_CODE (d) == FUNCTION_DECL | |
61289ca3 | 9144 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td) |
4d85e00e | 9145 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td))); |
f84b4be9 | 9146 | ) |
4d85e00e MM |
9147 | { |
9148 | /* The present template, TD, should not be a definition. If it | |
9149 | were a definition, we should be using it! Note that we | |
9150 | cannot restructure the loop to just keep going until we find | |
9151 | a template with a definition, since that might go too far if | |
9152 | a specialization was declared, but not defined. */ | |
4d85e00e MM |
9153 | my_friendly_assert (!(TREE_CODE (d) == VAR_DECL |
9154 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))), | |
9155 | 0); | |
9156 | ||
9157 | /* Fetch the more general template. */ | |
9158 | td = DECL_TI_TEMPLATE (td); | |
9159 | } | |
27bb8339 | 9160 | |
fee23f54 | 9161 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 9162 | |
5566b478 | 9163 | if (TREE_CODE (d) == FUNCTION_DECL) |
36a117a5 | 9164 | pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE); |
5566b478 | 9165 | else |
36a117a5 | 9166 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
de22184b MS |
9167 | |
9168 | push_to_top_level (); | |
9169 | lineno = DECL_SOURCE_LINE (d); | |
9170 | input_filename = DECL_SOURCE_FILE (d); | |
9171 | ||
de22184b | 9172 | if (pattern_defined) |
5566b478 MS |
9173 | { |
9174 | repo_template_used (d); | |
9175 | ||
9176 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
9177 | { | |
9178 | if (flag_alt_external_templates) | |
9179 | { | |
9180 | if (interface_unknown) | |
9181 | warn_if_unknown_interface (d); | |
9182 | } | |
fee23f54 | 9183 | else if (DECL_INTERFACE_KNOWN (code_pattern)) |
5566b478 MS |
9184 | { |
9185 | DECL_INTERFACE_KNOWN (d) = 1; | |
fee23f54 | 9186 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); |
5566b478 MS |
9187 | } |
9188 | else | |
fee23f54 | 9189 | warn_if_unknown_interface (code_pattern); |
5566b478 MS |
9190 | } |
9191 | ||
e92cc029 | 9192 | if (at_eof) |
5566b478 MS |
9193 | import_export_decl (d); |
9194 | } | |
9195 | ||
c4ae3f91 JM |
9196 | /* Reject all external templates except inline functions. */ |
9197 | if (DECL_INTERFACE_KNOWN (d) | |
9198 | && ! DECL_NOT_REALLY_EXTERN (d) | |
9199 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))) | |
9200 | goto out; | |
9201 | ||
67ffc812 | 9202 | if (TREE_CODE (d) == VAR_DECL |
711734a9 | 9203 | && TREE_READONLY (d) |
67ffc812 MM |
9204 | && DECL_INITIAL (d) == NULL_TREE |
9205 | && DECL_INITIAL (code_pattern) != NULL_TREE) | |
9206 | /* We need to set up DECL_INITIAL regardless of pattern_defined if | |
9207 | the variable is a static const initialized in the class body. */; | |
9208 | else if (! pattern_defined | |
9209 | || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested) | |
9210 | && ! at_eof)) | |
5566b478 | 9211 | { |
67ffc812 | 9212 | /* Defer all templates except inline functions used in another |
c27be9b9 JM |
9213 | function. We restore the source position here because it's used |
9214 | by add_pending_template. */ | |
9215 | lineno = line; | |
9216 | input_filename = file; | |
9217 | ||
03d0f4af MM |
9218 | if (at_eof && !pattern_defined |
9219 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
9220 | /* [temp.explicit] | |
9221 | ||
9222 | The definition of a non-exported function template, a | |
9223 | non-exported member function template, or a non-exported | |
9224 | member function or static data member of a class template | |
9225 | shall be present in every translation unit in which it is | |
9226 | explicitly instantiated. */ | |
8251199e | 9227 | cp_error ("explicit instantiation of `%D' but no definition available", |
03d0f4af MM |
9228 | d); |
9229 | ||
5566b478 | 9230 | add_pending_template (d); |
de22184b | 9231 | goto out; |
5566b478 MS |
9232 | } |
9233 | ||
2b0a63a3 MM |
9234 | /* We're now committed to instantiating this template. Mark it as |
9235 | instantiated so that recursive calls to instantiate_decl do not | |
9236 | try to instantiate it again. */ | |
fbf1c34b | 9237 | DECL_TEMPLATE_INSTANTIATED (d) = 1; |
120722ac | 9238 | |
2b0a63a3 MM |
9239 | /* Regenerate the declaration in case the template has been modified |
9240 | by a subsequent redeclaration. */ | |
9241 | regenerate_decl_from_template (d, td); | |
9242 | ||
120722ac MM |
9243 | /* We already set the file and line above. Reset them now in case |
9244 | they changed as a result of calling regenerate_decl_from_template. */ | |
5156628f MS |
9245 | lineno = DECL_SOURCE_LINE (d); |
9246 | input_filename = DECL_SOURCE_FILE (d); | |
9247 | ||
5566b478 MS |
9248 | if (TREE_CODE (d) == VAR_DECL) |
9249 | { | |
9250 | DECL_IN_AGGR_P (d) = 0; | |
9251 | if (DECL_INTERFACE_KNOWN (d)) | |
9252 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
9253 | else | |
9254 | { | |
9255 | DECL_EXTERNAL (d) = 1; | |
9256 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
9257 | } | |
9258 | cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0); | |
9259 | } | |
9260 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
9261 | { | |
fee23f54 | 9262 | tree t = DECL_SAVED_TREE (code_pattern); |
5566b478 | 9263 | |
c11b6f21 | 9264 | start_function (NULL_TREE, d, NULL_TREE, 1); |
5566b478 MS |
9265 | store_parm_decls (); |
9266 | ||
e76a2646 MS |
9267 | if (t && TREE_CODE (t) == RETURN_INIT) |
9268 | { | |
9269 | store_return_init | |
9270 | (TREE_OPERAND (t, 0), | |
4393e105 | 9271 | tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, tmpl)); |
e76a2646 MS |
9272 | t = TREE_CHAIN (t); |
9273 | } | |
9274 | ||
5566b478 MS |
9275 | if (t && TREE_CODE (t) == CTOR_INITIALIZER) |
9276 | { | |
9277 | current_member_init_list | |
9278 | = tsubst_expr_values (TREE_OPERAND (t, 0), args); | |
9279 | current_base_init_list | |
9280 | = tsubst_expr_values (TREE_OPERAND (t, 1), args); | |
9281 | t = TREE_CHAIN (t); | |
9282 | } | |
9283 | ||
9284 | setup_vtbl_ptr (); | |
9285 | /* Always keep the BLOCK node associated with the outermost | |
956d6950 | 9286 | pair of curly braces of a function. These are needed |
5566b478 MS |
9287 | for correct operation of dwarfout.c. */ |
9288 | keep_next_level (); | |
9289 | ||
9290 | my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42); | |
4393e105 | 9291 | tsubst_expr (t, args, /*complain=*/1, tmpl); |
a28e3c7f | 9292 | |
5566b478 | 9293 | finish_function (lineno, 0, nested); |
5566b478 MS |
9294 | } |
9295 | ||
de22184b | 9296 | out: |
c27be9b9 JM |
9297 | lineno = line; |
9298 | input_filename = file; | |
9299 | ||
5566b478 | 9300 | pop_from_top_level (); |
5566b478 | 9301 | pop_tinst_level (); |
a28e3c7f | 9302 | |
a28e3c7f MS |
9303 | return d; |
9304 | } | |
5566b478 | 9305 | |
4393e105 MM |
9306 | /* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it |
9307 | is an initializer list: the TREE_PURPOSEs are DECLs, and the | |
9308 | TREE_VALUEs are initializer values. Used by instantiate_decl. */ | |
9309 | ||
824b9a4c | 9310 | static tree |
5566b478 MS |
9311 | tsubst_expr_values (t, argvec) |
9312 | tree t, argvec; | |
9313 | { | |
9314 | tree first = NULL_TREE; | |
9315 | tree *p = &first; | |
9316 | ||
9317 | for (; t; t = TREE_CHAIN (t)) | |
9318 | { | |
4393e105 MM |
9319 | tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, |
9320 | /*complain=*/1, NULL_TREE); | |
9321 | tree val = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1, | |
9322 | NULL_TREE); | |
5566b478 MS |
9323 | *p = build_tree_list (pur, val); |
9324 | p = &TREE_CHAIN (*p); | |
9325 | } | |
9326 | return first; | |
9327 | } | |
9328 | ||
9329 | tree last_tree; | |
9330 | ||
9331 | void | |
9332 | add_tree (t) | |
9333 | tree t; | |
9334 | { | |
9335 | last_tree = TREE_CHAIN (last_tree) = t; | |
9336 | } | |
73aad9b9 | 9337 | |
75650646 MM |
9338 | |
9339 | void | |
9340 | begin_tree () | |
9341 | { | |
9342 | saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees); | |
9343 | last_tree = NULL_TREE; | |
9344 | } | |
9345 | ||
9346 | ||
9347 | void | |
9348 | end_tree () | |
9349 | { | |
9350 | my_friendly_assert (saved_trees != NULL_TREE, 0); | |
9351 | ||
9352 | last_tree = TREE_VALUE (saved_trees); | |
9353 | saved_trees = TREE_CHAIN (saved_trees); | |
9354 | } | |
9355 | ||
73aad9b9 JM |
9356 | /* D is an undefined function declaration in the presence of templates with |
9357 | the same name, listed in FNS. If one of them can produce D as an | |
9358 | instantiation, remember this so we can instantiate it at EOF if D has | |
9359 | not been defined by that time. */ | |
9360 | ||
9361 | void | |
9362 | add_maybe_template (d, fns) | |
9363 | tree d, fns; | |
9364 | { | |
9365 | tree t; | |
9366 | ||
9367 | if (DECL_MAYBE_TEMPLATE (d)) | |
9368 | return; | |
9369 | ||
e1467ff2 | 9370 | t = most_specialized (fns, d, NULL_TREE); |
73aad9b9 JM |
9371 | if (! t) |
9372 | return; | |
9373 | if (t == error_mark_node) | |
9374 | { | |
8251199e | 9375 | cp_error ("ambiguous template instantiation for `%D'", d); |
73aad9b9 JM |
9376 | return; |
9377 | } | |
9378 | ||
9379 | *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE); | |
9380 | maybe_template_tail = &TREE_CHAIN (*maybe_template_tail); | |
9381 | DECL_MAYBE_TEMPLATE (d) = 1; | |
9382 | } | |
b87692e5 | 9383 | |
61a127b3 MM |
9384 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
9385 | ||
9386 | static void | |
9387 | set_current_access_from_decl (decl) | |
9388 | tree decl; | |
9389 | { | |
9390 | if (TREE_PRIVATE (decl)) | |
9391 | current_access_specifier = access_private_node; | |
9392 | else if (TREE_PROTECTED (decl)) | |
9393 | current_access_specifier = access_protected_node; | |
9394 | else | |
9395 | current_access_specifier = access_public_node; | |
9396 | } | |
9397 | ||
dbfe2124 MM |
9398 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
9399 | is the instantiation (which should have been created with | |
9400 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 9401 | |
dbfe2124 MM |
9402 | static void |
9403 | tsubst_enum (tag, newtag, args) | |
9404 | tree tag; | |
9405 | tree newtag; | |
9406 | tree args; | |
b87692e5 | 9407 | { |
dbfe2124 | 9408 | tree e; |
b87692e5 MS |
9409 | |
9410 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
9411 | { | |
61a127b3 MM |
9412 | tree value; |
9413 | tree elt; | |
9414 | ||
9415 | /* Note that in a template enum, the TREE_VALUE is the | |
9416 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
9417 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
4393e105 MM |
9418 | args, /*complain=*/1, |
9419 | NULL_TREE); | |
61a127b3 MM |
9420 | |
9421 | /* Give this enumeration constant the correct access. */ | |
9422 | set_current_access_from_decl (TREE_VALUE (e)); | |
9423 | ||
9424 | /* Actually build the enumerator itself. */ | |
9425 | elt = build_enumerator (TREE_PURPOSE (e), value, newtag); | |
b87692e5 | 9426 | |
dbfe2124 MM |
9427 | /* We save the enumerators we have built so far in the |
9428 | TYPE_VALUES so that if the enumeration constants for | |
9429 | subsequent enumerators involve those for previous ones, | |
9430 | tsubst_copy will be able to find them. */ | |
9431 | TREE_CHAIN (elt) = TYPE_VALUES (newtag); | |
9432 | TYPE_VALUES (newtag) = elt; | |
9433 | } | |
b3d5a58b | 9434 | |
dbfe2124 | 9435 | finish_enum (newtag); |
b87692e5 | 9436 | } |
36a117a5 MM |
9437 | |
9438 | /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that | |
9439 | is either an instantiation or specialization of a template | |
9440 | function. */ | |
9441 | ||
9442 | static void | |
9443 | set_mangled_name_for_template_decl (decl) | |
9444 | tree decl; | |
9445 | { | |
9446 | tree saved_namespace; | |
b370501f | 9447 | tree context = NULL_TREE; |
36a117a5 MM |
9448 | tree fn_type; |
9449 | tree ret_type; | |
9450 | tree parm_types; | |
9451 | tree tparms; | |
9452 | tree targs; | |
9453 | tree tmpl; | |
9454 | int parm_depth; | |
9455 | ||
9456 | my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0); | |
9457 | my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0); | |
9458 | ||
9459 | /* The names of template functions must be mangled so as to indicate | |
9460 | what template is being specialized with what template arguments. | |
9461 | For example, each of the following three functions must get | |
9462 | different mangled names: | |
9463 | ||
9464 | void f(int); | |
9465 | template <> void f<7>(int); | |
9466 | template <> void f<8>(int); */ | |
9467 | ||
9468 | targs = DECL_TI_ARGS (decl); | |
9469 | if (uses_template_parms (targs)) | |
9470 | /* This DECL is for a partial instantiation. There's no need to | |
9471 | mangle the name of such an entity. */ | |
9472 | return; | |
9473 | ||
9474 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
9475 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
9476 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
9477 | ||
9478 | /* There should be as many levels of arguments as there are levels | |
9479 | of parameters. */ | |
9480 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
9481 | ||
9482 | /* We now compute the PARMS and RET_TYPE to give to | |
9483 | build_decl_overload_real. The PARMS and RET_TYPE are the | |
9484 | parameter and return types of the template, after all but the | |
9485 | innermost template arguments have been substituted, not the | |
9486 | parameter and return types of the function DECL. For example, | |
9487 | given: | |
9488 | ||
9489 | template <class T> T f(T); | |
9490 | ||
9491 | both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'. | |
9492 | A more subtle example is: | |
9493 | ||
9494 | template <class T> struct S { template <class U> void f(T, U); } | |
9495 | ||
9496 | Here, if DECL is `void S<int>::f(int, double)', PARMS should be | |
9497 | {int, U}. Thus, the args that we want to subsitute into the | |
9498 | return and parameter type for the function are those in TARGS, | |
9499 | with the innermost level omitted. */ | |
9500 | fn_type = TREE_TYPE (tmpl); | |
9501 | if (DECL_STATIC_FUNCTION_P (decl)) | |
61a127b3 | 9502 | context = DECL_CLASS_CONTEXT (decl); |
36a117a5 MM |
9503 | |
9504 | if (parm_depth == 1) | |
9505 | /* No substitution is necessary. */ | |
9506 | ; | |
9507 | else | |
9508 | { | |
9509 | int i; | |
9510 | tree partial_args; | |
9511 | ||
9512 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
9513 | let tsubst know not to subsitute for those parameters. */ | |
9514 | partial_args = make_temp_vec (TREE_VEC_LENGTH (targs)); | |
9515 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
9516 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
9517 | TMPL_ARGS_LEVEL (targs, i)); | |
9518 | SET_TMPL_ARGS_LEVEL (partial_args, | |
9519 | TMPL_ARGS_DEPTH (targs), | |
9520 | make_temp_vec (DECL_NTPARMS (tmpl))); | |
9521 | ||
9522 | /* Now, do the (partial) substitution to figure out the | |
9523 | appropriate function type. */ | |
4393e105 | 9524 | fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 | 9525 | if (DECL_STATIC_FUNCTION_P (decl)) |
4393e105 | 9526 | context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE); |
36a117a5 MM |
9527 | |
9528 | /* Substitute into the template parameters to obtain the real | |
9529 | innermost set of parameters. This step is important if the | |
9530 | innermost set of template parameters contains value | |
9531 | parameters whose types depend on outer template parameters. */ | |
9532 | TREE_VEC_LENGTH (partial_args)--; | |
4393e105 | 9533 | tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1); |
36a117a5 MM |
9534 | } |
9535 | ||
9536 | /* Now, get the innermost parameters and arguments, and figure out | |
9537 | the parameter and return types. */ | |
9538 | tparms = INNERMOST_TEMPLATE_PARMS (tparms); | |
9539 | targs = innermost_args (targs); | |
9540 | ret_type = TREE_TYPE (fn_type); | |
9541 | parm_types = TYPE_ARG_TYPES (fn_type); | |
9542 | ||
9543 | /* For a static member function, we generate a fake `this' pointer, | |
9544 | for the purposes of mangling. This indicates of which class the | |
9545 | function is a member. Because of: | |
9546 | ||
9547 | [class.static] | |
9548 | ||
9549 | There shall not be a static and a nonstatic member function | |
9550 | with the same name and the same parameter types | |
9551 | ||
9552 | we don't have to worry that this will result in a clash with a | |
9553 | non-static member function. */ | |
9554 | if (DECL_STATIC_FUNCTION_P (decl)) | |
9555 | parm_types = hash_tree_chain (build_pointer_type (context), parm_types); | |
9556 | ||
9557 | /* There should be the same number of template parameters as | |
9558 | template arguments. */ | |
9559 | my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs), | |
9560 | 0); | |
9561 | ||
9562 | /* If the template is in a namespace, we need to put that into the | |
9563 | mangled name. Unfortunately, build_decl_overload_real does not | |
9564 | get the decl to mangle, so it relies on the current | |
9565 | namespace. Therefore, we set that here temporarily. */ | |
9566 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702); | |
9567 | saved_namespace = current_namespace; | |
9568 | current_namespace = CP_DECL_CONTEXT (decl); | |
9569 | ||
9570 | /* Actually set the DCL_ASSEMBLER_NAME. */ | |
9571 | DECL_ASSEMBLER_NAME (decl) | |
9572 | = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type, | |
9573 | tparms, targs, | |
9574 | DECL_FUNCTION_MEMBER_P (decl) | |
9575 | + DECL_CONSTRUCTOR_P (decl)); | |
9576 | ||
9577 | /* Restore the previously active namespace. */ | |
9578 | current_namespace = saved_namespace; | |
9579 | } |