1 | /* Handle parameterized types (templates) for GNU -*- C++ -*-. |
---|---|
2 | Copyright (C) 1992-2025 Free Software Foundation, Inc. |
3 | Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing. |
4 | Rewritten by Jason Merrill (jason@cygnus.com). |
5 | |
6 | This file is part of GCC. |
7 | |
8 | GCC is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 3, or (at your option) |
11 | any later version. |
12 | |
13 | GCC is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ |
21 | |
22 | /* Known bugs or deficiencies include: |
23 | |
24 | all methods must be provided in header files; can't use a source |
25 | file that contains only the method templates and "just win". |
26 | |
27 | Fixed by: C++20 modules. */ |
28 | |
29 | #include "config.h" |
30 | #define INCLUDE_ALGORITHM // for std::equal |
31 | #include "system.h" |
32 | #include "coretypes.h" |
33 | #include "cp-tree.h" |
34 | #include "timevar.h" |
35 | #include "stringpool.h" |
36 | #include "varasm.h" |
37 | #include "attribs.h" |
38 | #include "stor-layout.h" |
39 | #include "intl.h" |
40 | #include "c-family/c-objc.h" |
41 | #include "cp-objcp-common.h" |
42 | #include "toplev.h" |
43 | #include "tree-iterator.h" |
44 | #include "type-utils.h" |
45 | #include "gimplify.h" |
46 | #include "gcc-rich-location.h" |
47 | #include "selftest.h" |
48 | #include "target.h" |
49 | #include "builtins.h" |
50 | #include "omp-general.h" |
51 | #include "pretty-print-markup.h" |
52 | |
53 | /* The type of functions taking a tree, and some additional data, and |
54 | returning an int. */ |
55 | typedef int (*tree_fn_t) (tree, void*); |
56 | |
57 | /* The PENDING_TEMPLATES is a list of templates whose instantiations |
58 | have been deferred, either because their definitions were not yet |
59 | available, or because we were putting off doing the work. */ |
60 | struct GTY ((chain_next ("%h.next"))) pending_template |
61 | { |
62 | struct pending_template *next; |
63 | struct tinst_level *tinst; |
64 | }; |
65 | |
66 | static GTY(()) struct pending_template *pending_templates; |
67 | static GTY(()) struct pending_template *last_pending_template; |
68 | |
69 | int processing_template_parmlist; |
70 | static int template_header_count; |
71 | |
72 | static vec<int> inline_parm_levels; |
73 | |
74 | static GTY(()) struct tinst_level *current_tinst_level; |
75 | |
76 | static GTY(()) vec<tree, va_gc> *saved_access_scope; |
77 | |
78 | /* Live only within one (recursive) call to tsubst_expr. We use |
79 | this to pass the statement expression node from the STMT_EXPR |
80 | to the EXPR_STMT that is its result. */ |
81 | static tree cur_stmt_expr; |
82 | |
83 | // -------------------------------------------------------------------------- // |
84 | // Local Specialization Stack |
85 | // |
86 | // Implementation of the RAII helper for creating new local |
87 | // specializations. |
88 | local_specialization_stack::local_specialization_stack (lss_policy policy) |
89 | : saved (local_specializations) |
90 | { |
91 | if (policy == lss_nop) |
92 | ; |
93 | else if (policy == lss_blank || !saved) |
94 | local_specializations = new hash_map<tree, tree>; |
95 | else |
96 | local_specializations = new hash_map<tree, tree>(*saved); |
97 | } |
98 | |
99 | local_specialization_stack::~local_specialization_stack () |
100 | { |
101 | if (local_specializations != saved) |
102 | { |
103 | delete local_specializations; |
104 | local_specializations = saved; |
105 | } |
106 | } |
107 | |
108 | /* True if we've recursed into fn_type_unification too many times. */ |
109 | static bool excessive_deduction_depth; |
110 | |
111 | struct spec_hasher : ggc_ptr_hash<spec_entry> |
112 | { |
113 | static hashval_t hash (tree, tree); |
114 | static hashval_t hash (spec_entry *); |
115 | static bool equal (spec_entry *, spec_entry *); |
116 | }; |
117 | |
118 | /* The general template is not in these tables. */ |
119 | typedef hash_table<spec_hasher> spec_hash_table; |
120 | static GTY (()) spec_hash_table *decl_specializations; |
121 | static GTY (()) spec_hash_table *type_specializations; |
122 | |
123 | /* Contains canonical template parameter types. The vector is indexed by |
124 | the TEMPLATE_TYPE_IDX of the template parameter. Each element is a |
125 | TREE_LIST, whose TREE_VALUEs contain the canonical template |
126 | parameters of various types and levels. */ |
127 | static GTY(()) vec<tree, va_gc> *canonical_template_parms; |
128 | |
129 | #define UNIFY_ALLOW_NONE 0 |
130 | #define UNIFY_ALLOW_MORE_CV_QUAL 1 |
131 | #define UNIFY_ALLOW_LESS_CV_QUAL 2 |
132 | #define UNIFY_ALLOW_DERIVED 4 |
133 | #define UNIFY_ALLOW_INTEGER 8 |
134 | #define UNIFY_ALLOW_OUTER_LEVEL 16 |
135 | #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32 |
136 | #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64 |
137 | |
138 | enum template_base_result { |
139 | tbr_incomplete_type, |
140 | tbr_ambiguous_baseclass, |
141 | tbr_success |
142 | }; |
143 | |
144 | static bool resolve_overloaded_unification (tree, tree, tree, tree, |
145 | unification_kind_t, int, |
146 | bool); |
147 | static int try_one_overload (tree, tree, tree, tree, tree, |
148 | unification_kind_t, int, bool, bool); |
149 | static int unify (tree, tree, tree, tree, int, bool); |
150 | static void add_pending_template (tree); |
151 | static tree reopen_tinst_level (struct tinst_level *); |
152 | static tree tsubst_initializer_list (tree, tree); |
153 | static tree get_partial_spec_bindings (tree, tree, tree); |
154 | static void tsubst_enum (tree, tree, tree); |
155 | static bool check_instantiated_args (tree, tree, tsubst_flags_t); |
156 | static int check_non_deducible_conversion (tree, tree, unification_kind_t, int, |
157 | struct conversion **, bool, bool); |
158 | static int maybe_adjust_types_for_deduction (tree, unification_kind_t, |
159 | tree*, tree*, tree); |
160 | static int type_unification_real (tree, tree, tree, const tree *, |
161 | unsigned int, int, unification_kind_t, |
162 | vec<deferred_access_check, va_gc> **, |
163 | bool); |
164 | static void note_template_header (int); |
165 | static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t); |
166 | static tree convert_nontype_argument (tree, tree, tsubst_flags_t); |
167 | static tree convert_template_argument (tree, tree, tree, |
168 | tsubst_flags_t, int, tree); |
169 | static tree for_each_template_parm (tree, tree_fn_t, void*, |
170 | hash_set<tree> *, bool, tree_fn_t = NULL); |
171 | static tree expand_template_argument_pack (tree); |
172 | static tree build_template_parm_index (int, int, int, tree, tree); |
173 | static bool inline_needs_template_parms (tree, bool); |
174 | static void push_inline_template_parms_recursive (tree, int); |
175 | static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t); |
176 | static int mark_template_parm (tree, void *); |
177 | static int template_parm_this_level_p (tree, void *); |
178 | static tree tsubst_friend_function (tree, tree); |
179 | static tree tsubst_friend_class (tree, tree); |
180 | static int can_complete_type_without_circularity (tree); |
181 | static tree get_bindings (tree, tree, tree, bool); |
182 | static int template_decl_level (tree); |
183 | static int check_cv_quals_for_unify (int, tree, tree); |
184 | static int unify_pack_expansion (tree, tree, tree, |
185 | tree, unification_kind_t, bool, bool); |
186 | static tree copy_template_args (tree); |
187 | static tree tsubst_template_parms (tree, tree, tsubst_flags_t); |
188 | static void tsubst_each_template_parm_constraints (tree, tree, tsubst_flags_t); |
189 | static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree); |
190 | static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree); |
191 | static bool check_specialization_scope (void); |
192 | static tree process_partial_specialization (tree); |
193 | static enum template_base_result get_template_base (tree, tree, tree, tree, |
194 | bool , tree *); |
195 | static tree try_class_unification (tree, tree, tree, tree, bool); |
196 | static bool class_nttp_const_wrapper_p (tree t); |
197 | static int coerce_template_template_parms (tree, tree, tsubst_flags_t, |
198 | tree, tree); |
199 | static bool template_template_parm_bindings_ok_p (tree, tree); |
200 | static void tsubst_default_arguments (tree, tsubst_flags_t); |
201 | static tree for_each_template_parm_r (tree *, int *, void *); |
202 | static tree copy_default_args_to_explicit_spec_1 (tree, tree); |
203 | static void copy_default_args_to_explicit_spec (tree); |
204 | static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t); |
205 | static bool dependent_template_arg_p (tree); |
206 | static bool dependent_type_p_r (tree); |
207 | static tree tsubst_stmt (tree, tree, tsubst_flags_t, tree); |
208 | static tree tsubst_decl (tree, tree, tsubst_flags_t, bool = true); |
209 | static tree tsubst_scope (tree, tree, tsubst_flags_t, tree); |
210 | static tree tsubst_name (tree, tree, tsubst_flags_t, tree); |
211 | static void perform_instantiation_time_access_checks (tree, tree); |
212 | static tree listify (tree); |
213 | static tree listify_autos (tree, tree); |
214 | static tree tsubst_template_parm (tree, tree, tsubst_flags_t); |
215 | static tree instantiate_alias_template (tree, tree, tsubst_flags_t); |
216 | static tree get_underlying_template (tree); |
217 | static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree); |
218 | static tree canonicalize_expr_argument (tree, tsubst_flags_t); |
219 | static tree make_argument_pack (tree); |
220 | static tree enclosing_instantiation_of (tree tctx); |
221 | static void instantiate_body (tree pattern, tree args, tree d, bool nested); |
222 | static tree maybe_dependent_member_ref (tree, tree, tsubst_flags_t, tree); |
223 | static void mark_template_arguments_used (tree, tree); |
224 | static bool uses_outer_template_parms (tree); |
225 | static tree alias_ctad_tweaks (tree, tree); |
226 | static tree inherited_ctad_tweaks (tree, tree, tsubst_flags_t); |
227 | static tree deduction_guides_for (tree, bool&, tsubst_flags_t); |
228 | |
229 | /* Make the current scope suitable for access checking when we are |
230 | processing T. T can be FUNCTION_DECL for instantiated function |
231 | template, VAR_DECL for static member variable, or TYPE_DECL for |
232 | for a class or alias template (needed by instantiate_decl). */ |
233 | |
234 | void |
235 | push_access_scope (tree t) |
236 | { |
237 | gcc_assert (VAR_OR_FUNCTION_DECL_P (t) |
238 | || TREE_CODE (t) == TYPE_DECL); |
239 | |
240 | if (DECL_FRIEND_CONTEXT (t)) |
241 | push_nested_class (DECL_FRIEND_CONTEXT (t)); |
242 | else if (DECL_IMPLICIT_TYPEDEF_P (t) |
243 | && CLASS_TYPE_P (TREE_TYPE (t))) |
244 | push_nested_class (TREE_TYPE (t)); |
245 | else if (DECL_CLASS_SCOPE_P (t)) |
246 | push_nested_class (DECL_CONTEXT (t)); |
247 | else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t)) |
248 | /* An artificial deduction guide should have the same access as |
249 | the constructor. */ |
250 | push_nested_class (TREE_TYPE (TREE_TYPE (t))); |
251 | else |
252 | push_to_top_level (); |
253 | |
254 | if (TREE_CODE (t) == FUNCTION_DECL) |
255 | { |
256 | vec_safe_push (v&: saved_access_scope, obj: current_function_decl); |
257 | current_function_decl = t; |
258 | } |
259 | } |
260 | |
261 | /* Restore the scope set up by push_access_scope. T is the node we |
262 | are processing. */ |
263 | |
264 | void |
265 | pop_access_scope (tree t) |
266 | { |
267 | if (TREE_CODE (t) == FUNCTION_DECL) |
268 | current_function_decl = saved_access_scope->pop(); |
269 | |
270 | if (DECL_FRIEND_CONTEXT (t) |
271 | || (DECL_IMPLICIT_TYPEDEF_P (t) |
272 | && CLASS_TYPE_P (TREE_TYPE (t))) |
273 | || DECL_CLASS_SCOPE_P (t) |
274 | || (deduction_guide_p (t) && DECL_ARTIFICIAL (t))) |
275 | pop_nested_class (); |
276 | else |
277 | pop_from_top_level (); |
278 | } |
279 | |
280 | /* Do any processing required when DECL (a member template |
281 | declaration) is finished. Returns the TEMPLATE_DECL corresponding |
282 | to DECL, unless it is a specialization, in which case the DECL |
283 | itself is returned. */ |
284 | |
285 | tree |
286 | finish_member_template_decl (tree decl) |
287 | { |
288 | if (decl == error_mark_node) |
289 | return error_mark_node; |
290 | |
291 | gcc_assert (DECL_P (decl)); |
292 | |
293 | if (TREE_CODE (decl) == TYPE_DECL) |
294 | { |
295 | tree type; |
296 | |
297 | type = TREE_TYPE (decl); |
298 | if (type == error_mark_node) |
299 | return error_mark_node; |
300 | if (MAYBE_CLASS_TYPE_P (type) |
301 | && CLASSTYPE_TEMPLATE_INFO (type) |
302 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) |
303 | { |
304 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
305 | check_member_template (tmpl); |
306 | return tmpl; |
307 | } |
308 | return NULL_TREE; |
309 | } |
310 | else if (TREE_CODE (decl) == FIELD_DECL) |
311 | error_at (DECL_SOURCE_LOCATION (decl), |
312 | "data member %qD cannot be a member template", decl); |
313 | else if (DECL_TEMPLATE_INFO (decl)) |
314 | { |
315 | if (!DECL_TEMPLATE_SPECIALIZATION (decl)) |
316 | { |
317 | check_member_template (DECL_TI_TEMPLATE (decl)); |
318 | return DECL_TI_TEMPLATE (decl); |
319 | } |
320 | else |
321 | return NULL_TREE; |
322 | } |
323 | else |
324 | error_at (DECL_SOURCE_LOCATION (decl), |
325 | "invalid member template declaration %qD", decl); |
326 | |
327 | return error_mark_node; |
328 | } |
329 | |
330 | /* Create a template info node. */ |
331 | |
332 | tree |
333 | build_template_info (tree template_decl, tree template_args) |
334 | { |
335 | tree result = make_node (TEMPLATE_INFO); |
336 | TI_TEMPLATE (result) = template_decl; |
337 | TI_ARGS (result) = template_args; |
338 | return result; |
339 | } |
340 | |
341 | /* DECL_TEMPLATE_INFO, if applicable, or NULL_TREE. */ |
342 | |
343 | static tree |
344 | decl_template_info (const_tree decl) |
345 | { |
346 | /* This needs to match template_info_decl_check. */ |
347 | if (DECL_LANG_SPECIFIC (decl)) |
348 | switch (TREE_CODE (decl)) |
349 | { |
350 | case FUNCTION_DECL: |
351 | if (DECL_THUNK_P (decl)) |
352 | break; |
353 | gcc_fallthrough (); |
354 | case VAR_DECL: |
355 | case FIELD_DECL: |
356 | case TYPE_DECL: |
357 | case CONCEPT_DECL: |
358 | case TEMPLATE_DECL: |
359 | return DECL_TEMPLATE_INFO (decl); |
360 | |
361 | default: |
362 | break; |
363 | } |
364 | return NULL_TREE; |
365 | } |
366 | |
367 | /* Return the template info node corresponding to T, whatever T is. */ |
368 | |
369 | tree |
370 | get_template_info (const_tree t) |
371 | { |
372 | tree tinfo = NULL_TREE; |
373 | |
374 | if (!t || t == error_mark_node) |
375 | return NULL; |
376 | |
377 | if (TREE_CODE (t) == NAMESPACE_DECL |
378 | || TREE_CODE (t) == PARM_DECL) |
379 | return NULL; |
380 | |
381 | if (DECL_P (t)) |
382 | tinfo = decl_template_info (decl: t); |
383 | |
384 | if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t)) |
385 | t = TREE_TYPE (t); |
386 | |
387 | if (OVERLOAD_TYPE_P (t)) |
388 | tinfo = TYPE_TEMPLATE_INFO (t); |
389 | else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
390 | tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t); |
391 | |
392 | return tinfo; |
393 | } |
394 | |
395 | /* Returns the template nesting level of the indicated class TYPE. |
396 | |
397 | For example, in: |
398 | template <class T> |
399 | struct A |
400 | { |
401 | template <class U> |
402 | struct B {}; |
403 | }; |
404 | |
405 | A<T>::B<U> has depth two, while A<T> has depth one. |
406 | Both A<T>::B<int> and A<int>::B<U> have depth one, if |
407 | they are instantiations, not specializations. |
408 | |
409 | This function is guaranteed to return 0 if passed NULL_TREE so |
410 | that, for example, `template_class_depth (current_class_type)' is |
411 | always safe. */ |
412 | |
413 | int |
414 | template_class_depth (tree type) |
415 | { |
416 | int depth; |
417 | |
418 | for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; ) |
419 | { |
420 | tree tinfo = get_template_info (t: type); |
421 | |
422 | if (tinfo |
423 | && TREE_CODE (TI_TEMPLATE (tinfo)) == TEMPLATE_DECL |
424 | && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) |
425 | && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))) |
426 | ++depth; |
427 | |
428 | if (DECL_P (type)) |
429 | { |
430 | if (tree fctx = DECL_FRIEND_CONTEXT (type)) |
431 | type = fctx; |
432 | else |
433 | type = CP_DECL_CONTEXT (type); |
434 | } |
435 | else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type)) |
436 | type = LAMBDA_TYPE_EXTRA_SCOPE (type); |
437 | else |
438 | type = CP_TYPE_CONTEXT (type); |
439 | } |
440 | |
441 | return depth; |
442 | } |
443 | |
444 | /* Return TRUE if NODE instantiates a template that has arguments of |
445 | its own, be it directly a primary template or indirectly through a |
446 | partial specializations. */ |
447 | static bool |
448 | instantiates_primary_template_p (tree node) |
449 | { |
450 | tree tinfo = get_template_info (t: node); |
451 | if (!tinfo) |
452 | return false; |
453 | |
454 | tree tmpl = TI_TEMPLATE (tinfo); |
455 | if (PRIMARY_TEMPLATE_P (tmpl)) |
456 | return true; |
457 | |
458 | if (!DECL_TEMPLATE_SPECIALIZATION (tmpl)) |
459 | return false; |
460 | |
461 | /* So now we know we have a specialization, but it could be a full |
462 | or a partial specialization. To tell which, compare the depth of |
463 | its template arguments with those of its context. */ |
464 | |
465 | tree ctxt = DECL_CONTEXT (tmpl); |
466 | tree ctinfo = get_template_info (t: ctxt); |
467 | if (!ctinfo) |
468 | return true; |
469 | |
470 | return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo)) |
471 | > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo))); |
472 | } |
473 | |
474 | /* Subroutine of maybe_begin_member_template_processing. |
475 | Returns true if processing DECL needs us to push template parms. */ |
476 | |
477 | static bool |
478 | inline_needs_template_parms (tree decl, bool nsdmi) |
479 | { |
480 | if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl))) |
481 | return false; |
482 | |
483 | return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl))) |
484 | > (current_template_depth + DECL_TEMPLATE_SPECIALIZATION (decl))); |
485 | } |
486 | |
487 | /* Subroutine of maybe_begin_member_template_processing. |
488 | Push the template parms in PARMS, starting from LEVELS steps into the |
489 | chain, and ending at the beginning, since template parms are listed |
490 | innermost first. */ |
491 | |
492 | static void |
493 | push_inline_template_parms_recursive (tree parmlist, int levels) |
494 | { |
495 | tree parms = TREE_VALUE (parmlist); |
496 | int i; |
497 | |
498 | if (levels > 1) |
499 | push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels: levels - 1); |
500 | |
501 | ++processing_template_decl; |
502 | current_template_parms |
503 | = tree_cons (size_int (current_template_depth + 1), |
504 | parms, current_template_parms); |
505 | TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) |
506 | = TEMPLATE_PARMS_CONSTRAINTS (parmlist); |
507 | TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; |
508 | |
509 | begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, |
510 | NULL); |
511 | for (i = 0; i < TREE_VEC_LENGTH (parms); ++i) |
512 | { |
513 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
514 | |
515 | if (error_operand_p (t: parm)) |
516 | continue; |
517 | |
518 | gcc_assert (DECL_P (parm)); |
519 | |
520 | switch (TREE_CODE (parm)) |
521 | { |
522 | case TYPE_DECL: |
523 | case TEMPLATE_DECL: |
524 | pushdecl (parm); |
525 | break; |
526 | |
527 | case PARM_DECL: |
528 | /* Push the CONST_DECL. */ |
529 | pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm))); |
530 | break; |
531 | |
532 | default: |
533 | gcc_unreachable (); |
534 | } |
535 | } |
536 | } |
537 | |
538 | /* Restore the template parameter context for a member template, a |
539 | friend template defined in a class definition, or a non-template |
540 | member of template class. */ |
541 | |
542 | void |
543 | maybe_begin_member_template_processing (tree decl) |
544 | { |
545 | tree parms; |
546 | int levels = 0; |
547 | bool nsdmi = TREE_CODE (decl) == FIELD_DECL; |
548 | |
549 | if (nsdmi) |
550 | { |
551 | tree ctx = DECL_CONTEXT (decl); |
552 | decl = (CLASSTYPE_TEMPLATE_INFO (ctx) |
553 | /* Disregard full specializations (c++/60999). */ |
554 | && uses_template_parms (ctx) |
555 | ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE); |
556 | } |
557 | |
558 | if (inline_needs_template_parms (decl, nsdmi)) |
559 | { |
560 | parms = DECL_TEMPLATE_PARMS (most_general_template (decl)); |
561 | levels = TMPL_PARMS_DEPTH (parms) - current_template_depth; |
562 | |
563 | if (DECL_TEMPLATE_SPECIALIZATION (decl)) |
564 | { |
565 | --levels; |
566 | parms = TREE_CHAIN (parms); |
567 | } |
568 | |
569 | push_inline_template_parms_recursive (parmlist: parms, levels); |
570 | } |
571 | |
572 | /* Remember how many levels of template parameters we pushed so that |
573 | we can pop them later. */ |
574 | inline_parm_levels.safe_push (obj: levels); |
575 | } |
576 | |
577 | /* Undo the effects of maybe_begin_member_template_processing. */ |
578 | |
579 | void |
580 | maybe_end_member_template_processing (void) |
581 | { |
582 | int i; |
583 | int last; |
584 | |
585 | if (inline_parm_levels.length () == 0) |
586 | return; |
587 | |
588 | last = inline_parm_levels.pop (); |
589 | for (i = 0; i < last; ++i) |
590 | { |
591 | --processing_template_decl; |
592 | current_template_parms = TREE_CHAIN (current_template_parms); |
593 | poplevel (0, 0, 0); |
594 | } |
595 | } |
596 | |
597 | /* Return a new template argument vector which contains all of ARGS, |
598 | but has as its innermost set of arguments the EXTRA_ARGS. */ |
599 | |
600 | tree |
601 | add_to_template_args (tree args, tree extra_args) |
602 | { |
603 | tree new_args; |
604 | int extra_depth; |
605 | int i; |
606 | int j; |
607 | |
608 | if (args == NULL_TREE || extra_args == error_mark_node) |
609 | return extra_args; |
610 | |
611 | extra_depth = TMPL_ARGS_DEPTH (extra_args); |
612 | new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth); |
613 | |
614 | for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i) |
615 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i)); |
616 | |
617 | for (j = 1; j <= extra_depth; ++j, ++i) |
618 | SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j)); |
619 | |
620 | return new_args; |
621 | } |
622 | |
623 | /* Like add_to_template_args, but only the outermost ARGS are added to |
624 | the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH |
625 | (EXTRA_ARGS) levels are added. This function is used to combine |
626 | the template arguments from a partial instantiation with the |
627 | template arguments used to attain the full instantiation from the |
628 | partial instantiation. |
629 | |
630 | If ARGS is a TEMPLATE_DECL, use its parameters as args. */ |
631 | |
632 | tree |
633 | add_outermost_template_args (tree args, tree extra_args) |
634 | { |
635 | tree new_args; |
636 | |
637 | if (!args) |
638 | return extra_args; |
639 | if (TREE_CODE (args) == TEMPLATE_DECL) |
640 | { |
641 | tree ti = get_template_info (DECL_TEMPLATE_RESULT (args)); |
642 | args = TI_ARGS (ti); |
643 | } |
644 | |
645 | /* If there are more levels of EXTRA_ARGS than there are ARGS, |
646 | something very fishy is going on. */ |
647 | gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args)); |
648 | |
649 | /* If *all* the new arguments will be the EXTRA_ARGS, just return |
650 | them. */ |
651 | if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args)) |
652 | return extra_args; |
653 | |
654 | /* For the moment, we make ARGS look like it contains fewer levels. */ |
655 | TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args); |
656 | |
657 | new_args = add_to_template_args (args, extra_args); |
658 | |
659 | /* Now, we restore ARGS to its full dimensions. */ |
660 | TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args); |
661 | |
662 | return new_args; |
663 | } |
664 | |
665 | /* Return the N levels of innermost template arguments from the ARGS. */ |
666 | |
667 | tree |
668 | get_innermost_template_args (tree args, int n) |
669 | { |
670 | tree new_args; |
671 | int extra_levels; |
672 | int i; |
673 | |
674 | gcc_assert (n >= 0); |
675 | |
676 | /* If N is 1, just return the innermost set of template arguments. */ |
677 | if (n == 1) |
678 | return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args)); |
679 | |
680 | /* If we're not removing anything, just return the arguments we were |
681 | given. */ |
682 | extra_levels = TMPL_ARGS_DEPTH (args) - n; |
683 | gcc_assert (extra_levels >= 0); |
684 | if (extra_levels == 0) |
685 | return args; |
686 | |
687 | /* Make a new set of arguments, not containing the outer arguments. */ |
688 | new_args = make_tree_vec (n); |
689 | for (i = 1; i <= n; ++i) |
690 | SET_TMPL_ARGS_LEVEL (new_args, i, |
691 | TMPL_ARGS_LEVEL (args, i + extra_levels)); |
692 | |
693 | return new_args; |
694 | } |
695 | |
696 | /* The inverse of get_innermost_template_args: Return all but the innermost |
697 | EXTRA_LEVELS levels of template arguments from the ARGS. */ |
698 | |
699 | static tree |
700 | strip_innermost_template_args (tree args, int extra_levels) |
701 | { |
702 | tree new_args; |
703 | int n = TMPL_ARGS_DEPTH (args) - extra_levels; |
704 | int i; |
705 | |
706 | gcc_assert (n >= 0); |
707 | |
708 | /* If N is 1, just return the outermost set of template arguments. */ |
709 | if (n == 1) |
710 | return TMPL_ARGS_LEVEL (args, 1); |
711 | |
712 | /* If we're not removing anything, just return the arguments we were |
713 | given. */ |
714 | gcc_assert (extra_levels >= 0); |
715 | if (extra_levels == 0) |
716 | return args; |
717 | |
718 | /* Make a new set of arguments, not containing the inner arguments. */ |
719 | new_args = make_tree_vec (n); |
720 | for (i = 1; i <= n; ++i) |
721 | SET_TMPL_ARGS_LEVEL (new_args, i, |
722 | TMPL_ARGS_LEVEL (args, i)); |
723 | |
724 | return new_args; |
725 | } |
726 | |
727 | /* We've got a template header coming up; push to a new level for storing |
728 | the parms. */ |
729 | |
730 | void |
731 | begin_template_parm_list (void) |
732 | { |
733 | /* We use a non-tag-transparent scope here, which causes pushtag to |
734 | put tags in this scope, rather than in the enclosing class or |
735 | namespace scope. This is the right thing, since we want |
736 | TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a |
737 | global template class, push_template_decl handles putting the |
738 | TEMPLATE_DECL into top-level scope. For a nested template class, |
739 | e.g.: |
740 | |
741 | template <class T> struct S1 { |
742 | template <class T> struct S2 {}; |
743 | }; |
744 | |
745 | pushtag contains special code to insert the TEMPLATE_DECL for S2 |
746 | at the right scope. */ |
747 | begin_scope (sk_template_parms, NULL); |
748 | ++processing_template_decl; |
749 | ++processing_template_parmlist; |
750 | note_template_header (0); |
751 | |
752 | /* Add a dummy parameter level while we process the parameter list. */ |
753 | current_template_parms |
754 | = tree_cons (size_int (current_template_depth + 1), |
755 | make_tree_vec (0), |
756 | current_template_parms); |
757 | } |
758 | |
759 | /* This routine is called when a specialization is declared. If it is |
760 | invalid to declare a specialization here, an error is reported and |
761 | false is returned, otherwise this routine will return true. */ |
762 | |
763 | static bool |
764 | check_specialization_scope (void) |
765 | { |
766 | tree scope = current_scope (); |
767 | |
768 | /* [temp.expl.spec] |
769 | |
770 | An explicit specialization shall be declared in the namespace of |
771 | which the template is a member, or, for member templates, in the |
772 | namespace of which the enclosing class or enclosing class |
773 | template is a member. An explicit specialization of a member |
774 | function, member class or static data member of a class template |
775 | shall be declared in the namespace of which the class template |
776 | is a member. */ |
777 | if (scope && TREE_CODE (scope) != NAMESPACE_DECL) |
778 | { |
779 | error ("explicit specialization in non-namespace scope %qD", scope); |
780 | return false; |
781 | } |
782 | |
783 | /* [temp.expl.spec] |
784 | |
785 | In an explicit specialization declaration for a member of a class |
786 | template or a member template that appears in namespace scope, |
787 | the member template and some of its enclosing class templates may |
788 | remain unspecialized, except that the declaration shall not |
789 | explicitly specialize a class member template if its enclosing |
790 | class templates are not explicitly specialized as well. */ |
791 | if (current_template_parms) |
792 | { |
793 | error ("enclosing class templates are not explicitly specialized"); |
794 | return false; |
795 | } |
796 | |
797 | return true; |
798 | } |
799 | |
800 | /* We've just seen template <>. */ |
801 | |
802 | bool |
803 | begin_specialization (void) |
804 | { |
805 | begin_scope (sk_template_spec, NULL); |
806 | note_template_header (1); |
807 | return check_specialization_scope (); |
808 | } |
809 | |
810 | /* Called at then end of processing a declaration preceded by |
811 | template<>. */ |
812 | |
813 | void |
814 | end_specialization (void) |
815 | { |
816 | finish_scope (); |
817 | reset_specialization (); |
818 | } |
819 | |
820 | /* Any template <>'s that we have seen thus far are not referring to a |
821 | function specialization. */ |
822 | |
823 | void |
824 | reset_specialization (void) |
825 | { |
826 | processing_specialization = 0; |
827 | template_header_count = 0; |
828 | } |
829 | |
830 | /* We've just seen a template header. If SPECIALIZATION is nonzero, |
831 | it was of the form template <>. */ |
832 | |
833 | static void |
834 | note_template_header (int specialization) |
835 | { |
836 | processing_specialization = specialization; |
837 | template_header_count++; |
838 | } |
839 | |
840 | /* We're beginning an explicit instantiation. */ |
841 | |
842 | void |
843 | begin_explicit_instantiation (void) |
844 | { |
845 | gcc_assert (!processing_explicit_instantiation); |
846 | processing_explicit_instantiation = true; |
847 | } |
848 | |
849 | |
850 | void |
851 | end_explicit_instantiation (void) |
852 | { |
853 | gcc_assert (processing_explicit_instantiation); |
854 | processing_explicit_instantiation = false; |
855 | } |
856 | |
857 | /* An explicit specialization or partial specialization of TMPL is being |
858 | declared. Check that the namespace in which the specialization is |
859 | occurring is permissible. Returns false iff it is invalid to |
860 | specialize TMPL in the current namespace. */ |
861 | |
862 | static bool |
863 | check_specialization_namespace (tree tmpl) |
864 | { |
865 | tree tpl_ns = decl_namespace_context (tmpl); |
866 | |
867 | /* [tmpl.expl.spec] |
868 | |
869 | An explicit specialization shall be declared in a namespace enclosing the |
870 | specialized template. An explicit specialization whose declarator-id is |
871 | not qualified shall be declared in the nearest enclosing namespace of the |
872 | template, or, if the namespace is inline (7.3.1), any namespace from its |
873 | enclosing namespace set. */ |
874 | if (current_scope() != DECL_CONTEXT (tmpl) |
875 | && !at_namespace_scope_p ()) |
876 | { |
877 | error ("specialization of %qD must appear at namespace scope", tmpl); |
878 | return false; |
879 | } |
880 | |
881 | if (is_nested_namespace (current_namespace, descendant: tpl_ns, inline_only: cxx_dialect < cxx11)) |
882 | /* Same or enclosing namespace. */ |
883 | return true; |
884 | else |
885 | { |
886 | auto_diagnostic_group d; |
887 | if (permerror (input_location, |
888 | "specialization of %qD in different namespace", tmpl)) |
889 | inform (DECL_SOURCE_LOCATION (tmpl), |
890 | " from definition of %q#D", tmpl); |
891 | return false; |
892 | } |
893 | } |
894 | |
895 | /* SPEC is an explicit instantiation. Check that it is valid to |
896 | perform this explicit instantiation in the current namespace. */ |
897 | |
898 | static void |
899 | check_explicit_instantiation_namespace (tree spec) |
900 | { |
901 | tree ns; |
902 | |
903 | /* DR 275: An explicit instantiation shall appear in an enclosing |
904 | namespace of its template. */ |
905 | ns = decl_namespace_context (spec); |
906 | if (!is_nested_namespace (current_namespace, descendant: ns)) |
907 | permerror (input_location, "explicit instantiation of %qD in namespace %qD " |
908 | "(which does not enclose namespace %qD)", |
909 | spec, current_namespace, ns); |
910 | } |
911 | |
912 | /* Returns true if TYPE is a new partial specialization that needs to be |
913 | set up. This may also modify TYPE to point to the correct (new or |
914 | existing) constrained partial specialization. */ |
915 | |
916 | static bool |
917 | maybe_new_partial_specialization (tree& type) |
918 | { |
919 | /* An implicit instantiation of an incomplete type implies |
920 | the definition of a new class template. |
921 | |
922 | template<typename T> |
923 | struct S; |
924 | |
925 | template<typename T> |
926 | struct S<T*>; |
927 | |
928 | Here, S<T*> is an implicit instantiation of S whose type |
929 | is incomplete. */ |
930 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type)) |
931 | return true; |
932 | |
933 | /* It can also be the case that TYPE is a completed specialization. |
934 | Continuing the previous example, suppose we also declare: |
935 | |
936 | template<typename T> |
937 | requires Integral<T> |
938 | struct S<T*>; |
939 | |
940 | Here, S<T*> refers to the specialization S<T*> defined |
941 | above. However, we need to differentiate definitions because |
942 | we intend to define a new partial specialization. In this case, |
943 | we rely on the fact that the constraints are different for |
944 | this declaration than that above. |
945 | |
946 | Note that we also get here for injected class names and |
947 | late-parsed template definitions. We must ensure that we |
948 | do not create new type declarations for those cases. */ |
949 | if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) |
950 | { |
951 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
952 | tree args = CLASSTYPE_TI_ARGS (type); |
953 | |
954 | /* If there are no template parameters, this cannot be a new |
955 | partial template specialization? */ |
956 | if (!current_template_parms) |
957 | return false; |
958 | |
959 | /* The injected-class-name is not a new partial specialization. */ |
960 | if (DECL_SELF_REFERENCE_P (TYPE_NAME (type))) |
961 | return false; |
962 | |
963 | /* If the constraints are not the same as those of the primary |
964 | then, we can probably create a new specialization. */ |
965 | tree type_constr = current_template_constraints (); |
966 | |
967 | if (type == TREE_TYPE (tmpl)) |
968 | { |
969 | tree main_constr = get_constraints (tmpl); |
970 | if (equivalent_constraints (type_constr, main_constr)) |
971 | return false; |
972 | } |
973 | |
974 | /* Also, if there's a pre-existing specialization with matching |
975 | constraints, then this also isn't new. */ |
976 | tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
977 | while (specs) |
978 | { |
979 | tree spec_tmpl = TREE_VALUE (specs); |
980 | tree spec_args = TREE_PURPOSE (specs); |
981 | tree spec_constr = get_constraints (spec_tmpl); |
982 | if (comp_template_args (args, spec_args) |
983 | && equivalent_constraints (type_constr, spec_constr)) |
984 | { |
985 | type = TREE_TYPE (spec_tmpl); |
986 | return false; |
987 | } |
988 | specs = TREE_CHAIN (specs); |
989 | } |
990 | |
991 | /* Create a new type node (and corresponding type decl) |
992 | for the newly declared specialization. */ |
993 | tree t = make_class_type (TREE_CODE (type)); |
994 | CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type); |
995 | SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args)); |
996 | TYPE_CONTEXT (t) = TYPE_CONTEXT (type); |
997 | |
998 | /* We only need a separate type node for storing the definition of this |
999 | partial specialization; uses of S<T*> are unconstrained, so all are |
1000 | equivalent. So keep TYPE_CANONICAL the same. */ |
1001 | TYPE_CANONICAL (t) = TYPE_CANONICAL (type); |
1002 | |
1003 | /* Build the corresponding type decl. */ |
1004 | tree d = create_implicit_typedef (DECL_NAME (tmpl), t); |
1005 | DECL_CONTEXT (d) = TYPE_CONTEXT (t); |
1006 | DECL_SOURCE_LOCATION (d) = input_location; |
1007 | TREE_PUBLIC (d) = TREE_PUBLIC (DECL_TEMPLATE_RESULT (tmpl)); |
1008 | |
1009 | set_instantiating_module (d); |
1010 | DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl); |
1011 | |
1012 | type = t; |
1013 | return true; |
1014 | } |
1015 | |
1016 | return false; |
1017 | } |
1018 | |
1019 | /* The TYPE is being declared. If it is a template type, that means it |
1020 | is a partial specialization. Do appropriate error-checking. */ |
1021 | |
1022 | tree |
1023 | maybe_process_partial_specialization (tree type) |
1024 | { |
1025 | tree context; |
1026 | |
1027 | if (type == error_mark_node) |
1028 | return error_mark_node; |
1029 | |
1030 | /* A lambda that appears in specialization context is not itself a |
1031 | specialization. */ |
1032 | if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type)) |
1033 | return type; |
1034 | |
1035 | /* An injected-class-name is not a specialization. */ |
1036 | if (DECL_SELF_REFERENCE_P (TYPE_NAME (type))) |
1037 | return type; |
1038 | |
1039 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) |
1040 | { |
1041 | error ("name of class shadows template template parameter %qD", |
1042 | TYPE_NAME (type)); |
1043 | return error_mark_node; |
1044 | } |
1045 | |
1046 | context = TYPE_CONTEXT (type); |
1047 | |
1048 | if (TYPE_ALIAS_P (type)) |
1049 | { |
1050 | tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type); |
1051 | |
1052 | if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo))) |
1053 | error ("specialization of alias template %qD", |
1054 | TI_TEMPLATE (tinfo)); |
1055 | else |
1056 | error ("explicit specialization of non-template %qT", type); |
1057 | return error_mark_node; |
1058 | } |
1059 | else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type)) |
1060 | { |
1061 | /* This is for ordinary explicit specialization and partial |
1062 | specialization of a template class such as: |
1063 | |
1064 | template <> class C<int>; |
1065 | |
1066 | or: |
1067 | |
1068 | template <class T> class C<T*>; |
1069 | |
1070 | Make sure that `C<int>' and `C<T*>' are implicit instantiations. */ |
1071 | |
1072 | if (maybe_new_partial_specialization (type)) |
1073 | { |
1074 | if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)) |
1075 | && !at_namespace_scope_p ()) |
1076 | return error_mark_node; |
1077 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
1078 | DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location; |
1079 | if (processing_template_decl) |
1080 | { |
1081 | tree decl = push_template_decl (TYPE_MAIN_DECL (type)); |
1082 | if (decl == error_mark_node) |
1083 | return error_mark_node; |
1084 | return TREE_TYPE (decl); |
1085 | } |
1086 | } |
1087 | else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type)) |
1088 | error ("specialization of %qT after instantiation", type); |
1089 | else if (errorcount && !processing_specialization |
1090 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (type) |
1091 | && !uses_template_parms (CLASSTYPE_TI_ARGS (type))) |
1092 | /* Trying to define a specialization either without a template<> header |
1093 | or in an inappropriate place. We've already given an error, so just |
1094 | bail now so we don't actually define the specialization. */ |
1095 | return error_mark_node; |
1096 | } |
1097 | else if (CLASS_TYPE_P (type) |
1098 | && !CLASSTYPE_USE_TEMPLATE (type) |
1099 | && CLASSTYPE_TEMPLATE_INFO (type) |
1100 | && context && CLASS_TYPE_P (context) |
1101 | && CLASSTYPE_TEMPLATE_INFO (context)) |
1102 | { |
1103 | /* This is for an explicit specialization of member class |
1104 | template according to [temp.expl.spec/18]: |
1105 | |
1106 | template <> template <class U> class C<int>::D; |
1107 | |
1108 | The context `C<int>' must be an implicit instantiation. |
1109 | Otherwise this is just a member class template declared |
1110 | earlier like: |
1111 | |
1112 | template <> class C<int> { template <class U> class D; }; |
1113 | template <> template <class U> class C<int>::D; |
1114 | |
1115 | In the first case, `C<int>::D' is a specialization of `C<T>::D' |
1116 | while in the second case, `C<int>::D' is a primary template |
1117 | and `C<T>::D' may not exist. */ |
1118 | |
1119 | if (CLASSTYPE_IMPLICIT_INSTANTIATION (context) |
1120 | && !COMPLETE_TYPE_P (type)) |
1121 | { |
1122 | tree t; |
1123 | tree tmpl = CLASSTYPE_TI_TEMPLATE (type); |
1124 | |
1125 | if (current_namespace |
1126 | != decl_namespace_context (tmpl)) |
1127 | { |
1128 | auto_diagnostic_group d; |
1129 | if (permerror (input_location, |
1130 | "specialization of %qD in different namespace", |
1131 | type)) |
1132 | inform (DECL_SOURCE_LOCATION (tmpl), |
1133 | "from definition of %q#D", tmpl); |
1134 | } |
1135 | |
1136 | /* Check for invalid specialization after instantiation: |
1137 | |
1138 | template <> template <> class C<int>::D<int>; |
1139 | template <> template <class U> class C<int>::D; */ |
1140 | |
1141 | for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
1142 | t; t = TREE_CHAIN (t)) |
1143 | { |
1144 | tree inst = TREE_VALUE (t); |
1145 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst) |
1146 | || !COMPLETE_OR_OPEN_TYPE_P (inst)) |
1147 | { |
1148 | /* We already have a full specialization of this partial |
1149 | instantiation, or a full specialization has been |
1150 | looked up but not instantiated. Reassign it to the |
1151 | new member specialization template. */ |
1152 | spec_entry elt; |
1153 | spec_entry *entry; |
1154 | |
1155 | elt.tmpl = most_general_template (tmpl); |
1156 | elt.args = CLASSTYPE_TI_ARGS (inst); |
1157 | elt.spec = inst; |
1158 | |
1159 | type_specializations->remove_elt (value: &elt); |
1160 | |
1161 | elt.tmpl = tmpl; |
1162 | CLASSTYPE_TI_ARGS (inst) |
1163 | = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args); |
1164 | elt.hash = 0; /* Recalculate after changing tmpl/args. */ |
1165 | |
1166 | spec_entry **slot |
1167 | = type_specializations->find_slot (value: &elt, insert: INSERT); |
1168 | entry = ggc_alloc<spec_entry> (); |
1169 | *entry = elt; |
1170 | *slot = entry; |
1171 | } |
1172 | else |
1173 | /* But if we've had an implicit instantiation, that's a |
1174 | problem ([temp.expl.spec]/6). */ |
1175 | error ("specialization %qT after instantiation %qT", |
1176 | type, inst); |
1177 | } |
1178 | |
1179 | /* Make sure that the specialization is valid. */ |
1180 | if (!redeclare_class_template (type, current_template_parms, |
1181 | current_template_constraints ())) |
1182 | return error_mark_node; |
1183 | |
1184 | /* Mark TYPE as a specialization. And as a result, we only |
1185 | have one level of template argument for the innermost |
1186 | class template. */ |
1187 | SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); |
1188 | DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location; |
1189 | CLASSTYPE_TI_ARGS (type) |
1190 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); |
1191 | } |
1192 | } |
1193 | else if (processing_specialization) |
1194 | { |
1195 | /* Someday C++0x may allow for enum template specialization. */ |
1196 | if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE |
1197 | && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context)) |
1198 | pedwarn (input_location, OPT_Wpedantic, "template specialization " |
1199 | "of %qD not allowed by ISO C++", type); |
1200 | else |
1201 | { |
1202 | error ("explicit specialization of non-template %qT", type); |
1203 | return error_mark_node; |
1204 | } |
1205 | } |
1206 | |
1207 | return type; |
1208 | } |
1209 | |
1210 | /* Make sure ARGS doesn't use any inappropriate typedefs; we should have |
1211 | gone through coerce_template_parms by now. */ |
1212 | |
1213 | static void |
1214 | verify_unstripped_args_1 (tree inner) |
1215 | { |
1216 | for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i) |
1217 | { |
1218 | tree arg = TREE_VEC_ELT (inner, i); |
1219 | if (TREE_CODE (arg) == TEMPLATE_DECL) |
1220 | /* OK */; |
1221 | else if (TYPE_P (arg)) |
1222 | gcc_assert (strip_typedefs (arg, NULL) == arg); |
1223 | else if (ARGUMENT_PACK_P (arg)) |
1224 | verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg)); |
1225 | else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg)) |
1226 | /* Allow typedefs on the type of a non-type argument, since a |
1227 | parameter can have them. */; |
1228 | else |
1229 | gcc_assert (strip_typedefs_expr (arg, NULL) == arg); |
1230 | } |
1231 | } |
1232 | |
1233 | static void |
1234 | verify_unstripped_args (tree args) |
1235 | { |
1236 | ++processing_template_decl; |
1237 | if (!any_dependent_template_arguments_p (args)) |
1238 | verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args)); |
1239 | --processing_template_decl; |
1240 | } |
1241 | |
1242 | /* Retrieve the specialization (in the sense of [temp.spec] - a |
1243 | specialization is either an instantiation or an explicit |
1244 | specialization) of TMPL for the given template ARGS. If there is |
1245 | no such specialization, return NULL_TREE. The ARGS are a vector of |
1246 | arguments, or a vector of vectors of arguments, in the case of |
1247 | templates with more than one level of parameters. |
1248 | |
1249 | If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true, |
1250 | then we search for a partial specialization matching ARGS. This |
1251 | parameter is ignored if TMPL is not a class template. |
1252 | |
1253 | We can also look up a FIELD_DECL, if it is a lambda capture pack; the |
1254 | result is a NONTYPE_ARGUMENT_PACK. */ |
1255 | |
1256 | static tree |
1257 | retrieve_specialization (tree tmpl, tree args, hashval_t hash) |
1258 | { |
1259 | if (tmpl == NULL_TREE) |
1260 | return NULL_TREE; |
1261 | |
1262 | if (args == error_mark_node) |
1263 | return NULL_TREE; |
1264 | |
1265 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL |
1266 | || TREE_CODE (tmpl) == FIELD_DECL); |
1267 | |
1268 | /* There should be as many levels of arguments as there are |
1269 | levels of parameters. */ |
1270 | gcc_assert (TMPL_ARGS_DEPTH (args) |
1271 | == (TREE_CODE (tmpl) == TEMPLATE_DECL |
1272 | ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)) |
1273 | : template_class_depth (DECL_CONTEXT (tmpl)))); |
1274 | |
1275 | if (flag_checking) |
1276 | verify_unstripped_args (args); |
1277 | |
1278 | /* Lambda functions in templates aren't instantiated normally, but through |
1279 | tsubst_lambda_expr. */ |
1280 | if (lambda_fn_in_template_p (tmpl)) |
1281 | return NULL_TREE; |
1282 | |
1283 | spec_entry elt; |
1284 | elt.tmpl = tmpl; |
1285 | elt.args = args; |
1286 | elt.hash = hash; |
1287 | |
1288 | spec_hash_table *specializations; |
1289 | if (DECL_CLASS_TEMPLATE_P (tmpl)) |
1290 | specializations = type_specializations; |
1291 | else |
1292 | specializations = decl_specializations; |
1293 | |
1294 | if (spec_entry *found = specializations->find (value: &elt)) |
1295 | return found->spec; |
1296 | |
1297 | return NULL_TREE; |
1298 | } |
1299 | |
1300 | /* Like retrieve_specialization, but for local declarations. */ |
1301 | |
1302 | tree |
1303 | retrieve_local_specialization (tree tmpl) |
1304 | { |
1305 | if (local_specializations == NULL) |
1306 | return NULL_TREE; |
1307 | |
1308 | tree *slot = local_specializations->get (k: tmpl); |
1309 | return slot ? *slot : NULL_TREE; |
1310 | } |
1311 | |
1312 | /* Returns nonzero iff DECL is a specialization of TMPL. */ |
1313 | |
1314 | int |
1315 | is_specialization_of (tree decl, tree tmpl) |
1316 | { |
1317 | tree t; |
1318 | |
1319 | if (TREE_CODE (decl) == FUNCTION_DECL) |
1320 | { |
1321 | for (t = decl; |
1322 | t != NULL_TREE; |
1323 | t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE) |
1324 | if (t == tmpl) |
1325 | return 1; |
1326 | } |
1327 | else |
1328 | { |
1329 | gcc_assert (TREE_CODE (decl) == TYPE_DECL); |
1330 | |
1331 | for (t = TREE_TYPE (decl); |
1332 | t != NULL_TREE; |
1333 | t = CLASSTYPE_USE_TEMPLATE (t) |
1334 | ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE) |
1335 | if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl))) |
1336 | return 1; |
1337 | } |
1338 | |
1339 | return 0; |
1340 | } |
1341 | |
1342 | /* Returns nonzero iff DECL is a specialization of friend declaration |
1343 | FRIEND_DECL according to [temp.friend]. */ |
1344 | |
1345 | bool |
1346 | is_specialization_of_friend (tree decl, tree friend_decl) |
1347 | { |
1348 | bool need_template = true; |
1349 | int template_depth; |
1350 | |
1351 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL |
1352 | || TREE_CODE (decl) == TYPE_DECL); |
1353 | |
1354 | /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function |
1355 | of a template class, we want to check if DECL is a specialization |
1356 | if this. */ |
1357 | if (TREE_CODE (friend_decl) == FUNCTION_DECL |
1358 | && DECL_CLASS_SCOPE_P (friend_decl) |
1359 | && DECL_TEMPLATE_INFO (friend_decl) |
1360 | && !DECL_USE_TEMPLATE (friend_decl)) |
1361 | { |
1362 | /* We want a TEMPLATE_DECL for `is_specialization_of'. */ |
1363 | friend_decl = DECL_TI_TEMPLATE (friend_decl); |
1364 | need_template = false; |
1365 | } |
1366 | else if (TREE_CODE (friend_decl) == TEMPLATE_DECL |
1367 | && !PRIMARY_TEMPLATE_P (friend_decl)) |
1368 | need_template = false; |
1369 | |
1370 | /* There is nothing to do if this is not a template friend. */ |
1371 | if (TREE_CODE (friend_decl) != TEMPLATE_DECL) |
1372 | return false; |
1373 | |
1374 | if (is_specialization_of (decl, tmpl: friend_decl)) |
1375 | return true; |
1376 | |
1377 | /* [temp.friend/6] |
1378 | A member of a class template may be declared to be a friend of a |
1379 | non-template class. In this case, the corresponding member of |
1380 | every specialization of the class template is a friend of the |
1381 | class granting friendship. |
1382 | |
1383 | For example, given a template friend declaration |
1384 | |
1385 | template <class T> friend void A<T>::f(); |
1386 | |
1387 | the member function below is considered a friend |
1388 | |
1389 | template <> struct A<int> { |
1390 | void f(); |
1391 | }; |
1392 | |
1393 | For this type of template friend, TEMPLATE_DEPTH below will be |
1394 | nonzero. To determine if DECL is a friend of FRIEND, we first |
1395 | check if the enclosing class is a specialization of another. */ |
1396 | |
1397 | template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl)); |
1398 | if (template_depth |
1399 | && DECL_CLASS_SCOPE_P (decl) |
1400 | && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)), |
1401 | CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl)))) |
1402 | { |
1403 | /* Next, we check the members themselves. In order to handle |
1404 | a few tricky cases, such as when FRIEND_DECL's are |
1405 | |
1406 | template <class T> friend void A<T>::g(T t); |
1407 | template <class T> template <T t> friend void A<T>::h(); |
1408 | |
1409 | and DECL's are |
1410 | |
1411 | void A<int>::g(int); |
1412 | template <int> void A<int>::h(); |
1413 | |
1414 | we need to figure out ARGS, the template arguments from |
1415 | the context of DECL. This is required for template substitution |
1416 | of `T' in the function parameter of `g' and template parameter |
1417 | of `h' in the above examples. Here ARGS corresponds to `int'. */ |
1418 | |
1419 | tree context = DECL_CONTEXT (decl); |
1420 | tree args = NULL_TREE; |
1421 | int current_depth = 0; |
1422 | |
1423 | while (current_depth < template_depth) |
1424 | { |
1425 | if (CLASSTYPE_TEMPLATE_INFO (context)) |
1426 | { |
1427 | if (current_depth == 0) |
1428 | args = TYPE_TI_ARGS (context); |
1429 | else |
1430 | args = add_to_template_args (TYPE_TI_ARGS (context), extra_args: args); |
1431 | current_depth++; |
1432 | } |
1433 | context = TYPE_CONTEXT (context); |
1434 | } |
1435 | |
1436 | if (TREE_CODE (decl) == FUNCTION_DECL) |
1437 | { |
1438 | bool is_template; |
1439 | tree friend_type; |
1440 | tree decl_type; |
1441 | tree friend_args_type; |
1442 | tree decl_args_type; |
1443 | |
1444 | /* Make sure that both DECL and FRIEND_DECL are templates or |
1445 | non-templates. */ |
1446 | is_template = DECL_TEMPLATE_INFO (decl) |
1447 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)); |
1448 | if (need_template ^ is_template) |
1449 | return false; |
1450 | else if (is_template) |
1451 | { |
1452 | /* If both are templates, check template parameter list. */ |
1453 | tree friend_parms |
1454 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
1455 | args, tf_none); |
1456 | if (!comp_template_parms |
1457 | (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)), |
1458 | friend_parms)) |
1459 | return false; |
1460 | |
1461 | decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl)); |
1462 | } |
1463 | else |
1464 | decl_type = TREE_TYPE (decl); |
1465 | |
1466 | friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args, |
1467 | tf_none, NULL_TREE); |
1468 | if (friend_type == error_mark_node) |
1469 | return false; |
1470 | |
1471 | /* Check if return types match. */ |
1472 | if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type))) |
1473 | return false; |
1474 | |
1475 | /* Check if function parameter types match, ignoring the |
1476 | `this' parameter. */ |
1477 | friend_args_type = TYPE_ARG_TYPES (friend_type); |
1478 | decl_args_type = TYPE_ARG_TYPES (decl_type); |
1479 | if (DECL_IOBJ_MEMBER_FUNCTION_P (friend_decl)) |
1480 | friend_args_type = TREE_CHAIN (friend_args_type); |
1481 | if (DECL_IOBJ_MEMBER_FUNCTION_P (decl)) |
1482 | decl_args_type = TREE_CHAIN (decl_args_type); |
1483 | |
1484 | return compparms (decl_args_type, friend_args_type); |
1485 | } |
1486 | else |
1487 | { |
1488 | /* DECL is a TYPE_DECL */ |
1489 | bool is_template; |
1490 | tree decl_type = TREE_TYPE (decl); |
1491 | |
1492 | /* Make sure that both DECL and FRIEND_DECL are templates or |
1493 | non-templates. */ |
1494 | is_template |
1495 | = CLASSTYPE_TEMPLATE_INFO (decl_type) |
1496 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type)); |
1497 | |
1498 | if (need_template ^ is_template) |
1499 | return false; |
1500 | else if (is_template) |
1501 | { |
1502 | tree friend_parms; |
1503 | /* If both are templates, check the name of the two |
1504 | TEMPLATE_DECL's first because is_friend didn't. */ |
1505 | if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type)) |
1506 | != DECL_NAME (friend_decl)) |
1507 | return false; |
1508 | |
1509 | /* Now check template parameter list. */ |
1510 | friend_parms |
1511 | = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl), |
1512 | args, tf_none); |
1513 | return comp_template_parms |
1514 | (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)), |
1515 | friend_parms); |
1516 | } |
1517 | else |
1518 | return (DECL_NAME (decl) |
1519 | == DECL_NAME (friend_decl)); |
1520 | } |
1521 | } |
1522 | return false; |
1523 | } |
1524 | |
1525 | /* Register the specialization SPEC as a specialization of TMPL with |
1526 | the indicated ARGS. IS_FRIEND indicates whether the specialization |
1527 | is actually just a friend declaration. ATTRLIST is the list of |
1528 | attributes that the specialization is declared with or NULL when |
1529 | it isn't. Returns SPEC, or an equivalent prior declaration, if |
1530 | available. |
1531 | |
1532 | We also store instantiations of field packs in the hash table, even |
1533 | though they are not themselves templates, to make lookup easier. */ |
1534 | |
1535 | static tree |
1536 | register_specialization (tree spec, tree tmpl, tree args, bool is_friend, |
1537 | hashval_t hash) |
1538 | { |
1539 | tree fn; |
1540 | |
1541 | gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec)) |
1542 | || (TREE_CODE (tmpl) == FIELD_DECL |
1543 | && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)); |
1544 | |
1545 | spec_entry elt; |
1546 | elt.tmpl = tmpl; |
1547 | elt.args = args; |
1548 | elt.spec = spec; |
1549 | elt.hash = hash; |
1550 | |
1551 | spec_entry **slot = decl_specializations->find_slot (value: &elt, insert: INSERT); |
1552 | if (*slot) |
1553 | fn = (*slot)->spec; |
1554 | else |
1555 | fn = NULL_TREE; |
1556 | |
1557 | /* We can sometimes try to re-register a specialization that we've |
1558 | already got. In particular, regenerate_decl_from_template calls |
1559 | duplicate_decls which will update the specialization list. But, |
1560 | we'll still get called again here anyhow. It's more convenient |
1561 | to simply allow this than to try to prevent it. */ |
1562 | if (fn == spec) |
1563 | return spec; |
1564 | else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec)) |
1565 | { |
1566 | if (DECL_TEMPLATE_INSTANTIATION (fn)) |
1567 | { |
1568 | if (DECL_ODR_USED (fn) |
1569 | || DECL_EXPLICIT_INSTANTIATION (fn)) |
1570 | { |
1571 | error ("specialization of %qD after instantiation", |
1572 | fn); |
1573 | return error_mark_node; |
1574 | } |
1575 | else |
1576 | { |
1577 | tree clone; |
1578 | /* This situation should occur only if the first |
1579 | specialization is an implicit instantiation, the |
1580 | second is an explicit specialization, and the |
1581 | implicit instantiation has not yet been used. That |
1582 | situation can occur if we have implicitly |
1583 | instantiated a member function and then specialized |
1584 | it later. |
1585 | |
1586 | We can also wind up here if a friend declaration that |
1587 | looked like an instantiation turns out to be a |
1588 | specialization: |
1589 | |
1590 | template <class T> void foo(T); |
1591 | class S { friend void foo<>(int) }; |
1592 | template <> void foo(int); |
1593 | |
1594 | We transform the existing DECL in place so that any |
1595 | pointers to it become pointers to the updated |
1596 | declaration. |
1597 | |
1598 | If there was a definition for the template, but not |
1599 | for the specialization, we want this to look as if |
1600 | there were no definition, and vice versa. */ |
1601 | DECL_INITIAL (fn) = NULL_TREE; |
1602 | duplicate_decls (spec, fn, /*hiding=*/is_friend); |
1603 | |
1604 | /* The call to duplicate_decls will have applied |
1605 | [temp.expl.spec]: |
1606 | |
1607 | An explicit specialization of a function template |
1608 | is inline only if it is explicitly declared to be, |
1609 | and independently of whether its function template |
1610 | is. |
1611 | |
1612 | to the primary function; now copy the inline bits to |
1613 | the various clones. */ |
1614 | FOR_EACH_CLONE (clone, fn) |
1615 | { |
1616 | DECL_DECLARED_INLINE_P (clone) |
1617 | = DECL_DECLARED_INLINE_P (fn); |
1618 | DECL_SOURCE_LOCATION (clone) |
1619 | = DECL_SOURCE_LOCATION (fn); |
1620 | DECL_DELETED_FN (clone) |
1621 | = DECL_DELETED_FN (fn); |
1622 | } |
1623 | check_specialization_namespace (tmpl); |
1624 | |
1625 | return fn; |
1626 | } |
1627 | } |
1628 | else if (DECL_TEMPLATE_SPECIALIZATION (fn)) |
1629 | { |
1630 | tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend); |
1631 | if (dd == error_mark_node) |
1632 | /* We've already complained in duplicate_decls. */ |
1633 | return error_mark_node; |
1634 | |
1635 | if (dd == NULL_TREE && DECL_INITIAL (spec)) |
1636 | /* Dup decl failed, but this is a new definition. Set the |
1637 | line number so any errors match this new |
1638 | definition. */ |
1639 | DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec); |
1640 | |
1641 | return fn; |
1642 | } |
1643 | } |
1644 | else if (fn) |
1645 | return duplicate_decls (spec, fn, /*hiding=*/is_friend); |
1646 | |
1647 | /* A specialization must be declared in the same namespace as the |
1648 | template it is specializing. */ |
1649 | if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec) |
1650 | && !check_specialization_namespace (tmpl)) |
1651 | DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl); |
1652 | |
1653 | spec_entry *entry = ggc_alloc<spec_entry> (); |
1654 | gcc_assert (tmpl && args && spec); |
1655 | *entry = elt; |
1656 | *slot = entry; |
1657 | if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec) |
1658 | && PRIMARY_TEMPLATE_P (tmpl) |
1659 | && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE) |
1660 | || module_maybe_has_cmi_p () |
1661 | || variable_template_p (t: tmpl)) |
1662 | /* If TMPL is a forward declaration of a template function, keep a list |
1663 | of all specializations in case we need to reassign them to a friend |
1664 | template later in tsubst_friend_function. |
1665 | |
1666 | If we're building a CMI, keep a list for all function templates. |
1667 | |
1668 | Also keep a list of all variable template instantiations so that |
1669 | process_partial_specialization can check whether a later partial |
1670 | specialization would have used it. */ |
1671 | DECL_TEMPLATE_INSTANTIATIONS (tmpl) |
1672 | = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl)); |
1673 | |
1674 | return spec; |
1675 | } |
1676 | |
1677 | /* Restricts tree and type comparisons. */ |
1678 | int comparing_specializations; |
1679 | int comparing_dependent_aliases; |
1680 | |
1681 | /* Whether we are comparing template arguments during partial ordering |
1682 | (and therefore want the comparison to look through dependent alias |
1683 | template specializations). */ |
1684 | |
1685 | static int comparing_for_partial_ordering; |
1686 | |
1687 | /* Returns true iff two spec_entry nodes are equivalent. */ |
1688 | |
1689 | bool |
1690 | spec_hasher::equal (spec_entry *e1, spec_entry *e2) |
1691 | { |
1692 | int equal; |
1693 | |
1694 | ++comparing_specializations; |
1695 | ++comparing_dependent_aliases; |
1696 | ++processing_template_decl; |
1697 | equal = (e1->tmpl == e2->tmpl |
1698 | && comp_template_args (e1->args, e2->args)); |
1699 | if (equal && flag_concepts |
1700 | /* tmpl could be a FIELD_DECL for a capture pack. */ |
1701 | && TREE_CODE (e1->tmpl) == TEMPLATE_DECL |
1702 | && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl)) |
1703 | && uses_template_parms (e1->args)) |
1704 | { |
1705 | /* Partial specializations of a variable template can be distinguished by |
1706 | constraints. */ |
1707 | tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE; |
1708 | tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE; |
1709 | equal = equivalent_constraints (c1, c2); |
1710 | } |
1711 | --processing_template_decl; |
1712 | --comparing_dependent_aliases; |
1713 | --comparing_specializations; |
1714 | |
1715 | return equal; |
1716 | } |
1717 | |
1718 | /* Returns a hash for a template TMPL and template arguments ARGS. */ |
1719 | |
1720 | static hashval_t |
1721 | hash_tmpl_and_args (tree tmpl, tree args) |
1722 | { |
1723 | hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0); |
1724 | return iterative_hash_template_arg (arg: args, val); |
1725 | } |
1726 | |
1727 | hashval_t |
1728 | spec_hasher::hash (tree tmpl, tree args) |
1729 | { |
1730 | ++comparing_specializations; |
1731 | hashval_t val = hash_tmpl_and_args (tmpl, args); |
1732 | --comparing_specializations; |
1733 | return val; |
1734 | } |
1735 | |
1736 | /* Returns a hash for a spec_entry node based on the TMPL and ARGS members, |
1737 | ignoring SPEC. */ |
1738 | |
1739 | hashval_t |
1740 | spec_hasher::hash (spec_entry *e) |
1741 | { |
1742 | if (e->hash == 0) |
1743 | e->hash = hash (tmpl: e->tmpl, args: e->args); |
1744 | return e->hash; |
1745 | } |
1746 | |
1747 | /* Recursively calculate a hash value for a template argument ARG, for use |
1748 | in the hash tables of template specializations. We must be |
1749 | careful to (at least) skip the same entities template_args_equal |
1750 | does. */ |
1751 | |
1752 | hashval_t |
1753 | iterative_hash_template_arg (tree arg, hashval_t val) |
1754 | { |
1755 | if (arg == NULL_TREE) |
1756 | return iterative_hash_hashval_t (val: 0, val2: val); |
1757 | |
1758 | if (!TYPE_P (arg)) |
1759 | /* Strip nop-like things, but not the same as STRIP_NOPS. */ |
1760 | while (CONVERT_EXPR_P (arg) |
1761 | || TREE_CODE (arg) == NON_LVALUE_EXPR |
1762 | || class_nttp_const_wrapper_p (t: arg)) |
1763 | arg = TREE_OPERAND (arg, 0); |
1764 | |
1765 | enum tree_code code = TREE_CODE (arg); |
1766 | |
1767 | val = iterative_hash_hashval_t (val: code, val2: val); |
1768 | |
1769 | switch (code) |
1770 | { |
1771 | case ARGUMENT_PACK_SELECT: |
1772 | /* Getting here with an ARGUMENT_PACK_SELECT means we're probably |
1773 | preserving it in a hash table, which is bad because it will change |
1774 | meaning when gen_elem_of_pack_expansion_instantiation changes the |
1775 | ARGUMENT_PACK_SELECT_INDEX. */ |
1776 | gcc_unreachable (); |
1777 | |
1778 | case ERROR_MARK: |
1779 | return val; |
1780 | |
1781 | case IDENTIFIER_NODE: |
1782 | return iterative_hash_hashval_t (IDENTIFIER_HASH_VALUE (arg), val2: val); |
1783 | |
1784 | case TREE_VEC: |
1785 | for (tree elt : tree_vec_range (arg)) |
1786 | val = iterative_hash_template_arg (arg: elt, val); |
1787 | return val; |
1788 | |
1789 | case TYPE_PACK_EXPANSION: |
1790 | case EXPR_PACK_EXPANSION: |
1791 | val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val); |
1792 | return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val); |
1793 | |
1794 | case PACK_INDEX_TYPE: |
1795 | case PACK_INDEX_EXPR: |
1796 | val = iterative_hash_template_arg (PACK_INDEX_PACK (arg), val); |
1797 | return iterative_hash_template_arg (PACK_INDEX_INDEX (arg), val); |
1798 | |
1799 | case TYPE_ARGUMENT_PACK: |
1800 | case NONTYPE_ARGUMENT_PACK: |
1801 | return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val); |
1802 | |
1803 | case TREE_LIST: |
1804 | for (; arg; arg = TREE_CHAIN (arg)) |
1805 | val = iterative_hash_template_arg (TREE_VALUE (arg), val); |
1806 | return val; |
1807 | |
1808 | case OVERLOAD: |
1809 | for (lkp_iterator iter (arg); iter; ++iter) |
1810 | val = iterative_hash_template_arg (arg: *iter, val); |
1811 | return val; |
1812 | |
1813 | case CONSTRUCTOR: |
1814 | { |
1815 | iterative_hash_template_arg (TREE_TYPE (arg), val); |
1816 | for (auto &e: CONSTRUCTOR_ELTS (arg)) |
1817 | { |
1818 | val = iterative_hash_template_arg (arg: e.index, val); |
1819 | val = iterative_hash_template_arg (arg: e.value, val); |
1820 | } |
1821 | return val; |
1822 | } |
1823 | |
1824 | case PARM_DECL: |
1825 | if (!DECL_ARTIFICIAL (arg)) |
1826 | { |
1827 | val = iterative_hash_object (DECL_PARM_INDEX (arg), val); |
1828 | val = iterative_hash_object (DECL_PARM_LEVEL (arg), val); |
1829 | } |
1830 | return iterative_hash_template_arg (TREE_TYPE (arg), val); |
1831 | |
1832 | case TEMPLATE_DECL: |
1833 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) |
1834 | return iterative_hash_template_arg (TREE_TYPE (arg), val); |
1835 | break; |
1836 | |
1837 | case TARGET_EXPR: |
1838 | return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val); |
1839 | |
1840 | case PTRMEM_CST: |
1841 | val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val); |
1842 | return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val); |
1843 | |
1844 | case TEMPLATE_PARM_INDEX: |
1845 | val = iterative_hash_template_arg |
1846 | (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val); |
1847 | val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val); |
1848 | return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val); |
1849 | |
1850 | case TRAIT_EXPR: |
1851 | val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val); |
1852 | val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val); |
1853 | return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val); |
1854 | |
1855 | case BASELINK: |
1856 | val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)), |
1857 | val); |
1858 | return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)), |
1859 | val); |
1860 | |
1861 | case MODOP_EXPR: |
1862 | val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val); |
1863 | code = TREE_CODE (TREE_OPERAND (arg, 1)); |
1864 | val = iterative_hash_object (code, val); |
1865 | return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val); |
1866 | |
1867 | case LAMBDA_EXPR: |
1868 | /* [temp.over.link] Two lambda-expressions are never considered |
1869 | equivalent. |
1870 | |
1871 | So just hash the closure type. */ |
1872 | return iterative_hash_template_arg (TREE_TYPE (arg), val); |
1873 | |
1874 | case CAST_EXPR: |
1875 | case IMPLICIT_CONV_EXPR: |
1876 | case STATIC_CAST_EXPR: |
1877 | case REINTERPRET_CAST_EXPR: |
1878 | case CONST_CAST_EXPR: |
1879 | case DYNAMIC_CAST_EXPR: |
1880 | case NEW_EXPR: |
1881 | val = iterative_hash_template_arg (TREE_TYPE (arg), val); |
1882 | /* Now hash operands as usual. */ |
1883 | break; |
1884 | |
1885 | case CALL_EXPR: |
1886 | { |
1887 | tree fn = CALL_EXPR_FN (arg); |
1888 | if (tree name = call_expr_dependent_name (arg)) |
1889 | { |
1890 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) |
1891 | val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val); |
1892 | fn = name; |
1893 | } |
1894 | val = iterative_hash_template_arg (arg: fn, val); |
1895 | call_expr_arg_iterator ai; |
1896 | for (tree x = first_call_expr_arg (exp: arg, iter: &ai); x; |
1897 | x = next_call_expr_arg (iter: &ai)) |
1898 | val = iterative_hash_template_arg (arg: x, val); |
1899 | return val; |
1900 | } |
1901 | |
1902 | default: |
1903 | break; |
1904 | } |
1905 | |
1906 | char tclass = TREE_CODE_CLASS (code); |
1907 | switch (tclass) |
1908 | { |
1909 | case tcc_type: |
1910 | if (tree ats = alias_template_specialization_p (arg, nt_transparent)) |
1911 | { |
1912 | // We want an alias specialization that survived strip_typedefs |
1913 | // to hash differently from its TYPE_CANONICAL, to avoid hash |
1914 | // collisions that compare as different in template_args_equal. |
1915 | // These could be dependent specializations that strip_typedefs |
1916 | // left alone for example. |
1917 | tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats); |
1918 | return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti)); |
1919 | } |
1920 | |
1921 | switch (code) |
1922 | { |
1923 | case DECLTYPE_TYPE: |
1924 | val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val); |
1925 | break; |
1926 | |
1927 | case TYPENAME_TYPE: |
1928 | if (comparing_specializations) |
1929 | { |
1930 | /* Hash the components that are relevant to TYPENAME_TYPE |
1931 | equivalence as determined by structural_comptypes. We |
1932 | can only coherently do this when comparing_specializations |
1933 | is set, because otherwise structural_comptypes tries |
1934 | resolving TYPENAME_TYPE via the current instantiation. */ |
1935 | tree context = TYPE_MAIN_VARIANT (TYPE_CONTEXT (arg)); |
1936 | tree fullname = TYPENAME_TYPE_FULLNAME (arg); |
1937 | val = iterative_hash_template_arg (arg: context, val); |
1938 | val = iterative_hash_template_arg (arg: fullname, val); |
1939 | } |
1940 | break; |
1941 | |
1942 | default: |
1943 | if (tree canonical = TYPE_CANONICAL (arg)) |
1944 | val = iterative_hash_hashval_t (TYPE_HASH (canonical), val2: val); |
1945 | else if (tree ti = TYPE_TEMPLATE_INFO (arg)) |
1946 | { |
1947 | val = iterative_hash_template_arg (TI_TEMPLATE (ti), val); |
1948 | val = iterative_hash_template_arg (TI_ARGS (ti), val); |
1949 | } |
1950 | break; |
1951 | } |
1952 | |
1953 | return val; |
1954 | |
1955 | case tcc_declaration: |
1956 | case tcc_constant: |
1957 | return iterative_hash_expr (tree: arg, seed: val); |
1958 | |
1959 | default: |
1960 | gcc_assert (IS_EXPR_CODE_CLASS (tclass)); |
1961 | for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i) |
1962 | val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val); |
1963 | return val; |
1964 | } |
1965 | } |
1966 | |
1967 | /* Unregister the specialization SPEC as a specialization of TMPL. |
1968 | Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true |
1969 | if the SPEC was listed as a specialization of TMPL. |
1970 | |
1971 | Note that SPEC has been ggc_freed, so we can't look inside it. */ |
1972 | |
1973 | bool |
1974 | reregister_specialization (tree spec, tree tinfo, tree new_spec) |
1975 | { |
1976 | spec_entry *entry; |
1977 | spec_entry elt; |
1978 | |
1979 | elt.tmpl = most_general_template (TI_TEMPLATE (tinfo)); |
1980 | elt.args = TI_ARGS (tinfo); |
1981 | |
1982 | entry = decl_specializations->find (value: &elt); |
1983 | if (entry != NULL) |
1984 | { |
1985 | gcc_assert (entry->spec == spec || entry->spec == new_spec); |
1986 | gcc_assert (new_spec != NULL_TREE); |
1987 | entry->spec = new_spec; |
1988 | |
1989 | /* We need to also remove SPEC from DECL_TEMPLATE_INSTANTIATIONS |
1990 | if it was placed there. */ |
1991 | for (tree *inst = &DECL_TEMPLATE_INSTANTIATIONS (elt.tmpl); |
1992 | *inst; inst = &TREE_CHAIN (*inst)) |
1993 | if (TREE_VALUE (*inst) == spec) |
1994 | { |
1995 | *inst = TREE_CHAIN (*inst); |
1996 | break; |
1997 | } |
1998 | |
1999 | return 1; |
2000 | } |
2001 | |
2002 | return 0; |
2003 | } |
2004 | |
2005 | /* Like register_specialization, but for local declarations. We are |
2006 | registering SPEC, an instantiation of TMPL. */ |
2007 | |
2008 | void |
2009 | register_local_specialization (tree spec, tree tmpl) |
2010 | { |
2011 | gcc_assert (tmpl != spec); |
2012 | local_specializations->put (k: tmpl, v: spec); |
2013 | } |
2014 | |
2015 | /* Registers T as a specialization of itself. This is used to preserve |
2016 | the references to already-parsed parameters when instantiating |
2017 | postconditions. */ |
2018 | |
2019 | void |
2020 | register_local_identity (tree t) |
2021 | { |
2022 | local_specializations->put (k: t, v: t); |
2023 | } |
2024 | |
2025 | /* TYPE is a class type. Returns true if TYPE is an explicitly |
2026 | specialized class. */ |
2027 | |
2028 | bool |
2029 | explicit_class_specialization_p (tree type) |
2030 | { |
2031 | if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type)) |
2032 | return false; |
2033 | return !uses_template_parms (CLASSTYPE_TI_ARGS (type)); |
2034 | } |
2035 | |
2036 | /* Print the list of functions at FNS, going through all the overloads |
2037 | for each element of the list. Alternatively, FNS cannot be a |
2038 | TREE_LIST, in which case it will be printed together with all the |
2039 | overloads. |
2040 | |
2041 | MORE and *STR should respectively be FALSE and NULL when the function |
2042 | is called from the outside. They are used internally on recursive |
2043 | calls. print_candidates manages the two parameters and leaves NULL |
2044 | in *STR when it ends. */ |
2045 | |
2046 | static void |
2047 | print_candidates_1 (tree fns, char **str, bool more = false) |
2048 | { |
2049 | if (TREE_CODE (fns) == TREE_LIST) |
2050 | for (; fns; fns = TREE_CHAIN (fns)) |
2051 | print_candidates_1 (TREE_VALUE (fns), str, more: more || TREE_CHAIN (fns)); |
2052 | else |
2053 | for (lkp_iterator iter (fns); iter;) |
2054 | { |
2055 | tree cand = *iter; |
2056 | ++iter; |
2057 | |
2058 | const char *pfx = *str; |
2059 | if (!pfx) |
2060 | { |
2061 | if (more || iter) |
2062 | pfx = _("candidates are:"); |
2063 | else |
2064 | pfx = _("candidate is:"); |
2065 | *str = get_spaces (pfx); |
2066 | } |
2067 | inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand); |
2068 | } |
2069 | } |
2070 | |
2071 | /* Print the list of candidate FNS in an error message. FNS can also |
2072 | be a TREE_LIST of non-functions in the case of an ambiguous lookup. */ |
2073 | |
2074 | void |
2075 | print_candidates (tree fns) |
2076 | { |
2077 | char *str = NULL; |
2078 | print_candidates_1 (fns, str: &str); |
2079 | free (ptr: str); |
2080 | } |
2081 | |
2082 | /* Get a (possibly) constrained template declaration for the |
2083 | purpose of ordering candidates. */ |
2084 | static tree |
2085 | get_template_for_ordering (tree list) |
2086 | { |
2087 | gcc_assert (TREE_CODE (list) == TREE_LIST); |
2088 | tree f = TREE_VALUE (list); |
2089 | if (f == NULL_TREE) |
2090 | /* Also handle a list from resolve_address_of_overloaded_function with the |
2091 | function in TREE_PURPOSE. */ |
2092 | f = TREE_PURPOSE (list); |
2093 | if (tree ti = DECL_TEMPLATE_INFO (f)) |
2094 | return TI_TEMPLATE (ti); |
2095 | return f; |
2096 | } |
2097 | |
2098 | /* Among candidates having the same signature, return the |
2099 | most constrained or NULL_TREE if there is no best candidate. |
2100 | If the signatures of candidates vary (e.g., template |
2101 | specialization vs. member function), then there can be no |
2102 | most constrained. |
2103 | |
2104 | Note that we don't compare constraints on the functions |
2105 | themselves, but rather those of their templates. */ |
2106 | tree |
2107 | most_constrained_function (tree candidates) |
2108 | { |
2109 | // Try to find the best candidate in a first pass. |
2110 | tree champ = candidates; |
2111 | for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c)) |
2112 | { |
2113 | int winner = more_constrained (get_template_for_ordering (list: champ), |
2114 | get_template_for_ordering (list: c)); |
2115 | if (winner == -1) |
2116 | champ = c; // The candidate is more constrained |
2117 | else if (winner == 0) |
2118 | return NULL_TREE; // Neither is more constrained |
2119 | } |
2120 | |
2121 | // Verify that the champ is better than previous candidates. |
2122 | for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) { |
2123 | if (!more_constrained (get_template_for_ordering (list: champ), |
2124 | get_template_for_ordering (list: c))) |
2125 | return NULL_TREE; |
2126 | } |
2127 | |
2128 | return champ; |
2129 | } |
2130 | |
2131 | |
2132 | /* Returns the template (one of the functions given by TEMPLATE_ID) |
2133 | which can be specialized to match the indicated DECL with the |
2134 | explicit template args given in TEMPLATE_ID. The DECL may be |
2135 | NULL_TREE if none is available. In that case, the functions in |
2136 | TEMPLATE_ID are non-members. |
2137 | |
2138 | If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a |
2139 | specialization of a member template. |
2140 | |
2141 | The TEMPLATE_COUNT is the number of references to qualifying |
2142 | template classes that appeared in the name of the function. See |
2143 | check_explicit_specialization for a more accurate description. |
2144 | |
2145 | TSK indicates what kind of template declaration (if any) is being |
2146 | declared. TSK_TEMPLATE indicates that the declaration given by |
2147 | DECL, though a FUNCTION_DECL, has template parameters, and is |
2148 | therefore a template function. |
2149 | |
2150 | The template args (those explicitly specified and those deduced) |
2151 | are output in a newly created vector *TARGS_OUT. |
2152 | |
2153 | If it is impossible to determine the result, an error message is |
2154 | issued. The error_mark_node is returned to indicate failure. */ |
2155 | |
2156 | static tree |
2157 | determine_specialization (tree template_id, |
2158 | tree decl, |
2159 | tree* targs_out, |
2160 | int need_member_template, |
2161 | int template_count, |
2162 | tmpl_spec_kind tsk) |
2163 | { |
2164 | tree fns; |
2165 | tree targs; |
2166 | tree explicit_targs; |
2167 | tree candidates = NULL_TREE; |
2168 | |
2169 | /* A TREE_LIST of templates of which DECL may be a specialization. |
2170 | The TREE_VALUE of each node is a TEMPLATE_DECL. The |
2171 | corresponding TREE_PURPOSE is the set of template arguments that, |
2172 | when used to instantiate the template, would produce a function |
2173 | with the signature of DECL. */ |
2174 | tree templates = NULL_TREE; |
2175 | int header_count; |
2176 | cp_binding_level *b; |
2177 | |
2178 | *targs_out = NULL_TREE; |
2179 | |
2180 | if (template_id == error_mark_node || decl == error_mark_node) |
2181 | return error_mark_node; |
2182 | |
2183 | /* We shouldn't be specializing a member template of an |
2184 | unspecialized class template; we already gave an error in |
2185 | check_specialization_scope, now avoid crashing. */ |
2186 | if (!VAR_P (decl) |
2187 | && template_count && DECL_CLASS_SCOPE_P (decl) |
2188 | && template_class_depth (DECL_CONTEXT (decl)) > 0) |
2189 | { |
2190 | gcc_assert (errorcount); |
2191 | return error_mark_node; |
2192 | } |
2193 | |
2194 | fns = TREE_OPERAND (template_id, 0); |
2195 | explicit_targs = TREE_OPERAND (template_id, 1); |
2196 | |
2197 | if (fns == error_mark_node) |
2198 | return error_mark_node; |
2199 | |
2200 | /* Check for baselinks. */ |
2201 | if (BASELINK_P (fns)) |
2202 | fns = BASELINK_FUNCTIONS (fns); |
2203 | |
2204 | if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns)) |
2205 | { |
2206 | error_at (DECL_SOURCE_LOCATION (decl), |
2207 | "%qD is not a function template", fns); |
2208 | return error_mark_node; |
2209 | } |
2210 | else if (VAR_P (decl) && !variable_template_p (t: fns)) |
2211 | { |
2212 | error ("%qD is not a variable template", fns); |
2213 | return error_mark_node; |
2214 | } |
2215 | |
2216 | /* Count the number of template headers specified for this |
2217 | specialization. */ |
2218 | header_count = 0; |
2219 | for (b = current_binding_level; |
2220 | b->kind == sk_template_parms; |
2221 | b = b->level_chain) |
2222 | ++header_count; |
2223 | |
2224 | tree orig_fns = fns; |
2225 | bool header_mismatch = false; |
2226 | |
2227 | if (variable_template_p (t: fns)) |
2228 | { |
2229 | tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns)); |
2230 | targs = coerce_template_parms (parms, explicit_targs, fns, |
2231 | tf_warning_or_error); |
2232 | if (targs != error_mark_node |
2233 | && constraints_satisfied_p (fns, targs)) |
2234 | templates = tree_cons (targs, fns, templates); |
2235 | } |
2236 | else for (lkp_iterator iter (fns); iter; ++iter) |
2237 | { |
2238 | tree fn = *iter; |
2239 | |
2240 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
2241 | { |
2242 | tree decl_arg_types; |
2243 | tree fn_arg_types; |
2244 | |
2245 | /* In case of explicit specialization, we need to check if |
2246 | the number of template headers appearing in the specialization |
2247 | is correct. This is usually done in check_explicit_specialization, |
2248 | but the check done there cannot be exhaustive when specializing |
2249 | member functions. Consider the following code: |
2250 | |
2251 | template <> void A<int>::f(int); |
2252 | template <> template <> void A<int>::f(int); |
2253 | |
2254 | Assuming that A<int> is not itself an explicit specialization |
2255 | already, the first line specializes "f" which is a non-template |
2256 | member function, whilst the second line specializes "f" which |
2257 | is a template member function. So both lines are syntactically |
2258 | correct, and check_explicit_specialization does not reject |
2259 | them. |
2260 | |
2261 | Here, we can do better, as we are matching the specialization |
2262 | against the declarations. We count the number of template |
2263 | headers, and we check if they match TEMPLATE_COUNT + 1 |
2264 | (TEMPLATE_COUNT is the number of qualifying template classes, |
2265 | plus there must be another header for the member template |
2266 | itself). |
2267 | |
2268 | Notice that if header_count is zero, this is not a |
2269 | specialization but rather a template instantiation, so there |
2270 | is no check we can perform here. */ |
2271 | if (header_count && header_count != template_count + 1) |
2272 | { |
2273 | header_mismatch = true; |
2274 | continue; |
2275 | } |
2276 | |
2277 | /* Check that the number of template arguments at the |
2278 | innermost level for DECL is the same as for FN. */ |
2279 | if (current_binding_level->kind == sk_template_parms |
2280 | && !current_binding_level->explicit_spec_p |
2281 | && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn)) |
2282 | != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS |
2283 | (current_template_parms)))) |
2284 | continue; |
2285 | |
2286 | /* DECL might be a specialization of FN. */ |
2287 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
2288 | fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
2289 | |
2290 | /* For a non-static member function, we need to make sure |
2291 | that the const qualification is the same. Since |
2292 | get_bindings does not try to merge the "this" parameter, |
2293 | we must do the comparison explicitly. */ |
2294 | if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)) |
2295 | { |
2296 | if (!same_type_p (TREE_VALUE (fn_arg_types), |
2297 | TREE_VALUE (decl_arg_types))) |
2298 | continue; |
2299 | |
2300 | /* And the ref-qualification. */ |
2301 | if (type_memfn_rqual (TREE_TYPE (decl)) |
2302 | != type_memfn_rqual (TREE_TYPE (fn))) |
2303 | continue; |
2304 | } |
2305 | |
2306 | /* Skip the "this" parameter and, for constructors of |
2307 | classes with virtual bases, the VTT parameter. A |
2308 | full specialization of a constructor will have a VTT |
2309 | parameter, but a template never will. */ |
2310 | decl_arg_types |
2311 | = skip_artificial_parms_for (decl, decl_arg_types); |
2312 | fn_arg_types |
2313 | = skip_artificial_parms_for (fn, fn_arg_types); |
2314 | |
2315 | /* Function templates cannot be specializations; there are |
2316 | no partial specializations of functions. Therefore, if |
2317 | the type of DECL does not match FN, there is no |
2318 | match. |
2319 | |
2320 | Note that it should never be the case that we have both |
2321 | candidates added here, and for regular member functions |
2322 | below. */ |
2323 | if (tsk == tsk_template) |
2324 | { |
2325 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn), |
2326 | current_template_parms)) |
2327 | continue; |
2328 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
2329 | TREE_TYPE (TREE_TYPE (fn)))) |
2330 | continue; |
2331 | if (!compparms (fn_arg_types, decl_arg_types)) |
2332 | continue; |
2333 | |
2334 | tree freq = get_constraints (fn); |
2335 | tree dreq = get_constraints (decl); |
2336 | if (!freq != !dreq) |
2337 | continue; |
2338 | if (freq) |
2339 | { |
2340 | /* C++20 CA104: Substitute directly into the |
2341 | constraint-expression. */ |
2342 | tree fargs = DECL_TI_ARGS (fn); |
2343 | tsubst_flags_t complain = tf_none; |
2344 | freq = tsubst_constraint_info (freq, fargs, complain, fn); |
2345 | if (!cp_tree_equal (freq, dreq)) |
2346 | continue; |
2347 | } |
2348 | |
2349 | candidates = tree_cons (NULL_TREE, fn, candidates); |
2350 | continue; |
2351 | } |
2352 | |
2353 | /* See whether this function might be a specialization of this |
2354 | template. Suppress access control because we might be trying |
2355 | to make this specialization a friend, and we have already done |
2356 | access control for the declaration of the specialization. */ |
2357 | push_deferring_access_checks (dk_no_check); |
2358 | targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true); |
2359 | pop_deferring_access_checks (); |
2360 | |
2361 | if (!targs) |
2362 | /* We cannot deduce template arguments that when used to |
2363 | specialize TMPL will produce DECL. */ |
2364 | continue; |
2365 | |
2366 | if (uses_template_parms (targs)) |
2367 | /* We deduced something involving 'auto', which isn't a valid |
2368 | template argument. */ |
2369 | continue; |
2370 | |
2371 | /* Save this template, and the arguments deduced. */ |
2372 | templates = tree_cons (targs, fn, templates); |
2373 | } |
2374 | else if (need_member_template) |
2375 | /* FN is an ordinary member function, and we need a |
2376 | specialization of a member template. */ |
2377 | ; |
2378 | else if (TREE_CODE (fn) != FUNCTION_DECL) |
2379 | /* We can get IDENTIFIER_NODEs here in certain erroneous |
2380 | cases. */ |
2381 | ; |
2382 | else if (!DECL_FUNCTION_MEMBER_P (fn)) |
2383 | /* This is just an ordinary non-member function. Nothing can |
2384 | be a specialization of that. */ |
2385 | ; |
2386 | else if (DECL_ARTIFICIAL (fn)) |
2387 | /* Cannot specialize functions that are created implicitly. */ |
2388 | ; |
2389 | else |
2390 | { |
2391 | tree decl_arg_types; |
2392 | |
2393 | /* This is an ordinary member function. However, since |
2394 | we're here, we can assume its enclosing class is a |
2395 | template class. For example, |
2396 | |
2397 | template <typename T> struct S { void f(); }; |
2398 | template <> void S<int>::f() {} |
2399 | |
2400 | Here, S<int>::f is a non-template, but S<int> is a |
2401 | template class. If FN has the same type as DECL, we |
2402 | might be in business. */ |
2403 | |
2404 | if (!DECL_TEMPLATE_INFO (fn)) |
2405 | /* Its enclosing class is an explicit specialization |
2406 | of a template class. This is not a candidate. */ |
2407 | continue; |
2408 | |
2409 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
2410 | TREE_TYPE (TREE_TYPE (fn)))) |
2411 | /* The return types differ. */ |
2412 | continue; |
2413 | |
2414 | /* Adjust the type of DECL in case FN is a static member. */ |
2415 | decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
2416 | if (DECL_STATIC_FUNCTION_P (fn) |
2417 | && DECL_IOBJ_MEMBER_FUNCTION_P (decl)) |
2418 | decl_arg_types = TREE_CHAIN (decl_arg_types); |
2419 | |
2420 | if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), |
2421 | decl_arg_types)) |
2422 | continue; |
2423 | |
2424 | if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) |
2425 | && (type_memfn_rqual (TREE_TYPE (decl)) |
2426 | != type_memfn_rqual (TREE_TYPE (fn)))) |
2427 | continue; |
2428 | |
2429 | // If the deduced arguments do not satisfy the constraints, |
2430 | // this is not a candidate. |
2431 | if (flag_concepts && !constraints_satisfied_p (fn)) |
2432 | continue; |
2433 | |
2434 | // Add the candidate. |
2435 | candidates = tree_cons (NULL_TREE, fn, candidates); |
2436 | } |
2437 | } |
2438 | |
2439 | if (templates && TREE_CHAIN (templates)) |
2440 | { |
2441 | /* We have: |
2442 | |
2443 | [temp.expl.spec] |
2444 | |
2445 | It is possible for a specialization with a given function |
2446 | signature to be instantiated from more than one function |
2447 | template. In such cases, explicit specification of the |
2448 | template arguments must be used to uniquely identify the |
2449 | function template specialization being specialized. |
2450 | |
2451 | Note that here, there's no suggestion that we're supposed to |
2452 | determine which of the candidate templates is most |
2453 | specialized. However, we, also have: |
2454 | |
2455 | [temp.func.order] |
2456 | |
2457 | Partial ordering of overloaded function template |
2458 | declarations is used in the following contexts to select |
2459 | the function template to which a function template |
2460 | specialization refers: |
2461 | |
2462 | -- when an explicit specialization refers to a function |
2463 | template. |
2464 | |
2465 | So, we do use the partial ordering rules, at least for now. |
2466 | This extension can only serve to make invalid programs valid, |
2467 | so it's safe. And, there is strong anecdotal evidence that |
2468 | the committee intended the partial ordering rules to apply; |
2469 | the EDG front end has that behavior, and John Spicer claims |
2470 | that the committee simply forgot to delete the wording in |
2471 | [temp.expl.spec]. */ |
2472 | tree tmpl = most_specialized_instantiation (templates); |
2473 | if (tmpl != error_mark_node) |
2474 | { |
2475 | templates = tmpl; |
2476 | TREE_CHAIN (templates) = NULL_TREE; |
2477 | } |
2478 | } |
2479 | |
2480 | // Concepts allows multiple declarations of member functions |
2481 | // with the same signature. Like above, we need to rely on |
2482 | // on the partial ordering of those candidates to determine which |
2483 | // is the best. |
2484 | if (flag_concepts && candidates && TREE_CHAIN (candidates)) |
2485 | { |
2486 | if (tree cand = most_constrained_function (candidates)) |
2487 | { |
2488 | candidates = cand; |
2489 | TREE_CHAIN (cand) = NULL_TREE; |
2490 | } |
2491 | } |
2492 | |
2493 | if (templates == NULL_TREE && candidates == NULL_TREE) |
2494 | { |
2495 | auto_diagnostic_group d; |
2496 | error ("template-id %qD for %q+D does not match any template " |
2497 | "declaration", template_id, decl); |
2498 | if (header_mismatch) |
2499 | inform (DECL_SOURCE_LOCATION (decl), |
2500 | "saw %d %<template<>%>, need %d for " |
2501 | "specializing a member function template", |
2502 | header_count, template_count + 1); |
2503 | print_candidates (fns: orig_fns); |
2504 | return error_mark_node; |
2505 | } |
2506 | else if ((templates && TREE_CHAIN (templates)) |
2507 | || (candidates && TREE_CHAIN (candidates)) |
2508 | || (templates && candidates)) |
2509 | { |
2510 | auto_diagnostic_group d; |
2511 | error ("ambiguous template specialization %qD for %q+D", |
2512 | template_id, decl); |
2513 | candidates = chainon (candidates, templates); |
2514 | print_candidates (fns: candidates); |
2515 | return error_mark_node; |
2516 | } |
2517 | |
2518 | /* We have one, and exactly one, match. */ |
2519 | if (candidates) |
2520 | { |
2521 | tree fn = TREE_VALUE (candidates); |
2522 | *targs_out = copy_node (DECL_TI_ARGS (fn)); |
2523 | |
2524 | /* Propagate the candidate's constraints to the declaration. */ |
2525 | if (tsk != tsk_template) |
2526 | set_constraints (decl, get_constraints (fn)); |
2527 | |
2528 | /* DECL is a re-declaration or partial instantiation of a template |
2529 | function. */ |
2530 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
2531 | return fn; |
2532 | /* It was a specialization of an ordinary member function in a |
2533 | template class. */ |
2534 | return DECL_TI_TEMPLATE (fn); |
2535 | } |
2536 | |
2537 | /* It was a specialization of a template. */ |
2538 | tree tmpl = TREE_VALUE (templates); |
2539 | *targs_out = add_outermost_template_args (args: tmpl, TREE_PURPOSE (templates)); |
2540 | |
2541 | /* Propagate the template's constraints to the declaration. */ |
2542 | if (tsk != tsk_template) |
2543 | set_constraints (decl, get_constraints (tmpl)); |
2544 | |
2545 | return tmpl; |
2546 | } |
2547 | |
2548 | /* Returns a chain of parameter types, exactly like the SPEC_TYPES, |
2549 | but with the default argument values filled in from those in the |
2550 | TMPL_TYPES. */ |
2551 | |
2552 | static tree |
2553 | copy_default_args_to_explicit_spec_1 (tree spec_types, |
2554 | tree tmpl_types) |
2555 | { |
2556 | tree new_spec_types; |
2557 | |
2558 | if (!spec_types) |
2559 | return NULL_TREE; |
2560 | |
2561 | if (spec_types == void_list_node) |
2562 | return void_list_node; |
2563 | |
2564 | /* Substitute into the rest of the list. */ |
2565 | new_spec_types = |
2566 | copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types), |
2567 | TREE_CHAIN (tmpl_types)); |
2568 | |
2569 | /* Add the default argument for this parameter. */ |
2570 | return hash_tree_cons (TREE_PURPOSE (tmpl_types), |
2571 | TREE_VALUE (spec_types), |
2572 | new_spec_types); |
2573 | } |
2574 | |
2575 | /* DECL is an explicit specialization. Replicate default arguments |
2576 | from the template it specializes. (That way, code like: |
2577 | |
2578 | template <class T> void f(T = 3); |
2579 | template <> void f(double); |
2580 | void g () { f (); } |
2581 | |
2582 | works, as required.) An alternative approach would be to look up |
2583 | the correct default arguments at the call-site, but this approach |
2584 | is consistent with how implicit instantiations are handled. */ |
2585 | |
2586 | static void |
2587 | copy_default_args_to_explicit_spec (tree decl) |
2588 | { |
2589 | tree tmpl; |
2590 | tree spec_types; |
2591 | tree tmpl_types; |
2592 | tree new_spec_types; |
2593 | tree old_type; |
2594 | tree new_type; |
2595 | tree t; |
2596 | tree object_type = NULL_TREE; |
2597 | tree in_charge = NULL_TREE; |
2598 | tree vtt = NULL_TREE; |
2599 | |
2600 | /* See if there's anything we need to do. */ |
2601 | tmpl = DECL_TI_TEMPLATE (decl); |
2602 | tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl))); |
2603 | for (t = tmpl_types; t; t = TREE_CHAIN (t)) |
2604 | if (TREE_PURPOSE (t)) |
2605 | break; |
2606 | if (!t) |
2607 | return; |
2608 | |
2609 | old_type = TREE_TYPE (decl); |
2610 | spec_types = TYPE_ARG_TYPES (old_type); |
2611 | |
2612 | if (DECL_IOBJ_MEMBER_FUNCTION_P (decl)) |
2613 | { |
2614 | /* Remove the this pointer, but remember the object's type for |
2615 | CV quals. */ |
2616 | object_type = TREE_TYPE (TREE_VALUE (spec_types)); |
2617 | spec_types = TREE_CHAIN (spec_types); |
2618 | tmpl_types = TREE_CHAIN (tmpl_types); |
2619 | |
2620 | if (DECL_HAS_IN_CHARGE_PARM_P (decl)) |
2621 | { |
2622 | /* DECL may contain more parameters than TMPL due to the extra |
2623 | in-charge parameter in constructors and destructors. */ |
2624 | in_charge = spec_types; |
2625 | spec_types = TREE_CHAIN (spec_types); |
2626 | } |
2627 | if (DECL_HAS_VTT_PARM_P (decl)) |
2628 | { |
2629 | vtt = spec_types; |
2630 | spec_types = TREE_CHAIN (spec_types); |
2631 | } |
2632 | } |
2633 | |
2634 | /* Compute the merged default arguments. */ |
2635 | new_spec_types = |
2636 | copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types); |
2637 | |
2638 | /* Compute the new FUNCTION_TYPE. */ |
2639 | if (object_type) |
2640 | { |
2641 | if (vtt) |
2642 | new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt), |
2643 | TREE_VALUE (vtt), |
2644 | new_spec_types); |
2645 | |
2646 | if (in_charge) |
2647 | /* Put the in-charge parameter back. */ |
2648 | new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge), |
2649 | TREE_VALUE (in_charge), |
2650 | new_spec_types); |
2651 | |
2652 | new_type = build_method_type_directly (object_type, |
2653 | TREE_TYPE (old_type), |
2654 | new_spec_types); |
2655 | } |
2656 | else |
2657 | new_type = build_function_type (TREE_TYPE (old_type), |
2658 | new_spec_types); |
2659 | new_type = cp_build_type_attribute_variant (new_type, |
2660 | TYPE_ATTRIBUTES (old_type)); |
2661 | new_type = cxx_copy_lang_qualifiers (new_type, old_type); |
2662 | |
2663 | TREE_TYPE (decl) = new_type; |
2664 | } |
2665 | |
2666 | /* Return the number of template headers we expect to see for a definition |
2667 | or specialization of CTYPE or one of its non-template members. */ |
2668 | |
2669 | int |
2670 | num_template_headers_for_class (tree ctype) |
2671 | { |
2672 | int num_templates = 0; |
2673 | |
2674 | while (ctype && CLASS_TYPE_P (ctype)) |
2675 | { |
2676 | /* You're supposed to have one `template <...>' for every |
2677 | template class, but you don't need one for a full |
2678 | specialization. For example: |
2679 | |
2680 | template <class T> struct S{}; |
2681 | template <> struct S<int> { void f(); }; |
2682 | void S<int>::f () {} |
2683 | |
2684 | is correct; there shouldn't be a `template <>' for the |
2685 | definition of `S<int>::f'. */ |
2686 | if (!CLASSTYPE_TEMPLATE_INFO (ctype)) |
2687 | /* If CTYPE does not have template information of any |
2688 | kind, then it is not a template, nor is it nested |
2689 | within a template. */ |
2690 | break; |
2691 | if (explicit_class_specialization_p (type: ctype)) |
2692 | break; |
2693 | if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype))) |
2694 | ++num_templates; |
2695 | |
2696 | ctype = TYPE_CONTEXT (ctype); |
2697 | } |
2698 | |
2699 | return num_templates; |
2700 | } |
2701 | |
2702 | /* Do a simple sanity check on the template headers that precede the |
2703 | variable declaration DECL. */ |
2704 | |
2705 | void |
2706 | check_template_variable (tree decl) |
2707 | { |
2708 | tree ctx = CP_DECL_CONTEXT (decl); |
2709 | int wanted = num_template_headers_for_class (ctype: ctx); |
2710 | if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) |
2711 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl))) |
2712 | { |
2713 | if (cxx_dialect < cxx14) |
2714 | pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions, |
2715 | "variable templates only available with " |
2716 | "%<-std=c++14%> or %<-std=gnu++14%>"); |
2717 | |
2718 | // Namespace-scope variable templates should have a template header. |
2719 | ++wanted; |
2720 | } |
2721 | if (template_header_count > wanted) |
2722 | { |
2723 | auto_diagnostic_group d; |
2724 | bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0, |
2725 | "too many template headers for %qD " |
2726 | "(should be %d)", |
2727 | decl, wanted); |
2728 | if (warned && CLASS_TYPE_P (ctx) |
2729 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx)) |
2730 | inform (DECL_SOURCE_LOCATION (decl), |
2731 | "members of an explicitly specialized class are defined " |
2732 | "without a template header"); |
2733 | } |
2734 | } |
2735 | |
2736 | /* An explicit specialization whose declarator-id or class-head-name is not |
2737 | qualified shall be declared in the nearest enclosing namespace of the |
2738 | template, or, if the namespace is inline (7.3.1), any namespace from its |
2739 | enclosing namespace set. |
2740 | |
2741 | If the name declared in the explicit instantiation is an unqualified name, |
2742 | the explicit instantiation shall appear in the namespace where its template |
2743 | is declared or, if that namespace is inline (7.3.1), any namespace from its |
2744 | enclosing namespace set. */ |
2745 | |
2746 | void |
2747 | check_unqualified_spec_or_inst (tree t, location_t loc) |
2748 | { |
2749 | tree tmpl = most_general_template (t); |
2750 | if (DECL_NAMESPACE_SCOPE_P (tmpl) |
2751 | && !is_nested_namespace (current_namespace, |
2752 | CP_DECL_CONTEXT (tmpl), inline_only: true)) |
2753 | { |
2754 | if (processing_specialization) |
2755 | permerror (loc, "explicit specialization of %qD outside its " |
2756 | "namespace must use a nested-name-specifier", tmpl); |
2757 | else if (processing_explicit_instantiation |
2758 | && cxx_dialect >= cxx11) |
2759 | /* This was allowed in C++98, so only pedwarn. */ |
2760 | pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD " |
2761 | "outside its namespace must use a nested-name-" |
2762 | "specifier", tmpl); |
2763 | } |
2764 | } |
2765 | |
2766 | /* Warn for a template specialization SPEC that is missing some of a set |
2767 | of function or type attributes that the template TEMPL is declared with. |
2768 | ATTRLIST is a list of additional attributes that SPEC should be taken |
2769 | to ultimately be declared with. */ |
2770 | |
2771 | static void |
2772 | warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist) |
2773 | { |
2774 | if (DECL_FUNCTION_TEMPLATE_P (tmpl)) |
2775 | tmpl = DECL_TEMPLATE_RESULT (tmpl); |
2776 | |
2777 | /* Avoid warning if the difference between the primary and |
2778 | the specialization is not in one of the attributes below. */ |
2779 | const char* const blacklist[] = { |
2780 | "alloc_align", "alloc_size", "assume_aligned", "format", |
2781 | "format_arg", "malloc", "nonnull", NULL |
2782 | }; |
2783 | |
2784 | /* Put together a list of the black listed attributes that the primary |
2785 | template is declared with that the specialization is not, in case |
2786 | it's not apparent from the most recent declaration of the primary. */ |
2787 | auto_vec<const char *> mismatches; |
2788 | unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist, |
2789 | blacklist, mismatches); |
2790 | |
2791 | if (!nattrs) |
2792 | return; |
2793 | |
2794 | auto_diagnostic_group d; |
2795 | if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes, |
2796 | "explicit specialization %q#D may be missing attributes", |
2797 | spec)) |
2798 | { |
2799 | pp_markup::comma_separated_quoted_strings e (mismatches); |
2800 | inform (DECL_SOURCE_LOCATION (tmpl), |
2801 | nattrs > 1 |
2802 | ? G_("missing primary template attributes %e") |
2803 | : G_("missing primary template attribute %e"), |
2804 | &e); |
2805 | } |
2806 | } |
2807 | |
2808 | /* Check to see if the function just declared, as indicated in |
2809 | DECLARATOR, and in DECL, is a specialization of a function |
2810 | template. We may also discover that the declaration is an explicit |
2811 | instantiation at this point. |
2812 | |
2813 | Returns DECL, or an equivalent declaration that should be used |
2814 | instead if all goes well. Issues an error message if something is |
2815 | amiss. Returns error_mark_node if the error is not easily |
2816 | recoverable. |
2817 | |
2818 | FLAGS is a bitmask consisting of the following flags: |
2819 | |
2820 | 2: The function has a definition. |
2821 | 4: The function is a friend. |
2822 | |
2823 | The TEMPLATE_COUNT is the number of references to qualifying |
2824 | template classes that appeared in the name of the function. For |
2825 | example, in |
2826 | |
2827 | template <class T> struct S { void f(); }; |
2828 | void S<int>::f(); |
2829 | |
2830 | the TEMPLATE_COUNT would be 1. However, explicitly specialized |
2831 | classes are not counted in the TEMPLATE_COUNT, so that in |
2832 | |
2833 | template <class T> struct S {}; |
2834 | template <> struct S<int> { void f(); } |
2835 | template <> void S<int>::f(); |
2836 | |
2837 | the TEMPLATE_COUNT would be 0. (Note that this declaration is |
2838 | invalid; there should be no template <>.) |
2839 | |
2840 | If the function is a specialization, it is marked as such via |
2841 | DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO |
2842 | is set up correctly, and it is added to the list of specializations |
2843 | for that template. */ |
2844 | |
2845 | tree |
2846 | check_explicit_specialization (tree declarator, |
2847 | tree decl, |
2848 | int template_count, |
2849 | int flags, |
2850 | tree attrlist) |
2851 | { |
2852 | int have_def = flags & 2; |
2853 | int is_friend = flags & 4; |
2854 | bool is_concept = flags & 8; |
2855 | int specialization = 0; |
2856 | int explicit_instantiation = 0; |
2857 | int member_specialization = 0; |
2858 | tree ctype = DECL_CLASS_CONTEXT (decl); |
2859 | tree dname = DECL_NAME (decl); |
2860 | tmpl_spec_kind tsk; |
2861 | |
2862 | if (is_friend) |
2863 | { |
2864 | if (!processing_specialization) |
2865 | tsk = tsk_none; |
2866 | else |
2867 | tsk = tsk_excessive_parms; |
2868 | } |
2869 | else |
2870 | tsk = current_tmpl_spec_kind (template_count); |
2871 | |
2872 | switch (tsk) |
2873 | { |
2874 | case tsk_none: |
2875 | if (processing_specialization && !VAR_P (decl)) |
2876 | { |
2877 | specialization = 1; |
2878 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
2879 | } |
2880 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR |
2881 | || (DECL_LANG_SPECIFIC (decl) |
2882 | && DECL_IMPLICIT_INSTANTIATION (decl))) |
2883 | { |
2884 | if (is_friend) |
2885 | /* This could be something like: |
2886 | |
2887 | template <class T> void f(T); |
2888 | class S { friend void f<>(int); } */ |
2889 | specialization = 1; |
2890 | else |
2891 | { |
2892 | /* This case handles bogus declarations like template <> |
2893 | template <class T> void f<int>(); */ |
2894 | |
2895 | error_at (cp_expr_loc_or_input_loc (t: declarator), |
2896 | "template-id %qE in declaration of primary template", |
2897 | declarator); |
2898 | return decl; |
2899 | } |
2900 | } |
2901 | break; |
2902 | |
2903 | case tsk_invalid_member_spec: |
2904 | /* The error has already been reported in |
2905 | check_specialization_scope. */ |
2906 | return error_mark_node; |
2907 | |
2908 | case tsk_invalid_expl_inst: |
2909 | error ("template parameter list used in explicit instantiation"); |
2910 | |
2911 | /* Fall through. */ |
2912 | |
2913 | case tsk_expl_inst: |
2914 | if (have_def) |
2915 | error ("definition provided for explicit instantiation"); |
2916 | |
2917 | explicit_instantiation = 1; |
2918 | break; |
2919 | |
2920 | case tsk_excessive_parms: |
2921 | case tsk_insufficient_parms: |
2922 | if (tsk == tsk_excessive_parms) |
2923 | error ("too many template parameter lists in declaration of %qD", |
2924 | decl); |
2925 | else if (template_header_count) |
2926 | error("too few template parameter lists in declaration of %qD", decl); |
2927 | else |
2928 | error("explicit specialization of %qD must be introduced by " |
2929 | "%<template <>%>", decl); |
2930 | |
2931 | /* Fall through. */ |
2932 | case tsk_expl_spec: |
2933 | if (is_concept) |
2934 | error ("explicit specialization declared %<concept%>"); |
2935 | |
2936 | if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR) |
2937 | /* In cases like template<> constexpr bool v = true; |
2938 | We'll give an error in check_template_variable. */ |
2939 | break; |
2940 | |
2941 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
2942 | if (ctype) |
2943 | member_specialization = 1; |
2944 | else |
2945 | specialization = 1; |
2946 | break; |
2947 | |
2948 | case tsk_template: |
2949 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
2950 | { |
2951 | /* This case handles bogus declarations like template <> |
2952 | template <class T> void f<int>(); */ |
2953 | |
2954 | if (!uses_template_parms (TREE_OPERAND (declarator, 1))) |
2955 | error_at (cp_expr_loc_or_input_loc (t: declarator), |
2956 | "template-id %qE in declaration of primary template", |
2957 | declarator); |
2958 | else if (variable_template_p (TREE_OPERAND (declarator, 0))) |
2959 | { |
2960 | /* Partial specialization of variable template. */ |
2961 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); |
2962 | specialization = 1; |
2963 | goto ok; |
2964 | } |
2965 | else if (cxx_dialect < cxx14) |
2966 | error_at (cp_expr_loc_or_input_loc (t: declarator), |
2967 | "non-type partial specialization %qE " |
2968 | "is not allowed", declarator); |
2969 | else |
2970 | error_at (cp_expr_loc_or_input_loc (t: declarator), |
2971 | "non-class, non-variable partial specialization %qE " |
2972 | "is not allowed", declarator); |
2973 | return decl; |
2974 | ok:; |
2975 | } |
2976 | |
2977 | if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) |
2978 | /* This is a specialization of a member template, without |
2979 | specialization the containing class. Something like: |
2980 | |
2981 | template <class T> struct S { |
2982 | template <class U> void f (U); |
2983 | }; |
2984 | template <> template <class U> void S<int>::f(U) {} |
2985 | |
2986 | That's a specialization -- but of the entire template. */ |
2987 | specialization = 1; |
2988 | break; |
2989 | |
2990 | default: |
2991 | gcc_unreachable (); |
2992 | } |
2993 | |
2994 | if ((specialization || member_specialization) |
2995 | /* This doesn't apply to variable templates. */ |
2996 | && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl))) |
2997 | { |
2998 | tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
2999 | for (; t; t = TREE_CHAIN (t)) |
3000 | if (TREE_PURPOSE (t)) |
3001 | { |
3002 | permerror (input_location, |
3003 | "default argument specified in explicit specialization"); |
3004 | break; |
3005 | } |
3006 | } |
3007 | |
3008 | if (specialization || member_specialization || explicit_instantiation) |
3009 | { |
3010 | tree tmpl = NULL_TREE; |
3011 | tree targs = NULL_TREE; |
3012 | bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR); |
3013 | bool found_hidden = false; |
3014 | |
3015 | /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */ |
3016 | if (!was_template_id) |
3017 | { |
3018 | tree fns; |
3019 | |
3020 | gcc_assert (identifier_p (declarator)); |
3021 | if (ctype) |
3022 | fns = dname; |
3023 | else |
3024 | { |
3025 | /* If there is no class context, the explicit instantiation |
3026 | must be at namespace scope. */ |
3027 | gcc_assert (DECL_NAMESPACE_SCOPE_P (decl)); |
3028 | |
3029 | /* Find the namespace binding, using the declaration |
3030 | context. */ |
3031 | fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), name: dname, |
3032 | LOOK_want::NORMAL, true); |
3033 | if (fns == error_mark_node) |
3034 | { |
3035 | /* If lookup fails, look for a friend declaration so we can |
3036 | give a better diagnostic. */ |
3037 | fns = (lookup_qualified_name |
3038 | (CP_DECL_CONTEXT (decl), name: dname, |
3039 | LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND, |
3040 | /*complain*/true)); |
3041 | found_hidden = true; |
3042 | } |
3043 | |
3044 | if (fns == error_mark_node || !is_overloaded_fn (fns)) |
3045 | { |
3046 | error ("%qD is not a template function", dname); |
3047 | fns = error_mark_node; |
3048 | } |
3049 | } |
3050 | |
3051 | declarator = lookup_template_function (fns, NULL_TREE); |
3052 | } |
3053 | |
3054 | if (declarator == error_mark_node) |
3055 | return error_mark_node; |
3056 | |
3057 | if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) |
3058 | { |
3059 | if (!explicit_instantiation) |
3060 | /* A specialization in class scope. This is invalid, |
3061 | but the error will already have been flagged by |
3062 | check_specialization_scope. */ |
3063 | return error_mark_node; |
3064 | else |
3065 | { |
3066 | /* It's not valid to write an explicit instantiation in |
3067 | class scope, e.g.: |
3068 | |
3069 | class C { template void f(); } |
3070 | |
3071 | This case is caught by the parser. However, on |
3072 | something like: |
3073 | |
3074 | template class C { void f(); }; |
3075 | |
3076 | (which is invalid) we can get here. The error will be |
3077 | issued later. */ |
3078 | ; |
3079 | } |
3080 | |
3081 | return decl; |
3082 | } |
3083 | else if (ctype != NULL_TREE |
3084 | && (identifier_p (TREE_OPERAND (declarator, 0)))) |
3085 | { |
3086 | // We'll match variable templates in start_decl. |
3087 | if (VAR_P (decl)) |
3088 | return decl; |
3089 | |
3090 | /* Find the list of functions in ctype that have the same |
3091 | name as the declared function. */ |
3092 | tree name = TREE_OPERAND (declarator, 0); |
3093 | |
3094 | if (constructor_name_p (name, ctype)) |
3095 | { |
3096 | if (DECL_CONSTRUCTOR_P (decl) |
3097 | ? !TYPE_HAS_USER_CONSTRUCTOR (ctype) |
3098 | : !CLASSTYPE_DESTRUCTOR (ctype)) |
3099 | { |
3100 | /* From [temp.expl.spec]: |
3101 | |
3102 | If such an explicit specialization for the member |
3103 | of a class template names an implicitly-declared |
3104 | special member function (clause _special_), the |
3105 | program is ill-formed. |
3106 | |
3107 | Similar language is found in [temp.explicit]. */ |
3108 | error ("specialization of implicitly-declared special member function"); |
3109 | return error_mark_node; |
3110 | } |
3111 | |
3112 | name = DECL_NAME (decl); |
3113 | } |
3114 | |
3115 | /* For a type-conversion operator, We might be looking for |
3116 | `operator int' which will be a specialization of |
3117 | `operator T'. Grab all the conversion operators, and |
3118 | then select from them. */ |
3119 | tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name) |
3120 | ? conv_op_identifier : name); |
3121 | |
3122 | if (fns == NULL_TREE) |
3123 | { |
3124 | error ("no member function %qD declared in %qT", name, ctype); |
3125 | return error_mark_node; |
3126 | } |
3127 | else |
3128 | TREE_OPERAND (declarator, 0) = fns; |
3129 | } |
3130 | |
3131 | /* Figure out what exactly is being specialized at this point. |
3132 | Note that for an explicit instantiation, even one for a |
3133 | member function, we cannot tell a priori whether the |
3134 | instantiation is for a member template, or just a member |
3135 | function of a template class. Even if a member template is |
3136 | being instantiated, the member template arguments may be |
3137 | elided if they can be deduced from the rest of the |
3138 | declaration. */ |
3139 | tmpl = determine_specialization (template_id: declarator, decl, |
3140 | targs_out: &targs, |
3141 | need_member_template: member_specialization, |
3142 | template_count, |
3143 | tsk); |
3144 | |
3145 | if (!tmpl || tmpl == error_mark_node) |
3146 | /* We couldn't figure out what this declaration was |
3147 | specializing. */ |
3148 | return error_mark_node; |
3149 | else |
3150 | { |
3151 | if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL) |
3152 | { |
3153 | auto_diagnostic_group d; |
3154 | if (pedwarn (DECL_SOURCE_LOCATION (decl), 0, |
3155 | "friend declaration %qD is not visible to " |
3156 | "explicit specialization", tmpl)) |
3157 | inform (DECL_SOURCE_LOCATION (tmpl), |
3158 | "friend declaration here"); |
3159 | } |
3160 | |
3161 | if (!ctype && !is_friend |
3162 | && CP_DECL_CONTEXT (decl) == current_namespace) |
3163 | check_unqualified_spec_or_inst (t: tmpl, DECL_SOURCE_LOCATION (decl)); |
3164 | |
3165 | tree gen_tmpl = most_general_template (tmpl); |
3166 | |
3167 | if (explicit_instantiation) |
3168 | { |
3169 | /* We don't set DECL_EXPLICIT_INSTANTIATION here; that |
3170 | is done by do_decl_instantiation later. */ |
3171 | |
3172 | int arg_depth = TMPL_ARGS_DEPTH (targs); |
3173 | int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
3174 | |
3175 | if (arg_depth > parm_depth) |
3176 | { |
3177 | /* If TMPL is not the most general template (for |
3178 | example, if TMPL is a friend template that is |
3179 | injected into namespace scope), then there will |
3180 | be too many levels of TARGS. Remove some of them |
3181 | here. */ |
3182 | int i; |
3183 | tree new_targs; |
3184 | |
3185 | new_targs = make_tree_vec (parm_depth); |
3186 | for (i = arg_depth - parm_depth; i < arg_depth; ++i) |
3187 | TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth)) |
3188 | = TREE_VEC_ELT (targs, i); |
3189 | targs = new_targs; |
3190 | } |
3191 | |
3192 | return instantiate_template (tmpl, targs, tf_error); |
3193 | } |
3194 | |
3195 | /* If we thought that the DECL was a member function, but it |
3196 | turns out to be specializing a static member function, |
3197 | make DECL a static member function as well. */ |
3198 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
3199 | && DECL_STATIC_FUNCTION_P (tmpl) |
3200 | && DECL_IOBJ_MEMBER_FUNCTION_P (decl)) |
3201 | revert_static_member_fn (decl); |
3202 | |
3203 | /* If this is a specialization of a member template of a |
3204 | template class, we want to return the TEMPLATE_DECL, not |
3205 | the specialization of it. */ |
3206 | if (tsk == tsk_template && !was_template_id) |
3207 | { |
3208 | tree result = DECL_TEMPLATE_RESULT (tmpl); |
3209 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
3210 | DECL_INITIAL (result) = NULL_TREE; |
3211 | if (have_def) |
3212 | { |
3213 | tree parm; |
3214 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
3215 | DECL_SOURCE_LOCATION (result) |
3216 | = DECL_SOURCE_LOCATION (decl); |
3217 | /* We want to use the argument list specified in the |
3218 | definition, not in the original declaration. */ |
3219 | DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl); |
3220 | for (parm = DECL_ARGUMENTS (result); parm; |
3221 | parm = DECL_CHAIN (parm)) |
3222 | DECL_CONTEXT (parm) = result; |
3223 | } |
3224 | decl = register_specialization (spec: tmpl, tmpl: gen_tmpl, args: targs, |
3225 | is_friend, hash: 0); |
3226 | remove_contract_attributes (result); |
3227 | return decl; |
3228 | } |
3229 | |
3230 | /* Set up the DECL_TEMPLATE_INFO for DECL. */ |
3231 | DECL_TEMPLATE_INFO (decl) = build_template_info (template_decl: tmpl, template_args: targs); |
3232 | |
3233 | if (was_template_id) |
3234 | TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true; |
3235 | |
3236 | /* Inherit default function arguments from the template |
3237 | DECL is specializing. */ |
3238 | if (DECL_FUNCTION_TEMPLATE_P (tmpl)) |
3239 | copy_default_args_to_explicit_spec (decl); |
3240 | |
3241 | /* This specialization has the same protection as the |
3242 | template it specializes. */ |
3243 | TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl); |
3244 | TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl); |
3245 | |
3246 | /* 7.1.1-1 [dcl.stc] |
3247 | |
3248 | A storage-class-specifier shall not be specified in an |
3249 | explicit specialization... |
3250 | |
3251 | The parser rejects these, so unless action is taken here, |
3252 | explicit function specializations will always appear with |
3253 | global linkage. |
3254 | |
3255 | The action recommended by the C++ CWG in response to C++ |
3256 | defect report 605 is to make the storage class and linkage |
3257 | of the explicit specialization match the templated function: |
3258 | |
3259 | http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605 |
3260 | */ |
3261 | if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl)) |
3262 | { |
3263 | tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl); |
3264 | gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL); |
3265 | |
3266 | /* This specialization has the same linkage and visibility as |
3267 | the function template it specializes. */ |
3268 | TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func); |
3269 | if (! TREE_PUBLIC (decl)) |
3270 | { |
3271 | DECL_INTERFACE_KNOWN (decl) = 1; |
3272 | DECL_NOT_REALLY_EXTERN (decl) = 1; |
3273 | } |
3274 | DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func); |
3275 | if (DECL_VISIBILITY_SPECIFIED (tmpl_func)) |
3276 | { |
3277 | DECL_VISIBILITY_SPECIFIED (decl) = 1; |
3278 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func); |
3279 | } |
3280 | } |
3281 | |
3282 | /* If DECL is a friend declaration, declared using an |
3283 | unqualified name, the namespace associated with DECL may |
3284 | have been set incorrectly. For example, in: |
3285 | |
3286 | template <typename T> void f(T); |
3287 | namespace N { |
3288 | struct S { friend void f<int>(int); } |
3289 | } |
3290 | |
3291 | we will have set the DECL_CONTEXT for the friend |
3292 | declaration to N, rather than to the global namespace. */ |
3293 | if (DECL_NAMESPACE_SCOPE_P (decl)) |
3294 | DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl); |
3295 | |
3296 | if (is_friend && !have_def) |
3297 | /* This is not really a declaration of a specialization. |
3298 | It's just the name of an instantiation. But, it's not |
3299 | a request for an instantiation, either. */ |
3300 | SET_DECL_IMPLICIT_INSTANTIATION (decl); |
3301 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
3302 | /* A specialization is not necessarily COMDAT. */ |
3303 | DECL_COMDAT (decl) = (TREE_PUBLIC (decl) |
3304 | && DECL_DECLARED_INLINE_P (decl)); |
3305 | else if (VAR_P (decl)) |
3306 | DECL_COMDAT (decl) = false; |
3307 | |
3308 | /* If this is a full specialization, register it so that we can find |
3309 | it again. Partial specializations will be registered in |
3310 | process_partial_specialization. */ |
3311 | if (!processing_template_decl) |
3312 | { |
3313 | warn_spec_missing_attributes (tmpl: gen_tmpl, spec: decl, attrlist); |
3314 | |
3315 | decl = register_specialization (spec: decl, tmpl: gen_tmpl, args: targs, |
3316 | is_friend, hash: 0); |
3317 | } |
3318 | |
3319 | /* If this is a specialization, splice any contracts that may have |
3320 | been inherited from the template, removing them. */ |
3321 | if (decl != error_mark_node && DECL_TEMPLATE_SPECIALIZATION (decl)) |
3322 | remove_contract_attributes (decl); |
3323 | |
3324 | /* A 'structor should already have clones. */ |
3325 | gcc_assert (decl == error_mark_node |
3326 | || variable_template_p (tmpl) |
3327 | || !(DECL_CONSTRUCTOR_P (decl) |
3328 | || DECL_DESTRUCTOR_P (decl)) |
3329 | || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl))); |
3330 | } |
3331 | } |
3332 | |
3333 | return decl; |
3334 | } |
3335 | |
3336 | /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template |
3337 | parameters. These are represented in the same format used for |
3338 | DECL_TEMPLATE_PARMS. */ |
3339 | |
3340 | int |
3341 | comp_template_parms (const_tree parms1, const_tree parms2) |
3342 | { |
3343 | if (parms1 == parms2) |
3344 | return 1; |
3345 | |
3346 | tree t1 = TREE_VALUE (parms1); |
3347 | tree t2 = TREE_VALUE (parms2); |
3348 | int i; |
3349 | |
3350 | gcc_assert (TREE_CODE (t1) == TREE_VEC); |
3351 | gcc_assert (TREE_CODE (t2) == TREE_VEC); |
3352 | |
3353 | if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2)) |
3354 | return 0; |
3355 | |
3356 | for (i = 0; i < TREE_VEC_LENGTH (t2); ++i) |
3357 | { |
3358 | tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i)); |
3359 | tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i)); |
3360 | |
3361 | /* If either of the template parameters are invalid, assume |
3362 | they match for the sake of error recovery. */ |
3363 | if (error_operand_p (t: parm1) || error_operand_p (t: parm2)) |
3364 | return 1; |
3365 | |
3366 | if (TREE_CODE (parm1) != TREE_CODE (parm2)) |
3367 | return 0; |
3368 | |
3369 | if (TREE_CODE (parm1) == TYPE_DECL |
3370 | && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm1)) |
3371 | == TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm2)))) |
3372 | continue; |
3373 | else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2))) |
3374 | return 0; |
3375 | } |
3376 | |
3377 | return 1; |
3378 | } |
3379 | |
3380 | /* Returns true if two template parameters are declared with |
3381 | equivalent constraints. */ |
3382 | |
3383 | static bool |
3384 | template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2) |
3385 | { |
3386 | tree req1 = TREE_TYPE (parm1); |
3387 | tree req2 = TREE_TYPE (parm2); |
3388 | if (!req1 != !req2) |
3389 | return false; |
3390 | if (req1) |
3391 | return cp_tree_equal (req1, req2); |
3392 | return true; |
3393 | } |
3394 | |
3395 | /* Returns true when two template parameters are equivalent. */ |
3396 | |
3397 | static bool |
3398 | template_parameters_equivalent_p (const_tree parm1, const_tree parm2) |
3399 | { |
3400 | tree decl1 = TREE_VALUE (parm1); |
3401 | tree decl2 = TREE_VALUE (parm2); |
3402 | |
3403 | /* If either of the template parameters are invalid, assume |
3404 | they match for the sake of error recovery. */ |
3405 | if (error_operand_p (t: decl1) || error_operand_p (t: decl2)) |
3406 | return true; |
3407 | |
3408 | /* ... they declare parameters of the same kind. */ |
3409 | if (TREE_CODE (decl1) != TREE_CODE (decl2)) |
3410 | return false; |
3411 | |
3412 | /* ... one parameter was introduced by a parameter declaration, then |
3413 | both are. This case arises as a result of eagerly rewriting declarations |
3414 | during parsing. */ |
3415 | if (DECL_IMPLICIT_TEMPLATE_PARM_P (decl1) |
3416 | != DECL_IMPLICIT_TEMPLATE_PARM_P (decl2)) |
3417 | return false; |
3418 | |
3419 | /* ... if either declares a pack, they both do. */ |
3420 | if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2)) |
3421 | return false; |
3422 | |
3423 | if (TREE_CODE (decl1) == PARM_DECL) |
3424 | { |
3425 | /* ... if they declare non-type parameters, the types are equivalent. */ |
3426 | if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2))) |
3427 | return false; |
3428 | } |
3429 | else if (TREE_CODE (decl2) == TEMPLATE_DECL) |
3430 | { |
3431 | /* ... if they declare template template parameters, their template |
3432 | parameter lists are equivalent. */ |
3433 | if (!template_heads_equivalent_p (decl1, decl2)) |
3434 | return false; |
3435 | } |
3436 | |
3437 | /* ... if they are declared with a qualified-concept name, they both |
3438 | are, and those names are equivalent. */ |
3439 | return template_parameter_constraints_equivalent_p (parm1, parm2); |
3440 | } |
3441 | |
3442 | /* Returns true if two template parameters lists are equivalent. |
3443 | Two template parameter lists are equivalent if they have the |
3444 | same length and their corresponding parameters are equivalent. |
3445 | |
3446 | PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the |
3447 | data structure returned by DECL_TEMPLATE_PARMS. |
3448 | |
3449 | This is generally the same implementation as comp_template_parms |
3450 | except that it also the concept names and arguments used to |
3451 | introduce parameters. */ |
3452 | |
3453 | static bool |
3454 | template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2) |
3455 | { |
3456 | if (parms1 == parms2) |
3457 | return true; |
3458 | |
3459 | tree list1 = TREE_VALUE (parms1); |
3460 | tree list2 = TREE_VALUE (parms2); |
3461 | |
3462 | if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2)) |
3463 | return 0; |
3464 | |
3465 | for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i) |
3466 | { |
3467 | tree parm1 = TREE_VEC_ELT (list1, i); |
3468 | tree parm2 = TREE_VEC_ELT (list2, i); |
3469 | if (!template_parameters_equivalent_p (parm1, parm2)) |
3470 | return false; |
3471 | } |
3472 | |
3473 | return true; |
3474 | } |
3475 | |
3476 | /* Return true if the requires-clause of the template parameter lists are |
3477 | equivalent and false otherwise. */ |
3478 | static bool |
3479 | template_requirements_equivalent_p (const_tree parms1, const_tree parms2) |
3480 | { |
3481 | tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1); |
3482 | tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2); |
3483 | if ((req1 != NULL_TREE) != (req2 != NULL_TREE)) |
3484 | return false; |
3485 | if (!cp_tree_equal (req1, req2)) |
3486 | return false; |
3487 | return true; |
3488 | } |
3489 | |
3490 | /* Returns true if two template heads are equivalent. 17.6.6.1p6: |
3491 | Two template heads are equivalent if their template parameter |
3492 | lists are equivalent and their requires clauses are equivalent. |
3493 | |
3494 | In pre-C++20, this is equivalent to calling comp_template_parms |
3495 | for the template parameters of TMPL1 and TMPL2. */ |
3496 | |
3497 | bool |
3498 | template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2) |
3499 | { |
3500 | tree parms1 = DECL_TEMPLATE_PARMS (tmpl1); |
3501 | tree parms2 = DECL_TEMPLATE_PARMS (tmpl2); |
3502 | |
3503 | /* ... have the same number of template parameters, and their |
3504 | corresponding parameters are equivalent. */ |
3505 | if (!template_parameter_lists_equivalent_p (parms1, parms2)) |
3506 | return false; |
3507 | |
3508 | /* ... if either has a requires-clause, they both do and their |
3509 | corresponding constraint-expressions are equivalent. */ |
3510 | return template_requirements_equivalent_p (parms1, parms2); |
3511 | } |
3512 | |
3513 | /* Determine whether PARM is a parameter pack. */ |
3514 | |
3515 | bool |
3516 | template_parameter_pack_p (const_tree parm) |
3517 | { |
3518 | /* Determine if we have a non-type template parameter pack. */ |
3519 | if (TREE_CODE (parm) == PARM_DECL) |
3520 | return (DECL_TEMPLATE_PARM_P (parm) |
3521 | && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))); |
3522 | if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX) |
3523 | return TEMPLATE_PARM_PARAMETER_PACK (parm); |
3524 | |
3525 | /* If this is a list of template parameters, we could get a |
3526 | TYPE_DECL or a TEMPLATE_DECL. */ |
3527 | if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL) |
3528 | parm = TREE_TYPE (parm); |
3529 | |
3530 | /* Otherwise it must be a type template parameter. */ |
3531 | return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
3532 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
3533 | && TEMPLATE_TYPE_PARAMETER_PACK (parm)); |
3534 | } |
3535 | |
3536 | /* Determine if T is a function parameter pack. */ |
3537 | |
3538 | bool |
3539 | function_parameter_pack_p (const_tree t) |
3540 | { |
3541 | if (t && TREE_CODE (t) == PARM_DECL) |
3542 | return DECL_PACK_P (t); |
3543 | return false; |
3544 | } |
3545 | |
3546 | /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST. |
3547 | PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */ |
3548 | |
3549 | tree |
3550 | get_function_template_decl (const_tree primary_func_tmpl_inst) |
3551 | { |
3552 | if (! primary_func_tmpl_inst |
3553 | || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL |
3554 | || ! primary_template_specialization_p (primary_func_tmpl_inst)) |
3555 | return NULL; |
3556 | |
3557 | return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst)); |
3558 | } |
3559 | |
3560 | /* Return true iff the function parameter PARAM_DECL was expanded |
3561 | from the function parameter pack PACK. */ |
3562 | |
3563 | bool |
3564 | function_parameter_expanded_from_pack_p (tree param_decl, tree pack) |
3565 | { |
3566 | if (DECL_ARTIFICIAL (param_decl) |
3567 | || !function_parameter_pack_p (t: pack)) |
3568 | return false; |
3569 | |
3570 | /* The parameter pack and its pack arguments have the same |
3571 | DECL_PARM_INDEX. */ |
3572 | return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl); |
3573 | } |
3574 | |
3575 | /* Determine whether ARGS describes a variadic template args list, |
3576 | i.e., one that is terminated by a template argument pack. */ |
3577 | |
3578 | static bool |
3579 | template_args_variadic_p (tree args) |
3580 | { |
3581 | int nargs; |
3582 | tree last_parm; |
3583 | |
3584 | if (args == NULL_TREE) |
3585 | return false; |
3586 | |
3587 | args = INNERMOST_TEMPLATE_ARGS (args); |
3588 | nargs = TREE_VEC_LENGTH (args); |
3589 | |
3590 | if (nargs == 0) |
3591 | return false; |
3592 | |
3593 | last_parm = TREE_VEC_ELT (args, nargs - 1); |
3594 | |
3595 | return ARGUMENT_PACK_P (last_parm); |
3596 | } |
3597 | |
3598 | /* Generate a new name for the parameter pack name NAME (an |
3599 | IDENTIFIER_NODE) that incorporates its */ |
3600 | |
3601 | static tree |
3602 | make_ith_pack_parameter_name (tree name, int i) |
3603 | { |
3604 | /* Munge the name to include the parameter index. */ |
3605 | #define NUMBUF_LEN 128 |
3606 | char numbuf[NUMBUF_LEN]; |
3607 | char* newname; |
3608 | int newname_len; |
3609 | |
3610 | if (name == NULL_TREE) |
3611 | return name; |
3612 | snprintf (s: numbuf, NUMBUF_LEN, format: "%i", i); |
3613 | newname_len = IDENTIFIER_LENGTH (name) |
3614 | + strlen (s: numbuf) + 2; |
3615 | newname = (char*)alloca (newname_len); |
3616 | snprintf (s: newname, maxlen: newname_len, |
3617 | format: "%s#%i", IDENTIFIER_POINTER (name), i); |
3618 | return get_identifier (newname); |
3619 | } |
3620 | |
3621 | /* Return true if T is a primary function, class or alias template |
3622 | specialization, not including the template pattern. */ |
3623 | |
3624 | bool |
3625 | primary_template_specialization_p (const_tree t) |
3626 | { |
3627 | if (!t) |
3628 | return false; |
3629 | |
3630 | if (VAR_OR_FUNCTION_DECL_P (t)) |
3631 | return (DECL_LANG_SPECIFIC (t) |
3632 | && DECL_USE_TEMPLATE (t) |
3633 | && DECL_TEMPLATE_INFO (t) |
3634 | && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))); |
3635 | else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t))) |
3636 | return (CLASSTYPE_TEMPLATE_INFO (t) |
3637 | && CLASSTYPE_USE_TEMPLATE (t) |
3638 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))); |
3639 | else if (alias_template_specialization_p (t, nt_transparent)) |
3640 | return true; |
3641 | return false; |
3642 | } |
3643 | |
3644 | /* Return true if PARM is a template template parameter. */ |
3645 | |
3646 | bool |
3647 | template_template_parameter_p (const_tree parm) |
3648 | { |
3649 | return DECL_TEMPLATE_TEMPLATE_PARM_P (parm); |
3650 | } |
3651 | |
3652 | /* Return true iff PARM is a DECL representing a type template |
3653 | parameter. */ |
3654 | |
3655 | bool |
3656 | template_type_parameter_p (const_tree parm) |
3657 | { |
3658 | return (parm |
3659 | && (TREE_CODE (parm) == TYPE_DECL |
3660 | || TREE_CODE (parm) == TEMPLATE_DECL) |
3661 | && DECL_TEMPLATE_PARM_P (parm)); |
3662 | } |
3663 | |
3664 | /* Return the template parameters of T if T is a |
3665 | primary template instantiation, NULL otherwise. */ |
3666 | |
3667 | tree |
3668 | get_primary_template_innermost_parameters (const_tree t) |
3669 | { |
3670 | tree parms = NULL, template_info = NULL; |
3671 | |
3672 | if ((template_info = get_template_info (t)) |
3673 | && primary_template_specialization_p (t)) |
3674 | parms = INNERMOST_TEMPLATE_PARMS |
3675 | (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info))); |
3676 | |
3677 | return parms; |
3678 | } |
3679 | |
3680 | /* Returns the template arguments of T if T is a template instantiation, |
3681 | NULL otherwise. */ |
3682 | |
3683 | tree |
3684 | get_template_innermost_arguments (const_tree t) |
3685 | { |
3686 | tree args = NULL, template_info = NULL; |
3687 | |
3688 | if ((template_info = get_template_info (t)) |
3689 | && TI_ARGS (template_info)) |
3690 | args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info)); |
3691 | |
3692 | return args; |
3693 | } |
3694 | |
3695 | /* Return the argument pack elements of T if T is a template argument pack, |
3696 | NULL otherwise. */ |
3697 | |
3698 | tree |
3699 | get_template_argument_pack_elems (const_tree t) |
3700 | { |
3701 | if (TREE_CODE (t) != TYPE_ARGUMENT_PACK |
3702 | && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK) |
3703 | return NULL; |
3704 | |
3705 | return ARGUMENT_PACK_ARGS (t); |
3706 | } |
3707 | |
3708 | /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the |
3709 | ARGUMENT_PACK_SELECT represents. */ |
3710 | |
3711 | static tree |
3712 | argument_pack_select_arg (tree t) |
3713 | { |
3714 | tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t)); |
3715 | tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t)); |
3716 | |
3717 | /* If the selected argument is an expansion E, that most likely means we were |
3718 | called from gen_elem_of_pack_expansion_instantiation during the |
3719 | substituting of an argument pack (of which the Ith element is a pack |
3720 | expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion. |
3721 | In this case, the Ith element resulting from this substituting is going to |
3722 | be a pack expansion, which pattern is the pattern of E. Let's return the |
3723 | pattern of E, and gen_elem_of_pack_expansion_instantiation will build the |
3724 | resulting pack expansion from it. */ |
3725 | if (PACK_EXPANSION_P (arg)) |
3726 | { |
3727 | /* Make sure we aren't throwing away arg info. */ |
3728 | gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg)); |
3729 | arg = PACK_EXPANSION_PATTERN (arg); |
3730 | } |
3731 | |
3732 | return arg; |
3733 | } |
3734 | |
3735 | /* Return a modification of ARGS that's suitable for preserving inside a hash |
3736 | table. In particular, this replaces each ARGUMENT_PACK_SELECT with its |
3737 | underlying argument. ARGS is copied (upon modification) iff COW_P. */ |
3738 | |
3739 | static tree |
3740 | preserve_args (tree args, bool cow_p = true) |
3741 | { |
3742 | if (!args) |
3743 | return NULL_TREE; |
3744 | |
3745 | for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i) |
3746 | { |
3747 | tree t = TREE_VEC_ELT (args, i); |
3748 | tree r; |
3749 | if (!t) |
3750 | r = NULL_TREE; |
3751 | else if (TREE_CODE (t) == ARGUMENT_PACK_SELECT) |
3752 | r = argument_pack_select_arg (t); |
3753 | else if (TREE_CODE (t) == TREE_VEC) |
3754 | r = preserve_args (args: t, cow_p); |
3755 | else |
3756 | r = t; |
3757 | if (r != t) |
3758 | { |
3759 | if (cow_p) |
3760 | { |
3761 | args = copy_template_args (args); |
3762 | cow_p = false; |
3763 | } |
3764 | TREE_VEC_ELT (args, i) = r; |
3765 | } |
3766 | } |
3767 | |
3768 | return args; |
3769 | } |
3770 | |
3771 | /* True iff FN is a function representing a built-in variadic parameter |
3772 | pack. */ |
3773 | |
3774 | bool |
3775 | builtin_pack_fn_p (tree fn) |
3776 | { |
3777 | if (!fn |
3778 | || TREE_CODE (fn) != FUNCTION_DECL |
3779 | || !DECL_IS_UNDECLARED_BUILTIN (fn)) |
3780 | return false; |
3781 | |
3782 | if (id_equal (DECL_NAME (fn), str: "__integer_pack")) |
3783 | return true; |
3784 | |
3785 | return false; |
3786 | } |
3787 | |
3788 | /* True iff CALL is a call to a function representing a built-in variadic |
3789 | parameter pack. */ |
3790 | |
3791 | static bool |
3792 | builtin_pack_call_p (tree call) |
3793 | { |
3794 | if (TREE_CODE (call) != CALL_EXPR) |
3795 | return false; |
3796 | return builtin_pack_fn_p (CALL_EXPR_FN (call)); |
3797 | } |
3798 | |
3799 | /* Return a TREE_VEC for the expansion of __integer_pack(HI). */ |
3800 | |
3801 | static tree |
3802 | expand_integer_pack (tree call, tree args, tsubst_flags_t complain, |
3803 | tree in_decl) |
3804 | { |
3805 | tree ohi = CALL_EXPR_ARG (call, 0); |
3806 | tree hi = tsubst_expr (ohi, args, complain, in_decl); |
3807 | |
3808 | if (instantiation_dependent_expression_p (hi)) |
3809 | { |
3810 | if (hi != ohi) |
3811 | { |
3812 | call = copy_node (call); |
3813 | CALL_EXPR_ARG (call, 0) = hi; |
3814 | } |
3815 | tree ex = make_pack_expansion (call, complain); |
3816 | tree vec = make_tree_vec (1); |
3817 | TREE_VEC_ELT (vec, 0) = ex; |
3818 | return vec; |
3819 | } |
3820 | else |
3821 | { |
3822 | hi = instantiate_non_dependent_expr (hi, complain); |
3823 | hi = cxx_constant_value (t: hi, complain); |
3824 | int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1; |
3825 | |
3826 | /* Calculate the largest value of len that won't make the size of the vec |
3827 | overflow an int. The compiler will exceed resource limits long before |
3828 | this, but it seems a decent place to diagnose. */ |
3829 | int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1; |
3830 | |
3831 | if (len < 0 || len > max) |
3832 | { |
3833 | if ((complain & tf_error) |
3834 | && hi != error_mark_node) |
3835 | error ("argument to %<__integer_pack%> must be between 0 and %d", |
3836 | max); |
3837 | return error_mark_node; |
3838 | } |
3839 | |
3840 | tree vec = make_tree_vec (len); |
3841 | |
3842 | for (int i = 0; i < len; ++i) |
3843 | TREE_VEC_ELT (vec, i) = size_int (i); |
3844 | |
3845 | return vec; |
3846 | } |
3847 | } |
3848 | |
3849 | /* Return a TREE_VEC for the expansion of built-in template parameter pack |
3850 | CALL. */ |
3851 | |
3852 | static tree |
3853 | expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain, |
3854 | tree in_decl) |
3855 | { |
3856 | if (!builtin_pack_call_p (call)) |
3857 | return NULL_TREE; |
3858 | |
3859 | tree fn = CALL_EXPR_FN (call); |
3860 | |
3861 | if (id_equal (DECL_NAME (fn), str: "__integer_pack")) |
3862 | return expand_integer_pack (call, args, complain, in_decl); |
3863 | |
3864 | return NULL_TREE; |
3865 | } |
3866 | |
3867 | /* Return true if the tree T has the extra args mechanism for |
3868 | avoiding partial instantiation. */ |
3869 | |
3870 | static bool |
3871 | has_extra_args_mechanism_p (const_tree t) |
3872 | { |
3873 | return (PACK_EXPANSION_P (t) /* PACK_EXPANSION_EXTRA_ARGS */ |
3874 | || TREE_CODE (t) == REQUIRES_EXPR /* REQUIRES_EXPR_EXTRA_ARGS */ |
3875 | || (TREE_CODE (t) == IF_STMT |
3876 | && IF_STMT_CONSTEXPR_P (t)) /* IF_STMT_EXTRA_ARGS */ |
3877 | || TREE_CODE (t) == LAMBDA_EXPR); /* LAMBDA_EXPR_EXTRA_ARGS */ |
3878 | } |
3879 | |
3880 | /* Return *_EXTRA_ARGS of the given supported tree T. */ |
3881 | |
3882 | static tree& |
3883 | tree_extra_args (tree t) |
3884 | { |
3885 | gcc_checking_assert (has_extra_args_mechanism_p (t)); |
3886 | |
3887 | if (PACK_EXPANSION_P (t)) |
3888 | return PACK_EXPANSION_EXTRA_ARGS (t); |
3889 | else if (TREE_CODE (t) == REQUIRES_EXPR) |
3890 | return REQUIRES_EXPR_EXTRA_ARGS (t); |
3891 | else if (TREE_CODE (t) == IF_STMT |
3892 | && IF_STMT_CONSTEXPR_P (t)) |
3893 | return IF_STMT_EXTRA_ARGS (t); |
3894 | else if (TREE_CODE (t) == LAMBDA_EXPR) |
3895 | return LAMBDA_EXPR_EXTRA_ARGS (t); |
3896 | |
3897 | gcc_unreachable (); |
3898 | } |
3899 | |
3900 | /* Structure used to track the progress of find_parameter_packs_r. */ |
3901 | struct find_parameter_pack_data |
3902 | { |
3903 | /* TREE_LIST that will contain all of the parameter packs found by |
3904 | the traversal. */ |
3905 | tree* parameter_packs; |
3906 | |
3907 | /* Set of AST nodes that have been visited by the traversal. */ |
3908 | hash_set<tree> *visited; |
3909 | |
3910 | /* True iff we found a subtree that has the extra args mechanism. */ |
3911 | bool found_extra_args_tree_p = false; |
3912 | }; |
3913 | |
3914 | /* Identifies all of the argument packs that occur in a template |
3915 | argument and appends them to the TREE_LIST inside DATA, which is a |
3916 | find_parameter_pack_data structure. This is a subroutine of |
3917 | make_pack_expansion and uses_parameter_packs. */ |
3918 | static tree |
3919 | find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data) |
3920 | { |
3921 | tree t = *tp; |
3922 | struct find_parameter_pack_data* ppd = |
3923 | (struct find_parameter_pack_data*)data; |
3924 | bool parameter_pack_p = false; |
3925 | |
3926 | #define WALK_SUBTREE(NODE) \ |
3927 | cp_walk_tree (&(NODE), &find_parameter_packs_r, \ |
3928 | ppd, ppd->visited) \ |
3929 | |
3930 | /* Don't look through typedefs; we are interested in whether a |
3931 | parameter pack is actually written in the expression/type we're |
3932 | looking at, not the target type. */ |
3933 | if (TYPE_P (t) && typedef_variant_p (type: t)) |
3934 | { |
3935 | /* But do look at arguments for an alias template. */ |
3936 | if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) |
3937 | cp_walk_tree (&TI_ARGS (tinfo), |
3938 | &find_parameter_packs_r, |
3939 | ppd, ppd->visited); |
3940 | *walk_subtrees = 0; |
3941 | return NULL_TREE; |
3942 | } |
3943 | |
3944 | /* Identify whether this is a parameter pack or not. */ |
3945 | switch (TREE_CODE (t)) |
3946 | { |
3947 | case TEMPLATE_PARM_INDEX: |
3948 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) |
3949 | parameter_pack_p = true; |
3950 | break; |
3951 | |
3952 | case TEMPLATE_TYPE_PARM: |
3953 | t = TYPE_MAIN_VARIANT (t); |
3954 | /* FALLTHRU */ |
3955 | case TEMPLATE_TEMPLATE_PARM: |
3956 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) |
3957 | parameter_pack_p = true; |
3958 | break; |
3959 | |
3960 | case FIELD_DECL: |
3961 | case PARM_DECL: |
3962 | if (DECL_PACK_P (t)) |
3963 | { |
3964 | /* We don't want to walk into the type of a PARM_DECL, |
3965 | because we don't want to see the type parameter pack. */ |
3966 | *walk_subtrees = 0; |
3967 | parameter_pack_p = true; |
3968 | } |
3969 | break; |
3970 | |
3971 | case VAR_DECL: |
3972 | if (DECL_PACK_P (t)) |
3973 | { |
3974 | /* We don't want to walk into the type of a variadic capture proxy, |
3975 | because we don't want to see the type parameter pack. */ |
3976 | *walk_subtrees = 0; |
3977 | parameter_pack_p = true; |
3978 | } |
3979 | else if (variable_template_specialization_p (t)) |
3980 | { |
3981 | cp_walk_tree (&DECL_TI_ARGS (t), |
3982 | find_parameter_packs_r, |
3983 | ppd, ppd->visited); |
3984 | *walk_subtrees = 0; |
3985 | } |
3986 | break; |
3987 | |
3988 | case CALL_EXPR: |
3989 | if (builtin_pack_call_p (call: t)) |
3990 | parameter_pack_p = true; |
3991 | break; |
3992 | |
3993 | case BASES: |
3994 | parameter_pack_p = true; |
3995 | break; |
3996 | default: |
3997 | /* Not a parameter pack. */ |
3998 | break; |
3999 | } |
4000 | |
4001 | if (parameter_pack_p) |
4002 | { |
4003 | /* Add this parameter pack to the list. */ |
4004 | *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs); |
4005 | } |
4006 | |
4007 | if (has_extra_args_mechanism_p (t) && !PACK_EXPANSION_P (t)) |
4008 | ppd->found_extra_args_tree_p = true; |
4009 | |
4010 | if (TYPE_P (t)) |
4011 | cp_walk_tree (&TYPE_CONTEXT (t), |
4012 | &find_parameter_packs_r, ppd, ppd->visited); |
4013 | |
4014 | /* This switch statement will return immediately if we don't find a |
4015 | parameter pack. ??? Should some of these be in cp_walk_subtrees? */ |
4016 | switch (TREE_CODE (t)) |
4017 | { |
4018 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
4019 | /* Check the template itself. */ |
4020 | cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)), |
4021 | &find_parameter_packs_r, ppd, ppd->visited); |
4022 | return NULL_TREE; |
4023 | |
4024 | case TEMPLATE_PARM_INDEX: |
4025 | if (parameter_pack_p) |
4026 | WALK_SUBTREE (TREE_TYPE (t)); |
4027 | return NULL_TREE; |
4028 | |
4029 | case DECL_EXPR: |
4030 | { |
4031 | tree decl = DECL_EXPR_DECL (t); |
4032 | /* Ignore the declaration of a capture proxy for a parameter pack. */ |
4033 | if (is_capture_proxy (decl)) |
4034 | *walk_subtrees = 0; |
4035 | if (is_typedef_decl (x: decl)) |
4036 | /* Since we stop at typedefs above, we need to look through them at |
4037 | the point of the DECL_EXPR. */ |
4038 | cp_walk_tree (&DECL_ORIGINAL_TYPE (decl), |
4039 | &find_parameter_packs_r, ppd, ppd->visited); |
4040 | return NULL_TREE; |
4041 | } |
4042 | |
4043 | case TEMPLATE_DECL: |
4044 | if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
4045 | return NULL_TREE; |
4046 | cp_walk_tree (&TREE_TYPE (t), |
4047 | &find_parameter_packs_r, ppd, ppd->visited); |
4048 | return NULL_TREE; |
4049 | |
4050 | case TYPE_PACK_EXPANSION: |
4051 | case EXPR_PACK_EXPANSION: |
4052 | *walk_subtrees = 0; |
4053 | return NULL_TREE; |
4054 | |
4055 | case PACK_INDEX_TYPE: |
4056 | case PACK_INDEX_EXPR: |
4057 | /* We can have an expansion of an expansion, such as "Ts...[Is]...", |
4058 | so do look into the index (but not the pack). */ |
4059 | cp_walk_tree (&PACK_INDEX_INDEX (t), &find_parameter_packs_r, ppd, |
4060 | ppd->visited); |
4061 | *walk_subtrees = 0; |
4062 | return NULL_TREE; |
4063 | |
4064 | case INTEGER_TYPE: |
4065 | cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r, |
4066 | ppd, ppd->visited); |
4067 | *walk_subtrees = 0; |
4068 | return NULL_TREE; |
4069 | |
4070 | case IDENTIFIER_NODE: |
4071 | cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd, |
4072 | ppd->visited); |
4073 | *walk_subtrees = 0; |
4074 | return NULL_TREE; |
4075 | |
4076 | case LAMBDA_EXPR: |
4077 | { |
4078 | /* Since we defer implicit capture, look in the parms and body. */ |
4079 | tree fn = lambda_function (t); |
4080 | cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd, |
4081 | ppd->visited); |
4082 | cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd, |
4083 | ppd->visited); |
4084 | return NULL_TREE; |
4085 | } |
4086 | |
4087 | case DECLTYPE_TYPE: |
4088 | cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r, |
4089 | ppd, ppd->visited); |
4090 | *walk_subtrees = 0; |
4091 | return NULL_TREE; |
4092 | |
4093 | case IF_STMT: |
4094 | cp_walk_tree (&IF_COND (t), &find_parameter_packs_r, |
4095 | ppd, ppd->visited); |
4096 | cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r, |
4097 | ppd, ppd->visited); |
4098 | cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r, |
4099 | ppd, ppd->visited); |
4100 | /* Don't walk into IF_STMT_EXTRA_ARGS. */ |
4101 | *walk_subtrees = 0; |
4102 | return NULL_TREE; |
4103 | |
4104 | case TAG_DEFN: |
4105 | t = TREE_TYPE (t); |
4106 | if (CLASS_TYPE_P (t)) |
4107 | { |
4108 | /* Local class, need to look through the whole definition. |
4109 | TYPE_BINFO might be unset for a partial instantiation. */ |
4110 | if (TYPE_BINFO (t)) |
4111 | for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t))) |
4112 | cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r, |
4113 | ppd, ppd->visited); |
4114 | } |
4115 | else |
4116 | /* Enum, look at the values. */ |
4117 | for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l)) |
4118 | cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)), |
4119 | &find_parameter_packs_r, |
4120 | ppd, ppd->visited); |
4121 | return NULL_TREE; |
4122 | |
4123 | case FUNCTION_TYPE: |
4124 | case METHOD_TYPE: |
4125 | WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t)); |
4126 | break; |
4127 | |
4128 | default: |
4129 | return NULL_TREE; |
4130 | } |
4131 | |
4132 | #undef WALK_SUBTREE |
4133 | |
4134 | return NULL_TREE; |
4135 | } |
4136 | |
4137 | /* Determines if the expression or type T uses any parameter packs. */ |
4138 | tree |
4139 | uses_parameter_packs (tree t) |
4140 | { |
4141 | tree parameter_packs = NULL_TREE; |
4142 | struct find_parameter_pack_data ppd; |
4143 | ppd.parameter_packs = ¶meter_packs; |
4144 | ppd.visited = new hash_set<tree>; |
4145 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
4146 | delete ppd.visited; |
4147 | return parameter_packs; |
4148 | } |
4149 | |
4150 | /* Turn ARG, which may be an expression, type, or a TREE_LIST |
4151 | representation a base-class initializer into a parameter pack |
4152 | expansion. If all goes well, the resulting node will be an |
4153 | EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST, |
4154 | respectively. */ |
4155 | tree |
4156 | make_pack_expansion (tree arg, tsubst_flags_t complain) |
4157 | { |
4158 | tree result; |
4159 | tree parameter_packs = NULL_TREE; |
4160 | bool for_types = false; |
4161 | struct find_parameter_pack_data ppd; |
4162 | |
4163 | if (!arg || arg == error_mark_node) |
4164 | return arg; |
4165 | |
4166 | if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg)) |
4167 | { |
4168 | /* A TREE_LIST with a non-null TREE_PURPOSE is for a base |
4169 | class initializer. In this case, the TREE_PURPOSE will be a |
4170 | _TYPE node (representing the base class expansion we're |
4171 | initializing) and the TREE_VALUE will be a TREE_LIST |
4172 | containing the initialization arguments. |
4173 | |
4174 | The resulting expansion looks somewhat different from most |
4175 | expansions. Rather than returning just one _EXPANSION, we |
4176 | return a TREE_LIST whose TREE_PURPOSE is a |
4177 | TYPE_PACK_EXPANSION containing the bases that will be |
4178 | initialized. The TREE_VALUE will be identical to the |
4179 | original TREE_VALUE, which is a list of arguments that will |
4180 | be passed to each base. We do not introduce any new pack |
4181 | expansion nodes into the TREE_VALUE (although it is possible |
4182 | that some already exist), because the TREE_PURPOSE and |
4183 | TREE_VALUE all need to be expanded together with the same |
4184 | _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the |
4185 | resulting TREE_PURPOSE will mention the parameter packs in |
4186 | both the bases and the arguments to the bases. */ |
4187 | tree purpose; |
4188 | tree value; |
4189 | tree parameter_packs = NULL_TREE; |
4190 | |
4191 | /* Determine which parameter packs will be used by the base |
4192 | class expansion. */ |
4193 | ppd.visited = new hash_set<tree>; |
4194 | ppd.parameter_packs = ¶meter_packs; |
4195 | gcc_assert (TYPE_P (TREE_PURPOSE (arg))); |
4196 | cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r, |
4197 | &ppd, ppd.visited); |
4198 | |
4199 | if (parameter_packs == NULL_TREE) |
4200 | { |
4201 | if (complain & tf_error) |
4202 | error ("base initializer expansion %qT contains no parameter packs", |
4203 | arg); |
4204 | delete ppd.visited; |
4205 | return error_mark_node; |
4206 | } |
4207 | |
4208 | if (TREE_VALUE (arg) != void_type_node) |
4209 | { |
4210 | /* Collect the sets of parameter packs used in each of the |
4211 | initialization arguments. */ |
4212 | for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value)) |
4213 | { |
4214 | /* Determine which parameter packs will be expanded in this |
4215 | argument. */ |
4216 | cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r, |
4217 | &ppd, ppd.visited); |
4218 | } |
4219 | } |
4220 | |
4221 | delete ppd.visited; |
4222 | |
4223 | /* Create the pack expansion type for the base type. */ |
4224 | purpose = cxx_make_type (TYPE_PACK_EXPANSION); |
4225 | PACK_EXPANSION_PATTERN (purpose) = TREE_PURPOSE (arg); |
4226 | PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs; |
4227 | PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p (); |
4228 | |
4229 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; |
4230 | they will rarely be compared to anything. */ |
4231 | SET_TYPE_STRUCTURAL_EQUALITY (purpose); |
4232 | |
4233 | return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE); |
4234 | } |
4235 | |
4236 | if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL) |
4237 | for_types = true; |
4238 | |
4239 | /* Build the PACK_EXPANSION_* node. */ |
4240 | result = for_types |
4241 | ? cxx_make_type (TYPE_PACK_EXPANSION) |
4242 | : make_node (EXPR_PACK_EXPANSION); |
4243 | PACK_EXPANSION_PATTERN (result) = arg; |
4244 | if (TREE_CODE (result) == EXPR_PACK_EXPANSION) |
4245 | { |
4246 | /* Propagate type and const-expression information. */ |
4247 | TREE_TYPE (result) = TREE_TYPE (arg); |
4248 | TREE_CONSTANT (result) = TREE_CONSTANT (arg); |
4249 | /* Mark this read now, since the expansion might be length 0. */ |
4250 | mark_exp_read (arg); |
4251 | } |
4252 | else |
4253 | /* Just use structural equality for these TYPE_PACK_EXPANSIONS; |
4254 | they will rarely be compared to anything. */ |
4255 | SET_TYPE_STRUCTURAL_EQUALITY (result); |
4256 | |
4257 | /* Determine which parameter packs will be expanded. */ |
4258 | ppd.parameter_packs = ¶meter_packs; |
4259 | ppd.visited = new hash_set<tree>; |
4260 | cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited); |
4261 | delete ppd.visited; |
4262 | |
4263 | /* Make sure we found some parameter packs. */ |
4264 | if (parameter_packs == NULL_TREE) |
4265 | { |
4266 | if (complain & tf_error) |
4267 | { |
4268 | if (TYPE_P (arg)) |
4269 | error ("expansion pattern %qT contains no parameter packs", arg); |
4270 | else |
4271 | error ("expansion pattern %qE contains no parameter packs", arg); |
4272 | } |
4273 | return error_mark_node; |
4274 | } |
4275 | PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs; |
4276 | |
4277 | PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p (); |
4278 | if (ppd.found_extra_args_tree_p) |
4279 | /* If the pattern of this pack expansion contains a subtree that has |
4280 | the extra args mechanism for avoiding partial instantiation, then |
4281 | force this pack expansion to also use extra args. Otherwise |
4282 | partial instantiation of this pack expansion may not lower the |
4283 | level of some parameter packs within the pattern, which would |
4284 | confuse tsubst_pack_expansion later (PR101764). */ |
4285 | PACK_EXPANSION_FORCE_EXTRA_ARGS_P (result) = true; |
4286 | |
4287 | return result; |
4288 | } |
4289 | |
4290 | /* Create a PACK_INDEX_* using the pack expansion PACK and index INDEX. */ |
4291 | |
4292 | tree |
4293 | make_pack_index (tree pack, tree index) |
4294 | { |
4295 | if (pack == error_mark_node) |
4296 | return error_mark_node; |
4297 | |
4298 | bool for_types; |
4299 | if (TREE_CODE (pack) == TYPE_PACK_EXPANSION) |
4300 | for_types = true; |
4301 | else if (TREE_CODE (pack) == EXPR_PACK_EXPANSION) |
4302 | for_types = false; |
4303 | else |
4304 | { |
4305 | /* Maybe we've already partially substituted the pack. */ |
4306 | gcc_checking_assert (TREE_CODE (pack) == TREE_VEC); |
4307 | for_types = TYPE_P (TREE_VEC_ELT (pack, 0)); |
4308 | } |
4309 | |
4310 | tree t = (for_types |
4311 | ? cxx_make_type (PACK_INDEX_TYPE) |
4312 | : make_node (PACK_INDEX_EXPR)); |
4313 | PACK_INDEX_PACK (t) = pack; |
4314 | PACK_INDEX_INDEX (t) = index; |
4315 | if (TREE_CODE (t) == PACK_INDEX_TYPE) |
4316 | SET_TYPE_STRUCTURAL_EQUALITY (t); |
4317 | return t; |
4318 | } |
4319 | |
4320 | /* Checks T for any "bare" parameter packs, which have not yet been |
4321 | expanded, and issues an error if any are found. This operation can |
4322 | only be done on full expressions or types (e.g., an expression |
4323 | statement, "if" condition, etc.), because we could have expressions like: |
4324 | |
4325 | foo(f(g(h(args)))...) |
4326 | |
4327 | where "args" is a parameter pack. check_for_bare_parameter_packs |
4328 | should not be called for the subexpressions args, h(args), |
4329 | g(h(args)), or f(g(h(args))), because we would produce erroneous |
4330 | error messages. |
4331 | |
4332 | Returns TRUE and emits an error if there were bare parameter packs, |
4333 | returns FALSE otherwise. */ |
4334 | bool |
4335 | check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */) |
4336 | { |
4337 | tree parameter_packs = NULL_TREE; |
4338 | struct find_parameter_pack_data ppd; |
4339 | |
4340 | if (!processing_template_decl || !t || t == error_mark_node) |
4341 | return false; |
4342 | |
4343 | if (TREE_CODE (t) == TYPE_DECL) |
4344 | t = TREE_TYPE (t); |
4345 | |
4346 | ppd.parameter_packs = ¶meter_packs; |
4347 | ppd.visited = new hash_set<tree>; |
4348 | cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited); |
4349 | delete ppd.visited; |
4350 | |
4351 | if (!parameter_packs) |
4352 | return false; |
4353 | |
4354 | if (loc == UNKNOWN_LOCATION) |
4355 | loc = cp_expr_loc_or_input_loc (t); |
4356 | |
4357 | /* It's OK for a lambda to have an unexpanded parameter pack from the |
4358 | containing context, but do complain about unexpanded capture packs. */ |
4359 | tree lam = current_lambda_expr (); |
4360 | if (lam) |
4361 | lam = TREE_TYPE (lam); |
4362 | |
4363 | if (lam && lam != current_class_type) |
4364 | { |
4365 | /* We're in a lambda, but it isn't the innermost class. |
4366 | This should work, but currently doesn't. */ |
4367 | sorry_at (loc, "unexpanded parameter pack in local class in lambda"); |
4368 | return true; |
4369 | } |
4370 | |
4371 | if (lam && CLASSTYPE_TEMPLATE_INFO (lam)) |
4372 | for (; parameter_packs; |
4373 | parameter_packs = TREE_CHAIN (parameter_packs)) |
4374 | { |
4375 | tree pack = TREE_VALUE (parameter_packs); |
4376 | if (is_capture_proxy (pack) |
4377 | || (TREE_CODE (pack) == PARM_DECL |
4378 | && DECL_CONTEXT (pack) |
4379 | && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam)) |
4380 | break; |
4381 | } |
4382 | |
4383 | if (parameter_packs) |
4384 | { |
4385 | auto_diagnostic_group d; |
4386 | error_at (loc, "parameter packs not expanded with %<...%>:"); |
4387 | while (parameter_packs) |
4388 | { |
4389 | tree pack = TREE_VALUE (parameter_packs); |
4390 | tree name = NULL_TREE; |
4391 | |
4392 | if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM |
4393 | || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM) |
4394 | name = TYPE_NAME (pack); |
4395 | else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX) |
4396 | name = DECL_NAME (TEMPLATE_PARM_DECL (pack)); |
4397 | else if (TREE_CODE (pack) == CALL_EXPR) |
4398 | name = DECL_NAME (CALL_EXPR_FN (pack)); |
4399 | else |
4400 | name = DECL_NAME (pack); |
4401 | |
4402 | if (name) |
4403 | inform (loc, " %qD", name); |
4404 | else |
4405 | inform (loc, " %s", "<anonymous>"); |
4406 | |
4407 | parameter_packs = TREE_CHAIN (parameter_packs); |
4408 | } |
4409 | |
4410 | return true; |
4411 | } |
4412 | |
4413 | return false; |
4414 | } |
4415 | |
4416 | /* Expand any parameter packs that occur in the template arguments in |
4417 | ARGS. */ |
4418 | tree |
4419 | expand_template_argument_pack (tree args) |
4420 | { |
4421 | if (args == error_mark_node) |
4422 | return error_mark_node; |
4423 | |
4424 | tree result_args = NULL_TREE; |
4425 | int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0; |
4426 | int num_result_args = -1; |
4427 | int non_default_args_count = -1; |
4428 | |
4429 | /* First, determine if we need to expand anything, and the number of |
4430 | slots we'll need. */ |
4431 | for (in_arg = 0; in_arg < nargs; ++in_arg) |
4432 | { |
4433 | tree arg = TREE_VEC_ELT (args, in_arg); |
4434 | if (arg == NULL_TREE) |
4435 | return args; |
4436 | if (ARGUMENT_PACK_P (arg)) |
4437 | { |
4438 | int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)); |
4439 | if (num_result_args < 0) |
4440 | num_result_args = in_arg + num_packed; |
4441 | else |
4442 | num_result_args += num_packed; |
4443 | } |
4444 | else |
4445 | { |
4446 | if (num_result_args >= 0) |
4447 | num_result_args++; |
4448 | } |
4449 | } |
4450 | |
4451 | /* If no expansion is necessary, we're done. */ |
4452 | if (num_result_args < 0) |
4453 | return args; |
4454 | |
4455 | /* Expand arguments. */ |
4456 | result_args = make_tree_vec (num_result_args); |
4457 | if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args)) |
4458 | non_default_args_count = |
4459 | GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args); |
4460 | for (in_arg = 0; in_arg < nargs; ++in_arg) |
4461 | { |
4462 | tree arg = TREE_VEC_ELT (args, in_arg); |
4463 | if (ARGUMENT_PACK_P (arg)) |
4464 | { |
4465 | tree packed = ARGUMENT_PACK_ARGS (arg); |
4466 | int i, num_packed = TREE_VEC_LENGTH (packed); |
4467 | for (i = 0; i < num_packed; ++i, ++out_arg) |
4468 | TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i); |
4469 | if (non_default_args_count > 0) |
4470 | non_default_args_count += num_packed - 1; |
4471 | } |
4472 | else |
4473 | { |
4474 | TREE_VEC_ELT (result_args, out_arg) = arg; |
4475 | ++out_arg; |
4476 | } |
4477 | } |
4478 | if (non_default_args_count >= 0) |
4479 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count); |
4480 | return result_args; |
4481 | } |
4482 | |
4483 | /* Checks if DECL shadows a template parameter. |
4484 | |
4485 | [temp.local]: A template-parameter shall not be redeclared within its |
4486 | scope (including nested scopes). |
4487 | |
4488 | Emits an error and returns TRUE if the DECL shadows a parameter, |
4489 | returns FALSE otherwise. */ |
4490 | |
4491 | bool |
4492 | check_template_shadow (tree decl) |
4493 | { |
4494 | tree olddecl; |
4495 | |
4496 | /* If we're not in a template, we can't possibly shadow a template |
4497 | parameter. */ |
4498 | if (!current_template_parms) |
4499 | return true; |
4500 | |
4501 | /* Figure out what we're shadowing. */ |
4502 | decl = OVL_FIRST (decl); |
4503 | olddecl = innermost_non_namespace_value (DECL_NAME (decl)); |
4504 | |
4505 | /* If there's no previous binding for this name, we're not shadowing |
4506 | anything, let alone a template parameter. */ |
4507 | if (!olddecl) |
4508 | return true; |
4509 | |
4510 | /* If we're not shadowing a template parameter, we're done. Note |
4511 | that OLDDECL might be an OVERLOAD (or perhaps even an |
4512 | ERROR_MARK), so we can't just blithely assume it to be a _DECL |
4513 | node. */ |
4514 | if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl)) |
4515 | return true; |
4516 | |
4517 | /* We check for decl != olddecl to avoid bogus errors for using a |
4518 | name inside a class. We check TPFI to avoid duplicate errors for |
4519 | inline member templates. */ |
4520 | if (decl == olddecl |
4521 | || (DECL_TEMPLATE_PARM_P (decl) |
4522 | && TEMPLATE_PARMS_FOR_INLINE (current_template_parms))) |
4523 | return true; |
4524 | |
4525 | /* Don't complain about the injected class name, as we've already |
4526 | complained about the class itself. */ |
4527 | if (DECL_SELF_REFERENCE_P (decl)) |
4528 | return false; |
4529 | |
4530 | auto_diagnostic_group d; |
4531 | if (DECL_TEMPLATE_PARM_P (decl)) |
4532 | error ("declaration of template parameter %q+D shadows " |
4533 | "template parameter", decl); |
4534 | else |
4535 | error ("declaration of %q+#D shadows template parameter", decl); |
4536 | inform (DECL_SOURCE_LOCATION (olddecl), |
4537 | "template parameter %qD declared here", olddecl); |
4538 | return false; |
4539 | } |
4540 | |
4541 | /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL, |
4542 | ORIG_LEVEL, DECL, and TYPE. */ |
4543 | |
4544 | static tree |
4545 | build_template_parm_index (int index, |
4546 | int level, |
4547 | int orig_level, |
4548 | tree decl, |
4549 | tree type) |
4550 | { |
4551 | tree t = make_node (TEMPLATE_PARM_INDEX); |
4552 | TEMPLATE_PARM_IDX (t) = index; |
4553 | TEMPLATE_PARM_LEVEL (t) = level; |
4554 | TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level; |
4555 | TEMPLATE_PARM_DECL (t) = decl; |
4556 | TREE_TYPE (t) = type; |
4557 | TREE_CONSTANT (t) = TREE_CONSTANT (decl); |
4558 | TREE_READONLY (t) = TREE_READONLY (decl); |
4559 | |
4560 | return t; |
4561 | } |
4562 | |
4563 | struct ctp_hasher : ggc_ptr_hash<tree_node> |
4564 | { |
4565 | static hashval_t hash (tree t) |
4566 | { |
4567 | ++comparing_specializations; |
4568 | tree_code code = TREE_CODE (t); |
4569 | hashval_t val = iterative_hash_object (code, 0); |
4570 | val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val); |
4571 | val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val); |
4572 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
4573 | { |
4574 | val |
4575 | = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val); |
4576 | if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t))) |
4577 | val = iterative_hash_placeholder_constraint (c, val); |
4578 | } |
4579 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
4580 | val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val); |
4581 | --comparing_specializations; |
4582 | return val; |
4583 | } |
4584 | |
4585 | static bool equal (tree t, tree u) |
4586 | { |
4587 | ++comparing_specializations; |
4588 | bool eq = comptypes (t, u, COMPARE_STRUCTURAL); |
4589 | --comparing_specializations; |
4590 | return eq; |
4591 | } |
4592 | }; |
4593 | |
4594 | static GTY (()) hash_table<ctp_hasher> *ctp_table; |
4595 | |
4596 | /* Find the canonical type parameter for the given template type |
4597 | parameter. Returns the canonical type parameter, which may be TYPE |
4598 | if no such parameter existed. */ |
4599 | |
4600 | tree |
4601 | canonical_type_parameter (tree type) |
4602 | { |
4603 | if (ctp_table == NULL) |
4604 | ctp_table = hash_table<ctp_hasher>::create_ggc (n: 61); |
4605 | |
4606 | tree& slot = *ctp_table->find_slot (value: type, insert: INSERT); |
4607 | if (slot == NULL_TREE) |
4608 | slot = type; |
4609 | return slot; |
4610 | } |
4611 | |
4612 | /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose |
4613 | TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a |
4614 | TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a |
4615 | new one is created. */ |
4616 | |
4617 | static tree |
4618 | reduce_template_parm_level (tree index, tree type, int levels, tree args, |
4619 | tsubst_flags_t complain) |
4620 | { |
4621 | if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE |
4622 | || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index)) |
4623 | != TEMPLATE_PARM_LEVEL (index) - levels) |
4624 | || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index)))) |
4625 | { |
4626 | tree orig_decl = TEMPLATE_PARM_DECL (index); |
4627 | |
4628 | tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl), |
4629 | TREE_CODE (orig_decl), DECL_NAME (orig_decl), |
4630 | type); |
4631 | TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl); |
4632 | TREE_READONLY (decl) = TREE_READONLY (orig_decl); |
4633 | DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl); |
4634 | DECL_ARTIFICIAL (decl) = 1; |
4635 | SET_DECL_TEMPLATE_PARM_P (decl); |
4636 | |
4637 | tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index), |
4638 | TEMPLATE_PARM_LEVEL (index) - levels, |
4639 | TEMPLATE_PARM_ORIG_LEVEL (index), |
4640 | decl, type); |
4641 | TEMPLATE_PARM_DESCENDANTS (index) = tpi; |
4642 | TEMPLATE_PARM_PARAMETER_PACK (tpi) |
4643 | = TEMPLATE_PARM_PARAMETER_PACK (index); |
4644 | |
4645 | /* Template template parameters need this. */ |
4646 | tree inner = decl; |
4647 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
4648 | { |
4649 | inner = build_lang_decl_loc (DECL_SOURCE_LOCATION (decl), |
4650 | TYPE_DECL, DECL_NAME (decl), type); |
4651 | DECL_TEMPLATE_RESULT (decl) = inner; |
4652 | DECL_ARTIFICIAL (inner) = true; |
4653 | tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (orig_decl), |
4654 | args, complain); |
4655 | DECL_TEMPLATE_PARMS (decl) = parms; |
4656 | tree orig_inner = DECL_TEMPLATE_RESULT (orig_decl); |
4657 | DECL_TEMPLATE_INFO (inner) |
4658 | = build_template_info (DECL_TI_TEMPLATE (orig_inner), |
4659 | template_args: template_parms_to_args (parms)); |
4660 | } |
4661 | |
4662 | /* Attach the TPI to the decl. */ |
4663 | if (TREE_CODE (inner) == TYPE_DECL) |
4664 | TEMPLATE_TYPE_PARM_INDEX (type) = tpi; |
4665 | else |
4666 | DECL_INITIAL (decl) = tpi; |
4667 | } |
4668 | |
4669 | return TEMPLATE_PARM_DESCENDANTS (index); |
4670 | } |
4671 | |
4672 | /* Process information from new template parameter PARM and append it |
4673 | to the LIST being built. This new parameter is a non-type |
4674 | parameter iff IS_NON_TYPE is true. This new parameter is a |
4675 | parameter pack iff IS_PARAMETER_PACK is true. The location of PARM |
4676 | is in PARM_LOC. */ |
4677 | |
4678 | tree |
4679 | process_template_parm (tree list, location_t parm_loc, tree parm, |
4680 | bool is_non_type, bool is_parameter_pack) |
4681 | { |
4682 | gcc_assert (TREE_CODE (parm) == TREE_LIST); |
4683 | tree prev = NULL_TREE; |
4684 | int idx = 0; |
4685 | |
4686 | if (list) |
4687 | { |
4688 | prev = tree_last (list); |
4689 | |
4690 | tree p = TREE_VALUE (prev); |
4691 | if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL) |
4692 | idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); |
4693 | else if (TREE_CODE (p) == PARM_DECL) |
4694 | idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p)); |
4695 | |
4696 | ++idx; |
4697 | } |
4698 | |
4699 | tree decl = NULL_TREE; |
4700 | tree defval = TREE_PURPOSE (parm); |
4701 | tree constr = TREE_TYPE (parm); |
4702 | |
4703 | if (is_non_type) |
4704 | { |
4705 | parm = TREE_VALUE (parm); |
4706 | |
4707 | SET_DECL_TEMPLATE_PARM_P (parm); |
4708 | |
4709 | if (TREE_TYPE (parm) != error_mark_node) |
4710 | { |
4711 | /* [temp.param] |
4712 | |
4713 | The top-level cv-qualifiers on the template-parameter are |
4714 | ignored when determining its type. */ |
4715 | TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm)); |
4716 | if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1)) |
4717 | TREE_TYPE (parm) = error_mark_node; |
4718 | else if (uses_parameter_packs (TREE_TYPE (parm)) |
4719 | && !is_parameter_pack |
4720 | /* If we're in a nested template parameter list, the template |
4721 | template parameter could be a parameter pack. */ |
4722 | && processing_template_parmlist == 1) |
4723 | { |
4724 | /* This template parameter is not a parameter pack, but it |
4725 | should be. Complain about "bare" parameter packs. */ |
4726 | check_for_bare_parameter_packs (TREE_TYPE (parm)); |
4727 | |
4728 | /* Recover by calling this a parameter pack. */ |
4729 | is_parameter_pack = true; |
4730 | } |
4731 | } |
4732 | |
4733 | /* A template parameter is not modifiable. */ |
4734 | TREE_CONSTANT (parm) = 1; |
4735 | TREE_READONLY (parm) = 1; |
4736 | decl = build_decl (parm_loc, |
4737 | CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); |
4738 | TREE_CONSTANT (decl) = 1; |
4739 | TREE_READONLY (decl) = 1; |
4740 | DECL_INITIAL (parm) = DECL_INITIAL (decl) |
4741 | = build_template_parm_index (index: idx, current_template_depth, |
4742 | current_template_depth, |
4743 | decl, TREE_TYPE (parm)); |
4744 | |
4745 | TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)) |
4746 | = is_parameter_pack; |
4747 | } |
4748 | else |
4749 | { |
4750 | tree t; |
4751 | parm = TREE_VALUE (TREE_VALUE (parm)); |
4752 | |
4753 | if (parm && TREE_CODE (parm) == TEMPLATE_DECL) |
4754 | { |
4755 | t = cxx_make_type (TEMPLATE_TEMPLATE_PARM); |
4756 | /* This is for distinguishing between real templates and template |
4757 | template parameters */ |
4758 | TREE_TYPE (parm) = t; |
4759 | |
4760 | /* any_template_parm_r expects to be able to get the targs of a |
4761 | DECL_TEMPLATE_RESULT. */ |
4762 | tree result = DECL_TEMPLATE_RESULT (parm); |
4763 | TREE_TYPE (result) = t; |
4764 | tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm)); |
4765 | tree tinfo = build_template_info (template_decl: parm, template_args: args); |
4766 | retrofit_lang_decl (result); |
4767 | DECL_TEMPLATE_INFO (result) = tinfo; |
4768 | |
4769 | decl = parm; |
4770 | } |
4771 | else |
4772 | { |
4773 | t = cxx_make_type (TEMPLATE_TYPE_PARM); |
4774 | /* parm is either IDENTIFIER_NODE or NULL_TREE. */ |
4775 | decl = build_decl (parm_loc, |
4776 | TYPE_DECL, parm, t); |
4777 | } |
4778 | |
4779 | TYPE_NAME (t) = decl; |
4780 | TYPE_STUB_DECL (t) = decl; |
4781 | parm = decl; |
4782 | TEMPLATE_TYPE_PARM_INDEX (t) |
4783 | = build_template_parm_index (index: idx, current_template_depth, |
4784 | current_template_depth, |
4785 | decl, TREE_TYPE (parm)); |
4786 | TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack; |
4787 | TYPE_CANONICAL (t) = canonical_type_parameter (type: t); |
4788 | } |
4789 | DECL_ARTIFICIAL (decl) = 1; |
4790 | SET_DECL_TEMPLATE_PARM_P (decl); |
4791 | |
4792 | if (TREE_CODE (parm) == TEMPLATE_DECL |
4793 | && !uses_outer_template_parms (parm)) |
4794 | TEMPLATE_TEMPLATE_PARM_SIMPLE_P (TREE_TYPE (parm)) = true; |
4795 | |
4796 | /* Build requirements for the type/template parameter. |
4797 | This must be done after SET_DECL_TEMPLATE_PARM_P or |
4798 | process_template_parm could fail. */ |
4799 | tree reqs = finish_shorthand_constraint (parm, constr); |
4800 | |
4801 | decl = pushdecl (decl); |
4802 | if (!is_non_type) |
4803 | parm = decl; |
4804 | |
4805 | /* Build the parameter node linking the parameter declaration, |
4806 | its default argument (if any), and its constraints (if any). */ |
4807 | parm = build_tree_list (defval, parm); |
4808 | TEMPLATE_PARM_CONSTRAINTS (parm) = reqs; |
4809 | |
4810 | if (prev) |
4811 | TREE_CHAIN (prev) = parm; |
4812 | else |
4813 | list = parm; |
4814 | |
4815 | return list; |
4816 | } |
4817 | |
4818 | /* The end of a template parameter list has been reached. Process the |
4819 | tree list into a parameter vector, converting each parameter into a more |
4820 | useful form. Type parameters are saved as IDENTIFIER_NODEs, and others |
4821 | as PARM_DECLs. */ |
4822 | |
4823 | tree |
4824 | end_template_parm_list (tree parms) |
4825 | { |
4826 | tree saved_parmlist = make_tree_vec (list_length (parms)); |
4827 | |
4828 | /* Pop the dummy parameter level and add the real one. We do not |
4829 | morph the dummy parameter in place, as it might have been |
4830 | captured by a (nested) template-template-parm. */ |
4831 | current_template_parms = TREE_CHAIN (current_template_parms); |
4832 | |
4833 | current_template_parms |
4834 | = tree_cons (size_int (current_template_depth + 1), |
4835 | saved_parmlist, current_template_parms); |
4836 | |
4837 | for (unsigned ix = 0; parms; ix++) |
4838 | { |
4839 | tree parm = parms; |
4840 | parms = TREE_CHAIN (parms); |
4841 | TREE_CHAIN (parm) = NULL_TREE; |
4842 | |
4843 | TREE_VEC_ELT (saved_parmlist, ix) = parm; |
4844 | } |
4845 | |
4846 | --processing_template_parmlist; |
4847 | |
4848 | return saved_parmlist; |
4849 | } |
4850 | |
4851 | // Explicitly indicate the end of the template parameter list. We assume |
4852 | // that the current template parameters have been constructed and/or |
4853 | // managed explicitly, as when creating new template template parameters |
4854 | // from a shorthand constraint. |
4855 | void |
4856 | end_template_parm_list () |
4857 | { |
4858 | --processing_template_parmlist; |
4859 | } |
4860 | |
4861 | /* end_template_decl is called after a template declaration is seen. */ |
4862 | |
4863 | void |
4864 | end_template_decl (void) |
4865 | { |
4866 | reset_specialization (); |
4867 | |
4868 | if (! processing_template_decl) |
4869 | return; |
4870 | |
4871 | /* This matches the pushlevel in begin_template_parm_list. */ |
4872 | finish_scope (); |
4873 | |
4874 | --processing_template_decl; |
4875 | current_template_parms = TREE_CHAIN (current_template_parms); |
4876 | } |
4877 | |
4878 | /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST |
4879 | thereof, and converts it into an argument suitable to be passed to |
4880 | the type substitution functions. Note that if the TREE_LIST contains |
4881 | an error_mark node, the returned argument is error_mark_node. */ |
4882 | |
4883 | tree |
4884 | template_parm_to_arg (tree t) |
4885 | { |
4886 | if (!t) |
4887 | return NULL_TREE; |
4888 | |
4889 | if (TREE_CODE (t) == TREE_LIST) |
4890 | t = TREE_VALUE (t); |
4891 | |
4892 | if (error_operand_p (t)) |
4893 | return error_mark_node; |
4894 | |
4895 | if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t)) |
4896 | { |
4897 | if (TREE_CODE (t) == TYPE_DECL |
4898 | || TREE_CODE (t) == TEMPLATE_DECL) |
4899 | t = TREE_TYPE (t); |
4900 | else |
4901 | t = DECL_INITIAL (t); |
4902 | } |
4903 | |
4904 | gcc_assert (TEMPLATE_PARM_P (t)); |
4905 | |
4906 | if (TREE_CODE (t) == TEMPLATE_TYPE_PARM |
4907 | || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) |
4908 | { |
4909 | if (TEMPLATE_TYPE_PARAMETER_PACK (t)) |
4910 | { |
4911 | /* Turn this argument into a TYPE_ARGUMENT_PACK |
4912 | with a single element, which expands T. */ |
4913 | tree vec = make_tree_vec (1); |
4914 | if (CHECKING_P) |
4915 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec)); |
4916 | |
4917 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (arg: t); |
4918 | |
4919 | t = cxx_make_type (TYPE_ARGUMENT_PACK); |
4920 | ARGUMENT_PACK_ARGS (t) = vec; |
4921 | } |
4922 | } |
4923 | else |
4924 | { |
4925 | if (TEMPLATE_PARM_PARAMETER_PACK (t)) |
4926 | { |
4927 | /* Turn this argument into a NONTYPE_ARGUMENT_PACK |
4928 | with a single element, which expands T. */ |
4929 | tree vec = make_tree_vec (1); |
4930 | if (CHECKING_P) |
4931 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec)); |
4932 | |
4933 | t = convert_from_reference (t); |
4934 | TREE_VEC_ELT (vec, 0) = make_pack_expansion (arg: t); |
4935 | |
4936 | t = make_node (NONTYPE_ARGUMENT_PACK); |
4937 | ARGUMENT_PACK_ARGS (t) = vec; |
4938 | } |
4939 | else |
4940 | t = convert_from_reference (t); |
4941 | } |
4942 | return t; |
4943 | } |
4944 | |
4945 | /* If T looks like a generic template argument produced by template_parm_to_arg, |
4946 | return the corresponding template parameter, otherwise return NULL_TREE. */ |
4947 | |
4948 | static tree |
4949 | template_arg_to_parm (tree t) |
4950 | { |
4951 | if (t == NULL_TREE) |
4952 | return NULL_TREE; |
4953 | |
4954 | if (ARGUMENT_PACK_P (t)) |
4955 | { |
4956 | tree args = ARGUMENT_PACK_ARGS (t); |
4957 | if (TREE_VEC_LENGTH (args) == 1 |
4958 | && PACK_EXPANSION_P (TREE_VEC_ELT (args, 0))) |
4959 | t = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (args, 0)); |
4960 | } |
4961 | |
4962 | if (REFERENCE_REF_P (t)) |
4963 | t = TREE_OPERAND (t, 0); |
4964 | |
4965 | if (TEMPLATE_PARM_P (t)) |
4966 | return t; |
4967 | else |
4968 | return NULL_TREE; |
4969 | } |
4970 | |
4971 | /* Given a single level of template parameters (a TREE_VEC), return it |
4972 | as a set of template arguments. */ |
4973 | |
4974 | tree |
4975 | template_parms_level_to_args (tree parms) |
4976 | { |
4977 | parms = copy_node (parms); |
4978 | TREE_TYPE (parms) = NULL_TREE; |
4979 | for (tree& parm : tree_vec_range (parms)) |
4980 | parm = template_parm_to_arg (t: parm); |
4981 | |
4982 | if (CHECKING_P) |
4983 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms)); |
4984 | |
4985 | return parms; |
4986 | } |
4987 | |
4988 | /* Given a set of template parameters, return them as a set of template |
4989 | arguments. The template parameters are represented as a TREE_VEC, in |
4990 | the form documented in cp-tree.h for template arguments. */ |
4991 | |
4992 | tree |
4993 | template_parms_to_args (tree parms) |
4994 | { |
4995 | tree header; |
4996 | tree args = NULL_TREE; |
4997 | int length = TMPL_PARMS_DEPTH (parms); |
4998 | int l = length; |
4999 | |
5000 | /* If there is only one level of template parameters, we do not |
5001 | create a TREE_VEC of TREE_VECs. Instead, we return a single |
5002 | TREE_VEC containing the arguments. */ |
5003 | if (length > 1) |
5004 | args = make_tree_vec (length); |
5005 | |
5006 | for (header = parms; header; header = TREE_CHAIN (header)) |
5007 | { |
5008 | tree a = template_parms_level_to_args (TREE_VALUE (header)); |
5009 | |
5010 | if (length > 1) |
5011 | TREE_VEC_ELT (args, --l) = a; |
5012 | else |
5013 | args = a; |
5014 | } |
5015 | |
5016 | return args; |
5017 | } |
5018 | |
5019 | /* Within the declaration of a template, return the currently active |
5020 | template parameters as an argument TREE_VEC. */ |
5021 | |
5022 | static tree |
5023 | current_template_args (void) |
5024 | { |
5025 | return template_parms_to_args (current_template_parms); |
5026 | } |
5027 | |
5028 | /* Return the fully generic arguments for of TMPL, i.e. what |
5029 | current_template_args would be while parsing it. */ |
5030 | |
5031 | tree |
5032 | generic_targs_for (tree tmpl) |
5033 | { |
5034 | if (tmpl == NULL_TREE) |
5035 | return NULL_TREE; |
5036 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) |
5037 | || DECL_TEMPLATE_SPECIALIZATION (tmpl)) |
5038 | /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template |
5039 | template parameter, it has no TEMPLATE_INFO; for a partial |
5040 | specialization, it has the arguments for the primary template, and we |
5041 | want the arguments for the partial specialization. */; |
5042 | else if (tree result = DECL_TEMPLATE_RESULT (tmpl)) |
5043 | if (tree ti = get_template_info (t: result)) |
5044 | return TI_ARGS (ti); |
5045 | return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)); |
5046 | } |
5047 | |
5048 | /* Return the template arguments corresponding to the template parameters of |
5049 | DECL's enclosing scope. When DECL is a member of a partial specialization, |
5050 | this returns the arguments for the partial specialization as opposed to those |
5051 | for the primary template, which is the main difference between this function |
5052 | and simply using e.g. the TYPE_TI_ARGS of DECL's DECL_CONTEXT. */ |
5053 | |
5054 | tree |
5055 | outer_template_args (const_tree decl) |
5056 | { |
5057 | if (TREE_CODE (decl) == TEMPLATE_DECL) |
5058 | decl = DECL_TEMPLATE_RESULT (decl); |
5059 | tree ti = get_template_info (t: decl); |
5060 | if (!ti) |
5061 | return NULL_TREE; |
5062 | tree args = TI_ARGS (ti); |
5063 | if (!PRIMARY_TEMPLATE_P (TI_TEMPLATE (ti))) |
5064 | return args; |
5065 | if (TMPL_ARGS_DEPTH (args) == 1) |
5066 | return NULL_TREE; |
5067 | return strip_innermost_template_args (args, extra_levels: 1); |
5068 | } |
5069 | |
5070 | /* Update the declared TYPE by doing any lookups which were thought to be |
5071 | dependent, but are not now that we know the SCOPE of the declarator. */ |
5072 | |
5073 | tree |
5074 | maybe_update_decl_type (tree orig_type, tree scope) |
5075 | { |
5076 | tree type = orig_type; |
5077 | |
5078 | if (type == NULL_TREE) |
5079 | return type; |
5080 | |
5081 | if (TREE_CODE (orig_type) == TYPE_DECL) |
5082 | type = TREE_TYPE (type); |
5083 | |
5084 | if (scope && TYPE_P (scope) && dependent_type_p (scope) |
5085 | && dependent_type_p (type) |
5086 | /* Don't bother building up the args in this case. */ |
5087 | && TREE_CODE (type) != TEMPLATE_TYPE_PARM) |
5088 | { |
5089 | /* tsubst in the args corresponding to the template parameters, |
5090 | including auto if present. Most things will be unchanged, but |
5091 | make_typename_type and tsubst_qualified_id will resolve |
5092 | TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */ |
5093 | tree args = current_template_args (); |
5094 | tree auto_node = type_uses_auto (type); |
5095 | tree pushed; |
5096 | if (auto_node) |
5097 | { |
5098 | tree auto_vec = make_tree_vec (1); |
5099 | TREE_VEC_ELT (auto_vec, 0) = auto_node; |
5100 | args = add_to_template_args (args, extra_args: auto_vec); |
5101 | } |
5102 | pushed = push_scope (scope); |
5103 | type = tsubst (type, args, tf_warning_or_error, NULL_TREE); |
5104 | if (pushed) |
5105 | pop_scope (scope); |
5106 | } |
5107 | |
5108 | if (type == error_mark_node) |
5109 | return orig_type; |
5110 | |
5111 | if (TREE_CODE (orig_type) == TYPE_DECL) |
5112 | { |
5113 | if (same_type_p (type, TREE_TYPE (orig_type))) |
5114 | type = orig_type; |
5115 | else |
5116 | type = TYPE_NAME (type); |
5117 | } |
5118 | return type; |
5119 | } |
5120 | |
5121 | /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated |
5122 | template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true, |
5123 | the new template is a member template. */ |
5124 | |
5125 | static tree |
5126 | build_template_decl (tree decl, tree parms, bool member_template_p) |
5127 | { |
5128 | gcc_checking_assert (TREE_CODE (decl) != TEMPLATE_DECL); |
5129 | |
5130 | tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE); |
5131 | SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl)); |
5132 | DECL_TEMPLATE_PARMS (tmpl) = parms; |
5133 | DECL_TEMPLATE_RESULT (tmpl) = decl; |
5134 | DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl); |
5135 | TREE_TYPE (tmpl) = TREE_TYPE (decl); |
5136 | DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl); |
5137 | DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p; |
5138 | |
5139 | /* Propagate module information from the decl. */ |
5140 | DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl); |
5141 | |
5142 | return tmpl; |
5143 | } |
5144 | |
5145 | struct template_parm_data |
5146 | { |
5147 | /* The level of the template parameters we are currently |
5148 | processing. */ |
5149 | int level; |
5150 | |
5151 | /* The index of the specialization argument we are currently |
5152 | processing. */ |
5153 | int current_arg; |
5154 | |
5155 | /* An array whose size is the number of template parameters. The |
5156 | elements are nonzero if the parameter has been used in any one |
5157 | of the arguments processed so far. */ |
5158 | int* parms; |
5159 | |
5160 | /* An array whose size is the number of template arguments. The |
5161 | elements are nonzero if the argument makes use of template |
5162 | parameters of this level. */ |
5163 | int* arg_uses_template_parms; |
5164 | }; |
5165 | |
5166 | /* Subroutine of push_template_decl used to see if each template |
5167 | parameter in a partial specialization is used in the explicit |
5168 | argument list. If T is of the LEVEL given in DATA (which is |
5169 | treated as a template_parm_data*), then DATA->PARMS is marked |
5170 | appropriately. */ |
5171 | |
5172 | static int |
5173 | mark_template_parm (tree t, void* data) |
5174 | { |
5175 | int level; |
5176 | int idx; |
5177 | struct template_parm_data* tpd = (struct template_parm_data*) data; |
5178 | |
5179 | template_parm_level_and_index (t, &level, &idx); |
5180 | |
5181 | if (level == tpd->level) |
5182 | { |
5183 | tpd->parms[idx] = 1; |
5184 | tpd->arg_uses_template_parms[tpd->current_arg] = 1; |
5185 | } |
5186 | |
5187 | /* In C++17 the type of a non-type argument is a deduced context. */ |
5188 | if (cxx_dialect >= cxx17 |
5189 | && TREE_CODE (t) == TEMPLATE_PARM_INDEX) |
5190 | for_each_template_parm (TREE_TYPE (t), |
5191 | &mark_template_parm, |
5192 | data, |
5193 | NULL, |
5194 | /*include_nondeduced_p=*/false); |
5195 | |
5196 | /* Return zero so that for_each_template_parm will continue the |
5197 | traversal of the tree; we want to mark *every* template parm. */ |
5198 | return 0; |
5199 | } |
5200 | |
5201 | /* Process the partial specialization DECL. */ |
5202 | |
5203 | static tree |
5204 | process_partial_specialization (tree decl) |
5205 | { |
5206 | tree type = TREE_TYPE (decl); |
5207 | tree tinfo = get_template_info (t: decl); |
5208 | tree maintmpl = TI_TEMPLATE (tinfo); |
5209 | tree specargs = TI_ARGS (tinfo); |
5210 | tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs); |
5211 | tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl); |
5212 | tree inner_parms; |
5213 | tree inst; |
5214 | int nargs = TREE_VEC_LENGTH (inner_args); |
5215 | int ntparms; |
5216 | int i; |
5217 | struct template_parm_data tpd; |
5218 | struct template_parm_data tpd2; |
5219 | |
5220 | gcc_assert (current_template_parms); |
5221 | |
5222 | inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); |
5223 | ntparms = TREE_VEC_LENGTH (inner_parms); |
5224 | |
5225 | /* We check that each of the template parameters given in the |
5226 | partial specialization is used in the argument list to the |
5227 | specialization. For example: |
5228 | |
5229 | template <class T> struct S; |
5230 | template <class T> struct S<T*>; |
5231 | |
5232 | The second declaration is OK because `T*' uses the template |
5233 | parameter T, whereas |
5234 | |
5235 | template <class T> struct S<int>; |
5236 | |
5237 | is no good. Even trickier is: |
5238 | |
5239 | template <class T> |
5240 | struct S1 |
5241 | { |
5242 | template <class U> |
5243 | struct S2; |
5244 | template <class U> |
5245 | struct S2<T>; |
5246 | }; |
5247 | |
5248 | The S2<T> declaration is actually invalid; it is a |
5249 | full-specialization. Of course, |
5250 | |
5251 | template <class U> |
5252 | struct S2<T (*)(U)>; |
5253 | |
5254 | or some such would have been OK. */ |
5255 | tpd.level = TMPL_PARMS_DEPTH (current_template_parms); |
5256 | tpd.parms = XALLOCAVEC (int, ntparms); |
5257 | memset (s: tpd.parms, c: 0, n: sizeof (int) * ntparms); |
5258 | |
5259 | tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs); |
5260 | memset (s: tpd.arg_uses_template_parms, c: 0, n: sizeof (int) * nargs); |
5261 | for (i = 0; i < nargs; ++i) |
5262 | { |
5263 | tpd.current_arg = i; |
5264 | for_each_template_parm (TREE_VEC_ELT (inner_args, i), |
5265 | &mark_template_parm, |
5266 | &tpd, |
5267 | NULL, |
5268 | /*include_nondeduced_p=*/false); |
5269 | } |
5270 | |
5271 | { |
5272 | auto_diagnostic_group d; |
5273 | bool did_error_intro = false; |
5274 | for (i = 0; i < ntparms; ++i) |
5275 | if (tpd.parms[i] == 0) |
5276 | { |
5277 | /* One of the template parms was not used in a deduced context in the |
5278 | specialization. */ |
5279 | if (!did_error_intro) |
5280 | { |
5281 | error ("template parameters not deducible in " |
5282 | "partial specialization:"); |
5283 | did_error_intro = true; |
5284 | } |
5285 | |
5286 | inform (input_location, " %qD", |
5287 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i))); |
5288 | } |
5289 | |
5290 | if (did_error_intro) |
5291 | return error_mark_node; |
5292 | } |
5293 | |
5294 | /* [temp.class.spec] |
5295 | |
5296 | The argument list of the specialization shall not be identical to |
5297 | the implicit argument list of the primary template. */ |
5298 | tree main_args |
5299 | = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl))); |
5300 | if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)) |
5301 | && (!flag_concepts |
5302 | || !strictly_subsumes (current_template_constraints (), maintmpl))) |
5303 | { |
5304 | auto_diagnostic_group d; |
5305 | if (!flag_concepts) |
5306 | error ("partial specialization %q+D does not specialize " |
5307 | "any template arguments; to define the primary template, " |
5308 | "remove the template argument list", decl); |
5309 | else |
5310 | error ("partial specialization %q+D does not specialize any " |
5311 | "template arguments and is not more constrained than " |
5312 | "the primary template; to define the primary template, " |
5313 | "remove the template argument list", decl); |
5314 | inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here"); |
5315 | } |
5316 | |
5317 | /* A partial specialization that replaces multiple parameters of the |
5318 | primary template with a pack expansion is less specialized for those |
5319 | parameters. */ |
5320 | if (nargs < DECL_NTPARMS (maintmpl)) |
5321 | { |
5322 | auto_diagnostic_group d; |
5323 | error ("partial specialization is not more specialized than the " |
5324 | "primary template because it replaces multiple parameters " |
5325 | "with a pack expansion"); |
5326 | inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here"); |
5327 | /* Avoid crash in process_partial_specialization. */ |
5328 | return decl; |
5329 | } |
5330 | |
5331 | else if (nargs > DECL_NTPARMS (maintmpl)) |
5332 | { |
5333 | auto_diagnostic_group d; |
5334 | error ("too many arguments for partial specialization %qT", type); |
5335 | inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here"); |
5336 | /* Avoid crash below. */ |
5337 | return decl; |
5338 | } |
5339 | |
5340 | /* If we aren't in a dependent class, we can actually try deduction. */ |
5341 | else if (tpd.level == 1 |
5342 | /* FIXME we should be able to handle a partial specialization of a |
5343 | partial instantiation, but currently we can't (c++/41727). */ |
5344 | && TMPL_ARGS_DEPTH (specargs) == 1 |
5345 | && !get_partial_spec_bindings (maintmpl, maintmpl, specargs)) |
5346 | { |
5347 | auto_diagnostic_group d; |
5348 | if (pedwarn (input_location, 0, |
5349 | "partial specialization %qD is not more specialized than", |
5350 | decl)) |
5351 | inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD", |
5352 | maintmpl); |
5353 | } |
5354 | |
5355 | /* [temp.spec.partial] |
5356 | |
5357 | The type of a template parameter corresponding to a specialized |
5358 | non-type argument shall not be dependent on a parameter of the |
5359 | specialization. |
5360 | |
5361 | Also, we verify that pack expansions only occur at the |
5362 | end of the argument list. */ |
5363 | tpd2.parms = 0; |
5364 | for (i = 0; i < nargs; ++i) |
5365 | { |
5366 | tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i)); |
5367 | tree arg = TREE_VEC_ELT (inner_args, i); |
5368 | tree packed_args = NULL_TREE; |
5369 | int j, len = 1; |
5370 | |
5371 | if (ARGUMENT_PACK_P (arg)) |
5372 | { |
5373 | /* Extract the arguments from the argument pack. We'll be |
5374 | iterating over these in the following loop. */ |
5375 | packed_args = ARGUMENT_PACK_ARGS (arg); |
5376 | len = TREE_VEC_LENGTH (packed_args); |
5377 | } |
5378 | |
5379 | for (j = 0; j < len; j++) |
5380 | { |
5381 | if (packed_args) |
5382 | /* Get the Jth argument in the parameter pack. */ |
5383 | arg = TREE_VEC_ELT (packed_args, j); |
5384 | |
5385 | if (PACK_EXPANSION_P (arg)) |
5386 | { |
5387 | /* Pack expansions must come at the end of the |
5388 | argument list. */ |
5389 | if ((packed_args && j < len - 1) |
5390 | || (!packed_args && i < nargs - 1)) |
5391 | { |
5392 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) |
5393 | error ("parameter pack argument %qE must be at the " |
5394 | "end of the template argument list", arg); |
5395 | else |
5396 | error ("parameter pack argument %qT must be at the " |
5397 | "end of the template argument list", arg); |
5398 | } |
5399 | } |
5400 | |
5401 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) |
5402 | /* We only care about the pattern. */ |
5403 | arg = PACK_EXPANSION_PATTERN (arg); |
5404 | |
5405 | if (/* These first two lines are the `non-type' bit. */ |
5406 | !TYPE_P (arg) |
5407 | && TREE_CODE (arg) != TEMPLATE_DECL |
5408 | /* This next two lines are the `argument expression is not just a |
5409 | simple identifier' condition and also the `specialized |
5410 | non-type argument' bit. */ |
5411 | && TREE_CODE (arg) != TEMPLATE_PARM_INDEX |
5412 | && !((REFERENCE_REF_P (arg) |
5413 | || TREE_CODE (arg) == VIEW_CONVERT_EXPR) |
5414 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX)) |
5415 | { |
5416 | /* Look at the corresponding template parameter, |
5417 | marking which template parameters its type depends |
5418 | upon. */ |
5419 | tree type = TREE_TYPE (parm); |
5420 | |
5421 | if (!tpd2.parms) |
5422 | { |
5423 | /* We haven't yet initialized TPD2. Do so now. */ |
5424 | tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs); |
5425 | /* The number of parameters here is the number in the |
5426 | main template, which, as checked in the assertion |
5427 | above, is NARGS. */ |
5428 | tpd2.parms = XALLOCAVEC (int, nargs); |
5429 | tpd2.level = |
5430 | TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl)); |
5431 | } |
5432 | |
5433 | /* Mark the template parameters. But this time, we're |
5434 | looking for the template parameters of the main |
5435 | template, not in the specialization. */ |
5436 | tpd2.current_arg = i; |
5437 | tpd2.arg_uses_template_parms[i] = 0; |
5438 | memset (s: tpd2.parms, c: 0, n: sizeof (int) * nargs); |
5439 | for_each_template_parm (type, |
5440 | &mark_template_parm, |
5441 | &tpd2, |
5442 | NULL, |
5443 | /*include_nondeduced_p=*/false); |
5444 | |
5445 | if (tpd2.arg_uses_template_parms [i]) |
5446 | { |
5447 | /* The type depended on some template parameters. |
5448 | If they are fully specialized in the |
5449 | specialization, that's OK. */ |
5450 | int j; |
5451 | int count = 0; |
5452 | for (j = 0; j < nargs; ++j) |
5453 | if (tpd2.parms[j] != 0 |
5454 | && tpd.arg_uses_template_parms [j]) |
5455 | ++count; |
5456 | if (count != 0) |
5457 | error_n (input_location, count, |
5458 | "type %qT of template argument %qE depends " |
5459 | "on a template parameter", |
5460 | "type %qT of template argument %qE depends " |
5461 | "on template parameters", |
5462 | type, |
5463 | arg); |
5464 | } |
5465 | } |
5466 | } |
5467 | } |
5468 | |
5469 | /* We should only get here once. */ |
5470 | if (TREE_CODE (decl) == TYPE_DECL) |
5471 | gcc_assert (!COMPLETE_TYPE_P (type)); |
5472 | |
5473 | // Build the template decl. |
5474 | tree tmpl = build_template_decl (decl, current_template_parms, |
5475 | DECL_MEMBER_TEMPLATE_P (maintmpl)); |
5476 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
5477 | DECL_TEMPLATE_INFO (tmpl) = build_template_info (template_decl: maintmpl, template_args: specargs); |
5478 | DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl; |
5479 | |
5480 | /* Give template template parms a DECL_CONTEXT of the template |
5481 | for which they are a parameter. */ |
5482 | for (i = 0; i < ntparms; ++i) |
5483 | { |
5484 | tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i)); |
5485 | if (TREE_CODE (parm) == TEMPLATE_DECL) |
5486 | DECL_CONTEXT (parm) = tmpl; |
5487 | } |
5488 | |
5489 | if (VAR_P (decl)) |
5490 | { |
5491 | /* We didn't register this in check_explicit_specialization so we could |
5492 | wait until the constraints were set. */ |
5493 | tree reg = register_specialization (spec: decl, tmpl: maintmpl, args: specargs, is_friend: false, hash: 0); |
5494 | if (reg != decl) |
5495 | /* Redeclaration. */ |
5496 | return reg; |
5497 | } |
5498 | else |
5499 | associate_classtype_constraints (type); |
5500 | |
5501 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) |
5502 | = tree_cons (specargs, tmpl, |
5503 | DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); |
5504 | TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; |
5505 | /* Link the DECL_TEMPLATE_RESULT back to the partial TEMPLATE_DECL. */ |
5506 | gcc_checking_assert (!TI_PARTIAL_INFO (tinfo)); |
5507 | TI_PARTIAL_INFO (tinfo) = build_template_info (template_decl: tmpl, NULL_TREE); |
5508 | |
5509 | set_defining_module_for_partial_spec (decl); |
5510 | |
5511 | for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst; |
5512 | inst = TREE_CHAIN (inst)) |
5513 | { |
5514 | tree instance = TREE_VALUE (inst); |
5515 | if (TYPE_P (instance) |
5516 | ? (COMPLETE_TYPE_P (instance) |
5517 | && CLASSTYPE_IMPLICIT_INSTANTIATION (instance)) |
5518 | : DECL_TEMPLATE_INSTANTIATION (instance)) |
5519 | { |
5520 | tree partial_ti = most_specialized_partial_spec (instance, tf_none, |
5521 | /*rechecking=*/true); |
5522 | tree inst_decl = (DECL_P (instance) |
5523 | ? instance : TYPE_NAME (instance)); |
5524 | if (!partial_ti) |
5525 | /* OK */; |
5526 | else if (partial_ti == error_mark_node) |
5527 | permerror (input_location, |
5528 | "declaration of %qD ambiguates earlier template " |
5529 | "instantiation for %qD", decl, inst_decl); |
5530 | else if (TI_TEMPLATE (partial_ti) == tmpl) |
5531 | permerror (input_location, |
5532 | "partial specialization of %qD after instantiation " |
5533 | "of %qD", decl, inst_decl); |
5534 | } |
5535 | } |
5536 | |
5537 | return decl; |
5538 | } |
5539 | |
5540 | /* PARM is a template parameter of some form; return the corresponding |
5541 | TEMPLATE_PARM_INDEX. */ |
5542 | |
5543 | static tree |
5544 | get_template_parm_index (tree parm) |
5545 | { |
5546 | if (TREE_CODE (parm) == PARM_DECL |
5547 | || TREE_CODE (parm) == CONST_DECL) |
5548 | parm = DECL_INITIAL (parm); |
5549 | else if (TREE_CODE (parm) == TYPE_DECL |
5550 | || TREE_CODE (parm) == TEMPLATE_DECL) |
5551 | parm = TREE_TYPE (parm); |
5552 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
5553 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM |
5554 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM) |
5555 | parm = TEMPLATE_TYPE_PARM_INDEX (parm); |
5556 | gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX); |
5557 | return parm; |
5558 | } |
5559 | |
5560 | /* Subroutine of fixed_parameter_pack_p below. Look for any template |
5561 | parameter packs used by the template parameter PARM. */ |
5562 | |
5563 | static void |
5564 | fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd) |
5565 | { |
5566 | /* A type parm can't refer to another parm. */ |
5567 | if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node) |
5568 | return; |
5569 | else if (TREE_CODE (parm) == PARM_DECL) |
5570 | { |
5571 | cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r, |
5572 | ppd, ppd->visited); |
5573 | return; |
5574 | } |
5575 | |
5576 | gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL); |
5577 | |
5578 | tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm)); |
5579 | for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i) |
5580 | { |
5581 | tree p = TREE_VALUE (TREE_VEC_ELT (vec, i)); |
5582 | if (template_parameter_pack_p (parm: p)) |
5583 | /* Any packs in the type are expanded by this parameter. */; |
5584 | else |
5585 | fixed_parameter_pack_p_1 (parm: p, ppd); |
5586 | } |
5587 | } |
5588 | |
5589 | /* PARM is a template parameter pack. Return any parameter packs used in |
5590 | its type or the type of any of its template parameters. If there are |
5591 | any such packs, it will be instantiated into a fixed template parameter |
5592 | list by partial instantiation rather than be fully deduced. */ |
5593 | |
5594 | tree |
5595 | fixed_parameter_pack_p (tree parm) |
5596 | { |
5597 | /* This can only be true in a member template. */ |
5598 | if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2) |
5599 | return NULL_TREE; |
5600 | /* This can only be true for a parameter pack. */ |
5601 | if (!template_parameter_pack_p (parm)) |
5602 | return NULL_TREE; |
5603 | /* A type parm can't refer to another parm. */ |
5604 | if (TREE_CODE (parm) == TYPE_DECL) |
5605 | return NULL_TREE; |
5606 | |
5607 | tree parameter_packs = NULL_TREE; |
5608 | struct find_parameter_pack_data ppd; |
5609 | ppd.parameter_packs = ¶meter_packs; |
5610 | ppd.visited = new hash_set<tree>; |
5611 | |
5612 | fixed_parameter_pack_p_1 (parm, ppd: &ppd); |
5613 | |
5614 | delete ppd.visited; |
5615 | return parameter_packs; |
5616 | } |
5617 | |
5618 | /* Check that a template declaration's use of default arguments and |
5619 | parameter packs is not invalid. Here, PARMS are the template |
5620 | parameters. IS_PRIMARY is true if DECL is the thing declared by |
5621 | a primary template. IS_PARTIAL is true if DECL is a partial |
5622 | specialization. |
5623 | |
5624 | IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend |
5625 | function template declaration or a friend class template |
5626 | declaration. In the function case, 1 indicates a declaration, 2 |
5627 | indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are |
5628 | emitted for extraneous default arguments. |
5629 | |
5630 | Returns TRUE if there were no errors found, FALSE otherwise. */ |
5631 | |
5632 | bool |
5633 | check_default_tmpl_args (tree decl, tree parms, bool is_primary, |
5634 | bool is_partial, int is_friend_decl) |
5635 | { |
5636 | const char *msg; |
5637 | int last_level_to_check; |
5638 | tree parm_level; |
5639 | bool no_errors = true; |
5640 | |
5641 | /* [temp.param] |
5642 | |
5643 | A default template-argument shall not be specified in a |
5644 | function template declaration or a function template definition, nor |
5645 | in the template-parameter-list of the definition of a member of a |
5646 | class template. */ |
5647 | |
5648 | if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL |
5649 | || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl))) |
5650 | /* You can't have a function template declaration in a local |
5651 | scope, nor you can you define a member of a class template in a |
5652 | local scope. */ |
5653 | return true; |
5654 | |
5655 | if ((DECL_IMPLICIT_TYPEDEF_P (decl) |
5656 | && LAMBDA_TYPE_P (TREE_TYPE (decl))) |
5657 | || (TREE_CODE (decl) == FUNCTION_DECL |
5658 | && LAMBDA_FUNCTION_P (decl))) |
5659 | /* A lambda doesn't have an explicit declaration; don't complain |
5660 | about the parms of the enclosing class. */ |
5661 | return true; |
5662 | |
5663 | if (current_class_type |
5664 | && !TYPE_BEING_DEFINED (current_class_type) |
5665 | && DECL_LANG_SPECIFIC (decl) |
5666 | && DECL_DECLARES_FUNCTION_P (decl) |
5667 | /* If this is either a friend defined in the scope of the class |
5668 | or a member function. */ |
5669 | && (DECL_FUNCTION_MEMBER_P (decl) |
5670 | ? same_type_p (DECL_CONTEXT (decl), current_class_type) |
5671 | : DECL_FRIEND_CONTEXT (decl) |
5672 | ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type) |
5673 | : false) |
5674 | /* And, if it was a member function, it really was defined in |
5675 | the scope of the class. */ |
5676 | && (!DECL_FUNCTION_MEMBER_P (decl) |
5677 | || DECL_INITIALIZED_IN_CLASS_P (decl))) |
5678 | /* We already checked these parameters when the template was |
5679 | declared, so there's no need to do it again now. This function |
5680 | was defined in class scope, but we're processing its body now |
5681 | that the class is complete. */ |
5682 | return true; |
5683 | |
5684 | /* Core issue 226 (C++0x only): the following only applies to class |
5685 | templates. */ |
5686 | if (is_primary |
5687 | && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)) |
5688 | { |
5689 | /* [temp.param] |
5690 | |
5691 | If a template-parameter has a default template-argument, all |
5692 | subsequent template-parameters shall have a default |
5693 | template-argument supplied. */ |
5694 | for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level)) |
5695 | { |
5696 | tree inner_parms = TREE_VALUE (parm_level); |
5697 | int ntparms = TREE_VEC_LENGTH (inner_parms); |
5698 | int seen_def_arg_p = 0; |
5699 | int i; |
5700 | |
5701 | for (i = 0; i < ntparms; ++i) |
5702 | { |
5703 | tree parm = TREE_VEC_ELT (inner_parms, i); |
5704 | |
5705 | if (parm == error_mark_node) |
5706 | continue; |
5707 | |
5708 | if (TREE_PURPOSE (parm)) |
5709 | seen_def_arg_p = 1; |
5710 | else if (seen_def_arg_p |
5711 | && !template_parameter_pack_p (TREE_VALUE (parm))) |
5712 | { |
5713 | error ("no default argument for %qD", TREE_VALUE (parm)); |
5714 | /* For better subsequent error-recovery, we indicate that |
5715 | there should have been a default argument. */ |
5716 | TREE_PURPOSE (parm) = error_mark_node; |
5717 | no_errors = false; |
5718 | } |
5719 | else if (!is_partial |
5720 | && !is_friend_decl |
5721 | /* Don't complain about an enclosing partial |
5722 | specialization. */ |
5723 | && parm_level == parms |
5724 | && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl)) |
5725 | && i < ntparms - 1 |
5726 | && template_parameter_pack_p (TREE_VALUE (parm)) |
5727 | /* A fixed parameter pack will be partially |
5728 | instantiated into a fixed length list. */ |
5729 | && !fixed_parameter_pack_p (TREE_VALUE (parm))) |
5730 | { |
5731 | /* A primary class template, primary variable template |
5732 | (DR 2032), or alias template can only have one |
5733 | parameter pack, at the end of the template |
5734 | parameter list. */ |
5735 | |
5736 | error ("parameter pack %q+D must be at the end of the" |
5737 | " template parameter list", TREE_VALUE (parm)); |
5738 | |
5739 | TREE_VALUE (TREE_VEC_ELT (inner_parms, i)) |
5740 | = error_mark_node; |
5741 | no_errors = false; |
5742 | } |
5743 | } |
5744 | } |
5745 | } |
5746 | |
5747 | if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL) |
5748 | || is_partial |
5749 | || !is_primary |
5750 | || is_friend_decl) |
5751 | /* For an ordinary class template, default template arguments are |
5752 | allowed at the innermost level, e.g.: |
5753 | template <class T = int> |
5754 | struct S {}; |
5755 | but, in a partial specialization, they're not allowed even |
5756 | there, as we have in [temp.class.spec]: |
5757 | |
5758 | The template parameter list of a specialization shall not |
5759 | contain default template argument values. |
5760 | |
5761 | So, for a partial specialization, or for a function template |
5762 | (in C++98/C++03), we look at all of them. */ |
5763 | ; |
5764 | else |
5765 | /* But, for a primary class template that is not a partial |
5766 | specialization we look at all template parameters except the |
5767 | innermost ones. */ |
5768 | parms = TREE_CHAIN (parms); |
5769 | |
5770 | /* Figure out what error message to issue. */ |
5771 | if (is_friend_decl == 2) |
5772 | msg = G_("default template arguments may not be used in function template " |
5773 | "friend re-declaration"); |
5774 | else if (is_friend_decl) |
5775 | msg = G_("default template arguments may not be used in template " |
5776 | "friend declarations"); |
5777 | else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98)) |
5778 | msg = G_("default template arguments may not be used in function templates " |
5779 | "without %<-std=c++11%> or %<-std=gnu++11%>"); |
5780 | else if (is_partial) |
5781 | msg = G_("default template arguments may not be used in " |
5782 | "partial specializations"); |
5783 | else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type)) |
5784 | msg = G_("default argument for template parameter for class enclosing %qD"); |
5785 | else |
5786 | /* Per [temp.param]/9, "A default template-argument shall not be |
5787 | specified in the template-parameter-lists of the definition of |
5788 | a member of a class template that appears outside of the member's |
5789 | class.", thus if we aren't handling a member of a class template |
5790 | there is no need to examine the parameters. */ |
5791 | return true; |
5792 | |
5793 | if (current_class_type && TYPE_BEING_DEFINED (current_class_type)) |
5794 | /* If we're inside a class definition, there's no need to |
5795 | examine the parameters to the class itself. On the one |
5796 | hand, they will be checked when the class is defined, and, |
5797 | on the other, default arguments are valid in things like: |
5798 | template <class T = double> |
5799 | struct S { template <class U> void f(U); }; |
5800 | Here the default argument for `S' has no bearing on the |
5801 | declaration of `f'. */ |
5802 | last_level_to_check = template_class_depth (current_class_type) + 1; |
5803 | else |
5804 | /* Check everything. */ |
5805 | last_level_to_check = 0; |
5806 | |
5807 | for (parm_level = parms; |
5808 | parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check; |
5809 | parm_level = TREE_CHAIN (parm_level)) |
5810 | { |
5811 | tree inner_parms = TREE_VALUE (parm_level); |
5812 | int i; |
5813 | int ntparms; |
5814 | |
5815 | ntparms = TREE_VEC_LENGTH (inner_parms); |
5816 | for (i = 0; i < ntparms; ++i) |
5817 | { |
5818 | if (TREE_VEC_ELT (inner_parms, i) == error_mark_node) |
5819 | continue; |
5820 | |
5821 | if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i))) |
5822 | { |
5823 | if (msg) |
5824 | { |
5825 | no_errors = false; |
5826 | if (is_friend_decl == 2) |
5827 | return no_errors; |
5828 | |
5829 | error (msg, decl); |
5830 | msg = 0; |
5831 | } |
5832 | |
5833 | /* Clear out the default argument so that we are not |
5834 | confused later. */ |
5835 | TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE; |
5836 | } |
5837 | } |
5838 | |
5839 | /* At this point, if we're still interested in issuing messages, |
5840 | they must apply to classes surrounding the object declared. */ |
5841 | if (msg) |
5842 | msg = G_("default argument for template parameter for class " |
5843 | "enclosing %qD"); |
5844 | } |
5845 | |
5846 | return no_errors; |
5847 | } |
5848 | |
5849 | /* Worker for push_template_decl_real, called via |
5850 | for_each_template_parm. DATA is really an int, indicating the |
5851 | level of the parameters we are interested in. If T is a template |
5852 | parameter of that level, return nonzero. */ |
5853 | |
5854 | static int |
5855 | template_parm_this_level_p (tree t, void* data) |
5856 | { |
5857 | int this_level = *(int *)data; |
5858 | int level; |
5859 | |
5860 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) |
5861 | level = TEMPLATE_PARM_LEVEL (t); |
5862 | else |
5863 | level = TEMPLATE_TYPE_LEVEL (t); |
5864 | return level == this_level; |
5865 | } |
5866 | |
5867 | /* Worker for uses_outer_template_parms, called via for_each_template_parm. |
5868 | DATA is really an int, indicating the innermost outer level of parameters. |
5869 | If T is a template parameter of that level or further out, return |
5870 | nonzero. */ |
5871 | |
5872 | static int |
5873 | template_parm_outer_level (tree t, void *data) |
5874 | { |
5875 | int this_level = *(int *)data; |
5876 | int level; |
5877 | |
5878 | if (TREE_CODE (t) == TEMPLATE_PARM_INDEX) |
5879 | level = TEMPLATE_PARM_LEVEL (t); |
5880 | else |
5881 | level = TEMPLATE_TYPE_LEVEL (t); |
5882 | return level <= this_level; |
5883 | } |
5884 | |
5885 | /* Creates a TEMPLATE_DECL for the indicated DECL using the template |
5886 | parameters given by current_template_args, or reuses a |
5887 | previously existing one, if appropriate. Returns the DECL, or an |
5888 | equivalent one, if it is replaced via a call to duplicate_decls. |
5889 | |
5890 | If IS_FRIEND is true, DECL is a friend declaration. */ |
5891 | |
5892 | tree |
5893 | push_template_decl (tree decl, bool is_friend) |
5894 | { |
5895 | if (decl == error_mark_node || !current_template_parms) |
5896 | return error_mark_node; |
5897 | |
5898 | /* See if this is a partial specialization. */ |
5899 | bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl) |
5900 | && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE |
5901 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) |
5902 | || (VAR_P (decl) |
5903 | && DECL_LANG_SPECIFIC (decl) |
5904 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
5905 | && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)))); |
5906 | |
5907 | /* No surprising friend functions. */ |
5908 | gcc_checking_assert (is_friend |
5909 | || !(TREE_CODE (decl) == FUNCTION_DECL |
5910 | && DECL_UNIQUE_FRIEND_P (decl))); |
5911 | |
5912 | tree ctx; |
5913 | if (is_friend) |
5914 | /* For a friend, we want the context of the friend, not |
5915 | the type of which it is a friend. */ |
5916 | ctx = CP_DECL_CONTEXT (decl); |
5917 | else if (CP_DECL_CONTEXT (decl) |
5918 | && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL) |
5919 | /* In the case of a virtual function, we want the class in which |
5920 | it is defined. */ |
5921 | ctx = CP_DECL_CONTEXT (decl); |
5922 | else |
5923 | /* Otherwise, if we're currently defining some class, the DECL |
5924 | is assumed to be a member of the class. */ |
5925 | ctx = current_scope (); |
5926 | |
5927 | if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL) |
5928 | ctx = NULL_TREE; |
5929 | |
5930 | if (!DECL_CONTEXT (decl)) |
5931 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
5932 | |
5933 | /* See if this is a primary template. */ |
5934 | bool is_primary = false; |
5935 | if (is_friend && ctx |
5936 | && uses_template_parms_level (ctx, current_template_depth)) |
5937 | /* A friend template that specifies a class context, i.e. |
5938 | template <typename T> friend void A<T>::f(); |
5939 | is not primary. */ |
5940 | ; |
5941 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) && LAMBDA_TYPE_P (TREE_TYPE (decl))) |
5942 | /* Lambdas are not primary. */ |
5943 | ; |
5944 | else |
5945 | is_primary = template_parm_scope_p (); |
5946 | |
5947 | /* True if the template is a member template, in the sense of |
5948 | [temp.mem]. */ |
5949 | bool member_template_p = false; |
5950 | |
5951 | if (is_primary) |
5952 | { |
5953 | warning (OPT_Wtemplates, "template %qD declared", decl); |
5954 | |
5955 | if (DECL_CLASS_SCOPE_P (decl)) |
5956 | member_template_p = true; |
5957 | |
5958 | if (TREE_CODE (decl) == TYPE_DECL |
5959 | && IDENTIFIER_ANON_P (DECL_NAME (decl))) |
5960 | { |
5961 | error ("template class without a name"); |
5962 | return error_mark_node; |
5963 | } |
5964 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
5965 | { |
5966 | if (member_template_p) |
5967 | { |
5968 | if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl)) |
5969 | error ("member template %qD may not have virt-specifiers", decl); |
5970 | } |
5971 | if (DECL_DESTRUCTOR_P (decl)) |
5972 | { |
5973 | /* [temp.mem] |
5974 | |
5975 | A destructor shall not be a member template. */ |
5976 | error_at (DECL_SOURCE_LOCATION (decl), |
5977 | "destructor %qD declared as member template", decl); |
5978 | return error_mark_node; |
5979 | } |
5980 | if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl)) |
5981 | && (!prototype_p (TREE_TYPE (decl)) |
5982 | || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node |
5983 | || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) |
5984 | || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))) |
5985 | == void_list_node))) |
5986 | { |
5987 | /* [basic.stc.dynamic.allocation] |
5988 | |
5989 | An allocation function can be a function |
5990 | template. ... Template allocation functions shall |
5991 | have two or more parameters. */ |
5992 | error ("invalid template declaration of %qD", decl); |
5993 | return error_mark_node; |
5994 | } |
5995 | } |
5996 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
5997 | && CLASS_TYPE_P (TREE_TYPE (decl))) |
5998 | /* Class template. */; |
5999 | else if (TREE_CODE (decl) == TYPE_DECL |
6000 | && TYPE_DECL_ALIAS_P (decl)) |
6001 | /* alias-declaration */ |
6002 | gcc_assert (!DECL_ARTIFICIAL (decl)); |
6003 | else if (VAR_P (decl)) |
6004 | /* C++14 variable template. */; |
6005 | else if (TREE_CODE (decl) == CONCEPT_DECL) |
6006 | /* C++20 concept definitions. */; |
6007 | else |
6008 | { |
6009 | error ("template declaration of %q#D", decl); |
6010 | return error_mark_node; |
6011 | } |
6012 | } |
6013 | |
6014 | bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl) |
6015 | && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL) |
6016 | || (VAR_OR_FUNCTION_DECL_P (decl) |
6017 | && DECL_LOCAL_DECL_P (decl)))); |
6018 | |
6019 | /* Check to see that the rules regarding the use of default |
6020 | arguments are not being violated. We check args for a friend |
6021 | functions when we know whether it's a definition, introducing |
6022 | declaration or re-declaration. */ |
6023 | if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)) |
6024 | check_default_tmpl_args (decl, current_template_parms, |
6025 | is_primary, is_partial, is_friend_decl: is_friend); |
6026 | |
6027 | /* Ensure that there are no parameter packs in the type of this |
6028 | declaration that have not been expanded. */ |
6029 | if (TREE_CODE (decl) == FUNCTION_DECL) |
6030 | { |
6031 | /* Check each of the arguments individually to see if there are |
6032 | any bare parameter packs. */ |
6033 | tree type = TREE_TYPE (decl); |
6034 | tree arg = DECL_ARGUMENTS (decl); |
6035 | tree argtype = TYPE_ARG_TYPES (type); |
6036 | |
6037 | while (arg && argtype) |
6038 | { |
6039 | if (!DECL_PACK_P (arg) |
6040 | && check_for_bare_parameter_packs (TREE_TYPE (arg))) |
6041 | { |
6042 | /* This is a PARM_DECL that contains unexpanded parameter |
6043 | packs. We have already complained about this in the |
6044 | check_for_bare_parameter_packs call, so just replace |
6045 | these types with ERROR_MARK_NODE. */ |
6046 | TREE_TYPE (arg) = error_mark_node; |
6047 | TREE_VALUE (argtype) = error_mark_node; |
6048 | } |
6049 | |
6050 | arg = DECL_CHAIN (arg); |
6051 | argtype = TREE_CHAIN (argtype); |
6052 | } |
6053 | |
6054 | /* Check for bare parameter packs in the return type and the |
6055 | exception specifiers. */ |
6056 | if (check_for_bare_parameter_packs (TREE_TYPE (type))) |
6057 | /* Errors were already issued, set return type to int |
6058 | as the frontend doesn't expect error_mark_node as |
6059 | the return type. */ |
6060 | TREE_TYPE (type) = integer_type_node; |
6061 | if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type))) |
6062 | TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE; |
6063 | } |
6064 | else |
6065 | { |
6066 | if (check_for_bare_parameter_packs (t: is_typedef_decl (x: decl) |
6067 | ? DECL_ORIGINAL_TYPE (decl) |
6068 | : TREE_TYPE (decl))) |
6069 | { |
6070 | TREE_TYPE (decl) = error_mark_node; |
6071 | return error_mark_node; |
6072 | } |
6073 | |
6074 | if (is_partial && VAR_P (decl) |
6075 | && check_for_bare_parameter_packs (DECL_TI_ARGS (decl))) |
6076 | return error_mark_node; |
6077 | } |
6078 | |
6079 | if (is_partial) |
6080 | return process_partial_specialization (decl); |
6081 | |
6082 | tree args = current_template_args (); |
6083 | tree tmpl = NULL_TREE; |
6084 | bool new_template_p = false; |
6085 | if (local_p) |
6086 | { |
6087 | /* Does not get a template head. */ |
6088 | tmpl = NULL_TREE; |
6089 | gcc_checking_assert (!is_primary); |
6090 | } |
6091 | else if (!ctx |
6092 | || TREE_CODE (ctx) == FUNCTION_DECL |
6093 | || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx)) |
6094 | || (DECL_IMPLICIT_TYPEDEF_P (decl) |
6095 | && LAMBDA_TYPE_P (TREE_TYPE (decl))) |
6096 | || (is_friend && !(DECL_LANG_SPECIFIC (decl) |
6097 | && DECL_TEMPLATE_INFO (decl)))) |
6098 | { |
6099 | if (DECL_LANG_SPECIFIC (decl) |
6100 | && DECL_TEMPLATE_INFO (decl) |
6101 | && DECL_TI_TEMPLATE (decl)) |
6102 | tmpl = DECL_TI_TEMPLATE (decl); |
6103 | /* If DECL is a TYPE_DECL for a class-template, then there won't |
6104 | be DECL_LANG_SPECIFIC. The information equivalent to |
6105 | DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */ |
6106 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
6107 | && TYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
6108 | && TYPE_TI_TEMPLATE (TREE_TYPE (decl))) |
6109 | { |
6110 | /* Since a template declaration already existed for this |
6111 | class-type, we must be redeclaring it here. Make sure |
6112 | that the redeclaration is valid. */ |
6113 | redeclare_class_template (TREE_TYPE (decl), |
6114 | current_template_parms, |
6115 | current_template_constraints ()); |
6116 | /* We don't need to create a new TEMPLATE_DECL; just use the |
6117 | one we already had. */ |
6118 | tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl)); |
6119 | } |
6120 | else |
6121 | { |
6122 | tmpl = build_template_decl (decl, current_template_parms, |
6123 | member_template_p); |
6124 | new_template_p = true; |
6125 | |
6126 | if (DECL_LANG_SPECIFIC (decl) |
6127 | && DECL_TEMPLATE_SPECIALIZATION (decl)) |
6128 | { |
6129 | /* A specialization of a member template of a template |
6130 | class. */ |
6131 | SET_DECL_TEMPLATE_SPECIALIZATION (tmpl); |
6132 | DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl); |
6133 | DECL_TEMPLATE_INFO (decl) = NULL_TREE; |
6134 | } |
6135 | } |
6136 | } |
6137 | else |
6138 | { |
6139 | tree a, t, current, parms; |
6140 | int i; |
6141 | tree tinfo = get_template_info (t: decl); |
6142 | |
6143 | if (!tinfo) |
6144 | { |
6145 | error ("template definition of non-template %q#D", decl); |
6146 | return error_mark_node; |
6147 | } |
6148 | |
6149 | tmpl = TI_TEMPLATE (tinfo); |
6150 | |
6151 | if (DECL_FUNCTION_TEMPLATE_P (tmpl) |
6152 | && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) |
6153 | && DECL_TEMPLATE_SPECIALIZATION (decl) |
6154 | && DECL_MEMBER_TEMPLATE_P (tmpl)) |
6155 | { |
6156 | /* The declaration is a specialization of a member |
6157 | template, declared outside the class. Therefore, the |
6158 | innermost template arguments will be NULL, so we |
6159 | replace them with the arguments determined by the |
6160 | earlier call to check_explicit_specialization. */ |
6161 | args = DECL_TI_ARGS (decl); |
6162 | |
6163 | tree new_tmpl |
6164 | = build_template_decl (decl, current_template_parms, |
6165 | member_template_p); |
6166 | DECL_TI_TEMPLATE (decl) = new_tmpl; |
6167 | SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl); |
6168 | DECL_TEMPLATE_INFO (new_tmpl) |
6169 | = build_template_info (template_decl: tmpl, template_args: args); |
6170 | |
6171 | register_specialization (spec: new_tmpl, |
6172 | tmpl: most_general_template (tmpl), |
6173 | args, |
6174 | is_friend, hash: 0); |
6175 | return decl; |
6176 | } |
6177 | |
6178 | /* Make sure the template headers we got make sense. */ |
6179 | |
6180 | parms = DECL_TEMPLATE_PARMS (tmpl); |
6181 | i = TMPL_PARMS_DEPTH (parms); |
6182 | if (TMPL_ARGS_DEPTH (args) != i) |
6183 | { |
6184 | error ("expected %d levels of template parms for %q#D, got %d", |
6185 | i, decl, TMPL_ARGS_DEPTH (args)); |
6186 | DECL_INTERFACE_KNOWN (decl) = 1; |
6187 | return error_mark_node; |
6188 | } |
6189 | else |
6190 | for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms)) |
6191 | { |
6192 | a = TMPL_ARGS_LEVEL (args, i); |
6193 | t = INNERMOST_TEMPLATE_PARMS (parms); |
6194 | |
6195 | if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) |
6196 | { |
6197 | if (current == decl) |
6198 | error ("got %d template parameters for %q#D", |
6199 | TREE_VEC_LENGTH (a), decl); |
6200 | else |
6201 | error ("got %d template parameters for %q#T", |
6202 | TREE_VEC_LENGTH (a), current); |
6203 | error (" but %d required", TREE_VEC_LENGTH (t)); |
6204 | /* Avoid crash in import_export_decl. */ |
6205 | DECL_INTERFACE_KNOWN (decl) = 1; |
6206 | return error_mark_node; |
6207 | } |
6208 | |
6209 | if (current == decl) |
6210 | current = ctx; |
6211 | else if (current == NULL_TREE) |
6212 | /* Can happen in erroneous input. */ |
6213 | break; |
6214 | else |
6215 | current = get_containing_scope (current); |
6216 | } |
6217 | |
6218 | /* Check that the parms are used in the appropriate qualifying scopes |
6219 | in the declarator. */ |
6220 | if (!comp_template_args |
6221 | (TI_ARGS (tinfo), |
6222 | TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl))))) |
6223 | { |
6224 | auto_diagnostic_group d; |
6225 | error ("template arguments to %qD do not match original " |
6226 | "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl)); |
6227 | if (!uses_template_parms (TI_ARGS (tinfo))) |
6228 | inform (input_location, "use %<template<>%> for" |
6229 | " an explicit specialization"); |
6230 | /* Avoid crash in import_export_decl. */ |
6231 | DECL_INTERFACE_KNOWN (decl) = 1; |
6232 | return error_mark_node; |
6233 | } |
6234 | |
6235 | /* Check that the constraints for each enclosing template scope are |
6236 | consistent with the original declarations. */ |
6237 | if (flag_concepts) |
6238 | { |
6239 | tree decl_parms = DECL_TEMPLATE_PARMS (tmpl); |
6240 | tree scope_parms = current_template_parms; |
6241 | if (PRIMARY_TEMPLATE_P (tmpl)) |
6242 | { |
6243 | decl_parms = TREE_CHAIN (decl_parms); |
6244 | scope_parms = TREE_CHAIN (scope_parms); |
6245 | } |
6246 | while (decl_parms) |
6247 | { |
6248 | if (!template_requirements_equivalent_p (parms1: decl_parms, parms2: scope_parms)) |
6249 | { |
6250 | error ("redeclaration of %qD with different constraints", |
6251 | TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms))); |
6252 | break; |
6253 | } |
6254 | decl_parms = TREE_CHAIN (decl_parms); |
6255 | scope_parms = TREE_CHAIN (scope_parms); |
6256 | } |
6257 | } |
6258 | } |
6259 | |
6260 | gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl); |
6261 | |
6262 | if (new_template_p) |
6263 | { |
6264 | /* Push template declarations for global functions and types. |
6265 | Note that we do not try to push a global template friend |
6266 | declared in a template class; such a thing may well depend on |
6267 | the template parameters of the class and we'll push it when |
6268 | instantiating the befriending class. */ |
6269 | if (!ctx |
6270 | && !(is_friend && template_class_depth (current_class_type) > 0)) |
6271 | { |
6272 | tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend); |
6273 | if (pushed == error_mark_node) |
6274 | return error_mark_node; |
6275 | |
6276 | /* pushdecl may have found an existing template. */ |
6277 | if (pushed != tmpl) |
6278 | { |
6279 | decl = DECL_TEMPLATE_RESULT (pushed); |
6280 | tmpl = NULL_TREE; |
6281 | } |
6282 | } |
6283 | else if (is_friend) |
6284 | { |
6285 | /* Record this decl as belonging to the current class. It's |
6286 | not chained onto anything else. */ |
6287 | DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true; |
6288 | gcc_checking_assert (!DECL_CHAIN (tmpl)); |
6289 | DECL_CHAIN (tmpl) = current_scope (); |
6290 | } |
6291 | } |
6292 | else if (tmpl) |
6293 | /* The type may have been completed, or (erroneously) changed. */ |
6294 | TREE_TYPE (tmpl) = TREE_TYPE (decl); |
6295 | |
6296 | if (tmpl) |
6297 | { |
6298 | if (is_primary) |
6299 | { |
6300 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
6301 | |
6302 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
6303 | |
6304 | /* Give template template parms a DECL_CONTEXT of the template |
6305 | for which they are a parameter. */ |
6306 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
6307 | for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) |
6308 | { |
6309 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
6310 | if (TREE_CODE (parm) == TEMPLATE_DECL) |
6311 | DECL_CONTEXT (parm) = tmpl; |
6312 | } |
6313 | |
6314 | if (TREE_CODE (decl) == TYPE_DECL |
6315 | && TYPE_DECL_ALIAS_P (decl)) |
6316 | { |
6317 | if (tree constr |
6318 | = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl))) |
6319 | { |
6320 | /* ??? Why don't we do this here for all templates? */ |
6321 | constr = build_constraints (constr, NULL_TREE); |
6322 | set_constraints (decl, constr); |
6323 | } |
6324 | } |
6325 | } |
6326 | |
6327 | /* The DECL_TI_ARGS of DECL contains full set of arguments |
6328 | referring wback to its most general template. If TMPL is a |
6329 | specialization, ARGS may only have the innermost set of |
6330 | arguments. Add the missing argument levels if necessary. */ |
6331 | if (DECL_TEMPLATE_INFO (tmpl)) |
6332 | args = add_outermost_template_args (DECL_TI_ARGS (tmpl), extra_args: args); |
6333 | |
6334 | tree info = build_template_info (template_decl: tmpl, template_args: args); |
6335 | |
6336 | if (DECL_IMPLICIT_TYPEDEF_P (decl)) |
6337 | SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info); |
6338 | else |
6339 | { |
6340 | retrofit_lang_decl (decl); |
6341 | DECL_TEMPLATE_INFO (decl) = info; |
6342 | } |
6343 | } |
6344 | |
6345 | if (is_typedef_decl (x: decl) |
6346 | && (dependent_opaque_alias_p (TREE_TYPE (decl)) |
6347 | || dependent_alias_template_spec_p (TREE_TYPE (decl), nt_opaque))) |
6348 | { |
6349 | /* Manually mark such aliases as dependent so that dependent_type_p_r |
6350 | doesn't have to handle them. */ |
6351 | TYPE_DEPENDENT_P_VALID (TREE_TYPE (decl)) = true; |
6352 | TYPE_DEPENDENT_P (TREE_TYPE (decl)) = true; |
6353 | /* The identity of such aliases is hairy; see structural_comptypes. */ |
6354 | SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (decl)); |
6355 | } |
6356 | |
6357 | if (flag_implicit_templates |
6358 | && !is_friend |
6359 | && TREE_PUBLIC (decl) |
6360 | && VAR_OR_FUNCTION_DECL_P (decl)) |
6361 | /* Set DECL_COMDAT on template instantiations; if we force |
6362 | them to be emitted by explicit instantiation, |
6363 | mark_needed will tell cgraph to do the right thing. */ |
6364 | DECL_COMDAT (decl) = true; |
6365 | |
6366 | gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl); |
6367 | |
6368 | return decl; |
6369 | } |
6370 | |
6371 | /* FN is an inheriting constructor that inherits from the constructor |
6372 | template INHERITED; turn FN into a constructor template with a matching |
6373 | template header. */ |
6374 | |
6375 | tree |
6376 | add_inherited_template_parms (tree fn, tree inherited) |
6377 | { |
6378 | tree inner_parms |
6379 | = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited)); |
6380 | inner_parms = copy_node (inner_parms); |
6381 | tree parms |
6382 | = tree_cons (size_int (current_template_depth + 1), |
6383 | inner_parms, current_template_parms); |
6384 | tree tmpl = build_template_decl (decl: fn, parms, /*member*/member_template_p: true); |
6385 | tree args = template_parms_to_args (parms); |
6386 | DECL_TEMPLATE_INFO (fn) = build_template_info (template_decl: tmpl, template_args: args); |
6387 | DECL_ARTIFICIAL (tmpl) = true; |
6388 | DECL_PRIMARY_TEMPLATE (tmpl) = tmpl; |
6389 | return tmpl; |
6390 | } |
6391 | |
6392 | /* Called when a class template TYPE is redeclared with the indicated |
6393 | template PARMS, e.g.: |
6394 | |
6395 | template <class T> struct S; |
6396 | template <class T> struct S {}; */ |
6397 | |
6398 | bool |
6399 | redeclare_class_template (tree type, tree parms, tree cons) |
6400 | { |
6401 | tree tmpl; |
6402 | tree tmpl_parms; |
6403 | int i; |
6404 | |
6405 | if (!TYPE_TEMPLATE_INFO (type)) |
6406 | { |
6407 | error ("%qT is not a template type", type); |
6408 | return false; |
6409 | } |
6410 | |
6411 | tmpl = TYPE_TI_TEMPLATE (type); |
6412 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
6413 | /* The type is nested in some template class. Nothing to worry |
6414 | about here; there are no new template parameters for the nested |
6415 | type. */ |
6416 | return true; |
6417 | |
6418 | if (!parms) |
6419 | { |
6420 | error ("template specifiers not specified in declaration of %qD", |
6421 | tmpl); |
6422 | return false; |
6423 | } |
6424 | |
6425 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
6426 | tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); |
6427 | |
6428 | if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms)) |
6429 | { |
6430 | auto_diagnostic_group d; |
6431 | error_n (input_location, TREE_VEC_LENGTH (parms), |
6432 | "redeclared with %d template parameter", |
6433 | "redeclared with %d template parameters", |
6434 | TREE_VEC_LENGTH (parms)); |
6435 | inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms), |
6436 | "previous declaration %qD used %d template parameter", |
6437 | "previous declaration %qD used %d template parameters", |
6438 | tmpl, TREE_VEC_LENGTH (tmpl_parms)); |
6439 | return false; |
6440 | } |
6441 | |
6442 | for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i) |
6443 | { |
6444 | tree tmpl_parm; |
6445 | tree parm; |
6446 | |
6447 | if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node |
6448 | || TREE_VEC_ELT (parms, i) == error_mark_node) |
6449 | continue; |
6450 | |
6451 | tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i)); |
6452 | if (error_operand_p (t: tmpl_parm)) |
6453 | return false; |
6454 | |
6455 | parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
6456 | |
6457 | /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or |
6458 | TEMPLATE_DECL. */ |
6459 | if (TREE_CODE (tmpl_parm) != TREE_CODE (parm) |
6460 | || (TREE_CODE (tmpl_parm) != TYPE_DECL |
6461 | && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))) |
6462 | || (TREE_CODE (tmpl_parm) != PARM_DECL |
6463 | && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm)) |
6464 | != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))) |
6465 | || (TREE_CODE (tmpl_parm) == PARM_DECL |
6466 | && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm)) |
6467 | != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))) |
6468 | { |
6469 | auto_diagnostic_group d; |
6470 | error ("template parameter %q+#D", tmpl_parm); |
6471 | if (DECL_P (parm)) |
6472 | inform (DECL_SOURCE_LOCATION (parm), "redeclared here as %q#D", parm); |
6473 | else |
6474 | inform (input_location, "redeclared here"); |
6475 | return false; |
6476 | } |
6477 | |
6478 | /* The parameters can be declared to introduce different |
6479 | constraints. */ |
6480 | tree p1 = TREE_VEC_ELT (tmpl_parms, i); |
6481 | tree p2 = TREE_VEC_ELT (parms, i); |
6482 | if (!template_parameter_constraints_equivalent_p (parm1: p1, parm2: p2)) |
6483 | { |
6484 | auto_diagnostic_group d; |
6485 | error ("declaration of template parameter %q+#D with different " |
6486 | "constraints", parm); |
6487 | inform (DECL_SOURCE_LOCATION (tmpl_parm), |
6488 | "original declaration appeared here"); |
6489 | return false; |
6490 | } |
6491 | |
6492 | /* Give each template template parm in this redeclaration a |
6493 | DECL_CONTEXT of the template for which they are a parameter. */ |
6494 | if (TREE_CODE (parm) == TEMPLATE_DECL) |
6495 | { |
6496 | gcc_checking_assert (DECL_CONTEXT (parm) == NULL_TREE |
6497 | || DECL_CONTEXT (parm) == tmpl); |
6498 | DECL_CONTEXT (parm) = tmpl; |
6499 | } |
6500 | } |
6501 | |
6502 | if (!merge_default_template_args (parms, tmpl_parms, /*class_p=*/true)) |
6503 | return false; |
6504 | |
6505 | tree ci = get_constraints (tmpl); |
6506 | tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE; |
6507 | tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE; |
6508 | |
6509 | /* Two classes with different constraints declare different entities. */ |
6510 | if (!cp_tree_equal (req1, req2)) |
6511 | { |
6512 | auto_diagnostic_group d; |
6513 | error_at (input_location, "redeclaration of %q#D with different " |
6514 | "constraints", tmpl); |
6515 | inform (DECL_SOURCE_LOCATION (tmpl), |
6516 | "original declaration appeared here"); |
6517 | return false; |
6518 | } |
6519 | |
6520 | return true; |
6521 | } |
6522 | |
6523 | /* The actual substitution part of instantiate_non_dependent_expr, |
6524 | to be used when the caller has already checked |
6525 | !instantiation_dependent_uneval_expression_p (expr) |
6526 | and cleared processing_template_decl. */ |
6527 | |
6528 | tree |
6529 | instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain) |
6530 | { |
6531 | return tsubst_expr (expr, /*args=*/NULL_TREE, complain, /*in_decl=*/NULL_TREE); |
6532 | } |
6533 | |
6534 | /* Instantiate the non-dependent expression EXPR. */ |
6535 | |
6536 | tree |
6537 | instantiate_non_dependent_expr (tree expr, |
6538 | tsubst_flags_t complain /* = tf_error */) |
6539 | { |
6540 | if (expr == NULL_TREE) |
6541 | return NULL_TREE; |
6542 | |
6543 | if (processing_template_decl) |
6544 | { |
6545 | /* The caller should have checked this already. */ |
6546 | gcc_checking_assert (!instantiation_dependent_uneval_expression_p (expr)); |
6547 | processing_template_decl_sentinel s; |
6548 | expr = instantiate_non_dependent_expr_internal (expr, complain); |
6549 | } |
6550 | return expr; |
6551 | } |
6552 | |
6553 | /* Like instantiate_non_dependent_expr, but return NULL_TREE if the |
6554 | expression is dependent or non-constant. */ |
6555 | |
6556 | tree |
6557 | instantiate_non_dependent_or_null (tree expr) |
6558 | { |
6559 | if (expr == NULL_TREE) |
6560 | return NULL_TREE; |
6561 | if (processing_template_decl) |
6562 | { |
6563 | if (!is_nondependent_constant_expression (expr)) |
6564 | expr = NULL_TREE; |
6565 | else |
6566 | { |
6567 | processing_template_decl_sentinel s; |
6568 | expr = instantiate_non_dependent_expr_internal (expr, complain: tf_error); |
6569 | } |
6570 | } |
6571 | return expr; |
6572 | } |
6573 | |
6574 | /* True iff T is a specialization of a variable template. */ |
6575 | |
6576 | bool |
6577 | variable_template_specialization_p (tree t) |
6578 | { |
6579 | if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t)) |
6580 | return false; |
6581 | tree tmpl = DECL_TI_TEMPLATE (t); |
6582 | return variable_template_p (t: tmpl); |
6583 | } |
6584 | |
6585 | /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias |
6586 | template declaration, or a TYPE_DECL for an alias declaration. */ |
6587 | |
6588 | bool |
6589 | alias_type_or_template_p (tree t) |
6590 | { |
6591 | if (t == NULL_TREE) |
6592 | return false; |
6593 | return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t)) |
6594 | || (TYPE_P (t) |
6595 | && TYPE_NAME (t) |
6596 | && TYPE_DECL_ALIAS_P (TYPE_NAME (t))) |
6597 | || DECL_ALIAS_TEMPLATE_P (t)); |
6598 | } |
6599 | |
6600 | /* If T is a specialization of an alias template, return it; otherwise return |
6601 | NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */ |
6602 | |
6603 | tree |
6604 | alias_template_specialization_p (const_tree t, |
6605 | bool transparent_typedefs) |
6606 | { |
6607 | if (!TYPE_P (t)) |
6608 | return NULL_TREE; |
6609 | |
6610 | /* It's an alias template specialization if it's an alias and its |
6611 | TYPE_NAME is a specialization of a primary template. */ |
6612 | if (typedef_variant_p (type: t)) |
6613 | { |
6614 | if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) |
6615 | if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))) |
6616 | return CONST_CAST_TREE (t); |
6617 | if (transparent_typedefs && !dependent_opaque_alias_p (t)) |
6618 | return alias_template_specialization_p (DECL_ORIGINAL_TYPE |
6619 | (TYPE_NAME (t)), |
6620 | transparent_typedefs); |
6621 | } |
6622 | |
6623 | return NULL_TREE; |
6624 | } |
6625 | |
6626 | /* A cache of the result of complex_alias_template_p. */ |
6627 | |
6628 | static GTY((deletable)) hash_map<const_tree, tree> *complex_alias_tmpl_info; |
6629 | |
6630 | /* Data structure for complex_alias_template_*. */ |
6631 | |
6632 | struct uses_all_template_parms_data |
6633 | { |
6634 | int level; |
6635 | tree *seen; |
6636 | }; |
6637 | |
6638 | /* walk_tree callback for complex_alias_template_p. */ |
6639 | |
6640 | static tree |
6641 | complex_alias_template_r (tree *tp, int *walk_subtrees, void *data_) |
6642 | { |
6643 | tree t = *tp; |
6644 | auto &data = *(struct uses_all_template_parms_data*)data_; |
6645 | |
6646 | switch (TREE_CODE (t)) |
6647 | { |
6648 | case TEMPLATE_TYPE_PARM: |
6649 | case TEMPLATE_PARM_INDEX: |
6650 | case TEMPLATE_TEMPLATE_PARM: |
6651 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
6652 | { |
6653 | tree idx = get_template_parm_index (parm: t); |
6654 | if (TEMPLATE_PARM_LEVEL (idx) == data.level) |
6655 | data.seen[TEMPLATE_PARM_IDX (idx)] = boolean_true_node; |
6656 | } |
6657 | |
6658 | default:; |
6659 | } |
6660 | |
6661 | if (!PACK_EXPANSION_P (t)) |
6662 | return 0; |
6663 | |
6664 | /* An alias template with a pack expansion that expands a pack from the |
6665 | enclosing class needs to be considered complex, to avoid confusion with |
6666 | the same pack being used as an argument to the alias's own template |
6667 | parameter (91966). */ |
6668 | for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; |
6669 | pack = TREE_CHAIN (pack)) |
6670 | { |
6671 | tree parm_pack = TREE_VALUE (pack); |
6672 | if (!TEMPLATE_PARM_P (parm_pack)) |
6673 | continue; |
6674 | int idx, level; |
6675 | template_parm_level_and_index (parm_pack, &level, &idx); |
6676 | if (level < data.level) |
6677 | return t; |
6678 | |
6679 | /* Consider the expanded packs to be used outside the expansion... */ |
6680 | data.seen[idx] = boolean_true_node; |
6681 | } |
6682 | |
6683 | /* ...but don't walk into the pattern. Consider PR104008: |
6684 | |
6685 | template <typename T, typename... Ts> |
6686 | using IsOneOf = disjunction<is_same<T, Ts>...>; |
6687 | |
6688 | where IsOneOf seemingly uses all of its template parameters in its |
6689 | expansion (and does not expand a pack from the enclosing class), so the |
6690 | alias was not marked as complex. However, if it is used like |
6691 | "IsOneOf<T>", the empty pack for Ts means that T no longer appears in the |
6692 | expansion. So only Ts is considered used by the pack expansion. */ |
6693 | *walk_subtrees = false; |
6694 | |
6695 | return 0; |
6696 | } |
6697 | |
6698 | /* An alias template is complex from a SFINAE perspective if a template-id |
6699 | using that alias can be ill-formed when the expansion is not, as with |
6700 | the void_t template. |
6701 | |
6702 | If this predicate returns true in the ordinary case, the out parameter |
6703 | SEEN_OUT is set to a TREE_VEC containing boolean_true_node at element I if |
6704 | the I'th template parameter of the alias template is used in the alias. */ |
6705 | |
6706 | static bool |
6707 | complex_alias_template_p (const_tree tmpl, tree *seen_out) |
6708 | { |
6709 | tmpl = most_general_template (tmpl); |
6710 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
6711 | return false; |
6712 | |
6713 | /* A renaming alias isn't complex. */ |
6714 | if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl) |
6715 | return false; |
6716 | |
6717 | /* Any other constrained alias is complex. */ |
6718 | if (get_constraints (tmpl)) |
6719 | return true; |
6720 | |
6721 | /* An alias with dependent type attributes is complex. */ |
6722 | if (dependent_opaque_alias_p (TREE_TYPE (tmpl))) |
6723 | return true; |
6724 | |
6725 | if (!complex_alias_tmpl_info) |
6726 | complex_alias_tmpl_info = hash_map<const_tree, tree>::create_ggc (size: 13); |
6727 | |
6728 | if (tree *slot = complex_alias_tmpl_info->get (k: tmpl)) |
6729 | { |
6730 | tree result = *slot; |
6731 | if (result == boolean_false_node) |
6732 | return false; |
6733 | if (result == boolean_true_node) |
6734 | return true; |
6735 | gcc_assert (TREE_CODE (result) == TREE_VEC); |
6736 | if (seen_out) |
6737 | *seen_out = result; |
6738 | return true; |
6739 | } |
6740 | |
6741 | struct uses_all_template_parms_data data; |
6742 | tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); |
6743 | tree parms = DECL_TEMPLATE_PARMS (tmpl); |
6744 | data.level = TMPL_PARMS_DEPTH (parms); |
6745 | int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms)); |
6746 | tree seen = make_tree_vec (len); |
6747 | data.seen = TREE_VEC_BEGIN (seen); |
6748 | for (int i = 0; i < len; ++i) |
6749 | data.seen[i] = boolean_false_node; |
6750 | |
6751 | if (cp_walk_tree_without_duplicates (&pat, complex_alias_template_r, &data)) |
6752 | { |
6753 | complex_alias_tmpl_info->put (k: tmpl, boolean_true_node); |
6754 | return true; |
6755 | } |
6756 | |
6757 | for (int i = 0; i < len; ++i) |
6758 | if (data.seen[i] != boolean_true_node) |
6759 | { |
6760 | complex_alias_tmpl_info->put (k: tmpl, v: seen); |
6761 | if (seen_out) |
6762 | *seen_out = seen; |
6763 | return true; |
6764 | } |
6765 | |
6766 | complex_alias_tmpl_info->put (k: tmpl, boolean_false_node); |
6767 | return false; |
6768 | } |
6769 | |
6770 | /* If T is a specialization of a complex alias template with a dependent |
6771 | argument for an unused template parameter, return it; otherwise return |
6772 | NULL_TREE. If T is a typedef to such a specialization, return the |
6773 | specialization. This predicate is usually checked alongside |
6774 | dependent_opaque_alias_p. Whereas dependent_opaque_alias_p checks |
6775 | type equivalence of an alias vs its expansion, this predicate more |
6776 | broadly checks SFINAE equivalence. */ |
6777 | |
6778 | tree |
6779 | dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs) |
6780 | { |
6781 | if (t == error_mark_node) |
6782 | return NULL_TREE; |
6783 | gcc_assert (TYPE_P (t)); |
6784 | |
6785 | if (!processing_template_decl || !typedef_variant_p (type: t)) |
6786 | return NULL_TREE; |
6787 | |
6788 | if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) |
6789 | { |
6790 | tree seen = NULL_TREE; |
6791 | if (complex_alias_template_p (TI_TEMPLATE (tinfo), seen_out: &seen)) |
6792 | { |
6793 | tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)); |
6794 | if (!seen) |
6795 | { |
6796 | if (any_dependent_template_arguments_p (args)) |
6797 | return CONST_CAST_TREE (t); |
6798 | } |
6799 | else |
6800 | { |
6801 | gcc_assert (TREE_VEC_LENGTH (args) == TREE_VEC_LENGTH (seen)); |
6802 | for (int i = 0, len = TREE_VEC_LENGTH (args); i < len; ++i) |
6803 | if (TREE_VEC_ELT (seen, i) != boolean_true_node |
6804 | && dependent_template_arg_p (TREE_VEC_ELT (args, i))) |
6805 | return CONST_CAST_TREE (t); |
6806 | } |
6807 | |
6808 | return NULL_TREE; |
6809 | } |
6810 | } |
6811 | |
6812 | if (transparent_typedefs && !dependent_opaque_alias_p (t)) |
6813 | { |
6814 | tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t)); |
6815 | return dependent_alias_template_spec_p (t: utype, transparent_typedefs); |
6816 | } |
6817 | |
6818 | return NULL_TREE; |
6819 | } |
6820 | |
6821 | /* Return true if substituting into T would yield a different type than |
6822 | substituting into its expansion. This predicate is usually checked |
6823 | alongside dependent_alias_template_spec_p. */ |
6824 | |
6825 | bool |
6826 | dependent_opaque_alias_p (const_tree t) |
6827 | { |
6828 | return (TYPE_P (t) |
6829 | && typedef_variant_p (type: t) |
6830 | && (any_dependent_type_attributes_p (DECL_ATTRIBUTES |
6831 | (TYPE_NAME (t))) |
6832 | /* Treat a dependent decltype(lambda) alias as opaque so that we |
6833 | don't prematurely strip it when used as a template argument. |
6834 | Otherwise substitution into each occurrence of the (stripped) |
6835 | alias would incorrectly yield a distinct lambda type. */ |
6836 | || (TREE_CODE (t) == DECLTYPE_TYPE |
6837 | && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == LAMBDA_EXPR |
6838 | && !typedef_variant_p (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))))); |
6839 | } |
6840 | |
6841 | /* Return the number of innermost template parameters in TMPL. */ |
6842 | |
6843 | static int |
6844 | num_innermost_template_parms (const_tree tmpl) |
6845 | { |
6846 | tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl)); |
6847 | return TREE_VEC_LENGTH (parms); |
6848 | } |
6849 | |
6850 | /* Return either TMPL or another template that it is equivalent to under DR |
6851 | 1286: An alias that just changes the name of a template is equivalent to |
6852 | the other template. */ |
6853 | |
6854 | static tree |
6855 | get_underlying_template (tree tmpl) |
6856 | { |
6857 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
6858 | while (DECL_ALIAS_TEMPLATE_P (tmpl)) |
6859 | { |
6860 | /* Determine if the alias is equivalent to an underlying template. */ |
6861 | tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); |
6862 | /* The underlying type may have been ill-formed. Don't proceed. */ |
6863 | if (!orig_type) |
6864 | break; |
6865 | tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type); |
6866 | if (!tinfo) |
6867 | break; |
6868 | |
6869 | tree underlying = TI_TEMPLATE (tinfo); |
6870 | if (!PRIMARY_TEMPLATE_P (underlying) |
6871 | || (num_innermost_template_parms (tmpl) |
6872 | != num_innermost_template_parms (tmpl: underlying))) |
6873 | break; |
6874 | |
6875 | /* Does the alias add cv-quals? */ |
6876 | if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl))) |
6877 | break; |
6878 | |
6879 | tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl)); |
6880 | if (!comp_template_args (TI_ARGS (tinfo), alias_args)) |
6881 | break; |
6882 | |
6883 | /* Are any default template arguments equivalent? */ |
6884 | tree aparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl)); |
6885 | tree uparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (underlying)); |
6886 | const int nparms = TREE_VEC_LENGTH (aparms); |
6887 | for (int i = 0; i < nparms; ++i) |
6888 | { |
6889 | tree adefarg = TREE_PURPOSE (TREE_VEC_ELT (aparms, i)); |
6890 | tree udefarg = TREE_PURPOSE (TREE_VEC_ELT (uparms, i)); |
6891 | if (!template_args_equal (adefarg, udefarg)) |
6892 | goto top_break; |
6893 | } |
6894 | |
6895 | /* If TMPL adds or changes any constraints, it isn't equivalent. I think |
6896 | it's appropriate to treat a less-constrained alias as equivalent. */ |
6897 | if (!at_least_as_constrained (underlying, tmpl)) |
6898 | break; |
6899 | |
6900 | /* If TMPL adds dependent type attributes, it isn't equivalent. */ |
6901 | if (dependent_opaque_alias_p (TREE_TYPE (tmpl))) |
6902 | break; |
6903 | |
6904 | /* Alias is equivalent. Strip it and repeat. */ |
6905 | tmpl = underlying; |
6906 | } |
6907 | top_break:; |
6908 | |
6909 | return tmpl; |
6910 | } |
6911 | |
6912 | /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which |
6913 | must be a reference-to-function or a pointer-to-function type, as specified |
6914 | in [temp.arg.nontype]: disambiguate EXPR if it is an overload set, |
6915 | and check that the resulting function has external linkage. */ |
6916 | |
6917 | static tree |
6918 | convert_nontype_argument_function (tree type, tree expr, |
6919 | tsubst_flags_t complain) |
6920 | { |
6921 | tree fns = expr; |
6922 | tree fn, fn_no_ptr; |
6923 | linkage_kind linkage; |
6924 | |
6925 | fn = instantiate_type (type, fns, tf_none); |
6926 | if (fn == error_mark_node) |
6927 | return error_mark_node; |
6928 | |
6929 | if (value_dependent_expression_p (fn)) |
6930 | goto accept; |
6931 | |
6932 | fn_no_ptr = fn; |
6933 | if (REFERENCE_REF_P (fn_no_ptr)) |
6934 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); |
6935 | fn_no_ptr = strip_fnptr_conv (fn_no_ptr); |
6936 | if (TREE_CODE (fn_no_ptr) == ADDR_EXPR) |
6937 | fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0); |
6938 | if (BASELINK_P (fn_no_ptr)) |
6939 | fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr); |
6940 | |
6941 | /* [temp.arg.nontype]/1 |
6942 | |
6943 | A template-argument for a non-type, non-template template-parameter |
6944 | shall be one of: |
6945 | [...] |
6946 | -- the address of an object or function with external [C++11: or |
6947 | internal] linkage. */ |
6948 | |
6949 | STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr); |
6950 | if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL) |
6951 | { |
6952 | if (complain & tf_error) |
6953 | { |
6954 | auto_diagnostic_group d; |
6955 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
6956 | error_at (loc, "%qE is not a valid template argument for type %qT", |
6957 | expr, type); |
6958 | if (TYPE_PTR_P (type)) |
6959 | inform (loc, "it must be the address of a function " |
6960 | "with external linkage"); |
6961 | else |
6962 | inform (loc, "it must be the name of a function with " |
6963 | "external linkage"); |
6964 | } |
6965 | return NULL_TREE; |
6966 | } |
6967 | |
6968 | linkage = decl_linkage (fn_no_ptr); |
6969 | if ((cxx_dialect < cxx11 && linkage != lk_external) |
6970 | || (cxx_dialect < cxx17 && linkage == lk_none)) |
6971 | { |
6972 | if (complain & tf_error) |
6973 | { |
6974 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
6975 | if (cxx_dialect >= cxx11) |
6976 | error_at (loc, "%qE is not a valid template argument for type " |
6977 | "%qT because %qD has no linkage", |
6978 | expr, type, fn_no_ptr); |
6979 | else |
6980 | error_at (loc, "%qE is not a valid template argument for type " |
6981 | "%qT because %qD does not have external linkage", |
6982 | expr, type, fn_no_ptr); |
6983 | } |
6984 | return NULL_TREE; |
6985 | } |
6986 | |
6987 | accept: |
6988 | if (TYPE_REF_P (type)) |
6989 | { |
6990 | if (REFERENCE_REF_P (fn)) |
6991 | fn = TREE_OPERAND (fn, 0); |
6992 | else |
6993 | fn = build_address (fn); |
6994 | } |
6995 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn))) |
6996 | fn = build_nop (type, fn); |
6997 | |
6998 | return fn; |
6999 | } |
7000 | |
7001 | /* Subroutine of convert_nontype_argument. |
7002 | Check if EXPR of type TYPE is a valid pointer-to-member constant. |
7003 | Emit an error otherwise. */ |
7004 | |
7005 | static bool |
7006 | check_valid_ptrmem_cst_expr (tree type, tree expr, |
7007 | tsubst_flags_t complain) |
7008 | { |
7009 | tree orig_expr = expr; |
7010 | STRIP_NOPS (expr); |
7011 | if (null_ptr_cst_p (expr)) |
7012 | return true; |
7013 | if (TREE_CODE (expr) == PTRMEM_CST |
7014 | && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type), |
7015 | PTRMEM_CST_CLASS (expr))) |
7016 | return true; |
7017 | if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr)) |
7018 | return true; |
7019 | if (processing_template_decl |
7020 | && TREE_CODE (expr) == ADDR_EXPR |
7021 | && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF) |
7022 | return true; |
7023 | if (complain & tf_error) |
7024 | { |
7025 | auto_diagnostic_group d; |
7026 | location_t loc = cp_expr_loc_or_input_loc (t: orig_expr); |
7027 | error_at (loc, "%qE is not a valid template argument for type %qT", |
7028 | orig_expr, type); |
7029 | if (TREE_CODE (expr) != PTRMEM_CST) |
7030 | inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>"); |
7031 | else |
7032 | inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr)); |
7033 | } |
7034 | return false; |
7035 | } |
7036 | |
7037 | /* Returns TRUE iff the address of OP is value-dependent. |
7038 | |
7039 | 14.6.2.4 [temp.dep.temp]: |
7040 | A non-integral non-type template-argument is dependent if its type is |
7041 | dependent or it has either of the following forms |
7042 | qualified-id |
7043 | & qualified-id |
7044 | and contains a nested-name-specifier which specifies a class-name that |
7045 | names a dependent type. |
7046 | |
7047 | We generalize this to just say that the address of a member of a |
7048 | dependent class is value-dependent; the above doesn't cover the |
7049 | address of a static data member named with an unqualified-id. */ |
7050 | |
7051 | static bool |
7052 | has_value_dependent_address (tree op) |
7053 | { |
7054 | STRIP_ANY_LOCATION_WRAPPER (op); |
7055 | |
7056 | /* We could use get_inner_reference here, but there's no need; |
7057 | this is only relevant for template non-type arguments, which |
7058 | can only be expressed as &id-expression. */ |
7059 | if (DECL_P (op)) |
7060 | { |
7061 | tree ctx = CP_DECL_CONTEXT (op); |
7062 | |
7063 | if (TYPE_P (ctx) && dependent_type_p (ctx)) |
7064 | return true; |
7065 | |
7066 | if (VAR_P (op) |
7067 | && TREE_STATIC (op) |
7068 | && TREE_CODE (ctx) == FUNCTION_DECL |
7069 | && type_dependent_expression_p (ctx)) |
7070 | return true; |
7071 | } |
7072 | |
7073 | return false; |
7074 | } |
7075 | |
7076 | /* The next set of functions are used for providing helpful explanatory |
7077 | diagnostics for failed overload resolution. Their messages should be |
7078 | indented by two spaces for consistency with the messages in |
7079 | call.cc */ |
7080 | |
7081 | static int |
7082 | unify_success (bool /*explain_p*/) |
7083 | { |
7084 | return 0; |
7085 | } |
7086 | |
7087 | /* Other failure functions should call this one, to provide a single function |
7088 | for setting a breakpoint on. */ |
7089 | |
7090 | static int |
7091 | unify_invalid (bool /*explain_p*/) |
7092 | { |
7093 | return 1; |
7094 | } |
7095 | |
7096 | static int |
7097 | unify_parameter_deduction_failure (bool explain_p, tree parm) |
7098 | { |
7099 | if (explain_p) |
7100 | inform (input_location, |
7101 | " couldn%'t deduce template parameter %qD", parm); |
7102 | return unify_invalid (explain_p); |
7103 | } |
7104 | |
7105 | static int |
7106 | unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg) |
7107 | { |
7108 | if (explain_p) |
7109 | inform (input_location, |
7110 | " types %qT and %qT have incompatible cv-qualifiers", |
7111 | parm, arg); |
7112 | return unify_invalid (explain_p); |
7113 | } |
7114 | |
7115 | static int |
7116 | unify_type_mismatch (bool explain_p, tree parm, tree arg) |
7117 | { |
7118 | if (explain_p) |
7119 | inform (input_location, " mismatched types %qT and %qT", parm, arg); |
7120 | return unify_invalid (explain_p); |
7121 | } |
7122 | |
7123 | static int |
7124 | unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg) |
7125 | { |
7126 | if (explain_p) |
7127 | inform (input_location, |
7128 | " template parameter %qD is not a parameter pack, but " |
7129 | "argument %qD is", |
7130 | parm, arg); |
7131 | return unify_invalid (explain_p); |
7132 | } |
7133 | |
7134 | static int |
7135 | unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg) |
7136 | { |
7137 | if (explain_p) |
7138 | inform (input_location, |
7139 | " template argument %qE does not match " |
7140 | "pointer-to-member constant %qE", |
7141 | arg, parm); |
7142 | return unify_invalid (explain_p); |
7143 | } |
7144 | |
7145 | static int |
7146 | unify_expression_unequal (bool explain_p, tree parm, tree arg) |
7147 | { |
7148 | if (explain_p) |
7149 | inform (input_location, " %qE is not equivalent to %qE", parm, arg); |
7150 | return unify_invalid (explain_p); |
7151 | } |
7152 | |
7153 | static int |
7154 | unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg) |
7155 | { |
7156 | if (explain_p) |
7157 | inform (input_location, |
7158 | " inconsistent parameter pack deduction with %qT and %qT", |
7159 | old_arg, new_arg); |
7160 | return unify_invalid (explain_p); |
7161 | } |
7162 | |
7163 | static int |
7164 | unify_inconsistency (bool explain_p, tree parm, tree first, tree second) |
7165 | { |
7166 | if (explain_p) |
7167 | { |
7168 | if (TYPE_P (parm)) |
7169 | inform (input_location, |
7170 | " deduced conflicting types for parameter %qT (%qT and %qT)", |
7171 | parm, first, second); |
7172 | else |
7173 | inform (input_location, |
7174 | " deduced conflicting values for non-type parameter " |
7175 | "%qE (%qE and %qE)", parm, first, second); |
7176 | } |
7177 | return unify_invalid (explain_p); |
7178 | } |
7179 | |
7180 | static int |
7181 | unify_vla_arg (bool explain_p, tree arg) |
7182 | { |
7183 | if (explain_p) |
7184 | inform (input_location, |
7185 | " variable-sized array type %qT is not " |
7186 | "a valid template argument", |
7187 | arg); |
7188 | return unify_invalid (explain_p); |
7189 | } |
7190 | |
7191 | static int |
7192 | unify_method_type_error (bool explain_p, tree arg) |
7193 | { |
7194 | if (explain_p) |
7195 | inform (input_location, |
7196 | " member function type %qT is not a valid template argument", |
7197 | arg); |
7198 | return unify_invalid (explain_p); |
7199 | } |
7200 | |
7201 | static int |
7202 | unify_arity (bool explain_p, int have, int wanted, bool least_p = false) |
7203 | { |
7204 | if (explain_p) |
7205 | { |
7206 | if (least_p) |
7207 | inform_n (input_location, wanted, |
7208 | " candidate expects at least %d argument, %d provided", |
7209 | " candidate expects at least %d arguments, %d provided", |
7210 | wanted, have); |
7211 | else |
7212 | inform_n (input_location, wanted, |
7213 | " candidate expects %d argument, %d provided", |
7214 | " candidate expects %d arguments, %d provided", |
7215 | wanted, have); |
7216 | } |
7217 | return unify_invalid (explain_p); |
7218 | } |
7219 | |
7220 | static int |
7221 | unify_too_many_arguments (bool explain_p, int have, int wanted) |
7222 | { |
7223 | return unify_arity (explain_p, have, wanted); |
7224 | } |
7225 | |
7226 | static int |
7227 | unify_too_few_arguments (bool explain_p, int have, int wanted, |
7228 | bool least_p = false) |
7229 | { |
7230 | return unify_arity (explain_p, have, wanted, least_p); |
7231 | } |
7232 | |
7233 | static int |
7234 | unify_arg_conversion (bool explain_p, tree to_type, |
7235 | tree from_type, tree arg) |
7236 | { |
7237 | if (explain_p) |
7238 | inform (cp_expr_loc_or_input_loc (t: arg), |
7239 | " cannot convert %qE (type %qT) to type %qT", |
7240 | arg, from_type, to_type); |
7241 | return unify_invalid (explain_p); |
7242 | } |
7243 | |
7244 | static int |
7245 | unify_no_common_base (bool explain_p, enum template_base_result r, |
7246 | tree parm, tree arg) |
7247 | { |
7248 | if (explain_p) |
7249 | switch (r) |
7250 | { |
7251 | case tbr_ambiguous_baseclass: |
7252 | inform (input_location, " %qT is an ambiguous base class of %qT", |
7253 | parm, arg); |
7254 | break; |
7255 | default: |
7256 | inform (input_location, " %qT is not derived from %qT", arg, parm); |
7257 | break; |
7258 | } |
7259 | return unify_invalid (explain_p); |
7260 | } |
7261 | |
7262 | static int |
7263 | unify_inconsistent_template_template_parameters (bool explain_p) |
7264 | { |
7265 | if (explain_p) |
7266 | inform (input_location, |
7267 | " template parameters of a template template argument are " |
7268 | "inconsistent with other deduced template arguments"); |
7269 | return unify_invalid (explain_p); |
7270 | } |
7271 | |
7272 | static int |
7273 | unify_template_deduction_failure (bool explain_p, tree parm, tree arg) |
7274 | { |
7275 | if (explain_p) |
7276 | inform (input_location, |
7277 | " cannot deduce a template for %qT from non-template type %qT", |
7278 | parm, arg); |
7279 | return unify_invalid (explain_p); |
7280 | } |
7281 | |
7282 | static int |
7283 | unify_template_argument_mismatch (bool explain_p, tree parm, tree arg) |
7284 | { |
7285 | if (explain_p) |
7286 | inform (input_location, |
7287 | " template argument %qE does not match %qE", arg, parm); |
7288 | return unify_invalid (explain_p); |
7289 | } |
7290 | |
7291 | /* Subroutine of convert_nontype_argument, to check whether EXPR, as an |
7292 | argument for TYPE, points to an unsuitable object. |
7293 | |
7294 | Also adjust the type of the index in C++20 array subobject references. */ |
7295 | |
7296 | static bool |
7297 | invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain) |
7298 | { |
7299 | switch (TREE_CODE (expr)) |
7300 | { |
7301 | CASE_CONVERT: |
7302 | return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0), |
7303 | complain); |
7304 | |
7305 | case TARGET_EXPR: |
7306 | return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr), |
7307 | complain); |
7308 | |
7309 | case CONSTRUCTOR: |
7310 | { |
7311 | for (auto &e: CONSTRUCTOR_ELTS (expr)) |
7312 | if (invalid_tparm_referent_p (TREE_TYPE (e.value), expr: e.value, complain)) |
7313 | return true; |
7314 | } |
7315 | break; |
7316 | |
7317 | case ADDR_EXPR: |
7318 | { |
7319 | tree decl = TREE_OPERAND (expr, 0); |
7320 | |
7321 | if (cxx_dialect >= cxx20) |
7322 | while (TREE_CODE (decl) == COMPONENT_REF |
7323 | || TREE_CODE (decl) == ARRAY_REF) |
7324 | { |
7325 | tree &op = TREE_OPERAND (decl, 1); |
7326 | if (TREE_CODE (decl) == ARRAY_REF |
7327 | && TREE_CODE (op) == INTEGER_CST) |
7328 | /* Canonicalize array offsets to ptrdiff_t; how they were |
7329 | written doesn't matter for subobject identity. */ |
7330 | op = fold_convert (ptrdiff_type_node, op); |
7331 | decl = TREE_OPERAND (decl, 0); |
7332 | } |
7333 | |
7334 | if (!VAR_OR_FUNCTION_DECL_P (decl)) |
7335 | { |
7336 | if (complain & tf_error) |
7337 | error_at (cp_expr_loc_or_input_loc (t: expr), |
7338 | "%qE is not a valid template argument of type %qT " |
7339 | "because %qE is not a variable or function", |
7340 | expr, type, decl); |
7341 | return true; |
7342 | } |
7343 | else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl)) |
7344 | { |
7345 | if (complain & tf_error) |
7346 | error_at (cp_expr_loc_or_input_loc (t: expr), |
7347 | "%qE is not a valid template argument of type %qT " |
7348 | "in C++98 because %qD does not have external linkage", |
7349 | expr, type, decl); |
7350 | return true; |
7351 | } |
7352 | else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17) |
7353 | && decl_linkage (decl) == lk_none) |
7354 | { |
7355 | if (complain & tf_error) |
7356 | error_at (cp_expr_loc_or_input_loc (t: expr), |
7357 | "%qE is not a valid template argument of type %qT " |
7358 | "because %qD has no linkage", expr, type, decl); |
7359 | return true; |
7360 | } |
7361 | /* C++17: For a non-type template-parameter of reference or pointer |
7362 | type, the value of the constant expression shall not refer to (or |
7363 | for a pointer type, shall not be the address of): |
7364 | * a subobject (4.5), |
7365 | * a temporary object (15.2), |
7366 | * a string literal (5.13.5), |
7367 | * the result of a typeid expression (8.2.8), or |
7368 | * a predefined __func__ variable (11.4.1). */ |
7369 | else if (VAR_P (decl) && DECL_ARTIFICIAL (decl) |
7370 | && !DECL_NTTP_OBJECT_P (decl)) |
7371 | { |
7372 | gcc_checking_assert (DECL_TINFO_P (decl) || DECL_FNAME_P (decl)); |
7373 | if (complain & tf_error) |
7374 | error ("the address of %qD is not a valid template argument", |
7375 | decl); |
7376 | return true; |
7377 | } |
7378 | else if (cxx_dialect < cxx20 |
7379 | && !(same_type_ignoring_top_level_qualifiers_p |
7380 | (strip_array_types (TREE_TYPE (type)), |
7381 | strip_array_types (TREE_TYPE (decl))))) |
7382 | { |
7383 | if (complain & tf_error) |
7384 | error ("the address of the %qT subobject of %qD is not a " |
7385 | "valid template argument", TREE_TYPE (type), decl); |
7386 | return true; |
7387 | } |
7388 | else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl)) |
7389 | { |
7390 | if (complain & tf_error) |
7391 | error ("the address of %qD is not a valid template argument " |
7392 | "because it does not have static storage duration", |
7393 | decl); |
7394 | return true; |
7395 | } |
7396 | } |
7397 | break; |
7398 | |
7399 | default: |
7400 | if (!INDIRECT_TYPE_P (type)) |
7401 | /* We're only concerned about pointers and references here. */; |
7402 | else if (cxx_dialect >= cxx11 && integer_zerop (expr)) |
7403 | /* Null pointer values are OK in C++11. */; |
7404 | else |
7405 | { |
7406 | if (VAR_P (expr)) |
7407 | { |
7408 | if (complain & tf_error) |
7409 | error ("%qD is not a valid template argument " |
7410 | "because %qD is a variable, not the address of " |
7411 | "a variable", expr, expr); |
7412 | return true; |
7413 | } |
7414 | else |
7415 | { |
7416 | if (complain & tf_error) |
7417 | error ("%qE is not a valid template argument for %qT " |
7418 | "because it is not the address of a variable", |
7419 | expr, type); |
7420 | return true; |
7421 | } |
7422 | } |
7423 | } |
7424 | return false; |
7425 | |
7426 | } |
7427 | |
7428 | /* Return a VAR_DECL for the C++20 template parameter object corresponding to |
7429 | template argument EXPR. */ |
7430 | |
7431 | static tree |
7432 | create_template_parm_object (tree expr, tsubst_flags_t complain) |
7433 | { |
7434 | tree orig = expr; |
7435 | if (TREE_CODE (expr) == TARGET_EXPR) |
7436 | expr = TARGET_EXPR_INITIAL (expr); |
7437 | |
7438 | if (!TREE_CONSTANT (expr)) |
7439 | { |
7440 | if ((complain & tf_error) |
7441 | && require_rvalue_constant_expression (orig)) |
7442 | cxx_constant_value (orig); |
7443 | return error_mark_node; |
7444 | } |
7445 | if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain)) |
7446 | return error_mark_node; |
7447 | |
7448 | /* This is no longer a compound literal. */ |
7449 | gcc_assert (!TREE_HAS_CONSTRUCTOR (expr)); |
7450 | |
7451 | return get_template_parm_object (expr, mangle: mangle_template_parm_object (expr)); |
7452 | } |
7453 | |
7454 | /* The template arguments corresponding to template parameter objects of types |
7455 | that contain pointers to members. */ |
7456 | |
7457 | static GTY(()) hash_map<tree, tree> *tparm_obj_values; |
7458 | |
7459 | /* Find or build an nttp object for (already-validated) EXPR with name |
7460 | NAME. When CHECK_INIT is false we don't need to process the initialiser, |
7461 | it's already been done. */ |
7462 | |
7463 | tree |
7464 | get_template_parm_object (tree expr, tree name, bool check_init/*=true*/) |
7465 | { |
7466 | tree decl = get_global_binding (id: name); |
7467 | if (decl) |
7468 | return decl; |
7469 | |
7470 | tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST); |
7471 | decl = create_temporary_var (type); |
7472 | DECL_NTTP_OBJECT_P (decl) = true; |
7473 | DECL_CONTEXT (decl) = NULL_TREE; |
7474 | TREE_STATIC (decl) = true; |
7475 | DECL_DECLARED_CONSTEXPR_P (decl) = true; |
7476 | TREE_READONLY (decl) = true; |
7477 | DECL_NAME (decl) = name; |
7478 | SET_DECL_ASSEMBLER_NAME (decl, name); |
7479 | comdat_linkage (decl); |
7480 | |
7481 | if (!zero_init_p (type)) |
7482 | { |
7483 | /* If EXPR contains any PTRMEM_CST, they will get clobbered by |
7484 | lower_var_init before we're done mangling. So store the original |
7485 | value elsewhere. We only need to unshare EXPR if it's not yet |
7486 | been processed. */ |
7487 | tree copy = check_init ? unshare_constructor (expr) : expr; |
7488 | hash_map_safe_put<hm_ggc> (h&: tparm_obj_values, k: decl, v: copy); |
7489 | } |
7490 | |
7491 | if (!check_init) |
7492 | { |
7493 | /* The EXPR is the already processed initializer, set it on the NTTP |
7494 | object now so that cp_finish_decl doesn't do it again later. */ |
7495 | gcc_checking_assert (reduced_constant_expression_p (expr)); |
7496 | DECL_INITIAL (decl) = expr; |
7497 | DECL_INITIALIZED_P (decl) = true; |
7498 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; |
7499 | /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ |
7500 | if (!TYPE_REF_P (type)) |
7501 | TREE_CONSTANT (decl) = true; |
7502 | } |
7503 | |
7504 | pushdecl_top_level_and_finish (decl, expr); |
7505 | |
7506 | return decl; |
7507 | } |
7508 | |
7509 | /* Return the actual template argument corresponding to template parameter |
7510 | object VAR. */ |
7511 | |
7512 | tree |
7513 | tparm_object_argument (tree var) |
7514 | { |
7515 | if (zero_init_p (TREE_TYPE (var))) |
7516 | return DECL_INITIAL (var); |
7517 | return *(tparm_obj_values->get (k: var)); |
7518 | } |
7519 | |
7520 | /* Attempt to convert the non-type template parameter EXPR to the |
7521 | indicated TYPE. If the conversion is successful, return the |
7522 | converted value. If the conversion is unsuccessful, return |
7523 | NULL_TREE if we issued an error message, or error_mark_node if we |
7524 | did not. We issue error messages for out-and-out bad template |
7525 | parameters, but not simply because the conversion failed, since we |
7526 | might be just trying to do argument deduction. Both TYPE and EXPR |
7527 | must be non-dependent. |
7528 | |
7529 | The conversion follows the special rules described in |
7530 | [temp.arg.nontype], and it is much more strict than an implicit |
7531 | conversion. |
7532 | |
7533 | This function is called twice for each template argument (see |
7534 | lookup_template_class for a more accurate description of this |
7535 | problem). This means that we need to handle expressions which |
7536 | are not valid in a C++ source, but can be created from the |
7537 | first call (for instance, casts to perform conversions). These |
7538 | hacks can go away after we fix the double coercion problem. */ |
7539 | |
7540 | static tree |
7541 | convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) |
7542 | { |
7543 | tree expr_type; |
7544 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
7545 | |
7546 | /* Detect immediately string literals as invalid non-type argument. |
7547 | This special-case is not needed for correctness (we would easily |
7548 | catch this later), but only to provide better diagnostic for this |
7549 | common user mistake. As suggested by DR 100, we do not mention |
7550 | linkage issues in the diagnostic as this is not the point. */ |
7551 | if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type)) |
7552 | { |
7553 | if (complain & tf_error) |
7554 | error ("%qE is not a valid template argument for type %qT " |
7555 | "because string literals can never be used in this context", |
7556 | expr, type); |
7557 | return NULL_TREE; |
7558 | } |
7559 | |
7560 | /* Add the ADDR_EXPR now for the benefit of |
7561 | value_dependent_expression_p. */ |
7562 | if (TYPE_PTROBV_P (type) |
7563 | && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE) |
7564 | { |
7565 | expr = decay_conversion (expr, complain); |
7566 | if (expr == error_mark_node) |
7567 | return error_mark_node; |
7568 | } |
7569 | |
7570 | /* If we are in a template, EXPR may be non-dependent, but still |
7571 | have a syntactic, rather than semantic, form. For example, EXPR |
7572 | might be a SCOPE_REF, rather than the VAR_DECL to which the |
7573 | SCOPE_REF refers. Preserving the qualifying scope is necessary |
7574 | so that access checking can be performed when the template is |
7575 | instantiated -- but here we need the resolved form so that we can |
7576 | convert the argument. */ |
7577 | bool non_dep = false; |
7578 | if (TYPE_REF_OBJ_P (type) |
7579 | && has_value_dependent_address (op: expr)) |
7580 | /* If we want the address and it's value-dependent, don't fold. */; |
7581 | else if (processing_template_decl |
7582 | && !instantiation_dependent_expression_p (expr)) |
7583 | non_dep = true; |
7584 | if (error_operand_p (t: expr)) |
7585 | return error_mark_node; |
7586 | expr_type = TREE_TYPE (expr); |
7587 | |
7588 | /* If the argument is non-dependent, perform any conversions in |
7589 | non-dependent context as well. */ |
7590 | processing_template_decl_sentinel s (non_dep); |
7591 | if (non_dep) |
7592 | expr = instantiate_non_dependent_expr_internal (expr, complain); |
7593 | |
7594 | bool val_dep_p = value_dependent_expression_p (expr); |
7595 | if (val_dep_p) |
7596 | expr = canonicalize_expr_argument (expr, complain); |
7597 | else |
7598 | STRIP_ANY_LOCATION_WRAPPER (expr); |
7599 | |
7600 | /* 14.3.2/5: The null pointer{,-to-member} conversion is applied |
7601 | to a non-type argument of "nullptr". */ |
7602 | if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type)) |
7603 | expr = fold_simple (convert (type, expr)); |
7604 | |
7605 | /* In C++11, integral or enumeration non-type template arguments can be |
7606 | arbitrary constant expressions. Pointer and pointer to |
7607 | member arguments can be general constant expressions that evaluate |
7608 | to a null value, but otherwise still need to be of a specific form. */ |
7609 | if (cxx_dialect >= cxx11) |
7610 | { |
7611 | if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type)) |
7612 | /* A PTRMEM_CST is already constant, and a valid template |
7613 | argument for a parameter of pointer to member type, we just want |
7614 | to leave it in that form rather than lower it to a |
7615 | CONSTRUCTOR. */; |
7616 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
7617 | || cxx_dialect >= cxx17) |
7618 | { |
7619 | /* C++17: A template-argument for a non-type template-parameter shall |
7620 | be a converted constant expression (8.20) of the type of the |
7621 | template-parameter. */ |
7622 | expr = build_converted_constant_expr (type, expr, complain); |
7623 | if (expr == error_mark_node) |
7624 | /* Make sure we return NULL_TREE only if we have really issued |
7625 | an error, as described above. */ |
7626 | return (complain & tf_error) ? NULL_TREE : error_mark_node; |
7627 | else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR) |
7628 | { |
7629 | IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true; |
7630 | return expr; |
7631 | } |
7632 | expr = maybe_constant_value (expr, NULL_TREE, mce_true); |
7633 | expr = convert_from_reference (expr); |
7634 | /* EXPR may have become value-dependent. */ |
7635 | val_dep_p = value_dependent_expression_p (expr); |
7636 | } |
7637 | else if (TYPE_PTR_OR_PTRMEM_P (type)) |
7638 | { |
7639 | tree folded = maybe_constant_value (expr, NULL_TREE, mce_true); |
7640 | if (TYPE_PTR_P (type) ? integer_zerop (folded) |
7641 | : null_member_pointer_value_p (folded)) |
7642 | expr = folded; |
7643 | } |
7644 | } |
7645 | |
7646 | if (TYPE_REF_P (type)) |
7647 | expr = mark_lvalue_use (expr); |
7648 | else |
7649 | expr = mark_rvalue_use (expr); |
7650 | |
7651 | /* HACK: Due to double coercion, we can get a |
7652 | NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here, |
7653 | which is the tree that we built on the first call (see |
7654 | below when coercing to reference to object or to reference to |
7655 | function). We just strip everything and get to the arg. |
7656 | See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C |
7657 | for examples. */ |
7658 | if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type)) |
7659 | { |
7660 | /* Check this before we strip *& to avoid redundancy. */ |
7661 | if (!mark_single_function (expr, complain)) |
7662 | return error_mark_node; |
7663 | |
7664 | tree probe_type, probe = expr; |
7665 | if (REFERENCE_REF_P (probe)) |
7666 | probe = TREE_OPERAND (probe, 0); |
7667 | probe_type = TREE_TYPE (probe); |
7668 | if (TREE_CODE (probe) == NOP_EXPR) |
7669 | { |
7670 | /* ??? Maybe we could use convert_from_reference here, but we |
7671 | would need to relax its constraints because the NOP_EXPR |
7672 | could actually change the type to something more cv-qualified, |
7673 | and this is not folded by convert_from_reference. */ |
7674 | tree addr = TREE_OPERAND (probe, 0); |
7675 | if (TYPE_REF_P (probe_type) |
7676 | && TREE_CODE (addr) == ADDR_EXPR |
7677 | && TYPE_PTR_P (TREE_TYPE (addr)) |
7678 | && (same_type_ignoring_top_level_qualifiers_p |
7679 | (TREE_TYPE (probe_type), |
7680 | TREE_TYPE (TREE_TYPE (addr))))) |
7681 | { |
7682 | expr = TREE_OPERAND (addr, 0); |
7683 | expr_type = TREE_TYPE (probe_type); |
7684 | } |
7685 | } |
7686 | } |
7687 | |
7688 | /* [temp.arg.nontype]/5, bullet 1 |
7689 | |
7690 | For a non-type template-parameter of integral or enumeration type, |
7691 | integral promotions (_conv.prom_) and integral conversions |
7692 | (_conv.integral_) are applied. */ |
7693 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type) |
7694 | || SCALAR_FLOAT_TYPE_P (type)) |
7695 | { |
7696 | if (cxx_dialect < cxx11) |
7697 | { |
7698 | tree t = build_converted_constant_expr (type, expr, complain); |
7699 | t = maybe_constant_value (t); |
7700 | if (t != error_mark_node) |
7701 | expr = t; |
7702 | } |
7703 | |
7704 | if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr))) |
7705 | return error_mark_node; |
7706 | |
7707 | /* Notice that there are constant expressions like '4 % 0' which |
7708 | do not fold into integer constants. */ |
7709 | if (!CONSTANT_CLASS_P (expr) && !val_dep_p) |
7710 | { |
7711 | if (complain & tf_error) |
7712 | { |
7713 | int errs = errorcount, warns = warningcount + werrorcount; |
7714 | if (!require_potential_constant_expression (expr)) |
7715 | expr = error_mark_node; |
7716 | else |
7717 | expr = cxx_constant_value (expr); |
7718 | if (errorcount > errs || warningcount + werrorcount > warns) |
7719 | inform (loc, "in template argument for type %qT", type); |
7720 | if (expr == error_mark_node) |
7721 | return NULL_TREE; |
7722 | /* else cxx_constant_value complained but gave us |
7723 | a real constant, so go ahead. */ |
7724 | if (!CONSTANT_CLASS_P (expr)) |
7725 | { |
7726 | /* Some assemble time constant expressions like |
7727 | (intptr_t)&&lab1 - (intptr_t)&&lab2 or |
7728 | 4 + (intptr_t)&&var satisfy reduced_constant_expression_p |
7729 | as we can emit them into .rodata initializers of |
7730 | variables, yet they can't fold into an INTEGER_CST at |
7731 | compile time. Refuse them here. */ |
7732 | gcc_checking_assert (reduced_constant_expression_p (expr)); |
7733 | error_at (loc, "template argument %qE for type %qT not " |
7734 | "a compile-time constant", expr, type); |
7735 | return NULL_TREE; |
7736 | } |
7737 | } |
7738 | else |
7739 | return NULL_TREE; |
7740 | } |
7741 | |
7742 | /* Avoid typedef problems. */ |
7743 | if (TREE_TYPE (expr) != type) |
7744 | expr = fold_convert (type, expr); |
7745 | } |
7746 | /* [temp.arg.nontype]/5, bullet 2 |
7747 | |
7748 | For a non-type template-parameter of type pointer to object, |
7749 | qualification conversions (_conv.qual_) and the array-to-pointer |
7750 | conversion (_conv.array_) are applied. */ |
7751 | else if (TYPE_PTROBV_P (type)) |
7752 | { |
7753 | tree decayed = expr; |
7754 | |
7755 | /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from |
7756 | decay_conversion or an explicit cast. If it's a problematic cast, |
7757 | we'll complain about it below. */ |
7758 | if (TREE_CODE (expr) == NOP_EXPR) |
7759 | { |
7760 | tree probe = expr; |
7761 | STRIP_NOPS (probe); |
7762 | if (TREE_CODE (probe) == ADDR_EXPR |
7763 | && TYPE_PTR_P (TREE_TYPE (probe))) |
7764 | { |
7765 | expr = probe; |
7766 | expr_type = TREE_TYPE (expr); |
7767 | } |
7768 | } |
7769 | |
7770 | /* [temp.arg.nontype]/1 (TC1 version, DR 49): |
7771 | |
7772 | A template-argument for a non-type, non-template template-parameter |
7773 | shall be one of: [...] |
7774 | |
7775 | -- the name of a non-type template-parameter; |
7776 | -- the address of an object or function with external linkage, [...] |
7777 | expressed as "& id-expression" where the & is optional if the name |
7778 | refers to a function or array, or if the corresponding |
7779 | template-parameter is a reference. |
7780 | |
7781 | Here, we do not care about functions, as they are invalid anyway |
7782 | for a parameter of type pointer-to-object. */ |
7783 | |
7784 | if (val_dep_p) |
7785 | /* Non-type template parameters are OK. */ |
7786 | ; |
7787 | else if (cxx_dialect >= cxx11 && integer_zerop (expr)) |
7788 | /* Null pointer values are OK in C++11. */; |
7789 | else if (TREE_CODE (expr) != ADDR_EXPR |
7790 | && !INDIRECT_TYPE_P (expr_type)) |
7791 | /* Other values, like integer constants, might be valid |
7792 | non-type arguments of some other type. */ |
7793 | return error_mark_node; |
7794 | else if (invalid_tparm_referent_p (type, expr, complain)) |
7795 | return NULL_TREE; |
7796 | |
7797 | expr = decayed; |
7798 | |
7799 | expr = perform_qualification_conversions (type, expr); |
7800 | if (expr == error_mark_node) |
7801 | return error_mark_node; |
7802 | } |
7803 | /* [temp.arg.nontype]/5, bullet 3 |
7804 | |
7805 | For a non-type template-parameter of type reference to object, no |
7806 | conversions apply. The type referred to by the reference may be more |
7807 | cv-qualified than the (otherwise identical) type of the |
7808 | template-argument. The template-parameter is bound directly to the |
7809 | template-argument, which must be an lvalue. */ |
7810 | else if (TYPE_REF_OBJ_P (type)) |
7811 | { |
7812 | if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type), |
7813 | expr_type)) |
7814 | return error_mark_node; |
7815 | |
7816 | if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type)) |
7817 | { |
7818 | if (complain & tf_error) |
7819 | error ("%qE is not a valid template argument for type %qT " |
7820 | "because of conflicts in cv-qualification", expr, type); |
7821 | return NULL_TREE; |
7822 | } |
7823 | |
7824 | if (!lvalue_p (expr)) |
7825 | { |
7826 | if (complain & tf_error) |
7827 | error ("%qE is not a valid template argument for type %qT " |
7828 | "because it is not an lvalue", expr, type); |
7829 | return NULL_TREE; |
7830 | } |
7831 | |
7832 | /* [temp.arg.nontype]/1 |
7833 | |
7834 | A template-argument for a non-type, non-template template-parameter |
7835 | shall be one of: [...] |
7836 | |
7837 | -- the address of an object or function with external linkage. */ |
7838 | if (INDIRECT_REF_P (expr) |
7839 | && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0)))) |
7840 | { |
7841 | expr = TREE_OPERAND (expr, 0); |
7842 | if (DECL_P (expr)) |
7843 | { |
7844 | if (complain & tf_error) |
7845 | error ("%q#D is not a valid template argument for type %qT " |
7846 | "because a reference variable does not have a constant " |
7847 | "address", expr, type); |
7848 | return NULL_TREE; |
7849 | } |
7850 | } |
7851 | |
7852 | if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p) |
7853 | /* OK, dependent reference. We don't want to ask whether a DECL is |
7854 | itself value-dependent, since what we want here is its address. */; |
7855 | else |
7856 | { |
7857 | expr = build_address (expr); |
7858 | |
7859 | if (invalid_tparm_referent_p (type, expr, complain)) |
7860 | return NULL_TREE; |
7861 | } |
7862 | |
7863 | if (!same_type_p (type, TREE_TYPE (expr))) |
7864 | expr = build_nop (type, expr); |
7865 | } |
7866 | /* [temp.arg.nontype]/5, bullet 4 |
7867 | |
7868 | For a non-type template-parameter of type pointer to function, only |
7869 | the function-to-pointer conversion (_conv.func_) is applied. If the |
7870 | template-argument represents a set of overloaded functions (or a |
7871 | pointer to such), the matching function is selected from the set |
7872 | (_over.over_). */ |
7873 | else if (TYPE_PTRFN_P (type)) |
7874 | { |
7875 | /* If the argument is a template-id, we might not have enough |
7876 | context information to decay the pointer. */ |
7877 | if (!type_unknown_p (expr: expr_type)) |
7878 | { |
7879 | expr = decay_conversion (expr, complain); |
7880 | if (expr == error_mark_node) |
7881 | return error_mark_node; |
7882 | } |
7883 | |
7884 | if (cxx_dialect >= cxx11 && integer_zerop (expr)) |
7885 | /* Null pointer values are OK in C++11. */ |
7886 | return perform_qualification_conversions (type, expr); |
7887 | |
7888 | expr = convert_nontype_argument_function (type, expr, complain); |
7889 | if (!expr || expr == error_mark_node) |
7890 | return expr; |
7891 | } |
7892 | /* [temp.arg.nontype]/5, bullet 5 |
7893 | |
7894 | For a non-type template-parameter of type reference to function, no |
7895 | conversions apply. If the template-argument represents a set of |
7896 | overloaded functions, the matching function is selected from the set |
7897 | (_over.over_). */ |
7898 | else if (TYPE_REFFN_P (type)) |
7899 | { |
7900 | if (TREE_CODE (expr) == ADDR_EXPR) |
7901 | { |
7902 | if (complain & tf_error) |
7903 | { |
7904 | auto_diagnostic_group d; |
7905 | error ("%qE is not a valid template argument for type %qT " |
7906 | "because it is a pointer", expr, type); |
7907 | inform (input_location, "try using %qE instead", |
7908 | TREE_OPERAND (expr, 0)); |
7909 | } |
7910 | return NULL_TREE; |
7911 | } |
7912 | |
7913 | expr = convert_nontype_argument_function (type, expr, complain); |
7914 | if (!expr || expr == error_mark_node) |
7915 | return expr; |
7916 | } |
7917 | /* [temp.arg.nontype]/5, bullet 6 |
7918 | |
7919 | For a non-type template-parameter of type pointer to member function, |
7920 | no conversions apply. If the template-argument represents a set of |
7921 | overloaded member functions, the matching member function is selected |
7922 | from the set (_over.over_). */ |
7923 | else if (TYPE_PTRMEMFUNC_P (type)) |
7924 | { |
7925 | expr = instantiate_type (type, expr, tf_none); |
7926 | if (expr == error_mark_node) |
7927 | return error_mark_node; |
7928 | |
7929 | /* [temp.arg.nontype] bullet 1 says the pointer to member |
7930 | expression must be a pointer-to-member constant. */ |
7931 | if (!val_dep_p |
7932 | && !check_valid_ptrmem_cst_expr (type, expr, complain)) |
7933 | return NULL_TREE; |
7934 | |
7935 | /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST |
7936 | into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */ |
7937 | if (fnptr_conv_p (type, TREE_TYPE (expr))) |
7938 | expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr)); |
7939 | } |
7940 | /* [temp.arg.nontype]/5, bullet 7 |
7941 | |
7942 | For a non-type template-parameter of type pointer to data member, |
7943 | qualification conversions (_conv.qual_) are applied. */ |
7944 | else if (TYPE_PTRDATAMEM_P (type)) |
7945 | { |
7946 | /* [temp.arg.nontype] bullet 1 says the pointer to member |
7947 | expression must be a pointer-to-member constant. */ |
7948 | if (!val_dep_p |
7949 | && !check_valid_ptrmem_cst_expr (type, expr, complain)) |
7950 | return NULL_TREE; |
7951 | |
7952 | expr = perform_qualification_conversions (type, expr); |
7953 | if (expr == error_mark_node) |
7954 | return expr; |
7955 | } |
7956 | else if (NULLPTR_TYPE_P (type)) |
7957 | { |
7958 | if (!NULLPTR_TYPE_P (TREE_TYPE (expr))) |
7959 | { |
7960 | if (complain & tf_error) |
7961 | error ("%qE is not a valid template argument for type %qT " |
7962 | "because it is of type %qT", expr, type, TREE_TYPE (expr)); |
7963 | return NULL_TREE; |
7964 | } |
7965 | return expr; |
7966 | } |
7967 | else if (CLASS_TYPE_P (type)) |
7968 | { |
7969 | /* Replace the argument with a reference to the corresponding template |
7970 | parameter object. */ |
7971 | if (!val_dep_p) |
7972 | expr = create_template_parm_object (expr, complain); |
7973 | if (expr == error_mark_node) |
7974 | return NULL_TREE; |
7975 | } |
7976 | /* A template non-type parameter must be one of the above. */ |
7977 | else |
7978 | gcc_unreachable (); |
7979 | |
7980 | /* Sanity check: did we actually convert the argument to the |
7981 | right type? */ |
7982 | gcc_assert (same_type_ignoring_top_level_qualifiers_p |
7983 | (type, TREE_TYPE (expr))); |
7984 | return convert_from_reference (expr); |
7985 | } |
7986 | |
7987 | /* Subroutine of coerce_template_template_parms, which returns true if |
7988 | PARM and ARG match using the rule for the template parameters of |
7989 | template template parameters. Both PARM and ARG are template parameters; |
7990 | the rest of the arguments are the same as for |
7991 | coerce_template_template_parms. */ |
7992 | |
7993 | static bool |
7994 | coerce_template_template_parm (tree parm, tree arg, tsubst_flags_t complain, |
7995 | tree in_decl, tree outer_args) |
7996 | { |
7997 | if (arg == NULL_TREE || error_operand_p (t: arg) |
7998 | || parm == NULL_TREE || error_operand_p (t: parm)) |
7999 | return false; |
8000 | |
8001 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
8002 | return false; |
8003 | |
8004 | switch (TREE_CODE (parm)) |
8005 | { |
8006 | case TEMPLATE_DECL: |
8007 | /* We encounter instantiations of templates like |
8008 | template <template <template <class> class> class TT> |
8009 | class C; */ |
8010 | { |
8011 | if (!coerce_template_template_parms |
8012 | (parm, arg, complain, in_decl, outer_args)) |
8013 | return false; |
8014 | } |
8015 | /* Fall through. */ |
8016 | |
8017 | case TYPE_DECL: |
8018 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg)) |
8019 | && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) |
8020 | /* Argument is a parameter pack but parameter is not. */ |
8021 | return false; |
8022 | break; |
8023 | |
8024 | case PARM_DECL: |
8025 | /* The tsubst call is used to handle cases such as |
8026 | |
8027 | template <int> class C {}; |
8028 | template <class T, template <T> class TT> class D {}; |
8029 | D<int, C> d; |
8030 | |
8031 | i.e. the parameter list of TT depends on earlier parameters. */ |
8032 | if (!uses_template_parms (TREE_TYPE (arg))) |
8033 | { |
8034 | ++processing_template_decl; |
8035 | tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl); |
8036 | --processing_template_decl; |
8037 | if (!uses_template_parms (t) |
8038 | && !same_type_p (t, TREE_TYPE (arg))) |
8039 | return false; |
8040 | } |
8041 | |
8042 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg)) |
8043 | && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) |
8044 | /* Argument is a parameter pack but parameter is not. */ |
8045 | return false; |
8046 | |
8047 | break; |
8048 | |
8049 | default: |
8050 | gcc_unreachable (); |
8051 | } |
8052 | |
8053 | return true; |
8054 | } |
8055 | |
8056 | /* Coerce template argument list ARGLIST for use with template |
8057 | template-parameter TEMPL. */ |
8058 | |
8059 | static tree |
8060 | coerce_template_args_for_ttp (tree templ, tree arglist, |
8061 | tsubst_flags_t complain) |
8062 | { |
8063 | /* Consider an example where a template template parameter declared as |
8064 | |
8065 | template <class T, class U = std::allocator<T> > class TT |
8066 | |
8067 | The template parameter level of T and U are one level larger than |
8068 | of TT. To proper process the default argument of U, say when an |
8069 | instantiation `TT<int>' is seen, we need to build the full |
8070 | arguments containing {int} as the innermost level. Outer levels, |
8071 | available when not appearing as default template argument, can be |
8072 | obtained from the arguments of the enclosing template. |
8073 | |
8074 | Suppose that TT is later substituted with std::vector. The above |
8075 | instantiation is `TT<int, std::allocator<T> >' with TT at |
8076 | level 1, and T at level 2, while the template arguments at level 1 |
8077 | becomes {std::vector} and the inner level 2 is {int}. */ |
8078 | |
8079 | tree outer = DECL_CONTEXT (templ); |
8080 | if (outer) |
8081 | outer = generic_targs_for (tmpl: outer); |
8082 | else if (current_template_parms) |
8083 | { |
8084 | /* This is an argument of the current template, so we haven't set |
8085 | DECL_CONTEXT yet. We can also get here when level-lowering a |
8086 | bound ttp. */ |
8087 | tree relevant_template_parms; |
8088 | |
8089 | /* Parameter levels that are greater than the level of the given |
8090 | template template parm are irrelevant. */ |
8091 | relevant_template_parms = current_template_parms; |
8092 | while (TMPL_PARMS_DEPTH (relevant_template_parms) |
8093 | != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ))) |
8094 | relevant_template_parms = TREE_CHAIN (relevant_template_parms); |
8095 | |
8096 | outer = template_parms_to_args (parms: relevant_template_parms); |
8097 | } |
8098 | |
8099 | if (outer) |
8100 | arglist = add_to_template_args (args: outer, extra_args: arglist); |
8101 | |
8102 | tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ); |
8103 | return coerce_template_parms (parmlist, arglist, templ, complain); |
8104 | } |
8105 | |
8106 | /* A cache of template template parameters with match-all default |
8107 | arguments. */ |
8108 | static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache; |
8109 | |
8110 | /* T is a bound template template-parameter. Copy its arguments into default |
8111 | arguments of the template template-parameter's template parameters. */ |
8112 | |
8113 | static tree |
8114 | add_defaults_to_ttp (tree otmpl) |
8115 | { |
8116 | if (tree *c = hash_map_safe_get (h: defaulted_ttp_cache, k: otmpl)) |
8117 | return *c; |
8118 | |
8119 | tree ntmpl = copy_node (otmpl); |
8120 | |
8121 | tree ntype = copy_node (TREE_TYPE (otmpl)); |
8122 | TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl; |
8123 | TYPE_MAIN_VARIANT (ntype) = ntype; |
8124 | TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE; |
8125 | TYPE_NAME (ntype) = ntmpl; |
8126 | SET_TYPE_STRUCTURAL_EQUALITY (ntype); |
8127 | |
8128 | tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype) |
8129 | = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype)); |
8130 | TEMPLATE_PARM_DECL (idx) = ntmpl; |
8131 | TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype; |
8132 | |
8133 | tree oparms = DECL_TEMPLATE_PARMS (otmpl); |
8134 | tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms); |
8135 | TREE_CHAIN (parms) = TREE_CHAIN (oparms); |
8136 | tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms)); |
8137 | for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i) |
8138 | { |
8139 | tree o = TREE_VEC_ELT (vec, i); |
8140 | if (!template_parameter_pack_p (TREE_VALUE (o))) |
8141 | { |
8142 | tree n = TREE_VEC_ELT (vec, i) = copy_node (o); |
8143 | TREE_PURPOSE (n) = any_targ_node; |
8144 | } |
8145 | } |
8146 | |
8147 | tree oresult = DECL_TEMPLATE_RESULT (otmpl); |
8148 | tree gen_otmpl = DECL_TI_TEMPLATE (oresult); |
8149 | tree gen_ntmpl; |
8150 | if (gen_otmpl == otmpl) |
8151 | gen_ntmpl = ntmpl; |
8152 | else |
8153 | gen_ntmpl = add_defaults_to_ttp (otmpl: gen_otmpl); |
8154 | |
8155 | tree nresult = copy_decl (oresult); |
8156 | DECL_TEMPLATE_INFO (nresult) |
8157 | = build_template_info (template_decl: gen_ntmpl, TI_ARGS (DECL_TEMPLATE_INFO (oresult))); |
8158 | DECL_TEMPLATE_RESULT (ntmpl) = nresult; |
8159 | |
8160 | hash_map_safe_put<hm_ggc> (h&: defaulted_ttp_cache, k: otmpl, v: ntmpl); |
8161 | return ntmpl; |
8162 | } |
8163 | |
8164 | /* ARG is a bound potential template template-argument, and PARGS is a list |
8165 | of arguments for the corresponding template template-parameter. Adjust |
8166 | PARGS as appropriate for application to ARG's template, and if ARG is a |
8167 | BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template |
8168 | arguments to the template template parameter. */ |
8169 | |
8170 | static tree |
8171 | coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain) |
8172 | { |
8173 | ++processing_template_decl; |
8174 | tree arg_tmpl = TYPE_TI_TEMPLATE (arg); |
8175 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl)) |
8176 | { |
8177 | /* When comparing two template template-parameters in partial ordering, |
8178 | rewrite the one currently being used as an argument to have default |
8179 | arguments for all parameters. */ |
8180 | arg_tmpl = add_defaults_to_ttp (otmpl: arg_tmpl); |
8181 | pargs = coerce_template_args_for_ttp (templ: arg_tmpl, arglist: pargs, complain); |
8182 | if (pargs != error_mark_node) |
8183 | arg = bind_template_template_parm (TREE_TYPE (arg_tmpl), |
8184 | TYPE_TI_ARGS (arg)); |
8185 | } |
8186 | else |
8187 | { |
8188 | tree aparms |
8189 | = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl)); |
8190 | pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain); |
8191 | } |
8192 | --processing_template_decl; |
8193 | return pargs; |
8194 | } |
8195 | |
8196 | /* Subroutine of unify for the case when PARM is a |
8197 | BOUND_TEMPLATE_TEMPLATE_PARM. */ |
8198 | |
8199 | static int |
8200 | unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg, |
8201 | bool explain_p) |
8202 | { |
8203 | tree parmvec = TYPE_TI_ARGS (parm); |
8204 | tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg)); |
8205 | |
8206 | /* The template template parm might be variadic and the argument |
8207 | not, so flatten both argument lists. */ |
8208 | parmvec = expand_template_argument_pack (args: parmvec); |
8209 | argvec = expand_template_argument_pack (args: argvec); |
8210 | |
8211 | if (flag_new_ttp) |
8212 | { |
8213 | /* In keeping with P0522R0, adjust P's template arguments |
8214 | to apply to A's template; then flatten it again. */ |
8215 | tree nparmvec = coerce_ttp_args_for_tta (arg, pargs: parmvec, complain: tf_none); |
8216 | nparmvec = expand_template_argument_pack (args: nparmvec); |
8217 | |
8218 | if (unify (tparms, targs, nparmvec, argvec, |
8219 | UNIFY_ALLOW_NONE, explain_p)) |
8220 | return 1; |
8221 | |
8222 | /* If the P0522 adjustment eliminated a pack expansion, deduce |
8223 | empty packs. */ |
8224 | if (flag_new_ttp |
8225 | && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec) |
8226 | && unify_pack_expansion (tparms, targs, parmvec, argvec, |
8227 | DEDUCE_EXACT, /*sub*/true, explain_p)) |
8228 | return 1; |
8229 | } |
8230 | else |
8231 | { |
8232 | /* Deduce arguments T, i from TT<T> or TT<i>. |
8233 | We check each element of PARMVEC and ARGVEC individually |
8234 | rather than the whole TREE_VEC since they can have |
8235 | different number of elements, which is allowed under N2555. */ |
8236 | |
8237 | int len = TREE_VEC_LENGTH (parmvec); |
8238 | |
8239 | /* Check if the parameters end in a pack, making them |
8240 | variadic. */ |
8241 | int parm_variadic_p = 0; |
8242 | if (len > 0 |
8243 | && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1))) |
8244 | parm_variadic_p = 1; |
8245 | |
8246 | for (int i = 0; i < len - parm_variadic_p; ++i) |
8247 | /* If the template argument list of P contains a pack |
8248 | expansion that is not the last template argument, the |
8249 | entire template argument list is a non-deduced |
8250 | context. */ |
8251 | if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i))) |
8252 | return unify_success (explain_p); |
8253 | |
8254 | if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p) |
8255 | return unify_too_few_arguments (explain_p, |
8256 | TREE_VEC_LENGTH (argvec), wanted: len); |
8257 | |
8258 | for (int i = 0; i < len - parm_variadic_p; ++i) |
8259 | if (unify (tparms, targs, |
8260 | TREE_VEC_ELT (parmvec, i), |
8261 | TREE_VEC_ELT (argvec, i), |
8262 | UNIFY_ALLOW_NONE, explain_p)) |
8263 | return 1; |
8264 | |
8265 | if (parm_variadic_p |
8266 | && unify_pack_expansion (tparms, targs, |
8267 | parmvec, argvec, |
8268 | DEDUCE_EXACT, |
8269 | /*subr=*/true, explain_p)) |
8270 | return 1; |
8271 | } |
8272 | |
8273 | return 0; |
8274 | } |
8275 | |
8276 | /* Return 1 if PARM_TMPL and ARG_TMPL match using rule for |
8277 | template template parameters. |
8278 | |
8279 | Consider the example: |
8280 | template <class T> class A; |
8281 | template<template <class U> class TT> class B; |
8282 | |
8283 | For B<A>, PARM_TMPL is TT, while ARG_TMPL is A, |
8284 | and OUTER_ARGS contains A. */ |
8285 | |
8286 | static int |
8287 | coerce_template_template_parms (tree parm_tmpl, |
8288 | tree arg_tmpl, |
8289 | tsubst_flags_t complain, |
8290 | tree in_decl, |
8291 | tree outer_args) |
8292 | { |
8293 | int nparms, nargs, i; |
8294 | tree parm, arg; |
8295 | int variadic_p = 0; |
8296 | |
8297 | tree parm_parms = DECL_INNERMOST_TEMPLATE_PARMS (parm_tmpl); |
8298 | tree arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (arg_tmpl); |
8299 | tree gen_arg_tmpl = most_general_template (arg_tmpl); |
8300 | tree gen_arg_parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_arg_tmpl); |
8301 | |
8302 | nparms = TREE_VEC_LENGTH (parm_parms); |
8303 | nargs = TREE_VEC_LENGTH (arg_parms); |
8304 | |
8305 | if (flag_new_ttp) |
8306 | { |
8307 | /* P0522R0: A template template-parameter P is at least as specialized as |
8308 | a template template-argument A if, given the following rewrite to two |
8309 | function templates, the function template corresponding to P is at |
8310 | least as specialized as the function template corresponding to A |
8311 | according to the partial ordering rules for function templates |
8312 | ([temp.func.order]). Given an invented class template X with the |
8313 | template parameter list of A (including default arguments): |
8314 | |
8315 | * Each of the two function templates has the same template parameters, |
8316 | respectively, as P or A. |
8317 | |
8318 | * Each function template has a single function parameter whose type is |
8319 | a specialization of X with template arguments corresponding to the |
8320 | template parameters from the respective function template where, for |
8321 | each template parameter PP in the template parameter list of the |
8322 | function template, a corresponding template argument AA is formed. If |
8323 | PP declares a parameter pack, then AA is the pack expansion |
8324 | PP... ([temp.variadic]); otherwise, AA is the id-expression PP. |
8325 | |
8326 | If the rewrite produces an invalid type, then P is not at least as |
8327 | specialized as A. */ |
8328 | |
8329 | /* So coerce P's args to apply to A's parms, and then deduce between A's |
8330 | args and the converted args. If that succeeds, A is at least as |
8331 | specialized as P, so they match.*/ |
8332 | processing_template_decl_sentinel ptds (/*reset*/false); |
8333 | ++processing_template_decl; |
8334 | |
8335 | tree pargs = template_parms_level_to_args (parms: parm_parms); |
8336 | |
8337 | /* PARM and ARG might be at different template depths, and we want to |
8338 | pass the right additional levels of args when coercing PARGS to |
8339 | ARG_PARMS in case we need to do any substitution into non-type |
8340 | template parameter types. |
8341 | |
8342 | OUTER_ARGS are not the right outer levels in this case, as they are |
8343 | the args we're building up for PARM, and for the coercion we want the |
8344 | args for ARG. If DECL_CONTEXT isn't set for a template template |
8345 | parameter, we can assume that it's in the current scope. */ |
8346 | tree ctx = DECL_CONTEXT (arg_tmpl); |
8347 | if (!ctx && DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl)) |
8348 | ctx = current_scope (); |
8349 | tree scope_args = NULL_TREE; |
8350 | if (tree tinfo = get_template_info (t: ctx)) |
8351 | scope_args = TI_ARGS (tinfo); |
8352 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl)) |
8353 | { |
8354 | int level = TEMPLATE_TYPE_LEVEL (TREE_TYPE (gen_arg_tmpl)); |
8355 | int scope_depth = TMPL_ARGS_DEPTH (scope_args); |
8356 | tree full_pargs = make_tree_vec (level + 1); |
8357 | |
8358 | /* Only use as many levels from the scope as needed |
8359 | (excluding the level of ARG). */ |
8360 | for (int i = 0; i < level - 1; ++i) |
8361 | if (i < scope_depth) |
8362 | TREE_VEC_ELT (full_pargs, i) = TMPL_ARGS_LEVEL (scope_args, i + 1); |
8363 | else |
8364 | TREE_VEC_ELT (full_pargs, i) = make_tree_vec (0); |
8365 | |
8366 | /* Add the arguments that appear at the levels of ARG. */ |
8367 | tree adjacent = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (arg_tmpl)); |
8368 | adjacent = TMPL_ARGS_LEVEL (adjacent, TMPL_ARGS_DEPTH (adjacent) - 1); |
8369 | TREE_VEC_ELT (full_pargs, level - 1) = adjacent; |
8370 | |
8371 | TREE_VEC_ELT (full_pargs, level) = pargs; |
8372 | pargs = full_pargs; |
8373 | } |
8374 | else |
8375 | pargs = add_to_template_args (args: scope_args, extra_args: pargs); |
8376 | |
8377 | pargs = coerce_template_parms (gen_arg_parms, pargs, |
8378 | NULL_TREE, tf_none); |
8379 | if (pargs != error_mark_node) |
8380 | { |
8381 | tree targs = make_tree_vec (nargs); |
8382 | tree aargs = template_parms_level_to_args (parms: arg_parms); |
8383 | if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE, |
8384 | /*explain*/false)) |
8385 | return 1; |
8386 | } |
8387 | } |
8388 | |
8389 | /* Determine whether we have a parameter pack at the end of the |
8390 | template template parameter's template parameter list. */ |
8391 | if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node) |
8392 | { |
8393 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1)); |
8394 | |
8395 | if (error_operand_p (t: parm)) |
8396 | return 0; |
8397 | |
8398 | switch (TREE_CODE (parm)) |
8399 | { |
8400 | case TEMPLATE_DECL: |
8401 | case TYPE_DECL: |
8402 | if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))) |
8403 | variadic_p = 1; |
8404 | break; |
8405 | |
8406 | case PARM_DECL: |
8407 | if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))) |
8408 | variadic_p = 1; |
8409 | break; |
8410 | |
8411 | default: |
8412 | gcc_unreachable (); |
8413 | } |
8414 | } |
8415 | |
8416 | if (nargs != nparms |
8417 | && !(variadic_p && nargs >= nparms - 1)) |
8418 | return 0; |
8419 | |
8420 | /* Check all of the template parameters except the parameter pack at |
8421 | the end (if any). */ |
8422 | for (i = 0; i < nparms - variadic_p; ++i) |
8423 | { |
8424 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node |
8425 | || TREE_VEC_ELT (arg_parms, i) == error_mark_node) |
8426 | continue; |
8427 | |
8428 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
8429 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); |
8430 | |
8431 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, |
8432 | outer_args)) |
8433 | return 0; |
8434 | |
8435 | } |
8436 | |
8437 | if (variadic_p) |
8438 | { |
8439 | /* Check each of the template parameters in the template |
8440 | argument against the template parameter pack at the end of |
8441 | the template template parameter. */ |
8442 | if (TREE_VEC_ELT (parm_parms, i) == error_mark_node) |
8443 | return 0; |
8444 | |
8445 | parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i)); |
8446 | |
8447 | for (; i < nargs; ++i) |
8448 | { |
8449 | if (TREE_VEC_ELT (arg_parms, i) == error_mark_node) |
8450 | continue; |
8451 | |
8452 | arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i)); |
8453 | |
8454 | if (!coerce_template_template_parm (parm, arg, complain, in_decl, |
8455 | outer_args)) |
8456 | return 0; |
8457 | } |
8458 | } |
8459 | |
8460 | return 1; |
8461 | } |
8462 | |
8463 | /* Verifies that the deduced template arguments (in TARGS) for the |
8464 | template template parameters (in TPARMS) represent valid bindings, |
8465 | by comparing the template parameter list of each template argument |
8466 | to the template parameter list of its corresponding template |
8467 | template parameter, in accordance with DR150. This |
8468 | routine can only be called after all template arguments have been |
8469 | deduced. It will return TRUE if all of the template template |
8470 | parameter bindings are okay, FALSE otherwise. */ |
8471 | bool |
8472 | template_template_parm_bindings_ok_p (tree tparms, tree targs) |
8473 | { |
8474 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
8475 | bool ret = true; |
8476 | |
8477 | /* We're dealing with template parms in this process. */ |
8478 | ++processing_template_decl; |
8479 | |
8480 | targs = INNERMOST_TEMPLATE_ARGS (targs); |
8481 | |
8482 | for (i = 0; i < ntparms; ++i) |
8483 | { |
8484 | tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); |
8485 | tree targ = TREE_VEC_ELT (targs, i); |
8486 | |
8487 | if (TREE_CODE (tparm) == TEMPLATE_DECL && targ) |
8488 | { |
8489 | tree packed_args = NULL_TREE; |
8490 | int idx, len = 1; |
8491 | |
8492 | if (ARGUMENT_PACK_P (targ)) |
8493 | { |
8494 | /* Look inside the argument pack. */ |
8495 | packed_args = ARGUMENT_PACK_ARGS (targ); |
8496 | len = TREE_VEC_LENGTH (packed_args); |
8497 | } |
8498 | |
8499 | for (idx = 0; idx < len; ++idx) |
8500 | { |
8501 | if (packed_args) |
8502 | /* Extract the next argument from the argument |
8503 | pack. */ |
8504 | targ = TREE_VEC_ELT (packed_args, idx); |
8505 | |
8506 | if (PACK_EXPANSION_P (targ)) |
8507 | /* Look at the pattern of the pack expansion. */ |
8508 | targ = PACK_EXPANSION_PATTERN (targ); |
8509 | |
8510 | /* Extract the template parameters from the template |
8511 | argument. */ |
8512 | if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM) |
8513 | targ = TYPE_NAME (targ); |
8514 | |
8515 | /* Verify that we can coerce the template template |
8516 | parameters from the template argument to the template |
8517 | parameter. This requires an exact match. */ |
8518 | if (TREE_CODE (targ) == TEMPLATE_DECL |
8519 | && !coerce_template_template_parms |
8520 | (parm_tmpl: tparm, |
8521 | arg_tmpl: targ, |
8522 | complain: tf_none, |
8523 | in_decl: tparm, |
8524 | outer_args: targs)) |
8525 | { |
8526 | ret = false; |
8527 | goto out; |
8528 | } |
8529 | } |
8530 | } |
8531 | } |
8532 | |
8533 | out: |
8534 | |
8535 | --processing_template_decl; |
8536 | return ret; |
8537 | } |
8538 | |
8539 | /* Since type attributes aren't mangled, we need to strip them from |
8540 | template type arguments. */ |
8541 | |
8542 | tree |
8543 | canonicalize_type_argument (tree arg, tsubst_flags_t complain) |
8544 | { |
8545 | if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg)) |
8546 | return arg; |
8547 | bool removed_attributes = false; |
8548 | tree canon = strip_typedefs (arg, &removed_attributes); |
8549 | if (removed_attributes |
8550 | && (complain & tf_warning)) |
8551 | warning (OPT_Wignored_attributes, |
8552 | "ignoring attributes on template argument %qT", arg); |
8553 | return canon; |
8554 | } |
8555 | |
8556 | /* And from inside dependent non-type arguments like sizeof(Type). */ |
8557 | |
8558 | static tree |
8559 | canonicalize_expr_argument (tree arg, tsubst_flags_t complain) |
8560 | { |
8561 | if (!arg || arg == error_mark_node) |
8562 | return arg; |
8563 | bool removed_attributes = false; |
8564 | tree canon = strip_typedefs_expr (arg, &removed_attributes); |
8565 | if (removed_attributes |
8566 | && (complain & tf_warning)) |
8567 | warning (OPT_Wignored_attributes, |
8568 | "ignoring attributes in template argument %qE", arg); |
8569 | return canon; |
8570 | } |
8571 | |
8572 | /* A template declaration can be substituted for a constrained |
8573 | template template parameter only when the argument is no more |
8574 | constrained than the parameter. */ |
8575 | |
8576 | static bool |
8577 | is_compatible_template_arg (tree parm, tree arg, tree args) |
8578 | { |
8579 | tree parm_cons = get_constraints (parm); |
8580 | |
8581 | /* For now, allow constrained template template arguments |
8582 | and unconstrained template template parameters. */ |
8583 | if (parm_cons == NULL_TREE) |
8584 | return true; |
8585 | |
8586 | /* If the template parameter is constrained, we need to rewrite its |
8587 | constraints in terms of the ARG's template parameters. This ensures |
8588 | that all of the template parameter types will have the same depth. |
8589 | |
8590 | Note that this is only valid when coerce_template_template_parm is |
8591 | true for the innermost template parameters of PARM and ARG. In other |
8592 | words, because coercion is successful, this conversion will be valid. */ |
8593 | tree new_args = NULL_TREE; |
8594 | if (parm_cons) |
8595 | { |
8596 | tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg); |
8597 | new_args = template_parms_level_to_args (parms: aparms); |
8598 | new_args = add_to_template_args (args, extra_args: new_args); |
8599 | ++processing_template_decl; |
8600 | parm_cons = tsubst_constraint_info (parm_cons, new_args, |
8601 | tf_none, NULL_TREE); |
8602 | --processing_template_decl; |
8603 | if (parm_cons == error_mark_node) |
8604 | return false; |
8605 | } |
8606 | |
8607 | return ttp_subsumes (parm_cons, arg); |
8608 | } |
8609 | |
8610 | /* We can't fully resolve ARG given as a non-type template argument to TYPE, |
8611 | because one of them is dependent. But we need to represent the |
8612 | conversion for the benefit of cp_tree_equal. */ |
8613 | |
8614 | static tree |
8615 | maybe_convert_nontype_argument (tree type, tree arg, bool force) |
8616 | { |
8617 | /* Auto parms get no conversion. */ |
8618 | if (type_uses_auto (type)) |
8619 | return arg; |
8620 | /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion? |
8621 | That would complicate other things, and it doesn't seem necessary. */ |
8622 | if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) |
8623 | return arg; |
8624 | /* We don't need or want to add this conversion now if we're going to use the |
8625 | argument for deduction. */ |
8626 | if (!value_dependent_expression_p (arg)) |
8627 | force = false; |
8628 | else if (!force) |
8629 | return arg; |
8630 | |
8631 | type = cv_unqualified (type); |
8632 | tree argtype = TREE_TYPE (arg); |
8633 | if (argtype && same_type_p (type, argtype)) |
8634 | return arg; |
8635 | |
8636 | arg = build1 (IMPLICIT_CONV_EXPR, type, arg); |
8637 | IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true; |
8638 | IMPLICIT_CONV_EXPR_FORCED (arg) = force; |
8639 | return arg; |
8640 | } |
8641 | |
8642 | /* Convert the indicated template ARG as necessary to match the |
8643 | indicated template PARM. Returns the converted ARG, or |
8644 | error_mark_node if the conversion was unsuccessful. Error and |
8645 | warning messages are issued under control of COMPLAIN. This |
8646 | conversion is for the Ith parameter in the parameter list. ARGS is |
8647 | the full set of template arguments deduced so far. */ |
8648 | |
8649 | static tree |
8650 | convert_template_argument (tree parm, |
8651 | tree arg, |
8652 | tree args, |
8653 | tsubst_flags_t complain, |
8654 | int i, |
8655 | tree in_decl) |
8656 | { |
8657 | tree orig_arg; |
8658 | tree val; |
8659 | int is_type, requires_type, is_tmpl_type, requires_tmpl_type; |
8660 | |
8661 | if (parm == error_mark_node || error_operand_p (t: arg)) |
8662 | return error_mark_node; |
8663 | |
8664 | if (arg == any_targ_node) |
8665 | return arg; |
8666 | |
8667 | if (TREE_CODE (arg) == TREE_LIST |
8668 | && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF) |
8669 | { |
8670 | /* The template argument was the name of some |
8671 | member function. That's usually |
8672 | invalid, but static members are OK. In any |
8673 | case, grab the underlying fields/functions |
8674 | and issue an error later if required. */ |
8675 | TREE_TYPE (arg) = unknown_type_node; |
8676 | } |
8677 | |
8678 | orig_arg = arg; |
8679 | |
8680 | requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; |
8681 | requires_type = (TREE_CODE (parm) == TYPE_DECL |
8682 | || requires_tmpl_type); |
8683 | |
8684 | /* When determining whether an argument pack expansion is a template, |
8685 | look at the pattern. */ |
8686 | if (PACK_EXPANSION_P (arg)) |
8687 | arg = PACK_EXPANSION_PATTERN (arg); |
8688 | |
8689 | /* Deal with an injected-class-name used as a template template arg. */ |
8690 | if (requires_tmpl_type && CLASS_TYPE_P (arg)) |
8691 | { |
8692 | tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg)); |
8693 | if (TREE_CODE (t) == TEMPLATE_DECL) |
8694 | { |
8695 | if (cxx_dialect >= cxx11) |
8696 | /* OK under DR 1004. */; |
8697 | else if (complain & tf_warning_or_error) |
8698 | pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD" |
8699 | " used as template template argument", TYPE_NAME (arg)); |
8700 | else if (flag_pedantic_errors) |
8701 | t = arg; |
8702 | |
8703 | arg = t; |
8704 | } |
8705 | } |
8706 | |
8707 | is_tmpl_type = |
8708 | ((TREE_CODE (arg) == TEMPLATE_DECL |
8709 | && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) |
8710 | || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK) |
8711 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM |
8712 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); |
8713 | |
8714 | if (is_tmpl_type |
8715 | && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM |
8716 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)) |
8717 | arg = TYPE_STUB_DECL (arg); |
8718 | |
8719 | is_type = TYPE_P (arg) || is_tmpl_type; |
8720 | |
8721 | if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF |
8722 | && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) |
8723 | { |
8724 | if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR) |
8725 | { |
8726 | if (complain & tf_error) |
8727 | error ("invalid use of destructor %qE as a type", orig_arg); |
8728 | return error_mark_node; |
8729 | } |
8730 | |
8731 | permerror (input_location, |
8732 | "to refer to a type member of a template parameter, " |
8733 | "use %<typename %E%>", orig_arg); |
8734 | |
8735 | orig_arg = make_typename_type (TREE_OPERAND (arg, 0), |
8736 | TREE_OPERAND (arg, 1), |
8737 | typename_type, |
8738 | complain); |
8739 | arg = orig_arg; |
8740 | is_type = 1; |
8741 | } |
8742 | if (is_type != requires_type) |
8743 | { |
8744 | if (in_decl) |
8745 | { |
8746 | if (complain & tf_error) |
8747 | { |
8748 | auto_diagnostic_group d; |
8749 | error ("type/value mismatch at argument %d in template " |
8750 | "parameter list for %qD", |
8751 | i + 1, in_decl); |
8752 | if (is_type) |
8753 | { |
8754 | /* The template argument is a type, but we're expecting |
8755 | an expression. */ |
8756 | inform (input_location, |
8757 | " expected a constant of type %qT, got %qT", |
8758 | TREE_TYPE (parm), |
8759 | (DECL_P (arg) ? DECL_NAME (arg) : orig_arg)); |
8760 | /* [temp.arg]/2: "In a template-argument, an ambiguity |
8761 | between a type-id and an expression is resolved to a |
8762 | type-id, regardless of the form of the corresponding |
8763 | template-parameter." So give the user a clue. */ |
8764 | if (TREE_CODE (arg) == FUNCTION_TYPE) |
8765 | inform (input_location, " ambiguous template argument " |
8766 | "for non-type template parameter is treated as " |
8767 | "function type"); |
8768 | } |
8769 | else if (requires_tmpl_type) |
8770 | inform (input_location, |
8771 | " expected a class template, got %qE", orig_arg); |
8772 | else |
8773 | inform (input_location, |
8774 | " expected a type, got %qE", orig_arg); |
8775 | } |
8776 | } |
8777 | return error_mark_node; |
8778 | } |
8779 | if (is_tmpl_type ^ requires_tmpl_type) |
8780 | { |
8781 | if (in_decl && (complain & tf_error)) |
8782 | { |
8783 | auto_diagnostic_group d; |
8784 | error ("type/value mismatch at argument %d in template " |
8785 | "parameter list for %qD", |
8786 | i + 1, in_decl); |
8787 | if (is_tmpl_type) |
8788 | inform (input_location, |
8789 | " expected a type, got %qT", DECL_NAME (arg)); |
8790 | else |
8791 | inform (input_location, |
8792 | " expected a class template, got %qT", orig_arg); |
8793 | } |
8794 | return error_mark_node; |
8795 | } |
8796 | |
8797 | if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg)) |
8798 | /* We already did the appropriate conversion when packing args. */ |
8799 | val = orig_arg; |
8800 | else if (is_type) |
8801 | { |
8802 | if (requires_tmpl_type) |
8803 | { |
8804 | if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE) |
8805 | /* The number of argument required is not known yet. |
8806 | Just accept it for now. */ |
8807 | val = orig_arg; |
8808 | else |
8809 | { |
8810 | /* Strip alias templates that are equivalent to another |
8811 | template. */ |
8812 | arg = get_underlying_template (tmpl: arg); |
8813 | |
8814 | if (coerce_template_template_parms (parm_tmpl: parm, arg_tmpl: arg, |
8815 | complain, in_decl, |
8816 | outer_args: args)) |
8817 | { |
8818 | val = arg; |
8819 | |
8820 | /* TEMPLATE_TEMPLATE_PARM node is preferred over |
8821 | TEMPLATE_DECL. */ |
8822 | if (val != error_mark_node) |
8823 | { |
8824 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (val)) |
8825 | val = TREE_TYPE (val); |
8826 | if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION) |
8827 | val = make_pack_expansion (arg: val, complain); |
8828 | } |
8829 | } |
8830 | else |
8831 | { |
8832 | if (in_decl && (complain & tf_error)) |
8833 | { |
8834 | auto_diagnostic_group d; |
8835 | error ("type/value mismatch at argument %d in " |
8836 | "template parameter list for %qD", |
8837 | i + 1, in_decl); |
8838 | inform (input_location, |
8839 | " expected a template of type %qD, got %qT", |
8840 | parm, orig_arg); |
8841 | } |
8842 | |
8843 | val = error_mark_node; |
8844 | } |
8845 | |
8846 | // Check that the constraints are compatible before allowing the |
8847 | // substitution. |
8848 | if (val != error_mark_node) |
8849 | if (!is_compatible_template_arg (parm, arg, args)) |
8850 | { |
8851 | if (in_decl && (complain & tf_error)) |
8852 | { |
8853 | auto_diagnostic_group d; |
8854 | error ("constraint mismatch at argument %d in " |
8855 | "template parameter list for %qD", |
8856 | i + 1, in_decl); |
8857 | inform (input_location, " expected %qD but got %qD", |
8858 | parm, arg); |
8859 | } |
8860 | val = error_mark_node; |
8861 | } |
8862 | } |
8863 | } |
8864 | else |
8865 | val = orig_arg; |
8866 | /* We only form one instance of each template specialization. |
8867 | Therefore, if we use a non-canonical variant (i.e., a |
8868 | typedef), any future messages referring to the type will use |
8869 | the typedef, which is confusing if those future uses do not |
8870 | themselves also use the typedef. */ |
8871 | if (TYPE_P (val)) |
8872 | val = canonicalize_type_argument (arg: val, complain); |
8873 | } |
8874 | else |
8875 | { |
8876 | tree t = TREE_TYPE (parm); |
8877 | |
8878 | if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm)) |
8879 | > TMPL_ARGS_DEPTH (args)) |
8880 | /* We don't have enough levels of args to do any substitution. This |
8881 | can happen in the context of -fnew-ttp-matching. */; |
8882 | else if (tree a = type_uses_auto (t)) |
8883 | { |
8884 | t = do_auto_deduction (t, arg, a, complain, adc_unify, args, |
8885 | LOOKUP_IMPLICIT, /*tmpl=*/in_decl); |
8886 | if (t == error_mark_node) |
8887 | return error_mark_node; |
8888 | } |
8889 | else |
8890 | t = tsubst (t, args, complain, in_decl); |
8891 | |
8892 | /* Perform array-to-pointer and function-to-pointer conversion |
8893 | as per [temp.param]/10. */ |
8894 | t = type_decays_to (t); |
8895 | |
8896 | if (invalid_nontype_parm_type_p (t, complain)) |
8897 | return error_mark_node; |
8898 | |
8899 | /* Drop top-level cv-qualifiers on the substituted/deduced type of |
8900 | this non-type template parameter, as per [temp.param]/6. */ |
8901 | t = cv_unqualified (t); |
8902 | |
8903 | if (t != TREE_TYPE (parm)) |
8904 | t = canonicalize_type_argument (arg: t, complain); |
8905 | |
8906 | /* We need to handle arguments for alias or concept templates |
8907 | differently: we need to force building an IMPLICIT_CONV_EXPR, because |
8908 | these arguments are going to be substituted directly into the |
8909 | dependent type; they might not get another chance at |
8910 | convert_nontype_argument. But if the argument ends up here again for |
8911 | a template that isn't one of those, remove the conversion for |
8912 | consistency between naming the same dependent type directly or through |
8913 | an alias. */ |
8914 | bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl) |
8915 | || concept_definition_p (t: in_decl)); |
8916 | if (!force_conv |
8917 | && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR |
8918 | && IMPLICIT_CONV_EXPR_FORCED (orig_arg) |
8919 | && same_type_p (TREE_TYPE (orig_arg), t)) |
8920 | orig_arg = TREE_OPERAND (orig_arg, 0); |
8921 | |
8922 | if (!type_dependent_expression_p (orig_arg) |
8923 | && !uses_template_parms (t)) |
8924 | /* We used to call digest_init here. However, digest_init |
8925 | will report errors, which we don't want when complain |
8926 | is zero. More importantly, digest_init will try too |
8927 | hard to convert things: for example, `0' should not be |
8928 | converted to pointer type at this point according to |
8929 | the standard. Accepting this is not merely an |
8930 | extension, since deciding whether or not these |
8931 | conversions can occur is part of determining which |
8932 | function template to call, or whether a given explicit |
8933 | argument specification is valid. */ |
8934 | val = convert_nontype_argument (type: t, expr: orig_arg, complain); |
8935 | else |
8936 | { |
8937 | val = canonicalize_expr_argument (arg: orig_arg, complain); |
8938 | val = maybe_convert_nontype_argument (type: t, arg: val, force: force_conv); |
8939 | } |
8940 | |
8941 | if (val == NULL_TREE) |
8942 | val = error_mark_node; |
8943 | else if (val == error_mark_node && (complain & tf_error)) |
8944 | error_at (cp_expr_loc_or_input_loc (t: orig_arg), |
8945 | "could not convert template argument %qE from %qT to %qT", |
8946 | orig_arg, TREE_TYPE (orig_arg), t); |
8947 | |
8948 | if (INDIRECT_REF_P (val)) |
8949 | { |
8950 | /* Reject template arguments that are references to built-in |
8951 | functions with no library fallbacks. */ |
8952 | const_tree inner = TREE_OPERAND (val, 0); |
8953 | const_tree innertype = TREE_TYPE (inner); |
8954 | if (innertype |
8955 | && TYPE_REF_P (innertype) |
8956 | && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE |
8957 | && TREE_OPERAND_LENGTH (inner) > 0 |
8958 | && reject_gcc_builtin (TREE_OPERAND (inner, 0))) |
8959 | return error_mark_node; |
8960 | } |
8961 | |
8962 | if (TREE_CODE (val) == SCOPE_REF) |
8963 | { |
8964 | /* Strip typedefs from the SCOPE_REF. */ |
8965 | tree type = canonicalize_type_argument (TREE_TYPE (val), complain); |
8966 | tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0), |
8967 | complain); |
8968 | val = build_qualified_name (type, scope, TREE_OPERAND (val, 1), |
8969 | QUALIFIED_NAME_IS_TEMPLATE (val)); |
8970 | } |
8971 | } |
8972 | |
8973 | return val; |
8974 | } |
8975 | |
8976 | /* Coerces the remaining template arguments in INNER_ARGS (from |
8977 | ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS. |
8978 | Returns the coerced argument pack. PARM_IDX is the position of this |
8979 | parameter in the template parameter list. ARGS is the original |
8980 | template argument list. */ |
8981 | static tree |
8982 | coerce_template_parameter_pack (tree parms, |
8983 | int parm_idx, |
8984 | tree args, |
8985 | tree inner_args, |
8986 | int arg_idx, |
8987 | tree new_args, |
8988 | int* lost, |
8989 | tree in_decl, |
8990 | tsubst_flags_t complain) |
8991 | { |
8992 | tree parm = TREE_VEC_ELT (parms, parm_idx); |
8993 | int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
8994 | tree packed_args; |
8995 | tree argument_pack; |
8996 | tree packed_parms = NULL_TREE; |
8997 | |
8998 | if (arg_idx > nargs) |
8999 | arg_idx = nargs; |
9000 | |
9001 | if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm))) |
9002 | { |
9003 | /* When the template parameter is a non-type template parameter pack |
9004 | or template template parameter pack whose type or template |
9005 | parameters use parameter packs, we know exactly how many arguments |
9006 | we are looking for. Build a vector of the instantiated decls for |
9007 | these template parameters in PACKED_PARMS. */ |
9008 | /* We can't use make_pack_expansion here because it would interpret a |
9009 | _DECL as a use rather than a declaration. */ |
9010 | tree decl = TREE_VALUE (parm); |
9011 | tree exp = cxx_make_type (TYPE_PACK_EXPANSION); |
9012 | PACK_EXPANSION_PATTERN (exp) = decl; |
9013 | PACK_EXPANSION_PARAMETER_PACKS (exp) = packs; |
9014 | SET_TYPE_STRUCTURAL_EQUALITY (exp); |
9015 | |
9016 | TREE_VEC_LENGTH (args)--; |
9017 | packed_parms = tsubst_pack_expansion (exp, args, complain, decl); |
9018 | TREE_VEC_LENGTH (args)++; |
9019 | |
9020 | if (packed_parms == error_mark_node) |
9021 | return error_mark_node; |
9022 | |
9023 | /* If we're doing a partial instantiation of a member template, |
9024 | verify that all of the types used for the non-type |
9025 | template parameter pack are, in fact, valid for non-type |
9026 | template parameters. */ |
9027 | if (arg_idx < nargs |
9028 | && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))) |
9029 | { |
9030 | int j, len = TREE_VEC_LENGTH (packed_parms); |
9031 | for (j = 0; j < len; ++j) |
9032 | { |
9033 | tree t = TREE_VEC_ELT (packed_parms, j); |
9034 | if (TREE_CODE (t) == PARM_DECL |
9035 | && invalid_nontype_parm_type_p (TREE_TYPE (t), complain)) |
9036 | return error_mark_node; |
9037 | } |
9038 | /* We don't know how many args we have yet, just |
9039 | use the unconverted ones for now. */ |
9040 | return NULL_TREE; |
9041 | } |
9042 | |
9043 | packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms)); |
9044 | } |
9045 | else |
9046 | packed_args = make_tree_vec (nargs - arg_idx); |
9047 | |
9048 | /* Convert the remaining arguments, which will be a part of the |
9049 | parameter pack "parm". */ |
9050 | int first_pack_arg = arg_idx; |
9051 | for (; arg_idx < nargs; ++arg_idx) |
9052 | { |
9053 | tree arg = TREE_VEC_ELT (inner_args, arg_idx); |
9054 | tree actual_parm = TREE_VALUE (parm); |
9055 | int pack_idx = arg_idx - first_pack_arg; |
9056 | |
9057 | if (packed_parms) |
9058 | { |
9059 | /* Once we've packed as many args as we have types, stop. */ |
9060 | if (pack_idx >= TREE_VEC_LENGTH (packed_parms)) |
9061 | break; |
9062 | else if (PACK_EXPANSION_P (arg)) |
9063 | /* We don't know how many args we have yet, just |
9064 | use the unconverted ones for now. */ |
9065 | return NULL_TREE; |
9066 | else |
9067 | actual_parm = TREE_VEC_ELT (packed_parms, pack_idx); |
9068 | } |
9069 | |
9070 | if (arg == error_mark_node) |
9071 | { |
9072 | if (complain & tf_error) |
9073 | error ("template argument %d is invalid", arg_idx + 1); |
9074 | } |
9075 | else |
9076 | arg = convert_template_argument (parm: actual_parm, |
9077 | arg, args: new_args, complain, i: parm_idx, |
9078 | in_decl); |
9079 | if (arg == error_mark_node) |
9080 | (*lost)++; |
9081 | TREE_VEC_ELT (packed_args, pack_idx) = arg; |
9082 | } |
9083 | |
9084 | if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args) |
9085 | && TREE_VEC_LENGTH (packed_args) > 0) |
9086 | { |
9087 | if (complain & tf_error) |
9088 | error ("wrong number of template arguments (%d, should be %d)", |
9089 | arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args)); |
9090 | return error_mark_node; |
9091 | } |
9092 | |
9093 | if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL |
9094 | || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL) |
9095 | argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK); |
9096 | else |
9097 | { |
9098 | argument_pack = make_node (NONTYPE_ARGUMENT_PACK); |
9099 | TREE_CONSTANT (argument_pack) = 1; |
9100 | } |
9101 | |
9102 | ARGUMENT_PACK_ARGS (argument_pack) = packed_args; |
9103 | if (CHECKING_P) |
9104 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args, |
9105 | TREE_VEC_LENGTH (packed_args)); |
9106 | return argument_pack; |
9107 | } |
9108 | |
9109 | /* Returns the number of pack expansions in the template argument vector |
9110 | ARGS. */ |
9111 | |
9112 | static int |
9113 | pack_expansion_args_count (tree args) |
9114 | { |
9115 | int i; |
9116 | int count = 0; |
9117 | if (args) |
9118 | for (i = 0; i < TREE_VEC_LENGTH (args); ++i) |
9119 | { |
9120 | tree elt = TREE_VEC_ELT (args, i); |
9121 | if (elt && PACK_EXPANSION_P (elt)) |
9122 | ++count; |
9123 | } |
9124 | return count; |
9125 | } |
9126 | |
9127 | /* Convert all template arguments to their appropriate types, and |
9128 | return a vector containing the innermost resulting template |
9129 | arguments. If any error occurs, return error_mark_node. Error and |
9130 | warning messages are issued under control of COMPLAIN. |
9131 | |
9132 | If PARMS represents all template parameters levels, this function |
9133 | returns a vector of vectors representing all the resulting argument |
9134 | levels. Note that in this case, only the innermost arguments are |
9135 | coerced because the outermost ones are supposed to have been coerced |
9136 | already. Otherwise, if PARMS represents only (the innermost) vector |
9137 | of parameters, this function returns a vector containing just the |
9138 | innermost resulting arguments. |
9139 | |
9140 | If REQUIRE_ALL_ARGS is false, argument deduction will be performed |
9141 | for arguments not specified in ARGS. If REQUIRE_ALL_ARGS is true, |
9142 | arguments not specified in ARGS must have default arguments which |
9143 | we'll use to fill in ARGS. */ |
9144 | |
9145 | tree |
9146 | coerce_template_parms (tree parms, |
9147 | tree args, |
9148 | tree in_decl, |
9149 | tsubst_flags_t complain, |
9150 | bool require_all_args /* = true */) |
9151 | { |
9152 | int nparms, nargs, parm_idx, arg_idx, lost = 0; |
9153 | tree orig_inner_args; |
9154 | tree inner_args; |
9155 | |
9156 | /* When used as a boolean value, indicates whether this is a |
9157 | variadic template parameter list. Since it's an int, we can also |
9158 | subtract it from nparms to get the number of non-variadic |
9159 | parameters. */ |
9160 | int variadic_p = 0; |
9161 | int variadic_args_p = 0; |
9162 | int post_variadic_parms = 0; |
9163 | |
9164 | /* Adjustment to nparms for fixed parameter packs. */ |
9165 | int fixed_pack_adjust = 0; |
9166 | int fixed_packs = 0; |
9167 | int missing = 0; |
9168 | |
9169 | /* Likewise for parameters with default arguments. */ |
9170 | int default_p = 0; |
9171 | |
9172 | if (args == error_mark_node) |
9173 | return error_mark_node; |
9174 | |
9175 | bool return_full_args = false; |
9176 | if (TREE_CODE (parms) == TREE_LIST) |
9177 | { |
9178 | if (TMPL_PARMS_DEPTH (parms) > 1) |
9179 | { |
9180 | gcc_assert (TMPL_PARMS_DEPTH (parms) == TMPL_ARGS_DEPTH (args)); |
9181 | return_full_args = true; |
9182 | } |
9183 | parms = INNERMOST_TEMPLATE_PARMS (parms); |
9184 | } |
9185 | |
9186 | nparms = TREE_VEC_LENGTH (parms); |
9187 | |
9188 | /* Determine if there are any parameter packs or default arguments. */ |
9189 | for (parm_idx = 0; parm_idx < nparms; ++parm_idx) |
9190 | { |
9191 | tree parm = TREE_VEC_ELT (parms, parm_idx); |
9192 | if (variadic_p) |
9193 | ++post_variadic_parms; |
9194 | if (template_parameter_pack_p (TREE_VALUE (parm))) |
9195 | ++variadic_p; |
9196 | if (TREE_PURPOSE (parm)) |
9197 | ++default_p; |
9198 | } |
9199 | |
9200 | inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args); |
9201 | /* If there are no parameters that follow a parameter pack, we need to |
9202 | expand any argument packs so that we can deduce a parameter pack from |
9203 | some non-packed args followed by an argument pack, as in variadic85.C. |
9204 | If there are such parameters, we need to leave argument packs intact |
9205 | so the arguments are assigned properly. This can happen when dealing |
9206 | with a nested class inside a partial specialization of a class |
9207 | template, as in variadic92.C, or when deducing a template parameter pack |
9208 | from a sub-declarator, as in variadic114.C. */ |
9209 | if (!post_variadic_parms) |
9210 | inner_args = expand_template_argument_pack (args: inner_args); |
9211 | |
9212 | /* Count any pack expansion args. */ |
9213 | variadic_args_p = pack_expansion_args_count (args: inner_args); |
9214 | |
9215 | nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0; |
9216 | if ((nargs - variadic_args_p > nparms && !variadic_p) |
9217 | || (nargs < nparms - variadic_p |
9218 | && require_all_args |
9219 | && !variadic_args_p |
9220 | && (TREE_VEC_ELT (parms, nargs) != error_mark_node |
9221 | && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))) |
9222 | { |
9223 | bad_nargs: |
9224 | if (complain & tf_error) |
9225 | { |
9226 | auto_diagnostic_group d; |
9227 | if (variadic_p || default_p) |
9228 | { |
9229 | nparms -= variadic_p + default_p; |
9230 | error ("wrong number of template arguments " |
9231 | "(%d, should be at least %d)", nargs, nparms); |
9232 | } |
9233 | else |
9234 | error ("wrong number of template arguments " |
9235 | "(%d, should be %d)", nargs, nparms); |
9236 | |
9237 | if (in_decl) |
9238 | inform (DECL_SOURCE_LOCATION (in_decl), |
9239 | "provided for %qD", in_decl); |
9240 | } |
9241 | |
9242 | return error_mark_node; |
9243 | } |
9244 | /* We can't pass a pack expansion to a non-pack parameter of an alias |
9245 | template (DR 1430). */ |
9246 | else if (in_decl |
9247 | && (DECL_ALIAS_TEMPLATE_P (in_decl) |
9248 | || concept_definition_p (t: in_decl)) |
9249 | && variadic_args_p |
9250 | && nargs - variadic_args_p < nparms - variadic_p) |
9251 | { |
9252 | if (complain & tf_error) |
9253 | { |
9254 | for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i) |
9255 | { |
9256 | tree arg = TREE_VEC_ELT (inner_args, i); |
9257 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
9258 | |
9259 | if (PACK_EXPANSION_P (arg) |
9260 | && !template_parameter_pack_p (parm)) |
9261 | { |
9262 | auto_diagnostic_group d; |
9263 | if (DECL_ALIAS_TEMPLATE_P (in_decl)) |
9264 | error_at (location_of (arg), |
9265 | "pack expansion argument for non-pack parameter " |
9266 | "%qD of alias template %qD", parm, in_decl); |
9267 | else |
9268 | error_at (location_of (arg), |
9269 | "pack expansion argument for non-pack parameter " |
9270 | "%qD of concept %qD", parm, in_decl); |
9271 | inform (DECL_SOURCE_LOCATION (parm), "declared here"); |
9272 | goto found; |
9273 | } |
9274 | } |
9275 | gcc_unreachable (); |
9276 | found:; |
9277 | } |
9278 | return error_mark_node; |
9279 | } |
9280 | |
9281 | /* We need to evaluate the template arguments, even though this |
9282 | template-id may be nested within a "sizeof". */ |
9283 | cp_evaluated ev; |
9284 | |
9285 | tree new_args = add_outermost_template_args (args, extra_args: make_tree_vec (nparms)); |
9286 | tree& new_inner_args = TMPL_ARGS_LEVEL (new_args, TMPL_ARGS_DEPTH (new_args)); |
9287 | int pack_adjust = 0; |
9288 | for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++) |
9289 | { |
9290 | tree arg; |
9291 | tree parm; |
9292 | |
9293 | /* Get the Ith template parameter. */ |
9294 | parm = TREE_VEC_ELT (parms, parm_idx); |
9295 | |
9296 | if (parm == error_mark_node) |
9297 | { |
9298 | TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node; |
9299 | continue; |
9300 | } |
9301 | |
9302 | /* Calculate the next argument. */ |
9303 | if (arg_idx < nargs) |
9304 | arg = TREE_VEC_ELT (inner_args, arg_idx); |
9305 | else |
9306 | arg = NULL_TREE; |
9307 | |
9308 | if (template_parameter_pack_p (TREE_VALUE (parm)) |
9309 | && (arg || require_all_args || !(complain & tf_partial)) |
9310 | && !(arg && ARGUMENT_PACK_P (arg))) |
9311 | { |
9312 | /* Some arguments will be placed in the |
9313 | template parameter pack PARM. */ |
9314 | arg = coerce_template_parameter_pack (parms, parm_idx, args, |
9315 | inner_args, arg_idx, |
9316 | new_args, lost: &lost, |
9317 | in_decl, complain); |
9318 | |
9319 | if (arg == NULL_TREE) |
9320 | { |
9321 | /* We don't know how many args we have yet, just use the |
9322 | unconverted (and still packed) ones for now. */ |
9323 | ggc_free (new_inner_args); |
9324 | new_inner_args = strip_typedefs (orig_inner_args, |
9325 | /*remove_attrs=*/nullptr, |
9326 | STF_KEEP_INJ_CLASS_NAME); |
9327 | arg_idx = nargs; |
9328 | break; |
9329 | } |
9330 | |
9331 | TREE_VEC_ELT (new_inner_args, parm_idx) = arg; |
9332 | |
9333 | /* Store this argument. */ |
9334 | if (arg == error_mark_node) |
9335 | { |
9336 | lost++; |
9337 | /* We are done with all of the arguments. */ |
9338 | arg_idx = nargs; |
9339 | break; |
9340 | } |
9341 | else |
9342 | { |
9343 | pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1; |
9344 | arg_idx += pack_adjust; |
9345 | if (fixed_parameter_pack_p (TREE_VALUE (parm))) |
9346 | { |
9347 | ++fixed_packs; |
9348 | fixed_pack_adjust += pack_adjust; |
9349 | } |
9350 | } |
9351 | |
9352 | continue; |
9353 | } |
9354 | else if (arg) |
9355 | { |
9356 | if (PACK_EXPANSION_P (arg)) |
9357 | { |
9358 | /* "If every valid specialization of a variadic template |
9359 | requires an empty template parameter pack, the template is |
9360 | ill-formed, no diagnostic required." So check that the |
9361 | pattern works with this parameter. */ |
9362 | tree pattern = PACK_EXPANSION_PATTERN (arg); |
9363 | tree conv = convert_template_argument (TREE_VALUE (parm), |
9364 | arg: pattern, args: new_args, |
9365 | complain, i: parm_idx, |
9366 | in_decl); |
9367 | if (conv == error_mark_node) |
9368 | { |
9369 | if (complain & tf_error) |
9370 | inform (input_location, "so any instantiation with a " |
9371 | "non-empty parameter pack would be ill-formed"); |
9372 | ++lost; |
9373 | } |
9374 | else if (TYPE_P (conv) && !TYPE_P (pattern)) |
9375 | /* Recover from missing typename. */ |
9376 | TREE_VEC_ELT (inner_args, arg_idx) |
9377 | = make_pack_expansion (arg: conv, complain); |
9378 | |
9379 | /* We don't know how many args we have yet, just |
9380 | use the unconverted (but unpacked) ones for now. */ |
9381 | ggc_free (new_inner_args); |
9382 | new_inner_args = strip_typedefs (inner_args, |
9383 | /*remove_attrs=*/nullptr, |
9384 | STF_KEEP_INJ_CLASS_NAME); |
9385 | arg_idx = nargs; |
9386 | break; |
9387 | } |
9388 | } |
9389 | else if (require_all_args) |
9390 | { |
9391 | /* There must be a default arg in this case. */ |
9392 | arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args, |
9393 | complain | (processing_template_decl |
9394 | ? tf_partial : tf_none), |
9395 | in_decl); |
9396 | /* The position of the first default template argument, |
9397 | is also the number of non-defaulted arguments in NEW_INNER_ARGS. |
9398 | Record that. */ |
9399 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args)) |
9400 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, |
9401 | arg_idx - pack_adjust); |
9402 | } |
9403 | else |
9404 | break; |
9405 | |
9406 | if (arg == error_mark_node) |
9407 | { |
9408 | if (complain & tf_error) |
9409 | error ("template argument %d is invalid", arg_idx + 1); |
9410 | } |
9411 | else if (!arg) |
9412 | { |
9413 | /* This can occur if there was an error in the template |
9414 | parameter list itself (which we would already have |
9415 | reported) that we are trying to recover from, e.g., a class |
9416 | template with a parameter list such as |
9417 | template<typename..., typename> (cpp0x/variadic150.C). */ |
9418 | ++lost; |
9419 | |
9420 | /* This can also happen with a fixed parameter pack (71834). */ |
9421 | if (arg_idx >= nargs) |
9422 | ++missing; |
9423 | } |
9424 | else |
9425 | arg = convert_template_argument (TREE_VALUE (parm), |
9426 | arg, args: new_args, complain, |
9427 | i: parm_idx, in_decl); |
9428 | |
9429 | if (arg == error_mark_node) |
9430 | lost++; |
9431 | |
9432 | TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg; |
9433 | } |
9434 | |
9435 | if (missing || arg_idx < nargs - variadic_args_p) |
9436 | { |
9437 | /* If we had fixed parameter packs, we didn't know how many arguments we |
9438 | actually needed earlier; now we do. */ |
9439 | nparms += fixed_pack_adjust; |
9440 | variadic_p -= fixed_packs; |
9441 | goto bad_nargs; |
9442 | } |
9443 | |
9444 | if (arg_idx < nargs) |
9445 | { |
9446 | /* We had some pack expansion arguments that will only work if the packs |
9447 | are empty, but wait until instantiation time to complain. |
9448 | See variadic-ttp3.C. */ |
9449 | |
9450 | /* Except that we can't provide empty packs to alias templates or |
9451 | concepts when there are no corresponding parameters. Basically, |
9452 | we can get here with this: |
9453 | |
9454 | template<typename T> concept C = true; |
9455 | |
9456 | template<typename... Args> |
9457 | requires C<Args...> |
9458 | void f(); |
9459 | |
9460 | When parsing C<Args...>, we try to form a concept check of |
9461 | C<?, Args...>. Without the extra check for substituting an empty |
9462 | pack past the last parameter, we can accept the check as valid. |
9463 | |
9464 | FIXME: This may be valid for alias templates (but I doubt it). |
9465 | |
9466 | FIXME: The error could be better also. */ |
9467 | if (in_decl && concept_definition_p (t: in_decl)) |
9468 | { |
9469 | if (complain & tf_error) |
9470 | error_at (location_of (TREE_VEC_ELT (args, arg_idx)), |
9471 | "too many arguments"); |
9472 | return error_mark_node; |
9473 | } |
9474 | |
9475 | int len = nparms + (nargs - arg_idx); |
9476 | tree args = make_tree_vec (len); |
9477 | int i = 0; |
9478 | for (; i < nparms; ++i) |
9479 | TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i); |
9480 | for (; i < len; ++i, ++arg_idx) |
9481 | TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args, |
9482 | arg_idx - pack_adjust); |
9483 | new_inner_args = args; |
9484 | } |
9485 | |
9486 | if (lost) |
9487 | { |
9488 | gcc_assert (!(complain & tf_error) || seen_error ()); |
9489 | return error_mark_node; |
9490 | } |
9491 | |
9492 | if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args)) |
9493 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, |
9494 | TREE_VEC_LENGTH (new_inner_args)); |
9495 | |
9496 | /* If we expanded packs in inner_args and aren't returning it now, the |
9497 | expanded vec is garbage. */ |
9498 | if (inner_args != new_inner_args |
9499 | && inner_args != orig_inner_args) |
9500 | ggc_free (inner_args); |
9501 | |
9502 | return return_full_args ? new_args : new_inner_args; |
9503 | } |
9504 | |
9505 | /* Returns true if T is a wrapper to make a C++20 template parameter |
9506 | object const. */ |
9507 | |
9508 | static bool |
9509 | class_nttp_const_wrapper_p (tree t) |
9510 | { |
9511 | if (cxx_dialect < cxx20) |
9512 | return false; |
9513 | return (TREE_CODE (t) == VIEW_CONVERT_EXPR |
9514 | && CP_TYPE_CONST_P (TREE_TYPE (t)) |
9515 | && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX); |
9516 | } |
9517 | |
9518 | /* Returns 1 if template args OT and NT are equivalent. */ |
9519 | |
9520 | int |
9521 | template_args_equal (tree ot, tree nt) |
9522 | { |
9523 | if (nt == ot) |
9524 | return 1; |
9525 | if (nt == NULL_TREE || ot == NULL_TREE) |
9526 | return false; |
9527 | if (nt == any_targ_node || ot == any_targ_node) |
9528 | return true; |
9529 | |
9530 | if (class_nttp_const_wrapper_p (t: nt)) |
9531 | nt = TREE_OPERAND (nt, 0); |
9532 | if (class_nttp_const_wrapper_p (t: ot)) |
9533 | ot = TREE_OPERAND (ot, 0); |
9534 | |
9535 | /* DR 1558: Don't treat an alias template specialization with dependent |
9536 | arguments as equivalent to its underlying type when used as a template |
9537 | argument; we need them to be distinct so that we substitute into the |
9538 | specialization arguments at instantiation time. And aliases can't be |
9539 | equivalent without being ==, so we don't need to look any deeper. |
9540 | |
9541 | During partial ordering, however, we need to treat them normally so we can |
9542 | order uses of the same alias with different cv-qualification (79960). */ |
9543 | auto cso = make_temp_override (var&: comparing_dependent_aliases); |
9544 | if (!comparing_for_partial_ordering) |
9545 | ++comparing_dependent_aliases; |
9546 | |
9547 | if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC) |
9548 | /* For member templates */ |
9549 | return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt); |
9550 | else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt)) |
9551 | return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt) |
9552 | && template_args_equal (PACK_EXPANSION_PATTERN (ot), |
9553 | PACK_EXPANSION_PATTERN (nt)) |
9554 | && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot), |
9555 | PACK_EXPANSION_EXTRA_ARGS (nt))); |
9556 | else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt)) |
9557 | return cp_tree_equal (ot, nt); |
9558 | else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT) |
9559 | gcc_unreachable (); |
9560 | else if (TYPE_P (nt) || TYPE_P (ot)) |
9561 | { |
9562 | if (!(TYPE_P (nt) && TYPE_P (ot))) |
9563 | return false; |
9564 | return same_type_p (ot, nt); |
9565 | } |
9566 | else |
9567 | { |
9568 | /* Try to treat a template non-type argument that has been converted |
9569 | to the parameter type as equivalent to one that hasn't yet. */ |
9570 | for (enum tree_code code1 = TREE_CODE (ot); |
9571 | CONVERT_EXPR_CODE_P (code1) |
9572 | || code1 == NON_LVALUE_EXPR; |
9573 | code1 = TREE_CODE (ot)) |
9574 | ot = TREE_OPERAND (ot, 0); |
9575 | |
9576 | for (enum tree_code code2 = TREE_CODE (nt); |
9577 | CONVERT_EXPR_CODE_P (code2) |
9578 | || code2 == NON_LVALUE_EXPR; |
9579 | code2 = TREE_CODE (nt)) |
9580 | nt = TREE_OPERAND (nt, 0); |
9581 | |
9582 | return cp_tree_equal (ot, nt); |
9583 | } |
9584 | } |
9585 | |
9586 | /* Returns true iff the OLDARGS and NEWARGS are in fact identical sets of |
9587 | template arguments. Returns false otherwise, and updates OLDARG_PTR and |
9588 | NEWARG_PTR with the offending arguments if they are non-NULL. */ |
9589 | |
9590 | bool |
9591 | comp_template_args (tree oldargs, tree newargs, |
9592 | tree *oldarg_ptr /* = NULL */, tree *newarg_ptr /* = NULL */) |
9593 | { |
9594 | if (oldargs == newargs) |
9595 | return true; |
9596 | |
9597 | if (!oldargs || !newargs) |
9598 | return false; |
9599 | |
9600 | if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs)) |
9601 | return false; |
9602 | |
9603 | for (int i = 0; i < TREE_VEC_LENGTH (oldargs); ++i) |
9604 | { |
9605 | tree nt = TREE_VEC_ELT (newargs, i); |
9606 | tree ot = TREE_VEC_ELT (oldargs, i); |
9607 | |
9608 | if (! template_args_equal (ot, nt)) |
9609 | { |
9610 | if (oldarg_ptr != NULL) |
9611 | *oldarg_ptr = ot; |
9612 | if (newarg_ptr != NULL) |
9613 | *newarg_ptr = nt; |
9614 | return false; |
9615 | } |
9616 | } |
9617 | return true; |
9618 | } |
9619 | |
9620 | static bool |
9621 | comp_template_args_porder (tree oargs, tree nargs) |
9622 | { |
9623 | ++comparing_for_partial_ordering; |
9624 | bool equal = comp_template_args (oldargs: oargs, newargs: nargs); |
9625 | --comparing_for_partial_ordering; |
9626 | return equal; |
9627 | } |
9628 | |
9629 | /* Implement a freelist interface for objects of type T. |
9630 | |
9631 | Head is a separate object, rather than a regular member, so that we |
9632 | can define it as a GTY deletable pointer, which is highly |
9633 | desirable. A data member could be declared that way, but then the |
9634 | containing object would implicitly get GTY((user)), which would |
9635 | prevent us from instantiating freelists as global objects. |
9636 | Although this way we can create freelist global objects, they're |
9637 | such thin wrappers that instantiating temporaries at every use |
9638 | loses nothing and saves permanent storage for the freelist object. |
9639 | |
9640 | Member functions next, anew, poison and reinit have default |
9641 | implementations that work for most of the types we're interested |
9642 | in, but if they don't work for some type, they should be explicitly |
9643 | specialized. See the comments before them for requirements, and |
9644 | the example specializations for the tree_list_freelist. */ |
9645 | template <typename T> |
9646 | class freelist |
9647 | { |
9648 | /* Return the next object in a chain. We could just do type |
9649 | punning, but if we access the object with its underlying type, we |
9650 | avoid strict-aliasing trouble. This needs only work between |
9651 | poison and reinit. */ |
9652 | static T *&next (T *obj) { return obj->next; } |
9653 | |
9654 | /* Return a newly allocated, uninitialized or minimally-initialized |
9655 | object of type T. Any initialization performed by anew should |
9656 | either remain across the life of the object and the execution of |
9657 | poison, or be redone by reinit. */ |
9658 | static T *anew () { return ggc_alloc<T> (); } |
9659 | |
9660 | /* Optionally scribble all over the bits holding the object, so that |
9661 | they become (mostly?) uninitialized memory. This is called while |
9662 | preparing to make the object part of the free list. */ |
9663 | static void poison (T *obj) { |
9664 | T *p ATTRIBUTE_UNUSED = obj; |
9665 | T **q ATTRIBUTE_UNUSED = &next (obj); |
9666 | |
9667 | #ifdef ENABLE_GC_CHECKING |
9668 | /* Poison the data, to indicate the data is garbage. */ |
9669 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p))); |
9670 | memset (p, 0xa5, sizeof (*p)); |
9671 | #endif |
9672 | /* Let valgrind know the object is free. */ |
9673 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p))); |
9674 | |
9675 | /* Let valgrind know the next portion of the object is available, |
9676 | but uninitialized. */ |
9677 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q))); |
9678 | } |
9679 | |
9680 | /* Bring an object that underwent at least one lifecycle after anew |
9681 | and before the most recent free and poison, back to a usable |
9682 | state, reinitializing whatever is needed for it to be |
9683 | functionally equivalent to an object just allocated and returned |
9684 | by anew. This may poison or clear the next field, used by |
9685 | freelist housekeeping after poison was called. */ |
9686 | static void reinit (T *obj) { |
9687 | T **q ATTRIBUTE_UNUSED = &next (obj); |
9688 | |
9689 | #ifdef ENABLE_GC_CHECKING |
9690 | memset (q, 0xa5, sizeof (*q)); |
9691 | #endif |
9692 | /* Let valgrind know the entire object is available, but |
9693 | uninitialized. */ |
9694 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj))); |
9695 | } |
9696 | |
9697 | /* Reference a GTY-deletable pointer that points to the first object |
9698 | in the free list proper. */ |
9699 | T *&head; |
9700 | public: |
9701 | /* Construct a freelist object chaining objects off of HEAD. */ |
9702 | freelist (T *&head) : head(head) {} |
9703 | |
9704 | /* Add OBJ to the free object list. The former head becomes OBJ's |
9705 | successor. */ |
9706 | void free (T *obj) |
9707 | { |
9708 | poison (obj); |
9709 | next (obj) = head; |
9710 | head = obj; |
9711 | } |
9712 | |
9713 | /* Take an object from the free list, if one is available, or |
9714 | allocate a new one. Objects taken from the free list should be |
9715 | regarded as filled with garbage, except for bits that are |
9716 | configured to be preserved across free and alloc. */ |
9717 | T *alloc () |
9718 | { |
9719 | if (head) |
9720 | { |
9721 | T *obj = head; |
9722 | head = next (obj: head); |
9723 | reinit (obj); |
9724 | return obj; |
9725 | } |
9726 | else |
9727 | return anew (); |
9728 | } |
9729 | }; |
9730 | |
9731 | /* Explicitly specialize the interfaces for freelist<tree_node>: we |
9732 | want to allocate a TREE_LIST using the usual interface, and ensure |
9733 | TREE_CHAIN remains functional. Alas, we have to duplicate a bit of |
9734 | build_tree_list logic in reinit, so this could go out of sync. */ |
9735 | template <> |
9736 | inline tree & |
9737 | freelist<tree_node>::next (tree obj) |
9738 | { |
9739 | return TREE_CHAIN (obj); |
9740 | } |
9741 | template <> |
9742 | inline tree |
9743 | freelist<tree_node>::anew () |
9744 | { |
9745 | return build_tree_list (NULL, NULL); |
9746 | } |
9747 | template <> |
9748 | inline void |
9749 | freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED) |
9750 | { |
9751 | int size ATTRIBUTE_UNUSED = sizeof (tree_list); |
9752 | tree p ATTRIBUTE_UNUSED = obj; |
9753 | tree_base *b ATTRIBUTE_UNUSED = &obj->base; |
9754 | tree *q ATTRIBUTE_UNUSED = &next (obj); |
9755 | |
9756 | #ifdef ENABLE_GC_CHECKING |
9757 | gcc_checking_assert (TREE_CODE (obj) == TREE_LIST); |
9758 | |
9759 | /* Poison the data, to indicate the data is garbage. */ |
9760 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size)); |
9761 | memset (s: p, c: 0xa5, n: size); |
9762 | #endif |
9763 | /* Let valgrind know the object is free. */ |
9764 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size)); |
9765 | /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */ |
9766 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b))); |
9767 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q))); |
9768 | |
9769 | #ifdef ENABLE_GC_CHECKING |
9770 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b))); |
9771 | /* Keep TREE_CHAIN functional. */ |
9772 | TREE_SET_CODE (obj, TREE_LIST); |
9773 | #else |
9774 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b))); |
9775 | #endif |
9776 | } |
9777 | template <> |
9778 | inline void |
9779 | freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED) |
9780 | { |
9781 | tree_common *c ATTRIBUTE_UNUSED = &obj->common; |
9782 | |
9783 | #ifdef ENABLE_GC_CHECKING |
9784 | gcc_checking_assert (TREE_CODE (obj) == TREE_LIST); |
9785 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list))); |
9786 | memset (s: obj, c: 0, n: sizeof (tree_list)); |
9787 | #endif |
9788 | |
9789 | /* Let valgrind know the entire object is available, but |
9790 | uninitialized. */ |
9791 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list))); |
9792 | |
9793 | #ifdef ENABLE_GC_CHECKING |
9794 | TREE_SET_CODE (obj, TREE_LIST); |
9795 | #else |
9796 | TREE_CHAIN (obj) = NULL_TREE; |
9797 | TREE_TYPE (obj) = NULL_TREE; |
9798 | #endif |
9799 | VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (c, sizeof (*c))); |
9800 | } |
9801 | |
9802 | /* Point to the first object in the TREE_LIST freelist. */ |
9803 | static GTY((deletable)) tree tree_list_freelist_head; |
9804 | /* Return the/an actual TREE_LIST freelist. */ |
9805 | static inline freelist<tree_node> |
9806 | tree_list_freelist () |
9807 | { |
9808 | return tree_list_freelist_head; |
9809 | } |
9810 | |
9811 | /* Point to the first object in the tinst_level freelist. */ |
9812 | static GTY((deletable)) tinst_level *tinst_level_freelist_head; |
9813 | /* Return the/an actual tinst_level freelist. */ |
9814 | static inline freelist<tinst_level> |
9815 | tinst_level_freelist () |
9816 | { |
9817 | return tinst_level_freelist_head; |
9818 | } |
9819 | |
9820 | /* Point to the first object in the pending_template freelist. */ |
9821 | static GTY((deletable)) pending_template *pending_template_freelist_head; |
9822 | /* Return the/an actual pending_template freelist. */ |
9823 | static inline freelist<pending_template> |
9824 | pending_template_freelist () |
9825 | { |
9826 | return pending_template_freelist_head; |
9827 | } |
9828 | |
9829 | /* Build the TREE_LIST object out of a split list, store it |
9830 | permanently, and return it. */ |
9831 | tree |
9832 | tinst_level::to_list () |
9833 | { |
9834 | gcc_assert (split_list_p ()); |
9835 | tree ret = tree_list_freelist ().alloc (); |
9836 | TREE_PURPOSE (ret) = tldcl; |
9837 | TREE_VALUE (ret) = targs; |
9838 | tldcl = ret; |
9839 | targs = NULL; |
9840 | gcc_assert (tree_list_p ()); |
9841 | return ret; |
9842 | } |
9843 | |
9844 | const unsigned short tinst_level::refcount_infinity; |
9845 | |
9846 | /* Increment OBJ's refcount unless it is already infinite. */ |
9847 | static tinst_level * |
9848 | inc_refcount_use (tinst_level *obj) |
9849 | { |
9850 | if (obj && obj->refcount != tinst_level::refcount_infinity) |
9851 | ++obj->refcount; |
9852 | return obj; |
9853 | } |
9854 | |
9855 | /* Release storage for OBJ and node, if it's a TREE_LIST. */ |
9856 | void |
9857 | tinst_level::free (tinst_level *obj) |
9858 | { |
9859 | if (obj->tree_list_p ()) |
9860 | tree_list_freelist ().free (obj: obj->get_node ()); |
9861 | tinst_level_freelist ().free (obj); |
9862 | } |
9863 | |
9864 | /* Decrement OBJ's refcount if not infinite. If it reaches zero, release |
9865 | OBJ's DECL and OBJ, and start over with the tinst_level object that |
9866 | used to be referenced by OBJ's NEXT. */ |
9867 | static void |
9868 | dec_refcount_use (tinst_level *obj) |
9869 | { |
9870 | while (obj |
9871 | && obj->refcount != tinst_level::refcount_infinity |
9872 | && !--obj->refcount) |
9873 | { |
9874 | tinst_level *next = obj->next; |
9875 | tinst_level::free (obj); |
9876 | obj = next; |
9877 | } |
9878 | } |
9879 | |
9880 | /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ |
9881 | and of the former PTR. Omitting the second argument is equivalent |
9882 | to passing (T*)NULL; this is allowed because passing the |
9883 | zero-valued integral constant NULL confuses type deduction and/or |
9884 | overload resolution. */ |
9885 | template <typename T> |
9886 | static void |
9887 | set_refcount_ptr (T *& ptr, T *obj = NULL) |
9888 | { |
9889 | T *save = ptr; |
9890 | ptr = inc_refcount_use (obj); |
9891 | dec_refcount_use (save); |
9892 | } |
9893 | |
9894 | static void |
9895 | add_pending_template (tree d) |
9896 | { |
9897 | tree ti = (TYPE_P (d) |
9898 | ? CLASSTYPE_TEMPLATE_INFO (d) |
9899 | : DECL_TEMPLATE_INFO (d)); |
9900 | struct pending_template *pt; |
9901 | int level; |
9902 | |
9903 | if (TI_PENDING_TEMPLATE_FLAG (ti)) |
9904 | return; |
9905 | |
9906 | /* We are called both from instantiate_decl, where we've already had a |
9907 | tinst_level pushed, and instantiate_template, where we haven't. |
9908 | Compensate. */ |
9909 | gcc_assert (TREE_CODE (d) != TREE_LIST); |
9910 | level = !current_tinst_level |
9911 | || current_tinst_level->maybe_get_node () != d; |
9912 | |
9913 | if (level) |
9914 | push_tinst_level (d); |
9915 | |
9916 | pt = pending_template_freelist ().alloc (); |
9917 | pt->next = NULL; |
9918 | pt->tinst = NULL; |
9919 | set_refcount_ptr (ptr&: pt->tinst, obj: current_tinst_level); |
9920 | if (last_pending_template) |
9921 | last_pending_template->next = pt; |
9922 | else |
9923 | pending_templates = pt; |
9924 | |
9925 | last_pending_template = pt; |
9926 | |
9927 | TI_PENDING_TEMPLATE_FLAG (ti) = 1; |
9928 | |
9929 | if (level) |
9930 | pop_tinst_level (); |
9931 | } |
9932 | |
9933 | /* Emit a diagnostic about instantiating a reference to TU-local entity E. */ |
9934 | |
9935 | static void |
9936 | complain_about_tu_local_entity (tree e) |
9937 | { |
9938 | auto_diagnostic_group d; |
9939 | error ("instantiation exposes TU-local entity %qD", |
9940 | TU_LOCAL_ENTITY_NAME (e)); |
9941 | inform (TU_LOCAL_ENTITY_LOCATION (e), "declared here"); |
9942 | } |
9943 | |
9944 | /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and |
9945 | ARGLIST. Valid choices for FNS are given in the cp-tree.def |
9946 | documentation for TEMPLATE_ID_EXPR. */ |
9947 | |
9948 | tree |
9949 | lookup_template_function (tree fns, tree arglist) |
9950 | { |
9951 | if (fns == error_mark_node || arglist == error_mark_node) |
9952 | return error_mark_node; |
9953 | |
9954 | gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC); |
9955 | |
9956 | if (!is_overloaded_fn (fns) && !identifier_p (t: fns)) |
9957 | { |
9958 | error ("%q#D is not a function template", fns); |
9959 | return error_mark_node; |
9960 | } |
9961 | |
9962 | if (BASELINK_P (fns)) |
9963 | { |
9964 | fns = copy_node (fns); |
9965 | BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR, |
9966 | unknown_type_node, |
9967 | BASELINK_FUNCTIONS (fns), |
9968 | arglist); |
9969 | return fns; |
9970 | } |
9971 | |
9972 | return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist); |
9973 | } |
9974 | |
9975 | /* Within the scope of a template class S<T>, the name S gets bound |
9976 | (in build_self_reference) to a TYPE_DECL for the class, not a |
9977 | TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type, |
9978 | or one of its enclosing classes, and that type is a template, |
9979 | return the associated TEMPLATE_DECL. Otherwise, the original |
9980 | DECL is returned. |
9981 | |
9982 | Also handle the case when DECL is a TREE_LIST of ambiguous |
9983 | injected-class-names from different bases. */ |
9984 | |
9985 | tree |
9986 | maybe_get_template_decl_from_type_decl (tree decl) |
9987 | { |
9988 | if (decl == NULL_TREE) |
9989 | return decl; |
9990 | |
9991 | /* DR 176: A lookup that finds an injected-class-name (10.2 |
9992 | [class.member.lookup]) can result in an ambiguity in certain cases |
9993 | (for example, if it is found in more than one base class). If all of |
9994 | the injected-class-names that are found refer to specializations of |
9995 | the same class template, and if the name is followed by a |
9996 | template-argument-list, the reference refers to the class template |
9997 | itself and not a specialization thereof, and is not ambiguous. */ |
9998 | if (TREE_CODE (decl) == TREE_LIST) |
9999 | { |
10000 | tree t, tmpl = NULL_TREE; |
10001 | for (t = decl; t; t = TREE_CHAIN (t)) |
10002 | { |
10003 | tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t)); |
10004 | if (!tmpl) |
10005 | tmpl = elt; |
10006 | else if (tmpl != elt) |
10007 | break; |
10008 | } |
10009 | if (tmpl && t == NULL_TREE) |
10010 | return tmpl; |
10011 | else |
10012 | return decl; |
10013 | } |
10014 | |
10015 | return (decl != NULL_TREE |
10016 | && DECL_SELF_REFERENCE_P (decl) |
10017 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
10018 | ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl; |
10019 | } |
10020 | |
10021 | /* If TYPE is the generic implicit instantiation A<T>, return the primary |
10022 | template type A<T> (which is suitable for entering into, e.g. for defining |
10023 | a member of A<T>), otherwise return TYPE. */ |
10024 | |
10025 | tree |
10026 | adjust_type_for_entering_scope (tree type) |
10027 | { |
10028 | if (CLASS_TYPE_P (type) |
10029 | && dependent_type_p (type) |
10030 | && TYPE_TEMPLATE_INFO (type) |
10031 | /* We detect the generic implicit instantiation A<T> by inspecting |
10032 | TYPE_CANONICAL, which lookup_template_class sets to the primary |
10033 | template type A<T>. */ |
10034 | && TYPE_CANONICAL (type) == TREE_TYPE (TYPE_TI_TEMPLATE (type))) |
10035 | type = TYPE_CANONICAL (type); |
10036 | |
10037 | return type; |
10038 | } |
10039 | |
10040 | /* Convenience wrapper over tsubst that does adjust_type_for_entering_scope |
10041 | on the result. */ |
10042 | |
10043 | static tree |
10044 | tsubst_entering_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
10045 | { |
10046 | t = tsubst (t, args, complain, in_decl); |
10047 | return adjust_type_for_entering_scope (type: t); |
10048 | } |
10049 | |
10050 | /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of |
10051 | parameters, find the desired type. |
10052 | |
10053 | D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments. |
10054 | |
10055 | If D1 is an identifier and CONTEXT is non-NULL, then the lookup is |
10056 | carried out in CONTEXT. Currently, only namespaces are supported for |
10057 | CONTEXT. |
10058 | |
10059 | If D1 is an identifier and CONTEXT is NULL, the lookup is performed |
10060 | in the innermost non-namespace binding. |
10061 | |
10062 | Otherwise CONTEXT is ignored and no lookup is carried out. |
10063 | |
10064 | IN_DECL, if non-NULL, is the template declaration we are trying to |
10065 | instantiate. |
10066 | |
10067 | Issue error and warning messages under control of COMPLAIN. |
10068 | |
10069 | ??? Note that this function is currently called *twice* for each |
10070 | template-id: the first time from the parser, while creating the |
10071 | incomplete type (finish_template_type), and the second type during the |
10072 | real instantiation (instantiate_template_class). This is surely something |
10073 | that we want to avoid. It also causes some problems with argument |
10074 | coercion (see convert_nontype_argument for more information on this). */ |
10075 | |
10076 | tree |
10077 | lookup_template_class (tree d1, tree arglist, tree in_decl, tree context, |
10078 | tsubst_flags_t complain) |
10079 | { |
10080 | auto_timevar tv (TV_TEMPLATE_INST); |
10081 | |
10082 | tree templ = NULL_TREE, parmlist; |
10083 | tree t; |
10084 | spec_entry **slot; |
10085 | spec_entry *entry; |
10086 | |
10087 | if (identifier_p (t: d1) && context) |
10088 | { |
10089 | gcc_checking_assert (TREE_CODE (context) == NAMESPACE_DECL); |
10090 | push_decl_namespace (context); |
10091 | templ = lookup_name (d1, LOOK_where::NAMESPACE, LOOK_want::NORMAL); |
10092 | pop_decl_namespace (); |
10093 | } |
10094 | else if (identifier_p (t: d1)) |
10095 | { |
10096 | tree value = innermost_non_namespace_value (d1); |
10097 | if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value)) |
10098 | templ = value; |
10099 | else |
10100 | { |
10101 | templ = lookup_name (name: d1); |
10102 | templ = maybe_get_template_decl_from_type_decl (decl: templ); |
10103 | } |
10104 | } |
10105 | else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1))) |
10106 | { |
10107 | tree type = TREE_TYPE (d1); |
10108 | |
10109 | /* If we are declaring a constructor, say A<T>::A<T>, we will get |
10110 | an implicit typename for the second A. Deal with it. */ |
10111 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) |
10112 | type = TREE_TYPE (type); |
10113 | |
10114 | if (CLASSTYPE_TEMPLATE_INFO (type)) |
10115 | { |
10116 | templ = CLASSTYPE_TI_TEMPLATE (type); |
10117 | d1 = DECL_NAME (templ); |
10118 | } |
10119 | } |
10120 | else if (TREE_CODE (d1) == ENUMERAL_TYPE |
10121 | || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1))) |
10122 | { |
10123 | templ = TYPE_TI_TEMPLATE (d1); |
10124 | d1 = DECL_NAME (templ); |
10125 | } |
10126 | else if (DECL_TYPE_TEMPLATE_P (d1)) |
10127 | { |
10128 | templ = d1; |
10129 | d1 = DECL_NAME (templ); |
10130 | } |
10131 | else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1)) |
10132 | { |
10133 | templ = d1; |
10134 | d1 = DECL_NAME (templ); |
10135 | } |
10136 | |
10137 | /* Issue an error message if we didn't find a template. */ |
10138 | if (! templ) |
10139 | { |
10140 | if (complain & tf_error) |
10141 | error ("%qT is not a template", d1); |
10142 | return error_mark_node; |
10143 | } |
10144 | |
10145 | if (TREE_CODE (templ) != TEMPLATE_DECL |
10146 | /* Make sure it's a user visible template, if it was named by |
10147 | the user. */ |
10148 | || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ) |
10149 | && !PRIMARY_TEMPLATE_P (templ))) |
10150 | { |
10151 | if (complain & tf_error) |
10152 | { |
10153 | error ("non-template type %qT used as a template", d1); |
10154 | if (in_decl) |
10155 | error ("for template declaration %q+D", in_decl); |
10156 | } |
10157 | return error_mark_node; |
10158 | } |
10159 | |
10160 | complain &= ~tf_user; |
10161 | |
10162 | /* An alias that just changes the name of a template is equivalent to the |
10163 | other template, so if any of the arguments are pack expansions, strip |
10164 | the alias to avoid problems with a pack expansion passed to a non-pack |
10165 | alias template parameter (DR 1430). */ |
10166 | if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist))) |
10167 | templ = get_underlying_template (tmpl: templ); |
10168 | |
10169 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ)) |
10170 | { |
10171 | tree parm; |
10172 | tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain); |
10173 | if (arglist2 == error_mark_node |
10174 | || (!uses_template_parms (arglist2) |
10175 | && check_instantiated_args (templ, arglist2, complain))) |
10176 | return error_mark_node; |
10177 | |
10178 | parm = bind_template_template_parm (TREE_TYPE (templ), arglist2); |
10179 | return parm; |
10180 | } |
10181 | else |
10182 | { |
10183 | tree template_type = TREE_TYPE (templ); |
10184 | tree gen_tmpl; |
10185 | tree type_decl; |
10186 | tree found = NULL_TREE; |
10187 | int arg_depth; |
10188 | int parm_depth; |
10189 | int is_dependent_type; |
10190 | int use_partial_inst_tmpl = false; |
10191 | |
10192 | if (template_type == error_mark_node) |
10193 | /* An error occurred while building the template TEMPL, and a |
10194 | diagnostic has most certainly been emitted for that |
10195 | already. Let's propagate that error. */ |
10196 | return error_mark_node; |
10197 | |
10198 | gen_tmpl = most_general_template (templ); |
10199 | if (modules_p ()) |
10200 | lazy_load_pendings (decl: gen_tmpl); |
10201 | |
10202 | parmlist = DECL_TEMPLATE_PARMS (gen_tmpl); |
10203 | parm_depth = TMPL_PARMS_DEPTH (parmlist); |
10204 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
10205 | |
10206 | if (arg_depth == 1 && parm_depth > 1) |
10207 | { |
10208 | /* We've been given an incomplete set of template arguments. |
10209 | For example, given: |
10210 | |
10211 | template <class T> struct S1 { |
10212 | template <class U> struct S2 {}; |
10213 | template <class U> struct S2<U*> {}; |
10214 | }; |
10215 | |
10216 | we will be called with an ARGLIST of `U*', but the |
10217 | TEMPLATE will be `template <class T> template |
10218 | <class U> struct S1<T>::S2'. We must fill in the missing |
10219 | arguments. */ |
10220 | tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ)); |
10221 | arglist = add_outermost_template_args (TI_ARGS (ti), extra_args: arglist); |
10222 | arg_depth = TMPL_ARGS_DEPTH (arglist); |
10223 | } |
10224 | |
10225 | /* Now we should have enough arguments. */ |
10226 | gcc_assert (parm_depth == arg_depth); |
10227 | |
10228 | if (DECL_ALIAS_TEMPLATE_P (gen_tmpl)) |
10229 | { |
10230 | /* The user referred to a specialization of an alias |
10231 | template represented by GEN_TMPL. |
10232 | |
10233 | [temp.alias]/2 says: |
10234 | |
10235 | When a template-id refers to the specialization of an |
10236 | alias template, it is equivalent to the associated |
10237 | type obtained by substitution of its |
10238 | template-arguments for the template-parameters in the |
10239 | type-id of the alias template. */ |
10240 | |
10241 | t = instantiate_alias_template (gen_tmpl, arglist, complain); |
10242 | /* Note that the call above (by indirectly calling |
10243 | register_specialization in tsubst_decl) registers the |
10244 | TYPE_DECL representing the specialization of the alias |
10245 | template. So next time someone substitutes ARGLIST for |
10246 | the template parms into the alias template (GEN_TMPL), |
10247 | she'll get that TYPE_DECL back. */ |
10248 | |
10249 | if (t == error_mark_node) |
10250 | return error_mark_node; |
10251 | return TREE_TYPE (t); |
10252 | } |
10253 | |
10254 | /* From here on, we're only interested in the most general |
10255 | template. */ |
10256 | |
10257 | /* Shortcut looking up the current class scope again. */ |
10258 | for (tree cur = current_nonlambda_class_type (); |
10259 | cur != NULL_TREE; |
10260 | cur = get_containing_scope (cur)) |
10261 | { |
10262 | if (!CLASS_TYPE_P (cur)) |
10263 | continue; |
10264 | |
10265 | tree ti = CLASSTYPE_TEMPLATE_INFO (cur); |
10266 | if (!ti || arg_depth > TMPL_ARGS_DEPTH (TI_ARGS (ti))) |
10267 | break; |
10268 | |
10269 | if (gen_tmpl == most_general_template (TI_TEMPLATE (ti)) |
10270 | && comp_template_args (oldargs: arglist, TI_ARGS (ti))) |
10271 | return cur; |
10272 | } |
10273 | |
10274 | /* Calculate the BOUND_ARGS. These will be the args that are |
10275 | actually tsubst'd into the definition to create the |
10276 | instantiation. */ |
10277 | if (PRIMARY_TEMPLATE_P (gen_tmpl)) |
10278 | arglist = coerce_template_parms (parms: parmlist, args: arglist, in_decl: gen_tmpl, complain); |
10279 | |
10280 | if (arglist == error_mark_node) |
10281 | /* We were unable to bind the arguments. */ |
10282 | return error_mark_node; |
10283 | |
10284 | /* In the scope of a template class, explicit references to the |
10285 | template class refer to the type of the template, not any |
10286 | instantiation of it. For example, in: |
10287 | |
10288 | template <class T> class C { void f(C<T>); } |
10289 | |
10290 | the `C<T>' is just the same as `C'. Outside of the |
10291 | class, however, such a reference is an instantiation. |
10292 | One can use adjust_type_for_entering_scope to make |
10293 | this adjustment as needed. */ |
10294 | if (!PRIMARY_TEMPLATE_P (gen_tmpl) |
10295 | || currently_open_class (template_type)) |
10296 | { |
10297 | tree tinfo = TYPE_TEMPLATE_INFO (template_type); |
10298 | |
10299 | if (tinfo && comp_template_args (TI_ARGS (tinfo), newargs: arglist)) |
10300 | return template_type; |
10301 | } |
10302 | |
10303 | /* If we already have this specialization, return it. */ |
10304 | spec_entry elt; |
10305 | elt.tmpl = gen_tmpl; |
10306 | elt.args = arglist; |
10307 | entry = type_specializations->find (value: &elt); |
10308 | |
10309 | if (entry) |
10310 | return entry->spec; |
10311 | |
10312 | /* If the template's constraints are not satisfied, |
10313 | then we cannot form a valid type. |
10314 | |
10315 | Note that the check is deferred until after the hash |
10316 | lookup. This prevents redundant checks on previously |
10317 | instantiated specializations. */ |
10318 | if (flag_concepts |
10319 | && !constraints_satisfied_p (gen_tmpl, arglist)) |
10320 | { |
10321 | if (complain & tf_error) |
10322 | { |
10323 | auto_diagnostic_group d; |
10324 | error ("template constraint failure for %qD", gen_tmpl); |
10325 | diagnose_constraints (input_location, gen_tmpl, arglist); |
10326 | } |
10327 | return error_mark_node; |
10328 | } |
10329 | |
10330 | is_dependent_type = uses_template_parms (arglist); |
10331 | |
10332 | /* If the deduced arguments are invalid, then the binding |
10333 | failed. */ |
10334 | if (!is_dependent_type |
10335 | && check_instantiated_args (gen_tmpl, |
10336 | INNERMOST_TEMPLATE_ARGS (arglist), |
10337 | complain)) |
10338 | return error_mark_node; |
10339 | |
10340 | if (!is_dependent_type |
10341 | && !PRIMARY_TEMPLATE_P (gen_tmpl) |
10342 | && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl)) |
10343 | && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL) |
10344 | /* This occurs when the user has tried to define a tagged type |
10345 | in a scope that forbids it. We emitted an error during the |
10346 | parse. We didn't complete the bail out then, so here we |
10347 | are. */ |
10348 | return error_mark_node; |
10349 | |
10350 | context = DECL_CONTEXT (gen_tmpl); |
10351 | if (context && TYPE_P (context)) |
10352 | { |
10353 | if (!uses_template_parms (DECL_CONTEXT (templ))) |
10354 | /* If the context of the partially instantiated template is |
10355 | already non-dependent, then we might as well use it. */ |
10356 | context = DECL_CONTEXT (templ); |
10357 | else |
10358 | { |
10359 | context = tsubst_entering_scope (t: context, args: arglist, |
10360 | complain, in_decl); |
10361 | /* Try completing the enclosing context if it's not already so. */ |
10362 | if (context != error_mark_node |
10363 | && !COMPLETE_TYPE_P (context)) |
10364 | { |
10365 | context = complete_type (context); |
10366 | if (COMPLETE_TYPE_P (context)) |
10367 | { |
10368 | /* Completion could have caused us to register the desired |
10369 | specialization already, so check the table again. */ |
10370 | entry = type_specializations->find (value: &elt); |
10371 | if (entry) |
10372 | return entry->spec; |
10373 | } |
10374 | } |
10375 | } |
10376 | } |
10377 | else |
10378 | context = tsubst (context, arglist, complain, in_decl); |
10379 | |
10380 | if (context == error_mark_node) |
10381 | return error_mark_node; |
10382 | |
10383 | if (!context) |
10384 | context = global_namespace; |
10385 | |
10386 | /* Create the type. */ |
10387 | if (TREE_CODE (template_type) == ENUMERAL_TYPE) |
10388 | { |
10389 | if (!is_dependent_type) |
10390 | { |
10391 | set_current_access_from_decl (TYPE_NAME (template_type)); |
10392 | t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE, |
10393 | tsubst (ENUM_UNDERLYING_TYPE (template_type), |
10394 | arglist, complain, in_decl), |
10395 | tsubst_attributes (TYPE_ATTRIBUTES (template_type), |
10396 | arglist, complain, in_decl), |
10397 | SCOPED_ENUM_P (template_type), NULL); |
10398 | |
10399 | if (t == error_mark_node) |
10400 | return t; |
10401 | } |
10402 | else |
10403 | { |
10404 | /* We don't want to call start_enum for this type, since |
10405 | the values for the enumeration constants may involve |
10406 | template parameters. And, no one should be interested |
10407 | in the enumeration constants for such a type. */ |
10408 | t = cxx_make_type (ENUMERAL_TYPE); |
10409 | SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type)); |
10410 | } |
10411 | SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type)); |
10412 | ENUM_FIXED_UNDERLYING_TYPE_P (t) |
10413 | = ENUM_FIXED_UNDERLYING_TYPE_P (template_type); |
10414 | } |
10415 | else if (CLASS_TYPE_P (template_type)) |
10416 | { |
10417 | /* Lambda closures are regenerated in tsubst_lambda_expr, not |
10418 | instantiated here. */ |
10419 | gcc_assert (!LAMBDA_TYPE_P (template_type)); |
10420 | |
10421 | t = make_class_type (TREE_CODE (template_type)); |
10422 | CLASSTYPE_DECLARED_CLASS (t) |
10423 | = CLASSTYPE_DECLARED_CLASS (template_type); |
10424 | SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t); |
10425 | |
10426 | /* A local class. Make sure the decl gets registered properly. */ |
10427 | if (context == current_function_decl) |
10428 | if (pushtag (DECL_NAME (gen_tmpl), t) |
10429 | == error_mark_node) |
10430 | return error_mark_node; |
10431 | |
10432 | if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), newargs: arglist)) |
10433 | /* This instantiation is another name for the primary |
10434 | template type. Set the TYPE_CANONICAL field |
10435 | appropriately. */ |
10436 | TYPE_CANONICAL (t) = template_type; |
10437 | else if (any_template_arguments_need_structural_equality_p (arglist)) |
10438 | SET_TYPE_STRUCTURAL_EQUALITY (t); |
10439 | } |
10440 | else |
10441 | gcc_unreachable (); |
10442 | |
10443 | /* If we called start_enum or pushtag above, this information |
10444 | will already be set up. */ |
10445 | type_decl = TYPE_NAME (t); |
10446 | if (!type_decl) |
10447 | { |
10448 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); |
10449 | |
10450 | type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t); |
10451 | DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t); |
10452 | DECL_SOURCE_LOCATION (type_decl) |
10453 | = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type)); |
10454 | } |
10455 | |
10456 | set_instantiating_module (type_decl); |
10457 | /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value |
10458 | of export flag. We want to propagate this because it might |
10459 | be a friend declaration that pushes a new hidden binding. */ |
10460 | DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl); |
10461 | |
10462 | if (CLASS_TYPE_P (template_type)) |
10463 | { |
10464 | TREE_PRIVATE (type_decl) |
10465 | = TREE_PRIVATE (TYPE_MAIN_DECL (template_type)); |
10466 | TREE_PROTECTED (type_decl) |
10467 | = TREE_PROTECTED (TYPE_MAIN_DECL (template_type)); |
10468 | if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type)) |
10469 | { |
10470 | DECL_VISIBILITY_SPECIFIED (type_decl) = 1; |
10471 | DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type); |
10472 | } |
10473 | } |
10474 | |
10475 | if (OVERLOAD_TYPE_P (t)) |
10476 | { |
10477 | static const char *tags[] = {"abi_tag", "may_alias"}; |
10478 | |
10479 | for (unsigned ix = 0; ix != 2; ix++) |
10480 | { |
10481 | tree attributes |
10482 | = lookup_attribute (attr_name: tags[ix], TYPE_ATTRIBUTES (template_type)); |
10483 | |
10484 | if (attributes) |
10485 | TYPE_ATTRIBUTES (t) |
10486 | = tree_cons (TREE_PURPOSE (attributes), |
10487 | TREE_VALUE (attributes), |
10488 | TYPE_ATTRIBUTES (t)); |
10489 | } |
10490 | } |
10491 | |
10492 | /* Let's consider the explicit specialization of a member |
10493 | of a class template specialization that is implicitly instantiated, |
10494 | e.g.: |
10495 | template<class T> |
10496 | struct S |
10497 | { |
10498 | template<class U> struct M {}; //#0 |
10499 | }; |
10500 | |
10501 | template<> |
10502 | template<> |
10503 | struct S<int>::M<char> //#1 |
10504 | { |
10505 | int i; |
10506 | }; |
10507 | [temp.expl.spec]/4 says this is valid. |
10508 | |
10509 | In this case, when we write: |
10510 | S<int>::M<char> m; |
10511 | |
10512 | M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from |
10513 | the one of #0. |
10514 | |
10515 | When we encounter #1, we want to store the partial instantiation |
10516 | of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE. |
10517 | |
10518 | For all cases other than this "explicit specialization of member of a |
10519 | class template", we just want to store the most general template into |
10520 | the CLASSTYPE_TI_TEMPLATE of M. |
10521 | |
10522 | This case of "explicit specialization of member of a class template" |
10523 | only happens when: |
10524 | 1/ the enclosing class is an instantiation of, and therefore not |
10525 | the same as, the context of the most general template, and |
10526 | 2/ we aren't looking at the partial instantiation itself, i.e. |
10527 | the innermost arguments are not the same as the innermost parms of |
10528 | the most general template. |
10529 | |
10530 | So it's only when 1/ and 2/ happens that we want to use the partial |
10531 | instantiation of the member template in lieu of its most general |
10532 | template. */ |
10533 | |
10534 | if (PRIMARY_TEMPLATE_P (gen_tmpl) |
10535 | && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist) |
10536 | /* the enclosing class must be an instantiation... */ |
10537 | && CLASS_TYPE_P (context) |
10538 | && !same_type_p (context, DECL_CONTEXT (gen_tmpl))) |
10539 | { |
10540 | TREE_VEC_LENGTH (arglist)--; |
10541 | ++processing_template_decl; |
10542 | tree tinfo = TYPE_TEMPLATE_INFO (TREE_TYPE (gen_tmpl)); |
10543 | tree partial_inst_args = |
10544 | tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)), |
10545 | arglist, complain, NULL_TREE); |
10546 | --processing_template_decl; |
10547 | TREE_VEC_LENGTH (arglist)++; |
10548 | if (partial_inst_args == error_mark_node) |
10549 | return error_mark_node; |
10550 | use_partial_inst_tmpl = |
10551 | /*...and we must not be looking at the partial instantiation |
10552 | itself. */ |
10553 | !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist), |
10554 | newargs: partial_inst_args); |
10555 | } |
10556 | |
10557 | if (!use_partial_inst_tmpl) |
10558 | /* This case is easy; there are no member templates involved. */ |
10559 | found = gen_tmpl; |
10560 | else |
10561 | { |
10562 | /* This is a full instantiation of a member template. Find |
10563 | the partial instantiation of which this is an instance. */ |
10564 | |
10565 | /* Temporarily reduce by one the number of levels in the ARGLIST |
10566 | so as to avoid comparing the last set of arguments. */ |
10567 | TREE_VEC_LENGTH (arglist)--; |
10568 | /* We don't use COMPLAIN in the following call because this isn't |
10569 | the immediate context of deduction. For instance, tf_partial |
10570 | could be set here as we might be at the beginning of template |
10571 | argument deduction when any explicitly specified template |
10572 | arguments are substituted into the function type. tf_partial |
10573 | could lead into trouble because we wouldn't find the partial |
10574 | instantiation that might have been created outside tf_partial |
10575 | context, because the levels of template parameters wouldn't |
10576 | match, because in a tf_partial context, tsubst doesn't reduce |
10577 | TEMPLATE_PARM_LEVEL. */ |
10578 | found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE); |
10579 | TREE_VEC_LENGTH (arglist)++; |
10580 | found = (TREE_CODE (found) == TEMPLATE_DECL |
10581 | ? found |
10582 | : CLASSTYPE_TI_TEMPLATE (found)); |
10583 | |
10584 | if (DECL_CLASS_TEMPLATE_P (found) |
10585 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found))) |
10586 | { |
10587 | /* If this partial instantiation is specialized, we want to |
10588 | use it for hash table lookup. */ |
10589 | elt.tmpl = found; |
10590 | elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist); |
10591 | elt.hash = 0; /* Recalculate after changing tmpl/args. */ |
10592 | } |
10593 | } |
10594 | |
10595 | /* Build template info for the new specialization. */ |
10596 | SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist)); |
10597 | |
10598 | elt.spec = t; |
10599 | slot = type_specializations->find_slot (value: &elt, insert: INSERT); |
10600 | gcc_checking_assert (*slot == NULL); |
10601 | entry = ggc_alloc<spec_entry> (); |
10602 | *entry = elt; |
10603 | *slot = entry; |
10604 | |
10605 | /* Note this use of the partial instantiation so we can check it |
10606 | later in maybe_process_partial_specialization. */ |
10607 | DECL_TEMPLATE_INSTANTIATIONS (found) |
10608 | = tree_cons (arglist, t, |
10609 | DECL_TEMPLATE_INSTANTIATIONS (found)); |
10610 | |
10611 | if (TREE_CODE (template_type) == ENUMERAL_TYPE |
10612 | && !uses_template_parms (current_nonlambda_scope ())) |
10613 | /* Now that the type has been registered on the instantiations |
10614 | list, we set up the enumerators. Because the enumeration |
10615 | constants may involve the enumeration type itself, we make |
10616 | sure to register the type first, and then create the |
10617 | constants. That way, doing tsubst_expr for the enumeration |
10618 | constants won't result in recursive calls here; we'll find |
10619 | the instantiation and exit above. */ |
10620 | tsubst_enum (template_type, t, arglist); |
10621 | |
10622 | if (CLASS_TYPE_P (template_type) && is_dependent_type) |
10623 | /* If the type makes use of template parameters, the |
10624 | code that generates debugging information will crash. */ |
10625 | DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1; |
10626 | |
10627 | /* Possibly limit visibility based on template args. */ |
10628 | TREE_PUBLIC (type_decl) = 1; |
10629 | determine_visibility (type_decl); |
10630 | |
10631 | inherit_targ_abi_tags (t); |
10632 | |
10633 | return t; |
10634 | } |
10635 | } |
10636 | |
10637 | /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */ |
10638 | |
10639 | tree |
10640 | lookup_template_variable (tree templ, tree arglist, tsubst_flags_t complain) |
10641 | { |
10642 | tree gen_templ = most_general_template (templ); |
10643 | tree parms = DECL_INNERMOST_TEMPLATE_PARMS (gen_templ); |
10644 | arglist = add_outermost_template_args (args: templ, extra_args: arglist); |
10645 | arglist = coerce_template_parms (parms, args: arglist, in_decl: templ, complain); |
10646 | if (arglist == error_mark_node) |
10647 | return error_mark_node; |
10648 | |
10649 | /* The type of the expression is NULL_TREE since the template-id could refer |
10650 | to an explicit or partial specialization. */ |
10651 | return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist); |
10652 | } |
10653 | |
10654 | /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's |
10655 | not dependent. */ |
10656 | |
10657 | tree |
10658 | finish_template_variable (tree var, tsubst_flags_t complain) |
10659 | { |
10660 | tree templ = TREE_OPERAND (var, 0); |
10661 | tree arglist = TREE_OPERAND (var, 1); |
10662 | |
10663 | /* If the template or arguments are dependent, then we |
10664 | can't resolve the TEMPLATE_ID_EXPR yet. */ |
10665 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1 |
10666 | || any_dependent_template_arguments_p (arglist)) |
10667 | return var; |
10668 | |
10669 | if (flag_concepts && !constraints_satisfied_p (templ, arglist)) |
10670 | { |
10671 | if (complain & tf_error) |
10672 | { |
10673 | auto_diagnostic_group d; |
10674 | error ("use of invalid variable template %qE", var); |
10675 | diagnose_constraints (location_of (var), templ, arglist); |
10676 | } |
10677 | return error_mark_node; |
10678 | } |
10679 | |
10680 | return instantiate_template (templ, arglist, complain); |
10681 | } |
10682 | |
10683 | /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having |
10684 | TARGS template args, and instantiate it if it's not dependent. */ |
10685 | |
10686 | tree |
10687 | lookup_and_finish_template_variable (tree templ, tree targs, |
10688 | tsubst_flags_t complain) |
10689 | { |
10690 | tree var = lookup_template_variable (templ, arglist: targs, complain); |
10691 | if (var == error_mark_node) |
10692 | return error_mark_node; |
10693 | var = finish_template_variable (var, complain); |
10694 | mark_used (var, complain); |
10695 | return var; |
10696 | } |
10697 | |
10698 | /* If the set of template parameters PARMS contains a template parameter |
10699 | at the given LEVEL and INDEX, then return this parameter. Otherwise |
10700 | return NULL_TREE. */ |
10701 | |
10702 | static tree |
10703 | corresponding_template_parameter_list (tree parms, int level, int index) |
10704 | { |
10705 | while (TMPL_PARMS_DEPTH (parms) > level) |
10706 | parms = TREE_CHAIN (parms); |
10707 | |
10708 | if (TMPL_PARMS_DEPTH (parms) != level |
10709 | || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index) |
10710 | return NULL_TREE; |
10711 | |
10712 | return TREE_VEC_ELT (TREE_VALUE (parms), index); |
10713 | } |
10714 | |
10715 | /* Return the TREE_LIST for the template parameter from PARMS that positionally |
10716 | corresponds to the template parameter PARM, or else return NULL_TREE. */ |
10717 | |
10718 | static tree |
10719 | corresponding_template_parameter_list (tree parms, tree parm) |
10720 | { |
10721 | int level, index; |
10722 | template_parm_level_and_index (parm, &level, &index); |
10723 | return corresponding_template_parameter_list (parms, level, index); |
10724 | } |
10725 | |
10726 | /* As above, but pull out the actual parameter. */ |
10727 | |
10728 | static tree |
10729 | corresponding_template_parameter (tree parms, tree parm) |
10730 | { |
10731 | tree list = corresponding_template_parameter_list (parms, parm); |
10732 | if (!list) |
10733 | return NULL_TREE; |
10734 | |
10735 | tree t = TREE_VALUE (list); |
10736 | /* As in template_parm_to_arg. */ |
10737 | if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL) |
10738 | t = TREE_TYPE (t); |
10739 | else |
10740 | t = DECL_INITIAL (t); |
10741 | |
10742 | gcc_assert (TEMPLATE_PARM_P (t)); |
10743 | return t; |
10744 | } |
10745 | |
10746 | struct pair_fn_data |
10747 | { |
10748 | tree_fn_t fn; |
10749 | tree_fn_t any_fn; |
10750 | void *data; |
10751 | /* True when we should also visit template parameters that occur in |
10752 | non-deduced contexts. */ |
10753 | bool include_nondeduced_p; |
10754 | hash_set<tree> *visited; |
10755 | }; |
10756 | |
10757 | /* Called from for_each_template_parm via walk_tree. */ |
10758 | |
10759 | static tree |
10760 | for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) |
10761 | { |
10762 | tree t = *tp; |
10763 | struct pair_fn_data *pfd = (struct pair_fn_data *) d; |
10764 | tree_fn_t fn = pfd->fn; |
10765 | void *data = pfd->data; |
10766 | tree result = NULL_TREE; |
10767 | |
10768 | #define WALK_SUBTREE(NODE) \ |
10769 | do \ |
10770 | { \ |
10771 | result = for_each_template_parm (NODE, fn, data, pfd->visited, \ |
10772 | pfd->include_nondeduced_p, \ |
10773 | pfd->any_fn); \ |
10774 | if (result) goto out; \ |
10775 | } \ |
10776 | while (0) |
10777 | |
10778 | if (pfd->any_fn && (*pfd->any_fn)(t, data)) |
10779 | return t; |
10780 | |
10781 | if (TYPE_P (t) |
10782 | && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)) |
10783 | WALK_SUBTREE (TYPE_CONTEXT (t)); |
10784 | |
10785 | switch (TREE_CODE (t)) |
10786 | { |
10787 | case RECORD_TYPE: |
10788 | if (TYPE_PTRMEMFUNC_P (t)) |
10789 | break; |
10790 | /* Fall through. */ |
10791 | |
10792 | case UNION_TYPE: |
10793 | case ENUMERAL_TYPE: |
10794 | if (!TYPE_TEMPLATE_INFO (t)) |
10795 | *walk_subtrees = 0; |
10796 | else |
10797 | WALK_SUBTREE (TYPE_TI_ARGS (t)); |
10798 | break; |
10799 | |
10800 | case METHOD_TYPE: |
10801 | /* Since we're not going to walk subtrees, we have to do this |
10802 | explicitly here. */ |
10803 | WALK_SUBTREE (TYPE_METHOD_BASETYPE (t)); |
10804 | /* Fall through. */ |
10805 | |
10806 | case FUNCTION_TYPE: |
10807 | /* Check the return type. */ |
10808 | WALK_SUBTREE (TREE_TYPE (t)); |
10809 | |
10810 | /* Check the parameter types. Since default arguments are not |
10811 | instantiated until they are needed, the TYPE_ARG_TYPES may |
10812 | contain expressions that involve template parameters. But, |
10813 | no-one should be looking at them yet. And, once they're |
10814 | instantiated, they don't contain template parameters, so |
10815 | there's no point in looking at them then, either. */ |
10816 | { |
10817 | tree parm; |
10818 | |
10819 | for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm)) |
10820 | WALK_SUBTREE (TREE_VALUE (parm)); |
10821 | |
10822 | /* Since we've already handled the TYPE_ARG_TYPES, we don't |
10823 | want walk_tree walking into them itself. */ |
10824 | *walk_subtrees = 0; |
10825 | } |
10826 | |
10827 | if (flag_noexcept_type) |
10828 | { |
10829 | tree spec = TYPE_RAISES_EXCEPTIONS (t); |
10830 | if (spec) |
10831 | WALK_SUBTREE (TREE_PURPOSE (spec)); |
10832 | } |
10833 | break; |
10834 | |
10835 | case TYPEOF_TYPE: |
10836 | case DECLTYPE_TYPE: |
10837 | if (pfd->include_nondeduced_p |
10838 | && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data, |
10839 | pfd->visited, |
10840 | pfd->include_nondeduced_p, |
10841 | pfd->any_fn)) |
10842 | return error_mark_node; |
10843 | *walk_subtrees = false; |
10844 | break; |
10845 | |
10846 | case TRAIT_TYPE: |
10847 | if (pfd->include_nondeduced_p) |
10848 | { |
10849 | WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t)); |
10850 | WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t)); |
10851 | } |
10852 | *walk_subtrees = false; |
10853 | break; |
10854 | |
10855 | case FUNCTION_DECL: |
10856 | case VAR_DECL: |
10857 | if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
10858 | WALK_SUBTREE (DECL_TI_ARGS (t)); |
10859 | break; |
10860 | |
10861 | case PARM_DECL: |
10862 | WALK_SUBTREE (TREE_TYPE (t)); |
10863 | break; |
10864 | |
10865 | case CONST_DECL: |
10866 | if (DECL_TEMPLATE_PARM_P (t)) |
10867 | WALK_SUBTREE (DECL_INITIAL (t)); |
10868 | if (DECL_CONTEXT (t) |
10869 | && pfd->include_nondeduced_p) |
10870 | WALK_SUBTREE (DECL_CONTEXT (t)); |
10871 | break; |
10872 | |
10873 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
10874 | /* Record template parameters such as `T' inside `TT<T>'. */ |
10875 | WALK_SUBTREE (TYPE_TI_ARGS (t)); |
10876 | /* Fall through. */ |
10877 | |
10878 | case TEMPLATE_TEMPLATE_PARM: |
10879 | case TEMPLATE_TYPE_PARM: |
10880 | case TEMPLATE_PARM_INDEX: |
10881 | if (fn && (*fn)(t, data)) |
10882 | return t; |
10883 | else if (!fn) |
10884 | return t; |
10885 | break; |
10886 | |
10887 | case TEMPLATE_DECL: |
10888 | /* A template template parameter is encountered. */ |
10889 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
10890 | WALK_SUBTREE (TREE_TYPE (t)); |
10891 | |
10892 | /* Already substituted template template parameter */ |
10893 | *walk_subtrees = 0; |
10894 | break; |
10895 | |
10896 | case TYPENAME_TYPE: |
10897 | /* A template-id in a TYPENAME_TYPE might be a deduced context after |
10898 | partial instantiation. */ |
10899 | WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t)); |
10900 | *walk_subtrees = 0; |
10901 | break; |
10902 | |
10903 | case INDIRECT_REF: |
10904 | case COMPONENT_REF: |
10905 | /* If there's no type, then this thing must be some expression |
10906 | involving template parameters. */ |
10907 | if (!fn && !TREE_TYPE (t)) |
10908 | return error_mark_node; |
10909 | break; |
10910 | |
10911 | case CONSTRUCTOR: |
10912 | case TRAIT_EXPR: |
10913 | case PLUS_EXPR: |
10914 | case MULT_EXPR: |
10915 | case SCOPE_REF: |
10916 | /* These are non-deduced contexts. */ |
10917 | if (!pfd->include_nondeduced_p) |
10918 | *walk_subtrees = 0; |
10919 | break; |
10920 | |
10921 | case MODOP_EXPR: |
10922 | case CAST_EXPR: |
10923 | case IMPLICIT_CONV_EXPR: |
10924 | case REINTERPRET_CAST_EXPR: |
10925 | case CONST_CAST_EXPR: |
10926 | case STATIC_CAST_EXPR: |
10927 | case DYNAMIC_CAST_EXPR: |
10928 | case ARROW_EXPR: |
10929 | case DOTSTAR_EXPR: |
10930 | case TYPEID_EXPR: |
10931 | case PSEUDO_DTOR_EXPR: |
10932 | if (!fn) |
10933 | return error_mark_node; |
10934 | break; |
10935 | |
10936 | default: |
10937 | break; |
10938 | } |
10939 | |
10940 | #undef WALK_SUBTREE |
10941 | |
10942 | /* We didn't find any template parameters we liked. */ |
10943 | out: |
10944 | return result; |
10945 | } |
10946 | |
10947 | /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, |
10948 | BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T, |
10949 | call FN with the parameter and the DATA. |
10950 | If FN returns nonzero, the iteration is terminated, and |
10951 | for_each_template_parm returns 1. Otherwise, the iteration |
10952 | continues. If FN never returns a nonzero value, the value |
10953 | returned by for_each_template_parm is 0. If FN is NULL, it is |
10954 | considered to be the function which always returns 1. |
10955 | |
10956 | If INCLUDE_NONDEDUCED_P, then this routine will also visit template |
10957 | parameters that occur in non-deduced contexts. When false, only |
10958 | visits those template parameters that can be deduced. */ |
10959 | |
10960 | static tree |
10961 | for_each_template_parm (tree t, tree_fn_t fn, void* data, |
10962 | hash_set<tree> *visited, |
10963 | bool include_nondeduced_p, |
10964 | tree_fn_t any_fn) |
10965 | { |
10966 | struct pair_fn_data pfd; |
10967 | tree result; |
10968 | |
10969 | /* Set up. */ |
10970 | pfd.fn = fn; |
10971 | pfd.any_fn = any_fn; |
10972 | pfd.data = data; |
10973 | pfd.include_nondeduced_p = include_nondeduced_p; |
10974 | |
10975 | /* Walk the tree. (Conceptually, we would like to walk without |
10976 | duplicates, but for_each_template_parm_r recursively calls |
10977 | for_each_template_parm, so we would need to reorganize a fair |
10978 | bit to use walk_tree_without_duplicates, so we keep our own |
10979 | visited list.) */ |
10980 | if (visited) |
10981 | pfd.visited = visited; |
10982 | else |
10983 | pfd.visited = new hash_set<tree>; |
10984 | result = cp_walk_tree (&t, |
10985 | for_each_template_parm_r, |
10986 | &pfd, |
10987 | pfd.visited); |
10988 | |
10989 | /* Clean up. */ |
10990 | if (!visited) |
10991 | { |
10992 | delete pfd.visited; |
10993 | pfd.visited = 0; |
10994 | } |
10995 | |
10996 | return result; |
10997 | } |
10998 | |
10999 | struct find_template_parameter_info |
11000 | { |
11001 | explicit find_template_parameter_info (tree ctx_parms) |
11002 | : ctx_parms (ctx_parms), |
11003 | max_depth (TMPL_PARMS_DEPTH (ctx_parms)) |
11004 | {} |
11005 | |
11006 | hash_set<tree> visited; |
11007 | hash_set<tree> parms; |
11008 | tree parm_list = NULL_TREE; |
11009 | tree *parm_list_tail = &parm_list; |
11010 | tree ctx_parms; |
11011 | int max_depth; |
11012 | |
11013 | tree find_in (tree); |
11014 | tree find_in_recursive (tree); |
11015 | bool found (tree); |
11016 | unsigned num_found () { return parms.elements (); } |
11017 | }; |
11018 | |
11019 | /* Appends the declaration of T to the list in DATA. */ |
11020 | |
11021 | static int |
11022 | keep_template_parm (tree t, void* data) |
11023 | { |
11024 | find_template_parameter_info *ftpi = (find_template_parameter_info*)data; |
11025 | |
11026 | /* Template parameters declared within the expression are not part of |
11027 | the parameter mapping. For example, in this concept: |
11028 | |
11029 | template<typename T> |
11030 | concept C = requires { <expr> } -> same_as<int>; |
11031 | |
11032 | the return specifier same_as<int> declares a new decltype parameter |
11033 | that must not be part of the parameter mapping. The same is true |
11034 | for generic lambda parameters, lambda template parameters, etc. */ |
11035 | int level; |
11036 | int index; |
11037 | template_parm_level_and_index (t, &level, &index); |
11038 | if (level == 0 || level > ftpi->max_depth) |
11039 | return 0; |
11040 | |
11041 | if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) |
11042 | /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the |
11043 | BOUND_TEMPLATE_TEMPLATE_PARM itself. */ |
11044 | t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t)); |
11045 | |
11046 | /* This template parameter might be an argument to a cached dependent |
11047 | specalization that was formed earlier inside some other template, in |
11048 | which case the parameter is not among the ones that are in-scope. |
11049 | Look in CTX_PARMS to find the corresponding in-scope template |
11050 | parameter, and use it instead. */ |
11051 | if (tree in_scope = corresponding_template_parameter (parms: ftpi->ctx_parms, parm: t)) |
11052 | t = in_scope; |
11053 | |
11054 | /* Arguments like const T yield parameters like const T. This means that |
11055 | a template-id like X<T, const T> would yield two distinct parameters: |
11056 | T and const T. Adjust types to their unqualified versions. */ |
11057 | if (TYPE_P (t)) |
11058 | t = TYPE_MAIN_VARIANT (t); |
11059 | if (!ftpi->parms.add (k: t)) |
11060 | { |
11061 | /* Append T to PARM_LIST. */ |
11062 | tree node = build_tree_list (NULL_TREE, t); |
11063 | *ftpi->parm_list_tail = node; |
11064 | ftpi->parm_list_tail = &TREE_CHAIN (node); |
11065 | } |
11066 | |
11067 | /* Verify the parameter we found has a valid index. */ |
11068 | if (flag_checking) |
11069 | { |
11070 | tree parms = ftpi->ctx_parms; |
11071 | while (TMPL_PARMS_DEPTH (parms) > level) |
11072 | parms = TREE_CHAIN (parms); |
11073 | if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms))) |
11074 | gcc_assert (index < len); |
11075 | } |
11076 | |
11077 | return 0; |
11078 | } |
11079 | |
11080 | /* Ensure that we recursively examine certain terms that are not normally |
11081 | visited in for_each_template_parm_r. */ |
11082 | |
11083 | static int |
11084 | any_template_parm_r (tree t, void *data) |
11085 | { |
11086 | find_template_parameter_info *ftpi = (find_template_parameter_info*)data; |
11087 | |
11088 | #define WALK_SUBTREE(NODE) \ |
11089 | do \ |
11090 | { \ |
11091 | for_each_template_parm (NODE, keep_template_parm, data, \ |
11092 | &ftpi->visited, true, \ |
11093 | any_template_parm_r); \ |
11094 | } \ |
11095 | while (0) |
11096 | |
11097 | /* A mention of a member alias/typedef is a use of all of its template |
11098 | arguments, including those from the enclosing class, so we don't use |
11099 | alias_template_specialization_p here. */ |
11100 | if (TYPE_P (t) && typedef_variant_p (type: t)) |
11101 | if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t)) |
11102 | WALK_SUBTREE (TI_ARGS (tinfo)); |
11103 | |
11104 | switch (TREE_CODE (t)) |
11105 | { |
11106 | case TEMPLATE_TYPE_PARM: |
11107 | /* Type constraints of a placeholder type may contain parameters. */ |
11108 | if (is_auto (t)) |
11109 | if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t)) |
11110 | WALK_SUBTREE (constr); |
11111 | break; |
11112 | |
11113 | case TEMPLATE_ID_EXPR: |
11114 | /* Search through references to variable templates. */ |
11115 | WALK_SUBTREE (TREE_OPERAND (t, 0)); |
11116 | WALK_SUBTREE (TREE_OPERAND (t, 1)); |
11117 | break; |
11118 | |
11119 | case TEMPLATE_PARM_INDEX: |
11120 | WALK_SUBTREE (TREE_TYPE (t)); |
11121 | break; |
11122 | |
11123 | case TEMPLATE_DECL: |
11124 | /* If T is a member template that shares template parameters with |
11125 | ctx_parms, we need to mark all those parameters for mapping. |
11126 | To that end, it should suffice to just walk the DECL_CONTEXT of |
11127 | the template (assuming the template is not overly general). */ |
11128 | WALK_SUBTREE (DECL_CONTEXT (t)); |
11129 | break; |
11130 | |
11131 | case LAMBDA_EXPR: |
11132 | { |
11133 | /* TREE_STATIC on LAMBDA_EXPR_EXTRA_ARGS means a full set of |
11134 | arguments, so we can just look there; they will replace |
11135 | any template parms in the rest of the LAMBDA_EXPR. */ |
11136 | if (tree args = LAMBDA_EXPR_EXTRA_ARGS (t)) |
11137 | { |
11138 | WALK_SUBTREE (args); |
11139 | /* Without TREE_STATIC the args are just outer levels, so we'd |
11140 | still need to look through the lambda for just inner |
11141 | parameters. Hopefully that's not necessary. */ |
11142 | gcc_checking_assert (TREE_STATIC (args)); |
11143 | return 0; |
11144 | } |
11145 | /* Look in the parms and body. */ |
11146 | tree fn = lambda_function (t); |
11147 | WALK_SUBTREE (TREE_TYPE (fn)); |
11148 | WALK_SUBTREE (DECL_SAVED_TREE (fn)); |
11149 | } |
11150 | break; |
11151 | |
11152 | case IDENTIFIER_NODE: |
11153 | if (IDENTIFIER_CONV_OP_P (t)) |
11154 | /* The conversion-type-id of a conversion operator may be dependent. */ |
11155 | WALK_SUBTREE (TREE_TYPE (t)); |
11156 | break; |
11157 | |
11158 | case CONVERT_EXPR: |
11159 | if (is_dummy_object (t)) |
11160 | WALK_SUBTREE (TREE_TYPE (t)); |
11161 | break; |
11162 | |
11163 | default: |
11164 | break; |
11165 | } |
11166 | |
11167 | /* Keep walking. */ |
11168 | return 0; |
11169 | } |
11170 | |
11171 | /* Look through T for template parameters. */ |
11172 | |
11173 | tree |
11174 | find_template_parameter_info::find_in (tree t) |
11175 | { |
11176 | return for_each_template_parm (t, fn: keep_template_parm, data: this, visited: &visited, |
11177 | /*include_nondeduced*/include_nondeduced_p: true, |
11178 | any_fn: any_template_parm_r); |
11179 | } |
11180 | |
11181 | /* As above, but also recursively look into the default arguments of template |
11182 | parameters we found. Used for alias CTAD. */ |
11183 | |
11184 | tree |
11185 | find_template_parameter_info::find_in_recursive (tree t) |
11186 | { |
11187 | if (tree r = find_in (t)) |
11188 | return r; |
11189 | /* Since newly found parms are added to the end of the list, we |
11190 | can just walk it until we reach the end. */ |
11191 | for (tree pl = parm_list; pl; pl = TREE_CHAIN (pl)) |
11192 | { |
11193 | tree parm = TREE_VALUE (pl); |
11194 | tree list = corresponding_template_parameter_list (parms: ctx_parms, parm); |
11195 | if (tree r = find_in (TREE_PURPOSE (list))) |
11196 | return r; |
11197 | } |
11198 | return NULL_TREE; |
11199 | } |
11200 | |
11201 | /* True if PARM was found by a previous call to find_in. PARM can be a |
11202 | TREE_LIST, a DECL_TEMPLATE_PARM_P, or a TEMPLATE_PARM_P. */ |
11203 | |
11204 | bool |
11205 | find_template_parameter_info::found (tree parm) |
11206 | { |
11207 | if (TREE_CODE (parm) == TREE_LIST) |
11208 | parm = TREE_VALUE (parm); |
11209 | if (TREE_CODE (parm) == TYPE_DECL |
11210 | || TREE_CODE (parm) == TEMPLATE_DECL) |
11211 | parm = TREE_TYPE (parm); |
11212 | else |
11213 | parm = DECL_INITIAL (parm); |
11214 | gcc_checking_assert (TEMPLATE_PARM_P (parm)); |
11215 | return parms.contains (k: parm); |
11216 | } |
11217 | |
11218 | /* Returns a list of unique template parameters found within T, where CTX_PARMS |
11219 | are the template parameters in scope. */ |
11220 | |
11221 | tree |
11222 | find_template_parameters (tree t, tree ctx_parms) |
11223 | { |
11224 | if (!ctx_parms) |
11225 | return NULL_TREE; |
11226 | |
11227 | find_template_parameter_info ftpi (ctx_parms); |
11228 | ftpi.find_in (t); |
11229 | return ftpi.parm_list; |
11230 | } |
11231 | |
11232 | /* Returns true if T depends on any template parameter. */ |
11233 | |
11234 | bool |
11235 | uses_template_parms (tree t) |
11236 | { |
11237 | if (t == NULL_TREE || t == error_mark_node) |
11238 | return false; |
11239 | |
11240 | /* Namespaces can't depend on any template parameters. */ |
11241 | if (TREE_CODE (t) == NAMESPACE_DECL) |
11242 | return false; |
11243 | |
11244 | processing_template_decl_sentinel ptds (/*reset*/false); |
11245 | ++processing_template_decl; |
11246 | |
11247 | if (TYPE_P (t)) |
11248 | return dependent_type_p (t); |
11249 | else if (TREE_CODE (t) == TREE_VEC) |
11250 | return any_dependent_template_arguments_p (t); |
11251 | else if (TREE_CODE (t) == TREE_LIST) |
11252 | return (uses_template_parms (TREE_VALUE (t)) |
11253 | || uses_template_parms (TREE_CHAIN (t))); |
11254 | else if (TREE_CODE (t) == TYPE_DECL) |
11255 | return dependent_type_p (TREE_TYPE (t)); |
11256 | else |
11257 | return instantiation_dependent_expression_p (t); |
11258 | } |
11259 | |
11260 | /* Returns true if T depends on any template parameter with level LEVEL. */ |
11261 | |
11262 | bool |
11263 | uses_template_parms_level (tree t, int level) |
11264 | { |
11265 | return for_each_template_parm (t, fn: template_parm_this_level_p, data: &level, NULL, |
11266 | /*include_nondeduced_p=*/true); |
11267 | } |
11268 | |
11269 | /* Returns true if the signature of DECL depends on any template parameter from |
11270 | its enclosing class. */ |
11271 | |
11272 | static bool |
11273 | uses_outer_template_parms (tree decl) |
11274 | { |
11275 | int depth; |
11276 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl)) |
11277 | depth = TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)) - 1; |
11278 | else |
11279 | depth = template_class_depth (CP_DECL_CONTEXT (decl)); |
11280 | if (depth == 0) |
11281 | return false; |
11282 | if (for_each_template_parm (TREE_TYPE (decl), fn: template_parm_outer_level, |
11283 | data: &depth, NULL, /*include_nondeduced_p=*/true)) |
11284 | return true; |
11285 | if (PRIMARY_TEMPLATE_P (decl) |
11286 | || DECL_TEMPLATE_TEMPLATE_PARM_P (decl)) |
11287 | { |
11288 | tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl)); |
11289 | for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i) |
11290 | { |
11291 | tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); |
11292 | tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i)); |
11293 | if (TREE_CODE (parm) == PARM_DECL |
11294 | && for_each_template_parm (TREE_TYPE (parm), |
11295 | fn: template_parm_outer_level, |
11296 | data: &depth, NULL, /*nondeduced*/include_nondeduced_p: true)) |
11297 | return true; |
11298 | if (TREE_CODE (parm) == TEMPLATE_DECL |
11299 | && uses_outer_template_parms (decl: parm)) |
11300 | return true; |
11301 | if (defarg |
11302 | && for_each_template_parm (t: defarg, fn: template_parm_outer_level, |
11303 | data: &depth, NULL, /*nondeduced*/include_nondeduced_p: true)) |
11304 | return true; |
11305 | } |
11306 | } |
11307 | if (uses_outer_template_parms_in_constraints (decl)) |
11308 | return true; |
11309 | return false; |
11310 | } |
11311 | |
11312 | /* Returns true if the constraints of DECL depend on any template parameters |
11313 | from its enclosing scope. */ |
11314 | |
11315 | bool |
11316 | uses_outer_template_parms_in_constraints (tree decl, tree ctx/*=NULL_TREE*/) |
11317 | { |
11318 | tree ci = get_constraints (decl); |
11319 | if (ci) |
11320 | ci = CI_ASSOCIATED_CONSTRAINTS (ci); |
11321 | if (!ci) |
11322 | return false; |
11323 | if (!ctx) |
11324 | { |
11325 | if (tree fc = DECL_FRIEND_CONTEXT (decl)) |
11326 | ctx = fc; |
11327 | else |
11328 | ctx = CP_DECL_CONTEXT (decl); |
11329 | } |
11330 | int depth = template_class_depth (type: ctx); |
11331 | if (depth == 0) |
11332 | return false; |
11333 | return for_each_template_parm (t: ci, fn: template_parm_outer_level, |
11334 | data: &depth, NULL, /*nondeduced*/include_nondeduced_p: true); |
11335 | } |
11336 | |
11337 | /* Returns TRUE iff INST is an instantiation we don't need to do in an |
11338 | ill-formed translation unit, i.e. a variable or function that isn't |
11339 | usable in a constant expression. */ |
11340 | |
11341 | static inline bool |
11342 | neglectable_inst_p (tree d) |
11343 | { |
11344 | return (d && DECL_P (d) |
11345 | && !undeduced_auto_decl (d) |
11346 | && !(TREE_CODE (d) == FUNCTION_DECL |
11347 | ? FNDECL_MANIFESTLY_CONST_EVALUATED (d) |
11348 | : decl_maybe_constant_var_p (d))); |
11349 | } |
11350 | |
11351 | /* Returns TRUE iff we should refuse to instantiate DECL because it's |
11352 | neglectable and instantiated from within an erroneous instantiation. */ |
11353 | |
11354 | static bool |
11355 | limit_bad_template_recursion (tree decl) |
11356 | { |
11357 | struct tinst_level *lev = current_tinst_level; |
11358 | int errs = errorcount + sorrycount; |
11359 | if (errs == 0 || !neglectable_inst_p (d: decl)) |
11360 | return false; |
11361 | |
11362 | /* Avoid instantiating members of an ill-formed class. */ |
11363 | bool refuse |
11364 | = (DECL_CLASS_SCOPE_P (decl) |
11365 | && CLASSTYPE_ERRONEOUS (DECL_CONTEXT (decl))); |
11366 | |
11367 | if (!refuse) |
11368 | { |
11369 | for (; lev; lev = lev->next) |
11370 | if (neglectable_inst_p (d: lev->maybe_get_node ())) |
11371 | break; |
11372 | refuse = (lev && errs > lev->errors); |
11373 | } |
11374 | |
11375 | if (refuse) |
11376 | { |
11377 | /* Don't warn about it not being defined. */ |
11378 | suppress_warning (decl, OPT_Wunused); |
11379 | tree clone; |
11380 | FOR_EACH_CLONE (clone, decl) |
11381 | suppress_warning (clone, OPT_Wunused); |
11382 | } |
11383 | return refuse; |
11384 | } |
11385 | |
11386 | static int tinst_depth; |
11387 | extern int max_tinst_depth; |
11388 | int depth_reached; |
11389 | int tinst_dump_id; |
11390 | |
11391 | static GTY(()) struct tinst_level *last_error_tinst_level; |
11392 | |
11393 | /* We're starting to instantiate D; record the template instantiation context |
11394 | at LOC for diagnostics and to restore it later. */ |
11395 | |
11396 | bool |
11397 | push_tinst_level_loc (tree tldcl, tree targs, location_t loc) |
11398 | { |
11399 | struct tinst_level *new_level; |
11400 | |
11401 | if (tinst_depth >= max_tinst_depth) |
11402 | { |
11403 | /* Tell error.cc not to try to instantiate any templates. */ |
11404 | at_eof = 3; |
11405 | fatal_error (input_location, |
11406 | "template instantiation depth exceeds maximum of %d" |
11407 | " (use %<-ftemplate-depth=%> to increase the maximum)", |
11408 | max_tinst_depth); |
11409 | return false; |
11410 | } |
11411 | |
11412 | /* If the current instantiation caused problems, don't let it instantiate |
11413 | anything else. Do allow deduction substitution and decls usable in |
11414 | constant expressions. */ |
11415 | if (!targs && limit_bad_template_recursion (decl: tldcl)) |
11416 | { |
11417 | /* Avoid no_linkage_errors and unused function (and all other) |
11418 | warnings for this decl. */ |
11419 | suppress_warning (tldcl); |
11420 | return false; |
11421 | } |
11422 | |
11423 | /* When not -quiet, dump template instantiations other than functions, since |
11424 | announce_function will take care of those. */ |
11425 | if (!quiet_flag && !targs |
11426 | && TREE_CODE (tldcl) != TREE_LIST |
11427 | && TREE_CODE (tldcl) != FUNCTION_DECL) |
11428 | fprintf (stderr, format: " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS)); |
11429 | |
11430 | new_level = tinst_level_freelist ().alloc (); |
11431 | new_level->tldcl = tldcl; |
11432 | new_level->targs = targs; |
11433 | new_level->locus = loc; |
11434 | new_level->errors = errorcount + sorrycount; |
11435 | new_level->had_errors = false; |
11436 | new_level->next = NULL; |
11437 | new_level->refcount = 0; |
11438 | new_level->path = new_level->visible = nullptr; |
11439 | set_refcount_ptr (ptr&: new_level->next, obj: current_tinst_level); |
11440 | set_refcount_ptr (ptr&: current_tinst_level, obj: new_level); |
11441 | |
11442 | if (cxx_dump_pretty_printer pp {tinst_dump_id}) |
11443 | { |
11444 | #if __GNUC__ >= 10 |
11445 | #pragma GCC diagnostic push |
11446 | #pragma GCC diagnostic ignored "-Wformat-diag" |
11447 | #endif |
11448 | bool list_p = new_level->list_p (); |
11449 | if (list_p && !pp.has_flag (f: TDF_DETAILS)) |
11450 | /* Skip non-instantiations unless -details. */; |
11451 | else |
11452 | { |
11453 | if (tinst_depth == 0) |
11454 | pp_newline (&pp); |
11455 | if (loc && pp.has_flag (f: TDF_LINENO)) |
11456 | { |
11457 | for (int i = 0; i < tinst_depth; ++i) |
11458 | pp_space (&pp); |
11459 | const expanded_location el = expand_location (loc); |
11460 | pp_printf (&pp, "%s:%d:%d", el.file, el.line, el.column); |
11461 | pp_newline (&pp); |
11462 | } |
11463 | for (int i = 0; i < tinst_depth; ++i) |
11464 | pp_space (&pp); |
11465 | if (list_p) |
11466 | pp_printf (&pp, "S %S", new_level->get_node ()); |
11467 | else |
11468 | pp_printf (&pp, "I %D", tldcl); |
11469 | pp_newline (&pp); |
11470 | } |
11471 | #if __GNUC__ >= 10 |
11472 | #pragma GCC diagnostic pop |
11473 | #endif |
11474 | } |
11475 | |
11476 | ++tinst_depth; |
11477 | if (GATHER_STATISTICS && (tinst_depth > depth_reached)) |
11478 | depth_reached = tinst_depth; |
11479 | |
11480 | return true; |
11481 | } |
11482 | |
11483 | /* We're starting substitution of TMPL<ARGS>; record the template |
11484 | substitution context for diagnostics and to restore it later. */ |
11485 | |
11486 | bool |
11487 | push_tinst_level (tree tmpl, tree args) |
11488 | { |
11489 | return push_tinst_level_loc (tldcl: tmpl, targs: args, loc: input_location); |
11490 | } |
11491 | |
11492 | /* We're starting to instantiate D; record INPUT_LOCATION and the |
11493 | template instantiation context for diagnostics and to restore it |
11494 | later. */ |
11495 | |
11496 | bool |
11497 | push_tinst_level (tree d) |
11498 | { |
11499 | return push_tinst_level_loc (d, input_location); |
11500 | } |
11501 | |
11502 | /* Likewise, but record LOC as the program location. */ |
11503 | |
11504 | bool |
11505 | push_tinst_level_loc (tree d, location_t loc) |
11506 | { |
11507 | gcc_assert (TREE_CODE (d) != TREE_LIST); |
11508 | return push_tinst_level_loc (tldcl: d, NULL, loc); |
11509 | } |
11510 | |
11511 | /* We're done instantiating this template; return to the instantiation |
11512 | context. */ |
11513 | |
11514 | void |
11515 | pop_tinst_level (void) |
11516 | { |
11517 | /* Restore the filename and line number stashed away when we started |
11518 | this instantiation. */ |
11519 | input_location = current_tinst_level->locus; |
11520 | if (unsigned errs = errorcount + sorrycount) |
11521 | if (errs > current_tinst_level->errors) |
11522 | current_tinst_level->had_errors = true; |
11523 | set_refcount_ptr (ptr&: current_tinst_level, obj: current_tinst_level->next); |
11524 | --tinst_depth; |
11525 | } |
11526 | |
11527 | /* True if the instantiation represented by LEVEL is complete. */ |
11528 | |
11529 | static bool |
11530 | tinst_complete_p (struct tinst_level *level) |
11531 | { |
11532 | gcc_assert (!level->list_p ()); |
11533 | tree node = level->get_node (); |
11534 | if (TYPE_P (node)) |
11535 | return COMPLETE_TYPE_P (node); |
11536 | else |
11537 | return (DECL_TEMPLATE_INSTANTIATED (node) |
11538 | || DECL_TEMPLATE_SPECIALIZATION (node)); |
11539 | } |
11540 | |
11541 | /* We're instantiating a deferred template; restore the template |
11542 | instantiation context in which the instantiation was requested, which |
11543 | is one step out from LEVEL. Return the corresponding DECL or TYPE. */ |
11544 | |
11545 | static tree |
11546 | reopen_tinst_level (struct tinst_level *level) |
11547 | { |
11548 | struct tinst_level *t; |
11549 | |
11550 | tinst_depth = 0; |
11551 | for (t = level; t; t = t->next) |
11552 | ++tinst_depth; |
11553 | |
11554 | set_refcount_ptr (ptr&: current_tinst_level, obj: level); |
11555 | pop_tinst_level (); |
11556 | if (current_tinst_level && !current_tinst_level->had_errors) |
11557 | current_tinst_level->errors = errorcount+sorrycount; |
11558 | |
11559 | if (cxx_dump_pretty_printer pp {tinst_dump_id}) |
11560 | { |
11561 | #if __GNUC__ >= 10 |
11562 | #pragma GCC diagnostic push |
11563 | #pragma GCC diagnostic ignored "-Wformat-diag" |
11564 | #endif |
11565 | /* Dump the reopened instantiation context. */ |
11566 | t = current_tinst_level; |
11567 | if (!pp.has_flag (f: TDF_DETAILS)) |
11568 | /* Skip non-instantiations unless -details. */ |
11569 | while (t && t->list_p ()) |
11570 | t = t->next; |
11571 | if (t) |
11572 | { |
11573 | static tree last_ctx = NULL_TREE; |
11574 | tree ctx = t->get_node (); |
11575 | if (ctx != last_ctx) |
11576 | { |
11577 | last_ctx = ctx; |
11578 | pp_newline (&pp); |
11579 | if (t->list_p ()) |
11580 | pp_printf (&pp, "RS %S", ctx); |
11581 | else |
11582 | pp_printf (&pp, "RI %D", ctx); |
11583 | pp_newline (&pp); |
11584 | } |
11585 | } |
11586 | #if __GNUC__ >= 10 |
11587 | #pragma GCC diagnostic pop |
11588 | #endif |
11589 | } |
11590 | |
11591 | tree decl = level->maybe_get_node (); |
11592 | if (decl && modules_p ()) |
11593 | { |
11594 | /* An instantiation is in module purview only if it had an explicit |
11595 | instantiation definition in module purview; mark_decl_instantiated uses |
11596 | set_instantiating_module to set the flag in that case. */ |
11597 | if (DECL_MODULE_PURVIEW_P (decl)) |
11598 | module_kind |= MK_PURVIEW; |
11599 | else |
11600 | module_kind &= ~MK_PURVIEW; |
11601 | } |
11602 | return decl; |
11603 | } |
11604 | |
11605 | /* Returns the TINST_LEVEL which gives the original instantiation |
11606 | context. */ |
11607 | |
11608 | struct tinst_level * |
11609 | outermost_tinst_level (void) |
11610 | { |
11611 | struct tinst_level *level = current_tinst_level; |
11612 | if (level) |
11613 | while (level->next) |
11614 | level = level->next; |
11615 | return level; |
11616 | } |
11617 | |
11618 | /* True iff T is a friend function declaration that is not itself a template |
11619 | and is not defined in a class template. */ |
11620 | |
11621 | bool |
11622 | non_templated_friend_p (tree t) |
11623 | { |
11624 | if (t && TREE_CODE (t) == FUNCTION_DECL |
11625 | && DECL_UNIQUE_FRIEND_P (t)) |
11626 | { |
11627 | tree ti = DECL_TEMPLATE_INFO (t); |
11628 | if (!ti) |
11629 | return true; |
11630 | /* DECL_FRIEND_CONTEXT is set for a friend defined in class. */ |
11631 | if (DECL_FRIEND_CONTEXT (t)) |
11632 | return false; |
11633 | /* Non-templated friends in a class template are still represented with a |
11634 | TEMPLATE_DECL; check that its primary template is the befriending |
11635 | class. Note that DECL_PRIMARY_TEMPLATE is null for |
11636 | template <class T> friend A<T>::f(); */ |
11637 | tree tmpl = TI_TEMPLATE (ti); |
11638 | tree primary = DECL_PRIMARY_TEMPLATE (tmpl); |
11639 | return (primary && primary != tmpl); |
11640 | } |
11641 | else |
11642 | return false; |
11643 | } |
11644 | |
11645 | /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the |
11646 | vector of template arguments, as for tsubst. |
11647 | |
11648 | Returns an appropriate tsubst'd friend declaration. */ |
11649 | |
11650 | static tree |
11651 | tsubst_friend_function (tree decl, tree args) |
11652 | { |
11653 | tree new_friend; |
11654 | |
11655 | if (TREE_CODE (decl) == FUNCTION_DECL |
11656 | && DECL_TEMPLATE_INSTANTIATION (decl) |
11657 | && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
11658 | /* This was a friend declared with an explicit template |
11659 | argument list, e.g.: |
11660 | |
11661 | friend void f<>(T); |
11662 | |
11663 | to indicate that f was a template instantiation, not a new |
11664 | function declaration. Now, we have to figure out what |
11665 | instantiation of what template. */ |
11666 | { |
11667 | tree template_id, arglist, fns; |
11668 | tree new_args; |
11669 | tree tmpl; |
11670 | tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type)); |
11671 | |
11672 | /* Friend functions are looked up in the containing namespace scope. |
11673 | We must enter that scope, to avoid finding member functions of the |
11674 | current class with same name. */ |
11675 | push_nested_namespace (ns); |
11676 | fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args, |
11677 | tf_warning_or_error, NULL_TREE); |
11678 | pop_nested_namespace (ns); |
11679 | arglist = tsubst (DECL_TI_ARGS (decl), args, |
11680 | tf_warning_or_error, NULL_TREE); |
11681 | template_id = lookup_template_function (fns, arglist); |
11682 | |
11683 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
11684 | tmpl = determine_specialization (template_id, decl: new_friend, |
11685 | targs_out: &new_args, |
11686 | /*need_member_template=*/0, |
11687 | TREE_VEC_LENGTH (args), |
11688 | tsk: tsk_none); |
11689 | return instantiate_template (tmpl, new_args, tf_error); |
11690 | } |
11691 | |
11692 | new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE); |
11693 | if (new_friend == error_mark_node) |
11694 | return error_mark_node; |
11695 | |
11696 | /* The NEW_FRIEND will look like an instantiation, to the |
11697 | compiler, but is not an instantiation from the point of view of |
11698 | the language. For example, we might have had: |
11699 | |
11700 | template <class T> struct S { |
11701 | template <class U> friend void f(T, U); |
11702 | }; |
11703 | |
11704 | Then, in S<int>, template <class U> void f(int, U) is not an |
11705 | instantiation of anything. */ |
11706 | |
11707 | DECL_USE_TEMPLATE (new_friend) = 0; |
11708 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
11709 | { |
11710 | DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false; |
11711 | DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0; |
11712 | DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend)) |
11713 | = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl)); |
11714 | |
11715 | /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will |
11716 | match in decls_match. */ |
11717 | tree parms = DECL_TEMPLATE_PARMS (new_friend); |
11718 | tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms); |
11719 | treqs = maybe_substitute_reqs_for (treqs, new_friend); |
11720 | if (treqs != TEMPLATE_PARMS_CONSTRAINTS (parms)) |
11721 | { |
11722 | TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs; |
11723 | /* As well as each TEMPLATE_PARM_CONSTRAINTS. */ |
11724 | tsubst_each_template_parm_constraints (parms, args, |
11725 | tf_warning_or_error); |
11726 | } |
11727 | } |
11728 | |
11729 | /* The mangled name for the NEW_FRIEND is incorrect. The function |
11730 | is not a template instantiation and should not be mangled like |
11731 | one. Therefore, we forget the mangling here; we'll recompute it |
11732 | later if we need it. */ |
11733 | if (TREE_CODE (new_friend) != TEMPLATE_DECL) |
11734 | { |
11735 | SET_DECL_RTL (new_friend, NULL); |
11736 | SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE); |
11737 | } |
11738 | |
11739 | if (DECL_NAMESPACE_SCOPE_P (new_friend)) |
11740 | { |
11741 | tree old_decl; |
11742 | tree ns; |
11743 | |
11744 | /* We must save some information from NEW_FRIEND before calling |
11745 | duplicate decls since that function will free NEW_FRIEND if |
11746 | possible. */ |
11747 | tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend); |
11748 | tree new_friend_result_template_info = NULL_TREE; |
11749 | bool new_friend_is_defn = |
11750 | (new_friend_template_info |
11751 | && (DECL_INITIAL (DECL_TEMPLATE_RESULT |
11752 | (template_for_substitution (new_friend))) |
11753 | != NULL_TREE)); |
11754 | tree not_tmpl = new_friend; |
11755 | |
11756 | if (TREE_CODE (new_friend) == TEMPLATE_DECL) |
11757 | { |
11758 | /* This declaration is a `primary' template. */ |
11759 | DECL_PRIMARY_TEMPLATE (new_friend) = new_friend; |
11760 | |
11761 | not_tmpl = DECL_TEMPLATE_RESULT (new_friend); |
11762 | new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl); |
11763 | } |
11764 | |
11765 | /* We need to propagate module attachment for the new friend from the |
11766 | owner of this template. */ |
11767 | propagate_defining_module (decl: new_friend, orig: decl); |
11768 | |
11769 | /* Inside pushdecl_namespace_level, we will push into the |
11770 | current namespace. However, the friend function should go |
11771 | into the namespace of the template. */ |
11772 | ns = decl_namespace_context (new_friend); |
11773 | push_nested_namespace (ns); |
11774 | old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true); |
11775 | pop_nested_namespace (ns); |
11776 | |
11777 | if (old_decl == error_mark_node) |
11778 | return error_mark_node; |
11779 | |
11780 | if (old_decl != new_friend) |
11781 | { |
11782 | /* This new friend declaration matched an existing |
11783 | declaration. For example, given: |
11784 | |
11785 | template <class T> void f(T); |
11786 | template <class U> class C { |
11787 | template <class T> friend void f(T) {} |
11788 | }; |
11789 | |
11790 | the friend declaration actually provides the definition |
11791 | of `f', once C has been instantiated for some type. So, |
11792 | old_decl will be the out-of-class template declaration, |
11793 | while new_friend is the in-class definition. |
11794 | |
11795 | But, if `f' was called before this point, the |
11796 | instantiation of `f' will have DECL_TI_ARGS corresponding |
11797 | to `T' but not to `U', references to which might appear |
11798 | in the definition of `f'. Previously, the most general |
11799 | template for an instantiation of `f' was the out-of-class |
11800 | version; now it is the in-class version. Therefore, we |
11801 | run through all specialization of `f', adding to their |
11802 | DECL_TI_ARGS appropriately. In particular, they need a |
11803 | new set of outer arguments, corresponding to the |
11804 | arguments for this class instantiation. |
11805 | |
11806 | The same situation can arise with something like this: |
11807 | |
11808 | friend void f(int); |
11809 | template <class T> class C { |
11810 | friend void f(T) {} |
11811 | }; |
11812 | |
11813 | when `C<int>' is instantiated. Now, `f(int)' is defined |
11814 | in the class. */ |
11815 | |
11816 | if (!new_friend_is_defn) |
11817 | /* On the other hand, if the in-class declaration does |
11818 | *not* provide a definition, then we don't want to alter |
11819 | existing definitions. We can just leave everything |
11820 | alone. */ |
11821 | ; |
11822 | else |
11823 | { |
11824 | tree old_template = most_general_template (old_decl); |
11825 | tree new_template = TI_TEMPLATE (new_friend_template_info); |
11826 | tree new_args = TI_ARGS (new_friend_template_info); |
11827 | |
11828 | /* Overwrite whatever template info was there before, if |
11829 | any, with the new template information pertaining to |
11830 | the declaration. */ |
11831 | DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info; |
11832 | |
11833 | if (TREE_CODE (old_decl) != TEMPLATE_DECL) |
11834 | { |
11835 | /* We should have called reregister_specialization in |
11836 | duplicate_decls. */ |
11837 | gcc_assert (retrieve_specialization (new_template, |
11838 | new_args, 0) |
11839 | == old_decl); |
11840 | |
11841 | /* Instantiate it if the global has already been used. */ |
11842 | if (DECL_ODR_USED (old_decl)) |
11843 | instantiate_decl (old_decl, /*defer_ok=*/true, |
11844 | /*expl_inst_class_mem_p=*/false); |
11845 | } |
11846 | else |
11847 | { |
11848 | tree t; |
11849 | |
11850 | /* Indicate that the old function template is a partial |
11851 | instantiation. */ |
11852 | DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl)) |
11853 | = new_friend_result_template_info; |
11854 | |
11855 | gcc_assert (new_template |
11856 | == most_general_template (new_template)); |
11857 | gcc_assert (new_template != old_decl); |
11858 | |
11859 | /* Reassign any specializations already in the hash table |
11860 | to the new more general template, and add the |
11861 | additional template args. */ |
11862 | for (t = DECL_TEMPLATE_INSTANTIATIONS (old_template); |
11863 | t != NULL_TREE; |
11864 | t = TREE_CHAIN (t)) |
11865 | { |
11866 | tree spec = TREE_VALUE (t); |
11867 | spec_entry elt; |
11868 | |
11869 | elt.tmpl = old_decl; |
11870 | elt.args = DECL_TI_ARGS (spec); |
11871 | elt.spec = NULL_TREE; |
11872 | |
11873 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (DECL_TI_ARGS (spec)) |
11874 | && !is_specialization_of_friend (decl: spec, friend_decl: new_template)) |
11875 | continue; |
11876 | |
11877 | decl_specializations->remove_elt (value: &elt); |
11878 | |
11879 | tree& spec_args = DECL_TI_ARGS (spec); |
11880 | spec_args = add_outermost_template_args |
11881 | (args: new_args, INNERMOST_TEMPLATE_ARGS (spec_args)); |
11882 | |
11883 | register_specialization |
11884 | (spec, tmpl: new_template, args: spec_args, is_friend: true, hash: 0); |
11885 | |
11886 | } |
11887 | DECL_TEMPLATE_INSTANTIATIONS (old_template) = NULL_TREE; |
11888 | } |
11889 | } |
11890 | |
11891 | /* The information from NEW_FRIEND has been merged into OLD_DECL |
11892 | by duplicate_decls. */ |
11893 | new_friend = old_decl; |
11894 | } |
11895 | |
11896 | /* We've just introduced a namespace-scope function in the purview |
11897 | without necessarily having opened the enclosing namespace, so |
11898 | make sure the namespace is in the purview now too. */ |
11899 | if (modules_p () |
11900 | && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend)) |
11901 | && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL) |
11902 | DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true; |
11903 | } |
11904 | else |
11905 | { |
11906 | tree context = DECL_CONTEXT (new_friend); |
11907 | bool dependent_p; |
11908 | |
11909 | /* In the code |
11910 | template <class T> class C { |
11911 | template <class U> friend void C1<U>::f (); // case 1 |
11912 | friend void C2<T>::f (); // case 2 |
11913 | }; |
11914 | we only need to make sure CONTEXT is a complete type for |
11915 | case 2. To distinguish between the two cases, we note that |
11916 | CONTEXT of case 1 remains dependent type after tsubst while |
11917 | this isn't true for case 2. */ |
11918 | ++processing_template_decl; |
11919 | dependent_p = dependent_type_p (context); |
11920 | --processing_template_decl; |
11921 | |
11922 | if (!dependent_p |
11923 | && !complete_type_or_else (context, NULL_TREE)) |
11924 | return error_mark_node; |
11925 | |
11926 | if (COMPLETE_TYPE_P (context)) |
11927 | { |
11928 | tree fn = new_friend; |
11929 | /* do_friend adds the TEMPLATE_DECL for any member friend |
11930 | template even if it isn't a member template, i.e. |
11931 | template <class T> friend A<T>::f(); |
11932 | Look through it in that case. */ |
11933 | if (TREE_CODE (fn) == TEMPLATE_DECL |
11934 | && !PRIMARY_TEMPLATE_P (fn)) |
11935 | fn = DECL_TEMPLATE_RESULT (fn); |
11936 | /* Check to see that the declaration is really present, and, |
11937 | possibly obtain an improved declaration. */ |
11938 | fn = check_classfn (context, fn, NULL_TREE); |
11939 | |
11940 | if (fn) |
11941 | new_friend = fn; |
11942 | } |
11943 | } |
11944 | |
11945 | return new_friend; |
11946 | } |
11947 | |
11948 | /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of |
11949 | template arguments, as for tsubst. |
11950 | |
11951 | Returns an appropriate tsubst'd friend type or error_mark_node on |
11952 | failure. */ |
11953 | |
11954 | static tree |
11955 | tsubst_friend_class (tree friend_tmpl, tree args) |
11956 | { |
11957 | tree tmpl; |
11958 | |
11959 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl)) |
11960 | { |
11961 | tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE); |
11962 | return TREE_TYPE (tmpl); |
11963 | } |
11964 | |
11965 | if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl)) == 1) |
11966 | /* The template has already been fully substituted, e.g. for |
11967 | |
11968 | template <typename> friend class ::C; |
11969 | |
11970 | so we can just return it directly. */ |
11971 | return TREE_TYPE (friend_tmpl); |
11972 | |
11973 | tree context = CP_DECL_CONTEXT (friend_tmpl); |
11974 | if (TREE_CODE (context) == NAMESPACE_DECL) |
11975 | push_nested_namespace (context); |
11976 | else |
11977 | { |
11978 | context = tsubst (context, args, tf_error, NULL_TREE); |
11979 | push_nested_class (context); |
11980 | } |
11981 | |
11982 | tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE, |
11983 | LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND); |
11984 | |
11985 | if (!tmpl) |
11986 | /* If we didn't find by name lookup, the type may still exist but as a |
11987 | 'hidden' import; we should check for this too to avoid accidentally |
11988 | instantiating a duplicate. */ |
11989 | tmpl = lookup_imported_hidden_friend (friend_tmpl); |
11990 | |
11991 | if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl)) |
11992 | { |
11993 | /* The friend template has already been declared. Just |
11994 | check to see that the declarations match, and install any new |
11995 | default parameters. We must tsubst the default parameters, |
11996 | of course. We only need the innermost template parameters |
11997 | because that is all that redeclare_class_template will look |
11998 | at. */ |
11999 | |
12000 | if (modules_p ()) |
12001 | /* Check that the existing declaration's module attachment is |
12002 | compatible with the attachment of the friend template. */ |
12003 | module_may_redeclare (olddecl: tmpl, newdecl: friend_tmpl); |
12004 | |
12005 | if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (friend_tmpl)) |
12006 | { |
12007 | tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl), |
12008 | args, tf_warning_or_error); |
12009 | tsubst_each_template_parm_constraints (parms, args, |
12010 | tf_warning_or_error); |
12011 | location_t saved_input_location = input_location; |
12012 | input_location = DECL_SOURCE_LOCATION (friend_tmpl); |
12013 | tree cons = get_constraints (friend_tmpl); |
12014 | ++processing_template_decl; |
12015 | cons = tsubst_constraint_info (cons, args, tf_warning_or_error, |
12016 | DECL_FRIEND_CONTEXT (friend_tmpl)); |
12017 | --processing_template_decl; |
12018 | redeclare_class_template (TREE_TYPE (tmpl), parms, cons); |
12019 | input_location = saved_input_location; |
12020 | } |
12021 | } |
12022 | else |
12023 | { |
12024 | /* The friend template has not already been declared. In this |
12025 | case, the instantiation of the template class will cause the |
12026 | injection of this template into the namespace scope. */ |
12027 | tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE); |
12028 | |
12029 | if (tmpl != error_mark_node) |
12030 | { |
12031 | /* The new TMPL is not an instantiation of anything, so we |
12032 | forget its origins. It is also not a specialization of |
12033 | anything. We don't reset CLASSTYPE_TI_TEMPLATE |
12034 | for the new type because that is supposed to be the |
12035 | corresponding template decl, i.e., TMPL. */ |
12036 | spec_entry elt; |
12037 | elt.tmpl = friend_tmpl; |
12038 | elt.args = CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)); |
12039 | elt.spec = TREE_TYPE (tmpl); |
12040 | type_specializations->remove_elt (value: &elt); |
12041 | |
12042 | DECL_USE_TEMPLATE (tmpl) = 0; |
12043 | DECL_TEMPLATE_INFO (tmpl) = NULL_TREE; |
12044 | CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0; |
12045 | CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)) |
12046 | = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))); |
12047 | DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = false; |
12048 | |
12049 | /* Substitute into and set the constraints on the new declaration. */ |
12050 | if (tree ci = get_constraints (friend_tmpl)) |
12051 | { |
12052 | ++processing_template_decl; |
12053 | ci = tsubst_constraint_info (ci, args, tf_warning_or_error, |
12054 | DECL_FRIEND_CONTEXT (friend_tmpl)); |
12055 | --processing_template_decl; |
12056 | set_constraints (tmpl, ci); |
12057 | tsubst_each_template_parm_constraints (DECL_TEMPLATE_PARMS (tmpl), |
12058 | args, tf_warning_or_error); |
12059 | } |
12060 | |
12061 | /* We need to propagate the attachment of the original template to the |
12062 | newly instantiated template type. */ |
12063 | propagate_defining_module (decl: tmpl, orig: friend_tmpl); |
12064 | |
12065 | /* Inject this template into the enclosing namspace scope. */ |
12066 | tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true); |
12067 | } |
12068 | } |
12069 | |
12070 | if (TREE_CODE (context) == NAMESPACE_DECL) |
12071 | pop_nested_namespace (context); |
12072 | else |
12073 | pop_nested_class (); |
12074 | |
12075 | return TREE_TYPE (tmpl); |
12076 | } |
12077 | |
12078 | /* Returns zero if TYPE cannot be completed later due to circularity. |
12079 | Otherwise returns one. */ |
12080 | |
12081 | static int |
12082 | can_complete_type_without_circularity (tree type) |
12083 | { |
12084 | if (type == NULL_TREE || type == error_mark_node) |
12085 | return 0; |
12086 | else if (COMPLETE_TYPE_P (type)) |
12087 | return 1; |
12088 | else if (TREE_CODE (type) == ARRAY_TYPE) |
12089 | return can_complete_type_without_circularity (TREE_TYPE (type)); |
12090 | else if (CLASS_TYPE_P (type) |
12091 | && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type))) |
12092 | return 0; |
12093 | else |
12094 | return 1; |
12095 | } |
12096 | |
12097 | static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree, |
12098 | tsubst_flags_t, tree); |
12099 | |
12100 | /* Instantiate the contract statement. */ |
12101 | |
12102 | static tree |
12103 | tsubst_contract (tree decl, tree t, tree args, tsubst_flags_t complain, |
12104 | tree in_decl) |
12105 | { |
12106 | tree type = decl ? TREE_TYPE (TREE_TYPE (decl)) : NULL_TREE; |
12107 | bool auto_p = type_uses_auto (type); |
12108 | |
12109 | tree r = copy_node (t); |
12110 | |
12111 | /* Rebuild the result variable. */ |
12112 | if (type && POSTCONDITION_P (t) && POSTCONDITION_IDENTIFIER (t)) |
12113 | { |
12114 | tree oldvar = POSTCONDITION_IDENTIFIER (t); |
12115 | |
12116 | tree newvar = copy_node (oldvar); |
12117 | TREE_TYPE (newvar) = type; |
12118 | DECL_CONTEXT (newvar) = decl; |
12119 | POSTCONDITION_IDENTIFIER (r) = newvar; |
12120 | |
12121 | /* Make sure the postcondition is valid. */ |
12122 | location_t loc = DECL_SOURCE_LOCATION (oldvar); |
12123 | if (!auto_p) |
12124 | if (!check_postcondition_result (decl, type, loc)) |
12125 | return invalidate_contract (r); |
12126 | |
12127 | /* Make the variable available for lookup. */ |
12128 | register_local_specialization (spec: newvar, tmpl: oldvar); |
12129 | } |
12130 | |
12131 | /* Instantiate the condition. If the return type is undeduced, process |
12132 | the expression as if inside a template to avoid spurious type errors. */ |
12133 | if (auto_p) |
12134 | ++processing_template_decl; |
12135 | ++processing_contract_condition; |
12136 | CONTRACT_CONDITION (r) |
12137 | = tsubst_expr (CONTRACT_CONDITION (t), args, complain, in_decl); |
12138 | --processing_contract_condition; |
12139 | if (auto_p) |
12140 | --processing_template_decl; |
12141 | |
12142 | /* And the comment. */ |
12143 | CONTRACT_COMMENT (r) |
12144 | = tsubst_expr (CONTRACT_COMMENT (r), args, complain, in_decl); |
12145 | |
12146 | return r; |
12147 | } |
12148 | |
12149 | /* Update T by instantiating its contract attribute. */ |
12150 | |
12151 | static void |
12152 | tsubst_contract_attribute (tree decl, tree t, tree args, |
12153 | tsubst_flags_t complain, tree in_decl) |
12154 | { |
12155 | /* For non-specializations, adjust the current declaration to the most general |
12156 | version of in_decl. Because we defer the instantiation of contracts as long |
12157 | as possible, they are still written in terms of the parameters (and return |
12158 | type) of the most general template. */ |
12159 | tree tmpl = DECL_TI_TEMPLATE (in_decl); |
12160 | if (!DECL_TEMPLATE_SPECIALIZATION (tmpl)) |
12161 | in_decl = DECL_TEMPLATE_RESULT (most_general_template (in_decl)); |
12162 | local_specialization_stack specs (lss_copy); |
12163 | register_parameter_specializations (in_decl, decl); |
12164 | |
12165 | /* Get the contract to be instantiated. */ |
12166 | tree contract = CONTRACT_STATEMENT (t); |
12167 | |
12168 | /* Use the complete set of template arguments for instantiation. The |
12169 | contract may not have been instantiated and still refer to outer levels |
12170 | of template parameters. */ |
12171 | args = DECL_TI_ARGS (decl); |
12172 | |
12173 | /* For member functions, make this available for semantic analysis. */ |
12174 | tree save_ccp = current_class_ptr; |
12175 | tree save_ccr = current_class_ref; |
12176 | if (DECL_IOBJ_MEMBER_FUNCTION_P (decl)) |
12177 | { |
12178 | tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl)); |
12179 | tree this_type = TREE_TYPE (TREE_VALUE (arg_types)); |
12180 | inject_this_parameter (this_type, cp_type_quals (this_type)); |
12181 | } |
12182 | |
12183 | contract = tsubst_contract (decl, t: contract, args, complain, in_decl); |
12184 | |
12185 | current_class_ptr = save_ccp; |
12186 | current_class_ref = save_ccr; |
12187 | |
12188 | /* Rebuild the attribute. */ |
12189 | TREE_VALUE (t) = build_tree_list (NULL_TREE, contract); |
12190 | } |
12191 | |
12192 | /* Rebuild the attribute list for DECL, substituting into contracts |
12193 | as needed. */ |
12194 | |
12195 | void |
12196 | tsubst_contract_attributes (tree decl, tree args, tsubst_flags_t complain, tree in_decl) |
12197 | { |
12198 | tree list = copy_list (DECL_ATTRIBUTES (decl)); |
12199 | for (tree attr = list; attr; attr = CONTRACT_CHAIN (attr)) |
12200 | { |
12201 | if (cxx_contract_attribute_p (attr)) |
12202 | tsubst_contract_attribute (decl, t: attr, args, complain, in_decl); |
12203 | } |
12204 | DECL_ATTRIBUTES (decl) = list; |
12205 | } |
12206 | |
12207 | /* Instantiate a single dependent attribute T (a TREE_LIST), and return either |
12208 | T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */ |
12209 | |
12210 | static tree |
12211 | tsubst_attribute (tree t, tree *decl_p, tree args, |
12212 | tsubst_flags_t complain, tree in_decl) |
12213 | { |
12214 | gcc_assert (ATTR_IS_DEPENDENT (t)); |
12215 | |
12216 | /* Note that contract attributes are never substituted from this function. |
12217 | Their instantiation is triggered by regenerate_from_template_decl when |
12218 | we instantiate the body of the function. */ |
12219 | |
12220 | tree val = TREE_VALUE (t); |
12221 | if (val == NULL_TREE) |
12222 | /* Nothing to do. */; |
12223 | else if ((flag_openmp || flag_openmp_simd) |
12224 | && is_attribute_p (attr_name: "omp declare simd", |
12225 | ident: get_attribute_name (t))) |
12226 | { |
12227 | tree clauses = TREE_VALUE (val); |
12228 | clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args, |
12229 | complain, in_decl); |
12230 | c_omp_declare_simd_clauses_to_decls (*decl_p, clauses); |
12231 | clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD); |
12232 | tree parms = DECL_ARGUMENTS (*decl_p); |
12233 | clauses |
12234 | = c_omp_declare_simd_clauses_to_numbers (parms, clauses); |
12235 | if (clauses) |
12236 | val = build_tree_list (NULL_TREE, clauses); |
12237 | else |
12238 | val = NULL_TREE; |
12239 | } |
12240 | else if (flag_openmp |
12241 | && is_attribute_p (attr_name: "omp declare variant base", |
12242 | ident: get_attribute_name (t))) |
12243 | { |
12244 | ++cp_unevaluated_operand; |
12245 | tree varid = tsubst_expr (TREE_PURPOSE (val), args, complain, in_decl); |
12246 | --cp_unevaluated_operand; |
12247 | tree chain = TREE_CHAIN (val); |
12248 | location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain)); |
12249 | tree ctx = copy_list (TREE_VALUE (val)); |
12250 | tree append_args_list = TREE_CHAIN (TREE_CHAIN (chain)); |
12251 | if (append_args_list |
12252 | && TREE_VALUE (append_args_list) |
12253 | && TREE_CHAIN (TREE_VALUE (append_args_list))) |
12254 | { |
12255 | append_args_list = TREE_VALUE (append_args_list); |
12256 | append_args_list = TREE_VALUE (TREE_CHAIN (append_args_list)); |
12257 | for (; append_args_list; |
12258 | append_args_list = TREE_CHAIN (append_args_list)) |
12259 | { |
12260 | tree pref_list = TREE_VALUE (append_args_list); |
12261 | if (pref_list == NULL_TREE || TREE_CODE (pref_list) != TREE_LIST) |
12262 | continue; |
12263 | tree fr_list = TREE_VALUE (pref_list); |
12264 | int len = TREE_VEC_LENGTH (fr_list); |
12265 | for (int i = 0; i < len; i++) |
12266 | { |
12267 | tree *fr_expr = &TREE_VEC_ELT (fr_list, i); |
12268 | /* Preserve NOP_EXPR to have a location. */ |
12269 | if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR) |
12270 | TREE_OPERAND (*fr_expr, 0) |
12271 | = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain, |
12272 | in_decl); |
12273 | else |
12274 | *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl); |
12275 | } |
12276 | } |
12277 | } |
12278 | for (tree tss = ctx; tss; tss = TREE_CHAIN (tss)) |
12279 | { |
12280 | enum omp_tss_code set = OMP_TSS_CODE (tss); |
12281 | tree selectors = NULL_TREE; |
12282 | for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; |
12283 | ts = TREE_CHAIN (ts)) |
12284 | { |
12285 | tree properties = NULL_TREE; |
12286 | tree scoreval = NULL_TREE; |
12287 | /* FIXME: The body of this loop should really be dispatching |
12288 | according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead |
12289 | of having hard-wired knowledge of specific selectors. */ |
12290 | if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD |
12291 | && set == OMP_TRAIT_SET_CONSTRUCT) |
12292 | { |
12293 | tree clauses = OMP_TS_PROPERTIES (ts); |
12294 | clauses = tsubst_omp_clauses (clauses, |
12295 | C_ORT_OMP_DECLARE_SIMD, args, |
12296 | complain, in_decl); |
12297 | c_omp_declare_simd_clauses_to_decls (*decl_p, clauses); |
12298 | clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD); |
12299 | properties = clauses; |
12300 | } |
12301 | else |
12302 | { |
12303 | tree v = OMP_TS_SCORE (ts); |
12304 | if (v) |
12305 | { |
12306 | v = tsubst_expr (v, args, complain, in_decl); |
12307 | v = fold_non_dependent_expr (v); |
12308 | if (!INTEGRAL_TYPE_P (TREE_TYPE (v)) |
12309 | || TREE_CODE (v) != INTEGER_CST) |
12310 | { |
12311 | location_t loc |
12312 | = cp_expr_loc_or_loc (OMP_TS_SCORE (ts), |
12313 | or_loc: match_loc); |
12314 | error_at (loc, "score argument must be " |
12315 | "constant integer expression"); |
12316 | return NULL_TREE; |
12317 | } |
12318 | else if (tree_int_cst_sgn (v) < 0) |
12319 | { |
12320 | location_t loc |
12321 | = cp_expr_loc_or_loc (OMP_TS_SCORE (ts), |
12322 | or_loc: match_loc); |
12323 | error_at (loc, "score argument must be " |
12324 | "non-negative"); |
12325 | return NULL_TREE; |
12326 | } |
12327 | scoreval = v; |
12328 | } |
12329 | properties = copy_list (OMP_TS_PROPERTIES (ts)); |
12330 | for (tree p = properties; p; p = TREE_CHAIN (p)) |
12331 | if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE) |
12332 | continue; |
12333 | else if (OMP_TP_VALUE (p)) |
12334 | { |
12335 | bool allow_string |
12336 | = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION |
12337 | || set != OMP_TRAIT_SET_USER); |
12338 | tree v = OMP_TP_VALUE (p); |
12339 | if (TREE_CODE (v) == STRING_CST && allow_string) |
12340 | continue; |
12341 | v = tsubst_expr (v, args, complain, in_decl); |
12342 | v = fold_non_dependent_expr (v); |
12343 | if (!INTEGRAL_TYPE_P (TREE_TYPE (v)) |
12344 | || !tree_fits_shwi_p (v)) |
12345 | { |
12346 | location_t loc |
12347 | = cp_expr_loc_or_loc (OMP_TP_VALUE (p), |
12348 | or_loc: match_loc); |
12349 | if (allow_string) |
12350 | error_at (loc, "property must be constant " |
12351 | "integer expression or string " |
12352 | "literal"); |
12353 | else |
12354 | error_at (loc, "property must be constant " |
12355 | "integer expression"); |
12356 | return NULL_TREE; |
12357 | } |
12358 | OMP_TP_VALUE (p) = v; |
12359 | } |
12360 | } |
12361 | selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval, |
12362 | properties, selectors); |
12363 | } |
12364 | OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors); |
12365 | } |
12366 | if (varid == error_mark_node) |
12367 | val = error_mark_node; |
12368 | else |
12369 | val = tree_cons (varid, ctx, chain); |
12370 | } |
12371 | /* If the first attribute argument is an identifier, don't |
12372 | pass it through tsubst. Attributes like mode, format, |
12373 | cleanup and several target specific attributes expect it |
12374 | unmodified. */ |
12375 | else if (attribute_takes_identifier_p (get_attribute_name (t))) |
12376 | { |
12377 | tree chain |
12378 | = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl); |
12379 | if (chain != TREE_CHAIN (val)) |
12380 | val = tree_cons (NULL_TREE, TREE_VALUE (val), chain); |
12381 | } |
12382 | else if (PACK_EXPANSION_P (val)) |
12383 | { |
12384 | /* An attribute pack expansion. */ |
12385 | tree purp = TREE_PURPOSE (t); |
12386 | tree pack = tsubst_pack_expansion (val, args, complain, in_decl); |
12387 | if (pack == error_mark_node) |
12388 | return error_mark_node; |
12389 | int len = TREE_VEC_LENGTH (pack); |
12390 | tree list = NULL_TREE; |
12391 | tree *q = &list; |
12392 | for (int i = 0; i < len; ++i) |
12393 | { |
12394 | tree elt = TREE_VEC_ELT (pack, i); |
12395 | *q = build_tree_list (purp, elt); |
12396 | q = &TREE_CHAIN (*q); |
12397 | } |
12398 | return list; |
12399 | } |
12400 | else |
12401 | val = tsubst_expr (val, args, complain, in_decl); |
12402 | |
12403 | if (val == error_mark_node) |
12404 | return error_mark_node; |
12405 | if (val != TREE_VALUE (t)) |
12406 | return build_tree_list (TREE_PURPOSE (t), val); |
12407 | return t; |
12408 | } |
12409 | |
12410 | /* Instantiate any dependent attributes in ATTRIBUTES, returning either it |
12411 | unchanged or a new TREE_LIST chain. */ |
12412 | |
12413 | static tree |
12414 | tsubst_attributes (tree attributes, tree args, |
12415 | tsubst_flags_t complain, tree in_decl) |
12416 | { |
12417 | tree last_dep = NULL_TREE; |
12418 | |
12419 | for (tree t = attributes; t; t = TREE_CHAIN (t)) |
12420 | if (ATTR_IS_DEPENDENT (t)) |
12421 | { |
12422 | last_dep = t; |
12423 | attributes = copy_list (attributes); |
12424 | break; |
12425 | } |
12426 | |
12427 | if (last_dep) |
12428 | for (tree *p = &attributes; *p; ) |
12429 | { |
12430 | tree t = *p; |
12431 | if (ATTR_IS_DEPENDENT (t)) |
12432 | { |
12433 | tree subst = tsubst_attribute (t, NULL, args, complain, in_decl); |
12434 | if (subst != t) |
12435 | { |
12436 | *p = subst; |
12437 | while (*p) |
12438 | p = &TREE_CHAIN (*p); |
12439 | *p = TREE_CHAIN (t); |
12440 | continue; |
12441 | } |
12442 | } |
12443 | p = &TREE_CHAIN (*p); |
12444 | } |
12445 | |
12446 | return attributes; |
12447 | } |
12448 | |
12449 | /* Apply any attributes which had to be deferred until instantiation |
12450 | time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes; |
12451 | ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally, |
12452 | false on error. */ |
12453 | |
12454 | static bool |
12455 | apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags, |
12456 | tree args, tsubst_flags_t complain, tree in_decl) |
12457 | { |
12458 | tree t; |
12459 | tree *p; |
12460 | |
12461 | if (attributes == NULL_TREE) |
12462 | return true; |
12463 | |
12464 | if (DECL_P (*decl_p)) |
12465 | { |
12466 | if (TREE_TYPE (*decl_p) == error_mark_node) |
12467 | return false; |
12468 | p = &DECL_ATTRIBUTES (*decl_p); |
12469 | /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical |
12470 | to our attributes parameter. */ |
12471 | gcc_assert (*p == attributes); |
12472 | } |
12473 | else if (FUNC_OR_METHOD_TYPE_P (*decl_p) |
12474 | || (attr_flags & ATTR_FLAG_TYPE_IN_PLACE) == 0) |
12475 | p = NULL; |
12476 | else |
12477 | { |
12478 | p = &TYPE_ATTRIBUTES (*decl_p); |
12479 | /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in |
12480 | lookup_template_class_1, and should be preserved. */ |
12481 | gcc_assert (*p != attributes); |
12482 | while (*p) |
12483 | p = &TREE_CHAIN (*p); |
12484 | } |
12485 | |
12486 | /* save_template_attributes puts the dependent attributes at the beginning of |
12487 | the list; find the non-dependent ones. */ |
12488 | for (t = attributes; t; t = TREE_CHAIN (t)) |
12489 | if (!ATTR_IS_DEPENDENT (t)) |
12490 | break; |
12491 | tree nondep = t; |
12492 | |
12493 | /* Apply any non-dependent attributes. */ |
12494 | if (p) |
12495 | *p = nondep; |
12496 | else if (nondep) |
12497 | *decl_p = cp_build_type_attribute_variant (*decl_p, nondep); |
12498 | |
12499 | if (nondep == attributes) |
12500 | return true; |
12501 | |
12502 | /* And then any dependent ones. */ |
12503 | tree late_attrs = NULL_TREE; |
12504 | tree *q = &late_attrs; |
12505 | for (t = attributes; t != nondep; t = TREE_CHAIN (t)) |
12506 | { |
12507 | *q = tsubst_attribute (t, decl_p, args, complain, in_decl); |
12508 | if (*q == error_mark_node) |
12509 | return false; |
12510 | if (*q == t) |
12511 | { |
12512 | *q = copy_node (t); |
12513 | TREE_CHAIN (*q) = NULL_TREE; |
12514 | } |
12515 | while (*q) |
12516 | q = &TREE_CHAIN (*q); |
12517 | } |
12518 | |
12519 | /* cplus_decl_attributes can add some attributes implicitly. For templates, |
12520 | those attributes should have been added already when those templates were |
12521 | parsed, and shouldn't be added based on from which context they are |
12522 | first time instantiated. */ |
12523 | auto o1 = make_temp_override (current_optimize_pragma, NULL_TREE); |
12524 | auto o2 = make_temp_override (optimization_current_node, |
12525 | optimization_default_node); |
12526 | auto o3 = make_temp_override (current_target_pragma, NULL_TREE); |
12527 | auto o4 = make_temp_override (var&: scope_chain->omp_declare_target_attribute, |
12528 | NULL); |
12529 | auto o5 = make_temp_override (var&: scope_chain->omp_begin_assumes, NULL); |
12530 | auto o6 = make_temp_override (target_option_current_node, |
12531 | target_option_default_node); |
12532 | |
12533 | cplus_decl_attributes (decl_p, late_attrs, attr_flags); |
12534 | |
12535 | return true; |
12536 | } |
12537 | |
12538 | /* The template TMPL is being instantiated with the template arguments TARGS. |
12539 | Perform the access checks that we deferred when parsing the template. */ |
12540 | |
12541 | static void |
12542 | perform_instantiation_time_access_checks (tree tmpl, tree targs) |
12543 | { |
12544 | unsigned i; |
12545 | deferred_access_check *chk; |
12546 | |
12547 | if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL) |
12548 | return; |
12549 | |
12550 | if (vec<deferred_access_check, va_gc> *access_checks |
12551 | = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl))) |
12552 | FOR_EACH_VEC_ELT (*access_checks, i, chk) |
12553 | { |
12554 | tree decl = chk->decl; |
12555 | tree diag_decl = chk->diag_decl; |
12556 | tree type_scope = TREE_TYPE (chk->binfo); |
12557 | |
12558 | if (uses_template_parms (t: type_scope)) |
12559 | type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE); |
12560 | |
12561 | /* Make access check error messages point to the location |
12562 | of the use of the typedef. */ |
12563 | iloc_sentinel ils (chk->loc); |
12564 | perform_or_defer_access_check (TYPE_BINFO (type_scope), |
12565 | decl, diag_decl, tf_warning_or_error); |
12566 | } |
12567 | } |
12568 | |
12569 | /* If the template T that we're about to instantiate contained errors at |
12570 | parse time that we downgraded into warnings or suppressed, diagnose the |
12571 | error now to render the TU ill-formed (if the TU has not already been |
12572 | deemed ill-formed by an earlier error). */ |
12573 | |
12574 | static void |
12575 | maybe_diagnose_erroneous_template (tree t) |
12576 | { |
12577 | if (erroneous_templates && !(seen_error) ()) |
12578 | if (location_t *error_loc = erroneous_templates->get (k: t)) |
12579 | { |
12580 | auto_diagnostic_group d; |
12581 | location_t decl_loc = location_of (t); |
12582 | error_at (decl_loc, "instantiating erroneous template"); |
12583 | inform (*error_loc, "first error appeared here"); |
12584 | } |
12585 | } |
12586 | |
12587 | tree |
12588 | instantiate_class_template (tree type) |
12589 | { |
12590 | auto_timevar tv (TV_TEMPLATE_INST); |
12591 | |
12592 | tree templ, args, pattern, t, member; |
12593 | tree typedecl; |
12594 | tree pbinfo; |
12595 | tree base_list; |
12596 | unsigned int saved_maximum_field_alignment; |
12597 | tree fn_context; |
12598 | |
12599 | if (type == error_mark_node) |
12600 | return error_mark_node; |
12601 | |
12602 | if (COMPLETE_OR_OPEN_TYPE_P (type) |
12603 | || uses_template_parms (t: type)) |
12604 | return type; |
12605 | |
12606 | /* Figure out which template is being instantiated. */ |
12607 | templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type)); |
12608 | gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL); |
12609 | |
12610 | /* Mark the type as in the process of being defined. */ |
12611 | TYPE_BEING_DEFINED (type) = 1; |
12612 | |
12613 | /* We may be in the middle of deferred access check. Disable |
12614 | it now. */ |
12615 | deferring_access_check_sentinel acs (dk_no_deferred); |
12616 | |
12617 | /* Determine what specialization of the original template to |
12618 | instantiate. */ |
12619 | t = most_specialized_partial_spec (type, tf_warning_or_error); |
12620 | if (t == error_mark_node) |
12621 | return error_mark_node; |
12622 | else if (t) |
12623 | { |
12624 | /* This TYPE is actually an instantiation of a partial |
12625 | specialization. We replace the innermost set of ARGS with |
12626 | the arguments appropriate for substitution. For example, |
12627 | given: |
12628 | |
12629 | template <class T> struct S {}; |
12630 | template <class T> struct S<T*> {}; |
12631 | |
12632 | and supposing that we are instantiating S<int*>, ARGS will |
12633 | presently be {int*} -- but we need {int}. */ |
12634 | pattern = TREE_TYPE (TI_TEMPLATE (t)); |
12635 | args = TI_ARGS (t); |
12636 | } |
12637 | else |
12638 | { |
12639 | pattern = TREE_TYPE (templ); |
12640 | args = CLASSTYPE_TI_ARGS (type); |
12641 | } |
12642 | |
12643 | /* If the template we're instantiating is incomplete, then clearly |
12644 | there's nothing we can do. */ |
12645 | if (!COMPLETE_TYPE_P (pattern)) |
12646 | { |
12647 | /* We can try again later. */ |
12648 | TYPE_BEING_DEFINED (type) = 0; |
12649 | return type; |
12650 | } |
12651 | |
12652 | /* If we've recursively instantiated too many templates, stop. */ |
12653 | if (! push_tinst_level (d: type)) |
12654 | return type; |
12655 | |
12656 | maybe_diagnose_erroneous_template (t: t ? TI_TEMPLATE (t) : templ); |
12657 | |
12658 | int saved_unevaluated_operand = cp_unevaluated_operand; |
12659 | int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; |
12660 | |
12661 | fn_context = decl_function_context (TYPE_MAIN_DECL (type)); |
12662 | /* Also avoid push_to_top_level for a lambda in an NSDMI. */ |
12663 | if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type)) |
12664 | fn_context = error_mark_node; |
12665 | if (!fn_context) |
12666 | push_to_top_level (); |
12667 | else |
12668 | { |
12669 | cp_unevaluated_operand = 0; |
12670 | c_inhibit_evaluation_warnings = 0; |
12671 | } |
12672 | |
12673 | mark_template_arguments_used (templ, CLASSTYPE_TI_ARGS (type)); |
12674 | |
12675 | /* Use #pragma pack from the template context. */ |
12676 | saved_maximum_field_alignment = maximum_field_alignment; |
12677 | maximum_field_alignment = TYPE_PRECISION (pattern); |
12678 | |
12679 | SET_CLASSTYPE_INTERFACE_UNKNOWN (type); |
12680 | |
12681 | /* Set the input location to the most specialized template definition. |
12682 | This is needed if tsubsting causes an error. */ |
12683 | typedecl = TYPE_MAIN_DECL (pattern); |
12684 | input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) = |
12685 | DECL_SOURCE_LOCATION (typedecl); |
12686 | |
12687 | set_instantiating_module (TYPE_NAME (type)); |
12688 | |
12689 | TYPE_PACKED (type) = TYPE_PACKED (pattern); |
12690 | SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern)); |
12691 | TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern); |
12692 | CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern); |
12693 | if (ANON_AGGR_TYPE_P (pattern)) |
12694 | SET_ANON_AGGR_TYPE_P (type); |
12695 | if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern)) |
12696 | { |
12697 | CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1; |
12698 | CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern); |
12699 | /* Adjust visibility for template arguments. */ |
12700 | determine_visibility (TYPE_MAIN_DECL (type)); |
12701 | } |
12702 | if (CLASS_TYPE_P (type)) |
12703 | CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern); |
12704 | |
12705 | pbinfo = TYPE_BINFO (pattern); |
12706 | |
12707 | /* We should never instantiate a nested class before its enclosing |
12708 | class; we need to look up the nested class by name before we can |
12709 | instantiate it, and that lookup should instantiate the enclosing |
12710 | class. */ |
12711 | gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) |
12712 | || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type))); |
12713 | |
12714 | /* When instantiating nested lambdas, ensure that they get the mangling |
12715 | scope of the new class type. */ |
12716 | start_lambda_scope (TYPE_NAME (type)); |
12717 | |
12718 | base_list = NULL_TREE; |
12719 | /* Defer access checking while we substitute into the types named in |
12720 | the base-clause. */ |
12721 | push_deferring_access_checks (dk_deferred); |
12722 | if (BINFO_N_BASE_BINFOS (pbinfo)) |
12723 | { |
12724 | tree pbase_binfo; |
12725 | int i; |
12726 | |
12727 | /* Substitute into each of the bases to determine the actual |
12728 | basetypes. */ |
12729 | for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++) |
12730 | { |
12731 | tree base; |
12732 | tree access = BINFO_BASE_ACCESS (pbinfo, i); |
12733 | tree expanded_bases = NULL_TREE; |
12734 | int idx, len = 1; |
12735 | |
12736 | if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo))) |
12737 | { |
12738 | expanded_bases = |
12739 | tsubst_pack_expansion (BINFO_TYPE (pbase_binfo), |
12740 | args, tf_error, NULL_TREE); |
12741 | if (expanded_bases == error_mark_node) |
12742 | continue; |
12743 | |
12744 | len = TREE_VEC_LENGTH (expanded_bases); |
12745 | } |
12746 | |
12747 | for (idx = 0; idx < len; idx++) |
12748 | { |
12749 | if (expanded_bases) |
12750 | /* Extract the already-expanded base class. */ |
12751 | base = TREE_VEC_ELT (expanded_bases, idx); |
12752 | else |
12753 | /* Substitute to figure out the base class. */ |
12754 | base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, |
12755 | NULL_TREE); |
12756 | |
12757 | if (base == error_mark_node) |
12758 | continue; |
12759 | |
12760 | base_list = tree_cons (access, base, base_list); |
12761 | if (BINFO_VIRTUAL_P (pbase_binfo)) |
12762 | TREE_TYPE (base_list) = integer_type_node; |
12763 | } |
12764 | } |
12765 | |
12766 | /* The list is now in reverse order; correct that. */ |
12767 | base_list = nreverse (base_list); |
12768 | } |
12769 | /* Now call xref_basetypes to set up all the base-class |
12770 | information. */ |
12771 | xref_basetypes (type, base_list); |
12772 | |
12773 | apply_late_template_attributes (decl_p: &type, TYPE_ATTRIBUTES (pattern), |
12774 | attr_flags: (int) ATTR_FLAG_TYPE_IN_PLACE, |
12775 | args, complain: tf_error, NULL_TREE); |
12776 | fixup_attribute_variants (type); |
12777 | |
12778 | /* Now that our base classes are set up, enter the scope of the |
12779 | class, so that name lookups into base classes, etc. will work |
12780 | correctly. This is precisely analogous to what we do in |
12781 | begin_class_definition when defining an ordinary non-template |
12782 | class, except we also need to push the enclosing classes. */ |
12783 | push_nested_class (type); |
12784 | |
12785 | /* Now check accessibility of the types named in its base-clause, |
12786 | relative to the scope of the class. */ |
12787 | pop_to_parent_deferring_access_checks (); |
12788 | |
12789 | /* A vector to hold members marked with attribute used. */ |
12790 | auto_vec<tree> used; |
12791 | |
12792 | /* Now members are processed in the order of declaration. */ |
12793 | for (member = CLASSTYPE_DECL_LIST (pattern); |
12794 | member; member = TREE_CHAIN (member)) |
12795 | { |
12796 | tree t = TREE_VALUE (member); |
12797 | |
12798 | if (TREE_PURPOSE (member)) |
12799 | { |
12800 | if (TYPE_P (t)) |
12801 | { |
12802 | if (LAMBDA_TYPE_P (t)) |
12803 | /* A closure type for a lambda in an NSDMI or default argument. |
12804 | Ignore it; it will be regenerated when needed. */ |
12805 | continue; |
12806 | |
12807 | /* If the member is a class template, we've |
12808 | already substituted its type. */ |
12809 | if (CLASS_TYPE_P (t) |
12810 | && CLASSTYPE_IS_TEMPLATE (t)) |
12811 | continue; |
12812 | |
12813 | tree newtag = tsubst (t, args, tf_error, NULL_TREE); |
12814 | if (newtag == error_mark_node) |
12815 | continue; |
12816 | |
12817 | if (TREE_CODE (newtag) != ENUMERAL_TYPE) |
12818 | { |
12819 | tree name = TYPE_IDENTIFIER (t); |
12820 | |
12821 | /* Now, install the tag. We don't use pushtag |
12822 | because that does too much work -- creating an |
12823 | implicit typedef, which we've already done. */ |
12824 | set_identifier_type_value (name, TYPE_NAME (newtag)); |
12825 | maybe_add_class_template_decl_list (type, newtag, false); |
12826 | TREE_PUBLIC (TYPE_NAME (newtag)) = true; |
12827 | determine_visibility (TYPE_NAME (newtag)); |
12828 | } |
12829 | } |
12830 | else if (DECL_DECLARES_FUNCTION_P (t)) |
12831 | { |
12832 | tree r; |
12833 | |
12834 | if (TREE_CODE (t) == TEMPLATE_DECL) |
12835 | ++processing_template_decl; |
12836 | r = tsubst (t, args, tf_error, NULL_TREE); |
12837 | if (TREE_CODE (t) == TEMPLATE_DECL) |
12838 | --processing_template_decl; |
12839 | |
12840 | set_current_access_from_decl (r); |
12841 | finish_member_declaration (r); |
12842 | /* Instantiate members marked with attribute used. */ |
12843 | if (r != error_mark_node && DECL_PRESERVE_P (r)) |
12844 | used.safe_push (obj: r); |
12845 | if (TREE_CODE (r) == FUNCTION_DECL |
12846 | && DECL_OMP_DECLARE_REDUCTION_P (r)) |
12847 | cp_check_omp_declare_reduction (r); |
12848 | } |
12849 | else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t)) |
12850 | && LAMBDA_TYPE_P (TREE_TYPE (t))) |
12851 | /* A closure type for a lambda in an NSDMI or default argument. |
12852 | Ignore it; it will be regenerated when needed. */; |
12853 | else |
12854 | { |
12855 | /* Build new TYPE_FIELDS. */ |
12856 | if (TREE_CODE (t) == STATIC_ASSERT) |
12857 | tsubst_stmt (t, args, tf_warning_or_error, NULL_TREE); |
12858 | else if (TREE_CODE (t) != CONST_DECL) |
12859 | { |
12860 | tree r; |
12861 | tree vec = NULL_TREE; |
12862 | int len = 1; |
12863 | |
12864 | gcc_checking_assert (TREE_CODE (t) != CONST_DECL); |
12865 | /* The file and line for this declaration, to |
12866 | assist in error message reporting. Since we |
12867 | called push_tinst_level above, we don't need to |
12868 | restore these. */ |
12869 | input_location = DECL_SOURCE_LOCATION (t); |
12870 | |
12871 | if (TREE_CODE (t) == TEMPLATE_DECL) |
12872 | ++processing_template_decl; |
12873 | r = tsubst (t, args, tf_warning_or_error, NULL_TREE); |
12874 | if (TREE_CODE (t) == TEMPLATE_DECL) |
12875 | --processing_template_decl; |
12876 | |
12877 | if (TREE_CODE (r) == TREE_VEC) |
12878 | { |
12879 | /* A capture pack became multiple fields. */ |
12880 | vec = r; |
12881 | len = TREE_VEC_LENGTH (vec); |
12882 | } |
12883 | |
12884 | for (int i = 0; i < len; ++i) |
12885 | { |
12886 | if (vec) |
12887 | r = TREE_VEC_ELT (vec, i); |
12888 | if (VAR_P (r)) |
12889 | { |
12890 | /* In [temp.inst]: |
12891 | |
12892 | [t]he initialization (and any associated |
12893 | side-effects) of a static data member does |
12894 | not occur unless the static data member is |
12895 | itself used in a way that requires the |
12896 | definition of the static data member to |
12897 | exist. |
12898 | |
12899 | Therefore, we do not substitute into the |
12900 | initialized for the static data member here. */ |
12901 | finish_static_data_member_decl |
12902 | (r, |
12903 | /*init=*/NULL_TREE, |
12904 | /*init_const_expr_p=*/false, |
12905 | /*asmspec_tree=*/NULL_TREE, |
12906 | /*flags=*/0); |
12907 | /* Instantiate members marked with attribute used. */ |
12908 | if (r != error_mark_node && DECL_PRESERVE_P (r)) |
12909 | used.safe_push (obj: r); |
12910 | } |
12911 | else if (TREE_CODE (r) == FIELD_DECL) |
12912 | { |
12913 | /* Determine whether R has a valid type and can be |
12914 | completed later. If R is invalid, then its type |
12915 | is replaced by error_mark_node. */ |
12916 | tree rtype = TREE_TYPE (r); |
12917 | if (can_complete_type_without_circularity (type: rtype)) |
12918 | complete_type (rtype); |
12919 | |
12920 | if (!complete_or_array_type_p (type: rtype)) |
12921 | { |
12922 | /* If R's type couldn't be completed and |
12923 | it isn't a flexible array member (whose |
12924 | type is incomplete by definition) give |
12925 | an error. */ |
12926 | cxx_incomplete_type_error (value: r, type: rtype); |
12927 | TREE_TYPE (r) = error_mark_node; |
12928 | } |
12929 | else if (TREE_CODE (rtype) == ARRAY_TYPE |
12930 | && TYPE_DOMAIN (rtype) == NULL_TREE |
12931 | && (TREE_CODE (type) == UNION_TYPE |
12932 | || TREE_CODE (type) == QUAL_UNION_TYPE)) |
12933 | { |
12934 | error ("flexible array member %qD in union", r); |
12935 | TREE_TYPE (r) = error_mark_node; |
12936 | } |
12937 | else if (!verify_type_context (input_location, |
12938 | TCTX_FIELD, rtype)) |
12939 | TREE_TYPE (r) = error_mark_node; |
12940 | } |
12941 | |
12942 | /* If it is a TYPE_DECL for a class-scoped |
12943 | ENUMERAL_TYPE, such a thing will already have |
12944 | been added to the field list by tsubst_enum |
12945 | in finish_member_declaration case above. */ |
12946 | if (!(TREE_CODE (r) == TYPE_DECL |
12947 | && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE |
12948 | && DECL_ARTIFICIAL (r))) |
12949 | { |
12950 | set_current_access_from_decl (r); |
12951 | finish_member_declaration (r); |
12952 | } |
12953 | } |
12954 | } |
12955 | } |
12956 | } |
12957 | else |
12958 | { |
12959 | if (TREE_CODE (t) == TU_LOCAL_ENTITY) |
12960 | /* Ignore. */; |
12961 | else if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t) |
12962 | || DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
12963 | { |
12964 | /* Build new CLASSTYPE_FRIEND_CLASSES. */ |
12965 | |
12966 | tree friend_type = t; |
12967 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
12968 | { |
12969 | /* template <class T> friend class C; */ |
12970 | friend_type = tsubst_friend_class (friend_tmpl: friend_type, args); |
12971 | } |
12972 | else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE) |
12973 | { |
12974 | /* template <class T> friend class C::D; */ |
12975 | friend_type = tsubst (friend_type, args, |
12976 | tf_warning_or_error, NULL_TREE); |
12977 | if (TREE_CODE (friend_type) == TEMPLATE_DECL) |
12978 | friend_type = TREE_TYPE (friend_type); |
12979 | } |
12980 | else if (TREE_CODE (friend_type) == TYPENAME_TYPE |
12981 | || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM) |
12982 | { |
12983 | /* This could be either |
12984 | |
12985 | friend class T::C; |
12986 | |
12987 | when dependent_type_p is false or |
12988 | |
12989 | template <class U> friend class T::C; |
12990 | |
12991 | otherwise. */ |
12992 | /* Bump processing_template_decl in case this is something like |
12993 | template <class T> friend struct A<T>::B. */ |
12994 | ++processing_template_decl; |
12995 | friend_type = tsubst (friend_type, args, |
12996 | tf_warning_or_error, NULL_TREE); |
12997 | --processing_template_decl; |
12998 | } |
12999 | else if (PACK_EXPANSION_P (friend_type)) |
13000 | { |
13001 | friend_type = tsubst_pack_expansion (friend_type, args, |
13002 | tf_warning_or_error, |
13003 | NULL_TREE); |
13004 | if (friend_type != error_mark_node) |
13005 | { |
13006 | unsigned int len = TREE_VEC_LENGTH (friend_type); |
13007 | for (unsigned int idx = 0; idx < len; ++idx) |
13008 | if (TREE_VEC_ELT (friend_type, idx) != error_mark_node) |
13009 | make_friend_class (type, |
13010 | TREE_VEC_ELT (friend_type, idx), |
13011 | /*complain=*/false); |
13012 | } |
13013 | friend_type = error_mark_node; |
13014 | } |
13015 | else if (uses_template_parms (t: friend_type)) |
13016 | /* friend class C<T>; */ |
13017 | friend_type = tsubst (friend_type, args, |
13018 | tf_warning_or_error, NULL_TREE); |
13019 | |
13020 | /* Otherwise it's |
13021 | |
13022 | friend class C; |
13023 | |
13024 | where C is already declared or |
13025 | |
13026 | friend class C<int>; |
13027 | |
13028 | We don't have to do anything in these cases. */ |
13029 | |
13030 | if (friend_type != error_mark_node) |
13031 | make_friend_class (type, friend_type, /*complain=*/false); |
13032 | } |
13033 | else |
13034 | { |
13035 | /* Build new DECL_FRIENDLIST. */ |
13036 | tree r; |
13037 | |
13038 | /* The file and line for this declaration, to |
13039 | assist in error message reporting. Since we |
13040 | called push_tinst_level above, we don't need to |
13041 | restore these. */ |
13042 | input_location = DECL_SOURCE_LOCATION (t); |
13043 | |
13044 | if (TREE_CODE (t) == TEMPLATE_DECL) |
13045 | { |
13046 | ++processing_template_decl; |
13047 | push_deferring_access_checks (dk_no_check); |
13048 | } |
13049 | |
13050 | r = tsubst_friend_function (decl: t, args); |
13051 | add_friend (type, r, /*complain=*/false); |
13052 | if (TREE_CODE (t) == TEMPLATE_DECL) |
13053 | { |
13054 | pop_deferring_access_checks (); |
13055 | --processing_template_decl; |
13056 | } |
13057 | } |
13058 | } |
13059 | } |
13060 | |
13061 | if (fn_context) |
13062 | { |
13063 | /* Restore these before substituting into the lambda capture |
13064 | initializers. */ |
13065 | cp_unevaluated_operand = saved_unevaluated_operand; |
13066 | c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings; |
13067 | } |
13068 | |
13069 | /* Set the file and line number information to whatever is given for |
13070 | the class itself. This puts error messages involving generated |
13071 | implicit functions at a predictable point, and the same point |
13072 | that would be used for non-template classes. */ |
13073 | input_location = DECL_SOURCE_LOCATION (typedecl); |
13074 | |
13075 | unreverse_member_declarations (type); |
13076 | finish_struct_1 (type); |
13077 | TYPE_BEING_DEFINED (type) = 0; |
13078 | |
13079 | finish_lambda_scope (); |
13080 | |
13081 | /* Remember if instantiating this class ran into errors, so we can avoid |
13082 | instantiating member functions in limit_bad_template_recursion. We set |
13083 | this flag even if the problem was in another instantiation triggered by |
13084 | this one, as that will likely also cause trouble for member functions. */ |
13085 | if (errorcount + sorrycount > current_tinst_level->errors) |
13086 | CLASSTYPE_ERRONEOUS (type) = true; |
13087 | |
13088 | /* We don't instantiate default arguments for member functions. 14.7.1: |
13089 | |
13090 | The implicit instantiation of a class template specialization causes |
13091 | the implicit instantiation of the declarations, but not of the |
13092 | definitions or default arguments, of the class member functions, |
13093 | member classes, static data members and member templates.... */ |
13094 | |
13095 | perform_instantiation_time_access_checks (tmpl: pattern, targs: args); |
13096 | perform_deferred_access_checks (tf_warning_or_error); |
13097 | |
13098 | /* Now that we've gone through all the members, instantiate those |
13099 | marked with attribute used. We must do this in the context of |
13100 | the class -- not the context we pushed from, as that might be |
13101 | inside a template and change the behaviour of mark_used. */ |
13102 | for (tree x : used) |
13103 | mark_used (x); |
13104 | |
13105 | pop_nested_class (); |
13106 | maximum_field_alignment = saved_maximum_field_alignment; |
13107 | if (!fn_context) |
13108 | pop_from_top_level (); |
13109 | pop_tinst_level (); |
13110 | |
13111 | /* The vtable for a template class can be emitted in any translation |
13112 | unit in which the class is instantiated. When there is no key |
13113 | method, however, finish_struct_1 will already have added TYPE to |
13114 | the keyed_classes. */ |
13115 | if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type)) |
13116 | vec_safe_push (v&: keyed_classes, obj: type); |
13117 | |
13118 | return type; |
13119 | } |
13120 | |
13121 | tree |
13122 | tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
13123 | { |
13124 | tree r; |
13125 | |
13126 | if (!t) |
13127 | r = t; |
13128 | else if (TYPE_P (t)) |
13129 | r = tsubst (t, args, complain, in_decl); |
13130 | else |
13131 | { |
13132 | if (!(complain & tf_warning)) |
13133 | ++c_inhibit_evaluation_warnings; |
13134 | r = tsubst_expr (t, args, complain, in_decl); |
13135 | if (!(complain & tf_warning)) |
13136 | --c_inhibit_evaluation_warnings; |
13137 | } |
13138 | |
13139 | return r; |
13140 | } |
13141 | |
13142 | /* Given a function parameter pack TMPL_PARM and some function parameters |
13143 | instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them |
13144 | and set *SPEC_P to point at the next point in the list. */ |
13145 | |
13146 | tree |
13147 | extract_fnparm_pack (tree tmpl_parm, tree *spec_p) |
13148 | { |
13149 | /* Collect all of the extra "packed" parameters into an |
13150 | argument pack. */ |
13151 | tree argpack; |
13152 | tree spec_parm = *spec_p; |
13153 | int len; |
13154 | |
13155 | for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm)) |
13156 | if (tmpl_parm |
13157 | && !function_parameter_expanded_from_pack_p (param_decl: spec_parm, pack: tmpl_parm)) |
13158 | break; |
13159 | |
13160 | spec_parm = *spec_p; |
13161 | if (len == 1 && DECL_PACK_P (spec_parm)) |
13162 | { |
13163 | /* The instantiation is still a parameter pack; don't wrap it in a |
13164 | NONTYPE_ARGUMENT_PACK. */ |
13165 | argpack = spec_parm; |
13166 | spec_parm = DECL_CHAIN (spec_parm); |
13167 | } |
13168 | else |
13169 | { |
13170 | /* Fill in PARMVEC with all of the parameters. */ |
13171 | tree parmvec = make_tree_vec (len); |
13172 | argpack = make_node (NONTYPE_ARGUMENT_PACK); |
13173 | for (int i = 0; i < len; i++) |
13174 | { |
13175 | tree elt = spec_parm; |
13176 | if (DECL_PACK_P (elt)) |
13177 | elt = make_pack_expansion (arg: elt); |
13178 | TREE_VEC_ELT (parmvec, i) = elt; |
13179 | spec_parm = DECL_CHAIN (spec_parm); |
13180 | } |
13181 | |
13182 | /* Build the argument packs. */ |
13183 | ARGUMENT_PACK_ARGS (argpack) = parmvec; |
13184 | } |
13185 | *spec_p = spec_parm; |
13186 | |
13187 | return argpack; |
13188 | } |
13189 | |
13190 | /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a |
13191 | NONTYPE_ARGUMENT_PACK. */ |
13192 | |
13193 | static tree |
13194 | make_fnparm_pack (tree spec_parm) |
13195 | { |
13196 | return extract_fnparm_pack (NULL_TREE, spec_p: &spec_parm); |
13197 | } |
13198 | |
13199 | /* Return 1 if the Ith element of the argument pack ARG_PACK is a |
13200 | pack expansion with no extra args, 2 if it has extra args, or 0 |
13201 | if it is not a pack expansion. */ |
13202 | |
13203 | static int |
13204 | argument_pack_element_is_expansion_p (tree arg_pack, int i) |
13205 | { |
13206 | if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) |
13207 | /* We're being called before this happens in tsubst_pack_expansion. */ |
13208 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); |
13209 | tree vec = ARGUMENT_PACK_ARGS (arg_pack); |
13210 | if (i >= TREE_VEC_LENGTH (vec)) |
13211 | return 0; |
13212 | tree elt = TREE_VEC_ELT (vec, i); |
13213 | if (DECL_P (elt)) |
13214 | /* A decl pack is itself an expansion. */ |
13215 | elt = TREE_TYPE (elt); |
13216 | if (!PACK_EXPANSION_P (elt)) |
13217 | return 0; |
13218 | if (PACK_EXPANSION_EXTRA_ARGS (elt)) |
13219 | return 2; |
13220 | return 1; |
13221 | } |
13222 | |
13223 | |
13224 | /* Creates and return an ARGUMENT_PACK_SELECT tree node. */ |
13225 | |
13226 | static tree |
13227 | make_argument_pack_select (tree arg_pack, unsigned index) |
13228 | { |
13229 | tree aps = make_node (ARGUMENT_PACK_SELECT); |
13230 | |
13231 | ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack; |
13232 | ARGUMENT_PACK_SELECT_INDEX (aps) = index; |
13233 | |
13234 | return aps; |
13235 | } |
13236 | |
13237 | /* This is a subroutine of tsubst_pack_expansion. |
13238 | |
13239 | It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS |
13240 | mechanism to store the (non complete list of) arguments of the |
13241 | substitution and return a non substituted pack expansion, in order |
13242 | to wait for when we have enough arguments to really perform the |
13243 | substitution. */ |
13244 | |
13245 | static bool |
13246 | use_pack_expansion_extra_args_p (tree t, |
13247 | tree parm_packs, |
13248 | int arg_pack_len, |
13249 | bool has_empty_arg) |
13250 | { |
13251 | if (has_empty_arg |
13252 | && PACK_EXPANSION_FORCE_EXTRA_ARGS_P (t)) |
13253 | return true; |
13254 | |
13255 | /* If one pack has an expansion and another pack has a normal |
13256 | argument or if one pack has an empty argument and an another |
13257 | one hasn't then tsubst_pack_expansion cannot perform the |
13258 | substitution and need to fall back on the |
13259 | PACK_EXPANSION_EXTRA mechanism. */ |
13260 | if (parm_packs == NULL_TREE) |
13261 | return false; |
13262 | else if (has_empty_arg) |
13263 | { |
13264 | /* If all the actual packs are pack expansions, we can still |
13265 | subsitute directly. */ |
13266 | for (tree p = parm_packs; p; p = TREE_CHAIN (p)) |
13267 | { |
13268 | tree a = TREE_VALUE (p); |
13269 | if (TREE_CODE (a) == ARGUMENT_PACK_SELECT) |
13270 | a = ARGUMENT_PACK_SELECT_FROM_PACK (a); |
13271 | a = ARGUMENT_PACK_ARGS (a); |
13272 | if (TREE_VEC_LENGTH (a) == 1) |
13273 | a = TREE_VEC_ELT (a, 0); |
13274 | if (PACK_EXPANSION_P (a)) |
13275 | continue; |
13276 | return true; |
13277 | } |
13278 | return false; |
13279 | } |
13280 | |
13281 | for (int i = 0 ; i < arg_pack_len; ++i) |
13282 | { |
13283 | bool has_expansion_arg = false; |
13284 | bool has_non_expansion_arg = false; |
13285 | for (tree parm_pack = parm_packs; |
13286 | parm_pack; |
13287 | parm_pack = TREE_CHAIN (parm_pack)) |
13288 | { |
13289 | tree arg = TREE_VALUE (parm_pack); |
13290 | |
13291 | int exp = argument_pack_element_is_expansion_p (arg_pack: arg, i); |
13292 | if (exp == 2) |
13293 | /* We can't substitute a pack expansion with extra args into |
13294 | our pattern. */ |
13295 | return true; |
13296 | else if (exp) |
13297 | has_expansion_arg = true; |
13298 | else |
13299 | has_non_expansion_arg = true; |
13300 | } |
13301 | |
13302 | if (has_expansion_arg && has_non_expansion_arg) |
13303 | { |
13304 | /* We can get here with: |
13305 | |
13306 | template <class... Ts> struct X { |
13307 | template <class... Us> using Y = Z<void(Ts, Us)...>; |
13308 | }; |
13309 | template <class A, class... P> |
13310 | using foo = X<int, int>::Y<A, P...>; |
13311 | |
13312 | where we compare int and A and then the second int and P..., |
13313 | whose expansion-ness doesn't match, but that's OK. */ |
13314 | return true; |
13315 | } |
13316 | } |
13317 | return false; |
13318 | } |
13319 | |
13320 | /* [temp.variadic]/6 says that: |
13321 | |
13322 | The instantiation of a pack expansion [...] |
13323 | produces a list E1,E2, ..., En, where N is the number of elements |
13324 | in the pack expansion parameters. |
13325 | |
13326 | This subroutine of tsubst_pack_expansion produces one of these Ei. |
13327 | |
13328 | PATTERN is the pattern of the pack expansion. PARM_PACKS is a |
13329 | TREE_LIST in which each TREE_PURPOSE is a parameter pack of |
13330 | PATTERN, and each TREE_VALUE is its corresponding argument pack. |
13331 | INDEX is the index 'i' of the element Ei to produce. ARGS, |
13332 | COMPLAIN, and IN_DECL are the same parameters as for the |
13333 | tsubst_pack_expansion function. |
13334 | |
13335 | The function returns the resulting Ei upon successful completion, |
13336 | or error_mark_node. |
13337 | |
13338 | Note that this function possibly modifies the ARGS parameter, so |
13339 | it's the responsibility of the caller to restore it. */ |
13340 | |
13341 | static tree |
13342 | gen_elem_of_pack_expansion_instantiation (tree pattern, |
13343 | tree parm_packs, |
13344 | unsigned index, |
13345 | tree args /* This parm gets |
13346 | modified. */, |
13347 | tsubst_flags_t complain, |
13348 | tree in_decl) |
13349 | { |
13350 | tree t; |
13351 | bool ith_elem_is_expansion = false; |
13352 | |
13353 | /* For each parameter pack, change the substitution of the parameter |
13354 | pack to the ith argument in its argument pack, then expand the |
13355 | pattern. */ |
13356 | for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack)) |
13357 | { |
13358 | tree parm = TREE_PURPOSE (pack); |
13359 | tree arg_pack = TREE_VALUE (pack); |
13360 | tree aps; /* instance of ARGUMENT_PACK_SELECT. */ |
13361 | |
13362 | ith_elem_is_expansion |= |
13363 | argument_pack_element_is_expansion_p (arg_pack, i: index); |
13364 | |
13365 | /* Select the Ith argument from the pack. */ |
13366 | if (TREE_CODE (parm) == PARM_DECL |
13367 | || VAR_P (parm) |
13368 | || TREE_CODE (parm) == FIELD_DECL) |
13369 | { |
13370 | if (index == 0) |
13371 | { |
13372 | aps = make_argument_pack_select (arg_pack, index); |
13373 | if (!mark_used (parm, complain) && !(complain & tf_error)) |
13374 | return error_mark_node; |
13375 | register_local_specialization (spec: aps, tmpl: parm); |
13376 | } |
13377 | else |
13378 | aps = retrieve_local_specialization (tmpl: parm); |
13379 | } |
13380 | else |
13381 | { |
13382 | int idx, level; |
13383 | template_parm_level_and_index (parm, &level, &idx); |
13384 | |
13385 | if (index == 0) |
13386 | { |
13387 | aps = make_argument_pack_select (arg_pack, index); |
13388 | /* Update the corresponding argument. */ |
13389 | TMPL_ARG (args, level, idx) = aps; |
13390 | } |
13391 | else |
13392 | /* Re-use the ARGUMENT_PACK_SELECT. */ |
13393 | aps = TMPL_ARG (args, level, idx); |
13394 | } |
13395 | ARGUMENT_PACK_SELECT_INDEX (aps) = index; |
13396 | } |
13397 | |
13398 | /* Substitute into the PATTERN with the (possibly altered) |
13399 | arguments. */ |
13400 | if (pattern == in_decl) |
13401 | /* Expanding a fixed parameter pack from |
13402 | coerce_template_parameter_pack. */ |
13403 | t = tsubst_decl (pattern, args, complain); |
13404 | else if (pattern == error_mark_node) |
13405 | t = error_mark_node; |
13406 | else if (!TYPE_P (pattern)) |
13407 | t = tsubst_expr (pattern, args, complain, in_decl); |
13408 | else |
13409 | { |
13410 | t = tsubst (pattern, args, complain, in_decl); |
13411 | if (is_auto (t) && !ith_elem_is_expansion) |
13412 | /* When expanding the fake auto... pack expansion from add_capture, we |
13413 | need to mark that the expansion is no longer a pack. */ |
13414 | TEMPLATE_TYPE_PARAMETER_PACK (t) = false; |
13415 | } |
13416 | |
13417 | /* If the Ith argument pack element is a pack expansion, then |
13418 | the Ith element resulting from the substituting is going to |
13419 | be a pack expansion as well. */ |
13420 | if (ith_elem_is_expansion) |
13421 | t = make_pack_expansion (arg: t, complain); |
13422 | |
13423 | return t; |
13424 | } |
13425 | |
13426 | /* When the unexpanded parameter pack in a fold expression expands to an empty |
13427 | sequence, the value of the expression is as follows; the program is |
13428 | ill-formed if the operator is not listed in this table. |
13429 | |
13430 | && true |
13431 | || false |
13432 | , void() */ |
13433 | |
13434 | tree |
13435 | expand_empty_fold (tree t, tsubst_flags_t complain) |
13436 | { |
13437 | tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0)); |
13438 | if (!FOLD_EXPR_MODIFY_P (t)) |
13439 | switch (code) |
13440 | { |
13441 | case TRUTH_ANDIF_EXPR: |
13442 | return boolean_true_node; |
13443 | case TRUTH_ORIF_EXPR: |
13444 | return boolean_false_node; |
13445 | case COMPOUND_EXPR: |
13446 | return void_node; |
13447 | default: |
13448 | break; |
13449 | } |
13450 | |
13451 | if (complain & tf_error) |
13452 | error_at (location_of (t), |
13453 | "fold of empty expansion over %O", code); |
13454 | return error_mark_node; |
13455 | } |
13456 | |
13457 | /* Given a fold-expression T and a current LEFT and RIGHT operand, |
13458 | form an expression that combines the two terms using the |
13459 | operator of T. */ |
13460 | |
13461 | static tree |
13462 | fold_expression (tree t, tree left, tree right, tsubst_flags_t complain) |
13463 | { |
13464 | tree_code code = FOLD_EXPR_OP (t); |
13465 | |
13466 | tree lookups = templated_operator_saved_lookups (t); |
13467 | |
13468 | // Handle compound assignment operators. |
13469 | if (FOLD_EXPR_MODIFY_P (t)) |
13470 | return build_x_modify_expr (input_location, left, code, right, |
13471 | lookups, complain); |
13472 | |
13473 | warning_sentinel s(warn_parentheses); |
13474 | switch (code) |
13475 | { |
13476 | case COMPOUND_EXPR: |
13477 | return build_x_compound_expr (input_location, left, right, |
13478 | lookups, complain); |
13479 | default: |
13480 | return build_x_binary_op (input_location, code, |
13481 | left, TREE_CODE (left), |
13482 | right, TREE_CODE (right), |
13483 | lookups, /*overload=*/NULL, |
13484 | complain); |
13485 | } |
13486 | } |
13487 | |
13488 | /* Substitute ARGS into the pack of a fold expression T. */ |
13489 | |
13490 | static inline tree |
13491 | tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
13492 | { |
13493 | return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl); |
13494 | } |
13495 | |
13496 | /* Substitute ARGS into the pack of a fold expression T. */ |
13497 | |
13498 | static inline tree |
13499 | tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
13500 | { |
13501 | return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl); |
13502 | } |
13503 | |
13504 | /* Expand a PACK of arguments into a grouped as left fold. |
13505 | Given a pack containing elements A0, A1, ..., An and an |
13506 | operator @, this builds the expression: |
13507 | |
13508 | ((A0 @ A1) @ A2) ... @ An |
13509 | |
13510 | Note that PACK must not be empty. |
13511 | |
13512 | The operator is defined by the original fold expression T. */ |
13513 | |
13514 | static tree |
13515 | expand_left_fold (tree t, tree pack, tsubst_flags_t complain) |
13516 | { |
13517 | tree left = TREE_VEC_ELT (pack, 0); |
13518 | for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i) |
13519 | { |
13520 | tree right = TREE_VEC_ELT (pack, i); |
13521 | left = fold_expression (t, left, right, complain); |
13522 | } |
13523 | return left; |
13524 | } |
13525 | |
13526 | /* Substitute into a unary left fold expression. */ |
13527 | |
13528 | static tree |
13529 | tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain, |
13530 | tree in_decl) |
13531 | { |
13532 | tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl); |
13533 | if (pack == error_mark_node) |
13534 | return error_mark_node; |
13535 | if (PACK_EXPANSION_P (pack)) |
13536 | { |
13537 | tree r = copy_node (t); |
13538 | FOLD_EXPR_PACK (r) = pack; |
13539 | return r; |
13540 | } |
13541 | if (TREE_VEC_LENGTH (pack) == 0) |
13542 | return expand_empty_fold (t, complain); |
13543 | else |
13544 | return expand_left_fold (t, pack, complain); |
13545 | } |
13546 | |
13547 | /* Substitute into a binary left fold expression. |
13548 | |
13549 | Do ths by building a single (non-empty) vector of argumnts and |
13550 | building the expression from those elements. */ |
13551 | |
13552 | static tree |
13553 | tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain, |
13554 | tree in_decl) |
13555 | { |
13556 | tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl); |
13557 | if (pack == error_mark_node) |
13558 | return error_mark_node; |
13559 | tree init = tsubst_fold_expr_init (t, args, complain, in_decl); |
13560 | if (init == error_mark_node) |
13561 | return error_mark_node; |
13562 | |
13563 | if (PACK_EXPANSION_P (pack)) |
13564 | { |
13565 | tree r = copy_node (t); |
13566 | FOLD_EXPR_PACK (r) = pack; |
13567 | FOLD_EXPR_INIT (r) = init; |
13568 | return r; |
13569 | } |
13570 | |
13571 | tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1); |
13572 | TREE_VEC_ELT (vec, 0) = init; |
13573 | for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i) |
13574 | TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i); |
13575 | |
13576 | return expand_left_fold (t, pack: vec, complain); |
13577 | } |
13578 | |
13579 | /* Expand a PACK of arguments into a grouped as right fold. |
13580 | Given a pack containing elementns A0, A1, ..., and an |
13581 | operator @, this builds the expression: |
13582 | |
13583 | A0@ ... (An-2 @ (An-1 @ An)) |
13584 | |
13585 | Note that PACK must not be empty. |
13586 | |
13587 | The operator is defined by the original fold expression T. */ |
13588 | |
13589 | tree |
13590 | expand_right_fold (tree t, tree pack, tsubst_flags_t complain) |
13591 | { |
13592 | // Build the expression. |
13593 | int n = TREE_VEC_LENGTH (pack); |
13594 | tree right = TREE_VEC_ELT (pack, n - 1); |
13595 | for (--n; n != 0; --n) |
13596 | { |
13597 | tree left = TREE_VEC_ELT (pack, n - 1); |
13598 | right = fold_expression (t, left, right, complain); |
13599 | } |
13600 | return right; |
13601 | } |
13602 | |
13603 | /* Substitute into a unary right fold expression. */ |
13604 | |
13605 | static tree |
13606 | tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain, |
13607 | tree in_decl) |
13608 | { |
13609 | tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl); |
13610 | if (pack == error_mark_node) |
13611 | return error_mark_node; |
13612 | if (PACK_EXPANSION_P (pack)) |
13613 | { |
13614 | tree r = copy_node (t); |
13615 | FOLD_EXPR_PACK (r) = pack; |
13616 | return r; |
13617 | } |
13618 | if (TREE_VEC_LENGTH (pack) == 0) |
13619 | return expand_empty_fold (t, complain); |
13620 | else |
13621 | return expand_right_fold (t, pack, complain); |
13622 | } |
13623 | |
13624 | /* Substitute into a binary right fold expression. |
13625 | |
13626 | Do ths by building a single (non-empty) vector of arguments and |
13627 | building the expression from those elements. */ |
13628 | |
13629 | static tree |
13630 | tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain, |
13631 | tree in_decl) |
13632 | { |
13633 | tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl); |
13634 | if (pack == error_mark_node) |
13635 | return error_mark_node; |
13636 | tree init = tsubst_fold_expr_init (t, args, complain, in_decl); |
13637 | if (init == error_mark_node) |
13638 | return error_mark_node; |
13639 | |
13640 | if (PACK_EXPANSION_P (pack)) |
13641 | { |
13642 | tree r = copy_node (t); |
13643 | FOLD_EXPR_PACK (r) = pack; |
13644 | FOLD_EXPR_INIT (r) = init; |
13645 | return r; |
13646 | } |
13647 | |
13648 | int n = TREE_VEC_LENGTH (pack); |
13649 | tree vec = make_tree_vec (n + 1); |
13650 | for (int i = 0; i < n; ++i) |
13651 | TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i); |
13652 | TREE_VEC_ELT (vec, n) = init; |
13653 | |
13654 | return expand_right_fold (t, pack: vec, complain); |
13655 | } |
13656 | |
13657 | /* Walk through the pattern of a pack expansion, adding everything in |
13658 | local_specializations to a list. */ |
13659 | |
13660 | class el_data |
13661 | { |
13662 | public: |
13663 | /* Set of variables declared within the pattern. */ |
13664 | hash_set<tree> internal; |
13665 | /* Set of AST nodes that have been visited by the traversal. */ |
13666 | hash_set<tree> visited; |
13667 | /* List of local_specializations used within the pattern. */ |
13668 | tree extra; |
13669 | tsubst_flags_t complain; |
13670 | /* True iff we don't want to walk into unevaluated contexts. */ |
13671 | bool skip_unevaluated_operands = false; |
13672 | /* The unevaluated contexts that we avoided walking. */ |
13673 | auto_vec<tree> skipped_trees; |
13674 | |
13675 | el_data (tsubst_flags_t c) |
13676 | : extra (NULL_TREE), complain (c) {} |
13677 | }; |
13678 | static tree |
13679 | extract_locals_r (tree *tp, int *walk_subtrees, void *data_) |
13680 | { |
13681 | el_data &data = *reinterpret_cast<el_data*>(data_); |
13682 | tree *extra = &data.extra; |
13683 | tsubst_flags_t complain = data.complain; |
13684 | |
13685 | if (data.skip_unevaluated_operands |
13686 | && unevaluated_p (TREE_CODE (*tp))) |
13687 | { |
13688 | data.skipped_trees.safe_push (obj: *tp); |
13689 | *walk_subtrees = 0; |
13690 | return NULL_TREE; |
13691 | } |
13692 | |
13693 | if (TYPE_P (*tp) && typedef_variant_p (type: *tp)) |
13694 | /* Remember local typedefs (85214). */ |
13695 | tp = &TYPE_NAME (*tp); |
13696 | |
13697 | if (has_extra_args_mechanism_p (t: *tp)) |
13698 | /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and |
13699 | potentially see a previously captured local in an evaluated context |
13700 | that's really only used in an unevaluated context (PR114303). This |
13701 | means callers of build_extra_args need to clear *_EXTRA_ARGS of the |
13702 | outermost tree. Nested *_EXTRA_ARGS should naturally be empty since |
13703 | the outermost (extra-args) tree will intercept any substitution before |
13704 | a nested tree can. */ |
13705 | gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE |
13706 | /* Except a lambda nested inside an extra-args tree |
13707 | can have extra args if we deferred partial |
13708 | substitution into it at template parse time. But |
13709 | we don't walk LAMBDA_EXPR_EXTRA_ARGS anyway. */ |
13710 | || TREE_CODE (*tp) == LAMBDA_EXPR); |
13711 | |
13712 | if (TREE_CODE (*tp) == DECL_EXPR) |
13713 | { |
13714 | tree decl = DECL_EXPR_DECL (*tp); |
13715 | data.internal.add (k: decl); |
13716 | if (DECL_DECOMPOSITION_P (decl) |
13717 | && TREE_TYPE (decl) != error_mark_node) |
13718 | { |
13719 | gcc_assert (DECL_NAME (decl) == NULL_TREE); |
13720 | for (tree decl2 = DECL_CHAIN (decl); |
13721 | decl2 |
13722 | && DECL_DECOMPOSITION_P (decl2) |
13723 | && DECL_NAME (decl2) |
13724 | && TREE_TYPE (decl2) != error_mark_node; |
13725 | decl2 = DECL_CHAIN (decl2)) |
13726 | { |
13727 | gcc_assert (DECL_DECOMP_BASE (decl2) == decl); |
13728 | data.internal.add (k: decl2); |
13729 | } |
13730 | } |
13731 | } |
13732 | else if (TREE_CODE (*tp) == LAMBDA_EXPR) |
13733 | { |
13734 | /* Since we defer implicit capture, look in the parms and body. */ |
13735 | tree fn = lambda_function (*tp); |
13736 | cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data, |
13737 | &data.visited); |
13738 | cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data, |
13739 | &data.visited); |
13740 | } |
13741 | else if (tree spec = retrieve_local_specialization (tmpl: *tp)) |
13742 | { |
13743 | if (data.internal.contains (k: *tp)) |
13744 | /* Don't mess with variables declared within the pattern. */ |
13745 | return NULL_TREE; |
13746 | if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK) |
13747 | { |
13748 | /* Maybe pull out the PARM_DECL for a partial instantiation. */ |
13749 | tree args = ARGUMENT_PACK_ARGS (spec); |
13750 | if (TREE_VEC_LENGTH (args) == 1) |
13751 | { |
13752 | tree elt = TREE_VEC_ELT (args, 0); |
13753 | if (PACK_EXPANSION_P (elt)) |
13754 | elt = PACK_EXPANSION_PATTERN (elt); |
13755 | if (DECL_PACK_P (elt)) |
13756 | spec = elt; |
13757 | } |
13758 | if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK) |
13759 | { |
13760 | /* Handle lambda capture here, since we aren't doing any |
13761 | substitution now, and so tsubst_copy won't call |
13762 | process_outer_var_ref. */ |
13763 | tree args = ARGUMENT_PACK_ARGS (spec); |
13764 | int len = TREE_VEC_LENGTH (args); |
13765 | for (int i = 0; i < len; ++i) |
13766 | { |
13767 | tree arg = TREE_VEC_ELT (args, i); |
13768 | tree carg = arg; |
13769 | if (outer_automatic_var_p (arg)) |
13770 | carg = process_outer_var_ref (arg, complain); |
13771 | if (carg != arg) |
13772 | { |
13773 | /* Make a new NONTYPE_ARGUMENT_PACK of the capture |
13774 | proxies. */ |
13775 | if (i == 0) |
13776 | { |
13777 | spec = copy_node (spec); |
13778 | args = copy_node (args); |
13779 | ARGUMENT_PACK_ARGS (spec) = args; |
13780 | register_local_specialization (spec, tmpl: *tp); |
13781 | } |
13782 | TREE_VEC_ELT (args, i) = carg; |
13783 | } |
13784 | } |
13785 | } |
13786 | } |
13787 | if (outer_automatic_var_p (spec)) |
13788 | spec = process_outer_var_ref (spec, complain); |
13789 | *extra = tree_cons (*tp, spec, *extra); |
13790 | } |
13791 | return NULL_TREE; |
13792 | } |
13793 | static tree |
13794 | extract_local_specs (tree pattern, tsubst_flags_t complain) |
13795 | { |
13796 | el_data data (complain); |
13797 | /* Walk the pattern twice, ignoring unevaluated operands the first time |
13798 | around, so that if a local specialization appears in both an evaluated |
13799 | and unevaluated context we prefer to process it in the evaluated context |
13800 | (since e.g. process_outer_var_ref is a no-op inside an unevaluated |
13801 | context). */ |
13802 | data.skip_unevaluated_operands = true; |
13803 | cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited); |
13804 | /* Now walk the unevaluated contexts we skipped the first time around. */ |
13805 | data.skip_unevaluated_operands = false; |
13806 | for (tree t : data.skipped_trees) |
13807 | { |
13808 | data.visited.remove (k: t); |
13809 | cp_walk_tree (&t, extract_locals_r, &data, &data.visited); |
13810 | } |
13811 | return data.extra; |
13812 | } |
13813 | |
13814 | /* Extract any uses of local_specializations from PATTERN and add them to ARGS |
13815 | for use in PACK_EXPANSION_EXTRA_ARGS. */ |
13816 | |
13817 | tree |
13818 | build_extra_args (tree pattern, tree args, tsubst_flags_t complain) |
13819 | { |
13820 | /* Make a copy of the extra arguments so that they won't get changed |
13821 | out from under us. */ |
13822 | tree extra = preserve_args (args: copy_template_args (args), /*cow_p=*/false); |
13823 | if (complain & tf_partial) |
13824 | /* Remember whether this is a partial substitution. */ |
13825 | TREE_STATIC (extra) = true; |
13826 | if (local_specializations) |
13827 | if (tree locals = extract_local_specs (pattern, complain)) |
13828 | extra = tree_cons (NULL_TREE, extra, locals); |
13829 | return extra; |
13830 | } |
13831 | |
13832 | /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the |
13833 | normal template args to ARGS. */ |
13834 | |
13835 | tree |
13836 | add_extra_args (tree extra, tree args, tsubst_flags_t complain, tree in_decl) |
13837 | { |
13838 | if (!extra) |
13839 | return args; |
13840 | |
13841 | if (TREE_CODE (extra) == TREE_LIST) |
13842 | { |
13843 | for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt)) |
13844 | { |
13845 | /* The partial instantiation involved local declarations collected in |
13846 | extract_local_specs; map from the general template to our local |
13847 | context. */ |
13848 | tree gen = TREE_PURPOSE (elt); |
13849 | tree inst = TREE_VALUE (elt); |
13850 | if (DECL_P (inst)) |
13851 | if (tree local = retrieve_local_specialization (tmpl: inst)) |
13852 | inst = local; |
13853 | /* else inst is already a full instantiation of the pack. */ |
13854 | register_local_specialization (spec: inst, tmpl: gen); |
13855 | if (is_normal_capture_proxy (gen)) |
13856 | register_local_specialization (spec: inst, DECL_CAPTURED_VARIABLE (gen)); |
13857 | } |
13858 | gcc_assert (!TREE_PURPOSE (extra)); |
13859 | extra = TREE_VALUE (extra); |
13860 | } |
13861 | if (TREE_STATIC (extra)) |
13862 | /* This is a partial substitution into e.g. a requires-expr or lambda-expr |
13863 | inside a default template argument; we expect 'extra' to be a full set |
13864 | of template arguments for the template context, so it suffices to just |
13865 | substitute into them. */ |
13866 | args = tsubst_template_args (extra, args, complain, in_decl); |
13867 | else |
13868 | args = add_to_template_args (args: extra, extra_args: args); |
13869 | return args; |
13870 | } |
13871 | |
13872 | /* Substitute ARGS into T, which is a pack expansion |
13873 | (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a |
13874 | TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node |
13875 | (if only a partial substitution could be performed) or |
13876 | ERROR_MARK_NODE if there was an error. */ |
13877 | |
13878 | tree |
13879 | tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain, |
13880 | tree in_decl) |
13881 | { |
13882 | tree pattern; |
13883 | tree pack, packs = NULL_TREE; |
13884 | bool unsubstituted_packs = false; |
13885 | int i, len = -1; |
13886 | tree result; |
13887 | bool need_local_specializations = false; |
13888 | int levels; |
13889 | |
13890 | gcc_assert (PACK_EXPANSION_P (t)); |
13891 | pattern = PACK_EXPANSION_PATTERN (t); |
13892 | |
13893 | /* Add in any args remembered from an earlier partial instantiation. */ |
13894 | args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args, complain, in_decl); |
13895 | |
13896 | levels = TMPL_ARGS_DEPTH (args); |
13897 | |
13898 | /* Determine the argument packs that will instantiate the parameter |
13899 | packs used in the expansion expression. While we're at it, |
13900 | compute the number of arguments to be expanded and make sure it |
13901 | is consistent. */ |
13902 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack; |
13903 | pack = TREE_CHAIN (pack)) |
13904 | { |
13905 | tree parm_pack = TREE_VALUE (pack); |
13906 | tree arg_pack = NULL_TREE; |
13907 | tree orig_arg = NULL_TREE; |
13908 | int level = 0; |
13909 | |
13910 | if (TREE_CODE (parm_pack) == BASES) |
13911 | { |
13912 | gcc_assert (parm_pack == pattern); |
13913 | tree type = tsubst (BASES_TYPE (parm_pack), args, complain, in_decl); |
13914 | if (BASES_DIRECT (parm_pack)) |
13915 | return calculate_direct_bases (type, complain); |
13916 | else |
13917 | return calculate_bases (type, complain); |
13918 | } |
13919 | else if (builtin_pack_call_p (call: parm_pack)) |
13920 | { |
13921 | if (parm_pack != pattern) |
13922 | { |
13923 | if (complain & tf_error) |
13924 | sorry ("%qE is not the entire pattern of the pack expansion", |
13925 | parm_pack); |
13926 | return error_mark_node; |
13927 | } |
13928 | return expand_builtin_pack_call (call: parm_pack, args, |
13929 | complain, in_decl); |
13930 | } |
13931 | else if (TREE_CODE (parm_pack) == PARM_DECL) |
13932 | { |
13933 | /* We know we have correct local_specializations if this |
13934 | expansion is at function scope, or if we're dealing with a |
13935 | local parameter in a requires expression; for the latter, |
13936 | tsubst_requires_expr set it up appropriately. */ |
13937 | if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack)) |
13938 | arg_pack = retrieve_local_specialization (tmpl: parm_pack); |
13939 | else |
13940 | /* We can't rely on local_specializations for a parameter |
13941 | name used later in a function declaration (such as in a |
13942 | late-specified return type). Even if it exists, it might |
13943 | have the wrong value for a recursive call. */ |
13944 | need_local_specializations = true; |
13945 | |
13946 | if (!arg_pack) |
13947 | { |
13948 | /* This parameter pack was used in an unevaluated context. Just |
13949 | make a dummy decl, since it's only used for its type. */ |
13950 | ++cp_unevaluated_operand; |
13951 | arg_pack = tsubst_decl (parm_pack, args, complain); |
13952 | --cp_unevaluated_operand; |
13953 | if (arg_pack && DECL_PACK_P (arg_pack)) |
13954 | /* Partial instantiation of the parm_pack, we can't build |
13955 | up an argument pack yet. */ |
13956 | arg_pack = NULL_TREE; |
13957 | else |
13958 | arg_pack = make_fnparm_pack (spec_parm: arg_pack); |
13959 | } |
13960 | else if (DECL_PACK_P (arg_pack)) |
13961 | /* This argument pack isn't fully instantiated yet. */ |
13962 | arg_pack = NULL_TREE; |
13963 | } |
13964 | else if (is_capture_proxy (parm_pack)) |
13965 | { |
13966 | arg_pack = retrieve_local_specialization (tmpl: parm_pack); |
13967 | if (DECL_PACK_P (arg_pack)) |
13968 | arg_pack = NULL_TREE; |
13969 | } |
13970 | else |
13971 | { |
13972 | int idx; |
13973 | template_parm_level_and_index (parm_pack, &level, &idx); |
13974 | if (level <= levels) |
13975 | arg_pack = TMPL_ARG (args, level, idx); |
13976 | |
13977 | if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM |
13978 | && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack)) |
13979 | arg_pack = NULL_TREE; |
13980 | } |
13981 | |
13982 | orig_arg = arg_pack; |
13983 | if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT) |
13984 | arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack); |
13985 | |
13986 | if (arg_pack && !ARGUMENT_PACK_P (arg_pack)) |
13987 | /* This can only happen if we forget to expand an argument |
13988 | pack somewhere else. Just return an error, silently. */ |
13989 | { |
13990 | result = make_tree_vec (1); |
13991 | TREE_VEC_ELT (result, 0) = error_mark_node; |
13992 | return result; |
13993 | } |
13994 | |
13995 | if (arg_pack) |
13996 | { |
13997 | int my_len = |
13998 | TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)); |
13999 | |
14000 | /* Don't bother trying to do a partial substitution with |
14001 | incomplete packs; we'll try again after deduction. */ |
14002 | if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack)) |
14003 | return t; |
14004 | |
14005 | if (len < 0) |
14006 | len = my_len; |
14007 | else if (len != my_len) |
14008 | { |
14009 | if (!(complain & tf_error)) |
14010 | /* Fail quietly. */; |
14011 | else if (TREE_CODE (t) == TYPE_PACK_EXPANSION) |
14012 | error ("mismatched argument pack lengths while expanding %qT", |
14013 | pattern); |
14014 | else |
14015 | error ("mismatched argument pack lengths while expanding %qE", |
14016 | pattern); |
14017 | return error_mark_node; |
14018 | } |
14019 | |
14020 | /* Keep track of the parameter packs and their corresponding |
14021 | argument packs. */ |
14022 | packs = tree_cons (parm_pack, arg_pack, packs); |
14023 | TREE_TYPE (packs) = orig_arg; |
14024 | } |
14025 | else |
14026 | { |
14027 | /* We can't substitute for this parameter pack. We use a flag as |
14028 | well as the missing_level counter because function parameter |
14029 | packs don't have a level. */ |
14030 | gcc_assert (processing_template_decl || is_auto (parm_pack)); |
14031 | unsubstituted_packs = true; |
14032 | } |
14033 | } |
14034 | |
14035 | /* If the expansion is just T..., return the matching argument pack, unless |
14036 | we need to call convert_from_reference on all the elements. This is an |
14037 | important optimization; see c++/68422. */ |
14038 | if (!unsubstituted_packs |
14039 | && TREE_PURPOSE (packs) == pattern) |
14040 | { |
14041 | tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs)); |
14042 | |
14043 | /* If the argument pack is a single pack expansion, pull it out. */ |
14044 | if (TREE_VEC_LENGTH (args) == 1 |
14045 | && pack_expansion_args_count (args)) |
14046 | { |
14047 | tree arg = TREE_VEC_ELT (args, 0); |
14048 | if (PACK_EXPANSION_SIZEOF_P (t) |
14049 | && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg))) |
14050 | /* Except if this isn't a simple sizeof...(T) which gets sZ |
14051 | mangling, keep the TREE_VEC to get sP mangling. */; |
14052 | else |
14053 | return TREE_VEC_ELT (args, 0); |
14054 | } |
14055 | |
14056 | /* Types need no adjustment, nor does sizeof..., and if we still have |
14057 | some pack expansion args we won't do anything yet. */ |
14058 | if (TREE_CODE (t) == TYPE_PACK_EXPANSION |
14059 | || PACK_EXPANSION_SIZEOF_P (t) |
14060 | || pack_expansion_args_count (args)) |
14061 | return args; |
14062 | /* Also optimize expression pack expansions if we can tell that the |
14063 | elements won't have reference type. */ |
14064 | tree type = TREE_TYPE (pattern); |
14065 | if (type && !TYPE_REF_P (type) |
14066 | && !PACK_EXPANSION_P (type) |
14067 | && !WILDCARD_TYPE_P (type)) |
14068 | return args; |
14069 | /* Otherwise use the normal path so we get convert_from_reference. */ |
14070 | } |
14071 | |
14072 | /* We cannot expand this expansion expression, because we don't have |
14073 | all of the argument packs we need. */ |
14074 | if (use_pack_expansion_extra_args_p (t, parm_packs: packs, arg_pack_len: len, has_empty_arg: unsubstituted_packs)) |
14075 | { |
14076 | /* We got some full packs, but we can't substitute them in until we |
14077 | have values for all the packs. So remember these until then. */ |
14078 | |
14079 | t = make_pack_expansion (arg: pattern, complain); |
14080 | PACK_EXPANSION_EXTRA_ARGS (t) |
14081 | = build_extra_args (pattern, args, complain); |
14082 | return t; |
14083 | } |
14084 | |
14085 | /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return |
14086 | type, so create our own local specializations map; the current map is |
14087 | either NULL or (in the case of recursive unification) might have |
14088 | bindings that we don't want to use or alter. */ |
14089 | local_specialization_stack lss (need_local_specializations |
14090 | ? lss_blank : lss_nop); |
14091 | |
14092 | if (unsubstituted_packs) |
14093 | { |
14094 | /* There were no real arguments, we're just replacing a parameter |
14095 | pack with another version of itself. Substitute into the |
14096 | pattern and return a PACK_EXPANSION_*. The caller will need to |
14097 | deal with that. */ |
14098 | if (TREE_CODE (t) == EXPR_PACK_EXPANSION) |
14099 | result = tsubst_expr (pattern, args, complain, in_decl); |
14100 | else |
14101 | result = tsubst (pattern, args, complain, in_decl); |
14102 | result = make_pack_expansion (arg: result, complain); |
14103 | if (result == error_mark_node) |
14104 | return error_mark_node; |
14105 | PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t); |
14106 | PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t); |
14107 | if (PACK_EXPANSION_AUTO_P (t)) |
14108 | { |
14109 | /* This is a fake auto... pack expansion created in add_capture with |
14110 | _PACKS that don't appear in the pattern. Copy one over. */ |
14111 | packs = PACK_EXPANSION_PARAMETER_PACKS (t); |
14112 | pack = retrieve_local_specialization (TREE_VALUE (packs)); |
14113 | gcc_checking_assert (DECL_PACK_P (pack)); |
14114 | PACK_EXPANSION_PARAMETER_PACKS (result) |
14115 | = build_tree_list (NULL_TREE, pack); |
14116 | PACK_EXPANSION_AUTO_P (result) = true; |
14117 | } |
14118 | return result; |
14119 | } |
14120 | |
14121 | gcc_assert (len >= 0); |
14122 | |
14123 | /* For each argument in each argument pack, substitute into the |
14124 | pattern. */ |
14125 | result = make_tree_vec (len); |
14126 | tree elem_args = copy_template_args (args); |
14127 | for (i = 0; i < len; ++i) |
14128 | { |
14129 | t = gen_elem_of_pack_expansion_instantiation (pattern, parm_packs: packs, |
14130 | index: i, |
14131 | args: elem_args, complain, |
14132 | in_decl); |
14133 | TREE_VEC_ELT (result, i) = t; |
14134 | if (t == error_mark_node) |
14135 | { |
14136 | result = error_mark_node; |
14137 | break; |
14138 | } |
14139 | } |
14140 | |
14141 | /* Update ARGS to restore the substitution from parameter packs to |
14142 | their argument packs. */ |
14143 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
14144 | { |
14145 | tree parm = TREE_PURPOSE (pack); |
14146 | |
14147 | if (TREE_CODE (parm) == PARM_DECL |
14148 | || VAR_P (parm) |
14149 | || TREE_CODE (parm) == FIELD_DECL) |
14150 | register_local_specialization (TREE_TYPE (pack), tmpl: parm); |
14151 | else |
14152 | { |
14153 | int idx, level; |
14154 | |
14155 | if (TREE_VALUE (pack) == NULL_TREE) |
14156 | continue; |
14157 | |
14158 | template_parm_level_and_index (parm, &level, &idx); |
14159 | |
14160 | /* Update the corresponding argument. */ |
14161 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) |
14162 | TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) = |
14163 | TREE_TYPE (pack); |
14164 | else |
14165 | TREE_VEC_ELT (args, idx) = TREE_TYPE (pack); |
14166 | } |
14167 | } |
14168 | |
14169 | /* If the dependent pack arguments were such that we end up with only a |
14170 | single pack expansion again, there's no need to keep it in a TREE_VEC. */ |
14171 | if (len == 1 && TREE_CODE (result) == TREE_VEC |
14172 | && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0))) |
14173 | return TREE_VEC_ELT (result, 0); |
14174 | |
14175 | return result; |
14176 | } |
14177 | |
14178 | /* Substitute ARGS into T, which is a pack index (i.e., PACK_INDEX_TYPE or |
14179 | PACK_INDEX_EXPR). Returns a single type or expression, a PACK_INDEX_* |
14180 | node if only a partial substitution could be performed, or ERROR_MARK_NODE |
14181 | if there was an error. */ |
14182 | |
14183 | tree |
14184 | tsubst_pack_index (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
14185 | { |
14186 | tree pack = PACK_INDEX_PACK (t); |
14187 | if (PACK_EXPANSION_P (pack)) |
14188 | pack = tsubst_pack_expansion (t: pack, args, complain, in_decl); |
14189 | else |
14190 | { |
14191 | /* PACK can be {*args#0} whose args#0's value-expr refers to |
14192 | a partially instantiated closure. Let tsubst find the |
14193 | fully-instantiated one. */ |
14194 | gcc_assert (TREE_CODE (pack) == TREE_VEC); |
14195 | pack = tsubst (pack, args, complain, in_decl); |
14196 | } |
14197 | if (TREE_CODE (pack) == TREE_VEC && TREE_VEC_LENGTH (pack) == 0) |
14198 | { |
14199 | if (complain & tf_error) |
14200 | error ("cannot index an empty pack"); |
14201 | return error_mark_node; |
14202 | } |
14203 | tree index = tsubst_expr (PACK_INDEX_INDEX (t), args, complain, in_decl); |
14204 | const bool parenthesized_p = (TREE_CODE (t) == PACK_INDEX_EXPR |
14205 | && PACK_INDEX_PARENTHESIZED_P (t)); |
14206 | if (!value_dependent_expression_p (index) && TREE_CODE (pack) == TREE_VEC) |
14207 | return pack_index_element (index, pack, parenthesized_p, complain); |
14208 | else |
14209 | return make_pack_index (pack, index); |
14210 | } |
14211 | |
14212 | /* Make an argument pack out of the TREE_VEC VEC. */ |
14213 | |
14214 | static tree |
14215 | make_argument_pack (tree vec) |
14216 | { |
14217 | tree pack; |
14218 | |
14219 | if (TYPE_P (TREE_VEC_ELT (vec, 0))) |
14220 | pack = cxx_make_type (TYPE_ARGUMENT_PACK); |
14221 | else |
14222 | { |
14223 | pack = make_node (NONTYPE_ARGUMENT_PACK); |
14224 | TREE_CONSTANT (pack) = 1; |
14225 | } |
14226 | ARGUMENT_PACK_ARGS (pack) = vec; |
14227 | return pack; |
14228 | } |
14229 | |
14230 | /* Return an exact copy of template args T that can be modified |
14231 | independently. */ |
14232 | |
14233 | static tree |
14234 | copy_template_args (tree t) |
14235 | { |
14236 | if (t == error_mark_node) |
14237 | return t; |
14238 | |
14239 | int len = TREE_VEC_LENGTH (t); |
14240 | tree new_vec = make_tree_vec (len); |
14241 | |
14242 | for (int i = 0; i < len; ++i) |
14243 | { |
14244 | tree elt = TREE_VEC_ELT (t, i); |
14245 | if (elt && TREE_CODE (elt) == TREE_VEC) |
14246 | elt = copy_template_args (t: elt); |
14247 | TREE_VEC_ELT (new_vec, i) = elt; |
14248 | } |
14249 | |
14250 | NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec) |
14251 | = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t); |
14252 | |
14253 | return new_vec; |
14254 | } |
14255 | |
14256 | /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */ |
14257 | |
14258 | tree |
14259 | tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain, |
14260 | tree in_decl) |
14261 | { |
14262 | /* This flag is used only during deduction, and we don't expect to |
14263 | substitute such ARGUMENT_PACKs. */ |
14264 | gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (orig_arg)); |
14265 | |
14266 | /* Substitute into each of the arguments. */ |
14267 | tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg), |
14268 | args, complain, in_decl); |
14269 | if (pack_args == error_mark_node) |
14270 | return error_mark_node; |
14271 | |
14272 | if (pack_args == ARGUMENT_PACK_ARGS (orig_arg)) |
14273 | return orig_arg; |
14274 | |
14275 | /* If we're substituting into a generic ARGUMENT_PACK for a variadic |
14276 | template parameter, we might be able to avoid allocating a new |
14277 | ARGUMENT_PACK and reuse the corresponding ARGUMENT_PACK from ARGS |
14278 | if the substituted result is identical to it. */ |
14279 | if (tree parm = template_arg_to_parm (t: orig_arg)) |
14280 | { |
14281 | int level, index; |
14282 | template_parm_level_and_index (parm, &level, &index); |
14283 | if (TMPL_ARGS_DEPTH (args) >= level) |
14284 | if (tree arg = TMPL_ARG (args, level, index)) |
14285 | if (TREE_CODE (arg) == TREE_CODE (orig_arg) |
14286 | && ARGUMENT_PACK_ARGS (arg) == pack_args) |
14287 | { |
14288 | gcc_assert (!ARGUMENT_PACK_INCOMPLETE_P (arg)); |
14289 | return arg; |
14290 | } |
14291 | } |
14292 | |
14293 | tree new_arg; |
14294 | if (TYPE_P (orig_arg)) |
14295 | { |
14296 | new_arg = cxx_make_type (TREE_CODE (orig_arg)); |
14297 | SET_TYPE_STRUCTURAL_EQUALITY (new_arg); |
14298 | } |
14299 | else |
14300 | { |
14301 | new_arg = make_node (TREE_CODE (orig_arg)); |
14302 | TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg); |
14303 | } |
14304 | ARGUMENT_PACK_ARGS (new_arg) = pack_args; |
14305 | return new_arg; |
14306 | } |
14307 | |
14308 | /* Substitute ARGS into the vector or list of template arguments T. */ |
14309 | |
14310 | tree |
14311 | tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
14312 | { |
14313 | if (t == error_mark_node) |
14314 | return error_mark_node; |
14315 | |
14316 | /* In "sizeof(X<I>)" we need to evaluate "I". */ |
14317 | cp_evaluated ev; |
14318 | |
14319 | const int len = TREE_VEC_LENGTH (t); |
14320 | tree *elts = XALLOCAVEC (tree, len); |
14321 | int expanded_len_adjust = 0; |
14322 | |
14323 | /* True iff the substituted result is identical to T. */ |
14324 | bool const_subst_p = true; |
14325 | |
14326 | for (int i = 0; i < len; i++) |
14327 | { |
14328 | tree orig_arg = TREE_VEC_ELT (t, i); |
14329 | tree new_arg; |
14330 | |
14331 | if (!orig_arg) |
14332 | new_arg = NULL_TREE; |
14333 | else if (TREE_CODE (orig_arg) == TREE_VEC) |
14334 | new_arg = tsubst_template_args (t: orig_arg, args, complain, in_decl); |
14335 | else if (PACK_EXPANSION_P (orig_arg)) |
14336 | { |
14337 | /* Substitute into an expansion expression. */ |
14338 | new_arg = tsubst_pack_expansion (t: orig_arg, args, complain, in_decl); |
14339 | |
14340 | if (TREE_CODE (new_arg) == TREE_VEC) |
14341 | /* Add to the expanded length adjustment the number of |
14342 | expanded arguments. We subtract one from this |
14343 | measurement, because the argument pack expression |
14344 | itself is already counted as 1 in |
14345 | LEN. EXPANDED_LEN_ADJUST can actually be negative, if |
14346 | the argument pack is empty. */ |
14347 | expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1; |
14348 | } |
14349 | else if (ARGUMENT_PACK_P (orig_arg)) |
14350 | new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl); |
14351 | else |
14352 | new_arg = tsubst_template_arg (t: orig_arg, args, complain, in_decl); |
14353 | |
14354 | if (new_arg == error_mark_node) |
14355 | return error_mark_node; |
14356 | |
14357 | elts[i] = new_arg; |
14358 | if (new_arg != orig_arg) |
14359 | const_subst_p = false; |
14360 | } |
14361 | |
14362 | if (const_subst_p) |
14363 | return t; |
14364 | |
14365 | tree maybe_reuse = NULL_TREE; |
14366 | |
14367 | /* If ARGS and T are both multi-level, the substituted result may be |
14368 | identical to ARGS. */ |
14369 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (t) |
14370 | && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args) |
14371 | && TMPL_ARGS_DEPTH (t) == TMPL_ARGS_DEPTH (args)) |
14372 | maybe_reuse = args; |
14373 | /* If T appears to be a vector of generic template arguments, the |
14374 | substituted result may be identical to the corresponding level |
14375 | from ARGS. */ |
14376 | else if (tree parm = template_arg_to_parm (TREE_VEC_ELT (t, 0))) |
14377 | { |
14378 | int level, index; |
14379 | template_parm_level_and_index (parm, &level, &index); |
14380 | if (index == 0 && TMPL_ARGS_DEPTH (args) >= level) |
14381 | maybe_reuse = TMPL_ARGS_LEVEL (args, level); |
14382 | } |
14383 | |
14384 | /* If the substituted result is identical to MAYBE_REUSE, return |
14385 | it and avoid allocating a new TREE_VEC, as an optimization. */ |
14386 | if (maybe_reuse != NULL_TREE |
14387 | && TREE_VEC_LENGTH (maybe_reuse) == len |
14388 | && std::equal (first1: elts, last1: elts+len, TREE_VEC_BEGIN (maybe_reuse))) |
14389 | return maybe_reuse; |
14390 | |
14391 | /* If T consists of only a pack expansion for which substitution yielded |
14392 | a TREE_VEC of the expanded elements, then reuse that TREE_VEC instead |
14393 | of effectively making a copy. */ |
14394 | if (len == 1 |
14395 | && PACK_EXPANSION_P (TREE_VEC_ELT (t, 0)) |
14396 | && TREE_CODE (elts[0]) == TREE_VEC) |
14397 | return elts[0]; |
14398 | |
14399 | /* Make space for the expanded arguments coming from template |
14400 | argument packs. */ |
14401 | tree r = make_tree_vec (len + expanded_len_adjust); |
14402 | /* T can contain TREE_VECs. That happens if T contains the |
14403 | arguments for a member template. |
14404 | In that case each TREE_VEC in T represents a level of template |
14405 | arguments, and T won't carry any non defaulted argument count. |
14406 | It will rather be the nested TREE_VECs that will carry one. |
14407 | In other words, T carries a non defaulted argument count only |
14408 | if it doesn't contain any nested TREE_VEC. */ |
14409 | if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (t)) |
14410 | { |
14411 | int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t); |
14412 | count += expanded_len_adjust; |
14413 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (r, count); |
14414 | } |
14415 | |
14416 | int out = 0; |
14417 | for (int i = 0; i < len; i++) |
14418 | { |
14419 | tree orig_arg = TREE_VEC_ELT (t, i); |
14420 | if (orig_arg |
14421 | && PACK_EXPANSION_P (orig_arg) |
14422 | && TREE_CODE (elts[i]) == TREE_VEC) |
14423 | { |
14424 | /* Now expand the template argument pack "in place". */ |
14425 | for (int idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++) |
14426 | TREE_VEC_ELT (r, out) = TREE_VEC_ELT (elts[i], idx); |
14427 | } |
14428 | else |
14429 | { |
14430 | TREE_VEC_ELT (r, out) = elts[i]; |
14431 | out++; |
14432 | } |
14433 | } |
14434 | gcc_assert (out == TREE_VEC_LENGTH (r)); |
14435 | |
14436 | return r; |
14437 | } |
14438 | |
14439 | /* Substitute ARGS into one level PARMS of template parameters. */ |
14440 | |
14441 | static tree |
14442 | tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain) |
14443 | { |
14444 | if (parms == error_mark_node) |
14445 | return error_mark_node; |
14446 | |
14447 | tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms)); |
14448 | |
14449 | for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i) |
14450 | { |
14451 | tree tuple = TREE_VEC_ELT (parms, i); |
14452 | |
14453 | if (tuple == error_mark_node) |
14454 | continue; |
14455 | |
14456 | TREE_VEC_ELT (new_vec, i) = |
14457 | tsubst_template_parm (tuple, args, complain); |
14458 | } |
14459 | |
14460 | return new_vec; |
14461 | } |
14462 | |
14463 | /* Return the result of substituting ARGS into the template parameters |
14464 | given by PARMS. If there are m levels of ARGS and m + n levels of |
14465 | PARMS, then the result will contain n levels of PARMS. For |
14466 | example, if PARMS is `template <class T> template <class U> |
14467 | template <T*, U, class V>' and ARGS is {{int}, {double}} then the |
14468 | result will be `template <int*, double, class V>'. */ |
14469 | |
14470 | static tree |
14471 | tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain) |
14472 | { |
14473 | tree r = NULL_TREE; |
14474 | tree* new_parms; |
14475 | |
14476 | /* When substituting into a template, we must set |
14477 | PROCESSING_TEMPLATE_DECL as the template parameters may be |
14478 | dependent if they are based on one-another, and the dependency |
14479 | predicates are short-circuit outside of templates. */ |
14480 | ++processing_template_decl; |
14481 | |
14482 | for (new_parms = &r; |
14483 | parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args); |
14484 | new_parms = &(TREE_CHAIN (*new_parms)), |
14485 | parms = TREE_CHAIN (parms)) |
14486 | { |
14487 | tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms), |
14488 | args, complain); |
14489 | *new_parms = |
14490 | tree_cons (size_int (TMPL_PARMS_DEPTH (parms) |
14491 | - TMPL_ARGS_DEPTH (args)), |
14492 | new_vec, NULL_TREE); |
14493 | TEMPLATE_PARMS_CONSTRAINTS (*new_parms) |
14494 | = TEMPLATE_PARMS_CONSTRAINTS (parms); |
14495 | } |
14496 | |
14497 | --processing_template_decl; |
14498 | |
14499 | return r; |
14500 | } |
14501 | |
14502 | /* Return the result of substituting ARGS into one template parameter |
14503 | given by T. T Must be a TREE_LIST which TREE_VALUE is the template |
14504 | parameter and which TREE_PURPOSE is the default argument of the |
14505 | template parameter. */ |
14506 | |
14507 | static tree |
14508 | tsubst_template_parm (tree t, tree args, tsubst_flags_t complain) |
14509 | { |
14510 | tree default_value, parm_decl; |
14511 | |
14512 | if (args == NULL_TREE |
14513 | || t == NULL_TREE |
14514 | || t == error_mark_node) |
14515 | return t; |
14516 | |
14517 | gcc_assert (TREE_CODE (t) == TREE_LIST); |
14518 | |
14519 | default_value = TREE_PURPOSE (t); |
14520 | parm_decl = TREE_VALUE (t); |
14521 | |
14522 | parm_decl = tsubst (parm_decl, args, complain, NULL_TREE); |
14523 | if (TREE_CODE (parm_decl) == PARM_DECL |
14524 | && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain)) |
14525 | parm_decl = error_mark_node; |
14526 | default_value = tsubst_template_arg (t: default_value, args, |
14527 | complain, NULL_TREE); |
14528 | |
14529 | tree r = build_tree_list (default_value, parm_decl); |
14530 | TEMPLATE_PARM_CONSTRAINTS (r) = TEMPLATE_PARM_CONSTRAINTS (t); |
14531 | return r; |
14532 | } |
14533 | |
14534 | /* Substitute in-place the TEMPLATE_PARM_CONSTRAINTS of each template |
14535 | parameter in PARMS for sake of declaration matching. */ |
14536 | |
14537 | static void |
14538 | tsubst_each_template_parm_constraints (tree parms, tree args, |
14539 | tsubst_flags_t complain) |
14540 | { |
14541 | ++processing_template_decl; |
14542 | for (; parms; parms = TREE_CHAIN (parms)) |
14543 | { |
14544 | tree level = TREE_VALUE (parms); |
14545 | for (tree parm : tree_vec_range (level)) |
14546 | TEMPLATE_PARM_CONSTRAINTS (parm) |
14547 | = tsubst_constraint (TEMPLATE_PARM_CONSTRAINTS (parm), args, |
14548 | complain, NULL_TREE); |
14549 | } |
14550 | --processing_template_decl; |
14551 | } |
14552 | |
14553 | /* Map from a FUNCTION_DECL to a vec of default argument instantiations, |
14554 | indexed in reverse order of the parameters. */ |
14555 | |
14556 | static GTY((cache)) hash_table<tree_vec_map_cache_hasher> *defarg_inst; |
14557 | |
14558 | /* Return a reference to the vec* of defarg insts for FN. */ |
14559 | |
14560 | static vec<tree,va_gc> *& |
14561 | defarg_insts_for (tree fn) |
14562 | { |
14563 | if (!defarg_inst) |
14564 | defarg_inst = hash_table<tree_vec_map_cache_hasher>::create_ggc (n: 13); |
14565 | tree_vec_map in = { .base: { .from: fn }, .to: nullptr }; |
14566 | tree_vec_map **slot |
14567 | = defarg_inst->find_slot_with_hash (comparable: &in, DECL_UID (fn), insert: INSERT); |
14568 | if (!*slot) |
14569 | { |
14570 | *slot = ggc_alloc<tree_vec_map> (); |
14571 | **slot = in; |
14572 | } |
14573 | return (*slot)->to; |
14574 | } |
14575 | |
14576 | /* Substitute into the default argument ARG (a default argument for |
14577 | FN), which has the indicated TYPE. */ |
14578 | |
14579 | tree |
14580 | tsubst_default_argument (tree fn, int parmnum, tree type, tree arg, |
14581 | tsubst_flags_t complain) |
14582 | { |
14583 | int errs = errorcount + sorrycount; |
14584 | |
14585 | /* This can happen in invalid code. */ |
14586 | if (TREE_CODE (arg) == DEFERRED_PARSE) |
14587 | return arg; |
14588 | |
14589 | /* Shortcut {}. */ |
14590 | if (BRACE_ENCLOSED_INITIALIZER_P (arg) |
14591 | && CONSTRUCTOR_NELTS (arg) == 0) |
14592 | return arg; |
14593 | |
14594 | tree parm = FUNCTION_FIRST_USER_PARM (fn); |
14595 | parm = chain_index (parmnum, parm); |
14596 | tree parmtype = TREE_TYPE (parm); |
14597 | if (DECL_BY_REFERENCE (parm)) |
14598 | parmtype = TREE_TYPE (parmtype); |
14599 | if (parmtype == error_mark_node) |
14600 | return error_mark_node; |
14601 | |
14602 | gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype)); |
14603 | |
14604 | /* Remember the location of the pointer to the vec rather than the location |
14605 | of the particular element, in case the vec grows in tsubst_expr. */ |
14606 | vec<tree,va_gc> *&defs = defarg_insts_for (fn); |
14607 | /* Index in reverse order to avoid allocating space for initial parameters |
14608 | that don't have default arguments. */ |
14609 | unsigned ridx = list_length (parm); |
14610 | if (vec_safe_length (v: defs) < ridx) |
14611 | vec_safe_grow_cleared (v&: defs, len: ridx); |
14612 | else if (tree inst = (*defs)[ridx - 1]) |
14613 | return inst; |
14614 | |
14615 | /* This default argument came from a template. Instantiate the |
14616 | default argument here, not in tsubst. In the case of |
14617 | something like: |
14618 | |
14619 | template <class T> |
14620 | struct S { |
14621 | static T t(); |
14622 | void f(T = t()); |
14623 | }; |
14624 | |
14625 | we must be careful to do name lookup in the scope of S<T>, |
14626 | rather than in the current class. */ |
14627 | push_to_top_level (); |
14628 | push_access_scope (t: fn); |
14629 | push_deferring_access_checks (dk_no_deferred); |
14630 | /* So in_immediate_context knows this is a default argument. */ |
14631 | begin_scope (sk_function_parms, fn); |
14632 | start_lambda_scope (decl: parm); |
14633 | |
14634 | /* The default argument expression may cause implicitly defined |
14635 | member functions to be synthesized, which will result in garbage |
14636 | collection. We must treat this situation as if we were within |
14637 | the body of function so as to avoid collecting live data on the |
14638 | stack. */ |
14639 | ++function_depth; |
14640 | arg = tsubst_expr (arg, DECL_TI_ARGS (fn), complain, NULL_TREE); |
14641 | --function_depth; |
14642 | |
14643 | finish_lambda_scope (); |
14644 | |
14645 | /* Make sure the default argument is reasonable. */ |
14646 | arg = check_default_argument (type, arg, complain); |
14647 | |
14648 | if (errorcount+sorrycount > errs |
14649 | && (complain & tf_warning_or_error)) |
14650 | inform (input_location, |
14651 | " when instantiating default argument for call to %qD", fn); |
14652 | |
14653 | leave_scope (); |
14654 | pop_deferring_access_checks (); |
14655 | pop_access_scope (t: fn); |
14656 | pop_from_top_level (); |
14657 | |
14658 | if (arg != error_mark_node && !cp_unevaluated_operand) |
14659 | (*defs)[ridx - 1] = arg; |
14660 | |
14661 | return arg; |
14662 | } |
14663 | |
14664 | /* Substitute into all the default arguments for FN. */ |
14665 | |
14666 | static void |
14667 | tsubst_default_arguments (tree fn, tsubst_flags_t complain) |
14668 | { |
14669 | tree arg; |
14670 | tree tmpl_args; |
14671 | |
14672 | tmpl_args = DECL_TI_ARGS (fn); |
14673 | |
14674 | /* If this function is not yet instantiated, we certainly don't need |
14675 | its default arguments. */ |
14676 | if (uses_template_parms (t: tmpl_args)) |
14677 | return; |
14678 | /* Don't do this again for clones. */ |
14679 | if (DECL_CLONED_FUNCTION_P (fn)) |
14680 | return; |
14681 | |
14682 | int i = 0; |
14683 | for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
14684 | arg; |
14685 | arg = TREE_CHAIN (arg), ++i) |
14686 | if (TREE_PURPOSE (arg)) |
14687 | TREE_PURPOSE (arg) = tsubst_default_argument (fn, parmnum: i, |
14688 | TREE_VALUE (arg), |
14689 | TREE_PURPOSE (arg), |
14690 | complain); |
14691 | } |
14692 | |
14693 | /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */ |
14694 | static GTY((cache)) decl_tree_cache_map *explicit_specifier_map; |
14695 | |
14696 | /* Store a pair to EXPLICIT_SPECIFIER_MAP. */ |
14697 | |
14698 | void |
14699 | store_explicit_specifier (tree v, tree t) |
14700 | { |
14701 | if (!explicit_specifier_map) |
14702 | explicit_specifier_map = decl_tree_cache_map::create_ggc (size: 37); |
14703 | DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true; |
14704 | explicit_specifier_map->put (k: v, v: t); |
14705 | } |
14706 | |
14707 | /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */ |
14708 | |
14709 | tree |
14710 | lookup_explicit_specifier (tree v) |
14711 | { |
14712 | return *explicit_specifier_map->get (k: v); |
14713 | } |
14714 | |
14715 | /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding |
14716 | FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types |
14717 | are ARG_TYPES, and exception specification is RAISES, and otherwise is |
14718 | identical to T. */ |
14719 | |
14720 | static tree |
14721 | rebuild_function_or_method_type (tree t, tree args, tree return_type, |
14722 | tree arg_types, tree raises, |
14723 | tsubst_flags_t complain) |
14724 | { |
14725 | gcc_assert (FUNC_OR_METHOD_TYPE_P (t)); |
14726 | |
14727 | tree new_type; |
14728 | if (TREE_CODE (t) == FUNCTION_TYPE) |
14729 | { |
14730 | new_type = build_function_type (return_type, arg_types); |
14731 | new_type = apply_memfn_quals (new_type, type_memfn_quals (t)); |
14732 | } |
14733 | else |
14734 | { |
14735 | tree r = TREE_TYPE (TREE_VALUE (arg_types)); |
14736 | /* Don't pick up extra function qualifiers from the basetype. */ |
14737 | r = cp_build_qualified_type (r, type_memfn_quals (t), complain); |
14738 | if (! MAYBE_CLASS_TYPE_P (r)) |
14739 | { |
14740 | /* [temp.deduct] |
14741 | |
14742 | Type deduction may fail for any of the following |
14743 | reasons: |
14744 | |
14745 | -- Attempting to create "pointer to member of T" when T |
14746 | is not a class type. */ |
14747 | if (complain & tf_error) |
14748 | error ("creating pointer to member function of non-class type %qT", |
14749 | r); |
14750 | return error_mark_node; |
14751 | } |
14752 | |
14753 | new_type = build_method_type_directly (r, return_type, |
14754 | TREE_CHAIN (arg_types)); |
14755 | } |
14756 | if (!apply_late_template_attributes (decl_p: &new_type, TYPE_ATTRIBUTES (t), attr_flags: 0, |
14757 | args, complain, NULL_TREE)) |
14758 | return error_mark_node; |
14759 | |
14760 | cp_ref_qualifier rqual = type_memfn_rqual (t); |
14761 | bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t); |
14762 | return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p); |
14763 | } |
14764 | |
14765 | /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of |
14766 | each of its formal parameters. If there is a disagreement then rebuild |
14767 | DECL's function type according to its formal parameter types, as part of a |
14768 | resolution for Core issues 1001/1322. */ |
14769 | |
14770 | static void |
14771 | maybe_rebuild_function_decl_type (tree decl, tree args) |
14772 | { |
14773 | bool function_type_needs_rebuilding = false; |
14774 | if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl)) |
14775 | { |
14776 | tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl); |
14777 | while (parm_type_list && parm_type_list != void_list_node) |
14778 | { |
14779 | tree parm_type = TREE_VALUE (parm_type_list); |
14780 | tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list)); |
14781 | if (!same_type_p (parm_type, formal_parm_type_unqual)) |
14782 | { |
14783 | function_type_needs_rebuilding = true; |
14784 | break; |
14785 | } |
14786 | |
14787 | parm_list = DECL_CHAIN (parm_list); |
14788 | parm_type_list = TREE_CHAIN (parm_type_list); |
14789 | } |
14790 | } |
14791 | |
14792 | if (!function_type_needs_rebuilding) |
14793 | return; |
14794 | |
14795 | const tree fntype = TREE_TYPE (decl); |
14796 | tree parm_list = DECL_ARGUMENTS (decl); |
14797 | tree old_parm_type_list = TYPE_ARG_TYPES (fntype); |
14798 | tree new_parm_type_list = NULL_TREE; |
14799 | tree *q = &new_parm_type_list; |
14800 | for (int skip = num_artificial_parms_for (decl); skip > 0; skip--) |
14801 | { |
14802 | *q = copy_node (old_parm_type_list); |
14803 | parm_list = DECL_CHAIN (parm_list); |
14804 | old_parm_type_list = TREE_CHAIN (old_parm_type_list); |
14805 | q = &TREE_CHAIN (*q); |
14806 | } |
14807 | while (old_parm_type_list && old_parm_type_list != void_list_node) |
14808 | { |
14809 | *q = copy_node (old_parm_type_list); |
14810 | tree *new_parm_type = &TREE_VALUE (*q); |
14811 | tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list)); |
14812 | if (!same_type_p (*new_parm_type, formal_parm_type_unqual)) |
14813 | *new_parm_type = formal_parm_type_unqual; |
14814 | |
14815 | parm_list = DECL_CHAIN (parm_list); |
14816 | old_parm_type_list = TREE_CHAIN (old_parm_type_list); |
14817 | q = &TREE_CHAIN (*q); |
14818 | } |
14819 | if (old_parm_type_list == void_list_node) |
14820 | *q = void_list_node; |
14821 | |
14822 | TREE_TYPE (decl) |
14823 | = rebuild_function_or_method_type (t: fntype, args, |
14824 | TREE_TYPE (fntype), arg_types: new_parm_type_list, |
14825 | TYPE_RAISES_EXCEPTIONS (fntype), complain: tf_none); |
14826 | } |
14827 | |
14828 | /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */ |
14829 | |
14830 | static tree |
14831 | tsubst_function_decl (tree t, tree args, tsubst_flags_t complain, |
14832 | tree lambda_fntype, bool use_spec_table = true) |
14833 | { |
14834 | tree gen_tmpl = NULL_TREE, argvec = NULL_TREE; |
14835 | hashval_t hash = 0; |
14836 | tree in_decl = t; |
14837 | |
14838 | /* Nobody should be tsubst'ing into non-template functions. */ |
14839 | gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE |
14840 | || DECL_LOCAL_DECL_P (t)); |
14841 | |
14842 | if (DECL_LOCAL_DECL_P (t)) |
14843 | { |
14844 | if (tree spec = retrieve_local_specialization (tmpl: t)) |
14845 | return spec; |
14846 | } |
14847 | else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL) |
14848 | { |
14849 | /* If T is not dependent, just return it. */ |
14850 | if (!uses_template_parms (DECL_TI_ARGS (t)) |
14851 | && !LAMBDA_FUNCTION_P (t)) |
14852 | return t; |
14853 | |
14854 | /* A non-templated friend doesn't get DECL_TEMPLATE_INFO. */ |
14855 | if (non_templated_friend_p (t)) |
14856 | goto friend_case; |
14857 | |
14858 | /* Calculate the most general template of which R is a |
14859 | specialization. */ |
14860 | gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t)); |
14861 | |
14862 | /* We're substituting a lambda function under tsubst_lambda_expr but not |
14863 | directly from it; find the matching function we're already inside. |
14864 | But don't do this if T is a generic lambda with a single level of |
14865 | template parms, as in that case we're doing a normal instantiation. */ |
14866 | if (LAMBDA_FUNCTION_P (t) && !lambda_fntype |
14867 | && (!generic_lambda_fn_p (t) |
14868 | || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1)) |
14869 | return enclosing_instantiation_of (tctx: t); |
14870 | |
14871 | /* Calculate the complete set of arguments used to |
14872 | specialize R. */ |
14873 | if (use_spec_table && !lambda_fntype) |
14874 | { |
14875 | argvec = tsubst_template_args (DECL_TI_ARGS |
14876 | (DECL_TEMPLATE_RESULT |
14877 | (DECL_TI_TEMPLATE (t))), |
14878 | args, complain, in_decl); |
14879 | if (argvec == error_mark_node) |
14880 | return error_mark_node; |
14881 | |
14882 | /* Check to see if we already have this specialization. */ |
14883 | hash = spec_hasher::hash (tmpl: gen_tmpl, args: argvec); |
14884 | if (tree spec = retrieve_specialization (tmpl: gen_tmpl, args: argvec, hash)) |
14885 | /* The spec for these args might be a partial instantiation of the |
14886 | template, but here what we want is the FUNCTION_DECL. */ |
14887 | return STRIP_TEMPLATE (spec); |
14888 | } |
14889 | else |
14890 | argvec = args; |
14891 | } |
14892 | else |
14893 | { |
14894 | /* This special case arises when we have something like this: |
14895 | |
14896 | template <class T> struct S { |
14897 | friend void f<int>(int, double); |
14898 | }; |
14899 | |
14900 | Here, the DECL_TI_TEMPLATE for the friend declaration |
14901 | will be an IDENTIFIER_NODE. We are being called from |
14902 | tsubst_friend_function, and we want only to create a |
14903 | new decl (R) with appropriate types so that we can call |
14904 | determine_specialization. */ |
14905 | friend_case: |
14906 | gen_tmpl = NULL_TREE; |
14907 | argvec = NULL_TREE; |
14908 | } |
14909 | |
14910 | tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype) |
14911 | : NULL_TREE); |
14912 | tree ctx = closure ? closure : DECL_CONTEXT (t); |
14913 | bool member = ctx && TYPE_P (ctx); |
14914 | |
14915 | /* If this is a static or xobj lambda, remove the 'this' pointer added in |
14916 | tsubst_lambda_expr now that we know the closure type. */ |
14917 | if (lambda_fntype && !DECL_IOBJ_MEMBER_FUNCTION_P (t)) |
14918 | lambda_fntype = static_fn_type (lambda_fntype); |
14919 | |
14920 | if (member && !closure) |
14921 | ctx = tsubst_entering_scope (t: ctx, args, complain, in_decl: t); |
14922 | |
14923 | tree type = (lambda_fntype ? lambda_fntype |
14924 | : tsubst (TREE_TYPE (t), args, |
14925 | complain | tf_fndecl_type, in_decl)); |
14926 | if (type == error_mark_node) |
14927 | return error_mark_node; |
14928 | |
14929 | /* If we hit excessive deduction depth, the type is bogus even if |
14930 | it isn't error_mark_node, so don't build a decl. */ |
14931 | if (excessive_deduction_depth) |
14932 | return error_mark_node; |
14933 | |
14934 | /* We do NOT check for matching decls pushed separately at this |
14935 | point, as they may not represent instantiations of this |
14936 | template, and in any case are considered separate under the |
14937 | discrete model. */ |
14938 | tree r = copy_decl (t); |
14939 | DECL_USE_TEMPLATE (r) = 0; |
14940 | TREE_TYPE (r) = type; |
14941 | /* Clear out the mangled name and RTL for the instantiation. */ |
14942 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); |
14943 | SET_DECL_RTL (r, NULL); |
14944 | /* Leave DECL_INITIAL set on deleted instantiations. */ |
14945 | if (!DECL_DELETED_FN (r)) |
14946 | DECL_INITIAL (r) = NULL_TREE; |
14947 | DECL_CONTEXT (r) = ctx; |
14948 | set_instantiating_module (r); |
14949 | |
14950 | /* Handle explicit(dependent-expr). */ |
14951 | if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t)) |
14952 | { |
14953 | tree spec = lookup_explicit_specifier (v: t); |
14954 | spec = tsubst_expr (spec, args, complain, in_decl); |
14955 | spec = build_explicit_specifier (spec, complain); |
14956 | if (spec == error_mark_node) |
14957 | return error_mark_node; |
14958 | if (instantiation_dependent_expression_p (spec)) |
14959 | store_explicit_specifier (v: r, t: spec); |
14960 | else |
14961 | { |
14962 | DECL_NONCONVERTING_P (r) = (spec == boolean_true_node); |
14963 | DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (r) = false; |
14964 | } |
14965 | } |
14966 | |
14967 | /* OpenMP UDRs have the only argument a reference to the declared |
14968 | type. We want to diagnose if the declared type is a reference, |
14969 | which is invalid, but as references to references are usually |
14970 | quietly merged, diagnose it here. */ |
14971 | if (DECL_OMP_DECLARE_REDUCTION_P (t)) |
14972 | { |
14973 | tree argtype |
14974 | = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t)))); |
14975 | argtype = tsubst (argtype, args, complain, in_decl); |
14976 | if (TYPE_REF_P (argtype)) |
14977 | error_at (DECL_SOURCE_LOCATION (t), |
14978 | "reference type %qT in " |
14979 | "%<#pragma omp declare reduction%>", argtype); |
14980 | if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), c: '~') == NULL) |
14981 | DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t), |
14982 | argtype); |
14983 | } |
14984 | |
14985 | if (member && DECL_CONV_FN_P (r)) |
14986 | /* Type-conversion operator. Reconstruct the name, in |
14987 | case it's the name of one of the template's parameters. */ |
14988 | DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type)); |
14989 | |
14990 | tree parms = DECL_ARGUMENTS (t); |
14991 | if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t)) |
14992 | parms = DECL_CHAIN (parms); |
14993 | parms = tsubst (parms, args, complain, t); |
14994 | if (parms == error_mark_node) |
14995 | return error_mark_node; |
14996 | for (tree parm = parms; parm; parm = DECL_CHAIN (parm)) |
14997 | DECL_CONTEXT (parm) = r; |
14998 | if (closure && DECL_IOBJ_MEMBER_FUNCTION_P (t)) |
14999 | { |
15000 | tree tparm = build_this_parm (r, closure, type_memfn_quals (type)); |
15001 | DECL_NAME (tparm) = closure_identifier; |
15002 | DECL_CHAIN (tparm) = parms; |
15003 | parms = tparm; |
15004 | } |
15005 | DECL_ARGUMENTS (r) = parms; |
15006 | DECL_RESULT (r) = NULL_TREE; |
15007 | |
15008 | maybe_rebuild_function_decl_type (decl: r, args); |
15009 | |
15010 | TREE_STATIC (r) = 0; |
15011 | TREE_PUBLIC (r) = TREE_PUBLIC (t); |
15012 | DECL_EXTERNAL (r) = 1; |
15013 | /* If this is an instantiation of a function with internal |
15014 | linkage, we already know what object file linkage will be |
15015 | assigned to the instantiation. */ |
15016 | DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r); |
15017 | DECL_DEFER_OUTPUT (r) = 0; |
15018 | DECL_CHAIN (r) = NULL_TREE; |
15019 | DECL_PENDING_INLINE_INFO (r) = 0; |
15020 | DECL_PENDING_INLINE_P (r) = 0; |
15021 | DECL_SAVED_TREE (r) = NULL_TREE; |
15022 | DECL_STRUCT_FUNCTION (r) = NULL; |
15023 | TREE_USED (r) = 0; |
15024 | /* We'll re-clone as appropriate in instantiate_template. */ |
15025 | DECL_CLONED_FUNCTION (r) = NULL_TREE; |
15026 | |
15027 | /* If we aren't complaining now, return on error before we register |
15028 | the specialization so that we'll complain eventually. */ |
15029 | if ((complain & tf_error) == 0 |
15030 | && IDENTIFIER_ANY_OP_P (DECL_NAME (r)) |
15031 | && !grok_op_properties (r, /*complain=*/false)) |
15032 | return error_mark_node; |
15033 | |
15034 | /* If we are looking at an xobj lambda, we might need to check the type of |
15035 | its xobj parameter. */ |
15036 | if (LAMBDA_FUNCTION_P (r) && DECL_XOBJ_MEMBER_FUNCTION_P (r)) |
15037 | { |
15038 | tree closure_obj = DECL_CONTEXT (r); |
15039 | tree lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure_obj); |
15040 | tree obj_param = TREE_TYPE (DECL_ARGUMENTS (r)); |
15041 | |
15042 | if (!(LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE |
15043 | || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))) |
15044 | /* If a lambda has an empty capture clause, an xobj parameter of |
15045 | unrelated type is not an error. */; |
15046 | else if (dependent_type_p (obj_param)) |
15047 | /* If we are coming from tsubst_lambda_expr we might not have |
15048 | substituted into our xobj parameter yet. We can't error out until |
15049 | we know what the type really is so do nothing... |
15050 | ...but if we are instantiating the call op for real and we don't |
15051 | have a real type then something has gone incredibly wrong. */ |
15052 | gcc_assert (lambda_fntype); |
15053 | else |
15054 | { |
15055 | /* We have a lambda with captures, and know the type of the xobj |
15056 | parameter, time to check it. */ |
15057 | tree obj_param_type = TYPE_MAIN_VARIANT (non_reference (obj_param)); |
15058 | if (!same_or_base_type_p (closure_obj, obj_param_type)) |
15059 | { |
15060 | /* This error does not emit when the lambda's call operator |
15061 | template is instantiated by taking its address, such as in |
15062 | the following case: |
15063 | |
15064 | auto f = [x = 0](this auto&&){}; |
15065 | int (*fp)(int&) = &decltype(f)::operator(); |
15066 | |
15067 | It only emits when explicitly calling the call operator with |
15068 | an explicit template parameter: |
15069 | |
15070 | template<typename T> |
15071 | struct S : T { |
15072 | using T::operator(); |
15073 | operator int() const {return {};} |
15074 | }; |
15075 | |
15076 | auto s = S{[x = 0](this auto&&) {}}; |
15077 | s.operator()<int>(); |
15078 | |
15079 | This is due to resolve_address_of_overloaded_function being |
15080 | deficient at reporting candidates when overload resolution |
15081 | fails. |
15082 | |
15083 | This diagnostic will be active in the first case if/when |
15084 | resolve_address_of_overloaded_function is fixed to properly |
15085 | emit candidates upon failure to resolve to an overload. */ |
15086 | if (complain & tf_error) |
15087 | error ("a lambda with captures may not have an explicit " |
15088 | "object parameter of an unrelated type"); |
15089 | return error_mark_node; |
15090 | } |
15091 | } |
15092 | } |
15093 | |
15094 | /* Associate the constraints directly with the instantiation. We |
15095 | don't substitute through the constraints; that's only done when |
15096 | they are checked. */ |
15097 | if (tree ci = get_constraints (t)) |
15098 | set_constraints (r, ci); |
15099 | |
15100 | if (DECL_FRIEND_CONTEXT (t)) |
15101 | SET_DECL_FRIEND_CONTEXT (r, |
15102 | tsubst (DECL_FRIEND_CONTEXT (t), |
15103 | args, complain, in_decl)); |
15104 | |
15105 | if (!apply_late_template_attributes (decl_p: &r, DECL_ATTRIBUTES (r), attr_flags: 0, |
15106 | args, complain, in_decl)) |
15107 | return error_mark_node; |
15108 | |
15109 | /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do |
15110 | this in the special friend case mentioned above where |
15111 | GEN_TMPL is NULL. */ |
15112 | if (gen_tmpl && !closure) |
15113 | { |
15114 | DECL_TEMPLATE_INFO (r) |
15115 | = build_template_info (template_decl: gen_tmpl, template_args: argvec); |
15116 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
15117 | |
15118 | if (use_spec_table) |
15119 | { |
15120 | tree new_r |
15121 | = register_specialization (spec: r, tmpl: gen_tmpl, args: argvec, is_friend: false, hash); |
15122 | if (new_r != r) |
15123 | /* We instantiated this while substituting into |
15124 | the type earlier (template/friend54.C). */ |
15125 | return new_r; |
15126 | } |
15127 | |
15128 | /* We're not supposed to instantiate default arguments |
15129 | until they are called, for a template. But, for a |
15130 | declaration like: |
15131 | |
15132 | template <class T> void f () |
15133 | { extern void g(int i = T()); } |
15134 | |
15135 | we should do the substitution when the template is |
15136 | instantiated. We handle the member function case in |
15137 | instantiate_class_template since the default arguments |
15138 | might refer to other members of the class. */ |
15139 | if (!member |
15140 | && !PRIMARY_TEMPLATE_P (gen_tmpl) |
15141 | && !uses_template_parms (t: argvec)) |
15142 | tsubst_default_arguments (fn: r, complain); |
15143 | } |
15144 | else if (DECL_LOCAL_DECL_P (r)) |
15145 | { |
15146 | if (!cp_unevaluated_operand) |
15147 | register_local_specialization (spec: r, tmpl: t); |
15148 | } |
15149 | else |
15150 | DECL_TEMPLATE_INFO (r) = NULL_TREE; |
15151 | |
15152 | /* Copy the list of befriending classes. */ |
15153 | for (tree *friends = &DECL_BEFRIENDING_CLASSES (r); |
15154 | *friends; |
15155 | friends = &TREE_CHAIN (*friends)) |
15156 | { |
15157 | *friends = copy_node (*friends); |
15158 | TREE_VALUE (*friends) |
15159 | = tsubst (TREE_VALUE (*friends), args, complain, in_decl); |
15160 | } |
15161 | |
15162 | if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r)) |
15163 | { |
15164 | maybe_retrofit_in_chrg (r); |
15165 | if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r)) |
15166 | return error_mark_node; |
15167 | /* If this is an instantiation of a member template, clone it. |
15168 | If it isn't, that'll be handled by |
15169 | clone_constructors_and_destructors. */ |
15170 | if (gen_tmpl && PRIMARY_TEMPLATE_P (gen_tmpl)) |
15171 | clone_cdtor (r, /*update_methods=*/false); |
15172 | } |
15173 | else if ((complain & tf_error) != 0 |
15174 | && IDENTIFIER_ANY_OP_P (DECL_NAME (r)) |
15175 | && !grok_op_properties (r, /*complain=*/true)) |
15176 | return error_mark_node; |
15177 | |
15178 | /* Possibly limit visibility based on template args. */ |
15179 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; |
15180 | if (DECL_VISIBILITY_SPECIFIED (t)) |
15181 | { |
15182 | DECL_VISIBILITY_SPECIFIED (r) = 0; |
15183 | DECL_ATTRIBUTES (r) |
15184 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); |
15185 | } |
15186 | determine_visibility (r); |
15187 | if (DECL_SECTION_NAME (t)) |
15188 | set_decl_section_name (r, t); |
15189 | if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r) |
15190 | && COMPLETE_TYPE_P (DECL_CONTEXT (r)) |
15191 | && !processing_template_decl) |
15192 | defaulted_late_check (r); |
15193 | |
15194 | if (flag_openmp) |
15195 | if (tree attr = lookup_attribute (attr_name: "omp declare variant base", |
15196 | DECL_ATTRIBUTES (r))) |
15197 | omp_declare_variant_finalize (r, attr); |
15198 | |
15199 | return r; |
15200 | } |
15201 | |
15202 | /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */ |
15203 | |
15204 | static tree |
15205 | tsubst_template_decl (tree t, tree args, tsubst_flags_t complain, |
15206 | tree lambda_fntype, tree lambda_tparms) |
15207 | { |
15208 | /* We can get here when processing a member function template, |
15209 | member class template, or template template parameter. */ |
15210 | tree decl = DECL_TEMPLATE_RESULT (t); |
15211 | tree in_decl = t; |
15212 | tree spec; |
15213 | tree tmpl_args; |
15214 | tree full_args = NULL_TREE; |
15215 | tree r; |
15216 | hashval_t hash = 0; |
15217 | |
15218 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
15219 | { |
15220 | /* Template template parameter is treated here. */ |
15221 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
15222 | if (new_type == error_mark_node) |
15223 | r = error_mark_node; |
15224 | /* If we get a real template back, return it. This can happen in |
15225 | the context of most_specialized_partial_spec. */ |
15226 | else if (TREE_CODE (new_type) == TEMPLATE_DECL) |
15227 | r = new_type; |
15228 | else |
15229 | /* The new TEMPLATE_DECL was built in |
15230 | reduce_template_parm_level. */ |
15231 | r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type); |
15232 | return r; |
15233 | } |
15234 | |
15235 | if (!lambda_fntype) |
15236 | { |
15237 | /* We might already have an instance of this template. |
15238 | The ARGS are for the surrounding class type, so the |
15239 | full args contain the tsubst'd args for the context, |
15240 | plus the innermost args from the template decl. */ |
15241 | tmpl_args = DECL_CLASS_TEMPLATE_P (t) |
15242 | ? CLASSTYPE_TI_ARGS (TREE_TYPE (t)) |
15243 | : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t)); |
15244 | /* Because this is a template, the arguments will still be |
15245 | dependent, even after substitution. If |
15246 | PROCESSING_TEMPLATE_DECL is not set, the dependency |
15247 | predicates will short-circuit. */ |
15248 | ++processing_template_decl; |
15249 | full_args = tsubst_template_args (t: tmpl_args, args, |
15250 | complain, in_decl); |
15251 | --processing_template_decl; |
15252 | if (full_args == error_mark_node) |
15253 | return error_mark_node; |
15254 | |
15255 | /* If this is a default template template argument, |
15256 | tsubst might not have changed anything. */ |
15257 | if (full_args == tmpl_args) |
15258 | return t; |
15259 | |
15260 | hash = spec_hasher::hash (tmpl: t, args: full_args); |
15261 | spec = retrieve_specialization (tmpl: t, args: full_args, hash); |
15262 | if (spec != NULL_TREE) |
15263 | { |
15264 | if (TYPE_P (spec)) |
15265 | /* Type partial instantiations are stored as the type by |
15266 | lookup_template_class_1, not here as the template. */ |
15267 | spec = CLASSTYPE_TI_TEMPLATE (spec); |
15268 | else if (TREE_CODE (spec) != TEMPLATE_DECL) |
15269 | spec = DECL_TI_TEMPLATE (spec); |
15270 | return spec; |
15271 | } |
15272 | } |
15273 | |
15274 | /* Make a new template decl. It will be similar to the |
15275 | original, but will record the current template arguments. |
15276 | We also create a new function declaration, which is just |
15277 | like the old one, but points to this new template, rather |
15278 | than the old one. */ |
15279 | r = copy_decl (t); |
15280 | gcc_assert (DECL_LANG_SPECIFIC (r) != 0); |
15281 | DECL_CHAIN (r) = NULL_TREE; |
15282 | |
15283 | // Build new template info linking to the original template decl. |
15284 | if (!lambda_fntype) |
15285 | { |
15286 | DECL_TEMPLATE_INFO (r) = build_template_info (template_decl: t, template_args: args); |
15287 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
15288 | } |
15289 | else |
15290 | DECL_TEMPLATE_INFO (r) = NULL_TREE; |
15291 | |
15292 | /* The template parameters for this new template are all the |
15293 | template parameters for the old template, except the |
15294 | outermost level of parameters. */ |
15295 | auto tparm_guard = make_temp_override (current_template_parms); |
15296 | DECL_TEMPLATE_PARMS (r) |
15297 | = current_template_parms |
15298 | = (lambda_tparms |
15299 | ? lambda_tparms |
15300 | : tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args, |
15301 | complain)); |
15302 | |
15303 | bool class_p = false; |
15304 | tree inner = decl; |
15305 | ++processing_template_decl; |
15306 | if (TREE_CODE (inner) == FUNCTION_DECL) |
15307 | inner = tsubst_function_decl (t: inner, args, complain, lambda_fntype, |
15308 | /*use_spec_table=*/false); |
15309 | else |
15310 | { |
15311 | if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner)) |
15312 | { |
15313 | class_p = true; |
15314 | inner = TREE_TYPE (inner); |
15315 | } |
15316 | if (class_p) |
15317 | inner = tsubst_entering_scope (t: inner, args, complain, in_decl); |
15318 | else |
15319 | inner = tsubst_decl (inner, args, complain, /*use_spec_table=*/false); |
15320 | } |
15321 | --processing_template_decl; |
15322 | if (inner == error_mark_node) |
15323 | return error_mark_node; |
15324 | |
15325 | if (class_p) |
15326 | { |
15327 | /* For a partial specialization, we need to keep pointing to |
15328 | the primary template. */ |
15329 | if (!DECL_TEMPLATE_SPECIALIZATION (t)) |
15330 | { |
15331 | CLASSTYPE_TI_TEMPLATE (inner) = r; |
15332 | CLASSTYPE_USE_TEMPLATE (inner) = 0; |
15333 | } |
15334 | |
15335 | DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner); |
15336 | inner = TYPE_MAIN_DECL (inner); |
15337 | } |
15338 | else if (lambda_fntype) |
15339 | { |
15340 | tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r)); |
15341 | DECL_TEMPLATE_INFO (inner) = build_template_info (template_decl: r, template_args: args); |
15342 | } |
15343 | else |
15344 | { |
15345 | DECL_TI_TEMPLATE (inner) = r; |
15346 | /* Set DECL_TI_ARGS to the full set of template arguments, |
15347 | which tsubst_function_decl / tsubst_decl didn't do due to |
15348 | use_spec_table=false. */ |
15349 | DECL_TI_ARGS (inner) = full_args; |
15350 | DECL_TI_ARGS (r) = DECL_TI_ARGS (inner); |
15351 | } |
15352 | |
15353 | DECL_TEMPLATE_RESULT (r) = inner; |
15354 | TREE_TYPE (r) = TREE_TYPE (inner); |
15355 | DECL_CONTEXT (r) = DECL_CONTEXT (inner); |
15356 | |
15357 | if (modules_p ()) |
15358 | { |
15359 | /* Propagate module information from the decl. */ |
15360 | DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner); |
15361 | if (DECL_LANG_SPECIFIC (inner)) |
15362 | /* If this is a constrained template, the above tsubst of |
15363 | inner can find the unconstrained template, which may have |
15364 | come from an import. This is ok, because we don't |
15365 | register this instantiation (see below). */ |
15366 | gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner) |
15367 | || (TEMPLATE_PARMS_CONSTRAINTS |
15368 | (DECL_TEMPLATE_PARMS (t)))); |
15369 | } |
15370 | |
15371 | DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE; |
15372 | DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE; |
15373 | |
15374 | if (PRIMARY_TEMPLATE_P (t)) |
15375 | DECL_PRIMARY_TEMPLATE (r) = r; |
15376 | |
15377 | if (!lambda_fntype && !class_p) |
15378 | { |
15379 | /* Record this non-type partial instantiation. */ |
15380 | /* FIXME we'd like to always register the TEMPLATE_DECL, or always |
15381 | the DECL_TEMPLATE_RESULT, but it seems the modules code relies |
15382 | on this current behavior. */ |
15383 | if (TREE_CODE (inner) == FUNCTION_DECL) |
15384 | register_specialization (spec: r, tmpl: t, args: full_args, is_friend: false, hash); |
15385 | else |
15386 | register_specialization (spec: inner, tmpl: t, args: full_args, is_friend: false, hash); |
15387 | } |
15388 | |
15389 | return r; |
15390 | } |
15391 | |
15392 | /* True if FN is the op() for a lambda in an uninstantiated template. */ |
15393 | |
15394 | bool |
15395 | lambda_fn_in_template_p (tree fn) |
15396 | { |
15397 | if (!fn || !LAMBDA_FUNCTION_P (fn)) |
15398 | return false; |
15399 | tree closure = DECL_CONTEXT (fn); |
15400 | return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE; |
15401 | } |
15402 | |
15403 | /* True if FN is the substitution (via tsubst_lambda_expr) of a function for |
15404 | which the above is true. */ |
15405 | |
15406 | bool |
15407 | regenerated_lambda_fn_p (tree fn) |
15408 | { |
15409 | if (!fn || !LAMBDA_FUNCTION_P (fn)) |
15410 | return false; |
15411 | tree closure = DECL_CONTEXT (fn); |
15412 | tree lam = CLASSTYPE_LAMBDA_EXPR (closure); |
15413 | return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE; |
15414 | } |
15415 | |
15416 | /* Return the LAMBDA_EXPR from which T was ultimately regenerated. |
15417 | If T is not a regenerated LAMBDA_EXPR, return T. */ |
15418 | |
15419 | tree |
15420 | most_general_lambda (tree t) |
15421 | { |
15422 | while (tree ti = LAMBDA_EXPR_REGEN_INFO (t)) |
15423 | t = TI_TEMPLATE (ti); |
15424 | return t; |
15425 | } |
15426 | |
15427 | /* Return the set of template arguments used to regenerate the lambda T |
15428 | from its most general lambda. */ |
15429 | |
15430 | tree |
15431 | lambda_regenerating_args (tree t) |
15432 | { |
15433 | if (LAMBDA_FUNCTION_P (t)) |
15434 | t = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (t)); |
15435 | gcc_assert (TREE_CODE (t) == LAMBDA_EXPR); |
15436 | if (tree ti = LAMBDA_EXPR_REGEN_INFO (t)) |
15437 | return TI_ARGS (ti); |
15438 | else |
15439 | return NULL_TREE; |
15440 | } |
15441 | |
15442 | /* We're instantiating a variable from template function TCTX. Return the |
15443 | corresponding current enclosing scope. We can match them up using |
15444 | DECL_SOURCE_LOCATION because lambdas only ever have one source location, and |
15445 | the DECL_SOURCE_LOCATION for a function instantiation is updated to match |
15446 | the template definition in regenerate_decl_from_template. */ |
15447 | |
15448 | static tree |
15449 | enclosing_instantiation_of (tree tctx) |
15450 | { |
15451 | tree fn = current_function_decl; |
15452 | |
15453 | /* We shouldn't ever need to do this for other artificial functions. */ |
15454 | gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx)); |
15455 | |
15456 | for (; fn; fn = decl_function_context (fn)) |
15457 | if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx)) |
15458 | return fn; |
15459 | gcc_unreachable (); |
15460 | } |
15461 | |
15462 | /* Substitute the ARGS into the T, which is a _DECL. Return the |
15463 | result of the substitution. Issue error and warning messages under |
15464 | control of COMPLAIN. The flag USE_SPEC_TABLE controls if we look up |
15465 | and insert into the specializations table or if we can assume it's |
15466 | the caller's responsibility; this is used by instantiate_template |
15467 | to avoid doing some redundant work. */ |
15468 | |
15469 | static tree |
15470 | tsubst_decl (tree t, tree args, tsubst_flags_t complain, |
15471 | bool use_spec_table /* = true */) |
15472 | { |
15473 | #define RETURN(EXP) do { r = (EXP); goto out; } while(0) |
15474 | location_t saved_loc; |
15475 | tree r = NULL_TREE; |
15476 | tree in_decl = t; |
15477 | hashval_t hash = 0; |
15478 | |
15479 | if (t == error_mark_node) |
15480 | return error_mark_node; |
15481 | |
15482 | /* Set the filename and linenumber to improve error-reporting. */ |
15483 | saved_loc = input_location; |
15484 | input_location = DECL_SOURCE_LOCATION (t); |
15485 | |
15486 | switch (TREE_CODE (t)) |
15487 | { |
15488 | case TEMPLATE_DECL: |
15489 | r = tsubst_template_decl (t, args, complain, |
15490 | /*lambda_fntype=*/NULL_TREE, |
15491 | /*lambda_tparms=*/NULL_TREE); |
15492 | break; |
15493 | |
15494 | case FUNCTION_DECL: |
15495 | r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE, |
15496 | use_spec_table); |
15497 | break; |
15498 | |
15499 | case PARM_DECL: |
15500 | { |
15501 | tree type = NULL_TREE; |
15502 | int i, len = 1; |
15503 | tree expanded_types = NULL_TREE; |
15504 | tree prev_r = NULL_TREE; |
15505 | tree first_r = NULL_TREE; |
15506 | |
15507 | if (DECL_PACK_P (t)) |
15508 | { |
15509 | /* If there is a local specialization that isn't a |
15510 | parameter pack, it means that we're doing a "simple" |
15511 | substitution from inside tsubst_pack_expansion. Just |
15512 | return the local specialization (which will be a single |
15513 | parm). */ |
15514 | tree spec = retrieve_local_specialization (tmpl: t); |
15515 | if (spec |
15516 | && TREE_CODE (spec) == PARM_DECL |
15517 | && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION) |
15518 | RETURN (spec); |
15519 | |
15520 | /* Expand the TYPE_PACK_EXPANSION that provides the types for |
15521 | the parameters in this function parameter pack. */ |
15522 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, |
15523 | complain, in_decl); |
15524 | if (TREE_CODE (expanded_types) == TREE_VEC) |
15525 | { |
15526 | len = TREE_VEC_LENGTH (expanded_types); |
15527 | |
15528 | /* Zero-length parameter packs are boring. Just substitute |
15529 | into the chain. */ |
15530 | if (len == 0 && !cp_unevaluated_operand) |
15531 | RETURN (tsubst (TREE_CHAIN (t), args, complain, |
15532 | TREE_CHAIN (t))); |
15533 | } |
15534 | else |
15535 | { |
15536 | /* All we did was update the type. Make a note of that. */ |
15537 | type = expanded_types; |
15538 | expanded_types = NULL_TREE; |
15539 | } |
15540 | } |
15541 | |
15542 | /* Loop through all of the parameters we'll build. When T is |
15543 | a function parameter pack, LEN is the number of expanded |
15544 | types in EXPANDED_TYPES; otherwise, LEN is 1. */ |
15545 | r = NULL_TREE; |
15546 | for (i = 0; i < len; ++i) |
15547 | { |
15548 | prev_r = r; |
15549 | r = copy_node (t); |
15550 | if (DECL_TEMPLATE_PARM_P (t)) |
15551 | SET_DECL_TEMPLATE_PARM_P (r); |
15552 | |
15553 | if (expanded_types) |
15554 | /* We're on the Ith parameter of the function parameter |
15555 | pack. */ |
15556 | { |
15557 | /* Get the Ith type. */ |
15558 | type = TREE_VEC_ELT (expanded_types, i); |
15559 | |
15560 | /* Rename the parameter to include the index. */ |
15561 | DECL_NAME (r) |
15562 | = make_ith_pack_parameter_name (DECL_NAME (r), i); |
15563 | } |
15564 | else if (!type) |
15565 | /* We're dealing with a normal parameter. */ |
15566 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
15567 | |
15568 | if (type == error_mark_node && !(complain & tf_error)) |
15569 | RETURN (error_mark_node); |
15570 | |
15571 | type = type_decays_to (type); |
15572 | TREE_TYPE (r) = type; |
15573 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
15574 | |
15575 | if (DECL_INITIAL (r)) |
15576 | { |
15577 | if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX) |
15578 | DECL_INITIAL (r) = TREE_TYPE (r); |
15579 | else |
15580 | DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, |
15581 | complain, in_decl); |
15582 | } |
15583 | |
15584 | DECL_CONTEXT (r) = NULL_TREE; |
15585 | |
15586 | if (!DECL_TEMPLATE_PARM_P (r)) |
15587 | DECL_ARG_TYPE (r) = type_passed_as (type); |
15588 | |
15589 | if (!apply_late_template_attributes (decl_p: &r, DECL_ATTRIBUTES (r), attr_flags: 0, |
15590 | args, complain, in_decl)) |
15591 | return error_mark_node; |
15592 | |
15593 | /* Keep track of the first new parameter we |
15594 | generate. That's what will be returned to the |
15595 | caller. */ |
15596 | if (!first_r) |
15597 | first_r = r; |
15598 | |
15599 | /* Build a proper chain of parameters when substituting |
15600 | into a function parameter pack. */ |
15601 | if (prev_r) |
15602 | DECL_CHAIN (prev_r) = r; |
15603 | } |
15604 | |
15605 | /* If cp_unevaluated_operand is set, we're just looking for a |
15606 | single dummy parameter, so don't keep going. */ |
15607 | if (DECL_CHAIN (t) && !cp_unevaluated_operand) |
15608 | { |
15609 | tree chain = tsubst (DECL_CHAIN (t), args, |
15610 | complain, DECL_CHAIN (t)); |
15611 | if (chain == error_mark_node) |
15612 | RETURN (error_mark_node); |
15613 | DECL_CHAIN (r) = chain; |
15614 | } |
15615 | |
15616 | /* FIRST_R contains the start of the chain we've built. */ |
15617 | r = first_r; |
15618 | } |
15619 | break; |
15620 | |
15621 | case FIELD_DECL: |
15622 | { |
15623 | tree type = NULL_TREE; |
15624 | tree vec = NULL_TREE; |
15625 | tree expanded_types = NULL_TREE; |
15626 | int len = 1; |
15627 | |
15628 | if (PACK_EXPANSION_P (TREE_TYPE (t))) |
15629 | { |
15630 | /* This field is a lambda capture pack. Return a TREE_VEC of |
15631 | the expanded fields to instantiate_class_template_1. */ |
15632 | expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args, |
15633 | complain, in_decl); |
15634 | if (TREE_CODE (expanded_types) == TREE_VEC) |
15635 | { |
15636 | len = TREE_VEC_LENGTH (expanded_types); |
15637 | vec = make_tree_vec (len); |
15638 | } |
15639 | else |
15640 | { |
15641 | /* All we did was update the type. Make a note of that. */ |
15642 | type = expanded_types; |
15643 | expanded_types = NULL_TREE; |
15644 | } |
15645 | } |
15646 | |
15647 | for (int i = 0; i < len; ++i) |
15648 | { |
15649 | r = copy_decl (t); |
15650 | if (expanded_types) |
15651 | { |
15652 | type = TREE_VEC_ELT (expanded_types, i); |
15653 | DECL_NAME (r) |
15654 | = make_ith_pack_parameter_name (DECL_NAME (r), i); |
15655 | } |
15656 | else if (!type) |
15657 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
15658 | |
15659 | if (type == error_mark_node) |
15660 | RETURN (error_mark_node); |
15661 | TREE_TYPE (r) = type; |
15662 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
15663 | |
15664 | if (DECL_C_BIT_FIELD (r)) |
15665 | /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the |
15666 | number of bits. */ |
15667 | DECL_BIT_FIELD_REPRESENTATIVE (r) |
15668 | = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args, |
15669 | complain, in_decl); |
15670 | if (DECL_INITIAL (t)) |
15671 | { |
15672 | /* Set up DECL_TEMPLATE_INFO so that we can get at the |
15673 | NSDMI in perform_member_init. Still set DECL_INITIAL |
15674 | so that we know there is one. */ |
15675 | DECL_INITIAL (r) = void_node; |
15676 | gcc_assert (DECL_LANG_SPECIFIC (r) == NULL); |
15677 | retrofit_lang_decl (r); |
15678 | DECL_TEMPLATE_INFO (r) = build_template_info (template_decl: t, template_args: args); |
15679 | } |
15680 | /* We don't have to set DECL_CONTEXT here; it is set by |
15681 | finish_member_declaration. */ |
15682 | DECL_CHAIN (r) = NULL_TREE; |
15683 | |
15684 | if (!apply_late_template_attributes (decl_p: &r, DECL_ATTRIBUTES (r), attr_flags: 0, |
15685 | args, complain, in_decl)) |
15686 | return error_mark_node; |
15687 | |
15688 | if (vec) |
15689 | TREE_VEC_ELT (vec, i) = r; |
15690 | } |
15691 | |
15692 | if (vec) |
15693 | r = vec; |
15694 | } |
15695 | break; |
15696 | |
15697 | case USING_DECL: |
15698 | /* We reach here only for member using decls. We also need to check |
15699 | uses_template_parms because DECL_DEPENDENT_P is not set for a |
15700 | using-declaration that designates a member of the current |
15701 | instantiation (c++/53549). */ |
15702 | if (DECL_DEPENDENT_P (t) |
15703 | || uses_template_parms (USING_DECL_SCOPE (t))) |
15704 | { |
15705 | /* True iff this using-decl was written as a pack expansion |
15706 | (and a pack appeared in its scope or name). If a pack |
15707 | appeared in both, we expand the packs separately and |
15708 | manually merge them. */ |
15709 | bool variadic_p = false; |
15710 | |
15711 | tree scope = USING_DECL_SCOPE (t); |
15712 | if (PACK_EXPANSION_P (scope)) |
15713 | { |
15714 | scope = tsubst_pack_expansion (t: scope, args, |
15715 | complain: complain | tf_qualifying_scope, |
15716 | in_decl); |
15717 | variadic_p = true; |
15718 | } |
15719 | else |
15720 | scope = tsubst_scope (scope, args, complain, in_decl); |
15721 | |
15722 | tree name = DECL_NAME (t); |
15723 | if (IDENTIFIER_CONV_OP_P (name) |
15724 | && PACK_EXPANSION_P (TREE_TYPE (name))) |
15725 | { |
15726 | name = tsubst_pack_expansion (TREE_TYPE (name), args, |
15727 | complain, in_decl); |
15728 | if (name == error_mark_node) |
15729 | { |
15730 | r = error_mark_node; |
15731 | break; |
15732 | } |
15733 | for (tree& elt : tree_vec_range (name)) |
15734 | elt = make_conv_op_name (elt); |
15735 | variadic_p = true; |
15736 | } |
15737 | else |
15738 | name = tsubst_name (name, args, complain, in_decl); |
15739 | |
15740 | int len; |
15741 | if (!variadic_p) |
15742 | len = 1; |
15743 | else if (TREE_CODE (scope) == TREE_VEC |
15744 | && TREE_CODE (name) == TREE_VEC) |
15745 | { |
15746 | if (TREE_VEC_LENGTH (scope) != TREE_VEC_LENGTH (name)) |
15747 | { |
15748 | error ("mismatched argument pack lengths (%d vs %d)", |
15749 | TREE_VEC_LENGTH (scope), TREE_VEC_LENGTH (name)); |
15750 | r = error_mark_node; |
15751 | break; |
15752 | } |
15753 | len = TREE_VEC_LENGTH (scope); |
15754 | } |
15755 | else if (TREE_CODE (scope) == TREE_VEC) |
15756 | len = TREE_VEC_LENGTH (scope); |
15757 | else /* TREE_CODE (name) == TREE_VEC */ |
15758 | len = TREE_VEC_LENGTH (name); |
15759 | |
15760 | r = make_tree_vec (len); |
15761 | for (int i = 0; i < len; ++i) |
15762 | { |
15763 | tree escope = (TREE_CODE (scope) == TREE_VEC |
15764 | ? TREE_VEC_ELT (scope, i) |
15765 | : scope); |
15766 | tree ename = (TREE_CODE (name) == TREE_VEC |
15767 | ? TREE_VEC_ELT (name, i) |
15768 | : name); |
15769 | tree elt = do_class_using_decl (escope, ename); |
15770 | if (!elt) |
15771 | { |
15772 | r = error_mark_node; |
15773 | break; |
15774 | } |
15775 | TREE_PROTECTED (elt) = TREE_PROTECTED (t); |
15776 | TREE_PRIVATE (elt) = TREE_PRIVATE (t); |
15777 | TREE_VEC_ELT (r, i) = elt; |
15778 | } |
15779 | |
15780 | if (!variadic_p && r != error_mark_node) |
15781 | r = TREE_VEC_ELT (r, 0); |
15782 | } |
15783 | else |
15784 | { |
15785 | r = copy_node (t); |
15786 | DECL_CHAIN (r) = NULL_TREE; |
15787 | } |
15788 | break; |
15789 | |
15790 | case TYPE_DECL: |
15791 | case VAR_DECL: |
15792 | { |
15793 | tree argvec = NULL_TREE; |
15794 | tree gen_tmpl = NULL_TREE; |
15795 | tree tmpl = NULL_TREE; |
15796 | tree type = NULL_TREE; |
15797 | |
15798 | if (TREE_TYPE (t) == error_mark_node) |
15799 | RETURN (error_mark_node); |
15800 | |
15801 | if (TREE_CODE (t) == TYPE_DECL |
15802 | && (TREE_CODE (TREE_TYPE (t)) == TU_LOCAL_ENTITY |
15803 | || t == TYPE_MAIN_DECL (TREE_TYPE (t)))) |
15804 | { |
15805 | /* If this is the canonical decl, we don't have to |
15806 | mess with instantiations, and often we can't (for |
15807 | typename, template type parms and such). Note that |
15808 | TYPE_NAME is not correct for the above test if |
15809 | we've copied the type for a typedef. */ |
15810 | type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
15811 | if (type == error_mark_node) |
15812 | RETURN (error_mark_node); |
15813 | r = TYPE_NAME (type); |
15814 | break; |
15815 | } |
15816 | |
15817 | /* Check to see if we already have the specialization we |
15818 | need. */ |
15819 | tree spec = NULL_TREE; |
15820 | bool local_p = false; |
15821 | tree ctx = DECL_CONTEXT (t); |
15822 | if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)) |
15823 | && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))) |
15824 | { |
15825 | local_p = false; |
15826 | if (DECL_CLASS_SCOPE_P (t)) |
15827 | { |
15828 | ctx = tsubst_entering_scope (t: ctx, args, complain, in_decl); |
15829 | if (DECL_SELF_REFERENCE_P (t)) |
15830 | /* The context and type of an injected-class-name are |
15831 | the same, so we don't need to substitute both. */ |
15832 | type = ctx; |
15833 | /* If CTX is unchanged, then T is in fact the |
15834 | specialization we want. That situation occurs when |
15835 | referencing a static data member within in its own |
15836 | class. We can use pointer equality, rather than |
15837 | same_type_p, because DECL_CONTEXT is always |
15838 | canonical... */ |
15839 | if (ctx == DECL_CONTEXT (t) |
15840 | /* ... unless T is a member template; in which |
15841 | case our caller can be willing to create a |
15842 | specialization of that template represented |
15843 | by T. */ |
15844 | && !(DECL_TI_TEMPLATE (t) |
15845 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))) |
15846 | spec = t; |
15847 | } |
15848 | |
15849 | if (!spec) |
15850 | { |
15851 | tmpl = DECL_TI_TEMPLATE (t); |
15852 | if (use_spec_table) |
15853 | { |
15854 | argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl); |
15855 | if (argvec == error_mark_node) |
15856 | RETURN (error_mark_node); |
15857 | gen_tmpl = most_general_template (tmpl); |
15858 | hash = spec_hasher::hash (tmpl: gen_tmpl, args: argvec); |
15859 | spec = retrieve_specialization (tmpl: gen_tmpl, args: argvec, hash); |
15860 | } |
15861 | else |
15862 | argvec = args; |
15863 | } |
15864 | } |
15865 | else |
15866 | { |
15867 | if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))) |
15868 | /* Subsequent calls to pushdecl will fill this in. */ |
15869 | ctx = NULL_TREE; |
15870 | /* A local variable. */ |
15871 | local_p = true; |
15872 | /* Unless this is a reference to a static variable from an |
15873 | enclosing function, in which case we need to fill it in now. */ |
15874 | if (TREE_STATIC (t)) |
15875 | { |
15876 | tree fn = enclosing_instantiation_of (DECL_CONTEXT (t)); |
15877 | if (fn != current_function_decl) |
15878 | ctx = fn; |
15879 | } |
15880 | spec = retrieve_local_specialization (tmpl: t); |
15881 | } |
15882 | /* If we already have the specialization we need, there is |
15883 | nothing more to do. */ |
15884 | if (spec) |
15885 | { |
15886 | r = spec; |
15887 | break; |
15888 | } |
15889 | |
15890 | /* Create a new node for the specialization we need. */ |
15891 | if (type == NULL_TREE) |
15892 | { |
15893 | if (is_typedef_decl (x: t)) |
15894 | type = DECL_ORIGINAL_TYPE (t); |
15895 | else |
15896 | type = TREE_TYPE (t); |
15897 | if (VAR_P (t) |
15898 | && VAR_HAD_UNKNOWN_BOUND (t) |
15899 | && type != error_mark_node) |
15900 | type = strip_array_domain (type); |
15901 | tsubst_flags_t tcomplain = complain; |
15902 | if (VAR_P (t)) |
15903 | tcomplain |= tf_tst_ok; |
15904 | type = tsubst (type, args, tcomplain, in_decl); |
15905 | /* Substituting the type might have recursively instantiated this |
15906 | same alias (c++/86171). */ |
15907 | if (use_spec_table && gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl) |
15908 | && (spec = retrieve_specialization (tmpl: gen_tmpl, args: argvec, hash))) |
15909 | { |
15910 | r = spec; |
15911 | break; |
15912 | } |
15913 | } |
15914 | if (type == error_mark_node && !(complain & tf_error)) |
15915 | RETURN (error_mark_node); |
15916 | r = copy_decl (t); |
15917 | if (VAR_P (r)) |
15918 | { |
15919 | DECL_INITIALIZED_P (r) = 0; |
15920 | DECL_TEMPLATE_INSTANTIATED (r) = 0; |
15921 | if (TREE_CODE (type) == FUNCTION_TYPE) |
15922 | { |
15923 | /* It may seem that this case cannot occur, since: |
15924 | |
15925 | typedef void f(); |
15926 | void g() { f x; } |
15927 | |
15928 | declares a function, not a variable. However: |
15929 | |
15930 | typedef void f(); |
15931 | template <typename T> void g() { T t; } |
15932 | template void g<f>(); |
15933 | |
15934 | is an attempt to declare a variable with function |
15935 | type. */ |
15936 | error ("variable %qD has function type", |
15937 | /* R is not yet sufficiently initialized, so we |
15938 | just use its name. */ |
15939 | DECL_NAME (r)); |
15940 | RETURN (error_mark_node); |
15941 | } |
15942 | type = complete_type (type); |
15943 | /* Wait until cp_finish_decl to set this again, to handle |
15944 | circular dependency (template/instantiate6.C). */ |
15945 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0; |
15946 | type = check_var_type (DECL_NAME (r), type, |
15947 | DECL_SOURCE_LOCATION (r)); |
15948 | if (DECL_HAS_VALUE_EXPR_P (t)) |
15949 | { |
15950 | tree ve = DECL_VALUE_EXPR (t); |
15951 | /* If the DECL_VALUE_EXPR is converted to the declared type, |
15952 | preserve the identity so that gimplify_type_sizes works. */ |
15953 | bool nop = (TREE_CODE (ve) == NOP_EXPR); |
15954 | if (nop) |
15955 | ve = TREE_OPERAND (ve, 0); |
15956 | ve = tsubst_expr (ve, args, complain, in_decl); |
15957 | if (REFERENCE_REF_P (ve)) |
15958 | { |
15959 | gcc_assert (TYPE_REF_P (type)); |
15960 | ve = TREE_OPERAND (ve, 0); |
15961 | } |
15962 | if (nop) |
15963 | ve = build_nop (type, ve); |
15964 | else if (DECL_LANG_SPECIFIC (t) |
15965 | && DECL_OMP_PRIVATIZED_MEMBER (t) |
15966 | && TREE_CODE (ve) == COMPONENT_REF |
15967 | && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL |
15968 | && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type) |
15969 | type = TREE_TYPE (ve); |
15970 | else |
15971 | gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve)) |
15972 | == TYPE_MAIN_VARIANT (type)); |
15973 | SET_DECL_VALUE_EXPR (r, ve); |
15974 | } |
15975 | if (CP_DECL_THREAD_LOCAL_P (r) |
15976 | && !processing_template_decl) |
15977 | set_decl_tls_model (r, decl_default_tls_model (r)); |
15978 | } |
15979 | else if (DECL_SELF_REFERENCE_P (t)) |
15980 | SET_DECL_SELF_REFERENCE_P (r); |
15981 | TREE_TYPE (r) = type; |
15982 | cp_apply_type_quals_to_decl (cp_type_quals (type), r); |
15983 | DECL_CONTEXT (r) = ctx; |
15984 | /* Clear out the mangled name and RTL for the instantiation. */ |
15985 | SET_DECL_ASSEMBLER_NAME (r, NULL_TREE); |
15986 | if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL)) |
15987 | SET_DECL_RTL (r, NULL); |
15988 | set_instantiating_module (r); |
15989 | |
15990 | /* The initializer must not be expanded until it is required; |
15991 | see [temp.inst]. */ |
15992 | DECL_INITIAL (r) = NULL_TREE; |
15993 | DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0; |
15994 | if (VAR_P (r)) |
15995 | { |
15996 | if (DECL_LANG_SPECIFIC (r)) |
15997 | SET_DECL_DEPENDENT_INIT_P (r, false); |
15998 | |
15999 | SET_DECL_MODE (r, VOIDmode); |
16000 | |
16001 | /* Possibly limit visibility based on template args. */ |
16002 | DECL_VISIBILITY (r) = VISIBILITY_DEFAULT; |
16003 | if (DECL_VISIBILITY_SPECIFIED (t)) |
16004 | { |
16005 | DECL_VISIBILITY_SPECIFIED (r) = 0; |
16006 | DECL_ATTRIBUTES (r) |
16007 | = remove_attribute ("visibility", DECL_ATTRIBUTES (r)); |
16008 | } |
16009 | determine_visibility (r); |
16010 | if ((!local_p || TREE_STATIC (t)) |
16011 | && !(flag_openmp && DECL_LANG_SPECIFIC (t) |
16012 | && DECL_OMP_DECLARE_MAPPER_P (t)) |
16013 | && DECL_SECTION_NAME (t)) |
16014 | set_decl_section_name (r, t); |
16015 | } |
16016 | |
16017 | if (!local_p) |
16018 | { |
16019 | /* A static data member declaration is always marked |
16020 | external when it is declared in-class, even if an |
16021 | initializer is present. We mimic the non-template |
16022 | processing here. */ |
16023 | DECL_EXTERNAL (r) = 1; |
16024 | if (DECL_NAMESPACE_SCOPE_P (t)) |
16025 | DECL_NOT_REALLY_EXTERN (r) = 1; |
16026 | |
16027 | DECL_TEMPLATE_INFO (r) = build_template_info (template_decl: tmpl, template_args: argvec); |
16028 | SET_DECL_IMPLICIT_INSTANTIATION (r); |
16029 | if (use_spec_table) |
16030 | register_specialization (spec: r, tmpl: gen_tmpl, args: argvec, is_friend: false, hash); |
16031 | } |
16032 | else |
16033 | { |
16034 | if (DECL_LANG_SPECIFIC (r)) |
16035 | DECL_TEMPLATE_INFO (r) = NULL_TREE; |
16036 | if (!cp_unevaluated_operand) |
16037 | register_local_specialization (spec: r, tmpl: t); |
16038 | } |
16039 | |
16040 | DECL_CHAIN (r) = NULL_TREE; |
16041 | |
16042 | if (!apply_late_template_attributes (decl_p: &r, DECL_ATTRIBUTES (r), |
16043 | /*flags=*/attr_flags: 0, |
16044 | args, complain, in_decl)) |
16045 | return error_mark_node; |
16046 | |
16047 | /* Preserve a typedef that names a type. */ |
16048 | if (is_typedef_decl (x: r) && type != error_mark_node) |
16049 | { |
16050 | DECL_ORIGINAL_TYPE (r) = NULL_TREE; |
16051 | set_underlying_type (r); |
16052 | |
16053 | /* common_handle_aligned_attribute doesn't apply the alignment |
16054 | to DECL_ORIGINAL_TYPE. */ |
16055 | if (TYPE_USER_ALIGN (TREE_TYPE (t))) |
16056 | TREE_TYPE (r) = build_aligned_type (TREE_TYPE (r), |
16057 | TYPE_ALIGN (TREE_TYPE (t))); |
16058 | |
16059 | /* Preserve structural-ness of a partially instantiated typedef. */ |
16060 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t)) |
16061 | && dependent_type_p (TREE_TYPE (r))) |
16062 | SET_TYPE_STRUCTURAL_EQUALITY (TREE_TYPE (r)); |
16063 | } |
16064 | |
16065 | if (flag_openmp |
16066 | && VAR_P (t) |
16067 | && DECL_LANG_SPECIFIC (t) |
16068 | && DECL_OMP_DECLARE_MAPPER_P (t) |
16069 | && strchr (IDENTIFIER_POINTER (DECL_NAME (t)), c: '~') == NULL) |
16070 | DECL_NAME (r) = omp_mapper_id (DECL_NAME (t), TREE_TYPE (r)); |
16071 | |
16072 | layout_decl (r, 0); |
16073 | } |
16074 | break; |
16075 | |
16076 | default: |
16077 | gcc_unreachable (); |
16078 | } |
16079 | #undef RETURN |
16080 | |
16081 | out: |
16082 | /* Restore the file and line information. */ |
16083 | input_location = saved_loc; |
16084 | |
16085 | return r; |
16086 | } |
16087 | |
16088 | /* Substitute into the complete parameter type list PARMS. */ |
16089 | |
16090 | tree |
16091 | tsubst_function_parms (tree parms, |
16092 | tree args, |
16093 | tsubst_flags_t complain, |
16094 | tree in_decl) |
16095 | { |
16096 | return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl); |
16097 | } |
16098 | |
16099 | /* Substitute into the ARG_TYPES of a function type. |
16100 | If END is a TREE_CHAIN, leave it and any following types |
16101 | un-substituted. */ |
16102 | |
16103 | static tree |
16104 | tsubst_arg_types (tree arg_types, |
16105 | tree args, |
16106 | tree end, |
16107 | tsubst_flags_t complain, |
16108 | tree in_decl) |
16109 | { |
16110 | tree type = NULL_TREE; |
16111 | int len = 1; |
16112 | tree expanded_args = NULL_TREE; |
16113 | |
16114 | if (!arg_types || arg_types == void_list_node || arg_types == end) |
16115 | return arg_types; |
16116 | |
16117 | if (PACK_EXPANSION_P (TREE_VALUE (arg_types))) |
16118 | { |
16119 | /* For a pack expansion, perform substitution on the |
16120 | entire expression. Later on, we'll handle the arguments |
16121 | one-by-one. */ |
16122 | expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types), |
16123 | args, complain, in_decl); |
16124 | |
16125 | if (TREE_CODE (expanded_args) == TREE_VEC) |
16126 | /* So that we'll spin through the parameters, one by one. */ |
16127 | len = TREE_VEC_LENGTH (expanded_args); |
16128 | else |
16129 | { |
16130 | /* We only partially substituted into the parameter |
16131 | pack. Our type is TYPE_PACK_EXPANSION. */ |
16132 | type = expanded_args; |
16133 | expanded_args = NULL_TREE; |
16134 | } |
16135 | } |
16136 | else |
16137 | type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl); |
16138 | |
16139 | /* Check if a substituted type is erroneous before substituting into |
16140 | the rest of the chain. */ |
16141 | for (int i = 0; i < len; i++) |
16142 | { |
16143 | if (expanded_args) |
16144 | type = TREE_VEC_ELT (expanded_args, i); |
16145 | |
16146 | if (type == error_mark_node) |
16147 | return error_mark_node; |
16148 | if (VOID_TYPE_P (type)) |
16149 | { |
16150 | if (complain & tf_error) |
16151 | { |
16152 | error ("invalid parameter type %qT", type); |
16153 | if (in_decl) |
16154 | error ("in declaration %q+D", in_decl); |
16155 | } |
16156 | return error_mark_node; |
16157 | } |
16158 | } |
16159 | |
16160 | /* We do not substitute into default arguments here. The standard |
16161 | mandates that they be instantiated only when needed, which is |
16162 | done in build_over_call. */ |
16163 | tree default_arg = TREE_PURPOSE (arg_types); |
16164 | |
16165 | /* Except that we do substitute default arguments under tsubst_lambda_expr, |
16166 | since the new op() won't have any associated template arguments for us |
16167 | to refer to later. */ |
16168 | if (lambda_fn_in_template_p (fn: in_decl) |
16169 | || (in_decl && TREE_CODE (in_decl) == FUNCTION_DECL |
16170 | && DECL_LOCAL_DECL_P (in_decl))) |
16171 | default_arg = tsubst_expr (default_arg, args, complain, in_decl); |
16172 | |
16173 | tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types), |
16174 | args, end, complain, in_decl); |
16175 | if (remaining_arg_types == error_mark_node) |
16176 | return error_mark_node; |
16177 | |
16178 | for (int i = len-1; i >= 0; i--) |
16179 | { |
16180 | if (expanded_args) |
16181 | type = TREE_VEC_ELT (expanded_args, i); |
16182 | |
16183 | /* Do array-to-pointer, function-to-pointer conversion, and ignore |
16184 | top-level qualifiers as required. */ |
16185 | type = cv_unqualified (type_decays_to (type)); |
16186 | |
16187 | if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE) |
16188 | { |
16189 | /* We've instantiated a template before its default arguments |
16190 | have been parsed. This can happen for a nested template |
16191 | class, and is not an error unless we require the default |
16192 | argument in a call of this function. */ |
16193 | remaining_arg_types |
16194 | = tree_cons (default_arg, type, remaining_arg_types); |
16195 | vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg), |
16196 | obj: remaining_arg_types); |
16197 | } |
16198 | else |
16199 | remaining_arg_types |
16200 | = hash_tree_cons (default_arg, type, remaining_arg_types); |
16201 | } |
16202 | |
16203 | return remaining_arg_types; |
16204 | } |
16205 | |
16206 | /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does |
16207 | *not* handle the exception-specification for FNTYPE, because the |
16208 | initial substitution of explicitly provided template parameters |
16209 | during argument deduction forbids substitution into the |
16210 | exception-specification: |
16211 | |
16212 | [temp.deduct] |
16213 | |
16214 | All references in the function type of the function template to the |
16215 | corresponding template parameters are replaced by the specified tem- |
16216 | plate argument values. If a substitution in a template parameter or |
16217 | in the function type of the function template results in an invalid |
16218 | type, type deduction fails. [Note: The equivalent substitution in |
16219 | exception specifications is done only when the function is instanti- |
16220 | ated, at which point a program is ill-formed if the substitution |
16221 | results in an invalid type.] */ |
16222 | |
16223 | static tree |
16224 | tsubst_function_type (tree t, |
16225 | tree args, |
16226 | tsubst_flags_t complain, |
16227 | tree in_decl) |
16228 | { |
16229 | tree return_type; |
16230 | tree arg_types = NULL_TREE; |
16231 | |
16232 | /* The TYPE_CONTEXT is not used for function/method types. */ |
16233 | gcc_assert (TYPE_CONTEXT (t) == NULL_TREE); |
16234 | |
16235 | /* DR 1227: Mixing immediate and non-immediate contexts in deduction |
16236 | failure. */ |
16237 | bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t); |
16238 | |
16239 | if (late_return_type_p) |
16240 | { |
16241 | /* Substitute the argument types. */ |
16242 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE, |
16243 | complain, in_decl); |
16244 | if (arg_types == error_mark_node) |
16245 | return error_mark_node; |
16246 | |
16247 | tree save_ccp = current_class_ptr; |
16248 | tree save_ccr = current_class_ref; |
16249 | tree this_type = (TREE_CODE (t) == METHOD_TYPE |
16250 | ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE); |
16251 | bool do_inject = this_type && CLASS_TYPE_P (this_type); |
16252 | if (do_inject) |
16253 | { |
16254 | /* DR 1207: 'this' is in scope in the trailing return type. */ |
16255 | inject_this_parameter (this_type, cp_type_quals (this_type)); |
16256 | } |
16257 | |
16258 | /* Substitute the return type. */ |
16259 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
16260 | |
16261 | if (do_inject) |
16262 | { |
16263 | current_class_ptr = save_ccp; |
16264 | current_class_ref = save_ccr; |
16265 | } |
16266 | } |
16267 | else |
16268 | /* Substitute the return type. */ |
16269 | return_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
16270 | |
16271 | if (return_type == error_mark_node) |
16272 | return error_mark_node; |
16273 | /* DR 486 clarifies that creation of a function type with an |
16274 | invalid return type is a deduction failure. */ |
16275 | if (TREE_CODE (return_type) == ARRAY_TYPE |
16276 | || TREE_CODE (return_type) == FUNCTION_TYPE) |
16277 | { |
16278 | if (complain & tf_error) |
16279 | { |
16280 | if (TREE_CODE (return_type) == ARRAY_TYPE) |
16281 | error ("function returning an array"); |
16282 | else |
16283 | error ("function returning a function"); |
16284 | } |
16285 | return error_mark_node; |
16286 | } |
16287 | |
16288 | if (!late_return_type_p) |
16289 | { |
16290 | /* Substitute the argument types. */ |
16291 | arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE, |
16292 | complain, in_decl); |
16293 | if (arg_types == error_mark_node) |
16294 | return error_mark_node; |
16295 | } |
16296 | |
16297 | /* Construct a new type node and return it. */ |
16298 | return rebuild_function_or_method_type (t, args, return_type, arg_types, |
16299 | /*raises=*/NULL_TREE, complain); |
16300 | } |
16301 | |
16302 | /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template |
16303 | ARGS into that specification, and return the substituted |
16304 | specification. If there is no specification, return NULL_TREE. */ |
16305 | |
16306 | static tree |
16307 | tsubst_exception_specification (tree fntype, |
16308 | tree args, |
16309 | tsubst_flags_t complain, |
16310 | tree in_decl, |
16311 | bool defer_ok) |
16312 | { |
16313 | tree specs; |
16314 | tree new_specs; |
16315 | |
16316 | specs = TYPE_RAISES_EXCEPTIONS (fntype); |
16317 | new_specs = NULL_TREE; |
16318 | if (specs && TREE_PURPOSE (specs)) |
16319 | { |
16320 | /* A noexcept-specifier. */ |
16321 | tree expr = TREE_PURPOSE (specs); |
16322 | if (TREE_CODE (expr) == INTEGER_CST) |
16323 | new_specs = expr; |
16324 | else if (defer_ok) |
16325 | { |
16326 | /* Defer instantiation of noexcept-specifiers to avoid |
16327 | excessive instantiations (c++/49107). */ |
16328 | new_specs = make_node (DEFERRED_NOEXCEPT); |
16329 | if (DEFERRED_NOEXCEPT_SPEC_P (specs)) |
16330 | { |
16331 | /* We already partially instantiated this member template, |
16332 | so combine the new args with the old. */ |
16333 | DEFERRED_NOEXCEPT_PATTERN (new_specs) |
16334 | = DEFERRED_NOEXCEPT_PATTERN (expr); |
16335 | DEFERRED_NOEXCEPT_ARGS (new_specs) |
16336 | = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), extra_args: args); |
16337 | } |
16338 | else |
16339 | { |
16340 | DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr; |
16341 | DEFERRED_NOEXCEPT_ARGS (new_specs) = args; |
16342 | } |
16343 | } |
16344 | else |
16345 | { |
16346 | if (DEFERRED_NOEXCEPT_SPEC_P (specs)) |
16347 | { |
16348 | args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), |
16349 | extra_args: args); |
16350 | expr = DEFERRED_NOEXCEPT_PATTERN (expr); |
16351 | } |
16352 | new_specs = tsubst_expr (expr, args, complain, in_decl); |
16353 | } |
16354 | new_specs = build_noexcept_spec (new_specs, complain); |
16355 | /* We've instantiated a template before a noexcept-specifier |
16356 | contained therein has been parsed. This can happen for |
16357 | a nested template class: |
16358 | |
16359 | struct S { |
16360 | template<typename> struct B { B() noexcept(...); }; |
16361 | struct A : B<int> { ... use B() ... }; |
16362 | }; |
16363 | |
16364 | where completing B<int> will trigger instantiating the |
16365 | noexcept, even though we only parse it at the end of S. */ |
16366 | if (UNPARSED_NOEXCEPT_SPEC_P (specs)) |
16367 | { |
16368 | gcc_checking_assert (defer_ok); |
16369 | vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), obj: new_specs); |
16370 | } |
16371 | } |
16372 | else if (specs) |
16373 | { |
16374 | if (! TREE_VALUE (specs)) |
16375 | new_specs = specs; |
16376 | else |
16377 | while (specs) |
16378 | { |
16379 | tree spec; |
16380 | int i, len = 1; |
16381 | tree expanded_specs = NULL_TREE; |
16382 | |
16383 | if (PACK_EXPANSION_P (TREE_VALUE (specs))) |
16384 | { |
16385 | /* Expand the pack expansion type. */ |
16386 | expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs), |
16387 | args, complain, |
16388 | in_decl); |
16389 | |
16390 | if (expanded_specs == error_mark_node) |
16391 | return error_mark_node; |
16392 | else if (TREE_CODE (expanded_specs) == TREE_VEC) |
16393 | len = TREE_VEC_LENGTH (expanded_specs); |
16394 | else |
16395 | { |
16396 | /* We're substituting into a member template, so |
16397 | we got a TYPE_PACK_EXPANSION back. Add that |
16398 | expansion and move on. */ |
16399 | gcc_assert (TREE_CODE (expanded_specs) |
16400 | == TYPE_PACK_EXPANSION); |
16401 | new_specs = add_exception_specifier (new_specs, |
16402 | expanded_specs, |
16403 | complain); |
16404 | specs = TREE_CHAIN (specs); |
16405 | continue; |
16406 | } |
16407 | } |
16408 | |
16409 | for (i = 0; i < len; ++i) |
16410 | { |
16411 | if (expanded_specs) |
16412 | spec = TREE_VEC_ELT (expanded_specs, i); |
16413 | else |
16414 | spec = tsubst (TREE_VALUE (specs), args, complain, in_decl); |
16415 | if (spec == error_mark_node) |
16416 | return spec; |
16417 | new_specs = add_exception_specifier (new_specs, spec, |
16418 | complain); |
16419 | } |
16420 | |
16421 | specs = TREE_CHAIN (specs); |
16422 | } |
16423 | } |
16424 | return new_specs; |
16425 | } |
16426 | |
16427 | /* Substitute through a TREE_LIST of types or expressions, handling pack |
16428 | expansions. */ |
16429 | |
16430 | tree |
16431 | tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
16432 | { |
16433 | if (t == void_list_node) |
16434 | return t; |
16435 | |
16436 | tree purpose = TREE_PURPOSE (t); |
16437 | tree purposevec = NULL_TREE; |
16438 | if (!purpose) |
16439 | ; |
16440 | else if (PACK_EXPANSION_P (purpose)) |
16441 | { |
16442 | purpose = tsubst_pack_expansion (t: purpose, args, complain, in_decl); |
16443 | if (TREE_CODE (purpose) == TREE_VEC) |
16444 | purposevec = purpose; |
16445 | } |
16446 | else if (TYPE_P (purpose)) |
16447 | purpose = tsubst (purpose, args, complain, in_decl); |
16448 | else |
16449 | purpose = tsubst_expr (purpose, args, complain, in_decl); |
16450 | if (purpose == error_mark_node || purposevec == error_mark_node) |
16451 | return error_mark_node; |
16452 | |
16453 | tree value = TREE_VALUE (t); |
16454 | tree valuevec = NULL_TREE; |
16455 | if (!value) |
16456 | ; |
16457 | else if (PACK_EXPANSION_P (value)) |
16458 | { |
16459 | value = tsubst_pack_expansion (t: value, args, complain, in_decl); |
16460 | if (TREE_CODE (value) == TREE_VEC) |
16461 | valuevec = value; |
16462 | } |
16463 | else if (TYPE_P (value)) |
16464 | value = tsubst (value, args, complain, in_decl); |
16465 | else |
16466 | value = tsubst_expr (value, args, complain, in_decl); |
16467 | if (value == error_mark_node || valuevec == error_mark_node) |
16468 | return error_mark_node; |
16469 | |
16470 | tree chain = TREE_CHAIN (t); |
16471 | if (!chain) |
16472 | ; |
16473 | else if (TREE_CODE (chain) == TREE_LIST) |
16474 | chain = tsubst_tree_list (t: chain, args, complain, in_decl); |
16475 | else if (TYPE_P (chain)) |
16476 | chain = tsubst (chain, args, complain, in_decl); |
16477 | else |
16478 | chain = tsubst_expr (chain, args, complain, in_decl); |
16479 | if (chain == error_mark_node) |
16480 | return error_mark_node; |
16481 | |
16482 | if (purpose == TREE_PURPOSE (t) |
16483 | && value == TREE_VALUE (t) |
16484 | && chain == TREE_CHAIN (t)) |
16485 | return t; |
16486 | |
16487 | int len; |
16488 | /* Determine the number of arguments. */ |
16489 | if (purposevec) |
16490 | { |
16491 | len = TREE_VEC_LENGTH (purposevec); |
16492 | gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec)); |
16493 | } |
16494 | else if (valuevec) |
16495 | len = TREE_VEC_LENGTH (valuevec); |
16496 | else |
16497 | len = 1; |
16498 | |
16499 | for (int i = len; i-- > 0; ) |
16500 | { |
16501 | if (purposevec) |
16502 | purpose = TREE_VEC_ELT (purposevec, i); |
16503 | if (valuevec) |
16504 | value = TREE_VEC_ELT (valuevec, i); |
16505 | |
16506 | if (value && TYPE_P (value)) |
16507 | chain = hash_tree_cons (purpose, value, chain); |
16508 | else |
16509 | chain = tree_cons (purpose, value, chain); |
16510 | } |
16511 | |
16512 | return chain; |
16513 | } |
16514 | |
16515 | /* Take the tree structure T and replace template parameters used |
16516 | therein with the argument vector ARGS. IN_DECL is an associated |
16517 | decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE. |
16518 | Issue error and warning messages under control of COMPLAIN. Note |
16519 | that we must be relatively non-tolerant of extensions here, in |
16520 | order to preserve conformance; if we allow substitutions that |
16521 | should not be allowed, we may allow argument deductions that should |
16522 | not succeed, and therefore report ambiguous overload situations |
16523 | where there are none. In theory, we could allow the substitution, |
16524 | but indicate that it should have failed, and allow our caller to |
16525 | make sure that the right thing happens, but we don't try to do this |
16526 | yet. |
16527 | |
16528 | This function is used for dealing with types, decls and the like; |
16529 | for expressions, use tsubst_expr or tsubst_copy. */ |
16530 | |
16531 | tree |
16532 | tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
16533 | { |
16534 | enum tree_code code; |
16535 | tree type, r = NULL_TREE; |
16536 | |
16537 | if (t == NULL_TREE || t == error_mark_node |
16538 | || t == integer_type_node |
16539 | || t == void_type_node |
16540 | || t == char_type_node |
16541 | || t == unknown_type_node |
16542 | || TREE_CODE (t) == NAMESPACE_DECL |
16543 | || TREE_CODE (t) == TRANSLATION_UNIT_DECL) |
16544 | return t; |
16545 | |
16546 | /* Any instantiation of a template containing a TU-local entity is an |
16547 | exposure, so always issue a hard error irrespective of complain. */ |
16548 | if (TREE_CODE (t) == TU_LOCAL_ENTITY) |
16549 | { |
16550 | complain_about_tu_local_entity (e: t); |
16551 | return error_mark_node; |
16552 | } |
16553 | |
16554 | tsubst_flags_t tst_ok_flag = (complain & tf_tst_ok); |
16555 | complain &= ~tf_tst_ok; |
16556 | |
16557 | tsubst_flags_t qualifying_scope_flag = (complain & tf_qualifying_scope); |
16558 | complain &= ~tf_qualifying_scope; |
16559 | |
16560 | if (DECL_P (t)) |
16561 | return tsubst_decl (t, args, complain); |
16562 | |
16563 | if (args == NULL_TREE) |
16564 | return t; |
16565 | |
16566 | code = TREE_CODE (t); |
16567 | |
16568 | gcc_assert (code != IDENTIFIER_NODE); |
16569 | type = TREE_TYPE (t); |
16570 | |
16571 | gcc_assert (type != unknown_type_node); |
16572 | |
16573 | if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl)) |
16574 | return d; |
16575 | |
16576 | /* Reuse typedefs. We need to do this to handle dependent attributes, |
16577 | such as attribute aligned. */ |
16578 | if (TYPE_P (t) |
16579 | && typedef_variant_p (type: t)) |
16580 | { |
16581 | tree decl = TYPE_NAME (t); |
16582 | |
16583 | if (alias_template_specialization_p (t, transparent_typedefs: nt_opaque)) |
16584 | { |
16585 | /* DECL represents an alias template and we want to |
16586 | instantiate it. */ |
16587 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); |
16588 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); |
16589 | r = instantiate_alias_template (tmpl, gen_args, complain); |
16590 | } |
16591 | else if (DECL_CLASS_SCOPE_P (decl) |
16592 | && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
16593 | && uses_template_parms (DECL_CONTEXT (decl))) |
16594 | { |
16595 | tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl)); |
16596 | tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl); |
16597 | r = retrieve_specialization (tmpl, args: gen_args, hash: 0); |
16598 | } |
16599 | else if (DECL_FUNCTION_SCOPE_P (decl) |
16600 | && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl)) |
16601 | && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl)))) |
16602 | r = retrieve_local_specialization (tmpl: decl); |
16603 | else |
16604 | /* The typedef is from a non-template context. */ |
16605 | return t; |
16606 | |
16607 | if (r) |
16608 | { |
16609 | r = TREE_TYPE (r); |
16610 | r = cp_build_qualified_type |
16611 | (r, cp_type_quals (t) | cp_type_quals (r), |
16612 | complain | tf_ignore_bad_quals); |
16613 | return r; |
16614 | } |
16615 | else |
16616 | { |
16617 | /* We don't have an instantiation yet, so drop the typedef. */ |
16618 | int quals = cp_type_quals (t); |
16619 | t = DECL_ORIGINAL_TYPE (decl); |
16620 | t = cp_build_qualified_type (t, quals, |
16621 | complain | tf_ignore_bad_quals); |
16622 | } |
16623 | } |
16624 | |
16625 | bool fndecl_type = (complain & tf_fndecl_type); |
16626 | complain &= ~tf_fndecl_type; |
16627 | |
16628 | if (type |
16629 | && code != TYPENAME_TYPE |
16630 | && code != TEMPLATE_TYPE_PARM |
16631 | && code != TEMPLATE_PARM_INDEX |
16632 | && code != IDENTIFIER_NODE |
16633 | && code != FUNCTION_TYPE |
16634 | && code != METHOD_TYPE |
16635 | && code != PACK_INDEX_TYPE) |
16636 | type = tsubst (t: type, args, complain, in_decl); |
16637 | if (type == error_mark_node) |
16638 | return error_mark_node; |
16639 | |
16640 | switch (code) |
16641 | { |
16642 | case RECORD_TYPE: |
16643 | if (TYPE_PTRMEMFUNC_P (t)) |
16644 | return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl); |
16645 | /* Fall through. */ |
16646 | case UNION_TYPE: |
16647 | case ENUMERAL_TYPE: |
16648 | if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t)) |
16649 | { |
16650 | /* Figure out what arguments are appropriate for the |
16651 | type we are trying to find. For example, given: |
16652 | |
16653 | template <class T> struct S; |
16654 | template <class T, class U> void f(T, U) { S<U> su; } |
16655 | |
16656 | and supposing that we are instantiating f<int, double>, |
16657 | then our ARGS will be {int, double}, but, when looking up |
16658 | S we only want {double}. */ |
16659 | tree argvec = tsubst_template_args (TYPE_TI_ARGS (t), args, |
16660 | complain, in_decl); |
16661 | if (argvec == error_mark_node) |
16662 | return error_mark_node; |
16663 | |
16664 | tree r = lookup_template_class (d1: t, arglist: argvec, in_decl, NULL_TREE, |
16665 | complain); |
16666 | return cp_build_qualified_type (r, cp_type_quals (t), complain); |
16667 | } |
16668 | else |
16669 | /* This is not a template type, so there's nothing to do. */ |
16670 | return t; |
16671 | |
16672 | case ERROR_MARK: |
16673 | case IDENTIFIER_NODE: |
16674 | case VOID_TYPE: |
16675 | case OPAQUE_TYPE: |
16676 | case REAL_TYPE: |
16677 | case COMPLEX_TYPE: |
16678 | case VECTOR_TYPE: |
16679 | case BOOLEAN_TYPE: |
16680 | case NULLPTR_TYPE: |
16681 | case LANG_TYPE: |
16682 | return t; |
16683 | |
16684 | case INTEGER_TYPE: |
16685 | if (t == integer_type_node) |
16686 | return t; |
16687 | |
16688 | if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST |
16689 | && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST) |
16690 | return t; |
16691 | |
16692 | { |
16693 | tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0); |
16694 | |
16695 | max = tsubst_expr (omax, args, complain, in_decl); |
16696 | |
16697 | /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if |
16698 | needed. */ |
16699 | if (TREE_CODE (max) == NOP_EXPR |
16700 | && TREE_SIDE_EFFECTS (omax) |
16701 | && !TREE_TYPE (max)) |
16702 | TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0)); |
16703 | |
16704 | /* If we're in a partial instantiation, preserve the magic NOP_EXPR |
16705 | with TREE_SIDE_EFFECTS that indicates this is not an integral |
16706 | constant expression. */ |
16707 | if (processing_template_decl |
16708 | && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR) |
16709 | { |
16710 | gcc_assert (TREE_CODE (max) == NOP_EXPR); |
16711 | TREE_SIDE_EFFECTS (max) = 1; |
16712 | } |
16713 | |
16714 | return compute_array_index_type (NULL_TREE, max, complain); |
16715 | } |
16716 | |
16717 | case TEMPLATE_TYPE_PARM: |
16718 | if (TEMPLATE_TYPE_LEVEL (t) == 0) |
16719 | { |
16720 | /* This is either an ordinary level-less auto or a CTAD placeholder |
16721 | auto. These get replaced only via do_auto_deduction which, in the |
16722 | ordinary case, temporarily overrides its level to 1 before calling |
16723 | tsubst. CTAD placeholders are replaced via do_class_deduction. */ |
16724 | gcc_checking_assert (is_auto (t)); |
16725 | tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (t); |
16726 | if (!tmpl) |
16727 | /* Ordinary level-less auto has nothing to substitute. */ |
16728 | return t; |
16729 | |
16730 | /* Substitute the template of this CTAD placeholder. */ |
16731 | tmpl = tsubst_expr (tmpl, args, complain, in_decl); |
16732 | if (TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) |
16733 | tmpl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (tmpl); |
16734 | |
16735 | if (tmpl != CLASS_PLACEHOLDER_TEMPLATE (t)) |
16736 | return make_template_placeholder (tmpl); |
16737 | else |
16738 | return t; |
16739 | } |
16740 | /* Fall through. */ |
16741 | case TEMPLATE_TEMPLATE_PARM: |
16742 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
16743 | case TEMPLATE_PARM_INDEX: |
16744 | { |
16745 | int idx; |
16746 | int level; |
16747 | int levels; |
16748 | tree arg = NULL_TREE; |
16749 | |
16750 | r = NULL_TREE; |
16751 | |
16752 | gcc_assert (TREE_VEC_LENGTH (args) > 0); |
16753 | template_parm_level_and_index (t, &level, &idx); |
16754 | |
16755 | levels = TMPL_ARGS_DEPTH (args); |
16756 | if (level <= levels |
16757 | && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0) |
16758 | { |
16759 | arg = TMPL_ARG (args, level, idx); |
16760 | |
16761 | /* See through ARGUMENT_PACK_SELECT arguments. */ |
16762 | if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) |
16763 | arg = argument_pack_select_arg (t: arg); |
16764 | } |
16765 | |
16766 | if (arg == error_mark_node) |
16767 | return error_mark_node; |
16768 | else if (arg != NULL_TREE) |
16769 | { |
16770 | if (ARGUMENT_PACK_P (arg)) |
16771 | /* If ARG is an argument pack, we don't actually want to |
16772 | perform a substitution here, because substitutions |
16773 | for argument packs are only done |
16774 | element-by-element. We can get to this point when |
16775 | substituting the type of a non-type template |
16776 | parameter pack, when that type actually contains |
16777 | template parameter packs from an outer template, e.g., |
16778 | |
16779 | template<typename... Types> struct A { |
16780 | template<Types... Values> struct B { }; |
16781 | }; */ |
16782 | return t; |
16783 | |
16784 | if (code == TEMPLATE_TYPE_PARM) |
16785 | { |
16786 | int quals; |
16787 | |
16788 | gcc_assert (TYPE_P (arg)); |
16789 | |
16790 | quals = cp_type_quals (arg) | cp_type_quals (t); |
16791 | |
16792 | return cp_build_qualified_type |
16793 | (arg, quals, complain | tf_ignore_bad_quals); |
16794 | } |
16795 | else if (code == BOUND_TEMPLATE_TEMPLATE_PARM) |
16796 | { |
16797 | /* We are processing a type constructed from a |
16798 | template template parameter. */ |
16799 | tree argvec = tsubst (TYPE_TI_ARGS (t), |
16800 | args, complain, in_decl); |
16801 | if (argvec == error_mark_node) |
16802 | return error_mark_node; |
16803 | |
16804 | gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM |
16805 | || TREE_CODE (arg) == TEMPLATE_DECL |
16806 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE); |
16807 | |
16808 | if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) |
16809 | /* Consider this code: |
16810 | |
16811 | template <template <class> class Template> |
16812 | struct Internal { |
16813 | template <class Arg> using Bind = Template<Arg>; |
16814 | }; |
16815 | |
16816 | template <template <class> class Template, class Arg> |
16817 | using Instantiate = Template<Arg>; //#0 |
16818 | |
16819 | template <template <class> class Template, |
16820 | class Argument> |
16821 | using Bind = |
16822 | Instantiate<Internal<Template>::template Bind, |
16823 | Argument>; //#1 |
16824 | |
16825 | When #1 is parsed, the |
16826 | BOUND_TEMPLATE_TEMPLATE_PARM representing the |
16827 | parameter `Template' in #0 matches the |
16828 | UNBOUND_CLASS_TEMPLATE representing the argument |
16829 | `Internal<Template>::template Bind'; We then want |
16830 | to assemble the type `Bind<Argument>' that can't |
16831 | be fully created right now, because |
16832 | `Internal<Template>' not being complete, the Bind |
16833 | template cannot be looked up in that context. So |
16834 | we need to "store" `Bind<Argument>' for later |
16835 | when the context of Bind becomes complete. Let's |
16836 | store that in a TYPENAME_TYPE. */ |
16837 | return make_typename_type (TYPE_CONTEXT (arg), |
16838 | build_nt (TEMPLATE_ID_EXPR, |
16839 | TYPE_IDENTIFIER (arg), |
16840 | argvec), |
16841 | typename_type, |
16842 | complain); |
16843 | |
16844 | /* We can get a TEMPLATE_TEMPLATE_PARM here when we |
16845 | are resolving nested-types in the signature of a |
16846 | member function templates. Otherwise ARG is a |
16847 | TEMPLATE_DECL and is the real template to be |
16848 | instantiated. */ |
16849 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
16850 | arg = TYPE_NAME (arg); |
16851 | |
16852 | r = lookup_template_class (d1: arg, |
16853 | arglist: argvec, in_decl, |
16854 | DECL_CONTEXT (arg), |
16855 | complain); |
16856 | return cp_build_qualified_type |
16857 | (r, cp_type_quals (t) | cp_type_quals (r), complain); |
16858 | } |
16859 | else if (code == TEMPLATE_TEMPLATE_PARM) |
16860 | return arg; |
16861 | else |
16862 | /* TEMPLATE_PARM_INDEX. */ |
16863 | return convert_from_reference (unshare_expr (arg)); |
16864 | } |
16865 | |
16866 | if (level == 1) |
16867 | /* This can happen during the attempted tsubst'ing in |
16868 | unify. This means that we don't yet have any information |
16869 | about the template parameter in question. */ |
16870 | return t; |
16871 | |
16872 | /* Early in template argument deduction substitution, we don't |
16873 | want to reduce the level of 'auto', or it will be confused |
16874 | with a normal template parm in subsequent deduction. |
16875 | Similarly, don't reduce the level of template parameters to |
16876 | avoid mismatches when deducing their types. */ |
16877 | if (complain & tf_partial) |
16878 | return t; |
16879 | |
16880 | /* If we get here, we must have been looking at a parm for a |
16881 | more deeply nested template. Make a new version of this |
16882 | template parameter, but with a lower level. */ |
16883 | int quals; |
16884 | switch (code) |
16885 | { |
16886 | case TEMPLATE_TYPE_PARM: |
16887 | case TEMPLATE_TEMPLATE_PARM: |
16888 | quals = cp_type_quals (t); |
16889 | if (quals) |
16890 | { |
16891 | gcc_checking_assert (code == TEMPLATE_TYPE_PARM); |
16892 | t = TYPE_MAIN_VARIANT (t); |
16893 | } |
16894 | |
16895 | if (tree d = TEMPLATE_TYPE_DESCENDANTS (t)) |
16896 | if (TEMPLATE_PARM_LEVEL (d) == TEMPLATE_TYPE_LEVEL (t) - levels |
16897 | && (code == TEMPLATE_TYPE_PARM |
16898 | || TEMPLATE_TEMPLATE_PARM_SIMPLE_P (t))) |
16899 | /* Cache lowering a type parameter or a simple template |
16900 | template parameter. */ |
16901 | r = TREE_TYPE (d); |
16902 | |
16903 | if (!r) |
16904 | { |
16905 | r = copy_type (t); |
16906 | TEMPLATE_TYPE_PARM_INDEX (r) |
16907 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t), |
16908 | type: r, levels, args, complain); |
16909 | TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r); |
16910 | TYPE_MAIN_VARIANT (r) = r; |
16911 | TYPE_POINTER_TO (r) = NULL_TREE; |
16912 | TYPE_REFERENCE_TO (r) = NULL_TREE; |
16913 | |
16914 | if (code == TEMPLATE_TYPE_PARM) |
16915 | if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)) |
16916 | /* Propagate constraints on placeholders since they are |
16917 | only instantiated during satisfaction. */ |
16918 | PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci; |
16919 | |
16920 | if (TYPE_STRUCTURAL_EQUALITY_P (t)) |
16921 | SET_TYPE_STRUCTURAL_EQUALITY (r); |
16922 | else |
16923 | TYPE_CANONICAL (r) = canonical_type_parameter (type: r); |
16924 | } |
16925 | |
16926 | if (quals) |
16927 | r = cp_build_qualified_type (r, quals, |
16928 | complain | tf_ignore_bad_quals); |
16929 | break; |
16930 | |
16931 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
16932 | { |
16933 | tree tinfo = TYPE_TEMPLATE_INFO (t); |
16934 | /* We might need to substitute into the types of non-type |
16935 | template parameters. This also lowers the level of |
16936 | the ttp appropriately. */ |
16937 | tree tmpl = tsubst (TI_TEMPLATE (tinfo), args, |
16938 | complain, in_decl); |
16939 | if (tmpl == error_mark_node) |
16940 | return error_mark_node; |
16941 | tree argvec = tsubst (TI_ARGS (tinfo), args, |
16942 | complain, in_decl); |
16943 | if (argvec == error_mark_node) |
16944 | return error_mark_node; |
16945 | r = lookup_template_class (d1: tmpl, arglist: argvec, in_decl, NULL_TREE, |
16946 | complain); |
16947 | r = cp_build_qualified_type (r, cp_type_quals (t), complain); |
16948 | break; |
16949 | } |
16950 | |
16951 | case TEMPLATE_PARM_INDEX: |
16952 | /* OK, now substitute the type of the non-type parameter. We |
16953 | couldn't do it earlier because it might be an auto parameter, |
16954 | and we wouldn't need to if we had an argument. */ |
16955 | type = tsubst (t: type, args, complain, in_decl); |
16956 | if (type == error_mark_node) |
16957 | return error_mark_node; |
16958 | r = reduce_template_parm_level (index: t, type, levels, args, complain); |
16959 | break; |
16960 | |
16961 | default: |
16962 | gcc_unreachable (); |
16963 | } |
16964 | |
16965 | return r; |
16966 | } |
16967 | |
16968 | case TREE_LIST: |
16969 | return tsubst_tree_list (t, args, complain, in_decl); |
16970 | |
16971 | case TREE_BINFO: |
16972 | /* We should never be tsubsting a binfo. */ |
16973 | gcc_unreachable (); |
16974 | |
16975 | case TREE_VEC: |
16976 | /* A vector of template arguments. */ |
16977 | gcc_assert (!type); |
16978 | return tsubst_template_args (t, args, complain, in_decl); |
16979 | |
16980 | case POINTER_TYPE: |
16981 | case REFERENCE_TYPE: |
16982 | { |
16983 | if (type == TREE_TYPE (t) |
16984 | && TREE_CODE (type) != METHOD_TYPE |
16985 | && (TYPE_ATTRIBUTES (t) == NULL_TREE |
16986 | || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t)))) |
16987 | return t; |
16988 | |
16989 | /* [temp.deduct] |
16990 | |
16991 | Type deduction may fail for any of the following |
16992 | reasons: |
16993 | |
16994 | -- Attempting to create a pointer to reference type. |
16995 | -- Attempting to create a reference to a reference type or |
16996 | a reference to void. |
16997 | |
16998 | Core issue 106 says that creating a reference to a reference |
16999 | during instantiation is no longer a cause for failure. We |
17000 | only enforce this check in strict C++98 mode. */ |
17001 | if ((TYPE_REF_P (type) |
17002 | && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE)) |
17003 | || (code == REFERENCE_TYPE && VOID_TYPE_P (type))) |
17004 | { |
17005 | static location_t last_loc; |
17006 | |
17007 | /* We keep track of the last time we issued this error |
17008 | message to avoid spewing a ton of messages during a |
17009 | single bad template instantiation. */ |
17010 | if (complain & tf_error |
17011 | && last_loc != input_location) |
17012 | { |
17013 | if (VOID_TYPE_P (type)) |
17014 | error ("forming reference to void"); |
17015 | else if (code == POINTER_TYPE) |
17016 | error ("forming pointer to reference type %qT", type); |
17017 | else |
17018 | error ("forming reference to reference type %qT", type); |
17019 | last_loc = input_location; |
17020 | } |
17021 | |
17022 | return error_mark_node; |
17023 | } |
17024 | else if (TREE_CODE (type) == FUNCTION_TYPE |
17025 | && (type_memfn_quals (type) != TYPE_UNQUALIFIED |
17026 | || type_memfn_rqual (type) != REF_QUAL_NONE)) |
17027 | { |
17028 | if (complain & tf_error) |
17029 | { |
17030 | if (code == POINTER_TYPE) |
17031 | error ("forming pointer to qualified function type %qT", |
17032 | type); |
17033 | else |
17034 | error ("forming reference to qualified function type %qT", |
17035 | type); |
17036 | } |
17037 | return error_mark_node; |
17038 | } |
17039 | else if (code == POINTER_TYPE) |
17040 | { |
17041 | r = build_pointer_type (type); |
17042 | if (TREE_CODE (type) == METHOD_TYPE) |
17043 | r = build_ptrmemfunc_type (r); |
17044 | } |
17045 | else if (TYPE_REF_P (type)) |
17046 | /* In C++0x, during template argument substitution, when there is an |
17047 | attempt to create a reference to a reference type, reference |
17048 | collapsing is applied as described in [14.3.1/4 temp.arg.type]: |
17049 | |
17050 | "If a template-argument for a template-parameter T names a type |
17051 | that is a reference to a type A, an attempt to create the type |
17052 | 'lvalue reference to cv T' creates the type 'lvalue reference to |
17053 | A,' while an attempt to create the type type rvalue reference to |
17054 | cv T' creates the type T" |
17055 | */ |
17056 | r = cp_build_reference_type (TREE_TYPE (type), |
17057 | TYPE_REF_IS_RVALUE (t) |
17058 | && TYPE_REF_IS_RVALUE (type)); |
17059 | else |
17060 | r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t)); |
17061 | r = cp_build_qualified_type (r, cp_type_quals (t), complain); |
17062 | |
17063 | if (r != error_mark_node) |
17064 | /* Will this ever be needed for TYPE_..._TO values? */ |
17065 | layout_type (r); |
17066 | |
17067 | if (!apply_late_template_attributes (decl_p: &r, TYPE_ATTRIBUTES (t), |
17068 | /*flags=*/attr_flags: 0, |
17069 | args, complain, in_decl)) |
17070 | return error_mark_node; |
17071 | |
17072 | return r; |
17073 | } |
17074 | case OFFSET_TYPE: |
17075 | { |
17076 | r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl); |
17077 | if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r)) |
17078 | { |
17079 | /* [temp.deduct] |
17080 | |
17081 | Type deduction may fail for any of the following |
17082 | reasons: |
17083 | |
17084 | -- Attempting to create "pointer to member of T" when T |
17085 | is not a class type. */ |
17086 | if (complain & tf_error) |
17087 | error ("creating pointer to member of non-class type %qT", r); |
17088 | return error_mark_node; |
17089 | } |
17090 | if (TYPE_REF_P (type)) |
17091 | { |
17092 | if (complain & tf_error) |
17093 | error ("creating pointer to member reference type %qT", type); |
17094 | return error_mark_node; |
17095 | } |
17096 | if (VOID_TYPE_P (type)) |
17097 | { |
17098 | if (complain & tf_error) |
17099 | error ("creating pointer to member of type void"); |
17100 | return error_mark_node; |
17101 | } |
17102 | gcc_assert (TREE_CODE (type) != METHOD_TYPE); |
17103 | if (TREE_CODE (type) == FUNCTION_TYPE) |
17104 | { |
17105 | /* The type of the implicit object parameter gets its |
17106 | cv-qualifiers from the FUNCTION_TYPE. */ |
17107 | tree memptr; |
17108 | tree method_type |
17109 | = build_memfn_type (type, r, type_memfn_quals (type), |
17110 | type_memfn_rqual (type)); |
17111 | memptr = build_ptrmemfunc_type (build_pointer_type (method_type)); |
17112 | return cp_build_qualified_type (memptr, cp_type_quals (t), |
17113 | complain); |
17114 | } |
17115 | else |
17116 | return cp_build_qualified_type (build_ptrmem_type (r, type), |
17117 | cp_type_quals (t), |
17118 | complain); |
17119 | } |
17120 | case FUNCTION_TYPE: |
17121 | case METHOD_TYPE: |
17122 | { |
17123 | tree fntype; |
17124 | tree specs; |
17125 | fntype = tsubst_function_type (t, args, complain, in_decl); |
17126 | if (fntype == error_mark_node) |
17127 | return error_mark_node; |
17128 | |
17129 | /* Substitute the exception specification. */ |
17130 | specs = tsubst_exception_specification (fntype: t, args, complain, in_decl, |
17131 | /*defer_ok*/fndecl_type); |
17132 | if (specs == error_mark_node) |
17133 | return error_mark_node; |
17134 | if (specs) |
17135 | fntype = build_exception_variant (fntype, specs); |
17136 | return fntype; |
17137 | } |
17138 | case ARRAY_TYPE: |
17139 | { |
17140 | tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl); |
17141 | if (domain == error_mark_node) |
17142 | return error_mark_node; |
17143 | |
17144 | /* As an optimization, we avoid regenerating the array type if |
17145 | it will obviously be the same as T. */ |
17146 | if (type == TREE_TYPE (t) |
17147 | && domain == TYPE_DOMAIN (t) |
17148 | && (TYPE_ATTRIBUTES (t) == NULL_TREE |
17149 | || !ATTR_IS_DEPENDENT (TYPE_ATTRIBUTES (t)))) |
17150 | return t; |
17151 | |
17152 | /* These checks should match the ones in create_array_type_for_decl. |
17153 | |
17154 | [temp.deduct] |
17155 | |
17156 | The deduction may fail for any of the following reasons: |
17157 | |
17158 | -- Attempting to create an array with an element type that |
17159 | is void, a function type, or a reference type, or [DR337] |
17160 | an abstract class type. */ |
17161 | if (VOID_TYPE_P (type) |
17162 | || TREE_CODE (type) == FUNCTION_TYPE |
17163 | || (TREE_CODE (type) == ARRAY_TYPE |
17164 | && TYPE_DOMAIN (type) == NULL_TREE) |
17165 | || TYPE_REF_P (type)) |
17166 | { |
17167 | if (complain & tf_error) |
17168 | error ("creating array of %qT", type); |
17169 | return error_mark_node; |
17170 | } |
17171 | |
17172 | if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type, |
17173 | !(complain & tf_error))) |
17174 | return error_mark_node; |
17175 | |
17176 | r = build_cplus_array_type (type, domain); |
17177 | |
17178 | if (!valid_array_size_p (input_location, r, in_decl, |
17179 | (complain & tf_error))) |
17180 | return error_mark_node; |
17181 | |
17182 | if (TYPE_USER_ALIGN (t)) |
17183 | { |
17184 | SET_TYPE_ALIGN (r, TYPE_ALIGN (t)); |
17185 | TYPE_USER_ALIGN (r) = 1; |
17186 | } |
17187 | |
17188 | if (!apply_late_template_attributes (decl_p: &r, TYPE_ATTRIBUTES (t), |
17189 | /*flags=*/attr_flags: 0, |
17190 | args, complain, in_decl)) |
17191 | return error_mark_node; |
17192 | |
17193 | return r; |
17194 | } |
17195 | |
17196 | case TYPENAME_TYPE: |
17197 | { |
17198 | tree ctx = TYPE_CONTEXT (t); |
17199 | if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION) |
17200 | { |
17201 | ctx = tsubst_pack_expansion (t: ctx, args, |
17202 | complain: complain | tf_qualifying_scope, |
17203 | in_decl); |
17204 | if (ctx == error_mark_node) |
17205 | return error_mark_node; |
17206 | if (TREE_VEC_LENGTH (ctx) > 1) |
17207 | { |
17208 | if (complain & tf_error) |
17209 | error ("%qD expanded to more than one element", |
17210 | TYPENAME_TYPE_FULLNAME (t)); |
17211 | return error_mark_node; |
17212 | } |
17213 | if (TREE_VEC_LENGTH (ctx) == 0) |
17214 | { |
17215 | if (complain & tf_error) |
17216 | error ("%qD is instantiated for an empty pack", |
17217 | TYPENAME_TYPE_FULLNAME (t)); |
17218 | return error_mark_node; |
17219 | } |
17220 | ctx = TREE_VEC_ELT (ctx, 0); |
17221 | } |
17222 | else |
17223 | ctx = tsubst_entering_scope (t: ctx, args, |
17224 | complain: complain | tf_qualifying_scope, |
17225 | in_decl); |
17226 | if (ctx == error_mark_node) |
17227 | return error_mark_node; |
17228 | |
17229 | tree f = tsubst_name (TYPENAME_TYPE_FULLNAME (t), args, |
17230 | complain, in_decl); |
17231 | if (f == error_mark_node) |
17232 | return error_mark_node; |
17233 | |
17234 | if (!MAYBE_CLASS_TYPE_P (ctx)) |
17235 | { |
17236 | if (complain & tf_error) |
17237 | error ("%qT is not a class, struct, or union type", ctx); |
17238 | return error_mark_node; |
17239 | } |
17240 | else if (!uses_template_parms (t: ctx) && !TYPE_BEING_DEFINED (ctx)) |
17241 | { |
17242 | /* Normally, make_typename_type does not require that the CTX |
17243 | have complete type in order to allow things like: |
17244 | |
17245 | template <class T> struct S { typename S<T>::X Y; }; |
17246 | |
17247 | But, such constructs have already been resolved by this |
17248 | point, so here CTX really should have complete type, unless |
17249 | it's a partial instantiation. */ |
17250 | if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain)) |
17251 | return error_mark_node; |
17252 | } |
17253 | |
17254 | /* FIXME: TYPENAME_IS_CLASS_P conflates 'class' vs 'struct' vs 'union' |
17255 | tags. TYPENAME_TYPE should probably remember the exact tag that |
17256 | was written. */ |
17257 | enum tag_types tag_type |
17258 | = TYPENAME_IS_CLASS_P (t) ? class_type |
17259 | : TYPENAME_IS_ENUM_P (t) ? enum_type |
17260 | : typename_type; |
17261 | tsubst_flags_t tcomplain = complain | tf_keep_type_decl; |
17262 | tcomplain |= tst_ok_flag | qualifying_scope_flag; |
17263 | f = make_typename_type (ctx, f, tag_type, tcomplain); |
17264 | if (f == error_mark_node) |
17265 | return f; |
17266 | if (TREE_CODE (f) == TYPE_DECL) |
17267 | { |
17268 | complain |= tf_ignore_bad_quals; |
17269 | f = TREE_TYPE (f); |
17270 | } |
17271 | |
17272 | if (TREE_CODE (f) != TYPENAME_TYPE) |
17273 | { |
17274 | if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE) |
17275 | { |
17276 | if (complain & tf_error) |
17277 | error ("%qT resolves to %qT, which is not an enumeration type", |
17278 | t, f); |
17279 | else |
17280 | return error_mark_node; |
17281 | } |
17282 | else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f)) |
17283 | { |
17284 | if (complain & tf_error) |
17285 | error ("%qT resolves to %qT, which is not a class type", |
17286 | t, f); |
17287 | else |
17288 | return error_mark_node; |
17289 | } |
17290 | } |
17291 | |
17292 | return cp_build_qualified_type |
17293 | (f, cp_type_quals (f) | cp_type_quals (t), complain); |
17294 | } |
17295 | |
17296 | case UNBOUND_CLASS_TEMPLATE: |
17297 | { |
17298 | tree name = TYPE_IDENTIFIER (t); |
17299 | if (name == error_mark_node) |
17300 | return error_mark_node; |
17301 | |
17302 | tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t)); |
17303 | parm_list = tsubst_template_parms (parms: parm_list, args, complain); |
17304 | if (parm_list == error_mark_node) |
17305 | return error_mark_node; |
17306 | |
17307 | if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1) |
17308 | ++processing_template_decl; |
17309 | tree ctx = tsubst_entering_scope (TYPE_CONTEXT (t), args, |
17310 | complain, in_decl); |
17311 | if (parm_list && TMPL_PARMS_DEPTH (parm_list) > 1) |
17312 | --processing_template_decl; |
17313 | if (ctx == error_mark_node) |
17314 | return error_mark_node; |
17315 | |
17316 | return make_unbound_class_template (ctx, name, parm_list, complain); |
17317 | } |
17318 | |
17319 | case TYPEOF_TYPE: |
17320 | { |
17321 | tree type; |
17322 | |
17323 | ++cp_unevaluated_operand; |
17324 | ++c_inhibit_evaluation_warnings; |
17325 | |
17326 | type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args, complain, in_decl); |
17327 | |
17328 | --cp_unevaluated_operand; |
17329 | --c_inhibit_evaluation_warnings; |
17330 | |
17331 | type = finish_typeof (type); |
17332 | return cp_build_qualified_type (type, |
17333 | cp_type_quals (t) |
17334 | | cp_type_quals (type), |
17335 | complain); |
17336 | } |
17337 | |
17338 | case DECLTYPE_TYPE: |
17339 | { |
17340 | tree type; |
17341 | |
17342 | ++cp_unevaluated_operand; |
17343 | ++c_inhibit_evaluation_warnings; |
17344 | |
17345 | type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args, |
17346 | complain|tf_decltype, in_decl); |
17347 | |
17348 | --cp_unevaluated_operand; |
17349 | --c_inhibit_evaluation_warnings; |
17350 | |
17351 | if (DECLTYPE_FOR_LAMBDA_CAPTURE (t)) |
17352 | type = lambda_capture_field_type (type, |
17353 | false /*explicit_init*/, |
17354 | DECLTYPE_FOR_REF_CAPTURE (t)); |
17355 | else if (DECLTYPE_FOR_LAMBDA_PROXY (t)) |
17356 | type = lambda_proxy_type (type); |
17357 | else |
17358 | { |
17359 | bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t); |
17360 | if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR |
17361 | && EXPR_P (type)) |
17362 | /* In a template ~id could be either a complement expression |
17363 | or an unqualified-id naming a destructor; if instantiating |
17364 | it produces an expression, it's not an id-expression or |
17365 | member access. */ |
17366 | id = false; |
17367 | type = finish_decltype_type (type, id, complain); |
17368 | } |
17369 | return cp_build_qualified_type (type, |
17370 | cp_type_quals (t) |
17371 | | cp_type_quals (type), |
17372 | complain | tf_ignore_bad_quals); |
17373 | } |
17374 | |
17375 | case TRAIT_TYPE: |
17376 | { |
17377 | tree type1 = TRAIT_TYPE_TYPE1 (t); |
17378 | if (TYPE_P (type1)) |
17379 | type1 = tsubst (t: type1, args, complain, in_decl); |
17380 | else |
17381 | type1 = tsubst_expr (type1, args, complain, in_decl); |
17382 | tree type2 = tsubst (TRAIT_TYPE_TYPE2 (t), args, complain, in_decl); |
17383 | type = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2, complain); |
17384 | return cp_build_qualified_type (type, |
17385 | cp_type_quals (t) | cp_type_quals (type), |
17386 | complain | tf_ignore_bad_quals); |
17387 | } |
17388 | |
17389 | case TYPE_ARGUMENT_PACK: |
17390 | case NONTYPE_ARGUMENT_PACK: |
17391 | return tsubst_argument_pack (orig_arg: t, args, complain, in_decl); |
17392 | |
17393 | case PACK_INDEX_TYPE: |
17394 | return tsubst_pack_index (t, args, complain, in_decl); |
17395 | |
17396 | case VOID_CST: |
17397 | case INTEGER_CST: |
17398 | case REAL_CST: |
17399 | case STRING_CST: |
17400 | case PLUS_EXPR: |
17401 | case MINUS_EXPR: |
17402 | case NEGATE_EXPR: |
17403 | case NOP_EXPR: |
17404 | case INDIRECT_REF: |
17405 | case ADDR_EXPR: |
17406 | case CALL_EXPR: |
17407 | case ARRAY_REF: |
17408 | case SCOPE_REF: |
17409 | case OMP_ARRAY_SECTION: |
17410 | /* We should use one of the expression tsubsts for these codes. */ |
17411 | gcc_unreachable (); |
17412 | |
17413 | default: |
17414 | sorry ("use of %qs in template", get_tree_code_name (code)); |
17415 | return error_mark_node; |
17416 | } |
17417 | } |
17418 | |
17419 | /* Convenience wrapper over tsubst for substituting into the LHS |
17420 | of the :: scope resolution operator. */ |
17421 | |
17422 | static tree |
17423 | tsubst_scope (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
17424 | { |
17425 | gcc_checking_assert (TYPE_P (t)); |
17426 | return tsubst (t, args, complain: complain | tf_qualifying_scope, in_decl); |
17427 | } |
17428 | |
17429 | /* Convenience wrapper over tsubst for substituting into an id-expression |
17430 | without resolving its terminal name. */ |
17431 | |
17432 | static tree |
17433 | tsubst_name (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
17434 | { |
17435 | return tsubst_expr (t, args, complain | tf_no_name_lookup, in_decl); |
17436 | } |
17437 | |
17438 | /* OLDFNS is a lookup set of member functions from some class template, and |
17439 | NEWFNS is a lookup set of member functions from NEWTYPE, a specialization |
17440 | of that class template. Return the subset of NEWFNS which are |
17441 | specializations of a function from OLDFNS. */ |
17442 | |
17443 | static tree |
17444 | filter_memfn_lookup (tree oldfns, tree newfns, tree newtype) |
17445 | { |
17446 | /* Record all member functions from the old lookup set OLDFNS into |
17447 | VISIBLE_SET. */ |
17448 | hash_set<tree> visible_set; |
17449 | bool seen_dep_using = false; |
17450 | for (tree fn : lkp_range (oldfns)) |
17451 | { |
17452 | if (TREE_CODE (fn) == USING_DECL) |
17453 | { |
17454 | /* Imprecisely handle dependent using-decl by keeping all members |
17455 | in the new lookup set that are defined in a base class, i.e. |
17456 | members that could plausibly have been introduced by this |
17457 | dependent using-decl. |
17458 | FIXME: Track which members are introduced by a dependent |
17459 | using-decl precisely, perhaps by performing another lookup |
17460 | from the substituted USING_DECL_SCOPE. */ |
17461 | gcc_checking_assert (DECL_DEPENDENT_P (fn)); |
17462 | seen_dep_using = true; |
17463 | } |
17464 | else |
17465 | visible_set.add (k: fn); |
17466 | } |
17467 | |
17468 | /* Returns true iff (a less specialized version of) FN appeared in |
17469 | the old lookup set OLDFNS. */ |
17470 | auto visible_p = [newtype, seen_dep_using, &visible_set] (tree fn) { |
17471 | if (DECL_CONTEXT (fn) != newtype) |
17472 | /* FN is a member function from a base class, introduced via a |
17473 | using-decl; if it might have been introduced by a dependent |
17474 | using-decl then just conservatively keep it, otherwise look |
17475 | in the old lookup set for FN exactly. */ |
17476 | return seen_dep_using || visible_set.contains (k: fn); |
17477 | else if (TREE_CODE (fn) == TEMPLATE_DECL) |
17478 | /* FN is a member function template from the current class; |
17479 | look in the old lookup set for the TEMPLATE_DECL from which |
17480 | it was specialized. */ |
17481 | return visible_set.contains (DECL_TI_TEMPLATE (fn)); |
17482 | else |
17483 | /* FN is a non-template member function from the current class; |
17484 | look in the old lookup set for the FUNCTION_DECL from which |
17485 | it was specialized. */ |
17486 | return visible_set.contains (DECL_TEMPLATE_RESULT |
17487 | (DECL_TI_TEMPLATE (fn))); |
17488 | }; |
17489 | |
17490 | bool lookup_changed_p = false; |
17491 | for (tree fn : lkp_range (newfns)) |
17492 | if (!visible_p (fn)) |
17493 | { |
17494 | lookup_changed_p = true; |
17495 | break; |
17496 | } |
17497 | if (!lookup_changed_p) |
17498 | return newfns; |
17499 | |
17500 | /* Filter out from NEWFNS the member functions that weren't |
17501 | previously visible according to OLDFNS. */ |
17502 | tree filtered_fns = NULL_TREE; |
17503 | unsigned filtered_size = 0; |
17504 | for (tree fn : lkp_range (newfns)) |
17505 | if (visible_p (fn)) |
17506 | { |
17507 | filtered_fns = lookup_add (fns: fn, lookup: filtered_fns); |
17508 | filtered_size++; |
17509 | } |
17510 | gcc_checking_assert (seen_dep_using |
17511 | ? filtered_size >= visible_set.elements () |
17512 | : filtered_size == visible_set.elements ()); |
17513 | |
17514 | return filtered_fns; |
17515 | } |
17516 | |
17517 | /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the |
17518 | expression on the left-hand side of the "." or "->" operator. We |
17519 | only do the lookup if we had a dependent BASELINK. Otherwise we |
17520 | adjust it onto the instantiated heirarchy. */ |
17521 | |
17522 | static tree |
17523 | tsubst_baselink (tree baselink, tree object_type, |
17524 | tree args, tsubst_flags_t complain, tree in_decl) |
17525 | { |
17526 | bool qualified_p = BASELINK_QUALIFIED_P (baselink); |
17527 | tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)); |
17528 | qualifying_scope = tsubst (t: qualifying_scope, args, complain, in_decl); |
17529 | |
17530 | tree optype = BASELINK_OPTYPE (baselink); |
17531 | optype = tsubst (t: optype, args, complain, in_decl); |
17532 | |
17533 | tree template_args = NULL_TREE; |
17534 | bool template_id_p = false; |
17535 | tree fns = BASELINK_FUNCTIONS (baselink); |
17536 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
17537 | { |
17538 | template_id_p = true; |
17539 | template_args = TREE_OPERAND (fns, 1); |
17540 | fns = TREE_OPERAND (fns, 0); |
17541 | if (template_args) |
17542 | template_args = tsubst_template_args (t: template_args, args, |
17543 | complain, in_decl); |
17544 | } |
17545 | |
17546 | tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink)); |
17547 | binfo_type = tsubst (t: binfo_type, args, complain, in_decl); |
17548 | bool dependent_p = (binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink)) |
17549 | || optype != BASELINK_OPTYPE (baselink)); |
17550 | |
17551 | if (dependent_p) |
17552 | { |
17553 | tree name = OVL_NAME (fns); |
17554 | if (IDENTIFIER_CONV_OP_P (name)) |
17555 | name = make_conv_op_name (optype); |
17556 | |
17557 | /* See maybe_dependent_member_ref. */ |
17558 | if ((complain & tf_dguide) && dependent_scope_p (qualifying_scope)) |
17559 | { |
17560 | if (template_id_p) |
17561 | name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name, |
17562 | template_args); |
17563 | return build_qualified_name (NULL_TREE, qualifying_scope, name, |
17564 | /* ::template */false); |
17565 | } |
17566 | |
17567 | if (name == complete_dtor_identifier) |
17568 | /* Treat as-if non-dependent below. */ |
17569 | dependent_p = false; |
17570 | |
17571 | bool maybe_incomplete = BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink); |
17572 | baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1, |
17573 | complain); |
17574 | if (maybe_incomplete) |
17575 | { |
17576 | /* Filter out from the new lookup set those functions which didn't |
17577 | appear in the original lookup set (in a less specialized form). |
17578 | This is needed to preserve the consistency of member lookup |
17579 | performed in an incomplete-class context, within which |
17580 | later-declared members ought to remain invisible. */ |
17581 | BASELINK_FUNCTIONS (baselink) |
17582 | = filter_memfn_lookup (oldfns: fns, BASELINK_FUNCTIONS (baselink), |
17583 | newtype: binfo_type); |
17584 | BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (baselink) = true; |
17585 | } |
17586 | |
17587 | if (!baselink) |
17588 | { |
17589 | if (complain & tf_error) |
17590 | { |
17591 | if (constructor_name_p (name, qualifying_scope)) |
17592 | error ("cannot call constructor %<%T::%D%> directly", |
17593 | qualifying_scope, name); |
17594 | else |
17595 | /* Lookup succeeded at parse time, but failed during |
17596 | instantiation; must be because we're trying to refer to it |
17597 | while forming its declaration (c++/120204). */ |
17598 | error ("declaration of %<%T::%D%> depends on itself", |
17599 | qualifying_scope, name); |
17600 | } |
17601 | return error_mark_node; |
17602 | } |
17603 | |
17604 | fns = BASELINK_FUNCTIONS (baselink); |
17605 | } |
17606 | else |
17607 | { |
17608 | /* We're going to overwrite pieces below, make a duplicate. */ |
17609 | baselink = copy_node (baselink); |
17610 | |
17611 | if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink))) |
17612 | { |
17613 | /* The decl we found was from non-dependent scope, but we still need |
17614 | to update the binfos for the instantiated qualifying_scope. */ |
17615 | BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope); |
17616 | BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type, |
17617 | ba_unique, nullptr, complain); |
17618 | } |
17619 | } |
17620 | |
17621 | /* If lookup found a single function, mark it as used at this point. |
17622 | (If lookup found multiple functions the one selected later by |
17623 | overload resolution will be marked as used at that point.) */ |
17624 | if (!template_id_p && !really_overloaded_fn (fns)) |
17625 | { |
17626 | tree fn = OVL_FIRST (fns); |
17627 | bool ok = mark_used (fn, complain); |
17628 | if (!ok && !(complain & tf_error)) |
17629 | return error_mark_node; |
17630 | if (ok && BASELINK_P (baselink)) |
17631 | /* We might have instantiated an auto function. */ |
17632 | TREE_TYPE (baselink) = TREE_TYPE (fn); |
17633 | } |
17634 | |
17635 | if (BASELINK_P (baselink)) |
17636 | { |
17637 | /* Add back the template arguments, if present. */ |
17638 | if (template_id_p) |
17639 | BASELINK_FUNCTIONS (baselink) |
17640 | = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args); |
17641 | |
17642 | /* Update the conversion operator type. */ |
17643 | BASELINK_OPTYPE (baselink) = optype; |
17644 | } |
17645 | |
17646 | if (!object_type) |
17647 | object_type = current_class_type; |
17648 | |
17649 | if (qualified_p || !dependent_p) |
17650 | { |
17651 | baselink = adjust_result_of_qualified_name_lookup (baselink, |
17652 | qualifying_scope, |
17653 | object_type); |
17654 | if (!qualified_p) |
17655 | /* We need to call adjust_result_of_qualified_name_lookup in case the |
17656 | destructor names a base class, but we unset BASELINK_QUALIFIED_P |
17657 | so that we still get virtual function binding. */ |
17658 | BASELINK_QUALIFIED_P (baselink) = false; |
17659 | } |
17660 | |
17661 | return baselink; |
17662 | } |
17663 | |
17664 | /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is |
17665 | true if the qualified-id will be a postfix-expression in-and-of |
17666 | itself; false if more of the postfix-expression follows the |
17667 | QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand |
17668 | of "&". */ |
17669 | |
17670 | static tree |
17671 | tsubst_qualified_id (tree qualified_id, tree args, |
17672 | tsubst_flags_t complain, tree in_decl, |
17673 | bool done, bool address_p) |
17674 | { |
17675 | tree expr; |
17676 | tree scope; |
17677 | tree name; |
17678 | bool is_template; |
17679 | tree template_args; |
17680 | location_t loc = EXPR_LOCATION (qualified_id); |
17681 | |
17682 | gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF); |
17683 | |
17684 | /* Figure out what name to look up. */ |
17685 | name = TREE_OPERAND (qualified_id, 1); |
17686 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
17687 | { |
17688 | is_template = true; |
17689 | template_args = TREE_OPERAND (name, 1); |
17690 | if (template_args) |
17691 | template_args = tsubst_template_args (t: template_args, args, |
17692 | complain, in_decl); |
17693 | if (template_args == error_mark_node) |
17694 | return error_mark_node; |
17695 | name = TREE_OPERAND (name, 0); |
17696 | } |
17697 | else |
17698 | { |
17699 | is_template = false; |
17700 | template_args = NULL_TREE; |
17701 | } |
17702 | |
17703 | /* Substitute into the qualifying scope. When there are no ARGS, we |
17704 | are just trying to simplify a non-dependent expression. In that |
17705 | case the qualifying scope may be dependent, and, in any case, |
17706 | substituting will not help. */ |
17707 | scope = TREE_OPERAND (qualified_id, 0); |
17708 | if (args) |
17709 | { |
17710 | scope = tsubst_scope (t: scope, args, complain, in_decl); |
17711 | expr = tsubst_name (t: name, args, complain, in_decl); |
17712 | } |
17713 | else |
17714 | expr = name; |
17715 | |
17716 | if (dependent_scope_p (scope)) |
17717 | { |
17718 | if (TREE_CODE (expr) == SCOPE_REF) |
17719 | /* We built one in tsubst_baselink. */ |
17720 | gcc_checking_assert (same_type_p (scope, TREE_OPERAND (expr, 0))); |
17721 | else |
17722 | { |
17723 | if (is_template) |
17724 | expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, |
17725 | template_args); |
17726 | expr = build_qualified_name (NULL_TREE, scope, expr, |
17727 | QUALIFIED_NAME_IS_TEMPLATE |
17728 | (qualified_id)); |
17729 | } |
17730 | REF_PARENTHESIZED_P (expr) = REF_PARENTHESIZED_P (qualified_id); |
17731 | return expr; |
17732 | } |
17733 | |
17734 | if (!BASELINK_P (name) && !DECL_P (expr)) |
17735 | { |
17736 | if (TREE_CODE (expr) == BIT_NOT_EXPR) |
17737 | { |
17738 | /* A BIT_NOT_EXPR is used to represent a destructor. */ |
17739 | if (!check_dtor_name (scope, TREE_OPERAND (expr, 0))) |
17740 | { |
17741 | error ("qualifying type %qT does not match destructor name ~%qT", |
17742 | scope, TREE_OPERAND (expr, 0)); |
17743 | expr = error_mark_node; |
17744 | } |
17745 | else |
17746 | expr = lookup_qualified_name (scope, complete_dtor_identifier, |
17747 | LOOK_want::NORMAL, false); |
17748 | } |
17749 | else |
17750 | expr = lookup_qualified_name (scope, name: expr, LOOK_want::NORMAL, false); |
17751 | if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL |
17752 | ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL) |
17753 | { |
17754 | if (complain & tf_error) |
17755 | { |
17756 | auto_diagnostic_group d; |
17757 | error ("dependent-name %qE is parsed as a non-type, but " |
17758 | "instantiation yields a type", qualified_id); |
17759 | inform (input_location, "say %<typename %E%> if a type is meant", qualified_id); |
17760 | } |
17761 | return error_mark_node; |
17762 | } |
17763 | } |
17764 | |
17765 | if (DECL_P (expr)) |
17766 | { |
17767 | if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE, |
17768 | scope, complain)) |
17769 | return error_mark_node; |
17770 | /* Remember that there was a reference to this entity. */ |
17771 | if (!mark_used (expr, complain) && !(complain & tf_error)) |
17772 | return error_mark_node; |
17773 | } |
17774 | |
17775 | if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST) |
17776 | { |
17777 | if (complain & tf_error) |
17778 | qualified_name_lookup_error (scope, |
17779 | TREE_OPERAND (qualified_id, 1), |
17780 | expr, input_location); |
17781 | return error_mark_node; |
17782 | } |
17783 | |
17784 | if (is_template) |
17785 | { |
17786 | if (variable_template_p (t: expr)) |
17787 | expr = lookup_and_finish_template_variable (templ: expr, targs: template_args, |
17788 | complain); |
17789 | else |
17790 | expr = lookup_template_function (fns: expr, arglist: template_args); |
17791 | } |
17792 | |
17793 | if (expr == error_mark_node && complain & tf_error) |
17794 | qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1), |
17795 | expr, input_location); |
17796 | else if (TYPE_P (scope)) |
17797 | { |
17798 | expr = (adjust_result_of_qualified_name_lookup |
17799 | (expr, scope, current_nonlambda_class_type ())); |
17800 | expr = (finish_qualified_id_expr |
17801 | (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id), |
17802 | QUALIFIED_NAME_IS_TEMPLATE (qualified_id), |
17803 | /*template_arg_p=*/false, complain)); |
17804 | } |
17805 | |
17806 | /* Expressions do not generally have reference type. */ |
17807 | if (TREE_CODE (expr) != SCOPE_REF |
17808 | /* However, if we're about to form a pointer-to-member, we just |
17809 | want the referenced member referenced. */ |
17810 | && TREE_CODE (expr) != OFFSET_REF) |
17811 | expr = convert_from_reference (expr); |
17812 | |
17813 | if (REF_PARENTHESIZED_P (qualified_id)) |
17814 | expr = force_paren_expr (expr); |
17815 | |
17816 | expr = maybe_wrap_with_location (expr, loc); |
17817 | |
17818 | return expr; |
17819 | } |
17820 | |
17821 | /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted |
17822 | initializer, DECL is the substituted VAR_DECL. Other arguments are as |
17823 | for tsubst. */ |
17824 | |
17825 | static tree |
17826 | tsubst_init (tree init, tree decl, tree args, |
17827 | tsubst_flags_t complain, tree in_decl) |
17828 | { |
17829 | if (!init) |
17830 | return NULL_TREE; |
17831 | |
17832 | init = tsubst_expr (init, args, complain, in_decl); |
17833 | |
17834 | tree type = TREE_TYPE (decl); |
17835 | |
17836 | if (!init && type != error_mark_node) |
17837 | { |
17838 | if (tree auto_node = type_uses_auto (type)) |
17839 | { |
17840 | if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node)) |
17841 | { |
17842 | if (complain & tf_error) |
17843 | error ("initializer for %q#D expands to an empty list " |
17844 | "of expressions", decl); |
17845 | return error_mark_node; |
17846 | } |
17847 | } |
17848 | else if (!dependent_type_p (type)) |
17849 | { |
17850 | /* If we had an initializer but it |
17851 | instantiated to nothing, |
17852 | value-initialize the object. This will |
17853 | only occur when the initializer was a |
17854 | pack expansion where the parameter packs |
17855 | used in that expansion were of length |
17856 | zero. */ |
17857 | init = build_value_init (type, complain); |
17858 | if (TREE_CODE (init) == AGGR_INIT_EXPR) |
17859 | init = get_target_expr (init, complain); |
17860 | if (TREE_CODE (init) == TARGET_EXPR) |
17861 | TARGET_EXPR_DIRECT_INIT_P (init) = true; |
17862 | } |
17863 | } |
17864 | |
17865 | return init; |
17866 | } |
17867 | |
17868 | /* If T is a reference to a dependent member of the current instantiation C and |
17869 | we are trying to refer to that member in a partial instantiation of C, |
17870 | return a SCOPE_REF; otherwise, return NULL_TREE. |
17871 | |
17872 | This can happen when forming a C++17 deduction guide, as in PR96199. */ |
17873 | |
17874 | static tree |
17875 | maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain, |
17876 | tree in_decl) |
17877 | { |
17878 | if (!(complain & tf_dguide)) |
17879 | return NULL_TREE; |
17880 | |
17881 | tree decl = (t && TYPE_P (t)) ? TYPE_NAME (t) : t; |
17882 | if (!decl || !DECL_P (decl)) |
17883 | return NULL_TREE; |
17884 | |
17885 | tree ctx = context_for_name_lookup (decl); |
17886 | if (!CLASS_TYPE_P (ctx)) |
17887 | return NULL_TREE; |
17888 | |
17889 | ctx = tsubst (t: ctx, args, complain, in_decl); |
17890 | if (!dependent_scope_p (ctx)) |
17891 | return NULL_TREE; |
17892 | |
17893 | if (TYPE_P (t)) |
17894 | { |
17895 | bool stripped = false; |
17896 | if (typedef_variant_p (type: t)) |
17897 | { |
17898 | /* Since this transformation may undesirably turn a deduced context |
17899 | into a non-deduced one, we'd rather strip typedefs than perform |
17900 | the transformation. */ |
17901 | tree u = strip_typedefs (t); |
17902 | if (u != t) |
17903 | { |
17904 | stripped = true; |
17905 | t = u; |
17906 | } |
17907 | } |
17908 | decl = TYPE_NAME (t); |
17909 | if (decl) |
17910 | decl = maybe_dependent_member_ref (t: decl, args, complain, in_decl); |
17911 | if (!decl) |
17912 | { |
17913 | if (stripped) |
17914 | /* The original type was an alias from the current instantiation |
17915 | which we stripped to something outside it. At this point we |
17916 | need to commit to using the stripped type rather than deferring |
17917 | to the caller (which would use the original type), to ensure |
17918 | eligible bits of the stripped type get transformed. */ |
17919 | return tsubst (t, args, complain, in_decl); |
17920 | else |
17921 | /* The original type wasn't a typedef, and we decided it doesn't |
17922 | need rewriting, so just let the caller (tsubst) substitute it |
17923 | normally. */ |
17924 | return NULL_TREE; |
17925 | } |
17926 | return cp_build_qualified_type (TREE_TYPE (decl), cp_type_quals (t), |
17927 | complain); |
17928 | } |
17929 | |
17930 | tree name = DECL_NAME (t); |
17931 | tree fullname = name; |
17932 | if (instantiates_primary_template_p (node: t)) |
17933 | { |
17934 | tree tinfo = get_template_info (t); |
17935 | name = DECL_NAME (TI_TEMPLATE (tinfo)); |
17936 | tree targs = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)); |
17937 | targs = tsubst_template_args (t: targs, args, complain, in_decl); |
17938 | fullname = build_nt (TEMPLATE_ID_EXPR, name, targs); |
17939 | } |
17940 | |
17941 | if (TREE_CODE (t) == TYPE_DECL) |
17942 | { |
17943 | if (!is_typedef_decl (x: t) |
17944 | && TREE_CODE (TREE_TYPE (t)) == TYPENAME_TYPE |
17945 | && TYPE_NAME (TREE_TYPE (t)) == t) |
17946 | /* The TYPE_DECL for a typename has DECL_CONTEXT of the typename |
17947 | scope, but it doesn't need to be rewritten again. */ |
17948 | return NULL_TREE; |
17949 | tree type = build_typename_type (ctx, name, fullname, typename_type); |
17950 | return TYPE_NAME (type); |
17951 | } |
17952 | else if (DECL_TYPE_TEMPLATE_P (t)) |
17953 | return make_unbound_class_template (ctx, name, |
17954 | NULL_TREE, complain); |
17955 | else |
17956 | return build_qualified_name (NULL_TREE, ctx, fullname, |
17957 | TREE_CODE (t) == TEMPLATE_DECL); |
17958 | } |
17959 | |
17960 | /* Helper function for tsubst_omp_clauses, used for instantiation of |
17961 | OMP_CLAUSE_DECL of clauses. */ |
17962 | |
17963 | static tree |
17964 | tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain, |
17965 | tree in_decl, tree *iterator_cache) |
17966 | { |
17967 | if (decl == NULL_TREE || decl == ridpointers[RID_OMP_ALL_MEMORY]) |
17968 | return decl; |
17969 | |
17970 | /* Handle OpenMP iterators. */ |
17971 | if (TREE_CODE (decl) == TREE_LIST |
17972 | && TREE_PURPOSE (decl) |
17973 | && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC) |
17974 | { |
17975 | tree ret; |
17976 | if (iterator_cache[0] == TREE_PURPOSE (decl)) |
17977 | ret = iterator_cache[1]; |
17978 | else |
17979 | { |
17980 | tree *tp = &ret; |
17981 | begin_scope (sk_omp, NULL); |
17982 | for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it)) |
17983 | { |
17984 | *tp = copy_node (it); |
17985 | TREE_VEC_ELT (*tp, 0) |
17986 | = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain); |
17987 | DECL_CONTEXT (TREE_VEC_ELT (*tp, 0)) = current_function_decl; |
17988 | pushdecl (TREE_VEC_ELT (*tp, 0)); |
17989 | TREE_VEC_ELT (*tp, 1) |
17990 | = tsubst_stmt (TREE_VEC_ELT (it, 1), args, complain, in_decl); |
17991 | TREE_VEC_ELT (*tp, 2) |
17992 | = tsubst_stmt (TREE_VEC_ELT (it, 2), args, complain, in_decl); |
17993 | TREE_VEC_ELT (*tp, 3) |
17994 | = tsubst_stmt (TREE_VEC_ELT (it, 3), args, complain, in_decl); |
17995 | TREE_CHAIN (*tp) = NULL_TREE; |
17996 | tp = &TREE_CHAIN (*tp); |
17997 | } |
17998 | TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0); |
17999 | iterator_cache[0] = TREE_PURPOSE (decl); |
18000 | iterator_cache[1] = ret; |
18001 | } |
18002 | return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl), |
18003 | args, complain, |
18004 | in_decl, NULL)); |
18005 | } |
18006 | |
18007 | /* Handle an OpenMP array section represented as a TREE_LIST (or |
18008 | OMP_CLAUSE_DOACROSS_KIND). An OMP_CLAUSE_DOACROSS (with a depend |
18009 | kind of OMP_CLAUSE_DOACROSS_SINK) can also be represented as a |
18010 | TREE_LIST. We can handle it exactly the same as an array section |
18011 | (purpose, value, and a chain), even though the nomenclature |
18012 | (low_bound, length, etc) is different. */ |
18013 | if (TREE_CODE (decl) == TREE_LIST) |
18014 | { |
18015 | tree low_bound |
18016 | = tsubst_stmt (TREE_PURPOSE (decl), args, complain, in_decl); |
18017 | tree length = tsubst_stmt (TREE_VALUE (decl), args, complain, in_decl); |
18018 | tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain, |
18019 | in_decl, NULL); |
18020 | if (TREE_PURPOSE (decl) == low_bound |
18021 | && TREE_VALUE (decl) == length |
18022 | && TREE_CHAIN (decl) == chain) |
18023 | return decl; |
18024 | tree ret = tree_cons (low_bound, length, chain); |
18025 | OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (ret) |
18026 | = OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (decl); |
18027 | return ret; |
18028 | } |
18029 | else if (TREE_CODE (decl) == OMP_ARRAY_SECTION) |
18030 | { |
18031 | tree low_bound |
18032 | = tsubst_stmt (TREE_OPERAND (decl, 1), args, complain, in_decl); |
18033 | tree length = tsubst_stmt (TREE_OPERAND (decl, 2), args, complain, |
18034 | in_decl); |
18035 | tree base = tsubst_omp_clause_decl (TREE_OPERAND (decl, 0), args, |
18036 | complain, in_decl, NULL); |
18037 | if (TREE_OPERAND (decl, 0) == base |
18038 | && TREE_OPERAND (decl, 1) == low_bound |
18039 | && TREE_OPERAND (decl, 2) == length) |
18040 | return decl; |
18041 | return build3 (OMP_ARRAY_SECTION, TREE_TYPE (base), base, low_bound, |
18042 | length); |
18043 | } |
18044 | tree ret = tsubst_stmt (decl, args, complain, in_decl); |
18045 | /* Undo convert_from_reference tsubst_expr could have called. */ |
18046 | if (decl |
18047 | && REFERENCE_REF_P (ret) |
18048 | && !REFERENCE_REF_P (decl)) |
18049 | ret = TREE_OPERAND (ret, 0); |
18050 | return ret; |
18051 | } |
18052 | |
18053 | /* Like tsubst_copy, but specifically for OpenMP clauses. */ |
18054 | |
18055 | static tree |
18056 | tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort, |
18057 | tree args, tsubst_flags_t complain, tree in_decl) |
18058 | { |
18059 | tree new_clauses = NULL_TREE, nc, oc; |
18060 | tree linear_no_step = NULL_TREE; |
18061 | tree iterator_cache[2] = { NULL_TREE, NULL_TREE }; |
18062 | |
18063 | for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc)) |
18064 | { |
18065 | nc = copy_node (oc); |
18066 | OMP_CLAUSE_CHAIN (nc) = new_clauses; |
18067 | new_clauses = nc; |
18068 | |
18069 | switch (OMP_CLAUSE_CODE (nc)) |
18070 | { |
18071 | case OMP_CLAUSE_LASTPRIVATE: |
18072 | if (OMP_CLAUSE_LASTPRIVATE_STMT (oc)) |
18073 | { |
18074 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list (); |
18075 | tsubst_stmt (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, |
18076 | complain, in_decl); |
18077 | OMP_CLAUSE_LASTPRIVATE_STMT (nc) |
18078 | = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc)); |
18079 | } |
18080 | /* FALLTHRU */ |
18081 | case OMP_CLAUSE_PRIVATE: |
18082 | case OMP_CLAUSE_SHARED: |
18083 | case OMP_CLAUSE_FIRSTPRIVATE: |
18084 | case OMP_CLAUSE_COPYIN: |
18085 | case OMP_CLAUSE_COPYPRIVATE: |
18086 | case OMP_CLAUSE_UNIFORM: |
18087 | case OMP_CLAUSE_DEPEND: |
18088 | case OMP_CLAUSE_DOACROSS: |
18089 | case OMP_CLAUSE_AFFINITY: |
18090 | case OMP_CLAUSE_FROM: |
18091 | case OMP_CLAUSE_TO: |
18092 | case OMP_CLAUSE_MAP: |
18093 | case OMP_CLAUSE__CACHE_: |
18094 | case OMP_CLAUSE_NONTEMPORAL: |
18095 | case OMP_CLAUSE_USE_DEVICE_PTR: |
18096 | case OMP_CLAUSE_USE_DEVICE_ADDR: |
18097 | case OMP_CLAUSE_IS_DEVICE_PTR: |
18098 | case OMP_CLAUSE_HAS_DEVICE_ADDR: |
18099 | case OMP_CLAUSE_INCLUSIVE: |
18100 | case OMP_CLAUSE_EXCLUSIVE: |
18101 | OMP_CLAUSE_DECL (nc) |
18102 | = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, |
18103 | in_decl, iterator_cache); |
18104 | break; |
18105 | case OMP_CLAUSE_NUM_TEAMS: |
18106 | if (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc)) |
18107 | OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (nc) |
18108 | = tsubst_stmt (OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (oc), args, |
18109 | complain, in_decl); |
18110 | /* FALLTHRU */ |
18111 | case OMP_CLAUSE_TILE: |
18112 | case OMP_CLAUSE_IF: |
18113 | case OMP_CLAUSE_SELF: |
18114 | case OMP_CLAUSE_NUM_THREADS: |
18115 | case OMP_CLAUSE_SCHEDULE: |
18116 | case OMP_CLAUSE_COLLAPSE: |
18117 | case OMP_CLAUSE_FINAL: |
18118 | case OMP_CLAUSE_DEVICE: |
18119 | case OMP_CLAUSE_DIST_SCHEDULE: |
18120 | case OMP_CLAUSE_THREAD_LIMIT: |
18121 | case OMP_CLAUSE_SAFELEN: |
18122 | case OMP_CLAUSE_SIMDLEN: |
18123 | case OMP_CLAUSE_NUM_TASKS: |
18124 | case OMP_CLAUSE_GRAINSIZE: |
18125 | case OMP_CLAUSE_PRIORITY: |
18126 | case OMP_CLAUSE_ORDERED: |
18127 | case OMP_CLAUSE_HINT: |
18128 | case OMP_CLAUSE_FILTER: |
18129 | case OMP_CLAUSE_NUM_GANGS: |
18130 | case OMP_CLAUSE_NUM_WORKERS: |
18131 | case OMP_CLAUSE_VECTOR_LENGTH: |
18132 | case OMP_CLAUSE_WORKER: |
18133 | case OMP_CLAUSE_VECTOR: |
18134 | case OMP_CLAUSE_ASYNC: |
18135 | case OMP_CLAUSE_WAIT: |
18136 | case OMP_CLAUSE_DETACH: |
18137 | OMP_CLAUSE_OPERAND (nc, 0) |
18138 | = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl); |
18139 | break; |
18140 | case OMP_CLAUSE_PARTIAL: |
18141 | OMP_CLAUSE_PARTIAL_EXPR (nc) |
18142 | = tsubst_expr (OMP_CLAUSE_PARTIAL_EXPR (oc), args, complain, |
18143 | in_decl); |
18144 | break; |
18145 | case OMP_CLAUSE_SIZES: |
18146 | OMP_CLAUSE_SIZES_LIST (nc) |
18147 | = tsubst_expr (OMP_CLAUSE_SIZES_LIST (oc), args, complain, |
18148 | in_decl); |
18149 | break; |
18150 | case OMP_CLAUSE_NOCONTEXT: |
18151 | OMP_CLAUSE_NOCONTEXT_EXPR (nc) |
18152 | = tsubst_expr (OMP_CLAUSE_NOCONTEXT_EXPR (oc), args, complain, |
18153 | in_decl); |
18154 | break; |
18155 | case OMP_CLAUSE_NOVARIANTS: |
18156 | OMP_CLAUSE_NOVARIANTS_EXPR (nc) |
18157 | = tsubst_expr (OMP_CLAUSE_NOVARIANTS_EXPR (oc), args, complain, |
18158 | in_decl); |
18159 | break; |
18160 | case OMP_CLAUSE_REDUCTION: |
18161 | case OMP_CLAUSE_IN_REDUCTION: |
18162 | case OMP_CLAUSE_TASK_REDUCTION: |
18163 | if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc)) |
18164 | { |
18165 | tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc); |
18166 | if (TREE_CODE (placeholder) == SCOPE_REF) |
18167 | { |
18168 | tree scope = tsubst (TREE_OPERAND (placeholder, 0), args, |
18169 | complain, in_decl); |
18170 | OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc) |
18171 | = build_qualified_name (NULL_TREE, scope, |
18172 | TREE_OPERAND (placeholder, 1), |
18173 | false); |
18174 | } |
18175 | else |
18176 | gcc_assert (identifier_p (placeholder)); |
18177 | } |
18178 | OMP_CLAUSE_DECL (nc) |
18179 | = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, |
18180 | in_decl, NULL); |
18181 | break; |
18182 | case OMP_CLAUSE_GANG: |
18183 | case OMP_CLAUSE_ALIGNED: |
18184 | OMP_CLAUSE_DECL (nc) |
18185 | = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, |
18186 | in_decl, NULL); |
18187 | OMP_CLAUSE_OPERAND (nc, 1) |
18188 | = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl); |
18189 | break; |
18190 | case OMP_CLAUSE_ALLOCATE: |
18191 | OMP_CLAUSE_DECL (nc) |
18192 | = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, |
18193 | in_decl, NULL); |
18194 | OMP_CLAUSE_OPERAND (nc, 1) |
18195 | = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 1), args, complain, in_decl); |
18196 | OMP_CLAUSE_OPERAND (nc, 2) |
18197 | = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 2), args, complain, in_decl); |
18198 | break; |
18199 | case OMP_CLAUSE_LINEAR: |
18200 | OMP_CLAUSE_DECL (nc) |
18201 | = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain, |
18202 | in_decl, NULL); |
18203 | if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE) |
18204 | { |
18205 | gcc_assert (!linear_no_step); |
18206 | linear_no_step = nc; |
18207 | } |
18208 | else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc)) |
18209 | OMP_CLAUSE_LINEAR_STEP (nc) |
18210 | = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args, |
18211 | complain, in_decl, NULL); |
18212 | else |
18213 | OMP_CLAUSE_LINEAR_STEP (nc) |
18214 | = tsubst_stmt (OMP_CLAUSE_LINEAR_STEP (oc), args, |
18215 | complain, in_decl); |
18216 | break; |
18217 | case OMP_CLAUSE_INIT: |
18218 | if (ort == C_ORT_OMP_INTEROP |
18219 | && OMP_CLAUSE_INIT_PREFER_TYPE (nc) |
18220 | && TREE_CODE (OMP_CLAUSE_INIT_PREFER_TYPE (nc)) == TREE_LIST |
18221 | && (OMP_CLAUSE_CHAIN (nc) == NULL_TREE |
18222 | || OMP_CLAUSE_CODE (OMP_CLAUSE_CHAIN (nc) ) != OMP_CLAUSE_INIT |
18223 | || (OMP_CLAUSE_INIT_PREFER_TYPE (nc) |
18224 | != OMP_CLAUSE_INIT_PREFER_TYPE (OMP_CLAUSE_CHAIN (nc) )))) |
18225 | { |
18226 | tree pref_list = OMP_CLAUSE_INIT_PREFER_TYPE (nc); |
18227 | tree fr_list = TREE_VALUE (pref_list); |
18228 | int len = TREE_VEC_LENGTH (fr_list); |
18229 | for (int i = 0; i < len; i++) |
18230 | { |
18231 | tree *fr_expr = &TREE_VEC_ELT (fr_list, i); |
18232 | /* Preserve NOP_EXPR to have a location. */ |
18233 | if (*fr_expr && TREE_CODE (*fr_expr) == NOP_EXPR) |
18234 | TREE_OPERAND (*fr_expr, 0) |
18235 | = tsubst_expr (TREE_OPERAND (*fr_expr, 0), args, complain, |
18236 | in_decl); |
18237 | else |
18238 | *fr_expr = tsubst_expr (*fr_expr, args, complain, in_decl); |
18239 | } |
18240 | } |
18241 | /* FALLTHRU */ |
18242 | case OMP_CLAUSE_DESTROY: |
18243 | case OMP_CLAUSE_USE: |
18244 | case OMP_CLAUSE_INTEROP: |
18245 | OMP_CLAUSE_OPERAND (nc, 0) |
18246 | = tsubst_stmt (OMP_CLAUSE_OPERAND (oc, 0), args, complain, in_decl); |
18247 | break; |
18248 | case OMP_CLAUSE_NOWAIT: |
18249 | case OMP_CLAUSE_DEFAULT: |
18250 | case OMP_CLAUSE_UNTIED: |
18251 | case OMP_CLAUSE_MERGEABLE: |
18252 | case OMP_CLAUSE_INBRANCH: |
18253 | case OMP_CLAUSE_NOTINBRANCH: |
18254 | case OMP_CLAUSE_PROC_BIND: |
18255 | case OMP_CLAUSE_FOR: |
18256 | case OMP_CLAUSE_PARALLEL: |
18257 | case OMP_CLAUSE_SECTIONS: |
18258 | case OMP_CLAUSE_TASKGROUP: |
18259 | case OMP_CLAUSE_NOGROUP: |
18260 | case OMP_CLAUSE_THREADS: |
18261 | case OMP_CLAUSE_SIMD: |
18262 | case OMP_CLAUSE_DEFAULTMAP: |
18263 | case OMP_CLAUSE_ORDER: |
18264 | case OMP_CLAUSE_BIND: |
18265 | case OMP_CLAUSE_INDEPENDENT: |
18266 | case OMP_CLAUSE_AUTO: |
18267 | case OMP_CLAUSE_SEQ: |
18268 | case OMP_CLAUSE_IF_PRESENT: |
18269 | case OMP_CLAUSE_FINALIZE: |
18270 | case OMP_CLAUSE_NOHOST: |
18271 | case OMP_CLAUSE_FULL: |
18272 | break; |
18273 | default: |
18274 | gcc_unreachable (); |
18275 | } |
18276 | if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP) |
18277 | switch (OMP_CLAUSE_CODE (nc)) |
18278 | { |
18279 | case OMP_CLAUSE_SHARED: |
18280 | case OMP_CLAUSE_PRIVATE: |
18281 | case OMP_CLAUSE_FIRSTPRIVATE: |
18282 | case OMP_CLAUSE_LASTPRIVATE: |
18283 | case OMP_CLAUSE_COPYPRIVATE: |
18284 | case OMP_CLAUSE_LINEAR: |
18285 | case OMP_CLAUSE_REDUCTION: |
18286 | case OMP_CLAUSE_IN_REDUCTION: |
18287 | case OMP_CLAUSE_TASK_REDUCTION: |
18288 | case OMP_CLAUSE_USE_DEVICE_PTR: |
18289 | case OMP_CLAUSE_USE_DEVICE_ADDR: |
18290 | case OMP_CLAUSE_IS_DEVICE_PTR: |
18291 | case OMP_CLAUSE_HAS_DEVICE_ADDR: |
18292 | case OMP_CLAUSE_INCLUSIVE: |
18293 | case OMP_CLAUSE_EXCLUSIVE: |
18294 | case OMP_CLAUSE_ALLOCATE: |
18295 | /* tsubst_expr on SCOPE_REF results in returning |
18296 | finish_non_static_data_member result. Undo that here. */ |
18297 | if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF |
18298 | && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1)) |
18299 | == IDENTIFIER_NODE)) |
18300 | { |
18301 | tree t = OMP_CLAUSE_DECL (nc); |
18302 | tree v = t; |
18303 | while (v) |
18304 | switch (TREE_CODE (v)) |
18305 | { |
18306 | case COMPONENT_REF: |
18307 | case MEM_REF: |
18308 | case INDIRECT_REF: |
18309 | CASE_CONVERT: |
18310 | case POINTER_PLUS_EXPR: |
18311 | v = TREE_OPERAND (v, 0); |
18312 | continue; |
18313 | case PARM_DECL: |
18314 | if (DECL_CONTEXT (v) == current_function_decl |
18315 | && DECL_ARTIFICIAL (v) |
18316 | && DECL_NAME (v) == this_identifier) |
18317 | OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1); |
18318 | /* FALLTHRU */ |
18319 | default: |
18320 | v = NULL_TREE; |
18321 | break; |
18322 | } |
18323 | } |
18324 | else if (VAR_P (OMP_CLAUSE_DECL (oc)) |
18325 | && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc)) |
18326 | && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc)) |
18327 | && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc)) |
18328 | && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc))) |
18329 | { |
18330 | tree decl = OMP_CLAUSE_DECL (nc); |
18331 | if (VAR_P (decl)) |
18332 | { |
18333 | retrofit_lang_decl (decl); |
18334 | DECL_OMP_PRIVATIZED_MEMBER (decl) = 1; |
18335 | } |
18336 | } |
18337 | break; |
18338 | default: |
18339 | break; |
18340 | } |
18341 | } |
18342 | |
18343 | new_clauses = nreverse (new_clauses); |
18344 | if (ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_OMP_DECLARE_MAPPER) |
18345 | { |
18346 | if (ort == C_ORT_OMP_TARGET) |
18347 | new_clauses = c_omp_instantiate_mappers (new_clauses); |
18348 | new_clauses = finish_omp_clauses (new_clauses, ort); |
18349 | if (linear_no_step) |
18350 | for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc)) |
18351 | if (nc == linear_no_step) |
18352 | { |
18353 | OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE; |
18354 | break; |
18355 | } |
18356 | } |
18357 | return new_clauses; |
18358 | } |
18359 | |
18360 | /* Like tsubst_copy, but specifically for OpenMP context selectors. */ |
18361 | static tree |
18362 | tsubst_omp_context_selector (tree ctx, tree args, tsubst_flags_t complain, |
18363 | tree in_decl) |
18364 | { |
18365 | tree new_ctx = NULL_TREE; |
18366 | for (tree set = ctx; set; set = TREE_CHAIN (set)) |
18367 | { |
18368 | tree selectors = NULL_TREE; |
18369 | for (tree sel = OMP_TSS_TRAIT_SELECTORS (set); sel; |
18370 | sel = TREE_CHAIN (sel)) |
18371 | { |
18372 | enum omp_ts_code code = OMP_TS_CODE (sel); |
18373 | tree properties = NULL_TREE; |
18374 | tree score = OMP_TS_SCORE (sel); |
18375 | tree t; |
18376 | |
18377 | if (score) |
18378 | { |
18379 | score = tsubst_expr (score, args, complain, in_decl); |
18380 | score = fold_non_dependent_expr (score); |
18381 | if (!value_dependent_expression_p (score) |
18382 | && !type_dependent_expression_p (score)) |
18383 | { |
18384 | if (!INTEGRAL_TYPE_P (TREE_TYPE (score)) |
18385 | || TREE_CODE (score) != INTEGER_CST) |
18386 | { |
18387 | error_at (cp_expr_loc_or_input_loc (t: score), |
18388 | "%<score%> argument must " |
18389 | "be constant integer expression"); |
18390 | score = NULL_TREE; |
18391 | } |
18392 | else if (tree_int_cst_sgn (score) < 0) |
18393 | { |
18394 | error_at (cp_expr_loc_or_input_loc (t: score), |
18395 | "%<score%> argument must be non-negative"); |
18396 | score = NULL_TREE; |
18397 | } |
18398 | } |
18399 | } |
18400 | |
18401 | enum omp_tp_type property_kind |
18402 | = omp_ts_map[OMP_TS_CODE (sel)].tp_type; |
18403 | switch (property_kind) |
18404 | { |
18405 | case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR: |
18406 | case OMP_TRAIT_PROPERTY_BOOL_EXPR: |
18407 | t = tsubst_expr (OMP_TP_VALUE (OMP_TS_PROPERTIES (sel)), |
18408 | args, complain, in_decl); |
18409 | t = fold_non_dependent_expr (t); |
18410 | if (!value_dependent_expression_p (t) |
18411 | && !type_dependent_expression_p (t)) |
18412 | { |
18413 | if (property_kind == OMP_TRAIT_PROPERTY_BOOL_EXPR) |
18414 | t = maybe_convert_cond (t); |
18415 | else |
18416 | { |
18417 | t = convert_from_reference (t); |
18418 | if (!INTEGRAL_TYPE_P (TREE_TYPE (t))) |
18419 | { |
18420 | error_at (cp_expr_loc_or_input_loc (t), |
18421 | "property must be integer expression"); |
18422 | t = error_mark_node; |
18423 | } |
18424 | } |
18425 | } |
18426 | if (t != error_mark_node |
18427 | && !processing_template_decl |
18428 | && TREE_CODE (t) != CLEANUP_POINT_EXPR) |
18429 | t = fold_build_cleanup_point_expr (TREE_TYPE (t), expr: t); |
18430 | properties = make_trait_property (NULL_TREE, t, NULL_TREE); |
18431 | break; |
18432 | case OMP_TRAIT_PROPERTY_CLAUSE_LIST: |
18433 | if (OMP_TS_CODE (sel) == OMP_TRAIT_CONSTRUCT_SIMD) |
18434 | properties = tsubst_omp_clauses (OMP_TS_PROPERTIES (sel), |
18435 | ort: C_ORT_OMP_DECLARE_SIMD, |
18436 | args, complain, in_decl); |
18437 | break; |
18438 | default: |
18439 | /* Nothing to do here, just copy. */ |
18440 | for (tree prop = OMP_TS_PROPERTIES (sel); |
18441 | prop; prop = TREE_CHAIN (prop)) |
18442 | properties = make_trait_property (OMP_TP_NAME (prop), |
18443 | OMP_TP_VALUE (prop), |
18444 | properties); |
18445 | } |
18446 | selectors = make_trait_selector (code, score, properties, selectors); |
18447 | } |
18448 | new_ctx = make_trait_set_selector (OMP_TSS_CODE (set), |
18449 | nreverse (selectors), |
18450 | new_ctx); |
18451 | } |
18452 | return nreverse (new_ctx); |
18453 | } |
18454 | |
18455 | |
18456 | /* Like tsubst_expr, but unshare TREE_LIST nodes. */ |
18457 | |
18458 | static tree |
18459 | tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain, |
18460 | tree in_decl) |
18461 | { |
18462 | #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl) |
18463 | |
18464 | tree purpose, value, chain; |
18465 | |
18466 | if (t == NULL) |
18467 | return t; |
18468 | |
18469 | if (TREE_CODE (t) != TREE_LIST) |
18470 | return tsubst_expr (t, args, complain, in_decl); |
18471 | |
18472 | if (t == void_list_node) |
18473 | return t; |
18474 | |
18475 | purpose = TREE_PURPOSE (t); |
18476 | if (purpose) |
18477 | purpose = RECUR (purpose); |
18478 | value = TREE_VALUE (t); |
18479 | if (value) |
18480 | { |
18481 | if (TREE_CODE (value) != LABEL_DECL) |
18482 | value = RECUR (value); |
18483 | else |
18484 | { |
18485 | value = lookup_label (DECL_NAME (value)); |
18486 | gcc_assert (TREE_CODE (value) == LABEL_DECL); |
18487 | TREE_USED (value) = 1; |
18488 | } |
18489 | } |
18490 | chain = TREE_CHAIN (t); |
18491 | if (chain && chain != void_type_node) |
18492 | chain = RECUR (chain); |
18493 | return tree_cons (purpose, value, chain); |
18494 | #undef RECUR |
18495 | } |
18496 | |
18497 | /* Used to temporarily communicate the list of #pragma omp parallel |
18498 | clauses to #pragma omp for instantiation if they are combined |
18499 | together. */ |
18500 | |
18501 | static tree *omp_parallel_combined_clauses; |
18502 | |
18503 | static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree, |
18504 | cp_decomp *); |
18505 | |
18506 | /* Substitute one OMP_FOR iterator. */ |
18507 | |
18508 | static bool |
18509 | tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv, |
18510 | tree initv, tree condv, tree incrv, tree *clauses, |
18511 | tree args, tsubst_flags_t complain, tree in_decl) |
18512 | { |
18513 | #define RECUR(NODE) \ |
18514 | tsubst_stmt ((NODE), args, complain, in_decl) |
18515 | tree decl, init, cond = NULL_TREE, incr = NULL_TREE; |
18516 | bool ret = false; |
18517 | |
18518 | init = TREE_VEC_ELT (OMP_FOR_INIT (t), i); |
18519 | gcc_assert (TREE_CODE (init) == MODIFY_EXPR); |
18520 | |
18521 | decl = TREE_OPERAND (init, 0); |
18522 | init = TREE_OPERAND (init, 1); |
18523 | tree decl_expr = NULL_TREE; |
18524 | bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace; |
18525 | if (range_for) |
18526 | { |
18527 | bool decomp = false; |
18528 | if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl)) |
18529 | { |
18530 | tree v = DECL_VALUE_EXPR (decl); |
18531 | if (TREE_CODE (v) == ARRAY_REF |
18532 | && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0))) |
18533 | { |
18534 | cp_decomp decomp_d = { NULL_TREE, .count: 0 }; |
18535 | tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain); |
18536 | maybe_push_decl (d); |
18537 | d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain, |
18538 | in_decl, &decomp_d); |
18539 | decomp = true; |
18540 | if (d == error_mark_node) |
18541 | decl = error_mark_node; |
18542 | else |
18543 | for (unsigned int i = 0; i < decomp_d.count; i++) |
18544 | { |
18545 | if (!DECL_HAS_VALUE_EXPR_P (decomp_d.decl)) |
18546 | { |
18547 | tree v = build_nt (ARRAY_REF, d, |
18548 | size_int (decomp_d.count - i - 1), |
18549 | NULL_TREE, NULL_TREE); |
18550 | SET_DECL_VALUE_EXPR (decomp_d.decl, v); |
18551 | DECL_HAS_VALUE_EXPR_P (decomp_d.decl) = 1; |
18552 | } |
18553 | fit_decomposition_lang_decl (decomp_d.decl, d); |
18554 | decomp_d.decl = DECL_CHAIN (decomp_d.decl); |
18555 | } |
18556 | } |
18557 | } |
18558 | decl = tsubst_decl (t: decl, args, complain); |
18559 | if (!decomp) |
18560 | maybe_push_decl (decl); |
18561 | } |
18562 | else if (init && TREE_CODE (init) == DECL_EXPR) |
18563 | { |
18564 | /* We need to jump through some hoops to handle declarations in the |
18565 | init-statement, since we might need to handle auto deduction, |
18566 | but we need to keep control of initialization. */ |
18567 | decl_expr = init; |
18568 | init = DECL_INITIAL (DECL_EXPR_DECL (init)); |
18569 | decl = tsubst_decl (t: decl, args, complain); |
18570 | } |
18571 | else |
18572 | { |
18573 | if (TREE_CODE (decl) == SCOPE_REF) |
18574 | { |
18575 | decl = RECUR (decl); |
18576 | if (TREE_CODE (decl) == COMPONENT_REF) |
18577 | { |
18578 | tree v = decl; |
18579 | while (v) |
18580 | switch (TREE_CODE (v)) |
18581 | { |
18582 | case COMPONENT_REF: |
18583 | case MEM_REF: |
18584 | case INDIRECT_REF: |
18585 | CASE_CONVERT: |
18586 | case POINTER_PLUS_EXPR: |
18587 | v = TREE_OPERAND (v, 0); |
18588 | continue; |
18589 | case PARM_DECL: |
18590 | if (DECL_CONTEXT (v) == current_function_decl |
18591 | && DECL_ARTIFICIAL (v) |
18592 | && DECL_NAME (v) == this_identifier) |
18593 | { |
18594 | decl = TREE_OPERAND (decl, 1); |
18595 | decl = omp_privatize_field (decl, false); |
18596 | } |
18597 | /* FALLTHRU */ |
18598 | default: |
18599 | v = NULL_TREE; |
18600 | break; |
18601 | } |
18602 | } |
18603 | } |
18604 | else |
18605 | decl = RECUR (decl); |
18606 | } |
18607 | if (init && TREE_CODE (init) == TREE_VEC) |
18608 | { |
18609 | init = copy_node (init); |
18610 | TREE_VEC_ELT (init, 0) |
18611 | = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain); |
18612 | TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1)); |
18613 | TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2)); |
18614 | } |
18615 | else |
18616 | init = RECUR (init); |
18617 | |
18618 | if (orig_declv && OMP_FOR_ORIG_DECLS (t)) |
18619 | { |
18620 | tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i); |
18621 | if (TREE_CODE (o) == TREE_LIST) |
18622 | TREE_VEC_ELT (orig_declv, i) |
18623 | = tree_cons (RECUR (TREE_PURPOSE (o)), |
18624 | RECUR (TREE_VALUE (o)), |
18625 | NULL_TREE); |
18626 | else |
18627 | TREE_VEC_ELT (orig_declv, i) = RECUR (o); |
18628 | } |
18629 | |
18630 | if (range_for) |
18631 | { |
18632 | tree this_pre_body = NULL_TREE; |
18633 | tree orig_init = NULL_TREE; |
18634 | tree orig_decl = NULL_TREE; |
18635 | tree init_sl = NULL_TREE; |
18636 | cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init, |
18637 | orig_init, cond, incr, true); |
18638 | if (orig_decl) |
18639 | { |
18640 | if (orig_declv == NULL_TREE) |
18641 | orig_declv = copy_node (declv); |
18642 | TREE_VEC_ELT (orig_declv, i) = orig_decl; |
18643 | ret = true; |
18644 | } |
18645 | else if (orig_declv) |
18646 | TREE_VEC_ELT (orig_declv, i) = decl; |
18647 | } |
18648 | |
18649 | tree auto_node = type_uses_auto (TREE_TYPE (decl)); |
18650 | if (!range_for && auto_node && init) |
18651 | TREE_TYPE (decl) |
18652 | = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain); |
18653 | |
18654 | gcc_assert (!type_dependent_expression_p (decl)); |
18655 | |
18656 | if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for) |
18657 | { |
18658 | if (decl_expr) |
18659 | { |
18660 | /* Declare the variable, but don't let that initialize it. */ |
18661 | tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr)); |
18662 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE; |
18663 | RECUR (decl_expr); |
18664 | DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav; |
18665 | } |
18666 | |
18667 | if (!range_for) |
18668 | { |
18669 | cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); |
18670 | if (COMPARISON_CLASS_P (cond) |
18671 | && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC) |
18672 | { |
18673 | tree lhs = RECUR (TREE_OPERAND (cond, 0)); |
18674 | tree rhs = copy_node (TREE_OPERAND (cond, 1)); |
18675 | TREE_VEC_ELT (rhs, 0) |
18676 | = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain); |
18677 | TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1)); |
18678 | TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2)); |
18679 | cond = build2 (TREE_CODE (cond), TREE_TYPE (cond), |
18680 | lhs, rhs); |
18681 | } |
18682 | else |
18683 | cond = RECUR (cond); |
18684 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); |
18685 | if (TREE_CODE (incr) == MODIFY_EXPR) |
18686 | { |
18687 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); |
18688 | tree rhs = RECUR (TREE_OPERAND (incr, 1)); |
18689 | incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs, |
18690 | NOP_EXPR, rhs, NULL_TREE, complain); |
18691 | } |
18692 | else |
18693 | incr = RECUR (incr); |
18694 | if (orig_declv && !OMP_FOR_ORIG_DECLS (t)) |
18695 | TREE_VEC_ELT (orig_declv, i) = decl; |
18696 | } |
18697 | TREE_VEC_ELT (declv, i) = decl; |
18698 | TREE_VEC_ELT (initv, i) = init; |
18699 | TREE_VEC_ELT (condv, i) = cond; |
18700 | TREE_VEC_ELT (incrv, i) = incr; |
18701 | return ret; |
18702 | } |
18703 | |
18704 | if (decl_expr) |
18705 | { |
18706 | /* Declare and initialize the variable. */ |
18707 | RECUR (decl_expr); |
18708 | init = NULL_TREE; |
18709 | } |
18710 | else if (init) |
18711 | { |
18712 | tree *pc; |
18713 | int j; |
18714 | for (j = ((omp_parallel_combined_clauses == NULL |
18715 | || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++) |
18716 | { |
18717 | for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; ) |
18718 | { |
18719 | if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE |
18720 | && OMP_CLAUSE_DECL (*pc) == decl) |
18721 | break; |
18722 | else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE |
18723 | && OMP_CLAUSE_DECL (*pc) == decl) |
18724 | { |
18725 | if (j) |
18726 | break; |
18727 | /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */ |
18728 | tree c = *pc; |
18729 | *pc = OMP_CLAUSE_CHAIN (c); |
18730 | OMP_CLAUSE_CHAIN (c) = *clauses; |
18731 | *clauses = c; |
18732 | } |
18733 | else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE |
18734 | && OMP_CLAUSE_DECL (*pc) == decl) |
18735 | { |
18736 | error ("iteration variable %qD should not be firstprivate", |
18737 | decl); |
18738 | *pc = OMP_CLAUSE_CHAIN (*pc); |
18739 | } |
18740 | else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION |
18741 | && OMP_CLAUSE_DECL (*pc) == decl) |
18742 | { |
18743 | error ("iteration variable %qD should not be reduction", |
18744 | decl); |
18745 | *pc = OMP_CLAUSE_CHAIN (*pc); |
18746 | } |
18747 | else |
18748 | pc = &OMP_CLAUSE_CHAIN (*pc); |
18749 | } |
18750 | if (*pc) |
18751 | break; |
18752 | } |
18753 | if (*pc == NULL_TREE) |
18754 | { |
18755 | tree c = build_omp_clause (input_location, |
18756 | TREE_CODE (t) == OMP_LOOP |
18757 | ? OMP_CLAUSE_LASTPRIVATE |
18758 | : OMP_CLAUSE_PRIVATE); |
18759 | OMP_CLAUSE_DECL (c) = decl; |
18760 | c = finish_omp_clauses (c, C_ORT_OMP); |
18761 | if (c) |
18762 | { |
18763 | OMP_CLAUSE_CHAIN (c) = *clauses; |
18764 | *clauses = c; |
18765 | } |
18766 | } |
18767 | } |
18768 | cond = TREE_VEC_ELT (OMP_FOR_COND (t), i); |
18769 | if (COMPARISON_CLASS_P (cond)) |
18770 | { |
18771 | tree op0 = RECUR (TREE_OPERAND (cond, 0)); |
18772 | tree op1 = RECUR (TREE_OPERAND (cond, 1)); |
18773 | cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1); |
18774 | } |
18775 | else |
18776 | cond = RECUR (cond); |
18777 | incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i); |
18778 | switch (TREE_CODE (incr)) |
18779 | { |
18780 | case PREINCREMENT_EXPR: |
18781 | case PREDECREMENT_EXPR: |
18782 | case POSTINCREMENT_EXPR: |
18783 | case POSTDECREMENT_EXPR: |
18784 | incr = build2 (TREE_CODE (incr), TREE_TYPE (decl), |
18785 | RECUR (TREE_OPERAND (incr, 0)), NULL_TREE); |
18786 | break; |
18787 | case MODIFY_EXPR: |
18788 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR |
18789 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) |
18790 | { |
18791 | tree rhs = TREE_OPERAND (incr, 1); |
18792 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); |
18793 | tree rhs0 = RECUR (TREE_OPERAND (rhs, 0)); |
18794 | tree rhs1 = RECUR (TREE_OPERAND (rhs, 1)); |
18795 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, |
18796 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), |
18797 | rhs0, rhs1)); |
18798 | } |
18799 | else |
18800 | incr = RECUR (incr); |
18801 | break; |
18802 | case MODOP_EXPR: |
18803 | if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR |
18804 | || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR) |
18805 | { |
18806 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); |
18807 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, |
18808 | build2 (TREE_CODE (TREE_OPERAND (incr, 1)), |
18809 | TREE_TYPE (decl), lhs, |
18810 | RECUR (TREE_OPERAND (incr, 2)))); |
18811 | } |
18812 | else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR |
18813 | && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR |
18814 | || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR))) |
18815 | { |
18816 | tree rhs = TREE_OPERAND (incr, 2); |
18817 | tree lhs = RECUR (TREE_OPERAND (incr, 0)); |
18818 | tree rhs0 = RECUR (TREE_OPERAND (rhs, 0)); |
18819 | tree rhs1 = RECUR (TREE_OPERAND (rhs, 1)); |
18820 | incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs, |
18821 | build2 (TREE_CODE (rhs), TREE_TYPE (decl), |
18822 | rhs0, rhs1)); |
18823 | } |
18824 | else |
18825 | incr = RECUR (incr); |
18826 | break; |
18827 | default: |
18828 | incr = RECUR (incr); |
18829 | break; |
18830 | } |
18831 | |
18832 | if (orig_declv && !OMP_FOR_ORIG_DECLS (t)) |
18833 | TREE_VEC_ELT (orig_declv, i) = decl; |
18834 | TREE_VEC_ELT (declv, i) = decl; |
18835 | TREE_VEC_ELT (initv, i) = init; |
18836 | TREE_VEC_ELT (condv, i) = cond; |
18837 | TREE_VEC_ELT (incrv, i) = incr; |
18838 | return false; |
18839 | #undef RECUR |
18840 | } |
18841 | |
18842 | /* Helper function of tsubst_expr, find OMP_TEAMS inside |
18843 | of OMP_TARGET's body. */ |
18844 | |
18845 | static tree |
18846 | tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *) |
18847 | { |
18848 | *walk_subtrees = 0; |
18849 | switch (TREE_CODE (*tp)) |
18850 | { |
18851 | case OMP_TEAMS: |
18852 | return *tp; |
18853 | case BIND_EXPR: |
18854 | case STATEMENT_LIST: |
18855 | *walk_subtrees = 1; |
18856 | break; |
18857 | default: |
18858 | break; |
18859 | } |
18860 | return NULL_TREE; |
18861 | } |
18862 | |
18863 | /* Helper function for tsubst_expr. For decomposition declaration |
18864 | artificial base DECL, which is tsubsted PATTERN_DECL, tsubst |
18865 | also the corresponding decls representing the identifiers |
18866 | of the decomposition declaration. Return DECL if successful |
18867 | or error_mark_node otherwise, set *FIRST to the first decl |
18868 | in the list chained through DECL_CHAIN and *CNT to the number |
18869 | of such decls. */ |
18870 | |
18871 | static tree |
18872 | tsubst_decomp_names (tree decl, tree pattern_decl, tree args, |
18873 | tsubst_flags_t complain, tree in_decl, cp_decomp *decomp) |
18874 | { |
18875 | tree decl2, decl3, prev = decl; |
18876 | decomp->count = 0; |
18877 | gcc_assert (DECL_NAME (decl) == NULL_TREE); |
18878 | for (decl2 = DECL_CHAIN (pattern_decl); |
18879 | decl2 |
18880 | && DECL_DECOMPOSITION_P (decl2) |
18881 | && DECL_NAME (decl2); |
18882 | decl2 = DECL_CHAIN (decl2)) |
18883 | { |
18884 | if (TREE_TYPE (decl2) == error_mark_node && decomp->count == 0) |
18885 | { |
18886 | gcc_assert (errorcount); |
18887 | return error_mark_node; |
18888 | } |
18889 | decomp->count++; |
18890 | gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl); |
18891 | gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2)); |
18892 | tree v = DECL_VALUE_EXPR (decl2); |
18893 | DECL_HAS_VALUE_EXPR_P (decl2) = 0; |
18894 | SET_DECL_VALUE_EXPR (decl2, NULL_TREE); |
18895 | decl3 = tsubst (t: decl2, args, complain, in_decl); |
18896 | SET_DECL_VALUE_EXPR (decl2, v); |
18897 | DECL_HAS_VALUE_EXPR_P (decl2) = 1; |
18898 | if (VAR_P (decl3)) |
18899 | DECL_TEMPLATE_INSTANTIATED (decl3) = 1; |
18900 | else |
18901 | { |
18902 | gcc_assert (errorcount); |
18903 | decl = error_mark_node; |
18904 | continue; |
18905 | } |
18906 | maybe_push_decl (decl3); |
18907 | if (error_operand_p (t: decl3)) |
18908 | decl = error_mark_node; |
18909 | else if (decl != error_mark_node |
18910 | && DECL_CHAIN (decl3) != prev |
18911 | && decl != prev) |
18912 | { |
18913 | gcc_assert (errorcount); |
18914 | decl = error_mark_node; |
18915 | } |
18916 | else |
18917 | prev = decl3; |
18918 | } |
18919 | decomp->decl = prev; |
18920 | return decl; |
18921 | } |
18922 | |
18923 | /* Return the proper local_specialization for init-capture pack DECL. */ |
18924 | |
18925 | static tree |
18926 | lookup_init_capture_pack (tree decl) |
18927 | { |
18928 | /* We handle normal pack captures by forwarding to the specialization of the |
18929 | captured parameter. We can't do that for pack init-captures; we need them |
18930 | to have their own local_specialization. We created the individual |
18931 | VAR_DECLs (if any) under build_capture_proxy, and we need to collect them |
18932 | when we process the DECL_EXPR for the pack init-capture in the template. |
18933 | So, how do we find them? We don't know the capture proxy pack when |
18934 | building the individual resulting proxies, and we don't know the |
18935 | individual proxies when instantiating the pack. What we have in common is |
18936 | the FIELD_DECL. |
18937 | |
18938 | So...when we instantiate the FIELD_DECL, we stick the result in |
18939 | local_specializations. Then at the DECL_EXPR we look up that result, see |
18940 | how many elements it has, synthesize the names, and look them up. */ |
18941 | |
18942 | tree cname = DECL_NAME (decl); |
18943 | tree val = DECL_VALUE_EXPR (decl); |
18944 | tree field = TREE_OPERAND (val, 1); |
18945 | gcc_assert (TREE_CODE (field) == FIELD_DECL); |
18946 | tree fpack = retrieve_local_specialization (tmpl: field); |
18947 | if (fpack == error_mark_node) |
18948 | return error_mark_node; |
18949 | |
18950 | int len = 1; |
18951 | tree vec = NULL_TREE; |
18952 | tree r = NULL_TREE; |
18953 | if (TREE_CODE (fpack) == TREE_VEC) |
18954 | { |
18955 | len = TREE_VEC_LENGTH (fpack); |
18956 | vec = make_tree_vec (len); |
18957 | r = make_node (NONTYPE_ARGUMENT_PACK); |
18958 | ARGUMENT_PACK_ARGS (r) = vec; |
18959 | } |
18960 | for (int i = 0; i < len; ++i) |
18961 | { |
18962 | tree ename = vec ? make_ith_pack_parameter_name (name: cname, i) : cname; |
18963 | tree elt = lookup_name (name: ename); |
18964 | if (vec) |
18965 | TREE_VEC_ELT (vec, i) = elt; |
18966 | else |
18967 | r = elt; |
18968 | } |
18969 | return r; |
18970 | } |
18971 | |
18972 | /* T is an operand of a template tree being substituted. Return whether |
18973 | T is dependent such that we should suppress some warnings that would |
18974 | make sense if the substituted expression were written directly, like |
18975 | template <int I> bool f() { return I == 2; } |
18976 | We don't want to warn when instantiating f that comparing two constants |
18977 | always has the same value. |
18978 | |
18979 | This is a more limited concept of dependence than instantiation-dependent; |
18980 | here we don't care whether substitution could fail. */ |
18981 | |
18982 | static bool |
18983 | dependent_operand_p (tree t) |
18984 | { |
18985 | while (TREE_CODE (t) == IMPLICIT_CONV_EXPR) |
18986 | t = TREE_OPERAND (t, 0); |
18987 | |
18988 | ++processing_template_decl; |
18989 | bool r = (potential_constant_expression (t) |
18990 | ? value_dependent_expression_p (t) |
18991 | : type_dependent_expression_p (t)); |
18992 | --processing_template_decl; |
18993 | return r; |
18994 | } |
18995 | |
18996 | /* A superset of tsubst_expr that also handles statement trees. */ |
18997 | |
18998 | static tree |
18999 | tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
19000 | { |
19001 | #define RETURN(EXP) do { r = (EXP); goto out; } while(0) |
19002 | #define RECUR(NODE) \ |
19003 | tsubst_stmt ((NODE), args, complain, in_decl) |
19004 | |
19005 | tree stmt, tmp; |
19006 | tree r; |
19007 | location_t loc; |
19008 | |
19009 | if (t == NULL_TREE || t == error_mark_node) |
19010 | return t; |
19011 | |
19012 | loc = input_location; |
19013 | if (location_t eloc = cp_expr_location (t_: t)) |
19014 | input_location = eloc; |
19015 | if (STATEMENT_CODE_P (TREE_CODE (t))) |
19016 | current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t); |
19017 | |
19018 | switch (TREE_CODE (t)) |
19019 | { |
19020 | case STATEMENT_LIST: |
19021 | { |
19022 | for (tree stmt : tsi_range (t)) |
19023 | RECUR (stmt); |
19024 | break; |
19025 | } |
19026 | |
19027 | case CTOR_INITIALIZER: |
19028 | finish_mem_initializers (tsubst_initializer_list |
19029 | (TREE_OPERAND (t, 0), args)); |
19030 | break; |
19031 | |
19032 | case RETURN_EXPR: |
19033 | finish_return_stmt (RECUR (TREE_OPERAND (t, 0))); |
19034 | break; |
19035 | |
19036 | case CO_RETURN_EXPR: |
19037 | finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0))); |
19038 | break; |
19039 | |
19040 | case EXPR_STMT: |
19041 | tmp = RECUR (EXPR_STMT_EXPR (t)); |
19042 | if (EXPR_STMT_STMT_EXPR_RESULT (t)) |
19043 | finish_stmt_expr_expr (tmp, cur_stmt_expr); |
19044 | else |
19045 | finish_expr_stmt (tmp); |
19046 | break; |
19047 | |
19048 | case USING_STMT: |
19049 | finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE); |
19050 | break; |
19051 | |
19052 | case PRECONDITION_STMT: |
19053 | case POSTCONDITION_STMT: |
19054 | gcc_unreachable (); |
19055 | |
19056 | case ASSERTION_STMT: |
19057 | { |
19058 | r = tsubst_contract (NULL_TREE, t, args, complain, in_decl); |
19059 | if (r != error_mark_node) |
19060 | add_stmt (r); |
19061 | RETURN (r); |
19062 | } |
19063 | break; |
19064 | |
19065 | case DECL_EXPR: |
19066 | { |
19067 | tree decl, pattern_decl; |
19068 | tree init; |
19069 | |
19070 | pattern_decl = decl = DECL_EXPR_DECL (t); |
19071 | if (TREE_CODE (decl) == LABEL_DECL) |
19072 | finish_label_decl (DECL_NAME (decl)); |
19073 | else if (TREE_CODE (decl) == USING_DECL) |
19074 | { |
19075 | tree scope = USING_DECL_SCOPE (decl); |
19076 | if (DECL_DEPENDENT_P (decl)) |
19077 | { |
19078 | scope = tsubst (t: scope, args, complain, in_decl); |
19079 | if (!MAYBE_CLASS_TYPE_P (scope) |
19080 | && TREE_CODE (scope) != ENUMERAL_TYPE) |
19081 | { |
19082 | if (complain & tf_error) |
19083 | error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a " |
19084 | "class, namespace, or enumeration", scope); |
19085 | return error_mark_node; |
19086 | } |
19087 | finish_nonmember_using_decl (scope, DECL_NAME (decl)); |
19088 | } |
19089 | else |
19090 | { |
19091 | /* This is a non-dependent using-decl, and we'll have |
19092 | used the names it found during template parsing. We do |
19093 | not want to do the lookup again, because we might not |
19094 | find the things we found then. */ |
19095 | gcc_checking_assert (scope == tsubst (scope, args, |
19096 | complain, in_decl)); |
19097 | /* We still need to push the bindings so that we can look up |
19098 | this name later. */ |
19099 | push_using_decl_bindings (DECL_NAME (decl), |
19100 | USING_DECL_DECLS (decl)); |
19101 | } |
19102 | } |
19103 | else if (is_capture_proxy (decl) |
19104 | && !DECL_TEMPLATE_INSTANTIATION (current_function_decl)) |
19105 | { |
19106 | /* We're in tsubst_lambda_expr, we've already inserted a new |
19107 | capture proxy, so look it up and register it. */ |
19108 | tree inst; |
19109 | if (!DECL_PACK_P (decl)) |
19110 | { |
19111 | inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK, |
19112 | LOOK_want::HIDDEN_LAMBDA); |
19113 | gcc_assert (inst != decl && is_capture_proxy (inst)); |
19114 | } |
19115 | else if (is_normal_capture_proxy (decl)) |
19116 | { |
19117 | inst = (retrieve_local_specialization |
19118 | (DECL_CAPTURED_VARIABLE (decl))); |
19119 | gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK |
19120 | || DECL_PACK_P (inst)); |
19121 | } |
19122 | else |
19123 | inst = lookup_init_capture_pack (decl); |
19124 | |
19125 | register_local_specialization (spec: inst, tmpl: decl); |
19126 | break; |
19127 | } |
19128 | else if (DECL_PRETTY_FUNCTION_P (decl)) |
19129 | decl = make_fname_decl (DECL_SOURCE_LOCATION (decl), |
19130 | DECL_NAME (decl), |
19131 | true/*DECL_PRETTY_FUNCTION_P (decl)*/); |
19132 | else if (DECL_IMPLICIT_TYPEDEF_P (decl) |
19133 | && LAMBDA_TYPE_P (TREE_TYPE (decl))) |
19134 | /* Don't copy the old closure; we'll create a new one in |
19135 | tsubst_lambda_expr. */ |
19136 | break; |
19137 | else |
19138 | { |
19139 | init = DECL_INITIAL (decl); |
19140 | decl = tsubst (t: decl, args, complain, in_decl); |
19141 | if (decl != error_mark_node) |
19142 | { |
19143 | /* By marking the declaration as instantiated, we avoid |
19144 | trying to instantiate it. Since instantiate_decl can't |
19145 | handle local variables, and since we've already done |
19146 | all that needs to be done, that's the right thing to |
19147 | do. */ |
19148 | if (VAR_P (decl)) |
19149 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; |
19150 | if (VAR_P (decl) && !DECL_NAME (decl) |
19151 | && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) |
19152 | /* Anonymous aggregates are a special case. */ |
19153 | finish_anon_union (decl); |
19154 | else if (is_capture_proxy (DECL_EXPR_DECL (t))) |
19155 | { |
19156 | DECL_CONTEXT (decl) = current_function_decl; |
19157 | insert_capture_proxy (decl); |
19158 | } |
19159 | else if (DECL_IMPLICIT_TYPEDEF_P (t)) |
19160 | /* We already did a pushtag. */; |
19161 | else if (VAR_OR_FUNCTION_DECL_P (decl) |
19162 | && DECL_LOCAL_DECL_P (decl)) |
19163 | { |
19164 | if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL) |
19165 | DECL_CONTEXT (decl) = NULL_TREE; |
19166 | decl = pushdecl (decl); |
19167 | if (TREE_CODE (decl) == FUNCTION_DECL |
19168 | && DECL_OMP_DECLARE_REDUCTION_P (decl) |
19169 | && cp_check_omp_declare_reduction (decl)) |
19170 | instantiate_body (pattern: pattern_decl, args, d: decl, nested: true); |
19171 | } |
19172 | else |
19173 | { |
19174 | bool const_init = false; |
19175 | cp_decomp decomp_d, *decomp = NULL; |
19176 | tree asmspec_tree = NULL_TREE; |
19177 | maybe_push_decl (decl); |
19178 | |
19179 | if (VAR_P (decl) |
19180 | && DECL_LANG_SPECIFIC (decl) |
19181 | && DECL_OMP_PRIVATIZED_MEMBER (decl)) |
19182 | break; |
19183 | |
19184 | if (DECL_DECOMPOSITION_P (decl) |
19185 | && TREE_TYPE (pattern_decl) != error_mark_node) |
19186 | { |
19187 | decomp = &decomp_d; |
19188 | if (tsubst_decomp_names (decl, pattern_decl, args, |
19189 | complain, in_decl, decomp) |
19190 | == error_mark_node) |
19191 | decomp = NULL; |
19192 | } |
19193 | |
19194 | init = tsubst_init (init, decl, args, complain, in_decl); |
19195 | |
19196 | if (VAR_P (decl)) |
19197 | const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P |
19198 | (pattern_decl)); |
19199 | |
19200 | /* In a non-template function, VLA type declarations are |
19201 | handled in grokdeclarator; for templates, handle them |
19202 | now. */ |
19203 | predeclare_vla (decl); |
19204 | |
19205 | if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl)) |
19206 | { |
19207 | tree id = DECL_ASSEMBLER_NAME (pattern_decl); |
19208 | const char *asmspec = IDENTIFIER_POINTER (id); |
19209 | gcc_assert (asmspec[0] == '*'); |
19210 | asmspec_tree |
19211 | = build_string (IDENTIFIER_LENGTH (id) - 1, |
19212 | asmspec + 1); |
19213 | TREE_TYPE (asmspec_tree) = char_array_type_node; |
19214 | } |
19215 | |
19216 | cp_finish_decl (decl, init, const_init, asmspec_tree, 0, |
19217 | decomp); |
19218 | } |
19219 | } |
19220 | } |
19221 | |
19222 | break; |
19223 | } |
19224 | |
19225 | case FOR_STMT: |
19226 | stmt = begin_for_stmt (NULL_TREE, NULL_TREE); |
19227 | RECUR (FOR_INIT_STMT (t)); |
19228 | finish_init_stmt (stmt); |
19229 | tmp = RECUR (FOR_COND (t)); |
19230 | finish_for_cond (tmp, stmt, false, 0, false); |
19231 | tmp = RECUR (FOR_EXPR (t)); |
19232 | finish_for_expr (tmp, stmt); |
19233 | { |
19234 | bool prev = note_iteration_stmt_body_start (); |
19235 | RECUR (FOR_BODY (t)); |
19236 | note_iteration_stmt_body_end (prev); |
19237 | } |
19238 | finish_for_stmt (stmt); |
19239 | break; |
19240 | |
19241 | case RANGE_FOR_STMT: |
19242 | { |
19243 | /* Construct another range_for, if this is not a final |
19244 | substitution (for inside a generic lambda of a |
19245 | template). Otherwise convert to a regular for. */ |
19246 | tree decl, expr; |
19247 | stmt = (processing_template_decl |
19248 | ? begin_range_for_stmt (NULL_TREE, NULL_TREE) |
19249 | : begin_for_stmt (NULL_TREE, NULL_TREE)); |
19250 | RECUR (RANGE_FOR_INIT_STMT (t)); |
19251 | decl = RANGE_FOR_DECL (t); |
19252 | decl = tsubst (t: decl, args, complain, in_decl); |
19253 | maybe_push_decl (decl); |
19254 | expr = RECUR (RANGE_FOR_EXPR (t)); |
19255 | |
19256 | cp_decomp decomp_d, *decomp = NULL; |
19257 | if (DECL_DECOMPOSITION_P (decl)) |
19258 | { |
19259 | decomp = &decomp_d; |
19260 | decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args, |
19261 | complain, in_decl, decomp); |
19262 | } |
19263 | |
19264 | tree unroll = RECUR (RANGE_FOR_UNROLL (t)); |
19265 | if (unroll) |
19266 | unroll |
19267 | = cp_check_pragma_unroll (EXPR_LOCATION (RANGE_FOR_UNROLL (t)), |
19268 | unroll); |
19269 | if (processing_template_decl) |
19270 | { |
19271 | RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t); |
19272 | RANGE_FOR_UNROLL (stmt) = unroll; |
19273 | RANGE_FOR_NOVECTOR (stmt) = RANGE_FOR_NOVECTOR (t); |
19274 | finish_range_for_decl (stmt, decl, expr); |
19275 | if (decomp && decl != error_mark_node) |
19276 | cp_finish_decomp (decl, decomp); |
19277 | } |
19278 | else |
19279 | stmt = cp_convert_range_for (stmt, decl, expr, decomp, |
19280 | RANGE_FOR_IVDEP (t), unroll, |
19281 | RANGE_FOR_NOVECTOR (t)); |
19282 | |
19283 | bool prev = note_iteration_stmt_body_start (); |
19284 | RECUR (RANGE_FOR_BODY (t)); |
19285 | note_iteration_stmt_body_end (prev); |
19286 | finish_for_stmt (stmt); |
19287 | } |
19288 | break; |
19289 | |
19290 | case WHILE_STMT: |
19291 | stmt = begin_while_stmt (); |
19292 | tmp = RECUR (WHILE_COND (t)); |
19293 | finish_while_stmt_cond (tmp, stmt, false, 0, false); |
19294 | { |
19295 | bool prev = note_iteration_stmt_body_start (); |
19296 | RECUR (WHILE_BODY (t)); |
19297 | note_iteration_stmt_body_end (prev); |
19298 | } |
19299 | finish_while_stmt (stmt); |
19300 | break; |
19301 | |
19302 | case DO_STMT: |
19303 | stmt = begin_do_stmt (); |
19304 | { |
19305 | bool prev = note_iteration_stmt_body_start (); |
19306 | RECUR (DO_BODY (t)); |
19307 | note_iteration_stmt_body_end (prev); |
19308 | } |
19309 | finish_do_body (stmt); |
19310 | tmp = RECUR (DO_COND (t)); |
19311 | finish_do_stmt (tmp, stmt, false, 0, false); |
19312 | break; |
19313 | |
19314 | case IF_STMT: |
19315 | stmt = begin_if_stmt (); |
19316 | IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t); |
19317 | IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t); |
19318 | if (IF_STMT_CONSTEXPR_P (t)) |
19319 | args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl); |
19320 | { |
19321 | tree cond = IF_COND (t); |
19322 | bool was_dep = dependent_operand_p (t: cond); |
19323 | cond = RECUR (cond); |
19324 | warning_sentinel s1(warn_address, was_dep); |
19325 | tmp = finish_if_stmt_cond (cond, stmt); |
19326 | } |
19327 | if (IF_STMT_CONSTEXPR_P (t) |
19328 | && instantiation_dependent_expression_p (tmp)) |
19329 | { |
19330 | /* We're partially instantiating a generic lambda, but the condition |
19331 | of the constexpr if is still dependent. Don't substitute into the |
19332 | branches now, just remember the template arguments. */ |
19333 | do_poplevel (IF_SCOPE (stmt)); |
19334 | IF_SCOPE (stmt) = NULL_TREE; |
19335 | IF_COND (stmt) = IF_COND (t); |
19336 | THEN_CLAUSE (stmt) = THEN_CLAUSE (t); |
19337 | ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t); |
19338 | IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (pattern: stmt, args, complain); |
19339 | add_stmt (stmt); |
19340 | break; |
19341 | } |
19342 | if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) |
19343 | /* Don't instantiate the THEN_CLAUSE. */; |
19344 | else if (IF_STMT_CONSTEVAL_P (t)) |
19345 | { |
19346 | bool save_in_consteval_if_p = in_consteval_if_p; |
19347 | in_consteval_if_p = true; |
19348 | RECUR (THEN_CLAUSE (t)); |
19349 | in_consteval_if_p = save_in_consteval_if_p; |
19350 | } |
19351 | else |
19352 | { |
19353 | tree folded = fold_non_dependent_expr (tmp, complain); |
19354 | bool inhibit = integer_zerop (folded); |
19355 | if (inhibit) |
19356 | ++c_inhibit_evaluation_warnings; |
19357 | RECUR (THEN_CLAUSE (t)); |
19358 | if (inhibit) |
19359 | --c_inhibit_evaluation_warnings; |
19360 | } |
19361 | finish_then_clause (stmt); |
19362 | |
19363 | if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp)) |
19364 | /* Don't instantiate the ELSE_CLAUSE. */; |
19365 | else if (ELSE_CLAUSE (t)) |
19366 | { |
19367 | tree folded = fold_non_dependent_expr (tmp, complain); |
19368 | bool inhibit = integer_nonzerop (folded); |
19369 | begin_else_clause (stmt); |
19370 | if (inhibit) |
19371 | ++c_inhibit_evaluation_warnings; |
19372 | RECUR (ELSE_CLAUSE (t)); |
19373 | if (inhibit) |
19374 | --c_inhibit_evaluation_warnings; |
19375 | finish_else_clause (stmt); |
19376 | } |
19377 | |
19378 | finish_if_stmt (stmt); |
19379 | break; |
19380 | |
19381 | case BIND_EXPR: |
19382 | if (BIND_EXPR_BODY_BLOCK (t)) |
19383 | stmt = begin_function_body (); |
19384 | else |
19385 | stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t) |
19386 | ? BCS_TRY_BLOCK : 0); |
19387 | |
19388 | RECUR (BIND_EXPR_BODY (t)); |
19389 | |
19390 | if (BIND_EXPR_BODY_BLOCK (t)) |
19391 | finish_function_body (stmt); |
19392 | else |
19393 | finish_compound_stmt (stmt); |
19394 | break; |
19395 | |
19396 | case BREAK_STMT: |
19397 | finish_break_stmt (); |
19398 | break; |
19399 | |
19400 | case CONTINUE_STMT: |
19401 | finish_continue_stmt (); |
19402 | break; |
19403 | |
19404 | case SWITCH_STMT: |
19405 | stmt = begin_switch_stmt (); |
19406 | tmp = RECUR (SWITCH_STMT_COND (t)); |
19407 | finish_switch_cond (tmp, stmt); |
19408 | RECUR (SWITCH_STMT_BODY (t)); |
19409 | finish_switch_stmt (stmt); |
19410 | break; |
19411 | |
19412 | case CASE_LABEL_EXPR: |
19413 | { |
19414 | tree decl = CASE_LABEL (t); |
19415 | tree low = RECUR (CASE_LOW (t)); |
19416 | tree high = RECUR (CASE_HIGH (t)); |
19417 | tree l = finish_case_label (EXPR_LOCATION (t), low, high); |
19418 | if (l && TREE_CODE (l) == CASE_LABEL_EXPR) |
19419 | { |
19420 | tree label = CASE_LABEL (l); |
19421 | FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl); |
19422 | if (DECL_ATTRIBUTES (decl) != NULL_TREE) |
19423 | cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); |
19424 | } |
19425 | } |
19426 | break; |
19427 | |
19428 | case LABEL_EXPR: |
19429 | { |
19430 | tree decl = LABEL_EXPR_LABEL (t); |
19431 | tree label = finish_label_stmt (DECL_NAME (decl)); |
19432 | if (TREE_CODE (label) == LABEL_DECL) |
19433 | { |
19434 | FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl); |
19435 | copy_warning (label, decl); |
19436 | } |
19437 | if (DECL_ATTRIBUTES (decl) != NULL_TREE) |
19438 | cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0); |
19439 | } |
19440 | break; |
19441 | |
19442 | case GOTO_EXPR: |
19443 | tmp = GOTO_DESTINATION (t); |
19444 | if (TREE_CODE (tmp) != LABEL_DECL) |
19445 | /* Computed goto's must be tsubst'd into. On the other hand, |
19446 | non-computed gotos must not be; the identifier in question |
19447 | will have no binding. */ |
19448 | tmp = RECUR (tmp); |
19449 | else |
19450 | tmp = DECL_NAME (tmp); |
19451 | finish_goto_stmt (tmp); |
19452 | break; |
19453 | |
19454 | case ASM_EXPR: |
19455 | { |
19456 | tree string = RECUR (ASM_STRING (t)); |
19457 | tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, |
19458 | complain, in_decl); |
19459 | tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args, |
19460 | complain, in_decl); |
19461 | tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, |
19462 | complain, in_decl); |
19463 | tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args, |
19464 | complain, in_decl); |
19465 | tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string, |
19466 | outputs, inputs, clobbers, labels, |
19467 | ASM_INLINE_P (t), false); |
19468 | tree asm_expr = tmp; |
19469 | if (asm_expr != error_mark_node) |
19470 | { |
19471 | if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR) |
19472 | asm_expr = TREE_OPERAND (asm_expr, 0); |
19473 | ASM_BASIC_P (asm_expr) = ASM_BASIC_P (t); |
19474 | } |
19475 | } |
19476 | break; |
19477 | |
19478 | case TRY_BLOCK: |
19479 | if (CLEANUP_P (t)) |
19480 | { |
19481 | stmt = begin_try_block (); |
19482 | RECUR (TRY_STMTS (t)); |
19483 | finish_cleanup_try_block (stmt); |
19484 | finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt); |
19485 | } |
19486 | else |
19487 | { |
19488 | tree compound_stmt = NULL_TREE; |
19489 | |
19490 | if (FN_TRY_BLOCK_P (t)) |
19491 | stmt = begin_function_try_block (&compound_stmt); |
19492 | else |
19493 | stmt = begin_try_block (); |
19494 | |
19495 | RECUR (TRY_STMTS (t)); |
19496 | |
19497 | if (FN_TRY_BLOCK_P (t)) |
19498 | finish_function_try_block (stmt); |
19499 | else |
19500 | finish_try_block (stmt); |
19501 | |
19502 | RECUR (TRY_HANDLERS (t)); |
19503 | if (FN_TRY_BLOCK_P (t)) |
19504 | finish_function_handler_sequence (stmt, compound_stmt); |
19505 | else |
19506 | finish_handler_sequence (stmt); |
19507 | } |
19508 | break; |
19509 | |
19510 | case HANDLER: |
19511 | { |
19512 | tree decl = HANDLER_PARMS (t); |
19513 | |
19514 | if (decl) |
19515 | { |
19516 | decl = tsubst (t: decl, args, complain, in_decl); |
19517 | /* Prevent instantiate_decl from trying to instantiate |
19518 | this variable. We've already done all that needs to be |
19519 | done. */ |
19520 | if (decl != error_mark_node) |
19521 | DECL_TEMPLATE_INSTANTIATED (decl) = 1; |
19522 | } |
19523 | stmt = begin_handler (); |
19524 | finish_handler_parms (decl, stmt); |
19525 | RECUR (HANDLER_BODY (t)); |
19526 | finish_handler (stmt); |
19527 | } |
19528 | break; |
19529 | |
19530 | case TAG_DEFN: |
19531 | tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE); |
19532 | if (dependent_type_p (tmp)) |
19533 | /* This is a partial instantiation, try again when full. */ |
19534 | add_stmt (build_min (TAG_DEFN, tmp)); |
19535 | else if (CLASS_TYPE_P (tmp)) |
19536 | { |
19537 | /* Local classes are not independent templates; they are |
19538 | instantiated along with their containing function. And this |
19539 | way we don't have to deal with pushing out of one local class |
19540 | to instantiate a member of another local class. */ |
19541 | /* Closures are handled by the LAMBDA_EXPR. */ |
19542 | gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t))); |
19543 | complete_type (tmp); |
19544 | tree save_ccp = current_class_ptr; |
19545 | tree save_ccr = current_class_ref; |
19546 | for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld)) |
19547 | if ((VAR_P (fld) |
19548 | || (TREE_CODE (fld) == FUNCTION_DECL |
19549 | && !DECL_ARTIFICIAL (fld))) |
19550 | && DECL_TEMPLATE_INSTANTIATION (fld)) |
19551 | instantiate_decl (fld, /*defer_ok=*/false, |
19552 | /*expl_inst_class=*/false); |
19553 | else if (TREE_CODE (fld) == FIELD_DECL) |
19554 | maybe_instantiate_nsdmi_init (fld, tf_warning_or_error); |
19555 | current_class_ptr = save_ccp; |
19556 | current_class_ref = save_ccr; |
19557 | } |
19558 | break; |
19559 | |
19560 | case STATIC_ASSERT: |
19561 | { |
19562 | tree condition, message; |
19563 | |
19564 | ++c_inhibit_evaluation_warnings; |
19565 | condition = tsubst_expr (STATIC_ASSERT_CONDITION (t), args, |
19566 | complain, in_decl); |
19567 | message = tsubst_expr (STATIC_ASSERT_MESSAGE (t), args, |
19568 | complain, in_decl); |
19569 | if (TREE_CODE (STATIC_ASSERT_MESSAGE (t)) != STRING_CST |
19570 | && TREE_CODE (message) == STRING_CST) |
19571 | message = build1_loc (STATIC_ASSERT_SOURCE_LOCATION (t), |
19572 | code: PAREN_EXPR, TREE_TYPE (message), arg1: message); |
19573 | --c_inhibit_evaluation_warnings; |
19574 | |
19575 | finish_static_assert (condition, message, |
19576 | STATIC_ASSERT_SOURCE_LOCATION (t), |
19577 | /*member_p=*/false, /*show_expr_p=*/true); |
19578 | } |
19579 | break; |
19580 | |
19581 | case OACC_KERNELS: |
19582 | case OACC_PARALLEL: |
19583 | case OACC_SERIAL: |
19584 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), ort: C_ORT_ACC_TARGET, args, |
19585 | complain, in_decl); |
19586 | stmt = begin_omp_parallel (); |
19587 | RECUR (OMP_BODY (t)); |
19588 | finish_omp_construct (TREE_CODE (t), stmt, tmp); |
19589 | break; |
19590 | |
19591 | case OMP_PARALLEL: |
19592 | r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t)); |
19593 | tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), ort: C_ORT_OMP, args, |
19594 | complain, in_decl); |
19595 | if (OMP_PARALLEL_COMBINED (t)) |
19596 | omp_parallel_combined_clauses = &tmp; |
19597 | stmt = begin_omp_parallel (); |
19598 | RECUR (OMP_PARALLEL_BODY (t)); |
19599 | gcc_assert (omp_parallel_combined_clauses == NULL); |
19600 | OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt)) |
19601 | = OMP_PARALLEL_COMBINED (t); |
19602 | pop_omp_privatization_clauses (r); |
19603 | break; |
19604 | |
19605 | case OMP_TASK: |
19606 | if (OMP_TASK_BODY (t) == NULL_TREE) |
19607 | { |
19608 | tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), ort: C_ORT_OMP, args, |
19609 | complain, in_decl); |
19610 | t = copy_node (t); |
19611 | OMP_TASK_CLAUSES (t) = tmp; |
19612 | add_stmt (t); |
19613 | break; |
19614 | } |
19615 | r = push_omp_privatization_clauses (false); |
19616 | tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), ort: C_ORT_OMP, args, |
19617 | complain, in_decl); |
19618 | stmt = begin_omp_task (); |
19619 | RECUR (OMP_TASK_BODY (t)); |
19620 | finish_omp_task (tmp, stmt); |
19621 | pop_omp_privatization_clauses (r); |
19622 | break; |
19623 | |
19624 | case OMP_FOR: |
19625 | case OMP_LOOP: |
19626 | case OMP_SIMD: |
19627 | case OMP_DISTRIBUTE: |
19628 | case OMP_TASKLOOP: |
19629 | case OMP_TILE: |
19630 | case OMP_UNROLL: |
19631 | case OACC_LOOP: |
19632 | { |
19633 | tree clauses, body, pre_body; |
19634 | tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE; |
19635 | tree orig_declv = NULL_TREE; |
19636 | tree incrv = NULL_TREE; |
19637 | enum c_omp_region_type ort = C_ORT_OMP; |
19638 | bool any_range_for = false; |
19639 | int i; |
19640 | |
19641 | if (TREE_CODE (t) == OACC_LOOP) |
19642 | ort = C_ORT_ACC; |
19643 | |
19644 | r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE); |
19645 | clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain, |
19646 | in_decl); |
19647 | if (OMP_FOR_INIT (t) != NULL_TREE) |
19648 | { |
19649 | declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
19650 | if (OMP_FOR_ORIG_DECLS (t)) |
19651 | orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
19652 | initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
19653 | condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
19654 | incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t))); |
19655 | } |
19656 | |
19657 | keep_next_level (true); |
19658 | stmt = begin_omp_structured_block (); |
19659 | |
19660 | pre_body = push_stmt_list (); |
19661 | RECUR (OMP_FOR_PRE_BODY (t)); |
19662 | pre_body = pop_stmt_list (pre_body); |
19663 | |
19664 | if (OMP_FOR_INIT (t) != NULL_TREE) |
19665 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) |
19666 | { |
19667 | if (TREE_VEC_ELT (OMP_FOR_INIT (t), i)) |
19668 | any_range_for |
19669 | |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, |
19670 | condv, incrv, clauses: &clauses, args, |
19671 | complain, in_decl); |
19672 | else |
19673 | { |
19674 | TREE_VEC_ELT (declv, i) = global_namespace; |
19675 | TREE_VEC_ELT (initv, i) = NULL_TREE; |
19676 | TREE_VEC_ELT (condv, i) = NULL_TREE; |
19677 | TREE_VEC_ELT (incrv, i) = NULL_TREE; |
19678 | if (orig_declv) |
19679 | TREE_VEC_ELT (orig_declv, i) = NULL_TREE; |
19680 | } |
19681 | } |
19682 | omp_parallel_combined_clauses = NULL; |
19683 | |
19684 | if (any_range_for) |
19685 | { |
19686 | gcc_assert (orig_declv); |
19687 | body = begin_omp_structured_block (); |
19688 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) |
19689 | if (TREE_VEC_ELT (declv, i) != global_namespace |
19690 | && TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i) |
19691 | && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST |
19692 | && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i))) |
19693 | cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i), |
19694 | TREE_VEC_ELT (declv, i)); |
19695 | } |
19696 | else |
19697 | body = push_stmt_list (); |
19698 | RECUR (OMP_FOR_BODY (t)); |
19699 | if (any_range_for) |
19700 | body = finish_omp_structured_block (body); |
19701 | else |
19702 | body = pop_stmt_list (body); |
19703 | |
19704 | if (OMP_FOR_INIT (t) != NULL_TREE) |
19705 | t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv, |
19706 | orig_declv, initv, condv, incrv, body, pre_body, |
19707 | NULL, clauses); |
19708 | else |
19709 | { |
19710 | t = make_node (TREE_CODE (t)); |
19711 | TREE_TYPE (t) = void_type_node; |
19712 | OMP_FOR_BODY (t) = body; |
19713 | OMP_FOR_PRE_BODY (t) = pre_body; |
19714 | OMP_FOR_CLAUSES (t) = clauses; |
19715 | SET_EXPR_LOCATION (t, EXPR_LOCATION (t)); |
19716 | add_stmt (t); |
19717 | } |
19718 | |
19719 | add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt), |
19720 | t)); |
19721 | pop_omp_privatization_clauses (r); |
19722 | |
19723 | if (any_range_for && !processing_template_decl && t) |
19724 | for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++) |
19725 | if (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i) |
19726 | && TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), |
19727 | i)) == TREE_LIST) |
19728 | { |
19729 | tree v = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i); |
19730 | if (TREE_CHAIN (v) == NULL_TREE |
19731 | || TREE_CODE (TREE_CHAIN (v)) != TREE_VEC) |
19732 | continue; |
19733 | v = TREE_VEC_ELT (TREE_CHAIN (v), 0); |
19734 | gcc_assert (VAR_P (v) && DECL_NAME (v) == NULL_TREE); |
19735 | DECL_NAME (v) = for_range_identifier; |
19736 | } |
19737 | } |
19738 | break; |
19739 | |
19740 | case OMP_SECTIONS: |
19741 | case OMP_MASKED: |
19742 | omp_parallel_combined_clauses = NULL; |
19743 | /* FALLTHRU */ |
19744 | case OMP_SINGLE: |
19745 | case OMP_SCOPE: |
19746 | case OMP_TEAMS: |
19747 | case OMP_CRITICAL: |
19748 | case OMP_TASKGROUP: |
19749 | case OMP_SCAN: |
19750 | r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS |
19751 | && OMP_TEAMS_COMBINED (t)); |
19752 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), ort: C_ORT_OMP, args, complain, |
19753 | in_decl); |
19754 | if (TREE_CODE (t) == OMP_TEAMS) |
19755 | { |
19756 | keep_next_level (true); |
19757 | stmt = begin_omp_structured_block (); |
19758 | RECUR (OMP_BODY (t)); |
19759 | stmt = finish_omp_structured_block (stmt); |
19760 | } |
19761 | else |
19762 | { |
19763 | stmt = push_stmt_list (); |
19764 | RECUR (OMP_BODY (t)); |
19765 | stmt = pop_stmt_list (stmt); |
19766 | } |
19767 | |
19768 | if (TREE_CODE (t) == OMP_CRITICAL |
19769 | && tmp != NULL_TREE |
19770 | && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp))) |
19771 | { |
19772 | error_at (OMP_CLAUSE_LOCATION (tmp), |
19773 | "%<#pragma omp critical%> with %<hint%> clause requires " |
19774 | "a name, except when %<omp_sync_hint_none%> is used"); |
19775 | RETURN (error_mark_node); |
19776 | } |
19777 | t = copy_node (t); |
19778 | OMP_BODY (t) = stmt; |
19779 | OMP_CLAUSES (t) = tmp; |
19780 | add_stmt (t); |
19781 | pop_omp_privatization_clauses (r); |
19782 | break; |
19783 | |
19784 | case OMP_DEPOBJ: |
19785 | r = RECUR (OMP_DEPOBJ_DEPOBJ (t)); |
19786 | if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node) |
19787 | { |
19788 | enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID; |
19789 | if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE) |
19790 | { |
19791 | tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), ort: C_ORT_OMP, |
19792 | args, complain, in_decl); |
19793 | if (tmp == NULL_TREE) |
19794 | tmp = error_mark_node; |
19795 | } |
19796 | else |
19797 | { |
19798 | kind = (enum omp_clause_depend_kind) |
19799 | tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t)); |
19800 | tmp = NULL_TREE; |
19801 | } |
19802 | finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp); |
19803 | } |
19804 | else |
19805 | finish_omp_depobj (EXPR_LOCATION (t), r, |
19806 | OMP_CLAUSE_DEPEND_INVALID, |
19807 | OMP_DEPOBJ_CLAUSES (t)); |
19808 | break; |
19809 | |
19810 | case OACC_DATA: |
19811 | case OMP_TARGET_DATA: |
19812 | case OMP_TARGET: |
19813 | tmp = tsubst_omp_clauses (OMP_CLAUSES (t), |
19814 | TREE_CODE (t) == OACC_DATA |
19815 | ? C_ORT_ACC |
19816 | : TREE_CODE (t) == OMP_TARGET |
19817 | ? C_ORT_OMP_TARGET : C_ORT_OMP, |
19818 | args, complain, in_decl); |
19819 | keep_next_level (true); |
19820 | stmt = begin_omp_structured_block (); |
19821 | |
19822 | RECUR (OMP_BODY (t)); |
19823 | stmt = finish_omp_structured_block (stmt); |
19824 | |
19825 | t = copy_node (t); |
19826 | OMP_BODY (t) = stmt; |
19827 | OMP_CLAUSES (t) = tmp; |
19828 | |
19829 | if (TREE_CODE (t) == OMP_TARGET) |
19830 | finish_omp_target_clauses (EXPR_LOCATION (t), OMP_BODY (t), |
19831 | &OMP_CLAUSES (t)); |
19832 | |
19833 | if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t)) |
19834 | { |
19835 | tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL); |
19836 | if (teams) |
19837 | /* For combined target teams, ensure the num_teams and |
19838 | thread_limit clause expressions are evaluated on the host, |
19839 | before entering the target construct. */ |
19840 | for (tree c = OMP_TEAMS_CLAUSES (teams); |
19841 | c; c = OMP_CLAUSE_CHAIN (c)) |
19842 | if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS |
19843 | || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT) |
19844 | for (int i = 0; |
19845 | i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i) |
19846 | if (OMP_CLAUSE_OPERAND (c, i) |
19847 | && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST) |
19848 | { |
19849 | tree expr = OMP_CLAUSE_OPERAND (c, i); |
19850 | expr = force_target_expr (TREE_TYPE (expr), expr, |
19851 | tf_none); |
19852 | if (expr == error_mark_node) |
19853 | continue; |
19854 | tmp = TARGET_EXPR_SLOT (expr); |
19855 | add_stmt (expr); |
19856 | OMP_CLAUSE_OPERAND (c, i) = expr; |
19857 | tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c), |
19858 | OMP_CLAUSE_FIRSTPRIVATE); |
19859 | OMP_CLAUSE_DECL (tc) = tmp; |
19860 | OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t); |
19861 | OMP_TARGET_CLAUSES (t) = tc; |
19862 | } |
19863 | } |
19864 | add_stmt (t); |
19865 | break; |
19866 | |
19867 | case OACC_DECLARE: |
19868 | t = copy_node (t); |
19869 | tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), ort: C_ORT_ACC, args, |
19870 | complain, in_decl); |
19871 | OACC_DECLARE_CLAUSES (t) = tmp; |
19872 | add_stmt (t); |
19873 | break; |
19874 | |
19875 | case OMP_TARGET_UPDATE: |
19876 | case OMP_TARGET_ENTER_DATA: |
19877 | case OMP_TARGET_EXIT_DATA: |
19878 | tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), ort: C_ORT_OMP, args, |
19879 | complain, in_decl); |
19880 | t = copy_node (t); |
19881 | OMP_STANDALONE_CLAUSES (t) = tmp; |
19882 | add_stmt (t); |
19883 | break; |
19884 | |
19885 | case OACC_CACHE: |
19886 | case OACC_ENTER_DATA: |
19887 | case OACC_EXIT_DATA: |
19888 | case OACC_UPDATE: |
19889 | tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), ort: C_ORT_ACC, args, |
19890 | complain, in_decl); |
19891 | t = copy_node (t); |
19892 | OMP_STANDALONE_CLAUSES (t) = tmp; |
19893 | add_stmt (t); |
19894 | break; |
19895 | |
19896 | case OMP_ORDERED: |
19897 | tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), ort: C_ORT_OMP, args, |
19898 | complain, in_decl); |
19899 | if (OMP_BODY (t)) |
19900 | { |
19901 | stmt = push_stmt_list (); |
19902 | RECUR (OMP_BODY (t)); |
19903 | stmt = pop_stmt_list (stmt); |
19904 | } |
19905 | else |
19906 | stmt = NULL_TREE; |
19907 | |
19908 | t = copy_node (t); |
19909 | OMP_BODY (t) = stmt; |
19910 | OMP_ORDERED_CLAUSES (t) = tmp; |
19911 | add_stmt (t); |
19912 | break; |
19913 | |
19914 | case OMP_MASTER: |
19915 | case OMP_STRUCTURED_BLOCK: |
19916 | omp_parallel_combined_clauses = NULL; |
19917 | /* FALLTHRU */ |
19918 | case OMP_SECTION: |
19919 | stmt = push_stmt_list (); |
19920 | RECUR (OMP_BODY (t)); |
19921 | stmt = pop_stmt_list (stmt); |
19922 | |
19923 | t = copy_node (t); |
19924 | OMP_BODY (t) = stmt; |
19925 | add_stmt (t); |
19926 | break; |
19927 | |
19928 | case OMP_DISPATCH: |
19929 | tmp = tsubst_omp_clauses (OMP_DISPATCH_CLAUSES (t), ort: C_ORT_OMP, args, |
19930 | complain, in_decl); |
19931 | stmt = RECUR (OMP_DISPATCH_BODY (t)); |
19932 | t = copy_node (t); |
19933 | OMP_DISPATCH_BODY (t) = stmt; |
19934 | OMP_DISPATCH_CLAUSES (t) = tmp; |
19935 | add_stmt (t); |
19936 | break; |
19937 | |
19938 | case OMP_ATOMIC: |
19939 | gcc_assert (OMP_ATOMIC_DEPENDENT_P (t)); |
19940 | tmp = NULL_TREE; |
19941 | if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE) |
19942 | tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), ort: C_ORT_OMP, args, |
19943 | complain, in_decl); |
19944 | if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR) |
19945 | { |
19946 | tree op1 = TREE_OPERAND (t, 1); |
19947 | tree rhs1 = NULL_TREE; |
19948 | tree r = NULL_TREE; |
19949 | tree lhs, rhs; |
19950 | if (TREE_CODE (op1) == COMPOUND_EXPR) |
19951 | { |
19952 | rhs1 = RECUR (TREE_OPERAND (op1, 0)); |
19953 | op1 = TREE_OPERAND (op1, 1); |
19954 | } |
19955 | if (TREE_CODE (op1) == COND_EXPR) |
19956 | { |
19957 | gcc_assert (rhs1 == NULL_TREE); |
19958 | tree c = TREE_OPERAND (op1, 0); |
19959 | if (TREE_CODE (c) == MODIFY_EXPR) |
19960 | { |
19961 | r = RECUR (TREE_OPERAND (c, 0)); |
19962 | c = TREE_OPERAND (c, 1); |
19963 | } |
19964 | gcc_assert (TREE_CODE (c) == EQ_EXPR); |
19965 | rhs = RECUR (TREE_OPERAND (c, 1)); |
19966 | lhs = RECUR (TREE_OPERAND (op1, 2)); |
19967 | rhs1 = RECUR (TREE_OPERAND (op1, 1)); |
19968 | } |
19969 | else |
19970 | { |
19971 | lhs = RECUR (TREE_OPERAND (op1, 0)); |
19972 | rhs = RECUR (TREE_OPERAND (op1, 1)); |
19973 | } |
19974 | finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1), |
19975 | lhs, rhs, NULL_TREE, NULL_TREE, rhs1, r, |
19976 | tmp, OMP_ATOMIC_MEMORY_ORDER (t), |
19977 | OMP_ATOMIC_WEAK (t)); |
19978 | } |
19979 | else |
19980 | { |
19981 | tree op1 = TREE_OPERAND (t, 1); |
19982 | tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE; |
19983 | tree rhs1 = NULL_TREE, r = NULL_TREE; |
19984 | enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1)); |
19985 | enum tree_code opcode = NOP_EXPR; |
19986 | if (code == OMP_ATOMIC_READ) |
19987 | { |
19988 | v = RECUR (TREE_OPERAND (op1, 0)); |
19989 | lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0)); |
19990 | } |
19991 | else if (code == OMP_ATOMIC_CAPTURE_OLD |
19992 | || code == OMP_ATOMIC_CAPTURE_NEW) |
19993 | { |
19994 | tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1); |
19995 | v = RECUR (TREE_OPERAND (op1, 0)); |
19996 | lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0)); |
19997 | if (TREE_CODE (op11) == COMPOUND_EXPR) |
19998 | { |
19999 | rhs1 = RECUR (TREE_OPERAND (op11, 0)); |
20000 | op11 = TREE_OPERAND (op11, 1); |
20001 | } |
20002 | if (TREE_CODE (op11) == COND_EXPR) |
20003 | { |
20004 | gcc_assert (rhs1 == NULL_TREE); |
20005 | tree c = TREE_OPERAND (op11, 0); |
20006 | if (TREE_CODE (c) == MODIFY_EXPR) |
20007 | { |
20008 | r = RECUR (TREE_OPERAND (c, 0)); |
20009 | c = TREE_OPERAND (c, 1); |
20010 | } |
20011 | gcc_assert (TREE_CODE (c) == EQ_EXPR); |
20012 | rhs = RECUR (TREE_OPERAND (c, 1)); |
20013 | lhs = RECUR (TREE_OPERAND (op11, 2)); |
20014 | rhs1 = RECUR (TREE_OPERAND (op11, 1)); |
20015 | } |
20016 | else |
20017 | { |
20018 | lhs = RECUR (TREE_OPERAND (op11, 0)); |
20019 | rhs = RECUR (TREE_OPERAND (op11, 1)); |
20020 | } |
20021 | opcode = TREE_CODE (op11); |
20022 | if (opcode == MODIFY_EXPR) |
20023 | opcode = NOP_EXPR; |
20024 | } |
20025 | else |
20026 | { |
20027 | code = OMP_ATOMIC; |
20028 | lhs = RECUR (TREE_OPERAND (op1, 0)); |
20029 | rhs = RECUR (TREE_OPERAND (op1, 1)); |
20030 | } |
20031 | finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v, |
20032 | lhs1, rhs1, r, tmp, |
20033 | OMP_ATOMIC_MEMORY_ORDER (t), OMP_ATOMIC_WEAK (t)); |
20034 | } |
20035 | break; |
20036 | |
20037 | case OMP_METADIRECTIVE: |
20038 | { |
20039 | tree variants = NULL_TREE; |
20040 | for (tree v = OMP_METADIRECTIVE_VARIANTS (t); v; v = TREE_CHAIN (v)) |
20041 | { |
20042 | tree ctx = OMP_METADIRECTIVE_VARIANT_SELECTOR (v); |
20043 | tree directive = OMP_METADIRECTIVE_VARIANT_DIRECTIVE (v); |
20044 | tree body = OMP_METADIRECTIVE_VARIANT_BODY (v); |
20045 | tree s; |
20046 | |
20047 | /* CTX is null if this is the default variant. */ |
20048 | if (ctx) |
20049 | { |
20050 | ctx = tsubst_omp_context_selector (ctx, args, complain, |
20051 | in_decl); |
20052 | /* Remove the selector from further consideration if it can be |
20053 | evaluated as a non-match at this point. */ |
20054 | if (omp_context_selector_matches (ctx, NULL_TREE, false) == 0) |
20055 | continue; |
20056 | } |
20057 | s = push_stmt_list (); |
20058 | RECUR (directive); |
20059 | directive = pop_stmt_list (s); |
20060 | if (body) |
20061 | { |
20062 | s = push_stmt_list (); |
20063 | RECUR (body); |
20064 | body = pop_stmt_list (s); |
20065 | } |
20066 | variants |
20067 | = chainon (variants, |
20068 | make_omp_metadirective_variant (ctx, directive, |
20069 | body)); |
20070 | } |
20071 | t = copy_node (t); |
20072 | OMP_METADIRECTIVE_VARIANTS (t) = variants; |
20073 | |
20074 | /* Try to resolve the metadirective early. */ |
20075 | vec<struct omp_variant> candidates |
20076 | = omp_early_resolve_metadirective (t); |
20077 | if (!candidates.is_empty ()) |
20078 | t = c_omp_expand_variant_construct (candidates); |
20079 | add_stmt (t); |
20080 | break; |
20081 | } |
20082 | |
20083 | case OMP_DECLARE_MAPPER: |
20084 | { |
20085 | t = copy_node (t); |
20086 | |
20087 | tree decl = OMP_DECLARE_MAPPER_DECL (t); |
20088 | decl = tsubst (t: decl, args, complain, in_decl); |
20089 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
20090 | tree clauses = OMP_DECLARE_MAPPER_CLAUSES (t); |
20091 | clauses = tsubst_omp_clauses (clauses, ort: C_ORT_OMP_DECLARE_MAPPER, args, |
20092 | complain, in_decl); |
20093 | TREE_TYPE (t) = type; |
20094 | OMP_DECLARE_MAPPER_DECL (t) = decl; |
20095 | OMP_DECLARE_MAPPER_CLAUSES (t) = clauses; |
20096 | RETURN (t); |
20097 | } |
20098 | |
20099 | case TRANSACTION_EXPR: |
20100 | { |
20101 | int flags = 0; |
20102 | flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0); |
20103 | flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0); |
20104 | |
20105 | if (TRANSACTION_EXPR_IS_STMT (t)) |
20106 | { |
20107 | tree body = TRANSACTION_EXPR_BODY (t); |
20108 | tree noex = NULL_TREE; |
20109 | if (TREE_CODE (body) == MUST_NOT_THROW_EXPR) |
20110 | { |
20111 | noex = MUST_NOT_THROW_COND (body); |
20112 | if (noex == NULL_TREE) |
20113 | noex = boolean_true_node; |
20114 | body = TREE_OPERAND (body, 0); |
20115 | } |
20116 | stmt = begin_transaction_stmt (input_location, NULL, flags); |
20117 | RECUR (body); |
20118 | finish_transaction_stmt (stmt, NULL, flags, RECUR (noex)); |
20119 | } |
20120 | else |
20121 | { |
20122 | stmt = build_transaction_expr (EXPR_LOCATION (t), |
20123 | RECUR (TRANSACTION_EXPR_BODY (t)), |
20124 | flags, NULL_TREE); |
20125 | RETURN (stmt); |
20126 | } |
20127 | } |
20128 | break; |
20129 | |
20130 | case OMP_INTEROP: |
20131 | tmp = tsubst_omp_clauses (OMP_INTEROP_CLAUSES (t), ort: C_ORT_OMP_INTEROP, |
20132 | args, complain, in_decl); |
20133 | t = copy_node (t); |
20134 | OMP_INTEROP_CLAUSES (t) = tmp; |
20135 | add_stmt (t); |
20136 | break; |
20137 | |
20138 | case MUST_NOT_THROW_EXPR: |
20139 | { |
20140 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
20141 | tree cond = RECUR (MUST_NOT_THROW_COND (t)); |
20142 | RETURN (build_must_not_throw_expr (op0, cond)); |
20143 | } |
20144 | |
20145 | case EXPR_PACK_EXPANSION: |
20146 | error ("invalid use of pack expansion expression"); |
20147 | RETURN (error_mark_node); |
20148 | |
20149 | case NONTYPE_ARGUMENT_PACK: |
20150 | error ("use %<...%> to expand argument pack"); |
20151 | RETURN (error_mark_node); |
20152 | |
20153 | case COMPOUND_EXPR: |
20154 | tmp = RECUR (TREE_OPERAND (t, 0)); |
20155 | if (tmp == NULL_TREE) |
20156 | /* If the first operand was a statement, we're done with it. */ |
20157 | RETURN (RECUR (TREE_OPERAND (t, 1))); |
20158 | RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp, |
20159 | RECUR (TREE_OPERAND (t, 1)), |
20160 | templated_operator_saved_lookups (t), |
20161 | complain)); |
20162 | |
20163 | case PREDICT_EXPR: |
20164 | RETURN (add_stmt (copy_node (t))); |
20165 | |
20166 | case ANNOTATE_EXPR: |
20167 | { |
20168 | /* Although ANNOTATE_EXPR is an expression, it can only appear in |
20169 | WHILE_COND, DO_COND or FOR_COND expressions, which are tsubsted |
20170 | using tsubst_stmt rather than tsubst_expr and can contain |
20171 | DECL_EXPRs. */ |
20172 | tree op1 = RECUR (TREE_OPERAND (t, 0)); |
20173 | tree op2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); |
20174 | tree op3 = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl); |
20175 | if (TREE_CODE (op2) == INTEGER_CST |
20176 | && wi::to_widest (t: op2) == (int) annot_expr_unroll_kind) |
20177 | op3 = cp_check_pragma_unroll (EXPR_LOCATION (TREE_OPERAND (t, 2)), |
20178 | op3); |
20179 | RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR, |
20180 | TREE_TYPE (op1), op1, op2, op3)); |
20181 | } |
20182 | |
20183 | default: |
20184 | gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t))); |
20185 | |
20186 | RETURN (tsubst_expr (t, args, complain, in_decl)); |
20187 | } |
20188 | |
20189 | RETURN (NULL_TREE); |
20190 | out: |
20191 | input_location = loc; |
20192 | return r; |
20193 | #undef RECUR |
20194 | #undef RETURN |
20195 | } |
20196 | |
20197 | /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION |
20198 | function. For description of the body see comment above |
20199 | cp_parser_omp_declare_reduction_exprs. */ |
20200 | |
20201 | static void |
20202 | tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
20203 | { |
20204 | if (t == NULL_TREE || t == error_mark_node) |
20205 | return; |
20206 | |
20207 | gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl); |
20208 | |
20209 | tree_stmt_iterator tsi; |
20210 | int i; |
20211 | tree stmts[7]; |
20212 | memset (s: stmts, c: 0, n: sizeof stmts); |
20213 | for (i = 0, tsi = tsi_start (t); |
20214 | i < 7 && !tsi_end_p (i: tsi); |
20215 | i++, tsi_next (i: &tsi)) |
20216 | stmts[i] = tsi_stmt (i: tsi); |
20217 | gcc_assert (tsi_end_p (tsi)); |
20218 | |
20219 | if (i >= 3) |
20220 | { |
20221 | gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR |
20222 | && TREE_CODE (stmts[1]) == DECL_EXPR); |
20223 | tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]), |
20224 | args, complain, in_decl); |
20225 | tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]), |
20226 | args, complain, in_decl); |
20227 | /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we |
20228 | expect to be pushing it. */ |
20229 | DECL_CONTEXT (omp_out) = current_function_decl; |
20230 | DECL_CONTEXT (omp_in) = current_function_decl; |
20231 | keep_next_level (true); |
20232 | tree block = begin_omp_structured_block (); |
20233 | tsubst_stmt (t: stmts[2], args, complain, in_decl); |
20234 | block = finish_omp_structured_block (block); |
20235 | block = maybe_cleanup_point_expr_void (block); |
20236 | add_decl_expr (omp_out); |
20237 | copy_warning (omp_out, DECL_EXPR_DECL (stmts[0])); |
20238 | add_decl_expr (omp_in); |
20239 | finish_expr_stmt (block); |
20240 | } |
20241 | if (i >= 6) |
20242 | { |
20243 | gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR |
20244 | && TREE_CODE (stmts[4]) == DECL_EXPR); |
20245 | tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]), |
20246 | args, complain, in_decl); |
20247 | tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]), |
20248 | args, complain, in_decl); |
20249 | DECL_CONTEXT (omp_priv) = current_function_decl; |
20250 | DECL_CONTEXT (omp_orig) = current_function_decl; |
20251 | keep_next_level (true); |
20252 | tree block = begin_omp_structured_block (); |
20253 | tsubst_stmt (t: stmts[5], args, complain, in_decl); |
20254 | block = finish_omp_structured_block (block); |
20255 | block = maybe_cleanup_point_expr_void (block); |
20256 | cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL); |
20257 | add_decl_expr (omp_priv); |
20258 | add_decl_expr (omp_orig); |
20259 | finish_expr_stmt (block); |
20260 | if (i == 7) |
20261 | add_decl_expr (omp_orig); |
20262 | } |
20263 | } |
20264 | |
20265 | /* T is a postfix-expression that is not being used in a function |
20266 | call. Return the substituted version of T. */ |
20267 | |
20268 | static tree |
20269 | tsubst_non_call_postfix_expression (tree t, tree args, |
20270 | tsubst_flags_t complain, |
20271 | tree in_decl) |
20272 | { |
20273 | if (TREE_CODE (t) == SCOPE_REF) |
20274 | t = tsubst_qualified_id (qualified_id: t, args, complain, in_decl, |
20275 | /*done=*/false, /*address_p=*/false); |
20276 | else |
20277 | t = tsubst_expr (t, args, complain, in_decl); |
20278 | |
20279 | return t; |
20280 | } |
20281 | |
20282 | /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the |
20283 | LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously |
20284 | dependent init-capture. EXPLICIT_P is true if the original list had |
20285 | explicit captures. */ |
20286 | |
20287 | static void |
20288 | prepend_one_capture (tree field, tree init, tree &list, bool explicit_p, |
20289 | tsubst_flags_t complain) |
20290 | { |
20291 | if (tree auto_node = type_uses_auto (TREE_TYPE (field))) |
20292 | { |
20293 | tree type = NULL_TREE; |
20294 | if (!init) |
20295 | { |
20296 | if (complain & tf_error) |
20297 | error ("empty initializer in lambda init-capture"); |
20298 | init = error_mark_node; |
20299 | } |
20300 | else if (TREE_CODE (init) == TREE_LIST) |
20301 | init = build_x_compound_expr_from_list (init, ELK_INIT, complain); |
20302 | if (!type) |
20303 | type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain); |
20304 | TREE_TYPE (field) = type; |
20305 | cp_apply_type_quals_to_decl (cp_type_quals (type), field); |
20306 | } |
20307 | list = tree_cons (field, init, list); |
20308 | LAMBDA_CAPTURE_EXPLICIT_P (list) = explicit_p; |
20309 | } |
20310 | |
20311 | /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current |
20312 | instantiation context. Instantiating a pack expansion containing a lambda |
20313 | might result in multiple lambdas all based on the same lambda in the |
20314 | template. */ |
20315 | |
20316 | tree |
20317 | tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
20318 | { |
20319 | tree oldfn = lambda_function (t); |
20320 | in_decl = oldfn; |
20321 | |
20322 | args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl); |
20323 | if (processing_template_decl |
20324 | && (!in_template_context || (complain & tf_partial))) |
20325 | { |
20326 | /* Defer templated substitution into a lambda-expr if we lost the |
20327 | necessary template context. This may happen for a lambda-expr |
20328 | used as a default template argument. |
20329 | |
20330 | Defer dependent substitution as well so that we don't prematurely |
20331 | lower the level of a deduced return type or any other auto or |
20332 | template parameter belonging to the lambda. */ |
20333 | t = copy_node (t); |
20334 | LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE; |
20335 | LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (pattern: t, args, complain); |
20336 | return t; |
20337 | } |
20338 | |
20339 | tree r = build_lambda_expr (); |
20340 | |
20341 | LAMBDA_EXPR_LOCATION (r) |
20342 | = LAMBDA_EXPR_LOCATION (t); |
20343 | LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r) |
20344 | = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t); |
20345 | if (tree ti = LAMBDA_EXPR_REGEN_INFO (t)) |
20346 | LAMBDA_EXPR_REGEN_INFO (r) |
20347 | = build_template_info (template_decl: t, template_args: add_to_template_args (TI_ARGS (ti), |
20348 | extra_args: preserve_args (args))); |
20349 | else |
20350 | LAMBDA_EXPR_REGEN_INFO (r) |
20351 | = build_template_info (template_decl: t, template_args: preserve_args (args)); |
20352 | |
20353 | gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (t) == NULL); |
20354 | |
20355 | vec<tree,va_gc>* field_packs = NULL; |
20356 | unsigned name_independent_cnt = 0; |
20357 | for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap; |
20358 | cap = TREE_CHAIN (cap)) |
20359 | { |
20360 | tree ofield = TREE_PURPOSE (cap); |
20361 | tree init = TREE_VALUE (cap); |
20362 | if (PACK_EXPANSION_P (init)) |
20363 | init = tsubst_pack_expansion (t: init, args, complain, in_decl); |
20364 | else |
20365 | init = tsubst_expr (init, args, complain, in_decl); |
20366 | |
20367 | if (init == error_mark_node) |
20368 | return error_mark_node; |
20369 | |
20370 | if (init && TREE_CODE (init) == TREE_LIST) |
20371 | init = build_x_compound_expr_from_list (init, ELK_INIT, complain); |
20372 | |
20373 | if (!processing_template_decl |
20374 | && init && TREE_CODE (init) != TREE_VEC |
20375 | && variably_modified_type_p (TREE_TYPE (init), NULL_TREE)) |
20376 | { |
20377 | /* For a VLA, simply tsubsting the field type won't work, we need to |
20378 | go through add_capture again. XXX do we want to do this for all |
20379 | captures? */ |
20380 | tree name = (get_identifier |
20381 | (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2)); |
20382 | tree ftype = TREE_TYPE (ofield); |
20383 | bool by_ref = (TYPE_REF_P (ftype) |
20384 | || (TREE_CODE (ftype) == DECLTYPE_TYPE |
20385 | && DECLTYPE_FOR_REF_CAPTURE (ftype))); |
20386 | add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield), |
20387 | &name_independent_cnt); |
20388 | continue; |
20389 | } |
20390 | |
20391 | if (PACK_EXPANSION_P (ofield)) |
20392 | ofield = PACK_EXPANSION_PATTERN (ofield); |
20393 | tree field = tsubst_decl (t: ofield, args, complain); |
20394 | |
20395 | if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield)) |
20396 | { |
20397 | /* Remember these for when we've pushed local_specializations. */ |
20398 | vec_safe_push (v&: field_packs, obj: ofield); |
20399 | vec_safe_push (v&: field_packs, obj: field); |
20400 | } |
20401 | |
20402 | if (field == error_mark_node) |
20403 | return error_mark_node; |
20404 | |
20405 | if (TREE_CODE (field) == TREE_VEC) |
20406 | { |
20407 | int len = TREE_VEC_LENGTH (field); |
20408 | gcc_assert (TREE_CODE (init) == TREE_VEC |
20409 | && TREE_VEC_LENGTH (init) == len); |
20410 | for (int i = 0; i < len; ++i) |
20411 | prepend_one_capture (TREE_VEC_ELT (field, i), |
20412 | TREE_VEC_ELT (init, i), |
20413 | LAMBDA_EXPR_CAPTURE_LIST (r), |
20414 | LAMBDA_CAPTURE_EXPLICIT_P (cap), |
20415 | complain); |
20416 | } |
20417 | else |
20418 | { |
20419 | prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r), |
20420 | LAMBDA_CAPTURE_EXPLICIT_P (cap), complain); |
20421 | |
20422 | if (id_equal (DECL_NAME (field), str: "__this")) |
20423 | LAMBDA_EXPR_THIS_CAPTURE (r) = field; |
20424 | } |
20425 | } |
20426 | |
20427 | tree type = begin_lambda_type (r); |
20428 | if (type == error_mark_node) |
20429 | { |
20430 | gcc_checking_assert (!(complain & tf_error) || seen_error ()); |
20431 | return error_mark_node; |
20432 | } |
20433 | |
20434 | if (LAMBDA_EXPR_EXTRA_SCOPE (t)) |
20435 | record_lambda_scope (lambda: r); |
20436 | if (TYPE_NAMESPACE_SCOPE_P (TREE_TYPE (t))) |
20437 | /* If we're pushed into another scope (PR105652), fix it. */ |
20438 | TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_NAME (type)) |
20439 | = TYPE_CONTEXT (TREE_TYPE (t)); |
20440 | record_lambda_scope_discriminator (lambda: r); |
20441 | |
20442 | /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */ |
20443 | determine_visibility (TYPE_NAME (type)); |
20444 | |
20445 | register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r)); |
20446 | |
20447 | tree oldtmpl = (generic_lambda_fn_p (oldfn) |
20448 | ? DECL_TI_TEMPLATE (oldfn) |
20449 | : NULL_TREE); |
20450 | |
20451 | tree tparms = NULL_TREE; |
20452 | if (oldtmpl) |
20453 | tparms = tsubst_template_parms (DECL_TEMPLATE_PARMS (oldtmpl), args, complain); |
20454 | |
20455 | tree fntype = static_fn_type (oldfn); |
20456 | |
20457 | tree saved_ctp = current_template_parms; |
20458 | if (oldtmpl) |
20459 | { |
20460 | ++processing_template_decl; |
20461 | current_template_parms = tparms; |
20462 | } |
20463 | fntype = tsubst (t: fntype, args, complain, in_decl); |
20464 | if (oldtmpl) |
20465 | { |
20466 | current_template_parms = saved_ctp; |
20467 | --processing_template_decl; |
20468 | } |
20469 | |
20470 | if (fntype == error_mark_node) |
20471 | r = error_mark_node; |
20472 | else |
20473 | { |
20474 | /* The body of a lambda-expression is not a subexpression of the |
20475 | enclosing expression. Parms are to have DECL_CHAIN tsubsted, |
20476 | which would be skipped if cp_unevaluated_operand. */ |
20477 | cp_evaluated ev; |
20478 | |
20479 | /* Fix the type of 'this'. |
20480 | For static and xobj member functions we use this to transport the |
20481 | lambda's closure type. It appears that in the regular case the |
20482 | object parameter is still pulled off, and then re-added again anyway. |
20483 | So perhaps we could do something better here? */ |
20484 | fntype = build_memfn_type (fntype, type, |
20485 | type_memfn_quals (fntype), |
20486 | type_memfn_rqual (fntype)); |
20487 | tree inst = (oldtmpl |
20488 | ? tsubst_template_decl (t: oldtmpl, args, complain, |
20489 | lambda_fntype: fntype, lambda_tparms: tparms) |
20490 | : tsubst_function_decl (t: oldfn, args, complain, lambda_fntype: fntype)); |
20491 | if (inst == error_mark_node) |
20492 | { |
20493 | r = error_mark_node; |
20494 | goto out; |
20495 | } |
20496 | finish_member_declaration (inst); |
20497 | record_lambda_scope_sig_discriminator (lambda: r, fn: inst); |
20498 | |
20499 | tree fn = oldtmpl ? DECL_TEMPLATE_RESULT (inst) : inst; |
20500 | |
20501 | /* Let finish_function set this. */ |
20502 | DECL_DECLARED_CONSTEXPR_P (fn) = false; |
20503 | |
20504 | bool nested = cfun; |
20505 | if (nested) |
20506 | push_function_context (); |
20507 | else |
20508 | /* Still increment function_depth so that we don't GC in the |
20509 | middle of an expression. */ |
20510 | ++function_depth; |
20511 | |
20512 | local_specialization_stack s (lss_copy); |
20513 | |
20514 | bool save_in_consteval_if_p = in_consteval_if_p; |
20515 | in_consteval_if_p = false; |
20516 | |
20517 | tree body = start_lambda_function (fn, lambda_expr: r); |
20518 | |
20519 | /* Now record them for lookup_init_capture_pack. */ |
20520 | int fplen = vec_safe_length (v: field_packs); |
20521 | for (int i = 0; i < fplen; ) |
20522 | { |
20523 | tree pack = (*field_packs)[i++]; |
20524 | tree inst = (*field_packs)[i++]; |
20525 | register_local_specialization (spec: inst, tmpl: pack); |
20526 | } |
20527 | release_tree_vector (field_packs); |
20528 | |
20529 | register_parameter_specializations (oldfn, fn); |
20530 | |
20531 | if (oldtmpl) |
20532 | { |
20533 | /* We might not partially instantiate some parts of the function, so |
20534 | copy these flags from the original template. */ |
20535 | language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language; |
20536 | current_function_returns_value = ol->returns_value; |
20537 | current_function_returns_null = ol->returns_null; |
20538 | current_function_returns_abnormally = ol->returns_abnormally; |
20539 | current_function_infinite_loop = ol->infinite_loop; |
20540 | } |
20541 | |
20542 | /* [temp.deduct] A lambda-expression appearing in a function type or a |
20543 | template parameter is not considered part of the immediate context for |
20544 | the purposes of template argument deduction. */ |
20545 | complain = tf_warning_or_error; |
20546 | |
20547 | tree saved = DECL_SAVED_TREE (oldfn); |
20548 | if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved)) |
20549 | /* We already have a body block from start_lambda_function, we don't |
20550 | need another to confuse NRV (91217). */ |
20551 | saved = BIND_EXPR_BODY (saved); |
20552 | |
20553 | tsubst_stmt (t: saved, args, complain, in_decl: r); |
20554 | |
20555 | finish_lambda_function (body); |
20556 | |
20557 | in_consteval_if_p = save_in_consteval_if_p; |
20558 | |
20559 | if (nested) |
20560 | pop_function_context (); |
20561 | else |
20562 | --function_depth; |
20563 | |
20564 | /* The capture list was built up in reverse order; fix that now. */ |
20565 | LAMBDA_EXPR_CAPTURE_LIST (r) |
20566 | = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r)); |
20567 | |
20568 | maybe_add_lambda_conv_op (type); |
20569 | } |
20570 | |
20571 | out: |
20572 | finish_struct (type, /*attr*/NULL_TREE); |
20573 | |
20574 | insert_pending_capture_proxies (); |
20575 | |
20576 | return r; |
20577 | } |
20578 | |
20579 | /* Subroutine of maybe_fold_fn_template_args. */ |
20580 | |
20581 | static bool |
20582 | fold_targs_r (tree targs, tsubst_flags_t complain) |
20583 | { |
20584 | int len = TREE_VEC_LENGTH (targs); |
20585 | for (int i = 0; i < len; ++i) |
20586 | { |
20587 | tree &elt = TREE_VEC_ELT (targs, i); |
20588 | if (!elt || TYPE_P (elt) |
20589 | || TREE_CODE (elt) == TEMPLATE_DECL) |
20590 | continue; |
20591 | if (TREE_CODE (elt) == NONTYPE_ARGUMENT_PACK) |
20592 | { |
20593 | if (!fold_targs_r (ARGUMENT_PACK_ARGS (elt), complain)) |
20594 | return false; |
20595 | } |
20596 | else if (/* We can only safely preevaluate scalar prvalues. */ |
20597 | SCALAR_TYPE_P (TREE_TYPE (elt)) |
20598 | && !glvalue_p (elt) |
20599 | && !TREE_CONSTANT (elt)) |
20600 | { |
20601 | elt = cxx_constant_value (t: elt, complain); |
20602 | if (elt == error_mark_node) |
20603 | return false; |
20604 | } |
20605 | } |
20606 | |
20607 | return true; |
20608 | } |
20609 | |
20610 | /* Try to do constant evaluation of any explicit template arguments in FN |
20611 | before overload resolution, to get any errors only once. Return true iff |
20612 | we didn't have any problems folding. */ |
20613 | |
20614 | static bool |
20615 | maybe_fold_fn_template_args (tree fn, tsubst_flags_t complain) |
20616 | { |
20617 | if (processing_template_decl || fn == NULL_TREE) |
20618 | return true; |
20619 | if (fn == error_mark_node) |
20620 | return false; |
20621 | if (TREE_CODE (fn) == OFFSET_REF |
20622 | || TREE_CODE (fn) == COMPONENT_REF) |
20623 | fn = TREE_OPERAND (fn, 1); |
20624 | if (BASELINK_P (fn)) |
20625 | fn = BASELINK_FUNCTIONS (fn); |
20626 | if (TREE_CODE (fn) != TEMPLATE_ID_EXPR) |
20627 | return true; |
20628 | tree targs = TREE_OPERAND (fn, 1); |
20629 | if (targs == NULL_TREE) |
20630 | return true; |
20631 | if (targs == error_mark_node) |
20632 | return false; |
20633 | return fold_targs_r (targs, complain); |
20634 | } |
20635 | |
20636 | /* Helper function for tsubst_expr CALL_EXPR and ARRAY_REF handling. */ |
20637 | |
20638 | static void |
20639 | tsubst_call_args (tree t, tree args, tsubst_flags_t complain, |
20640 | tree in_decl, releasing_vec &call_args) |
20641 | { |
20642 | unsigned int nargs = call_expr_nargs (t); |
20643 | for (unsigned int i = 0; i < nargs; ++i) |
20644 | { |
20645 | tree arg = CALL_EXPR_ARG (t, i); |
20646 | |
20647 | if (!PACK_EXPANSION_P (arg)) |
20648 | vec_safe_push (r&: call_args, t: tsubst_expr (arg, args, complain, in_decl)); |
20649 | else |
20650 | { |
20651 | /* Expand the pack expansion and push each entry onto CALL_ARGS. */ |
20652 | arg = tsubst_pack_expansion (t: arg, args, complain, in_decl); |
20653 | if (TREE_CODE (arg) == TREE_VEC) |
20654 | { |
20655 | unsigned int len, j; |
20656 | |
20657 | len = TREE_VEC_LENGTH (arg); |
20658 | for (j = 0; j < len; ++j) |
20659 | { |
20660 | tree value = TREE_VEC_ELT (arg, j); |
20661 | if (value != NULL_TREE) |
20662 | value = convert_from_reference (value); |
20663 | vec_safe_push (r&: call_args, t: value); |
20664 | } |
20665 | } |
20666 | else |
20667 | /* A partial substitution. Add one entry. */ |
20668 | vec_safe_push (r&: call_args, t: arg); |
20669 | } |
20670 | } |
20671 | } |
20672 | |
20673 | /* Like tsubst but deals with expressions and performs semantic |
20674 | analysis. */ |
20675 | |
20676 | tree |
20677 | tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) |
20678 | { |
20679 | #define RETURN(EXP) do { retval = (EXP); goto out; } while(0) |
20680 | #define RECUR(NODE) \ |
20681 | tsubst_expr (NODE, args, complain, in_decl) |
20682 | |
20683 | tree retval, op1; |
20684 | location_t save_loc; |
20685 | |
20686 | if (t == NULL_TREE || t == error_mark_node) |
20687 | return t; |
20688 | |
20689 | save_loc = input_location; |
20690 | if (location_t eloc = cp_expr_location (t_: t)) |
20691 | input_location = eloc; |
20692 | |
20693 | /* N3276 decltype magic only applies to calls at the top level or on the |
20694 | right side of a comma. */ |
20695 | tsubst_flags_t decltype_flag = (complain & tf_decltype); |
20696 | complain &= ~tf_decltype; |
20697 | |
20698 | /* This flag only applies to id-expressions at the top level, and |
20699 | controls resolution thereof. */ |
20700 | tsubst_flags_t no_name_lookup_flag = (complain & tf_no_name_lookup); |
20701 | complain &= ~tf_no_name_lookup; |
20702 | |
20703 | if (!no_name_lookup_flag) |
20704 | if (tree d = maybe_dependent_member_ref (t, args, complain, in_decl)) |
20705 | return d; |
20706 | |
20707 | switch (TREE_CODE (t)) |
20708 | { |
20709 | case USING_DECL: |
20710 | t = DECL_NAME (t); |
20711 | /* Fall through. */ |
20712 | case IDENTIFIER_NODE: |
20713 | { |
20714 | tree decl; |
20715 | cp_id_kind idk; |
20716 | const char *error_msg; |
20717 | |
20718 | if (IDENTIFIER_CONV_OP_P (t)) |
20719 | { |
20720 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
20721 | t = make_conv_op_name (new_type); |
20722 | } |
20723 | |
20724 | if (no_name_lookup_flag) |
20725 | RETURN (t); |
20726 | |
20727 | /* Look up the name. */ |
20728 | decl = lookup_name (name: t); |
20729 | |
20730 | /* By convention, expressions use ERROR_MARK_NODE to indicate |
20731 | failure, not NULL_TREE. */ |
20732 | if (decl == NULL_TREE) |
20733 | decl = error_mark_node; |
20734 | |
20735 | decl = finish_id_expression (t, decl, NULL_TREE, |
20736 | &idk, |
20737 | /*i_c_e_p=*/false, |
20738 | /*allow_i_c_e_p=*/true, |
20739 | /*non_i_c_e_p=*/nullptr, |
20740 | /*template_p=*/false, |
20741 | /*done=*/true, |
20742 | /*address_p=*/false, |
20743 | /*template_arg_p=*/false, |
20744 | &error_msg, |
20745 | input_location); |
20746 | if (error_msg) |
20747 | error (error_msg); |
20748 | if (identifier_p (t: decl)) |
20749 | { |
20750 | if (complain & tf_error) |
20751 | unqualified_name_lookup_error (decl); |
20752 | decl = error_mark_node; |
20753 | } |
20754 | RETURN (decl); |
20755 | } |
20756 | |
20757 | case TEMPLATE_ID_EXPR: |
20758 | { |
20759 | tree object; |
20760 | tree templ = TREE_OPERAND (t, 0); |
20761 | tree targs = TREE_OPERAND (t, 1); |
20762 | |
20763 | if (no_name_lookup_flag) |
20764 | templ = tsubst_name (t: templ, args, complain, in_decl); |
20765 | else |
20766 | templ = tsubst_expr (t: templ, args, complain, in_decl); |
20767 | |
20768 | if (targs) |
20769 | targs = tsubst_template_args (t: targs, args, complain, in_decl); |
20770 | if (targs == error_mark_node) |
20771 | RETURN (error_mark_node); |
20772 | |
20773 | if (TREE_CODE (templ) == SCOPE_REF) |
20774 | { |
20775 | tree name = TREE_OPERAND (templ, 1); |
20776 | tree tid = lookup_template_function (fns: name, arglist: targs); |
20777 | TREE_OPERAND (templ, 1) = tid; |
20778 | RETURN (templ); |
20779 | } |
20780 | |
20781 | if (concept_definition_p (t: templ)) |
20782 | { |
20783 | tree check = build_concept_check (templ, targs, complain); |
20784 | if (check == error_mark_node) |
20785 | RETURN (error_mark_node); |
20786 | RETURN (check); |
20787 | } |
20788 | |
20789 | if (variable_template_p (t: templ)) |
20790 | { |
20791 | if (no_name_lookup_flag) |
20792 | RETURN (lookup_template_variable (templ, targs, complain)); |
20793 | |
20794 | tree r = lookup_and_finish_template_variable (templ, targs, |
20795 | complain); |
20796 | r = convert_from_reference (r); |
20797 | r = maybe_wrap_with_location (r, EXPR_LOCATION (t)); |
20798 | RETURN (r); |
20799 | } |
20800 | |
20801 | if (TREE_CODE (templ) == COMPONENT_REF) |
20802 | { |
20803 | object = TREE_OPERAND (templ, 0); |
20804 | templ = TREE_OPERAND (templ, 1); |
20805 | } |
20806 | else |
20807 | object = NULL_TREE; |
20808 | |
20809 | tree tid = lookup_template_function (fns: templ, arglist: targs); |
20810 | protected_set_expr_location (tid, EXPR_LOCATION (t)); |
20811 | |
20812 | if (object) |
20813 | RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid), |
20814 | object, tid, NULL_TREE)); |
20815 | else if (no_name_lookup_flag) |
20816 | RETURN (tid); |
20817 | else if (identifier_p (t: templ)) |
20818 | { |
20819 | /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when |
20820 | name lookup found nothing when parsing the template name. */ |
20821 | gcc_assert (cxx_dialect >= cxx20 || seen_error ()); |
20822 | RETURN (tid); |
20823 | } |
20824 | else |
20825 | RETURN (baselink_for_fns (tid)); |
20826 | } |
20827 | |
20828 | case INDIRECT_REF: |
20829 | { |
20830 | tree r = RECUR (TREE_OPERAND (t, 0)); |
20831 | |
20832 | if (REFERENCE_REF_P (t)) |
20833 | { |
20834 | /* A type conversion to reference type will be enclosed in |
20835 | such an indirect ref, but the substitution of the cast |
20836 | will have also added such an indirect ref. */ |
20837 | r = convert_from_reference (r); |
20838 | } |
20839 | else |
20840 | r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, |
20841 | templated_operator_saved_lookups (t), |
20842 | complain|decltype_flag); |
20843 | |
20844 | if (REF_PARENTHESIZED_P (t)) |
20845 | r = force_paren_expr (r); |
20846 | |
20847 | RETURN (r); |
20848 | } |
20849 | |
20850 | case MEM_REF: |
20851 | { |
20852 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
20853 | tree op1 = RECUR (TREE_OPERAND (t, 0)); |
20854 | tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
20855 | RETURN (build2_loc (EXPR_LOCATION (t), MEM_REF, new_type, op0, op1)); |
20856 | } |
20857 | |
20858 | case NOP_EXPR: |
20859 | { |
20860 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
20861 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
20862 | RETURN (build_nop (type, op0)); |
20863 | } |
20864 | |
20865 | case IMPLICIT_CONV_EXPR: |
20866 | { |
20867 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
20868 | if (type == error_mark_node) |
20869 | RETURN (error_mark_node); |
20870 | tree expr = RECUR (TREE_OPERAND (t, 0)); |
20871 | if (dependent_type_p (type) || type_dependent_expression_p (expr)) |
20872 | { |
20873 | retval = copy_node (t); |
20874 | TREE_TYPE (retval) = type; |
20875 | TREE_OPERAND (retval, 0) = expr; |
20876 | RETURN (retval); |
20877 | } |
20878 | if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t)) |
20879 | { |
20880 | tree r = convert_nontype_argument (type, expr, complain); |
20881 | if (r == NULL_TREE) |
20882 | r = error_mark_node; |
20883 | RETURN (r); |
20884 | } |
20885 | int flags = LOOKUP_IMPLICIT; |
20886 | if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t)) |
20887 | flags = LOOKUP_NORMAL; |
20888 | if (IMPLICIT_CONV_EXPR_BRACED_INIT (t)) |
20889 | flags |= LOOKUP_NO_NARROWING; |
20890 | RETURN (perform_implicit_conversion_flags (type, expr, complain, |
20891 | flags)); |
20892 | } |
20893 | |
20894 | case CONVERT_EXPR: |
20895 | { |
20896 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
20897 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
20898 | if (op0 == error_mark_node) |
20899 | RETURN (error_mark_node); |
20900 | RETURN (build1 (CONVERT_EXPR, type, op0)); |
20901 | } |
20902 | |
20903 | case CAST_EXPR: |
20904 | case REINTERPRET_CAST_EXPR: |
20905 | case CONST_CAST_EXPR: |
20906 | case DYNAMIC_CAST_EXPR: |
20907 | case STATIC_CAST_EXPR: |
20908 | { |
20909 | tree type; |
20910 | tree op, r = NULL_TREE; |
20911 | |
20912 | tsubst_flags_t tcomplain = complain; |
20913 | if (TREE_CODE (t) == CAST_EXPR) |
20914 | tcomplain |= tf_tst_ok; |
20915 | type = tsubst (TREE_TYPE (t), args, complain: tcomplain, in_decl); |
20916 | |
20917 | op = RECUR (TREE_OPERAND (t, 0)); |
20918 | |
20919 | warning_sentinel s(warn_useless_cast); |
20920 | warning_sentinel s2(warn_ignored_qualifiers); |
20921 | warning_sentinel s3(warn_int_in_bool_context); |
20922 | switch (TREE_CODE (t)) |
20923 | { |
20924 | case CAST_EXPR: |
20925 | r = build_functional_cast (input_location, type, op, complain); |
20926 | break; |
20927 | case REINTERPRET_CAST_EXPR: |
20928 | r = build_reinterpret_cast (input_location, type, op, complain); |
20929 | break; |
20930 | case CONST_CAST_EXPR: |
20931 | r = build_const_cast (input_location, type, op, complain); |
20932 | break; |
20933 | case DYNAMIC_CAST_EXPR: |
20934 | r = build_dynamic_cast (input_location, type, op, complain); |
20935 | break; |
20936 | case STATIC_CAST_EXPR: |
20937 | r = build_static_cast (input_location, type, op, complain); |
20938 | if (IMPLICIT_RVALUE_P (t)) |
20939 | set_implicit_rvalue_p (r); |
20940 | break; |
20941 | default: |
20942 | gcc_unreachable (); |
20943 | } |
20944 | |
20945 | RETURN (r); |
20946 | } |
20947 | |
20948 | case BIT_CAST_EXPR: |
20949 | { |
20950 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
20951 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
20952 | RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain)); |
20953 | } |
20954 | |
20955 | case POSTDECREMENT_EXPR: |
20956 | case POSTINCREMENT_EXPR: |
20957 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
20958 | args, complain, in_decl); |
20959 | RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1, |
20960 | templated_operator_saved_lookups (t), |
20961 | complain|decltype_flag)); |
20962 | |
20963 | case BIT_NOT_EXPR: |
20964 | if (identifier_p (TREE_OPERAND (t, 0))) |
20965 | { |
20966 | gcc_checking_assert (no_name_lookup_flag); |
20967 | RETURN (t); |
20968 | } |
20969 | else if (TYPE_P (TREE_OPERAND (t, 0))) |
20970 | { |
20971 | gcc_checking_assert (no_name_lookup_flag); |
20972 | tree op0 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl); |
20973 | RETURN (build_min_nt_loc (EXPR_LOCATION (t), BIT_NOT_EXPR, op0)); |
20974 | } |
20975 | /* Fall through. */ |
20976 | case PREDECREMENT_EXPR: |
20977 | case PREINCREMENT_EXPR: |
20978 | case NEGATE_EXPR: |
20979 | case ABS_EXPR: |
20980 | case TRUTH_NOT_EXPR: |
20981 | case UNARY_PLUS_EXPR: /* Unary + */ |
20982 | case REALPART_EXPR: |
20983 | case IMAGPART_EXPR: |
20984 | RETURN (build_x_unary_op (input_location, TREE_CODE (t), |
20985 | RECUR (TREE_OPERAND (t, 0)), |
20986 | templated_operator_saved_lookups (t), |
20987 | complain|decltype_flag)); |
20988 | |
20989 | case EXCESS_PRECISION_EXPR: |
20990 | { |
20991 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
20992 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
20993 | if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR) |
20994 | RETURN (op0); |
20995 | RETURN (build1_loc (EXPR_LOCATION (t), EXCESS_PRECISION_EXPR, |
20996 | type, op0)); |
20997 | } |
20998 | |
20999 | case FIX_TRUNC_EXPR: |
21000 | /* convert_like should have created an IMPLICIT_CONV_EXPR. */ |
21001 | gcc_unreachable (); |
21002 | |
21003 | case ADDR_EXPR: |
21004 | op1 = TREE_OPERAND (t, 0); |
21005 | if (TREE_CODE (op1) == LABEL_DECL) |
21006 | RETURN (finish_label_address_expr (DECL_NAME (op1), |
21007 | EXPR_LOCATION (op1))); |
21008 | if (TREE_CODE (op1) == SCOPE_REF) |
21009 | op1 = tsubst_qualified_id (qualified_id: op1, args, complain, in_decl, |
21010 | /*done=*/true, /*address_p=*/true); |
21011 | else |
21012 | op1 = tsubst_non_call_postfix_expression (t: op1, args, complain, |
21013 | in_decl); |
21014 | RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1, |
21015 | templated_operator_saved_lookups (t), |
21016 | complain|decltype_flag)); |
21017 | |
21018 | case PLUS_EXPR: |
21019 | case MINUS_EXPR: |
21020 | case MULT_EXPR: |
21021 | case TRUNC_DIV_EXPR: |
21022 | case CEIL_DIV_EXPR: |
21023 | case FLOOR_DIV_EXPR: |
21024 | case ROUND_DIV_EXPR: |
21025 | case EXACT_DIV_EXPR: |
21026 | case BIT_AND_EXPR: |
21027 | case BIT_IOR_EXPR: |
21028 | case BIT_XOR_EXPR: |
21029 | case TRUNC_MOD_EXPR: |
21030 | case FLOOR_MOD_EXPR: |
21031 | case TRUTH_ANDIF_EXPR: |
21032 | case TRUTH_ORIF_EXPR: |
21033 | case TRUTH_AND_EXPR: |
21034 | case TRUTH_OR_EXPR: |
21035 | case RSHIFT_EXPR: |
21036 | case LSHIFT_EXPR: |
21037 | case EQ_EXPR: |
21038 | case NE_EXPR: |
21039 | case MAX_EXPR: |
21040 | case MIN_EXPR: |
21041 | case LE_EXPR: |
21042 | case GE_EXPR: |
21043 | case LT_EXPR: |
21044 | case GT_EXPR: |
21045 | case SPACESHIP_EXPR: |
21046 | case MEMBER_REF: |
21047 | case DOTSTAR_EXPR: |
21048 | { |
21049 | /* If either OP0 or OP1 was value- or type-dependent, suppress |
21050 | warnings that depend on the range of the types involved. */ |
21051 | tree op0 = TREE_OPERAND (t, 0); |
21052 | tree op1 = TREE_OPERAND (t, 1); |
21053 | const bool was_dep = (dependent_operand_p (t: op0) |
21054 | || dependent_operand_p (t: op1)); |
21055 | op0 = RECUR (op0); |
21056 | op1 = RECUR (op1); |
21057 | |
21058 | warning_sentinel s1(warn_type_limits, was_dep); |
21059 | warning_sentinel s2(warn_div_by_zero, was_dep); |
21060 | warning_sentinel s3(warn_logical_op, was_dep); |
21061 | warning_sentinel s4(warn_tautological_compare, was_dep); |
21062 | warning_sentinel s5(warn_address, was_dep); |
21063 | |
21064 | tree r = build_x_binary_op |
21065 | (input_location, TREE_CODE (t), |
21066 | op0, |
21067 | (warning_suppressed_p (TREE_OPERAND (t, 0)) |
21068 | ? ERROR_MARK |
21069 | : TREE_CODE (TREE_OPERAND (t, 0))), |
21070 | op1, |
21071 | (warning_suppressed_p (TREE_OPERAND (t, 1)) |
21072 | ? ERROR_MARK |
21073 | : TREE_CODE (TREE_OPERAND (t, 1))), |
21074 | templated_operator_saved_lookups (t), |
21075 | /*overload=*/NULL, |
21076 | complain|decltype_flag); |
21077 | if (EXPR_P (r)) |
21078 | copy_warning (r, t); |
21079 | |
21080 | RETURN (r); |
21081 | } |
21082 | |
21083 | case POINTER_PLUS_EXPR: |
21084 | { |
21085 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
21086 | if (op0 == error_mark_node) |
21087 | RETURN (error_mark_node); |
21088 | tree op1 = RECUR (TREE_OPERAND (t, 1)); |
21089 | if (op1 == error_mark_node) |
21090 | RETURN (error_mark_node); |
21091 | RETURN (fold_build_pointer_plus (op0, op1)); |
21092 | } |
21093 | |
21094 | case SCOPE_REF: |
21095 | if (no_name_lookup_flag) |
21096 | { |
21097 | tree op0 = tsubst_scope (TREE_OPERAND (t, 0), args, complain, in_decl); |
21098 | tree op1 = tsubst_name (TREE_OPERAND (t, 1), args, complain, in_decl); |
21099 | RETURN (build_qualified_name (/*type=*/NULL_TREE, op0, op1, |
21100 | QUALIFIED_NAME_IS_TEMPLATE (t))); |
21101 | } |
21102 | else |
21103 | RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true, |
21104 | /*address_p=*/false)); |
21105 | |
21106 | case BASELINK: |
21107 | RETURN (tsubst_baselink (t, current_nonlambda_class_type (), |
21108 | args, complain, in_decl)); |
21109 | |
21110 | case ARRAY_REF: |
21111 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
21112 | args, complain, in_decl); |
21113 | if (TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR |
21114 | && (CALL_EXPR_FN (TREE_OPERAND (t, 1)) |
21115 | == ovl_op_identifier (code: ARRAY_REF))) |
21116 | { |
21117 | tree c = TREE_OPERAND (t, 1); |
21118 | releasing_vec index_exp_list; |
21119 | tsubst_call_args (t: c, args, complain, in_decl, call_args&: index_exp_list); |
21120 | |
21121 | tree r; |
21122 | if (vec_safe_length (r&: index_exp_list) == 1 |
21123 | && !PACK_EXPANSION_P (index_exp_list[0])) |
21124 | r = grok_array_decl (EXPR_LOCATION (t), op1, |
21125 | index_exp_list[0], NULL, |
21126 | complain | decltype_flag); |
21127 | else |
21128 | r = grok_array_decl (EXPR_LOCATION (t), op1, |
21129 | NULL_TREE, &index_exp_list, |
21130 | complain | decltype_flag); |
21131 | RETURN (r); |
21132 | } |
21133 | RETURN (build_x_array_ref (EXPR_LOCATION (t), op1, |
21134 | RECUR (TREE_OPERAND (t, 1)), |
21135 | complain|decltype_flag)); |
21136 | |
21137 | case OMP_ARRAY_SECTION: |
21138 | { |
21139 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
21140 | tree op1 = NULL_TREE, op2 = NULL_TREE; |
21141 | if (op0 == error_mark_node) |
21142 | RETURN (error_mark_node); |
21143 | if (TREE_OPERAND (t, 1)) |
21144 | { |
21145 | op1 = RECUR (TREE_OPERAND (t, 1)); |
21146 | if (op1 == error_mark_node) |
21147 | RETURN (error_mark_node); |
21148 | } |
21149 | if (TREE_OPERAND (t, 2)) |
21150 | { |
21151 | op2 = RECUR (TREE_OPERAND (t, 2)); |
21152 | if (op2 == error_mark_node) |
21153 | RETURN (error_mark_node); |
21154 | } |
21155 | RETURN (build_omp_array_section (EXPR_LOCATION (t), op0, op1, op2)); |
21156 | } |
21157 | |
21158 | case OMP_DECLARE_MAPPER: |
21159 | { |
21160 | t = copy_node (t); |
21161 | |
21162 | tree decl = OMP_DECLARE_MAPPER_DECL (t); |
21163 | DECL_OMP_DECLARE_MAPPER_P (decl) = 1; |
21164 | decl = tsubst (t: decl, args, complain, in_decl); |
21165 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
21166 | tree clauses = OMP_DECLARE_MAPPER_CLAUSES (t); |
21167 | clauses = tsubst_omp_clauses (clauses, ort: C_ORT_OMP_DECLARE_MAPPER, args, |
21168 | complain, in_decl); |
21169 | TREE_TYPE (t) = type; |
21170 | OMP_DECLARE_MAPPER_DECL (t) = decl; |
21171 | OMP_DECLARE_MAPPER_CLAUSES (t) = clauses; |
21172 | RETURN (t); |
21173 | } |
21174 | |
21175 | case SIZEOF_EXPR: |
21176 | if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)) |
21177 | || ARGUMENT_PACK_P (TREE_OPERAND (t, 0))) |
21178 | { |
21179 | tree expanded, op = TREE_OPERAND (t, 0); |
21180 | int len = 0; |
21181 | |
21182 | if (SIZEOF_EXPR_TYPE_P (t)) |
21183 | op = TREE_TYPE (op); |
21184 | |
21185 | ++cp_unevaluated_operand; |
21186 | ++c_inhibit_evaluation_warnings; |
21187 | /* We only want to compute the number of arguments. */ |
21188 | if (PACK_EXPANSION_P (op)) |
21189 | expanded = tsubst_pack_expansion (t: op, args, complain, in_decl); |
21190 | else |
21191 | expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op), |
21192 | args, complain, in_decl); |
21193 | --cp_unevaluated_operand; |
21194 | --c_inhibit_evaluation_warnings; |
21195 | |
21196 | if (TREE_CODE (expanded) == TREE_VEC) |
21197 | { |
21198 | len = TREE_VEC_LENGTH (expanded); |
21199 | /* Set TREE_USED for the benefit of -Wunused. */ |
21200 | for (int i = 0; i < len; i++) |
21201 | if (DECL_P (TREE_VEC_ELT (expanded, i))) |
21202 | TREE_USED (TREE_VEC_ELT (expanded, i)) = true; |
21203 | } |
21204 | |
21205 | if (expanded == error_mark_node) |
21206 | RETURN (error_mark_node); |
21207 | else if (PACK_EXPANSION_P (expanded) |
21208 | || (TREE_CODE (expanded) == TREE_VEC |
21209 | && pack_expansion_args_count (args: expanded))) |
21210 | |
21211 | { |
21212 | if (PACK_EXPANSION_P (expanded)) |
21213 | /* OK. */; |
21214 | else |
21215 | expanded = make_argument_pack (vec: expanded); |
21216 | |
21217 | if (TYPE_P (expanded)) |
21218 | RETURN (cxx_sizeof_or_alignof_type (input_location, |
21219 | expanded, SIZEOF_EXPR, |
21220 | false, |
21221 | complain & tf_error)); |
21222 | else |
21223 | RETURN (cxx_sizeof_or_alignof_expr (input_location, |
21224 | expanded, SIZEOF_EXPR, |
21225 | false, |
21226 | complain & tf_error)); |
21227 | } |
21228 | else |
21229 | RETURN (build_int_cst (size_type_node, len)); |
21230 | } |
21231 | /* Fall through */ |
21232 | |
21233 | case ALIGNOF_EXPR: |
21234 | { |
21235 | tree r; |
21236 | |
21237 | op1 = TREE_OPERAND (t, 0); |
21238 | if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t)) |
21239 | op1 = TREE_TYPE (op1); |
21240 | bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR |
21241 | && ALIGNOF_EXPR_STD_P (t)); |
21242 | if (!args) |
21243 | { |
21244 | /* When there are no ARGS, we are trying to evaluate a |
21245 | non-dependent expression from the parser. Trying to do |
21246 | the substitutions may not work. */ |
21247 | if (!TYPE_P (op1)) |
21248 | op1 = TREE_TYPE (op1); |
21249 | } |
21250 | else |
21251 | { |
21252 | ++cp_unevaluated_operand; |
21253 | ++c_inhibit_evaluation_warnings; |
21254 | if (TYPE_P (op1)) |
21255 | op1 = tsubst (t: op1, args, complain, in_decl); |
21256 | else |
21257 | op1 = tsubst_expr (t: op1, args, complain, in_decl); |
21258 | --cp_unevaluated_operand; |
21259 | --c_inhibit_evaluation_warnings; |
21260 | } |
21261 | if (TYPE_P (op1)) |
21262 | r = cxx_sizeof_or_alignof_type (input_location, |
21263 | op1, TREE_CODE (t), std_alignof, |
21264 | complain & tf_error); |
21265 | else |
21266 | r = cxx_sizeof_or_alignof_expr (input_location, |
21267 | op1, TREE_CODE (t), std_alignof, |
21268 | complain & tf_error); |
21269 | if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node) |
21270 | { |
21271 | if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1)) |
21272 | { |
21273 | if (!processing_template_decl && TYPE_P (op1)) |
21274 | { |
21275 | r = build_min (SIZEOF_EXPR, size_type_node, |
21276 | build1 (NOP_EXPR, op1, error_mark_node)); |
21277 | SIZEOF_EXPR_TYPE_P (r) = 1; |
21278 | } |
21279 | else |
21280 | r = build_min (SIZEOF_EXPR, size_type_node, op1); |
21281 | TREE_SIDE_EFFECTS (r) = 0; |
21282 | TREE_READONLY (r) = 1; |
21283 | } |
21284 | SET_EXPR_LOCATION (r, EXPR_LOCATION (t)); |
21285 | copy_warning (r, t); |
21286 | } |
21287 | RETURN (r); |
21288 | } |
21289 | |
21290 | case AT_ENCODE_EXPR: |
21291 | { |
21292 | op1 = TREE_OPERAND (t, 0); |
21293 | ++cp_unevaluated_operand; |
21294 | ++c_inhibit_evaluation_warnings; |
21295 | op1 = tsubst (t: op1, args, complain, in_decl); |
21296 | --cp_unevaluated_operand; |
21297 | --c_inhibit_evaluation_warnings; |
21298 | RETURN (objc_build_encode_expr (op1)); |
21299 | } |
21300 | |
21301 | case NOEXCEPT_EXPR: |
21302 | op1 = TREE_OPERAND (t, 0); |
21303 | ++cp_unevaluated_operand; |
21304 | ++c_inhibit_evaluation_warnings; |
21305 | ++cp_noexcept_operand; |
21306 | op1 = tsubst_expr (t: op1, args, complain, in_decl); |
21307 | --cp_unevaluated_operand; |
21308 | --c_inhibit_evaluation_warnings; |
21309 | --cp_noexcept_operand; |
21310 | RETURN (finish_noexcept_expr (op1, complain)); |
21311 | |
21312 | case MODOP_EXPR: |
21313 | { |
21314 | warning_sentinel s(warn_div_by_zero); |
21315 | tree lhs = RECUR (TREE_OPERAND (t, 0)); |
21316 | tree rhs = RECUR (TREE_OPERAND (t, 2)); |
21317 | |
21318 | tree r = build_x_modify_expr |
21319 | (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs, |
21320 | templated_operator_saved_lookups (t), |
21321 | complain|decltype_flag); |
21322 | /* TREE_NO_WARNING must be set if either the expression was |
21323 | parenthesized or it uses an operator such as >>= rather |
21324 | than plain assignment. In the former case, it was already |
21325 | set and must be copied. In the latter case, |
21326 | build_x_modify_expr sets it and it must not be reset |
21327 | here. */ |
21328 | if (warning_suppressed_p (t, OPT_Wparentheses)) |
21329 | suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses); |
21330 | |
21331 | RETURN (r); |
21332 | } |
21333 | |
21334 | case ARROW_EXPR: |
21335 | op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
21336 | args, complain, in_decl); |
21337 | /* Remember that there was a reference to this entity. */ |
21338 | if (DECL_P (op1) |
21339 | && !mark_used (op1, complain) && !(complain & tf_error)) |
21340 | RETURN (error_mark_node); |
21341 | RETURN (build_x_arrow (input_location, op1, complain)); |
21342 | |
21343 | case NEW_EXPR: |
21344 | { |
21345 | tree placement = RECUR (TREE_OPERAND (t, 0)); |
21346 | tree init = RECUR (TREE_OPERAND (t, 3)); |
21347 | vec<tree, va_gc> *placement_vec; |
21348 | vec<tree, va_gc> *init_vec; |
21349 | tree ret; |
21350 | location_t loc = EXPR_LOCATION (t); |
21351 | |
21352 | if (placement == NULL_TREE) |
21353 | placement_vec = NULL; |
21354 | else if (placement == error_mark_node) |
21355 | RETURN (error_mark_node); |
21356 | else |
21357 | { |
21358 | placement_vec = make_tree_vector (); |
21359 | for (; placement != NULL_TREE; placement = TREE_CHAIN (placement)) |
21360 | vec_safe_push (v&: placement_vec, TREE_VALUE (placement)); |
21361 | } |
21362 | |
21363 | /* If there was an initializer in the original tree, but it |
21364 | instantiated to an empty list, then we should pass a |
21365 | non-NULL empty vector to tell build_new that it was an |
21366 | empty initializer() rather than no initializer. This can |
21367 | only happen when the initializer is a pack expansion whose |
21368 | parameter packs are of length zero. */ |
21369 | if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE) |
21370 | init_vec = NULL; |
21371 | else if (init == error_mark_node) |
21372 | RETURN (error_mark_node); |
21373 | else |
21374 | { |
21375 | init_vec = make_tree_vector (); |
21376 | if (init == void_node) |
21377 | gcc_assert (init_vec != NULL); |
21378 | else |
21379 | { |
21380 | for (; init != NULL_TREE; init = TREE_CHAIN (init)) |
21381 | vec_safe_push (v&: init_vec, TREE_VALUE (init)); |
21382 | } |
21383 | } |
21384 | |
21385 | /* Avoid passing an enclosing decl to valid_array_size_p. */ |
21386 | in_decl = NULL_TREE; |
21387 | |
21388 | tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); |
21389 | tree op2 = RECUR (TREE_OPERAND (t, 2)); |
21390 | ret = build_new (loc, &placement_vec, op1, op2, |
21391 | &init_vec, NEW_EXPR_USE_GLOBAL (t), |
21392 | complain); |
21393 | |
21394 | if (placement_vec != NULL) |
21395 | release_tree_vector (placement_vec); |
21396 | if (init_vec != NULL) |
21397 | release_tree_vector (init_vec); |
21398 | |
21399 | RETURN (ret); |
21400 | } |
21401 | |
21402 | case DELETE_EXPR: |
21403 | { |
21404 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
21405 | tree op1 = RECUR (TREE_OPERAND (t, 1)); |
21406 | RETURN (delete_sanity (input_location, op0, op1, |
21407 | DELETE_EXPR_USE_VEC (t), |
21408 | DELETE_EXPR_USE_GLOBAL (t), |
21409 | complain)); |
21410 | } |
21411 | |
21412 | case COMPOUND_EXPR: |
21413 | { |
21414 | tree op0 = tsubst_expr (TREE_OPERAND (t, 0), args, |
21415 | complain: complain & ~tf_decltype, in_decl); |
21416 | RETURN (build_x_compound_expr (EXPR_LOCATION (t), |
21417 | op0, |
21418 | RECUR (TREE_OPERAND (t, 1)), |
21419 | templated_operator_saved_lookups (t), |
21420 | complain|decltype_flag)); |
21421 | } |
21422 | |
21423 | case CALL_EXPR: |
21424 | { |
21425 | tree function; |
21426 | unsigned int nargs; |
21427 | bool qualified_p; |
21428 | bool koenig_p; |
21429 | tree ret; |
21430 | |
21431 | function = CALL_EXPR_FN (t); |
21432 | /* Internal function with no arguments. */ |
21433 | if (function == NULL_TREE && call_expr_nargs (t) == 0) |
21434 | RETURN (t); |
21435 | |
21436 | /* When we parsed the expression, we determined whether or |
21437 | not Koenig lookup should be performed. */ |
21438 | koenig_p = KOENIG_LOOKUP_P (t); |
21439 | if (function == NULL_TREE) |
21440 | { |
21441 | koenig_p = false; |
21442 | qualified_p = false; |
21443 | } |
21444 | else if (TREE_CODE (function) == SCOPE_REF) |
21445 | { |
21446 | qualified_p = true; |
21447 | function = tsubst_qualified_id (qualified_id: function, args, complain, in_decl, |
21448 | /*done=*/false, |
21449 | /*address_p=*/false); |
21450 | } |
21451 | else if (CALL_EXPR_STATIC_CHAIN (t) |
21452 | && TREE_CODE (function) == FUNCTION_DECL |
21453 | && fndecl_built_in_p (node: function, name1: BUILT_IN_CLASSIFY_TYPE)) |
21454 | { |
21455 | tree type = tsubst (CALL_EXPR_STATIC_CHAIN (t), args, complain, |
21456 | in_decl); |
21457 | if (dependent_type_p (type)) |
21458 | { |
21459 | ret = build_vl_exp (CALL_EXPR, 4); |
21460 | CALL_EXPR_FN (ret) = function; |
21461 | CALL_EXPR_STATIC_CHAIN (ret) = type; |
21462 | CALL_EXPR_ARG (ret, 0) |
21463 | = build_min (SIZEOF_EXPR, size_type_node, type); |
21464 | TREE_TYPE (ret) = integer_type_node; |
21465 | } |
21466 | else |
21467 | ret = build_int_cst (integer_type_node, type_to_class (type)); |
21468 | RETURN (ret); |
21469 | } |
21470 | else if (koenig_p |
21471 | && (identifier_p (t: function) |
21472 | || (TREE_CODE (function) == TEMPLATE_ID_EXPR |
21473 | && identifier_p (TREE_OPERAND (function, 0))))) |
21474 | { |
21475 | /* Do nothing; calling tsubst_expr on an identifier |
21476 | would incorrectly perform unqualified lookup again. |
21477 | |
21478 | Note that we can also have an IDENTIFIER_NODE if the earlier |
21479 | unqualified lookup found a dependent local extern declaration |
21480 | (as per finish_call_expr); in that case koenig_p will be false |
21481 | and we do want to do the lookup again to find the substituted |
21482 | declaration. */ |
21483 | qualified_p = false; |
21484 | |
21485 | if (TREE_CODE (function) == TEMPLATE_ID_EXPR) |
21486 | function = tsubst_name (t: function, args, complain, in_decl); |
21487 | } |
21488 | else |
21489 | { |
21490 | if (TREE_CODE (function) == COMPONENT_REF) |
21491 | { |
21492 | tree op = TREE_OPERAND (function, 1); |
21493 | |
21494 | qualified_p = (TREE_CODE (op) == SCOPE_REF |
21495 | || (BASELINK_P (op) |
21496 | && BASELINK_QUALIFIED_P (op))); |
21497 | } |
21498 | else |
21499 | qualified_p = false; |
21500 | |
21501 | if (TREE_CODE (function) == ADDR_EXPR |
21502 | && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL) |
21503 | /* Avoid error about taking the address of a constructor. */ |
21504 | function = TREE_OPERAND (function, 0); |
21505 | |
21506 | function = tsubst_expr (t: function, args, complain, in_decl); |
21507 | |
21508 | if (BASELINK_P (function)) |
21509 | qualified_p = true; |
21510 | } |
21511 | |
21512 | nargs = call_expr_nargs (t); |
21513 | releasing_vec call_args; |
21514 | tsubst_call_args (t, args, complain, in_decl, call_args); |
21515 | |
21516 | /* Stripped-down processing for a call in a thunk. Specifically, in |
21517 | the thunk template for a generic lambda. */ |
21518 | if (call_from_lambda_thunk_p (t)) |
21519 | { |
21520 | /* Now that we've expanded any packs, the number of call args |
21521 | might be different. */ |
21522 | unsigned int cargs = call_args->length (); |
21523 | tree thisarg = NULL_TREE; |
21524 | if (TREE_CODE (function) == COMPONENT_REF) |
21525 | { |
21526 | thisarg = TREE_OPERAND (function, 0); |
21527 | if (TREE_CODE (thisarg) == INDIRECT_REF) |
21528 | thisarg = TREE_OPERAND (thisarg, 0); |
21529 | function = TREE_OPERAND (function, 1); |
21530 | if (TREE_CODE (function) == BASELINK) |
21531 | function = BASELINK_FUNCTIONS (function); |
21532 | } |
21533 | /* We aren't going to do normal overload resolution, so force the |
21534 | template-id to resolve. */ |
21535 | function = resolve_nondeduced_context (function, complain); |
21536 | for (unsigned i = 0; i < cargs; ++i) |
21537 | { |
21538 | /* In a thunk, pass through args directly, without any |
21539 | conversions. */ |
21540 | tree arg = (*call_args)[i]; |
21541 | while (TREE_CODE (arg) != PARM_DECL) |
21542 | arg = TREE_OPERAND (arg, 0); |
21543 | (*call_args)[i] = arg; |
21544 | } |
21545 | if (thisarg) |
21546 | { |
21547 | /* If there are no other args, just push 'this'. */ |
21548 | if (cargs == 0) |
21549 | vec_safe_push (r&: call_args, t: thisarg); |
21550 | else |
21551 | { |
21552 | /* Otherwise, shift the other args over to make room. */ |
21553 | tree last = (*call_args)[cargs - 1]; |
21554 | vec_safe_push (r&: call_args, t: last); |
21555 | for (int i = cargs - 1; i > 0; --i) |
21556 | (*call_args)[i] = (*call_args)[i - 1]; |
21557 | (*call_args)[0] = thisarg; |
21558 | } |
21559 | } |
21560 | ret = build_call_a (function, call_args->length (), |
21561 | call_args->address ()); |
21562 | /* The thunk location is not interesting. */ |
21563 | SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION); |
21564 | CALL_FROM_THUNK_P (ret) = true; |
21565 | if (CLASS_TYPE_P (TREE_TYPE (ret))) |
21566 | CALL_EXPR_RETURN_SLOT_OPT (ret) = true; |
21567 | |
21568 | RETURN (ret); |
21569 | } |
21570 | |
21571 | /* We do not perform argument-dependent lookup if normal |
21572 | lookup finds a non-function, in accordance with the |
21573 | resolution of DR 218. */ |
21574 | if (koenig_p |
21575 | && ((is_overloaded_fn (function) |
21576 | /* If lookup found a member function, the Koenig lookup is |
21577 | not appropriate, even if an unqualified-name was used |
21578 | to denote the function. */ |
21579 | && !DECL_FUNCTION_MEMBER_P (get_first_fn (function))) |
21580 | || identifier_p (t: function) |
21581 | /* C++20 P0846: Lookup found nothing. */ |
21582 | || (TREE_CODE (function) == TEMPLATE_ID_EXPR |
21583 | && identifier_p (TREE_OPERAND (function, 0)))) |
21584 | /* Only do this when substitution turns a dependent call |
21585 | into a non-dependent call. */ |
21586 | && type_dependent_expression_p_push (t) |
21587 | && !any_type_dependent_arguments_p (call_args)) |
21588 | function = perform_koenig_lookup (function, call_args, tf_none); |
21589 | |
21590 | if (function != NULL_TREE |
21591 | && (identifier_p (t: function) |
21592 | || (TREE_CODE (function) == TEMPLATE_ID_EXPR |
21593 | && identifier_p (TREE_OPERAND (function, 0)) |
21594 | && !any_dependent_template_arguments_p (TREE_OPERAND |
21595 | (function, 1)))) |
21596 | && !any_type_dependent_arguments_p (call_args)) |
21597 | { |
21598 | bool template_id_p = (TREE_CODE (function) == TEMPLATE_ID_EXPR); |
21599 | if (template_id_p) |
21600 | function = TREE_OPERAND (function, 0); |
21601 | if (koenig_p && (complain & tf_warning_or_error)) |
21602 | { |
21603 | /* For backwards compatibility and good diagnostics, try |
21604 | the unqualified lookup again if we aren't in SFINAE |
21605 | context. */ |
21606 | tree unq = tsubst_expr (t: function, args, complain, in_decl); |
21607 | if (unq == error_mark_node) |
21608 | RETURN (error_mark_node); |
21609 | |
21610 | if (unq != function) |
21611 | { |
21612 | auto_diagnostic_group d; |
21613 | char const *const msg |
21614 | = G_("%qD was not declared in this scope, " |
21615 | "and no declarations were found by " |
21616 | "argument-dependent lookup at the point " |
21617 | "of instantiation"); |
21618 | |
21619 | bool in_lambda = (current_class_type |
21620 | && LAMBDA_TYPE_P (current_class_type)); |
21621 | /* In a lambda fn, we have to be careful to not |
21622 | introduce new this captures. Legacy code can't |
21623 | be using lambdas anyway, so it's ok to be |
21624 | stricter. Be strict with C++20 template-id ADL too. |
21625 | And be strict if we're already failing anyway. */ |
21626 | bool strict = in_lambda || template_id_p || seen_error (); |
21627 | bool diag = true; |
21628 | if (strict) |
21629 | error_at (cp_expr_loc_or_input_loc (t), |
21630 | msg, function); |
21631 | else |
21632 | diag = permerror (cp_expr_loc_or_input_loc (t), |
21633 | msg, function); |
21634 | if (diag) |
21635 | { |
21636 | tree fn = unq; |
21637 | |
21638 | if (INDIRECT_REF_P (fn)) |
21639 | fn = TREE_OPERAND (fn, 0); |
21640 | if (is_overloaded_fn (fn)) |
21641 | fn = get_first_fn (fn); |
21642 | |
21643 | if (!DECL_P (fn)) |
21644 | /* Can't say anything more. */; |
21645 | else if (DECL_CLASS_SCOPE_P (fn)) |
21646 | { |
21647 | location_t loc = cp_expr_loc_or_input_loc (t); |
21648 | inform (loc, |
21649 | "declarations in dependent base %qT are " |
21650 | "not found by unqualified lookup", |
21651 | DECL_CLASS_CONTEXT (fn)); |
21652 | if (current_class_ptr) |
21653 | inform (loc, |
21654 | "use %<this->%D%> instead", function); |
21655 | else |
21656 | inform (loc, |
21657 | "use %<%T::%D%> instead", |
21658 | current_class_name, function); |
21659 | } |
21660 | else |
21661 | inform (DECL_SOURCE_LOCATION (fn), |
21662 | "%qD declared here, later in the " |
21663 | "translation unit", fn); |
21664 | if (strict) |
21665 | RETURN (error_mark_node); |
21666 | } |
21667 | |
21668 | function = unq; |
21669 | } |
21670 | } |
21671 | if (identifier_p (t: function)) |
21672 | { |
21673 | if (complain & tf_error) |
21674 | unqualified_name_lookup_error (function); |
21675 | RETURN (error_mark_node); |
21676 | } |
21677 | } |
21678 | |
21679 | /* Remember that there was a reference to this entity. */ |
21680 | if (function != NULL_TREE |
21681 | && DECL_P (function) |
21682 | && !mark_used (function, complain) && !(complain & tf_error)) |
21683 | RETURN (error_mark_node); |
21684 | |
21685 | if (!maybe_fold_fn_template_args (fn: function, complain)) |
21686 | return error_mark_node; |
21687 | |
21688 | /* Put back tf_decltype for the actual call. */ |
21689 | complain |= decltype_flag; |
21690 | |
21691 | if (function == NULL_TREE) |
21692 | switch (CALL_EXPR_IFN (t)) |
21693 | { |
21694 | case IFN_LAUNDER: |
21695 | gcc_assert (nargs == 1); |
21696 | if (vec_safe_length (r&: call_args) != 1) |
21697 | { |
21698 | error_at (cp_expr_loc_or_input_loc (t), |
21699 | "wrong number of arguments to " |
21700 | "%<__builtin_launder%>"); |
21701 | ret = error_mark_node; |
21702 | } |
21703 | else |
21704 | ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t), |
21705 | (*call_args)[0], complain); |
21706 | break; |
21707 | |
21708 | case IFN_VEC_CONVERT: |
21709 | gcc_assert (nargs == 1); |
21710 | if (vec_safe_length (r&: call_args) != 1) |
21711 | { |
21712 | error_at (cp_expr_loc_or_input_loc (t), |
21713 | "wrong number of arguments to " |
21714 | "%<__builtin_convertvector%>"); |
21715 | ret = error_mark_node; |
21716 | break; |
21717 | } |
21718 | ret = cp_build_vec_convert ((*call_args)[0], input_location, |
21719 | tsubst (TREE_TYPE (t), args, |
21720 | complain, in_decl), |
21721 | complain); |
21722 | if (TREE_CODE (ret) == VIEW_CONVERT_EXPR) |
21723 | RETURN (ret); |
21724 | break; |
21725 | |
21726 | case IFN_SHUFFLEVECTOR: |
21727 | { |
21728 | ret = build_x_shufflevector (input_location, call_args, |
21729 | complain); |
21730 | if (ret != error_mark_node) |
21731 | RETURN (ret); |
21732 | break; |
21733 | } |
21734 | |
21735 | case IFN_ASSUME: |
21736 | gcc_assert (nargs == 1); |
21737 | if (vec_safe_length (r&: call_args) != 1) |
21738 | { |
21739 | error_at (cp_expr_loc_or_input_loc (t), |
21740 | "wrong number of arguments to " |
21741 | "%<assume%> attribute"); |
21742 | ret = error_mark_node; |
21743 | } |
21744 | else |
21745 | { |
21746 | tree &arg = (*call_args)[0]; |
21747 | if (!type_dependent_expression_p (arg)) |
21748 | arg = contextual_conv_bool (arg, tf_warning_or_error); |
21749 | if (error_operand_p (t: arg)) |
21750 | { |
21751 | ret = error_mark_node; |
21752 | break; |
21753 | } |
21754 | ret = build_assume_call (EXPR_LOCATION (t), arg); |
21755 | RETURN (ret); |
21756 | } |
21757 | break; |
21758 | |
21759 | case IFN_GOMP_DISPATCH: |
21760 | ret = build_call_expr_internal_loc (EXPR_LOCATION (t), |
21761 | IFN_GOMP_DISPATCH, |
21762 | TREE_TYPE (call_args[0]), 1, |
21763 | call_args[0]); |
21764 | RETURN (ret); |
21765 | break; |
21766 | |
21767 | default: |
21768 | /* Unsupported internal function with arguments. */ |
21769 | gcc_unreachable (); |
21770 | } |
21771 | else if (TREE_CODE (function) == OFFSET_REF |
21772 | || TREE_CODE (function) == DOTSTAR_EXPR |
21773 | || TREE_CODE (function) == MEMBER_REF) |
21774 | ret = build_offset_ref_call_from_tree (function, &call_args, |
21775 | complain); |
21776 | else if (concept_check_p (t: function)) |
21777 | /* Calls to concepts should have been previously diagnosed. */ |
21778 | gcc_assert (false); |
21779 | else |
21780 | ret = finish_call_expr (function, &call_args, |
21781 | /*disallow_virtual=*/qualified_p, |
21782 | koenig_p, |
21783 | complain); |
21784 | |
21785 | if (ret != error_mark_node) |
21786 | { |
21787 | bool op = CALL_EXPR_OPERATOR_SYNTAX (t); |
21788 | bool ord = CALL_EXPR_ORDERED_ARGS (t); |
21789 | bool rev = CALL_EXPR_REVERSE_ARGS (t); |
21790 | bool mtc = false; |
21791 | if (TREE_CODE (t) == CALL_EXPR) |
21792 | mtc = CALL_EXPR_MUST_TAIL_CALL (t); |
21793 | if (op || ord || rev || mtc) |
21794 | if (tree call = extract_call_expr (ret)) |
21795 | { |
21796 | CALL_EXPR_OPERATOR_SYNTAX (call) = op; |
21797 | CALL_EXPR_ORDERED_ARGS (call) = ord; |
21798 | CALL_EXPR_REVERSE_ARGS (call) = rev; |
21799 | if (TREE_CODE (call) == CALL_EXPR) |
21800 | CALL_EXPR_MUST_TAIL_CALL (call) = mtc; |
21801 | else if (TREE_CODE (call) == AGGR_INIT_EXPR) |
21802 | AGGR_INIT_EXPR_MUST_TAIL (call) = mtc; |
21803 | } |
21804 | if (CALL_FROM_NEW_OR_DELETE_P (t)) |
21805 | { |
21806 | tree call = extract_call_expr (ret); |
21807 | tree fn = cp_get_callee_fndecl_nofold (call); |
21808 | if (fn ? !DECL_IS_REPLACEABLE_OPERATOR (fn) |
21809 | : !processing_template_decl) |
21810 | { |
21811 | auto_diagnostic_group d; |
21812 | location_t loc = cp_expr_loc_or_input_loc (t); |
21813 | if (!fn || IDENTIFIER_NEW_OP_P (DECL_NAME (fn))) |
21814 | error_at (loc, "call to %<__builtin_operator_new%> " |
21815 | "does not select replaceable global " |
21816 | "allocation function"); |
21817 | else |
21818 | error_at (loc, "call to %<__builtin_operator_delete%> " |
21819 | "does not select replaceable global " |
21820 | "deallocation function"); |
21821 | if (fn) |
21822 | inform (DECL_SOURCE_LOCATION (fn), |
21823 | "selected function declared here"); |
21824 | } |
21825 | else if (call && TREE_CODE (call) == CALL_EXPR) |
21826 | CALL_FROM_NEW_OR_DELETE_P (call) = 1; |
21827 | } |
21828 | if (warning_suppressed_p (t, OPT_Wpessimizing_move)) |
21829 | /* This also suppresses -Wredundant-move. */ |
21830 | suppress_warning (ret, OPT_Wpessimizing_move); |
21831 | } |
21832 | |
21833 | RETURN (ret); |
21834 | } |
21835 | |
21836 | case COND_EXPR: |
21837 | { |
21838 | tree cond = RECUR (TREE_OPERAND (t, 0)); |
21839 | cond = mark_rvalue_use (cond); |
21840 | tree folded_cond = fold_non_dependent_expr (cond, complain); |
21841 | tree exp1, exp2; |
21842 | |
21843 | if (TREE_CODE (folded_cond) == INTEGER_CST) |
21844 | { |
21845 | if (integer_zerop (folded_cond)) |
21846 | { |
21847 | ++c_inhibit_evaluation_warnings; |
21848 | exp1 = RECUR (TREE_OPERAND (t, 1)); |
21849 | --c_inhibit_evaluation_warnings; |
21850 | exp2 = RECUR (TREE_OPERAND (t, 2)); |
21851 | } |
21852 | else |
21853 | { |
21854 | exp1 = RECUR (TREE_OPERAND (t, 1)); |
21855 | ++c_inhibit_evaluation_warnings; |
21856 | exp2 = RECUR (TREE_OPERAND (t, 2)); |
21857 | --c_inhibit_evaluation_warnings; |
21858 | } |
21859 | cond = folded_cond; |
21860 | } |
21861 | else |
21862 | { |
21863 | exp1 = RECUR (TREE_OPERAND (t, 1)); |
21864 | exp2 = RECUR (TREE_OPERAND (t, 2)); |
21865 | } |
21866 | |
21867 | warning_sentinel s(warn_duplicated_branches); |
21868 | RETURN (build_x_conditional_expr (EXPR_LOCATION (t), |
21869 | cond, exp1, exp2, complain)); |
21870 | } |
21871 | |
21872 | case PSEUDO_DTOR_EXPR: |
21873 | { |
21874 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
21875 | tree op1 = RECUR (TREE_OPERAND (t, 1)); |
21876 | tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl); |
21877 | RETURN (finish_pseudo_destructor_expr (op0, op1, op2, |
21878 | input_location, complain)); |
21879 | } |
21880 | |
21881 | case TREE_LIST: |
21882 | RETURN (tsubst_tree_list (t, args, complain, in_decl)); |
21883 | |
21884 | case COMPONENT_REF: |
21885 | { |
21886 | tree object; |
21887 | tree object_type; |
21888 | tree member; |
21889 | tree r; |
21890 | |
21891 | object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0), |
21892 | args, complain, in_decl); |
21893 | /* Remember that there was a reference to this entity. */ |
21894 | if (DECL_P (object) |
21895 | && !mark_used (object, complain) && !(complain & tf_error)) |
21896 | RETURN (error_mark_node); |
21897 | object_type = TREE_TYPE (object); |
21898 | |
21899 | member = TREE_OPERAND (t, 1); |
21900 | if (BASELINK_P (member)) |
21901 | member = tsubst_baselink (baselink: member, |
21902 | object_type: non_reference (TREE_TYPE (object)), |
21903 | args, complain, in_decl); |
21904 | else |
21905 | member = tsubst_name (t: member, args, complain, in_decl); |
21906 | if (member == error_mark_node) |
21907 | RETURN (error_mark_node); |
21908 | |
21909 | if (object_type && TYPE_PTRMEMFUNC_P (object_type) |
21910 | && TREE_CODE (member) == FIELD_DECL) |
21911 | { |
21912 | r = build_ptrmemfunc_access_expr (object, DECL_NAME (member)); |
21913 | RETURN (r); |
21914 | } |
21915 | else if (TREE_CODE (member) == FIELD_DECL) |
21916 | { |
21917 | r = finish_non_static_data_member (member, object, NULL_TREE, |
21918 | complain); |
21919 | if (REF_PARENTHESIZED_P (t)) |
21920 | r = force_paren_expr (r); |
21921 | RETURN (r); |
21922 | } |
21923 | else if (type_dependent_expression_p (object)) |
21924 | /* We can't do much here. */; |
21925 | else if (!CLASS_TYPE_P (object_type)) |
21926 | { |
21927 | if (scalarish_type_p (object_type)) |
21928 | { |
21929 | tree s = NULL_TREE; |
21930 | tree dtor = member; |
21931 | |
21932 | if (TREE_CODE (dtor) == SCOPE_REF) |
21933 | { |
21934 | s = TREE_OPERAND (dtor, 0); |
21935 | dtor = TREE_OPERAND (dtor, 1); |
21936 | } |
21937 | if (TREE_CODE (dtor) == BIT_NOT_EXPR) |
21938 | { |
21939 | dtor = TREE_OPERAND (dtor, 0); |
21940 | if (TYPE_P (dtor)) |
21941 | RETURN (finish_pseudo_destructor_expr |
21942 | (object, s, dtor, input_location, complain)); |
21943 | } |
21944 | } |
21945 | } |
21946 | else if (TREE_CODE (member) == SCOPE_REF |
21947 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) |
21948 | { |
21949 | /* Lookup the template functions now that we know what the |
21950 | scope is. */ |
21951 | tree scope = TREE_OPERAND (member, 0); |
21952 | tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); |
21953 | tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); |
21954 | member = lookup_qualified_name (scope, name: tmpl, LOOK_want::NORMAL, |
21955 | /*complain=*/false); |
21956 | if (BASELINK_P (member)) |
21957 | { |
21958 | BASELINK_FUNCTIONS (member) |
21959 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), |
21960 | args); |
21961 | member = (adjust_result_of_qualified_name_lookup |
21962 | (member, BINFO_TYPE (BASELINK_BINFO (member)), |
21963 | object_type)); |
21964 | } |
21965 | else |
21966 | { |
21967 | qualified_name_lookup_error (scope, tmpl, member, |
21968 | input_location); |
21969 | RETURN (error_mark_node); |
21970 | } |
21971 | } |
21972 | else if (TREE_CODE (member) == SCOPE_REF |
21973 | && !CLASS_TYPE_P (TREE_OPERAND (member, 0)) |
21974 | && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL) |
21975 | { |
21976 | if (complain & tf_error) |
21977 | { |
21978 | if (TYPE_P (TREE_OPERAND (member, 0))) |
21979 | error ("%qT is not a class or namespace", |
21980 | TREE_OPERAND (member, 0)); |
21981 | else |
21982 | error ("%qD is not a class or namespace", |
21983 | TREE_OPERAND (member, 0)); |
21984 | } |
21985 | RETURN (error_mark_node); |
21986 | } |
21987 | |
21988 | r = finish_class_member_access_expr (object, member, |
21989 | /*template_p=*/false, |
21990 | complain); |
21991 | if (REF_PARENTHESIZED_P (t)) |
21992 | r = force_paren_expr (r); |
21993 | RETURN (r); |
21994 | } |
21995 | |
21996 | case THROW_EXPR: |
21997 | RETURN (build_throw |
21998 | (input_location, RECUR (TREE_OPERAND (t, 0)), complain)); |
21999 | |
22000 | case CONSTRUCTOR: |
22001 | { |
22002 | vec<constructor_elt, va_gc> *n; |
22003 | constructor_elt *ce; |
22004 | unsigned HOST_WIDE_INT idx; |
22005 | bool process_index_p; |
22006 | int newlen; |
22007 | bool need_copy_p = false; |
22008 | tree r; |
22009 | |
22010 | tsubst_flags_t tcomplain = complain; |
22011 | if (COMPOUND_LITERAL_P (t)) |
22012 | tcomplain |= tf_tst_ok; |
22013 | tree type = tsubst (TREE_TYPE (t), args, complain: tcomplain, in_decl); |
22014 | if (type == error_mark_node) |
22015 | RETURN (error_mark_node); |
22016 | |
22017 | /* We do not want to process the index of aggregate |
22018 | initializers as they are identifier nodes which will be |
22019 | looked up by digest_init. */ |
22020 | process_index_p = !(type && MAYBE_CLASS_TYPE_P (type)); |
22021 | |
22022 | if (null_member_pointer_value_p (t)) |
22023 | { |
22024 | gcc_assert (same_type_p (type, TREE_TYPE (t))); |
22025 | RETURN (t); |
22026 | } |
22027 | |
22028 | n = vec_safe_copy (CONSTRUCTOR_ELTS (t)); |
22029 | newlen = vec_safe_length (v: n); |
22030 | FOR_EACH_VEC_SAFE_ELT (n, idx, ce) |
22031 | { |
22032 | if (ce->index && process_index_p |
22033 | /* An identifier index is looked up in the type |
22034 | being initialized, not the current scope. */ |
22035 | && TREE_CODE (ce->index) != IDENTIFIER_NODE) |
22036 | ce->index = RECUR (ce->index); |
22037 | |
22038 | if (PACK_EXPANSION_P (ce->value)) |
22039 | { |
22040 | /* Substitute into the pack expansion. */ |
22041 | ce->value = tsubst_pack_expansion (t: ce->value, args, complain, |
22042 | in_decl); |
22043 | |
22044 | if (ce->value == error_mark_node |
22045 | || PACK_EXPANSION_P (ce->value)) |
22046 | ; |
22047 | else if (TREE_VEC_LENGTH (ce->value) == 1) |
22048 | /* Just move the argument into place. */ |
22049 | ce->value = TREE_VEC_ELT (ce->value, 0); |
22050 | else |
22051 | { |
22052 | /* Update the length of the final CONSTRUCTOR |
22053 | arguments vector, and note that we will need to |
22054 | copy.*/ |
22055 | newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1; |
22056 | need_copy_p = true; |
22057 | } |
22058 | } |
22059 | else |
22060 | ce->value = RECUR (ce->value); |
22061 | } |
22062 | |
22063 | if (need_copy_p) |
22064 | { |
22065 | vec<constructor_elt, va_gc> *old_n = n; |
22066 | |
22067 | vec_alloc (v&: n, nelems: newlen); |
22068 | FOR_EACH_VEC_ELT (*old_n, idx, ce) |
22069 | { |
22070 | if (TREE_CODE (ce->value) == TREE_VEC) |
22071 | { |
22072 | int i, len = TREE_VEC_LENGTH (ce->value); |
22073 | for (i = 0; i < len; ++i) |
22074 | CONSTRUCTOR_APPEND_ELT (n, 0, |
22075 | TREE_VEC_ELT (ce->value, i)); |
22076 | } |
22077 | else |
22078 | CONSTRUCTOR_APPEND_ELT (n, 0, ce->value); |
22079 | } |
22080 | } |
22081 | |
22082 | r = build_constructor (init_list_type_node, n); |
22083 | CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t); |
22084 | CONSTRUCTOR_IS_DESIGNATED_INIT (r) |
22085 | = CONSTRUCTOR_IS_DESIGNATED_INIT (t); |
22086 | |
22087 | if (TREE_HAS_CONSTRUCTOR (t)) |
22088 | { |
22089 | fcl_t cl = fcl_functional; |
22090 | if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t)) |
22091 | cl = fcl_c99; |
22092 | RETURN (finish_compound_literal (type, r, complain, cl)); |
22093 | } |
22094 | |
22095 | TREE_TYPE (r) = type; |
22096 | RETURN (r); |
22097 | } |
22098 | |
22099 | case TYPEID_EXPR: |
22100 | { |
22101 | tree operand_0 = TREE_OPERAND (t, 0); |
22102 | if (TYPE_P (operand_0)) |
22103 | { |
22104 | operand_0 = tsubst (t: operand_0, args, complain, in_decl); |
22105 | RETURN (get_typeid (operand_0, complain)); |
22106 | } |
22107 | else |
22108 | { |
22109 | operand_0 = RECUR (operand_0); |
22110 | RETURN (build_typeid (operand_0, complain)); |
22111 | } |
22112 | } |
22113 | |
22114 | case FUNCTION_DECL: |
22115 | case PARM_DECL: |
22116 | case VAR_DECL: |
22117 | if (!args) |
22118 | RETURN (t); |
22119 | tree r; |
22120 | if (VAR_OR_FUNCTION_DECL_P (t) |
22121 | && DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) |
22122 | r = tsubst_decl (t, args, complain); |
22123 | else if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t)) |
22124 | { |
22125 | /* Local specialization will usually have been created when |
22126 | we instantiated the DECL_EXPR_DECL. */ |
22127 | r = retrieve_local_specialization (tmpl: t); |
22128 | if (!r) |
22129 | { |
22130 | /* We're in a generic lambda referencing a local extern |
22131 | from an outer block-scope of a non-template. */ |
22132 | gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl)); |
22133 | r = t; |
22134 | } |
22135 | } |
22136 | else if (local_variable_p (t) |
22137 | && ((r = retrieve_local_specialization (tmpl: t)) |
22138 | || TREE_CODE (t) == PARM_DECL |
22139 | || uses_template_parms (DECL_CONTEXT (t)))) |
22140 | { |
22141 | if (r == NULL_TREE && TREE_CODE (t) == PARM_DECL) |
22142 | { |
22143 | /* We get here for a use of 'this' in an NSDMI. */ |
22144 | if (DECL_NAME (t) == this_identifier && current_class_ptr) |
22145 | RETURN (current_class_ptr); |
22146 | |
22147 | /* This can happen for a parameter name used later in a function |
22148 | declaration (such as in a late-specified return type). Just |
22149 | make a dummy decl, since it's only used for its type. */ |
22150 | gcc_assert (cp_unevaluated_operand); |
22151 | r = tsubst_decl (t, args, complain); |
22152 | /* Give it the template pattern as its context; its true context |
22153 | hasn't been instantiated yet and this is good enough for |
22154 | mangling. */ |
22155 | DECL_CONTEXT (r) = DECL_CONTEXT (t); |
22156 | } |
22157 | else if (r == NULL_TREE) |
22158 | { |
22159 | /* First try name lookup to find the instantiation. */ |
22160 | r = lookup_name (DECL_NAME (t)); |
22161 | if (r) |
22162 | { |
22163 | if (!VAR_P (r)) |
22164 | { |
22165 | /* During error-recovery we may find a non-variable, |
22166 | even an OVERLOAD: just bail out and avoid ICEs and |
22167 | duplicate diagnostics (c++/62207). */ |
22168 | gcc_assert (seen_error ()); |
22169 | RETURN (error_mark_node); |
22170 | } |
22171 | if (!is_capture_proxy (r)) |
22172 | { |
22173 | /* Make sure the one we found is the one we want. */ |
22174 | tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t)); |
22175 | if (ctx != DECL_CONTEXT (r)) |
22176 | r = NULL_TREE; |
22177 | } |
22178 | } |
22179 | |
22180 | if (r) |
22181 | /* OK */; |
22182 | else |
22183 | { |
22184 | /* This can happen for a variable used in a |
22185 | late-specified return type of a local lambda, or for a |
22186 | local static or constant. Building a new VAR_DECL |
22187 | should be OK in all those cases. */ |
22188 | r = tsubst_decl (t, args, complain); |
22189 | if (local_specializations) |
22190 | /* Avoid infinite recursion (79640). */ |
22191 | register_local_specialization (spec: r, tmpl: t); |
22192 | if (decl_maybe_constant_var_p (r)) |
22193 | { |
22194 | /* We can't call cp_finish_decl, so handle the |
22195 | initializer by hand. */ |
22196 | tree init = tsubst_init (DECL_INITIAL (t), decl: r, args, |
22197 | complain, in_decl); |
22198 | if (!processing_template_decl) |
22199 | init = maybe_constant_init (init); |
22200 | if (processing_template_decl |
22201 | ? potential_constant_expression (init) |
22202 | : reduced_constant_expression_p (init)) |
22203 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) |
22204 | = TREE_CONSTANT (r) = true; |
22205 | DECL_INITIAL (r) = init; |
22206 | if (tree auto_node = type_uses_auto (TREE_TYPE (r))) |
22207 | TREE_TYPE (r) |
22208 | = do_auto_deduction (TREE_TYPE (r), init, auto_node, |
22209 | complain, adc_variable_type); |
22210 | } |
22211 | gcc_assert (cp_unevaluated_operand |
22212 | || processing_contract_condition |
22213 | || TREE_STATIC (r) |
22214 | || decl_constant_var_p (r) |
22215 | || seen_error ()); |
22216 | if (!processing_template_decl |
22217 | && !TREE_STATIC (r)) |
22218 | r = process_outer_var_ref (r, complain); |
22219 | } |
22220 | /* Remember this for subsequent uses. */ |
22221 | if (local_specializations) |
22222 | register_local_specialization (spec: r, tmpl: t); |
22223 | } |
22224 | if (TREE_CODE (r) == ARGUMENT_PACK_SELECT) |
22225 | r = argument_pack_select_arg (t: r); |
22226 | } |
22227 | else |
22228 | r = t; |
22229 | if (!mark_used (r, complain)) |
22230 | RETURN (error_mark_node); |
22231 | |
22232 | if (!no_name_lookup_flag |
22233 | && (TREE_CODE (t) == PARM_DECL || TREE_CODE (t) == VAR_DECL)) |
22234 | { |
22235 | /* ??? We're doing a subset of finish_id_expression here. */ |
22236 | if (tree wrap = maybe_get_tls_wrapper_call (r)) |
22237 | /* Replace an evaluated use of the thread_local variable with |
22238 | a call to its wrapper. */ |
22239 | r = wrap; |
22240 | else if (outer_automatic_var_p (r)) |
22241 | r = process_outer_var_ref (r, complain); |
22242 | |
22243 | if (!TYPE_REF_P (TREE_TYPE (t))) |
22244 | /* If the original type was a reference, we'll be wrapped in |
22245 | the appropriate INDIRECT_REF. */ |
22246 | r = convert_from_reference (r); |
22247 | } |
22248 | RETURN (r); |
22249 | |
22250 | case CONST_DECL: |
22251 | { |
22252 | tree enum_type; |
22253 | tree v; |
22254 | |
22255 | if (DECL_TEMPLATE_PARM_P (t)) |
22256 | RETURN (RECUR (DECL_INITIAL (t))); |
22257 | if (!args || !uses_template_parms (DECL_CONTEXT (t))) |
22258 | RETURN (t); |
22259 | |
22260 | /* Unfortunately, we cannot just call lookup_name here. |
22261 | Consider: |
22262 | |
22263 | template <int I> int f() { |
22264 | enum E { a = I }; |
22265 | struct S { void g() { E e = a; } }; |
22266 | }; |
22267 | |
22268 | When we instantiate f<7>::S::g(), say, lookup_name is not |
22269 | clever enough to find f<7>::a. */ |
22270 | enum_type = tsubst (DECL_CONTEXT (t), args, complain, in_decl); |
22271 | |
22272 | for (v = TYPE_VALUES (enum_type); |
22273 | v != NULL_TREE; |
22274 | v = TREE_CHAIN (v)) |
22275 | if (TREE_PURPOSE (v) == DECL_NAME (t)) |
22276 | RETURN (TREE_VALUE (v)); |
22277 | |
22278 | /* We didn't find the name. That should never happen; if |
22279 | name-lookup found it during preliminary parsing, we |
22280 | should find it again here during instantiation. */ |
22281 | gcc_unreachable (); |
22282 | RETURN (t); |
22283 | } |
22284 | |
22285 | case FIELD_DECL: |
22286 | if (DECL_CONTEXT (t)) |
22287 | { |
22288 | tree ctx; |
22289 | |
22290 | ctx = tsubst_entering_scope (DECL_CONTEXT (t), args, |
22291 | complain, in_decl); |
22292 | if (ctx != DECL_CONTEXT (t)) |
22293 | { |
22294 | tree r = lookup_field (ctx, DECL_NAME (t), 0, false); |
22295 | if (!r) |
22296 | { |
22297 | if (complain & tf_error) |
22298 | error ("using invalid field %qD", t); |
22299 | RETURN (error_mark_node); |
22300 | } |
22301 | RETURN (r); |
22302 | } |
22303 | } |
22304 | RETURN (t); |
22305 | |
22306 | case NAMESPACE_DECL: |
22307 | RETURN (t); |
22308 | |
22309 | case OVERLOAD: |
22310 | if (modules_p ()) |
22311 | for (tree ovl : lkp_range (t)) |
22312 | if (TREE_CODE (ovl) == TU_LOCAL_ENTITY) |
22313 | { |
22314 | complain_about_tu_local_entity (e: ovl); |
22315 | RETURN (error_mark_node); |
22316 | } |
22317 | RETURN (t); |
22318 | |
22319 | case TEMPLATE_DECL: |
22320 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)) |
22321 | RETURN (tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), |
22322 | args, complain, in_decl)); |
22323 | else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t)) |
22324 | RETURN (tsubst (t, args, complain, in_decl)); |
22325 | else if (DECL_CLASS_SCOPE_P (t) |
22326 | && uses_template_parms (DECL_CONTEXT (t))) |
22327 | { |
22328 | /* Template template argument like the following example need |
22329 | special treatment: |
22330 | |
22331 | template <template <class> class TT> struct C {}; |
22332 | template <class T> struct D { |
22333 | template <class U> struct E {}; |
22334 | C<E> c; // #1 |
22335 | }; |
22336 | D<int> d; // #2 |
22337 | |
22338 | We are processing the template argument `E' in #1 for |
22339 | the template instantiation #2. Originally, `E' is a |
22340 | TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we |
22341 | have to substitute this with one having context `D<int>'. */ |
22342 | |
22343 | tree context = tsubst_entering_scope (DECL_CONTEXT (t), args, |
22344 | complain, in_decl); |
22345 | RETURN (lookup_field (context, DECL_NAME(t), 0, false)); |
22346 | } |
22347 | else |
22348 | /* Ordinary template template argument. */ |
22349 | RETURN (t); |
22350 | |
22351 | case TEMPLATE_PARM_INDEX: |
22352 | case TYPE_DECL: |
22353 | RETURN (tsubst (t, args, complain, in_decl)); |
22354 | |
22355 | case CLEANUP_POINT_EXPR: |
22356 | /* We shouldn't have built any of these during initial template |
22357 | generation. Instead, they should be built during instantiation |
22358 | in response to the saved STMT_IS_FULL_EXPR_P setting. */ |
22359 | gcc_unreachable (); |
22360 | |
22361 | case OFFSET_REF: |
22362 | { |
22363 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
22364 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
22365 | tree op1 = RECUR (TREE_OPERAND (t, 1)); |
22366 | r = build2 (OFFSET_REF, type, op0, op1); |
22367 | PTRMEM_OK_P (r) = PTRMEM_OK_P (t); |
22368 | if (!mark_used (TREE_OPERAND (r, 1), complain) |
22369 | && !(complain & tf_error)) |
22370 | RETURN (error_mark_node); |
22371 | RETURN (r); |
22372 | } |
22373 | |
22374 | case PACK_INDEX_EXPR: |
22375 | RETURN (tsubst_pack_index (t, args, complain, in_decl)); |
22376 | |
22377 | case EXPR_PACK_EXPANSION: |
22378 | error ("invalid use of pack expansion expression"); |
22379 | RETURN (error_mark_node); |
22380 | |
22381 | case NONTYPE_ARGUMENT_PACK: |
22382 | error ("use %<...%> to expand argument pack"); |
22383 | RETURN (error_mark_node); |
22384 | |
22385 | case VOID_CST: |
22386 | gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t))); |
22387 | RETURN (t); |
22388 | |
22389 | case INTEGER_CST: |
22390 | case REAL_CST: |
22391 | case COMPLEX_CST: |
22392 | case VECTOR_CST: |
22393 | { |
22394 | /* Instantiate any typedefs in the type. */ |
22395 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
22396 | r = fold_convert (type, t); |
22397 | gcc_assert (TREE_CODE (r) == TREE_CODE (t)); |
22398 | RETURN (r); |
22399 | } |
22400 | |
22401 | case STRING_CST: |
22402 | { |
22403 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
22404 | r = t; |
22405 | if (type != TREE_TYPE (t)) |
22406 | { |
22407 | r = copy_node (t); |
22408 | TREE_TYPE (r) = type; |
22409 | } |
22410 | RETURN (r); |
22411 | } |
22412 | |
22413 | case RAW_DATA_CST: |
22414 | { |
22415 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
22416 | r = copy_node (t); |
22417 | TREE_TYPE (r) = type; |
22418 | RETURN (r); |
22419 | } |
22420 | |
22421 | case PTRMEM_CST: |
22422 | /* These can sometimes show up in a partial instantiation, but never |
22423 | involve template parms. */ |
22424 | gcc_assert (!uses_template_parms (t)); |
22425 | RETURN (t); |
22426 | |
22427 | case UNARY_LEFT_FOLD_EXPR: |
22428 | RETURN (tsubst_unary_left_fold (t, args, complain, in_decl)); |
22429 | case UNARY_RIGHT_FOLD_EXPR: |
22430 | RETURN (tsubst_unary_right_fold (t, args, complain, in_decl)); |
22431 | case BINARY_LEFT_FOLD_EXPR: |
22432 | RETURN (tsubst_binary_left_fold (t, args, complain, in_decl)); |
22433 | case BINARY_RIGHT_FOLD_EXPR: |
22434 | RETURN (tsubst_binary_right_fold (t, args, complain, in_decl)); |
22435 | case PREDICT_EXPR: |
22436 | RETURN (t); |
22437 | |
22438 | case DEBUG_BEGIN_STMT: |
22439 | /* ??? There's no point in copying it for now, but maybe some |
22440 | day it will contain more information, such as a pointer back |
22441 | to the containing function, inlined copy or so. */ |
22442 | RETURN (t); |
22443 | |
22444 | case CO_YIELD_EXPR: |
22445 | RETURN (finish_co_yield_expr (input_location, |
22446 | RECUR (TREE_OPERAND (t, 0)))); |
22447 | |
22448 | case CO_AWAIT_EXPR: |
22449 | RETURN (finish_co_await_expr (input_location, |
22450 | RECUR (TREE_OPERAND (t, 0)))); |
22451 | |
22452 | case VA_ARG_EXPR: |
22453 | { |
22454 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
22455 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
22456 | RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type)); |
22457 | } |
22458 | |
22459 | case OFFSETOF_EXPR: |
22460 | { |
22461 | tree object_ptr |
22462 | = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl); |
22463 | RETURN (finish_offsetof (object_ptr, |
22464 | RECUR (TREE_OPERAND (t, 0)), |
22465 | EXPR_LOCATION (t))); |
22466 | } |
22467 | |
22468 | case ADDRESSOF_EXPR: |
22469 | RETURN (cp_build_addressof (EXPR_LOCATION (t), |
22470 | RECUR (TREE_OPERAND (t, 0)), complain)); |
22471 | |
22472 | case TRAIT_EXPR: |
22473 | { |
22474 | tree type1 = TRAIT_EXPR_TYPE1 (t); |
22475 | if (TYPE_P (type1)) |
22476 | type1 = tsubst (t: type1, args, complain, in_decl); |
22477 | else |
22478 | type1 = tsubst_expr (t: type1, args, complain, in_decl); |
22479 | tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args, |
22480 | complain, in_decl); |
22481 | RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t), |
22482 | TRAIT_EXPR_KIND (t), type1, type2)); |
22483 | } |
22484 | |
22485 | case STMT_EXPR: |
22486 | { |
22487 | tree old_stmt_expr = cur_stmt_expr; |
22488 | tree stmt_expr = begin_stmt_expr (); |
22489 | |
22490 | cur_stmt_expr = stmt_expr; |
22491 | tsubst_stmt (STMT_EXPR_STMT (t), args, complain, in_decl); |
22492 | stmt_expr = finish_stmt_expr (stmt_expr, false); |
22493 | cur_stmt_expr = old_stmt_expr; |
22494 | |
22495 | /* If the resulting list of expression statement is empty, |
22496 | fold it further into void_node. */ |
22497 | if (empty_expr_stmt_p (stmt_expr)) |
22498 | stmt_expr = void_node; |
22499 | |
22500 | RETURN (stmt_expr); |
22501 | } |
22502 | |
22503 | case LAMBDA_EXPR: |
22504 | { |
22505 | tree r = tsubst_lambda_expr (t, args, complain, in_decl); |
22506 | |
22507 | RETURN (build_lambda_object (r)); |
22508 | } |
22509 | |
22510 | case TRANSACTION_EXPR: |
22511 | gcc_checking_assert (!TRANSACTION_EXPR_IS_STMT (t)); |
22512 | RETURN (tsubst_stmt (t, args, complain, in_decl)); |
22513 | |
22514 | case PAREN_EXPR: |
22515 | if (REF_PARENTHESIZED_P (t)) |
22516 | RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0)))); |
22517 | else |
22518 | /* Recreate the PAREN_EXPR from __builtin_assoc_barrier. */ |
22519 | { |
22520 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
22521 | RETURN (build1_loc (input_location, PAREN_EXPR, |
22522 | TREE_TYPE (op0), op0)); |
22523 | } |
22524 | |
22525 | case VEC_PERM_EXPR: |
22526 | { |
22527 | tree op0 = RECUR (TREE_OPERAND (t, 0)); |
22528 | tree op1 = RECUR (TREE_OPERAND (t, 1)); |
22529 | tree op2 = RECUR (TREE_OPERAND (t, 2)); |
22530 | RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2, |
22531 | complain)); |
22532 | } |
22533 | |
22534 | case REQUIRES_EXPR: |
22535 | { |
22536 | complain &= ~tf_warning_or_error; |
22537 | tree r = tsubst_requires_expr (t, args, complain, in_decl); |
22538 | RETURN (r); |
22539 | } |
22540 | |
22541 | case RANGE_EXPR: |
22542 | /* No need to substitute further, a RANGE_EXPR will always be built |
22543 | with constant operands. */ |
22544 | RETURN (t); |
22545 | |
22546 | case NON_LVALUE_EXPR: |
22547 | case VIEW_CONVERT_EXPR: |
22548 | { |
22549 | tree op = RECUR (TREE_OPERAND (t, 0)); |
22550 | |
22551 | if (location_wrapper_p (exp: t)) |
22552 | /* We need to do this here as well as in tsubst_copy so we get the |
22553 | other tsubst_copy_and_build semantics for a PARM_DECL operand. */ |
22554 | RETURN (maybe_wrap_with_location (op, EXPR_LOCATION (t))); |
22555 | |
22556 | gcc_checking_assert (TREE_CODE (t) == VIEW_CONVERT_EXPR); |
22557 | if (REF_PARENTHESIZED_P (t)) |
22558 | /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */ |
22559 | RETURN (finish_parenthesized_expr (op)); |
22560 | |
22561 | /* Otherwise, we're dealing with a wrapper to make a C++20 template |
22562 | parameter object const. */ |
22563 | if (TREE_TYPE (op) == NULL_TREE |
22564 | || !CP_TYPE_CONST_P (TREE_TYPE (op))) |
22565 | { |
22566 | /* The template argument is not const, presumably because |
22567 | it is still dependent, and so not the const template parm |
22568 | object. */ |
22569 | tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); |
22570 | if (TREE_CODE (op) == CONSTRUCTOR |
22571 | || TREE_CODE (op) == IMPLICIT_CONV_EXPR) |
22572 | { |
22573 | /* Don't add a wrapper to these. */ |
22574 | op = copy_node (op); |
22575 | TREE_TYPE (op) = type; |
22576 | } |
22577 | else |
22578 | /* Do add a wrapper otherwise (in particular, if op is |
22579 | another TEMPLATE_PARM_INDEX). */ |
22580 | op = build1 (VIEW_CONVERT_EXPR, type, op); |
22581 | } |
22582 | RETURN (op); |
22583 | } |
22584 | |
22585 | case TU_LOCAL_ENTITY: |
22586 | complain_about_tu_local_entity (e: t); |
22587 | RETURN (error_mark_node); |
22588 | |
22589 | default: |
22590 | /* Handle Objective-C++ constructs, if appropriate. */ |
22591 | if (tree subst = objcp_tsubst_expr (t, args, complain, in_decl)) |
22592 | RETURN (subst); |
22593 | |
22594 | /* We shouldn't get here, but keep going if !flag_checking. */ |
22595 | if (flag_checking) |
22596 | gcc_unreachable (); |
22597 | RETURN (t); |
22598 | } |
22599 | |
22600 | #undef RECUR |
22601 | #undef RETURN |
22602 | out: |
22603 | input_location = save_loc; |
22604 | return retval; |
22605 | } |
22606 | |
22607 | /* Verify that the instantiated ARGS are valid. For type arguments, |
22608 | make sure that the type's linkage is ok. For non-type arguments, |
22609 | make sure they are constants if they are integral or enumerations. |
22610 | Emit an error under control of COMPLAIN, and return TRUE on error. */ |
22611 | |
22612 | static bool |
22613 | check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain) |
22614 | { |
22615 | if (dependent_template_arg_p (t)) |
22616 | return false; |
22617 | if (ARGUMENT_PACK_P (t)) |
22618 | { |
22619 | tree vec = ARGUMENT_PACK_ARGS (t); |
22620 | int len = TREE_VEC_LENGTH (vec); |
22621 | bool result = false; |
22622 | int i; |
22623 | |
22624 | for (i = 0; i < len; ++i) |
22625 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain)) |
22626 | result = true; |
22627 | return result; |
22628 | } |
22629 | else if (TYPE_P (t)) |
22630 | { |
22631 | /* [basic.link]: A name with no linkage (notably, the name |
22632 | of a class or enumeration declared in a local scope) |
22633 | shall not be used to declare an entity with linkage. |
22634 | This implies that names with no linkage cannot be used as |
22635 | template arguments |
22636 | |
22637 | DR 757 relaxes this restriction for C++0x. */ |
22638 | tree nt = (cxx_dialect > cxx98 ? NULL_TREE |
22639 | : no_linkage_check (t, /*relaxed_p=*/false)); |
22640 | |
22641 | if (nt) |
22642 | { |
22643 | /* DR 488 makes use of a type with no linkage cause |
22644 | type deduction to fail. */ |
22645 | if (complain & tf_error) |
22646 | { |
22647 | if (TYPE_UNNAMED_P (nt)) |
22648 | error ("%qT is/uses unnamed type", t); |
22649 | else |
22650 | error ("template argument for %qD uses local type %qT", |
22651 | tmpl, t); |
22652 | } |
22653 | return true; |
22654 | } |
22655 | /* In order to avoid all sorts of complications, we do not |
22656 | allow variably-modified types as template arguments. */ |
22657 | else if (variably_modified_type_p (t, NULL_TREE)) |
22658 | { |
22659 | if (complain & tf_error) |
22660 | error ("%qT is a variably modified type", t); |
22661 | return true; |
22662 | } |
22663 | } |
22664 | /* Class template and alias template arguments should be OK. */ |
22665 | else if (DECL_TYPE_TEMPLATE_P (t)) |
22666 | ; |
22667 | /* A non-type argument of integral or enumerated type must be a |
22668 | constant. */ |
22669 | else if (TREE_TYPE (t) |
22670 | && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)) |
22671 | && !REFERENCE_REF_P (t) |
22672 | && !TREE_CONSTANT (t)) |
22673 | { |
22674 | if (complain & tf_error) |
22675 | error ("integral expression %qE is not constant", t); |
22676 | return true; |
22677 | } |
22678 | return false; |
22679 | } |
22680 | |
22681 | static bool |
22682 | check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain) |
22683 | { |
22684 | int ix, len = DECL_NTPARMS (tmpl); |
22685 | bool result = false; |
22686 | |
22687 | for (ix = 0; ix != len; ix++) |
22688 | { |
22689 | if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain)) |
22690 | result = true; |
22691 | } |
22692 | if (result && (complain & tf_error)) |
22693 | error (" trying to instantiate %qD", tmpl); |
22694 | return result; |
22695 | } |
22696 | |
22697 | /* Call mark_used on each entity within the non-type template arguments in |
22698 | ARGS for an instantiation of TMPL, to ensure that each such entity is |
22699 | considered odr-used (and therefore marked for instantiation) regardless of |
22700 | whether the specialization was first formed in a template context (which |
22701 | inhibits mark_used). |
22702 | |
22703 | This function assumes push_to_top_level has been called beforehand. */ |
22704 | |
22705 | static void |
22706 | mark_template_arguments_used (tree tmpl, tree args) |
22707 | { |
22708 | /* It suffices to do this only when instantiating a primary template. */ |
22709 | if (TREE_CODE (tmpl) != TEMPLATE_DECL || !PRIMARY_TEMPLATE_P (tmpl)) |
22710 | return; |
22711 | |
22712 | /* We already marked outer arguments when specializing the context. */ |
22713 | args = INNERMOST_TEMPLATE_ARGS (args); |
22714 | |
22715 | for (tree arg : tree_vec_range (args)) |
22716 | { |
22717 | /* A (pointer/reference to) function or variable NTTP argument. */ |
22718 | if (TREE_CODE (arg) == ADDR_EXPR |
22719 | || TREE_CODE (arg) == INDIRECT_REF) |
22720 | { |
22721 | while (TREE_CODE (arg) == ADDR_EXPR |
22722 | || REFERENCE_REF_P (arg) |
22723 | || CONVERT_EXPR_P (arg)) |
22724 | arg = TREE_OPERAND (arg, 0); |
22725 | if (VAR_OR_FUNCTION_DECL_P (arg)) |
22726 | { |
22727 | /* Pass tf_none to avoid duplicate diagnostics: if this call |
22728 | fails then an earlier call to mark_used for this argument |
22729 | must have also failed and emitted a diagnostic. */ |
22730 | bool ok = mark_used (arg, tf_none); |
22731 | gcc_checking_assert (ok || seen_error ()); |
22732 | } |
22733 | } |
22734 | /* A member function pointer. */ |
22735 | else if (TREE_CODE (arg) == PTRMEM_CST) |
22736 | { |
22737 | bool ok = mark_used (PTRMEM_CST_MEMBER (arg), tf_none); |
22738 | gcc_checking_assert (ok || seen_error ()); |
22739 | } |
22740 | /* A class NTTP argument. */ |
22741 | else if (VAR_P (arg) |
22742 | && DECL_NTTP_OBJECT_P (arg)) |
22743 | { |
22744 | auto mark_used_r = [](tree *tp, int *, void *) { |
22745 | if (VAR_OR_FUNCTION_DECL_P (*tp)) |
22746 | { |
22747 | bool ok = mark_used (*tp, tf_none); |
22748 | gcc_checking_assert (ok || seen_error ()); |
22749 | } |
22750 | return NULL_TREE; |
22751 | }; |
22752 | cp_walk_tree_without_duplicates (&DECL_INITIAL (arg), |
22753 | mark_used_r, nullptr); |
22754 | } |
22755 | } |
22756 | } |
22757 | |
22758 | /* We're out of SFINAE context now, so generate diagnostics for the access |
22759 | errors we saw earlier when instantiating D from TMPL and ARGS. */ |
22760 | |
22761 | static void |
22762 | recheck_decl_substitution (tree d, tree tmpl, tree args) |
22763 | { |
22764 | tree pattern = DECL_TEMPLATE_RESULT (tmpl); |
22765 | tree type = TREE_TYPE (pattern); |
22766 | location_t loc = input_location; |
22767 | |
22768 | push_access_scope (t: d); |
22769 | push_deferring_access_checks (dk_no_deferred); |
22770 | input_location = DECL_SOURCE_LOCATION (pattern); |
22771 | tsubst (t: type, args, complain: tf_warning_or_error, in_decl: d); |
22772 | input_location = loc; |
22773 | pop_deferring_access_checks (); |
22774 | pop_access_scope (t: d); |
22775 | } |
22776 | |
22777 | /* Instantiate the indicated variable, function, or alias template TMPL with |
22778 | the template arguments in TARG_PTR. */ |
22779 | |
22780 | tree |
22781 | instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) |
22782 | { |
22783 | auto_timevar tv (TV_TEMPLATE_INST); |
22784 | |
22785 | tree targ_ptr = orig_args; |
22786 | tree fndecl; |
22787 | tree gen_tmpl; |
22788 | bool access_ok = true; |
22789 | |
22790 | if (tmpl == error_mark_node) |
22791 | return error_mark_node; |
22792 | |
22793 | /* The other flags are not relevant anymore here, especially tf_partial |
22794 | shouldn't be set. For instance, we may be called while doing a partial |
22795 | substitution of a template variable, but the type of the variable |
22796 | template may be auto, in which case we will call do_auto_deduction |
22797 | in mark_used (which clears tf_partial) and the auto must be properly |
22798 | reduced at that time for the deduction to work. |
22799 | |
22800 | Note that later below we set tf_partial iff there are dependent arguments; |
22801 | the above is concerned specifically about the non-dependent case. */ |
22802 | complain &= tf_warning_or_error; |
22803 | |
22804 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
22805 | |
22806 | if (modules_p ()) |
22807 | lazy_load_pendings (decl: tmpl); |
22808 | |
22809 | /* If this function is a clone, handle it specially. */ |
22810 | if (DECL_CLONED_FUNCTION_P (tmpl)) |
22811 | { |
22812 | tree spec; |
22813 | tree clone; |
22814 | |
22815 | /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have |
22816 | DECL_CLONED_FUNCTION. */ |
22817 | spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl), |
22818 | orig_args: targ_ptr, complain); |
22819 | if (spec == error_mark_node) |
22820 | return error_mark_node; |
22821 | |
22822 | /* Look for the clone. */ |
22823 | FOR_EACH_CLONE (clone, spec) |
22824 | if (DECL_NAME (clone) == DECL_NAME (tmpl)) |
22825 | return clone; |
22826 | /* We should always have found the clone by now. */ |
22827 | gcc_unreachable (); |
22828 | return NULL_TREE; |
22829 | } |
22830 | |
22831 | if (targ_ptr == error_mark_node) |
22832 | return error_mark_node; |
22833 | |
22834 | /* Check to see if we already have this specialization. */ |
22835 | gen_tmpl = most_general_template (tmpl); |
22836 | if (TMPL_ARGS_DEPTH (targ_ptr) |
22837 | < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))) |
22838 | /* targ_ptr only has the innermost template args, so add the outer ones |
22839 | from tmpl, which could be either a partial instantiation or gen_tmpl (in |
22840 | the case of a non-dependent call within a template definition). */ |
22841 | targ_ptr = (add_outermost_template_args |
22842 | (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)), |
22843 | extra_args: targ_ptr)); |
22844 | |
22845 | hashval_t hash = spec_hasher::hash (tmpl: gen_tmpl, args: targ_ptr); |
22846 | tree spec = retrieve_specialization (tmpl: gen_tmpl, args: targ_ptr, hash); |
22847 | |
22848 | gcc_checking_assert (tmpl == gen_tmpl |
22849 | || ((fndecl |
22850 | = retrieve_specialization (tmpl, orig_args, 0)) |
22851 | == spec) |
22852 | || fndecl == NULL_TREE); |
22853 | |
22854 | if (spec != NULL_TREE) |
22855 | { |
22856 | if (FNDECL_HAS_ACCESS_ERRORS (spec)) |
22857 | { |
22858 | if (complain & tf_error) |
22859 | recheck_decl_substitution (d: spec, tmpl: gen_tmpl, args: targ_ptr); |
22860 | return error_mark_node; |
22861 | } |
22862 | return spec; |
22863 | } |
22864 | |
22865 | if (check_instantiated_args (tmpl: gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr), |
22866 | complain)) |
22867 | return error_mark_node; |
22868 | |
22869 | /* We are building a FUNCTION_DECL, during which the access of its |
22870 | parameters and return types have to be checked. However this |
22871 | FUNCTION_DECL which is the desired context for access checking |
22872 | is not built yet. We solve this chicken-and-egg problem by |
22873 | deferring all checks until we have the FUNCTION_DECL. */ |
22874 | deferring_access_check_sentinel dacs (dk_deferred); |
22875 | |
22876 | /* Instantiation of the function happens in the context of the function |
22877 | template, not the context of the overload resolution we're doing. */ |
22878 | push_to_top_level (); |
22879 | /* If there are dependent arguments, e.g. because we're doing partial |
22880 | ordering, make sure processing_template_decl stays set. And set |
22881 | tf_partial mainly for benefit of instantiation of alias templates |
22882 | that contain extra-args trees. */ |
22883 | if (uses_template_parms (t: targ_ptr)) |
22884 | { |
22885 | ++processing_template_decl; |
22886 | complain |= tf_partial; |
22887 | } |
22888 | if (DECL_CLASS_SCOPE_P (gen_tmpl)) |
22889 | { |
22890 | tree ctx; |
22891 | if (!uses_template_parms (DECL_CONTEXT (tmpl))) |
22892 | /* If the context of the partially instantiated template is |
22893 | already non-dependent, then we might as well use it. */ |
22894 | ctx = DECL_CONTEXT (tmpl); |
22895 | else |
22896 | ctx = tsubst_entering_scope (DECL_CONTEXT (gen_tmpl), args: targ_ptr, |
22897 | complain, in_decl: gen_tmpl); |
22898 | push_nested_class (ctx); |
22899 | } |
22900 | |
22901 | tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl); |
22902 | |
22903 | tree partial_ti = NULL_TREE; |
22904 | fndecl = NULL_TREE; |
22905 | if (VAR_P (pattern)) |
22906 | { |
22907 | /* We need to determine if we're using a partial or explicit |
22908 | specialization now, because the type of the variable could be |
22909 | different. */ |
22910 | tree tid = build2 (TEMPLATE_ID_EXPR, NULL_TREE, tmpl, targ_ptr); |
22911 | partial_ti = most_specialized_partial_spec (tid, complain); |
22912 | if (partial_ti == error_mark_node) |
22913 | pattern = error_mark_node; |
22914 | else if (partial_ti) |
22915 | { |
22916 | tree partial_tmpl = TI_TEMPLATE (partial_ti); |
22917 | tree partial_args = TI_ARGS (partial_ti); |
22918 | tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl); |
22919 | fndecl = tsubst_decl (t: partial_pat, args: partial_args, complain, |
22920 | /*use_spec_table=*/false); |
22921 | } |
22922 | } |
22923 | |
22924 | /* Substitute template parameters to obtain the specialization. */ |
22925 | if (fndecl == NULL_TREE) |
22926 | fndecl = tsubst_decl (t: pattern, args: targ_ptr, complain, /*use_spec_table=*/false); |
22927 | if (DECL_CLASS_SCOPE_P (gen_tmpl)) |
22928 | pop_nested_class (); |
22929 | pop_from_top_level (); |
22930 | |
22931 | if (fndecl == error_mark_node) |
22932 | return error_mark_node; |
22933 | |
22934 | /* Substituting the type might have recursively instantiated this |
22935 | same alias (c++/117530). */ |
22936 | if (DECL_ALIAS_TEMPLATE_P (gen_tmpl) |
22937 | && (spec = retrieve_specialization (tmpl: gen_tmpl, args: targ_ptr, hash))) |
22938 | return spec; |
22939 | |
22940 | /* The DECL_TI_TEMPLATE should always be the immediate parent |
22941 | template, not the most general template. */ |
22942 | DECL_TI_TEMPLATE (fndecl) = tmpl; |
22943 | DECL_TI_ARGS (fndecl) = targ_ptr; |
22944 | if (VAR_P (pattern)) |
22945 | { |
22946 | /* Now that we we've formed this variable template specialization, |
22947 | remember the result of most_specialized_partial_spec for it. */ |
22948 | TI_PARTIAL_INFO (DECL_TEMPLATE_INFO (fndecl)) = partial_ti; |
22949 | |
22950 | /* And remember if the variable was declared with []. */ |
22951 | if (TREE_CODE (TREE_TYPE (fndecl)) == ARRAY_TYPE |
22952 | && TYPE_DOMAIN (TREE_TYPE (fndecl)) == NULL_TREE) |
22953 | SET_VAR_HAD_UNKNOWN_BOUND (fndecl); |
22954 | } |
22955 | |
22956 | fndecl = register_specialization (spec: fndecl, tmpl: gen_tmpl, args: targ_ptr, is_friend: false, hash); |
22957 | if (fndecl == error_mark_node) |
22958 | return error_mark_node; |
22959 | |
22960 | set_instantiating_module (fndecl); |
22961 | |
22962 | /* Now we know the specialization, compute access previously |
22963 | deferred. Do no access control for inheriting constructors, |
22964 | as we already checked access for the inherited constructor. */ |
22965 | if (!(flag_new_inheriting_ctors |
22966 | && DECL_INHERITED_CTOR (fndecl))) |
22967 | { |
22968 | push_access_scope (t: fndecl); |
22969 | if (!perform_deferred_access_checks (complain)) |
22970 | access_ok = false; |
22971 | pop_access_scope (t: fndecl); |
22972 | } |
22973 | |
22974 | /* If we've just instantiated the main entry point for a function, |
22975 | instantiate all the alternate entry points as well. We do this |
22976 | by cloning the instantiation of the main entry point, not by |
22977 | instantiating the template clones. */ |
22978 | if (tree chain = DECL_CHAIN (gen_tmpl)) |
22979 | if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain)) |
22980 | clone_cdtor (fndecl, /*update_methods=*/false); |
22981 | |
22982 | if (!access_ok) |
22983 | { |
22984 | if (!(complain & tf_error)) |
22985 | { |
22986 | /* Remember to reinstantiate when we're out of SFINAE so the user |
22987 | can see the errors. */ |
22988 | FNDECL_HAS_ACCESS_ERRORS (fndecl) = true; |
22989 | } |
22990 | return error_mark_node; |
22991 | } |
22992 | |
22993 | return fndecl; |
22994 | } |
22995 | |
22996 | /* Instantiate the alias template TMPL with ARGS. Also push a template |
22997 | instantiation level, which instantiate_template doesn't do because |
22998 | functions and variables have sufficient context established by the |
22999 | callers. */ |
23000 | |
23001 | static tree |
23002 | instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain) |
23003 | { |
23004 | if (tmpl == error_mark_node || args == error_mark_node) |
23005 | return error_mark_node; |
23006 | |
23007 | args = coerce_template_parms (DECL_TEMPLATE_PARMS (tmpl), |
23008 | args, in_decl: tmpl, complain); |
23009 | if (args == error_mark_node) |
23010 | return error_mark_node; |
23011 | |
23012 | /* FIXME check for satisfaction in check_instantiated_args. */ |
23013 | if (!constraints_satisfied_p (tmpl, args)) |
23014 | { |
23015 | if (complain & tf_error) |
23016 | { |
23017 | auto_diagnostic_group d; |
23018 | error ("template constraint failure for %qD", tmpl); |
23019 | diagnose_constraints (input_location, tmpl, args); |
23020 | } |
23021 | return error_mark_node; |
23022 | } |
23023 | |
23024 | if (!push_tinst_level (tmpl, args)) |
23025 | return error_mark_node; |
23026 | tree r = instantiate_template (tmpl, orig_args: args, complain); |
23027 | pop_tinst_level (); |
23028 | |
23029 | if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), transparent_typedefs: nt_opaque)) |
23030 | { |
23031 | /* Note this is also done at parse time from push_template_decl. */ |
23032 | /* An alias template specialization can be dependent |
23033 | even if its underlying type is not. */ |
23034 | TYPE_DEPENDENT_P (d) = true; |
23035 | TYPE_DEPENDENT_P_VALID (d) = true; |
23036 | /* Sometimes a dependent alias spec is equivalent to its expansion, |
23037 | sometimes not. So always use structural_comptypes. */ |
23038 | SET_TYPE_STRUCTURAL_EQUALITY (d); |
23039 | } |
23040 | |
23041 | return r; |
23042 | } |
23043 | |
23044 | /* PARM is a template parameter pack for FN. Returns true iff |
23045 | PARM is used in a deducible way in the argument list of FN. */ |
23046 | |
23047 | static bool |
23048 | pack_deducible_p (tree parm, tree fn) |
23049 | { |
23050 | tree t = FUNCTION_FIRST_USER_PARMTYPE (fn); |
23051 | for (; t; t = TREE_CHAIN (t)) |
23052 | { |
23053 | tree type = TREE_VALUE (t); |
23054 | tree packs; |
23055 | if (!PACK_EXPANSION_P (type)) |
23056 | continue; |
23057 | for (packs = PACK_EXPANSION_PARAMETER_PACKS (type); |
23058 | packs; packs = TREE_CHAIN (packs)) |
23059 | if (template_args_equal (TREE_VALUE (packs), nt: parm)) |
23060 | { |
23061 | /* The template parameter pack is used in a function parameter |
23062 | pack. If this is the end of the parameter list, the |
23063 | template parameter pack is deducible. */ |
23064 | if (TREE_CHAIN (t) == void_list_node) |
23065 | return true; |
23066 | else |
23067 | /* Otherwise, not. Well, it could be deduced from |
23068 | a non-pack parameter, but doing so would end up with |
23069 | a deduction mismatch, so don't bother. */ |
23070 | return false; |
23071 | } |
23072 | } |
23073 | /* The template parameter pack isn't used in any function parameter |
23074 | packs, but it might be used deeper, e.g. tuple<Args...>. */ |
23075 | return true; |
23076 | } |
23077 | |
23078 | /* Subroutine of fn_type_unification: check non-dependent parms for |
23079 | convertibility. */ |
23080 | |
23081 | static int |
23082 | check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs, |
23083 | tree fn, unification_kind_t strict, int flags, |
23084 | struct conversion **convs, bool explain_p, |
23085 | bool noninst_only_p) |
23086 | { |
23087 | /* Non-constructor methods need to leave a conversion for 'this', which |
23088 | isn't included in nargs here. */ |
23089 | unsigned offset = (DECL_IOBJ_MEMBER_FUNCTION_P (fn) |
23090 | && !DECL_CONSTRUCTOR_P (fn)); |
23091 | |
23092 | for (unsigned ia = 0; |
23093 | parms && parms != void_list_node && ia < nargs; ) |
23094 | { |
23095 | tree parm = TREE_VALUE (parms); |
23096 | |
23097 | if (TREE_CODE (parm) == TYPE_PACK_EXPANSION |
23098 | && (!TREE_CHAIN (parms) |
23099 | || TREE_CHAIN (parms) == void_list_node)) |
23100 | /* For a function parameter pack that occurs at the end of the |
23101 | parameter-declaration-list, the type A of each remaining |
23102 | argument of the call is compared with the type P of the |
23103 | declarator-id of the function parameter pack. */ |
23104 | break; |
23105 | |
23106 | parms = TREE_CHAIN (parms); |
23107 | |
23108 | if (TREE_CODE (parm) == TYPE_PACK_EXPANSION) |
23109 | /* For a function parameter pack that does not occur at the |
23110 | end of the parameter-declaration-list, the type of the |
23111 | parameter pack is a non-deduced context. */ |
23112 | continue; |
23113 | |
23114 | if (!uses_template_parms (t: parm)) |
23115 | { |
23116 | tree arg = args[ia]; |
23117 | conversion **conv_p = convs ? &convs[ia+offset] : NULL; |
23118 | int lflags = conv_flags (ia, nargs, fn, arg, flags); |
23119 | |
23120 | if (check_non_deducible_conversion (parm, arg, strict, lflags, |
23121 | conv_p, explain_p, noninst_only_p)) |
23122 | return 1; |
23123 | } |
23124 | |
23125 | ++ia; |
23126 | } |
23127 | |
23128 | return 0; |
23129 | } |
23130 | |
23131 | /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with |
23132 | NARGS elements of the arguments that are being used when calling |
23133 | it. TARGS is a vector into which the deduced template arguments |
23134 | are placed. |
23135 | |
23136 | Returns either a FUNCTION_DECL for the matching specialization of FN or |
23137 | NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is |
23138 | true, diagnostics will be printed to explain why it failed. |
23139 | |
23140 | If FN is a conversion operator, or we are trying to produce a specific |
23141 | specialization, RETURN_TYPE is the return type desired. |
23142 | |
23143 | The EXPLICIT_TARGS are explicit template arguments provided via a |
23144 | template-id. |
23145 | |
23146 | The parameter STRICT is one of: |
23147 | |
23148 | DEDUCE_CALL: |
23149 | We are deducing arguments for a function call, as in |
23150 | [temp.deduct.call]. If RETURN_TYPE is non-null, we are |
23151 | deducing arguments for a call to the result of a conversion |
23152 | function template, as in [over.call.object]. |
23153 | |
23154 | DEDUCE_CONV: |
23155 | We are deducing arguments for a conversion function, as in |
23156 | [temp.deduct.conv]. |
23157 | |
23158 | DEDUCE_EXACT: |
23159 | We are deducing arguments when doing an explicit instantiation |
23160 | as in [temp.explicit], when determining an explicit specialization |
23161 | as in [temp.expl.spec], or when taking the address of a function |
23162 | template, as in [temp.deduct.funcaddr]. */ |
23163 | |
23164 | tree |
23165 | fn_type_unification (tree fn, |
23166 | tree explicit_targs, |
23167 | tree targs, |
23168 | const tree *args, |
23169 | unsigned int nargs, |
23170 | tree return_type, |
23171 | unification_kind_t strict, |
23172 | int flags, |
23173 | struct conversion **convs, |
23174 | bool explain_p, |
23175 | bool decltype_p) |
23176 | { |
23177 | tree parms; |
23178 | tree fntype; |
23179 | tree decl = NULL_TREE; |
23180 | tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none); |
23181 | bool ok; |
23182 | static int deduction_depth; |
23183 | /* type_unification_real will pass back any access checks from default |
23184 | template argument substitution. */ |
23185 | vec<deferred_access_check, va_gc> *checks = NULL; |
23186 | /* We don't have all the template args yet. */ |
23187 | bool incomplete = true; |
23188 | |
23189 | tree orig_fn = fn; |
23190 | if (flag_new_inheriting_ctors) |
23191 | fn = strip_inheriting_ctors (fn); |
23192 | |
23193 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn); |
23194 | tree r = error_mark_node; |
23195 | |
23196 | tree full_targs = targs; |
23197 | if (TMPL_ARGS_DEPTH (targs) |
23198 | < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn))) |
23199 | full_targs = (add_outermost_template_args |
23200 | (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)), |
23201 | extra_args: targs)); |
23202 | |
23203 | if (decltype_p) |
23204 | complain |= tf_decltype; |
23205 | |
23206 | /* In C++0x, it's possible to have a function template whose type depends |
23207 | on itself recursively. This is most obvious with decltype, but can also |
23208 | occur with enumeration scope (c++/48969). So we need to catch infinite |
23209 | recursion and reject the substitution at deduction time; this function |
23210 | will return error_mark_node for any repeated substitution. |
23211 | |
23212 | This also catches excessive recursion such as when f<N> depends on |
23213 | f<N-1> across all integers, and returns error_mark_node for all the |
23214 | substitutions back up to the initial one. |
23215 | |
23216 | This is, of course, not reentrant. */ |
23217 | if (excessive_deduction_depth) |
23218 | return error_mark_node; |
23219 | ++deduction_depth; |
23220 | |
23221 | gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL); |
23222 | |
23223 | fntype = TREE_TYPE (fn); |
23224 | if (explicit_targs) |
23225 | { |
23226 | /* [temp.deduct] |
23227 | |
23228 | The specified template arguments must match the template |
23229 | parameters in kind (i.e., type, nontype, template), and there |
23230 | must not be more arguments than there are parameters; |
23231 | otherwise type deduction fails. |
23232 | |
23233 | Nontype arguments must match the types of the corresponding |
23234 | nontype template parameters, or must be convertible to the |
23235 | types of the corresponding nontype parameters as specified in |
23236 | _temp.arg.nontype_, otherwise type deduction fails. |
23237 | |
23238 | All references in the function type of the function template |
23239 | to the corresponding template parameters are replaced by the |
23240 | specified template argument values. If a substitution in a |
23241 | template parameter or in the function type of the function |
23242 | template results in an invalid type, type deduction fails. */ |
23243 | int i, len = TREE_VEC_LENGTH (tparms); |
23244 | location_t loc = input_location; |
23245 | incomplete = false; |
23246 | |
23247 | if (explicit_targs == error_mark_node) |
23248 | goto fail; |
23249 | |
23250 | if (TMPL_ARGS_DEPTH (explicit_targs) |
23251 | < TMPL_ARGS_DEPTH (full_targs)) |
23252 | explicit_targs = add_outermost_template_args (args: full_targs, |
23253 | extra_args: explicit_targs); |
23254 | |
23255 | /* Adjust any explicit template arguments before entering the |
23256 | substitution context. */ |
23257 | explicit_targs |
23258 | = (coerce_template_parms (parms: tparms, args: explicit_targs, in_decl: fn, |
23259 | complain: complain|tf_partial, |
23260 | /*require_all_args=*/false)); |
23261 | if (explicit_targs == error_mark_node) |
23262 | goto fail; |
23263 | |
23264 | /* Substitute the explicit args into the function type. This is |
23265 | necessary so that, for instance, explicitly declared function |
23266 | arguments can match null pointed constants. If we were given |
23267 | an incomplete set of explicit args, we must not do semantic |
23268 | processing during substitution as we could create partial |
23269 | instantiations. */ |
23270 | for (i = 0; i < len; i++) |
23271 | { |
23272 | tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i)); |
23273 | bool parameter_pack = false; |
23274 | tree targ = TREE_VEC_ELT (explicit_targs, i); |
23275 | |
23276 | /* Dig out the actual parm. */ |
23277 | if (TREE_CODE (parm) == TYPE_DECL |
23278 | || TREE_CODE (parm) == TEMPLATE_DECL) |
23279 | { |
23280 | parm = TREE_TYPE (parm); |
23281 | parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm); |
23282 | } |
23283 | else if (TREE_CODE (parm) == PARM_DECL) |
23284 | { |
23285 | parm = DECL_INITIAL (parm); |
23286 | parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm); |
23287 | } |
23288 | |
23289 | if (targ == NULL_TREE) |
23290 | /* No explicit argument for this template parameter. */ |
23291 | incomplete = true; |
23292 | else if (parameter_pack && pack_deducible_p (parm, fn)) |
23293 | { |
23294 | /* Mark the argument pack as "incomplete". We could |
23295 | still deduce more arguments during unification. |
23296 | We remove this mark in type_unification_real. */ |
23297 | ARGUMENT_PACK_INCOMPLETE_P(targ) = 1; |
23298 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) |
23299 | = ARGUMENT_PACK_ARGS (targ); |
23300 | |
23301 | /* We have some incomplete argument packs. */ |
23302 | incomplete = true; |
23303 | } |
23304 | } |
23305 | |
23306 | if (incomplete) |
23307 | { |
23308 | if (!push_tinst_level (tmpl: fn, args: explicit_targs)) |
23309 | { |
23310 | excessive_deduction_depth = true; |
23311 | goto fail; |
23312 | } |
23313 | ++processing_template_decl; |
23314 | input_location = DECL_SOURCE_LOCATION (fn); |
23315 | /* Ignore any access checks; we'll see them again in |
23316 | instantiate_template and they might have the wrong |
23317 | access path at this point. */ |
23318 | push_deferring_access_checks (dk_deferred); |
23319 | tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type; |
23320 | fntype = tsubst (TREE_TYPE (fn), args: explicit_targs, complain: ecomplain, NULL_TREE); |
23321 | pop_deferring_access_checks (); |
23322 | input_location = loc; |
23323 | --processing_template_decl; |
23324 | pop_tinst_level (); |
23325 | |
23326 | if (fntype == error_mark_node) |
23327 | goto fail; |
23328 | } |
23329 | |
23330 | /* Place the explicitly specified arguments in TARGS. */ |
23331 | explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs); |
23332 | for (i = NUM_TMPL_ARGS (explicit_targs); i--;) |
23333 | TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i); |
23334 | if (!incomplete && CHECKING_P |
23335 | && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) |
23336 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT |
23337 | (targs, NUM_TMPL_ARGS (explicit_targs)); |
23338 | } |
23339 | |
23340 | if (return_type && strict != DEDUCE_CALL) |
23341 | { |
23342 | tree *new_args = XALLOCAVEC (tree, nargs + 1); |
23343 | new_args[0] = return_type; |
23344 | memcpy (dest: new_args + 1, src: args, n: nargs * sizeof (tree)); |
23345 | args = new_args; |
23346 | ++nargs; |
23347 | } |
23348 | |
23349 | if (!incomplete) |
23350 | goto deduced; |
23351 | |
23352 | /* Never do unification on the 'this' parameter. */ |
23353 | parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype)); |
23354 | |
23355 | if (return_type && strict == DEDUCE_CALL) |
23356 | { |
23357 | /* We're deducing for a call to the result of a template conversion |
23358 | function. The parms we really want are in return_type. */ |
23359 | if (INDIRECT_TYPE_P (return_type)) |
23360 | return_type = TREE_TYPE (return_type); |
23361 | parms = TYPE_ARG_TYPES (return_type); |
23362 | } |
23363 | else if (return_type) |
23364 | { |
23365 | parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms); |
23366 | } |
23367 | |
23368 | /* We allow incomplete unification without an error message here |
23369 | because the standard doesn't seem to explicitly prohibit it. Our |
23370 | callers must be ready to deal with unification failures in any |
23371 | event. */ |
23372 | |
23373 | /* If we aren't explaining yet, push tinst context so we can see where |
23374 | any errors (e.g. from class instantiations triggered by instantiation |
23375 | of default template arguments) come from. If we are explaining, this |
23376 | context is redundant. */ |
23377 | if (!explain_p && !push_tinst_level (tmpl: fn, args: targs)) |
23378 | { |
23379 | excessive_deduction_depth = true; |
23380 | goto fail; |
23381 | } |
23382 | |
23383 | ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
23384 | full_targs, parms, args, nargs, /*subr=*/0, |
23385 | strict, &checks, explain_p); |
23386 | if (!explain_p) |
23387 | pop_tinst_level (); |
23388 | if (!ok) |
23389 | goto fail; |
23390 | |
23391 | /* Now that we have bindings for all of the template arguments, |
23392 | ensure that the arguments deduced for the template template |
23393 | parameters have compatible template parameter lists. We cannot |
23394 | check this property before we have deduced all template |
23395 | arguments, because the template parameter types of a template |
23396 | template parameter might depend on prior template parameters |
23397 | deduced after the template template parameter. The following |
23398 | ill-formed example illustrates this issue: |
23399 | |
23400 | template<typename T, template<T> class C> void f(C<5>, T); |
23401 | |
23402 | template<int N> struct X {}; |
23403 | |
23404 | void g() { |
23405 | f(X<5>(), 5l); // error: template argument deduction fails |
23406 | } |
23407 | |
23408 | The template parameter list of 'C' depends on the template type |
23409 | parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to |
23410 | 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the |
23411 | time that we deduce 'C'. */ |
23412 | if (!template_template_parm_bindings_ok_p |
23413 | (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs)) |
23414 | { |
23415 | unify_inconsistent_template_template_parameters (explain_p); |
23416 | goto fail; |
23417 | } |
23418 | |
23419 | deduced: |
23420 | |
23421 | /* As a refinement of CWG2369, check first and foremost non-dependent |
23422 | conversions that we know are not going to induce template instantiation |
23423 | (PR99599). */ |
23424 | if (strict == DEDUCE_CALL |
23425 | && incomplete && flag_concepts |
23426 | && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags, |
23427 | convs, explain_p, |
23428 | /*noninst_only_p=*/true)) |
23429 | goto fail; |
23430 | |
23431 | /* CWG2369: Check satisfaction before non-deducible conversions. */ |
23432 | if (!constraints_satisfied_p (fn, targs)) |
23433 | { |
23434 | if (explain_p) |
23435 | diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs); |
23436 | goto fail; |
23437 | } |
23438 | |
23439 | /* DR 1391: All parameters have args, now check non-dependent parms for |
23440 | convertibility. We don't do this if all args were explicitly specified, |
23441 | as the standard says that we substitute explicit args immediately. */ |
23442 | if (incomplete |
23443 | && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags, |
23444 | convs, explain_p, |
23445 | /*noninst_only_p=*/false)) |
23446 | goto fail; |
23447 | |
23448 | /* All is well so far. Now, check: |
23449 | |
23450 | [temp.deduct] |
23451 | |
23452 | When all template arguments have been deduced, all uses of |
23453 | template parameters in nondeduced contexts are replaced with |
23454 | the corresponding deduced argument values. If the |
23455 | substitution results in an invalid type, as described above, |
23456 | type deduction fails. */ |
23457 | if (!push_tinst_level (tmpl: fn, args: targs)) |
23458 | { |
23459 | excessive_deduction_depth = true; |
23460 | goto fail; |
23461 | } |
23462 | |
23463 | /* Also collect access checks from the instantiation. */ |
23464 | reopen_deferring_access_checks (checks); |
23465 | |
23466 | decl = instantiate_template (tmpl: fn, orig_args: targs, complain); |
23467 | |
23468 | checks = get_deferred_access_checks (); |
23469 | pop_deferring_access_checks (); |
23470 | |
23471 | pop_tinst_level (); |
23472 | |
23473 | if (decl == error_mark_node) |
23474 | goto fail; |
23475 | |
23476 | /* Now perform any access checks encountered during substitution. */ |
23477 | push_access_scope (t: decl); |
23478 | ok = perform_access_checks (checks, complain); |
23479 | pop_access_scope (t: decl); |
23480 | if (!ok) |
23481 | goto fail; |
23482 | |
23483 | /* If we're looking for an exact match, check that what we got |
23484 | is indeed an exact match. It might not be if some template |
23485 | parameters are used in non-deduced contexts. But don't check |
23486 | for an exact match if we have dependent template arguments; |
23487 | in that case we're doing partial ordering, and we already know |
23488 | that we have two candidates that will provide the actual type. */ |
23489 | if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs)) |
23490 | { |
23491 | tree substed = TREE_TYPE (decl); |
23492 | unsigned int i; |
23493 | |
23494 | tree sarg |
23495 | = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed)); |
23496 | if (return_type) |
23497 | sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg); |
23498 | for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg)) |
23499 | if (!same_type_p (args[i], TREE_VALUE (sarg))) |
23500 | { |
23501 | unify_type_mismatch (explain_p, parm: args[i], |
23502 | TREE_VALUE (sarg)); |
23503 | goto fail; |
23504 | } |
23505 | if ((i < nargs || sarg) |
23506 | /* add_candidates uses DEDUCE_EXACT for x.operator foo(), but args |
23507 | doesn't contain the trailing void, and conv fns are always (). */ |
23508 | && !DECL_CONV_FN_P (decl)) |
23509 | { |
23510 | unsigned nsargs = i + list_length (sarg); |
23511 | unify_arity (explain_p, have: nargs, wanted: nsargs); |
23512 | goto fail; |
23513 | } |
23514 | } |
23515 | |
23516 | /* After doing deduction with the inherited constructor, actually return an |
23517 | instantiation of the inheriting constructor. */ |
23518 | if (orig_fn != fn) |
23519 | decl = instantiate_template (tmpl: orig_fn, orig_args: targs, complain); |
23520 | |
23521 | r = decl; |
23522 | |
23523 | fail: |
23524 | --deduction_depth; |
23525 | if (excessive_deduction_depth) |
23526 | { |
23527 | if (deduction_depth == 0) |
23528 | /* Reset once we're all the way out. */ |
23529 | excessive_deduction_depth = false; |
23530 | } |
23531 | |
23532 | return r; |
23533 | } |
23534 | |
23535 | /* Returns true iff PARM is a forwarding reference in the context of |
23536 | template argument deduction for TMPL. */ |
23537 | |
23538 | static bool |
23539 | forwarding_reference_p (tree parm, tree tmpl) |
23540 | { |
23541 | /* [temp.deduct.call], "A forwarding reference is an rvalue reference to a |
23542 | cv-unqualified template parameter ..." */ |
23543 | if (TYPE_REF_P (parm) |
23544 | && TYPE_REF_IS_RVALUE (parm) |
23545 | && TREE_CODE (TREE_TYPE (parm)) == TEMPLATE_TYPE_PARM |
23546 | && cp_type_quals (TREE_TYPE (parm)) == TYPE_UNQUALIFIED) |
23547 | { |
23548 | parm = TREE_TYPE (parm); |
23549 | /* [temp.deduct.call], "... that does not represent a template parameter |
23550 | of a class template (during class template argument deduction)." */ |
23551 | if (tmpl |
23552 | && deduction_guide_p (tmpl) |
23553 | && DECL_ARTIFICIAL (tmpl)) |
23554 | { |
23555 | /* Since the template parameters of a synthesized guide consist of |
23556 | the template parameters of the class template followed by those of |
23557 | the constructor (if any), we can tell if PARM represents a template |
23558 | parameter of the class template by comparing its index with the |
23559 | arity of the class template. */ |
23560 | tree ctmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (TREE_TYPE (tmpl))); |
23561 | if (TEMPLATE_TYPE_IDX (parm) |
23562 | < TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (ctmpl))) |
23563 | return false; |
23564 | } |
23565 | return true; |
23566 | } |
23567 | return false; |
23568 | } |
23569 | |
23570 | /* Adjust types before performing type deduction, as described in |
23571 | [temp.deduct.call] and [temp.deduct.conv]. The rules in these two |
23572 | sections are symmetric. PARM is the type of a function parameter |
23573 | or the return type of the conversion function. ARG is the type of |
23574 | the argument passed to the call, or the type of the value |
23575 | initialized with the result of the conversion function. |
23576 | ARG_EXPR is the original argument expression, which may be null. */ |
23577 | |
23578 | static int |
23579 | maybe_adjust_types_for_deduction (tree tparms, |
23580 | unification_kind_t strict, |
23581 | tree* parm, |
23582 | tree* arg, |
23583 | tree arg_expr) |
23584 | { |
23585 | int result = 0; |
23586 | |
23587 | switch (strict) |
23588 | { |
23589 | case DEDUCE_CALL: |
23590 | break; |
23591 | |
23592 | case DEDUCE_CONV: |
23593 | /* [temp.deduct.conv] First remove a reference type on parm. |
23594 | DRs 322 & 976 affected this. */ |
23595 | if (TYPE_REF_P (*parm)) |
23596 | *parm = TREE_TYPE (*parm); |
23597 | |
23598 | /* Swap PARM and ARG throughout the remainder of this |
23599 | function; the handling is precisely symmetric since PARM |
23600 | will initialize ARG rather than vice versa. */ |
23601 | std::swap (a&: parm, b&: arg); |
23602 | |
23603 | break; |
23604 | |
23605 | case DEDUCE_EXACT: |
23606 | /* Core issue #873: Do the DR606 thing (see below) for these cases, |
23607 | too, but here handle it by stripping the reference from PARM |
23608 | rather than by adding it to ARG. */ |
23609 | if (forwarding_reference_p (parm: *parm, TPARMS_PRIMARY_TEMPLATE (tparms)) |
23610 | && TYPE_REF_P (*arg) |
23611 | && !TYPE_REF_IS_RVALUE (*arg)) |
23612 | *parm = TREE_TYPE (*parm); |
23613 | /* Nothing else to do in this case. */ |
23614 | return 0; |
23615 | |
23616 | default: |
23617 | gcc_unreachable (); |
23618 | } |
23619 | |
23620 | if (!TYPE_REF_P (*parm)) |
23621 | { |
23622 | /* [temp.deduct.call] |
23623 | |
23624 | If P is not a reference type: |
23625 | |
23626 | --If A is an array type, the pointer type produced by the |
23627 | array-to-pointer standard conversion (_conv.array_) is |
23628 | used in place of A for type deduction; otherwise, |
23629 | |
23630 | --If A is a function type, the pointer type produced by |
23631 | the function-to-pointer standard conversion |
23632 | (_conv.func_) is used in place of A for type deduction; |
23633 | otherwise, |
23634 | |
23635 | --If A is a cv-qualified type, the top level |
23636 | cv-qualifiers of A's type are ignored for type |
23637 | deduction. */ |
23638 | if (TREE_CODE (*arg) == ARRAY_TYPE) |
23639 | *arg = build_pointer_type (TREE_TYPE (*arg)); |
23640 | else if (TREE_CODE (*arg) == FUNCTION_TYPE) |
23641 | *arg = build_pointer_type (*arg); |
23642 | else |
23643 | *arg = TYPE_MAIN_VARIANT (*arg); |
23644 | } |
23645 | |
23646 | /* [temp.deduct.call], "If P is a forwarding reference and the argument is |
23647 | an lvalue, the type 'lvalue reference to A' is used in place of A for |
23648 | type deduction." */ |
23649 | if (forwarding_reference_p (parm: *parm, TPARMS_PRIMARY_TEMPLATE (tparms)) |
23650 | && (arg_expr ? lvalue_p (arg_expr) |
23651 | /* try_one_overload doesn't provide an arg_expr, but |
23652 | functions are always lvalues. */ |
23653 | : TREE_CODE (*arg) == FUNCTION_TYPE)) |
23654 | *arg = build_reference_type (*arg); |
23655 | |
23656 | /* [temp.deduct.call] |
23657 | |
23658 | If P is a cv-qualified type, the top level cv-qualifiers |
23659 | of P's type are ignored for type deduction. If P is a |
23660 | reference type, the type referred to by P is used for |
23661 | type deduction. */ |
23662 | *parm = TYPE_MAIN_VARIANT (*parm); |
23663 | if (TYPE_REF_P (*parm)) |
23664 | { |
23665 | *parm = TREE_TYPE (*parm); |
23666 | result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
23667 | } |
23668 | |
23669 | return result; |
23670 | } |
23671 | |
23672 | /* Return true if computing a conversion from FROM to TO might consider |
23673 | user-defined conversions, which could lead to arbitrary template |
23674 | instantiations (e.g. g++.dg/cpp2a/concepts-nondep1.C). If this predicate |
23675 | returns false then computing the conversion definitely won't try UDCs. |
23676 | |
23677 | Note that this restriction parallels LOOKUP_DEFAULTED for CWG1092, but in |
23678 | this case we want the early filter to pass instead of fail. */ |
23679 | |
23680 | static bool |
23681 | conversion_may_instantiate_p (tree to, tree from) |
23682 | { |
23683 | to = non_reference (to); |
23684 | from = non_reference (from); |
23685 | |
23686 | /* Converting between reference-related types is a standard conversion. */ |
23687 | if (reference_related_p (to, from)) |
23688 | return false; |
23689 | |
23690 | /* Converting to a non-aggregate class type will consider its |
23691 | user-declared constructors, which might induce instantiation. */ |
23692 | if (CLASS_TYPE_P (complete_type (to)) |
23693 | && type_has_converting_constructor (to)) |
23694 | return true; |
23695 | |
23696 | /* Similarly, converting from a class type will consider its conversion |
23697 | functions. */ |
23698 | if (CLASS_TYPE_P (complete_type (from)) |
23699 | && TYPE_HAS_CONVERSION (from)) |
23700 | return true; |
23701 | |
23702 | /* Otherwise, computing this conversion won't risk arbitrary |
23703 | template instantiation. */ |
23704 | return false; |
23705 | } |
23706 | |
23707 | /* Subroutine of fn_type_unification. PARM is a function parameter of a |
23708 | template which doesn't contain any deducible template parameters; check if |
23709 | ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in |
23710 | unify_one_argument. */ |
23711 | |
23712 | static int |
23713 | check_non_deducible_conversion (tree parm, tree arg, unification_kind_t strict, |
23714 | int flags, struct conversion **conv_p, |
23715 | bool explain_p, bool noninst_only_p) |
23716 | { |
23717 | tree type; |
23718 | |
23719 | if (!TYPE_P (arg)) |
23720 | type = TREE_TYPE (arg); |
23721 | else |
23722 | type = arg; |
23723 | |
23724 | if (same_type_p (parm, type)) |
23725 | return unify_success (explain_p); |
23726 | |
23727 | tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none); |
23728 | if (strict == DEDUCE_CONV) |
23729 | { |
23730 | if (can_convert_arg (type, parm, NULL_TREE, flags, complain)) |
23731 | return unify_success (explain_p); |
23732 | } |
23733 | else if (strict == DEDUCE_CALL) |
23734 | { |
23735 | if (conv_p && *conv_p) |
23736 | { |
23737 | /* This conversion was already computed earlier (when |
23738 | computing only non-instantiating conversions). */ |
23739 | gcc_checking_assert (!noninst_only_p); |
23740 | return unify_success (explain_p); |
23741 | } |
23742 | |
23743 | if (noninst_only_p |
23744 | && conversion_may_instantiate_p (to: parm, from: type)) |
23745 | return unify_success (explain_p); |
23746 | |
23747 | bool ok = false; |
23748 | tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg; |
23749 | if (conv_p) |
23750 | /* Avoid recalculating this in add_function_candidate. */ |
23751 | ok = (*conv_p |
23752 | = good_conversion (parm, type, conv_arg, flags, complain)); |
23753 | else |
23754 | ok = can_convert_arg (parm, type, conv_arg, flags, complain); |
23755 | if (ok) |
23756 | return unify_success (explain_p); |
23757 | } |
23758 | |
23759 | if (strict == DEDUCE_EXACT) |
23760 | return unify_type_mismatch (explain_p, parm, arg); |
23761 | else |
23762 | return unify_arg_conversion (explain_p, to_type: parm, from_type: type, arg); |
23763 | } |
23764 | |
23765 | static bool uses_deducible_template_parms (tree type); |
23766 | |
23767 | /* Returns true iff the expression EXPR is one from which a template |
23768 | argument can be deduced. In other words, if it's an undecorated |
23769 | use of a template non-type parameter. */ |
23770 | |
23771 | static bool |
23772 | deducible_expression (tree expr) |
23773 | { |
23774 | /* Strip implicit conversions and implicit INDIRECT_REFs. */ |
23775 | while (CONVERT_EXPR_P (expr) |
23776 | || TREE_CODE (expr) == VIEW_CONVERT_EXPR |
23777 | || TREE_CODE (expr) == IMPLICIT_CONV_EXPR |
23778 | || REFERENCE_REF_P (expr)) |
23779 | expr = TREE_OPERAND (expr, 0); |
23780 | return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX); |
23781 | } |
23782 | |
23783 | /* Returns true iff the array domain DOMAIN uses a template parameter in a |
23784 | deducible way; that is, if it has a max value of <PARM> - 1. */ |
23785 | |
23786 | static bool |
23787 | deducible_array_bound (tree domain) |
23788 | { |
23789 | if (domain == NULL_TREE) |
23790 | return false; |
23791 | |
23792 | tree max = TYPE_MAX_VALUE (domain); |
23793 | if (TREE_CODE (max) != MINUS_EXPR) |
23794 | return false; |
23795 | |
23796 | return deducible_expression (TREE_OPERAND (max, 0)); |
23797 | } |
23798 | |
23799 | /* Returns true iff the template arguments ARGS use a template parameter |
23800 | in a deducible way. */ |
23801 | |
23802 | static bool |
23803 | deducible_template_args (tree args) |
23804 | { |
23805 | for (tree elt : tree_vec_range (args)) |
23806 | { |
23807 | bool deducible; |
23808 | if (ARGUMENT_PACK_P (elt)) |
23809 | deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt)); |
23810 | else |
23811 | { |
23812 | if (PACK_EXPANSION_P (elt)) |
23813 | elt = PACK_EXPANSION_PATTERN (elt); |
23814 | if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM) |
23815 | deducible = true; |
23816 | else if (TYPE_P (elt)) |
23817 | deducible = uses_deducible_template_parms (type: elt); |
23818 | else |
23819 | deducible = deducible_expression (expr: elt); |
23820 | } |
23821 | if (deducible) |
23822 | return true; |
23823 | } |
23824 | return false; |
23825 | } |
23826 | |
23827 | /* Returns true iff TYPE contains any deducible references to template |
23828 | parameters, as per 14.8.2.5. */ |
23829 | |
23830 | static bool |
23831 | uses_deducible_template_parms (tree type) |
23832 | { |
23833 | if (PACK_EXPANSION_P (type)) |
23834 | type = PACK_EXPANSION_PATTERN (type); |
23835 | |
23836 | /* T |
23837 | cv-list T |
23838 | TT<T> |
23839 | TT<i> |
23840 | TT<> */ |
23841 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
23842 | || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) |
23843 | return true; |
23844 | |
23845 | /* T* |
23846 | T& |
23847 | T&& */ |
23848 | if (INDIRECT_TYPE_P (type)) |
23849 | return uses_deducible_template_parms (TREE_TYPE (type)); |
23850 | |
23851 | /* T[integer-constant ] |
23852 | type [i] */ |
23853 | if (TREE_CODE (type) == ARRAY_TYPE) |
23854 | return (uses_deducible_template_parms (TREE_TYPE (type)) |
23855 | || deducible_array_bound (TYPE_DOMAIN (type))); |
23856 | |
23857 | /* T type ::* |
23858 | type T::* |
23859 | T T::* |
23860 | T (type ::*)() |
23861 | type (T::*)() |
23862 | type (type ::*)(T) |
23863 | type (T::*)(T) |
23864 | T (type ::*)(T) |
23865 | T (T::*)() |
23866 | T (T::*)(T) */ |
23867 | if (TYPE_PTRMEM_P (type)) |
23868 | return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type)) |
23869 | || (uses_deducible_template_parms |
23870 | (TYPE_PTRMEM_POINTED_TO_TYPE (type)))); |
23871 | |
23872 | /* template-name <T> (where template-name refers to a class template) |
23873 | template-name <i> (where template-name refers to a class template) */ |
23874 | if (CLASS_TYPE_P (type) |
23875 | && CLASSTYPE_TEMPLATE_INFO (type) |
23876 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) |
23877 | return deducible_template_args (INNERMOST_TEMPLATE_ARGS |
23878 | (CLASSTYPE_TI_ARGS (type))); |
23879 | |
23880 | /* type (T) |
23881 | T() |
23882 | T(T) */ |
23883 | if (FUNC_OR_METHOD_TYPE_P (type)) |
23884 | { |
23885 | if (uses_deducible_template_parms (TREE_TYPE (type))) |
23886 | return true; |
23887 | tree parm = TYPE_ARG_TYPES (type); |
23888 | if (TREE_CODE (type) == METHOD_TYPE) |
23889 | parm = TREE_CHAIN (parm); |
23890 | for (; parm; parm = TREE_CHAIN (parm)) |
23891 | if (uses_deducible_template_parms (TREE_VALUE (parm))) |
23892 | return true; |
23893 | if (flag_noexcept_type |
23894 | && TYPE_RAISES_EXCEPTIONS (type) |
23895 | && TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type)) |
23896 | && deducible_expression (TREE_PURPOSE (TYPE_RAISES_EXCEPTIONS (type)))) |
23897 | return true; |
23898 | } |
23899 | |
23900 | return false; |
23901 | } |
23902 | |
23903 | /* Subroutine of type_unification_real and unify_pack_expansion to |
23904 | handle unification of a single P/A pair. Parameters are as |
23905 | for those functions. */ |
23906 | |
23907 | static int |
23908 | unify_one_argument (tree tparms, tree targs, tree parm, tree arg, |
23909 | int subr, unification_kind_t strict, |
23910 | bool explain_p) |
23911 | { |
23912 | tree arg_expr = NULL_TREE; |
23913 | int arg_strict; |
23914 | |
23915 | if (arg == error_mark_node || parm == error_mark_node) |
23916 | return unify_invalid (explain_p); |
23917 | if (arg == unknown_type_node) |
23918 | /* We can't deduce anything from this, but we might get all the |
23919 | template args from other function args. */ |
23920 | return unify_success (explain_p); |
23921 | |
23922 | /* Implicit conversions (Clause 4) will be performed on a function |
23923 | argument to convert it to the type of the corresponding function |
23924 | parameter if the parameter type contains no template-parameters that |
23925 | participate in template argument deduction. */ |
23926 | if (strict != DEDUCE_EXACT |
23927 | && TYPE_P (parm) && !uses_deducible_template_parms (type: parm)) |
23928 | /* For function parameters with no deducible template parameters, |
23929 | just return. We'll check non-dependent conversions later. */ |
23930 | return unify_success (explain_p); |
23931 | |
23932 | switch (strict) |
23933 | { |
23934 | case DEDUCE_CALL: |
23935 | arg_strict = (UNIFY_ALLOW_OUTER_LEVEL |
23936 | | UNIFY_ALLOW_MORE_CV_QUAL |
23937 | | UNIFY_ALLOW_DERIVED); |
23938 | break; |
23939 | |
23940 | case DEDUCE_CONV: |
23941 | arg_strict = UNIFY_ALLOW_LESS_CV_QUAL; |
23942 | break; |
23943 | |
23944 | case DEDUCE_EXACT: |
23945 | arg_strict = UNIFY_ALLOW_NONE; |
23946 | break; |
23947 | |
23948 | default: |
23949 | gcc_unreachable (); |
23950 | } |
23951 | |
23952 | /* We only do these transformations if this is the top-level |
23953 | parameter_type_list in a call or declaration matching; in other |
23954 | situations (nested function declarators, template argument lists) we |
23955 | won't be comparing a type to an expression, and we don't do any type |
23956 | adjustments. */ |
23957 | if (!subr) |
23958 | { |
23959 | if (!TYPE_P (arg)) |
23960 | { |
23961 | gcc_assert (TREE_TYPE (arg) != NULL_TREE); |
23962 | if (type_unknown_p (expr: arg)) |
23963 | { |
23964 | /* [temp.deduct.type] A template-argument can be |
23965 | deduced from a pointer to function or pointer |
23966 | to member function argument if the set of |
23967 | overloaded functions does not contain function |
23968 | templates and at most one of a set of |
23969 | overloaded functions provides a unique |
23970 | match. */ |
23971 | resolve_overloaded_unification (tparms, targs, parm, |
23972 | arg, strict, |
23973 | arg_strict, explain_p); |
23974 | /* If a unique match was not found, this is a |
23975 | non-deduced context, so we still succeed. */ |
23976 | return unify_success (explain_p); |
23977 | } |
23978 | |
23979 | arg_expr = arg; |
23980 | arg = unlowered_expr_type (arg); |
23981 | if (arg == error_mark_node) |
23982 | return unify_invalid (explain_p); |
23983 | } |
23984 | |
23985 | arg_strict |= maybe_adjust_types_for_deduction (tparms, strict, |
23986 | parm: &parm, arg: &arg, arg_expr); |
23987 | } |
23988 | else |
23989 | if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL) |
23990 | != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)) |
23991 | return unify_template_argument_mismatch (explain_p, parm, arg); |
23992 | |
23993 | /* For deduction from an init-list we need the actual list. */ |
23994 | if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr)) |
23995 | arg = arg_expr; |
23996 | return unify (tparms, targs, parm, arg, arg_strict, explain_p); |
23997 | } |
23998 | |
23999 | /* for_each_template_parm callback that always returns 0. */ |
24000 | |
24001 | static int |
24002 | zero_r (tree, void *) |
24003 | { |
24004 | return 0; |
24005 | } |
24006 | |
24007 | /* for_each_template_parm any_fn callback to handle deduction of a template |
24008 | type argument from the type of an array bound. */ |
24009 | |
24010 | static int |
24011 | array_deduction_r (tree t, void *data) |
24012 | { |
24013 | tree_pair_p d = (tree_pair_p)data; |
24014 | tree &tparms = d->purpose; |
24015 | tree &targs = d->value; |
24016 | |
24017 | if (TREE_CODE (t) == ARRAY_TYPE) |
24018 | if (tree dom = TYPE_DOMAIN (t)) |
24019 | if (tree max = TYPE_MAX_VALUE (dom)) |
24020 | { |
24021 | if (TREE_CODE (max) == MINUS_EXPR) |
24022 | max = TREE_OPERAND (max, 0); |
24023 | if (TREE_CODE (max) == TEMPLATE_PARM_INDEX) |
24024 | unify (tparms, targs, TREE_TYPE (max), size_type_node, |
24025 | UNIFY_ALLOW_NONE, /*explain*/false); |
24026 | } |
24027 | |
24028 | /* Keep walking. */ |
24029 | return 0; |
24030 | } |
24031 | |
24032 | /* Try to deduce any not-yet-deduced template type arguments from the type of |
24033 | an array bound. This is handled separately from unify because 14.8.2.5 says |
24034 | "The type of a type parameter is only deduced from an array bound if it is |
24035 | not otherwise deduced." */ |
24036 | |
24037 | static void |
24038 | try_array_deduction (tree tparms, tree targs, tree parm) |
24039 | { |
24040 | tree_pair_s data = { .purpose: tparms, .value: targs }; |
24041 | hash_set<tree> visited; |
24042 | for_each_template_parm (t: parm, fn: zero_r, data: &data, visited: &visited, |
24043 | /*nondeduced*/include_nondeduced_p: false, any_fn: array_deduction_r); |
24044 | } |
24045 | |
24046 | /* Most parms like fn_type_unification. |
24047 | |
24048 | If SUBR is 1, we're being called recursively (to unify the |
24049 | arguments of a function or method parameter of a function |
24050 | template). |
24051 | |
24052 | CHECKS is a pointer to a vector of access checks encountered while |
24053 | substituting default template arguments. */ |
24054 | |
24055 | static int |
24056 | type_unification_real (tree tparms, |
24057 | tree full_targs, |
24058 | tree xparms, |
24059 | const tree *xargs, |
24060 | unsigned int xnargs, |
24061 | int subr, |
24062 | unification_kind_t strict, |
24063 | vec<deferred_access_check, va_gc> **checks, |
24064 | bool explain_p) |
24065 | { |
24066 | tree parm, arg; |
24067 | int i; |
24068 | int ntparms = TREE_VEC_LENGTH (tparms); |
24069 | int saw_undeduced = 0; |
24070 | tree parms; |
24071 | const tree *args; |
24072 | unsigned int nargs; |
24073 | unsigned int ia; |
24074 | |
24075 | gcc_assert (TREE_CODE (tparms) == TREE_VEC); |
24076 | gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST); |
24077 | gcc_assert (ntparms > 0); |
24078 | |
24079 | tree targs = INNERMOST_TEMPLATE_ARGS (full_targs); |
24080 | |
24081 | /* Reset the number of non-defaulted template arguments contained |
24082 | in TARGS. */ |
24083 | NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE; |
24084 | |
24085 | again: |
24086 | parms = xparms; |
24087 | args = xargs; |
24088 | nargs = xnargs; |
24089 | |
24090 | /* Only fn_type_unification cares about terminal void. */ |
24091 | if (nargs && args[nargs-1] == void_type_node) |
24092 | --nargs; |
24093 | |
24094 | ia = 0; |
24095 | while (parms && parms != void_list_node |
24096 | && ia < nargs) |
24097 | { |
24098 | parm = TREE_VALUE (parms); |
24099 | |
24100 | if (TREE_CODE (parm) == TYPE_PACK_EXPANSION |
24101 | && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node)) |
24102 | /* For a function parameter pack that occurs at the end of the |
24103 | parameter-declaration-list, the type A of each remaining |
24104 | argument of the call is compared with the type P of the |
24105 | declarator-id of the function parameter pack. */ |
24106 | break; |
24107 | |
24108 | parms = TREE_CHAIN (parms); |
24109 | |
24110 | if (TREE_CODE (parm) == TYPE_PACK_EXPANSION) |
24111 | /* For a function parameter pack that does not occur at the |
24112 | end of the parameter-declaration-list, the type of the |
24113 | parameter pack is a non-deduced context. */ |
24114 | continue; |
24115 | |
24116 | /* [temp.deduct.conv] only applies to the deduction of the return |
24117 | type, which is always the first argument here. Other arguments |
24118 | (notably, explicit object parameters) should undergo normal |
24119 | call-like unification. */ |
24120 | unification_kind_t kind = strict; |
24121 | if (strict == DEDUCE_CONV && ia > 0) |
24122 | kind = DEDUCE_CALL; |
24123 | |
24124 | arg = args[ia]; |
24125 | ++ia; |
24126 | |
24127 | if (unify_one_argument (tparms, targs: full_targs, parm, arg, subr, strict: kind, |
24128 | explain_p)) |
24129 | return 1; |
24130 | } |
24131 | |
24132 | if (parms |
24133 | && parms != void_list_node |
24134 | && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION) |
24135 | { |
24136 | gcc_assert (strict != DEDUCE_CONV); |
24137 | |
24138 | /* Unify the remaining arguments with the pack expansion type. */ |
24139 | tree argvec; |
24140 | tree parmvec = make_tree_vec (1); |
24141 | |
24142 | /* Allocate a TREE_VEC and copy in all of the arguments */ |
24143 | argvec = make_tree_vec (nargs - ia); |
24144 | for (i = 0; ia < nargs; ++ia, ++i) |
24145 | TREE_VEC_ELT (argvec, i) = args[ia]; |
24146 | |
24147 | /* Copy the parameter into parmvec. */ |
24148 | TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms); |
24149 | if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict, |
24150 | /*subr=*/subr, explain_p)) |
24151 | return 1; |
24152 | |
24153 | /* Advance to the end of the list of parameters. */ |
24154 | parms = TREE_CHAIN (parms); |
24155 | } |
24156 | |
24157 | /* Fail if we've reached the end of the parm list, and more args |
24158 | are present, and the parm list isn't variadic. */ |
24159 | if (ia < nargs && parms == void_list_node) |
24160 | return unify_too_many_arguments (explain_p, have: nargs, wanted: ia); |
24161 | /* Fail if parms are left and they don't have default values and |
24162 | they aren't all deduced as empty packs (c++/57397). This is |
24163 | consistent with sufficient_parms_p. */ |
24164 | if (parms && parms != void_list_node |
24165 | && TREE_PURPOSE (parms) == NULL_TREE) |
24166 | { |
24167 | unsigned int count = nargs; |
24168 | tree p = parms; |
24169 | bool type_pack_p; |
24170 | do |
24171 | { |
24172 | type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION; |
24173 | if (!type_pack_p) |
24174 | count++; |
24175 | p = TREE_CHAIN (p); |
24176 | } |
24177 | while (p && p != void_list_node); |
24178 | if (count != nargs) |
24179 | return unify_too_few_arguments (explain_p, have: ia, wanted: count, |
24180 | least_p: type_pack_p); |
24181 | } |
24182 | |
24183 | if (!subr) |
24184 | { |
24185 | tsubst_flags_t complain = (explain_p |
24186 | ? tf_warning_or_error |
24187 | : tf_none); |
24188 | bool tried_array_deduction = (cxx_dialect < cxx17); |
24189 | |
24190 | for (i = 0; i < ntparms; i++) |
24191 | { |
24192 | tree targ = TREE_VEC_ELT (targs, i); |
24193 | tree tparm = TREE_VEC_ELT (tparms, i); |
24194 | |
24195 | /* Clear the "incomplete" flags on all argument packs now so that |
24196 | substituting them into later default arguments works. */ |
24197 | if (targ && ARGUMENT_PACK_P (targ)) |
24198 | { |
24199 | ARGUMENT_PACK_INCOMPLETE_P (targ) = 0; |
24200 | ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE; |
24201 | } |
24202 | |
24203 | if (targ || tparm == error_mark_node) |
24204 | continue; |
24205 | tparm = TREE_VALUE (tparm); |
24206 | |
24207 | if (TREE_CODE (tparm) == TYPE_DECL |
24208 | && !tried_array_deduction) |
24209 | { |
24210 | try_array_deduction (tparms, targs, parm: xparms); |
24211 | tried_array_deduction = true; |
24212 | if (TREE_VEC_ELT (targs, i)) |
24213 | continue; |
24214 | } |
24215 | |
24216 | /* If this is an undeduced nontype parameter that depends on |
24217 | a type parameter, try another pass; its type may have been |
24218 | deduced from a later argument than the one from which |
24219 | this parameter can be deduced. */ |
24220 | if (TREE_CODE (tparm) == PARM_DECL |
24221 | && !is_auto (TREE_TYPE (tparm)) |
24222 | && uses_template_parms (TREE_TYPE (tparm)) |
24223 | && saw_undeduced < 2) |
24224 | { |
24225 | saw_undeduced = 1; |
24226 | continue; |
24227 | } |
24228 | |
24229 | /* Core issue #226 (C++0x) [temp.deduct]: |
24230 | |
24231 | If a template argument has not been deduced, its |
24232 | default template argument, if any, is used. |
24233 | |
24234 | When we are in C++98 mode, TREE_PURPOSE will either |
24235 | be NULL_TREE or ERROR_MARK_NODE, so we do not need |
24236 | to explicitly check cxx_dialect here. */ |
24237 | if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i))) |
24238 | /* OK, there is a default argument. Wait until after the |
24239 | conversion check to do substitution. */ |
24240 | continue; |
24241 | |
24242 | /* If the type parameter is a parameter pack, then it will |
24243 | be deduced to an empty parameter pack. */ |
24244 | if (template_parameter_pack_p (parm: tparm)) |
24245 | { |
24246 | tree arg; |
24247 | |
24248 | if (TREE_CODE (tparm) == PARM_DECL) |
24249 | { |
24250 | arg = make_node (NONTYPE_ARGUMENT_PACK); |
24251 | TREE_CONSTANT (arg) = 1; |
24252 | } |
24253 | else |
24254 | arg = cxx_make_type (TYPE_ARGUMENT_PACK); |
24255 | |
24256 | ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0); |
24257 | |
24258 | TREE_VEC_ELT (targs, i) = arg; |
24259 | continue; |
24260 | } |
24261 | |
24262 | return unify_parameter_deduction_failure (explain_p, parm: tparm); |
24263 | } |
24264 | |
24265 | /* During partial ordering, we deduce dependent template args. */ |
24266 | bool any_dependent_targs = false; |
24267 | |
24268 | /* Now substitute into the default template arguments. */ |
24269 | for (i = 0; i < ntparms; i++) |
24270 | { |
24271 | tree targ = TREE_VEC_ELT (targs, i); |
24272 | tree tparm = TREE_VEC_ELT (tparms, i); |
24273 | |
24274 | if (targ) |
24275 | { |
24276 | if (!any_dependent_targs && dependent_template_arg_p (targ)) |
24277 | any_dependent_targs = true; |
24278 | continue; |
24279 | } |
24280 | if (tparm == error_mark_node) |
24281 | continue; |
24282 | |
24283 | tree parm = TREE_VALUE (tparm); |
24284 | tree arg = TREE_PURPOSE (tparm); |
24285 | reopen_deferring_access_checks (*checks); |
24286 | location_t save_loc = input_location; |
24287 | if (DECL_P (parm)) |
24288 | input_location = DECL_SOURCE_LOCATION (parm); |
24289 | |
24290 | if (saw_undeduced == 1 |
24291 | && TREE_CODE (parm) == PARM_DECL |
24292 | && !is_auto (TREE_TYPE (parm)) |
24293 | && uses_template_parms (TREE_TYPE (parm))) |
24294 | { |
24295 | /* The type of this non-type parameter depends on undeduced |
24296 | parameters. Don't try to use its default argument yet, |
24297 | since we might deduce an argument for it on the next pass, |
24298 | but do check whether the arguments we already have cause |
24299 | substitution failure, so that that happens before we try |
24300 | later default arguments (78489). */ |
24301 | ++processing_template_decl; |
24302 | tree type = tsubst (TREE_TYPE (parm), args: full_targs, complain, |
24303 | NULL_TREE); |
24304 | --processing_template_decl; |
24305 | if (type == error_mark_node) |
24306 | arg = error_mark_node; |
24307 | else |
24308 | arg = NULL_TREE; |
24309 | } |
24310 | else |
24311 | { |
24312 | /* Even if the call is happening in template context, getting |
24313 | here means it's non-dependent, and a default argument is |
24314 | considered a separate definition under [temp.decls], so we can |
24315 | do this substitution without processing_template_decl. This |
24316 | is important if the default argument contains something that |
24317 | might be instantiation-dependent like access (87480). */ |
24318 | processing_template_decl_sentinel s (!any_dependent_targs); |
24319 | |
24320 | tree used_tparms = NULL_TREE; |
24321 | if (saw_undeduced == 1) |
24322 | { |
24323 | tree tparms_list = build_tree_list (size_int (1), tparms); |
24324 | used_tparms = find_template_parameters (t: arg, ctx_parms: tparms_list); |
24325 | for (; used_tparms; used_tparms = TREE_CHAIN (used_tparms)) |
24326 | { |
24327 | int level, index; |
24328 | template_parm_level_and_index (TREE_VALUE (used_tparms), |
24329 | &level, &index); |
24330 | if (TREE_VEC_ELT (targs, index) == NULL_TREE) |
24331 | break; |
24332 | } |
24333 | } |
24334 | |
24335 | if (!used_tparms) |
24336 | { |
24337 | /* All template parameters within this default argument are |
24338 | deduced, so we can use it. */ |
24339 | arg = tsubst_template_arg (t: arg, args: full_targs, complain, |
24340 | NULL_TREE); |
24341 | arg = convert_template_argument (parm, arg, args: full_targs, |
24342 | complain, i, NULL_TREE); |
24343 | } |
24344 | else if (saw_undeduced == 1) |
24345 | arg = NULL_TREE; |
24346 | else if (!any_dependent_targs) |
24347 | arg = error_mark_node; |
24348 | } |
24349 | |
24350 | input_location = save_loc; |
24351 | *checks = get_deferred_access_checks (); |
24352 | pop_deferring_access_checks (); |
24353 | |
24354 | if (arg == error_mark_node) |
24355 | return 1; |
24356 | else if (arg) |
24357 | { |
24358 | TREE_VEC_ELT (targs, i) = arg; |
24359 | /* The position of the first default template argument, |
24360 | is also the number of non-defaulted arguments in TARGS. |
24361 | Record that. */ |
24362 | if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) |
24363 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i); |
24364 | } |
24365 | } |
24366 | |
24367 | if (saw_undeduced++ == 1) |
24368 | goto again; |
24369 | } |
24370 | |
24371 | if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs)) |
24372 | SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs)); |
24373 | |
24374 | return unify_success (explain_p); |
24375 | } |
24376 | |
24377 | /* Subroutine of type_unification_real. Args are like the variables |
24378 | at the call site. ARG is an overloaded function (or template-id); |
24379 | we try deducing template args from each of the overloads, and if |
24380 | only one succeeds, we go with that. Modifies TARGS and returns |
24381 | true on success. */ |
24382 | |
24383 | static bool |
24384 | resolve_overloaded_unification (tree tparms, |
24385 | tree targs, |
24386 | tree parm, |
24387 | tree arg, |
24388 | unification_kind_t strict, |
24389 | int sub_strict, |
24390 | bool explain_p) |
24391 | { |
24392 | tree tempargs = copy_node (targs); |
24393 | int good = 0; |
24394 | tree goodfn = NULL_TREE; |
24395 | bool addr_p; |
24396 | |
24397 | if (TREE_CODE (arg) == ADDR_EXPR) |
24398 | { |
24399 | arg = TREE_OPERAND (arg, 0); |
24400 | addr_p = true; |
24401 | } |
24402 | else |
24403 | addr_p = false; |
24404 | |
24405 | if (TREE_CODE (arg) == COMPONENT_REF) |
24406 | /* Handle `&x' where `x' is some static or non-static member |
24407 | function name. */ |
24408 | arg = TREE_OPERAND (arg, 1); |
24409 | |
24410 | if (TREE_CODE (arg) == OFFSET_REF) |
24411 | arg = TREE_OPERAND (arg, 1); |
24412 | |
24413 | /* Strip baselink information. */ |
24414 | if (BASELINK_P (arg)) |
24415 | arg = BASELINK_FUNCTIONS (arg); |
24416 | |
24417 | if (TREE_CODE (arg) == TEMPLATE_ID_EXPR) |
24418 | { |
24419 | /* If we got some explicit template args, we need to plug them into |
24420 | the affected templates before we try to unify, in case the |
24421 | explicit args will completely resolve the templates in question. */ |
24422 | |
24423 | int ok = 0; |
24424 | tree expl_subargs = TREE_OPERAND (arg, 1); |
24425 | arg = TREE_OPERAND (arg, 0); |
24426 | |
24427 | for (lkp_iterator iter (arg); iter; ++iter) |
24428 | { |
24429 | tree fn = *iter; |
24430 | tree subargs, elem; |
24431 | |
24432 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
24433 | continue; |
24434 | |
24435 | subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
24436 | args: expl_subargs, NULL_TREE, complain: tf_none); |
24437 | if (subargs != error_mark_node |
24438 | && !any_dependent_template_arguments_p (subargs)) |
24439 | { |
24440 | fn = instantiate_template (tmpl: fn, orig_args: subargs, complain: tf_none); |
24441 | if (!constraints_satisfied_p (fn)) |
24442 | continue; |
24443 | if (undeduced_auto_decl (fn)) |
24444 | { |
24445 | /* Instantiate the function to deduce its return type. */ |
24446 | ++function_depth; |
24447 | instantiate_decl (fn, /*defer*/false, /*class*/false); |
24448 | --function_depth; |
24449 | } |
24450 | |
24451 | if (flag_noexcept_type) |
24452 | maybe_instantiate_noexcept (fn, tf_none); |
24453 | |
24454 | elem = TREE_TYPE (fn); |
24455 | if (try_one_overload (tparms, targs, tempargs, parm, |
24456 | elem, strict, sub_strict, addr_p, explain_p) |
24457 | && (!goodfn || !same_type_p (goodfn, elem))) |
24458 | { |
24459 | goodfn = elem; |
24460 | ++good; |
24461 | } |
24462 | } |
24463 | else if (subargs) |
24464 | ++ok; |
24465 | } |
24466 | /* If no templates (or more than one) are fully resolved by the |
24467 | explicit arguments, this template-id is a non-deduced context; it |
24468 | could still be OK if we deduce all template arguments for the |
24469 | enclosing call through other arguments. */ |
24470 | if (good != 1) |
24471 | good = ok; |
24472 | } |
24473 | else if (!OVL_P (arg)) |
24474 | /* If ARG is, for example, "(0, &f)" then its type will be unknown |
24475 | -- but the deduction does not succeed because the expression is |
24476 | not just the function on its own. */ |
24477 | return false; |
24478 | else |
24479 | for (lkp_iterator iter (arg); iter; ++iter) |
24480 | { |
24481 | tree fn = *iter; |
24482 | if (flag_noexcept_type) |
24483 | maybe_instantiate_noexcept (fn, tf_none); |
24484 | if (TREE_CODE (fn) == FUNCTION_DECL && !constraints_satisfied_p (fn)) |
24485 | continue; |
24486 | tree elem = TREE_TYPE (fn); |
24487 | if (try_one_overload (tparms, targs, tempargs, parm, elem, |
24488 | strict, sub_strict, addr_p, explain_p) |
24489 | && (!goodfn || !same_type_p (goodfn, elem))) |
24490 | { |
24491 | goodfn = elem; |
24492 | ++good; |
24493 | } |
24494 | } |
24495 | |
24496 | /* [temp.deduct.type] A template-argument can be deduced from a pointer |
24497 | to function or pointer to member function argument if the set of |
24498 | overloaded functions does not contain function templates and at most |
24499 | one of a set of overloaded functions provides a unique match. |
24500 | |
24501 | CWG2918 allows multiple functions to match if they all have the same type, |
24502 | so that we can choose the most constrained later. |
24503 | |
24504 | So if we found multiple possibilities, we return success but don't |
24505 | deduce anything. */ |
24506 | |
24507 | if (good == 1) |
24508 | { |
24509 | int i = TREE_VEC_LENGTH (targs); |
24510 | for (; i--; ) |
24511 | if (TREE_VEC_ELT (tempargs, i)) |
24512 | { |
24513 | tree old = TREE_VEC_ELT (targs, i); |
24514 | tree new_ = TREE_VEC_ELT (tempargs, i); |
24515 | if (new_ && old && ARGUMENT_PACK_P (old) |
24516 | && ARGUMENT_PACK_EXPLICIT_ARGS (old)) |
24517 | /* Don't forget explicit template arguments in a pack. */ |
24518 | ARGUMENT_PACK_EXPLICIT_ARGS (new_) |
24519 | = ARGUMENT_PACK_EXPLICIT_ARGS (old); |
24520 | TREE_VEC_ELT (targs, i) = new_; |
24521 | } |
24522 | } |
24523 | if (good) |
24524 | return true; |
24525 | |
24526 | return false; |
24527 | } |
24528 | |
24529 | /* Core DR 115: In contexts where deduction is done and fails, or in |
24530 | contexts where deduction is not done, if a template argument list is |
24531 | specified and it, along with any default template arguments, identifies |
24532 | a single function template specialization, then the template-id is an |
24533 | lvalue for the function template specialization. */ |
24534 | |
24535 | tree |
24536 | resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain) |
24537 | { |
24538 | tree expr, offset, baselink; |
24539 | bool addr; |
24540 | |
24541 | if (!type_unknown_p (expr: orig_expr)) |
24542 | return orig_expr; |
24543 | |
24544 | expr = orig_expr; |
24545 | addr = false; |
24546 | offset = NULL_TREE; |
24547 | baselink = NULL_TREE; |
24548 | |
24549 | if (TREE_CODE (expr) == ADDR_EXPR) |
24550 | { |
24551 | expr = TREE_OPERAND (expr, 0); |
24552 | addr = true; |
24553 | } |
24554 | if (TREE_CODE (expr) == OFFSET_REF) |
24555 | { |
24556 | offset = expr; |
24557 | expr = TREE_OPERAND (expr, 1); |
24558 | } |
24559 | if (BASELINK_P (expr)) |
24560 | { |
24561 | baselink = expr; |
24562 | expr = BASELINK_FUNCTIONS (expr); |
24563 | } |
24564 | |
24565 | if (TREE_CODE (expr) == TEMPLATE_ID_EXPR) |
24566 | { |
24567 | int good = 0; |
24568 | tree goodfn = NULL_TREE; |
24569 | |
24570 | /* If we got some explicit template args, we need to plug them into |
24571 | the affected templates before we try to unify, in case the |
24572 | explicit args will completely resolve the templates in question. */ |
24573 | |
24574 | tree expl_subargs = TREE_OPERAND (expr, 1); |
24575 | tree arg = TREE_OPERAND (expr, 0); |
24576 | tree badfn = NULL_TREE; |
24577 | tree badargs = NULL_TREE; |
24578 | |
24579 | for (lkp_iterator iter (arg); iter; ++iter) |
24580 | { |
24581 | tree fn = *iter; |
24582 | tree subargs, elem; |
24583 | |
24584 | if (TREE_CODE (fn) != TEMPLATE_DECL) |
24585 | continue; |
24586 | |
24587 | subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn), |
24588 | args: expl_subargs, NULL_TREE, complain: tf_none); |
24589 | if (subargs != error_mark_node |
24590 | && !any_dependent_template_arguments_p (subargs)) |
24591 | { |
24592 | elem = instantiate_template (tmpl: fn, orig_args: subargs, complain: tf_none); |
24593 | if (elem == error_mark_node) |
24594 | { |
24595 | badfn = fn; |
24596 | badargs = subargs; |
24597 | } |
24598 | else if (elem && (!goodfn || !decls_match (goodfn, elem)) |
24599 | && constraints_satisfied_p (elem)) |
24600 | { |
24601 | goodfn = elem; |
24602 | ++good; |
24603 | } |
24604 | } |
24605 | } |
24606 | if (good == 1) |
24607 | { |
24608 | mark_used (goodfn); |
24609 | expr = goodfn; |
24610 | if (baselink) |
24611 | expr = build_baselink (BASELINK_BINFO (baselink), |
24612 | BASELINK_ACCESS_BINFO (baselink), |
24613 | expr, BASELINK_OPTYPE (baselink)); |
24614 | if (offset) |
24615 | { |
24616 | tree base |
24617 | = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0))); |
24618 | expr = build_offset_ref (base, expr, addr, complain); |
24619 | } |
24620 | if (addr) |
24621 | expr = cp_build_addr_expr (expr, complain); |
24622 | return expr; |
24623 | } |
24624 | else if (good == 0 && badargs && (complain & tf_error)) |
24625 | /* There were no good options and at least one bad one, so let the |
24626 | user know what the problem is. */ |
24627 | instantiate_template (tmpl: badfn, orig_args: badargs, complain); |
24628 | } |
24629 | return orig_expr; |
24630 | } |
24631 | |
24632 | /* As above, but error out if the expression remains overloaded. */ |
24633 | |
24634 | tree |
24635 | resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain) |
24636 | { |
24637 | exp = resolve_nondeduced_context (orig_expr: exp, complain); |
24638 | if (type_unknown_p (expr: exp)) |
24639 | { |
24640 | if (complain & tf_error) |
24641 | cxx_incomplete_type_error (value: exp, TREE_TYPE (exp)); |
24642 | return error_mark_node; |
24643 | } |
24644 | return exp; |
24645 | } |
24646 | |
24647 | /* Subroutine of resolve_overloaded_unification; does deduction for a single |
24648 | overload. Fills TARGS with any deduced arguments, or error_mark_node if |
24649 | different overloads deduce different arguments for a given parm. |
24650 | ADDR_P is true if the expression for which deduction is being |
24651 | performed was of the form "& fn" rather than simply "fn". |
24652 | |
24653 | Returns 1 on success. */ |
24654 | |
24655 | static int |
24656 | try_one_overload (tree tparms, |
24657 | tree orig_targs, |
24658 | tree targs, |
24659 | tree parm, |
24660 | tree arg, |
24661 | unification_kind_t strict, |
24662 | int sub_strict, |
24663 | bool addr_p, |
24664 | bool explain_p) |
24665 | { |
24666 | int nargs; |
24667 | tree tempargs; |
24668 | int i; |
24669 | |
24670 | if (arg == error_mark_node) |
24671 | return 0; |
24672 | |
24673 | /* [temp.deduct.type] A template-argument can be deduced from a pointer |
24674 | to function or pointer to member function argument if the set of |
24675 | overloaded functions does not contain function templates and at most |
24676 | one of a set of overloaded functions provides a unique match. |
24677 | |
24678 | So if this is a template, just return success. */ |
24679 | |
24680 | if (uses_template_parms (t: arg)) |
24681 | return 1; |
24682 | |
24683 | if (TREE_CODE (arg) == METHOD_TYPE) |
24684 | arg = build_ptrmemfunc_type (build_pointer_type (arg)); |
24685 | else if (addr_p) |
24686 | arg = build_pointer_type (arg); |
24687 | |
24688 | sub_strict |= maybe_adjust_types_for_deduction (tparms, strict, |
24689 | parm: &parm, arg: &arg, NULL_TREE); |
24690 | |
24691 | /* We don't copy orig_targs for this because if we have already deduced |
24692 | some template args from previous args, unify would complain when we |
24693 | try to deduce a template parameter for the same argument, even though |
24694 | there isn't really a conflict. */ |
24695 | nargs = TREE_VEC_LENGTH (targs); |
24696 | tempargs = make_tree_vec (nargs); |
24697 | |
24698 | if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p)) |
24699 | return 0; |
24700 | |
24701 | /* First make sure we didn't deduce anything that conflicts with |
24702 | explicitly specified args. */ |
24703 | for (i = nargs; i--; ) |
24704 | { |
24705 | tree elt = TREE_VEC_ELT (tempargs, i); |
24706 | tree oldelt = TREE_VEC_ELT (orig_targs, i); |
24707 | |
24708 | if (!elt) |
24709 | /*NOP*/; |
24710 | else if (uses_template_parms (t: elt)) |
24711 | /* Since we're unifying against ourselves, we will fill in |
24712 | template args used in the function parm list with our own |
24713 | template parms. Discard them. */ |
24714 | TREE_VEC_ELT (tempargs, i) = NULL_TREE; |
24715 | else if (oldelt && ARGUMENT_PACK_P (oldelt)) |
24716 | { |
24717 | /* Check that the argument at each index of the deduced argument pack |
24718 | is equivalent to the corresponding explicitly specified argument. |
24719 | We may have deduced more arguments than were explicitly specified, |
24720 | and that's OK. */ |
24721 | |
24722 | /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but |
24723 | that's wrong if we deduce the same argument pack from multiple |
24724 | function arguments: it's only incomplete the first time. */ |
24725 | |
24726 | tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt); |
24727 | tree deduced_pack = ARGUMENT_PACK_ARGS (elt); |
24728 | |
24729 | if (TREE_VEC_LENGTH (deduced_pack) |
24730 | < TREE_VEC_LENGTH (explicit_pack)) |
24731 | return 0; |
24732 | |
24733 | for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++) |
24734 | if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j), |
24735 | TREE_VEC_ELT (deduced_pack, j))) |
24736 | return 0; |
24737 | } |
24738 | else if (oldelt && !template_args_equal (ot: oldelt, nt: elt)) |
24739 | return 0; |
24740 | } |
24741 | |
24742 | for (i = nargs; i--; ) |
24743 | { |
24744 | tree elt = TREE_VEC_ELT (tempargs, i); |
24745 | |
24746 | if (elt) |
24747 | TREE_VEC_ELT (targs, i) = elt; |
24748 | } |
24749 | |
24750 | return 1; |
24751 | } |
24752 | |
24753 | /* PARM is a template class (perhaps with unbound template |
24754 | parameters). ARG is a fully instantiated type. If ARG can be |
24755 | bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and |
24756 | TARGS are as for unify. */ |
24757 | |
24758 | static tree |
24759 | try_class_unification (tree tparms, tree targs, tree parm, tree arg, |
24760 | bool explain_p) |
24761 | { |
24762 | if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
24763 | return NULL_TREE; |
24764 | else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
24765 | /* Matches anything. */; |
24766 | else if (CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm)) |
24767 | return NULL_TREE; |
24768 | |
24769 | /* We need to make a new template argument vector for the call to |
24770 | unify. If we used TARGS, we'd clutter it up with the result of |
24771 | the attempted unification, even if this class didn't work out. |
24772 | We also don't want to commit ourselves to all the unifications |
24773 | we've already done, since unification is supposed to be done on |
24774 | an argument-by-argument basis. In other words, consider the |
24775 | following pathological case: |
24776 | |
24777 | template <int I, int J, int K> |
24778 | struct S {}; |
24779 | |
24780 | template <int I, int J> |
24781 | struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {}; |
24782 | |
24783 | template <int I, int J, int K> |
24784 | void f(S<I, J, K>, S<I, I, I>); |
24785 | |
24786 | void g() { |
24787 | S<0, 0, 0> s0; |
24788 | S<0, 1, 2> s2; |
24789 | |
24790 | f(s0, s2); |
24791 | } |
24792 | |
24793 | Now, by the time we consider the unification involving `s2', we |
24794 | already know that we must have `f<0, 0, 0>'. But, even though |
24795 | `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid |
24796 | because there are two ways to unify base classes of S<0, 1, 2> |
24797 | with S<I, I, I>. If we kept the already deduced knowledge, we |
24798 | would reject the possibility I=1. */ |
24799 | targs = copy_template_args (t: targs); |
24800 | for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) |
24801 | targ = NULL_TREE; |
24802 | |
24803 | int err; |
24804 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
24805 | err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p); |
24806 | else |
24807 | err = unify (tparms, targs, |
24808 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)), |
24809 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (arg)), |
24810 | UNIFY_ALLOW_NONE, explain_p); |
24811 | |
24812 | return err ? NULL_TREE : arg; |
24813 | } |
24814 | |
24815 | /* Given a template type PARM and a class type ARG, find the unique |
24816 | base type in ARG that is an instance of PARM. We do not examine |
24817 | ARG itself; only its base-classes. If there is not exactly one |
24818 | appropriate base class, return NULL_TREE. PARM may be the type of |
24819 | a partial specialization, as well as a plain template type. Used |
24820 | by unify. */ |
24821 | |
24822 | static enum template_base_result |
24823 | get_template_base (tree tparms, tree targs, tree parm, tree arg, |
24824 | bool explain_p, tree *result) |
24825 | { |
24826 | tree rval = NULL_TREE; |
24827 | tree binfo; |
24828 | |
24829 | gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg))); |
24830 | |
24831 | binfo = TYPE_BINFO (complete_type (arg)); |
24832 | if (!binfo) |
24833 | { |
24834 | /* The type could not be completed. */ |
24835 | *result = NULL_TREE; |
24836 | return tbr_incomplete_type; |
24837 | } |
24838 | |
24839 | /* Walk in inheritance graph order. The search order is not |
24840 | important, and this avoids multiple walks of virtual bases. */ |
24841 | for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo)) |
24842 | { |
24843 | tree r = try_class_unification (tparms, targs, parm, |
24844 | BINFO_TYPE (binfo), explain_p); |
24845 | |
24846 | if (r) |
24847 | { |
24848 | /* If there is more than one satisfactory baseclass, then: |
24849 | |
24850 | [temp.deduct.call] |
24851 | |
24852 | If they yield more than one possible deduced A, the type |
24853 | deduction fails. |
24854 | |
24855 | applies. */ |
24856 | if (rval && !same_type_p (r, rval)) |
24857 | { |
24858 | /* [temp.deduct.call]/4.3: If there is a class C that is a |
24859 | (direct or indirect) base class of D and derived (directly or |
24860 | indirectly) from a class B and that would be a valid deduced |
24861 | A, the deduced A cannot be B or pointer to B, respectively. */ |
24862 | if (DERIVED_FROM_P (r, rval)) |
24863 | /* Ignore r. */ |
24864 | continue; |
24865 | else if (DERIVED_FROM_P (rval, r)) |
24866 | /* Ignore rval. */; |
24867 | else |
24868 | { |
24869 | *result = NULL_TREE; |
24870 | return tbr_ambiguous_baseclass; |
24871 | } |
24872 | } |
24873 | |
24874 | rval = r; |
24875 | } |
24876 | } |
24877 | |
24878 | *result = rval; |
24879 | return tbr_success; |
24880 | } |
24881 | |
24882 | /* Returns the level of DECL, which declares a template parameter. */ |
24883 | |
24884 | static int |
24885 | template_decl_level (tree decl) |
24886 | { |
24887 | switch (TREE_CODE (decl)) |
24888 | { |
24889 | case TYPE_DECL: |
24890 | case TEMPLATE_DECL: |
24891 | return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl)); |
24892 | |
24893 | case PARM_DECL: |
24894 | return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl)); |
24895 | |
24896 | default: |
24897 | gcc_unreachable (); |
24898 | } |
24899 | return 0; |
24900 | } |
24901 | |
24902 | /* Decide whether ARG can be unified with PARM, considering only the |
24903 | cv-qualifiers of each type, given STRICT as documented for unify. |
24904 | Returns nonzero iff the unification is OK on that basis. */ |
24905 | |
24906 | static int |
24907 | check_cv_quals_for_unify (int strict, tree arg, tree parm) |
24908 | { |
24909 | int arg_quals = cp_type_quals (arg); |
24910 | int parm_quals = cp_type_quals (parm); |
24911 | |
24912 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
24913 | && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
24914 | { |
24915 | /* Although a CVR qualifier is ignored when being applied to a |
24916 | substituted template parameter ([8.3.2]/1 for example), that |
24917 | does not allow us to unify "const T" with "int&" because both |
24918 | types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type]. |
24919 | It is ok when we're allowing additional CV qualifiers |
24920 | at the outer level [14.8.2.1]/3,1st bullet. */ |
24921 | if ((TYPE_REF_P (arg) |
24922 | || FUNC_OR_METHOD_TYPE_P (arg)) |
24923 | && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE))) |
24924 | return 0; |
24925 | |
24926 | if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM) |
24927 | && (parm_quals & TYPE_QUAL_RESTRICT)) |
24928 | return 0; |
24929 | } |
24930 | |
24931 | if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL)) |
24932 | && (arg_quals & parm_quals) != parm_quals) |
24933 | return 0; |
24934 | |
24935 | if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL)) |
24936 | && (parm_quals & arg_quals) != arg_quals) |
24937 | return 0; |
24938 | |
24939 | return 1; |
24940 | } |
24941 | |
24942 | /* Determines the LEVEL and INDEX for the template parameter PARM. */ |
24943 | void |
24944 | template_parm_level_and_index (tree parm, int* level, int* index) |
24945 | { |
24946 | if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
24947 | || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
24948 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
24949 | { |
24950 | *index = TEMPLATE_TYPE_IDX (parm); |
24951 | *level = TEMPLATE_TYPE_LEVEL (parm); |
24952 | } |
24953 | else |
24954 | { |
24955 | *index = TEMPLATE_PARM_IDX (parm); |
24956 | *level = TEMPLATE_PARM_LEVEL (parm); |
24957 | } |
24958 | } |
24959 | |
24960 | #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \ |
24961 | do { \ |
24962 | if (unify (TP, TA, P, A, S, EP)) \ |
24963 | return 1; \ |
24964 | } while (0) |
24965 | |
24966 | /* Unifies the remaining arguments in PACKED_ARGS with the pack |
24967 | expansion at the end of PACKED_PARMS. Returns 0 if the type |
24968 | deduction succeeds, 1 otherwise. STRICT is the same as in |
24969 | fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a |
24970 | function call argument list. We'll need to adjust the arguments to make them |
24971 | types. SUBR tells us if this is from a recursive call to |
24972 | type_unification_real, or for comparing two template argument |
24973 | lists. */ |
24974 | |
24975 | static int |
24976 | unify_pack_expansion (tree tparms, tree targs, tree packed_parms, |
24977 | tree packed_args, unification_kind_t strict, |
24978 | bool subr, bool explain_p) |
24979 | { |
24980 | tree parm |
24981 | = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1); |
24982 | tree pattern = PACK_EXPANSION_PATTERN (parm); |
24983 | tree pack, packs = NULL_TREE; |
24984 | int i, start = TREE_VEC_LENGTH (packed_parms) - 1; |
24985 | |
24986 | /* Add in any args remembered from an earlier partial instantiation. */ |
24987 | targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), extra_args: targs); |
24988 | int levels = TMPL_ARGS_DEPTH (targs); |
24989 | |
24990 | packed_args = expand_template_argument_pack (args: packed_args); |
24991 | |
24992 | int len = TREE_VEC_LENGTH (packed_args); |
24993 | |
24994 | /* Determine the parameter packs we will be deducing from the |
24995 | pattern, and record their current deductions. */ |
24996 | for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm); |
24997 | pack; pack = TREE_CHAIN (pack)) |
24998 | { |
24999 | tree parm_pack = TREE_VALUE (pack); |
25000 | int idx, level; |
25001 | |
25002 | /* Only template parameter packs can be deduced, not e.g. function |
25003 | parameter packs or __bases or __integer_pack. */ |
25004 | if (!TEMPLATE_PARM_P (parm_pack)) |
25005 | continue; |
25006 | |
25007 | /* Determine the index and level of this parameter pack. */ |
25008 | template_parm_level_and_index (parm: parm_pack, level: &level, index: &idx); |
25009 | if (level > levels) |
25010 | continue; |
25011 | |
25012 | /* Keep track of the parameter packs and their corresponding |
25013 | argument packs. */ |
25014 | packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs); |
25015 | TREE_TYPE (packs) = make_tree_vec (len - start); |
25016 | } |
25017 | |
25018 | /* Loop through all of the arguments that have not yet been |
25019 | unified and unify each with the pattern. */ |
25020 | for (i = start; i < len; i++) |
25021 | { |
25022 | tree parm; |
25023 | bool any_explicit = false; |
25024 | tree arg = TREE_VEC_ELT (packed_args, i); |
25025 | |
25026 | /* For each parameter pack, set its TMPL_ARG to either NULL_TREE |
25027 | or the element of its argument pack at the current index if |
25028 | this argument was explicitly specified. */ |
25029 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
25030 | { |
25031 | int idx, level; |
25032 | tree arg, pargs; |
25033 | template_parm_level_and_index (TREE_PURPOSE (pack), level: &level, index: &idx); |
25034 | |
25035 | arg = NULL_TREE; |
25036 | if (TREE_VALUE (pack) |
25037 | && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack))) |
25038 | && (i - start < TREE_VEC_LENGTH (pargs))) |
25039 | { |
25040 | any_explicit = true; |
25041 | arg = TREE_VEC_ELT (pargs, i - start); |
25042 | } |
25043 | TMPL_ARG (targs, level, idx) = arg; |
25044 | } |
25045 | |
25046 | /* If we had explicit template arguments, substitute them into the |
25047 | pattern before deduction. */ |
25048 | if (any_explicit) |
25049 | { |
25050 | /* Some arguments might still be unspecified or dependent. */ |
25051 | bool dependent; |
25052 | ++processing_template_decl; |
25053 | dependent = any_dependent_template_arguments_p (targs); |
25054 | if (!dependent) |
25055 | --processing_template_decl; |
25056 | parm = tsubst (t: pattern, args: targs, |
25057 | complain: explain_p ? tf_warning_or_error : tf_none, |
25058 | NULL_TREE); |
25059 | if (dependent) |
25060 | --processing_template_decl; |
25061 | if (parm == error_mark_node) |
25062 | return 1; |
25063 | } |
25064 | else |
25065 | parm = pattern; |
25066 | |
25067 | /* Unify the pattern with the current argument. */ |
25068 | if (unify_one_argument (tparms, targs, parm, arg, subr, strict, |
25069 | explain_p)) |
25070 | return 1; |
25071 | |
25072 | /* For each parameter pack, collect the deduced value. */ |
25073 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
25074 | { |
25075 | int idx, level; |
25076 | template_parm_level_and_index (TREE_PURPOSE (pack), level: &level, index: &idx); |
25077 | |
25078 | TREE_VEC_ELT (TREE_TYPE (pack), i - start) = |
25079 | TMPL_ARG (targs, level, idx); |
25080 | } |
25081 | } |
25082 | |
25083 | /* Verify that the results of unification with the parameter packs |
25084 | produce results consistent with what we've seen before, and make |
25085 | the deduced argument packs available. */ |
25086 | for (pack = packs; pack; pack = TREE_CHAIN (pack)) |
25087 | { |
25088 | tree old_pack = TREE_VALUE (pack); |
25089 | tree new_args = TREE_TYPE (pack); |
25090 | int i, len = TREE_VEC_LENGTH (new_args); |
25091 | int idx, level; |
25092 | bool nondeduced_p = false; |
25093 | |
25094 | /* By default keep the original deduced argument pack. |
25095 | If necessary, more specific code is going to update the |
25096 | resulting deduced argument later down in this function. */ |
25097 | template_parm_level_and_index (TREE_PURPOSE (pack), level: &level, index: &idx); |
25098 | TMPL_ARG (targs, level, idx) = old_pack; |
25099 | |
25100 | /* If NEW_ARGS contains any NULL_TREE entries, we didn't |
25101 | actually deduce anything. */ |
25102 | for (i = 0; i < len && !nondeduced_p; ++i) |
25103 | if (TREE_VEC_ELT (new_args, i) == NULL_TREE) |
25104 | nondeduced_p = true; |
25105 | if (nondeduced_p) |
25106 | continue; |
25107 | |
25108 | if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack)) |
25109 | { |
25110 | /* If we had fewer function args than explicit template args, |
25111 | just use the explicits. */ |
25112 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); |
25113 | int explicit_len = TREE_VEC_LENGTH (explicit_args); |
25114 | if (len < explicit_len) |
25115 | new_args = explicit_args; |
25116 | } |
25117 | |
25118 | if (!old_pack) |
25119 | { |
25120 | tree result; |
25121 | /* Build the deduced *_ARGUMENT_PACK. */ |
25122 | if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX) |
25123 | { |
25124 | result = make_node (NONTYPE_ARGUMENT_PACK); |
25125 | TREE_CONSTANT (result) = 1; |
25126 | } |
25127 | else |
25128 | result = cxx_make_type (TYPE_ARGUMENT_PACK); |
25129 | |
25130 | ARGUMENT_PACK_ARGS (result) = new_args; |
25131 | |
25132 | /* Note the deduced argument packs for this parameter |
25133 | pack. */ |
25134 | TMPL_ARG (targs, level, idx) = result; |
25135 | } |
25136 | else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack) |
25137 | && (ARGUMENT_PACK_ARGS (old_pack) |
25138 | == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack))) |
25139 | { |
25140 | /* We only had the explicitly-provided arguments before, but |
25141 | now we have a complete set of arguments. */ |
25142 | tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack); |
25143 | |
25144 | ARGUMENT_PACK_ARGS (old_pack) = new_args; |
25145 | ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1; |
25146 | ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args; |
25147 | } |
25148 | else |
25149 | { |
25150 | tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE; |
25151 | tree old_args = ARGUMENT_PACK_ARGS (old_pack); |
25152 | temp_override<int> ovl (TREE_VEC_LENGTH (old_args)); |
25153 | /* During template argument deduction for the aggregate deduction |
25154 | candidate, the number of elements in a trailing parameter pack |
25155 | is only deduced from the number of remaining function |
25156 | arguments if it is not otherwise deduced. */ |
25157 | if (cxx_dialect >= cxx20 |
25158 | && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args) |
25159 | /* FIXME This isn't set properly for partial instantiations. */ |
25160 | && TPARMS_PRIMARY_TEMPLATE (tparms) |
25161 | && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms))) |
25162 | TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args); |
25163 | if (!comp_template_args (oldargs: old_args, newargs: new_args, |
25164 | oldarg_ptr: &bad_old_arg, newarg_ptr: &bad_new_arg)) |
25165 | /* Inconsistent unification of this parameter pack. */ |
25166 | return unify_parameter_pack_inconsistent (explain_p, |
25167 | old_arg: bad_old_arg, |
25168 | new_arg: bad_new_arg); |
25169 | } |
25170 | } |
25171 | |
25172 | return unify_success (explain_p); |
25173 | } |
25174 | |
25175 | /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are |
25176 | INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other |
25177 | parameters and return value are as for unify. */ |
25178 | |
25179 | static int |
25180 | unify_array_domain (tree tparms, tree targs, |
25181 | tree parm_dom, tree arg_dom, |
25182 | bool explain_p) |
25183 | { |
25184 | tree parm_max; |
25185 | tree arg_max; |
25186 | bool parm_cst; |
25187 | bool arg_cst; |
25188 | |
25189 | /* Our representation of array types uses "N - 1" as the |
25190 | TYPE_MAX_VALUE for an array with "N" elements, if "N" is |
25191 | not an integer constant. We cannot unify arbitrarily |
25192 | complex expressions, so we eliminate the MINUS_EXPRs |
25193 | here. */ |
25194 | parm_max = TYPE_MAX_VALUE (parm_dom); |
25195 | parm_cst = TREE_CODE (parm_max) == INTEGER_CST; |
25196 | if (!parm_cst) |
25197 | { |
25198 | gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR); |
25199 | parm_max = TREE_OPERAND (parm_max, 0); |
25200 | } |
25201 | arg_max = TYPE_MAX_VALUE (arg_dom); |
25202 | arg_cst = TREE_CODE (arg_max) == INTEGER_CST; |
25203 | if (!arg_cst) |
25204 | { |
25205 | /* The ARG_MAX may not be a simple MINUS_EXPR, if we are |
25206 | trying to unify the type of a variable with the type |
25207 | of a template parameter. For example: |
25208 | |
25209 | template <unsigned int N> |
25210 | void f (char (&) [N]); |
25211 | int g(); |
25212 | void h(int i) { |
25213 | char a[g(i)]; |
25214 | f(a); |
25215 | } |
25216 | |
25217 | Here, the type of the ARG will be "int [g(i)]", and |
25218 | may be a SAVE_EXPR, etc. */ |
25219 | if (TREE_CODE (arg_max) != MINUS_EXPR) |
25220 | return unify_vla_arg (explain_p, arg: arg_dom); |
25221 | arg_max = TREE_OPERAND (arg_max, 0); |
25222 | } |
25223 | |
25224 | /* If only one of the bounds used a MINUS_EXPR, compensate |
25225 | by adding one to the other bound. */ |
25226 | if (parm_cst && !arg_cst) |
25227 | parm_max = fold_build2_loc (input_location, PLUS_EXPR, |
25228 | integer_type_node, |
25229 | parm_max, |
25230 | integer_one_node); |
25231 | else if (arg_cst && !parm_cst) |
25232 | arg_max = fold_build2_loc (input_location, PLUS_EXPR, |
25233 | integer_type_node, |
25234 | arg_max, |
25235 | integer_one_node); |
25236 | |
25237 | return unify (tparms, targs, parm_max, arg_max, |
25238 | UNIFY_ALLOW_INTEGER, explain_p); |
25239 | } |
25240 | |
25241 | /* Returns whether T, a P or A in unify, is a type, template or expression. */ |
25242 | |
25243 | enum pa_kind_t { pa_type, pa_tmpl, pa_expr }; |
25244 | |
25245 | static pa_kind_t |
25246 | pa_kind (tree t) |
25247 | { |
25248 | if (PACK_EXPANSION_P (t)) |
25249 | t = PACK_EXPANSION_PATTERN (t); |
25250 | if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM |
25251 | || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE |
25252 | || DECL_TYPE_TEMPLATE_P (t)) |
25253 | return pa_tmpl; |
25254 | else if (TYPE_P (t)) |
25255 | return pa_type; |
25256 | else |
25257 | return pa_expr; |
25258 | } |
25259 | |
25260 | /* Deduce the value of template parameters. TPARMS is the (innermost) |
25261 | set of template parameters to a template. TARGS is the bindings |
25262 | for those template parameters, as determined thus far; TARGS may |
25263 | include template arguments for outer levels of template parameters |
25264 | as well. PARM is a parameter to a template function, or a |
25265 | subcomponent of that parameter; ARG is the corresponding argument. |
25266 | This function attempts to match PARM with ARG in a manner |
25267 | consistent with the existing assignments in TARGS. If more values |
25268 | are deduced, then TARGS is updated. |
25269 | |
25270 | Returns 0 if the type deduction succeeds, 1 otherwise. The |
25271 | parameter STRICT is a bitwise or of the following flags: |
25272 | |
25273 | UNIFY_ALLOW_NONE: |
25274 | Require an exact match between PARM and ARG. |
25275 | UNIFY_ALLOW_MORE_CV_QUAL: |
25276 | Allow the deduced ARG to be more cv-qualified (by qualification |
25277 | conversion) than ARG. |
25278 | UNIFY_ALLOW_LESS_CV_QUAL: |
25279 | Allow the deduced ARG to be less cv-qualified than ARG. |
25280 | UNIFY_ALLOW_DERIVED: |
25281 | Allow the deduced ARG to be a template base class of ARG, |
25282 | or a pointer to a template base class of the type pointed to by |
25283 | ARG. |
25284 | UNIFY_ALLOW_INTEGER: |
25285 | Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX |
25286 | case for more information. |
25287 | UNIFY_ALLOW_OUTER_LEVEL: |
25288 | This is the outermost level of a deduction. Used to determine validity |
25289 | of qualification conversions. A valid qualification conversion must |
25290 | have const qualified pointers leading up to the inner type which |
25291 | requires additional CV quals, except at the outer level, where const |
25292 | is not required [conv.qual]. It would be normal to set this flag in |
25293 | addition to setting UNIFY_ALLOW_MORE_CV_QUAL. |
25294 | UNIFY_ALLOW_OUTER_MORE_CV_QUAL: |
25295 | This is the outermost level of a deduction, and PARM can be more CV |
25296 | qualified at this point. |
25297 | UNIFY_ALLOW_OUTER_LESS_CV_QUAL: |
25298 | This is the outermost level of a deduction, and PARM can be less CV |
25299 | qualified at this point. */ |
25300 | |
25301 | static int |
25302 | unify (tree tparms, tree targs, tree parm, tree arg, int strict, |
25303 | bool explain_p) |
25304 | { |
25305 | int idx; |
25306 | tree targ; |
25307 | tree tparm; |
25308 | int strict_in = strict; |
25309 | tsubst_flags_t complain = (explain_p |
25310 | ? tf_warning_or_error |
25311 | : tf_none); |
25312 | |
25313 | if (arg == error_mark_node) |
25314 | return unify_invalid (explain_p); |
25315 | if (arg == unknown_type_node |
25316 | || arg == init_list_type_node) |
25317 | /* We can't deduce anything from this, but we might get all the |
25318 | template args from other function args. */ |
25319 | return unify_success (explain_p); |
25320 | |
25321 | if (parm == any_targ_node || arg == any_targ_node) |
25322 | return unify_success (explain_p); |
25323 | |
25324 | /* Strip conversions that will interfere with NTTP deduction. |
25325 | I don't think this will do the right thing with respect to types. |
25326 | But the only case I've seen it in so far has been array bounds, where |
25327 | signedness is the only information lost, and I think that will be |
25328 | okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to |
25329 | finish_id_expression_1, and are also OK. */ |
25330 | if (deducible_expression (expr: parm)) |
25331 | while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR |
25332 | || TREE_CODE (parm) == IMPLICIT_CONV_EXPR) |
25333 | parm = TREE_OPERAND (parm, 0); |
25334 | |
25335 | /* If PARM uses template parameters, then we can't bail out here, |
25336 | even if ARG == PARM, since we won't record unifications for the |
25337 | template parameters. We might need them if we're trying to |
25338 | figure out which of two things is more specialized. */ |
25339 | if (arg == parm |
25340 | && (DECL_P (parm) || !uses_template_parms (t: parm))) |
25341 | return unify_success (explain_p); |
25342 | |
25343 | /* Handle init lists early, so the rest of the function can assume |
25344 | we're dealing with a type. */ |
25345 | if (BRACE_ENCLOSED_INITIALIZER_P (arg)) |
25346 | { |
25347 | tree elttype; |
25348 | tree orig_parm = parm; |
25349 | |
25350 | if (!is_std_init_list (parm) |
25351 | && TREE_CODE (parm) != ARRAY_TYPE) |
25352 | /* We can only deduce from an initializer list argument if the |
25353 | parameter is std::initializer_list or an array; otherwise this |
25354 | is a non-deduced context. */ |
25355 | return unify_success (explain_p); |
25356 | |
25357 | if (TREE_CODE (parm) == ARRAY_TYPE) |
25358 | elttype = TREE_TYPE (parm); |
25359 | else |
25360 | { |
25361 | elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); |
25362 | /* Deduction is defined in terms of a single type, so just punt |
25363 | on the (bizarre) std::initializer_list<T...>. */ |
25364 | if (PACK_EXPANSION_P (elttype)) |
25365 | return unify_success (explain_p); |
25366 | } |
25367 | |
25368 | if (strict != DEDUCE_EXACT |
25369 | && TYPE_P (elttype) |
25370 | && !uses_deducible_template_parms (type: elttype)) |
25371 | /* If ELTTYPE has no deducible template parms, skip deduction from |
25372 | the list elements. */; |
25373 | else |
25374 | for (auto &e: CONSTRUCTOR_ELTS (arg)) |
25375 | { |
25376 | tree elt = e.value; |
25377 | int elt_strict = strict; |
25378 | |
25379 | if (elt == error_mark_node) |
25380 | return unify_invalid (explain_p); |
25381 | |
25382 | if (!BRACE_ENCLOSED_INITIALIZER_P (elt)) |
25383 | { |
25384 | tree type = TREE_TYPE (elt); |
25385 | if (type == error_mark_node) |
25386 | return unify_invalid (explain_p); |
25387 | /* It should only be possible to get here for a call. */ |
25388 | gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL); |
25389 | elt_strict |= maybe_adjust_types_for_deduction |
25390 | (tparms, strict: DEDUCE_CALL, parm: &elttype, arg: &type, arg_expr: elt); |
25391 | elt = type; |
25392 | } |
25393 | |
25394 | RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict, |
25395 | explain_p); |
25396 | } |
25397 | |
25398 | if (TREE_CODE (parm) == ARRAY_TYPE |
25399 | && deducible_array_bound (TYPE_DOMAIN (parm))) |
25400 | { |
25401 | /* Also deduce from the length of the initializer list. */ |
25402 | tree max = size_int (count_ctor_elements (arg)); |
25403 | tree idx = compute_array_index_type (NULL_TREE, max, tf_none); |
25404 | if (idx == error_mark_node) |
25405 | return unify_invalid (explain_p); |
25406 | return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm), |
25407 | arg_dom: idx, explain_p); |
25408 | } |
25409 | |
25410 | /* If the std::initializer_list<T> deduction worked, replace the |
25411 | deduced A with std::initializer_list<A>. */ |
25412 | if (orig_parm != parm) |
25413 | { |
25414 | idx = TEMPLATE_TYPE_IDX (orig_parm); |
25415 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
25416 | targ = listify (targ); |
25417 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ; |
25418 | } |
25419 | return unify_success (explain_p); |
25420 | } |
25421 | |
25422 | /* If parm and arg aren't the same kind of thing (template, type, or |
25423 | expression), fail early. */ |
25424 | if (pa_kind (t: parm) != pa_kind (t: arg)) |
25425 | return unify_invalid (explain_p); |
25426 | |
25427 | /* Immediately reject some pairs that won't unify because of |
25428 | cv-qualification mismatches. */ |
25429 | if (TREE_CODE (arg) == TREE_CODE (parm) |
25430 | && TYPE_P (arg) |
25431 | /* It is the elements of the array which hold the cv quals of an array |
25432 | type, and the elements might be template type parms. We'll check |
25433 | when we recurse. */ |
25434 | && TREE_CODE (arg) != ARRAY_TYPE |
25435 | /* We check the cv-qualifiers when unifying with template type |
25436 | parameters below. We want to allow ARG `const T' to unify with |
25437 | PARM `T' for example, when computing which of two templates |
25438 | is more specialized, for example. */ |
25439 | && TREE_CODE (arg) != TEMPLATE_TYPE_PARM |
25440 | && !check_cv_quals_for_unify (strict: strict_in, arg, parm)) |
25441 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
25442 | |
25443 | if (!(strict & UNIFY_ALLOW_OUTER_LEVEL) |
25444 | && TYPE_P (parm) && !CP_TYPE_CONST_P (parm) |
25445 | && !FUNC_OR_METHOD_TYPE_P (parm)) |
25446 | strict &= ~UNIFY_ALLOW_MORE_CV_QUAL; |
25447 | /* PMFs recurse at the same level, so don't strip this yet. */ |
25448 | if (!TYPE_PTRMEMFUNC_P (parm)) |
25449 | strict &= ~UNIFY_ALLOW_OUTER_LEVEL; |
25450 | strict &= ~UNIFY_ALLOW_DERIVED; |
25451 | strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL; |
25452 | strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL; |
25453 | |
25454 | switch (TREE_CODE (parm)) |
25455 | { |
25456 | case TYPENAME_TYPE: |
25457 | case SCOPE_REF: |
25458 | case UNBOUND_CLASS_TEMPLATE: |
25459 | /* In a type which contains a nested-name-specifier, template |
25460 | argument values cannot be deduced for template parameters used |
25461 | within the nested-name-specifier. */ |
25462 | return unify_success (explain_p); |
25463 | |
25464 | case TEMPLATE_TYPE_PARM: |
25465 | case TEMPLATE_TEMPLATE_PARM: |
25466 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
25467 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
25468 | if (error_operand_p (t: tparm)) |
25469 | return unify_invalid (explain_p); |
25470 | |
25471 | if (TEMPLATE_TYPE_LEVEL (parm) |
25472 | != template_decl_level (decl: tparm)) |
25473 | /* The PARM is not one we're trying to unify. Just check |
25474 | to see if it matches ARG. */ |
25475 | { |
25476 | if (TREE_CODE (arg) == TREE_CODE (parm) |
25477 | && (is_auto (parm) ? is_auto (arg) |
25478 | : same_type_p (parm, arg))) |
25479 | return unify_success (explain_p); |
25480 | else |
25481 | return unify_type_mismatch (explain_p, parm, arg); |
25482 | } |
25483 | idx = TEMPLATE_TYPE_IDX (parm); |
25484 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
25485 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx)); |
25486 | if (error_operand_p (t: tparm)) |
25487 | return unify_invalid (explain_p); |
25488 | |
25489 | /* Check for mixed types and values. */ |
25490 | if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM |
25491 | && TREE_CODE (tparm) != TYPE_DECL) |
25492 | || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
25493 | && TREE_CODE (tparm) != TEMPLATE_DECL)) |
25494 | gcc_unreachable (); |
25495 | |
25496 | if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
25497 | { |
25498 | if ((strict_in & UNIFY_ALLOW_DERIVED) |
25499 | && CLASS_TYPE_P (arg)) |
25500 | { |
25501 | /* First try to match ARG directly. */ |
25502 | tree t = try_class_unification (tparms, targs, parm, arg, |
25503 | explain_p); |
25504 | if (!t) |
25505 | { |
25506 | /* Otherwise, look for a suitable base of ARG, as below. */ |
25507 | enum template_base_result r; |
25508 | r = get_template_base (tparms, targs, parm, arg, |
25509 | explain_p, result: &t); |
25510 | if (!t) |
25511 | return unify_no_common_base (explain_p, r, parm, arg); |
25512 | arg = t; |
25513 | } |
25514 | } |
25515 | /* ARG must be constructed from a template class or a template |
25516 | template parameter. */ |
25517 | else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM |
25518 | && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) |
25519 | return unify_template_deduction_failure (explain_p, parm, arg); |
25520 | |
25521 | /* Deduce arguments T, i from TT<T> or TT<i>. */ |
25522 | if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p)) |
25523 | return 1; |
25524 | |
25525 | arg = TYPE_TI_TEMPLATE (arg); |
25526 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg)) |
25527 | /* If the template is a template template parameter, use the |
25528 | TEMPLATE_TEMPLATE_PARM for matching. */ |
25529 | arg = TREE_TYPE (arg); |
25530 | |
25531 | /* Fall through to deduce template name. */ |
25532 | } |
25533 | |
25534 | if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM |
25535 | || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) |
25536 | { |
25537 | /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */ |
25538 | |
25539 | /* Simple cases: Value already set, does match or doesn't. */ |
25540 | if (targ != NULL_TREE && template_args_equal (ot: targ, nt: arg)) |
25541 | return unify_success (explain_p); |
25542 | else if (targ) |
25543 | return unify_inconsistency (explain_p, parm, first: targ, second: arg); |
25544 | } |
25545 | else |
25546 | { |
25547 | /* If PARM is `const T' and ARG is only `int', we don't have |
25548 | a match unless we are allowing additional qualification. |
25549 | If ARG is `const int' and PARM is just `T' that's OK; |
25550 | that binds `const int' to `T'. */ |
25551 | if (!check_cv_quals_for_unify (strict: strict_in | UNIFY_ALLOW_LESS_CV_QUAL, |
25552 | arg, parm)) |
25553 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
25554 | |
25555 | /* Consider the case where ARG is `const volatile int' and |
25556 | PARM is `const T'. Then, T should be `volatile int'. */ |
25557 | arg = cp_build_qualified_type |
25558 | (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none); |
25559 | if (arg == error_mark_node) |
25560 | return unify_invalid (explain_p); |
25561 | |
25562 | /* Simple cases: Value already set, does match or doesn't. */ |
25563 | if (targ != NULL_TREE && same_type_p (targ, arg)) |
25564 | return unify_success (explain_p); |
25565 | else if (targ) |
25566 | return unify_inconsistency (explain_p, parm, first: targ, second: arg); |
25567 | |
25568 | /* Make sure that ARG is not a variable-sized array. (Note |
25569 | that were talking about variable-sized arrays (like |
25570 | `int[n]'), rather than arrays of unknown size (like |
25571 | `int[]').) We'll get very confused by such a type since |
25572 | the bound of the array is not constant, and therefore |
25573 | not mangleable. Besides, such types are not allowed in |
25574 | ISO C++, so we can do as we please here. We do allow |
25575 | them for 'auto' deduction, since that isn't ABI-exposed. */ |
25576 | if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE)) |
25577 | return unify_vla_arg (explain_p, arg); |
25578 | |
25579 | /* Strip typedefs as in convert_template_argument. */ |
25580 | arg = canonicalize_type_argument (arg, complain: tf_none); |
25581 | } |
25582 | |
25583 | /* If ARG is a parameter pack or an expansion, we cannot unify |
25584 | against it unless PARM is also a parameter pack. */ |
25585 | if ((template_parameter_pack_p (parm: arg) || PACK_EXPANSION_P (arg)) |
25586 | && !template_parameter_pack_p (parm)) |
25587 | return unify_parameter_pack_mismatch (explain_p, parm, arg); |
25588 | |
25589 | /* If the argument deduction results is a METHOD_TYPE, |
25590 | then there is a problem. |
25591 | METHOD_TYPE doesn't map to any real C++ type the result of |
25592 | the deduction cannot be of that type. */ |
25593 | if (TREE_CODE (arg) == METHOD_TYPE) |
25594 | return unify_method_type_error (explain_p, arg); |
25595 | |
25596 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
25597 | return unify_success (explain_p); |
25598 | |
25599 | case TEMPLATE_PARM_INDEX: |
25600 | tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0)); |
25601 | if (error_operand_p (t: tparm)) |
25602 | return unify_invalid (explain_p); |
25603 | |
25604 | if (TEMPLATE_PARM_LEVEL (parm) |
25605 | != template_decl_level (decl: tparm)) |
25606 | { |
25607 | /* The PARM is not one we're trying to unify. Just check |
25608 | to see if it matches ARG. */ |
25609 | int result = !(TREE_CODE (arg) == TREE_CODE (parm) |
25610 | && cp_tree_equal (parm, arg)); |
25611 | if (result) |
25612 | unify_expression_unequal (explain_p, parm, arg); |
25613 | return result; |
25614 | } |
25615 | |
25616 | idx = TEMPLATE_PARM_IDX (parm); |
25617 | targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx); |
25618 | |
25619 | if (targ) |
25620 | { |
25621 | if ((strict & UNIFY_ALLOW_INTEGER) |
25622 | && TREE_TYPE (targ) && TREE_TYPE (arg) |
25623 | && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ))) |
25624 | /* We're deducing from an array bound, the type doesn't matter. |
25625 | This conversion should match the one below. */ |
25626 | arg = fold (build_nop (TREE_TYPE (targ), arg)); |
25627 | int x = !cp_tree_equal (targ, arg); |
25628 | if (x) |
25629 | unify_inconsistency (explain_p, parm, first: targ, second: arg); |
25630 | return x; |
25631 | } |
25632 | |
25633 | /* [temp.deduct.type] If, in the declaration of a function template |
25634 | with a non-type template-parameter, the non-type |
25635 | template-parameter is used in an expression in the function |
25636 | parameter-list and, if the corresponding template-argument is |
25637 | deduced, the template-argument type shall match the type of the |
25638 | template-parameter exactly, except that a template-argument |
25639 | deduced from an array bound may be of any integral type. |
25640 | The non-type parameter might use already deduced type parameters. */ |
25641 | tparm = TREE_TYPE (parm); |
25642 | if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs)) |
25643 | /* We don't have enough levels of args to do any substitution. This |
25644 | can happen in the context of -fnew-ttp-matching. */; |
25645 | else |
25646 | { |
25647 | ++processing_template_decl; |
25648 | tparm = tsubst (t: tparm, args: targs, complain: tf_none, NULL_TREE); |
25649 | --processing_template_decl; |
25650 | |
25651 | if (tree a = type_uses_auto (tparm)) |
25652 | { |
25653 | tparm = do_auto_deduction (tparm, arg, a, |
25654 | complain, adc_unify, targs, |
25655 | LOOKUP_NORMAL, |
25656 | TPARMS_PRIMARY_TEMPLATE (tparms)); |
25657 | if (tparm == error_mark_node) |
25658 | return 1; |
25659 | } |
25660 | } |
25661 | |
25662 | if (!TREE_TYPE (arg) |
25663 | || TREE_CODE (TREE_TYPE (arg)) == DEPENDENT_OPERATOR_TYPE) |
25664 | /* Template-parameter dependent expression. Just accept it for now. |
25665 | It will later be processed in convert_template_argument. */ |
25666 | ; |
25667 | else if (same_type_ignoring_top_level_qualifiers_p |
25668 | (non_reference (TREE_TYPE (arg)), |
25669 | non_reference (tparm))) |
25670 | /* OK. Ignore top-level quals here because a class-type template |
25671 | parameter object is const. */; |
25672 | else if ((strict & UNIFY_ALLOW_INTEGER) |
25673 | && CP_INTEGRAL_TYPE_P (tparm)) |
25674 | /* Convert the ARG to the type of PARM; the deduced non-type |
25675 | template argument must exactly match the types of the |
25676 | corresponding parameter. This conversion should match the |
25677 | one above. */ |
25678 | arg = fold (build_nop (tparm, arg)); |
25679 | else if (uses_template_parms (t: tparm)) |
25680 | { |
25681 | /* We haven't deduced the type of this parameter yet. */ |
25682 | if (cxx_dialect >= cxx17 |
25683 | /* We deduce from array bounds in try_array_deduction. */ |
25684 | && !(strict & UNIFY_ALLOW_INTEGER) |
25685 | && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs)) |
25686 | { |
25687 | /* Deduce it from the non-type argument. As above, ignore |
25688 | top-level quals here too. */ |
25689 | tree atype = cv_unqualified (TREE_TYPE (arg)); |
25690 | RECUR_AND_CHECK_FAILURE (tparms, targs, |
25691 | tparm, atype, |
25692 | UNIFY_ALLOW_NONE, explain_p); |
25693 | /* Now check whether the type of this parameter is still |
25694 | dependent, and give up if so. */ |
25695 | ++processing_template_decl; |
25696 | tparm = tsubst (TREE_TYPE (parm), args: targs, complain: tf_none, NULL_TREE); |
25697 | --processing_template_decl; |
25698 | if (uses_template_parms (t: tparm)) |
25699 | return unify_success (explain_p); |
25700 | } |
25701 | else |
25702 | /* Try again later. */ |
25703 | return unify_success (explain_p); |
25704 | } |
25705 | else |
25706 | return unify_type_mismatch (explain_p, parm: tparm, TREE_TYPE (arg)); |
25707 | |
25708 | /* If ARG is a parameter pack or an expansion, we cannot unify |
25709 | against it unless PARM is also a parameter pack. */ |
25710 | if ((template_parameter_pack_p (parm: arg) || PACK_EXPANSION_P (arg)) |
25711 | && !TEMPLATE_PARM_PARAMETER_PACK (parm)) |
25712 | return unify_parameter_pack_mismatch (explain_p, parm, arg); |
25713 | |
25714 | { |
25715 | bool removed_attr = false; |
25716 | arg = strip_typedefs_expr (arg, &removed_attr); |
25717 | } |
25718 | TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg; |
25719 | return unify_success (explain_p); |
25720 | |
25721 | case PTRMEM_CST: |
25722 | { |
25723 | /* A pointer-to-member constant can be unified only with |
25724 | another constant. */ |
25725 | if (TREE_CODE (arg) != PTRMEM_CST) |
25726 | return unify_ptrmem_cst_mismatch (explain_p, parm, arg); |
25727 | |
25728 | /* Just unify the class member. It would be useless (and possibly |
25729 | wrong, depending on the strict flags) to unify also |
25730 | PTRMEM_CST_CLASS, because we want to be sure that both parm and |
25731 | arg refer to the same variable, even if through different |
25732 | classes. For instance: |
25733 | |
25734 | struct A { int x; }; |
25735 | struct B : A { }; |
25736 | |
25737 | Unification of &A::x and &B::x must succeed. */ |
25738 | return unify (tparms, targs, PTRMEM_CST_MEMBER (parm), |
25739 | PTRMEM_CST_MEMBER (arg), strict, explain_p); |
25740 | } |
25741 | |
25742 | case POINTER_TYPE: |
25743 | { |
25744 | if (!TYPE_PTR_P (arg)) |
25745 | return unify_type_mismatch (explain_p, parm, arg); |
25746 | |
25747 | /* [temp.deduct.call] |
25748 | |
25749 | A can be another pointer or pointer to member type that can |
25750 | be converted to the deduced A via a qualification |
25751 | conversion (_conv.qual_). |
25752 | |
25753 | We pass down STRICT here rather than UNIFY_ALLOW_NONE. |
25754 | This will allow for additional cv-qualification of the |
25755 | pointed-to types if appropriate. */ |
25756 | |
25757 | if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE) |
25758 | /* The derived-to-base conversion only persists through one |
25759 | level of pointers. */ |
25760 | strict |= (strict_in & UNIFY_ALLOW_DERIVED); |
25761 | |
25762 | return unify (tparms, targs, TREE_TYPE (parm), |
25763 | TREE_TYPE (arg), strict, explain_p); |
25764 | } |
25765 | |
25766 | case REFERENCE_TYPE: |
25767 | if (!TYPE_REF_P (arg) |
25768 | || TYPE_REF_IS_RVALUE (parm) != TYPE_REF_IS_RVALUE (arg)) |
25769 | return unify_type_mismatch (explain_p, parm, arg); |
25770 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
25771 | strict: strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p); |
25772 | |
25773 | case ARRAY_TYPE: |
25774 | if (TREE_CODE (arg) != ARRAY_TYPE) |
25775 | return unify_type_mismatch (explain_p, parm, arg); |
25776 | if ((TYPE_DOMAIN (parm) == NULL_TREE) |
25777 | != (TYPE_DOMAIN (arg) == NULL_TREE)) |
25778 | return unify_type_mismatch (explain_p, parm, arg); |
25779 | RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
25780 | strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p); |
25781 | if (TYPE_DOMAIN (parm) != NULL_TREE) |
25782 | return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm), |
25783 | TYPE_DOMAIN (arg), explain_p); |
25784 | return unify_success (explain_p); |
25785 | |
25786 | case REAL_TYPE: |
25787 | case COMPLEX_TYPE: |
25788 | case VECTOR_TYPE: |
25789 | case INTEGER_TYPE: |
25790 | case BOOLEAN_TYPE: |
25791 | case ENUMERAL_TYPE: |
25792 | case VOID_TYPE: |
25793 | case OPAQUE_TYPE: |
25794 | case NULLPTR_TYPE: |
25795 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
25796 | return unify_type_mismatch (explain_p, parm, arg); |
25797 | |
25798 | /* We have already checked cv-qualification at the top of the |
25799 | function. */ |
25800 | if (!same_type_ignoring_top_level_qualifiers_p (arg, parm)) |
25801 | return unify_type_mismatch (explain_p, parm, arg); |
25802 | |
25803 | /* As far as unification is concerned, this wins. Later checks |
25804 | will invalidate it if necessary. */ |
25805 | return unify_success (explain_p); |
25806 | |
25807 | /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */ |
25808 | /* Type INTEGER_CST can come from ordinary constant template args. */ |
25809 | case INTEGER_CST: |
25810 | case REAL_CST: |
25811 | if (TREE_TYPE (arg) == NULL_TREE |
25812 | || !same_type_p (TREE_TYPE (parm), TREE_TYPE (arg))) |
25813 | return unify_template_argument_mismatch (explain_p, parm, arg); |
25814 | while (CONVERT_EXPR_P (arg)) |
25815 | arg = TREE_OPERAND (arg, 0); |
25816 | |
25817 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
25818 | return unify_template_argument_mismatch (explain_p, parm, arg); |
25819 | return (simple_cst_equal (parm, arg) |
25820 | ? unify_success (explain_p) |
25821 | : unify_template_argument_mismatch (explain_p, parm, arg)); |
25822 | |
25823 | case TREE_VEC: |
25824 | { |
25825 | int i, len, argslen; |
25826 | int parm_variadic_p = 0; |
25827 | |
25828 | if (TREE_CODE (arg) != TREE_VEC) |
25829 | return unify_template_argument_mismatch (explain_p, parm, arg); |
25830 | |
25831 | len = TREE_VEC_LENGTH (parm); |
25832 | argslen = TREE_VEC_LENGTH (arg); |
25833 | |
25834 | /* Check for pack expansions in the parameters. */ |
25835 | for (i = 0; i < len; ++i) |
25836 | { |
25837 | if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i))) |
25838 | { |
25839 | if (i == len - 1) |
25840 | /* We can unify against something with a trailing |
25841 | parameter pack. */ |
25842 | parm_variadic_p = 1; |
25843 | else |
25844 | /* [temp.deduct.type]/9: If the template argument list of |
25845 | P contains a pack expansion that is not the last |
25846 | template argument, the entire template argument list |
25847 | is a non-deduced context. */ |
25848 | return unify_success (explain_p); |
25849 | } |
25850 | } |
25851 | |
25852 | /* If we don't have enough arguments to satisfy the parameters |
25853 | (not counting the pack expression at the end), or we have |
25854 | too many arguments for a parameter list that doesn't end in |
25855 | a pack expression, we can't unify. */ |
25856 | if (parm_variadic_p |
25857 | ? argslen < len - parm_variadic_p |
25858 | : argslen != len) |
25859 | return unify_arity (explain_p, TREE_VEC_LENGTH (arg), wanted: len); |
25860 | |
25861 | /* Unify all of the parameters that precede the (optional) |
25862 | pack expression. */ |
25863 | for (i = 0; i < len - parm_variadic_p; ++i) |
25864 | { |
25865 | RECUR_AND_CHECK_FAILURE (tparms, targs, |
25866 | TREE_VEC_ELT (parm, i), |
25867 | TREE_VEC_ELT (arg, i), |
25868 | UNIFY_ALLOW_NONE, explain_p); |
25869 | } |
25870 | if (parm_variadic_p) |
25871 | return unify_pack_expansion (tparms, targs, packed_parms: parm, packed_args: arg, |
25872 | strict: DEDUCE_EXACT, |
25873 | /*subr=*/true, explain_p); |
25874 | return unify_success (explain_p); |
25875 | } |
25876 | |
25877 | case RECORD_TYPE: |
25878 | case UNION_TYPE: |
25879 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
25880 | return unify_type_mismatch (explain_p, parm, arg); |
25881 | |
25882 | if (TYPE_PTRMEMFUNC_P (parm)) |
25883 | { |
25884 | if (!TYPE_PTRMEMFUNC_P (arg)) |
25885 | return unify_type_mismatch (explain_p, parm, arg); |
25886 | |
25887 | return unify (tparms, targs, |
25888 | TYPE_PTRMEMFUNC_FN_TYPE (parm), |
25889 | TYPE_PTRMEMFUNC_FN_TYPE (arg), |
25890 | strict, explain_p); |
25891 | } |
25892 | else if (TYPE_PTRMEMFUNC_P (arg)) |
25893 | return unify_type_mismatch (explain_p, parm, arg); |
25894 | |
25895 | if (CLASSTYPE_TEMPLATE_INFO (parm)) |
25896 | { |
25897 | tree t = NULL_TREE; |
25898 | |
25899 | if (strict_in & UNIFY_ALLOW_DERIVED) |
25900 | { |
25901 | /* First, we try to unify the PARM and ARG directly. */ |
25902 | t = try_class_unification (tparms, targs, |
25903 | parm, arg, explain_p); |
25904 | |
25905 | if (!t) |
25906 | { |
25907 | /* Fallback to the special case allowed in |
25908 | [temp.deduct.call]: |
25909 | |
25910 | If P is a class, and P has the form |
25911 | template-id, then A can be a derived class of |
25912 | the deduced A. Likewise, if P is a pointer to |
25913 | a class of the form template-id, A can be a |
25914 | pointer to a derived class pointed to by the |
25915 | deduced A. */ |
25916 | enum template_base_result r; |
25917 | r = get_template_base (tparms, targs, parm, arg, |
25918 | explain_p, result: &t); |
25919 | |
25920 | if (!t) |
25921 | { |
25922 | /* Don't give the derived diagnostic if we're |
25923 | already dealing with the same template. */ |
25924 | bool same_template |
25925 | = (CLASSTYPE_TEMPLATE_INFO (arg) |
25926 | && (CLASSTYPE_TI_TEMPLATE (parm) |
25927 | == CLASSTYPE_TI_TEMPLATE (arg))); |
25928 | return unify_no_common_base (explain_p: explain_p && !same_template, |
25929 | r, parm, arg); |
25930 | } |
25931 | } |
25932 | } |
25933 | else if (CLASSTYPE_TEMPLATE_INFO (arg) |
25934 | && (CLASSTYPE_TI_TEMPLATE (parm) |
25935 | == CLASSTYPE_TI_TEMPLATE (arg))) |
25936 | /* Perhaps PARM is something like S<U> and ARG is S<int>. |
25937 | Then, we should unify `int' and `U'. */ |
25938 | t = arg; |
25939 | else |
25940 | /* There's no chance of unification succeeding. */ |
25941 | return unify_type_mismatch (explain_p, parm, arg); |
25942 | |
25943 | if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) |
25944 | return unify (tparms, targs, |
25945 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (parm)), |
25946 | INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (t)), |
25947 | UNIFY_ALLOW_NONE, explain_p); |
25948 | gcc_checking_assert (t == arg); |
25949 | } |
25950 | |
25951 | if (!same_type_ignoring_top_level_qualifiers_p (parm, arg)) |
25952 | return unify_type_mismatch (explain_p, parm, arg); |
25953 | return unify_success (explain_p); |
25954 | |
25955 | case METHOD_TYPE: |
25956 | case FUNCTION_TYPE: |
25957 | { |
25958 | unsigned int nargs; |
25959 | tree *args; |
25960 | tree a; |
25961 | unsigned int i; |
25962 | |
25963 | if (TREE_CODE (arg) != TREE_CODE (parm)) |
25964 | return unify_type_mismatch (explain_p, parm, arg); |
25965 | |
25966 | /* CV qualifications for methods can never be deduced, they must |
25967 | match exactly. We need to check them explicitly here, |
25968 | because type_unification_real treats them as any other |
25969 | cv-qualified parameter. */ |
25970 | if (TREE_CODE (parm) == METHOD_TYPE |
25971 | && (!check_cv_quals_for_unify |
25972 | (UNIFY_ALLOW_NONE, |
25973 | arg: class_of_this_parm (fntype: arg), |
25974 | parm: class_of_this_parm (fntype: parm)))) |
25975 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
25976 | if (TREE_CODE (arg) == FUNCTION_TYPE |
25977 | && type_memfn_quals (parm) != type_memfn_quals (arg)) |
25978 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
25979 | if (type_memfn_rqual (parm) != type_memfn_rqual (arg)) |
25980 | return unify_type_mismatch (explain_p, parm, arg); |
25981 | |
25982 | RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), |
25983 | TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p); |
25984 | |
25985 | nargs = list_length (TYPE_ARG_TYPES (arg)); |
25986 | args = XALLOCAVEC (tree, nargs); |
25987 | for (a = TYPE_ARG_TYPES (arg), i = 0; |
25988 | a != NULL_TREE && a != void_list_node; |
25989 | a = TREE_CHAIN (a), ++i) |
25990 | args[i] = TREE_VALUE (a); |
25991 | nargs = i; |
25992 | |
25993 | if (type_unification_real (tparms, full_targs: targs, TYPE_ARG_TYPES (parm), |
25994 | xargs: args, xnargs: nargs, subr: 1, strict: DEDUCE_EXACT, |
25995 | NULL, explain_p)) |
25996 | return 1; |
25997 | |
25998 | if (flag_noexcept_type) |
25999 | { |
26000 | tree pspec = TYPE_RAISES_EXCEPTIONS (parm); |
26001 | tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg)); |
26002 | if (pspec == NULL_TREE) pspec = noexcept_false_spec; |
26003 | if (aspec == NULL_TREE) aspec = noexcept_false_spec; |
26004 | if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec) |
26005 | && uses_template_parms (TREE_PURPOSE (pspec))) |
26006 | RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec), |
26007 | TREE_PURPOSE (aspec), |
26008 | UNIFY_ALLOW_NONE, explain_p); |
26009 | else |
26010 | { |
26011 | bool pn = nothrow_spec_p (pspec); |
26012 | bool an = nothrow_spec_p (aspec); |
26013 | /* Here "less cv-qual" means the deduced arg (i.e. parm) has |
26014 | /more/ noexcept, since function pointer conversions are the |
26015 | reverse of qualification conversions. */ |
26016 | if (an == pn |
26017 | || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL)) |
26018 | || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL))) |
26019 | /* OK. */; |
26020 | else |
26021 | return unify_type_mismatch (explain_p, parm, arg); |
26022 | } |
26023 | } |
26024 | if (flag_tm) |
26025 | { |
26026 | /* As for noexcept. */ |
26027 | bool pn = tx_safe_fn_type_p (parm); |
26028 | bool an = tx_safe_fn_type_p (arg); |
26029 | if (an == pn |
26030 | || (an < pn && (strict & UNIFY_ALLOW_LESS_CV_QUAL)) |
26031 | || (an > pn && (strict & UNIFY_ALLOW_MORE_CV_QUAL))) |
26032 | /* OK. */; |
26033 | else |
26034 | return unify_type_mismatch (explain_p, parm, arg); |
26035 | } |
26036 | |
26037 | return 0; |
26038 | } |
26039 | |
26040 | case OFFSET_TYPE: |
26041 | /* Unify a pointer to member with a pointer to member function, which |
26042 | deduces the type of the member as a function type. */ |
26043 | if (TYPE_PTRMEMFUNC_P (arg)) |
26044 | { |
26045 | /* Check top-level cv qualifiers */ |
26046 | if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm)) |
26047 | return unify_cv_qual_mismatch (explain_p, parm, arg); |
26048 | |
26049 | RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
26050 | TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), |
26051 | UNIFY_ALLOW_NONE, explain_p); |
26052 | |
26053 | /* Determine the type of the function we are unifying against. */ |
26054 | tree fntype = static_fn_type (arg); |
26055 | |
26056 | return unify (tparms, targs, TREE_TYPE (parm), arg: fntype, strict, explain_p); |
26057 | } |
26058 | |
26059 | if (TREE_CODE (arg) != OFFSET_TYPE) |
26060 | return unify_type_mismatch (explain_p, parm, arg); |
26061 | RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm), |
26062 | TYPE_OFFSET_BASETYPE (arg), |
26063 | UNIFY_ALLOW_NONE, explain_p); |
26064 | return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg), |
26065 | strict, explain_p); |
26066 | |
26067 | case CONST_DECL: |
26068 | /* CONST_DECL should already have been folded to its DECL_INITIAL. */ |
26069 | gcc_unreachable (); |
26070 | |
26071 | case FIELD_DECL: |
26072 | case FUNCTION_DECL: |
26073 | case TEMPLATE_DECL: |
26074 | /* Matched cases are handled by the ARG == PARM test above. */ |
26075 | return unify_template_argument_mismatch (explain_p, parm, arg); |
26076 | |
26077 | case VAR_DECL: |
26078 | /* We might get a variable as a non-type template argument in parm if the |
26079 | corresponding parameter is type-dependent. Make any necessary |
26080 | adjustments based on whether arg is a reference. */ |
26081 | if (CONSTANT_CLASS_P (arg)) |
26082 | parm = fold_non_dependent_expr (parm, complain); |
26083 | else if (REFERENCE_REF_P (arg)) |
26084 | { |
26085 | tree sub = TREE_OPERAND (arg, 0); |
26086 | STRIP_NOPS (sub); |
26087 | if (TREE_CODE (sub) == ADDR_EXPR) |
26088 | arg = TREE_OPERAND (sub, 0); |
26089 | } |
26090 | /* Now use the normal expression code to check whether they match. */ |
26091 | goto expr; |
26092 | |
26093 | case TYPE_ARGUMENT_PACK: |
26094 | case NONTYPE_ARGUMENT_PACK: |
26095 | return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm), |
26096 | ARGUMENT_PACK_ARGS (arg), strict, explain_p); |
26097 | |
26098 | case TYPEOF_TYPE: |
26099 | case DECLTYPE_TYPE: |
26100 | case TRAIT_TYPE: |
26101 | /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE, |
26102 | or TRAIT_TYPE nodes. */ |
26103 | return unify_success (explain_p); |
26104 | |
26105 | case ERROR_MARK: |
26106 | /* Unification fails if we hit an error node. */ |
26107 | return unify_invalid (explain_p); |
26108 | |
26109 | case INDIRECT_REF: |
26110 | if (REFERENCE_REF_P (parm)) |
26111 | { |
26112 | bool pexp = PACK_EXPANSION_P (arg); |
26113 | if (pexp) |
26114 | arg = PACK_EXPANSION_PATTERN (arg); |
26115 | if (REFERENCE_REF_P (arg)) |
26116 | arg = TREE_OPERAND (arg, 0); |
26117 | if (pexp) |
26118 | arg = make_pack_expansion (arg, complain); |
26119 | return unify (tparms, targs, TREE_OPERAND (parm, 0), arg, |
26120 | strict, explain_p); |
26121 | } |
26122 | /* FALLTHRU */ |
26123 | |
26124 | default: |
26125 | /* An unresolved overload is a nondeduced context. */ |
26126 | if (is_overloaded_fn (parm) || type_unknown_p (expr: parm)) |
26127 | return unify_success (explain_p); |
26128 | gcc_assert (EXPR_P (parm) |
26129 | || TREE_CODE (parm) == CONSTRUCTOR |
26130 | || TREE_CODE (parm) == TRAIT_EXPR); |
26131 | expr: |
26132 | /* We must be looking at an expression. This can happen with |
26133 | something like: |
26134 | |
26135 | template <int I> |
26136 | void foo(S<I>, S<I + 2>); |
26137 | |
26138 | or |
26139 | |
26140 | template<typename T> |
26141 | void foo(A<T, T{}>); |
26142 | |
26143 | This is a "non-deduced context": |
26144 | |
26145 | [deduct.type] |
26146 | |
26147 | The non-deduced contexts are: |
26148 | |
26149 | --A non-type template argument or an array bound in which |
26150 | a subexpression references a template parameter. |
26151 | |
26152 | In these cases, we assume deduction succeeded, but don't |
26153 | actually infer any unifications. */ |
26154 | |
26155 | if (!uses_template_parms (t: parm) |
26156 | && !template_args_equal (ot: parm, nt: arg)) |
26157 | return unify_expression_unequal (explain_p, parm, arg); |
26158 | else |
26159 | return unify_success (explain_p); |
26160 | } |
26161 | } |
26162 | #undef RECUR_AND_CHECK_FAILURE |
26163 | |
26164 | /* Note that DECL can be defined in this translation unit, if |
26165 | required. */ |
26166 | |
26167 | static void |
26168 | mark_definable (tree decl) |
26169 | { |
26170 | tree clone; |
26171 | DECL_NOT_REALLY_EXTERN (decl) = 1; |
26172 | FOR_EACH_CLONE (clone, decl) |
26173 | DECL_NOT_REALLY_EXTERN (clone) = 1; |
26174 | } |
26175 | |
26176 | /* DECL is an explicit instantiation definition, ensure that it will |
26177 | be written out here and that it won't clash with other instantiations |
26178 | in other translation units. */ |
26179 | |
26180 | void |
26181 | setup_explicit_instantiation_definition_linkage (tree decl) |
26182 | { |
26183 | mark_definable (decl); |
26184 | mark_needed (decl); |
26185 | /* Always make artificials weak. */ |
26186 | if (DECL_ARTIFICIAL (decl) && flag_weak) |
26187 | comdat_linkage (decl); |
26188 | /* We also want to put explicit instantiations in linkonce sections. */ |
26189 | else if (TREE_PUBLIC (decl)) |
26190 | maybe_make_one_only (decl); |
26191 | } |
26192 | |
26193 | /* Called if RESULT is explicitly instantiated, or is a member of an |
26194 | explicitly instantiated class. */ |
26195 | |
26196 | void |
26197 | mark_decl_instantiated (tree result, int extern_p) |
26198 | { |
26199 | SET_DECL_EXPLICIT_INSTANTIATION (result); |
26200 | |
26201 | /* If this entity has already been written out, it's too late to |
26202 | make any modifications. */ |
26203 | if (TREE_ASM_WRITTEN (result)) |
26204 | return; |
26205 | |
26206 | /* consteval functions are never emitted. */ |
26207 | if (TREE_CODE (result) == FUNCTION_DECL |
26208 | && DECL_IMMEDIATE_FUNCTION_P (result)) |
26209 | return; |
26210 | |
26211 | /* For anonymous namespace we don't need to do anything. */ |
26212 | if (decl_internal_context_p (result)) |
26213 | { |
26214 | gcc_assert (!TREE_PUBLIC (result)); |
26215 | return; |
26216 | } |
26217 | |
26218 | if (TREE_CODE (result) != FUNCTION_DECL) |
26219 | /* The TREE_PUBLIC flag for function declarations will have been |
26220 | set correctly by tsubst. */ |
26221 | TREE_PUBLIC (result) = 1; |
26222 | |
26223 | if (extern_p) |
26224 | { |
26225 | DECL_EXTERNAL (result) = 1; |
26226 | DECL_NOT_REALLY_EXTERN (result) = 0; |
26227 | } |
26228 | else |
26229 | { |
26230 | set_instantiating_module (result); |
26231 | setup_explicit_instantiation_definition_linkage (result); |
26232 | if (TREE_CODE (result) == FUNCTION_DECL |
26233 | && DECL_TEMPLATE_INSTANTIATED (result)) |
26234 | /* If the function has already been instantiated, clear DECL_EXTERNAL, |
26235 | since start_preparsed_function wouldn't have if we had an earlier |
26236 | extern explicit instantiation. */ |
26237 | DECL_EXTERNAL (result) = 0; |
26238 | } |
26239 | |
26240 | /* If EXTERN_P, then this function will not be emitted -- unless |
26241 | followed by an explicit instantiation, at which point its linkage |
26242 | will be adjusted. If !EXTERN_P, then this function will be |
26243 | emitted here. In neither circumstance do we want |
26244 | import_export_decl to adjust the linkage. */ |
26245 | DECL_INTERFACE_KNOWN (result) = 1; |
26246 | } |
26247 | |
26248 | /* Subroutine of more_specialized_fn: check whether TARGS is missing any |
26249 | important template arguments. If any are missing, we check whether |
26250 | they're important by using error_mark_node for substituting into any |
26251 | args that were used for partial ordering (the ones between ARGS and END) |
26252 | and seeing if it bubbles up. */ |
26253 | |
26254 | static bool |
26255 | check_undeduced_parms (tree targs, tree args, tree end) |
26256 | { |
26257 | bool found = false; |
26258 | for (tree& targ : tree_vec_range (targs)) |
26259 | if (targ == NULL_TREE) |
26260 | { |
26261 | found = true; |
26262 | targ = error_mark_node; |
26263 | } |
26264 | if (found) |
26265 | { |
26266 | tree substed = tsubst_arg_types (arg_types: args, args: targs, end, complain: tf_none, NULL_TREE); |
26267 | if (substed == error_mark_node) |
26268 | return true; |
26269 | } |
26270 | return false; |
26271 | } |
26272 | |
26273 | /* Given two function templates PAT1 and PAT2, return: |
26274 | |
26275 | 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order]. |
26276 | -1 if PAT2 is more specialized than PAT1. |
26277 | 0 if neither is more specialized. |
26278 | |
26279 | LEN indicates the number of parameters we should consider |
26280 | (defaulted parameters should not be considered). |
26281 | |
26282 | The 1998 std underspecified function template partial ordering, and |
26283 | DR214 addresses the issue. We take pairs of arguments, one from |
26284 | each of the templates, and deduce them against each other. One of |
26285 | the templates will be more specialized if all the *other* |
26286 | template's arguments deduce against its arguments and at least one |
26287 | of its arguments *does* *not* deduce against the other template's |
26288 | corresponding argument. Deduction is done as for class templates. |
26289 | The arguments used in deduction have reference and top level cv |
26290 | qualifiers removed. Iff both arguments were originally reference |
26291 | types *and* deduction succeeds in both directions, an lvalue reference |
26292 | wins against an rvalue reference and otherwise the template |
26293 | with the more cv-qualified argument wins for that pairing (if |
26294 | neither is more cv-qualified, they both are equal). Unlike regular |
26295 | deduction, after all the arguments have been deduced in this way, |
26296 | we do *not* verify the deduced template argument values can be |
26297 | substituted into non-deduced contexts. |
26298 | |
26299 | The logic can be a bit confusing here, because we look at deduce1 and |
26300 | targs1 to see if pat2 is at least as specialized, and vice versa; if we |
26301 | can find template arguments for pat1 to make arg1 look like arg2, that |
26302 | means that arg2 is at least as specialized as arg1. */ |
26303 | |
26304 | int |
26305 | more_specialized_fn (tree pat1, tree pat2, int len) |
26306 | { |
26307 | tree decl1 = DECL_TEMPLATE_RESULT (pat1); |
26308 | tree decl2 = DECL_TEMPLATE_RESULT (pat2); |
26309 | tree targs1 = make_tree_vec (DECL_NTPARMS (pat1)); |
26310 | tree targs2 = make_tree_vec (DECL_NTPARMS (pat2)); |
26311 | tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1); |
26312 | tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2); |
26313 | tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1)); |
26314 | tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2)); |
26315 | tree origs1, origs2; |
26316 | bool lose1 = false; |
26317 | bool lose2 = false; |
26318 | |
26319 | /* C++17 [temp.func.order]/3 (CWG532) |
26320 | |
26321 | If only one of the function templates M is a non-static member of some |
26322 | class A, M is considered to have a new first parameter inserted in its |
26323 | function parameter list. Given cv as the cv-qualifiers of M (if any), the |
26324 | new parameter is of type "rvalue reference to cv A" if the optional |
26325 | ref-qualifier of M is && or if M has no ref-qualifier and the first |
26326 | parameter of the other template has rvalue reference type. Otherwise, the |
26327 | new parameter is of type "lvalue reference to cv A". */ |
26328 | |
26329 | if (DECL_STATIC_FUNCTION_P (decl1) || DECL_STATIC_FUNCTION_P (decl2)) |
26330 | { |
26331 | /* Note C++20 DR2445 extended the above to static member functions, but |
26332 | I think the old G++ behavior of just skipping the object |
26333 | parameter when comparing to a static member function was better, so |
26334 | let's stick with that for now. This is CWG2834. --jason 2023-12 */ |
26335 | if (DECL_OBJECT_MEMBER_FUNCTION_P (decl1)) |
26336 | { |
26337 | len--; /* LEN is the number of significant arguments for DECL1 */ |
26338 | args1 = TREE_CHAIN (args1); |
26339 | } |
26340 | else if (DECL_OBJECT_MEMBER_FUNCTION_P (decl2)) |
26341 | args2 = TREE_CHAIN (args2); |
26342 | } |
26343 | else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1) |
26344 | && DECL_IOBJ_MEMBER_FUNCTION_P (decl2)) |
26345 | { |
26346 | /* Note DR2445 also (IMO wrongly) removed the "only one" above, which |
26347 | would break e.g. cpp1y/lambda-generic-variadic5.C. */ |
26348 | len--; |
26349 | args1 = TREE_CHAIN (args1); |
26350 | args2 = TREE_CHAIN (args2); |
26351 | } |
26352 | else if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1) |
26353 | || DECL_IOBJ_MEMBER_FUNCTION_P (decl2)) |
26354 | { |
26355 | /* The other is a non-member or explicit object member function; |
26356 | rewrite the implicit object parameter to a reference. */ |
26357 | tree ns = DECL_IOBJ_MEMBER_FUNCTION_P (decl2) ? decl2 : decl1; |
26358 | tree &nsargs = ns == decl2 ? args2 : args1; |
26359 | tree obtype = TREE_TYPE (TREE_VALUE (nsargs)); |
26360 | |
26361 | nsargs = TREE_CHAIN (nsargs); |
26362 | |
26363 | cp_ref_qualifier rqual = type_memfn_rqual (TREE_TYPE (ns)); |
26364 | if (rqual == REF_QUAL_NONE) |
26365 | { |
26366 | tree otherfirst = ns == decl1 ? args2 : args1; |
26367 | otherfirst = TREE_VALUE (otherfirst); |
26368 | if (TREE_CODE (otherfirst) == REFERENCE_TYPE |
26369 | && TYPE_REF_IS_RVALUE (otherfirst)) |
26370 | rqual = REF_QUAL_RVALUE; |
26371 | } |
26372 | obtype = cp_build_reference_type (obtype, rqual == REF_QUAL_RVALUE); |
26373 | nsargs = tree_cons (NULL_TREE, obtype, nsargs); |
26374 | } |
26375 | |
26376 | /* If only one is a conversion operator, they are unordered. */ |
26377 | if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2)) |
26378 | return 0; |
26379 | |
26380 | /* Consider the return type for a conversion function */ |
26381 | if (DECL_CONV_FN_P (decl1)) |
26382 | { |
26383 | args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1); |
26384 | args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2); |
26385 | len++; |
26386 | } |
26387 | |
26388 | processing_template_decl++; |
26389 | |
26390 | origs1 = args1; |
26391 | origs2 = args2; |
26392 | |
26393 | while (len-- |
26394 | /* Stop when an ellipsis is seen. */ |
26395 | && args1 != NULL_TREE && args2 != NULL_TREE) |
26396 | { |
26397 | tree arg1 = TREE_VALUE (args1); |
26398 | tree arg2 = TREE_VALUE (args2); |
26399 | int deduce1, deduce2; |
26400 | int quals1 = -1; |
26401 | int quals2 = -1; |
26402 | int ref1 = 0; |
26403 | int ref2 = 0; |
26404 | |
26405 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
26406 | && TREE_CODE (arg2) == TYPE_PACK_EXPANSION) |
26407 | { |
26408 | /* When both arguments are pack expansions, we need only |
26409 | unify the patterns themselves. */ |
26410 | arg1 = PACK_EXPANSION_PATTERN (arg1); |
26411 | arg2 = PACK_EXPANSION_PATTERN (arg2); |
26412 | |
26413 | /* This is the last comparison we need to do. */ |
26414 | len = 0; |
26415 | } |
26416 | |
26417 | if (TYPE_REF_P (arg1)) |
26418 | { |
26419 | ref1 = TYPE_REF_IS_RVALUE (arg1) + 1; |
26420 | arg1 = TREE_TYPE (arg1); |
26421 | quals1 = cp_type_quals (arg1); |
26422 | } |
26423 | |
26424 | if (TYPE_REF_P (arg2)) |
26425 | { |
26426 | ref2 = TYPE_REF_IS_RVALUE (arg2) + 1; |
26427 | arg2 = TREE_TYPE (arg2); |
26428 | quals2 = cp_type_quals (arg2); |
26429 | } |
26430 | |
26431 | arg1 = TYPE_MAIN_VARIANT (arg1); |
26432 | arg2 = TYPE_MAIN_VARIANT (arg2); |
26433 | |
26434 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION) |
26435 | { |
26436 | int i, len2 = remaining_arguments (args2); |
26437 | tree parmvec = make_tree_vec (1); |
26438 | tree argvec = make_tree_vec (len2); |
26439 | tree ta = args2; |
26440 | |
26441 | /* Setup the parameter vector, which contains only ARG1. */ |
26442 | TREE_VEC_ELT (parmvec, 0) = arg1; |
26443 | |
26444 | /* Setup the argument vector, which contains the remaining |
26445 | arguments. */ |
26446 | for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta)) |
26447 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); |
26448 | |
26449 | deduce1 = (unify_pack_expansion (tparms: tparms1, targs: targs1, packed_parms: parmvec, |
26450 | packed_args: argvec, strict: DEDUCE_EXACT, |
26451 | /*subr=*/true, /*explain_p=*/false) |
26452 | == 0); |
26453 | |
26454 | /* We cannot deduce in the other direction, because ARG1 is |
26455 | a pack expansion but ARG2 is not. */ |
26456 | deduce2 = 0; |
26457 | } |
26458 | else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION) |
26459 | { |
26460 | int i, len1 = remaining_arguments (args1); |
26461 | tree parmvec = make_tree_vec (1); |
26462 | tree argvec = make_tree_vec (len1); |
26463 | tree ta = args1; |
26464 | |
26465 | /* Setup the parameter vector, which contains only ARG1. */ |
26466 | TREE_VEC_ELT (parmvec, 0) = arg2; |
26467 | |
26468 | /* Setup the argument vector, which contains the remaining |
26469 | arguments. */ |
26470 | for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta)) |
26471 | TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta); |
26472 | |
26473 | deduce2 = (unify_pack_expansion (tparms: tparms2, targs: targs2, packed_parms: parmvec, |
26474 | packed_args: argvec, strict: DEDUCE_EXACT, |
26475 | /*subr=*/true, /*explain_p=*/false) |
26476 | == 0); |
26477 | |
26478 | /* We cannot deduce in the other direction, because ARG2 is |
26479 | a pack expansion but ARG1 is not.*/ |
26480 | deduce1 = 0; |
26481 | } |
26482 | |
26483 | else |
26484 | { |
26485 | /* The normal case, where neither argument is a pack |
26486 | expansion. */ |
26487 | deduce1 = (unify (tparms: tparms1, targs: targs1, parm: arg1, arg: arg2, |
26488 | UNIFY_ALLOW_NONE, /*explain_p=*/false) |
26489 | == 0); |
26490 | deduce2 = (unify (tparms: tparms2, targs: targs2, parm: arg2, arg: arg1, |
26491 | UNIFY_ALLOW_NONE, /*explain_p=*/false) |
26492 | == 0); |
26493 | } |
26494 | |
26495 | /* If we couldn't deduce arguments for tparms1 to make arg1 match |
26496 | arg2, then arg2 is not as specialized as arg1. */ |
26497 | if (!deduce1) |
26498 | lose2 = true; |
26499 | if (!deduce2) |
26500 | lose1 = true; |
26501 | |
26502 | /* "If, for a given type, deduction succeeds in both directions |
26503 | (i.e., the types are identical after the transformations above) |
26504 | and both P and A were reference types (before being replaced with |
26505 | the type referred to above): |
26506 | - if the type from the argument template was an lvalue reference and |
26507 | the type from the parameter template was not, the argument type is |
26508 | considered to be more specialized than the other; otherwise, |
26509 | - if the type from the argument template is more cv-qualified |
26510 | than the type from the parameter template (as described above), |
26511 | the argument type is considered to be more specialized than the other; |
26512 | otherwise, |
26513 | - neither type is more specialized than the other." */ |
26514 | |
26515 | if (deduce1 && deduce2) |
26516 | { |
26517 | if (ref1 && ref2 && ref1 != ref2) |
26518 | { |
26519 | if (ref1 > ref2) |
26520 | lose1 = true; |
26521 | else |
26522 | lose2 = true; |
26523 | } |
26524 | else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0) |
26525 | { |
26526 | if ((quals1 & quals2) == quals2) |
26527 | lose2 = true; |
26528 | if ((quals1 & quals2) == quals1) |
26529 | lose1 = true; |
26530 | } |
26531 | } |
26532 | |
26533 | if (lose1 && lose2) |
26534 | /* We've failed to deduce something in either direction. |
26535 | These must be unordered. */ |
26536 | break; |
26537 | |
26538 | if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION |
26539 | || TREE_CODE (arg2) == TYPE_PACK_EXPANSION) |
26540 | /* We have already processed all of the arguments in our |
26541 | handing of the pack expansion type. */ |
26542 | len = 0; |
26543 | |
26544 | args1 = TREE_CHAIN (args1); |
26545 | args2 = TREE_CHAIN (args2); |
26546 | } |
26547 | |
26548 | /* "In most cases, all template parameters must have values in order for |
26549 | deduction to succeed, but for partial ordering purposes a template |
26550 | parameter may remain without a value provided it is not used in the |
26551 | types being used for partial ordering." |
26552 | |
26553 | Thus, if we are missing any of the targs1 we need to substitute into |
26554 | origs1, then pat2 is not as specialized as pat1. This can happen when |
26555 | there is a nondeduced context. */ |
26556 | if (!lose2 && check_undeduced_parms (targs: targs1, args: origs1, end: args1)) |
26557 | lose2 = true; |
26558 | if (!lose1 && check_undeduced_parms (targs: targs2, args: origs2, end: args2)) |
26559 | lose1 = true; |
26560 | |
26561 | processing_template_decl--; |
26562 | |
26563 | /* If both deductions succeed, the partial ordering selects the more |
26564 | constrained template. */ |
26565 | /* P2113: If the corresponding template-parameters of the |
26566 | template-parameter-lists are not equivalent ([temp.over.link]) or if |
26567 | the function parameters that positionally correspond between the two |
26568 | templates are not of the same type, neither template is more |
26569 | specialized than the other. */ |
26570 | if (!lose1 && !lose2 |
26571 | && comp_template_parms (DECL_TEMPLATE_PARMS (pat1), |
26572 | DECL_TEMPLATE_PARMS (pat2)) |
26573 | && compparms (origs1, origs2)) |
26574 | { |
26575 | int winner = more_constrained (decl1, decl2); |
26576 | if (winner > 0) |
26577 | lose2 = true; |
26578 | else if (winner < 0) |
26579 | lose1 = true; |
26580 | } |
26581 | |
26582 | /* All things being equal, if the next argument is a pack expansion |
26583 | for one function but not for the other, prefer the |
26584 | non-variadic function. FIXME this is bogus; see c++/41958. */ |
26585 | if (lose1 == lose2 |
26586 | && args1 && TREE_VALUE (args1) |
26587 | && args2 && TREE_VALUE (args2)) |
26588 | { |
26589 | lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION; |
26590 | lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION; |
26591 | } |
26592 | |
26593 | if (lose1 == lose2) |
26594 | return 0; |
26595 | else if (!lose1) |
26596 | return 1; |
26597 | else |
26598 | return -1; |
26599 | } |
26600 | |
26601 | /* Determine which of two partial specializations of TMPL is more |
26602 | specialized. |
26603 | |
26604 | PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding |
26605 | to the first partial specialization. The TREE_PURPOSE is the |
26606 | innermost set of template parameters for the partial |
26607 | specialization. PAT2 is similar, but for the second template. |
26608 | |
26609 | Return 1 if the first partial specialization is more specialized; |
26610 | -1 if the second is more specialized; 0 if neither is more |
26611 | specialized. |
26612 | |
26613 | See [temp.class.order] for information about determining which of |
26614 | two templates is more specialized. */ |
26615 | |
26616 | static int |
26617 | more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2) |
26618 | { |
26619 | tree targs; |
26620 | int winner = 0; |
26621 | bool any_deductions = false; |
26622 | |
26623 | tree tmpl1 = TREE_VALUE (pat1); |
26624 | tree tmpl2 = TREE_VALUE (pat2); |
26625 | tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1))); |
26626 | tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2))); |
26627 | |
26628 | /* Just like what happens for functions, if we are ordering between |
26629 | different template specializations, we may encounter dependent |
26630 | types in the arguments, and we need our dependency check functions |
26631 | to behave correctly. */ |
26632 | ++processing_template_decl; |
26633 | targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2); |
26634 | if (targs) |
26635 | { |
26636 | --winner; |
26637 | any_deductions = true; |
26638 | } |
26639 | |
26640 | targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1); |
26641 | if (targs) |
26642 | { |
26643 | ++winner; |
26644 | any_deductions = true; |
26645 | } |
26646 | --processing_template_decl; |
26647 | |
26648 | /* If both deductions succeed, the partial ordering selects the more |
26649 | constrained template. */ |
26650 | if (!winner && any_deductions) |
26651 | winner = more_constrained (tmpl1, tmpl2); |
26652 | |
26653 | /* In the case of a tie where at least one of the templates |
26654 | has a parameter pack at the end, the template with the most |
26655 | non-packed parameters wins. */ |
26656 | if (winner == 0 |
26657 | && any_deductions |
26658 | && (template_args_variadic_p (TREE_PURPOSE (pat1)) |
26659 | || template_args_variadic_p (TREE_PURPOSE (pat2)))) |
26660 | { |
26661 | tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1)); |
26662 | tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2)); |
26663 | int len1 = TREE_VEC_LENGTH (args1); |
26664 | int len2 = TREE_VEC_LENGTH (args2); |
26665 | |
26666 | /* We don't count the pack expansion at the end. */ |
26667 | if (template_args_variadic_p (TREE_PURPOSE (pat1))) |
26668 | --len1; |
26669 | if (template_args_variadic_p (TREE_PURPOSE (pat2))) |
26670 | --len2; |
26671 | |
26672 | if (len1 > len2) |
26673 | return 1; |
26674 | else if (len1 < len2) |
26675 | return -1; |
26676 | } |
26677 | |
26678 | return winner; |
26679 | } |
26680 | |
26681 | /* Return the template arguments that will produce the function signature |
26682 | DECL from the function template FN, with the explicit template |
26683 | arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must |
26684 | also match. Return NULL_TREE if no satisfactory arguments could be |
26685 | found. */ |
26686 | |
26687 | static tree |
26688 | get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype) |
26689 | { |
26690 | int ntparms = DECL_NTPARMS (fn); |
26691 | tree targs = make_tree_vec (ntparms); |
26692 | tree decl_type = TREE_TYPE (decl); |
26693 | tree decl_arg_types; |
26694 | tree *args; |
26695 | unsigned int nargs, ix; |
26696 | tree arg; |
26697 | |
26698 | gcc_assert (decl != DECL_TEMPLATE_RESULT (fn)); |
26699 | |
26700 | /* Never do unification on the 'this' parameter. */ |
26701 | decl_arg_types = skip_artificial_parms_for (decl, |
26702 | TYPE_ARG_TYPES (decl_type)); |
26703 | |
26704 | nargs = list_length (decl_arg_types); |
26705 | args = XALLOCAVEC (tree, nargs); |
26706 | for (arg = decl_arg_types, ix = 0; |
26707 | arg != NULL_TREE; |
26708 | arg = TREE_CHAIN (arg), ++ix) |
26709 | args[ix] = TREE_VALUE (arg); |
26710 | |
26711 | if (fn_type_unification (fn, explicit_targs: explicit_args, targs, |
26712 | args, nargs: ix, |
26713 | return_type: (check_rettype || DECL_CONV_FN_P (fn) |
26714 | ? TREE_TYPE (decl_type) : NULL_TREE), |
26715 | strict: DEDUCE_EXACT, LOOKUP_NORMAL, NULL, |
26716 | /*explain_p=*/false, |
26717 | /*decltype*/decltype_p: false) |
26718 | == error_mark_node) |
26719 | return NULL_TREE; |
26720 | |
26721 | return targs; |
26722 | } |
26723 | |
26724 | /* Return the innermost template arguments that, when applied to a partial |
26725 | specialization SPEC_TMPL of TMPL, yield the ARGS. |
26726 | |
26727 | For example, suppose we have: |
26728 | |
26729 | template <class T, class U> struct S {}; |
26730 | template <class T> struct S<T*, int> {}; |
26731 | |
26732 | Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the |
26733 | partial specialization and the ARGS will be {double*, int}. The resulting |
26734 | vector will be {double}, indicating that `T' is bound to `double'. */ |
26735 | |
26736 | static tree |
26737 | get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args) |
26738 | { |
26739 | tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl); |
26740 | tree spec_args |
26741 | = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl))); |
26742 | int i, ntparms = TREE_VEC_LENGTH (tparms); |
26743 | tree deduced_args; |
26744 | tree innermost_deduced_args; |
26745 | |
26746 | innermost_deduced_args = make_tree_vec (ntparms); |
26747 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) |
26748 | { |
26749 | deduced_args = copy_node (args); |
26750 | SET_TMPL_ARGS_LEVEL (deduced_args, |
26751 | TMPL_ARGS_DEPTH (deduced_args), |
26752 | innermost_deduced_args); |
26753 | } |
26754 | else |
26755 | deduced_args = innermost_deduced_args; |
26756 | |
26757 | bool tried_array_deduction = (cxx_dialect < cxx17); |
26758 | again: |
26759 | if (unify (tparms, targs: deduced_args, |
26760 | INNERMOST_TEMPLATE_ARGS (spec_args), |
26761 | INNERMOST_TEMPLATE_ARGS (args), |
26762 | UNIFY_ALLOW_NONE, /*explain_p=*/false)) |
26763 | return NULL_TREE; |
26764 | |
26765 | for (i = 0; i < ntparms; ++i) |
26766 | if (! TREE_VEC_ELT (innermost_deduced_args, i)) |
26767 | { |
26768 | if (!tried_array_deduction) |
26769 | { |
26770 | try_array_deduction (tparms, targs: innermost_deduced_args, |
26771 | INNERMOST_TEMPLATE_ARGS (spec_args)); |
26772 | tried_array_deduction = true; |
26773 | if (TREE_VEC_ELT (innermost_deduced_args, i)) |
26774 | goto again; |
26775 | } |
26776 | return NULL_TREE; |
26777 | } |
26778 | |
26779 | if (!push_tinst_level (tmpl: spec_tmpl, args: deduced_args)) |
26780 | { |
26781 | excessive_deduction_depth = true; |
26782 | return NULL_TREE; |
26783 | } |
26784 | |
26785 | /* Verify that nondeduced template arguments agree with the type |
26786 | obtained from argument deduction. |
26787 | |
26788 | For example: |
26789 | |
26790 | struct A { typedef int X; }; |
26791 | template <class T, class U> struct C {}; |
26792 | template <class T> struct C<T, typename T::X> {}; |
26793 | |
26794 | Then with the instantiation `C<A, int>', we can deduce that |
26795 | `T' is `A' but unify () does not check whether `typename T::X' |
26796 | is `int'. */ |
26797 | spec_args = tsubst (t: spec_args, args: deduced_args, complain: tf_none, NULL_TREE); |
26798 | |
26799 | if (spec_args != error_mark_node) |
26800 | spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), |
26801 | INNERMOST_TEMPLATE_ARGS (spec_args), |
26802 | in_decl: tmpl, complain: tf_none, require_all_args: false); |
26803 | |
26804 | pop_tinst_level (); |
26805 | |
26806 | if (spec_args == error_mark_node |
26807 | /* We only need to check the innermost arguments; the other |
26808 | arguments will always agree. */ |
26809 | || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args), |
26810 | INNERMOST_TEMPLATE_ARGS (args))) |
26811 | return NULL_TREE; |
26812 | |
26813 | /* Now that we have bindings for all of the template arguments, |
26814 | ensure that the arguments deduced for the template template |
26815 | parameters have compatible template parameter lists. See the use |
26816 | of template_template_parm_bindings_ok_p in fn_type_unification |
26817 | for more information. */ |
26818 | if (!template_template_parm_bindings_ok_p (tparms, targs: deduced_args)) |
26819 | return NULL_TREE; |
26820 | |
26821 | return deduced_args; |
26822 | } |
26823 | |
26824 | // Compare two function templates T1 and T2 by deducing bindings |
26825 | // from one against the other. If both deductions succeed, compare |
26826 | // constraints to see which is more constrained. |
26827 | static int |
26828 | more_specialized_inst (tree t1, tree t2) |
26829 | { |
26830 | int fate = 0; |
26831 | int count = 0; |
26832 | |
26833 | if (get_bindings (fn: t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, check_rettype: true)) |
26834 | { |
26835 | --fate; |
26836 | ++count; |
26837 | } |
26838 | |
26839 | if (get_bindings (fn: t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, check_rettype: true)) |
26840 | { |
26841 | ++fate; |
26842 | ++count; |
26843 | } |
26844 | |
26845 | // If both deductions succeed, then one may be more constrained. |
26846 | if (count == 2 && fate == 0) |
26847 | fate = more_constrained (t1, t2); |
26848 | |
26849 | return fate; |
26850 | } |
26851 | |
26852 | /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL. |
26853 | Return the TREE_LIST node with the most specialized template, if |
26854 | any. If there is no most specialized template, the error_mark_node |
26855 | is returned. |
26856 | |
26857 | Note that this function does not look at, or modify, the |
26858 | TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node |
26859 | returned is one of the elements of INSTANTIATIONS, callers may |
26860 | store information in the TREE_PURPOSE or TREE_TYPE of the nodes, |
26861 | and retrieve it from the value returned. */ |
26862 | |
26863 | tree |
26864 | most_specialized_instantiation (tree templates) |
26865 | { |
26866 | tree fn, champ; |
26867 | |
26868 | ++processing_template_decl; |
26869 | |
26870 | champ = templates; |
26871 | for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn)) |
26872 | { |
26873 | gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn)); |
26874 | int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)); |
26875 | if (fate == -1) |
26876 | champ = fn; |
26877 | else if (!fate) |
26878 | { |
26879 | /* Equally specialized, move to next function. If there |
26880 | is no next function, nothing's most specialized. */ |
26881 | fn = TREE_CHAIN (fn); |
26882 | champ = fn; |
26883 | if (!fn) |
26884 | break; |
26885 | } |
26886 | } |
26887 | |
26888 | if (champ) |
26889 | /* Now verify that champ is better than everything earlier in the |
26890 | instantiation list. */ |
26891 | for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) { |
26892 | if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1) |
26893 | { |
26894 | champ = NULL_TREE; |
26895 | break; |
26896 | } |
26897 | } |
26898 | |
26899 | processing_template_decl--; |
26900 | |
26901 | if (!champ) |
26902 | return error_mark_node; |
26903 | |
26904 | return champ; |
26905 | } |
26906 | |
26907 | /* If DECL is a specialization of some template, return the most |
26908 | general such template. Otherwise, returns NULL_TREE. |
26909 | |
26910 | For example, given: |
26911 | |
26912 | template <class T> struct S { template <class U> void f(U); }; |
26913 | |
26914 | if TMPL is `template <class U> void S<int>::f(U)' this will return |
26915 | the full template. This function will not trace past partial |
26916 | specializations, however. For example, given in addition: |
26917 | |
26918 | template <class T> struct S<T*> { template <class U> void f(U); }; |
26919 | |
26920 | if TMPL is `template <class U> void S<int*>::f(U)' this will return |
26921 | `template <class T> template <class U> S<T*>::f(U)'. */ |
26922 | |
26923 | tree |
26924 | most_general_template (const_tree decl) |
26925 | { |
26926 | if (TREE_CODE (decl) != TEMPLATE_DECL) |
26927 | { |
26928 | if (tree tinfo = get_template_info (t: decl)) |
26929 | decl = TI_TEMPLATE (tinfo); |
26930 | /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a |
26931 | template friend, or a FIELD_DECL for a capture pack. */ |
26932 | if (TREE_CODE (decl) != TEMPLATE_DECL) |
26933 | return NULL_TREE; |
26934 | } |
26935 | |
26936 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (decl)) |
26937 | return DECL_TI_TEMPLATE (DECL_TEMPLATE_RESULT (decl)); |
26938 | |
26939 | /* Look for more and more general templates. */ |
26940 | while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) |
26941 | { |
26942 | /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases. |
26943 | (See cp-tree.h for details.) */ |
26944 | if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL) |
26945 | break; |
26946 | |
26947 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
26948 | && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl))) |
26949 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) |
26950 | break; |
26951 | |
26952 | /* Stop if we run into an explicitly specialized class template. */ |
26953 | if (!DECL_NAMESPACE_SCOPE_P (decl) |
26954 | && DECL_CONTEXT (decl) |
26955 | && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl))) |
26956 | break; |
26957 | |
26958 | decl = DECL_TI_TEMPLATE (decl); |
26959 | } |
26960 | |
26961 | return CONST_CAST_TREE (decl); |
26962 | } |
26963 | |
26964 | /* Return the most specialized of the template partial specializations |
26965 | which can produce TARGET, a specialization of some class or variable |
26966 | template. The value returned is a TEMPLATE_INFO; the TI_TEMPLATE is a |
26967 | TEMPLATE_DECL node corresponding to the partial specialization, while |
26968 | the TI_ARGS is the set of template arguments that must be substituted |
26969 | into the template pattern in order to generate TARGET. The result is |
26970 | cached in the TI_PARTIAL_INFO of the corresponding TEMPLATE_INFO unless |
26971 | RECHECKING is true. |
26972 | |
26973 | If the choice of partial specialization is ambiguous, a diagnostic |
26974 | is issued, and the error_mark_node is returned. If there are no |
26975 | partial specializations matching TARGET, then NULL_TREE is |
26976 | returned, indicating that the primary template should be used. */ |
26977 | |
26978 | tree |
26979 | most_specialized_partial_spec (tree target, tsubst_flags_t complain, |
26980 | bool rechecking /* = false */) |
26981 | { |
26982 | tree tinfo = NULL_TREE; |
26983 | tree tmpl, args, decl; |
26984 | if (TYPE_P (target)) |
26985 | { |
26986 | tinfo = CLASSTYPE_TEMPLATE_INFO (target); |
26987 | tmpl = TI_TEMPLATE (tinfo); |
26988 | args = TI_ARGS (tinfo); |
26989 | decl = TYPE_NAME (target); |
26990 | } |
26991 | else if (TREE_CODE (target) == TEMPLATE_ID_EXPR) |
26992 | { |
26993 | tmpl = TREE_OPERAND (target, 0); |
26994 | args = TREE_OPERAND (target, 1); |
26995 | decl = DECL_TEMPLATE_RESULT (tmpl); |
26996 | } |
26997 | else if (VAR_P (target)) |
26998 | { |
26999 | tinfo = DECL_TEMPLATE_INFO (target); |
27000 | tmpl = TI_TEMPLATE (tinfo); |
27001 | args = TI_ARGS (tinfo); |
27002 | decl = target; |
27003 | } |
27004 | else |
27005 | gcc_unreachable (); |
27006 | |
27007 | if (!PRIMARY_TEMPLATE_P (tmpl)) |
27008 | return NULL_TREE; |
27009 | |
27010 | if (!rechecking |
27011 | && tinfo |
27012 | && (VAR_P (target) || COMPLETE_TYPE_P (target))) |
27013 | return TI_PARTIAL_INFO (tinfo); |
27014 | |
27015 | tree main_tmpl = most_general_template (decl: tmpl); |
27016 | tree specs = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); |
27017 | if (!specs) |
27018 | /* There are no partial specializations of this template. */ |
27019 | return NULL_TREE; |
27020 | |
27021 | push_access_scope_guard pas (decl); |
27022 | deferring_access_check_sentinel acs (dk_no_deferred); |
27023 | |
27024 | /* For determining which partial specialization to use, only the |
27025 | innermost args are interesting. */ |
27026 | tree outer_args = NULL_TREE; |
27027 | if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)) |
27028 | { |
27029 | outer_args = strip_innermost_template_args (args, extra_levels: 1); |
27030 | args = INNERMOST_TEMPLATE_ARGS (args); |
27031 | } |
27032 | |
27033 | /* The caller hasn't called push_to_top_level yet, but we need |
27034 | get_partial_spec_bindings to be done in non-template context so that we'll |
27035 | fully resolve everything. */ |
27036 | processing_template_decl_sentinel ptds; |
27037 | |
27038 | tree list = NULL_TREE; |
27039 | for (tree t = specs; t; t = TREE_CHAIN (t)) |
27040 | { |
27041 | const tree ospec_tmpl = TREE_VALUE (t); |
27042 | |
27043 | tree spec_tmpl; |
27044 | if (outer_args) |
27045 | { |
27046 | /* Substitute in the template args from the enclosing class. */ |
27047 | ++processing_template_decl; |
27048 | spec_tmpl = tsubst (t: ospec_tmpl, args: outer_args, complain: tf_none, NULL_TREE); |
27049 | --processing_template_decl; |
27050 | if (spec_tmpl == error_mark_node) |
27051 | return error_mark_node; |
27052 | } |
27053 | else |
27054 | spec_tmpl = ospec_tmpl; |
27055 | |
27056 | tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args); |
27057 | if (spec_args) |
27058 | { |
27059 | if (outer_args) |
27060 | spec_args = add_to_template_args (args: outer_args, extra_args: spec_args); |
27061 | |
27062 | /* Keep the candidate only if its constraints are satisfied. */ |
27063 | if (constraints_satisfied_p (ospec_tmpl, spec_args)) |
27064 | list = tree_cons (spec_args, ospec_tmpl, list); |
27065 | } |
27066 | } |
27067 | |
27068 | if (! list) |
27069 | return NULL_TREE; |
27070 | |
27071 | tree champ = list; |
27072 | bool ambiguous_p = false; |
27073 | for (tree t = TREE_CHAIN (list); t; t = TREE_CHAIN (t)) |
27074 | { |
27075 | int fate = more_specialized_partial_spec (tmpl, pat1: champ, pat2: t); |
27076 | if (fate == 1) |
27077 | ; |
27078 | else |
27079 | { |
27080 | if (fate == 0) |
27081 | { |
27082 | t = TREE_CHAIN (t); |
27083 | if (! t) |
27084 | { |
27085 | ambiguous_p = true; |
27086 | break; |
27087 | } |
27088 | } |
27089 | champ = t; |
27090 | } |
27091 | } |
27092 | |
27093 | if (!ambiguous_p) |
27094 | for (tree t = list; t && t != champ; t = TREE_CHAIN (t)) |
27095 | { |
27096 | int fate = more_specialized_partial_spec (tmpl, pat1: champ, pat2: t); |
27097 | if (fate != 1) |
27098 | { |
27099 | ambiguous_p = true; |
27100 | break; |
27101 | } |
27102 | } |
27103 | |
27104 | if (ambiguous_p) |
27105 | { |
27106 | const char *str; |
27107 | char *spaces = NULL; |
27108 | if (!(complain & tf_error)) |
27109 | return error_mark_node; |
27110 | auto_diagnostic_group d; |
27111 | if (TYPE_P (target)) |
27112 | error ("ambiguous template instantiation for %q#T", target); |
27113 | else |
27114 | error ("ambiguous template instantiation for %q#D", target); |
27115 | str = ngettext (msgid1: "candidate is:", msgid2: "candidates are:", n: list_length (list)); |
27116 | for (tree t = list; t; t = TREE_CHAIN (t)) |
27117 | { |
27118 | tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t)); |
27119 | inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)), |
27120 | "%s %#qS", spaces ? spaces : str, subst); |
27121 | spaces = spaces ? spaces : get_spaces (str); |
27122 | } |
27123 | free (ptr: spaces); |
27124 | return error_mark_node; |
27125 | } |
27126 | |
27127 | tree result = build_template_info (TREE_VALUE (champ), TREE_PURPOSE (champ)); |
27128 | if (!rechecking && tinfo) |
27129 | TI_PARTIAL_INFO (tinfo) = result; |
27130 | return result; |
27131 | } |
27132 | |
27133 | /* Explicitly instantiate DECL. */ |
27134 | |
27135 | void |
27136 | do_decl_instantiation (tree decl, tree storage) |
27137 | { |
27138 | tree result = NULL_TREE; |
27139 | int extern_p = 0; |
27140 | |
27141 | if (!decl || decl == error_mark_node) |
27142 | /* An error occurred, for which grokdeclarator has already issued |
27143 | an appropriate message. */ |
27144 | return; |
27145 | else if (! DECL_LANG_SPECIFIC (decl)) |
27146 | { |
27147 | error ("explicit instantiation of non-template %q#D", decl); |
27148 | return; |
27149 | } |
27150 | |
27151 | bool var_templ = (DECL_TEMPLATE_INFO (decl) |
27152 | && variable_template_p (DECL_TI_TEMPLATE (decl))); |
27153 | |
27154 | if (VAR_P (decl) && !var_templ) |
27155 | { |
27156 | /* There is an asymmetry here in the way VAR_DECLs and |
27157 | FUNCTION_DECLs are handled by grokdeclarator. In the case of |
27158 | the latter, the DECL we get back will be marked as a |
27159 | template instantiation, and the appropriate |
27160 | DECL_TEMPLATE_INFO will be set up. This does not happen for |
27161 | VAR_DECLs so we do the lookup here. Probably, grokdeclarator |
27162 | should handle VAR_DECLs as it currently handles |
27163 | FUNCTION_DECLs. */ |
27164 | if (!DECL_CLASS_SCOPE_P (decl)) |
27165 | { |
27166 | error ("%qD is not a static data member of a class template", decl); |
27167 | return; |
27168 | } |
27169 | result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false); |
27170 | if (!result || !VAR_P (result)) |
27171 | { |
27172 | error ("no matching template for %qD found", decl); |
27173 | return; |
27174 | } |
27175 | if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl))) |
27176 | { |
27177 | error ("type %qT for explicit instantiation %qD does not match " |
27178 | "declared type %qT", TREE_TYPE (result), decl, |
27179 | TREE_TYPE (decl)); |
27180 | return; |
27181 | } |
27182 | } |
27183 | else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ) |
27184 | { |
27185 | error ("explicit instantiation of %q#D", decl); |
27186 | return; |
27187 | } |
27188 | else |
27189 | result = decl; |
27190 | |
27191 | /* Check for various error cases. Note that if the explicit |
27192 | instantiation is valid the RESULT will currently be marked as an |
27193 | *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set |
27194 | until we get here. */ |
27195 | |
27196 | if (DECL_TEMPLATE_SPECIALIZATION (result)) |
27197 | { |
27198 | /* DR 259 [temp.spec]. |
27199 | |
27200 | Both an explicit instantiation and a declaration of an explicit |
27201 | specialization shall not appear in a program unless the explicit |
27202 | instantiation follows a declaration of the explicit specialization. |
27203 | |
27204 | For a given set of template parameters, if an explicit |
27205 | instantiation of a template appears after a declaration of an |
27206 | explicit specialization for that template, the explicit |
27207 | instantiation has no effect. */ |
27208 | return; |
27209 | } |
27210 | else if (DECL_EXPLICIT_INSTANTIATION (result)) |
27211 | { |
27212 | /* [temp.spec] |
27213 | |
27214 | No program shall explicitly instantiate any template more |
27215 | than once. |
27216 | |
27217 | We check DECL_NOT_REALLY_EXTERN so as not to complain when |
27218 | the first instantiation was `extern' and the second is not, |
27219 | and EXTERN_P for the opposite case. */ |
27220 | if (DECL_NOT_REALLY_EXTERN (result) && !extern_p) |
27221 | permerror (input_location, "duplicate explicit instantiation of %q#D", result); |
27222 | /* If an "extern" explicit instantiation follows an ordinary |
27223 | explicit instantiation, the template is instantiated. */ |
27224 | if (extern_p) |
27225 | return; |
27226 | } |
27227 | else if (!DECL_IMPLICIT_INSTANTIATION (result)) |
27228 | { |
27229 | error ("no matching template for %qD found", result); |
27230 | return; |
27231 | } |
27232 | else if (!DECL_TEMPLATE_INFO (result)) |
27233 | { |
27234 | permerror (input_location, "explicit instantiation of non-template %q#D", result); |
27235 | return; |
27236 | } |
27237 | |
27238 | if (storage == NULL_TREE) |
27239 | ; |
27240 | else if (storage == ridpointers[(int) RID_EXTERN]) |
27241 | { |
27242 | if (cxx_dialect == cxx98 && pedantic) |
27243 | pedwarn (input_location, OPT_Wc__11_extensions, |
27244 | "ISO C++ 1998 forbids the use of %<extern%> on explicit " |
27245 | "instantiations"); |
27246 | extern_p = 1; |
27247 | } |
27248 | else |
27249 | error ("storage class %qD applied to template instantiation", storage); |
27250 | |
27251 | check_explicit_instantiation_namespace (spec: result); |
27252 | mark_decl_instantiated (result, extern_p); |
27253 | if (! extern_p) |
27254 | instantiate_decl (result, /*defer_ok=*/true, |
27255 | /*expl_inst_class_mem_p=*/false); |
27256 | } |
27257 | |
27258 | static void |
27259 | mark_class_instantiated (tree t, int extern_p) |
27260 | { |
27261 | SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t); |
27262 | SET_CLASSTYPE_INTERFACE_KNOWN (t); |
27263 | CLASSTYPE_INTERFACE_ONLY (t) = extern_p; |
27264 | TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p; |
27265 | if (! extern_p) |
27266 | { |
27267 | CLASSTYPE_DEBUG_REQUESTED (t) = 1; |
27268 | rest_of_type_compilation (t, 1); |
27269 | } |
27270 | } |
27271 | |
27272 | /* Perform an explicit instantiation of template class T. STORAGE, if |
27273 | non-null, is the RID for extern, inline or static. COMPLAIN is |
27274 | nonzero if this is called from the parser, zero if called recursively, |
27275 | since the standard is unclear (as detailed below). */ |
27276 | |
27277 | void |
27278 | do_type_instantiation (tree t, tree storage, tsubst_flags_t complain) |
27279 | { |
27280 | if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t))) |
27281 | { |
27282 | if (tree ti = TYPE_TEMPLATE_INFO (t)) |
27283 | error ("explicit instantiation of non-class template %qD", |
27284 | TI_TEMPLATE (ti)); |
27285 | else |
27286 | error ("explicit instantiation of non-template type %qT", t); |
27287 | return; |
27288 | } |
27289 | |
27290 | complete_type (t); |
27291 | |
27292 | if (!COMPLETE_TYPE_P (t)) |
27293 | { |
27294 | if (complain & tf_error) |
27295 | error ("explicit instantiation of %q#T before definition of template", |
27296 | t); |
27297 | return; |
27298 | } |
27299 | |
27300 | /* At most one of these will be true. */ |
27301 | bool extern_p = false; |
27302 | bool nomem_p = false; |
27303 | bool static_p = false; |
27304 | |
27305 | if (storage != NULL_TREE) |
27306 | { |
27307 | if (storage == ridpointers[(int) RID_EXTERN]) |
27308 | { |
27309 | if (cxx_dialect == cxx98 && pedantic) |
27310 | pedwarn (input_location, OPT_Wc__11_extensions, |
27311 | "ISO C++ 1998 forbids the use of %<extern%> on " |
27312 | "explicit instantiations"); |
27313 | } |
27314 | else |
27315 | pedwarn (input_location, OPT_Wpedantic, |
27316 | "ISO C++ forbids the use of %qE" |
27317 | " on explicit instantiations", storage); |
27318 | |
27319 | if (storage == ridpointers[(int) RID_INLINE]) |
27320 | nomem_p = true; |
27321 | else if (storage == ridpointers[(int) RID_EXTERN]) |
27322 | extern_p = true; |
27323 | else if (storage == ridpointers[(int) RID_STATIC]) |
27324 | static_p = true; |
27325 | else |
27326 | error ("storage class %qD applied to template instantiation", |
27327 | storage); |
27328 | } |
27329 | |
27330 | if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) |
27331 | /* DR 259 [temp.spec]. |
27332 | |
27333 | Both an explicit instantiation and a declaration of an explicit |
27334 | specialization shall not appear in a program unless the |
27335 | explicit instantiation follows a declaration of the explicit |
27336 | specialization. |
27337 | |
27338 | For a given set of template parameters, if an explicit |
27339 | instantiation of a template appears after a declaration of an |
27340 | explicit specialization for that template, the explicit |
27341 | instantiation has no effect. */ |
27342 | return; |
27343 | |
27344 | if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t)) |
27345 | { |
27346 | /* We've already instantiated the template. */ |
27347 | |
27348 | /* [temp.spec] |
27349 | |
27350 | No program shall explicitly instantiate any template more |
27351 | than once. |
27352 | |
27353 | If EXTERN_P then this is ok. */ |
27354 | if (!extern_p && (complain & tf_error)) |
27355 | permerror (input_location, |
27356 | "duplicate explicit instantiation of %q#T", t); |
27357 | |
27358 | return; |
27359 | } |
27360 | |
27361 | check_explicit_instantiation_namespace (TYPE_NAME (t)); |
27362 | mark_class_instantiated (t, extern_p); |
27363 | |
27364 | if (nomem_p) |
27365 | return; |
27366 | |
27367 | /* In contrast to implicit instantiation, where only the |
27368 | declarations, and not the definitions, of members are |
27369 | instantiated, we have here: |
27370 | |
27371 | [temp.explicit] |
27372 | |
27373 | An explicit instantiation that names a class template |
27374 | specialization is also an explicit instantiation of the same |
27375 | kind (declaration or definition) of each of its members (not |
27376 | including members inherited from base classes and members |
27377 | that are templates) that has not been previously explicitly |
27378 | specialized in the translation unit containing the explicit |
27379 | instantiation, provided that the associated constraints, if |
27380 | any, of that member are satisfied by the template arguments |
27381 | of the explicit instantiation. */ |
27382 | for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld)) |
27383 | if ((VAR_P (fld) |
27384 | || (TREE_CODE (fld) == FUNCTION_DECL |
27385 | && !static_p |
27386 | && user_provided_p (fld))) |
27387 | && DECL_TEMPLATE_INSTANTIATION (fld) |
27388 | && constraints_satisfied_p (fld)) |
27389 | { |
27390 | mark_decl_instantiated (result: fld, extern_p); |
27391 | if (! extern_p) |
27392 | instantiate_decl (fld, /*defer_ok=*/true, |
27393 | /*expl_inst_class_mem_p=*/true); |
27394 | } |
27395 | else if (DECL_IMPLICIT_TYPEDEF_P (fld)) |
27396 | { |
27397 | tree type = TREE_TYPE (fld); |
27398 | |
27399 | if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
27400 | && !uses_template_parms (CLASSTYPE_TI_ARGS (type))) |
27401 | do_type_instantiation (t: type, storage, complain: 0); |
27402 | } |
27403 | } |
27404 | |
27405 | /* Given a function DECL, which is a specialization of TMPL, modify |
27406 | DECL to be a re-instantiation of TMPL with the same template |
27407 | arguments. TMPL should be the template into which tsubst'ing |
27408 | should occur for DECL, not the most general template. |
27409 | |
27410 | One reason for doing this is a scenario like this: |
27411 | |
27412 | template <class T> |
27413 | void f(const T&, int i); |
27414 | |
27415 | void g() { f(3, 7); } |
27416 | |
27417 | template <class T> |
27418 | void f(const T& t, const int i) { } |
27419 | |
27420 | Note that when the template is first instantiated, with |
27421 | instantiate_template, the resulting DECL will have no name for the |
27422 | first parameter, and the wrong type for the second. So, when we go |
27423 | to instantiate the DECL, we regenerate it. */ |
27424 | |
27425 | static void |
27426 | regenerate_decl_from_template (tree decl, tree tmpl, tree args) |
27427 | { |
27428 | /* The arguments used to instantiate DECL, from the most general |
27429 | template. */ |
27430 | tree code_pattern = DECL_TEMPLATE_RESULT (tmpl); |
27431 | |
27432 | /* Make sure that we can see identifiers, and compute access correctly. */ |
27433 | push_access_scope (t: decl); |
27434 | |
27435 | if (TREE_CODE (decl) == FUNCTION_DECL) |
27436 | { |
27437 | tree specs; |
27438 | int args_depth; |
27439 | int parms_depth; |
27440 | |
27441 | /* Don't bother with this for unique friends that can't be redeclared and |
27442 | might change type if regenerated (PR69836). */ |
27443 | if (DECL_UNIQUE_FRIEND_P (decl)) |
27444 | goto done; |
27445 | |
27446 | /* A template with a lambda in the signature also changes type if |
27447 | regenerated (PR119401). */ |
27448 | walk_tree_fn find_lambda |
27449 | = [](tree *tp, int *, void *) |
27450 | { |
27451 | if (TREE_CODE (*tp) == LAMBDA_EXPR) |
27452 | return *tp; |
27453 | return NULL_TREE; |
27454 | }; |
27455 | if (cp_walk_tree_without_duplicates |
27456 | (&TREE_TYPE (tmpl), find_lambda, nullptr)) |
27457 | goto done; |
27458 | |
27459 | /* Use the source location of the definition. */ |
27460 | DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl); |
27461 | |
27462 | args_depth = TMPL_ARGS_DEPTH (args); |
27463 | parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
27464 | if (args_depth > parms_depth) |
27465 | args = get_innermost_template_args (args, n: parms_depth); |
27466 | |
27467 | /* Instantiate a dynamic exception-specification. noexcept will be |
27468 | handled below. */ |
27469 | if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern))) |
27470 | if (TREE_VALUE (raises)) |
27471 | { |
27472 | specs = tsubst_exception_specification (TREE_TYPE (code_pattern), |
27473 | args, complain: tf_error, NULL_TREE, |
27474 | /*defer_ok*/false); |
27475 | if (specs && specs != error_mark_node) |
27476 | TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), |
27477 | specs); |
27478 | } |
27479 | |
27480 | /* Merge parameter declarations. */ |
27481 | if (tree pattern_parm |
27482 | = skip_artificial_parms_for (code_pattern, |
27483 | DECL_ARGUMENTS (code_pattern))) |
27484 | { |
27485 | tree *p = &DECL_ARGUMENTS (decl); |
27486 | for (int skip = num_artificial_parms_for (decl); skip; --skip) |
27487 | p = &DECL_CHAIN (*p); |
27488 | *p = tsubst_decl (t: pattern_parm, args, complain: tf_error); |
27489 | for (tree t = *p; t; t = DECL_CHAIN (t)) |
27490 | DECL_CONTEXT (t) = decl; |
27491 | } |
27492 | |
27493 | if (DECL_CONTRACTS (decl)) |
27494 | { |
27495 | /* If we're regenerating a specialization, the contracts will have |
27496 | been copied from the most general template. Replace those with |
27497 | the ones from the actual specialization. */ |
27498 | tree tmpl = DECL_TI_TEMPLATE (decl); |
27499 | if (DECL_TEMPLATE_SPECIALIZATION (tmpl)) |
27500 | { |
27501 | remove_contract_attributes (decl); |
27502 | copy_contract_attributes (decl, code_pattern); |
27503 | } |
27504 | |
27505 | tsubst_contract_attributes (decl, args, complain: tf_warning_or_error, in_decl: code_pattern); |
27506 | } |
27507 | |
27508 | /* Merge additional specifiers from the CODE_PATTERN. */ |
27509 | if (DECL_DECLARED_INLINE_P (code_pattern) |
27510 | && !DECL_DECLARED_INLINE_P (decl)) |
27511 | DECL_DECLARED_INLINE_P (decl) = 1; |
27512 | |
27513 | maybe_instantiate_noexcept (decl, tf_error); |
27514 | } |
27515 | else if (VAR_P (decl)) |
27516 | { |
27517 | start_lambda_scope (decl); |
27518 | DECL_INITIAL (decl) = |
27519 | tsubst_init (DECL_INITIAL (code_pattern), decl, args, |
27520 | complain: tf_error, DECL_TI_TEMPLATE (decl)); |
27521 | finish_lambda_scope (); |
27522 | if (VAR_HAD_UNKNOWN_BOUND (decl)) |
27523 | TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args, |
27524 | complain: tf_error, DECL_TI_TEMPLATE (decl)); |
27525 | } |
27526 | else |
27527 | gcc_unreachable (); |
27528 | |
27529 | done: |
27530 | pop_access_scope (t: decl); |
27531 | } |
27532 | |
27533 | /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be |
27534 | substituted to get DECL. */ |
27535 | |
27536 | tree |
27537 | template_for_substitution (tree decl) |
27538 | { |
27539 | tree tmpl = DECL_TI_TEMPLATE (decl); |
27540 | |
27541 | /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern |
27542 | for the instantiation. This is not always the most general |
27543 | template. Consider, for example: |
27544 | |
27545 | template <class T> |
27546 | struct S { template <class U> void f(); |
27547 | template <> void f<int>(); }; |
27548 | |
27549 | and an instantiation of S<double>::f<int>. We want TD to be the |
27550 | specialization S<T>::f<int>, not the more general S<T>::f<U>. */ |
27551 | while (/* An instantiation cannot have a definition, so we need a |
27552 | more general template. */ |
27553 | DECL_TEMPLATE_INSTANTIATION (tmpl) |
27554 | /* We must also deal with friend templates. Given: |
27555 | |
27556 | template <class T> struct S { |
27557 | template <class U> friend void f() {}; |
27558 | }; |
27559 | |
27560 | S<int>::f<U> say, is not an instantiation of S<T>::f<U>, |
27561 | so far as the language is concerned, but that's still |
27562 | where we get the pattern for the instantiation from. On |
27563 | other hand, if the definition comes outside the class, say: |
27564 | |
27565 | template <class T> struct S { |
27566 | template <class U> friend void f(); |
27567 | }; |
27568 | template <class U> friend void f() {} |
27569 | |
27570 | we don't need to look any further. That's what the check for |
27571 | DECL_INITIAL is for. */ |
27572 | || (TREE_CODE (decl) == FUNCTION_DECL |
27573 | && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl) |
27574 | && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)))) |
27575 | { |
27576 | /* The present template, TD, should not be a definition. If it |
27577 | were a definition, we should be using it! Note that we |
27578 | cannot restructure the loop to just keep going until we find |
27579 | a template with a definition, since that might go too far if |
27580 | a specialization was declared, but not defined. */ |
27581 | |
27582 | /* Fetch the more general template. */ |
27583 | tmpl = DECL_TI_TEMPLATE (tmpl); |
27584 | } |
27585 | |
27586 | return tmpl; |
27587 | } |
27588 | |
27589 | /* Returns true if we need to instantiate this template instance even if we |
27590 | know we aren't going to emit it. */ |
27591 | |
27592 | bool |
27593 | always_instantiate_p (tree decl) |
27594 | { |
27595 | /* We always instantiate inline functions so that we can inline them. An |
27596 | explicit instantiation declaration prohibits implicit instantiation of |
27597 | non-inline functions. With high levels of optimization, we would |
27598 | normally inline non-inline functions -- but we're not allowed to do |
27599 | that for "extern template" functions. Therefore, we check |
27600 | DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */ |
27601 | return ((TREE_CODE (decl) == FUNCTION_DECL |
27602 | && (DECL_DECLARED_INLINE_P (decl) |
27603 | || type_uses_auto (TREE_TYPE (TREE_TYPE (decl))))) |
27604 | /* And we need to instantiate static data members so that |
27605 | their initializers are available in integral constant |
27606 | expressions. */ |
27607 | || (VAR_P (decl) |
27608 | && decl_maybe_constant_var_p (decl))); |
27609 | } |
27610 | |
27611 | /* If FN has a noexcept-specifier that hasn't been instantiated yet, |
27612 | instantiate it now, modifying TREE_TYPE (fn). Returns false on |
27613 | error, true otherwise. */ |
27614 | |
27615 | bool |
27616 | maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain) |
27617 | { |
27618 | if (fn == error_mark_node) |
27619 | return false; |
27620 | |
27621 | /* Don't instantiate a noexcept-specification from template context. */ |
27622 | if (processing_template_decl |
27623 | && (!flag_noexcept_type || type_dependent_expression_p (fn))) |
27624 | return true; |
27625 | |
27626 | tree fntype = TREE_TYPE (fn); |
27627 | tree spec = TYPE_RAISES_EXCEPTIONS (fntype); |
27628 | |
27629 | if ((!spec || UNEVALUATED_NOEXCEPT_SPEC_P (spec)) |
27630 | && DECL_MAYBE_DELETED (fn)) |
27631 | { |
27632 | if (fn == current_function_decl) |
27633 | /* We're in start_preparsed_function, keep going. */ |
27634 | return true; |
27635 | |
27636 | ++function_depth; |
27637 | maybe_synthesize_method (fn); |
27638 | --function_depth; |
27639 | return !DECL_DELETED_FN (fn); |
27640 | } |
27641 | |
27642 | if (!spec || !TREE_PURPOSE (spec)) |
27643 | return true; |
27644 | |
27645 | tree noex = TREE_PURPOSE (spec); |
27646 | if (TREE_CODE (noex) != DEFERRED_NOEXCEPT |
27647 | && TREE_CODE (noex) != DEFERRED_PARSE) |
27648 | return true; |
27649 | |
27650 | tree orig_fn = NULL_TREE; |
27651 | /* For a member friend template we can get a TEMPLATE_DECL. Let's use |
27652 | its FUNCTION_DECL for the rest of this function -- push_access_scope |
27653 | doesn't accept TEMPLATE_DECLs. */ |
27654 | if (DECL_FUNCTION_TEMPLATE_P (fn)) |
27655 | { |
27656 | orig_fn = fn; |
27657 | fn = DECL_TEMPLATE_RESULT (fn); |
27658 | } |
27659 | |
27660 | if (DECL_CLONED_FUNCTION_P (fn)) |
27661 | { |
27662 | tree prime = DECL_CLONED_FUNCTION (fn); |
27663 | if (!maybe_instantiate_noexcept (fn: prime, complain)) |
27664 | return false; |
27665 | spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime)); |
27666 | } |
27667 | else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT) |
27668 | { |
27669 | static hash_set<tree>* fns = new hash_set<tree>; |
27670 | bool added = false; |
27671 | tree pattern = DEFERRED_NOEXCEPT_PATTERN (noex); |
27672 | if (pattern == NULL_TREE) |
27673 | { |
27674 | spec = get_defaulted_eh_spec (fn, complain); |
27675 | if (spec == error_mark_node) |
27676 | /* This might have failed because of an unparsed DMI, so |
27677 | let's try again later. */ |
27678 | return false; |
27679 | } |
27680 | else if (!(added = !fns->add (k: fn))) |
27681 | { |
27682 | /* If hash_set::add returns true, the element was already there. */ |
27683 | location_t loc = cp_expr_loc_or_loc (t: pattern, |
27684 | DECL_SOURCE_LOCATION (fn)); |
27685 | error_at (loc, |
27686 | "exception specification of %qD depends on itself", |
27687 | fn); |
27688 | spec = noexcept_false_spec; |
27689 | } |
27690 | else if (TREE_CODE (pattern) == DEFERRED_PARSE) |
27691 | { |
27692 | error ("exception specification of %qD is not available " |
27693 | "until end of class definition", fn); |
27694 | spec = noexcept_false_spec; |
27695 | } |
27696 | else if (push_tinst_level (d: fn)) |
27697 | { |
27698 | const bool push_to_top = maybe_push_to_top_level (fn); |
27699 | push_access_scope (t: fn); |
27700 | push_deferring_access_checks (dk_no_deferred); |
27701 | input_location = DECL_SOURCE_LOCATION (fn); |
27702 | |
27703 | if (DECL_IOBJ_MEMBER_FUNCTION_P (fn) |
27704 | && !DECL_LOCAL_DECL_P (fn)) |
27705 | { |
27706 | /* If needed, set current_class_ptr for the benefit of |
27707 | tsubst_copy/PARM_DECL. */ |
27708 | tree this_parm = DECL_ARGUMENTS (fn); |
27709 | current_class_ptr = NULL_TREE; |
27710 | current_class_ref = cp_build_fold_indirect_ref (this_parm); |
27711 | current_class_ptr = this_parm; |
27712 | } |
27713 | |
27714 | /* If this function is represented by a TEMPLATE_DECL, then |
27715 | the deferred noexcept-specification might still contain |
27716 | dependent types, even after substitution. And we need the |
27717 | dependency check functions to work in build_noexcept_spec. */ |
27718 | if (orig_fn) |
27719 | ++processing_template_decl; |
27720 | |
27721 | /* Do deferred instantiation of the noexcept-specifier. */ |
27722 | noex = tsubst_expr (t: pattern, DEFERRED_NOEXCEPT_ARGS (noex), |
27723 | complain: tf_warning_or_error, in_decl: fn); |
27724 | /* Build up the noexcept-specification. */ |
27725 | spec = build_noexcept_spec (noex, tf_warning_or_error); |
27726 | |
27727 | if (orig_fn) |
27728 | --processing_template_decl; |
27729 | |
27730 | pop_deferring_access_checks (); |
27731 | pop_access_scope (t: fn); |
27732 | pop_tinst_level (); |
27733 | maybe_pop_from_top_level (push_to_top); |
27734 | } |
27735 | else |
27736 | spec = noexcept_false_spec; |
27737 | |
27738 | if (added) |
27739 | fns->remove (k: fn); |
27740 | } |
27741 | |
27742 | if (spec == error_mark_node) |
27743 | { |
27744 | /* This failed with a hard error, so let's go with false. */ |
27745 | gcc_assert (seen_error ()); |
27746 | spec = noexcept_false_spec; |
27747 | } |
27748 | |
27749 | TREE_TYPE (fn) = build_exception_variant (fntype, spec); |
27750 | if (orig_fn) |
27751 | TREE_TYPE (orig_fn) = TREE_TYPE (fn); |
27752 | |
27753 | return true; |
27754 | } |
27755 | |
27756 | /* We're starting to process the function INST, an instantiation of PATTERN; |
27757 | add their parameters to local_specializations. */ |
27758 | |
27759 | void |
27760 | register_parameter_specializations (tree pattern, tree inst) |
27761 | { |
27762 | tree tmpl_parm = DECL_ARGUMENTS (pattern); |
27763 | tree spec_parm = DECL_ARGUMENTS (inst); |
27764 | if (DECL_IOBJ_MEMBER_FUNCTION_P (inst)) |
27765 | { |
27766 | register_local_specialization (spec: spec_parm, tmpl: tmpl_parm); |
27767 | spec_parm = skip_artificial_parms_for (inst, spec_parm); |
27768 | tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm); |
27769 | } |
27770 | for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm)) |
27771 | { |
27772 | if (!DECL_PACK_P (tmpl_parm)) |
27773 | { |
27774 | register_local_specialization (spec: spec_parm, tmpl: tmpl_parm); |
27775 | spec_parm = DECL_CHAIN (spec_parm); |
27776 | } |
27777 | else |
27778 | { |
27779 | /* Register the (value) argument pack as a specialization of |
27780 | TMPL_PARM, then move on. */ |
27781 | tree argpack = extract_fnparm_pack (tmpl_parm, spec_p: &spec_parm); |
27782 | register_local_specialization (spec: argpack, tmpl: tmpl_parm); |
27783 | } |
27784 | } |
27785 | gcc_assert (!spec_parm); |
27786 | } |
27787 | |
27788 | /* Instantiate the body of D using PATTERN with ARGS. We have |
27789 | already determined PATTERN is the correct template to use. |
27790 | NESTED_P is true if this is a nested function, in which case |
27791 | PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */ |
27792 | |
27793 | static void |
27794 | instantiate_body (tree pattern, tree args, tree d, bool nested_p) |
27795 | { |
27796 | tree td = NULL_TREE; |
27797 | tree code_pattern = pattern; |
27798 | |
27799 | if (!nested_p) |
27800 | { |
27801 | td = pattern; |
27802 | code_pattern = DECL_TEMPLATE_RESULT (td); |
27803 | } |
27804 | else |
27805 | /* Only OMP reductions are nested. */ |
27806 | gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)); |
27807 | |
27808 | vec<tree> omp_privatization_save; |
27809 | if (current_function_decl) |
27810 | save_omp_privatization_clauses (omp_privatization_save); |
27811 | |
27812 | bool push_to_top = maybe_push_to_top_level (d); |
27813 | |
27814 | mark_template_arguments_used (tmpl: pattern, args); |
27815 | |
27816 | if (VAR_P (d)) |
27817 | { |
27818 | /* The variable might be a lambda's extra scope, and that |
27819 | lambda's visibility depends on D's. */ |
27820 | maybe_commonize_var (d); |
27821 | determine_visibility (d); |
27822 | } |
27823 | |
27824 | /* Mark D as instantiated so that recursive calls to |
27825 | instantiate_decl do not try to instantiate it again. */ |
27826 | DECL_TEMPLATE_INSTANTIATED (d) = 1; |
27827 | |
27828 | if (td) |
27829 | /* Regenerate the declaration in case the template has been modified |
27830 | by a subsequent redeclaration. */ |
27831 | regenerate_decl_from_template (decl: d, tmpl: td, args); |
27832 | |
27833 | /* We already set the file and line above. Reset them now in case |
27834 | they changed as a result of calling regenerate_decl_from_template. */ |
27835 | input_location = DECL_SOURCE_LOCATION (d); |
27836 | |
27837 | if (VAR_P (d)) |
27838 | { |
27839 | /* Clear out DECL_RTL; whatever was there before may not be right |
27840 | since we've reset the type of the declaration. */ |
27841 | SET_DECL_RTL (d, NULL); |
27842 | DECL_IN_AGGR_P (d) = 0; |
27843 | |
27844 | /* The initializer is placed in DECL_INITIAL by |
27845 | regenerate_decl_from_template so we don't need to |
27846 | push/pop_access_scope again here. Pull it out so that |
27847 | cp_finish_decl can process it. */ |
27848 | bool const_init = false; |
27849 | tree init = DECL_INITIAL (d); |
27850 | DECL_INITIAL (d) = NULL_TREE; |
27851 | DECL_INITIALIZED_P (d) = 0; |
27852 | |
27853 | /* Clear DECL_EXTERNAL so that cp_finish_decl will process the |
27854 | initializer. That function will defer actual emission until |
27855 | we have a chance to determine linkage. */ |
27856 | DECL_EXTERNAL (d) = 0; |
27857 | |
27858 | /* Enter the scope of D so that access-checking works correctly. */ |
27859 | bool enter_context = DECL_CLASS_SCOPE_P (d); |
27860 | if (enter_context) |
27861 | push_nested_class (DECL_CONTEXT (d)); |
27862 | |
27863 | const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern); |
27864 | cp_finish_decl (d, init, const_init, NULL_TREE, 0); |
27865 | |
27866 | if (enter_context) |
27867 | pop_nested_class (); |
27868 | } |
27869 | else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern)) |
27870 | synthesize_method (d); |
27871 | else if (TREE_CODE (d) == FUNCTION_DECL) |
27872 | { |
27873 | /* Set up the list of local specializations. */ |
27874 | local_specialization_stack lss (push_to_top ? lss_blank : lss_copy); |
27875 | tree block = NULL_TREE; |
27876 | |
27877 | /* Set up context. */ |
27878 | if (nested_p) |
27879 | block = push_stmt_list (); |
27880 | else |
27881 | { |
27882 | start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED); |
27883 | |
27884 | perform_instantiation_time_access_checks (tmpl: code_pattern, targs: args); |
27885 | } |
27886 | |
27887 | /* Create substitution entries for the parameters. */ |
27888 | register_parameter_specializations (pattern: code_pattern, inst: d); |
27889 | |
27890 | /* Substitute into the body of the function. */ |
27891 | if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)) |
27892 | tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args, |
27893 | complain: tf_warning_or_error, in_decl: d); |
27894 | else |
27895 | { |
27896 | tsubst_stmt (DECL_SAVED_TREE (code_pattern), args, |
27897 | complain: tf_warning_or_error, DECL_TI_TEMPLATE (d)); |
27898 | |
27899 | /* Set the current input_location to the end of the function |
27900 | so that finish_function knows where we are. */ |
27901 | input_location |
27902 | = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus; |
27903 | |
27904 | /* Remember if we saw an infinite loop in the template. */ |
27905 | current_function_infinite_loop |
27906 | = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop; |
27907 | } |
27908 | |
27909 | /* Finish the function. */ |
27910 | if (nested_p) |
27911 | DECL_SAVED_TREE (d) = pop_stmt_list (block); |
27912 | else |
27913 | { |
27914 | d = finish_function (/*inline_p=*/false); |
27915 | expand_or_defer_fn (d); |
27916 | } |
27917 | |
27918 | if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)) |
27919 | cp_check_omp_declare_reduction (d); |
27920 | |
27921 | if (int errs = errorcount + sorrycount) |
27922 | if (errs > current_tinst_level->errors) |
27923 | if (function *f = DECL_STRUCT_FUNCTION (d)) |
27924 | f->language->erroneous = true; |
27925 | } |
27926 | |
27927 | /* We're not deferring instantiation any more. */ |
27928 | if (!nested_p) |
27929 | TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0; |
27930 | |
27931 | maybe_pop_from_top_level (push_to_top); |
27932 | |
27933 | if (current_function_decl) |
27934 | restore_omp_privatization_clauses (omp_privatization_save); |
27935 | } |
27936 | |
27937 | /* Produce the definition of D, a _DECL generated from a template. If |
27938 | DEFER_OK is true, then we don't have to actually do the |
27939 | instantiation now; we just have to do it sometime. Normally it is |
27940 | an error if this is an explicit instantiation but D is undefined. |
27941 | EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly |
27942 | instantiated class template. */ |
27943 | |
27944 | tree |
27945 | instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p) |
27946 | { |
27947 | tree tmpl = DECL_TI_TEMPLATE (d); |
27948 | tree gen_args; |
27949 | tree args; |
27950 | tree td; |
27951 | tree code_pattern; |
27952 | tree spec; |
27953 | tree gen_tmpl; |
27954 | bool pattern_defined; |
27955 | location_t saved_loc = input_location; |
27956 | bool external_p; |
27957 | bool deleted_p; |
27958 | |
27959 | /* This function should only be used to instantiate templates for |
27960 | functions and static member variables. */ |
27961 | gcc_assert (VAR_OR_FUNCTION_DECL_P (d)); |
27962 | |
27963 | gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d)); |
27964 | |
27965 | if (modules_p ()) |
27966 | /* We may have a pending instantiation of D itself. */ |
27967 | lazy_load_pendings (decl: d); |
27968 | |
27969 | /* Variables are never deferred; if instantiation is required, they |
27970 | are instantiated right away. That allows for better code in the |
27971 | case that an expression refers to the value of the variable -- |
27972 | if the variable has a constant value the referring expression can |
27973 | take advantage of that fact. */ |
27974 | if (VAR_P (d)) |
27975 | defer_ok = false; |
27976 | |
27977 | /* Don't instantiate cloned functions. Instead, instantiate the |
27978 | functions they cloned. */ |
27979 | if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d)) |
27980 | d = DECL_CLONED_FUNCTION (d); |
27981 | |
27982 | if (DECL_TEMPLATE_INSTANTIATED (d) |
27983 | || TREE_TYPE (d) == error_mark_node |
27984 | || (TREE_CODE (d) == FUNCTION_DECL |
27985 | && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d)) |
27986 | || DECL_TEMPLATE_SPECIALIZATION (d)) |
27987 | /* D has already been instantiated or explicitly specialized, so |
27988 | there's nothing for us to do here. |
27989 | |
27990 | It might seem reasonable to check whether or not D is an explicit |
27991 | instantiation, and, if so, stop here. But when an explicit |
27992 | instantiation is deferred until the end of the compilation, |
27993 | DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do |
27994 | the instantiation. */ |
27995 | return d; |
27996 | |
27997 | /* Check to see whether we know that this template will be |
27998 | instantiated in some other file, as with "extern template" |
27999 | extension. */ |
28000 | external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d)); |
28001 | |
28002 | /* In general, we do not instantiate such templates. */ |
28003 | if (external_p && !always_instantiate_p (decl: d)) |
28004 | return d; |
28005 | |
28006 | gen_tmpl = most_general_template (decl: tmpl); |
28007 | gen_args = DECL_TI_ARGS (d); |
28008 | |
28009 | /* We should already have the extra args. */ |
28010 | gcc_checking_assert (tmpl == gen_tmpl |
28011 | || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) |
28012 | == TMPL_ARGS_DEPTH (gen_args))); |
28013 | /* And what's in the hash table should match D. */ |
28014 | gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) |
28015 | == d |
28016 | || spec == NULL_TREE); |
28017 | |
28018 | /* This needs to happen before any tsubsting. */ |
28019 | if (! push_tinst_level (d)) |
28020 | return d; |
28021 | |
28022 | auto_timevar tv (TV_TEMPLATE_INST); |
28023 | |
28024 | /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern |
28025 | for the instantiation. */ |
28026 | td = template_for_substitution (decl: d); |
28027 | args = gen_args; |
28028 | |
28029 | if (variable_template_specialization_p (t: d)) |
28030 | { |
28031 | /* Look up an explicit specialization, if any. */ |
28032 | tree partial_ti = most_specialized_partial_spec (target: d, complain: tf_warning_or_error); |
28033 | if (partial_ti && partial_ti != error_mark_node) |
28034 | { |
28035 | td = TI_TEMPLATE (partial_ti); |
28036 | args = TI_ARGS (partial_ti); |
28037 | } |
28038 | } |
28039 | |
28040 | maybe_diagnose_erroneous_template (t: td); |
28041 | |
28042 | code_pattern = DECL_TEMPLATE_RESULT (td); |
28043 | |
28044 | /* We should never be trying to instantiate a member of a class |
28045 | template or partial specialization. */ |
28046 | gcc_assert (d != code_pattern); |
28047 | |
28048 | if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d)) |
28049 | || DECL_TEMPLATE_SPECIALIZATION (td)) |
28050 | /* In the case of a friend template whose definition is provided |
28051 | outside the class, we may have too many arguments. Drop the |
28052 | ones we don't need. The same is true for specializations. */ |
28053 | args = get_innermost_template_args |
28054 | (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td))); |
28055 | |
28056 | if (TREE_CODE (d) == FUNCTION_DECL) |
28057 | { |
28058 | deleted_p = DECL_DELETED_FN (code_pattern); |
28059 | pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE |
28060 | && DECL_INITIAL (code_pattern) != error_mark_node) |
28061 | || DECL_DEFAULTED_FN (code_pattern) |
28062 | || deleted_p); |
28063 | } |
28064 | else |
28065 | { |
28066 | deleted_p = false; |
28067 | if (DECL_CLASS_SCOPE_P (code_pattern)) |
28068 | pattern_defined = ! DECL_IN_AGGR_P (code_pattern); |
28069 | else |
28070 | pattern_defined = ! DECL_EXTERNAL (code_pattern); |
28071 | } |
28072 | |
28073 | /* We may be in the middle of deferred access check. Disable it now. */ |
28074 | push_deferring_access_checks (dk_no_deferred); |
28075 | |
28076 | /* Unless an explicit instantiation directive has already determined |
28077 | the linkage of D, remember that a definition is available for |
28078 | this entity. */ |
28079 | if (pattern_defined |
28080 | && !DECL_INTERFACE_KNOWN (d) |
28081 | && !DECL_NOT_REALLY_EXTERN (d)) |
28082 | mark_definable (decl: d); |
28083 | |
28084 | DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern); |
28085 | DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern); |
28086 | input_location = DECL_SOURCE_LOCATION (d); |
28087 | |
28088 | /* If D is a member of an explicitly instantiated class template, |
28089 | and no definition is available, treat it like an implicit |
28090 | instantiation. */ |
28091 | if (!pattern_defined && expl_inst_class_mem_p |
28092 | && DECL_EXPLICIT_INSTANTIATION (d)) |
28093 | { |
28094 | /* Leave linkage flags alone on instantiations with anonymous |
28095 | visibility. */ |
28096 | if (TREE_PUBLIC (d)) |
28097 | { |
28098 | DECL_NOT_REALLY_EXTERN (d) = 0; |
28099 | DECL_INTERFACE_KNOWN (d) = 0; |
28100 | } |
28101 | SET_DECL_IMPLICIT_INSTANTIATION (d); |
28102 | } |
28103 | |
28104 | /* Defer all other templates, unless we have been explicitly |
28105 | forbidden from doing so. */ |
28106 | if (/* If there is no definition, we cannot instantiate the |
28107 | template. */ |
28108 | ! pattern_defined |
28109 | /* If it's OK to postpone instantiation, do so. */ |
28110 | || defer_ok |
28111 | /* If this is a static data member that will be defined |
28112 | elsewhere, we don't want to instantiate the entire data |
28113 | member, but we do want to instantiate the initializer so that |
28114 | we can substitute that elsewhere. */ |
28115 | || (external_p && VAR_P (d)) |
28116 | /* Handle here a deleted function too, avoid generating |
28117 | its body (c++/61080). */ |
28118 | || deleted_p |
28119 | /* We need the initializer for an OpenMP declare mapper. */ |
28120 | || (VAR_P (d) && DECL_LANG_SPECIFIC (d) && DECL_OMP_DECLARE_MAPPER_P (d))) |
28121 | { |
28122 | /* The definition of the static data member is now required so |
28123 | we must substitute the initializer. */ |
28124 | if (VAR_P (d) |
28125 | && !DECL_INITIAL (d) |
28126 | && DECL_INITIAL (code_pattern)) |
28127 | { |
28128 | tree ns; |
28129 | tree init; |
28130 | bool const_init = false; |
28131 | bool enter_context = DECL_CLASS_SCOPE_P (d); |
28132 | |
28133 | ns = decl_namespace_context (d); |
28134 | push_nested_namespace (ns); |
28135 | if (enter_context) |
28136 | push_nested_class (DECL_CONTEXT (d)); |
28137 | init = tsubst_expr (DECL_INITIAL (code_pattern), |
28138 | args, |
28139 | complain: tf_warning_or_error, NULL_TREE); |
28140 | /* If instantiating the initializer involved instantiating this |
28141 | again, don't call cp_finish_decl twice. */ |
28142 | if (!DECL_INITIAL (d)) |
28143 | { |
28144 | /* Make sure the initializer is still constant, in case of |
28145 | circular dependency (template/instantiate6.C). */ |
28146 | const_init |
28147 | = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern); |
28148 | cp_finish_decl (d, init, /*init_const_expr_p=*/const_init, |
28149 | /*asmspec_tree=*/NULL_TREE, 0); |
28150 | } |
28151 | if (enter_context) |
28152 | pop_nested_class (); |
28153 | pop_nested_namespace (ns); |
28154 | } |
28155 | |
28156 | /* We restore the source position here because it's used by |
28157 | add_pending_template. */ |
28158 | input_location = saved_loc; |
28159 | |
28160 | if (at_eof && !pattern_defined |
28161 | && DECL_EXPLICIT_INSTANTIATION (d) |
28162 | && DECL_NOT_REALLY_EXTERN (d)) |
28163 | /* [temp.explicit] |
28164 | |
28165 | The definition of a non-exported function template, a |
28166 | non-exported member function template, or a non-exported |
28167 | member function or static data member of a class template |
28168 | shall be present in every translation unit in which it is |
28169 | explicitly instantiated. */ |
28170 | permerror (input_location, "explicit instantiation of %qD " |
28171 | "but no definition available", d); |
28172 | |
28173 | /* If we're in unevaluated context, we just wanted to get the |
28174 | constant value; this isn't an odr use, so don't queue |
28175 | a full instantiation. */ |
28176 | if (!cp_unevaluated_operand |
28177 | /* ??? Historically, we have instantiated inline functions, even |
28178 | when marked as "extern template". */ |
28179 | && !(external_p && VAR_P (d))) |
28180 | add_pending_template (d); |
28181 | } |
28182 | else |
28183 | { |
28184 | set_instantiating_module (d); |
28185 | if (variable_template_p (t: gen_tmpl)) |
28186 | note_vague_linkage_variable (d); |
28187 | instantiate_body (pattern: td, args, d, nested_p: false); |
28188 | } |
28189 | |
28190 | pop_deferring_access_checks (); |
28191 | pop_tinst_level (); |
28192 | input_location = saved_loc; |
28193 | |
28194 | return d; |
28195 | } |
28196 | |
28197 | /* Run through the list of templates that we wish we could |
28198 | instantiate, and instantiate any we can. RETRIES is the |
28199 | number of times we retry pending template instantiation. */ |
28200 | |
28201 | void |
28202 | instantiate_pending_templates (int retries) |
28203 | { |
28204 | int reconsider; |
28205 | location_t saved_loc = input_location; |
28206 | unsigned saved_module_kind = module_kind; |
28207 | |
28208 | /* Instantiating templates may trigger vtable generation. This in turn |
28209 | may require further template instantiations. We place a limit here |
28210 | to avoid infinite loop. */ |
28211 | if (pending_templates && retries >= max_tinst_depth) |
28212 | { |
28213 | tree decl = pending_templates->tinst->maybe_get_node (); |
28214 | |
28215 | fatal_error (input_location, |
28216 | "template instantiation depth exceeds maximum of %d" |
28217 | " instantiating %q+D, possibly from virtual table generation" |
28218 | " (use %<-ftemplate-depth=%> to increase the maximum)", |
28219 | max_tinst_depth, decl); |
28220 | if (TREE_CODE (decl) == FUNCTION_DECL) |
28221 | /* Pretend that we defined it. */ |
28222 | DECL_INITIAL (decl) = error_mark_node; |
28223 | return; |
28224 | } |
28225 | |
28226 | do |
28227 | { |
28228 | struct pending_template **t = &pending_templates; |
28229 | struct pending_template *last = NULL; |
28230 | reconsider = 0; |
28231 | while (*t) |
28232 | { |
28233 | struct tinst_level *tinst = (*t)->tinst; |
28234 | bool complete = tinst_complete_p (level: tinst); |
28235 | |
28236 | if (!complete) |
28237 | { |
28238 | tree instantiation = reopen_tinst_level (level: tinst); |
28239 | |
28240 | if (limit_bad_template_recursion (decl: instantiation)) |
28241 | /* Do nothing. */; |
28242 | else if (TYPE_P (instantiation)) |
28243 | { |
28244 | instantiate_class_template (type: instantiation); |
28245 | if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation)) |
28246 | for (tree fld = TYPE_FIELDS (instantiation); |
28247 | fld; fld = TREE_CHAIN (fld)) |
28248 | if ((VAR_P (fld) |
28249 | || (TREE_CODE (fld) == FUNCTION_DECL |
28250 | && !DECL_ARTIFICIAL (fld))) |
28251 | && DECL_TEMPLATE_INSTANTIATION (fld)) |
28252 | instantiate_decl (d: fld, |
28253 | /*defer_ok=*/false, |
28254 | /*expl_inst_class_mem_p=*/false); |
28255 | |
28256 | if (COMPLETE_TYPE_P (instantiation)) |
28257 | reconsider = 1; |
28258 | } |
28259 | else |
28260 | { |
28261 | instantiation |
28262 | = instantiate_decl (d: instantiation, |
28263 | /*defer_ok=*/false, |
28264 | /*expl_inst_class_mem_p=*/false); |
28265 | if (DECL_TEMPLATE_INSTANTIATED (instantiation)) |
28266 | reconsider = 1; |
28267 | } |
28268 | |
28269 | complete = tinst_complete_p (level: tinst); |
28270 | |
28271 | tinst_depth = 0; |
28272 | set_refcount_ptr (ptr&: current_tinst_level); |
28273 | } |
28274 | |
28275 | if (complete) |
28276 | { |
28277 | /* If INSTANTIATION has been instantiated, then we don't |
28278 | need to consider it again in the future. */ |
28279 | struct pending_template *drop = *t; |
28280 | *t = (*t)->next; |
28281 | set_refcount_ptr (ptr&: drop->tinst); |
28282 | pending_template_freelist ().free (obj: drop); |
28283 | } |
28284 | else |
28285 | { |
28286 | last = *t; |
28287 | t = &(*t)->next; |
28288 | } |
28289 | } |
28290 | last_pending_template = last; |
28291 | } |
28292 | while (reconsider); |
28293 | |
28294 | input_location = saved_loc; |
28295 | module_kind = saved_module_kind; |
28296 | } |
28297 | |
28298 | /* Substitute ARGVEC into T, which is a list of initializers for |
28299 | either base class or a non-static data member. The TREE_PURPOSEs |
28300 | are DECLs, and the TREE_VALUEs are the initializer values. Used by |
28301 | instantiate_decl. */ |
28302 | |
28303 | static tree |
28304 | tsubst_initializer_list (tree t, tree argvec) |
28305 | { |
28306 | tree inits = NULL_TREE; |
28307 | tree target_ctor = error_mark_node; |
28308 | |
28309 | for (; t; t = TREE_CHAIN (t)) |
28310 | { |
28311 | tree decl; |
28312 | tree init; |
28313 | tree expanded_bases = NULL_TREE; |
28314 | tree expanded_arguments = NULL_TREE; |
28315 | int i, len = 1; |
28316 | |
28317 | if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION) |
28318 | { |
28319 | tree expr; |
28320 | tree arg; |
28321 | |
28322 | /* Expand the base class expansion type into separate base |
28323 | classes. */ |
28324 | expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), args: argvec, |
28325 | complain: tf_warning_or_error, |
28326 | NULL_TREE); |
28327 | if (expanded_bases == error_mark_node) |
28328 | continue; |
28329 | |
28330 | /* We'll be building separate TREE_LISTs of arguments for |
28331 | each base. */ |
28332 | len = TREE_VEC_LENGTH (expanded_bases); |
28333 | expanded_arguments = make_tree_vec (len); |
28334 | for (i = 0; i < len; i++) |
28335 | TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE; |
28336 | |
28337 | /* Build a dummy EXPR_PACK_EXPANSION that will be used to |
28338 | expand each argument in the TREE_VALUE of t. */ |
28339 | expr = make_node (EXPR_PACK_EXPANSION); |
28340 | PACK_EXPANSION_LOCAL_P (expr) = true; |
28341 | PACK_EXPANSION_PARAMETER_PACKS (expr) = |
28342 | PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t)); |
28343 | |
28344 | if (TREE_VALUE (t) == void_type_node) |
28345 | /* VOID_TYPE_NODE is used to indicate |
28346 | value-initialization. */ |
28347 | { |
28348 | for (i = 0; i < len; i++) |
28349 | TREE_VEC_ELT (expanded_arguments, i) = void_type_node; |
28350 | } |
28351 | else |
28352 | { |
28353 | /* Substitute parameter packs into each argument in the |
28354 | TREE_LIST. */ |
28355 | in_base_initializer = 1; |
28356 | for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg)) |
28357 | { |
28358 | tree expanded_exprs; |
28359 | |
28360 | /* Expand the argument. */ |
28361 | tree value; |
28362 | if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION) |
28363 | value = TREE_VALUE (arg); |
28364 | else |
28365 | { |
28366 | value = expr; |
28367 | PACK_EXPANSION_PATTERN (value) = TREE_VALUE (arg); |
28368 | } |
28369 | expanded_exprs |
28370 | = tsubst_pack_expansion (t: value, args: argvec, |
28371 | complain: tf_warning_or_error, |
28372 | NULL_TREE); |
28373 | if (expanded_exprs == error_mark_node) |
28374 | continue; |
28375 | |
28376 | /* Prepend each of the expanded expressions to the |
28377 | corresponding TREE_LIST in EXPANDED_ARGUMENTS. */ |
28378 | for (i = 0; i < len; i++) |
28379 | if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION) |
28380 | for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++) |
28381 | TREE_VEC_ELT (expanded_arguments, i) |
28382 | = tree_cons (NULL_TREE, |
28383 | TREE_VEC_ELT (expanded_exprs, j), |
28384 | TREE_VEC_ELT (expanded_arguments, i)); |
28385 | else |
28386 | TREE_VEC_ELT (expanded_arguments, i) |
28387 | = tree_cons (NULL_TREE, |
28388 | TREE_VEC_ELT (expanded_exprs, i), |
28389 | TREE_VEC_ELT (expanded_arguments, i)); |
28390 | } |
28391 | in_base_initializer = 0; |
28392 | |
28393 | /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS, |
28394 | since we built them backwards. */ |
28395 | for (i = 0; i < len; i++) |
28396 | { |
28397 | TREE_VEC_ELT (expanded_arguments, i) = |
28398 | nreverse (TREE_VEC_ELT (expanded_arguments, i)); |
28399 | } |
28400 | } |
28401 | } |
28402 | |
28403 | for (i = 0; i < len; ++i) |
28404 | { |
28405 | if (expanded_bases) |
28406 | { |
28407 | decl = TREE_VEC_ELT (expanded_bases, i); |
28408 | decl = expand_member_init (decl); |
28409 | init = TREE_VEC_ELT (expanded_arguments, i); |
28410 | } |
28411 | else |
28412 | { |
28413 | tree tmp; |
28414 | if (TYPE_P (TREE_PURPOSE (t))) |
28415 | decl = tsubst (TREE_PURPOSE (t), args: argvec, |
28416 | complain: tf_warning_or_error, NULL_TREE); |
28417 | else |
28418 | decl = tsubst_expr (TREE_PURPOSE (t), args: argvec, |
28419 | complain: tf_warning_or_error, NULL_TREE); |
28420 | |
28421 | decl = expand_member_init (decl); |
28422 | if (decl && !DECL_P (decl)) |
28423 | in_base_initializer = 1; |
28424 | |
28425 | init = TREE_VALUE (t); |
28426 | tmp = init; |
28427 | if (init != void_type_node) |
28428 | init = tsubst_expr (t: init, args: argvec, |
28429 | complain: tf_warning_or_error, NULL_TREE); |
28430 | if (init == NULL_TREE && tmp != NULL_TREE) |
28431 | /* If we had an initializer but it instantiated to nothing, |
28432 | value-initialize the object. This will only occur when |
28433 | the initializer was a pack expansion where the parameter |
28434 | packs used in that expansion were of length zero. */ |
28435 | init = void_type_node; |
28436 | in_base_initializer = 0; |
28437 | } |
28438 | |
28439 | if (target_ctor != error_mark_node |
28440 | && init != error_mark_node) |
28441 | { |
28442 | error ("mem-initializer for %qD follows constructor delegation", |
28443 | decl); |
28444 | return inits; |
28445 | } |
28446 | /* Look for a target constructor. */ |
28447 | if (init != error_mark_node |
28448 | && decl && CLASS_TYPE_P (decl) |
28449 | && same_type_p (decl, current_class_type)) |
28450 | { |
28451 | maybe_warn_cpp0x (str: CPP0X_DELEGATING_CTORS); |
28452 | if (inits) |
28453 | { |
28454 | error ("constructor delegation follows mem-initializer for %qD", |
28455 | TREE_PURPOSE (inits)); |
28456 | continue; |
28457 | } |
28458 | target_ctor = init; |
28459 | } |
28460 | |
28461 | if (decl) |
28462 | { |
28463 | init = build_tree_list (decl, init); |
28464 | /* Carry over the dummy TREE_TYPE node containing the source |
28465 | location. */ |
28466 | TREE_TYPE (init) = TREE_TYPE (t); |
28467 | TREE_CHAIN (init) = inits; |
28468 | inits = init; |
28469 | } |
28470 | } |
28471 | } |
28472 | return inits; |
28473 | } |
28474 | |
28475 | /* Instantiate an enumerated type. TAG is the template type, NEWTAG |
28476 | is the instantiation (which should have been created with |
28477 | start_enum) and ARGS are the template arguments to use. */ |
28478 | |
28479 | static void |
28480 | tsubst_enum (tree tag, tree newtag, tree args) |
28481 | { |
28482 | tree e; |
28483 | |
28484 | if (SCOPED_ENUM_P (newtag)) |
28485 | begin_scope (sk_scoped_enum, newtag); |
28486 | |
28487 | for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e)) |
28488 | { |
28489 | tree value; |
28490 | tree decl = TREE_VALUE (e); |
28491 | |
28492 | /* Note that in a template enum, the TREE_VALUE is the |
28493 | CONST_DECL, not the corresponding INTEGER_CST. */ |
28494 | value = tsubst_expr (DECL_INITIAL (decl), |
28495 | args, complain: tf_warning_or_error, NULL_TREE); |
28496 | |
28497 | /* Give this enumeration constant the correct access. */ |
28498 | set_current_access_from_decl (decl); |
28499 | |
28500 | /* Actually build the enumerator itself. Here we're assuming that |
28501 | enumerators can't have dependent attributes. */ |
28502 | tree newdecl = build_enumerator (DECL_NAME (decl), value, newtag, |
28503 | DECL_ATTRIBUTES (decl), |
28504 | DECL_SOURCE_LOCATION (decl)); |
28505 | /* Attribute deprecated without an argument isn't sticky: it'll |
28506 | melt into a tree flag, so we need to propagate the flag here, |
28507 | since we just created a new enumerator. */ |
28508 | TREE_DEPRECATED (newdecl) = TREE_DEPRECATED (decl); |
28509 | TREE_UNAVAILABLE (newdecl) = TREE_UNAVAILABLE (decl); |
28510 | } |
28511 | |
28512 | if (SCOPED_ENUM_P (newtag)) |
28513 | finish_scope (); |
28514 | |
28515 | finish_enum_value_list (newtag); |
28516 | finish_enum (newtag); |
28517 | |
28518 | DECL_SOURCE_LOCATION (TYPE_NAME (newtag)) |
28519 | = DECL_SOURCE_LOCATION (TYPE_NAME (tag)); |
28520 | TREE_DEPRECATED (newtag) = TREE_DEPRECATED (tag); |
28521 | TREE_UNAVAILABLE (newtag) = TREE_UNAVAILABLE (tag); |
28522 | } |
28523 | |
28524 | /* DECL is a FUNCTION_DECL that is a template specialization. Return |
28525 | its type -- but without substituting the innermost set of template |
28526 | arguments. So, innermost set of template parameters will appear in |
28527 | the type. */ |
28528 | |
28529 | tree |
28530 | get_mostly_instantiated_function_type (tree decl) |
28531 | { |
28532 | /* For a function, DECL_TI_TEMPLATE is partially instantiated. */ |
28533 | return TREE_TYPE (DECL_TI_TEMPLATE (decl)); |
28534 | } |
28535 | |
28536 | /* Return truthvalue if we're processing a template different from |
28537 | the last one involved in diagnostics. */ |
28538 | bool |
28539 | problematic_instantiation_changed (void) |
28540 | { |
28541 | return current_tinst_level != last_error_tinst_level; |
28542 | } |
28543 | |
28544 | /* Remember current template involved in diagnostics. */ |
28545 | void |
28546 | record_last_problematic_instantiation (void) |
28547 | { |
28548 | set_refcount_ptr (ptr&: last_error_tinst_level, obj: current_tinst_level); |
28549 | } |
28550 | |
28551 | struct tinst_level * |
28552 | current_instantiation (void) |
28553 | { |
28554 | return current_tinst_level; |
28555 | } |
28556 | |
28557 | /* Return TRUE if current_function_decl is being instantiated, false |
28558 | otherwise. */ |
28559 | |
28560 | bool |
28561 | instantiating_current_function_p (void) |
28562 | { |
28563 | return (current_instantiation () |
28564 | && (current_instantiation ()->maybe_get_node () |
28565 | == current_function_decl)); |
28566 | } |
28567 | |
28568 | /* [temp.param] Check that template non-type parm TYPE is of an allowable |
28569 | type. Return false for ok, true for disallowed. Issue error and |
28570 | inform messages under control of COMPLAIN. */ |
28571 | |
28572 | static bool |
28573 | invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain) |
28574 | { |
28575 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
28576 | return false; |
28577 | else if (TYPE_PTR_P (type)) |
28578 | return false; |
28579 | else if (TYPE_REF_P (type) |
28580 | && !TYPE_REF_IS_RVALUE (type)) |
28581 | return false; |
28582 | else if (TYPE_PTRMEM_P (type)) |
28583 | return false; |
28584 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
28585 | { |
28586 | if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20) |
28587 | { |
28588 | if (complain & tf_error) |
28589 | error ("non-type template parameters of deduced class type only " |
28590 | "available with %<-std=c++20%> or %<-std=gnu++20%>"); |
28591 | return true; |
28592 | } |
28593 | return false; |
28594 | } |
28595 | else if (TREE_CODE (type) == NULLPTR_TYPE) |
28596 | return false; |
28597 | else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM |
28598 | && cxx_dialect < cxx11) |
28599 | /* Fall through; before C++11 alias templates, a bound ttp |
28600 | always instantiates into a class type. */; |
28601 | else if (WILDCARD_TYPE_P (type)) |
28602 | /* Any other wildcard type not already handled above is allowed. */ |
28603 | return false; |
28604 | else if (TREE_CODE (type) == COMPLEX_TYPE) |
28605 | /* Fall through. */; |
28606 | else if (VOID_TYPE_P (type)) |
28607 | /* Fall through. */; |
28608 | else if (cxx_dialect >= cxx20) |
28609 | { |
28610 | if (dependent_type_p (type)) |
28611 | return false; |
28612 | if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) |
28613 | return true; |
28614 | if (structural_type_p (type)) |
28615 | return false; |
28616 | if (complain & tf_error) |
28617 | { |
28618 | auto_diagnostic_group d; |
28619 | error ("%qT is not a valid type for a template non-type " |
28620 | "parameter because it is not structural", type); |
28621 | structural_type_p (type, true); |
28622 | } |
28623 | return true; |
28624 | } |
28625 | else if (CLASS_TYPE_P (type)) |
28626 | { |
28627 | if (complain & tf_error) |
28628 | error ("non-type template parameters of class type only available " |
28629 | "with %<-std=c++20%> or %<-std=gnu++20%>"); |
28630 | return true; |
28631 | } |
28632 | |
28633 | if (complain & tf_error) |
28634 | { |
28635 | if (type == error_mark_node) |
28636 | inform (input_location, "invalid template non-type parameter"); |
28637 | else |
28638 | error ("%q#T is not a valid type for a template non-type parameter", |
28639 | type); |
28640 | } |
28641 | return true; |
28642 | } |
28643 | |
28644 | /* Returns true iff the noexcept-specifier for TYPE is value-dependent. */ |
28645 | |
28646 | static bool |
28647 | value_dependent_noexcept_spec_p (tree type) |
28648 | { |
28649 | if (tree spec = TYPE_RAISES_EXCEPTIONS (type)) |
28650 | if (tree noex = TREE_PURPOSE (spec)) |
28651 | /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't |
28652 | affect overload resolution and treating it as dependent breaks |
28653 | things. Same for an unparsed noexcept expression. */ |
28654 | if (TREE_CODE (noex) != DEFERRED_NOEXCEPT |
28655 | && TREE_CODE (noex) != DEFERRED_PARSE |
28656 | && value_dependent_expression_p (noex)) |
28657 | return true; |
28658 | |
28659 | return false; |
28660 | } |
28661 | |
28662 | /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type]. |
28663 | Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/ |
28664 | |
28665 | static bool |
28666 | dependent_type_p_r (tree type) |
28667 | { |
28668 | tree scope; |
28669 | |
28670 | /* [temp.dep.type] |
28671 | |
28672 | A type is dependent if it is: |
28673 | |
28674 | -- a template parameter. Template template parameters are types |
28675 | for us (since TYPE_P holds true for them) so we handle |
28676 | them here. */ |
28677 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
28678 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM) |
28679 | return true; |
28680 | /* -- a qualified-id with a nested-name-specifier which contains a |
28681 | class-name that names a dependent type or whose unqualified-id |
28682 | names a dependent type. */ |
28683 | if (TREE_CODE (type) == TYPENAME_TYPE) |
28684 | return true; |
28685 | |
28686 | /* -- a cv-qualified type where the cv-unqualified type is |
28687 | dependent. |
28688 | No code is necessary for this bullet; the code below handles |
28689 | cv-qualified types, and we don't want to strip aliases with |
28690 | TYPE_MAIN_VARIANT because of DR 1558. */ |
28691 | /* -- a compound type constructed from any dependent type. */ |
28692 | if (TYPE_PTRMEM_P (type)) |
28693 | return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type)) |
28694 | || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE |
28695 | (type))); |
28696 | else if (INDIRECT_TYPE_P (type)) |
28697 | return dependent_type_p (TREE_TYPE (type)); |
28698 | else if (FUNC_OR_METHOD_TYPE_P (type)) |
28699 | { |
28700 | tree arg_type; |
28701 | |
28702 | if (dependent_type_p (TREE_TYPE (type))) |
28703 | return true; |
28704 | for (arg_type = TYPE_ARG_TYPES (type); |
28705 | arg_type; |
28706 | arg_type = TREE_CHAIN (arg_type)) |
28707 | if (dependent_type_p (TREE_VALUE (arg_type))) |
28708 | return true; |
28709 | if (cxx_dialect >= cxx17 |
28710 | && value_dependent_noexcept_spec_p (type)) |
28711 | /* A value-dependent noexcept-specifier makes the type dependent. */ |
28712 | return true; |
28713 | return false; |
28714 | } |
28715 | /* -- an array type constructed from any dependent type or whose |
28716 | size is specified by a constant expression that is |
28717 | value-dependent. |
28718 | |
28719 | We checked for type- and value-dependence of the bounds in |
28720 | compute_array_index_type, so TYPE_DEPENDENT_P is already set. */ |
28721 | if (TREE_CODE (type) == ARRAY_TYPE) |
28722 | { |
28723 | if (TYPE_DOMAIN (type) |
28724 | && dependent_type_p (TYPE_DOMAIN (type))) |
28725 | return true; |
28726 | return dependent_type_p (TREE_TYPE (type)); |
28727 | } |
28728 | |
28729 | /* -- a template-id in which either the template name is a template |
28730 | parameter ... */ |
28731 | if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) |
28732 | return true; |
28733 | /* ... or any of the template arguments is a dependent type or |
28734 | an expression that is type-dependent or value-dependent. */ |
28735 | else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type) |
28736 | && (any_dependent_template_arguments_p |
28737 | (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type))))) |
28738 | return true; |
28739 | |
28740 | /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and TRAIT_TYPEs are |
28741 | dependent; if the argument of the `typeof' expression is not |
28742 | type-dependent, then it should already been have resolved. */ |
28743 | if (TREE_CODE (type) == TYPEOF_TYPE |
28744 | || TREE_CODE (type) == DECLTYPE_TYPE |
28745 | || TREE_CODE (type) == TRAIT_TYPE) |
28746 | return true; |
28747 | |
28748 | /* A template argument pack is dependent if any of its packed |
28749 | arguments are. */ |
28750 | if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) |
28751 | { |
28752 | tree args = ARGUMENT_PACK_ARGS (type); |
28753 | for (tree arg : tree_vec_range (args)) |
28754 | if (dependent_template_arg_p (arg)) |
28755 | return true; |
28756 | } |
28757 | |
28758 | /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must |
28759 | be template parameters. This includes pack-index-specifiers. */ |
28760 | if (TREE_CODE (type) == TYPE_PACK_EXPANSION |
28761 | || TREE_CODE (type) == PACK_INDEX_TYPE) |
28762 | return true; |
28763 | |
28764 | if (TREE_CODE (type) == DEPENDENT_OPERATOR_TYPE) |
28765 | return true; |
28766 | |
28767 | if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type))) |
28768 | return true; |
28769 | |
28770 | /* The standard does not specifically mention types that are local |
28771 | to template functions or local classes, but they should be |
28772 | considered dependent too. For example: |
28773 | |
28774 | template <int I> void f() { |
28775 | enum E { a = I }; |
28776 | S<sizeof (E)> s; |
28777 | } |
28778 | |
28779 | The size of `E' cannot be known until the value of `I' has been |
28780 | determined. Therefore, `E' must be considered dependent. */ |
28781 | scope = TYPE_CONTEXT (type); |
28782 | if (scope && TYPE_P (scope)) |
28783 | return dependent_type_p (scope); |
28784 | /* Don't use type_dependent_expression_p here, as it can lead |
28785 | to infinite recursion trying to determine whether a lambda |
28786 | nested in a lambda is dependent (c++/47687). */ |
28787 | else if (scope && TREE_CODE (scope) == FUNCTION_DECL |
28788 | && DECL_LANG_SPECIFIC (scope) |
28789 | && DECL_TEMPLATE_INFO (scope) |
28790 | && (any_dependent_template_arguments_p |
28791 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope))))) |
28792 | return true; |
28793 | |
28794 | /* Other types are non-dependent. */ |
28795 | return false; |
28796 | } |
28797 | |
28798 | /* Returns TRUE if TYPE is dependent, in the sense of |
28799 | [temp.dep.type]. Note that a NULL type is considered dependent. */ |
28800 | |
28801 | bool |
28802 | dependent_type_p (tree type) |
28803 | { |
28804 | /* If there are no template parameters in scope, then there can't be |
28805 | any dependent types. */ |
28806 | if (!processing_template_decl) |
28807 | { |
28808 | /* If we are not processing a template, then nobody should be |
28809 | providing us with a dependent type. */ |
28810 | gcc_assert (type); |
28811 | gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type) |
28812 | || seen_error ()); |
28813 | return false; |
28814 | } |
28815 | |
28816 | /* If the type is NULL, we have not computed a type for the entity |
28817 | in question; in that case, the type is dependent. */ |
28818 | if (!type) |
28819 | return true; |
28820 | |
28821 | /* Erroneous types can be considered non-dependent. */ |
28822 | if (type == error_mark_node) |
28823 | return false; |
28824 | |
28825 | /* If we have not already computed the appropriate value for TYPE, |
28826 | do so now. */ |
28827 | if (!TYPE_DEPENDENT_P_VALID (type)) |
28828 | { |
28829 | TYPE_DEPENDENT_P (type) = dependent_type_p_r (type); |
28830 | TYPE_DEPENDENT_P_VALID (type) = 1; |
28831 | } |
28832 | |
28833 | return TYPE_DEPENDENT_P (type); |
28834 | } |
28835 | |
28836 | /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any |
28837 | lookup. In other words, a dependent type that is not the current |
28838 | instantiation. */ |
28839 | |
28840 | bool |
28841 | dependent_scope_p (tree scope) |
28842 | { |
28843 | return (scope && TYPE_P (scope) && dependent_type_p (type: scope) |
28844 | && !currently_open_class (scope)); |
28845 | } |
28846 | |
28847 | /* True if we might find more declarations in SCOPE during instantiation than |
28848 | we can when parsing the template. */ |
28849 | |
28850 | bool |
28851 | dependentish_scope_p (tree scope) |
28852 | { |
28853 | return dependent_scope_p (scope) || any_dependent_bases_p (scope); |
28854 | } |
28855 | |
28856 | /* T is a SCOPE_REF. Return whether it represents a non-static member of |
28857 | an unknown base of 'this' (and is therefore instantiation-dependent). */ |
28858 | |
28859 | static bool |
28860 | unknown_base_ref_p (tree t) |
28861 | { |
28862 | if (!current_class_ptr) |
28863 | return false; |
28864 | |
28865 | tree mem = TREE_OPERAND (t, 1); |
28866 | if (shared_member_p (mem)) |
28867 | return false; |
28868 | |
28869 | tree cur = current_nonlambda_class_type (); |
28870 | if (!any_dependent_bases_p (cur)) |
28871 | return false; |
28872 | |
28873 | tree ctx = TREE_OPERAND (t, 0); |
28874 | if (DERIVED_FROM_P (ctx, cur)) |
28875 | return false; |
28876 | |
28877 | return true; |
28878 | } |
28879 | |
28880 | /* T is a SCOPE_REF; return whether we need to consider it |
28881 | instantiation-dependent so that we can check access at instantiation |
28882 | time even though we know which member it resolves to. */ |
28883 | |
28884 | static bool |
28885 | instantiation_dependent_scope_ref_p (tree t) |
28886 | { |
28887 | if (DECL_P (TREE_OPERAND (t, 1)) |
28888 | && CLASS_TYPE_P (TREE_OPERAND (t, 0)) |
28889 | && !dependent_scope_p (TREE_OPERAND (t, 0)) |
28890 | && !unknown_base_ref_p (t) |
28891 | && accessible_in_template_p (TREE_OPERAND (t, 0), |
28892 | TREE_OPERAND (t, 1))) |
28893 | return false; |
28894 | else |
28895 | return true; |
28896 | } |
28897 | |
28898 | /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of |
28899 | [temp.dep.constexpr]. EXPRESSION is already known to be a constant |
28900 | expression. */ |
28901 | |
28902 | /* Note that this predicate is not appropriate for general expressions; |
28903 | only constant expressions (that satisfy potential_constant_expression) |
28904 | can be tested for value dependence. */ |
28905 | |
28906 | bool |
28907 | value_dependent_expression_p (tree expression) |
28908 | { |
28909 | if (!processing_template_decl || expression == NULL_TREE) |
28910 | return false; |
28911 | |
28912 | /* A type-dependent expression is also value-dependent. */ |
28913 | if (type_dependent_expression_p (expression)) |
28914 | return true; |
28915 | |
28916 | switch (TREE_CODE (expression)) |
28917 | { |
28918 | case BASELINK: |
28919 | /* A dependent member function of the current instantiation. */ |
28920 | return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression))); |
28921 | |
28922 | case FUNCTION_DECL: |
28923 | /* A dependent member function of the current instantiation. */ |
28924 | if (DECL_CLASS_SCOPE_P (expression) |
28925 | && dependent_type_p (DECL_CONTEXT (expression))) |
28926 | return true; |
28927 | break; |
28928 | |
28929 | case IDENTIFIER_NODE: |
28930 | /* A name that has not been looked up -- must be dependent. */ |
28931 | return true; |
28932 | |
28933 | case TEMPLATE_PARM_INDEX: |
28934 | /* A non-type template parm. */ |
28935 | return true; |
28936 | |
28937 | case CONST_DECL: |
28938 | /* A non-type template parm. */ |
28939 | if (DECL_TEMPLATE_PARM_P (expression)) |
28940 | return true; |
28941 | return value_dependent_expression_p (DECL_INITIAL (expression)); |
28942 | |
28943 | case VAR_DECL: |
28944 | /* A constant with literal type and is initialized |
28945 | with an expression that is value-dependent. */ |
28946 | if (DECL_DEPENDENT_INIT_P (expression)) |
28947 | return true; |
28948 | if (DECL_HAS_VALUE_EXPR_P (expression)) |
28949 | { |
28950 | tree value_expr = DECL_VALUE_EXPR (expression); |
28951 | if (value_dependent_expression_p (expression: value_expr) |
28952 | /* __PRETTY_FUNCTION__ inside a template function is dependent |
28953 | on the name of the function. */ |
28954 | || (DECL_PRETTY_FUNCTION_P (expression) |
28955 | /* It might be used in a template, but not a template |
28956 | function, in which case its DECL_VALUE_EXPR will be |
28957 | "top level". */ |
28958 | && value_expr == error_mark_node)) |
28959 | return true; |
28960 | } |
28961 | else if (TYPE_REF_P (TREE_TYPE (expression))) |
28962 | /* FIXME cp_finish_decl doesn't fold reference initializers. */ |
28963 | return true; |
28964 | /* We have a constexpr variable and we're processing a template. When |
28965 | there's lifetime extension involved (for which finish_compound_literal |
28966 | used to create a temporary), we'll not be able to evaluate the |
28967 | variable until instantiating, so pretend it's value-dependent. */ |
28968 | else if (DECL_DECLARED_CONSTEXPR_P (expression) |
28969 | && !TREE_CONSTANT (expression)) |
28970 | return true; |
28971 | return false; |
28972 | |
28973 | case DYNAMIC_CAST_EXPR: |
28974 | case STATIC_CAST_EXPR: |
28975 | case CONST_CAST_EXPR: |
28976 | case REINTERPRET_CAST_EXPR: |
28977 | case CAST_EXPR: |
28978 | case IMPLICIT_CONV_EXPR: |
28979 | /* These expressions are value-dependent if the type to which |
28980 | the cast occurs is dependent or the expression being casted |
28981 | is value-dependent. */ |
28982 | { |
28983 | tree type = TREE_TYPE (expression); |
28984 | |
28985 | if (dependent_type_p (type)) |
28986 | return true; |
28987 | |
28988 | /* A functional cast has a list of operands. */ |
28989 | expression = TREE_OPERAND (expression, 0); |
28990 | if (!expression) |
28991 | { |
28992 | /* If there are no operands, it must be an expression such |
28993 | as "int()". This should not happen for aggregate types |
28994 | because it would form non-constant expressions. */ |
28995 | gcc_assert (cxx_dialect >= cxx11 |
28996 | || INTEGRAL_OR_ENUMERATION_TYPE_P (type)); |
28997 | |
28998 | return false; |
28999 | } |
29000 | |
29001 | if (TREE_CODE (expression) == TREE_LIST) |
29002 | return any_value_dependent_elements_p (expression); |
29003 | |
29004 | if (TREE_CODE (type) == REFERENCE_TYPE |
29005 | && has_value_dependent_address (op: expression)) |
29006 | return true; |
29007 | |
29008 | return value_dependent_expression_p (expression); |
29009 | } |
29010 | |
29011 | case SIZEOF_EXPR: |
29012 | if (SIZEOF_EXPR_TYPE_P (expression)) |
29013 | return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0))); |
29014 | /* FALLTHRU */ |
29015 | case ALIGNOF_EXPR: |
29016 | case TYPEID_EXPR: |
29017 | /* A `sizeof' expression is value-dependent if the operand is |
29018 | type-dependent or is a pack expansion. */ |
29019 | expression = TREE_OPERAND (expression, 0); |
29020 | if (PACK_EXPANSION_P (expression)) |
29021 | return true; |
29022 | else if (TYPE_P (expression)) |
29023 | return dependent_type_p (type: expression); |
29024 | return instantiation_dependent_uneval_expression_p (expression); |
29025 | |
29026 | case AT_ENCODE_EXPR: |
29027 | /* An 'encode' expression is value-dependent if the operand is |
29028 | type-dependent. */ |
29029 | expression = TREE_OPERAND (expression, 0); |
29030 | return dependent_type_p (type: expression); |
29031 | |
29032 | case NOEXCEPT_EXPR: |
29033 | expression = TREE_OPERAND (expression, 0); |
29034 | return instantiation_dependent_uneval_expression_p (expression); |
29035 | |
29036 | case SCOPE_REF: |
29037 | /* All instantiation-dependent expressions should also be considered |
29038 | value-dependent. */ |
29039 | return instantiation_dependent_scope_ref_p (t: expression); |
29040 | |
29041 | case COMPONENT_REF: |
29042 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) |
29043 | || value_dependent_expression_p (TREE_OPERAND (expression, 1))); |
29044 | |
29045 | case NONTYPE_ARGUMENT_PACK: |
29046 | /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument |
29047 | is value-dependent. */ |
29048 | for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression))) |
29049 | if (value_dependent_expression_p (expression: arg)) |
29050 | return true; |
29051 | return false; |
29052 | |
29053 | case TRAIT_EXPR: |
29054 | { |
29055 | if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))) |
29056 | return true; |
29057 | |
29058 | tree type2 = TRAIT_EXPR_TYPE2 (expression); |
29059 | if (!type2) |
29060 | return false; |
29061 | |
29062 | if (TREE_CODE (type2) != TREE_VEC) |
29063 | return dependent_type_p (type: type2); |
29064 | |
29065 | for (tree arg : tree_vec_range (type2)) |
29066 | if (dependent_type_p (type: arg)) |
29067 | return true; |
29068 | |
29069 | return false; |
29070 | } |
29071 | |
29072 | case MODOP_EXPR: |
29073 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) |
29074 | || (value_dependent_expression_p (TREE_OPERAND (expression, 2)))); |
29075 | |
29076 | case ARRAY_REF: |
29077 | return ((value_dependent_expression_p (TREE_OPERAND (expression, 0))) |
29078 | || (value_dependent_expression_p (TREE_OPERAND (expression, 1)))); |
29079 | |
29080 | case ADDR_EXPR: |
29081 | { |
29082 | tree op = TREE_OPERAND (expression, 0); |
29083 | return (value_dependent_expression_p (expression: op) |
29084 | || has_value_dependent_address (op)); |
29085 | } |
29086 | |
29087 | case REQUIRES_EXPR: |
29088 | /* Treat all requires-expressions as value-dependent so |
29089 | we don't try to fold them. */ |
29090 | return true; |
29091 | |
29092 | case TYPE_REQ: |
29093 | return dependent_type_p (TREE_OPERAND (expression, 0)); |
29094 | |
29095 | case CALL_EXPR: |
29096 | { |
29097 | if (value_dependent_expression_p (CALL_EXPR_FN (expression))) |
29098 | return true; |
29099 | tree fn = get_callee_fndecl (expression); |
29100 | int i, nargs; |
29101 | nargs = call_expr_nargs (expression); |
29102 | for (i = 0; i < nargs; ++i) |
29103 | { |
29104 | tree op = CALL_EXPR_ARG (expression, i); |
29105 | /* In a call to a constexpr member function, look through the |
29106 | implicit ADDR_EXPR on the object argument so that it doesn't |
29107 | cause the call to be considered value-dependent. We also |
29108 | look through it in potential_constant_expression. */ |
29109 | if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn) |
29110 | && DECL_IOBJ_MEMBER_FUNCTION_P (fn) |
29111 | && TREE_CODE (op) == ADDR_EXPR) |
29112 | op = TREE_OPERAND (op, 0); |
29113 | if (value_dependent_expression_p (expression: op)) |
29114 | return true; |
29115 | } |
29116 | return false; |
29117 | } |
29118 | |
29119 | case TEMPLATE_ID_EXPR: |
29120 | return concept_definition_p (TREE_OPERAND (expression, 0)) |
29121 | && any_dependent_template_arguments_p (TREE_OPERAND (expression, 1)); |
29122 | |
29123 | case CONSTRUCTOR: |
29124 | { |
29125 | unsigned ix; |
29126 | tree val; |
29127 | if (dependent_type_p (TREE_TYPE (expression))) |
29128 | return true; |
29129 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val) |
29130 | if (value_dependent_expression_p (expression: val)) |
29131 | return true; |
29132 | return false; |
29133 | } |
29134 | |
29135 | case STMT_EXPR: |
29136 | /* Treat a GNU statement expression as dependent to avoid crashing |
29137 | under instantiate_non_dependent_expr; it can't be constant. */ |
29138 | return true; |
29139 | |
29140 | case NEW_EXPR: |
29141 | case VEC_NEW_EXPR: |
29142 | /* The second operand is a type, which type_dependent_expression_p |
29143 | (and therefore value_dependent_expression_p) doesn't want to see. */ |
29144 | return (value_dependent_expression_p (TREE_OPERAND (expression, 0)) |
29145 | || value_dependent_expression_p (TREE_OPERAND (expression, 2)) |
29146 | || value_dependent_expression_p (TREE_OPERAND (expression, 3))); |
29147 | |
29148 | default: |
29149 | /* A constant expression is value-dependent if any subexpression is |
29150 | value-dependent. */ |
29151 | switch (TREE_CODE_CLASS (TREE_CODE (expression))) |
29152 | { |
29153 | case tcc_reference: |
29154 | case tcc_unary: |
29155 | case tcc_comparison: |
29156 | case tcc_binary: |
29157 | case tcc_expression: |
29158 | case tcc_vl_exp: |
29159 | { |
29160 | int i, len = cp_tree_operand_length (expression); |
29161 | |
29162 | for (i = 0; i < len; i++) |
29163 | { |
29164 | tree t = TREE_OPERAND (expression, i); |
29165 | |
29166 | /* In some cases, some of the operands may be missing. |
29167 | (For example, in the case of PREDECREMENT_EXPR, the |
29168 | amount to increment by may be missing.) That doesn't |
29169 | make the expression dependent. */ |
29170 | if (t && value_dependent_expression_p (expression: t)) |
29171 | return true; |
29172 | } |
29173 | } |
29174 | break; |
29175 | default: |
29176 | break; |
29177 | } |
29178 | break; |
29179 | } |
29180 | |
29181 | /* The expression is not value-dependent. */ |
29182 | return false; |
29183 | } |
29184 | |
29185 | /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of |
29186 | [temp.dep.expr]. Note that an expression with no type is |
29187 | considered dependent. Other parts of the compiler arrange for an |
29188 | expression with type-dependent subexpressions to have no type, so |
29189 | this function doesn't have to be fully recursive. */ |
29190 | |
29191 | bool |
29192 | type_dependent_expression_p (tree expression) |
29193 | { |
29194 | if (!processing_template_decl) |
29195 | return false; |
29196 | |
29197 | if (expression == NULL_TREE || expression == error_mark_node) |
29198 | return false; |
29199 | |
29200 | gcc_checking_assert (!TYPE_P (expression)); |
29201 | |
29202 | STRIP_ANY_LOCATION_WRAPPER (expression); |
29203 | |
29204 | /* Assume a TU-local entity is not dependent, we'll error later when |
29205 | instantiating anyway. */ |
29206 | if (TREE_CODE (expression) == TU_LOCAL_ENTITY) |
29207 | return false; |
29208 | |
29209 | /* An unresolved name is always dependent. */ |
29210 | if (identifier_p (t: expression) |
29211 | || TREE_CODE (expression) == USING_DECL) |
29212 | return true; |
29213 | |
29214 | /* A lambda-expression in template context is dependent. dependent_type_p is |
29215 | true for a lambda in the scope of a class or function template, but that |
29216 | doesn't cover all template contexts, like a default template argument. */ |
29217 | if (TREE_CODE (expression) == LAMBDA_EXPR) |
29218 | return true; |
29219 | |
29220 | /* A fold expression is type-dependent. */ |
29221 | if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR |
29222 | || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR |
29223 | || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR |
29224 | || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR) |
29225 | return true; |
29226 | |
29227 | /* Some expression forms are never type-dependent. */ |
29228 | if (TREE_CODE (expression) == SIZEOF_EXPR |
29229 | || TREE_CODE (expression) == ALIGNOF_EXPR |
29230 | || TREE_CODE (expression) == AT_ENCODE_EXPR |
29231 | || TREE_CODE (expression) == NOEXCEPT_EXPR |
29232 | || TREE_CODE (expression) == TRAIT_EXPR |
29233 | || TREE_CODE (expression) == TYPEID_EXPR |
29234 | || TREE_CODE (expression) == DELETE_EXPR |
29235 | || TREE_CODE (expression) == VEC_DELETE_EXPR |
29236 | || TREE_CODE (expression) == THROW_EXPR |
29237 | || TREE_CODE (expression) == REQUIRES_EXPR) |
29238 | return false; |
29239 | |
29240 | /* The types of these expressions depends only on the type to which |
29241 | the cast occurs. */ |
29242 | if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR |
29243 | || TREE_CODE (expression) == STATIC_CAST_EXPR |
29244 | || TREE_CODE (expression) == CONST_CAST_EXPR |
29245 | || TREE_CODE (expression) == REINTERPRET_CAST_EXPR |
29246 | || TREE_CODE (expression) == IMPLICIT_CONV_EXPR |
29247 | || TREE_CODE (expression) == CAST_EXPR) |
29248 | return dependent_type_p (TREE_TYPE (expression)); |
29249 | |
29250 | /* The types of these expressions depends only on the type created |
29251 | by the expression. */ |
29252 | if (TREE_CODE (expression) == NEW_EXPR |
29253 | || TREE_CODE (expression) == VEC_NEW_EXPR) |
29254 | { |
29255 | /* For NEW_EXPR tree nodes created inside a template, either |
29256 | the object type itself or a TREE_LIST may appear as the |
29257 | operand 1. */ |
29258 | tree type = TREE_OPERAND (expression, 1); |
29259 | if (TREE_CODE (type) == TREE_LIST) |
29260 | /* This is an array type. We need to check array dimensions |
29261 | as well. */ |
29262 | return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type))) |
29263 | || value_dependent_expression_p |
29264 | (TREE_OPERAND (TREE_VALUE (type), 1)); |
29265 | /* Array type whose dimension has to be deduced. */ |
29266 | else if (TREE_CODE (type) == ARRAY_TYPE |
29267 | && TREE_OPERAND (expression, 2) == NULL_TREE) |
29268 | return true; |
29269 | else |
29270 | return dependent_type_p (type); |
29271 | } |
29272 | |
29273 | if (TREE_CODE (expression) == SCOPE_REF) |
29274 | { |
29275 | tree scope = TREE_OPERAND (expression, 0); |
29276 | tree name = TREE_OPERAND (expression, 1); |
29277 | |
29278 | /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it |
29279 | contains an identifier associated by name lookup with one or more |
29280 | declarations declared with a dependent type, or...a |
29281 | nested-name-specifier or qualified-id that names a member of an |
29282 | unknown specialization. */ |
29283 | return (type_dependent_expression_p (expression: name) |
29284 | || dependent_scope_p (scope)); |
29285 | } |
29286 | |
29287 | if (TREE_CODE (expression) == TEMPLATE_DECL |
29288 | && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression)) |
29289 | return uses_outer_template_parms (decl: expression); |
29290 | |
29291 | if (TREE_CODE (expression) == STMT_EXPR) |
29292 | expression = stmt_expr_value_expr (expression); |
29293 | |
29294 | if (BRACE_ENCLOSED_INITIALIZER_P (expression)) |
29295 | { |
29296 | for (auto &elt : CONSTRUCTOR_ELTS (expression)) |
29297 | if (type_dependent_expression_p (expression: elt.value)) |
29298 | return true; |
29299 | return false; |
29300 | } |
29301 | |
29302 | /* A static data member of the current instantiation with incomplete |
29303 | array type is type-dependent, as the definition and specializations |
29304 | can have different bounds. */ |
29305 | if (VAR_P (expression) |
29306 | && DECL_CLASS_SCOPE_P (expression) |
29307 | && dependent_type_p (DECL_CONTEXT (expression)) |
29308 | && VAR_HAD_UNKNOWN_BOUND (expression)) |
29309 | return true; |
29310 | |
29311 | /* An array of unknown bound depending on a variadic parameter, eg: |
29312 | |
29313 | template<typename... Args> |
29314 | void foo (Args... args) |
29315 | { |
29316 | int arr[] = { args... }; |
29317 | } |
29318 | |
29319 | template<int... vals> |
29320 | void bar () |
29321 | { |
29322 | int arr[] = { vals... }; |
29323 | } |
29324 | |
29325 | If the array has no length and has an initializer, it must be that |
29326 | we couldn't determine its length in cp_complete_array_type because |
29327 | it is dependent. */ |
29328 | if (((VAR_P (expression) && DECL_INITIAL (expression)) |
29329 | || COMPOUND_LITERAL_P (expression)) |
29330 | && TREE_TYPE (expression) != NULL_TREE |
29331 | && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE |
29332 | && !TYPE_DOMAIN (TREE_TYPE (expression))) |
29333 | return true; |
29334 | |
29335 | /* Pull a FUNCTION_DECL out of a BASELINK if we can. */ |
29336 | if (BASELINK_P (expression)) |
29337 | { |
29338 | if (BASELINK_OPTYPE (expression) |
29339 | && dependent_type_p (BASELINK_OPTYPE (expression))) |
29340 | return true; |
29341 | expression = BASELINK_FUNCTIONS (expression); |
29342 | } |
29343 | |
29344 | /* A function or variable template-id is type-dependent if it has any |
29345 | dependent template arguments. */ |
29346 | if (VAR_OR_FUNCTION_DECL_P (expression) |
29347 | && DECL_LANG_SPECIFIC (expression) |
29348 | && DECL_TEMPLATE_INFO (expression)) |
29349 | { |
29350 | /* Consider the innermost template arguments, since those are the ones |
29351 | that come from the template-id; the template arguments for the |
29352 | enclosing class do not make it type-dependent unless they are used in |
29353 | the type of the decl. */ |
29354 | if (instantiates_primary_template_p (node: expression) |
29355 | && (any_dependent_template_arguments_p |
29356 | (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression))))) |
29357 | return true; |
29358 | } |
29359 | |
29360 | /* Otherwise, if the function decl isn't from a dependent scope, it can't be |
29361 | type-dependent. Checking this is important for functions with auto return |
29362 | type, which looks like a dependent type. */ |
29363 | if (TREE_CODE (expression) == FUNCTION_DECL |
29364 | && !(DECL_CLASS_SCOPE_P (expression) |
29365 | && dependent_type_p (DECL_CONTEXT (expression))) |
29366 | && !(DECL_LANG_SPECIFIC (expression) |
29367 | && DECL_UNIQUE_FRIEND_P (expression) |
29368 | && (!DECL_FRIEND_CONTEXT (expression) |
29369 | || dependent_type_p (DECL_FRIEND_CONTEXT (expression)))) |
29370 | && !DECL_LOCAL_DECL_P (expression)) |
29371 | { |
29372 | gcc_assert (!dependent_type_p (TREE_TYPE (expression)) |
29373 | || undeduced_auto_decl (expression)); |
29374 | return false; |
29375 | } |
29376 | |
29377 | /* Otherwise, its constraints could still depend on outer template parameters |
29378 | from its (dependent) scope. */ |
29379 | if (TREE_CODE (expression) == FUNCTION_DECL |
29380 | /* As an optimization, check this cheaper sufficient condition first. |
29381 | (At this point we've established that we're looking at a member of |
29382 | a dependent class, so it makes sense to start treating say undeduced |
29383 | auto as dependent.) */ |
29384 | && !dependent_type_p (TREE_TYPE (expression)) |
29385 | && uses_outer_template_parms_in_constraints (decl: expression)) |
29386 | return true; |
29387 | |
29388 | /* Always dependent, on the number of arguments if nothing else. */ |
29389 | if (TREE_CODE (expression) == EXPR_PACK_EXPANSION) |
29390 | return true; |
29391 | |
29392 | /* [temp.dep.expr]: "A pack-index-expression is type-dependent if its |
29393 | id-expression is type-dependent." */ |
29394 | if (TREE_CODE (expression) == PACK_INDEX_EXPR) |
29395 | return type_dependent_expression_p (PACK_INDEX_PACK (expression)); |
29396 | |
29397 | if (TREE_TYPE (expression) == unknown_type_node) |
29398 | { |
29399 | if (TREE_CODE (expression) == ADDR_EXPR) |
29400 | return type_dependent_expression_p (TREE_OPERAND (expression, 0)); |
29401 | if (TREE_CODE (expression) == COMPONENT_REF |
29402 | || TREE_CODE (expression) == OFFSET_REF) |
29403 | { |
29404 | if (type_dependent_object_expression_p (TREE_OPERAND (expression, 0))) |
29405 | return true; |
29406 | expression = TREE_OPERAND (expression, 1); |
29407 | if (identifier_p (t: expression)) |
29408 | return false; |
29409 | } |
29410 | /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */ |
29411 | if (TREE_CODE (expression) == SCOPE_REF) |
29412 | return false; |
29413 | |
29414 | if (BASELINK_P (expression)) |
29415 | { |
29416 | if (BASELINK_OPTYPE (expression) |
29417 | && dependent_type_p (BASELINK_OPTYPE (expression))) |
29418 | return true; |
29419 | expression = BASELINK_FUNCTIONS (expression); |
29420 | } |
29421 | |
29422 | if (TREE_CODE (expression) == TEMPLATE_ID_EXPR) |
29423 | { |
29424 | tree args = TREE_OPERAND (expression, 1); |
29425 | if (any_dependent_template_arguments_p (args)) |
29426 | return true; |
29427 | /* Arguments of a function template-id aren't necessarily coerced |
29428 | yet so we must conservatively assume that the address (and not |
29429 | just value) of the argument matters as per [temp.dep.temp]/3. */ |
29430 | for (tree arg : tree_vec_range (args)) |
29431 | if (has_value_dependent_address (op: arg)) |
29432 | return true; |
29433 | expression = TREE_OPERAND (expression, 0); |
29434 | if (identifier_p (t: expression)) |
29435 | return true; |
29436 | } |
29437 | |
29438 | gcc_assert (OVL_P (expression)); |
29439 | |
29440 | for (lkp_iterator iter (expression); iter; ++iter) |
29441 | if (type_dependent_expression_p (expression: *iter)) |
29442 | return true; |
29443 | |
29444 | return false; |
29445 | } |
29446 | |
29447 | /* The type of a non-type template parm declared with a placeholder type |
29448 | depends on the corresponding template argument, even though |
29449 | placeholders are not normally considered dependent. */ |
29450 | if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX |
29451 | && is_auto (TREE_TYPE (expression))) |
29452 | return true; |
29453 | |
29454 | gcc_assert (TREE_CODE (expression) != TYPE_DECL); |
29455 | |
29456 | /* Dependent type attributes might not have made it from the decl to |
29457 | the type yet. */ |
29458 | if (DECL_P (expression) |
29459 | && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression))) |
29460 | return true; |
29461 | |
29462 | return (dependent_type_p (TREE_TYPE (expression))); |
29463 | } |
29464 | |
29465 | /* [temp.dep.expr]/5: A class member access expression (5.2.5) is |
29466 | type-dependent if the expression refers to a member of the current |
29467 | instantiation and the type of the referenced member is dependent, or the |
29468 | class member access expression refers to a member of an unknown |
29469 | specialization. |
29470 | |
29471 | This function returns true if the OBJECT in such a class member access |
29472 | expression is of an unknown specialization. */ |
29473 | |
29474 | bool |
29475 | type_dependent_object_expression_p (tree object) |
29476 | { |
29477 | /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still |
29478 | dependent. */ |
29479 | if (TREE_CODE (object) == IDENTIFIER_NODE) |
29480 | return true; |
29481 | tree scope = TREE_TYPE (object); |
29482 | return (!scope || dependent_scope_p (scope)); |
29483 | } |
29484 | |
29485 | /* walk_tree callback function for instantiation_dependent_expression_p, |
29486 | below. Returns non-zero if a dependent subexpression is found. */ |
29487 | |
29488 | static tree |
29489 | instantiation_dependent_r (tree *tp, int *walk_subtrees, |
29490 | void * /*data*/) |
29491 | { |
29492 | if (TYPE_P (*tp)) |
29493 | { |
29494 | /* We don't have to worry about decltype currently because decltype |
29495 | of an instantiation-dependent expr is a dependent type. This |
29496 | might change depending on the resolution of DR 1172. */ |
29497 | *walk_subtrees = false; |
29498 | return NULL_TREE; |
29499 | } |
29500 | enum tree_code code = TREE_CODE (*tp); |
29501 | switch (code) |
29502 | { |
29503 | /* Don't treat an argument list as dependent just because it has no |
29504 | TREE_TYPE. */ |
29505 | case TREE_LIST: |
29506 | case TREE_VEC: |
29507 | case NONTYPE_ARGUMENT_PACK: |
29508 | return NULL_TREE; |
29509 | |
29510 | case TEMPLATE_PARM_INDEX: |
29511 | if (dependent_type_p (TREE_TYPE (*tp))) |
29512 | return *tp; |
29513 | if (TEMPLATE_PARM_PARAMETER_PACK (*tp)) |
29514 | return *tp; |
29515 | /* We'll check value-dependence separately. */ |
29516 | return NULL_TREE; |
29517 | |
29518 | /* Handle expressions with type operands. */ |
29519 | case SIZEOF_EXPR: |
29520 | case ALIGNOF_EXPR: |
29521 | case TYPEID_EXPR: |
29522 | case AT_ENCODE_EXPR: |
29523 | { |
29524 | tree op = TREE_OPERAND (*tp, 0); |
29525 | if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp)) |
29526 | op = TREE_TYPE (op); |
29527 | if (TYPE_P (op)) |
29528 | { |
29529 | if (dependent_type_p (type: op)) |
29530 | return *tp; |
29531 | else |
29532 | { |
29533 | *walk_subtrees = false; |
29534 | return NULL_TREE; |
29535 | } |
29536 | } |
29537 | break; |
29538 | } |
29539 | |
29540 | case COMPONENT_REF: |
29541 | if (identifier_p (TREE_OPERAND (*tp, 1))) |
29542 | /* In a template, finish_class_member_access_expr creates a |
29543 | COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't |
29544 | type-dependent, so that we can check access control at |
29545 | instantiation time (PR 42277). See also Core issue 1273. */ |
29546 | return *tp; |
29547 | break; |
29548 | |
29549 | case SCOPE_REF: |
29550 | if (instantiation_dependent_scope_ref_p (t: *tp)) |
29551 | return *tp; |
29552 | else |
29553 | break; |
29554 | |
29555 | /* Treat statement-expressions as dependent. */ |
29556 | case BIND_EXPR: |
29557 | return *tp; |
29558 | |
29559 | /* Treat requires-expressions as dependent. */ |
29560 | case REQUIRES_EXPR: |
29561 | return *tp; |
29562 | |
29563 | case CONSTRUCTOR: |
29564 | if (CONSTRUCTOR_IS_DEPENDENT (*tp)) |
29565 | return *tp; |
29566 | break; |
29567 | |
29568 | case TEMPLATE_DECL: |
29569 | case FUNCTION_DECL: |
29570 | /* Before C++17, a noexcept-specifier isn't part of the function type |
29571 | so it doesn't affect type dependence, but we still want to consider it |
29572 | for instantiation dependence. */ |
29573 | if (cxx_dialect < cxx17 |
29574 | && DECL_DECLARES_FUNCTION_P (*tp) |
29575 | && value_dependent_noexcept_spec_p (TREE_TYPE (*tp))) |
29576 | return *tp; |
29577 | break; |
29578 | |
29579 | default: |
29580 | break; |
29581 | } |
29582 | |
29583 | if (type_dependent_expression_p (expression: *tp)) |
29584 | return *tp; |
29585 | else |
29586 | return NULL_TREE; |
29587 | } |
29588 | |
29589 | /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the |
29590 | sense defined by the ABI: |
29591 | |
29592 | "An expression is instantiation-dependent if it is type-dependent |
29593 | or value-dependent, or it has a subexpression that is type-dependent |
29594 | or value-dependent." |
29595 | |
29596 | Except don't actually check value-dependence for unevaluated expressions, |
29597 | because in sizeof(i) we don't care about the value of i. Checking |
29598 | type-dependence will in turn check value-dependence of array bounds/template |
29599 | arguments as needed. */ |
29600 | |
29601 | bool |
29602 | instantiation_dependent_uneval_expression_p (tree expression) |
29603 | { |
29604 | tree result; |
29605 | |
29606 | if (!processing_template_decl) |
29607 | return false; |
29608 | |
29609 | if (expression == error_mark_node) |
29610 | return false; |
29611 | |
29612 | result = cp_walk_tree_without_duplicates (&expression, |
29613 | instantiation_dependent_r, NULL); |
29614 | return result != NULL_TREE; |
29615 | } |
29616 | |
29617 | /* As above, but also check value-dependence of the expression as a whole. */ |
29618 | |
29619 | bool |
29620 | instantiation_dependent_expression_p (tree expression) |
29621 | { |
29622 | return (instantiation_dependent_uneval_expression_p (expression) |
29623 | || (processing_template_decl |
29624 | && potential_constant_expression (expression) |
29625 | && value_dependent_expression_p (expression))); |
29626 | } |
29627 | |
29628 | /* Like type_dependent_expression_p, but it also works while not processing |
29629 | a template definition, i.e. during substitution or mangling. */ |
29630 | |
29631 | bool |
29632 | type_dependent_expression_p_push (tree expr) |
29633 | { |
29634 | bool b; |
29635 | ++processing_template_decl; |
29636 | b = type_dependent_expression_p (expression: expr); |
29637 | --processing_template_decl; |
29638 | return b; |
29639 | } |
29640 | |
29641 | /* Returns TRUE if ARGS contains a type-dependent expression. */ |
29642 | |
29643 | bool |
29644 | any_type_dependent_arguments_p (const vec<tree, va_gc> *args) |
29645 | { |
29646 | if (!processing_template_decl || !args) |
29647 | return false; |
29648 | |
29649 | for (tree arg : *args) |
29650 | if (type_dependent_expression_p (expression: arg)) |
29651 | return true; |
29652 | |
29653 | return false; |
29654 | } |
29655 | |
29656 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
29657 | expressions) contains any type-dependent expressions. */ |
29658 | |
29659 | bool |
29660 | any_type_dependent_elements_p (const_tree list) |
29661 | { |
29662 | for (; list; list = TREE_CHAIN (list)) |
29663 | if (type_dependent_expression_p (TREE_VALUE (list))) |
29664 | return true; |
29665 | |
29666 | return false; |
29667 | } |
29668 | |
29669 | /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are |
29670 | expressions) contains any value-dependent expressions. */ |
29671 | |
29672 | bool |
29673 | any_value_dependent_elements_p (const_tree list) |
29674 | { |
29675 | for (; list; list = TREE_CHAIN (list)) |
29676 | if (value_dependent_expression_p (TREE_VALUE (list))) |
29677 | return true; |
29678 | |
29679 | return false; |
29680 | } |
29681 | |
29682 | /* Returns TRUE if the ARG (a template argument) is dependent. */ |
29683 | |
29684 | bool |
29685 | dependent_template_arg_p (tree arg) |
29686 | { |
29687 | if (!processing_template_decl) |
29688 | return false; |
29689 | |
29690 | /* Assume a template argument that was wrongly written by the user |
29691 | is dependent. This is consistent with what |
29692 | any_dependent_template_arguments_p [that calls this function] |
29693 | does. */ |
29694 | if (!arg || arg == error_mark_node) |
29695 | return true; |
29696 | |
29697 | if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT) |
29698 | arg = argument_pack_select_arg (t: arg); |
29699 | |
29700 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) |
29701 | return true; |
29702 | if (TREE_CODE (arg) == TEMPLATE_DECL) |
29703 | { |
29704 | if (DECL_TEMPLATE_PARM_P (arg)) |
29705 | return true; |
29706 | /* A member template of a dependent class is not necessarily |
29707 | type-dependent, but it is a dependent template argument because it |
29708 | will be a member of an unknown specialization to that template. */ |
29709 | tree scope = CP_DECL_CONTEXT (arg); |
29710 | return TYPE_P (scope) && dependent_type_p (type: scope); |
29711 | } |
29712 | else if (ARGUMENT_PACK_P (arg)) |
29713 | { |
29714 | tree args = ARGUMENT_PACK_ARGS (arg); |
29715 | for (tree arg : tree_vec_range (args)) |
29716 | if (dependent_template_arg_p (arg)) |
29717 | return true; |
29718 | return false; |
29719 | } |
29720 | else if (TYPE_P (arg)) |
29721 | return dependent_type_p (type: arg); |
29722 | else |
29723 | return value_dependent_expression_p (expression: arg); |
29724 | } |
29725 | |
29726 | /* Identify any expressions that use function parms. */ |
29727 | |
29728 | static tree |
29729 | find_parm_usage_r (tree *tp, int *walk_subtrees, void*) |
29730 | { |
29731 | tree t = *tp; |
29732 | if (TREE_CODE (t) == PARM_DECL) |
29733 | { |
29734 | *walk_subtrees = 0; |
29735 | return t; |
29736 | } |
29737 | return NULL_TREE; |
29738 | } |
29739 | |
29740 | /* Returns true if a type specialization formed using the template |
29741 | arguments ARGS needs to use structural equality. */ |
29742 | |
29743 | bool |
29744 | any_template_arguments_need_structural_equality_p (tree args) |
29745 | { |
29746 | int i; |
29747 | int j; |
29748 | |
29749 | if (!args) |
29750 | return false; |
29751 | if (args == error_mark_node) |
29752 | return true; |
29753 | |
29754 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
29755 | { |
29756 | tree level = TMPL_ARGS_LEVEL (args, i + 1); |
29757 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
29758 | { |
29759 | tree arg = TREE_VEC_ELT (level, j); |
29760 | tree packed_args = NULL_TREE; |
29761 | int k, len = 1; |
29762 | |
29763 | if (ARGUMENT_PACK_P (arg)) |
29764 | { |
29765 | /* Look inside the argument pack. */ |
29766 | packed_args = ARGUMENT_PACK_ARGS (arg); |
29767 | len = TREE_VEC_LENGTH (packed_args); |
29768 | } |
29769 | |
29770 | for (k = 0; k < len; ++k) |
29771 | { |
29772 | if (packed_args) |
29773 | arg = TREE_VEC_ELT (packed_args, k); |
29774 | |
29775 | if (error_operand_p (t: arg)) |
29776 | return true; |
29777 | else if (TREE_CODE (arg) == TEMPLATE_DECL) |
29778 | continue; |
29779 | else if (arg == any_targ_node) |
29780 | /* An any_targ_node argument (added by add_defaults_to_ttp) |
29781 | makes the corresponding specialization not canonicalizable, |
29782 | since template_args_equal always return true for it. We |
29783 | may see this when called from bind_template_template_parm. */ |
29784 | return true; |
29785 | /* Checking current_function_decl because this structural |
29786 | comparison is only necessary for redeclaration. */ |
29787 | else if (!current_function_decl |
29788 | && dependent_template_arg_p (arg) |
29789 | && (cp_walk_tree_without_duplicates |
29790 | (&arg, find_parm_usage_r, NULL))) |
29791 | /* The identity of a class template specialization that uses |
29792 | a function parameter depends on the identity of the function. |
29793 | And if this specialization appeared in the trailing return |
29794 | type thereof, we don't know the identity of the function |
29795 | (e.g. if it's a redeclaration or a new function) until we |
29796 | form its signature and go through duplicate_decls. Thus |
29797 | it's unsafe to decide on a canonical type now (which depends |
29798 | on the DECL_CONTEXT of the function parameter, which can get |
29799 | mutated after the fact by duplicate_decls), so just require |
29800 | structural equality in this case (PR52830). */ |
29801 | return true; |
29802 | else if (TYPE_P (arg) |
29803 | && TYPE_STRUCTURAL_EQUALITY_P (arg) |
29804 | && (dependent_alias_template_spec_p (t: arg, transparent_typedefs: nt_opaque) |
29805 | || dependent_opaque_alias_p (t: arg))) |
29806 | /* Require structural equality for specializations written |
29807 | in terms of a dependent alias template specialization. */ |
29808 | return true; |
29809 | else if (CLASS_TYPE_P (arg) |
29810 | && TYPE_TEMPLATE_INFO (arg) |
29811 | && TYPE_STRUCTURAL_EQUALITY_P (arg)) |
29812 | /* Require structural equality for specializations written |
29813 | in terms of a class template specialization that itself |
29814 | needs structural equality. */ |
29815 | return true; |
29816 | } |
29817 | } |
29818 | } |
29819 | |
29820 | return false; |
29821 | } |
29822 | |
29823 | /* Returns true if ARGS (a collection of template arguments) contains |
29824 | any dependent arguments. */ |
29825 | |
29826 | bool |
29827 | any_dependent_template_arguments_p (const_tree args) |
29828 | { |
29829 | if (args == error_mark_node) |
29830 | return true; |
29831 | if (!processing_template_decl || !args) |
29832 | return false; |
29833 | |
29834 | for (int i = 0, depth = TMPL_ARGS_DEPTH (args); i < depth; ++i) |
29835 | { |
29836 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
29837 | for (tree arg : tree_vec_range (CONST_CAST_TREE (level))) |
29838 | if (dependent_template_arg_p (arg)) |
29839 | return true; |
29840 | } |
29841 | |
29842 | return false; |
29843 | } |
29844 | |
29845 | /* Returns true if ARGS contains any errors. */ |
29846 | |
29847 | bool |
29848 | any_erroneous_template_args_p (const_tree args) |
29849 | { |
29850 | int i; |
29851 | int j; |
29852 | |
29853 | if (args == error_mark_node) |
29854 | return true; |
29855 | |
29856 | if (args && TREE_CODE (args) != TREE_VEC) |
29857 | { |
29858 | if (tree ti = get_template_info (t: args)) |
29859 | args = TI_ARGS (ti); |
29860 | else |
29861 | args = NULL_TREE; |
29862 | } |
29863 | |
29864 | if (!args) |
29865 | return false; |
29866 | |
29867 | for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i) |
29868 | { |
29869 | const_tree level = TMPL_ARGS_LEVEL (args, i + 1); |
29870 | for (j = 0; j < TREE_VEC_LENGTH (level); ++j) |
29871 | if (error_operand_p (TREE_VEC_ELT (level, j))) |
29872 | return true; |
29873 | } |
29874 | |
29875 | return false; |
29876 | } |
29877 | |
29878 | /* Returns TRUE if the template TMPL is type-dependent. */ |
29879 | |
29880 | bool |
29881 | dependent_template_p (tree tmpl) |
29882 | { |
29883 | if (TREE_CODE (tmpl) == OVERLOAD) |
29884 | { |
29885 | for (lkp_iterator iter (tmpl); iter; ++iter) |
29886 | if (dependent_template_p (tmpl: *iter)) |
29887 | return true; |
29888 | return false; |
29889 | } |
29890 | |
29891 | /* Template template parameters are dependent. */ |
29892 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl) |
29893 | || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM) |
29894 | return true; |
29895 | /* So are names that have not been looked up. */ |
29896 | if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (t: tmpl)) |
29897 | return true; |
29898 | return false; |
29899 | } |
29900 | |
29901 | /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */ |
29902 | |
29903 | bool |
29904 | dependent_template_id_p (tree tmpl, tree args) |
29905 | { |
29906 | return (dependent_template_p (tmpl) |
29907 | || any_dependent_template_arguments_p (args)); |
29908 | } |
29909 | |
29910 | /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors |
29911 | are dependent. BODY is the body to use for loop transforming |
29912 | constructs. */ |
29913 | |
29914 | bool |
29915 | dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv, tree body) |
29916 | { |
29917 | int i, k; |
29918 | |
29919 | if (!processing_template_decl) |
29920 | return false; |
29921 | |
29922 | for (i = 0, k = 0; i < TREE_VEC_LENGTH (declv); i++) |
29923 | { |
29924 | tree decl = TREE_VEC_ELT (declv, i); |
29925 | tree init = TREE_VEC_ELT (initv, i); |
29926 | tree cond = TREE_VEC_ELT (condv, i); |
29927 | tree incr = TREE_VEC_ELT (incrv, i); |
29928 | |
29929 | if (decl == NULL_TREE) |
29930 | { |
29931 | tree stmt = body; |
29932 | int j = c_omp_find_generated_loop (stmt, k++, cp_walk_subtrees); |
29933 | init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j); |
29934 | decl = TREE_OPERAND (init, 0); |
29935 | cond = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j); |
29936 | incr = TREE_VEC_ELT (OMP_FOR_INIT (stmt), j); |
29937 | } |
29938 | |
29939 | if (type_dependent_expression_p (expression: decl) |
29940 | || TREE_CODE (decl) == SCOPE_REF) |
29941 | return true; |
29942 | |
29943 | if (init && type_dependent_expression_p (expression: init)) |
29944 | return true; |
29945 | |
29946 | if (cond == global_namespace) |
29947 | return true; |
29948 | |
29949 | if (type_dependent_expression_p (expression: cond)) |
29950 | return true; |
29951 | |
29952 | if (COMPARISON_CLASS_P (cond) |
29953 | && (type_dependent_expression_p (TREE_OPERAND (cond, 0)) |
29954 | || type_dependent_expression_p (TREE_OPERAND (cond, 1)))) |
29955 | return true; |
29956 | |
29957 | if (TREE_CODE (incr) == MODOP_EXPR) |
29958 | { |
29959 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0)) |
29960 | || type_dependent_expression_p (TREE_OPERAND (incr, 2))) |
29961 | return true; |
29962 | } |
29963 | else if (type_dependent_expression_p (expression: incr)) |
29964 | return true; |
29965 | else if (TREE_CODE (incr) == MODIFY_EXPR) |
29966 | { |
29967 | if (type_dependent_expression_p (TREE_OPERAND (incr, 0))) |
29968 | return true; |
29969 | else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1))) |
29970 | { |
29971 | tree t = TREE_OPERAND (incr, 1); |
29972 | if (type_dependent_expression_p (TREE_OPERAND (t, 0)) |
29973 | || type_dependent_expression_p (TREE_OPERAND (t, 1))) |
29974 | return true; |
29975 | |
29976 | /* If this loop has a class iterator with != comparison |
29977 | with increment other than i++/++i/i--/--i, make sure the |
29978 | increment is constant. */ |
29979 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
29980 | && TREE_CODE (cond) == NE_EXPR) |
29981 | { |
29982 | if (TREE_OPERAND (t, 0) == decl) |
29983 | t = TREE_OPERAND (t, 1); |
29984 | else |
29985 | t = TREE_OPERAND (t, 0); |
29986 | if (TREE_CODE (t) != INTEGER_CST) |
29987 | return true; |
29988 | } |
29989 | } |
29990 | } |
29991 | } |
29992 | |
29993 | return false; |
29994 | } |
29995 | |
29996 | /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the |
29997 | TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if |
29998 | no such TYPE can be found. Note that this function peers inside |
29999 | uninstantiated templates and therefore should be used only in |
30000 | extremely limited situations. ONLY_CURRENT_P restricts this |
30001 | peering to the currently open classes hierarchy (which is required |
30002 | when comparing types). */ |
30003 | |
30004 | tree |
30005 | resolve_typename_type (tree type, bool only_current_p) |
30006 | { |
30007 | tree scope; |
30008 | tree name; |
30009 | tree decl; |
30010 | int quals; |
30011 | tree pushed_scope; |
30012 | tree result; |
30013 | |
30014 | gcc_assert (TREE_CODE (type) == TYPENAME_TYPE); |
30015 | |
30016 | scope = TYPE_CONTEXT (type); |
30017 | /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */ |
30018 | gcc_checking_assert (uses_template_parms (scope)); |
30019 | |
30020 | /* Usually the non-qualified identifier of a TYPENAME_TYPE is |
30021 | TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a |
30022 | TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL |
30023 | representing the typedef. In that case TYPE_IDENTIFIER (type) is |
30024 | not the non-qualified identifier of the TYPENAME_TYPE anymore. |
30025 | So by getting the TYPE_IDENTIFIER of the _main declaration_ of |
30026 | the TYPENAME_TYPE instead, we avoid messing up with a possible |
30027 | typedef variant case. */ |
30028 | name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); |
30029 | |
30030 | /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve |
30031 | it first before we can figure out what NAME refers to. */ |
30032 | if (TREE_CODE (scope) == TYPENAME_TYPE) |
30033 | { |
30034 | if (TYPENAME_IS_RESOLVING_P (scope)) |
30035 | /* Given a class template A with a dependent base with nested type C, |
30036 | typedef typename A::C::C C will land us here, as trying to resolve |
30037 | the initial A::C leads to the local C typedef, which leads back to |
30038 | A::C::C. So we break the recursion now. */ |
30039 | return type; |
30040 | else |
30041 | scope = resolve_typename_type (type: scope, only_current_p); |
30042 | } |
30043 | /* If we don't know what SCOPE refers to, then we cannot resolve the |
30044 | TYPENAME_TYPE. */ |
30045 | if (!CLASS_TYPE_P (scope)) |
30046 | return type; |
30047 | /* If this is a typedef, we don't want to look inside (c++/11987). */ |
30048 | if (typedef_variant_p (type)) |
30049 | return type; |
30050 | /* If SCOPE isn't the template itself, it will not have a valid |
30051 | TYPE_FIELDS list. */ |
30052 | if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope))) |
30053 | /* scope is either the template itself or a compatible instantiation |
30054 | like X<T>, so look up the name in the original template. */ |
30055 | scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope); |
30056 | /* If scope has no fields, it can't be a current instantiation. Check this |
30057 | before currently_open_class to avoid infinite recursion (71515). */ |
30058 | if (!TYPE_FIELDS (scope)) |
30059 | return type; |
30060 | /* If the SCOPE is not the current instantiation, there's no reason |
30061 | to look inside it. */ |
30062 | if (only_current_p && !currently_open_class (scope)) |
30063 | return type; |
30064 | /* Enter the SCOPE so that name lookup will be resolved as if we |
30065 | were in the class definition. In particular, SCOPE will no |
30066 | longer be considered a dependent type. */ |
30067 | pushed_scope = push_scope (scope); |
30068 | /* Look up the declaration. */ |
30069 | decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true, |
30070 | tf_warning_or_error); |
30071 | |
30072 | result = NULL_TREE; |
30073 | |
30074 | /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to |
30075 | find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */ |
30076 | tree fullname = TYPENAME_TYPE_FULLNAME (type); |
30077 | if (!decl) |
30078 | /*nop*/; |
30079 | else if (identifier_p (t: fullname) |
30080 | && TREE_CODE (decl) == TYPE_DECL) |
30081 | { |
30082 | result = TREE_TYPE (decl); |
30083 | if (result == error_mark_node) |
30084 | result = NULL_TREE; |
30085 | } |
30086 | else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR |
30087 | && DECL_CLASS_TEMPLATE_P (decl)) |
30088 | { |
30089 | /* Obtain the template and the arguments. */ |
30090 | tree tmpl = TREE_OPERAND (fullname, 0); |
30091 | if (TREE_CODE (tmpl) == IDENTIFIER_NODE) |
30092 | { |
30093 | /* We get here with a plain identifier because a previous tentative |
30094 | parse of the nested-name-specifier as part of a ptr-operator saw |
30095 | ::template X<A>. The use of ::template is necessary in a |
30096 | ptr-operator, but wrong in a declarator-id. |
30097 | |
30098 | [temp.names]: In a qualified-id of a declarator-id, the keyword |
30099 | template shall not appear at the top level. */ |
30100 | pedwarn (cp_expr_loc_or_input_loc (t: fullname), OPT_Wpedantic, |
30101 | "keyword %<template%> not allowed in declarator-id"); |
30102 | tmpl = decl; |
30103 | } |
30104 | tree args = TREE_OPERAND (fullname, 1); |
30105 | /* Instantiate the template. */ |
30106 | result = lookup_template_class (d1: tmpl, arglist: args, NULL_TREE, NULL_TREE, |
30107 | complain: tf_error | tf_user); |
30108 | result = adjust_type_for_entering_scope (type: result); |
30109 | if (result == error_mark_node) |
30110 | result = NULL_TREE; |
30111 | } |
30112 | |
30113 | /* Leave the SCOPE. */ |
30114 | if (pushed_scope) |
30115 | pop_scope (pushed_scope); |
30116 | |
30117 | /* If we failed to resolve it, return the original typename. */ |
30118 | if (!result) |
30119 | return type; |
30120 | |
30121 | /* If lookup found a typename type, resolve that too. */ |
30122 | if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result)) |
30123 | { |
30124 | /* Ill-formed programs can cause infinite recursion here, so we |
30125 | must catch that. */ |
30126 | TYPENAME_IS_RESOLVING_P (result) = 1; |
30127 | result = resolve_typename_type (type: result, only_current_p); |
30128 | TYPENAME_IS_RESOLVING_P (result) = 0; |
30129 | } |
30130 | |
30131 | /* Qualify the resulting type. */ |
30132 | quals = cp_type_quals (type); |
30133 | if (quals) |
30134 | result = cp_build_qualified_type (result, cp_type_quals (result) | quals); |
30135 | |
30136 | return result; |
30137 | } |
30138 | |
30139 | /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a |
30140 | TEMPLATE_TYPE_PARM with a level one deeper than the actual template parms, |
30141 | by default. If set_canonical is true, we set TYPE_CANONICAL on it. */ |
30142 | |
30143 | static tree |
30144 | make_auto_1 (tree name, bool set_canonical, int level = -1) |
30145 | { |
30146 | if (level == -1) |
30147 | level = current_template_depth + 1; |
30148 | tree au = cxx_make_type (TEMPLATE_TYPE_PARM); |
30149 | TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au); |
30150 | TYPE_STUB_DECL (au) = TYPE_NAME (au); |
30151 | TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index |
30152 | (index: 0, level, orig_level: level, TYPE_NAME (au), NULL_TREE); |
30153 | if (set_canonical) |
30154 | TYPE_CANONICAL (au) = canonical_type_parameter (type: au); |
30155 | DECL_ARTIFICIAL (TYPE_NAME (au)) = 1; |
30156 | SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au)); |
30157 | if (name == decltype_auto_identifier) |
30158 | AUTO_IS_DECLTYPE (au) = true; |
30159 | |
30160 | return au; |
30161 | } |
30162 | |
30163 | tree |
30164 | make_decltype_auto (void) |
30165 | { |
30166 | return make_auto_1 (decltype_auto_identifier, set_canonical: true); |
30167 | } |
30168 | |
30169 | tree |
30170 | make_auto (void) |
30171 | { |
30172 | return make_auto_1 (auto_identifier, set_canonical: true); |
30173 | } |
30174 | |
30175 | /* Return a C++17 deduction placeholder for class template TMPL. |
30176 | There are represented as an 'auto' with the special level 0 and |
30177 | CLASS_PLACEHOLDER_TEMPLATE set. */ |
30178 | |
30179 | tree |
30180 | make_template_placeholder (tree tmpl) |
30181 | { |
30182 | tree t = make_auto_1 (auto_identifier, set_canonical: false, /*level=*/0); |
30183 | CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl; |
30184 | /* Our canonical type depends on the placeholder. */ |
30185 | TYPE_CANONICAL (t) = canonical_type_parameter (type: t); |
30186 | return t; |
30187 | } |
30188 | |
30189 | /* True iff T is a C++17 class template deduction placeholder. */ |
30190 | |
30191 | bool |
30192 | template_placeholder_p (tree t) |
30193 | { |
30194 | return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t); |
30195 | } |
30196 | |
30197 | /* Return an auto for an explicit cast expression auto(x). |
30198 | Like CTAD placeholders, these have level 0 so that they're |
30199 | not accidentally replaced via tsubst and are always directly |
30200 | resolved via do_auto_deduction. */ |
30201 | |
30202 | tree |
30203 | make_cast_auto () |
30204 | { |
30205 | return make_auto_1 (auto_identifier, set_canonical: true, /*level=*/0); |
30206 | } |
30207 | |
30208 | /* Make a "constrained auto" type-specifier. This is an auto or |
30209 | decltype(auto) type with constraints that must be associated after |
30210 | deduction. The constraint is formed from the given concept CON |
30211 | and its optional sequence of template arguments ARGS. |
30212 | |
30213 | TYPE must be the result of make_auto_type or make_decltype_auto_type. */ |
30214 | |
30215 | static tree |
30216 | make_constrained_placeholder_type (tree type, tree con, tree args) |
30217 | { |
30218 | /* Build the constraint. */ |
30219 | tree tmpl = DECL_TI_TEMPLATE (con); |
30220 | ++processing_template_decl; |
30221 | tree expr = build_concept_check (tmpl, type, args, tf_warning_or_error); |
30222 | --processing_template_decl; |
30223 | |
30224 | PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type) |
30225 | = build_tree_list (current_template_parms, expr); |
30226 | |
30227 | /* Our canonical type depends on the constraint. */ |
30228 | TYPE_CANONICAL (type) = canonical_type_parameter (type); |
30229 | |
30230 | /* Attach the constraint to the type declaration. */ |
30231 | return TYPE_NAME (type); |
30232 | } |
30233 | |
30234 | /* Make a "constrained auto" type-specifier. */ |
30235 | |
30236 | tree |
30237 | make_constrained_auto (tree con, tree args) |
30238 | { |
30239 | tree type = make_auto_1 (auto_identifier, set_canonical: false); |
30240 | return make_constrained_placeholder_type (type, con, args); |
30241 | } |
30242 | |
30243 | /* Make a "constrained decltype(auto)" type-specifier. */ |
30244 | |
30245 | tree |
30246 | make_constrained_decltype_auto (tree con, tree args) |
30247 | { |
30248 | tree type = make_auto_1 (decltype_auto_identifier, set_canonical: false); |
30249 | return make_constrained_placeholder_type (type, con, args); |
30250 | } |
30251 | |
30252 | /* Returns true if the placeholder type constraint T has any dependent |
30253 | (explicit) template arguments. */ |
30254 | |
30255 | static bool |
30256 | placeholder_type_constraint_dependent_p (tree t) |
30257 | { |
30258 | gcc_assert (concept_check_p (t)); |
30259 | tree args = TREE_OPERAND (t, 1); |
30260 | tree first = TREE_VEC_ELT (args, 0); |
30261 | if (ARGUMENT_PACK_P (first)) |
30262 | { |
30263 | args = expand_template_argument_pack (args); |
30264 | first = TREE_VEC_ELT (args, 0); |
30265 | } |
30266 | gcc_checking_assert (is_auto (first)); |
30267 | for (int i = 1; i < TREE_VEC_LENGTH (args); ++i) |
30268 | if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) |
30269 | return true; |
30270 | return false; |
30271 | } |
30272 | |
30273 | /* Prepare and return a concept definition. */ |
30274 | |
30275 | tree |
30276 | start_concept_definition (cp_expr id) |
30277 | { |
30278 | gcc_assert (identifier_p (id)); |
30279 | gcc_assert (processing_template_decl); |
30280 | |
30281 | location_t loc = id.get_location(); |
30282 | |
30283 | /* A concept-definition shall not have associated constraints. */ |
30284 | if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)) |
30285 | { |
30286 | error_at (loc, "a concept cannot be constrained"); |
30287 | TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE; |
30288 | } |
30289 | |
30290 | /* A concept-definition shall appear in namespace scope. Templates |
30291 | aren't allowed in block scope, so we only need to check for class |
30292 | scope. */ |
30293 | if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ())) |
30294 | { |
30295 | error_at (loc, "concept %qE not in namespace scope", *id); |
30296 | return error_mark_node; |
30297 | } |
30298 | |
30299 | if (current_template_depth > 1) |
30300 | { |
30301 | error_at (loc, "concept %qE has multiple template parameter lists", *id); |
30302 | return error_mark_node; |
30303 | } |
30304 | |
30305 | /* Initially build the concept declaration; its type is bool. */ |
30306 | tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node); |
30307 | DECL_CONTEXT (decl) = current_scope (); |
30308 | TREE_PUBLIC (decl) = true; |
30309 | |
30310 | return decl; |
30311 | } |
30312 | |
30313 | /* Finish building a concept definition. Like other templates, the |
30314 | CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the |
30315 | the TEMPLATE_DECL. */ |
30316 | |
30317 | tree |
30318 | finish_concept_definition (tree decl, tree init, tree attrs) |
30319 | { |
30320 | DECL_INITIAL (decl) = init; |
30321 | |
30322 | if (attrs) |
30323 | cplus_decl_attributes (&decl, attrs, 0); |
30324 | |
30325 | set_originating_module (decl, friend_p: false); |
30326 | check_module_decl_linkage (decl); |
30327 | |
30328 | /* Push the enclosing template. */ |
30329 | return push_template_decl (decl); |
30330 | } |
30331 | |
30332 | /* Given type ARG, return std::initializer_list<ARG>. */ |
30333 | |
30334 | static tree |
30335 | listify (tree arg) |
30336 | { |
30337 | tree std_init_list = lookup_qualified_name (std_node, init_list_identifier); |
30338 | |
30339 | if (std_init_list == error_mark_node |
30340 | || !DECL_CLASS_TEMPLATE_P (std_init_list)) |
30341 | { |
30342 | gcc_rich_location richloc (input_location); |
30343 | maybe_add_include_fixit (&richloc, "<initializer_list>", false); |
30344 | error_at (&richloc, |
30345 | "deducing from brace-enclosed initializer list" |
30346 | " requires %<#include <initializer_list>%>"); |
30347 | |
30348 | return error_mark_node; |
30349 | } |
30350 | tree argvec = make_tree_vec (1); |
30351 | TREE_VEC_ELT (argvec, 0) = arg; |
30352 | |
30353 | return lookup_template_class (d1: std_init_list, arglist: argvec, NULL_TREE, |
30354 | NULL_TREE, complain: tf_warning_or_error); |
30355 | } |
30356 | |
30357 | /* Replace auto in TYPE with std::initializer_list<auto>. */ |
30358 | |
30359 | static tree |
30360 | listify_autos (tree type, tree auto_node) |
30361 | { |
30362 | tree init_auto = listify (arg: strip_top_quals (auto_node)); |
30363 | tree argvec = make_tree_vec (1); |
30364 | TREE_VEC_ELT (argvec, 0) = init_auto; |
30365 | if (processing_template_decl) |
30366 | argvec = add_to_template_args (args: current_template_args (), extra_args: argvec); |
30367 | return tsubst (t: type, args: argvec, complain: tf_warning_or_error, NULL_TREE); |
30368 | } |
30369 | |
30370 | /* Hash traits for hashing possibly constrained 'auto' |
30371 | TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */ |
30372 | |
30373 | struct auto_hash : default_hash_traits<tree> |
30374 | { |
30375 | static inline hashval_t hash (tree); |
30376 | static inline bool equal (tree, tree); |
30377 | }; |
30378 | |
30379 | /* Hash the 'auto' T. */ |
30380 | |
30381 | inline hashval_t |
30382 | auto_hash::hash (tree t) |
30383 | { |
30384 | if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t))) |
30385 | /* Matching constrained-type-specifiers denote the same template |
30386 | parameter, so hash the constraint. */ |
30387 | return iterative_hash_placeholder_constraint (c, 0); |
30388 | else |
30389 | /* But unconstrained autos are all separate, so just hash the pointer. */ |
30390 | return iterative_hash_object (t, 0); |
30391 | } |
30392 | |
30393 | /* Compare two 'auto's. */ |
30394 | |
30395 | inline bool |
30396 | auto_hash::equal (tree t1, tree t2) |
30397 | { |
30398 | if (t1 == t2) |
30399 | return true; |
30400 | |
30401 | tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1); |
30402 | tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2); |
30403 | |
30404 | /* Two unconstrained autos are distinct. */ |
30405 | if (!c1 || !c2) |
30406 | return false; |
30407 | |
30408 | return equivalent_placeholder_constraints (c1, c2); |
30409 | } |
30410 | |
30411 | /* The stem for deduction guide names. */ |
30412 | const char *const dguide_base = "__dguide_"; |
30413 | |
30414 | /* Return the name for a deduction guide for class template TMPL. */ |
30415 | |
30416 | tree |
30417 | dguide_name (tree tmpl) |
30418 | { |
30419 | tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl)); |
30420 | tree tname = TYPE_IDENTIFIER (type); |
30421 | char *buf = (char *) alloca (1 + strlen (dguide_base) |
30422 | + IDENTIFIER_LENGTH (tname)); |
30423 | memcpy (dest: buf, src: dguide_base, n: strlen (s: dguide_base)); |
30424 | memcpy (dest: buf + strlen (s: dguide_base), IDENTIFIER_POINTER (tname), |
30425 | IDENTIFIER_LENGTH (tname) + 1); |
30426 | tree dname = get_identifier (buf); |
30427 | TREE_TYPE (dname) = type; |
30428 | return dname; |
30429 | } |
30430 | |
30431 | /* True if NAME is the name of a deduction guide. */ |
30432 | |
30433 | bool |
30434 | dguide_name_p (tree name) |
30435 | { |
30436 | return (TREE_CODE (name) == IDENTIFIER_NODE |
30437 | && TREE_TYPE (name) |
30438 | && startswith (IDENTIFIER_POINTER (name), prefix: dguide_base)); |
30439 | } |
30440 | |
30441 | /* True if FN is a deduction guide. */ |
30442 | |
30443 | bool |
30444 | deduction_guide_p (const_tree fn) |
30445 | { |
30446 | if (DECL_P (fn)) |
30447 | if (tree name = DECL_NAME (fn)) |
30448 | return dguide_name_p (name); |
30449 | return false; |
30450 | } |
30451 | |
30452 | /* True if FN is the copy deduction guide, i.e. A(A)->A. */ |
30453 | |
30454 | bool |
30455 | copy_guide_p (const_tree fn) |
30456 | { |
30457 | gcc_assert (deduction_guide_p (fn)); |
30458 | if (!DECL_ARTIFICIAL (fn)) |
30459 | return false; |
30460 | tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn)); |
30461 | return (TREE_CHAIN (parms) == void_list_node |
30462 | && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn)))); |
30463 | } |
30464 | |
30465 | /* True if FN is a guide generated from a constructor template. */ |
30466 | |
30467 | bool |
30468 | template_guide_p (const_tree fn) |
30469 | { |
30470 | gcc_assert (deduction_guide_p (fn)); |
30471 | if (!DECL_ARTIFICIAL (fn)) |
30472 | return false; |
30473 | tree tmpl = DECL_TI_TEMPLATE (fn); |
30474 | if (tree org = DECL_ABSTRACT_ORIGIN (tmpl)) |
30475 | return PRIMARY_TEMPLATE_P (org); |
30476 | return false; |
30477 | } |
30478 | |
30479 | /* True if FN is an aggregate initialization guide or the copy deduction |
30480 | guide. */ |
30481 | |
30482 | bool |
30483 | builtin_guide_p (const_tree fn) |
30484 | { |
30485 | if (!deduction_guide_p (fn)) |
30486 | return false; |
30487 | if (!DECL_ARTIFICIAL (fn)) |
30488 | /* Explicitly declared. */ |
30489 | return false; |
30490 | if (DECL_ABSTRACT_ORIGIN (fn)) |
30491 | /* Derived from a constructor. */ |
30492 | return false; |
30493 | return true; |
30494 | } |
30495 | |
30496 | /* True if FN is a C++23 inherited guide. */ |
30497 | |
30498 | bool |
30499 | inherited_guide_p (const_tree fn) |
30500 | { |
30501 | gcc_assert (deduction_guide_p (fn)); |
30502 | return LANG_DECL_FN_CHECK (fn)->context != NULL_TREE; |
30503 | } |
30504 | |
30505 | /* Set the base class BASE from which the transformed guide FN |
30506 | was inherited as part of C++23 inherited CTAD. */ |
30507 | |
30508 | static void |
30509 | set_inherited_guide_context (const_tree fn, tree base) |
30510 | { |
30511 | gcc_assert (deduction_guide_p (fn)); |
30512 | LANG_DECL_FN_CHECK (fn)->context = base; |
30513 | } |
30514 | |
30515 | /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at |
30516 | LEVEL:INDEX, using tsubst_args and complain for substitution into non-type |
30517 | template parameter types. Note that the handling of template template |
30518 | parameters relies on current_template_parms being set appropriately for the |
30519 | new template. */ |
30520 | |
30521 | static tree |
30522 | rewrite_template_parm (tree olddecl, unsigned index, unsigned level, |
30523 | tree tsubst_args, tsubst_flags_t complain) |
30524 | { |
30525 | if (olddecl == error_mark_node) |
30526 | return error_mark_node; |
30527 | |
30528 | tree oldidx = get_template_parm_index (parm: olddecl); |
30529 | |
30530 | tree newtype; |
30531 | if (TREE_CODE (olddecl) == TYPE_DECL |
30532 | || TREE_CODE (olddecl) == TEMPLATE_DECL) |
30533 | { |
30534 | tree oldtype = TREE_TYPE (olddecl); |
30535 | newtype = cxx_make_type (TREE_CODE (oldtype)); |
30536 | TYPE_MAIN_VARIANT (newtype) = newtype; |
30537 | } |
30538 | else |
30539 | { |
30540 | newtype = TREE_TYPE (olddecl); |
30541 | if (type_uses_auto (newtype)) |
30542 | { |
30543 | // Substitute once to fix references to other template parameters. |
30544 | newtype = tsubst (t: newtype, args: tsubst_args, |
30545 | complain: complain|tf_partial, NULL_TREE); |
30546 | // Now substitute again to reduce the level of the auto. |
30547 | newtype = tsubst (t: newtype, args: current_template_args (), |
30548 | complain, NULL_TREE); |
30549 | } |
30550 | else |
30551 | newtype = tsubst (t: newtype, args: tsubst_args, |
30552 | complain, NULL_TREE); |
30553 | } |
30554 | |
30555 | tree newdecl |
30556 | = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl), |
30557 | DECL_NAME (olddecl), newtype); |
30558 | SET_DECL_TEMPLATE_PARM_P (newdecl); |
30559 | |
30560 | tree newidx; |
30561 | if (TREE_CODE (olddecl) == TYPE_DECL |
30562 | || TREE_CODE (olddecl) == TEMPLATE_DECL) |
30563 | { |
30564 | newidx = TEMPLATE_TYPE_PARM_INDEX (newtype) |
30565 | = build_template_parm_index (index, level, orig_level: level, |
30566 | decl: newdecl, type: newtype); |
30567 | TEMPLATE_PARM_PARAMETER_PACK (newidx) |
30568 | = TEMPLATE_PARM_PARAMETER_PACK (oldidx); |
30569 | TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl; |
30570 | |
30571 | if (TREE_CODE (olddecl) == TEMPLATE_DECL) |
30572 | { |
30573 | tree newresult |
30574 | = build_lang_decl_loc (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL, |
30575 | DECL_NAME (olddecl), newtype); |
30576 | DECL_ARTIFICIAL (newresult) = true; |
30577 | DECL_TEMPLATE_RESULT (newdecl) = newresult; |
30578 | // First create a copy (ttargs) of tsubst_args with an |
30579 | // additional level for the template template parameter's own |
30580 | // template parameters (ttparms). |
30581 | tree ttparms = (INNERMOST_TEMPLATE_PARMS |
30582 | (DECL_TEMPLATE_PARMS (olddecl))); |
30583 | const int depth = TMPL_ARGS_DEPTH (tsubst_args); |
30584 | tree ttargs = make_tree_vec (depth + 1); |
30585 | for (int i = 0; i < depth; ++i) |
30586 | TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1); |
30587 | TREE_VEC_ELT (ttargs, depth) |
30588 | = template_parms_level_to_args (parms: ttparms); |
30589 | // Substitute ttargs into ttparms to fix references to |
30590 | // other template parameters. |
30591 | ttparms = tsubst_template_parms_level (parms: ttparms, args: ttargs, |
30592 | complain: complain|tf_partial); |
30593 | // Now substitute again with args based on tparms, to reduce |
30594 | // the level of the ttparms. |
30595 | ttargs = current_template_args (); |
30596 | ttparms = tsubst_template_parms_level (parms: ttparms, args: ttargs, |
30597 | complain); |
30598 | // Finally, tack the adjusted parms onto tparms. |
30599 | ttparms = tree_cons (size_int (level + 1), ttparms, |
30600 | copy_node (current_template_parms)); |
30601 | // As with all template template parms, the parameter list captured |
30602 | // by this template template parm that corresponds to its own level |
30603 | // should be empty. This avoids infinite recursion when structurally |
30604 | // comparing two such rewritten template template parms (PR102479). |
30605 | gcc_assert (!TREE_VEC_LENGTH |
30606 | (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl))))); |
30607 | gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level); |
30608 | TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0); |
30609 | // All done. |
30610 | DECL_TEMPLATE_PARMS (newdecl) = ttparms; |
30611 | DECL_TEMPLATE_INFO (newresult) |
30612 | = build_template_info (template_decl: newdecl, template_args: template_parms_to_args (parms: ttparms)); |
30613 | } |
30614 | |
30615 | if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl))) |
30616 | SET_TYPE_STRUCTURAL_EQUALITY (newtype); |
30617 | else |
30618 | TYPE_CANONICAL (newtype) = canonical_type_parameter (type: newtype); |
30619 | } |
30620 | else |
30621 | { |
30622 | tree oldconst = TEMPLATE_PARM_DECL (oldidx); |
30623 | tree newconst |
30624 | = build_decl (DECL_SOURCE_LOCATION (oldconst), |
30625 | TREE_CODE (oldconst), |
30626 | DECL_NAME (oldconst), newtype); |
30627 | TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl) |
30628 | = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true; |
30629 | SET_DECL_TEMPLATE_PARM_P (newconst); |
30630 | newidx = build_template_parm_index (index, level, orig_level: level, |
30631 | decl: newconst, type: newtype); |
30632 | TEMPLATE_PARM_PARAMETER_PACK (newidx) |
30633 | = TEMPLATE_PARM_PARAMETER_PACK (oldidx); |
30634 | DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx; |
30635 | } |
30636 | |
30637 | return newdecl; |
30638 | } |
30639 | |
30640 | /* As rewrite_template_parm, but for the whole TREE_LIST representing a |
30641 | template parameter. */ |
30642 | |
30643 | static tree |
30644 | rewrite_tparm_list (tree oldelt, unsigned index, unsigned level, |
30645 | tree targs, unsigned targs_index, tsubst_flags_t complain) |
30646 | { |
30647 | tree olddecl = TREE_VALUE (oldelt); |
30648 | tree newdecl = rewrite_template_parm (olddecl, index, level, |
30649 | tsubst_args: targs, complain); |
30650 | if (newdecl == error_mark_node) |
30651 | return error_mark_node; |
30652 | tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt), |
30653 | args: targs, complain, NULL_TREE); |
30654 | tree list = build_tree_list (newdef, newdecl); |
30655 | TEMPLATE_PARM_CONSTRAINTS (list) |
30656 | = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt), |
30657 | targs, complain, NULL_TREE); |
30658 | int depth = TMPL_ARGS_DEPTH (targs); |
30659 | TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (t: list); |
30660 | return list; |
30661 | } |
30662 | |
30663 | /* Returns a C++17 class deduction guide template based on the constructor |
30664 | CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default |
30665 | guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an |
30666 | aggregate initialization guide. OUTER_ARGS are the template arguments |
30667 | for the enclosing scope of the class. */ |
30668 | |
30669 | static tree |
30670 | build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain) |
30671 | { |
30672 | tree tparms, targs, fparms, fargs, ci; |
30673 | bool memtmpl = false; |
30674 | bool explicit_p; |
30675 | location_t loc; |
30676 | tree fn_tmpl = NULL_TREE; |
30677 | |
30678 | if (outer_args) |
30679 | { |
30680 | ++processing_template_decl; |
30681 | type = tsubst (t: type, args: outer_args, complain, CLASSTYPE_TI_TEMPLATE (type)); |
30682 | --processing_template_decl; |
30683 | } |
30684 | |
30685 | if (!DECL_DECLARES_FUNCTION_P (ctor)) |
30686 | { |
30687 | if (TYPE_P (ctor)) |
30688 | { |
30689 | bool copy_p = TYPE_REF_P (ctor); |
30690 | if (copy_p) |
30691 | fparms = tree_cons (NULL_TREE, type, void_list_node); |
30692 | else |
30693 | fparms = void_list_node; |
30694 | } |
30695 | else if (TREE_CODE (ctor) == TREE_LIST) |
30696 | fparms = ctor; |
30697 | else |
30698 | gcc_unreachable (); |
30699 | |
30700 | tree ctmpl = CLASSTYPE_TI_TEMPLATE (type); |
30701 | tparms = DECL_TEMPLATE_PARMS (ctmpl); |
30702 | targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); |
30703 | ci = NULL_TREE; |
30704 | fargs = NULL_TREE; |
30705 | loc = DECL_SOURCE_LOCATION (ctmpl); |
30706 | explicit_p = false; |
30707 | } |
30708 | else |
30709 | { |
30710 | ++processing_template_decl; |
30711 | bool ok = true; |
30712 | |
30713 | complain |= tf_dguide; |
30714 | |
30715 | fn_tmpl |
30716 | = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor |
30717 | : DECL_TI_TEMPLATE (ctor)); |
30718 | if (outer_args) |
30719 | fn_tmpl = tsubst (t: fn_tmpl, args: outer_args, complain, in_decl: ctor); |
30720 | ctor = DECL_TEMPLATE_RESULT (fn_tmpl); |
30721 | |
30722 | tparms = DECL_TEMPLATE_PARMS (fn_tmpl); |
30723 | /* If type is a member class template, DECL_TI_ARGS (ctor) will have |
30724 | fully specialized args for the enclosing class. Strip those off, as |
30725 | the deduction guide won't have those template parameters. */ |
30726 | targs = get_innermost_template_args (DECL_TI_ARGS (ctor), |
30727 | TMPL_PARMS_DEPTH (tparms)); |
30728 | /* Discard the 'this' parameter. */ |
30729 | fparms = FUNCTION_ARG_CHAIN (ctor); |
30730 | fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor)); |
30731 | ci = get_constraints (ctor); |
30732 | loc = DECL_SOURCE_LOCATION (ctor); |
30733 | explicit_p = DECL_NONCONVERTING_P (ctor); |
30734 | |
30735 | if (PRIMARY_TEMPLATE_P (fn_tmpl)) |
30736 | { |
30737 | memtmpl = true; |
30738 | |
30739 | /* For a member template constructor, we need to flatten the two |
30740 | template parameter lists into one, and then adjust the function |
30741 | signature accordingly. This gets...complicated. */ |
30742 | tree save_parms = current_template_parms; |
30743 | |
30744 | /* For a member template we should have two levels of parms/args, one |
30745 | for the class and one for the constructor. We stripped |
30746 | specialized args for further enclosing classes above. */ |
30747 | const int depth = 2; |
30748 | gcc_assert (TMPL_ARGS_DEPTH (targs) == depth); |
30749 | |
30750 | /* Template args for translating references to the two-level template |
30751 | parameters into references to the one-level template parameters we |
30752 | are creating. */ |
30753 | tree tsubst_args = copy_node (targs); |
30754 | TMPL_ARGS_LEVEL (tsubst_args, depth) |
30755 | = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth)); |
30756 | |
30757 | /* Template parms for the constructor template. */ |
30758 | tree ftparms = TREE_VALUE (tparms); |
30759 | unsigned flen = TREE_VEC_LENGTH (ftparms); |
30760 | /* Template parms for the class template. */ |
30761 | tparms = TREE_CHAIN (tparms); |
30762 | tree ctparms = TREE_VALUE (tparms); |
30763 | unsigned clen = TREE_VEC_LENGTH (ctparms); |
30764 | /* Template parms for the deduction guide start as a copy of the |
30765 | template parms for the class. We set current_template_parms for |
30766 | lookup_template_class_1. */ |
30767 | current_template_parms = tparms = copy_node (tparms); |
30768 | tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen); |
30769 | for (unsigned i = 0; i < clen; ++i) |
30770 | TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i); |
30771 | |
30772 | /* Now we need to rewrite the constructor parms to append them to the |
30773 | class parms. */ |
30774 | for (unsigned i = 0; i < flen; ++i) |
30775 | { |
30776 | unsigned index = i + clen; |
30777 | unsigned level = 1; |
30778 | tree oldelt = TREE_VEC_ELT (ftparms, i); |
30779 | tree newelt |
30780 | = rewrite_tparm_list (oldelt, index, level, |
30781 | targs: tsubst_args, targs_index: i, complain); |
30782 | if (newelt == error_mark_node) |
30783 | ok = false; |
30784 | TREE_VEC_ELT (new_vec, index) = newelt; |
30785 | } |
30786 | |
30787 | /* Now we have a final set of template parms to substitute into the |
30788 | function signature. */ |
30789 | targs = template_parms_to_args (parms: tparms); |
30790 | fparms = tsubst_arg_types (arg_types: fparms, args: tsubst_args, NULL_TREE, |
30791 | complain, in_decl: ctor); |
30792 | if (fparms == error_mark_node) |
30793 | ok = false; |
30794 | if (ci) |
30795 | { |
30796 | if (outer_args) |
30797 | /* FIXME: We'd like to avoid substituting outer template |
30798 | arguments into the constraint ahead of time, but the |
30799 | construction of tsubst_args assumes that outer arguments |
30800 | are already substituted in. */ |
30801 | ci = tsubst_constraint_info (ci, outer_args, complain, ctor); |
30802 | ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor); |
30803 | } |
30804 | |
30805 | /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if |
30806 | cp_unevaluated_operand. */ |
30807 | cp_evaluated ev; |
30808 | fargs = tsubst (t: fargs, args: tsubst_args, complain, in_decl: ctor); |
30809 | current_template_parms = save_parms; |
30810 | } |
30811 | else |
30812 | { |
30813 | /* Substitute in the same arguments to rewrite class members into |
30814 | references to members of an unknown specialization. */ |
30815 | cp_evaluated ev; |
30816 | fparms = tsubst_arg_types (arg_types: fparms, args: targs, NULL_TREE, complain, in_decl: ctor); |
30817 | if (fparms == error_mark_node) |
30818 | ok = false; |
30819 | fargs = tsubst (t: fargs, args: targs, complain, in_decl: ctor); |
30820 | if (ci) |
30821 | { |
30822 | if (outer_args) |
30823 | /* FIXME: As above. */ |
30824 | ci = tsubst_constraint_info (ci, outer_args, complain, ctor); |
30825 | ci = tsubst_constraint_info (ci, targs, complain, ctor); |
30826 | } |
30827 | } |
30828 | |
30829 | --processing_template_decl; |
30830 | if (!ok) |
30831 | return error_mark_node; |
30832 | } |
30833 | |
30834 | if (!memtmpl) |
30835 | { |
30836 | /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */ |
30837 | tparms = copy_node (tparms); |
30838 | INNERMOST_TEMPLATE_PARMS (tparms) |
30839 | = copy_node (INNERMOST_TEMPLATE_PARMS (tparms)); |
30840 | } |
30841 | |
30842 | tree fntype = build_function_type (type, fparms); |
30843 | tree ded_fn = build_lang_decl_loc (loc, |
30844 | FUNCTION_DECL, |
30845 | dguide_name (tmpl: type), fntype); |
30846 | DECL_ARGUMENTS (ded_fn) = fargs; |
30847 | DECL_ARTIFICIAL (ded_fn) = true; |
30848 | DECL_NONCONVERTING_P (ded_fn) = explicit_p; |
30849 | tree ded_tmpl = build_template_decl (decl: ded_fn, parms: tparms, /*member*/member_template_p: false); |
30850 | DECL_ARTIFICIAL (ded_tmpl) = true; |
30851 | DECL_TEMPLATE_INFO (ded_fn) = build_template_info (template_decl: ded_tmpl, template_args: targs); |
30852 | DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl; |
30853 | if (DECL_P (ctor)) |
30854 | DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl; |
30855 | if (ci) |
30856 | set_constraints (ded_tmpl, ci); |
30857 | |
30858 | return ded_tmpl; |
30859 | } |
30860 | |
30861 | /* Add to LIST the member types for the reshaped initializer CTOR. */ |
30862 | |
30863 | static tree |
30864 | collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE) |
30865 | { |
30866 | vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor); |
30867 | tree idx, val; unsigned i; |
30868 | FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val) |
30869 | { |
30870 | tree ftype = elt ? elt : TREE_TYPE (idx); |
30871 | if (BRACE_ENCLOSED_INITIALIZER_P (val) |
30872 | && CONSTRUCTOR_BRACES_ELIDED_P (val)) |
30873 | { |
30874 | tree subelt = NULL_TREE; |
30875 | if (TREE_CODE (ftype) == ARRAY_TYPE) |
30876 | subelt = TREE_TYPE (ftype); |
30877 | list = collect_ctor_idx_types (ctor: val, list, elt: subelt); |
30878 | continue; |
30879 | } |
30880 | tree arg = NULL_TREE; |
30881 | if (i == v->length() - 1 |
30882 | && PACK_EXPANSION_P (ftype)) |
30883 | /* Give the trailing pack expansion parameter a default argument to |
30884 | match aggregate initialization behavior, even if we deduce the |
30885 | length of the pack separately to more than we have initializers. */ |
30886 | arg = build_constructor (init_list_type_node, NULL); |
30887 | /* if ei is of array type and xi is a braced-init-list or string literal, |
30888 | Ti is an rvalue reference to the declared type of ei */ |
30889 | STRIP_ANY_LOCATION_WRAPPER (val); |
30890 | if (TREE_CODE (ftype) == ARRAY_TYPE |
30891 | && (BRACE_ENCLOSED_INITIALIZER_P (val) |
30892 | || TREE_CODE (val) == STRING_CST)) |
30893 | { |
30894 | if (TREE_CODE (val) == STRING_CST) |
30895 | ftype = cp_build_qualified_type |
30896 | (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST); |
30897 | ftype = (cp_build_reference_type |
30898 | (ftype, BRACE_ENCLOSED_INITIALIZER_P (val))); |
30899 | } |
30900 | list = tree_cons (arg, ftype, list); |
30901 | } |
30902 | |
30903 | return list; |
30904 | } |
30905 | |
30906 | /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */ |
30907 | |
30908 | static bool |
30909 | is_spec_or_derived (tree etype, tree tmpl) |
30910 | { |
30911 | if (!etype || !CLASS_TYPE_P (etype)) |
30912 | return false; |
30913 | |
30914 | etype = cv_unqualified (etype); |
30915 | tree type = TREE_TYPE (tmpl); |
30916 | tree tparms = (INNERMOST_TEMPLATE_PARMS |
30917 | (DECL_TEMPLATE_PARMS (tmpl))); |
30918 | tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms)); |
30919 | int err = unify (tparms, targs, parm: type, arg: etype, |
30920 | UNIFY_ALLOW_DERIVED, /*explain*/explain_p: false); |
30921 | ggc_free (targs); |
30922 | return !err; |
30923 | } |
30924 | |
30925 | /* Return a C++20 aggregate deduction candidate for TYPE initialized from |
30926 | INIT. */ |
30927 | |
30928 | static tree |
30929 | maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args) |
30930 | { |
30931 | if (cxx_dialect < cxx20) |
30932 | return NULL_TREE; |
30933 | |
30934 | if (init == NULL_TREE) |
30935 | return NULL_TREE; |
30936 | |
30937 | if (DECL_ALIAS_TEMPLATE_P (tmpl)) |
30938 | { |
30939 | tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); |
30940 | tree tinfo = get_template_info (t: under); |
30941 | if (tree guide = maybe_aggr_guide (TI_TEMPLATE (tinfo), init, args)) |
30942 | return alias_ctad_tweaks (tmpl, guide); |
30943 | return NULL_TREE; |
30944 | } |
30945 | |
30946 | /* We might be creating a guide for a class member template, e.g., |
30947 | |
30948 | template<typename U> struct A { |
30949 | template<typename T> struct B { T t; }; |
30950 | }; |
30951 | |
30952 | At this point, A will have been instantiated. Below, we need to |
30953 | use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */ |
30954 | const bool member_template_p |
30955 | = (DECL_TEMPLATE_INFO (tmpl) |
30956 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl))); |
30957 | tree type = TREE_TYPE (tmpl); |
30958 | tree template_type = (member_template_p |
30959 | ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl)) |
30960 | : type); |
30961 | if (!CP_AGGREGATE_TYPE_P (template_type)) |
30962 | return NULL_TREE; |
30963 | |
30964 | /* No aggregate candidate for copy-initialization. */ |
30965 | if (args->length() == 1) |
30966 | { |
30967 | tree val = (*args)[0]; |
30968 | if (is_spec_or_derived (TREE_TYPE (val), tmpl)) |
30969 | return NULL_TREE; |
30970 | } |
30971 | |
30972 | /* If we encounter a problem, we just won't add the candidate. */ |
30973 | tsubst_flags_t complain = tf_none; |
30974 | |
30975 | tree parms = NULL_TREE; |
30976 | if (BRACE_ENCLOSED_INITIALIZER_P (init)) |
30977 | { |
30978 | init = reshape_init (template_type, init, complain); |
30979 | if (init == error_mark_node) |
30980 | return NULL_TREE; |
30981 | parms = collect_ctor_idx_types (ctor: init, list: parms); |
30982 | } |
30983 | else if (TREE_CODE (init) == TREE_LIST) |
30984 | { |
30985 | int len = list_length (init); |
30986 | for (tree binfo : BINFO_BASE_BINFOS (TYPE_BINFO (template_type))) |
30987 | { |
30988 | if (!len) |
30989 | break; |
30990 | parms = tree_cons (NULL_TREE, BINFO_TYPE (binfo), parms); |
30991 | --len; |
30992 | } |
30993 | for (tree field = TYPE_FIELDS (template_type); |
30994 | len; |
30995 | --len, field = DECL_CHAIN (field)) |
30996 | { |
30997 | field = next_aggregate_field (field); |
30998 | if (!field) |
30999 | return NULL_TREE; |
31000 | tree ftype = finish_decltype_type (field, true, complain); |
31001 | parms = tree_cons (NULL_TREE, ftype, parms); |
31002 | } |
31003 | } |
31004 | else |
31005 | /* Aggregate initialization doesn't apply to an initializer expression. */ |
31006 | return NULL_TREE; |
31007 | |
31008 | /* If we're creating a deduction guide for a member class template, |
31009 | we've used the original template pattern type for the reshape_init |
31010 | above; this is done because we want PARMS to be a template parameter |
31011 | type, something that can be deduced when used as a function template |
31012 | parameter. At this point the outer class template has already been |
31013 | partially instantiated (we deferred the deduction until the enclosing |
31014 | scope is non-dependent). Therefore we have to partially instantiate |
31015 | PARMS, so that its template level is properly reduced and we don't get |
31016 | mismatches when deducing types using the guide with PARMS. */ |
31017 | if (member_template_p) |
31018 | { |
31019 | ++processing_template_decl; |
31020 | parms = tsubst (t: parms, args: outer_template_args (decl: tmpl), complain, in_decl: init); |
31021 | --processing_template_decl; |
31022 | } |
31023 | |
31024 | if (parms) |
31025 | { |
31026 | tree last = parms; |
31027 | parms = nreverse (parms); |
31028 | TREE_CHAIN (last) = void_list_node; |
31029 | tree guide = build_deduction_guide (type, ctor: parms, NULL_TREE, complain); |
31030 | return guide; |
31031 | } |
31032 | |
31033 | return NULL_TREE; |
31034 | } |
31035 | |
31036 | /* UGUIDES are the deduction guides for the underlying template of alias |
31037 | template TMPL; adjust them to be deduction guides for TMPL. |
31038 | |
31039 | This routine also handles C++23 inherited CTAD, in which case TMPL is a |
31040 | TREE_LIST representing a synthetic alias template whose TREE_PURPOSE is |
31041 | the template parameter list of the alias template (equivalently, of the |
31042 | derived class) and TREE_VALUE the defining-type-id (equivalently, the |
31043 | base whose guides we're inheriting). UGUIDES are the base's guides. */ |
31044 | |
31045 | static tree |
31046 | alias_ctad_tweaks (tree tmpl, tree uguides) |
31047 | { |
31048 | /* [over.match.class.deduct]: When resolving a placeholder for a deduced |
31049 | class type (9.2.8.2) where the template-name names an alias template A, |
31050 | the defining-type-id of A must be of the form |
31051 | |
31052 | typename(opt) nested-name-specifier(opt) template(opt) simple-template-id |
31053 | |
31054 | as specified in 9.2.8.2. The guides of A are the set of functions or |
31055 | function templates formed as follows. For each function or function |
31056 | template f in the guides of the template named by the simple-template-id |
31057 | of the defining-type-id, the template arguments of the return type of f |
31058 | are deduced from the defining-type-id of A according to the process in |
31059 | 13.10.2.5 with the exception that deduction does not fail if not all |
31060 | template arguments are deduced. Let g denote the result of substituting |
31061 | these deductions into f. If substitution succeeds, form a function or |
31062 | function template f' with the following properties and add it to the set |
31063 | of guides of A: |
31064 | |
31065 | * The function type of f' is the function type of g. |
31066 | |
31067 | * If f is a function template, f' is a function template whose template |
31068 | parameter list consists of all the template parameters of A (including |
31069 | their default template arguments) that appear in the above deductions or |
31070 | (recursively) in their default template arguments, followed by the |
31071 | template parameters of f that were not deduced (including their default |
31072 | template arguments), otherwise f' is not a function template. |
31073 | |
31074 | * The associated constraints (13.5.2) are the conjunction of the |
31075 | associated constraints of g and a constraint that is satisfied if and only |
31076 | if the arguments of A are deducible (see below) from the return type. |
31077 | |
31078 | * If f is a copy deduction candidate (12.4.1.8), then f' is considered to |
31079 | be so as well. |
31080 | |
31081 | * If f was generated from a deduction-guide (12.4.1.8), then f' is |
31082 | considered to be so as well. |
31083 | |
31084 | * The explicit-specifier of f' is the explicit-specifier of g (if |
31085 | any). */ |
31086 | |
31087 | enum { alias, inherited } ctad_kind; |
31088 | tree atype, fullatparms, utype, name; |
31089 | if (TREE_CODE (tmpl) == TEMPLATE_DECL) |
31090 | { |
31091 | ctad_kind = alias; |
31092 | atype = TREE_TYPE (tmpl); |
31093 | fullatparms = DECL_TEMPLATE_PARMS (tmpl); |
31094 | utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); |
31095 | name = dguide_name (tmpl); |
31096 | } |
31097 | else |
31098 | { |
31099 | ctad_kind = inherited; |
31100 | atype = NULL_TREE; |
31101 | fullatparms = TREE_PURPOSE (tmpl); |
31102 | utype = TREE_VALUE (tmpl); |
31103 | name = dguide_name (TPARMS_PRIMARY_TEMPLATE |
31104 | (INNERMOST_TEMPLATE_PARMS (fullatparms))); |
31105 | } |
31106 | |
31107 | tsubst_flags_t complain = tf_none; |
31108 | tree aguides = NULL_TREE; |
31109 | tree atparms = INNERMOST_TEMPLATE_PARMS (fullatparms); |
31110 | unsigned natparms = TREE_VEC_LENGTH (atparms); |
31111 | for (tree f : lkp_range (uguides)) |
31112 | { |
31113 | tree in_decl = f; |
31114 | location_t loc = DECL_SOURCE_LOCATION (f); |
31115 | tree ret = TREE_TYPE (TREE_TYPE (f)); |
31116 | tree fprime = f; |
31117 | if (TREE_CODE (f) == TEMPLATE_DECL) |
31118 | { |
31119 | processing_template_decl_sentinel ptds (/*reset*/false); |
31120 | ++processing_template_decl; |
31121 | |
31122 | /* Deduce template arguments for f from the type-id of A. */ |
31123 | tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f)); |
31124 | unsigned len = TREE_VEC_LENGTH (ftparms); |
31125 | tree targs = make_tree_vec (len); |
31126 | int err = unify (tparms: ftparms, targs, parm: ret, arg: utype, UNIFY_ALLOW_NONE, explain_p: false); |
31127 | if (err) |
31128 | /* CWG2664: Discard any deductions, still build the guide. */ |
31129 | for (unsigned i = 0; i < len; ++i) |
31130 | TREE_VEC_ELT (targs, i) = NULL_TREE; |
31131 | |
31132 | /* The number of parms for f' is the number of parms of A used in |
31133 | the deduced arguments plus non-deduced parms of f. */ |
31134 | unsigned ndlen = 0; |
31135 | unsigned j; |
31136 | for (unsigned i = 0; i < len; ++i) |
31137 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
31138 | ++ndlen; |
31139 | find_template_parameter_info ftpi (fullatparms); |
31140 | ftpi.find_in_recursive (t: targs); |
31141 | unsigned nusedatparms = ftpi.num_found (); |
31142 | unsigned nfparms = nusedatparms + ndlen; |
31143 | tree gtparms = make_tree_vec (nfparms); |
31144 | |
31145 | /* Set current_template_parms as in build_deduction_guide. */ |
31146 | auto ctp = make_temp_override (current_template_parms); |
31147 | current_template_parms = copy_node (fullatparms); |
31148 | TREE_VALUE (current_template_parms) = gtparms; |
31149 | |
31150 | j = 0; |
31151 | unsigned level = 1; |
31152 | |
31153 | /* First copy over the used parms of A. */ |
31154 | tree atargs = make_tree_vec (natparms); |
31155 | for (unsigned i = 0; i < natparms; ++i) |
31156 | { |
31157 | tree elt = TREE_VEC_ELT (atparms, i); |
31158 | if (ftpi.found (parm: elt)) |
31159 | { |
31160 | unsigned index = j++; |
31161 | tree nelt = rewrite_tparm_list (oldelt: elt, index, level, |
31162 | targs: atargs, targs_index: i, complain); |
31163 | TREE_VEC_ELT (gtparms, index) = nelt; |
31164 | } |
31165 | } |
31166 | gcc_checking_assert (j == nusedatparms); |
31167 | |
31168 | /* Adjust the deduced template args for f to refer to the A parms |
31169 | with their new indexes. */ |
31170 | if (nusedatparms && nusedatparms != natparms) |
31171 | targs = tsubst_template_args (t: targs, args: atargs, complain, in_decl); |
31172 | |
31173 | /* Now rewrite the non-deduced parms of f. */ |
31174 | for (unsigned i = 0; ndlen && i < len; ++i) |
31175 | if (TREE_VEC_ELT (targs, i) == NULL_TREE) |
31176 | { |
31177 | --ndlen; |
31178 | unsigned index = j++; |
31179 | tree oldlist = TREE_VEC_ELT (ftparms, i); |
31180 | tree list = rewrite_tparm_list (oldelt: oldlist, index, level, |
31181 | targs, targs_index: i, complain); |
31182 | TREE_VEC_ELT (gtparms, index) = list; |
31183 | } |
31184 | gtparms = build_tree_list (size_one_node, gtparms); |
31185 | |
31186 | /* Substitute the deduced arguments plus the rewritten template |
31187 | parameters into f to get g. This covers the type, copyness, |
31188 | guideness, and explicit-specifier. */ |
31189 | tree g; |
31190 | { |
31191 | /* Parms are to have DECL_CHAIN tsubsted, which would be skipped |
31192 | if cp_unevaluated_operand. */ |
31193 | cp_evaluated ev; |
31194 | g = tsubst_decl (DECL_TEMPLATE_RESULT (f), args: targs, complain, |
31195 | /*use_spec_table=*/false); |
31196 | } |
31197 | if (g == error_mark_node) |
31198 | continue; |
31199 | DECL_NAME (g) = name; |
31200 | if (nfparms == 0) |
31201 | { |
31202 | /* The targs are all non-dependent, so g isn't a template. */ |
31203 | fprime = g; |
31204 | ret = TREE_TYPE (TREE_TYPE (fprime)); |
31205 | goto non_template; |
31206 | } |
31207 | DECL_USE_TEMPLATE (g) = 0; |
31208 | fprime = build_template_decl (decl: g, parms: gtparms, member_template_p: false); |
31209 | DECL_TEMPLATE_RESULT (fprime) = g; |
31210 | TREE_TYPE (fprime) = TREE_TYPE (g); |
31211 | tree gtargs = template_parms_to_args (parms: gtparms); |
31212 | DECL_TEMPLATE_INFO (g) = build_template_info (template_decl: fprime, template_args: gtargs); |
31213 | DECL_PRIMARY_TEMPLATE (fprime) = fprime; |
31214 | |
31215 | /* Substitute the associated constraints. */ |
31216 | tree ci = get_constraints (f); |
31217 | if (ci) |
31218 | { |
31219 | if (tree outer_targs = outer_template_args (decl: f)) |
31220 | ci = tsubst_constraint_info (ci, outer_targs, complain, in_decl); |
31221 | ci = tsubst_constraint_info (ci, targs, complain, in_decl); |
31222 | } |
31223 | if (ci == error_mark_node) |
31224 | continue; |
31225 | |
31226 | /* Add a constraint that the return type matches the instantiation of |
31227 | A with the same template arguments. */ |
31228 | ret = TREE_TYPE (TREE_TYPE (fprime)); |
31229 | if (ctad_kind == alias |
31230 | /* Use template_args_equal instead of same_type_p to get the |
31231 | comparing_dependent_aliases behavior. */ |
31232 | && !template_args_equal (ot: atype, nt: ret)) |
31233 | { |
31234 | tree same = finish_trait_expr (loc, CPTK_IS_DEDUCIBLE, tmpl, ret); |
31235 | ci = append_constraint (ci, same); |
31236 | } |
31237 | |
31238 | if (ci) |
31239 | { |
31240 | remove_constraints (fprime); |
31241 | set_constraints (fprime, ci); |
31242 | } |
31243 | } |
31244 | else |
31245 | { |
31246 | /* For a non-template deduction guide, if the arguments of A aren't |
31247 | deducible from the return type, don't add the candidate. */ |
31248 | non_template: |
31249 | if (ctad_kind == alias |
31250 | && !type_targs_deducible_from (tmpl, ret)) |
31251 | continue; |
31252 | } |
31253 | |
31254 | /* Rewrite the return type of the inherited guide in terms of the |
31255 | derived class. This is specified as replacing the return type R |
31256 | with typename CC<R>::type where the partially specialized CC maps a |
31257 | base class specialization to a specialization of the derived class |
31258 | having such a base (inducing substitution failure if no such derived |
31259 | class exists). |
31260 | |
31261 | As specified this mapping would be done at instantiation time using |
31262 | non-dependent template arguments, but we do it ahead of time using |
31263 | the generic arguments. This seems to be good enough since generic |
31264 | deduction should succeed only if concrete deduction would. */ |
31265 | if (ctad_kind == inherited) |
31266 | { |
31267 | processing_template_decl_sentinel ptds (/*reset*/false); |
31268 | if (TREE_CODE (fprime) == TEMPLATE_DECL) |
31269 | ++processing_template_decl; |
31270 | |
31271 | tree targs = type_targs_deducible_from (tmpl, ret); |
31272 | if (!targs) |
31273 | continue; |
31274 | |
31275 | if (TREE_CODE (f) != TEMPLATE_DECL) |
31276 | fprime = copy_decl (fprime); |
31277 | tree fntype = TREE_TYPE (fprime); |
31278 | ret = lookup_template_class (TPARMS_PRIMARY_TEMPLATE (atparms), arglist: targs, |
31279 | in_decl, NULL_TREE, complain); |
31280 | fntype = build_function_type (ret, TYPE_ARG_TYPES (fntype)); |
31281 | TREE_TYPE (fprime) = fntype; |
31282 | if (TREE_CODE (fprime) == TEMPLATE_DECL) |
31283 | TREE_TYPE (DECL_TEMPLATE_RESULT (fprime)) = fntype; |
31284 | set_inherited_guide_context (fn: fprime, base: utype); |
31285 | } |
31286 | |
31287 | aguides = lookup_add (fns: fprime, lookup: aguides); |
31288 | } |
31289 | |
31290 | return aguides; |
31291 | } |
31292 | |
31293 | /* CTOR is a using-decl inheriting the constructors of some base of the class |
31294 | template TMPL; adjust the base's guides be deduction guides for TMPL. */ |
31295 | |
31296 | static tree |
31297 | inherited_ctad_tweaks (tree tmpl, tree ctor, tsubst_flags_t complain) |
31298 | { |
31299 | /* [over.match.class.deduct]: In addition, if C is defined and inherits |
31300 | constructors ([namespace.udecl]) from a direct base class denoted in the |
31301 | base-specifier-list by a class-or-decltype B, let A be an alias template |
31302 | whose template parameter list is that of C and whose defining-type-id is |
31303 | B. If A is a deducible template ([dcl.type.simple]), the set contains the |
31304 | guides of A with the return type R of each guide replaced with typename |
31305 | CC::type given a class template |
31306 | |
31307 | template <typename> class CC; |
31308 | |
31309 | whose primary template is not defined and with a single partial |
31310 | specialization whose template parameter list is that of A and whose |
31311 | template argument list is a specialization of A with the template argument |
31312 | list of A ([temp.dep.type]) having a member typedef type designating a |
31313 | template specialization with the template argument list of A but with C as |
31314 | the template. */ |
31315 | |
31316 | tree scope = USING_DECL_SCOPE (ctor); |
31317 | if (TREE_CODE (scope) == TYPENAME_TYPE |
31318 | && (TYPE_IDENTIFIER (TYPE_CONTEXT (scope)) |
31319 | == TYPENAME_TYPE_FULLNAME (scope))) |
31320 | /* Recognize using B<T>::B::B as an inherited constructor. */ |
31321 | /* FIXME: Also recognize using C::B::B? We might have to call |
31322 | resolve_typename_type for that. */ |
31323 | scope = TYPE_CONTEXT (scope); |
31324 | if (!CLASS_TYPE_P (scope) |
31325 | || !CLASSTYPE_TEMPLATE_INFO (scope) |
31326 | || !PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))) |
31327 | return NULL_TREE; |
31328 | |
31329 | tree t = build_tree_list (DECL_TEMPLATE_PARMS (tmpl), scope); |
31330 | bool any_dguides_p; |
31331 | tree uguides = deduction_guides_for (CLASSTYPE_TI_TEMPLATE (scope), |
31332 | any_dguides_p, complain); |
31333 | return alias_ctad_tweaks (tmpl: t, uguides); |
31334 | } |
31335 | |
31336 | /* If template arguments for TMPL can be deduced from TYPE, return |
31337 | the deduced arguments, otherwise return NULL_TREE. |
31338 | Used to implement CPTK_IS_DEDUCIBLE for alias CTAD according to |
31339 | [over.match.class.deduct]. |
31340 | |
31341 | This check is specified in terms of partial specialization, so the behavior |
31342 | should be parallel to that of get_partial_spec_bindings. */ |
31343 | |
31344 | tree |
31345 | type_targs_deducible_from (tree tmpl, tree type) |
31346 | { |
31347 | tree tparms, ttype; |
31348 | if (TREE_CODE (tmpl) == TEMPLATE_DECL) |
31349 | { |
31350 | /* If tmpl is a class template, this is trivial: it's deducible if |
31351 | TYPE is a specialization of TMPL. */ |
31352 | if (DECL_CLASS_TEMPLATE_P (tmpl)) |
31353 | { |
31354 | if (CLASS_TYPE_P (type) |
31355 | && CLASSTYPE_TEMPLATE_INFO (type) |
31356 | && CLASSTYPE_TI_TEMPLATE (type) == tmpl) |
31357 | return INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); |
31358 | else |
31359 | return NULL_TREE; |
31360 | } |
31361 | |
31362 | /* Otherwise it's an alias template. */ |
31363 | tparms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); |
31364 | ttype = TREE_TYPE (tmpl); |
31365 | } |
31366 | else |
31367 | { |
31368 | /* TMPL is a synthetic alias template represented as a TREE_LIST as |
31369 | per alias_ctad_tweaks. */ |
31370 | tparms = INNERMOST_TEMPLATE_PARMS (TREE_PURPOSE (tmpl)); |
31371 | ttype = TREE_VALUE (tmpl); |
31372 | tmpl = TI_TEMPLATE (TYPE_TEMPLATE_INFO_MAYBE_ALIAS (ttype)); |
31373 | } |
31374 | |
31375 | int len = TREE_VEC_LENGTH (tparms); |
31376 | tree targs = make_tree_vec (len); |
31377 | bool tried_array_deduction = (cxx_dialect < cxx17); |
31378 | |
31379 | again: |
31380 | if (unify (tparms, targs, parm: ttype, arg: type, |
31381 | UNIFY_ALLOW_NONE, explain_p: false)) |
31382 | return NULL_TREE; |
31383 | |
31384 | /* We don't fail on an undeduced targ the second time through (like |
31385 | get_partial_spec_bindings) because we're going to try defaults. */ |
31386 | for (int i = 0; i < len; ++i) |
31387 | if (! TREE_VEC_ELT (targs, i)) |
31388 | { |
31389 | tree tparm = TREE_VEC_ELT (tparms, i); |
31390 | tparm = TREE_VALUE (tparm); |
31391 | |
31392 | if (!tried_array_deduction |
31393 | && TREE_CODE (tparm) == TYPE_DECL) |
31394 | { |
31395 | try_array_deduction (tparms, targs, parm: ttype); |
31396 | tried_array_deduction = true; |
31397 | if (TREE_VEC_ELT (targs, i)) |
31398 | goto again; |
31399 | } |
31400 | /* If the type parameter is a parameter pack, then it will be deduced |
31401 | to an empty parameter pack. This is another case that doesn't model |
31402 | well as partial specialization. */ |
31403 | if (template_parameter_pack_p (parm: tparm)) |
31404 | { |
31405 | tree arg; |
31406 | if (TREE_CODE (tparm) == PARM_DECL) |
31407 | { |
31408 | arg = make_node (NONTYPE_ARGUMENT_PACK); |
31409 | TREE_CONSTANT (arg) = 1; |
31410 | } |
31411 | else |
31412 | arg = cxx_make_type (TYPE_ARGUMENT_PACK); |
31413 | ARGUMENT_PACK_ARGS (arg) = make_tree_vec (0); |
31414 | TREE_VEC_ELT (targs, i) = arg; |
31415 | } |
31416 | } |
31417 | |
31418 | /* Maybe add in default template args. This seems like a flaw in the |
31419 | specification in terms of partial specialization, since it says the |
31420 | partial specialization has the template parameter list of A, but a |
31421 | partial specialization can't have default targs. */ |
31422 | targs = coerce_template_parms (parms: tparms, args: targs, in_decl: tmpl, complain: tf_none); |
31423 | if (targs == error_mark_node) |
31424 | return NULL_TREE; |
31425 | |
31426 | /* I believe we don't need the template_template_parm_bindings_ok_p call |
31427 | because coerce_template_parms did coerce_template_template_parms. */ |
31428 | |
31429 | if (!constraints_satisfied_p (tmpl, targs)) |
31430 | return NULL_TREE; |
31431 | |
31432 | return targs; |
31433 | } |
31434 | |
31435 | /* Return artificial deduction guides built from the constructors of class |
31436 | template TMPL. */ |
31437 | |
31438 | static tree |
31439 | ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain) |
31440 | { |
31441 | tree outer_args = outer_template_args (decl: tmpl); |
31442 | tree type = TREE_TYPE (most_general_template (tmpl)); |
31443 | |
31444 | tree cands = NULL_TREE; |
31445 | |
31446 | for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter) |
31447 | { |
31448 | /* We handle C++23 inherited CTAD below. */ |
31449 | if (iter.using_p ()) |
31450 | continue; |
31451 | |
31452 | tree guide = build_deduction_guide (type, ctor: *iter, outer_args, complain); |
31453 | cands = lookup_add (fns: guide, lookup: cands); |
31454 | } |
31455 | |
31456 | if (cxx_dialect >= cxx23) |
31457 | /* FIXME: CLASSTYPE_CONSTRUCTORS doesn't contain inherited constructors if |
31458 | e.g. the class also has a user-defined constructor. So instead iterate |
31459 | over TYPE_FIELDS manually to robustly find all relevant using-decls. */ |
31460 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
31461 | if (TREE_CODE (field) == USING_DECL |
31462 | && DECL_NAME (field) == ctor_identifier) |
31463 | { |
31464 | tree uguides = inherited_ctad_tweaks (tmpl, ctor: field, complain); |
31465 | if (uguides) |
31466 | cands = lookup_add (fns: uguides, lookup: cands); |
31467 | } |
31468 | |
31469 | /* Add implicit default constructor deduction guide. */ |
31470 | if (!TYPE_HAS_USER_CONSTRUCTOR (type)) |
31471 | { |
31472 | tree guide = build_deduction_guide (type, ctor: type, outer_args, |
31473 | complain); |
31474 | cands = lookup_add (fns: guide, lookup: cands); |
31475 | } |
31476 | |
31477 | /* Add copy guide. */ |
31478 | { |
31479 | tree gtype = build_reference_type (type); |
31480 | tree guide = build_deduction_guide (type, ctor: gtype, outer_args, |
31481 | complain); |
31482 | cands = lookup_add (fns: guide, lookup: cands); |
31483 | } |
31484 | |
31485 | return cands; |
31486 | } |
31487 | |
31488 | static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache; |
31489 | |
31490 | /* Return the non-aggregate deduction guides for deducible template TMPL. The |
31491 | aggregate candidate is added separately because it depends on the |
31492 | initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction |
31493 | guide. */ |
31494 | |
31495 | static tree |
31496 | deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain) |
31497 | { |
31498 | tree guides = NULL_TREE; |
31499 | if (DECL_ALIAS_TEMPLATE_P (tmpl)) |
31500 | { |
31501 | tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); |
31502 | tree tinfo = get_template_info (t: under); |
31503 | guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p, |
31504 | complain); |
31505 | } |
31506 | else |
31507 | { |
31508 | guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), |
31509 | name: dguide_name (tmpl), |
31510 | LOOK_want::ANY_REACHABLE, |
31511 | /*complain=*/false); |
31512 | if (guides == error_mark_node) |
31513 | guides = NULL_TREE; |
31514 | else |
31515 | any_dguides_p = true; |
31516 | } |
31517 | |
31518 | /* Cache the deduction guides for a template. We also remember the result of |
31519 | lookup, and rebuild everything if it changes; should be very rare. */ |
31520 | /* FIXME: Also rebuild if this is a class template that inherits guides from a |
31521 | base class, and lookup for the latter changed. */ |
31522 | tree_pair_p cache = NULL; |
31523 | if (tree_pair_p &r |
31524 | = hash_map_safe_get_or_insert<hm_ggc> (h&: dguide_cache, k: tmpl)) |
31525 | { |
31526 | cache = r; |
31527 | if (cache->purpose == guides) |
31528 | return cache->value; |
31529 | } |
31530 | else |
31531 | { |
31532 | r = cache = ggc_cleared_alloc<tree_pair_s> (); |
31533 | cache->purpose = guides; |
31534 | } |
31535 | |
31536 | tree cands = NULL_TREE; |
31537 | if (DECL_ALIAS_TEMPLATE_P (tmpl)) |
31538 | cands = alias_ctad_tweaks (tmpl, uguides: guides); |
31539 | else |
31540 | { |
31541 | cands = ctor_deduction_guides_for (tmpl, complain); |
31542 | for (lkp_iterator it (guides); it; ++it) |
31543 | cands = lookup_add (fns: *it, lookup: cands); |
31544 | } |
31545 | |
31546 | cache->value = cands; |
31547 | return cands; |
31548 | } |
31549 | |
31550 | /* Return whether TMPL is a (class template argument-) deducible template. */ |
31551 | |
31552 | bool |
31553 | ctad_template_p (tree tmpl) |
31554 | { |
31555 | /* A deducible template is either a class template or is an alias template |
31556 | whose defining-type-id is of the form |
31557 | |
31558 | typename(opt) nested-name-specifier(opt) template(opt) simple-template-id |
31559 | |
31560 | where the nested-name-specifier (if any) is non-dependent and the |
31561 | template-name of the simple-template-id names a deducible template. */ |
31562 | |
31563 | if (DECL_CLASS_TEMPLATE_P (tmpl) |
31564 | && IDENTIFIER_TRAIT_P (DECL_NAME (tmpl))) |
31565 | /* Don't consider CTAD for templates with the same name as a trait; that |
31566 | is ambiguous with e.g. __is_invocable(_Fn,_Args...). */ |
31567 | return false; |
31568 | if (DECL_CLASS_TEMPLATE_P (tmpl) |
31569 | || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)) |
31570 | return true; |
31571 | if (!DECL_ALIAS_TEMPLATE_P (tmpl)) |
31572 | return false; |
31573 | tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl)); |
31574 | if (tree tinfo = get_template_info (t: orig)) |
31575 | return ctad_template_p (TI_TEMPLATE (tinfo)); |
31576 | return false; |
31577 | } |
31578 | |
31579 | /* Deduce template arguments for the class template placeholder PTYPE for |
31580 | template TMPL based on the initializer INIT, and return the resulting |
31581 | type. */ |
31582 | |
31583 | static tree |
31584 | do_class_deduction (tree ptype, tree tmpl, tree init, tree outer_targs, |
31585 | int flags, tsubst_flags_t complain) |
31586 | { |
31587 | /* We should have handled this in the caller. */ |
31588 | if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)) |
31589 | return ptype; |
31590 | |
31591 | /* If the class was erroneous, don't try to deduce, because that |
31592 | can generate a lot of diagnostic. */ |
31593 | if (TREE_TYPE (tmpl) |
31594 | && TYPE_LANG_SPECIFIC (TREE_TYPE (tmpl)) |
31595 | && CLASSTYPE_ERRONEOUS (TREE_TYPE (tmpl))) |
31596 | return ptype; |
31597 | |
31598 | /* Wait until the enclosing scope is non-dependent. */ |
31599 | if (DECL_CLASS_SCOPE_P (tmpl) |
31600 | && dependent_type_p (DECL_CONTEXT (tmpl))) |
31601 | return ptype; |
31602 | |
31603 | /* Initializing one placeholder from another. */ |
31604 | if (init |
31605 | && (TREE_CODE (init) == TEMPLATE_PARM_INDEX |
31606 | || (TREE_CODE (init) == EXPR_PACK_EXPANSION |
31607 | && (TREE_CODE (PACK_EXPANSION_PATTERN (init)) |
31608 | == TEMPLATE_PARM_INDEX))) |
31609 | && is_auto (TREE_TYPE (init)) |
31610 | && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl) |
31611 | return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype)); |
31612 | |
31613 | if (!ctad_template_p (tmpl)) |
31614 | { |
31615 | if (complain & tf_error) |
31616 | error ("non-deducible template %qT used without template arguments", tmpl); |
31617 | return error_mark_node; |
31618 | } |
31619 | else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl)) |
31620 | { |
31621 | if (complain & tf_error) |
31622 | { |
31623 | /* Be permissive with equivalent alias templates. */ |
31624 | tree u = get_underlying_template (tmpl); |
31625 | auto_diagnostic_group d; |
31626 | diagnostic_t dk = (u == tmpl) ? DK_ERROR : DK_PEDWARN; |
31627 | bool complained |
31628 | = emit_diagnostic (dk, input_location, 0, |
31629 | "alias template deduction only available " |
31630 | "with %<-std=c++20%> or %<-std=gnu++20%>"); |
31631 | if (u == tmpl) |
31632 | return error_mark_node; |
31633 | else if (complained) |
31634 | { |
31635 | inform (input_location, "use %qD directly instead", u); |
31636 | tmpl = u; |
31637 | } |
31638 | } |
31639 | else |
31640 | return error_mark_node; |
31641 | } |
31642 | |
31643 | /* Wait until the initializer is non-dependent. */ |
31644 | if (type_dependent_expression_p (expression: init)) |
31645 | return ptype; |
31646 | |
31647 | if (outer_targs) |
31648 | { |
31649 | int args_depth = TMPL_ARGS_DEPTH (outer_targs); |
31650 | int parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); |
31651 | if (parms_depth > 1) |
31652 | { |
31653 | /* Substitute outer arguments into this CTAD template from the |
31654 | current instantiation. */ |
31655 | int want = std::min (a: args_depth, b: parms_depth - 1); |
31656 | outer_targs = strip_innermost_template_args (args: outer_targs, |
31657 | extra_levels: args_depth - want); |
31658 | tmpl = tsubst (t: tmpl, args: outer_targs, complain, NULL_TREE); |
31659 | if (tmpl == error_mark_node) |
31660 | return error_mark_node; |
31661 | } |
31662 | } |
31663 | |
31664 | /* Don't bother with the alias rules for an equivalent template. */ |
31665 | tmpl = get_underlying_template (tmpl); |
31666 | |
31667 | tree type = TREE_TYPE (tmpl); |
31668 | |
31669 | bool try_list_cand = false; |
31670 | bool list_init_p = false; |
31671 | |
31672 | releasing_vec rv_args = NULL; |
31673 | vec<tree,va_gc> *&args = *&rv_args; |
31674 | if (init == NULL_TREE) |
31675 | args = make_tree_vector (); |
31676 | else if (BRACE_ENCLOSED_INITIALIZER_P (init)) |
31677 | { |
31678 | list_init_p = true; |
31679 | try_list_cand = true; |
31680 | if (CONSTRUCTOR_NELTS (init) == 1 |
31681 | && !CONSTRUCTOR_IS_DESIGNATED_INIT (init)) |
31682 | { |
31683 | /* As an exception, the first phase in 16.3.1.7 (considering the |
31684 | initializer list as a single argument) is omitted if the |
31685 | initializer list consists of a single expression of type cv U, |
31686 | where U is a specialization of C or a class derived from a |
31687 | specialization of C. */ |
31688 | tree elt = CONSTRUCTOR_ELT (init, 0)->value; |
31689 | if (is_spec_or_derived (TREE_TYPE (elt), tmpl)) |
31690 | try_list_cand = false; |
31691 | } |
31692 | if (try_list_cand || is_std_init_list (type)) |
31693 | args = make_tree_vector_single (init); |
31694 | else |
31695 | args = make_tree_vector_from_ctor (init); |
31696 | } |
31697 | else if (TREE_CODE (init) == TREE_LIST) |
31698 | args = make_tree_vector_from_list (init); |
31699 | else |
31700 | args = make_tree_vector_single (init); |
31701 | |
31702 | /* Do this now to avoid problems with erroneous args later on. */ |
31703 | args = resolve_args (args, complain); |
31704 | if (args == NULL) |
31705 | return error_mark_node; |
31706 | |
31707 | bool any_dguides_p = false; |
31708 | tree cands = deduction_guides_for (tmpl, any_dguides_p, complain); |
31709 | if (cands == error_mark_node) |
31710 | return error_mark_node; |
31711 | |
31712 | /* Prune explicit deduction guides in copy-initialization context (but |
31713 | not copy-list-initialization). */ |
31714 | bool elided = false; |
31715 | if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING)) |
31716 | { |
31717 | for (lkp_iterator iter (cands); !elided && iter; ++iter) |
31718 | if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter))) |
31719 | elided = true; |
31720 | |
31721 | if (elided) |
31722 | { |
31723 | /* Found a nonconverting guide, prune the candidates. */ |
31724 | tree pruned = NULL_TREE; |
31725 | for (lkp_iterator iter (cands); iter; ++iter) |
31726 | if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter))) |
31727 | pruned = lookup_add (fns: *iter, lookup: pruned); |
31728 | |
31729 | cands = pruned; |
31730 | } |
31731 | } |
31732 | |
31733 | if (!any_dguides_p) |
31734 | if (tree guide = maybe_aggr_guide (tmpl, init, args)) |
31735 | cands = lookup_add (fns: guide, lookup: cands); |
31736 | |
31737 | tree fndecl = error_mark_node; |
31738 | |
31739 | /* If this is list-initialization and the class has a list guide, first |
31740 | try deducing from the list as a single argument, as [over.match.list]. */ |
31741 | if (try_list_cand) |
31742 | { |
31743 | tree list_cands = NULL_TREE; |
31744 | for (tree dg : lkp_range (cands)) |
31745 | if (is_list_ctor (dg)) |
31746 | list_cands = lookup_add (fns: dg, lookup: list_cands); |
31747 | if (list_cands) |
31748 | fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none); |
31749 | if (fndecl == error_mark_node) |
31750 | { |
31751 | /* That didn't work, now try treating the list as a sequence of |
31752 | arguments. */ |
31753 | release_tree_vector (args); |
31754 | args = make_tree_vector_from_ctor (init); |
31755 | args = resolve_args (args, complain); |
31756 | if (args == NULL) |
31757 | return error_mark_node; |
31758 | } |
31759 | } |
31760 | |
31761 | if (elided && !cands) |
31762 | { |
31763 | error ("cannot deduce template arguments for copy-initialization" |
31764 | " of %qT, as it has no non-explicit deduction guides or " |
31765 | "user-declared constructors", type); |
31766 | return error_mark_node; |
31767 | } |
31768 | else if (!cands && fndecl == error_mark_node) |
31769 | { |
31770 | error ("cannot deduce template arguments of %qT, as it has no viable " |
31771 | "deduction guides", type); |
31772 | return error_mark_node; |
31773 | } |
31774 | |
31775 | if (fndecl == error_mark_node) |
31776 | fndecl = perform_dguide_overload_resolution (cands, args, tf_none); |
31777 | |
31778 | if (fndecl == error_mark_node) |
31779 | { |
31780 | if (complain & tf_warning_or_error) |
31781 | { |
31782 | auto_diagnostic_group d; |
31783 | error ("class template argument deduction failed:"); |
31784 | perform_dguide_overload_resolution (cands, args, complain); |
31785 | if (elided) |
31786 | inform (input_location, "explicit deduction guides not considered " |
31787 | "for copy-initialization"); |
31788 | } |
31789 | return error_mark_node; |
31790 | } |
31791 | /* [over.match.list]/1: In copy-list-initialization, if an explicit |
31792 | constructor is chosen, the initialization is ill-formed. */ |
31793 | else if (flags & LOOKUP_ONLYCONVERTING) |
31794 | { |
31795 | if (DECL_NONCONVERTING_P (fndecl)) |
31796 | { |
31797 | if (complain & tf_warning_or_error) |
31798 | { |
31799 | // TODO: Pass down location from cp_finish_decl. |
31800 | auto_diagnostic_group d; |
31801 | error ("class template argument deduction for %qT failed: " |
31802 | "explicit deduction guide selected in " |
31803 | "copy-list-initialization", type); |
31804 | inform (DECL_SOURCE_LOCATION (fndecl), |
31805 | "explicit deduction guide declared here"); |
31806 | |
31807 | } |
31808 | return error_mark_node; |
31809 | } |
31810 | } |
31811 | |
31812 | /* If CTAD succeeded but the type doesn't have any explicit deduction |
31813 | guides, this deduction might not be what the user intended. */ |
31814 | if (fndecl != error_mark_node && !any_dguides_p && (complain & tf_warning)) |
31815 | { |
31816 | auto_diagnostic_group d; |
31817 | if ((!DECL_IN_SYSTEM_HEADER (fndecl) |
31818 | || global_dc->m_warn_system_headers) |
31819 | && warning (OPT_Wctad_maybe_unsupported, |
31820 | "%qT may not intend to support class template argument " |
31821 | "deduction", type)) |
31822 | inform (input_location, "add a deduction guide to suppress this " |
31823 | "warning"); |
31824 | } |
31825 | |
31826 | return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)), |
31827 | cp_type_quals (ptype)); |
31828 | } |
31829 | |
31830 | /* Return true if INIT is an unparenthesized id-expression or an |
31831 | unparenthesized class member access. Used for the argument of |
31832 | decltype(auto). */ |
31833 | |
31834 | bool |
31835 | unparenthesized_id_or_class_member_access_p (tree init) |
31836 | { |
31837 | STRIP_ANY_LOCATION_WRAPPER (init); |
31838 | |
31839 | /* We need to be able to tell '(r)' and 'r' apart (when it's of |
31840 | reference type). Only the latter is an id-expression. */ |
31841 | if (REFERENCE_REF_P (init) |
31842 | && !REF_PARENTHESIZED_P (init)) |
31843 | init = TREE_OPERAND (init, 0); |
31844 | return (DECL_P (init) |
31845 | || ((TREE_CODE (init) == COMPONENT_REF |
31846 | || TREE_CODE (init) == SCOPE_REF) |
31847 | && !REF_PARENTHESIZED_P (init))); |
31848 | } |
31849 | |
31850 | /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced |
31851 | from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. |
31852 | The CONTEXT determines the context in which auto deduction is performed |
31853 | and is used to control error diagnostics. FLAGS are the LOOKUP_* flags. |
31854 | |
31855 | OUTER_TARGS is used during template argument deduction (context == adc_unify) |
31856 | to properly substitute the result. It's also used in the adc_unify and |
31857 | adc_requirement contexts to communicate the necessary template arguments |
31858 | to satisfaction. OUTER_TARGS is ignored in other contexts. |
31859 | |
31860 | Additionally for adc_unify contexts TMPL is the template for which TYPE |
31861 | is a template parameter type. |
31862 | |
31863 | For partial-concept-ids, extra args from OUTER_TARGS, TMPL and the current |
31864 | scope may be appended to the list of deduced template arguments prior to |
31865 | determining constraint satisfaction as appropriate. */ |
31866 | |
31867 | tree |
31868 | do_auto_deduction (tree type, tree init, tree auto_node, |
31869 | tsubst_flags_t complain /* = tf_warning_or_error */, |
31870 | auto_deduction_context context /* = adc_unspecified */, |
31871 | tree outer_targs /* = NULL_TREE */, |
31872 | int flags /* = LOOKUP_NORMAL */, |
31873 | tree tmpl /* = NULL_TREE */) |
31874 | { |
31875 | if (type == error_mark_node || init == error_mark_node) |
31876 | return error_mark_node; |
31877 | |
31878 | if (init && type_dependent_expression_p (expression: init) |
31879 | && context != adc_unify) |
31880 | /* Defining a subset of type-dependent expressions that we can deduce |
31881 | from ahead of time isn't worth the trouble. */ |
31882 | return type; |
31883 | |
31884 | /* Similarly, we can't deduce from another undeduced decl. */ |
31885 | if (init && undeduced_auto_decl (init)) |
31886 | return type; |
31887 | |
31888 | /* We may be doing a partial substitution, but we still want to replace |
31889 | auto_node. */ |
31890 | complain &= ~tf_partial; |
31891 | |
31892 | if (init && BRACE_ENCLOSED_INITIALIZER_P (init)) |
31893 | { |
31894 | /* We don't recurse here because we can't deduce from a nested |
31895 | initializer_list. */ |
31896 | if (CONSTRUCTOR_ELTS (init)) |
31897 | for (constructor_elt &elt : CONSTRUCTOR_ELTS (init)) |
31898 | elt.value = resolve_nondeduced_context (orig_expr: elt.value, complain); |
31899 | } |
31900 | else if (init) |
31901 | init = resolve_nondeduced_context (orig_expr: init, complain); |
31902 | |
31903 | /* In C++23, we must deduce the type to int&& for code like |
31904 | decltype(auto) f(int&& x) { return (x); } |
31905 | or |
31906 | auto&& f(int x) { return x; } |
31907 | so we use treat_lvalue_as_rvalue_p. But don't do it for |
31908 | decltype(auto) f(int x) { return x; } |
31909 | where we should deduce 'int' rather than 'int&&'; transmogrifying |
31910 | INIT to an rvalue would break that. */ |
31911 | tree r; |
31912 | if (cxx_dialect >= cxx23 |
31913 | && context == adc_return_type |
31914 | && (!AUTO_IS_DECLTYPE (auto_node) |
31915 | || !unparenthesized_id_or_class_member_access_p (init)) |
31916 | && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init), |
31917 | /*return*/true))) |
31918 | init = r; |
31919 | |
31920 | if (tree ctmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) |
31921 | /* C++17 class template argument deduction. */ |
31922 | return do_class_deduction (ptype: type, tmpl: ctmpl, init, outer_targs, flags, complain); |
31923 | |
31924 | if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE) |
31925 | /* Nothing we can do with this, even in deduction context. */ |
31926 | return type; |
31927 | |
31928 | location_t loc = cp_expr_loc_or_input_loc (t: init); |
31929 | |
31930 | /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto |
31931 | with either a new invented type template parameter U or, if the |
31932 | initializer is a braced-init-list (8.5.4), with |
31933 | std::initializer_list<U>. */ |
31934 | if (BRACE_ENCLOSED_INITIALIZER_P (init)) |
31935 | { |
31936 | if (!DIRECT_LIST_INIT_P (init)) |
31937 | type = listify_autos (type, auto_node); |
31938 | else if (CONSTRUCTOR_NELTS (init) == 1) |
31939 | init = CONSTRUCTOR_ELT (init, 0)->value; |
31940 | else |
31941 | { |
31942 | if (complain & tf_warning_or_error) |
31943 | { |
31944 | auto_diagnostic_group d; |
31945 | if (permerror (loc, "direct-list-initialization of " |
31946 | "%<auto%> requires exactly one element")) |
31947 | inform (loc, |
31948 | "for deduction to %<std::initializer_list%>, use copy-" |
31949 | "list-initialization (i.e. add %<=%> before the %<{%>)"); |
31950 | } |
31951 | type = listify_autos (type, auto_node); |
31952 | } |
31953 | } |
31954 | |
31955 | if (type == error_mark_node || init == error_mark_node) |
31956 | return error_mark_node; |
31957 | |
31958 | tree targs; |
31959 | if (context == adc_decomp_type |
31960 | && auto_node == type |
31961 | && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) |
31962 | { |
31963 | /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers |
31964 | and initializer has array type, deduce cv-qualified array type. */ |
31965 | targs = make_tree_vec (1); |
31966 | TREE_VEC_ELT (targs, 0) = TREE_TYPE (init); |
31967 | } |
31968 | else if (AUTO_IS_DECLTYPE (auto_node)) |
31969 | { |
31970 | const bool id = unparenthesized_id_or_class_member_access_p (init); |
31971 | tree deduced = finish_decltype_type (init, id, complain); |
31972 | deduced = canonicalize_type_argument (arg: deduced, complain); |
31973 | if (deduced == error_mark_node) |
31974 | return error_mark_node; |
31975 | targs = make_tree_vec (1); |
31976 | TREE_VEC_ELT (targs, 0) = deduced; |
31977 | } |
31978 | else |
31979 | { |
31980 | if (error_operand_p (t: init)) |
31981 | return error_mark_node; |
31982 | |
31983 | tree parms = build_tree_list (NULL_TREE, type); |
31984 | tree tparms = make_tree_vec (1); |
31985 | TREE_VEC_ELT (tparms, 0) |
31986 | = build_tree_list (NULL_TREE, TYPE_NAME (auto_node)); |
31987 | |
31988 | targs = make_tree_vec (TREE_VEC_LENGTH (tparms)); |
31989 | int val = type_unification_real (tparms, full_targs: targs, xparms: parms, xargs: &init, xnargs: 1, subr: 0, |
31990 | strict: DEDUCE_CALL, |
31991 | NULL, /*explain_p=*/false); |
31992 | if (val > 0) |
31993 | { |
31994 | if (processing_template_decl) |
31995 | /* Try again at instantiation time. */ |
31996 | return type; |
31997 | if (type && type != error_mark_node |
31998 | && (complain & tf_error)) |
31999 | /* If type is error_mark_node a diagnostic must have been |
32000 | emitted by now. Also, having a mention to '<type error>' |
32001 | in the diagnostic is not really useful to the user. */ |
32002 | { |
32003 | if (cfun |
32004 | && FNDECL_USED_AUTO (current_function_decl) |
32005 | && (auto_node |
32006 | == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl)) |
32007 | && LAMBDA_FUNCTION_P (current_function_decl)) |
32008 | error_at (loc, "unable to deduce lambda return type from %qE", |
32009 | init); |
32010 | else |
32011 | error_at (loc, "unable to deduce %qT from %qE", type, init); |
32012 | type_unification_real (tparms, full_targs: targs, xparms: parms, xargs: &init, xnargs: 1, subr: 0, |
32013 | strict: DEDUCE_CALL, |
32014 | NULL, /*explain_p=*/true); |
32015 | } |
32016 | return error_mark_node; |
32017 | } |
32018 | } |
32019 | |
32020 | /* Check any placeholder constraints against the deduced type. */ |
32021 | if (processing_template_decl && context == adc_unify) |
32022 | /* Constraints will be checked after deduction. */; |
32023 | else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))) |
32024 | { |
32025 | if (processing_template_decl) |
32026 | { |
32027 | gcc_checking_assert (context == adc_variable_type |
32028 | || context == adc_return_type |
32029 | || context == adc_decomp_type); |
32030 | gcc_checking_assert (!type_dependent_expression_p (init)); |
32031 | /* If the constraint is dependent, we need to wait until |
32032 | instantiation time to resolve the placeholder. */ |
32033 | if (placeholder_type_constraint_dependent_p (t: constr)) |
32034 | return type; |
32035 | } |
32036 | |
32037 | if (context == adc_return_type |
32038 | || context == adc_variable_type |
32039 | || context == adc_decomp_type) |
32040 | if (tree fn = current_function_decl) |
32041 | if (DECL_TEMPLATE_INFO (fn) || LAMBDA_FUNCTION_P (fn)) |
32042 | { |
32043 | outer_targs = DECL_TEMPLATE_INFO (fn) |
32044 | ? DECL_TI_ARGS (fn) : NULL_TREE; |
32045 | if (LAMBDA_FUNCTION_P (fn)) |
32046 | { |
32047 | /* As in satisfy_declaration_constraints. */ |
32048 | tree regen_args = lambda_regenerating_args (t: fn); |
32049 | if (outer_targs) |
32050 | outer_targs = add_to_template_args (args: regen_args, extra_args: outer_targs); |
32051 | else |
32052 | outer_targs = regen_args; |
32053 | } |
32054 | } |
32055 | |
32056 | tree full_targs = outer_targs; |
32057 | if (context == adc_unify && tmpl) |
32058 | full_targs = add_outermost_template_args (args: tmpl, extra_args: full_targs); |
32059 | full_targs = add_to_template_args (args: full_targs, extra_args: targs); |
32060 | |
32061 | /* HACK: Compensate for callers not always communicating all levels of |
32062 | outer template arguments by filling in the outermost missing levels |
32063 | with dummy levels before checking satisfaction. We'll still crash |
32064 | if the constraint depends on a template argument belonging to one of |
32065 | these missing levels, but this hack otherwise allows us to handle a |
32066 | large subset of possible constraints (including all non-dependent |
32067 | constraints). */ |
32068 | if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node) |
32069 | - TMPL_ARGS_DEPTH (full_targs))) |
32070 | { |
32071 | tree dummy_levels = make_tree_vec (missing_levels); |
32072 | for (int i = 0; i < missing_levels; ++i) |
32073 | TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0); |
32074 | full_targs = add_to_template_args (args: dummy_levels, extra_args: full_targs); |
32075 | } |
32076 | |
32077 | if (!constraints_satisfied_p (auto_node, full_targs)) |
32078 | { |
32079 | if (complain & tf_warning_or_error) |
32080 | { |
32081 | auto_diagnostic_group d; |
32082 | switch (context) |
32083 | { |
32084 | case adc_unspecified: |
32085 | case adc_unify: |
32086 | error_at (loc, "placeholder constraints not satisfied"); |
32087 | break; |
32088 | case adc_variable_type: |
32089 | case adc_decomp_type: |
32090 | error_at (loc, "deduced initializer does not satisfy " |
32091 | "placeholder constraints"); |
32092 | break; |
32093 | case adc_return_type: |
32094 | error_at (loc, "deduced return type does not satisfy " |
32095 | "placeholder constraints"); |
32096 | break; |
32097 | case adc_requirement: |
32098 | error_at (loc, "deduced expression type does not satisfy " |
32099 | "placeholder constraints"); |
32100 | break; |
32101 | } |
32102 | diagnose_constraints (loc, auto_node, full_targs); |
32103 | } |
32104 | return error_mark_node; |
32105 | } |
32106 | } |
32107 | |
32108 | if (TEMPLATE_TYPE_LEVEL (auto_node) == 0) |
32109 | { |
32110 | /* Substitute this level-less auto via tsubst by temporarily |
32111 | overriding its level to 1. */ |
32112 | TEMPLATE_TYPE_LEVEL (auto_node) = 1; |
32113 | type = tsubst (t: type, args: targs, complain, NULL_TREE); |
32114 | TEMPLATE_TYPE_LEVEL (auto_node) = 0; |
32115 | return type; |
32116 | } |
32117 | |
32118 | if (TEMPLATE_TYPE_LEVEL (auto_node) == 1) |
32119 | /* The outer template arguments are already substituted into type |
32120 | (but we still may have used them for constraint checking above). */; |
32121 | else if (context == adc_unify) |
32122 | targs = add_to_template_args (args: outer_targs, extra_args: targs); |
32123 | else if (processing_template_decl) |
32124 | targs = add_to_template_args (args: current_template_args (), extra_args: targs); |
32125 | return tsubst (t: type, args: targs, complain, NULL_TREE); |
32126 | } |
32127 | |
32128 | /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the |
32129 | result. */ |
32130 | |
32131 | tree |
32132 | splice_late_return_type (tree type, tree late_return_type) |
32133 | { |
32134 | if (late_return_type) |
32135 | { |
32136 | gcc_assert (is_auto (type) || seen_error ()); |
32137 | return late_return_type; |
32138 | } |
32139 | |
32140 | if (tree auto_node = find_type_usage (t: type, pred: is_auto)) |
32141 | if (TEMPLATE_TYPE_LEVEL (auto_node) <= current_template_depth) |
32142 | { |
32143 | /* In an abbreviated function template we didn't know we were dealing |
32144 | with a function template when we saw the auto return type, so rebuild |
32145 | the return type using an auto with the correct level. */ |
32146 | tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), set_canonical: false); |
32147 | tree auto_vec = make_tree_vec (1); |
32148 | TREE_VEC_ELT (auto_vec, 0) = new_auto; |
32149 | tree targs = add_outermost_template_args (args: current_template_args (), |
32150 | extra_args: auto_vec); |
32151 | /* Also rebuild the constraint info in terms of the new auto. */ |
32152 | if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node)) |
32153 | PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto) |
32154 | = build_tree_list (current_template_parms, |
32155 | tsubst_constraint (TREE_VALUE (ci), targs, |
32156 | tf_none, NULL_TREE)); |
32157 | TYPE_CANONICAL (new_auto) = canonical_type_parameter (type: new_auto); |
32158 | return tsubst (t: type, args: targs, complain: tf_none, NULL_TREE); |
32159 | } |
32160 | return type; |
32161 | } |
32162 | |
32163 | /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or |
32164 | 'decltype(auto)' or a deduced class template. */ |
32165 | |
32166 | bool |
32167 | is_auto (const_tree type) |
32168 | { |
32169 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM |
32170 | && (TYPE_IDENTIFIER (type) == auto_identifier |
32171 | || TYPE_IDENTIFIER (type) == decltype_auto_identifier)) |
32172 | return true; |
32173 | else |
32174 | return false; |
32175 | } |
32176 | |
32177 | /* for_each_template_parm callback for type_uses_auto. */ |
32178 | |
32179 | int |
32180 | is_auto_r (tree tp, void */*data*/) |
32181 | { |
32182 | return is_auto (type: tp); |
32183 | } |
32184 | |
32185 | /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains |
32186 | a use of `auto'. Returns NULL_TREE otherwise. */ |
32187 | |
32188 | tree |
32189 | type_uses_auto (tree type) |
32190 | { |
32191 | if (type == NULL_TREE) |
32192 | return NULL_TREE; |
32193 | |
32194 | /* For parameter packs, check the contents of the pack. */ |
32195 | if (PACK_EXPANSION_P (type)) |
32196 | type = PACK_EXPANSION_PATTERN (type); |
32197 | |
32198 | return find_type_usage (t: type, pred: is_auto); |
32199 | } |
32200 | |
32201 | /* Recursively walk over && expressions searching for EXPR. Return a reference |
32202 | to that expression. */ |
32203 | |
32204 | static tree *find_template_requirement (tree *t, tree key) |
32205 | { |
32206 | if (*t == key) |
32207 | return t; |
32208 | if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR) |
32209 | { |
32210 | if (tree *p = find_template_requirement (t: &TREE_OPERAND (*t, 0), key)) |
32211 | return p; |
32212 | if (tree *p = find_template_requirement (t: &TREE_OPERAND (*t, 1), key)) |
32213 | return p; |
32214 | } |
32215 | return 0; |
32216 | } |
32217 | |
32218 | /* Convert the generic type parameters in PARM that match the types given in the |
32219 | range [START_IDX, END_IDX) from the current_template_parms into generic type |
32220 | packs. */ |
32221 | |
32222 | tree |
32223 | convert_generic_types_to_packs (tree parm, int start_idx, int end_idx) |
32224 | { |
32225 | tree current = current_template_parms; |
32226 | int depth = TMPL_PARMS_DEPTH (current); |
32227 | current = INNERMOST_TEMPLATE_PARMS (current); |
32228 | tree replacement = make_tree_vec (TREE_VEC_LENGTH (current)); |
32229 | |
32230 | for (int i = 0; i < start_idx; ++i) |
32231 | TREE_VEC_ELT (replacement, i) |
32232 | = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i))); |
32233 | |
32234 | for (int i = start_idx; i < end_idx; ++i) |
32235 | { |
32236 | /* Create a distinct parameter pack type from the current parm and add it |
32237 | to the replacement args to tsubst below into the generic function |
32238 | parameter. */ |
32239 | tree node = TREE_VEC_ELT (current, i); |
32240 | tree o = TREE_TYPE (TREE_VALUE (node)); |
32241 | tree t = copy_type (o); |
32242 | TEMPLATE_TYPE_PARM_INDEX (t) |
32243 | = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o), |
32244 | type: t, levels: 0, args: 0, complain: tf_none); |
32245 | TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t; |
32246 | TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t); |
32247 | TYPE_MAIN_VARIANT (t) = t; |
32248 | TEMPLATE_TYPE_PARAMETER_PACK (t) = true; |
32249 | TYPE_CANONICAL (t) = canonical_type_parameter (type: t); |
32250 | TREE_VEC_ELT (replacement, i) = t; |
32251 | |
32252 | /* Replace the current template parameter with new pack. */ |
32253 | TREE_VALUE (node) = TREE_CHAIN (t); |
32254 | |
32255 | /* Surgically adjust the associated constraint of adjusted parameter |
32256 | and it's corresponding contribution to the current template |
32257 | requirements. */ |
32258 | if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node)) |
32259 | { |
32260 | gcc_assert (concept_check_p (constr)); |
32261 | TREE_VEC_ELT (TREE_OPERAND (constr, 1), 0) = t; |
32262 | /* Use UNKNOWN_LOCATION so write_template_args can tell the |
32263 | difference between this and a fold the user wrote. */ |
32264 | location_t loc = UNKNOWN_LOCATION; |
32265 | tree fold = finish_left_unary_fold_expr (loc, constr, |
32266 | TRUTH_ANDIF_EXPR); |
32267 | TEMPLATE_PARM_CONSTRAINTS (node) = fold; |
32268 | |
32269 | /* If there was a constraint, we also need to replace that in |
32270 | the template requirements, which we've already built. */ |
32271 | tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); |
32272 | reqs = find_template_requirement (t: reqs, key: constr); |
32273 | *reqs = fold; |
32274 | } |
32275 | } |
32276 | |
32277 | for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i) |
32278 | TREE_VEC_ELT (replacement, i) |
32279 | = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i))); |
32280 | |
32281 | /* If there are more levels then build up the replacement with the outer |
32282 | template parms. */ |
32283 | if (depth > 1) |
32284 | replacement = add_to_template_args (args: template_parms_to_args |
32285 | (TREE_CHAIN (current_template_parms)), |
32286 | extra_args: replacement); |
32287 | |
32288 | return tsubst (t: parm, args: replacement, complain: tf_none, NULL_TREE); |
32289 | } |
32290 | |
32291 | /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from |
32292 | 0..N-1. */ |
32293 | |
32294 | void |
32295 | declare_integer_pack (void) |
32296 | { |
32297 | tree ipfn = push_library_fn (get_identifier ("__integer_pack"), |
32298 | build_function_type_list (integer_type_node, |
32299 | integer_type_node, |
32300 | NULL_TREE), |
32301 | NULL_TREE, ECF_CONST); |
32302 | DECL_DECLARED_CONSTEXPR_P (ipfn) = true; |
32303 | set_decl_built_in_function (decl: ipfn, fclass: BUILT_IN_FRONTEND, |
32304 | fcode: CP_BUILT_IN_INTEGER_PACK); |
32305 | } |
32306 | |
32307 | /* Walk the decl or type specialization table calling FN on each |
32308 | entry. */ |
32309 | |
32310 | void |
32311 | walk_specializations (bool decls_p, |
32312 | void (*fn) (bool decls_p, spec_entry *entry, void *data), |
32313 | void *data) |
32314 | { |
32315 | spec_hash_table *table = decls_p ? decl_specializations |
32316 | : type_specializations; |
32317 | spec_hash_table::iterator end (table->end ()); |
32318 | for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter) |
32319 | fn (decls_p, *iter, data); |
32320 | } |
32321 | |
32322 | /* Lookup the specialization of *ELT, in the decl or type |
32323 | specialization table. Return the SPEC that's already there, or |
32324 | NULL if nothing. */ |
32325 | |
32326 | tree |
32327 | match_mergeable_specialization (bool decl_p, spec_entry *elt) |
32328 | { |
32329 | hash_table<spec_hasher> *specializations |
32330 | = decl_p ? decl_specializations : type_specializations; |
32331 | auto *slot = specializations->find_slot (value: elt, insert: NO_INSERT); |
32332 | |
32333 | if (slot) |
32334 | return (*slot)->spec; |
32335 | |
32336 | return NULL_TREE; |
32337 | } |
32338 | |
32339 | /* Return flags encoding whether SPEC is on the instantiation and/or |
32340 | specialization lists of TMPL. */ |
32341 | |
32342 | unsigned |
32343 | get_mergeable_specialization_flags (bool decl_p, tree tmpl, tree decl) |
32344 | { |
32345 | unsigned flags = 0; |
32346 | |
32347 | tree spec = decl_p ? decl : TREE_TYPE (decl); |
32348 | for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl); |
32349 | inst; inst = TREE_CHAIN (inst)) |
32350 | if (TREE_VALUE (inst) == spec) |
32351 | { |
32352 | flags |= 1; |
32353 | break; |
32354 | } |
32355 | |
32356 | if (CLASS_TYPE_P (TREE_TYPE (decl)) |
32357 | && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)) |
32358 | && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2) |
32359 | /* Only need to search if DECL is a partial specialization. */ |
32360 | for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); |
32361 | part; part = TREE_CHAIN (part)) |
32362 | if (TREE_VALUE (part) == decl) |
32363 | { |
32364 | flags |= 2; |
32365 | break; |
32366 | } |
32367 | |
32368 | return flags; |
32369 | } |
32370 | |
32371 | /* Add a new specialization described by SPEC. DECL is the |
32372 | maybe-template decl and FLAGS is as returned from |
32373 | get_mergeable_specialization_flags. */ |
32374 | |
32375 | void |
32376 | add_mergeable_specialization (bool decl_p, spec_entry *elt, tree decl, |
32377 | unsigned flags) |
32378 | { |
32379 | if (decl_p) |
32380 | { |
32381 | auto *slot = decl_specializations->find_slot (value: elt, insert: INSERT); |
32382 | |
32383 | gcc_checking_assert (!*slot); |
32384 | auto entry = ggc_alloc<spec_entry> (); |
32385 | *entry = *elt; |
32386 | *slot = entry; |
32387 | } |
32388 | else |
32389 | { |
32390 | auto *slot = type_specializations->find_slot (value: elt, insert: INSERT); |
32391 | |
32392 | /* We don't distinguish different constrained partial type |
32393 | specializations, so there could be duplicates. In that case we |
32394 | must propagate TYPE_CANONICAL so that they are treated as the |
32395 | same type. Everything else must be new. */ |
32396 | if (*slot) |
32397 | { |
32398 | gcc_checking_assert (flags & 2); |
32399 | TYPE_CANONICAL (elt->spec) = TYPE_CANONICAL ((*slot)->spec); |
32400 | } |
32401 | else |
32402 | { |
32403 | auto entry = ggc_alloc<spec_entry> (); |
32404 | *entry = *elt; |
32405 | *slot = entry; |
32406 | } |
32407 | } |
32408 | |
32409 | if (flags & 1) |
32410 | DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl) |
32411 | = tree_cons (elt->args, elt->spec, |
32412 | DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)); |
32413 | |
32414 | if (flags & 2) |
32415 | { |
32416 | /* A partial specialization. */ |
32417 | tree cons = tree_cons (elt->args, decl, |
32418 | DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl)); |
32419 | TREE_TYPE (cons) = decl_p ? TREE_TYPE (elt->spec) : elt->spec; |
32420 | DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons; |
32421 | set_defining_module_for_partial_spec (STRIP_TEMPLATE (decl)); |
32422 | } |
32423 | } |
32424 | |
32425 | /* Set up the hash tables for template instantiations. */ |
32426 | |
32427 | void |
32428 | init_template_processing (void) |
32429 | { |
32430 | decl_specializations = hash_table<spec_hasher>::create_ggc (n: 37); |
32431 | type_specializations = hash_table<spec_hasher>::create_ggc (n: 37); |
32432 | |
32433 | if (cxx_dialect >= cxx11) |
32434 | declare_integer_pack (); |
32435 | } |
32436 | |
32437 | /* Print stats about the template hash tables for -fstats. */ |
32438 | |
32439 | void |
32440 | print_template_statistics (void) |
32441 | { |
32442 | fprintf (stderr, format: "decl_specializations: size "HOST_SIZE_T_PRINT_DEC ", " |
32443 | HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n", |
32444 | (fmt_size_t) decl_specializations->size (), |
32445 | (fmt_size_t) decl_specializations->elements (), |
32446 | decl_specializations->collisions ()); |
32447 | fprintf (stderr, format: "type_specializations: size "HOST_SIZE_T_PRINT_DEC ", " |
32448 | HOST_SIZE_T_PRINT_DEC " elements, %f collisions\n", |
32449 | (fmt_size_t) type_specializations->size (), |
32450 | (fmt_size_t) type_specializations->elements (), |
32451 | type_specializations->collisions ()); |
32452 | } |
32453 | |
32454 | #if CHECKING_P |
32455 | |
32456 | namespace selftest { |
32457 | |
32458 | /* Verify that type_dependent_expression_p () works correctly, even |
32459 | in the presence of location wrapper nodes. */ |
32460 | |
32461 | static void |
32462 | test_type_dependent_expression_p () |
32463 | { |
32464 | location_t loc = BUILTINS_LOCATION; |
32465 | |
32466 | tree name = get_identifier ("foo"); |
32467 | |
32468 | /* If no templates are involved, nothing is type-dependent. */ |
32469 | gcc_assert (!processing_template_decl); |
32470 | ASSERT_FALSE (type_dependent_expression_p (name)); |
32471 | |
32472 | ++processing_template_decl; |
32473 | |
32474 | /* Within a template, an unresolved name is always type-dependent. */ |
32475 | ASSERT_TRUE (type_dependent_expression_p (name)); |
32476 | |
32477 | /* Ensure it copes with NULL_TREE and errors. */ |
32478 | ASSERT_FALSE (type_dependent_expression_p (NULL_TREE)); |
32479 | ASSERT_FALSE (type_dependent_expression_p (error_mark_node)); |
32480 | |
32481 | /* A USING_DECL in a template should be type-dependent, even if wrapped |
32482 | with a location wrapper (PR c++/83799). */ |
32483 | tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE); |
32484 | TREE_TYPE (using_decl) = integer_type_node; |
32485 | ASSERT_TRUE (type_dependent_expression_p (using_decl)); |
32486 | tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc); |
32487 | ASSERT_TRUE (location_wrapper_p (wrapped_using_decl)); |
32488 | ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl)); |
32489 | |
32490 | --processing_template_decl; |
32491 | } |
32492 | |
32493 | /* Run all of the selftests within this file. */ |
32494 | |
32495 | void |
32496 | cp_pt_cc_tests () |
32497 | { |
32498 | test_type_dependent_expression_p (); |
32499 | } |
32500 | |
32501 | } // namespace selftest |
32502 | |
32503 | #endif /* #if CHECKING_P */ |
32504 | |
32505 | #include "gt-cp-pt.h" |
32506 |
Definitions
- pending_template
- pending_templates
- last_pending_template
- processing_template_parmlist
- template_header_count
- inline_parm_levels
- current_tinst_level
- saved_access_scope
- cur_stmt_expr
- local_specialization_stack
- ~local_specialization_stack
- excessive_deduction_depth
- spec_hasher
- decl_specializations
- type_specializations
- canonical_template_parms
- template_base_result
- push_access_scope
- pop_access_scope
- finish_member_template_decl
- build_template_info
- decl_template_info
- get_template_info
- template_class_depth
- instantiates_primary_template_p
- inline_needs_template_parms
- push_inline_template_parms_recursive
- maybe_begin_member_template_processing
- maybe_end_member_template_processing
- add_to_template_args
- add_outermost_template_args
- get_innermost_template_args
- strip_innermost_template_args
- begin_template_parm_list
- check_specialization_scope
- begin_specialization
- end_specialization
- reset_specialization
- note_template_header
- begin_explicit_instantiation
- end_explicit_instantiation
- check_specialization_namespace
- check_explicit_instantiation_namespace
- maybe_new_partial_specialization
- maybe_process_partial_specialization
- verify_unstripped_args_1
- verify_unstripped_args
- retrieve_specialization
- retrieve_local_specialization
- is_specialization_of
- is_specialization_of_friend
- register_specialization
- comparing_specializations
- comparing_dependent_aliases
- comparing_for_partial_ordering
- equal
- hash_tmpl_and_args
- hash
- hash
- iterative_hash_template_arg
- reregister_specialization
- register_local_specialization
- register_local_identity
- explicit_class_specialization_p
- print_candidates_1
- print_candidates
- get_template_for_ordering
- most_constrained_function
- determine_specialization
- copy_default_args_to_explicit_spec_1
- copy_default_args_to_explicit_spec
- num_template_headers_for_class
- check_template_variable
- check_unqualified_spec_or_inst
- warn_spec_missing_attributes
- check_explicit_specialization
- comp_template_parms
- template_parameter_constraints_equivalent_p
- template_parameters_equivalent_p
- template_parameter_lists_equivalent_p
- template_requirements_equivalent_p
- template_heads_equivalent_p
- template_parameter_pack_p
- function_parameter_pack_p
- get_function_template_decl
- function_parameter_expanded_from_pack_p
- template_args_variadic_p
- make_ith_pack_parameter_name
- primary_template_specialization_p
- template_template_parameter_p
- template_type_parameter_p
- get_primary_template_innermost_parameters
- get_template_innermost_arguments
- get_template_argument_pack_elems
- argument_pack_select_arg
- preserve_args
- builtin_pack_fn_p
- builtin_pack_call_p
- expand_integer_pack
- expand_builtin_pack_call
- has_extra_args_mechanism_p
- tree_extra_args
- find_parameter_pack_data
- find_parameter_packs_r
- uses_parameter_packs
- make_pack_expansion
- make_pack_index
- check_for_bare_parameter_packs
- expand_template_argument_pack
- check_template_shadow
- build_template_parm_index
- ctp_hasher
- hash
- equal
- ctp_table
- canonical_type_parameter
- reduce_template_parm_level
- process_template_parm
- end_template_parm_list
- end_template_parm_list
- end_template_decl
- template_parm_to_arg
- template_arg_to_parm
- template_parms_level_to_args
- template_parms_to_args
- current_template_args
- generic_targs_for
- outer_template_args
- maybe_update_decl_type
- build_template_decl
- template_parm_data
- mark_template_parm
- process_partial_specialization
- get_template_parm_index
- fixed_parameter_pack_p_1
- fixed_parameter_pack_p
- check_default_tmpl_args
- template_parm_this_level_p
- template_parm_outer_level
- push_template_decl
- add_inherited_template_parms
- redeclare_class_template
- instantiate_non_dependent_expr_internal
- instantiate_non_dependent_expr
- instantiate_non_dependent_or_null
- variable_template_specialization_p
- alias_type_or_template_p
- alias_template_specialization_p
- complex_alias_tmpl_info
- uses_all_template_parms_data
- complex_alias_template_r
- complex_alias_template_p
- dependent_alias_template_spec_p
- dependent_opaque_alias_p
- num_innermost_template_parms
- get_underlying_template
- convert_nontype_argument_function
- check_valid_ptrmem_cst_expr
- has_value_dependent_address
- unify_success
- unify_invalid
- unify_parameter_deduction_failure
- unify_cv_qual_mismatch
- unify_type_mismatch
- unify_parameter_pack_mismatch
- unify_ptrmem_cst_mismatch
- unify_expression_unequal
- unify_parameter_pack_inconsistent
- unify_inconsistency
- unify_vla_arg
- unify_method_type_error
- unify_arity
- unify_too_many_arguments
- unify_too_few_arguments
- unify_arg_conversion
- unify_no_common_base
- unify_inconsistent_template_template_parameters
- unify_template_deduction_failure
- unify_template_argument_mismatch
- invalid_tparm_referent_p
- create_template_parm_object
- tparm_obj_values
- get_template_parm_object
- tparm_object_argument
- convert_nontype_argument
- coerce_template_template_parm
- coerce_template_args_for_ttp
- defaulted_ttp_cache
- add_defaults_to_ttp
- coerce_ttp_args_for_tta
- unify_bound_ttp_args
- coerce_template_template_parms
- template_template_parm_bindings_ok_p
- canonicalize_type_argument
- canonicalize_expr_argument
- is_compatible_template_arg
- maybe_convert_nontype_argument
- convert_template_argument
- coerce_template_parameter_pack
- pack_expansion_args_count
- coerce_template_parms
- class_nttp_const_wrapper_p
- template_args_equal
- comp_template_args
- comp_template_args_porder
- freelist
- next
- anew
- poison
- reinit
- freelist
- free
- alloc
- next
- anew
- poison
- reinit
- tree_list_freelist_head
- tree_list_freelist
- tinst_level_freelist_head
- tinst_level_freelist
- pending_template_freelist_head
- pending_template_freelist
- to_list
- refcount_infinity
- inc_refcount_use
- free
- dec_refcount_use
- set_refcount_ptr
- add_pending_template
- complain_about_tu_local_entity
- lookup_template_function
- maybe_get_template_decl_from_type_decl
- adjust_type_for_entering_scope
- tsubst_entering_scope
- lookup_template_class
- lookup_template_variable
- finish_template_variable
- lookup_and_finish_template_variable
- corresponding_template_parameter_list
- corresponding_template_parameter_list
- corresponding_template_parameter
- pair_fn_data
- for_each_template_parm_r
- for_each_template_parm
- find_template_parameter_info
- find_template_parameter_info
- num_found
- keep_template_parm
- any_template_parm_r
- find_in
- find_in_recursive
- found
- find_template_parameters
- uses_template_parms
- uses_template_parms_level
- uses_outer_template_parms
- uses_outer_template_parms_in_constraints
- neglectable_inst_p
- limit_bad_template_recursion
- tinst_depth
- depth_reached
- tinst_dump_id
- last_error_tinst_level
- push_tinst_level_loc
- push_tinst_level
- push_tinst_level
- push_tinst_level_loc
- pop_tinst_level
- tinst_complete_p
- reopen_tinst_level
- outermost_tinst_level
- non_templated_friend_p
- tsubst_friend_function
- tsubst_friend_class
- can_complete_type_without_circularity
- tsubst_contract
- tsubst_contract_attribute
- tsubst_contract_attributes
- tsubst_attribute
- tsubst_attributes
- apply_late_template_attributes
- perform_instantiation_time_access_checks
- maybe_diagnose_erroneous_template
- instantiate_class_template
- tsubst_template_arg
- extract_fnparm_pack
- make_fnparm_pack
- argument_pack_element_is_expansion_p
- make_argument_pack_select
- use_pack_expansion_extra_args_p
- gen_elem_of_pack_expansion_instantiation
- expand_empty_fold
- fold_expression
- tsubst_fold_expr_pack
- tsubst_fold_expr_init
- expand_left_fold
- tsubst_unary_left_fold
- tsubst_binary_left_fold
- expand_right_fold
- tsubst_unary_right_fold
- tsubst_binary_right_fold
- el_data
- el_data
- extract_locals_r
- extract_local_specs
- build_extra_args
- add_extra_args
- tsubst_pack_expansion
- tsubst_pack_index
- make_argument_pack
- copy_template_args
- tsubst_argument_pack
- tsubst_template_args
- tsubst_template_parms_level
- tsubst_template_parms
- tsubst_template_parm
- tsubst_each_template_parm_constraints
- defarg_inst
- defarg_insts_for
- tsubst_default_argument
- tsubst_default_arguments
- explicit_specifier_map
- store_explicit_specifier
- lookup_explicit_specifier
- rebuild_function_or_method_type
- maybe_rebuild_function_decl_type
- tsubst_function_decl
- tsubst_template_decl
- lambda_fn_in_template_p
- regenerated_lambda_fn_p
- most_general_lambda
- lambda_regenerating_args
- enclosing_instantiation_of
- tsubst_decl
- tsubst_function_parms
- tsubst_arg_types
- tsubst_function_type
- tsubst_exception_specification
- tsubst_tree_list
- tsubst
- tsubst_scope
- tsubst_name
- filter_memfn_lookup
- tsubst_baselink
- tsubst_qualified_id
- tsubst_init
- maybe_dependent_member_ref
- tsubst_omp_clause_decl
- tsubst_omp_clauses
- tsubst_omp_context_selector
- tsubst_copy_asm_operands
- omp_parallel_combined_clauses
- tsubst_omp_for_iterator
- tsubst_find_omp_teams
- tsubst_decomp_names
- lookup_init_capture_pack
- dependent_operand_p
- tsubst_stmt
- tsubst_omp_udr
- tsubst_non_call_postfix_expression
- prepend_one_capture
- tsubst_lambda_expr
- fold_targs_r
- maybe_fold_fn_template_args
- tsubst_call_args
- tsubst_expr
- check_instantiated_arg
- check_instantiated_args
- mark_template_arguments_used
- recheck_decl_substitution
- instantiate_template
- instantiate_alias_template
- pack_deducible_p
- check_non_deducible_conversions
- fn_type_unification
- forwarding_reference_p
- maybe_adjust_types_for_deduction
- conversion_may_instantiate_p
- check_non_deducible_conversion
- deducible_expression
- deducible_array_bound
- deducible_template_args
- uses_deducible_template_parms
- unify_one_argument
- zero_r
- array_deduction_r
- try_array_deduction
- type_unification_real
- resolve_overloaded_unification
- resolve_nondeduced_context
- resolve_nondeduced_context_or_error
- try_one_overload
- try_class_unification
- get_template_base
- template_decl_level
- check_cv_quals_for_unify
- template_parm_level_and_index
- unify_pack_expansion
- unify_array_domain
- pa_kind_t
- pa_kind
- unify
- mark_definable
- setup_explicit_instantiation_definition_linkage
- mark_decl_instantiated
- check_undeduced_parms
- more_specialized_fn
- more_specialized_partial_spec
- get_bindings
- get_partial_spec_bindings
- more_specialized_inst
- most_specialized_instantiation
- most_general_template
- most_specialized_partial_spec
- do_decl_instantiation
- mark_class_instantiated
- do_type_instantiation
- regenerate_decl_from_template
- template_for_substitution
- always_instantiate_p
- maybe_instantiate_noexcept
- register_parameter_specializations
- instantiate_body
- instantiate_decl
- instantiate_pending_templates
- tsubst_initializer_list
- tsubst_enum
- get_mostly_instantiated_function_type
- problematic_instantiation_changed
- record_last_problematic_instantiation
- current_instantiation
- instantiating_current_function_p
- invalid_nontype_parm_type_p
- value_dependent_noexcept_spec_p
- dependent_type_p_r
- dependent_type_p
- dependent_scope_p
- dependentish_scope_p
- unknown_base_ref_p
- instantiation_dependent_scope_ref_p
- value_dependent_expression_p
- type_dependent_expression_p
- type_dependent_object_expression_p
- instantiation_dependent_r
- instantiation_dependent_uneval_expression_p
- instantiation_dependent_expression_p
- type_dependent_expression_p_push
- any_type_dependent_arguments_p
- any_type_dependent_elements_p
- any_value_dependent_elements_p
- dependent_template_arg_p
- find_parm_usage_r
- any_template_arguments_need_structural_equality_p
- any_dependent_template_arguments_p
- any_erroneous_template_args_p
- dependent_template_p
- dependent_template_id_p
- dependent_omp_for_p
- resolve_typename_type
- make_auto_1
- make_decltype_auto
- make_auto
- make_template_placeholder
- template_placeholder_p
- make_cast_auto
- make_constrained_placeholder_type
- make_constrained_auto
- make_constrained_decltype_auto
- placeholder_type_constraint_dependent_p
- start_concept_definition
- finish_concept_definition
- listify
- listify_autos
- auto_hash
- hash
- equal
- dguide_base
- dguide_name
- dguide_name_p
- deduction_guide_p
- copy_guide_p
- template_guide_p
- builtin_guide_p
- inherited_guide_p
- set_inherited_guide_context
- rewrite_template_parm
- rewrite_tparm_list
- build_deduction_guide
- collect_ctor_idx_types
- is_spec_or_derived
- maybe_aggr_guide
- alias_ctad_tweaks
- inherited_ctad_tweaks
- type_targs_deducible_from
- ctor_deduction_guides_for
- dguide_cache
- deduction_guides_for
- ctad_template_p
- do_class_deduction
- unparenthesized_id_or_class_member_access_p
- do_auto_deduction
- splice_late_return_type
- is_auto
- is_auto_r
- type_uses_auto
- find_template_requirement
- convert_generic_types_to_packs
- declare_integer_pack
- walk_specializations
- match_mergeable_specialization
- get_mergeable_specialization_flags
- add_mergeable_specialization
- init_template_processing
- print_template_statistics
- test_type_dependent_expression_p
Learn to use CMake with our Intro Training
Find out more