]>
Commit | Line | Data |
---|---|---|
1 | /* Handle parameterized types (templates) for GNU C++. | |
2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, | |
3 | 2001, 2002, 2003 Free Software Foundation, Inc. | |
4 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. | |
5 | Rewritten by Jason Merrill (jason@cygnus.com). | |
6 | ||
7 | This file is part of GCC. | |
8 | ||
9 | GCC is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2, or (at your option) | |
12 | any later version. | |
13 | ||
14 | GCC is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with GCC; see the file COPYING. If not, write to | |
21 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | /* Known bugs or deficiencies include: | |
25 | ||
26 | all methods must be provided in header files; can't use a source | |
27 | file that contains only the method templates and "just win". */ | |
28 | ||
29 | #include "config.h" | |
30 | #include "system.h" | |
31 | #include "coretypes.h" | |
32 | #include "tm.h" | |
33 | #include "obstack.h" | |
34 | #include "tree.h" | |
35 | #include "flags.h" | |
36 | #include "cp-tree.h" | |
37 | #include "tree-inline.h" | |
38 | #include "decl.h" | |
39 | #include "lex.h" | |
40 | #include "output.h" | |
41 | #include "except.h" | |
42 | #include "toplev.h" | |
43 | #include "rtl.h" | |
44 | #include "timevar.h" | |
45 | ||
46 | /* The type of functions taking a tree, and some additional data, and | |
47 | returning an int. */ | |
48 | typedef int (*tree_fn_t) PARAMS ((tree, void*)); | |
49 | ||
50 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose | |
51 | instantiations have been deferred, either because their definitions | |
52 | were not yet available, or because we were putting off doing the | |
53 | work. The TREE_PURPOSE of each entry is a SRCLOC indicating where | |
54 | the instantiate request occurred; the TREE_VALUE is either a DECL | |
55 | (for a function or static data member), or a TYPE (for a class) | |
56 | indicating what we are hoping to instantiate. */ | |
57 | static GTY(()) tree pending_templates; | |
58 | static GTY(()) tree last_pending_template; | |
59 | ||
60 | int processing_template_parmlist; | |
61 | static int template_header_count; | |
62 | ||
63 | static GTY(()) tree saved_trees; | |
64 | static GTY(()) varray_type inline_parm_levels; | |
65 | static size_t inline_parm_levels_used; | |
66 | ||
67 | static GTY(()) tree current_tinst_level; | |
68 | ||
69 | static GTY(()) tree saved_access_scope; | |
70 | ||
71 | /* A map from local variable declarations in the body of the template | |
72 | presently being instantiated to the corresponding instantiated | |
73 | local variables. */ | |
74 | static htab_t local_specializations; | |
75 | ||
76 | #define UNIFY_ALLOW_NONE 0 | |
77 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
78 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
79 | #define UNIFY_ALLOW_DERIVED 4 | |
80 | #define UNIFY_ALLOW_INTEGER 8 | |
81 | #define UNIFY_ALLOW_OUTER_LEVEL 16 | |
82 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 | |
83 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
84 | #define UNIFY_ALLOW_MAX_CORRECTION 128 | |
85 | ||
86 | #define GTB_VIA_VIRTUAL 1 /* The base class we are examining is | |
87 | virtual, or a base class of a virtual | |
88 | base. */ | |
89 | #define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current | |
90 | type with the desired type. */ | |
91 | ||
92 | static void push_access_scope_real PARAMS ((tree, tree, tree)); | |
93 | static void push_access_scope PARAMS ((tree)); | |
94 | static void pop_access_scope PARAMS ((tree)); | |
95 | static int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree, | |
96 | unification_kind_t, int)); | |
97 | static int try_one_overload PARAMS ((tree, tree, tree, tree, tree, | |
98 | unification_kind_t, int)); | |
99 | static int unify PARAMS ((tree, tree, tree, tree, int)); | |
100 | static void add_pending_template PARAMS ((tree)); | |
101 | static void reopen_tinst_level PARAMS ((tree)); | |
102 | static tree classtype_mangled_name PARAMS ((tree)); | |
103 | static char *mangle_class_name_for_template PARAMS ((const char *, | |
104 | tree, tree)); | |
105 | static tree tsubst_initializer_list PARAMS ((tree, tree)); | |
106 | static int list_eq PARAMS ((tree, tree)); | |
107 | static tree get_class_bindings PARAMS ((tree, tree, tree)); | |
108 | static tree coerce_template_parms PARAMS ((tree, tree, tree, | |
109 | tsubst_flags_t, int)); | |
110 | static void tsubst_enum PARAMS ((tree, tree, tree)); | |
111 | static tree add_to_template_args PARAMS ((tree, tree)); | |
112 | static tree add_outermost_template_args PARAMS ((tree, tree)); | |
113 | static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*, | |
114 | tree*)); | |
115 | static int type_unification_real PARAMS ((tree, tree, tree, tree, | |
116 | int, unification_kind_t, int, int)); | |
117 | static void note_template_header PARAMS ((int)); | |
118 | static tree maybe_fold_nontype_arg PARAMS ((tree)); | |
119 | static tree convert_nontype_argument PARAMS ((tree, tree)); | |
120 | static tree convert_template_argument PARAMS ((tree, tree, tree, | |
121 | tsubst_flags_t, int, tree)); | |
122 | static tree get_bindings_overload PARAMS ((tree, tree, tree)); | |
123 | static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*, htab_t)); | |
124 | static tree build_template_parm_index PARAMS ((int, int, int, tree, tree)); | |
125 | static int inline_needs_template_parms PARAMS ((tree)); | |
126 | static void push_inline_template_parms_recursive PARAMS ((tree, int)); | |
127 | static tree retrieve_specialization PARAMS ((tree, tree)); | |
128 | static tree retrieve_local_specialization PARAMS ((tree)); | |
129 | static tree register_specialization PARAMS ((tree, tree, tree)); | |
130 | static void register_local_specialization PARAMS ((tree, tree)); | |
131 | static int unregister_specialization PARAMS ((tree, tree)); | |
132 | static tree reduce_template_parm_level PARAMS ((tree, tree, int)); | |
133 | static tree build_template_decl PARAMS ((tree, tree)); | |
134 | static int mark_template_parm PARAMS ((tree, void *)); | |
135 | static int template_parm_this_level_p PARAMS ((tree, void *)); | |
136 | static tree tsubst_friend_function PARAMS ((tree, tree)); | |
137 | static tree tsubst_friend_class PARAMS ((tree, tree)); | |
138 | static int can_complete_type_without_circularity PARAMS ((tree)); | |
139 | static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int)); | |
140 | static int template_decl_level PARAMS ((tree)); | |
141 | static int check_cv_quals_for_unify PARAMS ((int, tree, tree)); | |
142 | static tree tsubst_template_arg_vector PARAMS ((tree, tree, tsubst_flags_t)); | |
143 | static tree tsubst_template_parms PARAMS ((tree, tree, tsubst_flags_t)); | |
144 | static void regenerate_decl_from_template PARAMS ((tree, tree)); | |
145 | static tree most_specialized PARAMS ((tree, tree, tree)); | |
146 | static tree most_specialized_class PARAMS ((tree, tree)); | |
147 | static int template_class_depth_real PARAMS ((tree, int)); | |
148 | static tree tsubst_aggr_type PARAMS ((tree, tree, tsubst_flags_t, tree, int)); | |
149 | static tree tsubst_decl PARAMS ((tree, tree, tree, tsubst_flags_t)); | |
150 | static tree tsubst_arg_types PARAMS ((tree, tree, tsubst_flags_t, tree)); | |
151 | static tree tsubst_function_type PARAMS ((tree, tree, tsubst_flags_t, tree)); | |
152 | static void check_specialization_scope PARAMS ((void)); | |
153 | static tree process_partial_specialization PARAMS ((tree)); | |
154 | static void set_current_access_from_decl PARAMS ((tree)); | |
155 | static void check_default_tmpl_args PARAMS ((tree, tree, int, int)); | |
156 | static tree tsubst_call_declarator_parms PARAMS ((tree, tree, | |
157 | tsubst_flags_t, tree)); | |
158 | static tree get_template_base_recursive PARAMS ((tree, tree, | |
159 | tree, tree, tree, int)); | |
160 | static tree get_template_base PARAMS ((tree, tree, tree, tree)); | |
161 | static int verify_class_unification PARAMS ((tree, tree, tree)); | |
162 | static tree try_class_unification PARAMS ((tree, tree, tree, tree)); | |
163 | static int coerce_template_template_parms PARAMS ((tree, tree, tsubst_flags_t, | |
164 | tree, tree)); | |
165 | static tree determine_specialization PARAMS ((tree, tree, tree *, int)); | |
166 | static int template_args_equal PARAMS ((tree, tree)); | |
167 | static void tsubst_default_arguments PARAMS ((tree)); | |
168 | static tree for_each_template_parm_r PARAMS ((tree *, int *, void *)); | |
169 | static tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree)); | |
170 | static void copy_default_args_to_explicit_spec PARAMS ((tree)); | |
171 | static int invalid_nontype_parm_type_p PARAMS ((tree, tsubst_flags_t)); | |
172 | static int eq_local_specializations (const void *, const void *); | |
173 | static tree template_for_substitution (tree); | |
174 | static bool dependent_type_p_r (tree); | |
175 | static bool dependent_template_id_p (tree, tree); | |
176 | static tree tsubst (tree, tree, tsubst_flags_t, tree); | |
177 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree); | |
178 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); | |
179 | static tree tsubst_copy_and_build (tree, tree, tsubst_flags_t, tree); | |
180 | ||
181 | /* Make the current scope suitable for access checking when we are | |
182 | processing T. T can be FUNCTION_DECL for instantiated function | |
183 | template, TEMPLATE_DECL for uninstantiated one, or VAR_DECL for | |
184 | static member variable (need by instantiate_decl). ARGS is the | |
185 | template argument for TEMPLATE_DECL. If CONTEXT is not NULL_TREE, | |
186 | this is used instead of the context of T. */ | |
187 | ||
188 | void | |
189 | push_access_scope_real (t, args, context) | |
190 | tree t, args, context; | |
191 | { | |
192 | if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) | |
193 | { | |
194 | /* When we are processing specialization `foo<Outer>' for code like | |
195 | ||
196 | template <class U> typename U::Inner foo (); | |
197 | class Outer { | |
198 | struct Inner {}; | |
199 | friend Outer::Inner foo<Outer> (); | |
200 | }; | |
201 | ||
202 | `T' is a TEMPLATE_DECL, but `Outer' is only a friend of one of | |
203 | its specialization. We can get the FUNCTION_DECL with the right | |
204 | information because this specialization has already been | |
205 | registered by the friend declaration above. */ | |
206 | ||
207 | if (DECL_FUNCTION_TEMPLATE_P (t) && args) | |
208 | { | |
209 | tree full_args = tsubst_template_arg_vector | |
210 | (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)), args, tf_none); | |
211 | tree spec = NULL_TREE; | |
212 | if (full_args != error_mark_node) | |
213 | spec = retrieve_specialization (t, full_args); | |
214 | if (spec) | |
215 | t = spec; | |
216 | } | |
217 | } | |
218 | ||
219 | if (!context) | |
220 | context = DECL_CONTEXT (t); | |
221 | if (context && TYPE_P (context)) | |
222 | push_nested_class (context); | |
223 | else | |
224 | push_to_top_level (); | |
225 | ||
226 | if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) | |
227 | { | |
228 | saved_access_scope = tree_cons | |
229 | (NULL_TREE, current_function_decl, saved_access_scope); | |
230 | current_function_decl = t; | |
231 | } | |
232 | } | |
233 | ||
234 | /* Like push_access_scope_real, but always uses DECL_CONTEXT. */ | |
235 | ||
236 | void | |
237 | push_access_scope (t) | |
238 | tree t; | |
239 | { | |
240 | push_access_scope_real (t, NULL_TREE, NULL_TREE); | |
241 | } | |
242 | ||
243 | /* Restore the scope set up by push_access_scope. T is the node we | |
244 | are processing. */ | |
245 | ||
246 | void | |
247 | pop_access_scope (t) | |
248 | tree t; | |
249 | { | |
250 | if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t)) | |
251 | { | |
252 | current_function_decl = TREE_VALUE (saved_access_scope); | |
253 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
254 | } | |
255 | ||
256 | if (DECL_CLASS_SCOPE_P (t)) | |
257 | pop_nested_class (); | |
258 | else | |
259 | pop_from_top_level (); | |
260 | } | |
261 | ||
262 | /* Do any processing required when DECL (a member template | |
263 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
264 | to DECL, unless it is a specialization, in which case the DECL | |
265 | itself is returned. */ | |
266 | ||
267 | tree | |
268 | finish_member_template_decl (decl) | |
269 | tree decl; | |
270 | { | |
271 | if (decl == error_mark_node) | |
272 | return error_mark_node; | |
273 | ||
274 | my_friendly_assert (DECL_P (decl), 20020812); | |
275 | ||
276 | if (TREE_CODE (decl) == TYPE_DECL) | |
277 | { | |
278 | tree type; | |
279 | ||
280 | type = TREE_TYPE (decl); | |
281 | if (IS_AGGR_TYPE (type) | |
282 | && CLASSTYPE_TEMPLATE_INFO (type) | |
283 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
284 | { | |
285 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); | |
286 | check_member_template (tmpl); | |
287 | return tmpl; | |
288 | } | |
289 | return NULL_TREE; | |
290 | } | |
291 | else if (TREE_CODE (decl) == FIELD_DECL) | |
292 | error ("data member `%D' cannot be a member template", decl); | |
293 | else if (DECL_TEMPLATE_INFO (decl)) | |
294 | { | |
295 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) | |
296 | { | |
297 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
298 | return DECL_TI_TEMPLATE (decl); | |
299 | } | |
300 | else | |
301 | return decl; | |
302 | } | |
303 | else | |
304 | error ("invalid member template declaration `%D'", decl); | |
305 | ||
306 | return error_mark_node; | |
307 | } | |
308 | ||
309 | /* Returns the template nesting level of the indicated class TYPE. | |
310 | ||
311 | For example, in: | |
312 | template <class T> | |
313 | struct A | |
314 | { | |
315 | template <class U> | |
316 | struct B {}; | |
317 | }; | |
318 | ||
319 | A<T>::B<U> has depth two, while A<T> has depth one. | |
320 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
321 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
322 | specializations. | |
323 | ||
324 | This function is guaranteed to return 0 if passed NULL_TREE so | |
325 | that, for example, `template_class_depth (current_class_type)' is | |
326 | always safe. */ | |
327 | ||
328 | static int | |
329 | template_class_depth_real (type, count_specializations) | |
330 | tree type; | |
331 | int count_specializations; | |
332 | { | |
333 | int depth; | |
334 | ||
335 | for (depth = 0; | |
336 | type && TREE_CODE (type) != NAMESPACE_DECL; | |
337 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
338 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) | |
339 | { | |
340 | if (TREE_CODE (type) != FUNCTION_DECL) | |
341 | { | |
342 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
343 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
344 | && ((count_specializations | |
345 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
346 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) | |
347 | ++depth; | |
348 | } | |
349 | else | |
350 | { | |
351 | if (DECL_TEMPLATE_INFO (type) | |
352 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
353 | && ((count_specializations | |
354 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
355 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
356 | ++depth; | |
357 | } | |
358 | } | |
359 | ||
360 | return depth; | |
361 | } | |
362 | ||
363 | /* Returns the template nesting level of the indicated class TYPE. | |
364 | Like template_class_depth_real, but instantiations do not count in | |
365 | the depth. */ | |
366 | ||
367 | int | |
368 | template_class_depth (type) | |
369 | tree type; | |
370 | { | |
371 | return template_class_depth_real (type, /*count_specializations=*/0); | |
372 | } | |
373 | ||
374 | /* Returns 1 if processing DECL as part of do_pending_inlines | |
375 | needs us to push template parms. */ | |
376 | ||
377 | static int | |
378 | inline_needs_template_parms (decl) | |
379 | tree decl; | |
380 | { | |
381 | if (! DECL_TEMPLATE_INFO (decl)) | |
382 | return 0; | |
383 | ||
384 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) | |
385 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); | |
386 | } | |
387 | ||
388 | /* Subroutine of maybe_begin_member_template_processing. | |
389 | Push the template parms in PARMS, starting from LEVELS steps into the | |
390 | chain, and ending at the beginning, since template parms are listed | |
391 | innermost first. */ | |
392 | ||
393 | static void | |
394 | push_inline_template_parms_recursive (parmlist, levels) | |
395 | tree parmlist; | |
396 | int levels; | |
397 | { | |
398 | tree parms = TREE_VALUE (parmlist); | |
399 | int i; | |
400 | ||
401 | if (levels > 1) | |
402 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
403 | ||
404 | ++processing_template_decl; | |
405 | current_template_parms | |
406 | = tree_cons (size_int (processing_template_decl), | |
407 | parms, current_template_parms); | |
408 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; | |
409 | ||
410 | pushlevel (0); | |
411 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) | |
412 | { | |
413 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
414 | my_friendly_assert (DECL_P (parm), 0); | |
415 | ||
416 | switch (TREE_CODE (parm)) | |
417 | { | |
418 | case TYPE_DECL: | |
419 | case TEMPLATE_DECL: | |
420 | pushdecl (parm); | |
421 | break; | |
422 | ||
423 | case PARM_DECL: | |
424 | { | |
425 | /* Make a CONST_DECL as is done in process_template_parm. | |
426 | It is ugly that we recreate this here; the original | |
427 | version built in process_template_parm is no longer | |
428 | available. */ | |
429 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), | |
430 | TREE_TYPE (parm)); | |
431 | DECL_ARTIFICIAL (decl) = 1; | |
432 | DECL_INITIAL (decl) = DECL_INITIAL (parm); | |
433 | SET_DECL_TEMPLATE_PARM_P (decl); | |
434 | pushdecl (decl); | |
435 | } | |
436 | break; | |
437 | ||
438 | default: | |
439 | abort (); | |
440 | } | |
441 | } | |
442 | } | |
443 | ||
444 | /* Restore the template parameter context for a member template or | |
445 | a friend template defined in a class definition. */ | |
446 | ||
447 | void | |
448 | maybe_begin_member_template_processing (decl) | |
449 | tree decl; | |
450 | { | |
451 | tree parms; | |
452 | int levels = 0; | |
453 | ||
454 | if (inline_needs_template_parms (decl)) | |
455 | { | |
456 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
457 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
458 | ||
459 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) | |
460 | { | |
461 | --levels; | |
462 | parms = TREE_CHAIN (parms); | |
463 | } | |
464 | ||
465 | push_inline_template_parms_recursive (parms, levels); | |
466 | } | |
467 | ||
468 | /* Remember how many levels of template parameters we pushed so that | |
469 | we can pop them later. */ | |
470 | if (!inline_parm_levels) | |
471 | VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); | |
472 | if (inline_parm_levels_used == inline_parm_levels->num_elements) | |
473 | VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); | |
474 | VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; | |
475 | ++inline_parm_levels_used; | |
476 | } | |
477 | ||
478 | /* Undo the effects of begin_member_template_processing. */ | |
479 | ||
480 | void | |
481 | maybe_end_member_template_processing () | |
482 | { | |
483 | int i; | |
484 | ||
485 | if (!inline_parm_levels_used) | |
486 | return; | |
487 | ||
488 | --inline_parm_levels_used; | |
489 | for (i = 0; | |
490 | i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); | |
491 | ++i) | |
492 | { | |
493 | --processing_template_decl; | |
494 | current_template_parms = TREE_CHAIN (current_template_parms); | |
495 | poplevel (0, 0, 0); | |
496 | } | |
497 | } | |
498 | ||
499 | /* Returns nonzero iff T is a member template function. We must be | |
500 | careful as in | |
501 | ||
502 | template <class T> class C { void f(); } | |
503 | ||
504 | Here, f is a template function, and a member, but not a member | |
505 | template. This function does not concern itself with the origin of | |
506 | T, only its present state. So if we have | |
507 | ||
508 | template <class T> class C { template <class U> void f(U); } | |
509 | ||
510 | then neither C<int>::f<char> nor C<T>::f<double> is considered | |
511 | to be a member template. But, `template <class U> void | |
512 | C<int>::f(U)' is considered a member template. */ | |
513 | ||
514 | int | |
515 | is_member_template (t) | |
516 | tree t; | |
517 | { | |
518 | if (!DECL_FUNCTION_TEMPLATE_P (t)) | |
519 | /* Anything that isn't a function or a template function is | |
520 | certainly not a member template. */ | |
521 | return 0; | |
522 | ||
523 | /* A local class can't have member templates. */ | |
524 | if (decl_function_context (t)) | |
525 | return 0; | |
526 | ||
527 | return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)) | |
528 | /* If there are more levels of template parameters than | |
529 | there are template classes surrounding the declaration, | |
530 | then we have a member template. */ | |
531 | && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > | |
532 | template_class_depth (DECL_CONTEXT (t)))); | |
533 | } | |
534 | ||
535 | #if 0 /* UNUSED */ | |
536 | /* Returns nonzero iff T is a member template class. See | |
537 | is_member_template for a description of what precisely constitutes | |
538 | a member template. */ | |
539 | ||
540 | int | |
541 | is_member_template_class (t) | |
542 | tree t; | |
543 | { | |
544 | if (!DECL_CLASS_TEMPLATE_P (t)) | |
545 | /* Anything that isn't a class template, is certainly not a member | |
546 | template. */ | |
547 | return 0; | |
548 | ||
549 | if (!DECL_CLASS_SCOPE_P (t)) | |
550 | /* Anything whose context isn't a class type is surely not a | |
551 | member template. */ | |
552 | return 0; | |
553 | ||
554 | /* If there are more levels of template parameters than there are | |
555 | template classes surrounding the declaration, then we have a | |
556 | member template. */ | |
557 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > | |
558 | template_class_depth (DECL_CONTEXT (t))); | |
559 | } | |
560 | #endif | |
561 | ||
562 | /* Return a new template argument vector which contains all of ARGS, | |
563 | but has as its innermost set of arguments the EXTRA_ARGS. */ | |
564 | ||
565 | static tree | |
566 | add_to_template_args (args, extra_args) | |
567 | tree args; | |
568 | tree extra_args; | |
569 | { | |
570 | tree new_args; | |
571 | int extra_depth; | |
572 | int i; | |
573 | int j; | |
574 | ||
575 | extra_depth = TMPL_ARGS_DEPTH (extra_args); | |
576 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); | |
577 | ||
578 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) | |
579 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
580 | ||
581 | for (j = 1; j <= extra_depth; ++j, ++i) | |
582 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
583 | ||
584 | return new_args; | |
585 | } | |
586 | ||
587 | /* Like add_to_template_args, but only the outermost ARGS are added to | |
588 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
589 | (EXTRA_ARGS) levels are added. This function is used to combine | |
590 | the template arguments from a partial instantiation with the | |
591 | template arguments used to attain the full instantiation from the | |
592 | partial instantiation. */ | |
593 | ||
594 | static tree | |
595 | add_outermost_template_args (args, extra_args) | |
596 | tree args; | |
597 | tree extra_args; | |
598 | { | |
599 | tree new_args; | |
600 | ||
601 | /* If there are more levels of EXTRA_ARGS than there are ARGS, | |
602 | something very fishy is going on. */ | |
603 | my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args), | |
604 | 0); | |
605 | ||
606 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
607 | them. */ | |
608 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
609 | return extra_args; | |
610 | ||
611 | /* For the moment, we make ARGS look like it contains fewer levels. */ | |
612 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
613 | ||
614 | new_args = add_to_template_args (args, extra_args); | |
615 | ||
616 | /* Now, we restore ARGS to its full dimensions. */ | |
617 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
618 | ||
619 | return new_args; | |
620 | } | |
621 | ||
622 | /* Return the N levels of innermost template arguments from the ARGS. */ | |
623 | ||
624 | tree | |
625 | get_innermost_template_args (args, n) | |
626 | tree args; | |
627 | int n; | |
628 | { | |
629 | tree new_args; | |
630 | int extra_levels; | |
631 | int i; | |
632 | ||
633 | my_friendly_assert (n >= 0, 20000603); | |
634 | ||
635 | /* If N is 1, just return the innermost set of template arguments. */ | |
636 | if (n == 1) | |
637 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
638 | ||
639 | /* If we're not removing anything, just return the arguments we were | |
640 | given. */ | |
641 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
642 | my_friendly_assert (extra_levels >= 0, 20000603); | |
643 | if (extra_levels == 0) | |
644 | return args; | |
645 | ||
646 | /* Make a new set of arguments, not containing the outer arguments. */ | |
647 | new_args = make_tree_vec (n); | |
648 | for (i = 1; i <= n; ++i) | |
649 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
650 | TMPL_ARGS_LEVEL (args, i + extra_levels)); | |
651 | ||
652 | return new_args; | |
653 | } | |
654 | ||
655 | /* We've got a template header coming up; push to a new level for storing | |
656 | the parms. */ | |
657 | ||
658 | void | |
659 | begin_template_parm_list () | |
660 | { | |
661 | /* We use a non-tag-transparent scope here, which causes pushtag to | |
662 | put tags in this scope, rather than in the enclosing class or | |
663 | namespace scope. This is the right thing, since we want | |
664 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
665 | global template class, push_template_decl handles putting the | |
666 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
667 | e.g.: | |
668 | ||
669 | template <class T> struct S1 { | |
670 | template <class T> struct S2 {}; | |
671 | }; | |
672 | ||
673 | pushtag contains special code to call pushdecl_with_scope on the | |
674 | TEMPLATE_DECL for S2. */ | |
675 | begin_scope (sk_template_parms); | |
676 | ++processing_template_decl; | |
677 | ++processing_template_parmlist; | |
678 | note_template_header (0); | |
679 | } | |
680 | ||
681 | /* This routine is called when a specialization is declared. If it is | |
682 | invalid to declare a specialization here, an error is reported. */ | |
683 | ||
684 | static void | |
685 | check_specialization_scope () | |
686 | { | |
687 | tree scope = current_scope (); | |
688 | ||
689 | /* [temp.expl.spec] | |
690 | ||
691 | An explicit specialization shall be declared in the namespace of | |
692 | which the template is a member, or, for member templates, in the | |
693 | namespace of which the enclosing class or enclosing class | |
694 | template is a member. An explicit specialization of a member | |
695 | function, member class or static data member of a class template | |
696 | shall be declared in the namespace of which the class template | |
697 | is a member. */ | |
698 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
699 | error ("explicit specialization in non-namespace scope `%D'", | |
700 | scope); | |
701 | ||
702 | /* [temp.expl.spec] | |
703 | ||
704 | In an explicit specialization declaration for a member of a class | |
705 | template or a member template that appears in namespace scope, | |
706 | the member template and some of its enclosing class templates may | |
707 | remain unspecialized, except that the declaration shall not | |
708 | explicitly specialize a class member template if its enclosing | |
709 | class templates are not explicitly specialized as well. */ | |
710 | if (current_template_parms) | |
711 | error ("enclosing class templates are not explicitly specialized"); | |
712 | } | |
713 | ||
714 | /* We've just seen template <>. */ | |
715 | ||
716 | void | |
717 | begin_specialization () | |
718 | { | |
719 | begin_scope (sk_template_spec); | |
720 | note_template_header (1); | |
721 | check_specialization_scope (); | |
722 | } | |
723 | ||
724 | /* Called at then end of processing a declaration preceded by | |
725 | template<>. */ | |
726 | ||
727 | void | |
728 | end_specialization () | |
729 | { | |
730 | finish_scope (); | |
731 | reset_specialization (); | |
732 | } | |
733 | ||
734 | /* Any template <>'s that we have seen thus far are not referring to a | |
735 | function specialization. */ | |
736 | ||
737 | void | |
738 | reset_specialization () | |
739 | { | |
740 | processing_specialization = 0; | |
741 | template_header_count = 0; | |
742 | } | |
743 | ||
744 | /* We've just seen a template header. If SPECIALIZATION is nonzero, | |
745 | it was of the form template <>. */ | |
746 | ||
747 | static void | |
748 | note_template_header (specialization) | |
749 | int specialization; | |
750 | { | |
751 | processing_specialization = specialization; | |
752 | template_header_count++; | |
753 | } | |
754 | ||
755 | /* We're beginning an explicit instantiation. */ | |
756 | ||
757 | void | |
758 | begin_explicit_instantiation () | |
759 | { | |
760 | my_friendly_assert (!processing_explicit_instantiation, 20020913); | |
761 | processing_explicit_instantiation = true; | |
762 | } | |
763 | ||
764 | ||
765 | void | |
766 | end_explicit_instantiation () | |
767 | { | |
768 | my_friendly_assert(processing_explicit_instantiation, 20020913); | |
769 | processing_explicit_instantiation = false; | |
770 | } | |
771 | ||
772 | /* The TYPE is being declared. If it is a template type, that means it | |
773 | is a partial specialization. Do appropriate error-checking. */ | |
774 | ||
775 | void | |
776 | maybe_process_partial_specialization (type) | |
777 | tree type; | |
778 | { | |
779 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) | |
780 | { | |
781 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) | |
782 | && !COMPLETE_TYPE_P (type)) | |
783 | { | |
784 | if (current_namespace | |
785 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
786 | { | |
787 | pedwarn ("specializing `%#T' in different namespace", type); | |
788 | cp_pedwarn_at (" from definition of `%#D'", | |
789 | CLASSTYPE_TI_TEMPLATE (type)); | |
790 | } | |
791 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
792 | if (processing_template_decl) | |
793 | push_template_decl (TYPE_MAIN_DECL (type)); | |
794 | } | |
795 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
796 | error ("specialization of `%T' after instantiation", type); | |
797 | } | |
798 | else if (processing_specialization) | |
799 | error ("explicit specialization of non-template `%T'", type); | |
800 | } | |
801 | ||
802 | /* Retrieve the specialization (in the sense of [temp.spec] - a | |
803 | specialization is either an instantiation or an explicit | |
804 | specialization) of TMPL for the given template ARGS. If there is | |
805 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
806 | arguments, or a vector of vectors of arguments, in the case of | |
807 | templates with more than one level of parameters. */ | |
808 | ||
809 | static tree | |
810 | retrieve_specialization (tmpl, args) | |
811 | tree tmpl; | |
812 | tree args; | |
813 | { | |
814 | tree s; | |
815 | ||
816 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
817 | ||
818 | /* There should be as many levels of arguments as there are | |
819 | levels of parameters. */ | |
820 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
821 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
822 | 0); | |
823 | ||
824 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
825 | s != NULL_TREE; | |
826 | s = TREE_CHAIN (s)) | |
827 | if (comp_template_args (TREE_PURPOSE (s), args)) | |
828 | return TREE_VALUE (s); | |
829 | ||
830 | return NULL_TREE; | |
831 | } | |
832 | ||
833 | /* Like retrieve_specialization, but for local declarations. */ | |
834 | ||
835 | static tree | |
836 | retrieve_local_specialization (tmpl) | |
837 | tree tmpl; | |
838 | { | |
839 | tree spec = | |
840 | (tree) htab_find_with_hash (local_specializations, tmpl, | |
841 | htab_hash_pointer (tmpl)); | |
842 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; | |
843 | } | |
844 | ||
845 | /* Returns nonzero iff DECL is a specialization of TMPL. */ | |
846 | ||
847 | int | |
848 | is_specialization_of (decl, tmpl) | |
849 | tree decl; | |
850 | tree tmpl; | |
851 | { | |
852 | tree t; | |
853 | ||
854 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
855 | { | |
856 | for (t = decl; | |
857 | t != NULL_TREE; | |
858 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
859 | if (t == tmpl) | |
860 | return 1; | |
861 | } | |
862 | else | |
863 | { | |
864 | my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0); | |
865 | ||
866 | for (t = TREE_TYPE (decl); | |
867 | t != NULL_TREE; | |
868 | t = CLASSTYPE_USE_TEMPLATE (t) | |
869 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
870 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) | |
871 | return 1; | |
872 | } | |
873 | ||
874 | return 0; | |
875 | } | |
876 | ||
877 | /* Register the specialization SPEC as a specialization of TMPL with | |
878 | the indicated ARGS. Returns SPEC, or an equivalent prior | |
879 | declaration, if available. */ | |
880 | ||
881 | static tree | |
882 | register_specialization (spec, tmpl, args) | |
883 | tree spec; | |
884 | tree tmpl; | |
885 | tree args; | |
886 | { | |
887 | tree s; | |
888 | ||
889 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
890 | ||
891 | if (TREE_CODE (spec) == FUNCTION_DECL | |
892 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
893 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
894 | register it; we want the corresponding TEMPLATE_DECL instead. | |
895 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
896 | the more obvious `uses_template_parms (spec)' to avoid problems | |
897 | with default function arguments. In particular, given | |
898 | something like this: | |
899 | ||
900 | template <class T> void f(T t1, T t = T()) | |
901 | ||
902 | the default argument expression is not substituted for in an | |
903 | instantiation unless and until it is actually needed. */ | |
904 | return spec; | |
905 | ||
906 | /* There should be as many levels of arguments as there are | |
907 | levels of parameters. */ | |
908 | my_friendly_assert (TMPL_ARGS_DEPTH (args) | |
909 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)), | |
910 | 0); | |
911 | ||
912 | for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
913 | s != NULL_TREE; | |
914 | s = TREE_CHAIN (s)) | |
915 | { | |
916 | tree fn = TREE_VALUE (s); | |
917 | ||
918 | /* We can sometimes try to re-register a specialization that we've | |
919 | already got. In particular, regenerate_decl_from_template | |
920 | calls duplicate_decls which will update the specialization | |
921 | list. But, we'll still get called again here anyhow. It's | |
922 | more convenient to simply allow this than to try to prevent it. */ | |
923 | if (fn == spec) | |
924 | return spec; | |
925 | else if (comp_template_args (TREE_PURPOSE (s), args)) | |
926 | { | |
927 | if (DECL_TEMPLATE_SPECIALIZATION (spec)) | |
928 | { | |
929 | if (DECL_TEMPLATE_INSTANTIATION (fn)) | |
930 | { | |
931 | if (TREE_USED (fn) | |
932 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
933 | { | |
934 | error ("specialization of %D after instantiation", | |
935 | fn); | |
936 | return spec; | |
937 | } | |
938 | else | |
939 | { | |
940 | /* This situation should occur only if the first | |
941 | specialization is an implicit instantiation, | |
942 | the second is an explicit specialization, and | |
943 | the implicit instantiation has not yet been | |
944 | used. That situation can occur if we have | |
945 | implicitly instantiated a member function and | |
946 | then specialized it later. | |
947 | ||
948 | We can also wind up here if a friend | |
949 | declaration that looked like an instantiation | |
950 | turns out to be a specialization: | |
951 | ||
952 | template <class T> void foo(T); | |
953 | class S { friend void foo<>(int) }; | |
954 | template <> void foo(int); | |
955 | ||
956 | We transform the existing DECL in place so that | |
957 | any pointers to it become pointers to the | |
958 | updated declaration. | |
959 | ||
960 | If there was a definition for the template, but | |
961 | not for the specialization, we want this to | |
962 | look as if there is no definition, and vice | |
963 | versa. */ | |
964 | DECL_INITIAL (fn) = NULL_TREE; | |
965 | duplicate_decls (spec, fn); | |
966 | ||
967 | return fn; | |
968 | } | |
969 | } | |
970 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) | |
971 | { | |
972 | duplicate_decls (spec, fn); | |
973 | return fn; | |
974 | } | |
975 | } | |
976 | } | |
977 | } | |
978 | ||
979 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
980 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
981 | ||
982 | return spec; | |
983 | } | |
984 | ||
985 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
986 | Returns nonzero if the SPEC was listed as a specialization of | |
987 | TMPL. */ | |
988 | ||
989 | static int | |
990 | unregister_specialization (spec, tmpl) | |
991 | tree spec; | |
992 | tree tmpl; | |
993 | { | |
994 | tree* s; | |
995 | ||
996 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
997 | *s != NULL_TREE; | |
998 | s = &TREE_CHAIN (*s)) | |
999 | if (TREE_VALUE (*s) == spec) | |
1000 | { | |
1001 | *s = TREE_CHAIN (*s); | |
1002 | return 1; | |
1003 | } | |
1004 | ||
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | /* Compare an entry in the local specializations hash table P1 (which | |
1009 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1010 | DECL). */ | |
1011 | ||
1012 | static int | |
1013 | eq_local_specializations (const void *p1, const void *p2) | |
1014 | { | |
1015 | return TREE_VALUE ((tree) p1) == (tree) p2; | |
1016 | } | |
1017 | ||
1018 | /* Hash P1, an entry in the local specializations table. */ | |
1019 | ||
1020 | static hashval_t | |
1021 | hash_local_specialization (const void* p1) | |
1022 | { | |
1023 | return htab_hash_pointer (TREE_VALUE ((tree) p1)); | |
1024 | } | |
1025 | ||
1026 | /* Like register_specialization, but for local declarations. We are | |
1027 | registering SPEC, an instantiation of TMPL. */ | |
1028 | ||
1029 | static void | |
1030 | register_local_specialization (spec, tmpl) | |
1031 | tree spec; | |
1032 | tree tmpl; | |
1033 | { | |
1034 | void **slot; | |
1035 | ||
1036 | slot = htab_find_slot_with_hash (local_specializations, tmpl, | |
1037 | htab_hash_pointer (tmpl), INSERT); | |
1038 | *slot = build_tree_list (spec, tmpl); | |
1039 | } | |
1040 | ||
1041 | /* Print the list of candidate FNS in an error message. */ | |
1042 | ||
1043 | void | |
1044 | print_candidates (fns) | |
1045 | tree fns; | |
1046 | { | |
1047 | tree fn; | |
1048 | ||
1049 | const char *str = "candidates are:"; | |
1050 | ||
1051 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1052 | { | |
1053 | tree f; | |
1054 | ||
1055 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
1056 | cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); | |
1057 | str = " "; | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | /* Returns the template (one of the functions given by TEMPLATE_ID) | |
1062 | which can be specialized to match the indicated DECL with the | |
1063 | explicit template args given in TEMPLATE_ID. The DECL may be | |
1064 | NULL_TREE if none is available. In that case, the functions in | |
1065 | TEMPLATE_ID are non-members. | |
1066 | ||
1067 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a | |
1068 | specialization of a member template. | |
1069 | ||
1070 | The template args (those explicitly specified and those deduced) | |
1071 | are output in a newly created vector *TARGS_OUT. | |
1072 | ||
1073 | If it is impossible to determine the result, an error message is | |
1074 | issued. The error_mark_node is returned to indicate failure. */ | |
1075 | ||
1076 | static tree | |
1077 | determine_specialization (template_id, decl, targs_out, | |
1078 | need_member_template) | |
1079 | tree template_id; | |
1080 | tree decl; | |
1081 | tree* targs_out; | |
1082 | int need_member_template; | |
1083 | { | |
1084 | tree fns; | |
1085 | tree targs; | |
1086 | tree explicit_targs; | |
1087 | tree candidates = NULL_TREE; | |
1088 | tree templates = NULL_TREE; | |
1089 | ||
1090 | *targs_out = NULL_TREE; | |
1091 | ||
1092 | if (template_id == error_mark_node) | |
1093 | return error_mark_node; | |
1094 | ||
1095 | fns = TREE_OPERAND (template_id, 0); | |
1096 | explicit_targs = TREE_OPERAND (template_id, 1); | |
1097 | ||
1098 | if (fns == error_mark_node) | |
1099 | return error_mark_node; | |
1100 | ||
1101 | /* Check for baselinks. */ | |
1102 | if (BASELINK_P (fns)) | |
1103 | fns = BASELINK_FUNCTIONS (fns); | |
1104 | ||
1105 | if (!is_overloaded_fn (fns)) | |
1106 | { | |
1107 | error ("`%D' is not a function template", fns); | |
1108 | return error_mark_node; | |
1109 | } | |
1110 | ||
1111 | for (; fns; fns = OVL_NEXT (fns)) | |
1112 | { | |
1113 | tree fn = OVL_CURRENT (fns); | |
1114 | ||
1115 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1116 | { | |
1117 | tree decl_arg_types; | |
1118 | ||
1119 | /* DECL might be a specialization of FN. */ | |
1120 | ||
1121 | /* Adjust the type of DECL in case FN is a static member. */ | |
1122 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1123 | if (DECL_STATIC_FUNCTION_P (fn) | |
1124 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1125 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1126 | ||
1127 | /* Check that the number of function parameters matches. | |
1128 | For example, | |
1129 | template <class T> void f(int i = 0); | |
1130 | template <> void f<int>(); | |
1131 | The specialization f<int> is invalid but is not caught | |
1132 | by get_bindings below. */ | |
1133 | ||
1134 | if (list_length (TYPE_ARG_TYPES (TREE_TYPE (fn))) | |
1135 | != list_length (decl_arg_types)) | |
1136 | continue; | |
1137 | ||
1138 | /* See whether this function might be a specialization of this | |
1139 | template. */ | |
1140 | targs = get_bindings (fn, decl, explicit_targs); | |
1141 | ||
1142 | if (!targs) | |
1143 | /* We cannot deduce template arguments that when used to | |
1144 | specialize TMPL will produce DECL. */ | |
1145 | continue; | |
1146 | ||
1147 | /* Save this template, and the arguments deduced. */ | |
1148 | templates = tree_cons (targs, fn, templates); | |
1149 | } | |
1150 | else if (need_member_template) | |
1151 | /* FN is an ordinary member function, and we need a | |
1152 | specialization of a member template. */ | |
1153 | ; | |
1154 | else if (TREE_CODE (fn) != FUNCTION_DECL) | |
1155 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1156 | cases. */ | |
1157 | ; | |
1158 | else if (!DECL_FUNCTION_MEMBER_P (fn)) | |
1159 | /* This is just an ordinary non-member function. Nothing can | |
1160 | be a specialization of that. */ | |
1161 | ; | |
1162 | else if (DECL_ARTIFICIAL (fn)) | |
1163 | /* Cannot specialize functions that are created implicitly. */ | |
1164 | ; | |
1165 | else | |
1166 | { | |
1167 | tree decl_arg_types; | |
1168 | ||
1169 | /* This is an ordinary member function. However, since | |
1170 | we're here, we can assume it's enclosing class is a | |
1171 | template class. For example, | |
1172 | ||
1173 | template <typename T> struct S { void f(); }; | |
1174 | template <> void S<int>::f() {} | |
1175 | ||
1176 | Here, S<int>::f is a non-template, but S<int> is a | |
1177 | template class. If FN has the same type as DECL, we | |
1178 | might be in business. */ | |
1179 | ||
1180 | if (!DECL_TEMPLATE_INFO (fn)) | |
1181 | /* Its enclosing class is an explicit specialization | |
1182 | of a template class. This is not a candidate. */ | |
1183 | continue; | |
1184 | ||
1185 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), | |
1186 | TREE_TYPE (TREE_TYPE (fn)))) | |
1187 | /* The return types differ. */ | |
1188 | continue; | |
1189 | ||
1190 | /* Adjust the type of DECL in case FN is a static member. */ | |
1191 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1192 | if (DECL_STATIC_FUNCTION_P (fn) | |
1193 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1194 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1195 | ||
1196 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1197 | decl_arg_types)) | |
1198 | /* They match! */ | |
1199 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | if (templates && TREE_CHAIN (templates)) | |
1204 | { | |
1205 | /* We have: | |
1206 | ||
1207 | [temp.expl.spec] | |
1208 | ||
1209 | It is possible for a specialization with a given function | |
1210 | signature to be instantiated from more than one function | |
1211 | template. In such cases, explicit specification of the | |
1212 | template arguments must be used to uniquely identify the | |
1213 | function template specialization being specialized. | |
1214 | ||
1215 | Note that here, there's no suggestion that we're supposed to | |
1216 | determine which of the candidate templates is most | |
1217 | specialized. However, we, also have: | |
1218 | ||
1219 | [temp.func.order] | |
1220 | ||
1221 | Partial ordering of overloaded function template | |
1222 | declarations is used in the following contexts to select | |
1223 | the function template to which a function template | |
1224 | specialization refers: | |
1225 | ||
1226 | -- when an explicit specialization refers to a function | |
1227 | template. | |
1228 | ||
1229 | So, we do use the partial ordering rules, at least for now. | |
1230 | This extension can only serve to make invalid programs valid, | |
1231 | so it's safe. And, there is strong anecdotal evidence that | |
1232 | the committee intended the partial ordering rules to apply; | |
1233 | the EDG front-end has that behavior, and John Spicer claims | |
1234 | that the committee simply forgot to delete the wording in | |
1235 | [temp.expl.spec]. */ | |
1236 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1237 | if (tmpl && tmpl != error_mark_node) | |
1238 | { | |
1239 | targs = get_bindings (tmpl, decl, explicit_targs); | |
1240 | templates = tree_cons (targs, tmpl, NULL_TREE); | |
1241 | } | |
1242 | } | |
1243 | ||
1244 | if (templates == NULL_TREE && candidates == NULL_TREE) | |
1245 | { | |
1246 | cp_error_at ("template-id `%D' for `%+D' does not match any template declaration", | |
1247 | template_id, decl); | |
1248 | return error_mark_node; | |
1249 | } | |
1250 | else if ((templates && TREE_CHAIN (templates)) | |
1251 | || (candidates && TREE_CHAIN (candidates)) | |
1252 | || (templates && candidates)) | |
1253 | { | |
1254 | cp_error_at ("ambiguous template specialization `%D' for `%+D'", | |
1255 | template_id, decl); | |
1256 | chainon (candidates, templates); | |
1257 | print_candidates (candidates); | |
1258 | return error_mark_node; | |
1259 | } | |
1260 | ||
1261 | /* We have one, and exactly one, match. */ | |
1262 | if (candidates) | |
1263 | { | |
1264 | /* It was a specialization of an ordinary member function in a | |
1265 | template class. */ | |
1266 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1267 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1268 | } | |
1269 | ||
1270 | /* It was a specialization of a template. */ | |
1271 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); | |
1272 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) | |
1273 | { | |
1274 | *targs_out = copy_node (targs); | |
1275 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1276 | TMPL_ARGS_DEPTH (*targs_out), | |
1277 | TREE_PURPOSE (templates)); | |
1278 | } | |
1279 | else | |
1280 | *targs_out = TREE_PURPOSE (templates); | |
1281 | return TREE_VALUE (templates); | |
1282 | } | |
1283 | ||
1284 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1285 | but with the default argument values filled in from those in the | |
1286 | TMPL_TYPES. */ | |
1287 | ||
1288 | static tree | |
1289 | copy_default_args_to_explicit_spec_1 (spec_types, | |
1290 | tmpl_types) | |
1291 | tree spec_types; | |
1292 | tree tmpl_types; | |
1293 | { | |
1294 | tree new_spec_types; | |
1295 | ||
1296 | if (!spec_types) | |
1297 | return NULL_TREE; | |
1298 | ||
1299 | if (spec_types == void_list_node) | |
1300 | return void_list_node; | |
1301 | ||
1302 | /* Substitute into the rest of the list. */ | |
1303 | new_spec_types = | |
1304 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1305 | TREE_CHAIN (tmpl_types)); | |
1306 | ||
1307 | /* Add the default argument for this parameter. */ | |
1308 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1309 | TREE_VALUE (spec_types), | |
1310 | new_spec_types); | |
1311 | } | |
1312 | ||
1313 | /* DECL is an explicit specialization. Replicate default arguments | |
1314 | from the template it specializes. (That way, code like: | |
1315 | ||
1316 | template <class T> void f(T = 3); | |
1317 | template <> void f(double); | |
1318 | void g () { f (); } | |
1319 | ||
1320 | works, as required.) An alternative approach would be to look up | |
1321 | the correct default arguments at the call-site, but this approach | |
1322 | is consistent with how implicit instantiations are handled. */ | |
1323 | ||
1324 | static void | |
1325 | copy_default_args_to_explicit_spec (decl) | |
1326 | tree decl; | |
1327 | { | |
1328 | tree tmpl; | |
1329 | tree spec_types; | |
1330 | tree tmpl_types; | |
1331 | tree new_spec_types; | |
1332 | tree old_type; | |
1333 | tree new_type; | |
1334 | tree t; | |
1335 | tree object_type = NULL_TREE; | |
1336 | tree in_charge = NULL_TREE; | |
1337 | tree vtt = NULL_TREE; | |
1338 | ||
1339 | /* See if there's anything we need to do. */ | |
1340 | tmpl = DECL_TI_TEMPLATE (decl); | |
1341 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1342 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1343 | if (TREE_PURPOSE (t)) | |
1344 | break; | |
1345 | if (!t) | |
1346 | return; | |
1347 | ||
1348 | old_type = TREE_TYPE (decl); | |
1349 | spec_types = TYPE_ARG_TYPES (old_type); | |
1350 | ||
1351 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1352 | { | |
1353 | /* Remove the this pointer, but remember the object's type for | |
1354 | CV quals. */ | |
1355 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); | |
1356 | spec_types = TREE_CHAIN (spec_types); | |
1357 | tmpl_types = TREE_CHAIN (tmpl_types); | |
1358 | ||
1359 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) | |
1360 | { | |
1361 | /* DECL may contain more parameters than TMPL due to the extra | |
1362 | in-charge parameter in constructors and destructors. */ | |
1363 | in_charge = spec_types; | |
1364 | spec_types = TREE_CHAIN (spec_types); | |
1365 | } | |
1366 | if (DECL_HAS_VTT_PARM_P (decl)) | |
1367 | { | |
1368 | vtt = spec_types; | |
1369 | spec_types = TREE_CHAIN (spec_types); | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | /* Compute the merged default arguments. */ | |
1374 | new_spec_types = | |
1375 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); | |
1376 | ||
1377 | /* Compute the new FUNCTION_TYPE. */ | |
1378 | if (object_type) | |
1379 | { | |
1380 | if (vtt) | |
1381 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), | |
1382 | TREE_VALUE (vtt), | |
1383 | new_spec_types); | |
1384 | ||
1385 | if (in_charge) | |
1386 | /* Put the in-charge parameter back. */ | |
1387 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1388 | TREE_VALUE (in_charge), | |
1389 | new_spec_types); | |
1390 | ||
1391 | new_type = build_cplus_method_type (object_type, | |
1392 | TREE_TYPE (old_type), | |
1393 | new_spec_types); | |
1394 | } | |
1395 | else | |
1396 | new_type = build_function_type (TREE_TYPE (old_type), | |
1397 | new_spec_types); | |
1398 | new_type = build_type_attribute_variant (new_type, | |
1399 | TYPE_ATTRIBUTES (old_type)); | |
1400 | new_type = build_exception_variant (new_type, | |
1401 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1402 | TREE_TYPE (decl) = new_type; | |
1403 | } | |
1404 | ||
1405 | /* Check to see if the function just declared, as indicated in | |
1406 | DECLARATOR, and in DECL, is a specialization of a function | |
1407 | template. We may also discover that the declaration is an explicit | |
1408 | instantiation at this point. | |
1409 | ||
1410 | Returns DECL, or an equivalent declaration that should be used | |
1411 | instead if all goes well. Issues an error message if something is | |
1412 | amiss. Returns error_mark_node if the error is not easily | |
1413 | recoverable. | |
1414 | ||
1415 | FLAGS is a bitmask consisting of the following flags: | |
1416 | ||
1417 | 2: The function has a definition. | |
1418 | 4: The function is a friend. | |
1419 | ||
1420 | The TEMPLATE_COUNT is the number of references to qualifying | |
1421 | template classes that appeared in the name of the function. For | |
1422 | example, in | |
1423 | ||
1424 | template <class T> struct S { void f(); }; | |
1425 | void S<int>::f(); | |
1426 | ||
1427 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1428 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1429 | ||
1430 | template <class T> struct S {}; | |
1431 | template <> struct S<int> { void f(); } | |
1432 | template <> void S<int>::f(); | |
1433 | ||
1434 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
1435 | invalid; there should be no template <>.) | |
1436 | ||
1437 | If the function is a specialization, it is marked as such via | |
1438 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1439 | is set up correctly, and it is added to the list of specializations | |
1440 | for that template. */ | |
1441 | ||
1442 | tree | |
1443 | check_explicit_specialization (declarator, decl, template_count, flags) | |
1444 | tree declarator; | |
1445 | tree decl; | |
1446 | int template_count; | |
1447 | int flags; | |
1448 | { | |
1449 | int have_def = flags & 2; | |
1450 | int is_friend = flags & 4; | |
1451 | int specialization = 0; | |
1452 | int explicit_instantiation = 0; | |
1453 | int member_specialization = 0; | |
1454 | tree ctype = DECL_CLASS_CONTEXT (decl); | |
1455 | tree dname = DECL_NAME (decl); | |
1456 | tmpl_spec_kind tsk; | |
1457 | ||
1458 | tsk = current_tmpl_spec_kind (template_count); | |
1459 | ||
1460 | switch (tsk) | |
1461 | { | |
1462 | case tsk_none: | |
1463 | if (processing_specialization) | |
1464 | { | |
1465 | specialization = 1; | |
1466 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1467 | } | |
1468 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
1469 | { | |
1470 | if (is_friend) | |
1471 | /* This could be something like: | |
1472 | ||
1473 | template <class T> void f(T); | |
1474 | class S { friend void f<>(int); } */ | |
1475 | specialization = 1; | |
1476 | else | |
1477 | { | |
1478 | /* This case handles bogus declarations like template <> | |
1479 | template <class T> void f<int>(); */ | |
1480 | ||
1481 | error ("template-id `%D' in declaration of primary template", | |
1482 | declarator); | |
1483 | return decl; | |
1484 | } | |
1485 | } | |
1486 | break; | |
1487 | ||
1488 | case tsk_invalid_member_spec: | |
1489 | /* The error has already been reported in | |
1490 | check_specialization_scope. */ | |
1491 | return error_mark_node; | |
1492 | ||
1493 | case tsk_invalid_expl_inst: | |
1494 | error ("template parameter list used in explicit instantiation"); | |
1495 | ||
1496 | /* Fall through. */ | |
1497 | ||
1498 | case tsk_expl_inst: | |
1499 | if (have_def) | |
1500 | error ("definition provided for explicit instantiation"); | |
1501 | ||
1502 | explicit_instantiation = 1; | |
1503 | break; | |
1504 | ||
1505 | case tsk_excessive_parms: | |
1506 | error ("too many template parameter lists in declaration of `%D'", | |
1507 | decl); | |
1508 | return error_mark_node; | |
1509 | ||
1510 | /* Fall through. */ | |
1511 | case tsk_expl_spec: | |
1512 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1513 | if (ctype) | |
1514 | member_specialization = 1; | |
1515 | else | |
1516 | specialization = 1; | |
1517 | break; | |
1518 | ||
1519 | case tsk_insufficient_parms: | |
1520 | if (template_header_count) | |
1521 | { | |
1522 | error("too few template parameter lists in declaration of `%D'", | |
1523 | decl); | |
1524 | return decl; | |
1525 | } | |
1526 | else if (ctype != NULL_TREE | |
1527 | && !TYPE_BEING_DEFINED (ctype) | |
1528 | && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype) | |
1529 | && !is_friend) | |
1530 | { | |
1531 | /* For backwards compatibility, we accept: | |
1532 | ||
1533 | template <class T> struct S { void f(); }; | |
1534 | void S<int>::f() {} // Missing template <> | |
1535 | ||
1536 | That used to be valid C++. */ | |
1537 | if (pedantic) | |
1538 | pedwarn | |
1539 | ("explicit specialization not preceded by `template <>'"); | |
1540 | specialization = 1; | |
1541 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1542 | } | |
1543 | break; | |
1544 | ||
1545 | case tsk_template: | |
1546 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
1547 | { | |
1548 | /* This case handles bogus declarations like template <> | |
1549 | template <class T> void f<int>(); */ | |
1550 | ||
1551 | if (uses_template_parms (declarator)) | |
1552 | error ("partial specialization `%D' of function template", | |
1553 | declarator); | |
1554 | else | |
1555 | error ("template-id `%D' in declaration of primary template", | |
1556 | declarator); | |
1557 | return decl; | |
1558 | } | |
1559 | ||
1560 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1561 | /* This is a specialization of a member template, without | |
1562 | specialization the containing class. Something like: | |
1563 | ||
1564 | template <class T> struct S { | |
1565 | template <class U> void f (U); | |
1566 | }; | |
1567 | template <> template <class U> void S<int>::f(U) {} | |
1568 | ||
1569 | That's a specialization -- but of the entire template. */ | |
1570 | specialization = 1; | |
1571 | break; | |
1572 | ||
1573 | default: | |
1574 | abort (); | |
1575 | } | |
1576 | ||
1577 | if (specialization || member_specialization) | |
1578 | { | |
1579 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1580 | for (; t; t = TREE_CHAIN (t)) | |
1581 | if (TREE_PURPOSE (t)) | |
1582 | { | |
1583 | pedwarn | |
1584 | ("default argument specified in explicit specialization"); | |
1585 | break; | |
1586 | } | |
1587 | if (current_lang_name == lang_name_c) | |
1588 | error ("template specialization with C linkage"); | |
1589 | } | |
1590 | ||
1591 | if (specialization || member_specialization || explicit_instantiation) | |
1592 | { | |
1593 | tree tmpl = NULL_TREE; | |
1594 | tree targs = NULL_TREE; | |
1595 | ||
1596 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
1597 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) | |
1598 | { | |
1599 | tree fns; | |
1600 | ||
1601 | my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE, | |
1602 | 0); | |
1603 | if (!ctype) | |
1604 | fns = IDENTIFIER_NAMESPACE_VALUE (dname); | |
1605 | else | |
1606 | fns = dname; | |
1607 | ||
1608 | declarator = | |
1609 | lookup_template_function (fns, NULL_TREE); | |
1610 | } | |
1611 | ||
1612 | if (declarator == error_mark_node) | |
1613 | return error_mark_node; | |
1614 | ||
1615 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) | |
1616 | { | |
1617 | if (!explicit_instantiation) | |
1618 | /* A specialization in class scope. This is invalid, | |
1619 | but the error will already have been flagged by | |
1620 | check_specialization_scope. */ | |
1621 | return error_mark_node; | |
1622 | else | |
1623 | { | |
1624 | /* It's not valid to write an explicit instantiation in | |
1625 | class scope, e.g.: | |
1626 | ||
1627 | class C { template void f(); } | |
1628 | ||
1629 | This case is caught by the parser. However, on | |
1630 | something like: | |
1631 | ||
1632 | template class C { void f(); }; | |
1633 | ||
1634 | (which is invalid) we can get here. The error will be | |
1635 | issued later. */ | |
1636 | ; | |
1637 | } | |
1638 | ||
1639 | return decl; | |
1640 | } | |
1641 | else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) | |
1642 | { | |
1643 | /* A friend declaration. We can't do much, because we don't | |
1644 | know what this resolves to, yet. */ | |
1645 | my_friendly_assert (is_friend != 0, 0); | |
1646 | my_friendly_assert (!explicit_instantiation, 0); | |
1647 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
1648 | return decl; | |
1649 | } | |
1650 | else if (ctype != NULL_TREE | |
1651 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1652 | IDENTIFIER_NODE)) | |
1653 | { | |
1654 | /* Find the list of functions in ctype that have the same | |
1655 | name as the declared function. */ | |
1656 | tree name = TREE_OPERAND (declarator, 0); | |
1657 | tree fns = NULL_TREE; | |
1658 | int idx; | |
1659 | ||
1660 | if (constructor_name_p (name, ctype)) | |
1661 | { | |
1662 | int is_constructor = DECL_CONSTRUCTOR_P (decl); | |
1663 | ||
1664 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
1665 | : !TYPE_HAS_DESTRUCTOR (ctype)) | |
1666 | { | |
1667 | /* From [temp.expl.spec]: | |
1668 | ||
1669 | If such an explicit specialization for the member | |
1670 | of a class template names an implicitly-declared | |
1671 | special member function (clause _special_), the | |
1672 | program is ill-formed. | |
1673 | ||
1674 | Similar language is found in [temp.explicit]. */ | |
1675 | error ("specialization of implicitly-declared special member function"); | |
1676 | return error_mark_node; | |
1677 | } | |
1678 | ||
1679 | name = is_constructor ? ctor_identifier : dtor_identifier; | |
1680 | } | |
1681 | ||
1682 | if (!DECL_CONV_FN_P (decl)) | |
1683 | { | |
1684 | idx = lookup_fnfields_1 (ctype, name); | |
1685 | if (idx >= 0) | |
1686 | fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx); | |
1687 | } | |
1688 | else | |
1689 | { | |
1690 | tree methods; | |
1691 | ||
1692 | /* For a type-conversion operator, we cannot do a | |
1693 | name-based lookup. We might be looking for `operator | |
1694 | int' which will be a specialization of `operator T'. | |
1695 | So, we find *all* the conversion operators, and then | |
1696 | select from them. */ | |
1697 | fns = NULL_TREE; | |
1698 | ||
1699 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1700 | if (methods) | |
1701 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; | |
1702 | idx < TREE_VEC_LENGTH (methods); ++idx) | |
1703 | { | |
1704 | tree ovl = TREE_VEC_ELT (methods, idx); | |
1705 | ||
1706 | if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl))) | |
1707 | /* There are no more conversion functions. */ | |
1708 | break; | |
1709 | ||
1710 | /* Glue all these conversion functions together | |
1711 | with those we already have. */ | |
1712 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1713 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1714 | } | |
1715 | } | |
1716 | ||
1717 | if (fns == NULL_TREE) | |
1718 | { | |
1719 | error ("no member function `%D' declared in `%T'", | |
1720 | name, ctype); | |
1721 | return error_mark_node; | |
1722 | } | |
1723 | else | |
1724 | TREE_OPERAND (declarator, 0) = fns; | |
1725 | } | |
1726 | ||
1727 | /* Figure out what exactly is being specialized at this point. | |
1728 | Note that for an explicit instantiation, even one for a | |
1729 | member function, we cannot tell apriori whether the | |
1730 | instantiation is for a member template, or just a member | |
1731 | function of a template class. Even if a member template is | |
1732 | being instantiated, the member template arguments may be | |
1733 | elided if they can be deduced from the rest of the | |
1734 | declaration. */ | |
1735 | tmpl = determine_specialization (declarator, decl, | |
1736 | &targs, | |
1737 | member_specialization); | |
1738 | ||
1739 | if (!tmpl || tmpl == error_mark_node) | |
1740 | /* We couldn't figure out what this declaration was | |
1741 | specializing. */ | |
1742 | return error_mark_node; | |
1743 | else | |
1744 | { | |
1745 | tree gen_tmpl = most_general_template (tmpl); | |
1746 | ||
1747 | if (explicit_instantiation) | |
1748 | { | |
1749 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that | |
1750 | is done by do_decl_instantiation later. */ | |
1751 | ||
1752 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
1753 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
1754 | ||
1755 | if (arg_depth > parm_depth) | |
1756 | { | |
1757 | /* If TMPL is not the most general template (for | |
1758 | example, if TMPL is a friend template that is | |
1759 | injected into namespace scope), then there will | |
1760 | be too many levels of TARGS. Remove some of them | |
1761 | here. */ | |
1762 | int i; | |
1763 | tree new_targs; | |
1764 | ||
1765 | new_targs = make_tree_vec (parm_depth); | |
1766 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) | |
1767 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
1768 | = TREE_VEC_ELT (targs, i); | |
1769 | targs = new_targs; | |
1770 | } | |
1771 | ||
1772 | return instantiate_template (tmpl, targs); | |
1773 | } | |
1774 | ||
1775 | /* If this is a specialization of a member template of a | |
1776 | template class. In we want to return the TEMPLATE_DECL, | |
1777 | not the specialization of it. */ | |
1778 | if (tsk == tsk_template) | |
1779 | { | |
1780 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
1781 | DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; | |
1782 | return tmpl; | |
1783 | } | |
1784 | ||
1785 | /* If we thought that the DECL was a member function, but it | |
1786 | turns out to be specializing a static member function, | |
1787 | make DECL a static member function as well. */ | |
1788 | if (DECL_STATIC_FUNCTION_P (tmpl) | |
1789 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1790 | { | |
1791 | revert_static_member_fn (decl); | |
1792 | last_function_parms = TREE_CHAIN (last_function_parms); | |
1793 | } | |
1794 | ||
1795 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ | |
1796 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); | |
1797 | ||
1798 | /* Inherit default function arguments from the template | |
1799 | DECL is specializing. */ | |
1800 | copy_default_args_to_explicit_spec (decl); | |
1801 | ||
1802 | /* This specialization has the same protection as the | |
1803 | template it specializes. */ | |
1804 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
1805 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
1806 | ||
1807 | if (is_friend && !have_def) | |
1808 | /* This is not really a declaration of a specialization. | |
1809 | It's just the name of an instantiation. But, it's not | |
1810 | a request for an instantiation, either. */ | |
1811 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
1812 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) | |
1813 | /* This is indeed a specialization. In case of constructors | |
1814 | and destructors, we need in-charge and not-in-charge | |
1815 | versions in V3 ABI. */ | |
1816 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
1817 | ||
1818 | /* Register this specialization so that we can find it | |
1819 | again. */ | |
1820 | decl = register_specialization (decl, gen_tmpl, targs); | |
1821 | } | |
1822 | } | |
1823 | ||
1824 | return decl; | |
1825 | } | |
1826 | ||
1827 | /* TYPE is being declared. Verify that the use of template headers | |
1828 | and such is reasonable. Issue error messages if not. */ | |
1829 | ||
1830 | void | |
1831 | maybe_check_template_type (type) | |
1832 | tree type; | |
1833 | { | |
1834 | if (template_header_count) | |
1835 | { | |
1836 | /* We are in the scope of some `template <...>' header. */ | |
1837 | ||
1838 | int context_depth | |
1839 | = template_class_depth_real (TYPE_CONTEXT (type), | |
1840 | /*count_specializations=*/1); | |
1841 | ||
1842 | if (template_header_count <= context_depth) | |
1843 | /* This is OK; the template headers are for the context. We | |
1844 | are actually too lenient here; like | |
1845 | check_explicit_specialization we should consider the number | |
1846 | of template types included in the actual declaration. For | |
1847 | example, | |
1848 | ||
1849 | template <class T> struct S { | |
1850 | template <class U> template <class V> | |
1851 | struct I {}; | |
1852 | }; | |
1853 | ||
1854 | is invalid, but: | |
1855 | ||
1856 | template <class T> struct S { | |
1857 | template <class U> struct I; | |
1858 | }; | |
1859 | ||
1860 | template <class T> template <class U. | |
1861 | struct S<T>::I {}; | |
1862 | ||
1863 | is not. */ | |
1864 | ; | |
1865 | else if (template_header_count > context_depth + 1) | |
1866 | /* There are two many template parameter lists. */ | |
1867 | error ("too many template parameter lists in declaration of `%T'", type); | |
1868 | } | |
1869 | } | |
1870 | ||
1871 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template | |
1872 | parameters. These are represented in the same format used for | |
1873 | DECL_TEMPLATE_PARMS. */ | |
1874 | ||
1875 | int comp_template_parms (parms1, parms2) | |
1876 | tree parms1; | |
1877 | tree parms2; | |
1878 | { | |
1879 | tree p1; | |
1880 | tree p2; | |
1881 | ||
1882 | if (parms1 == parms2) | |
1883 | return 1; | |
1884 | ||
1885 | for (p1 = parms1, p2 = parms2; | |
1886 | p1 != NULL_TREE && p2 != NULL_TREE; | |
1887 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
1888 | { | |
1889 | tree t1 = TREE_VALUE (p1); | |
1890 | tree t2 = TREE_VALUE (p2); | |
1891 | int i; | |
1892 | ||
1893 | my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0); | |
1894 | my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0); | |
1895 | ||
1896 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
1897 | return 0; | |
1898 | ||
1899 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
1900 | { | |
1901 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
1902 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
1903 | ||
1904 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
1905 | return 0; | |
1906 | ||
1907 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
1908 | continue; | |
1909 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) | |
1910 | return 0; | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
1915 | /* One set of parameters has more parameters lists than the | |
1916 | other. */ | |
1917 | return 0; | |
1918 | ||
1919 | return 1; | |
1920 | } | |
1921 | ||
1922 | /* Complain if DECL shadows a template parameter. | |
1923 | ||
1924 | [temp.local]: A template-parameter shall not be redeclared within its | |
1925 | scope (including nested scopes). */ | |
1926 | ||
1927 | void | |
1928 | check_template_shadow (decl) | |
1929 | tree decl; | |
1930 | { | |
1931 | tree olddecl; | |
1932 | ||
1933 | /* If we're not in a template, we can't possibly shadow a template | |
1934 | parameter. */ | |
1935 | if (!current_template_parms) | |
1936 | return; | |
1937 | ||
1938 | /* Figure out what we're shadowing. */ | |
1939 | if (TREE_CODE (decl) == OVERLOAD) | |
1940 | decl = OVL_CURRENT (decl); | |
1941 | olddecl = IDENTIFIER_VALUE (DECL_NAME (decl)); | |
1942 | ||
1943 | /* If there's no previous binding for this name, we're not shadowing | |
1944 | anything, let alone a template parameter. */ | |
1945 | if (!olddecl) | |
1946 | return; | |
1947 | ||
1948 | /* If we're not shadowing a template parameter, we're done. Note | |
1949 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
1950 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
1951 | node. */ | |
1952 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) | |
1953 | return; | |
1954 | ||
1955 | /* We check for decl != olddecl to avoid bogus errors for using a | |
1956 | name inside a class. We check TPFI to avoid duplicate errors for | |
1957 | inline member templates. */ | |
1958 | if (decl == olddecl | |
1959 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
1960 | return; | |
1961 | ||
1962 | cp_error_at ("declaration of `%#D'", decl); | |
1963 | cp_error_at (" shadows template parm `%#D'", olddecl); | |
1964 | } | |
1965 | ||
1966 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, | |
1967 | ORIG_LEVEL, DECL, and TYPE. */ | |
1968 | ||
1969 | static tree | |
1970 | build_template_parm_index (index, level, orig_level, decl, type) | |
1971 | int index; | |
1972 | int level; | |
1973 | int orig_level; | |
1974 | tree decl; | |
1975 | tree type; | |
1976 | { | |
1977 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
1978 | TEMPLATE_PARM_IDX (t) = index; | |
1979 | TEMPLATE_PARM_LEVEL (t) = level; | |
1980 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
1981 | TEMPLATE_PARM_DECL (t) = decl; | |
1982 | TREE_TYPE (t) = type; | |
1983 | ||
1984 | return t; | |
1985 | } | |
1986 | ||
1987 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose | |
1988 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a | |
1989 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a | |
1990 | new one is created. */ | |
1991 | ||
1992 | static tree | |
1993 | reduce_template_parm_level (index, type, levels) | |
1994 | tree index; | |
1995 | tree type; | |
1996 | int levels; | |
1997 | { | |
1998 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
1999 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
2000 | != TEMPLATE_PARM_LEVEL (index) - levels)) | |
2001 | { | |
2002 | tree decl | |
2003 | = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), | |
2004 | DECL_NAME (TEMPLATE_PARM_DECL (index)), | |
2005 | type); | |
2006 | tree t | |
2007 | = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
2008 | TEMPLATE_PARM_LEVEL (index) - levels, | |
2009 | TEMPLATE_PARM_ORIG_LEVEL (index), | |
2010 | decl, type); | |
2011 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
2012 | ||
2013 | DECL_ARTIFICIAL (decl) = 1; | |
2014 | SET_DECL_TEMPLATE_PARM_P (decl); | |
2015 | ||
2016 | /* Template template parameters need this. */ | |
2017 | DECL_TEMPLATE_PARMS (decl) | |
2018 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
2019 | } | |
2020 | ||
2021 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2022 | } | |
2023 | ||
2024 | /* Process information from new template parameter NEXT and append it to the | |
2025 | LIST being built. */ | |
2026 | ||
2027 | tree | |
2028 | process_template_parm (list, next) | |
2029 | tree list, next; | |
2030 | { | |
2031 | tree parm; | |
2032 | tree decl = 0; | |
2033 | tree defval; | |
2034 | int is_type, idx; | |
2035 | ||
2036 | parm = next; | |
2037 | my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259); | |
2038 | defval = TREE_PURPOSE (parm); | |
2039 | parm = TREE_VALUE (parm); | |
2040 | is_type = TREE_PURPOSE (parm) == class_type_node; | |
2041 | ||
2042 | if (list) | |
2043 | { | |
2044 | tree p = TREE_VALUE (tree_last (list)); | |
2045 | ||
2046 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
2047 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
2048 | else | |
2049 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
2050 | ++idx; | |
2051 | } | |
2052 | else | |
2053 | idx = 0; | |
2054 | ||
2055 | if (!is_type) | |
2056 | { | |
2057 | my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260); | |
2058 | /* is a const-param */ | |
2059 | parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), | |
2060 | PARM, 0, NULL); | |
2061 | SET_DECL_TEMPLATE_PARM_P (parm); | |
2062 | ||
2063 | /* [temp.param] | |
2064 | ||
2065 | The top-level cv-qualifiers on the template-parameter are | |
2066 | ignored when determining its type. */ | |
2067 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
2068 | ||
2069 | /* A template parameter is not modifiable. */ | |
2070 | TREE_READONLY (parm) = 1; | |
2071 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
2072 | TREE_TYPE (parm) = void_type_node; | |
2073 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); | |
2074 | DECL_INITIAL (parm) = DECL_INITIAL (decl) | |
2075 | = build_template_parm_index (idx, processing_template_decl, | |
2076 | processing_template_decl, | |
2077 | decl, TREE_TYPE (parm)); | |
2078 | } | |
2079 | else | |
2080 | { | |
2081 | tree t; | |
2082 | parm = TREE_VALUE (parm); | |
2083 | ||
2084 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
2085 | { | |
2086 | t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); | |
2087 | /* This is for distinguishing between real templates and template | |
2088 | template parameters */ | |
2089 | TREE_TYPE (parm) = t; | |
2090 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
2091 | decl = parm; | |
2092 | } | |
2093 | else | |
2094 | { | |
2095 | t = make_aggr_type (TEMPLATE_TYPE_PARM); | |
2096 | /* parm is either IDENTIFIER_NODE or NULL_TREE */ | |
2097 | decl = build_decl (TYPE_DECL, parm, t); | |
2098 | } | |
2099 | ||
2100 | TYPE_NAME (t) = decl; | |
2101 | TYPE_STUB_DECL (t) = decl; | |
2102 | parm = decl; | |
2103 | TEMPLATE_TYPE_PARM_INDEX (t) | |
2104 | = build_template_parm_index (idx, processing_template_decl, | |
2105 | processing_template_decl, | |
2106 | decl, TREE_TYPE (parm)); | |
2107 | } | |
2108 | DECL_ARTIFICIAL (decl) = 1; | |
2109 | SET_DECL_TEMPLATE_PARM_P (decl); | |
2110 | pushdecl (decl); | |
2111 | parm = build_tree_list (defval, parm); | |
2112 | return chainon (list, parm); | |
2113 | } | |
2114 | ||
2115 | /* The end of a template parameter list has been reached. Process the | |
2116 | tree list into a parameter vector, converting each parameter into a more | |
2117 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
2118 | as PARM_DECLs. */ | |
2119 | ||
2120 | tree | |
2121 | end_template_parm_list (parms) | |
2122 | tree parms; | |
2123 | { | |
2124 | int nparms; | |
2125 | tree parm, next; | |
2126 | tree saved_parmlist = make_tree_vec (list_length (parms)); | |
2127 | ||
2128 | current_template_parms | |
2129 | = tree_cons (size_int (processing_template_decl), | |
2130 | saved_parmlist, current_template_parms); | |
2131 | ||
2132 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) | |
2133 | { | |
2134 | next = TREE_CHAIN (parm); | |
2135 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
2136 | TREE_CHAIN (parm) = NULL_TREE; | |
2137 | } | |
2138 | ||
2139 | --processing_template_parmlist; | |
2140 | ||
2141 | return saved_parmlist; | |
2142 | } | |
2143 | ||
2144 | /* end_template_decl is called after a template declaration is seen. */ | |
2145 | ||
2146 | void | |
2147 | end_template_decl () | |
2148 | { | |
2149 | reset_specialization (); | |
2150 | ||
2151 | if (! processing_template_decl) | |
2152 | return; | |
2153 | ||
2154 | /* This matches the pushlevel in begin_template_parm_list. */ | |
2155 | finish_scope (); | |
2156 | ||
2157 | --processing_template_decl; | |
2158 | current_template_parms = TREE_CHAIN (current_template_parms); | |
2159 | } | |
2160 | ||
2161 | /* Given a template argument vector containing the template PARMS. | |
2162 | The innermost PARMS are given first. */ | |
2163 | ||
2164 | tree | |
2165 | current_template_args () | |
2166 | { | |
2167 | tree header; | |
2168 | tree args = NULL_TREE; | |
2169 | int length = TMPL_PARMS_DEPTH (current_template_parms); | |
2170 | int l = length; | |
2171 | ||
2172 | /* If there is only one level of template parameters, we do not | |
2173 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
2174 | TREE_VEC containing the arguments. */ | |
2175 | if (length > 1) | |
2176 | args = make_tree_vec (length); | |
2177 | ||
2178 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
2179 | { | |
2180 | tree a = copy_node (TREE_VALUE (header)); | |
2181 | int i; | |
2182 | ||
2183 | TREE_TYPE (a) = NULL_TREE; | |
2184 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) | |
2185 | { | |
2186 | tree t = TREE_VEC_ELT (a, i); | |
2187 | ||
2188 | /* T will be a list if we are called from within a | |
2189 | begin/end_template_parm_list pair, but a vector directly | |
2190 | if within a begin/end_member_template_processing pair. */ | |
2191 | if (TREE_CODE (t) == TREE_LIST) | |
2192 | { | |
2193 | t = TREE_VALUE (t); | |
2194 | ||
2195 | if (TREE_CODE (t) == TYPE_DECL | |
2196 | || TREE_CODE (t) == TEMPLATE_DECL) | |
2197 | t = TREE_TYPE (t); | |
2198 | else | |
2199 | t = DECL_INITIAL (t); | |
2200 | TREE_VEC_ELT (a, i) = t; | |
2201 | } | |
2202 | } | |
2203 | ||
2204 | if (length > 1) | |
2205 | TREE_VEC_ELT (args, --l) = a; | |
2206 | else | |
2207 | args = a; | |
2208 | } | |
2209 | ||
2210 | return args; | |
2211 | } | |
2212 | ||
2213 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated | |
2214 | template PARMS. Used by push_template_decl below. */ | |
2215 | ||
2216 | static tree | |
2217 | build_template_decl (decl, parms) | |
2218 | tree decl; | |
2219 | tree parms; | |
2220 | { | |
2221 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
2222 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
2223 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
2224 | if (DECL_LANG_SPECIFIC (decl)) | |
2225 | { | |
2226 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); | |
2227 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
2228 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); | |
2229 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); | |
2230 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); | |
2231 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
2232 | SET_OVERLOADED_OPERATOR_CODE (tmpl, | |
2233 | DECL_OVERLOADED_OPERATOR_P (decl)); | |
2234 | } | |
2235 | ||
2236 | return tmpl; | |
2237 | } | |
2238 | ||
2239 | struct template_parm_data | |
2240 | { | |
2241 | /* The level of the template parameters we are currently | |
2242 | processing. */ | |
2243 | int level; | |
2244 | ||
2245 | /* The index of the specialization argument we are currently | |
2246 | processing. */ | |
2247 | int current_arg; | |
2248 | ||
2249 | /* An array whose size is the number of template parameters. The | |
2250 | elements are nonzero if the parameter has been used in any one | |
2251 | of the arguments processed so far. */ | |
2252 | int* parms; | |
2253 | ||
2254 | /* An array whose size is the number of template arguments. The | |
2255 | elements are nonzero if the argument makes use of template | |
2256 | parameters of this level. */ | |
2257 | int* arg_uses_template_parms; | |
2258 | }; | |
2259 | ||
2260 | /* Subroutine of push_template_decl used to see if each template | |
2261 | parameter in a partial specialization is used in the explicit | |
2262 | argument list. If T is of the LEVEL given in DATA (which is | |
2263 | treated as a template_parm_data*), then DATA->PARMS is marked | |
2264 | appropriately. */ | |
2265 | ||
2266 | static int | |
2267 | mark_template_parm (t, data) | |
2268 | tree t; | |
2269 | void* data; | |
2270 | { | |
2271 | int level; | |
2272 | int idx; | |
2273 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
2274 | ||
2275 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2276 | { | |
2277 | level = TEMPLATE_PARM_LEVEL (t); | |
2278 | idx = TEMPLATE_PARM_IDX (t); | |
2279 | } | |
2280 | else | |
2281 | { | |
2282 | level = TEMPLATE_TYPE_LEVEL (t); | |
2283 | idx = TEMPLATE_TYPE_IDX (t); | |
2284 | } | |
2285 | ||
2286 | if (level == tpd->level) | |
2287 | { | |
2288 | tpd->parms[idx] = 1; | |
2289 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
2290 | } | |
2291 | ||
2292 | /* Return zero so that for_each_template_parm will continue the | |
2293 | traversal of the tree; we want to mark *every* template parm. */ | |
2294 | return 0; | |
2295 | } | |
2296 | ||
2297 | /* Process the partial specialization DECL. */ | |
2298 | ||
2299 | static tree | |
2300 | process_partial_specialization (decl) | |
2301 | tree decl; | |
2302 | { | |
2303 | tree type = TREE_TYPE (decl); | |
2304 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
2305 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
2306 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); | |
2307 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); | |
2308 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
2309 | int nargs = TREE_VEC_LENGTH (inner_args); | |
2310 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2311 | int i; | |
2312 | int did_error_intro = 0; | |
2313 | struct template_parm_data tpd; | |
2314 | struct template_parm_data tpd2; | |
2315 | ||
2316 | /* We check that each of the template parameters given in the | |
2317 | partial specialization is used in the argument list to the | |
2318 | specialization. For example: | |
2319 | ||
2320 | template <class T> struct S; | |
2321 | template <class T> struct S<T*>; | |
2322 | ||
2323 | The second declaration is OK because `T*' uses the template | |
2324 | parameter T, whereas | |
2325 | ||
2326 | template <class T> struct S<int>; | |
2327 | ||
2328 | is no good. Even trickier is: | |
2329 | ||
2330 | template <class T> | |
2331 | struct S1 | |
2332 | { | |
2333 | template <class U> | |
2334 | struct S2; | |
2335 | template <class U> | |
2336 | struct S2<T>; | |
2337 | }; | |
2338 | ||
2339 | The S2<T> declaration is actually invalid; it is a | |
2340 | full-specialization. Of course, | |
2341 | ||
2342 | template <class U> | |
2343 | struct S2<T (*)(U)>; | |
2344 | ||
2345 | or some such would have been OK. */ | |
2346 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2347 | tpd.parms = alloca (sizeof (int) * ntparms); | |
2348 | memset ((PTR) tpd.parms, 0, sizeof (int) * ntparms); | |
2349 | ||
2350 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
2351 | memset ((PTR) tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); | |
2352 | for (i = 0; i < nargs; ++i) | |
2353 | { | |
2354 | tpd.current_arg = i; | |
2355 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2356 | &mark_template_parm, | |
2357 | &tpd, | |
2358 | NULL); | |
2359 | } | |
2360 | for (i = 0; i < ntparms; ++i) | |
2361 | if (tpd.parms[i] == 0) | |
2362 | { | |
2363 | /* One of the template parms was not used in the | |
2364 | specialization. */ | |
2365 | if (!did_error_intro) | |
2366 | { | |
2367 | error ("template parameters not used in partial specialization:"); | |
2368 | did_error_intro = 1; | |
2369 | } | |
2370 | ||
2371 | error (" `%D'", | |
2372 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); | |
2373 | } | |
2374 | ||
2375 | /* [temp.class.spec] | |
2376 | ||
2377 | The argument list of the specialization shall not be identical to | |
2378 | the implicit argument list of the primary template. */ | |
2379 | if (comp_template_args | |
2380 | (inner_args, | |
2381 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2382 | (maintmpl))))) | |
2383 | error ("partial specialization `%T' does not specialize any template arguments", type); | |
2384 | ||
2385 | /* [temp.class.spec] | |
2386 | ||
2387 | A partially specialized non-type argument expression shall not | |
2388 | involve template parameters of the partial specialization except | |
2389 | when the argument expression is a simple identifier. | |
2390 | ||
2391 | The type of a template parameter corresponding to a specialized | |
2392 | non-type argument shall not be dependent on a parameter of the | |
2393 | specialization. */ | |
2394 | my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0); | |
2395 | tpd2.parms = 0; | |
2396 | for (i = 0; i < nargs; ++i) | |
2397 | { | |
2398 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2399 | if (/* These first two lines are the `non-type' bit. */ | |
2400 | !TYPE_P (arg) | |
2401 | && TREE_CODE (arg) != TEMPLATE_DECL | |
2402 | /* This next line is the `argument expression is not just a | |
2403 | simple identifier' condition and also the `specialized | |
2404 | non-type argument' bit. */ | |
2405 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2406 | { | |
2407 | if (tpd.arg_uses_template_parms[i]) | |
2408 | error ("template argument `%E' involves template parameter(s)", arg); | |
2409 | else | |
2410 | { | |
2411 | /* Look at the corresponding template parameter, | |
2412 | marking which template parameters its type depends | |
2413 | upon. */ | |
2414 | tree type = | |
2415 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2416 | i))); | |
2417 | ||
2418 | if (!tpd2.parms) | |
2419 | { | |
2420 | /* We haven't yet initialized TPD2. Do so now. */ | |
2421 | tpd2.arg_uses_template_parms | |
2422 | = (int*) alloca (sizeof (int) * nargs); | |
2423 | /* The number of parameters here is the number in the | |
2424 | main template, which, as checked in the assertion | |
2425 | above, is NARGS. */ | |
2426 | tpd2.parms = (int*) alloca (sizeof (int) * nargs); | |
2427 | tpd2.level = | |
2428 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2429 | } | |
2430 | ||
2431 | /* Mark the template parameters. But this time, we're | |
2432 | looking for the template parameters of the main | |
2433 | template, not in the specialization. */ | |
2434 | tpd2.current_arg = i; | |
2435 | tpd2.arg_uses_template_parms[i] = 0; | |
2436 | memset ((PTR) tpd2.parms, 0, sizeof (int) * nargs); | |
2437 | for_each_template_parm (type, | |
2438 | &mark_template_parm, | |
2439 | &tpd2, | |
2440 | NULL); | |
2441 | ||
2442 | if (tpd2.arg_uses_template_parms [i]) | |
2443 | { | |
2444 | /* The type depended on some template parameters. | |
2445 | If they are fully specialized in the | |
2446 | specialization, that's OK. */ | |
2447 | int j; | |
2448 | for (j = 0; j < nargs; ++j) | |
2449 | if (tpd2.parms[j] != 0 | |
2450 | && tpd.arg_uses_template_parms [j]) | |
2451 | { | |
2452 | error ("type `%T' of template argument `%E' depends on template parameter(s)", | |
2453 | type, | |
2454 | arg); | |
2455 | break; | |
2456 | } | |
2457 | } | |
2458 | } | |
2459 | } | |
2460 | } | |
2461 | ||
2462 | if (retrieve_specialization (maintmpl, specargs)) | |
2463 | /* We've already got this specialization. */ | |
2464 | return decl; | |
2465 | ||
2466 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) | |
2467 | = tree_cons (inner_args, inner_parms, | |
2468 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
2469 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; | |
2470 | return decl; | |
2471 | } | |
2472 | ||
2473 | /* Check that a template declaration's use of default arguments is not | |
2474 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
2475 | nonzero if DECL is the thing declared by a primary template. | |
2476 | IS_PARTIAL is nonzero if DECL is a partial specialization. */ | |
2477 | ||
2478 | static void | |
2479 | check_default_tmpl_args (decl, parms, is_primary, is_partial) | |
2480 | tree decl; | |
2481 | tree parms; | |
2482 | int is_primary; | |
2483 | int is_partial; | |
2484 | { | |
2485 | const char *msg; | |
2486 | int last_level_to_check; | |
2487 | tree parm_level; | |
2488 | ||
2489 | /* [temp.param] | |
2490 | ||
2491 | A default template-argument shall not be specified in a | |
2492 | function template declaration or a function template definition, nor | |
2493 | in the template-parameter-list of the definition of a member of a | |
2494 | class template. */ | |
2495 | ||
2496 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) | |
2497 | /* You can't have a function template declaration in a local | |
2498 | scope, nor you can you define a member of a class template in a | |
2499 | local scope. */ | |
2500 | return; | |
2501 | ||
2502 | if (current_class_type | |
2503 | && !TYPE_BEING_DEFINED (current_class_type) | |
2504 | && DECL_LANG_SPECIFIC (decl) | |
2505 | /* If this is either a friend defined in the scope of the class | |
2506 | or a member function. */ | |
2507 | && ((DECL_CONTEXT (decl) | |
2508 | && same_type_p (DECL_CONTEXT (decl), current_class_type)) | |
2509 | || (DECL_FRIEND_CONTEXT (decl) | |
2510 | && same_type_p (DECL_FRIEND_CONTEXT (decl), | |
2511 | current_class_type))) | |
2512 | /* And, if it was a member function, it really was defined in | |
2513 | the scope of the class. */ | |
2514 | && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
2515 | /* We already checked these parameters when the template was | |
2516 | declared, so there's no need to do it again now. This function | |
2517 | was defined in class scope, but we're processing it's body now | |
2518 | that the class is complete. */ | |
2519 | return; | |
2520 | ||
2521 | /* [temp.param] | |
2522 | ||
2523 | If a template-parameter has a default template-argument, all | |
2524 | subsequent template-parameters shall have a default | |
2525 | template-argument supplied. */ | |
2526 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
2527 | { | |
2528 | tree inner_parms = TREE_VALUE (parm_level); | |
2529 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2530 | int seen_def_arg_p = 0; | |
2531 | int i; | |
2532 | ||
2533 | for (i = 0; i < ntparms; ++i) | |
2534 | { | |
2535 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
2536 | if (TREE_PURPOSE (parm)) | |
2537 | seen_def_arg_p = 1; | |
2538 | else if (seen_def_arg_p) | |
2539 | { | |
2540 | error ("no default argument for `%D'", TREE_VALUE (parm)); | |
2541 | /* For better subsequent error-recovery, we indicate that | |
2542 | there should have been a default argument. */ | |
2543 | TREE_PURPOSE (parm) = error_mark_node; | |
2544 | } | |
2545 | } | |
2546 | } | |
2547 | ||
2548 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) | |
2549 | /* For an ordinary class template, default template arguments are | |
2550 | allowed at the innermost level, e.g.: | |
2551 | template <class T = int> | |
2552 | struct S {}; | |
2553 | but, in a partial specialization, they're not allowed even | |
2554 | there, as we have in [temp.class.spec]: | |
2555 | ||
2556 | The template parameter list of a specialization shall not | |
2557 | contain default template argument values. | |
2558 | ||
2559 | So, for a partial specialization, or for a function template, | |
2560 | we look at all of them. */ | |
2561 | ; | |
2562 | else | |
2563 | /* But, for a primary class template that is not a partial | |
2564 | specialization we look at all template parameters except the | |
2565 | innermost ones. */ | |
2566 | parms = TREE_CHAIN (parms); | |
2567 | ||
2568 | /* Figure out what error message to issue. */ | |
2569 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2570 | msg = "default template arguments may not be used in function templates"; | |
2571 | else if (is_partial) | |
2572 | msg = "default template arguments may not be used in partial specializations"; | |
2573 | else | |
2574 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2575 | ||
2576 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2577 | /* If we're inside a class definition, there's no need to | |
2578 | examine the parameters to the class itself. On the one | |
2579 | hand, they will be checked when the class is defined, and, | |
2580 | on the other, default arguments are valid in things like: | |
2581 | template <class T = double> | |
2582 | struct S { template <class U> void f(U); }; | |
2583 | Here the default argument for `S' has no bearing on the | |
2584 | declaration of `f'. */ | |
2585 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2586 | else | |
2587 | /* Check everything. */ | |
2588 | last_level_to_check = 0; | |
2589 | ||
2590 | for (parm_level = parms; | |
2591 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
2592 | parm_level = TREE_CHAIN (parm_level)) | |
2593 | { | |
2594 | tree inner_parms = TREE_VALUE (parm_level); | |
2595 | int i; | |
2596 | int ntparms; | |
2597 | ||
2598 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2599 | for (i = 0; i < ntparms; ++i) | |
2600 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2601 | { | |
2602 | if (msg) | |
2603 | { | |
2604 | error (msg, decl); | |
2605 | msg = 0; | |
2606 | } | |
2607 | ||
2608 | /* Clear out the default argument so that we are not | |
2609 | confused later. */ | |
2610 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2611 | } | |
2612 | ||
2613 | /* At this point, if we're still interested in issuing messages, | |
2614 | they must apply to classes surrounding the object declared. */ | |
2615 | if (msg) | |
2616 | msg = "default argument for template parameter for class enclosing `%D'"; | |
2617 | } | |
2618 | } | |
2619 | ||
2620 | /* Worker for push_template_decl_real, called via | |
2621 | for_each_template_parm. DATA is really an int, indicating the | |
2622 | level of the parameters we are interested in. If T is a template | |
2623 | parameter of that level, return nonzero. */ | |
2624 | ||
2625 | static int | |
2626 | template_parm_this_level_p (t, data) | |
2627 | tree t; | |
2628 | void *data; | |
2629 | { | |
2630 | int this_level = *(int *)data; | |
2631 | int level; | |
2632 | ||
2633 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2634 | level = TEMPLATE_PARM_LEVEL (t); | |
2635 | else | |
2636 | level = TEMPLATE_TYPE_LEVEL (t); | |
2637 | return level == this_level; | |
2638 | } | |
2639 | ||
2640 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template | |
2641 | parameters given by current_template_args, or reuses a | |
2642 | previously existing one, if appropriate. Returns the DECL, or an | |
2643 | equivalent one, if it is replaced via a call to duplicate_decls. | |
2644 | ||
2645 | If IS_FRIEND is nonzero, DECL is a friend declaration. */ | |
2646 | ||
2647 | tree | |
2648 | push_template_decl_real (decl, is_friend) | |
2649 | tree decl; | |
2650 | int is_friend; | |
2651 | { | |
2652 | tree tmpl; | |
2653 | tree args; | |
2654 | tree info; | |
2655 | tree ctx; | |
2656 | int primary; | |
2657 | int is_partial; | |
2658 | int new_template_p = 0; | |
2659 | ||
2660 | /* See if this is a partial specialization. */ | |
2661 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) | |
2662 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE | |
2663 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); | |
2664 | ||
2665 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
2666 | ||
2667 | if (is_friend) | |
2668 | /* For a friend, we want the context of the friend function, not | |
2669 | the type of which it is a friend. */ | |
2670 | ctx = DECL_CONTEXT (decl); | |
2671 | else if (CP_DECL_CONTEXT (decl) | |
2672 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
2673 | /* In the case of a virtual function, we want the class in which | |
2674 | it is defined. */ | |
2675 | ctx = CP_DECL_CONTEXT (decl); | |
2676 | else | |
2677 | /* Otherwise, if we're currently defining some class, the DECL | |
2678 | is assumed to be a member of the class. */ | |
2679 | ctx = current_scope (); | |
2680 | ||
2681 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) | |
2682 | ctx = NULL_TREE; | |
2683 | ||
2684 | if (!DECL_CONTEXT (decl)) | |
2685 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); | |
2686 | ||
2687 | /* See if this is a primary template. */ | |
2688 | primary = template_parm_scope_p (); | |
2689 | ||
2690 | if (primary) | |
2691 | { | |
2692 | if (current_lang_name == lang_name_c) | |
2693 | error ("template with C linkage"); | |
2694 | else if (TREE_CODE (decl) == TYPE_DECL | |
2695 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
2696 | error ("template class without a name"); | |
2697 | else if ((DECL_IMPLICIT_TYPEDEF_P (decl) | |
2698 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2699 | || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)) | |
2700 | || TREE_CODE (decl) == FUNCTION_DECL) | |
2701 | /* OK */; | |
2702 | else | |
2703 | error ("template declaration of `%#D'", decl); | |
2704 | } | |
2705 | ||
2706 | /* Check to see that the rules regarding the use of default | |
2707 | arguments are not being violated. */ | |
2708 | check_default_tmpl_args (decl, current_template_parms, | |
2709 | primary, is_partial); | |
2710 | ||
2711 | if (is_partial) | |
2712 | return process_partial_specialization (decl); | |
2713 | ||
2714 | args = current_template_args (); | |
2715 | ||
2716 | if (!ctx | |
2717 | || TREE_CODE (ctx) == FUNCTION_DECL | |
2718 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) | |
2719 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) | |
2720 | { | |
2721 | if (DECL_LANG_SPECIFIC (decl) | |
2722 | && DECL_TEMPLATE_INFO (decl) | |
2723 | && DECL_TI_TEMPLATE (decl)) | |
2724 | tmpl = DECL_TI_TEMPLATE (decl); | |
2725 | /* If DECL is a TYPE_DECL for a class-template, then there won't | |
2726 | be DECL_LANG_SPECIFIC. The information equivalent to | |
2727 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
2728 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) | |
2729 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2730 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2731 | { | |
2732 | /* Since a template declaration already existed for this | |
2733 | class-type, we must be redeclaring it here. Make sure | |
2734 | that the redeclaration is valid. */ | |
2735 | redeclare_class_template (TREE_TYPE (decl), | |
2736 | current_template_parms); | |
2737 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
2738 | one we already had. */ | |
2739 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
2740 | } | |
2741 | else | |
2742 | { | |
2743 | tmpl = build_template_decl (decl, current_template_parms); | |
2744 | new_template_p = 1; | |
2745 | ||
2746 | if (DECL_LANG_SPECIFIC (decl) | |
2747 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2748 | { | |
2749 | /* A specialization of a member template of a template | |
2750 | class. */ | |
2751 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
2752 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
2753 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
2754 | } | |
2755 | } | |
2756 | } | |
2757 | else | |
2758 | { | |
2759 | tree a, t, current, parms; | |
2760 | int i; | |
2761 | ||
2762 | if (TREE_CODE (decl) == TYPE_DECL) | |
2763 | { | |
2764 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) | |
2765 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
2766 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
2767 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
2768 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
2769 | else | |
2770 | { | |
2771 | error ("`%D' does not declare a template type", decl); | |
2772 | return decl; | |
2773 | } | |
2774 | } | |
2775 | else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl)) | |
2776 | { | |
2777 | error ("template definition of non-template `%#D'", decl); | |
2778 | return decl; | |
2779 | } | |
2780 | else | |
2781 | tmpl = DECL_TI_TEMPLATE (decl); | |
2782 | ||
2783 | if (is_member_template (tmpl) | |
2784 | && DECL_FUNCTION_TEMPLATE_P (tmpl) | |
2785 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) | |
2786 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
2787 | { | |
2788 | tree new_tmpl; | |
2789 | ||
2790 | /* The declaration is a specialization of a member | |
2791 | template, declared outside the class. Therefore, the | |
2792 | innermost template arguments will be NULL, so we | |
2793 | replace them with the arguments determined by the | |
2794 | earlier call to check_explicit_specialization. */ | |
2795 | args = DECL_TI_ARGS (decl); | |
2796 | ||
2797 | new_tmpl | |
2798 | = build_template_decl (decl, current_template_parms); | |
2799 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; | |
2800 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
2801 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
2802 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
2803 | DECL_TEMPLATE_INFO (new_tmpl) | |
2804 | = tree_cons (tmpl, args, NULL_TREE); | |
2805 | ||
2806 | register_specialization (new_tmpl, | |
2807 | most_general_template (tmpl), | |
2808 | args); | |
2809 | return decl; | |
2810 | } | |
2811 | ||
2812 | /* Make sure the template headers we got make sense. */ | |
2813 | ||
2814 | parms = DECL_TEMPLATE_PARMS (tmpl); | |
2815 | i = TMPL_PARMS_DEPTH (parms); | |
2816 | if (TMPL_ARGS_DEPTH (args) != i) | |
2817 | { | |
2818 | error ("expected %d levels of template parms for `%#D', got %d", | |
2819 | i, decl, TMPL_ARGS_DEPTH (args)); | |
2820 | } | |
2821 | else | |
2822 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
2823 | { | |
2824 | a = TMPL_ARGS_LEVEL (args, i); | |
2825 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
2826 | ||
2827 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
2828 | { | |
2829 | if (current == decl) | |
2830 | error ("got %d template parameters for `%#D'", | |
2831 | TREE_VEC_LENGTH (a), decl); | |
2832 | else | |
2833 | error ("got %d template parameters for `%#T'", | |
2834 | TREE_VEC_LENGTH (a), current); | |
2835 | error (" but %d required", TREE_VEC_LENGTH (t)); | |
2836 | } | |
2837 | ||
2838 | /* Perhaps we should also check that the parms are used in the | |
2839 | appropriate qualifying scopes in the declarator? */ | |
2840 | ||
2841 | if (current == decl) | |
2842 | current = ctx; | |
2843 | else | |
2844 | current = TYPE_CONTEXT (current); | |
2845 | } | |
2846 | } | |
2847 | ||
2848 | DECL_TEMPLATE_RESULT (tmpl) = decl; | |
2849 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
2850 | ||
2851 | /* Push template declarations for global functions and types. Note | |
2852 | that we do not try to push a global template friend declared in a | |
2853 | template class; such a thing may well depend on the template | |
2854 | parameters of the class. */ | |
2855 | if (new_template_p && !ctx | |
2856 | && !(is_friend && template_class_depth (current_class_type) > 0)) | |
2857 | tmpl = pushdecl_namespace_level (tmpl); | |
2858 | ||
2859 | if (primary) | |
2860 | { | |
2861 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; | |
2862 | if (DECL_CONV_FN_P (tmpl)) | |
2863 | { | |
2864 | int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2865 | ||
2866 | /* It is a conversion operator. See if the type converted to | |
2867 | depends on innermost template operands. */ | |
2868 | ||
2869 | if (for_each_template_parm (TREE_TYPE (TREE_TYPE (tmpl)), | |
2870 | template_parm_this_level_p, | |
2871 | &depth, | |
2872 | NULL)) | |
2873 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; | |
2874 | } | |
2875 | } | |
2876 | ||
2877 | info = tree_cons (tmpl, args, NULL_TREE); | |
2878 | ||
2879 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) | |
2880 | { | |
2881 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); | |
2882 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
2883 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE | |
2884 | /* Don't change the name if we've already set it up. */ | |
2885 | && !IDENTIFIER_TEMPLATE (DECL_NAME (decl))) | |
2886 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); | |
2887 | } | |
2888 | else if (DECL_LANG_SPECIFIC (decl)) | |
2889 | DECL_TEMPLATE_INFO (decl) = info; | |
2890 | ||
2891 | return DECL_TEMPLATE_RESULT (tmpl); | |
2892 | } | |
2893 | ||
2894 | tree | |
2895 | push_template_decl (decl) | |
2896 | tree decl; | |
2897 | { | |
2898 | return push_template_decl_real (decl, 0); | |
2899 | } | |
2900 | ||
2901 | /* Called when a class template TYPE is redeclared with the indicated | |
2902 | template PARMS, e.g.: | |
2903 | ||
2904 | template <class T> struct S; | |
2905 | template <class T> struct S {}; */ | |
2906 | ||
2907 | void | |
2908 | redeclare_class_template (type, parms) | |
2909 | tree type; | |
2910 | tree parms; | |
2911 | { | |
2912 | tree tmpl; | |
2913 | tree tmpl_parms; | |
2914 | int i; | |
2915 | ||
2916 | if (!TYPE_TEMPLATE_INFO (type)) | |
2917 | { | |
2918 | error ("`%T' is not a template type", type); | |
2919 | return; | |
2920 | } | |
2921 | ||
2922 | tmpl = TYPE_TI_TEMPLATE (type); | |
2923 | if (!PRIMARY_TEMPLATE_P (tmpl)) | |
2924 | /* The type is nested in some template class. Nothing to worry | |
2925 | about here; there are no new template parameters for the nested | |
2926 | type. */ | |
2927 | return; | |
2928 | ||
2929 | parms = INNERMOST_TEMPLATE_PARMS (parms); | |
2930 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
2931 | ||
2932 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) | |
2933 | { | |
2934 | cp_error_at ("previous declaration `%D'", tmpl); | |
2935 | error ("used %d template parameter%s instead of %d", | |
2936 | TREE_VEC_LENGTH (tmpl_parms), | |
2937 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
2938 | TREE_VEC_LENGTH (parms)); | |
2939 | return; | |
2940 | } | |
2941 | ||
2942 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
2943 | { | |
2944 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
2945 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
2946 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
2947 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
2948 | ||
2949 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)) | |
2950 | { | |
2951 | cp_error_at ("template parameter `%#D'", tmpl_parm); | |
2952 | error ("redeclared here as `%#D'", parm); | |
2953 | return; | |
2954 | } | |
2955 | ||
2956 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
2957 | { | |
2958 | /* We have in [temp.param]: | |
2959 | ||
2960 | A template-parameter may not be given default arguments | |
2961 | by two different declarations in the same scope. */ | |
2962 | error ("redefinition of default argument for `%#D'", parm); | |
2963 | cp_error_at (" original definition appeared here", tmpl_parm); | |
2964 | return; | |
2965 | } | |
2966 | ||
2967 | if (parm_default != NULL_TREE) | |
2968 | /* Update the previous template parameters (which are the ones | |
2969 | that will really count) with the new default value. */ | |
2970 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
2971 | else if (tmpl_default != NULL_TREE) | |
2972 | /* Update the new parameters, too; they'll be used as the | |
2973 | parameters for any members. */ | |
2974 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
2975 | } | |
2976 | } | |
2977 | ||
2978 | /* Attempt to convert the non-type template parameter EXPR to the | |
2979 | indicated TYPE. If the conversion is successful, return the | |
2980 | converted value. If the conversion is unsuccessful, return | |
2981 | NULL_TREE if we issued an error message, or error_mark_node if we | |
2982 | did not. We issue error messages for out-and-out bad template | |
2983 | parameters, but not simply because the conversion failed, since we | |
2984 | might be just trying to do argument deduction. By the time this | |
2985 | function is called, neither TYPE nor EXPR may make use of template | |
2986 | parameters. */ | |
2987 | ||
2988 | static tree | |
2989 | convert_nontype_argument (type, expr) | |
2990 | tree type; | |
2991 | tree expr; | |
2992 | { | |
2993 | tree expr_type = TREE_TYPE (expr); | |
2994 | ||
2995 | /* A template-argument for a non-type, non-template | |
2996 | template-parameter shall be one of: | |
2997 | ||
2998 | --an integral constant-expression of integral or enumeration | |
2999 | type; or | |
3000 | ||
3001 | --the name of a non-type template-parameter; or | |
3002 | ||
3003 | --the name of an object or function with external linkage, | |
3004 | including function templates and function template-ids but | |
3005 | excluding non-static class members, expressed as id-expression; | |
3006 | or | |
3007 | ||
3008 | --the address of an object or function with external linkage, | |
3009 | including function templates and function template-ids but | |
3010 | excluding non-static class members, expressed as & id-expression | |
3011 | where the & is optional if the name refers to a function or | |
3012 | array; or | |
3013 | ||
3014 | --a pointer to member expressed as described in _expr.unary.op_. */ | |
3015 | ||
3016 | /* An integral constant-expression can include const variables or | |
3017 | enumerators. Simplify things by folding them to their values, | |
3018 | unless we're about to bind the declaration to a reference | |
3019 | parameter. */ | |
3020 | if (INTEGRAL_TYPE_P (expr_type) | |
3021 | && TREE_CODE (type) != REFERENCE_TYPE) | |
3022 | expr = decl_constant_value (expr); | |
3023 | ||
3024 | if (is_overloaded_fn (expr)) | |
3025 | /* OK for now. We'll check that it has external linkage later. | |
3026 | Check this first since if expr_type is the unknown_type_node | |
3027 | we would otherwise complain below. */ | |
3028 | ; | |
3029 | else if (TYPE_PTRMEM_P (expr_type) | |
3030 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
3031 | { | |
3032 | if (TREE_CODE (expr) != PTRMEM_CST) | |
3033 | goto bad_argument; | |
3034 | } | |
3035 | else if (TYPE_PTR_P (expr_type) | |
3036 | || TYPE_PTRMEM_P (expr_type) | |
3037 | || TREE_CODE (expr_type) == ARRAY_TYPE | |
3038 | || TREE_CODE (type) == REFERENCE_TYPE | |
3039 | /* If expr is the address of an overloaded function, we | |
3040 | will get the unknown_type_node at this point. */ | |
3041 | || expr_type == unknown_type_node) | |
3042 | { | |
3043 | tree referent; | |
3044 | tree e = expr; | |
3045 | STRIP_NOPS (e); | |
3046 | ||
3047 | if (TREE_CODE (expr_type) == ARRAY_TYPE | |
3048 | || (TREE_CODE (type) == REFERENCE_TYPE | |
3049 | && TREE_CODE (e) != ADDR_EXPR)) | |
3050 | referent = e; | |
3051 | else | |
3052 | { | |
3053 | if (TREE_CODE (e) != ADDR_EXPR) | |
3054 | { | |
3055 | bad_argument: | |
3056 | error ("`%E' is not a valid template argument", expr); | |
3057 | if (TYPE_PTR_P (expr_type)) | |
3058 | { | |
3059 | if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) | |
3060 | error ("it must be the address of a function with external linkage"); | |
3061 | else | |
3062 | error ("it must be the address of an object with external linkage"); | |
3063 | } | |
3064 | else if (TYPE_PTRMEM_P (expr_type) | |
3065 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
3066 | error ("it must be a pointer-to-member of the form `&X::Y'"); | |
3067 | ||
3068 | return NULL_TREE; | |
3069 | } | |
3070 | ||
3071 | referent = TREE_OPERAND (e, 0); | |
3072 | STRIP_NOPS (referent); | |
3073 | } | |
3074 | ||
3075 | if (TREE_CODE (referent) == STRING_CST) | |
3076 | { | |
3077 | error ("string literal %E is not a valid template argument because it is the address of an object with static linkage", | |
3078 | referent); | |
3079 | return NULL_TREE; | |
3080 | } | |
3081 | ||
3082 | if (is_overloaded_fn (referent)) | |
3083 | /* We'll check that it has external linkage later. */ | |
3084 | ; | |
3085 | else if (TREE_CODE (referent) != VAR_DECL) | |
3086 | goto bad_argument; | |
3087 | else if (!DECL_EXTERNAL_LINKAGE_P (referent)) | |
3088 | { | |
3089 | error ("address of non-extern `%E' cannot be used as template argument", referent); | |
3090 | return error_mark_node; | |
3091 | } | |
3092 | } | |
3093 | else if (INTEGRAL_TYPE_P (expr_type) | |
3094 | || TYPE_PTRMEM_P (expr_type) | |
3095 | || TYPE_PTRMEMFUNC_P (expr_type)) | |
3096 | { | |
3097 | if (! TREE_CONSTANT (expr)) | |
3098 | { | |
3099 | non_constant: | |
3100 | error ("non-constant `%E' cannot be used as template argument", | |
3101 | expr); | |
3102 | return NULL_TREE; | |
3103 | } | |
3104 | } | |
3105 | else | |
3106 | { | |
3107 | error ("object `%E' cannot be used as template argument", expr); | |
3108 | return NULL_TREE; | |
3109 | } | |
3110 | ||
3111 | switch (TREE_CODE (type)) | |
3112 | { | |
3113 | case INTEGER_TYPE: | |
3114 | case BOOLEAN_TYPE: | |
3115 | case ENUMERAL_TYPE: | |
3116 | /* For a non-type template-parameter of integral or enumeration | |
3117 | type, integral promotions (_conv.prom_) and integral | |
3118 | conversions (_conv.integral_) are applied. */ | |
3119 | if (!INTEGRAL_TYPE_P (expr_type)) | |
3120 | return error_mark_node; | |
3121 | ||
3122 | /* It's safe to call digest_init in this case; we know we're | |
3123 | just converting one integral constant expression to another. */ | |
3124 | expr = digest_init (type, expr, (tree*) 0); | |
3125 | ||
3126 | if (TREE_CODE (expr) != INTEGER_CST) | |
3127 | /* Curiously, some TREE_CONSTANT integral expressions do not | |
3128 | simplify to integer constants. For example, `3 % 0', | |
3129 | remains a TRUNC_MOD_EXPR. */ | |
3130 | goto non_constant; | |
3131 | ||
3132 | return expr; | |
3133 | ||
3134 | case POINTER_TYPE: | |
3135 | { | |
3136 | tree type_pointed_to = TREE_TYPE (type); | |
3137 | ||
3138 | if (TYPE_PTRMEM_P (type)) | |
3139 | { | |
3140 | tree e; | |
3141 | ||
3142 | /* For a non-type template-parameter of type pointer to data | |
3143 | member, qualification conversions (_conv.qual_) are | |
3144 | applied. */ | |
3145 | e = perform_qualification_conversions (type, expr); | |
3146 | if (TREE_CODE (e) == NOP_EXPR) | |
3147 | /* The call to perform_qualification_conversions will | |
3148 | insert a NOP_EXPR over EXPR to do express conversion, | |
3149 | if necessary. But, that will confuse us if we use | |
3150 | this (converted) template parameter to instantiate | |
3151 | another template; then the thing will not look like a | |
3152 | valid template argument. So, just make a new | |
3153 | constant, of the appropriate type. */ | |
3154 | e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); | |
3155 | return e; | |
3156 | } | |
3157 | else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE) | |
3158 | { | |
3159 | /* For a non-type template-parameter of type pointer to | |
3160 | function, only the function-to-pointer conversion | |
3161 | (_conv.func_) is applied. If the template-argument | |
3162 | represents a set of overloaded functions (or a pointer to | |
3163 | such), the matching function is selected from the set | |
3164 | (_over.over_). */ | |
3165 | tree fns; | |
3166 | tree fn; | |
3167 | ||
3168 | if (TREE_CODE (expr) == ADDR_EXPR) | |
3169 | fns = TREE_OPERAND (expr, 0); | |
3170 | else | |
3171 | fns = expr; | |
3172 | ||
3173 | fn = instantiate_type (type_pointed_to, fns, tf_none); | |
3174 | ||
3175 | if (fn == error_mark_node) | |
3176 | return error_mark_node; | |
3177 | ||
3178 | if (!DECL_EXTERNAL_LINKAGE_P (fn)) | |
3179 | { | |
3180 | if (really_overloaded_fn (fns)) | |
3181 | return error_mark_node; | |
3182 | else | |
3183 | goto bad_argument; | |
3184 | } | |
3185 | ||
3186 | expr = build_unary_op (ADDR_EXPR, fn, 0); | |
3187 | ||
3188 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), | |
3189 | 0); | |
3190 | return expr; | |
3191 | } | |
3192 | else | |
3193 | { | |
3194 | /* For a non-type template-parameter of type pointer to | |
3195 | object, qualification conversions (_conv.qual_) and the | |
3196 | array-to-pointer conversion (_conv.array_) are applied. | |
3197 | [Note: In particular, neither the null pointer conversion | |
3198 | (_conv.ptr_) nor the derived-to-base conversion | |
3199 | (_conv.ptr_) are applied. Although 0 is a valid | |
3200 | template-argument for a non-type template-parameter of | |
3201 | integral type, it is not a valid template-argument for a | |
3202 | non-type template-parameter of pointer type.] | |
3203 | ||
3204 | The call to decay_conversion performs the | |
3205 | array-to-pointer conversion, if appropriate. */ | |
3206 | expr = decay_conversion (expr); | |
3207 | ||
3208 | if (expr == error_mark_node) | |
3209 | return error_mark_node; | |
3210 | else | |
3211 | return perform_qualification_conversions (type, expr); | |
3212 | } | |
3213 | } | |
3214 | break; | |
3215 | ||
3216 | case REFERENCE_TYPE: | |
3217 | { | |
3218 | tree type_referred_to = TREE_TYPE (type); | |
3219 | ||
3220 | /* If this expression already has reference type, get the | |
3221 | underling object. */ | |
3222 | if (TREE_CODE (expr_type) == REFERENCE_TYPE) | |
3223 | { | |
3224 | my_friendly_assert (TREE_CODE (expr) == ADDR_EXPR, 20000604); | |
3225 | expr = TREE_OPERAND (expr, 0); | |
3226 | expr_type = TREE_TYPE (expr); | |
3227 | } | |
3228 | ||
3229 | if (TREE_CODE (type_referred_to) == FUNCTION_TYPE) | |
3230 | { | |
3231 | /* For a non-type template-parameter of type reference to | |
3232 | function, no conversions apply. If the | |
3233 | template-argument represents a set of overloaded | |
3234 | functions, the matching function is selected from the | |
3235 | set (_over.over_). */ | |
3236 | tree fn; | |
3237 | ||
3238 | fn = instantiate_type (type_referred_to, expr, tf_none); | |
3239 | ||
3240 | if (fn == error_mark_node) | |
3241 | return error_mark_node; | |
3242 | ||
3243 | if (!DECL_EXTERNAL_LINKAGE_P (fn)) | |
3244 | { | |
3245 | if (really_overloaded_fn (expr)) | |
3246 | /* Don't issue an error here; we might get a different | |
3247 | function if the overloading had worked out | |
3248 | differently. */ | |
3249 | return error_mark_node; | |
3250 | else | |
3251 | goto bad_argument; | |
3252 | } | |
3253 | ||
3254 | my_friendly_assert (same_type_p (type_referred_to, | |
3255 | TREE_TYPE (fn)), | |
3256 | 0); | |
3257 | ||
3258 | expr = fn; | |
3259 | } | |
3260 | else | |
3261 | { | |
3262 | /* For a non-type template-parameter of type reference to | |
3263 | object, no conversions apply. The type referred to by the | |
3264 | reference may be more cv-qualified than the (otherwise | |
3265 | identical) type of the template-argument. The | |
3266 | template-parameter is bound directly to the | |
3267 | template-argument, which must be an lvalue. */ | |
3268 | if (!same_type_p (TYPE_MAIN_VARIANT (expr_type), | |
3269 | TYPE_MAIN_VARIANT (type_referred_to)) | |
3270 | || !at_least_as_qualified_p (type_referred_to, | |
3271 | expr_type) | |
3272 | || !real_lvalue_p (expr)) | |
3273 | return error_mark_node; | |
3274 | } | |
3275 | ||
3276 | cxx_mark_addressable (expr); | |
3277 | return build1 (ADDR_EXPR, type, expr); | |
3278 | } | |
3279 | break; | |
3280 | ||
3281 | case RECORD_TYPE: | |
3282 | { | |
3283 | my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 20010112); | |
3284 | ||
3285 | /* For a non-type template-parameter of type pointer to member | |
3286 | function, no conversions apply. If the template-argument | |
3287 | represents a set of overloaded member functions, the | |
3288 | matching member function is selected from the set | |
3289 | (_over.over_). */ | |
3290 | ||
3291 | if (!TYPE_PTRMEMFUNC_P (expr_type) && | |
3292 | expr_type != unknown_type_node) | |
3293 | return error_mark_node; | |
3294 | ||
3295 | if (TREE_CODE (expr) == PTRMEM_CST) | |
3296 | { | |
3297 | /* A ptr-to-member constant. */ | |
3298 | if (!same_type_p (type, expr_type)) | |
3299 | return error_mark_node; | |
3300 | else | |
3301 | return expr; | |
3302 | } | |
3303 | ||
3304 | if (TREE_CODE (expr) != ADDR_EXPR) | |
3305 | return error_mark_node; | |
3306 | ||
3307 | expr = instantiate_type (type, expr, tf_none); | |
3308 | ||
3309 | if (expr == error_mark_node) | |
3310 | return error_mark_node; | |
3311 | ||
3312 | my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), | |
3313 | 0); | |
3314 | return expr; | |
3315 | } | |
3316 | break; | |
3317 | ||
3318 | default: | |
3319 | /* All non-type parameters must have one of these types. */ | |
3320 | abort (); | |
3321 | break; | |
3322 | } | |
3323 | ||
3324 | return error_mark_node; | |
3325 | } | |
3326 | ||
3327 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for | |
3328 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
3329 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
3330 | or PARM_DECL. | |
3331 | ||
3332 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
3333 | the case, then extra parameters must have default arguments. | |
3334 | ||
3335 | Consider the example: | |
3336 | template <class T, class Allocator = allocator> class vector; | |
3337 | template<template <class U> class TT> class C; | |
3338 | ||
3339 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
3340 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
3341 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
3342 | substitute the TT parameter. */ | |
3343 | ||
3344 | static int | |
3345 | coerce_template_template_parms (parm_parms, arg_parms, complain, | |
3346 | in_decl, outer_args) | |
3347 | tree parm_parms, arg_parms; | |
3348 | tsubst_flags_t complain; | |
3349 | tree in_decl, outer_args; | |
3350 | { | |
3351 | int nparms, nargs, i; | |
3352 | tree parm, arg; | |
3353 | ||
3354 | my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0); | |
3355 | my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0); | |
3356 | ||
3357 | nparms = TREE_VEC_LENGTH (parm_parms); | |
3358 | nargs = TREE_VEC_LENGTH (arg_parms); | |
3359 | ||
3360 | /* The rule here is opposite of coerce_template_parms. */ | |
3361 | if (nargs < nparms | |
3362 | || (nargs > nparms | |
3363 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
3364 | return 0; | |
3365 | ||
3366 | for (i = 0; i < nparms; ++i) | |
3367 | { | |
3368 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
3369 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
3370 | ||
3371 | if (arg == NULL_TREE || arg == error_mark_node | |
3372 | || parm == NULL_TREE || parm == error_mark_node) | |
3373 | return 0; | |
3374 | ||
3375 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
3376 | return 0; | |
3377 | ||
3378 | switch (TREE_CODE (parm)) | |
3379 | { | |
3380 | case TYPE_DECL: | |
3381 | break; | |
3382 | ||
3383 | case TEMPLATE_DECL: | |
3384 | /* We encounter instantiations of templates like | |
3385 | template <template <template <class> class> class TT> | |
3386 | class C; */ | |
3387 | { | |
3388 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3389 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3390 | ||
3391 | if (!coerce_template_template_parms (parmparm, argparm, | |
3392 | complain, in_decl, | |
3393 | outer_args)) | |
3394 | return 0; | |
3395 | } | |
3396 | break; | |
3397 | ||
3398 | case PARM_DECL: | |
3399 | /* The tsubst call is used to handle cases such as | |
3400 | template <class T, template <T> class TT> class D; | |
3401 | i.e. the parameter list of TT depends on earlier parameters. */ | |
3402 | if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, | |
3403 | complain, in_decl), | |
3404 | TREE_TYPE (arg))) | |
3405 | return 0; | |
3406 | break; | |
3407 | ||
3408 | default: | |
3409 | abort (); | |
3410 | } | |
3411 | } | |
3412 | return 1; | |
3413 | } | |
3414 | ||
3415 | /* Convert the indicated template ARG as necessary to match the | |
3416 | indicated template PARM. Returns the converted ARG, or | |
3417 | error_mark_node if the conversion was unsuccessful. Error and | |
3418 | warning messages are issued under control of COMPLAIN. This | |
3419 | conversion is for the Ith parameter in the parameter list. ARGS is | |
3420 | the full set of template arguments deduced so far. */ | |
3421 | ||
3422 | static tree | |
3423 | convert_template_argument (parm, arg, args, complain, i, in_decl) | |
3424 | tree parm; | |
3425 | tree arg; | |
3426 | tree args; | |
3427 | tsubst_flags_t complain; | |
3428 | int i; | |
3429 | tree in_decl; | |
3430 | { | |
3431 | tree val; | |
3432 | tree inner_args; | |
3433 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
3434 | ||
3435 | inner_args = INNERMOST_TEMPLATE_ARGS (args); | |
3436 | ||
3437 | if (TREE_CODE (arg) == TREE_LIST | |
3438 | && TREE_TYPE (arg) != NULL_TREE | |
3439 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
3440 | { | |
3441 | /* The template argument was the name of some | |
3442 | member function. That's usually | |
3443 | invalid, but static members are OK. In any | |
3444 | case, grab the underlying fields/functions | |
3445 | and issue an error later if required. */ | |
3446 | arg = TREE_VALUE (arg); | |
3447 | TREE_TYPE (arg) = unknown_type_node; | |
3448 | } | |
3449 | ||
3450 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3451 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3452 | || requires_tmpl_type); | |
3453 | ||
3454 | if (TREE_CODE (arg) != RECORD_TYPE) | |
3455 | is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL | |
3456 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3457 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
3458 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
3459 | else if (CLASSTYPE_TEMPLATE_INFO (arg) && !CLASSTYPE_USE_TEMPLATE (arg) | |
3460 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (arg))) | |
3461 | { | |
3462 | if (is_base_of_enclosing_class (arg, current_class_type)) | |
3463 | /* This is a template name used within the scope of the | |
3464 | template. It could be the template, or it could be the | |
3465 | instantiation. Choose whichever makes sense. */ | |
3466 | is_tmpl_type = requires_tmpl_type; | |
3467 | else | |
3468 | is_tmpl_type = 1; | |
3469 | } | |
3470 | else | |
3471 | /* It is a non-template class, or a specialization of a template | |
3472 | class, or a non-template member of a template class. */ | |
3473 | is_tmpl_type = 0; | |
3474 | ||
3475 | if (is_tmpl_type | |
3476 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
3477 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
3478 | arg = TYPE_STUB_DECL (arg); | |
3479 | else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) | |
3480 | arg = CLASSTYPE_TI_TEMPLATE (arg); | |
3481 | ||
3482 | is_type = TYPE_P (arg) || is_tmpl_type; | |
3483 | ||
3484 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3485 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3486 | { | |
3487 | pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg); | |
3488 | ||
3489 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3490 | TREE_OPERAND (arg, 1), | |
3491 | complain & tf_error); | |
3492 | is_type = 1; | |
3493 | } | |
3494 | if (is_type != requires_type) | |
3495 | { | |
3496 | if (in_decl) | |
3497 | { | |
3498 | if (complain & tf_error) | |
3499 | { | |
3500 | error ("type/value mismatch at argument %d in template parameter list for `%D'", | |
3501 | i + 1, in_decl); | |
3502 | if (is_type) | |
3503 | error (" expected a constant of type `%T', got `%T'", | |
3504 | TREE_TYPE (parm), | |
3505 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
3506 | else | |
3507 | error (" expected a type, got `%E'", arg); | |
3508 | } | |
3509 | } | |
3510 | return error_mark_node; | |
3511 | } | |
3512 | if (is_tmpl_type ^ requires_tmpl_type) | |
3513 | { | |
3514 | if (in_decl && (complain & tf_error)) | |
3515 | { | |
3516 | error ("type/value mismatch at argument %d in template parameter list for `%D'", | |
3517 | i + 1, in_decl); | |
3518 | if (is_tmpl_type) | |
3519 | error (" expected a type, got `%T'", DECL_NAME (arg)); | |
3520 | else | |
3521 | error (" expected a class template, got `%T'", arg); | |
3522 | } | |
3523 | return error_mark_node; | |
3524 | } | |
3525 | ||
3526 | if (is_type) | |
3527 | { | |
3528 | if (requires_tmpl_type) | |
3529 | { | |
3530 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) | |
3531 | /* The number of argument required is not known yet. | |
3532 | Just accept it for now. */ | |
3533 | val = TREE_TYPE (arg); | |
3534 | else | |
3535 | { | |
3536 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3537 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3538 | ||
3539 | if (coerce_template_template_parms (parmparm, argparm, | |
3540 | complain, in_decl, | |
3541 | inner_args)) | |
3542 | { | |
3543 | val = arg; | |
3544 | ||
3545 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3546 | TEMPLATE_DECL. */ | |
3547 | if (val != error_mark_node | |
3548 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3549 | val = TREE_TYPE (val); | |
3550 | } | |
3551 | else | |
3552 | { | |
3553 | if (in_decl && (complain & tf_error)) | |
3554 | { | |
3555 | error ("type/value mismatch at argument %d in template parameter list for `%D'", | |
3556 | i + 1, in_decl); | |
3557 | error (" expected a template of type `%D', got `%D'", parm, arg); | |
3558 | } | |
3559 | ||
3560 | val = error_mark_node; | |
3561 | } | |
3562 | } | |
3563 | } | |
3564 | else | |
3565 | { | |
3566 | val = groktypename (arg); | |
3567 | if (! processing_template_decl) | |
3568 | { | |
3569 | /* [basic.link]: A name with no linkage (notably, the | |
3570 | name of a class or enumeration declared in a local | |
3571 | scope) shall not be used to declare an entity with | |
3572 | linkage. This implies that names with no linkage | |
3573 | cannot be used as template arguments. */ | |
3574 | tree t = no_linkage_check (val); | |
3575 | if (t) | |
3576 | { | |
3577 | if (TYPE_ANONYMOUS_P (t)) | |
3578 | pedwarn | |
3579 | ("template-argument `%T' uses anonymous type", val); | |
3580 | else | |
3581 | error | |
3582 | ("template-argument `%T' uses local type `%T'", | |
3583 | val, t); | |
3584 | return error_mark_node; | |
3585 | } | |
3586 | ||
3587 | /* In order to avoid all sorts of complications, we do | |
3588 | not allow variably-modified types as template | |
3589 | arguments. */ | |
3590 | if (variably_modified_type_p (val)) | |
3591 | { | |
3592 | error ("template-argument `%T' is a variably modified type", | |
3593 | val); | |
3594 | return error_mark_node; | |
3595 | } | |
3596 | } | |
3597 | } | |
3598 | } | |
3599 | else | |
3600 | { | |
3601 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); | |
3602 | ||
3603 | if (invalid_nontype_parm_type_p (t, complain)) | |
3604 | return error_mark_node; | |
3605 | ||
3606 | if (processing_template_decl) | |
3607 | arg = maybe_fold_nontype_arg (arg); | |
3608 | ||
3609 | if (!uses_template_parms (arg) && !uses_template_parms (t)) | |
3610 | /* We used to call digest_init here. However, digest_init | |
3611 | will report errors, which we don't want when complain | |
3612 | is zero. More importantly, digest_init will try too | |
3613 | hard to convert things: for example, `0' should not be | |
3614 | converted to pointer type at this point according to | |
3615 | the standard. Accepting this is not merely an | |
3616 | extension, since deciding whether or not these | |
3617 | conversions can occur is part of determining which | |
3618 | function template to call, or whether a given explicit | |
3619 | argument specification is valid. */ | |
3620 | val = convert_nontype_argument (t, arg); | |
3621 | else | |
3622 | val = arg; | |
3623 | ||
3624 | if (val == NULL_TREE) | |
3625 | val = error_mark_node; | |
3626 | else if (val == error_mark_node && (complain & tf_error)) | |
3627 | error ("could not convert template argument `%E' to `%T'", | |
3628 | arg, t); | |
3629 | } | |
3630 | ||
3631 | return val; | |
3632 | } | |
3633 | ||
3634 | /* Convert all template arguments to their appropriate types, and | |
3635 | return a vector containing the innermost resulting template | |
3636 | arguments. If any error occurs, return error_mark_node. Error and | |
3637 | warning messages are issued under control of COMPLAIN. Some error | |
3638 | messages are issued even if COMPLAIN is zero; for instance, if a | |
3639 | template argument is composed from a local class. | |
3640 | ||
3641 | If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be | |
3642 | provided in ARGLIST, or else trailing parameters must have default | |
3643 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
3644 | deduction for any unspecified trailing arguments. */ | |
3645 | ||
3646 | static tree | |
3647 | coerce_template_parms (parms, args, in_decl, | |
3648 | complain, | |
3649 | require_all_arguments) | |
3650 | tree parms, args; | |
3651 | tree in_decl; | |
3652 | tsubst_flags_t complain; | |
3653 | int require_all_arguments; | |
3654 | { | |
3655 | int nparms, nargs, i, lost = 0; | |
3656 | tree inner_args; | |
3657 | tree new_args; | |
3658 | tree new_inner_args; | |
3659 | ||
3660 | inner_args = INNERMOST_TEMPLATE_ARGS (args); | |
3661 | nargs = NUM_TMPL_ARGS (inner_args); | |
3662 | nparms = TREE_VEC_LENGTH (parms); | |
3663 | ||
3664 | if (nargs > nparms | |
3665 | || (nargs < nparms | |
3666 | && require_all_arguments | |
3667 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) | |
3668 | { | |
3669 | if (complain & tf_error) | |
3670 | { | |
3671 | error ("wrong number of template arguments (%d, should be %d)", | |
3672 | nargs, nparms); | |
3673 | ||
3674 | if (in_decl) | |
3675 | cp_error_at ("provided for `%D'", in_decl); | |
3676 | } | |
3677 | ||
3678 | return error_mark_node; | |
3679 | } | |
3680 | ||
3681 | new_inner_args = make_tree_vec (nparms); | |
3682 | new_args = add_outermost_template_args (args, new_inner_args); | |
3683 | for (i = 0; i < nparms; i++) | |
3684 | { | |
3685 | tree arg; | |
3686 | tree parm; | |
3687 | ||
3688 | /* Get the Ith template parameter. */ | |
3689 | parm = TREE_VEC_ELT (parms, i); | |
3690 | ||
3691 | /* Calculate the Ith argument. */ | |
3692 | if (inner_args && TREE_CODE (inner_args) == TREE_LIST) | |
3693 | { | |
3694 | arg = TREE_VALUE (inner_args); | |
3695 | inner_args = TREE_CHAIN (inner_args); | |
3696 | } | |
3697 | else if (i < nargs) | |
3698 | arg = TREE_VEC_ELT (inner_args, i); | |
3699 | /* If no template argument was supplied, look for a default | |
3700 | value. */ | |
3701 | else if (TREE_PURPOSE (parm) == NULL_TREE) | |
3702 | { | |
3703 | /* There was no default value. */ | |
3704 | my_friendly_assert (!require_all_arguments, 0); | |
3705 | break; | |
3706 | } | |
3707 | else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) | |
3708 | arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl); | |
3709 | else | |
3710 | arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain, | |
3711 | in_decl); | |
3712 | ||
3713 | /* Now, convert the Ith argument, as necessary. */ | |
3714 | if (arg == NULL_TREE) | |
3715 | /* We're out of arguments. */ | |
3716 | { | |
3717 | my_friendly_assert (!require_all_arguments, 0); | |
3718 | break; | |
3719 | } | |
3720 | else if (arg == error_mark_node) | |
3721 | { | |
3722 | error ("template argument %d is invalid", i + 1); | |
3723 | arg = error_mark_node; | |
3724 | } | |
3725 | else | |
3726 | arg = convert_template_argument (TREE_VALUE (parm), | |
3727 | arg, new_args, complain, i, | |
3728 | in_decl); | |
3729 | ||
3730 | if (arg == error_mark_node) | |
3731 | lost++; | |
3732 | TREE_VEC_ELT (new_inner_args, i) = arg; | |
3733 | } | |
3734 | ||
3735 | if (lost) | |
3736 | return error_mark_node; | |
3737 | ||
3738 | return new_inner_args; | |
3739 | } | |
3740 | ||
3741 | /* Returns 1 if template args OT and NT are equivalent. */ | |
3742 | ||
3743 | static int | |
3744 | template_args_equal (ot, nt) | |
3745 | tree ot, nt; | |
3746 | { | |
3747 | if (nt == ot) | |
3748 | return 1; | |
3749 | ||
3750 | if (TREE_CODE (nt) == TREE_VEC) | |
3751 | /* For member templates */ | |
3752 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); | |
3753 | else if (TYPE_P (nt)) | |
3754 | return TYPE_P (ot) && same_type_p (ot, nt); | |
3755 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) | |
3756 | return 0; | |
3757 | else | |
3758 | return (cp_tree_equal (ot, nt) > 0); | |
3759 | } | |
3760 | ||
3761 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
3762 | of template arguments. Returns 0 otherwise. */ | |
3763 | ||
3764 | int | |
3765 | comp_template_args (oldargs, newargs) | |
3766 | tree oldargs, newargs; | |
3767 | { | |
3768 | int i; | |
3769 | ||
3770 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) | |
3771 | return 0; | |
3772 | ||
3773 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) | |
3774 | { | |
3775 | tree nt = TREE_VEC_ELT (newargs, i); | |
3776 | tree ot = TREE_VEC_ELT (oldargs, i); | |
3777 | ||
3778 | if (! template_args_equal (ot, nt)) | |
3779 | return 0; | |
3780 | } | |
3781 | return 1; | |
3782 | } | |
3783 | ||
3784 | /* Given class template name and parameter list, produce a user-friendly name | |
3785 | for the instantiation. */ | |
3786 | ||
3787 | static char * | |
3788 | mangle_class_name_for_template (name, parms, arglist) | |
3789 | const char *name; | |
3790 | tree parms, arglist; | |
3791 | { | |
3792 | static struct obstack scratch_obstack; | |
3793 | static char *scratch_firstobj; | |
3794 | int i, nparms; | |
3795 | ||
3796 | if (!scratch_firstobj) | |
3797 | gcc_obstack_init (&scratch_obstack); | |
3798 | else | |
3799 | obstack_free (&scratch_obstack, scratch_firstobj); | |
3800 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); | |
3801 | ||
3802 | #define ccat(C) obstack_1grow (&scratch_obstack, (C)); | |
3803 | #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S)) | |
3804 | ||
3805 | cat (name); | |
3806 | ccat ('<'); | |
3807 | nparms = TREE_VEC_LENGTH (parms); | |
3808 | arglist = INNERMOST_TEMPLATE_ARGS (arglist); | |
3809 | my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268); | |
3810 | for (i = 0; i < nparms; i++) | |
3811 | { | |
3812 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
3813 | tree arg = TREE_VEC_ELT (arglist, i); | |
3814 | ||
3815 | if (i) | |
3816 | ccat (','); | |
3817 | ||
3818 | if (TREE_CODE (parm) == TYPE_DECL) | |
3819 | { | |
3820 | cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); | |
3821 | continue; | |
3822 | } | |
3823 | else if (TREE_CODE (parm) == TEMPLATE_DECL) | |
3824 | { | |
3825 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
3826 | { | |
3827 | /* Already substituted with real template. Just output | |
3828 | the template name here */ | |
3829 | tree context = DECL_CONTEXT (arg); | |
3830 | if (context) | |
3831 | { | |
3832 | /* The template may be defined in a namespace, or | |
3833 | may be a member template. */ | |
3834 | my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL | |
3835 | || CLASS_TYPE_P (context), | |
3836 | 980422); | |
3837 | cat(decl_as_string (DECL_CONTEXT (arg), TFF_PLAIN_IDENTIFIER)); | |
3838 | cat("::"); | |
3839 | } | |
3840 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
3841 | } | |
3842 | else | |
3843 | /* Output the parameter declaration */ | |
3844 | cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); | |
3845 | continue; | |
3846 | } | |
3847 | else | |
3848 | my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269); | |
3849 | ||
3850 | if (TREE_CODE (arg) == TREE_LIST) | |
3851 | { | |
3852 | /* New list cell was built because old chain link was in | |
3853 | use. */ | |
3854 | my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270); | |
3855 | arg = TREE_VALUE (arg); | |
3856 | } | |
3857 | /* No need to check arglist against parmlist here; we did that | |
3858 | in coerce_template_parms, called from lookup_template_class. */ | |
3859 | cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER)); | |
3860 | } | |
3861 | { | |
3862 | char *bufp = obstack_next_free (&scratch_obstack); | |
3863 | int offset = 0; | |
3864 | while (bufp[offset - 1] == ' ') | |
3865 | offset--; | |
3866 | obstack_blank_fast (&scratch_obstack, offset); | |
3867 | ||
3868 | /* B<C<char> >, not B<C<char>> */ | |
3869 | if (bufp[offset - 1] == '>') | |
3870 | ccat (' '); | |
3871 | } | |
3872 | ccat ('>'); | |
3873 | ccat ('\0'); | |
3874 | return (char *) obstack_base (&scratch_obstack); | |
3875 | } | |
3876 | ||
3877 | static tree | |
3878 | classtype_mangled_name (t) | |
3879 | tree t; | |
3880 | { | |
3881 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
3882 | /* Specializations have already had their names set up in | |
3883 | lookup_template_class. */ | |
3884 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) | |
3885 | { | |
3886 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
3887 | ||
3888 | /* For non-primary templates, the template parameters are | |
3889 | implicit from their surrounding context. */ | |
3890 | if (PRIMARY_TEMPLATE_P (tmpl)) | |
3891 | { | |
3892 | tree name = DECL_NAME (tmpl); | |
3893 | char *mangled_name = mangle_class_name_for_template | |
3894 | (IDENTIFIER_POINTER (name), | |
3895 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
3896 | CLASSTYPE_TI_ARGS (t)); | |
3897 | tree id = get_identifier (mangled_name); | |
3898 | IDENTIFIER_TEMPLATE (id) = name; | |
3899 | return id; | |
3900 | } | |
3901 | } | |
3902 | ||
3903 | return TYPE_IDENTIFIER (t); | |
3904 | } | |
3905 | ||
3906 | static void | |
3907 | add_pending_template (d) | |
3908 | tree d; | |
3909 | { | |
3910 | tree ti = (TYPE_P (d) | |
3911 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
3912 | : DECL_TEMPLATE_INFO (d)); | |
3913 | tree pt; | |
3914 | int level; | |
3915 | ||
3916 | if (TI_PENDING_TEMPLATE_FLAG (ti)) | |
3917 | return; | |
3918 | ||
3919 | /* We are called both from instantiate_decl, where we've already had a | |
3920 | tinst_level pushed, and instantiate_template, where we haven't. | |
3921 | Compensate. */ | |
3922 | level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d); | |
3923 | ||
3924 | if (level) | |
3925 | push_tinst_level (d); | |
3926 | ||
3927 | pt = tree_cons (current_tinst_level, d, NULL_TREE); | |
3928 | if (last_pending_template) | |
3929 | TREE_CHAIN (last_pending_template) = pt; | |
3930 | else | |
3931 | pending_templates = pt; | |
3932 | ||
3933 | last_pending_template = pt; | |
3934 | ||
3935 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; | |
3936 | ||
3937 | if (level) | |
3938 | pop_tinst_level (); | |
3939 | } | |
3940 | ||
3941 | ||
3942 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and | |
3943 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
3944 | documentation for TEMPLATE_ID_EXPR. */ | |
3945 | ||
3946 | tree | |
3947 | lookup_template_function (fns, arglist) | |
3948 | tree fns, arglist; | |
3949 | { | |
3950 | tree type; | |
3951 | ||
3952 | if (fns == error_mark_node || arglist == error_mark_node) | |
3953 | return error_mark_node; | |
3954 | ||
3955 | if (fns == NULL_TREE) | |
3956 | { | |
3957 | error ("non-template used as template"); | |
3958 | return error_mark_node; | |
3959 | } | |
3960 | ||
3961 | my_friendly_assert (TREE_CODE (fns) == TEMPLATE_DECL | |
3962 | || TREE_CODE (fns) == OVERLOAD | |
3963 | || BASELINK_P (fns) | |
3964 | || TREE_CODE (fns) == IDENTIFIER_NODE | |
3965 | || TREE_CODE (fns) == LOOKUP_EXPR, | |
3966 | 20020730); | |
3967 | ||
3968 | if (BASELINK_P (fns)) | |
3969 | { | |
3970 | BASELINK_FUNCTIONS (fns) = build (TEMPLATE_ID_EXPR, | |
3971 | unknown_type_node, | |
3972 | BASELINK_FUNCTIONS (fns), | |
3973 | arglist); | |
3974 | return fns; | |
3975 | } | |
3976 | ||
3977 | type = TREE_TYPE (fns); | |
3978 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
3979 | type = unknown_type_node; | |
3980 | ||
3981 | return build (TEMPLATE_ID_EXPR, type, fns, arglist); | |
3982 | } | |
3983 | ||
3984 | /* Within the scope of a template class S<T>, the name S gets bound | |
3985 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
3986 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
3987 | or one of its enclosing classes, and that type is a template, | |
3988 | return the associated TEMPLATE_DECL. Otherwise, the original | |
3989 | DECL is returned. */ | |
3990 | ||
3991 | tree | |
3992 | maybe_get_template_decl_from_type_decl (decl) | |
3993 | tree decl; | |
3994 | { | |
3995 | return (decl != NULL_TREE | |
3996 | && TREE_CODE (decl) == TYPE_DECL | |
3997 | && DECL_ARTIFICIAL (decl) | |
3998 | && CLASS_TYPE_P (TREE_TYPE (decl)) | |
3999 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) | |
4000 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
4001 | } | |
4002 | ||
4003 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of | |
4004 | parameters, find the desired type. | |
4005 | ||
4006 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
4007 | (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will | |
4008 | be a TREE_LIST if called directly from the parser, and a TREE_VEC | |
4009 | otherwise.) | |
4010 | ||
4011 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
4012 | instantiate. | |
4013 | ||
4014 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of | |
4015 | the class we are looking up. | |
4016 | ||
4017 | Issue error and warning messages under control of COMPLAIN. | |
4018 | ||
4019 | If the template class is really a local class in a template | |
4020 | function, then the FUNCTION_CONTEXT is the function in which it is | |
4021 | being instantiated. */ | |
4022 | ||
4023 | tree | |
4024 | lookup_template_class (d1, arglist, in_decl, context, entering_scope, complain) | |
4025 | tree d1, arglist; | |
4026 | tree in_decl; | |
4027 | tree context; | |
4028 | int entering_scope; | |
4029 | tsubst_flags_t complain; | |
4030 | { | |
4031 | tree template = NULL_TREE, parmlist; | |
4032 | tree t; | |
4033 | ||
4034 | timevar_push (TV_NAME_LOOKUP); | |
4035 | if (TREE_CODE (d1) == IDENTIFIER_NODE) | |
4036 | { | |
4037 | if (IDENTIFIER_VALUE (d1) | |
4038 | && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1))) | |
4039 | template = IDENTIFIER_VALUE (d1); | |
4040 | else | |
4041 | { | |
4042 | if (context) | |
4043 | push_decl_namespace (context); | |
4044 | template = lookup_name (d1, /*prefer_type=*/0); | |
4045 | template = maybe_get_template_decl_from_type_decl (template); | |
4046 | if (context) | |
4047 | pop_decl_namespace (); | |
4048 | } | |
4049 | if (template) | |
4050 | context = DECL_CONTEXT (template); | |
4051 | } | |
4052 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) | |
4053 | { | |
4054 | tree type = TREE_TYPE (d1); | |
4055 | ||
4056 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
4057 | an implicit typename for the second A. Deal with it. */ | |
4058 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
4059 | type = TREE_TYPE (type); | |
4060 | ||
4061 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
4062 | { | |
4063 | template = CLASSTYPE_TI_TEMPLATE (type); | |
4064 | d1 = DECL_NAME (template); | |
4065 | } | |
4066 | } | |
4067 | else if (TREE_CODE (d1) == ENUMERAL_TYPE | |
4068 | || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) | |
4069 | { | |
4070 | template = TYPE_TI_TEMPLATE (d1); | |
4071 | d1 = DECL_NAME (template); | |
4072 | } | |
4073 | else if (TREE_CODE (d1) == TEMPLATE_DECL | |
4074 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) | |
4075 | { | |
4076 | template = d1; | |
4077 | d1 = DECL_NAME (template); | |
4078 | context = DECL_CONTEXT (template); | |
4079 | } | |
4080 | ||
4081 | /* With something like `template <class T> class X class X { ... };' | |
4082 | we could end up with D1 having nothing but an IDENTIFIER_VALUE. | |
4083 | We don't want to do that, but we have to deal with the situation, | |
4084 | so let's give them some syntax errors to chew on instead of a | |
4085 | crash. Alternatively D1 might not be a template type at all. */ | |
4086 | if (! template) | |
4087 | { | |
4088 | if (complain & tf_error) | |
4089 | error ("`%T' is not a template", d1); | |
4090 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4091 | } | |
4092 | ||
4093 | if (TREE_CODE (template) != TEMPLATE_DECL | |
4094 | /* If we're called from the parser, make sure it's a user visible | |
4095 | template. */ | |
4096 | || ((!arglist || TREE_CODE (arglist) == TREE_LIST) | |
4097 | && !DECL_TEMPLATE_PARM_P (template) | |
4098 | && !PRIMARY_TEMPLATE_P (template))) | |
4099 | { | |
4100 | if (complain & tf_error) | |
4101 | { | |
4102 | error ("non-template type `%T' used as a template", d1); | |
4103 | if (in_decl) | |
4104 | cp_error_at ("for template declaration `%D'", in_decl); | |
4105 | } | |
4106 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4107 | } | |
4108 | ||
4109 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) | |
4110 | { | |
4111 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
4112 | template arguments */ | |
4113 | ||
4114 | tree parm; | |
4115 | tree arglist2; | |
4116 | ||
4117 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); | |
4118 | ||
4119 | /* Consider an example where a template template parameter declared as | |
4120 | ||
4121 | template <class T, class U = std::allocator<T> > class TT | |
4122 | ||
4123 | The template parameter level of T and U are one level larger than | |
4124 | of TT. To proper process the default argument of U, say when an | |
4125 | instantiation `TT<int>' is seen, we need to build the full | |
4126 | arguments containing {int} as the innermost level. Outer levels, | |
4127 | available when not appearing as default template argument, can be | |
4128 | obtained from `current_template_args ()'. | |
4129 | ||
4130 | Suppose that TT is later substituted with std::vector. The above | |
4131 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
4132 | level 1, and T at level 2, while the template arguments at level 1 | |
4133 | becomes {std::vector} and the inner level 2 is {int}. */ | |
4134 | ||
4135 | if (current_template_parms) | |
4136 | arglist = add_to_template_args (current_template_args (), arglist); | |
4137 | ||
4138 | arglist2 = coerce_template_parms (parmlist, arglist, template, | |
4139 | complain, /*require_all_args=*/1); | |
4140 | if (arglist2 == error_mark_node) | |
4141 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4142 | ||
4143 | parm = bind_template_template_parm (TREE_TYPE (template), arglist2); | |
4144 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); | |
4145 | } | |
4146 | else | |
4147 | { | |
4148 | tree template_type = TREE_TYPE (template); | |
4149 | tree gen_tmpl; | |
4150 | tree type_decl; | |
4151 | tree found = NULL_TREE; | |
4152 | tree *tp; | |
4153 | int arg_depth; | |
4154 | int parm_depth; | |
4155 | int is_partial_instantiation; | |
4156 | ||
4157 | gen_tmpl = most_general_template (template); | |
4158 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
4159 | parm_depth = TMPL_PARMS_DEPTH (parmlist); | |
4160 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
4161 | ||
4162 | if (arg_depth == 1 && parm_depth > 1) | |
4163 | { | |
4164 | /* We've been given an incomplete set of template arguments. | |
4165 | For example, given: | |
4166 | ||
4167 | template <class T> struct S1 { | |
4168 | template <class U> struct S2 {}; | |
4169 | template <class U> struct S2<U*> {}; | |
4170 | }; | |
4171 | ||
4172 | we will be called with an ARGLIST of `U*', but the | |
4173 | TEMPLATE will be `template <class T> template | |
4174 | <class U> struct S1<T>::S2'. We must fill in the missing | |
4175 | arguments. */ | |
4176 | arglist | |
4177 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), | |
4178 | arglist); | |
4179 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
4180 | } | |
4181 | ||
4182 | /* Now we should have enough arguments. */ | |
4183 | my_friendly_assert (parm_depth == arg_depth, 0); | |
4184 | ||
4185 | /* From here on, we're only interested in the most general | |
4186 | template. */ | |
4187 | template = gen_tmpl; | |
4188 | ||
4189 | /* Calculate the BOUND_ARGS. These will be the args that are | |
4190 | actually tsubst'd into the definition to create the | |
4191 | instantiation. */ | |
4192 | if (parm_depth > 1) | |
4193 | { | |
4194 | /* We have multiple levels of arguments to coerce, at once. */ | |
4195 | int i; | |
4196 | int saved_depth = TMPL_ARGS_DEPTH (arglist); | |
4197 | ||
4198 | tree bound_args = make_tree_vec (parm_depth); | |
4199 | ||
4200 | for (i = saved_depth, | |
4201 | t = DECL_TEMPLATE_PARMS (template); | |
4202 | i > 0 && t != NULL_TREE; | |
4203 | --i, t = TREE_CHAIN (t)) | |
4204 | { | |
4205 | tree a = coerce_template_parms (TREE_VALUE (t), | |
4206 | arglist, template, | |
4207 | complain, /*require_all_args=*/1); | |
4208 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); | |
4209 | ||
4210 | /* We temporarily reduce the length of the ARGLIST so | |
4211 | that coerce_template_parms will see only the arguments | |
4212 | corresponding to the template parameters it is | |
4213 | examining. */ | |
4214 | TREE_VEC_LENGTH (arglist)--; | |
4215 | } | |
4216 | ||
4217 | /* Restore the ARGLIST to its full size. */ | |
4218 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
4219 | ||
4220 | arglist = bound_args; | |
4221 | } | |
4222 | else | |
4223 | arglist | |
4224 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
4225 | INNERMOST_TEMPLATE_ARGS (arglist), | |
4226 | template, | |
4227 | complain, /*require_all_args=*/1); | |
4228 | ||
4229 | if (arglist == error_mark_node) | |
4230 | /* We were unable to bind the arguments. */ | |
4231 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4232 | ||
4233 | /* In the scope of a template class, explicit references to the | |
4234 | template class refer to the type of the template, not any | |
4235 | instantiation of it. For example, in: | |
4236 | ||
4237 | template <class T> class C { void f(C<T>); } | |
4238 | ||
4239 | the `C<T>' is just the same as `C'. Outside of the | |
4240 | class, however, such a reference is an instantiation. */ | |
4241 | if (comp_template_args (TYPE_TI_ARGS (template_type), | |
4242 | arglist)) | |
4243 | { | |
4244 | found = template_type; | |
4245 | ||
4246 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
4247 | { | |
4248 | tree ctx; | |
4249 | ||
4250 | /* Note that we use DECL_CONTEXT, rather than | |
4251 | CP_DECL_CONTEXT, so that the termination test is | |
4252 | always just `ctx'. We're not interested in namespace | |
4253 | scopes. */ | |
4254 | for (ctx = current_class_type; | |
4255 | ctx; | |
4256 | ctx = (TYPE_P (ctx)) ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx)) | |
4257 | if (same_type_p (ctx, template_type)) | |
4258 | break; | |
4259 | ||
4260 | if (!ctx) | |
4261 | /* We're not in the scope of the class, so the | |
4262 | TEMPLATE_TYPE is not the type we want after | |
4263 | all. */ | |
4264 | found = NULL_TREE; | |
4265 | } | |
4266 | } | |
4267 | if (found) | |
4268 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
4269 | ||
4270 | for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template); | |
4271 | *tp; | |
4272 | tp = &TREE_CHAIN (*tp)) | |
4273 | if (comp_template_args (TREE_PURPOSE (*tp), arglist)) | |
4274 | { | |
4275 | found = *tp; | |
4276 | ||
4277 | /* Use the move-to-front heuristic to speed up future | |
4278 | searches. */ | |
4279 | *tp = TREE_CHAIN (*tp); | |
4280 | TREE_CHAIN (found) | |
4281 | = DECL_TEMPLATE_INSTANTIATIONS (template); | |
4282 | DECL_TEMPLATE_INSTANTIATIONS (template) = found; | |
4283 | ||
4284 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found)); | |
4285 | } | |
4286 | ||
4287 | /* This type is a "partial instantiation" if any of the template | |
4288 | arguments still involve template parameters. Note that we set | |
4289 | IS_PARTIAL_INSTANTIATION for partial specializations as | |
4290 | well. */ | |
4291 | is_partial_instantiation = uses_template_parms (arglist); | |
4292 | ||
4293 | if (!is_partial_instantiation | |
4294 | && !PRIMARY_TEMPLATE_P (template) | |
4295 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
4296 | { | |
4297 | found = xref_tag_from_type (TREE_TYPE (template), | |
4298 | DECL_NAME (template), | |
4299 | /*globalize=*/1); | |
4300 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
4301 | } | |
4302 | ||
4303 | context = tsubst (DECL_CONTEXT (template), arglist, | |
4304 | complain, in_decl); | |
4305 | if (!context) | |
4306 | context = global_namespace; | |
4307 | ||
4308 | /* Create the type. */ | |
4309 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) | |
4310 | { | |
4311 | if (!is_partial_instantiation) | |
4312 | { | |
4313 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
4314 | t = start_enum (TYPE_IDENTIFIER (template_type)); | |
4315 | } | |
4316 | else | |
4317 | /* We don't want to call start_enum for this type, since | |
4318 | the values for the enumeration constants may involve | |
4319 | template parameters. And, no one should be interested | |
4320 | in the enumeration constants for such a type. */ | |
4321 | t = make_node (ENUMERAL_TYPE); | |
4322 | } | |
4323 | else | |
4324 | { | |
4325 | t = make_aggr_type (TREE_CODE (template_type)); | |
4326 | CLASSTYPE_DECLARED_CLASS (t) | |
4327 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
4328 | CLASSTYPE_GOT_SEMICOLON (t) = 1; | |
4329 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); | |
4330 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); | |
4331 | ||
4332 | /* A local class. Make sure the decl gets registered properly. */ | |
4333 | if (context == current_function_decl) | |
4334 | pushtag (DECL_NAME (template), t, 0); | |
4335 | } | |
4336 | ||
4337 | /* If we called start_enum or pushtag above, this information | |
4338 | will already be set up. */ | |
4339 | if (!TYPE_NAME (t)) | |
4340 | { | |
4341 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
4342 | ||
4343 | type_decl = create_implicit_typedef (DECL_NAME (template), t); | |
4344 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); | |
4345 | TYPE_STUB_DECL (t) = type_decl; | |
4346 | DECL_SOURCE_LOCATION (type_decl) | |
4347 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); | |
4348 | } | |
4349 | else | |
4350 | type_decl = TYPE_NAME (t); | |
4351 | ||
4352 | TREE_PRIVATE (type_decl) | |
4353 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
4354 | TREE_PROTECTED (type_decl) | |
4355 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
4356 | ||
4357 | /* Set up the template information. We have to figure out which | |
4358 | template is the immediate parent if this is a full | |
4359 | instantiation. */ | |
4360 | if (parm_depth == 1 || is_partial_instantiation | |
4361 | || !PRIMARY_TEMPLATE_P (template)) | |
4362 | /* This case is easy; there are no member templates involved. */ | |
4363 | found = template; | |
4364 | else | |
4365 | { | |
4366 | /* This is a full instantiation of a member template. Look | |
4367 | for a partial instantiation of which this is an instance. */ | |
4368 | ||
4369 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
4370 | found; found = TREE_CHAIN (found)) | |
4371 | { | |
4372 | int success; | |
4373 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
4374 | ||
4375 | /* We only want partial instantiations, here, not | |
4376 | specializations or full instantiations. */ | |
4377 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) | |
4378 | || !uses_template_parms (TREE_VALUE (found))) | |
4379 | continue; | |
4380 | ||
4381 | /* Temporarily reduce by one the number of levels in the | |
4382 | ARGLIST and in FOUND so as to avoid comparing the | |
4383 | last set of arguments. */ | |
4384 | TREE_VEC_LENGTH (arglist)--; | |
4385 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
4386 | ||
4387 | /* See if the arguments match. If they do, then TMPL is | |
4388 | the partial instantiation we want. */ | |
4389 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
4390 | ||
4391 | /* Restore the argument vectors to their full size. */ | |
4392 | TREE_VEC_LENGTH (arglist)++; | |
4393 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
4394 | ||
4395 | if (success) | |
4396 | { | |
4397 | found = tmpl; | |
4398 | break; | |
4399 | } | |
4400 | } | |
4401 | ||
4402 | if (!found) | |
4403 | { | |
4404 | /* There was no partial instantiation. This happens | |
4405 | where C<T> is a member template of A<T> and it's used | |
4406 | in something like | |
4407 | ||
4408 | template <typename T> struct B { A<T>::C<int> m; }; | |
4409 | B<float>; | |
4410 | ||
4411 | Create the partial instantiation. | |
4412 | */ | |
4413 | TREE_VEC_LENGTH (arglist)--; | |
4414 | found = tsubst (template, arglist, complain, NULL_TREE); | |
4415 | TREE_VEC_LENGTH (arglist)++; | |
4416 | } | |
4417 | } | |
4418 | ||
4419 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); | |
4420 | DECL_TEMPLATE_INSTANTIATIONS (template) | |
4421 | = tree_cons (arglist, t, | |
4422 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
4423 | ||
4424 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
4425 | && !is_partial_instantiation) | |
4426 | /* Now that the type has been registered on the instantiations | |
4427 | list, we set up the enumerators. Because the enumeration | |
4428 | constants may involve the enumeration type itself, we make | |
4429 | sure to register the type first, and then create the | |
4430 | constants. That way, doing tsubst_expr for the enumeration | |
4431 | constants won't result in recursive calls here; we'll find | |
4432 | the instantiation and exit above. */ | |
4433 | tsubst_enum (template_type, t, arglist); | |
4434 | ||
4435 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO | |
4436 | is set up. */ | |
4437 | if (TREE_CODE (t) != ENUMERAL_TYPE) | |
4438 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
4439 | if (!is_partial_instantiation) | |
4440 | { | |
4441 | /* For backwards compatibility; code that uses | |
4442 | -fexternal-templates expects looking up a template to | |
4443 | instantiate it. I think DDD still relies on this. | |
4444 | (jason 8/20/1998) */ | |
4445 | if (TREE_CODE (t) != ENUMERAL_TYPE | |
4446 | && flag_external_templates | |
4447 | && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template)) | |
4448 | && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template))) | |
4449 | add_pending_template (t); | |
4450 | } | |
4451 | else | |
4452 | /* If the type makes use of template parameters, the | |
4453 | code that generates debugging information will crash. */ | |
4454 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
4455 | ||
4456 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); | |
4457 | } | |
4458 | timevar_pop (TV_NAME_LOOKUP); | |
4459 | } | |
4460 | \f | |
4461 | struct pair_fn_data | |
4462 | { | |
4463 | tree_fn_t fn; | |
4464 | void *data; | |
4465 | htab_t visited; | |
4466 | }; | |
4467 | ||
4468 | /* Called from for_each_template_parm via walk_tree. */ | |
4469 | ||
4470 | static tree | |
4471 | for_each_template_parm_r (tp, walk_subtrees, d) | |
4472 | tree *tp; | |
4473 | int *walk_subtrees; | |
4474 | void *d; | |
4475 | { | |
4476 | tree t = *tp; | |
4477 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
4478 | tree_fn_t fn = pfd->fn; | |
4479 | void *data = pfd->data; | |
4480 | void **slot; | |
4481 | ||
4482 | /* If we have already visited this tree, there's no need to walk | |
4483 | subtrees. Otherwise, add it to the visited table. */ | |
4484 | slot = htab_find_slot (pfd->visited, *tp, INSERT); | |
4485 | if (*slot) | |
4486 | { | |
4487 | *walk_subtrees = 0; | |
4488 | return NULL_TREE; | |
4489 | } | |
4490 | *slot = *tp; | |
4491 | ||
4492 | if (TYPE_P (t) | |
4493 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited)) | |
4494 | return error_mark_node; | |
4495 | ||
4496 | switch (TREE_CODE (t)) | |
4497 | { | |
4498 | case RECORD_TYPE: | |
4499 | if (TYPE_PTRMEMFUNC_P (t)) | |
4500 | break; | |
4501 | /* Fall through. */ | |
4502 | ||
4503 | case UNION_TYPE: | |
4504 | case ENUMERAL_TYPE: | |
4505 | if (!TYPE_TEMPLATE_INFO (t)) | |
4506 | *walk_subtrees = 0; | |
4507 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
4508 | fn, data, pfd->visited)) | |
4509 | return error_mark_node; | |
4510 | break; | |
4511 | ||
4512 | case METHOD_TYPE: | |
4513 | /* Since we're not going to walk subtrees, we have to do this | |
4514 | explicitly here. */ | |
4515 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, | |
4516 | pfd->visited)) | |
4517 | return error_mark_node; | |
4518 | /* Fall through. */ | |
4519 | ||
4520 | case FUNCTION_TYPE: | |
4521 | /* Check the return type. */ | |
4522 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) | |
4523 | return error_mark_node; | |
4524 | ||
4525 | /* Check the parameter types. Since default arguments are not | |
4526 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4527 | contain expressions that involve template parameters. But, | |
4528 | no-one should be looking at them yet. And, once they're | |
4529 | instantiated, they don't contain template parameters, so | |
4530 | there's no point in looking at them then, either. */ | |
4531 | { | |
4532 | tree parm; | |
4533 | ||
4534 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4535 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, | |
4536 | pfd->visited)) | |
4537 | return error_mark_node; | |
4538 | ||
4539 | /* Since we've already handled the TYPE_ARG_TYPES, we don't | |
4540 | want walk_tree walking into them itself. */ | |
4541 | *walk_subtrees = 0; | |
4542 | } | |
4543 | break; | |
4544 | ||
4545 | case TYPEOF_TYPE: | |
4546 | if (for_each_template_parm (TYPE_FIELDS (t), fn, data, | |
4547 | pfd->visited)) | |
4548 | return error_mark_node; | |
4549 | break; | |
4550 | ||
4551 | case FUNCTION_DECL: | |
4552 | case VAR_DECL: | |
4553 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) | |
4554 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, | |
4555 | pfd->visited)) | |
4556 | return error_mark_node; | |
4557 | /* Fall through. */ | |
4558 | ||
4559 | case PARM_DECL: | |
4560 | case CONST_DECL: | |
4561 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
4562 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
4563 | pfd->visited)) | |
4564 | return error_mark_node; | |
4565 | if (DECL_CONTEXT (t) | |
4566 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, | |
4567 | pfd->visited)) | |
4568 | return error_mark_node; | |
4569 | break; | |
4570 | ||
4571 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
4572 | /* Record template parameters such as `T' inside `TT<T>'. */ | |
4573 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited)) | |
4574 | return error_mark_node; | |
4575 | /* Fall through. */ | |
4576 | ||
4577 | case TEMPLATE_TEMPLATE_PARM: | |
4578 | case TEMPLATE_TYPE_PARM: | |
4579 | case TEMPLATE_PARM_INDEX: | |
4580 | if (fn && (*fn)(t, data)) | |
4581 | return error_mark_node; | |
4582 | else if (!fn) | |
4583 | return error_mark_node; | |
4584 | break; | |
4585 | ||
4586 | case TEMPLATE_DECL: | |
4587 | /* A template template parameter is encountered */ | |
4588 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) | |
4589 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) | |
4590 | return error_mark_node; | |
4591 | ||
4592 | /* Already substituted template template parameter */ | |
4593 | *walk_subtrees = 0; | |
4594 | break; | |
4595 | ||
4596 | case TYPENAME_TYPE: | |
4597 | if (!fn | |
4598 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, | |
4599 | data, pfd->visited)) | |
4600 | return error_mark_node; | |
4601 | break; | |
4602 | ||
4603 | case CONSTRUCTOR: | |
4604 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
4605 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE | |
4606 | (TREE_TYPE (t)), fn, data, | |
4607 | pfd->visited)) | |
4608 | return error_mark_node; | |
4609 | break; | |
4610 | ||
4611 | case INDIRECT_REF: | |
4612 | case COMPONENT_REF: | |
4613 | /* If there's no type, then this thing must be some expression | |
4614 | involving template parameters. */ | |
4615 | if (!fn && !TREE_TYPE (t)) | |
4616 | return error_mark_node; | |
4617 | break; | |
4618 | ||
4619 | case MODOP_EXPR: | |
4620 | case CAST_EXPR: | |
4621 | case REINTERPRET_CAST_EXPR: | |
4622 | case CONST_CAST_EXPR: | |
4623 | case STATIC_CAST_EXPR: | |
4624 | case DYNAMIC_CAST_EXPR: | |
4625 | case ARROW_EXPR: | |
4626 | case DOTSTAR_EXPR: | |
4627 | case TYPEID_EXPR: | |
4628 | case LOOKUP_EXPR: | |
4629 | case PSEUDO_DTOR_EXPR: | |
4630 | if (!fn) | |
4631 | return error_mark_node; | |
4632 | break; | |
4633 | ||
4634 | case BASELINK: | |
4635 | /* If we do not handle this case specially, we end up walking | |
4636 | the BINFO hierarchy, which is circular, and therefore | |
4637 | confuses walk_tree. */ | |
4638 | *walk_subtrees = 0; | |
4639 | if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data, | |
4640 | pfd->visited)) | |
4641 | return error_mark_node; | |
4642 | break; | |
4643 | ||
4644 | default: | |
4645 | break; | |
4646 | } | |
4647 | ||
4648 | /* We didn't find any template parameters we liked. */ | |
4649 | return NULL_TREE; | |
4650 | } | |
4651 | ||
4652 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, | |
4653 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
4654 | call FN with the parameter and the DATA. | |
4655 | If FN returns nonzero, the iteration is terminated, and | |
4656 | for_each_template_parm returns 1. Otherwise, the iteration | |
4657 | continues. If FN never returns a nonzero value, the value | |
4658 | returned by for_each_template_parm is 0. If FN is NULL, it is | |
4659 | considered to be the function which always returns 1. */ | |
4660 | ||
4661 | static int | |
4662 | for_each_template_parm (t, fn, data, visited) | |
4663 | tree t; | |
4664 | tree_fn_t fn; | |
4665 | void* data; | |
4666 | htab_t visited; | |
4667 | { | |
4668 | struct pair_fn_data pfd; | |
4669 | int result; | |
4670 | ||
4671 | /* Set up. */ | |
4672 | pfd.fn = fn; | |
4673 | pfd.data = data; | |
4674 | ||
4675 | /* Walk the tree. (Conceptually, we would like to walk without | |
4676 | duplicates, but for_each_template_parm_r recursively calls | |
4677 | for_each_template_parm, so we would need to reorganize a fair | |
4678 | bit to use walk_tree_without_duplicates, so we keep our own | |
4679 | visited list.) */ | |
4680 | if (visited) | |
4681 | pfd.visited = visited; | |
4682 | else | |
4683 | pfd.visited = htab_create (37, htab_hash_pointer, htab_eq_pointer, | |
4684 | NULL); | |
4685 | result = walk_tree (&t, | |
4686 | for_each_template_parm_r, | |
4687 | &pfd, | |
4688 | NULL) != NULL_TREE; | |
4689 | ||
4690 | /* Clean up. */ | |
4691 | if (!visited) | |
4692 | htab_delete (pfd.visited); | |
4693 | ||
4694 | return result; | |
4695 | } | |
4696 | ||
4697 | int | |
4698 | uses_template_parms (t) | |
4699 | tree t; | |
4700 | { | |
4701 | return for_each_template_parm (t, 0, 0, NULL); | |
4702 | } | |
4703 | ||
4704 | static int tinst_depth; | |
4705 | extern int max_tinst_depth; | |
4706 | #ifdef GATHER_STATISTICS | |
4707 | int depth_reached; | |
4708 | #endif | |
4709 | static int tinst_level_tick; | |
4710 | static int last_template_error_tick; | |
4711 | ||
4712 | /* We're starting to instantiate D; record the template instantiation context | |
4713 | for diagnostics and to restore it later. */ | |
4714 | ||
4715 | int | |
4716 | push_tinst_level (d) | |
4717 | tree d; | |
4718 | { | |
4719 | tree new; | |
4720 | ||
4721 | if (tinst_depth >= max_tinst_depth) | |
4722 | { | |
4723 | /* If the instantiation in question still has unbound template parms, | |
4724 | we don't really care if we can't instantiate it, so just return. | |
4725 | This happens with base instantiation for implicit `typename'. */ | |
4726 | if (uses_template_parms (d)) | |
4727 | return 0; | |
4728 | ||
4729 | last_template_error_tick = tinst_level_tick; | |
4730 | error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'", | |
4731 | max_tinst_depth, d); | |
4732 | ||
4733 | print_instantiation_context (); | |
4734 | ||
4735 | return 0; | |
4736 | } | |
4737 | ||
4738 | new = build_expr_wfl (d, input_filename, lineno, 0); | |
4739 | TREE_CHAIN (new) = current_tinst_level; | |
4740 | current_tinst_level = new; | |
4741 | ||
4742 | ++tinst_depth; | |
4743 | #ifdef GATHER_STATISTICS | |
4744 | if (tinst_depth > depth_reached) | |
4745 | depth_reached = tinst_depth; | |
4746 | #endif | |
4747 | ||
4748 | ++tinst_level_tick; | |
4749 | return 1; | |
4750 | } | |
4751 | ||
4752 | /* We're done instantiating this template; return to the instantiation | |
4753 | context. */ | |
4754 | ||
4755 | void | |
4756 | pop_tinst_level () | |
4757 | { | |
4758 | tree old = current_tinst_level; | |
4759 | ||
4760 | /* Restore the filename and line number stashed away when we started | |
4761 | this instantiation. */ | |
4762 | lineno = TINST_LINE (old); | |
4763 | input_filename = TINST_FILE (old); | |
4764 | extract_interface_info (); | |
4765 | ||
4766 | current_tinst_level = TREE_CHAIN (old); | |
4767 | --tinst_depth; | |
4768 | ++tinst_level_tick; | |
4769 | } | |
4770 | ||
4771 | /* We're instantiating a deferred template; restore the template | |
4772 | instantiation context in which the instantiation was requested, which | |
4773 | is one step out from LEVEL. */ | |
4774 | ||
4775 | static void | |
4776 | reopen_tinst_level (level) | |
4777 | tree level; | |
4778 | { | |
4779 | tree t; | |
4780 | ||
4781 | tinst_depth = 0; | |
4782 | for (t = level; t; t = TREE_CHAIN (t)) | |
4783 | ++tinst_depth; | |
4784 | ||
4785 | current_tinst_level = level; | |
4786 | pop_tinst_level (); | |
4787 | } | |
4788 | ||
4789 | /* Return the outermost template instantiation context, for use with | |
4790 | -falt-external-templates. */ | |
4791 | ||
4792 | tree | |
4793 | tinst_for_decl () | |
4794 | { | |
4795 | tree p = current_tinst_level; | |
4796 | ||
4797 | if (p) | |
4798 | for (; TREE_CHAIN (p) ; p = TREE_CHAIN (p)) | |
4799 | ; | |
4800 | return p; | |
4801 | } | |
4802 | ||
4803 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the | |
4804 | vector of template arguments, as for tsubst. | |
4805 | ||
4806 | Returns an appropriate tsubst'd friend declaration. */ | |
4807 | ||
4808 | static tree | |
4809 | tsubst_friend_function (decl, args) | |
4810 | tree decl; | |
4811 | tree args; | |
4812 | { | |
4813 | tree new_friend; | |
4814 | int line = lineno; | |
4815 | const char *file = input_filename; | |
4816 | ||
4817 | lineno = DECL_SOURCE_LINE (decl); | |
4818 | input_filename = DECL_SOURCE_FILE (decl); | |
4819 | ||
4820 | if (TREE_CODE (decl) == FUNCTION_DECL | |
4821 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
4822 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
4823 | /* This was a friend declared with an explicit template | |
4824 | argument list, e.g.: | |
4825 | ||
4826 | friend void f<>(T); | |
4827 | ||
4828 | to indicate that f was a template instantiation, not a new | |
4829 | function declaration. Now, we have to figure out what | |
4830 | instantiation of what template. */ | |
4831 | { | |
4832 | tree template_id, arglist, fns; | |
4833 | tree new_args; | |
4834 | tree tmpl; | |
4835 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); | |
4836 | ||
4837 | /* Friend functions are looked up in the containing namespace scope. | |
4838 | We must enter that scope, to avoid finding member functions of the | |
4839 | current cless with same name. */ | |
4840 | push_nested_namespace (ns); | |
4841 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
4842 | tf_error | tf_warning, NULL_TREE); | |
4843 | pop_nested_namespace (ns); | |
4844 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
4845 | tf_error | tf_warning, NULL_TREE); | |
4846 | template_id = lookup_template_function (fns, arglist); | |
4847 | ||
4848 | new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); | |
4849 | tmpl = determine_specialization (template_id, new_friend, | |
4850 | &new_args, | |
4851 | /*need_member_template=*/0); | |
4852 | new_friend = instantiate_template (tmpl, new_args); | |
4853 | goto done; | |
4854 | } | |
4855 | ||
4856 | new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); | |
4857 | ||
4858 | /* The NEW_FRIEND will look like an instantiation, to the | |
4859 | compiler, but is not an instantiation from the point of view of | |
4860 | the language. For example, we might have had: | |
4861 | ||
4862 | template <class T> struct S { | |
4863 | template <class U> friend void f(T, U); | |
4864 | }; | |
4865 | ||
4866 | Then, in S<int>, template <class U> void f(int, U) is not an | |
4867 | instantiation of anything. */ | |
4868 | DECL_USE_TEMPLATE (new_friend) = 0; | |
4869 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
4870 | { | |
4871 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
4872 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
4873 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
4874 | } | |
4875 | ||
4876 | /* The mangled name for the NEW_FRIEND is incorrect. The function | |
4877 | is not a template instantiation and should not be mangled like | |
4878 | one. Therefore, we forget the mangling here; we'll recompute it | |
4879 | later if we need it. */ | |
4880 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) | |
4881 | { | |
4882 | SET_DECL_RTL (new_friend, NULL_RTX); | |
4883 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); | |
4884 | } | |
4885 | ||
4886 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) | |
4887 | { | |
4888 | tree old_decl; | |
4889 | tree new_friend_template_info; | |
4890 | tree new_friend_result_template_info; | |
4891 | tree ns; | |
4892 | int new_friend_is_defn; | |
4893 | ||
4894 | /* We must save some information from NEW_FRIEND before calling | |
4895 | duplicate decls since that function will free NEW_FRIEND if | |
4896 | possible. */ | |
4897 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
4898 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) | |
4899 | { | |
4900 | /* This declaration is a `primary' template. */ | |
4901 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
4902 | ||
4903 | new_friend_is_defn | |
4904 | = DECL_INITIAL (DECL_TEMPLATE_RESULT (new_friend)) != NULL_TREE; | |
4905 | new_friend_result_template_info | |
4906 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); | |
4907 | } | |
4908 | else | |
4909 | { | |
4910 | new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE; | |
4911 | new_friend_result_template_info = NULL_TREE; | |
4912 | } | |
4913 | ||
4914 | /* Inside pushdecl_namespace_level, we will push into the | |
4915 | current namespace. However, the friend function should go | |
4916 | into the namespace of the template. */ | |
4917 | ns = decl_namespace_context (new_friend); | |
4918 | push_nested_namespace (ns); | |
4919 | old_decl = pushdecl_namespace_level (new_friend); | |
4920 | pop_nested_namespace (ns); | |
4921 | ||
4922 | if (old_decl != new_friend) | |
4923 | { | |
4924 | /* This new friend declaration matched an existing | |
4925 | declaration. For example, given: | |
4926 | ||
4927 | template <class T> void f(T); | |
4928 | template <class U> class C { | |
4929 | template <class T> friend void f(T) {} | |
4930 | }; | |
4931 | ||
4932 | the friend declaration actually provides the definition | |
4933 | of `f', once C has been instantiated for some type. So, | |
4934 | old_decl will be the out-of-class template declaration, | |
4935 | while new_friend is the in-class definition. | |
4936 | ||
4937 | But, if `f' was called before this point, the | |
4938 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
4939 | to `T' but not to `U', references to which might appear | |
4940 | in the definition of `f'. Previously, the most general | |
4941 | template for an instantiation of `f' was the out-of-class | |
4942 | version; now it is the in-class version. Therefore, we | |
4943 | run through all specialization of `f', adding to their | |
4944 | DECL_TI_ARGS appropriately. In particular, they need a | |
4945 | new set of outer arguments, corresponding to the | |
4946 | arguments for this class instantiation. | |
4947 | ||
4948 | The same situation can arise with something like this: | |
4949 | ||
4950 | friend void f(int); | |
4951 | template <class T> class C { | |
4952 | friend void f(T) {} | |
4953 | }; | |
4954 | ||
4955 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
4956 | in the class. */ | |
4957 | ||
4958 | if (!new_friend_is_defn) | |
4959 | /* On the other hand, if the in-class declaration does | |
4960 | *not* provide a definition, then we don't want to alter | |
4961 | existing definitions. We can just leave everything | |
4962 | alone. */ | |
4963 | ; | |
4964 | else | |
4965 | { | |
4966 | /* Overwrite whatever template info was there before, if | |
4967 | any, with the new template information pertaining to | |
4968 | the declaration. */ | |
4969 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
4970 | ||
4971 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
4972 | /* duplicate_decls will take care of this case. */ | |
4973 | ; | |
4974 | else | |
4975 | { | |
4976 | tree t; | |
4977 | tree new_friend_args; | |
4978 | ||
4979 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) | |
4980 | = new_friend_result_template_info; | |
4981 | ||
4982 | new_friend_args = TI_ARGS (new_friend_template_info); | |
4983 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
4984 | t != NULL_TREE; | |
4985 | t = TREE_CHAIN (t)) | |
4986 | { | |
4987 | tree spec = TREE_VALUE (t); | |
4988 | ||
4989 | DECL_TI_ARGS (spec) | |
4990 | = add_outermost_template_args (new_friend_args, | |
4991 | DECL_TI_ARGS (spec)); | |
4992 | } | |
4993 | ||
4994 | /* Now, since specializations are always supposed to | |
4995 | hang off of the most general template, we must move | |
4996 | them. */ | |
4997 | t = most_general_template (old_decl); | |
4998 | if (t != old_decl) | |
4999 | { | |
5000 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
5001 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
5002 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
5003 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
5004 | } | |
5005 | } | |
5006 | } | |
5007 | ||
5008 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
5009 | by duplicate_decls. */ | |
5010 | new_friend = old_decl; | |
5011 | } | |
5012 | } | |
5013 | else if (COMPLETE_TYPE_P (DECL_CONTEXT (new_friend))) | |
5014 | { | |
5015 | /* Check to see that the declaration is really present, and, | |
5016 | possibly obtain an improved declaration. */ | |
5017 | tree fn = check_classfn (DECL_CONTEXT (new_friend), | |
5018 | new_friend); | |
5019 | ||
5020 | if (fn) | |
5021 | new_friend = fn; | |
5022 | } | |
5023 | ||
5024 | done: | |
5025 | lineno = line; | |
5026 | input_filename = file; | |
5027 | return new_friend; | |
5028 | } | |
5029 | ||
5030 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of | |
5031 | template arguments, as for tsubst. | |
5032 | ||
5033 | Returns an appropriate tsubst'd friend type or error_mark_node on | |
5034 | failure. */ | |
5035 | ||
5036 | static tree | |
5037 | tsubst_friend_class (friend_tmpl, args) | |
5038 | tree friend_tmpl; | |
5039 | tree args; | |
5040 | { | |
5041 | tree friend_type; | |
5042 | tree tmpl; | |
5043 | tree context; | |
5044 | ||
5045 | context = DECL_CONTEXT (friend_tmpl); | |
5046 | ||
5047 | if (context) | |
5048 | { | |
5049 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
5050 | push_nested_namespace (context); | |
5051 | else | |
5052 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); | |
5053 | } | |
5054 | ||
5055 | /* First, we look for a class template. */ | |
5056 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); | |
5057 | ||
5058 | /* But, if we don't find one, it might be because we're in a | |
5059 | situation like this: | |
5060 | ||
5061 | template <class T> | |
5062 | struct S { | |
5063 | template <class U> | |
5064 | friend struct S; | |
5065 | }; | |
5066 | ||
5067 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
5068 | for `S<int>', not the TEMPLATE_DECL. */ | |
5069 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
5070 | { | |
5071 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); | |
5072 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
5073 | } | |
5074 | ||
5075 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) | |
5076 | { | |
5077 | /* The friend template has already been declared. Just | |
5078 | check to see that the declarations match, and install any new | |
5079 | default parameters. We must tsubst the default parameters, | |
5080 | of course. We only need the innermost template parameters | |
5081 | because that is all that redeclare_class_template will look | |
5082 | at. */ | |
5083 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) | |
5084 | > TMPL_ARGS_DEPTH (args)) | |
5085 | { | |
5086 | tree parms; | |
5087 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
5088 | args, tf_error | tf_warning); | |
5089 | redeclare_class_template (TREE_TYPE (tmpl), parms); | |
5090 | } | |
5091 | ||
5092 | friend_type = TREE_TYPE (tmpl); | |
5093 | } | |
5094 | else | |
5095 | { | |
5096 | /* The friend template has not already been declared. In this | |
5097 | case, the instantiation of the template class will cause the | |
5098 | injection of this template into the global scope. */ | |
5099 | tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE); | |
5100 | ||
5101 | /* The new TMPL is not an instantiation of anything, so we | |
5102 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
5103 | the new type because that is supposed to be the corresponding | |
5104 | template decl, i.e., TMPL. */ | |
5105 | DECL_USE_TEMPLATE (tmpl) = 0; | |
5106 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
5107 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
5108 | ||
5109 | /* Inject this template into the global scope. */ | |
5110 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
5111 | } | |
5112 | ||
5113 | if (context) | |
5114 | { | |
5115 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
5116 | pop_nested_namespace (context); | |
5117 | else | |
5118 | pop_nested_class (); | |
5119 | } | |
5120 | ||
5121 | return friend_type; | |
5122 | } | |
5123 | ||
5124 | /* Returns zero if TYPE cannot be completed later due to circularity. | |
5125 | Otherwise returns one. */ | |
5126 | ||
5127 | static int | |
5128 | can_complete_type_without_circularity (type) | |
5129 | tree type; | |
5130 | { | |
5131 | if (type == NULL_TREE || type == error_mark_node) | |
5132 | return 0; | |
5133 | else if (COMPLETE_TYPE_P (type)) | |
5134 | return 1; | |
5135 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
5136 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
5137 | else if (CLASS_TYPE_P (type) && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
5138 | return 0; | |
5139 | else | |
5140 | return 1; | |
5141 | } | |
5142 | ||
5143 | tree | |
5144 | instantiate_class_template (type) | |
5145 | tree type; | |
5146 | { | |
5147 | tree template, args, pattern, t, member; | |
5148 | tree typedecl; | |
5149 | ||
5150 | if (type == error_mark_node) | |
5151 | return error_mark_node; | |
5152 | ||
5153 | if (TYPE_BEING_DEFINED (type) || COMPLETE_TYPE_P (type)) | |
5154 | return type; | |
5155 | ||
5156 | /* Figure out which template is being instantiated. */ | |
5157 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); | |
5158 | my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279); | |
5159 | ||
5160 | /* Figure out which arguments are being used to do the | |
5161 | instantiation. */ | |
5162 | args = CLASSTYPE_TI_ARGS (type); | |
5163 | ||
5164 | /* Determine what specialization of the original template to | |
5165 | instantiate. */ | |
5166 | t = most_specialized_class (template, args); | |
5167 | if (t == error_mark_node) | |
5168 | { | |
5169 | const char *str = "candidates are:"; | |
5170 | error ("ambiguous class template instantiation for `%#T'", type); | |
5171 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; | |
5172 | t = TREE_CHAIN (t)) | |
5173 | { | |
5174 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
5175 | args)) | |
5176 | { | |
5177 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); | |
5178 | str = " "; | |
5179 | } | |
5180 | } | |
5181 | TYPE_BEING_DEFINED (type) = 1; | |
5182 | return error_mark_node; | |
5183 | } | |
5184 | ||
5185 | if (t) | |
5186 | pattern = TREE_TYPE (t); | |
5187 | else | |
5188 | pattern = TREE_TYPE (template); | |
5189 | ||
5190 | /* If the template we're instantiating is incomplete, then clearly | |
5191 | there's nothing we can do. */ | |
5192 | if (!COMPLETE_TYPE_P (pattern)) | |
5193 | return type; | |
5194 | ||
5195 | /* If we've recursively instantiated too many templates, stop. */ | |
5196 | if (! push_tinst_level (type)) | |
5197 | return type; | |
5198 | ||
5199 | /* Now we're really doing the instantiation. Mark the type as in | |
5200 | the process of being defined. */ | |
5201 | TYPE_BEING_DEFINED (type) = 1; | |
5202 | ||
5203 | maybe_push_to_top_level (uses_template_parms (type)); | |
5204 | ||
5205 | if (t) | |
5206 | { | |
5207 | /* This TYPE is actually an instantiation of a partial | |
5208 | specialization. We replace the innermost set of ARGS with | |
5209 | the arguments appropriate for substitution. For example, | |
5210 | given: | |
5211 | ||
5212 | template <class T> struct S {}; | |
5213 | template <class T> struct S<T*> {}; | |
5214 | ||
5215 | and supposing that we are instantiating S<int*>, ARGS will | |
5216 | present be {int*} but we need {int}. */ | |
5217 | tree inner_args | |
5218 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
5219 | args); | |
5220 | ||
5221 | /* If there were multiple levels in ARGS, replacing the | |
5222 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
5223 | want, so we make a copy first. */ | |
5224 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
5225 | { | |
5226 | args = copy_node (args); | |
5227 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
5228 | } | |
5229 | else | |
5230 | args = inner_args; | |
5231 | } | |
5232 | ||
5233 | if (flag_external_templates) | |
5234 | { | |
5235 | if (flag_alt_external_templates) | |
5236 | { | |
5237 | CLASSTYPE_INTERFACE_ONLY (type) = interface_only; | |
5238 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown); | |
5239 | } | |
5240 | else | |
5241 | { | |
5242 | CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern); | |
5243 | SET_CLASSTYPE_INTERFACE_UNKNOWN_X | |
5244 | (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern)); | |
5245 | } | |
5246 | } | |
5247 | else | |
5248 | { | |
5249 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); | |
5250 | } | |
5251 | ||
5252 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); | |
5253 | TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern); | |
5254 | TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern); | |
5255 | TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern); | |
5256 | TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern); | |
5257 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); | |
5258 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
5259 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); | |
5260 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); | |
5261 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
5262 | TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern); | |
5263 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); | |
5264 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
5265 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); | |
5266 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
5267 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type) | |
5268 | = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern); | |
5269 | TYPE_USES_MULTIPLE_INHERITANCE (type) | |
5270 | = TYPE_USES_MULTIPLE_INHERITANCE (pattern); | |
5271 | TYPE_USES_VIRTUAL_BASECLASSES (type) | |
5272 | = TYPE_USES_VIRTUAL_BASECLASSES (pattern); | |
5273 | TYPE_PACKED (type) = TYPE_PACKED (pattern); | |
5274 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
5275 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); | |
5276 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ | |
5277 | if (ANON_AGGR_TYPE_P (pattern)) | |
5278 | SET_ANON_AGGR_TYPE_P (type); | |
5279 | ||
5280 | if (TYPE_BINFO_BASETYPES (pattern)) | |
5281 | { | |
5282 | tree base_list = NULL_TREE; | |
5283 | tree pbases = TYPE_BINFO_BASETYPES (pattern); | |
5284 | int i; | |
5285 | ||
5286 | /* Substitute into each of the bases to determine the actual | |
5287 | basetypes. */ | |
5288 | for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i) | |
5289 | { | |
5290 | tree base; | |
5291 | tree access; | |
5292 | tree pbase; | |
5293 | ||
5294 | pbase = TREE_VEC_ELT (pbases, i); | |
5295 | ||
5296 | /* Substitute to figure out the base class. */ | |
5297 | base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE); | |
5298 | if (base == error_mark_node) | |
5299 | continue; | |
5300 | ||
5301 | /* Calculate the correct access node. */ | |
5302 | if (TREE_VIA_VIRTUAL (pbase)) | |
5303 | { | |
5304 | if (TREE_VIA_PUBLIC (pbase)) | |
5305 | access = access_public_virtual_node; | |
5306 | else if (TREE_VIA_PROTECTED (pbase)) | |
5307 | access = access_protected_virtual_node; | |
5308 | else | |
5309 | access = access_private_virtual_node; | |
5310 | } | |
5311 | else | |
5312 | { | |
5313 | if (TREE_VIA_PUBLIC (pbase)) | |
5314 | access = access_public_node; | |
5315 | else if (TREE_VIA_PROTECTED (pbase)) | |
5316 | access = access_protected_node; | |
5317 | else | |
5318 | access = access_private_node; | |
5319 | } | |
5320 | ||
5321 | base_list = tree_cons (access, base, base_list); | |
5322 | } | |
5323 | ||
5324 | /* The list is now in reverse order; correct that. */ | |
5325 | base_list = nreverse (base_list); | |
5326 | ||
5327 | /* Now call xref_basetypes to set up all the base-class | |
5328 | information. */ | |
5329 | xref_basetypes (type, base_list); | |
5330 | } | |
5331 | ||
5332 | /* Now that our base classes are set up, enter the scope of the | |
5333 | class, so that name lookups into base classes, etc. will work | |
5334 | correctly. This is precisely analogous to what we do in | |
5335 | begin_class_definition when defining an ordinary non-template | |
5336 | class. */ | |
5337 | pushclass (type, true); | |
5338 | ||
5339 | /* Now members are processed in the order of declaration. */ | |
5340 | for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member)) | |
5341 | { | |
5342 | tree t = TREE_VALUE (member); | |
5343 | ||
5344 | if (TREE_PURPOSE (member)) | |
5345 | { | |
5346 | if (TYPE_P (t)) | |
5347 | { | |
5348 | /* Build new CLASSTYPE_TAGS. */ | |
5349 | ||
5350 | tree tag = t; | |
5351 | tree name = TYPE_IDENTIFIER (tag); | |
5352 | tree newtag; | |
5353 | ||
5354 | newtag = tsubst (tag, args, tf_error, NULL_TREE); | |
5355 | my_friendly_assert (newtag != error_mark_node, 20010206); | |
5356 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) | |
5357 | { | |
5358 | if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag)) | |
5359 | /* Unfortunately, lookup_template_class sets | |
5360 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
5361 | instantiation (i.e., for the type of a member template | |
5362 | class nested within a template class.) This behavior is | |
5363 | required for maybe_process_partial_specialization to work | |
5364 | correctly, but is not accurate in this case; the TAG is not | |
5365 | an instantiation of anything. (The corresponding | |
5366 | TEMPLATE_DECL is an instantiation, but the TYPE is not.) */ | |
5367 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; | |
5368 | ||
5369 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
5370 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
5371 | pushtag calling push_template_decl. We don't have to do | |
5372 | this for enums because it will already have been done in | |
5373 | tsubst_enum. */ | |
5374 | if (name) | |
5375 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
5376 | pushtag (name, newtag, /*globalize=*/0); | |
5377 | } | |
5378 | } | |
5379 | else if (TREE_CODE (t) == FUNCTION_DECL | |
5380 | || DECL_FUNCTION_TEMPLATE_P (t)) | |
5381 | { | |
5382 | /* Build new TYPE_METHODS. */ | |
5383 | ||
5384 | tree r = tsubst (t, args, tf_error, NULL_TREE); | |
5385 | set_current_access_from_decl (r); | |
5386 | grok_special_member_properties (r); | |
5387 | finish_member_declaration (r); | |
5388 | } | |
5389 | else | |
5390 | { | |
5391 | /* Build new TYPE_FIELDS. */ | |
5392 | ||
5393 | if (TREE_CODE (t) != CONST_DECL) | |
5394 | { | |
5395 | tree r; | |
5396 | ||
5397 | /* The the file and line for this declaration, to assist | |
5398 | in error message reporting. Since we called | |
5399 | push_tinst_level above, we don't need to restore these. */ | |
5400 | lineno = DECL_SOURCE_LINE (t); | |
5401 | input_filename = DECL_SOURCE_FILE (t); | |
5402 | ||
5403 | r = tsubst (t, args, tf_error | tf_warning, NULL_TREE); | |
5404 | if (TREE_CODE (r) == VAR_DECL) | |
5405 | { | |
5406 | tree init; | |
5407 | ||
5408 | if (DECL_INITIALIZED_IN_CLASS_P (r)) | |
5409 | init = tsubst_expr (DECL_INITIAL (t), args, | |
5410 | tf_error | tf_warning, NULL_TREE); | |
5411 | else | |
5412 | init = NULL_TREE; | |
5413 | ||
5414 | finish_static_data_member_decl (r, init, | |
5415 | /*asmspec_tree=*/NULL_TREE, | |
5416 | /*flags=*/0); | |
5417 | ||
5418 | if (DECL_INITIALIZED_IN_CLASS_P (r)) | |
5419 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5420 | } | |
5421 | else if (TREE_CODE (r) == FIELD_DECL) | |
5422 | { | |
5423 | /* Determine whether R has a valid type and can be | |
5424 | completed later. If R is invalid, then it is | |
5425 | replaced by error_mark_node so that it will not be | |
5426 | added to TYPE_FIELDS. */ | |
5427 | tree rtype = TREE_TYPE (r); | |
5428 | if (can_complete_type_without_circularity (rtype)) | |
5429 | complete_type (rtype); | |
5430 | ||
5431 | if (!COMPLETE_TYPE_P (rtype)) | |
5432 | { | |
5433 | cxx_incomplete_type_error (r, rtype); | |
5434 | r = error_mark_node; | |
5435 | } | |
5436 | } | |
5437 | ||
5438 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, | |
5439 | such a thing will already have been added to the field | |
5440 | list by tsubst_enum in finish_member_declaration in the | |
5441 | CLASSTYPE_TAGS case above. */ | |
5442 | if (!(TREE_CODE (r) == TYPE_DECL | |
5443 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
5444 | && TYPE_CONTEXT (TREE_TYPE (r)) == type)) | |
5445 | { | |
5446 | set_current_access_from_decl (r); | |
5447 | finish_member_declaration (r); | |
5448 | } | |
5449 | } | |
5450 | } | |
5451 | } | |
5452 | else | |
5453 | { | |
5454 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
5455 | { | |
5456 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
5457 | ||
5458 | tree friend_type = t; | |
5459 | tree new_friend_type; | |
5460 | ||
5461 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5462 | new_friend_type = tsubst_friend_class (friend_type, args); | |
5463 | else if (uses_template_parms (friend_type)) | |
5464 | new_friend_type = tsubst (friend_type, args, | |
5465 | tf_error | tf_warning, NULL_TREE); | |
5466 | else | |
5467 | { | |
5468 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); | |
5469 | ||
5470 | /* The call to xref_tag_from_type does injection for friend | |
5471 | classes. */ | |
5472 | push_nested_namespace (ns); | |
5473 | new_friend_type = | |
5474 | xref_tag_from_type (friend_type, NULL_TREE, 1); | |
5475 | pop_nested_namespace (ns); | |
5476 | } | |
5477 | ||
5478 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5479 | /* Trick make_friend_class into realizing that the friend | |
5480 | we're adding is a template, not an ordinary class. It's | |
5481 | important that we use make_friend_class since it will | |
5482 | perform some error-checking and output cross-reference | |
5483 | information. */ | |
5484 | ++processing_template_decl; | |
5485 | ||
5486 | if (new_friend_type != error_mark_node) | |
5487 | make_friend_class (type, new_friend_type); | |
5488 | ||
5489 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5490 | --processing_template_decl; | |
5491 | } | |
5492 | else | |
5493 | { | |
5494 | /* Build new DECL_FRIENDLIST. */ | |
5495 | ||
5496 | add_friend (type, | |
5497 | tsubst_friend_function (t, args)); | |
5498 | } | |
5499 | } | |
5500 | } | |
5501 | ||
5502 | /* Set the file and line number information to whatever is given for | |
5503 | the class itself. This puts error messages involving generated | |
5504 | implicit functions at a predictable point, and the same point | |
5505 | that would be used for non-template classes. */ | |
5506 | typedecl = TYPE_MAIN_DECL (type); | |
5507 | lineno = DECL_SOURCE_LINE (typedecl); | |
5508 | input_filename = DECL_SOURCE_FILE (typedecl); | |
5509 | ||
5510 | unreverse_member_declarations (type); | |
5511 | finish_struct_1 (type); | |
5512 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
5513 | ||
5514 | /* Clear this now so repo_template_used is happy. */ | |
5515 | TYPE_BEING_DEFINED (type) = 0; | |
5516 | repo_template_used (type); | |
5517 | ||
5518 | /* Now that the class is complete, instantiate default arguments for | |
5519 | any member functions. We don't do this earlier because the | |
5520 | default arguments may reference members of the class. */ | |
5521 | if (!PRIMARY_TEMPLATE_P (template)) | |
5522 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
5523 | if (TREE_CODE (t) == FUNCTION_DECL | |
5524 | /* Implicitly generated member functions will not have template | |
5525 | information; they are not instantiations, but instead are | |
5526 | created "fresh" for each instantiation. */ | |
5527 | && DECL_TEMPLATE_INFO (t)) | |
5528 | tsubst_default_arguments (t); | |
5529 | ||
5530 | popclass (); | |
5531 | pop_from_top_level (); | |
5532 | pop_tinst_level (); | |
5533 | ||
5534 | if (TYPE_CONTAINS_VPTR_P (type)) | |
5535 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); | |
5536 | ||
5537 | return type; | |
5538 | } | |
5539 | ||
5540 | static int | |
5541 | list_eq (t1, t2) | |
5542 | tree t1, t2; | |
5543 | { | |
5544 | if (t1 == NULL_TREE) | |
5545 | return t2 == NULL_TREE; | |
5546 | if (t2 == NULL_TREE) | |
5547 | return 0; | |
5548 | /* Don't care if one declares its arg const and the other doesn't -- the | |
5549 | main variant of the arg type is all that matters. */ | |
5550 | if (TYPE_MAIN_VARIANT (TREE_VALUE (t1)) | |
5551 | != TYPE_MAIN_VARIANT (TREE_VALUE (t2))) | |
5552 | return 0; | |
5553 | return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2)); | |
5554 | } | |
5555 | ||
5556 | /* If arg is a non-type template parameter that does not depend on template | |
5557 | arguments, fold it like we weren't in the body of a template. */ | |
5558 | ||
5559 | static tree | |
5560 | maybe_fold_nontype_arg (arg) | |
5561 | tree arg; | |
5562 | { | |
5563 | if (arg && !TYPE_P (arg) && !uses_template_parms (arg)) | |
5564 | { | |
5565 | /* Sometimes, one of the args was an expression involving a | |
5566 | template constant parameter, like N - 1. Now that we've | |
5567 | tsubst'd, we might have something like 2 - 1. This will | |
5568 | confuse lookup_template_class, so we do constant folding | |
5569 | here. We have to unset processing_template_decl, to | |
5570 | fool build_expr_from_tree() into building an actual | |
5571 | tree. */ | |
5572 | ||
5573 | /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already | |
5574 | as simple as it's going to get, and trying to reprocess | |
5575 | the trees will break. */ | |
5576 | if (!TREE_TYPE (arg)) | |
5577 | { | |
5578 | int saved_processing_template_decl = processing_template_decl; | |
5579 | processing_template_decl = 0; | |
5580 | arg = build_expr_from_tree (arg); | |
5581 | processing_template_decl = saved_processing_template_decl; | |
5582 | } | |
5583 | ||
5584 | arg = fold (arg); | |
5585 | } | |
5586 | return arg; | |
5587 | } | |
5588 | ||
5589 | /* Substitute ARGS into the vector of template arguments T. */ | |
5590 | ||
5591 | static tree | |
5592 | tsubst_template_arg_vector (t, args, complain) | |
5593 | tree t; | |
5594 | tree args; | |
5595 | tsubst_flags_t complain; | |
5596 | { | |
5597 | int len = TREE_VEC_LENGTH (t), need_new = 0, i; | |
5598 | tree *elts = (tree *) alloca (len * sizeof (tree)); | |
5599 | ||
5600 | memset ((char *) elts, 0, len * sizeof (tree)); | |
5601 | ||
5602 | for (i = 0; i < len; i++) | |
5603 | { | |
5604 | if (TREE_VEC_ELT (t, i) != NULL_TREE | |
5605 | && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC) | |
5606 | elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), | |
5607 | args, complain); | |
5608 | else | |
5609 | elts[i] = maybe_fold_nontype_arg | |
5610 | (tsubst_expr (TREE_VEC_ELT (t, i), args, complain, | |
5611 | NULL_TREE)); | |
5612 | ||
5613 | if (elts[i] == error_mark_node) | |
5614 | return error_mark_node; | |
5615 | ||
5616 | if (elts[i] != TREE_VEC_ELT (t, i)) | |
5617 | need_new = 1; | |
5618 | } | |
5619 | ||
5620 | if (!need_new) | |
5621 | return t; | |
5622 | ||
5623 | t = make_tree_vec (len); | |
5624 | for (i = 0; i < len; i++) | |
5625 | TREE_VEC_ELT (t, i) = elts[i]; | |
5626 | ||
5627 | return t; | |
5628 | } | |
5629 | ||
5630 | /* Return the result of substituting ARGS into the template parameters | |
5631 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5632 | PARMS, then the result will contain n levels of PARMS. For | |
5633 | example, if PARMS is `template <class T> template <class U> | |
5634 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5635 | result will be `template <int*, double, class V>'. */ | |
5636 | ||
5637 | static tree | |
5638 | tsubst_template_parms (parms, args, complain) | |
5639 | tree parms; | |
5640 | tree args; | |
5641 | tsubst_flags_t complain; | |
5642 | { | |
5643 | tree r = NULL_TREE; | |
5644 | tree* new_parms; | |
5645 | ||
5646 | for (new_parms = &r; | |
5647 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5648 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5649 | parms = TREE_CHAIN (parms)) | |
5650 | { | |
5651 | tree new_vec = | |
5652 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5653 | int i; | |
5654 | ||
5655 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5656 | { | |
5657 | tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
5658 | tree default_value = TREE_PURPOSE (tuple); | |
5659 | tree parm_decl = TREE_VALUE (tuple); | |
5660 | ||
5661 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
5662 | default_value = tsubst_expr (default_value, args, | |
5663 | complain, NULL_TREE); | |
5664 | tuple = build_tree_list (maybe_fold_nontype_arg (default_value), | |
5665 | parm_decl); | |
5666 | TREE_VEC_ELT (new_vec, i) = tuple; | |
5667 | } | |
5668 | ||
5669 | *new_parms = | |
5670 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
5671 | - TMPL_ARGS_DEPTH (args)), | |
5672 | new_vec, NULL_TREE); | |
5673 | } | |
5674 | ||
5675 | return r; | |
5676 | } | |
5677 | ||
5678 | /* Substitute the ARGS into the indicated aggregate (or enumeration) | |
5679 | type T. If T is not an aggregate or enumeration type, it is | |
5680 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
5681 | ENTERING_SCOPE is nonzero, T is the context for a template which | |
5682 | we are presently tsubst'ing. Return the substituted value. */ | |
5683 | ||
5684 | static tree | |
5685 | tsubst_aggr_type (t, args, complain, in_decl, entering_scope) | |
5686 | tree t; | |
5687 | tree args; | |
5688 | tsubst_flags_t complain; | |
5689 | tree in_decl; | |
5690 | int entering_scope; | |
5691 | { | |
5692 | if (t == NULL_TREE) | |
5693 | return NULL_TREE; | |
5694 | ||
5695 | switch (TREE_CODE (t)) | |
5696 | { | |
5697 | case RECORD_TYPE: | |
5698 | if (TYPE_PTRMEMFUNC_P (t)) | |
5699 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); | |
5700 | ||
5701 | /* else fall through */ | |
5702 | case ENUMERAL_TYPE: | |
5703 | case UNION_TYPE: | |
5704 | if (TYPE_TEMPLATE_INFO (t)) | |
5705 | { | |
5706 | tree argvec; | |
5707 | tree context; | |
5708 | tree r; | |
5709 | ||
5710 | /* First, determine the context for the type we are looking | |
5711 | up. */ | |
5712 | if (TYPE_CONTEXT (t) != NULL_TREE) | |
5713 | context = tsubst_aggr_type (TYPE_CONTEXT (t), args, | |
5714 | complain, | |
5715 | in_decl, /*entering_scope=*/1); | |
5716 | else | |
5717 | context = NULL_TREE; | |
5718 | ||
5719 | /* Then, figure out what arguments are appropriate for the | |
5720 | type we are trying to find. For example, given: | |
5721 | ||
5722 | template <class T> struct S; | |
5723 | template <class T, class U> void f(T, U) { S<U> su; } | |
5724 | ||
5725 | and supposing that we are instantiating f<int, double>, | |
5726 | then our ARGS will be {int, double}, but, when looking up | |
5727 | S we only want {double}. */ | |
5728 | argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args, | |
5729 | complain); | |
5730 | if (argvec == error_mark_node) | |
5731 | return error_mark_node; | |
5732 | ||
5733 | r = lookup_template_class (t, argvec, in_decl, context, | |
5734 | entering_scope, complain); | |
5735 | ||
5736 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
5737 | } | |
5738 | else | |
5739 | /* This is not a template type, so there's nothing to do. */ | |
5740 | return t; | |
5741 | ||
5742 | default: | |
5743 | return tsubst (t, args, complain, in_decl); | |
5744 | } | |
5745 | } | |
5746 | ||
5747 | /* Substitute into the default argument ARG (a default argument for | |
5748 | FN), which has the indicated TYPE. */ | |
5749 | ||
5750 | tree | |
5751 | tsubst_default_argument (fn, type, arg) | |
5752 | tree fn; | |
5753 | tree type; | |
5754 | tree arg; | |
5755 | { | |
5756 | /* This default argument came from a template. Instantiate the | |
5757 | default argument here, not in tsubst. In the case of | |
5758 | something like: | |
5759 | ||
5760 | template <class T> | |
5761 | struct S { | |
5762 | static T t(); | |
5763 | void f(T = t()); | |
5764 | }; | |
5765 | ||
5766 | we must be careful to do name lookup in the scope of S<T>, | |
5767 | rather than in the current class. | |
5768 | ||
5769 | ??? current_class_type affects a lot more than name lookup. This is | |
5770 | very fragile. Fortunately, it will go away when we do 2-phase name | |
5771 | binding properly. */ | |
5772 | ||
5773 | /* FN is already the desired FUNCTION_DECL. */ | |
5774 | push_access_scope (fn); | |
5775 | ||
5776 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), | |
5777 | tf_error | tf_warning, NULL_TREE); | |
5778 | ||
5779 | pop_access_scope (fn); | |
5780 | ||
5781 | /* Make sure the default argument is reasonable. */ | |
5782 | arg = check_default_argument (type, arg); | |
5783 | ||
5784 | return arg; | |
5785 | } | |
5786 | ||
5787 | /* Substitute into all the default arguments for FN. */ | |
5788 | ||
5789 | static void | |
5790 | tsubst_default_arguments (fn) | |
5791 | tree fn; | |
5792 | { | |
5793 | tree arg; | |
5794 | tree tmpl_args; | |
5795 | ||
5796 | tmpl_args = DECL_TI_ARGS (fn); | |
5797 | ||
5798 | /* If this function is not yet instantiated, we certainly don't need | |
5799 | its default arguments. */ | |
5800 | if (uses_template_parms (tmpl_args)) | |
5801 | return; | |
5802 | ||
5803 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
5804 | arg; | |
5805 | arg = TREE_CHAIN (arg)) | |
5806 | if (TREE_PURPOSE (arg)) | |
5807 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, | |
5808 | TREE_VALUE (arg), | |
5809 | TREE_PURPOSE (arg)); | |
5810 | } | |
5811 | ||
5812 | /* Substitute the ARGS into the T, which is a _DECL. TYPE is the | |
5813 | (already computed) substitution of ARGS into TREE_TYPE (T), if | |
5814 | appropriate. Return the result of the substitution. Issue error | |
5815 | and warning messages under control of COMPLAIN. */ | |
5816 | ||
5817 | static tree | |
5818 | tsubst_decl (t, args, type, complain) | |
5819 | tree t; | |
5820 | tree args; | |
5821 | tree type; | |
5822 | tsubst_flags_t complain; | |
5823 | { | |
5824 | int saved_lineno; | |
5825 | const char *saved_filename; | |
5826 | tree r = NULL_TREE; | |
5827 | tree in_decl = t; | |
5828 | ||
5829 | /* Set the filename and linenumber to improve error-reporting. */ | |
5830 | saved_lineno = lineno; | |
5831 | saved_filename = input_filename; | |
5832 | lineno = DECL_SOURCE_LINE (t); | |
5833 | input_filename = DECL_SOURCE_FILE (t); | |
5834 | ||
5835 | switch (TREE_CODE (t)) | |
5836 | { | |
5837 | case TEMPLATE_DECL: | |
5838 | { | |
5839 | /* We can get here when processing a member template function | |
5840 | of a template class. */ | |
5841 | tree decl = DECL_TEMPLATE_RESULT (t); | |
5842 | tree spec; | |
5843 | int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t); | |
5844 | ||
5845 | if (!is_template_template_parm) | |
5846 | { | |
5847 | /* We might already have an instance of this template. | |
5848 | The ARGS are for the surrounding class type, so the | |
5849 | full args contain the tsubst'd args for the context, | |
5850 | plus the innermost args from the template decl. */ | |
5851 | tree tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
5852 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
5853 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
5854 | tree full_args; | |
5855 | ||
5856 | full_args = tsubst_template_arg_vector (tmpl_args, args, | |
5857 | complain); | |
5858 | ||
5859 | /* tsubst_template_arg_vector doesn't copy the vector if | |
5860 | nothing changed. But, *something* should have | |
5861 | changed. */ | |
5862 | my_friendly_assert (full_args != tmpl_args, 0); | |
5863 | ||
5864 | spec = retrieve_specialization (t, full_args); | |
5865 | if (spec != NULL_TREE) | |
5866 | { | |
5867 | r = spec; | |
5868 | break; | |
5869 | } | |
5870 | } | |
5871 | ||
5872 | /* Make a new template decl. It will be similar to the | |
5873 | original, but will record the current template arguments. | |
5874 | We also create a new function declaration, which is just | |
5875 | like the old one, but points to this new template, rather | |
5876 | than the old one. */ | |
5877 | r = copy_decl (t); | |
5878 | my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0); | |
5879 | TREE_CHAIN (r) = NULL_TREE; | |
5880 | ||
5881 | if (is_template_template_parm) | |
5882 | { | |
5883 | tree new_decl = tsubst (decl, args, complain, in_decl); | |
5884 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
5885 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5886 | break; | |
5887 | } | |
5888 | ||
5889 | DECL_CONTEXT (r) | |
5890 | = tsubst_aggr_type (DECL_CONTEXT (t), args, | |
5891 | complain, in_decl, | |
5892 | /*entering_scope=*/1); | |
5893 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); | |
5894 | ||
5895 | if (TREE_CODE (decl) == TYPE_DECL) | |
5896 | { | |
5897 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
5898 | TREE_TYPE (r) = new_type; | |
5899 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
5900 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); | |
5901 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); | |
5902 | } | |
5903 | else | |
5904 | { | |
5905 | tree new_decl = tsubst (decl, args, complain, in_decl); | |
5906 | ||
5907 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
5908 | DECL_TI_TEMPLATE (new_decl) = r; | |
5909 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
5910 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
5911 | } | |
5912 | ||
5913 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
5914 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
5915 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
5916 | ||
5917 | /* The template parameters for this new template are all the | |
5918 | template parameters for the old template, except the | |
5919 | outermost level of parameters. */ | |
5920 | DECL_TEMPLATE_PARMS (r) | |
5921 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, | |
5922 | complain); | |
5923 | ||
5924 | if (PRIMARY_TEMPLATE_P (t)) | |
5925 | DECL_PRIMARY_TEMPLATE (r) = r; | |
5926 | ||
5927 | /* We don't partially instantiate partial specializations. */ | |
5928 | if (TREE_CODE (decl) == TYPE_DECL) | |
5929 | break; | |
5930 | ||
5931 | /* Record this partial instantiation. */ | |
5932 | register_specialization (r, t, | |
5933 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r))); | |
5934 | ||
5935 | } | |
5936 | break; | |
5937 | ||
5938 | case FUNCTION_DECL: | |
5939 | { | |
5940 | tree ctx; | |
5941 | tree argvec = NULL_TREE; | |
5942 | tree *friends; | |
5943 | tree gen_tmpl; | |
5944 | int member; | |
5945 | int args_depth; | |
5946 | int parms_depth; | |
5947 | ||
5948 | /* Nobody should be tsubst'ing into non-template functions. */ | |
5949 | my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0); | |
5950 | ||
5951 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
5952 | { | |
5953 | tree spec; | |
5954 | ||
5955 | /* Calculate the most general template of which R is a | |
5956 | specialization, and the complete set of arguments used to | |
5957 | specialize R. */ | |
5958 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
5959 | argvec | |
5960 | = tsubst_template_arg_vector (DECL_TI_ARGS | |
5961 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
5962 | args, complain); | |
5963 | ||
5964 | /* Check to see if we already have this specialization. */ | |
5965 | spec = retrieve_specialization (gen_tmpl, argvec); | |
5966 | ||
5967 | if (spec) | |
5968 | { | |
5969 | r = spec; | |
5970 | break; | |
5971 | } | |
5972 | ||
5973 | /* We can see more levels of arguments than parameters if | |
5974 | there was a specialization of a member template, like | |
5975 | this: | |
5976 | ||
5977 | template <class T> struct S { template <class U> void f(); } | |
5978 | template <> template <class U> void S<int>::f(U); | |
5979 | ||
5980 | Here, we'll be substituting into the specialization, | |
5981 | because that's where we can find the code we actually | |
5982 | want to generate, but we'll have enough arguments for | |
5983 | the most general template. | |
5984 | ||
5985 | We also deal with the peculiar case: | |
5986 | ||
5987 | template <class T> struct S { | |
5988 | template <class U> friend void f(); | |
5989 | }; | |
5990 | template <class U> void f() {} | |
5991 | template S<int>; | |
5992 | template void f<double>(); | |
5993 | ||
5994 | Here, the ARGS for the instantiation of will be {int, | |
5995 | double}. But, we only need as many ARGS as there are | |
5996 | levels of template parameters in CODE_PATTERN. We are | |
5997 | careful not to get fooled into reducing the ARGS in | |
5998 | situations like: | |
5999 | ||
6000 | template <class T> struct S { template <class U> void f(U); } | |
6001 | template <class T> template <> void S<T>::f(int) {} | |
6002 | ||
6003 | which we can spot because the pattern will be a | |
6004 | specialization in this case. */ | |
6005 | args_depth = TMPL_ARGS_DEPTH (args); | |
6006 | parms_depth = | |
6007 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
6008 | if (args_depth > parms_depth | |
6009 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
6010 | args = get_innermost_template_args (args, parms_depth); | |
6011 | } | |
6012 | else | |
6013 | { | |
6014 | /* This special case arises when we have something like this: | |
6015 | ||
6016 | template <class T> struct S { | |
6017 | friend void f<int>(int, double); | |
6018 | }; | |
6019 | ||
6020 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
6021 | will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are | |
6022 | being called from tsubst_friend_function, and we want | |
6023 | only to create a new decl (R) with appropriate types so | |
6024 | that we can call determine_specialization. */ | |
6025 | gen_tmpl = NULL_TREE; | |
6026 | } | |
6027 | ||
6028 | if (DECL_CLASS_SCOPE_P (t)) | |
6029 | { | |
6030 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) | |
6031 | member = 2; | |
6032 | else | |
6033 | member = 1; | |
6034 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, | |
6035 | complain, t, | |
6036 | /*entering_scope=*/1); | |
6037 | } | |
6038 | else | |
6039 | { | |
6040 | member = 0; | |
6041 | ctx = DECL_CONTEXT (t); | |
6042 | } | |
6043 | type = tsubst (type, args, complain, in_decl); | |
6044 | if (type == error_mark_node) | |
6045 | return error_mark_node; | |
6046 | ||
6047 | /* We do NOT check for matching decls pushed separately at this | |
6048 | point, as they may not represent instantiations of this | |
6049 | template, and in any case are considered separate under the | |
6050 | discrete model. */ | |
6051 | r = copy_decl (t); | |
6052 | DECL_USE_TEMPLATE (r) = 0; | |
6053 | TREE_TYPE (r) = type; | |
6054 | /* Clear out the mangled name and RTL for the instantiation. */ | |
6055 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
6056 | SET_DECL_RTL (r, NULL_RTX); | |
6057 | ||
6058 | DECL_CONTEXT (r) = ctx; | |
6059 | ||
6060 | if (member && DECL_CONV_FN_P (r)) | |
6061 | /* Type-conversion operator. Reconstruct the name, in | |
6062 | case it's the name of one of the template's parameters. */ | |
6063 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
6064 | ||
6065 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, | |
6066 | complain, t); | |
6067 | DECL_RESULT (r) = NULL_TREE; | |
6068 | ||
6069 | TREE_STATIC (r) = 0; | |
6070 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
6071 | DECL_EXTERNAL (r) = 1; | |
6072 | DECL_INTERFACE_KNOWN (r) = 0; | |
6073 | DECL_DEFER_OUTPUT (r) = 0; | |
6074 | TREE_CHAIN (r) = NULL_TREE; | |
6075 | DECL_PENDING_INLINE_INFO (r) = 0; | |
6076 | DECL_PENDING_INLINE_P (r) = 0; | |
6077 | DECL_SAVED_TREE (r) = NULL_TREE; | |
6078 | TREE_USED (r) = 0; | |
6079 | if (DECL_CLONED_FUNCTION (r)) | |
6080 | { | |
6081 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
6082 | args, complain, t); | |
6083 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); | |
6084 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
6085 | } | |
6086 | ||
6087 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do | |
6088 | this in the special friend case mentioned above where | |
6089 | GEN_TMPL is NULL. */ | |
6090 | if (gen_tmpl) | |
6091 | { | |
6092 | DECL_TEMPLATE_INFO (r) | |
6093 | = tree_cons (gen_tmpl, argvec, NULL_TREE); | |
6094 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
6095 | register_specialization (r, gen_tmpl, argvec); | |
6096 | ||
6097 | /* We're not supposed to instantiate default arguments | |
6098 | until they are called, for a template. But, for a | |
6099 | declaration like: | |
6100 | ||
6101 | template <class T> void f () | |
6102 | { extern void g(int i = T()); } | |
6103 | ||
6104 | we should do the substitution when the template is | |
6105 | instantiated. We handle the member function case in | |
6106 | instantiate_class_template since the default arguments | |
6107 | might refer to other members of the class. */ | |
6108 | if (!member | |
6109 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
6110 | && !uses_template_parms (argvec)) | |
6111 | tsubst_default_arguments (r); | |
6112 | } | |
6113 | ||
6114 | /* Copy the list of befriending classes. */ | |
6115 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
6116 | *friends; | |
6117 | friends = &TREE_CHAIN (*friends)) | |
6118 | { | |
6119 | *friends = copy_node (*friends); | |
6120 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
6121 | args, complain, | |
6122 | in_decl); | |
6123 | } | |
6124 | ||
6125 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) | |
6126 | { | |
6127 | maybe_retrofit_in_chrg (r); | |
6128 | if (DECL_CONSTRUCTOR_P (r)) | |
6129 | grok_ctor_properties (ctx, r); | |
6130 | /* If this is an instantiation of a member template, clone it. | |
6131 | If it isn't, that'll be handled by | |
6132 | clone_constructors_and_destructors. */ | |
6133 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) | |
6134 | clone_function_decl (r, /*update_method_vec_p=*/0); | |
6135 | } | |
6136 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) | |
6137 | grok_op_properties (r, DECL_FRIEND_P (r)); | |
6138 | } | |
6139 | break; | |
6140 | ||
6141 | case PARM_DECL: | |
6142 | { | |
6143 | r = copy_node (t); | |
6144 | if (DECL_TEMPLATE_PARM_P (t)) | |
6145 | SET_DECL_TEMPLATE_PARM_P (r); | |
6146 | ||
6147 | TREE_TYPE (r) = type; | |
6148 | c_apply_type_quals_to_decl (cp_type_quals (type), r); | |
6149 | ||
6150 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
6151 | DECL_INITIAL (r) = TREE_TYPE (r); | |
6152 | else | |
6153 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
6154 | complain, in_decl); | |
6155 | ||
6156 | DECL_CONTEXT (r) = NULL_TREE; | |
6157 | ||
6158 | if (!DECL_TEMPLATE_PARM_P (r)) | |
6159 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
6160 | if (TREE_CHAIN (t)) | |
6161 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, | |
6162 | complain, TREE_CHAIN (t)); | |
6163 | } | |
6164 | break; | |
6165 | ||
6166 | case FIELD_DECL: | |
6167 | { | |
6168 | r = copy_decl (t); | |
6169 | TREE_TYPE (r) = type; | |
6170 | c_apply_type_quals_to_decl (cp_type_quals (type), r); | |
6171 | ||
6172 | /* We don't have to set DECL_CONTEXT here; it is set by | |
6173 | finish_member_declaration. */ | |
6174 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, | |
6175 | complain, in_decl); | |
6176 | TREE_CHAIN (r) = NULL_TREE; | |
6177 | if (VOID_TYPE_P (type)) | |
6178 | cp_error_at ("instantiation of `%D' as type `%T'", r, type); | |
6179 | } | |
6180 | break; | |
6181 | ||
6182 | case USING_DECL: | |
6183 | { | |
6184 | r = copy_node (t); | |
6185 | DECL_INITIAL (r) | |
6186 | = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
6187 | TREE_CHAIN (r) = NULL_TREE; | |
6188 | } | |
6189 | break; | |
6190 | ||
6191 | case TYPE_DECL: | |
6192 | if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM | |
6193 | || t == TYPE_MAIN_DECL (TREE_TYPE (t))) | |
6194 | { | |
6195 | /* If this is the canonical decl, we don't have to mess with | |
6196 | instantiations, and often we can't (for typename, template | |
6197 | type parms and such). Note that TYPE_NAME is not correct for | |
6198 | the above test if we've copied the type for a typedef. */ | |
6199 | r = TYPE_NAME (type); | |
6200 | break; | |
6201 | } | |
6202 | ||
6203 | /* Fall through. */ | |
6204 | ||
6205 | case VAR_DECL: | |
6206 | { | |
6207 | tree argvec = NULL_TREE; | |
6208 | tree gen_tmpl = NULL_TREE; | |
6209 | tree spec; | |
6210 | tree tmpl = NULL_TREE; | |
6211 | tree ctx; | |
6212 | int local_p; | |
6213 | ||
6214 | /* Assume this is a non-local variable. */ | |
6215 | local_p = 0; | |
6216 | ||
6217 | if (TYPE_P (CP_DECL_CONTEXT (t))) | |
6218 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, | |
6219 | complain, | |
6220 | in_decl, /*entering_scope=*/1); | |
6221 | else if (DECL_NAMESPACE_SCOPE_P (t)) | |
6222 | ctx = DECL_CONTEXT (t); | |
6223 | else | |
6224 | { | |
6225 | /* Subsequent calls to pushdecl will fill this in. */ | |
6226 | ctx = NULL_TREE; | |
6227 | local_p = 1; | |
6228 | } | |
6229 | ||
6230 | /* Check to see if we already have this specialization. */ | |
6231 | if (!local_p) | |
6232 | { | |
6233 | tmpl = DECL_TI_TEMPLATE (t); | |
6234 | gen_tmpl = most_general_template (tmpl); | |
6235 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
6236 | spec = retrieve_specialization (gen_tmpl, argvec); | |
6237 | } | |
6238 | else | |
6239 | spec = retrieve_local_specialization (t); | |
6240 | ||
6241 | if (spec) | |
6242 | { | |
6243 | r = spec; | |
6244 | break; | |
6245 | } | |
6246 | ||
6247 | r = copy_decl (t); | |
6248 | if (TREE_CODE (r) == VAR_DECL) | |
6249 | type = complete_type (type); | |
6250 | TREE_TYPE (r) = type; | |
6251 | c_apply_type_quals_to_decl (cp_type_quals (type), r); | |
6252 | DECL_CONTEXT (r) = ctx; | |
6253 | /* Clear out the mangled name and RTL for the instantiation. */ | |
6254 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
6255 | SET_DECL_RTL (r, NULL_RTX); | |
6256 | ||
6257 | /* Don't try to expand the initializer until someone tries to use | |
6258 | this variable; otherwise we run into circular dependencies. */ | |
6259 | DECL_INITIAL (r) = NULL_TREE; | |
6260 | SET_DECL_RTL (r, NULL_RTX); | |
6261 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; | |
6262 | ||
6263 | /* Even if the original location is out of scope, the newly | |
6264 | substituted one is not. */ | |
6265 | if (TREE_CODE (r) == VAR_DECL) | |
6266 | { | |
6267 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
6268 | DECL_INITIALIZED_P (r) = 0; | |
6269 | } | |
6270 | ||
6271 | if (!local_p) | |
6272 | { | |
6273 | /* A static data member declaration is always marked | |
6274 | external when it is declared in-class, even if an | |
6275 | initializer is present. We mimic the non-template | |
6276 | processing here. */ | |
6277 | DECL_EXTERNAL (r) = 1; | |
6278 | ||
6279 | register_specialization (r, gen_tmpl, argvec); | |
6280 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); | |
6281 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
6282 | } | |
6283 | else | |
6284 | register_local_specialization (r, t); | |
6285 | ||
6286 | TREE_CHAIN (r) = NULL_TREE; | |
6287 | if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type)) | |
6288 | cp_error_at ("instantiation of `%D' as type `%T'", r, type); | |
6289 | /* Compute the size, alignment, etc. of R. */ | |
6290 | layout_decl (r, 0); | |
6291 | } | |
6292 | break; | |
6293 | ||
6294 | default: | |
6295 | abort (); | |
6296 | } | |
6297 | ||
6298 | /* Restore the file and line information. */ | |
6299 | lineno = saved_lineno; | |
6300 | input_filename = saved_filename; | |
6301 | ||
6302 | return r; | |
6303 | } | |
6304 | ||
6305 | /* Substitue into the ARG_TYPES of a function type. */ | |
6306 | ||
6307 | static tree | |
6308 | tsubst_arg_types (arg_types, args, complain, in_decl) | |
6309 | tree arg_types; | |
6310 | tree args; | |
6311 | tsubst_flags_t complain; | |
6312 | tree in_decl; | |
6313 | { | |
6314 | tree remaining_arg_types; | |
6315 | tree type; | |
6316 | ||
6317 | if (!arg_types || arg_types == void_list_node) | |
6318 | return arg_types; | |
6319 | ||
6320 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
6321 | args, complain, in_decl); | |
6322 | if (remaining_arg_types == error_mark_node) | |
6323 | return error_mark_node; | |
6324 | ||
6325 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
6326 | if (type == error_mark_node) | |
6327 | return error_mark_node; | |
6328 | if (VOID_TYPE_P (type)) | |
6329 | { | |
6330 | if (complain & tf_error) | |
6331 | { | |
6332 | error ("invalid parameter type `%T'", type); | |
6333 | if (in_decl) | |
6334 | cp_error_at ("in declaration `%D'", in_decl); | |
6335 | } | |
6336 | return error_mark_node; | |
6337 | } | |
6338 | ||
6339 | /* Do array-to-pointer, function-to-pointer conversion, and ignore | |
6340 | top-level qualifiers as required. */ | |
6341 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
6342 | ||
6343 | /* Note that we do not substitute into default arguments here. The | |
6344 | standard mandates that they be instantiated only when needed, | |
6345 | which is done in build_over_call. */ | |
6346 | return hash_tree_cons (TREE_PURPOSE (arg_types), type, | |
6347 | remaining_arg_types); | |
6348 | ||
6349 | } | |
6350 | ||
6351 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does | |
6352 | *not* handle the exception-specification for FNTYPE, because the | |
6353 | initial substitution of explicitly provided template parameters | |
6354 | during argument deduction forbids substitution into the | |
6355 | exception-specification: | |
6356 | ||
6357 | [temp.deduct] | |
6358 | ||
6359 | All references in the function type of the function template to the | |
6360 | corresponding template parameters are replaced by the specified tem- | |
6361 | plate argument values. If a substitution in a template parameter or | |
6362 | in the function type of the function template results in an invalid | |
6363 | type, type deduction fails. [Note: The equivalent substitution in | |
6364 | exception specifications is done only when the function is instanti- | |
6365 | ated, at which point a program is ill-formed if the substitution | |
6366 | results in an invalid type.] */ | |
6367 | ||
6368 | static tree | |
6369 | tsubst_function_type (t, args, complain, in_decl) | |
6370 | tree t; | |
6371 | tree args; | |
6372 | tsubst_flags_t complain; | |
6373 | tree in_decl; | |
6374 | { | |
6375 | tree return_type; | |
6376 | tree arg_types; | |
6377 | tree fntype; | |
6378 | ||
6379 | /* The TYPE_CONTEXT is not used for function/method types. */ | |
6380 | my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0); | |
6381 | ||
6382 | /* Substitute the return type. */ | |
6383 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6384 | if (return_type == error_mark_node) | |
6385 | return error_mark_node; | |
6386 | ||
6387 | /* Substitue the argument types. */ | |
6388 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, | |
6389 | complain, in_decl); | |
6390 | if (arg_types == error_mark_node) | |
6391 | return error_mark_node; | |
6392 | ||
6393 | /* Construct a new type node and return it. */ | |
6394 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
6395 | fntype = build_function_type (return_type, arg_types); | |
6396 | else | |
6397 | { | |
6398 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
6399 | if (! IS_AGGR_TYPE (r)) | |
6400 | { | |
6401 | /* [temp.deduct] | |
6402 | ||
6403 | Type deduction may fail for any of the following | |
6404 | reasons: | |
6405 | ||
6406 | -- Attempting to create "pointer to member of T" when T | |
6407 | is not a class type. */ | |
6408 | if (complain & tf_error) | |
6409 | error ("creating pointer to member function of non-class type `%T'", | |
6410 | r); | |
6411 | return error_mark_node; | |
6412 | } | |
6413 | ||
6414 | fntype = build_cplus_method_type (r, return_type, TREE_CHAIN | |
6415 | (arg_types)); | |
6416 | } | |
6417 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); | |
6418 | fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); | |
6419 | ||
6420 | return fntype; | |
6421 | } | |
6422 | ||
6423 | /* Substitute into the PARMS of a call-declarator. */ | |
6424 | ||
6425 | static tree | |
6426 | tsubst_call_declarator_parms (parms, args, complain, in_decl) | |
6427 | tree parms; | |
6428 | tree args; | |
6429 | tsubst_flags_t complain; | |
6430 | tree in_decl; | |
6431 | { | |
6432 | tree new_parms; | |
6433 | tree type; | |
6434 | tree defarg; | |
6435 | ||
6436 | if (!parms || parms == void_list_node) | |
6437 | return parms; | |
6438 | ||
6439 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
6440 | args, complain, in_decl); | |
6441 | ||
6442 | /* Figure out the type of this parameter. */ | |
6443 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); | |
6444 | ||
6445 | /* Figure out the default argument as well. Note that we use | |
6446 | tsubst_expr since the default argument is really an expression. */ | |
6447 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
6448 | ||
6449 | /* Chain this parameter on to the front of those we have already | |
6450 | processed. We don't use hash_tree_cons because that function | |
6451 | doesn't check TREE_PARMLIST. */ | |
6452 | new_parms = tree_cons (defarg, type, new_parms); | |
6453 | ||
6454 | /* And note that these are parameters. */ | |
6455 | TREE_PARMLIST (new_parms) = 1; | |
6456 | ||
6457 | return new_parms; | |
6458 | } | |
6459 | ||
6460 | /* Take the tree structure T and replace template parameters used | |
6461 | therein with the argument vector ARGS. IN_DECL is an associated | |
6462 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
6463 | Issue error and warning messages under control of COMPLAIN. Note | |
6464 | that we must be relatively non-tolerant of extensions here, in | |
6465 | order to preserve conformance; if we allow substitutions that | |
6466 | should not be allowed, we may allow argument deductions that should | |
6467 | not succeed, and therefore report ambiguous overload situations | |
6468 | where there are none. In theory, we could allow the substitution, | |
6469 | but indicate that it should have failed, and allow our caller to | |
6470 | make sure that the right thing happens, but we don't try to do this | |
6471 | yet. | |
6472 | ||
6473 | This function is used for dealing with types, decls and the like; | |
6474 | for expressions, use tsubst_expr or tsubst_copy. */ | |
6475 | ||
6476 | static tree | |
6477 | tsubst (t, args, complain, in_decl) | |
6478 | tree t, args; | |
6479 | tsubst_flags_t complain; | |
6480 | tree in_decl; | |
6481 | { | |
6482 | tree type, r; | |
6483 | ||
6484 | if (t == NULL_TREE || t == error_mark_node | |
6485 | || t == integer_type_node | |
6486 | || t == void_type_node | |
6487 | || t == char_type_node | |
6488 | || TREE_CODE (t) == NAMESPACE_DECL) | |
6489 | return t; | |
6490 | ||
6491 | if (TREE_CODE (t) == IDENTIFIER_NODE) | |
6492 | type = IDENTIFIER_TYPE_VALUE (t); | |
6493 | else | |
6494 | type = TREE_TYPE (t); | |
6495 | if (type == unknown_type_node) | |
6496 | abort (); | |
6497 | ||
6498 | if (type && TREE_CODE (t) != FUNCTION_DECL | |
6499 | && TREE_CODE (t) != TYPENAME_TYPE | |
6500 | && TREE_CODE (t) != TEMPLATE_DECL | |
6501 | && TREE_CODE (t) != IDENTIFIER_NODE | |
6502 | && TREE_CODE (t) != FUNCTION_TYPE | |
6503 | && TREE_CODE (t) != METHOD_TYPE) | |
6504 | type = tsubst (type, args, complain, in_decl); | |
6505 | if (type == error_mark_node) | |
6506 | return error_mark_node; | |
6507 | ||
6508 | if (DECL_P (t)) | |
6509 | return tsubst_decl (t, args, type, complain); | |
6510 | ||
6511 | switch (TREE_CODE (t)) | |
6512 | { | |
6513 | case RECORD_TYPE: | |
6514 | case UNION_TYPE: | |
6515 | case ENUMERAL_TYPE: | |
6516 | return tsubst_aggr_type (t, args, complain, in_decl, | |
6517 | /*entering_scope=*/0); | |
6518 | ||
6519 | case ERROR_MARK: | |
6520 | case IDENTIFIER_NODE: | |
6521 | case VOID_TYPE: | |
6522 | case REAL_TYPE: | |
6523 | case COMPLEX_TYPE: | |
6524 | case VECTOR_TYPE: | |
6525 | case BOOLEAN_TYPE: | |
6526 | case INTEGER_CST: | |
6527 | case REAL_CST: | |
6528 | case STRING_CST: | |
6529 | return t; | |
6530 | ||
6531 | case INTEGER_TYPE: | |
6532 | if (t == integer_type_node) | |
6533 | return t; | |
6534 | ||
6535 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
6536 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
6537 | return t; | |
6538 | ||
6539 | { | |
6540 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); | |
6541 | ||
6542 | max = tsubst_expr (omax, args, complain, in_decl); | |
6543 | if (max == error_mark_node) | |
6544 | return error_mark_node; | |
6545 | ||
6546 | /* See if we can reduce this expression to something simpler. */ | |
6547 | max = maybe_fold_nontype_arg (max); | |
6548 | if (!processing_template_decl) | |
6549 | max = decl_constant_value (max); | |
6550 | ||
6551 | if (processing_template_decl | |
6552 | /* When providing explicit arguments to a template | |
6553 | function, but leaving some arguments for subsequent | |
6554 | deduction, MAX may be template-dependent even if we're | |
6555 | not PROCESSING_TEMPLATE_DECL. We still need to check for | |
6556 | template parms, though; MAX won't be an INTEGER_CST for | |
6557 | dynamic arrays, either. */ | |
6558 | || (TREE_CODE (max) != INTEGER_CST | |
6559 | && uses_template_parms (max))) | |
6560 | { | |
6561 | tree itype = make_node (INTEGER_TYPE); | |
6562 | TYPE_MIN_VALUE (itype) = size_zero_node; | |
6563 | TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max, | |
6564 | integer_one_node); | |
6565 | return itype; | |
6566 | } | |
6567 | ||
6568 | if (integer_zerop (omax)) | |
6569 | { | |
6570 | /* Still allow an explicit array of size zero. */ | |
6571 | if (pedantic) | |
6572 | pedwarn ("creating array with size zero"); | |
6573 | } | |
6574 | else if (integer_zerop (max) | |
6575 | || (TREE_CODE (max) == INTEGER_CST | |
6576 | && INT_CST_LT (max, integer_zero_node))) | |
6577 | { | |
6578 | /* [temp.deduct] | |
6579 | ||
6580 | Type deduction may fail for any of the following | |
6581 | reasons: | |
6582 | ||
6583 | Attempting to create an array with a size that is | |
6584 | zero or negative. */ | |
6585 | if (complain & tf_error) | |
6586 | error ("creating array with size zero (`%E')", max); | |
6587 | ||
6588 | return error_mark_node; | |
6589 | } | |
6590 | ||
6591 | return compute_array_index_type (NULL_TREE, max); | |
6592 | } | |
6593 | ||
6594 | case TEMPLATE_TYPE_PARM: | |
6595 | case TEMPLATE_TEMPLATE_PARM: | |
6596 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
6597 | case TEMPLATE_PARM_INDEX: | |
6598 | { | |
6599 | int idx; | |
6600 | int level; | |
6601 | int levels; | |
6602 | ||
6603 | r = NULL_TREE; | |
6604 | ||
6605 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
6606 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM | |
6607 | || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
6608 | { | |
6609 | idx = TEMPLATE_TYPE_IDX (t); | |
6610 | level = TEMPLATE_TYPE_LEVEL (t); | |
6611 | } | |
6612 | else | |
6613 | { | |
6614 | idx = TEMPLATE_PARM_IDX (t); | |
6615 | level = TEMPLATE_PARM_LEVEL (t); | |
6616 | } | |
6617 | ||
6618 | if (TREE_VEC_LENGTH (args) > 0) | |
6619 | { | |
6620 | tree arg = NULL_TREE; | |
6621 | ||
6622 | levels = TMPL_ARGS_DEPTH (args); | |
6623 | if (level <= levels) | |
6624 | arg = TMPL_ARG (args, level, idx); | |
6625 | ||
6626 | if (arg == error_mark_node) | |
6627 | return error_mark_node; | |
6628 | else if (arg != NULL_TREE) | |
6629 | { | |
6630 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
6631 | { | |
6632 | my_friendly_assert (TYPE_P (arg), 0); | |
6633 | return cp_build_qualified_type_real | |
6634 | (arg, cp_type_quals (arg) | cp_type_quals (t), | |
6635 | complain | tf_ignore_bad_quals); | |
6636 | } | |
6637 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
6638 | { | |
6639 | /* We are processing a type constructed from | |
6640 | a template template parameter */ | |
6641 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
6642 | args, complain, in_decl); | |
6643 | if (argvec == error_mark_node) | |
6644 | return error_mark_node; | |
6645 | ||
6646 | /* We can get a TEMPLATE_TEMPLATE_PARM here when | |
6647 | we are resolving nested-types in the signature of | |
6648 | a member function templates. | |
6649 | Otherwise ARG is a TEMPLATE_DECL and is the real | |
6650 | template to be instantiated. */ | |
6651 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
6652 | arg = TYPE_NAME (arg); | |
6653 | ||
6654 | r = lookup_template_class (arg, | |
6655 | argvec, in_decl, | |
6656 | DECL_CONTEXT (arg), | |
6657 | /*entering_scope=*/0, | |
6658 | complain); | |
6659 | return cp_build_qualified_type_real | |
6660 | (r, TYPE_QUALS (t), complain); | |
6661 | } | |
6662 | else | |
6663 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
6664 | return arg; | |
6665 | } | |
6666 | } | |
6667 | else | |
6668 | abort (); | |
6669 | ||
6670 | if (level == 1) | |
6671 | /* This can happen during the attempted tsubst'ing in | |
6672 | unify. This means that we don't yet have any information | |
6673 | about the template parameter in question. */ | |
6674 | return t; | |
6675 | ||
6676 | /* If we get here, we must have been looking at a parm for a | |
6677 | more deeply nested template. Make a new version of this | |
6678 | template parameter, but with a lower level. */ | |
6679 | switch (TREE_CODE (t)) | |
6680 | { | |
6681 | case TEMPLATE_TYPE_PARM: | |
6682 | case TEMPLATE_TEMPLATE_PARM: | |
6683 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
6684 | if (cp_type_quals (t)) | |
6685 | { | |
6686 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); | |
6687 | r = cp_build_qualified_type_real | |
6688 | (r, cp_type_quals (t), | |
6689 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
6690 | ? tf_ignore_bad_quals : 0)); | |
6691 | } | |
6692 | else | |
6693 | { | |
6694 | r = copy_type (t); | |
6695 | TEMPLATE_TYPE_PARM_INDEX (r) | |
6696 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
6697 | r, levels); | |
6698 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
6699 | TYPE_MAIN_VARIANT (r) = r; | |
6700 | TYPE_POINTER_TO (r) = NULL_TREE; | |
6701 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
6702 | ||
6703 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
6704 | { | |
6705 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
6706 | complain, in_decl); | |
6707 | if (argvec == error_mark_node) | |
6708 | return error_mark_node; | |
6709 | ||
6710 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) | |
6711 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
6712 | } | |
6713 | } | |
6714 | break; | |
6715 | ||
6716 | case TEMPLATE_PARM_INDEX: | |
6717 | r = reduce_template_parm_level (t, type, levels); | |
6718 | break; | |
6719 | ||
6720 | default: | |
6721 | abort (); | |
6722 | } | |
6723 | ||
6724 | return r; | |
6725 | } | |
6726 | ||
6727 | case TREE_LIST: | |
6728 | { | |
6729 | tree purpose, value, chain, result; | |
6730 | ||
6731 | if (t == void_list_node) | |
6732 | return t; | |
6733 | ||
6734 | purpose = TREE_PURPOSE (t); | |
6735 | if (purpose) | |
6736 | { | |
6737 | purpose = tsubst (purpose, args, complain, in_decl); | |
6738 | if (purpose == error_mark_node) | |
6739 | return error_mark_node; | |
6740 | } | |
6741 | value = TREE_VALUE (t); | |
6742 | if (value) | |
6743 | { | |
6744 | value = tsubst (value, args, complain, in_decl); | |
6745 | if (value == error_mark_node) | |
6746 | return error_mark_node; | |
6747 | } | |
6748 | chain = TREE_CHAIN (t); | |
6749 | if (chain && chain != void_type_node) | |
6750 | { | |
6751 | chain = tsubst (chain, args, complain, in_decl); | |
6752 | if (chain == error_mark_node) | |
6753 | return error_mark_node; | |
6754 | } | |
6755 | if (purpose == TREE_PURPOSE (t) | |
6756 | && value == TREE_VALUE (t) | |
6757 | && chain == TREE_CHAIN (t)) | |
6758 | return t; | |
6759 | if (TREE_PARMLIST (t)) | |
6760 | { | |
6761 | result = tree_cons (purpose, value, chain); | |
6762 | TREE_PARMLIST (result) = 1; | |
6763 | } | |
6764 | else | |
6765 | result = hash_tree_cons (purpose, value, chain); | |
6766 | return result; | |
6767 | } | |
6768 | case TREE_VEC: | |
6769 | if (type != NULL_TREE) | |
6770 | { | |
6771 | /* A binfo node. We always need to make a copy, of the node | |
6772 | itself and of its BINFO_BASETYPES. */ | |
6773 | ||
6774 | t = copy_node (t); | |
6775 | ||
6776 | /* Make sure type isn't a typedef copy. */ | |
6777 | type = BINFO_TYPE (TYPE_BINFO (type)); | |
6778 | ||
6779 | TREE_TYPE (t) = complete_type (type); | |
6780 | if (IS_AGGR_TYPE (type)) | |
6781 | { | |
6782 | BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type); | |
6783 | BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type); | |
6784 | if (TYPE_BINFO_BASETYPES (type) != NULL_TREE) | |
6785 | BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type)); | |
6786 | } | |
6787 | return t; | |
6788 | } | |
6789 | ||
6790 | /* Otherwise, a vector of template arguments. */ | |
6791 | return tsubst_template_arg_vector (t, args, complain); | |
6792 | ||
6793 | case POINTER_TYPE: | |
6794 | case REFERENCE_TYPE: | |
6795 | { | |
6796 | enum tree_code code; | |
6797 | ||
6798 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) | |
6799 | return t; | |
6800 | ||
6801 | code = TREE_CODE (t); | |
6802 | ||
6803 | ||
6804 | /* [temp.deduct] | |
6805 | ||
6806 | Type deduction may fail for any of the following | |
6807 | reasons: | |
6808 | ||
6809 | -- Attempting to create a pointer to reference type. | |
6810 | -- Attempting to create a reference to a reference type or | |
6811 | a reference to void. */ | |
6812 | if (TREE_CODE (type) == REFERENCE_TYPE | |
6813 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
6814 | { | |
6815 | static int last_line = 0; | |
6816 | static const char* last_file = 0; | |
6817 | ||
6818 | /* We keep track of the last time we issued this error | |
6819 | message to avoid spewing a ton of messages during a | |
6820 | single bad template instantiation. */ | |
6821 | if (complain & tf_error | |
6822 | && (last_line != lineno || last_file != input_filename)) | |
6823 | { | |
6824 | if (TREE_CODE (type) == VOID_TYPE) | |
6825 | error ("forming reference to void"); | |
6826 | else | |
6827 | error ("forming %s to reference type `%T'", | |
6828 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
6829 | type); | |
6830 | last_line = lineno; | |
6831 | last_file = input_filename; | |
6832 | } | |
6833 | ||
6834 | return error_mark_node; | |
6835 | } | |
6836 | else if (code == POINTER_TYPE) | |
6837 | { | |
6838 | r = build_pointer_type (type); | |
6839 | if (TREE_CODE (type) == METHOD_TYPE) | |
6840 | r = build_ptrmemfunc_type (r); | |
6841 | } | |
6842 | else | |
6843 | r = build_reference_type (type); | |
6844 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
6845 | ||
6846 | if (r != error_mark_node) | |
6847 | /* Will this ever be needed for TYPE_..._TO values? */ | |
6848 | layout_type (r); | |
6849 | ||
6850 | return r; | |
6851 | } | |
6852 | case OFFSET_TYPE: | |
6853 | { | |
6854 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); | |
6855 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
6856 | { | |
6857 | /* [temp.deduct] | |
6858 | ||
6859 | Type deduction may fail for any of the following | |
6860 | reasons: | |
6861 | ||
6862 | -- Attempting to create "pointer to member of T" when T | |
6863 | is not a class type. */ | |
6864 | if (complain & tf_error) | |
6865 | error ("creating pointer to member of non-class type `%T'", r); | |
6866 | return error_mark_node; | |
6867 | } | |
6868 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
6869 | { | |
6870 | if (complain & tf_error) | |
6871 | error ("creating pointer to member reference type `%T'", type); | |
6872 | ||
6873 | return error_mark_node; | |
6874 | } | |
6875 | my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231); | |
6876 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
6877 | /* This is really a method type. The cv qualifiers of the | |
6878 | this pointer should _not_ be determined by the cv | |
6879 | qualifiers of the class type. They should be held | |
6880 | somewhere in the FUNCTION_TYPE, but we don't do that at | |
6881 | the moment. Consider | |
6882 | typedef void (Func) () const; | |
6883 | ||
6884 | template <typename T1> void Foo (Func T1::*); | |
6885 | ||
6886 | */ | |
6887 | return build_cplus_method_type (TYPE_MAIN_VARIANT (r), | |
6888 | TREE_TYPE (type), | |
6889 | TYPE_ARG_TYPES (type)); | |
6890 | else | |
6891 | return build_offset_type (r, type); | |
6892 | } | |
6893 | case FUNCTION_TYPE: | |
6894 | case METHOD_TYPE: | |
6895 | { | |
6896 | tree fntype; | |
6897 | tree raises; | |
6898 | ||
6899 | fntype = tsubst_function_type (t, args, complain, in_decl); | |
6900 | if (fntype == error_mark_node) | |
6901 | return error_mark_node; | |
6902 | ||
6903 | /* Substitue the exception specification. */ | |
6904 | raises = TYPE_RAISES_EXCEPTIONS (t); | |
6905 | if (raises) | |
6906 | { | |
6907 | tree list = NULL_TREE; | |
6908 | ||
6909 | if (! TREE_VALUE (raises)) | |
6910 | list = raises; | |
6911 | else | |
6912 | for (; raises != NULL_TREE; raises = TREE_CHAIN (raises)) | |
6913 | { | |
6914 | tree spec = TREE_VALUE (raises); | |
6915 | ||
6916 | spec = tsubst (spec, args, complain, in_decl); | |
6917 | if (spec == error_mark_node) | |
6918 | return spec; | |
6919 | list = add_exception_specifier (list, spec, complain); | |
6920 | } | |
6921 | fntype = build_exception_variant (fntype, list); | |
6922 | } | |
6923 | return fntype; | |
6924 | } | |
6925 | case ARRAY_TYPE: | |
6926 | { | |
6927 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); | |
6928 | if (domain == error_mark_node) | |
6929 | return error_mark_node; | |
6930 | ||
6931 | /* As an optimization, we avoid regenerating the array type if | |
6932 | it will obviously be the same as T. */ | |
6933 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) | |
6934 | return t; | |
6935 | ||
6936 | /* These checks should match the ones in grokdeclarator. | |
6937 | ||
6938 | [temp.deduct] | |
6939 | ||
6940 | The deduction may fail for any of the following reasons: | |
6941 | ||
6942 | -- Attempting to create an array with an element type that | |
6943 | is void, a function type, or a reference type. */ | |
6944 | if (TREE_CODE (type) == VOID_TYPE | |
6945 | || TREE_CODE (type) == FUNCTION_TYPE | |
6946 | || TREE_CODE (type) == REFERENCE_TYPE) | |
6947 | { | |
6948 | if (complain & tf_error) | |
6949 | error ("creating array of `%T'", type); | |
6950 | return error_mark_node; | |
6951 | } | |
6952 | ||
6953 | r = build_cplus_array_type (type, domain); | |
6954 | return r; | |
6955 | } | |
6956 | ||
6957 | case PLUS_EXPR: | |
6958 | case MINUS_EXPR: | |
6959 | { | |
6960 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); | |
6961 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
6962 | ||
6963 | if (e1 == error_mark_node || e2 == error_mark_node) | |
6964 | return error_mark_node; | |
6965 | ||
6966 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2)); | |
6967 | } | |
6968 | ||
6969 | case NEGATE_EXPR: | |
6970 | case NOP_EXPR: | |
6971 | { | |
6972 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); | |
6973 | if (e == error_mark_node) | |
6974 | return error_mark_node; | |
6975 | ||
6976 | return fold (build (TREE_CODE (t), TREE_TYPE (t), e)); | |
6977 | } | |
6978 | ||
6979 | case TYPENAME_TYPE: | |
6980 | { | |
6981 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
6982 | in_decl, /*entering_scope=*/1); | |
6983 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
6984 | complain, in_decl); | |
6985 | ||
6986 | if (ctx == error_mark_node || f == error_mark_node) | |
6987 | return error_mark_node; | |
6988 | ||
6989 | if (!IS_AGGR_TYPE (ctx)) | |
6990 | { | |
6991 | if (complain & tf_error) | |
6992 | error ("`%T' is not a class, struct, or union type", | |
6993 | ctx); | |
6994 | return error_mark_node; | |
6995 | } | |
6996 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
6997 | { | |
6998 | /* Normally, make_typename_type does not require that the CTX | |
6999 | have complete type in order to allow things like: | |
7000 | ||
7001 | template <class T> struct S { typename S<T>::X Y; }; | |
7002 | ||
7003 | But, such constructs have already been resolved by this | |
7004 | point, so here CTX really should have complete type, unless | |
7005 | it's a partial instantiation. */ | |
7006 | ctx = complete_type (ctx); | |
7007 | if (!COMPLETE_TYPE_P (ctx)) | |
7008 | { | |
7009 | if (complain & tf_error) | |
7010 | cxx_incomplete_type_error (NULL_TREE, ctx); | |
7011 | return error_mark_node; | |
7012 | } | |
7013 | } | |
7014 | ||
7015 | f = make_typename_type (ctx, f, | |
7016 | (complain & tf_error) | tf_keep_type_decl); | |
7017 | if (f == error_mark_node) | |
7018 | return f; | |
7019 | if (TREE_CODE (f) == TYPE_DECL) | |
7020 | { | |
7021 | complain |= tf_ignore_bad_quals; | |
7022 | f = TREE_TYPE (f); | |
7023 | } | |
7024 | ||
7025 | return cp_build_qualified_type_real | |
7026 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
7027 | } | |
7028 | ||
7029 | case UNBOUND_CLASS_TEMPLATE: | |
7030 | { | |
7031 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
7032 | in_decl, /*entering_scope=*/1); | |
7033 | tree name = TYPE_IDENTIFIER (t); | |
7034 | ||
7035 | if (ctx == error_mark_node || name == error_mark_node) | |
7036 | return error_mark_node; | |
7037 | ||
7038 | return make_unbound_class_template (ctx, name, complain); | |
7039 | } | |
7040 | ||
7041 | case INDIRECT_REF: | |
7042 | { | |
7043 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7044 | if (e == error_mark_node) | |
7045 | return error_mark_node; | |
7046 | return make_pointer_declarator (type, e); | |
7047 | } | |
7048 | ||
7049 | case ADDR_EXPR: | |
7050 | { | |
7051 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7052 | if (e == error_mark_node) | |
7053 | return error_mark_node; | |
7054 | return make_reference_declarator (type, e); | |
7055 | } | |
7056 | ||
7057 | case ARRAY_REF: | |
7058 | { | |
7059 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7060 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); | |
7061 | if (e1 == error_mark_node || e2 == error_mark_node) | |
7062 | return error_mark_node; | |
7063 | ||
7064 | return build_nt (ARRAY_REF, e1, e2, tsubst_expr); | |
7065 | } | |
7066 | ||
7067 | case CALL_EXPR: | |
7068 | { | |
7069 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7070 | tree e2 = (tsubst_call_declarator_parms | |
7071 | (CALL_DECLARATOR_PARMS (t), args, complain, in_decl)); | |
7072 | tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args, | |
7073 | complain, in_decl); | |
7074 | ||
7075 | if (e1 == error_mark_node || e2 == error_mark_node | |
7076 | || e3 == error_mark_node) | |
7077 | return error_mark_node; | |
7078 | ||
7079 | return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3); | |
7080 | } | |
7081 | ||
7082 | case SCOPE_REF: | |
7083 | { | |
7084 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7085 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
7086 | if (e1 == error_mark_node || e2 == error_mark_node) | |
7087 | return error_mark_node; | |
7088 | ||
7089 | return build_nt (TREE_CODE (t), e1, e2); | |
7090 | } | |
7091 | ||
7092 | case TYPEOF_TYPE: | |
7093 | { | |
7094 | tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl); | |
7095 | if (e1 == error_mark_node) | |
7096 | return error_mark_node; | |
7097 | ||
7098 | return TREE_TYPE (e1); | |
7099 | } | |
7100 | ||
7101 | default: | |
7102 | sorry ("use of `%s' in template", | |
7103 | tree_code_name [(int) TREE_CODE (t)]); | |
7104 | return error_mark_node; | |
7105 | } | |
7106 | } | |
7107 | ||
7108 | /* Like tsubst, but deals with expressions. This function just replaces | |
7109 | template parms; to finish processing the resultant expression, use | |
7110 | tsubst_expr. */ | |
7111 | ||
7112 | static tree | |
7113 | tsubst_copy (t, args, complain, in_decl) | |
7114 | tree t, args; | |
7115 | tsubst_flags_t complain; | |
7116 | tree in_decl; | |
7117 | { | |
7118 | enum tree_code code; | |
7119 | tree r; | |
7120 | ||
7121 | if (t == NULL_TREE || t == error_mark_node) | |
7122 | return t; | |
7123 | ||
7124 | code = TREE_CODE (t); | |
7125 | ||
7126 | switch (code) | |
7127 | { | |
7128 | case PARM_DECL: | |
7129 | r = retrieve_local_specialization (t); | |
7130 | my_friendly_assert (r != NULL, 20020903); | |
7131 | return r; | |
7132 | ||
7133 | case CONST_DECL: | |
7134 | { | |
7135 | tree enum_type; | |
7136 | tree v; | |
7137 | ||
7138 | if (DECL_TEMPLATE_PARM_P (t)) | |
7139 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7140 | if (!DECL_CONTEXT (t)) | |
7141 | /* This is a global enumeration constant. */ | |
7142 | return t; | |
7143 | ||
7144 | /* Unfortunately, we cannot just call lookup_name here. | |
7145 | Consider: | |
7146 | ||
7147 | template <int I> int f() { | |
7148 | enum E { a = I }; | |
7149 | struct S { void g() { E e = a; } }; | |
7150 | }; | |
7151 | ||
7152 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
7153 | clever enough to find f<7>::a. */ | |
7154 | enum_type | |
7155 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
7156 | /*entering_scope=*/0); | |
7157 | ||
7158 | for (v = TYPE_VALUES (enum_type); | |
7159 | v != NULL_TREE; | |
7160 | v = TREE_CHAIN (v)) | |
7161 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
7162 | return TREE_VALUE (v); | |
7163 | ||
7164 | /* We didn't find the name. That should never happen; if | |
7165 | name-lookup found it during preliminary parsing, we | |
7166 | should find it again here during instantiation. */ | |
7167 | abort (); | |
7168 | } | |
7169 | return t; | |
7170 | ||
7171 | case FIELD_DECL: | |
7172 | if (DECL_CONTEXT (t)) | |
7173 | { | |
7174 | tree ctx; | |
7175 | ||
7176 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, | |
7177 | /*entering_scope=*/1); | |
7178 | if (ctx != DECL_CONTEXT (t)) | |
7179 | return lookup_field (ctx, DECL_NAME (t), 0, false); | |
7180 | } | |
7181 | return t; | |
7182 | ||
7183 | case VAR_DECL: | |
7184 | case FUNCTION_DECL: | |
7185 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) | |
7186 | || local_variable_p (t)) | |
7187 | t = tsubst (t, args, complain, in_decl); | |
7188 | mark_used (t); | |
7189 | return t; | |
7190 | ||
7191 | case BASELINK: | |
7192 | { | |
7193 | tree name; | |
7194 | tree qualifying_scope; | |
7195 | tree fns; | |
7196 | tree template_args = 0; | |
7197 | bool template_id_p = false; | |
7198 | ||
7199 | /* A baselink indicates a function from a base class. The | |
7200 | BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have | |
7201 | non-dependent types; otherwise, the lookup could not have | |
7202 | succeeded. However, they may indicate bases of the template | |
7203 | class, rather than the instantiated class. | |
7204 | ||
7205 | In addition, lookups that were not ambiguous before may be | |
7206 | ambiguous now. Therefore, we perform the lookup again. */ | |
7207 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (t)); | |
7208 | fns = BASELINK_FUNCTIONS (t); | |
7209 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) | |
7210 | { | |
7211 | template_id_p = true; | |
7212 | template_args = TREE_OPERAND (fns, 1); | |
7213 | fns = TREE_OPERAND (fns, 0); | |
7214 | } | |
7215 | name = DECL_NAME (get_first_fn (fns)); | |
7216 | t = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
7217 | if (BASELINK_P (t) && template_id_p) | |
7218 | BASELINK_FUNCTIONS (t) | |
7219 | = build_nt (TEMPLATE_ID_EXPR, | |
7220 | BASELINK_FUNCTIONS (t), | |
7221 | template_args); | |
7222 | return adjust_result_of_qualified_name_lookup (t, | |
7223 | qualifying_scope, | |
7224 | current_class_type); | |
7225 | } | |
7226 | ||
7227 | case TEMPLATE_DECL: | |
7228 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) | |
7229 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), | |
7230 | args, complain, in_decl); | |
7231 | else if (is_member_template (t)) | |
7232 | return tsubst (t, args, complain, in_decl); | |
7233 | else | |
7234 | return t; | |
7235 | ||
7236 | case LOOKUP_EXPR: | |
7237 | { | |
7238 | /* We must tsubst into a LOOKUP_EXPR in case the names to | |
7239 | which it refers is a conversion operator; in that case the | |
7240 | name will change. We avoid making unnecessary copies, | |
7241 | however. */ | |
7242 | ||
7243 | tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7244 | ||
7245 | if (id != TREE_OPERAND (t, 0)) | |
7246 | { | |
7247 | r = build_nt (LOOKUP_EXPR, id); | |
7248 | LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t); | |
7249 | t = r; | |
7250 | } | |
7251 | ||
7252 | return t; | |
7253 | } | |
7254 | ||
7255 | case CAST_EXPR: | |
7256 | case REINTERPRET_CAST_EXPR: | |
7257 | case CONST_CAST_EXPR: | |
7258 | case STATIC_CAST_EXPR: | |
7259 | case DYNAMIC_CAST_EXPR: | |
7260 | case NOP_EXPR: | |
7261 | return build1 | |
7262 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7263 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
7264 | ||
7265 | case INDIRECT_REF: | |
7266 | case NEGATE_EXPR: | |
7267 | case TRUTH_NOT_EXPR: | |
7268 | case BIT_NOT_EXPR: | |
7269 | case ADDR_EXPR: | |
7270 | case CONVERT_EXPR: /* Unary + */ | |
7271 | case SIZEOF_EXPR: | |
7272 | case ALIGNOF_EXPR: | |
7273 | case ARROW_EXPR: | |
7274 | case THROW_EXPR: | |
7275 | case TYPEID_EXPR: | |
7276 | case REALPART_EXPR: | |
7277 | case IMAGPART_EXPR: | |
7278 | return build1 | |
7279 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7280 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
7281 | ||
7282 | case COMPONENT_REF: | |
7283 | { | |
7284 | tree object; | |
7285 | tree name; | |
7286 | ||
7287 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7288 | name = TREE_OPERAND (t, 1); | |
7289 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
7290 | { | |
7291 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7292 | complain, in_decl); | |
7293 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
7294 | } | |
7295 | else if (TREE_CODE (name) == SCOPE_REF | |
7296 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
7297 | { | |
7298 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7299 | complain, in_decl); | |
7300 | name = TREE_OPERAND (name, 1); | |
7301 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7302 | complain, in_decl); | |
7303 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
7304 | name = build_nt (SCOPE_REF, base, name); | |
7305 | } | |
7306 | else | |
7307 | name = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl); | |
7308 | return build_nt (COMPONENT_REF, object, name); | |
7309 | } | |
7310 | ||
7311 | case PLUS_EXPR: | |
7312 | case MINUS_EXPR: | |
7313 | case MULT_EXPR: | |
7314 | case TRUNC_DIV_EXPR: | |
7315 | case CEIL_DIV_EXPR: | |
7316 | case FLOOR_DIV_EXPR: | |
7317 | case ROUND_DIV_EXPR: | |
7318 | case EXACT_DIV_EXPR: | |
7319 | case BIT_AND_EXPR: | |
7320 | case BIT_ANDTC_EXPR: | |
7321 | case BIT_IOR_EXPR: | |
7322 | case BIT_XOR_EXPR: | |
7323 | case TRUNC_MOD_EXPR: | |
7324 | case FLOOR_MOD_EXPR: | |
7325 | case TRUTH_ANDIF_EXPR: | |
7326 | case TRUTH_ORIF_EXPR: | |
7327 | case TRUTH_AND_EXPR: | |
7328 | case TRUTH_OR_EXPR: | |
7329 | case RSHIFT_EXPR: | |
7330 | case LSHIFT_EXPR: | |
7331 | case RROTATE_EXPR: | |
7332 | case LROTATE_EXPR: | |
7333 | case EQ_EXPR: | |
7334 | case NE_EXPR: | |
7335 | case MAX_EXPR: | |
7336 | case MIN_EXPR: | |
7337 | case LE_EXPR: | |
7338 | case GE_EXPR: | |
7339 | case LT_EXPR: | |
7340 | case GT_EXPR: | |
7341 | case ARRAY_REF: | |
7342 | case COMPOUND_EXPR: | |
7343 | case SCOPE_REF: | |
7344 | case DOTSTAR_EXPR: | |
7345 | case MEMBER_REF: | |
7346 | case PREDECREMENT_EXPR: | |
7347 | case PREINCREMENT_EXPR: | |
7348 | case POSTDECREMENT_EXPR: | |
7349 | case POSTINCREMENT_EXPR: | |
7350 | return build_nt | |
7351 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7352 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
7353 | ||
7354 | case CALL_EXPR: | |
7355 | return build_nt (code, | |
7356 | tsubst_copy (TREE_OPERAND (t, 0), args, | |
7357 | complain, in_decl), | |
7358 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, | |
7359 | in_decl), | |
7360 | NULL_TREE); | |
7361 | ||
7362 | case METHOD_CALL_EXPR: | |
7363 | return build_nt | |
7364 | (code, | |
7365 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7366 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7367 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl), | |
7368 | NULL_TREE); | |
7369 | ||
7370 | case STMT_EXPR: | |
7371 | /* This processing should really occur in tsubst_expr. However, | |
7372 | tsubst_expr does not recurse into expressions, since it | |
7373 | assumes that there aren't any statements inside them. So, we | |
7374 | need to expand the STMT_EXPR here. */ | |
7375 | if (!processing_template_decl) | |
7376 | { | |
7377 | tree stmt_expr = begin_stmt_expr (); | |
7378 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl); | |
7379 | return finish_stmt_expr (stmt_expr); | |
7380 | } | |
7381 | ||
7382 | return t; | |
7383 | ||
7384 | case COND_EXPR: | |
7385 | case MODOP_EXPR: | |
7386 | case PSEUDO_DTOR_EXPR: | |
7387 | { | |
7388 | r = build_nt | |
7389 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7390 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7391 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
7392 | return r; | |
7393 | } | |
7394 | ||
7395 | case NEW_EXPR: | |
7396 | { | |
7397 | r = build_nt | |
7398 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7399 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7400 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
7401 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); | |
7402 | return r; | |
7403 | } | |
7404 | ||
7405 | case DELETE_EXPR: | |
7406 | { | |
7407 | r = build_nt | |
7408 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7409 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
7410 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); | |
7411 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
7412 | return r; | |
7413 | } | |
7414 | ||
7415 | case TEMPLATE_ID_EXPR: | |
7416 | { | |
7417 | /* Substituted template arguments */ | |
7418 | tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain, | |
7419 | in_decl); | |
7420 | ||
7421 | if (targs && TREE_CODE (targs) == TREE_LIST) | |
7422 | { | |
7423 | tree chain; | |
7424 | for (chain = targs; chain; chain = TREE_CHAIN (chain)) | |
7425 | TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain)); | |
7426 | } | |
7427 | else if (targs) | |
7428 | { | |
7429 | int i; | |
7430 | for (i = 0; i < TREE_VEC_LENGTH (targs); ++i) | |
7431 | TREE_VEC_ELT (targs, i) | |
7432 | = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i)); | |
7433 | } | |
7434 | ||
7435 | return lookup_template_function | |
7436 | (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs); | |
7437 | } | |
7438 | ||
7439 | case TREE_LIST: | |
7440 | { | |
7441 | tree purpose, value, chain; | |
7442 | ||
7443 | if (t == void_list_node) | |
7444 | return t; | |
7445 | ||
7446 | purpose = TREE_PURPOSE (t); | |
7447 | if (purpose) | |
7448 | purpose = tsubst_copy (purpose, args, complain, in_decl); | |
7449 | value = TREE_VALUE (t); | |
7450 | if (value) | |
7451 | value = tsubst_copy (value, args, complain, in_decl); | |
7452 | chain = TREE_CHAIN (t); | |
7453 | if (chain && chain != void_type_node) | |
7454 | chain = tsubst_copy (chain, args, complain, in_decl); | |
7455 | if (purpose == TREE_PURPOSE (t) | |
7456 | && value == TREE_VALUE (t) | |
7457 | && chain == TREE_CHAIN (t)) | |
7458 | return t; | |
7459 | return tree_cons (purpose, value, chain); | |
7460 | } | |
7461 | ||
7462 | case RECORD_TYPE: | |
7463 | case UNION_TYPE: | |
7464 | case ENUMERAL_TYPE: | |
7465 | case INTEGER_TYPE: | |
7466 | case TEMPLATE_TYPE_PARM: | |
7467 | case TEMPLATE_TEMPLATE_PARM: | |
7468 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
7469 | case TEMPLATE_PARM_INDEX: | |
7470 | case POINTER_TYPE: | |
7471 | case REFERENCE_TYPE: | |
7472 | case OFFSET_TYPE: | |
7473 | case FUNCTION_TYPE: | |
7474 | case METHOD_TYPE: | |
7475 | case ARRAY_TYPE: | |
7476 | case TYPENAME_TYPE: | |
7477 | case UNBOUND_CLASS_TEMPLATE: | |
7478 | case TYPEOF_TYPE: | |
7479 | case TYPE_DECL: | |
7480 | return tsubst (t, args, complain, in_decl); | |
7481 | ||
7482 | case IDENTIFIER_NODE: | |
7483 | if (IDENTIFIER_TYPENAME_P (t)) | |
7484 | { | |
7485 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
7486 | return mangle_conv_op_name_for_type (new_type); | |
7487 | } | |
7488 | else | |
7489 | return t; | |
7490 | ||
7491 | case CONSTRUCTOR: | |
7492 | { | |
7493 | r = build | |
7494 | (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7495 | NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args, | |
7496 | complain, in_decl)); | |
7497 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); | |
7498 | return r; | |
7499 | } | |
7500 | ||
7501 | case VA_ARG_EXPR: | |
7502 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, | |
7503 | in_decl), | |
7504 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
7505 | ||
7506 | default: | |
7507 | return t; | |
7508 | } | |
7509 | } | |
7510 | ||
7511 | /* Like tsubst_copy for expressions, etc. but also does semantic | |
7512 | processing. */ | |
7513 | ||
7514 | static tree | |
7515 | tsubst_expr (t, args, complain, in_decl) | |
7516 | tree t, args; | |
7517 | tsubst_flags_t complain; | |
7518 | tree in_decl; | |
7519 | { | |
7520 | tree stmt, tmp; | |
7521 | ||
7522 | if (t == NULL_TREE || t == error_mark_node) | |
7523 | return t; | |
7524 | ||
7525 | if (processing_template_decl) | |
7526 | return tsubst_copy (t, args, complain, in_decl); | |
7527 | ||
7528 | if (!statement_code_p (TREE_CODE (t))) | |
7529 | return tsubst_copy_and_build (t, args, complain, in_decl); | |
7530 | ||
7531 | switch (TREE_CODE (t)) | |
7532 | { | |
7533 | case CTOR_INITIALIZER: | |
7534 | prep_stmt (t); | |
7535 | finish_mem_initializers (tsubst_initializer_list | |
7536 | (TREE_OPERAND (t, 0), args)); | |
7537 | break; | |
7538 | ||
7539 | case RETURN_STMT: | |
7540 | prep_stmt (t); | |
7541 | finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t), | |
7542 | args, complain, in_decl)); | |
7543 | break; | |
7544 | ||
7545 | case EXPR_STMT: | |
7546 | prep_stmt (t); | |
7547 | finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t), | |
7548 | args, complain, in_decl)); | |
7549 | break; | |
7550 | ||
7551 | case USING_STMT: | |
7552 | prep_stmt (t); | |
7553 | do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t), | |
7554 | args, complain, in_decl)); | |
7555 | break; | |
7556 | ||
7557 | case DECL_STMT: | |
7558 | { | |
7559 | tree decl; | |
7560 | tree init; | |
7561 | ||
7562 | prep_stmt (t); | |
7563 | decl = DECL_STMT_DECL (t); | |
7564 | if (TREE_CODE (decl) == LABEL_DECL) | |
7565 | finish_label_decl (DECL_NAME (decl)); | |
7566 | else if (TREE_CODE (decl) == USING_DECL) | |
7567 | { | |
7568 | tree scope = DECL_INITIAL (decl); | |
7569 | tree name = DECL_NAME (decl); | |
7570 | ||
7571 | scope = tsubst_expr (scope, args, complain, in_decl); | |
7572 | do_local_using_decl (lookup_qualified_name (scope, | |
7573 | name, | |
7574 | /*is_type_p=*/0, | |
7575 | /*flags=*/0)); | |
7576 | } | |
7577 | else | |
7578 | { | |
7579 | init = DECL_INITIAL (decl); | |
7580 | decl = tsubst (decl, args, complain, in_decl); | |
7581 | if (decl != error_mark_node) | |
7582 | { | |
7583 | if (init) | |
7584 | DECL_INITIAL (decl) = error_mark_node; | |
7585 | /* By marking the declaration as instantiated, we avoid | |
7586 | trying to instantiate it. Since instantiate_decl can't | |
7587 | handle local variables, and since we've already done | |
7588 | all that needs to be done, that's the right thing to | |
7589 | do. */ | |
7590 | if (TREE_CODE (decl) == VAR_DECL) | |
7591 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
7592 | if (TREE_CODE (decl) == VAR_DECL | |
7593 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
7594 | /* Anonymous aggregates are a special case. */ | |
7595 | finish_anon_union (decl); | |
7596 | else | |
7597 | { | |
7598 | maybe_push_decl (decl); | |
7599 | if (DECL_PRETTY_FUNCTION_P (decl)) | |
7600 | { | |
7601 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
7602 | initializer. */ | |
7603 | const char *const name | |
7604 | = cxx_printable_name (current_function_decl, 2); | |
7605 | init = cp_fname_init (name); | |
7606 | TREE_TYPE (decl) = TREE_TYPE (init); | |
7607 | } | |
7608 | else | |
7609 | init = tsubst_expr (init, args, complain, in_decl); | |
7610 | cp_finish_decl (decl, init, NULL_TREE, 0); | |
7611 | } | |
7612 | } | |
7613 | } | |
7614 | ||
7615 | /* A DECL_STMT can also be used as an expression, in the condition | |
7616 | clause of an if/for/while construct. If we aren't followed by | |
7617 | another statement, return our decl. */ | |
7618 | if (TREE_CHAIN (t) == NULL_TREE) | |
7619 | return decl; | |
7620 | } | |
7621 | break; | |
7622 | ||
7623 | case FOR_STMT: | |
7624 | { | |
7625 | prep_stmt (t); | |
7626 | ||
7627 | stmt = begin_for_stmt (); | |
7628 | tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl); | |
7629 | finish_for_init_stmt (stmt); | |
7630 | finish_for_cond (tsubst_expr (FOR_COND (t), | |
7631 | args, complain, in_decl), | |
7632 | stmt); | |
7633 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); | |
7634 | finish_for_expr (tmp, stmt); | |
7635 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); | |
7636 | finish_for_stmt (stmt); | |
7637 | } | |
7638 | break; | |
7639 | ||
7640 | case WHILE_STMT: | |
7641 | { | |
7642 | prep_stmt (t); | |
7643 | stmt = begin_while_stmt (); | |
7644 | finish_while_stmt_cond (tsubst_expr (WHILE_COND (t), | |
7645 | args, complain, in_decl), | |
7646 | stmt); | |
7647 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); | |
7648 | finish_while_stmt (stmt); | |
7649 | } | |
7650 | break; | |
7651 | ||
7652 | case DO_STMT: | |
7653 | { | |
7654 | prep_stmt (t); | |
7655 | stmt = begin_do_stmt (); | |
7656 | tsubst_expr (DO_BODY (t), args, complain, in_decl); | |
7657 | finish_do_body (stmt); | |
7658 | finish_do_stmt (tsubst_expr (DO_COND (t), | |
7659 | args, complain, in_decl), | |
7660 | stmt); | |
7661 | } | |
7662 | break; | |
7663 | ||
7664 | case IF_STMT: | |
7665 | { | |
7666 | prep_stmt (t); | |
7667 | stmt = begin_if_stmt (); | |
7668 | finish_if_stmt_cond (tsubst_expr (IF_COND (t), | |
7669 | args, complain, in_decl), | |
7670 | stmt); | |
7671 | ||
7672 | if (tmp = THEN_CLAUSE (t), tmp) | |
7673 | { | |
7674 | tsubst_expr (tmp, args, complain, in_decl); | |
7675 | finish_then_clause (stmt); | |
7676 | } | |
7677 | ||
7678 | if (tmp = ELSE_CLAUSE (t), tmp) | |
7679 | { | |
7680 | begin_else_clause (); | |
7681 | tsubst_expr (tmp, args, complain, in_decl); | |
7682 | finish_else_clause (stmt); | |
7683 | } | |
7684 | ||
7685 | finish_if_stmt (); | |
7686 | } | |
7687 | break; | |
7688 | ||
7689 | case COMPOUND_STMT: | |
7690 | { | |
7691 | prep_stmt (t); | |
7692 | if (COMPOUND_STMT_BODY_BLOCK (t)) | |
7693 | stmt = begin_function_body (); | |
7694 | else | |
7695 | stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)); | |
7696 | ||
7697 | tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl); | |
7698 | ||
7699 | if (COMPOUND_STMT_BODY_BLOCK (t)) | |
7700 | finish_function_body (stmt); | |
7701 | else | |
7702 | finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt); | |
7703 | } | |
7704 | break; | |
7705 | ||
7706 | case BREAK_STMT: | |
7707 | prep_stmt (t); | |
7708 | finish_break_stmt (); | |
7709 | break; | |
7710 | ||
7711 | case CONTINUE_STMT: | |
7712 | prep_stmt (t); | |
7713 | finish_continue_stmt (); | |
7714 | break; | |
7715 | ||
7716 | case SWITCH_STMT: | |
7717 | { | |
7718 | tree val; | |
7719 | ||
7720 | prep_stmt (t); | |
7721 | stmt = begin_switch_stmt (); | |
7722 | val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl); | |
7723 | finish_switch_cond (val, stmt); | |
7724 | tsubst_expr (SWITCH_BODY (t), args, complain, in_decl); | |
7725 | finish_switch_stmt (stmt); | |
7726 | } | |
7727 | break; | |
7728 | ||
7729 | case CASE_LABEL: | |
7730 | prep_stmt (t); | |
7731 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), | |
7732 | tsubst_expr (CASE_HIGH (t), args, complain, | |
7733 | in_decl)); | |
7734 | break; | |
7735 | ||
7736 | case LABEL_STMT: | |
7737 | lineno = STMT_LINENO (t); | |
7738 | finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t))); | |
7739 | break; | |
7740 | ||
7741 | case FILE_STMT: | |
7742 | input_filename = FILE_STMT_FILENAME (t); | |
7743 | add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t))); | |
7744 | break; | |
7745 | ||
7746 | case GOTO_STMT: | |
7747 | prep_stmt (t); | |
7748 | tmp = GOTO_DESTINATION (t); | |
7749 | if (TREE_CODE (tmp) != LABEL_DECL) | |
7750 | /* Computed goto's must be tsubst'd into. On the other hand, | |
7751 | non-computed gotos must not be; the identifier in question | |
7752 | will have no binding. */ | |
7753 | tmp = tsubst_expr (tmp, args, complain, in_decl); | |
7754 | else | |
7755 | tmp = DECL_NAME (tmp); | |
7756 | finish_goto_stmt (tmp); | |
7757 | break; | |
7758 | ||
7759 | case ASM_STMT: | |
7760 | prep_stmt (t); | |
7761 | tmp = finish_asm_stmt | |
7762 | (ASM_CV_QUAL (t), | |
7763 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), | |
7764 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
7765 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
7766 | tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl)); | |
7767 | ASM_INPUT_P (tmp) = ASM_INPUT_P (t); | |
7768 | break; | |
7769 | ||
7770 | case TRY_BLOCK: | |
7771 | prep_stmt (t); | |
7772 | if (CLEANUP_P (t)) | |
7773 | { | |
7774 | stmt = begin_try_block (); | |
7775 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); | |
7776 | finish_cleanup_try_block (stmt); | |
7777 | finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args, | |
7778 | complain, in_decl), | |
7779 | stmt); | |
7780 | } | |
7781 | else | |
7782 | { | |
7783 | if (FN_TRY_BLOCK_P (t)) | |
7784 | stmt = begin_function_try_block (); | |
7785 | else | |
7786 | stmt = begin_try_block (); | |
7787 | ||
7788 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); | |
7789 | ||
7790 | if (FN_TRY_BLOCK_P (t)) | |
7791 | finish_function_try_block (stmt); | |
7792 | else | |
7793 | finish_try_block (stmt); | |
7794 | ||
7795 | tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl); | |
7796 | if (FN_TRY_BLOCK_P (t)) | |
7797 | finish_function_handler_sequence (stmt); | |
7798 | else | |
7799 | finish_handler_sequence (stmt); | |
7800 | } | |
7801 | break; | |
7802 | ||
7803 | case HANDLER: | |
7804 | { | |
7805 | tree decl; | |
7806 | ||
7807 | prep_stmt (t); | |
7808 | stmt = begin_handler (); | |
7809 | if (HANDLER_PARMS (t)) | |
7810 | { | |
7811 | decl = DECL_STMT_DECL (HANDLER_PARMS (t)); | |
7812 | decl = tsubst (decl, args, complain, in_decl); | |
7813 | /* Prevent instantiate_decl from trying to instantiate | |
7814 | this variable. We've already done all that needs to be | |
7815 | done. */ | |
7816 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
7817 | } | |
7818 | else | |
7819 | decl = NULL_TREE; | |
7820 | finish_handler_parms (decl, stmt); | |
7821 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); | |
7822 | finish_handler (stmt); | |
7823 | } | |
7824 | break; | |
7825 | ||
7826 | case TAG_DEFN: | |
7827 | prep_stmt (t); | |
7828 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); | |
7829 | break; | |
7830 | ||
7831 | default: | |
7832 | abort (); | |
7833 | } | |
7834 | ||
7835 | return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl); | |
7836 | } | |
7837 | ||
7838 | /* Like tsubst but deals with expressions and performs semantic | |
7839 | analysis. */ | |
7840 | ||
7841 | static tree | |
7842 | tsubst_copy_and_build (t, args, complain, in_decl) | |
7843 | tree t, args; | |
7844 | tsubst_flags_t complain; | |
7845 | tree in_decl; | |
7846 | { | |
7847 | if (t == NULL_TREE || t == error_mark_node) | |
7848 | return t; | |
7849 | ||
7850 | switch (TREE_CODE (t)) | |
7851 | { | |
7852 | case IDENTIFIER_NODE: | |
7853 | if (IDENTIFIER_TYPENAME_P (t)) | |
7854 | { | |
7855 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
7856 | return do_identifier (mangle_conv_op_name_for_type (new_type), | |
7857 | NULL_TREE); | |
7858 | } | |
7859 | else | |
7860 | return do_identifier (t, NULL_TREE); | |
7861 | ||
7862 | case LOOKUP_EXPR: | |
7863 | { | |
7864 | if (LOOKUP_EXPR_GLOBAL (t)) | |
7865 | { | |
7866 | tree token | |
7867 | = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7868 | return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token)); | |
7869 | } | |
7870 | else | |
7871 | { | |
7872 | t = do_identifier | |
7873 | (tsubst_copy | |
7874 | (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7875 | NULL_TREE); | |
7876 | if (TREE_CODE (t) == ALIAS_DECL) | |
7877 | t = DECL_INITIAL (t); | |
7878 | return t; | |
7879 | } | |
7880 | } | |
7881 | ||
7882 | case TEMPLATE_ID_EXPR: | |
7883 | { | |
7884 | tree object; | |
7885 | tree template | |
7886 | = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7887 | ||
7888 | if (TREE_CODE (template) == COMPONENT_REF) | |
7889 | { | |
7890 | object = TREE_OPERAND (template, 0); | |
7891 | template = TREE_OPERAND (template, 1); | |
7892 | } | |
7893 | else | |
7894 | object = NULL_TREE; | |
7895 | ||
7896 | template = lookup_template_function | |
7897 | (template, | |
7898 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
7899 | ||
7900 | if (object) | |
7901 | return build (COMPONENT_REF, TREE_TYPE (template), | |
7902 | object, template); | |
7903 | else | |
7904 | return template; | |
7905 | } | |
7906 | ||
7907 | case INDIRECT_REF: | |
7908 | return build_x_indirect_ref | |
7909 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7910 | "unary *"); | |
7911 | ||
7912 | case CAST_EXPR: | |
7913 | return build_functional_cast | |
7914 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7915 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
7916 | ||
7917 | case REINTERPRET_CAST_EXPR: | |
7918 | return build_reinterpret_cast | |
7919 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7920 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
7921 | ||
7922 | case CONST_CAST_EXPR: | |
7923 | return build_const_cast | |
7924 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7925 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
7926 | ||
7927 | case DYNAMIC_CAST_EXPR: | |
7928 | return build_dynamic_cast | |
7929 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7930 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
7931 | ||
7932 | case STATIC_CAST_EXPR: | |
7933 | return build_static_cast | |
7934 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7935 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
7936 | ||
7937 | case PREDECREMENT_EXPR: | |
7938 | case PREINCREMENT_EXPR: | |
7939 | case POSTDECREMENT_EXPR: | |
7940 | case POSTINCREMENT_EXPR: | |
7941 | if (TREE_TYPE (t)) | |
7942 | return tsubst_copy (t, args, complain, in_decl); | |
7943 | else | |
7944 | return build_x_unary_op | |
7945 | (TREE_CODE (t), | |
7946 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, | |
7947 | in_decl)); | |
7948 | ||
7949 | case NEGATE_EXPR: | |
7950 | case BIT_NOT_EXPR: | |
7951 | if (TREE_TYPE (t)) | |
7952 | return tsubst_copy (t, args, complain, in_decl); | |
7953 | else | |
7954 | return build_x_unary_op | |
7955 | (TREE_CODE (t), | |
7956 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, | |
7957 | in_decl)); | |
7958 | ||
7959 | case ABS_EXPR: | |
7960 | if (TREE_TYPE (t)) | |
7961 | return t; | |
7962 | return build_x_unary_op | |
7963 | (TREE_CODE (t), | |
7964 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
7965 | ||
7966 | case TRUTH_NOT_EXPR: | |
7967 | case ADDR_EXPR: | |
7968 | case CONVERT_EXPR: /* Unary + */ | |
7969 | case REALPART_EXPR: | |
7970 | case IMAGPART_EXPR: | |
7971 | if (TREE_TYPE (t)) | |
7972 | return tsubst_copy (t, args, complain, in_decl); | |
7973 | else | |
7974 | return build_x_unary_op | |
7975 | (TREE_CODE (t), | |
7976 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, | |
7977 | in_decl)); | |
7978 | ||
7979 | case PLUS_EXPR: | |
7980 | case MINUS_EXPR: | |
7981 | case MULT_EXPR: | |
7982 | case TRUNC_DIV_EXPR: | |
7983 | case CEIL_DIV_EXPR: | |
7984 | case FLOOR_DIV_EXPR: | |
7985 | case ROUND_DIV_EXPR: | |
7986 | case EXACT_DIV_EXPR: | |
7987 | case BIT_AND_EXPR: | |
7988 | case BIT_ANDTC_EXPR: | |
7989 | case BIT_IOR_EXPR: | |
7990 | case BIT_XOR_EXPR: | |
7991 | case TRUNC_MOD_EXPR: | |
7992 | case FLOOR_MOD_EXPR: | |
7993 | case TRUTH_ANDIF_EXPR: | |
7994 | case TRUTH_ORIF_EXPR: | |
7995 | case TRUTH_AND_EXPR: | |
7996 | case TRUTH_OR_EXPR: | |
7997 | case RSHIFT_EXPR: | |
7998 | case LSHIFT_EXPR: | |
7999 | case RROTATE_EXPR: | |
8000 | case LROTATE_EXPR: | |
8001 | case EQ_EXPR: | |
8002 | case NE_EXPR: | |
8003 | case MAX_EXPR: | |
8004 | case MIN_EXPR: | |
8005 | case LE_EXPR: | |
8006 | case GE_EXPR: | |
8007 | case LT_EXPR: | |
8008 | case GT_EXPR: | |
8009 | case MEMBER_REF: | |
8010 | return build_x_binary_op | |
8011 | (TREE_CODE (t), | |
8012 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8013 | tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
8014 | ||
8015 | case DOTSTAR_EXPR: | |
8016 | return build_m_component_ref | |
8017 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8018 | tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
8019 | ||
8020 | case SCOPE_REF: | |
8021 | return build_offset_ref | |
8022 | (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8023 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
8024 | ||
8025 | case ARRAY_REF: | |
8026 | { | |
8027 | if (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl) | |
8028 | == NULL_TREE) | |
8029 | /* new-type-id */ | |
8030 | return build_nt | |
8031 | (ARRAY_REF, NULL_TREE, | |
8032 | tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, | |
8033 | in_decl)); | |
8034 | ||
8035 | return grok_array_decl | |
8036 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, | |
8037 | in_decl), | |
8038 | tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, | |
8039 | in_decl)); | |
8040 | } | |
8041 | ||
8042 | case SIZEOF_EXPR: | |
8043 | case ALIGNOF_EXPR: | |
8044 | { | |
8045 | tree r = | |
8046 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl); | |
8047 | if (!TYPE_P (r)) | |
8048 | return TREE_CODE (t) == SIZEOF_EXPR ? | |
8049 | expr_sizeof (r) : c_alignof_expr (r); | |
8050 | else | |
8051 | return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true); | |
8052 | } | |
8053 | ||
8054 | case MODOP_EXPR: | |
8055 | return build_x_modify_expr | |
8056 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8057 | TREE_CODE (TREE_OPERAND (t, 1)), | |
8058 | tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
8059 | ||
8060 | case ARROW_EXPR: | |
8061 | return build_x_arrow | |
8062 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
8063 | ||
8064 | case NEW_EXPR: | |
8065 | return build_new | |
8066 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8067 | tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8068 | tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl), | |
8069 | NEW_EXPR_USE_GLOBAL (t)); | |
8070 | ||
8071 | case DELETE_EXPR: | |
8072 | return delete_sanity | |
8073 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8074 | tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8075 | DELETE_EXPR_USE_VEC (t), | |
8076 | DELETE_EXPR_USE_GLOBAL (t)); | |
8077 | ||
8078 | case COMPOUND_EXPR: | |
8079 | { | |
8080 | if (tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl) | |
8081 | == NULL_TREE) | |
8082 | return build_x_compound_expr | |
8083 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, | |
8084 | in_decl)); | |
8085 | else | |
8086 | abort (); | |
8087 | } | |
8088 | ||
8089 | case METHOD_CALL_EXPR: | |
8090 | { | |
8091 | tree method | |
8092 | = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
8093 | ||
8094 | if (TREE_CODE (method) == SCOPE_REF) | |
8095 | { | |
8096 | tree name = TREE_OPERAND (method, 1); | |
8097 | ||
8098 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
8099 | name = build_nt (TEMPLATE_ID_EXPR, | |
8100 | TREE_OPERAND (name, 0), | |
8101 | TREE_OPERAND (name, 1)); | |
8102 | ||
8103 | return build_scoped_method_call | |
8104 | (tsubst_copy_and_build | |
8105 | (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8106 | TREE_OPERAND (method, 0), | |
8107 | name, | |
8108 | tsubst_copy_and_build | |
8109 | (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
8110 | } | |
8111 | else | |
8112 | { | |
8113 | /* We can get a TEMPLATE_ID_EXPR here on code like: | |
8114 | ||
8115 | x->f<2>(); | |
8116 | ||
8117 | so we must resolve that. However, we can also get things | |
8118 | like a BIT_NOT_EXPR here, when referring to a destructor, | |
8119 | and things like that are not correctly resolved by this | |
8120 | function so just use it when we really need it. */ | |
8121 | if (TREE_CODE (method) == TEMPLATE_ID_EXPR) | |
8122 | method = lookup_template_function | |
8123 | (TREE_OPERAND (method, 0), | |
8124 | TREE_OPERAND (method, 1)); | |
8125 | ||
8126 | return build_method_call | |
8127 | (tsubst_copy_and_build | |
8128 | (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8129 | method, | |
8130 | tsubst_copy_and_build | |
8131 | (TREE_OPERAND (t, 2), args, complain, in_decl), | |
8132 | NULL_TREE, LOOKUP_NORMAL); | |
8133 | } | |
8134 | } | |
8135 | ||
8136 | case CALL_EXPR: | |
8137 | { | |
8138 | tree function, copy_args; | |
8139 | ||
8140 | function = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
8141 | copy_args = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, | |
8142 | complain, in_decl); | |
8143 | ||
8144 | if (BASELINK_P (function)) | |
8145 | return build_call_from_tree (function, copy_args, 1); | |
8146 | else if (TREE_CODE (function) == SCOPE_REF) | |
8147 | { | |
8148 | tree name = TREE_OPERAND (function, 1); | |
8149 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
8150 | name = build_nt (TEMPLATE_ID_EXPR, | |
8151 | TREE_OPERAND (name, 0), | |
8152 | TREE_OPERAND (name, 1)); | |
8153 | ||
8154 | function = resolve_scoped_fn_name (TREE_OPERAND (function, 0), | |
8155 | name); | |
8156 | ||
8157 | return build_call_from_tree (function, copy_args, 1); | |
8158 | } | |
8159 | else | |
8160 | { | |
8161 | tree name = function; | |
8162 | tree id; | |
8163 | ||
8164 | if (copy_args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR | |
8165 | && !LOOKUP_EXPR_GLOBAL (name) | |
8166 | && (TREE_CODE ((id = TREE_OPERAND (name, 0))) | |
8167 | == IDENTIFIER_NODE) | |
8168 | && (!current_class_type | |
8169 | || !lookup_member (current_class_type, id, 0, false))) | |
8170 | { | |
8171 | /* Do Koenig lookup if there are no class members. */ | |
8172 | name = do_identifier (id, copy_args); | |
8173 | } | |
8174 | else if (TREE_CODE (name) == TEMPLATE_ID_EXPR | |
8175 | || ! really_overloaded_fn (name)) | |
8176 | name = build_expr_from_tree (name); | |
8177 | ||
8178 | if (TREE_CODE (name) == OFFSET_REF) | |
8179 | return build_offset_ref_call_from_tree (name, copy_args); | |
8180 | if (TREE_CODE (name) == COMPONENT_REF) | |
8181 | return finish_object_call_expr (TREE_OPERAND (name, 1), | |
8182 | TREE_OPERAND (name, 0), | |
8183 | copy_args); | |
8184 | name = convert_from_reference (name); | |
8185 | return build_call_from_tree (name, copy_args, | |
8186 | /*disallow_virtual=*/false); | |
8187 | } | |
8188 | } | |
8189 | ||
8190 | case COND_EXPR: | |
8191 | return build_x_conditional_expr | |
8192 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8193 | tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8194 | tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
8195 | ||
8196 | case PSEUDO_DTOR_EXPR: | |
8197 | return finish_pseudo_destructor_expr | |
8198 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8199 | tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8200 | tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
8201 | ||
8202 | case TREE_LIST: | |
8203 | { | |
8204 | tree purpose, value, chain; | |
8205 | ||
8206 | if (t == void_list_node) | |
8207 | return t; | |
8208 | ||
8209 | purpose = TREE_PURPOSE (t); | |
8210 | if (purpose) | |
8211 | purpose = tsubst_copy_and_build (purpose, args, complain, in_decl); | |
8212 | value = TREE_VALUE (t); | |
8213 | if (value) | |
8214 | value = tsubst_copy_and_build (value, args, complain, in_decl); | |
8215 | chain = TREE_CHAIN (t); | |
8216 | if (chain && chain != void_type_node) | |
8217 | chain = tsubst_copy_and_build (chain, args, complain, in_decl); | |
8218 | if (purpose == TREE_PURPOSE (t) | |
8219 | && value == TREE_VALUE (t) | |
8220 | && chain == TREE_CHAIN (t)) | |
8221 | return t; | |
8222 | return tree_cons (purpose, value, chain); | |
8223 | } | |
8224 | ||
8225 | case COMPONENT_REF: | |
8226 | { | |
8227 | tree object = | |
8228 | tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl); | |
8229 | tree member = | |
8230 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl); | |
8231 | ||
8232 | if (!CLASS_TYPE_P (TREE_TYPE (object))) | |
8233 | { | |
8234 | if (TREE_CODE (member) == BIT_NOT_EXPR) | |
8235 | return finish_pseudo_destructor_expr (object, | |
8236 | NULL_TREE, | |
8237 | TREE_TYPE (object)); | |
8238 | else if (TREE_CODE (member) == SCOPE_REF | |
8239 | && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR)) | |
8240 | return finish_pseudo_destructor_expr (object, | |
8241 | object, | |
8242 | TREE_TYPE (object)); | |
8243 | } | |
8244 | else if (TREE_CODE (member) == SCOPE_REF | |
8245 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
8246 | { | |
8247 | tree tmpl; | |
8248 | tree args; | |
8249 | ||
8250 | /* Lookup the template functions now that we know what the | |
8251 | scope is. */ | |
8252 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
8253 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
8254 | member = lookup_qualified_name (TREE_OPERAND (member, 0), | |
8255 | tmpl, | |
8256 | /*is_type=*/0, | |
8257 | /*flags=*/0); | |
8258 | if (BASELINK_P (member)) | |
8259 | BASELINK_FUNCTIONS (member) | |
8260 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), | |
8261 | args); | |
8262 | else | |
8263 | { | |
8264 | error ("`%D' is not a member of `%T'", | |
8265 | tmpl, TREE_TYPE (object)); | |
8266 | return error_mark_node; | |
8267 | } | |
8268 | } | |
8269 | ||
8270 | return finish_class_member_access_expr (object, member); | |
8271 | } | |
8272 | ||
8273 | case THROW_EXPR: | |
8274 | return build_throw | |
8275 | (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
8276 | ||
8277 | case CONSTRUCTOR: | |
8278 | { | |
8279 | tree r; | |
8280 | tree elts; | |
8281 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8282 | bool purpose_p; | |
8283 | ||
8284 | /* digest_init will do the wrong thing if we let it. */ | |
8285 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
8286 | return t; | |
8287 | ||
8288 | r = NULL_TREE; | |
8289 | /* We do not want to process the purpose of aggregate | |
8290 | initializers as they are identifier nodes which will be | |
8291 | looked up by digest_init. */ | |
8292 | purpose_p = !(type && IS_AGGR_TYPE (type)); | |
8293 | for (elts = CONSTRUCTOR_ELTS (t); | |
8294 | elts; | |
8295 | elts = TREE_CHAIN (elts)) | |
8296 | { | |
8297 | tree purpose = TREE_PURPOSE (elts); | |
8298 | tree value = TREE_VALUE (elts); | |
8299 | ||
8300 | if (purpose && purpose_p) | |
8301 | purpose | |
8302 | = tsubst_copy_and_build (purpose, args, complain, in_decl); | |
8303 | value = tsubst_copy_and_build (value, args, complain, in_decl); | |
8304 | r = tree_cons (purpose, value, r); | |
8305 | } | |
8306 | ||
8307 | r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r)); | |
8308 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); | |
8309 | ||
8310 | if (type) | |
8311 | return digest_init (type, r, 0); | |
8312 | return r; | |
8313 | } | |
8314 | ||
8315 | case TYPEID_EXPR: | |
8316 | { | |
8317 | tree operand_0 | |
8318 | = tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, | |
8319 | in_decl); | |
8320 | ||
8321 | if (TYPE_P (operand_0)) | |
8322 | return get_typeid (operand_0); | |
8323 | return build_typeid (operand_0); | |
8324 | } | |
8325 | ||
8326 | case PARM_DECL: | |
8327 | return convert_from_reference (tsubst_copy (t, args, complain, in_decl)); | |
8328 | ||
8329 | case VAR_DECL: | |
8330 | return convert_from_reference (tsubst_copy (t, args, complain, in_decl)); | |
8331 | ||
8332 | case VA_ARG_EXPR: | |
8333 | return build_x_va_arg | |
8334 | (tsubst_copy_and_build | |
8335 | (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8336 | tsubst_copy (TREE_TYPE (t), args, complain, in_decl)); | |
8337 | ||
8338 | default: | |
8339 | return tsubst_copy (t, args, complain, in_decl); | |
8340 | } | |
8341 | } | |
8342 | ||
8343 | /* Instantiate the indicated variable or function template TMPL with | |
8344 | the template arguments in TARG_PTR. */ | |
8345 | ||
8346 | tree | |
8347 | instantiate_template (tmpl, targ_ptr) | |
8348 | tree tmpl, targ_ptr; | |
8349 | { | |
8350 | tree fndecl; | |
8351 | tree gen_tmpl; | |
8352 | tree spec; | |
8353 | int i, len; | |
8354 | tree inner_args; | |
8355 | ||
8356 | if (tmpl == error_mark_node) | |
8357 | return error_mark_node; | |
8358 | ||
8359 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283); | |
8360 | ||
8361 | /* If this function is a clone, handle it specially. */ | |
8362 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
8363 | { | |
8364 | tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr); | |
8365 | tree clone; | |
8366 | ||
8367 | /* Look for the clone. */ | |
8368 | for (clone = TREE_CHAIN (spec); | |
8369 | clone && DECL_CLONED_FUNCTION_P (clone); | |
8370 | clone = TREE_CHAIN (clone)) | |
8371 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) | |
8372 | return clone; | |
8373 | /* We should always have found the clone by now. */ | |
8374 | abort (); | |
8375 | return NULL_TREE; | |
8376 | } | |
8377 | ||
8378 | /* Check to see if we already have this specialization. */ | |
8379 | spec = retrieve_specialization (tmpl, targ_ptr); | |
8380 | if (spec != NULL_TREE) | |
8381 | return spec; | |
8382 | ||
8383 | gen_tmpl = most_general_template (tmpl); | |
8384 | if (tmpl != gen_tmpl) | |
8385 | { | |
8386 | /* The TMPL is a partial instantiation. To get a full set of | |
8387 | arguments we must add the arguments used to perform the | |
8388 | partial instantiation. */ | |
8389 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
8390 | targ_ptr); | |
8391 | ||
8392 | /* Check to see if we already have this specialization. */ | |
8393 | spec = retrieve_specialization (gen_tmpl, targ_ptr); | |
8394 | if (spec != NULL_TREE) | |
8395 | return spec; | |
8396 | } | |
8397 | ||
8398 | len = DECL_NTPARMS (gen_tmpl); | |
8399 | inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr); | |
8400 | i = len; | |
8401 | while (i--) | |
8402 | { | |
8403 | tree t = TREE_VEC_ELT (inner_args, i); | |
8404 | if (TYPE_P (t)) | |
8405 | { | |
8406 | tree nt = target_type (t); | |
8407 | if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt))) | |
8408 | { | |
8409 | error ("type `%T' composed from a local class is not a valid template-argument", t); | |
8410 | error (" trying to instantiate `%D'", gen_tmpl); | |
8411 | return error_mark_node; | |
8412 | } | |
8413 | } | |
8414 | } | |
8415 | ||
8416 | /* Make sure that we can see identifiers, and compute access | |
8417 | correctly. The desired FUNCTION_DECL for FNDECL may or may not be | |
8418 | created earlier. Let push_access_scope_real figure that out. */ | |
8419 | push_access_scope_real | |
8420 | (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr, | |
8421 | tf_error, gen_tmpl)); | |
8422 | ||
8423 | /* substitute template parameters */ | |
8424 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), | |
8425 | targ_ptr, tf_error, gen_tmpl); | |
8426 | ||
8427 | pop_access_scope (gen_tmpl); | |
8428 | ||
8429 | /* The DECL_TI_TEMPLATE should always be the immediate parent | |
8430 | template, not the most general template. */ | |
8431 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8432 | ||
8433 | if (flag_external_templates) | |
8434 | add_pending_template (fndecl); | |
8435 | ||
8436 | /* If we've just instantiated the main entry point for a function, | |
8437 | instantiate all the alternate entry points as well. We do this | |
8438 | by cloning the instantiation of the main entry point, not by | |
8439 | instantiating the template clones. */ | |
8440 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
8441 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
8442 | ||
8443 | return fndecl; | |
8444 | } | |
8445 | ||
8446 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the | |
8447 | arguments that are being used when calling it. TARGS is a vector | |
8448 | into which the deduced template arguments are placed. | |
8449 | ||
8450 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
8451 | all the types, and 1 for complete failure. An error message will be | |
8452 | printed only for an incomplete match. | |
8453 | ||
8454 | If FN is a conversion operator, or we are trying to produce a specific | |
8455 | specialization, RETURN_TYPE is the return type desired. | |
8456 | ||
8457 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
8458 | template-id. | |
8459 | ||
8460 | The parameter STRICT is one of: | |
8461 | ||
8462 | DEDUCE_CALL: | |
8463 | We are deducing arguments for a function call, as in | |
8464 | [temp.deduct.call]. | |
8465 | ||
8466 | DEDUCE_CONV: | |
8467 | We are deducing arguments for a conversion function, as in | |
8468 | [temp.deduct.conv]. | |
8469 | ||
8470 | DEDUCE_EXACT: | |
8471 | We are deducing arguments when doing an explicit instantiation | |
8472 | as in [temp.explicit], when determining an explicit specialization | |
8473 | as in [temp.expl.spec], or when taking the address of a function | |
8474 | template, as in [temp.deduct.funcaddr]. | |
8475 | ||
8476 | DEDUCE_ORDER: | |
8477 | We are deducing arguments when calculating the partial | |
8478 | ordering between specializations of function or class | |
8479 | templates, as in [temp.func.order] and [temp.class.order]. | |
8480 | ||
8481 | LEN is the number of parms to consider before returning success, or -1 | |
8482 | for all. This is used in partial ordering to avoid comparing parms for | |
8483 | which no actual argument was passed, since they are not considered in | |
8484 | overload resolution (and are explicitly excluded from consideration in | |
8485 | partial ordering in [temp.func.order]/6). */ | |
8486 | ||
8487 | int | |
8488 | fn_type_unification (fn, explicit_targs, targs, args, return_type, | |
8489 | strict, len) | |
8490 | tree fn, explicit_targs, targs, args, return_type; | |
8491 | unification_kind_t strict; | |
8492 | int len; | |
8493 | { | |
8494 | tree parms; | |
8495 | tree fntype; | |
8496 | int result; | |
8497 | ||
8498 | my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0); | |
8499 | ||
8500 | fntype = TREE_TYPE (fn); | |
8501 | if (explicit_targs) | |
8502 | { | |
8503 | /* [temp.deduct] | |
8504 | ||
8505 | The specified template arguments must match the template | |
8506 | parameters in kind (i.e., type, nontype, template), and there | |
8507 | must not be more arguments than there are parameters; | |
8508 | otherwise type deduction fails. | |
8509 | ||
8510 | Nontype arguments must match the types of the corresponding | |
8511 | nontype template parameters, or must be convertible to the | |
8512 | types of the corresponding nontype parameters as specified in | |
8513 | _temp.arg.nontype_, otherwise type deduction fails. | |
8514 | ||
8515 | All references in the function type of the function template | |
8516 | to the corresponding template parameters are replaced by the | |
8517 | specified template argument values. If a substitution in a | |
8518 | template parameter or in the function type of the function | |
8519 | template results in an invalid type, type deduction fails. */ | |
8520 | int i; | |
8521 | tree converted_args; | |
8522 | ||
8523 | converted_args | |
8524 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
8525 | explicit_targs, NULL_TREE, tf_none, | |
8526 | /*require_all_arguments=*/0)); | |
8527 | if (converted_args == error_mark_node) | |
8528 | return 1; | |
8529 | ||
8530 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); | |
8531 | if (fntype == error_mark_node) | |
8532 | return 1; | |
8533 | ||
8534 | /* Place the explicitly specified arguments in TARGS. */ | |
8535 | for (i = 0; i < TREE_VEC_LENGTH (targs); i++) | |
8536 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); | |
8537 | } | |
8538 | ||
8539 | parms = TYPE_ARG_TYPES (fntype); | |
8540 | /* Never do unification on the 'this' parameter. */ | |
8541 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) | |
8542 | parms = TREE_CHAIN (parms); | |
8543 | ||
8544 | if (return_type) | |
8545 | { | |
8546 | /* We've been given a return type to match, prepend it. */ | |
8547 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); | |
8548 | args = tree_cons (NULL_TREE, return_type, args); | |
8549 | if (len >= 0) | |
8550 | ++len; | |
8551 | } | |
8552 | ||
8553 | /* We allow incomplete unification without an error message here | |
8554 | because the standard doesn't seem to explicitly prohibit it. Our | |
8555 | callers must be ready to deal with unification failures in any | |
8556 | event. */ | |
8557 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
8558 | targs, parms, args, /*subr=*/0, | |
8559 | strict, /*allow_incomplete*/1, len); | |
8560 | ||
8561 | if (result == 0) | |
8562 | /* All is well so far. Now, check: | |
8563 | ||
8564 | [temp.deduct] | |
8565 | ||
8566 | When all template arguments have been deduced, all uses of | |
8567 | template parameters in nondeduced contexts are replaced with | |
8568 | the corresponding deduced argument values. If the | |
8569 | substitution results in an invalid type, as described above, | |
8570 | type deduction fails. */ | |
8571 | if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) | |
8572 | == error_mark_node) | |
8573 | return 1; | |
8574 | ||
8575 | return result; | |
8576 | } | |
8577 | ||
8578 | /* Adjust types before performing type deduction, as described in | |
8579 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
8580 | sections are symmetric. PARM is the type of a function parameter | |
8581 | or the return type of the conversion function. ARG is the type of | |
8582 | the argument passed to the call, or the type of the value | |
8583 | initialized with the result of the conversion function. */ | |
8584 | ||
8585 | static int | |
8586 | maybe_adjust_types_for_deduction (strict, parm, arg) | |
8587 | unification_kind_t strict; | |
8588 | tree* parm; | |
8589 | tree* arg; | |
8590 | { | |
8591 | int result = 0; | |
8592 | ||
8593 | switch (strict) | |
8594 | { | |
8595 | case DEDUCE_CALL: | |
8596 | break; | |
8597 | ||
8598 | case DEDUCE_CONV: | |
8599 | { | |
8600 | /* Swap PARM and ARG throughout the remainder of this | |
8601 | function; the handling is precisely symmetric since PARM | |
8602 | will initialize ARG rather than vice versa. */ | |
8603 | tree* temp = parm; | |
8604 | parm = arg; | |
8605 | arg = temp; | |
8606 | break; | |
8607 | } | |
8608 | ||
8609 | case DEDUCE_EXACT: | |
8610 | /* There is nothing to do in this case. */ | |
8611 | return 0; | |
8612 | ||
8613 | case DEDUCE_ORDER: | |
8614 | /* DR 214. [temp.func.order] is underspecified, and leads to no | |
8615 | ordering between things like `T *' and `T const &' for `U *'. | |
8616 | The former has T=U and the latter T=U*. The former looks more | |
8617 | specialized and John Spicer considers it well-formed (the EDG | |
8618 | compiler accepts it). | |
8619 | ||
8620 | John also confirms that deduction should proceed as in a function | |
8621 | call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL. | |
8622 | However, in ordering, ARG can have REFERENCE_TYPE, but no argument | |
8623 | to an actual call can have such a type. | |
8624 | ||
8625 | If both ARG and PARM are REFERENCE_TYPE, we change neither. | |
8626 | If only ARG is a REFERENCE_TYPE, we look through that and then | |
8627 | proceed as with DEDUCE_CALL (which could further convert it). */ | |
8628 | if (TREE_CODE (*arg) == REFERENCE_TYPE) | |
8629 | { | |
8630 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
8631 | return 0; | |
8632 | *arg = TREE_TYPE (*arg); | |
8633 | } | |
8634 | break; | |
8635 | default: | |
8636 | abort (); | |
8637 | } | |
8638 | ||
8639 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
8640 | { | |
8641 | /* [temp.deduct.call] | |
8642 | ||
8643 | If P is not a reference type: | |
8644 | ||
8645 | --If A is an array type, the pointer type produced by the | |
8646 | array-to-pointer standard conversion (_conv.array_) is | |
8647 | used in place of A for type deduction; otherwise, | |
8648 | ||
8649 | --If A is a function type, the pointer type produced by | |
8650 | the function-to-pointer standard conversion | |
8651 | (_conv.func_) is used in place of A for type deduction; | |
8652 | otherwise, | |
8653 | ||
8654 | --If A is a cv-qualified type, the top level | |
8655 | cv-qualifiers of A's type are ignored for type | |
8656 | deduction. */ | |
8657 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
8658 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
8659 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) | |
8660 | *arg = build_pointer_type (*arg); | |
8661 | else | |
8662 | *arg = TYPE_MAIN_VARIANT (*arg); | |
8663 | } | |
8664 | ||
8665 | /* [temp.deduct.call] | |
8666 | ||
8667 | If P is a cv-qualified type, the top level cv-qualifiers | |
8668 | of P's type are ignored for type deduction. If P is a | |
8669 | reference type, the type referred to by P is used for | |
8670 | type deduction. */ | |
8671 | *parm = TYPE_MAIN_VARIANT (*parm); | |
8672 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
8673 | { | |
8674 | *parm = TREE_TYPE (*parm); | |
8675 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
8676 | } | |
8677 | return result; | |
8678 | } | |
8679 | ||
8680 | /* Most parms like fn_type_unification. | |
8681 | ||
8682 | If SUBR is 1, we're being called recursively (to unify the | |
8683 | arguments of a function or method parameter of a function | |
8684 | template). */ | |
8685 | ||
8686 | static int | |
8687 | type_unification_real (tparms, targs, xparms, xargs, subr, | |
8688 | strict, allow_incomplete, xlen) | |
8689 | tree tparms, targs, xparms, xargs; | |
8690 | int subr; | |
8691 | unification_kind_t strict; | |
8692 | int allow_incomplete, xlen; | |
8693 | { | |
8694 | tree parm, arg; | |
8695 | int i; | |
8696 | int ntparms = TREE_VEC_LENGTH (tparms); | |
8697 | int sub_strict; | |
8698 | int saw_undeduced = 0; | |
8699 | tree parms, args; | |
8700 | int len; | |
8701 | ||
8702 | my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289); | |
8703 | my_friendly_assert (xparms == NULL_TREE | |
8704 | || TREE_CODE (xparms) == TREE_LIST, 290); | |
8705 | /* ARGS could be NULL. */ | |
8706 | if (xargs) | |
8707 | my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291); | |
8708 | my_friendly_assert (ntparms > 0, 292); | |
8709 | ||
8710 | switch (strict) | |
8711 | { | |
8712 | case DEDUCE_CALL: | |
8713 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL | |
8714 | | UNIFY_ALLOW_DERIVED); | |
8715 | break; | |
8716 | ||
8717 | case DEDUCE_CONV: | |
8718 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
8719 | break; | |
8720 | ||
8721 | case DEDUCE_EXACT: | |
8722 | sub_strict = UNIFY_ALLOW_NONE; | |
8723 | break; | |
8724 | ||
8725 | case DEDUCE_ORDER: | |
8726 | sub_strict = UNIFY_ALLOW_NONE; | |
8727 | break; | |
8728 | ||
8729 | default: | |
8730 | abort (); | |
8731 | } | |
8732 | ||
8733 | if (xlen == 0) | |
8734 | return 0; | |
8735 | ||
8736 | again: | |
8737 | parms = xparms; | |
8738 | args = xargs; | |
8739 | len = xlen; | |
8740 | ||
8741 | while (parms | |
8742 | && parms != void_list_node | |
8743 | && args | |
8744 | && args != void_list_node) | |
8745 | { | |
8746 | parm = TREE_VALUE (parms); | |
8747 | parms = TREE_CHAIN (parms); | |
8748 | arg = TREE_VALUE (args); | |
8749 | args = TREE_CHAIN (args); | |
8750 | ||
8751 | if (arg == error_mark_node) | |
8752 | return 1; | |
8753 | if (arg == unknown_type_node) | |
8754 | /* We can't deduce anything from this, but we might get all the | |
8755 | template args from other function args. */ | |
8756 | continue; | |
8757 | ||
8758 | /* Conversions will be performed on a function argument that | |
8759 | corresponds with a function parameter that contains only | |
8760 | non-deducible template parameters and explicitly specified | |
8761 | template parameters. */ | |
8762 | if (! uses_template_parms (parm)) | |
8763 | { | |
8764 | tree type; | |
8765 | ||
8766 | if (!TYPE_P (arg)) | |
8767 | type = TREE_TYPE (arg); | |
8768 | else | |
8769 | { | |
8770 | type = arg; | |
8771 | arg = NULL_TREE; | |
8772 | } | |
8773 | ||
8774 | if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER) | |
8775 | { | |
8776 | if (same_type_p (parm, type)) | |
8777 | continue; | |
8778 | } | |
8779 | else | |
8780 | /* It might work; we shouldn't check now, because we might | |
8781 | get into infinite recursion. Overload resolution will | |
8782 | handle it. */ | |
8783 | continue; | |
8784 | ||
8785 | return 1; | |
8786 | } | |
8787 | ||
8788 | if (!TYPE_P (arg)) | |
8789 | { | |
8790 | my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293); | |
8791 | if (type_unknown_p (arg)) | |
8792 | { | |
8793 | /* [temp.deduct.type] A template-argument can be deduced from | |
8794 | a pointer to function or pointer to member function | |
8795 | argument if the set of overloaded functions does not | |
8796 | contain function templates and at most one of a set of | |
8797 | overloaded functions provides a unique match. */ | |
8798 | ||
8799 | if (resolve_overloaded_unification | |
8800 | (tparms, targs, parm, arg, strict, sub_strict) | |
8801 | != 0) | |
8802 | return 1; | |
8803 | continue; | |
8804 | } | |
8805 | arg = TREE_TYPE (arg); | |
8806 | } | |
8807 | ||
8808 | { | |
8809 | int arg_strict = sub_strict; | |
8810 | ||
8811 | if (!subr) | |
8812 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
8813 | ||
8814 | if (unify (tparms, targs, parm, arg, arg_strict)) | |
8815 | return 1; | |
8816 | } | |
8817 | ||
8818 | /* Are we done with the interesting parms? */ | |
8819 | if (--len == 0) | |
8820 | goto done; | |
8821 | } | |
8822 | /* Fail if we've reached the end of the parm list, and more args | |
8823 | are present, and the parm list isn't variadic. */ | |
8824 | if (args && args != void_list_node && parms == void_list_node) | |
8825 | return 1; | |
8826 | /* Fail if parms are left and they don't have default values. */ | |
8827 | if (parms | |
8828 | && parms != void_list_node | |
8829 | && TREE_PURPOSE (parms) == NULL_TREE) | |
8830 | return 1; | |
8831 | ||
8832 | done: | |
8833 | if (!subr) | |
8834 | for (i = 0; i < ntparms; i++) | |
8835 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) | |
8836 | { | |
8837 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
8838 | ||
8839 | /* If this is an undeduced nontype parameter that depends on | |
8840 | a type parameter, try another pass; its type may have been | |
8841 | deduced from a later argument than the one from which | |
8842 | this parameter can be deduced. */ | |
8843 | if (TREE_CODE (tparm) == PARM_DECL | |
8844 | && uses_template_parms (TREE_TYPE (tparm)) | |
8845 | && !saw_undeduced++) | |
8846 | goto again; | |
8847 | ||
8848 | if (!allow_incomplete) | |
8849 | error ("incomplete type unification"); | |
8850 | return 2; | |
8851 | } | |
8852 | return 0; | |
8853 | } | |
8854 | ||
8855 | /* Subroutine of type_unification_real. Args are like the variables at the | |
8856 | call site. ARG is an overloaded function (or template-id); we try | |
8857 | deducing template args from each of the overloads, and if only one | |
8858 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
8859 | ||
8860 | static int | |
8861 | resolve_overloaded_unification (tparms, targs, parm, arg, strict, | |
8862 | sub_strict) | |
8863 | tree tparms, targs, parm, arg; | |
8864 | unification_kind_t strict; | |
8865 | int sub_strict; | |
8866 | { | |
8867 | tree tempargs = copy_node (targs); | |
8868 | int good = 0; | |
8869 | ||
8870 | if (TREE_CODE (arg) == ADDR_EXPR) | |
8871 | arg = TREE_OPERAND (arg, 0); | |
8872 | ||
8873 | if (TREE_CODE (arg) == COMPONENT_REF) | |
8874 | /* Handle `&x' where `x' is some static or non-static member | |
8875 | function name. */ | |
8876 | arg = TREE_OPERAND (arg, 1); | |
8877 | ||
8878 | if (TREE_CODE (arg) == OFFSET_REF) | |
8879 | arg = TREE_OPERAND (arg, 1); | |
8880 | ||
8881 | /* Strip baselink information. */ | |
8882 | if (BASELINK_P (arg)) | |
8883 | arg = BASELINK_FUNCTIONS (arg); | |
8884 | ||
8885 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) | |
8886 | { | |
8887 | /* If we got some explicit template args, we need to plug them into | |
8888 | the affected templates before we try to unify, in case the | |
8889 | explicit args will completely resolve the templates in question. */ | |
8890 | ||
8891 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
8892 | arg = TREE_OPERAND (arg, 0); | |
8893 | ||
8894 | for (; arg; arg = OVL_NEXT (arg)) | |
8895 | { | |
8896 | tree fn = OVL_CURRENT (arg); | |
8897 | tree subargs, elem; | |
8898 | ||
8899 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
8900 | continue; | |
8901 | ||
8902 | subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn), | |
8903 | expl_subargs); | |
8904 | if (subargs) | |
8905 | { | |
8906 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); | |
8907 | if (TREE_CODE (elem) == METHOD_TYPE) | |
8908 | elem = build_ptrmemfunc_type (build_pointer_type (elem)); | |
8909 | good += try_one_overload (tparms, targs, tempargs, parm, elem, | |
8910 | strict, sub_strict); | |
8911 | } | |
8912 | } | |
8913 | } | |
8914 | else if (TREE_CODE (arg) == OVERLOAD | |
8915 | || TREE_CODE (arg) == FUNCTION_DECL) | |
8916 | { | |
8917 | for (; arg; arg = OVL_NEXT (arg)) | |
8918 | { | |
8919 | tree type = TREE_TYPE (OVL_CURRENT (arg)); | |
8920 | if (TREE_CODE (type) == METHOD_TYPE) | |
8921 | type = build_ptrmemfunc_type (build_pointer_type (type)); | |
8922 | good += try_one_overload (tparms, targs, tempargs, parm, | |
8923 | type, | |
8924 | strict, sub_strict); | |
8925 | } | |
8926 | } | |
8927 | else | |
8928 | abort (); | |
8929 | ||
8930 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
8931 | to function or pointer to member function argument if the set of | |
8932 | overloaded functions does not contain function templates and at most | |
8933 | one of a set of overloaded functions provides a unique match. | |
8934 | ||
8935 | So if we found multiple possibilities, we return success but don't | |
8936 | deduce anything. */ | |
8937 | ||
8938 | if (good == 1) | |
8939 | { | |
8940 | int i = TREE_VEC_LENGTH (targs); | |
8941 | for (; i--; ) | |
8942 | if (TREE_VEC_ELT (tempargs, i)) | |
8943 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
8944 | } | |
8945 | if (good) | |
8946 | return 0; | |
8947 | ||
8948 | return 1; | |
8949 | } | |
8950 | ||
8951 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
8952 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
8953 | different overloads deduce different arguments for a given parm. | |
8954 | Returns 1 on success. */ | |
8955 | ||
8956 | static int | |
8957 | try_one_overload (tparms, orig_targs, targs, parm, arg, strict, | |
8958 | sub_strict) | |
8959 | tree tparms, orig_targs, targs, parm, arg; | |
8960 | unification_kind_t strict; | |
8961 | int sub_strict; | |
8962 | { | |
8963 | int nargs; | |
8964 | tree tempargs; | |
8965 | int i; | |
8966 | ||
8967 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
8968 | to function or pointer to member function argument if the set of | |
8969 | overloaded functions does not contain function templates and at most | |
8970 | one of a set of overloaded functions provides a unique match. | |
8971 | ||
8972 | So if this is a template, just return success. */ | |
8973 | ||
8974 | if (uses_template_parms (arg)) | |
8975 | return 1; | |
8976 | ||
8977 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
8978 | ||
8979 | /* We don't copy orig_targs for this because if we have already deduced | |
8980 | some template args from previous args, unify would complain when we | |
8981 | try to deduce a template parameter for the same argument, even though | |
8982 | there isn't really a conflict. */ | |
8983 | nargs = TREE_VEC_LENGTH (targs); | |
8984 | tempargs = make_tree_vec (nargs); | |
8985 | ||
8986 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) | |
8987 | return 0; | |
8988 | ||
8989 | /* First make sure we didn't deduce anything that conflicts with | |
8990 | explicitly specified args. */ | |
8991 | for (i = nargs; i--; ) | |
8992 | { | |
8993 | tree elt = TREE_VEC_ELT (tempargs, i); | |
8994 | tree oldelt = TREE_VEC_ELT (orig_targs, i); | |
8995 | ||
8996 | if (elt == NULL_TREE) | |
8997 | continue; | |
8998 | else if (uses_template_parms (elt)) | |
8999 | { | |
9000 | /* Since we're unifying against ourselves, we will fill in template | |
9001 | args used in the function parm list with our own template parms. | |
9002 | Discard them. */ | |
9003 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
9004 | continue; | |
9005 | } | |
9006 | else if (oldelt && ! template_args_equal (oldelt, elt)) | |
9007 | return 0; | |
9008 | } | |
9009 | ||
9010 | for (i = nargs; i--; ) | |
9011 | { | |
9012 | tree elt = TREE_VEC_ELT (tempargs, i); | |
9013 | ||
9014 | if (elt) | |
9015 | TREE_VEC_ELT (targs, i) = elt; | |
9016 | } | |
9017 | ||
9018 | return 1; | |
9019 | } | |
9020 | ||
9021 | /* Verify that nondeduce template argument agrees with the type | |
9022 | obtained from argument deduction. Return nonzero if the | |
9023 | verification fails. | |
9024 | ||
9025 | For example: | |
9026 | ||
9027 | struct A { typedef int X; }; | |
9028 | template <class T, class U> struct C {}; | |
9029 | template <class T> struct C<T, typename T::X> {}; | |
9030 | ||
9031 | Then with the instantiation `C<A, int>', we can deduce that | |
9032 | `T' is `A' but unify () does not check whether `typename T::X' | |
9033 | is `int'. This function ensure that they agree. | |
9034 | ||
9035 | TARGS, PARMS are the same as the arguments of unify. | |
9036 | ARGS contains template arguments from all levels. */ | |
9037 | ||
9038 | static int | |
9039 | verify_class_unification (targs, parms, args) | |
9040 | tree targs, parms, args; | |
9041 | { | |
9042 | parms = tsubst (parms, add_outermost_template_args (args, targs), | |
9043 | tf_none, NULL_TREE); | |
9044 | if (parms == error_mark_node) | |
9045 | return 1; | |
9046 | ||
9047 | return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args)); | |
9048 | } | |
9049 | ||
9050 | /* PARM is a template class (perhaps with unbound template | |
9051 | parameters). ARG is a fully instantiated type. If ARG can be | |
9052 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
9053 | TARGS are as for unify. */ | |
9054 | ||
9055 | static tree | |
9056 | try_class_unification (tparms, targs, parm, arg) | |
9057 | tree tparms; | |
9058 | tree targs; | |
9059 | tree parm; | |
9060 | tree arg; | |
9061 | { | |
9062 | tree copy_of_targs; | |
9063 | ||
9064 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
9065 | || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) | |
9066 | return NULL_TREE; | |
9067 | ||
9068 | /* We need to make a new template argument vector for the call to | |
9069 | unify. If we used TARGS, we'd clutter it up with the result of | |
9070 | the attempted unification, even if this class didn't work out. | |
9071 | We also don't want to commit ourselves to all the unifications | |
9072 | we've already done, since unification is supposed to be done on | |
9073 | an argument-by-argument basis. In other words, consider the | |
9074 | following pathological case: | |
9075 | ||
9076 | template <int I, int J, int K> | |
9077 | struct S {}; | |
9078 | ||
9079 | template <int I, int J> | |
9080 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
9081 | ||
9082 | template <int I, int J, int K> | |
9083 | void f(S<I, J, K>, S<I, I, I>); | |
9084 | ||
9085 | void g() { | |
9086 | S<0, 0, 0> s0; | |
9087 | S<0, 1, 2> s2; | |
9088 | ||
9089 | f(s0, s2); | |
9090 | } | |
9091 | ||
9092 | Now, by the time we consider the unification involving `s2', we | |
9093 | already know that we must have `f<0, 0, 0>'. But, even though | |
9094 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid | |
9095 | because there are two ways to unify base classes of S<0, 1, 2> | |
9096 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
9097 | would reject the possibility I=1. */ | |
9098 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); | |
9099 | ||
9100 | /* If unification failed, we're done. */ | |
9101 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), | |
9102 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
9103 | return NULL_TREE; | |
9104 | ||
9105 | return arg; | |
9106 | } | |
9107 | ||
9108 | /* Subroutine of get_template_base. RVAL, if non-NULL, is a base we | |
9109 | have already discovered to be satisfactory. ARG_BINFO is the binfo | |
9110 | for the base class of ARG that we are currently examining. */ | |
9111 | ||
9112 | static tree | |
9113 | get_template_base_recursive (tparms, targs, parm, | |
9114 | arg_binfo, rval, flags) | |
9115 | tree tparms; | |
9116 | tree targs; | |
9117 | tree arg_binfo; | |
9118 | tree rval; | |
9119 | tree parm; | |
9120 | int flags; | |
9121 | { | |
9122 | tree binfos; | |
9123 | int i, n_baselinks; | |
9124 | tree arg = BINFO_TYPE (arg_binfo); | |
9125 | ||
9126 | if (!(flags & GTB_IGNORE_TYPE)) | |
9127 | { | |
9128 | tree r = try_class_unification (tparms, targs, | |
9129 | parm, arg); | |
9130 | ||
9131 | /* If there is more than one satisfactory baseclass, then: | |
9132 | ||
9133 | [temp.deduct.call] | |
9134 | ||
9135 | If they yield more than one possible deduced A, the type | |
9136 | deduction fails. | |
9137 | ||
9138 | applies. */ | |
9139 | if (r && rval && !same_type_p (r, rval)) | |
9140 | return error_mark_node; | |
9141 | else if (r) | |
9142 | rval = r; | |
9143 | } | |
9144 | ||
9145 | binfos = BINFO_BASETYPES (arg_binfo); | |
9146 | n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
9147 | ||
9148 | /* Process base types. */ | |
9149 | for (i = 0; i < n_baselinks; i++) | |
9150 | { | |
9151 | tree base_binfo = TREE_VEC_ELT (binfos, i); | |
9152 | int this_virtual; | |
9153 | ||
9154 | /* Skip this base, if we've already seen it. */ | |
9155 | if (BINFO_MARKED (base_binfo)) | |
9156 | continue; | |
9157 | ||
9158 | this_virtual = | |
9159 | (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo); | |
9160 | ||
9161 | /* When searching for a non-virtual, we cannot mark virtually | |
9162 | found binfos. */ | |
9163 | if (! this_virtual) | |
9164 | SET_BINFO_MARKED (base_binfo); | |
9165 | ||
9166 | rval = get_template_base_recursive (tparms, targs, | |
9167 | parm, | |
9168 | base_binfo, | |
9169 | rval, | |
9170 | GTB_VIA_VIRTUAL * this_virtual); | |
9171 | ||
9172 | /* If we discovered more than one matching base class, we can | |
9173 | stop now. */ | |
9174 | if (rval == error_mark_node) | |
9175 | return error_mark_node; | |
9176 | } | |
9177 | ||
9178 | return rval; | |
9179 | } | |
9180 | ||
9181 | /* Given a template type PARM and a class type ARG, find the unique | |
9182 | base type in ARG that is an instance of PARM. We do not examine | |
9183 | ARG itself; only its base-classes. If there is no appropriate base | |
9184 | class, return NULL_TREE. If there is more than one, return | |
9185 | error_mark_node. PARM may be the type of a partial specialization, | |
9186 | as well as a plain template type. Used by unify. */ | |
9187 | ||
9188 | static tree | |
9189 | get_template_base (tparms, targs, parm, arg) | |
9190 | tree tparms; | |
9191 | tree targs; | |
9192 | tree parm; | |
9193 | tree arg; | |
9194 | { | |
9195 | tree rval; | |
9196 | tree arg_binfo; | |
9197 | ||
9198 | my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92); | |
9199 | ||
9200 | arg_binfo = TYPE_BINFO (complete_type (arg)); | |
9201 | rval = get_template_base_recursive (tparms, targs, | |
9202 | parm, arg_binfo, | |
9203 | NULL_TREE, | |
9204 | GTB_IGNORE_TYPE); | |
9205 | ||
9206 | /* Since get_template_base_recursive marks the bases classes, we | |
9207 | must unmark them here. */ | |
9208 | dfs_walk (arg_binfo, dfs_unmark, markedp, 0); | |
9209 | ||
9210 | return rval; | |
9211 | } | |
9212 | ||
9213 | /* Returns the level of DECL, which declares a template parameter. */ | |
9214 | ||
9215 | static int | |
9216 | template_decl_level (decl) | |
9217 | tree decl; | |
9218 | { | |
9219 | switch (TREE_CODE (decl)) | |
9220 | { | |
9221 | case TYPE_DECL: | |
9222 | case TEMPLATE_DECL: | |
9223 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
9224 | ||
9225 | case PARM_DECL: | |
9226 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
9227 | ||
9228 | default: | |
9229 | abort (); | |
9230 | return 0; | |
9231 | } | |
9232 | } | |
9233 | ||
9234 | /* Decide whether ARG can be unified with PARM, considering only the | |
9235 | cv-qualifiers of each type, given STRICT as documented for unify. | |
9236 | Returns nonzero iff the unification is OK on that basis.*/ | |
9237 | ||
9238 | static int | |
9239 | check_cv_quals_for_unify (strict, arg, parm) | |
9240 | int strict; | |
9241 | tree arg; | |
9242 | tree parm; | |
9243 | { | |
9244 | int arg_quals = cp_type_quals (arg); | |
9245 | int parm_quals = cp_type_quals (parm); | |
9246 | ||
9247 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM) | |
9248 | { | |
9249 | /* If the cvr quals of parm will not unify with ARG, they'll be | |
9250 | ignored in instantiation, so we have to do the same here. */ | |
9251 | if (TREE_CODE (arg) == REFERENCE_TYPE) | |
9252 | parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE); | |
9253 | if (!POINTER_TYPE_P (arg) && | |
9254 | TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
9255 | parm_quals &= ~TYPE_QUAL_RESTRICT; | |
9256 | } | |
9257 | ||
9258 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
9259 | && (arg_quals & parm_quals) != parm_quals) | |
9260 | return 0; | |
9261 | ||
9262 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) | |
9263 | && (parm_quals & arg_quals) != arg_quals) | |
9264 | return 0; | |
9265 | ||
9266 | return 1; | |
9267 | } | |
9268 | ||
9269 | /* Takes parameters as for type_unification. Returns 0 if the | |
9270 | type deduction succeeds, 1 otherwise. The parameter STRICT is a | |
9271 | bitwise or of the following flags: | |
9272 | ||
9273 | UNIFY_ALLOW_NONE: | |
9274 | Require an exact match between PARM and ARG. | |
9275 | UNIFY_ALLOW_MORE_CV_QUAL: | |
9276 | Allow the deduced ARG to be more cv-qualified (by qualification | |
9277 | conversion) than ARG. | |
9278 | UNIFY_ALLOW_LESS_CV_QUAL: | |
9279 | Allow the deduced ARG to be less cv-qualified than ARG. | |
9280 | UNIFY_ALLOW_DERIVED: | |
9281 | Allow the deduced ARG to be a template base class of ARG, | |
9282 | or a pointer to a template base class of the type pointed to by | |
9283 | ARG. | |
9284 | UNIFY_ALLOW_INTEGER: | |
9285 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
9286 | case for more information. | |
9287 | UNIFY_ALLOW_OUTER_LEVEL: | |
9288 | This is the outermost level of a deduction. Used to determine validity | |
9289 | of qualification conversions. A valid qualification conversion must | |
9290 | have const qualified pointers leading up to the inner type which | |
9291 | requires additional CV quals, except at the outer level, where const | |
9292 | is not required [conv.qual]. It would be normal to set this flag in | |
9293 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. | |
9294 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
9295 | This is the outermost level of a deduction, and PARM can be more CV | |
9296 | qualified at this point. | |
9297 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
9298 | This is the outermost level of a deduction, and PARM can be less CV | |
9299 | qualified at this point. | |
9300 | UNIFY_ALLOW_MAX_CORRECTION: | |
9301 | This is an INTEGER_TYPE's maximum value. Used if the range may | |
9302 | have been derived from a size specification, such as an array size. | |
9303 | If the size was given by a nontype template parameter N, the maximum | |
9304 | value will have the form N-1. The flag says that we can (and indeed | |
9305 | must) unify N with (ARG + 1), an exception to the normal rules on | |
9306 | folding PARM. */ | |
9307 | ||
9308 | static int | |
9309 | unify (tparms, targs, parm, arg, strict) | |
9310 | tree tparms, targs, parm, arg; | |
9311 | int strict; | |
9312 | { | |
9313 | int idx; | |
9314 | tree targ; | |
9315 | tree tparm; | |
9316 | int strict_in = strict; | |
9317 | ||
9318 | /* I don't think this will do the right thing with respect to types. | |
9319 | But the only case I've seen it in so far has been array bounds, where | |
9320 | signedness is the only information lost, and I think that will be | |
9321 | okay. */ | |
9322 | while (TREE_CODE (parm) == NOP_EXPR) | |
9323 | parm = TREE_OPERAND (parm, 0); | |
9324 | ||
9325 | if (arg == error_mark_node) | |
9326 | return 1; | |
9327 | if (arg == unknown_type_node) | |
9328 | /* We can't deduce anything from this, but we might get all the | |
9329 | template args from other function args. */ | |
9330 | return 0; | |
9331 | ||
9332 | /* If PARM uses template parameters, then we can't bail out here, | |
9333 | even if ARG == PARM, since we won't record unifications for the | |
9334 | template parameters. We might need them if we're trying to | |
9335 | figure out which of two things is more specialized. */ | |
9336 | if (arg == parm && !uses_template_parms (parm)) | |
9337 | return 0; | |
9338 | ||
9339 | /* Immediately reject some pairs that won't unify because of | |
9340 | cv-qualification mismatches. */ | |
9341 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
9342 | && TYPE_P (arg) | |
9343 | /* It is the elements of the array which hold the cv quals of an array | |
9344 | type, and the elements might be template type parms. We'll check | |
9345 | when we recurse. */ | |
9346 | && TREE_CODE (arg) != ARRAY_TYPE | |
9347 | /* We check the cv-qualifiers when unifying with template type | |
9348 | parameters below. We want to allow ARG `const T' to unify with | |
9349 | PARM `T' for example, when computing which of two templates | |
9350 | is more specialized, for example. */ | |
9351 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
9352 | && !check_cv_quals_for_unify (strict_in, arg, parm)) | |
9353 | return 1; | |
9354 | ||
9355 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) | |
9356 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) | |
9357 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; | |
9358 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
9359 | strict &= ~UNIFY_ALLOW_DERIVED; | |
9360 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
9361 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
9362 | strict &= ~UNIFY_ALLOW_MAX_CORRECTION; | |
9363 | ||
9364 | switch (TREE_CODE (parm)) | |
9365 | { | |
9366 | case TYPENAME_TYPE: | |
9367 | case SCOPE_REF: | |
9368 | case UNBOUND_CLASS_TEMPLATE: | |
9369 | /* In a type which contains a nested-name-specifier, template | |
9370 | argument values cannot be deduced for template parameters used | |
9371 | within the nested-name-specifier. */ | |
9372 | return 0; | |
9373 | ||
9374 | case TEMPLATE_TYPE_PARM: | |
9375 | case TEMPLATE_TEMPLATE_PARM: | |
9376 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
9377 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); | |
9378 | ||
9379 | if (TEMPLATE_TYPE_LEVEL (parm) | |
9380 | != template_decl_level (tparm)) | |
9381 | /* The PARM is not one we're trying to unify. Just check | |
9382 | to see if it matches ARG. */ | |
9383 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
9384 | && same_type_p (parm, arg)) ? 0 : 1; | |
9385 | idx = TEMPLATE_TYPE_IDX (parm); | |
9386 | targ = TREE_VEC_ELT (targs, idx); | |
9387 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); | |
9388 | ||
9389 | /* Check for mixed types and values. */ | |
9390 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM | |
9391 | && TREE_CODE (tparm) != TYPE_DECL) | |
9392 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
9393 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
9394 | return 1; | |
9395 | ||
9396 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
9397 | { | |
9398 | /* ARG must be constructed from a template class or a template | |
9399 | template parameter. */ | |
9400 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
9401 | && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))) | |
9402 | return 1; | |
9403 | ||
9404 | { | |
9405 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); | |
9406 | tree parmvec = TYPE_TI_ARGS (parm); | |
9407 | tree argvec = TYPE_TI_ARGS (arg); | |
9408 | tree argtmplvec | |
9409 | = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); | |
9410 | int i; | |
9411 | ||
9412 | /* The parameter and argument roles have to be switched here | |
9413 | in order to handle default arguments properly. For example, | |
9414 | template<template <class> class TT> void f(TT<int>) | |
9415 | should be able to accept vector<int> which comes from | |
9416 | template <class T, class Allocator = allocator> | |
9417 | class vector. */ | |
9418 | ||
9419 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) | |
9420 | == error_mark_node) | |
9421 | return 1; | |
9422 | ||
9423 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
9424 | We check each element of PARMVEC and ARGVEC individually | |
9425 | rather than the whole TREE_VEC since they can have | |
9426 | different number of elements. */ | |
9427 | ||
9428 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) | |
9429 | { | |
9430 | tree t = TREE_VEC_ELT (parmvec, i); | |
9431 | ||
9432 | if (unify (tparms, targs, t, | |
9433 | TREE_VEC_ELT (argvec, i), | |
9434 | UNIFY_ALLOW_NONE)) | |
9435 | return 1; | |
9436 | } | |
9437 | } | |
9438 | arg = TYPE_TI_TEMPLATE (arg); | |
9439 | ||
9440 | /* Fall through to deduce template name. */ | |
9441 | } | |
9442 | ||
9443 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
9444 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
9445 | { | |
9446 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
9447 | ||
9448 | /* Simple cases: Value already set, does match or doesn't. */ | |
9449 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
9450 | return 0; | |
9451 | else if (targ) | |
9452 | return 1; | |
9453 | } | |
9454 | else | |
9455 | { | |
9456 | /* If ARG is an offset type, we're trying to unify '*T' with | |
9457 | 'U C::*', which is ill-formed. See the comment in the | |
9458 | POINTER_TYPE case about this ugliness. */ | |
9459 | if (TREE_CODE (arg) == OFFSET_TYPE) | |
9460 | return 1; | |
9461 | ||
9462 | /* If PARM is `const T' and ARG is only `int', we don't have | |
9463 | a match unless we are allowing additional qualification. | |
9464 | If ARG is `const int' and PARM is just `T' that's OK; | |
9465 | that binds `const int' to `T'. */ | |
9466 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, | |
9467 | arg, parm)) | |
9468 | return 1; | |
9469 | ||
9470 | /* Consider the case where ARG is `const volatile int' and | |
9471 | PARM is `const T'. Then, T should be `volatile int'. */ | |
9472 | arg = cp_build_qualified_type_real | |
9473 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
9474 | if (arg == error_mark_node) | |
9475 | return 1; | |
9476 | ||
9477 | /* Simple cases: Value already set, does match or doesn't. */ | |
9478 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
9479 | return 0; | |
9480 | else if (targ) | |
9481 | return 1; | |
9482 | ||
9483 | /* Make sure that ARG is not a variable-sized array. (Note | |
9484 | that were talking about variable-sized arrays (like | |
9485 | `int[n]'), rather than arrays of unknown size (like | |
9486 | `int[]').) We'll get very confused by such a type since | |
9487 | the bound of the array will not be computable in an | |
9488 | instantiation. Besides, such types are not allowed in | |
9489 | ISO C++, so we can do as we please here. */ | |
9490 | if (variably_modified_type_p (arg)) | |
9491 | return 1; | |
9492 | } | |
9493 | ||
9494 | TREE_VEC_ELT (targs, idx) = arg; | |
9495 | return 0; | |
9496 | ||
9497 | case TEMPLATE_PARM_INDEX: | |
9498 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); | |
9499 | ||
9500 | if (TEMPLATE_PARM_LEVEL (parm) | |
9501 | != template_decl_level (tparm)) | |
9502 | /* The PARM is not one we're trying to unify. Just check | |
9503 | to see if it matches ARG. */ | |
9504 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
9505 | && cp_tree_equal (parm, arg) > 0) ? 0 : 1; | |
9506 | ||
9507 | idx = TEMPLATE_PARM_IDX (parm); | |
9508 | targ = TREE_VEC_ELT (targs, idx); | |
9509 | ||
9510 | if (targ) | |
9511 | { | |
9512 | int i = (cp_tree_equal (targ, arg) > 0); | |
9513 | if (i == 1) | |
9514 | return 0; | |
9515 | else if (i == 0) | |
9516 | return 1; | |
9517 | else | |
9518 | abort (); | |
9519 | } | |
9520 | ||
9521 | /* [temp.deduct.type] If, in the declaration of a function template | |
9522 | with a non-type template-parameter, the non-type | |
9523 | template-parameter is used in an expression in the function | |
9524 | parameter-list and, if the corresponding template-argument is | |
9525 | deduced, the template-argument type shall match the type of the | |
9526 | template-parameter exactly, except that a template-argument | |
9527 | deduced from an array bound may be of any integral type. | |
9528 | The non-type parameter might use already deduced type parameters. */ | |
9529 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); | |
9530 | if (!TREE_TYPE (arg)) | |
9531 | /* Template-parameter dependent expression. Just accept it for now. | |
9532 | It will later be processed in convert_template_argument. */ | |
9533 | ; | |
9534 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
9535 | /* OK */; | |
9536 | else if ((strict & UNIFY_ALLOW_INTEGER) | |
9537 | && (TREE_CODE (tparm) == INTEGER_TYPE | |
9538 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
9539 | /* OK */; | |
9540 | else if (uses_template_parms (tparm)) | |
9541 | /* We haven't deduced the type of this parameter yet. Try again | |
9542 | later. */ | |
9543 | return 0; | |
9544 | else | |
9545 | return 1; | |
9546 | ||
9547 | TREE_VEC_ELT (targs, idx) = arg; | |
9548 | return 0; | |
9549 | ||
9550 | case POINTER_TYPE: | |
9551 | { | |
9552 | if (TREE_CODE (arg) != POINTER_TYPE) | |
9553 | return 1; | |
9554 | ||
9555 | /* [temp.deduct.call] | |
9556 | ||
9557 | A can be another pointer or pointer to member type that can | |
9558 | be converted to the deduced A via a qualification | |
9559 | conversion (_conv.qual_). | |
9560 | ||
9561 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
9562 | This will allow for additional cv-qualification of the | |
9563 | pointed-to types if appropriate. */ | |
9564 | ||
9565 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) | |
9566 | /* The derived-to-base conversion only persists through one | |
9567 | level of pointers. */ | |
9568 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); | |
9569 | ||
9570 | if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE | |
9571 | && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) | |
9572 | { | |
9573 | /* Avoid getting confused about cv-quals; don't recurse here. | |
9574 | Pointers to members should really be just OFFSET_TYPE, not | |
9575 | this two-level nonsense... */ | |
9576 | ||
9577 | parm = TREE_TYPE (parm); | |
9578 | arg = TREE_TYPE (arg); | |
9579 | goto offset; | |
9580 | } | |
9581 | ||
9582 | return unify (tparms, targs, TREE_TYPE (parm), | |
9583 | TREE_TYPE (arg), strict); | |
9584 | } | |
9585 | ||
9586 | case REFERENCE_TYPE: | |
9587 | if (TREE_CODE (arg) != REFERENCE_TYPE) | |
9588 | return 1; | |
9589 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
9590 | strict & UNIFY_ALLOW_MORE_CV_QUAL); | |
9591 | ||
9592 | case ARRAY_TYPE: | |
9593 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
9594 | return 1; | |
9595 | if ((TYPE_DOMAIN (parm) == NULL_TREE) | |
9596 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
9597 | return 1; | |
9598 | if (TYPE_DOMAIN (parm) != NULL_TREE | |
9599 | && unify (tparms, targs, TYPE_DOMAIN (parm), | |
9600 | TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0) | |
9601 | return 1; | |
9602 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
9603 | strict & UNIFY_ALLOW_MORE_CV_QUAL); | |
9604 | ||
9605 | case REAL_TYPE: | |
9606 | case COMPLEX_TYPE: | |
9607 | case VECTOR_TYPE: | |
9608 | case INTEGER_TYPE: | |
9609 | case BOOLEAN_TYPE: | |
9610 | case VOID_TYPE: | |
9611 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
9612 | return 1; | |
9613 | ||
9614 | if (TREE_CODE (parm) == INTEGER_TYPE | |
9615 | && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST) | |
9616 | { | |
9617 | if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg) | |
9618 | && unify (tparms, targs, TYPE_MIN_VALUE (parm), | |
9619 | TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER)) | |
9620 | return 1; | |
9621 | if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg) | |
9622 | && unify (tparms, targs, TYPE_MAX_VALUE (parm), | |
9623 | TYPE_MAX_VALUE (arg), | |
9624 | UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION)) | |
9625 | return 1; | |
9626 | } | |
9627 | /* We have already checked cv-qualification at the top of the | |
9628 | function. */ | |
9629 | else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) | |
9630 | return 1; | |
9631 | ||
9632 | /* As far as unification is concerned, this wins. Later checks | |
9633 | will invalidate it if necessary. */ | |
9634 | return 0; | |
9635 | ||
9636 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
9637 | /* Type INTEGER_CST can come from ordinary constant template args. */ | |
9638 | case INTEGER_CST: | |
9639 | while (TREE_CODE (arg) == NOP_EXPR) | |
9640 | arg = TREE_OPERAND (arg, 0); | |
9641 | ||
9642 | if (TREE_CODE (arg) != INTEGER_CST) | |
9643 | return 1; | |
9644 | return !tree_int_cst_equal (parm, arg); | |
9645 | ||
9646 | case TREE_VEC: | |
9647 | { | |
9648 | int i; | |
9649 | if (TREE_CODE (arg) != TREE_VEC) | |
9650 | return 1; | |
9651 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
9652 | return 1; | |
9653 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) | |
9654 | if (unify (tparms, targs, | |
9655 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), | |
9656 | UNIFY_ALLOW_NONE)) | |
9657 | return 1; | |
9658 | return 0; | |
9659 | } | |
9660 | ||
9661 | case RECORD_TYPE: | |
9662 | case UNION_TYPE: | |
9663 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
9664 | return 1; | |
9665 | ||
9666 | if (TYPE_PTRMEMFUNC_P (parm)) | |
9667 | { | |
9668 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
9669 | return 1; | |
9670 | ||
9671 | return unify (tparms, targs, | |
9672 | TYPE_PTRMEMFUNC_FN_TYPE (parm), | |
9673 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
9674 | strict); | |
9675 | } | |
9676 | ||
9677 | if (CLASSTYPE_TEMPLATE_INFO (parm)) | |
9678 | { | |
9679 | tree t = NULL_TREE; | |
9680 | ||
9681 | if (strict_in & UNIFY_ALLOW_DERIVED) | |
9682 | { | |
9683 | /* First, we try to unify the PARM and ARG directly. */ | |
9684 | t = try_class_unification (tparms, targs, | |
9685 | parm, arg); | |
9686 | ||
9687 | if (!t) | |
9688 | { | |
9689 | /* Fallback to the special case allowed in | |
9690 | [temp.deduct.call]: | |
9691 | ||
9692 | If P is a class, and P has the form | |
9693 | template-id, then A can be a derived class of | |
9694 | the deduced A. Likewise, if P is a pointer to | |
9695 | a class of the form template-id, A can be a | |
9696 | pointer to a derived class pointed to by the | |
9697 | deduced A. */ | |
9698 | t = get_template_base (tparms, targs, | |
9699 | parm, arg); | |
9700 | ||
9701 | if (! t || t == error_mark_node) | |
9702 | return 1; | |
9703 | } | |
9704 | } | |
9705 | else if (CLASSTYPE_TEMPLATE_INFO (arg) | |
9706 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
9707 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
9708 | /* Perhaps PARM is something like S<U> and ARG is S<int>. | |
9709 | Then, we should unify `int' and `U'. */ | |
9710 | t = arg; | |
9711 | else | |
9712 | /* There's no chance of unification succeeding. */ | |
9713 | return 1; | |
9714 | ||
9715 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), | |
9716 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); | |
9717 | } | |
9718 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) | |
9719 | return 1; | |
9720 | return 0; | |
9721 | ||
9722 | case METHOD_TYPE: | |
9723 | case FUNCTION_TYPE: | |
9724 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
9725 | return 1; | |
9726 | ||
9727 | if (unify (tparms, targs, TREE_TYPE (parm), | |
9728 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) | |
9729 | return 1; | |
9730 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), | |
9731 | TYPE_ARG_TYPES (arg), 1, | |
9732 | DEDUCE_EXACT, 0, -1); | |
9733 | ||
9734 | case OFFSET_TYPE: | |
9735 | offset: | |
9736 | if (TREE_CODE (arg) != OFFSET_TYPE) | |
9737 | return 1; | |
9738 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
9739 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) | |
9740 | return 1; | |
9741 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
9742 | strict); | |
9743 | ||
9744 | case CONST_DECL: | |
9745 | if (DECL_TEMPLATE_PARM_P (parm)) | |
9746 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
9747 | if (arg != decl_constant_value (parm)) | |
9748 | return 1; | |
9749 | return 0; | |
9750 | ||
9751 | case TEMPLATE_DECL: | |
9752 | /* Matched cases are handled by the ARG == PARM test above. */ | |
9753 | return 1; | |
9754 | ||
9755 | case MINUS_EXPR: | |
9756 | if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node) | |
9757 | && (strict_in & UNIFY_ALLOW_MAX_CORRECTION)) | |
9758 | { | |
9759 | /* We handle this case specially, since it comes up with | |
9760 | arrays. In particular, something like: | |
9761 | ||
9762 | template <int N> void f(int (&x)[N]); | |
9763 | ||
9764 | Here, we are trying to unify the range type, which | |
9765 | looks like [0 ... (N - 1)]. */ | |
9766 | tree t, t1, t2; | |
9767 | t1 = TREE_OPERAND (parm, 0); | |
9768 | t2 = TREE_OPERAND (parm, 1); | |
9769 | ||
9770 | t = fold (build (PLUS_EXPR, integer_type_node, arg, t2)); | |
9771 | ||
9772 | return unify (tparms, targs, t1, t, strict); | |
9773 | } | |
9774 | /* else fall through */ | |
9775 | ||
9776 | default: | |
9777 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm)))) | |
9778 | { | |
9779 | ||
9780 | /* We're looking at an expression. This can happen with | |
9781 | something like: | |
9782 | ||
9783 | template <int I> | |
9784 | void foo(S<I>, S<I + 2>); | |
9785 | ||
9786 | This is a "nondeduced context": | |
9787 | ||
9788 | [deduct.type] | |
9789 | ||
9790 | The nondeduced contexts are: | |
9791 | ||
9792 | --A type that is a template-id in which one or more of | |
9793 | the template-arguments is an expression that references | |
9794 | a template-parameter. | |
9795 | ||
9796 | In these cases, we assume deduction succeeded, but don't | |
9797 | actually infer any unifications. */ | |
9798 | ||
9799 | if (!uses_template_parms (parm) | |
9800 | && !template_args_equal (parm, arg)) | |
9801 | return 1; | |
9802 | else | |
9803 | return 0; | |
9804 | } | |
9805 | else | |
9806 | sorry ("use of `%s' in template type unification", | |
9807 | tree_code_name [(int) TREE_CODE (parm)]); | |
9808 | ||
9809 | return 1; | |
9810 | } | |
9811 | } | |
9812 | \f | |
9813 | /* Called if RESULT is explicitly instantiated, or is a member of an | |
9814 | explicitly instantiated class, or if using -frepo and the | |
9815 | instantiation of RESULT has been assigned to this file. */ | |
9816 | ||
9817 | void | |
9818 | mark_decl_instantiated (result, extern_p) | |
9819 | tree result; | |
9820 | int extern_p; | |
9821 | { | |
9822 | if (TREE_CODE (result) != FUNCTION_DECL) | |
9823 | /* The TREE_PUBLIC flag for function declarations will have been | |
9824 | set correctly by tsubst. */ | |
9825 | TREE_PUBLIC (result) = 1; | |
9826 | ||
9827 | /* We used to set this unconditionally; we moved that to | |
9828 | do_decl_instantiation so it wouldn't get set on members of | |
9829 | explicit class template instantiations. But we still need to set | |
9830 | it here for the 'extern template' case in order to suppress | |
9831 | implicit instantiations. */ | |
9832 | if (extern_p) | |
9833 | SET_DECL_EXPLICIT_INSTANTIATION (result); | |
9834 | ||
9835 | if (! extern_p) | |
9836 | { | |
9837 | DECL_INTERFACE_KNOWN (result) = 1; | |
9838 | DECL_NOT_REALLY_EXTERN (result) = 1; | |
9839 | ||
9840 | /* Always make artificials weak. */ | |
9841 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
9842 | comdat_linkage (result); | |
9843 | /* For WIN32 we also want to put explicit instantiations in | |
9844 | linkonce sections. */ | |
9845 | else if (TREE_PUBLIC (result)) | |
9846 | maybe_make_one_only (result); | |
9847 | } | |
9848 | else if (TREE_CODE (result) == FUNCTION_DECL) | |
9849 | defer_fn (result); | |
9850 | } | |
9851 | ||
9852 | /* Given two function templates PAT1 and PAT2, return: | |
9853 | ||
9854 | DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER. | |
9855 | ||
9856 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
9857 | -1 if PAT2 is more specialized than PAT1. | |
9858 | 0 if neither is more specialized. | |
9859 | ||
9860 | LEN is passed through to fn_type_unification. */ | |
9861 | ||
9862 | int | |
9863 | more_specialized (pat1, pat2, deduce, len) | |
9864 | tree pat1, pat2; | |
9865 | int deduce; | |
9866 | int len; | |
9867 | { | |
9868 | tree targs; | |
9869 | int winner = 0; | |
9870 | ||
9871 | targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2), | |
9872 | NULL_TREE, 0, deduce, len); | |
9873 | if (targs) | |
9874 | --winner; | |
9875 | ||
9876 | targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1), | |
9877 | NULL_TREE, 0, deduce, len); | |
9878 | if (targs) | |
9879 | ++winner; | |
9880 | ||
9881 | return winner; | |
9882 | } | |
9883 | ||
9884 | /* Given two class template specialization list nodes PAT1 and PAT2, return: | |
9885 | ||
9886 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. | |
9887 | -1 if PAT2 is more specialized than PAT1. | |
9888 | 0 if neither is more specialized. */ | |
9889 | ||
9890 | int | |
9891 | more_specialized_class (pat1, pat2) | |
9892 | tree pat1, pat2; | |
9893 | { | |
9894 | tree targs; | |
9895 | int winner = 0; | |
9896 | ||
9897 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), | |
9898 | TREE_PURPOSE (pat2)); | |
9899 | if (targs) | |
9900 | --winner; | |
9901 | ||
9902 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), | |
9903 | TREE_PURPOSE (pat1)); | |
9904 | if (targs) | |
9905 | ++winner; | |
9906 | ||
9907 | return winner; | |
9908 | } | |
9909 | ||
9910 | /* Return the template arguments that will produce the function signature | |
9911 | DECL from the function template FN, with the explicit template | |
9912 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must | |
9913 | also match. Return NULL_TREE if no satisfactory arguments could be | |
9914 | found. DEDUCE and LEN are passed through to fn_type_unification. */ | |
9915 | ||
9916 | static tree | |
9917 | get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len) | |
9918 | tree fn, decl, explicit_args; | |
9919 | int check_rettype, deduce, len; | |
9920 | { | |
9921 | int ntparms = DECL_NTPARMS (fn); | |
9922 | tree targs = make_tree_vec (ntparms); | |
9923 | tree decl_type; | |
9924 | tree decl_arg_types; | |
9925 | int i; | |
9926 | ||
9927 | /* Substitute the explicit template arguments into the type of DECL. | |
9928 | The call to fn_type_unification will handle substitution into the | |
9929 | FN. */ | |
9930 | decl_type = TREE_TYPE (decl); | |
9931 | if (explicit_args && uses_template_parms (decl_type)) | |
9932 | { | |
9933 | tree tmpl; | |
9934 | tree converted_args; | |
9935 | ||
9936 | if (DECL_TEMPLATE_INFO (decl)) | |
9937 | tmpl = DECL_TI_TEMPLATE (decl); | |
9938 | else | |
9939 | /* We can get here for some invalid specializations. */ | |
9940 | return NULL_TREE; | |
9941 | ||
9942 | converted_args | |
9943 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
9944 | explicit_args, NULL_TREE, | |
9945 | tf_none, /*require_all_arguments=*/0)); | |
9946 | if (converted_args == error_mark_node) | |
9947 | return NULL_TREE; | |
9948 | ||
9949 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
9950 | if (decl_type == error_mark_node) | |
9951 | return NULL_TREE; | |
9952 | } | |
9953 | ||
9954 | decl_arg_types = TYPE_ARG_TYPES (decl_type); | |
9955 | /* Never do unification on the 'this' parameter. */ | |
9956 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
9957 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
9958 | ||
9959 | i = fn_type_unification (fn, explicit_args, targs, | |
9960 | decl_arg_types, | |
9961 | (check_rettype || DECL_CONV_FN_P (fn) | |
9962 | ? TREE_TYPE (decl_type) : NULL_TREE), | |
9963 | deduce, len); | |
9964 | ||
9965 | if (i != 0) | |
9966 | return NULL_TREE; | |
9967 | ||
9968 | return targs; | |
9969 | } | |
9970 | ||
9971 | /* For most uses, we want to check the return type. */ | |
9972 | ||
9973 | tree | |
9974 | get_bindings (fn, decl, explicit_args) | |
9975 | tree fn, decl, explicit_args; | |
9976 | { | |
9977 | return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1); | |
9978 | } | |
9979 | ||
9980 | /* But for resolve_overloaded_unification, we only care about the parameter | |
9981 | types. */ | |
9982 | ||
9983 | static tree | |
9984 | get_bindings_overload (fn, decl, explicit_args) | |
9985 | tree fn, decl, explicit_args; | |
9986 | { | |
9987 | return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1); | |
9988 | } | |
9989 | ||
9990 | /* Return the innermost template arguments that, when applied to a | |
9991 | template specialization whose innermost template parameters are | |
9992 | TPARMS, and whose specialization arguments are PARMS, yield the | |
9993 | ARGS. | |
9994 | ||
9995 | For example, suppose we have: | |
9996 | ||
9997 | template <class T, class U> struct S {}; | |
9998 | template <class T> struct S<T*, int> {}; | |
9999 | ||
10000 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
10001 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
10002 | int}. The resulting vector will be {double}, indicating that `T' | |
10003 | is bound to `double'. */ | |
10004 | ||
10005 | static tree | |
10006 | get_class_bindings (tparms, parms, args) | |
10007 | tree tparms, parms, args; | |
10008 | { | |
10009 | int i, ntparms = TREE_VEC_LENGTH (tparms); | |
10010 | tree vec = make_tree_vec (ntparms); | |
10011 | ||
10012 | if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args), | |
10013 | UNIFY_ALLOW_NONE)) | |
10014 | return NULL_TREE; | |
10015 | ||
10016 | for (i = 0; i < ntparms; ++i) | |
10017 | if (! TREE_VEC_ELT (vec, i)) | |
10018 | return NULL_TREE; | |
10019 | ||
10020 | if (verify_class_unification (vec, parms, args)) | |
10021 | return NULL_TREE; | |
10022 | ||
10023 | return vec; | |
10024 | } | |
10025 | ||
10026 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. | |
10027 | Pick the most specialized template, and return the corresponding | |
10028 | instantiation, or if there is no corresponding instantiation, the | |
10029 | template itself. If there is no most specialized template, | |
10030 | error_mark_node is returned. If there are no templates at all, | |
10031 | NULL_TREE is returned. */ | |
10032 | ||
10033 | tree | |
10034 | most_specialized_instantiation (instantiations) | |
10035 | tree instantiations; | |
10036 | { | |
10037 | tree fn, champ; | |
10038 | int fate; | |
10039 | ||
10040 | if (!instantiations) | |
10041 | return NULL_TREE; | |
10042 | ||
10043 | champ = instantiations; | |
10044 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
10045 | { | |
10046 | fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn), | |
10047 | DEDUCE_EXACT, -1); | |
10048 | if (fate == 1) | |
10049 | ; | |
10050 | else | |
10051 | { | |
10052 | if (fate == 0) | |
10053 | { | |
10054 | fn = TREE_CHAIN (fn); | |
10055 | if (! fn) | |
10056 | return error_mark_node; | |
10057 | } | |
10058 | champ = fn; | |
10059 | } | |
10060 | } | |
10061 | ||
10062 | for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn)) | |
10063 | { | |
10064 | fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn), | |
10065 | DEDUCE_EXACT, -1); | |
10066 | if (fate != 1) | |
10067 | return error_mark_node; | |
10068 | } | |
10069 | ||
10070 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); | |
10071 | } | |
10072 | ||
10073 | /* Return the most specialized of the list of templates in FNS that can | |
10074 | produce an instantiation matching DECL, given the explicit template | |
10075 | arguments EXPLICIT_ARGS. */ | |
10076 | ||
10077 | static tree | |
10078 | most_specialized (fns, decl, explicit_args) | |
10079 | tree fns, decl, explicit_args; | |
10080 | { | |
10081 | tree candidates = NULL_TREE; | |
10082 | tree fn, args; | |
10083 | ||
10084 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
10085 | { | |
10086 | tree candidate = TREE_VALUE (fn); | |
10087 | ||
10088 | args = get_bindings (candidate, decl, explicit_args); | |
10089 | if (args) | |
10090 | candidates = tree_cons (NULL_TREE, candidate, candidates); | |
10091 | } | |
10092 | ||
10093 | return most_specialized_instantiation (candidates); | |
10094 | } | |
10095 | ||
10096 | /* If DECL is a specialization of some template, return the most | |
10097 | general such template. Otherwise, returns NULL_TREE. | |
10098 | ||
10099 | For example, given: | |
10100 | ||
10101 | template <class T> struct S { template <class U> void f(U); }; | |
10102 | ||
10103 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
10104 | the full template. This function will not trace past partial | |
10105 | specializations, however. For example, given in addition: | |
10106 | ||
10107 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
10108 | ||
10109 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
10110 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
10111 | ||
10112 | tree | |
10113 | most_general_template (decl) | |
10114 | tree decl; | |
10115 | { | |
10116 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is | |
10117 | an immediate specialization. */ | |
10118 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
10119 | { | |
10120 | if (DECL_TEMPLATE_INFO (decl)) { | |
10121 | decl = DECL_TI_TEMPLATE (decl); | |
10122 | ||
10123 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
10124 | template friend. */ | |
10125 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
10126 | return NULL_TREE; | |
10127 | } else | |
10128 | return NULL_TREE; | |
10129 | } | |
10130 | ||
10131 | /* Look for more and more general templates. */ | |
10132 | while (DECL_TEMPLATE_INFO (decl)) | |
10133 | { | |
10134 | /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE | |
10135 | in some cases. (See cp-tree.h for details.) */ | |
10136 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
10137 | break; | |
10138 | ||
10139 | /* Stop if we run into an explicitly specialized class template. */ | |
10140 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
10141 | && DECL_CONTEXT (decl) | |
10142 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
10143 | break; | |
10144 | ||
10145 | decl = DECL_TI_TEMPLATE (decl); | |
10146 | } | |
10147 | ||
10148 | return decl; | |
10149 | } | |
10150 | ||
10151 | /* Return the most specialized of the class template specializations | |
10152 | of TMPL which can produce an instantiation matching ARGS, or | |
10153 | error_mark_node if the choice is ambiguous. */ | |
10154 | ||
10155 | static tree | |
10156 | most_specialized_class (tmpl, args) | |
10157 | tree tmpl; | |
10158 | tree args; | |
10159 | { | |
10160 | tree list = NULL_TREE; | |
10161 | tree t; | |
10162 | tree champ; | |
10163 | int fate; | |
10164 | ||
10165 | tmpl = most_general_template (tmpl); | |
10166 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
10167 | { | |
10168 | tree spec_args | |
10169 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
10170 | if (spec_args) | |
10171 | { | |
10172 | list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); | |
10173 | TREE_TYPE (list) = TREE_TYPE (t); | |
10174 | } | |
10175 | } | |
10176 | ||
10177 | if (! list) | |
10178 | return NULL_TREE; | |
10179 | ||
10180 | t = list; | |
10181 | champ = t; | |
10182 | t = TREE_CHAIN (t); | |
10183 | for (; t; t = TREE_CHAIN (t)) | |
10184 | { | |
10185 | fate = more_specialized_class (champ, t); | |
10186 | if (fate == 1) | |
10187 | ; | |
10188 | else | |
10189 | { | |
10190 | if (fate == 0) | |
10191 | { | |
10192 | t = TREE_CHAIN (t); | |
10193 | if (! t) | |
10194 | return error_mark_node; | |
10195 | } | |
10196 | champ = t; | |
10197 | } | |
10198 | } | |
10199 | ||
10200 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
10201 | { | |
10202 | fate = more_specialized_class (champ, t); | |
10203 | if (fate != 1) | |
10204 | return error_mark_node; | |
10205 | } | |
10206 | ||
10207 | return champ; | |
10208 | } | |
10209 | ||
10210 | /* Explicitly instantiate DECL. */ | |
10211 | ||
10212 | void | |
10213 | do_decl_instantiation (tree decl, tree storage) | |
10214 | { | |
10215 | tree result = NULL_TREE; | |
10216 | int extern_p = 0; | |
10217 | ||
10218 | if (!decl) | |
10219 | /* An error occurred, for which grokdeclarator has already issued | |
10220 | an appropriate message. */ | |
10221 | return; | |
10222 | else if (! DECL_LANG_SPECIFIC (decl)) | |
10223 | { | |
10224 | error ("explicit instantiation of non-template `%#D'", decl); | |
10225 | return; | |
10226 | } | |
10227 | else if (TREE_CODE (decl) == VAR_DECL) | |
10228 | { | |
10229 | /* There is an asymmetry here in the way VAR_DECLs and | |
10230 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
10231 | the latter, the DECL we get back will be marked as a | |
10232 | template instantiation, and the appropriate | |
10233 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
10234 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
10235 | should handle VAR_DECLs as it currently handles | |
10236 | FUNCTION_DECLs. */ | |
10237 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); | |
10238 | if (!result || TREE_CODE (result) != VAR_DECL) | |
10239 | { | |
10240 | error ("no matching template for `%D' found", decl); | |
10241 | return; | |
10242 | } | |
10243 | } | |
10244 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
10245 | { | |
10246 | error ("explicit instantiation of `%#D'", decl); | |
10247 | return; | |
10248 | } | |
10249 | else | |
10250 | result = decl; | |
10251 | ||
10252 | /* Check for various error cases. Note that if the explicit | |
10253 | instantiation is valid the RESULT will currently be marked as an | |
10254 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set | |
10255 | until we get here. */ | |
10256 | ||
10257 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
10258 | { | |
10259 | /* DR 259 [temp.spec]. | |
10260 | ||
10261 | Both an explicit instantiation and a declaration of an explicit | |
10262 | specialization shall not appear in a program unless the explicit | |
10263 | instantiation follows a declaration of the explicit specialization. | |
10264 | ||
10265 | For a given set of template parameters, if an explicit | |
10266 | instantiation of a template appears after a declaration of an | |
10267 | explicit specialization for that template, the explicit | |
10268 | instantiation has no effect. */ | |
10269 | return; | |
10270 | } | |
10271 | else if (DECL_EXPLICIT_INSTANTIATION (result)) | |
10272 | { | |
10273 | /* [temp.spec] | |
10274 | ||
10275 | No program shall explicitly instantiate any template more | |
10276 | than once. | |
10277 | ||
10278 | We check DECL_INTERFACE_KNOWN so as not to complain when the first | |
10279 | instantiation was `extern' and the second is not, and EXTERN_P for | |
10280 | the opposite case. If -frepo, chances are we already got marked | |
10281 | as an explicit instantiation because of the repo file. */ | |
10282 | if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository) | |
10283 | pedwarn ("duplicate explicit instantiation of `%#D'", result); | |
10284 | ||
10285 | /* If we've already instantiated the template, just return now. */ | |
10286 | if (DECL_INTERFACE_KNOWN (result)) | |
10287 | return; | |
10288 | } | |
10289 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
10290 | { | |
10291 | error ("no matching template for `%D' found", result); | |
10292 | return; | |
10293 | } | |
10294 | else if (!DECL_TEMPLATE_INFO (result)) | |
10295 | { | |
10296 | pedwarn ("explicit instantiation of non-template `%#D'", result); | |
10297 | return; | |
10298 | } | |
10299 | ||
10300 | if (flag_external_templates) | |
10301 | return; | |
10302 | ||
10303 | if (storage == NULL_TREE) | |
10304 | ; | |
10305 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
10306 | { | |
10307 | if (pedantic && !in_system_header) | |
10308 | pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations"); | |
10309 | extern_p = 1; | |
10310 | } | |
10311 | else | |
10312 | error ("storage class `%D' applied to template instantiation", | |
10313 | storage); | |
10314 | ||
10315 | SET_DECL_EXPLICIT_INSTANTIATION (result); | |
10316 | mark_decl_instantiated (result, extern_p); | |
10317 | repo_template_instantiated (result, extern_p); | |
10318 | if (! extern_p) | |
10319 | instantiate_decl (result, /*defer_ok=*/1); | |
10320 | } | |
10321 | ||
10322 | void | |
10323 | mark_class_instantiated (t, extern_p) | |
10324 | tree t; | |
10325 | int extern_p; | |
10326 | { | |
10327 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
10328 | SET_CLASSTYPE_INTERFACE_KNOWN (t); | |
10329 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
10330 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; | |
10331 | if (! extern_p) | |
10332 | { | |
10333 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
10334 | rest_of_type_compilation (t, 1); | |
10335 | } | |
10336 | } | |
10337 | ||
10338 | /* Perform an explicit instantiation of template class T. STORAGE, if | |
10339 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
10340 | nonzero if this is called from the parser, zero if called recursively, | |
10341 | since the standard is unclear (as detailed below). */ | |
10342 | ||
10343 | void | |
10344 | do_type_instantiation (t, storage, complain) | |
10345 | tree t, storage; | |
10346 | tsubst_flags_t complain; | |
10347 | { | |
10348 | int extern_p = 0; | |
10349 | int nomem_p = 0; | |
10350 | int static_p = 0; | |
10351 | ||
10352 | if (TREE_CODE (t) == TYPE_DECL) | |
10353 | t = TREE_TYPE (t); | |
10354 | ||
10355 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) | |
10356 | { | |
10357 | error ("explicit instantiation of non-template type `%T'", t); | |
10358 | return; | |
10359 | } | |
10360 | ||
10361 | complete_type (t); | |
10362 | ||
10363 | /* With -fexternal-templates, explicit instantiations are treated the same | |
10364 | as implicit ones. */ | |
10365 | if (flag_external_templates) | |
10366 | return; | |
10367 | ||
10368 | if (!COMPLETE_TYPE_P (t)) | |
10369 | { | |
10370 | if (complain & tf_error) | |
10371 | error ("explicit instantiation of `%#T' before definition of template", | |
10372 | t); | |
10373 | return; | |
10374 | } | |
10375 | ||
10376 | if (storage != NULL_TREE) | |
10377 | { | |
10378 | if (pedantic && !in_system_header) | |
10379 | pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations", | |
10380 | IDENTIFIER_POINTER (storage)); | |
10381 | ||
10382 | if (storage == ridpointers[(int) RID_INLINE]) | |
10383 | nomem_p = 1; | |
10384 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
10385 | extern_p = 1; | |
10386 | else if (storage == ridpointers[(int) RID_STATIC]) | |
10387 | static_p = 1; | |
10388 | else | |
10389 | { | |
10390 | error ("storage class `%D' applied to template instantiation", | |
10391 | storage); | |
10392 | extern_p = 0; | |
10393 | } | |
10394 | } | |
10395 | ||
10396 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) | |
10397 | { | |
10398 | /* DR 259 [temp.spec]. | |
10399 | ||
10400 | Both an explicit instantiation and a declaration of an explicit | |
10401 | specialization shall not appear in a program unless the explicit | |
10402 | instantiation follows a declaration of the explicit specialization. | |
10403 | ||
10404 | For a given set of template parameters, if an explicit | |
10405 | instantiation of a template appears after a declaration of an | |
10406 | explicit specialization for that template, the explicit | |
10407 | instantiation has no effect. */ | |
10408 | return; | |
10409 | } | |
10410 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
10411 | { | |
10412 | /* [temp.spec] | |
10413 | ||
10414 | No program shall explicitly instantiate any template more | |
10415 | than once. | |
10416 | ||
10417 | If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation | |
10418 | was `extern'. If EXTERN_P then the second is. If -frepo, chances | |
10419 | are we already got marked as an explicit instantiation because of the | |
10420 | repo file. All these cases are OK. */ | |
10421 | if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository | |
10422 | && (complain & tf_error)) | |
10423 | pedwarn ("duplicate explicit instantiation of `%#T'", t); | |
10424 | ||
10425 | /* If we've already instantiated the template, just return now. */ | |
10426 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
10427 | return; | |
10428 | } | |
10429 | ||
10430 | mark_class_instantiated (t, extern_p); | |
10431 | repo_template_instantiated (t, extern_p); | |
10432 | ||
10433 | if (nomem_p) | |
10434 | return; | |
10435 | ||
10436 | { | |
10437 | tree tmp; | |
10438 | ||
10439 | /* In contrast to implicit instantiation, where only the | |
10440 | declarations, and not the definitions, of members are | |
10441 | instantiated, we have here: | |
10442 | ||
10443 | [temp.explicit] | |
10444 | ||
10445 | The explicit instantiation of a class template specialization | |
10446 | implies the instantiation of all of its members not | |
10447 | previously explicitly specialized in the translation unit | |
10448 | containing the explicit instantiation. | |
10449 | ||
10450 | Of course, we can't instantiate member template classes, since | |
10451 | we don't have any arguments for them. Note that the standard | |
10452 | is unclear on whether the instantiation of the members are | |
10453 | *explicit* instantiations or not. We choose to be generous, | |
10454 | and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow | |
10455 | the explicit instantiation of a class where some of the members | |
10456 | have no definition in the current translation unit. */ | |
10457 | ||
10458 | if (! static_p) | |
10459 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
10460 | if (TREE_CODE (tmp) == FUNCTION_DECL | |
10461 | && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
10462 | { | |
10463 | mark_decl_instantiated (tmp, extern_p); | |
10464 | repo_template_instantiated (tmp, extern_p); | |
10465 | if (! extern_p) | |
10466 | instantiate_decl (tmp, /*defer_ok=*/1); | |
10467 | } | |
10468 | ||
10469 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
10470 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
10471 | { | |
10472 | mark_decl_instantiated (tmp, extern_p); | |
10473 | repo_template_instantiated (tmp, extern_p); | |
10474 | if (! extern_p) | |
10475 | instantiate_decl (tmp, /*defer_ok=*/1); | |
10476 | } | |
10477 | ||
10478 | for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
10479 | if (IS_AGGR_TYPE (TREE_VALUE (tmp)) | |
10480 | && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp)))) | |
10481 | do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0); | |
10482 | } | |
10483 | } | |
10484 | ||
10485 | /* Given a function DECL, which is a specialization of TMPL, modify | |
10486 | DECL to be a re-instantiation of TMPL with the same template | |
10487 | arguments. TMPL should be the template into which tsubst'ing | |
10488 | should occur for DECL, not the most general template. | |
10489 | ||
10490 | One reason for doing this is a scenario like this: | |
10491 | ||
10492 | template <class T> | |
10493 | void f(const T&, int i); | |
10494 | ||
10495 | void g() { f(3, 7); } | |
10496 | ||
10497 | template <class T> | |
10498 | void f(const T& t, const int i) { } | |
10499 | ||
10500 | Note that when the template is first instantiated, with | |
10501 | instantiate_template, the resulting DECL will have no name for the | |
10502 | first parameter, and the wrong type for the second. So, when we go | |
10503 | to instantiate the DECL, we regenerate it. */ | |
10504 | ||
10505 | static void | |
10506 | regenerate_decl_from_template (decl, tmpl) | |
10507 | tree decl; | |
10508 | tree tmpl; | |
10509 | { | |
10510 | /* The most general version of TMPL. */ | |
10511 | tree gen_tmpl; | |
10512 | /* The arguments used to instantiate DECL, from the most general | |
10513 | template. */ | |
10514 | tree args; | |
10515 | tree code_pattern; | |
10516 | tree new_decl; | |
10517 | int unregistered; | |
10518 | ||
10519 | args = DECL_TI_ARGS (decl); | |
10520 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
10521 | ||
10522 | /* Unregister the specialization so that when we tsubst we will not | |
10523 | just return DECL. We don't have to unregister DECL from TMPL | |
10524 | because if would only be registered there if it were a partial | |
10525 | instantiation of a specialization, which it isn't: it's a full | |
10526 | instantiation. */ | |
10527 | gen_tmpl = most_general_template (tmpl); | |
10528 | push_access_scope_real (gen_tmpl, args, DECL_CONTEXT (decl)); | |
10529 | unregistered = unregister_specialization (decl, gen_tmpl); | |
10530 | ||
10531 | /* If the DECL was not unregistered then something peculiar is | |
10532 | happening: we created a specialization but did not call | |
10533 | register_specialization for it. */ | |
10534 | my_friendly_assert (unregistered, 0); | |
10535 | ||
10536 | /* Do the substitution to get the new declaration. */ | |
10537 | new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE); | |
10538 | ||
10539 | if (TREE_CODE (decl) == VAR_DECL) | |
10540 | { | |
10541 | /* Set up DECL_INITIAL, since tsubst doesn't. */ | |
10542 | if (!DECL_INITIALIZED_IN_CLASS_P (decl)) | |
10543 | DECL_INITIAL (new_decl) = | |
10544 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
10545 | tf_error, DECL_TI_TEMPLATE (decl)); | |
10546 | } | |
10547 | else if (TREE_CODE (decl) == FUNCTION_DECL) | |
10548 | { | |
10549 | /* Convince duplicate_decls to use the DECL_ARGUMENTS from the | |
10550 | new decl. */ | |
10551 | DECL_INITIAL (new_decl) = error_mark_node; | |
10552 | /* And don't complain about a duplicate definition. */ | |
10553 | DECL_INITIAL (decl) = NULL_TREE; | |
10554 | } | |
10555 | ||
10556 | pop_access_scope (decl); | |
10557 | ||
10558 | /* The immediate parent of the new template is still whatever it was | |
10559 | before, even though tsubst sets DECL_TI_TEMPLATE up as the most | |
10560 | general template. We also reset the DECL_ASSEMBLER_NAME since | |
10561 | tsubst always calculates the name as if the function in question | |
10562 | were really a template instance, and sometimes, with friend | |
10563 | functions, this is not so. See tsubst_friend_function for | |
10564 | details. */ | |
10565 | DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl); | |
10566 | COPY_DECL_ASSEMBLER_NAME (decl, new_decl); | |
10567 | COPY_DECL_RTL (decl, new_decl); | |
10568 | DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl); | |
10569 | ||
10570 | /* Call duplicate decls to merge the old and new declarations. */ | |
10571 | duplicate_decls (new_decl, decl); | |
10572 | ||
10573 | /* Now, re-register the specialization. */ | |
10574 | register_specialization (decl, gen_tmpl, args); | |
10575 | } | |
10576 | ||
10577 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be | |
10578 | substituted to get DECL. */ | |
10579 | ||
10580 | static tree | |
10581 | template_for_substitution (tree decl) | |
10582 | { | |
10583 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
10584 | ||
10585 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
10586 | for the instantiation. This is not always the most general | |
10587 | template. Consider, for example: | |
10588 | ||
10589 | template <class T> | |
10590 | struct S { template <class U> void f(); | |
10591 | template <> void f<int>(); }; | |
10592 | ||
10593 | and an instantiation of S<double>::f<int>. We want TD to be the | |
10594 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
10595 | while (/* An instantiation cannot have a definition, so we need a | |
10596 | more general template. */ | |
10597 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
10598 | /* We must also deal with friend templates. Given: | |
10599 | ||
10600 | template <class T> struct S { | |
10601 | template <class U> friend void f() {}; | |
10602 | }; | |
10603 | ||
10604 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
10605 | so far as the language is concerned, but that's still | |
10606 | where we get the pattern for the instantiation from. On | |
10607 | other hand, if the definition comes outside the class, say: | |
10608 | ||
10609 | template <class T> struct S { | |
10610 | template <class U> friend void f(); | |
10611 | }; | |
10612 | template <class U> friend void f() {} | |
10613 | ||
10614 | we don't need to look any further. That's what the check for | |
10615 | DECL_INITIAL is for. */ | |
10616 | || (TREE_CODE (decl) == FUNCTION_DECL | |
10617 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
10618 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
10619 | { | |
10620 | /* The present template, TD, should not be a definition. If it | |
10621 | were a definition, we should be using it! Note that we | |
10622 | cannot restructure the loop to just keep going until we find | |
10623 | a template with a definition, since that might go too far if | |
10624 | a specialization was declared, but not defined. */ | |
10625 | my_friendly_assert (!(TREE_CODE (decl) == VAR_DECL | |
10626 | && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))), | |
10627 | 0); | |
10628 | ||
10629 | /* Fetch the more general template. */ | |
10630 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
10631 | } | |
10632 | ||
10633 | return tmpl; | |
10634 | } | |
10635 | ||
10636 | /* Produce the definition of D, a _DECL generated from a template. If | |
10637 | DEFER_OK is nonzero, then we don't have to actually do the | |
10638 | instantiation now; we just have to do it sometime. */ | |
10639 | ||
10640 | tree | |
10641 | instantiate_decl (d, defer_ok) | |
10642 | tree d; | |
10643 | int defer_ok; | |
10644 | { | |
10645 | tree tmpl = DECL_TI_TEMPLATE (d); | |
10646 | tree gen_args; | |
10647 | tree args; | |
10648 | tree td; | |
10649 | tree code_pattern; | |
10650 | tree spec; | |
10651 | tree gen_tmpl; | |
10652 | int pattern_defined; | |
10653 | int line = lineno; | |
10654 | int need_push; | |
10655 | const char *file = input_filename; | |
10656 | ||
10657 | /* This function should only be used to instantiate templates for | |
10658 | functions and static member variables. */ | |
10659 | my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL | |
10660 | || TREE_CODE (d) == VAR_DECL, 0); | |
10661 | ||
10662 | /* Don't instantiate cloned functions. Instead, instantiate the | |
10663 | functions they cloned. */ | |
10664 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
10665 | d = DECL_CLONED_FUNCTION (d); | |
10666 | ||
10667 | if (DECL_TEMPLATE_INSTANTIATED (d)) | |
10668 | /* D has already been instantiated. It might seem reasonable to | |
10669 | check whether or not D is an explicit instantiation, and, if so, | |
10670 | stop here. But when an explicit instantiation is deferred | |
10671 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
10672 | is set, even though we still need to do the instantiation. */ | |
10673 | return d; | |
10674 | ||
10675 | /* If we already have a specialization of this declaration, then | |
10676 | there's no reason to instantiate it. Note that | |
10677 | retrieve_specialization gives us both instantiations and | |
10678 | specializations, so we must explicitly check | |
10679 | DECL_TEMPLATE_SPECIALIZATION. */ | |
10680 | gen_tmpl = most_general_template (tmpl); | |
10681 | gen_args = DECL_TI_ARGS (d); | |
10682 | spec = retrieve_specialization (gen_tmpl, gen_args); | |
10683 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
10684 | return spec; | |
10685 | ||
10686 | /* This needs to happen before any tsubsting. */ | |
10687 | if (! push_tinst_level (d)) | |
10688 | return d; | |
10689 | ||
10690 | timevar_push (TV_PARSE); | |
10691 | ||
10692 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern | |
10693 | for the instantiation. */ | |
10694 | td = template_for_substitution (d); | |
10695 | code_pattern = DECL_TEMPLATE_RESULT (td); | |
10696 | ||
10697 | /* In the case of a friend template whose definition is provided | |
10698 | outside the class, we may have too many arguments. Drop the ones | |
10699 | we don't need. */ | |
10700 | args = get_innermost_template_args (gen_args, | |
10701 | TMPL_PARMS_DEPTH | |
10702 | (DECL_TEMPLATE_PARMS (td))); | |
10703 | ||
10704 | if (TREE_CODE (d) == FUNCTION_DECL) | |
10705 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); | |
10706 | else | |
10707 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); | |
10708 | ||
10709 | lineno = DECL_SOURCE_LINE (d); | |
10710 | input_filename = DECL_SOURCE_FILE (d); | |
10711 | ||
10712 | if (pattern_defined) | |
10713 | { | |
10714 | /* Let the repository code that this template definition is | |
10715 | available. | |
10716 | ||
10717 | The repository doesn't need to know about cloned functions | |
10718 | because they never actually show up in the object file. It | |
10719 | does need to know about the clones; those are the symbols | |
10720 | that the linker will be emitting error messages about. */ | |
10721 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d) | |
10722 | || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d)) | |
10723 | { | |
10724 | tree t; | |
10725 | ||
10726 | for (t = TREE_CHAIN (d); | |
10727 | t && DECL_CLONED_FUNCTION_P (t); | |
10728 | t = TREE_CHAIN (t)) | |
10729 | repo_template_used (t); | |
10730 | } | |
10731 | else | |
10732 | repo_template_used (d); | |
10733 | ||
10734 | if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d)) | |
10735 | { | |
10736 | if (flag_alt_external_templates) | |
10737 | { | |
10738 | if (interface_unknown) | |
10739 | warn_if_unknown_interface (d); | |
10740 | } | |
10741 | else if (DECL_INTERFACE_KNOWN (code_pattern)) | |
10742 | { | |
10743 | DECL_INTERFACE_KNOWN (d) = 1; | |
10744 | DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern); | |
10745 | } | |
10746 | else | |
10747 | warn_if_unknown_interface (code_pattern); | |
10748 | } | |
10749 | ||
10750 | if (at_eof) | |
10751 | import_export_decl (d); | |
10752 | } | |
10753 | ||
10754 | if (!defer_ok) | |
10755 | { | |
10756 | /* Recheck the substitutions to obtain any warning messages | |
10757 | about ignoring cv qualifiers. */ | |
10758 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
10759 | tree type = TREE_TYPE (gen); | |
10760 | ||
10761 | /* Make sure that we can see identifiers, and compute access | |
10762 | correctly. D is already the target FUNCTION_DECL with the | |
10763 | right context. */ | |
10764 | push_access_scope (d); | |
10765 | ||
10766 | if (TREE_CODE (gen) == FUNCTION_DECL) | |
10767 | { | |
10768 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d); | |
10769 | tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args, | |
10770 | tf_error | tf_warning, d); | |
10771 | /* Don't simply tsubst the function type, as that will give | |
10772 | duplicate warnings about poor parameter qualifications. | |
10773 | The function arguments are the same as the decl_arguments | |
10774 | without the top level cv qualifiers. */ | |
10775 | type = TREE_TYPE (type); | |
10776 | } | |
10777 | tsubst (type, gen_args, tf_error | tf_warning, d); | |
10778 | ||
10779 | pop_access_scope (d); | |
10780 | } | |
10781 | ||
10782 | if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d) | |
10783 | && DECL_INITIAL (d) == NULL_TREE) | |
10784 | /* We should have set up DECL_INITIAL in instantiate_class_template. */ | |
10785 | abort (); | |
10786 | /* Reject all external templates except inline functions. */ | |
10787 | else if (DECL_INTERFACE_KNOWN (d) | |
10788 | && ! DECL_NOT_REALLY_EXTERN (d) | |
10789 | && ! (TREE_CODE (d) == FUNCTION_DECL | |
10790 | && DECL_INLINE (d))) | |
10791 | goto out; | |
10792 | /* Defer all other templates, unless we have been explicitly | |
10793 | forbidden from doing so. We restore the source position here | |
10794 | because it's used by add_pending_template. */ | |
10795 | else if (! pattern_defined || defer_ok) | |
10796 | { | |
10797 | lineno = line; | |
10798 | input_filename = file; | |
10799 | ||
10800 | if (at_eof && !pattern_defined | |
10801 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
10802 | /* [temp.explicit] | |
10803 | ||
10804 | The definition of a non-exported function template, a | |
10805 | non-exported member function template, or a non-exported | |
10806 | member function or static data member of a class template | |
10807 | shall be present in every translation unit in which it is | |
10808 | explicitly instantiated. */ | |
10809 | pedwarn | |
10810 | ("explicit instantiation of `%D' but no definition available", d); | |
10811 | ||
10812 | add_pending_template (d); | |
10813 | goto out; | |
10814 | } | |
10815 | ||
10816 | need_push = !global_bindings_p (); | |
10817 | if (need_push) | |
10818 | push_to_top_level (); | |
10819 | ||
10820 | /* We're now committed to instantiating this template. Mark it as | |
10821 | instantiated so that recursive calls to instantiate_decl do not | |
10822 | try to instantiate it again. */ | |
10823 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
10824 | ||
10825 | /* Regenerate the declaration in case the template has been modified | |
10826 | by a subsequent redeclaration. */ | |
10827 | regenerate_decl_from_template (d, td); | |
10828 | ||
10829 | /* We already set the file and line above. Reset them now in case | |
10830 | they changed as a result of calling regenerate_decl_from_template. */ | |
10831 | lineno = DECL_SOURCE_LINE (d); | |
10832 | input_filename = DECL_SOURCE_FILE (d); | |
10833 | ||
10834 | if (TREE_CODE (d) == VAR_DECL) | |
10835 | { | |
10836 | DECL_IN_AGGR_P (d) = 0; | |
10837 | if (DECL_INTERFACE_KNOWN (d)) | |
10838 | DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d); | |
10839 | else | |
10840 | { | |
10841 | DECL_EXTERNAL (d) = 1; | |
10842 | DECL_NOT_REALLY_EXTERN (d) = 1; | |
10843 | } | |
10844 | cp_finish_decl (d, | |
10845 | (!DECL_INITIALIZED_IN_CLASS_P (d) | |
10846 | ? DECL_INITIAL (d) : NULL_TREE), | |
10847 | NULL_TREE, 0); | |
10848 | } | |
10849 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
10850 | { | |
10851 | htab_t saved_local_specializations; | |
10852 | tree subst_decl; | |
10853 | tree tmpl_parm; | |
10854 | tree spec_parm; | |
10855 | ||
10856 | /* Save away the current list, in case we are instantiating one | |
10857 | template from within the body of another. */ | |
10858 | saved_local_specializations = local_specializations; | |
10859 | ||
10860 | /* Set up the list of local specializations. */ | |
10861 | local_specializations = htab_create (37, | |
10862 | hash_local_specialization, | |
10863 | eq_local_specializations, | |
10864 | NULL); | |
10865 | ||
10866 | /* Set up context. */ | |
10867 | start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED); | |
10868 | ||
10869 | /* Create substitution entries for the parameters. */ | |
10870 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
10871 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
10872 | spec_parm = DECL_ARGUMENTS (d); | |
10873 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
10874 | { | |
10875 | register_local_specialization (spec_parm, tmpl_parm); | |
10876 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
10877 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
10878 | } | |
10879 | while (tmpl_parm) | |
10880 | { | |
10881 | register_local_specialization (spec_parm, tmpl_parm); | |
10882 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
10883 | spec_parm = TREE_CHAIN (spec_parm); | |
10884 | } | |
10885 | my_friendly_assert (!spec_parm, 20020813); | |
10886 | ||
10887 | /* Substitute into the body of the function. */ | |
10888 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
10889 | tf_error | tf_warning, tmpl); | |
10890 | ||
10891 | /* We don't need the local specializations any more. */ | |
10892 | htab_delete (local_specializations); | |
10893 | local_specializations = saved_local_specializations; | |
10894 | ||
10895 | /* Finish the function. */ | |
10896 | expand_body (finish_function (0)); | |
10897 | } | |
10898 | ||
10899 | /* We're not deferring instantiation any more. */ | |
10900 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
10901 | ||
10902 | if (need_push) | |
10903 | pop_from_top_level (); | |
10904 | ||
10905 | out: | |
10906 | lineno = line; | |
10907 | input_filename = file; | |
10908 | ||
10909 | pop_tinst_level (); | |
10910 | ||
10911 | timevar_pop (TV_PARSE); | |
10912 | ||
10913 | return d; | |
10914 | } | |
10915 | ||
10916 | /* Run through the list of templates that we wish we could | |
10917 | instantiate, and instantiate any we can. */ | |
10918 | ||
10919 | int | |
10920 | instantiate_pending_templates () | |
10921 | { | |
10922 | tree *t; | |
10923 | tree last = NULL_TREE; | |
10924 | int instantiated_something = 0; | |
10925 | int reconsider; | |
10926 | ||
10927 | do | |
10928 | { | |
10929 | reconsider = 0; | |
10930 | ||
10931 | t = &pending_templates; | |
10932 | while (*t) | |
10933 | { | |
10934 | tree instantiation = TREE_VALUE (*t); | |
10935 | ||
10936 | reopen_tinst_level (TREE_PURPOSE (*t)); | |
10937 | ||
10938 | if (TYPE_P (instantiation)) | |
10939 | { | |
10940 | tree fn; | |
10941 | ||
10942 | if (!COMPLETE_TYPE_P (instantiation)) | |
10943 | { | |
10944 | instantiate_class_template (instantiation); | |
10945 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
10946 | for (fn = TYPE_METHODS (instantiation); | |
10947 | fn; | |
10948 | fn = TREE_CHAIN (fn)) | |
10949 | if (! DECL_ARTIFICIAL (fn)) | |
10950 | instantiate_decl (fn, /*defer_ok=*/0); | |
10951 | if (COMPLETE_TYPE_P (instantiation)) | |
10952 | { | |
10953 | instantiated_something = 1; | |
10954 | reconsider = 1; | |
10955 | } | |
10956 | } | |
10957 | ||
10958 | if (COMPLETE_TYPE_P (instantiation)) | |
10959 | /* If INSTANTIATION has been instantiated, then we don't | |
10960 | need to consider it again in the future. */ | |
10961 | *t = TREE_CHAIN (*t); | |
10962 | else | |
10963 | { | |
10964 | last = *t; | |
10965 | t = &TREE_CHAIN (*t); | |
10966 | } | |
10967 | } | |
10968 | else | |
10969 | { | |
10970 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) | |
10971 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
10972 | { | |
10973 | instantiation = instantiate_decl (instantiation, | |
10974 | /*defer_ok=*/0); | |
10975 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
10976 | { | |
10977 | instantiated_something = 1; | |
10978 | reconsider = 1; | |
10979 | } | |
10980 | } | |
10981 | ||
10982 | if (DECL_TEMPLATE_SPECIALIZATION (instantiation) | |
10983 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) | |
10984 | /* If INSTANTIATION has been instantiated, then we don't | |
10985 | need to consider it again in the future. */ | |
10986 | *t = TREE_CHAIN (*t); | |
10987 | else | |
10988 | { | |
10989 | last = *t; | |
10990 | t = &TREE_CHAIN (*t); | |
10991 | } | |
10992 | } | |
10993 | tinst_depth = 0; | |
10994 | current_tinst_level = NULL_TREE; | |
10995 | } | |
10996 | last_pending_template = last; | |
10997 | } | |
10998 | while (reconsider); | |
10999 | ||
11000 | return instantiated_something; | |
11001 | } | |
11002 | ||
11003 | /* Substitute ARGVEC into T, which is a list of initializers for | |
11004 | either base class or a non-static data member. The TREE_PURPOSEs | |
11005 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
11006 | instantiate_decl. */ | |
11007 | ||
11008 | static tree | |
11009 | tsubst_initializer_list (t, argvec) | |
11010 | tree t, argvec; | |
11011 | { | |
11012 | tree inits = NULL_TREE; | |
11013 | ||
11014 | for (; t; t = TREE_CHAIN (t)) | |
11015 | { | |
11016 | tree decl; | |
11017 | tree init; | |
11018 | tree val; | |
11019 | ||
11020 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning, | |
11021 | NULL_TREE); | |
11022 | init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning, | |
11023 | NULL_TREE); | |
11024 | ||
11025 | if (!init) | |
11026 | ; | |
11027 | else if (TREE_CODE (init) == TREE_LIST) | |
11028 | for (val = init; val; val = TREE_CHAIN (val)) | |
11029 | TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val)); | |
11030 | else if (init != void_type_node) | |
11031 | init = convert_from_reference (init); | |
11032 | ||
11033 | init = expand_member_init (decl, init); | |
11034 | if (init) | |
11035 | { | |
11036 | TREE_CHAIN (init) = inits; | |
11037 | inits = init; | |
11038 | } | |
11039 | } | |
11040 | return inits; | |
11041 | } | |
11042 | ||
11043 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ | |
11044 | ||
11045 | static void | |
11046 | set_current_access_from_decl (decl) | |
11047 | tree decl; | |
11048 | { | |
11049 | if (TREE_PRIVATE (decl)) | |
11050 | current_access_specifier = access_private_node; | |
11051 | else if (TREE_PROTECTED (decl)) | |
11052 | current_access_specifier = access_protected_node; | |
11053 | else | |
11054 | current_access_specifier = access_public_node; | |
11055 | } | |
11056 | ||
11057 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG | |
11058 | is the instantiation (which should have been created with | |
11059 | start_enum) and ARGS are the template arguments to use. */ | |
11060 | ||
11061 | static void | |
11062 | tsubst_enum (tag, newtag, args) | |
11063 | tree tag; | |
11064 | tree newtag; | |
11065 | tree args; | |
11066 | { | |
11067 | tree e; | |
11068 | ||
11069 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
11070 | { | |
11071 | tree value; | |
11072 | ||
11073 | /* Note that in a template enum, the TREE_VALUE is the | |
11074 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
11075 | value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)), | |
11076 | args, tf_error | tf_warning, | |
11077 | NULL_TREE); | |
11078 | ||
11079 | /* Give this enumeration constant the correct access. */ | |
11080 | set_current_access_from_decl (TREE_VALUE (e)); | |
11081 | ||
11082 | /* Actually build the enumerator itself. */ | |
11083 | build_enumerator (TREE_PURPOSE (e), value, newtag); | |
11084 | } | |
11085 | ||
11086 | finish_enum (newtag); | |
11087 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) | |
11088 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
11089 | } | |
11090 | ||
11091 | /* DECL is a FUNCTION_DECL that is a template specialization. Return | |
11092 | its type -- but without substituting the innermost set of template | |
11093 | arguments. So, innermost set of template parameters will appear in | |
11094 | the type. */ | |
11095 | ||
11096 | tree | |
11097 | get_mostly_instantiated_function_type (decl) | |
11098 | tree decl; | |
11099 | { | |
11100 | tree fn_type; | |
11101 | tree tmpl; | |
11102 | tree targs; | |
11103 | tree tparms; | |
11104 | int parm_depth; | |
11105 | ||
11106 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
11107 | targs = DECL_TI_ARGS (decl); | |
11108 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
11109 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
11110 | ||
11111 | /* There should be as many levels of arguments as there are levels | |
11112 | of parameters. */ | |
11113 | my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0); | |
11114 | ||
11115 | fn_type = TREE_TYPE (tmpl); | |
11116 | ||
11117 | if (parm_depth == 1) | |
11118 | /* No substitution is necessary. */ | |
11119 | ; | |
11120 | else | |
11121 | { | |
11122 | int i; | |
11123 | tree partial_args; | |
11124 | ||
11125 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
11126 | let tsubst know not to substitute for those parameters. */ | |
11127 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); | |
11128 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
11129 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
11130 | TMPL_ARGS_LEVEL (targs, i)); | |
11131 | SET_TMPL_ARGS_LEVEL (partial_args, | |
11132 | TMPL_ARGS_DEPTH (targs), | |
11133 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
11134 | ||
11135 | /* Make sure that we can see identifiers, and compute access | |
11136 | correctly. We can just use the context of DECL for the | |
11137 | partial substitution here. It depends only on outer template | |
11138 | parameters, regardless of whether the innermost level is | |
11139 | specialized or not. */ | |
11140 | push_access_scope (decl); | |
11141 | ||
11142 | /* Now, do the (partial) substitution to figure out the | |
11143 | appropriate function type. */ | |
11144 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); | |
11145 | ||
11146 | /* Substitute into the template parameters to obtain the real | |
11147 | innermost set of parameters. This step is important if the | |
11148 | innermost set of template parameters contains value | |
11149 | parameters whose types depend on outer template parameters. */ | |
11150 | TREE_VEC_LENGTH (partial_args)--; | |
11151 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); | |
11152 | ||
11153 | pop_access_scope (decl); | |
11154 | } | |
11155 | ||
11156 | return fn_type; | |
11157 | } | |
11158 | ||
11159 | /* Return truthvalue if we're processing a template different from | |
11160 | the last one involved in diagnostics. */ | |
11161 | int | |
11162 | problematic_instantiation_changed () | |
11163 | { | |
11164 | return last_template_error_tick != tinst_level_tick; | |
11165 | } | |
11166 | ||
11167 | /* Remember current template involved in diagnostics. */ | |
11168 | void | |
11169 | record_last_problematic_instantiation () | |
11170 | { | |
11171 | last_template_error_tick = tinst_level_tick; | |
11172 | } | |
11173 | ||
11174 | tree | |
11175 | current_instantiation () | |
11176 | { | |
11177 | return current_tinst_level; | |
11178 | } | |
11179 | ||
11180 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
11181 | type. Return zero for ok, nonzero for disallowed. Issue error and | |
11182 | warning messages under control of COMPLAIN. */ | |
11183 | ||
11184 | static int | |
11185 | invalid_nontype_parm_type_p (type, complain) | |
11186 | tree type; | |
11187 | tsubst_flags_t complain; | |
11188 | { | |
11189 | if (INTEGRAL_TYPE_P (type)) | |
11190 | return 0; | |
11191 | else if (POINTER_TYPE_P (type)) | |
11192 | return 0; | |
11193 | else if (TYPE_PTRMEM_P (type)) | |
11194 | return 0; | |
11195 | else if (TYPE_PTRMEMFUNC_P (type)) | |
11196 | return 0; | |
11197 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) | |
11198 | return 0; | |
11199 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
11200 | return 0; | |
11201 | ||
11202 | if (complain & tf_error) | |
11203 | error ("`%#T' is not a valid type for a template constant parameter", | |
11204 | type); | |
11205 | return 1; | |
11206 | } | |
11207 | ||
11208 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. | |
11209 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
11210 | ||
11211 | static bool | |
11212 | dependent_type_p_r (tree type) | |
11213 | { | |
11214 | tree scope; | |
11215 | ||
11216 | /* [temp.dep.type] | |
11217 | ||
11218 | A type is dependent if it is: | |
11219 | ||
11220 | -- a template parameter. */ | |
11221 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) | |
11222 | return true; | |
11223 | /* -- a qualified-id with a nested-name-specifier which contains a | |
11224 | class-name that names a dependent type or whose unqualified-id | |
11225 | names a dependent type. */ | |
11226 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
11227 | return true; | |
11228 | /* -- a cv-qualified type where the cv-unqualified type is | |
11229 | dependent. */ | |
11230 | type = TYPE_MAIN_VARIANT (type); | |
11231 | /* -- a compound type constructed from any dependent type. */ | |
11232 | if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type)) | |
11233 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) | |
11234 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE | |
11235 | (type))); | |
11236 | else if (TREE_CODE (type) == POINTER_TYPE | |
11237 | || TREE_CODE (type) == REFERENCE_TYPE) | |
11238 | return dependent_type_p (TREE_TYPE (type)); | |
11239 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
11240 | || TREE_CODE (type) == METHOD_TYPE) | |
11241 | { | |
11242 | tree arg_type; | |
11243 | ||
11244 | if (dependent_type_p (TREE_TYPE (type))) | |
11245 | return true; | |
11246 | for (arg_type = TYPE_ARG_TYPES (type); | |
11247 | arg_type; | |
11248 | arg_type = TREE_CHAIN (arg_type)) | |
11249 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
11250 | return true; | |
11251 | return false; | |
11252 | } | |
11253 | /* -- an array type constructed from any dependent type or whose | |
11254 | size is specified by a constant expression that is | |
11255 | value-dependent. */ | |
11256 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11257 | { | |
11258 | if (TYPE_DOMAIN (type) | |
11259 | && ((value_dependent_expression_p | |
11260 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) | |
11261 | || (type_dependent_expression_p | |
11262 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))))) | |
11263 | return true; | |
11264 | return dependent_type_p (TREE_TYPE (type)); | |
11265 | } | |
11266 | /* -- a template-id in which either the template name is a template | |
11267 | parameter or any of the template arguments is a dependent type or | |
11268 | an expression that is type-dependent or value-dependent. | |
11269 | ||
11270 | This language seems somewhat confused; for example, it does not | |
11271 | discuss template template arguments. Therefore, we use the | |
11272 | definition for dependent template arguments in [temp.dep.temp]. */ | |
11273 | if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) | |
11274 | && (dependent_template_id_p | |
11275 | (CLASSTYPE_TI_TEMPLATE (type), | |
11276 | CLASSTYPE_TI_ARGS (type)))) | |
11277 | return true; | |
11278 | else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
11279 | return true; | |
11280 | /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof' | |
11281 | expression is not type-dependent, then it should already been | |
11282 | have resolved. */ | |
11283 | if (TREE_CODE (type) == TYPEOF_TYPE) | |
11284 | return true; | |
11285 | /* The standard does not specifically mention types that are local | |
11286 | to template functions or local classes, but they should be | |
11287 | considered dependent too. For example: | |
11288 | ||
11289 | template <int I> void f() { | |
11290 | enum E { a = I }; | |
11291 | S<sizeof (E)> s; | |
11292 | } | |
11293 | ||
11294 | The size of `E' cannot be known until the value of `I' has been | |
11295 | determined. Therefore, `E' must be considered dependent. */ | |
11296 | scope = TYPE_CONTEXT (type); | |
11297 | if (scope && TYPE_P (scope)) | |
11298 | return dependent_type_p (scope); | |
11299 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
11300 | return type_dependent_expression_p (scope); | |
11301 | ||
11302 | /* Other types are non-dependent. */ | |
11303 | return false; | |
11304 | } | |
11305 | ||
11306 | /* Returns TRUE if TYPE is dependent, in the sense of | |
11307 | [temp.dep.type]. */ | |
11308 | ||
11309 | bool | |
11310 | dependent_type_p (tree type) | |
11311 | { | |
11312 | /* If there are no template parameters in scope, then there can't be | |
11313 | any dependent types. */ | |
11314 | if (!processing_template_decl) | |
11315 | return false; | |
11316 | ||
11317 | /* If the type is NULL, we have not computed a type for the entity | |
11318 | in question; in that case, the type is dependent. */ | |
11319 | if (!type) | |
11320 | return true; | |
11321 | ||
11322 | /* Erroneous types can be considered non-dependent. */ | |
11323 | if (type == error_mark_node) | |
11324 | return false; | |
11325 | ||
11326 | /* If we have not already computed the appropriate value for TYPE, | |
11327 | do so now. */ | |
11328 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
11329 | { | |
11330 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
11331 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
11332 | } | |
11333 | ||
11334 | return TYPE_DEPENDENT_P (type); | |
11335 | } | |
11336 | ||
11337 | /* Returns TRUE if the EXPRESSION is value-dependent. */ | |
11338 | ||
11339 | bool | |
11340 | value_dependent_expression_p (tree expression) | |
11341 | { | |
11342 | if (!processing_template_decl) | |
11343 | return false; | |
11344 | ||
11345 | /* A name declared with a dependent type. */ | |
11346 | if (TREE_CODE (expression) == LOOKUP_EXPR | |
11347 | || (DECL_P (expression) | |
11348 | && dependent_type_p (TREE_TYPE (expression)))) | |
11349 | return true; | |
11350 | /* A non-type template parameter. */ | |
11351 | if ((TREE_CODE (expression) == CONST_DECL | |
11352 | && DECL_TEMPLATE_PARM_P (expression)) | |
11353 | || TREE_CODE (expression) == TEMPLATE_PARM_INDEX) | |
11354 | return true; | |
11355 | /* A constant with integral or enumeration type and is initialized | |
11356 | with an expression that is value-dependent. */ | |
11357 | if (TREE_CODE (expression) == VAR_DECL | |
11358 | && DECL_INITIAL (expression) | |
11359 | && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression)) | |
11360 | || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE) | |
11361 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
11362 | return true; | |
11363 | /* These expressions are value-dependent if the type to which the | |
11364 | cast occurs is dependent. */ | |
11365 | if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
11366 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
11367 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
11368 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
11369 | || TREE_CODE (expression) == CAST_EXPR) | |
11370 | && dependent_type_p (TREE_TYPE (expression))) | |
11371 | return true; | |
11372 | /* A `sizeof' expression where the sizeof operand is a type is | |
11373 | value-dependent if the type is dependent. If the type was not | |
11374 | dependent, we would no longer have a SIZEOF_EXPR, so any | |
11375 | SIZEOF_EXPR is dependent. */ | |
11376 | if (TREE_CODE (expression) == SIZEOF_EXPR) | |
11377 | return true; | |
11378 | /* A constant expression is value-dependent if any subexpression is | |
11379 | value-dependent. */ | |
11380 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression)))) | |
11381 | { | |
11382 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) | |
11383 | { | |
11384 | case '1': | |
11385 | return (value_dependent_expression_p | |
11386 | (TREE_OPERAND (expression, 0))); | |
11387 | case '<': | |
11388 | case '2': | |
11389 | return ((value_dependent_expression_p | |
11390 | (TREE_OPERAND (expression, 0))) | |
11391 | || (value_dependent_expression_p | |
11392 | (TREE_OPERAND (expression, 1)))); | |
11393 | case 'e': | |
11394 | { | |
11395 | int i; | |
11396 | for (i = 0; i < first_rtl_op (TREE_CODE (expression)); ++i) | |
11397 | /* In some cases, some of the operands may be missing. | |
11398 | (For example, in the case of PREDECREMENT_EXPR, the | |
11399 | amount to increment by may be missing.) That doesn't | |
11400 | make the expression dependent. */ | |
11401 | if (TREE_OPERAND (expression, i) | |
11402 | && (value_dependent_expression_p | |
11403 | (TREE_OPERAND (expression, i)))) | |
11404 | return true; | |
11405 | return false; | |
11406 | } | |
11407 | } | |
11408 | } | |
11409 | ||
11410 | /* The expression is not value-dependent. */ | |
11411 | return false; | |
11412 | } | |
11413 | ||
11414 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
11415 | [temp.dep.expr]. */ | |
11416 | ||
11417 | bool | |
11418 | type_dependent_expression_p (expression) | |
11419 | tree expression; | |
11420 | { | |
11421 | if (!processing_template_decl) | |
11422 | return false; | |
11423 | ||
11424 | /* Some expression forms are never type-dependent. */ | |
11425 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
11426 | || TREE_CODE (expression) == SIZEOF_EXPR | |
11427 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
11428 | || TREE_CODE (expression) == TYPEID_EXPR | |
11429 | || TREE_CODE (expression) == DELETE_EXPR | |
11430 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
11431 | || TREE_CODE (expression) == THROW_EXPR) | |
11432 | return false; | |
11433 | ||
11434 | /* The types of these expressions depends only on the type to which | |
11435 | the cast occurs. */ | |
11436 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
11437 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
11438 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
11439 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
11440 | || TREE_CODE (expression) == CAST_EXPR) | |
11441 | return dependent_type_p (TREE_TYPE (expression)); | |
11442 | /* The types of these expressions depends only on the type created | |
11443 | by the expression. */ | |
11444 | else if (TREE_CODE (expression) == NEW_EXPR | |
11445 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
11446 | return dependent_type_p (TREE_OPERAND (expression, 1)); | |
11447 | ||
11448 | if (TREE_CODE (expression) == FUNCTION_DECL | |
11449 | && DECL_LANG_SPECIFIC (expression) | |
11450 | && DECL_TEMPLATE_INFO (expression) | |
11451 | && (dependent_template_id_p | |
11452 | (DECL_TI_TEMPLATE (expression), | |
11453 | INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
11454 | return true; | |
11455 | ||
11456 | return (dependent_type_p (TREE_TYPE (expression))); | |
11457 | } | |
11458 | ||
11459 | /* Returns TRUE if the ARG (a template argument) is dependent. */ | |
11460 | ||
11461 | bool | |
11462 | dependent_template_arg_p (tree arg) | |
11463 | { | |
11464 | if (!processing_template_decl) | |
11465 | return false; | |
11466 | ||
11467 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
11468 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
11469 | return dependent_template_p (arg); | |
11470 | else if (TYPE_P (arg)) | |
11471 | return dependent_type_p (arg); | |
11472 | else | |
11473 | return (type_dependent_expression_p (arg) | |
11474 | || value_dependent_expression_p (arg)); | |
11475 | } | |
11476 | ||
11477 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ | |
11478 | ||
11479 | static bool | |
11480 | dependent_template_id_p (tree tmpl, tree args) | |
11481 | { | |
11482 | int i; | |
11483 | ||
11484 | if (dependent_template_p (tmpl)) | |
11485 | return true; | |
11486 | for (i = 0; i < TREE_VEC_LENGTH (args); ++i) | |
11487 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) | |
11488 | return true; | |
11489 | return false; | |
11490 | } | |
11491 | ||
11492 | /* Returns TRUE if the template TMPL is dependent. */ | |
11493 | ||
11494 | bool | |
11495 | dependent_template_p (tree tmpl) | |
11496 | { | |
11497 | /* Template template parameters are dependent. */ | |
11498 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
11499 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
11500 | return true; | |
11501 | /* So are member templates of dependent classes. */ | |
11502 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
11503 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
11504 | return false; | |
11505 | } | |
11506 | ||
11507 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the | |
11508 | TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE | |
11509 | can be found. Note that this function peers inside uninstantiated | |
11510 | templates and therefore should be used only in extremely limited | |
11511 | situations. */ | |
11512 | ||
11513 | tree | |
11514 | resolve_typename_type (tree type, bool only_current_p) | |
11515 | { | |
11516 | tree scope; | |
11517 | tree name; | |
11518 | tree decl; | |
11519 | int quals; | |
11520 | ||
11521 | my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE, | |
11522 | 20010702); | |
11523 | ||
11524 | scope = TYPE_CONTEXT (type); | |
11525 | name = TYPE_IDENTIFIER (type); | |
11526 | ||
11527 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
11528 | it first before we can figure out what NAME refers to. */ | |
11529 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
11530 | scope = resolve_typename_type (scope, only_current_p); | |
11531 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
11532 | TYPENAME_TYPE. */ | |
11533 | if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE) | |
11534 | return error_mark_node; | |
11535 | /* If the SCOPE is a template type parameter, we have no way of | |
11536 | resolving the name. */ | |
11537 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
11538 | return type; | |
11539 | /* If the SCOPE is not the current instantiation, there's no reason | |
11540 | to look inside it. */ | |
11541 | if (only_current_p && !currently_open_class (scope)) | |
11542 | return error_mark_node; | |
11543 | /* Enter the SCOPE so that name lookup will be resolved as if we | |
11544 | were in the class definition. In particular, SCOPE will no | |
11545 | longer be considered a dependent type. */ | |
11546 | push_scope (scope); | |
11547 | /* Look up the declaration. */ | |
11548 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); | |
11549 | /* Obtain the set of qualifiers applied to the TYPE. */ | |
11550 | quals = cp_type_quals (type); | |
11551 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to | |
11552 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
11553 | if (!decl) | |
11554 | type = error_mark_node; | |
11555 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE | |
11556 | && TREE_CODE (decl) == TYPE_DECL) | |
11557 | type = TREE_TYPE (decl); | |
11558 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR | |
11559 | && DECL_CLASS_TEMPLATE_P (decl)) | |
11560 | { | |
11561 | tree tmpl; | |
11562 | tree args; | |
11563 | /* Obtain the template and the arguments. */ | |
11564 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
11565 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
11566 | /* Instantiate the template. */ | |
11567 | type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, | |
11568 | /*entering_scope=*/0, | |
11569 | tf_error); | |
11570 | } | |
11571 | else | |
11572 | type = error_mark_node; | |
11573 | /* Qualify the resulting type. */ | |
11574 | if (type != error_mark_node && quals) | |
11575 | type = cp_build_qualified_type (type, quals); | |
11576 | /* Leave the SCOPE. */ | |
11577 | pop_scope (scope); | |
11578 | ||
11579 | return type; | |
11580 | } | |
11581 | ||
11582 | tree | |
11583 | resolve_typename_type_in_current_instantiation (tree type) | |
11584 | { | |
11585 | tree t; | |
11586 | ||
11587 | t = resolve_typename_type (type, /*only_current_p=*/true); | |
11588 | return (t != error_mark_node) ? t : type; | |
11589 | } | |
11590 | ||
11591 | #include "gt-cp-pt.h" |