]>
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 |
1788952f KC |
21 | the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
22 | Boston, MA 02110-1301, 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 | 36 | #include "flags.h" |
e58a9aa1 | 37 | #include "c-common.h" |
8d08fdba | 38 | #include "cp-tree.h" |
e58a9aa1 | 39 | #include "cp-objcp-common.h" |
25af8512 | 40 | #include "tree-inline.h" |
8d08fdba | 41 | #include "decl.h" |
e8abc66f | 42 | #include "output.h" |
49c249e1 | 43 | #include "except.h" |
54f92bfb | 44 | #include "toplev.h" |
3dcaad8b | 45 | #include "rtl.h" |
297a5329 | 46 | #include "timevar.h" |
325c3691 | 47 | #include "tree-iterator.h" |
53824026 | 48 | #include "vecprim.h" |
49c249e1 | 49 | |
050367a3 MM |
50 | /* The type of functions taking a tree, and some additional data, and |
51 | returning an int. */ | |
3a978d72 | 52 | typedef int (*tree_fn_t) (tree, void*); |
050367a3 | 53 | |
0aafb128 MM |
54 | /* The PENDING_TEMPLATES is a TREE_LIST of templates whose |
55 | instantiations have been deferred, either because their definitions | |
d78e771d ZW |
56 | were not yet available, or because we were putting off doing the work. |
57 | The TREE_PURPOSE of each entry is either a DECL (for a function or | |
58 | static data member), or a TYPE (for a class) indicating what we are | |
59 | hoping to instantiate. The TREE_VALUE is not used. */ | |
e2500fed | 60 | static GTY(()) tree pending_templates; |
13e73b29 | 61 | static GTY(()) tree last_pending_template; |
73aad9b9 | 62 | |
67ffc812 | 63 | int processing_template_parmlist; |
386b8a85 JM |
64 | static int template_header_count; |
65 | ||
e2500fed | 66 | static GTY(()) tree saved_trees; |
53824026 | 67 | static VEC(int,heap) *inline_parm_levels; |
75650646 | 68 | |
e2500fed | 69 | static GTY(()) tree current_tinst_level; |
3ae18eaf | 70 | |
2b59fc25 KL |
71 | static GTY(()) tree saved_access_scope; |
72 | ||
0fe0caa6 RH |
73 | /* Live only within one (recursive) call to tsubst_expr. We use |
74 | this to pass the statement expression node from the STMT_EXPR | |
75 | to the EXPR_STMT that is its result. */ | |
76 | static tree cur_stmt_expr; | |
77 | ||
6dfbb909 MM |
78 | /* A map from local variable declarations in the body of the template |
79 | presently being instantiated to the corresponding instantiated | |
80 | local variables. */ | |
81 | static htab_t local_specializations; | |
82 | ||
830bfa74 MM |
83 | #define UNIFY_ALLOW_NONE 0 |
84 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 | |
85 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 | |
86 | #define UNIFY_ALLOW_DERIVED 4 | |
161c12b0 | 87 | #define UNIFY_ALLOW_INTEGER 8 |
028d1f20 | 88 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
62e4a758 NS |
89 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
90 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 | |
830bfa74 | 91 | |
3a978d72 NN |
92 | static void push_access_scope (tree); |
93 | static void pop_access_scope (tree); | |
94 | static int resolve_overloaded_unification (tree, tree, tree, tree, | |
95 | unification_kind_t, int); | |
96 | static int try_one_overload (tree, tree, tree, tree, tree, | |
f23fb7f5 | 97 | unification_kind_t, int, bool); |
3a978d72 NN |
98 | static int unify (tree, tree, tree, tree, int); |
99 | static void add_pending_template (tree); | |
aa9d8196 VR |
100 | static int push_tinst_level (tree); |
101 | static void pop_tinst_level (void); | |
3a978d72 NN |
102 | static void reopen_tinst_level (tree); |
103 | static tree classtype_mangled_name (tree); | |
104 | static char* mangle_class_name_for_template (const char *, tree, tree); | |
105 | static tree tsubst_initializer_list (tree, tree); | |
3a978d72 | 106 | static tree get_class_bindings (tree, tree, tree); |
3db45ab5 | 107 | static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, |
e7e93965 | 108 | bool, bool); |
3a978d72 NN |
109 | static void tsubst_enum (tree, tree, tree); |
110 | static tree add_to_template_args (tree, tree); | |
111 | static tree add_outermost_template_args (tree, tree); | |
112 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); | |
c8094d83 | 113 | static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*); |
3a978d72 | 114 | static int type_unification_real (tree, tree, tree, tree, |
30f86ec3 | 115 | int, unification_kind_t, int); |
3a978d72 | 116 | static void note_template_header (int); |
b6ab6892 | 117 | static tree convert_nontype_argument_function (tree, tree); |
3a978d72 NN |
118 | static tree convert_nontype_argument (tree, tree); |
119 | static tree convert_template_argument (tree, tree, tree, | |
120 | tsubst_flags_t, int, tree); | |
0c58f841 MA |
121 | static int for_each_template_parm (tree, tree_fn_t, void*, |
122 | struct pointer_set_t*); | |
3a978d72 NN |
123 | static tree build_template_parm_index (int, int, int, tree, tree); |
124 | static int inline_needs_template_parms (tree); | |
125 | static void push_inline_template_parms_recursive (tree, int); | |
3a978d72 | 126 | static tree retrieve_local_specialization (tree); |
3a978d72 | 127 | static void register_local_specialization (tree, tree); |
3a978d72 | 128 | static tree reduce_template_parm_level (tree, tree, int); |
3a978d72 NN |
129 | static int mark_template_parm (tree, void *); |
130 | static int template_parm_this_level_p (tree, void *); | |
131 | static tree tsubst_friend_function (tree, tree); | |
132 | static tree tsubst_friend_class (tree, tree); | |
133 | static int can_complete_type_without_circularity (tree); | |
a34d3336 | 134 | static tree get_bindings (tree, tree, tree, bool); |
3a978d72 NN |
135 | static int template_decl_level (tree); |
136 | static int check_cv_quals_for_unify (int, tree, tree); | |
a91db711 NS |
137 | static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree); |
138 | static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree); | |
3a978d72 NN |
139 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
140 | static void regenerate_decl_from_template (tree, tree); | |
3a978d72 | 141 | static tree most_specialized_class (tree, tree); |
3a978d72 | 142 | static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int); |
3a978d72 NN |
143 | static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree); |
144 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); | |
9b910171 | 145 | static bool check_specialization_scope (void); |
3a978d72 NN |
146 | static tree process_partial_specialization (tree); |
147 | static void set_current_access_from_decl (tree); | |
148 | static void check_default_tmpl_args (tree, tree, int, int); | |
3a978d72 | 149 | static tree get_template_base (tree, tree, tree, tree); |
3a978d72 NN |
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); | |
3a978d72 NN |
153 | static int template_args_equal (tree, tree); |
154 | static void tsubst_default_arguments (tree); | |
155 | static tree for_each_template_parm_r (tree *, int *, void *); | |
156 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); | |
157 | static void copy_default_args_to_explicit_spec (tree); | |
158 | static int invalid_nontype_parm_type_p (tree, tsubst_flags_t); | |
a723baf1 | 159 | static int eq_local_specializations (const void *, const void *); |
5552b43c | 160 | static bool dependent_type_p_r (tree); |
14d22dd6 | 161 | static tree tsubst (tree, tree, tsubst_flags_t, tree); |
015c2c66 | 162 | static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool); |
14d22dd6 | 163 | static tree tsubst_copy (tree, tree, tsubst_flags_t, tree); |
36a117a5 | 164 | |
2b59fc25 KL |
165 | /* Make the current scope suitable for access checking when we are |
166 | processing T. T can be FUNCTION_DECL for instantiated function | |
2b907f5c KL |
167 | template, or VAR_DECL for static member variable (need by |
168 | instantiate_decl). */ | |
2b59fc25 | 169 | |
8ce33230 | 170 | static void |
2b907f5c | 171 | push_access_scope (tree t) |
2b59fc25 | 172 | { |
50bc768d NS |
173 | gcc_assert (TREE_CODE (t) == FUNCTION_DECL |
174 | || TREE_CODE (t) == VAR_DECL); | |
2b59fc25 | 175 | |
23ff7e2d KL |
176 | if (DECL_FRIEND_CONTEXT (t)) |
177 | push_nested_class (DECL_FRIEND_CONTEXT (t)); | |
178 | else if (DECL_CLASS_SCOPE_P (t)) | |
2b907f5c | 179 | push_nested_class (DECL_CONTEXT (t)); |
0f399e5f KL |
180 | else |
181 | push_to_top_level (); | |
c8094d83 | 182 | |
2b907f5c | 183 | if (TREE_CODE (t) == FUNCTION_DECL) |
0f399e5f KL |
184 | { |
185 | saved_access_scope = tree_cons | |
186 | (NULL_TREE, current_function_decl, saved_access_scope); | |
187 | current_function_decl = t; | |
188 | } | |
2b59fc25 KL |
189 | } |
190 | ||
2b59fc25 KL |
191 | /* Restore the scope set up by push_access_scope. T is the node we |
192 | are processing. */ | |
193 | ||
8ce33230 | 194 | static void |
3a978d72 | 195 | pop_access_scope (tree t) |
2b59fc25 | 196 | { |
2b907f5c | 197 | if (TREE_CODE (t) == FUNCTION_DECL) |
2b59fc25 KL |
198 | { |
199 | current_function_decl = TREE_VALUE (saved_access_scope); | |
200 | saved_access_scope = TREE_CHAIN (saved_access_scope); | |
201 | } | |
0f399e5f | 202 | |
23ff7e2d | 203 | if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t)) |
0f399e5f KL |
204 | pop_nested_class (); |
205 | else | |
206 | pop_from_top_level (); | |
2b59fc25 KL |
207 | } |
208 | ||
a723baf1 MM |
209 | /* Do any processing required when DECL (a member template |
210 | declaration) is finished. Returns the TEMPLATE_DECL corresponding | |
211 | to DECL, unless it is a specialization, in which case the DECL | |
212 | itself is returned. */ | |
e1467ff2 MM |
213 | |
214 | tree | |
3a978d72 | 215 | finish_member_template_decl (tree decl) |
e1467ff2 | 216 | { |
a723baf1 MM |
217 | if (decl == error_mark_node) |
218 | return error_mark_node; | |
219 | ||
50bc768d | 220 | gcc_assert (DECL_P (decl)); |
a723baf1 MM |
221 | |
222 | if (TREE_CODE (decl) == TYPE_DECL) | |
93cdc044 | 223 | { |
a723baf1 MM |
224 | tree type; |
225 | ||
226 | type = TREE_TYPE (decl); | |
c8094d83 | 227 | if (IS_AGGR_TYPE (type) |
a723baf1 MM |
228 | && CLASSTYPE_TEMPLATE_INFO (type) |
229 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
93cdc044 | 230 | { |
a723baf1 | 231 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
93cdc044 JM |
232 | check_member_template (tmpl); |
233 | return tmpl; | |
234 | } | |
8d019cef | 235 | return NULL_TREE; |
93cdc044 | 236 | } |
07c88314 | 237 | else if (TREE_CODE (decl) == FIELD_DECL) |
0f51ccfc | 238 | error ("data member %qD cannot be a member template", decl); |
a1da6cba | 239 | else if (DECL_TEMPLATE_INFO (decl)) |
e1467ff2 | 240 | { |
a1da6cba MM |
241 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
242 | { | |
243 | check_member_template (DECL_TI_TEMPLATE (decl)); | |
244 | return DECL_TI_TEMPLATE (decl); | |
245 | } | |
246 | else | |
247 | return decl; | |
c8094d83 | 248 | } |
a1da6cba | 249 | else |
0f51ccfc | 250 | error ("invalid member template declaration %qD", decl); |
e1467ff2 | 251 | |
a1da6cba | 252 | return error_mark_node; |
f84b4be9 | 253 | } |
e1467ff2 | 254 | |
f84b4be9 | 255 | /* Returns the template nesting level of the indicated class TYPE. |
c8094d83 | 256 | |
f84b4be9 JM |
257 | For example, in: |
258 | template <class T> | |
259 | struct A | |
260 | { | |
261 | template <class U> | |
262 | struct B {}; | |
263 | }; | |
264 | ||
c8094d83 | 265 | A<T>::B<U> has depth two, while A<T> has depth one. |
39c01e4c | 266 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
260cd73f | 267 | they are instantiations, not specializations. |
39c01e4c MM |
268 | |
269 | This function is guaranteed to return 0 if passed NULL_TREE so | |
270 | that, for example, `template_class_depth (current_class_type)' is | |
271 | always safe. */ | |
f84b4be9 | 272 | |
260cd73f VR |
273 | int |
274 | template_class_depth (tree type) | |
f84b4be9 | 275 | { |
93cdc044 | 276 | int depth; |
f84b4be9 | 277 | |
c8094d83 | 278 | for (depth = 0; |
ed44da02 | 279 | type && TREE_CODE (type) != NAMESPACE_DECL; |
c8094d83 | 280 | type = (TREE_CODE (type) == FUNCTION_DECL) |
4f1c5b7d | 281 | ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type)) |
ed44da02 MM |
282 | { |
283 | if (TREE_CODE (type) != FUNCTION_DECL) | |
284 | { | |
285 | if (CLASSTYPE_TEMPLATE_INFO (type) | |
286 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) | |
260cd73f | 287 | && uses_template_parms (CLASSTYPE_TI_ARGS (type))) |
ed44da02 MM |
288 | ++depth; |
289 | } | |
c8094d83 | 290 | else |
ed44da02 MM |
291 | { |
292 | if (DECL_TEMPLATE_INFO (type) | |
293 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type)) | |
260cd73f | 294 | && uses_template_parms (DECL_TI_ARGS (type))) |
ed44da02 MM |
295 | ++depth; |
296 | } | |
297 | } | |
f84b4be9 JM |
298 | |
299 | return depth; | |
e1467ff2 | 300 | } |
98c1c668 | 301 | |
cae40af6 JM |
302 | /* Returns 1 if processing DECL as part of do_pending_inlines |
303 | needs us to push template parms. */ | |
304 | ||
305 | static int | |
3a978d72 | 306 | inline_needs_template_parms (tree decl) |
cae40af6 JM |
307 | { |
308 | if (! DECL_TEMPLATE_INFO (decl)) | |
309 | return 0; | |
f84b4be9 | 310 | |
36a117a5 | 311 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
cae40af6 JM |
312 | > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl))); |
313 | } | |
314 | ||
315 | /* Subroutine of maybe_begin_member_template_processing. | |
316 | Push the template parms in PARMS, starting from LEVELS steps into the | |
317 | chain, and ending at the beginning, since template parms are listed | |
318 | innermost first. */ | |
319 | ||
320 | static void | |
3a978d72 | 321 | push_inline_template_parms_recursive (tree parmlist, int levels) |
cae40af6 JM |
322 | { |
323 | tree parms = TREE_VALUE (parmlist); | |
324 | int i; | |
325 | ||
326 | if (levels > 1) | |
327 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1); | |
786b5245 | 328 | |
98c1c668 | 329 | ++processing_template_decl; |
cae40af6 | 330 | current_template_parms |
4890c2f4 | 331 | = tree_cons (size_int (processing_template_decl), |
98c1c668 | 332 | parms, current_template_parms); |
cae40af6 JM |
333 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
334 | ||
ac20c67a | 335 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
0cbd7506 | 336 | NULL); |
c8094d83 | 337 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
98c1c668 | 338 | { |
0f67a82f | 339 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
42b304f1 | 340 | |
0f67a82f LM |
341 | if (parm == error_mark_node) |
342 | continue; | |
42b304f1 | 343 | |
50bc768d | 344 | gcc_assert (DECL_P (parm)); |
cae40af6 | 345 | |
98c1c668 JM |
346 | switch (TREE_CODE (parm)) |
347 | { | |
786b5245 | 348 | case TYPE_DECL: |
73b0fce8 | 349 | case TEMPLATE_DECL: |
98c1c668 JM |
350 | pushdecl (parm); |
351 | break; | |
786b5245 MM |
352 | |
353 | case PARM_DECL: | |
354 | { | |
fc03edb3 MM |
355 | /* Make a CONST_DECL as is done in process_template_parm. |
356 | It is ugly that we recreate this here; the original | |
357 | version built in process_template_parm is no longer | |
358 | available. */ | |
786b5245 MM |
359 | tree decl = build_decl (CONST_DECL, DECL_NAME (parm), |
360 | TREE_TYPE (parm)); | |
c727aa5e | 361 | DECL_ARTIFICIAL (decl) = 1; |
6de9cd9a DN |
362 | TREE_CONSTANT (decl) = 1; |
363 | TREE_INVARIANT (decl) = 1; | |
364 | TREE_READONLY (decl) = 1; | |
786b5245 | 365 | DECL_INITIAL (decl) = DECL_INITIAL (parm); |
cd9f6678 | 366 | SET_DECL_TEMPLATE_PARM_P (decl); |
786b5245 MM |
367 | pushdecl (decl); |
368 | } | |
cae40af6 | 369 | break; |
786b5245 | 370 | |
98c1c668 | 371 | default: |
315fb5db | 372 | gcc_unreachable (); |
98c1c668 JM |
373 | } |
374 | } | |
375 | } | |
376 | ||
cae40af6 JM |
377 | /* Restore the template parameter context for a member template or |
378 | a friend template defined in a class definition. */ | |
379 | ||
380 | void | |
3a978d72 | 381 | maybe_begin_member_template_processing (tree decl) |
cae40af6 JM |
382 | { |
383 | tree parms; | |
3dcaad8b | 384 | int levels = 0; |
cae40af6 | 385 | |
3dcaad8b MM |
386 | if (inline_needs_template_parms (decl)) |
387 | { | |
388 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); | |
389 | levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl; | |
cae40af6 | 390 | |
3dcaad8b MM |
391 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
392 | { | |
393 | --levels; | |
394 | parms = TREE_CHAIN (parms); | |
395 | } | |
cae40af6 | 396 | |
3dcaad8b | 397 | push_inline_template_parms_recursive (parms, levels); |
cae40af6 JM |
398 | } |
399 | ||
3dcaad8b MM |
400 | /* Remember how many levels of template parameters we pushed so that |
401 | we can pop them later. */ | |
53824026 | 402 | VEC_safe_push (int, heap, inline_parm_levels, levels); |
cae40af6 JM |
403 | } |
404 | ||
1875c2b7 | 405 | /* Undo the effects of maybe_begin_member_template_processing. */ |
98c1c668 | 406 | |
c8094d83 | 407 | void |
3a978d72 | 408 | maybe_end_member_template_processing (void) |
98c1c668 | 409 | { |
3dcaad8b | 410 | int i; |
53824026 | 411 | int last; |
3dcaad8b | 412 | |
53824026 | 413 | if (VEC_length (int, inline_parm_levels) == 0) |
98c1c668 JM |
414 | return; |
415 | ||
53824026 KH |
416 | last = VEC_pop (int, inline_parm_levels); |
417 | for (i = 0; i < last; ++i) | |
cae40af6 JM |
418 | { |
419 | --processing_template_decl; | |
420 | current_template_parms = TREE_CHAIN (current_template_parms); | |
421 | poplevel (0, 0, 0); | |
422 | } | |
98c1c668 JM |
423 | } |
424 | ||
36a117a5 | 425 | /* Return a new template argument vector which contains all of ARGS, |
f9a7ae04 | 426 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
e6f1275f JM |
427 | |
428 | static tree | |
3a978d72 | 429 | add_to_template_args (tree args, tree extra_args) |
e6f1275f | 430 | { |
36a117a5 MM |
431 | tree new_args; |
432 | int extra_depth; | |
433 | int i; | |
434 | int j; | |
e6f1275f | 435 | |
36a117a5 | 436 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
f31c0a32 | 437 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
e6f1275f | 438 | |
36a117a5 MM |
439 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
440 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); | |
e6f1275f | 441 | |
36a117a5 MM |
442 | for (j = 1; j <= extra_depth; ++j, ++i) |
443 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); | |
c8094d83 | 444 | |
e6f1275f JM |
445 | return new_args; |
446 | } | |
447 | ||
36a117a5 MM |
448 | /* Like add_to_template_args, but only the outermost ARGS are added to |
449 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH | |
450 | (EXTRA_ARGS) levels are added. This function is used to combine | |
451 | the template arguments from a partial instantiation with the | |
452 | template arguments used to attain the full instantiation from the | |
453 | partial instantiation. */ | |
98c1c668 | 454 | |
4966381a | 455 | static tree |
3a978d72 | 456 | add_outermost_template_args (tree args, tree extra_args) |
98c1c668 JM |
457 | { |
458 | tree new_args; | |
459 | ||
e4a84209 MM |
460 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
461 | something very fishy is going on. */ | |
50bc768d | 462 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
e4a84209 MM |
463 | |
464 | /* If *all* the new arguments will be the EXTRA_ARGS, just return | |
465 | them. */ | |
466 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) | |
467 | return extra_args; | |
468 | ||
36a117a5 MM |
469 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
470 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); | |
c8094d83 | 471 | |
36a117a5 | 472 | new_args = add_to_template_args (args, extra_args); |
98c1c668 | 473 | |
36a117a5 MM |
474 | /* Now, we restore ARGS to its full dimensions. */ |
475 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); | |
98c1c668 JM |
476 | |
477 | return new_args; | |
478 | } | |
5566b478 | 479 | |
f9a7ae04 MM |
480 | /* Return the N levels of innermost template arguments from the ARGS. */ |
481 | ||
482 | tree | |
3a978d72 | 483 | get_innermost_template_args (tree args, int n) |
f9a7ae04 MM |
484 | { |
485 | tree new_args; | |
486 | int extra_levels; | |
487 | int i; | |
488 | ||
50bc768d | 489 | gcc_assert (n >= 0); |
f9a7ae04 MM |
490 | |
491 | /* If N is 1, just return the innermost set of template arguments. */ | |
492 | if (n == 1) | |
493 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); | |
c8094d83 | 494 | |
f9a7ae04 MM |
495 | /* If we're not removing anything, just return the arguments we were |
496 | given. */ | |
497 | extra_levels = TMPL_ARGS_DEPTH (args) - n; | |
50bc768d | 498 | gcc_assert (extra_levels >= 0); |
f9a7ae04 MM |
499 | if (extra_levels == 0) |
500 | return args; | |
501 | ||
502 | /* Make a new set of arguments, not containing the outer arguments. */ | |
503 | new_args = make_tree_vec (n); | |
504 | for (i = 1; i <= n; ++i) | |
c8094d83 | 505 | SET_TMPL_ARGS_LEVEL (new_args, i, |
f9a7ae04 MM |
506 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
507 | ||
508 | return new_args; | |
509 | } | |
510 | ||
5566b478 MS |
511 | /* We've got a template header coming up; push to a new level for storing |
512 | the parms. */ | |
8d08fdba | 513 | |
8d08fdba | 514 | void |
3a978d72 | 515 | begin_template_parm_list (void) |
8d08fdba | 516 | { |
6757edfe MM |
517 | /* We use a non-tag-transparent scope here, which causes pushtag to |
518 | put tags in this scope, rather than in the enclosing class or | |
519 | namespace scope. This is the right thing, since we want | |
520 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a | |
521 | global template class, push_template_decl handles putting the | |
522 | TEMPLATE_DECL into top-level scope. For a nested template class, | |
523 | e.g.: | |
524 | ||
525 | template <class T> struct S1 { | |
0cbd7506 | 526 | template <class T> struct S2 {}; |
6757edfe MM |
527 | }; |
528 | ||
529 | pushtag contains special code to call pushdecl_with_scope on the | |
530 | TEMPLATE_DECL for S2. */ | |
ac20c67a | 531 | begin_scope (sk_template_parms, NULL); |
5156628f | 532 | ++processing_template_decl; |
67ffc812 | 533 | ++processing_template_parmlist; |
386b8a85 JM |
534 | note_template_header (0); |
535 | } | |
536 | ||
6c30752f | 537 | /* This routine is called when a specialization is declared. If it is |
9b910171 LM |
538 | invalid to declare a specialization here, an error is reported and |
539 | false is returned, otherwise this routine will return true. */ | |
6c30752f | 540 | |
9b910171 | 541 | static bool |
3a978d72 | 542 | check_specialization_scope (void) |
6c30752f MM |
543 | { |
544 | tree scope = current_scope (); | |
3ddfb0e6 | 545 | |
c8094d83 MS |
546 | /* [temp.expl.spec] |
547 | ||
6c30752f MM |
548 | An explicit specialization shall be declared in the namespace of |
549 | which the template is a member, or, for member templates, in the | |
550 | namespace of which the enclosing class or enclosing class | |
551 | template is a member. An explicit specialization of a member | |
552 | function, member class or static data member of a class template | |
553 | shall be declared in the namespace of which the class template | |
554 | is a member. */ | |
555 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) | |
9b910171 LM |
556 | { |
557 | error ("explicit specialization in non-namespace scope %qD", scope); | |
558 | return false; | |
559 | } | |
3ddfb0e6 | 560 | |
c8094d83 | 561 | /* [temp.expl.spec] |
6c30752f MM |
562 | |
563 | In an explicit specialization declaration for a member of a class | |
564 | template or a member template that appears in namespace scope, | |
565 | the member template and some of its enclosing class templates may | |
566 | remain unspecialized, except that the declaration shall not | |
567 | explicitly specialize a class member template if its enclosing | |
568 | class templates are not explicitly specialized as well. */ | |
c8094d83 | 569 | if (current_template_parms) |
9b910171 LM |
570 | { |
571 | error ("enclosing class templates are not explicitly specialized"); | |
572 | return false; | |
573 | } | |
574 | ||
575 | return true; | |
6c30752f MM |
576 | } |
577 | ||
c6002625 | 578 | /* We've just seen template <>. */ |
386b8a85 | 579 | |
9b910171 | 580 | bool |
3a978d72 | 581 | begin_specialization (void) |
386b8a85 | 582 | { |
ac20c67a | 583 | begin_scope (sk_template_spec, NULL); |
386b8a85 | 584 | note_template_header (1); |
9b910171 | 585 | return check_specialization_scope (); |
386b8a85 JM |
586 | } |
587 | ||
dc957d14 | 588 | /* Called at then end of processing a declaration preceded by |
386b8a85 JM |
589 | template<>. */ |
590 | ||
c8094d83 | 591 | void |
3a978d72 | 592 | end_specialization (void) |
386b8a85 | 593 | { |
74b846e0 | 594 | finish_scope (); |
386b8a85 JM |
595 | reset_specialization (); |
596 | } | |
597 | ||
386b8a85 | 598 | /* Any template <>'s that we have seen thus far are not referring to a |
c6002625 | 599 | function specialization. */ |
386b8a85 JM |
600 | |
601 | void | |
3a978d72 | 602 | reset_specialization (void) |
386b8a85 JM |
603 | { |
604 | processing_specialization = 0; | |
605 | template_header_count = 0; | |
606 | } | |
607 | ||
838dfd8a | 608 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
386b8a85 JM |
609 | it was of the form template <>. */ |
610 | ||
c8094d83 | 611 | static void |
3a978d72 | 612 | note_template_header (int specialization) |
386b8a85 JM |
613 | { |
614 | processing_specialization = specialization; | |
615 | template_header_count++; | |
616 | } | |
617 | ||
75650646 | 618 | /* We're beginning an explicit instantiation. */ |
386b8a85 | 619 | |
75650646 | 620 | void |
3a978d72 | 621 | begin_explicit_instantiation (void) |
386b8a85 | 622 | { |
50bc768d | 623 | gcc_assert (!processing_explicit_instantiation); |
a723baf1 | 624 | processing_explicit_instantiation = true; |
75650646 | 625 | } |
386b8a85 | 626 | |
386b8a85 | 627 | |
75650646 | 628 | void |
3a978d72 | 629 | end_explicit_instantiation (void) |
75650646 | 630 | { |
50bc768d | 631 | gcc_assert (processing_explicit_instantiation); |
a723baf1 | 632 | processing_explicit_instantiation = false; |
75650646 | 633 | } |
386b8a85 | 634 | |
13a44ee0 | 635 | /* An explicit specialization or partial specialization TMPL is being |
b1cc95ce MM |
636 | declared. Check that the namespace in which the specialization is |
637 | occurring is permissible. Returns false iff it is invalid to | |
638 | specialize TMPL in the current namespace. */ | |
c8094d83 | 639 | |
b1cc95ce MM |
640 | static bool |
641 | check_specialization_namespace (tree tmpl) | |
642 | { | |
643 | tree tpl_ns = decl_namespace_context (tmpl); | |
644 | ||
645 | /* [tmpl.expl.spec] | |
c8094d83 | 646 | |
b1cc95ce MM |
647 | An explicit specialization shall be declared in the namespace of |
648 | which the template is a member, or, for member templates, in the | |
649 | namespace of which the enclosing class or enclosing class | |
650 | template is a member. An explicit specialization of a member | |
651 | function, member class or static data member of a class template | |
652 | shall be declared in the namespace of which the class template is | |
653 | a member. */ | |
654 | if (is_associated_namespace (current_namespace, tpl_ns)) | |
655 | /* Same or super-using namespace. */ | |
656 | return true; | |
657 | else | |
658 | { | |
0f51ccfc | 659 | pedwarn ("specialization of %qD in different namespace", tmpl); |
dee15844 | 660 | pedwarn (" from definition of %q+#D", tmpl); |
b1cc95ce MM |
661 | return false; |
662 | } | |
663 | } | |
664 | ||
9c12301f MM |
665 | /* SPEC is an explicit instantiation. Check that it is valid to |
666 | perform this explicit instantiation in the current namespace. */ | |
667 | ||
668 | static void | |
669 | check_explicit_instantiation_namespace (tree spec) | |
670 | { | |
671 | tree ns; | |
672 | ||
673 | /* DR 275: An explicit instantiation shall appear in an enclosing | |
3db45ab5 | 674 | namespace of its template. */ |
9c12301f MM |
675 | ns = decl_namespace_context (spec); |
676 | if (!is_ancestor (current_namespace, ns)) | |
677 | pedwarn ("explicit instantiation of %qD in namespace %qD " | |
312ea236 | 678 | "(which does not enclose namespace %qD)", |
9c12301f MM |
679 | spec, current_namespace, ns); |
680 | } | |
681 | ||
36a117a5 MM |
682 | /* The TYPE is being declared. If it is a template type, that means it |
683 | is a partial specialization. Do appropriate error-checking. */ | |
684 | ||
9ce387d9 | 685 | tree |
3a978d72 | 686 | maybe_process_partial_specialization (tree type) |
36a117a5 | 687 | { |
c9043d03 VR |
688 | tree context; |
689 | ||
690 | if (type == error_mark_node) | |
9ce387d9 VR |
691 | return error_mark_node; |
692 | ||
693 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
694 | { | |
695 | error ("name of class shadows template template parameter %qD", | |
696 | TYPE_NAME (type)); | |
697 | return error_mark_node; | |
698 | } | |
c9043d03 VR |
699 | |
700 | context = TYPE_CONTEXT (type); | |
6e049fcd | 701 | |
a723baf1 | 702 | if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
36a117a5 | 703 | { |
6e049fcd KL |
704 | /* This is for ordinary explicit specialization and partial |
705 | specialization of a template class such as: | |
706 | ||
707 | template <> class C<int>; | |
708 | ||
709 | or: | |
710 | ||
711 | template <class T> class C<T*>; | |
712 | ||
713 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ | |
714 | ||
36a117a5 | 715 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) |
d0f062fb | 716 | && !COMPLETE_TYPE_P (type)) |
36a117a5 | 717 | { |
b1cc95ce | 718 | check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)); |
370af2d5 | 719 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
36a117a5 MM |
720 | if (processing_template_decl) |
721 | push_template_decl (TYPE_MAIN_DECL (type)); | |
722 | } | |
723 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) | |
0f51ccfc | 724 | error ("specialization of %qT after instantiation", type); |
36a117a5 | 725 | } |
6e049fcd KL |
726 | else if (CLASS_TYPE_P (type) |
727 | && !CLASSTYPE_USE_TEMPLATE (type) | |
728 | && CLASSTYPE_TEMPLATE_INFO (type) | |
729 | && context && CLASS_TYPE_P (context) | |
730 | && CLASSTYPE_TEMPLATE_INFO (context)) | |
731 | { | |
732 | /* This is for an explicit specialization of member class | |
733 | template according to [temp.expl.spec/18]: | |
734 | ||
735 | template <> template <class U> class C<int>::D; | |
736 | ||
737 | The context `C<int>' must be an implicit instantiation. | |
738 | Otherwise this is just a member class template declared | |
739 | earlier like: | |
740 | ||
741 | template <> class C<int> { template <class U> class D; }; | |
742 | template <> template <class U> class C<int>::D; | |
743 | ||
744 | In the first case, `C<int>::D' is a specialization of `C<T>::D' | |
745 | while in the second case, `C<int>::D' is a primary template | |
746 | and `C<T>::D' may not exist. */ | |
747 | ||
748 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) | |
749 | && !COMPLETE_TYPE_P (type)) | |
750 | { | |
751 | tree t; | |
752 | ||
753 | if (current_namespace | |
754 | != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) | |
755 | { | |
0f51ccfc | 756 | pedwarn ("specializing %q#T in different namespace", type); |
dee15844 JM |
757 | pedwarn (" from definition of %q+#D", |
758 | CLASSTYPE_TI_TEMPLATE (type)); | |
6e049fcd KL |
759 | } |
760 | ||
761 | /* Check for invalid specialization after instantiation: | |
762 | ||
763 | template <> template <> class C<int>::D<int>; | |
764 | template <> template <class U> class C<int>::D; */ | |
765 | ||
766 | for (t = DECL_TEMPLATE_INSTANTIATIONS | |
767 | (most_general_template (CLASSTYPE_TI_TEMPLATE (type))); | |
768 | t; t = TREE_CHAIN (t)) | |
769 | if (TREE_VALUE (t) != type | |
770 | && TYPE_CONTEXT (TREE_VALUE (t)) == context) | |
0f51ccfc | 771 | error ("specialization %qT after instantiation %qT", |
6e049fcd KL |
772 | type, TREE_VALUE (t)); |
773 | ||
774 | /* Mark TYPE as a specialization. And as a result, we only | |
775 | have one level of template argument for the innermost | |
776 | class template. */ | |
777 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); | |
778 | CLASSTYPE_TI_ARGS (type) | |
779 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
780 | } | |
781 | } | |
20496fa2 | 782 | else if (processing_specialization) |
9ce387d9 VR |
783 | { |
784 | error ("explicit specialization of non-template %qT", type); | |
785 | return error_mark_node; | |
786 | } | |
787 | ||
788 | return type; | |
36a117a5 MM |
789 | } |
790 | ||
ee81147e | 791 | /* Returns nonzero if we can optimize the retrieval of specializations |
c7222c02 MM |
792 | for TMPL, a TEMPLATE_DECL. In particular, for such a template, we |
793 | do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */ | |
794 | ||
c8094d83 | 795 | static inline bool |
c7222c02 MM |
796 | optimize_specialization_lookup_p (tree tmpl) |
797 | { | |
798 | return (DECL_FUNCTION_TEMPLATE_P (tmpl) | |
799 | && DECL_CLASS_SCOPE_P (tmpl) | |
800 | /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template | |
801 | parameter. */ | |
802 | && CLASS_TYPE_P (DECL_CONTEXT (tmpl)) | |
1ca939e5 MM |
803 | /* The optimized lookup depends on the fact that the |
804 | template arguments for the member function template apply | |
805 | purely to the containing class, which is not true if the | |
806 | containing class is an explicit or partial | |
807 | specialization. */ | |
808 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl)) | |
c7222c02 MM |
809 | && !DECL_MEMBER_TEMPLATE_P (tmpl) |
810 | && !DECL_CONV_FN_P (tmpl) | |
811 | /* It is possible to have a template that is not a member | |
812 | template and is not a member of a template class: | |
c8094d83 MS |
813 | |
814 | template <typename T> | |
c7222c02 | 815 | struct S { friend A::f(); }; |
c8094d83 | 816 | |
c7222c02 MM |
817 | Here, the friend function is a template, but the context does |
818 | not have template information. The optimized lookup relies | |
819 | on having ARGS be the template arguments for both the class | |
820 | and the function template. */ | |
821 | && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl))); | |
822 | } | |
823 | ||
75650646 MM |
824 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
825 | specialization is either an instantiation or an explicit | |
826 | specialization) of TMPL for the given template ARGS. If there is | |
827 | no such specialization, return NULL_TREE. The ARGS are a vector of | |
828 | arguments, or a vector of vectors of arguments, in the case of | |
c8094d83 | 829 | templates with more than one level of parameters. |
c7222c02 MM |
830 | |
831 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, | |
832 | then we search for a partial specialization matching ARGS. This | |
833 | parameter is ignored if TMPL is not a class template. */ | |
c8094d83 | 834 | |
75650646 | 835 | static tree |
c8094d83 | 836 | retrieve_specialization (tree tmpl, tree args, |
c7222c02 | 837 | bool class_specializations_p) |
75650646 | 838 | { |
2d8ba2c7 LM |
839 | if (args == error_mark_node) |
840 | return NULL_TREE; | |
841 | ||
50bc768d | 842 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 843 | |
36a117a5 MM |
844 | /* There should be as many levels of arguments as there are |
845 | levels of parameters. */ | |
c8094d83 | 846 | gcc_assert (TMPL_ARGS_DEPTH (args) |
50bc768d | 847 | == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); |
c8094d83 | 848 | |
c7222c02 MM |
849 | if (optimize_specialization_lookup_p (tmpl)) |
850 | { | |
851 | tree class_template; | |
852 | tree class_specialization; | |
d4e6fecb | 853 | VEC(tree,gc) *methods; |
c7222c02 MM |
854 | tree fns; |
855 | int idx; | |
856 | ||
857 | /* The template arguments actually apply to the containing | |
858 | class. Find the class specialization with those | |
859 | arguments. */ | |
860 | class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl)); | |
c8094d83 | 861 | class_specialization |
c7222c02 MM |
862 | = retrieve_specialization (class_template, args, |
863 | /*class_specializations_p=*/false); | |
864 | if (!class_specialization) | |
865 | return NULL_TREE; | |
866 | /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC | |
867 | for the specialization. */ | |
868 | idx = class_method_index_for_fn (class_specialization, tmpl); | |
869 | if (idx == -1) | |
870 | return NULL_TREE; | |
871 | /* Iterate through the methods with the indicated name, looking | |
872 | for the one that has an instance of TMPL. */ | |
873 | methods = CLASSTYPE_METHOD_VEC (class_specialization); | |
874 | for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns)) | |
875 | { | |
876 | tree fn = OVL_CURRENT (fns); | |
877 | if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl) | |
878 | return fn; | |
879 | } | |
880 | return NULL_TREE; | |
881 | } | |
882 | else | |
883 | { | |
884 | tree *sp; | |
885 | tree *head; | |
886 | ||
887 | /* Class templates store their instantiations on the | |
888 | DECL_TEMPLATE_INSTANTIATIONS list; other templates use the | |
889 | DECL_TEMPLATE_SPECIALIZATIONS list. */ | |
890 | if (!class_specializations_p | |
891 | && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL) | |
892 | sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl); | |
893 | else | |
894 | sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
895 | head = sp; | |
896 | /* Iterate through the list until we find a matching template. */ | |
897 | while (*sp != NULL_TREE) | |
898 | { | |
899 | tree spec = *sp; | |
c8094d83 | 900 | |
c7222c02 MM |
901 | if (comp_template_args (TREE_PURPOSE (spec), args)) |
902 | { | |
903 | /* Use the move-to-front heuristic to speed up future | |
c8094d83 | 904 | searches. */ |
c7222c02 MM |
905 | if (spec != *head) |
906 | { | |
907 | *sp = TREE_CHAIN (*sp); | |
908 | TREE_CHAIN (spec) = *head; | |
909 | *head = spec; | |
910 | } | |
911 | return TREE_VALUE (spec); | |
912 | } | |
913 | sp = &TREE_CHAIN (spec); | |
914 | } | |
915 | } | |
75650646 MM |
916 | |
917 | return NULL_TREE; | |
386b8a85 JM |
918 | } |
919 | ||
dc957d14 | 920 | /* Like retrieve_specialization, but for local declarations. */ |
9188c363 MM |
921 | |
922 | static tree | |
3a978d72 | 923 | retrieve_local_specialization (tree tmpl) |
9188c363 | 924 | { |
67f5655f | 925 | tree spec = (tree) htab_find_with_hash (local_specializations, tmpl, |
3db45ab5 | 926 | htab_hash_pointer (tmpl)); |
a723baf1 | 927 | return spec ? TREE_PURPOSE (spec) : NULL_TREE; |
9188c363 MM |
928 | } |
929 | ||
838dfd8a | 930 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
6757edfe MM |
931 | |
932 | int | |
3a978d72 | 933 | is_specialization_of (tree decl, tree tmpl) |
6757edfe MM |
934 | { |
935 | tree t; | |
936 | ||
937 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
938 | { | |
c8094d83 | 939 | for (t = decl; |
6757edfe MM |
940 | t != NULL_TREE; |
941 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) | |
942 | if (t == tmpl) | |
943 | return 1; | |
944 | } | |
c8094d83 | 945 | else |
6757edfe | 946 | { |
50bc768d | 947 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
6757edfe MM |
948 | |
949 | for (t = TREE_TYPE (decl); | |
950 | t != NULL_TREE; | |
951 | t = CLASSTYPE_USE_TEMPLATE (t) | |
952 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) | |
9edc3913 | 953 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
6757edfe | 954 | return 1; |
c8094d83 | 955 | } |
386b8a85 | 956 | |
6757edfe MM |
957 | return 0; |
958 | } | |
75650646 | 959 | |
d43f603d KL |
960 | /* Returns nonzero iff DECL is a specialization of friend declaration |
961 | FRIEND according to [temp.friend]. */ | |
962 | ||
963 | bool | |
964 | is_specialization_of_friend (tree decl, tree friend) | |
965 | { | |
966 | bool need_template = true; | |
967 | int template_depth; | |
968 | ||
b939a023 KL |
969 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
970 | || TREE_CODE (decl) == TYPE_DECL); | |
d43f603d KL |
971 | |
972 | /* For [temp.friend/6] when FRIEND is an ordinary member function | |
973 | of a template class, we want to check if DECL is a specialization | |
974 | if this. */ | |
975 | if (TREE_CODE (friend) == FUNCTION_DECL | |
976 | && DECL_TEMPLATE_INFO (friend) | |
977 | && !DECL_USE_TEMPLATE (friend)) | |
978 | { | |
b939a023 | 979 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
d43f603d KL |
980 | friend = DECL_TI_TEMPLATE (friend); |
981 | need_template = false; | |
982 | } | |
b939a023 KL |
983 | else if (TREE_CODE (friend) == TEMPLATE_DECL |
984 | && !PRIMARY_TEMPLATE_P (friend)) | |
985 | need_template = false; | |
d43f603d KL |
986 | |
987 | /* There is nothing to do if this is not a template friend. */ | |
988 | if (TREE_CODE (friend) != TEMPLATE_DECL) | |
b939a023 | 989 | return false; |
d43f603d KL |
990 | |
991 | if (is_specialization_of (decl, friend)) | |
b939a023 | 992 | return true; |
d43f603d KL |
993 | |
994 | /* [temp.friend/6] | |
995 | A member of a class template may be declared to be a friend of a | |
996 | non-template class. In this case, the corresponding member of | |
997 | every specialization of the class template is a friend of the | |
998 | class granting friendship. | |
c8094d83 | 999 | |
d43f603d KL |
1000 | For example, given a template friend declaration |
1001 | ||
1002 | template <class T> friend void A<T>::f(); | |
1003 | ||
1004 | the member function below is considered a friend | |
1005 | ||
1006 | template <> struct A<int> { | |
1007 | void f(); | |
1008 | }; | |
1009 | ||
1010 | For this type of template friend, TEMPLATE_DEPTH below will be | |
5c425df5 | 1011 | nonzero. To determine if DECL is a friend of FRIEND, we first |
d43f603d KL |
1012 | check if the enclosing class is a specialization of another. */ |
1013 | ||
1014 | template_depth = template_class_depth (DECL_CONTEXT (friend)); | |
1015 | if (template_depth | |
1016 | && DECL_CLASS_SCOPE_P (decl) | |
c8094d83 | 1017 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
d43f603d KL |
1018 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend)))) |
1019 | { | |
1020 | /* Next, we check the members themselves. In order to handle | |
b939a023 | 1021 | a few tricky cases, such as when FRIEND's are |
d43f603d KL |
1022 | |
1023 | template <class T> friend void A<T>::g(T t); | |
1024 | template <class T> template <T t> friend void A<T>::h(); | |
1025 | ||
b939a023 KL |
1026 | and DECL's are |
1027 | ||
1028 | void A<int>::g(int); | |
1029 | template <int> void A<int>::h(); | |
1030 | ||
1031 | we need to figure out ARGS, the template arguments from | |
1032 | the context of DECL. This is required for template substitution | |
1033 | of `T' in the function parameter of `g' and template parameter | |
1034 | of `h' in the above examples. Here ARGS corresponds to `int'. */ | |
d43f603d KL |
1035 | |
1036 | tree context = DECL_CONTEXT (decl); | |
1037 | tree args = NULL_TREE; | |
1038 | int current_depth = 0; | |
b939a023 | 1039 | |
d43f603d KL |
1040 | while (current_depth < template_depth) |
1041 | { | |
1042 | if (CLASSTYPE_TEMPLATE_INFO (context)) | |
1043 | { | |
1044 | if (current_depth == 0) | |
1045 | args = TYPE_TI_ARGS (context); | |
1046 | else | |
1047 | args = add_to_template_args (TYPE_TI_ARGS (context), args); | |
1048 | current_depth++; | |
1049 | } | |
1050 | context = TYPE_CONTEXT (context); | |
1051 | } | |
1052 | ||
1053 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
1054 | { | |
1055 | bool is_template; | |
1056 | tree friend_type; | |
1057 | tree decl_type; | |
1058 | tree friend_args_type; | |
1059 | tree decl_args_type; | |
1060 | ||
1061 | /* Make sure that both DECL and FRIEND are templates or | |
1062 | non-templates. */ | |
1063 | is_template = DECL_TEMPLATE_INFO (decl) | |
1064 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); | |
1065 | if (need_template ^ is_template) | |
b939a023 | 1066 | return false; |
d43f603d KL |
1067 | else if (is_template) |
1068 | { | |
da1d7781 | 1069 | /* If both are templates, check template parameter list. */ |
d43f603d KL |
1070 | tree friend_parms |
1071 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1072 | args, tf_none); | |
1073 | if (!comp_template_parms | |
1074 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), | |
1075 | friend_parms)) | |
b939a023 | 1076 | return false; |
d43f603d KL |
1077 | |
1078 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); | |
1079 | } | |
1080 | else | |
1081 | decl_type = TREE_TYPE (decl); | |
1082 | ||
1083 | friend_type = tsubst_function_type (TREE_TYPE (friend), args, | |
1084 | tf_none, NULL_TREE); | |
1085 | if (friend_type == error_mark_node) | |
b939a023 | 1086 | return false; |
d43f603d KL |
1087 | |
1088 | /* Check if return types match. */ | |
1089 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) | |
b939a023 | 1090 | return false; |
d43f603d KL |
1091 | |
1092 | /* Check if function parameter types match, ignoring the | |
1093 | `this' parameter. */ | |
1094 | friend_args_type = TYPE_ARG_TYPES (friend_type); | |
1095 | decl_args_type = TYPE_ARG_TYPES (decl_type); | |
1096 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend)) | |
1097 | friend_args_type = TREE_CHAIN (friend_args_type); | |
1098 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
1099 | decl_args_type = TREE_CHAIN (decl_args_type); | |
b939a023 KL |
1100 | |
1101 | return compparms (decl_args_type, friend_args_type); | |
1102 | } | |
1103 | else | |
1104 | { | |
1105 | /* DECL is a TYPE_DECL */ | |
1106 | bool is_template; | |
1107 | tree decl_type = TREE_TYPE (decl); | |
1108 | ||
1109 | /* Make sure that both DECL and FRIEND are templates or | |
1110 | non-templates. */ | |
1111 | is_template | |
1112 | = CLASSTYPE_TEMPLATE_INFO (decl_type) | |
1113 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); | |
1114 | ||
1115 | if (need_template ^ is_template) | |
1116 | return false; | |
1117 | else if (is_template) | |
1118 | { | |
1119 | tree friend_parms; | |
1120 | /* If both are templates, check the name of the two | |
1121 | TEMPLATE_DECL's first because is_friend didn't. */ | |
1122 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) | |
1123 | != DECL_NAME (friend)) | |
1124 | return false; | |
1125 | ||
1126 | /* Now check template parameter list. */ | |
1127 | friend_parms | |
1128 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend), | |
1129 | args, tf_none); | |
1130 | return comp_template_parms | |
1131 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), | |
1132 | friend_parms); | |
1133 | } | |
1134 | else | |
1135 | return (DECL_NAME (decl) | |
1136 | == DECL_NAME (friend)); | |
d43f603d KL |
1137 | } |
1138 | } | |
b939a023 | 1139 | return false; |
d43f603d KL |
1140 | } |
1141 | ||
75650646 | 1142 | /* Register the specialization SPEC as a specialization of TMPL with |
d63d5d0c ILT |
1143 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1144 | is actually just a friend declaration. Returns SPEC, or an | |
1145 | equivalent prior declaration, if available. */ | |
75650646 | 1146 | |
36a117a5 | 1147 | static tree |
d63d5d0c | 1148 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend) |
75650646 | 1149 | { |
c7222c02 | 1150 | tree fn; |
75650646 | 1151 | |
50bc768d | 1152 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
75650646 | 1153 | |
c8094d83 | 1154 | if (TREE_CODE (spec) == FUNCTION_DECL |
36a117a5 MM |
1155 | && uses_template_parms (DECL_TI_ARGS (spec))) |
1156 | /* This is the FUNCTION_DECL for a partial instantiation. Don't | |
1157 | register it; we want the corresponding TEMPLATE_DECL instead. | |
1158 | We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than | |
1159 | the more obvious `uses_template_parms (spec)' to avoid problems | |
1160 | with default function arguments. In particular, given | |
1161 | something like this: | |
1162 | ||
0cbd7506 | 1163 | template <class T> void f(T t1, T t = T()) |
36a117a5 MM |
1164 | |
1165 | the default argument expression is not substituted for in an | |
1166 | instantiation unless and until it is actually needed. */ | |
1167 | return spec; | |
28ba38e3 | 1168 | |
c8094d83 | 1169 | fn = retrieve_specialization (tmpl, args, |
c7222c02 MM |
1170 | /*class_specializations_p=*/false); |
1171 | /* We can sometimes try to re-register a specialization that we've | |
1172 | already got. In particular, regenerate_decl_from_template calls | |
1173 | duplicate_decls which will update the specialization list. But, | |
1174 | we'll still get called again here anyhow. It's more convenient | |
1175 | to simply allow this than to try to prevent it. */ | |
1176 | if (fn == spec) | |
1177 | return spec; | |
1178 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) | |
22e9174f | 1179 | { |
c7222c02 | 1180 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
22e9174f | 1181 | { |
c8094d83 | 1182 | if (TREE_USED (fn) |
c7222c02 | 1183 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
22e9174f | 1184 | { |
c7222c02 MM |
1185 | error ("specialization of %qD after instantiation", |
1186 | fn); | |
99f4234a | 1187 | return error_mark_node; |
22e9174f | 1188 | } |
c7222c02 | 1189 | else |
b1cc95ce | 1190 | { |
a39041fa | 1191 | tree clone; |
c7222c02 MM |
1192 | /* This situation should occur only if the first |
1193 | specialization is an implicit instantiation, the | |
1194 | second is an explicit specialization, and the | |
1195 | implicit instantiation has not yet been used. That | |
1196 | situation can occur if we have implicitly | |
1197 | instantiated a member function and then specialized | |
1198 | it later. | |
c8094d83 | 1199 | |
c7222c02 MM |
1200 | We can also wind up here if a friend declaration that |
1201 | looked like an instantiation turns out to be a | |
1202 | specialization: | |
c8094d83 | 1203 | |
c7222c02 MM |
1204 | template <class T> void foo(T); |
1205 | class S { friend void foo<>(int) }; | |
c8094d83 MS |
1206 | template <> void foo(int); |
1207 | ||
c7222c02 MM |
1208 | We transform the existing DECL in place so that any |
1209 | pointers to it become pointers to the updated | |
1210 | declaration. | |
1211 | ||
1212 | If there was a definition for the template, but not | |
1213 | for the specialization, we want this to look as if | |
1214 | there were no definition, and vice versa. */ | |
1215 | DECL_INITIAL (fn) = NULL_TREE; | |
d63d5d0c | 1216 | duplicate_decls (spec, fn, is_friend); |
a39041fa | 1217 | /* The call to duplicate_decls will have applied |
3db45ab5 | 1218 | [temp.expl.spec]: |
a39041fa | 1219 | |
3db45ab5 | 1220 | An explicit specialization of a function template |
a39041fa | 1221 | is inline only if it is explicitly declared to be, |
bcf51da2 | 1222 | and independently of whether its function template |
a39041fa MM |
1223 | is. |
1224 | ||
1225 | to the primary function; now copy the inline bits to | |
3db45ab5 | 1226 | the various clones. */ |
a39041fa MM |
1227 | FOR_EACH_CLONE (clone, fn) |
1228 | { | |
1229 | DECL_DECLARED_INLINE_P (clone) | |
1230 | = DECL_DECLARED_INLINE_P (fn); | |
1231 | DECL_INLINE (clone) | |
1232 | = DECL_INLINE (fn); | |
1233 | } | |
9c12301f | 1234 | check_specialization_namespace (fn); |
c8094d83 | 1235 | |
b1cc95ce MM |
1236 | return fn; |
1237 | } | |
22e9174f | 1238 | } |
c7222c02 MM |
1239 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1240 | { | |
d63d5d0c | 1241 | if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec)) |
c7222c02 MM |
1242 | /* Dup decl failed, but this is a new definition. Set the |
1243 | line number so any errors match this new | |
1244 | definition. */ | |
1245 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); | |
c8094d83 | 1246 | |
c7222c02 MM |
1247 | return fn; |
1248 | } | |
b1cc95ce MM |
1249 | } |
1250 | ||
1251 | /* A specialization must be declared in the same namespace as the | |
1252 | template it is specializing. */ | |
1253 | if (DECL_TEMPLATE_SPECIALIZATION (spec) | |
1254 | && !check_specialization_namespace (tmpl)) | |
74b0d490 | 1255 | DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl)); |
75650646 | 1256 | |
c7222c02 MM |
1257 | if (!optimize_specialization_lookup_p (tmpl)) |
1258 | DECL_TEMPLATE_SPECIALIZATIONS (tmpl) | |
1259 | = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); | |
36a117a5 MM |
1260 | |
1261 | return spec; | |
1262 | } | |
1263 | ||
1264 | /* Unregister the specialization SPEC as a specialization of TMPL. | |
b3445994 MM |
1265 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1266 | if the SPEC was listed as a specialization of TMPL. */ | |
36a117a5 | 1267 | |
b3445994 MM |
1268 | bool |
1269 | reregister_specialization (tree spec, tree tmpl, tree new_spec) | |
36a117a5 MM |
1270 | { |
1271 | tree* s; | |
1272 | ||
1273 | for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
1274 | *s != NULL_TREE; | |
1275 | s = &TREE_CHAIN (*s)) | |
1276 | if (TREE_VALUE (*s) == spec) | |
1277 | { | |
b3445994 MM |
1278 | if (!new_spec) |
1279 | *s = TREE_CHAIN (*s); | |
1280 | else | |
a3d87771 | 1281 | TREE_VALUE (*s) = new_spec; |
36a117a5 MM |
1282 | return 1; |
1283 | } | |
1284 | ||
1285 | return 0; | |
75650646 MM |
1286 | } |
1287 | ||
a723baf1 MM |
1288 | /* Compare an entry in the local specializations hash table P1 (which |
1289 | is really a pointer to a TREE_LIST) with P2 (which is really a | |
1290 | DECL). */ | |
1291 | ||
1292 | static int | |
1293 | eq_local_specializations (const void *p1, const void *p2) | |
1294 | { | |
1295 | return TREE_VALUE ((tree) p1) == (tree) p2; | |
1296 | } | |
1297 | ||
69f794a7 MM |
1298 | /* Hash P1, an entry in the local specializations table. */ |
1299 | ||
1300 | static hashval_t | |
1301 | hash_local_specialization (const void* p1) | |
1302 | { | |
1303 | return htab_hash_pointer (TREE_VALUE ((tree) p1)); | |
1304 | } | |
1305 | ||
6dfbb909 MM |
1306 | /* Like register_specialization, but for local declarations. We are |
1307 | registering SPEC, an instantiation of TMPL. */ | |
9188c363 | 1308 | |
414ea4aa | 1309 | static void |
3a978d72 | 1310 | register_local_specialization (tree spec, tree tmpl) |
9188c363 | 1311 | { |
6dfbb909 MM |
1312 | void **slot; |
1313 | ||
c8094d83 | 1314 | slot = htab_find_slot_with_hash (local_specializations, tmpl, |
69f794a7 | 1315 | htab_hash_pointer (tmpl), INSERT); |
a723baf1 | 1316 | *slot = build_tree_list (spec, tmpl); |
9188c363 MM |
1317 | } |
1318 | ||
9ba7a2f2 MM |
1319 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
1320 | specialized class. */ | |
1321 | ||
1322 | bool | |
1323 | explicit_class_specialization_p (tree type) | |
1324 | { | |
1325 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) | |
1326 | return false; | |
1327 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); | |
1328 | } | |
1329 | ||
e1467ff2 MM |
1330 | /* Print the list of candidate FNS in an error message. */ |
1331 | ||
104bf76a | 1332 | void |
3a978d72 | 1333 | print_candidates (tree fns) |
e1467ff2 MM |
1334 | { |
1335 | tree fn; | |
1336 | ||
d8e178a0 | 1337 | const char *str = "candidates are:"; |
e1467ff2 MM |
1338 | |
1339 | for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) | |
1340 | { | |
d6479fe7 MM |
1341 | tree f; |
1342 | ||
1343 | for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f)) | |
dee15844 | 1344 | error ("%s %+#D", str, OVL_CURRENT (f)); |
e1467ff2 MM |
1345 | str = " "; |
1346 | } | |
1347 | } | |
1348 | ||
75650646 | 1349 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
e1467ff2 | 1350 | which can be specialized to match the indicated DECL with the |
03017874 MM |
1351 | explicit template args given in TEMPLATE_ID. The DECL may be |
1352 | NULL_TREE if none is available. In that case, the functions in | |
1353 | TEMPLATE_ID are non-members. | |
1354 | ||
838dfd8a | 1355 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
03017874 MM |
1356 | specialization of a member template. |
1357 | ||
5fe7b654 GB |
1358 | The TEMPLATE_COUNT is the number of references to qualifying |
1359 | template classes that appeared in the name of the function. See | |
1360 | check_explicit_specialization for a more accurate description. | |
1361 | ||
29a1da1c MM |
1362 | TSK indicates what kind of template declaration (if any) is being |
1363 | declared. TSK_TEMPLATE indicates that the declaration given by | |
1364 | DECL, though a FUNCTION_DECL, has template parameters, and is | |
1365 | therefore a template function. | |
1366 | ||
03017874 MM |
1367 | The template args (those explicitly specified and those deduced) |
1368 | are output in a newly created vector *TARGS_OUT. | |
1369 | ||
1370 | If it is impossible to determine the result, an error message is | |
bf8f3f93 | 1371 | issued. The error_mark_node is returned to indicate failure. */ |
75650646 | 1372 | |
e9659ab0 | 1373 | static tree |
c8094d83 | 1374 | determine_specialization (tree template_id, |
0cbd7506 MS |
1375 | tree decl, |
1376 | tree* targs_out, | |
5fe7b654 | 1377 | int need_member_template, |
29a1da1c MM |
1378 | int template_count, |
1379 | tmpl_spec_kind tsk) | |
386b8a85 | 1380 | { |
03017874 MM |
1381 | tree fns; |
1382 | tree targs; | |
1383 | tree explicit_targs; | |
1384 | tree candidates = NULL_TREE; | |
7ca383e6 MM |
1385 | /* A TREE_LIST of templates of which DECL may be a specialization. |
1386 | The TREE_VALUE of each node is a TEMPLATE_DECL. The | |
1387 | corresponding TREE_PURPOSE is the set of template arguments that, | |
1388 | when used to instantiate the template, would produce a function | |
1389 | with the signature of DECL. */ | |
03017874 | 1390 | tree templates = NULL_TREE; |
5fe7b654 GB |
1391 | int header_count; |
1392 | struct cp_binding_level *b; | |
386b8a85 | 1393 | |
e1467ff2 MM |
1394 | *targs_out = NULL_TREE; |
1395 | ||
728da672 | 1396 | if (template_id == error_mark_node || decl == error_mark_node) |
f2e48b67 BK |
1397 | return error_mark_node; |
1398 | ||
1399 | fns = TREE_OPERAND (template_id, 0); | |
03017874 | 1400 | explicit_targs = TREE_OPERAND (template_id, 1); |
f2e48b67 | 1401 | |
27fafc8d JM |
1402 | if (fns == error_mark_node) |
1403 | return error_mark_node; | |
1404 | ||
c6002625 | 1405 | /* Check for baselinks. */ |
91e490ab | 1406 | if (BASELINK_P (fns)) |
da15dae6 | 1407 | fns = BASELINK_FUNCTIONS (fns); |
386b8a85 | 1408 | |
91e490ab MM |
1409 | if (!is_overloaded_fn (fns)) |
1410 | { | |
0f51ccfc | 1411 | error ("%qD is not a function template", fns); |
91e490ab MM |
1412 | return error_mark_node; |
1413 | } | |
1414 | ||
5fe7b654 GB |
1415 | /* Count the number of template headers specified for this |
1416 | specialization. */ | |
1417 | header_count = 0; | |
c8094d83 | 1418 | for (b = current_binding_level; |
98f99d7f | 1419 | b->kind == sk_template_parms; |
5fe7b654 GB |
1420 | b = b->level_chain) |
1421 | ++header_count; | |
1422 | ||
2c73f9f5 | 1423 | for (; fns; fns = OVL_NEXT (fns)) |
386b8a85 | 1424 | { |
9dfce8fd | 1425 | tree fn = OVL_CURRENT (fns); |
03017874 MM |
1426 | |
1427 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
d955f6ea KL |
1428 | { |
1429 | tree decl_arg_types; | |
0da99d4e | 1430 | tree fn_arg_types; |
d955f6ea KL |
1431 | |
1432 | /* DECL might be a specialization of FN. */ | |
1433 | ||
1434 | /* Adjust the type of DECL in case FN is a static member. */ | |
1435 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
c8094d83 | 1436 | if (DECL_STATIC_FUNCTION_P (fn) |
d955f6ea KL |
1437 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1438 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1439 | ||
1440 | /* Check that the number of function parameters matches. | |
1441 | For example, | |
1442 | template <class T> void f(int i = 0); | |
1443 | template <> void f<int>(); | |
0e339752 | 1444 | The specialization f<int> is invalid but is not caught |
d955f6ea KL |
1445 | by get_bindings below. */ |
1446 | ||
0da99d4e GB |
1447 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
1448 | if (list_length (fn_arg_types) != list_length (decl_arg_types)) | |
1449 | continue; | |
1450 | ||
1451 | /* For a non-static member function, we need to make sure that | |
1452 | the const qualification is the same. This can be done by | |
1453 | checking the 'this' in the argument list. */ | |
1454 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) | |
c8094d83 | 1455 | && !same_type_p (TREE_VALUE (fn_arg_types), |
0da99d4e | 1456 | TREE_VALUE (decl_arg_types))) |
d955f6ea KL |
1457 | continue; |
1458 | ||
5fe7b654 GB |
1459 | /* In case of explicit specialization, we need to check if |
1460 | the number of template headers appearing in the specialization | |
1461 | is correct. This is usually done in check_explicit_specialization, | |
1462 | but the check done there cannot be exhaustive when specializing | |
1463 | member functions. Consider the following code: | |
1464 | ||
1465 | template <> void A<int>::f(int); | |
1466 | template <> template <> void A<int>::f(int); | |
1467 | ||
1468 | Assuming that A<int> is not itself an explicit specialization | |
1469 | already, the first line specializes "f" which is a non-template | |
1470 | member function, whilst the second line specializes "f" which | |
1471 | is a template member function. So both lines are syntactically | |
1472 | correct, and check_explicit_specialization does not reject | |
1473 | them. | |
c8094d83 | 1474 | |
5fe7b654 GB |
1475 | Here, we can do better, as we are matching the specialization |
1476 | against the declarations. We count the number of template | |
1477 | headers, and we check if they match TEMPLATE_COUNT + 1 | |
1478 | (TEMPLATE_COUNT is the number of qualifying template classes, | |
1479 | plus there must be another header for the member template | |
1480 | itself). | |
c8094d83 | 1481 | |
5fe7b654 GB |
1482 | Notice that if header_count is zero, this is not a |
1483 | specialization but rather a template instantiation, so there | |
1484 | is no check we can perform here. */ | |
1485 | if (header_count && header_count != template_count + 1) | |
1486 | continue; | |
1487 | ||
98f99d7f MM |
1488 | /* Check that the number of template arguments at the |
1489 | innermost level for DECL is the same as for FN. */ | |
1490 | if (current_binding_level->kind == sk_template_parms | |
1491 | && !current_binding_level->explicit_spec_p | |
1492 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) | |
3db45ab5 | 1493 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
916b63c3 | 1494 | (current_template_parms)))) |
98f99d7f | 1495 | continue; |
c8094d83 | 1496 | |
29a1da1c MM |
1497 | /* Function templates cannot be specializations; there are |
1498 | no partial specializations of functions. Therefore, if | |
1499 | the type of DECL does not match FN, there is no | |
1500 | match. */ | |
1501 | if (tsk == tsk_template) | |
1502 | { | |
1503 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1504 | decl_arg_types)) | |
1505 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
1506 | continue; | |
1507 | } | |
1508 | ||
d955f6ea KL |
1509 | /* See whether this function might be a specialization of this |
1510 | template. */ | |
a34d3336 | 1511 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
d955f6ea KL |
1512 | |
1513 | if (!targs) | |
1514 | /* We cannot deduce template arguments that when used to | |
1515 | specialize TMPL will produce DECL. */ | |
1516 | continue; | |
1517 | ||
1518 | /* Save this template, and the arguments deduced. */ | |
1519 | templates = tree_cons (targs, fn, templates); | |
1520 | } | |
03017874 MM |
1521 | else if (need_member_template) |
1522 | /* FN is an ordinary member function, and we need a | |
1523 | specialization of a member template. */ | |
d955f6ea | 1524 | ; |
03017874 MM |
1525 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
1526 | /* We can get IDENTIFIER_NODEs here in certain erroneous | |
1527 | cases. */ | |
d955f6ea | 1528 | ; |
03017874 MM |
1529 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
1530 | /* This is just an ordinary non-member function. Nothing can | |
1531 | be a specialization of that. */ | |
d955f6ea | 1532 | ; |
3b82c249 KL |
1533 | else if (DECL_ARTIFICIAL (fn)) |
1534 | /* Cannot specialize functions that are created implicitly. */ | |
d955f6ea | 1535 | ; |
75650646 | 1536 | else |
03017874 MM |
1537 | { |
1538 | tree decl_arg_types; | |
386b8a85 | 1539 | |
03017874 MM |
1540 | /* This is an ordinary member function. However, since |
1541 | we're here, we can assume it's enclosing class is a | |
1542 | template class. For example, | |
c8094d83 | 1543 | |
03017874 MM |
1544 | template <typename T> struct S { void f(); }; |
1545 | template <> void S<int>::f() {} | |
1546 | ||
1547 | Here, S<int>::f is a non-template, but S<int> is a | |
1548 | template class. If FN has the same type as DECL, we | |
1549 | might be in business. */ | |
f5d47abd KL |
1550 | |
1551 | if (!DECL_TEMPLATE_INFO (fn)) | |
1552 | /* Its enclosing class is an explicit specialization | |
1553 | of a template class. This is not a candidate. */ | |
1554 | continue; | |
1555 | ||
03017874 MM |
1556 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
1557 | TREE_TYPE (TREE_TYPE (fn)))) | |
1558 | /* The return types differ. */ | |
1559 | continue; | |
1560 | ||
1561 | /* Adjust the type of DECL in case FN is a static member. */ | |
1562 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
c8094d83 | 1563 | if (DECL_STATIC_FUNCTION_P (fn) |
03017874 MM |
1564 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1565 | decl_arg_types = TREE_CHAIN (decl_arg_types); | |
1566 | ||
c8094d83 | 1567 | if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
03017874 MM |
1568 | decl_arg_types)) |
1569 | /* They match! */ | |
1570 | candidates = tree_cons (NULL_TREE, fn, candidates); | |
03017874 | 1571 | } |
386b8a85 | 1572 | } |
03017874 | 1573 | |
bf8f3f93 | 1574 | if (templates && TREE_CHAIN (templates)) |
386b8a85 | 1575 | { |
03017874 | 1576 | /* We have: |
c8094d83 | 1577 | |
03017874 MM |
1578 | [temp.expl.spec] |
1579 | ||
1580 | It is possible for a specialization with a given function | |
1581 | signature to be instantiated from more than one function | |
1582 | template. In such cases, explicit specification of the | |
1583 | template arguments must be used to uniquely identify the | |
1584 | function template specialization being specialized. | |
1585 | ||
1586 | Note that here, there's no suggestion that we're supposed to | |
1587 | determine which of the candidate templates is most | |
1588 | specialized. However, we, also have: | |
1589 | ||
1590 | [temp.func.order] | |
1591 | ||
1592 | Partial ordering of overloaded function template | |
1593 | declarations is used in the following contexts to select | |
1594 | the function template to which a function template | |
c8094d83 | 1595 | specialization refers: |
03017874 | 1596 | |
0cbd7506 | 1597 | -- when an explicit specialization refers to a function |
c8094d83 | 1598 | template. |
03017874 MM |
1599 | |
1600 | So, we do use the partial ordering rules, at least for now. | |
0e339752 | 1601 | This extension can only serve to make invalid programs valid, |
03017874 MM |
1602 | so it's safe. And, there is strong anecdotal evidence that |
1603 | the committee intended the partial ordering rules to apply; | |
1604 | the EDG front-end has that behavior, and John Spicer claims | |
1605 | that the committee simply forgot to delete the wording in | |
1606 | [temp.expl.spec]. */ | |
7ca383e6 MM |
1607 | tree tmpl = most_specialized_instantiation (templates); |
1608 | if (tmpl != error_mark_node) | |
1609 | { | |
1610 | templates = tmpl; | |
1611 | TREE_CHAIN (templates) = NULL_TREE; | |
1612 | } | |
e1467ff2 MM |
1613 | } |
1614 | ||
03017874 | 1615 | if (templates == NULL_TREE && candidates == NULL_TREE) |
e1467ff2 | 1616 | { |
dee15844 JM |
1617 | error ("template-id %qD for %q+D does not match any template " |
1618 | "declaration", template_id, decl); | |
03017874 | 1619 | return error_mark_node; |
386b8a85 | 1620 | } |
03017874 | 1621 | else if ((templates && TREE_CHAIN (templates)) |
bf8f3f93 MM |
1622 | || (candidates && TREE_CHAIN (candidates)) |
1623 | || (templates && candidates)) | |
386b8a85 | 1624 | { |
dee15844 JM |
1625 | error ("ambiguous template specialization %qD for %q+D", |
1626 | template_id, decl); | |
bf8f3f93 MM |
1627 | chainon (candidates, templates); |
1628 | print_candidates (candidates); | |
03017874 | 1629 | return error_mark_node; |
386b8a85 JM |
1630 | } |
1631 | ||
c6002625 | 1632 | /* We have one, and exactly one, match. */ |
03017874 MM |
1633 | if (candidates) |
1634 | { | |
29a1da1c MM |
1635 | tree fn = TREE_VALUE (candidates); |
1636 | /* DECL is a re-declaration of a template function. */ | |
1637 | if (TREE_CODE (fn) == TEMPLATE_DECL) | |
1638 | return fn; | |
03017874 MM |
1639 | /* It was a specialization of an ordinary member function in a |
1640 | template class. */ | |
29a1da1c MM |
1641 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
1642 | return DECL_TI_TEMPLATE (fn); | |
03017874 MM |
1643 | } |
1644 | ||
1645 | /* It was a specialization of a template. */ | |
17aec3eb | 1646 | targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates))); |
03017874 MM |
1647 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) |
1648 | { | |
1649 | *targs_out = copy_node (targs); | |
c8094d83 | 1650 | SET_TMPL_ARGS_LEVEL (*targs_out, |
03017874 MM |
1651 | TMPL_ARGS_DEPTH (*targs_out), |
1652 | TREE_PURPOSE (templates)); | |
1653 | } | |
1654 | else | |
1655 | *targs_out = TREE_PURPOSE (templates); | |
e1467ff2 | 1656 | return TREE_VALUE (templates); |
8d08fdba | 1657 | } |
8afa707f MM |
1658 | |
1659 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, | |
1660 | but with the default argument values filled in from those in the | |
1661 | TMPL_TYPES. */ | |
c8094d83 | 1662 | |
8afa707f | 1663 | static tree |
3a978d72 NN |
1664 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
1665 | tree tmpl_types) | |
8afa707f MM |
1666 | { |
1667 | tree new_spec_types; | |
1668 | ||
1669 | if (!spec_types) | |
1670 | return NULL_TREE; | |
1671 | ||
1672 | if (spec_types == void_list_node) | |
1673 | return void_list_node; | |
1674 | ||
1675 | /* Substitute into the rest of the list. */ | |
1676 | new_spec_types = | |
1677 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), | |
1678 | TREE_CHAIN (tmpl_types)); | |
c8094d83 | 1679 | |
8afa707f MM |
1680 | /* Add the default argument for this parameter. */ |
1681 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), | |
1682 | TREE_VALUE (spec_types), | |
1683 | new_spec_types); | |
1684 | } | |
1685 | ||
1686 | /* DECL is an explicit specialization. Replicate default arguments | |
1687 | from the template it specializes. (That way, code like: | |
1688 | ||
1689 | template <class T> void f(T = 3); | |
1690 | template <> void f(double); | |
c8094d83 | 1691 | void g () { f (); } |
8afa707f MM |
1692 | |
1693 | works, as required.) An alternative approach would be to look up | |
1694 | the correct default arguments at the call-site, but this approach | |
1695 | is consistent with how implicit instantiations are handled. */ | |
1696 | ||
1697 | static void | |
3a978d72 | 1698 | copy_default_args_to_explicit_spec (tree decl) |
8afa707f MM |
1699 | { |
1700 | tree tmpl; | |
1701 | tree spec_types; | |
1702 | tree tmpl_types; | |
1703 | tree new_spec_types; | |
1704 | tree old_type; | |
1705 | tree new_type; | |
1706 | tree t; | |
08c2df0f NS |
1707 | tree object_type = NULL_TREE; |
1708 | tree in_charge = NULL_TREE; | |
e0fff4b3 | 1709 | tree vtt = NULL_TREE; |
8afa707f MM |
1710 | |
1711 | /* See if there's anything we need to do. */ | |
1712 | tmpl = DECL_TI_TEMPLATE (decl); | |
1713 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); | |
1714 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) | |
1715 | if (TREE_PURPOSE (t)) | |
1716 | break; | |
1717 | if (!t) | |
1718 | return; | |
1719 | ||
1720 | old_type = TREE_TYPE (decl); | |
1721 | spec_types = TYPE_ARG_TYPES (old_type); | |
c8094d83 | 1722 | |
8afa707f MM |
1723 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) |
1724 | { | |
08c2df0f | 1725 | /* Remove the this pointer, but remember the object's type for |
0cbd7506 | 1726 | CV quals. */ |
08c2df0f | 1727 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
8afa707f MM |
1728 | spec_types = TREE_CHAIN (spec_types); |
1729 | tmpl_types = TREE_CHAIN (tmpl_types); | |
c8094d83 | 1730 | |
8afa707f | 1731 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
0cbd7506 MS |
1732 | { |
1733 | /* DECL may contain more parameters than TMPL due to the extra | |
1734 | in-charge parameter in constructors and destructors. */ | |
1735 | in_charge = spec_types; | |
08c2df0f NS |
1736 | spec_types = TREE_CHAIN (spec_types); |
1737 | } | |
e0fff4b3 JM |
1738 | if (DECL_HAS_VTT_PARM_P (decl)) |
1739 | { | |
1740 | vtt = spec_types; | |
1741 | spec_types = TREE_CHAIN (spec_types); | |
1742 | } | |
8afa707f MM |
1743 | } |
1744 | ||
1745 | /* Compute the merged default arguments. */ | |
c8094d83 | 1746 | new_spec_types = |
8afa707f MM |
1747 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
1748 | ||
08c2df0f NS |
1749 | /* Compute the new FUNCTION_TYPE. */ |
1750 | if (object_type) | |
8afa707f | 1751 | { |
e0fff4b3 | 1752 | if (vtt) |
0cbd7506 MS |
1753 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
1754 | TREE_VALUE (vtt), | |
1755 | new_spec_types); | |
e0fff4b3 | 1756 | |
08c2df0f | 1757 | if (in_charge) |
0cbd7506 MS |
1758 | /* Put the in-charge parameter back. */ |
1759 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), | |
1760 | TREE_VALUE (in_charge), | |
1761 | new_spec_types); | |
08c2df0f | 1762 | |
43dc123f MM |
1763 | new_type = build_method_type_directly (object_type, |
1764 | TREE_TYPE (old_type), | |
1765 | new_spec_types); | |
8afa707f | 1766 | } |
8afa707f MM |
1767 | else |
1768 | new_type = build_function_type (TREE_TYPE (old_type), | |
08c2df0f | 1769 | new_spec_types); |
e9525111 MM |
1770 | new_type = cp_build_type_attribute_variant (new_type, |
1771 | TYPE_ATTRIBUTES (old_type)); | |
8afa707f MM |
1772 | new_type = build_exception_variant (new_type, |
1773 | TYPE_RAISES_EXCEPTIONS (old_type)); | |
1774 | TREE_TYPE (decl) = new_type; | |
1775 | } | |
1776 | ||
386b8a85 | 1777 | /* Check to see if the function just declared, as indicated in |
75650646 MM |
1778 | DECLARATOR, and in DECL, is a specialization of a function |
1779 | template. We may also discover that the declaration is an explicit | |
1780 | instantiation at this point. | |
1781 | ||
e1467ff2 | 1782 | Returns DECL, or an equivalent declaration that should be used |
03017874 MM |
1783 | instead if all goes well. Issues an error message if something is |
1784 | amiss. Returns error_mark_node if the error is not easily | |
1785 | recoverable. | |
c8094d83 MS |
1786 | |
1787 | FLAGS is a bitmask consisting of the following flags: | |
75650646 | 1788 | |
75650646 MM |
1789 | 2: The function has a definition. |
1790 | 4: The function is a friend. | |
75650646 MM |
1791 | |
1792 | The TEMPLATE_COUNT is the number of references to qualifying | |
1793 | template classes that appeared in the name of the function. For | |
1794 | example, in | |
1795 | ||
1796 | template <class T> struct S { void f(); }; | |
1797 | void S<int>::f(); | |
c8094d83 | 1798 | |
75650646 MM |
1799 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
1800 | classes are not counted in the TEMPLATE_COUNT, so that in | |
1801 | ||
1802 | template <class T> struct S {}; | |
1803 | template <> struct S<int> { void f(); } | |
36a117a5 | 1804 | template <> void S<int>::f(); |
75650646 MM |
1805 | |
1806 | the TEMPLATE_COUNT would be 0. (Note that this declaration is | |
0e339752 | 1807 | invalid; there should be no template <>.) |
75650646 MM |
1808 | |
1809 | If the function is a specialization, it is marked as such via | |
1810 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO | |
c8094d83 | 1811 | is set up correctly, and it is added to the list of specializations |
75650646 | 1812 | for that template. */ |
386b8a85 | 1813 | |
e1467ff2 | 1814 | tree |
c8094d83 | 1815 | check_explicit_specialization (tree declarator, |
0cbd7506 MS |
1816 | tree decl, |
1817 | int template_count, | |
1818 | int flags) | |
386b8a85 | 1819 | { |
75650646 MM |
1820 | int have_def = flags & 2; |
1821 | int is_friend = flags & 4; | |
1822 | int specialization = 0; | |
e1467ff2 | 1823 | int explicit_instantiation = 0; |
fd4de5ff | 1824 | int member_specialization = 0; |
75650646 MM |
1825 | tree ctype = DECL_CLASS_CONTEXT (decl); |
1826 | tree dname = DECL_NAME (decl); | |
74b846e0 | 1827 | tmpl_spec_kind tsk; |
386b8a85 | 1828 | |
f65b7de3 GB |
1829 | if (is_friend) |
1830 | { | |
1831 | if (!processing_specialization) | |
1832 | tsk = tsk_none; | |
1833 | else | |
1834 | tsk = tsk_excessive_parms; | |
1835 | } | |
1836 | else | |
1837 | tsk = current_tmpl_spec_kind (template_count); | |
75650646 | 1838 | |
74b846e0 MM |
1839 | switch (tsk) |
1840 | { | |
1841 | case tsk_none: | |
c8094d83 | 1842 | if (processing_specialization) |
75650646 | 1843 | { |
fd4de5ff MM |
1844 | specialization = 1; |
1845 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
75650646 | 1846 | } |
74b846e0 | 1847 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
386b8a85 | 1848 | { |
74b846e0 MM |
1849 | if (is_friend) |
1850 | /* This could be something like: | |
75650646 | 1851 | |
74b846e0 MM |
1852 | template <class T> void f(T); |
1853 | class S { friend void f<>(int); } */ | |
1854 | specialization = 1; | |
1855 | else | |
1856 | { | |
1857 | /* This case handles bogus declarations like template <> | |
1858 | template <class T> void f<int>(); */ | |
1859 | ||
0f51ccfc | 1860 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1861 | declarator); |
74b846e0 MM |
1862 | return decl; |
1863 | } | |
1864 | } | |
1865 | break; | |
1866 | ||
1867 | case tsk_invalid_member_spec: | |
1868 | /* The error has already been reported in | |
1869 | check_specialization_scope. */ | |
1870 | return error_mark_node; | |
1871 | ||
1872 | case tsk_invalid_expl_inst: | |
33bd39a2 | 1873 | error ("template parameter list used in explicit instantiation"); |
74b846e0 MM |
1874 | |
1875 | /* Fall through. */ | |
1876 | ||
1877 | case tsk_expl_inst: | |
fd4de5ff | 1878 | if (have_def) |
33bd39a2 | 1879 | error ("definition provided for explicit instantiation"); |
c8094d83 | 1880 | |
fd4de5ff | 1881 | explicit_instantiation = 1; |
74b846e0 | 1882 | break; |
fd4de5ff | 1883 | |
74b846e0 | 1884 | case tsk_excessive_parms: |
f03adc6b GB |
1885 | case tsk_insufficient_parms: |
1886 | if (tsk == tsk_excessive_parms) | |
0cbd7506 | 1887 | error ("too many template parameter lists in declaration of %qD", |
f03adc6b GB |
1888 | decl); |
1889 | else if (template_header_count) | |
0f51ccfc | 1890 | error("too few template parameter lists in declaration of %qD", decl); |
f03adc6b | 1891 | else |
0f51ccfc | 1892 | error("explicit specialization of %qD must be introduced by " |
9e637a26 | 1893 | "%<template <>%>", decl); |
75650646 | 1894 | |
74b846e0 MM |
1895 | /* Fall through. */ |
1896 | case tsk_expl_spec: | |
1897 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
1898 | if (ctype) | |
1899 | member_specialization = 1; | |
1900 | else | |
1901 | specialization = 1; | |
1902 | break; | |
74b846e0 MM |
1903 | |
1904 | case tsk_template: | |
1905 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
c6f2ed0d | 1906 | { |
fd4de5ff MM |
1907 | /* This case handles bogus declarations like template <> |
1908 | template <class T> void f<int>(); */ | |
75650646 | 1909 | |
655dc6ee | 1910 | if (uses_template_parms (declarator)) |
0f51ccfc | 1911 | error ("function template partial specialization %qD " |
e1e93ad8 | 1912 | "is not allowed", declarator); |
655dc6ee | 1913 | else |
0f51ccfc | 1914 | error ("template-id %qD in declaration of primary template", |
0cbd7506 | 1915 | declarator); |
fd4de5ff | 1916 | return decl; |
386b8a85 | 1917 | } |
74b846e0 MM |
1918 | |
1919 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) | |
1920 | /* This is a specialization of a member template, without | |
1921 | specialization the containing class. Something like: | |
1922 | ||
1923 | template <class T> struct S { | |
c8094d83 | 1924 | template <class U> void f (U); |
0cbd7506 | 1925 | }; |
74b846e0 | 1926 | template <> template <class U> void S<int>::f(U) {} |
c8094d83 | 1927 | |
74b846e0 MM |
1928 | That's a specialization -- but of the entire template. */ |
1929 | specialization = 1; | |
1930 | break; | |
1931 | ||
1932 | default: | |
315fb5db | 1933 | gcc_unreachable (); |
75650646 | 1934 | } |
386b8a85 | 1935 | |
670960ac JM |
1936 | if (specialization || member_specialization) |
1937 | { | |
1938 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
1939 | for (; t; t = TREE_CHAIN (t)) | |
1940 | if (TREE_PURPOSE (t)) | |
1941 | { | |
33bd39a2 | 1942 | pedwarn |
8251199e | 1943 | ("default argument specified in explicit specialization"); |
670960ac JM |
1944 | break; |
1945 | } | |
1946 | } | |
1947 | ||
e1467ff2 | 1948 | if (specialization || member_specialization || explicit_instantiation) |
75650646 MM |
1949 | { |
1950 | tree tmpl = NULL_TREE; | |
1951 | tree targs = NULL_TREE; | |
75650646 MM |
1952 | |
1953 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
1954 | if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
1955 | { | |
1956 | tree fns; | |
1957 | ||
50bc768d | 1958 | gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE); |
2f54a1db | 1959 | if (ctype) |
386b8a85 | 1960 | fns = dname; |
2f54a1db GB |
1961 | else |
1962 | { | |
1963 | /* If there is no class context, the explicit instantiation | |
0cbd7506 | 1964 | must be at namespace scope. */ |
50bc768d | 1965 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
2f54a1db GB |
1966 | |
1967 | /* Find the namespace binding, using the declaration | |
0cbd7506 | 1968 | context. */ |
ddf74938 | 1969 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname, |
664a90c0 | 1970 | false, true); |
4230cec2 NS |
1971 | if (!fns || !is_overloaded_fn (fns)) |
1972 | { | |
1973 | error ("%qD is not a template function", dname); | |
1974 | fns = error_mark_node; | |
1975 | } | |
664a90c0 JM |
1976 | else |
1977 | { | |
1978 | tree fn = OVL_CURRENT (fns); | |
ddf74938 | 1979 | if (!is_associated_namespace (CP_DECL_CONTEXT (decl), |
08d295c5 JM |
1980 | CP_DECL_CONTEXT (fn))) |
1981 | error ("%qD is not declared in %qD", | |
1982 | decl, current_namespace); | |
664a90c0 | 1983 | } |
2f54a1db | 1984 | } |
386b8a85 | 1985 | |
2f54a1db | 1986 | declarator = lookup_template_function (fns, NULL_TREE); |
386b8a85 JM |
1987 | } |
1988 | ||
f2e48b67 BK |
1989 | if (declarator == error_mark_node) |
1990 | return error_mark_node; | |
1991 | ||
75650646 MM |
1992 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
1993 | { | |
8ec2ac34 | 1994 | if (!explicit_instantiation) |
0e339752 | 1995 | /* A specialization in class scope. This is invalid, |
6c30752f MM |
1996 | but the error will already have been flagged by |
1997 | check_specialization_scope. */ | |
1998 | return error_mark_node; | |
8ec2ac34 | 1999 | else |
b370501f | 2000 | { |
0e339752 | 2001 | /* It's not valid to write an explicit instantiation in |
b370501f | 2002 | class scope, e.g.: |
8ec2ac34 | 2003 | |
0cbd7506 | 2004 | class C { template void f(); } |
8ec2ac34 | 2005 | |
b370501f KG |
2006 | This case is caught by the parser. However, on |
2007 | something like: | |
c8094d83 | 2008 | |
b370501f | 2009 | template class C { void f(); }; |
8ec2ac34 | 2010 | |
0e339752 | 2011 | (which is invalid) we can get here. The error will be |
b370501f KG |
2012 | issued later. */ |
2013 | ; | |
2014 | } | |
8ec2ac34 | 2015 | |
e1467ff2 | 2016 | return decl; |
75650646 | 2017 | } |
c8094d83 | 2018 | else if (ctype != NULL_TREE |
75650646 MM |
2019 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) == |
2020 | IDENTIFIER_NODE)) | |
386b8a85 | 2021 | { |
75650646 MM |
2022 | /* Find the list of functions in ctype that have the same |
2023 | name as the declared function. */ | |
2024 | tree name = TREE_OPERAND (declarator, 0); | |
03017874 MM |
2025 | tree fns = NULL_TREE; |
2026 | int idx; | |
2027 | ||
8ba658ee | 2028 | if (constructor_name_p (name, ctype)) |
386b8a85 | 2029 | { |
75650646 | 2030 | int is_constructor = DECL_CONSTRUCTOR_P (decl); |
c8094d83 | 2031 | |
75650646 | 2032 | if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype) |
9f4faeae | 2033 | : !CLASSTYPE_DESTRUCTORS (ctype)) |
75650646 MM |
2034 | { |
2035 | /* From [temp.expl.spec]: | |
c8094d83 | 2036 | |
75650646 MM |
2037 | If such an explicit specialization for the member |
2038 | of a class template names an implicitly-declared | |
2039 | special member function (clause _special_), the | |
c8094d83 | 2040 | program is ill-formed. |
e1467ff2 MM |
2041 | |
2042 | Similar language is found in [temp.explicit]. */ | |
33bd39a2 | 2043 | error ("specialization of implicitly-declared special member function"); |
03017874 | 2044 | return error_mark_node; |
75650646 | 2045 | } |
386b8a85 | 2046 | |
42da2fd8 | 2047 | name = is_constructor ? ctor_identifier : dtor_identifier; |
75650646 | 2048 | } |
42da2fd8 | 2049 | |
421844e7 | 2050 | if (!DECL_CONV_FN_P (decl)) |
03017874 MM |
2051 | { |
2052 | idx = lookup_fnfields_1 (ctype, name); | |
2053 | if (idx >= 0) | |
aaaa46d2 | 2054 | fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx); |
03017874 MM |
2055 | } |
2056 | else | |
2057 | { | |
d4e6fecb | 2058 | VEC(tree,gc) *methods; |
aaaa46d2 | 2059 | tree ovl; |
03017874 MM |
2060 | |
2061 | /* For a type-conversion operator, we cannot do a | |
2062 | name-based lookup. We might be looking for `operator | |
2063 | int' which will be a specialization of `operator T'. | |
2064 | So, we find *all* the conversion operators, and then | |
2065 | select from them. */ | |
2066 | fns = NULL_TREE; | |
2067 | ||
2068 | methods = CLASSTYPE_METHOD_VEC (ctype); | |
2069 | if (methods) | |
5dd236e2 | 2070 | for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT; |
9ba5ff0f | 2071 | VEC_iterate (tree, methods, idx, ovl); |
aaaa46d2 | 2072 | ++idx) |
03017874 | 2073 | { |
aaaa46d2 | 2074 | if (!DECL_CONV_FN_P (OVL_CURRENT (ovl))) |
03017874 MM |
2075 | /* There are no more conversion functions. */ |
2076 | break; | |
2077 | ||
2078 | /* Glue all these conversion functions together | |
2079 | with those we already have. */ | |
2080 | for (; ovl; ovl = OVL_NEXT (ovl)) | |
2081 | fns = ovl_cons (OVL_CURRENT (ovl), fns); | |
2082 | } | |
2083 | } | |
c8094d83 MS |
2084 | |
2085 | if (fns == NULL_TREE) | |
386b8a85 | 2086 | { |
0f51ccfc | 2087 | error ("no member function %qD declared in %qT", name, ctype); |
03017874 | 2088 | return error_mark_node; |
386b8a85 JM |
2089 | } |
2090 | else | |
2091 | TREE_OPERAND (declarator, 0) = fns; | |
2092 | } | |
c8094d83 | 2093 | |
e1467ff2 MM |
2094 | /* Figure out what exactly is being specialized at this point. |
2095 | Note that for an explicit instantiation, even one for a | |
38e01259 | 2096 | member function, we cannot tell apriori whether the |
e1467ff2 | 2097 | instantiation is for a member template, or just a member |
36a117a5 MM |
2098 | function of a template class. Even if a member template is |
2099 | being instantiated, the member template arguments may be | |
2100 | elided if they can be deduced from the rest of the | |
2101 | declaration. */ | |
e1467ff2 | 2102 | tmpl = determine_specialization (declarator, decl, |
c8094d83 | 2103 | &targs, |
5fe7b654 | 2104 | member_specialization, |
29a1da1c MM |
2105 | template_count, |
2106 | tsk); | |
c8094d83 | 2107 | |
03017874 MM |
2108 | if (!tmpl || tmpl == error_mark_node) |
2109 | /* We couldn't figure out what this declaration was | |
2110 | specializing. */ | |
2111 | return error_mark_node; | |
2112 | else | |
386b8a85 | 2113 | { |
25aab5d0 | 2114 | tree gen_tmpl = most_general_template (tmpl); |
36a117a5 | 2115 | |
e1467ff2 MM |
2116 | if (explicit_instantiation) |
2117 | { | |
03d0f4af | 2118 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
c8094d83 | 2119 | is done by do_decl_instantiation later. */ |
25aab5d0 MM |
2120 | |
2121 | int arg_depth = TMPL_ARGS_DEPTH (targs); | |
2122 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); | |
2123 | ||
2124 | if (arg_depth > parm_depth) | |
2125 | { | |
2126 | /* If TMPL is not the most general template (for | |
2127 | example, if TMPL is a friend template that is | |
2128 | injected into namespace scope), then there will | |
dc957d14 | 2129 | be too many levels of TARGS. Remove some of them |
25aab5d0 MM |
2130 | here. */ |
2131 | int i; | |
2132 | tree new_targs; | |
2133 | ||
f31c0a32 | 2134 | new_targs = make_tree_vec (parm_depth); |
25aab5d0 MM |
2135 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
2136 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) | |
2137 | = TREE_VEC_ELT (targs, i); | |
2138 | targs = new_targs; | |
2139 | } | |
c8094d83 | 2140 | |
3e4a3562 | 2141 | return instantiate_template (tmpl, targs, tf_error); |
e1467ff2 | 2142 | } |
74b846e0 | 2143 | |
6c07f448 KL |
2144 | /* If we thought that the DECL was a member function, but it |
2145 | turns out to be specializing a static member function, | |
4546865e | 2146 | make DECL a static member function as well. */ |
6c07f448 KL |
2147 | if (DECL_STATIC_FUNCTION_P (tmpl) |
2148 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
4546865e | 2149 | revert_static_member_fn (decl); |
6c07f448 | 2150 | |
f9a7ae04 | 2151 | /* If this is a specialization of a member template of a |
29a1da1c MM |
2152 | template class, we want to return the TEMPLATE_DECL, not |
2153 | the specialization of it. */ | |
74b846e0 MM |
2154 | if (tsk == tsk_template) |
2155 | { | |
2156 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); | |
f9a7ae04 | 2157 | DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE; |
b190f239 NS |
2158 | if (have_def) |
2159 | { | |
f31686a3 RH |
2160 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
2161 | DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl)) | |
2162 | = DECL_SOURCE_LOCATION (decl); | |
08167d1c AO |
2163 | /* We want to use the argument list specified in the |
2164 | definition, not in the original declaration. */ | |
2165 | DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl)) | |
2166 | = DECL_ARGUMENTS (decl); | |
b190f239 | 2167 | } |
74b846e0 MM |
2168 | return tmpl; |
2169 | } | |
2170 | ||
36a117a5 | 2171 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
e1b3e07d | 2172 | DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE); |
36a117a5 | 2173 | |
8afa707f MM |
2174 | /* Inherit default function arguments from the template |
2175 | DECL is specializing. */ | |
2176 | copy_default_args_to_explicit_spec (decl); | |
2177 | ||
c750255c MM |
2178 | /* This specialization has the same protection as the |
2179 | template it specializes. */ | |
2180 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); | |
2181 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); | |
b9e75696 | 2182 | |
9c12301f MM |
2183 | /* If DECL is a friend declaration, declared using an |
2184 | unqualified name, the namespace associated with DECL may | |
2185 | have been set incorrectly. For example, in: | |
3db45ab5 MS |
2186 | |
2187 | template <typename T> void f(T); | |
2188 | namespace N { | |
2189 | struct S { friend void f<int>(int); } | |
2190 | } | |
2191 | ||
2192 | we will have set the DECL_CONTEXT for the friend | |
2193 | declaration to N, rather than to the global namespace. */ | |
9c12301f MM |
2194 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
2195 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); | |
8d039470 | 2196 | |
386b8a85 | 2197 | if (is_friend && !have_def) |
36a117a5 MM |
2198 | /* This is not really a declaration of a specialization. |
2199 | It's just the name of an instantiation. But, it's not | |
2200 | a request for an instantiation, either. */ | |
fbf1c34b | 2201 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
08511114 KL |
2202 | else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl)) |
2203 | /* This is indeed a specialization. In case of constructors | |
2204 | and destructors, we need in-charge and not-in-charge | |
2205 | versions in V3 ABI. */ | |
2206 | clone_function_decl (decl, /*update_method_vec_p=*/0); | |
75650646 | 2207 | |
36a117a5 MM |
2208 | /* Register this specialization so that we can find it |
2209 | again. */ | |
d63d5d0c | 2210 | decl = register_specialization (decl, gen_tmpl, targs, is_friend); |
386b8a85 JM |
2211 | } |
2212 | } | |
c8094d83 | 2213 | |
e1467ff2 | 2214 | return decl; |
386b8a85 | 2215 | } |
75650646 | 2216 | |
75650646 MM |
2217 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
2218 | parameters. These are represented in the same format used for | |
2219 | DECL_TEMPLATE_PARMS. */ | |
2220 | ||
50bc768d NS |
2221 | int |
2222 | comp_template_parms (tree parms1, tree parms2) | |
75650646 MM |
2223 | { |
2224 | tree p1; | |
2225 | tree p2; | |
2226 | ||
2227 | if (parms1 == parms2) | |
2228 | return 1; | |
2229 | ||
c8094d83 | 2230 | for (p1 = parms1, p2 = parms2; |
75650646 MM |
2231 | p1 != NULL_TREE && p2 != NULL_TREE; |
2232 | p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2)) | |
2233 | { | |
2234 | tree t1 = TREE_VALUE (p1); | |
2235 | tree t2 = TREE_VALUE (p2); | |
2236 | int i; | |
2237 | ||
50bc768d NS |
2238 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
2239 | gcc_assert (TREE_CODE (t2) == TREE_VEC); | |
75650646 MM |
2240 | |
2241 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) | |
2242 | return 0; | |
2243 | ||
c8094d83 | 2244 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
75650646 | 2245 | { |
0f67a82f LM |
2246 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
2247 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); | |
42b304f1 | 2248 | |
0f67a82f LM |
2249 | /* If either of the template parameters are invalid, assume |
2250 | they match for the sake of error recovery. */ | |
2251 | if (parm1 == error_mark_node || parm2 == error_mark_node) | |
2252 | return 1; | |
75650646 MM |
2253 | |
2254 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) | |
2255 | return 0; | |
2256 | ||
2257 | if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM) | |
2258 | continue; | |
3bfdc719 | 2259 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
75650646 MM |
2260 | return 0; |
2261 | } | |
2262 | } | |
2263 | ||
2264 | if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) | |
2265 | /* One set of parameters has more parameters lists than the | |
2266 | other. */ | |
2267 | return 0; | |
2268 | ||
2269 | return 1; | |
2270 | } | |
2271 | ||
f3400fe2 JM |
2272 | /* Complain if DECL shadows a template parameter. |
2273 | ||
2274 | [temp.local]: A template-parameter shall not be redeclared within its | |
2275 | scope (including nested scopes). */ | |
2276 | ||
2277 | void | |
3a978d72 | 2278 | check_template_shadow (tree decl) |
f3400fe2 | 2279 | { |
8f032717 MM |
2280 | tree olddecl; |
2281 | ||
b5d9b9ab MM |
2282 | /* If we're not in a template, we can't possibly shadow a template |
2283 | parameter. */ | |
2284 | if (!current_template_parms) | |
2285 | return; | |
2286 | ||
2287 | /* Figure out what we're shadowing. */ | |
8f032717 MM |
2288 | if (TREE_CODE (decl) == OVERLOAD) |
2289 | decl = OVL_CURRENT (decl); | |
90ea9897 | 2290 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
f3400fe2 | 2291 | |
b5d9b9ab MM |
2292 | /* If there's no previous binding for this name, we're not shadowing |
2293 | anything, let alone a template parameter. */ | |
2294 | if (!olddecl) | |
2295 | return; | |
2296 | ||
2297 | /* If we're not shadowing a template parameter, we're done. Note | |
2298 | that OLDDECL might be an OVERLOAD (or perhaps even an | |
2299 | ERROR_MARK), so we can't just blithely assume it to be a _DECL | |
2300 | node. */ | |
2f939d94 | 2301 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
b5d9b9ab MM |
2302 | return; |
2303 | ||
2304 | /* We check for decl != olddecl to avoid bogus errors for using a | |
2305 | name inside a class. We check TPFI to avoid duplicate errors for | |
2306 | inline member templates. */ | |
c8094d83 | 2307 | if (decl == olddecl |
b5d9b9ab MM |
2308 | || TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) |
2309 | return; | |
2310 | ||
dee15844 JM |
2311 | error ("declaration of %q+#D", decl); |
2312 | error (" shadows template parm %q+#D", olddecl); | |
f3400fe2 | 2313 | } |
22a7be53 | 2314 | |
f3400fe2 | 2315 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
f84b4be9 JM |
2316 | ORIG_LEVEL, DECL, and TYPE. */ |
2317 | ||
2318 | static tree | |
c8094d83 | 2319 | build_template_parm_index (int index, |
0cbd7506 MS |
2320 | int level, |
2321 | int orig_level, | |
2322 | tree decl, | |
2323 | tree type) | |
f84b4be9 JM |
2324 | { |
2325 | tree t = make_node (TEMPLATE_PARM_INDEX); | |
2326 | TEMPLATE_PARM_IDX (t) = index; | |
2327 | TEMPLATE_PARM_LEVEL (t) = level; | |
2328 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; | |
2329 | TEMPLATE_PARM_DECL (t) = decl; | |
2330 | TREE_TYPE (t) = type; | |
3e4a3562 | 2331 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
6de9cd9a | 2332 | TREE_INVARIANT (t) = TREE_INVARIANT (decl); |
3e4a3562 | 2333 | TREE_READONLY (t) = TREE_READONLY (decl); |
f84b4be9 JM |
2334 | |
2335 | return t; | |
2336 | } | |
2337 | ||
f84b4be9 | 2338 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
93cdc044 | 2339 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
f84b4be9 JM |
2340 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
2341 | new one is created. */ | |
2342 | ||
c8094d83 | 2343 | static tree |
3a978d72 | 2344 | reduce_template_parm_level (tree index, tree type, int levels) |
f84b4be9 JM |
2345 | { |
2346 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE | |
2347 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) | |
93cdc044 | 2348 | != TEMPLATE_PARM_LEVEL (index) - levels)) |
f84b4be9 | 2349 | { |
3e4a3562 NS |
2350 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
2351 | tree decl, t; | |
c8094d83 | 2352 | |
3e4a3562 NS |
2353 | decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type); |
2354 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); | |
6de9cd9a | 2355 | TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl); |
3e4a3562 NS |
2356 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); |
2357 | DECL_ARTIFICIAL (decl) = 1; | |
2358 | SET_DECL_TEMPLATE_PARM_P (decl); | |
c8094d83 | 2359 | |
3e4a3562 | 2360 | t = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
93cdc044 | 2361 | TEMPLATE_PARM_LEVEL (index) - levels, |
f84b4be9 JM |
2362 | TEMPLATE_PARM_ORIG_LEVEL (index), |
2363 | decl, type); | |
2364 | TEMPLATE_PARM_DESCENDANTS (index) = t; | |
cae40af6 | 2365 | |
820cc88f DB |
2366 | /* Template template parameters need this. */ |
2367 | if (TREE_CODE (decl) != CONST_DECL) | |
2368 | DECL_TEMPLATE_PARMS (decl) | |
2369 | = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)); | |
f84b4be9 JM |
2370 | } |
2371 | ||
2372 | return TEMPLATE_PARM_DESCENDANTS (index); | |
2373 | } | |
2374 | ||
a14de1af | 2375 | /* Process information from new template parameter PARM and append it to the |
058b15c1 MM |
2376 | LIST being built. This new parameter is a non-type parameter iff |
2377 | IS_NON_TYPE is true. */ | |
e92cc029 | 2378 | |
8d08fdba | 2379 | tree |
a14de1af | 2380 | process_template_parm (tree list, tree parm, bool is_non_type) |
8d08fdba | 2381 | { |
8d08fdba | 2382 | tree decl = 0; |
a292b002 | 2383 | tree defval; |
0f67a82f | 2384 | tree err_parm_list; |
d47e3adf | 2385 | int idx = 0; |
f84b4be9 | 2386 | |
50bc768d | 2387 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
a292b002 | 2388 | defval = TREE_PURPOSE (parm); |
5566b478 MS |
2389 | |
2390 | if (list) | |
2391 | { | |
d47e3adf LM |
2392 | tree p = tree_last (list); |
2393 | ||
0f67a82f | 2394 | if (p && TREE_VALUE (p) != error_mark_node) |
d47e3adf LM |
2395 | { |
2396 | p = TREE_VALUE (p); | |
2397 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) | |
2398 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); | |
2399 | else | |
2400 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); | |
2401 | } | |
5566b478 | 2402 | |
5566b478 MS |
2403 | ++idx; |
2404 | } | |
2405 | else | |
2406 | idx = 0; | |
2407 | ||
058b15c1 | 2408 | if (is_non_type) |
8d08fdba | 2409 | { |
058b15c1 MM |
2410 | parm = TREE_VALUE (parm); |
2411 | ||
833aa4c4 | 2412 | SET_DECL_TEMPLATE_PARM_P (parm); |
d490621d | 2413 | |
620188c9 | 2414 | if (TREE_TYPE (parm) == error_mark_node) |
0f67a82f LM |
2415 | { |
2416 | err_parm_list = build_tree_list (defval, parm); | |
2417 | TREE_VALUE (err_parm_list) = error_mark_node; | |
2418 | return chainon (list, err_parm_list); | |
2419 | } | |
620188c9 VR |
2420 | else |
2421 | { | |
2422 | /* [temp.param] | |
d490621d | 2423 | |
620188c9 VR |
2424 | The top-level cv-qualifiers on the template-parameter are |
2425 | ignored when determining its type. */ | |
2426 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); | |
2427 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) | |
0f67a82f LM |
2428 | { |
2429 | err_parm_list = build_tree_list (defval, parm); | |
2430 | TREE_VALUE (err_parm_list) = error_mark_node; | |
2431 | return chainon (list, err_parm_list); | |
2432 | } | |
620188c9 | 2433 | } |
d490621d | 2434 | |
8d08fdba | 2435 | /* A template parameter is not modifiable. */ |
6de9cd9a DN |
2436 | TREE_CONSTANT (parm) = 1; |
2437 | TREE_INVARIANT (parm) = 1; | |
2438 | TREE_READONLY (parm) = 1; | |
8d08fdba | 2439 | decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
6de9cd9a DN |
2440 | TREE_CONSTANT (decl) = 1; |
2441 | TREE_INVARIANT (decl) = 1; | |
2442 | TREE_READONLY (decl) = 1; | |
c8094d83 | 2443 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
f84b4be9 JM |
2444 | = build_template_parm_index (idx, processing_template_decl, |
2445 | processing_template_decl, | |
2446 | decl, TREE_TYPE (parm)); | |
8d08fdba MS |
2447 | } |
2448 | else | |
2449 | { | |
73b0fce8 | 2450 | tree t; |
058b15c1 | 2451 | parm = TREE_VALUE (TREE_VALUE (parm)); |
c8094d83 | 2452 | |
73b0fce8 KL |
2453 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
2454 | { | |
33848bb0 | 2455 | t = make_aggr_type (TEMPLATE_TEMPLATE_PARM); |
c8094d83 | 2456 | /* This is for distinguishing between real templates and template |
73b0fce8 KL |
2457 | template parameters */ |
2458 | TREE_TYPE (parm) = t; | |
2459 | TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t; | |
2460 | decl = parm; | |
2461 | } | |
2462 | else | |
2463 | { | |
33848bb0 | 2464 | t = make_aggr_type (TEMPLATE_TYPE_PARM); |
f4f206f4 | 2465 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
73b0fce8 KL |
2466 | decl = build_decl (TYPE_DECL, parm, t); |
2467 | } | |
c8094d83 | 2468 | |
d2e5ee5c MS |
2469 | TYPE_NAME (t) = decl; |
2470 | TYPE_STUB_DECL (t) = decl; | |
a292b002 | 2471 | parm = decl; |
f84b4be9 | 2472 | TEMPLATE_TYPE_PARM_INDEX (t) |
c8094d83 | 2473 | = build_template_parm_index (idx, processing_template_decl, |
f84b4be9 JM |
2474 | processing_template_decl, |
2475 | decl, TREE_TYPE (parm)); | |
8d08fdba | 2476 | } |
c727aa5e | 2477 | DECL_ARTIFICIAL (decl) = 1; |
cd9f6678 | 2478 | SET_DECL_TEMPLATE_PARM_P (decl); |
8d08fdba | 2479 | pushdecl (decl); |
a292b002 | 2480 | parm = build_tree_list (defval, parm); |
8d08fdba MS |
2481 | return chainon (list, parm); |
2482 | } | |
2483 | ||
2484 | /* The end of a template parameter list has been reached. Process the | |
2485 | tree list into a parameter vector, converting each parameter into a more | |
2486 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others | |
2487 | as PARM_DECLs. */ | |
2488 | ||
2489 | tree | |
3a978d72 | 2490 | end_template_parm_list (tree parms) |
8d08fdba | 2491 | { |
5566b478 | 2492 | int nparms; |
9471d3e2 | 2493 | tree parm, next; |
5566b478 MS |
2494 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
2495 | ||
5566b478 | 2496 | current_template_parms |
4890c2f4 | 2497 | = tree_cons (size_int (processing_template_decl), |
5566b478 | 2498 | saved_parmlist, current_template_parms); |
8d08fdba | 2499 | |
9471d3e2 NS |
2500 | for (parm = parms, nparms = 0; parm; parm = next, nparms++) |
2501 | { | |
2502 | next = TREE_CHAIN (parm); | |
2503 | TREE_VEC_ELT (saved_parmlist, nparms) = parm; | |
2504 | TREE_CHAIN (parm) = NULL_TREE; | |
2505 | } | |
a292b002 | 2506 | |
67ffc812 MM |
2507 | --processing_template_parmlist; |
2508 | ||
8d08fdba MS |
2509 | return saved_parmlist; |
2510 | } | |
2511 | ||
5566b478 MS |
2512 | /* end_template_decl is called after a template declaration is seen. */ |
2513 | ||
8d08fdba | 2514 | void |
3a978d72 | 2515 | end_template_decl (void) |
8d08fdba | 2516 | { |
386b8a85 JM |
2517 | reset_specialization (); |
2518 | ||
5156628f | 2519 | if (! processing_template_decl) |
73aad9b9 JM |
2520 | return; |
2521 | ||
5566b478 | 2522 | /* This matches the pushlevel in begin_template_parm_list. */ |
74b846e0 | 2523 | finish_scope (); |
8d08fdba | 2524 | |
5566b478 MS |
2525 | --processing_template_decl; |
2526 | current_template_parms = TREE_CHAIN (current_template_parms); | |
5566b478 | 2527 | } |
8d08fdba | 2528 | |
36a117a5 MM |
2529 | /* Given a template argument vector containing the template PARMS. |
2530 | The innermost PARMS are given first. */ | |
9a3b49ac | 2531 | |
b5791fdc | 2532 | static tree |
3a978d72 | 2533 | current_template_args (void) |
5566b478 | 2534 | { |
36a117a5 | 2535 | tree header; |
b370501f | 2536 | tree args = NULL_TREE; |
36a117a5 | 2537 | int length = TMPL_PARMS_DEPTH (current_template_parms); |
98c1c668 JM |
2538 | int l = length; |
2539 | ||
36a117a5 MM |
2540 | /* If there is only one level of template parameters, we do not |
2541 | create a TREE_VEC of TREE_VECs. Instead, we return a single | |
2542 | TREE_VEC containing the arguments. */ | |
2543 | if (length > 1) | |
2544 | args = make_tree_vec (length); | |
2545 | ||
2546 | for (header = current_template_parms; header; header = TREE_CHAIN (header)) | |
8d08fdba | 2547 | { |
5566b478 | 2548 | tree a = copy_node (TREE_VALUE (header)); |
36a117a5 MM |
2549 | int i; |
2550 | ||
5566b478 | 2551 | TREE_TYPE (a) = NULL_TREE; |
36a117a5 | 2552 | for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) |
5566b478 | 2553 | { |
98c1c668 JM |
2554 | tree t = TREE_VEC_ELT (a, i); |
2555 | ||
36a117a5 | 2556 | /* T will be a list if we are called from within a |
98c1c668 JM |
2557 | begin/end_template_parm_list pair, but a vector directly |
2558 | if within a begin/end_member_template_processing pair. */ | |
c8094d83 | 2559 | if (TREE_CODE (t) == TREE_LIST) |
98c1c668 JM |
2560 | { |
2561 | t = TREE_VALUE (t); | |
c8094d83 | 2562 | |
0f67a82f LM |
2563 | if (t != error_mark_node) |
2564 | { | |
2565 | if (TREE_CODE (t) == TYPE_DECL | |
2566 | || TREE_CODE (t) == TEMPLATE_DECL) | |
2567 | t = TREE_TYPE (t); | |
2568 | else | |
2569 | t = DECL_INITIAL (t); | |
2570 | } | |
2571 | ||
36a117a5 | 2572 | TREE_VEC_ELT (a, i) = t; |
98c1c668 | 2573 | } |
5566b478 | 2574 | } |
36a117a5 MM |
2575 | |
2576 | if (length > 1) | |
2577 | TREE_VEC_ELT (args, --l) = a; | |
2578 | else | |
2579 | args = a; | |
8d08fdba MS |
2580 | } |
2581 | ||
9a3b49ac MS |
2582 | return args; |
2583 | } | |
75650646 | 2584 | |
e1467ff2 | 2585 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
c7222c02 | 2586 | template PARMS. If MEMBER_TEMPLATE_P is true, the new template is |
ee81147e | 2587 | a member template. Used by push_template_decl below. */ |
e1467ff2 | 2588 | |
75650646 | 2589 | static tree |
c7222c02 | 2590 | build_template_decl (tree decl, tree parms, bool member_template_p) |
75650646 MM |
2591 | { |
2592 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); | |
2593 | DECL_TEMPLATE_PARMS (tmpl) = parms; | |
2594 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); | |
c7222c02 | 2595 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
75650646 MM |
2596 | if (DECL_LANG_SPECIFIC (decl)) |
2597 | { | |
8f032717 MM |
2598 | DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); |
2599 | DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl); | |
c8460010 | 2600 | DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl); |
11f98788 | 2601 | DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl); |
1f6e1acc AS |
2602 | DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl); |
2603 | if (DECL_OVERLOADED_OPERATOR_P (decl)) | |
c8094d83 | 2604 | SET_OVERLOADED_OPERATOR_CODE (tmpl, |
1f6e1acc | 2605 | DECL_OVERLOADED_OPERATOR_P (decl)); |
75650646 MM |
2606 | } |
2607 | ||
2608 | return tmpl; | |
2609 | } | |
2610 | ||
050367a3 MM |
2611 | struct template_parm_data |
2612 | { | |
6c30752f MM |
2613 | /* The level of the template parameters we are currently |
2614 | processing. */ | |
050367a3 | 2615 | int level; |
6c30752f MM |
2616 | |
2617 | /* The index of the specialization argument we are currently | |
2618 | processing. */ | |
2619 | int current_arg; | |
2620 | ||
2621 | /* An array whose size is the number of template parameters. The | |
838dfd8a | 2622 | elements are nonzero if the parameter has been used in any one |
6c30752f | 2623 | of the arguments processed so far. */ |
050367a3 | 2624 | int* parms; |
6c30752f MM |
2625 | |
2626 | /* An array whose size is the number of template arguments. The | |
838dfd8a | 2627 | elements are nonzero if the argument makes use of template |
6c30752f MM |
2628 | parameters of this level. */ |
2629 | int* arg_uses_template_parms; | |
050367a3 MM |
2630 | }; |
2631 | ||
2632 | /* Subroutine of push_template_decl used to see if each template | |
2633 | parameter in a partial specialization is used in the explicit | |
2634 | argument list. If T is of the LEVEL given in DATA (which is | |
2635 | treated as a template_parm_data*), then DATA->PARMS is marked | |
2636 | appropriately. */ | |
2637 | ||
2638 | static int | |
3a978d72 | 2639 | mark_template_parm (tree t, void* data) |
050367a3 MM |
2640 | { |
2641 | int level; | |
2642 | int idx; | |
2643 | struct template_parm_data* tpd = (struct template_parm_data*) data; | |
2644 | ||
2645 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
2646 | { | |
2647 | level = TEMPLATE_PARM_LEVEL (t); | |
2648 | idx = TEMPLATE_PARM_IDX (t); | |
2649 | } | |
2650 | else | |
2651 | { | |
2652 | level = TEMPLATE_TYPE_LEVEL (t); | |
2653 | idx = TEMPLATE_TYPE_IDX (t); | |
2654 | } | |
2655 | ||
2656 | if (level == tpd->level) | |
6c30752f MM |
2657 | { |
2658 | tpd->parms[idx] = 1; | |
2659 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; | |
2660 | } | |
050367a3 MM |
2661 | |
2662 | /* Return zero so that for_each_template_parm will continue the | |
2663 | traversal of the tree; we want to mark *every* template parm. */ | |
2664 | return 0; | |
2665 | } | |
2666 | ||
6c30752f MM |
2667 | /* Process the partial specialization DECL. */ |
2668 | ||
e9659ab0 | 2669 | static tree |
3a978d72 | 2670 | process_partial_specialization (tree decl) |
6c30752f MM |
2671 | { |
2672 | tree type = TREE_TYPE (decl); | |
2673 | tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); | |
2674 | tree specargs = CLASSTYPE_TI_ARGS (type); | |
f9a7ae04 | 2675 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
6c30752f MM |
2676 | tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
2677 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); | |
2678 | int nargs = TREE_VEC_LENGTH (inner_args); | |
2679 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
2680 | int i; | |
2681 | int did_error_intro = 0; | |
6c30752f MM |
2682 | struct template_parm_data tpd; |
2683 | struct template_parm_data tpd2; | |
2684 | ||
6c30752f MM |
2685 | /* We check that each of the template parameters given in the |
2686 | partial specialization is used in the argument list to the | |
2687 | specialization. For example: | |
2688 | ||
2689 | template <class T> struct S; | |
2690 | template <class T> struct S<T*>; | |
2691 | ||
2692 | The second declaration is OK because `T*' uses the template | |
2693 | parameter T, whereas | |
2694 | ||
2695 | template <class T> struct S<int>; | |
2696 | ||
2697 | is no good. Even trickier is: | |
2698 | ||
2699 | template <class T> | |
2700 | struct S1 | |
2701 | { | |
2702 | template <class U> | |
2703 | struct S2; | |
2704 | template <class U> | |
2705 | struct S2<T>; | |
2706 | }; | |
2707 | ||
0e339752 | 2708 | The S2<T> declaration is actually invalid; it is a |
c8094d83 | 2709 | full-specialization. Of course, |
6c30752f MM |
2710 | |
2711 | template <class U> | |
2712 | struct S2<T (*)(U)>; | |
2713 | ||
2714 | or some such would have been OK. */ | |
2715 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); | |
67f5655f | 2716 | tpd.parms = (int *) alloca (sizeof (int) * ntparms); |
fad205ff | 2717 | memset (tpd.parms, 0, sizeof (int) * ntparms); |
6c30752f | 2718 | |
67f5655f | 2719 | tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs); |
fad205ff | 2720 | memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs); |
6c30752f MM |
2721 | for (i = 0; i < nargs; ++i) |
2722 | { | |
2723 | tpd.current_arg = i; | |
2724 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), | |
2725 | &mark_template_parm, | |
4f2c9d7e MM |
2726 | &tpd, |
2727 | NULL); | |
6c30752f MM |
2728 | } |
2729 | for (i = 0; i < ntparms; ++i) | |
2730 | if (tpd.parms[i] == 0) | |
2731 | { | |
2732 | /* One of the template parms was not used in the | |
f9a7ae04 | 2733 | specialization. */ |
6c30752f MM |
2734 | if (!did_error_intro) |
2735 | { | |
33bd39a2 | 2736 | error ("template parameters not used in partial specialization:"); |
6c30752f MM |
2737 | did_error_intro = 1; |
2738 | } | |
2739 | ||
0f51ccfc | 2740 | error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
6c30752f MM |
2741 | } |
2742 | ||
2743 | /* [temp.class.spec] | |
2744 | ||
2745 | The argument list of the specialization shall not be identical to | |
2746 | the implicit argument list of the primary template. */ | |
c8094d83 MS |
2747 | if (comp_template_args |
2748 | (inner_args, | |
f9a7ae04 MM |
2749 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE |
2750 | (maintmpl))))) | |
0f51ccfc | 2751 | error ("partial specialization %qT does not specialize any template arguments", type); |
6c30752f MM |
2752 | |
2753 | /* [temp.class.spec] | |
2754 | ||
2755 | A partially specialized non-type argument expression shall not | |
2756 | involve template parameters of the partial specialization except | |
2757 | when the argument expression is a simple identifier. | |
2758 | ||
2759 | The type of a template parameter corresponding to a specialized | |
2760 | non-type argument shall not be dependent on a parameter of the | |
2761 | specialization. */ | |
50bc768d | 2762 | gcc_assert (nargs == DECL_NTPARMS (maintmpl)); |
6c30752f MM |
2763 | tpd2.parms = 0; |
2764 | for (i = 0; i < nargs; ++i) | |
2765 | { | |
2766 | tree arg = TREE_VEC_ELT (inner_args, i); | |
2767 | if (/* These first two lines are the `non-type' bit. */ | |
2f939d94 | 2768 | !TYPE_P (arg) |
6c30752f MM |
2769 | && TREE_CODE (arg) != TEMPLATE_DECL |
2770 | /* This next line is the `argument expression is not just a | |
2771 | simple identifier' condition and also the `specialized | |
2772 | non-type argument' bit. */ | |
2773 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX) | |
2774 | { | |
2775 | if (tpd.arg_uses_template_parms[i]) | |
0f51ccfc | 2776 | error ("template argument %qE involves template parameter(s)", arg); |
c8094d83 | 2777 | else |
6c30752f MM |
2778 | { |
2779 | /* Look at the corresponding template parameter, | |
2780 | marking which template parameters its type depends | |
2781 | upon. */ | |
c8094d83 MS |
2782 | tree type = |
2783 | TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms, | |
6c30752f MM |
2784 | i))); |
2785 | ||
2786 | if (!tpd2.parms) | |
2787 | { | |
2788 | /* We haven't yet initialized TPD2. Do so now. */ | |
c8094d83 | 2789 | tpd2.arg_uses_template_parms |
67f5655f | 2790 | = (int *) alloca (sizeof (int) * nargs); |
104bf76a | 2791 | /* The number of parameters here is the number in the |
76a83782 RH |
2792 | main template, which, as checked in the assertion |
2793 | above, is NARGS. */ | |
67f5655f | 2794 | tpd2.parms = (int *) alloca (sizeof (int) * nargs); |
c8094d83 | 2795 | tpd2.level = |
6c30752f MM |
2796 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); |
2797 | } | |
2798 | ||
104bf76a | 2799 | /* Mark the template parameters. But this time, we're |
6c30752f MM |
2800 | looking for the template parameters of the main |
2801 | template, not in the specialization. */ | |
2802 | tpd2.current_arg = i; | |
2803 | tpd2.arg_uses_template_parms[i] = 0; | |
fad205ff | 2804 | memset (tpd2.parms, 0, sizeof (int) * nargs); |
6c30752f MM |
2805 | for_each_template_parm (type, |
2806 | &mark_template_parm, | |
4f2c9d7e MM |
2807 | &tpd2, |
2808 | NULL); | |
c8094d83 | 2809 | |
6c30752f MM |
2810 | if (tpd2.arg_uses_template_parms [i]) |
2811 | { | |
2812 | /* The type depended on some template parameters. | |
2813 | If they are fully specialized in the | |
2814 | specialization, that's OK. */ | |
2815 | int j; | |
2816 | for (j = 0; j < nargs; ++j) | |
2817 | if (tpd2.parms[j] != 0 | |
2818 | && tpd.arg_uses_template_parms [j]) | |
2819 | { | |
0f51ccfc | 2820 | error ("type %qT of template argument %qE depends " |
0cbd7506 MS |
2821 | "on template parameter(s)", |
2822 | type, | |
2823 | arg); | |
6c30752f MM |
2824 | break; |
2825 | } | |
2826 | } | |
2827 | } | |
2828 | } | |
2829 | } | |
2830 | ||
c8094d83 | 2831 | if (retrieve_specialization (maintmpl, specargs, |
c7222c02 | 2832 | /*class_specializations_p=*/true)) |
6c30752f MM |
2833 | /* We've already got this specialization. */ |
2834 | return decl; | |
2835 | ||
d8b64f80 | 2836 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
916b63c3 | 2837 | = tree_cons (specargs, inner_parms, |
e1b3e07d | 2838 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
6c30752f MM |
2839 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
2840 | return decl; | |
2841 | } | |
2842 | ||
6ba89f8e MM |
2843 | /* Check that a template declaration's use of default arguments is not |
2844 | invalid. Here, PARMS are the template parameters. IS_PRIMARY is | |
838dfd8a KH |
2845 | nonzero if DECL is the thing declared by a primary template. |
2846 | IS_PARTIAL is nonzero if DECL is a partial specialization. */ | |
6ba89f8e MM |
2847 | |
2848 | static void | |
3a978d72 | 2849 | check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial) |
6ba89f8e | 2850 | { |
d8e178a0 | 2851 | const char *msg; |
66191c20 MM |
2852 | int last_level_to_check; |
2853 | tree parm_level; | |
6ba89f8e | 2854 | |
c8094d83 | 2855 | /* [temp.param] |
6ba89f8e MM |
2856 | |
2857 | A default template-argument shall not be specified in a | |
2858 | function template declaration or a function template definition, nor | |
2859 | in the template-parameter-list of the definition of a member of a | |
2860 | class template. */ | |
2861 | ||
4f1c5b7d | 2862 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
e0942dcd MM |
2863 | /* You can't have a function template declaration in a local |
2864 | scope, nor you can you define a member of a class template in a | |
2865 | local scope. */ | |
2866 | return; | |
2867 | ||
6ba89f8e MM |
2868 | if (current_class_type |
2869 | && !TYPE_BEING_DEFINED (current_class_type) | |
3d7de1fa | 2870 | && DECL_LANG_SPECIFIC (decl) |
5937a6f9 MM |
2871 | /* If this is either a friend defined in the scope of the class |
2872 | or a member function. */ | |
6df5158a NS |
2873 | && (DECL_FUNCTION_MEMBER_P (decl) |
2874 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) | |
2875 | : DECL_FRIEND_CONTEXT (decl) | |
2876 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) | |
2877 | : false) | |
5937a6f9 MM |
2878 | /* And, if it was a member function, it really was defined in |
2879 | the scope of the class. */ | |
6df5158a NS |
2880 | && (!DECL_FUNCTION_MEMBER_P (decl) |
2881 | || DECL_INITIALIZED_IN_CLASS_P (decl))) | |
6ba89f8e | 2882 | /* We already checked these parameters when the template was |
5937a6f9 MM |
2883 | declared, so there's no need to do it again now. This function |
2884 | was defined in class scope, but we're processing it's body now | |
2885 | that the class is complete. */ | |
6ba89f8e MM |
2886 | return; |
2887 | ||
66191c20 | 2888 | /* [temp.param] |
c8094d83 | 2889 | |
66191c20 MM |
2890 | If a template-parameter has a default template-argument, all |
2891 | subsequent template-parameters shall have a default | |
2892 | template-argument supplied. */ | |
2893 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) | |
2894 | { | |
2895 | tree inner_parms = TREE_VALUE (parm_level); | |
2896 | int ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 2897 | int seen_def_arg_p = 0; |
66191c20 MM |
2898 | int i; |
2899 | ||
c8094d83 | 2900 | for (i = 0; i < ntparms; ++i) |
66191c20 MM |
2901 | { |
2902 | tree parm = TREE_VEC_ELT (inner_parms, i); | |
42b304f1 LM |
2903 | |
2904 | if (parm == error_mark_node) | |
2905 | continue; | |
2906 | ||
66191c20 MM |
2907 | if (TREE_PURPOSE (parm)) |
2908 | seen_def_arg_p = 1; | |
2909 | else if (seen_def_arg_p) | |
2910 | { | |
0f51ccfc | 2911 | error ("no default argument for %qD", TREE_VALUE (parm)); |
66191c20 MM |
2912 | /* For better subsequent error-recovery, we indicate that |
2913 | there should have been a default argument. */ | |
2914 | TREE_PURPOSE (parm) = error_mark_node; | |
2915 | } | |
2916 | } | |
2917 | } | |
2918 | ||
6ba89f8e MM |
2919 | if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary) |
2920 | /* For an ordinary class template, default template arguments are | |
2921 | allowed at the innermost level, e.g.: | |
0cbd7506 | 2922 | template <class T = int> |
6ba89f8e MM |
2923 | struct S {}; |
2924 | but, in a partial specialization, they're not allowed even | |
2925 | there, as we have in [temp.class.spec]: | |
c8094d83 | 2926 | |
6ba89f8e | 2927 | The template parameter list of a specialization shall not |
c8094d83 | 2928 | contain default template argument values. |
6ba89f8e MM |
2929 | |
2930 | So, for a partial specialization, or for a function template, | |
2931 | we look at all of them. */ | |
2932 | ; | |
2933 | else | |
2934 | /* But, for a primary class template that is not a partial | |
2935 | specialization we look at all template parameters except the | |
2936 | innermost ones. */ | |
2937 | parms = TREE_CHAIN (parms); | |
2938 | ||
2939 | /* Figure out what error message to issue. */ | |
2940 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
8653a2c3 | 2941 | msg = "default template arguments may not be used in function templates"; |
6ba89f8e | 2942 | else if (is_partial) |
8653a2c3 | 2943 | msg = "default template arguments may not be used in partial specializations"; |
6ba89f8e | 2944 | else |
0f51ccfc | 2945 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
2946 | |
2947 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) | |
2948 | /* If we're inside a class definition, there's no need to | |
104bf76a | 2949 | examine the parameters to the class itself. On the one |
6ba89f8e | 2950 | hand, they will be checked when the class is defined, and, |
0e339752 | 2951 | on the other, default arguments are valid in things like: |
0cbd7506 MS |
2952 | template <class T = double> |
2953 | struct S { template <class U> void f(U); }; | |
6ba89f8e MM |
2954 | Here the default argument for `S' has no bearing on the |
2955 | declaration of `f'. */ | |
2956 | last_level_to_check = template_class_depth (current_class_type) + 1; | |
2957 | else | |
2958 | /* Check everything. */ | |
2959 | last_level_to_check = 0; | |
2960 | ||
c8094d83 MS |
2961 | for (parm_level = parms; |
2962 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; | |
66191c20 | 2963 | parm_level = TREE_CHAIN (parm_level)) |
6ba89f8e | 2964 | { |
66191c20 MM |
2965 | tree inner_parms = TREE_VALUE (parm_level); |
2966 | int i; | |
2967 | int ntparms; | |
6ba89f8e MM |
2968 | |
2969 | ntparms = TREE_VEC_LENGTH (inner_parms); | |
c8094d83 | 2970 | for (i = 0; i < ntparms; ++i) |
42b304f1 LM |
2971 | { |
2972 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) | |
2973 | continue; | |
6ba89f8e | 2974 | |
42b304f1 LM |
2975 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
2976 | { | |
2977 | if (msg) | |
2978 | { | |
2979 | error (msg, decl); | |
2980 | msg = 0; | |
2981 | } | |
2982 | ||
2983 | /* Clear out the default argument so that we are not | |
2984 | confused later. */ | |
2985 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; | |
2986 | } | |
2987 | } | |
6ba89f8e MM |
2988 | |
2989 | /* At this point, if we're still interested in issuing messages, | |
2990 | they must apply to classes surrounding the object declared. */ | |
2991 | if (msg) | |
c8094d83 | 2992 | msg = "default argument for template parameter for class enclosing %qD"; |
6ba89f8e MM |
2993 | } |
2994 | } | |
2995 | ||
5dd236e2 NS |
2996 | /* Worker for push_template_decl_real, called via |
2997 | for_each_template_parm. DATA is really an int, indicating the | |
2998 | level of the parameters we are interested in. If T is a template | |
838dfd8a | 2999 | parameter of that level, return nonzero. */ |
5dd236e2 NS |
3000 | |
3001 | static int | |
3a978d72 | 3002 | template_parm_this_level_p (tree t, void* data) |
5dd236e2 | 3003 | { |
6e04241f | 3004 | int this_level = *(int *)data; |
5dd236e2 NS |
3005 | int level; |
3006 | ||
3007 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) | |
3008 | level = TEMPLATE_PARM_LEVEL (t); | |
3009 | else | |
3010 | level = TEMPLATE_TYPE_LEVEL (t); | |
3011 | return level == this_level; | |
3012 | } | |
3013 | ||
3ac3d9ea | 3014 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
f84b4be9 JM |
3015 | parameters given by current_template_args, or reuses a |
3016 | previously existing one, if appropriate. Returns the DECL, or an | |
c8094d83 | 3017 | equivalent one, if it is replaced via a call to duplicate_decls. |
6757edfe | 3018 | |
d63d5d0c | 3019 | If IS_FRIEND is true, DECL is a friend declaration. */ |
3ac3d9ea MM |
3020 | |
3021 | tree | |
d63d5d0c | 3022 | push_template_decl_real (tree decl, bool is_friend) |
9a3b49ac MS |
3023 | { |
3024 | tree tmpl; | |
f84b4be9 | 3025 | tree args; |
9a3b49ac | 3026 | tree info; |
f84b4be9 JM |
3027 | tree ctx; |
3028 | int primary; | |
6ba89f8e | 3029 | int is_partial; |
cfe507be | 3030 | int new_template_p = 0; |
c7222c02 MM |
3031 | /* True if the template is a member template, in the sense of |
3032 | [temp.mem]. */ | |
3033 | bool member_template_p = false; | |
6ba89f8e | 3034 | |
c0694c4b MM |
3035 | if (decl == error_mark_node) |
3036 | return decl; | |
3037 | ||
6ba89f8e | 3038 | /* See if this is a partial specialization. */ |
9188c363 | 3039 | is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl) |
6ba89f8e | 3040 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
370af2d5 | 3041 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))); |
6757edfe | 3042 | |
d63d5d0c ILT |
3043 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)) |
3044 | is_friend = true; | |
f84b4be9 JM |
3045 | |
3046 | if (is_friend) | |
3047 | /* For a friend, we want the context of the friend function, not | |
3048 | the type of which it is a friend. */ | |
3049 | ctx = DECL_CONTEXT (decl); | |
4f1c5b7d MM |
3050 | else if (CP_DECL_CONTEXT (decl) |
3051 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
f84b4be9 JM |
3052 | /* In the case of a virtual function, we want the class in which |
3053 | it is defined. */ | |
4f1c5b7d | 3054 | ctx = CP_DECL_CONTEXT (decl); |
f84b4be9 | 3055 | else |
dc957d14 | 3056 | /* Otherwise, if we're currently defining some class, the DECL |
f84b4be9 | 3057 | is assumed to be a member of the class. */ |
9188c363 | 3058 | ctx = current_scope (); |
f84b4be9 | 3059 | |
2c73f9f5 ML |
3060 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
3061 | ctx = NULL_TREE; | |
3062 | ||
3063 | if (!DECL_CONTEXT (decl)) | |
cb0dbb9a | 3064 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 3065 | |
6ba89f8e | 3066 | /* See if this is a primary template. */ |
c9cbfca6 JM |
3067 | if (is_friend && ctx) |
3068 | /* A friend template that specifies a class context, i.e. | |
3069 | template <typename T> friend void A<T>::f(); | |
3070 | is not primary. */ | |
3071 | primary = 0; | |
3072 | else | |
3073 | primary = template_parm_scope_p (); | |
9a3b49ac | 3074 | |
83566abf JM |
3075 | if (primary) |
3076 | { | |
c7222c02 MM |
3077 | if (DECL_CLASS_SCOPE_P (decl)) |
3078 | member_template_p = true; | |
2f1b1731 MM |
3079 | if (TREE_CODE (decl) == TYPE_DECL |
3080 | && ANON_AGGRNAME_P (DECL_NAME (decl))) | |
33bd39a2 | 3081 | error ("template class without a name"); |
717a7d5d | 3082 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
4b0d3cbe | 3083 | { |
717a7d5d MM |
3084 | if (DECL_DESTRUCTOR_P (decl)) |
3085 | { | |
3086 | /* [temp.mem] | |
c8094d83 | 3087 | |
0cbd7506 | 3088 | A destructor shall not be a member template. */ |
0f51ccfc | 3089 | error ("destructor %qD declared as member template", decl); |
717a7d5d MM |
3090 | return error_mark_node; |
3091 | } | |
3092 | if (NEW_DELETE_OPNAME_P (DECL_NAME (decl)) | |
3093 | && (!TYPE_ARG_TYPES (TREE_TYPE (decl)) | |
3094 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node | |
3095 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) | |
3096 | || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl)))) | |
3097 | == void_list_node))) | |
3098 | { | |
c8094d83 | 3099 | /* [basic.stc.dynamic.allocation] |
717a7d5d | 3100 | |
0cbd7506 | 3101 | An allocation function can be a function |
717a7d5d MM |
3102 | template. ... Template allocation functions shall |
3103 | have two or more parameters. */ | |
0f51ccfc | 3104 | error ("invalid template declaration of %qD", decl); |
7c60008e | 3105 | return error_mark_node; |
717a7d5d | 3106 | } |
4b0d3cbe | 3107 | } |
8ca4bf25 MM |
3108 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
3109 | && CLASS_TYPE_P (TREE_TYPE (decl))) | |
2aaf816d JM |
3110 | /* OK */; |
3111 | else | |
11325dcd | 3112 | { |
0f51ccfc | 3113 | error ("template declaration of %q#D", decl); |
11325dcd KL |
3114 | return error_mark_node; |
3115 | } | |
83566abf JM |
3116 | } |
3117 | ||
6ba89f8e MM |
3118 | /* Check to see that the rules regarding the use of default |
3119 | arguments are not being violated. */ | |
c8094d83 | 3120 | check_default_tmpl_args (decl, current_template_parms, |
6ba89f8e | 3121 | primary, is_partial); |
73aad9b9 | 3122 | |
6ba89f8e MM |
3123 | if (is_partial) |
3124 | return process_partial_specialization (decl); | |
d32789d8 | 3125 | |
9a3b49ac MS |
3126 | args = current_template_args (); |
3127 | ||
c8094d83 | 3128 | if (!ctx |
f84b4be9 | 3129 | || TREE_CODE (ctx) == FUNCTION_DECL |
a723baf1 | 3130 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
f84b4be9 | 3131 | || (is_friend && !DECL_TEMPLATE_INFO (decl))) |
8d08fdba | 3132 | { |
75650646 | 3133 | if (DECL_LANG_SPECIFIC (decl) |
f84b4be9 JM |
3134 | && DECL_TEMPLATE_INFO (decl) |
3135 | && DECL_TI_TEMPLATE (decl)) | |
3136 | tmpl = DECL_TI_TEMPLATE (decl); | |
1c10870d AS |
3137 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
3138 | be DECL_LANG_SPECIFIC. The information equivalent to | |
3139 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ | |
c8094d83 | 3140 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
1c10870d AS |
3141 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
3142 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3143 | { | |
3144 | /* Since a template declaration already existed for this | |
3145 | class-type, we must be redeclaring it here. Make sure | |
0e339752 | 3146 | that the redeclaration is valid. */ |
1c10870d AS |
3147 | redeclare_class_template (TREE_TYPE (decl), |
3148 | current_template_parms); | |
3149 | /* We don't need to create a new TEMPLATE_DECL; just use the | |
3150 | one we already had. */ | |
3151 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
3152 | } | |
f84b4be9 | 3153 | else |
786b5245 | 3154 | { |
c7222c02 MM |
3155 | tmpl = build_template_decl (decl, current_template_parms, |
3156 | member_template_p); | |
cfe507be MM |
3157 | new_template_p = 1; |
3158 | ||
f84b4be9 JM |
3159 | if (DECL_LANG_SPECIFIC (decl) |
3160 | && DECL_TEMPLATE_SPECIALIZATION (decl)) | |
3161 | { | |
3162 | /* A specialization of a member template of a template | |
c6002625 | 3163 | class. */ |
f84b4be9 JM |
3164 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3165 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); | |
3166 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; | |
3167 | } | |
786b5245 | 3168 | } |
8d08fdba MS |
3169 | } |
3170 | else | |
3171 | { | |
e1a5ccf7 | 3172 | tree a, t, current, parms; |
ba4f4e5d | 3173 | int i; |
6633d636 | 3174 | |
6b9ab5cc MM |
3175 | if (TREE_CODE (decl) == TYPE_DECL) |
3176 | { | |
ed44da02 MM |
3177 | if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl))) |
3178 | || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) | |
3179 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) | |
3180 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) | |
3181 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); | |
6b9ab5cc MM |
3182 | else |
3183 | { | |
0f51ccfc | 3184 | error ("%qD does not declare a template type", decl); |
6b9ab5cc MM |
3185 | return decl; |
3186 | } | |
3187 | } | |
f3368a90 | 3188 | else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl)) |
c91a56d2 | 3189 | { |
0f51ccfc | 3190 | error ("template definition of non-template %q#D", decl); |
3ac3d9ea | 3191 | return decl; |
c91a56d2 | 3192 | } |
8d08fdba | 3193 | else |
5566b478 | 3194 | tmpl = DECL_TI_TEMPLATE (decl); |
c8094d83 | 3195 | |
c353b8e3 | 3196 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
c8094d83 | 3197 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
c353b8e3 | 3198 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
c7222c02 | 3199 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
98c1c668 | 3200 | { |
e1a5ccf7 JM |
3201 | tree new_tmpl; |
3202 | ||
3203 | /* The declaration is a specialization of a member | |
3204 | template, declared outside the class. Therefore, the | |
3205 | innermost template arguments will be NULL, so we | |
3206 | replace them with the arguments determined by the | |
3207 | earlier call to check_explicit_specialization. */ | |
3208 | args = DECL_TI_ARGS (decl); | |
3209 | ||
c8094d83 | 3210 | new_tmpl |
c7222c02 MM |
3211 | = build_template_decl (decl, current_template_parms, |
3212 | member_template_p); | |
e1a5ccf7 JM |
3213 | DECL_TEMPLATE_RESULT (new_tmpl) = decl; |
3214 | TREE_TYPE (new_tmpl) = TREE_TYPE (decl); | |
3215 | DECL_TI_TEMPLATE (decl) = new_tmpl; | |
3216 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); | |
c8094d83 | 3217 | DECL_TEMPLATE_INFO (new_tmpl) |
e1b3e07d | 3218 | = tree_cons (tmpl, args, NULL_TREE); |
e1a5ccf7 | 3219 | |
c8094d83 MS |
3220 | register_specialization (new_tmpl, |
3221 | most_general_template (tmpl), | |
d63d5d0c ILT |
3222 | args, |
3223 | is_friend); | |
e1a5ccf7 | 3224 | return decl; |
98c1c668 | 3225 | } |
98c1c668 | 3226 | |
e1a5ccf7 | 3227 | /* Make sure the template headers we got make sense. */ |
6633d636 | 3228 | |
e1a5ccf7 JM |
3229 | parms = DECL_TEMPLATE_PARMS (tmpl); |
3230 | i = TMPL_PARMS_DEPTH (parms); | |
3231 | if (TMPL_ARGS_DEPTH (args) != i) | |
98c1c668 | 3232 | { |
0f51ccfc | 3233 | error ("expected %d levels of template parms for %q#D, got %d", |
0cbd7506 | 3234 | i, decl, TMPL_ARGS_DEPTH (args)); |
98c1c668 | 3235 | } |
e1a5ccf7 JM |
3236 | else |
3237 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) | |
3238 | { | |
3239 | a = TMPL_ARGS_LEVEL (args, i); | |
3240 | t = INNERMOST_TEMPLATE_PARMS (parms); | |
3241 | ||
3242 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) | |
3243 | { | |
3244 | if (current == decl) | |
0f51ccfc | 3245 | error ("got %d template parameters for %q#D", |
0cbd7506 | 3246 | TREE_VEC_LENGTH (a), decl); |
e1a5ccf7 | 3247 | else |
0f51ccfc | 3248 | error ("got %d template parameters for %q#T", |
0cbd7506 | 3249 | TREE_VEC_LENGTH (a), current); |
33bd39a2 | 3250 | error (" but %d required", TREE_VEC_LENGTH (t)); |
f1cc0515 | 3251 | return error_mark_node; |
e1a5ccf7 | 3252 | } |
98c1c668 | 3253 | |
e1a5ccf7 | 3254 | /* Perhaps we should also check that the parms are used in the |
0cbd7506 | 3255 | appropriate qualifying scopes in the declarator? */ |
6633d636 | 3256 | |
e1a5ccf7 JM |
3257 | if (current == decl) |
3258 | current = ctx; | |
3259 | else | |
3260 | current = TYPE_CONTEXT (current); | |
3261 | } | |
5566b478 | 3262 | } |
8d08fdba | 3263 | |
5566b478 MS |
3264 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
3265 | TREE_TYPE (tmpl) = TREE_TYPE (decl); | |
8d08fdba | 3266 | |
36a117a5 MM |
3267 | /* Push template declarations for global functions and types. Note |
3268 | that we do not try to push a global template friend declared in a | |
3269 | template class; such a thing may well depend on the template | |
39c01e4c | 3270 | parameters of the class. */ |
c8094d83 | 3271 | if (new_template_p && !ctx |
36a117a5 | 3272 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
c6f9f83b | 3273 | { |
d63d5d0c | 3274 | tmpl = pushdecl_namespace_level (tmpl, is_friend); |
c6f9f83b KL |
3275 | if (tmpl == error_mark_node) |
3276 | return error_mark_node; | |
bd3d082e KL |
3277 | |
3278 | /* Hide template friend classes that haven't been declared yet. */ | |
3279 | if (is_friend && TREE_CODE (decl) == TYPE_DECL) | |
3280 | { | |
3281 | DECL_ANTICIPATED (tmpl) = 1; | |
3282 | DECL_FRIEND_P (tmpl) = 1; | |
3283 | } | |
c6f9f83b | 3284 | } |
8d08fdba | 3285 | |
5566b478 | 3286 | if (primary) |
5dd236e2 NS |
3287 | { |
3288 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; | |
3289 | if (DECL_CONV_FN_P (tmpl)) | |
3290 | { | |
6e04241f GS |
3291 | int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
3292 | ||
5dd236e2 NS |
3293 | /* It is a conversion operator. See if the type converted to |
3294 | depends on innermost template operands. */ | |
c8094d83 | 3295 | |
d43f603d KL |
3296 | if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)), |
3297 | depth)) | |
5dd236e2 NS |
3298 | DECL_TEMPLATE_CONV_FN_P (tmpl) = 1; |
3299 | } | |
3300 | } | |
5566b478 | 3301 | |
a692ad2e | 3302 | /* The DECL_TI_ARGS of DECL contains full set of arguments referring |
28ba38e3 KL |
3303 | back to its most general template. If TMPL is a specialization, |
3304 | ARGS may only have the innermost set of arguments. Add the missing | |
3305 | argument levels if necessary. */ | |
3306 | if (DECL_TEMPLATE_INFO (tmpl)) | |
3307 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args); | |
3308 | ||
e1b3e07d | 3309 | info = tree_cons (tmpl, args, NULL_TREE); |
5566b478 | 3310 | |
9188c363 | 3311 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
8d08fdba | 3312 | { |
ed44da02 MM |
3313 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
3314 | if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL) | |
b60ecc04 MM |
3315 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
3316 | /* Don't change the name if we've already set it up. */ | |
3317 | && !IDENTIFIER_TEMPLATE (DECL_NAME (decl))) | |
75650646 | 3318 | DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); |
51c184be | 3319 | } |
2aaf816d | 3320 | else if (DECL_LANG_SPECIFIC (decl)) |
5566b478 | 3321 | DECL_TEMPLATE_INFO (decl) = info; |
3ac3d9ea MM |
3322 | |
3323 | return DECL_TEMPLATE_RESULT (tmpl); | |
8d08fdba MS |
3324 | } |
3325 | ||
6757edfe | 3326 | tree |
3a978d72 | 3327 | push_template_decl (tree decl) |
6757edfe | 3328 | { |
d63d5d0c | 3329 | return push_template_decl_real (decl, false); |
6757edfe MM |
3330 | } |
3331 | ||
3332 | /* Called when a class template TYPE is redeclared with the indicated | |
3333 | template PARMS, e.g.: | |
7fe6899f MM |
3334 | |
3335 | template <class T> struct S; | |
3336 | template <class T> struct S {}; */ | |
3337 | ||
60feef2c | 3338 | bool |
3a978d72 | 3339 | redeclare_class_template (tree type, tree parms) |
7fe6899f | 3340 | { |
3d7de1fa | 3341 | tree tmpl; |
6757edfe | 3342 | tree tmpl_parms; |
7fe6899f MM |
3343 | int i; |
3344 | ||
3d7de1fa MM |
3345 | if (!TYPE_TEMPLATE_INFO (type)) |
3346 | { | |
0f51ccfc | 3347 | error ("%qT is not a template type", type); |
60feef2c | 3348 | return false; |
3d7de1fa MM |
3349 | } |
3350 | ||
3351 | tmpl = TYPE_TI_TEMPLATE (type); | |
7fe6899f MM |
3352 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
3353 | /* The type is nested in some template class. Nothing to worry | |
3354 | about here; there are no new template parameters for the nested | |
3355 | type. */ | |
60feef2c | 3356 | return true; |
7fe6899f | 3357 | |
ee921ddf MM |
3358 | if (!parms) |
3359 | { | |
3360 | error ("template specifiers not specified in declaration of %qD", | |
3361 | tmpl); | |
60feef2c | 3362 | return false; |
ee921ddf MM |
3363 | } |
3364 | ||
6757edfe MM |
3365 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
3366 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); | |
3367 | ||
7fe6899f MM |
3368 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
3369 | { | |
dee15844 | 3370 | error ("previous declaration %q+D", tmpl); |
f49d8c52 | 3371 | error ("used %d template parameter(s) instead of %d", |
c8094d83 | 3372 | TREE_VEC_LENGTH (tmpl_parms), |
f49d8c52 | 3373 | TREE_VEC_LENGTH (parms)); |
60feef2c | 3374 | return false; |
7fe6899f MM |
3375 | } |
3376 | ||
3377 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) | |
3378 | { | |
2d8ba2c7 LM |
3379 | tree tmpl_parm; |
3380 | tree parm; | |
3381 | tree tmpl_default; | |
3382 | tree parm_default; | |
3383 | ||
3384 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node | |
3385 | || TREE_VEC_ELT (parms, i) == error_mark_node) | |
3386 | continue; | |
3387 | ||
3388 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); | |
3389 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); | |
3390 | tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)); | |
3391 | parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); | |
7fe6899f | 3392 | |
2649701f KL |
3393 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
3394 | TEMPLATE_DECL. */ | |
0f67a82f LM |
3395 | if (tmpl_parm != error_mark_node |
3396 | && (TREE_CODE (tmpl_parm) != TREE_CODE (parm) | |
3397 | || (TREE_CODE (tmpl_parm) != TYPE_DECL | |
3398 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))) | |
7fe6899f | 3399 | { |
dee15844 | 3400 | error ("template parameter %q+#D", tmpl_parm); |
0f51ccfc | 3401 | error ("redeclared here as %q#D", parm); |
60feef2c | 3402 | return false; |
7fe6899f MM |
3403 | } |
3404 | ||
3405 | if (tmpl_default != NULL_TREE && parm_default != NULL_TREE) | |
3406 | { | |
3407 | /* We have in [temp.param]: | |
3408 | ||
3409 | A template-parameter may not be given default arguments | |
3410 | by two different declarations in the same scope. */ | |
0f51ccfc | 3411 | error ("redefinition of default argument for %q#D", parm); |
ddd2d57e | 3412 | error ("%J original definition appeared here", tmpl_parm); |
60feef2c | 3413 | return false; |
7fe6899f MM |
3414 | } |
3415 | ||
3416 | if (parm_default != NULL_TREE) | |
3417 | /* Update the previous template parameters (which are the ones | |
3418 | that will really count) with the new default value. */ | |
3419 | TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default; | |
7ce74c5e MM |
3420 | else if (tmpl_default != NULL_TREE) |
3421 | /* Update the new parameters, too; they'll be used as the | |
3422 | parameters for any members. */ | |
3423 | TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default; | |
7fe6899f | 3424 | } |
60feef2c LM |
3425 | |
3426 | return true; | |
7fe6899f | 3427 | } |
75650646 | 3428 | |
9baa27a9 MM |
3429 | /* Simplify EXPR if it is a non-dependent expression. Returns the |
3430 | (possibly simplified) expression. */ | |
3431 | ||
3432 | tree | |
3433 | fold_non_dependent_expr (tree expr) | |
3434 | { | |
d4a200d3 SM |
3435 | if (expr == NULL_TREE) |
3436 | return NULL_TREE; | |
3437 | ||
9baa27a9 MM |
3438 | /* If we're in a template, but EXPR isn't value dependent, simplify |
3439 | it. We're supposed to treat: | |
c8094d83 | 3440 | |
9baa27a9 MM |
3441 | template <typename T> void f(T[1 + 1]); |
3442 | template <typename T> void f(T[2]); | |
c8094d83 | 3443 | |
9baa27a9 MM |
3444 | as two declarations of the same function, for example. */ |
3445 | if (processing_template_decl | |
3446 | && !type_dependent_expression_p (expr) | |
7416ab02 | 3447 | && !value_dependent_expression_p (expr)) |
9baa27a9 MM |
3448 | { |
3449 | HOST_WIDE_INT saved_processing_template_decl; | |
3450 | ||
3451 | saved_processing_template_decl = processing_template_decl; | |
3452 | processing_template_decl = 0; | |
3453 | expr = tsubst_copy_and_build (expr, | |
3454 | /*args=*/NULL_TREE, | |
3455 | tf_error, | |
3456 | /*in_decl=*/NULL_TREE, | |
015c2c66 MM |
3457 | /*function_p=*/false, |
3458 | /*integral_constant_expression_p=*/true); | |
9baa27a9 MM |
3459 | processing_template_decl = saved_processing_template_decl; |
3460 | } | |
3461 | return expr; | |
3462 | } | |
3463 | ||
b6ab6892 GB |
3464 | /* EXPR is an expression which is used in a constant-expression context. |
3465 | For instance, it could be a VAR_DECL with a constant initializer. | |
3466 | Extract the innest constant expression. | |
c8094d83 | 3467 | |
8a784e4a NS |
3468 | This is basically a more powerful version of |
3469 | integral_constant_value, which can be used also in templates where | |
3470 | initializers can maintain a syntactic rather than semantic form | |
3471 | (even if they are non-dependent, for access-checking purposes). */ | |
b6ab6892 | 3472 | |
993acaec | 3473 | static tree |
b6ab6892 GB |
3474 | fold_decl_constant_value (tree expr) |
3475 | { | |
4ef69b83 GB |
3476 | tree const_expr = expr; |
3477 | do | |
b6ab6892 | 3478 | { |
b6ab6892 | 3479 | expr = fold_non_dependent_expr (const_expr); |
4ef69b83 | 3480 | const_expr = integral_constant_value (expr); |
b6ab6892 | 3481 | } |
4ef69b83 | 3482 | while (expr != const_expr); |
b6ab6892 | 3483 | |
8a784e4a | 3484 | return expr; |
b6ab6892 GB |
3485 | } |
3486 | ||
3487 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which | |
3488 | must be a function or a pointer-to-function type, as specified | |
3489 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, | |
3490 | and check that the resulting function has external linkage. */ | |
3491 | ||
3492 | static tree | |
3493 | convert_nontype_argument_function (tree type, tree expr) | |
3494 | { | |
3495 | tree fns = expr; | |
3496 | tree fn, fn_no_ptr; | |
3497 | ||
3498 | fn = instantiate_type (type, fns, tf_none); | |
3499 | if (fn == error_mark_node) | |
3500 | return error_mark_node; | |
3501 | ||
3502 | fn_no_ptr = fn; | |
3503 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) | |
3504 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); | |
eff3a276 MM |
3505 | if (TREE_CODE (fn_no_ptr) == BASELINK) |
3506 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); | |
3507 | ||
b6ab6892 GB |
3508 | /* [temp.arg.nontype]/1 |
3509 | ||
3510 | A template-argument for a non-type, non-template template-parameter | |
3511 | shall be one of: | |
3512 | [...] | |
3513 | -- the address of an object or function with external linkage. */ | |
3514 | if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr)) | |
3515 | { | |
3516 | error ("%qE is not a valid template argument for type %qT " | |
3517 | "because function %qD has not external linkage", | |
3518 | expr, type, fn_no_ptr); | |
3519 | return NULL_TREE; | |
3520 | } | |
3521 | ||
3522 | return fn; | |
3523 | } | |
3524 | ||
75650646 MM |
3525 | /* Attempt to convert the non-type template parameter EXPR to the |
3526 | indicated TYPE. If the conversion is successful, return the | |
dc957d14 | 3527 | converted value. If the conversion is unsuccessful, return |
75650646 MM |
3528 | NULL_TREE if we issued an error message, or error_mark_node if we |
3529 | did not. We issue error messages for out-and-out bad template | |
3530 | parameters, but not simply because the conversion failed, since we | |
9baa27a9 | 3531 | might be just trying to do argument deduction. Both TYPE and EXPR |
b6ab6892 GB |
3532 | must be non-dependent. |
3533 | ||
3534 | The conversion follows the special rules described in | |
3535 | [temp.arg.nontype], and it is much more strict than an implicit | |
3536 | conversion. | |
3537 | ||
3538 | This function is called twice for each template argument (see | |
3539 | lookup_template_class for a more accurate description of this | |
3540 | problem). This means that we need to handle expressions which | |
3541 | are not valid in a C++ source, but can be created from the | |
3542 | first call (for instance, casts to perform conversions). These | |
3543 | hacks can go away after we fix the double coercion problem. */ | |
75650646 MM |
3544 | |
3545 | static tree | |
3a978d72 | 3546 | convert_nontype_argument (tree type, tree expr) |
75650646 | 3547 | { |
9baa27a9 MM |
3548 | tree expr_type; |
3549 | ||
b6ab6892 GB |
3550 | /* Detect immediately string literals as invalid non-type argument. |
3551 | This special-case is not needed for correctness (we would easily | |
3552 | catch this later), but only to provide better diagnostic for this | |
3553 | common user mistake. As suggested by DR 100, we do not mention | |
3554 | linkage issues in the diagnostic as this is not the point. */ | |
3555 | if (TREE_CODE (expr) == STRING_CST) | |
3556 | { | |
3557 | error ("%qE is not a valid template argument for type %qT " | |
3558 | "because string literals can never be used in this context", | |
3559 | expr, type); | |
3560 | return NULL_TREE; | |
3561 | } | |
3562 | ||
9baa27a9 MM |
3563 | /* If we are in a template, EXPR may be non-dependent, but still |
3564 | have a syntactic, rather than semantic, form. For example, EXPR | |
3565 | might be a SCOPE_REF, rather than the VAR_DECL to which the | |
3566 | SCOPE_REF refers. Preserving the qualifying scope is necessary | |
3567 | so that access checking can be performed when the template is | |
3568 | instantiated -- but here we need the resolved form so that we can | |
3569 | convert the argument. */ | |
3570 | expr = fold_non_dependent_expr (expr); | |
b166a559 VR |
3571 | if (error_operand_p (expr)) |
3572 | return error_mark_node; | |
9baa27a9 | 3573 | expr_type = TREE_TYPE (expr); |
75650646 | 3574 | |
b6ab6892 GB |
3575 | /* HACK: Due to double coercion, we can get a |
3576 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, | |
3577 | which is the tree that we built on the first call (see | |
3578 | below when coercing to reference to object or to reference to | |
3579 | function). We just strip everything and get to the arg. | |
3580 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C | |
3581 | for examples. */ | |
3582 | if (TREE_CODE (expr) == NOP_EXPR) | |
75650646 | 3583 | { |
b6ab6892 | 3584 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
75650646 | 3585 | { |
b6ab6892 GB |
3586 | /* ??? Maybe we could use convert_from_reference here, but we |
3587 | would need to relax its constraints because the NOP_EXPR | |
3588 | could actually change the type to something more cv-qualified, | |
3589 | and this is not folded by convert_from_reference. */ | |
3590 | tree addr = TREE_OPERAND (expr, 0); | |
3591 | gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE); | |
3592 | gcc_assert (TREE_CODE (addr) == ADDR_EXPR); | |
3593 | gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE); | |
3594 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
3595 | (TREE_TYPE (expr_type), | |
3596 | TREE_TYPE (TREE_TYPE (addr)))); | |
3597 | ||
3598 | expr = TREE_OPERAND (addr, 0); | |
3599 | expr_type = TREE_TYPE (expr); | |
75650646 MM |
3600 | } |
3601 | ||
b6ab6892 GB |
3602 | /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the |
3603 | parameter is a pointer to object, through decay and | |
3604 | qualification conversion. Let's strip everything. */ | |
3605 | else if (TYPE_PTROBV_P (type)) | |
75650646 | 3606 | { |
b6ab6892 GB |
3607 | STRIP_NOPS (expr); |
3608 | gcc_assert (TREE_CODE (expr) == ADDR_EXPR); | |
3609 | gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE); | |
3610 | /* Skip the ADDR_EXPR only if it is part of the decay for | |
3611 | an array. Otherwise, it is part of the original argument | |
3612 | in the source code. */ | |
3613 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE) | |
3614 | expr = TREE_OPERAND (expr, 0); | |
3615 | expr_type = TREE_TYPE (expr); | |
f3400fe2 | 3616 | } |
75650646 | 3617 | } |
c61dce3a | 3618 | |
b6ab6892 | 3619 | /* [temp.arg.nontype]/5, bullet 1 |
75650646 | 3620 | |
b6ab6892 GB |
3621 | For a non-type template-parameter of integral or enumeration type, |
3622 | integral promotions (_conv.prom_) and integral conversions | |
3623 | (_conv.integral_) are applied. */ | |
3624 | if (INTEGRAL_TYPE_P (type)) | |
75650646 | 3625 | { |
75650646 MM |
3626 | if (!INTEGRAL_TYPE_P (expr_type)) |
3627 | return error_mark_node; | |
fddabb2c | 3628 | |
b6ab6892 GB |
3629 | expr = fold_decl_constant_value (expr); |
3630 | /* Notice that there are constant expressions like '4 % 0' which | |
3631 | do not fold into integer constants. */ | |
db02b6b9 | 3632 | if (TREE_CODE (expr) != INTEGER_CST) |
b6ab6892 GB |
3633 | { |
3634 | error ("%qE is not a valid template argument for type %qT " | |
3635 | "because it is a non-constant expression", expr, type); | |
3636 | return NULL_TREE; | |
3637 | } | |
75650646 | 3638 | |
b6ab6892 GB |
3639 | /* At this point, an implicit conversion does what we want, |
3640 | because we already know that the expression is of integral | |
3641 | type. */ | |
3642 | expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT); | |
3643 | if (expr == error_mark_node) | |
3644 | return error_mark_node; | |
75650646 | 3645 | |
b6ab6892 GB |
3646 | /* Conversion was allowed: fold it to a bare integer constant. */ |
3647 | expr = fold (expr); | |
3648 | } | |
3649 | /* [temp.arg.nontype]/5, bullet 2 | |
75650646 | 3650 | |
b6ab6892 GB |
3651 | For a non-type template-parameter of type pointer to object, |
3652 | qualification conversions (_conv.qual_) and the array-to-pointer | |
3653 | conversion (_conv.array_) are applied. */ | |
3654 | else if (TYPE_PTROBV_P (type)) | |
3655 | { | |
3656 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): | |
75650646 | 3657 | |
b6ab6892 GB |
3658 | A template-argument for a non-type, non-template template-parameter |
3659 | shall be one of: [...] | |
75650646 | 3660 | |
b6ab6892 GB |
3661 | -- the name of a non-type template-parameter; |
3662 | -- the address of an object or function with external linkage, [...] | |
3663 | expressed as "& id-expression" where the & is optional if the name | |
3664 | refers to a function or array, or if the corresponding | |
3665 | template-parameter is a reference. | |
c8094d83 | 3666 | |
b6ab6892 GB |
3667 | Here, we do not care about functions, as they are invalid anyway |
3668 | for a parameter of type pointer-to-object. */ | |
1082fd10 MM |
3669 | |
3670 | if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)) | |
3671 | /* Non-type template parameters are OK. */ | |
3672 | ; | |
3673 | else if (TREE_CODE (expr) != ADDR_EXPR | |
3674 | && TREE_CODE (expr_type) != ARRAY_TYPE) | |
3675 | { | |
3676 | if (TREE_CODE (expr) == VAR_DECL) | |
3677 | { | |
3678 | error ("%qD is not a valid template argument " | |
3679 | "because %qD is a variable, not the address of " | |
3680 | "a variable", | |
3681 | expr, expr); | |
3682 | return NULL_TREE; | |
3683 | } | |
3684 | /* Other values, like integer constants, might be valid | |
3685 | non-type arguments of some other type. */ | |
3686 | return error_mark_node; | |
3687 | } | |
3688 | else | |
3689 | { | |
3690 | tree decl; | |
3691 | ||
3692 | decl = ((TREE_CODE (expr) == ADDR_EXPR) | |
3693 | ? TREE_OPERAND (expr, 0) : expr); | |
3694 | if (TREE_CODE (decl) != VAR_DECL) | |
3695 | { | |
3696 | error ("%qE is not a valid template argument of type %qT " | |
3697 | "because %qE is not a variable", | |
3698 | expr, type, decl); | |
3699 | return NULL_TREE; | |
3700 | } | |
3701 | else if (!DECL_EXTERNAL_LINKAGE_P (decl)) | |
3702 | { | |
3703 | error ("%qE is not a valid template argument of type %qT " | |
3704 | "because %qD does not have external linkage", | |
3705 | expr, type, decl); | |
3706 | return NULL_TREE; | |
3707 | } | |
3708 | } | |
b6ab6892 GB |
3709 | |
3710 | expr = decay_conversion (expr); | |
3711 | if (expr == error_mark_node) | |
3712 | return error_mark_node; | |
75650646 | 3713 | |
b6ab6892 GB |
3714 | expr = perform_qualification_conversions (type, expr); |
3715 | if (expr == error_mark_node) | |
3716 | return error_mark_node; | |
b6ab6892 GB |
3717 | } |
3718 | /* [temp.arg.nontype]/5, bullet 3 | |
3719 | ||
3720 | For a non-type template-parameter of type reference to object, no | |
3721 | conversions apply. The type referred to by the reference may be more | |
3722 | cv-qualified than the (otherwise identical) type of the | |
3723 | template-argument. The template-parameter is bound directly to the | |
3724 | template-argument, which must be an lvalue. */ | |
3725 | else if (TYPE_REF_OBJ_P (type)) | |
3726 | { | |
3727 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), | |
3728 | expr_type)) | |
3729 | return error_mark_node; | |
75650646 | 3730 | |
b6ab6892 GB |
3731 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
3732 | { | |
3733 | error ("%qE is not a valid template argument for type %qT " | |
3734 | "because of conflicts in cv-qualification", expr, type); | |
3735 | return NULL_TREE; | |
3736 | } | |
c8094d83 | 3737 | |
b6ab6892 GB |
3738 | if (!real_lvalue_p (expr)) |
3739 | { | |
3740 | error ("%qE is not a valid template argument for type %qT " | |
944fd207 | 3741 | "because it is not an lvalue", expr, type); |
b6ab6892 GB |
3742 | return NULL_TREE; |
3743 | } | |
e6e174e5 | 3744 | |
b6ab6892 | 3745 | /* [temp.arg.nontype]/1 |
75650646 | 3746 | |
b6ab6892 GB |
3747 | A template-argument for a non-type, non-template template-parameter |
3748 | shall be one of: [...] | |
75650646 | 3749 | |
03fd3f84 | 3750 | -- the address of an object or function with external linkage. */ |
b6ab6892 GB |
3751 | if (!DECL_EXTERNAL_LINKAGE_P (expr)) |
3752 | { | |
3753 | error ("%qE is not a valid template argument for type %qT " | |
3754 | "because object %qD has not external linkage", | |
3755 | expr, type, expr); | |
3756 | return NULL_TREE; | |
3757 | } | |
0dc09a61 | 3758 | |
b6ab6892 GB |
3759 | expr = build_nop (type, build_address (expr)); |
3760 | } | |
3761 | /* [temp.arg.nontype]/5, bullet 4 | |
3762 | ||
3763 | For a non-type template-parameter of type pointer to function, only | |
3764 | the function-to-pointer conversion (_conv.func_) is applied. If the | |
3765 | template-argument represents a set of overloaded functions (or a | |
3766 | pointer to such), the matching function is selected from the set | |
3767 | (_over.over_). */ | |
3768 | else if (TYPE_PTRFN_P (type)) | |
3769 | { | |
3770 | /* If the argument is a template-id, we might not have enough | |
2c164de6 | 3771 | context information to decay the pointer. */ |
b6ab6892 GB |
3772 | if (!type_unknown_p (expr_type)) |
3773 | { | |
3774 | expr = decay_conversion (expr); | |
3775 | if (expr == error_mark_node) | |
3776 | return error_mark_node; | |
3777 | } | |
75650646 | 3778 | |
b6ab6892 GB |
3779 | expr = convert_nontype_argument_function (type, expr); |
3780 | if (!expr || expr == error_mark_node) | |
3781 | return expr; | |
3782 | } | |
3783 | /* [temp.arg.nontype]/5, bullet 5 | |
75650646 | 3784 | |
b6ab6892 GB |
3785 | For a non-type template-parameter of type reference to function, no |
3786 | conversions apply. If the template-argument represents a set of | |
3787 | overloaded functions, the matching function is selected from the set | |
3788 | (_over.over_). */ | |
3789 | else if (TYPE_REFFN_P (type)) | |
3790 | { | |
3791 | if (TREE_CODE (expr) == ADDR_EXPR) | |
3792 | { | |
3793 | error ("%qE is not a valid template argument for type %qT " | |
3794 | "because it is a pointer", expr, type); | |
3795 | inform ("try using %qE instead", TREE_OPERAND (expr, 0)); | |
3796 | return NULL_TREE; | |
3797 | } | |
75650646 | 3798 | |
b6ab6892 GB |
3799 | expr = convert_nontype_argument_function (TREE_TYPE (type), expr); |
3800 | if (!expr || expr == error_mark_node) | |
3801 | return expr; | |
75650646 | 3802 | |
7866705a | 3803 | expr = build_nop (type, build_address (expr)); |
b6ab6892 GB |
3804 | } |
3805 | /* [temp.arg.nontype]/5, bullet 6 | |
75650646 | 3806 | |
b6ab6892 GB |
3807 | For a non-type template-parameter of type pointer to member function, |
3808 | no conversions apply. If the template-argument represents a set of | |
3809 | overloaded member functions, the matching member function is selected | |
3810 | from the set (_over.over_). */ | |
3811 | else if (TYPE_PTRMEMFUNC_P (type)) | |
3812 | { | |
3813 | expr = instantiate_type (type, expr, tf_none); | |
3814 | if (expr == error_mark_node) | |
3815 | return error_mark_node; | |
75650646 | 3816 | |
b6ab6892 GB |
3817 | /* There is no way to disable standard conversions in |
3818 | resolve_address_of_overloaded_function (called by | |
3819 | instantiate_type). It is possible that the call succeeded by | |
3820 | converting &B::I to &D::I (where B is a base of D), so we need | |
3821 | to reject this conversion here. | |
75650646 | 3822 | |
b6ab6892 GB |
3823 | Actually, even if there was a way to disable standard conversions, |
3824 | it would still be better to reject them here so that we can | |
3825 | provide a superior diagnostic. */ | |
3826 | if (!same_type_p (TREE_TYPE (expr), type)) | |
3827 | { | |
3828 | /* Make sure we are just one standard conversion off. */ | |
3829 | gcc_assert (can_convert (type, TREE_TYPE (expr))); | |
3830 | error ("%qE is not a valid template argument for type %qT " | |
3831 | "because it is of type %qT", expr, type, | |
3832 | TREE_TYPE (expr)); | |
3833 | inform ("standard conversions are not allowed in this context"); | |
3834 | return NULL_TREE; | |
3835 | } | |
3836 | } | |
3837 | /* [temp.arg.nontype]/5, bullet 7 | |
59e7a901 | 3838 | |
b6ab6892 GB |
3839 | For a non-type template-parameter of type pointer to data member, |
3840 | qualification conversions (_conv.qual_) are applied. */ | |
3841 | else if (TYPE_PTRMEM_P (type)) | |
3842 | { | |
3843 | expr = perform_qualification_conversions (type, expr); | |
3844 | if (expr == error_mark_node) | |
75650646 | 3845 | return expr; |
75650646 | 3846 | } |
b6ab6892 GB |
3847 | /* A template non-type parameter must be one of the above. */ |
3848 | else | |
3849 | gcc_unreachable (); | |
75650646 | 3850 | |
b6ab6892 GB |
3851 | /* Sanity check: did we actually convert the argument to the |
3852 | right type? */ | |
3853 | gcc_assert (same_type_p (type, TREE_TYPE (expr))); | |
3854 | return expr; | |
75650646 MM |
3855 | } |
3856 | ||
b6ab6892 | 3857 | |
c8094d83 MS |
3858 | /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for |
3859 | template template parameters. Both PARM_PARMS and ARG_PARMS are | |
3860 | vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL | |
744fac59 | 3861 | or PARM_DECL. |
c8094d83 | 3862 | |
744fac59 | 3863 | Consider the example: |
e7e93965 MM |
3864 | template <class T> class A; |
3865 | template<template <class U> class TT> class B; | |
744fac59 | 3866 | |
e7e93965 MM |
3867 | For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are |
3868 | the parameters to A, and OUTER_ARGS contains A. */ | |
744fac59 KL |
3869 | |
3870 | static int | |
c8094d83 | 3871 | coerce_template_template_parms (tree parm_parms, |
0cbd7506 MS |
3872 | tree arg_parms, |
3873 | tsubst_flags_t complain, | |
3a978d72 | 3874 | tree in_decl, |
0cbd7506 | 3875 | tree outer_args) |
744fac59 KL |
3876 | { |
3877 | int nparms, nargs, i; | |
3878 | tree parm, arg; | |
3879 | ||
50bc768d NS |
3880 | gcc_assert (TREE_CODE (parm_parms) == TREE_VEC); |
3881 | gcc_assert (TREE_CODE (arg_parms) == TREE_VEC); | |
744fac59 KL |
3882 | |
3883 | nparms = TREE_VEC_LENGTH (parm_parms); | |
3884 | nargs = TREE_VEC_LENGTH (arg_parms); | |
3885 | ||
e7e93965 | 3886 | if (nargs != nparms) |
744fac59 KL |
3887 | return 0; |
3888 | ||
3889 | for (i = 0; i < nparms; ++i) | |
3890 | { | |
2d8ba2c7 LM |
3891 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
3892 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) | |
42b304f1 LM |
3893 | continue; |
3894 | ||
744fac59 KL |
3895 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
3896 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); | |
3897 | ||
3898 | if (arg == NULL_TREE || arg == error_mark_node | |
0cbd7506 | 3899 | || parm == NULL_TREE || parm == error_mark_node) |
744fac59 KL |
3900 | return 0; |
3901 | ||
3902 | if (TREE_CODE (arg) != TREE_CODE (parm)) | |
0cbd7506 | 3903 | return 0; |
744fac59 KL |
3904 | |
3905 | switch (TREE_CODE (parm)) | |
3906 | { | |
3907 | case TYPE_DECL: | |
3908 | break; | |
3909 | ||
3910 | case TEMPLATE_DECL: | |
3911 | /* We encounter instantiations of templates like | |
3912 | template <template <template <class> class> class TT> | |
3913 | class C; */ | |
700466c2 JM |
3914 | { |
3915 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); | |
3916 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
3917 | ||
8c6ab2db NS |
3918 | if (!coerce_template_template_parms |
3919 | (parmparm, argparm, complain, in_decl, outer_args)) | |
700466c2 JM |
3920 | return 0; |
3921 | } | |
3922 | break; | |
744fac59 KL |
3923 | |
3924 | case PARM_DECL: | |
3925 | /* The tsubst call is used to handle cases such as | |
00bdb87f KL |
3926 | |
3927 | template <int> class C {}; | |
3928 | template <class T, template <T> class TT> class D {}; | |
3929 | D<int, C> d; | |
3930 | ||
744fac59 | 3931 | i.e. the parameter list of TT depends on earlier parameters. */ |
00bdb87f KL |
3932 | if (!dependent_type_p (TREE_TYPE (arg)) |
3933 | && !same_type_p | |
3934 | (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl), | |
3935 | TREE_TYPE (arg))) | |
744fac59 KL |
3936 | return 0; |
3937 | break; | |
c8094d83 | 3938 | |
744fac59 | 3939 | default: |
315fb5db | 3940 | gcc_unreachable (); |
744fac59 KL |
3941 | } |
3942 | } | |
3943 | return 1; | |
3944 | } | |
3945 | ||
8b5b8b7c MM |
3946 | /* Convert the indicated template ARG as necessary to match the |
3947 | indicated template PARM. Returns the converted ARG, or | |
c2ea3a40 NS |
3948 | error_mark_node if the conversion was unsuccessful. Error and |
3949 | warning messages are issued under control of COMPLAIN. This | |
3950 | conversion is for the Ith parameter in the parameter list. ARGS is | |
3951 | the full set of template arguments deduced so far. */ | |
8b5b8b7c MM |
3952 | |
3953 | static tree | |
c8094d83 | 3954 | convert_template_argument (tree parm, |
0cbd7506 MS |
3955 | tree arg, |
3956 | tree args, | |
3957 | tsubst_flags_t complain, | |
3958 | int i, | |
3959 | tree in_decl) | |
8b5b8b7c MM |
3960 | { |
3961 | tree val; | |
8b5b8b7c | 3962 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
c8094d83 | 3963 | |
c8094d83 | 3964 | if (TREE_CODE (arg) == TREE_LIST |
a5ac359a | 3965 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
c8094d83 | 3966 | { |
8b5b8b7c MM |
3967 | /* The template argument was the name of some |
3968 | member function. That's usually | |
0e339752 | 3969 | invalid, but static members are OK. In any |
8b5b8b7c MM |
3970 | case, grab the underlying fields/functions |
3971 | and issue an error later if required. */ | |
3972 | arg = TREE_VALUE (arg); | |
3973 | TREE_TYPE (arg) = unknown_type_node; | |
3974 | } | |
3975 | ||
3976 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; | |
3977 | requires_type = (TREE_CODE (parm) == TYPE_DECL | |
3978 | || requires_tmpl_type); | |
3979 | ||
cbd63935 KL |
3980 | is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL |
3981 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) | |
3982 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
3983 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); | |
c8094d83 | 3984 | |
b8c6534b KL |
3985 | if (is_tmpl_type |
3986 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
3987 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) | |
8b5b8b7c | 3988 | arg = TYPE_STUB_DECL (arg); |
8b5b8b7c | 3989 | |
2f939d94 | 3990 | is_type = TYPE_P (arg) || is_tmpl_type; |
8b5b8b7c MM |
3991 | |
3992 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF | |
3993 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) | |
3994 | { | |
0f51ccfc | 3995 | pedwarn ("to refer to a type member of a template parameter, " |
0cbd7506 | 3996 | "use %<typename %E%>", arg); |
c8094d83 | 3997 | |
8b5b8b7c | 3998 | arg = make_typename_type (TREE_OPERAND (arg, 0), |
3baa501d | 3999 | TREE_OPERAND (arg, 1), |
fc6a28d7 | 4000 | typename_type, |
c2ea3a40 | 4001 | complain & tf_error); |
8b5b8b7c MM |
4002 | is_type = 1; |
4003 | } | |
4004 | if (is_type != requires_type) | |
4005 | { | |
4006 | if (in_decl) | |
4007 | { | |
c2ea3a40 | 4008 | if (complain & tf_error) |
8b5b8b7c | 4009 | { |
0f51ccfc | 4010 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4011 | "parameter list for %qD", |
4012 | i + 1, in_decl); | |
8b5b8b7c | 4013 | if (is_type) |
0f51ccfc | 4014 | error (" expected a constant of type %qT, got %qT", |
0cbd7506 MS |
4015 | TREE_TYPE (parm), |
4016 | (is_tmpl_type ? DECL_NAME (arg) : arg)); | |
d12a7283 | 4017 | else if (requires_tmpl_type) |
0f51ccfc | 4018 | error (" expected a class template, got %qE", arg); |
8b5b8b7c | 4019 | else |
0f51ccfc | 4020 | error (" expected a type, got %qE", arg); |
8b5b8b7c MM |
4021 | } |
4022 | } | |
4023 | return error_mark_node; | |
4024 | } | |
4025 | if (is_tmpl_type ^ requires_tmpl_type) | |
4026 | { | |
c2ea3a40 | 4027 | if (in_decl && (complain & tf_error)) |
8b5b8b7c | 4028 | { |
0f51ccfc | 4029 | error ("type/value mismatch at argument %d in template " |
0cbd7506 MS |
4030 | "parameter list for %qD", |
4031 | i + 1, in_decl); | |
8b5b8b7c | 4032 | if (is_tmpl_type) |
0f51ccfc | 4033 | error (" expected a type, got %qT", DECL_NAME (arg)); |
8b5b8b7c | 4034 | else |
0f51ccfc | 4035 | error (" expected a class template, got %qT", arg); |
8b5b8b7c MM |
4036 | } |
4037 | return error_mark_node; | |
4038 | } | |
c8094d83 | 4039 | |
8b5b8b7c MM |
4040 | if (is_type) |
4041 | { | |
4042 | if (requires_tmpl_type) | |
4043 | { | |
b8c6534b KL |
4044 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
4045 | /* The number of argument required is not known yet. | |
4046 | Just accept it for now. */ | |
4047 | val = TREE_TYPE (arg); | |
8b5b8b7c MM |
4048 | else |
4049 | { | |
b8c6534b KL |
4050 | tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); |
4051 | tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); | |
4052 | ||
4053 | if (coerce_template_template_parms (parmparm, argparm, | |
4054 | complain, in_decl, | |
6150b602 | 4055 | args)) |
8b5b8b7c | 4056 | { |
b8c6534b | 4057 | val = arg; |
c8094d83 MS |
4058 | |
4059 | /* TEMPLATE_TEMPLATE_PARM node is preferred over | |
b8c6534b | 4060 | TEMPLATE_DECL. */ |
c8094d83 | 4061 | if (val != error_mark_node |
b8c6534b KL |
4062 | && DECL_TEMPLATE_TEMPLATE_PARM_P (val)) |
4063 | val = TREE_TYPE (val); | |
8b5b8b7c | 4064 | } |
b8c6534b KL |
4065 | else |
4066 | { | |
c2ea3a40 | 4067 | if (in_decl && (complain & tf_error)) |
b8c6534b | 4068 | { |
0f51ccfc | 4069 | error ("type/value mismatch at argument %d in " |
0cbd7506 MS |
4070 | "template parameter list for %qD", |
4071 | i + 1, in_decl); | |
0f51ccfc | 4072 | error (" expected a template of type %qD, got %qD", |
0cbd7506 | 4073 | parm, arg); |
b8c6534b | 4074 | } |
c8094d83 | 4075 | |
b8c6534b KL |
4076 | val = error_mark_node; |
4077 | } | |
8b5b8b7c MM |
4078 | } |
4079 | } | |
4080 | else | |
058b15c1 | 4081 | val = arg; |
685e39c2 MM |
4082 | /* We only form one instance of each template specialization. |
4083 | Therefore, if we use a non-canonical variant (i.e., a | |
3db45ab5 | 4084 | typedef), any future messages referring to the type will use |
685e39c2 MM |
4085 | the typedef, which is confusing if those future uses do not |
4086 | themselves also use the typedef. */ | |
4087 | if (TYPE_P (val)) | |
4088 | val = canonical_type_variant (val); | |
8b5b8b7c MM |
4089 | } |
4090 | else | |
4091 | { | |
4393e105 | 4092 | tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl); |
8b5b8b7c | 4093 | |
11b810f1 | 4094 | if (invalid_nontype_parm_type_p (t, complain)) |
0cbd7506 | 4095 | return error_mark_node; |
c8094d83 | 4096 | |
d768a589 | 4097 | if (!uses_template_parms (arg) && !uses_template_parms (t)) |
8b5b8b7c MM |
4098 | /* We used to call digest_init here. However, digest_init |
4099 | will report errors, which we don't want when complain | |
4100 | is zero. More importantly, digest_init will try too | |
4101 | hard to convert things: for example, `0' should not be | |
4102 | converted to pointer type at this point according to | |
4103 | the standard. Accepting this is not merely an | |
4104 | extension, since deciding whether or not these | |
4105 | conversions can occur is part of determining which | |
dc957d14 | 4106 | function template to call, or whether a given explicit |
0e339752 | 4107 | argument specification is valid. */ |
8b5b8b7c MM |
4108 | val = convert_nontype_argument (t, arg); |
4109 | else | |
4110 | val = arg; | |
4111 | ||
4112 | if (val == NULL_TREE) | |
4113 | val = error_mark_node; | |
c2ea3a40 | 4114 | else if (val == error_mark_node && (complain & tf_error)) |
0f51ccfc | 4115 | error ("could not convert template argument %qE to %qT", arg, t); |
8b5b8b7c MM |
4116 | } |
4117 | ||
4118 | return val; | |
4119 | } | |
4120 | ||
4121 | /* Convert all template arguments to their appropriate types, and | |
4122 | return a vector containing the innermost resulting template | |
c2ea3a40 | 4123 | arguments. If any error occurs, return error_mark_node. Error and |
3e4a3562 | 4124 | warning messages are issued under control of COMPLAIN. |
75650646 | 4125 | |
e7e93965 | 4126 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
84dc00e8 | 4127 | for arguments not specified in ARGS. Otherwise, if |
e7e93965 MM |
4128 | USE_DEFAULT_ARGS is true, default arguments will be used to fill in |
4129 | unspecified arguments. If REQUIRE_ALL_ARGS is true, but | |
4130 | USE_DEFAULT_ARGS is false, then all arguments must be specified in | |
4131 | ARGS. */ | |
c8094d83 | 4132 | |
8d08fdba | 4133 | static tree |
c8094d83 | 4134 | coerce_template_parms (tree parms, |
0cbd7506 MS |
4135 | tree args, |
4136 | tree in_decl, | |
3a978d72 | 4137 | tsubst_flags_t complain, |
e7e93965 MM |
4138 | bool require_all_args, |
4139 | bool use_default_args) | |
8d08fdba | 4140 | { |
a292b002 | 4141 | int nparms, nargs, i, lost = 0; |
e4a84209 | 4142 | tree inner_args; |
8b5b8b7c MM |
4143 | tree new_args; |
4144 | tree new_inner_args; | |
a2c5ed87 | 4145 | bool saved_skip_evaluation; |
a292b002 | 4146 | |
f9a7ae04 | 4147 | inner_args = INNERMOST_TEMPLATE_ARGS (args); |
bf12d54d | 4148 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
a292b002 MS |
4149 | nparms = TREE_VEC_LENGTH (parms); |
4150 | ||
4151 | if (nargs > nparms | |
4152 | || (nargs < nparms | |
e7e93965 MM |
4153 | && require_all_args |
4154 | && (!use_default_args | |
42b304f1 LM |
4155 | || (TREE_VEC_ELT (parms, nargs) != error_mark_node |
4156 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)))))) | |
8d08fdba | 4157 | { |
c8094d83 | 4158 | if (complain & tf_error) |
75650646 | 4159 | { |
33bd39a2 | 4160 | error ("wrong number of template arguments (%d, should be %d)", |
0cbd7506 | 4161 | nargs, nparms); |
c8094d83 | 4162 | |
75650646 | 4163 | if (in_decl) |
dee15844 | 4164 | error ("provided for %q+D", in_decl); |
75650646 MM |
4165 | } |
4166 | ||
8d08fdba MS |
4167 | return error_mark_node; |
4168 | } | |
4169 | ||
a2c5ed87 MM |
4170 | /* We need to evaluate the template arguments, even though this |
4171 | template-id may be nested within a "sizeof". */ | |
4172 | saved_skip_evaluation = skip_evaluation; | |
4173 | skip_evaluation = false; | |
f31c0a32 | 4174 | new_inner_args = make_tree_vec (nparms); |
8b5b8b7c MM |
4175 | new_args = add_outermost_template_args (args, new_inner_args); |
4176 | for (i = 0; i < nparms; i++) | |
8d08fdba | 4177 | { |
8b5b8b7c MM |
4178 | tree arg; |
4179 | tree parm; | |
e4a84209 | 4180 | |
8b5b8b7c MM |
4181 | /* Get the Ith template parameter. */ |
4182 | parm = TREE_VEC_ELT (parms, i); | |
42b304f1 LM |
4183 | |
4184 | if (parm == error_mark_node) | |
2d8ba2c7 LM |
4185 | { |
4186 | TREE_VEC_ELT (new_inner_args, i) = error_mark_node; | |
42b304f1 | 4187 | continue; |
2d8ba2c7 | 4188 | } |
75650646 | 4189 | |
8b5b8b7c | 4190 | /* Calculate the Ith argument. */ |
bf12d54d | 4191 | if (i < nargs) |
8b5b8b7c | 4192 | arg = TREE_VEC_ELT (inner_args, i); |
e7e93965 | 4193 | else if (require_all_args) |
04c06002 | 4194 | /* There must be a default arg in this case. */ |
a91db711 NS |
4195 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, |
4196 | complain, in_decl); | |
ffd49b19 NS |
4197 | else |
4198 | break; | |
c8094d83 | 4199 | |
50bc768d | 4200 | gcc_assert (arg); |
ffd49b19 | 4201 | if (arg == error_mark_node) |
e34b0922 KL |
4202 | { |
4203 | if (complain & tf_error) | |
4204 | error ("template argument %d is invalid", i + 1); | |
4205 | } | |
c8094d83 MS |
4206 | else |
4207 | arg = convert_template_argument (TREE_VALUE (parm), | |
8b5b8b7c | 4208 | arg, new_args, complain, i, |
c8094d83 MS |
4209 | in_decl); |
4210 | ||
8b5b8b7c | 4211 | if (arg == error_mark_node) |
8d08fdba | 4212 | lost++; |
8b5b8b7c | 4213 | TREE_VEC_ELT (new_inner_args, i) = arg; |
8d08fdba | 4214 | } |
a2c5ed87 | 4215 | skip_evaluation = saved_skip_evaluation; |
8b5b8b7c | 4216 | |
8d08fdba MS |
4217 | if (lost) |
4218 | return error_mark_node; | |
8b5b8b7c MM |
4219 | |
4220 | return new_inner_args; | |
8d08fdba MS |
4221 | } |
4222 | ||
34016c81 JM |
4223 | /* Returns 1 if template args OT and NT are equivalent. */ |
4224 | ||
d8e178a0 | 4225 | static int |
3a978d72 | 4226 | template_args_equal (tree ot, tree nt) |
34016c81 JM |
4227 | { |
4228 | if (nt == ot) | |
4229 | return 1; | |
74601d7c | 4230 | |
34016c81 JM |
4231 | if (TREE_CODE (nt) == TREE_VEC) |
4232 | /* For member templates */ | |
74601d7c KL |
4233 | return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt); |
4234 | else if (TYPE_P (nt)) | |
660845bf | 4235 | return TYPE_P (ot) && same_type_p (ot, nt); |
74601d7c KL |
4236 | else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot)) |
4237 | return 0; | |
34016c81 | 4238 | else |
c8a209ca | 4239 | return cp_tree_equal (ot, nt); |
34016c81 JM |
4240 | } |
4241 | ||
4242 | /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets | |
f84b4be9 JM |
4243 | of template arguments. Returns 0 otherwise. */ |
4244 | ||
6757edfe | 4245 | int |
3a978d72 | 4246 | comp_template_args (tree oldargs, tree newargs) |
5566b478 MS |
4247 | { |
4248 | int i; | |
4249 | ||
386b8a85 JM |
4250 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
4251 | return 0; | |
4252 | ||
5566b478 MS |
4253 | for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
4254 | { | |
4255 | tree nt = TREE_VEC_ELT (newargs, i); | |
4256 | tree ot = TREE_VEC_ELT (oldargs, i); | |
4257 | ||
34016c81 | 4258 | if (! template_args_equal (ot, nt)) |
61a127b3 | 4259 | return 0; |
5566b478 MS |
4260 | } |
4261 | return 1; | |
4262 | } | |
4263 | ||
8d08fdba MS |
4264 | /* Given class template name and parameter list, produce a user-friendly name |
4265 | for the instantiation. */ | |
e92cc029 | 4266 | |
8d08fdba | 4267 | static char * |
3a978d72 | 4268 | mangle_class_name_for_template (const char* name, tree parms, tree arglist) |
8d08fdba MS |
4269 | { |
4270 | static struct obstack scratch_obstack; | |
4271 | static char *scratch_firstobj; | |
4272 | int i, nparms; | |
8d08fdba MS |
4273 | |
4274 | if (!scratch_firstobj) | |
fc378698 | 4275 | gcc_obstack_init (&scratch_obstack); |
8d08fdba MS |
4276 | else |
4277 | obstack_free (&scratch_obstack, scratch_firstobj); | |
67f5655f | 4278 | scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1); |
8d08fdba | 4279 | |
18e314ad GS |
4280 | #define ccat(C) obstack_1grow (&scratch_obstack, (C)); |
4281 | #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S)) | |
8d08fdba MS |
4282 | |
4283 | cat (name); | |
4284 | ccat ('<'); | |
4285 | nparms = TREE_VEC_LENGTH (parms); | |
f9a7ae04 | 4286 | arglist = INNERMOST_TEMPLATE_ARGS (arglist); |
50bc768d | 4287 | gcc_assert (nparms == TREE_VEC_LENGTH (arglist)); |
8d08fdba MS |
4288 | for (i = 0; i < nparms; i++) |
4289 | { | |
42b304f1 LM |
4290 | tree parm; |
4291 | tree arg; | |
4292 | ||
42b304f1 LM |
4293 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
4294 | arg = TREE_VEC_ELT (arglist, i); | |
8d08fdba | 4295 | |
0f67a82f LM |
4296 | if (parm == error_mark_node) |
4297 | continue; | |
4298 | ||
8d08fdba MS |
4299 | if (i) |
4300 | ccat (','); | |
4301 | ||
a292b002 | 4302 | if (TREE_CODE (parm) == TYPE_DECL) |
8d08fdba | 4303 | { |
761f0855 | 4304 | cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); |
8d08fdba MS |
4305 | continue; |
4306 | } | |
73b0fce8 KL |
4307 | else if (TREE_CODE (parm) == TEMPLATE_DECL) |
4308 | { | |
4309 | if (TREE_CODE (arg) == TEMPLATE_DECL) | |
2c73f9f5 | 4310 | { |
c8094d83 | 4311 | /* Already substituted with real template. Just output |
2c73f9f5 | 4312 | the template name here */ |
0cbd7506 MS |
4313 | tree context = DECL_CONTEXT (arg); |
4314 | if (context) | |
4315 | { | |
4316 | /* The template may be defined in a namespace, or | |
4317 | may be a member template. */ | |
4318 | gcc_assert (TREE_CODE (context) == NAMESPACE_DECL | |
50bc768d NS |
4319 | || CLASS_TYPE_P (context)); |
4320 | cat (decl_as_string (DECL_CONTEXT (arg), | |
4321 | TFF_PLAIN_IDENTIFIER)); | |
4322 | cat ("::"); | |
2c73f9f5 ML |
4323 | } |
4324 | cat (IDENTIFIER_POINTER (DECL_NAME (arg))); | |
4325 | } | |
73b0fce8 | 4326 | else |
f4f206f4 | 4327 | /* Output the parameter declaration. */ |
761f0855 | 4328 | cat (type_as_string (arg, TFF_CHASE_TYPEDEF)); |
73b0fce8 KL |
4329 | continue; |
4330 | } | |
8d08fdba | 4331 | else |
50bc768d | 4332 | gcc_assert (TREE_CODE (parm) == PARM_DECL); |
8d08fdba | 4333 | |
8d08fdba MS |
4334 | /* No need to check arglist against parmlist here; we did that |
4335 | in coerce_template_parms, called from lookup_template_class. */ | |
761f0855 | 4336 | cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER)); |
8d08fdba MS |
4337 | } |
4338 | { | |
4339 | char *bufp = obstack_next_free (&scratch_obstack); | |
4340 | int offset = 0; | |
4341 | while (bufp[offset - 1] == ' ') | |
4342 | offset--; | |
4343 | obstack_blank_fast (&scratch_obstack, offset); | |
4344 | ||
4345 | /* B<C<char> >, not B<C<char>> */ | |
4346 | if (bufp[offset - 1] == '>') | |
4347 | ccat (' '); | |
4348 | } | |
4349 | ccat ('>'); | |
4350 | ccat ('\0'); | |
4351 | return (char *) obstack_base (&scratch_obstack); | |
8d08fdba MS |
4352 | } |
4353 | ||
bd6dd845 | 4354 | static tree |
3a978d72 | 4355 | classtype_mangled_name (tree t) |
5566b478 MS |
4356 | { |
4357 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
36a117a5 MM |
4358 | /* Specializations have already had their names set up in |
4359 | lookup_template_class. */ | |
370af2d5 | 4360 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
9fbf56f7 MM |
4361 | { |
4362 | tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t)); | |
4363 | ||
36a117a5 MM |
4364 | /* For non-primary templates, the template parameters are |
4365 | implicit from their surrounding context. */ | |
9fbf56f7 MM |
4366 | if (PRIMARY_TEMPLATE_P (tmpl)) |
4367 | { | |
4368 | tree name = DECL_NAME (tmpl); | |
4369 | char *mangled_name = mangle_class_name_for_template | |
c8094d83 | 4370 | (IDENTIFIER_POINTER (name), |
9fbf56f7 MM |
4371 | DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
4372 | CLASSTYPE_TI_ARGS (t)); | |
4373 | tree id = get_identifier (mangled_name); | |
4374 | IDENTIFIER_TEMPLATE (id) = name; | |
4375 | return id; | |
4376 | } | |
5566b478 | 4377 | } |
9fbf56f7 MM |
4378 | |
4379 | return TYPE_IDENTIFIER (t); | |
5566b478 MS |
4380 | } |
4381 | ||
4382 | static void | |
3a978d72 | 4383 | add_pending_template (tree d) |
5566b478 | 4384 | { |
3ae18eaf JM |
4385 | tree ti = (TYPE_P (d) |
4386 | ? CLASSTYPE_TEMPLATE_INFO (d) | |
4387 | : DECL_TEMPLATE_INFO (d)); | |
46ccf50a | 4388 | tree pt; |
3ae18eaf | 4389 | int level; |
e92cc029 | 4390 | |
824b9a4c | 4391 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
5566b478 MS |
4392 | return; |
4393 | ||
3ae18eaf JM |
4394 | /* We are called both from instantiate_decl, where we've already had a |
4395 | tinst_level pushed, and instantiate_template, where we haven't. | |
4396 | Compensate. */ | |
4397 | level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d); | |
4398 | ||
4399 | if (level) | |
4400 | push_tinst_level (d); | |
4401 | ||
46ccf50a JM |
4402 | pt = tree_cons (current_tinst_level, d, NULL_TREE); |
4403 | if (last_pending_template) | |
4404 | TREE_CHAIN (last_pending_template) = pt; | |
4405 | else | |
4406 | pending_templates = pt; | |
4407 | ||
4408 | last_pending_template = pt; | |
4409 | ||
824b9a4c | 4410 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
3ae18eaf JM |
4411 | |
4412 | if (level) | |
4413 | pop_tinst_level (); | |
5566b478 MS |
4414 | } |
4415 | ||
386b8a85 | 4416 | |
4ba126e4 MM |
4417 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
4418 | ARGLIST. Valid choices for FNS are given in the cp-tree.def | |
4419 | documentation for TEMPLATE_ID_EXPR. */ | |
386b8a85 JM |
4420 | |
4421 | tree | |
3a978d72 | 4422 | lookup_template_function (tree fns, tree arglist) |
386b8a85 | 4423 | { |
2c73f9f5 | 4424 | tree type; |
050367a3 | 4425 | |
4ba126e4 MM |
4426 | if (fns == error_mark_node || arglist == error_mark_node) |
4427 | return error_mark_node; | |
4428 | ||
50bc768d | 4429 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
4230cec2 NS |
4430 | gcc_assert (fns && (is_overloaded_fn (fns) |
4431 | || TREE_CODE (fns) == IDENTIFIER_NODE)); | |
4ba126e4 | 4432 | |
50ad9642 MM |
4433 | if (BASELINK_P (fns)) |
4434 | { | |
f293ce4b RS |
4435 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
4436 | unknown_type_node, | |
4437 | BASELINK_FUNCTIONS (fns), | |
4438 | arglist); | |
50ad9642 MM |
4439 | return fns; |
4440 | } | |
4441 | ||
2c73f9f5 ML |
4442 | type = TREE_TYPE (fns); |
4443 | if (TREE_CODE (fns) == OVERLOAD || !type) | |
4444 | type = unknown_type_node; | |
c8094d83 | 4445 | |
f293ce4b | 4446 | return build2 (TEMPLATE_ID_EXPR, type, fns, arglist); |
386b8a85 JM |
4447 | } |
4448 | ||
a2b60a0e MM |
4449 | /* Within the scope of a template class S<T>, the name S gets bound |
4450 | (in build_self_reference) to a TYPE_DECL for the class, not a | |
4451 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, | |
4452 | or one of its enclosing classes, and that type is a template, | |
4453 | return the associated TEMPLATE_DECL. Otherwise, the original | |
4454 | DECL is returned. */ | |
4455 | ||
a723baf1 | 4456 | tree |
3a978d72 | 4457 | maybe_get_template_decl_from_type_decl (tree decl) |
a2b60a0e MM |
4458 | { |
4459 | return (decl != NULL_TREE | |
c8094d83 | 4460 | && TREE_CODE (decl) == TYPE_DECL |
a2b60a0e | 4461 | && DECL_ARTIFICIAL (decl) |
511b60ff | 4462 | && CLASS_TYPE_P (TREE_TYPE (decl)) |
c8094d83 | 4463 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
a2b60a0e MM |
4464 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
4465 | } | |
386b8a85 | 4466 | |
8d08fdba MS |
4467 | /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of |
4468 | parameters, find the desired type. | |
4469 | ||
4470 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. | |
8d08fdba MS |
4471 | |
4472 | IN_DECL, if non-NULL, is the template declaration we are trying to | |
c8094d83 | 4473 | instantiate. |
75650646 | 4474 | |
838dfd8a | 4475 | If ENTERING_SCOPE is nonzero, we are about to enter the scope of |
36a117a5 | 4476 | the class we are looking up. |
c8094d83 | 4477 | |
c2ea3a40 | 4478 | Issue error and warning messages under control of COMPLAIN. |
36a117a5 | 4479 | |
75650646 MM |
4480 | If the template class is really a local class in a template |
4481 | function, then the FUNCTION_CONTEXT is the function in which it is | |
c8094d83 | 4482 | being instantiated. |
b6ab6892 GB |
4483 | |
4484 | ??? Note that this function is currently called *twice* for each | |
4485 | template-id: the first time from the parser, while creating the | |
4486 | incomplete type (finish_template_type), and the second type during the | |
4487 | real instantiation (instantiate_template_class). This is surely something | |
4488 | that we want to avoid. It also causes some problems with argument | |
4489 | coercion (see convert_nontype_argument for more information on this). */ | |
e92cc029 | 4490 | |
8d08fdba | 4491 | tree |
c8094d83 | 4492 | lookup_template_class (tree d1, |
0cbd7506 MS |
4493 | tree arglist, |
4494 | tree in_decl, | |
4495 | tree context, | |
4496 | int entering_scope, | |
4497 | tsubst_flags_t complain) | |
8d08fdba | 4498 | { |
a703fb38 | 4499 | tree template = NULL_TREE, parmlist; |
dbfe2124 | 4500 | tree t; |
c8094d83 | 4501 | |
fd295cb2 | 4502 | timevar_push (TV_NAME_LOOKUP); |
c8094d83 | 4503 | |
5566b478 MS |
4504 | if (TREE_CODE (d1) == IDENTIFIER_NODE) |
4505 | { | |
90ea9897 MM |
4506 | tree value = innermost_non_namespace_value (d1); |
4507 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) | |
4508 | template = value; | |
73b0fce8 KL |
4509 | else |
4510 | { | |
2c73f9f5 ML |
4511 | if (context) |
4512 | push_decl_namespace (context); | |
10e6657a | 4513 | template = lookup_name (d1); |
3ebc5c52 | 4514 | template = maybe_get_template_decl_from_type_decl (template); |
2c73f9f5 ML |
4515 | if (context) |
4516 | pop_decl_namespace (); | |
73b0fce8 | 4517 | } |
8d019cef JM |
4518 | if (template) |
4519 | context = DECL_CONTEXT (template); | |
5566b478 | 4520 | } |
c91a56d2 MS |
4521 | else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1))) |
4522 | { | |
802dbc34 JM |
4523 | tree type = TREE_TYPE (d1); |
4524 | ||
4525 | /* If we are declaring a constructor, say A<T>::A<T>, we will get | |
4526 | an implicit typename for the second A. Deal with it. */ | |
4527 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) | |
4528 | type = TREE_TYPE (type); | |
c8094d83 | 4529 | |
802dbc34 | 4530 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
f3400fe2 | 4531 | { |
802dbc34 | 4532 | template = CLASSTYPE_TI_TEMPLATE (type); |
f3400fe2 JM |
4533 | d1 = DECL_NAME (template); |
4534 | } | |
c91a56d2 | 4535 | } |
c8094d83 | 4536 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
2f939d94 | 4537 | || (TYPE_P (d1) && IS_AGGR_TYPE (d1))) |
5566b478 | 4538 | { |
ed44da02 | 4539 | template = TYPE_TI_TEMPLATE (d1); |
5566b478 MS |
4540 | d1 = DECL_NAME (template); |
4541 | } | |
93cdc044 | 4542 | else if (TREE_CODE (d1) == TEMPLATE_DECL |
17aec3eb | 4543 | && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL) |
93cdc044 JM |
4544 | { |
4545 | template = d1; | |
4546 | d1 = DECL_NAME (template); | |
4547 | context = DECL_CONTEXT (template); | |
4548 | } | |
8d08fdba | 4549 | |
90ea9897 | 4550 | /* Issue an error message if we didn't find a template. */ |
8d08fdba | 4551 | if (! template) |
f3400fe2 | 4552 | { |
c2ea3a40 | 4553 | if (complain & tf_error) |
0cbd7506 | 4554 | error ("%qT is not a template", d1); |
fd295cb2 | 4555 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f3400fe2 | 4556 | } |
2c73f9f5 | 4557 | |
a87b4257 | 4558 | if (TREE_CODE (template) != TEMPLATE_DECL |
0cbd7506 | 4559 | /* Make sure it's a user visible template, if it was named by |
42eaed49 NS |
4560 | the user. */ |
4561 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template) | |
4562 | && !PRIMARY_TEMPLATE_P (template))) | |
8d08fdba | 4563 | { |
c2ea3a40 | 4564 | if (complain & tf_error) |
0cbd7506 MS |
4565 | { |
4566 | error ("non-template type %qT used as a template", d1); | |
4567 | if (in_decl) | |
dee15844 | 4568 | error ("for template declaration %q+D", in_decl); |
f9c244b8 | 4569 | } |
fd295cb2 | 4570 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
8d08fdba | 4571 | } |
8d08fdba | 4572 | |
42eaed49 | 4573 | complain &= ~tf_user; |
c8094d83 | 4574 | |
73b0fce8 KL |
4575 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (template)) |
4576 | { | |
4577 | /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store | |
0cbd7506 | 4578 | template arguments */ |
73b0fce8 | 4579 | |
1899c3a4 | 4580 | tree parm; |
73b0fce8 KL |
4581 | tree arglist2; |
4582 | ||
73b0fce8 KL |
4583 | parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template); |
4584 | ||
4f96ff63 KL |
4585 | /* Consider an example where a template template parameter declared as |
4586 | ||
4587 | template <class T, class U = std::allocator<T> > class TT | |
4588 | ||
c8094d83 MS |
4589 | The template parameter level of T and U are one level larger than |
4590 | of TT. To proper process the default argument of U, say when an | |
4f96ff63 | 4591 | instantiation `TT<int>' is seen, we need to build the full |
342cea95 KL |
4592 | arguments containing {int} as the innermost level. Outer levels, |
4593 | available when not appearing as default template argument, can be | |
4594 | obtained from `current_template_args ()'. | |
4f96ff63 | 4595 | |
342cea95 KL |
4596 | Suppose that TT is later substituted with std::vector. The above |
4597 | instantiation is `TT<int, std::allocator<T> >' with TT at | |
4598 | level 1, and T at level 2, while the template arguments at level 1 | |
4599 | becomes {std::vector} and the inner level 2 is {int}. */ | |
4600 | ||
4601 | if (current_template_parms) | |
4f96ff63 KL |
4602 | arglist = add_to_template_args (current_template_args (), arglist); |
4603 | ||
f9c244b8 | 4604 | arglist2 = coerce_template_parms (parmlist, arglist, template, |
3db45ab5 | 4605 | complain, |
e7e93965 MM |
4606 | /*require_all_args=*/true, |
4607 | /*use_default_args=*/true); | |
3e4a3562 | 4608 | if (arglist2 == error_mark_node |
544aef8c | 4609 | || (!uses_template_parms (arglist2) |
3e4a3562 | 4610 | && check_instantiated_args (template, arglist2, complain))) |
0cbd7506 | 4611 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
73b0fce8 | 4612 | |
dac65501 | 4613 | parm = bind_template_template_parm (TREE_TYPE (template), arglist2); |
fd295cb2 | 4614 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm); |
73b0fce8 | 4615 | } |
c8094d83 | 4616 | else |
8d08fdba | 4617 | { |
36a117a5 | 4618 | tree template_type = TREE_TYPE (template); |
7ac7b28f | 4619 | tree gen_tmpl; |
36a117a5 MM |
4620 | tree type_decl; |
4621 | tree found = NULL_TREE; | |
4622 | int arg_depth; | |
4623 | int parm_depth; | |
dbfe2124 | 4624 | int is_partial_instantiation; |
830bfa74 | 4625 | |
7ac7b28f MM |
4626 | gen_tmpl = most_general_template (template); |
4627 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); | |
36a117a5 MM |
4628 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
4629 | arg_depth = TMPL_ARGS_DEPTH (arglist); | |
4630 | ||
4631 | if (arg_depth == 1 && parm_depth > 1) | |
4632 | { | |
39c01e4c | 4633 | /* We've been given an incomplete set of template arguments. |
36a117a5 MM |
4634 | For example, given: |
4635 | ||
4636 | template <class T> struct S1 { | |
0cbd7506 | 4637 | template <class U> struct S2 {}; |
36a117a5 | 4638 | template <class U> struct S2<U*> {}; |
0cbd7506 | 4639 | }; |
c8094d83 | 4640 | |
36a117a5 MM |
4641 | we will be called with an ARGLIST of `U*', but the |
4642 | TEMPLATE will be `template <class T> template | |
4643 | <class U> struct S1<T>::S2'. We must fill in the missing | |
4644 | arguments. */ | |
c8094d83 | 4645 | arglist |
7ac7b28f MM |
4646 | = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)), |
4647 | arglist); | |
36a117a5 MM |
4648 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
4649 | } | |
5566b478 | 4650 | |
41f5d4b1 | 4651 | /* Now we should have enough arguments. */ |
50bc768d | 4652 | gcc_assert (parm_depth == arg_depth); |
c8094d83 | 4653 | |
7ac7b28f MM |
4654 | /* From here on, we're only interested in the most general |
4655 | template. */ | |
4656 | template = gen_tmpl; | |
4657 | ||
36a117a5 MM |
4658 | /* Calculate the BOUND_ARGS. These will be the args that are |
4659 | actually tsubst'd into the definition to create the | |
4660 | instantiation. */ | |
4661 | if (parm_depth > 1) | |
830bfa74 MM |
4662 | { |
4663 | /* We have multiple levels of arguments to coerce, at once. */ | |
830bfa74 | 4664 | int i; |
e4a84209 | 4665 | int saved_depth = TMPL_ARGS_DEPTH (arglist); |
36a117a5 | 4666 | |
f31c0a32 | 4667 | tree bound_args = make_tree_vec (parm_depth); |
c8094d83 | 4668 | |
e4a84209 | 4669 | for (i = saved_depth, |
c8094d83 | 4670 | t = DECL_TEMPLATE_PARMS (template); |
e4a84209 | 4671 | i > 0 && t != NULL_TREE; |
830bfa74 | 4672 | --i, t = TREE_CHAIN (t)) |
e4a84209 | 4673 | { |
ee3071ef NS |
4674 | tree a = coerce_template_parms (TREE_VALUE (t), |
4675 | arglist, template, | |
3db45ab5 | 4676 | complain, |
e7e93965 MM |
4677 | /*require_all_args=*/true, |
4678 | /*use_default_args=*/true); | |
88e98cfe KL |
4679 | |
4680 | /* Don't process further if one of the levels fails. */ | |
4681 | if (a == error_mark_node) | |
4682 | { | |
4683 | /* Restore the ARGLIST to its full size. */ | |
4684 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
4685 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
4686 | } | |
c8094d83 | 4687 | |
e4a84209 MM |
4688 | SET_TMPL_ARGS_LEVEL (bound_args, i, a); |
4689 | ||
4690 | /* We temporarily reduce the length of the ARGLIST so | |
4691 | that coerce_template_parms will see only the arguments | |
4692 | corresponding to the template parameters it is | |
4693 | examining. */ | |
4694 | TREE_VEC_LENGTH (arglist)--; | |
4695 | } | |
4696 | ||
4697 | /* Restore the ARGLIST to its full size. */ | |
4698 | TREE_VEC_LENGTH (arglist) = saved_depth; | |
4699 | ||
36a117a5 | 4700 | arglist = bound_args; |
830bfa74 MM |
4701 | } |
4702 | else | |
36a117a5 MM |
4703 | arglist |
4704 | = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist), | |
f9a7ae04 | 4705 | INNERMOST_TEMPLATE_ARGS (arglist), |
f9c244b8 | 4706 | template, |
3db45ab5 | 4707 | complain, |
e7e93965 MM |
4708 | /*require_all_args=*/true, |
4709 | /*use_default_args=*/true); | |
36a117a5 | 4710 | |
c353b8e3 | 4711 | if (arglist == error_mark_node) |
36a117a5 | 4712 | /* We were unable to bind the arguments. */ |
fd295cb2 | 4713 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
5566b478 | 4714 | |
36a117a5 MM |
4715 | /* In the scope of a template class, explicit references to the |
4716 | template class refer to the type of the template, not any | |
4717 | instantiation of it. For example, in: | |
c8094d83 | 4718 | |
36a117a5 MM |
4719 | template <class T> class C { void f(C<T>); } |
4720 | ||
4721 | the `C<T>' is just the same as `C'. Outside of the | |
4722 | class, however, such a reference is an instantiation. */ | |
ed44da02 | 4723 | if (comp_template_args (TYPE_TI_ARGS (template_type), |
36a117a5 MM |
4724 | arglist)) |
4725 | { | |
4726 | found = template_type; | |
c8094d83 | 4727 | |
36a117a5 | 4728 | if (!entering_scope && PRIMARY_TEMPLATE_P (template)) |
5566b478 | 4729 | { |
36a117a5 | 4730 | tree ctx; |
c8094d83 MS |
4731 | |
4732 | for (ctx = current_class_type; | |
5f04800c KL |
4733 | ctx && TREE_CODE (ctx) != NAMESPACE_DECL; |
4734 | ctx = (TYPE_P (ctx) | |
4735 | ? TYPE_CONTEXT (ctx) | |
4736 | : DECL_CONTEXT (ctx))) | |
4737 | if (TYPE_P (ctx) && same_type_p (ctx, template_type)) | |
4738 | goto found_ctx; | |
c8094d83 | 4739 | |
6df5158a NS |
4740 | /* We're not in the scope of the class, so the |
4741 | TEMPLATE_TYPE is not the type we want after all. */ | |
4742 | found = NULL_TREE; | |
4743 | found_ctx:; | |
5566b478 MS |
4744 | } |
4745 | } | |
36a117a5 | 4746 | if (found) |
0cbd7506 | 4747 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
414ea4aa | 4748 | |
c7222c02 MM |
4749 | /* If we already have this specialization, return it. */ |
4750 | found = retrieve_specialization (template, arglist, | |
4751 | /*class_specializations_p=*/false); | |
4752 | if (found) | |
4753 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); | |
5566b478 | 4754 | |
dbfe2124 | 4755 | /* This type is a "partial instantiation" if any of the template |
ab097535 | 4756 | arguments still involve template parameters. Note that we set |
486e4077 MM |
4757 | IS_PARTIAL_INSTANTIATION for partial specializations as |
4758 | well. */ | |
dbfe2124 MM |
4759 | is_partial_instantiation = uses_template_parms (arglist); |
4760 | ||
c353b8e3 MM |
4761 | /* If the deduced arguments are invalid, then the binding |
4762 | failed. */ | |
4763 | if (!is_partial_instantiation | |
4764 | && check_instantiated_args (template, | |
4765 | INNERMOST_TEMPLATE_ARGS (arglist), | |
4766 | complain)) | |
4767 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
c8094d83 MS |
4768 | |
4769 | if (!is_partial_instantiation | |
3ebc5c52 MM |
4770 | && !PRIMARY_TEMPLATE_P (template) |
4771 | && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL) | |
4772 | { | |
3ebc5c52 MM |
4773 | found = xref_tag_from_type (TREE_TYPE (template), |
4774 | DECL_NAME (template), | |
29ef83de | 4775 | /*tag_scope=*/ts_global); |
fd295cb2 | 4776 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found); |
3ebc5c52 | 4777 | } |
c8094d83 | 4778 | |
95ee998c | 4779 | context = tsubst (DECL_CONTEXT (template), arglist, |
c2ea3a40 | 4780 | complain, in_decl); |
95ee998c MM |
4781 | if (!context) |
4782 | context = global_namespace; | |
4783 | ||
36a117a5 | 4784 | /* Create the type. */ |
ed44da02 MM |
4785 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
4786 | { | |
dbfe2124 | 4787 | if (!is_partial_instantiation) |
92777ce4 NS |
4788 | { |
4789 | set_current_access_from_decl (TYPE_NAME (template_type)); | |
4790 | t = start_enum (TYPE_IDENTIFIER (template_type)); | |
4791 | } | |
ed44da02 | 4792 | else |
dbfe2124 | 4793 | /* We don't want to call start_enum for this type, since |
ed44da02 MM |
4794 | the values for the enumeration constants may involve |
4795 | template parameters. And, no one should be interested | |
4796 | in the enumeration constants for such a type. */ | |
4797 | t = make_node (ENUMERAL_TYPE); | |
4798 | } | |
4799 | else | |
4800 | { | |
33848bb0 | 4801 | t = make_aggr_type (TREE_CODE (template_type)); |
c8094d83 | 4802 | CLASSTYPE_DECLARED_CLASS (t) |
ed44da02 | 4803 | = CLASSTYPE_DECLARED_CLASS (template_type); |
ed44da02 | 4804 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
f668f160 | 4805 | TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type); |
ae673f14 JM |
4806 | |
4807 | /* A local class. Make sure the decl gets registered properly. */ | |
4808 | if (context == current_function_decl) | |
bd3d082e | 4809 | pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current); |
ed44da02 MM |
4810 | } |
4811 | ||
ae673f14 JM |
4812 | /* If we called start_enum or pushtag above, this information |
4813 | will already be set up. */ | |
ed44da02 MM |
4814 | if (!TYPE_NAME (t)) |
4815 | { | |
4816 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
c8094d83 | 4817 | |
9188c363 | 4818 | type_decl = create_implicit_typedef (DECL_NAME (template), t); |
ed44da02 | 4819 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
9188c363 | 4820 | TYPE_STUB_DECL (t) = type_decl; |
c8094d83 | 4821 | DECL_SOURCE_LOCATION (type_decl) |
f31686a3 | 4822 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
ed44da02 MM |
4823 | } |
4824 | else | |
4825 | type_decl = TYPE_NAME (t); | |
36a117a5 | 4826 | |
cab7a9a3 KL |
4827 | TREE_PRIVATE (type_decl) |
4828 | = TREE_PRIVATE (TYPE_STUB_DECL (template_type)); | |
4829 | TREE_PROTECTED (type_decl) | |
4830 | = TREE_PROTECTED (TYPE_STUB_DECL (template_type)); | |
12af7ba3 MM |
4831 | DECL_IN_SYSTEM_HEADER (type_decl) |
4832 | = DECL_IN_SYSTEM_HEADER (template); | |
8d039470 MS |
4833 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
4834 | { | |
4835 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; | |
4836 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); | |
4837 | } | |
cab7a9a3 | 4838 | |
9fbf56f7 MM |
4839 | /* Set up the template information. We have to figure out which |
4840 | template is the immediate parent if this is a full | |
4841 | instantiation. */ | |
4842 | if (parm_depth == 1 || is_partial_instantiation | |
4843 | || !PRIMARY_TEMPLATE_P (template)) | |
4844 | /* This case is easy; there are no member templates involved. */ | |
4845 | found = template; | |
4846 | else | |
4847 | { | |
ab097535 NS |
4848 | /* This is a full instantiation of a member template. Look |
4849 | for a partial instantiation of which this is an instance. */ | |
9fbf56f7 MM |
4850 | |
4851 | for (found = DECL_TEMPLATE_INSTANTIATIONS (template); | |
4852 | found; found = TREE_CHAIN (found)) | |
4853 | { | |
4854 | int success; | |
4855 | tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found)); | |
4856 | ||
4857 | /* We only want partial instantiations, here, not | |
4858 | specializations or full instantiations. */ | |
370af2d5 | 4859 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found)) |
9fbf56f7 MM |
4860 | || !uses_template_parms (TREE_VALUE (found))) |
4861 | continue; | |
4862 | ||
4863 | /* Temporarily reduce by one the number of levels in the | |
4864 | ARGLIST and in FOUND so as to avoid comparing the | |
4865 | last set of arguments. */ | |
4866 | TREE_VEC_LENGTH (arglist)--; | |
4867 | TREE_VEC_LENGTH (TREE_PURPOSE (found)) --; | |
4868 | ||
4869 | /* See if the arguments match. If they do, then TMPL is | |
4870 | the partial instantiation we want. */ | |
4871 | success = comp_template_args (TREE_PURPOSE (found), arglist); | |
4872 | ||
4873 | /* Restore the argument vectors to their full size. */ | |
4874 | TREE_VEC_LENGTH (arglist)++; | |
4875 | TREE_VEC_LENGTH (TREE_PURPOSE (found))++; | |
4876 | ||
4877 | if (success) | |
4878 | { | |
4879 | found = tmpl; | |
4880 | break; | |
4881 | } | |
4882 | } | |
4883 | ||
4884 | if (!found) | |
ab097535 NS |
4885 | { |
4886 | /* There was no partial instantiation. This happens | |
0cbd7506 MS |
4887 | where C<T> is a member template of A<T> and it's used |
4888 | in something like | |
c8094d83 | 4889 | |
0cbd7506 MS |
4890 | template <typename T> struct B { A<T>::C<int> m; }; |
4891 | B<float>; | |
c8094d83 | 4892 | |
0cbd7506 MS |
4893 | Create the partial instantiation. |
4894 | */ | |
4895 | TREE_VEC_LENGTH (arglist)--; | |
4896 | found = tsubst (template, arglist, complain, NULL_TREE); | |
4897 | TREE_VEC_LENGTH (arglist)++; | |
4898 | } | |
9fbf56f7 MM |
4899 | } |
4900 | ||
c8094d83 MS |
4901 | SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE)); |
4902 | DECL_TEMPLATE_INSTANTIATIONS (template) | |
4903 | = tree_cons (arglist, t, | |
dbfe2124 MM |
4904 | DECL_TEMPLATE_INSTANTIATIONS (template)); |
4905 | ||
c8094d83 | 4906 | if (TREE_CODE (t) == ENUMERAL_TYPE |
dbfe2124 | 4907 | && !is_partial_instantiation) |
61a127b3 MM |
4908 | /* Now that the type has been registered on the instantiations |
4909 | list, we set up the enumerators. Because the enumeration | |
4910 | constants may involve the enumeration type itself, we make | |
4911 | sure to register the type first, and then create the | |
4912 | constants. That way, doing tsubst_expr for the enumeration | |
4913 | constants won't result in recursive calls here; we'll find | |
4914 | the instantiation and exit above. */ | |
4915 | tsubst_enum (template_type, t, arglist); | |
dbfe2124 | 4916 | |
36a117a5 MM |
4917 | /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO |
4918 | is set up. */ | |
ed44da02 MM |
4919 | if (TREE_CODE (t) != ENUMERAL_TYPE) |
4920 | DECL_NAME (type_decl) = classtype_mangled_name (t); | |
7813d14c | 4921 | if (is_partial_instantiation) |
077e7015 MM |
4922 | /* If the type makes use of template parameters, the |
4923 | code that generates debugging information will crash. */ | |
4924 | DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1; | |
8d08fdba | 4925 | |
b9e75696 JM |
4926 | /* Possibly limit visibility based on template args. */ |
4927 | TREE_PUBLIC (type_decl) = 1; | |
4928 | determine_visibility (type_decl); | |
4929 | ||
fd295cb2 | 4930 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
36a117a5 | 4931 | } |
fd295cb2 | 4932 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
4933 | } |
4934 | \f | |
c8094d83 | 4935 | struct pair_fn_data |
8d08fdba | 4936 | { |
8dfaeb63 MM |
4937 | tree_fn_t fn; |
4938 | void *data; | |
0c58f841 | 4939 | struct pointer_set_t *visited; |
8dfaeb63 MM |
4940 | }; |
4941 | ||
4942 | /* Called from for_each_template_parm via walk_tree. */ | |
581d38d0 | 4943 | |
8dfaeb63 | 4944 | static tree |
350fae66 | 4945 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
8dfaeb63 MM |
4946 | { |
4947 | tree t = *tp; | |
4948 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; | |
4949 | tree_fn_t fn = pfd->fn; | |
4950 | void *data = pfd->data; | |
4f2c9d7e | 4951 | |
2f939d94 | 4952 | if (TYPE_P (t) |
4f2c9d7e | 4953 | && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited)) |
8dfaeb63 | 4954 | return error_mark_node; |
581d38d0 | 4955 | |
8d08fdba MS |
4956 | switch (TREE_CODE (t)) |
4957 | { | |
8d08fdba | 4958 | case RECORD_TYPE: |
9076e292 | 4959 | if (TYPE_PTRMEMFUNC_P (t)) |
8dfaeb63 | 4960 | break; |
ed44da02 MM |
4961 | /* Fall through. */ |
4962 | ||
8d08fdba | 4963 | case UNION_TYPE: |
ed44da02 | 4964 | case ENUMERAL_TYPE: |
8dfaeb63 MM |
4965 | if (!TYPE_TEMPLATE_INFO (t)) |
4966 | *walk_subtrees = 0; | |
4967 | else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)), | |
4f2c9d7e | 4968 | fn, data, pfd->visited)) |
8dfaeb63 MM |
4969 | return error_mark_node; |
4970 | break; | |
4971 | ||
8f6e6bf3 EB |
4972 | case INTEGER_TYPE: |
4973 | if (for_each_template_parm (TYPE_MIN_VALUE (t), | |
4974 | fn, data, pfd->visited) | |
4975 | || for_each_template_parm (TYPE_MAX_VALUE (t), | |
4976 | fn, data, pfd->visited)) | |
4977 | return error_mark_node; | |
4978 | break; | |
4979 | ||
588c2d1c | 4980 | case METHOD_TYPE: |
8dfaeb63 MM |
4981 | /* Since we're not going to walk subtrees, we have to do this |
4982 | explicitly here. */ | |
4f2c9d7e MM |
4983 | if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data, |
4984 | pfd->visited)) | |
8dfaeb63 | 4985 | return error_mark_node; |
4890c2f4 | 4986 | /* Fall through. */ |
588c2d1c MM |
4987 | |
4988 | case FUNCTION_TYPE: | |
8dfaeb63 | 4989 | /* Check the return type. */ |
4f2c9d7e | 4990 | if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
4991 | return error_mark_node; |
4992 | ||
588c2d1c MM |
4993 | /* Check the parameter types. Since default arguments are not |
4994 | instantiated until they are needed, the TYPE_ARG_TYPES may | |
4995 | contain expressions that involve template parameters. But, | |
4996 | no-one should be looking at them yet. And, once they're | |
4997 | instantiated, they don't contain template parameters, so | |
4998 | there's no point in looking at them then, either. */ | |
4999 | { | |
5000 | tree parm; | |
5001 | ||
5002 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) | |
4f2c9d7e MM |
5003 | if (for_each_template_parm (TREE_VALUE (parm), fn, data, |
5004 | pfd->visited)) | |
8dfaeb63 | 5005 | return error_mark_node; |
5566b478 | 5006 | |
8dfaeb63 MM |
5007 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
5008 | want walk_tree walking into them itself. */ | |
5009 | *walk_subtrees = 0; | |
5010 | } | |
5011 | break; | |
3ac3d9ea | 5012 | |
a723baf1 | 5013 | case TYPEOF_TYPE: |
c8094d83 | 5014 | if (for_each_template_parm (TYPE_FIELDS (t), fn, data, |
a723baf1 MM |
5015 | pfd->visited)) |
5016 | return error_mark_node; | |
5017 | break; | |
5018 | ||
8d08fdba | 5019 | case FUNCTION_DECL: |
5566b478 | 5020 | case VAR_DECL: |
5566b478 | 5021 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t) |
4f2c9d7e MM |
5022 | && for_each_template_parm (DECL_TI_ARGS (t), fn, data, |
5023 | pfd->visited)) | |
8dfaeb63 MM |
5024 | return error_mark_node; |
5025 | /* Fall through. */ | |
5026 | ||
8d08fdba | 5027 | case PARM_DECL: |
a723baf1 MM |
5028 | case CONST_DECL: |
5029 | if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t) | |
5030 | && for_each_template_parm (DECL_INITIAL (t), fn, data, | |
5031 | pfd->visited)) | |
5032 | return error_mark_node; | |
c8094d83 | 5033 | if (DECL_CONTEXT (t) |
4f2c9d7e MM |
5034 | && for_each_template_parm (DECL_CONTEXT (t), fn, data, |
5035 | pfd->visited)) | |
8dfaeb63 MM |
5036 | return error_mark_node; |
5037 | break; | |
8d08fdba | 5038 | |
a1281f45 | 5039 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
744fac59 | 5040 | /* Record template parameters such as `T' inside `TT<T>'. */ |
4f2c9d7e | 5041 | if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited)) |
8dfaeb63 MM |
5042 | return error_mark_node; |
5043 | /* Fall through. */ | |
5044 | ||
a1281f45 | 5045 | case TEMPLATE_TEMPLATE_PARM: |
744fac59 | 5046 | case TEMPLATE_TYPE_PARM: |
f84b4be9 | 5047 | case TEMPLATE_PARM_INDEX: |
8dfaeb63 MM |
5048 | if (fn && (*fn)(t, data)) |
5049 | return error_mark_node; | |
5050 | else if (!fn) | |
5051 | return error_mark_node; | |
5052 | break; | |
5156628f | 5053 | |
8dfaeb63 | 5054 | case TEMPLATE_DECL: |
f4f206f4 | 5055 | /* A template template parameter is encountered. */ |
8dfaeb63 | 5056 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t) |
4f2c9d7e | 5057 | && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited)) |
8dfaeb63 | 5058 | return error_mark_node; |
db5ae43f | 5059 | |
8dfaeb63 MM |
5060 | /* Already substituted template template parameter */ |
5061 | *walk_subtrees = 0; | |
5062 | break; | |
b8865407 | 5063 | |
4699c561 | 5064 | case TYPENAME_TYPE: |
c8094d83 | 5065 | if (!fn |
4f2c9d7e MM |
5066 | || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn, |
5067 | data, pfd->visited)) | |
8dfaeb63 MM |
5068 | return error_mark_node; |
5069 | break; | |
4699c561 | 5070 | |
8dfaeb63 MM |
5071 | case CONSTRUCTOR: |
5072 | if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)) | |
5073 | && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE | |
4f2c9d7e MM |
5074 | (TREE_TYPE (t)), fn, data, |
5075 | pfd->visited)) | |
8dfaeb63 MM |
5076 | return error_mark_node; |
5077 | break; | |
c8094d83 | 5078 | |
b8865407 MM |
5079 | case INDIRECT_REF: |
5080 | case COMPONENT_REF: | |
4699c561 | 5081 | /* If there's no type, then this thing must be some expression |
b8865407 | 5082 | involving template parameters. */ |
4699c561 | 5083 | if (!fn && !TREE_TYPE (t)) |
8dfaeb63 MM |
5084 | return error_mark_node; |
5085 | break; | |
b8865407 | 5086 | |
42976354 BK |
5087 | case MODOP_EXPR: |
5088 | case CAST_EXPR: | |
5089 | case REINTERPRET_CAST_EXPR: | |
5090 | case CONST_CAST_EXPR: | |
5091 | case STATIC_CAST_EXPR: | |
5092 | case DYNAMIC_CAST_EXPR: | |
42976354 BK |
5093 | case ARROW_EXPR: |
5094 | case DOTSTAR_EXPR: | |
5095 | case TYPEID_EXPR: | |
40242ccf | 5096 | case PSEUDO_DTOR_EXPR: |
b8865407 | 5097 | if (!fn) |
8dfaeb63 MM |
5098 | return error_mark_node; |
5099 | break; | |
abff8e06 | 5100 | |
bd9bb3d2 MM |
5101 | case BASELINK: |
5102 | /* If we do not handle this case specially, we end up walking | |
5103 | the BINFO hierarchy, which is circular, and therefore | |
5104 | confuses walk_tree. */ | |
5105 | *walk_subtrees = 0; | |
4f2c9d7e MM |
5106 | if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data, |
5107 | pfd->visited)) | |
bd9bb3d2 MM |
5108 | return error_mark_node; |
5109 | break; | |
5110 | ||
8d08fdba | 5111 | default: |
8dfaeb63 | 5112 | break; |
8d08fdba | 5113 | } |
8dfaeb63 MM |
5114 | |
5115 | /* We didn't find any template parameters we liked. */ | |
5116 | return NULL_TREE; | |
5117 | } | |
5118 | ||
c8094d83 MS |
5119 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
5120 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, | |
a1281f45 | 5121 | call FN with the parameter and the DATA. |
838dfd8a | 5122 | If FN returns nonzero, the iteration is terminated, and |
8dfaeb63 | 5123 | for_each_template_parm returns 1. Otherwise, the iteration |
838dfd8a | 5124 | continues. If FN never returns a nonzero value, the value |
8dfaeb63 MM |
5125 | returned by for_each_template_parm is 0. If FN is NULL, it is |
5126 | considered to be the function which always returns 1. */ | |
5127 | ||
5128 | static int | |
0c58f841 MA |
5129 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
5130 | struct pointer_set_t *visited) | |
8dfaeb63 MM |
5131 | { |
5132 | struct pair_fn_data pfd; | |
ad2ae3b2 | 5133 | int result; |
8dfaeb63 MM |
5134 | |
5135 | /* Set up. */ | |
5136 | pfd.fn = fn; | |
5137 | pfd.data = data; | |
5138 | ||
4890c2f4 MM |
5139 | /* Walk the tree. (Conceptually, we would like to walk without |
5140 | duplicates, but for_each_template_parm_r recursively calls | |
5141 | for_each_template_parm, so we would need to reorganize a fair | |
4f2c9d7e MM |
5142 | bit to use walk_tree_without_duplicates, so we keep our own |
5143 | visited list.) */ | |
5144 | if (visited) | |
5145 | pfd.visited = visited; | |
5146 | else | |
0c58f841 | 5147 | pfd.visited = pointer_set_create (); |
c8094d83 MS |
5148 | result = walk_tree (&t, |
5149 | for_each_template_parm_r, | |
ad2ae3b2 | 5150 | &pfd, |
c1e39976 | 5151 | pfd.visited) != NULL_TREE; |
ad2ae3b2 MM |
5152 | |
5153 | /* Clean up. */ | |
5154 | if (!visited) | |
0c58f841 MA |
5155 | { |
5156 | pointer_set_destroy (pfd.visited); | |
5157 | pfd.visited = 0; | |
5158 | } | |
ad2ae3b2 MM |
5159 | |
5160 | return result; | |
8d08fdba MS |
5161 | } |
5162 | ||
d43f603d KL |
5163 | /* Returns true if T depends on any template parameter. */ |
5164 | ||
050367a3 | 5165 | int |
3a978d72 | 5166 | uses_template_parms (tree t) |
050367a3 | 5167 | { |
c353b8e3 MM |
5168 | bool dependent_p; |
5169 | int saved_processing_template_decl; | |
5170 | ||
5171 | saved_processing_template_decl = processing_template_decl; | |
5172 | if (!saved_processing_template_decl) | |
5173 | processing_template_decl = 1; | |
5174 | if (TYPE_P (t)) | |
5175 | dependent_p = dependent_type_p (t); | |
5176 | else if (TREE_CODE (t) == TREE_VEC) | |
5177 | dependent_p = any_dependent_template_arguments_p (t); | |
5178 | else if (TREE_CODE (t) == TREE_LIST) | |
5179 | dependent_p = (uses_template_parms (TREE_VALUE (t)) | |
5180 | || uses_template_parms (TREE_CHAIN (t))); | |
3ce5fa4f NS |
5181 | else if (TREE_CODE (t) == TYPE_DECL) |
5182 | dependent_p = dependent_type_p (TREE_TYPE (t)); | |
c8094d83 MS |
5183 | else if (DECL_P (t) |
5184 | || EXPR_P (t) | |
c353b8e3 MM |
5185 | || TREE_CODE (t) == TEMPLATE_PARM_INDEX |
5186 | || TREE_CODE (t) == OVERLOAD | |
5187 | || TREE_CODE (t) == BASELINK | |
b207d6e2 | 5188 | || TREE_CODE (t) == IDENTIFIER_NODE |
6615c446 | 5189 | || CONSTANT_CLASS_P (t)) |
c353b8e3 MM |
5190 | dependent_p = (type_dependent_expression_p (t) |
5191 | || value_dependent_expression_p (t)); | |
315fb5db NS |
5192 | else |
5193 | { | |
5194 | gcc_assert (t == error_mark_node); | |
5195 | dependent_p = false; | |
5196 | } | |
c8094d83 | 5197 | |
c353b8e3 MM |
5198 | processing_template_decl = saved_processing_template_decl; |
5199 | ||
5200 | return dependent_p; | |
050367a3 MM |
5201 | } |
5202 | ||
d43f603d KL |
5203 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
5204 | ||
5205 | int | |
5206 | uses_template_parms_level (tree t, int level) | |
5207 | { | |
5208 | return for_each_template_parm (t, template_parm_this_level_p, &level, NULL); | |
5209 | } | |
5210 | ||
27fafc8d | 5211 | static int tinst_depth; |
e9f32eb5 | 5212 | extern int max_tinst_depth; |
5566b478 | 5213 | #ifdef GATHER_STATISTICS |
27fafc8d | 5214 | int depth_reached; |
5566b478 | 5215 | #endif |
8dfaeb63 MM |
5216 | static int tinst_level_tick; |
5217 | static int last_template_error_tick; | |
8d08fdba | 5218 | |
3ae18eaf JM |
5219 | /* We're starting to instantiate D; record the template instantiation context |
5220 | for diagnostics and to restore it later. */ | |
5221 | ||
aa9d8196 | 5222 | static int |
3a978d72 | 5223 | push_tinst_level (tree d) |
8d08fdba | 5224 | { |
3ae18eaf | 5225 | tree new; |
8d08fdba | 5226 | |
7215f9a0 MS |
5227 | if (tinst_depth >= max_tinst_depth) |
5228 | { | |
8adf5b5e JM |
5229 | /* If the instantiation in question still has unbound template parms, |
5230 | we don't really care if we can't instantiate it, so just return. | |
0cbd7506 | 5231 | This happens with base instantiation for implicit `typename'. */ |
8adf5b5e JM |
5232 | if (uses_template_parms (d)) |
5233 | return 0; | |
5234 | ||
1139b3d8 | 5235 | last_template_error_tick = tinst_level_tick; |
0f51ccfc | 5236 | error ("template instantiation depth exceeds maximum of %d (use " |
0cbd7506 | 5237 | "-ftemplate-depth-NN to increase the maximum) instantiating %qD", |
b4f4233d | 5238 | max_tinst_depth, d); |
5566b478 | 5239 | |
cb753e49 | 5240 | print_instantiation_context (); |
5566b478 | 5241 | |
7215f9a0 MS |
5242 | return 0; |
5243 | } | |
5244 | ||
12af7ba3 MM |
5245 | new = make_node (TINST_LEVEL); |
5246 | TINST_DECL (new) = d; | |
5247 | TINST_LOCATION (new) = input_location; | |
5248 | TINST_IN_SYSTEM_HEADER_P (new) = in_system_header; | |
3ae18eaf | 5249 | TREE_CHAIN (new) = current_tinst_level; |
8d08fdba | 5250 | current_tinst_level = new; |
5566b478 | 5251 | |
7215f9a0 | 5252 | ++tinst_depth; |
5566b478 MS |
5253 | #ifdef GATHER_STATISTICS |
5254 | if (tinst_depth > depth_reached) | |
5255 | depth_reached = tinst_depth; | |
5256 | #endif | |
5257 | ||
27fafc8d | 5258 | ++tinst_level_tick; |
7215f9a0 | 5259 | return 1; |
8d08fdba MS |
5260 | } |
5261 | ||
3ae18eaf JM |
5262 | /* We're done instantiating this template; return to the instantiation |
5263 | context. */ | |
5264 | ||
aa9d8196 | 5265 | static void |
3a978d72 | 5266 | pop_tinst_level (void) |
8d08fdba | 5267 | { |
3ae18eaf | 5268 | tree old = current_tinst_level; |
8d08fdba | 5269 | |
ae58fa02 MM |
5270 | /* Restore the filename and line number stashed away when we started |
5271 | this instantiation. */ | |
406d77a4 | 5272 | input_location = TINST_LOCATION (old); |
12af7ba3 | 5273 | in_system_header = TINST_IN_SYSTEM_HEADER_P (old); |
3ae18eaf | 5274 | current_tinst_level = TREE_CHAIN (old); |
7215f9a0 | 5275 | --tinst_depth; |
27fafc8d | 5276 | ++tinst_level_tick; |
8d08fdba MS |
5277 | } |
5278 | ||
3ae18eaf JM |
5279 | /* We're instantiating a deferred template; restore the template |
5280 | instantiation context in which the instantiation was requested, which | |
5281 | is one step out from LEVEL. */ | |
5282 | ||
5283 | static void | |
3a978d72 | 5284 | reopen_tinst_level (tree level) |
3ae18eaf JM |
5285 | { |
5286 | tree t; | |
5287 | ||
5288 | tinst_depth = 0; | |
5289 | for (t = level; t; t = TREE_CHAIN (t)) | |
5290 | ++tinst_depth; | |
5291 | ||
5292 | current_tinst_level = level; | |
5293 | pop_tinst_level (); | |
5294 | } | |
5295 | ||
f84b4be9 JM |
5296 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
5297 | vector of template arguments, as for tsubst. | |
5298 | ||
dc957d14 | 5299 | Returns an appropriate tsubst'd friend declaration. */ |
f84b4be9 JM |
5300 | |
5301 | static tree | |
3a978d72 | 5302 | tsubst_friend_function (tree decl, tree args) |
f84b4be9 JM |
5303 | { |
5304 | tree new_friend; | |
27fafc8d | 5305 | |
c8094d83 | 5306 | if (TREE_CODE (decl) == FUNCTION_DECL |
f84b4be9 JM |
5307 | && DECL_TEMPLATE_INSTANTIATION (decl) |
5308 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) | |
5309 | /* This was a friend declared with an explicit template | |
5310 | argument list, e.g.: | |
c8094d83 | 5311 | |
f84b4be9 | 5312 | friend void f<>(T); |
c8094d83 | 5313 | |
f84b4be9 JM |
5314 | to indicate that f was a template instantiation, not a new |
5315 | function declaration. Now, we have to figure out what | |
5316 | instantiation of what template. */ | |
5317 | { | |
76e57b45 | 5318 | tree template_id, arglist, fns; |
f84b4be9 JM |
5319 | tree new_args; |
5320 | tree tmpl; | |
ed2fa432 | 5321 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
c8094d83 | 5322 | |
76e57b45 | 5323 | /* Friend functions are looked up in the containing namespace scope. |
0cbd7506 MS |
5324 | We must enter that scope, to avoid finding member functions of the |
5325 | current cless with same name. */ | |
76e57b45 NS |
5326 | push_nested_namespace (ns); |
5327 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, | |
015c2c66 MM |
5328 | tf_warning_or_error, NULL_TREE, |
5329 | /*integral_constant_expression_p=*/false); | |
76e57b45 NS |
5330 | pop_nested_namespace (ns); |
5331 | arglist = tsubst (DECL_TI_ARGS (decl), args, | |
23fca1f5 | 5332 | tf_warning_or_error, NULL_TREE); |
76e57b45 | 5333 | template_id = lookup_template_function (fns, arglist); |
c8094d83 | 5334 | |
23fca1f5 | 5335 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
36a117a5 | 5336 | tmpl = determine_specialization (template_id, new_friend, |
c8094d83 | 5337 | &new_args, |
5fe7b654 | 5338 | /*need_member_template=*/0, |
29a1da1c MM |
5339 | TREE_VEC_LENGTH (args), |
5340 | tsk_none); | |
6e432b31 | 5341 | return instantiate_template (tmpl, new_args, tf_error); |
f84b4be9 | 5342 | } |
36a117a5 | 5343 | |
23fca1f5 | 5344 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
c8094d83 | 5345 | |
36a117a5 | 5346 | /* The NEW_FRIEND will look like an instantiation, to the |
f84b4be9 JM |
5347 | compiler, but is not an instantiation from the point of view of |
5348 | the language. For example, we might have had: | |
c8094d83 | 5349 | |
f84b4be9 JM |
5350 | template <class T> struct S { |
5351 | template <class U> friend void f(T, U); | |
5352 | }; | |
c8094d83 | 5353 | |
f84b4be9 JM |
5354 | Then, in S<int>, template <class U> void f(int, U) is not an |
5355 | instantiation of anything. */ | |
ac2b3222 AP |
5356 | if (new_friend == error_mark_node) |
5357 | return error_mark_node; | |
c8094d83 | 5358 | |
f84b4be9 JM |
5359 | DECL_USE_TEMPLATE (new_friend) = 0; |
5360 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
655dc6ee JM |
5361 | { |
5362 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; | |
5363 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) | |
5364 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); | |
5365 | } | |
36a117a5 | 5366 | |
92643fea MM |
5367 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
5368 | is not a template instantiation and should not be mangled like | |
5369 | one. Therefore, we forget the mangling here; we'll recompute it | |
5370 | later if we need it. */ | |
36a117a5 MM |
5371 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
5372 | { | |
19e7881c | 5373 | SET_DECL_RTL (new_friend, NULL_RTX); |
92643fea | 5374 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
36a117a5 | 5375 | } |
c8094d83 | 5376 | |
6eb3bb27 | 5377 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
f84b4be9 | 5378 | { |
36a117a5 | 5379 | tree old_decl; |
fbf1c34b MM |
5380 | tree new_friend_template_info; |
5381 | tree new_friend_result_template_info; | |
92da7074 | 5382 | tree ns; |
fbf1c34b MM |
5383 | int new_friend_is_defn; |
5384 | ||
5385 | /* We must save some information from NEW_FRIEND before calling | |
5386 | duplicate decls since that function will free NEW_FRIEND if | |
5387 | possible. */ | |
5388 | new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); | |
5bd61841 | 5389 | new_friend_is_defn = |
c8094d83 | 5390 | (DECL_INITIAL (DECL_TEMPLATE_RESULT |
5bd61841 MM |
5391 | (template_for_substitution (new_friend))) |
5392 | != NULL_TREE); | |
f84b4be9 | 5393 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
fbf1c34b MM |
5394 | { |
5395 | /* This declaration is a `primary' template. */ | |
5396 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; | |
c8094d83 | 5397 | |
fbf1c34b | 5398 | new_friend_result_template_info |
17aec3eb | 5399 | = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend)); |
fbf1c34b MM |
5400 | } |
5401 | else | |
5bd61841 | 5402 | new_friend_result_template_info = NULL_TREE; |
36a117a5 | 5403 | |
b01e6d2b JM |
5404 | /* Make the init_value nonzero so pushdecl knows this is a defn. */ |
5405 | if (new_friend_is_defn) | |
5406 | DECL_INITIAL (new_friend) = error_mark_node; | |
5407 | ||
1c227897 MM |
5408 | /* Inside pushdecl_namespace_level, we will push into the |
5409 | current namespace. However, the friend function should go | |
c6002625 | 5410 | into the namespace of the template. */ |
92da7074 ML |
5411 | ns = decl_namespace_context (new_friend); |
5412 | push_nested_namespace (ns); | |
d63d5d0c | 5413 | old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true); |
92da7074 | 5414 | pop_nested_namespace (ns); |
36a117a5 | 5415 | |
0014c247 VR |
5416 | if (old_decl == error_mark_node) |
5417 | return error_mark_node; | |
5418 | ||
36a117a5 MM |
5419 | if (old_decl != new_friend) |
5420 | { | |
5421 | /* This new friend declaration matched an existing | |
5422 | declaration. For example, given: | |
5423 | ||
5424 | template <class T> void f(T); | |
c8094d83 MS |
5425 | template <class U> class C { |
5426 | template <class T> friend void f(T) {} | |
36a117a5 MM |
5427 | }; |
5428 | ||
5429 | the friend declaration actually provides the definition | |
5430 | of `f', once C has been instantiated for some type. So, | |
5431 | old_decl will be the out-of-class template declaration, | |
5432 | while new_friend is the in-class definition. | |
5433 | ||
5434 | But, if `f' was called before this point, the | |
5435 | instantiation of `f' will have DECL_TI_ARGS corresponding | |
5436 | to `T' but not to `U', references to which might appear | |
5437 | in the definition of `f'. Previously, the most general | |
5438 | template for an instantiation of `f' was the out-of-class | |
5439 | version; now it is the in-class version. Therefore, we | |
5440 | run through all specialization of `f', adding to their | |
5441 | DECL_TI_ARGS appropriately. In particular, they need a | |
5442 | new set of outer arguments, corresponding to the | |
c8094d83 | 5443 | arguments for this class instantiation. |
36a117a5 MM |
5444 | |
5445 | The same situation can arise with something like this: | |
5446 | ||
5447 | friend void f(int); | |
c8094d83 | 5448 | template <class T> class C { |
0cbd7506 MS |
5449 | friend void f(T) {} |
5450 | }; | |
36a117a5 MM |
5451 | |
5452 | when `C<int>' is instantiated. Now, `f(int)' is defined | |
5453 | in the class. */ | |
5454 | ||
fbf1c34b MM |
5455 | if (!new_friend_is_defn) |
5456 | /* On the other hand, if the in-class declaration does | |
5457 | *not* provide a definition, then we don't want to alter | |
5458 | existing definitions. We can just leave everything | |
5459 | alone. */ | |
36a117a5 | 5460 | ; |
fbf1c34b | 5461 | else |
36a117a5 | 5462 | { |
fbf1c34b MM |
5463 | /* Overwrite whatever template info was there before, if |
5464 | any, with the new template information pertaining to | |
5465 | the declaration. */ | |
5466 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; | |
5467 | ||
5468 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) | |
8d83f792 MM |
5469 | reregister_specialization (new_friend, |
5470 | most_general_template (old_decl), | |
5471 | old_decl); | |
c8094d83 | 5472 | else |
36a117a5 | 5473 | { |
fbf1c34b MM |
5474 | tree t; |
5475 | tree new_friend_args; | |
5476 | ||
c8094d83 | 5477 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
fbf1c34b | 5478 | = new_friend_result_template_info; |
c8094d83 | 5479 | |
fbf1c34b | 5480 | new_friend_args = TI_ARGS (new_friend_template_info); |
c8094d83 | 5481 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl); |
fbf1c34b MM |
5482 | t != NULL_TREE; |
5483 | t = TREE_CHAIN (t)) | |
5484 | { | |
5485 | tree spec = TREE_VALUE (t); | |
c8094d83 MS |
5486 | |
5487 | DECL_TI_ARGS (spec) | |
fbf1c34b MM |
5488 | = add_outermost_template_args (new_friend_args, |
5489 | DECL_TI_ARGS (spec)); | |
fbf1c34b MM |
5490 | } |
5491 | ||
5492 | /* Now, since specializations are always supposed to | |
5493 | hang off of the most general template, we must move | |
5494 | them. */ | |
5495 | t = most_general_template (old_decl); | |
5496 | if (t != old_decl) | |
5497 | { | |
5498 | DECL_TEMPLATE_SPECIALIZATIONS (t) | |
5499 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t), | |
5500 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl)); | |
5501 | DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE; | |
5502 | } | |
36a117a5 MM |
5503 | } |
5504 | } | |
5505 | ||
5506 | /* The information from NEW_FRIEND has been merged into OLD_DECL | |
5507 | by duplicate_decls. */ | |
5508 | new_friend = old_decl; | |
5509 | } | |
f84b4be9 | 5510 | } |
6e432b31 | 5511 | else |
f84b4be9 | 5512 | { |
6e432b31 KL |
5513 | tree context = DECL_CONTEXT (new_friend); |
5514 | bool dependent_p; | |
5515 | ||
5516 | /* In the code | |
5517 | template <class T> class C { | |
5518 | template <class U> friend void C1<U>::f (); // case 1 | |
5519 | friend void C2<T>::f (); // case 2 | |
5520 | }; | |
5521 | we only need to make sure CONTEXT is a complete type for | |
5522 | case 2. To distinguish between the two cases, we note that | |
5523 | CONTEXT of case 1 remains dependent type after tsubst while | |
5524 | this isn't true for case 2. */ | |
5525 | ++processing_template_decl; | |
5526 | dependent_p = dependent_type_p (context); | |
5527 | --processing_template_decl; | |
5528 | ||
5529 | if (!dependent_p | |
5530 | && !complete_type_or_else (context, NULL_TREE)) | |
5531 | return error_mark_node; | |
5532 | ||
5533 | if (COMPLETE_TYPE_P (context)) | |
5534 | { | |
5535 | /* Check to see that the declaration is really present, and, | |
5536 | possibly obtain an improved declaration. */ | |
5537 | tree fn = check_classfn (context, | |
5538 | new_friend, NULL_TREE); | |
5539 | ||
5540 | if (fn) | |
5541 | new_friend = fn; | |
5542 | } | |
f84b4be9 JM |
5543 | } |
5544 | ||
5545 | return new_friend; | |
5546 | } | |
5547 | ||
1aed5355 MM |
5548 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
5549 | template arguments, as for tsubst. | |
6757edfe | 5550 | |
dc957d14 | 5551 | Returns an appropriate tsubst'd friend type or error_mark_node on |
4b054b80 | 5552 | failure. */ |
6757edfe MM |
5553 | |
5554 | static tree | |
3a978d72 | 5555 | tsubst_friend_class (tree friend_tmpl, tree args) |
6757edfe | 5556 | { |
1aed5355 | 5557 | tree friend_type; |
25aab5d0 | 5558 | tree tmpl; |
3e0ec82f | 5559 | tree context; |
6757edfe | 5560 | |
3e0ec82f MM |
5561 | context = DECL_CONTEXT (friend_tmpl); |
5562 | ||
5563 | if (context) | |
77adef84 | 5564 | { |
3e0ec82f MM |
5565 | if (TREE_CODE (context) == NAMESPACE_DECL) |
5566 | push_nested_namespace (context); | |
5567 | else | |
c8094d83 | 5568 | push_nested_class (tsubst (context, args, tf_none, NULL_TREE)); |
3e0ec82f | 5569 | } |
25aab5d0 | 5570 | |
105d72c5 MM |
5571 | /* Look for a class template declaration. We look for hidden names |
5572 | because two friend declarations of the same template are the | |
5573 | same. For example, in: | |
5574 | ||
5575 | struct A { | |
5576 | template <typename> friend class F; | |
5577 | }; | |
5578 | template <typename> struct B { | |
5579 | template <typename> friend class F; | |
5580 | }; | |
5581 | ||
5582 | both F templates are the same. */ | |
5583 | tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0, | |
5584 | /*block_p=*/true, 0, | |
5585 | LOOKUP_COMPLAIN | LOOKUP_HIDDEN); | |
25aab5d0 | 5586 | |
3e0ec82f MM |
5587 | /* But, if we don't find one, it might be because we're in a |
5588 | situation like this: | |
77adef84 | 5589 | |
3e0ec82f MM |
5590 | template <class T> |
5591 | struct S { | |
5592 | template <class U> | |
5593 | friend struct S; | |
5594 | }; | |
5595 | ||
5596 | Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL | |
5597 | for `S<int>', not the TEMPLATE_DECL. */ | |
5598 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
5599 | { | |
10e6657a | 5600 | tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1); |
3e0ec82f | 5601 | tmpl = maybe_get_template_decl_from_type_decl (tmpl); |
25aab5d0 | 5602 | } |
6757edfe | 5603 | |
25aab5d0 | 5604 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
6757edfe MM |
5605 | { |
5606 | /* The friend template has already been declared. Just | |
36a117a5 MM |
5607 | check to see that the declarations match, and install any new |
5608 | default parameters. We must tsubst the default parameters, | |
5609 | of course. We only need the innermost template parameters | |
5610 | because that is all that redeclare_class_template will look | |
5611 | at. */ | |
3e0ec82f MM |
5612 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) |
5613 | > TMPL_ARGS_DEPTH (args)) | |
5614 | { | |
5615 | tree parms; | |
5616 | parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), | |
23fca1f5 | 5617 | args, tf_warning_or_error); |
3e0ec82f MM |
5618 | redeclare_class_template (TREE_TYPE (tmpl), parms); |
5619 | } | |
5620 | ||
6757edfe MM |
5621 | friend_type = TREE_TYPE (tmpl); |
5622 | } | |
5623 | else | |
5624 | { | |
5625 | /* The friend template has not already been declared. In this | |
5626 | case, the instantiation of the template class will cause the | |
5627 | injection of this template into the global scope. */ | |
23fca1f5 | 5628 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
dca56f77 VR |
5629 | if (tmpl == error_mark_node) |
5630 | return error_mark_node; | |
6757edfe MM |
5631 | |
5632 | /* The new TMPL is not an instantiation of anything, so we | |
0cbd7506 | 5633 | forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for |
6757edfe MM |
5634 | the new type because that is supposed to be the corresponding |
5635 | template decl, i.e., TMPL. */ | |
5636 | DECL_USE_TEMPLATE (tmpl) = 0; | |
5637 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; | |
5638 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; | |
68c78847 KL |
5639 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
5640 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); | |
6757edfe MM |
5641 | |
5642 | /* Inject this template into the global scope. */ | |
d63d5d0c | 5643 | friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true)); |
6757edfe MM |
5644 | } |
5645 | ||
c8094d83 | 5646 | if (context) |
3e0ec82f MM |
5647 | { |
5648 | if (TREE_CODE (context) == NAMESPACE_DECL) | |
5649 | pop_nested_namespace (context); | |
5650 | else | |
5651 | pop_nested_class (); | |
5652 | } | |
5653 | ||
6757edfe MM |
5654 | return friend_type; |
5655 | } | |
f84b4be9 | 5656 | |
17f29637 KL |
5657 | /* Returns zero if TYPE cannot be completed later due to circularity. |
5658 | Otherwise returns one. */ | |
5659 | ||
d5372501 | 5660 | static int |
3a978d72 | 5661 | can_complete_type_without_circularity (tree type) |
17f29637 KL |
5662 | { |
5663 | if (type == NULL_TREE || type == error_mark_node) | |
5664 | return 0; | |
5665 | else if (COMPLETE_TYPE_P (type)) | |
5666 | return 1; | |
5667 | else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type)) | |
5668 | return can_complete_type_without_circularity (TREE_TYPE (type)); | |
8c6ab2db NS |
5669 | else if (CLASS_TYPE_P (type) |
5670 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) | |
17f29637 KL |
5671 | return 0; |
5672 | else | |
5673 | return 1; | |
5674 | } | |
5675 | ||
8d08fdba | 5676 | tree |
3a978d72 | 5677 | instantiate_class_template (tree type) |
8d08fdba | 5678 | { |
7088fca9 | 5679 | tree template, args, pattern, t, member; |
36a117a5 | 5680 | tree typedecl; |
dbbf88d1 | 5681 | tree pbinfo; |
cad7e87b | 5682 | tree base_list; |
c8094d83 | 5683 | |
5566b478 | 5684 | if (type == error_mark_node) |
8d08fdba MS |
5685 | return error_mark_node; |
5686 | ||
c8094d83 | 5687 | if (TYPE_BEING_DEFINED (type) |
ca099ac8 MM |
5688 | || COMPLETE_TYPE_P (type) |
5689 | || dependent_type_p (type)) | |
5566b478 MS |
5690 | return type; |
5691 | ||
6bdb985f | 5692 | /* Figure out which template is being instantiated. */ |
36a117a5 | 5693 | template = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
50bc768d | 5694 | gcc_assert (TREE_CODE (template) == TEMPLATE_DECL); |
73aad9b9 | 5695 | |
4c571114 MM |
5696 | /* Determine what specialization of the original template to |
5697 | instantiate. */ | |
916b63c3 | 5698 | t = most_specialized_class (type, template); |
8fbc5ae7 | 5699 | if (t == error_mark_node) |
73aad9b9 | 5700 | { |
8fbc5ae7 MM |
5701 | TYPE_BEING_DEFINED (type) = 1; |
5702 | return error_mark_node; | |
73aad9b9 | 5703 | } |
916b63c3 MM |
5704 | else if (t) |
5705 | { | |
5706 | /* This TYPE is actually an instantiation of a partial | |
5707 | specialization. We replace the innermost set of ARGS with | |
5708 | the arguments appropriate for substitution. For example, | |
5709 | given: | |
3db45ab5 MS |
5710 | |
5711 | template <class T> struct S {}; | |
916b63c3 | 5712 | template <class T> struct S<T*> {}; |
6bdb985f | 5713 | |
916b63c3 | 5714 | and supposing that we are instantiating S<int*>, ARGS will |
3db45ab5 | 5715 | presently be {int*} -- but we need {int}. */ |
916b63c3 MM |
5716 | pattern = TREE_TYPE (t); |
5717 | args = TREE_PURPOSE (t); | |
5718 | } | |
73aad9b9 | 5719 | else |
916b63c3 MM |
5720 | { |
5721 | pattern = TREE_TYPE (template); | |
5722 | args = CLASSTYPE_TI_ARGS (type); | |
5723 | } | |
5566b478 | 5724 | |
4c571114 MM |
5725 | /* If the template we're instantiating is incomplete, then clearly |
5726 | there's nothing we can do. */ | |
d0f062fb | 5727 | if (!COMPLETE_TYPE_P (pattern)) |
f31c0a32 | 5728 | return type; |
5566b478 | 5729 | |
4c571114 MM |
5730 | /* If we've recursively instantiated too many templates, stop. */ |
5731 | if (! push_tinst_level (type)) | |
f31c0a32 | 5732 | return type; |
4c571114 MM |
5733 | |
5734 | /* Now we're really doing the instantiation. Mark the type as in | |
5735 | the process of being defined. */ | |
5736 | TYPE_BEING_DEFINED (type) = 1; | |
5737 | ||
78757caa KL |
5738 | /* We may be in the middle of deferred access check. Disable |
5739 | it now. */ | |
5740 | push_deferring_access_checks (dk_no_deferred); | |
5741 | ||
c353b8e3 | 5742 | push_to_top_level (); |
4c571114 | 5743 | |
7813d14c | 5744 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
8d08fdba | 5745 | |
68ea098a NS |
5746 | /* Set the input location to the template definition. This is needed |
5747 | if tsubsting causes an error. */ | |
12af7ba3 MM |
5748 | typedecl = TYPE_MAIN_DECL (type); |
5749 | input_location = DECL_SOURCE_LOCATION (typedecl); | |
5750 | in_system_header = DECL_IN_SYSTEM_HEADER (typedecl); | |
68ea098a | 5751 | |
f7da6097 | 5752 | TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern); |
834c6dff MM |
5753 | TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern); |
5754 | TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern); | |
f7da6097 | 5755 | TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern); |
f7da6097 MS |
5756 | TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern); |
5757 | TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern); | |
f7da6097 MS |
5758 | TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern); |
5759 | TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern); | |
f7da6097 MS |
5760 | TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern); |
5761 | TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern); | |
f7da6097 MS |
5762 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
5763 | TYPE_ALIGN (type) = TYPE_ALIGN (pattern); | |
11cf4d18 | 5764 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
eff71ab0 | 5765 | TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */ |
6bdb8141 JM |
5766 | if (ANON_AGGR_TYPE_P (pattern)) |
5767 | SET_ANON_AGGR_TYPE_P (type); | |
8d039470 MS |
5768 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
5769 | { | |
5770 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; | |
5771 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); | |
5772 | } | |
f7da6097 | 5773 | |
dbbf88d1 | 5774 | pbinfo = TYPE_BINFO (pattern); |
1456deaf | 5775 | |
315fb5db NS |
5776 | /* We should never instantiate a nested class before its enclosing |
5777 | class; we need to look up the nested class by name before we can | |
5778 | instantiate it, and that lookup should instantiate the enclosing | |
5779 | class. */ | |
5780 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) | |
5781 | || COMPLETE_TYPE_P (TYPE_CONTEXT (type)) | |
5782 | || TYPE_BEING_DEFINED (TYPE_CONTEXT (type))); | |
1456deaf | 5783 | |
cad7e87b | 5784 | base_list = NULL_TREE; |
fa743e8c | 5785 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
3fd71a52 | 5786 | { |
fa743e8c | 5787 | tree pbase_binfo; |
a2507277 | 5788 | tree context = TYPE_CONTEXT (type); |
4514aa8c | 5789 | tree pushed_scope; |
3fd71a52 | 5790 | int i; |
5566b478 | 5791 | |
a2507277 NS |
5792 | /* We must enter the scope containing the type, as that is where |
5793 | the accessibility of types named in dependent bases are | |
5794 | looked up from. */ | |
4514aa8c | 5795 | pushed_scope = push_scope (context ? context : global_namespace); |
c8094d83 | 5796 | |
3fd71a52 MM |
5797 | /* Substitute into each of the bases to determine the actual |
5798 | basetypes. */ | |
fa743e8c | 5799 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
3fd71a52 MM |
5800 | { |
5801 | tree base; | |
fa743e8c | 5802 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
711734a9 | 5803 | |
dc957d14 | 5804 | /* Substitute to figure out the base class. */ |
fa743e8c | 5805 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE); |
3fd71a52 MM |
5806 | if (base == error_mark_node) |
5807 | continue; | |
c8094d83 | 5808 | |
3fd71a52 | 5809 | base_list = tree_cons (access, base, base_list); |
fa743e8c | 5810 | if (BINFO_VIRTUAL_P (pbase_binfo)) |
809e3e7f | 5811 | TREE_TYPE (base_list) = integer_type_node; |
3fd71a52 | 5812 | } |
dfbcd65a | 5813 | |
3fd71a52 MM |
5814 | /* The list is now in reverse order; correct that. */ |
5815 | base_list = nreverse (base_list); | |
5816 | ||
4514aa8c NS |
5817 | if (pushed_scope) |
5818 | pop_scope (pushed_scope); | |
3fd71a52 | 5819 | } |
cad7e87b NS |
5820 | /* Now call xref_basetypes to set up all the base-class |
5821 | information. */ | |
5822 | xref_basetypes (type, base_list); | |
5823 | ||
5566b478 | 5824 | |
b74a0560 MM |
5825 | /* Now that our base classes are set up, enter the scope of the |
5826 | class, so that name lookups into base classes, etc. will work | |
dc957d14 | 5827 | correctly. This is precisely analogous to what we do in |
b74a0560 MM |
5828 | begin_class_definition when defining an ordinary non-template |
5829 | class. */ | |
29370796 | 5830 | pushclass (type); |
b74a0560 | 5831 | |
7088fca9 | 5832 | /* Now members are processed in the order of declaration. */ |
8c6ab2db NS |
5833 | for (member = CLASSTYPE_DECL_LIST (pattern); |
5834 | member; member = TREE_CHAIN (member)) | |
8d08fdba | 5835 | { |
7088fca9 | 5836 | tree t = TREE_VALUE (member); |
5566b478 | 5837 | |
7088fca9 | 5838 | if (TREE_PURPOSE (member)) |
ed44da02 | 5839 | { |
7088fca9 KL |
5840 | if (TYPE_P (t)) |
5841 | { | |
5e0c54e5 | 5842 | /* Build new CLASSTYPE_NESTED_UTDS. */ |
8d08fdba | 5843 | |
7088fca9 | 5844 | tree newtag; |
883a2bff MM |
5845 | bool class_template_p; |
5846 | ||
2678bae8 VR |
5847 | class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE |
5848 | && TYPE_LANG_SPECIFIC (t) | |
5849 | && CLASSTYPE_IS_TEMPLATE (t)); | |
883a2bff | 5850 | /* If the member is a class template, then -- even after |
6c745393 | 5851 | substitution -- there may be dependent types in the |
883a2bff MM |
5852 | template argument list for the class. We increment |
5853 | PROCESSING_TEMPLATE_DECL so that dependent_type_p, as | |
5854 | that function will assume that no types are dependent | |
5855 | when outside of a template. */ | |
5856 | if (class_template_p) | |
5857 | ++processing_template_decl; | |
2678bae8 | 5858 | newtag = tsubst (t, args, tf_error, NULL_TREE); |
883a2bff MM |
5859 | if (class_template_p) |
5860 | --processing_template_decl; | |
2620d095 KL |
5861 | if (newtag == error_mark_node) |
5862 | continue; | |
5863 | ||
7088fca9 KL |
5864 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
5865 | { | |
2678bae8 VR |
5866 | tree name = TYPE_IDENTIFIER (t); |
5867 | ||
883a2bff | 5868 | if (class_template_p) |
7088fca9 KL |
5869 | /* Unfortunately, lookup_template_class sets |
5870 | CLASSTYPE_IMPLICIT_INSTANTIATION for a partial | |
3afd2e20 NS |
5871 | instantiation (i.e., for the type of a member |
5872 | template class nested within a template class.) | |
5873 | This behavior is required for | |
5874 | maybe_process_partial_specialization to work | |
5875 | correctly, but is not accurate in this case; | |
5876 | the TAG is not an instantiation of anything. | |
5877 | (The corresponding TEMPLATE_DECL is an | |
5878 | instantiation, but the TYPE is not.) */ | |
7088fca9 KL |
5879 | CLASSTYPE_USE_TEMPLATE (newtag) = 0; |
5880 | ||
5881 | /* Now, we call pushtag to put this NEWTAG into the scope of | |
5882 | TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid | |
5883 | pushtag calling push_template_decl. We don't have to do | |
5884 | this for enums because it will already have been done in | |
5885 | tsubst_enum. */ | |
5886 | if (name) | |
5887 | SET_IDENTIFIER_TYPE_VALUE (name, newtag); | |
bd3d082e | 5888 | pushtag (name, newtag, /*tag_scope=*/ts_current); |
7088fca9 KL |
5889 | } |
5890 | } | |
c8094d83 | 5891 | else if (TREE_CODE (t) == FUNCTION_DECL |
7088fca9 KL |
5892 | || DECL_FUNCTION_TEMPLATE_P (t)) |
5893 | { | |
5894 | /* Build new TYPE_METHODS. */ | |
fecafe5e | 5895 | tree r; |
c8094d83 | 5896 | |
fecafe5e | 5897 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 5898 | ++processing_template_decl; |
fecafe5e NS |
5899 | r = tsubst (t, args, tf_error, NULL_TREE); |
5900 | if (TREE_CODE (t) == TEMPLATE_DECL) | |
9579624e | 5901 | --processing_template_decl; |
7088fca9 | 5902 | set_current_access_from_decl (r); |
7088fca9 KL |
5903 | finish_member_declaration (r); |
5904 | } | |
5905 | else | |
5906 | { | |
5907 | /* Build new TYPE_FIELDS. */ | |
55a3debe DG |
5908 | if (TREE_CODE (t) == STATIC_ASSERT) |
5909 | { | |
5910 | tree condition = | |
5911 | tsubst_expr (STATIC_ASSERT_CONDITION (t), args, | |
5912 | tf_warning_or_error, NULL_TREE, | |
5913 | /*integral_constant_expression_p=*/true); | |
5914 | finish_static_assert (condition, | |
5915 | STATIC_ASSERT_MESSAGE (t), | |
5916 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
5917 | /*member_p=*/true); | |
5918 | } | |
5919 | else if (TREE_CODE (t) != CONST_DECL) | |
7088fca9 KL |
5920 | { |
5921 | tree r; | |
fa8d6e85 | 5922 | |
d479d37f NS |
5923 | /* The the file and line for this declaration, to |
5924 | assist in error message reporting. Since we | |
5925 | called push_tinst_level above, we don't need to | |
5926 | restore these. */ | |
f31686a3 | 5927 | input_location = DECL_SOURCE_LOCATION (t); |
fa8d6e85 | 5928 | |
fb5ce3c9 | 5929 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 5930 | ++processing_template_decl; |
23fca1f5 | 5931 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
fb5ce3c9 | 5932 | if (TREE_CODE (t) == TEMPLATE_DECL) |
9579624e | 5933 | --processing_template_decl; |
7088fca9 KL |
5934 | if (TREE_CODE (r) == VAR_DECL) |
5935 | { | |
b794e321 MM |
5936 | /* In [temp.inst]: |
5937 | ||
5938 | [t]he initialization (and any associated | |
5939 | side-effects) of a static data member does | |
5940 | not occur unless the static data member is | |
5941 | itself used in a way that requires the | |
5942 | definition of the static data member to | |
3db45ab5 | 5943 | exist. |
b794e321 MM |
5944 | |
5945 | Therefore, we do not substitute into the | |
3db45ab5 MS |
5946 | initialized for the static data member here. */ |
5947 | finish_static_data_member_decl | |
5948 | (r, | |
5949 | /*init=*/NULL_TREE, | |
d174af6c | 5950 | /*init_const_expr_p=*/false, |
3db45ab5 | 5951 | /*asmspec_tree=*/NULL_TREE, |
b794e321 | 5952 | /*flags=*/0); |
7088fca9 KL |
5953 | if (DECL_INITIALIZED_IN_CLASS_P (r)) |
5954 | check_static_variable_definition (r, TREE_TYPE (r)); | |
5955 | } | |
5956 | else if (TREE_CODE (r) == FIELD_DECL) | |
5957 | { | |
5958 | /* Determine whether R has a valid type and can be | |
5959 | completed later. If R is invalid, then it is | |
5960 | replaced by error_mark_node so that it will not be | |
5961 | added to TYPE_FIELDS. */ | |
5962 | tree rtype = TREE_TYPE (r); | |
5963 | if (can_complete_type_without_circularity (rtype)) | |
5964 | complete_type (rtype); | |
5965 | ||
5966 | if (!COMPLETE_TYPE_P (rtype)) | |
5967 | { | |
5968 | cxx_incomplete_type_error (r, rtype); | |
0cbd7506 | 5969 | r = error_mark_node; |
7088fca9 KL |
5970 | } |
5971 | } | |
5566b478 | 5972 | |
7088fca9 KL |
5973 | /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE, |
5974 | such a thing will already have been added to the field | |
5975 | list by tsubst_enum in finish_member_declaration in the | |
5e0c54e5 | 5976 | CLASSTYPE_NESTED_UTDS case above. */ |
7088fca9 KL |
5977 | if (!(TREE_CODE (r) == TYPE_DECL |
5978 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE | |
43b26a01 | 5979 | && DECL_ARTIFICIAL (r))) |
7088fca9 KL |
5980 | { |
5981 | set_current_access_from_decl (r); | |
5982 | finish_member_declaration (r); | |
5983 | } | |
0cbd7506 | 5984 | } |
7088fca9 | 5985 | } |
61fc8c9e | 5986 | } |
7088fca9 KL |
5987 | else |
5988 | { | |
5989 | if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)) | |
5990 | { | |
5991 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ | |
5992 | ||
5993 | tree friend_type = t; | |
b939a023 | 5994 | bool adjust_processing_template_decl = false; |
1aed5355 | 5995 | |
7088fca9 | 5996 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
b939a023 | 5997 | { |
5a24482e | 5998 | /* template <class T> friend class C; */ |
b939a023 | 5999 | friend_type = tsubst_friend_class (friend_type, args); |
0cbd7506 | 6000 | adjust_processing_template_decl = true; |
b939a023 KL |
6001 | } |
6002 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) | |
6003 | { | |
5a24482e | 6004 | /* template <class T> friend class C::D; */ |
b939a023 | 6005 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 6006 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
6007 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
6008 | friend_type = TREE_TYPE (friend_type); | |
0cbd7506 | 6009 | adjust_processing_template_decl = true; |
b939a023 KL |
6010 | } |
6011 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE) | |
6012 | { | |
5a24482e KL |
6013 | /* This could be either |
6014 | ||
6015 | friend class T::C; | |
6016 | ||
6017 | when dependent_type_p is false or | |
6018 | ||
6019 | template <class U> friend class T::C; | |
6020 | ||
6021 | otherwise. */ | |
b939a023 | 6022 | friend_type = tsubst (friend_type, args, |
23fca1f5 | 6023 | tf_warning_or_error, NULL_TREE); |
b939a023 KL |
6024 | /* Bump processing_template_decl for correct |
6025 | dependent_type_p calculation. */ | |
6026 | ++processing_template_decl; | |
6027 | if (dependent_type_p (friend_type)) | |
6028 | adjust_processing_template_decl = true; | |
6029 | --processing_template_decl; | |
6030 | } | |
5a24482e KL |
6031 | else if (!CLASSTYPE_USE_TEMPLATE (friend_type) |
6032 | && hidden_name_p (TYPE_NAME (friend_type))) | |
7088fca9 | 6033 | { |
5a24482e KL |
6034 | /* friend class C; |
6035 | ||
6036 | where C hasn't been declared yet. Let's lookup name | |
6037 | from namespace scope directly, bypassing any name that | |
6038 | come from dependent base class. */ | |
7088fca9 KL |
6039 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type)); |
6040 | ||
6041 | /* The call to xref_tag_from_type does injection for friend | |
6042 | classes. */ | |
6043 | push_nested_namespace (ns); | |
c8094d83 MS |
6044 | friend_type = |
6045 | xref_tag_from_type (friend_type, NULL_TREE, | |
5a24482e | 6046 | /*tag_scope=*/ts_current); |
7088fca9 KL |
6047 | pop_nested_namespace (ns); |
6048 | } | |
5a24482e KL |
6049 | else if (uses_template_parms (friend_type)) |
6050 | /* friend class C<T>; */ | |
6051 | friend_type = tsubst (friend_type, args, | |
23fca1f5 | 6052 | tf_warning_or_error, NULL_TREE); |
5a24482e KL |
6053 | /* Otherwise it's |
6054 | ||
6055 | friend class C; | |
6056 | ||
6057 | where C is already declared or | |
6058 | ||
6059 | friend class C<int>; | |
6060 | ||
0cbd7506 | 6061 | We don't have to do anything in these cases. */ |
1aed5355 | 6062 | |
b939a023 | 6063 | if (adjust_processing_template_decl) |
7088fca9 KL |
6064 | /* Trick make_friend_class into realizing that the friend |
6065 | we're adding is a template, not an ordinary class. It's | |
6066 | important that we use make_friend_class since it will | |
6067 | perform some error-checking and output cross-reference | |
6068 | information. */ | |
6069 | ++processing_template_decl; | |
fc378698 | 6070 | |
b939a023 | 6071 | if (friend_type != error_mark_node) |
0cbd7506 | 6072 | make_friend_class (type, friend_type, /*complain=*/false); |
fc378698 | 6073 | |
b939a023 | 6074 | if (adjust_processing_template_decl) |
7088fca9 KL |
6075 | --processing_template_decl; |
6076 | } | |
6077 | else | |
9579624e KL |
6078 | { |
6079 | /* Build new DECL_FRIENDLIST. */ | |
6080 | tree r; | |
6081 | ||
6e432b31 KL |
6082 | /* The the file and line for this declaration, to |
6083 | assist in error message reporting. Since we | |
6084 | called push_tinst_level above, we don't need to | |
6085 | restore these. */ | |
6086 | input_location = DECL_SOURCE_LOCATION (t); | |
6087 | ||
9579624e | 6088 | if (TREE_CODE (t) == TEMPLATE_DECL) |
cad7e87b NS |
6089 | { |
6090 | ++processing_template_decl; | |
6091 | push_deferring_access_checks (dk_no_check); | |
6092 | } | |
c8094d83 | 6093 | |
9579624e | 6094 | r = tsubst_friend_function (t, args); |
9579624e | 6095 | add_friend (type, r, /*complain=*/false); |
cad7e87b NS |
6096 | if (TREE_CODE (t) == TEMPLATE_DECL) |
6097 | { | |
6098 | pop_deferring_access_checks (); | |
6099 | --processing_template_decl; | |
6100 | } | |
9579624e | 6101 | } |
7088fca9 KL |
6102 | } |
6103 | } | |
5566b478 | 6104 | |
61a127b3 MM |
6105 | /* Set the file and line number information to whatever is given for |
6106 | the class itself. This puts error messages involving generated | |
6107 | implicit functions at a predictable point, and the same point | |
6108 | that would be used for non-template classes. */ | |
f31686a3 | 6109 | input_location = DECL_SOURCE_LOCATION (typedecl); |
6de9cd9a | 6110 | |
61a127b3 | 6111 | unreverse_member_declarations (type); |
9f33663b | 6112 | finish_struct_1 (type); |
5524676d | 6113 | TYPE_BEING_DEFINED (type) = 0; |
8d08fdba | 6114 | |
9188c363 MM |
6115 | /* Now that the class is complete, instantiate default arguments for |
6116 | any member functions. We don't do this earlier because the | |
6117 | default arguments may reference members of the class. */ | |
6118 | if (!PRIMARY_TEMPLATE_P (template)) | |
6119 | for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t)) | |
c8094d83 | 6120 | if (TREE_CODE (t) == FUNCTION_DECL |
dc957d14 | 6121 | /* Implicitly generated member functions will not have template |
9188c363 MM |
6122 | information; they are not instantiations, but instead are |
6123 | created "fresh" for each instantiation. */ | |
6124 | && DECL_TEMPLATE_INFO (t)) | |
6125 | tsubst_default_arguments (t); | |
6126 | ||
b74a0560 | 6127 | popclass (); |
5566b478 | 6128 | pop_from_top_level (); |
78757caa | 6129 | pop_deferring_access_checks (); |
5566b478 MS |
6130 | pop_tinst_level (); |
6131 | ||
4684cd27 MM |
6132 | /* The vtable for a template class can be emitted in any translation |
6133 | unit in which the class is instantiated. When there is no key | |
6134 | method, however, finish_struct_1 will already have added TYPE to | |
6135 | the keyed_classes list. */ | |
6136 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) | |
9aad8f83 MA |
6137 | keyed_classes = tree_cons (NULL_TREE, type, keyed_classes); |
6138 | ||
5566b478 | 6139 | return type; |
8d08fdba MS |
6140 | } |
6141 | ||
00d3396f | 6142 | static tree |
a91db711 | 6143 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
00d3396f | 6144 | { |
a91db711 | 6145 | tree r; |
c8094d83 | 6146 | |
a91db711 NS |
6147 | if (!t) |
6148 | r = t; | |
6149 | else if (TYPE_P (t)) | |
6150 | r = tsubst (t, args, complain, in_decl); | |
6151 | else | |
00d3396f | 6152 | { |
015c2c66 MM |
6153 | r = tsubst_expr (t, args, complain, in_decl, |
6154 | /*integral_constant_expression_p=*/true); | |
6f25cb35 | 6155 | r = fold_non_dependent_expr (r); |
bd83b409 | 6156 | } |
a91db711 | 6157 | return r; |
bd83b409 NS |
6158 | } |
6159 | ||
a91db711 | 6160 | /* Substitute ARGS into the vector or list of template arguments T. */ |
830bfa74 | 6161 | |
e9659ab0 | 6162 | static tree |
a91db711 | 6163 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
830bfa74 | 6164 | { |
bf12d54d | 6165 | int len = TREE_VEC_LENGTH (t); |
a91db711 | 6166 | int need_new = 0, i; |
67f5655f | 6167 | tree *elts = (tree *) alloca (len * sizeof (tree)); |
c8094d83 | 6168 | |
830bfa74 MM |
6169 | for (i = 0; i < len; i++) |
6170 | { | |
bf12d54d NS |
6171 | tree orig_arg = TREE_VEC_ELT (t, i); |
6172 | tree new_arg; | |
a91db711 | 6173 | |
bf12d54d NS |
6174 | if (TREE_CODE (orig_arg) == TREE_VEC) |
6175 | new_arg = tsubst_template_args (orig_arg, args, complain, in_decl); | |
830bfa74 | 6176 | else |
a91db711 | 6177 | new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl); |
c8094d83 | 6178 | |
a91db711 | 6179 | if (new_arg == error_mark_node) |
08e72a19 JM |
6180 | return error_mark_node; |
6181 | ||
a91db711 NS |
6182 | elts[i] = new_arg; |
6183 | if (new_arg != orig_arg) | |
830bfa74 MM |
6184 | need_new = 1; |
6185 | } | |
c8094d83 | 6186 | |
830bfa74 MM |
6187 | if (!need_new) |
6188 | return t; | |
a91db711 | 6189 | |
bf12d54d NS |
6190 | t = make_tree_vec (len); |
6191 | for (i = 0; i < len; i++) | |
6192 | TREE_VEC_ELT (t, i) = elts[i]; | |
c8094d83 | 6193 | |
830bfa74 MM |
6194 | return t; |
6195 | } | |
6196 | ||
36a117a5 MM |
6197 | /* Return the result of substituting ARGS into the template parameters |
6198 | given by PARMS. If there are m levels of ARGS and m + n levels of | |
6199 | PARMS, then the result will contain n levels of PARMS. For | |
6200 | example, if PARMS is `template <class T> template <class U> | |
6201 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the | |
6202 | result will be `template <int*, double, class V>'. */ | |
6203 | ||
e9659ab0 | 6204 | static tree |
3a978d72 | 6205 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
36a117a5 | 6206 | { |
f71f87f9 MM |
6207 | tree r = NULL_TREE; |
6208 | tree* new_parms; | |
36a117a5 | 6209 | |
8ca4bf25 MM |
6210 | /* When substituting into a template, we must set |
6211 | PROCESSING_TEMPLATE_DECL as the template parameters may be | |
6212 | dependent if they are based on one-another, and the dependency | |
6213 | predicates are short-circuit outside of templates. */ | |
6214 | ++processing_template_decl; | |
6215 | ||
36a117a5 MM |
6216 | for (new_parms = &r; |
6217 | TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); | |
6218 | new_parms = &(TREE_CHAIN (*new_parms)), | |
6219 | parms = TREE_CHAIN (parms)) | |
6220 | { | |
c8094d83 | 6221 | tree new_vec = |
36a117a5 MM |
6222 | make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms))); |
6223 | int i; | |
c8094d83 | 6224 | |
36a117a5 MM |
6225 | for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
6226 | { | |
42b304f1 LM |
6227 | tree tuple; |
6228 | tree default_value; | |
6229 | tree parm_decl; | |
6230 | ||
6231 | if (parms == error_mark_node) | |
6232 | continue; | |
6233 | ||
6234 | tuple = TREE_VEC_ELT (TREE_VALUE (parms), i); | |
6235 | ||
6236 | if (tuple == error_mark_node) | |
6237 | continue; | |
6238 | ||
6239 | default_value = TREE_PURPOSE (tuple); | |
6240 | parm_decl = TREE_VALUE (tuple); | |
833aa4c4 NS |
6241 | |
6242 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); | |
a207780f VR |
6243 | if (TREE_CODE (parm_decl) == PARM_DECL |
6244 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) | |
6245 | parm_decl = error_mark_node; | |
a91db711 NS |
6246 | default_value = tsubst_template_arg (default_value, args, |
6247 | complain, NULL_TREE); | |
c8094d83 | 6248 | |
a91db711 | 6249 | tuple = build_tree_list (default_value, parm_decl); |
833aa4c4 | 6250 | TREE_VEC_ELT (new_vec, i) = tuple; |
36a117a5 | 6251 | } |
c8094d83 MS |
6252 | |
6253 | *new_parms = | |
6254 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) | |
4890c2f4 | 6255 | - TMPL_ARGS_DEPTH (args)), |
36a117a5 MM |
6256 | new_vec, NULL_TREE); |
6257 | } | |
6258 | ||
8ca4bf25 MM |
6259 | --processing_template_decl; |
6260 | ||
36a117a5 MM |
6261 | return r; |
6262 | } | |
6263 | ||
ed44da02 MM |
6264 | /* Substitute the ARGS into the indicated aggregate (or enumeration) |
6265 | type T. If T is not an aggregate or enumeration type, it is | |
6266 | handled as if by tsubst. IN_DECL is as for tsubst. If | |
838dfd8a | 6267 | ENTERING_SCOPE is nonzero, T is the context for a template which |
dc957d14 | 6268 | we are presently tsubst'ing. Return the substituted value. */ |
36a117a5 | 6269 | |
e9659ab0 | 6270 | static tree |
c8094d83 | 6271 | tsubst_aggr_type (tree t, |
0cbd7506 MS |
6272 | tree args, |
6273 | tsubst_flags_t complain, | |
6274 | tree in_decl, | |
6275 | int entering_scope) | |
36a117a5 MM |
6276 | { |
6277 | if (t == NULL_TREE) | |
6278 | return NULL_TREE; | |
6279 | ||
6280 | switch (TREE_CODE (t)) | |
6281 | { | |
6282 | case RECORD_TYPE: | |
6283 | if (TYPE_PTRMEMFUNC_P (t)) | |
46c895ac | 6284 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
36a117a5 | 6285 | |
f4f206f4 | 6286 | /* Else fall through. */ |
ed44da02 | 6287 | case ENUMERAL_TYPE: |
36a117a5 | 6288 | case UNION_TYPE: |
5db698f6 | 6289 | if (TYPE_TEMPLATE_INFO (t)) |
36a117a5 MM |
6290 | { |
6291 | tree argvec; | |
6292 | tree context; | |
6293 | tree r; | |
3489ea0c MM |
6294 | bool saved_skip_evaluation; |
6295 | ||
6296 | /* In "sizeof(X<I>)" we need to evaluate "I". */ | |
6297 | saved_skip_evaluation = skip_evaluation; | |
6298 | skip_evaluation = false; | |
36a117a5 MM |
6299 | |
6300 | /* First, determine the context for the type we are looking | |
6301 | up. */ | |
4f7847ca NS |
6302 | context = TYPE_CONTEXT (t); |
6303 | if (context) | |
6304 | context = tsubst_aggr_type (context, args, complain, | |
36a117a5 | 6305 | in_decl, /*entering_scope=*/1); |
36a117a5 MM |
6306 | |
6307 | /* Then, figure out what arguments are appropriate for the | |
6308 | type we are trying to find. For example, given: | |
6309 | ||
6310 | template <class T> struct S; | |
6311 | template <class T, class U> void f(T, U) { S<U> su; } | |
6312 | ||
6313 | and supposing that we are instantiating f<int, double>, | |
6314 | then our ARGS will be {int, double}, but, when looking up | |
6315 | S we only want {double}. */ | |
a91db711 NS |
6316 | argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
6317 | complain, in_decl); | |
08e72a19 | 6318 | if (argvec == error_mark_node) |
3489ea0c MM |
6319 | r = error_mark_node; |
6320 | else | |
6321 | { | |
6322 | r = lookup_template_class (t, argvec, in_decl, context, | |
6323 | entering_scope, complain); | |
6324 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); | |
6325 | } | |
3db45ab5 | 6326 | |
3489ea0c | 6327 | skip_evaluation = saved_skip_evaluation; |
36a117a5 | 6328 | |
3489ea0c | 6329 | return r; |
36a117a5 | 6330 | } |
c8094d83 | 6331 | else |
36a117a5 MM |
6332 | /* This is not a template type, so there's nothing to do. */ |
6333 | return t; | |
6334 | ||
6335 | default: | |
4393e105 | 6336 | return tsubst (t, args, complain, in_decl); |
36a117a5 MM |
6337 | } |
6338 | } | |
6339 | ||
9188c363 MM |
6340 | /* Substitute into the default argument ARG (a default argument for |
6341 | FN), which has the indicated TYPE. */ | |
6342 | ||
6343 | tree | |
3a978d72 | 6344 | tsubst_default_argument (tree fn, tree type, tree arg) |
9188c363 | 6345 | { |
2436b51f MM |
6346 | tree saved_class_ptr = NULL_TREE; |
6347 | tree saved_class_ref = NULL_TREE; | |
6348 | ||
9188c363 MM |
6349 | /* This default argument came from a template. Instantiate the |
6350 | default argument here, not in tsubst. In the case of | |
c8094d83 MS |
6351 | something like: |
6352 | ||
9188c363 MM |
6353 | template <class T> |
6354 | struct S { | |
6355 | static T t(); | |
6356 | void f(T = t()); | |
6357 | }; | |
c8094d83 | 6358 | |
9188c363 | 6359 | we must be careful to do name lookup in the scope of S<T>, |
d5a10cf0 | 6360 | rather than in the current class. */ |
2b59fc25 | 6361 | push_access_scope (fn); |
2436b51f MM |
6362 | /* The "this" pointer is not valid in a default argument. */ |
6363 | if (cfun) | |
6364 | { | |
6365 | saved_class_ptr = current_class_ptr; | |
6366 | cp_function_chain->x_current_class_ptr = NULL_TREE; | |
6367 | saved_class_ref = current_class_ref; | |
6368 | cp_function_chain->x_current_class_ref = NULL_TREE; | |
6369 | } | |
9188c363 | 6370 | |
d5a10cf0 | 6371 | push_deferring_access_checks(dk_no_deferred); |
5a8613b2 MM |
6372 | /* The default argument expression may cause implicitly defined |
6373 | member functions to be synthesized, which will result in garbage | |
6374 | collection. We must treat this situation as if we were within | |
6375 | the body of function so as to avoid collecting live data on the | |
6376 | stack. */ | |
6377 | ++function_depth; | |
c2ea3a40 | 6378 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), |
015c2c66 MM |
6379 | tf_warning_or_error, NULL_TREE, |
6380 | /*integral_constant_expression_p=*/false); | |
5a8613b2 | 6381 | --function_depth; |
d5a10cf0 MM |
6382 | pop_deferring_access_checks(); |
6383 | ||
2436b51f MM |
6384 | /* Restore the "this" pointer. */ |
6385 | if (cfun) | |
6386 | { | |
6387 | cp_function_chain->x_current_class_ptr = saved_class_ptr; | |
6388 | cp_function_chain->x_current_class_ref = saved_class_ref; | |
6389 | } | |
6390 | ||
2b59fc25 | 6391 | pop_access_scope (fn); |
9188c363 MM |
6392 | |
6393 | /* Make sure the default argument is reasonable. */ | |
6394 | arg = check_default_argument (type, arg); | |
6395 | ||
6396 | return arg; | |
6397 | } | |
6398 | ||
6399 | /* Substitute into all the default arguments for FN. */ | |
6400 | ||
6401 | static void | |
3a978d72 | 6402 | tsubst_default_arguments (tree fn) |
9188c363 MM |
6403 | { |
6404 | tree arg; | |
6405 | tree tmpl_args; | |
6406 | ||
6407 | tmpl_args = DECL_TI_ARGS (fn); | |
6408 | ||
6409 | /* If this function is not yet instantiated, we certainly don't need | |
6410 | its default arguments. */ | |
6411 | if (uses_template_parms (tmpl_args)) | |
6412 | return; | |
6413 | ||
c8094d83 MS |
6414 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
6415 | arg; | |
9188c363 MM |
6416 | arg = TREE_CHAIN (arg)) |
6417 | if (TREE_PURPOSE (arg)) | |
c8094d83 | 6418 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, |
9188c363 MM |
6419 | TREE_VALUE (arg), |
6420 | TREE_PURPOSE (arg)); | |
6421 | } | |
6422 | ||
fc6a28d7 MM |
6423 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
6424 | result of the substitution. Issue error and warning messages under | |
6425 | control of COMPLAIN. */ | |
00d3396f | 6426 | |
e9659ab0 | 6427 | static tree |
fc6a28d7 | 6428 | tsubst_decl (tree t, tree args, tsubst_flags_t complain) |
8d08fdba | 6429 | { |
82a98427 | 6430 | location_t saved_loc; |
b370501f | 6431 | tree r = NULL_TREE; |
4b2811e9 | 6432 | tree in_decl = t; |
830bfa74 | 6433 | |
ae58fa02 | 6434 | /* Set the filename and linenumber to improve error-reporting. */ |
82a98427 | 6435 | saved_loc = input_location; |
f31686a3 | 6436 | input_location = DECL_SOURCE_LOCATION (t); |
b7484fbe | 6437 | |
8d08fdba MS |
6438 | switch (TREE_CODE (t)) |
6439 | { | |
98c1c668 JM |
6440 | case TEMPLATE_DECL: |
6441 | { | |
28e42b7e KL |
6442 | /* We can get here when processing a member function template, |
6443 | member class template, and template template parameter of | |
6444 | a template class. */ | |
98c1c668 | 6445 | tree decl = DECL_TEMPLATE_RESULT (t); |
386b8a85 | 6446 | tree spec; |
28e42b7e KL |
6447 | tree tmpl_args; |
6448 | tree full_args; | |
98c1c668 | 6449 | |
28e42b7e | 6450 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
db2767b6 | 6451 | { |
28e42b7e KL |
6452 | /* Template template parameter is treated here. */ |
6453 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6454 | if (new_type == error_mark_node) | |
6455 | return error_mark_node; | |
36a117a5 | 6456 | |
28e42b7e KL |
6457 | r = copy_decl (t); |
6458 | TREE_CHAIN (r) = NULL_TREE; | |
6459 | TREE_TYPE (r) = new_type; | |
6460 | DECL_TEMPLATE_RESULT (r) | |
6461 | = build_decl (TYPE_DECL, DECL_NAME (decl), new_type); | |
c8094d83 | 6462 | DECL_TEMPLATE_PARMS (r) |
28e42b7e KL |
6463 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
6464 | complain); | |
6465 | TYPE_NAME (new_type) = r; | |
6466 | break; | |
6467 | } | |
36a117a5 | 6468 | |
28e42b7e KL |
6469 | /* We might already have an instance of this template. |
6470 | The ARGS are for the surrounding class type, so the | |
6471 | full args contain the tsubst'd args for the context, | |
6472 | plus the innermost args from the template decl. */ | |
c8094d83 | 6473 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
28e42b7e KL |
6474 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
6475 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); | |
8ca4bf25 MM |
6476 | /* Because this is a template, the arguments will still be |
6477 | dependent, even after substitution. If | |
6478 | PROCESSING_TEMPLATE_DECL is not set, the dependency | |
6479 | predicates will short-circuit. */ | |
6480 | ++processing_template_decl; | |
28e42b7e KL |
6481 | full_args = tsubst_template_args (tmpl_args, args, |
6482 | complain, in_decl); | |
8ca4bf25 | 6483 | --processing_template_decl; |
bf2f7328 VR |
6484 | if (full_args == error_mark_node) |
6485 | return error_mark_node; | |
28e42b7e KL |
6486 | |
6487 | /* tsubst_template_args doesn't copy the vector if | |
6488 | nothing changed. But, *something* should have | |
6489 | changed. */ | |
6490 | gcc_assert (full_args != tmpl_args); | |
6491 | ||
6492 | spec = retrieve_specialization (t, full_args, | |
6493 | /*class_specializations_p=*/true); | |
6494 | if (spec != NULL_TREE) | |
6495 | { | |
6496 | r = spec; | |
6497 | break; | |
db2767b6 | 6498 | } |
98c1c668 JM |
6499 | |
6500 | /* Make a new template decl. It will be similar to the | |
c8094d83 | 6501 | original, but will record the current template arguments. |
98c1c668 JM |
6502 | We also create a new function declaration, which is just |
6503 | like the old one, but points to this new template, rather | |
6504 | than the old one. */ | |
0acf7199 | 6505 | r = copy_decl (t); |
50bc768d | 6506 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
ae58fa02 | 6507 | TREE_CHAIN (r) = NULL_TREE; |
db2767b6 | 6508 | |
ae58fa02 | 6509 | DECL_TEMPLATE_INFO (r) = build_tree_list (t, args); |
93cdc044 JM |
6510 | |
6511 | if (TREE_CODE (decl) == TYPE_DECL) | |
6512 | { | |
8ca4bf25 MM |
6513 | tree new_type; |
6514 | ++processing_template_decl; | |
6515 | new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
3db45ab5 | 6516 | --processing_template_decl; |
2620d095 KL |
6517 | if (new_type == error_mark_node) |
6518 | return error_mark_node; | |
6519 | ||
ae58fa02 MM |
6520 | TREE_TYPE (r) = new_type; |
6521 | CLASSTYPE_TI_TEMPLATE (new_type) = r; | |
17aec3eb | 6522 | DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type); |
ae58fa02 | 6523 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type); |
8ca4bf25 | 6524 | DECL_CONTEXT (r) = TYPE_CONTEXT (new_type); |
93cdc044 JM |
6525 | } |
6526 | else | |
6527 | { | |
8ca4bf25 MM |
6528 | tree new_decl; |
6529 | ++processing_template_decl; | |
6530 | new_decl = tsubst (decl, args, complain, in_decl); | |
6531 | --processing_template_decl; | |
caec1dc0 KL |
6532 | if (new_decl == error_mark_node) |
6533 | return error_mark_node; | |
17aec3eb RK |
6534 | |
6535 | DECL_TEMPLATE_RESULT (r) = new_decl; | |
ae58fa02 MM |
6536 | DECL_TI_TEMPLATE (new_decl) = r; |
6537 | TREE_TYPE (r) = TREE_TYPE (new_decl); | |
6538 | DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl); | |
3db45ab5 | 6539 | DECL_CONTEXT (r) = DECL_CONTEXT (new_decl); |
93cdc044 JM |
6540 | } |
6541 | ||
ae58fa02 MM |
6542 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
6543 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; | |
6544 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; | |
98c1c668 JM |
6545 | |
6546 | /* The template parameters for this new template are all the | |
6547 | template parameters for the old template, except the | |
c6002625 | 6548 | outermost level of parameters. */ |
c8094d83 | 6549 | DECL_TEMPLATE_PARMS (r) |
4393e105 | 6550 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
c2ea3a40 | 6551 | complain); |
98c1c668 | 6552 | |
93cdc044 | 6553 | if (PRIMARY_TEMPLATE_P (t)) |
ae58fa02 | 6554 | DECL_PRIMARY_TEMPLATE (r) = r; |
93cdc044 | 6555 | |
8c6ab2db NS |
6556 | if (TREE_CODE (decl) != TYPE_DECL) |
6557 | /* Record this non-type partial instantiation. */ | |
c8094d83 | 6558 | register_specialization (r, t, |
d63d5d0c ILT |
6559 | DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)), |
6560 | false); | |
98c1c668 | 6561 | } |
ae58fa02 | 6562 | break; |
8d08fdba MS |
6563 | |
6564 | case FUNCTION_DECL: | |
6565 | { | |
386b8a85 | 6566 | tree ctx; |
87603ed0 | 6567 | tree argvec = NULL_TREE; |
cf38f48a | 6568 | tree *friends; |
36a117a5 | 6569 | tree gen_tmpl; |
fc6a28d7 | 6570 | tree type; |
5566b478 | 6571 | int member; |
d8c4447d MM |
6572 | int args_depth; |
6573 | int parms_depth; | |
5566b478 | 6574 | |
36a117a5 | 6575 | /* Nobody should be tsubst'ing into non-template functions. */ |
50bc768d | 6576 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE); |
36a117a5 MM |
6577 | |
6578 | if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) | |
6579 | { | |
6580 | tree spec; | |
00cf3e31 MM |
6581 | bool dependent_p; |
6582 | ||
6583 | /* If T is not dependent, just return it. We have to | |
6584 | increment PROCESSING_TEMPLATE_DECL because | |
6585 | value_dependent_expression_p assumes that nothing is | |
6586 | dependent when PROCESSING_TEMPLATE_DECL is zero. */ | |
6587 | ++processing_template_decl; | |
6588 | dependent_p = value_dependent_expression_p (t); | |
6589 | --processing_template_decl; | |
6590 | if (!dependent_p) | |
6591 | return t; | |
36a117a5 MM |
6592 | |
6593 | /* Calculate the most general template of which R is a | |
6594 | specialization, and the complete set of arguments used to | |
6595 | specialize R. */ | |
6596 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); | |
c8094d83 | 6597 | argvec = tsubst_template_args (DECL_TI_ARGS |
a91db711 | 6598 | (DECL_TEMPLATE_RESULT (gen_tmpl)), |
c8094d83 | 6599 | args, complain, in_decl); |
36a117a5 MM |
6600 | |
6601 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
6602 | spec = retrieve_specialization (gen_tmpl, argvec, |
6603 | /*class_specializations_p=*/false); | |
7ddedda4 | 6604 | |
36a117a5 | 6605 | if (spec) |
ae58fa02 MM |
6606 | { |
6607 | r = spec; | |
6608 | break; | |
6609 | } | |
d8c4447d | 6610 | |
f9a7ae04 MM |
6611 | /* We can see more levels of arguments than parameters if |
6612 | there was a specialization of a member template, like | |
6613 | this: | |
6614 | ||
0cbd7506 | 6615 | template <class T> struct S { template <class U> void f(); } |
c8094d83 | 6616 | template <> template <class U> void S<int>::f(U); |
f9a7ae04 | 6617 | |
dc957d14 | 6618 | Here, we'll be substituting into the specialization, |
f9a7ae04 MM |
6619 | because that's where we can find the code we actually |
6620 | want to generate, but we'll have enough arguments for | |
c8094d83 | 6621 | the most general template. |
f9a7ae04 MM |
6622 | |
6623 | We also deal with the peculiar case: | |
d8c4447d | 6624 | |
c8094d83 | 6625 | template <class T> struct S { |
d8c4447d MM |
6626 | template <class U> friend void f(); |
6627 | }; | |
74b846e0 | 6628 | template <class U> void f() {} |
d8c4447d MM |
6629 | template S<int>; |
6630 | template void f<double>(); | |
6631 | ||
6632 | Here, the ARGS for the instantiation of will be {int, | |
6633 | double}. But, we only need as many ARGS as there are | |
6634 | levels of template parameters in CODE_PATTERN. We are | |
6635 | careful not to get fooled into reducing the ARGS in | |
6636 | situations like: | |
6637 | ||
6638 | template <class T> struct S { template <class U> void f(U); } | |
6639 | template <class T> template <> void S<T>::f(int) {} | |
6640 | ||
6641 | which we can spot because the pattern will be a | |
6642 | specialization in this case. */ | |
6643 | args_depth = TMPL_ARGS_DEPTH (args); | |
c8094d83 MS |
6644 | parms_depth = |
6645 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); | |
d8c4447d MM |
6646 | if (args_depth > parms_depth |
6647 | && !DECL_TEMPLATE_SPECIALIZATION (t)) | |
f9a7ae04 | 6648 | args = get_innermost_template_args (args, parms_depth); |
36a117a5 MM |
6649 | } |
6650 | else | |
6651 | { | |
6652 | /* This special case arises when we have something like this: | |
6653 | ||
0cbd7506 | 6654 | template <class T> struct S { |
c8094d83 | 6655 | friend void f<int>(int, double); |
36a117a5 MM |
6656 | }; |
6657 | ||
6658 | Here, the DECL_TI_TEMPLATE for the friend declaration | |
10b1d5e7 MM |
6659 | will be an IDENTIFIER_NODE. We are being called from |
6660 | tsubst_friend_function, and we want only to create a | |
6661 | new decl (R) with appropriate types so that we can call | |
6662 | determine_specialization. */ | |
36a117a5 MM |
6663 | gen_tmpl = NULL_TREE; |
6664 | } | |
6665 | ||
6eb3bb27 | 6666 | if (DECL_CLASS_SCOPE_P (t)) |
8d08fdba | 6667 | { |
5566b478 MS |
6668 | if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t))) |
6669 | member = 2; | |
6670 | else | |
6671 | member = 1; | |
c8094d83 | 6672 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, |
8c6ab2db | 6673 | complain, t, /*entering_scope=*/1); |
5566b478 MS |
6674 | } |
6675 | else | |
6676 | { | |
6677 | member = 0; | |
4f1c5b7d | 6678 | ctx = DECL_CONTEXT (t); |
5566b478 | 6679 | } |
fc6a28d7 | 6680 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
b3da7bb1 MM |
6681 | if (type == error_mark_node) |
6682 | return error_mark_node; | |
8d08fdba | 6683 | |
5566b478 | 6684 | /* We do NOT check for matching decls pushed separately at this |
0cbd7506 MS |
6685 | point, as they may not represent instantiations of this |
6686 | template, and in any case are considered separate under the | |
6687 | discrete model. */ | |
0acf7199 | 6688 | r = copy_decl (t); |
e1467ff2 | 6689 | DECL_USE_TEMPLATE (r) = 0; |
5566b478 | 6690 | TREE_TYPE (r) = type; |
92643fea MM |
6691 | /* Clear out the mangled name and RTL for the instantiation. */ |
6692 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
6693 | SET_DECL_RTL (r, NULL_RTX); | |
5bd61841 | 6694 | DECL_INITIAL (r) = NULL_TREE; |
4f1c5b7d | 6695 | DECL_CONTEXT (r) = ctx; |
5566b478 | 6696 | |
c8094d83 | 6697 | if (member && DECL_CONV_FN_P (r)) |
1f84ec23 MM |
6698 | /* Type-conversion operator. Reconstruct the name, in |
6699 | case it's the name of one of the template's parameters. */ | |
6700 | DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type)); | |
5566b478 | 6701 | |
4393e105 | 6702 | DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, |
c2ea3a40 | 6703 | complain, t); |
477f6664 | 6704 | DECL_RESULT (r) = NULL_TREE; |
711734a9 JM |
6705 | |
6706 | TREE_STATIC (r) = 0; | |
6707 | TREE_PUBLIC (r) = TREE_PUBLIC (t); | |
6708 | DECL_EXTERNAL (r) = 1; | |
4684cd27 MM |
6709 | /* If this is an instantiation of a function with internal |
6710 | linkage, we already know what object file linkage will be | |
6711 | assigned to the instantiation. */ | |
6712 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); | |
711734a9 JM |
6713 | DECL_DEFER_OUTPUT (r) = 0; |
6714 | TREE_CHAIN (r) = NULL_TREE; | |
6715 | DECL_PENDING_INLINE_INFO (r) = 0; | |
59026e79 | 6716 | DECL_PENDING_INLINE_P (r) = 0; |
655dc6ee | 6717 | DECL_SAVED_TREE (r) = NULL_TREE; |
711734a9 | 6718 | TREE_USED (r) = 0; |
db9b2174 MM |
6719 | if (DECL_CLONED_FUNCTION (r)) |
6720 | { | |
6721 | DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t), | |
c2ea3a40 | 6722 | args, complain, t); |
db9b2174 MM |
6723 | TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r)); |
6724 | TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r; | |
6725 | } | |
711734a9 | 6726 | |
92643fea MM |
6727 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
6728 | this in the special friend case mentioned above where | |
6729 | GEN_TMPL is NULL. */ | |
36a117a5 | 6730 | if (gen_tmpl) |
386b8a85 | 6731 | { |
c8094d83 | 6732 | DECL_TEMPLATE_INFO (r) |
e1b3e07d | 6733 | = tree_cons (gen_tmpl, argvec, NULL_TREE); |
36a117a5 | 6734 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
d63d5d0c | 6735 | register_specialization (r, gen_tmpl, argvec, false); |
36a117a5 | 6736 | |
9188c363 MM |
6737 | /* We're not supposed to instantiate default arguments |
6738 | until they are called, for a template. But, for a | |
6739 | declaration like: | |
6740 | ||
0cbd7506 MS |
6741 | template <class T> void f () |
6742 | { extern void g(int i = T()); } | |
c8094d83 | 6743 | |
9188c363 MM |
6744 | we should do the substitution when the template is |
6745 | instantiated. We handle the member function case in | |
6746 | instantiate_class_template since the default arguments | |
6747 | might refer to other members of the class. */ | |
6748 | if (!member | |
6749 | && !PRIMARY_TEMPLATE_P (gen_tmpl) | |
6750 | && !uses_template_parms (argvec)) | |
6751 | tsubst_default_arguments (r); | |
386b8a85 | 6752 | } |
c43e95f8 MM |
6753 | else |
6754 | DECL_TEMPLATE_INFO (r) = NULL_TREE; | |
f181d4ae | 6755 | |
cf38f48a MM |
6756 | /* Copy the list of befriending classes. */ |
6757 | for (friends = &DECL_BEFRIENDING_CLASSES (r); | |
6758 | *friends; | |
c8094d83 | 6759 | friends = &TREE_CHAIN (*friends)) |
cf38f48a MM |
6760 | { |
6761 | *friends = copy_node (*friends); | |
6762 | TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends), | |
c2ea3a40 | 6763 | args, complain, |
cf38f48a MM |
6764 | in_decl); |
6765 | } | |
6766 | ||
212e7048 | 6767 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
f181d4ae MM |
6768 | { |
6769 | maybe_retrofit_in_chrg (r); | |
212e7048 MM |
6770 | if (DECL_CONSTRUCTOR_P (r)) |
6771 | grok_ctor_properties (ctx, r); | |
2be678ff JM |
6772 | /* If this is an instantiation of a member template, clone it. |
6773 | If it isn't, that'll be handled by | |
6774 | clone_constructors_and_destructors. */ | |
5e818b93 | 6775 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
271e6f02 | 6776 | clone_function_decl (r, /*update_method_vec_p=*/0); |
f181d4ae | 6777 | } |
398cd199 VR |
6778 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)) |
6779 | && !grok_op_properties (r, (complain & tf_error) != 0)) | |
6780 | return error_mark_node; | |
c8b2e872 MM |
6781 | |
6782 | if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t)) | |
6783 | SET_DECL_FRIEND_CONTEXT (r, | |
6784 | tsubst (DECL_FRIEND_CONTEXT (t), | |
6785 | args, complain, in_decl)); | |
b9e75696 JM |
6786 | |
6787 | /* Possibly limit visibility based on template args. */ | |
6788 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
6789 | if (DECL_VISIBILITY_SPECIFIED (t)) |
6790 | { | |
6791 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
6792 | DECL_ATTRIBUTES (r) | |
6793 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
6794 | } | |
b9e75696 | 6795 | determine_visibility (r); |
8d08fdba | 6796 | } |
ae58fa02 | 6797 | break; |
8d08fdba MS |
6798 | |
6799 | case PARM_DECL: | |
6800 | { | |
fc6a28d7 MM |
6801 | tree type; |
6802 | ||
ae58fa02 | 6803 | r = copy_node (t); |
833aa4c4 NS |
6804 | if (DECL_TEMPLATE_PARM_P (t)) |
6805 | SET_DECL_TEMPLATE_PARM_P (r); | |
8e51619a | 6806 | |
fc6a28d7 | 6807 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
02bab9db | 6808 | type = type_decays_to (type); |
5566b478 | 6809 | TREE_TYPE (r) = type; |
9804209d | 6810 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 6811 | |
560ad596 MM |
6812 | if (DECL_INITIAL (r)) |
6813 | { | |
6814 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) | |
6815 | DECL_INITIAL (r) = TREE_TYPE (r); | |
6816 | else | |
6817 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, | |
6818 | complain, in_decl); | |
6819 | } | |
db2767b6 | 6820 | |
5566b478 | 6821 | DECL_CONTEXT (r) = NULL_TREE; |
8e51619a JM |
6822 | |
6823 | if (!DECL_TEMPLATE_PARM_P (r)) | |
6824 | DECL_ARG_TYPE (r) = type_passed_as (type); | |
8d08fdba | 6825 | if (TREE_CHAIN (t)) |
4393e105 | 6826 | TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, |
c2ea3a40 | 6827 | complain, TREE_CHAIN (t)); |
8d08fdba | 6828 | } |
ae58fa02 | 6829 | break; |
8d08fdba | 6830 | |
5566b478 MS |
6831 | case FIELD_DECL: |
6832 | { | |
fc6a28d7 MM |
6833 | tree type; |
6834 | ||
0acf7199 | 6835 | r = copy_decl (t); |
fc6a28d7 MM |
6836 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
6837 | if (type == error_mark_node) | |
6838 | return error_mark_node; | |
1b8899d1 | 6839 | TREE_TYPE (r) = type; |
9804209d | 6840 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
1b8899d1 | 6841 | |
015c2c66 MM |
6842 | /* DECL_INITIAL gives the number of bits in a bit-field. */ |
6843 | DECL_INITIAL (r) | |
6844 | = tsubst_expr (DECL_INITIAL (t), args, | |
6845 | complain, in_decl, | |
6846 | /*integral_constant_expression_p=*/true); | |
1b8899d1 MM |
6847 | /* We don't have to set DECL_CONTEXT here; it is set by |
6848 | finish_member_declaration. */ | |
5566b478 | 6849 | TREE_CHAIN (r) = NULL_TREE; |
c8094d83 | 6850 | if (VOID_TYPE_P (type)) |
dee15844 | 6851 | error ("instantiation of %q+D as type %qT", r, type); |
5566b478 | 6852 | } |
ae58fa02 | 6853 | break; |
5566b478 MS |
6854 | |
6855 | case USING_DECL: | |
98ed9dae NS |
6856 | /* We reach here only for member using decls. */ |
6857 | if (DECL_DEPENDENT_P (t)) | |
6858 | { | |
6859 | r = do_class_using_decl | |
6860 | (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl), | |
6861 | tsubst_copy (DECL_NAME (t), args, complain, in_decl)); | |
6862 | if (!r) | |
6863 | r = error_mark_node; | |
6864 | } | |
6865 | else | |
6866 | { | |
6867 | r = copy_node (t); | |
6868 | TREE_CHAIN (r) = NULL_TREE; | |
6869 | } | |
ae58fa02 | 6870 | break; |
5566b478 | 6871 | |
9188c363 | 6872 | case TYPE_DECL: |
5566b478 MS |
6873 | case VAR_DECL: |
6874 | { | |
1cea0434 KG |
6875 | tree argvec = NULL_TREE; |
6876 | tree gen_tmpl = NULL_TREE; | |
36a117a5 | 6877 | tree spec; |
1cea0434 | 6878 | tree tmpl = NULL_TREE; |
9188c363 | 6879 | tree ctx; |
fc6a28d7 | 6880 | tree type = NULL_TREE; |
f604fc3b | 6881 | bool local_p; |
9188c363 | 6882 | |
fc6a28d7 MM |
6883 | if (TREE_CODE (t) == TYPE_DECL) |
6884 | { | |
6885 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
6886 | if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM | |
6887 | || t == TYPE_MAIN_DECL (TREE_TYPE (t))) | |
6888 | { | |
6889 | /* If this is the canonical decl, we don't have to | |
6890 | mess with instantiations, and often we can't (for | |
6891 | typename, template type parms and such). Note that | |
6892 | TYPE_NAME is not correct for the above test if | |
6893 | we've copied the type for a typedef. */ | |
6894 | r = TYPE_NAME (type); | |
6895 | break; | |
6896 | } | |
6897 | } | |
c8094d83 | 6898 | |
f604fc3b MM |
6899 | /* Check to see if we already have the specialization we |
6900 | need. */ | |
6901 | spec = NULL_TREE; | |
6902 | if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)) | |
6903 | { | |
6904 | /* T is a static data member or namespace-scope entity. | |
6905 | We have to substitute into namespace-scope variables | |
6906 | (even though such entities are never templates) because | |
6907 | of cases like: | |
6908 | ||
6909 | template <class T> void f() { extern T t; } | |
6910 | ||
6911 | where the entity referenced is not known until | |
6912 | instantiation time. */ | |
6913 | local_p = false; | |
6914 | ctx = DECL_CONTEXT (t); | |
6915 | if (DECL_CLASS_SCOPE_P (t)) | |
6916 | { | |
6917 | ctx = tsubst_aggr_type (ctx, args, | |
6918 | complain, | |
6919 | in_decl, /*entering_scope=*/1); | |
6920 | /* If CTX is unchanged, then T is in fact the | |
6921 | specialization we want. That situation occurs when | |
6922 | referencing a static data member within in its own | |
6923 | class. We can use pointer equality, rather than | |
6924 | same_type_p, because DECL_CONTEXT is always | |
6925 | canonical. */ | |
6926 | if (ctx == DECL_CONTEXT (t)) | |
6927 | spec = t; | |
6928 | } | |
5566b478 | 6929 | |
f604fc3b MM |
6930 | if (!spec) |
6931 | { | |
6932 | tmpl = DECL_TI_TEMPLATE (t); | |
6933 | gen_tmpl = most_general_template (tmpl); | |
6934 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); | |
6935 | spec = (retrieve_specialization | |
6936 | (gen_tmpl, argvec, | |
6937 | /*class_specializations_p=*/false)); | |
6938 | } | |
6939 | } | |
9188c363 | 6940 | else |
6dfbb909 | 6941 | { |
f604fc3b MM |
6942 | /* A local variable. */ |
6943 | local_p = true; | |
6dfbb909 MM |
6944 | /* Subsequent calls to pushdecl will fill this in. */ |
6945 | ctx = NULL_TREE; | |
f604fc3b | 6946 | spec = retrieve_local_specialization (t); |
6dfbb909 | 6947 | } |
f604fc3b MM |
6948 | /* If we already have the specialization we need, there is |
6949 | nothing more to do. */ | |
36a117a5 | 6950 | if (spec) |
ae58fa02 MM |
6951 | { |
6952 | r = spec; | |
6953 | break; | |
6954 | } | |
5566b478 | 6955 | |
f604fc3b | 6956 | /* Create a new node for the specialization we need. */ |
0acf7199 | 6957 | r = copy_decl (t); |
edebf865 | 6958 | if (TREE_CODE (r) == VAR_DECL) |
39703eb9 | 6959 | { |
8b0a8500 MM |
6960 | /* Even if the original location is out of scope, the |
6961 | newly substituted one is not. */ | |
6962 | DECL_DEAD_FOR_LOCAL (r) = 0; | |
6963 | DECL_INITIALIZED_P (r) = 0; | |
6964 | DECL_TEMPLATE_INSTANTIATED (r) = 0; | |
fc6a28d7 MM |
6965 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
6966 | if (type == error_mark_node) | |
6967 | return error_mark_node; | |
2c05d05e MM |
6968 | if (TREE_CODE (type) == FUNCTION_TYPE) |
6969 | { | |
6970 | /* It may seem that this case cannot occur, since: | |
6971 | ||
6972 | typedef void f(); | |
6973 | void g() { f x; } | |
6974 | ||
6975 | declares a function, not a variable. However: | |
6976 | ||
6977 | typedef void f(); | |
6978 | template <typename T> void g() { T t; } | |
6979 | template void g<f>(); | |
6980 | ||
6981 | is an attempt to declare a variable with function | |
6982 | type. */ | |
6983 | error ("variable %qD has function type", | |
6984 | /* R is not yet sufficiently initialized, so we | |
6985 | just use its name. */ | |
6986 | DECL_NAME (r)); | |
6987 | return error_mark_node; | |
6988 | } | |
39703eb9 MM |
6989 | type = complete_type (type); |
6990 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) | |
6991 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t); | |
62e19030 | 6992 | type = check_var_type (DECL_NAME (r), type); |
56b4ea3d RH |
6993 | |
6994 | if (DECL_HAS_VALUE_EXPR_P (t)) | |
6995 | { | |
6996 | tree ve = DECL_VALUE_EXPR (t); | |
015c2c66 MM |
6997 | ve = tsubst_expr (ve, args, complain, in_decl, |
6998 | /*constant_expression_p=*/false); | |
3db45ab5 | 6999 | SET_DECL_VALUE_EXPR (r, ve); |
56b4ea3d | 7000 | } |
39703eb9 | 7001 | } |
a3d87771 MM |
7002 | else if (DECL_SELF_REFERENCE_P (t)) |
7003 | SET_DECL_SELF_REFERENCE_P (r); | |
01f4137f | 7004 | TREE_TYPE (r) = type; |
9804209d | 7005 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
5566b478 | 7006 | DECL_CONTEXT (r) = ctx; |
92643fea MM |
7007 | /* Clear out the mangled name and RTL for the instantiation. */ |
7008 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); | |
820cc88f DB |
7009 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
7010 | SET_DECL_RTL (r, NULL_RTX); | |
8b0a8500 MM |
7011 | /* The initializer must not be expanded until it is required; |
7012 | see [temp.inst]. */ | |
d11ad92e | 7013 | DECL_INITIAL (r) = NULL_TREE; |
820cc88f DB |
7014 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
7015 | SET_DECL_RTL (r, NULL_RTX); | |
06ceef4e | 7016 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
b9e75696 JM |
7017 | if (TREE_CODE (r) == VAR_DECL) |
7018 | { | |
7019 | /* Possibly limit visibility based on template args. */ | |
7020 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; | |
b70f0f48 JM |
7021 | if (DECL_VISIBILITY_SPECIFIED (t)) |
7022 | { | |
7023 | DECL_VISIBILITY_SPECIFIED (r) = 0; | |
7024 | DECL_ATTRIBUTES (r) | |
7025 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); | |
7026 | } | |
b9e75696 JM |
7027 | determine_visibility (r); |
7028 | } | |
5566b478 | 7029 | |
6dfbb909 MM |
7030 | if (!local_p) |
7031 | { | |
7032 | /* A static data member declaration is always marked | |
7033 | external when it is declared in-class, even if an | |
7034 | initializer is present. We mimic the non-template | |
7035 | processing here. */ | |
7036 | DECL_EXTERNAL (r) = 1; | |
fa8d6e85 | 7037 | |
d63d5d0c | 7038 | register_specialization (r, gen_tmpl, argvec, false); |
6dfbb909 MM |
7039 | DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE); |
7040 | SET_DECL_IMPLICIT_INSTANTIATION (r); | |
7041 | } | |
9188c363 | 7042 | else |
6dfbb909 | 7043 | register_local_specialization (r, t); |
5566b478 | 7044 | |
5566b478 | 7045 | TREE_CHAIN (r) = NULL_TREE; |
edebf865 | 7046 | layout_decl (r, 0); |
5566b478 | 7047 | } |
ae58fa02 | 7048 | break; |
5566b478 | 7049 | |
ae58fa02 | 7050 | default: |
315fb5db | 7051 | gcc_unreachable (); |
c8094d83 | 7052 | } |
ae58fa02 MM |
7053 | |
7054 | /* Restore the file and line information. */ | |
82a98427 | 7055 | input_location = saved_loc; |
ae58fa02 MM |
7056 | |
7057 | return r; | |
7058 | } | |
7059 | ||
34cd5ae7 | 7060 | /* Substitute into the ARG_TYPES of a function type. */ |
cabc336a | 7061 | |
e9659ab0 | 7062 | static tree |
c8094d83 | 7063 | tsubst_arg_types (tree arg_types, |
0cbd7506 MS |
7064 | tree args, |
7065 | tsubst_flags_t complain, | |
7066 | tree in_decl) | |
cabc336a MM |
7067 | { |
7068 | tree remaining_arg_types; | |
cabc336a | 7069 | tree type; |
5e97d404 NS |
7070 | tree default_arg; |
7071 | tree result = NULL_TREE; | |
cabc336a MM |
7072 | |
7073 | if (!arg_types || arg_types == void_list_node) | |
7074 | return arg_types; | |
c8094d83 | 7075 | |
cabc336a | 7076 | remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
4393e105 MM |
7077 | args, complain, in_decl); |
7078 | if (remaining_arg_types == error_mark_node) | |
7079 | return error_mark_node; | |
7080 | ||
7081 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); | |
7082 | if (type == error_mark_node) | |
7083 | return error_mark_node; | |
4b2811e9 NS |
7084 | if (VOID_TYPE_P (type)) |
7085 | { | |
c2ea3a40 | 7086 | if (complain & tf_error) |
0cbd7506 MS |
7087 | { |
7088 | error ("invalid parameter type %qT", type); | |
7089 | if (in_decl) | |
dee15844 | 7090 | error ("in declaration %q+D", in_decl); |
0cbd7506 | 7091 | } |
4b2811e9 NS |
7092 | return error_mark_node; |
7093 | } | |
cabc336a | 7094 | |
4393e105 MM |
7095 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
7096 | top-level qualifiers as required. */ | |
7097 | type = TYPE_MAIN_VARIANT (type_decays_to (type)); | |
cabc336a | 7098 | |
5e97d404 NS |
7099 | /* We do not substitute into default arguments here. The standard |
7100 | mandates that they be instantiated only when needed, which is | |
7101 | done in build_over_call. */ | |
7102 | default_arg = TREE_PURPOSE (arg_types); | |
c8094d83 | 7103 | |
5e97d404 NS |
7104 | if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG) |
7105 | { | |
7106 | /* We've instantiated a template before its default arguments | |
0cbd7506 MS |
7107 | have been parsed. This can happen for a nested template |
7108 | class, and is not an error unless we require the default | |
7109 | argument in a call of this function. */ | |
5e97d404 | 7110 | result = tree_cons (default_arg, type, remaining_arg_types); |
01ea1ea8 | 7111 | VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result); |
5e97d404 NS |
7112 | } |
7113 | else | |
7114 | result = hash_tree_cons (default_arg, type, remaining_arg_types); | |
c8094d83 | 7115 | |
5e97d404 | 7116 | return result; |
cabc336a MM |
7117 | } |
7118 | ||
4393e105 MM |
7119 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
7120 | *not* handle the exception-specification for FNTYPE, because the | |
7121 | initial substitution of explicitly provided template parameters | |
7122 | during argument deduction forbids substitution into the | |
7123 | exception-specification: | |
7124 | ||
7125 | [temp.deduct] | |
7126 | ||
7127 | All references in the function type of the function template to the | |
7128 | corresponding template parameters are replaced by the specified tem- | |
7129 | plate argument values. If a substitution in a template parameter or | |
7130 | in the function type of the function template results in an invalid | |
7131 | type, type deduction fails. [Note: The equivalent substitution in | |
7132 | exception specifications is done only when the function is instanti- | |
7133 | ated, at which point a program is ill-formed if the substitution | |
7134 | results in an invalid type.] */ | |
7135 | ||
7136 | static tree | |
c8094d83 | 7137 | tsubst_function_type (tree t, |
0cbd7506 MS |
7138 | tree args, |
7139 | tsubst_flags_t complain, | |
7140 | tree in_decl) | |
4393e105 MM |
7141 | { |
7142 | tree return_type; | |
7143 | tree arg_types; | |
7144 | tree fntype; | |
7145 | ||
8dd3f57a | 7146 | /* The TYPE_CONTEXT is not used for function/method types. */ |
50bc768d | 7147 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
4393e105 | 7148 | |
46c895ac | 7149 | /* Substitute the return type. */ |
4393e105 MM |
7150 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
7151 | if (return_type == error_mark_node) | |
7152 | return error_mark_node; | |
6e2993bf MM |
7153 | /* The standard does not presently indicate that creation of a |
7154 | function type with an invalid return type is a deduction failure. | |
270af55d | 7155 | However, that is clearly analogous to creating an array of "void" |
c8094d83 | 7156 | or a reference to a reference. This is core issue #486. */ |
6e2993bf MM |
7157 | if (TREE_CODE (return_type) == ARRAY_TYPE |
7158 | || TREE_CODE (return_type) == FUNCTION_TYPE) | |
7159 | { | |
7160 | if (complain & tf_error) | |
7161 | { | |
7162 | if (TREE_CODE (return_type) == ARRAY_TYPE) | |
7163 | error ("function returning an array"); | |
7164 | else | |
7165 | error ("function returning a function"); | |
7166 | } | |
7167 | return error_mark_node; | |
7168 | } | |
4393e105 | 7169 | |
34cd5ae7 | 7170 | /* Substitute the argument types. */ |
4393e105 | 7171 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, |
c8094d83 | 7172 | complain, in_decl); |
4393e105 MM |
7173 | if (arg_types == error_mark_node) |
7174 | return error_mark_node; | |
c8094d83 | 7175 | |
1891dec4 DM |
7176 | if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED |
7177 | && in_decl != NULL_TREE | |
7178 | && !TREE_NO_WARNING (in_decl) | |
7179 | && (SCALAR_TYPE_P (return_type) || VOID_TYPE_P (return_type))) | |
7180 | warning (OPT_Wreturn_type, | |
7181 | "type qualifiers ignored on function return type"); | |
7182 | ||
4393e105 MM |
7183 | /* Construct a new type node and return it. */ |
7184 | if (TREE_CODE (t) == FUNCTION_TYPE) | |
7185 | fntype = build_function_type (return_type, arg_types); | |
7186 | else | |
7187 | { | |
7188 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); | |
7189 | if (! IS_AGGR_TYPE (r)) | |
7190 | { | |
7191 | /* [temp.deduct] | |
c8094d83 | 7192 | |
4393e105 MM |
7193 | Type deduction may fail for any of the following |
7194 | reasons: | |
c8094d83 | 7195 | |
4393e105 MM |
7196 | -- Attempting to create "pointer to member of T" when T |
7197 | is not a class type. */ | |
c2ea3a40 | 7198 | if (complain & tf_error) |
0f51ccfc | 7199 | error ("creating pointer to member function of non-class type %qT", |
4393e105 MM |
7200 | r); |
7201 | return error_mark_node; | |
7202 | } | |
c8094d83 MS |
7203 | |
7204 | fntype = build_method_type_directly (r, return_type, | |
43dc123f | 7205 | TREE_CHAIN (arg_types)); |
4393e105 | 7206 | } |
c2ea3a40 | 7207 | fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain); |
e9525111 | 7208 | fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t)); |
c8094d83 MS |
7209 | |
7210 | return fntype; | |
4393e105 MM |
7211 | } |
7212 | ||
c7222c02 MM |
7213 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
7214 | ARGS into that specification, and return the substituted | |
7215 | specification. If there is no specification, return NULL_TREE. */ | |
7216 | ||
7217 | static tree | |
c8094d83 MS |
7218 | tsubst_exception_specification (tree fntype, |
7219 | tree args, | |
c7222c02 MM |
7220 | tsubst_flags_t complain, |
7221 | tree in_decl) | |
7222 | { | |
7223 | tree specs; | |
7224 | tree new_specs; | |
7225 | ||
7226 | specs = TYPE_RAISES_EXCEPTIONS (fntype); | |
7227 | new_specs = NULL_TREE; | |
7228 | if (specs) | |
7229 | { | |
7230 | if (! TREE_VALUE (specs)) | |
7231 | new_specs = specs; | |
7232 | else | |
7233 | while (specs) | |
7234 | { | |
7235 | tree spec; | |
7236 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); | |
7237 | if (spec == error_mark_node) | |
7238 | return spec; | |
7239 | new_specs = add_exception_specifier (new_specs, spec, complain); | |
7240 | specs = TREE_CHAIN (specs); | |
7241 | } | |
7242 | } | |
7243 | return new_specs; | |
7244 | } | |
7245 | ||
4393e105 MM |
7246 | /* Take the tree structure T and replace template parameters used |
7247 | therein with the argument vector ARGS. IN_DECL is an associated | |
7248 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. | |
c2ea3a40 NS |
7249 | Issue error and warning messages under control of COMPLAIN. Note |
7250 | that we must be relatively non-tolerant of extensions here, in | |
7251 | order to preserve conformance; if we allow substitutions that | |
7252 | should not be allowed, we may allow argument deductions that should | |
7253 | not succeed, and therefore report ambiguous overload situations | |
7254 | where there are none. In theory, we could allow the substitution, | |
7255 | but indicate that it should have failed, and allow our caller to | |
7256 | make sure that the right thing happens, but we don't try to do this | |
7257 | yet. | |
4393e105 MM |
7258 | |
7259 | This function is used for dealing with types, decls and the like; | |
7260 | for expressions, use tsubst_expr or tsubst_copy. */ | |
ae58fa02 | 7261 | |
14d22dd6 | 7262 | static tree |
3a978d72 | 7263 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
ae58fa02 | 7264 | { |
0ecfe0b4 | 7265 | tree type, r; |
ae58fa02 MM |
7266 | |
7267 | if (t == NULL_TREE || t == error_mark_node | |
7268 | || t == integer_type_node | |
7269 | || t == void_type_node | |
7270 | || t == char_type_node | |
539599c1 | 7271 | || t == unknown_type_node |
ae58fa02 MM |
7272 | || TREE_CODE (t) == NAMESPACE_DECL) |
7273 | return t; | |
7274 | ||
fc6a28d7 MM |
7275 | if (DECL_P (t)) |
7276 | return tsubst_decl (t, args, complain); | |
7277 | ||
ae58fa02 MM |
7278 | if (TREE_CODE (t) == IDENTIFIER_NODE) |
7279 | type = IDENTIFIER_TYPE_VALUE (t); | |
7280 | else | |
7281 | type = TREE_TYPE (t); | |
399dedb9 | 7282 | |
50bc768d | 7283 | gcc_assert (type != unknown_type_node); |
ae58fa02 | 7284 | |
fc6a28d7 | 7285 | if (type |
ae58fa02 | 7286 | && TREE_CODE (t) != TYPENAME_TYPE |
4393e105 MM |
7287 | && TREE_CODE (t) != IDENTIFIER_NODE |
7288 | && TREE_CODE (t) != FUNCTION_TYPE | |
7289 | && TREE_CODE (t) != METHOD_TYPE) | |
7290 | type = tsubst (type, args, complain, in_decl); | |
7291 | if (type == error_mark_node) | |
7292 | return error_mark_node; | |
ae58fa02 | 7293 | |
ae58fa02 MM |
7294 | switch (TREE_CODE (t)) |
7295 | { | |
7296 | case RECORD_TYPE: | |
7297 | case UNION_TYPE: | |
7298 | case ENUMERAL_TYPE: | |
4393e105 MM |
7299 | return tsubst_aggr_type (t, args, complain, in_decl, |
7300 | /*entering_scope=*/0); | |
ae58fa02 MM |
7301 | |
7302 | case ERROR_MARK: | |
7303 | case IDENTIFIER_NODE: | |
ae58fa02 MM |
7304 | case VOID_TYPE: |
7305 | case REAL_TYPE: | |
7306 | case COMPLEX_TYPE: | |
c00996a3 | 7307 | case VECTOR_TYPE: |
ae58fa02 MM |
7308 | case BOOLEAN_TYPE: |
7309 | case INTEGER_CST: | |
7310 | case REAL_CST: | |
7311 | case STRING_CST: | |
7312 | return t; | |
7313 | ||
7314 | case INTEGER_TYPE: | |
7315 | if (t == integer_type_node) | |
7316 | return t; | |
7317 | ||
7318 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST | |
7319 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) | |
7320 | return t; | |
d2e5ee5c | 7321 | |
5566b478 | 7322 | { |
ddce3528 | 7323 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
7ddedda4 | 7324 | |
6ee3ffe8 MM |
7325 | max = tsubst_expr (omax, args, complain, in_decl, |
7326 | /*integral_constant_expression_p=*/false); | |
4ef69b83 | 7327 | max = fold_decl_constant_value (max); |
8dd3f57a | 7328 | |
eff3a276 MM |
7329 | if (TREE_CODE (max) != INTEGER_CST |
7330 | && TREE_CODE (max) != TEMPLATE_PARM_INDEX | |
7331 | && !at_function_scope_p ()) | |
7332 | { | |
7333 | if (complain & tf_error) | |
7334 | error ("array bound is not an integer constant"); | |
7335 | return error_mark_node; | |
7336 | } | |
7337 | ||
95cd6f6f | 7338 | /* [temp.deduct] |
4393e105 | 7339 | |
95cd6f6f JC |
7340 | Type deduction may fail for any of the following |
7341 | reasons: | |
4393e105 | 7342 | |
95cd6f6f JC |
7343 | Attempting to create an array with a size that is |
7344 | zero or negative. */ | |
7345 | if (integer_zerop (max) && !(complain & tf_error)) | |
7346 | /* We must fail if performing argument deduction (as | |
7347 | indicated by the state of complain), so that | |
7348 | another substitution can be found. */ | |
7349 | return error_mark_node; | |
95cd6f6f JC |
7350 | else if (TREE_CODE (max) == INTEGER_CST |
7351 | && INT_CST_LT (max, integer_zero_node)) | |
7352 | { | |
c2ea3a40 | 7353 | if (complain & tf_error) |
95cd6f6f | 7354 | error ("creating array with negative size (%qE)", max); |
4393e105 MM |
7355 | |
7356 | return error_mark_node; | |
0ecfe0b4 JM |
7357 | } |
7358 | ||
c95cd22e | 7359 | return compute_array_index_type (NULL_TREE, max); |
ae58fa02 MM |
7360 | } |
7361 | ||
7362 | case TEMPLATE_TYPE_PARM: | |
7363 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 7364 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
ae58fa02 MM |
7365 | case TEMPLATE_PARM_INDEX: |
7366 | { | |
7367 | int idx; | |
7368 | int level; | |
7369 | int levels; | |
315fb5db | 7370 | tree arg = NULL_TREE; |
0ecfe0b4 JM |
7371 | |
7372 | r = NULL_TREE; | |
ae58fa02 | 7373 | |
315fb5db | 7374 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
ae58fa02 | 7375 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
a1281f45 KL |
7376 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM |
7377 | || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
ae58fa02 MM |
7378 | { |
7379 | idx = TEMPLATE_TYPE_IDX (t); | |
7380 | level = TEMPLATE_TYPE_LEVEL (t); | |
7381 | } | |
7382 | else | |
7383 | { | |
7384 | idx = TEMPLATE_PARM_IDX (t); | |
7385 | level = TEMPLATE_PARM_LEVEL (t); | |
7386 | } | |
7387 | ||
315fb5db NS |
7388 | levels = TMPL_ARGS_DEPTH (args); |
7389 | if (level <= levels) | |
7390 | arg = TMPL_ARG (args, level, idx); | |
ae58fa02 | 7391 | |
315fb5db NS |
7392 | if (arg == error_mark_node) |
7393 | return error_mark_node; | |
7394 | else if (arg != NULL_TREE) | |
7395 | { | |
7396 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
ae58fa02 | 7397 | { |
9180c238 | 7398 | int quals; |
315fb5db | 7399 | gcc_assert (TYPE_P (arg)); |
9180c238 JM |
7400 | |
7401 | /* cv-quals from the template are discarded when | |
7402 | substituting in a function or reference type. */ | |
7403 | if (TREE_CODE (arg) == FUNCTION_TYPE | |
7404 | || TREE_CODE (arg) == METHOD_TYPE | |
7405 | || TREE_CODE (arg) == REFERENCE_TYPE) | |
7406 | quals = cp_type_quals (arg); | |
7407 | else | |
7408 | quals = cp_type_quals (arg) | cp_type_quals (t); | |
7409 | ||
315fb5db | 7410 | return cp_build_qualified_type_real |
9180c238 | 7411 | (arg, quals, complain | tf_ignore_bad_quals); |
315fb5db NS |
7412 | } |
7413 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
7414 | { | |
7415 | /* We are processing a type constructed from a | |
7416 | template template parameter. */ | |
7417 | tree argvec = tsubst (TYPE_TI_ARGS (t), | |
7418 | args, complain, in_decl); | |
7419 | if (argvec == error_mark_node) | |
7420 | return error_mark_node; | |
c8094d83 | 7421 | |
315fb5db NS |
7422 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
7423 | are resolving nested-types in the signature of a | |
7424 | member function templates. Otherwise ARG is a | |
7425 | TEMPLATE_DECL and is the real template to be | |
7426 | instantiated. */ | |
7427 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
7428 | arg = TYPE_NAME (arg); | |
c8094d83 MS |
7429 | |
7430 | r = lookup_template_class (arg, | |
7431 | argvec, in_decl, | |
315fb5db NS |
7432 | DECL_CONTEXT (arg), |
7433 | /*entering_scope=*/0, | |
7434 | complain); | |
7435 | return cp_build_qualified_type_real | |
7436 | (r, TYPE_QUALS (t), complain); | |
ae58fa02 | 7437 | } |
315fb5db NS |
7438 | else |
7439 | /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */ | |
7440 | return arg; | |
ae58fa02 MM |
7441 | } |
7442 | ||
7443 | if (level == 1) | |
7444 | /* This can happen during the attempted tsubst'ing in | |
7445 | unify. This means that we don't yet have any information | |
7446 | about the template parameter in question. */ | |
7447 | return t; | |
7448 | ||
7449 | /* If we get here, we must have been looking at a parm for a | |
7450 | more deeply nested template. Make a new version of this | |
7451 | template parameter, but with a lower level. */ | |
7452 | switch (TREE_CODE (t)) | |
7453 | { | |
7454 | case TEMPLATE_TYPE_PARM: | |
7455 | case TEMPLATE_TEMPLATE_PARM: | |
a1281f45 | 7456 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
89d684bb | 7457 | if (cp_type_quals (t)) |
ae58fa02 | 7458 | { |
9ccf6541 | 7459 | r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl); |
0cbd7506 MS |
7460 | r = cp_build_qualified_type_real |
7461 | (r, cp_type_quals (t), | |
4f2b0fb2 NS |
7462 | complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
7463 | ? tf_ignore_bad_quals : 0)); | |
9ccf6541 MM |
7464 | } |
7465 | else | |
7466 | { | |
11e74ea6 | 7467 | r = copy_type (t); |
9ccf6541 MM |
7468 | TEMPLATE_TYPE_PARM_INDEX (r) |
7469 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), | |
7470 | r, levels); | |
7471 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); | |
7472 | TYPE_MAIN_VARIANT (r) = r; | |
7473 | TYPE_POINTER_TO (r) = NULL_TREE; | |
7474 | TYPE_REFERENCE_TO (r) = NULL_TREE; | |
7475 | ||
a1281f45 | 7476 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
9ccf6541 MM |
7477 | { |
7478 | tree argvec = tsubst (TYPE_TI_ARGS (t), args, | |
c8094d83 | 7479 | complain, in_decl); |
9ccf6541 MM |
7480 | if (argvec == error_mark_node) |
7481 | return error_mark_node; | |
4393e105 | 7482 | |
9ccf6541 MM |
7483 | TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r) |
7484 | = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE); | |
7485 | } | |
ae58fa02 MM |
7486 | } |
7487 | break; | |
7488 | ||
7489 | case TEMPLATE_PARM_INDEX: | |
7490 | r = reduce_template_parm_level (t, type, levels); | |
7491 | break; | |
c8094d83 | 7492 | |
ae58fa02 | 7493 | default: |
315fb5db | 7494 | gcc_unreachable (); |
ae58fa02 MM |
7495 | } |
7496 | ||
5566b478 | 7497 | return r; |
ae58fa02 | 7498 | } |
5566b478 | 7499 | |
8d08fdba MS |
7500 | case TREE_LIST: |
7501 | { | |
058b15c1 | 7502 | tree purpose, value, chain; |
8d08fdba MS |
7503 | |
7504 | if (t == void_list_node) | |
7505 | return t; | |
7506 | ||
8d08fdba MS |
7507 | purpose = TREE_PURPOSE (t); |
7508 | if (purpose) | |
4393e105 MM |
7509 | { |
7510 | purpose = tsubst (purpose, args, complain, in_decl); | |
7511 | if (purpose == error_mark_node) | |
7512 | return error_mark_node; | |
7513 | } | |
8d08fdba MS |
7514 | value = TREE_VALUE (t); |
7515 | if (value) | |
4393e105 MM |
7516 | { |
7517 | value = tsubst (value, args, complain, in_decl); | |
7518 | if (value == error_mark_node) | |
7519 | return error_mark_node; | |
7520 | } | |
8d08fdba MS |
7521 | chain = TREE_CHAIN (t); |
7522 | if (chain && chain != void_type_node) | |
4393e105 MM |
7523 | { |
7524 | chain = tsubst (chain, args, complain, in_decl); | |
7525 | if (chain == error_mark_node) | |
7526 | return error_mark_node; | |
7527 | } | |
8d08fdba MS |
7528 | if (purpose == TREE_PURPOSE (t) |
7529 | && value == TREE_VALUE (t) | |
7530 | && chain == TREE_CHAIN (t)) | |
7531 | return t; | |
058b15c1 | 7532 | return hash_tree_cons (purpose, value, chain); |
8d08fdba | 7533 | } |
c8094d83 | 7534 | |
95b4aca6 | 7535 | case TREE_BINFO: |
bd7eccc4 | 7536 | /* We should never be tsubsting a binfo. */ |
315fb5db | 7537 | gcc_unreachable (); |
85b71cf2 | 7538 | |
95b4aca6 NS |
7539 | case TREE_VEC: |
7540 | /* A vector of template arguments. */ | |
50bc768d | 7541 | gcc_assert (!type); |
a91db711 | 7542 | return tsubst_template_args (t, args, complain, in_decl); |
8d08fdba | 7543 | |
8d08fdba MS |
7544 | case POINTER_TYPE: |
7545 | case REFERENCE_TYPE: | |
7546 | { | |
8d08fdba | 7547 | enum tree_code code; |
79a7c7fa | 7548 | |
46c895ac | 7549 | if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE) |
8d08fdba MS |
7550 | return t; |
7551 | ||
7552 | code = TREE_CODE (t); | |
4393e105 MM |
7553 | |
7554 | ||
7555 | /* [temp.deduct] | |
c8094d83 | 7556 | |
4393e105 | 7557 | Type deduction may fail for any of the following |
c8094d83 | 7558 | reasons: |
4393e105 MM |
7559 | |
7560 | -- Attempting to create a pointer to reference type. | |
7561 | -- Attempting to create a reference to a reference type or | |
7562 | a reference to void. */ | |
0ecfe0b4 JM |
7563 | if (TREE_CODE (type) == REFERENCE_TYPE |
7564 | || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE)) | |
79a7c7fa | 7565 | { |
82a98427 | 7566 | static location_t last_loc; |
79a7c7fa JM |
7567 | |
7568 | /* We keep track of the last time we issued this error | |
7569 | message to avoid spewing a ton of messages during a | |
7570 | single bad template instantiation. */ | |
c2ea3a40 | 7571 | if (complain & tf_error |
93409b8c | 7572 | #ifdef USE_MAPPED_LOCATION |
9b60dfd7 | 7573 | && last_loc != input_location |
93409b8c | 7574 | #else |
82a98427 | 7575 | && (last_loc.line != input_line |
9b60dfd7 | 7576 | || last_loc.file != input_filename) |
93409b8c | 7577 | #endif |
9b60dfd7 | 7578 | ) |
79a7c7fa | 7579 | { |
0ecfe0b4 | 7580 | if (TREE_CODE (type) == VOID_TYPE) |
33bd39a2 | 7581 | error ("forming reference to void"); |
0ecfe0b4 | 7582 | else |
0f51ccfc | 7583 | error ("forming %s to reference type %qT", |
0cbd7506 MS |
7584 | (code == POINTER_TYPE) ? "pointer" : "reference", |
7585 | type); | |
82a98427 | 7586 | last_loc = input_location; |
79a7c7fa JM |
7587 | } |
7588 | ||
4393e105 | 7589 | return error_mark_node; |
79a7c7fa JM |
7590 | } |
7591 | else if (code == POINTER_TYPE) | |
46c895ac NS |
7592 | { |
7593 | r = build_pointer_type (type); | |
7594 | if (TREE_CODE (type) == METHOD_TYPE) | |
7595 | r = build_ptrmemfunc_type (r); | |
7596 | } | |
8d08fdba MS |
7597 | else |
7598 | r = build_reference_type (type); | |
adecb3f4 | 7599 | r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain); |
79a7c7fa | 7600 | |
a5f1c5f8 NS |
7601 | if (r != error_mark_node) |
7602 | /* Will this ever be needed for TYPE_..._TO values? */ | |
7603 | layout_type (r); | |
c8094d83 | 7604 | |
8d08fdba MS |
7605 | return r; |
7606 | } | |
a4443a08 | 7607 | case OFFSET_TYPE: |
0ecfe0b4 | 7608 | { |
4393e105 MM |
7609 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
7610 | if (r == error_mark_node || !IS_AGGR_TYPE (r)) | |
7611 | { | |
7612 | /* [temp.deduct] | |
7613 | ||
7614 | Type deduction may fail for any of the following | |
7615 | reasons: | |
c8094d83 | 7616 | |
4393e105 | 7617 | -- Attempting to create "pointer to member of T" when T |
0cbd7506 | 7618 | is not a class type. */ |
c2ea3a40 | 7619 | if (complain & tf_error) |
0f51ccfc | 7620 | error ("creating pointer to member of non-class type %qT", r); |
4393e105 MM |
7621 | return error_mark_node; |
7622 | } | |
46c895ac NS |
7623 | if (TREE_CODE (type) == REFERENCE_TYPE) |
7624 | { | |
4f09be91 | 7625 | if (complain & tf_error) |
0f51ccfc | 7626 | error ("creating pointer to member reference type %qT", type); |
cb6d4a9f VR |
7627 | return error_mark_node; |
7628 | } | |
7629 | if (TREE_CODE (type) == VOID_TYPE) | |
7630 | { | |
7631 | if (complain & tf_error) | |
7632 | error ("creating pointer to member of type void"); | |
46c895ac NS |
7633 | return error_mark_node; |
7634 | } | |
50bc768d | 7635 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
46c895ac | 7636 | if (TREE_CODE (type) == FUNCTION_TYPE) |
a5ac359a | 7637 | { |
0cbd7506 MS |
7638 | /* The type of the implicit object parameter gets its |
7639 | cv-qualifiers from the FUNCTION_TYPE. */ | |
a5ac359a | 7640 | tree method_type; |
0cbd7506 MS |
7641 | tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r), |
7642 | cp_type_quals (type)); | |
7643 | tree memptr; | |
7644 | method_type = build_method_type_directly (this_type, | |
43dc123f MM |
7645 | TREE_TYPE (type), |
7646 | TYPE_ARG_TYPES (type)); | |
0cbd7506 MS |
7647 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
7648 | return cp_build_qualified_type_real (memptr, cp_type_quals (t), | |
7649 | complain); | |
a5ac359a | 7650 | } |
46c895ac | 7651 | else |
b7a78333 MM |
7652 | return cp_build_qualified_type_real (build_ptrmem_type (r, type), |
7653 | TYPE_QUALS (t), | |
7654 | complain); | |
0ecfe0b4 | 7655 | } |
8d08fdba MS |
7656 | case FUNCTION_TYPE: |
7657 | case METHOD_TYPE: | |
7658 | { | |
c11b6f21 | 7659 | tree fntype; |
c7222c02 | 7660 | tree specs; |
4393e105 MM |
7661 | fntype = tsubst_function_type (t, args, complain, in_decl); |
7662 | if (fntype == error_mark_node) | |
7663 | return error_mark_node; | |
cabc336a | 7664 | |
34cd5ae7 | 7665 | /* Substitute the exception specification. */ |
c8094d83 | 7666 | specs = tsubst_exception_specification (t, args, complain, |
c7222c02 | 7667 | in_decl); |
9f6206d9 VR |
7668 | if (specs == error_mark_node) |
7669 | return error_mark_node; | |
c7222c02 MM |
7670 | if (specs) |
7671 | fntype = build_exception_variant (fntype, specs); | |
c11b6f21 | 7672 | return fntype; |
8d08fdba MS |
7673 | } |
7674 | case ARRAY_TYPE: | |
7675 | { | |
4393e105 MM |
7676 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
7677 | if (domain == error_mark_node) | |
7678 | return error_mark_node; | |
7679 | ||
7680 | /* As an optimization, we avoid regenerating the array type if | |
7681 | it will obviously be the same as T. */ | |
8d08fdba MS |
7682 | if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t)) |
7683 | return t; | |
0ecfe0b4 | 7684 | |
c8094d83 | 7685 | /* These checks should match the ones in grokdeclarator. |
4393e105 | 7686 | |
c8094d83 MS |
7687 | [temp.deduct] |
7688 | ||
7689 | The deduction may fail for any of the following reasons: | |
4393e105 MM |
7690 | |
7691 | -- Attempting to create an array with an element type that | |
c8094d83 | 7692 | is void, a function type, or a reference type, or [DR337] |
cfb91b67 | 7693 | an abstract class type. */ |
c8094d83 | 7694 | if (TREE_CODE (type) == VOID_TYPE |
4393e105 MM |
7695 | || TREE_CODE (type) == FUNCTION_TYPE |
7696 | || TREE_CODE (type) == REFERENCE_TYPE) | |
0ecfe0b4 | 7697 | { |
c2ea3a40 | 7698 | if (complain & tf_error) |
0f51ccfc | 7699 | error ("creating array of %qT", type); |
4393e105 | 7700 | return error_mark_node; |
0ecfe0b4 | 7701 | } |
cfb91b67 GB |
7702 | if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type)) |
7703 | { | |
7704 | if (complain & tf_error) | |
c8094d83 | 7705 | error ("creating array of %qT, which is an abstract class type", |
cfb91b67 | 7706 | type); |
c8094d83 | 7707 | return error_mark_node; |
cfb91b67 | 7708 | } |
0ecfe0b4 | 7709 | |
8d08fdba MS |
7710 | r = build_cplus_array_type (type, domain); |
7711 | return r; | |
7712 | } | |
7713 | ||
8d08fdba | 7714 | case PLUS_EXPR: |
5566b478 | 7715 | case MINUS_EXPR: |
4393e105 | 7716 | { |
c2ea3a40 NS |
7717 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
7718 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); | |
4393e105 MM |
7719 | |
7720 | if (e1 == error_mark_node || e2 == error_mark_node) | |
7721 | return error_mark_node; | |
7722 | ||
7866705a | 7723 | return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2); |
4393e105 | 7724 | } |
8d08fdba MS |
7725 | |
7726 | case NEGATE_EXPR: | |
7727 | case NOP_EXPR: | |
4393e105 | 7728 | { |
c2ea3a40 | 7729 | tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
7730 | if (e == error_mark_node) |
7731 | return error_mark_node; | |
7732 | ||
7866705a | 7733 | return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e); |
4393e105 | 7734 | } |
8d08fdba | 7735 | |
5566b478 MS |
7736 | case TYPENAME_TYPE: |
7737 | { | |
4393e105 MM |
7738 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, |
7739 | in_decl, /*entering_scope=*/1); | |
7740 | tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, | |
c8094d83 | 7741 | complain, in_decl); |
4393e105 MM |
7742 | |
7743 | if (ctx == error_mark_node || f == error_mark_node) | |
7744 | return error_mark_node; | |
ae58fa02 | 7745 | |
bf8f3f93 MM |
7746 | if (!IS_AGGR_TYPE (ctx)) |
7747 | { | |
c2ea3a40 | 7748 | if (complain & tf_error) |
0f51ccfc | 7749 | error ("%qT is not a class, struct, or union type", ctx); |
bf8f3f93 MM |
7750 | return error_mark_node; |
7751 | } | |
7752 | else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx)) | |
7753 | { | |
7754 | /* Normally, make_typename_type does not require that the CTX | |
7755 | have complete type in order to allow things like: | |
c8094d83 | 7756 | |
0cbd7506 | 7757 | template <class T> struct S { typename S<T>::X Y; }; |
ae58fa02 | 7758 | |
bf8f3f93 MM |
7759 | But, such constructs have already been resolved by this |
7760 | point, so here CTX really should have complete type, unless | |
7761 | it's a partial instantiation. */ | |
4393e105 | 7762 | ctx = complete_type (ctx); |
d0f062fb | 7763 | if (!COMPLETE_TYPE_P (ctx)) |
4393e105 | 7764 | { |
c2ea3a40 | 7765 | if (complain & tf_error) |
7a228918 | 7766 | cxx_incomplete_type_error (NULL_TREE, ctx); |
4393e105 MM |
7767 | return error_mark_node; |
7768 | } | |
7769 | } | |
ae58fa02 | 7770 | |
fc6a28d7 | 7771 | f = make_typename_type (ctx, f, typename_type, |
4f2b0fb2 | 7772 | (complain & tf_error) | tf_keep_type_decl); |
f0bcd168 MM |
7773 | if (f == error_mark_node) |
7774 | return f; | |
0cbd7506 MS |
7775 | if (TREE_CODE (f) == TYPE_DECL) |
7776 | { | |
4f2b0fb2 | 7777 | complain |= tf_ignore_bad_quals; |
0cbd7506 MS |
7778 | f = TREE_TYPE (f); |
7779 | } | |
c8094d83 | 7780 | |
fc6a28d7 MM |
7781 | if (TREE_CODE (f) != TYPENAME_TYPE) |
7782 | { | |
7783 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) | |
c8094d83 | 7784 | error ("%qT resolves to %qT, which is not an enumeration type", |
fc6a28d7 MM |
7785 | t, f); |
7786 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) | |
c8094d83 | 7787 | error ("%qT resolves to %qT, which is is not a class type", |
fc6a28d7 MM |
7788 | t, f); |
7789 | } | |
7790 | ||
0cbd7506 MS |
7791 | return cp_build_qualified_type_real |
7792 | (f, cp_type_quals (f) | cp_type_quals (t), complain); | |
5566b478 | 7793 | } |
c8094d83 | 7794 | |
b8c6534b KL |
7795 | case UNBOUND_CLASS_TEMPLATE: |
7796 | { | |
7797 | tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain, | |
7798 | in_decl, /*entering_scope=*/1); | |
7799 | tree name = TYPE_IDENTIFIER (t); | |
b939a023 | 7800 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
b8c6534b KL |
7801 | |
7802 | if (ctx == error_mark_node || name == error_mark_node) | |
7803 | return error_mark_node; | |
7804 | ||
b939a023 KL |
7805 | if (parm_list) |
7806 | parm_list = tsubst_template_parms (parm_list, args, complain); | |
7807 | return make_unbound_class_template (ctx, name, parm_list, complain); | |
b8c6534b KL |
7808 | } |
7809 | ||
5566b478 | 7810 | case INDIRECT_REF: |
5566b478 | 7811 | case ADDR_EXPR: |
058b15c1 | 7812 | case CALL_EXPR: |
315fb5db | 7813 | gcc_unreachable (); |
5566b478 MS |
7814 | |
7815 | case ARRAY_REF: | |
4393e105 | 7816 | { |
c2ea3a40 | 7817 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
015c2c66 MM |
7818 | tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl, |
7819 | /*integral_constant_expression_p=*/false); | |
4393e105 MM |
7820 | if (e1 == error_mark_node || e2 == error_mark_node) |
7821 | return error_mark_node; | |
7822 | ||
44de5aeb | 7823 | return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE); |
4393e105 | 7824 | } |
5566b478 | 7825 | |
fc378698 | 7826 | case SCOPE_REF: |
4393e105 | 7827 | { |
c2ea3a40 | 7828 | tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
4393e105 MM |
7829 | tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
7830 | if (e1 == error_mark_node || e2 == error_mark_node) | |
7831 | return error_mark_node; | |
7832 | ||
02ed62dd MM |
7833 | return build_qualified_name (/*type=*/NULL_TREE, |
7834 | e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t)); | |
4393e105 | 7835 | } |
fc378698 | 7836 | |
b894fc05 | 7837 | case TYPEOF_TYPE: |
4393e105 | 7838 | { |
b830b74c | 7839 | tree type; |
4393e105 | 7840 | |
015c2c66 MM |
7841 | type = finish_typeof (tsubst_expr |
7842 | (TYPEOF_TYPE_EXPR (t), args, | |
7843 | complain, in_decl, | |
7844 | /*integral_constant_expression_p=*/false)); | |
b830b74c | 7845 | return cp_build_qualified_type_real (type, |
dce50630 | 7846 | cp_type_quals (t) |
b830b74c | 7847 | | cp_type_quals (type), |
dce50630 | 7848 | complain); |
4393e105 | 7849 | } |
b894fc05 | 7850 | |
8d08fdba | 7851 | default: |
9e637a26 | 7852 | sorry ("use of %qs in template", |
8d08fdba MS |
7853 | tree_code_name [(int) TREE_CODE (t)]); |
7854 | return error_mark_node; | |
7855 | } | |
7856 | } | |
7857 | ||
ee76b931 MM |
7858 | /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the |
7859 | type of the expression on the left-hand side of the "." or "->" | |
7860 | operator. */ | |
7861 | ||
7862 | static tree | |
7863 | tsubst_baselink (tree baselink, tree object_type, | |
7864 | tree args, tsubst_flags_t complain, tree in_decl) | |
7865 | { | |
7866 | tree name; | |
7867 | tree qualifying_scope; | |
7868 | tree fns; | |
6f67f000 | 7869 | tree optype; |
ee76b931 MM |
7870 | tree template_args = 0; |
7871 | bool template_id_p = false; | |
7872 | ||
51a203d9 MM |
7873 | /* A baselink indicates a function from a base class. Both the |
7874 | BASELINK_ACCESS_BINFO and the base class referenced may | |
7875 | indicate bases of the template class, rather than the | |
7876 | instantiated class. In addition, lookups that were not | |
7877 | ambiguous before may be ambiguous now. Therefore, we perform | |
7878 | the lookup again. */ | |
ee76b931 | 7879 | qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
51a203d9 MM |
7880 | qualifying_scope = tsubst (qualifying_scope, args, |
7881 | complain, in_decl); | |
ee76b931 | 7882 | fns = BASELINK_FUNCTIONS (baselink); |
6f67f000 | 7883 | optype = BASELINK_OPTYPE (baselink); |
ee76b931 MM |
7884 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
7885 | { | |
7886 | template_id_p = true; | |
7887 | template_args = TREE_OPERAND (fns, 1); | |
7888 | fns = TREE_OPERAND (fns, 0); | |
bf12d54d NS |
7889 | if (template_args) |
7890 | template_args = tsubst_template_args (template_args, args, | |
7891 | complain, in_decl); | |
ee76b931 MM |
7892 | } |
7893 | name = DECL_NAME (get_first_fn (fns)); | |
7894 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1); | |
c8094d83 | 7895 | |
9b60dfd7 MM |
7896 | /* If lookup found a single function, mark it as used at this |
7897 | point. (If it lookup found multiple functions the one selected | |
7898 | later by overload resolution will be marked as used at that | |
7899 | point.) */ | |
7900 | if (BASELINK_P (baselink)) | |
7901 | fns = BASELINK_FUNCTIONS (baselink); | |
7902 | if (!template_id_p && !really_overloaded_fn (fns)) | |
7903 | mark_used (OVL_CURRENT (fns)); | |
7904 | ||
7905 | /* Add back the template arguments, if present. */ | |
ee76b931 | 7906 | if (BASELINK_P (baselink) && template_id_p) |
c8094d83 | 7907 | BASELINK_FUNCTIONS (baselink) |
ee76b931 MM |
7908 | = build_nt (TEMPLATE_ID_EXPR, |
7909 | BASELINK_FUNCTIONS (baselink), | |
7910 | template_args); | |
6f67f000 MM |
7911 | /* Update the conversion operator type. */ |
7912 | BASELINK_OPTYPE (baselink) | |
7913 | = tsubst (optype, args, complain, in_decl); | |
9b60dfd7 | 7914 | |
ee76b931 MM |
7915 | if (!object_type) |
7916 | object_type = current_class_type; | |
c8094d83 | 7917 | return adjust_result_of_qualified_name_lookup (baselink, |
ee76b931 MM |
7918 | qualifying_scope, |
7919 | object_type); | |
7920 | } | |
7921 | ||
7922 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is | |
7923 | true if the qualified-id will be a postfix-expression in-and-of | |
7924 | itself; false if more of the postfix-expression follows the | |
7925 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand | |
7926 | of "&". */ | |
7927 | ||
7928 | static tree | |
c8094d83 | 7929 | tsubst_qualified_id (tree qualified_id, tree args, |
ee76b931 MM |
7930 | tsubst_flags_t complain, tree in_decl, |
7931 | bool done, bool address_p) | |
7932 | { | |
7933 | tree expr; | |
7934 | tree scope; | |
7935 | tree name; | |
7936 | bool is_template; | |
7937 | tree template_args; | |
7938 | ||
50bc768d | 7939 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
ee76b931 | 7940 | |
ee76b931 MM |
7941 | /* Figure out what name to look up. */ |
7942 | name = TREE_OPERAND (qualified_id, 1); | |
7943 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
7944 | { | |
7945 | is_template = true; | |
bf12d54d NS |
7946 | template_args = TREE_OPERAND (name, 1); |
7947 | if (template_args) | |
7948 | template_args = tsubst_template_args (template_args, args, | |
7949 | complain, in_decl); | |
ee76b931 MM |
7950 | name = TREE_OPERAND (name, 0); |
7951 | } | |
7952 | else | |
7953 | { | |
7954 | is_template = false; | |
7955 | template_args = NULL_TREE; | |
7956 | } | |
7957 | ||
6eeba0cc MM |
7958 | /* Substitute into the qualifying scope. When there are no ARGS, we |
7959 | are just trying to simplify a non-dependent expression. In that | |
7960 | case the qualifying scope may be dependent, and, in any case, | |
7961 | substituting will not help. */ | |
7962 | scope = TREE_OPERAND (qualified_id, 0); | |
7963 | if (args) | |
7964 | { | |
7965 | scope = tsubst (scope, args, complain, in_decl); | |
7966 | expr = tsubst_copy (name, args, complain, in_decl); | |
7967 | } | |
7968 | else | |
7969 | expr = name; | |
10b1d5e7 | 7970 | |
ab73670a | 7971 | if (dependent_type_p (scope)) |
3db45ab5 MS |
7972 | return build_qualified_name (/*type=*/NULL_TREE, |
7973 | scope, expr, | |
02ed62dd | 7974 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id)); |
c8094d83 | 7975 | |
5e08432e | 7976 | if (!BASELINK_P (name) && !DECL_P (expr)) |
12483c9f | 7977 | { |
8ca4bf25 MM |
7978 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
7979 | /* If this were actually a destructor call, it would have been | |
7980 | parsed as such by the parser. */ | |
7981 | expr = error_mark_node; | |
7982 | else | |
7983 | expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false); | |
12483c9f NS |
7984 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
7985 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) | |
7986 | { | |
7987 | if (complain & tf_error) | |
b2693faf | 7988 | { |
0f51ccfc | 7989 | error ("dependent-name %qE is parsed as a non-type, but " |
b2693faf | 7990 | "instantiation yields a type", qualified_id); |
0f51ccfc | 7991 | inform ("say %<typename %E%> if a type is meant", qualified_id); |
b2693faf | 7992 | } |
12483c9f NS |
7993 | return error_mark_node; |
7994 | } | |
7995 | } | |
c8094d83 | 7996 | |
279b8466 | 7997 | if (DECL_P (expr)) |
8f78f01f MM |
7998 | { |
7999 | check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, | |
8000 | scope); | |
8001 | /* Remember that there was a reference to this entity. */ | |
8002 | mark_used (expr); | |
8003 | } | |
8004 | ||
8005 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) | |
8006 | { | |
8007 | if (complain & tf_error) | |
c8094d83 | 8008 | qualified_name_lookup_error (scope, |
8f78f01f MM |
8009 | TREE_OPERAND (qualified_id, 1), |
8010 | expr); | |
8011 | return error_mark_node; | |
8012 | } | |
ee76b931 MM |
8013 | |
8014 | if (is_template) | |
10b1d5e7 | 8015 | expr = lookup_template_function (expr, template_args); |
ee76b931 | 8016 | |
22038b2c | 8017 | if (expr == error_mark_node && complain & tf_error) |
8f78f01f MM |
8018 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
8019 | expr); | |
22038b2c | 8020 | else if (TYPE_P (scope)) |
ee76b931 | 8021 | { |
c8094d83 | 8022 | expr = (adjust_result_of_qualified_name_lookup |
ee76b931 | 8023 | (expr, scope, current_class_type)); |
3db45ab5 | 8024 | expr = (finish_qualified_id_expr |
02ed62dd MM |
8025 | (scope, expr, done, address_p, |
8026 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), | |
8027 | /*template_arg_p=*/false)); | |
ee76b931 | 8028 | } |
c8094d83 | 8029 | |
015c2c66 MM |
8030 | /* Expressions do not generally have reference type. */ |
8031 | if (TREE_CODE (expr) != SCOPE_REF | |
8032 | /* However, if we're about to form a pointer-to-member, we just | |
8033 | want the referenced member referenced. */ | |
8034 | && TREE_CODE (expr) != OFFSET_REF) | |
aec5ba60 | 8035 | expr = convert_from_reference (expr); |
ee76b931 MM |
8036 | |
8037 | return expr; | |
8038 | } | |
8039 | ||
00d3396f JM |
8040 | /* Like tsubst, but deals with expressions. This function just replaces |
8041 | template parms; to finish processing the resultant expression, use | |
8042 | tsubst_expr. */ | |
8043 | ||
14d22dd6 | 8044 | static tree |
3a978d72 | 8045 | tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
5566b478 MS |
8046 | { |
8047 | enum tree_code code; | |
8452b1d3 | 8048 | tree r; |
8d08fdba | 8049 | |
5566b478 MS |
8050 | if (t == NULL_TREE || t == error_mark_node) |
8051 | return t; | |
8052 | ||
8053 | code = TREE_CODE (t); | |
b7484fbe | 8054 | |
5566b478 MS |
8055 | switch (code) |
8056 | { | |
8057 | case PARM_DECL: | |
a723baf1 | 8058 | r = retrieve_local_specialization (t); |
50bc768d | 8059 | gcc_assert (r != NULL); |
c0694c4b | 8060 | mark_used (r); |
a723baf1 | 8061 | return r; |
5566b478 MS |
8062 | |
8063 | case CONST_DECL: | |
ed44da02 MM |
8064 | { |
8065 | tree enum_type; | |
8066 | tree v; | |
8067 | ||
a723baf1 MM |
8068 | if (DECL_TEMPLATE_PARM_P (t)) |
8069 | return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl); | |
7b6d72fc MM |
8070 | /* There is no need to substitute into namespace-scope |
8071 | enumerators. */ | |
8072 | if (DECL_NAMESPACE_SCOPE_P (t)) | |
ed44da02 | 8073 | return t; |
d5a10cf0 MM |
8074 | /* If ARGS is NULL, then T is known to be non-dependent. */ |
8075 | if (args == NULL_TREE) | |
8a784e4a | 8076 | return integral_constant_value (t); |
ed44da02 MM |
8077 | |
8078 | /* Unfortunately, we cannot just call lookup_name here. | |
9188c363 | 8079 | Consider: |
c8094d83 | 8080 | |
9188c363 MM |
8081 | template <int I> int f() { |
8082 | enum E { a = I }; | |
8083 | struct S { void g() { E e = a; } }; | |
8084 | }; | |
c8094d83 | 8085 | |
9188c363 MM |
8086 | When we instantiate f<7>::S::g(), say, lookup_name is not |
8087 | clever enough to find f<7>::a. */ | |
c8094d83 MS |
8088 | enum_type |
8089 | = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl, | |
ed44da02 MM |
8090 | /*entering_scope=*/0); |
8091 | ||
c8094d83 MS |
8092 | for (v = TYPE_VALUES (enum_type); |
8093 | v != NULL_TREE; | |
ed44da02 MM |
8094 | v = TREE_CHAIN (v)) |
8095 | if (TREE_PURPOSE (v) == DECL_NAME (t)) | |
8096 | return TREE_VALUE (v); | |
8097 | ||
8098 | /* We didn't find the name. That should never happen; if | |
8099 | name-lookup found it during preliminary parsing, we | |
8100 | should find it again here during instantiation. */ | |
315fb5db | 8101 | gcc_unreachable (); |
ed44da02 | 8102 | } |
db04386f | 8103 | return t; |
ed44da02 | 8104 | |
5566b478 MS |
8105 | case FIELD_DECL: |
8106 | if (DECL_CONTEXT (t)) | |
8107 | { | |
0978790f | 8108 | tree ctx; |
0978790f | 8109 | |
4393e105 | 8110 | ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl, |
36a117a5 | 8111 | /*entering_scope=*/1); |
0978790f | 8112 | if (ctx != DECL_CONTEXT (t)) |
bad1f462 KL |
8113 | { |
8114 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); | |
8115 | if (!r) | |
8116 | { | |
8117 | if (complain & tf_error) | |
8118 | error ("using invalid field %qD", t); | |
8119 | return error_mark_node; | |
8120 | } | |
8121 | return r; | |
8122 | } | |
5566b478 | 8123 | } |
c8094d83 | 8124 | |
5566b478 MS |
8125 | return t; |
8126 | ||
8127 | case VAR_DECL: | |
8128 | case FUNCTION_DECL: | |
a723baf1 MM |
8129 | if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
8130 | || local_variable_p (t)) | |
4393e105 | 8131 | t = tsubst (t, args, complain, in_decl); |
5566b478 MS |
8132 | mark_used (t); |
8133 | return t; | |
8134 | ||
a723baf1 | 8135 | case BASELINK: |
ee76b931 | 8136 | return tsubst_baselink (t, current_class_type, args, complain, in_decl); |
a723baf1 | 8137 | |
98c1c668 | 8138 | case TEMPLATE_DECL: |
a723baf1 | 8139 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
c8094d83 | 8140 | return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
a723baf1 | 8141 | args, complain, in_decl); |
c7222c02 | 8142 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
4393e105 | 8143 | return tsubst (t, args, complain, in_decl); |
fcea7401 KL |
8144 | else if (DECL_CLASS_SCOPE_P (t) |
8145 | && uses_template_parms (DECL_CONTEXT (t))) | |
8146 | { | |
8147 | /* Template template argument like the following example need | |
8148 | special treatment: | |
8149 | ||
8150 | template <template <class> class TT> struct C {}; | |
8151 | template <class T> struct D { | |
8152 | template <class U> struct E {}; | |
0cbd7506 | 8153 | C<E> c; // #1 |
fcea7401 KL |
8154 | }; |
8155 | D<int> d; // #2 | |
8156 | ||
8157 | We are processing the template argument `E' in #1 for | |
8158 | the template instantiation #2. Originally, `E' is a | |
8159 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we | |
8160 | have to substitute this with one having context `D<int>'. */ | |
8161 | ||
8162 | tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); | |
8163 | return lookup_field (context, DECL_NAME(t), 0, false); | |
8164 | } | |
98c1c668 | 8165 | else |
fcea7401 | 8166 | /* Ordinary template template argument. */ |
98c1c668 JM |
8167 | return t; |
8168 | ||
5566b478 MS |
8169 | case CAST_EXPR: |
8170 | case REINTERPRET_CAST_EXPR: | |
e92cc029 MS |
8171 | case CONST_CAST_EXPR: |
8172 | case STATIC_CAST_EXPR: | |
8173 | case DYNAMIC_CAST_EXPR: | |
51924768 | 8174 | case NOP_EXPR: |
5566b478 | 8175 | return build1 |
4393e105 MM |
8176 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
8177 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); | |
5566b478 MS |
8178 | |
8179 | case INDIRECT_REF: | |
5566b478 MS |
8180 | case NEGATE_EXPR: |
8181 | case TRUTH_NOT_EXPR: | |
b87692e5 | 8182 | case BIT_NOT_EXPR: |
5566b478 | 8183 | case ADDR_EXPR: |
392e3d51 | 8184 | case UNARY_PLUS_EXPR: /* Unary + */ |
5566b478 | 8185 | case SIZEOF_EXPR: |
abff8e06 | 8186 | case ALIGNOF_EXPR: |
5566b478 | 8187 | case ARROW_EXPR: |
fc378698 | 8188 | case THROW_EXPR: |
5156628f | 8189 | case TYPEID_EXPR: |
f5733617 SS |
8190 | case REALPART_EXPR: |
8191 | case IMAGPART_EXPR: | |
5566b478 | 8192 | return build1 |
6a629cac | 8193 | (code, tsubst (TREE_TYPE (t), args, complain, in_decl), |
4393e105 | 8194 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)); |
5566b478 | 8195 | |
a723baf1 MM |
8196 | case COMPONENT_REF: |
8197 | { | |
8198 | tree object; | |
8199 | tree name; | |
8200 | ||
8201 | object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl); | |
8202 | name = TREE_OPERAND (t, 1); | |
c8094d83 | 8203 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
a723baf1 MM |
8204 | { |
8205 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
8206 | complain, in_decl); | |
8207 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
8208 | } | |
8209 | else if (TREE_CODE (name) == SCOPE_REF | |
8210 | && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR) | |
8211 | { | |
8212 | tree base = tsubst_copy (TREE_OPERAND (name, 0), args, | |
8213 | complain, in_decl); | |
8214 | name = TREE_OPERAND (name, 1); | |
8215 | name = tsubst_copy (TREE_OPERAND (name, 0), args, | |
8216 | complain, in_decl); | |
8217 | name = build1 (BIT_NOT_EXPR, NULL_TREE, name); | |
02ed62dd | 8218 | name = build_qualified_name (/*type=*/NULL_TREE, |
3db45ab5 | 8219 | base, name, |
02ed62dd | 8220 | /*template_p=*/false); |
a723baf1 | 8221 | } |
ee76b931 | 8222 | else if (TREE_CODE (name) == BASELINK) |
c8094d83 MS |
8223 | name = tsubst_baselink (name, |
8224 | non_reference (TREE_TYPE (object)), | |
8225 | args, complain, | |
ee76b931 | 8226 | in_decl); |
a723baf1 | 8227 | else |
ee76b931 | 8228 | name = tsubst_copy (name, args, complain, in_decl); |
44de5aeb | 8229 | return build_nt (COMPONENT_REF, object, name, NULL_TREE); |
a723baf1 MM |
8230 | } |
8231 | ||
5566b478 MS |
8232 | case PLUS_EXPR: |
8233 | case MINUS_EXPR: | |
8234 | case MULT_EXPR: | |
8235 | case TRUNC_DIV_EXPR: | |
8236 | case CEIL_DIV_EXPR: | |
8237 | case FLOOR_DIV_EXPR: | |
8238 | case ROUND_DIV_EXPR: | |
8239 | case EXACT_DIV_EXPR: | |
8240 | case BIT_AND_EXPR: | |
5566b478 MS |
8241 | case BIT_IOR_EXPR: |
8242 | case BIT_XOR_EXPR: | |
8243 | case TRUNC_MOD_EXPR: | |
8244 | case FLOOR_MOD_EXPR: | |
8245 | case TRUTH_ANDIF_EXPR: | |
8246 | case TRUTH_ORIF_EXPR: | |
8247 | case TRUTH_AND_EXPR: | |
8248 | case TRUTH_OR_EXPR: | |
8249 | case RSHIFT_EXPR: | |
8250 | case LSHIFT_EXPR: | |
8251 | case RROTATE_EXPR: | |
8252 | case LROTATE_EXPR: | |
8253 | case EQ_EXPR: | |
8254 | case NE_EXPR: | |
8255 | case MAX_EXPR: | |
8256 | case MIN_EXPR: | |
8257 | case LE_EXPR: | |
8258 | case GE_EXPR: | |
8259 | case LT_EXPR: | |
8260 | case GT_EXPR: | |
5566b478 | 8261 | case COMPOUND_EXPR: |
5566b478 MS |
8262 | case DOTSTAR_EXPR: |
8263 | case MEMBER_REF: | |
519c9806 MM |
8264 | case PREDECREMENT_EXPR: |
8265 | case PREINCREMENT_EXPR: | |
8266 | case POSTDECREMENT_EXPR: | |
8267 | case POSTINCREMENT_EXPR: | |
5566b478 | 8268 | return build_nt |
4393e105 MM |
8269 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
8270 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 | 8271 | |
02ed62dd MM |
8272 | case SCOPE_REF: |
8273 | return build_qualified_name (/*type=*/NULL_TREE, | |
8274 | tsubst_copy (TREE_OPERAND (t, 0), | |
8275 | args, complain, in_decl), | |
8276 | tsubst_copy (TREE_OPERAND (t, 1), | |
8277 | args, complain, in_decl), | |
8278 | QUALIFIED_NAME_IS_TEMPLATE (t)); | |
8279 | ||
d8987adb NS |
8280 | case ARRAY_REF: |
8281 | return build_nt | |
8282 | (ARRAY_REF, | |
8283 | tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), | |
8284 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8285 | NULL_TREE, NULL_TREE); | |
8286 | ||
5566b478 | 8287 | case CALL_EXPR: |
c8094d83 | 8288 | return build_nt (code, |
a723baf1 MM |
8289 | tsubst_copy (TREE_OPERAND (t, 0), args, |
8290 | complain, in_decl), | |
8291 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, | |
8292 | in_decl), | |
8293 | NULL_TREE); | |
5566b478 | 8294 | |
5566b478 MS |
8295 | case COND_EXPR: |
8296 | case MODOP_EXPR: | |
40242ccf | 8297 | case PSEUDO_DTOR_EXPR: |
67da3287 | 8298 | { |
8452b1d3 | 8299 | r = build_nt |
4393e105 MM |
8300 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
8301 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8302 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
e4c2c34b | 8303 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); |
67da3287 MM |
8304 | return r; |
8305 | } | |
5566b478 MS |
8306 | |
8307 | case NEW_EXPR: | |
8308 | { | |
8452b1d3 | 8309 | r = build_nt |
4393e105 MM |
8310 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
8311 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl), | |
8312 | tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl)); | |
5566b478 MS |
8313 | NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t); |
8314 | return r; | |
8315 | } | |
8316 | ||
8317 | case DELETE_EXPR: | |
8318 | { | |
8452b1d3 | 8319 | r = build_nt |
4393e105 MM |
8320 | (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), |
8321 | tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)); | |
5566b478 MS |
8322 | DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t); |
8323 | DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t); | |
8324 | return r; | |
8325 | } | |
8326 | ||
386b8a85 JM |
8327 | case TEMPLATE_ID_EXPR: |
8328 | { | |
0cbd7506 | 8329 | /* Substituted template arguments */ |
a91db711 NS |
8330 | tree fn = TREE_OPERAND (t, 0); |
8331 | tree targs = TREE_OPERAND (t, 1); | |
856216bb | 8332 | |
a91db711 | 8333 | fn = tsubst_copy (fn, args, complain, in_decl); |
bf12d54d NS |
8334 | if (targs) |
8335 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 8336 | |
a91db711 | 8337 | return lookup_template_function (fn, targs); |
386b8a85 JM |
8338 | } |
8339 | ||
5566b478 MS |
8340 | case TREE_LIST: |
8341 | { | |
8342 | tree purpose, value, chain; | |
8343 | ||
8344 | if (t == void_list_node) | |
8345 | return t; | |
8346 | ||
8347 | purpose = TREE_PURPOSE (t); | |
8348 | if (purpose) | |
4393e105 | 8349 | purpose = tsubst_copy (purpose, args, complain, in_decl); |
5566b478 MS |
8350 | value = TREE_VALUE (t); |
8351 | if (value) | |
4393e105 | 8352 | value = tsubst_copy (value, args, complain, in_decl); |
5566b478 MS |
8353 | chain = TREE_CHAIN (t); |
8354 | if (chain && chain != void_type_node) | |
4393e105 | 8355 | chain = tsubst_copy (chain, args, complain, in_decl); |
5566b478 MS |
8356 | if (purpose == TREE_PURPOSE (t) |
8357 | && value == TREE_VALUE (t) | |
8358 | && chain == TREE_CHAIN (t)) | |
8359 | return t; | |
8360 | return tree_cons (purpose, value, chain); | |
8361 | } | |
8362 | ||
8363 | case RECORD_TYPE: | |
8364 | case UNION_TYPE: | |
8365 | case ENUMERAL_TYPE: | |
8366 | case INTEGER_TYPE: | |
8367 | case TEMPLATE_TYPE_PARM: | |
73b0fce8 | 8368 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 8369 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
f84b4be9 | 8370 | case TEMPLATE_PARM_INDEX: |
5566b478 MS |
8371 | case POINTER_TYPE: |
8372 | case REFERENCE_TYPE: | |
8373 | case OFFSET_TYPE: | |
8374 | case FUNCTION_TYPE: | |
8375 | case METHOD_TYPE: | |
8376 | case ARRAY_TYPE: | |
8377 | case TYPENAME_TYPE: | |
b8c6534b | 8378 | case UNBOUND_CLASS_TEMPLATE: |
2a2b1d56 | 8379 | case TYPEOF_TYPE: |
f84b4be9 | 8380 | case TYPE_DECL: |
4393e105 | 8381 | return tsubst (t, args, complain, in_decl); |
5566b478 | 8382 | |
e92cc029 | 8383 | case IDENTIFIER_NODE: |
421844e7 | 8384 | if (IDENTIFIER_TYPENAME_P (t)) |
1f6e1acc AS |
8385 | { |
8386 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
1f84ec23 | 8387 | return mangle_conv_op_name_for_type (new_type); |
1f6e1acc | 8388 | } |
e92cc029 MS |
8389 | else |
8390 | return t; | |
8391 | ||
5156628f | 8392 | case CONSTRUCTOR: |
4038c495 GB |
8393 | /* This is handled by tsubst_copy_and_build. */ |
8394 | gcc_unreachable (); | |
5156628f | 8395 | |
371534a9 | 8396 | case VA_ARG_EXPR: |
ea333e1c | 8397 | return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain, |
c2ea3a40 NS |
8398 | in_decl), |
8399 | tsubst (TREE_TYPE (t), args, complain, in_decl)); | |
f9817201 | 8400 | |
543a0daa RH |
8401 | case CLEANUP_POINT_EXPR: |
8402 | /* We shouldn't have built any of these during initial template | |
8403 | generation. Instead, they should be built during instantiation | |
8404 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ | |
315fb5db | 8405 | gcc_unreachable (); |
543a0daa | 8406 | |
7eab6e7b MM |
8407 | case OFFSET_REF: |
8408 | mark_used (TREE_OPERAND (t, 1)); | |
8409 | return t; | |
8410 | ||
5566b478 MS |
8411 | default: |
8412 | return t; | |
8413 | } | |
8414 | } | |
8415 | ||
1799e5d5 RH |
8416 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
8417 | ||
8418 | static tree | |
8419 | tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain, | |
8420 | tree in_decl) | |
8421 | { | |
8422 | tree new_clauses = NULL, nc, oc; | |
8423 | ||
8424 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) | |
8425 | { | |
8426 | nc = copy_node (oc); | |
8427 | OMP_CLAUSE_CHAIN (nc) = new_clauses; | |
8428 | new_clauses = nc; | |
8429 | ||
8430 | switch (OMP_CLAUSE_CODE (nc)) | |
8431 | { | |
8432 | case OMP_CLAUSE_PRIVATE: | |
8433 | case OMP_CLAUSE_SHARED: | |
8434 | case OMP_CLAUSE_FIRSTPRIVATE: | |
8435 | case OMP_CLAUSE_LASTPRIVATE: | |
8436 | case OMP_CLAUSE_REDUCTION: | |
8437 | case OMP_CLAUSE_COPYIN: | |
8438 | case OMP_CLAUSE_COPYPRIVATE: | |
8439 | case OMP_CLAUSE_IF: | |
8440 | case OMP_CLAUSE_NUM_THREADS: | |
8441 | case OMP_CLAUSE_SCHEDULE: | |
8442 | OMP_CLAUSE_OPERAND (nc, 0) | |
015c2c66 MM |
8443 | = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain, |
8444 | in_decl, /*integral_constant_expression_p=*/false); | |
1799e5d5 RH |
8445 | break; |
8446 | case OMP_CLAUSE_NOWAIT: | |
8447 | case OMP_CLAUSE_ORDERED: | |
8448 | case OMP_CLAUSE_DEFAULT: | |
8449 | break; | |
8450 | default: | |
8451 | gcc_unreachable (); | |
8452 | } | |
8453 | } | |
8454 | ||
8455 | return finish_omp_clauses (nreverse (new_clauses)); | |
8456 | } | |
8457 | ||
f74dcfb7 JJ |
8458 | /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */ |
8459 | ||
8460 | static tree | |
8461 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, | |
8462 | tree in_decl) | |
8463 | { | |
8464 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) | |
8465 | ||
8466 | tree purpose, value, chain; | |
8467 | ||
8468 | if (t == NULL) | |
8469 | return t; | |
8470 | ||
8471 | if (TREE_CODE (t) != TREE_LIST) | |
8472 | return tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
8473 | /*function_p=*/false, |
8474 | /*integral_constant_expression_p=*/false); | |
f74dcfb7 JJ |
8475 | |
8476 | if (t == void_list_node) | |
8477 | return t; | |
8478 | ||
8479 | purpose = TREE_PURPOSE (t); | |
8480 | if (purpose) | |
8481 | purpose = RECUR (purpose); | |
8482 | value = TREE_VALUE (t); | |
8483 | if (value) | |
8484 | value = RECUR (value); | |
8485 | chain = TREE_CHAIN (t); | |
8486 | if (chain && chain != void_type_node) | |
8487 | chain = RECUR (chain); | |
8488 | return tree_cons (purpose, value, chain); | |
8489 | #undef RECUR | |
8490 | } | |
8491 | ||
cc23546e JO |
8492 | /* Like tsubst_copy for expressions, etc. but also does semantic |
8493 | processing. */ | |
00d3396f | 8494 | |
14d22dd6 | 8495 | static tree |
015c2c66 MM |
8496 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, |
8497 | bool integral_constant_expression_p) | |
5566b478 | 8498 | { |
015c2c66 MM |
8499 | #define RECUR(NODE) \ |
8500 | tsubst_expr ((NODE), args, complain, in_decl, \ | |
8501 | integral_constant_expression_p) | |
8502 | ||
fd10dd09 | 8503 | tree stmt, tmp; |
558475f0 | 8504 | |
5566b478 MS |
8505 | if (t == NULL_TREE || t == error_mark_node) |
8506 | return t; | |
8507 | ||
93409b8c PB |
8508 | if (EXPR_HAS_LOCATION (t)) |
8509 | input_location = EXPR_LOCATION (t); | |
7c34ced1 RH |
8510 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
8511 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); | |
8512 | ||
5566b478 | 8513 | switch (TREE_CODE (t)) |
8d08fdba | 8514 | { |
325c3691 RH |
8515 | case STATEMENT_LIST: |
8516 | { | |
8517 | tree_stmt_iterator i; | |
8518 | for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i)) | |
015c2c66 | 8519 | RECUR (tsi_stmt (i)); |
325c3691 RH |
8520 | break; |
8521 | } | |
8522 | ||
558475f0 | 8523 | case CTOR_INITIALIZER: |
c8094d83 | 8524 | finish_mem_initializers (tsubst_initializer_list |
2282d28d MM |
8525 | (TREE_OPERAND (t, 0), args)); |
8526 | break; | |
558475f0 | 8527 | |
5088b058 | 8528 | case RETURN_EXPR: |
015c2c66 | 8529 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
5566b478 MS |
8530 | break; |
8531 | ||
8532 | case EXPR_STMT: | |
015c2c66 | 8533 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
7c34ced1 RH |
8534 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
8535 | finish_stmt_expr_expr (tmp, cur_stmt_expr); | |
8536 | else | |
8537 | finish_expr_stmt (tmp); | |
8538 | break; | |
5566b478 | 8539 | |
9da99f7d | 8540 | case USING_STMT: |
015c2c66 | 8541 | do_using_directive (RECUR (USING_STMT_NAMESPACE (t))); |
9da99f7d | 8542 | break; |
c8094d83 | 8543 | |
350fae66 | 8544 | case DECL_EXPR: |
5566b478 | 8545 | { |
e0942dcd MM |
8546 | tree decl; |
8547 | tree init; | |
5566b478 | 8548 | |
350fae66 | 8549 | decl = DECL_EXPR_DECL (t); |
acef433b MM |
8550 | if (TREE_CODE (decl) == LABEL_DECL) |
8551 | finish_label_decl (DECL_NAME (decl)); | |
fbfe8c9e NS |
8552 | else if (TREE_CODE (decl) == USING_DECL) |
8553 | { | |
98ed9dae | 8554 | tree scope = USING_DECL_SCOPE (decl); |
fbfe8c9e | 8555 | tree name = DECL_NAME (decl); |
22038b2c | 8556 | tree decl; |
c8094d83 | 8557 | |
015c2c66 | 8558 | scope = RECUR (scope); |
22038b2c | 8559 | decl = lookup_qualified_name (scope, name, |
12483c9f NS |
8560 | /*is_type_p=*/false, |
8561 | /*complain=*/false); | |
8f78f01f MM |
8562 | if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST) |
8563 | qualified_name_lookup_error (scope, name, decl); | |
22038b2c | 8564 | else |
ed5f054f | 8565 | do_local_using_decl (decl, scope, name); |
fbfe8c9e | 8566 | } |
acef433b MM |
8567 | else |
8568 | { | |
8569 | init = DECL_INITIAL (decl); | |
8570 | decl = tsubst (decl, args, complain, in_decl); | |
ce2e5191 NS |
8571 | if (decl != error_mark_node) |
8572 | { | |
0cbd7506 MS |
8573 | /* By marking the declaration as instantiated, we avoid |
8574 | trying to instantiate it. Since instantiate_decl can't | |
8575 | handle local variables, and since we've already done | |
8576 | all that needs to be done, that's the right thing to | |
8577 | do. */ | |
8578 | if (TREE_CODE (decl) == VAR_DECL) | |
8579 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
edebf865 MM |
8580 | if (TREE_CODE (decl) == VAR_DECL |
8581 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) | |
8582 | /* Anonymous aggregates are a special case. */ | |
8583 | finish_anon_union (decl); | |
c8094d83 | 8584 | else |
ed2fa432 | 8585 | { |
edebf865 | 8586 | maybe_push_decl (decl); |
39703eb9 MM |
8587 | if (TREE_CODE (decl) == VAR_DECL |
8588 | && DECL_PRETTY_FUNCTION_P (decl)) | |
edebf865 MM |
8589 | { |
8590 | /* For __PRETTY_FUNCTION__ we have to adjust the | |
8591 | initializer. */ | |
8592 | const char *const name | |
8593 | = cxx_printable_name (current_function_decl, 2); | |
bb885938 | 8594 | init = cp_fname_init (name, &TREE_TYPE (decl)); |
edebf865 MM |
8595 | } |
8596 | else | |
015c2c66 | 8597 | init = RECUR (init); |
d174af6c | 8598 | finish_decl (decl, init, NULL_TREE); |
ed2fa432 | 8599 | } |
ce2e5191 | 8600 | } |
acef433b | 8601 | } |
fd10dd09 | 8602 | |
350fae66 | 8603 | /* A DECL_EXPR can also be used as an expression, in the condition |
325c3691 RH |
8604 | clause of an if/for/while construct. */ |
8605 | return decl; | |
5566b478 | 8606 | } |
8d08fdba | 8607 | |
5566b478 | 8608 | case FOR_STMT: |
7c34ced1 | 8609 | stmt = begin_for_stmt (); |
015c2c66 | 8610 | RECUR (FOR_INIT_STMT (t)); |
7c34ced1 | 8611 | finish_for_init_stmt (stmt); |
015c2c66 | 8612 | tmp = RECUR (FOR_COND (t)); |
7c34ced1 | 8613 | finish_for_cond (tmp, stmt); |
015c2c66 | 8614 | tmp = RECUR (FOR_EXPR (t)); |
7c34ced1 | 8615 | finish_for_expr (tmp, stmt); |
015c2c66 | 8616 | RECUR (FOR_BODY (t)); |
7c34ced1 | 8617 | finish_for_stmt (stmt); |
5566b478 | 8618 | break; |
8d08fdba | 8619 | |
5566b478 | 8620 | case WHILE_STMT: |
7c34ced1 | 8621 | stmt = begin_while_stmt (); |
015c2c66 | 8622 | tmp = RECUR (WHILE_COND (t)); |
7c34ced1 | 8623 | finish_while_stmt_cond (tmp, stmt); |
015c2c66 | 8624 | RECUR (WHILE_BODY (t)); |
7c34ced1 | 8625 | finish_while_stmt (stmt); |
5566b478 | 8626 | break; |
8d08fdba | 8627 | |
5566b478 | 8628 | case DO_STMT: |
7c34ced1 | 8629 | stmt = begin_do_stmt (); |
015c2c66 | 8630 | RECUR (DO_BODY (t)); |
7c34ced1 | 8631 | finish_do_body (stmt); |
015c2c66 | 8632 | tmp = RECUR (DO_COND (t)); |
7c34ced1 | 8633 | finish_do_stmt (tmp, stmt); |
5566b478 | 8634 | break; |
a0a33927 | 8635 | |
5566b478 | 8636 | case IF_STMT: |
7c34ced1 | 8637 | stmt = begin_if_stmt (); |
015c2c66 | 8638 | tmp = RECUR (IF_COND (t)); |
7c34ced1 | 8639 | finish_if_stmt_cond (tmp, stmt); |
015c2c66 | 8640 | RECUR (THEN_CLAUSE (t)); |
7c34ced1 | 8641 | finish_then_clause (stmt); |
8d08fdba | 8642 | |
7c34ced1 RH |
8643 | if (ELSE_CLAUSE (t)) |
8644 | { | |
8645 | begin_else_clause (stmt); | |
015c2c66 | 8646 | RECUR (ELSE_CLAUSE (t)); |
7c34ced1 RH |
8647 | finish_else_clause (stmt); |
8648 | } | |
8649 | ||
8650 | finish_if_stmt (stmt); | |
5566b478 | 8651 | break; |
8d08fdba | 8652 | |
5882f0f3 | 8653 | case BIND_EXPR: |
7c34ced1 RH |
8654 | if (BIND_EXPR_BODY_BLOCK (t)) |
8655 | stmt = begin_function_body (); | |
8656 | else | |
8657 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) | |
8658 | ? BCS_TRY_BLOCK : 0); | |
ade3dc07 | 8659 | |
015c2c66 | 8660 | RECUR (BIND_EXPR_BODY (t)); |
ade3dc07 | 8661 | |
7c34ced1 RH |
8662 | if (BIND_EXPR_BODY_BLOCK (t)) |
8663 | finish_function_body (stmt); | |
8664 | else | |
8665 | finish_compound_stmt (stmt); | |
5566b478 | 8666 | break; |
8d08fdba | 8667 | |
5566b478 | 8668 | case BREAK_STMT: |
ad321293 | 8669 | finish_break_stmt (); |
5566b478 | 8670 | break; |
8d08fdba | 8671 | |
6467930b | 8672 | case CONTINUE_STMT: |
ad321293 | 8673 | finish_continue_stmt (); |
6467930b MS |
8674 | break; |
8675 | ||
5566b478 | 8676 | case SWITCH_STMT: |
7c34ced1 | 8677 | stmt = begin_switch_stmt (); |
015c2c66 | 8678 | tmp = RECUR (SWITCH_STMT_COND (t)); |
7c34ced1 | 8679 | finish_switch_cond (tmp, stmt); |
015c2c66 | 8680 | RECUR (SWITCH_STMT_BODY (t)); |
7c34ced1 | 8681 | finish_switch_stmt (stmt); |
5566b478 MS |
8682 | break; |
8683 | ||
8c161995 | 8684 | case CASE_LABEL_EXPR: |
015c2c66 MM |
8685 | finish_case_label (RECUR (CASE_LOW (t)), |
8686 | RECUR (CASE_HIGH (t))); | |
5566b478 MS |
8687 | break; |
8688 | ||
9e14e18f | 8689 | case LABEL_EXPR: |
9e14e18f | 8690 | finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t))); |
5566b478 MS |
8691 | break; |
8692 | ||
9e14e18f | 8693 | case GOTO_EXPR: |
fd10dd09 JM |
8694 | tmp = GOTO_DESTINATION (t); |
8695 | if (TREE_CODE (tmp) != LABEL_DECL) | |
aa09da44 MM |
8696 | /* Computed goto's must be tsubst'd into. On the other hand, |
8697 | non-computed gotos must not be; the identifier in question | |
8698 | will have no binding. */ | |
015c2c66 | 8699 | tmp = RECUR (tmp); |
3fa56191 | 8700 | else |
fd10dd09 JM |
8701 | tmp = DECL_NAME (tmp); |
8702 | finish_goto_stmt (tmp); | |
ad321293 MM |
8703 | break; |
8704 | ||
e130a54b | 8705 | case ASM_EXPR: |
c87978aa | 8706 | tmp = finish_asm_stmt |
6de9cd9a | 8707 | (ASM_VOLATILE_P (t), |
015c2c66 | 8708 | RECUR (ASM_STRING (t)), |
f74dcfb7 JJ |
8709 | tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl), |
8710 | tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl), | |
8711 | tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl)); | |
131263fa AP |
8712 | { |
8713 | tree asm_expr = tmp; | |
8714 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) | |
8715 | asm_expr = TREE_OPERAND (asm_expr, 0); | |
8716 | ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t); | |
8717 | } | |
5566b478 | 8718 | break; |
faf5394a MS |
8719 | |
8720 | case TRY_BLOCK: | |
f1dedc31 | 8721 | if (CLEANUP_P (t)) |
62409b39 | 8722 | { |
57b52417 | 8723 | stmt = begin_try_block (); |
015c2c66 | 8724 | RECUR (TRY_STMTS (t)); |
62409b39 | 8725 | finish_cleanup_try_block (stmt); |
015c2c66 | 8726 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
62409b39 | 8727 | } |
f1dedc31 MM |
8728 | else |
8729 | { | |
eaf6fb90 MM |
8730 | tree compound_stmt = NULL_TREE; |
8731 | ||
62409b39 | 8732 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 8733 | stmt = begin_function_try_block (&compound_stmt); |
62409b39 MM |
8734 | else |
8735 | stmt = begin_try_block (); | |
8736 | ||
015c2c66 | 8737 | RECUR (TRY_STMTS (t)); |
62409b39 MM |
8738 | |
8739 | if (FN_TRY_BLOCK_P (t)) | |
8740 | finish_function_try_block (stmt); | |
8741 | else | |
8742 | finish_try_block (stmt); | |
8743 | ||
015c2c66 | 8744 | RECUR (TRY_HANDLERS (t)); |
84df082b | 8745 | if (FN_TRY_BLOCK_P (t)) |
eaf6fb90 | 8746 | finish_function_handler_sequence (stmt, compound_stmt); |
84df082b MM |
8747 | else |
8748 | finish_handler_sequence (stmt); | |
f1dedc31 | 8749 | } |
faf5394a | 8750 | break; |
c8094d83 | 8751 | |
faf5394a | 8752 | case HANDLER: |
b35d4555 | 8753 | { |
3a2419a7 | 8754 | tree decl = HANDLER_PARMS (t); |
b35d4555 | 8755 | |
3a2419a7 | 8756 | if (decl) |
b35d4555 | 8757 | { |
b35d4555 | 8758 | decl = tsubst (decl, args, complain, in_decl); |
f8191e64 MM |
8759 | /* Prevent instantiate_decl from trying to instantiate |
8760 | this variable. We've already done all that needs to be | |
8761 | done. */ | |
3a2419a7 VR |
8762 | if (decl != error_mark_node) |
8763 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; | |
b35d4555 | 8764 | } |
3a2419a7 | 8765 | stmt = begin_handler (); |
1a6025b4 | 8766 | finish_handler_parms (decl, stmt); |
015c2c66 | 8767 | RECUR (HANDLER_BODY (t)); |
1a6025b4 | 8768 | finish_handler (stmt); |
b35d4555 | 8769 | } |
faf5394a MS |
8770 | break; |
8771 | ||
b87692e5 | 8772 | case TAG_DEFN: |
fd10dd09 | 8773 | tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
b87692e5 | 8774 | break; |
55a3debe DG |
8775 | |
8776 | case STATIC_ASSERT: | |
8777 | { | |
8778 | tree condition = | |
8779 | tsubst_expr (STATIC_ASSERT_CONDITION (t), | |
8780 | args, | |
8781 | complain, in_decl, | |
8782 | /*integral_constant_expression_p=*/true); | |
8783 | finish_static_assert (condition, | |
8784 | STATIC_ASSERT_MESSAGE (t), | |
8785 | STATIC_ASSERT_SOURCE_LOCATION (t), | |
8786 | /*member_p=*/false); | |
8787 | } | |
8788 | break; | |
b87692e5 | 8789 | |
1799e5d5 RH |
8790 | case OMP_PARALLEL: |
8791 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), | |
8792 | args, complain, in_decl); | |
8793 | stmt = begin_omp_parallel (); | |
015c2c66 | 8794 | RECUR (OMP_PARALLEL_BODY (t)); |
761041be JJ |
8795 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
8796 | = OMP_PARALLEL_COMBINED (t); | |
1799e5d5 RH |
8797 | break; |
8798 | ||
8799 | case OMP_FOR: | |
8800 | { | |
8801 | tree clauses, decl, init, cond, incr, body, pre_body; | |
8802 | ||
8803 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), | |
8804 | args, complain, in_decl); | |
8805 | init = OMP_FOR_INIT (t); | |
8806 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); | |
015c2c66 MM |
8807 | decl = RECUR (TREE_OPERAND (init, 0)); |
8808 | init = RECUR (TREE_OPERAND (init, 1)); | |
8809 | cond = RECUR (OMP_FOR_COND (t)); | |
8810 | incr = RECUR (OMP_FOR_INCR (t)); | |
1799e5d5 RH |
8811 | |
8812 | stmt = begin_omp_structured_block (); | |
8813 | ||
8814 | pre_body = push_stmt_list (); | |
015c2c66 | 8815 | RECUR (OMP_FOR_PRE_BODY (t)); |
1799e5d5 RH |
8816 | pre_body = pop_stmt_list (pre_body); |
8817 | ||
8818 | body = push_stmt_list (); | |
015c2c66 | 8819 | RECUR (OMP_FOR_BODY (t)); |
1799e5d5 RH |
8820 | body = pop_stmt_list (body); |
8821 | ||
8822 | t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body, | |
8823 | pre_body); | |
8824 | if (t) | |
8825 | OMP_FOR_CLAUSES (t) = clauses; | |
8826 | ||
8827 | add_stmt (finish_omp_structured_block (stmt)); | |
8828 | } | |
8829 | break; | |
8830 | ||
8831 | case OMP_SECTIONS: | |
8832 | case OMP_SINGLE: | |
8833 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl); | |
8834 | stmt = push_stmt_list (); | |
015c2c66 | 8835 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
8836 | stmt = pop_stmt_list (stmt); |
8837 | ||
8838 | t = copy_node (t); | |
8839 | OMP_BODY (t) = stmt; | |
8840 | OMP_CLAUSES (t) = tmp; | |
8841 | add_stmt (t); | |
8842 | break; | |
8843 | ||
8844 | case OMP_SECTION: | |
8845 | case OMP_CRITICAL: | |
8846 | case OMP_MASTER: | |
8847 | case OMP_ORDERED: | |
8848 | stmt = push_stmt_list (); | |
015c2c66 | 8849 | RECUR (OMP_BODY (t)); |
1799e5d5 RH |
8850 | stmt = pop_stmt_list (stmt); |
8851 | ||
8852 | t = copy_node (t); | |
8853 | OMP_BODY (t) = stmt; | |
8854 | add_stmt (t); | |
8855 | break; | |
8856 | ||
8857 | case OMP_ATOMIC: | |
8858 | { | |
8859 | tree op0, op1; | |
015c2c66 MM |
8860 | op0 = RECUR (TREE_OPERAND (t, 0)); |
8861 | op1 = RECUR (TREE_OPERAND (t, 1)); | |
fe89d797 | 8862 | finish_omp_atomic (OMP_ATOMIC_CODE (t), op0, op1); |
1799e5d5 RH |
8863 | } |
8864 | break; | |
8865 | ||
5566b478 | 8866 | default: |
315fb5db | 8867 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
c8094d83 | 8868 | |
315fb5db | 8869 | return tsubst_copy_and_build (t, args, complain, in_decl, |
015c2c66 MM |
8870 | /*function_p=*/false, |
8871 | integral_constant_expression_p); | |
5566b478 | 8872 | } |
fd10dd09 | 8873 | |
325c3691 | 8874 | return NULL_TREE; |
015c2c66 | 8875 | #undef RECUR |
8d08fdba MS |
8876 | } |
8877 | ||
b3445994 MM |
8878 | /* T is a postfix-expression that is not being used in a function |
8879 | call. Return the substituted version of T. */ | |
8880 | ||
8881 | static tree | |
c8094d83 | 8882 | tsubst_non_call_postfix_expression (tree t, tree args, |
b3445994 MM |
8883 | tsubst_flags_t complain, |
8884 | tree in_decl) | |
8885 | { | |
8886 | if (TREE_CODE (t) == SCOPE_REF) | |
8887 | t = tsubst_qualified_id (t, args, complain, in_decl, | |
8888 | /*done=*/false, /*address_p=*/false); | |
8889 | else | |
8890 | t = tsubst_copy_and_build (t, args, complain, in_decl, | |
015c2c66 MM |
8891 | /*function_p=*/false, |
8892 | /*integral_constant_expression_p=*/false); | |
b3445994 MM |
8893 | |
8894 | return t; | |
8895 | } | |
8896 | ||
cc23546e | 8897 | /* Like tsubst but deals with expressions and performs semantic |
b3445994 | 8898 | analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */ |
cc23546e | 8899 | |
ee76b931 | 8900 | tree |
c8094d83 | 8901 | tsubst_copy_and_build (tree t, |
0cbd7506 MS |
8902 | tree args, |
8903 | tsubst_flags_t complain, | |
8904 | tree in_decl, | |
015c2c66 MM |
8905 | bool function_p, |
8906 | bool integral_constant_expression_p) | |
cc23546e | 8907 | { |
015c2c66 MM |
8908 | #define RECUR(NODE) \ |
8909 | tsubst_copy_and_build (NODE, args, complain, in_decl, \ | |
8910 | /*function_p=*/false, \ | |
8911 | integral_constant_expression_p) | |
b3445994 | 8912 | |
ee76b931 MM |
8913 | tree op1; |
8914 | ||
cc23546e JO |
8915 | if (t == NULL_TREE || t == error_mark_node) |
8916 | return t; | |
8917 | ||
8918 | switch (TREE_CODE (t)) | |
8919 | { | |
399dedb9 NS |
8920 | case USING_DECL: |
8921 | t = DECL_NAME (t); | |
852dcbdd | 8922 | /* Fall through. */ |
b3445994 | 8923 | case IDENTIFIER_NODE: |
cc23546e | 8924 | { |
b3445994 | 8925 | tree decl; |
b3445994 | 8926 | cp_id_kind idk; |
67c03833 | 8927 | bool non_integral_constant_expression_p; |
b3445994 MM |
8928 | const char *error_msg; |
8929 | ||
b3445994 | 8930 | if (IDENTIFIER_TYPENAME_P (t)) |
cc23546e | 8931 | { |
b3445994 MM |
8932 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
8933 | t = mangle_conv_op_name_for_type (new_type); | |
cc23546e | 8934 | } |
b3445994 MM |
8935 | |
8936 | /* Look up the name. */ | |
10e6657a | 8937 | decl = lookup_name (t); |
b3445994 MM |
8938 | |
8939 | /* By convention, expressions use ERROR_MARK_NODE to indicate | |
8940 | failure, not NULL_TREE. */ | |
8941 | if (decl == NULL_TREE) | |
8942 | decl = error_mark_node; | |
8943 | ||
10b1d5e7 | 8944 | decl = finish_id_expression (t, decl, NULL_TREE, |
b3445994 | 8945 | &idk, |
015c2c66 | 8946 | integral_constant_expression_p, |
67c03833 JM |
8947 | /*allow_non_integral_constant_expression_p=*/false, |
8948 | &non_integral_constant_expression_p, | |
02ed62dd MM |
8949 | /*template_p=*/false, |
8950 | /*done=*/true, | |
8951 | /*address_p=*/false, | |
8952 | /*template_arg_p=*/false, | |
b3445994 MM |
8953 | &error_msg); |
8954 | if (error_msg) | |
8955 | error (error_msg); | |
8956 | if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE) | |
8957 | decl = unqualified_name_lookup_error (decl); | |
8958 | return decl; | |
cc23546e JO |
8959 | } |
8960 | ||
8961 | case TEMPLATE_ID_EXPR: | |
8962 | { | |
8963 | tree object; | |
b3445994 | 8964 | tree template = RECUR (TREE_OPERAND (t, 0)); |
bf12d54d NS |
8965 | tree targs = TREE_OPERAND (t, 1); |
8966 | ||
8967 | if (targs) | |
8968 | targs = tsubst_template_args (targs, args, complain, in_decl); | |
c8094d83 | 8969 | |
cc23546e JO |
8970 | if (TREE_CODE (template) == COMPONENT_REF) |
8971 | { | |
8972 | object = TREE_OPERAND (template, 0); | |
8973 | template = TREE_OPERAND (template, 1); | |
8974 | } | |
8975 | else | |
8976 | object = NULL_TREE; | |
bd83b409 | 8977 | template = lookup_template_function (template, targs); |
c8094d83 | 8978 | |
cc23546e | 8979 | if (object) |
c8094d83 | 8980 | return build3 (COMPONENT_REF, TREE_TYPE (template), |
f293ce4b | 8981 | object, template, NULL_TREE); |
cc23546e | 8982 | else |
eff3a276 | 8983 | return baselink_for_fns (template); |
cc23546e JO |
8984 | } |
8985 | ||
8986 | case INDIRECT_REF: | |
db24eb1f NS |
8987 | { |
8988 | tree r = RECUR (TREE_OPERAND (t, 0)); | |
8989 | ||
8990 | if (REFERENCE_REF_P (t)) | |
8991 | { | |
e8c66fe0 NS |
8992 | /* A type conversion to reference type will be enclosed in |
8993 | such an indirect ref, but the substitution of the cast | |
8994 | will have also added such an indirect ref. */ | |
8995 | if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE) | |
8996 | r = convert_from_reference (r); | |
db24eb1f NS |
8997 | } |
8998 | else | |
8999 | r = build_x_indirect_ref (r, "unary *"); | |
9000 | return r; | |
9001 | } | |
cc23546e | 9002 | |
0da99d4e GB |
9003 | case NOP_EXPR: |
9004 | return build_nop | |
9005 | (tsubst (TREE_TYPE (t), args, complain, in_decl), | |
9006 | RECUR (TREE_OPERAND (t, 0))); | |
9007 | ||
cc23546e | 9008 | case CAST_EXPR: |
cc23546e | 9009 | case REINTERPRET_CAST_EXPR: |
cc23546e | 9010 | case CONST_CAST_EXPR: |
cc23546e | 9011 | case DYNAMIC_CAST_EXPR: |
cc23546e | 9012 | case STATIC_CAST_EXPR: |
015c2c66 MM |
9013 | { |
9014 | tree type; | |
9015 | tree op; | |
9016 | ||
9017 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); | |
9018 | if (integral_constant_expression_p | |
9019 | && !cast_valid_in_integral_constant_expression_p (type)) | |
9020 | { | |
9021 | error ("a cast to a type other than an integral or " | |
9022 | "enumeration type cannot appear in a constant-expression"); | |
9023 | return error_mark_node; | |
9024 | } | |
9025 | ||
9026 | op = RECUR (TREE_OPERAND (t, 0)); | |
9027 | ||
9028 | switch (TREE_CODE (t)) | |
9029 | { | |
9030 | case CAST_EXPR: | |
9031 | return build_functional_cast (type, op); | |
9032 | case REINTERPRET_CAST_EXPR: | |
9033 | return build_reinterpret_cast (type, op); | |
9034 | case CONST_CAST_EXPR: | |
9035 | return build_const_cast (type, op); | |
9036 | case DYNAMIC_CAST_EXPR: | |
9037 | return build_dynamic_cast (type, op); | |
9038 | case STATIC_CAST_EXPR: | |
9039 | return build_static_cast (type, op); | |
9040 | default: | |
9041 | gcc_unreachable (); | |
9042 | } | |
9043 | } | |
cc23546e | 9044 | |
cc23546e JO |
9045 | case POSTDECREMENT_EXPR: |
9046 | case POSTINCREMENT_EXPR: | |
b3445994 MM |
9047 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
9048 | args, complain, in_decl); | |
ee76b931 MM |
9049 | return build_x_unary_op (TREE_CODE (t), op1); |
9050 | ||
9051 | case PREDECREMENT_EXPR: | |
9052 | case PREINCREMENT_EXPR: | |
cc23546e JO |
9053 | case NEGATE_EXPR: |
9054 | case BIT_NOT_EXPR: | |
cc23546e | 9055 | case ABS_EXPR: |
d17811fd | 9056 | case TRUTH_NOT_EXPR: |
392e3d51 | 9057 | case UNARY_PLUS_EXPR: /* Unary + */ |
d17811fd MM |
9058 | case REALPART_EXPR: |
9059 | case IMAGPART_EXPR: | |
b3445994 | 9060 | return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0))); |
cc23546e | 9061 | |
cc23546e | 9062 | case ADDR_EXPR: |
ee76b931 MM |
9063 | op1 = TREE_OPERAND (t, 0); |
9064 | if (TREE_CODE (op1) == SCOPE_REF) | |
c8094d83 | 9065 | op1 = tsubst_qualified_id (op1, args, complain, in_decl, |
ee76b931 MM |
9066 | /*done=*/true, /*address_p=*/true); |
9067 | else | |
c8094d83 | 9068 | op1 = tsubst_non_call_postfix_expression (op1, args, complain, |
b3445994 | 9069 | in_decl); |
fc2b8477 MM |
9070 | if (TREE_CODE (op1) == LABEL_DECL) |
9071 | return finish_label_address_expr (DECL_NAME (op1)); | |
ee76b931 MM |
9072 | return build_x_unary_op (ADDR_EXPR, op1); |
9073 | ||
cc23546e JO |
9074 | case PLUS_EXPR: |
9075 | case MINUS_EXPR: | |
9076 | case MULT_EXPR: | |
9077 | case TRUNC_DIV_EXPR: | |
9078 | case CEIL_DIV_EXPR: | |
9079 | case FLOOR_DIV_EXPR: | |
9080 | case ROUND_DIV_EXPR: | |
9081 | case EXACT_DIV_EXPR: | |
9082 | case BIT_AND_EXPR: | |
cc23546e JO |
9083 | case BIT_IOR_EXPR: |
9084 | case BIT_XOR_EXPR: | |
9085 | case TRUNC_MOD_EXPR: | |
9086 | case FLOOR_MOD_EXPR: | |
9087 | case TRUTH_ANDIF_EXPR: | |
9088 | case TRUTH_ORIF_EXPR: | |
9089 | case TRUTH_AND_EXPR: | |
9090 | case TRUTH_OR_EXPR: | |
9091 | case RSHIFT_EXPR: | |
9092 | case LSHIFT_EXPR: | |
9093 | case RROTATE_EXPR: | |
9094 | case LROTATE_EXPR: | |
9095 | case EQ_EXPR: | |
9096 | case NE_EXPR: | |
9097 | case MAX_EXPR: | |
9098 | case MIN_EXPR: | |
9099 | case LE_EXPR: | |
9100 | case GE_EXPR: | |
9101 | case LT_EXPR: | |
9102 | case GT_EXPR: | |
9103 | case MEMBER_REF: | |
b3445994 | 9104 | case DOTSTAR_EXPR: |
cc23546e | 9105 | return build_x_binary_op |
c8094d83 | 9106 | (TREE_CODE (t), |
b3445994 | 9107 | RECUR (TREE_OPERAND (t, 0)), |
2a67bec2 ILT |
9108 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) |
9109 | ? ERROR_MARK | |
9110 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
ec835fb2 | 9111 | RECUR (TREE_OPERAND (t, 1)), |
2a67bec2 ILT |
9112 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) |
9113 | ? ERROR_MARK | |
9114 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
ec835fb2 | 9115 | /*overloaded_p=*/NULL); |
cc23546e JO |
9116 | |
9117 | case SCOPE_REF: | |
ee76b931 MM |
9118 | return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
9119 | /*address_p=*/false); | |
cc23546e | 9120 | case ARRAY_REF: |
b3445994 MM |
9121 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
9122 | args, complain, in_decl); | |
2a67bec2 ILT |
9123 | return build_x_binary_op (ARRAY_REF, op1, |
9124 | (TREE_NO_WARNING (TREE_OPERAND (t, 0)) | |
9125 | ? ERROR_MARK | |
9126 | : TREE_CODE (TREE_OPERAND (t, 0))), | |
9127 | RECUR (TREE_OPERAND (t, 1)), | |
9128 | (TREE_NO_WARNING (TREE_OPERAND (t, 1)) | |
9129 | ? ERROR_MARK | |
9130 | : TREE_CODE (TREE_OPERAND (t, 1))), | |
d8987adb | 9131 | /*overloaded_p=*/NULL); |
c8094d83 | 9132 | |
cc23546e JO |
9133 | case SIZEOF_EXPR: |
9134 | case ALIGNOF_EXPR: | |
d17811fd MM |
9135 | op1 = TREE_OPERAND (t, 0); |
9136 | if (!args) | |
9137 | { | |
9138 | /* When there are no ARGS, we are trying to evaluate a | |
9139 | non-dependent expression from the parser. Trying to do | |
9140 | the substitutions may not work. */ | |
9141 | if (!TYPE_P (op1)) | |
9142 | op1 = TREE_TYPE (op1); | |
9143 | } | |
9144 | else | |
9145 | { | |
9146 | ++skip_evaluation; | |
015c2c66 MM |
9147 | op1 = tsubst_copy_and_build (op1, args, complain, in_decl, |
9148 | /*function_p=*/false, | |
9149 | /*integral_constant_expression_p=*/false); | |
d17811fd MM |
9150 | --skip_evaluation; |
9151 | } | |
7a18b933 NS |
9152 | if (TYPE_P (op1)) |
9153 | return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true); | |
d17811fd | 9154 | else |
7a18b933 | 9155 | return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t)); |
cc23546e JO |
9156 | |
9157 | case MODOP_EXPR: | |
e4c2c34b JM |
9158 | { |
9159 | tree r = build_x_modify_expr | |
9160 | (RECUR (TREE_OPERAND (t, 0)), | |
9161 | TREE_CODE (TREE_OPERAND (t, 1)), | |
9162 | RECUR (TREE_OPERAND (t, 2))); | |
bcf9a914 JM |
9163 | /* TREE_NO_WARNING must be set if either the expression was |
9164 | parenthesized or it uses an operator such as >>= rather | |
9165 | than plain assignment. In the former case, it was already | |
9166 | set and must be copied. In the latter case, | |
9167 | build_x_modify_expr sets it and it must not be reset | |
9168 | here. */ | |
9169 | if (TREE_NO_WARNING (t)) | |
9170 | TREE_NO_WARNING (r) = TREE_NO_WARNING (t); | |
e4c2c34b JM |
9171 | return r; |
9172 | } | |
cc23546e JO |
9173 | |
9174 | case ARROW_EXPR: | |
b3445994 MM |
9175 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
9176 | args, complain, in_decl); | |
ee76b931 MM |
9177 | /* Remember that there was a reference to this entity. */ |
9178 | if (DECL_P (op1)) | |
9179 | mark_used (op1); | |
9180 | return build_x_arrow (op1); | |
cc23546e JO |
9181 | |
9182 | case NEW_EXPR: | |
9183 | return build_new | |
b3445994 MM |
9184 | (RECUR (TREE_OPERAND (t, 0)), |
9185 | RECUR (TREE_OPERAND (t, 1)), | |
9186 | RECUR (TREE_OPERAND (t, 2)), | |
058b15c1 | 9187 | RECUR (TREE_OPERAND (t, 3)), |
cc23546e JO |
9188 | NEW_EXPR_USE_GLOBAL (t)); |
9189 | ||
9190 | case DELETE_EXPR: | |
9191 | return delete_sanity | |
b3445994 MM |
9192 | (RECUR (TREE_OPERAND (t, 0)), |
9193 | RECUR (TREE_OPERAND (t, 1)), | |
cc23546e JO |
9194 | DELETE_EXPR_USE_VEC (t), |
9195 | DELETE_EXPR_USE_GLOBAL (t)); | |
9196 | ||
9197 | case COMPOUND_EXPR: | |
b3445994 MM |
9198 | return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)), |
9199 | RECUR (TREE_OPERAND (t, 1))); | |
cc23546e | 9200 | |
ee76b931 | 9201 | case CALL_EXPR: |
cc23546e | 9202 | { |
ee76b931 MM |
9203 | tree function; |
9204 | tree call_args; | |
ee76b931 | 9205 | bool qualified_p; |
ee935db4 | 9206 | bool koenig_p; |
ee76b931 MM |
9207 | |
9208 | function = TREE_OPERAND (t, 0); | |
6d80c4b9 MM |
9209 | /* When we parsed the expression, we determined whether or |
9210 | not Koenig lookup should be performed. */ | |
9211 | koenig_p = KOENIG_LOOKUP_P (t); | |
ee76b931 | 9212 | if (TREE_CODE (function) == SCOPE_REF) |
cc23546e | 9213 | { |
ee76b931 MM |
9214 | qualified_p = true; |
9215 | function = tsubst_qualified_id (function, args, complain, in_decl, | |
c8094d83 | 9216 | /*done=*/false, |
ee76b931 | 9217 | /*address_p=*/false); |
cc23546e | 9218 | } |
ee76b931 | 9219 | else |
cc23546e | 9220 | { |
61e71a9e NS |
9221 | if (TREE_CODE (function) == COMPONENT_REF) |
9222 | { | |
9223 | tree op = TREE_OPERAND (function, 1); | |
9224 | ||
9225 | qualified_p = (TREE_CODE (op) == SCOPE_REF | |
9226 | || (BASELINK_P (op) | |
9227 | && BASELINK_QUALIFIED_P (op))); | |
9228 | } | |
9229 | else | |
9230 | qualified_p = false; | |
3db45ab5 | 9231 | |
c8094d83 | 9232 | function = tsubst_copy_and_build (function, args, complain, |
b3445994 | 9233 | in_decl, |
015c2c66 MM |
9234 | !qualified_p, |
9235 | integral_constant_expression_p); | |
9236 | ||
6d80c4b9 MM |
9237 | if (BASELINK_P (function)) |
9238 | qualified_p = true; | |
cc23546e | 9239 | } |
cc23546e | 9240 | |
b3445994 | 9241 | call_args = RECUR (TREE_OPERAND (t, 1)); |
676e33ca MM |
9242 | |
9243 | /* We do not perform argument-dependent lookup if normal | |
9244 | lookup finds a non-function, in accordance with the | |
9245 | expected resolution of DR 218. */ | |
ee935db4 | 9246 | if (koenig_p |
44370687 MM |
9247 | && ((is_overloaded_fn (function) |
9248 | /* If lookup found a member function, the Koenig lookup is | |
9249 | not appropriate, even if an unqualified-name was used | |
9250 | to denote the function. */ | |
9251 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) | |
b3445994 | 9252 | || TREE_CODE (function) == IDENTIFIER_NODE)) |
6d80c4b9 MM |
9253 | function = perform_koenig_lookup (function, call_args); |
9254 | ||
9255 | if (TREE_CODE (function) == IDENTIFIER_NODE) | |
b3445994 | 9256 | { |
6d80c4b9 MM |
9257 | unqualified_name_lookup_error (function); |
9258 | return error_mark_node; | |
b3445994 MM |
9259 | } |
9260 | ||
9261 | /* Remember that there was a reference to this entity. */ | |
9262 | if (DECL_P (function)) | |
9263 | mark_used (function); | |
9264 | ||
d17811fd MM |
9265 | if (TREE_CODE (function) == OFFSET_REF) |
9266 | return build_offset_ref_call_from_tree (function, call_args); | |
9267 | if (TREE_CODE (function) == COMPONENT_REF) | |
9f880ef9 MM |
9268 | { |
9269 | if (!BASELINK_P (TREE_OPERAND (function, 1))) | |
9270 | return finish_call_expr (function, call_args, | |
9271 | /*disallow_virtual=*/false, | |
9272 | /*koenig_p=*/false); | |
9273 | else | |
c8094d83 | 9274 | return (build_new_method_call |
9f880ef9 MM |
9275 | (TREE_OPERAND (function, 0), |
9276 | TREE_OPERAND (function, 1), | |
c8094d83 | 9277 | call_args, NULL_TREE, |
63c9a190 MM |
9278 | qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL, |
9279 | /*fn_p=*/NULL)); | |
9f880ef9 | 9280 | } |
c8094d83 | 9281 | return finish_call_expr (function, call_args, |
6d80c4b9 MM |
9282 | /*disallow_virtual=*/qualified_p, |
9283 | koenig_p); | |
cc23546e JO |
9284 | } |
9285 | ||
9286 | case COND_EXPR: | |
9287 | return build_x_conditional_expr | |
b3445994 MM |
9288 | (RECUR (TREE_OPERAND (t, 0)), |
9289 | RECUR (TREE_OPERAND (t, 1)), | |
9290 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
9291 | |
9292 | case PSEUDO_DTOR_EXPR: | |
c8094d83 | 9293 | return finish_pseudo_destructor_expr |
b3445994 MM |
9294 | (RECUR (TREE_OPERAND (t, 0)), |
9295 | RECUR (TREE_OPERAND (t, 1)), | |
9296 | RECUR (TREE_OPERAND (t, 2))); | |
cc23546e JO |
9297 | |
9298 | case TREE_LIST: | |
9299 | { | |
9300 | tree purpose, value, chain; | |
9301 | ||
9302 | if (t == void_list_node) | |
9303 | return t; | |
9304 | ||
9305 | purpose = TREE_PURPOSE (t); | |
9306 | if (purpose) | |
b3445994 | 9307 | purpose = RECUR (purpose); |
cc23546e JO |
9308 | value = TREE_VALUE (t); |
9309 | if (value) | |
b3445994 | 9310 | value = RECUR (value); |
cc23546e JO |
9311 | chain = TREE_CHAIN (t); |
9312 | if (chain && chain != void_type_node) | |
b3445994 | 9313 | chain = RECUR (chain); |
cc23546e JO |
9314 | if (purpose == TREE_PURPOSE (t) |
9315 | && value == TREE_VALUE (t) | |
9316 | && chain == TREE_CHAIN (t)) | |
9317 | return t; | |
9318 | return tree_cons (purpose, value, chain); | |
9319 | } | |
9320 | ||
9321 | case COMPONENT_REF: | |
9322 | { | |
ee76b931 | 9323 | tree object; |
3897c0aa | 9324 | tree object_type; |
ee76b931 MM |
9325 | tree member; |
9326 | ||
b3445994 MM |
9327 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
9328 | args, complain, in_decl); | |
ee76b931 MM |
9329 | /* Remember that there was a reference to this entity. */ |
9330 | if (DECL_P (object)) | |
9331 | mark_used (object); | |
3897c0aa | 9332 | object_type = TREE_TYPE (object); |
ee76b931 MM |
9333 | |
9334 | member = TREE_OPERAND (t, 1); | |
9335 | if (BASELINK_P (member)) | |
c8094d83 | 9336 | member = tsubst_baselink (member, |
ee76b931 MM |
9337 | non_reference (TREE_TYPE (object)), |
9338 | args, complain, in_decl); | |
9339 | else | |
9340 | member = tsubst_copy (member, args, complain, in_decl); | |
bad1f462 KL |
9341 | if (member == error_mark_node) |
9342 | return error_mark_node; | |
3897c0aa MM |
9343 | |
9344 | if (object_type && !CLASS_TYPE_P (object_type)) | |
cc23546e JO |
9345 | { |
9346 | if (TREE_CODE (member) == BIT_NOT_EXPR) | |
c8094d83 | 9347 | return finish_pseudo_destructor_expr (object, |
cc23546e | 9348 | NULL_TREE, |
3897c0aa | 9349 | object_type); |
cc23546e JO |
9350 | else if (TREE_CODE (member) == SCOPE_REF |
9351 | && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR)) | |
c8094d83 | 9352 | return finish_pseudo_destructor_expr (object, |
cc23546e | 9353 | object, |
3897c0aa | 9354 | object_type); |
cc23546e JO |
9355 | } |
9356 | else if (TREE_CODE (member) == SCOPE_REF | |
9357 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
9358 | { | |
9359 | tree tmpl; | |
9360 | tree args; | |
c8094d83 | 9361 | |
cc23546e JO |
9362 | /* Lookup the template functions now that we know what the |
9363 | scope is. */ | |
9364 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
9365 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
c8094d83 | 9366 | member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl, |
12483c9f NS |
9367 | /*is_type_p=*/false, |
9368 | /*complain=*/false); | |
cc23546e | 9369 | if (BASELINK_P (member)) |
44370687 | 9370 | { |
c8094d83 | 9371 | BASELINK_FUNCTIONS (member) |
44370687 MM |
9372 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
9373 | args); | |
c8094d83 MS |
9374 | member = (adjust_result_of_qualified_name_lookup |
9375 | (member, BINFO_TYPE (BASELINK_BINFO (member)), | |
3897c0aa | 9376 | object_type)); |
44370687 | 9377 | } |
cc23546e JO |
9378 | else |
9379 | { | |
3897c0aa | 9380 | qualified_name_lookup_error (object_type, tmpl, member); |
cc23546e JO |
9381 | return error_mark_node; |
9382 | } | |
9383 | } | |
2436b51f MM |
9384 | else if (TREE_CODE (member) == SCOPE_REF |
9385 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) | |
9386 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) | |
9387 | { | |
9388 | if (complain & tf_error) | |
9389 | { | |
9390 | if (TYPE_P (TREE_OPERAND (member, 0))) | |
c8094d83 | 9391 | error ("%qT is not a class or namespace", |
2436b51f MM |
9392 | TREE_OPERAND (member, 0)); |
9393 | else | |
c8094d83 | 9394 | error ("%qD is not a class or namespace", |
2436b51f MM |
9395 | TREE_OPERAND (member, 0)); |
9396 | } | |
9397 | return error_mark_node; | |
9398 | } | |
a3f10e50 NS |
9399 | else if (TREE_CODE (member) == FIELD_DECL) |
9400 | return finish_non_static_data_member (member, object, NULL_TREE); | |
cc23546e | 9401 | |
02ed62dd MM |
9402 | return finish_class_member_access_expr (object, member, |
9403 | /*template_p=*/false); | |
cc23546e JO |
9404 | } |
9405 | ||
9406 | case THROW_EXPR: | |
9407 | return build_throw | |
b3445994 | 9408 | (RECUR (TREE_OPERAND (t, 0))); |
cc23546e JO |
9409 | |
9410 | case CONSTRUCTOR: | |
9411 | { | |
4038c495 GB |
9412 | VEC(constructor_elt,gc) *n; |
9413 | constructor_elt *ce; | |
9414 | unsigned HOST_WIDE_INT idx; | |
cc23546e | 9415 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
4038c495 | 9416 | bool process_index_p; |
cc23546e | 9417 | |
a97728cf VR |
9418 | if (type == error_mark_node) |
9419 | return error_mark_node; | |
9420 | ||
cc23546e JO |
9421 | /* digest_init will do the wrong thing if we let it. */ |
9422 | if (type && TYPE_PTRMEMFUNC_P (type)) | |
9423 | return t; | |
9424 | ||
4038c495 | 9425 | /* We do not want to process the index of aggregate |
cc23546e JO |
9426 | initializers as they are identifier nodes which will be |
9427 | looked up by digest_init. */ | |
4038c495 | 9428 | process_index_p = !(type && IS_AGGR_TYPE (type)); |
c8094d83 | 9429 | |
4038c495 GB |
9430 | n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t)); |
9431 | for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++) | |
9432 | { | |
9433 | if (ce->index && process_index_p) | |
9434 | ce->index = RECUR (ce->index); | |
9435 | ce->value = RECUR (ce->value); | |
cc23546e | 9436 | } |
c8094d83 | 9437 | |
05b22df9 MM |
9438 | if (TREE_HAS_CONSTRUCTOR (t)) |
9439 | return finish_compound_literal (type, n); | |
cc23546e | 9440 | |
05b22df9 | 9441 | return build_constructor (NULL_TREE, n); |
cc23546e JO |
9442 | } |
9443 | ||
9444 | case TYPEID_EXPR: | |
9445 | { | |
b3445994 | 9446 | tree operand_0 = RECUR (TREE_OPERAND (t, 0)); |
cc23546e JO |
9447 | if (TYPE_P (operand_0)) |
9448 | return get_typeid (operand_0); | |
9449 | return build_typeid (operand_0); | |
9450 | } | |
9451 | ||
cc23546e | 9452 | case VAR_DECL: |
db24eb1f NS |
9453 | if (!args) |
9454 | return t; | |
9455 | /* Fall through */ | |
c8094d83 | 9456 | |
db24eb1f NS |
9457 | case PARM_DECL: |
9458 | { | |
9459 | tree r = tsubst_copy (t, args, complain, in_decl); | |
c8094d83 | 9460 | |
db24eb1f NS |
9461 | if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE) |
9462 | /* If the original type was a reference, we'll be wrapped in | |
9463 | the appropriate INDIRECT_REF. */ | |
9464 | r = convert_from_reference (r); | |
9465 | return r; | |
9466 | } | |
cc23546e JO |
9467 | |
9468 | case VA_ARG_EXPR: | |
b3445994 | 9469 | return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)), |
c8094d83 | 9470 | tsubst_copy (TREE_TYPE (t), args, complain, |
b3445994 | 9471 | in_decl)); |
cc23546e | 9472 | |
4bceb077 | 9473 | case OFFSETOF_EXPR: |
c291f8b1 | 9474 | return finish_offsetof (RECUR (TREE_OPERAND (t, 0))); |
4bceb077 | 9475 | |
0fe0caa6 RH |
9476 | case STMT_EXPR: |
9477 | { | |
9478 | tree old_stmt_expr = cur_stmt_expr; | |
9479 | tree stmt_expr = begin_stmt_expr (); | |
9480 | ||
9481 | cur_stmt_expr = stmt_expr; | |
015c2c66 MM |
9482 | tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl, |
9483 | integral_constant_expression_p); | |
0fe0caa6 RH |
9484 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
9485 | cur_stmt_expr = old_stmt_expr; | |
9486 | ||
9487 | return stmt_expr; | |
9488 | } | |
9489 | ||
a5201a91 MM |
9490 | case CONST_DECL: |
9491 | t = tsubst_copy (t, args, complain, in_decl); | |
9492 | /* As in finish_id_expression, we resolve enumeration constants | |
9493 | to their underlying values. */ | |
9494 | if (TREE_CODE (t) == CONST_DECL) | |
6193b8b7 DJ |
9495 | { |
9496 | used_types_insert (TREE_TYPE (t)); | |
9497 | return DECL_INITIAL (t); | |
9498 | } | |
a5201a91 MM |
9499 | return t; |
9500 | ||
cc23546e | 9501 | default: |
e58a9aa1 ZL |
9502 | /* Handle Objective-C++ constructs, if appropriate. */ |
9503 | { | |
9504 | tree subst | |
9505 | = objcp_tsubst_copy_and_build (t, args, complain, | |
9506 | in_decl, /*function_p=*/false); | |
9507 | if (subst) | |
9508 | return subst; | |
9509 | } | |
cc23546e JO |
9510 | return tsubst_copy (t, args, complain, in_decl); |
9511 | } | |
b3445994 MM |
9512 | |
9513 | #undef RECUR | |
cc23546e JO |
9514 | } |
9515 | ||
3e4a3562 NS |
9516 | /* Verify that the instantiated ARGS are valid. For type arguments, |
9517 | make sure that the type's linkage is ok. For non-type arguments, | |
34cd5ae7 | 9518 | make sure they are constants if they are integral or enumerations. |
9bcb9aae | 9519 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
3e4a3562 NS |
9520 | |
9521 | static bool | |
3a978d72 | 9522 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
3e4a3562 NS |
9523 | { |
9524 | int ix, len = DECL_NTPARMS (tmpl); | |
9525 | bool result = false; | |
9526 | ||
9527 | for (ix = 0; ix != len; ix++) | |
9528 | { | |
9529 | tree t = TREE_VEC_ELT (args, ix); | |
c8094d83 | 9530 | |
3e4a3562 NS |
9531 | if (TYPE_P (t)) |
9532 | { | |
9533 | /* [basic.link]: A name with no linkage (notably, the name | |
9534 | of a class or enumeration declared in a local scope) | |
9535 | shall not be used to declare an entity with linkage. | |
9536 | This implies that names with no linkage cannot be used as | |
9537 | template arguments. */ | |
4684cd27 | 9538 | tree nt = no_linkage_check (t, /*relaxed_p=*/false); |
3e4a3562 NS |
9539 | |
9540 | if (nt) | |
9541 | { | |
cb807ba3 | 9542 | /* DR 488 makes use of a type with no linkage cause |
3db45ab5 | 9543 | type deduction to fail. */ |
2010cdcd MM |
9544 | if (complain & tf_error) |
9545 | { | |
9546 | if (TYPE_ANONYMOUS_P (nt)) | |
9547 | error ("%qT is/uses anonymous type", t); | |
9548 | else | |
9df540a8 | 9549 | error ("template argument for %qD uses local type %qT", |
3db45ab5 | 9550 | tmpl, t); |
2010cdcd | 9551 | } |
3e4a3562 NS |
9552 | result = true; |
9553 | } | |
9554 | /* In order to avoid all sorts of complications, we do not | |
9555 | allow variably-modified types as template arguments. */ | |
5377d5ba | 9556 | else if (variably_modified_type_p (t, NULL_TREE)) |
3e4a3562 NS |
9557 | { |
9558 | if (complain & tf_error) | |
0f51ccfc | 9559 | error ("%qT is a variably modified type", t); |
3e4a3562 NS |
9560 | result = true; |
9561 | } | |
9562 | } | |
9563 | /* A non-type argument of integral or enumerated type must be a | |
9564 | constant. */ | |
9565 | else if (TREE_TYPE (t) | |
9566 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) | |
9567 | && !TREE_CONSTANT (t)) | |
9568 | { | |
9569 | if (complain & tf_error) | |
0f51ccfc | 9570 | error ("integral expression %qE is not constant", t); |
3e4a3562 NS |
9571 | result = true; |
9572 | } | |
9573 | } | |
2010cdcd | 9574 | if (result && (complain & tf_error)) |
0f51ccfc | 9575 | error (" trying to instantiate %qD", tmpl); |
3e4a3562 NS |
9576 | return result; |
9577 | } | |
9578 | ||
6ba89f8e | 9579 | /* Instantiate the indicated variable or function template TMPL with |
36a117a5 MM |
9580 | the template arguments in TARG_PTR. */ |
9581 | ||
5566b478 | 9582 | tree |
3a978d72 | 9583 | instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain) |
8d08fdba | 9584 | { |
5566b478 | 9585 | tree fndecl; |
36a117a5 MM |
9586 | tree gen_tmpl; |
9587 | tree spec; | |
a95799ec | 9588 | HOST_WIDE_INT saved_processing_template_decl; |
5566b478 | 9589 | |
27fafc8d JM |
9590 | if (tmpl == error_mark_node) |
9591 | return error_mark_node; | |
9592 | ||
50bc768d | 9593 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
386b8a85 | 9594 | |
db9b2174 MM |
9595 | /* If this function is a clone, handle it specially. */ |
9596 | if (DECL_CLONED_FUNCTION_P (tmpl)) | |
3ad97789 | 9597 | { |
a30f62e0 | 9598 | tree spec; |
3ad97789 | 9599 | tree clone; |
c8094d83 | 9600 | |
a30f62e0 MM |
9601 | spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr, |
9602 | complain); | |
9603 | if (spec == error_mark_node) | |
9604 | return error_mark_node; | |
9605 | ||
c6002625 | 9606 | /* Look for the clone. */ |
4684cd27 | 9607 | FOR_EACH_CLONE (clone, spec) |
3ad97789 NS |
9608 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
9609 | return clone; | |
9610 | /* We should always have found the clone by now. */ | |
315fb5db | 9611 | gcc_unreachable (); |
3ad97789 NS |
9612 | return NULL_TREE; |
9613 | } | |
c8094d83 | 9614 | |
36a117a5 | 9615 | /* Check to see if we already have this specialization. */ |
c8094d83 | 9616 | spec = retrieve_specialization (tmpl, targ_ptr, |
c7222c02 | 9617 | /*class_specializations_p=*/false); |
36a117a5 MM |
9618 | if (spec != NULL_TREE) |
9619 | return spec; | |
9620 | ||
f9a7ae04 MM |
9621 | gen_tmpl = most_general_template (tmpl); |
9622 | if (tmpl != gen_tmpl) | |
386b8a85 | 9623 | { |
36a117a5 MM |
9624 | /* The TMPL is a partial instantiation. To get a full set of |
9625 | arguments we must add the arguments used to perform the | |
9626 | partial instantiation. */ | |
9627 | targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl), | |
9628 | targ_ptr); | |
36a117a5 MM |
9629 | |
9630 | /* Check to see if we already have this specialization. */ | |
c7222c02 MM |
9631 | spec = retrieve_specialization (gen_tmpl, targ_ptr, |
9632 | /*class_specializations_p=*/false); | |
75650646 MM |
9633 | if (spec != NULL_TREE) |
9634 | return spec; | |
386b8a85 JM |
9635 | } |
9636 | ||
3e4a3562 NS |
9637 | if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
9638 | complain)) | |
9639 | return error_mark_node; | |
c8094d83 | 9640 | |
2b907f5c KL |
9641 | /* We are building a FUNCTION_DECL, during which the access of its |
9642 | parameters and return types have to be checked. However this | |
9643 | FUNCTION_DECL which is the desired context for access checking | |
9644 | is not built yet. We solve this chicken-and-egg problem by | |
9645 | deferring all checks until we have the FUNCTION_DECL. */ | |
9646 | push_deferring_access_checks (dk_deferred); | |
5c74d5b0 | 9647 | |
a95799ec MM |
9648 | /* Although PROCESSING_TEMPLATE_DECL may be true at this point |
9649 | (because, for example, we have encountered a non-dependent | |
a1d08991 MM |
9650 | function call in the body of a template function and must now |
9651 | determine which of several overloaded functions will be called), | |
9652 | within the instantiation itself we are not processing a | |
9653 | template. */ | |
a95799ec MM |
9654 | saved_processing_template_decl = processing_template_decl; |
9655 | processing_template_decl = 0; | |
9656 | /* Substitute template parameters to obtain the specialization. */ | |
17aec3eb | 9657 | fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl), |
3e4a3562 | 9658 | targ_ptr, complain, gen_tmpl); |
a95799ec | 9659 | processing_template_decl = saved_processing_template_decl; |
6b6b60af MM |
9660 | if (fndecl == error_mark_node) |
9661 | return error_mark_node; | |
5c74d5b0 | 9662 | |
2b907f5c KL |
9663 | /* Now we know the specialization, compute access previously |
9664 | deferred. */ | |
9665 | push_access_scope (fndecl); | |
9666 | perform_deferred_access_checks (); | |
9667 | pop_access_scope (fndecl); | |
9668 | pop_deferring_access_checks (); | |
5c74d5b0 | 9669 | |
36a117a5 MM |
9670 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
9671 | template, not the most general template. */ | |
9672 | DECL_TI_TEMPLATE (fndecl) = tmpl; | |
8d08fdba | 9673 | |
94350948 | 9674 | /* If we've just instantiated the main entry point for a function, |
3ad97789 NS |
9675 | instantiate all the alternate entry points as well. We do this |
9676 | by cloning the instantiation of the main entry point, not by | |
9677 | instantiating the template clones. */ | |
9678 | if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl))) | |
9679 | clone_function_decl (fndecl, /*update_method_vec_p=*/0); | |
94350948 | 9680 | |
5566b478 | 9681 | return fndecl; |
8d08fdba | 9682 | } |
5566b478 | 9683 | |
4393e105 MM |
9684 | /* The FN is a TEMPLATE_DECL for a function. The ARGS are the |
9685 | arguments that are being used when calling it. TARGS is a vector | |
c8094d83 | 9686 | into which the deduced template arguments are placed. |
8d08fdba MS |
9687 | |
9688 | Return zero for success, 2 for an incomplete match that doesn't resolve | |
9689 | all the types, and 1 for complete failure. An error message will be | |
9690 | printed only for an incomplete match. | |
9691 | ||
e5214479 JM |
9692 | If FN is a conversion operator, or we are trying to produce a specific |
9693 | specialization, RETURN_TYPE is the return type desired. | |
9f54c803 MM |
9694 | |
9695 | The EXPLICIT_TARGS are explicit template arguments provided via a | |
9696 | template-id. | |
6467930b | 9697 | |
830bfa74 MM |
9698 | The parameter STRICT is one of: |
9699 | ||
c8094d83 | 9700 | DEDUCE_CALL: |
830bfa74 MM |
9701 | We are deducing arguments for a function call, as in |
9702 | [temp.deduct.call]. | |
9703 | ||
9704 | DEDUCE_CONV: | |
c8094d83 | 9705 | We are deducing arguments for a conversion function, as in |
830bfa74 MM |
9706 | [temp.deduct.conv]. |
9707 | ||
9708 | DEDUCE_EXACT: | |
62e4a758 NS |
9709 | We are deducing arguments when doing an explicit instantiation |
9710 | as in [temp.explicit], when determining an explicit specialization | |
9711 | as in [temp.expl.spec], or when taking the address of a function | |
a34d3336 | 9712 | template, as in [temp.deduct.funcaddr]. */ |
8d08fdba MS |
9713 | |
9714 | int | |
c8094d83 | 9715 | fn_type_unification (tree fn, |
0cbd7506 MS |
9716 | tree explicit_targs, |
9717 | tree targs, | |
9718 | tree args, | |
9719 | tree return_type, | |
30f86ec3 FJ |
9720 | unification_kind_t strict, |
9721 | int flags) | |
386b8a85 | 9722 | { |
4393e105 MM |
9723 | tree parms; |
9724 | tree fntype; | |
adecb3f4 | 9725 | int result; |
386b8a85 | 9726 | |
50bc768d | 9727 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
2b59fc25 | 9728 | |
4393e105 MM |
9729 | fntype = TREE_TYPE (fn); |
9730 | if (explicit_targs) | |
75650646 | 9731 | { |
4393e105 | 9732 | /* [temp.deduct] |
c8094d83 | 9733 | |
4393e105 MM |
9734 | The specified template arguments must match the template |
9735 | parameters in kind (i.e., type, nontype, template), and there | |
9736 | must not be more arguments than there are parameters; | |
9737 | otherwise type deduction fails. | |
9738 | ||
9739 | Nontype arguments must match the types of the corresponding | |
9740 | nontype template parameters, or must be convertible to the | |
9741 | types of the corresponding nontype parameters as specified in | |
9742 | _temp.arg.nontype_, otherwise type deduction fails. | |
9743 | ||
9744 | All references in the function type of the function template | |
9745 | to the corresponding template parameters are replaced by the | |
9746 | specified template argument values. If a substitution in a | |
9747 | template parameter or in the function type of the function | |
9748 | template results in an invalid type, type deduction fails. */ | |
9749 | int i; | |
9750 | tree converted_args; | |
ffd49b19 | 9751 | bool incomplete; |
75650646 | 9752 | |
c8a7ed43 AO |
9753 | if (explicit_targs == error_mark_node) |
9754 | return 1; | |
9755 | ||
4393e105 | 9756 | converted_args |
c8094d83 MS |
9757 | = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
9758 | explicit_targs, NULL_TREE, tf_none, | |
e7e93965 MM |
9759 | /*require_all_args=*/false, |
9760 | /*use_default_args=*/false)); | |
4393e105 | 9761 | if (converted_args == error_mark_node) |
75650646 | 9762 | return 1; |
386b8a85 | 9763 | |
ffd49b19 | 9764 | /* Substitute the explicit args into the function type. This is |
0cbd7506 MS |
9765 | necessary so that, for instance, explicitly declared function |
9766 | arguments can match null pointed constants. If we were given | |
9767 | an incomplete set of explicit args, we must not do semantic | |
9768 | processing during substitution as we could create partial | |
9769 | instantiations. */ | |
ffd49b19 NS |
9770 | incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs); |
9771 | processing_template_decl += incomplete; | |
c2ea3a40 | 9772 | fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE); |
ffd49b19 | 9773 | processing_template_decl -= incomplete; |
c8094d83 | 9774 | |
4393e105 MM |
9775 | if (fntype == error_mark_node) |
9776 | return 1; | |
050367a3 | 9777 | |
4393e105 | 9778 | /* Place the explicitly specified arguments in TARGS. */ |
ffd49b19 | 9779 | for (i = NUM_TMPL_ARGS (converted_args); i--;) |
4393e105 | 9780 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i); |
75650646 | 9781 | } |
c8094d83 | 9782 | |
4393e105 | 9783 | parms = TYPE_ARG_TYPES (fntype); |
e5214479 JM |
9784 | /* Never do unification on the 'this' parameter. */ |
9785 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)) | |
9786 | parms = TREE_CHAIN (parms); | |
c8094d83 | 9787 | |
8d3631f8 NS |
9788 | if (return_type) |
9789 | { | |
8d3631f8 NS |
9790 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
9791 | args = tree_cons (NULL_TREE, return_type, args); | |
4393e105 MM |
9792 | } |
9793 | ||
4393e105 MM |
9794 | /* We allow incomplete unification without an error message here |
9795 | because the standard doesn't seem to explicitly prohibit it. Our | |
9796 | callers must be ready to deal with unification failures in any | |
9797 | event. */ | |
c8094d83 | 9798 | result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
adecb3f4 | 9799 | targs, parms, args, /*subr=*/0, |
30f86ec3 | 9800 | strict, flags); |
adecb3f4 | 9801 | |
c8094d83 | 9802 | if (result == 0) |
adecb3f4 | 9803 | /* All is well so far. Now, check: |
c8094d83 MS |
9804 | |
9805 | [temp.deduct] | |
9806 | ||
adecb3f4 MM |
9807 | When all template arguments have been deduced, all uses of |
9808 | template parameters in nondeduced contexts are replaced with | |
9809 | the corresponding deduced argument values. If the | |
9810 | substitution results in an invalid type, as described above, | |
9811 | type deduction fails. */ | |
c2ea3a40 | 9812 | if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE) |
adecb3f4 MM |
9813 | == error_mark_node) |
9814 | return 1; | |
9815 | ||
9816 | return result; | |
830bfa74 MM |
9817 | } |
9818 | ||
9819 | /* Adjust types before performing type deduction, as described in | |
9820 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two | |
4c7d0dff MM |
9821 | sections are symmetric. PARM is the type of a function parameter |
9822 | or the return type of the conversion function. ARG is the type of | |
9823 | the argument passed to the call, or the type of the value | |
dc957d14 | 9824 | initialized with the result of the conversion function. */ |
386b8a85 | 9825 | |
62e4a758 | 9826 | static int |
c8094d83 | 9827 | maybe_adjust_types_for_deduction (unification_kind_t strict, |
0cbd7506 MS |
9828 | tree* parm, |
9829 | tree* arg) | |
830bfa74 | 9830 | { |
62e4a758 | 9831 | int result = 0; |
c8094d83 | 9832 | |
830bfa74 MM |
9833 | switch (strict) |
9834 | { | |
9835 | case DEDUCE_CALL: | |
9836 | break; | |
9837 | ||
9838 | case DEDUCE_CONV: | |
9839 | { | |
4c7d0dff MM |
9840 | /* Swap PARM and ARG throughout the remainder of this |
9841 | function; the handling is precisely symmetric since PARM | |
9842 | will initialize ARG rather than vice versa. */ | |
830bfa74 MM |
9843 | tree* temp = parm; |
9844 | parm = arg; | |
9845 | arg = temp; | |
9846 | break; | |
9847 | } | |
9848 | ||
9849 | case DEDUCE_EXACT: | |
9850 | /* There is nothing to do in this case. */ | |
62e4a758 | 9851 | return 0; |
830bfa74 MM |
9852 | |
9853 | default: | |
315fb5db | 9854 | gcc_unreachable (); |
830bfa74 MM |
9855 | } |
9856 | ||
9857 | if (TREE_CODE (*parm) != REFERENCE_TYPE) | |
9858 | { | |
9859 | /* [temp.deduct.call] | |
c8094d83 | 9860 | |
830bfa74 | 9861 | If P is not a reference type: |
c8094d83 | 9862 | |
830bfa74 MM |
9863 | --If A is an array type, the pointer type produced by the |
9864 | array-to-pointer standard conversion (_conv.array_) is | |
9865 | used in place of A for type deduction; otherwise, | |
c8094d83 | 9866 | |
830bfa74 MM |
9867 | --If A is a function type, the pointer type produced by |
9868 | the function-to-pointer standard conversion | |
9869 | (_conv.func_) is used in place of A for type deduction; | |
9870 | otherwise, | |
c8094d83 | 9871 | |
830bfa74 MM |
9872 | --If A is a cv-qualified type, the top level |
9873 | cv-qualifiers of A's type are ignored for type | |
9874 | deduction. */ | |
9875 | if (TREE_CODE (*arg) == ARRAY_TYPE) | |
9876 | *arg = build_pointer_type (TREE_TYPE (*arg)); | |
d8f8dca1 | 9877 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
830bfa74 MM |
9878 | *arg = build_pointer_type (*arg); |
9879 | else | |
9880 | *arg = TYPE_MAIN_VARIANT (*arg); | |
9881 | } | |
c8094d83 | 9882 | |
830bfa74 | 9883 | /* [temp.deduct.call] |
c8094d83 | 9884 | |
830bfa74 MM |
9885 | If P is a cv-qualified type, the top level cv-qualifiers |
9886 | of P's type are ignored for type deduction. If P is a | |
9887 | reference type, the type referred to by P is used for | |
9888 | type deduction. */ | |
9889 | *parm = TYPE_MAIN_VARIANT (*parm); | |
9890 | if (TREE_CODE (*parm) == REFERENCE_TYPE) | |
62e4a758 NS |
9891 | { |
9892 | *parm = TREE_TYPE (*parm); | |
9893 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; | |
9894 | } | |
1c82cc90 NS |
9895 | |
9896 | /* DR 322. For conversion deduction, remove a reference type on parm | |
9897 | too (which has been swapped into ARG). */ | |
9898 | if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE) | |
9899 | *arg = TREE_TYPE (*arg); | |
c8094d83 | 9900 | |
62e4a758 | 9901 | return result; |
386b8a85 JM |
9902 | } |
9903 | ||
e5214479 | 9904 | /* Most parms like fn_type_unification. |
9f54c803 MM |
9905 | |
9906 | If SUBR is 1, we're being called recursively (to unify the | |
9907 | arguments of a function or method parameter of a function | |
38d18b1a | 9908 | template). */ |
386b8a85 | 9909 | |
4966381a | 9910 | static int |
c8094d83 | 9911 | type_unification_real (tree tparms, |
0cbd7506 MS |
9912 | tree targs, |
9913 | tree xparms, | |
9914 | tree xargs, | |
9915 | int subr, | |
30f86ec3 FJ |
9916 | unification_kind_t strict, |
9917 | int flags) | |
8d08fdba MS |
9918 | { |
9919 | tree parm, arg; | |
9920 | int i; | |
9921 | int ntparms = TREE_VEC_LENGTH (tparms); | |
830bfa74 | 9922 | int sub_strict; |
bd0d5d4a JM |
9923 | int saw_undeduced = 0; |
9924 | tree parms, args; | |
8d08fdba | 9925 | |
50bc768d NS |
9926 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
9927 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); | |
9928 | gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST); | |
9929 | gcc_assert (ntparms > 0); | |
8d08fdba | 9930 | |
830bfa74 MM |
9931 | switch (strict) |
9932 | { | |
9933 | case DEDUCE_CALL: | |
028d1f20 | 9934 | sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL |
0cbd7506 | 9935 | | UNIFY_ALLOW_DERIVED); |
830bfa74 | 9936 | break; |
c8094d83 | 9937 | |
830bfa74 MM |
9938 | case DEDUCE_CONV: |
9939 | sub_strict = UNIFY_ALLOW_LESS_CV_QUAL; | |
9940 | break; | |
9941 | ||
9942 | case DEDUCE_EXACT: | |
9943 | sub_strict = UNIFY_ALLOW_NONE; | |
9944 | break; | |
c8094d83 | 9945 | |
830bfa74 | 9946 | default: |
315fb5db | 9947 | gcc_unreachable (); |
830bfa74 MM |
9948 | } |
9949 | ||
bd0d5d4a JM |
9950 | again: |
9951 | parms = xparms; | |
9952 | args = xargs; | |
bd0d5d4a | 9953 | |
a34d3336 NS |
9954 | while (parms && parms != void_list_node |
9955 | && args && args != void_list_node) | |
8d08fdba MS |
9956 | { |
9957 | parm = TREE_VALUE (parms); | |
9958 | parms = TREE_CHAIN (parms); | |
9959 | arg = TREE_VALUE (args); | |
9960 | args = TREE_CHAIN (args); | |
9961 | ||
9962 | if (arg == error_mark_node) | |
9963 | return 1; | |
9964 | if (arg == unknown_type_node) | |
34016c81 JM |
9965 | /* We can't deduce anything from this, but we might get all the |
9966 | template args from other function args. */ | |
9967 | continue; | |
b7484fbe | 9968 | |
03e70705 JM |
9969 | /* Conversions will be performed on a function argument that |
9970 | corresponds with a function parameter that contains only | |
9971 | non-deducible template parameters and explicitly specified | |
9972 | template parameters. */ | |
c353b8e3 | 9973 | if (!uses_template_parms (parm)) |
b7484fbe | 9974 | { |
03e70705 JM |
9975 | tree type; |
9976 | ||
2f939d94 | 9977 | if (!TYPE_P (arg)) |
03e70705 JM |
9978 | type = TREE_TYPE (arg); |
9979 | else | |
c353b8e3 | 9980 | type = arg; |
03e70705 | 9981 | |
fad86f7a | 9982 | if (same_type_p (parm, type)) |
343c89cd | 9983 | continue; |
fad86f7a | 9984 | if (strict != DEDUCE_EXACT |
3db45ab5 | 9985 | && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, |
30f86ec3 | 9986 | flags)) |
fad86f7a | 9987 | continue; |
3db45ab5 | 9988 | |
b7484fbe MS |
9989 | return 1; |
9990 | } | |
c8094d83 | 9991 | |
2f939d94 | 9992 | if (!TYPE_P (arg)) |
8d08fdba | 9993 | { |
50bc768d | 9994 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
34016c81 | 9995 | if (type_unknown_p (arg)) |
28cbf42c | 9996 | { |
34016c81 JM |
9997 | /* [temp.deduct.type] A template-argument can be deduced from |
9998 | a pointer to function or pointer to member function | |
9999 | argument if the set of overloaded functions does not | |
10000 | contain function templates and at most one of a set of | |
10001 | overloaded functions provides a unique match. */ | |
10002 | ||
10003 | if (resolve_overloaded_unification | |
4393e105 | 10004 | (tparms, targs, parm, arg, strict, sub_strict) |
34016c81 JM |
10005 | != 0) |
10006 | return 1; | |
10007 | continue; | |
28cbf42c | 10008 | } |
8d08fdba | 10009 | arg = TREE_TYPE (arg); |
08476342 NS |
10010 | if (arg == error_mark_node) |
10011 | return 1; | |
8d08fdba | 10012 | } |
c8094d83 | 10013 | |
62e4a758 | 10014 | { |
0cbd7506 | 10015 | int arg_strict = sub_strict; |
c8094d83 | 10016 | |
0cbd7506 | 10017 | if (!subr) |
62e4a758 | 10018 | arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); |
4393e105 | 10019 | |
0cbd7506 MS |
10020 | if (unify (tparms, targs, parm, arg, arg_strict)) |
10021 | return 1; | |
62e4a758 | 10022 | } |
8d08fdba | 10023 | } |
c8094d83 | 10024 | |
8d08fdba MS |
10025 | /* Fail if we've reached the end of the parm list, and more args |
10026 | are present, and the parm list isn't variadic. */ | |
10027 | if (args && args != void_list_node && parms == void_list_node) | |
10028 | return 1; | |
f4f206f4 | 10029 | /* Fail if parms are left and they don't have default values. */ |
a34d3336 | 10030 | if (parms && parms != void_list_node |
8d08fdba MS |
10031 | && TREE_PURPOSE (parms) == NULL_TREE) |
10032 | return 1; | |
bd0d5d4a | 10033 | |
8d08fdba MS |
10034 | if (!subr) |
10035 | for (i = 0; i < ntparms; i++) | |
a34d3336 | 10036 | if (!TREE_VEC_ELT (targs, i)) |
8d08fdba | 10037 | { |
2d8ba2c7 LM |
10038 | tree tparm; |
10039 | ||
10040 | if (TREE_VEC_ELT (tparms, i) == error_mark_node) | |
10041 | continue; | |
10042 | ||
10043 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); | |
bd0d5d4a JM |
10044 | |
10045 | /* If this is an undeduced nontype parameter that depends on | |
10046 | a type parameter, try another pass; its type may have been | |
10047 | deduced from a later argument than the one from which | |
10048 | this parameter can be deduced. */ | |
10049 | if (TREE_CODE (tparm) == PARM_DECL | |
10050 | && uses_template_parms (TREE_TYPE (tparm)) | |
10051 | && !saw_undeduced++) | |
10052 | goto again; | |
10053 | ||
8d08fdba MS |
10054 | return 2; |
10055 | } | |
c8094d83 | 10056 | |
8d08fdba MS |
10057 | return 0; |
10058 | } | |
10059 | ||
34016c81 JM |
10060 | /* Subroutine of type_unification_real. Args are like the variables at the |
10061 | call site. ARG is an overloaded function (or template-id); we try | |
10062 | deducing template args from each of the overloads, and if only one | |
10063 | succeeds, we go with that. Modifies TARGS and returns 0 on success. */ | |
10064 | ||
10065 | static int | |
c8094d83 | 10066 | resolve_overloaded_unification (tree tparms, |
0cbd7506 MS |
10067 | tree targs, |
10068 | tree parm, | |
10069 | tree arg, | |
10070 | unification_kind_t strict, | |
3a978d72 | 10071 | int sub_strict) |
34016c81 JM |
10072 | { |
10073 | tree tempargs = copy_node (targs); | |
10074 | int good = 0; | |
f23fb7f5 | 10075 | bool addr_p; |
34016c81 JM |
10076 | |
10077 | if (TREE_CODE (arg) == ADDR_EXPR) | |
f23fb7f5 MM |
10078 | { |
10079 | arg = TREE_OPERAND (arg, 0); | |
10080 | addr_p = true; | |
10081 | } | |
10082 | else | |
10083 | addr_p = false; | |
9f3d9e46 | 10084 | |
d8f8dca1 MM |
10085 | if (TREE_CODE (arg) == COMPONENT_REF) |
10086 | /* Handle `&x' where `x' is some static or non-static member | |
10087 | function name. */ | |
10088 | arg = TREE_OPERAND (arg, 1); | |
10089 | ||
05e0b2f4 JM |
10090 | if (TREE_CODE (arg) == OFFSET_REF) |
10091 | arg = TREE_OPERAND (arg, 1); | |
10092 | ||
9f3d9e46 | 10093 | /* Strip baselink information. */ |
50ad9642 MM |
10094 | if (BASELINK_P (arg)) |
10095 | arg = BASELINK_FUNCTIONS (arg); | |
9f3d9e46 | 10096 | |
34016c81 JM |
10097 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
10098 | { | |
10099 | /* If we got some explicit template args, we need to plug them into | |
10100 | the affected templates before we try to unify, in case the | |
10101 | explicit args will completely resolve the templates in question. */ | |
10102 | ||
10103 | tree expl_subargs = TREE_OPERAND (arg, 1); | |
10104 | arg = TREE_OPERAND (arg, 0); | |
10105 | ||
10106 | for (; arg; arg = OVL_NEXT (arg)) | |
10107 | { | |
10108 | tree fn = OVL_CURRENT (arg); | |
10109 | tree subargs, elem; | |
10110 | ||
10111 | if (TREE_CODE (fn) != TEMPLATE_DECL) | |
10112 | continue; | |
10113 | ||
a34d3336 NS |
10114 | subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn), |
10115 | expl_subargs, /*check_ret=*/false); | |
34016c81 JM |
10116 | if (subargs) |
10117 | { | |
c2ea3a40 | 10118 | elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE); |
c8094d83 | 10119 | good += try_one_overload (tparms, targs, tempargs, parm, |
f23fb7f5 | 10120 | elem, strict, sub_strict, addr_p); |
34016c81 JM |
10121 | } |
10122 | } | |
10123 | } | |
315fb5db | 10124 | else |
34016c81 | 10125 | { |
315fb5db NS |
10126 | gcc_assert (TREE_CODE (arg) == OVERLOAD |
10127 | || TREE_CODE (arg) == FUNCTION_DECL); | |
c8094d83 | 10128 | |
34016c81 | 10129 | for (; arg; arg = OVL_NEXT (arg)) |
f23fb7f5 MM |
10130 | good += try_one_overload (tparms, targs, tempargs, parm, |
10131 | TREE_TYPE (OVL_CURRENT (arg)), | |
10132 | strict, sub_strict, addr_p); | |
34016c81 | 10133 | } |
34016c81 JM |
10134 | |
10135 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
10136 | to function or pointer to member function argument if the set of | |
10137 | overloaded functions does not contain function templates and at most | |
10138 | one of a set of overloaded functions provides a unique match. | |
10139 | ||
10140 | So if we found multiple possibilities, we return success but don't | |
10141 | deduce anything. */ | |
10142 | ||
10143 | if (good == 1) | |
10144 | { | |
10145 | int i = TREE_VEC_LENGTH (targs); | |
10146 | for (; i--; ) | |
10147 | if (TREE_VEC_ELT (tempargs, i)) | |
10148 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i); | |
10149 | } | |
10150 | if (good) | |
10151 | return 0; | |
10152 | ||
10153 | return 1; | |
10154 | } | |
10155 | ||
10156 | /* Subroutine of resolve_overloaded_unification; does deduction for a single | |
10157 | overload. Fills TARGS with any deduced arguments, or error_mark_node if | |
10158 | different overloads deduce different arguments for a given parm. | |
f23fb7f5 MM |
10159 | ADDR_P is true if the expression for which deduction is being |
10160 | performed was of the form "& fn" rather than simply "fn". | |
10161 | ||
34016c81 JM |
10162 | Returns 1 on success. */ |
10163 | ||
10164 | static int | |
3a978d72 | 10165 | try_one_overload (tree tparms, |
0cbd7506 MS |
10166 | tree orig_targs, |
10167 | tree targs, | |
10168 | tree parm, | |
10169 | tree arg, | |
10170 | unification_kind_t strict, | |
f23fb7f5 MM |
10171 | int sub_strict, |
10172 | bool addr_p) | |
34016c81 JM |
10173 | { |
10174 | int nargs; | |
10175 | tree tempargs; | |
10176 | int i; | |
10177 | ||
10178 | /* [temp.deduct.type] A template-argument can be deduced from a pointer | |
10179 | to function or pointer to member function argument if the set of | |
10180 | overloaded functions does not contain function templates and at most | |
10181 | one of a set of overloaded functions provides a unique match. | |
10182 | ||
10183 | So if this is a template, just return success. */ | |
10184 | ||
10185 | if (uses_template_parms (arg)) | |
10186 | return 1; | |
10187 | ||
f23fb7f5 MM |
10188 | if (TREE_CODE (arg) == METHOD_TYPE) |
10189 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); | |
10190 | else if (addr_p) | |
10191 | arg = build_pointer_type (arg); | |
10192 | ||
62e4a758 | 10193 | sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg); |
34016c81 JM |
10194 | |
10195 | /* We don't copy orig_targs for this because if we have already deduced | |
10196 | some template args from previous args, unify would complain when we | |
10197 | try to deduce a template parameter for the same argument, even though | |
10198 | there isn't really a conflict. */ | |
10199 | nargs = TREE_VEC_LENGTH (targs); | |
f31c0a32 | 10200 | tempargs = make_tree_vec (nargs); |
34016c81 | 10201 | |
4393e105 | 10202 | if (unify (tparms, tempargs, parm, arg, sub_strict) != 0) |
34016c81 JM |
10203 | return 0; |
10204 | ||
10205 | /* First make sure we didn't deduce anything that conflicts with | |
e97e5263 | 10206 | explicitly specified args. */ |
34016c81 JM |
10207 | for (i = nargs; i--; ) |
10208 | { | |
10209 | tree elt = TREE_VEC_ELT (tempargs, i); | |
e97e5263 | 10210 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
34016c81 | 10211 | |
a34d3336 NS |
10212 | if (!elt) |
10213 | /*NOP*/; | |
34016c81 | 10214 | else if (uses_template_parms (elt)) |
a34d3336 NS |
10215 | /* Since we're unifying against ourselves, we will fill in |
10216 | template args used in the function parm list with our own | |
10217 | template parms. Discard them. */ | |
10218 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; | |
10219 | else if (oldelt && !template_args_equal (oldelt, elt)) | |
34016c81 JM |
10220 | return 0; |
10221 | } | |
10222 | ||
10223 | for (i = nargs; i--; ) | |
10224 | { | |
10225 | tree elt = TREE_VEC_ELT (tempargs, i); | |
10226 | ||
10227 | if (elt) | |
10228 | TREE_VEC_ELT (targs, i) = elt; | |
10229 | } | |
10230 | ||
10231 | return 1; | |
10232 | } | |
10233 | ||
4393e105 MM |
10234 | /* PARM is a template class (perhaps with unbound template |
10235 | parameters). ARG is a fully instantiated type. If ARG can be | |
10236 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and | |
10237 | TARGS are as for unify. */ | |
fcfb9f96 MM |
10238 | |
10239 | static tree | |
3a978d72 | 10240 | try_class_unification (tree tparms, tree targs, tree parm, tree arg) |
4393e105 | 10241 | { |
4393e105 MM |
10242 | tree copy_of_targs; |
10243 | ||
10244 | if (!CLASSTYPE_TEMPLATE_INFO (arg) | |
c8094d83 | 10245 | || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg)) |
68361a03 | 10246 | != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))) |
4393e105 MM |
10247 | return NULL_TREE; |
10248 | ||
10249 | /* We need to make a new template argument vector for the call to | |
10250 | unify. If we used TARGS, we'd clutter it up with the result of | |
10251 | the attempted unification, even if this class didn't work out. | |
10252 | We also don't want to commit ourselves to all the unifications | |
10253 | we've already done, since unification is supposed to be done on | |
10254 | an argument-by-argument basis. In other words, consider the | |
10255 | following pathological case: | |
10256 | ||
10257 | template <int I, int J, int K> | |
10258 | struct S {}; | |
c8094d83 | 10259 | |
4393e105 MM |
10260 | template <int I, int J> |
10261 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; | |
c8094d83 | 10262 | |
4393e105 MM |
10263 | template <int I, int J, int K> |
10264 | void f(S<I, J, K>, S<I, I, I>); | |
c8094d83 | 10265 | |
4393e105 | 10266 | void g() { |
0cbd7506 MS |
10267 | S<0, 0, 0> s0; |
10268 | S<0, 1, 2> s2; | |
c8094d83 | 10269 | |
0cbd7506 | 10270 | f(s0, s2); |
4393e105 MM |
10271 | } |
10272 | ||
10273 | Now, by the time we consider the unification involving `s2', we | |
10274 | already know that we must have `f<0, 0, 0>'. But, even though | |
0e339752 | 10275 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
4393e105 MM |
10276 | because there are two ways to unify base classes of S<0, 1, 2> |
10277 | with S<I, I, I>. If we kept the already deduced knowledge, we | |
10278 | would reject the possibility I=1. */ | |
f31c0a32 | 10279 | copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); |
c8094d83 | 10280 | |
4393e105 | 10281 | /* If unification failed, we're done. */ |
74601d7c KL |
10282 | if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), |
10283 | CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE)) | |
4393e105 | 10284 | return NULL_TREE; |
74601d7c KL |
10285 | |
10286 | return arg; | |
4393e105 MM |
10287 | } |
10288 | ||
a3a0fc7f NS |
10289 | /* Given a template type PARM and a class type ARG, find the unique |
10290 | base type in ARG that is an instance of PARM. We do not examine | |
10291 | ARG itself; only its base-classes. If there is not exactly one | |
10292 | appropriate base class, return NULL_TREE. PARM may be the type of | |
10293 | a partial specialization, as well as a plain template type. Used | |
10294 | by unify. */ | |
4393e105 MM |
10295 | |
10296 | static tree | |
a3a0fc7f | 10297 | get_template_base (tree tparms, tree targs, tree parm, tree arg) |
fcfb9f96 | 10298 | { |
a3a0fc7f NS |
10299 | tree rval = NULL_TREE; |
10300 | tree binfo; | |
10301 | ||
10302 | gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg))); | |
c8094d83 | 10303 | |
a3a0fc7f NS |
10304 | binfo = TYPE_BINFO (complete_type (arg)); |
10305 | if (!binfo) | |
10306 | /* The type could not be completed. */ | |
10307 | return NULL_TREE; | |
fcfb9f96 | 10308 | |
a3a0fc7f NS |
10309 | /* Walk in inheritance graph order. The search order is not |
10310 | important, and this avoids multiple walks of virtual bases. */ | |
10311 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) | |
fcfb9f96 | 10312 | { |
a3a0fc7f | 10313 | tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo)); |
fcfb9f96 | 10314 | |
8d83768f NS |
10315 | if (r) |
10316 | { | |
10317 | /* If there is more than one satisfactory baseclass, then: | |
fcfb9f96 | 10318 | |
8d83768f | 10319 | [temp.deduct.call] |
fcfb9f96 | 10320 | |
8d83768f NS |
10321 | If they yield more than one possible deduced A, the type |
10322 | deduction fails. | |
4393e105 | 10323 | |
8d83768f | 10324 | applies. */ |
a3a0fc7f NS |
10325 | if (rval && !same_type_p (r, rval)) |
10326 | return NULL_TREE; | |
c8094d83 | 10327 | |
a3a0fc7f | 10328 | rval = r; |
8d83768f | 10329 | } |
fcfb9f96 MM |
10330 | } |
10331 | ||
a3a0fc7f | 10332 | return rval; |
fcfb9f96 MM |
10333 | } |
10334 | ||
db2767b6 MM |
10335 | /* Returns the level of DECL, which declares a template parameter. */ |
10336 | ||
e9659ab0 | 10337 | static int |
3a978d72 | 10338 | template_decl_level (tree decl) |
db2767b6 MM |
10339 | { |
10340 | switch (TREE_CODE (decl)) | |
10341 | { | |
10342 | case TYPE_DECL: | |
10343 | case TEMPLATE_DECL: | |
10344 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); | |
10345 | ||
10346 | case PARM_DECL: | |
10347 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); | |
10348 | ||
10349 | default: | |
315fb5db | 10350 | gcc_unreachable (); |
db2767b6 | 10351 | } |
315fb5db | 10352 | return 0; |
db2767b6 MM |
10353 | } |
10354 | ||
830bfa74 MM |
10355 | /* Decide whether ARG can be unified with PARM, considering only the |
10356 | cv-qualifiers of each type, given STRICT as documented for unify. | |
324f9dfb | 10357 | Returns nonzero iff the unification is OK on that basis. */ |
e92cc029 | 10358 | |
e9659ab0 | 10359 | static int |
3a978d72 | 10360 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
830bfa74 | 10361 | { |
4f2b0fb2 NS |
10362 | int arg_quals = cp_type_quals (arg); |
10363 | int parm_quals = cp_type_quals (parm); | |
10364 | ||
355f774d NS |
10365 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
10366 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) | |
4f2b0fb2 | 10367 | { |
2e9ceb77 | 10368 | /* Although a CVR qualifier is ignored when being applied to a |
0cbd7506 MS |
10369 | substituted template parameter ([8.3.2]/1 for example), that |
10370 | does not apply during deduction [14.8.2.4]/1, (even though | |
10371 | that is not explicitly mentioned, [14.8.2.4]/9 indicates | |
10372 | this). Except when we're allowing additional CV qualifiers | |
10373 | at the outer level [14.8.2.1]/3,1st bullet. */ | |
2e9ceb77 NS |
10374 | if ((TREE_CODE (arg) == REFERENCE_TYPE |
10375 | || TREE_CODE (arg) == FUNCTION_TYPE | |
10376 | || TREE_CODE (arg) == METHOD_TYPE) | |
10377 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) | |
10378 | return 0; | |
10379 | ||
10380 | if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) | |
10381 | && (parm_quals & TYPE_QUAL_RESTRICT)) | |
10382 | return 0; | |
4f2b0fb2 | 10383 | } |
2e9ceb77 | 10384 | |
62e4a758 | 10385 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
4f2b0fb2 | 10386 | && (arg_quals & parm_quals) != parm_quals) |
ef637255 MM |
10387 | return 0; |
10388 | ||
62e4a758 | 10389 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
4f2b0fb2 | 10390 | && (parm_quals & arg_quals) != arg_quals) |
ef637255 MM |
10391 | return 0; |
10392 | ||
ef637255 | 10393 | return 1; |
830bfa74 MM |
10394 | } |
10395 | ||
916b63c3 MM |
10396 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
10397 | set of template parameters to a template. TARGS is the bindings | |
10398 | for those template parameters, as determined thus far; TARGS may | |
10399 | include template arguments for outer levels of template parameters | |
10400 | as well. PARM is a parameter to a template function, or a | |
10401 | subcomponent of that parameter; ARG is the corresponding argument. | |
10402 | This function attempts to match PARM with ARG in a manner | |
10403 | consistent with the existing assignments in TARGS. If more values | |
10404 | are deduced, then TARGS is updated. | |
10405 | ||
10406 | Returns 0 if the type deduction succeeds, 1 otherwise. The | |
10407 | parameter STRICT is a bitwise or of the following flags: | |
830bfa74 MM |
10408 | |
10409 | UNIFY_ALLOW_NONE: | |
10410 | Require an exact match between PARM and ARG. | |
10411 | UNIFY_ALLOW_MORE_CV_QUAL: | |
028d1f20 NS |
10412 | Allow the deduced ARG to be more cv-qualified (by qualification |
10413 | conversion) than ARG. | |
830bfa74 MM |
10414 | UNIFY_ALLOW_LESS_CV_QUAL: |
10415 | Allow the deduced ARG to be less cv-qualified than ARG. | |
10416 | UNIFY_ALLOW_DERIVED: | |
10417 | Allow the deduced ARG to be a template base class of ARG, | |
10418 | or a pointer to a template base class of the type pointed to by | |
161c12b0 JM |
10419 | ARG. |
10420 | UNIFY_ALLOW_INTEGER: | |
10421 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX | |
c8094d83 | 10422 | case for more information. |
028d1f20 NS |
10423 | UNIFY_ALLOW_OUTER_LEVEL: |
10424 | This is the outermost level of a deduction. Used to determine validity | |
10425 | of qualification conversions. A valid qualification conversion must | |
10426 | have const qualified pointers leading up to the inner type which | |
10427 | requires additional CV quals, except at the outer level, where const | |
10428 | is not required [conv.qual]. It would be normal to set this flag in | |
62e4a758 NS |
10429 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
10430 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: | |
10431 | This is the outermost level of a deduction, and PARM can be more CV | |
10432 | qualified at this point. | |
10433 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: | |
10434 | This is the outermost level of a deduction, and PARM can be less CV | |
8baddbf1 | 10435 | qualified at this point. */ |
830bfa74 | 10436 | |
e9659ab0 | 10437 | static int |
3a978d72 | 10438 | unify (tree tparms, tree targs, tree parm, tree arg, int strict) |
8d08fdba MS |
10439 | { |
10440 | int idx; | |
050367a3 | 10441 | tree targ; |
db2767b6 | 10442 | tree tparm; |
028d1f20 | 10443 | int strict_in = strict; |
8d08fdba MS |
10444 | |
10445 | /* I don't think this will do the right thing with respect to types. | |
10446 | But the only case I've seen it in so far has been array bounds, where | |
10447 | signedness is the only information lost, and I think that will be | |
10448 | okay. */ | |
10449 | while (TREE_CODE (parm) == NOP_EXPR) | |
10450 | parm = TREE_OPERAND (parm, 0); | |
10451 | ||
10452 | if (arg == error_mark_node) | |
10453 | return 1; | |
10454 | if (arg == unknown_type_node) | |
34016c81 JM |
10455 | /* We can't deduce anything from this, but we might get all the |
10456 | template args from other function args. */ | |
10457 | return 0; | |
10458 | ||
db2767b6 | 10459 | /* If PARM uses template parameters, then we can't bail out here, |
6bdb985f | 10460 | even if ARG == PARM, since we won't record unifications for the |
db2767b6 MM |
10461 | template parameters. We might need them if we're trying to |
10462 | figure out which of two things is more specialized. */ | |
10463 | if (arg == parm && !uses_template_parms (parm)) | |
8d08fdba MS |
10464 | return 0; |
10465 | ||
830bfa74 MM |
10466 | /* Immediately reject some pairs that won't unify because of |
10467 | cv-qualification mismatches. */ | |
10468 | if (TREE_CODE (arg) == TREE_CODE (parm) | |
2f939d94 | 10469 | && TYPE_P (arg) |
d0ab7624 | 10470 | /* It is the elements of the array which hold the cv quals of an array |
0cbd7506 MS |
10471 | type, and the elements might be template type parms. We'll check |
10472 | when we recurse. */ | |
d0ab7624 | 10473 | && TREE_CODE (arg) != ARRAY_TYPE |
830bfa74 MM |
10474 | /* We check the cv-qualifiers when unifying with template type |
10475 | parameters below. We want to allow ARG `const T' to unify with | |
10476 | PARM `T' for example, when computing which of two templates | |
10477 | is more specialized, for example. */ | |
10478 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM | |
028d1f20 | 10479 | && !check_cv_quals_for_unify (strict_in, arg, parm)) |
49432171 JM |
10480 | return 1; |
10481 | ||
028d1f20 | 10482 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
3ea099f1 | 10483 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm)) |
028d1f20 NS |
10484 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
10485 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; | |
10486 | strict &= ~UNIFY_ALLOW_DERIVED; | |
62e4a758 NS |
10487 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
10488 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; | |
c8094d83 | 10489 | |
8d08fdba MS |
10490 | switch (TREE_CODE (parm)) |
10491 | { | |
2ca340ae | 10492 | case TYPENAME_TYPE: |
fccef71e | 10493 | case SCOPE_REF: |
b8c6534b | 10494 | case UNBOUND_CLASS_TEMPLATE: |
2ca340ae JM |
10495 | /* In a type which contains a nested-name-specifier, template |
10496 | argument values cannot be deduced for template parameters used | |
10497 | within the nested-name-specifier. */ | |
10498 | return 0; | |
10499 | ||
8d08fdba | 10500 | case TEMPLATE_TYPE_PARM: |
73b0fce8 | 10501 | case TEMPLATE_TEMPLATE_PARM: |
a1281f45 | 10502 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
db2767b6 MM |
10503 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
10504 | ||
10505 | if (TEMPLATE_TYPE_LEVEL (parm) | |
10506 | != template_decl_level (tparm)) | |
10507 | /* The PARM is not one we're trying to unify. Just check | |
10508 | to see if it matches ARG. */ | |
10509 | return (TREE_CODE (arg) == TREE_CODE (parm) | |
3bfdc719 | 10510 | && same_type_p (parm, arg)) ? 0 : 1; |
73b0fce8 | 10511 | idx = TEMPLATE_TYPE_IDX (parm); |
916b63c3 | 10512 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 10513 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
050367a3 | 10514 | |
73b0fce8 | 10515 | /* Check for mixed types and values. */ |
db2767b6 MM |
10516 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
10517 | && TREE_CODE (tparm) != TYPE_DECL) | |
c8094d83 | 10518 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
db2767b6 | 10519 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
73b0fce8 KL |
10520 | return 1; |
10521 | ||
a1281f45 | 10522 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
73b0fce8 | 10523 | { |
b429fdf0 KL |
10524 | /* ARG must be constructed from a template class or a template |
10525 | template parameter. */ | |
10526 | if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM | |
4e95268d | 10527 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
a1281f45 | 10528 | return 1; |
73b0fce8 | 10529 | |
a1281f45 | 10530 | { |
a1281f45 | 10531 | tree parmvec = TYPE_TI_ARGS (parm); |
6df91b00 | 10532 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
a1281f45 | 10533 | tree argtmplvec |
b429fdf0 | 10534 | = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg)); |
a1281f45 | 10535 | int i; |
73b0fce8 | 10536 | |
e7e93965 MM |
10537 | /* The resolution to DR150 makes clear that default |
10538 | arguments for an N-argument may not be used to bind T | |
10539 | to a template template parameter with fewer than N | |
10540 | parameters. It is not safe to permit the binding of | |
10541 | default arguments as an extension, as that may change | |
10542 | the meaning of a conforming program. Consider: | |
10543 | ||
10544 | struct Dense { static const unsigned int dim = 1; }; | |
10545 | ||
10546 | template <template <typename> class View, | |
10547 | typename Block> | |
10548 | void operator+(float, View<Block> const&); | |
10549 | ||
3db45ab5 MS |
10550 | template <typename Block, |
10551 | unsigned int Dim = Block::dim> | |
e7e93965 MM |
10552 | struct Lvalue_proxy { operator float() const; }; |
10553 | ||
10554 | void | |
10555 | test_1d (void) { | |
10556 | Lvalue_proxy<Dense> p; | |
10557 | float b; | |
10558 | b + p; | |
10559 | } | |
a1281f45 | 10560 | |
e7e93965 MM |
10561 | Here, if Lvalue_proxy is permitted to bind to View, then |
10562 | the global operator+ will be used; if they are not, the | |
3db45ab5 MS |
10563 | Lvalue_proxy will be converted to float. */ |
10564 | if (coerce_template_parms (argtmplvec, parmvec, | |
e7e93965 MM |
10565 | TYPE_TI_TEMPLATE (parm), |
10566 | tf_none, | |
10567 | /*require_all_args=*/true, | |
10568 | /*use_default_args=*/false) | |
0cbd7506 | 10569 | == error_mark_node) |
a1281f45 | 10570 | return 1; |
c8094d83 MS |
10571 | |
10572 | /* Deduce arguments T, i from TT<T> or TT<i>. | |
a1281f45 KL |
10573 | We check each element of PARMVEC and ARGVEC individually |
10574 | rather than the whole TREE_VEC since they can have | |
10575 | different number of elements. */ | |
6b9b6b15 | 10576 | |
a1281f45 KL |
10577 | for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i) |
10578 | { | |
0cbd7506 | 10579 | if (unify (tparms, targs, |
c8094d83 MS |
10580 | TREE_VEC_ELT (parmvec, i), |
10581 | TREE_VEC_ELT (argvec, i), | |
a1281f45 KL |
10582 | UNIFY_ALLOW_NONE)) |
10583 | return 1; | |
73b0fce8 | 10584 | } |
a1281f45 | 10585 | } |
b429fdf0 | 10586 | arg = TYPE_TI_TEMPLATE (arg); |
a1281f45 KL |
10587 | |
10588 | /* Fall through to deduce template name. */ | |
10589 | } | |
10590 | ||
10591 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM | |
10592 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
10593 | { | |
10594 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ | |
10595 | ||
10596 | /* Simple cases: Value already set, does match or doesn't. */ | |
10597 | if (targ != NULL_TREE && template_args_equal (targ, arg)) | |
10598 | return 0; | |
10599 | else if (targ) | |
10600 | return 1; | |
db2767b6 MM |
10601 | } |
10602 | else | |
10603 | { | |
830bfa74 MM |
10604 | /* If PARM is `const T' and ARG is only `int', we don't have |
10605 | a match unless we are allowing additional qualification. | |
10606 | If ARG is `const int' and PARM is just `T' that's OK; | |
10607 | that binds `const int' to `T'. */ | |
c8094d83 | 10608 | if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
830bfa74 | 10609 | arg, parm)) |
db2767b6 MM |
10610 | return 1; |
10611 | ||
830bfa74 MM |
10612 | /* Consider the case where ARG is `const volatile int' and |
10613 | PARM is `const T'. Then, T should be `volatile int'. */ | |
c2ea3a40 NS |
10614 | arg = cp_build_qualified_type_real |
10615 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); | |
adecb3f4 MM |
10616 | if (arg == error_mark_node) |
10617 | return 1; | |
73b0fce8 | 10618 | |
a1281f45 KL |
10619 | /* Simple cases: Value already set, does match or doesn't. */ |
10620 | if (targ != NULL_TREE && same_type_p (targ, arg)) | |
10621 | return 0; | |
10622 | else if (targ) | |
10623 | return 1; | |
61cd552e | 10624 | |
94fc547c MM |
10625 | /* Make sure that ARG is not a variable-sized array. (Note |
10626 | that were talking about variable-sized arrays (like | |
10627 | `int[n]'), rather than arrays of unknown size (like | |
10628 | `int[]').) We'll get very confused by such a type since | |
10629 | the bound of the array will not be computable in an | |
10630 | instantiation. Besides, such types are not allowed in | |
10631 | ISO C++, so we can do as we please here. */ | |
5377d5ba | 10632 | if (variably_modified_type_p (arg, NULL_TREE)) |
94fc547c MM |
10633 | return 1; |
10634 | } | |
61cd552e | 10635 | |
916b63c3 | 10636 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
73b0fce8 KL |
10637 | return 0; |
10638 | ||
f84b4be9 | 10639 | case TEMPLATE_PARM_INDEX: |
db2767b6 | 10640 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
3e9ac7e5 VR |
10641 | if (tparm == error_mark_node) |
10642 | return 1; | |
db2767b6 | 10643 | |
c8094d83 | 10644 | if (TEMPLATE_PARM_LEVEL (parm) |
db2767b6 MM |
10645 | != template_decl_level (tparm)) |
10646 | /* The PARM is not one we're trying to unify. Just check | |
10647 | to see if it matches ARG. */ | |
c8a209ca NS |
10648 | return !(TREE_CODE (arg) == TREE_CODE (parm) |
10649 | && cp_tree_equal (parm, arg)); | |
db2767b6 | 10650 | |
f84b4be9 | 10651 | idx = TEMPLATE_PARM_IDX (parm); |
916b63c3 | 10652 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
db2767b6 | 10653 | |
050367a3 | 10654 | if (targ) |
c8a209ca | 10655 | return !cp_tree_equal (targ, arg); |
8d08fdba | 10656 | |
161c12b0 JM |
10657 | /* [temp.deduct.type] If, in the declaration of a function template |
10658 | with a non-type template-parameter, the non-type | |
10659 | template-parameter is used in an expression in the function | |
10660 | parameter-list and, if the corresponding template-argument is | |
10661 | deduced, the template-argument type shall match the type of the | |
10662 | template-parameter exactly, except that a template-argument | |
c8094d83 | 10663 | deduced from an array bound may be of any integral type. |
d7c4edd0 | 10664 | The non-type parameter might use already deduced type parameters. */ |
bd0d5d4a | 10665 | tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE); |
e2005c8d KL |
10666 | if (!TREE_TYPE (arg)) |
10667 | /* Template-parameter dependent expression. Just accept it for now. | |
10668 | It will later be processed in convert_template_argument. */ | |
10669 | ; | |
10670 | else if (same_type_p (TREE_TYPE (arg), tparm)) | |
10671 | /* OK */; | |
161c12b0 | 10672 | else if ((strict & UNIFY_ALLOW_INTEGER) |
bd0d5d4a JM |
10673 | && (TREE_CODE (tparm) == INTEGER_TYPE |
10674 | || TREE_CODE (tparm) == BOOLEAN_TYPE)) | |
8baddbf1 MM |
10675 | /* Convert the ARG to the type of PARM; the deduced non-type |
10676 | template argument must exactly match the types of the | |
10677 | corresponding parameter. */ | |
10678 | arg = fold (build_nop (TREE_TYPE (parm), arg)); | |
bd0d5d4a JM |
10679 | else if (uses_template_parms (tparm)) |
10680 | /* We haven't deduced the type of this parameter yet. Try again | |
10681 | later. */ | |
10682 | return 0; | |
161c12b0 JM |
10683 | else |
10684 | return 1; | |
10685 | ||
916b63c3 | 10686 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
8d08fdba MS |
10687 | return 0; |
10688 | ||
28e8f3a0 GB |
10689 | case PTRMEM_CST: |
10690 | { | |
0cbd7506 MS |
10691 | /* A pointer-to-member constant can be unified only with |
10692 | another constant. */ | |
28e8f3a0 | 10693 | if (TREE_CODE (arg) != PTRMEM_CST) |
0cbd7506 | 10694 | return 1; |
28e8f3a0 GB |
10695 | |
10696 | /* Just unify the class member. It would be useless (and possibly | |
0cbd7506 MS |
10697 | wrong, depending on the strict flags) to unify also |
10698 | PTRMEM_CST_CLASS, because we want to be sure that both parm and | |
10699 | arg refer to the same variable, even if through different | |
10700 | classes. For instance: | |
28e8f3a0 | 10701 | |
0cbd7506 MS |
10702 | struct A { int x; }; |
10703 | struct B : A { }; | |
28e8f3a0 | 10704 | |
0cbd7506 | 10705 | Unification of &A::x and &B::x must succeed. */ |
28e8f3a0 | 10706 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
0cbd7506 | 10707 | PTRMEM_CST_MEMBER (arg), strict); |
28e8f3a0 GB |
10708 | } |
10709 | ||
8d08fdba | 10710 | case POINTER_TYPE: |
830bfa74 | 10711 | { |
830bfa74 MM |
10712 | if (TREE_CODE (arg) != POINTER_TYPE) |
10713 | return 1; | |
c8094d83 | 10714 | |
830bfa74 MM |
10715 | /* [temp.deduct.call] |
10716 | ||
10717 | A can be another pointer or pointer to member type that can | |
10718 | be converted to the deduced A via a qualification | |
10719 | conversion (_conv.qual_). | |
10720 | ||
10721 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. | |
10722 | This will allow for additional cv-qualification of the | |
028d1f20 | 10723 | pointed-to types if appropriate. */ |
c8094d83 | 10724 | |
028d1f20 | 10725 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
830bfa74 MM |
10726 | /* The derived-to-base conversion only persists through one |
10727 | level of pointers. */ | |
028d1f20 | 10728 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
830bfa74 | 10729 | |
c8094d83 | 10730 | return unify (tparms, targs, TREE_TYPE (parm), |
028d1f20 | 10731 | TREE_TYPE (arg), strict); |
830bfa74 | 10732 | } |
8d08fdba MS |
10733 | |
10734 | case REFERENCE_TYPE: | |
830bfa74 MM |
10735 | if (TREE_CODE (arg) != REFERENCE_TYPE) |
10736 | return 1; | |
10737 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), | |
028d1f20 | 10738 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
10739 | |
10740 | case ARRAY_TYPE: | |
10741 | if (TREE_CODE (arg) != ARRAY_TYPE) | |
10742 | return 1; | |
3042d5be MM |
10743 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
10744 | != (TYPE_DOMAIN (arg) == NULL_TREE)) | |
10745 | return 1; | |
8baddbf1 MM |
10746 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
10747 | { | |
10748 | tree parm_max; | |
10749 | tree arg_max; | |
6ee3ffe8 MM |
10750 | bool parm_cst; |
10751 | bool arg_cst; | |
8baddbf1 MM |
10752 | |
10753 | /* Our representation of array types uses "N - 1" as the | |
10754 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is | |
6ee3ffe8 MM |
10755 | not an integer constant. We cannot unify arbitrarily |
10756 | complex expressions, so we eliminate the MINUS_EXPRs | |
10757 | here. */ | |
10758 | parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm)); | |
10759 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; | |
10760 | if (!parm_cst) | |
8baddbf1 | 10761 | { |
6ee3ffe8 | 10762 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
8baddbf1 MM |
10763 | parm_max = TREE_OPERAND (parm_max, 0); |
10764 | } | |
6ee3ffe8 MM |
10765 | arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg)); |
10766 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; | |
10767 | if (!arg_cst) | |
10768 | { | |
10769 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are | |
10770 | trying to unify the type of a variable with the type | |
10771 | of a template parameter. For example: | |
10772 | ||
10773 | template <unsigned int N> | |
10774 | void f (char (&) [N]); | |
10775 | int g(); | |
10776 | void h(int i) { | |
10777 | char a[g(i)]; | |
10778 | f(a); | |
10779 | } | |
10780 | ||
10781 | Here, the type of the ARG will be "int [g(i)]", and | |
10782 | may be a SAVE_EXPR, etc. */ | |
10783 | if (TREE_CODE (arg_max) != MINUS_EXPR) | |
10784 | return 1; | |
10785 | arg_max = TREE_OPERAND (arg_max, 0); | |
10786 | } | |
10787 | ||
10788 | /* If only one of the bounds used a MINUS_EXPR, compensate | |
10789 | by adding one to the other bound. */ | |
10790 | if (parm_cst && !arg_cst) | |
10791 | parm_max = fold_build2 (PLUS_EXPR, | |
10792 | integer_type_node, | |
10793 | parm_max, | |
10794 | integer_one_node); | |
10795 | else if (arg_cst && !parm_cst) | |
10796 | arg_max = fold_build2 (PLUS_EXPR, | |
10797 | integer_type_node, | |
10798 | arg_max, | |
10799 | integer_one_node); | |
8baddbf1 MM |
10800 | |
10801 | if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER)) | |
10802 | return 1; | |
10803 | } | |
830bfa74 | 10804 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
712467a4 | 10805 | strict & UNIFY_ALLOW_MORE_CV_QUAL); |
8d08fdba MS |
10806 | |
10807 | case REAL_TYPE: | |
37c46b43 | 10808 | case COMPLEX_TYPE: |
c00996a3 | 10809 | case VECTOR_TYPE: |
8d08fdba | 10810 | case INTEGER_TYPE: |
42976354 | 10811 | case BOOLEAN_TYPE: |
3590f0a6 | 10812 | case ENUMERAL_TYPE: |
5ad5a526 | 10813 | case VOID_TYPE: |
f376e137 MS |
10814 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
10815 | return 1; | |
c8094d83 | 10816 | |
9edc3913 | 10817 | /* We have already checked cv-qualification at the top of the |
514a1f18 | 10818 | function. */ |
8baddbf1 | 10819 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
ca79f85d JM |
10820 | return 1; |
10821 | ||
8d08fdba MS |
10822 | /* As far as unification is concerned, this wins. Later checks |
10823 | will invalidate it if necessary. */ | |
10824 | return 0; | |
10825 | ||
10826 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ | |
bd6dd845 | 10827 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
8d08fdba | 10828 | case INTEGER_CST: |
bd6dd845 MS |
10829 | while (TREE_CODE (arg) == NOP_EXPR) |
10830 | arg = TREE_OPERAND (arg, 0); | |
10831 | ||
8d08fdba MS |
10832 | if (TREE_CODE (arg) != INTEGER_CST) |
10833 | return 1; | |
10834 | return !tree_int_cst_equal (parm, arg); | |
10835 | ||
8d08fdba MS |
10836 | case TREE_VEC: |
10837 | { | |
10838 | int i; | |
10839 | if (TREE_CODE (arg) != TREE_VEC) | |
10840 | return 1; | |
10841 | if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg)) | |
10842 | return 1; | |
0dc09a61 | 10843 | for (i = 0; i < TREE_VEC_LENGTH (parm); ++i) |
830bfa74 | 10844 | if (unify (tparms, targs, |
8d08fdba | 10845 | TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i), |
4393e105 | 10846 | UNIFY_ALLOW_NONE)) |
8d08fdba MS |
10847 | return 1; |
10848 | return 0; | |
10849 | } | |
10850 | ||
8d08fdba | 10851 | case RECORD_TYPE: |
f181d4ae | 10852 | case UNION_TYPE: |
f181d4ae | 10853 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
a4443a08 | 10854 | return 1; |
c8094d83 | 10855 | |
a7a64a77 MM |
10856 | if (TYPE_PTRMEMFUNC_P (parm)) |
10857 | { | |
10858 | if (!TYPE_PTRMEMFUNC_P (arg)) | |
10859 | return 1; | |
10860 | ||
c8094d83 | 10861 | return unify (tparms, targs, |
a7a64a77 MM |
10862 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
10863 | TYPE_PTRMEMFUNC_FN_TYPE (arg), | |
10864 | strict); | |
10865 | } | |
10866 | ||
5db698f6 | 10867 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
5566b478 | 10868 | { |
6467930b | 10869 | tree t = NULL_TREE; |
4393e105 | 10870 | |
028d1f20 | 10871 | if (strict_in & UNIFY_ALLOW_DERIVED) |
4393e105 MM |
10872 | { |
10873 | /* First, we try to unify the PARM and ARG directly. */ | |
10874 | t = try_class_unification (tparms, targs, | |
10875 | parm, arg); | |
10876 | ||
10877 | if (!t) | |
10878 | { | |
10879 | /* Fallback to the special case allowed in | |
10880 | [temp.deduct.call]: | |
c8094d83 | 10881 | |
4393e105 MM |
10882 | If P is a class, and P has the form |
10883 | template-id, then A can be a derived class of | |
10884 | the deduced A. Likewise, if P is a pointer to | |
10885 | a class of the form template-id, A can be a | |
10886 | pointer to a derived class pointed to by the | |
10887 | deduced A. */ | |
8d83768f | 10888 | t = get_template_base (tparms, targs, parm, arg); |
4393e105 | 10889 | |
8d83768f | 10890 | if (!t) |
4393e105 MM |
10891 | return 1; |
10892 | } | |
10893 | } | |
c8094d83 MS |
10894 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
10895 | && (CLASSTYPE_TI_TEMPLATE (parm) | |
9fbf56f7 | 10896 | == CLASSTYPE_TI_TEMPLATE (arg))) |
6df47b06 MM |
10897 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
10898 | Then, we should unify `int' and `U'. */ | |
6467930b | 10899 | t = arg; |
4393e105 | 10900 | else |
dc957d14 | 10901 | /* There's no chance of unification succeeding. */ |
5566b478 | 10902 | return 1; |
6467930b | 10903 | |
830bfa74 | 10904 | return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), |
4393e105 | 10905 | CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE); |
5566b478 | 10906 | } |
9edc3913 | 10907 | else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
5566b478 | 10908 | return 1; |
a4443a08 | 10909 | return 0; |
8d08fdba MS |
10910 | |
10911 | case METHOD_TYPE: | |
8d08fdba | 10912 | case FUNCTION_TYPE: |
830bfa74 | 10913 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8d08fdba | 10914 | return 1; |
830bfa74 | 10915 | |
38d18b1a | 10916 | /* CV qualifications for methods can never be deduced, they must |
3db45ab5 MS |
10917 | match exactly. We need to check them explicitly here, |
10918 | because type_unification_real treats them as any other | |
10919 | cvqualified parameter. */ | |
38d18b1a NS |
10920 | if (TREE_CODE (parm) == METHOD_TYPE |
10921 | && (!check_cv_quals_for_unify | |
10922 | (UNIFY_ALLOW_NONE, | |
10923 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))), | |
10924 | TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm)))))) | |
10925 | return 1; | |
10926 | ||
830bfa74 | 10927 | if (unify (tparms, targs, TREE_TYPE (parm), |
4393e105 | 10928 | TREE_TYPE (arg), UNIFY_ALLOW_NONE)) |
28cbf42c | 10929 | return 1; |
386b8a85 | 10930 | return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm), |
30f86ec3 FJ |
10931 | TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT, |
10932 | LOOKUP_NORMAL); | |
a4443a08 MS |
10933 | |
10934 | case OFFSET_TYPE: | |
9804209d | 10935 | /* Unify a pointer to member with a pointer to member function, which |
0cbd7506 | 10936 | deduces the type of the member as a function type. */ |
9804209d | 10937 | if (TYPE_PTRMEMFUNC_P (arg)) |
0cbd7506 MS |
10938 | { |
10939 | tree method_type; | |
10940 | tree fntype; | |
10941 | cp_cv_quals cv_quals; | |
10942 | ||
10943 | /* Check top-level cv qualifiers */ | |
10944 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) | |
10945 | return 1; | |
10946 | ||
10947 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), | |
10948 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE)) | |
10949 | return 1; | |
10950 | ||
10951 | /* Determine the type of the function we are unifying against. */ | |
10952 | method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg)); | |
10953 | fntype = | |
10954 | build_function_type (TREE_TYPE (method_type), | |
10955 | TREE_CHAIN (TYPE_ARG_TYPES (method_type))); | |
10956 | ||
10957 | /* Extract the cv-qualifiers of the member function from the | |
10958 | implicit object parameter and place them on the function | |
10959 | type to be restored later. */ | |
10960 | cv_quals = | |
10961 | cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type)))); | |
10962 | fntype = build_qualified_type (fntype, cv_quals); | |
10963 | return unify (tparms, targs, TREE_TYPE (parm), fntype, strict); | |
10964 | } | |
9804209d | 10965 | |
a4443a08 MS |
10966 | if (TREE_CODE (arg) != OFFSET_TYPE) |
10967 | return 1; | |
830bfa74 | 10968 | if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
4393e105 | 10969 | TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE)) |
a4443a08 | 10970 | return 1; |
830bfa74 | 10971 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
4393e105 | 10972 | strict); |
a4443a08 | 10973 | |
f62dbf03 | 10974 | case CONST_DECL: |
a723baf1 MM |
10975 | if (DECL_TEMPLATE_PARM_P (parm)) |
10976 | return unify (tparms, targs, DECL_INITIAL (parm), arg, strict); | |
c8094d83 | 10977 | if (arg != integral_constant_value (parm)) |
f62dbf03 JM |
10978 | return 1; |
10979 | return 0; | |
10980 | ||
28e8f3a0 | 10981 | case FIELD_DECL: |
027905b4 KL |
10982 | case TEMPLATE_DECL: |
10983 | /* Matched cases are handled by the ARG == PARM test above. */ | |
10984 | return 1; | |
10985 | ||
8d08fdba | 10986 | default: |
6615c446 | 10987 | gcc_assert (EXPR_P (parm)); |
c8094d83 | 10988 | |
98ddffc1 | 10989 | /* We must be looking at an expression. This can happen with |
c8094d83 MS |
10990 | something like: |
10991 | ||
98ddffc1 NS |
10992 | template <int I> |
10993 | void foo(S<I>, S<I + 2>); | |
050367a3 | 10994 | |
98ddffc1 | 10995 | This is a "nondeduced context": |
050367a3 | 10996 | |
98ddffc1 | 10997 | [deduct.type] |
c8094d83 | 10998 | |
98ddffc1 | 10999 | The nondeduced contexts are: |
050367a3 | 11000 | |
98ddffc1 NS |
11001 | --A type that is a template-id in which one or more of |
11002 | the template-arguments is an expression that references | |
c8094d83 | 11003 | a template-parameter. |
050367a3 | 11004 | |
98ddffc1 NS |
11005 | In these cases, we assume deduction succeeded, but don't |
11006 | actually infer any unifications. */ | |
74601d7c | 11007 | |
98ddffc1 NS |
11008 | if (!uses_template_parms (parm) |
11009 | && !template_args_equal (parm, arg)) | |
11010 | return 1; | |
11011 | else | |
11012 | return 0; | |
8d08fdba MS |
11013 | } |
11014 | } | |
8d08fdba | 11015 | \f |
4684cd27 MM |
11016 | /* Note that DECL can be defined in this translation unit, if |
11017 | required. */ | |
11018 | ||
11019 | static void | |
11020 | mark_definable (tree decl) | |
11021 | { | |
11022 | tree clone; | |
11023 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
11024 | FOR_EACH_CLONE (clone, decl) | |
11025 | DECL_NOT_REALLY_EXTERN (clone) = 1; | |
11026 | } | |
11027 | ||
03d0f4af | 11028 | /* Called if RESULT is explicitly instantiated, or is a member of an |
4684cd27 | 11029 | explicitly instantiated class. */ |
03d0f4af | 11030 | |
faae18ab | 11031 | void |
3a978d72 | 11032 | mark_decl_instantiated (tree result, int extern_p) |
faae18ab | 11033 | { |
415c974c | 11034 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
3ae18eaf | 11035 | |
1f6f0cb6 MM |
11036 | /* If this entity has already been written out, it's too late to |
11037 | make any modifications. */ | |
11038 | if (TREE_ASM_WRITTEN (result)) | |
11039 | return; | |
11040 | ||
11041 | if (TREE_CODE (result) != FUNCTION_DECL) | |
11042 | /* The TREE_PUBLIC flag for function declarations will have been | |
11043 | set correctly by tsubst. */ | |
11044 | TREE_PUBLIC (result) = 1; | |
11045 | ||
346eeb15 JM |
11046 | /* This might have been set by an earlier implicit instantiation. */ |
11047 | DECL_COMDAT (result) = 0; | |
11048 | ||
4684cd27 MM |
11049 | if (extern_p) |
11050 | DECL_NOT_REALLY_EXTERN (result) = 0; | |
11051 | else | |
faae18ab | 11052 | { |
4684cd27 | 11053 | mark_definable (result); |
1a408d07 JM |
11054 | /* Always make artificials weak. */ |
11055 | if (DECL_ARTIFICIAL (result) && flag_weak) | |
11056 | comdat_linkage (result); | |
a7d87521 JM |
11057 | /* For WIN32 we also want to put explicit instantiations in |
11058 | linkonce sections. */ | |
1a408d07 | 11059 | else if (TREE_PUBLIC (result)) |
b385c841 | 11060 | maybe_make_one_only (result); |
faae18ab | 11061 | } |
c8094d83 | 11062 | |
4684cd27 MM |
11063 | /* If EXTERN_P, then this function will not be emitted -- unless |
11064 | followed by an explicit instantiation, at which point its linkage | |
11065 | will be adjusted. If !EXTERN_P, then this function will be | |
11066 | emitted here. In neither circumstance do we want | |
11067 | import_export_decl to adjust the linkage. */ | |
c8094d83 | 11068 | DECL_INTERFACE_KNOWN (result) = 1; |
faae18ab MS |
11069 | } |
11070 | ||
e5214479 | 11071 | /* Given two function templates PAT1 and PAT2, return: |
6467930b MS |
11072 | |
11073 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. | |
11074 | -1 if PAT2 is more specialized than PAT1. | |
e5214479 JM |
11075 | 0 if neither is more specialized. |
11076 | ||
dda04398 NS |
11077 | LEN indicates the number of parameters we should consider |
11078 | (defaulted parameters should not be considered). | |
11079 | ||
11080 | The 1998 std underspecified function template partial ordering, and | |
11081 | DR214 addresses the issue. We take pairs of arguments, one from | |
c51940a2 | 11082 | each of the templates, and deduce them against each other. One of |
dda04398 NS |
11083 | the templates will be more specialized if all the *other* |
11084 | template's arguments deduce against its arguments and at least one | |
11085 | of its arguments *does* *not* deduce against the other template's | |
11086 | corresponding argument. Deduction is done as for class templates. | |
11087 | The arguments used in deduction have reference and top level cv | |
11088 | qualifiers removed. Iff both arguments were originally reference | |
11089 | types *and* deduction succeeds in both directions, the template | |
11090 | with the more cv-qualified argument wins for that pairing (if | |
11091 | neither is more cv-qualified, they both are equal). Unlike regular | |
11092 | deduction, after all the arguments have been deduced in this way, | |
11093 | we do *not* verify the deduced template argument values can be | |
11094 | substituted into non-deduced contexts, nor do we have to verify | |
11095 | that all template arguments have been deduced. */ | |
c8094d83 | 11096 | |
6467930b | 11097 | int |
dda04398 NS |
11098 | more_specialized_fn (tree pat1, tree pat2, int len) |
11099 | { | |
11100 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); | |
11101 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); | |
11102 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); | |
11103 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); | |
11104 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); | |
11105 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); | |
11106 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); | |
11107 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); | |
11108 | int better1 = 0; | |
11109 | int better2 = 0; | |
3db45ab5 | 11110 | |
48884537 NS |
11111 | /* Remove the this parameter from non-static member functions. If |
11112 | one is a non-static member function and the other is not a static | |
11113 | member function, remove the first parameter from that function | |
11114 | also. This situation occurs for operator functions where we | |
11115 | locate both a member function (with this pointer) and non-member | |
11116 | operator (with explicit first operand). */ | |
dda04398 | 11117 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1)) |
48884537 NS |
11118 | { |
11119 | len--; /* LEN is the number of significant arguments for DECL1 */ | |
11120 | args1 = TREE_CHAIN (args1); | |
11121 | if (!DECL_STATIC_FUNCTION_P (decl2)) | |
11122 | args2 = TREE_CHAIN (args2); | |
11123 | } | |
11124 | else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2)) | |
11125 | { | |
11126 | args2 = TREE_CHAIN (args2); | |
11127 | if (!DECL_STATIC_FUNCTION_P (decl1)) | |
11128 | { | |
11129 | len--; | |
11130 | args1 = TREE_CHAIN (args1); | |
11131 | } | |
11132 | } | |
3db45ab5 | 11133 | |
ee307009 NS |
11134 | /* If only one is a conversion operator, they are unordered. */ |
11135 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) | |
11136 | return 0; | |
c8094d83 | 11137 | |
dda04398 NS |
11138 | /* Consider the return type for a conversion function */ |
11139 | if (DECL_CONV_FN_P (decl1)) | |
11140 | { | |
dda04398 NS |
11141 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
11142 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); | |
11143 | len++; | |
11144 | } | |
c8094d83 | 11145 | |
dda04398 | 11146 | processing_template_decl++; |
c8094d83 | 11147 | |
dda04398 NS |
11148 | while (len--) |
11149 | { | |
11150 | tree arg1 = TREE_VALUE (args1); | |
11151 | tree arg2 = TREE_VALUE (args2); | |
11152 | int deduce1, deduce2; | |
11153 | int quals1 = -1; | |
11154 | int quals2 = -1; | |
6467930b | 11155 | |
dda04398 NS |
11156 | if (TREE_CODE (arg1) == REFERENCE_TYPE) |
11157 | { | |
11158 | arg1 = TREE_TYPE (arg1); | |
11159 | quals1 = cp_type_quals (arg1); | |
11160 | } | |
c8094d83 | 11161 | |
dda04398 NS |
11162 | if (TREE_CODE (arg2) == REFERENCE_TYPE) |
11163 | { | |
11164 | arg2 = TREE_TYPE (arg2); | |
11165 | quals2 = cp_type_quals (arg2); | |
11166 | } | |
6467930b | 11167 | |
dda04398 NS |
11168 | if ((quals1 < 0) != (quals2 < 0)) |
11169 | { | |
11170 | /* Only of the args is a reference, see if we should apply | |
11171 | array/function pointer decay to it. This is not part of | |
11172 | DR214, but is, IMHO, consistent with the deduction rules | |
11173 | for the function call itself, and with our earlier | |
11174 | implementation of the underspecified partial ordering | |
11175 | rules. (nathan). */ | |
11176 | if (quals1 >= 0) | |
11177 | { | |
11178 | switch (TREE_CODE (arg1)) | |
11179 | { | |
11180 | case ARRAY_TYPE: | |
11181 | arg1 = TREE_TYPE (arg1); | |
11182 | /* FALLTHROUGH. */ | |
11183 | case FUNCTION_TYPE: | |
11184 | arg1 = build_pointer_type (arg1); | |
11185 | break; | |
c8094d83 | 11186 | |
dda04398 NS |
11187 | default: |
11188 | break; | |
11189 | } | |
11190 | } | |
11191 | else | |
11192 | { | |
11193 | switch (TREE_CODE (arg2)) | |
11194 | { | |
11195 | case ARRAY_TYPE: | |
11196 | arg2 = TREE_TYPE (arg2); | |
11197 | /* FALLTHROUGH. */ | |
11198 | case FUNCTION_TYPE: | |
11199 | arg2 = build_pointer_type (arg2); | |
11200 | break; | |
c8094d83 | 11201 | |
dda04398 NS |
11202 | default: |
11203 | break; | |
11204 | } | |
11205 | } | |
11206 | } | |
c8094d83 | 11207 | |
dda04398 NS |
11208 | arg1 = TYPE_MAIN_VARIANT (arg1); |
11209 | arg2 = TYPE_MAIN_VARIANT (arg2); | |
c8094d83 | 11210 | |
dda04398 NS |
11211 | deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE); |
11212 | deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE); | |
11213 | ||
11214 | if (!deduce1) | |
11215 | better2 = -1; | |
11216 | if (!deduce2) | |
11217 | better1 = -1; | |
11218 | if (better1 < 0 && better2 < 0) | |
11219 | /* We've failed to deduce something in either direction. | |
11220 | These must be unordered. */ | |
11221 | break; | |
c8094d83 | 11222 | |
dda04398 NS |
11223 | if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0) |
11224 | { | |
11225 | /* Deduces in both directions, see if quals can | |
11226 | disambiguate. Pretend the worse one failed to deduce. */ | |
11227 | if ((quals1 & quals2) == quals2) | |
11228 | deduce1 = 0; | |
11229 | if ((quals1 & quals2) == quals1) | |
11230 | deduce2 = 0; | |
11231 | } | |
11232 | if (deduce1 && !deduce2 && !better2) | |
11233 | better2 = 1; | |
11234 | if (deduce2 && !deduce1 && !better1) | |
11235 | better1 = 1; | |
c8094d83 | 11236 | |
dda04398 NS |
11237 | args1 = TREE_CHAIN (args1); |
11238 | args2 = TREE_CHAIN (args2); | |
11239 | } | |
11240 | ||
11241 | processing_template_decl--; | |
11242 | ||
11243 | return (better1 > 0) - (better2 > 0); | |
73aad9b9 | 11244 | } |
6467930b | 11245 | |
916b63c3 | 11246 | /* Determine which of two partial specializations is more specialized. |
6467930b | 11247 | |
916b63c3 MM |
11248 | PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding |
11249 | to the first partial specialization. The TREE_VALUE is the | |
11250 | innermost set of template parameters for the partial | |
11251 | specialization. PAT2 is similar, but for the second template. | |
11252 | ||
11253 | Return 1 if the first partial specialization is more specialized; | |
11254 | -1 if the second is more specialized; 0 if neither is more | |
11255 | specialized. | |
55ece1b3 | 11256 | |
916b63c3 | 11257 | See [temp.class.order] for information about determining which of |
3db45ab5 | 11258 | two templates is more specialized. */ |
c8094d83 | 11259 | |
b5791fdc | 11260 | static int |
916b63c3 | 11261 | more_specialized_class (tree pat1, tree pat2) |
73aad9b9 JM |
11262 | { |
11263 | tree targs; | |
916b63c3 | 11264 | tree tmpl1, tmpl2; |
73aad9b9 JM |
11265 | int winner = 0; |
11266 | ||
916b63c3 MM |
11267 | tmpl1 = TREE_TYPE (pat1); |
11268 | tmpl2 = TREE_TYPE (pat2); | |
11269 | ||
c8094d83 | 11270 | /* Just like what happens for functions, if we are ordering between |
baa49730 GB |
11271 | different class template specializations, we may encounter dependent |
11272 | types in the arguments, and we need our dependency check functions | |
11273 | to behave correctly. */ | |
11274 | ++processing_template_decl; | |
3db45ab5 | 11275 | targs = get_class_bindings (TREE_VALUE (pat1), |
916b63c3 MM |
11276 | CLASSTYPE_TI_ARGS (tmpl1), |
11277 | CLASSTYPE_TI_ARGS (tmpl2)); | |
73aad9b9 JM |
11278 | if (targs) |
11279 | --winner; | |
11280 | ||
3db45ab5 | 11281 | targs = get_class_bindings (TREE_VALUE (pat2), |
916b63c3 MM |
11282 | CLASSTYPE_TI_ARGS (tmpl2), |
11283 | CLASSTYPE_TI_ARGS (tmpl1)); | |
73aad9b9 | 11284 | if (targs) |
6467930b | 11285 | ++winner; |
baa49730 | 11286 | --processing_template_decl; |
6467930b MS |
11287 | |
11288 | return winner; | |
11289 | } | |
73aad9b9 JM |
11290 | |
11291 | /* Return the template arguments that will produce the function signature | |
e1467ff2 | 11292 | DECL from the function template FN, with the explicit template |
a34d3336 | 11293 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
4393e105 | 11294 | also match. Return NULL_TREE if no satisfactory arguments could be |
a34d3336 | 11295 | found. */ |
c8094d83 | 11296 | |
76b9a14d | 11297 | static tree |
a34d3336 | 11298 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
73aad9b9 | 11299 | { |
98c1c668 | 11300 | int ntparms = DECL_NTPARMS (fn); |
f31c0a32 | 11301 | tree targs = make_tree_vec (ntparms); |
4393e105 | 11302 | tree decl_type; |
03017874 | 11303 | tree decl_arg_types; |
98c1c668 | 11304 | |
4393e105 MM |
11305 | /* Substitute the explicit template arguments into the type of DECL. |
11306 | The call to fn_type_unification will handle substitution into the | |
11307 | FN. */ | |
11308 | decl_type = TREE_TYPE (decl); | |
11309 | if (explicit_args && uses_template_parms (decl_type)) | |
11310 | { | |
11311 | tree tmpl; | |
11312 | tree converted_args; | |
11313 | ||
11314 | if (DECL_TEMPLATE_INFO (decl)) | |
11315 | tmpl = DECL_TI_TEMPLATE (decl); | |
11316 | else | |
0e339752 | 11317 | /* We can get here for some invalid specializations. */ |
4393e105 MM |
11318 | return NULL_TREE; |
11319 | ||
11320 | converted_args | |
e7e93965 MM |
11321 | = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
11322 | explicit_args, NULL_TREE, | |
3db45ab5 | 11323 | tf_none, |
e7e93965 MM |
11324 | /*require_all_args=*/false, |
11325 | /*use_default_args=*/false); | |
4393e105 MM |
11326 | if (converted_args == error_mark_node) |
11327 | return NULL_TREE; | |
c8094d83 MS |
11328 | |
11329 | decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE); | |
4393e105 MM |
11330 | if (decl_type == error_mark_node) |
11331 | return NULL_TREE; | |
11332 | } | |
11333 | ||
11334 | decl_arg_types = TYPE_ARG_TYPES (decl_type); | |
e5214479 JM |
11335 | /* Never do unification on the 'this' parameter. */ |
11336 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) | |
03017874 | 11337 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
d7684f2d | 11338 | |
c8094d83 | 11339 | if (fn_type_unification (fn, explicit_args, targs, |
03017874 | 11340 | decl_arg_types, |
8d3631f8 | 11341 | (check_rettype || DECL_CONV_FN_P (fn) |
0cbd7506 | 11342 | ? TREE_TYPE (decl_type) : NULL_TREE), |
30f86ec3 | 11343 | DEDUCE_EXACT, LOOKUP_NORMAL)) |
76b9a14d JM |
11344 | return NULL_TREE; |
11345 | ||
76b9a14d JM |
11346 | return targs; |
11347 | } | |
11348 | ||
36a117a5 MM |
11349 | /* Return the innermost template arguments that, when applied to a |
11350 | template specialization whose innermost template parameters are | |
9471d3e2 | 11351 | TPARMS, and whose specialization arguments are PARMS, yield the |
c8094d83 | 11352 | ARGS. |
36a117a5 MM |
11353 | |
11354 | For example, suppose we have: | |
11355 | ||
11356 | template <class T, class U> struct S {}; | |
11357 | template <class T> struct S<T*, int> {}; | |
11358 | ||
11359 | Then, suppose we want to get `S<double*, int>'. The TPARMS will be | |
916b63c3 | 11360 | {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*, |
36a117a5 MM |
11361 | int}. The resulting vector will be {double}, indicating that `T' |
11362 | is bound to `double'. */ | |
11363 | ||
bd6dd845 | 11364 | static tree |
916b63c3 | 11365 | get_class_bindings (tree tparms, tree spec_args, tree args) |
73aad9b9 | 11366 | { |
3b3ba9f0 | 11367 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
916b63c3 MM |
11368 | tree deduced_args; |
11369 | tree innermost_deduced_args; | |
73aad9b9 | 11370 | |
916b63c3 MM |
11371 | innermost_deduced_args = make_tree_vec (ntparms); |
11372 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) | |
11373 | { | |
11374 | deduced_args = copy_node (args); | |
11375 | SET_TMPL_ARGS_LEVEL (deduced_args, | |
11376 | TMPL_ARGS_DEPTH (deduced_args), | |
11377 | innermost_deduced_args); | |
11378 | } | |
11379 | else | |
3db45ab5 | 11380 | deduced_args = innermost_deduced_args; |
916b63c3 | 11381 | |
3db45ab5 MS |
11382 | if (unify (tparms, deduced_args, |
11383 | INNERMOST_TEMPLATE_ARGS (spec_args), | |
11384 | INNERMOST_TEMPLATE_ARGS (args), | |
0cbd7506 | 11385 | UNIFY_ALLOW_NONE)) |
fcfb9f96 | 11386 | return NULL_TREE; |
73aad9b9 JM |
11387 | |
11388 | for (i = 0; i < ntparms; ++i) | |
916b63c3 | 11389 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
73aad9b9 JM |
11390 | return NULL_TREE; |
11391 | ||
916b63c3 MM |
11392 | /* Verify that nondeduced template arguments agree with the type |
11393 | obtained from argument deduction. | |
3db45ab5 | 11394 | |
916b63c3 MM |
11395 | For example: |
11396 | ||
11397 | struct A { typedef int X; }; | |
11398 | template <class T, class U> struct C {}; | |
11399 | template <class T> struct C<T, typename T::X> {}; | |
11400 | ||
11401 | Then with the instantiation `C<A, int>', we can deduce that | |
11402 | `T' is `A' but unify () does not check whether `typename T::X' | |
11403 | is `int'. */ | |
11404 | spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE); | |
11405 | if (spec_args == error_mark_node | |
11406 | /* We only need to check the innermost arguments; the other | |
11407 | arguments will always agree. */ | |
11408 | || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args), | |
11409 | INNERMOST_TEMPLATE_ARGS (args))) | |
74601d7c KL |
11410 | return NULL_TREE; |
11411 | ||
916b63c3 | 11412 | return deduced_args; |
73aad9b9 JM |
11413 | } |
11414 | ||
7ca383e6 MM |
11415 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
11416 | Return the TREE_LIST node with the most specialized template, if | |
11417 | any. If there is no most specialized template, the error_mark_node | |
11418 | is returned. | |
11419 | ||
11420 | Note that this function does not look at, or modify, the | |
11421 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node | |
11422 | returned is one of the elements of INSTANTIATIONS, callers may | |
11423 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, | |
11424 | and retrieve it from the value returned. */ | |
73aad9b9 JM |
11425 | |
11426 | tree | |
7ca383e6 | 11427 | most_specialized_instantiation (tree templates) |
73aad9b9 | 11428 | { |
104bf76a | 11429 | tree fn, champ; |
73aad9b9 | 11430 | |
dda04398 | 11431 | ++processing_template_decl; |
c8094d83 | 11432 | |
7ca383e6 MM |
11433 | champ = templates; |
11434 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) | |
73aad9b9 | 11435 | { |
dda04398 | 11436 | int fate = 0; |
c8094d83 | 11437 | |
a34d3336 NS |
11438 | if (get_bindings (TREE_VALUE (champ), |
11439 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
11440 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
11441 | fate--; |
11442 | ||
a34d3336 NS |
11443 | if (get_bindings (TREE_VALUE (fn), |
11444 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
11445 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 | 11446 | fate++; |
c8094d83 | 11447 | |
8ca4bf25 MM |
11448 | if (fate == -1) |
11449 | champ = fn; | |
11450 | else if (!fate) | |
73aad9b9 | 11451 | { |
8ca4bf25 MM |
11452 | /* Equally specialized, move to next function. If there |
11453 | is no next function, nothing's most specialized. */ | |
11454 | fn = TREE_CHAIN (fn); | |
7ca383e6 | 11455 | champ = fn; |
8ca4bf25 MM |
11456 | if (!fn) |
11457 | break; | |
73aad9b9 JM |
11458 | } |
11459 | } | |
c8094d83 | 11460 | |
dda04398 NS |
11461 | if (champ) |
11462 | /* Now verify that champ is better than everything earlier in the | |
11463 | instantiation list. */ | |
7ca383e6 | 11464 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) |
a34d3336 NS |
11465 | if (get_bindings (TREE_VALUE (champ), |
11466 | DECL_TEMPLATE_RESULT (TREE_VALUE (fn)), | |
11467 | NULL_TREE, /*check_ret=*/false) | |
11468 | || !get_bindings (TREE_VALUE (fn), | |
11469 | DECL_TEMPLATE_RESULT (TREE_VALUE (champ)), | |
11470 | NULL_TREE, /*check_ret=*/false)) | |
dda04398 NS |
11471 | { |
11472 | champ = NULL_TREE; | |
11473 | break; | |
11474 | } | |
c8094d83 | 11475 | |
dda04398 | 11476 | processing_template_decl--; |
c8094d83 | 11477 | |
dda04398 NS |
11478 | if (!champ) |
11479 | return error_mark_node; | |
73aad9b9 | 11480 | |
7ca383e6 | 11481 | return champ; |
73aad9b9 JM |
11482 | } |
11483 | ||
36a117a5 | 11484 | /* If DECL is a specialization of some template, return the most |
f9a7ae04 MM |
11485 | general such template. Otherwise, returns NULL_TREE. |
11486 | ||
11487 | For example, given: | |
36a117a5 MM |
11488 | |
11489 | template <class T> struct S { template <class U> void f(U); }; | |
11490 | ||
11491 | if TMPL is `template <class U> void S<int>::f(U)' this will return | |
11492 | the full template. This function will not trace past partial | |
11493 | specializations, however. For example, given in addition: | |
11494 | ||
11495 | template <class T> struct S<T*> { template <class U> void f(U); }; | |
11496 | ||
11497 | if TMPL is `template <class U> void S<int*>::f(U)' this will return | |
11498 | `template <class T> template <class U> S<T*>::f(U)'. */ | |
73aad9b9 | 11499 | |
612c671a | 11500 | tree |
3a978d72 | 11501 | most_general_template (tree decl) |
73aad9b9 | 11502 | { |
f9a7ae04 MM |
11503 | /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is |
11504 | an immediate specialization. */ | |
11505 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
11506 | { | |
11507 | if (DECL_TEMPLATE_INFO (decl)) { | |
11508 | decl = DECL_TI_TEMPLATE (decl); | |
11509 | ||
11510 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a | |
11511 | template friend. */ | |
11512 | if (TREE_CODE (decl) != TEMPLATE_DECL) | |
11513 | return NULL_TREE; | |
11514 | } else | |
11515 | return NULL_TREE; | |
11516 | } | |
11517 | ||
11518 | /* Look for more and more general templates. */ | |
11519 | while (DECL_TEMPLATE_INFO (decl)) | |
11520 | { | |
10b1d5e7 MM |
11521 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
11522 | (See cp-tree.h for details.) */ | |
f9a7ae04 MM |
11523 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
11524 | break; | |
11525 | ||
6e049fcd KL |
11526 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
11527 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
11528 | break; | |
11529 | ||
f9a7ae04 MM |
11530 | /* Stop if we run into an explicitly specialized class template. */ |
11531 | if (!DECL_NAMESPACE_SCOPE_P (decl) | |
11532 | && DECL_CONTEXT (decl) | |
11533 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) | |
11534 | break; | |
11535 | ||
11536 | decl = DECL_TI_TEMPLATE (decl); | |
11537 | } | |
36a117a5 MM |
11538 | |
11539 | return decl; | |
11540 | } | |
11541 | ||
916b63c3 MM |
11542 | /* Return the most specialized of the class template partial |
11543 | specializations of TMPL which can produce TYPE, a specialization of | |
11544 | TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is | |
11545 | a _TYPE node corresponding to the partial specialization, while the | |
11546 | TREE_PURPOSE is the set of template arguments that must be | |
11547 | substituted into the TREE_TYPE in order to generate TYPE. | |
11548 | ||
11549 | If the choice of partial specialization is ambiguous, a diagnostic | |
11550 | is issued, and the error_mark_node is returned. If there are no | |
11551 | partial specializations of TMPL matching TYPE, then NULL_TREE is | |
11552 | returned. */ | |
36a117a5 | 11553 | |
e9659ab0 | 11554 | static tree |
916b63c3 | 11555 | most_specialized_class (tree type, tree tmpl) |
36a117a5 MM |
11556 | { |
11557 | tree list = NULL_TREE; | |
11558 | tree t; | |
11559 | tree champ; | |
73aad9b9 | 11560 | int fate; |
916b63c3 MM |
11561 | bool ambiguous_p; |
11562 | tree args; | |
73aad9b9 | 11563 | |
36a117a5 | 11564 | tmpl = most_general_template (tmpl); |
916b63c3 | 11565 | args = CLASSTYPE_TI_ARGS (type); |
36a117a5 | 11566 | for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t)) |
73aad9b9 | 11567 | { |
916b63c3 MM |
11568 | tree partial_spec_args; |
11569 | tree spec_args; | |
11570 | ||
11571 | partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t)); | |
3db45ab5 MS |
11572 | spec_args = get_class_bindings (TREE_VALUE (t), |
11573 | partial_spec_args, | |
916b63c3 | 11574 | args); |
36a117a5 | 11575 | if (spec_args) |
73aad9b9 | 11576 | { |
916b63c3 | 11577 | list = tree_cons (spec_args, TREE_VALUE (t), list); |
73aad9b9 JM |
11578 | TREE_TYPE (list) = TREE_TYPE (t); |
11579 | } | |
11580 | } | |
11581 | ||
11582 | if (! list) | |
11583 | return NULL_TREE; | |
11584 | ||
916b63c3 | 11585 | ambiguous_p = false; |
73aad9b9 JM |
11586 | t = list; |
11587 | champ = t; | |
11588 | t = TREE_CHAIN (t); | |
11589 | for (; t; t = TREE_CHAIN (t)) | |
11590 | { | |
916b63c3 | 11591 | fate = more_specialized_class (champ, t); |
73aad9b9 JM |
11592 | if (fate == 1) |
11593 | ; | |
11594 | else | |
11595 | { | |
11596 | if (fate == 0) | |
11597 | { | |
11598 | t = TREE_CHAIN (t); | |
11599 | if (! t) | |
916b63c3 MM |
11600 | { |
11601 | ambiguous_p = true; | |
11602 | break; | |
11603 | } | |
73aad9b9 JM |
11604 | } |
11605 | champ = t; | |
11606 | } | |
11607 | } | |
11608 | ||
916b63c3 MM |
11609 | if (!ambiguous_p) |
11610 | for (t = list; t && t != champ; t = TREE_CHAIN (t)) | |
11611 | { | |
11612 | fate = more_specialized_class (champ, t); | |
11613 | if (fate != 1) | |
11614 | { | |
11615 | ambiguous_p = true; | |
11616 | break; | |
11617 | } | |
11618 | } | |
11619 | ||
11620 | if (ambiguous_p) | |
73aad9b9 | 11621 | { |
916b63c3 MM |
11622 | const char *str = "candidates are:"; |
11623 | error ("ambiguous class template instantiation for %q#T", type); | |
11624 | for (t = list; t; t = TREE_CHAIN (t)) | |
11625 | { | |
11626 | error ("%s %+#T", str, TREE_TYPE (t)); | |
11627 | str = " "; | |
11628 | } | |
11629 | return error_mark_node; | |
73aad9b9 JM |
11630 | } |
11631 | ||
11632 | return champ; | |
11633 | } | |
11634 | ||
eb8845be | 11635 | /* Explicitly instantiate DECL. */ |
e92cc029 | 11636 | |
8d08fdba | 11637 | void |
eb8845be | 11638 | do_decl_instantiation (tree decl, tree storage) |
8d08fdba | 11639 | { |
8d08fdba | 11640 | tree result = NULL_TREE; |
faae18ab | 11641 | int extern_p = 0; |
e8abc66f | 11642 | |
ad47b891 | 11643 | if (!decl || decl == error_mark_node) |
dc957d14 | 11644 | /* An error occurred, for which grokdeclarator has already issued |
3fa56191 MM |
11645 | an appropriate message. */ |
11646 | return; | |
11647 | else if (! DECL_LANG_SPECIFIC (decl)) | |
ec255269 | 11648 | { |
0f51ccfc | 11649 | error ("explicit instantiation of non-template %q#D", decl); |
ec255269 MS |
11650 | return; |
11651 | } | |
03d0f4af | 11652 | else if (TREE_CODE (decl) == VAR_DECL) |
6633d636 | 11653 | { |
03d0f4af MM |
11654 | /* There is an asymmetry here in the way VAR_DECLs and |
11655 | FUNCTION_DECLs are handled by grokdeclarator. In the case of | |
11656 | the latter, the DECL we get back will be marked as a | |
11657 | template instantiation, and the appropriate | |
11658 | DECL_TEMPLATE_INFO will be set up. This does not happen for | |
11659 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator | |
11660 | should handle VAR_DECLs as it currently handles | |
11661 | FUNCTION_DECLs. */ | |
86ac0575 | 11662 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
e0860732 | 11663 | if (!result || TREE_CODE (result) != VAR_DECL) |
03d0f4af | 11664 | { |
0f51ccfc | 11665 | error ("no matching template for %qD found", decl); |
03d0f4af MM |
11666 | return; |
11667 | } | |
6633d636 MS |
11668 | } |
11669 | else if (TREE_CODE (decl) != FUNCTION_DECL) | |
11670 | { | |
0f51ccfc | 11671 | error ("explicit instantiation of %q#D", decl); |
6633d636 MS |
11672 | return; |
11673 | } | |
03d0f4af MM |
11674 | else |
11675 | result = decl; | |
672476cb | 11676 | |
03d0f4af | 11677 | /* Check for various error cases. Note that if the explicit |
0e339752 | 11678 | instantiation is valid the RESULT will currently be marked as an |
03d0f4af MM |
11679 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
11680 | until we get here. */ | |
11681 | ||
11682 | if (DECL_TEMPLATE_SPECIALIZATION (result)) | |
672476cb | 11683 | { |
07782718 KL |
11684 | /* DR 259 [temp.spec]. |
11685 | ||
11686 | Both an explicit instantiation and a declaration of an explicit | |
11687 | specialization shall not appear in a program unless the explicit | |
11688 | instantiation follows a declaration of the explicit specialization. | |
03d0f4af | 11689 | |
07782718 KL |
11690 | For a given set of template parameters, if an explicit |
11691 | instantiation of a template appears after a declaration of an | |
11692 | explicit specialization for that template, the explicit | |
11693 | instantiation has no effect. */ | |
672476cb MM |
11694 | return; |
11695 | } | |
03d0f4af MM |
11696 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
11697 | { | |
11698 | /* [temp.spec] | |
98c1c668 | 11699 | |
03d0f4af | 11700 | No program shall explicitly instantiate any template more |
c8094d83 | 11701 | than once. |
03d0f4af | 11702 | |
4684cd27 MM |
11703 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
11704 | the first instantiation was `extern' and the second is not, | |
11705 | and EXTERN_P for the opposite case. */ | |
11706 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) | |
0f51ccfc | 11707 | pedwarn ("duplicate explicit instantiation of %q#D", result); |
4684cd27 MM |
11708 | /* If an "extern" explicit instantiation follows an ordinary |
11709 | explicit instantiation, the template is instantiated. */ | |
11710 | if (extern_p) | |
03d0f4af MM |
11711 | return; |
11712 | } | |
11713 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) | |
faae18ab | 11714 | { |
0f51ccfc | 11715 | error ("no matching template for %qD found", result); |
faae18ab MS |
11716 | return; |
11717 | } | |
03d0f4af | 11718 | else if (!DECL_TEMPLATE_INFO (result)) |
6633d636 | 11719 | { |
0f51ccfc | 11720 | pedwarn ("explicit instantiation of non-template %q#D", result); |
6633d636 MS |
11721 | return; |
11722 | } | |
11723 | ||
f0e01782 | 11724 | if (storage == NULL_TREE) |
00595019 | 11725 | ; |
faae18ab | 11726 | else if (storage == ridpointers[(int) RID_EXTERN]) |
03d0f4af | 11727 | { |
c02f5e29 | 11728 | if (pedantic && !in_system_header) |
0f51ccfc | 11729 | pedwarn ("ISO C++ forbids the use of %<extern%> on explicit " |
0cbd7506 | 11730 | "instantiations"); |
03d0f4af MM |
11731 | extern_p = 1; |
11732 | } | |
f0e01782 | 11733 | else |
0f51ccfc | 11734 | error ("storage class %qD applied to template instantiation", storage); |
3db45ab5 | 11735 | |
9c12301f | 11736 | check_explicit_instantiation_namespace (result); |
5566b478 | 11737 | mark_decl_instantiated (result, extern_p); |
c91a56d2 | 11738 | if (! extern_p) |
3db45ab5 | 11739 | instantiate_decl (result, /*defer_ok=*/1, |
eba839f9 | 11740 | /*expl_inst_class_mem_p=*/false); |
7177d104 MS |
11741 | } |
11742 | ||
b5791fdc | 11743 | static void |
3a978d72 | 11744 | mark_class_instantiated (tree t, int extern_p) |
faae18ab MS |
11745 | { |
11746 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); | |
eb773359 JM |
11747 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
11748 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; | |
faae18ab MS |
11749 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
11750 | if (! extern_p) | |
11751 | { | |
11752 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; | |
11753 | rest_of_type_compilation (t, 1); | |
11754 | } | |
c8094d83 | 11755 | } |
e8abc66f | 11756 | |
5e0c54e5 | 11757 | /* Called from do_type_instantiation through binding_table_foreach to |
9bcb9aae | 11758 | do recursive instantiation for the type bound in ENTRY. */ |
5e0c54e5 GDR |
11759 | static void |
11760 | bt_instantiate_type_proc (binding_entry entry, void *data) | |
11761 | { | |
11762 | tree storage = *(tree *) data; | |
11763 | ||
11764 | if (IS_AGGR_TYPE (entry->type) | |
11765 | && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type))) | |
11766 | do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0); | |
11767 | } | |
11768 | ||
415c974c MA |
11769 | /* Called from do_type_instantiation to instantiate a member |
11770 | (a member function or a static member variable) of an | |
03fd3f84 | 11771 | explicitly instantiated class template. */ |
415c974c MA |
11772 | static void |
11773 | instantiate_class_member (tree decl, int extern_p) | |
11774 | { | |
11775 | mark_decl_instantiated (decl, extern_p); | |
415c974c | 11776 | if (! extern_p) |
3db45ab5 | 11777 | instantiate_decl (decl, /*defer_ok=*/1, |
eba839f9 | 11778 | /*expl_inst_class_mem_p=*/true); |
415c974c MA |
11779 | } |
11780 | ||
a1bcc528 JM |
11781 | /* Perform an explicit instantiation of template class T. STORAGE, if |
11782 | non-null, is the RID for extern, inline or static. COMPLAIN is | |
838dfd8a | 11783 | nonzero if this is called from the parser, zero if called recursively, |
a1bcc528 | 11784 | since the standard is unclear (as detailed below). */ |
c8094d83 | 11785 | |
7177d104 | 11786 | void |
3a978d72 | 11787 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
7177d104 | 11788 | { |
e8abc66f MS |
11789 | int extern_p = 0; |
11790 | int nomem_p = 0; | |
5566b478 | 11791 | int static_p = 0; |
4746cf84 | 11792 | int previous_instantiation_extern_p = 0; |
5566b478 | 11793 | |
ca79f85d JM |
11794 | if (TREE_CODE (t) == TYPE_DECL) |
11795 | t = TREE_TYPE (t); | |
11796 | ||
7ddedda4 | 11797 | if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t)) |
ca79f85d | 11798 | { |
0f51ccfc | 11799 | error ("explicit instantiation of non-template type %qT", t); |
ca79f85d JM |
11800 | return; |
11801 | } | |
11802 | ||
5566b478 | 11803 | complete_type (t); |
7177d104 | 11804 | |
d0f062fb | 11805 | if (!COMPLETE_TYPE_P (t)) |
f0e01782 | 11806 | { |
c2ea3a40 | 11807 | if (complain & tf_error) |
0f51ccfc | 11808 | error ("explicit instantiation of %q#T before definition of template", |
0cbd7506 | 11809 | t); |
f0e01782 MS |
11810 | return; |
11811 | } | |
11812 | ||
03d0f4af | 11813 | if (storage != NULL_TREE) |
f0e01782 | 11814 | { |
c02f5e29 | 11815 | if (pedantic && !in_system_header) |
c8094d83 | 11816 | pedwarn("ISO C++ forbids the use of %qE on explicit instantiations", |
0cbd7506 | 11817 | storage); |
03d0f4af MM |
11818 | |
11819 | if (storage == ridpointers[(int) RID_INLINE]) | |
11820 | nomem_p = 1; | |
11821 | else if (storage == ridpointers[(int) RID_EXTERN]) | |
11822 | extern_p = 1; | |
11823 | else if (storage == ridpointers[(int) RID_STATIC]) | |
11824 | static_p = 1; | |
11825 | else | |
11826 | { | |
0f51ccfc | 11827 | error ("storage class %qD applied to template instantiation", |
0cbd7506 | 11828 | storage); |
03d0f4af MM |
11829 | extern_p = 0; |
11830 | } | |
f0e01782 MS |
11831 | } |
11832 | ||
370af2d5 | 11833 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
03d0f4af | 11834 | { |
07782718 | 11835 | /* DR 259 [temp.spec]. |
a292b002 | 11836 | |
07782718 KL |
11837 | Both an explicit instantiation and a declaration of an explicit |
11838 | specialization shall not appear in a program unless the explicit | |
11839 | instantiation follows a declaration of the explicit specialization. | |
11840 | ||
11841 | For a given set of template parameters, if an explicit | |
11842 | instantiation of a template appears after a declaration of an | |
11843 | explicit specialization for that template, the explicit | |
11844 | instantiation has no effect. */ | |
03d0f4af MM |
11845 | return; |
11846 | } | |
11847 | else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t)) | |
44a8d0b3 | 11848 | { |
03d0f4af MM |
11849 | /* [temp.spec] |
11850 | ||
11851 | No program shall explicitly instantiate any template more | |
c8094d83 | 11852 | than once. |
03d0f4af | 11853 | |
0cbd7506 | 11854 | If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit |
4746cf84 | 11855 | instantiation was `extern'. If EXTERN_P then the second is. |
4684cd27 | 11856 | These cases are OK. */ |
4746cf84 MA |
11857 | previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t); |
11858 | ||
11859 | if (!previous_instantiation_extern_p && !extern_p | |
c2ea3a40 | 11860 | && (complain & tf_error)) |
0f51ccfc | 11861 | pedwarn ("duplicate explicit instantiation of %q#T", t); |
c8094d83 | 11862 | |
03d0f4af MM |
11863 | /* If we've already instantiated the template, just return now. */ |
11864 | if (!CLASSTYPE_INTERFACE_ONLY (t)) | |
11865 | return; | |
44a8d0b3 | 11866 | } |
e8abc66f | 11867 | |
9c12301f | 11868 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
03d0f4af | 11869 | mark_class_instantiated (t, extern_p); |
03d0f4af | 11870 | |
e8abc66f MS |
11871 | if (nomem_p) |
11872 | return; | |
11873 | ||
7177d104 | 11874 | { |
db5ae43f | 11875 | tree tmp; |
5566b478 | 11876 | |
03d0f4af MM |
11877 | /* In contrast to implicit instantiation, where only the |
11878 | declarations, and not the definitions, of members are | |
11879 | instantiated, we have here: | |
11880 | ||
0cbd7506 | 11881 | [temp.explicit] |
03d0f4af MM |
11882 | |
11883 | The explicit instantiation of a class template specialization | |
11884 | implies the instantiation of all of its members not | |
11885 | previously explicitly specialized in the translation unit | |
c8094d83 | 11886 | containing the explicit instantiation. |
03d0f4af MM |
11887 | |
11888 | Of course, we can't instantiate member template classes, since | |
11889 | we don't have any arguments for them. Note that the standard | |
dc957d14 | 11890 | is unclear on whether the instantiation of the members are |
415c974c | 11891 | *explicit* instantiations or not. However, the most natural |
03fd3f84 | 11892 | interpretation is that it should be an explicit instantiation. */ |
03d0f4af | 11893 | |
5566b478 MS |
11894 | if (! static_p) |
11895 | for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
a7d87521 | 11896 | if (TREE_CODE (tmp) == FUNCTION_DECL |
1f109f0f | 11897 | && DECL_TEMPLATE_INSTANTIATION (tmp)) |
415c974c | 11898 | instantiate_class_member (tmp, extern_p); |
5566b478 MS |
11899 | |
11900 | for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp)) | |
11901 | if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp)) | |
415c974c | 11902 | instantiate_class_member (tmp, extern_p); |
7177d104 | 11903 | |
5e0c54e5 GDR |
11904 | if (CLASSTYPE_NESTED_UTDS (t)) |
11905 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (t), | |
0cbd7506 | 11906 | bt_instantiate_type_proc, &storage); |
a292b002 | 11907 | } |
8d08fdba | 11908 | } |
a28e3c7f | 11909 | |
36a117a5 MM |
11910 | /* Given a function DECL, which is a specialization of TMPL, modify |
11911 | DECL to be a re-instantiation of TMPL with the same template | |
11912 | arguments. TMPL should be the template into which tsubst'ing | |
11913 | should occur for DECL, not the most general template. | |
830bfa74 MM |
11914 | |
11915 | One reason for doing this is a scenario like this: | |
11916 | ||
11917 | template <class T> | |
11918 | void f(const T&, int i); | |
11919 | ||
11920 | void g() { f(3, 7); } | |
11921 | ||
11922 | template <class T> | |
11923 | void f(const T& t, const int i) { } | |
11924 | ||
11925 | Note that when the template is first instantiated, with | |
11926 | instantiate_template, the resulting DECL will have no name for the | |
11927 | first parameter, and the wrong type for the second. So, when we go | |
11928 | to instantiate the DECL, we regenerate it. */ | |
11929 | ||
e9659ab0 | 11930 | static void |
3a978d72 | 11931 | regenerate_decl_from_template (tree decl, tree tmpl) |
830bfa74 | 11932 | { |
f9a7ae04 MM |
11933 | /* The arguments used to instantiate DECL, from the most general |
11934 | template. */ | |
830bfa74 | 11935 | tree args; |
830bfa74 | 11936 | tree code_pattern; |
830bfa74 MM |
11937 | |
11938 | args = DECL_TI_ARGS (decl); | |
11939 | code_pattern = DECL_TEMPLATE_RESULT (tmpl); | |
11940 | ||
2b907f5c KL |
11941 | /* Make sure that we can see identifiers, and compute access |
11942 | correctly. */ | |
11943 | push_access_scope (decl); | |
11944 | ||
c7222c02 MM |
11945 | if (TREE_CODE (decl) == FUNCTION_DECL) |
11946 | { | |
11947 | tree decl_parm; | |
11948 | tree pattern_parm; | |
11949 | tree specs; | |
11950 | int args_depth; | |
11951 | int parms_depth; | |
c8094d83 | 11952 | |
c7222c02 | 11953 | args_depth = TMPL_ARGS_DEPTH (args); |
c8094d83 | 11954 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
c7222c02 MM |
11955 | if (args_depth > parms_depth) |
11956 | args = get_innermost_template_args (args, parms_depth); | |
11957 | ||
11958 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), | |
11959 | args, tf_error, NULL_TREE); | |
11960 | if (specs) | |
11961 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), | |
11962 | specs); | |
11963 | ||
11964 | /* Merge parameter declarations. */ | |
c8094d83 | 11965 | decl_parm = skip_artificial_parms_for (decl, |
c7222c02 | 11966 | DECL_ARGUMENTS (decl)); |
c8094d83 | 11967 | pattern_parm |
c7222c02 MM |
11968 | = skip_artificial_parms_for (code_pattern, |
11969 | DECL_ARGUMENTS (code_pattern)); | |
11970 | while (decl_parm) | |
11971 | { | |
11972 | tree parm_type; | |
b17bba6d | 11973 | tree attributes; |
c7222c02 MM |
11974 | |
11975 | if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm)) | |
11976 | DECL_NAME (decl_parm) = DECL_NAME (pattern_parm); | |
11977 | parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error, | |
11978 | NULL_TREE); | |
02bab9db | 11979 | parm_type = type_decays_to (parm_type); |
c7222c02 MM |
11980 | if (!same_type_p (TREE_TYPE (decl_parm), parm_type)) |
11981 | TREE_TYPE (decl_parm) = parm_type; | |
b17bba6d MM |
11982 | attributes = DECL_ATTRIBUTES (pattern_parm); |
11983 | if (DECL_ATTRIBUTES (decl_parm) != attributes) | |
11984 | { | |
11985 | DECL_ATTRIBUTES (decl_parm) = attributes; | |
11986 | cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0); | |
11987 | } | |
c7222c02 MM |
11988 | decl_parm = TREE_CHAIN (decl_parm); |
11989 | pattern_parm = TREE_CHAIN (pattern_parm); | |
11990 | } | |
830bfa74 | 11991 | |
c7222c02 MM |
11992 | /* Merge additional specifiers from the CODE_PATTERN. */ |
11993 | if (DECL_DECLARED_INLINE_P (code_pattern) | |
11994 | && !DECL_DECLARED_INLINE_P (decl)) | |
11995 | DECL_DECLARED_INLINE_P (decl) = 1; | |
11996 | if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl)) | |
11997 | DECL_INLINE (decl) = 1; | |
11998 | } | |
11999 | else if (TREE_CODE (decl) == VAR_DECL) | |
b794e321 MM |
12000 | DECL_INITIAL (decl) = |
12001 | tsubst_expr (DECL_INITIAL (code_pattern), args, | |
015c2c66 MM |
12002 | tf_error, DECL_TI_TEMPLATE (decl), |
12003 | /*integral_constant_expression_p=*/false); | |
c7222c02 MM |
12004 | else |
12005 | gcc_unreachable (); | |
36a117a5 | 12006 | |
2b59fc25 | 12007 | pop_access_scope (decl); |
830bfa74 MM |
12008 | } |
12009 | ||
a723baf1 MM |
12010 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
12011 | substituted to get DECL. */ | |
12012 | ||
d58b7c2d | 12013 | tree |
a723baf1 MM |
12014 | template_for_substitution (tree decl) |
12015 | { | |
12016 | tree tmpl = DECL_TI_TEMPLATE (decl); | |
12017 | ||
12018 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern | |
12019 | for the instantiation. This is not always the most general | |
12020 | template. Consider, for example: | |
12021 | ||
0cbd7506 | 12022 | template <class T> |
a723baf1 | 12023 | struct S { template <class U> void f(); |
0cbd7506 | 12024 | template <> void f<int>(); }; |
a723baf1 MM |
12025 | |
12026 | and an instantiation of S<double>::f<int>. We want TD to be the | |
12027 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ | |
12028 | while (/* An instantiation cannot have a definition, so we need a | |
12029 | more general template. */ | |
12030 | DECL_TEMPLATE_INSTANTIATION (tmpl) | |
12031 | /* We must also deal with friend templates. Given: | |
12032 | ||
c8094d83 | 12033 | template <class T> struct S { |
a723baf1 MM |
12034 | template <class U> friend void f() {}; |
12035 | }; | |
12036 | ||
12037 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, | |
12038 | so far as the language is concerned, but that's still | |
12039 | where we get the pattern for the instantiation from. On | |
12040 | other hand, if the definition comes outside the class, say: | |
12041 | ||
c8094d83 | 12042 | template <class T> struct S { |
a723baf1 MM |
12043 | template <class U> friend void f(); |
12044 | }; | |
12045 | template <class U> friend void f() {} | |
12046 | ||
12047 | we don't need to look any further. That's what the check for | |
12048 | DECL_INITIAL is for. */ | |
12049 | || (TREE_CODE (decl) == FUNCTION_DECL | |
12050 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) | |
12051 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) | |
12052 | { | |
12053 | /* The present template, TD, should not be a definition. If it | |
12054 | were a definition, we should be using it! Note that we | |
12055 | cannot restructure the loop to just keep going until we find | |
12056 | a template with a definition, since that might go too far if | |
12057 | a specialization was declared, but not defined. */ | |
50bc768d NS |
12058 | gcc_assert (TREE_CODE (decl) != VAR_DECL |
12059 | || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))); | |
c8094d83 | 12060 | |
a723baf1 MM |
12061 | /* Fetch the more general template. */ |
12062 | tmpl = DECL_TI_TEMPLATE (tmpl); | |
12063 | } | |
12064 | ||
12065 | return tmpl; | |
12066 | } | |
12067 | ||
16d53b64 | 12068 | /* Produce the definition of D, a _DECL generated from a template. If |
838dfd8a | 12069 | DEFER_OK is nonzero, then we don't have to actually do the |
415c974c MA |
12070 | instantiation now; we just have to do it sometime. Normally it is |
12071 | an error if this is an explicit instantiation but D is undefined. | |
eba839f9 MM |
12072 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an |
12073 | explicitly instantiated class template. */ | |
f84b4be9 | 12074 | |
a28e3c7f | 12075 | tree |
3db45ab5 | 12076 | instantiate_decl (tree d, int defer_ok, |
eba839f9 | 12077 | bool expl_inst_class_mem_p) |
a28e3c7f | 12078 | { |
36a117a5 | 12079 | tree tmpl = DECL_TI_TEMPLATE (d); |
65f8b0fb MM |
12080 | tree gen_args; |
12081 | tree args; | |
830bfa74 | 12082 | tree td; |
36a117a5 MM |
12083 | tree code_pattern; |
12084 | tree spec; | |
12085 | tree gen_tmpl; | |
b794e321 | 12086 | bool pattern_defined; |
31a714f6 | 12087 | int need_push; |
82a98427 | 12088 | location_t saved_loc = input_location; |
f7e4e484 | 12089 | bool external_p; |
c8094d83 | 12090 | |
36a117a5 MM |
12091 | /* This function should only be used to instantiate templates for |
12092 | functions and static member variables. */ | |
50bc768d NS |
12093 | gcc_assert (TREE_CODE (d) == FUNCTION_DECL |
12094 | || TREE_CODE (d) == VAR_DECL); | |
36a117a5 | 12095 | |
cec24319 MM |
12096 | /* Variables are never deferred; if instantiation is required, they |
12097 | are instantiated right away. That allows for better code in the | |
12098 | case that an expression refers to the value of the variable -- | |
12099 | if the variable has a constant value the referring expression can | |
12100 | take advantage of that fact. */ | |
12101 | if (TREE_CODE (d) == VAR_DECL) | |
12102 | defer_ok = 0; | |
12103 | ||
db9b2174 MM |
12104 | /* Don't instantiate cloned functions. Instead, instantiate the |
12105 | functions they cloned. */ | |
12106 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) | |
12107 | d = DECL_CLONED_FUNCTION (d); | |
12108 | ||
fbf1c34b | 12109 | if (DECL_TEMPLATE_INSTANTIATED (d)) |
03d0f4af | 12110 | /* D has already been instantiated. It might seem reasonable to |
dc957d14 | 12111 | check whether or not D is an explicit instantiation, and, if so, |
03d0f4af MM |
12112 | stop here. But when an explicit instantiation is deferred |
12113 | until the end of the compilation, DECL_EXPLICIT_INSTANTIATION | |
12114 | is set, even though we still need to do the instantiation. */ | |
36a117a5 MM |
12115 | return d; |
12116 | ||
12117 | /* If we already have a specialization of this declaration, then | |
12118 | there's no reason to instantiate it. Note that | |
12119 | retrieve_specialization gives us both instantiations and | |
12120 | specializations, so we must explicitly check | |
12121 | DECL_TEMPLATE_SPECIALIZATION. */ | |
12122 | gen_tmpl = most_general_template (tmpl); | |
65f8b0fb | 12123 | gen_args = DECL_TI_ARGS (d); |
c7222c02 MM |
12124 | spec = retrieve_specialization (gen_tmpl, gen_args, |
12125 | /*class_specializations_p=*/false); | |
36a117a5 MM |
12126 | if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec)) |
12127 | return spec; | |
12128 | ||
12129 | /* This needs to happen before any tsubsting. */ | |
12130 | if (! push_tinst_level (d)) | |
12131 | return d; | |
12132 | ||
297a5329 JM |
12133 | timevar_push (TV_PARSE); |
12134 | ||
4d85e00e | 12135 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
a723baf1 MM |
12136 | for the instantiation. */ |
12137 | td = template_for_substitution (d); | |
fee23f54 | 12138 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27bb8339 | 12139 | |
2d22db1f | 12140 | /* We should never be trying to instantiate a member of a class |
3db45ab5 | 12141 | template or partial specialization. */ |
2d22db1f | 12142 | gcc_assert (d != code_pattern); |
3db45ab5 | 12143 | |
76d3baad KL |
12144 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
12145 | || DECL_TEMPLATE_SPECIALIZATION (td)) | |
649fc72d NS |
12146 | /* In the case of a friend template whose definition is provided |
12147 | outside the class, we may have too many arguments. Drop the | |
76d3baad | 12148 | ones we don't need. The same is true for specializations. */ |
649fc72d NS |
12149 | args = get_innermost_template_args |
12150 | (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); | |
12151 | else | |
12152 | args = gen_args; | |
65f8b0fb | 12153 | |
5566b478 | 12154 | if (TREE_CODE (d) == FUNCTION_DECL) |
39c76b4f | 12155 | pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE); |
5566b478 | 12156 | else |
36a117a5 | 12157 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
b794e321 MM |
12158 | |
12159 | /* We may be in the middle of deferred access check. Disable it now. */ | |
12160 | push_deferring_access_checks (dk_no_deferred); | |
12161 | ||
4684cd27 MM |
12162 | /* Unless an explicit instantiation directive has already determined |
12163 | the linkage of D, remember that a definition is available for | |
12164 | this entity. */ | |
c8094d83 | 12165 | if (pattern_defined |
4684cd27 MM |
12166 | && !DECL_INTERFACE_KNOWN (d) |
12167 | && !DECL_NOT_REALLY_EXTERN (d)) | |
12168 | mark_definable (d); | |
de22184b | 12169 | |
f31686a3 | 12170 | input_location = DECL_SOURCE_LOCATION (d); |
de22184b | 12171 | |
eba839f9 MM |
12172 | /* If D is a member of an explicitly instantiated class template, |
12173 | and no definition is available, treat it like an implicit | |
3db45ab5 MS |
12174 | instantiation. */ |
12175 | if (!pattern_defined && expl_inst_class_mem_p | |
12176 | && DECL_EXPLICIT_INSTANTIATION (d)) | |
5566b478 | 12177 | { |
4684cd27 | 12178 | DECL_NOT_REALLY_EXTERN (d) = 0; |
eba839f9 | 12179 | DECL_INTERFACE_KNOWN (d) = 0; |
4684cd27 | 12180 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
5566b478 MS |
12181 | } |
12182 | ||
4f2b0fb2 NS |
12183 | if (!defer_ok) |
12184 | { | |
12185 | /* Recheck the substitutions to obtain any warning messages | |
12186 | about ignoring cv qualifiers. */ | |
12187 | tree gen = DECL_TEMPLATE_RESULT (gen_tmpl); | |
12188 | tree type = TREE_TYPE (gen); | |
12189 | ||
0e902d98 | 12190 | /* Make sure that we can see identifiers, and compute access |
2b59fc25 KL |
12191 | correctly. D is already the target FUNCTION_DECL with the |
12192 | right context. */ | |
12193 | push_access_scope (d); | |
0e902d98 | 12194 | |
4f2b0fb2 NS |
12195 | if (TREE_CODE (gen) == FUNCTION_DECL) |
12196 | { | |
23fca1f5 | 12197 | tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d); |
65f8b0fb | 12198 | tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args, |
23fca1f5 | 12199 | tf_warning_or_error, d); |
4f2b0fb2 NS |
12200 | /* Don't simply tsubst the function type, as that will give |
12201 | duplicate warnings about poor parameter qualifications. | |
12202 | The function arguments are the same as the decl_arguments | |
c6002625 | 12203 | without the top level cv qualifiers. */ |
4f2b0fb2 NS |
12204 | type = TREE_TYPE (type); |
12205 | } | |
23fca1f5 | 12206 | tsubst (type, gen_args, tf_warning_or_error, d); |
0e902d98 | 12207 | |
2b59fc25 | 12208 | pop_access_scope (d); |
4f2b0fb2 | 12209 | } |
c8094d83 | 12210 | |
f7e4e484 MM |
12211 | /* Check to see whether we know that this template will be |
12212 | instantiated in some other file, as with "extern template" | |
12213 | extension. */ | |
12214 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); | |
12215 | /* In general, we do not instantiate such templates... */ | |
12216 | if (external_p | |
12217 | /* ... but we instantiate inline functions so that we can inline | |
12218 | them and ... */ | |
12219 | && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)) | |
12220 | /* ... we instantiate static data members whose values are | |
12221 | needed in integral constant expressions. */ | |
3db45ab5 | 12222 | && ! (TREE_CODE (d) == VAR_DECL |
f7e4e484 | 12223 | && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d))) |
930cd796 | 12224 | goto out; |
16d53b64 | 12225 | /* Defer all other templates, unless we have been explicitly |
f7e4e484 MM |
12226 | forbidden from doing so. */ |
12227 | if (/* If there is no definition, we cannot instantiate the | |
12228 | template. */ | |
3db45ab5 | 12229 | ! pattern_defined |
f7e4e484 MM |
12230 | /* If it's OK to postpone instantiation, do so. */ |
12231 | || defer_ok | |
12232 | /* If this is a static data member that will be defined | |
12233 | elsewhere, we don't want to instantiate the entire data | |
12234 | member, but we do want to instantiate the initializer so that | |
12235 | we can substitute that elsewhere. */ | |
12236 | || (external_p && TREE_CODE (d) == VAR_DECL)) | |
16d53b64 | 12237 | { |
b794e321 MM |
12238 | /* The definition of the static data member is now required so |
12239 | we must substitute the initializer. */ | |
12240 | if (TREE_CODE (d) == VAR_DECL | |
3db45ab5 | 12241 | && !DECL_INITIAL (d) |
b794e321 MM |
12242 | && DECL_INITIAL (code_pattern)) |
12243 | { | |
4bff36d3 MM |
12244 | tree ns; |
12245 | tree init; | |
12246 | ||
12247 | ns = decl_namespace_context (d); | |
27a725e2 | 12248 | push_nested_namespace (ns); |
b794e321 | 12249 | push_nested_class (DECL_CONTEXT (d)); |
3db45ab5 | 12250 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
4bff36d3 | 12251 | args, |
015c2c66 MM |
12252 | tf_warning_or_error, NULL_TREE, |
12253 | /*integral_constant_expression_p=*/false); | |
d174af6c MM |
12254 | cp_finish_decl (d, init, /*init_const_expr_p=*/false, |
12255 | /*asmspec_tree=*/NULL_TREE, | |
86414779 | 12256 | LOOKUP_ONLYCONVERTING); |
b794e321 | 12257 | pop_nested_class (); |
27a725e2 | 12258 | pop_nested_namespace (ns); |
b794e321 MM |
12259 | } |
12260 | ||
f7e4e484 MM |
12261 | /* We restore the source position here because it's used by |
12262 | add_pending_template. */ | |
82a98427 | 12263 | input_location = saved_loc; |
c27be9b9 | 12264 | |
c8094d83 | 12265 | if (at_eof && !pattern_defined |
03d0f4af MM |
12266 | && DECL_EXPLICIT_INSTANTIATION (d)) |
12267 | /* [temp.explicit] | |
12268 | ||
12269 | The definition of a non-exported function template, a | |
12270 | non-exported member function template, or a non-exported | |
12271 | member function or static data member of a class template | |
12272 | shall be present in every translation unit in which it is | |
12273 | explicitly instantiated. */ | |
33bd39a2 | 12274 | pedwarn |
0f51ccfc | 12275 | ("explicit instantiation of %qD but no definition available", d); |
03d0f4af | 12276 | |
f7e4e484 MM |
12277 | /* ??? Historically, we have instantiated inline functions, even |
12278 | when marked as "extern template". */ | |
12279 | if (!(external_p && TREE_CODE (d) == VAR_DECL)) | |
12280 | add_pending_template (d); | |
de22184b | 12281 | goto out; |
5566b478 | 12282 | } |
4684cd27 MM |
12283 | /* Tell the repository that D is available in this translation unit |
12284 | -- and see if it is supposed to be instantiated here. */ | |
12285 | if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d)) | |
12286 | { | |
12287 | /* In a PCH file, despite the fact that the repository hasn't | |
12288 | requested instantiation in the PCH it is still possible that | |
12289 | an instantiation will be required in a file that includes the | |
12290 | PCH. */ | |
12291 | if (pch_file) | |
12292 | add_pending_template (d); | |
12293 | /* Instantiate inline functions so that the inliner can do its | |
12294 | job, even though we'll not be emitting a copy of this | |
12295 | function. */ | |
c2a124b2 | 12296 | if (!(TREE_CODE (d) == FUNCTION_DECL |
c8094d83 | 12297 | && flag_inline_trees |
c2a124b2 | 12298 | && DECL_DECLARED_INLINE_P (d))) |
4684cd27 MM |
12299 | goto out; |
12300 | } | |
5566b478 | 12301 | |
6de9cd9a | 12302 | need_push = !cfun || !global_bindings_p (); |
31a714f6 MM |
12303 | if (need_push) |
12304 | push_to_top_level (); | |
414ea4aa | 12305 | |
66e0c440 KL |
12306 | /* Mark D as instantiated so that recursive calls to |
12307 | instantiate_decl do not try to instantiate it again. */ | |
12308 | DECL_TEMPLATE_INSTANTIATED (d) = 1; | |
12309 | ||
2b0a63a3 MM |
12310 | /* Regenerate the declaration in case the template has been modified |
12311 | by a subsequent redeclaration. */ | |
12312 | regenerate_decl_from_template (d, td); | |
4684cd27 | 12313 | |
120722ac | 12314 | /* We already set the file and line above. Reset them now in case |
6de9cd9a | 12315 | they changed as a result of calling regenerate_decl_from_template. */ |
f31686a3 | 12316 | input_location = DECL_SOURCE_LOCATION (d); |
5156628f | 12317 | |
5566b478 MS |
12318 | if (TREE_CODE (d) == VAR_DECL) |
12319 | { | |
e92fb501 MM |
12320 | tree init; |
12321 | ||
1d62c33e MM |
12322 | /* Clear out DECL_RTL; whatever was there before may not be right |
12323 | since we've reset the type of the declaration. */ | |
12324 | SET_DECL_RTL (d, NULL_RTX); | |
5566b478 | 12325 | DECL_IN_AGGR_P (d) = 0; |
ea56c40c | 12326 | |
e92fb501 MM |
12327 | /* The initializer is placed in DECL_INITIAL by |
12328 | regenerate_decl_from_template. Pull it out so that | |
12329 | finish_decl can process it. */ | |
12330 | init = DECL_INITIAL (d); | |
12331 | DECL_INITIAL (d) = NULL_TREE; | |
12332 | DECL_INITIALIZED_P (d) = 0; | |
12333 | ||
4684cd27 MM |
12334 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
12335 | initializer. That function will defer actual emission until | |
12336 | we have a chance to determine linkage. */ | |
12337 | DECL_EXTERNAL (d) = 0; | |
12338 | ||
73a8adb6 | 12339 | /* Enter the scope of D so that access-checking works correctly. */ |
4684cd27 | 12340 | push_nested_class (DECL_CONTEXT (d)); |
e92fb501 | 12341 | finish_decl (d, init, NULL_TREE); |
73a8adb6 | 12342 | pop_nested_class (); |
5566b478 MS |
12343 | } |
12344 | else if (TREE_CODE (d) == FUNCTION_DECL) | |
12345 | { | |
6bbf1598 | 12346 | htab_t saved_local_specializations; |
a723baf1 MM |
12347 | tree subst_decl; |
12348 | tree tmpl_parm; | |
12349 | tree spec_parm; | |
6bbf1598 MM |
12350 | |
12351 | /* Save away the current list, in case we are instantiating one | |
12352 | template from within the body of another. */ | |
12353 | saved_local_specializations = local_specializations; | |
12354 | ||
6dfbb909 | 12355 | /* Set up the list of local specializations. */ |
c8094d83 | 12356 | local_specializations = htab_create (37, |
69f794a7 | 12357 | hash_local_specialization, |
a723baf1 | 12358 | eq_local_specializations, |
6dfbb909 MM |
12359 | NULL); |
12360 | ||
558475f0 | 12361 | /* Set up context. */ |
058b15c1 | 12362 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
5566b478 | 12363 | |
a723baf1 MM |
12364 | /* Create substitution entries for the parameters. */ |
12365 | subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d)); | |
12366 | tmpl_parm = DECL_ARGUMENTS (subst_decl); | |
12367 | spec_parm = DECL_ARGUMENTS (d); | |
12368 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d)) | |
12369 | { | |
12370 | register_local_specialization (spec_parm, tmpl_parm); | |
12371 | spec_parm = skip_artificial_parms_for (d, spec_parm); | |
12372 | tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm); | |
12373 | } | |
12374 | while (tmpl_parm) | |
12375 | { | |
12376 | register_local_specialization (spec_parm, tmpl_parm); | |
12377 | tmpl_parm = TREE_CHAIN (tmpl_parm); | |
12378 | spec_parm = TREE_CHAIN (spec_parm); | |
12379 | } | |
50bc768d | 12380 | gcc_assert (!spec_parm); |
a723baf1 | 12381 | |
558475f0 MM |
12382 | /* Substitute into the body of the function. */ |
12383 | tsubst_expr (DECL_SAVED_TREE (code_pattern), args, | |
015c2c66 MM |
12384 | tf_warning_or_error, tmpl, |
12385 | /*integral_constant_expression_p=*/false); | |
558475f0 | 12386 | |
6dfbb909 MM |
12387 | /* We don't need the local specializations any more. */ |
12388 | htab_delete (local_specializations); | |
6bbf1598 | 12389 | local_specializations = saved_local_specializations; |
6dfbb909 | 12390 | |
4d6abc1c | 12391 | /* Finish the function. */ |
b2dd096b | 12392 | d = finish_function (0); |
8cd2462c | 12393 | expand_or_defer_fn (d); |
5566b478 MS |
12394 | } |
12395 | ||
971cbc14 MM |
12396 | /* We're not deferring instantiation any more. */ |
12397 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; | |
12398 | ||
31a714f6 MM |
12399 | if (need_push) |
12400 | pop_from_top_level (); | |
414ea4aa | 12401 | |
de22184b | 12402 | out: |
82a98427 | 12403 | input_location = saved_loc; |
7d021397 | 12404 | pop_deferring_access_checks (); |
5566b478 | 12405 | pop_tinst_level (); |
a28e3c7f | 12406 | |
297a5329 JM |
12407 | timevar_pop (TV_PARSE); |
12408 | ||
a28e3c7f MS |
12409 | return d; |
12410 | } | |
5566b478 | 12411 | |
0aafb128 | 12412 | /* Run through the list of templates that we wish we could |
35046a54 KL |
12413 | instantiate, and instantiate any we can. RETRIES is the |
12414 | number of times we retry pending template instantiation. */ | |
0aafb128 | 12415 | |
35046a54 KL |
12416 | void |
12417 | instantiate_pending_templates (int retries) | |
0aafb128 MM |
12418 | { |
12419 | tree *t; | |
46ccf50a | 12420 | tree last = NULL_TREE; |
0aafb128 | 12421 | int reconsider; |
aad626f7 | 12422 | location_t saved_loc = input_location; |
12af7ba3 | 12423 | int saved_in_system_header = in_system_header; |
35046a54 KL |
12424 | |
12425 | /* Instantiating templates may trigger vtable generation. This in turn | |
12426 | may require further template instantiations. We place a limit here | |
12427 | to avoid infinite loop. */ | |
12428 | if (pending_templates && retries >= max_tinst_depth) | |
12429 | { | |
96c993a8 JM |
12430 | tree decl = TREE_VALUE (pending_templates); |
12431 | ||
dee15844 | 12432 | error ("template instantiation depth exceeds maximum of %d" |
96c993a8 JM |
12433 | " instantiating %q+D, possibly from virtual table generation" |
12434 | " (use -ftemplate-depth-NN to increase the maximum)", | |
12435 | max_tinst_depth, decl); | |
12436 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
12437 | /* Pretend that we defined it. */ | |
12438 | DECL_INITIAL (decl) = error_mark_node; | |
35046a54 KL |
12439 | return; |
12440 | } | |
12441 | ||
c8094d83 | 12442 | do |
0aafb128 MM |
12443 | { |
12444 | reconsider = 0; | |
12445 | ||
12446 | t = &pending_templates; | |
12447 | while (*t) | |
12448 | { | |
0aafb128 MM |
12449 | tree instantiation = TREE_VALUE (*t); |
12450 | ||
3ae18eaf | 12451 | reopen_tinst_level (TREE_PURPOSE (*t)); |
0aafb128 | 12452 | |
2f939d94 | 12453 | if (TYPE_P (instantiation)) |
0aafb128 MM |
12454 | { |
12455 | tree fn; | |
12456 | ||
d0f062fb | 12457 | if (!COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
12458 | { |
12459 | instantiate_class_template (instantiation); | |
12460 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) | |
c8094d83 | 12461 | for (fn = TYPE_METHODS (instantiation); |
0aafb128 MM |
12462 | fn; |
12463 | fn = TREE_CHAIN (fn)) | |
12464 | if (! DECL_ARTIFICIAL (fn)) | |
3db45ab5 | 12465 | instantiate_decl (fn, |
eba839f9 MM |
12466 | /*defer_ok=*/0, |
12467 | /*expl_inst_class_mem_p=*/false); | |
d0f062fb | 12468 | if (COMPLETE_TYPE_P (instantiation)) |
35046a54 | 12469 | reconsider = 1; |
0aafb128 MM |
12470 | } |
12471 | ||
d0f062fb | 12472 | if (COMPLETE_TYPE_P (instantiation)) |
0aafb128 MM |
12473 | /* If INSTANTIATION has been instantiated, then we don't |
12474 | need to consider it again in the future. */ | |
12475 | *t = TREE_CHAIN (*t); | |
46ccf50a JM |
12476 | else |
12477 | { | |
12478 | last = *t; | |
12479 | t = &TREE_CHAIN (*t); | |
12480 | } | |
0aafb128 MM |
12481 | } |
12482 | else | |
12483 | { | |
16d53b64 | 12484 | if (!DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
12485 | && !DECL_TEMPLATE_INSTANTIATED (instantiation)) |
12486 | { | |
3db45ab5 | 12487 | instantiation |
eba839f9 MM |
12488 | = instantiate_decl (instantiation, |
12489 | /*defer_ok=*/0, | |
12490 | /*expl_inst_class_mem_p=*/false); | |
0aafb128 | 12491 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
35046a54 | 12492 | reconsider = 1; |
0aafb128 MM |
12493 | } |
12494 | ||
16d53b64 | 12495 | if (DECL_TEMPLATE_SPECIALIZATION (instantiation) |
0aafb128 MM |
12496 | || DECL_TEMPLATE_INSTANTIATED (instantiation)) |
12497 | /* If INSTANTIATION has been instantiated, then we don't | |
12498 | need to consider it again in the future. */ | |
12499 | *t = TREE_CHAIN (*t); | |
46ccf50a JM |
12500 | else |
12501 | { | |
12502 | last = *t; | |
12503 | t = &TREE_CHAIN (*t); | |
12504 | } | |
0aafb128 | 12505 | } |
84e5ca0f NS |
12506 | tinst_depth = 0; |
12507 | current_tinst_level = NULL_TREE; | |
0aafb128 | 12508 | } |
46ccf50a | 12509 | last_pending_template = last; |
c8094d83 | 12510 | } |
0aafb128 MM |
12511 | while (reconsider); |
12512 | ||
aad626f7 | 12513 | input_location = saved_loc; |
12af7ba3 | 12514 | in_system_header = saved_in_system_header; |
0aafb128 MM |
12515 | } |
12516 | ||
fd74ca0b MM |
12517 | /* Substitute ARGVEC into T, which is a list of initializers for |
12518 | either base class or a non-static data member. The TREE_PURPOSEs | |
12519 | are DECLs, and the TREE_VALUEs are the initializer values. Used by | |
12520 | instantiate_decl. */ | |
4393e105 | 12521 | |
824b9a4c | 12522 | static tree |
3a978d72 | 12523 | tsubst_initializer_list (tree t, tree argvec) |
5566b478 | 12524 | { |
2282d28d | 12525 | tree inits = NULL_TREE; |
5566b478 MS |
12526 | |
12527 | for (; t; t = TREE_CHAIN (t)) | |
12528 | { | |
fd74ca0b MM |
12529 | tree decl; |
12530 | tree init; | |
fd74ca0b | 12531 | |
23fca1f5 | 12532 | decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error, |
fd74ca0b | 12533 | NULL_TREE); |
1f5a253a NS |
12534 | decl = expand_member_init (decl); |
12535 | if (decl && !DECL_P (decl)) | |
12536 | in_base_initializer = 1; | |
c8094d83 | 12537 | |
23fca1f5 | 12538 | init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error, |
015c2c66 MM |
12539 | NULL_TREE, |
12540 | /*integral_constant_expression_p=*/false); | |
1f5a253a NS |
12541 | in_base_initializer = 0; |
12542 | ||
12543 | if (decl) | |
2282d28d | 12544 | { |
1f5a253a | 12545 | init = build_tree_list (decl, init); |
2282d28d MM |
12546 | TREE_CHAIN (init) = inits; |
12547 | inits = init; | |
12548 | } | |
5566b478 | 12549 | } |
2282d28d | 12550 | return inits; |
5566b478 MS |
12551 | } |
12552 | ||
61a127b3 MM |
12553 | /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */ |
12554 | ||
12555 | static void | |
3a978d72 | 12556 | set_current_access_from_decl (tree decl) |
61a127b3 MM |
12557 | { |
12558 | if (TREE_PRIVATE (decl)) | |
12559 | current_access_specifier = access_private_node; | |
12560 | else if (TREE_PROTECTED (decl)) | |
12561 | current_access_specifier = access_protected_node; | |
12562 | else | |
12563 | current_access_specifier = access_public_node; | |
12564 | } | |
12565 | ||
dbfe2124 MM |
12566 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
12567 | is the instantiation (which should have been created with | |
12568 | start_enum) and ARGS are the template arguments to use. */ | |
b87692e5 | 12569 | |
dbfe2124 | 12570 | static void |
3a978d72 | 12571 | tsubst_enum (tree tag, tree newtag, tree args) |
b87692e5 | 12572 | { |
dbfe2124 | 12573 | tree e; |
b87692e5 MS |
12574 | |
12575 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) | |
12576 | { | |
61a127b3 | 12577 | tree value; |
7b6d72fc MM |
12578 | tree decl; |
12579 | ||
12580 | decl = TREE_VALUE (e); | |
61a127b3 MM |
12581 | /* Note that in a template enum, the TREE_VALUE is the |
12582 | CONST_DECL, not the corresponding INTEGER_CST. */ | |
c8094d83 | 12583 | value = tsubst_expr (DECL_INITIAL (decl), |
015c2c66 MM |
12584 | args, tf_warning_or_error, NULL_TREE, |
12585 | /*integral_constant_expression_p=*/true); | |
61a127b3 MM |
12586 | |
12587 | /* Give this enumeration constant the correct access. */ | |
7b6d72fc | 12588 | set_current_access_from_decl (decl); |
61a127b3 MM |
12589 | |
12590 | /* Actually build the enumerator itself. */ | |
c8094d83 | 12591 | build_enumerator (DECL_NAME (decl), value, newtag); |
dbfe2124 | 12592 | } |
b3d5a58b | 12593 | |
219670f1 | 12594 | finish_enum (newtag); |
f31686a3 RH |
12595 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
12596 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); | |
b87692e5 | 12597 | } |
36a117a5 | 12598 | |
1f6e1acc AS |
12599 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
12600 | its type -- but without substituting the innermost set of template | |
12601 | arguments. So, innermost set of template parameters will appear in | |
5c74d5b0 | 12602 | the type. */ |
1f6e1acc | 12603 | |
c8094d83 | 12604 | tree |
3a978d72 | 12605 | get_mostly_instantiated_function_type (tree decl) |
1f6e1acc | 12606 | { |
1f6e1acc AS |
12607 | tree fn_type; |
12608 | tree tmpl; | |
12609 | tree targs; | |
12610 | tree tparms; | |
12611 | int parm_depth; | |
12612 | ||
12613 | tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); | |
12614 | targs = DECL_TI_ARGS (decl); | |
12615 | tparms = DECL_TEMPLATE_PARMS (tmpl); | |
12616 | parm_depth = TMPL_PARMS_DEPTH (tparms); | |
12617 | ||
12618 | /* There should be as many levels of arguments as there are levels | |
12619 | of parameters. */ | |
50bc768d | 12620 | gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs)); |
1f6e1acc AS |
12621 | |
12622 | fn_type = TREE_TYPE (tmpl); | |
1f6e1acc AS |
12623 | |
12624 | if (parm_depth == 1) | |
12625 | /* No substitution is necessary. */ | |
12626 | ; | |
12627 | else | |
12628 | { | |
fae6e246 | 12629 | int i, save_access_control; |
1f6e1acc AS |
12630 | tree partial_args; |
12631 | ||
12632 | /* Replace the innermost level of the TARGS with NULL_TREEs to | |
dc957d14 | 12633 | let tsubst know not to substitute for those parameters. */ |
1f6e1acc AS |
12634 | partial_args = make_tree_vec (TREE_VEC_LENGTH (targs)); |
12635 | for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i) | |
12636 | SET_TMPL_ARGS_LEVEL (partial_args, i, | |
12637 | TMPL_ARGS_LEVEL (targs, i)); | |
12638 | SET_TMPL_ARGS_LEVEL (partial_args, | |
12639 | TMPL_ARGS_DEPTH (targs), | |
12640 | make_tree_vec (DECL_NTPARMS (tmpl))); | |
12641 | ||
fae6e246 RH |
12642 | /* Disable access control as this function is used only during |
12643 | name-mangling. */ | |
12644 | save_access_control = flag_access_control; | |
12645 | flag_access_control = 0; | |
5c74d5b0 | 12646 | |
9579624e | 12647 | ++processing_template_decl; |
1f6e1acc AS |
12648 | /* Now, do the (partial) substitution to figure out the |
12649 | appropriate function type. */ | |
c2ea3a40 | 12650 | fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE); |
9579624e | 12651 | --processing_template_decl; |
1f6e1acc AS |
12652 | |
12653 | /* Substitute into the template parameters to obtain the real | |
12654 | innermost set of parameters. This step is important if the | |
12655 | innermost set of template parameters contains value | |
12656 | parameters whose types depend on outer template parameters. */ | |
12657 | TREE_VEC_LENGTH (partial_args)--; | |
c2ea3a40 | 12658 | tparms = tsubst_template_parms (tparms, partial_args, tf_error); |
1f6e1acc | 12659 | |
fae6e246 | 12660 | flag_access_control = save_access_control; |
5c74d5b0 | 12661 | } |
1f6e1acc AS |
12662 | |
12663 | return fn_type; | |
12664 | } | |
669ec2b4 | 12665 | |
cb753e49 | 12666 | /* Return truthvalue if we're processing a template different from |
dc957d14 | 12667 | the last one involved in diagnostics. */ |
cb753e49 | 12668 | int |
3a978d72 | 12669 | problematic_instantiation_changed (void) |
cb753e49 GDR |
12670 | { |
12671 | return last_template_error_tick != tinst_level_tick; | |
12672 | } | |
12673 | ||
12674 | /* Remember current template involved in diagnostics. */ | |
12675 | void | |
3a978d72 | 12676 | record_last_problematic_instantiation (void) |
cb753e49 GDR |
12677 | { |
12678 | last_template_error_tick = tinst_level_tick; | |
12679 | } | |
12680 | ||
12681 | tree | |
3a978d72 | 12682 | current_instantiation (void) |
cb753e49 GDR |
12683 | { |
12684 | return current_tinst_level; | |
12685 | } | |
db3f4e4e NS |
12686 | |
12687 | /* [temp.param] Check that template non-type parm TYPE is of an allowable | |
838dfd8a | 12688 | type. Return zero for ok, nonzero for disallowed. Issue error and |
c2ea3a40 | 12689 | warning messages under control of COMPLAIN. */ |
db3f4e4e NS |
12690 | |
12691 | static int | |
3a978d72 | 12692 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
db3f4e4e NS |
12693 | { |
12694 | if (INTEGRAL_TYPE_P (type)) | |
12695 | return 0; | |
12696 | else if (POINTER_TYPE_P (type)) | |
12697 | return 0; | |
a5ac359a | 12698 | else if (TYPE_PTR_TO_MEMBER_P (type)) |
db3f4e4e | 12699 | return 0; |
db3f4e4e NS |
12700 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
12701 | return 0; | |
12702 | else if (TREE_CODE (type) == TYPENAME_TYPE) | |
12703 | return 0; | |
c8094d83 | 12704 | |
c2ea3a40 | 12705 | if (complain & tf_error) |
0f51ccfc | 12706 | error ("%q#T is not a valid type for a template constant parameter", type); |
db3f4e4e NS |
12707 | return 1; |
12708 | } | |
e2500fed | 12709 | |
5552b43c MM |
12710 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
12711 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ | |
1fb3244a | 12712 | |
5552b43c MM |
12713 | static bool |
12714 | dependent_type_p_r (tree type) | |
1fb3244a MM |
12715 | { |
12716 | tree scope; | |
12717 | ||
1fb3244a MM |
12718 | /* [temp.dep.type] |
12719 | ||
12720 | A type is dependent if it is: | |
12721 | ||
6615c446 JO |
12722 | -- a template parameter. Template template parameters are types |
12723 | for us (since TYPE_P holds true for them) so we handle | |
12724 | them here. */ | |
c8094d83 | 12725 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
6bf92cb6 | 12726 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
1fb3244a MM |
12727 | return true; |
12728 | /* -- a qualified-id with a nested-name-specifier which contains a | |
0cbd7506 | 12729 | class-name that names a dependent type or whose unqualified-id |
1fb3244a MM |
12730 | names a dependent type. */ |
12731 | if (TREE_CODE (type) == TYPENAME_TYPE) | |
12732 | return true; | |
12733 | /* -- a cv-qualified type where the cv-unqualified type is | |
0cbd7506 | 12734 | dependent. */ |
1fb3244a MM |
12735 | type = TYPE_MAIN_VARIANT (type); |
12736 | /* -- a compound type constructed from any dependent type. */ | |
a5ac359a | 12737 | if (TYPE_PTR_TO_MEMBER_P (type)) |
1fb3244a | 12738 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
c8094d83 | 12739 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
1fb3244a MM |
12740 | (type))); |
12741 | else if (TREE_CODE (type) == POINTER_TYPE | |
12742 | || TREE_CODE (type) == REFERENCE_TYPE) | |
12743 | return dependent_type_p (TREE_TYPE (type)); | |
12744 | else if (TREE_CODE (type) == FUNCTION_TYPE | |
12745 | || TREE_CODE (type) == METHOD_TYPE) | |
12746 | { | |
12747 | tree arg_type; | |
12748 | ||
12749 | if (dependent_type_p (TREE_TYPE (type))) | |
12750 | return true; | |
c8094d83 MS |
12751 | for (arg_type = TYPE_ARG_TYPES (type); |
12752 | arg_type; | |
1fb3244a MM |
12753 | arg_type = TREE_CHAIN (arg_type)) |
12754 | if (dependent_type_p (TREE_VALUE (arg_type))) | |
12755 | return true; | |
12756 | return false; | |
12757 | } | |
12758 | /* -- an array type constructed from any dependent type or whose | |
0cbd7506 | 12759 | size is specified by a constant expression that is |
1fb3244a MM |
12760 | value-dependent. */ |
12761 | if (TREE_CODE (type) == ARRAY_TYPE) | |
12762 | { | |
12763 | if (TYPE_DOMAIN (type) | |
c8094d83 | 12764 | && ((value_dependent_expression_p |
1fb3244a MM |
12765 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) |
12766 | || (type_dependent_expression_p | |
12767 | (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))))) | |
12768 | return true; | |
12769 | return dependent_type_p (TREE_TYPE (type)); | |
12770 | } | |
c8094d83 | 12771 | |
1fb3244a | 12772 | /* -- a template-id in which either the template name is a template |
86306a6b NS |
12773 | parameter ... */ |
12774 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) | |
1fb3244a | 12775 | return true; |
86306a6b | 12776 | /* ... or any of the template arguments is a dependent type or |
04c06002 | 12777 | an expression that is type-dependent or value-dependent. */ |
86306a6b | 12778 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
c8094d83 | 12779 | && (any_dependent_template_arguments_p |
7e99327d | 12780 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
1fb3244a | 12781 | return true; |
c8094d83 | 12782 | |
1fb3244a MM |
12783 | /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof' |
12784 | expression is not type-dependent, then it should already been | |
12785 | have resolved. */ | |
12786 | if (TREE_CODE (type) == TYPEOF_TYPE) | |
12787 | return true; | |
c8094d83 | 12788 | |
1fb3244a MM |
12789 | /* The standard does not specifically mention types that are local |
12790 | to template functions or local classes, but they should be | |
12791 | considered dependent too. For example: | |
12792 | ||
c8094d83 | 12793 | template <int I> void f() { |
0cbd7506 | 12794 | enum E { a = I }; |
1fb3244a MM |
12795 | S<sizeof (E)> s; |
12796 | } | |
12797 | ||
12798 | The size of `E' cannot be known until the value of `I' has been | |
12799 | determined. Therefore, `E' must be considered dependent. */ | |
12800 | scope = TYPE_CONTEXT (type); | |
12801 | if (scope && TYPE_P (scope)) | |
12802 | return dependent_type_p (scope); | |
12803 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
12804 | return type_dependent_expression_p (scope); | |
12805 | ||
12806 | /* Other types are non-dependent. */ | |
12807 | return false; | |
12808 | } | |
12809 | ||
5552b43c MM |
12810 | /* Returns TRUE if TYPE is dependent, in the sense of |
12811 | [temp.dep.type]. */ | |
12812 | ||
12813 | bool | |
12814 | dependent_type_p (tree type) | |
12815 | { | |
12816 | /* If there are no template parameters in scope, then there can't be | |
12817 | any dependent types. */ | |
12818 | if (!processing_template_decl) | |
e7e93965 MM |
12819 | { |
12820 | /* If we are not processing a template, then nobody should be | |
12821 | providing us with a dependent type. */ | |
12822 | gcc_assert (type); | |
12823 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM); | |
12824 | return false; | |
12825 | } | |
5552b43c MM |
12826 | |
12827 | /* If the type is NULL, we have not computed a type for the entity | |
12828 | in question; in that case, the type is dependent. */ | |
12829 | if (!type) | |
12830 | return true; | |
12831 | ||
12832 | /* Erroneous types can be considered non-dependent. */ | |
12833 | if (type == error_mark_node) | |
12834 | return false; | |
12835 | ||
12836 | /* If we have not already computed the appropriate value for TYPE, | |
12837 | do so now. */ | |
12838 | if (!TYPE_DEPENDENT_P_VALID (type)) | |
12839 | { | |
12840 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); | |
12841 | TYPE_DEPENDENT_P_VALID (type) = 1; | |
12842 | } | |
12843 | ||
12844 | return TYPE_DEPENDENT_P (type); | |
12845 | } | |
12846 | ||
8d83f792 MM |
12847 | /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */ |
12848 | ||
12849 | static bool | |
12850 | dependent_scope_ref_p (tree expression, bool criterion (tree)) | |
12851 | { | |
12852 | tree scope; | |
12853 | tree name; | |
12854 | ||
50bc768d | 12855 | gcc_assert (TREE_CODE (expression) == SCOPE_REF); |
8d83f792 MM |
12856 | |
12857 | if (!TYPE_P (TREE_OPERAND (expression, 0))) | |
12858 | return true; | |
12859 | ||
12860 | scope = TREE_OPERAND (expression, 0); | |
12861 | name = TREE_OPERAND (expression, 1); | |
12862 | ||
12863 | /* [temp.dep.expr] | |
12864 | ||
12865 | An id-expression is type-dependent if it contains a | |
12866 | nested-name-specifier that contains a class-name that names a | |
12867 | dependent type. */ | |
12868 | /* The suggested resolution to Core Issue 2 implies that if the | |
12869 | qualifying type is the current class, then we must peek | |
12870 | inside it. */ | |
c8094d83 | 12871 | if (DECL_P (name) |
8d83f792 MM |
12872 | && currently_open_class (scope) |
12873 | && !criterion (name)) | |
12874 | return false; | |
12875 | if (dependent_type_p (scope)) | |
12876 | return true; | |
12877 | ||
12878 | return false; | |
12879 | } | |
12880 | ||
20929c7f | 12881 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
fe89d797 MM |
12882 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
12883 | expression. */ | |
1fb3244a | 12884 | |
14d22dd6 | 12885 | bool |
1fb3244a MM |
12886 | value_dependent_expression_p (tree expression) |
12887 | { | |
12888 | if (!processing_template_decl) | |
12889 | return false; | |
12890 | ||
12891 | /* A name declared with a dependent type. */ | |
7416ab02 | 12892 | if (DECL_P (expression) && type_dependent_expression_p (expression)) |
1fb3244a | 12893 | return true; |
c8094d83 | 12894 | |
7416ab02 NS |
12895 | switch (TREE_CODE (expression)) |
12896 | { | |
12897 | case IDENTIFIER_NODE: | |
12898 | /* A name that has not been looked up -- must be dependent. */ | |
12899 | return true; | |
12900 | ||
12901 | case TEMPLATE_PARM_INDEX: | |
12902 | /* A non-type template parm. */ | |
12903 | return true; | |
12904 | ||
12905 | case CONST_DECL: | |
12906 | /* A non-type template parm. */ | |
12907 | if (DECL_TEMPLATE_PARM_P (expression)) | |
d17811fd | 12908 | return true; |
7416ab02 | 12909 | return false; |
d36d5600 | 12910 | |
7416ab02 | 12911 | case VAR_DECL: |
c8094d83 | 12912 | /* A constant with integral or enumeration type and is initialized |
0cbd7506 | 12913 | with an expression that is value-dependent. */ |
7416ab02 NS |
12914 | if (DECL_INITIAL (expression) |
12915 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression)) | |
12916 | && value_dependent_expression_p (DECL_INITIAL (expression))) | |
12917 | return true; | |
12918 | return false; | |
12919 | ||
12920 | case DYNAMIC_CAST_EXPR: | |
12921 | case STATIC_CAST_EXPR: | |
12922 | case CONST_CAST_EXPR: | |
12923 | case REINTERPRET_CAST_EXPR: | |
12924 | case CAST_EXPR: | |
12925 | /* These expressions are value-dependent if the type to which | |
0cbd7506 MS |
12926 | the cast occurs is dependent or the expression being casted |
12927 | is value-dependent. */ | |
7416ab02 NS |
12928 | { |
12929 | tree type = TREE_TYPE (expression); | |
c8094d83 | 12930 | |
7416ab02 NS |
12931 | if (dependent_type_p (type)) |
12932 | return true; | |
c8094d83 | 12933 | |
7416ab02 NS |
12934 | /* A functional cast has a list of operands. */ |
12935 | expression = TREE_OPERAND (expression, 0); | |
12936 | if (!expression) | |
12937 | { | |
12938 | /* If there are no operands, it must be an expression such | |
12939 | as "int()". This should not happen for aggregate types | |
12940 | because it would form non-constant expressions. */ | |
12941 | gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
c8094d83 | 12942 | |
7416ab02 NS |
12943 | return false; |
12944 | } | |
c8094d83 | 12945 | |
7416ab02 NS |
12946 | if (TREE_CODE (expression) == TREE_LIST) |
12947 | { | |
12948 | for (; expression; expression = TREE_CHAIN (expression)) | |
d17811fd MM |
12949 | if (value_dependent_expression_p (TREE_VALUE (expression))) |
12950 | return true; | |
7416ab02 NS |
12951 | return false; |
12952 | } | |
c8094d83 | 12953 | |
d17811fd | 12954 | return value_dependent_expression_p (expression); |
7416ab02 | 12955 | } |
c8094d83 | 12956 | |
7416ab02 NS |
12957 | case SIZEOF_EXPR: |
12958 | case ALIGNOF_EXPR: | |
12959 | /* A `sizeof' expression is value-dependent if the operand is | |
0cbd7506 | 12960 | type-dependent. */ |
d17811fd MM |
12961 | expression = TREE_OPERAND (expression, 0); |
12962 | if (TYPE_P (expression)) | |
12963 | return dependent_type_p (expression); | |
12964 | return type_dependent_expression_p (expression); | |
100d337a | 12965 | |
7416ab02 NS |
12966 | case SCOPE_REF: |
12967 | return dependent_scope_ref_p (expression, value_dependent_expression_p); | |
100d337a | 12968 | |
7416ab02 NS |
12969 | case COMPONENT_REF: |
12970 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) | |
12971 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); | |
12972 | ||
12973 | case CALL_EXPR: | |
fe89d797 MM |
12974 | /* A CALL_EXPR may appear in a constant expression if it is a |
12975 | call to a builtin function, e.g., __builtin_constant_p. All | |
12976 | such calls are value-dependent. */ | |
12977 | return true; | |
7416ab02 NS |
12978 | |
12979 | default: | |
12980 | /* A constant expression is value-dependent if any subexpression is | |
0cbd7506 | 12981 | value-dependent. */ |
1fb3244a MM |
12982 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
12983 | { | |
7416ab02 | 12984 | case tcc_reference: |
6615c446 | 12985 | case tcc_unary: |
c8094d83 | 12986 | return (value_dependent_expression_p |
1fb3244a | 12987 | (TREE_OPERAND (expression, 0))); |
c8094d83 | 12988 | |
6615c446 JO |
12989 | case tcc_comparison: |
12990 | case tcc_binary: | |
c8094d83 | 12991 | return ((value_dependent_expression_p |
1fb3244a | 12992 | (TREE_OPERAND (expression, 0))) |
c8094d83 | 12993 | || (value_dependent_expression_p |
1fb3244a | 12994 | (TREE_OPERAND (expression, 1)))); |
c8094d83 | 12995 | |
6615c446 | 12996 | case tcc_expression: |
1fb3244a MM |
12997 | { |
12998 | int i; | |
54e4aedb | 12999 | for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i) |
14d22dd6 MM |
13000 | /* In some cases, some of the operands may be missing. |
13001 | (For example, in the case of PREDECREMENT_EXPR, the | |
13002 | amount to increment by may be missing.) That doesn't | |
13003 | make the expression dependent. */ | |
13004 | if (TREE_OPERAND (expression, i) | |
13005 | && (value_dependent_expression_p | |
13006 | (TREE_OPERAND (expression, i)))) | |
1fb3244a MM |
13007 | return true; |
13008 | return false; | |
13009 | } | |
c8094d83 | 13010 | |
6615c446 | 13011 | default: |
7416ab02 | 13012 | break; |
1fb3244a MM |
13013 | } |
13014 | } | |
c8094d83 | 13015 | |
1fb3244a MM |
13016 | /* The expression is not value-dependent. */ |
13017 | return false; | |
13018 | } | |
13019 | ||
13020 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of | |
13021 | [temp.dep.expr]. */ | |
13022 | ||
13023 | bool | |
3a978d72 | 13024 | type_dependent_expression_p (tree expression) |
1fb3244a MM |
13025 | { |
13026 | if (!processing_template_decl) | |
13027 | return false; | |
13028 | ||
7efa3e22 NS |
13029 | if (expression == error_mark_node) |
13030 | return false; | |
10b1d5e7 MM |
13031 | |
13032 | /* An unresolved name is always dependent. */ | |
b01e6d2b JM |
13033 | if (TREE_CODE (expression) == IDENTIFIER_NODE |
13034 | || TREE_CODE (expression) == USING_DECL) | |
10b1d5e7 | 13035 | return true; |
c8094d83 | 13036 | |
1fb3244a MM |
13037 | /* Some expression forms are never type-dependent. */ |
13038 | if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR | |
13039 | || TREE_CODE (expression) == SIZEOF_EXPR | |
13040 | || TREE_CODE (expression) == ALIGNOF_EXPR | |
13041 | || TREE_CODE (expression) == TYPEID_EXPR | |
13042 | || TREE_CODE (expression) == DELETE_EXPR | |
13043 | || TREE_CODE (expression) == VEC_DELETE_EXPR | |
13044 | || TREE_CODE (expression) == THROW_EXPR) | |
13045 | return false; | |
13046 | ||
13047 | /* The types of these expressions depends only on the type to which | |
13048 | the cast occurs. */ | |
13049 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR | |
13050 | || TREE_CODE (expression) == STATIC_CAST_EXPR | |
13051 | || TREE_CODE (expression) == CONST_CAST_EXPR | |
13052 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR | |
13053 | || TREE_CODE (expression) == CAST_EXPR) | |
13054 | return dependent_type_p (TREE_TYPE (expression)); | |
d17811fd | 13055 | |
1fb3244a MM |
13056 | /* The types of these expressions depends only on the type created |
13057 | by the expression. */ | |
d17811fd MM |
13058 | if (TREE_CODE (expression) == NEW_EXPR |
13059 | || TREE_CODE (expression) == VEC_NEW_EXPR) | |
09d2f85f KL |
13060 | { |
13061 | /* For NEW_EXPR tree nodes created inside a template, either | |
13062 | the object type itself or a TREE_LIST may appear as the | |
13063 | operand 1. */ | |
13064 | tree type = TREE_OPERAND (expression, 1); | |
13065 | if (TREE_CODE (type) == TREE_LIST) | |
13066 | /* This is an array type. We need to check array dimensions | |
13067 | as well. */ | |
13068 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) | |
13069 | || value_dependent_expression_p | |
13070 | (TREE_OPERAND (TREE_VALUE (type), 1)); | |
13071 | else | |
13072 | return dependent_type_p (type); | |
13073 | } | |
1fb3244a | 13074 | |
5a57f1b2 JM |
13075 | if (TREE_CODE (expression) == SCOPE_REF |
13076 | && dependent_scope_ref_p (expression, | |
13077 | type_dependent_expression_p)) | |
13078 | return true; | |
13079 | ||
13080 | if (TREE_CODE (expression) == FUNCTION_DECL | |
13081 | && DECL_LANG_SPECIFIC (expression) | |
13082 | && DECL_TEMPLATE_INFO (expression) | |
13083 | && (any_dependent_template_arguments_p | |
13084 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) | |
13085 | return true; | |
13086 | ||
13087 | if (TREE_CODE (expression) == TEMPLATE_DECL | |
13088 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) | |
13089 | return false; | |
13090 | ||
d17811fd MM |
13091 | if (TREE_TYPE (expression) == unknown_type_node) |
13092 | { | |
13093 | if (TREE_CODE (expression) == ADDR_EXPR) | |
13094 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); | |
c353b8e3 MM |
13095 | if (TREE_CODE (expression) == COMPONENT_REF |
13096 | || TREE_CODE (expression) == OFFSET_REF) | |
6cb89308 NS |
13097 | { |
13098 | if (type_dependent_expression_p (TREE_OPERAND (expression, 0))) | |
13099 | return true; | |
13100 | expression = TREE_OPERAND (expression, 1); | |
13101 | if (TREE_CODE (expression) == IDENTIFIER_NODE) | |
13102 | return false; | |
13103 | } | |
3601f003 KL |
13104 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
13105 | if (TREE_CODE (expression) == SCOPE_REF) | |
13106 | return false; | |
c8094d83 | 13107 | |
d17811fd MM |
13108 | if (TREE_CODE (expression) == BASELINK) |
13109 | expression = BASELINK_FUNCTIONS (expression); | |
c8094d83 | 13110 | |
d17811fd MM |
13111 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
13112 | { | |
ee3071ef NS |
13113 | if (any_dependent_template_arguments_p |
13114 | (TREE_OPERAND (expression, 1))) | |
d17811fd MM |
13115 | return true; |
13116 | expression = TREE_OPERAND (expression, 0); | |
13117 | } | |
b207d6e2 MM |
13118 | gcc_assert (TREE_CODE (expression) == OVERLOAD |
13119 | || TREE_CODE (expression) == FUNCTION_DECL); | |
c8094d83 | 13120 | |
315fb5db | 13121 | while (expression) |
d17811fd | 13122 | { |
315fb5db NS |
13123 | if (type_dependent_expression_p (OVL_CURRENT (expression))) |
13124 | return true; | |
13125 | expression = OVL_NEXT (expression); | |
d17811fd | 13126 | } |
315fb5db | 13127 | return false; |
d17811fd | 13128 | } |
c8094d83 | 13129 | |
3ce5fa4f | 13130 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
3db45ab5 | 13131 | |
1fb3244a MM |
13132 | return (dependent_type_p (TREE_TYPE (expression))); |
13133 | } | |
13134 | ||
d17811fd MM |
13135 | /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call) |
13136 | contains a type-dependent expression. */ | |
1fb3244a MM |
13137 | |
13138 | bool | |
d17811fd MM |
13139 | any_type_dependent_arguments_p (tree args) |
13140 | { | |
13141 | while (args) | |
13142 | { | |
7efa3e22 NS |
13143 | tree arg = TREE_VALUE (args); |
13144 | ||
13145 | if (type_dependent_expression_p (arg)) | |
d17811fd MM |
13146 | return true; |
13147 | args = TREE_CHAIN (args); | |
13148 | } | |
13149 | return false; | |
13150 | } | |
13151 | ||
13152 | /* Returns TRUE if the ARG (a template argument) is dependent. */ | |
13153 | ||
13154 | static bool | |
1fb3244a MM |
13155 | dependent_template_arg_p (tree arg) |
13156 | { | |
13157 | if (!processing_template_decl) | |
13158 | return false; | |
13159 | ||
13160 | if (TREE_CODE (arg) == TEMPLATE_DECL | |
13161 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
13162 | return dependent_template_p (arg); | |
13163 | else if (TYPE_P (arg)) | |
13164 | return dependent_type_p (arg); | |
13165 | else | |
13166 | return (type_dependent_expression_p (arg) | |
13167 | || value_dependent_expression_p (arg)); | |
13168 | } | |
13169 | ||
d17811fd MM |
13170 | /* Returns true if ARGS (a collection of template arguments) contains |
13171 | any dependent arguments. */ | |
1fb3244a | 13172 | |
d17811fd MM |
13173 | bool |
13174 | any_dependent_template_arguments_p (tree args) | |
1fb3244a | 13175 | { |
bf12d54d | 13176 | int i; |
c353b8e3 MM |
13177 | int j; |
13178 | ||
d17811fd MM |
13179 | if (!args) |
13180 | return false; | |
7e497d0c VR |
13181 | if (args == error_mark_node) |
13182 | return true; | |
d17811fd | 13183 | |
c353b8e3 MM |
13184 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
13185 | { | |
13186 | tree level = TMPL_ARGS_LEVEL (args, i + 1); | |
13187 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) | |
13188 | if (dependent_template_arg_p (TREE_VEC_ELT (level, j))) | |
13189 | return true; | |
13190 | } | |
1fb3244a | 13191 | |
1fb3244a MM |
13192 | return false; |
13193 | } | |
13194 | ||
13195 | /* Returns TRUE if the template TMPL is dependent. */ | |
13196 | ||
13197 | bool | |
13198 | dependent_template_p (tree tmpl) | |
13199 | { | |
b95cc51a MM |
13200 | if (TREE_CODE (tmpl) == OVERLOAD) |
13201 | { | |
13202 | while (tmpl) | |
13203 | { | |
13204 | if (dependent_template_p (OVL_FUNCTION (tmpl))) | |
13205 | return true; | |
13206 | tmpl = OVL_CHAIN (tmpl); | |
13207 | } | |
13208 | return false; | |
13209 | } | |
13210 | ||
1fb3244a MM |
13211 | /* Template template parameters are dependent. */ |
13212 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) | |
13213 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) | |
13214 | return true; | |
27ab0504 | 13215 | /* So are names that have not been looked up. */ |
acccf788 MM |
13216 | if (TREE_CODE (tmpl) == SCOPE_REF |
13217 | || TREE_CODE (tmpl) == IDENTIFIER_NODE) | |
d17811fd | 13218 | return true; |
1fb3244a MM |
13219 | /* So are member templates of dependent classes. */ |
13220 | if (TYPE_P (CP_DECL_CONTEXT (tmpl))) | |
13221 | return dependent_type_p (DECL_CONTEXT (tmpl)); | |
13222 | return false; | |
13223 | } | |
13224 | ||
d17811fd MM |
13225 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
13226 | ||
13227 | bool | |
13228 | dependent_template_id_p (tree tmpl, tree args) | |
13229 | { | |
13230 | return (dependent_template_p (tmpl) | |
13231 | || any_dependent_template_arguments_p (args)); | |
13232 | } | |
13233 | ||
14d22dd6 MM |
13234 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
13235 | TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE | |
13236 | can be found. Note that this function peers inside uninstantiated | |
13237 | templates and therefore should be used only in extremely limited | |
dda04398 | 13238 | situations. ONLY_CURRENT_P restricts this peering to the currently |
c51940a2 | 13239 | open classes hierarchy (which is required when comparing types). */ |
14d22dd6 MM |
13240 | |
13241 | tree | |
13242 | resolve_typename_type (tree type, bool only_current_p) | |
13243 | { | |
13244 | tree scope; | |
13245 | tree name; | |
13246 | tree decl; | |
13247 | int quals; | |
4514aa8c | 13248 | tree pushed_scope; |
14d22dd6 | 13249 | |
50bc768d | 13250 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
14d22dd6 MM |
13251 | |
13252 | scope = TYPE_CONTEXT (type); | |
13253 | name = TYPE_IDENTIFIER (type); | |
13254 | ||
13255 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve | |
13256 | it first before we can figure out what NAME refers to. */ | |
13257 | if (TREE_CODE (scope) == TYPENAME_TYPE) | |
13258 | scope = resolve_typename_type (scope, only_current_p); | |
13259 | /* If we don't know what SCOPE refers to, then we cannot resolve the | |
13260 | TYPENAME_TYPE. */ | |
13261 | if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE) | |
13262 | return error_mark_node; | |
13263 | /* If the SCOPE is a template type parameter, we have no way of | |
13264 | resolving the name. */ | |
13265 | if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM) | |
13266 | return type; | |
13267 | /* If the SCOPE is not the current instantiation, there's no reason | |
13268 | to look inside it. */ | |
13269 | if (only_current_p && !currently_open_class (scope)) | |
13270 | return error_mark_node; | |
ca099ac8 MM |
13271 | /* If SCOPE is a partial instantiation, it will not have a valid |
13272 | TYPE_FIELDS list, so use the original template. */ | |
353b4fc0 | 13273 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); |
14d22dd6 MM |
13274 | /* Enter the SCOPE so that name lookup will be resolved as if we |
13275 | were in the class definition. In particular, SCOPE will no | |
13276 | longer be considered a dependent type. */ | |
4514aa8c | 13277 | pushed_scope = push_scope (scope); |
14d22dd6 | 13278 | /* Look up the declaration. */ |
86ac0575 | 13279 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true); |
14d22dd6 MM |
13280 | /* Obtain the set of qualifiers applied to the TYPE. */ |
13281 | quals = cp_type_quals (type); | |
13282 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to | |
13283 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ | |
13284 | if (!decl) | |
13285 | type = error_mark_node; | |
13286 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE | |
13287 | && TREE_CODE (decl) == TYPE_DECL) | |
13288 | type = TREE_TYPE (decl); | |
13289 | else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR | |
13290 | && DECL_CLASS_TEMPLATE_P (decl)) | |
13291 | { | |
13292 | tree tmpl; | |
13293 | tree args; | |
13294 | /* Obtain the template and the arguments. */ | |
13295 | tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0); | |
13296 | args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1); | |
13297 | /* Instantiate the template. */ | |
13298 | type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE, | |
42eaed49 | 13299 | /*entering_scope=*/0, tf_error | tf_user); |
14d22dd6 MM |
13300 | } |
13301 | else | |
13302 | type = error_mark_node; | |
13303 | /* Qualify the resulting type. */ | |
13304 | if (type != error_mark_node && quals) | |
13305 | type = cp_build_qualified_type (type, quals); | |
13306 | /* Leave the SCOPE. */ | |
4514aa8c NS |
13307 | if (pushed_scope) |
13308 | pop_scope (pushed_scope); | |
14d22dd6 MM |
13309 | |
13310 | return type; | |
13311 | } | |
13312 | ||
d17811fd MM |
13313 | /* EXPR is an expression which is not type-dependent. Return a proxy |
13314 | for EXPR that can be used to compute the types of larger | |
13315 | expressions containing EXPR. */ | |
13316 | ||
13317 | tree | |
13318 | build_non_dependent_expr (tree expr) | |
13319 | { | |
0deb916c MM |
13320 | tree inner_expr; |
13321 | ||
c8094d83 | 13322 | /* Preserve null pointer constants so that the type of things like |
d17811fd MM |
13323 | "p == 0" where "p" is a pointer can be determined. */ |
13324 | if (null_ptr_cst_p (expr)) | |
13325 | return expr; | |
13326 | /* Preserve OVERLOADs; the functions must be available to resolve | |
13327 | types. */ | |
c497db75 AO |
13328 | inner_expr = expr; |
13329 | if (TREE_CODE (inner_expr) == ADDR_EXPR) | |
13330 | inner_expr = TREE_OPERAND (inner_expr, 0); | |
13331 | if (TREE_CODE (inner_expr) == COMPONENT_REF) | |
13332 | inner_expr = TREE_OPERAND (inner_expr, 1); | |
2226e997 | 13333 | if (is_overloaded_fn (inner_expr) |
6439fffd | 13334 | || TREE_CODE (inner_expr) == OFFSET_REF) |
d17811fd | 13335 | return expr; |
5ae9ba3e MM |
13336 | /* There is no need to return a proxy for a variable. */ |
13337 | if (TREE_CODE (expr) == VAR_DECL) | |
13338 | return expr; | |
7433e6d4 MM |
13339 | /* Preserve string constants; conversions from string constants to |
13340 | "char *" are allowed, even though normally a "const char *" | |
13341 | cannot be used to initialize a "char *". */ | |
13342 | if (TREE_CODE (expr) == STRING_CST) | |
13343 | return expr; | |
b7c707d1 MM |
13344 | /* Preserve arithmetic constants, as an optimization -- there is no |
13345 | reason to create a new node. */ | |
13346 | if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST) | |
13347 | return expr; | |
9b7be7b5 MM |
13348 | /* Preserve THROW_EXPRs -- all throw-expressions have type "void". |
13349 | There is at least one place where we want to know that a | |
13350 | particular expression is a throw-expression: when checking a ?: | |
13351 | expression, there are special rules if the second or third | |
878cbb73 | 13352 | argument is a throw-expression. */ |
9b7be7b5 MM |
13353 | if (TREE_CODE (expr) == THROW_EXPR) |
13354 | return expr; | |
47d4c811 NS |
13355 | |
13356 | if (TREE_CODE (expr) == COND_EXPR) | |
f293ce4b RS |
13357 | return build3 (COND_EXPR, |
13358 | TREE_TYPE (expr), | |
13359 | TREE_OPERAND (expr, 0), | |
c8094d83 | 13360 | (TREE_OPERAND (expr, 1) |
f293ce4b RS |
13361 | ? build_non_dependent_expr (TREE_OPERAND (expr, 1)) |
13362 | : build_non_dependent_expr (TREE_OPERAND (expr, 0))), | |
13363 | build_non_dependent_expr (TREE_OPERAND (expr, 2))); | |
8e1daa34 NS |
13364 | if (TREE_CODE (expr) == COMPOUND_EXPR |
13365 | && !COMPOUND_EXPR_OVERLOADED (expr)) | |
f293ce4b RS |
13366 | return build2 (COMPOUND_EXPR, |
13367 | TREE_TYPE (expr), | |
13368 | TREE_OPERAND (expr, 0), | |
13369 | build_non_dependent_expr (TREE_OPERAND (expr, 1))); | |
c8094d83 | 13370 | |
fdeff563 NS |
13371 | /* If the type is unknown, it can't really be non-dependent */ |
13372 | gcc_assert (TREE_TYPE (expr) != unknown_type_node); | |
3db45ab5 | 13373 | |
c8094d83 | 13374 | /* Otherwise, build a NON_DEPENDENT_EXPR. |
d17811fd MM |
13375 | |
13376 | REFERENCE_TYPEs are not stripped for expressions in templates | |
13377 | because doing so would play havoc with mangling. Consider, for | |
13378 | example: | |
13379 | ||
c8094d83 | 13380 | template <typename T> void f<T& g>() { g(); } |
d17811fd MM |
13381 | |
13382 | In the body of "f", the expression for "g" will have | |
13383 | REFERENCE_TYPE, even though the standard says that it should | |
13384 | not. The reason is that we must preserve the syntactic form of | |
13385 | the expression so that mangling (say) "f<g>" inside the body of | |
13386 | "f" works out correctly. Therefore, the REFERENCE_TYPE is | |
13387 | stripped here. */ | |
018a5803 | 13388 | return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr); |
d17811fd MM |
13389 | } |
13390 | ||
13391 | /* ARGS is a TREE_LIST of expressions as arguments to a function call. | |
13392 | Return a new TREE_LIST with the various arguments replaced with | |
13393 | equivalent non-dependent expressions. */ | |
13394 | ||
13395 | tree | |
13396 | build_non_dependent_args (tree args) | |
13397 | { | |
13398 | tree a; | |
13399 | tree new_args; | |
13400 | ||
13401 | new_args = NULL_TREE; | |
13402 | for (a = args; a; a = TREE_CHAIN (a)) | |
c8094d83 | 13403 | new_args = tree_cons (NULL_TREE, |
d17811fd MM |
13404 | build_non_dependent_expr (TREE_VALUE (a)), |
13405 | new_args); | |
13406 | return nreverse (new_args); | |
13407 | } | |
13408 | ||
e2500fed | 13409 | #include "gt-cp-pt.h" |