]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle parameterized types (templates) for GNU C++. |
3febd123 | 2 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
4514aa8c | 3 | 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
8d08fdba | 4 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
fc378698 | 5 | Rewritten by Jason Merrill (jason@cygnus.com). |
8d08fdba | 6 | |
f5adbb8d | 7 | This file is part of GCC. |
8d08fdba | 8 | |
f5adbb8d | 9 | GCC is free software; you can redistribute it and/or modify |
8d08fdba MS |
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 | ||
f5adbb8d | 14 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
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 | |
f5adbb8d | 20 | along with GCC; see the file COPYING. If not, write to |
e9fa0c7c RK |
21 | the Free Software Foundation, 59 Temple Place - Suite 330, |
22 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
23 | |
24 | /* Known bugs or deficiencies include: | |
e92cc029 | 25 | |
e92cc029 MS |
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". */ | |
8d08fdba MS |
28 | |
29 | #include "config.h" | |
8d052bc7 | 30 | #include "system.h" |
4977bab6 ZW |
31 | #include "coretypes.h" |
32 | #include "tm.h" | |
8d08fdba | 33 | #include "obstack.h" |
8d08fdba | 34 | #include "tree.h" |
0c58f841 | 35 | #include "pointer-set.h" |
8d08fdba MS |
36 | #include "flags.h" |
37 | #include "cp-tree.h" | |
25af8512 | 38 | #include "tree-inline.h" |
8d08fdba | 39 | #include "decl.h" |
e8abc66f | 40 | #include "output.h" |
49c249e1 | 41 | #include "except.h" |
54f92bfb | 42 | #include "toplev.h" |
3dcaad8b | 43 | #include "rtl.h" |
297a5329 | 44 | #include "timevar.h" |
325c3691 | 45 | #include "tree-iterator.h" |
49c249e1 | 46 | |
050367a3 MM |
47 | /* The type of functions taking a tree, and some additional data, and |
48 | returning an int. */ | |
3a978d72 | 49 | typedef int (*tree_fn_t) (tree, void*); |
050367a3 | 50 | |
0aafb128 MM |
51 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
52 | instantiations have been deferred, either because their definitions | |
d78e771d ZW |
53 | were not yet available, or because we were putting off doing the work. |
54 | The TREE_PURPOSE of each entry is either a DECL (for a function or | |
55 | static data member), or a TYPE (for a class) indicating what we are | |
56 | hoping to instantiate. The TREE_VALUE is not used. */ | |
e2500fed | 57 | static GTY(()) tree pending_templates; |
13e73b29 | 58 | static GTY(()) tree last_pending_template; |
73aad9b9 | 59 | |
67ffc812 | 60 | int processing_template_parmlist; |
386b8a85 JM |
61 | static int template_header_count; |
62 | ||
e2500fed GK |
63 | static GTY(()) tree saved_trees; |
64 | static GTY(()) varray_type inline_parm_levels; | |
3dcaad8b | 65 | static size_t inline_parm_levels_used; |
75650646 | 66 | |
e2500fed | 67 | static GTY(()) tree current_tinst_level; |
3ae18eaf | 68 | |
2b59fc25 KL |
69 | static GTY(()) tree saved_access_scope; |
70 | ||
0fe0caa6 RH |
71 | /* Live only within one (recursive) call to tsubst_expr. We use |
72 | this to pass the statement expression node from the STMT_EXPR | |
73 | to the EXPR_STMT that is its result. */ | |
74 | static tree cur_stmt_expr; | |
75 | ||
6dfbb909 MM |
76 | /* A map from local variable declarations in the body of the template |
77 | presently being instantiated to the corresponding instantiated | |
78 | local variables. */ | |
79 | static htab_t local_specializations; | |
80 | ||
830bfa74 MM |
81 | #define UNIFY_ALLOW_NONE 0 |
82 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
83 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
84 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 85 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 86 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
87 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
88 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
d9f818d9 | 89 | #define UNIFY_ALLOW_MAX_CORRECTION 128 |
830bfa74 | 90 | |
3a978d72 NN |
91 | static void push_access_scope (tree); |
92 | static void pop_access_scope (tree); | |
93 | static int resolve_overloaded_unification (tree, tree, tree, tree, | |
94 | unification_kind_t, int); | |
95 | static int try_one_overload (tree, tree, tree, tree, tree, | |
f23fb7f5 | 96 | unification_kind_t, int, bool); |
3a978d72 NN |
97 | static int unify (tree, tree, tree, tree, int); |
98 | static void add_pending_template (tree); | |
99 | static void reopen_tinst_level (tree); | |
100 | static tree classtype_mangled_name (tree); | |
101 | static char* mangle_class_name_for_template (const char *, tree, tree); | |
102 | static tree tsubst_initializer_list (tree, tree); | |
3a978d72 NN |
103 | static tree get_class_bindings (tree, tree, tree); |
104 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int); | |
105 | static void tsubst_enum (tree, tree, tree); | |
106 | static tree add_to_template_args (tree, tree); | |
107 | static tree add_outermost_template_args (tree, tree); | |
108 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
109 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*); | |
110 | static int type_unification_real (tree, tree, tree, tree, | |
a34d3336 | 111 | int, unification_kind_t, int); |
3a978d72 | 112 | static void note_template_header (int); |
b6ab6892 | 113 | static tree convert_nontype_argument_function (tree, tree); |
3a978d72 NN |
114 | static tree convert_nontype_argument (tree, tree); |
115 | static tree convert_template_argument (tree, tree, tree, | |
116 | tsubst_flags_t, int, tree); | |
0c58f841 MA |
117 | static int for_each_template_parm (tree, tree_fn_t, void*, |
118 | struct pointer_set_t*); | |
3a978d72 NN |
119 | static tree build_template_parm_index (int, int, int, tree, tree); |
120 | static int inline_needs_template_parms (tree); | |
121 | static void push_inline_template_parms_recursive (tree, int); | |
3a978d72 | 122 | static tree retrieve_local_specialization (tree); |
3a978d72 | 123 | static void register_local_specialization (tree, tree); |
3a978d72 | 124 | static tree reduce_template_parm_level (tree, tree, int); |
3a978d72 NN |
125 | static int mark_template_parm (tree, void *); |
126 | static int template_parm_this_level_p (tree, void *); | |
127 | static tree tsubst_friend_function (tree, tree); | |
128 | static tree tsubst_friend_class (tree, tree); | |
129 | static int can_complete_type_without_circularity (tree); | |
a34d3336 | 130 | static tree get_bindings (tree, tree, tree, bool); |
3a978d72 NN |
131 | static int template_decl_level (tree); |
132 | static int check_cv_quals_for_unify (int, tree, tree); | |
a91db711 NS |
133 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
134 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
135 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
136 | static void regenerate_decl_from_template (tree, tree); | |
137 | static tree most_specialized (tree, tree, tree); | |
138 | static tree most_specialized_class (tree, tree); | |
139 | static int template_class_depth_real (tree, int); | |
140 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); | |
3a978d72 NN |
141 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
142 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
143 | static void check_specialization_scope (void); | |
144 | static tree process_partial_specialization (tree); | |
145 | static void set_current_access_from_decl (tree); | |
146 | static void check_default_tmpl_args (tree, tree, int, int); | |
147 | static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
148 | static tree get_template_base (tree, tree, tree, tree); |
149 | static int verify_class_unification (tree, tree, tree); | |
150 | static tree try_class_unification (tree, tree, tree, tree); | |
151 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, | |
152 | tree, tree); | |
5fe7b654 | 153 | static tree determine_specialization (tree, tree, tree *, int, int); |
3a978d72 NN |
154 | static int template_args_equal (tree, tree); |
155 | static void tsubst_default_arguments (tree); | |
156 | static tree for_each_template_parm_r (tree *, int *, void *); | |
157 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
158 | static void copy_default_args_to_explicit_spec (tree); | |
159 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 160 | static int eq_local_specializations (const void *, const void *); |
5552b43c | 161 | static bool dependent_type_p_r (tree); |
14d22dd6 MM |
162 | static tree tsubst (tree, tree, tsubst_flags_t, tree); |
163 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree); | |
164 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); | |
36a117a5 | 165 | |
2b59fc25 KL |
166 | /* Make the current scope suitable for access checking when we are |
167 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
168 | template, or VAR_DECL for static member variable (need by |
169 | instantiate_decl). */ | |
2b59fc25 | 170 | |
8ce33230 | 171 | static void |
2b907f5c | 172 | push_access_scope (tree t) |
2b59fc25 | 173 | { |
50bc768d NS |
174 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
175 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 176 | |
23ff7e2d KL |
177 | if (DECL_FRIEND_CONTEXT (t)) |
178 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
179 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 180 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
181 | else |
182 | push_to_top_level (); | |
183 | ||
2b907f5c | 184 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
185 | { |
186 | saved_access_scope = tree_cons | |
187 | (NULL_TREE, current_function_decl, saved_access_scope); | |
188 | current_function_decl = t; | |
189 | } | |
2b59fc25 KL |
190 | } |
191 | ||
2b59fc25 KL |
192 | /* Restore the scope set up by push_access_scope. T is the node we |
193 | are processing. */ | |
194 | ||
8ce33230 | 195 | static void |
3a978d72 | 196 | pop_access_scope (tree t) |
2b59fc25 | 197 | { |
2b907f5c | 198 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
199 | { |
200 | current_function_decl = TREE_VALUE (saved_access_scope); | |
201 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
202 | } | |
0f399e5f | 203 | |
23ff7e2d | 204 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
205 | pop_nested_class (); |
206 | else | |
207 | pop_from_top_level (); | |
2b59fc25 KL |
208 | } |
209 | ||
a723baf1 MM |
210 | /* Do any processing required when DECL (a member template |
211 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
212 | to DECL, unless it is a specialization, in which case the DECL | |
213 | itself is returned. */ | |
e1467ff2 MM |
214 | |
215 | tree | |
3a978d72 | 216 | finish_member_template_decl (tree decl) |
e1467ff2 | 217 | { |
a723baf1 MM |
218 | if (decl == error_mark_node) |
219 | return error_mark_node; | |
220 | ||
50bc768d | 221 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
222 | |
223 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 224 | { |
a723baf1 MM |
225 | tree type; |
226 | ||
227 | type = TREE_TYPE (decl); | |
228 | if (IS_AGGR_TYPE (type) | |
229 | && CLASSTYPE_TEMPLATE_INFO (type) | |
230 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 231 | { |
a723baf1 | 232 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
233 | check_member_template (tmpl); |
234 | return tmpl; | |
235 | } | |
8d019cef | 236 | return NULL_TREE; |
93cdc044 | 237 | } |
07c88314 | 238 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 239 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 240 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 241 | { |
a1da6cba MM |
242 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
243 | { | |
244 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
245 | return DECL_TI_TEMPLATE (decl); | |
246 | } | |
247 | else | |
248 | return decl; | |
249 | } | |
250 | else | |
0f51ccfc | 251 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 252 | |
a1da6cba | 253 | return error_mark_node; |
f84b4be9 | 254 | } |
e1467ff2 | 255 | |
f84b4be9 JM |
256 | /* Returns the template nesting level of the indicated class TYPE. |
257 | ||
258 | For example, in: | |
259 | template <class T> | |
260 | struct A | |
261 | { | |
262 | template <class U> | |
263 | struct B {}; | |
264 | }; | |
265 | ||
39c01e4c MM |
266 | A<T>::B<U> has depth two, while A<T> has depth one. |
267 | Both A<T>::B<int> and A<int>::B<U> have depth one, if | |
268 | COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not | |
269 | specializations. | |
270 | ||
271 | This function is guaranteed to return 0 if passed NULL_TREE so | |
272 | that, for example, `template_class_depth (current_class_type)' is | |
273 | always safe. */ | |
f84b4be9 | 274 | |
e9659ab0 | 275 | static int |
3a978d72 | 276 | template_class_depth_real (tree type, int count_specializations) |
f84b4be9 | 277 | { |
93cdc044 | 278 | int depth; |
f84b4be9 | 279 | |
2c73f9f5 | 280 | for (depth = 0; |
ed44da02 MM |
281 | type && TREE_CODE (type) != NAMESPACE_DECL; |
282 | type = (TREE_CODE (type) == FUNCTION_DECL) | |
4f1c5b7d | 283 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 MM |
284 | { |
285 | if (TREE_CODE (type) != FUNCTION_DECL) | |
286 | { | |
287 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
288 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
289 | && ((count_specializations | |
370af2d5 | 290 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) |
ed44da02 MM |
291 | || uses_template_parms (CLASSTYPE_TI_ARGS (type)))) |
292 | ++depth; | |
293 | } | |
294 | else | |
295 | { | |
296 | if (DECL_TEMPLATE_INFO (type) | |
297 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
298 | && ((count_specializations | |
299 | && DECL_TEMPLATE_SPECIALIZATION (type)) | |
300 | || uses_template_parms (DECL_TI_ARGS (type)))) | |
301 | ++depth; | |
302 | } | |
303 | } | |
f84b4be9 JM |
304 | |
305 | return depth; | |
e1467ff2 | 306 | } |
98c1c668 | 307 | |
39c01e4c MM |
308 | /* Returns the template nesting level of the indicated class TYPE. |
309 | Like template_class_depth_real, but instantiations do not count in | |
310 | the depth. */ | |
311 | ||
312 | int | |
3a978d72 | 313 | template_class_depth (tree type) |
39c01e4c MM |
314 | { |
315 | return template_class_depth_real (type, /*count_specializations=*/0); | |
316 | } | |
317 | ||
cae40af6 JM |
318 | /* Returns 1 if processing DECL as part of do_pending_inlines |
319 | needs us to push template parms. */ | |
320 | ||
321 | static int | |
3a978d72 | 322 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
323 | { |
324 | if (! DECL_TEMPLATE_INFO (decl)) | |
325 | return 0; | |
f84b4be9 | 326 | |
36a117a5 | 327 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
328 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
329 | } | |
330 | ||
331 | /* Subroutine of maybe_begin_member_template_processing. | |
332 | Push the template parms in PARMS, starting from LEVELS steps into the | |
333 | chain, and ending at the beginning, since template parms are listed | |
334 | innermost first. */ | |
335 | ||
336 | static void | |
3a978d72 | 337 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
338 | { |
339 | tree parms = TREE_VALUE (parmlist); | |
340 | int i; | |
341 | ||
342 | if (levels > 1) | |
343 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 344 | |
98c1c668 | 345 | ++processing_template_decl; |
cae40af6 | 346 | current_template_parms |
4890c2f4 | 347 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 348 | parms, current_template_parms); |
cae40af6 JM |
349 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
350 | ||
ac20c67a GDR |
351 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
352 | NULL); | |
98c1c668 JM |
353 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
354 | { | |
786b5245 | 355 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
50bc768d | 356 | gcc_assert (DECL_P (parm)); |
cae40af6 | 357 | |
98c1c668 JM |
358 | switch (TREE_CODE (parm)) |
359 | { | |
786b5245 | 360 | case TYPE_DECL: |
73b0fce8 | 361 | case TEMPLATE_DECL: |
98c1c668 JM |
362 | pushdecl (parm); |
363 | break; | |
786b5245 MM |
364 | |
365 | case PARM_DECL: | |
366 | { | |
fc03edb3 MM |
367 | /* Make a CONST_DECL as is done in process_template_parm. |
368 | It is ugly that we recreate this here; the original | |
369 | version built in process_template_parm is no longer | |
370 | available. */ | |
786b5245 MM |
371 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
372 | TREE_TYPE (parm)); | |
c727aa5e | 373 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a DN |
374 | TREE_CONSTANT (decl) = 1; |
375 | TREE_INVARIANT (decl) = 1; | |
376 | TREE_READONLY (decl) = 1; | |
786b5245 | 377 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 378 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
379 | pushdecl (decl); |
380 | } | |
cae40af6 | 381 | break; |
786b5245 | 382 | |
98c1c668 | 383 | default: |
315fb5db | 384 | gcc_unreachable (); |
98c1c668 JM |
385 | } |
386 | } | |
387 | } | |
388 | ||
cae40af6 JM |
389 | /* Restore the template parameter context for a member template or |
390 | a friend template defined in a class definition. */ | |
391 | ||
392 | void | |
3a978d72 | 393 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
394 | { |
395 | tree parms; | |
3dcaad8b | 396 | int levels = 0; |
cae40af6 | 397 | |
3dcaad8b MM |
398 | if (inline_needs_template_parms (decl)) |
399 | { | |
400 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
401 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 402 | |
3dcaad8b MM |
403 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
404 | { | |
405 | --levels; | |
406 | parms = TREE_CHAIN (parms); | |
407 | } | |
cae40af6 | 408 | |
3dcaad8b | 409 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
410 | } |
411 | ||
3dcaad8b MM |
412 | /* Remember how many levels of template parameters we pushed so that |
413 | we can pop them later. */ | |
414 | if (!inline_parm_levels) | |
415 | VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels"); | |
416 | if (inline_parm_levels_used == inline_parm_levels->num_elements) | |
417 | VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used); | |
418 | VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels; | |
419 | ++inline_parm_levels_used; | |
cae40af6 JM |
420 | } |
421 | ||
c6002625 | 422 | /* Undo the effects of begin_member_template_processing. */ |
98c1c668 JM |
423 | |
424 | void | |
3a978d72 | 425 | maybe_end_member_template_processing (void) |
98c1c668 | 426 | { |
3dcaad8b MM |
427 | int i; |
428 | ||
429 | if (!inline_parm_levels_used) | |
98c1c668 JM |
430 | return; |
431 | ||
3dcaad8b MM |
432 | --inline_parm_levels_used; |
433 | for (i = 0; | |
434 | i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used); | |
435 | ++i) | |
cae40af6 JM |
436 | { |
437 | --processing_template_decl; | |
438 | current_template_parms = TREE_CHAIN (current_template_parms); | |
439 | poplevel (0, 0, 0); | |
440 | } | |
98c1c668 JM |
441 | } |
442 | ||
36a117a5 | 443 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 444 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
445 | |
446 | static tree | |
3a978d72 | 447 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 448 | { |
36a117a5 MM |
449 | tree new_args; |
450 | int extra_depth; | |
451 | int i; | |
452 | int j; | |
e6f1275f | 453 | |
36a117a5 | 454 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 455 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 456 | |
36a117a5 MM |
457 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
458 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 459 | |
36a117a5 MM |
460 | for (j = 1; j <= extra_depth; ++j, ++i) |
461 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
462 | ||
e6f1275f JM |
463 | return new_args; |
464 | } | |
465 | ||
36a117a5 MM |
466 | /* Like add_to_template_args, but only the outermost ARGS are added to |
467 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
468 | (EXTRA_ARGS) levels are added. This function is used to combine | |
469 | the template arguments from a partial instantiation with the | |
470 | template arguments used to attain the full instantiation from the | |
471 | partial instantiation. */ | |
98c1c668 | 472 | |
4966381a | 473 | static tree |
3a978d72 | 474 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
475 | { |
476 | tree new_args; | |
477 | ||
e4a84209 MM |
478 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
479 | something very fishy is going on. */ | |
50bc768d | 480 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
481 | |
482 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
483 | them. */ | |
484 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
485 | return extra_args; | |
486 | ||
36a117a5 MM |
487 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
488 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
489 | ||
490 | new_args = add_to_template_args (args, extra_args); | |
98c1c668 | 491 | |
36a117a5 MM |
492 | /* Now, we restore ARGS to its full dimensions. */ |
493 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
494 | |
495 | return new_args; | |
496 | } | |
5566b478 | 497 | |
f9a7ae04 MM |
498 | /* Return the N levels of innermost template arguments from the ARGS. */ |
499 | ||
500 | tree | |
3a978d72 | 501 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
502 | { |
503 | tree new_args; | |
504 | int extra_levels; | |
505 | int i; | |
506 | ||
50bc768d | 507 | gcc_assert (n >= 0); |
f9a7ae04 MM |
508 | |
509 | /* If N is 1, just return the innermost set of template arguments. */ | |
510 | if (n == 1) | |
511 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
512 | ||
513 | /* If we're not removing anything, just return the arguments we were | |
514 | given. */ | |
515 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 516 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
517 | if (extra_levels == 0) |
518 | return args; | |
519 | ||
520 | /* Make a new set of arguments, not containing the outer arguments. */ | |
521 | new_args = make_tree_vec (n); | |
522 | for (i = 1; i <= n; ++i) | |
523 | SET_TMPL_ARGS_LEVEL (new_args, i, | |
524 | TMPL_ARGS_LEVEL (args, i + extra_levels)); | |
525 | ||
526 | return new_args; | |
527 | } | |
528 | ||
5566b478 MS |
529 | /* We've got a template header coming up; push to a new level for storing |
530 | the parms. */ | |
8d08fdba | 531 | |
8d08fdba | 532 | void |
3a978d72 | 533 | begin_template_parm_list (void) |
8d08fdba | 534 | { |
6757edfe MM |
535 | /* We use a non-tag-transparent scope here, which causes pushtag to |
536 | put tags in this scope, rather than in the enclosing class or | |
537 | namespace scope. This is the right thing, since we want | |
538 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
539 | global template class, push_template_decl handles putting the | |
540 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
541 | e.g.: | |
542 | ||
543 | template <class T> struct S1 { | |
544 | template <class T> struct S2 {}; | |
545 | }; | |
546 | ||
547 | pushtag contains special code to call pushdecl_with_scope on the | |
548 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 549 | begin_scope (sk_template_parms, NULL); |
5156628f | 550 | ++processing_template_decl; |
67ffc812 | 551 | ++processing_template_parmlist; |
386b8a85 JM |
552 | note_template_header (0); |
553 | } | |
554 | ||
6c30752f | 555 | /* This routine is called when a specialization is declared. If it is |
0e339752 | 556 | invalid to declare a specialization here, an error is reported. */ |
6c30752f | 557 | |
e9659ab0 | 558 | static void |
3a978d72 | 559 | check_specialization_scope (void) |
6c30752f MM |
560 | { |
561 | tree scope = current_scope (); | |
3ddfb0e6 | 562 | |
6c30752f MM |
563 | /* [temp.expl.spec] |
564 | ||
565 | An explicit specialization shall be declared in the namespace of | |
566 | which the template is a member, or, for member templates, in the | |
567 | namespace of which the enclosing class or enclosing class | |
568 | template is a member. An explicit specialization of a member | |
569 | function, member class or static data member of a class template | |
570 | shall be declared in the namespace of which the class template | |
571 | is a member. */ | |
572 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
0f51ccfc | 573 | error ("explicit specialization in non-namespace scope %qD", scope); |
3ddfb0e6 | 574 | |
6c30752f MM |
575 | /* [temp.expl.spec] |
576 | ||
577 | In an explicit specialization declaration for a member of a class | |
578 | template or a member template that appears in namespace scope, | |
579 | the member template and some of its enclosing class templates may | |
580 | remain unspecialized, except that the declaration shall not | |
581 | explicitly specialize a class member template if its enclosing | |
582 | class templates are not explicitly specialized as well. */ | |
583 | if (current_template_parms) | |
33bd39a2 | 584 | error ("enclosing class templates are not explicitly specialized"); |
6c30752f MM |
585 | } |
586 | ||
c6002625 | 587 | /* We've just seen template <>. */ |
386b8a85 JM |
588 | |
589 | void | |
3a978d72 | 590 | begin_specialization (void) |
386b8a85 | 591 | { |
ac20c67a | 592 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 593 | note_template_header (1); |
6c30752f | 594 | check_specialization_scope (); |
386b8a85 JM |
595 | } |
596 | ||
dc957d14 | 597 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
598 | template<>. */ |
599 | ||
600 | void | |
3a978d72 | 601 | end_specialization (void) |
386b8a85 | 602 | { |
74b846e0 | 603 | finish_scope (); |
386b8a85 JM |
604 | reset_specialization (); |
605 | } | |
606 | ||
386b8a85 | 607 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 608 | function specialization. */ |
386b8a85 JM |
609 | |
610 | void | |
3a978d72 | 611 | reset_specialization (void) |
386b8a85 JM |
612 | { |
613 | processing_specialization = 0; | |
614 | template_header_count = 0; | |
615 | } | |
616 | ||
838dfd8a | 617 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
618 | it was of the form template <>. */ |
619 | ||
4966381a | 620 | static void |
3a978d72 | 621 | note_template_header (int specialization) |
386b8a85 JM |
622 | { |
623 | processing_specialization = specialization; | |
624 | template_header_count++; | |
625 | } | |
626 | ||
75650646 | 627 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 628 | |
75650646 | 629 | void |
3a978d72 | 630 | begin_explicit_instantiation (void) |
386b8a85 | 631 | { |
50bc768d | 632 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 633 | processing_explicit_instantiation = true; |
75650646 | 634 | } |
386b8a85 | 635 | |
386b8a85 | 636 | |
75650646 | 637 | void |
3a978d72 | 638 | end_explicit_instantiation (void) |
75650646 | 639 | { |
50bc768d | 640 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 641 | processing_explicit_instantiation = false; |
75650646 | 642 | } |
386b8a85 | 643 | |
b1cc95ce MM |
644 | /* A explicit specialization or partial specialization TMPL is being |
645 | declared. Check that the namespace in which the specialization is | |
646 | occurring is permissible. Returns false iff it is invalid to | |
647 | specialize TMPL in the current namespace. */ | |
648 | ||
649 | static bool | |
650 | check_specialization_namespace (tree tmpl) | |
651 | { | |
652 | tree tpl_ns = decl_namespace_context (tmpl); | |
653 | ||
654 | /* [tmpl.expl.spec] | |
655 | ||
656 | An explicit specialization shall be declared in the namespace of | |
657 | which the template is a member, or, for member templates, in the | |
658 | namespace of which the enclosing class or enclosing class | |
659 | template is a member. An explicit specialization of a member | |
660 | function, member class or static data member of a class template | |
661 | shall be declared in the namespace of which the class template is | |
662 | a member. */ | |
663 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
664 | /* Same or super-using namespace. */ | |
665 | return true; | |
666 | else | |
667 | { | |
0f51ccfc GDR |
668 | pedwarn ("specialization of %qD in different namespace", tmpl); |
669 | cp_pedwarn_at (" from definition of %q#D", tmpl); | |
b1cc95ce MM |
670 | return false; |
671 | } | |
672 | } | |
673 | ||
36a117a5 MM |
674 | /* The TYPE is being declared. If it is a template type, that means it |
675 | is a partial specialization. Do appropriate error-checking. */ | |
676 | ||
677 | void | |
3a978d72 | 678 | maybe_process_partial_specialization (tree type) |
36a117a5 | 679 | { |
6e049fcd KL |
680 | /* TYPE maybe an ERROR_MARK_NODE. */ |
681 | tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE; | |
682 | ||
a723baf1 | 683 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 684 | { |
6e049fcd KL |
685 | /* This is for ordinary explicit specialization and partial |
686 | specialization of a template class such as: | |
687 | ||
688 | template <> class C<int>; | |
689 | ||
690 | or: | |
691 | ||
692 | template <class T> class C<T*>; | |
693 | ||
694 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
695 | ||
36a117a5 | 696 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 697 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 698 | { |
b1cc95ce | 699 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 700 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 MM |
701 | if (processing_template_decl) |
702 | push_template_decl (TYPE_MAIN_DECL (type)); | |
703 | } | |
704 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 705 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 706 | } |
6e049fcd KL |
707 | else if (CLASS_TYPE_P (type) |
708 | && !CLASSTYPE_USE_TEMPLATE (type) | |
709 | && CLASSTYPE_TEMPLATE_INFO (type) | |
710 | && context && CLASS_TYPE_P (context) | |
711 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
712 | { | |
713 | /* This is for an explicit specialization of member class | |
714 | template according to [temp.expl.spec/18]: | |
715 | ||
716 | template <> template <class U> class C<int>::D; | |
717 | ||
718 | The context `C<int>' must be an implicit instantiation. | |
719 | Otherwise this is just a member class template declared | |
720 | earlier like: | |
721 | ||
722 | template <> class C<int> { template <class U> class D; }; | |
723 | template <> template <class U> class C<int>::D; | |
724 | ||
725 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
726 | while in the second case, `C<int>::D' is a primary template | |
727 | and `C<T>::D' may not exist. */ | |
728 | ||
729 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
730 | && !COMPLETE_TYPE_P (type)) | |
731 | { | |
732 | tree t; | |
733 | ||
734 | if (current_namespace | |
735 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
736 | { | |
0f51ccfc GDR |
737 | pedwarn ("specializing %q#T in different namespace", type); |
738 | cp_pedwarn_at (" from definition of %q#D", | |
6e049fcd KL |
739 | CLASSTYPE_TI_TEMPLATE (type)); |
740 | } | |
741 | ||
742 | /* Check for invalid specialization after instantiation: | |
743 | ||
744 | template <> template <> class C<int>::D<int>; | |
745 | template <> template <class U> class C<int>::D; */ | |
746 | ||
747 | for (t = DECL_TEMPLATE_INSTANTIATIONS | |
748 | (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); | |
749 | t; t = TREE_CHAIN (t)) | |
750 | if (TREE_VALUE (t) != type | |
751 | && TYPE_CONTEXT (TREE_VALUE (t)) == context) | |
0f51ccfc | 752 | error ("specialization %qT after instantiation %qT", |
6e049fcd KL |
753 | type, TREE_VALUE (t)); |
754 | ||
755 | /* Mark TYPE as a specialization. And as a result, we only | |
756 | have one level of template argument for the innermost | |
757 | class template. */ | |
758 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
759 | CLASSTYPE_TI_ARGS (type) | |
760 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
761 | } | |
762 | } | |
20496fa2 | 763 | else if (processing_specialization) |
0f51ccfc | 764 | error ("explicit specialization of non-template %qT", type); |
36a117a5 MM |
765 | } |
766 | ||
ee81147e | 767 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
768 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
769 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
770 | ||
771 | static inline bool | |
772 | optimize_specialization_lookup_p (tree tmpl) | |
773 | { | |
774 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
775 | && DECL_CLASS_SCOPE_P (tmpl) | |
776 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
777 | parameter. */ | |
778 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
779 | /* The optimized lookup depends on the fact that the |
780 | template arguments for the member function template apply | |
781 | purely to the containing class, which is not true if the | |
782 | containing class is an explicit or partial | |
783 | specialization. */ | |
784 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
785 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
786 | && !DECL_CONV_FN_P (tmpl) | |
787 | /* It is possible to have a template that is not a member | |
788 | template and is not a member of a template class: | |
789 | ||
790 | template <typename T> | |
791 | struct S { friend A::f(); }; | |
792 | ||
793 | Here, the friend function is a template, but the context does | |
794 | not have template information. The optimized lookup relies | |
795 | on having ARGS be the template arguments for both the class | |
796 | and the function template. */ | |
797 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
798 | } | |
799 | ||
75650646 MM |
800 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
801 | specialization is either an instantiation or an explicit | |
802 | specialization) of TMPL for the given template ARGS. If there is | |
803 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
804 | arguments, or a vector of vectors of arguments, in the case of | |
c7222c02 MM |
805 | templates with more than one level of parameters. |
806 | ||
807 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
808 | then we search for a partial specialization matching ARGS. This | |
809 | parameter is ignored if TMPL is not a class template. */ | |
75650646 MM |
810 | |
811 | static tree | |
c7222c02 MM |
812 | retrieve_specialization (tree tmpl, tree args, |
813 | bool class_specializations_p) | |
75650646 | 814 | { |
50bc768d | 815 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 816 | |
36a117a5 MM |
817 | /* There should be as many levels of arguments as there are |
818 | levels of parameters. */ | |
50bc768d NS |
819 | gcc_assert (TMPL_ARGS_DEPTH (args) |
820 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); | |
c7222c02 MM |
821 | |
822 | if (optimize_specialization_lookup_p (tmpl)) | |
823 | { | |
824 | tree class_template; | |
825 | tree class_specialization; | |
826 | VEC(tree) *methods; | |
827 | tree fns; | |
828 | int idx; | |
829 | ||
830 | /* The template arguments actually apply to the containing | |
831 | class. Find the class specialization with those | |
832 | arguments. */ | |
833 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
834 | class_specialization | |
835 | = retrieve_specialization (class_template, args, | |
836 | /*class_specializations_p=*/false); | |
837 | if (!class_specialization) | |
838 | return NULL_TREE; | |
839 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
840 | for the specialization. */ | |
841 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
842 | if (idx == -1) | |
843 | return NULL_TREE; | |
844 | /* Iterate through the methods with the indicated name, looking | |
845 | for the one that has an instance of TMPL. */ | |
846 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
847 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
848 | { | |
849 | tree fn = OVL_CURRENT (fns); | |
850 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl) | |
851 | return fn; | |
852 | } | |
853 | return NULL_TREE; | |
854 | } | |
855 | else | |
856 | { | |
857 | tree *sp; | |
858 | tree *head; | |
859 | ||
860 | /* Class templates store their instantiations on the | |
861 | DECL_TEMPLATE_INSTANTIATIONS list; other templates use the | |
862 | DECL_TEMPLATE_SPECIALIZATIONS list. */ | |
863 | if (!class_specializations_p | |
864 | && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL) | |
865 | sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
866 | else | |
867 | sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
868 | head = sp; | |
869 | /* Iterate through the list until we find a matching template. */ | |
870 | while (*sp != NULL_TREE) | |
871 | { | |
872 | tree spec = *sp; | |
873 | ||
874 | if (comp_template_args (TREE_PURPOSE (spec), args)) | |
875 | { | |
876 | /* Use the move-to-front heuristic to speed up future | |
877 | searches. */ | |
878 | if (spec != *head) | |
879 | { | |
880 | *sp = TREE_CHAIN (*sp); | |
881 | TREE_CHAIN (spec) = *head; | |
882 | *head = spec; | |
883 | } | |
884 | return TREE_VALUE (spec); | |
885 | } | |
886 | sp = &TREE_CHAIN (spec); | |
887 | } | |
888 | } | |
75650646 MM |
889 | |
890 | return NULL_TREE; | |
386b8a85 JM |
891 | } |
892 | ||
dc957d14 | 893 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
894 | |
895 | static tree | |
3a978d72 | 896 | retrieve_local_specialization (tree tmpl) |
9188c363 | 897 | { |
c68b0a84 KG |
898 | tree spec = htab_find_with_hash (local_specializations, tmpl, |
899 | htab_hash_pointer (tmpl)); | |
a723baf1 | 900 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
901 | } |
902 | ||
838dfd8a | 903 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
904 | |
905 | int | |
3a978d72 | 906 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
907 | { |
908 | tree t; | |
909 | ||
910 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
911 | { | |
912 | for (t = decl; | |
913 | t != NULL_TREE; | |
914 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
915 | if (t == tmpl) | |
916 | return 1; | |
917 | } | |
918 | else | |
919 | { | |
50bc768d | 920 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
921 | |
922 | for (t = TREE_TYPE (decl); | |
923 | t != NULL_TREE; | |
924 | t = CLASSTYPE_USE_TEMPLATE (t) | |
925 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 926 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe MM |
927 | return 1; |
928 | } | |
386b8a85 | 929 | |
6757edfe MM |
930 | return 0; |
931 | } | |
75650646 | 932 | |
d43f603d KL |
933 | /* Returns nonzero iff DECL is a specialization of friend declaration |
934 | FRIEND according to [temp.friend]. */ | |
935 | ||
936 | bool | |
937 | is_specialization_of_friend (tree decl, tree friend) | |
938 | { | |
939 | bool need_template = true; | |
940 | int template_depth; | |
941 | ||
b939a023 KL |
942 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
943 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d KL |
944 | |
945 | /* For [temp.friend/6] when FRIEND is an ordinary member function | |
946 | of a template class, we want to check if DECL is a specialization | |
947 | if this. */ | |
948 | if (TREE_CODE (friend) == FUNCTION_DECL | |
949 | && DECL_TEMPLATE_INFO (friend) | |
950 | && !DECL_USE_TEMPLATE (friend)) | |
951 | { | |
b939a023 | 952 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
d43f603d KL |
953 | friend = DECL_TI_TEMPLATE (friend); |
954 | need_template = false; | |
955 | } | |
b939a023 KL |
956 | else if (TREE_CODE (friend) == TEMPLATE_DECL |
957 | && !PRIMARY_TEMPLATE_P (friend)) | |
958 | need_template = false; | |
d43f603d KL |
959 | |
960 | /* There is nothing to do if this is not a template friend. */ | |
961 | if (TREE_CODE (friend) != TEMPLATE_DECL) | |
b939a023 | 962 | return false; |
d43f603d KL |
963 | |
964 | if (is_specialization_of (decl, friend)) | |
b939a023 | 965 | return true; |
d43f603d KL |
966 | |
967 | /* [temp.friend/6] | |
968 | A member of a class template may be declared to be a friend of a | |
969 | non-template class. In this case, the corresponding member of | |
970 | every specialization of the class template is a friend of the | |
971 | class granting friendship. | |
972 | ||
973 | For example, given a template friend declaration | |
974 | ||
975 | template <class T> friend void A<T>::f(); | |
976 | ||
977 | the member function below is considered a friend | |
978 | ||
979 | template <> struct A<int> { | |
980 | void f(); | |
981 | }; | |
982 | ||
983 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 984 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
985 | check if the enclosing class is a specialization of another. */ |
986 | ||
987 | template_depth = template_class_depth (DECL_CONTEXT (friend)); | |
988 | if (template_depth | |
989 | && DECL_CLASS_SCOPE_P (decl) | |
990 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), | |
991 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend)))) | |
992 | { | |
993 | /* Next, we check the members themselves. In order to handle | |
b939a023 | 994 | a few tricky cases, such as when FRIEND's are |
d43f603d KL |
995 | |
996 | template <class T> friend void A<T>::g(T t); | |
997 | template <class T> template <T t> friend void A<T>::h(); | |
998 | ||
b939a023 KL |
999 | and DECL's are |
1000 | ||
1001 | void A<int>::g(int); | |
1002 | template <int> void A<int>::h(); | |
1003 | ||
1004 | we need to figure out ARGS, the template arguments from | |
1005 | the context of DECL. This is required for template substitution | |
1006 | of `T' in the function parameter of `g' and template parameter | |
1007 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1008 | |
1009 | tree context = DECL_CONTEXT (decl); | |
1010 | tree args = NULL_TREE; | |
1011 | int current_depth = 0; | |
b939a023 | 1012 | |
d43f603d KL |
1013 | while (current_depth < template_depth) |
1014 | { | |
1015 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1016 | { | |
1017 | if (current_depth == 0) | |
1018 | args = TYPE_TI_ARGS (context); | |
1019 | else | |
1020 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1021 | current_depth++; | |
1022 | } | |
1023 | context = TYPE_CONTEXT (context); | |
1024 | } | |
1025 | ||
1026 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1027 | { | |
1028 | bool is_template; | |
1029 | tree friend_type; | |
1030 | tree decl_type; | |
1031 | tree friend_args_type; | |
1032 | tree decl_args_type; | |
1033 | ||
1034 | /* Make sure that both DECL and FRIEND are templates or | |
1035 | non-templates. */ | |
1036 | is_template = DECL_TEMPLATE_INFO (decl) | |
1037 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1038 | if (need_template ^ is_template) | |
b939a023 | 1039 | return false; |
d43f603d KL |
1040 | else if (is_template) |
1041 | { | |
da1d7781 | 1042 | /* If both are templates, check template parameter list. */ |
d43f603d KL |
1043 | tree friend_parms |
1044 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1045 | args, tf_none); | |
1046 | if (!comp_template_parms | |
1047 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1048 | friend_parms)) | |
b939a023 | 1049 | return false; |
d43f603d KL |
1050 | |
1051 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1052 | } | |
1053 | else | |
1054 | decl_type = TREE_TYPE (decl); | |
1055 | ||
1056 | friend_type = tsubst_function_type (TREE_TYPE (friend), args, | |
1057 | tf_none, NULL_TREE); | |
1058 | if (friend_type == error_mark_node) | |
b939a023 | 1059 | return false; |
d43f603d KL |
1060 | |
1061 | /* Check if return types match. */ | |
1062 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1063 | return false; |
d43f603d KL |
1064 | |
1065 | /* Check if function parameter types match, ignoring the | |
1066 | `this' parameter. */ | |
1067 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1068 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
1069 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend)) | |
1070 | friend_args_type = TREE_CHAIN (friend_args_type); | |
1071 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1072 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1073 | |
1074 | return compparms (decl_args_type, friend_args_type); | |
1075 | } | |
1076 | else | |
1077 | { | |
1078 | /* DECL is a TYPE_DECL */ | |
1079 | bool is_template; | |
1080 | tree decl_type = TREE_TYPE (decl); | |
1081 | ||
1082 | /* Make sure that both DECL and FRIEND are templates or | |
1083 | non-templates. */ | |
1084 | is_template | |
1085 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1086 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1087 | ||
1088 | if (need_template ^ is_template) | |
1089 | return false; | |
1090 | else if (is_template) | |
1091 | { | |
1092 | tree friend_parms; | |
1093 | /* If both are templates, check the name of the two | |
1094 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1095 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
1096 | != DECL_NAME (friend)) | |
1097 | return false; | |
1098 | ||
1099 | /* Now check template parameter list. */ | |
1100 | friend_parms | |
1101 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1102 | args, tf_none); | |
1103 | return comp_template_parms | |
1104 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1105 | friend_parms); | |
1106 | } | |
1107 | else | |
1108 | return (DECL_NAME (decl) | |
1109 | == DECL_NAME (friend)); | |
d43f603d KL |
1110 | } |
1111 | } | |
b939a023 | 1112 | return false; |
d43f603d KL |
1113 | } |
1114 | ||
75650646 | 1115 | /* Register the specialization SPEC as a specialization of TMPL with |
36a117a5 MM |
1116 | the indicated ARGS. Returns SPEC, or an equivalent prior |
1117 | declaration, if available. */ | |
75650646 | 1118 | |
36a117a5 | 1119 | static tree |
3a978d72 | 1120 | register_specialization (tree spec, tree tmpl, tree args) |
75650646 | 1121 | { |
c7222c02 | 1122 | tree fn; |
75650646 | 1123 | |
50bc768d | 1124 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 1125 | |
36a117a5 MM |
1126 | if (TREE_CODE (spec) == FUNCTION_DECL |
1127 | && uses_template_parms (DECL_TI_ARGS (spec))) | |
1128 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1129 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1130 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1131 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1132 | with default function arguments. In particular, given | |
1133 | something like this: | |
1134 | ||
1135 | template <class T> void f(T t1, T t = T()) | |
1136 | ||
1137 | the default argument expression is not substituted for in an | |
1138 | instantiation unless and until it is actually needed. */ | |
1139 | return spec; | |
28ba38e3 | 1140 | |
36a117a5 MM |
1141 | /* There should be as many levels of arguments as there are |
1142 | levels of parameters. */ | |
50bc768d NS |
1143 | gcc_assert (TMPL_ARGS_DEPTH (args) |
1144 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); | |
36a117a5 | 1145 | |
c7222c02 MM |
1146 | fn = retrieve_specialization (tmpl, args, |
1147 | /*class_specializations_p=*/false); | |
1148 | /* We can sometimes try to re-register a specialization that we've | |
1149 | already got. In particular, regenerate_decl_from_template calls | |
1150 | duplicate_decls which will update the specialization list. But, | |
1151 | we'll still get called again here anyhow. It's more convenient | |
1152 | to simply allow this than to try to prevent it. */ | |
1153 | if (fn == spec) | |
1154 | return spec; | |
1155 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1156 | { |
c7222c02 | 1157 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1158 | { |
c7222c02 MM |
1159 | if (TREE_USED (fn) |
1160 | || DECL_EXPLICIT_INSTANTIATION (fn)) | |
22e9174f | 1161 | { |
c7222c02 MM |
1162 | error ("specialization of %qD after instantiation", |
1163 | fn); | |
1164 | return spec; | |
22e9174f | 1165 | } |
c7222c02 | 1166 | else |
b1cc95ce | 1167 | { |
c7222c02 MM |
1168 | /* This situation should occur only if the first |
1169 | specialization is an implicit instantiation, the | |
1170 | second is an explicit specialization, and the | |
1171 | implicit instantiation has not yet been used. That | |
1172 | situation can occur if we have implicitly | |
1173 | instantiated a member function and then specialized | |
1174 | it later. | |
1175 | ||
1176 | We can also wind up here if a friend declaration that | |
1177 | looked like an instantiation turns out to be a | |
1178 | specialization: | |
1179 | ||
1180 | template <class T> void foo(T); | |
1181 | class S { friend void foo<>(int) }; | |
1182 | template <> void foo(int); | |
1183 | ||
1184 | We transform the existing DECL in place so that any | |
1185 | pointers to it become pointers to the updated | |
1186 | declaration. | |
1187 | ||
1188 | If there was a definition for the template, but not | |
1189 | for the specialization, we want this to look as if | |
1190 | there were no definition, and vice versa. */ | |
1191 | DECL_INITIAL (fn) = NULL_TREE; | |
1192 | duplicate_decls (spec, fn); | |
b1cc95ce MM |
1193 | |
1194 | return fn; | |
1195 | } | |
22e9174f | 1196 | } |
c7222c02 MM |
1197 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1198 | { | |
1199 | if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec)) | |
1200 | /* Dup decl failed, but this is a new definition. Set the | |
1201 | line number so any errors match this new | |
1202 | definition. */ | |
1203 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
1204 | ||
1205 | return fn; | |
1206 | } | |
b1cc95ce MM |
1207 | } |
1208 | ||
1209 | /* A specialization must be declared in the same namespace as the | |
1210 | template it is specializing. */ | |
1211 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1212 | && !check_specialization_namespace (tmpl)) | |
1213 | DECL_CONTEXT (spec) = decl_namespace_context (tmpl); | |
75650646 | 1214 | |
c7222c02 MM |
1215 | if (!optimize_specialization_lookup_p (tmpl)) |
1216 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
1217 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
1218 | |
1219 | return spec; | |
1220 | } | |
1221 | ||
1222 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
b3445994 MM |
1223 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1224 | if the SPEC was listed as a specialization of TMPL. */ | |
36a117a5 | 1225 | |
b3445994 MM |
1226 | bool |
1227 | reregister_specialization (tree spec, tree tmpl, tree new_spec) | |
36a117a5 MM |
1228 | { |
1229 | tree* s; | |
1230 | ||
1231 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
1232 | *s != NULL_TREE; | |
1233 | s = &TREE_CHAIN (*s)) | |
1234 | if (TREE_VALUE (*s) == spec) | |
1235 | { | |
b3445994 MM |
1236 | if (!new_spec) |
1237 | *s = TREE_CHAIN (*s); | |
1238 | else | |
a3d87771 | 1239 | TREE_VALUE (*s) = new_spec; |
36a117a5 MM |
1240 | return 1; |
1241 | } | |
1242 | ||
1243 | return 0; | |
75650646 MM |
1244 | } |
1245 | ||
a723baf1 MM |
1246 | /* Compare an entry in the local specializations hash table P1 (which |
1247 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1248 | DECL). */ | |
1249 | ||
1250 | static int | |
1251 | eq_local_specializations (const void *p1, const void *p2) | |
1252 | { | |
1253 | return TREE_VALUE ((tree) p1) == (tree) p2; | |
1254 | } | |
1255 | ||
69f794a7 MM |
1256 | /* Hash P1, an entry in the local specializations table. */ |
1257 | ||
1258 | static hashval_t | |
1259 | hash_local_specialization (const void* p1) | |
1260 | { | |
1261 | return htab_hash_pointer (TREE_VALUE ((tree) p1)); | |
1262 | } | |
1263 | ||
6dfbb909 MM |
1264 | /* Like register_specialization, but for local declarations. We are |
1265 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1266 | |
414ea4aa | 1267 | static void |
3a978d72 | 1268 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1269 | { |
6dfbb909 MM |
1270 | void **slot; |
1271 | ||
69f794a7 MM |
1272 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
1273 | htab_hash_pointer (tmpl), INSERT); | |
a723baf1 | 1274 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1275 | } |
1276 | ||
e1467ff2 MM |
1277 | /* Print the list of candidate FNS in an error message. */ |
1278 | ||
104bf76a | 1279 | void |
3a978d72 | 1280 | print_candidates (tree fns) |
e1467ff2 MM |
1281 | { |
1282 | tree fn; | |
1283 | ||
d8e178a0 | 1284 | const char *str = "candidates are:"; |
e1467ff2 MM |
1285 | |
1286 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1287 | { | |
d6479fe7 MM |
1288 | tree f; |
1289 | ||
1290 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
1291 | cp_error_at ("%s %+#D", str, OVL_CURRENT (f)); | |
e1467ff2 MM |
1292 | str = " "; |
1293 | } | |
1294 | } | |
1295 | ||
75650646 | 1296 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1297 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1298 | explicit template args given in TEMPLATE_ID. The DECL may be |
1299 | NULL_TREE if none is available. In that case, the functions in | |
1300 | TEMPLATE_ID are non-members. | |
1301 | ||
838dfd8a | 1302 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1303 | specialization of a member template. |
1304 | ||
5fe7b654 GB |
1305 | The TEMPLATE_COUNT is the number of references to qualifying |
1306 | template classes that appeared in the name of the function. See | |
1307 | check_explicit_specialization for a more accurate description. | |
1308 | ||
03017874 MM |
1309 | The template args (those explicitly specified and those deduced) |
1310 | are output in a newly created vector *TARGS_OUT. | |
1311 | ||
1312 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1313 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1314 | |
e9659ab0 | 1315 | static tree |
3a978d72 NN |
1316 | determine_specialization (tree template_id, |
1317 | tree decl, | |
1318 | tree* targs_out, | |
5fe7b654 GB |
1319 | int need_member_template, |
1320 | int template_count) | |
386b8a85 | 1321 | { |
03017874 MM |
1322 | tree fns; |
1323 | tree targs; | |
1324 | tree explicit_targs; | |
1325 | tree candidates = NULL_TREE; | |
1326 | tree templates = NULL_TREE; | |
5fe7b654 GB |
1327 | int header_count; |
1328 | struct cp_binding_level *b; | |
386b8a85 | 1329 | |
e1467ff2 MM |
1330 | *targs_out = NULL_TREE; |
1331 | ||
f2e48b67 BK |
1332 | if (template_id == error_mark_node) |
1333 | return error_mark_node; | |
1334 | ||
1335 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1336 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1337 | |
27fafc8d JM |
1338 | if (fns == error_mark_node) |
1339 | return error_mark_node; | |
1340 | ||
c6002625 | 1341 | /* Check for baselinks. */ |
91e490ab | 1342 | if (BASELINK_P (fns)) |
da15dae6 | 1343 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1344 | |
91e490ab MM |
1345 | if (!is_overloaded_fn (fns)) |
1346 | { | |
0f51ccfc | 1347 | error ("%qD is not a function template", fns); |
91e490ab MM |
1348 | return error_mark_node; |
1349 | } | |
1350 | ||
5fe7b654 GB |
1351 | /* Count the number of template headers specified for this |
1352 | specialization. */ | |
1353 | header_count = 0; | |
98f99d7f MM |
1354 | for (b = current_binding_level; |
1355 | b->kind == sk_template_parms; | |
5fe7b654 GB |
1356 | b = b->level_chain) |
1357 | ++header_count; | |
1358 | ||
2c73f9f5 | 1359 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1360 | { |
9dfce8fd | 1361 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1362 | |
1363 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1364 | { |
1365 | tree decl_arg_types; | |
0da99d4e | 1366 | tree fn_arg_types; |
d955f6ea KL |
1367 | |
1368 | /* DECL might be a specialization of FN. */ | |
1369 | ||
1370 | /* Adjust the type of DECL in case FN is a static member. */ | |
1371 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1372 | if (DECL_STATIC_FUNCTION_P (fn) | |
1373 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1374 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1375 | ||
1376 | /* Check that the number of function parameters matches. | |
1377 | For example, | |
1378 | template <class T> void f(int i = 0); | |
1379 | template <> void f<int>(); | |
0e339752 | 1380 | The specialization f<int> is invalid but is not caught |
d955f6ea KL |
1381 | by get_bindings below. */ |
1382 | ||
0da99d4e GB |
1383 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
1384 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1385 | continue; | |
1386 | ||
1387 | /* For a non-static member function, we need to make sure that | |
1388 | the const qualification is the same. This can be done by | |
1389 | checking the 'this' in the argument list. */ | |
1390 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
1391 | && !same_type_p (TREE_VALUE (fn_arg_types), | |
1392 | TREE_VALUE (decl_arg_types))) | |
d955f6ea KL |
1393 | continue; |
1394 | ||
5fe7b654 GB |
1395 | /* In case of explicit specialization, we need to check if |
1396 | the number of template headers appearing in the specialization | |
1397 | is correct. This is usually done in check_explicit_specialization, | |
1398 | but the check done there cannot be exhaustive when specializing | |
1399 | member functions. Consider the following code: | |
1400 | ||
1401 | template <> void A<int>::f(int); | |
1402 | template <> template <> void A<int>::f(int); | |
1403 | ||
1404 | Assuming that A<int> is not itself an explicit specialization | |
1405 | already, the first line specializes "f" which is a non-template | |
1406 | member function, whilst the second line specializes "f" which | |
1407 | is a template member function. So both lines are syntactically | |
1408 | correct, and check_explicit_specialization does not reject | |
1409 | them. | |
1410 | ||
1411 | Here, we can do better, as we are matching the specialization | |
1412 | against the declarations. We count the number of template | |
1413 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1414 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1415 | plus there must be another header for the member template | |
1416 | itself). | |
1417 | ||
1418 | Notice that if header_count is zero, this is not a | |
1419 | specialization but rather a template instantiation, so there | |
1420 | is no check we can perform here. */ | |
1421 | if (header_count && header_count != template_count + 1) | |
1422 | continue; | |
1423 | ||
98f99d7f MM |
1424 | /* Check that the number of template arguments at the |
1425 | innermost level for DECL is the same as for FN. */ | |
1426 | if (current_binding_level->kind == sk_template_parms | |
1427 | && !current_binding_level->explicit_spec_p | |
1428 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
1429 | != TREE_VEC_LENGTH (TREE_VALUE (current_template_parms)))) | |
1430 | continue; | |
1431 | ||
d955f6ea KL |
1432 | /* See whether this function might be a specialization of this |
1433 | template. */ | |
a34d3336 | 1434 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d955f6ea KL |
1435 | |
1436 | if (!targs) | |
1437 | /* We cannot deduce template arguments that when used to | |
1438 | specialize TMPL will produce DECL. */ | |
1439 | continue; | |
1440 | ||
1441 | /* Save this template, and the arguments deduced. */ | |
1442 | templates = tree_cons (targs, fn, templates); | |
1443 | } | |
03017874 MM |
1444 | else if (need_member_template) |
1445 | /* FN is an ordinary member function, and we need a | |
1446 | specialization of a member template. */ | |
d955f6ea | 1447 | ; |
03017874 MM |
1448 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1449 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1450 | cases. */ | |
d955f6ea | 1451 | ; |
03017874 MM |
1452 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1453 | /* This is just an ordinary non-member function. Nothing can | |
1454 | be a specialization of that. */ | |
d955f6ea | 1455 | ; |
3b82c249 KL |
1456 | else if (DECL_ARTIFICIAL (fn)) |
1457 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1458 | ; |
75650646 | 1459 | else |
03017874 MM |
1460 | { |
1461 | tree decl_arg_types; | |
386b8a85 | 1462 | |
03017874 MM |
1463 | /* This is an ordinary member function. However, since |
1464 | we're here, we can assume it's enclosing class is a | |
1465 | template class. For example, | |
1466 | ||
1467 | template <typename T> struct S { void f(); }; | |
1468 | template <> void S<int>::f() {} | |
1469 | ||
1470 | Here, S<int>::f is a non-template, but S<int> is a | |
1471 | template class. If FN has the same type as DECL, we | |
1472 | might be in business. */ | |
f5d47abd KL |
1473 | |
1474 | if (!DECL_TEMPLATE_INFO (fn)) | |
1475 | /* Its enclosing class is an explicit specialization | |
1476 | of a template class. This is not a candidate. */ | |
1477 | continue; | |
1478 | ||
03017874 MM |
1479 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1480 | TREE_TYPE (TREE_TYPE (fn)))) | |
1481 | /* The return types differ. */ | |
1482 | continue; | |
1483 | ||
1484 | /* Adjust the type of DECL in case FN is a static member. */ | |
1485 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1486 | if (DECL_STATIC_FUNCTION_P (fn) | |
1487 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1488 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1489 | ||
1490 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1491 | decl_arg_types)) | |
1492 | /* They match! */ | |
1493 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1494 | } |
386b8a85 | 1495 | } |
03017874 | 1496 | |
bf8f3f93 | 1497 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1498 | { |
03017874 MM |
1499 | /* We have: |
1500 | ||
1501 | [temp.expl.spec] | |
1502 | ||
1503 | It is possible for a specialization with a given function | |
1504 | signature to be instantiated from more than one function | |
1505 | template. In such cases, explicit specification of the | |
1506 | template arguments must be used to uniquely identify the | |
1507 | function template specialization being specialized. | |
1508 | ||
1509 | Note that here, there's no suggestion that we're supposed to | |
1510 | determine which of the candidate templates is most | |
1511 | specialized. However, we, also have: | |
1512 | ||
1513 | [temp.func.order] | |
1514 | ||
1515 | Partial ordering of overloaded function template | |
1516 | declarations is used in the following contexts to select | |
1517 | the function template to which a function template | |
1518 | specialization refers: | |
1519 | ||
1520 | -- when an explicit specialization refers to a function | |
1521 | template. | |
1522 | ||
1523 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 1524 | This extension can only serve to make invalid programs valid, |
03017874 MM |
1525 | so it's safe. And, there is strong anecdotal evidence that |
1526 | the committee intended the partial ordering rules to apply; | |
1527 | the EDG front-end has that behavior, and John Spicer claims | |
1528 | that the committee simply forgot to delete the wording in | |
1529 | [temp.expl.spec]. */ | |
1530 | tree tmpl = most_specialized (templates, decl, explicit_targs); | |
1531 | if (tmpl && tmpl != error_mark_node) | |
1532 | { | |
a34d3336 | 1533 | targs = get_bindings (tmpl, decl, explicit_targs, /*check_ret=*/true); |
e1b3e07d | 1534 | templates = tree_cons (targs, tmpl, NULL_TREE); |
03017874 | 1535 | } |
e1467ff2 MM |
1536 | } |
1537 | ||
03017874 | 1538 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1539 | { |
0f51ccfc GDR |
1540 | cp_error_at ("template-id %qD for %q+D does not match any template " |
1541 | "declaration", | |
bf8f3f93 | 1542 | template_id, decl); |
03017874 | 1543 | return error_mark_node; |
386b8a85 | 1544 | } |
03017874 | 1545 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1546 | || (candidates && TREE_CHAIN (candidates)) |
1547 | || (templates && candidates)) | |
386b8a85 | 1548 | { |
0f51ccfc | 1549 | cp_error_at ("ambiguous template specialization %qD for %q+D", |
bf8f3f93 MM |
1550 | template_id, decl); |
1551 | chainon (candidates, templates); | |
1552 | print_candidates (candidates); | |
03017874 | 1553 | return error_mark_node; |
386b8a85 JM |
1554 | } |
1555 | ||
c6002625 | 1556 | /* We have one, and exactly one, match. */ |
03017874 MM |
1557 | if (candidates) |
1558 | { | |
1559 | /* It was a specialization of an ordinary member function in a | |
1560 | template class. */ | |
1561 | *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates))); | |
1562 | return DECL_TI_TEMPLATE (TREE_VALUE (candidates)); | |
1563 | } | |
1564 | ||
1565 | /* It was a specialization of a template. */ | |
17aec3eb | 1566 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1567 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1568 | { | |
1569 | *targs_out = copy_node (targs); | |
1570 | SET_TMPL_ARGS_LEVEL (*targs_out, | |
1571 | TMPL_ARGS_DEPTH (*targs_out), | |
1572 | TREE_PURPOSE (templates)); | |
1573 | } | |
1574 | else | |
1575 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1576 | return TREE_VALUE (templates); |
8d08fdba | 1577 | } |
8afa707f MM |
1578 | |
1579 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1580 | but with the default argument values filled in from those in the | |
1581 | TMPL_TYPES. */ | |
6757edfe | 1582 | |
8afa707f | 1583 | static tree |
3a978d72 NN |
1584 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1585 | tree tmpl_types) | |
8afa707f MM |
1586 | { |
1587 | tree new_spec_types; | |
1588 | ||
1589 | if (!spec_types) | |
1590 | return NULL_TREE; | |
1591 | ||
1592 | if (spec_types == void_list_node) | |
1593 | return void_list_node; | |
1594 | ||
1595 | /* Substitute into the rest of the list. */ | |
1596 | new_spec_types = | |
1597 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1598 | TREE_CHAIN (tmpl_types)); | |
1599 | ||
1600 | /* Add the default argument for this parameter. */ | |
1601 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1602 | TREE_VALUE (spec_types), | |
1603 | new_spec_types); | |
1604 | } | |
1605 | ||
1606 | /* DECL is an explicit specialization. Replicate default arguments | |
1607 | from the template it specializes. (That way, code like: | |
1608 | ||
1609 | template <class T> void f(T = 3); | |
1610 | template <> void f(double); | |
1611 | void g () { f (); } | |
1612 | ||
1613 | works, as required.) An alternative approach would be to look up | |
1614 | the correct default arguments at the call-site, but this approach | |
1615 | is consistent with how implicit instantiations are handled. */ | |
1616 | ||
1617 | static void | |
3a978d72 | 1618 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
1619 | { |
1620 | tree tmpl; | |
1621 | tree spec_types; | |
1622 | tree tmpl_types; | |
1623 | tree new_spec_types; | |
1624 | tree old_type; | |
1625 | tree new_type; | |
1626 | tree t; | |
08c2df0f NS |
1627 | tree object_type = NULL_TREE; |
1628 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 1629 | tree vtt = NULL_TREE; |
8afa707f MM |
1630 | |
1631 | /* See if there's anything we need to do. */ | |
1632 | tmpl = DECL_TI_TEMPLATE (decl); | |
1633 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1634 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1635 | if (TREE_PURPOSE (t)) | |
1636 | break; | |
1637 | if (!t) | |
1638 | return; | |
1639 | ||
1640 | old_type = TREE_TYPE (decl); | |
1641 | spec_types = TYPE_ARG_TYPES (old_type); | |
1642 | ||
8afa707f MM |
1643 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1644 | { | |
08c2df0f NS |
1645 | /* Remove the this pointer, but remember the object's type for |
1646 | CV quals. */ | |
1647 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); | |
8afa707f MM |
1648 | spec_types = TREE_CHAIN (spec_types); |
1649 | tmpl_types = TREE_CHAIN (tmpl_types); | |
08c2df0f | 1650 | |
8afa707f | 1651 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
08c2df0f NS |
1652 | { |
1653 | /* DECL may contain more parameters than TMPL due to the extra | |
1654 | in-charge parameter in constructors and destructors. */ | |
1655 | in_charge = spec_types; | |
1656 | spec_types = TREE_CHAIN (spec_types); | |
1657 | } | |
e0fff4b3 JM |
1658 | if (DECL_HAS_VTT_PARM_P (decl)) |
1659 | { | |
1660 | vtt = spec_types; | |
1661 | spec_types = TREE_CHAIN (spec_types); | |
1662 | } | |
8afa707f MM |
1663 | } |
1664 | ||
1665 | /* Compute the merged default arguments. */ | |
1666 | new_spec_types = | |
1667 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); | |
1668 | ||
08c2df0f NS |
1669 | /* Compute the new FUNCTION_TYPE. */ |
1670 | if (object_type) | |
8afa707f | 1671 | { |
e0fff4b3 JM |
1672 | if (vtt) |
1673 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), | |
1674 | TREE_VALUE (vtt), | |
1675 | new_spec_types); | |
1676 | ||
08c2df0f NS |
1677 | if (in_charge) |
1678 | /* Put the in-charge parameter back. */ | |
1679 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1680 | TREE_VALUE (in_charge), | |
1681 | new_spec_types); | |
1682 | ||
43dc123f MM |
1683 | new_type = build_method_type_directly (object_type, |
1684 | TREE_TYPE (old_type), | |
1685 | new_spec_types); | |
8afa707f | 1686 | } |
8afa707f MM |
1687 | else |
1688 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 1689 | new_spec_types); |
e9525111 MM |
1690 | new_type = cp_build_type_attribute_variant (new_type, |
1691 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
1692 | new_type = build_exception_variant (new_type, |
1693 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1694 | TREE_TYPE (decl) = new_type; | |
1695 | } | |
1696 | ||
386b8a85 | 1697 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1698 | DECLARATOR, and in DECL, is a specialization of a function |
1699 | template. We may also discover that the declaration is an explicit | |
1700 | instantiation at this point. | |
1701 | ||
e1467ff2 | 1702 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1703 | instead if all goes well. Issues an error message if something is |
1704 | amiss. Returns error_mark_node if the error is not easily | |
1705 | recoverable. | |
75650646 | 1706 | |
75650646 MM |
1707 | FLAGS is a bitmask consisting of the following flags: |
1708 | ||
75650646 MM |
1709 | 2: The function has a definition. |
1710 | 4: The function is a friend. | |
75650646 MM |
1711 | |
1712 | The TEMPLATE_COUNT is the number of references to qualifying | |
1713 | template classes that appeared in the name of the function. For | |
1714 | example, in | |
1715 | ||
1716 | template <class T> struct S { void f(); }; | |
1717 | void S<int>::f(); | |
1718 | ||
1719 | the TEMPLATE_COUNT would be 1. However, explicitly specialized | |
1720 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1721 | ||
1722 | template <class T> struct S {}; | |
1723 | template <> struct S<int> { void f(); } | |
36a117a5 | 1724 | template <> void S<int>::f(); |
75650646 MM |
1725 | |
1726 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 1727 | invalid; there should be no template <>.) |
75650646 MM |
1728 | |
1729 | If the function is a specialization, it is marked as such via | |
1730 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
1731 | is set up correctly, and it is added to the list of specializations | |
1732 | for that template. */ | |
386b8a85 | 1733 | |
e1467ff2 | 1734 | tree |
3a978d72 NN |
1735 | check_explicit_specialization (tree declarator, |
1736 | tree decl, | |
1737 | int template_count, | |
1738 | int flags) | |
386b8a85 | 1739 | { |
75650646 MM |
1740 | int have_def = flags & 2; |
1741 | int is_friend = flags & 4; | |
1742 | int specialization = 0; | |
e1467ff2 | 1743 | int explicit_instantiation = 0; |
fd4de5ff | 1744 | int member_specialization = 0; |
75650646 MM |
1745 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1746 | tree dname = DECL_NAME (decl); | |
74b846e0 | 1747 | tmpl_spec_kind tsk; |
386b8a85 | 1748 | |
f65b7de3 GB |
1749 | if (is_friend) |
1750 | { | |
1751 | if (!processing_specialization) | |
1752 | tsk = tsk_none; | |
1753 | else | |
1754 | tsk = tsk_excessive_parms; | |
1755 | } | |
1756 | else | |
1757 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 1758 | |
74b846e0 MM |
1759 | switch (tsk) |
1760 | { | |
1761 | case tsk_none: | |
1762 | if (processing_specialization) | |
75650646 | 1763 | { |
fd4de5ff MM |
1764 | specialization = 1; |
1765 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1766 | } |
74b846e0 | 1767 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 1768 | { |
74b846e0 MM |
1769 | if (is_friend) |
1770 | /* This could be something like: | |
75650646 | 1771 | |
74b846e0 MM |
1772 | template <class T> void f(T); |
1773 | class S { friend void f<>(int); } */ | |
1774 | specialization = 1; | |
1775 | else | |
1776 | { | |
1777 | /* This case handles bogus declarations like template <> | |
1778 | template <class T> void f<int>(); */ | |
1779 | ||
0f51ccfc GDR |
1780 | error ("template-id %qD in declaration of primary template", |
1781 | declarator); | |
74b846e0 MM |
1782 | return decl; |
1783 | } | |
1784 | } | |
1785 | break; | |
1786 | ||
1787 | case tsk_invalid_member_spec: | |
1788 | /* The error has already been reported in | |
1789 | check_specialization_scope. */ | |
1790 | return error_mark_node; | |
1791 | ||
1792 | case tsk_invalid_expl_inst: | |
33bd39a2 | 1793 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
1794 | |
1795 | /* Fall through. */ | |
1796 | ||
1797 | case tsk_expl_inst: | |
fd4de5ff | 1798 | if (have_def) |
33bd39a2 | 1799 | error ("definition provided for explicit instantiation"); |
74b846e0 | 1800 | |
fd4de5ff | 1801 | explicit_instantiation = 1; |
74b846e0 | 1802 | break; |
fd4de5ff | 1803 | |
74b846e0 | 1804 | case tsk_excessive_parms: |
f03adc6b GB |
1805 | case tsk_insufficient_parms: |
1806 | if (tsk == tsk_excessive_parms) | |
0f51ccfc | 1807 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
1808 | decl); |
1809 | else if (template_header_count) | |
0f51ccfc | 1810 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 1811 | else |
0f51ccfc | 1812 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 1813 | "%<template <>%>", decl); |
75650646 | 1814 | |
74b846e0 MM |
1815 | /* Fall through. */ |
1816 | case tsk_expl_spec: | |
1817 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1818 | if (ctype) | |
1819 | member_specialization = 1; | |
1820 | else | |
1821 | specialization = 1; | |
1822 | break; | |
74b846e0 MM |
1823 | |
1824 | case tsk_template: | |
1825 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 1826 | { |
fd4de5ff MM |
1827 | /* This case handles bogus declarations like template <> |
1828 | template <class T> void f<int>(); */ | |
75650646 | 1829 | |
655dc6ee | 1830 | if (uses_template_parms (declarator)) |
0f51ccfc | 1831 | error ("function template partial specialization %qD " |
e1e93ad8 | 1832 | "is not allowed", declarator); |
655dc6ee | 1833 | else |
0f51ccfc GDR |
1834 | error ("template-id %qD in declaration of primary template", |
1835 | declarator); | |
fd4de5ff | 1836 | return decl; |
386b8a85 | 1837 | } |
74b846e0 MM |
1838 | |
1839 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1840 | /* This is a specialization of a member template, without | |
1841 | specialization the containing class. Something like: | |
1842 | ||
1843 | template <class T> struct S { | |
1844 | template <class U> void f (U); | |
1845 | }; | |
1846 | template <> template <class U> void S<int>::f(U) {} | |
1847 | ||
1848 | That's a specialization -- but of the entire template. */ | |
1849 | specialization = 1; | |
1850 | break; | |
1851 | ||
1852 | default: | |
315fb5db | 1853 | gcc_unreachable (); |
75650646 | 1854 | } |
386b8a85 | 1855 | |
670960ac JM |
1856 | if (specialization || member_specialization) |
1857 | { | |
1858 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1859 | for (; t; t = TREE_CHAIN (t)) | |
1860 | if (TREE_PURPOSE (t)) | |
1861 | { | |
33bd39a2 | 1862 | pedwarn |
8251199e | 1863 | ("default argument specified in explicit specialization"); |
670960ac JM |
1864 | break; |
1865 | } | |
f3400fe2 | 1866 | if (current_lang_name == lang_name_c) |
33bd39a2 | 1867 | error ("template specialization with C linkage"); |
670960ac JM |
1868 | } |
1869 | ||
e1467ff2 | 1870 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1871 | { |
1872 | tree tmpl = NULL_TREE; | |
1873 | tree targs = NULL_TREE; | |
75650646 MM |
1874 | |
1875 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1876 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1877 | { | |
1878 | tree fns; | |
1879 | ||
50bc768d | 1880 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 1881 | if (ctype) |
386b8a85 | 1882 | fns = dname; |
2f54a1db GB |
1883 | else |
1884 | { | |
1885 | /* If there is no class context, the explicit instantiation | |
1886 | must be at namespace scope. */ | |
50bc768d | 1887 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
1888 | |
1889 | /* Find the namespace binding, using the declaration | |
1890 | context. */ | |
1891 | fns = namespace_binding (dname, CP_DECL_CONTEXT (decl)); | |
4230cec2 NS |
1892 | if (!fns || !is_overloaded_fn (fns)) |
1893 | { | |
1894 | error ("%qD is not a template function", dname); | |
1895 | fns = error_mark_node; | |
1896 | } | |
2f54a1db | 1897 | } |
386b8a85 | 1898 | |
2f54a1db | 1899 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
1900 | } |
1901 | ||
f2e48b67 BK |
1902 | if (declarator == error_mark_node) |
1903 | return error_mark_node; | |
1904 | ||
75650646 MM |
1905 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1906 | { | |
8ec2ac34 | 1907 | if (!explicit_instantiation) |
0e339752 | 1908 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
1909 | but the error will already have been flagged by |
1910 | check_specialization_scope. */ | |
1911 | return error_mark_node; | |
8ec2ac34 | 1912 | else |
b370501f | 1913 | { |
0e339752 | 1914 | /* It's not valid to write an explicit instantiation in |
b370501f | 1915 | class scope, e.g.: |
8ec2ac34 | 1916 | |
b370501f | 1917 | class C { template void f(); } |
8ec2ac34 | 1918 | |
b370501f KG |
1919 | This case is caught by the parser. However, on |
1920 | something like: | |
8ec2ac34 | 1921 | |
b370501f | 1922 | template class C { void f(); }; |
8ec2ac34 | 1923 | |
0e339752 | 1924 | (which is invalid) we can get here. The error will be |
b370501f KG |
1925 | issued later. */ |
1926 | ; | |
1927 | } | |
8ec2ac34 | 1928 | |
e1467ff2 | 1929 | return decl; |
75650646 MM |
1930 | } |
1931 | else if (ctype != NULL_TREE | |
1932 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == | |
1933 | IDENTIFIER_NODE)) | |
386b8a85 | 1934 | { |
75650646 MM |
1935 | /* Find the list of functions in ctype that have the same |
1936 | name as the declared function. */ | |
1937 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
1938 | tree fns = NULL_TREE; |
1939 | int idx; | |
1940 | ||
8ba658ee | 1941 | if (constructor_name_p (name, ctype)) |
386b8a85 | 1942 | { |
75650646 MM |
1943 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
1944 | ||
1945 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) | |
9f4faeae | 1946 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
75650646 MM |
1947 | { |
1948 | /* From [temp.expl.spec]: | |
e1467ff2 | 1949 | |
75650646 MM |
1950 | If such an explicit specialization for the member |
1951 | of a class template names an implicitly-declared | |
1952 | special member function (clause _special_), the | |
e1467ff2 MM |
1953 | program is ill-formed. |
1954 | ||
1955 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 1956 | error ("specialization of implicitly-declared special member function"); |
03017874 | 1957 | return error_mark_node; |
75650646 | 1958 | } |
386b8a85 | 1959 | |
42da2fd8 | 1960 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 1961 | } |
42da2fd8 | 1962 | |
421844e7 | 1963 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
1964 | { |
1965 | idx = lookup_fnfields_1 (ctype, name); | |
1966 | if (idx >= 0) | |
aaaa46d2 | 1967 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
1968 | } |
1969 | else | |
1970 | { | |
aaaa46d2 MM |
1971 | VEC(tree) *methods; |
1972 | tree ovl; | |
03017874 MM |
1973 | |
1974 | /* For a type-conversion operator, we cannot do a | |
1975 | name-based lookup. We might be looking for `operator | |
1976 | int' which will be a specialization of `operator T'. | |
1977 | So, we find *all* the conversion operators, and then | |
1978 | select from them. */ | |
1979 | fns = NULL_TREE; | |
1980 | ||
1981 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
1982 | if (methods) | |
5dd236e2 | 1983 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 1984 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 1985 | ++idx) |
03017874 | 1986 | { |
aaaa46d2 | 1987 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
1988 | /* There are no more conversion functions. */ |
1989 | break; | |
1990 | ||
1991 | /* Glue all these conversion functions together | |
1992 | with those we already have. */ | |
1993 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
1994 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
1995 | } | |
1996 | } | |
1997 | ||
386b8a85 JM |
1998 | if (fns == NULL_TREE) |
1999 | { | |
0f51ccfc | 2000 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 2001 | return error_mark_node; |
386b8a85 JM |
2002 | } |
2003 | else | |
2004 | TREE_OPERAND (declarator, 0) = fns; | |
2005 | } | |
75650646 | 2006 | |
e1467ff2 MM |
2007 | /* Figure out what exactly is being specialized at this point. |
2008 | Note that for an explicit instantiation, even one for a | |
38e01259 | 2009 | member function, we cannot tell apriori whether the |
e1467ff2 | 2010 | instantiation is for a member template, or just a member |
36a117a5 MM |
2011 | function of a template class. Even if a member template is |
2012 | being instantiated, the member template arguments may be | |
2013 | elided if they can be deduced from the rest of the | |
2014 | declaration. */ | |
e1467ff2 MM |
2015 | tmpl = determine_specialization (declarator, decl, |
2016 | &targs, | |
5fe7b654 GB |
2017 | member_specialization, |
2018 | template_count); | |
386b8a85 | 2019 | |
03017874 MM |
2020 | if (!tmpl || tmpl == error_mark_node) |
2021 | /* We couldn't figure out what this declaration was | |
2022 | specializing. */ | |
2023 | return error_mark_node; | |
2024 | else | |
386b8a85 | 2025 | { |
25aab5d0 | 2026 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2027 | |
e1467ff2 MM |
2028 | if (explicit_instantiation) |
2029 | { | |
03d0f4af | 2030 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
25aab5d0 MM |
2031 | is done by do_decl_instantiation later. */ |
2032 | ||
2033 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2034 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2035 | ||
2036 | if (arg_depth > parm_depth) | |
2037 | { | |
2038 | /* If TMPL is not the most general template (for | |
2039 | example, if TMPL is a friend template that is | |
2040 | injected into namespace scope), then there will | |
dc957d14 | 2041 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2042 | here. */ |
2043 | int i; | |
2044 | tree new_targs; | |
2045 | ||
f31c0a32 | 2046 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2047 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2048 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2049 | = TREE_VEC_ELT (targs, i); | |
2050 | targs = new_targs; | |
2051 | } | |
2052 | ||
3e4a3562 | 2053 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2054 | } |
74b846e0 | 2055 | |
6c07f448 KL |
2056 | /* If we thought that the DECL was a member function, but it |
2057 | turns out to be specializing a static member function, | |
4546865e | 2058 | make DECL a static member function as well. */ |
6c07f448 KL |
2059 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2060 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2061 | revert_static_member_fn (decl); |
6c07f448 | 2062 | |
f9a7ae04 MM |
2063 | /* If this is a specialization of a member template of a |
2064 | template class. In we want to return the TEMPLATE_DECL, | |
2065 | not the specialization of it. */ | |
74b846e0 MM |
2066 | if (tsk == tsk_template) |
2067 | { | |
2068 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
f9a7ae04 | 2069 | DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; |
b190f239 NS |
2070 | if (have_def) |
2071 | { | |
f31686a3 RH |
2072 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
2073 | DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)) | |
2074 | = DECL_SOURCE_LOCATION (decl); | |
08167d1c AO |
2075 | /* We want to use the argument list specified in the |
2076 | definition, not in the original declaration. */ | |
2077 | DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl)) | |
2078 | = DECL_ARGUMENTS (decl); | |
b190f239 | 2079 | } |
74b846e0 MM |
2080 | return tmpl; |
2081 | } | |
2082 | ||
36a117a5 | 2083 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 2084 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 2085 | |
8afa707f MM |
2086 | /* Inherit default function arguments from the template |
2087 | DECL is specializing. */ | |
2088 | copy_default_args_to_explicit_spec (decl); | |
2089 | ||
c750255c MM |
2090 | /* This specialization has the same protection as the |
2091 | template it specializes. */ | |
2092 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2093 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
2094 | ||
386b8a85 | 2095 | if (is_friend && !have_def) |
36a117a5 MM |
2096 | /* This is not really a declaration of a specialization. |
2097 | It's just the name of an instantiation. But, it's not | |
2098 | a request for an instantiation, either. */ | |
fbf1c34b | 2099 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2100 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2101 | /* This is indeed a specialization. In case of constructors | |
2102 | and destructors, we need in-charge and not-in-charge | |
2103 | versions in V3 ABI. */ | |
2104 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2105 | |
36a117a5 MM |
2106 | /* Register this specialization so that we can find it |
2107 | again. */ | |
2108 | decl = register_specialization (decl, gen_tmpl, targs); | |
386b8a85 JM |
2109 | } |
2110 | } | |
75650646 | 2111 | |
e1467ff2 | 2112 | return decl; |
386b8a85 | 2113 | } |
75650646 | 2114 | |
75650646 MM |
2115 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2116 | parameters. These are represented in the same format used for | |
2117 | DECL_TEMPLATE_PARMS. */ | |
2118 | ||
50bc768d NS |
2119 | int |
2120 | comp_template_parms (tree parms1, tree parms2) | |
75650646 MM |
2121 | { |
2122 | tree p1; | |
2123 | tree p2; | |
2124 | ||
2125 | if (parms1 == parms2) | |
2126 | return 1; | |
2127 | ||
2128 | for (p1 = parms1, p2 = parms2; | |
2129 | p1 != NULL_TREE && p2 != NULL_TREE; | |
2130 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2131 | { | |
2132 | tree t1 = TREE_VALUE (p1); | |
2133 | tree t2 = TREE_VALUE (p2); | |
2134 | int i; | |
2135 | ||
50bc768d NS |
2136 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2137 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2138 | |
2139 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2140 | return 0; | |
2141 | ||
2142 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) | |
2143 | { | |
2144 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); | |
2145 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
2146 | ||
2147 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2148 | return 0; | |
2149 | ||
2150 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
2151 | continue; | |
3bfdc719 | 2152 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2153 | return 0; |
2154 | } | |
2155 | } | |
2156 | ||
2157 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2158 | /* One set of parameters has more parameters lists than the | |
2159 | other. */ | |
2160 | return 0; | |
2161 | ||
2162 | return 1; | |
2163 | } | |
2164 | ||
f3400fe2 JM |
2165 | /* Complain if DECL shadows a template parameter. |
2166 | ||
2167 | [temp.local]: A template-parameter shall not be redeclared within its | |
2168 | scope (including nested scopes). */ | |
2169 | ||
2170 | void | |
3a978d72 | 2171 | check_template_shadow (tree decl) |
f3400fe2 | 2172 | { |
8f032717 MM |
2173 | tree olddecl; |
2174 | ||
b5d9b9ab MM |
2175 | /* If we're not in a template, we can't possibly shadow a template |
2176 | parameter. */ | |
2177 | if (!current_template_parms) | |
2178 | return; | |
2179 | ||
2180 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
2181 | if (TREE_CODE (decl) == OVERLOAD) |
2182 | decl = OVL_CURRENT (decl); | |
90ea9897 | 2183 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 2184 | |
b5d9b9ab MM |
2185 | /* If there's no previous binding for this name, we're not shadowing |
2186 | anything, let alone a template parameter. */ | |
2187 | if (!olddecl) | |
2188 | return; | |
2189 | ||
2190 | /* If we're not shadowing a template parameter, we're done. Note | |
2191 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
2192 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
2193 | node. */ | |
2f939d94 | 2194 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
b5d9b9ab MM |
2195 | return; |
2196 | ||
2197 | /* We check for decl != olddecl to avoid bogus errors for using a | |
2198 | name inside a class. We check TPFI to avoid duplicate errors for | |
2199 | inline member templates. */ | |
2200 | if (decl == olddecl | |
2201 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) | |
2202 | return; | |
2203 | ||
0f51ccfc GDR |
2204 | cp_error_at ("declaration of %q#D", decl); |
2205 | cp_error_at (" shadows template parm %q#D", olddecl); | |
f3400fe2 | 2206 | } |
22a7be53 | 2207 | |
f3400fe2 | 2208 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
2209 | ORIG_LEVEL, DECL, and TYPE. */ |
2210 | ||
2211 | static tree | |
3a978d72 NN |
2212 | build_template_parm_index (int index, |
2213 | int level, | |
2214 | int orig_level, | |
2215 | tree decl, | |
2216 | tree type) | |
f84b4be9 JM |
2217 | { |
2218 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
2219 | TEMPLATE_PARM_IDX (t) = index; | |
2220 | TEMPLATE_PARM_LEVEL (t) = level; | |
2221 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
2222 | TEMPLATE_PARM_DECL (t) = decl; | |
2223 | TREE_TYPE (t) = type; | |
3e4a3562 | 2224 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
6de9cd9a | 2225 | TREE_INVARIANT (t) = TREE_INVARIANT (decl); |
3e4a3562 | 2226 | TREE_READONLY (t) = TREE_READONLY (decl); |
f84b4be9 JM |
2227 | |
2228 | return t; | |
2229 | } | |
2230 | ||
f84b4be9 | 2231 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 2232 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
2233 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
2234 | new one is created. */ | |
2235 | ||
2236 | static tree | |
3a978d72 | 2237 | reduce_template_parm_level (tree index, tree type, int levels) |
f84b4be9 JM |
2238 | { |
2239 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
2240 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 2241 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 | 2242 | { |
3e4a3562 NS |
2243 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
2244 | tree decl, t; | |
2245 | ||
2246 | decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); | |
2247 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); | |
6de9cd9a | 2248 | TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl); |
3e4a3562 NS |
2249 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); |
2250 | DECL_ARTIFICIAL (decl) = 1; | |
2251 | SET_DECL_TEMPLATE_PARM_P (decl); | |
2252 | ||
2253 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), | |
93cdc044 | 2254 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
2255 | TEMPLATE_PARM_ORIG_LEVEL (index), |
2256 | decl, type); | |
2257 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 JM |
2258 | |
2259 | /* Template template parameters need this. */ | |
2260 | DECL_TEMPLATE_PARMS (decl) | |
2261 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
2262 | } |
2263 | ||
2264 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2265 | } | |
2266 | ||
8d08fdba | 2267 | /* Process information from new template parameter NEXT and append it to the |
058b15c1 MM |
2268 | LIST being built. This new parameter is a non-type parameter iff |
2269 | IS_NON_TYPE is true. */ | |
e92cc029 | 2270 | |
8d08fdba | 2271 | tree |
058b15c1 | 2272 | process_template_parm (tree list, tree next, bool is_non_type) |
8d08fdba MS |
2273 | { |
2274 | tree parm; | |
2275 | tree decl = 0; | |
a292b002 | 2276 | tree defval; |
058b15c1 | 2277 | int idx; |
f84b4be9 | 2278 | |
8d08fdba | 2279 | parm = next; |
50bc768d | 2280 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 2281 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
2282 | |
2283 | if (list) | |
2284 | { | |
2285 | tree p = TREE_VALUE (tree_last (list)); | |
2286 | ||
1899c3a4 | 2287 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) |
5566b478 MS |
2288 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); |
2289 | else | |
f84b4be9 | 2290 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
5566b478 MS |
2291 | ++idx; |
2292 | } | |
2293 | else | |
2294 | idx = 0; | |
2295 | ||
058b15c1 | 2296 | if (is_non_type) |
8d08fdba | 2297 | { |
058b15c1 MM |
2298 | parm = TREE_VALUE (parm); |
2299 | ||
833aa4c4 | 2300 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d MM |
2301 | |
2302 | /* [temp.param] | |
2303 | ||
2304 | The top-level cv-qualifiers on the template-parameter are | |
2305 | ignored when determining its type. */ | |
2306 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
2307 | ||
8d08fdba | 2308 | /* A template parameter is not modifiable. */ |
6de9cd9a DN |
2309 | TREE_CONSTANT (parm) = 1; |
2310 | TREE_INVARIANT (parm) = 1; | |
2311 | TREE_READONLY (parm) = 1; | |
11b810f1 | 2312 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) |
db3f4e4e | 2313 | TREE_TYPE (parm) = void_type_node; |
8d08fdba | 2314 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
6de9cd9a DN |
2315 | TREE_CONSTANT (decl) = 1; |
2316 | TREE_INVARIANT (decl) = 1; | |
2317 | TREE_READONLY (decl) = 1; | |
f84b4be9 JM |
2318 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
2319 | = build_template_parm_index (idx, processing_template_decl, | |
2320 | processing_template_decl, | |
2321 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
2322 | } |
2323 | else | |
2324 | { | |
73b0fce8 | 2325 | tree t; |
058b15c1 | 2326 | parm = TREE_VALUE (TREE_VALUE (parm)); |
73b0fce8 KL |
2327 | |
2328 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) | |
2329 | { | |
33848bb0 | 2330 | t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); |
73b0fce8 KL |
2331 | /* This is for distinguishing between real templates and template |
2332 | template parameters */ | |
2333 | TREE_TYPE (parm) = t; | |
2334 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
2335 | decl = parm; | |
2336 | } | |
2337 | else | |
2338 | { | |
33848bb0 | 2339 | t = make_aggr_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 2340 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
73b0fce8 KL |
2341 | decl = build_decl (TYPE_DECL, parm, t); |
2342 | } | |
2343 | ||
d2e5ee5c MS |
2344 | TYPE_NAME (t) = decl; |
2345 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 2346 | parm = decl; |
f84b4be9 JM |
2347 | TEMPLATE_TYPE_PARM_INDEX (t) |
2348 | = build_template_parm_index (idx, processing_template_decl, | |
2349 | processing_template_decl, | |
2350 | decl, TREE_TYPE (parm)); | |
8d08fdba | 2351 | } |
c727aa5e | 2352 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 2353 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 2354 | pushdecl (decl); |
a292b002 | 2355 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
2356 | return chainon (list, parm); |
2357 | } | |
2358 | ||
2359 | /* The end of a template parameter list has been reached. Process the | |
2360 | tree list into a parameter vector, converting each parameter into a more | |
2361 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
2362 | as PARM_DECLs. */ | |
2363 | ||
2364 | tree | |
3a978d72 | 2365 | end_template_parm_list (tree parms) |
8d08fdba | 2366 | { |
5566b478 | 2367 | int nparms; |
9471d3e2 | 2368 | tree parm, next; |
5566b478 MS |
2369 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
2370 | ||
5566b478 | 2371 | current_template_parms |
4890c2f4 | 2372 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 2373 | saved_parmlist, current_template_parms); |
8d08fdba | 2374 | |
9471d3e2 NS |
2375 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
2376 | { | |
2377 | next = TREE_CHAIN (parm); | |
2378 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
2379 | TREE_CHAIN (parm) = NULL_TREE; | |
2380 | } | |
a292b002 | 2381 | |
67ffc812 MM |
2382 | --processing_template_parmlist; |
2383 | ||
8d08fdba MS |
2384 | return saved_parmlist; |
2385 | } | |
2386 | ||
5566b478 MS |
2387 | /* end_template_decl is called after a template declaration is seen. */ |
2388 | ||
8d08fdba | 2389 | void |
3a978d72 | 2390 | end_template_decl (void) |
8d08fdba | 2391 | { |
386b8a85 JM |
2392 | reset_specialization (); |
2393 | ||
5156628f | 2394 | if (! processing_template_decl) |
73aad9b9 JM |
2395 | return; |
2396 | ||
5566b478 | 2397 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 2398 | finish_scope (); |
8d08fdba | 2399 | |
5566b478 MS |
2400 | --processing_template_decl; |
2401 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 2402 | } |
8d08fdba | 2403 | |
36a117a5 MM |
2404 | /* Given a template argument vector containing the template PARMS. |
2405 | The innermost PARMS are given first. */ | |
9a3b49ac MS |
2406 | |
2407 | tree | |
3a978d72 | 2408 | current_template_args (void) |
5566b478 | 2409 | { |
36a117a5 | 2410 | tree header; |
b370501f | 2411 | tree args = NULL_TREE; |
36a117a5 | 2412 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
2413 | int l = length; |
2414 | ||
36a117a5 MM |
2415 | /* If there is only one level of template parameters, we do not |
2416 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
2417 | TREE_VEC containing the arguments. */ | |
2418 | if (length > 1) | |
2419 | args = make_tree_vec (length); | |
2420 | ||
2421 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 2422 | { |
5566b478 | 2423 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
2424 | int i; |
2425 | ||
5566b478 | 2426 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 2427 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 2428 | { |
98c1c668 JM |
2429 | tree t = TREE_VEC_ELT (a, i); |
2430 | ||
36a117a5 | 2431 | /* T will be a list if we are called from within a |
98c1c668 JM |
2432 | begin/end_template_parm_list pair, but a vector directly |
2433 | if within a begin/end_member_template_processing pair. */ | |
2434 | if (TREE_CODE (t) == TREE_LIST) | |
2435 | { | |
2436 | t = TREE_VALUE (t); | |
2437 | ||
73b0fce8 KL |
2438 | if (TREE_CODE (t) == TYPE_DECL |
2439 | || TREE_CODE (t) == TEMPLATE_DECL) | |
98c1c668 JM |
2440 | t = TREE_TYPE (t); |
2441 | else | |
2442 | t = DECL_INITIAL (t); | |
36a117a5 | 2443 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 2444 | } |
5566b478 | 2445 | } |
36a117a5 MM |
2446 | |
2447 | if (length > 1) | |
2448 | TREE_VEC_ELT (args, --l) = a; | |
2449 | else | |
2450 | args = a; | |
8d08fdba MS |
2451 | } |
2452 | ||
9a3b49ac MS |
2453 | return args; |
2454 | } | |
75650646 | 2455 | |
e1467ff2 | 2456 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 2457 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 2458 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 2459 | |
75650646 | 2460 | static tree |
c7222c02 | 2461 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
2462 | { |
2463 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
2464 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
2465 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 2466 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
2467 | if (DECL_LANG_SPECIFIC (decl)) |
2468 | { | |
8f032717 MM |
2469 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
2470 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
c8460010 | 2471 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); |
11f98788 | 2472 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
1f6e1acc AS |
2473 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); |
2474 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
2475 | SET_OVERLOADED_OPERATOR_CODE (tmpl, | |
2476 | DECL_OVERLOADED_OPERATOR_P (decl)); | |
75650646 MM |
2477 | } |
2478 | ||
2479 | return tmpl; | |
2480 | } | |
2481 | ||
050367a3 MM |
2482 | struct template_parm_data |
2483 | { | |
6c30752f MM |
2484 | /* The level of the template parameters we are currently |
2485 | processing. */ | |
050367a3 | 2486 | int level; |
6c30752f MM |
2487 | |
2488 | /* The index of the specialization argument we are currently | |
2489 | processing. */ | |
2490 | int current_arg; | |
2491 | ||
2492 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 2493 | elements are nonzero if the parameter has been used in any one |
6c30752f | 2494 | of the arguments processed so far. */ |
050367a3 | 2495 | int* parms; |
6c30752f MM |
2496 | |
2497 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 2498 | elements are nonzero if the argument makes use of template |
6c30752f MM |
2499 | parameters of this level. */ |
2500 | int* arg_uses_template_parms; | |
050367a3 MM |
2501 | }; |
2502 | ||
2503 | /* Subroutine of push_template_decl used to see if each template | |
2504 | parameter in a partial specialization is used in the explicit | |
2505 | argument list. If T is of the LEVEL given in DATA (which is | |
2506 | treated as a template_parm_data*), then DATA->PARMS is marked | |
2507 | appropriately. */ | |
2508 | ||
2509 | static int | |
3a978d72 | 2510 | mark_template_parm (tree t, void* data) |
050367a3 MM |
2511 | { |
2512 | int level; | |
2513 | int idx; | |
2514 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
2515 | ||
2516 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2517 | { | |
2518 | level = TEMPLATE_PARM_LEVEL (t); | |
2519 | idx = TEMPLATE_PARM_IDX (t); | |
2520 | } | |
2521 | else | |
2522 | { | |
2523 | level = TEMPLATE_TYPE_LEVEL (t); | |
2524 | idx = TEMPLATE_TYPE_IDX (t); | |
2525 | } | |
2526 | ||
2527 | if (level == tpd->level) | |
6c30752f MM |
2528 | { |
2529 | tpd->parms[idx] = 1; | |
2530 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
2531 | } | |
050367a3 MM |
2532 | |
2533 | /* Return zero so that for_each_template_parm will continue the | |
2534 | traversal of the tree; we want to mark *every* template parm. */ | |
2535 | return 0; | |
2536 | } | |
2537 | ||
6c30752f MM |
2538 | /* Process the partial specialization DECL. */ |
2539 | ||
e9659ab0 | 2540 | static tree |
3a978d72 | 2541 | process_partial_specialization (tree decl) |
6c30752f MM |
2542 | { |
2543 | tree type = TREE_TYPE (decl); | |
2544 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
2545 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 2546 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f MM |
2547 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
2548 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
2549 | int nargs = TREE_VEC_LENGTH (inner_args); | |
2550 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2551 | int i; | |
2552 | int did_error_intro = 0; | |
6c30752f MM |
2553 | struct template_parm_data tpd; |
2554 | struct template_parm_data tpd2; | |
2555 | ||
6c30752f MM |
2556 | /* We check that each of the template parameters given in the |
2557 | partial specialization is used in the argument list to the | |
2558 | specialization. For example: | |
2559 | ||
2560 | template <class T> struct S; | |
2561 | template <class T> struct S<T*>; | |
2562 | ||
2563 | The second declaration is OK because `T*' uses the template | |
2564 | parameter T, whereas | |
2565 | ||
2566 | template <class T> struct S<int>; | |
2567 | ||
2568 | is no good. Even trickier is: | |
2569 | ||
2570 | template <class T> | |
2571 | struct S1 | |
2572 | { | |
2573 | template <class U> | |
2574 | struct S2; | |
2575 | template <class U> | |
2576 | struct S2<T>; | |
2577 | }; | |
2578 | ||
0e339752 | 2579 | The S2<T> declaration is actually invalid; it is a |
6c30752f MM |
2580 | full-specialization. Of course, |
2581 | ||
2582 | template <class U> | |
2583 | struct S2<T (*)(U)>; | |
2584 | ||
2585 | or some such would have been OK. */ | |
2586 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
2587 | tpd.parms = alloca (sizeof (int) * ntparms); | |
fad205ff | 2588 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f MM |
2589 | |
2590 | tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); | |
fad205ff | 2591 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
2592 | for (i = 0; i < nargs; ++i) |
2593 | { | |
2594 | tpd.current_arg = i; | |
2595 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2596 | &mark_template_parm, | |
4f2c9d7e MM |
2597 | &tpd, |
2598 | NULL); | |
6c30752f MM |
2599 | } |
2600 | for (i = 0; i < ntparms; ++i) | |
2601 | if (tpd.parms[i] == 0) | |
2602 | { | |
2603 | /* One of the template parms was not used in the | |
f9a7ae04 | 2604 | specialization. */ |
6c30752f MM |
2605 | if (!did_error_intro) |
2606 | { | |
33bd39a2 | 2607 | error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2608 | did_error_intro = 1; |
2609 | } | |
2610 | ||
0f51ccfc | 2611 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
2612 | } |
2613 | ||
2614 | /* [temp.class.spec] | |
2615 | ||
2616 | The argument list of the specialization shall not be identical to | |
2617 | the implicit argument list of the primary template. */ | |
f9a7ae04 MM |
2618 | if (comp_template_args |
2619 | (inner_args, | |
2620 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE | |
2621 | (maintmpl))))) | |
0f51ccfc | 2622 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
2623 | |
2624 | /* [temp.class.spec] | |
2625 | ||
2626 | A partially specialized non-type argument expression shall not | |
2627 | involve template parameters of the partial specialization except | |
2628 | when the argument expression is a simple identifier. | |
2629 | ||
2630 | The type of a template parameter corresponding to a specialized | |
2631 | non-type argument shall not be dependent on a parameter of the | |
2632 | specialization. */ | |
50bc768d | 2633 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
2634 | tpd2.parms = 0; |
2635 | for (i = 0; i < nargs; ++i) | |
2636 | { | |
2637 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2638 | if (/* These first two lines are the `non-type' bit. */ | |
2f939d94 | 2639 | !TYPE_P (arg) |
6c30752f MM |
2640 | && TREE_CODE (arg) != TEMPLATE_DECL |
2641 | /* This next line is the `argument expression is not just a | |
2642 | simple identifier' condition and also the `specialized | |
2643 | non-type argument' bit. */ | |
2644 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2645 | { | |
2646 | if (tpd.arg_uses_template_parms[i]) | |
0f51ccfc | 2647 | error ("template argument %qE involves template parameter(s)", arg); |
6c30752f MM |
2648 | else |
2649 | { | |
2650 | /* Look at the corresponding template parameter, | |
2651 | marking which template parameters its type depends | |
2652 | upon. */ | |
2653 | tree type = | |
2654 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
2655 | i))); | |
2656 | ||
2657 | if (!tpd2.parms) | |
2658 | { | |
2659 | /* We haven't yet initialized TPD2. Do so now. */ | |
2660 | tpd2.arg_uses_template_parms | |
c68b0a84 | 2661 | = alloca (sizeof (int) * nargs); |
104bf76a | 2662 | /* The number of parameters here is the number in the |
76a83782 RH |
2663 | main template, which, as checked in the assertion |
2664 | above, is NARGS. */ | |
c68b0a84 | 2665 | tpd2.parms = alloca (sizeof (int) * nargs); |
6c30752f MM |
2666 | tpd2.level = |
2667 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); | |
2668 | } | |
2669 | ||
104bf76a | 2670 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2671 | looking for the template parameters of the main |
2672 | template, not in the specialization. */ | |
2673 | tpd2.current_arg = i; | |
2674 | tpd2.arg_uses_template_parms[i] = 0; | |
fad205ff | 2675 | memset (tpd2.parms, 0, sizeof (int) * nargs); |
6c30752f MM |
2676 | for_each_template_parm (type, |
2677 | &mark_template_parm, | |
4f2c9d7e MM |
2678 | &tpd2, |
2679 | NULL); | |
6c30752f MM |
2680 | |
2681 | if (tpd2.arg_uses_template_parms [i]) | |
2682 | { | |
2683 | /* The type depended on some template parameters. | |
2684 | If they are fully specialized in the | |
2685 | specialization, that's OK. */ | |
2686 | int j; | |
2687 | for (j = 0; j < nargs; ++j) | |
2688 | if (tpd2.parms[j] != 0 | |
2689 | && tpd.arg_uses_template_parms [j]) | |
2690 | { | |
0f51ccfc GDR |
2691 | error ("type %qT of template argument %qE depends " |
2692 | "on template parameter(s)", | |
2693 | type, | |
2694 | arg); | |
6c30752f MM |
2695 | break; |
2696 | } | |
2697 | } | |
2698 | } | |
2699 | } | |
2700 | } | |
2701 | ||
c7222c02 MM |
2702 | if (retrieve_specialization (maintmpl, specargs, |
2703 | /*class_specializations_p=*/true)) | |
6c30752f MM |
2704 | /* We've already got this specialization. */ |
2705 | return decl; | |
2706 | ||
d8b64f80 | 2707 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
e1b3e07d MM |
2708 | = tree_cons (inner_args, inner_parms, |
2709 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); | |
6c30752f MM |
2710 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
2711 | return decl; | |
2712 | } | |
2713 | ||
6ba89f8e MM |
2714 | /* Check that a template declaration's use of default arguments is not |
2715 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
838dfd8a KH |
2716 | nonzero if DECL is the thing declared by a primary template. |
2717 | IS_PARTIAL is nonzero if DECL is a partial specialization. */ | |
6ba89f8e MM |
2718 | |
2719 | static void | |
3a978d72 | 2720 | check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial) |
6ba89f8e | 2721 | { |
d8e178a0 | 2722 | const char *msg; |
66191c20 MM |
2723 | int last_level_to_check; |
2724 | tree parm_level; | |
6ba89f8e MM |
2725 | |
2726 | /* [temp.param] | |
2727 | ||
2728 | A default template-argument shall not be specified in a | |
2729 | function template declaration or a function template definition, nor | |
2730 | in the template-parameter-list of the definition of a member of a | |
2731 | class template. */ | |
2732 | ||
4f1c5b7d | 2733 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
2734 | /* You can't have a function template declaration in a local |
2735 | scope, nor you can you define a member of a class template in a | |
2736 | local scope. */ | |
2737 | return; | |
2738 | ||
6ba89f8e MM |
2739 | if (current_class_type |
2740 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 2741 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
2742 | /* If this is either a friend defined in the scope of the class |
2743 | or a member function. */ | |
6df5158a NS |
2744 | && (DECL_FUNCTION_MEMBER_P (decl) |
2745 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
2746 | : DECL_FRIEND_CONTEXT (decl) | |
2747 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
2748 | : false) | |
5937a6f9 MM |
2749 | /* And, if it was a member function, it really was defined in |
2750 | the scope of the class. */ | |
6df5158a NS |
2751 | && (!DECL_FUNCTION_MEMBER_P (decl) |
2752 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 2753 | /* We already checked these parameters when the template was |
5937a6f9 MM |
2754 | declared, so there's no need to do it again now. This function |
2755 | was defined in class scope, but we're processing it's body now | |
2756 | that the class is complete. */ | |
6ba89f8e MM |
2757 | return; |
2758 | ||
66191c20 MM |
2759 | /* [temp.param] |
2760 | ||
2761 | If a template-parameter has a default template-argument, all | |
2762 | subsequent template-parameters shall have a default | |
2763 | template-argument supplied. */ | |
2764 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
2765 | { | |
2766 | tree inner_parms = TREE_VALUE (parm_level); | |
2767 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2768 | int seen_def_arg_p = 0; | |
2769 | int i; | |
2770 | ||
2771 | for (i = 0; i < ntparms; ++i) | |
2772 | { | |
2773 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
2774 | if (TREE_PURPOSE (parm)) | |
2775 | seen_def_arg_p = 1; | |
2776 | else if (seen_def_arg_p) | |
2777 | { | |
0f51ccfc | 2778 | error ("no default argument for %qD", TREE_VALUE (parm)); |
66191c20 MM |
2779 | /* For better subsequent error-recovery, we indicate that |
2780 | there should have been a default argument. */ | |
2781 | TREE_PURPOSE (parm) = error_mark_node; | |
2782 | } | |
2783 | } | |
2784 | } | |
2785 | ||
6ba89f8e MM |
2786 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) |
2787 | /* For an ordinary class template, default template arguments are | |
2788 | allowed at the innermost level, e.g.: | |
2789 | template <class T = int> | |
2790 | struct S {}; | |
2791 | but, in a partial specialization, they're not allowed even | |
2792 | there, as we have in [temp.class.spec]: | |
2793 | ||
2794 | The template parameter list of a specialization shall not | |
2795 | contain default template argument values. | |
2796 | ||
2797 | So, for a partial specialization, or for a function template, | |
2798 | we look at all of them. */ | |
2799 | ; | |
2800 | else | |
2801 | /* But, for a primary class template that is not a partial | |
2802 | specialization we look at all template parameters except the | |
2803 | innermost ones. */ | |
2804 | parms = TREE_CHAIN (parms); | |
2805 | ||
2806 | /* Figure out what error message to issue. */ | |
2807 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
8653a2c3 | 2808 | msg = "default template arguments may not be used in function templates"; |
6ba89f8e | 2809 | else if (is_partial) |
8653a2c3 | 2810 | msg = "default template arguments may not be used in partial specializations"; |
6ba89f8e | 2811 | else |
0f51ccfc | 2812 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
2813 | |
2814 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2815 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2816 | examine the parameters to the class itself. On the one |
6ba89f8e | 2817 | hand, they will be checked when the class is defined, and, |
0e339752 | 2818 | on the other, default arguments are valid in things like: |
6ba89f8e MM |
2819 | template <class T = double> |
2820 | struct S { template <class U> void f(U); }; | |
2821 | Here the default argument for `S' has no bearing on the | |
2822 | declaration of `f'. */ | |
2823 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2824 | else | |
2825 | /* Check everything. */ | |
2826 | last_level_to_check = 0; | |
2827 | ||
66191c20 MM |
2828 | for (parm_level = parms; |
2829 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
2830 | parm_level = TREE_CHAIN (parm_level)) | |
6ba89f8e | 2831 | { |
66191c20 MM |
2832 | tree inner_parms = TREE_VALUE (parm_level); |
2833 | int i; | |
2834 | int ntparms; | |
6ba89f8e MM |
2835 | |
2836 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
2837 | for (i = 0; i < ntparms; ++i) | |
2838 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) | |
2839 | { | |
2840 | if (msg) | |
2841 | { | |
33bd39a2 | 2842 | error (msg, decl); |
6ba89f8e MM |
2843 | msg = 0; |
2844 | } | |
2845 | ||
2846 | /* Clear out the default argument so that we are not | |
2847 | confused later. */ | |
2848 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2849 | } | |
2850 | ||
2851 | /* At this point, if we're still interested in issuing messages, | |
2852 | they must apply to classes surrounding the object declared. */ | |
2853 | if (msg) | |
0f51ccfc | 2854 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
2855 | } |
2856 | } | |
2857 | ||
5dd236e2 NS |
2858 | /* Worker for push_template_decl_real, called via |
2859 | for_each_template_parm. DATA is really an int, indicating the | |
2860 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 2861 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
2862 | |
2863 | static int | |
3a978d72 | 2864 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 2865 | { |
6e04241f | 2866 | int this_level = *(int *)data; |
5dd236e2 NS |
2867 | int level; |
2868 | ||
2869 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2870 | level = TEMPLATE_PARM_LEVEL (t); | |
2871 | else | |
2872 | level = TEMPLATE_TYPE_LEVEL (t); | |
2873 | return level == this_level; | |
2874 | } | |
2875 | ||
3ac3d9ea | 2876 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
2877 | parameters given by current_template_args, or reuses a |
2878 | previously existing one, if appropriate. Returns the DECL, or an | |
6757edfe MM |
2879 | equivalent one, if it is replaced via a call to duplicate_decls. |
2880 | ||
838dfd8a | 2881 | If IS_FRIEND is nonzero, DECL is a friend declaration. */ |
3ac3d9ea MM |
2882 | |
2883 | tree | |
3a978d72 | 2884 | push_template_decl_real (tree decl, int is_friend) |
9a3b49ac MS |
2885 | { |
2886 | tree tmpl; | |
f84b4be9 | 2887 | tree args; |
9a3b49ac | 2888 | tree info; |
f84b4be9 JM |
2889 | tree ctx; |
2890 | int primary; | |
6ba89f8e | 2891 | int is_partial; |
cfe507be | 2892 | int new_template_p = 0; |
c7222c02 MM |
2893 | /* True if the template is a member template, in the sense of |
2894 | [temp.mem]. */ | |
2895 | bool member_template_p = false; | |
6ba89f8e | 2896 | |
c0694c4b MM |
2897 | if (decl == error_mark_node) |
2898 | return decl; | |
2899 | ||
6ba89f8e | 2900 | /* See if this is a partial specialization. */ |
9188c363 | 2901 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 2902 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 2903 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe MM |
2904 | |
2905 | is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)); | |
f84b4be9 JM |
2906 | |
2907 | if (is_friend) | |
2908 | /* For a friend, we want the context of the friend function, not | |
2909 | the type of which it is a friend. */ | |
2910 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
2911 | else if (CP_DECL_CONTEXT (decl) |
2912 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
2913 | /* In the case of a virtual function, we want the class in which |
2914 | it is defined. */ | |
4f1c5b7d | 2915 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 2916 | else |
dc957d14 | 2917 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 2918 | is assumed to be a member of the class. */ |
9188c363 | 2919 | ctx = current_scope (); |
f84b4be9 | 2920 | |
2c73f9f5 ML |
2921 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
2922 | ctx = NULL_TREE; | |
2923 | ||
2924 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 2925 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 2926 | |
6ba89f8e | 2927 | /* See if this is a primary template. */ |
74b846e0 | 2928 | primary = template_parm_scope_p (); |
9a3b49ac | 2929 | |
83566abf JM |
2930 | if (primary) |
2931 | { | |
c7222c02 MM |
2932 | if (DECL_CLASS_SCOPE_P (decl)) |
2933 | member_template_p = true; | |
83566abf | 2934 | if (current_lang_name == lang_name_c) |
33bd39a2 | 2935 | error ("template with C linkage"); |
4ce3d537 MM |
2936 | else if (TREE_CODE (decl) == TYPE_DECL |
2937 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
33bd39a2 | 2938 | error ("template class without a name"); |
717a7d5d | 2939 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 2940 | { |
717a7d5d MM |
2941 | if (DECL_DESTRUCTOR_P (decl)) |
2942 | { | |
2943 | /* [temp.mem] | |
2944 | ||
2945 | A destructor shall not be a member template. */ | |
0f51ccfc | 2946 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
2947 | return error_mark_node; |
2948 | } | |
2949 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
2950 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
2951 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
2952 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
2953 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
2954 | == void_list_node))) | |
2955 | { | |
2956 | /* [basic.stc.dynamic.allocation] | |
2957 | ||
2958 | An allocation function can be a function | |
2959 | template. ... Template allocation functions shall | |
2960 | have two or more parameters. */ | |
0f51ccfc | 2961 | error ("invalid template declaration of %qD", decl); |
717a7d5d MM |
2962 | return decl; |
2963 | } | |
4b0d3cbe | 2964 | } |
2aaf816d JM |
2965 | else if ((DECL_IMPLICIT_TYPEDEF_P (decl) |
2966 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
717a7d5d | 2967 | || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))) |
2aaf816d JM |
2968 | /* OK */; |
2969 | else | |
11325dcd | 2970 | { |
0f51ccfc | 2971 | error ("template declaration of %q#D", decl); |
11325dcd KL |
2972 | return error_mark_node; |
2973 | } | |
83566abf JM |
2974 | } |
2975 | ||
6ba89f8e MM |
2976 | /* Check to see that the rules regarding the use of default |
2977 | arguments are not being violated. */ | |
2978 | check_default_tmpl_args (decl, current_template_parms, | |
2979 | primary, is_partial); | |
73aad9b9 | 2980 | |
6ba89f8e MM |
2981 | if (is_partial) |
2982 | return process_partial_specialization (decl); | |
d32789d8 | 2983 | |
9a3b49ac MS |
2984 | args = current_template_args (); |
2985 | ||
f84b4be9 JM |
2986 | if (!ctx |
2987 | || TREE_CODE (ctx) == FUNCTION_DECL | |
a723baf1 | 2988 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 2989 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 2990 | { |
75650646 | 2991 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
2992 | && DECL_TEMPLATE_INFO (decl) |
2993 | && DECL_TI_TEMPLATE (decl)) | |
2994 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
2995 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
2996 | be DECL_LANG_SPECIFIC. The information equivalent to | |
2997 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
2998 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) | |
2999 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
3000 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3001 | { | |
3002 | /* Since a template declaration already existed for this | |
3003 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 3004 | that the redeclaration is valid. */ |
1c10870d AS |
3005 | redeclare_class_template (TREE_TYPE (decl), |
3006 | current_template_parms); | |
3007 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
3008 | one we already had. */ | |
3009 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
3010 | } | |
f84b4be9 | 3011 | else |
786b5245 | 3012 | { |
c7222c02 MM |
3013 | tmpl = build_template_decl (decl, current_template_parms, |
3014 | member_template_p); | |
cfe507be MM |
3015 | new_template_p = 1; |
3016 | ||
f84b4be9 JM |
3017 | if (DECL_LANG_SPECIFIC (decl) |
3018 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
3019 | { | |
3020 | /* A specialization of a member template of a template | |
c6002625 | 3021 | class. */ |
f84b4be9 JM |
3022 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3023 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
3024 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
3025 | } | |
786b5245 | 3026 | } |
8d08fdba MS |
3027 | } |
3028 | else | |
3029 | { | |
e1a5ccf7 | 3030 | tree a, t, current, parms; |
ba4f4e5d | 3031 | int i; |
6633d636 | 3032 | |
6b9ab5cc MM |
3033 | if (TREE_CODE (decl) == TYPE_DECL) |
3034 | { | |
ed44da02 MM |
3035 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
3036 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
3037 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
3038 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3039 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
3040 | else |
3041 | { | |
0f51ccfc | 3042 | error ("%qD does not declare a template type", decl); |
6b9ab5cc MM |
3043 | return decl; |
3044 | } | |
3045 | } | |
f3368a90 | 3046 | else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 3047 | { |
0f51ccfc | 3048 | error ("template definition of non-template %q#D", decl); |
3ac3d9ea | 3049 | return decl; |
c91a56d2 | 3050 | } |
8d08fdba | 3051 | else |
5566b478 | 3052 | tmpl = DECL_TI_TEMPLATE (decl); |
98c1c668 | 3053 | |
c353b8e3 | 3054 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
e1a5ccf7 | 3055 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 3056 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 3057 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 3058 | { |
e1a5ccf7 JM |
3059 | tree new_tmpl; |
3060 | ||
3061 | /* The declaration is a specialization of a member | |
3062 | template, declared outside the class. Therefore, the | |
3063 | innermost template arguments will be NULL, so we | |
3064 | replace them with the arguments determined by the | |
3065 | earlier call to check_explicit_specialization. */ | |
3066 | args = DECL_TI_ARGS (decl); | |
3067 | ||
3068 | new_tmpl | |
c7222c02 MM |
3069 | = build_template_decl (decl, current_template_parms, |
3070 | member_template_p); | |
e1a5ccf7 JM |
3071 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
3072 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
3073 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
3074 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
e1b3e07d MM |
3075 | DECL_TEMPLATE_INFO (new_tmpl) |
3076 | = tree_cons (tmpl, args, NULL_TREE); | |
e1a5ccf7 | 3077 | |
f9a7ae04 MM |
3078 | register_specialization (new_tmpl, |
3079 | most_general_template (tmpl), | |
3080 | args); | |
e1a5ccf7 | 3081 | return decl; |
98c1c668 | 3082 | } |
98c1c668 | 3083 | |
e1a5ccf7 | 3084 | /* Make sure the template headers we got make sense. */ |
6633d636 | 3085 | |
e1a5ccf7 JM |
3086 | parms = DECL_TEMPLATE_PARMS (tmpl); |
3087 | i = TMPL_PARMS_DEPTH (parms); | |
3088 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 3089 | { |
0f51ccfc GDR |
3090 | error ("expected %d levels of template parms for %q#D, got %d", |
3091 | i, decl, TMPL_ARGS_DEPTH (args)); | |
98c1c668 | 3092 | } |
e1a5ccf7 JM |
3093 | else |
3094 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
3095 | { | |
3096 | a = TMPL_ARGS_LEVEL (args, i); | |
3097 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
3098 | ||
3099 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
3100 | { | |
3101 | if (current == decl) | |
0f51ccfc GDR |
3102 | error ("got %d template parameters for %q#D", |
3103 | TREE_VEC_LENGTH (a), decl); | |
e1a5ccf7 | 3104 | else |
0f51ccfc GDR |
3105 | error ("got %d template parameters for %q#T", |
3106 | TREE_VEC_LENGTH (a), current); | |
33bd39a2 | 3107 | error (" but %d required", TREE_VEC_LENGTH (t)); |
e1a5ccf7 | 3108 | } |
98c1c668 | 3109 | |
e1a5ccf7 JM |
3110 | /* Perhaps we should also check that the parms are used in the |
3111 | appropriate qualifying scopes in the declarator? */ | |
6633d636 | 3112 | |
e1a5ccf7 JM |
3113 | if (current == decl) |
3114 | current = ctx; | |
3115 | else | |
3116 | current = TYPE_CONTEXT (current); | |
3117 | } | |
5566b478 | 3118 | } |
8d08fdba | 3119 | |
5566b478 MS |
3120 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
3121 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 3122 | |
36a117a5 MM |
3123 | /* Push template declarations for global functions and types. Note |
3124 | that we do not try to push a global template friend declared in a | |
3125 | template class; such a thing may well depend on the template | |
39c01e4c | 3126 | parameters of the class. */ |
cfe507be | 3127 | if (new_template_p && !ctx |
36a117a5 | 3128 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b KL |
3129 | { |
3130 | tmpl = pushdecl_namespace_level (tmpl); | |
3131 | if (tmpl == error_mark_node) | |
3132 | return error_mark_node; | |
bd3d082e KL |
3133 | |
3134 | /* Hide template friend classes that haven't been declared yet. */ | |
3135 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
3136 | { | |
3137 | DECL_ANTICIPATED (tmpl) = 1; | |
3138 | DECL_FRIEND_P (tmpl) = 1; | |
3139 | } | |
c6f9f83b | 3140 | } |
8d08fdba | 3141 | |
5566b478 | 3142 | if (primary) |
5dd236e2 NS |
3143 | { |
3144 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; | |
3145 | if (DECL_CONV_FN_P (tmpl)) | |
3146 | { | |
6e04241f GS |
3147 | int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
3148 | ||
5dd236e2 NS |
3149 | /* It is a conversion operator. See if the type converted to |
3150 | depends on innermost template operands. */ | |
3151 | ||
d43f603d KL |
3152 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
3153 | depth)) | |
5dd236e2 NS |
3154 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
3155 | } | |
3156 | } | |
5566b478 | 3157 | |
a692ad2e | 3158 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
3159 | back to its most general template. If TMPL is a specialization, |
3160 | ARGS may only have the innermost set of arguments. Add the missing | |
3161 | argument levels if necessary. */ | |
3162 | if (DECL_TEMPLATE_INFO (tmpl)) | |
3163 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
3164 | ||
e1b3e07d | 3165 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 3166 | |
9188c363 | 3167 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
8d08fdba | 3168 | { |
ed44da02 MM |
3169 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
3170 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
b60ecc04 MM |
3171 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
3172 | /* Don't change the name if we've already set it up. */ | |
3173 | && !IDENTIFIER_TEMPLATE (DECL_NAME (decl))) | |
75650646 | 3174 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 3175 | } |
2aaf816d | 3176 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 3177 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
3178 | |
3179 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
3180 | } |
3181 | ||
6757edfe | 3182 | tree |
3a978d72 | 3183 | push_template_decl (tree decl) |
6757edfe MM |
3184 | { |
3185 | return push_template_decl_real (decl, 0); | |
3186 | } | |
3187 | ||
3188 | /* Called when a class template TYPE is redeclared with the indicated | |
3189 | template PARMS, e.g.: | |
7fe6899f MM |
3190 | |
3191 | template <class T> struct S; | |
3192 | template <class T> struct S {}; */ | |
3193 | ||
3194 | void | |
3a978d72 | 3195 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 3196 | { |
3d7de1fa | 3197 | tree tmpl; |
6757edfe | 3198 | tree tmpl_parms; |
7fe6899f MM |
3199 | int i; |
3200 | ||
3d7de1fa MM |
3201 | if (!TYPE_TEMPLATE_INFO (type)) |
3202 | { | |
0f51ccfc | 3203 | error ("%qT is not a template type", type); |
3d7de1fa MM |
3204 | return; |
3205 | } | |
3206 | ||
3207 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
3208 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
3209 | /* The type is nested in some template class. Nothing to worry | |
3210 | about here; there are no new template parameters for the nested | |
3211 | type. */ | |
3212 | return; | |
3213 | ||
ee921ddf MM |
3214 | if (!parms) |
3215 | { | |
3216 | error ("template specifiers not specified in declaration of %qD", | |
3217 | tmpl); | |
3218 | return; | |
3219 | } | |
3220 | ||
6757edfe MM |
3221 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
3222 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
3223 | ||
7fe6899f MM |
3224 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
3225 | { | |
0f51ccfc | 3226 | cp_error_at ("previous declaration %qD", tmpl); |
33bd39a2 | 3227 | error ("used %d template parameter%s instead of %d", |
7fe6899f MM |
3228 | TREE_VEC_LENGTH (tmpl_parms), |
3229 | TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", | |
3230 | TREE_VEC_LENGTH (parms)); | |
3231 | return; | |
3232 | } | |
3233 | ||
3234 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
3235 | { | |
3236 | tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
3237 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
3238 | tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
3239 | tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
3240 | ||
2649701f KL |
3241 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
3242 | TEMPLATE_DECL. */ | |
3243 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm) | |
3244 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
3245 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))) | |
7fe6899f | 3246 | { |
0f51ccfc GDR |
3247 | cp_error_at ("template parameter %q#D", tmpl_parm); |
3248 | error ("redeclared here as %q#D", parm); | |
7fe6899f MM |
3249 | return; |
3250 | } | |
3251 | ||
3252 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
3253 | { | |
3254 | /* We have in [temp.param]: | |
3255 | ||
3256 | A template-parameter may not be given default arguments | |
3257 | by two different declarations in the same scope. */ | |
0f51ccfc | 3258 | error ("redefinition of default argument for %q#D", parm); |
ddd2d57e | 3259 | error ("%J original definition appeared here", tmpl_parm); |
7fe6899f MM |
3260 | return; |
3261 | } | |
3262 | ||
3263 | if (parm_default != NULL_TREE) | |
3264 | /* Update the previous template parameters (which are the ones | |
3265 | that will really count) with the new default value. */ | |
3266 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
3267 | else if (tmpl_default != NULL_TREE) |
3268 | /* Update the new parameters, too; they'll be used as the | |
3269 | parameters for any members. */ | |
3270 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f MM |
3271 | } |
3272 | } | |
75650646 | 3273 | |
9baa27a9 MM |
3274 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
3275 | (possibly simplified) expression. */ | |
3276 | ||
3277 | tree | |
3278 | fold_non_dependent_expr (tree expr) | |
3279 | { | |
3280 | /* If we're in a template, but EXPR isn't value dependent, simplify | |
3281 | it. We're supposed to treat: | |
3282 | ||
3283 | template <typename T> void f(T[1 + 1]); | |
3284 | template <typename T> void f(T[2]); | |
3285 | ||
3286 | as two declarations of the same function, for example. */ | |
3287 | if (processing_template_decl | |
3288 | && !type_dependent_expression_p (expr) | |
7416ab02 | 3289 | && !value_dependent_expression_p (expr)) |
9baa27a9 MM |
3290 | { |
3291 | HOST_WIDE_INT saved_processing_template_decl; | |
3292 | ||
3293 | saved_processing_template_decl = processing_template_decl; | |
3294 | processing_template_decl = 0; | |
3295 | expr = tsubst_copy_and_build (expr, | |
3296 | /*args=*/NULL_TREE, | |
3297 | tf_error, | |
3298 | /*in_decl=*/NULL_TREE, | |
3299 | /*function_p=*/false); | |
3300 | processing_template_decl = saved_processing_template_decl; | |
3301 | } | |
3302 | return expr; | |
3303 | } | |
3304 | ||
b6ab6892 GB |
3305 | /* EXPR is an expression which is used in a constant-expression context. |
3306 | For instance, it could be a VAR_DECL with a constant initializer. | |
3307 | Extract the innest constant expression. | |
3308 | ||
8a784e4a NS |
3309 | This is basically a more powerful version of |
3310 | integral_constant_value, which can be used also in templates where | |
3311 | initializers can maintain a syntactic rather than semantic form | |
3312 | (even if they are non-dependent, for access-checking purposes). */ | |
b6ab6892 GB |
3313 | |
3314 | tree | |
3315 | fold_decl_constant_value (tree expr) | |
3316 | { | |
4ef69b83 GB |
3317 | tree const_expr = expr; |
3318 | do | |
b6ab6892 | 3319 | { |
b6ab6892 | 3320 | expr = fold_non_dependent_expr (const_expr); |
4ef69b83 | 3321 | const_expr = integral_constant_value (expr); |
b6ab6892 | 3322 | } |
4ef69b83 | 3323 | while (expr != const_expr); |
b6ab6892 | 3324 | |
8a784e4a | 3325 | return expr; |
b6ab6892 GB |
3326 | } |
3327 | ||
3328 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
3329 | must be a function or a pointer-to-function type, as specified | |
3330 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
3331 | and check that the resulting function has external linkage. */ | |
3332 | ||
3333 | static tree | |
3334 | convert_nontype_argument_function (tree type, tree expr) | |
3335 | { | |
3336 | tree fns = expr; | |
3337 | tree fn, fn_no_ptr; | |
3338 | ||
3339 | fn = instantiate_type (type, fns, tf_none); | |
3340 | if (fn == error_mark_node) | |
3341 | return error_mark_node; | |
3342 | ||
3343 | fn_no_ptr = fn; | |
3344 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
3345 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
3346 | ||
3347 | /* [temp.arg.nontype]/1 | |
3348 | ||
3349 | A template-argument for a non-type, non-template template-parameter | |
3350 | shall be one of: | |
3351 | [...] | |
3352 | -- the address of an object or function with external linkage. */ | |
3353 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
3354 | { | |
3355 | error ("%qE is not a valid template argument for type %qT " | |
3356 | "because function %qD has not external linkage", | |
3357 | expr, type, fn_no_ptr); | |
3358 | return NULL_TREE; | |
3359 | } | |
3360 | ||
3361 | return fn; | |
3362 | } | |
3363 | ||
75650646 MM |
3364 | /* Attempt to convert the non-type template parameter EXPR to the |
3365 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 3366 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
3367 | NULL_TREE if we issued an error message, or error_mark_node if we |
3368 | did not. We issue error messages for out-and-out bad template | |
3369 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 3370 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
3371 | must be non-dependent. |
3372 | ||
3373 | The conversion follows the special rules described in | |
3374 | [temp.arg.nontype], and it is much more strict than an implicit | |
3375 | conversion. | |
3376 | ||
3377 | This function is called twice for each template argument (see | |
3378 | lookup_template_class for a more accurate description of this | |
3379 | problem). This means that we need to handle expressions which | |
3380 | are not valid in a C++ source, but can be created from the | |
3381 | first call (for instance, casts to perform conversions). These | |
3382 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
3383 | |
3384 | static tree | |
3a978d72 | 3385 | convert_nontype_argument (tree type, tree expr) |
75650646 | 3386 | { |
9baa27a9 MM |
3387 | tree expr_type; |
3388 | ||
b6ab6892 GB |
3389 | /* Detect immediately string literals as invalid non-type argument. |
3390 | This special-case is not needed for correctness (we would easily | |
3391 | catch this later), but only to provide better diagnostic for this | |
3392 | common user mistake. As suggested by DR 100, we do not mention | |
3393 | linkage issues in the diagnostic as this is not the point. */ | |
3394 | if (TREE_CODE (expr) == STRING_CST) | |
3395 | { | |
3396 | error ("%qE is not a valid template argument for type %qT " | |
3397 | "because string literals can never be used in this context", | |
3398 | expr, type); | |
3399 | return NULL_TREE; | |
3400 | } | |
3401 | ||
9baa27a9 MM |
3402 | /* If we are in a template, EXPR may be non-dependent, but still |
3403 | have a syntactic, rather than semantic, form. For example, EXPR | |
3404 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
3405 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
3406 | so that access checking can be performed when the template is | |
3407 | instantiated -- but here we need the resolved form so that we can | |
3408 | convert the argument. */ | |
3409 | expr = fold_non_dependent_expr (expr); | |
3410 | expr_type = TREE_TYPE (expr); | |
75650646 | 3411 | |
b6ab6892 GB |
3412 | /* HACK: Due to double coercion, we can get a |
3413 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
3414 | which is the tree that we built on the first call (see | |
3415 | below when coercing to reference to object or to reference to | |
3416 | function). We just strip everything and get to the arg. | |
3417 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
3418 | for examples. */ | |
3419 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 3420 | { |
b6ab6892 | 3421 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 3422 | { |
b6ab6892 GB |
3423 | /* ??? Maybe we could use convert_from_reference here, but we |
3424 | would need to relax its constraints because the NOP_EXPR | |
3425 | could actually change the type to something more cv-qualified, | |
3426 | and this is not folded by convert_from_reference. */ | |
3427 | tree addr = TREE_OPERAND (expr, 0); | |
3428 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
3429 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
3430 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
3431 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
3432 | (TREE_TYPE (expr_type), | |
3433 | TREE_TYPE (TREE_TYPE (addr)))); | |
3434 | ||
3435 | expr = TREE_OPERAND (addr, 0); | |
3436 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
3437 | } |
3438 | ||
b6ab6892 GB |
3439 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
3440 | parameter is a pointer to object, through decay and | |
3441 | qualification conversion. Let's strip everything. */ | |
3442 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 3443 | { |
b6ab6892 GB |
3444 | STRIP_NOPS (expr); |
3445 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
3446 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
3447 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
3448 | an array. Otherwise, it is part of the original argument | |
3449 | in the source code. */ | |
3450 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
3451 | expr = TREE_OPERAND (expr, 0); | |
3452 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 3453 | } |
75650646 | 3454 | } |
c61dce3a | 3455 | |
b6ab6892 | 3456 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 3457 | |
b6ab6892 GB |
3458 | For a non-type template-parameter of integral or enumeration type, |
3459 | integral promotions (_conv.prom_) and integral conversions | |
3460 | (_conv.integral_) are applied. */ | |
3461 | if (INTEGRAL_TYPE_P (type)) | |
75650646 | 3462 | { |
75650646 MM |
3463 | if (!INTEGRAL_TYPE_P (expr_type)) |
3464 | return error_mark_node; | |
fddabb2c | 3465 | |
b6ab6892 GB |
3466 | expr = fold_decl_constant_value (expr); |
3467 | /* Notice that there are constant expressions like '4 % 0' which | |
3468 | do not fold into integer constants. */ | |
db02b6b9 | 3469 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 GB |
3470 | { |
3471 | error ("%qE is not a valid template argument for type %qT " | |
3472 | "because it is a non-constant expression", expr, type); | |
3473 | return NULL_TREE; | |
3474 | } | |
75650646 | 3475 | |
b6ab6892 GB |
3476 | /* At this point, an implicit conversion does what we want, |
3477 | because we already know that the expression is of integral | |
3478 | type. */ | |
3479 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
3480 | if (expr == error_mark_node) | |
3481 | return error_mark_node; | |
75650646 | 3482 | |
b6ab6892 GB |
3483 | /* Conversion was allowed: fold it to a bare integer constant. */ |
3484 | expr = fold (expr); | |
3485 | } | |
3486 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 3487 | |
b6ab6892 GB |
3488 | For a non-type template-parameter of type pointer to object, |
3489 | qualification conversions (_conv.qual_) and the array-to-pointer | |
3490 | conversion (_conv.array_) are applied. */ | |
3491 | else if (TYPE_PTROBV_P (type)) | |
3492 | { | |
3493 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 3494 | |
b6ab6892 GB |
3495 | A template-argument for a non-type, non-template template-parameter |
3496 | shall be one of: [...] | |
75650646 | 3497 | |
b6ab6892 GB |
3498 | -- the name of a non-type template-parameter; |
3499 | -- the address of an object or function with external linkage, [...] | |
3500 | expressed as "& id-expression" where the & is optional if the name | |
3501 | refers to a function or array, or if the corresponding | |
3502 | template-parameter is a reference. | |
e1467ff2 | 3503 | |
b6ab6892 GB |
3504 | Here, we do not care about functions, as they are invalid anyway |
3505 | for a parameter of type pointer-to-object. */ | |
3506 | bool constant_address_p = | |
3507 | (TREE_CODE (expr) == ADDR_EXPR | |
3508 | || TREE_CODE (expr_type) == ARRAY_TYPE | |
3509 | || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))); | |
3510 | ||
3511 | expr = decay_conversion (expr); | |
3512 | if (expr == error_mark_node) | |
3513 | return error_mark_node; | |
75650646 | 3514 | |
b6ab6892 GB |
3515 | expr = perform_qualification_conversions (type, expr); |
3516 | if (expr == error_mark_node) | |
3517 | return error_mark_node; | |
0dc09a61 | 3518 | |
b6ab6892 GB |
3519 | if (!constant_address_p) |
3520 | { | |
3521 | error ("%qE is not a valid template argument for type %qT " | |
3522 | "because it is not a constant pointer", expr, type); | |
3523 | return NULL_TREE; | |
3524 | } | |
3525 | } | |
3526 | /* [temp.arg.nontype]/5, bullet 3 | |
3527 | ||
3528 | For a non-type template-parameter of type reference to object, no | |
3529 | conversions apply. The type referred to by the reference may be more | |
3530 | cv-qualified than the (otherwise identical) type of the | |
3531 | template-argument. The template-parameter is bound directly to the | |
3532 | template-argument, which must be an lvalue. */ | |
3533 | else if (TYPE_REF_OBJ_P (type)) | |
3534 | { | |
3535 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
3536 | expr_type)) | |
3537 | return error_mark_node; | |
75650646 | 3538 | |
b6ab6892 GB |
3539 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
3540 | { | |
3541 | error ("%qE is not a valid template argument for type %qT " | |
3542 | "because of conflicts in cv-qualification", expr, type); | |
3543 | return NULL_TREE; | |
3544 | } | |
3545 | ||
3546 | if (!real_lvalue_p (expr)) | |
3547 | { | |
3548 | error ("%qE is not a valid template argument for type %qT " | |
3549 | "because it is not a lvalue", expr, type); | |
3550 | return NULL_TREE; | |
3551 | } | |
e6e174e5 | 3552 | |
b6ab6892 | 3553 | /* [temp.arg.nontype]/1 |
75650646 | 3554 | |
b6ab6892 GB |
3555 | A template-argument for a non-type, non-template template-parameter |
3556 | shall be one of: [...] | |
75650646 | 3557 | |
03fd3f84 | 3558 | -- the address of an object or function with external linkage. */ |
b6ab6892 GB |
3559 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
3560 | { | |
3561 | error ("%qE is not a valid template argument for type %qT " | |
3562 | "because object %qD has not external linkage", | |
3563 | expr, type, expr); | |
3564 | return NULL_TREE; | |
3565 | } | |
0dc09a61 | 3566 | |
b6ab6892 GB |
3567 | expr = build_nop (type, build_address (expr)); |
3568 | } | |
3569 | /* [temp.arg.nontype]/5, bullet 4 | |
3570 | ||
3571 | For a non-type template-parameter of type pointer to function, only | |
3572 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
3573 | template-argument represents a set of overloaded functions (or a | |
3574 | pointer to such), the matching function is selected from the set | |
3575 | (_over.over_). */ | |
3576 | else if (TYPE_PTRFN_P (type)) | |
3577 | { | |
3578 | /* If the argument is a template-id, we might not have enough | |
3579 | context information to decay the pointer. | |
3580 | ??? Why static5.C requires decay and subst1.C works fine | |
3581 | even without it? */ | |
3582 | if (!type_unknown_p (expr_type)) | |
3583 | { | |
3584 | expr = decay_conversion (expr); | |
3585 | if (expr == error_mark_node) | |
3586 | return error_mark_node; | |
3587 | } | |
75650646 | 3588 | |
b6ab6892 GB |
3589 | expr = convert_nontype_argument_function (type, expr); |
3590 | if (!expr || expr == error_mark_node) | |
3591 | return expr; | |
3592 | } | |
3593 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 3594 | |
b6ab6892 GB |
3595 | For a non-type template-parameter of type reference to function, no |
3596 | conversions apply. If the template-argument represents a set of | |
3597 | overloaded functions, the matching function is selected from the set | |
3598 | (_over.over_). */ | |
3599 | else if (TYPE_REFFN_P (type)) | |
3600 | { | |
3601 | if (TREE_CODE (expr) == ADDR_EXPR) | |
3602 | { | |
3603 | error ("%qE is not a valid template argument for type %qT " | |
3604 | "because it is a pointer", expr, type); | |
3605 | inform ("try using %qE instead", TREE_OPERAND (expr, 0)); | |
3606 | return NULL_TREE; | |
3607 | } | |
75650646 | 3608 | |
b6ab6892 GB |
3609 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
3610 | if (!expr || expr == error_mark_node) | |
3611 | return expr; | |
75650646 | 3612 | |
7866705a | 3613 | expr = build_nop (type, build_address (expr)); |
b6ab6892 GB |
3614 | } |
3615 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 3616 | |
b6ab6892 GB |
3617 | For a non-type template-parameter of type pointer to member function, |
3618 | no conversions apply. If the template-argument represents a set of | |
3619 | overloaded member functions, the matching member function is selected | |
3620 | from the set (_over.over_). */ | |
3621 | else if (TYPE_PTRMEMFUNC_P (type)) | |
3622 | { | |
3623 | expr = instantiate_type (type, expr, tf_none); | |
3624 | if (expr == error_mark_node) | |
3625 | return error_mark_node; | |
75650646 | 3626 | |
b6ab6892 GB |
3627 | /* There is no way to disable standard conversions in |
3628 | resolve_address_of_overloaded_function (called by | |
3629 | instantiate_type). It is possible that the call succeeded by | |
3630 | converting &B::I to &D::I (where B is a base of D), so we need | |
3631 | to reject this conversion here. | |
75650646 | 3632 | |
b6ab6892 GB |
3633 | Actually, even if there was a way to disable standard conversions, |
3634 | it would still be better to reject them here so that we can | |
3635 | provide a superior diagnostic. */ | |
3636 | if (!same_type_p (TREE_TYPE (expr), type)) | |
3637 | { | |
3638 | /* Make sure we are just one standard conversion off. */ | |
3639 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
3640 | error ("%qE is not a valid template argument for type %qT " | |
3641 | "because it is of type %qT", expr, type, | |
3642 | TREE_TYPE (expr)); | |
3643 | inform ("standard conversions are not allowed in this context"); | |
3644 | return NULL_TREE; | |
3645 | } | |
3646 | } | |
3647 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 3648 | |
b6ab6892 GB |
3649 | For a non-type template-parameter of type pointer to data member, |
3650 | qualification conversions (_conv.qual_) are applied. */ | |
3651 | else if (TYPE_PTRMEM_P (type)) | |
3652 | { | |
3653 | expr = perform_qualification_conversions (type, expr); | |
3654 | if (expr == error_mark_node) | |
75650646 | 3655 | return expr; |
75650646 | 3656 | } |
b6ab6892 GB |
3657 | /* A template non-type parameter must be one of the above. */ |
3658 | else | |
3659 | gcc_unreachable (); | |
75650646 | 3660 | |
b6ab6892 GB |
3661 | /* Sanity check: did we actually convert the argument to the |
3662 | right type? */ | |
3663 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
3664 | return expr; | |
75650646 MM |
3665 | } |
3666 | ||
b6ab6892 | 3667 | |
744fac59 KL |
3668 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
3669 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
3670 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
3671 | or PARM_DECL. | |
3672 | ||
3673 | ARG_PARMS may contain more parameters than PARM_PARMS. If this is | |
3674 | the case, then extra parameters must have default arguments. | |
3675 | ||
3676 | Consider the example: | |
3677 | template <class T, class Allocator = allocator> class vector; | |
3678 | template<template <class U> class TT> class C; | |
3679 | ||
3680 | C<vector> is a valid instantiation. PARM_PARMS for the above code | |
3681 | contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for | |
3682 | T and Allocator) and OUTER_ARGS contains the argument that is used to | |
3683 | substitute the TT parameter. */ | |
3684 | ||
3685 | static int | |
3a978d72 NN |
3686 | coerce_template_template_parms (tree parm_parms, |
3687 | tree arg_parms, | |
3688 | tsubst_flags_t complain, | |
3689 | tree in_decl, | |
3690 | tree outer_args) | |
744fac59 KL |
3691 | { |
3692 | int nparms, nargs, i; | |
3693 | tree parm, arg; | |
3694 | ||
50bc768d NS |
3695 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
3696 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
3697 | |
3698 | nparms = TREE_VEC_LENGTH (parm_parms); | |
3699 | nargs = TREE_VEC_LENGTH (arg_parms); | |
3700 | ||
3701 | /* The rule here is opposite of coerce_template_parms. */ | |
3702 | if (nargs < nparms | |
3703 | || (nargs > nparms | |
3704 | && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE)) | |
3705 | return 0; | |
3706 | ||
3707 | for (i = 0; i < nparms; ++i) | |
3708 | { | |
3709 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); | |
3710 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
3711 | ||
3712 | if (arg == NULL_TREE || arg == error_mark_node | |
3713 | || parm == NULL_TREE || parm == error_mark_node) | |
3714 | return 0; | |
3715 | ||
3716 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
3717 | return 0; | |
3718 | ||
3719 | switch (TREE_CODE (parm)) | |
3720 | { | |
3721 | case TYPE_DECL: | |
3722 | break; | |
3723 | ||
3724 | case TEMPLATE_DECL: | |
3725 | /* We encounter instantiations of templates like | |
3726 | template <template <template <class> class> class TT> | |
3727 | class C; */ | |
700466c2 JM |
3728 | { |
3729 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3730 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3731 | ||
8c6ab2db NS |
3732 | if (!coerce_template_template_parms |
3733 | (parmparm, argparm, complain, in_decl, outer_args)) | |
700466c2 JM |
3734 | return 0; |
3735 | } | |
3736 | break; | |
744fac59 KL |
3737 | |
3738 | case PARM_DECL: | |
3739 | /* The tsubst call is used to handle cases such as | |
00bdb87f KL |
3740 | |
3741 | template <int> class C {}; | |
3742 | template <class T, template <T> class TT> class D {}; | |
3743 | D<int, C> d; | |
3744 | ||
744fac59 | 3745 | i.e. the parameter list of TT depends on earlier parameters. */ |
00bdb87f KL |
3746 | if (!dependent_type_p (TREE_TYPE (arg)) |
3747 | && !same_type_p | |
3748 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
3749 | TREE_TYPE (arg))) | |
744fac59 KL |
3750 | return 0; |
3751 | break; | |
3752 | ||
3753 | default: | |
315fb5db | 3754 | gcc_unreachable (); |
744fac59 KL |
3755 | } |
3756 | } | |
3757 | return 1; | |
3758 | } | |
3759 | ||
8b5b8b7c MM |
3760 | /* Convert the indicated template ARG as necessary to match the |
3761 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
3762 | error_mark_node if the conversion was unsuccessful. Error and |
3763 | warning messages are issued under control of COMPLAIN. This | |
3764 | conversion is for the Ith parameter in the parameter list. ARGS is | |
3765 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
3766 | |
3767 | static tree | |
3a978d72 NN |
3768 | convert_template_argument (tree parm, |
3769 | tree arg, | |
3770 | tree args, | |
3771 | tsubst_flags_t complain, | |
3772 | int i, | |
3773 | tree in_decl) | |
8b5b8b7c MM |
3774 | { |
3775 | tree val; | |
3776 | tree inner_args; | |
3777 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; | |
3778 | ||
f9a7ae04 | 3779 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
8b5b8b7c MM |
3780 | |
3781 | if (TREE_CODE (arg) == TREE_LIST | |
a5ac359a | 3782 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
8b5b8b7c MM |
3783 | { |
3784 | /* The template argument was the name of some | |
3785 | member function. That's usually | |
0e339752 | 3786 | invalid, but static members are OK. In any |
8b5b8b7c MM |
3787 | case, grab the underlying fields/functions |
3788 | and issue an error later if required. */ | |
3789 | arg = TREE_VALUE (arg); | |
3790 | TREE_TYPE (arg) = unknown_type_node; | |
3791 | } | |
3792 | ||
3793 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3794 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3795 | || requires_tmpl_type); | |
3796 | ||
cbd63935 KL |
3797 | is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL |
3798 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3799 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
3800 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
303d1c55 | 3801 | |
b8c6534b KL |
3802 | if (is_tmpl_type |
3803 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
3804 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 3805 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 3806 | |
2f939d94 | 3807 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
3808 | |
3809 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3810 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3811 | { | |
0f51ccfc GDR |
3812 | pedwarn ("to refer to a type member of a template parameter, " |
3813 | "use %<typename %E%>", arg); | |
8b5b8b7c MM |
3814 | |
3815 | arg = make_typename_type (TREE_OPERAND (arg, 0), | |
3baa501d | 3816 | TREE_OPERAND (arg, 1), |
fc6a28d7 | 3817 | typename_type, |
c2ea3a40 | 3818 | complain & tf_error); |
8b5b8b7c MM |
3819 | is_type = 1; |
3820 | } | |
3821 | if (is_type != requires_type) | |
3822 | { | |
3823 | if (in_decl) | |
3824 | { | |
c2ea3a40 | 3825 | if (complain & tf_error) |
8b5b8b7c | 3826 | { |
0f51ccfc GDR |
3827 | error ("type/value mismatch at argument %d in template " |
3828 | "parameter list for %qD", | |
3829 | i + 1, in_decl); | |
8b5b8b7c | 3830 | if (is_type) |
0f51ccfc GDR |
3831 | error (" expected a constant of type %qT, got %qT", |
3832 | TREE_TYPE (parm), | |
3833 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
d12a7283 | 3834 | else if (requires_tmpl_type) |
0f51ccfc | 3835 | error (" expected a class template, got %qE", arg); |
8b5b8b7c | 3836 | else |
0f51ccfc | 3837 | error (" expected a type, got %qE", arg); |
8b5b8b7c MM |
3838 | } |
3839 | } | |
3840 | return error_mark_node; | |
3841 | } | |
3842 | if (is_tmpl_type ^ requires_tmpl_type) | |
3843 | { | |
c2ea3a40 | 3844 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 3845 | { |
0f51ccfc GDR |
3846 | error ("type/value mismatch at argument %d in template " |
3847 | "parameter list for %qD", | |
3848 | i + 1, in_decl); | |
8b5b8b7c | 3849 | if (is_tmpl_type) |
0f51ccfc | 3850 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 3851 | else |
0f51ccfc | 3852 | error (" expected a class template, got %qT", arg); |
8b5b8b7c MM |
3853 | } |
3854 | return error_mark_node; | |
3855 | } | |
3856 | ||
3857 | if (is_type) | |
3858 | { | |
3859 | if (requires_tmpl_type) | |
3860 | { | |
b8c6534b KL |
3861 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
3862 | /* The number of argument required is not known yet. | |
3863 | Just accept it for now. */ | |
3864 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
3865 | else |
3866 | { | |
b8c6534b KL |
3867 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
3868 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3869 | ||
3870 | if (coerce_template_template_parms (parmparm, argparm, | |
3871 | complain, in_decl, | |
3872 | inner_args)) | |
8b5b8b7c | 3873 | { |
b8c6534b KL |
3874 | val = arg; |
3875 | ||
3876 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
3877 | TEMPLATE_DECL. */ | |
3878 | if (val != error_mark_node | |
3879 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) | |
3880 | val = TREE_TYPE (val); | |
8b5b8b7c | 3881 | } |
b8c6534b KL |
3882 | else |
3883 | { | |
c2ea3a40 | 3884 | if (in_decl && (complain & tf_error)) |
b8c6534b | 3885 | { |
0f51ccfc GDR |
3886 | error ("type/value mismatch at argument %d in " |
3887 | "template parameter list for %qD", | |
3888 | i + 1, in_decl); | |
3889 | error (" expected a template of type %qD, got %qD", | |
3890 | parm, arg); | |
b8c6534b | 3891 | } |
8b5b8b7c | 3892 | |
b8c6534b KL |
3893 | val = error_mark_node; |
3894 | } | |
8b5b8b7c MM |
3895 | } |
3896 | } | |
3897 | else | |
058b15c1 | 3898 | val = arg; |
8b5b8b7c MM |
3899 | } |
3900 | else | |
3901 | { | |
4393e105 | 3902 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 3903 | |
11b810f1 | 3904 | if (invalid_nontype_parm_type_p (t, complain)) |
db3f4e4e NS |
3905 | return error_mark_node; |
3906 | ||
d768a589 | 3907 | if (!uses_template_parms (arg) && !uses_template_parms (t)) |
8b5b8b7c MM |
3908 | /* We used to call digest_init here. However, digest_init |
3909 | will report errors, which we don't want when complain | |
3910 | is zero. More importantly, digest_init will try too | |
3911 | hard to convert things: for example, `0' should not be | |
3912 | converted to pointer type at this point according to | |
3913 | the standard. Accepting this is not merely an | |
3914 | extension, since deciding whether or not these | |
3915 | conversions can occur is part of determining which | |
dc957d14 | 3916 | function template to call, or whether a given explicit |
0e339752 | 3917 | argument specification is valid. */ |
8b5b8b7c MM |
3918 | val = convert_nontype_argument (t, arg); |
3919 | else | |
3920 | val = arg; | |
3921 | ||
3922 | if (val == NULL_TREE) | |
3923 | val = error_mark_node; | |
c2ea3a40 | 3924 | else if (val == error_mark_node && (complain & tf_error)) |
0f51ccfc | 3925 | error ("could not convert template argument %qE to %qT", arg, t); |
8b5b8b7c MM |
3926 | } |
3927 | ||
3928 | return val; | |
3929 | } | |
3930 | ||
3931 | /* Convert all template arguments to their appropriate types, and | |
3932 | return a vector containing the innermost resulting template | |
c2ea3a40 | 3933 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 3934 | warning messages are issued under control of COMPLAIN. |
75650646 | 3935 | |
838dfd8a | 3936 | If REQUIRE_ALL_ARGUMENTS is nonzero, all arguments must be |
75650646 MM |
3937 | provided in ARGLIST, or else trailing parameters must have default |
3938 | values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument | |
6dfbb909 | 3939 | deduction for any unspecified trailing arguments. */ |
de575009 | 3940 | |
8d08fdba | 3941 | static tree |
3a978d72 NN |
3942 | coerce_template_parms (tree parms, |
3943 | tree args, | |
3944 | tree in_decl, | |
3945 | tsubst_flags_t complain, | |
3946 | int require_all_arguments) | |
8d08fdba | 3947 | { |
a292b002 | 3948 | int nparms, nargs, i, lost = 0; |
e4a84209 | 3949 | tree inner_args; |
8b5b8b7c MM |
3950 | tree new_args; |
3951 | tree new_inner_args; | |
a292b002 | 3952 | |
f9a7ae04 | 3953 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
bf12d54d | 3954 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
a292b002 MS |
3955 | nparms = TREE_VEC_LENGTH (parms); |
3956 | ||
3957 | if (nargs > nparms | |
3958 | || (nargs < nparms | |
75650646 | 3959 | && require_all_arguments |
a292b002 | 3960 | && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) |
8d08fdba | 3961 | { |
c2ea3a40 | 3962 | if (complain & tf_error) |
75650646 | 3963 | { |
33bd39a2 | 3964 | error ("wrong number of template arguments (%d, should be %d)", |
0f51ccfc | 3965 | nargs, nparms); |
75650646 MM |
3966 | |
3967 | if (in_decl) | |
0f51ccfc | 3968 | cp_error_at ("provided for %qD", in_decl); |
75650646 MM |
3969 | } |
3970 | ||
8d08fdba MS |
3971 | return error_mark_node; |
3972 | } | |
3973 | ||
f31c0a32 | 3974 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c MM |
3975 | new_args = add_outermost_template_args (args, new_inner_args); |
3976 | for (i = 0; i < nparms; i++) | |
8d08fdba | 3977 | { |
8b5b8b7c MM |
3978 | tree arg; |
3979 | tree parm; | |
e4a84209 | 3980 | |
8b5b8b7c MM |
3981 | /* Get the Ith template parameter. */ |
3982 | parm = TREE_VEC_ELT (parms, i); | |
75650646 | 3983 | |
8b5b8b7c | 3984 | /* Calculate the Ith argument. */ |
bf12d54d | 3985 | if (i < nargs) |
8b5b8b7c | 3986 | arg = TREE_VEC_ELT (inner_args, i); |
ffd49b19 | 3987 | else if (require_all_arguments) |
04c06002 | 3988 | /* There must be a default arg in this case. */ |
a91db711 NS |
3989 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, |
3990 | complain, in_decl); | |
ffd49b19 NS |
3991 | else |
3992 | break; | |
3993 | ||
50bc768d | 3994 | gcc_assert (arg); |
ffd49b19 | 3995 | if (arg == error_mark_node) |
e34b0922 KL |
3996 | { |
3997 | if (complain & tf_error) | |
3998 | error ("template argument %d is invalid", i + 1); | |
3999 | } | |
8b5b8b7c MM |
4000 | else |
4001 | arg = convert_template_argument (TREE_VALUE (parm), | |
4002 | arg, new_args, complain, i, | |
4003 | in_decl); | |
4004 | ||
4005 | if (arg == error_mark_node) | |
8d08fdba | 4006 | lost++; |
8b5b8b7c | 4007 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 4008 | } |
8b5b8b7c | 4009 | |
8d08fdba MS |
4010 | if (lost) |
4011 | return error_mark_node; | |
8b5b8b7c MM |
4012 | |
4013 | return new_inner_args; | |
8d08fdba MS |
4014 | } |
4015 | ||
34016c81 JM |
4016 | /* Returns 1 if template args OT and NT are equivalent. */ |
4017 | ||
d8e178a0 | 4018 | static int |
3a978d72 | 4019 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
4020 | { |
4021 | if (nt == ot) | |
4022 | return 1; | |
74601d7c | 4023 | |
34016c81 JM |
4024 | if (TREE_CODE (nt) == TREE_VEC) |
4025 | /* For member templates */ | |
74601d7c KL |
4026 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
4027 | else if (TYPE_P (nt)) | |
4028 | return TYPE_P (ot) && same_type_p (ot, nt); | |
4029 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) | |
4030 | return 0; | |
34016c81 | 4031 | else |
c8a209ca | 4032 | return cp_tree_equal (ot, nt); |
34016c81 JM |
4033 | } |
4034 | ||
4035 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
4036 | of template arguments. Returns 0 otherwise. */ |
4037 | ||
6757edfe | 4038 | int |
3a978d72 | 4039 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
4040 | { |
4041 | int i; | |
4042 | ||
386b8a85 JM |
4043 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
4044 | return 0; | |
4045 | ||
5566b478 MS |
4046 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
4047 | { | |
4048 | tree nt = TREE_VEC_ELT (newargs, i); | |
4049 | tree ot = TREE_VEC_ELT (oldargs, i); | |
4050 | ||
34016c81 | 4051 | if (! template_args_equal (ot, nt)) |
61a127b3 | 4052 | return 0; |
5566b478 MS |
4053 | } |
4054 | return 1; | |
4055 | } | |
4056 | ||
8d08fdba MS |
4057 | /* Given class template name and parameter list, produce a user-friendly name |
4058 | for the instantiation. */ | |
e92cc029 | 4059 | |
8d08fdba | 4060 | static char * |
3a978d72 | 4061 | mangle_class_name_for_template (const char* name, tree parms, tree arglist) |
8d08fdba MS |
4062 | { |
4063 | static struct obstack scratch_obstack; | |
4064 | static char *scratch_firstobj; | |
4065 | int i, nparms; | |
8d08fdba MS |
4066 | |
4067 | if (!scratch_firstobj) | |
fc378698 | 4068 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
4069 | else |
4070 | obstack_free (&scratch_obstack, scratch_firstobj); | |
fc378698 | 4071 | scratch_firstobj = obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 4072 | |
18e314ad GS |
4073 | #define ccat(C) obstack_1grow (&scratch_obstack, (C)); |
4074 | #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S)) | |
8d08fdba MS |
4075 | |
4076 | cat (name); | |
4077 | ccat ('<'); | |
4078 | nparms = TREE_VEC_LENGTH (parms); | |
f9a7ae04 | 4079 | arglist = INNERMOST_TEMPLATE_ARGS (arglist); |
50bc768d | 4080 | gcc_assert (nparms == TREE_VEC_LENGTH (arglist)); |
8d08fdba MS |
4081 | for (i = 0; i < nparms; i++) |
4082 | { | |
a292b002 MS |
4083 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
4084 | tree arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba MS |
4085 | |
4086 | if (i) | |
4087 | ccat (','); | |
4088 | ||
a292b002 | 4089 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 4090 | { |
761f0855 | 4091 | cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); |
8d08fdba MS |
4092 | continue; |
4093 | } | |
73b0fce8 KL |
4094 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
4095 | { | |
4096 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 ML |
4097 | { |
4098 | /* Already substituted with real template. Just output | |
4099 | the template name here */ | |
cb0dbb9a | 4100 | tree context = DECL_CONTEXT (arg); |
ec4f972f AS |
4101 | if (context) |
4102 | { | |
4103 | /* The template may be defined in a namespace, or | |
4104 | may be a member template. */ | |
50bc768d NS |
4105 | gcc_assert (TREE_CODE (context) == NAMESPACE_DECL |
4106 | || CLASS_TYPE_P (context)); | |
4107 | cat (decl_as_string (DECL_CONTEXT (arg), | |
4108 | TFF_PLAIN_IDENTIFIER)); | |
4109 | cat ("::"); | |
2c73f9f5 ML |
4110 | } |
4111 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
4112 | } | |
73b0fce8 | 4113 | else |
f4f206f4 | 4114 | /* Output the parameter declaration. */ |
761f0855 | 4115 | cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); |
73b0fce8 KL |
4116 | continue; |
4117 | } | |
8d08fdba | 4118 | else |
50bc768d | 4119 | gcc_assert (TREE_CODE (parm) == PARM_DECL); |
8d08fdba | 4120 | |
8d08fdba MS |
4121 | /* No need to check arglist against parmlist here; we did that |
4122 | in coerce_template_parms, called from lookup_template_class. */ | |
761f0855 | 4123 | cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER)); |
8d08fdba MS |
4124 | } |
4125 | { | |
4126 | char *bufp = obstack_next_free (&scratch_obstack); | |
4127 | int offset = 0; | |
4128 | while (bufp[offset - 1] == ' ') | |
4129 | offset--; | |
4130 | obstack_blank_fast (&scratch_obstack, offset); | |
4131 | ||
4132 | /* B<C<char> >, not B<C<char>> */ | |
4133 | if (bufp[offset - 1] == '>') | |
4134 | ccat (' '); | |
4135 | } | |
4136 | ccat ('>'); | |
4137 | ccat ('\0'); | |
4138 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
4139 | } |
4140 | ||
bd6dd845 | 4141 | static tree |
3a978d72 | 4142 | classtype_mangled_name (tree t) |
5566b478 MS |
4143 | { |
4144 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
4145 | /* Specializations have already had their names set up in |
4146 | lookup_template_class. */ | |
370af2d5 | 4147 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
9fbf56f7 MM |
4148 | { |
4149 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
4150 | ||
36a117a5 MM |
4151 | /* For non-primary templates, the template parameters are |
4152 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
4153 | if (PRIMARY_TEMPLATE_P (tmpl)) |
4154 | { | |
4155 | tree name = DECL_NAME (tmpl); | |
4156 | char *mangled_name = mangle_class_name_for_template | |
4157 | (IDENTIFIER_POINTER (name), | |
4158 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
4159 | CLASSTYPE_TI_ARGS (t)); | |
4160 | tree id = get_identifier (mangled_name); | |
4161 | IDENTIFIER_TEMPLATE (id) = name; | |
4162 | return id; | |
4163 | } | |
5566b478 | 4164 | } |
9fbf56f7 MM |
4165 | |
4166 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
4167 | } |
4168 | ||
4169 | static void | |
3a978d72 | 4170 | add_pending_template (tree d) |
5566b478 | 4171 | { |
3ae18eaf JM |
4172 | tree ti = (TYPE_P (d) |
4173 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
4174 | : DECL_TEMPLATE_INFO (d)); | |
46ccf50a | 4175 | tree pt; |
3ae18eaf | 4176 | int level; |
e92cc029 | 4177 | |
824b9a4c | 4178 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
4179 | return; |
4180 | ||
3ae18eaf JM |
4181 | /* We are called both from instantiate_decl, where we've already had a |
4182 | tinst_level pushed, and instantiate_template, where we haven't. | |
4183 | Compensate. */ | |
4184 | level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d); | |
4185 | ||
4186 | if (level) | |
4187 | push_tinst_level (d); | |
4188 | ||
46ccf50a JM |
4189 | pt = tree_cons (current_tinst_level, d, NULL_TREE); |
4190 | if (last_pending_template) | |
4191 | TREE_CHAIN (last_pending_template) = pt; | |
4192 | else | |
4193 | pending_templates = pt; | |
4194 | ||
4195 | last_pending_template = pt; | |
4196 | ||
824b9a4c | 4197 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
4198 | |
4199 | if (level) | |
4200 | pop_tinst_level (); | |
5566b478 MS |
4201 | } |
4202 | ||
386b8a85 | 4203 | |
4ba126e4 MM |
4204 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
4205 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
4206 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
4207 | |
4208 | tree | |
3a978d72 | 4209 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 4210 | { |
2c73f9f5 | 4211 | tree type; |
050367a3 | 4212 | |
4ba126e4 MM |
4213 | if (fns == error_mark_node || arglist == error_mark_node) |
4214 | return error_mark_node; | |
4215 | ||
50bc768d | 4216 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
4230cec2 NS |
4217 | gcc_assert (fns && (is_overloaded_fn (fns) |
4218 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
4ba126e4 | 4219 | |
50ad9642 MM |
4220 | if (BASELINK_P (fns)) |
4221 | { | |
f293ce4b RS |
4222 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
4223 | unknown_type_node, | |
4224 | BASELINK_FUNCTIONS (fns), | |
4225 | arglist); | |
50ad9642 MM |
4226 | return fns; |
4227 | } | |
4228 | ||
2c73f9f5 ML |
4229 | type = TREE_TYPE (fns); |
4230 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
4231 | type = unknown_type_node; | |
4ba126e4 | 4232 | |
f293ce4b | 4233 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
4234 | } |
4235 | ||
a2b60a0e MM |
4236 | /* Within the scope of a template class S<T>, the name S gets bound |
4237 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
4238 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
4239 | or one of its enclosing classes, and that type is a template, | |
4240 | return the associated TEMPLATE_DECL. Otherwise, the original | |
4241 | DECL is returned. */ | |
4242 | ||
a723baf1 | 4243 | tree |
3a978d72 | 4244 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e MM |
4245 | { |
4246 | return (decl != NULL_TREE | |
4247 | && TREE_CODE (decl) == TYPE_DECL | |
4248 | && DECL_ARTIFICIAL (decl) | |
511b60ff | 4249 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
a2b60a0e MM |
4250 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
4251 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; | |
4252 | } | |
386b8a85 | 4253 | |
8d08fdba MS |
4254 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
4255 | parameters, find the desired type. | |
4256 | ||
4257 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
4258 | |
4259 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
75650646 MM |
4260 | instantiate. |
4261 | ||
838dfd8a | 4262 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 4263 | the class we are looking up. |
f9c244b8 | 4264 | |
c2ea3a40 | 4265 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 4266 | |
75650646 MM |
4267 | If the template class is really a local class in a template |
4268 | function, then the FUNCTION_CONTEXT is the function in which it is | |
b6ab6892 GB |
4269 | being instantiated. |
4270 | ||
4271 | ??? Note that this function is currently called *twice* for each | |
4272 | template-id: the first time from the parser, while creating the | |
4273 | incomplete type (finish_template_type), and the second type during the | |
4274 | real instantiation (instantiate_template_class). This is surely something | |
4275 | that we want to avoid. It also causes some problems with argument | |
4276 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 4277 | |
8d08fdba | 4278 | tree |
3a978d72 NN |
4279 | lookup_template_class (tree d1, |
4280 | tree arglist, | |
4281 | tree in_decl, | |
4282 | tree context, | |
4283 | int entering_scope, | |
4284 | tsubst_flags_t complain) | |
8d08fdba | 4285 | { |
a703fb38 | 4286 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 4287 | tree t; |
42eaed49 | 4288 | |
fd295cb2 | 4289 | timevar_push (TV_NAME_LOOKUP); |
42eaed49 | 4290 | |
5566b478 MS |
4291 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
4292 | { | |
90ea9897 MM |
4293 | tree value = innermost_non_namespace_value (d1); |
4294 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
4295 | template = value; | |
73b0fce8 KL |
4296 | else |
4297 | { | |
2c73f9f5 ML |
4298 | if (context) |
4299 | push_decl_namespace (context); | |
3ebc5c52 MM |
4300 | template = lookup_name (d1, /*prefer_type=*/0); |
4301 | template = maybe_get_template_decl_from_type_decl (template); | |
2c73f9f5 ML |
4302 | if (context) |
4303 | pop_decl_namespace (); | |
73b0fce8 | 4304 | } |
8d019cef JM |
4305 | if (template) |
4306 | context = DECL_CONTEXT (template); | |
5566b478 | 4307 | } |
c91a56d2 MS |
4308 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
4309 | { | |
802dbc34 JM |
4310 | tree type = TREE_TYPE (d1); |
4311 | ||
4312 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
4313 | an implicit typename for the second A. Deal with it. */ | |
4314 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
4315 | type = TREE_TYPE (type); | |
4316 | ||
4317 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
f3400fe2 | 4318 | { |
802dbc34 | 4319 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
4320 | d1 = DECL_NAME (template); |
4321 | } | |
c91a56d2 | 4322 | } |
ed44da02 | 4323 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
2f939d94 | 4324 | || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) |
5566b478 | 4325 | { |
ed44da02 | 4326 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
4327 | d1 = DECL_NAME (template); |
4328 | } | |
93cdc044 | 4329 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
17aec3eb | 4330 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 JM |
4331 | { |
4332 | template = d1; | |
4333 | d1 = DECL_NAME (template); | |
4334 | context = DECL_CONTEXT (template); | |
4335 | } | |
8d08fdba | 4336 | |
90ea9897 | 4337 | /* Issue an error message if we didn't find a template. */ |
8d08fdba | 4338 | if (! template) |
f3400fe2 | 4339 | { |
c2ea3a40 | 4340 | if (complain & tf_error) |
0f51ccfc | 4341 | error ("%qT is not a template", d1); |
fd295cb2 | 4342 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 4343 | } |
2c73f9f5 | 4344 | |
a87b4257 | 4345 | if (TREE_CODE (template) != TEMPLATE_DECL |
42eaed49 NS |
4346 | /* Make sure it's a user visible template, if it was named by |
4347 | the user. */ | |
4348 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template) | |
4349 | && !PRIMARY_TEMPLATE_P (template))) | |
8d08fdba | 4350 | { |
c2ea3a40 | 4351 | if (complain & tf_error) |
f9c244b8 | 4352 | { |
0f51ccfc | 4353 | error ("non-template type %qT used as a template", d1); |
f9c244b8 | 4354 | if (in_decl) |
0f51ccfc | 4355 | cp_error_at ("for template declaration %qD", in_decl); |
f9c244b8 | 4356 | } |
fd295cb2 | 4357 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 4358 | } |
8d08fdba | 4359 | |
42eaed49 NS |
4360 | complain &= ~tf_user; |
4361 | ||
73b0fce8 KL |
4362 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
4363 | { | |
4364 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
4365 | template arguments */ | |
4366 | ||
1899c3a4 | 4367 | tree parm; |
73b0fce8 KL |
4368 | tree arglist2; |
4369 | ||
73b0fce8 KL |
4370 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
4371 | ||
4f96ff63 KL |
4372 | /* Consider an example where a template template parameter declared as |
4373 | ||
4374 | template <class T, class U = std::allocator<T> > class TT | |
4375 | ||
4376 | The template parameter level of T and U are one level larger than | |
4377 | of TT. To proper process the default argument of U, say when an | |
4378 | instantiation `TT<int>' is seen, we need to build the full | |
342cea95 KL |
4379 | arguments containing {int} as the innermost level. Outer levels, |
4380 | available when not appearing as default template argument, can be | |
4381 | obtained from `current_template_args ()'. | |
4f96ff63 | 4382 | |
342cea95 KL |
4383 | Suppose that TT is later substituted with std::vector. The above |
4384 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
4385 | level 1, and T at level 2, while the template arguments at level 1 | |
4386 | becomes {std::vector} and the inner level 2 is {int}. */ | |
4387 | ||
4388 | if (current_template_parms) | |
4f96ff63 KL |
4389 | arglist = add_to_template_args (current_template_args (), arglist); |
4390 | ||
f9c244b8 NS |
4391 | arglist2 = coerce_template_parms (parmlist, arglist, template, |
4392 | complain, /*require_all_args=*/1); | |
3e4a3562 | 4393 | if (arglist2 == error_mark_node |
544aef8c | 4394 | || (!uses_template_parms (arglist2) |
3e4a3562 | 4395 | && check_instantiated_args (template, arglist2, complain))) |
fd295cb2 | 4396 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 4397 | |
dac65501 | 4398 | parm = bind_template_template_parm (TREE_TYPE (template), arglist2); |
fd295cb2 | 4399 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 4400 | } |
36a117a5 | 4401 | else |
8d08fdba | 4402 | { |
36a117a5 | 4403 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 4404 | tree gen_tmpl; |
36a117a5 MM |
4405 | tree type_decl; |
4406 | tree found = NULL_TREE; | |
4407 | int arg_depth; | |
4408 | int parm_depth; | |
dbfe2124 | 4409 | int is_partial_instantiation; |
830bfa74 | 4410 | |
7ac7b28f MM |
4411 | gen_tmpl = most_general_template (template); |
4412 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
4413 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
4414 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
4415 | ||
4416 | if (arg_depth == 1 && parm_depth > 1) | |
4417 | { | |
39c01e4c | 4418 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
4419 | For example, given: |
4420 | ||
4421 | template <class T> struct S1 { | |
4422 | template <class U> struct S2 {}; | |
4423 | template <class U> struct S2<U*> {}; | |
4424 | }; | |
4425 | ||
4426 | we will be called with an ARGLIST of `U*', but the | |
4427 | TEMPLATE will be `template <class T> template | |
4428 | <class U> struct S1<T>::S2'. We must fill in the missing | |
4429 | arguments. */ | |
7ac7b28f MM |
4430 | arglist |
4431 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), | |
4432 | arglist); | |
36a117a5 MM |
4433 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
4434 | } | |
5566b478 | 4435 | |
41f5d4b1 | 4436 | /* Now we should have enough arguments. */ |
50bc768d | 4437 | gcc_assert (parm_depth == arg_depth); |
36a117a5 | 4438 | |
7ac7b28f MM |
4439 | /* From here on, we're only interested in the most general |
4440 | template. */ | |
4441 | template = gen_tmpl; | |
4442 | ||
36a117a5 MM |
4443 | /* Calculate the BOUND_ARGS. These will be the args that are |
4444 | actually tsubst'd into the definition to create the | |
4445 | instantiation. */ | |
4446 | if (parm_depth > 1) | |
830bfa74 MM |
4447 | { |
4448 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 4449 | int i; |
e4a84209 | 4450 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 4451 | |
f31c0a32 | 4452 | tree bound_args = make_tree_vec (parm_depth); |
830bfa74 | 4453 | |
e4a84209 | 4454 | for (i = saved_depth, |
830bfa74 | 4455 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 4456 | i > 0 && t != NULL_TREE; |
830bfa74 | 4457 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 4458 | { |
ee3071ef NS |
4459 | tree a = coerce_template_parms (TREE_VALUE (t), |
4460 | arglist, template, | |
4461 | complain, /*require_all_args=*/1); | |
88e98cfe KL |
4462 | |
4463 | /* Don't process further if one of the levels fails. */ | |
4464 | if (a == error_mark_node) | |
4465 | { | |
4466 | /* Restore the ARGLIST to its full size. */ | |
4467 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
4468 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4469 | } | |
4470 | ||
e4a84209 MM |
4471 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
4472 | ||
4473 | /* We temporarily reduce the length of the ARGLIST so | |
4474 | that coerce_template_parms will see only the arguments | |
4475 | corresponding to the template parameters it is | |
4476 | examining. */ | |
4477 | TREE_VEC_LENGTH (arglist)--; | |
4478 | } | |
4479 | ||
4480 | /* Restore the ARGLIST to its full size. */ | |
4481 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
4482 | ||
36a117a5 | 4483 | arglist = bound_args; |
830bfa74 MM |
4484 | } |
4485 | else | |
36a117a5 MM |
4486 | arglist |
4487 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
f9a7ae04 | 4488 | INNERMOST_TEMPLATE_ARGS (arglist), |
f9c244b8 NS |
4489 | template, |
4490 | complain, /*require_all_args=*/1); | |
36a117a5 | 4491 | |
c353b8e3 | 4492 | if (arglist == error_mark_node) |
36a117a5 | 4493 | /* We were unable to bind the arguments. */ |
fd295cb2 | 4494 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 4495 | |
36a117a5 MM |
4496 | /* In the scope of a template class, explicit references to the |
4497 | template class refer to the type of the template, not any | |
4498 | instantiation of it. For example, in: | |
4499 | ||
4500 | template <class T> class C { void f(C<T>); } | |
4501 | ||
4502 | the `C<T>' is just the same as `C'. Outside of the | |
4503 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 4504 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
4505 | arglist)) |
4506 | { | |
4507 | found = template_type; | |
4508 | ||
4509 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) | |
5566b478 | 4510 | { |
36a117a5 MM |
4511 | tree ctx; |
4512 | ||
36a117a5 | 4513 | for (ctx = current_class_type; |
5f04800c KL |
4514 | ctx && TREE_CODE (ctx) != NAMESPACE_DECL; |
4515 | ctx = (TYPE_P (ctx) | |
4516 | ? TYPE_CONTEXT (ctx) | |
4517 | : DECL_CONTEXT (ctx))) | |
4518 | if (TYPE_P (ctx) && same_type_p (ctx, template_type)) | |
4519 | goto found_ctx; | |
36a117a5 | 4520 | |
6df5158a NS |
4521 | /* We're not in the scope of the class, so the |
4522 | TEMPLATE_TYPE is not the type we want after all. */ | |
4523 | found = NULL_TREE; | |
4524 | found_ctx:; | |
5566b478 MS |
4525 | } |
4526 | } | |
36a117a5 | 4527 | if (found) |
fd295cb2 | 4528 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
414ea4aa | 4529 | |
c7222c02 MM |
4530 | /* If we already have this specialization, return it. */ |
4531 | found = retrieve_specialization (template, arglist, | |
4532 | /*class_specializations_p=*/false); | |
4533 | if (found) | |
4534 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
5566b478 | 4535 | |
dbfe2124 | 4536 | /* This type is a "partial instantiation" if any of the template |
ab097535 | 4537 | arguments still involve template parameters. Note that we set |
486e4077 MM |
4538 | IS_PARTIAL_INSTANTIATION for partial specializations as |
4539 | well. */ | |
dbfe2124 MM |
4540 | is_partial_instantiation = uses_template_parms (arglist); |
4541 | ||
c353b8e3 MM |
4542 | /* If the deduced arguments are invalid, then the binding |
4543 | failed. */ | |
4544 | if (!is_partial_instantiation | |
4545 | && check_instantiated_args (template, | |
4546 | INNERMOST_TEMPLATE_ARGS (arglist), | |
4547 | complain)) | |
4548 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4549 | ||
3ebc5c52 MM |
4550 | if (!is_partial_instantiation |
4551 | && !PRIMARY_TEMPLATE_P (template) | |
4552 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
4553 | { | |
3ebc5c52 MM |
4554 | found = xref_tag_from_type (TREE_TYPE (template), |
4555 | DECL_NAME (template), | |
29ef83de | 4556 | /*tag_scope=*/ts_global); |
fd295cb2 | 4557 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 4558 | } |
3ebc5c52 | 4559 | |
95ee998c | 4560 | context = tsubst (DECL_CONTEXT (template), arglist, |
c2ea3a40 | 4561 | complain, in_decl); |
95ee998c MM |
4562 | if (!context) |
4563 | context = global_namespace; | |
4564 | ||
36a117a5 | 4565 | /* Create the type. */ |
ed44da02 MM |
4566 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
4567 | { | |
dbfe2124 | 4568 | if (!is_partial_instantiation) |
92777ce4 NS |
4569 | { |
4570 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
4571 | t = start_enum (TYPE_IDENTIFIER (template_type)); | |
4572 | } | |
ed44da02 | 4573 | else |
dbfe2124 | 4574 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
4575 | the values for the enumeration constants may involve |
4576 | template parameters. And, no one should be interested | |
4577 | in the enumeration constants for such a type. */ | |
4578 | t = make_node (ENUMERAL_TYPE); | |
4579 | } | |
4580 | else | |
4581 | { | |
33848bb0 | 4582 | t = make_aggr_type (TREE_CODE (template_type)); |
ed44da02 MM |
4583 | CLASSTYPE_DECLARED_CLASS (t) |
4584 | = CLASSTYPE_DECLARED_CLASS (template_type); | |
ed44da02 | 4585 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 4586 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
4587 | |
4588 | /* A local class. Make sure the decl gets registered properly. */ | |
4589 | if (context == current_function_decl) | |
bd3d082e | 4590 | pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current); |
ed44da02 MM |
4591 | } |
4592 | ||
ae673f14 JM |
4593 | /* If we called start_enum or pushtag above, this information |
4594 | will already be set up. */ | |
ed44da02 MM |
4595 | if (!TYPE_NAME (t)) |
4596 | { | |
4597 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
36a117a5 | 4598 | |
9188c363 | 4599 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 4600 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 4601 | TYPE_STUB_DECL (t) = type_decl; |
f31686a3 RH |
4602 | DECL_SOURCE_LOCATION (type_decl) |
4603 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); | |
ed44da02 MM |
4604 | } |
4605 | else | |
4606 | type_decl = TYPE_NAME (t); | |
36a117a5 | 4607 | |
cab7a9a3 KL |
4608 | TREE_PRIVATE (type_decl) |
4609 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
4610 | TREE_PROTECTED (type_decl) | |
4611 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
4612 | ||
9fbf56f7 MM |
4613 | /* Set up the template information. We have to figure out which |
4614 | template is the immediate parent if this is a full | |
4615 | instantiation. */ | |
4616 | if (parm_depth == 1 || is_partial_instantiation | |
4617 | || !PRIMARY_TEMPLATE_P (template)) | |
4618 | /* This case is easy; there are no member templates involved. */ | |
4619 | found = template; | |
4620 | else | |
4621 | { | |
ab097535 NS |
4622 | /* This is a full instantiation of a member template. Look |
4623 | for a partial instantiation of which this is an instance. */ | |
9fbf56f7 MM |
4624 | |
4625 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
4626 | found; found = TREE_CHAIN (found)) | |
4627 | { | |
4628 | int success; | |
4629 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
4630 | ||
4631 | /* We only want partial instantiations, here, not | |
4632 | specializations or full instantiations. */ | |
370af2d5 | 4633 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
9fbf56f7 MM |
4634 | || !uses_template_parms (TREE_VALUE (found))) |
4635 | continue; | |
4636 | ||
4637 | /* Temporarily reduce by one the number of levels in the | |
4638 | ARGLIST and in FOUND so as to avoid comparing the | |
4639 | last set of arguments. */ | |
4640 | TREE_VEC_LENGTH (arglist)--; | |
4641 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
4642 | ||
4643 | /* See if the arguments match. If they do, then TMPL is | |
4644 | the partial instantiation we want. */ | |
4645 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
4646 | ||
4647 | /* Restore the argument vectors to their full size. */ | |
4648 | TREE_VEC_LENGTH (arglist)++; | |
4649 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
4650 | ||
4651 | if (success) | |
4652 | { | |
4653 | found = tmpl; | |
4654 | break; | |
4655 | } | |
4656 | } | |
4657 | ||
4658 | if (!found) | |
ab097535 NS |
4659 | { |
4660 | /* There was no partial instantiation. This happens | |
4661 | where C<T> is a member template of A<T> and it's used | |
4662 | in something like | |
4663 | ||
4664 | template <typename T> struct B { A<T>::C<int> m; }; | |
4665 | B<float>; | |
4666 | ||
4667 | Create the partial instantiation. | |
4668 | */ | |
4669 | TREE_VEC_LENGTH (arglist)--; | |
c2ea3a40 | 4670 | found = tsubst (template, arglist, complain, NULL_TREE); |
ab097535 | 4671 | TREE_VEC_LENGTH (arglist)++; |
ab097535 | 4672 | } |
9fbf56f7 MM |
4673 | } |
4674 | ||
ab097535 | 4675 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
dbfe2124 MM |
4676 | DECL_TEMPLATE_INSTANTIATIONS (template) |
4677 | = tree_cons (arglist, t, | |
4678 | DECL_TEMPLATE_INSTANTIATIONS (template)); | |
4679 | ||
4680 | if (TREE_CODE (t) == ENUMERAL_TYPE | |
4681 | && !is_partial_instantiation) | |
61a127b3 MM |
4682 | /* Now that the type has been registered on the instantiations |
4683 | list, we set up the enumerators. Because the enumeration | |
4684 | constants may involve the enumeration type itself, we make | |
4685 | sure to register the type first, and then create the | |
4686 | constants. That way, doing tsubst_expr for the enumeration | |
4687 | constants won't result in recursive calls here; we'll find | |
4688 | the instantiation and exit above. */ | |
4689 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 4690 | |
36a117a5 MM |
4691 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
4692 | is set up. */ | |
ed44da02 MM |
4693 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
4694 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
7813d14c | 4695 | if (is_partial_instantiation) |
077e7015 MM |
4696 | /* If the type makes use of template parameters, the |
4697 | code that generates debugging information will crash. */ | |
4698 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 4699 | |
fd295cb2 | 4700 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 4701 | } |
fd295cb2 | 4702 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
4703 | } |
4704 | \f | |
8dfaeb63 | 4705 | struct pair_fn_data |
8d08fdba | 4706 | { |
8dfaeb63 MM |
4707 | tree_fn_t fn; |
4708 | void *data; | |
0c58f841 | 4709 | struct pointer_set_t *visited; |
8dfaeb63 MM |
4710 | }; |
4711 | ||
4712 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 4713 | |
8dfaeb63 | 4714 | static tree |
350fae66 | 4715 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
4716 | { |
4717 | tree t = *tp; | |
4718 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
4719 | tree_fn_t fn = pfd->fn; | |
4720 | void *data = pfd->data; | |
4f2c9d7e | 4721 | |
2f939d94 | 4722 | if (TYPE_P (t) |
4f2c9d7e | 4723 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited)) |
8dfaeb63 | 4724 | return error_mark_node; |
581d38d0 | 4725 | |
8d08fdba MS |
4726 | switch (TREE_CODE (t)) |
4727 | { | |
8d08fdba | 4728 | case RECORD_TYPE: |
9076e292 | 4729 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 4730 | break; |
ed44da02 MM |
4731 | /* Fall through. */ |
4732 | ||
8d08fdba | 4733 | case UNION_TYPE: |
ed44da02 | 4734 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
4735 | if (!TYPE_TEMPLATE_INFO (t)) |
4736 | *walk_subtrees = 0; | |
4737 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
4f2c9d7e | 4738 | fn, data, pfd->visited)) |
8dfaeb63 MM |
4739 | return error_mark_node; |
4740 | break; | |
4741 | ||
588c2d1c | 4742 | case METHOD_TYPE: |
8dfaeb63 MM |
4743 | /* Since we're not going to walk subtrees, we have to do this |
4744 | explicitly here. */ | |
4f2c9d7e MM |
4745 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
4746 | pfd->visited)) | |
8dfaeb63 | 4747 | return error_mark_node; |
4890c2f4 | 4748 | /* Fall through. */ |
588c2d1c MM |
4749 | |
4750 | case FUNCTION_TYPE: | |
8dfaeb63 | 4751 | /* Check the return type. */ |
4f2c9d7e | 4752 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
4753 | return error_mark_node; |
4754 | ||
588c2d1c MM |
4755 | /* Check the parameter types. Since default arguments are not |
4756 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4757 | contain expressions that involve template parameters. But, | |
4758 | no-one should be looking at them yet. And, once they're | |
4759 | instantiated, they don't contain template parameters, so | |
4760 | there's no point in looking at them then, either. */ | |
4761 | { | |
4762 | tree parm; | |
4763 | ||
4764 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e MM |
4765 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
4766 | pfd->visited)) | |
8dfaeb63 | 4767 | return error_mark_node; |
5566b478 | 4768 | |
8dfaeb63 MM |
4769 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
4770 | want walk_tree walking into them itself. */ | |
4771 | *walk_subtrees = 0; | |
4772 | } | |
4773 | break; | |
3ac3d9ea | 4774 | |
a723baf1 MM |
4775 | case TYPEOF_TYPE: |
4776 | if (for_each_template_parm (TYPE_FIELDS (t), fn, data, | |
4777 | pfd->visited)) | |
4778 | return error_mark_node; | |
4779 | break; | |
4780 | ||
8d08fdba | 4781 | case FUNCTION_DECL: |
5566b478 | 4782 | case VAR_DECL: |
5566b478 | 4783 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e MM |
4784 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
4785 | pfd->visited)) | |
8dfaeb63 MM |
4786 | return error_mark_node; |
4787 | /* Fall through. */ | |
4788 | ||
8d08fdba | 4789 | case PARM_DECL: |
a723baf1 MM |
4790 | case CONST_DECL: |
4791 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
4792 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
4793 | pfd->visited)) | |
4794 | return error_mark_node; | |
050367a3 | 4795 | if (DECL_CONTEXT (t) |
4f2c9d7e MM |
4796 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
4797 | pfd->visited)) | |
8dfaeb63 MM |
4798 | return error_mark_node; |
4799 | break; | |
8d08fdba | 4800 | |
a1281f45 | 4801 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4802 | /* Record template parameters such as `T' inside `TT<T>'. */ |
4f2c9d7e | 4803 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
4804 | return error_mark_node; |
4805 | /* Fall through. */ | |
4806 | ||
a1281f45 | 4807 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 4808 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 4809 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
4810 | if (fn && (*fn)(t, data)) |
4811 | return error_mark_node; | |
4812 | else if (!fn) | |
4813 | return error_mark_node; | |
4814 | break; | |
5156628f | 4815 | |
8dfaeb63 | 4816 | case TEMPLATE_DECL: |
f4f206f4 | 4817 | /* A template template parameter is encountered. */ |
8dfaeb63 | 4818 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
4f2c9d7e | 4819 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 | 4820 | return error_mark_node; |
db5ae43f | 4821 | |
8dfaeb63 MM |
4822 | /* Already substituted template template parameter */ |
4823 | *walk_subtrees = 0; | |
4824 | break; | |
b8865407 | 4825 | |
4699c561 | 4826 | case TYPENAME_TYPE: |
4f2c9d7e MM |
4827 | if (!fn |
4828 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, | |
4829 | data, pfd->visited)) | |
8dfaeb63 MM |
4830 | return error_mark_node; |
4831 | break; | |
4699c561 | 4832 | |
8dfaeb63 MM |
4833 | case CONSTRUCTOR: |
4834 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
4835 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE | |
4f2c9d7e MM |
4836 | (TREE_TYPE (t)), fn, data, |
4837 | pfd->visited)) | |
8dfaeb63 MM |
4838 | return error_mark_node; |
4839 | break; | |
4840 | ||
b8865407 MM |
4841 | case INDIRECT_REF: |
4842 | case COMPONENT_REF: | |
4699c561 | 4843 | /* If there's no type, then this thing must be some expression |
b8865407 | 4844 | involving template parameters. */ |
4699c561 | 4845 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
4846 | return error_mark_node; |
4847 | break; | |
b8865407 | 4848 | |
42976354 BK |
4849 | case MODOP_EXPR: |
4850 | case CAST_EXPR: | |
4851 | case REINTERPRET_CAST_EXPR: | |
4852 | case CONST_CAST_EXPR: | |
4853 | case STATIC_CAST_EXPR: | |
4854 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
4855 | case ARROW_EXPR: |
4856 | case DOTSTAR_EXPR: | |
4857 | case TYPEID_EXPR: | |
40242ccf | 4858 | case PSEUDO_DTOR_EXPR: |
b8865407 | 4859 | if (!fn) |
8dfaeb63 MM |
4860 | return error_mark_node; |
4861 | break; | |
abff8e06 | 4862 | |
bd9bb3d2 MM |
4863 | case BASELINK: |
4864 | /* If we do not handle this case specially, we end up walking | |
4865 | the BINFO hierarchy, which is circular, and therefore | |
4866 | confuses walk_tree. */ | |
4867 | *walk_subtrees = 0; | |
4f2c9d7e MM |
4868 | if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data, |
4869 | pfd->visited)) | |
bd9bb3d2 MM |
4870 | return error_mark_node; |
4871 | break; | |
4872 | ||
8d08fdba | 4873 | default: |
8dfaeb63 | 4874 | break; |
8d08fdba | 4875 | } |
8dfaeb63 MM |
4876 | |
4877 | /* We didn't find any template parameters we liked. */ | |
4878 | return NULL_TREE; | |
4879 | } | |
4880 | ||
a1281f45 KL |
4881 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
4882 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
4883 | call FN with the parameter and the DATA. | |
838dfd8a | 4884 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 4885 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 4886 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 MM |
4887 | returned by for_each_template_parm is 0. If FN is NULL, it is |
4888 | considered to be the function which always returns 1. */ | |
4889 | ||
4890 | static int | |
0c58f841 MA |
4891 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
4892 | struct pointer_set_t *visited) | |
8dfaeb63 MM |
4893 | { |
4894 | struct pair_fn_data pfd; | |
ad2ae3b2 | 4895 | int result; |
8dfaeb63 MM |
4896 | |
4897 | /* Set up. */ | |
4898 | pfd.fn = fn; | |
4899 | pfd.data = data; | |
4900 | ||
4890c2f4 MM |
4901 | /* Walk the tree. (Conceptually, we would like to walk without |
4902 | duplicates, but for_each_template_parm_r recursively calls | |
4903 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
4904 | bit to use walk_tree_without_duplicates, so we keep our own |
4905 | visited list.) */ | |
4906 | if (visited) | |
4907 | pfd.visited = visited; | |
4908 | else | |
0c58f841 | 4909 | pfd.visited = pointer_set_create (); |
ad2ae3b2 MM |
4910 | result = walk_tree (&t, |
4911 | for_each_template_parm_r, | |
4912 | &pfd, | |
c1e39976 | 4913 | pfd.visited) != NULL_TREE; |
ad2ae3b2 MM |
4914 | |
4915 | /* Clean up. */ | |
4916 | if (!visited) | |
0c58f841 MA |
4917 | { |
4918 | pointer_set_destroy (pfd.visited); | |
4919 | pfd.visited = 0; | |
4920 | } | |
ad2ae3b2 MM |
4921 | |
4922 | return result; | |
8d08fdba MS |
4923 | } |
4924 | ||
d43f603d KL |
4925 | /* Returns true if T depends on any template parameter. */ |
4926 | ||
050367a3 | 4927 | int |
3a978d72 | 4928 | uses_template_parms (tree t) |
050367a3 | 4929 | { |
c353b8e3 MM |
4930 | bool dependent_p; |
4931 | int saved_processing_template_decl; | |
4932 | ||
4933 | saved_processing_template_decl = processing_template_decl; | |
4934 | if (!saved_processing_template_decl) | |
4935 | processing_template_decl = 1; | |
4936 | if (TYPE_P (t)) | |
4937 | dependent_p = dependent_type_p (t); | |
4938 | else if (TREE_CODE (t) == TREE_VEC) | |
4939 | dependent_p = any_dependent_template_arguments_p (t); | |
4940 | else if (TREE_CODE (t) == TREE_LIST) | |
4941 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
4942 | || uses_template_parms (TREE_CHAIN (t))); | |
4943 | else if (DECL_P (t) | |
4944 | || EXPR_P (t) | |
4945 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX | |
4946 | || TREE_CODE (t) == OVERLOAD | |
4947 | || TREE_CODE (t) == BASELINK | |
6615c446 | 4948 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
4949 | dependent_p = (type_dependent_expression_p (t) |
4950 | || value_dependent_expression_p (t)); | |
315fb5db NS |
4951 | else |
4952 | { | |
4953 | gcc_assert (t == error_mark_node); | |
4954 | dependent_p = false; | |
4955 | } | |
4956 | ||
c353b8e3 MM |
4957 | processing_template_decl = saved_processing_template_decl; |
4958 | ||
4959 | return dependent_p; | |
050367a3 MM |
4960 | } |
4961 | ||
d43f603d KL |
4962 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
4963 | ||
4964 | int | |
4965 | uses_template_parms_level (tree t, int level) | |
4966 | { | |
4967 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL); | |
4968 | } | |
4969 | ||
27fafc8d | 4970 | static int tinst_depth; |
e9f32eb5 | 4971 | extern int max_tinst_depth; |
5566b478 | 4972 | #ifdef GATHER_STATISTICS |
27fafc8d | 4973 | int depth_reached; |
5566b478 | 4974 | #endif |
8dfaeb63 MM |
4975 | static int tinst_level_tick; |
4976 | static int last_template_error_tick; | |
8d08fdba | 4977 | |
3ae18eaf JM |
4978 | /* We're starting to instantiate D; record the template instantiation context |
4979 | for diagnostics and to restore it later. */ | |
4980 | ||
742a37d5 | 4981 | int |
3a978d72 | 4982 | push_tinst_level (tree d) |
8d08fdba | 4983 | { |
3ae18eaf | 4984 | tree new; |
8d08fdba | 4985 | |
7215f9a0 MS |
4986 | if (tinst_depth >= max_tinst_depth) |
4987 | { | |
8adf5b5e JM |
4988 | /* If the instantiation in question still has unbound template parms, |
4989 | we don't really care if we can't instantiate it, so just return. | |
4990 | This happens with base instantiation for implicit `typename'. */ | |
4991 | if (uses_template_parms (d)) | |
4992 | return 0; | |
4993 | ||
1139b3d8 | 4994 | last_template_error_tick = tinst_level_tick; |
0f51ccfc GDR |
4995 | error ("template instantiation depth exceeds maximum of %d (use " |
4996 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", | |
b4f4233d | 4997 | max_tinst_depth, d); |
5566b478 | 4998 | |
cb753e49 | 4999 | print_instantiation_context (); |
5566b478 | 5000 | |
7215f9a0 MS |
5001 | return 0; |
5002 | } | |
5003 | ||
406d77a4 | 5004 | new = make_tinst_level (d, input_location); |
3ae18eaf | 5005 | TREE_CHAIN (new) = current_tinst_level; |
8d08fdba | 5006 | current_tinst_level = new; |
5566b478 | 5007 | |
7215f9a0 | 5008 | ++tinst_depth; |
5566b478 MS |
5009 | #ifdef GATHER_STATISTICS |
5010 | if (tinst_depth > depth_reached) | |
5011 | depth_reached = tinst_depth; | |
5012 | #endif | |
5013 | ||
27fafc8d | 5014 | ++tinst_level_tick; |
7215f9a0 | 5015 | return 1; |
8d08fdba MS |
5016 | } |
5017 | ||
3ae18eaf JM |
5018 | /* We're done instantiating this template; return to the instantiation |
5019 | context. */ | |
5020 | ||
8d08fdba | 5021 | void |
3a978d72 | 5022 | pop_tinst_level (void) |
8d08fdba | 5023 | { |
3ae18eaf | 5024 | tree old = current_tinst_level; |
8d08fdba | 5025 | |
ae58fa02 MM |
5026 | /* Restore the filename and line number stashed away when we started |
5027 | this instantiation. */ | |
406d77a4 | 5028 | input_location = TINST_LOCATION (old); |
3ae18eaf | 5029 | current_tinst_level = TREE_CHAIN (old); |
7215f9a0 | 5030 | --tinst_depth; |
27fafc8d | 5031 | ++tinst_level_tick; |
8d08fdba MS |
5032 | } |
5033 | ||
3ae18eaf JM |
5034 | /* We're instantiating a deferred template; restore the template |
5035 | instantiation context in which the instantiation was requested, which | |
5036 | is one step out from LEVEL. */ | |
5037 | ||
5038 | static void | |
3a978d72 | 5039 | reopen_tinst_level (tree level) |
3ae18eaf JM |
5040 | { |
5041 | tree t; | |
5042 | ||
5043 | tinst_depth = 0; | |
5044 | for (t = level; t; t = TREE_CHAIN (t)) | |
5045 | ++tinst_depth; | |
5046 | ||
5047 | current_tinst_level = level; | |
5048 | pop_tinst_level (); | |
5049 | } | |
5050 | ||
f84b4be9 JM |
5051 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
5052 | vector of template arguments, as for tsubst. | |
5053 | ||
dc957d14 | 5054 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
5055 | |
5056 | static tree | |
3a978d72 | 5057 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
5058 | { |
5059 | tree new_friend; | |
27fafc8d | 5060 | |
f84b4be9 JM |
5061 | if (TREE_CODE (decl) == FUNCTION_DECL |
5062 | && DECL_TEMPLATE_INSTANTIATION (decl) | |
5063 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
5064 | /* This was a friend declared with an explicit template | |
5065 | argument list, e.g.: | |
5066 | ||
5067 | friend void f<>(T); | |
5068 | ||
5069 | to indicate that f was a template instantiation, not a new | |
5070 | function declaration. Now, we have to figure out what | |
5071 | instantiation of what template. */ | |
5072 | { | |
76e57b45 | 5073 | tree template_id, arglist, fns; |
f84b4be9 JM |
5074 | tree new_args; |
5075 | tree tmpl; | |
ed2fa432 | 5076 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
76e57b45 NS |
5077 | |
5078 | /* Friend functions are looked up in the containing namespace scope. | |
5079 | We must enter that scope, to avoid finding member functions of the | |
5080 | current cless with same name. */ | |
5081 | push_nested_namespace (ns); | |
5082 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
c2ea3a40 | 5083 | tf_error | tf_warning, NULL_TREE); |
76e57b45 NS |
5084 | pop_nested_namespace (ns); |
5085 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
c2ea3a40 | 5086 | tf_error | tf_warning, NULL_TREE); |
76e57b45 NS |
5087 | template_id = lookup_template_function (fns, arglist); |
5088 | ||
c2ea3a40 | 5089 | new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); |
36a117a5 MM |
5090 | tmpl = determine_specialization (template_id, new_friend, |
5091 | &new_args, | |
5fe7b654 GB |
5092 | /*need_member_template=*/0, |
5093 | TREE_VEC_LENGTH (args)); | |
6e432b31 | 5094 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 5095 | } |
36a117a5 | 5096 | |
c2ea3a40 | 5097 | new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE); |
f84b4be9 | 5098 | |
36a117a5 | 5099 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
5100 | compiler, but is not an instantiation from the point of view of |
5101 | the language. For example, we might have had: | |
5102 | ||
5103 | template <class T> struct S { | |
5104 | template <class U> friend void f(T, U); | |
5105 | }; | |
5106 | ||
5107 | Then, in S<int>, template <class U> void f(int, U) is not an | |
5108 | instantiation of anything. */ | |
ac2b3222 AP |
5109 | if (new_friend == error_mark_node) |
5110 | return error_mark_node; | |
5111 | ||
f84b4be9 JM |
5112 | DECL_USE_TEMPLATE (new_friend) = 0; |
5113 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
5114 | { |
5115 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
5116 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
5117 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
5118 | } | |
36a117a5 | 5119 | |
92643fea MM |
5120 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
5121 | is not a template instantiation and should not be mangled like | |
5122 | one. Therefore, we forget the mangling here; we'll recompute it | |
5123 | later if we need it. */ | |
36a117a5 MM |
5124 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
5125 | { | |
19e7881c | 5126 | SET_DECL_RTL (new_friend, NULL_RTX); |
92643fea | 5127 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 MM |
5128 | } |
5129 | ||
6eb3bb27 | 5130 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 5131 | { |
36a117a5 | 5132 | tree old_decl; |
fbf1c34b MM |
5133 | tree new_friend_template_info; |
5134 | tree new_friend_result_template_info; | |
92da7074 | 5135 | tree ns; |
fbf1c34b MM |
5136 | int new_friend_is_defn; |
5137 | ||
5138 | /* We must save some information from NEW_FRIEND before calling | |
5139 | duplicate decls since that function will free NEW_FRIEND if | |
5140 | possible. */ | |
5141 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 MM |
5142 | new_friend_is_defn = |
5143 | (DECL_INITIAL (DECL_TEMPLATE_RESULT | |
5144 | (template_for_substitution (new_friend))) | |
5145 | != NULL_TREE); | |
f84b4be9 | 5146 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
5147 | { |
5148 | /* This declaration is a `primary' template. */ | |
5149 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
5150 | ||
fbf1c34b | 5151 | new_friend_result_template_info |
17aec3eb | 5152 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
5153 | } |
5154 | else | |
5bd61841 | 5155 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 5156 | |
1c227897 MM |
5157 | /* Inside pushdecl_namespace_level, we will push into the |
5158 | current namespace. However, the friend function should go | |
c6002625 | 5159 | into the namespace of the template. */ |
92da7074 ML |
5160 | ns = decl_namespace_context (new_friend); |
5161 | push_nested_namespace (ns); | |
36a117a5 | 5162 | old_decl = pushdecl_namespace_level (new_friend); |
92da7074 | 5163 | pop_nested_namespace (ns); |
36a117a5 MM |
5164 | |
5165 | if (old_decl != new_friend) | |
5166 | { | |
5167 | /* This new friend declaration matched an existing | |
5168 | declaration. For example, given: | |
5169 | ||
5170 | template <class T> void f(T); | |
5171 | template <class U> class C { | |
5172 | template <class T> friend void f(T) {} | |
5173 | }; | |
5174 | ||
5175 | the friend declaration actually provides the definition | |
5176 | of `f', once C has been instantiated for some type. So, | |
5177 | old_decl will be the out-of-class template declaration, | |
5178 | while new_friend is the in-class definition. | |
5179 | ||
5180 | But, if `f' was called before this point, the | |
5181 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
5182 | to `T' but not to `U', references to which might appear | |
5183 | in the definition of `f'. Previously, the most general | |
5184 | template for an instantiation of `f' was the out-of-class | |
5185 | version; now it is the in-class version. Therefore, we | |
5186 | run through all specialization of `f', adding to their | |
5187 | DECL_TI_ARGS appropriately. In particular, they need a | |
5188 | new set of outer arguments, corresponding to the | |
5189 | arguments for this class instantiation. | |
5190 | ||
5191 | The same situation can arise with something like this: | |
5192 | ||
5193 | friend void f(int); | |
5194 | template <class T> class C { | |
5195 | friend void f(T) {} | |
5196 | }; | |
5197 | ||
5198 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
5199 | in the class. */ | |
5200 | ||
fbf1c34b MM |
5201 | if (!new_friend_is_defn) |
5202 | /* On the other hand, if the in-class declaration does | |
5203 | *not* provide a definition, then we don't want to alter | |
5204 | existing definitions. We can just leave everything | |
5205 | alone. */ | |
36a117a5 | 5206 | ; |
fbf1c34b | 5207 | else |
36a117a5 | 5208 | { |
fbf1c34b MM |
5209 | /* Overwrite whatever template info was there before, if |
5210 | any, with the new template information pertaining to | |
5211 | the declaration. */ | |
5212 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
5213 | ||
5214 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
8d83f792 MM |
5215 | reregister_specialization (new_friend, |
5216 | most_general_template (old_decl), | |
5217 | old_decl); | |
fbf1c34b | 5218 | else |
36a117a5 | 5219 | { |
fbf1c34b MM |
5220 | tree t; |
5221 | tree new_friend_args; | |
5222 | ||
17aec3eb | 5223 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b MM |
5224 | = new_friend_result_template_info; |
5225 | ||
5226 | new_friend_args = TI_ARGS (new_friend_template_info); | |
5227 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); | |
5228 | t != NULL_TREE; | |
5229 | t = TREE_CHAIN (t)) | |
5230 | { | |
5231 | tree spec = TREE_VALUE (t); | |
36a117a5 | 5232 | |
fbf1c34b MM |
5233 | DECL_TI_ARGS (spec) |
5234 | = add_outermost_template_args (new_friend_args, | |
5235 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
5236 | } |
5237 | ||
5238 | /* Now, since specializations are always supposed to | |
5239 | hang off of the most general template, we must move | |
5240 | them. */ | |
5241 | t = most_general_template (old_decl); | |
5242 | if (t != old_decl) | |
5243 | { | |
5244 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
5245 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
5246 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
5247 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
5248 | } | |
36a117a5 MM |
5249 | } |
5250 | } | |
5251 | ||
5252 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
5253 | by duplicate_decls. */ | |
5254 | new_friend = old_decl; | |
5255 | } | |
f84b4be9 | 5256 | } |
6e432b31 | 5257 | else |
f84b4be9 | 5258 | { |
6e432b31 KL |
5259 | tree context = DECL_CONTEXT (new_friend); |
5260 | bool dependent_p; | |
5261 | ||
5262 | /* In the code | |
5263 | template <class T> class C { | |
5264 | template <class U> friend void C1<U>::f (); // case 1 | |
5265 | friend void C2<T>::f (); // case 2 | |
5266 | }; | |
5267 | we only need to make sure CONTEXT is a complete type for | |
5268 | case 2. To distinguish between the two cases, we note that | |
5269 | CONTEXT of case 1 remains dependent type after tsubst while | |
5270 | this isn't true for case 2. */ | |
5271 | ++processing_template_decl; | |
5272 | dependent_p = dependent_type_p (context); | |
5273 | --processing_template_decl; | |
5274 | ||
5275 | if (!dependent_p | |
5276 | && !complete_type_or_else (context, NULL_TREE)) | |
5277 | return error_mark_node; | |
5278 | ||
5279 | if (COMPLETE_TYPE_P (context)) | |
5280 | { | |
5281 | /* Check to see that the declaration is really present, and, | |
5282 | possibly obtain an improved declaration. */ | |
5283 | tree fn = check_classfn (context, | |
5284 | new_friend, NULL_TREE); | |
5285 | ||
5286 | if (fn) | |
5287 | new_friend = fn; | |
5288 | } | |
f84b4be9 JM |
5289 | } |
5290 | ||
5291 | return new_friend; | |
5292 | } | |
5293 | ||
1aed5355 MM |
5294 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
5295 | template arguments, as for tsubst. | |
6757edfe | 5296 | |
dc957d14 | 5297 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 5298 | failure. */ |
6757edfe MM |
5299 | |
5300 | static tree | |
3a978d72 | 5301 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 5302 | { |
1aed5355 | 5303 | tree friend_type; |
25aab5d0 | 5304 | tree tmpl; |
3e0ec82f | 5305 | tree context; |
6757edfe | 5306 | |
3e0ec82f MM |
5307 | context = DECL_CONTEXT (friend_tmpl); |
5308 | ||
5309 | if (context) | |
77adef84 | 5310 | { |
3e0ec82f MM |
5311 | if (TREE_CODE (context) == NAMESPACE_DECL) |
5312 | push_nested_namespace (context); | |
5313 | else | |
14d22dd6 | 5314 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 5315 | } |
25aab5d0 | 5316 | |
3e0ec82f MM |
5317 | /* First, we look for a class template. */ |
5318 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0); | |
25aab5d0 | 5319 | |
3e0ec82f MM |
5320 | /* But, if we don't find one, it might be because we're in a |
5321 | situation like this: | |
77adef84 | 5322 | |
3e0ec82f MM |
5323 | template <class T> |
5324 | struct S { | |
5325 | template <class U> | |
5326 | friend struct S; | |
5327 | }; | |
5328 | ||
5329 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
5330 | for `S<int>', not the TEMPLATE_DECL. */ | |
5331 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
5332 | { | |
5333 | tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1); | |
5334 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); | |
25aab5d0 | 5335 | } |
6757edfe | 5336 | |
25aab5d0 | 5337 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
5338 | { |
5339 | /* The friend template has already been declared. Just | |
36a117a5 MM |
5340 | check to see that the declarations match, and install any new |
5341 | default parameters. We must tsubst the default parameters, | |
5342 | of course. We only need the innermost template parameters | |
5343 | because that is all that redeclare_class_template will look | |
5344 | at. */ | |
3e0ec82f MM |
5345 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
5346 | > TMPL_ARGS_DEPTH (args)) | |
5347 | { | |
5348 | tree parms; | |
5349 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
5350 | args, tf_error | tf_warning); | |
5351 | redeclare_class_template (TREE_TYPE (tmpl), parms); | |
5352 | } | |
5353 | ||
6757edfe MM |
5354 | friend_type = TREE_TYPE (tmpl); |
5355 | } | |
5356 | else | |
5357 | { | |
5358 | /* The friend template has not already been declared. In this | |
5359 | case, the instantiation of the template class will cause the | |
5360 | injection of this template into the global scope. */ | |
c2ea3a40 | 5361 | tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE); |
6757edfe MM |
5362 | |
5363 | /* The new TMPL is not an instantiation of anything, so we | |
5364 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for | |
5365 | the new type because that is supposed to be the corresponding | |
5366 | template decl, i.e., TMPL. */ | |
5367 | DECL_USE_TEMPLATE (tmpl) = 0; | |
5368 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
5369 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
5370 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
5371 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
5372 | |
5373 | /* Inject this template into the global scope. */ | |
5374 | friend_type = TREE_TYPE (pushdecl_top_level (tmpl)); | |
5375 | } | |
5376 | ||
3e0ec82f MM |
5377 | if (context) |
5378 | { | |
5379 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
5380 | pop_nested_namespace (context); | |
5381 | else | |
5382 | pop_nested_class (); | |
5383 | } | |
5384 | ||
6757edfe MM |
5385 | return friend_type; |
5386 | } | |
f84b4be9 | 5387 | |
17f29637 KL |
5388 | /* Returns zero if TYPE cannot be completed later due to circularity. |
5389 | Otherwise returns one. */ | |
5390 | ||
d5372501 | 5391 | static int |
3a978d72 | 5392 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
5393 | { |
5394 | if (type == NULL_TREE || type == error_mark_node) | |
5395 | return 0; | |
5396 | else if (COMPLETE_TYPE_P (type)) | |
5397 | return 1; | |
5398 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
5399 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
5400 | else if (CLASS_TYPE_P (type) |
5401 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
5402 | return 0; |
5403 | else | |
5404 | return 1; | |
5405 | } | |
5406 | ||
8d08fdba | 5407 | tree |
3a978d72 | 5408 | instantiate_class_template (tree type) |
8d08fdba | 5409 | { |
7088fca9 | 5410 | tree template, args, pattern, t, member; |
36a117a5 | 5411 | tree typedecl; |
dbbf88d1 | 5412 | tree pbinfo; |
cad7e87b | 5413 | tree base_list; |
dbbf88d1 | 5414 | |
5566b478 | 5415 | if (type == error_mark_node) |
8d08fdba MS |
5416 | return error_mark_node; |
5417 | ||
ca099ac8 MM |
5418 | if (TYPE_BEING_DEFINED (type) |
5419 | || COMPLETE_TYPE_P (type) | |
5420 | || dependent_type_p (type)) | |
5566b478 MS |
5421 | return type; |
5422 | ||
6bdb985f | 5423 | /* Figure out which template is being instantiated. */ |
36a117a5 | 5424 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
50bc768d | 5425 | gcc_assert (TREE_CODE (template) == TEMPLATE_DECL); |
73aad9b9 | 5426 | |
6bdb985f MM |
5427 | /* Figure out which arguments are being used to do the |
5428 | instantiation. */ | |
5429 | args = CLASSTYPE_TI_ARGS (type); | |
4c571114 MM |
5430 | |
5431 | /* Determine what specialization of the original template to | |
5432 | instantiate. */ | |
8fbc5ae7 MM |
5433 | t = most_specialized_class (template, args); |
5434 | if (t == error_mark_node) | |
73aad9b9 | 5435 | { |
8fbc5ae7 | 5436 | const char *str = "candidates are:"; |
0f51ccfc | 5437 | error ("ambiguous class template instantiation for %q#T", type); |
8fbc5ae7 MM |
5438 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; |
5439 | t = TREE_CHAIN (t)) | |
73aad9b9 | 5440 | { |
8c6ab2db | 5441 | if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args)) |
73aad9b9 | 5442 | { |
8fbc5ae7 MM |
5443 | cp_error_at ("%s %+#T", str, TREE_TYPE (t)); |
5444 | str = " "; | |
73aad9b9 JM |
5445 | } |
5446 | } | |
8fbc5ae7 MM |
5447 | TYPE_BEING_DEFINED (type) = 1; |
5448 | return error_mark_node; | |
73aad9b9 | 5449 | } |
6bdb985f MM |
5450 | |
5451 | if (t) | |
73aad9b9 JM |
5452 | pattern = TREE_TYPE (t); |
5453 | else | |
5454 | pattern = TREE_TYPE (template); | |
5566b478 | 5455 | |
4c571114 MM |
5456 | /* If the template we're instantiating is incomplete, then clearly |
5457 | there's nothing we can do. */ | |
d0f062fb | 5458 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 5459 | return type; |
5566b478 | 5460 | |
4c571114 MM |
5461 | /* If we've recursively instantiated too many templates, stop. */ |
5462 | if (! push_tinst_level (type)) | |
f31c0a32 | 5463 | return type; |
4c571114 MM |
5464 | |
5465 | /* Now we're really doing the instantiation. Mark the type as in | |
5466 | the process of being defined. */ | |
5467 | TYPE_BEING_DEFINED (type) = 1; | |
5468 | ||
78757caa KL |
5469 | /* We may be in the middle of deferred access check. Disable |
5470 | it now. */ | |
5471 | push_deferring_access_checks (dk_no_deferred); | |
5472 | ||
c353b8e3 | 5473 | push_to_top_level (); |
4c571114 | 5474 | |
73aad9b9 | 5475 | if (t) |
36a117a5 | 5476 | { |
27631dae | 5477 | /* This TYPE is actually an instantiation of a partial |
36a117a5 MM |
5478 | specialization. We replace the innermost set of ARGS with |
5479 | the arguments appropriate for substitution. For example, | |
5480 | given: | |
5481 | ||
5482 | template <class T> struct S {}; | |
5483 | template <class T> struct S<T*> {}; | |
5484 | ||
5485 | and supposing that we are instantiating S<int*>, ARGS will | |
5486 | present be {int*} but we need {int}. */ | |
5487 | tree inner_args | |
5488 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), | |
5489 | args); | |
5490 | ||
5491 | /* If there were multiple levels in ARGS, replacing the | |
5492 | innermost level would alter CLASSTYPE_TI_ARGS, which we don't | |
5493 | want, so we make a copy first. */ | |
5494 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
5495 | { | |
5496 | args = copy_node (args); | |
5497 | SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args); | |
5498 | } | |
5499 | else | |
5500 | args = inner_args; | |
5501 | } | |
8d019cef | 5502 | |
7813d14c | 5503 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 5504 | |
68ea098a NS |
5505 | /* Set the input location to the template definition. This is needed |
5506 | if tsubsting causes an error. */ | |
f31686a3 | 5507 | input_location = DECL_SOURCE_LOCATION (TYPE_NAME (pattern)); |
68ea098a | 5508 | |
f7da6097 | 5509 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
834c6dff MM |
5510 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
5511 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 5512 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
f7da6097 MS |
5513 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
5514 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
f7da6097 MS |
5515 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
5516 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
5517 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
5518 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
5519 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
5520 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 5521 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 5522 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
5523 | if (ANON_AGGR_TYPE_P (pattern)) |
5524 | SET_ANON_AGGR_TYPE_P (type); | |
f7da6097 | 5525 | |
dbbf88d1 | 5526 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 5527 | |
315fb5db NS |
5528 | /* We should never instantiate a nested class before its enclosing |
5529 | class; we need to look up the nested class by name before we can | |
5530 | instantiate it, and that lookup should instantiate the enclosing | |
5531 | class. */ | |
5532 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
5533 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
5534 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
1456deaf | 5535 | |
cad7e87b | 5536 | base_list = NULL_TREE; |
fa743e8c | 5537 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 5538 | { |
fa743e8c | 5539 | tree pbase_binfo; |
a2507277 | 5540 | tree context = TYPE_CONTEXT (type); |
4514aa8c | 5541 | tree pushed_scope; |
3fd71a52 | 5542 | int i; |
5566b478 | 5543 | |
a2507277 NS |
5544 | /* We must enter the scope containing the type, as that is where |
5545 | the accessibility of types named in dependent bases are | |
5546 | looked up from. */ | |
4514aa8c | 5547 | pushed_scope = push_scope (context ? context : global_namespace); |
a2507277 | 5548 | |
3fd71a52 MM |
5549 | /* Substitute into each of the bases to determine the actual |
5550 | basetypes. */ | |
fa743e8c | 5551 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
5552 | { |
5553 | tree base; | |
fa743e8c | 5554 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
711734a9 | 5555 | |
dc957d14 | 5556 | /* Substitute to figure out the base class. */ |
fa743e8c | 5557 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE); |
3fd71a52 MM |
5558 | if (base == error_mark_node) |
5559 | continue; | |
dbbf88d1 | 5560 | |
3fd71a52 | 5561 | base_list = tree_cons (access, base, base_list); |
fa743e8c | 5562 | if (BINFO_VIRTUAL_P (pbase_binfo)) |
809e3e7f | 5563 | TREE_TYPE (base_list) = integer_type_node; |
3fd71a52 | 5564 | } |
dfbcd65a | 5565 | |
3fd71a52 MM |
5566 | /* The list is now in reverse order; correct that. */ |
5567 | base_list = nreverse (base_list); | |
5568 | ||
4514aa8c NS |
5569 | if (pushed_scope) |
5570 | pop_scope (pushed_scope); | |
3fd71a52 | 5571 | } |
cad7e87b NS |
5572 | /* Now call xref_basetypes to set up all the base-class |
5573 | information. */ | |
5574 | xref_basetypes (type, base_list); | |
5575 | ||
5566b478 | 5576 | |
b74a0560 MM |
5577 | /* Now that our base classes are set up, enter the scope of the |
5578 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 5579 | correctly. This is precisely analogous to what we do in |
b74a0560 MM |
5580 | begin_class_definition when defining an ordinary non-template |
5581 | class. */ | |
29370796 | 5582 | pushclass (type); |
b74a0560 | 5583 | |
7088fca9 | 5584 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
5585 | for (member = CLASSTYPE_DECL_LIST (pattern); |
5586 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 5587 | { |
7088fca9 | 5588 | tree t = TREE_VALUE (member); |
5566b478 | 5589 | |
7088fca9 | 5590 | if (TREE_PURPOSE (member)) |
ed44da02 | 5591 | { |
7088fca9 KL |
5592 | if (TYPE_P (t)) |
5593 | { | |
5e0c54e5 | 5594 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 5595 | |
7088fca9 KL |
5596 | tree tag = t; |
5597 | tree name = TYPE_IDENTIFIER (tag); | |
5598 | tree newtag; | |
883a2bff MM |
5599 | bool class_template_p; |
5600 | ||
5601 | class_template_p = (TREE_CODE (tag) != ENUMERAL_TYPE | |
5602 | && TYPE_LANG_SPECIFIC (tag) | |
5603 | && CLASSTYPE_IS_TEMPLATE (tag)); | |
5604 | /* If the member is a class template, then -- even after | |
6c745393 | 5605 | substitution -- there may be dependent types in the |
883a2bff MM |
5606 | template argument list for the class. We increment |
5607 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
5608 | that function will assume that no types are dependent | |
5609 | when outside of a template. */ | |
5610 | if (class_template_p) | |
5611 | ++processing_template_decl; | |
7088fca9 | 5612 | newtag = tsubst (tag, args, tf_error, NULL_TREE); |
883a2bff MM |
5613 | if (class_template_p) |
5614 | --processing_template_decl; | |
2620d095 KL |
5615 | if (newtag == error_mark_node) |
5616 | continue; | |
5617 | ||
7088fca9 KL |
5618 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
5619 | { | |
883a2bff | 5620 | if (class_template_p) |
7088fca9 KL |
5621 | /* Unfortunately, lookup_template_class sets |
5622 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
5623 | instantiation (i.e., for the type of a member |
5624 | template class nested within a template class.) | |
5625 | This behavior is required for | |
5626 | maybe_process_partial_specialization to work | |
5627 | correctly, but is not accurate in this case; | |
5628 | the TAG is not an instantiation of anything. | |
5629 | (The corresponding TEMPLATE_DECL is an | |
5630 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
5631 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
5632 | ||
5633 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
5634 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
5635 | pushtag calling push_template_decl. We don't have to do | |
5636 | this for enums because it will already have been done in | |
5637 | tsubst_enum. */ | |
5638 | if (name) | |
5639 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
bd3d082e | 5640 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7088fca9 KL |
5641 | } |
5642 | } | |
5643 | else if (TREE_CODE (t) == FUNCTION_DECL | |
5644 | || DECL_FUNCTION_TEMPLATE_P (t)) | |
5645 | { | |
5646 | /* Build new TYPE_METHODS. */ | |
fecafe5e NS |
5647 | tree r; |
5648 | ||
5649 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 5650 | ++processing_template_decl; |
fecafe5e NS |
5651 | r = tsubst (t, args, tf_error, NULL_TREE); |
5652 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 5653 | --processing_template_decl; |
7088fca9 KL |
5654 | set_current_access_from_decl (r); |
5655 | grok_special_member_properties (r); | |
5656 | finish_member_declaration (r); | |
5657 | } | |
5658 | else | |
5659 | { | |
5660 | /* Build new TYPE_FIELDS. */ | |
fa8d6e85 | 5661 | |
7088fca9 KL |
5662 | if (TREE_CODE (t) != CONST_DECL) |
5663 | { | |
5664 | tree r; | |
fa8d6e85 | 5665 | |
d479d37f NS |
5666 | /* The the file and line for this declaration, to |
5667 | assist in error message reporting. Since we | |
5668 | called push_tinst_level above, we don't need to | |
5669 | restore these. */ | |
f31686a3 | 5670 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 5671 | |
fb5ce3c9 | 5672 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 5673 | ++processing_template_decl; |
7088fca9 | 5674 | r = tsubst (t, args, tf_error | tf_warning, NULL_TREE); |
fb5ce3c9 | 5675 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 5676 | --processing_template_decl; |
7088fca9 KL |
5677 | if (TREE_CODE (r) == VAR_DECL) |
5678 | { | |
5679 | tree init; | |
17f29637 | 5680 | |
7088fca9 KL |
5681 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
5682 | init = tsubst_expr (DECL_INITIAL (t), args, | |
5683 | tf_error | tf_warning, NULL_TREE); | |
5684 | else | |
5685 | init = NULL_TREE; | |
8d08fdba | 5686 | |
8c6ab2db NS |
5687 | finish_static_data_member_decl |
5688 | (r, init, /*asmspec_tree=*/NULL_TREE, /*flags=*/0); | |
8d08fdba | 5689 | |
7088fca9 KL |
5690 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
5691 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5692 | } | |
5693 | else if (TREE_CODE (r) == FIELD_DECL) | |
5694 | { | |
5695 | /* Determine whether R has a valid type and can be | |
5696 | completed later. If R is invalid, then it is | |
5697 | replaced by error_mark_node so that it will not be | |
5698 | added to TYPE_FIELDS. */ | |
5699 | tree rtype = TREE_TYPE (r); | |
5700 | if (can_complete_type_without_circularity (rtype)) | |
5701 | complete_type (rtype); | |
5702 | ||
5703 | if (!COMPLETE_TYPE_P (rtype)) | |
5704 | { | |
5705 | cxx_incomplete_type_error (r, rtype); | |
5706 | r = error_mark_node; | |
5707 | } | |
5708 | } | |
5566b478 | 5709 | |
7088fca9 KL |
5710 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
5711 | such a thing will already have been added to the field | |
5712 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 5713 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
5714 | if (!(TREE_CODE (r) == TYPE_DECL |
5715 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 5716 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
5717 | { |
5718 | set_current_access_from_decl (r); | |
5719 | finish_member_declaration (r); | |
5720 | } | |
5721 | } | |
5722 | } | |
61fc8c9e | 5723 | } |
7088fca9 KL |
5724 | else |
5725 | { | |
5726 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
5727 | { | |
5728 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
5729 | ||
5730 | tree friend_type = t; | |
b939a023 | 5731 | bool adjust_processing_template_decl = false; |
1aed5355 | 5732 | |
7088fca9 | 5733 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 | 5734 | { |
5a24482e | 5735 | /* template <class T> friend class C; */ |
b939a023 KL |
5736 | friend_type = tsubst_friend_class (friend_type, args); |
5737 | adjust_processing_template_decl = true; | |
5738 | } | |
5739 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
5740 | { | |
5a24482e | 5741 | /* template <class T> friend class C::D; */ |
b939a023 KL |
5742 | friend_type = tsubst (friend_type, args, |
5743 | tf_error | tf_warning, NULL_TREE); | |
5744 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) | |
5745 | friend_type = TREE_TYPE (friend_type); | |
5746 | adjust_processing_template_decl = true; | |
5747 | } | |
5748 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
5749 | { | |
5a24482e KL |
5750 | /* This could be either |
5751 | ||
5752 | friend class T::C; | |
5753 | ||
5754 | when dependent_type_p is false or | |
5755 | ||
5756 | template <class U> friend class T::C; | |
5757 | ||
5758 | otherwise. */ | |
b939a023 KL |
5759 | friend_type = tsubst (friend_type, args, |
5760 | tf_error | tf_warning, NULL_TREE); | |
5761 | /* Bump processing_template_decl for correct | |
5762 | dependent_type_p calculation. */ | |
5763 | ++processing_template_decl; | |
5764 | if (dependent_type_p (friend_type)) | |
5765 | adjust_processing_template_decl = true; | |
5766 | --processing_template_decl; | |
5767 | } | |
5a24482e KL |
5768 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
5769 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7088fca9 | 5770 | { |
5a24482e KL |
5771 | /* friend class C; |
5772 | ||
5773 | where C hasn't been declared yet. Let's lookup name | |
5774 | from namespace scope directly, bypassing any name that | |
5775 | come from dependent base class. */ | |
7088fca9 KL |
5776 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
5777 | ||
5778 | /* The call to xref_tag_from_type does injection for friend | |
5779 | classes. */ | |
5780 | push_nested_namespace (ns); | |
b939a023 | 5781 | friend_type = |
29ef83de | 5782 | xref_tag_from_type (friend_type, NULL_TREE, |
5a24482e | 5783 | /*tag_scope=*/ts_current); |
7088fca9 KL |
5784 | pop_nested_namespace (ns); |
5785 | } | |
5a24482e KL |
5786 | else if (uses_template_parms (friend_type)) |
5787 | /* friend class C<T>; */ | |
5788 | friend_type = tsubst (friend_type, args, | |
5789 | tf_error | tf_warning, NULL_TREE); | |
5790 | /* Otherwise it's | |
5791 | ||
5792 | friend class C; | |
5793 | ||
5794 | where C is already declared or | |
5795 | ||
5796 | friend class C<int>; | |
5797 | ||
5798 | We don't have to do anything in these cases. */ | |
1aed5355 | 5799 | |
b939a023 | 5800 | if (adjust_processing_template_decl) |
7088fca9 KL |
5801 | /* Trick make_friend_class into realizing that the friend |
5802 | we're adding is a template, not an ordinary class. It's | |
5803 | important that we use make_friend_class since it will | |
5804 | perform some error-checking and output cross-reference | |
5805 | information. */ | |
5806 | ++processing_template_decl; | |
fc378698 | 5807 | |
b939a023 KL |
5808 | if (friend_type != error_mark_node) |
5809 | make_friend_class (type, friend_type, /*complain=*/false); | |
fc378698 | 5810 | |
b939a023 | 5811 | if (adjust_processing_template_decl) |
7088fca9 KL |
5812 | --processing_template_decl; |
5813 | } | |
5814 | else | |
9579624e KL |
5815 | { |
5816 | /* Build new DECL_FRIENDLIST. */ | |
5817 | tree r; | |
5818 | ||
6e432b31 KL |
5819 | /* The the file and line for this declaration, to |
5820 | assist in error message reporting. Since we | |
5821 | called push_tinst_level above, we don't need to | |
5822 | restore these. */ | |
5823 | input_location = DECL_SOURCE_LOCATION (t); | |
5824 | ||
9579624e | 5825 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
5826 | { |
5827 | ++processing_template_decl; | |
5828 | push_deferring_access_checks (dk_no_check); | |
5829 | } | |
5830 | ||
9579624e | 5831 | r = tsubst_friend_function (t, args); |
9579624e | 5832 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
5833 | if (TREE_CODE (t) == TEMPLATE_DECL) |
5834 | { | |
5835 | pop_deferring_access_checks (); | |
5836 | --processing_template_decl; | |
5837 | } | |
9579624e | 5838 | } |
7088fca9 KL |
5839 | } |
5840 | } | |
5566b478 | 5841 | |
61a127b3 MM |
5842 | /* Set the file and line number information to whatever is given for |
5843 | the class itself. This puts error messages involving generated | |
5844 | implicit functions at a predictable point, and the same point | |
5845 | that would be used for non-template classes. */ | |
7088fca9 | 5846 | typedecl = TYPE_MAIN_DECL (type); |
f31686a3 | 5847 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 5848 | |
61a127b3 | 5849 | unreverse_member_declarations (type); |
9f33663b | 5850 | finish_struct_1 (type); |
5524676d | 5851 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 5852 | |
9188c363 MM |
5853 | /* Now that the class is complete, instantiate default arguments for |
5854 | any member functions. We don't do this earlier because the | |
5855 | default arguments may reference members of the class. */ | |
5856 | if (!PRIMARY_TEMPLATE_P (template)) | |
5857 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
5858 | if (TREE_CODE (t) == FUNCTION_DECL | |
dc957d14 | 5859 | /* Implicitly generated member functions will not have template |
9188c363 MM |
5860 | information; they are not instantiations, but instead are |
5861 | created "fresh" for each instantiation. */ | |
5862 | && DECL_TEMPLATE_INFO (t)) | |
5863 | tsubst_default_arguments (t); | |
5864 | ||
b74a0560 | 5865 | popclass (); |
5566b478 | 5866 | pop_from_top_level (); |
78757caa | 5867 | pop_deferring_access_checks (); |
5566b478 MS |
5868 | pop_tinst_level (); |
5869 | ||
4684cd27 MM |
5870 | /* The vtable for a template class can be emitted in any translation |
5871 | unit in which the class is instantiated. When there is no key | |
5872 | method, however, finish_struct_1 will already have added TYPE to | |
5873 | the keyed_classes list. */ | |
5874 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
5875 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
5876 | ||
5566b478 | 5877 | return type; |
8d08fdba MS |
5878 | } |
5879 | ||
00d3396f | 5880 | static tree |
a91db711 | 5881 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 5882 | { |
a91db711 NS |
5883 | tree r; |
5884 | ||
5885 | if (!t) | |
5886 | r = t; | |
5887 | else if (TYPE_P (t)) | |
5888 | r = tsubst (t, args, complain, in_decl); | |
5889 | else | |
00d3396f | 5890 | { |
a91db711 NS |
5891 | r = tsubst_expr (t, args, complain, in_decl); |
5892 | ||
5893 | if (!uses_template_parms (r)) | |
4d5f3fbd | 5894 | { |
a91db711 NS |
5895 | /* Sometimes, one of the args was an expression involving a |
5896 | template constant parameter, like N - 1. Now that we've | |
5897 | tsubst'd, we might have something like 2 - 1. This will | |
5898 | confuse lookup_template_class, so we do constant folding | |
5899 | here. We have to unset processing_template_decl, to fool | |
5900 | tsubst_copy_and_build() into building an actual tree. */ | |
5901 | ||
5902 | /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already | |
5903 | as simple as it's going to get, and trying to reprocess | |
5904 | the trees will break. Once tsubst_expr et al DTRT for | |
5905 | non-dependent exprs, this code can go away, as the type | |
5906 | will always be set. */ | |
5907 | if (!TREE_TYPE (r)) | |
5908 | { | |
5909 | int saved_processing_template_decl = processing_template_decl; | |
5910 | processing_template_decl = 0; | |
5911 | r = tsubst_copy_and_build (r, /*args=*/NULL_TREE, | |
5912 | tf_error, /*in_decl=*/NULL_TREE, | |
5913 | /*function_p=*/false); | |
5914 | processing_template_decl = saved_processing_template_decl; | |
5915 | } | |
5916 | r = fold (r); | |
4d5f3fbd | 5917 | } |
bd83b409 | 5918 | } |
a91db711 | 5919 | return r; |
bd83b409 NS |
5920 | } |
5921 | ||
a91db711 | 5922 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 5923 | |
e9659ab0 | 5924 | static tree |
a91db711 | 5925 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 5926 | { |
bf12d54d | 5927 | int len = TREE_VEC_LENGTH (t); |
a91db711 | 5928 | int need_new = 0, i; |
c68b0a84 | 5929 | tree *elts = alloca (len * sizeof (tree)); |
830bfa74 | 5930 | |
830bfa74 MM |
5931 | for (i = 0; i < len; i++) |
5932 | { | |
bf12d54d NS |
5933 | tree orig_arg = TREE_VEC_ELT (t, i); |
5934 | tree new_arg; | |
a91db711 | 5935 | |
bf12d54d NS |
5936 | if (TREE_CODE (orig_arg) == TREE_VEC) |
5937 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
830bfa74 | 5938 | else |
a91db711 | 5939 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
830bfa74 | 5940 | |
a91db711 | 5941 | if (new_arg == error_mark_node) |
08e72a19 JM |
5942 | return error_mark_node; |
5943 | ||
a91db711 NS |
5944 | elts[i] = new_arg; |
5945 | if (new_arg != orig_arg) | |
830bfa74 MM |
5946 | need_new = 1; |
5947 | } | |
5948 | ||
5949 | if (!need_new) | |
5950 | return t; | |
a91db711 | 5951 | |
bf12d54d NS |
5952 | t = make_tree_vec (len); |
5953 | for (i = 0; i < len; i++) | |
5954 | TREE_VEC_ELT (t, i) = elts[i]; | |
830bfa74 MM |
5955 | |
5956 | return t; | |
5957 | } | |
5958 | ||
36a117a5 MM |
5959 | /* Return the result of substituting ARGS into the template parameters |
5960 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
5961 | PARMS, then the result will contain n levels of PARMS. For | |
5962 | example, if PARMS is `template <class T> template <class U> | |
5963 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
5964 | result will be `template <int*, double, class V>'. */ | |
5965 | ||
e9659ab0 | 5966 | static tree |
3a978d72 | 5967 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 5968 | { |
f71f87f9 MM |
5969 | tree r = NULL_TREE; |
5970 | tree* new_parms; | |
36a117a5 MM |
5971 | |
5972 | for (new_parms = &r; | |
5973 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
5974 | new_parms = &(TREE_CHAIN (*new_parms)), | |
5975 | parms = TREE_CHAIN (parms)) | |
5976 | { | |
5977 | tree new_vec = | |
5978 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); | |
5979 | int i; | |
5980 | ||
5981 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) | |
5982 | { | |
833aa4c4 NS |
5983 | tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); |
5984 | tree default_value = TREE_PURPOSE (tuple); | |
5985 | tree parm_decl = TREE_VALUE (tuple); | |
5986 | ||
5987 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
a91db711 NS |
5988 | default_value = tsubst_template_arg (default_value, args, |
5989 | complain, NULL_TREE); | |
5990 | ||
5991 | tuple = build_tree_list (default_value, parm_decl); | |
833aa4c4 | 5992 | TREE_VEC_ELT (new_vec, i) = tuple; |
36a117a5 MM |
5993 | } |
5994 | ||
5995 | *new_parms = | |
4890c2f4 MM |
5996 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) |
5997 | - TMPL_ARGS_DEPTH (args)), | |
36a117a5 MM |
5998 | new_vec, NULL_TREE); |
5999 | } | |
6000 | ||
6001 | return r; | |
6002 | } | |
6003 | ||
ed44da02 MM |
6004 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
6005 | type T. If T is not an aggregate or enumeration type, it is | |
6006 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 6007 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 6008 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 6009 | |
e9659ab0 | 6010 | static tree |
3a978d72 NN |
6011 | tsubst_aggr_type (tree t, |
6012 | tree args, | |
6013 | tsubst_flags_t complain, | |
6014 | tree in_decl, | |
6015 | int entering_scope) | |
36a117a5 MM |
6016 | { |
6017 | if (t == NULL_TREE) | |
6018 | return NULL_TREE; | |
6019 | ||
6020 | switch (TREE_CODE (t)) | |
6021 | { | |
6022 | case RECORD_TYPE: | |
6023 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 6024 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 6025 | |
f4f206f4 | 6026 | /* Else fall through. */ |
ed44da02 | 6027 | case ENUMERAL_TYPE: |
36a117a5 | 6028 | case UNION_TYPE: |
5db698f6 | 6029 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
6030 | { |
6031 | tree argvec; | |
6032 | tree context; | |
6033 | tree r; | |
6034 | ||
6035 | /* First, determine the context for the type we are looking | |
6036 | up. */ | |
4f7847ca NS |
6037 | context = TYPE_CONTEXT (t); |
6038 | if (context) | |
6039 | context = tsubst_aggr_type (context, args, complain, | |
36a117a5 | 6040 | in_decl, /*entering_scope=*/1); |
36a117a5 MM |
6041 | |
6042 | /* Then, figure out what arguments are appropriate for the | |
6043 | type we are trying to find. For example, given: | |
6044 | ||
6045 | template <class T> struct S; | |
6046 | template <class T, class U> void f(T, U) { S<U> su; } | |
6047 | ||
6048 | and supposing that we are instantiating f<int, double>, | |
6049 | then our ARGS will be {int, double}, but, when looking up | |
6050 | S we only want {double}. */ | |
a91db711 NS |
6051 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
6052 | complain, in_decl); | |
08e72a19 JM |
6053 | if (argvec == error_mark_node) |
6054 | return error_mark_node; | |
36a117a5 MM |
6055 | |
6056 | r = lookup_template_class (t, argvec, in_decl, context, | |
f9c244b8 | 6057 | entering_scope, complain); |
36a117a5 | 6058 | |
c2ea3a40 | 6059 | return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
36a117a5 MM |
6060 | } |
6061 | else | |
6062 | /* This is not a template type, so there's nothing to do. */ | |
6063 | return t; | |
6064 | ||
6065 | default: | |
4393e105 | 6066 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
6067 | } |
6068 | } | |
6069 | ||
9188c363 MM |
6070 | /* Substitute into the default argument ARG (a default argument for |
6071 | FN), which has the indicated TYPE. */ | |
6072 | ||
6073 | tree | |
3a978d72 | 6074 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 6075 | { |
2436b51f MM |
6076 | tree saved_class_ptr = NULL_TREE; |
6077 | tree saved_class_ref = NULL_TREE; | |
6078 | ||
9188c363 MM |
6079 | /* This default argument came from a template. Instantiate the |
6080 | default argument here, not in tsubst. In the case of | |
6081 | something like: | |
6082 | ||
6083 | template <class T> | |
6084 | struct S { | |
6085 | static T t(); | |
6086 | void f(T = t()); | |
6087 | }; | |
6088 | ||
6089 | we must be careful to do name lookup in the scope of S<T>, | |
d5a10cf0 | 6090 | rather than in the current class. */ |
2b59fc25 | 6091 | push_access_scope (fn); |
c6671cbb MM |
6092 | /* The default argument expression should not be considered to be |
6093 | within the scope of FN. Since push_access_scope sets | |
6094 | current_function_decl, we must explicitly clear it here. */ | |
6095 | current_function_decl = NULL_TREE; | |
2436b51f MM |
6096 | /* The "this" pointer is not valid in a default argument. */ |
6097 | if (cfun) | |
6098 | { | |
6099 | saved_class_ptr = current_class_ptr; | |
6100 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
6101 | saved_class_ref = current_class_ref; | |
6102 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
6103 | } | |
9188c363 | 6104 | |
d5a10cf0 | 6105 | push_deferring_access_checks(dk_no_deferred); |
c2ea3a40 NS |
6106 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
6107 | tf_error | tf_warning, NULL_TREE); | |
d5a10cf0 MM |
6108 | pop_deferring_access_checks(); |
6109 | ||
2436b51f MM |
6110 | /* Restore the "this" pointer. */ |
6111 | if (cfun) | |
6112 | { | |
6113 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
6114 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
6115 | } | |
6116 | ||
2b59fc25 | 6117 | pop_access_scope (fn); |
9188c363 MM |
6118 | |
6119 | /* Make sure the default argument is reasonable. */ | |
6120 | arg = check_default_argument (type, arg); | |
6121 | ||
6122 | return arg; | |
6123 | } | |
6124 | ||
6125 | /* Substitute into all the default arguments for FN. */ | |
6126 | ||
6127 | static void | |
3a978d72 | 6128 | tsubst_default_arguments (tree fn) |
9188c363 MM |
6129 | { |
6130 | tree arg; | |
6131 | tree tmpl_args; | |
6132 | ||
6133 | tmpl_args = DECL_TI_ARGS (fn); | |
6134 | ||
6135 | /* If this function is not yet instantiated, we certainly don't need | |
6136 | its default arguments. */ | |
6137 | if (uses_template_parms (tmpl_args)) | |
6138 | return; | |
6139 | ||
6140 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
6141 | arg; | |
6142 | arg = TREE_CHAIN (arg)) | |
6143 | if (TREE_PURPOSE (arg)) | |
6144 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, | |
6145 | TREE_VALUE (arg), | |
6146 | TREE_PURPOSE (arg)); | |
6147 | } | |
6148 | ||
fc6a28d7 MM |
6149 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
6150 | result of the substitution. Issue error and warning messages under | |
6151 | control of COMPLAIN. */ | |
00d3396f | 6152 | |
e9659ab0 | 6153 | static tree |
fc6a28d7 | 6154 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 6155 | { |
82a98427 | 6156 | location_t saved_loc; |
b370501f | 6157 | tree r = NULL_TREE; |
4b2811e9 | 6158 | tree in_decl = t; |
830bfa74 | 6159 | |
ae58fa02 | 6160 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 6161 | saved_loc = input_location; |
f31686a3 | 6162 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 6163 | |
8d08fdba MS |
6164 | switch (TREE_CODE (t)) |
6165 | { | |
98c1c668 JM |
6166 | case TEMPLATE_DECL: |
6167 | { | |
28e42b7e KL |
6168 | /* We can get here when processing a member function template, |
6169 | member class template, and template template parameter of | |
6170 | a template class. */ | |
98c1c668 | 6171 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 6172 | tree spec; |
28e42b7e KL |
6173 | tree tmpl_args; |
6174 | tree full_args; | |
98c1c668 | 6175 | |
28e42b7e | 6176 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 6177 | { |
28e42b7e KL |
6178 | /* Template template parameter is treated here. */ |
6179 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6180 | if (new_type == error_mark_node) | |
6181 | return error_mark_node; | |
36a117a5 | 6182 | |
28e42b7e KL |
6183 | r = copy_decl (t); |
6184 | TREE_CHAIN (r) = NULL_TREE; | |
6185 | TREE_TYPE (r) = new_type; | |
6186 | DECL_TEMPLATE_RESULT (r) | |
6187 | = build_decl (TYPE_DECL, DECL_NAME (decl), new_type); | |
6188 | DECL_TEMPLATE_PARMS (r) | |
6189 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, | |
6190 | complain); | |
6191 | TYPE_NAME (new_type) = r; | |
6192 | break; | |
6193 | } | |
36a117a5 | 6194 | |
28e42b7e KL |
6195 | /* We might already have an instance of this template. |
6196 | The ARGS are for the surrounding class type, so the | |
6197 | full args contain the tsubst'd args for the context, | |
6198 | plus the innermost args from the template decl. */ | |
6199 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) | |
6200 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) | |
6201 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
6202 | full_args = tsubst_template_args (tmpl_args, args, | |
6203 | complain, in_decl); | |
6204 | ||
6205 | /* tsubst_template_args doesn't copy the vector if | |
6206 | nothing changed. But, *something* should have | |
6207 | changed. */ | |
6208 | gcc_assert (full_args != tmpl_args); | |
6209 | ||
6210 | spec = retrieve_specialization (t, full_args, | |
6211 | /*class_specializations_p=*/true); | |
6212 | if (spec != NULL_TREE) | |
6213 | { | |
6214 | r = spec; | |
6215 | break; | |
db2767b6 | 6216 | } |
98c1c668 JM |
6217 | |
6218 | /* Make a new template decl. It will be similar to the | |
6219 | original, but will record the current template arguments. | |
6220 | We also create a new function declaration, which is just | |
6221 | like the old one, but points to this new template, rather | |
6222 | than the old one. */ | |
0acf7199 | 6223 | r = copy_decl (t); |
50bc768d | 6224 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
ae58fa02 | 6225 | TREE_CHAIN (r) = NULL_TREE; |
db2767b6 | 6226 | |
ae58fa02 | 6227 | DECL_CONTEXT (r) |
4f1c5b7d | 6228 | = tsubst_aggr_type (DECL_CONTEXT (t), args, |
c2ea3a40 | 6229 | complain, in_decl, |
4393e105 | 6230 | /*entering_scope=*/1); |
ae58fa02 | 6231 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
6232 | |
6233 | if (TREE_CODE (decl) == TYPE_DECL) | |
6234 | { | |
c2ea3a40 | 6235 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
2620d095 KL |
6236 | if (new_type == error_mark_node) |
6237 | return error_mark_node; | |
6238 | ||
ae58fa02 MM |
6239 | TREE_TYPE (r) = new_type; |
6240 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 6241 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 6242 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
93cdc044 JM |
6243 | } |
6244 | else | |
6245 | { | |
c2ea3a40 | 6246 | tree new_decl = tsubst (decl, args, complain, in_decl); |
caec1dc0 KL |
6247 | if (new_decl == error_mark_node) |
6248 | return error_mark_node; | |
17aec3eb RK |
6249 | |
6250 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
6251 | DECL_TI_TEMPLATE (new_decl) = r; |
6252 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
6253 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
93cdc044 JM |
6254 | } |
6255 | ||
ae58fa02 MM |
6256 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
6257 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
6258 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
6259 | |
6260 | /* The template parameters for this new template are all the | |
6261 | template parameters for the old template, except the | |
c6002625 | 6262 | outermost level of parameters. */ |
ae58fa02 | 6263 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 6264 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 6265 | complain); |
98c1c668 | 6266 | |
93cdc044 | 6267 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 6268 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 6269 | |
8c6ab2db NS |
6270 | if (TREE_CODE (decl) != TYPE_DECL) |
6271 | /* Record this non-type partial instantiation. */ | |
6272 | register_specialization (r, t, | |
6273 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r))); | |
98c1c668 | 6274 | } |
ae58fa02 | 6275 | break; |
8d08fdba MS |
6276 | |
6277 | case FUNCTION_DECL: | |
6278 | { | |
386b8a85 | 6279 | tree ctx; |
87603ed0 | 6280 | tree argvec = NULL_TREE; |
cf38f48a | 6281 | tree *friends; |
36a117a5 | 6282 | tree gen_tmpl; |
fc6a28d7 | 6283 | tree type; |
5566b478 | 6284 | int member; |
d8c4447d MM |
6285 | int args_depth; |
6286 | int parms_depth; | |
5566b478 | 6287 | |
36a117a5 | 6288 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 6289 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
6290 | |
6291 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
6292 | { | |
6293 | tree spec; | |
00cf3e31 MM |
6294 | bool dependent_p; |
6295 | ||
6296 | /* If T is not dependent, just return it. We have to | |
6297 | increment PROCESSING_TEMPLATE_DECL because | |
6298 | value_dependent_expression_p assumes that nothing is | |
6299 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
6300 | ++processing_template_decl; | |
6301 | dependent_p = value_dependent_expression_p (t); | |
6302 | --processing_template_decl; | |
6303 | if (!dependent_p) | |
6304 | return t; | |
36a117a5 MM |
6305 | |
6306 | /* Calculate the most general template of which R is a | |
6307 | specialization, and the complete set of arguments used to | |
6308 | specialize R. */ | |
6309 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
a91db711 NS |
6310 | argvec = tsubst_template_args (DECL_TI_ARGS |
6311 | (DECL_TEMPLATE_RESULT (gen_tmpl)), | |
6312 | args, complain, in_decl); | |
36a117a5 MM |
6313 | |
6314 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
6315 | spec = retrieve_specialization (gen_tmpl, argvec, |
6316 | /*class_specializations_p=*/false); | |
7ddedda4 | 6317 | |
36a117a5 | 6318 | if (spec) |
ae58fa02 MM |
6319 | { |
6320 | r = spec; | |
6321 | break; | |
6322 | } | |
d8c4447d | 6323 | |
f9a7ae04 MM |
6324 | /* We can see more levels of arguments than parameters if |
6325 | there was a specialization of a member template, like | |
6326 | this: | |
6327 | ||
6328 | template <class T> struct S { template <class U> void f(); } | |
6329 | template <> template <class U> void S<int>::f(U); | |
6330 | ||
dc957d14 | 6331 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
6332 | because that's where we can find the code we actually |
6333 | want to generate, but we'll have enough arguments for | |
6334 | the most general template. | |
6335 | ||
6336 | We also deal with the peculiar case: | |
d8c4447d MM |
6337 | |
6338 | template <class T> struct S { | |
6339 | template <class U> friend void f(); | |
6340 | }; | |
74b846e0 | 6341 | template <class U> void f() {} |
d8c4447d MM |
6342 | template S<int>; |
6343 | template void f<double>(); | |
6344 | ||
6345 | Here, the ARGS for the instantiation of will be {int, | |
6346 | double}. But, we only need as many ARGS as there are | |
6347 | levels of template parameters in CODE_PATTERN. We are | |
6348 | careful not to get fooled into reducing the ARGS in | |
6349 | situations like: | |
6350 | ||
6351 | template <class T> struct S { template <class U> void f(U); } | |
6352 | template <class T> template <> void S<T>::f(int) {} | |
6353 | ||
6354 | which we can spot because the pattern will be a | |
6355 | specialization in this case. */ | |
6356 | args_depth = TMPL_ARGS_DEPTH (args); | |
6357 | parms_depth = | |
6358 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
6359 | if (args_depth > parms_depth | |
6360 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 6361 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
6362 | } |
6363 | else | |
6364 | { | |
6365 | /* This special case arises when we have something like this: | |
6366 | ||
6367 | template <class T> struct S { | |
6368 | friend void f<int>(int, double); | |
6369 | }; | |
6370 | ||
6371 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
6372 | will be an IDENTIFIER_NODE. We are being called from |
6373 | tsubst_friend_function, and we want only to create a | |
6374 | new decl (R) with appropriate types so that we can call | |
6375 | determine_specialization. */ | |
36a117a5 MM |
6376 | gen_tmpl = NULL_TREE; |
6377 | } | |
6378 | ||
6eb3bb27 | 6379 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 6380 | { |
5566b478 MS |
6381 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
6382 | member = 2; | |
6383 | else | |
6384 | member = 1; | |
4f1c5b7d | 6385 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 6386 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
6387 | } |
6388 | else | |
6389 | { | |
6390 | member = 0; | |
4f1c5b7d | 6391 | ctx = DECL_CONTEXT (t); |
5566b478 | 6392 | } |
fc6a28d7 | 6393 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 MM |
6394 | if (type == error_mark_node) |
6395 | return error_mark_node; | |
8d08fdba | 6396 | |
5566b478 MS |
6397 | /* We do NOT check for matching decls pushed separately at this |
6398 | point, as they may not represent instantiations of this | |
6399 | template, and in any case are considered separate under the | |
0a7394bc | 6400 | discrete model. */ |
0acf7199 | 6401 | r = copy_decl (t); |
e1467ff2 | 6402 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 6403 | TREE_TYPE (r) = type; |
92643fea MM |
6404 | /* Clear out the mangled name and RTL for the instantiation. */ |
6405 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
6406 | SET_DECL_RTL (r, NULL_RTX); | |
5bd61841 | 6407 | DECL_INITIAL (r) = NULL_TREE; |
4f1c5b7d | 6408 | DECL_CONTEXT (r) = ctx; |
5566b478 | 6409 | |
1f6e1acc | 6410 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
6411 | /* Type-conversion operator. Reconstruct the name, in |
6412 | case it's the name of one of the template's parameters. */ | |
6413 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 6414 | |
4393e105 | 6415 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 6416 | complain, t); |
477f6664 | 6417 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
6418 | |
6419 | TREE_STATIC (r) = 0; | |
6420 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
6421 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
6422 | /* If this is an instantiation of a function with internal |
6423 | linkage, we already know what object file linkage will be | |
6424 | assigned to the instantiation. */ | |
6425 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 JM |
6426 | DECL_DEFER_OUTPUT (r) = 0; |
6427 | TREE_CHAIN (r) = NULL_TREE; | |
6428 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 6429 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 6430 | DECL_SAVED_TREE (r) = NULL_TREE; |
711734a9 | 6431 | TREE_USED (r) = 0; |
db9b2174 MM |
6432 | if (DECL_CLONED_FUNCTION (r)) |
6433 | { | |
6434 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
c2ea3a40 | 6435 | args, complain, t); |
db9b2174 MM |
6436 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); |
6437 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
6438 | } | |
711734a9 | 6439 | |
92643fea MM |
6440 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
6441 | this in the special friend case mentioned above where | |
6442 | GEN_TMPL is NULL. */ | |
36a117a5 | 6443 | if (gen_tmpl) |
386b8a85 | 6444 | { |
36a117a5 | 6445 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 6446 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 MM |
6447 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
6448 | register_specialization (r, gen_tmpl, argvec); | |
6449 | ||
9188c363 MM |
6450 | /* We're not supposed to instantiate default arguments |
6451 | until they are called, for a template. But, for a | |
6452 | declaration like: | |
6453 | ||
6454 | template <class T> void f () | |
6455 | { extern void g(int i = T()); } | |
6456 | ||
6457 | we should do the substitution when the template is | |
6458 | instantiated. We handle the member function case in | |
6459 | instantiate_class_template since the default arguments | |
6460 | might refer to other members of the class. */ | |
6461 | if (!member | |
6462 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
6463 | && !uses_template_parms (argvec)) | |
6464 | tsubst_default_arguments (r); | |
386b8a85 | 6465 | } |
f181d4ae | 6466 | |
cf38f48a MM |
6467 | /* Copy the list of befriending classes. */ |
6468 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
6469 | *friends; | |
6470 | friends = &TREE_CHAIN (*friends)) | |
6471 | { | |
6472 | *friends = copy_node (*friends); | |
6473 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 6474 | args, complain, |
cf38f48a MM |
6475 | in_decl); |
6476 | } | |
6477 | ||
212e7048 | 6478 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
6479 | { |
6480 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
6481 | if (DECL_CONSTRUCTOR_P (r)) |
6482 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
6483 | /* If this is an instantiation of a member template, clone it. |
6484 | If it isn't, that'll be handled by | |
6485 | clone_constructors_and_destructors. */ | |
5e818b93 | 6486 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 6487 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 6488 | } |
596ea4e5 | 6489 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))) |
4b0d3cbe MM |
6490 | grok_op_properties (r, DECL_FRIEND_P (r), |
6491 | (complain & tf_error) != 0); | |
c8b2e872 MM |
6492 | |
6493 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
6494 | SET_DECL_FRIEND_CONTEXT (r, | |
6495 | tsubst (DECL_FRIEND_CONTEXT (t), | |
6496 | args, complain, in_decl)); | |
8d08fdba | 6497 | } |
ae58fa02 | 6498 | break; |
8d08fdba MS |
6499 | |
6500 | case PARM_DECL: | |
6501 | { | |
fc6a28d7 MM |
6502 | tree type; |
6503 | ||
ae58fa02 | 6504 | r = copy_node (t); |
833aa4c4 NS |
6505 | if (DECL_TEMPLATE_PARM_P (t)) |
6506 | SET_DECL_TEMPLATE_PARM_P (r); | |
8e51619a | 6507 | |
fc6a28d7 | 6508 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
02bab9db | 6509 | type = type_decays_to (type); |
5566b478 | 6510 | TREE_TYPE (r) = type; |
9804209d | 6511 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 6512 | |
560ad596 MM |
6513 | if (DECL_INITIAL (r)) |
6514 | { | |
6515 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
6516 | DECL_INITIAL (r) = TREE_TYPE (r); | |
6517 | else | |
6518 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
6519 | complain, in_decl); | |
6520 | } | |
db2767b6 | 6521 | |
5566b478 | 6522 | DECL_CONTEXT (r) = NULL_TREE; |
8e51619a JM |
6523 | |
6524 | if (!DECL_TEMPLATE_PARM_P (r)) | |
6525 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
8d08fdba | 6526 | if (TREE_CHAIN (t)) |
4393e105 | 6527 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
c2ea3a40 | 6528 | complain, TREE_CHAIN (t)); |
8d08fdba | 6529 | } |
ae58fa02 | 6530 | break; |
8d08fdba | 6531 | |
5566b478 MS |
6532 | case FIELD_DECL: |
6533 | { | |
fc6a28d7 MM |
6534 | tree type; |
6535 | ||
0acf7199 | 6536 | r = copy_decl (t); |
fc6a28d7 MM |
6537 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
6538 | if (type == error_mark_node) | |
6539 | return error_mark_node; | |
1b8899d1 | 6540 | TREE_TYPE (r) = type; |
9804209d | 6541 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 MM |
6542 | |
6543 | /* We don't have to set DECL_CONTEXT here; it is set by | |
6544 | finish_member_declaration. */ | |
4393e105 | 6545 | DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, |
c2ea3a40 | 6546 | complain, in_decl); |
5566b478 | 6547 | TREE_CHAIN (r) = NULL_TREE; |
dfbb4f34 | 6548 | if (VOID_TYPE_P (type)) |
0f51ccfc | 6549 | cp_error_at ("instantiation of %qD as type %qT", r, type); |
5566b478 | 6550 | } |
ae58fa02 | 6551 | break; |
5566b478 MS |
6552 | |
6553 | case USING_DECL: | |
6554 | { | |
ae58fa02 | 6555 | r = copy_node (t); |
fd9aef9d NS |
6556 | /* It is not a dependent using decl any more. */ |
6557 | TREE_TYPE (r) = void_type_node; | |
5566b478 | 6558 | DECL_INITIAL (r) |
c2ea3a40 | 6559 | = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); |
53400b9e GB |
6560 | DECL_NAME (r) |
6561 | = tsubst_copy (DECL_NAME (t), args, complain, in_decl); | |
5566b478 | 6562 | TREE_CHAIN (r) = NULL_TREE; |
5566b478 | 6563 | } |
ae58fa02 | 6564 | break; |
5566b478 | 6565 | |
9188c363 | 6566 | case TYPE_DECL: |
5566b478 MS |
6567 | case VAR_DECL: |
6568 | { | |
1cea0434 KG |
6569 | tree argvec = NULL_TREE; |
6570 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 6571 | tree spec; |
1cea0434 | 6572 | tree tmpl = NULL_TREE; |
9188c363 | 6573 | tree ctx; |
fc6a28d7 | 6574 | tree type = NULL_TREE; |
6dfbb909 | 6575 | int local_p; |
9188c363 | 6576 | |
fc6a28d7 MM |
6577 | if (TREE_CODE (t) == TYPE_DECL) |
6578 | { | |
6579 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6580 | if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM | |
6581 | || t == TYPE_MAIN_DECL (TREE_TYPE (t))) | |
6582 | { | |
6583 | /* If this is the canonical decl, we don't have to | |
6584 | mess with instantiations, and often we can't (for | |
6585 | typename, template type parms and such). Note that | |
6586 | TYPE_NAME is not correct for the above test if | |
6587 | we've copied the type for a typedef. */ | |
6588 | r = TYPE_NAME (type); | |
6589 | break; | |
6590 | } | |
6591 | } | |
6592 | ||
6dfbb909 MM |
6593 | /* Assume this is a non-local variable. */ |
6594 | local_p = 0; | |
5566b478 | 6595 | |
de96bf57 | 6596 | if (TYPE_P (CP_DECL_CONTEXT (t))) |
9188c363 | 6597 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
c2ea3a40 | 6598 | complain, |
9188c363 | 6599 | in_decl, /*entering_scope=*/1); |
2ba9c47e MM |
6600 | else if (DECL_NAMESPACE_SCOPE_P (t)) |
6601 | ctx = DECL_CONTEXT (t); | |
9188c363 | 6602 | else |
6dfbb909 MM |
6603 | { |
6604 | /* Subsequent calls to pushdecl will fill this in. */ | |
6605 | ctx = NULL_TREE; | |
2ba9c47e | 6606 | local_p = 1; |
6dfbb909 | 6607 | } |
9188c363 | 6608 | |
36a117a5 | 6609 | /* Check to see if we already have this specialization. */ |
6dfbb909 MM |
6610 | if (!local_p) |
6611 | { | |
6612 | tmpl = DECL_TI_TEMPLATE (t); | |
6613 | gen_tmpl = most_general_template (tmpl); | |
c2ea3a40 | 6614 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); |
c7222c02 MM |
6615 | spec = retrieve_specialization (gen_tmpl, argvec, |
6616 | /*class_specializations_p=*/false); | |
6dfbb909 | 6617 | } |
9188c363 | 6618 | else |
6dfbb909 | 6619 | spec = retrieve_local_specialization (t); |
9188c363 | 6620 | |
36a117a5 | 6621 | if (spec) |
ae58fa02 MM |
6622 | { |
6623 | r = spec; | |
6624 | break; | |
6625 | } | |
5566b478 | 6626 | |
0acf7199 | 6627 | r = copy_decl (t); |
edebf865 | 6628 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 6629 | { |
fc6a28d7 MM |
6630 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
6631 | if (type == error_mark_node) | |
6632 | return error_mark_node; | |
39703eb9 MM |
6633 | type = complete_type (type); |
6634 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
6635 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
62e19030 | 6636 | type = check_var_type (DECL_NAME (r), type); |
39703eb9 | 6637 | } |
a3d87771 MM |
6638 | else if (DECL_SELF_REFERENCE_P (t)) |
6639 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 6640 | TREE_TYPE (r) = type; |
9804209d | 6641 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 6642 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
6643 | /* Clear out the mangled name and RTL for the instantiation. */ |
6644 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
6645 | SET_DECL_RTL (r, NULL_RTX); | |
d11ad92e MS |
6646 | |
6647 | /* Don't try to expand the initializer until someone tries to use | |
6648 | this variable; otherwise we run into circular dependencies. */ | |
6649 | DECL_INITIAL (r) = NULL_TREE; | |
19e7881c | 6650 | SET_DECL_RTL (r, NULL_RTX); |
06ceef4e | 6651 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
5566b478 | 6652 | |
e0942dcd MM |
6653 | /* Even if the original location is out of scope, the newly |
6654 | substituted one is not. */ | |
6655 | if (TREE_CODE (r) == VAR_DECL) | |
17bbb839 MM |
6656 | { |
6657 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
6658 | DECL_INITIALIZED_P (r) = 0; | |
6659 | } | |
e0942dcd | 6660 | |
6dfbb909 MM |
6661 | if (!local_p) |
6662 | { | |
6663 | /* A static data member declaration is always marked | |
6664 | external when it is declared in-class, even if an | |
6665 | initializer is present. We mimic the non-template | |
6666 | processing here. */ | |
6667 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 6668 | |
6dfbb909 MM |
6669 | register_specialization (r, gen_tmpl, argvec); |
6670 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); | |
6671 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
6672 | } | |
9188c363 | 6673 | else |
6dfbb909 | 6674 | register_local_specialization (r, t); |
5566b478 | 6675 | |
5566b478 | 6676 | TREE_CHAIN (r) = NULL_TREE; |
edebf865 | 6677 | layout_decl (r, 0); |
5566b478 | 6678 | } |
ae58fa02 | 6679 | break; |
5566b478 | 6680 | |
ae58fa02 | 6681 | default: |
315fb5db | 6682 | gcc_unreachable (); |
ae58fa02 MM |
6683 | } |
6684 | ||
6685 | /* Restore the file and line information. */ | |
82a98427 | 6686 | input_location = saved_loc; |
ae58fa02 MM |
6687 | |
6688 | return r; | |
6689 | } | |
6690 | ||
34cd5ae7 | 6691 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 6692 | |
e9659ab0 | 6693 | static tree |
3a978d72 NN |
6694 | tsubst_arg_types (tree arg_types, |
6695 | tree args, | |
6696 | tsubst_flags_t complain, | |
6697 | tree in_decl) | |
cabc336a MM |
6698 | { |
6699 | tree remaining_arg_types; | |
cabc336a MM |
6700 | tree type; |
6701 | ||
6702 | if (!arg_types || arg_types == void_list_node) | |
6703 | return arg_types; | |
6704 | ||
6705 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), | |
4393e105 MM |
6706 | args, complain, in_decl); |
6707 | if (remaining_arg_types == error_mark_node) | |
6708 | return error_mark_node; | |
6709 | ||
6710 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
6711 | if (type == error_mark_node) | |
6712 | return error_mark_node; | |
4b2811e9 NS |
6713 | if (VOID_TYPE_P (type)) |
6714 | { | |
c2ea3a40 | 6715 | if (complain & tf_error) |
4b2811e9 | 6716 | { |
0f51ccfc | 6717 | error ("invalid parameter type %qT", type); |
4b2811e9 | 6718 | if (in_decl) |
0f51ccfc | 6719 | cp_error_at ("in declaration %qD", in_decl); |
4b2811e9 NS |
6720 | } |
6721 | return error_mark_node; | |
6722 | } | |
cabc336a | 6723 | |
4393e105 MM |
6724 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
6725 | top-level qualifiers as required. */ | |
6726 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a MM |
6727 | |
6728 | /* Note that we do not substitute into default arguments here. The | |
6729 | standard mandates that they be instantiated only when needed, | |
6730 | which is done in build_over_call. */ | |
51632249 JM |
6731 | return hash_tree_cons (TREE_PURPOSE (arg_types), type, |
6732 | remaining_arg_types); | |
cabc336a MM |
6733 | |
6734 | } | |
6735 | ||
4393e105 MM |
6736 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
6737 | *not* handle the exception-specification for FNTYPE, because the | |
6738 | initial substitution of explicitly provided template parameters | |
6739 | during argument deduction forbids substitution into the | |
6740 | exception-specification: | |
6741 | ||
6742 | [temp.deduct] | |
6743 | ||
6744 | All references in the function type of the function template to the | |
6745 | corresponding template parameters are replaced by the specified tem- | |
6746 | plate argument values. If a substitution in a template parameter or | |
6747 | in the function type of the function template results in an invalid | |
6748 | type, type deduction fails. [Note: The equivalent substitution in | |
6749 | exception specifications is done only when the function is instanti- | |
6750 | ated, at which point a program is ill-formed if the substitution | |
6751 | results in an invalid type.] */ | |
6752 | ||
6753 | static tree | |
3a978d72 NN |
6754 | tsubst_function_type (tree t, |
6755 | tree args, | |
6756 | tsubst_flags_t complain, | |
6757 | tree in_decl) | |
4393e105 MM |
6758 | { |
6759 | tree return_type; | |
6760 | tree arg_types; | |
6761 | tree fntype; | |
6762 | ||
8dd3f57a | 6763 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 6764 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 6765 | |
46c895ac | 6766 | /* Substitute the return type. */ |
4393e105 MM |
6767 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
6768 | if (return_type == error_mark_node) | |
6769 | return error_mark_node; | |
6e2993bf MM |
6770 | /* The standard does not presently indicate that creation of a |
6771 | function type with an invalid return type is a deduction failure. | |
270af55d | 6772 | However, that is clearly analogous to creating an array of "void" |
6e2993bf MM |
6773 | or a reference to a reference. This is core issue #486. */ |
6774 | if (TREE_CODE (return_type) == ARRAY_TYPE | |
6775 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
6776 | { | |
6777 | if (complain & tf_error) | |
6778 | { | |
6779 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
6780 | error ("function returning an array"); | |
6781 | else | |
6782 | error ("function returning a function"); | |
6783 | } | |
6784 | return error_mark_node; | |
6785 | } | |
4393e105 | 6786 | |
34cd5ae7 | 6787 | /* Substitute the argument types. */ |
4393e105 MM |
6788 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
6789 | complain, in_decl); | |
6790 | if (arg_types == error_mark_node) | |
6791 | return error_mark_node; | |
6792 | ||
6793 | /* Construct a new type node and return it. */ | |
6794 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
6795 | fntype = build_function_type (return_type, arg_types); | |
6796 | else | |
6797 | { | |
6798 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
6799 | if (! IS_AGGR_TYPE (r)) | |
6800 | { | |
6801 | /* [temp.deduct] | |
6802 | ||
6803 | Type deduction may fail for any of the following | |
6804 | reasons: | |
6805 | ||
6806 | -- Attempting to create "pointer to member of T" when T | |
6807 | is not a class type. */ | |
c2ea3a40 | 6808 | if (complain & tf_error) |
0f51ccfc | 6809 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
6810 | r); |
6811 | return error_mark_node; | |
6812 | } | |
6813 | ||
43dc123f MM |
6814 | fntype = build_method_type_directly (r, return_type, |
6815 | TREE_CHAIN (arg_types)); | |
4393e105 | 6816 | } |
c2ea3a40 | 6817 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
e9525111 | 6818 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
4393e105 MM |
6819 | |
6820 | return fntype; | |
6821 | } | |
6822 | ||
c7222c02 MM |
6823 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
6824 | ARGS into that specification, and return the substituted | |
6825 | specification. If there is no specification, return NULL_TREE. */ | |
6826 | ||
6827 | static tree | |
6828 | tsubst_exception_specification (tree fntype, | |
6829 | tree args, | |
6830 | tsubst_flags_t complain, | |
6831 | tree in_decl) | |
6832 | { | |
6833 | tree specs; | |
6834 | tree new_specs; | |
6835 | ||
6836 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
6837 | new_specs = NULL_TREE; | |
6838 | if (specs) | |
6839 | { | |
6840 | if (! TREE_VALUE (specs)) | |
6841 | new_specs = specs; | |
6842 | else | |
6843 | while (specs) | |
6844 | { | |
6845 | tree spec; | |
6846 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
6847 | if (spec == error_mark_node) | |
6848 | return spec; | |
6849 | new_specs = add_exception_specifier (new_specs, spec, complain); | |
6850 | specs = TREE_CHAIN (specs); | |
6851 | } | |
6852 | } | |
6853 | return new_specs; | |
6854 | } | |
6855 | ||
297e73d8 MM |
6856 | /* Substitute into the PARMS of a call-declarator. */ |
6857 | ||
e9659ab0 | 6858 | static tree |
3a978d72 NN |
6859 | tsubst_call_declarator_parms (tree parms, |
6860 | tree args, | |
6861 | tsubst_flags_t complain, | |
6862 | tree in_decl) | |
297e73d8 MM |
6863 | { |
6864 | tree new_parms; | |
6865 | tree type; | |
6866 | tree defarg; | |
6867 | ||
6868 | if (!parms || parms == void_list_node) | |
6869 | return parms; | |
6870 | ||
6871 | new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms), | |
4393e105 | 6872 | args, complain, in_decl); |
297e73d8 MM |
6873 | |
6874 | /* Figure out the type of this parameter. */ | |
4393e105 | 6875 | type = tsubst (TREE_VALUE (parms), args, complain, in_decl); |
297e73d8 MM |
6876 | |
6877 | /* Figure out the default argument as well. Note that we use | |
4393e105 MM |
6878 | tsubst_expr since the default argument is really an expression. */ |
6879 | defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl); | |
297e73d8 MM |
6880 | |
6881 | /* Chain this parameter on to the front of those we have already | |
6882 | processed. We don't use hash_tree_cons because that function | |
6883 | doesn't check TREE_PARMLIST. */ | |
6884 | new_parms = tree_cons (defarg, type, new_parms); | |
6885 | ||
297e73d8 MM |
6886 | return new_parms; |
6887 | } | |
6888 | ||
4393e105 MM |
6889 | /* Take the tree structure T and replace template parameters used |
6890 | therein with the argument vector ARGS. IN_DECL is an associated | |
6891 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
6892 | Issue error and warning messages under control of COMPLAIN. Note |
6893 | that we must be relatively non-tolerant of extensions here, in | |
6894 | order to preserve conformance; if we allow substitutions that | |
6895 | should not be allowed, we may allow argument deductions that should | |
6896 | not succeed, and therefore report ambiguous overload situations | |
6897 | where there are none. In theory, we could allow the substitution, | |
6898 | but indicate that it should have failed, and allow our caller to | |
6899 | make sure that the right thing happens, but we don't try to do this | |
6900 | yet. | |
4393e105 MM |
6901 | |
6902 | This function is used for dealing with types, decls and the like; | |
6903 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 6904 | |
14d22dd6 | 6905 | static tree |
3a978d72 | 6906 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 6907 | { |
0ecfe0b4 | 6908 | tree type, r; |
ae58fa02 MM |
6909 | |
6910 | if (t == NULL_TREE || t == error_mark_node | |
6911 | || t == integer_type_node | |
6912 | || t == void_type_node | |
6913 | || t == char_type_node | |
539599c1 | 6914 | || t == unknown_type_node |
ae58fa02 MM |
6915 | || TREE_CODE (t) == NAMESPACE_DECL) |
6916 | return t; | |
6917 | ||
fc6a28d7 MM |
6918 | if (DECL_P (t)) |
6919 | return tsubst_decl (t, args, complain); | |
6920 | ||
ae58fa02 MM |
6921 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
6922 | type = IDENTIFIER_TYPE_VALUE (t); | |
6923 | else | |
6924 | type = TREE_TYPE (t); | |
399dedb9 | 6925 | |
50bc768d | 6926 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 6927 | |
fc6a28d7 | 6928 | if (type |
ae58fa02 | 6929 | && TREE_CODE (t) != TYPENAME_TYPE |
4393e105 MM |
6930 | && TREE_CODE (t) != IDENTIFIER_NODE |
6931 | && TREE_CODE (t) != FUNCTION_TYPE | |
6932 | && TREE_CODE (t) != METHOD_TYPE) | |
6933 | type = tsubst (type, args, complain, in_decl); | |
6934 | if (type == error_mark_node) | |
6935 | return error_mark_node; | |
ae58fa02 | 6936 | |
ae58fa02 MM |
6937 | switch (TREE_CODE (t)) |
6938 | { | |
6939 | case RECORD_TYPE: | |
6940 | case UNION_TYPE: | |
6941 | case ENUMERAL_TYPE: | |
4393e105 MM |
6942 | return tsubst_aggr_type (t, args, complain, in_decl, |
6943 | /*entering_scope=*/0); | |
ae58fa02 MM |
6944 | |
6945 | case ERROR_MARK: | |
6946 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
6947 | case VOID_TYPE: |
6948 | case REAL_TYPE: | |
6949 | case COMPLEX_TYPE: | |
c00996a3 | 6950 | case VECTOR_TYPE: |
ae58fa02 MM |
6951 | case BOOLEAN_TYPE: |
6952 | case INTEGER_CST: | |
6953 | case REAL_CST: | |
6954 | case STRING_CST: | |
6955 | return t; | |
6956 | ||
6957 | case INTEGER_TYPE: | |
6958 | if (t == integer_type_node) | |
6959 | return t; | |
6960 | ||
6961 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
6962 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
6963 | return t; | |
d2e5ee5c | 6964 | |
5566b478 | 6965 | { |
ddce3528 | 6966 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 6967 | |
a91db711 NS |
6968 | /* The array dimension behaves like a non-type template arg, |
6969 | in that we want to fold it as much as possible. */ | |
6970 | max = tsubst_template_arg (omax, args, complain, in_decl); | |
4ef69b83 | 6971 | max = fold_decl_constant_value (max); |
8dd3f57a | 6972 | |
ddce3528 JM |
6973 | if (integer_zerop (omax)) |
6974 | { | |
6975 | /* Still allow an explicit array of size zero. */ | |
6976 | if (pedantic) | |
6977 | pedwarn ("creating array with size zero"); | |
6978 | } | |
07c88314 MM |
6979 | else if (integer_zerop (max) |
6980 | || (TREE_CODE (max) == INTEGER_CST | |
6981 | && INT_CST_LT (max, integer_zero_node))) | |
0ecfe0b4 | 6982 | { |
4393e105 MM |
6983 | /* [temp.deduct] |
6984 | ||
6985 | Type deduction may fail for any of the following | |
6986 | reasons: | |
6987 | ||
bf8f3f93 MM |
6988 | Attempting to create an array with a size that is |
6989 | zero or negative. */ | |
c2ea3a40 | 6990 | if (complain & tf_error) |
0f51ccfc | 6991 | error ("creating array with size zero (%qE)", max); |
4393e105 MM |
6992 | |
6993 | return error_mark_node; | |
0ecfe0b4 JM |
6994 | } |
6995 | ||
c95cd22e | 6996 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
6997 | } |
6998 | ||
6999 | case TEMPLATE_TYPE_PARM: | |
7000 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 7001 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
7002 | case TEMPLATE_PARM_INDEX: |
7003 | { | |
7004 | int idx; | |
7005 | int level; | |
7006 | int levels; | |
315fb5db | 7007 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
7008 | |
7009 | r = NULL_TREE; | |
ae58fa02 | 7010 | |
315fb5db | 7011 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
ae58fa02 | 7012 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
a1281f45 KL |
7013 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM |
7014 | || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
ae58fa02 MM |
7015 | { |
7016 | idx = TEMPLATE_TYPE_IDX (t); | |
7017 | level = TEMPLATE_TYPE_LEVEL (t); | |
7018 | } | |
7019 | else | |
7020 | { | |
7021 | idx = TEMPLATE_PARM_IDX (t); | |
7022 | level = TEMPLATE_PARM_LEVEL (t); | |
7023 | } | |
7024 | ||
315fb5db NS |
7025 | levels = TMPL_ARGS_DEPTH (args); |
7026 | if (level <= levels) | |
7027 | arg = TMPL_ARG (args, level, idx); | |
ae58fa02 | 7028 | |
315fb5db NS |
7029 | if (arg == error_mark_node) |
7030 | return error_mark_node; | |
7031 | else if (arg != NULL_TREE) | |
7032 | { | |
7033 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
ae58fa02 | 7034 | { |
315fb5db NS |
7035 | gcc_assert (TYPE_P (arg)); |
7036 | return cp_build_qualified_type_real | |
7037 | (arg, cp_type_quals (arg) | cp_type_quals (t), | |
7038 | complain | tf_ignore_bad_quals); | |
7039 | } | |
7040 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
7041 | { | |
7042 | /* We are processing a type constructed from a | |
7043 | template template parameter. */ | |
7044 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
7045 | args, complain, in_decl); | |
7046 | if (argvec == error_mark_node) | |
7047 | return error_mark_node; | |
4393e105 | 7048 | |
315fb5db NS |
7049 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
7050 | are resolving nested-types in the signature of a | |
7051 | member function templates. Otherwise ARG is a | |
7052 | TEMPLATE_DECL and is the real template to be | |
7053 | instantiated. */ | |
7054 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
7055 | arg = TYPE_NAME (arg); | |
7056 | ||
7057 | r = lookup_template_class (arg, | |
7058 | argvec, in_decl, | |
7059 | DECL_CONTEXT (arg), | |
7060 | /*entering_scope=*/0, | |
7061 | complain); | |
7062 | return cp_build_qualified_type_real | |
7063 | (r, TYPE_QUALS (t), complain); | |
ae58fa02 | 7064 | } |
315fb5db NS |
7065 | else |
7066 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
7067 | return arg; | |
ae58fa02 MM |
7068 | } |
7069 | ||
7070 | if (level == 1) | |
7071 | /* This can happen during the attempted tsubst'ing in | |
7072 | unify. This means that we don't yet have any information | |
7073 | about the template parameter in question. */ | |
7074 | return t; | |
7075 | ||
7076 | /* If we get here, we must have been looking at a parm for a | |
7077 | more deeply nested template. Make a new version of this | |
7078 | template parameter, but with a lower level. */ | |
7079 | switch (TREE_CODE (t)) | |
7080 | { | |
7081 | case TEMPLATE_TYPE_PARM: | |
7082 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 7083 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 7084 | if (cp_type_quals (t)) |
ae58fa02 | 7085 | { |
9ccf6541 | 7086 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
4f2b0fb2 NS |
7087 | r = cp_build_qualified_type_real |
7088 | (r, cp_type_quals (t), | |
7089 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM | |
7090 | ? tf_ignore_bad_quals : 0)); | |
9ccf6541 MM |
7091 | } |
7092 | else | |
7093 | { | |
11e74ea6 | 7094 | r = copy_type (t); |
9ccf6541 MM |
7095 | TEMPLATE_TYPE_PARM_INDEX (r) |
7096 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
7097 | r, levels); | |
7098 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
7099 | TYPE_MAIN_VARIANT (r) = r; | |
7100 | TYPE_POINTER_TO (r) = NULL_TREE; | |
7101 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
7102 | ||
a1281f45 | 7103 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
7104 | { |
7105 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
7106 | complain, in_decl); | |
7107 | if (argvec == error_mark_node) | |
7108 | return error_mark_node; | |
4393e105 | 7109 | |
9ccf6541 MM |
7110 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
7111 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
7112 | } | |
ae58fa02 MM |
7113 | } |
7114 | break; | |
7115 | ||
7116 | case TEMPLATE_PARM_INDEX: | |
7117 | r = reduce_template_parm_level (t, type, levels); | |
7118 | break; | |
7119 | ||
7120 | default: | |
315fb5db | 7121 | gcc_unreachable (); |
ae58fa02 MM |
7122 | } |
7123 | ||
5566b478 | 7124 | return r; |
ae58fa02 | 7125 | } |
5566b478 | 7126 | |
8d08fdba MS |
7127 | case TREE_LIST: |
7128 | { | |
058b15c1 | 7129 | tree purpose, value, chain; |
8d08fdba MS |
7130 | |
7131 | if (t == void_list_node) | |
7132 | return t; | |
7133 | ||
8d08fdba MS |
7134 | purpose = TREE_PURPOSE (t); |
7135 | if (purpose) | |
4393e105 MM |
7136 | { |
7137 | purpose = tsubst (purpose, args, complain, in_decl); | |
7138 | if (purpose == error_mark_node) | |
7139 | return error_mark_node; | |
7140 | } | |
8d08fdba MS |
7141 | value = TREE_VALUE (t); |
7142 | if (value) | |
4393e105 MM |
7143 | { |
7144 | value = tsubst (value, args, complain, in_decl); | |
7145 | if (value == error_mark_node) | |
7146 | return error_mark_node; | |
7147 | } | |
8d08fdba MS |
7148 | chain = TREE_CHAIN (t); |
7149 | if (chain && chain != void_type_node) | |
4393e105 MM |
7150 | { |
7151 | chain = tsubst (chain, args, complain, in_decl); | |
7152 | if (chain == error_mark_node) | |
7153 | return error_mark_node; | |
7154 | } | |
8d08fdba MS |
7155 | if (purpose == TREE_PURPOSE (t) |
7156 | && value == TREE_VALUE (t) | |
7157 | && chain == TREE_CHAIN (t)) | |
7158 | return t; | |
058b15c1 | 7159 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 7160 | } |
95b4aca6 NS |
7161 | |
7162 | case TREE_BINFO: | |
bd7eccc4 | 7163 | /* We should never be tsubsting a binfo. */ |
315fb5db | 7164 | gcc_unreachable (); |
85b71cf2 | 7165 | |
95b4aca6 NS |
7166 | case TREE_VEC: |
7167 | /* A vector of template arguments. */ | |
50bc768d | 7168 | gcc_assert (!type); |
a91db711 | 7169 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 7170 | |
8d08fdba MS |
7171 | case POINTER_TYPE: |
7172 | case REFERENCE_TYPE: | |
7173 | { | |
8d08fdba | 7174 | enum tree_code code; |
79a7c7fa | 7175 | |
46c895ac | 7176 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
7177 | return t; |
7178 | ||
7179 | code = TREE_CODE (t); | |
4393e105 MM |
7180 | |
7181 | ||
7182 | /* [temp.deduct] | |
7183 | ||
7184 | Type deduction may fail for any of the following | |
7185 | reasons: | |
7186 | ||
7187 | -- Attempting to create a pointer to reference type. | |
7188 | -- Attempting to create a reference to a reference type or | |
7189 | a reference to void. */ | |
0ecfe0b4 JM |
7190 | if (TREE_CODE (type) == REFERENCE_TYPE |
7191 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa | 7192 | { |
82a98427 | 7193 | static location_t last_loc; |
79a7c7fa JM |
7194 | |
7195 | /* We keep track of the last time we issued this error | |
7196 | message to avoid spewing a ton of messages during a | |
7197 | single bad template instantiation. */ | |
c2ea3a40 | 7198 | if (complain & tf_error |
93409b8c | 7199 | #ifdef USE_MAPPED_LOCATION |
9b60dfd7 | 7200 | && last_loc != input_location |
93409b8c | 7201 | #else |
82a98427 | 7202 | && (last_loc.line != input_line |
9b60dfd7 | 7203 | || last_loc.file != input_filename) |
93409b8c | 7204 | #endif |
9b60dfd7 | 7205 | ) |
79a7c7fa | 7206 | { |
0ecfe0b4 | 7207 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 7208 | error ("forming reference to void"); |
0ecfe0b4 | 7209 | else |
0f51ccfc GDR |
7210 | error ("forming %s to reference type %qT", |
7211 | (code == POINTER_TYPE) ? "pointer" : "reference", | |
7212 | type); | |
82a98427 | 7213 | last_loc = input_location; |
79a7c7fa JM |
7214 | } |
7215 | ||
4393e105 | 7216 | return error_mark_node; |
79a7c7fa JM |
7217 | } |
7218 | else if (code == POINTER_TYPE) | |
46c895ac NS |
7219 | { |
7220 | r = build_pointer_type (type); | |
7221 | if (TREE_CODE (type) == METHOD_TYPE) | |
7222 | r = build_ptrmemfunc_type (r); | |
7223 | } | |
8d08fdba MS |
7224 | else |
7225 | r = build_reference_type (type); | |
adecb3f4 | 7226 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 7227 | |
a5f1c5f8 NS |
7228 | if (r != error_mark_node) |
7229 | /* Will this ever be needed for TYPE_..._TO values? */ | |
7230 | layout_type (r); | |
7231 | ||
8d08fdba MS |
7232 | return r; |
7233 | } | |
a4443a08 | 7234 | case OFFSET_TYPE: |
0ecfe0b4 | 7235 | { |
4393e105 MM |
7236 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
7237 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
7238 | { | |
7239 | /* [temp.deduct] | |
7240 | ||
7241 | Type deduction may fail for any of the following | |
7242 | reasons: | |
7243 | ||
7244 | -- Attempting to create "pointer to member of T" when T | |
7245 | is not a class type. */ | |
c2ea3a40 | 7246 | if (complain & tf_error) |
0f51ccfc | 7247 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
7248 | return error_mark_node; |
7249 | } | |
46c895ac NS |
7250 | if (TREE_CODE (type) == REFERENCE_TYPE) |
7251 | { | |
4f09be91 | 7252 | if (complain & tf_error) |
0f51ccfc | 7253 | error ("creating pointer to member reference type %qT", type); |
46c895ac NS |
7254 | |
7255 | return error_mark_node; | |
7256 | } | |
50bc768d | 7257 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 7258 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a | 7259 | { |
9804209d DG |
7260 | /* The type of the implicit object parameter gets its |
7261 | cv-qualifiers from the FUNCTION_TYPE. */ | |
a5ac359a | 7262 | tree method_type; |
9804209d DG |
7263 | tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r), |
7264 | cp_type_quals (type)); | |
7265 | tree memptr; | |
7266 | method_type = build_method_type_directly (this_type, | |
43dc123f MM |
7267 | TREE_TYPE (type), |
7268 | TYPE_ARG_TYPES (type)); | |
9804209d DG |
7269 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
7270 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
7271 | complain); | |
a5ac359a | 7272 | } |
46c895ac | 7273 | else |
b7a78333 MM |
7274 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
7275 | TYPE_QUALS (t), | |
7276 | complain); | |
0ecfe0b4 | 7277 | } |
8d08fdba MS |
7278 | case FUNCTION_TYPE: |
7279 | case METHOD_TYPE: | |
7280 | { | |
c11b6f21 | 7281 | tree fntype; |
c7222c02 | 7282 | tree specs; |
4393e105 MM |
7283 | fntype = tsubst_function_type (t, args, complain, in_decl); |
7284 | if (fntype == error_mark_node) | |
7285 | return error_mark_node; | |
cabc336a | 7286 | |
34cd5ae7 | 7287 | /* Substitute the exception specification. */ |
c7222c02 MM |
7288 | specs = tsubst_exception_specification (t, args, complain, |
7289 | in_decl); | |
7290 | if (specs) | |
7291 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 7292 | return fntype; |
8d08fdba MS |
7293 | } |
7294 | case ARRAY_TYPE: | |
7295 | { | |
4393e105 MM |
7296 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
7297 | if (domain == error_mark_node) | |
7298 | return error_mark_node; | |
7299 | ||
7300 | /* As an optimization, we avoid regenerating the array type if | |
7301 | it will obviously be the same as T. */ | |
8d08fdba MS |
7302 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
7303 | return t; | |
0ecfe0b4 | 7304 | |
4393e105 MM |
7305 | /* These checks should match the ones in grokdeclarator. |
7306 | ||
7307 | [temp.deduct] | |
7308 | ||
7309 | The deduction may fail for any of the following reasons: | |
7310 | ||
7311 | -- Attempting to create an array with an element type that | |
cfb91b67 GB |
7312 | is void, a function type, or a reference type, or [DR337] |
7313 | an abstract class type. */ | |
4393e105 MM |
7314 | if (TREE_CODE (type) == VOID_TYPE |
7315 | || TREE_CODE (type) == FUNCTION_TYPE | |
7316 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 7317 | { |
c2ea3a40 | 7318 | if (complain & tf_error) |
0f51ccfc | 7319 | error ("creating array of %qT", type); |
4393e105 | 7320 | return error_mark_node; |
0ecfe0b4 | 7321 | } |
cfb91b67 GB |
7322 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
7323 | { | |
7324 | if (complain & tf_error) | |
0f51ccfc | 7325 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 GB |
7326 | type); |
7327 | return error_mark_node; | |
7328 | } | |
0ecfe0b4 | 7329 | |
8d08fdba MS |
7330 | r = build_cplus_array_type (type, domain); |
7331 | return r; | |
7332 | } | |
7333 | ||
8d08fdba | 7334 | case PLUS_EXPR: |
5566b478 | 7335 | case MINUS_EXPR: |
4393e105 | 7336 | { |
c2ea3a40 NS |
7337 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
7338 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
7339 | |
7340 | if (e1 == error_mark_node || e2 == error_mark_node) | |
7341 | return error_mark_node; | |
7342 | ||
7866705a | 7343 | return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2); |
4393e105 | 7344 | } |
8d08fdba MS |
7345 | |
7346 | case NEGATE_EXPR: | |
7347 | case NOP_EXPR: | |
4393e105 | 7348 | { |
c2ea3a40 | 7349 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
7350 | if (e == error_mark_node) |
7351 | return error_mark_node; | |
7352 | ||
7866705a | 7353 | return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e); |
4393e105 | 7354 | } |
8d08fdba | 7355 | |
5566b478 MS |
7356 | case TYPENAME_TYPE: |
7357 | { | |
4393e105 MM |
7358 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
7359 | in_decl, /*entering_scope=*/1); | |
7360 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
7361 | complain, in_decl); | |
7362 | ||
7363 | if (ctx == error_mark_node || f == error_mark_node) | |
7364 | return error_mark_node; | |
ae58fa02 | 7365 | |
bf8f3f93 MM |
7366 | if (!IS_AGGR_TYPE (ctx)) |
7367 | { | |
c2ea3a40 | 7368 | if (complain & tf_error) |
0f51ccfc | 7369 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
7370 | return error_mark_node; |
7371 | } | |
7372 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
7373 | { | |
7374 | /* Normally, make_typename_type does not require that the CTX | |
7375 | have complete type in order to allow things like: | |
ae58fa02 | 7376 | |
bf8f3f93 | 7377 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 7378 | |
bf8f3f93 MM |
7379 | But, such constructs have already been resolved by this |
7380 | point, so here CTX really should have complete type, unless | |
7381 | it's a partial instantiation. */ | |
4393e105 | 7382 | ctx = complete_type (ctx); |
d0f062fb | 7383 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 7384 | { |
c2ea3a40 | 7385 | if (complain & tf_error) |
7a228918 | 7386 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
7387 | return error_mark_node; |
7388 | } | |
7389 | } | |
ae58fa02 | 7390 | |
fc6a28d7 | 7391 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 7392 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
7393 | if (f == error_mark_node) |
7394 | return f; | |
4f2b0fb2 NS |
7395 | if (TREE_CODE (f) == TYPE_DECL) |
7396 | { | |
7397 | complain |= tf_ignore_bad_quals; | |
7398 | f = TREE_TYPE (f); | |
7399 | } | |
7400 | ||
fc6a28d7 MM |
7401 | if (TREE_CODE (f) != TYPENAME_TYPE) |
7402 | { | |
7403 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
7404 | error ("%qT resolves to %qT, which is not an enumeration type", | |
7405 | t, f); | |
7406 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
7407 | error ("%qT resolves to %qT, which is is not a class type", | |
7408 | t, f); | |
7409 | } | |
7410 | ||
4f2b0fb2 NS |
7411 | return cp_build_qualified_type_real |
7412 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 7413 | } |
c2ea3a40 | 7414 | |
b8c6534b KL |
7415 | case UNBOUND_CLASS_TEMPLATE: |
7416 | { | |
7417 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
7418 | in_decl, /*entering_scope=*/1); | |
7419 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 7420 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
7421 | |
7422 | if (ctx == error_mark_node || name == error_mark_node) | |
7423 | return error_mark_node; | |
7424 | ||
b939a023 KL |
7425 | if (parm_list) |
7426 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
7427 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
7428 | } |
7429 | ||
5566b478 | 7430 | case INDIRECT_REF: |
5566b478 | 7431 | case ADDR_EXPR: |
058b15c1 | 7432 | case CALL_EXPR: |
315fb5db | 7433 | gcc_unreachable (); |
5566b478 MS |
7434 | |
7435 | case ARRAY_REF: | |
4393e105 | 7436 | { |
c2ea3a40 NS |
7437 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
7438 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
7439 | if (e1 == error_mark_node || e2 == error_mark_node) |
7440 | return error_mark_node; | |
7441 | ||
44de5aeb | 7442 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
4393e105 | 7443 | } |
5566b478 | 7444 | |
fc378698 | 7445 | case SCOPE_REF: |
4393e105 | 7446 | { |
c2ea3a40 | 7447 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
7448 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
7449 | if (e1 == error_mark_node || e2 == error_mark_node) | |
7450 | return error_mark_node; | |
7451 | ||
718b8ea5 | 7452 | return build_nt (TREE_CODE (t), e1, e2); |
4393e105 | 7453 | } |
fc378698 | 7454 | |
b894fc05 | 7455 | case TYPEOF_TYPE: |
4393e105 | 7456 | { |
b830b74c | 7457 | tree type; |
4393e105 | 7458 | |
eb34af89 RK |
7459 | type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args, |
7460 | complain, in_decl)); | |
b830b74c | 7461 | return cp_build_qualified_type_real (type, |
dce50630 | 7462 | cp_type_quals (t) |
b830b74c | 7463 | | cp_type_quals (type), |
dce50630 | 7464 | complain); |
4393e105 | 7465 | } |
b894fc05 | 7466 | |
8d08fdba | 7467 | default: |
9e637a26 | 7468 | sorry ("use of %qs in template", |
8d08fdba MS |
7469 | tree_code_name [(int) TREE_CODE (t)]); |
7470 | return error_mark_node; | |
7471 | } | |
7472 | } | |
7473 | ||
ee76b931 MM |
7474 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
7475 | type of the expression on the left-hand side of the "." or "->" | |
7476 | operator. */ | |
7477 | ||
7478 | static tree | |
7479 | tsubst_baselink (tree baselink, tree object_type, | |
7480 | tree args, tsubst_flags_t complain, tree in_decl) | |
7481 | { | |
7482 | tree name; | |
7483 | tree qualifying_scope; | |
7484 | tree fns; | |
7485 | tree template_args = 0; | |
7486 | bool template_id_p = false; | |
7487 | ||
7488 | /* A baselink indicates a function from a base class. The | |
7489 | BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have | |
7490 | non-dependent types; otherwise, the lookup could not have | |
7491 | succeeded. However, they may indicate bases of the template | |
7492 | class, rather than the instantiated class. | |
7493 | ||
7494 | In addition, lookups that were not ambiguous before may be | |
04c06002 | 7495 | ambiguous now. Therefore, we perform the lookup again. */ |
ee76b931 MM |
7496 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
7497 | fns = BASELINK_FUNCTIONS (baselink); | |
7498 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) | |
7499 | { | |
7500 | template_id_p = true; | |
7501 | template_args = TREE_OPERAND (fns, 1); | |
7502 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
7503 | if (template_args) |
7504 | template_args = tsubst_template_args (template_args, args, | |
7505 | complain, in_decl); | |
ee76b931 MM |
7506 | } |
7507 | name = DECL_NAME (get_first_fn (fns)); | |
7508 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
9b60dfd7 MM |
7509 | |
7510 | /* If lookup found a single function, mark it as used at this | |
7511 | point. (If it lookup found multiple functions the one selected | |
7512 | later by overload resolution will be marked as used at that | |
7513 | point.) */ | |
7514 | if (BASELINK_P (baselink)) | |
7515 | fns = BASELINK_FUNCTIONS (baselink); | |
7516 | if (!template_id_p && !really_overloaded_fn (fns)) | |
7517 | mark_used (OVL_CURRENT (fns)); | |
7518 | ||
7519 | /* Add back the template arguments, if present. */ | |
ee76b931 MM |
7520 | if (BASELINK_P (baselink) && template_id_p) |
7521 | BASELINK_FUNCTIONS (baselink) | |
7522 | = build_nt (TEMPLATE_ID_EXPR, | |
7523 | BASELINK_FUNCTIONS (baselink), | |
7524 | template_args); | |
9b60dfd7 | 7525 | |
ee76b931 MM |
7526 | if (!object_type) |
7527 | object_type = current_class_type; | |
7528 | return adjust_result_of_qualified_name_lookup (baselink, | |
7529 | qualifying_scope, | |
7530 | object_type); | |
7531 | } | |
7532 | ||
7533 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
7534 | true if the qualified-id will be a postfix-expression in-and-of | |
7535 | itself; false if more of the postfix-expression follows the | |
7536 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
7537 | of "&". */ | |
7538 | ||
7539 | static tree | |
7540 | tsubst_qualified_id (tree qualified_id, tree args, | |
7541 | tsubst_flags_t complain, tree in_decl, | |
7542 | bool done, bool address_p) | |
7543 | { | |
7544 | tree expr; | |
7545 | tree scope; | |
7546 | tree name; | |
7547 | bool is_template; | |
7548 | tree template_args; | |
7549 | ||
50bc768d | 7550 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 7551 | |
ee76b931 MM |
7552 | /* Figure out what name to look up. */ |
7553 | name = TREE_OPERAND (qualified_id, 1); | |
7554 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
7555 | { | |
7556 | is_template = true; | |
bf12d54d NS |
7557 | template_args = TREE_OPERAND (name, 1); |
7558 | if (template_args) | |
7559 | template_args = tsubst_template_args (template_args, args, | |
7560 | complain, in_decl); | |
ee76b931 MM |
7561 | name = TREE_OPERAND (name, 0); |
7562 | } | |
7563 | else | |
7564 | { | |
7565 | is_template = false; | |
7566 | template_args = NULL_TREE; | |
7567 | } | |
7568 | ||
6eeba0cc MM |
7569 | /* Substitute into the qualifying scope. When there are no ARGS, we |
7570 | are just trying to simplify a non-dependent expression. In that | |
7571 | case the qualifying scope may be dependent, and, in any case, | |
7572 | substituting will not help. */ | |
7573 | scope = TREE_OPERAND (qualified_id, 0); | |
7574 | if (args) | |
7575 | { | |
7576 | scope = tsubst (scope, args, complain, in_decl); | |
7577 | expr = tsubst_copy (name, args, complain, in_decl); | |
7578 | } | |
7579 | else | |
7580 | expr = name; | |
10b1d5e7 | 7581 | |
ab73670a MM |
7582 | if (dependent_type_p (scope)) |
7583 | return build_nt (SCOPE_REF, scope, expr); | |
fb5ce3c9 | 7584 | |
5e08432e | 7585 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f NS |
7586 | { |
7587 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
7588 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL | |
7589 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
7590 | { | |
7591 | if (complain & tf_error) | |
b2693faf | 7592 | { |
0f51ccfc | 7593 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 7594 | "instantiation yields a type", qualified_id); |
0f51ccfc | 7595 | inform ("say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 7596 | } |
12483c9f NS |
7597 | return error_mark_node; |
7598 | } | |
7599 | } | |
fb5ce3c9 | 7600 | |
279b8466 | 7601 | if (DECL_P (expr)) |
8f78f01f MM |
7602 | { |
7603 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
7604 | scope); | |
7605 | /* Remember that there was a reference to this entity. */ | |
7606 | mark_used (expr); | |
7607 | } | |
7608 | ||
7609 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
7610 | { | |
7611 | if (complain & tf_error) | |
7612 | qualified_name_lookup_error (scope, | |
7613 | TREE_OPERAND (qualified_id, 1), | |
7614 | expr); | |
7615 | return error_mark_node; | |
7616 | } | |
ee76b931 MM |
7617 | |
7618 | if (is_template) | |
10b1d5e7 | 7619 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 7620 | |
22038b2c | 7621 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f MM |
7622 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
7623 | expr); | |
22038b2c | 7624 | else if (TYPE_P (scope)) |
ee76b931 MM |
7625 | { |
7626 | expr = (adjust_result_of_qualified_name_lookup | |
7627 | (expr, scope, current_class_type)); | |
7628 | expr = finish_qualified_id_expr (scope, expr, done, address_p); | |
7629 | } | |
5aa56f9d NS |
7630 | |
7631 | expr = convert_from_reference (expr); | |
ee76b931 MM |
7632 | |
7633 | return expr; | |
7634 | } | |
7635 | ||
00d3396f JM |
7636 | /* Like tsubst, but deals with expressions. This function just replaces |
7637 | template parms; to finish processing the resultant expression, use | |
7638 | tsubst_expr. */ | |
7639 | ||
14d22dd6 | 7640 | static tree |
3a978d72 | 7641 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
7642 | { |
7643 | enum tree_code code; | |
8452b1d3 | 7644 | tree r; |
8d08fdba | 7645 | |
5566b478 MS |
7646 | if (t == NULL_TREE || t == error_mark_node) |
7647 | return t; | |
7648 | ||
7649 | code = TREE_CODE (t); | |
b7484fbe | 7650 | |
5566b478 MS |
7651 | switch (code) |
7652 | { | |
7653 | case PARM_DECL: | |
a723baf1 | 7654 | r = retrieve_local_specialization (t); |
50bc768d | 7655 | gcc_assert (r != NULL); |
c0694c4b | 7656 | mark_used (r); |
a723baf1 | 7657 | return r; |
5566b478 MS |
7658 | |
7659 | case CONST_DECL: | |
ed44da02 MM |
7660 | { |
7661 | tree enum_type; | |
7662 | tree v; | |
7663 | ||
a723baf1 MM |
7664 | if (DECL_TEMPLATE_PARM_P (t)) |
7665 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
7666 | /* There is no need to substitute into namespace-scope |
7667 | enumerators. */ | |
7668 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 7669 | return t; |
d5a10cf0 MM |
7670 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
7671 | if (args == NULL_TREE) | |
8a784e4a | 7672 | return integral_constant_value (t); |
ed44da02 MM |
7673 | |
7674 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 MM |
7675 | Consider: |
7676 | ||
7677 | template <int I> int f() { | |
7678 | enum E { a = I }; | |
7679 | struct S { void g() { E e = a; } }; | |
7680 | }; | |
7681 | ||
7682 | When we instantiate f<7>::S::g(), say, lookup_name is not | |
7683 | clever enough to find f<7>::a. */ | |
ed44da02 | 7684 | enum_type |
4393e105 | 7685 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, |
ed44da02 MM |
7686 | /*entering_scope=*/0); |
7687 | ||
7688 | for (v = TYPE_VALUES (enum_type); | |
7689 | v != NULL_TREE; | |
7690 | v = TREE_CHAIN (v)) | |
7691 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
7692 | return TREE_VALUE (v); | |
7693 | ||
7694 | /* We didn't find the name. That should never happen; if | |
7695 | name-lookup found it during preliminary parsing, we | |
7696 | should find it again here during instantiation. */ | |
315fb5db | 7697 | gcc_unreachable (); |
ed44da02 | 7698 | } |
db04386f | 7699 | return t; |
ed44da02 | 7700 | |
5566b478 MS |
7701 | case FIELD_DECL: |
7702 | if (DECL_CONTEXT (t)) | |
7703 | { | |
0978790f | 7704 | tree ctx; |
0978790f | 7705 | |
4393e105 | 7706 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 7707 | /*entering_scope=*/1); |
0978790f | 7708 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
7709 | { |
7710 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
7711 | if (!r) | |
7712 | { | |
7713 | if (complain & tf_error) | |
7714 | error ("using invalid field %qD", t); | |
7715 | return error_mark_node; | |
7716 | } | |
7717 | return r; | |
7718 | } | |
5566b478 | 7719 | } |
fa743e8c | 7720 | |
5566b478 MS |
7721 | return t; |
7722 | ||
7723 | case VAR_DECL: | |
7724 | case FUNCTION_DECL: | |
a723baf1 MM |
7725 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
7726 | || local_variable_p (t)) | |
4393e105 | 7727 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
7728 | mark_used (t); |
7729 | return t; | |
7730 | ||
a723baf1 | 7731 | case BASELINK: |
ee76b931 | 7732 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 7733 | |
98c1c668 | 7734 | case TEMPLATE_DECL: |
a723baf1 MM |
7735 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
7736 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), | |
7737 | args, complain, in_decl); | |
c7222c02 | 7738 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 7739 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
7740 | else if (DECL_CLASS_SCOPE_P (t) |
7741 | && uses_template_parms (DECL_CONTEXT (t))) | |
7742 | { | |
7743 | /* Template template argument like the following example need | |
7744 | special treatment: | |
7745 | ||
7746 | template <template <class> class TT> struct C {}; | |
7747 | template <class T> struct D { | |
7748 | template <class U> struct E {}; | |
7749 | C<E> c; // #1 | |
7750 | }; | |
7751 | D<int> d; // #2 | |
7752 | ||
7753 | We are processing the template argument `E' in #1 for | |
7754 | the template instantiation #2. Originally, `E' is a | |
7755 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
7756 | have to substitute this with one having context `D<int>'. */ | |
7757 | ||
7758 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
7759 | return lookup_field (context, DECL_NAME(t), 0, false); | |
7760 | } | |
98c1c668 | 7761 | else |
fcea7401 | 7762 | /* Ordinary template template argument. */ |
98c1c668 JM |
7763 | return t; |
7764 | ||
5566b478 MS |
7765 | case CAST_EXPR: |
7766 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
7767 | case CONST_CAST_EXPR: |
7768 | case STATIC_CAST_EXPR: | |
7769 | case DYNAMIC_CAST_EXPR: | |
51924768 | 7770 | case NOP_EXPR: |
5566b478 | 7771 | return build1 |
4393e105 MM |
7772 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
7773 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
7774 | |
7775 | case INDIRECT_REF: | |
5566b478 MS |
7776 | case NEGATE_EXPR: |
7777 | case TRUTH_NOT_EXPR: | |
b87692e5 | 7778 | case BIT_NOT_EXPR: |
5566b478 MS |
7779 | case ADDR_EXPR: |
7780 | case CONVERT_EXPR: /* Unary + */ | |
7781 | case SIZEOF_EXPR: | |
abff8e06 | 7782 | case ALIGNOF_EXPR: |
5566b478 | 7783 | case ARROW_EXPR: |
fc378698 | 7784 | case THROW_EXPR: |
5156628f | 7785 | case TYPEID_EXPR: |
f5733617 SS |
7786 | case REALPART_EXPR: |
7787 | case IMAGPART_EXPR: | |
5566b478 | 7788 | return build1 |
6a629cac | 7789 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 7790 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 7791 | |
a723baf1 MM |
7792 | case COMPONENT_REF: |
7793 | { | |
7794 | tree object; | |
7795 | tree name; | |
7796 | ||
7797 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
7798 | name = TREE_OPERAND (t, 1); | |
7799 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
7800 | { | |
7801 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7802 | complain, in_decl); | |
7803 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
7804 | } | |
7805 | else if (TREE_CODE (name) == SCOPE_REF | |
7806 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
7807 | { | |
7808 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7809 | complain, in_decl); | |
7810 | name = TREE_OPERAND (name, 1); | |
7811 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
7812 | complain, in_decl); | |
7813 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
7814 | name = build_nt (SCOPE_REF, base, name); | |
7815 | } | |
ee76b931 MM |
7816 | else if (TREE_CODE (name) == BASELINK) |
7817 | name = tsubst_baselink (name, | |
7818 | non_reference (TREE_TYPE (object)), | |
7819 | args, complain, | |
7820 | in_decl); | |
a723baf1 | 7821 | else |
ee76b931 | 7822 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 7823 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
7824 | } |
7825 | ||
5566b478 MS |
7826 | case PLUS_EXPR: |
7827 | case MINUS_EXPR: | |
7828 | case MULT_EXPR: | |
7829 | case TRUNC_DIV_EXPR: | |
7830 | case CEIL_DIV_EXPR: | |
7831 | case FLOOR_DIV_EXPR: | |
7832 | case ROUND_DIV_EXPR: | |
7833 | case EXACT_DIV_EXPR: | |
7834 | case BIT_AND_EXPR: | |
5566b478 MS |
7835 | case BIT_IOR_EXPR: |
7836 | case BIT_XOR_EXPR: | |
7837 | case TRUNC_MOD_EXPR: | |
7838 | case FLOOR_MOD_EXPR: | |
7839 | case TRUTH_ANDIF_EXPR: | |
7840 | case TRUTH_ORIF_EXPR: | |
7841 | case TRUTH_AND_EXPR: | |
7842 | case TRUTH_OR_EXPR: | |
7843 | case RSHIFT_EXPR: | |
7844 | case LSHIFT_EXPR: | |
7845 | case RROTATE_EXPR: | |
7846 | case LROTATE_EXPR: | |
7847 | case EQ_EXPR: | |
7848 | case NE_EXPR: | |
7849 | case MAX_EXPR: | |
7850 | case MIN_EXPR: | |
7851 | case LE_EXPR: | |
7852 | case GE_EXPR: | |
7853 | case LT_EXPR: | |
7854 | case GT_EXPR: | |
5566b478 MS |
7855 | case COMPOUND_EXPR: |
7856 | case SCOPE_REF: | |
7857 | case DOTSTAR_EXPR: | |
7858 | case MEMBER_REF: | |
519c9806 MM |
7859 | case PREDECREMENT_EXPR: |
7860 | case PREINCREMENT_EXPR: | |
7861 | case POSTDECREMENT_EXPR: | |
7862 | case POSTINCREMENT_EXPR: | |
5566b478 | 7863 | return build_nt |
4393e105 MM |
7864 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7865 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 7866 | |
d8987adb NS |
7867 | case ARRAY_REF: |
7868 | return build_nt | |
7869 | (ARRAY_REF, | |
7870 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
7871 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7872 | NULL_TREE, NULL_TREE); | |
7873 | ||
5566b478 | 7874 | case CALL_EXPR: |
a723baf1 MM |
7875 | return build_nt (code, |
7876 | tsubst_copy (TREE_OPERAND (t, 0), args, | |
7877 | complain, in_decl), | |
7878 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, | |
7879 | in_decl), | |
7880 | NULL_TREE); | |
5566b478 | 7881 | |
5566b478 MS |
7882 | case COND_EXPR: |
7883 | case MODOP_EXPR: | |
40242ccf | 7884 | case PSEUDO_DTOR_EXPR: |
67da3287 | 7885 | { |
8452b1d3 | 7886 | r = build_nt |
4393e105 MM |
7887 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7888 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7889 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 7890 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
7891 | return r; |
7892 | } | |
5566b478 MS |
7893 | |
7894 | case NEW_EXPR: | |
7895 | { | |
8452b1d3 | 7896 | r = build_nt |
4393e105 MM |
7897 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7898 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
7899 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
7900 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
7901 | return r; | |
7902 | } | |
7903 | ||
7904 | case DELETE_EXPR: | |
7905 | { | |
8452b1d3 | 7906 | r = build_nt |
4393e105 MM |
7907 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
7908 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
7909 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
7910 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
7911 | return r; | |
7912 | } | |
7913 | ||
386b8a85 JM |
7914 | case TEMPLATE_ID_EXPR: |
7915 | { | |
00d3396f | 7916 | /* Substituted template arguments */ |
a91db711 NS |
7917 | tree fn = TREE_OPERAND (t, 0); |
7918 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 7919 | |
a91db711 | 7920 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
7921 | if (targs) |
7922 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
a91db711 NS |
7923 | |
7924 | return lookup_template_function (fn, targs); | |
386b8a85 JM |
7925 | } |
7926 | ||
5566b478 MS |
7927 | case TREE_LIST: |
7928 | { | |
7929 | tree purpose, value, chain; | |
7930 | ||
7931 | if (t == void_list_node) | |
7932 | return t; | |
7933 | ||
7934 | purpose = TREE_PURPOSE (t); | |
7935 | if (purpose) | |
4393e105 | 7936 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
7937 | value = TREE_VALUE (t); |
7938 | if (value) | |
4393e105 | 7939 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
7940 | chain = TREE_CHAIN (t); |
7941 | if (chain && chain != void_type_node) | |
4393e105 | 7942 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
7943 | if (purpose == TREE_PURPOSE (t) |
7944 | && value == TREE_VALUE (t) | |
7945 | && chain == TREE_CHAIN (t)) | |
7946 | return t; | |
7947 | return tree_cons (purpose, value, chain); | |
7948 | } | |
7949 | ||
7950 | case RECORD_TYPE: | |
7951 | case UNION_TYPE: | |
7952 | case ENUMERAL_TYPE: | |
7953 | case INTEGER_TYPE: | |
7954 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 7955 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 7956 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 7957 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
7958 | case POINTER_TYPE: |
7959 | case REFERENCE_TYPE: | |
7960 | case OFFSET_TYPE: | |
7961 | case FUNCTION_TYPE: | |
7962 | case METHOD_TYPE: | |
7963 | case ARRAY_TYPE: | |
7964 | case TYPENAME_TYPE: | |
b8c6534b | 7965 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 7966 | case TYPEOF_TYPE: |
f84b4be9 | 7967 | case TYPE_DECL: |
4393e105 | 7968 | return tsubst (t, args, complain, in_decl); |
5566b478 | 7969 | |
e92cc029 | 7970 | case IDENTIFIER_NODE: |
421844e7 | 7971 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
7972 | { |
7973 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 7974 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 7975 | } |
e92cc029 MS |
7976 | else |
7977 | return t; | |
7978 | ||
5156628f | 7979 | case CONSTRUCTOR: |
8452b1d3 | 7980 | { |
dcf92453 ZW |
7981 | r = build_constructor |
7982 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
7983 | tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl)); | |
8452b1d3 JM |
7984 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
7985 | return r; | |
7986 | } | |
5156628f | 7987 | |
371534a9 | 7988 | case VA_ARG_EXPR: |
ea333e1c | 7989 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
7990 | in_decl), |
7991 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 7992 | |
543a0daa RH |
7993 | case CLEANUP_POINT_EXPR: |
7994 | /* We shouldn't have built any of these during initial template | |
7995 | generation. Instead, they should be built during instantiation | |
7996 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 7997 | gcc_unreachable (); |
543a0daa | 7998 | |
5566b478 MS |
7999 | default: |
8000 | return t; | |
8001 | } | |
8002 | } | |
8003 | ||
cc23546e JO |
8004 | /* Like tsubst_copy for expressions, etc. but also does semantic |
8005 | processing. */ | |
00d3396f | 8006 | |
14d22dd6 | 8007 | static tree |
3a978d72 | 8008 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 | 8009 | { |
fd10dd09 | 8010 | tree stmt, tmp; |
558475f0 | 8011 | |
5566b478 MS |
8012 | if (t == NULL_TREE || t == error_mark_node) |
8013 | return t; | |
8014 | ||
93409b8c PB |
8015 | if (EXPR_HAS_LOCATION (t)) |
8016 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
8017 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
8018 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
8019 | ||
5566b478 | 8020 | switch (TREE_CODE (t)) |
8d08fdba | 8021 | { |
325c3691 RH |
8022 | case STATEMENT_LIST: |
8023 | { | |
8024 | tree_stmt_iterator i; | |
8025 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
8026 | tsubst_expr (tsi_stmt (i), args, complain, in_decl); | |
8027 | break; | |
8028 | } | |
8029 | ||
558475f0 | 8030 | case CTOR_INITIALIZER: |
2282d28d MM |
8031 | finish_mem_initializers (tsubst_initializer_list |
8032 | (TREE_OPERAND (t, 0), args)); | |
8033 | break; | |
558475f0 | 8034 | |
5088b058 RH |
8035 | case RETURN_EXPR: |
8036 | finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0), | |
4393e105 | 8037 | args, complain, in_decl)); |
5566b478 MS |
8038 | break; |
8039 | ||
8040 | case EXPR_STMT: | |
7c34ced1 RH |
8041 | tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl); |
8042 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) | |
8043 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
8044 | else | |
8045 | finish_expr_stmt (tmp); | |
8046 | break; | |
5566b478 | 8047 | |
9da99f7d | 8048 | case USING_STMT: |
9da99f7d NS |
8049 | do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t), |
8050 | args, complain, in_decl)); | |
8051 | break; | |
8052 | ||
350fae66 | 8053 | case DECL_EXPR: |
5566b478 | 8054 | { |
e0942dcd MM |
8055 | tree decl; |
8056 | tree init; | |
5566b478 | 8057 | |
350fae66 | 8058 | decl = DECL_EXPR_DECL (t); |
acef433b MM |
8059 | if (TREE_CODE (decl) == LABEL_DECL) |
8060 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
8061 | else if (TREE_CODE (decl) == USING_DECL) |
8062 | { | |
8063 | tree scope = DECL_INITIAL (decl); | |
8064 | tree name = DECL_NAME (decl); | |
22038b2c | 8065 | tree decl; |
fbfe8c9e NS |
8066 | |
8067 | scope = tsubst_expr (scope, args, complain, in_decl); | |
22038b2c | 8068 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
8069 | /*is_type_p=*/false, |
8070 | /*complain=*/false); | |
8f78f01f MM |
8071 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
8072 | qualified_name_lookup_error (scope, name, decl); | |
22038b2c | 8073 | else |
ed5f054f | 8074 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 8075 | } |
acef433b MM |
8076 | else |
8077 | { | |
8078 | init = DECL_INITIAL (decl); | |
8079 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
8080 | if (decl != error_mark_node) |
8081 | { | |
8082 | if (init) | |
8083 | DECL_INITIAL (decl) = error_mark_node; | |
8084 | /* By marking the declaration as instantiated, we avoid | |
8085 | trying to instantiate it. Since instantiate_decl can't | |
8086 | handle local variables, and since we've already done | |
8087 | all that needs to be done, that's the right thing to | |
8088 | do. */ | |
8089 | if (TREE_CODE (decl) == VAR_DECL) | |
8090 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
8091 | if (TREE_CODE (decl) == VAR_DECL |
8092 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
8093 | /* Anonymous aggregates are a special case. */ | |
8094 | finish_anon_union (decl); | |
8095 | else | |
ed2fa432 | 8096 | { |
edebf865 | 8097 | maybe_push_decl (decl); |
39703eb9 MM |
8098 | if (TREE_CODE (decl) == VAR_DECL |
8099 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
8100 | { |
8101 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
8102 | initializer. */ | |
8103 | const char *const name | |
8104 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 8105 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
8106 | } |
8107 | else | |
8108 | init = tsubst_expr (init, args, complain, in_decl); | |
8109 | cp_finish_decl (decl, init, NULL_TREE, 0); | |
ed2fa432 | 8110 | } |
ce2e5191 | 8111 | } |
acef433b | 8112 | } |
fd10dd09 | 8113 | |
350fae66 | 8114 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
8115 | clause of an if/for/while construct. */ |
8116 | return decl; | |
5566b478 | 8117 | } |
8d08fdba | 8118 | |
5566b478 | 8119 | case FOR_STMT: |
7c34ced1 RH |
8120 | stmt = begin_for_stmt (); |
8121 | tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl); | |
8122 | finish_for_init_stmt (stmt); | |
8123 | tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl); | |
8124 | finish_for_cond (tmp, stmt); | |
8125 | tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl); | |
8126 | finish_for_expr (tmp, stmt); | |
8127 | tsubst_expr (FOR_BODY (t), args, complain, in_decl); | |
8128 | finish_for_stmt (stmt); | |
5566b478 | 8129 | break; |
8d08fdba | 8130 | |
5566b478 | 8131 | case WHILE_STMT: |
7c34ced1 RH |
8132 | stmt = begin_while_stmt (); |
8133 | tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl); | |
8134 | finish_while_stmt_cond (tmp, stmt); | |
8135 | tsubst_expr (WHILE_BODY (t), args, complain, in_decl); | |
8136 | finish_while_stmt (stmt); | |
5566b478 | 8137 | break; |
8d08fdba | 8138 | |
5566b478 | 8139 | case DO_STMT: |
7c34ced1 RH |
8140 | stmt = begin_do_stmt (); |
8141 | tsubst_expr (DO_BODY (t), args, complain, in_decl); | |
8142 | finish_do_body (stmt); | |
8143 | tmp = tsubst_expr (DO_COND (t), args, complain, in_decl); | |
8144 | finish_do_stmt (tmp, stmt); | |
5566b478 | 8145 | break; |
a0a33927 | 8146 | |
5566b478 | 8147 | case IF_STMT: |
7c34ced1 RH |
8148 | stmt = begin_if_stmt (); |
8149 | tmp = tsubst_expr (IF_COND (t), args, complain, in_decl); | |
8150 | finish_if_stmt_cond (tmp, stmt); | |
8151 | tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl); | |
8152 | finish_then_clause (stmt); | |
8d08fdba | 8153 | |
7c34ced1 RH |
8154 | if (ELSE_CLAUSE (t)) |
8155 | { | |
8156 | begin_else_clause (stmt); | |
8157 | tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl); | |
8158 | finish_else_clause (stmt); | |
8159 | } | |
8160 | ||
8161 | finish_if_stmt (stmt); | |
5566b478 | 8162 | break; |
8d08fdba | 8163 | |
5882f0f3 | 8164 | case BIND_EXPR: |
7c34ced1 RH |
8165 | if (BIND_EXPR_BODY_BLOCK (t)) |
8166 | stmt = begin_function_body (); | |
8167 | else | |
8168 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
8169 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 8170 | |
7c34ced1 | 8171 | tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl); |
ade3dc07 | 8172 | |
7c34ced1 RH |
8173 | if (BIND_EXPR_BODY_BLOCK (t)) |
8174 | finish_function_body (stmt); | |
8175 | else | |
8176 | finish_compound_stmt (stmt); | |
5566b478 | 8177 | break; |
8d08fdba | 8178 | |
5566b478 | 8179 | case BREAK_STMT: |
ad321293 | 8180 | finish_break_stmt (); |
5566b478 | 8181 | break; |
8d08fdba | 8182 | |
6467930b | 8183 | case CONTINUE_STMT: |
ad321293 | 8184 | finish_continue_stmt (); |
6467930b MS |
8185 | break; |
8186 | ||
5566b478 | 8187 | case SWITCH_STMT: |
7c34ced1 | 8188 | stmt = begin_switch_stmt (); |
ebaae582 | 8189 | tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl); |
7c34ced1 | 8190 | finish_switch_cond (tmp, stmt); |
ebaae582 | 8191 | tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl); |
7c34ced1 | 8192 | finish_switch_stmt (stmt); |
5566b478 MS |
8193 | break; |
8194 | ||
8c161995 | 8195 | case CASE_LABEL_EXPR: |
4393e105 | 8196 | finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl), |
56cb9733 MM |
8197 | tsubst_expr (CASE_HIGH (t), args, complain, |
8198 | in_decl)); | |
5566b478 MS |
8199 | break; |
8200 | ||
9e14e18f | 8201 | case LABEL_EXPR: |
9e14e18f | 8202 | finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t))); |
5566b478 MS |
8203 | break; |
8204 | ||
9e14e18f | 8205 | case GOTO_EXPR: |
fd10dd09 JM |
8206 | tmp = GOTO_DESTINATION (t); |
8207 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
8208 | /* Computed goto's must be tsubst'd into. On the other hand, |
8209 | non-computed gotos must not be; the identifier in question | |
8210 | will have no binding. */ | |
fd10dd09 | 8211 | tmp = tsubst_expr (tmp, args, complain, in_decl); |
3fa56191 | 8212 | else |
fd10dd09 JM |
8213 | tmp = DECL_NAME (tmp); |
8214 | finish_goto_stmt (tmp); | |
ad321293 MM |
8215 | break; |
8216 | ||
e130a54b | 8217 | case ASM_EXPR: |
c87978aa | 8218 | tmp = finish_asm_stmt |
6de9cd9a | 8219 | (ASM_VOLATILE_P (t), |
c87978aa JM |
8220 | tsubst_expr (ASM_STRING (t), args, complain, in_decl), |
8221 | tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl), | |
8222 | tsubst_expr (ASM_INPUTS (t), args, complain, in_decl), | |
8223 | tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl)); | |
131263fa AP |
8224 | { |
8225 | tree asm_expr = tmp; | |
8226 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
8227 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
8228 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
8229 | } | |
5566b478 | 8230 | break; |
faf5394a MS |
8231 | |
8232 | case TRY_BLOCK: | |
f1dedc31 | 8233 | if (CLEANUP_P (t)) |
62409b39 | 8234 | { |
57b52417 | 8235 | stmt = begin_try_block (); |
62409b39 MM |
8236 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); |
8237 | finish_cleanup_try_block (stmt); | |
8238 | finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args, | |
8239 | complain, in_decl), | |
8240 | stmt); | |
8241 | } | |
f1dedc31 MM |
8242 | else |
8243 | { | |
62409b39 MM |
8244 | if (FN_TRY_BLOCK_P (t)) |
8245 | stmt = begin_function_try_block (); | |
8246 | else | |
8247 | stmt = begin_try_block (); | |
8248 | ||
8249 | tsubst_expr (TRY_STMTS (t), args, complain, in_decl); | |
8250 | ||
8251 | if (FN_TRY_BLOCK_P (t)) | |
8252 | finish_function_try_block (stmt); | |
8253 | else | |
8254 | finish_try_block (stmt); | |
8255 | ||
fd10dd09 | 8256 | tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl); |
84df082b MM |
8257 | if (FN_TRY_BLOCK_P (t)) |
8258 | finish_function_handler_sequence (stmt); | |
8259 | else | |
8260 | finish_handler_sequence (stmt); | |
f1dedc31 | 8261 | } |
faf5394a | 8262 | break; |
b35d4555 | 8263 | |
faf5394a | 8264 | case HANDLER: |
b35d4555 MM |
8265 | { |
8266 | tree decl; | |
b35d4555 | 8267 | |
b35d4555 MM |
8268 | stmt = begin_handler (); |
8269 | if (HANDLER_PARMS (t)) | |
8270 | { | |
325c3691 | 8271 | decl = HANDLER_PARMS (t); |
b35d4555 | 8272 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
8273 | /* Prevent instantiate_decl from trying to instantiate |
8274 | this variable. We've already done all that needs to be | |
8275 | done. */ | |
8276 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 MM |
8277 | } |
8278 | else | |
8279 | decl = NULL_TREE; | |
1a6025b4 | 8280 | finish_handler_parms (decl, stmt); |
b35d4555 | 8281 | tsubst_expr (HANDLER_BODY (t), args, complain, in_decl); |
1a6025b4 | 8282 | finish_handler (stmt); |
b35d4555 | 8283 | } |
faf5394a MS |
8284 | break; |
8285 | ||
b87692e5 | 8286 | case TAG_DEFN: |
fd10dd09 | 8287 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 MS |
8288 | break; |
8289 | ||
5566b478 | 8290 | default: |
315fb5db NS |
8291 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
8292 | ||
8293 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
8294 | /*function_p=*/false); | |
5566b478 | 8295 | } |
fd10dd09 | 8296 | |
325c3691 | 8297 | return NULL_TREE; |
8d08fdba MS |
8298 | } |
8299 | ||
b3445994 MM |
8300 | /* T is a postfix-expression that is not being used in a function |
8301 | call. Return the substituted version of T. */ | |
8302 | ||
8303 | static tree | |
8304 | tsubst_non_call_postfix_expression (tree t, tree args, | |
8305 | tsubst_flags_t complain, | |
8306 | tree in_decl) | |
8307 | { | |
8308 | if (TREE_CODE (t) == SCOPE_REF) | |
8309 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
8310 | /*done=*/false, /*address_p=*/false); | |
8311 | else | |
8312 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
8313 | /*function_p=*/false); | |
8314 | ||
8315 | return t; | |
8316 | } | |
8317 | ||
cc23546e | 8318 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 8319 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 8320 | |
ee76b931 | 8321 | tree |
3a978d72 NN |
8322 | tsubst_copy_and_build (tree t, |
8323 | tree args, | |
8324 | tsubst_flags_t complain, | |
b3445994 MM |
8325 | tree in_decl, |
8326 | bool function_p) | |
cc23546e | 8327 | { |
b3445994 MM |
8328 | #define RECUR(NODE) \ |
8329 | tsubst_copy_and_build (NODE, args, complain, in_decl, /*function_p=*/false) | |
8330 | ||
ee76b931 MM |
8331 | tree op1; |
8332 | ||
cc23546e JO |
8333 | if (t == NULL_TREE || t == error_mark_node) |
8334 | return t; | |
8335 | ||
8336 | switch (TREE_CODE (t)) | |
8337 | { | |
399dedb9 NS |
8338 | case USING_DECL: |
8339 | t = DECL_NAME (t); | |
852dcbdd | 8340 | /* Fall through. */ |
b3445994 | 8341 | case IDENTIFIER_NODE: |
cc23546e | 8342 | { |
b3445994 | 8343 | tree decl; |
b3445994 MM |
8344 | cp_id_kind idk; |
8345 | tree qualifying_class; | |
67c03833 | 8346 | bool non_integral_constant_expression_p; |
b3445994 MM |
8347 | const char *error_msg; |
8348 | ||
b3445994 | 8349 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 8350 | { |
b3445994 MM |
8351 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8352 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 8353 | } |
b3445994 MM |
8354 | |
8355 | /* Look up the name. */ | |
10b1d5e7 | 8356 | decl = lookup_name (t, 0); |
b3445994 MM |
8357 | |
8358 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
8359 | failure, not NULL_TREE. */ | |
8360 | if (decl == NULL_TREE) | |
8361 | decl = error_mark_node; | |
8362 | ||
10b1d5e7 | 8363 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 MM |
8364 | &idk, |
8365 | &qualifying_class, | |
67c03833 JM |
8366 | /*integral_constant_expression_p=*/false, |
8367 | /*allow_non_integral_constant_expression_p=*/false, | |
8368 | &non_integral_constant_expression_p, | |
b3445994 MM |
8369 | &error_msg); |
8370 | if (error_msg) | |
8371 | error (error_msg); | |
8372 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
8373 | decl = unqualified_name_lookup_error (decl); | |
8374 | return decl; | |
cc23546e JO |
8375 | } |
8376 | ||
8377 | case TEMPLATE_ID_EXPR: | |
8378 | { | |
8379 | tree object; | |
b3445994 | 8380 | tree template = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
8381 | tree targs = TREE_OPERAND (t, 1); |
8382 | ||
8383 | if (targs) | |
8384 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
cc23546e JO |
8385 | |
8386 | if (TREE_CODE (template) == COMPONENT_REF) | |
8387 | { | |
8388 | object = TREE_OPERAND (template, 0); | |
8389 | template = TREE_OPERAND (template, 1); | |
8390 | } | |
8391 | else | |
8392 | object = NULL_TREE; | |
bd83b409 | 8393 | template = lookup_template_function (template, targs); |
cc23546e JO |
8394 | |
8395 | if (object) | |
f293ce4b RS |
8396 | return build3 (COMPONENT_REF, TREE_TYPE (template), |
8397 | object, template, NULL_TREE); | |
cc23546e JO |
8398 | else |
8399 | return template; | |
8400 | } | |
8401 | ||
8402 | case INDIRECT_REF: | |
db24eb1f NS |
8403 | { |
8404 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
8405 | ||
8406 | if (REFERENCE_REF_P (t)) | |
8407 | { | |
e8c66fe0 NS |
8408 | /* A type conversion to reference type will be enclosed in |
8409 | such an indirect ref, but the substitution of the cast | |
8410 | will have also added such an indirect ref. */ | |
8411 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
8412 | r = convert_from_reference (r); | |
db24eb1f NS |
8413 | } |
8414 | else | |
8415 | r = build_x_indirect_ref (r, "unary *"); | |
8416 | return r; | |
8417 | } | |
cc23546e | 8418 | |
0da99d4e GB |
8419 | case NOP_EXPR: |
8420 | return build_nop | |
8421 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
8422 | RECUR (TREE_OPERAND (t, 0))); | |
8423 | ||
cc23546e JO |
8424 | case CAST_EXPR: |
8425 | return build_functional_cast | |
8426 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8427 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8428 | |
8429 | case REINTERPRET_CAST_EXPR: | |
8430 | return build_reinterpret_cast | |
8431 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8432 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8433 | |
8434 | case CONST_CAST_EXPR: | |
8435 | return build_const_cast | |
8436 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8437 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8438 | |
8439 | case DYNAMIC_CAST_EXPR: | |
8440 | return build_dynamic_cast | |
8441 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8442 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8443 | |
8444 | case STATIC_CAST_EXPR: | |
8445 | return build_static_cast | |
8446 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
b3445994 | 8447 | RECUR (TREE_OPERAND (t, 0))); |
cc23546e | 8448 | |
cc23546e JO |
8449 | case POSTDECREMENT_EXPR: |
8450 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
8451 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
8452 | args, complain, in_decl); | |
ee76b931 MM |
8453 | return build_x_unary_op (TREE_CODE (t), op1); |
8454 | ||
8455 | case PREDECREMENT_EXPR: | |
8456 | case PREINCREMENT_EXPR: | |
cc23546e JO |
8457 | case NEGATE_EXPR: |
8458 | case BIT_NOT_EXPR: | |
cc23546e | 8459 | case ABS_EXPR: |
d17811fd MM |
8460 | case TRUTH_NOT_EXPR: |
8461 | case CONVERT_EXPR: /* Unary + */ | |
8462 | case REALPART_EXPR: | |
8463 | case IMAGPART_EXPR: | |
b3445994 | 8464 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0))); |
cc23546e | 8465 | |
cc23546e | 8466 | case ADDR_EXPR: |
ee76b931 MM |
8467 | op1 = TREE_OPERAND (t, 0); |
8468 | if (TREE_CODE (op1) == SCOPE_REF) | |
8469 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, | |
8470 | /*done=*/true, /*address_p=*/true); | |
8471 | else | |
b3445994 MM |
8472 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
8473 | in_decl); | |
fc2b8477 MM |
8474 | if (TREE_CODE (op1) == LABEL_DECL) |
8475 | return finish_label_address_expr (DECL_NAME (op1)); | |
ee76b931 MM |
8476 | return build_x_unary_op (ADDR_EXPR, op1); |
8477 | ||
cc23546e JO |
8478 | case PLUS_EXPR: |
8479 | case MINUS_EXPR: | |
8480 | case MULT_EXPR: | |
8481 | case TRUNC_DIV_EXPR: | |
8482 | case CEIL_DIV_EXPR: | |
8483 | case FLOOR_DIV_EXPR: | |
8484 | case ROUND_DIV_EXPR: | |
8485 | case EXACT_DIV_EXPR: | |
8486 | case BIT_AND_EXPR: | |
cc23546e JO |
8487 | case BIT_IOR_EXPR: |
8488 | case BIT_XOR_EXPR: | |
8489 | case TRUNC_MOD_EXPR: | |
8490 | case FLOOR_MOD_EXPR: | |
8491 | case TRUTH_ANDIF_EXPR: | |
8492 | case TRUTH_ORIF_EXPR: | |
8493 | case TRUTH_AND_EXPR: | |
8494 | case TRUTH_OR_EXPR: | |
8495 | case RSHIFT_EXPR: | |
8496 | case LSHIFT_EXPR: | |
8497 | case RROTATE_EXPR: | |
8498 | case LROTATE_EXPR: | |
8499 | case EQ_EXPR: | |
8500 | case NE_EXPR: | |
8501 | case MAX_EXPR: | |
8502 | case MIN_EXPR: | |
8503 | case LE_EXPR: | |
8504 | case GE_EXPR: | |
8505 | case LT_EXPR: | |
8506 | case GT_EXPR: | |
8507 | case MEMBER_REF: | |
b3445994 | 8508 | case DOTSTAR_EXPR: |
cc23546e JO |
8509 | return build_x_binary_op |
8510 | (TREE_CODE (t), | |
b3445994 | 8511 | RECUR (TREE_OPERAND (t, 0)), |
ec835fb2 MM |
8512 | RECUR (TREE_OPERAND (t, 1)), |
8513 | /*overloaded_p=*/NULL); | |
cc23546e JO |
8514 | |
8515 | case SCOPE_REF: | |
ee76b931 MM |
8516 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
8517 | /*address_p=*/false); | |
cc23546e | 8518 | case ARRAY_REF: |
b3445994 MM |
8519 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
8520 | args, complain, in_decl); | |
d8987adb NS |
8521 | return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)), |
8522 | /*overloaded_p=*/NULL); | |
8523 | ||
cc23546e JO |
8524 | case SIZEOF_EXPR: |
8525 | case ALIGNOF_EXPR: | |
d17811fd MM |
8526 | op1 = TREE_OPERAND (t, 0); |
8527 | if (!args) | |
8528 | { | |
8529 | /* When there are no ARGS, we are trying to evaluate a | |
8530 | non-dependent expression from the parser. Trying to do | |
8531 | the substitutions may not work. */ | |
8532 | if (!TYPE_P (op1)) | |
8533 | op1 = TREE_TYPE (op1); | |
8534 | } | |
8535 | else | |
8536 | { | |
8537 | ++skip_evaluation; | |
b3445994 | 8538 | op1 = RECUR (op1); |
d17811fd MM |
8539 | --skip_evaluation; |
8540 | } | |
7a18b933 NS |
8541 | if (TYPE_P (op1)) |
8542 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true); | |
d17811fd | 8543 | else |
7a18b933 | 8544 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t)); |
cc23546e JO |
8545 | |
8546 | case MODOP_EXPR: | |
e4c2c34b JM |
8547 | { |
8548 | tree r = build_x_modify_expr | |
8549 | (RECUR (TREE_OPERAND (t, 0)), | |
8550 | TREE_CODE (TREE_OPERAND (t, 1)), | |
8551 | RECUR (TREE_OPERAND (t, 2))); | |
bcf9a914 JM |
8552 | /* TREE_NO_WARNING must be set if either the expression was |
8553 | parenthesized or it uses an operator such as >>= rather | |
8554 | than plain assignment. In the former case, it was already | |
8555 | set and must be copied. In the latter case, | |
8556 | build_x_modify_expr sets it and it must not be reset | |
8557 | here. */ | |
8558 | if (TREE_NO_WARNING (t)) | |
8559 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
8560 | return r; |
8561 | } | |
cc23546e JO |
8562 | |
8563 | case ARROW_EXPR: | |
b3445994 MM |
8564 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
8565 | args, complain, in_decl); | |
ee76b931 MM |
8566 | /* Remember that there was a reference to this entity. */ |
8567 | if (DECL_P (op1)) | |
8568 | mark_used (op1); | |
8569 | return build_x_arrow (op1); | |
cc23546e JO |
8570 | |
8571 | case NEW_EXPR: | |
8572 | return build_new | |
b3445994 MM |
8573 | (RECUR (TREE_OPERAND (t, 0)), |
8574 | RECUR (TREE_OPERAND (t, 1)), | |
8575 | RECUR (TREE_OPERAND (t, 2)), | |
058b15c1 | 8576 | RECUR (TREE_OPERAND (t, 3)), |
cc23546e JO |
8577 | NEW_EXPR_USE_GLOBAL (t)); |
8578 | ||
8579 | case DELETE_EXPR: | |
8580 | return delete_sanity | |
b3445994 MM |
8581 | (RECUR (TREE_OPERAND (t, 0)), |
8582 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
8583 | DELETE_EXPR_USE_VEC (t), |
8584 | DELETE_EXPR_USE_GLOBAL (t)); | |
8585 | ||
8586 | case COMPOUND_EXPR: | |
b3445994 MM |
8587 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
8588 | RECUR (TREE_OPERAND (t, 1))); | |
cc23546e | 8589 | |
ee76b931 | 8590 | case CALL_EXPR: |
cc23546e | 8591 | { |
ee76b931 MM |
8592 | tree function; |
8593 | tree call_args; | |
ee76b931 | 8594 | bool qualified_p; |
ee935db4 | 8595 | bool koenig_p; |
ee76b931 MM |
8596 | |
8597 | function = TREE_OPERAND (t, 0); | |
6d80c4b9 MM |
8598 | /* When we parsed the expression, we determined whether or |
8599 | not Koenig lookup should be performed. */ | |
8600 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 8601 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 8602 | { |
ee76b931 MM |
8603 | qualified_p = true; |
8604 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
8605 | /*done=*/false, | |
8606 | /*address_p=*/false); | |
cc23546e | 8607 | } |
ee76b931 | 8608 | else |
cc23546e | 8609 | { |
ee76b931 MM |
8610 | qualified_p = (TREE_CODE (function) == COMPONENT_REF |
8611 | && (TREE_CODE (TREE_OPERAND (function, 1)) | |
8612 | == SCOPE_REF)); | |
8613 | function = tsubst_copy_and_build (function, args, complain, | |
b3445994 MM |
8614 | in_decl, |
8615 | !qualified_p); | |
6d80c4b9 MM |
8616 | if (BASELINK_P (function)) |
8617 | qualified_p = true; | |
cc23546e | 8618 | } |
cc23546e | 8619 | |
b3445994 | 8620 | call_args = RECUR (TREE_OPERAND (t, 1)); |
676e33ca MM |
8621 | |
8622 | /* We do not perform argument-dependent lookup if normal | |
8623 | lookup finds a non-function, in accordance with the | |
8624 | expected resolution of DR 218. */ | |
ee935db4 | 8625 | if (koenig_p |
44370687 MM |
8626 | && ((is_overloaded_fn (function) |
8627 | /* If lookup found a member function, the Koenig lookup is | |
8628 | not appropriate, even if an unqualified-name was used | |
8629 | to denote the function. */ | |
8630 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
b3445994 | 8631 | || TREE_CODE (function) == IDENTIFIER_NODE)) |
6d80c4b9 MM |
8632 | function = perform_koenig_lookup (function, call_args); |
8633 | ||
8634 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 8635 | { |
6d80c4b9 MM |
8636 | unqualified_name_lookup_error (function); |
8637 | return error_mark_node; | |
b3445994 MM |
8638 | } |
8639 | ||
8640 | /* Remember that there was a reference to this entity. */ | |
8641 | if (DECL_P (function)) | |
8642 | mark_used (function); | |
8643 | ||
d17811fd MM |
8644 | if (TREE_CODE (function) == OFFSET_REF) |
8645 | return build_offset_ref_call_from_tree (function, call_args); | |
8646 | if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 MM |
8647 | { |
8648 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
8649 | return finish_call_expr (function, call_args, | |
8650 | /*disallow_virtual=*/false, | |
8651 | /*koenig_p=*/false); | |
8652 | else | |
8653 | return (build_new_method_call | |
8654 | (TREE_OPERAND (function, 0), | |
8655 | TREE_OPERAND (function, 1), | |
8656 | call_args, NULL_TREE, | |
8657 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)); | |
8658 | } | |
d17811fd | 8659 | return finish_call_expr (function, call_args, |
6d80c4b9 MM |
8660 | /*disallow_virtual=*/qualified_p, |
8661 | koenig_p); | |
cc23546e JO |
8662 | } |
8663 | ||
8664 | case COND_EXPR: | |
8665 | return build_x_conditional_expr | |
b3445994 MM |
8666 | (RECUR (TREE_OPERAND (t, 0)), |
8667 | RECUR (TREE_OPERAND (t, 1)), | |
8668 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
8669 | |
8670 | case PSEUDO_DTOR_EXPR: | |
8671 | return finish_pseudo_destructor_expr | |
b3445994 MM |
8672 | (RECUR (TREE_OPERAND (t, 0)), |
8673 | RECUR (TREE_OPERAND (t, 1)), | |
8674 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
8675 | |
8676 | case TREE_LIST: | |
8677 | { | |
8678 | tree purpose, value, chain; | |
8679 | ||
8680 | if (t == void_list_node) | |
8681 | return t; | |
8682 | ||
8683 | purpose = TREE_PURPOSE (t); | |
8684 | if (purpose) | |
b3445994 | 8685 | purpose = RECUR (purpose); |
cc23546e JO |
8686 | value = TREE_VALUE (t); |
8687 | if (value) | |
b3445994 | 8688 | value = RECUR (value); |
cc23546e JO |
8689 | chain = TREE_CHAIN (t); |
8690 | if (chain && chain != void_type_node) | |
b3445994 | 8691 | chain = RECUR (chain); |
cc23546e JO |
8692 | if (purpose == TREE_PURPOSE (t) |
8693 | && value == TREE_VALUE (t) | |
8694 | && chain == TREE_CHAIN (t)) | |
8695 | return t; | |
8696 | return tree_cons (purpose, value, chain); | |
8697 | } | |
8698 | ||
8699 | case COMPONENT_REF: | |
8700 | { | |
ee76b931 MM |
8701 | tree object; |
8702 | tree member; | |
8703 | ||
b3445994 MM |
8704 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
8705 | args, complain, in_decl); | |
ee76b931 MM |
8706 | /* Remember that there was a reference to this entity. */ |
8707 | if (DECL_P (object)) | |
8708 | mark_used (object); | |
8709 | ||
8710 | member = TREE_OPERAND (t, 1); | |
8711 | if (BASELINK_P (member)) | |
8712 | member = tsubst_baselink (member, | |
8713 | non_reference (TREE_TYPE (object)), | |
8714 | args, complain, in_decl); | |
8715 | else | |
8716 | member = tsubst_copy (member, args, complain, in_decl); | |
cc23546e | 8717 | |
bad1f462 KL |
8718 | if (member == error_mark_node) |
8719 | return error_mark_node; | |
8720 | else if (!CLASS_TYPE_P (TREE_TYPE (object))) | |
cc23546e JO |
8721 | { |
8722 | if (TREE_CODE (member) == BIT_NOT_EXPR) | |
8723 | return finish_pseudo_destructor_expr (object, | |
8724 | NULL_TREE, | |
8725 | TREE_TYPE (object)); | |
8726 | else if (TREE_CODE (member) == SCOPE_REF | |
8727 | && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR)) | |
8728 | return finish_pseudo_destructor_expr (object, | |
8729 | object, | |
8730 | TREE_TYPE (object)); | |
8731 | } | |
8732 | else if (TREE_CODE (member) == SCOPE_REF | |
8733 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
8734 | { | |
8735 | tree tmpl; | |
8736 | tree args; | |
8737 | ||
8738 | /* Lookup the template functions now that we know what the | |
8739 | scope is. */ | |
8740 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
8741 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
22038b2c | 8742 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
8743 | /*is_type_p=*/false, |
8744 | /*complain=*/false); | |
cc23546e | 8745 | if (BASELINK_P (member)) |
44370687 MM |
8746 | { |
8747 | BASELINK_FUNCTIONS (member) | |
8748 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), | |
8749 | args); | |
8750 | member = (adjust_result_of_qualified_name_lookup | |
8751 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
8752 | TREE_TYPE (object))); | |
8753 | } | |
cc23546e JO |
8754 | else |
8755 | { | |
8f78f01f MM |
8756 | qualified_name_lookup_error (TREE_TYPE (object), tmpl, |
8757 | member); | |
cc23546e JO |
8758 | return error_mark_node; |
8759 | } | |
8760 | } | |
2436b51f MM |
8761 | else if (TREE_CODE (member) == SCOPE_REF |
8762 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
8763 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
8764 | { | |
8765 | if (complain & tf_error) | |
8766 | { | |
8767 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
0f51ccfc | 8768 | error ("%qT is not a class or namespace", |
2436b51f MM |
8769 | TREE_OPERAND (member, 0)); |
8770 | else | |
0f51ccfc | 8771 | error ("%qD is not a class or namespace", |
2436b51f MM |
8772 | TREE_OPERAND (member, 0)); |
8773 | } | |
8774 | return error_mark_node; | |
8775 | } | |
a3f10e50 NS |
8776 | else if (TREE_CODE (member) == FIELD_DECL) |
8777 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e JO |
8778 | |
8779 | return finish_class_member_access_expr (object, member); | |
8780 | } | |
8781 | ||
8782 | case THROW_EXPR: | |
8783 | return build_throw | |
b3445994 | 8784 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
8785 | |
8786 | case CONSTRUCTOR: | |
8787 | { | |
8788 | tree r; | |
8789 | tree elts; | |
8790 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
8791 | bool purpose_p; | |
8792 | ||
8793 | /* digest_init will do the wrong thing if we let it. */ | |
8794 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
8795 | return t; | |
8796 | ||
8797 | r = NULL_TREE; | |
8798 | /* We do not want to process the purpose of aggregate | |
8799 | initializers as they are identifier nodes which will be | |
8800 | looked up by digest_init. */ | |
8801 | purpose_p = !(type && IS_AGGR_TYPE (type)); | |
db77ef4c | 8802 | for (elts = CONSTRUCTOR_ELTS (t); |
cc23546e JO |
8803 | elts; |
8804 | elts = TREE_CHAIN (elts)) | |
8805 | { | |
8806 | tree purpose = TREE_PURPOSE (elts); | |
8807 | tree value = TREE_VALUE (elts); | |
8808 | ||
8809 | if (purpose && purpose_p) | |
b3445994 MM |
8810 | purpose = RECUR (purpose); |
8811 | value = RECUR (value); | |
cc23546e JO |
8812 | r = tree_cons (purpose, value, r); |
8813 | } | |
8814 | ||
dcf92453 | 8815 | r = build_constructor (NULL_TREE, nreverse (r)); |
cc23546e JO |
8816 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
8817 | ||
8818 | if (type) | |
8819 | return digest_init (type, r, 0); | |
8820 | return r; | |
8821 | } | |
8822 | ||
8823 | case TYPEID_EXPR: | |
8824 | { | |
b3445994 | 8825 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
cc23546e JO |
8826 | if (TYPE_P (operand_0)) |
8827 | return get_typeid (operand_0); | |
8828 | return build_typeid (operand_0); | |
8829 | } | |
8830 | ||
cc23546e | 8831 | case VAR_DECL: |
db24eb1f NS |
8832 | if (!args) |
8833 | return t; | |
8834 | /* Fall through */ | |
8835 | ||
8836 | case PARM_DECL: | |
8837 | { | |
8838 | tree r = tsubst_copy (t, args, complain, in_decl); | |
8839 | ||
8840 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) | |
8841 | /* If the original type was a reference, we'll be wrapped in | |
8842 | the appropriate INDIRECT_REF. */ | |
8843 | r = convert_from_reference (r); | |
8844 | return r; | |
8845 | } | |
cc23546e JO |
8846 | |
8847 | case VA_ARG_EXPR: | |
b3445994 MM |
8848 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
8849 | tsubst_copy (TREE_TYPE (t), args, complain, | |
8850 | in_decl)); | |
cc23546e | 8851 | |
4bceb077 RH |
8852 | case OFFSETOF_EXPR: |
8853 | return fold_offsetof (RECUR (TREE_OPERAND (t, 0))); | |
8854 | ||
0fe0caa6 RH |
8855 | case STMT_EXPR: |
8856 | { | |
8857 | tree old_stmt_expr = cur_stmt_expr; | |
8858 | tree stmt_expr = begin_stmt_expr (); | |
8859 | ||
8860 | cur_stmt_expr = stmt_expr; | |
8861 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl); | |
8862 | stmt_expr = finish_stmt_expr (stmt_expr, false); | |
8863 | cur_stmt_expr = old_stmt_expr; | |
8864 | ||
8865 | return stmt_expr; | |
8866 | } | |
8867 | ||
a5201a91 MM |
8868 | case CONST_DECL: |
8869 | t = tsubst_copy (t, args, complain, in_decl); | |
8870 | /* As in finish_id_expression, we resolve enumeration constants | |
8871 | to their underlying values. */ | |
8872 | if (TREE_CODE (t) == CONST_DECL) | |
8873 | return DECL_INITIAL (t); | |
8874 | return t; | |
8875 | ||
cc23546e JO |
8876 | default: |
8877 | return tsubst_copy (t, args, complain, in_decl); | |
8878 | } | |
b3445994 MM |
8879 | |
8880 | #undef RECUR | |
cc23546e JO |
8881 | } |
8882 | ||
3e4a3562 NS |
8883 | /* Verify that the instantiated ARGS are valid. For type arguments, |
8884 | make sure that the type's linkage is ok. For non-type arguments, | |
34cd5ae7 | 8885 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 8886 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
8887 | |
8888 | static bool | |
3a978d72 | 8889 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
3e4a3562 NS |
8890 | { |
8891 | int ix, len = DECL_NTPARMS (tmpl); | |
8892 | bool result = false; | |
6c02751b | 8893 | bool error_p = complain & tf_error; |
3e4a3562 NS |
8894 | |
8895 | for (ix = 0; ix != len; ix++) | |
8896 | { | |
8897 | tree t = TREE_VEC_ELT (args, ix); | |
8898 | ||
8899 | if (TYPE_P (t)) | |
8900 | { | |
8901 | /* [basic.link]: A name with no linkage (notably, the name | |
8902 | of a class or enumeration declared in a local scope) | |
8903 | shall not be used to declare an entity with linkage. | |
8904 | This implies that names with no linkage cannot be used as | |
8905 | template arguments. */ | |
4684cd27 | 8906 | tree nt = no_linkage_check (t, /*relaxed_p=*/false); |
3e4a3562 NS |
8907 | |
8908 | if (nt) | |
8909 | { | |
aae4a277 | 8910 | if (TYPE_ANONYMOUS_P (nt)) |
6c02751b | 8911 | error ("%qT is/uses anonymous type", t); |
3e4a3562 | 8912 | else |
0f51ccfc | 8913 | error ("%qT uses local type %qT", t, nt); |
3e4a3562 | 8914 | result = true; |
6c02751b | 8915 | error_p = true; |
3e4a3562 NS |
8916 | } |
8917 | /* In order to avoid all sorts of complications, we do not | |
8918 | allow variably-modified types as template arguments. */ | |
5377d5ba | 8919 | else if (variably_modified_type_p (t, NULL_TREE)) |
3e4a3562 NS |
8920 | { |
8921 | if (complain & tf_error) | |
0f51ccfc | 8922 | error ("%qT is a variably modified type", t); |
3e4a3562 NS |
8923 | result = true; |
8924 | } | |
8925 | } | |
8926 | /* A non-type argument of integral or enumerated type must be a | |
8927 | constant. */ | |
8928 | else if (TREE_TYPE (t) | |
8929 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
8930 | && !TREE_CONSTANT (t)) | |
8931 | { | |
8932 | if (complain & tf_error) | |
0f51ccfc | 8933 | error ("integral expression %qE is not constant", t); |
3e4a3562 NS |
8934 | result = true; |
8935 | } | |
8936 | } | |
6c02751b | 8937 | if (result && error_p) |
0f51ccfc | 8938 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
8939 | return result; |
8940 | } | |
8941 | ||
6ba89f8e | 8942 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
8943 | the template arguments in TARG_PTR. */ |
8944 | ||
5566b478 | 8945 | tree |
3a978d72 | 8946 | instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain) |
8d08fdba | 8947 | { |
5566b478 | 8948 | tree fndecl; |
36a117a5 MM |
8949 | tree gen_tmpl; |
8950 | tree spec; | |
5566b478 | 8951 | |
27fafc8d JM |
8952 | if (tmpl == error_mark_node) |
8953 | return error_mark_node; | |
8954 | ||
50bc768d | 8955 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 8956 | |
db9b2174 MM |
8957 | /* If this function is a clone, handle it specially. */ |
8958 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 8959 | { |
a30f62e0 | 8960 | tree spec; |
3ad97789 NS |
8961 | tree clone; |
8962 | ||
a30f62e0 MM |
8963 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr, |
8964 | complain); | |
8965 | if (spec == error_mark_node) | |
8966 | return error_mark_node; | |
8967 | ||
c6002625 | 8968 | /* Look for the clone. */ |
4684cd27 | 8969 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
8970 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
8971 | return clone; | |
8972 | /* We should always have found the clone by now. */ | |
315fb5db | 8973 | gcc_unreachable (); |
3ad97789 NS |
8974 | return NULL_TREE; |
8975 | } | |
8976 | ||
36a117a5 | 8977 | /* Check to see if we already have this specialization. */ |
c7222c02 MM |
8978 | spec = retrieve_specialization (tmpl, targ_ptr, |
8979 | /*class_specializations_p=*/false); | |
36a117a5 MM |
8980 | if (spec != NULL_TREE) |
8981 | return spec; | |
8982 | ||
f9a7ae04 MM |
8983 | gen_tmpl = most_general_template (tmpl); |
8984 | if (tmpl != gen_tmpl) | |
386b8a85 | 8985 | { |
36a117a5 MM |
8986 | /* The TMPL is a partial instantiation. To get a full set of |
8987 | arguments we must add the arguments used to perform the | |
8988 | partial instantiation. */ | |
8989 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
8990 | targ_ptr); | |
36a117a5 MM |
8991 | |
8992 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
8993 | spec = retrieve_specialization (gen_tmpl, targ_ptr, |
8994 | /*class_specializations_p=*/false); | |
75650646 MM |
8995 | if (spec != NULL_TREE) |
8996 | return spec; | |
386b8a85 JM |
8997 | } |
8998 | ||
3e4a3562 NS |
8999 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
9000 | complain)) | |
9001 | return error_mark_node; | |
9002 | ||
2b907f5c KL |
9003 | /* We are building a FUNCTION_DECL, during which the access of its |
9004 | parameters and return types have to be checked. However this | |
9005 | FUNCTION_DECL which is the desired context for access checking | |
9006 | is not built yet. We solve this chicken-and-egg problem by | |
9007 | deferring all checks until we have the FUNCTION_DECL. */ | |
9008 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 9009 | |
f4f206f4 | 9010 | /* Substitute template parameters. */ |
17aec3eb | 9011 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 9012 | targ_ptr, complain, gen_tmpl); |
5c74d5b0 | 9013 | |
2b907f5c KL |
9014 | /* Now we know the specialization, compute access previously |
9015 | deferred. */ | |
9016 | push_access_scope (fndecl); | |
9017 | perform_deferred_access_checks (); | |
9018 | pop_access_scope (fndecl); | |
9019 | pop_deferring_access_checks (); | |
5c74d5b0 | 9020 | |
36a117a5 MM |
9021 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
9022 | template, not the most general template. */ | |
9023 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 9024 | |
94350948 | 9025 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
9026 | instantiate all the alternate entry points as well. We do this |
9027 | by cloning the instantiation of the main entry point, not by | |
9028 | instantiating the template clones. */ | |
9029 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
9030 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
94350948 | 9031 | |
5566b478 | 9032 | return fndecl; |
8d08fdba | 9033 | } |
5566b478 | 9034 | |
4393e105 MM |
9035 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
9036 | arguments that are being used when calling it. TARGS is a vector | |
9037 | into which the deduced template arguments are placed. | |
8d08fdba MS |
9038 | |
9039 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
9040 | all the types, and 1 for complete failure. An error message will be | |
9041 | printed only for an incomplete match. | |
9042 | ||
e5214479 JM |
9043 | If FN is a conversion operator, or we are trying to produce a specific |
9044 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
9045 | |
9046 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
9047 | template-id. | |
6467930b | 9048 | |
830bfa74 MM |
9049 | The parameter STRICT is one of: |
9050 | ||
9051 | DEDUCE_CALL: | |
9052 | We are deducing arguments for a function call, as in | |
9053 | [temp.deduct.call]. | |
9054 | ||
9055 | DEDUCE_CONV: | |
9056 | We are deducing arguments for a conversion function, as in | |
9057 | [temp.deduct.conv]. | |
9058 | ||
9059 | DEDUCE_EXACT: | |
62e4a758 NS |
9060 | We are deducing arguments when doing an explicit instantiation |
9061 | as in [temp.explicit], when determining an explicit specialization | |
9062 | as in [temp.expl.spec], or when taking the address of a function | |
a34d3336 | 9063 | template, as in [temp.deduct.funcaddr]. */ |
8d08fdba MS |
9064 | |
9065 | int | |
3a978d72 NN |
9066 | fn_type_unification (tree fn, |
9067 | tree explicit_targs, | |
9068 | tree targs, | |
9069 | tree args, | |
9070 | tree return_type, | |
a34d3336 | 9071 | unification_kind_t strict) |
386b8a85 | 9072 | { |
4393e105 MM |
9073 | tree parms; |
9074 | tree fntype; | |
adecb3f4 | 9075 | int result; |
386b8a85 | 9076 | |
50bc768d | 9077 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 9078 | |
4393e105 MM |
9079 | fntype = TREE_TYPE (fn); |
9080 | if (explicit_targs) | |
75650646 | 9081 | { |
4393e105 MM |
9082 | /* [temp.deduct] |
9083 | ||
9084 | The specified template arguments must match the template | |
9085 | parameters in kind (i.e., type, nontype, template), and there | |
9086 | must not be more arguments than there are parameters; | |
9087 | otherwise type deduction fails. | |
9088 | ||
9089 | Nontype arguments must match the types of the corresponding | |
9090 | nontype template parameters, or must be convertible to the | |
9091 | types of the corresponding nontype parameters as specified in | |
9092 | _temp.arg.nontype_, otherwise type deduction fails. | |
9093 | ||
9094 | All references in the function type of the function template | |
9095 | to the corresponding template parameters are replaced by the | |
9096 | specified template argument values. If a substitution in a | |
9097 | template parameter or in the function type of the function | |
9098 | template results in an invalid type, type deduction fails. */ | |
9099 | int i; | |
9100 | tree converted_args; | |
ffd49b19 | 9101 | bool incomplete; |
75650646 | 9102 | |
c8a7ed43 AO |
9103 | if (explicit_targs == error_mark_node) |
9104 | return 1; | |
9105 | ||
4393e105 MM |
9106 | converted_args |
9107 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), | |
c2ea3a40 | 9108 | explicit_targs, NULL_TREE, tf_none, |
4393e105 MM |
9109 | /*require_all_arguments=*/0)); |
9110 | if (converted_args == error_mark_node) | |
75650646 | 9111 | return 1; |
386b8a85 | 9112 | |
ffd49b19 NS |
9113 | /* Substitute the explicit args into the function type. This is |
9114 | necessary so that, for instance, explicitly declared function | |
9115 | arguments can match null pointed constants. If we were given | |
9116 | an incomplete set of explicit args, we must not do semantic | |
9117 | processing during substitution as we could create partial | |
9118 | instantiations. */ | |
9119 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); | |
9120 | processing_template_decl += incomplete; | |
c2ea3a40 | 9121 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 NS |
9122 | processing_template_decl -= incomplete; |
9123 | ||
4393e105 MM |
9124 | if (fntype == error_mark_node) |
9125 | return 1; | |
050367a3 | 9126 | |
4393e105 | 9127 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 9128 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 9129 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 9130 | } |
4393e105 MM |
9131 | |
9132 | parms = TYPE_ARG_TYPES (fntype); | |
e5214479 JM |
9133 | /* Never do unification on the 'this' parameter. */ |
9134 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) | |
9135 | parms = TREE_CHAIN (parms); | |
8d3631f8 NS |
9136 | |
9137 | if (return_type) | |
9138 | { | |
9139 | /* We've been given a return type to match, prepend it. */ | |
9140 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); | |
9141 | args = tree_cons (NULL_TREE, return_type, args); | |
4393e105 MM |
9142 | } |
9143 | ||
4393e105 MM |
9144 | /* We allow incomplete unification without an error message here |
9145 | because the standard doesn't seem to explicitly prohibit it. Our | |
9146 | callers must be ready to deal with unification failures in any | |
9147 | event. */ | |
adecb3f4 MM |
9148 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
9149 | targs, parms, args, /*subr=*/0, | |
a34d3336 | 9150 | strict, /*allow_incomplete*/1); |
adecb3f4 MM |
9151 | |
9152 | if (result == 0) | |
9153 | /* All is well so far. Now, check: | |
9154 | ||
9155 | [temp.deduct] | |
9156 | ||
9157 | When all template arguments have been deduced, all uses of | |
9158 | template parameters in nondeduced contexts are replaced with | |
9159 | the corresponding deduced argument values. If the | |
9160 | substitution results in an invalid type, as described above, | |
9161 | type deduction fails. */ | |
c2ea3a40 | 9162 | if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) |
adecb3f4 MM |
9163 | == error_mark_node) |
9164 | return 1; | |
9165 | ||
9166 | return result; | |
830bfa74 MM |
9167 | } |
9168 | ||
9169 | /* Adjust types before performing type deduction, as described in | |
9170 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
9171 | sections are symmetric. PARM is the type of a function parameter |
9172 | or the return type of the conversion function. ARG is the type of | |
9173 | the argument passed to the call, or the type of the value | |
dc957d14 | 9174 | initialized with the result of the conversion function. */ |
386b8a85 | 9175 | |
62e4a758 | 9176 | static int |
3a978d72 NN |
9177 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
9178 | tree* parm, | |
9179 | tree* arg) | |
830bfa74 | 9180 | { |
62e4a758 NS |
9181 | int result = 0; |
9182 | ||
830bfa74 MM |
9183 | switch (strict) |
9184 | { | |
9185 | case DEDUCE_CALL: | |
9186 | break; | |
9187 | ||
9188 | case DEDUCE_CONV: | |
9189 | { | |
4c7d0dff MM |
9190 | /* Swap PARM and ARG throughout the remainder of this |
9191 | function; the handling is precisely symmetric since PARM | |
9192 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
9193 | tree* temp = parm; |
9194 | parm = arg; | |
9195 | arg = temp; | |
9196 | break; | |
9197 | } | |
9198 | ||
9199 | case DEDUCE_EXACT: | |
9200 | /* There is nothing to do in this case. */ | |
62e4a758 | 9201 | return 0; |
830bfa74 MM |
9202 | |
9203 | default: | |
315fb5db | 9204 | gcc_unreachable (); |
830bfa74 MM |
9205 | } |
9206 | ||
9207 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
9208 | { | |
9209 | /* [temp.deduct.call] | |
9210 | ||
9211 | If P is not a reference type: | |
9212 | ||
9213 | --If A is an array type, the pointer type produced by the | |
9214 | array-to-pointer standard conversion (_conv.array_) is | |
9215 | used in place of A for type deduction; otherwise, | |
9216 | ||
9217 | --If A is a function type, the pointer type produced by | |
9218 | the function-to-pointer standard conversion | |
9219 | (_conv.func_) is used in place of A for type deduction; | |
9220 | otherwise, | |
9221 | ||
9222 | --If A is a cv-qualified type, the top level | |
9223 | cv-qualifiers of A's type are ignored for type | |
9224 | deduction. */ | |
9225 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
9226 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 9227 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
9228 | *arg = build_pointer_type (*arg); |
9229 | else | |
9230 | *arg = TYPE_MAIN_VARIANT (*arg); | |
9231 | } | |
9232 | ||
9233 | /* [temp.deduct.call] | |
9234 | ||
9235 | If P is a cv-qualified type, the top level cv-qualifiers | |
9236 | of P's type are ignored for type deduction. If P is a | |
9237 | reference type, the type referred to by P is used for | |
9238 | type deduction. */ | |
9239 | *parm = TYPE_MAIN_VARIANT (*parm); | |
9240 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
9241 | { |
9242 | *parm = TREE_TYPE (*parm); | |
9243 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
9244 | } | |
1c82cc90 NS |
9245 | |
9246 | /* DR 322. For conversion deduction, remove a reference type on parm | |
9247 | too (which has been swapped into ARG). */ | |
9248 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
9249 | *arg = TREE_TYPE (*arg); | |
9250 | ||
62e4a758 | 9251 | return result; |
386b8a85 JM |
9252 | } |
9253 | ||
e5214479 | 9254 | /* Most parms like fn_type_unification. |
9f54c803 MM |
9255 | |
9256 | If SUBR is 1, we're being called recursively (to unify the | |
9257 | arguments of a function or method parameter of a function | |
9258 | template). */ | |
386b8a85 | 9259 | |
4966381a | 9260 | static int |
3a978d72 NN |
9261 | type_unification_real (tree tparms, |
9262 | tree targs, | |
9263 | tree xparms, | |
9264 | tree xargs, | |
9265 | int subr, | |
9266 | unification_kind_t strict, | |
a34d3336 | 9267 | int allow_incomplete) |
8d08fdba MS |
9268 | { |
9269 | tree parm, arg; | |
9270 | int i; | |
9271 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 9272 | int sub_strict; |
bd0d5d4a JM |
9273 | int saw_undeduced = 0; |
9274 | tree parms, args; | |
8d08fdba | 9275 | |
50bc768d NS |
9276 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
9277 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
9278 | gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST); | |
9279 | gcc_assert (ntparms > 0); | |
8d08fdba | 9280 | |
830bfa74 MM |
9281 | switch (strict) |
9282 | { | |
9283 | case DEDUCE_CALL: | |
028d1f20 NS |
9284 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
9285 | | UNIFY_ALLOW_DERIVED); | |
830bfa74 MM |
9286 | break; |
9287 | ||
9288 | case DEDUCE_CONV: | |
9289 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
9290 | break; | |
9291 | ||
9292 | case DEDUCE_EXACT: | |
9293 | sub_strict = UNIFY_ALLOW_NONE; | |
9294 | break; | |
62e4a758 | 9295 | |
830bfa74 | 9296 | default: |
315fb5db | 9297 | gcc_unreachable (); |
830bfa74 MM |
9298 | } |
9299 | ||
bd0d5d4a JM |
9300 | again: |
9301 | parms = xparms; | |
9302 | args = xargs; | |
bd0d5d4a | 9303 | |
a34d3336 NS |
9304 | while (parms && parms != void_list_node |
9305 | && args && args != void_list_node) | |
8d08fdba MS |
9306 | { |
9307 | parm = TREE_VALUE (parms); | |
9308 | parms = TREE_CHAIN (parms); | |
9309 | arg = TREE_VALUE (args); | |
9310 | args = TREE_CHAIN (args); | |
9311 | ||
9312 | if (arg == error_mark_node) | |
9313 | return 1; | |
9314 | if (arg == unknown_type_node) | |
34016c81 JM |
9315 | /* We can't deduce anything from this, but we might get all the |
9316 | template args from other function args. */ | |
9317 | continue; | |
b7484fbe | 9318 | |
03e70705 JM |
9319 | /* Conversions will be performed on a function argument that |
9320 | corresponds with a function parameter that contains only | |
9321 | non-deducible template parameters and explicitly specified | |
9322 | template parameters. */ | |
c353b8e3 | 9323 | if (!uses_template_parms (parm)) |
b7484fbe | 9324 | { |
03e70705 JM |
9325 | tree type; |
9326 | ||
2f939d94 | 9327 | if (!TYPE_P (arg)) |
03e70705 JM |
9328 | type = TREE_TYPE (arg); |
9329 | else | |
c353b8e3 | 9330 | type = arg; |
03e70705 | 9331 | |
dda04398 | 9332 | if (strict == DEDUCE_EXACT) |
03e70705 | 9333 | { |
3bfdc719 | 9334 | if (same_type_p (parm, type)) |
03e70705 JM |
9335 | continue; |
9336 | } | |
03e70705 | 9337 | else |
343c89cd JM |
9338 | /* It might work; we shouldn't check now, because we might |
9339 | get into infinite recursion. Overload resolution will | |
9340 | handle it. */ | |
9341 | continue; | |
03e70705 | 9342 | |
b7484fbe MS |
9343 | return 1; |
9344 | } | |
9345 | ||
2f939d94 | 9346 | if (!TYPE_P (arg)) |
8d08fdba | 9347 | { |
50bc768d | 9348 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 9349 | if (type_unknown_p (arg)) |
28cbf42c | 9350 | { |
34016c81 JM |
9351 | /* [temp.deduct.type] A template-argument can be deduced from |
9352 | a pointer to function or pointer to member function | |
9353 | argument if the set of overloaded functions does not | |
9354 | contain function templates and at most one of a set of | |
9355 | overloaded functions provides a unique match. */ | |
9356 | ||
9357 | if (resolve_overloaded_unification | |
4393e105 | 9358 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
9359 | != 0) |
9360 | return 1; | |
9361 | continue; | |
28cbf42c | 9362 | } |
8d08fdba | 9363 | arg = TREE_TYPE (arg); |
08476342 NS |
9364 | if (arg == error_mark_node) |
9365 | return 1; | |
8d08fdba | 9366 | } |
62e4a758 NS |
9367 | |
9368 | { | |
9369 | int arg_strict = sub_strict; | |
9370 | ||
9371 | if (!subr) | |
9372 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); | |
4393e105 | 9373 | |
62e4a758 NS |
9374 | if (unify (tparms, targs, parm, arg, arg_strict)) |
9375 | return 1; | |
9376 | } | |
8d08fdba | 9377 | } |
a34d3336 | 9378 | |
8d08fdba MS |
9379 | /* Fail if we've reached the end of the parm list, and more args |
9380 | are present, and the parm list isn't variadic. */ | |
9381 | if (args && args != void_list_node && parms == void_list_node) | |
9382 | return 1; | |
f4f206f4 | 9383 | /* Fail if parms are left and they don't have default values. */ |
a34d3336 | 9384 | if (parms && parms != void_list_node |
8d08fdba MS |
9385 | && TREE_PURPOSE (parms) == NULL_TREE) |
9386 | return 1; | |
bd0d5d4a | 9387 | |
8d08fdba MS |
9388 | if (!subr) |
9389 | for (i = 0; i < ntparms; i++) | |
a34d3336 | 9390 | if (!TREE_VEC_ELT (targs, i)) |
8d08fdba | 9391 | { |
bd0d5d4a JM |
9392 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); |
9393 | ||
9394 | /* If this is an undeduced nontype parameter that depends on | |
9395 | a type parameter, try another pass; its type may have been | |
9396 | deduced from a later argument than the one from which | |
9397 | this parameter can be deduced. */ | |
9398 | if (TREE_CODE (tparm) == PARM_DECL | |
9399 | && uses_template_parms (TREE_TYPE (tparm)) | |
9400 | && !saw_undeduced++) | |
9401 | goto again; | |
9402 | ||
386b8a85 | 9403 | if (!allow_incomplete) |
8251199e | 9404 | error ("incomplete type unification"); |
8d08fdba MS |
9405 | return 2; |
9406 | } | |
a34d3336 | 9407 | |
8d08fdba MS |
9408 | return 0; |
9409 | } | |
9410 | ||
34016c81 JM |
9411 | /* Subroutine of type_unification_real. Args are like the variables at the |
9412 | call site. ARG is an overloaded function (or template-id); we try | |
9413 | deducing template args from each of the overloads, and if only one | |
9414 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
9415 | ||
9416 | static int | |
3a978d72 NN |
9417 | resolve_overloaded_unification (tree tparms, |
9418 | tree targs, | |
9419 | tree parm, | |
9420 | tree arg, | |
9421 | unification_kind_t strict, | |
9422 | int sub_strict) | |
34016c81 JM |
9423 | { |
9424 | tree tempargs = copy_node (targs); | |
9425 | int good = 0; | |
f23fb7f5 | 9426 | bool addr_p; |
34016c81 JM |
9427 | |
9428 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
9429 | { |
9430 | arg = TREE_OPERAND (arg, 0); | |
9431 | addr_p = true; | |
9432 | } | |
9433 | else | |
9434 | addr_p = false; | |
9f3d9e46 | 9435 | |
d8f8dca1 MM |
9436 | if (TREE_CODE (arg) == COMPONENT_REF) |
9437 | /* Handle `&x' where `x' is some static or non-static member | |
9438 | function name. */ | |
9439 | arg = TREE_OPERAND (arg, 1); | |
9440 | ||
05e0b2f4 JM |
9441 | if (TREE_CODE (arg) == OFFSET_REF) |
9442 | arg = TREE_OPERAND (arg, 1); | |
9443 | ||
9f3d9e46 | 9444 | /* Strip baselink information. */ |
50ad9642 MM |
9445 | if (BASELINK_P (arg)) |
9446 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 9447 | |
34016c81 JM |
9448 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
9449 | { | |
9450 | /* If we got some explicit template args, we need to plug them into | |
9451 | the affected templates before we try to unify, in case the | |
9452 | explicit args will completely resolve the templates in question. */ | |
9453 | ||
9454 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
9455 | arg = TREE_OPERAND (arg, 0); | |
9456 | ||
9457 | for (; arg; arg = OVL_NEXT (arg)) | |
9458 | { | |
9459 | tree fn = OVL_CURRENT (arg); | |
9460 | tree subargs, elem; | |
9461 | ||
9462 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
9463 | continue; | |
9464 | ||
a34d3336 NS |
9465 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
9466 | expl_subargs, /*check_ret=*/false); | |
34016c81 JM |
9467 | if (subargs) |
9468 | { | |
c2ea3a40 | 9469 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
f23fb7f5 MM |
9470 | good += try_one_overload (tparms, targs, tempargs, parm, |
9471 | elem, strict, sub_strict, addr_p); | |
34016c81 JM |
9472 | } |
9473 | } | |
9474 | } | |
315fb5db | 9475 | else |
34016c81 | 9476 | { |
315fb5db NS |
9477 | gcc_assert (TREE_CODE (arg) == OVERLOAD |
9478 | || TREE_CODE (arg) == FUNCTION_DECL); | |
9479 | ||
34016c81 | 9480 | for (; arg; arg = OVL_NEXT (arg)) |
f23fb7f5 MM |
9481 | good += try_one_overload (tparms, targs, tempargs, parm, |
9482 | TREE_TYPE (OVL_CURRENT (arg)), | |
9483 | strict, sub_strict, addr_p); | |
34016c81 | 9484 | } |
34016c81 JM |
9485 | |
9486 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
9487 | to function or pointer to member function argument if the set of | |
9488 | overloaded functions does not contain function templates and at most | |
9489 | one of a set of overloaded functions provides a unique match. | |
9490 | ||
9491 | So if we found multiple possibilities, we return success but don't | |
9492 | deduce anything. */ | |
9493 | ||
9494 | if (good == 1) | |
9495 | { | |
9496 | int i = TREE_VEC_LENGTH (targs); | |
9497 | for (; i--; ) | |
9498 | if (TREE_VEC_ELT (tempargs, i)) | |
9499 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
9500 | } | |
9501 | if (good) | |
9502 | return 0; | |
9503 | ||
9504 | return 1; | |
9505 | } | |
9506 | ||
9507 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
9508 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
9509 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
9510 | ADDR_P is true if the expression for which deduction is being |
9511 | performed was of the form "& fn" rather than simply "fn". | |
9512 | ||
34016c81 JM |
9513 | Returns 1 on success. */ |
9514 | ||
9515 | static int | |
3a978d72 NN |
9516 | try_one_overload (tree tparms, |
9517 | tree orig_targs, | |
9518 | tree targs, | |
9519 | tree parm, | |
9520 | tree arg, | |
9521 | unification_kind_t strict, | |
f23fb7f5 MM |
9522 | int sub_strict, |
9523 | bool addr_p) | |
34016c81 JM |
9524 | { |
9525 | int nargs; | |
9526 | tree tempargs; | |
9527 | int i; | |
9528 | ||
9529 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
9530 | to function or pointer to member function argument if the set of | |
9531 | overloaded functions does not contain function templates and at most | |
9532 | one of a set of overloaded functions provides a unique match. | |
9533 | ||
9534 | So if this is a template, just return success. */ | |
9535 | ||
9536 | if (uses_template_parms (arg)) | |
9537 | return 1; | |
9538 | ||
f23fb7f5 MM |
9539 | if (TREE_CODE (arg) == METHOD_TYPE) |
9540 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
9541 | else if (addr_p) | |
9542 | arg = build_pointer_type (arg); | |
9543 | ||
62e4a758 | 9544 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); |
34016c81 JM |
9545 | |
9546 | /* We don't copy orig_targs for this because if we have already deduced | |
9547 | some template args from previous args, unify would complain when we | |
9548 | try to deduce a template parameter for the same argument, even though | |
9549 | there isn't really a conflict. */ | |
9550 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 9551 | tempargs = make_tree_vec (nargs); |
34016c81 | 9552 | |
4393e105 | 9553 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
9554 | return 0; |
9555 | ||
9556 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 9557 | explicitly specified args. */ |
34016c81 JM |
9558 | for (i = nargs; i--; ) |
9559 | { | |
9560 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 9561 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 | 9562 | |
a34d3336 NS |
9563 | if (!elt) |
9564 | /*NOP*/; | |
34016c81 | 9565 | else if (uses_template_parms (elt)) |
a34d3336 NS |
9566 | /* Since we're unifying against ourselves, we will fill in |
9567 | template args used in the function parm list with our own | |
9568 | template parms. Discard them. */ | |
9569 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
9570 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
34016c81 JM |
9571 | return 0; |
9572 | } | |
9573 | ||
9574 | for (i = nargs; i--; ) | |
9575 | { | |
9576 | tree elt = TREE_VEC_ELT (tempargs, i); | |
9577 | ||
9578 | if (elt) | |
9579 | TREE_VEC_ELT (targs, i) = elt; | |
9580 | } | |
9581 | ||
9582 | return 1; | |
9583 | } | |
9584 | ||
74601d7c KL |
9585 | /* Verify that nondeduce template argument agrees with the type |
9586 | obtained from argument deduction. Return nonzero if the | |
9587 | verification fails. | |
9588 | ||
9589 | For example: | |
9590 | ||
9591 | struct A { typedef int X; }; | |
9592 | template <class T, class U> struct C {}; | |
9593 | template <class T> struct C<T, typename T::X> {}; | |
9594 | ||
9595 | Then with the instantiation `C<A, int>', we can deduce that | |
9596 | `T' is `A' but unify () does not check whether `typename T::X' | |
9597 | is `int'. This function ensure that they agree. | |
9598 | ||
9599 | TARGS, PARMS are the same as the arguments of unify. | |
9600 | ARGS contains template arguments from all levels. */ | |
9601 | ||
9602 | static int | |
3a978d72 | 9603 | verify_class_unification (tree targs, tree parms, tree args) |
74601d7c | 9604 | { |
e2005c8d KL |
9605 | parms = tsubst (parms, add_outermost_template_args (args, targs), |
9606 | tf_none, NULL_TREE); | |
9607 | if (parms == error_mark_node) | |
74601d7c KL |
9608 | return 1; |
9609 | ||
e2005c8d | 9610 | return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args)); |
74601d7c KL |
9611 | } |
9612 | ||
4393e105 MM |
9613 | /* PARM is a template class (perhaps with unbound template |
9614 | parameters). ARG is a fully instantiated type. If ARG can be | |
9615 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
9616 | TARGS are as for unify. */ | |
fcfb9f96 MM |
9617 | |
9618 | static tree | |
3a978d72 | 9619 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 9620 | { |
4393e105 MM |
9621 | tree copy_of_targs; |
9622 | ||
9623 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
68361a03 MM |
9624 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
9625 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) | |
4393e105 MM |
9626 | return NULL_TREE; |
9627 | ||
9628 | /* We need to make a new template argument vector for the call to | |
9629 | unify. If we used TARGS, we'd clutter it up with the result of | |
9630 | the attempted unification, even if this class didn't work out. | |
9631 | We also don't want to commit ourselves to all the unifications | |
9632 | we've already done, since unification is supposed to be done on | |
9633 | an argument-by-argument basis. In other words, consider the | |
9634 | following pathological case: | |
9635 | ||
9636 | template <int I, int J, int K> | |
9637 | struct S {}; | |
9638 | ||
9639 | template <int I, int J> | |
9640 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
9641 | ||
9642 | template <int I, int J, int K> | |
9643 | void f(S<I, J, K>, S<I, I, I>); | |
9644 | ||
9645 | void g() { | |
9646 | S<0, 0, 0> s0; | |
9647 | S<0, 1, 2> s2; | |
9648 | ||
9649 | f(s0, s2); | |
9650 | } | |
9651 | ||
9652 | Now, by the time we consider the unification involving `s2', we | |
9653 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 9654 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
9655 | because there are two ways to unify base classes of S<0, 1, 2> |
9656 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
9657 | would reject the possibility I=1. */ | |
f31c0a32 | 9658 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
4393e105 MM |
9659 | |
9660 | /* If unification failed, we're done. */ | |
74601d7c KL |
9661 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
9662 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 9663 | return NULL_TREE; |
74601d7c KL |
9664 | |
9665 | return arg; | |
4393e105 MM |
9666 | } |
9667 | ||
a3a0fc7f NS |
9668 | /* Given a template type PARM and a class type ARG, find the unique |
9669 | base type in ARG that is an instance of PARM. We do not examine | |
9670 | ARG itself; only its base-classes. If there is not exactly one | |
9671 | appropriate base class, return NULL_TREE. PARM may be the type of | |
9672 | a partial specialization, as well as a plain template type. Used | |
9673 | by unify. */ | |
4393e105 MM |
9674 | |
9675 | static tree | |
a3a0fc7f | 9676 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 9677 | { |
a3a0fc7f NS |
9678 | tree rval = NULL_TREE; |
9679 | tree binfo; | |
9680 | ||
9681 | gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg))); | |
9682 | ||
9683 | binfo = TYPE_BINFO (complete_type (arg)); | |
9684 | if (!binfo) | |
9685 | /* The type could not be completed. */ | |
9686 | return NULL_TREE; | |
fcfb9f96 | 9687 | |
a3a0fc7f NS |
9688 | /* Walk in inheritance graph order. The search order is not |
9689 | important, and this avoids multiple walks of virtual bases. */ | |
9690 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 9691 | { |
a3a0fc7f | 9692 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 9693 | |
8d83768f NS |
9694 | if (r) |
9695 | { | |
9696 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 9697 | |
8d83768f | 9698 | [temp.deduct.call] |
fcfb9f96 | 9699 | |
8d83768f NS |
9700 | If they yield more than one possible deduced A, the type |
9701 | deduction fails. | |
4393e105 | 9702 | |
8d83768f | 9703 | applies. */ |
a3a0fc7f NS |
9704 | if (rval && !same_type_p (r, rval)) |
9705 | return NULL_TREE; | |
8d83768f | 9706 | |
a3a0fc7f | 9707 | rval = r; |
8d83768f | 9708 | } |
fcfb9f96 MM |
9709 | } |
9710 | ||
a3a0fc7f | 9711 | return rval; |
fcfb9f96 MM |
9712 | } |
9713 | ||
db2767b6 MM |
9714 | /* Returns the level of DECL, which declares a template parameter. */ |
9715 | ||
e9659ab0 | 9716 | static int |
3a978d72 | 9717 | template_decl_level (tree decl) |
db2767b6 MM |
9718 | { |
9719 | switch (TREE_CODE (decl)) | |
9720 | { | |
9721 | case TYPE_DECL: | |
9722 | case TEMPLATE_DECL: | |
9723 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
9724 | ||
9725 | case PARM_DECL: | |
9726 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
9727 | ||
9728 | default: | |
315fb5db | 9729 | gcc_unreachable (); |
db2767b6 | 9730 | } |
315fb5db | 9731 | return 0; |
db2767b6 MM |
9732 | } |
9733 | ||
830bfa74 MM |
9734 | /* Decide whether ARG can be unified with PARM, considering only the |
9735 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 9736 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 9737 | |
e9659ab0 | 9738 | static int |
3a978d72 | 9739 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 9740 | { |
4f2b0fb2 NS |
9741 | int arg_quals = cp_type_quals (arg); |
9742 | int parm_quals = cp_type_quals (parm); | |
9743 | ||
355f774d NS |
9744 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
9745 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 9746 | { |
2e9ceb77 NS |
9747 | /* Although a CVR qualifier is ignored when being applied to a |
9748 | substituted template parameter ([8.3.2]/1 for example), that | |
9749 | does not apply during deduction [14.8.2.4]/1, (even though | |
9750 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
355f774d NS |
9751 | this). Except when we're allowing additional CV qualifiers |
9752 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
2e9ceb77 NS |
9753 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
9754 | || TREE_CODE (arg) == FUNCTION_TYPE | |
9755 | || TREE_CODE (arg) == METHOD_TYPE) | |
9756 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
9757 | return 0; | |
9758 | ||
9759 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
9760 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
9761 | return 0; | |
4f2b0fb2 | 9762 | } |
2e9ceb77 | 9763 | |
62e4a758 | 9764 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 9765 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
9766 | return 0; |
9767 | ||
62e4a758 | 9768 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 9769 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
9770 | return 0; |
9771 | ||
ef637255 | 9772 | return 1; |
830bfa74 MM |
9773 | } |
9774 | ||
9775 | /* Takes parameters as for type_unification. Returns 0 if the | |
dc957d14 | 9776 | type deduction succeeds, 1 otherwise. The parameter STRICT is a |
830bfa74 MM |
9777 | bitwise or of the following flags: |
9778 | ||
9779 | UNIFY_ALLOW_NONE: | |
9780 | Require an exact match between PARM and ARG. | |
9781 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
9782 | Allow the deduced ARG to be more cv-qualified (by qualification |
9783 | conversion) than ARG. | |
830bfa74 MM |
9784 | UNIFY_ALLOW_LESS_CV_QUAL: |
9785 | Allow the deduced ARG to be less cv-qualified than ARG. | |
9786 | UNIFY_ALLOW_DERIVED: | |
9787 | Allow the deduced ARG to be a template base class of ARG, | |
9788 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
9789 | ARG. |
9790 | UNIFY_ALLOW_INTEGER: | |
9791 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
028d1f20 NS |
9792 | case for more information. |
9793 | UNIFY_ALLOW_OUTER_LEVEL: | |
9794 | This is the outermost level of a deduction. Used to determine validity | |
9795 | of qualification conversions. A valid qualification conversion must | |
9796 | have const qualified pointers leading up to the inner type which | |
9797 | requires additional CV quals, except at the outer level, where const | |
9798 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
9799 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
9800 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
9801 | This is the outermost level of a deduction, and PARM can be more CV | |
9802 | qualified at this point. | |
9803 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
9804 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 9805 | qualified at this point. */ |
830bfa74 | 9806 | |
e9659ab0 | 9807 | static int |
3a978d72 | 9808 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
9809 | { |
9810 | int idx; | |
050367a3 | 9811 | tree targ; |
db2767b6 | 9812 | tree tparm; |
028d1f20 | 9813 | int strict_in = strict; |
8d08fdba MS |
9814 | |
9815 | /* I don't think this will do the right thing with respect to types. | |
9816 | But the only case I've seen it in so far has been array bounds, where | |
9817 | signedness is the only information lost, and I think that will be | |
9818 | okay. */ | |
9819 | while (TREE_CODE (parm) == NOP_EXPR) | |
9820 | parm = TREE_OPERAND (parm, 0); | |
9821 | ||
9822 | if (arg == error_mark_node) | |
9823 | return 1; | |
9824 | if (arg == unknown_type_node) | |
34016c81 JM |
9825 | /* We can't deduce anything from this, but we might get all the |
9826 | template args from other function args. */ | |
9827 | return 0; | |
9828 | ||
db2767b6 | 9829 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 9830 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
9831 | template parameters. We might need them if we're trying to |
9832 | figure out which of two things is more specialized. */ | |
9833 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
9834 | return 0; |
9835 | ||
830bfa74 MM |
9836 | /* Immediately reject some pairs that won't unify because of |
9837 | cv-qualification mismatches. */ | |
9838 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 9839 | && TYPE_P (arg) |
d0ab7624 NS |
9840 | /* It is the elements of the array which hold the cv quals of an array |
9841 | type, and the elements might be template type parms. We'll check | |
9842 | when we recurse. */ | |
9843 | && TREE_CODE (arg) != ARRAY_TYPE | |
830bfa74 MM |
9844 | /* We check the cv-qualifiers when unifying with template type |
9845 | parameters below. We want to allow ARG `const T' to unify with | |
9846 | PARM `T' for example, when computing which of two templates | |
9847 | is more specialized, for example. */ | |
9848 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 9849 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
9850 | return 1; |
9851 | ||
028d1f20 | 9852 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 9853 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
9854 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
9855 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
9856 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
9857 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
9858 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
028d1f20 | 9859 | |
8d08fdba MS |
9860 | switch (TREE_CODE (parm)) |
9861 | { | |
2ca340ae | 9862 | case TYPENAME_TYPE: |
fccef71e | 9863 | case SCOPE_REF: |
b8c6534b | 9864 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
9865 | /* In a type which contains a nested-name-specifier, template |
9866 | argument values cannot be deduced for template parameters used | |
9867 | within the nested-name-specifier. */ | |
9868 | return 0; | |
9869 | ||
8d08fdba | 9870 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 9871 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 9872 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
9873 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
9874 | ||
9875 | if (TEMPLATE_TYPE_LEVEL (parm) | |
9876 | != template_decl_level (tparm)) | |
9877 | /* The PARM is not one we're trying to unify. Just check | |
9878 | to see if it matches ARG. */ | |
9879 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 9880 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 9881 | idx = TEMPLATE_TYPE_IDX (parm); |
050367a3 | 9882 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 9883 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 9884 | |
73b0fce8 | 9885 | /* Check for mixed types and values. */ |
db2767b6 MM |
9886 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
9887 | && TREE_CODE (tparm) != TYPE_DECL) | |
9888 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
9889 | && TREE_CODE (tparm) != TEMPLATE_DECL)) | |
73b0fce8 KL |
9890 | return 1; |
9891 | ||
a1281f45 | 9892 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 9893 | { |
b429fdf0 KL |
9894 | /* ARG must be constructed from a template class or a template |
9895 | template parameter. */ | |
9896 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
9897 | && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))) | |
a1281f45 | 9898 | return 1; |
73b0fce8 | 9899 | |
a1281f45 KL |
9900 | { |
9901 | tree parmtmpl = TYPE_TI_TEMPLATE (parm); | |
9902 | tree parmvec = TYPE_TI_ARGS (parm); | |
6df91b00 | 9903 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
a1281f45 | 9904 | tree argtmplvec |
b429fdf0 | 9905 | = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); |
a1281f45 | 9906 | int i; |
73b0fce8 | 9907 | |
a1281f45 KL |
9908 | /* The parameter and argument roles have to be switched here |
9909 | in order to handle default arguments properly. For example, | |
9910 | template<template <class> class TT> void f(TT<int>) | |
9911 | should be able to accept vector<int> which comes from | |
9912 | template <class T, class Allocator = allocator> | |
9913 | class vector. */ | |
9914 | ||
ee3071ef NS |
9915 | if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1) |
9916 | == error_mark_node) | |
a1281f45 | 9917 | return 1; |
73b0fce8 | 9918 | |
a1281f45 KL |
9919 | /* Deduce arguments T, i from TT<T> or TT<i>. |
9920 | We check each element of PARMVEC and ARGVEC individually | |
9921 | rather than the whole TREE_VEC since they can have | |
9922 | different number of elements. */ | |
6b9b6b15 | 9923 | |
a1281f45 KL |
9924 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
9925 | { | |
6df91b00 KL |
9926 | if (unify (tparms, targs, |
9927 | TREE_VEC_ELT (parmvec, i), | |
a1281f45 KL |
9928 | TREE_VEC_ELT (argvec, i), |
9929 | UNIFY_ALLOW_NONE)) | |
9930 | return 1; | |
73b0fce8 | 9931 | } |
a1281f45 | 9932 | } |
b429fdf0 | 9933 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
9934 | |
9935 | /* Fall through to deduce template name. */ | |
9936 | } | |
9937 | ||
9938 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
9939 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
9940 | { | |
9941 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
9942 | ||
9943 | /* Simple cases: Value already set, does match or doesn't. */ | |
9944 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
9945 | return 0; | |
9946 | else if (targ) | |
9947 | return 1; | |
db2767b6 MM |
9948 | } |
9949 | else | |
9950 | { | |
830bfa74 MM |
9951 | /* If PARM is `const T' and ARG is only `int', we don't have |
9952 | a match unless we are allowing additional qualification. | |
9953 | If ARG is `const int' and PARM is just `T' that's OK; | |
9954 | that binds `const int' to `T'. */ | |
028d1f20 | 9955 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 9956 | arg, parm)) |
db2767b6 MM |
9957 | return 1; |
9958 | ||
830bfa74 MM |
9959 | /* Consider the case where ARG is `const volatile int' and |
9960 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
9961 | arg = cp_build_qualified_type_real |
9962 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
9963 | if (arg == error_mark_node) |
9964 | return 1; | |
73b0fce8 | 9965 | |
a1281f45 KL |
9966 | /* Simple cases: Value already set, does match or doesn't. */ |
9967 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
9968 | return 0; | |
9969 | else if (targ) | |
9970 | return 1; | |
61cd552e | 9971 | |
94fc547c MM |
9972 | /* Make sure that ARG is not a variable-sized array. (Note |
9973 | that were talking about variable-sized arrays (like | |
9974 | `int[n]'), rather than arrays of unknown size (like | |
9975 | `int[]').) We'll get very confused by such a type since | |
9976 | the bound of the array will not be computable in an | |
9977 | instantiation. Besides, such types are not allowed in | |
9978 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 9979 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c MM |
9980 | return 1; |
9981 | } | |
61cd552e | 9982 | |
050367a3 | 9983 | TREE_VEC_ELT (targs, idx) = arg; |
73b0fce8 KL |
9984 | return 0; |
9985 | ||
f84b4be9 | 9986 | case TEMPLATE_PARM_INDEX: |
db2767b6 MM |
9987 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
9988 | ||
9989 | if (TEMPLATE_PARM_LEVEL (parm) | |
9990 | != template_decl_level (tparm)) | |
9991 | /* The PARM is not one we're trying to unify. Just check | |
9992 | to see if it matches ARG. */ | |
c8a209ca NS |
9993 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
9994 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 9995 | |
f84b4be9 | 9996 | idx = TEMPLATE_PARM_IDX (parm); |
050367a3 | 9997 | targ = TREE_VEC_ELT (targs, idx); |
db2767b6 | 9998 | |
050367a3 | 9999 | if (targ) |
c8a209ca | 10000 | return !cp_tree_equal (targ, arg); |
8d08fdba | 10001 | |
161c12b0 JM |
10002 | /* [temp.deduct.type] If, in the declaration of a function template |
10003 | with a non-type template-parameter, the non-type | |
10004 | template-parameter is used in an expression in the function | |
10005 | parameter-list and, if the corresponding template-argument is | |
10006 | deduced, the template-argument type shall match the type of the | |
10007 | template-parameter exactly, except that a template-argument | |
d7c4edd0 NS |
10008 | deduced from an array bound may be of any integral type. |
10009 | The non-type parameter might use already deduced type parameters. */ | |
bd0d5d4a | 10010 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
10011 | if (!TREE_TYPE (arg)) |
10012 | /* Template-parameter dependent expression. Just accept it for now. | |
10013 | It will later be processed in convert_template_argument. */ | |
10014 | ; | |
10015 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
10016 | /* OK */; | |
161c12b0 | 10017 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
10018 | && (TREE_CODE (tparm) == INTEGER_TYPE |
10019 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
10020 | /* Convert the ARG to the type of PARM; the deduced non-type |
10021 | template argument must exactly match the types of the | |
10022 | corresponding parameter. */ | |
10023 | arg = fold (build_nop (TREE_TYPE (parm), arg)); | |
bd0d5d4a JM |
10024 | else if (uses_template_parms (tparm)) |
10025 | /* We haven't deduced the type of this parameter yet. Try again | |
10026 | later. */ | |
10027 | return 0; | |
161c12b0 JM |
10028 | else |
10029 | return 1; | |
10030 | ||
2a1e9fdd | 10031 | TREE_VEC_ELT (targs, idx) = arg; |
8d08fdba MS |
10032 | return 0; |
10033 | ||
28e8f3a0 GB |
10034 | case PTRMEM_CST: |
10035 | { | |
10036 | /* A pointer-to-member constant can be unified only with | |
10037 | another constant. */ | |
10038 | if (TREE_CODE (arg) != PTRMEM_CST) | |
10039 | return 1; | |
10040 | ||
10041 | /* Just unify the class member. It would be useless (and possibly | |
10042 | wrong, depending on the strict flags) to unify also | |
10043 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
10044 | arg refer to the same variable, even if through different | |
10045 | classes. For instance: | |
10046 | ||
10047 | struct A { int x; }; | |
10048 | struct B : A { }; | |
10049 | ||
10050 | Unification of &A::x and &B::x must succeed. */ | |
10051 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), | |
10052 | PTRMEM_CST_MEMBER (arg), strict); | |
10053 | } | |
10054 | ||
8d08fdba | 10055 | case POINTER_TYPE: |
830bfa74 | 10056 | { |
830bfa74 MM |
10057 | if (TREE_CODE (arg) != POINTER_TYPE) |
10058 | return 1; | |
10059 | ||
10060 | /* [temp.deduct.call] | |
10061 | ||
10062 | A can be another pointer or pointer to member type that can | |
10063 | be converted to the deduced A via a qualification | |
10064 | conversion (_conv.qual_). | |
10065 | ||
10066 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
10067 | This will allow for additional cv-qualification of the | |
028d1f20 | 10068 | pointed-to types if appropriate. */ |
830bfa74 | 10069 | |
028d1f20 | 10070 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
10071 | /* The derived-to-base conversion only persists through one |
10072 | level of pointers. */ | |
028d1f20 | 10073 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 10074 | |
a7a64a77 | 10075 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 10076 | TREE_TYPE (arg), strict); |
830bfa74 | 10077 | } |
8d08fdba MS |
10078 | |
10079 | case REFERENCE_TYPE: | |
830bfa74 MM |
10080 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
10081 | return 1; | |
10082 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 10083 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
10084 | |
10085 | case ARRAY_TYPE: | |
10086 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
10087 | return 1; | |
3042d5be MM |
10088 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
10089 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
10090 | return 1; | |
8baddbf1 MM |
10091 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
10092 | { | |
10093 | tree parm_max; | |
10094 | tree arg_max; | |
10095 | ||
10096 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
10097 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); | |
10098 | ||
10099 | /* Our representation of array types uses "N - 1" as the | |
10100 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
10101 | not an integer constant. */ | |
10102 | if (TREE_CODE (parm_max) == MINUS_EXPR) | |
10103 | { | |
7866705a SB |
10104 | arg_max = fold_build2 (PLUS_EXPR, |
10105 | integer_type_node, | |
10106 | arg_max, | |
10107 | TREE_OPERAND (parm_max, 1)); | |
8baddbf1 MM |
10108 | parm_max = TREE_OPERAND (parm_max, 0); |
10109 | } | |
10110 | ||
10111 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
10112 | return 1; | |
10113 | } | |
830bfa74 | 10114 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 10115 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
10116 | |
10117 | case REAL_TYPE: | |
37c46b43 | 10118 | case COMPLEX_TYPE: |
c00996a3 | 10119 | case VECTOR_TYPE: |
8d08fdba | 10120 | case INTEGER_TYPE: |
42976354 | 10121 | case BOOLEAN_TYPE: |
3590f0a6 | 10122 | case ENUMERAL_TYPE: |
5ad5a526 | 10123 | case VOID_TYPE: |
f376e137 MS |
10124 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
10125 | return 1; | |
8baddbf1 | 10126 | |
9edc3913 | 10127 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 10128 | function. */ |
8baddbf1 | 10129 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
10130 | return 1; |
10131 | ||
8d08fdba MS |
10132 | /* As far as unification is concerned, this wins. Later checks |
10133 | will invalidate it if necessary. */ | |
10134 | return 0; | |
10135 | ||
10136 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 10137 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 10138 | case INTEGER_CST: |
bd6dd845 MS |
10139 | while (TREE_CODE (arg) == NOP_EXPR) |
10140 | arg = TREE_OPERAND (arg, 0); | |
10141 | ||
8d08fdba MS |
10142 | if (TREE_CODE (arg) != INTEGER_CST) |
10143 | return 1; | |
10144 | return !tree_int_cst_equal (parm, arg); | |
10145 | ||
8d08fdba MS |
10146 | case TREE_VEC: |
10147 | { | |
10148 | int i; | |
10149 | if (TREE_CODE (arg) != TREE_VEC) | |
10150 | return 1; | |
10151 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
10152 | return 1; | |
0dc09a61 | 10153 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 10154 | if (unify (tparms, targs, |
8d08fdba | 10155 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 10156 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
10157 | return 1; |
10158 | return 0; | |
10159 | } | |
10160 | ||
8d08fdba | 10161 | case RECORD_TYPE: |
f181d4ae | 10162 | case UNION_TYPE: |
f181d4ae | 10163 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 10164 | return 1; |
830bfa74 | 10165 | |
a7a64a77 MM |
10166 | if (TYPE_PTRMEMFUNC_P (parm)) |
10167 | { | |
10168 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
10169 | return 1; | |
10170 | ||
10171 | return unify (tparms, targs, | |
10172 | TYPE_PTRMEMFUNC_FN_TYPE (parm), | |
10173 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
10174 | strict); | |
10175 | } | |
10176 | ||
5db698f6 | 10177 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 10178 | { |
6467930b | 10179 | tree t = NULL_TREE; |
4393e105 | 10180 | |
028d1f20 | 10181 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
10182 | { |
10183 | /* First, we try to unify the PARM and ARG directly. */ | |
10184 | t = try_class_unification (tparms, targs, | |
10185 | parm, arg); | |
10186 | ||
10187 | if (!t) | |
10188 | { | |
10189 | /* Fallback to the special case allowed in | |
10190 | [temp.deduct.call]: | |
10191 | ||
10192 | If P is a class, and P has the form | |
10193 | template-id, then A can be a derived class of | |
10194 | the deduced A. Likewise, if P is a pointer to | |
10195 | a class of the form template-id, A can be a | |
10196 | pointer to a derived class pointed to by the | |
10197 | deduced A. */ | |
8d83768f | 10198 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 10199 | |
8d83768f | 10200 | if (!t) |
4393e105 MM |
10201 | return 1; |
10202 | } | |
10203 | } | |
6df47b06 | 10204 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
9fbf56f7 MM |
10205 | && (CLASSTYPE_TI_TEMPLATE (parm) |
10206 | == CLASSTYPE_TI_TEMPLATE (arg))) | |
6df47b06 MM |
10207 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
10208 | Then, we should unify `int' and `U'. */ | |
6467930b | 10209 | t = arg; |
4393e105 | 10210 | else |
dc957d14 | 10211 | /* There's no chance of unification succeeding. */ |
5566b478 | 10212 | return 1; |
6467930b | 10213 | |
830bfa74 | 10214 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 10215 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 10216 | } |
9edc3913 | 10217 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 10218 | return 1; |
a4443a08 | 10219 | return 0; |
8d08fdba MS |
10220 | |
10221 | case METHOD_TYPE: | |
8d08fdba | 10222 | case FUNCTION_TYPE: |
830bfa74 | 10223 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 10224 | return 1; |
830bfa74 MM |
10225 | |
10226 | if (unify (tparms, targs, TREE_TYPE (parm), | |
4393e105 | 10227 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 10228 | return 1; |
386b8a85 | 10229 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
3b3ba9f0 | 10230 | TYPE_ARG_TYPES (arg), 1, |
a34d3336 | 10231 | DEDUCE_EXACT, 0); |
a4443a08 MS |
10232 | |
10233 | case OFFSET_TYPE: | |
9804209d DG |
10234 | /* Unify a pointer to member with a pointer to member function, which |
10235 | deduces the type of the member as a function type. */ | |
10236 | if (TYPE_PTRMEMFUNC_P (arg)) | |
10237 | { | |
10238 | tree method_type; | |
10239 | tree fntype; | |
10240 | cp_cv_quals cv_quals; | |
10241 | ||
10242 | /* Check top-level cv qualifiers */ | |
10243 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
10244 | return 1; | |
10245 | ||
10246 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
10247 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
10248 | return 1; | |
10249 | ||
10250 | /* Determine the type of the function we are unifying against. */ | |
10251 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
10252 | fntype = | |
10253 | build_function_type (TREE_TYPE (method_type), | |
10254 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
10255 | ||
10256 | /* Extract the cv-qualifiers of the member function from the | |
10257 | implicit object parameter and place them on the function | |
10258 | type to be restored later. */ | |
10259 | cv_quals = | |
10260 | cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type)))); | |
10261 | fntype = build_qualified_type (fntype, cv_quals); | |
10262 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); | |
10263 | } | |
10264 | ||
a4443a08 MS |
10265 | if (TREE_CODE (arg) != OFFSET_TYPE) |
10266 | return 1; | |
830bfa74 | 10267 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 10268 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 10269 | return 1; |
830bfa74 | 10270 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 10271 | strict); |
a4443a08 | 10272 | |
f62dbf03 | 10273 | case CONST_DECL: |
a723baf1 MM |
10274 | if (DECL_TEMPLATE_PARM_P (parm)) |
10275 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
8a784e4a | 10276 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
10277 | return 1; |
10278 | return 0; | |
10279 | ||
28e8f3a0 | 10280 | case FIELD_DECL: |
027905b4 KL |
10281 | case TEMPLATE_DECL: |
10282 | /* Matched cases are handled by the ARG == PARM test above. */ | |
10283 | return 1; | |
10284 | ||
8d08fdba | 10285 | default: |
6615c446 | 10286 | gcc_assert (EXPR_P (parm)); |
98ddffc1 NS |
10287 | |
10288 | /* We must be looking at an expression. This can happen with | |
10289 | something like: | |
4393e105 | 10290 | |
98ddffc1 NS |
10291 | template <int I> |
10292 | void foo(S<I>, S<I + 2>); | |
050367a3 | 10293 | |
98ddffc1 | 10294 | This is a "nondeduced context": |
050367a3 | 10295 | |
98ddffc1 | 10296 | [deduct.type] |
4393e105 | 10297 | |
98ddffc1 | 10298 | The nondeduced contexts are: |
050367a3 | 10299 | |
98ddffc1 NS |
10300 | --A type that is a template-id in which one or more of |
10301 | the template-arguments is an expression that references | |
10302 | a template-parameter. | |
050367a3 | 10303 | |
98ddffc1 NS |
10304 | In these cases, we assume deduction succeeded, but don't |
10305 | actually infer any unifications. */ | |
74601d7c | 10306 | |
98ddffc1 NS |
10307 | if (!uses_template_parms (parm) |
10308 | && !template_args_equal (parm, arg)) | |
10309 | return 1; | |
10310 | else | |
10311 | return 0; | |
8d08fdba MS |
10312 | } |
10313 | } | |
8d08fdba | 10314 | \f |
4684cd27 MM |
10315 | /* Note that DECL can be defined in this translation unit, if |
10316 | required. */ | |
10317 | ||
10318 | static void | |
10319 | mark_definable (tree decl) | |
10320 | { | |
10321 | tree clone; | |
10322 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
10323 | FOR_EACH_CLONE (clone, decl) | |
10324 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
10325 | } | |
10326 | ||
03d0f4af | 10327 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 10328 | explicitly instantiated class. */ |
03d0f4af | 10329 | |
faae18ab | 10330 | void |
3a978d72 | 10331 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 10332 | { |
415c974c | 10333 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 10334 | |
1f6f0cb6 MM |
10335 | /* If this entity has already been written out, it's too late to |
10336 | make any modifications. */ | |
10337 | if (TREE_ASM_WRITTEN (result)) | |
10338 | return; | |
10339 | ||
10340 | if (TREE_CODE (result) != FUNCTION_DECL) | |
10341 | /* The TREE_PUBLIC flag for function declarations will have been | |
10342 | set correctly by tsubst. */ | |
10343 | TREE_PUBLIC (result) = 1; | |
10344 | ||
346eeb15 JM |
10345 | /* This might have been set by an earlier implicit instantiation. */ |
10346 | DECL_COMDAT (result) = 0; | |
10347 | ||
4684cd27 MM |
10348 | if (extern_p) |
10349 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
10350 | else | |
faae18ab | 10351 | { |
4684cd27 | 10352 | mark_definable (result); |
1a408d07 JM |
10353 | /* Always make artificials weak. */ |
10354 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
10355 | comdat_linkage (result); | |
a7d87521 JM |
10356 | /* For WIN32 we also want to put explicit instantiations in |
10357 | linkonce sections. */ | |
1a408d07 | 10358 | else if (TREE_PUBLIC (result)) |
b385c841 | 10359 | maybe_make_one_only (result); |
faae18ab | 10360 | } |
4684cd27 MM |
10361 | |
10362 | /* If EXTERN_P, then this function will not be emitted -- unless | |
10363 | followed by an explicit instantiation, at which point its linkage | |
10364 | will be adjusted. If !EXTERN_P, then this function will be | |
10365 | emitted here. In neither circumstance do we want | |
10366 | import_export_decl to adjust the linkage. */ | |
10367 | DECL_INTERFACE_KNOWN (result) = 1; | |
faae18ab MS |
10368 | } |
10369 | ||
e5214479 | 10370 | /* Given two function templates PAT1 and PAT2, return: |
6467930b MS |
10371 | |
10372 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
10373 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
10374 | 0 if neither is more specialized. |
10375 | ||
dda04398 NS |
10376 | LEN indicates the number of parameters we should consider |
10377 | (defaulted parameters should not be considered). | |
10378 | ||
10379 | The 1998 std underspecified function template partial ordering, and | |
10380 | DR214 addresses the issue. We take pairs of arguments, one from | |
c51940a2 | 10381 | each of the templates, and deduce them against each other. One of |
dda04398 NS |
10382 | the templates will be more specialized if all the *other* |
10383 | template's arguments deduce against its arguments and at least one | |
10384 | of its arguments *does* *not* deduce against the other template's | |
10385 | corresponding argument. Deduction is done as for class templates. | |
10386 | The arguments used in deduction have reference and top level cv | |
10387 | qualifiers removed. Iff both arguments were originally reference | |
10388 | types *and* deduction succeeds in both directions, the template | |
10389 | with the more cv-qualified argument wins for that pairing (if | |
10390 | neither is more cv-qualified, they both are equal). Unlike regular | |
10391 | deduction, after all the arguments have been deduced in this way, | |
10392 | we do *not* verify the deduced template argument values can be | |
10393 | substituted into non-deduced contexts, nor do we have to verify | |
10394 | that all template arguments have been deduced. */ | |
6467930b MS |
10395 | |
10396 | int | |
dda04398 NS |
10397 | more_specialized_fn (tree pat1, tree pat2, int len) |
10398 | { | |
10399 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
10400 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
10401 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
10402 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
10403 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
10404 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
10405 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
10406 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
10407 | int better1 = 0; | |
10408 | int better2 = 0; | |
10409 | ||
ee307009 NS |
10410 | /* If only one is a member function, they are unordered. */ |
10411 | if (DECL_FUNCTION_MEMBER_P (decl1) != DECL_FUNCTION_MEMBER_P (decl2)) | |
10412 | return 0; | |
10413 | ||
dda04398 NS |
10414 | /* Don't consider 'this' parameter. */ |
10415 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) | |
10416 | args1 = TREE_CHAIN (args1); | |
dda04398 NS |
10417 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) |
10418 | args2 = TREE_CHAIN (args2); | |
6467930b | 10419 | |
ee307009 NS |
10420 | /* If only one is a conversion operator, they are unordered. */ |
10421 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
10422 | return 0; | |
10423 | ||
dda04398 NS |
10424 | /* Consider the return type for a conversion function */ |
10425 | if (DECL_CONV_FN_P (decl1)) | |
10426 | { | |
dda04398 NS |
10427 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
10428 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
10429 | len++; | |
10430 | } | |
10431 | ||
10432 | processing_template_decl++; | |
10433 | ||
10434 | while (len--) | |
10435 | { | |
10436 | tree arg1 = TREE_VALUE (args1); | |
10437 | tree arg2 = TREE_VALUE (args2); | |
10438 | int deduce1, deduce2; | |
10439 | int quals1 = -1; | |
10440 | int quals2 = -1; | |
6467930b | 10441 | |
dda04398 NS |
10442 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
10443 | { | |
10444 | arg1 = TREE_TYPE (arg1); | |
10445 | quals1 = cp_type_quals (arg1); | |
10446 | } | |
10447 | ||
10448 | if (TREE_CODE (arg2) == REFERENCE_TYPE) | |
10449 | { | |
10450 | arg2 = TREE_TYPE (arg2); | |
10451 | quals2 = cp_type_quals (arg2); | |
10452 | } | |
6467930b | 10453 | |
dda04398 NS |
10454 | if ((quals1 < 0) != (quals2 < 0)) |
10455 | { | |
10456 | /* Only of the args is a reference, see if we should apply | |
10457 | array/function pointer decay to it. This is not part of | |
10458 | DR214, but is, IMHO, consistent with the deduction rules | |
10459 | for the function call itself, and with our earlier | |
10460 | implementation of the underspecified partial ordering | |
10461 | rules. (nathan). */ | |
10462 | if (quals1 >= 0) | |
10463 | { | |
10464 | switch (TREE_CODE (arg1)) | |
10465 | { | |
10466 | case ARRAY_TYPE: | |
10467 | arg1 = TREE_TYPE (arg1); | |
10468 | /* FALLTHROUGH. */ | |
10469 | case FUNCTION_TYPE: | |
10470 | arg1 = build_pointer_type (arg1); | |
10471 | break; | |
10472 | ||
10473 | default: | |
10474 | break; | |
10475 | } | |
10476 | } | |
10477 | else | |
10478 | { | |
10479 | switch (TREE_CODE (arg2)) | |
10480 | { | |
10481 | case ARRAY_TYPE: | |
10482 | arg2 = TREE_TYPE (arg2); | |
10483 | /* FALLTHROUGH. */ | |
10484 | case FUNCTION_TYPE: | |
10485 | arg2 = build_pointer_type (arg2); | |
10486 | break; | |
10487 | ||
10488 | default: | |
10489 | break; | |
10490 | } | |
10491 | } | |
10492 | } | |
10493 | ||
10494 | arg1 = TYPE_MAIN_VARIANT (arg1); | |
10495 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
10496 | ||
10497 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); | |
10498 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
10499 | ||
10500 | if (!deduce1) | |
10501 | better2 = -1; | |
10502 | if (!deduce2) | |
10503 | better1 = -1; | |
10504 | if (better1 < 0 && better2 < 0) | |
10505 | /* We've failed to deduce something in either direction. | |
10506 | These must be unordered. */ | |
10507 | break; | |
10508 | ||
10509 | if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0) | |
10510 | { | |
10511 | /* Deduces in both directions, see if quals can | |
10512 | disambiguate. Pretend the worse one failed to deduce. */ | |
10513 | if ((quals1 & quals2) == quals2) | |
10514 | deduce1 = 0; | |
10515 | if ((quals1 & quals2) == quals1) | |
10516 | deduce2 = 0; | |
10517 | } | |
10518 | if (deduce1 && !deduce2 && !better2) | |
10519 | better2 = 1; | |
10520 | if (deduce2 && !deduce1 && !better1) | |
10521 | better1 = 1; | |
10522 | ||
10523 | args1 = TREE_CHAIN (args1); | |
10524 | args2 = TREE_CHAIN (args2); | |
10525 | } | |
10526 | ||
10527 | processing_template_decl--; | |
10528 | ||
10529 | return (better1 > 0) - (better2 > 0); | |
73aad9b9 | 10530 | } |
6467930b | 10531 | |
73aad9b9 | 10532 | /* Given two class template specialization list nodes PAT1 and PAT2, return: |
6467930b | 10533 | |
73aad9b9 JM |
10534 | 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order]. |
10535 | -1 if PAT2 is more specialized than PAT1. | |
55ece1b3 KL |
10536 | 0 if neither is more specialized. |
10537 | ||
10538 | FULL_ARGS is the full set of template arguments that triggers this | |
10539 | partial ordering. */ | |
73aad9b9 JM |
10540 | |
10541 | int | |
55ece1b3 | 10542 | more_specialized_class (tree pat1, tree pat2, tree full_args) |
73aad9b9 JM |
10543 | { |
10544 | tree targs; | |
10545 | int winner = 0; | |
10546 | ||
baa49730 GB |
10547 | /* Just like what happens for functions, if we are ordering between |
10548 | different class template specializations, we may encounter dependent | |
10549 | types in the arguments, and we need our dependency check functions | |
10550 | to behave correctly. */ | |
10551 | ++processing_template_decl; | |
36a117a5 | 10552 | targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1), |
55ece1b3 | 10553 | add_outermost_template_args (full_args, TREE_PURPOSE (pat2))); |
73aad9b9 JM |
10554 | if (targs) |
10555 | --winner; | |
10556 | ||
36a117a5 | 10557 | targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2), |
55ece1b3 | 10558 | add_outermost_template_args (full_args, TREE_PURPOSE (pat1))); |
73aad9b9 | 10559 | if (targs) |
6467930b | 10560 | ++winner; |
baa49730 | 10561 | --processing_template_decl; |
6467930b MS |
10562 | |
10563 | return winner; | |
10564 | } | |
73aad9b9 JM |
10565 | |
10566 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 10567 | DECL from the function template FN, with the explicit template |
a34d3336 | 10568 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
4393e105 | 10569 | also match. Return NULL_TREE if no satisfactory arguments could be |
a34d3336 | 10570 | found. */ |
e5214479 | 10571 | |
76b9a14d | 10572 | static tree |
a34d3336 | 10573 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
73aad9b9 | 10574 | { |
98c1c668 | 10575 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 10576 | tree targs = make_tree_vec (ntparms); |
4393e105 | 10577 | tree decl_type; |
03017874 | 10578 | tree decl_arg_types; |
98c1c668 | 10579 | |
4393e105 MM |
10580 | /* Substitute the explicit template arguments into the type of DECL. |
10581 | The call to fn_type_unification will handle substitution into the | |
10582 | FN. */ | |
10583 | decl_type = TREE_TYPE (decl); | |
10584 | if (explicit_args && uses_template_parms (decl_type)) | |
10585 | { | |
10586 | tree tmpl; | |
10587 | tree converted_args; | |
10588 | ||
10589 | if (DECL_TEMPLATE_INFO (decl)) | |
10590 | tmpl = DECL_TI_TEMPLATE (decl); | |
10591 | else | |
0e339752 | 10592 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
10593 | return NULL_TREE; |
10594 | ||
10595 | converted_args | |
10596 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), | |
10597 | explicit_args, NULL_TREE, | |
c2ea3a40 | 10598 | tf_none, /*require_all_arguments=*/0)); |
4393e105 MM |
10599 | if (converted_args == error_mark_node) |
10600 | return NULL_TREE; | |
10601 | ||
c2ea3a40 | 10602 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); |
4393e105 MM |
10603 | if (decl_type == error_mark_node) |
10604 | return NULL_TREE; | |
10605 | } | |
10606 | ||
10607 | decl_arg_types = TYPE_ARG_TYPES (decl_type); | |
e5214479 JM |
10608 | /* Never do unification on the 'this' parameter. */ |
10609 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 10610 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 10611 | |
a34d3336 | 10612 | if (fn_type_unification (fn, explicit_args, targs, |
03017874 | 10613 | decl_arg_types, |
8d3631f8 NS |
10614 | (check_rettype || DECL_CONV_FN_P (fn) |
10615 | ? TREE_TYPE (decl_type) : NULL_TREE), | |
a34d3336 | 10616 | DEDUCE_EXACT)) |
76b9a14d JM |
10617 | return NULL_TREE; |
10618 | ||
76b9a14d JM |
10619 | return targs; |
10620 | } | |
10621 | ||
36a117a5 MM |
10622 | /* Return the innermost template arguments that, when applied to a |
10623 | template specialization whose innermost template parameters are | |
9471d3e2 | 10624 | TPARMS, and whose specialization arguments are PARMS, yield the |
36a117a5 MM |
10625 | ARGS. |
10626 | ||
10627 | For example, suppose we have: | |
10628 | ||
10629 | template <class T, class U> struct S {}; | |
10630 | template <class T> struct S<T*, int> {}; | |
10631 | ||
10632 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
10633 | {T}, the PARMS will be {T*, int} and the ARGS will be {double*, | |
10634 | int}. The resulting vector will be {double}, indicating that `T' | |
10635 | is bound to `double'. */ | |
10636 | ||
bd6dd845 | 10637 | static tree |
3a978d72 | 10638 | get_class_bindings (tree tparms, tree parms, tree args) |
73aad9b9 | 10639 | { |
3b3ba9f0 | 10640 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
f31c0a32 | 10641 | tree vec = make_tree_vec (ntparms); |
73aad9b9 | 10642 | |
74601d7c KL |
10643 | if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args), |
10644 | UNIFY_ALLOW_NONE)) | |
fcfb9f96 | 10645 | return NULL_TREE; |
73aad9b9 JM |
10646 | |
10647 | for (i = 0; i < ntparms; ++i) | |
10648 | if (! TREE_VEC_ELT (vec, i)) | |
10649 | return NULL_TREE; | |
10650 | ||
74601d7c KL |
10651 | if (verify_class_unification (vec, parms, args)) |
10652 | return NULL_TREE; | |
10653 | ||
73aad9b9 JM |
10654 | return vec; |
10655 | } | |
10656 | ||
104bf76a MM |
10657 | /* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs. |
10658 | Pick the most specialized template, and return the corresponding | |
10659 | instantiation, or if there is no corresponding instantiation, the | |
e5214479 JM |
10660 | template itself. If there is no most specialized template, |
10661 | error_mark_node is returned. If there are no templates at all, | |
10662 | NULL_TREE is returned. */ | |
73aad9b9 JM |
10663 | |
10664 | tree | |
3a978d72 | 10665 | most_specialized_instantiation (tree instantiations) |
73aad9b9 | 10666 | { |
104bf76a | 10667 | tree fn, champ; |
73aad9b9 | 10668 | |
104bf76a | 10669 | if (!instantiations) |
73aad9b9 | 10670 | return NULL_TREE; |
dda04398 NS |
10671 | |
10672 | ++processing_template_decl; | |
10673 | ||
104bf76a MM |
10674 | champ = instantiations; |
10675 | for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 10676 | { |
dda04398 NS |
10677 | int fate = 0; |
10678 | ||
a34d3336 NS |
10679 | if (get_bindings (TREE_VALUE (champ), |
10680 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
10681 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
10682 | fate--; |
10683 | ||
a34d3336 NS |
10684 | if (get_bindings (TREE_VALUE (fn), |
10685 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
10686 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
10687 | fate++; |
10688 | ||
10689 | if (fate != 1) | |
73aad9b9 | 10690 | { |
dda04398 NS |
10691 | if (!fate) |
10692 | /* Equally specialized, move to next function. If there | |
10693 | is no next function, nothing's most specialized. */ | |
10694 | fn = TREE_CHAIN (fn); | |
104bf76a | 10695 | champ = fn; |
73aad9b9 JM |
10696 | } |
10697 | } | |
dda04398 NS |
10698 | |
10699 | if (champ) | |
10700 | /* Now verify that champ is better than everything earlier in the | |
10701 | instantiation list. */ | |
10702 | for (fn = instantiations; fn != champ; fn = TREE_CHAIN (fn)) | |
a34d3336 NS |
10703 | if (get_bindings (TREE_VALUE (champ), |
10704 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
10705 | NULL_TREE, /*check_ret=*/false) | |
10706 | || !get_bindings (TREE_VALUE (fn), | |
10707 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
10708 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
10709 | { |
10710 | champ = NULL_TREE; | |
10711 | break; | |
10712 | } | |
10713 | ||
10714 | processing_template_decl--; | |
10715 | ||
10716 | if (!champ) | |
10717 | return error_mark_node; | |
73aad9b9 | 10718 | |
104bf76a MM |
10719 | return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ); |
10720 | } | |
10721 | ||
10722 | /* Return the most specialized of the list of templates in FNS that can | |
10723 | produce an instantiation matching DECL, given the explicit template | |
10724 | arguments EXPLICIT_ARGS. */ | |
10725 | ||
e9659ab0 | 10726 | static tree |
3a978d72 | 10727 | most_specialized (tree fns, tree decl, tree explicit_args) |
104bf76a MM |
10728 | { |
10729 | tree candidates = NULL_TREE; | |
10730 | tree fn, args; | |
10731 | ||
10732 | for (fn = fns; fn; fn = TREE_CHAIN (fn)) | |
10733 | { | |
10734 | tree candidate = TREE_VALUE (fn); | |
10735 | ||
a34d3336 | 10736 | args = get_bindings (candidate, decl, explicit_args, /*check_ret=*/true); |
104bf76a | 10737 | if (args) |
e1b3e07d | 10738 | candidates = tree_cons (NULL_TREE, candidate, candidates); |
104bf76a MM |
10739 | } |
10740 | ||
e5214479 | 10741 | return most_specialized_instantiation (candidates); |
73aad9b9 JM |
10742 | } |
10743 | ||
36a117a5 | 10744 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
10745 | general such template. Otherwise, returns NULL_TREE. |
10746 | ||
10747 | For example, given: | |
36a117a5 MM |
10748 | |
10749 | template <class T> struct S { template <class U> void f(U); }; | |
10750 | ||
10751 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
10752 | the full template. This function will not trace past partial | |
10753 | specializations, however. For example, given in addition: | |
10754 | ||
10755 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
10756 | ||
10757 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
10758 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 10759 | |
612c671a | 10760 | tree |
3a978d72 | 10761 | most_general_template (tree decl) |
73aad9b9 | 10762 | { |
f9a7ae04 MM |
10763 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
10764 | an immediate specialization. */ | |
10765 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
10766 | { | |
10767 | if (DECL_TEMPLATE_INFO (decl)) { | |
10768 | decl = DECL_TI_TEMPLATE (decl); | |
10769 | ||
10770 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
10771 | template friend. */ | |
10772 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
10773 | return NULL_TREE; | |
10774 | } else | |
10775 | return NULL_TREE; | |
10776 | } | |
10777 | ||
10778 | /* Look for more and more general templates. */ | |
10779 | while (DECL_TEMPLATE_INFO (decl)) | |
10780 | { | |
10b1d5e7 MM |
10781 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
10782 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
10783 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
10784 | break; | |
10785 | ||
6e049fcd KL |
10786 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
10787 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
10788 | break; | |
10789 | ||
f9a7ae04 MM |
10790 | /* Stop if we run into an explicitly specialized class template. */ |
10791 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
10792 | && DECL_CONTEXT (decl) | |
10793 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
10794 | break; | |
10795 | ||
10796 | decl = DECL_TI_TEMPLATE (decl); | |
10797 | } | |
36a117a5 MM |
10798 | |
10799 | return decl; | |
10800 | } | |
10801 | ||
10802 | /* Return the most specialized of the class template specializations | |
10803 | of TMPL which can produce an instantiation matching ARGS, or | |
10804 | error_mark_node if the choice is ambiguous. */ | |
10805 | ||
e9659ab0 | 10806 | static tree |
3a978d72 | 10807 | most_specialized_class (tree tmpl, tree args) |
36a117a5 MM |
10808 | { |
10809 | tree list = NULL_TREE; | |
10810 | tree t; | |
10811 | tree champ; | |
73aad9b9 JM |
10812 | int fate; |
10813 | ||
36a117a5 MM |
10814 | tmpl = most_general_template (tmpl); |
10815 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) | |
73aad9b9 | 10816 | { |
36a117a5 MM |
10817 | tree spec_args |
10818 | = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args); | |
10819 | if (spec_args) | |
73aad9b9 | 10820 | { |
1f8f4a0b | 10821 | list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list); |
73aad9b9 JM |
10822 | TREE_TYPE (list) = TREE_TYPE (t); |
10823 | } | |
10824 | } | |
10825 | ||
10826 | if (! list) | |
10827 | return NULL_TREE; | |
10828 | ||
10829 | t = list; | |
10830 | champ = t; | |
10831 | t = TREE_CHAIN (t); | |
10832 | for (; t; t = TREE_CHAIN (t)) | |
10833 | { | |
55ece1b3 | 10834 | fate = more_specialized_class (champ, t, args); |
73aad9b9 JM |
10835 | if (fate == 1) |
10836 | ; | |
10837 | else | |
10838 | { | |
10839 | if (fate == 0) | |
10840 | { | |
10841 | t = TREE_CHAIN (t); | |
10842 | if (! t) | |
10843 | return error_mark_node; | |
10844 | } | |
10845 | champ = t; | |
10846 | } | |
10847 | } | |
10848 | ||
10849 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
10850 | { | |
55ece1b3 | 10851 | fate = more_specialized_class (champ, t, args); |
73aad9b9 JM |
10852 | if (fate != 1) |
10853 | return error_mark_node; | |
10854 | } | |
10855 | ||
10856 | return champ; | |
10857 | } | |
10858 | ||
eb8845be | 10859 | /* Explicitly instantiate DECL. */ |
e92cc029 | 10860 | |
8d08fdba | 10861 | void |
eb8845be | 10862 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 10863 | { |
8d08fdba | 10864 | tree result = NULL_TREE; |
faae18ab | 10865 | int extern_p = 0; |
e8abc66f | 10866 | |
3fa56191 | 10867 | if (!decl) |
dc957d14 | 10868 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
10869 | an appropriate message. */ |
10870 | return; | |
10871 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 10872 | { |
0f51ccfc | 10873 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
10874 | return; |
10875 | } | |
03d0f4af | 10876 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 10877 | { |
03d0f4af MM |
10878 | /* There is an asymmetry here in the way VAR_DECLs and |
10879 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
10880 | the latter, the DECL we get back will be marked as a | |
10881 | template instantiation, and the appropriate | |
10882 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
10883 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
10884 | should handle VAR_DECLs as it currently handles | |
10885 | FUNCTION_DECLs. */ | |
86ac0575 | 10886 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 10887 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 10888 | { |
0f51ccfc | 10889 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
10890 | return; |
10891 | } | |
6633d636 MS |
10892 | } |
10893 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
10894 | { | |
0f51ccfc | 10895 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
10896 | return; |
10897 | } | |
03d0f4af MM |
10898 | else |
10899 | result = decl; | |
672476cb | 10900 | |
03d0f4af | 10901 | /* Check for various error cases. Note that if the explicit |
0e339752 | 10902 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
10903 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
10904 | until we get here. */ | |
10905 | ||
10906 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 10907 | { |
07782718 KL |
10908 | /* DR 259 [temp.spec]. |
10909 | ||
10910 | Both an explicit instantiation and a declaration of an explicit | |
10911 | specialization shall not appear in a program unless the explicit | |
10912 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 10913 | |
07782718 KL |
10914 | For a given set of template parameters, if an explicit |
10915 | instantiation of a template appears after a declaration of an | |
10916 | explicit specialization for that template, the explicit | |
10917 | instantiation has no effect. */ | |
672476cb MM |
10918 | return; |
10919 | } | |
03d0f4af MM |
10920 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
10921 | { | |
10922 | /* [temp.spec] | |
98c1c668 | 10923 | |
03d0f4af MM |
10924 | No program shall explicitly instantiate any template more |
10925 | than once. | |
10926 | ||
4684cd27 MM |
10927 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
10928 | the first instantiation was `extern' and the second is not, | |
10929 | and EXTERN_P for the opposite case. */ | |
10930 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
0f51ccfc | 10931 | pedwarn ("duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
10932 | /* If an "extern" explicit instantiation follows an ordinary |
10933 | explicit instantiation, the template is instantiated. */ | |
10934 | if (extern_p) | |
03d0f4af MM |
10935 | return; |
10936 | } | |
10937 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 10938 | { |
0f51ccfc | 10939 | error ("no matching template for %qD found", result); |
faae18ab MS |
10940 | return; |
10941 | } | |
03d0f4af | 10942 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 10943 | { |
0f51ccfc | 10944 | pedwarn ("explicit instantiation of non-template %q#D", result); |
6633d636 MS |
10945 | return; |
10946 | } | |
10947 | ||
f0e01782 | 10948 | if (storage == NULL_TREE) |
00595019 | 10949 | ; |
faae18ab | 10950 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 10951 | { |
c02f5e29 | 10952 | if (pedantic && !in_system_header) |
0f51ccfc GDR |
10953 | pedwarn ("ISO C++ forbids the use of %<extern%> on explicit " |
10954 | "instantiations"); | |
03d0f4af MM |
10955 | extern_p = 1; |
10956 | } | |
f0e01782 | 10957 | else |
0f51ccfc | 10958 | error ("storage class %qD applied to template instantiation", storage); |
5566b478 | 10959 | |
5566b478 | 10960 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 10961 | if (! extern_p) |
415c974c | 10962 | instantiate_decl (result, /*defer_ok=*/1, /*undefined_ok=*/0); |
7177d104 MS |
10963 | } |
10964 | ||
faae18ab | 10965 | void |
3a978d72 | 10966 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
10967 | { |
10968 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
10969 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
10970 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
10971 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
10972 | if (! extern_p) | |
10973 | { | |
10974 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
10975 | rest_of_type_compilation (t, 1); | |
10976 | } | |
10977 | } | |
e8abc66f | 10978 | |
5e0c54e5 | 10979 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 10980 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
10981 | static void |
10982 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
10983 | { | |
10984 | tree storage = *(tree *) data; | |
10985 | ||
10986 | if (IS_AGGR_TYPE (entry->type) | |
10987 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) | |
10988 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
10989 | } | |
10990 | ||
415c974c MA |
10991 | /* Called from do_type_instantiation to instantiate a member |
10992 | (a member function or a static member variable) of an | |
03fd3f84 | 10993 | explicitly instantiated class template. */ |
415c974c MA |
10994 | static void |
10995 | instantiate_class_member (tree decl, int extern_p) | |
10996 | { | |
10997 | mark_decl_instantiated (decl, extern_p); | |
415c974c MA |
10998 | if (! extern_p) |
10999 | instantiate_decl (decl, /*defer_ok=*/1, /* undefined_ok=*/1); | |
11000 | } | |
11001 | ||
a1bcc528 JM |
11002 | /* Perform an explicit instantiation of template class T. STORAGE, if |
11003 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 11004 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 JM |
11005 | since the standard is unclear (as detailed below). */ |
11006 | ||
7177d104 | 11007 | void |
3a978d72 | 11008 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 11009 | { |
e8abc66f MS |
11010 | int extern_p = 0; |
11011 | int nomem_p = 0; | |
5566b478 | 11012 | int static_p = 0; |
4746cf84 | 11013 | int previous_instantiation_extern_p = 0; |
5566b478 | 11014 | |
ca79f85d JM |
11015 | if (TREE_CODE (t) == TYPE_DECL) |
11016 | t = TREE_TYPE (t); | |
11017 | ||
7ddedda4 | 11018 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 11019 | { |
0f51ccfc | 11020 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
11021 | return; |
11022 | } | |
11023 | ||
5566b478 | 11024 | complete_type (t); |
7177d104 | 11025 | |
d0f062fb | 11026 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 11027 | { |
c2ea3a40 | 11028 | if (complain & tf_error) |
0f51ccfc GDR |
11029 | error ("explicit instantiation of %q#T before definition of template", |
11030 | t); | |
f0e01782 MS |
11031 | return; |
11032 | } | |
11033 | ||
03d0f4af | 11034 | if (storage != NULL_TREE) |
f0e01782 | 11035 | { |
c02f5e29 | 11036 | if (pedantic && !in_system_header) |
c4f73174 | 11037 | pedwarn("ISO C++ forbids the use of %qE on explicit instantiations", |
4460cef2 | 11038 | storage); |
03d0f4af MM |
11039 | |
11040 | if (storage == ridpointers[(int) RID_INLINE]) | |
11041 | nomem_p = 1; | |
11042 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
11043 | extern_p = 1; | |
11044 | else if (storage == ridpointers[(int) RID_STATIC]) | |
11045 | static_p = 1; | |
11046 | else | |
11047 | { | |
0f51ccfc GDR |
11048 | error ("storage class %qD applied to template instantiation", |
11049 | storage); | |
03d0f4af MM |
11050 | extern_p = 0; |
11051 | } | |
f0e01782 MS |
11052 | } |
11053 | ||
370af2d5 | 11054 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 11055 | { |
07782718 | 11056 | /* DR 259 [temp.spec]. |
a292b002 | 11057 | |
07782718 KL |
11058 | Both an explicit instantiation and a declaration of an explicit |
11059 | specialization shall not appear in a program unless the explicit | |
11060 | instantiation follows a declaration of the explicit specialization. | |
11061 | ||
11062 | For a given set of template parameters, if an explicit | |
11063 | instantiation of a template appears after a declaration of an | |
11064 | explicit specialization for that template, the explicit | |
11065 | instantiation has no effect. */ | |
03d0f4af MM |
11066 | return; |
11067 | } | |
11068 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 11069 | { |
03d0f4af MM |
11070 | /* [temp.spec] |
11071 | ||
11072 | No program shall explicitly instantiate any template more | |
11073 | than once. | |
11074 | ||
4746cf84 MA |
11075 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
11076 | instantiation was `extern'. If EXTERN_P then the second is. | |
4684cd27 | 11077 | These cases are OK. */ |
4746cf84 MA |
11078 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
11079 | ||
11080 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 11081 | && (complain & tf_error)) |
0f51ccfc | 11082 | pedwarn ("duplicate explicit instantiation of %q#T", t); |
03d0f4af MM |
11083 | |
11084 | /* If we've already instantiated the template, just return now. */ | |
11085 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
11086 | return; | |
44a8d0b3 | 11087 | } |
e8abc66f | 11088 | |
03d0f4af | 11089 | mark_class_instantiated (t, extern_p); |
03d0f4af | 11090 | |
e8abc66f MS |
11091 | if (nomem_p) |
11092 | return; | |
11093 | ||
7177d104 | 11094 | { |
db5ae43f | 11095 | tree tmp; |
5566b478 | 11096 | |
03d0f4af MM |
11097 | /* In contrast to implicit instantiation, where only the |
11098 | declarations, and not the definitions, of members are | |
11099 | instantiated, we have here: | |
11100 | ||
11101 | [temp.explicit] | |
11102 | ||
11103 | The explicit instantiation of a class template specialization | |
11104 | implies the instantiation of all of its members not | |
11105 | previously explicitly specialized in the translation unit | |
11106 | containing the explicit instantiation. | |
11107 | ||
11108 | Of course, we can't instantiate member template classes, since | |
11109 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 11110 | is unclear on whether the instantiation of the members are |
415c974c | 11111 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 11112 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 11113 | |
5566b478 MS |
11114 | if (! static_p) |
11115 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 11116 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 11117 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 11118 | instantiate_class_member (tmp, extern_p); |
5566b478 MS |
11119 | |
11120 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
11121 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
415c974c | 11122 | instantiate_class_member (tmp, extern_p); |
7177d104 | 11123 | |
5e0c54e5 GDR |
11124 | if (CLASSTYPE_NESTED_UTDS (t)) |
11125 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
11126 | bt_instantiate_type_proc, &storage); | |
a292b002 | 11127 | } |
8d08fdba | 11128 | } |
a28e3c7f | 11129 | |
36a117a5 MM |
11130 | /* Given a function DECL, which is a specialization of TMPL, modify |
11131 | DECL to be a re-instantiation of TMPL with the same template | |
11132 | arguments. TMPL should be the template into which tsubst'ing | |
11133 | should occur for DECL, not the most general template. | |
830bfa74 MM |
11134 | |
11135 | One reason for doing this is a scenario like this: | |
11136 | ||
11137 | template <class T> | |
11138 | void f(const T&, int i); | |
11139 | ||
11140 | void g() { f(3, 7); } | |
11141 | ||
11142 | template <class T> | |
11143 | void f(const T& t, const int i) { } | |
11144 | ||
11145 | Note that when the template is first instantiated, with | |
11146 | instantiate_template, the resulting DECL will have no name for the | |
11147 | first parameter, and the wrong type for the second. So, when we go | |
11148 | to instantiate the DECL, we regenerate it. */ | |
11149 | ||
e9659ab0 | 11150 | static void |
3a978d72 | 11151 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 11152 | { |
f9a7ae04 MM |
11153 | /* The arguments used to instantiate DECL, from the most general |
11154 | template. */ | |
830bfa74 | 11155 | tree args; |
830bfa74 | 11156 | tree code_pattern; |
830bfa74 MM |
11157 | |
11158 | args = DECL_TI_ARGS (decl); | |
11159 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
11160 | ||
2b907f5c KL |
11161 | /* Make sure that we can see identifiers, and compute access |
11162 | correctly. */ | |
11163 | push_access_scope (decl); | |
11164 | ||
c7222c02 MM |
11165 | if (TREE_CODE (decl) == FUNCTION_DECL) |
11166 | { | |
11167 | tree decl_parm; | |
11168 | tree pattern_parm; | |
11169 | tree specs; | |
11170 | int args_depth; | |
11171 | int parms_depth; | |
11172 | ||
11173 | args_depth = TMPL_ARGS_DEPTH (args); | |
11174 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
11175 | if (args_depth > parms_depth) | |
11176 | args = get_innermost_template_args (args, parms_depth); | |
11177 | ||
11178 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
11179 | args, tf_error, NULL_TREE); | |
11180 | if (specs) | |
11181 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
11182 | specs); | |
11183 | ||
11184 | /* Merge parameter declarations. */ | |
11185 | decl_parm = skip_artificial_parms_for (decl, | |
11186 | DECL_ARGUMENTS (decl)); | |
11187 | pattern_parm | |
11188 | = skip_artificial_parms_for (code_pattern, | |
11189 | DECL_ARGUMENTS (code_pattern)); | |
11190 | while (decl_parm) | |
11191 | { | |
11192 | tree parm_type; | |
11193 | ||
11194 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
11195 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
11196 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
11197 | NULL_TREE); | |
02bab9db | 11198 | parm_type = type_decays_to (parm_type); |
c7222c02 MM |
11199 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
11200 | TREE_TYPE (decl_parm) = parm_type; | |
11201 | decl_parm = TREE_CHAIN (decl_parm); | |
11202 | pattern_parm = TREE_CHAIN (pattern_parm); | |
11203 | } | |
830bfa74 | 11204 | |
c7222c02 MM |
11205 | /* Merge additional specifiers from the CODE_PATTERN. */ |
11206 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
11207 | && !DECL_DECLARED_INLINE_P (decl)) | |
11208 | DECL_DECLARED_INLINE_P (decl) = 1; | |
11209 | if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl)) | |
11210 | DECL_INLINE (decl) = 1; | |
11211 | } | |
11212 | else if (TREE_CODE (decl) == VAR_DECL) | |
830bfa74 | 11213 | { |
c7222c02 MM |
11214 | if (!DECL_INITIALIZED_IN_CLASS_P (decl) |
11215 | && DECL_INITIAL (code_pattern)) | |
11216 | DECL_INITIAL (decl) = | |
17bbb839 MM |
11217 | tsubst_expr (DECL_INITIAL (code_pattern), args, |
11218 | tf_error, DECL_TI_TEMPLATE (decl)); | |
830bfa74 | 11219 | } |
c7222c02 MM |
11220 | else |
11221 | gcc_unreachable (); | |
36a117a5 | 11222 | |
2b59fc25 | 11223 | pop_access_scope (decl); |
830bfa74 MM |
11224 | } |
11225 | ||
a723baf1 MM |
11226 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
11227 | substituted to get DECL. */ | |
11228 | ||
d58b7c2d | 11229 | tree |
a723baf1 MM |
11230 | template_for_substitution (tree decl) |
11231 | { | |
11232 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
11233 | ||
11234 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
11235 | for the instantiation. This is not always the most general | |
11236 | template. Consider, for example: | |
11237 | ||
11238 | template <class T> | |
11239 | struct S { template <class U> void f(); | |
11240 | template <> void f<int>(); }; | |
11241 | ||
11242 | and an instantiation of S<double>::f<int>. We want TD to be the | |
11243 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
11244 | while (/* An instantiation cannot have a definition, so we need a | |
11245 | more general template. */ | |
11246 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
11247 | /* We must also deal with friend templates. Given: | |
11248 | ||
11249 | template <class T> struct S { | |
11250 | template <class U> friend void f() {}; | |
11251 | }; | |
11252 | ||
11253 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
11254 | so far as the language is concerned, but that's still | |
11255 | where we get the pattern for the instantiation from. On | |
11256 | other hand, if the definition comes outside the class, say: | |
11257 | ||
11258 | template <class T> struct S { | |
11259 | template <class U> friend void f(); | |
11260 | }; | |
11261 | template <class U> friend void f() {} | |
11262 | ||
11263 | we don't need to look any further. That's what the check for | |
11264 | DECL_INITIAL is for. */ | |
11265 | || (TREE_CODE (decl) == FUNCTION_DECL | |
11266 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
11267 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
11268 | { | |
11269 | /* The present template, TD, should not be a definition. If it | |
11270 | were a definition, we should be using it! Note that we | |
11271 | cannot restructure the loop to just keep going until we find | |
11272 | a template with a definition, since that might go too far if | |
11273 | a specialization was declared, but not defined. */ | |
50bc768d NS |
11274 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
11275 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
a723baf1 MM |
11276 | |
11277 | /* Fetch the more general template. */ | |
11278 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
11279 | } | |
11280 | ||
11281 | return tmpl; | |
11282 | } | |
11283 | ||
16d53b64 | 11284 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 11285 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
11286 | instantiation now; we just have to do it sometime. Normally it is |
11287 | an error if this is an explicit instantiation but D is undefined. | |
11288 | If UNDEFINED_OK is nonzero, then instead we treat it as an implicit | |
11289 | instantiation. UNDEFINED_OK is nonzero only if we are being used | |
11290 | to instantiate the members of an explicitly instantiated class | |
03fd3f84 | 11291 | template. */ |
415c974c | 11292 | |
f84b4be9 | 11293 | |
a28e3c7f | 11294 | tree |
415c974c | 11295 | instantiate_decl (tree d, int defer_ok, int undefined_ok) |
a28e3c7f | 11296 | { |
36a117a5 | 11297 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
11298 | tree gen_args; |
11299 | tree args; | |
830bfa74 | 11300 | tree td; |
36a117a5 MM |
11301 | tree code_pattern; |
11302 | tree spec; | |
11303 | tree gen_tmpl; | |
5566b478 | 11304 | int pattern_defined; |
31a714f6 | 11305 | int need_push; |
82a98427 NS |
11306 | location_t saved_loc = input_location; |
11307 | ||
36a117a5 MM |
11308 | /* This function should only be used to instantiate templates for |
11309 | functions and static member variables. */ | |
50bc768d NS |
11310 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
11311 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 11312 | |
cec24319 MM |
11313 | /* Variables are never deferred; if instantiation is required, they |
11314 | are instantiated right away. That allows for better code in the | |
11315 | case that an expression refers to the value of the variable -- | |
11316 | if the variable has a constant value the referring expression can | |
11317 | take advantage of that fact. */ | |
11318 | if (TREE_CODE (d) == VAR_DECL) | |
11319 | defer_ok = 0; | |
11320 | ||
db9b2174 MM |
11321 | /* Don't instantiate cloned functions. Instead, instantiate the |
11322 | functions they cloned. */ | |
11323 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
11324 | d = DECL_CLONED_FUNCTION (d); | |
11325 | ||
fbf1c34b | 11326 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af | 11327 | /* D has already been instantiated. It might seem reasonable to |
dc957d14 | 11328 | check whether or not D is an explicit instantiation, and, if so, |
03d0f4af MM |
11329 | stop here. But when an explicit instantiation is deferred |
11330 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
11331 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
11332 | return d; |
11333 | ||
11334 | /* If we already have a specialization of this declaration, then | |
11335 | there's no reason to instantiate it. Note that | |
11336 | retrieve_specialization gives us both instantiations and | |
11337 | specializations, so we must explicitly check | |
11338 | DECL_TEMPLATE_SPECIALIZATION. */ | |
11339 | gen_tmpl = most_general_template (tmpl); | |
65f8b0fb | 11340 | gen_args = DECL_TI_ARGS (d); |
c7222c02 MM |
11341 | spec = retrieve_specialization (gen_tmpl, gen_args, |
11342 | /*class_specializations_p=*/false); | |
36a117a5 MM |
11343 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) |
11344 | return spec; | |
11345 | ||
11346 | /* This needs to happen before any tsubsting. */ | |
11347 | if (! push_tinst_level (d)) | |
11348 | return d; | |
11349 | ||
297a5329 JM |
11350 | timevar_push (TV_PARSE); |
11351 | ||
c1f927e8 | 11352 | /* We may be in the middle of deferred access check. Disable it now. */ |
7d021397 KL |
11353 | push_deferring_access_checks (dk_no_deferred); |
11354 | ||
4d85e00e | 11355 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
11356 | for the instantiation. */ |
11357 | td = template_for_substitution (d); | |
fee23f54 | 11358 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 11359 | |
76d3baad KL |
11360 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
11361 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
11362 | /* In the case of a friend template whose definition is provided |
11363 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 11364 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
11365 | args = get_innermost_template_args |
11366 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
11367 | else | |
11368 | args = gen_args; | |
65f8b0fb | 11369 | |
5566b478 | 11370 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 11371 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 11372 | else |
36a117a5 | 11373 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
4684cd27 MM |
11374 | /* Unless an explicit instantiation directive has already determined |
11375 | the linkage of D, remember that a definition is available for | |
11376 | this entity. */ | |
11377 | if (pattern_defined | |
11378 | && !DECL_INTERFACE_KNOWN (d) | |
11379 | && !DECL_NOT_REALLY_EXTERN (d)) | |
11380 | mark_definable (d); | |
de22184b | 11381 | |
f31686a3 | 11382 | input_location = DECL_SOURCE_LOCATION (d); |
de22184b | 11383 | |
4684cd27 | 11384 | if (! pattern_defined && DECL_EXPLICIT_INSTANTIATION (d) && undefined_ok) |
5566b478 | 11385 | { |
4684cd27 MM |
11386 | DECL_NOT_REALLY_EXTERN (d) = 0; |
11387 | SET_DECL_IMPLICIT_INSTANTIATION (d); | |
5566b478 MS |
11388 | } |
11389 | ||
4f2b0fb2 NS |
11390 | if (!defer_ok) |
11391 | { | |
11392 | /* Recheck the substitutions to obtain any warning messages | |
11393 | about ignoring cv qualifiers. */ | |
11394 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
11395 | tree type = TREE_TYPE (gen); | |
11396 | ||
0e902d98 | 11397 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
11398 | correctly. D is already the target FUNCTION_DECL with the |
11399 | right context. */ | |
11400 | push_access_scope (d); | |
0e902d98 | 11401 | |
4f2b0fb2 NS |
11402 | if (TREE_CODE (gen) == FUNCTION_DECL) |
11403 | { | |
65f8b0fb MM |
11404 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d); |
11405 | tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args, | |
4f2b0fb2 NS |
11406 | tf_error | tf_warning, d); |
11407 | /* Don't simply tsubst the function type, as that will give | |
11408 | duplicate warnings about poor parameter qualifications. | |
11409 | The function arguments are the same as the decl_arguments | |
c6002625 | 11410 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
11411 | type = TREE_TYPE (type); |
11412 | } | |
65f8b0fb | 11413 | tsubst (type, gen_args, tf_error | tf_warning, d); |
0e902d98 | 11414 | |
2b59fc25 | 11415 | pop_access_scope (d); |
4f2b0fb2 NS |
11416 | } |
11417 | ||
4684cd27 MM |
11418 | /* We should have set up DECL_INITIAL in instantiate_class_template |
11419 | for in-class definitions of static data members. */ | |
50bc768d NS |
11420 | gcc_assert (!(TREE_CODE (d) == VAR_DECL |
11421 | && DECL_INITIALIZED_IN_CLASS_P (d) | |
11422 | && DECL_INITIAL (d) == NULL_TREE)); | |
4684cd27 MM |
11423 | |
11424 | /* Do not instantiate templates that we know will be defined | |
11425 | elsewhere. */ | |
11426 | if (DECL_INTERFACE_KNOWN (d) | |
11427 | && DECL_REALLY_EXTERN (d) | |
11428 | && ! (TREE_CODE (d) == FUNCTION_DECL | |
11429 | && DECL_INLINE (d))) | |
930cd796 | 11430 | goto out; |
16d53b64 MM |
11431 | /* Defer all other templates, unless we have been explicitly |
11432 | forbidden from doing so. We restore the source position here | |
11433 | because it's used by add_pending_template. */ | |
11434 | else if (! pattern_defined || defer_ok) | |
11435 | { | |
82a98427 | 11436 | input_location = saved_loc; |
c27be9b9 | 11437 | |
03d0f4af MM |
11438 | if (at_eof && !pattern_defined |
11439 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
11440 | /* [temp.explicit] | |
11441 | ||
11442 | The definition of a non-exported function template, a | |
11443 | non-exported member function template, or a non-exported | |
11444 | member function or static data member of a class template | |
11445 | shall be present in every translation unit in which it is | |
11446 | explicitly instantiated. */ | |
33bd39a2 | 11447 | pedwarn |
0f51ccfc | 11448 | ("explicit instantiation of %qD but no definition available", d); |
03d0f4af | 11449 | |
5566b478 | 11450 | add_pending_template (d); |
de22184b | 11451 | goto out; |
5566b478 | 11452 | } |
4684cd27 MM |
11453 | /* Tell the repository that D is available in this translation unit |
11454 | -- and see if it is supposed to be instantiated here. */ | |
11455 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
11456 | { | |
11457 | /* In a PCH file, despite the fact that the repository hasn't | |
11458 | requested instantiation in the PCH it is still possible that | |
11459 | an instantiation will be required in a file that includes the | |
11460 | PCH. */ | |
11461 | if (pch_file) | |
11462 | add_pending_template (d); | |
11463 | /* Instantiate inline functions so that the inliner can do its | |
11464 | job, even though we'll not be emitting a copy of this | |
11465 | function. */ | |
c2a124b2 MM |
11466 | if (!(TREE_CODE (d) == FUNCTION_DECL |
11467 | && flag_inline_trees | |
11468 | && DECL_DECLARED_INLINE_P (d))) | |
4684cd27 MM |
11469 | goto out; |
11470 | } | |
5566b478 | 11471 | |
6de9cd9a | 11472 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
11473 | if (need_push) |
11474 | push_to_top_level (); | |
414ea4aa | 11475 | |
66e0c440 KL |
11476 | /* Mark D as instantiated so that recursive calls to |
11477 | instantiate_decl do not try to instantiate it again. */ | |
11478 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
11479 | ||
2b0a63a3 MM |
11480 | /* Regenerate the declaration in case the template has been modified |
11481 | by a subsequent redeclaration. */ | |
11482 | regenerate_decl_from_template (d, td); | |
4684cd27 | 11483 | |
120722ac | 11484 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 11485 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 11486 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 11487 | |
5566b478 MS |
11488 | if (TREE_CODE (d) == VAR_DECL) |
11489 | { | |
1d62c33e MM |
11490 | /* Clear out DECL_RTL; whatever was there before may not be right |
11491 | since we've reset the type of the declaration. */ | |
11492 | SET_DECL_RTL (d, NULL_RTX); | |
5566b478 | 11493 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 11494 | |
4684cd27 MM |
11495 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
11496 | initializer. That function will defer actual emission until | |
11497 | we have a chance to determine linkage. */ | |
11498 | DECL_EXTERNAL (d) = 0; | |
11499 | ||
73a8adb6 | 11500 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 MM |
11501 | push_nested_class (DECL_CONTEXT (d)); |
11502 | cp_finish_decl (d, | |
11503 | (!DECL_INITIALIZED_IN_CLASS_P (d) | |
11504 | ? DECL_INITIAL (d) : NULL_TREE), | |
11505 | NULL_TREE, 0); | |
73a8adb6 | 11506 | pop_nested_class (); |
5566b478 MS |
11507 | } |
11508 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
11509 | { | |
6bbf1598 | 11510 | htab_t saved_local_specializations; |
a723baf1 MM |
11511 | tree subst_decl; |
11512 | tree tmpl_parm; | |
11513 | tree spec_parm; | |
6bbf1598 MM |
11514 | |
11515 | /* Save away the current list, in case we are instantiating one | |
11516 | template from within the body of another. */ | |
11517 | saved_local_specializations = local_specializations; | |
11518 | ||
6dfbb909 | 11519 | /* Set up the list of local specializations. */ |
6dfbb909 | 11520 | local_specializations = htab_create (37, |
69f794a7 | 11521 | hash_local_specialization, |
a723baf1 | 11522 | eq_local_specializations, |
6dfbb909 MM |
11523 | NULL); |
11524 | ||
558475f0 | 11525 | /* Set up context. */ |
058b15c1 | 11526 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 11527 | |
a723baf1 MM |
11528 | /* Create substitution entries for the parameters. */ |
11529 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
11530 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
11531 | spec_parm = DECL_ARGUMENTS (d); | |
11532 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
11533 | { | |
11534 | register_local_specialization (spec_parm, tmpl_parm); | |
11535 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
11536 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
11537 | } | |
11538 | while (tmpl_parm) | |
11539 | { | |
11540 | register_local_specialization (spec_parm, tmpl_parm); | |
11541 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
11542 | spec_parm = TREE_CHAIN (spec_parm); | |
11543 | } | |
50bc768d | 11544 | gcc_assert (!spec_parm); |
a723baf1 | 11545 | |
558475f0 MM |
11546 | /* Substitute into the body of the function. */ |
11547 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
c2ea3a40 | 11548 | tf_error | tf_warning, tmpl); |
558475f0 | 11549 | |
6dfbb909 MM |
11550 | /* We don't need the local specializations any more. */ |
11551 | htab_delete (local_specializations); | |
6bbf1598 | 11552 | local_specializations = saved_local_specializations; |
6dfbb909 | 11553 | |
4d6abc1c | 11554 | /* Finish the function. */ |
b2dd096b | 11555 | d = finish_function (0); |
8cd2462c | 11556 | expand_or_defer_fn (d); |
5566b478 MS |
11557 | } |
11558 | ||
971cbc14 MM |
11559 | /* We're not deferring instantiation any more. */ |
11560 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
11561 | ||
31a714f6 MM |
11562 | if (need_push) |
11563 | pop_from_top_level (); | |
414ea4aa | 11564 | |
de22184b | 11565 | out: |
82a98427 | 11566 | input_location = saved_loc; |
7d021397 | 11567 | pop_deferring_access_checks (); |
5566b478 | 11568 | pop_tinst_level (); |
a28e3c7f | 11569 | |
297a5329 JM |
11570 | timevar_pop (TV_PARSE); |
11571 | ||
a28e3c7f MS |
11572 | return d; |
11573 | } | |
5566b478 | 11574 | |
0aafb128 | 11575 | /* Run through the list of templates that we wish we could |
35046a54 KL |
11576 | instantiate, and instantiate any we can. RETRIES is the |
11577 | number of times we retry pending template instantiation. */ | |
0aafb128 | 11578 | |
35046a54 KL |
11579 | void |
11580 | instantiate_pending_templates (int retries) | |
0aafb128 MM |
11581 | { |
11582 | tree *t; | |
46ccf50a | 11583 | tree last = NULL_TREE; |
0aafb128 | 11584 | int reconsider; |
aad626f7 | 11585 | location_t saved_loc = input_location; |
35046a54 KL |
11586 | |
11587 | /* Instantiating templates may trigger vtable generation. This in turn | |
11588 | may require further template instantiations. We place a limit here | |
11589 | to avoid infinite loop. */ | |
11590 | if (pending_templates && retries >= max_tinst_depth) | |
11591 | { | |
11592 | cp_error_at ("template instantiation depth exceeds maximum of %d" | |
11593 | " (use -ftemplate-depth-NN to increase the maximum)" | |
0f51ccfc | 11594 | " instantiating %q+D, possibly from virtual table" |
35046a54 KL |
11595 | " generation", |
11596 | max_tinst_depth, TREE_VALUE (pending_templates)); | |
11597 | return; | |
11598 | } | |
11599 | ||
0aafb128 MM |
11600 | do |
11601 | { | |
11602 | reconsider = 0; | |
11603 | ||
11604 | t = &pending_templates; | |
11605 | while (*t) | |
11606 | { | |
0aafb128 MM |
11607 | tree instantiation = TREE_VALUE (*t); |
11608 | ||
3ae18eaf | 11609 | reopen_tinst_level (TREE_PURPOSE (*t)); |
0aafb128 | 11610 | |
2f939d94 | 11611 | if (TYPE_P (instantiation)) |
0aafb128 MM |
11612 | { |
11613 | tree fn; | |
11614 | ||
d0f062fb | 11615 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
11616 | { |
11617 | instantiate_class_template (instantiation); | |
11618 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
11619 | for (fn = TYPE_METHODS (instantiation); | |
11620 | fn; | |
11621 | fn = TREE_CHAIN (fn)) | |
11622 | if (! DECL_ARTIFICIAL (fn)) | |
061cae1f NS |
11623 | instantiate_decl (fn, /*defer_ok=*/0, |
11624 | /*undefined_ok=*/0); | |
d0f062fb | 11625 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 11626 | reconsider = 1; |
0aafb128 MM |
11627 | } |
11628 | ||
d0f062fb | 11629 | if (COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
11630 | /* If INSTANTIATION has been instantiated, then we don't |
11631 | need to consider it again in the future. */ | |
11632 | *t = TREE_CHAIN (*t); | |
46ccf50a JM |
11633 | else |
11634 | { | |
11635 | last = *t; | |
11636 | t = &TREE_CHAIN (*t); | |
11637 | } | |
0aafb128 MM |
11638 | } |
11639 | else | |
11640 | { | |
16d53b64 | 11641 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
11642 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
11643 | { | |
16d53b64 | 11644 | instantiation = instantiate_decl (instantiation, |
415c974c MA |
11645 | /*defer_ok=*/0, |
11646 | /*undefined_ok=*/0); | |
0aafb128 | 11647 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 11648 | reconsider = 1; |
0aafb128 MM |
11649 | } |
11650 | ||
16d53b64 | 11651 | if (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
11652 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) |
11653 | /* If INSTANTIATION has been instantiated, then we don't | |
11654 | need to consider it again in the future. */ | |
11655 | *t = TREE_CHAIN (*t); | |
46ccf50a JM |
11656 | else |
11657 | { | |
11658 | last = *t; | |
11659 | t = &TREE_CHAIN (*t); | |
11660 | } | |
0aafb128 | 11661 | } |
84e5ca0f NS |
11662 | tinst_depth = 0; |
11663 | current_tinst_level = NULL_TREE; | |
0aafb128 | 11664 | } |
46ccf50a | 11665 | last_pending_template = last; |
0aafb128 MM |
11666 | } |
11667 | while (reconsider); | |
11668 | ||
aad626f7 | 11669 | input_location = saved_loc; |
0aafb128 MM |
11670 | } |
11671 | ||
fd74ca0b MM |
11672 | /* Substitute ARGVEC into T, which is a list of initializers for |
11673 | either base class or a non-static data member. The TREE_PURPOSEs | |
11674 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
11675 | instantiate_decl. */ | |
4393e105 | 11676 | |
824b9a4c | 11677 | static tree |
3a978d72 | 11678 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 11679 | { |
2282d28d | 11680 | tree inits = NULL_TREE; |
5566b478 MS |
11681 | |
11682 | for (; t; t = TREE_CHAIN (t)) | |
11683 | { | |
fd74ca0b MM |
11684 | tree decl; |
11685 | tree init; | |
fd74ca0b | 11686 | |
c2ea3a40 | 11687 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning, |
fd74ca0b | 11688 | NULL_TREE); |
1f5a253a NS |
11689 | decl = expand_member_init (decl); |
11690 | if (decl && !DECL_P (decl)) | |
11691 | in_base_initializer = 1; | |
11692 | ||
c2ea3a40 | 11693 | init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning, |
fd74ca0b | 11694 | NULL_TREE); |
1f5a253a NS |
11695 | in_base_initializer = 0; |
11696 | ||
11697 | if (decl) | |
2282d28d | 11698 | { |
1f5a253a | 11699 | init = build_tree_list (decl, init); |
2282d28d MM |
11700 | TREE_CHAIN (init) = inits; |
11701 | inits = init; | |
11702 | } | |
5566b478 | 11703 | } |
2282d28d | 11704 | return inits; |
5566b478 MS |
11705 | } |
11706 | ||
61a127b3 MM |
11707 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
11708 | ||
11709 | static void | |
3a978d72 | 11710 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
11711 | { |
11712 | if (TREE_PRIVATE (decl)) | |
11713 | current_access_specifier = access_private_node; | |
11714 | else if (TREE_PROTECTED (decl)) | |
11715 | current_access_specifier = access_protected_node; | |
11716 | else | |
11717 | current_access_specifier = access_public_node; | |
11718 | } | |
11719 | ||
dbfe2124 MM |
11720 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
11721 | is the instantiation (which should have been created with | |
11722 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 11723 | |
dbfe2124 | 11724 | static void |
3a978d72 | 11725 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 11726 | { |
dbfe2124 | 11727 | tree e; |
b87692e5 MS |
11728 | |
11729 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
11730 | { | |
61a127b3 | 11731 | tree value; |
7b6d72fc MM |
11732 | tree decl; |
11733 | ||
11734 | decl = TREE_VALUE (e); | |
61a127b3 MM |
11735 | /* Note that in a template enum, the TREE_VALUE is the |
11736 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
7b6d72fc | 11737 | value = tsubst_expr (DECL_INITIAL (decl), |
c2ea3a40 | 11738 | args, tf_error | tf_warning, |
4393e105 | 11739 | NULL_TREE); |
61a127b3 MM |
11740 | |
11741 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 11742 | set_current_access_from_decl (decl); |
61a127b3 MM |
11743 | |
11744 | /* Actually build the enumerator itself. */ | |
7b6d72fc | 11745 | build_enumerator (DECL_NAME (decl), value, newtag); |
dbfe2124 | 11746 | } |
b3d5a58b | 11747 | |
219670f1 | 11748 | finish_enum (newtag); |
f31686a3 RH |
11749 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
11750 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 11751 | } |
36a117a5 | 11752 | |
1f6e1acc AS |
11753 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
11754 | its type -- but without substituting the innermost set of template | |
11755 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 11756 | the type. */ |
1f6e1acc AS |
11757 | |
11758 | tree | |
3a978d72 | 11759 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 11760 | { |
1f6e1acc AS |
11761 | tree fn_type; |
11762 | tree tmpl; | |
11763 | tree targs; | |
11764 | tree tparms; | |
11765 | int parm_depth; | |
11766 | ||
11767 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
11768 | targs = DECL_TI_ARGS (decl); | |
11769 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
11770 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
11771 | ||
11772 | /* There should be as many levels of arguments as there are levels | |
11773 | of parameters. */ | |
50bc768d | 11774 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
11775 | |
11776 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
11777 | |
11778 | if (parm_depth == 1) | |
11779 | /* No substitution is necessary. */ | |
11780 | ; | |
11781 | else | |
11782 | { | |
fae6e246 | 11783 | int i, save_access_control; |
1f6e1acc AS |
11784 | tree partial_args; |
11785 | ||
11786 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 11787 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
11788 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
11789 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
11790 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
11791 | TMPL_ARGS_LEVEL (targs, i)); | |
11792 | SET_TMPL_ARGS_LEVEL (partial_args, | |
11793 | TMPL_ARGS_DEPTH (targs), | |
11794 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
11795 | ||
fae6e246 RH |
11796 | /* Disable access control as this function is used only during |
11797 | name-mangling. */ | |
11798 | save_access_control = flag_access_control; | |
11799 | flag_access_control = 0; | |
5c74d5b0 | 11800 | |
9579624e | 11801 | ++processing_template_decl; |
1f6e1acc AS |
11802 | /* Now, do the (partial) substitution to figure out the |
11803 | appropriate function type. */ | |
c2ea3a40 | 11804 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 11805 | --processing_template_decl; |
1f6e1acc AS |
11806 | |
11807 | /* Substitute into the template parameters to obtain the real | |
11808 | innermost set of parameters. This step is important if the | |
11809 | innermost set of template parameters contains value | |
11810 | parameters whose types depend on outer template parameters. */ | |
11811 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 11812 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 11813 | |
fae6e246 | 11814 | flag_access_control = save_access_control; |
5c74d5b0 | 11815 | } |
1f6e1acc AS |
11816 | |
11817 | return fn_type; | |
11818 | } | |
669ec2b4 | 11819 | |
cb753e49 | 11820 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 11821 | the last one involved in diagnostics. */ |
cb753e49 | 11822 | int |
3a978d72 | 11823 | problematic_instantiation_changed (void) |
cb753e49 GDR |
11824 | { |
11825 | return last_template_error_tick != tinst_level_tick; | |
11826 | } | |
11827 | ||
11828 | /* Remember current template involved in diagnostics. */ | |
11829 | void | |
3a978d72 | 11830 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
11831 | { |
11832 | last_template_error_tick = tinst_level_tick; | |
11833 | } | |
11834 | ||
11835 | tree | |
3a978d72 | 11836 | current_instantiation (void) |
cb753e49 GDR |
11837 | { |
11838 | return current_tinst_level; | |
11839 | } | |
db3f4e4e NS |
11840 | |
11841 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 11842 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 11843 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
11844 | |
11845 | static int | |
3a978d72 | 11846 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e NS |
11847 | { |
11848 | if (INTEGRAL_TYPE_P (type)) | |
11849 | return 0; | |
11850 | else if (POINTER_TYPE_P (type)) | |
11851 | return 0; | |
a5ac359a | 11852 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 11853 | return 0; |
db3f4e4e NS |
11854 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
11855 | return 0; | |
11856 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
11857 | return 0; | |
11858 | ||
c2ea3a40 | 11859 | if (complain & tf_error) |
0f51ccfc | 11860 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
11861 | return 1; |
11862 | } | |
e2500fed | 11863 | |
5552b43c MM |
11864 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
11865 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 11866 | |
5552b43c MM |
11867 | static bool |
11868 | dependent_type_p_r (tree type) | |
1fb3244a MM |
11869 | { |
11870 | tree scope; | |
11871 | ||
1fb3244a MM |
11872 | /* [temp.dep.type] |
11873 | ||
11874 | A type is dependent if it is: | |
11875 | ||
6615c446 JO |
11876 | -- a template parameter. Template template parameters are types |
11877 | for us (since TYPE_P holds true for them) so we handle | |
11878 | them here. */ | |
6bf92cb6 GB |
11879 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
11880 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) | |
1fb3244a MM |
11881 | return true; |
11882 | /* -- a qualified-id with a nested-name-specifier which contains a | |
11883 | class-name that names a dependent type or whose unqualified-id | |
11884 | names a dependent type. */ | |
11885 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
11886 | return true; | |
11887 | /* -- a cv-qualified type where the cv-unqualified type is | |
11888 | dependent. */ | |
11889 | type = TYPE_MAIN_VARIANT (type); | |
11890 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 11891 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a MM |
11892 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
11893 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE | |
11894 | (type))); | |
11895 | else if (TREE_CODE (type) == POINTER_TYPE | |
11896 | || TREE_CODE (type) == REFERENCE_TYPE) | |
11897 | return dependent_type_p (TREE_TYPE (type)); | |
11898 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
11899 | || TREE_CODE (type) == METHOD_TYPE) | |
11900 | { | |
11901 | tree arg_type; | |
11902 | ||
11903 | if (dependent_type_p (TREE_TYPE (type))) | |
11904 | return true; | |
11905 | for (arg_type = TYPE_ARG_TYPES (type); | |
11906 | arg_type; | |
11907 | arg_type = TREE_CHAIN (arg_type)) | |
11908 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
11909 | return true; | |
11910 | return false; | |
11911 | } | |
11912 | /* -- an array type constructed from any dependent type or whose | |
11913 | size is specified by a constant expression that is | |
11914 | value-dependent. */ | |
11915 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11916 | { | |
11917 | if (TYPE_DOMAIN (type) | |
11918 | && ((value_dependent_expression_p | |
11919 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) | |
11920 | || (type_dependent_expression_p | |
11921 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))))) | |
11922 | return true; | |
11923 | return dependent_type_p (TREE_TYPE (type)); | |
11924 | } | |
86306a6b | 11925 | |
1fb3244a | 11926 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
11927 | parameter ... */ |
11928 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 11929 | return true; |
86306a6b | 11930 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 11931 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 11932 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
7e99327d MM |
11933 | && (any_dependent_template_arguments_p |
11934 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) | |
1fb3244a | 11935 | return true; |
86306a6b | 11936 | |
1fb3244a MM |
11937 | /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof' |
11938 | expression is not type-dependent, then it should already been | |
11939 | have resolved. */ | |
11940 | if (TREE_CODE (type) == TYPEOF_TYPE) | |
11941 | return true; | |
86306a6b | 11942 | |
1fb3244a MM |
11943 | /* The standard does not specifically mention types that are local |
11944 | to template functions or local classes, but they should be | |
11945 | considered dependent too. For example: | |
11946 | ||
11947 | template <int I> void f() { | |
11948 | enum E { a = I }; | |
11949 | S<sizeof (E)> s; | |
11950 | } | |
11951 | ||
11952 | The size of `E' cannot be known until the value of `I' has been | |
11953 | determined. Therefore, `E' must be considered dependent. */ | |
11954 | scope = TYPE_CONTEXT (type); | |
11955 | if (scope && TYPE_P (scope)) | |
11956 | return dependent_type_p (scope); | |
11957 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
11958 | return type_dependent_expression_p (scope); | |
11959 | ||
11960 | /* Other types are non-dependent. */ | |
11961 | return false; | |
11962 | } | |
11963 | ||
5552b43c MM |
11964 | /* Returns TRUE if TYPE is dependent, in the sense of |
11965 | [temp.dep.type]. */ | |
11966 | ||
11967 | bool | |
11968 | dependent_type_p (tree type) | |
11969 | { | |
11970 | /* If there are no template parameters in scope, then there can't be | |
11971 | any dependent types. */ | |
11972 | if (!processing_template_decl) | |
11973 | return false; | |
11974 | ||
11975 | /* If the type is NULL, we have not computed a type for the entity | |
11976 | in question; in that case, the type is dependent. */ | |
11977 | if (!type) | |
11978 | return true; | |
11979 | ||
11980 | /* Erroneous types can be considered non-dependent. */ | |
11981 | if (type == error_mark_node) | |
11982 | return false; | |
11983 | ||
11984 | /* If we have not already computed the appropriate value for TYPE, | |
11985 | do so now. */ | |
11986 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
11987 | { | |
11988 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
11989 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
11990 | } | |
11991 | ||
11992 | return TYPE_DEPENDENT_P (type); | |
11993 | } | |
11994 | ||
8d83f792 MM |
11995 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
11996 | ||
11997 | static bool | |
11998 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
11999 | { | |
12000 | tree scope; | |
12001 | tree name; | |
12002 | ||
50bc768d | 12003 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
8d83f792 MM |
12004 | |
12005 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
12006 | return true; | |
12007 | ||
12008 | scope = TREE_OPERAND (expression, 0); | |
12009 | name = TREE_OPERAND (expression, 1); | |
12010 | ||
12011 | /* [temp.dep.expr] | |
12012 | ||
12013 | An id-expression is type-dependent if it contains a | |
12014 | nested-name-specifier that contains a class-name that names a | |
12015 | dependent type. */ | |
12016 | /* The suggested resolution to Core Issue 2 implies that if the | |
12017 | qualifying type is the current class, then we must peek | |
12018 | inside it. */ | |
12019 | if (DECL_P (name) | |
12020 | && currently_open_class (scope) | |
12021 | && !criterion (name)) | |
12022 | return false; | |
12023 | if (dependent_type_p (scope)) | |
12024 | return true; | |
12025 | ||
12026 | return false; | |
12027 | } | |
12028 | ||
20929c7f GB |
12029 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
12030 | [temp.dep.constexpr] */ | |
1fb3244a | 12031 | |
14d22dd6 | 12032 | bool |
1fb3244a MM |
12033 | value_dependent_expression_p (tree expression) |
12034 | { | |
12035 | if (!processing_template_decl) | |
12036 | return false; | |
12037 | ||
12038 | /* A name declared with a dependent type. */ | |
7416ab02 | 12039 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
1fb3244a | 12040 | return true; |
7416ab02 NS |
12041 | |
12042 | switch (TREE_CODE (expression)) | |
12043 | { | |
12044 | case IDENTIFIER_NODE: | |
12045 | /* A name that has not been looked up -- must be dependent. */ | |
12046 | return true; | |
12047 | ||
12048 | case TEMPLATE_PARM_INDEX: | |
12049 | /* A non-type template parm. */ | |
12050 | return true; | |
12051 | ||
12052 | case CONST_DECL: | |
12053 | /* A non-type template parm. */ | |
12054 | if (DECL_TEMPLATE_PARM_P (expression)) | |
d17811fd | 12055 | return true; |
7416ab02 | 12056 | return false; |
d36d5600 | 12057 | |
7416ab02 NS |
12058 | case VAR_DECL: |
12059 | /* A constant with integral or enumeration type and is initialized | |
12060 | with an expression that is value-dependent. */ | |
12061 | if (DECL_INITIAL (expression) | |
12062 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
12063 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
12064 | return true; | |
12065 | return false; | |
12066 | ||
12067 | case DYNAMIC_CAST_EXPR: | |
12068 | case STATIC_CAST_EXPR: | |
12069 | case CONST_CAST_EXPR: | |
12070 | case REINTERPRET_CAST_EXPR: | |
12071 | case CAST_EXPR: | |
12072 | /* These expressions are value-dependent if the type to which | |
12073 | the cast occurs is dependent or the expression being casted | |
12074 | is value-dependent. */ | |
12075 | { | |
12076 | tree type = TREE_TYPE (expression); | |
12077 | ||
12078 | if (dependent_type_p (type)) | |
12079 | return true; | |
12080 | ||
12081 | /* A functional cast has a list of operands. */ | |
12082 | expression = TREE_OPERAND (expression, 0); | |
12083 | if (!expression) | |
12084 | { | |
12085 | /* If there are no operands, it must be an expression such | |
12086 | as "int()". This should not happen for aggregate types | |
12087 | because it would form non-constant expressions. */ | |
12088 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
12089 | ||
12090 | return false; | |
12091 | } | |
12092 | ||
12093 | if (TREE_CODE (expression) == TREE_LIST) | |
12094 | { | |
12095 | for (; expression; expression = TREE_CHAIN (expression)) | |
d17811fd MM |
12096 | if (value_dependent_expression_p (TREE_VALUE (expression))) |
12097 | return true; | |
7416ab02 NS |
12098 | return false; |
12099 | } | |
12100 | ||
d17811fd | 12101 | return value_dependent_expression_p (expression); |
7416ab02 NS |
12102 | } |
12103 | ||
12104 | case SIZEOF_EXPR: | |
12105 | case ALIGNOF_EXPR: | |
12106 | /* A `sizeof' expression is value-dependent if the operand is | |
12107 | type-dependent. */ | |
d17811fd MM |
12108 | expression = TREE_OPERAND (expression, 0); |
12109 | if (TYPE_P (expression)) | |
12110 | return dependent_type_p (expression); | |
12111 | return type_dependent_expression_p (expression); | |
100d337a | 12112 | |
7416ab02 NS |
12113 | case SCOPE_REF: |
12114 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
100d337a | 12115 | |
7416ab02 NS |
12116 | case COMPONENT_REF: |
12117 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
12118 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
12119 | ||
12120 | case CALL_EXPR: | |
12121 | /* A CALL_EXPR is value-dependent if any argument is | |
12122 | value-dependent. Why do we have to handle CALL_EXPRs in this | |
12123 | function at all? First, some function calls, those for which | |
12124 | value_dependent_expression_p is true, man appear in constant | |
12125 | expressions. Second, there appear to be bugs which result in | |
12126 | other CALL_EXPRs reaching this point. */ | |
12127 | { | |
12128 | tree function = TREE_OPERAND (expression, 0); | |
12129 | tree args = TREE_OPERAND (expression, 1); | |
12130 | ||
12131 | if (value_dependent_expression_p (function)) | |
12132 | return true; | |
12133 | ||
12134 | if (! args) | |
12135 | return false; | |
12136 | ||
12137 | if (TREE_CODE (args) == TREE_LIST) | |
12138 | { | |
12139 | for (; args; args = TREE_CHAIN (args)) | |
100d337a MA |
12140 | if (value_dependent_expression_p (TREE_VALUE (args))) |
12141 | return true; | |
7416ab02 NS |
12142 | return false; |
12143 | } | |
12144 | ||
100d337a | 12145 | return value_dependent_expression_p (args); |
7416ab02 NS |
12146 | } |
12147 | ||
12148 | default: | |
12149 | /* A constant expression is value-dependent if any subexpression is | |
12150 | value-dependent. */ | |
1fb3244a MM |
12151 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
12152 | { | |
7416ab02 | 12153 | case tcc_reference: |
6615c446 | 12154 | case tcc_unary: |
1fb3244a MM |
12155 | return (value_dependent_expression_p |
12156 | (TREE_OPERAND (expression, 0))); | |
7416ab02 | 12157 | |
6615c446 JO |
12158 | case tcc_comparison: |
12159 | case tcc_binary: | |
1fb3244a MM |
12160 | return ((value_dependent_expression_p |
12161 | (TREE_OPERAND (expression, 0))) | |
12162 | || (value_dependent_expression_p | |
12163 | (TREE_OPERAND (expression, 1)))); | |
7416ab02 | 12164 | |
6615c446 | 12165 | case tcc_expression: |
1fb3244a MM |
12166 | { |
12167 | int i; | |
54e4aedb | 12168 | for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i) |
14d22dd6 MM |
12169 | /* In some cases, some of the operands may be missing. |
12170 | (For example, in the case of PREDECREMENT_EXPR, the | |
12171 | amount to increment by may be missing.) That doesn't | |
12172 | make the expression dependent. */ | |
12173 | if (TREE_OPERAND (expression, i) | |
12174 | && (value_dependent_expression_p | |
12175 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
12176 | return true; |
12177 | return false; | |
12178 | } | |
7416ab02 | 12179 | |
6615c446 | 12180 | default: |
7416ab02 | 12181 | break; |
1fb3244a MM |
12182 | } |
12183 | } | |
7416ab02 | 12184 | |
1fb3244a MM |
12185 | /* The expression is not value-dependent. */ |
12186 | return false; | |
12187 | } | |
12188 | ||
12189 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
12190 | [temp.dep.expr]. */ | |
12191 | ||
12192 | bool | |
3a978d72 | 12193 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
12194 | { |
12195 | if (!processing_template_decl) | |
12196 | return false; | |
12197 | ||
7efa3e22 NS |
12198 | if (expression == error_mark_node) |
12199 | return false; | |
10b1d5e7 MM |
12200 | |
12201 | /* An unresolved name is always dependent. */ | |
12202 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
12203 | return true; | |
7efa3e22 | 12204 | |
1fb3244a MM |
12205 | /* Some expression forms are never type-dependent. */ |
12206 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
12207 | || TREE_CODE (expression) == SIZEOF_EXPR | |
12208 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
12209 | || TREE_CODE (expression) == TYPEID_EXPR | |
12210 | || TREE_CODE (expression) == DELETE_EXPR | |
12211 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
12212 | || TREE_CODE (expression) == THROW_EXPR) | |
12213 | return false; | |
12214 | ||
12215 | /* The types of these expressions depends only on the type to which | |
12216 | the cast occurs. */ | |
12217 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
12218 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
12219 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
12220 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
12221 | || TREE_CODE (expression) == CAST_EXPR) | |
12222 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 12223 | |
1fb3244a MM |
12224 | /* The types of these expressions depends only on the type created |
12225 | by the expression. */ | |
d17811fd MM |
12226 | if (TREE_CODE (expression) == NEW_EXPR |
12227 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
12228 | { |
12229 | /* For NEW_EXPR tree nodes created inside a template, either | |
12230 | the object type itself or a TREE_LIST may appear as the | |
12231 | operand 1. */ | |
12232 | tree type = TREE_OPERAND (expression, 1); | |
12233 | if (TREE_CODE (type) == TREE_LIST) | |
12234 | /* This is an array type. We need to check array dimensions | |
12235 | as well. */ | |
12236 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
12237 | || value_dependent_expression_p | |
12238 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
12239 | else | |
12240 | return dependent_type_p (type); | |
12241 | } | |
1fb3244a | 12242 | |
5a57f1b2 JM |
12243 | if (TREE_CODE (expression) == SCOPE_REF |
12244 | && dependent_scope_ref_p (expression, | |
12245 | type_dependent_expression_p)) | |
12246 | return true; | |
12247 | ||
12248 | if (TREE_CODE (expression) == FUNCTION_DECL | |
12249 | && DECL_LANG_SPECIFIC (expression) | |
12250 | && DECL_TEMPLATE_INFO (expression) | |
12251 | && (any_dependent_template_arguments_p | |
12252 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
12253 | return true; | |
12254 | ||
12255 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
12256 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
12257 | return false; | |
12258 | ||
d17811fd MM |
12259 | if (TREE_TYPE (expression) == unknown_type_node) |
12260 | { | |
12261 | if (TREE_CODE (expression) == ADDR_EXPR) | |
12262 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
12263 | if (TREE_CODE (expression) == COMPONENT_REF |
12264 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
12265 | { |
12266 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
12267 | return true; | |
12268 | expression = TREE_OPERAND (expression, 1); | |
12269 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
12270 | return false; | |
12271 | } | |
3601f003 KL |
12272 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
12273 | if (TREE_CODE (expression) == SCOPE_REF) | |
12274 | return false; | |
5a57f1b2 | 12275 | |
d17811fd MM |
12276 | if (TREE_CODE (expression) == BASELINK) |
12277 | expression = BASELINK_FUNCTIONS (expression); | |
315fb5db | 12278 | |
d17811fd MM |
12279 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
12280 | { | |
ee3071ef NS |
12281 | if (any_dependent_template_arguments_p |
12282 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
12283 | return true; |
12284 | expression = TREE_OPERAND (expression, 0); | |
12285 | } | |
315fb5db NS |
12286 | gcc_assert (TREE_CODE (expression) == OVERLOAD); |
12287 | ||
12288 | while (expression) | |
d17811fd | 12289 | { |
315fb5db NS |
12290 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
12291 | return true; | |
12292 | expression = OVL_NEXT (expression); | |
d17811fd | 12293 | } |
315fb5db | 12294 | return false; |
d17811fd MM |
12295 | } |
12296 | ||
1fb3244a MM |
12297 | return (dependent_type_p (TREE_TYPE (expression))); |
12298 | } | |
12299 | ||
d17811fd MM |
12300 | /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call) |
12301 | contains a type-dependent expression. */ | |
1fb3244a MM |
12302 | |
12303 | bool | |
d17811fd MM |
12304 | any_type_dependent_arguments_p (tree args) |
12305 | { | |
12306 | while (args) | |
12307 | { | |
7efa3e22 NS |
12308 | tree arg = TREE_VALUE (args); |
12309 | ||
12310 | if (type_dependent_expression_p (arg)) | |
d17811fd MM |
12311 | return true; |
12312 | args = TREE_CHAIN (args); | |
12313 | } | |
12314 | return false; | |
12315 | } | |
12316 | ||
12317 | /* Returns TRUE if the ARG (a template argument) is dependent. */ | |
12318 | ||
12319 | static bool | |
1fb3244a MM |
12320 | dependent_template_arg_p (tree arg) |
12321 | { | |
12322 | if (!processing_template_decl) | |
12323 | return false; | |
12324 | ||
12325 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
12326 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
12327 | return dependent_template_p (arg); | |
12328 | else if (TYPE_P (arg)) | |
12329 | return dependent_type_p (arg); | |
12330 | else | |
12331 | return (type_dependent_expression_p (arg) | |
12332 | || value_dependent_expression_p (arg)); | |
12333 | } | |
12334 | ||
d17811fd MM |
12335 | /* Returns true if ARGS (a collection of template arguments) contains |
12336 | any dependent arguments. */ | |
1fb3244a | 12337 | |
d17811fd MM |
12338 | bool |
12339 | any_dependent_template_arguments_p (tree args) | |
1fb3244a | 12340 | { |
bf12d54d | 12341 | int i; |
c353b8e3 MM |
12342 | int j; |
12343 | ||
d17811fd MM |
12344 | if (!args) |
12345 | return false; | |
12346 | ||
c353b8e3 MM |
12347 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
12348 | { | |
12349 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
12350 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
12351 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
12352 | return true; | |
12353 | } | |
1fb3244a | 12354 | |
1fb3244a MM |
12355 | return false; |
12356 | } | |
12357 | ||
12358 | /* Returns TRUE if the template TMPL is dependent. */ | |
12359 | ||
12360 | bool | |
12361 | dependent_template_p (tree tmpl) | |
12362 | { | |
b95cc51a MM |
12363 | if (TREE_CODE (tmpl) == OVERLOAD) |
12364 | { | |
12365 | while (tmpl) | |
12366 | { | |
12367 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
12368 | return true; | |
12369 | tmpl = OVL_CHAIN (tmpl); | |
12370 | } | |
12371 | return false; | |
12372 | } | |
12373 | ||
1fb3244a MM |
12374 | /* Template template parameters are dependent. */ |
12375 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
12376 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
12377 | return true; | |
27ab0504 | 12378 | /* So are names that have not been looked up. */ |
acccf788 MM |
12379 | if (TREE_CODE (tmpl) == SCOPE_REF |
12380 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 12381 | return true; |
1fb3244a MM |
12382 | /* So are member templates of dependent classes. */ |
12383 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
12384 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
12385 | return false; | |
12386 | } | |
12387 | ||
d17811fd MM |
12388 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
12389 | ||
12390 | bool | |
12391 | dependent_template_id_p (tree tmpl, tree args) | |
12392 | { | |
12393 | return (dependent_template_p (tmpl) | |
12394 | || any_dependent_template_arguments_p (args)); | |
12395 | } | |
12396 | ||
14d22dd6 MM |
12397 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
12398 | TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE | |
12399 | can be found. Note that this function peers inside uninstantiated | |
12400 | templates and therefore should be used only in extremely limited | |
dda04398 | 12401 | situations. ONLY_CURRENT_P restricts this peering to the currently |
c51940a2 | 12402 | open classes hierarchy (which is required when comparing types). */ |
14d22dd6 MM |
12403 | |
12404 | tree | |
12405 | resolve_typename_type (tree type, bool only_current_p) | |
12406 | { | |
12407 | tree scope; | |
12408 | tree name; | |
12409 | tree decl; | |
12410 | int quals; | |
4514aa8c | 12411 | tree pushed_scope; |
14d22dd6 | 12412 | |
50bc768d | 12413 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
12414 | |
12415 | scope = TYPE_CONTEXT (type); | |
12416 | name = TYPE_IDENTIFIER (type); | |
12417 | ||
12418 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
12419 | it first before we can figure out what NAME refers to. */ | |
12420 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
12421 | scope = resolve_typename_type (scope, only_current_p); | |
12422 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
12423 | TYPENAME_TYPE. */ | |
12424 | if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE) | |
12425 | return error_mark_node; | |
12426 | /* If the SCOPE is a template type parameter, we have no way of | |
12427 | resolving the name. */ | |
12428 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
12429 | return type; | |
12430 | /* If the SCOPE is not the current instantiation, there's no reason | |
12431 | to look inside it. */ | |
12432 | if (only_current_p && !currently_open_class (scope)) | |
12433 | return error_mark_node; | |
ca099ac8 MM |
12434 | /* If SCOPE is a partial instantiation, it will not have a valid |
12435 | TYPE_FIELDS list, so use the original template. */ | |
353b4fc0 | 12436 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); |
14d22dd6 MM |
12437 | /* Enter the SCOPE so that name lookup will be resolved as if we |
12438 | were in the class definition. In particular, SCOPE will no | |
12439 | longer be considered a dependent type. */ | |
4514aa8c | 12440 | pushed_scope = push_scope (scope); |
14d22dd6 | 12441 | /* Look up the declaration. */ |
86ac0575 | 12442 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
14d22dd6 MM |
12443 | /* Obtain the set of qualifiers applied to the TYPE. */ |
12444 | quals = cp_type_quals (type); | |
12445 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to | |
12446 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
12447 | if (!decl) | |
12448 | type = error_mark_node; | |
12449 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE | |
12450 | && TREE_CODE (decl) == TYPE_DECL) | |
12451 | type = TREE_TYPE (decl); | |
12452 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR | |
12453 | && DECL_CLASS_TEMPLATE_P (decl)) | |
12454 | { | |
12455 | tree tmpl; | |
12456 | tree args; | |
12457 | /* Obtain the template and the arguments. */ | |
12458 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
12459 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
12460 | /* Instantiate the template. */ | |
12461 | type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, | |
42eaed49 | 12462 | /*entering_scope=*/0, tf_error | tf_user); |
14d22dd6 MM |
12463 | } |
12464 | else | |
12465 | type = error_mark_node; | |
12466 | /* Qualify the resulting type. */ | |
12467 | if (type != error_mark_node && quals) | |
12468 | type = cp_build_qualified_type (type, quals); | |
12469 | /* Leave the SCOPE. */ | |
4514aa8c NS |
12470 | if (pushed_scope) |
12471 | pop_scope (pushed_scope); | |
14d22dd6 MM |
12472 | |
12473 | return type; | |
12474 | } | |
12475 | ||
d17811fd MM |
12476 | /* EXPR is an expression which is not type-dependent. Return a proxy |
12477 | for EXPR that can be used to compute the types of larger | |
12478 | expressions containing EXPR. */ | |
12479 | ||
12480 | tree | |
12481 | build_non_dependent_expr (tree expr) | |
12482 | { | |
0deb916c MM |
12483 | tree inner_expr; |
12484 | ||
d17811fd MM |
12485 | /* Preserve null pointer constants so that the type of things like |
12486 | "p == 0" where "p" is a pointer can be determined. */ | |
12487 | if (null_ptr_cst_p (expr)) | |
12488 | return expr; | |
12489 | /* Preserve OVERLOADs; the functions must be available to resolve | |
12490 | types. */ | |
0deb916c MM |
12491 | inner_expr = (TREE_CODE (expr) == ADDR_EXPR ? |
12492 | TREE_OPERAND (expr, 0) : expr); | |
12493 | if (TREE_CODE (inner_expr) == OVERLOAD | |
12494 | || TREE_CODE (inner_expr) == FUNCTION_DECL | |
12495 | || TREE_CODE (inner_expr) == TEMPLATE_DECL | |
6439fffd KL |
12496 | || TREE_CODE (inner_expr) == TEMPLATE_ID_EXPR |
12497 | || TREE_CODE (inner_expr) == OFFSET_REF) | |
d17811fd | 12498 | return expr; |
5ae9ba3e MM |
12499 | /* There is no need to return a proxy for a variable. */ |
12500 | if (TREE_CODE (expr) == VAR_DECL) | |
12501 | return expr; | |
7433e6d4 MM |
12502 | /* Preserve string constants; conversions from string constants to |
12503 | "char *" are allowed, even though normally a "const char *" | |
12504 | cannot be used to initialize a "char *". */ | |
12505 | if (TREE_CODE (expr) == STRING_CST) | |
12506 | return expr; | |
b7c707d1 MM |
12507 | /* Preserve arithmetic constants, as an optimization -- there is no |
12508 | reason to create a new node. */ | |
12509 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
12510 | return expr; | |
9b7be7b5 MM |
12511 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
12512 | There is at least one place where we want to know that a | |
12513 | particular expression is a throw-expression: when checking a ?: | |
12514 | expression, there are special rules if the second or third | |
878cbb73 | 12515 | argument is a throw-expression. */ |
9b7be7b5 MM |
12516 | if (TREE_CODE (expr) == THROW_EXPR) |
12517 | return expr; | |
47d4c811 NS |
12518 | |
12519 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
12520 | return build3 (COND_EXPR, |
12521 | TREE_TYPE (expr), | |
12522 | TREE_OPERAND (expr, 0), | |
12523 | (TREE_OPERAND (expr, 1) | |
12524 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) | |
12525 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
12526 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
12527 | if (TREE_CODE (expr) == COMPOUND_EXPR |
12528 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
12529 | return build2 (COMPOUND_EXPR, |
12530 | TREE_TYPE (expr), | |
12531 | TREE_OPERAND (expr, 0), | |
12532 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
47d4c811 | 12533 | |
d17811fd MM |
12534 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
12535 | ||
12536 | REFERENCE_TYPEs are not stripped for expressions in templates | |
12537 | because doing so would play havoc with mangling. Consider, for | |
12538 | example: | |
12539 | ||
12540 | template <typename T> void f<T& g>() { g(); } | |
12541 | ||
12542 | In the body of "f", the expression for "g" will have | |
12543 | REFERENCE_TYPE, even though the standard says that it should | |
12544 | not. The reason is that we must preserve the syntactic form of | |
12545 | the expression so that mangling (say) "f<g>" inside the body of | |
12546 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
12547 | stripped here. */ | |
018a5803 | 12548 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
12549 | } |
12550 | ||
12551 | /* ARGS is a TREE_LIST of expressions as arguments to a function call. | |
12552 | Return a new TREE_LIST with the various arguments replaced with | |
12553 | equivalent non-dependent expressions. */ | |
12554 | ||
12555 | tree | |
12556 | build_non_dependent_args (tree args) | |
12557 | { | |
12558 | tree a; | |
12559 | tree new_args; | |
12560 | ||
12561 | new_args = NULL_TREE; | |
12562 | for (a = args; a; a = TREE_CHAIN (a)) | |
12563 | new_args = tree_cons (NULL_TREE, | |
12564 | build_non_dependent_expr (TREE_VALUE (a)), | |
12565 | new_args); | |
12566 | return nreverse (new_args); | |
12567 | } | |
12568 | ||
e2500fed | 12569 | #include "gt-cp-pt.h" |