constraint.cc revision 1.6 1 1.1 mrg /* Processing rules for constraints.
2 1.6 mrg Copyright (C) 2013-2020 Free Software Foundation, Inc.
3 1.1 mrg Contributed by Andrew Sutton (andrew.n.sutton (at) gmail.com)
4 1.1 mrg
5 1.1 mrg This file is part of GCC.
6 1.1 mrg
7 1.1 mrg GCC is free software; you can redistribute it and/or modify
8 1.1 mrg it under the terms of the GNU General Public License as published by
9 1.1 mrg the Free Software Foundation; either version 3, or (at your option)
10 1.1 mrg any later version.
11 1.1 mrg
12 1.1 mrg GCC is distributed in the hope that it will be useful,
13 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 mrg GNU General Public License for more details.
16 1.1 mrg
17 1.1 mrg You should have received a copy of the GNU General Public License
18 1.1 mrg along with GCC; see the file COPYING3. If not see
19 1.1 mrg <http://www.gnu.org/licenses/>. */
20 1.1 mrg
21 1.1 mrg #include "config.h"
22 1.1 mrg #include "system.h"
23 1.1 mrg #include "coretypes.h"
24 1.1 mrg #include "tm.h"
25 1.1 mrg #include "timevar.h"
26 1.1 mrg #include "hash-set.h"
27 1.1 mrg #include "machmode.h"
28 1.1 mrg #include "vec.h"
29 1.1 mrg #include "double-int.h"
30 1.1 mrg #include "input.h"
31 1.1 mrg #include "alias.h"
32 1.1 mrg #include "symtab.h"
33 1.1 mrg #include "wide-int.h"
34 1.1 mrg #include "inchash.h"
35 1.1 mrg #include "tree.h"
36 1.1 mrg #include "stringpool.h"
37 1.1 mrg #include "attribs.h"
38 1.1 mrg #include "intl.h"
39 1.1 mrg #include "flags.h"
40 1.1 mrg #include "cp-tree.h"
41 1.1 mrg #include "c-family/c-common.h"
42 1.1 mrg #include "c-family/c-objc.h"
43 1.1 mrg #include "cp-objcp-common.h"
44 1.1 mrg #include "tree-inline.h"
45 1.1 mrg #include "decl.h"
46 1.1 mrg #include "toplev.h"
47 1.1 mrg #include "type-utils.h"
48 1.1 mrg
49 1.6 mrg static tree satisfaction_value (tree t);
50 1.6 mrg
51 1.6 mrg /* When we're parsing or substuting a constraint expression, we have slightly
52 1.6 mrg different expression semantics. In particular, we don't want to reduce a
53 1.6 mrg concept-id to a satisfaction value. */
54 1.6 mrg
55 1.6 mrg processing_constraint_expression_sentinel::
56 1.6 mrg processing_constraint_expression_sentinel ()
57 1.6 mrg {
58 1.6 mrg ++scope_chain->x_processing_constraint;
59 1.6 mrg }
60 1.6 mrg
61 1.6 mrg processing_constraint_expression_sentinel::
62 1.6 mrg ~processing_constraint_expression_sentinel ()
63 1.6 mrg {
64 1.6 mrg --scope_chain->x_processing_constraint;
65 1.6 mrg }
66 1.6 mrg
67 1.6 mrg bool
68 1.6 mrg processing_constraint_expression_p ()
69 1.6 mrg {
70 1.6 mrg return scope_chain->x_processing_constraint != 0;
71 1.6 mrg }
72 1.6 mrg
73 1.1 mrg /*---------------------------------------------------------------------------
74 1.6 mrg Constraint expressions
75 1.1 mrg ---------------------------------------------------------------------------*/
76 1.1 mrg
77 1.6 mrg /* Information provided to substitution. */
78 1.6 mrg
79 1.6 mrg struct subst_info
80 1.6 mrg {
81 1.6 mrg subst_info (tsubst_flags_t cmp, tree in)
82 1.6 mrg : complain (cmp), in_decl (in)
83 1.6 mrg { }
84 1.6 mrg
85 1.6 mrg /* True if we should not diagnose errors. */
86 1.6 mrg bool quiet() const
87 1.6 mrg {
88 1.6 mrg return complain == tf_none;
89 1.6 mrg }
90 1.6 mrg
91 1.6 mrg /* True if we should diagnose errors. */
92 1.6 mrg bool noisy() const
93 1.6 mrg {
94 1.6 mrg return !quiet ();
95 1.6 mrg }
96 1.6 mrg
97 1.6 mrg tsubst_flags_t complain;
98 1.6 mrg tree in_decl;
99 1.6 mrg };
100 1.6 mrg
101 1.6 mrg static tree satisfy_constraint (tree, tree, subst_info);
102 1.6 mrg
103 1.6 mrg /* True if T is known to be some type other than bool. Note that this
104 1.6 mrg is false for dependent types and errors. */
105 1.1 mrg
106 1.1 mrg static inline bool
107 1.6 mrg known_non_bool_p (tree t)
108 1.1 mrg {
109 1.6 mrg return (t && !WILDCARD_TYPE_P (t) && TREE_CODE (t) != BOOLEAN_TYPE);
110 1.1 mrg }
111 1.1 mrg
112 1.6 mrg static bool
113 1.6 mrg check_constraint_atom (cp_expr expr)
114 1.6 mrg {
115 1.6 mrg if (known_non_bool_p (TREE_TYPE (expr)))
116 1.6 mrg {
117 1.6 mrg error_at (expr.get_location (),
118 1.6 mrg "constraint expression does not have type %<bool%>");
119 1.6 mrg return false;
120 1.6 mrg }
121 1.6 mrg
122 1.6 mrg /* Check that we're using function concepts correctly. */
123 1.6 mrg if (concept_check_p (expr))
124 1.6 mrg {
125 1.6 mrg tree id = unpack_concept_check (expr);
126 1.6 mrg tree tmpl = TREE_OPERAND (id, 0);
127 1.6 mrg if (OVL_P (tmpl) && TREE_CODE (expr) == TEMPLATE_ID_EXPR)
128 1.6 mrg {
129 1.6 mrg error_at (EXPR_LOC_OR_LOC (expr, input_location),
130 1.6 mrg "function concept must be called");
131 1.6 mrg return false;
132 1.6 mrg }
133 1.6 mrg }
134 1.6 mrg
135 1.6 mrg return true;
136 1.6 mrg }
137 1.1 mrg
138 1.6 mrg static bool
139 1.6 mrg check_constraint_operands (location_t, cp_expr lhs, cp_expr rhs)
140 1.1 mrg {
141 1.6 mrg return check_constraint_atom (lhs) && check_constraint_atom (rhs);
142 1.1 mrg }
143 1.1 mrg
144 1.6 mrg /* Validate the semantic properties of the constraint expression. */
145 1.6 mrg
146 1.6 mrg static cp_expr
147 1.6 mrg finish_constraint_binary_op (location_t loc,
148 1.6 mrg tree_code code,
149 1.6 mrg cp_expr lhs,
150 1.6 mrg cp_expr rhs)
151 1.6 mrg {
152 1.6 mrg gcc_assert (processing_constraint_expression_p ());
153 1.6 mrg if (lhs == error_mark_node || rhs == error_mark_node)
154 1.6 mrg return error_mark_node;
155 1.6 mrg if (!check_constraint_operands (loc, lhs, rhs))
156 1.6 mrg return error_mark_node;
157 1.6 mrg tree overload;
158 1.6 mrg cp_expr expr = build_x_binary_op (loc, code,
159 1.6 mrg lhs, TREE_CODE (lhs),
160 1.6 mrg rhs, TREE_CODE (rhs),
161 1.6 mrg &overload, tf_none);
162 1.6 mrg /* When either operand is dependent, the overload set may be non-empty. */
163 1.6 mrg if (expr == error_mark_node)
164 1.6 mrg return error_mark_node;
165 1.6 mrg expr.set_location (loc);
166 1.6 mrg expr.set_range (lhs.get_start (), rhs.get_finish ());
167 1.6 mrg return expr;
168 1.6 mrg }
169 1.1 mrg
170 1.6 mrg cp_expr
171 1.6 mrg finish_constraint_or_expr (location_t loc, cp_expr lhs, cp_expr rhs)
172 1.6 mrg {
173 1.6 mrg return finish_constraint_binary_op (loc, TRUTH_ORIF_EXPR, lhs, rhs);
174 1.6 mrg }
175 1.1 mrg
176 1.6 mrg cp_expr
177 1.6 mrg finish_constraint_and_expr (location_t loc, cp_expr lhs, cp_expr rhs)
178 1.6 mrg {
179 1.6 mrg return finish_constraint_binary_op (loc, TRUTH_ANDIF_EXPR, lhs, rhs);
180 1.6 mrg }
181 1.1 mrg
182 1.6 mrg cp_expr
183 1.6 mrg finish_constraint_primary_expr (cp_expr expr)
184 1.6 mrg {
185 1.6 mrg if (expr == error_mark_node)
186 1.6 mrg return error_mark_node;
187 1.6 mrg if (!check_constraint_atom (expr))
188 1.6 mrg return cp_expr (error_mark_node, expr.get_location ());
189 1.6 mrg return expr;
190 1.6 mrg }
191 1.1 mrg
192 1.6 mrg /* Combine two constraint-expressions with a logical-and. */
193 1.1 mrg
194 1.1 mrg tree
195 1.6 mrg combine_constraint_expressions (tree lhs, tree rhs)
196 1.1 mrg {
197 1.6 mrg processing_constraint_expression_sentinel pce;
198 1.6 mrg if (!lhs)
199 1.6 mrg return rhs;
200 1.6 mrg if (!rhs)
201 1.6 mrg return lhs;
202 1.6 mrg return finish_constraint_and_expr (input_location, lhs, rhs);
203 1.1 mrg }
204 1.1 mrg
205 1.6 mrg /* Extract the template-id from a concept check. For standard and variable
206 1.6 mrg checks, this is simply T. For function concept checks, this is the
207 1.6 mrg called function. */
208 1.1 mrg
209 1.1 mrg tree
210 1.6 mrg unpack_concept_check (tree t)
211 1.1 mrg {
212 1.6 mrg gcc_assert (concept_check_p (t));
213 1.6 mrg
214 1.6 mrg if (TREE_CODE (t) == CALL_EXPR)
215 1.6 mrg t = CALL_EXPR_FN (t);
216 1.6 mrg
217 1.6 mrg gcc_assert (TREE_CODE (t) == TEMPLATE_ID_EXPR);
218 1.6 mrg return t;
219 1.1 mrg }
220 1.1 mrg
221 1.6 mrg /* Extract the TEMPLATE_DECL from a concept check. */
222 1.1 mrg
223 1.6 mrg tree
224 1.6 mrg get_concept_check_template (tree t)
225 1.1 mrg {
226 1.6 mrg tree id = unpack_concept_check (t);
227 1.6 mrg tree tmpl = TREE_OPERAND (id, 0);
228 1.6 mrg if (OVL_P (tmpl))
229 1.6 mrg tmpl = OVL_FIRST (tmpl);
230 1.6 mrg return tmpl;
231 1.1 mrg }
232 1.1 mrg
233 1.6 mrg /* Returns true if any of the arguments in the template argument list is
234 1.6 mrg a wildcard or wildcard pack. */
235 1.1 mrg
236 1.1 mrg bool
237 1.1 mrg contains_wildcard_p (tree args)
238 1.1 mrg {
239 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
240 1.1 mrg {
241 1.1 mrg tree arg = TREE_VEC_ELT (args, i);
242 1.1 mrg if (TREE_CODE (arg) == WILDCARD_DECL)
243 1.1 mrg return true;
244 1.1 mrg }
245 1.1 mrg return false;
246 1.1 mrg }
247 1.1 mrg
248 1.1 mrg /*---------------------------------------------------------------------------
249 1.1 mrg Resolution of qualified concept names
250 1.1 mrg ---------------------------------------------------------------------------*/
251 1.1 mrg
252 1.6 mrg /* This facility is used to resolve constraint checks from requirement
253 1.6 mrg expressions. A constraint check is a call to a function template declared
254 1.6 mrg with the keyword 'concept'.
255 1.6 mrg
256 1.6 mrg The result of resolution is a pair (a TREE_LIST) whose value is the
257 1.6 mrg matched declaration, and whose purpose contains the coerced template
258 1.6 mrg arguments that can be substituted into the call. */
259 1.6 mrg
260 1.6 mrg /* Given an overload set OVL, try to find a unique definition that can be
261 1.6 mrg instantiated by the template arguments ARGS.
262 1.6 mrg
263 1.6 mrg This function is not called for arbitrary call expressions. In particular,
264 1.6 mrg the call expression must be written with explicit template arguments
265 1.6 mrg and no function arguments. For example:
266 1.6 mrg
267 1.6 mrg f<T, U>()
268 1.6 mrg
269 1.6 mrg If a single match is found, this returns a TREE_LIST whose VALUE
270 1.6 mrg is the constraint function (not the template), and its PURPOSE is
271 1.6 mrg the complete set of arguments substituted into the parameter list. */
272 1.1 mrg
273 1.1 mrg static tree
274 1.6 mrg resolve_function_concept_overload (tree ovl, tree args)
275 1.1 mrg {
276 1.1 mrg int nerrs = 0;
277 1.1 mrg tree cands = NULL_TREE;
278 1.4 mrg for (lkp_iterator iter (ovl); iter; ++iter)
279 1.1 mrg {
280 1.4 mrg tree tmpl = *iter;
281 1.1 mrg if (TREE_CODE (tmpl) != TEMPLATE_DECL)
282 1.1 mrg continue;
283 1.1 mrg
284 1.6 mrg /* Don't try to deduce checks for non-concepts. We often end up trying
285 1.6 mrg to resolve constraints in functional casts as part of a
286 1.6 mrg postfix-expression. We can save time and headaches by not
287 1.6 mrg instantiating those declarations.
288 1.6 mrg
289 1.6 mrg NOTE: This masks a potential error, caused by instantiating
290 1.6 mrg non-deduced contexts using placeholder arguments. */
291 1.1 mrg tree fn = DECL_TEMPLATE_RESULT (tmpl);
292 1.1 mrg if (DECL_ARGUMENTS (fn))
293 1.1 mrg continue;
294 1.1 mrg if (!DECL_DECLARED_CONCEPT_P (fn))
295 1.1 mrg continue;
296 1.1 mrg
297 1.6 mrg /* Remember the candidate if we can deduce a substitution. */
298 1.1 mrg ++processing_template_decl;
299 1.1 mrg tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
300 1.1 mrg if (tree subst = coerce_template_parms (parms, args, tmpl))
301 1.1 mrg {
302 1.1 mrg if (subst == error_mark_node)
303 1.1 mrg ++nerrs;
304 1.1 mrg else
305 1.1 mrg cands = tree_cons (subst, fn, cands);
306 1.1 mrg }
307 1.1 mrg --processing_template_decl;
308 1.1 mrg }
309 1.1 mrg
310 1.1 mrg if (!cands)
311 1.1 mrg /* We either had no candidates or failed deductions. */
312 1.1 mrg return nerrs ? error_mark_node : NULL_TREE;
313 1.1 mrg else if (TREE_CHAIN (cands))
314 1.1 mrg /* There are multiple candidates. */
315 1.1 mrg return error_mark_node;
316 1.1 mrg
317 1.1 mrg return cands;
318 1.1 mrg }
319 1.1 mrg
320 1.6 mrg /* Determine if the call expression CALL is a constraint check, and
321 1.6 mrg return the concept declaration and arguments being checked. If CALL
322 1.6 mrg does not denote a constraint check, return NULL. */
323 1.6 mrg
324 1.1 mrg tree
325 1.6 mrg resolve_function_concept_check (tree call)
326 1.1 mrg {
327 1.1 mrg gcc_assert (TREE_CODE (call) == CALL_EXPR);
328 1.1 mrg
329 1.6 mrg /* A constraint check must be only a template-id expression.
330 1.6 mrg If it's a call to a base-link, its function(s) should be a
331 1.6 mrg template-id expression. If this is not a template-id, then
332 1.6 mrg it cannot be a concept-check. */
333 1.1 mrg tree target = CALL_EXPR_FN (call);
334 1.1 mrg if (BASELINK_P (target))
335 1.1 mrg target = BASELINK_FUNCTIONS (target);
336 1.1 mrg if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
337 1.1 mrg return NULL_TREE;
338 1.1 mrg
339 1.6 mrg /* Get the overload set and template arguments and try to
340 1.6 mrg resolve the target. */
341 1.1 mrg tree ovl = TREE_OPERAND (target, 0);
342 1.1 mrg
343 1.6 mrg /* This is a function call of a variable concept... ill-formed. */
344 1.1 mrg if (TREE_CODE (ovl) == TEMPLATE_DECL)
345 1.1 mrg {
346 1.1 mrg error_at (location_of (call),
347 1.1 mrg "function call of variable concept %qE", call);
348 1.1 mrg return error_mark_node;
349 1.1 mrg }
350 1.1 mrg
351 1.1 mrg tree args = TREE_OPERAND (target, 1);
352 1.6 mrg return resolve_function_concept_overload (ovl, args);
353 1.1 mrg }
354 1.1 mrg
355 1.6 mrg /* Returns a pair containing the checked concept and its associated
356 1.6 mrg prototype parameter. The result is a TREE_LIST whose TREE_VALUE
357 1.6 mrg is the concept (non-template) and whose TREE_PURPOSE contains
358 1.6 mrg the converted template arguments, including the deduced prototype
359 1.6 mrg parameter (in position 0). */
360 1.1 mrg
361 1.1 mrg tree
362 1.6 mrg resolve_concept_check (tree check)
363 1.1 mrg {
364 1.6 mrg gcc_assert (concept_check_p (check));
365 1.6 mrg tree id = unpack_concept_check (check);
366 1.1 mrg tree tmpl = TREE_OPERAND (id, 0);
367 1.1 mrg
368 1.6 mrg /* If this is an overloaded function concept, perform overload
369 1.6 mrg resolution (this only happens when deducing prototype parameters
370 1.6 mrg and template introductions). */
371 1.6 mrg if (TREE_CODE (tmpl) == OVERLOAD)
372 1.6 mrg {
373 1.6 mrg if (OVL_CHAIN (tmpl))
374 1.6 mrg return resolve_function_concept_check (check);
375 1.6 mrg tmpl = OVL_FIRST (tmpl);
376 1.6 mrg }
377 1.1 mrg
378 1.6 mrg tree args = TREE_OPERAND (id, 1);
379 1.1 mrg tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
380 1.1 mrg ++processing_template_decl;
381 1.1 mrg tree result = coerce_template_parms (parms, args, tmpl);
382 1.1 mrg --processing_template_decl;
383 1.6 mrg if (result == error_mark_node)
384 1.1 mrg return error_mark_node;
385 1.6 mrg return build_tree_list (result, DECL_TEMPLATE_RESULT (tmpl));
386 1.1 mrg }
387 1.1 mrg
388 1.6 mrg /* Given a call expression or template-id expression to a concept EXPR
389 1.6 mrg possibly including a wildcard, deduce the concept being checked and
390 1.6 mrg the prototype parameter. Returns true if the constraint and prototype
391 1.6 mrg can be deduced and false otherwise. Note that the CHECK and PROTO
392 1.6 mrg arguments are set to NULL_TREE if this returns false. */
393 1.1 mrg
394 1.1 mrg bool
395 1.1 mrg deduce_constrained_parameter (tree expr, tree& check, tree& proto)
396 1.1 mrg {
397 1.6 mrg tree info = resolve_concept_check (expr);
398 1.1 mrg if (info && info != error_mark_node)
399 1.1 mrg {
400 1.1 mrg check = TREE_VALUE (info);
401 1.1 mrg tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
402 1.1 mrg if (ARGUMENT_PACK_P (arg))
403 1.1 mrg arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
404 1.1 mrg proto = TREE_TYPE (arg);
405 1.1 mrg return true;
406 1.1 mrg }
407 1.6 mrg
408 1.1 mrg check = proto = NULL_TREE;
409 1.1 mrg return false;
410 1.1 mrg }
411 1.1 mrg
412 1.6 mrg /* Given a call expression or template-id expression to a concept, EXPR,
413 1.6 mrg deduce the concept being checked and return the template arguments.
414 1.6 mrg Returns NULL_TREE if deduction fails. */
415 1.6 mrg static tree
416 1.6 mrg deduce_concept_introduction (tree check)
417 1.6 mrg {
418 1.6 mrg tree info = resolve_concept_check (check);
419 1.1 mrg if (info && info != error_mark_node)
420 1.1 mrg return TREE_PURPOSE (info);
421 1.1 mrg return NULL_TREE;
422 1.1 mrg }
423 1.1 mrg
424 1.6 mrg /* Build a constrained placeholder type where SPEC is a type-constraint.
425 1.6 mrg SPEC can be anything were concept_definition_p is true.
426 1.1 mrg
427 1.6 mrg If DECLTYPE_P is true, then the placeholder is decltype(auto).
428 1.1 mrg
429 1.6 mrg Returns a pair whose FIRST is the concept being checked and whose
430 1.6 mrg SECOND is the prototype parameter. */
431 1.1 mrg
432 1.6 mrg tree_pair
433 1.6 mrg finish_type_constraints (tree spec, tree args, tsubst_flags_t complain)
434 1.1 mrg {
435 1.6 mrg gcc_assert (concept_definition_p (spec));
436 1.1 mrg
437 1.6 mrg /* Build an initial concept check. */
438 1.6 mrg tree check = build_type_constraint (spec, args, complain);
439 1.6 mrg if (check == error_mark_node)
440 1.6 mrg return std::make_pair (error_mark_node, NULL_TREE);
441 1.1 mrg
442 1.6 mrg /* Extract the concept and prototype parameter from the check. */
443 1.6 mrg tree con;
444 1.6 mrg tree proto;
445 1.6 mrg if (!deduce_constrained_parameter (check, con, proto))
446 1.6 mrg return std::make_pair (error_mark_node, NULL_TREE);
447 1.1 mrg
448 1.6 mrg return std::make_pair (con, proto);
449 1.1 mrg }
450 1.1 mrg
451 1.1 mrg /*---------------------------------------------------------------------------
452 1.1 mrg Expansion of concept definitions
453 1.1 mrg ---------------------------------------------------------------------------*/
454 1.1 mrg
455 1.1 mrg /* Returns the expression of a function concept. */
456 1.1 mrg
457 1.6 mrg static tree
458 1.1 mrg get_returned_expression (tree fn)
459 1.1 mrg {
460 1.1 mrg /* Extract the body of the function minus the return expression. */
461 1.1 mrg tree body = DECL_SAVED_TREE (fn);
462 1.1 mrg if (!body)
463 1.1 mrg return error_mark_node;
464 1.1 mrg if (TREE_CODE (body) == BIND_EXPR)
465 1.1 mrg body = BIND_EXPR_BODY (body);
466 1.1 mrg if (TREE_CODE (body) != RETURN_EXPR)
467 1.1 mrg return error_mark_node;
468 1.1 mrg
469 1.1 mrg return TREE_OPERAND (body, 0);
470 1.1 mrg }
471 1.1 mrg
472 1.1 mrg /* Returns the initializer of a variable concept. */
473 1.1 mrg
474 1.6 mrg static tree
475 1.1 mrg get_variable_initializer (tree var)
476 1.1 mrg {
477 1.1 mrg tree init = DECL_INITIAL (var);
478 1.1 mrg if (!init)
479 1.1 mrg return error_mark_node;
480 1.6 mrg if (BRACE_ENCLOSED_INITIALIZER_P (init)
481 1.6 mrg && CONSTRUCTOR_NELTS (init) == 1)
482 1.6 mrg init = CONSTRUCTOR_ELT (init, 0)->value;
483 1.1 mrg return init;
484 1.1 mrg }
485 1.1 mrg
486 1.1 mrg /* Returns the definition of a variable or function concept. */
487 1.1 mrg
488 1.6 mrg static tree
489 1.1 mrg get_concept_definition (tree decl)
490 1.1 mrg {
491 1.6 mrg if (TREE_CODE (decl) == OVERLOAD)
492 1.6 mrg decl = OVL_FIRST (decl);
493 1.6 mrg
494 1.6 mrg if (TREE_CODE (decl) == TEMPLATE_DECL)
495 1.6 mrg decl = DECL_TEMPLATE_RESULT (decl);
496 1.6 mrg
497 1.6 mrg if (TREE_CODE (decl) == CONCEPT_DECL)
498 1.6 mrg return DECL_INITIAL (decl);
499 1.3 mrg if (VAR_P (decl))
500 1.1 mrg return get_variable_initializer (decl);
501 1.6 mrg if (TREE_CODE (decl) == FUNCTION_DECL)
502 1.1 mrg return get_returned_expression (decl);
503 1.1 mrg gcc_unreachable ();
504 1.1 mrg }
505 1.1 mrg
506 1.6 mrg /*---------------------------------------------------------------------------
507 1.6 mrg Normalization of expressions
508 1.6 mrg
509 1.6 mrg This set of functions will transform an expression into a constraint
510 1.6 mrg in a sequence of steps.
511 1.6 mrg ---------------------------------------------------------------------------*/
512 1.6 mrg
513 1.6 mrg void
514 1.6 mrg debug_parameter_mapping (tree map)
515 1.6 mrg {
516 1.6 mrg for (tree p = map; p; p = TREE_CHAIN (p))
517 1.6 mrg {
518 1.6 mrg tree parm = TREE_VALUE (p);
519 1.6 mrg tree arg = TREE_PURPOSE (p);
520 1.6 mrg if (TYPE_P (parm))
521 1.6 mrg verbatim ("MAP %qD TO %qT", TEMPLATE_TYPE_DECL (parm), arg);
522 1.6 mrg else
523 1.6 mrg verbatim ("MAP %qD TO %qE", TEMPLATE_PARM_DECL (parm), arg);
524 1.6 mrg // debug_tree (parm);
525 1.6 mrg // debug_tree (arg);
526 1.6 mrg }
527 1.6 mrg }
528 1.6 mrg
529 1.6 mrg void
530 1.6 mrg debug_argument_list (tree args)
531 1.6 mrg {
532 1.6 mrg for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
533 1.6 mrg {
534 1.6 mrg tree arg = TREE_VEC_ELT (args, i);
535 1.6 mrg if (TYPE_P (arg))
536 1.6 mrg verbatim ("ARG %qT", arg);
537 1.6 mrg else
538 1.6 mrg verbatim ("ARG %qE", arg);
539 1.6 mrg }
540 1.6 mrg }
541 1.6 mrg
542 1.6 mrg /* Associate each parameter in PARMS with its corresponding template
543 1.6 mrg argument in ARGS. */
544 1.1 mrg
545 1.6 mrg static tree
546 1.6 mrg map_arguments (tree parms, tree args)
547 1.1 mrg {
548 1.6 mrg for (tree p = parms; p; p = TREE_CHAIN (p))
549 1.6 mrg if (args)
550 1.6 mrg {
551 1.6 mrg int level;
552 1.6 mrg int index;
553 1.6 mrg template_parm_level_and_index (TREE_VALUE (p), &level, &index);
554 1.6 mrg TREE_PURPOSE (p) = TMPL_ARG (args, level, index);
555 1.6 mrg }
556 1.6 mrg else
557 1.6 mrg TREE_PURPOSE (p) = template_parm_to_arg (p);
558 1.6 mrg
559 1.6 mrg return parms;
560 1.6 mrg }
561 1.1 mrg
562 1.6 mrg /* Build the parameter mapping for EXPR using ARGS. */
563 1.1 mrg
564 1.6 mrg static tree
565 1.6 mrg build_parameter_mapping (tree expr, tree args, tree decl)
566 1.6 mrg {
567 1.6 mrg tree ctx_parms = NULL_TREE;
568 1.6 mrg if (decl)
569 1.6 mrg {
570 1.6 mrg gcc_assert (TREE_CODE (decl) == TEMPLATE_DECL);
571 1.6 mrg ctx_parms = DECL_TEMPLATE_PARMS (decl);
572 1.6 mrg }
573 1.6 mrg else if (current_template_parms)
574 1.6 mrg {
575 1.6 mrg /* TODO: This should probably be the only case, but because the
576 1.6 mrg point of declaration of concepts is currently set after the
577 1.6 mrg initializer, the template parameter lists are not available
578 1.6 mrg when normalizing concept definitions, hence the case above. */
579 1.6 mrg ctx_parms = current_template_parms;
580 1.6 mrg }
581 1.1 mrg
582 1.6 mrg tree parms = find_template_parameters (expr, ctx_parms);
583 1.6 mrg tree map = map_arguments (parms, args);
584 1.6 mrg return map;
585 1.6 mrg }
586 1.1 mrg
587 1.6 mrg /* True if the parameter mappings of two atomic constraints are equivalent. */
588 1.1 mrg
589 1.6 mrg static bool
590 1.6 mrg parameter_mapping_equivalent_p (tree t1, tree t2)
591 1.1 mrg {
592 1.6 mrg tree map1 = ATOMIC_CONSTR_MAP (t1);
593 1.6 mrg tree map2 = ATOMIC_CONSTR_MAP (t2);
594 1.6 mrg while (map1 && map2)
595 1.6 mrg {
596 1.6 mrg tree arg1 = TREE_PURPOSE (map1);
597 1.6 mrg tree arg2 = TREE_PURPOSE (map2);
598 1.6 mrg if (!template_args_equal (arg1, arg2))
599 1.6 mrg return false;
600 1.6 mrg map1 = TREE_CHAIN (map1);
601 1.6 mrg map2 = TREE_CHAIN (map2);
602 1.6 mrg }
603 1.6 mrg return true;
604 1.1 mrg }
605 1.1 mrg
606 1.6 mrg /* Provides additional context for normalization. */
607 1.1 mrg
608 1.6 mrg struct norm_info : subst_info
609 1.1 mrg {
610 1.6 mrg explicit norm_info (tsubst_flags_t complain)
611 1.6 mrg : subst_info (tf_warning_or_error | complain, NULL_TREE),
612 1.6 mrg context()
613 1.6 mrg {}
614 1.1 mrg
615 1.6 mrg /* Construct a top-level context for DECL. */
616 1.1 mrg
617 1.6 mrg norm_info (tree in_decl, tsubst_flags_t complain)
618 1.6 mrg : subst_info (tf_warning_or_error | complain, in_decl),
619 1.6 mrg context (make_context (in_decl))
620 1.6 mrg {}
621 1.1 mrg
622 1.6 mrg bool generate_diagnostics() const
623 1.6 mrg {
624 1.6 mrg return complain & tf_norm;
625 1.6 mrg }
626 1.1 mrg
627 1.6 mrg tree make_context(tree in_decl)
628 1.6 mrg {
629 1.6 mrg if (generate_diagnostics ())
630 1.6 mrg return build_tree_list (NULL_TREE, in_decl);
631 1.6 mrg return NULL_TREE;
632 1.6 mrg }
633 1.1 mrg
634 1.6 mrg void update_context(tree expr, tree args)
635 1.6 mrg {
636 1.6 mrg if (generate_diagnostics ())
637 1.6 mrg {
638 1.6 mrg tree map = build_parameter_mapping (expr, args, in_decl);
639 1.6 mrg context = tree_cons (map, expr, context);
640 1.6 mrg }
641 1.6 mrg in_decl = get_concept_check_template (expr);
642 1.6 mrg }
643 1.1 mrg
644 1.6 mrg /* Provides information about the source of a constraint. This is a
645 1.6 mrg TREE_LIST whose VALUE is either a concept check or a constrained
646 1.6 mrg declaration. The PURPOSE, for concept checks is a parameter mapping
647 1.6 mrg for that check. */
648 1.1 mrg
649 1.6 mrg tree context;
650 1.6 mrg };
651 1.1 mrg
652 1.6 mrg static tree normalize_expression (tree, tree, norm_info);
653 1.1 mrg
654 1.1 mrg /* Transform a logical-or or logical-and expression into either
655 1.1 mrg a conjunction or disjunction. */
656 1.1 mrg
657 1.6 mrg static tree
658 1.6 mrg normalize_logical_operation (tree t, tree args, tree_code c, norm_info info)
659 1.1 mrg {
660 1.6 mrg tree t0 = normalize_expression (TREE_OPERAND (t, 0), args, info);
661 1.6 mrg tree t1 = normalize_expression (TREE_OPERAND (t, 1), args, info);
662 1.1 mrg
663 1.6 mrg /* Build a new info object for the constraint. */
664 1.6 mrg tree ci = info.generate_diagnostics()
665 1.6 mrg ? build_tree_list (t, info.context)
666 1.6 mrg : NULL_TREE;
667 1.1 mrg
668 1.6 mrg return build2 (c, ci, t0, t1);
669 1.1 mrg }
670 1.1 mrg
671 1.6 mrg static tree
672 1.6 mrg normalize_concept_check (tree check, tree args, norm_info info)
673 1.1 mrg {
674 1.6 mrg tree id = unpack_concept_check (check);
675 1.6 mrg tree tmpl = TREE_OPERAND (id, 0);
676 1.6 mrg tree targs = TREE_OPERAND (id, 1);
677 1.1 mrg
678 1.6 mrg /* A function concept is wrapped in an overload. */
679 1.6 mrg if (TREE_CODE (tmpl) == OVERLOAD)
680 1.1 mrg {
681 1.6 mrg /* TODO: Can we diagnose this error during parsing? */
682 1.6 mrg if (TREE_CODE (check) == TEMPLATE_ID_EXPR)
683 1.6 mrg error_at (EXPR_LOC_OR_LOC (check, input_location),
684 1.6 mrg "function concept must be called");
685 1.6 mrg tmpl = OVL_FIRST (tmpl);
686 1.1 mrg }
687 1.1 mrg
688 1.6 mrg /* Substitute through the arguments of the concept check. */
689 1.6 mrg if (args)
690 1.6 mrg targs = tsubst_template_args (targs, args, info.complain, info.in_decl);
691 1.6 mrg if (targs == error_mark_node)
692 1.6 mrg return error_mark_node;
693 1.1 mrg
694 1.6 mrg /* Build the substitution for the concept definition. */
695 1.6 mrg tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
696 1.6 mrg /* Turn on template processing; coercing non-type template arguments
697 1.6 mrg will automatically assume they're non-dependent. */
698 1.6 mrg ++processing_template_decl;
699 1.6 mrg tree subst = coerce_template_parms (parms, targs, tmpl);
700 1.6 mrg --processing_template_decl;
701 1.6 mrg if (subst == error_mark_node)
702 1.6 mrg return error_mark_node;
703 1.1 mrg
704 1.6 mrg /* The concept may have been ill-formed. */
705 1.6 mrg tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
706 1.6 mrg if (def == error_mark_node)
707 1.6 mrg return error_mark_node;
708 1.1 mrg
709 1.6 mrg info.update_context (check, args);
710 1.6 mrg return normalize_expression (def, subst, info);
711 1.1 mrg }
712 1.1 mrg
713 1.6 mrg /* The normal form of an atom depends on the expression. The normal
714 1.6 mrg form of a function call to a function concept is a check constraint
715 1.6 mrg for that concept. The normal form of a reference to a variable
716 1.6 mrg concept is a check constraint for that concept. Otherwise, the
717 1.6 mrg constraint is a predicate constraint. */
718 1.1 mrg
719 1.6 mrg static tree
720 1.6 mrg normalize_atom (tree t, tree args, norm_info info)
721 1.1 mrg {
722 1.6 mrg /* Concept checks are not atomic. */
723 1.6 mrg if (concept_check_p (t))
724 1.6 mrg return normalize_concept_check (t, args, info);
725 1.1 mrg
726 1.6 mrg /* Build the parameter mapping for the atom. */
727 1.6 mrg tree map = build_parameter_mapping (t, args, info.in_decl);
728 1.1 mrg
729 1.6 mrg /* Build a new info object for the atom. */
730 1.6 mrg tree ci = build_tree_list (t, info.context);
731 1.1 mrg
732 1.6 mrg return build1 (ATOMIC_CONSTR, ci, map);
733 1.1 mrg }
734 1.1 mrg
735 1.6 mrg /* Returns the normal form of an expression. */
736 1.1 mrg
737 1.6 mrg static tree
738 1.6 mrg normalize_expression (tree t, tree args, norm_info info)
739 1.1 mrg {
740 1.6 mrg if (!t)
741 1.6 mrg return NULL_TREE;
742 1.6 mrg
743 1.6 mrg if (t == error_mark_node)
744 1.6 mrg return error_mark_node;
745 1.6 mrg
746 1.6 mrg switch (TREE_CODE (t))
747 1.1 mrg {
748 1.6 mrg case TRUTH_ANDIF_EXPR:
749 1.6 mrg return normalize_logical_operation (t, args, CONJ_CONSTR, info);
750 1.6 mrg case TRUTH_ORIF_EXPR:
751 1.6 mrg return normalize_logical_operation (t, args, DISJ_CONSTR, info);
752 1.6 mrg default:
753 1.6 mrg return normalize_atom (t, args, info);
754 1.1 mrg }
755 1.1 mrg }
756 1.1 mrg
757 1.6 mrg /* Cache of the normalized form of constraints. Marked as deletable because it
758 1.6 mrg can all be recalculated. */
759 1.6 mrg static GTY((deletable)) hash_map<tree,tree> *normalized_map;
760 1.1 mrg
761 1.6 mrg static tree
762 1.6 mrg get_normalized_constraints (tree t, tree args, norm_info info)
763 1.1 mrg {
764 1.6 mrg auto_timevar time (TV_CONSTRAINT_NORM);
765 1.6 mrg return normalize_expression (t, args, info);
766 1.1 mrg }
767 1.1 mrg
768 1.6 mrg /* Returns the normalized constraints from a constraint-info object
769 1.6 mrg or NULL_TREE if the constraints are null. ARGS provide the initial
770 1.6 mrg arguments for normalization and IN_DECL provides the declaration
771 1.6 mrg to which the constraints belong. */
772 1.1 mrg
773 1.6 mrg static tree
774 1.6 mrg get_normalized_constraints_from_info (tree ci, tree args, tree in_decl,
775 1.6 mrg bool diag = false)
776 1.1 mrg {
777 1.6 mrg if (ci == NULL_TREE)
778 1.6 mrg return NULL_TREE;
779 1.1 mrg
780 1.6 mrg /* Substitution errors during normalization are fatal. */
781 1.6 mrg ++processing_template_decl;
782 1.6 mrg norm_info info (in_decl, diag ? tf_norm : tf_none);
783 1.6 mrg tree t = get_normalized_constraints (CI_ASSOCIATED_CONSTRAINTS (ci),
784 1.6 mrg args, info);
785 1.6 mrg --processing_template_decl;
786 1.1 mrg
787 1.6 mrg return t;
788 1.1 mrg }
789 1.1 mrg
790 1.6 mrg /* Returns the normalized constraints for the declaration D. */
791 1.1 mrg
792 1.6 mrg static tree
793 1.6 mrg get_normalized_constraints_from_decl (tree d, bool diag = false)
794 1.1 mrg {
795 1.6 mrg tree tmpl;
796 1.6 mrg tree decl;
797 1.1 mrg
798 1.6 mrg /* For inherited constructors, consider the original declaration;
799 1.6 mrg it has the correct template information attached. */
800 1.6 mrg d = strip_inheriting_ctors (d);
801 1.1 mrg
802 1.6 mrg if (TREE_CODE (d) == TEMPLATE_DECL)
803 1.1 mrg {
804 1.6 mrg tmpl = d;
805 1.6 mrg decl = DECL_TEMPLATE_RESULT (tmpl);
806 1.1 mrg }
807 1.6 mrg else
808 1.1 mrg {
809 1.6 mrg if (tree ti = DECL_TEMPLATE_INFO (d))
810 1.6 mrg tmpl = TI_TEMPLATE (ti);
811 1.6 mrg else
812 1.6 mrg tmpl = NULL_TREE;
813 1.6 mrg decl = d;
814 1.1 mrg }
815 1.1 mrg
816 1.6 mrg /* Get the most general template for the declaration, and compute
817 1.6 mrg arguments from that. This ensures that the arguments used for
818 1.6 mrg normalization are always template parameters and not arguments
819 1.6 mrg used for outer specializations. For example:
820 1.6 mrg
821 1.6 mrg template<typename T>
822 1.6 mrg struct S {
823 1.6 mrg template<typename U> requires C<T, U> void f(U);
824 1.6 mrg };
825 1.6 mrg
826 1.6 mrg S<int>::f(0);
827 1.6 mrg
828 1.6 mrg When we normalize the requirements for S<int>::f, we want the
829 1.6 mrg arguments to be {T, U}, not {int, U}. One reason for this is that
830 1.6 mrg accepting the latter causes the template parameter level of U
831 1.6 mrg to be reduced in a way that makes it overly difficult substitute
832 1.6 mrg concrete arguments (i.e., eventually {int, int} during satisfaction. */
833 1.6 mrg if (tmpl)
834 1.6 mrg {
835 1.6 mrg if (DECL_LANG_SPECIFIC(tmpl) && !DECL_TEMPLATE_SPECIALIZATION (tmpl))
836 1.6 mrg tmpl = most_general_template (tmpl);
837 1.6 mrg }
838 1.1 mrg
839 1.6 mrg /* If we're not diagnosing errors, use cached constraints, if any. */
840 1.6 mrg if (!diag)
841 1.6 mrg if (tree *p = hash_map_safe_get (normalized_map, tmpl))
842 1.6 mrg return *p;
843 1.1 mrg
844 1.6 mrg push_nested_class_guard pncs (DECL_CONTEXT (d));
845 1.1 mrg
846 1.6 mrg tree args = generic_targs_for (tmpl);
847 1.6 mrg tree ci = get_constraints (decl);
848 1.6 mrg tree norm = get_normalized_constraints_from_info (ci, args, tmpl, diag);
849 1.1 mrg
850 1.6 mrg if (!diag)
851 1.6 mrg hash_map_safe_put<hm_ggc> (normalized_map, tmpl, norm);
852 1.1 mrg
853 1.6 mrg return norm;
854 1.1 mrg }
855 1.1 mrg
856 1.6 mrg /* Returns the normal form of TMPL's definition. */
857 1.1 mrg
858 1.6 mrg static tree
859 1.6 mrg normalize_concept_definition (tree tmpl, bool diag = false)
860 1.1 mrg {
861 1.6 mrg if (!diag)
862 1.6 mrg if (tree *p = hash_map_safe_get (normalized_map, tmpl))
863 1.6 mrg return *p;
864 1.6 mrg
865 1.6 mrg gcc_assert (concept_definition_p (tmpl));
866 1.6 mrg if (OVL_P (tmpl))
867 1.6 mrg tmpl = OVL_FIRST (tmpl);
868 1.6 mrg gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
869 1.6 mrg tree args = generic_targs_for (tmpl);
870 1.6 mrg tree def = get_concept_definition (DECL_TEMPLATE_RESULT (tmpl));
871 1.6 mrg ++processing_template_decl;
872 1.6 mrg norm_info info (tmpl, diag ? tf_norm : tf_none);
873 1.6 mrg tree norm = get_normalized_constraints (def, args, info);
874 1.6 mrg --processing_template_decl;
875 1.6 mrg
876 1.6 mrg if (!diag)
877 1.6 mrg hash_map_safe_put<hm_ggc> (normalized_map, tmpl, norm);
878 1.6 mrg
879 1.6 mrg return norm;
880 1.1 mrg }
881 1.1 mrg
882 1.6 mrg /* Returns the normal form of TMPL's requirements. */
883 1.1 mrg
884 1.6 mrg static tree
885 1.6 mrg normalize_template_requirements (tree tmpl, bool diag = false)
886 1.1 mrg {
887 1.6 mrg return get_normalized_constraints_from_decl (tmpl, diag);
888 1.1 mrg }
889 1.1 mrg
890 1.6 mrg /* Returns the normal form of TMPL's requirements. */
891 1.1 mrg
892 1.6 mrg static tree
893 1.6 mrg normalize_nontemplate_requirements (tree decl, bool diag = false)
894 1.1 mrg {
895 1.6 mrg return get_normalized_constraints_from_decl (decl, diag);
896 1.6 mrg }
897 1.1 mrg
898 1.6 mrg /* Normalize an EXPR as a constraint using ARGS. */
899 1.1 mrg
900 1.6 mrg static tree
901 1.6 mrg normalize_constraint_expression (tree expr, tree args, bool diag = false)
902 1.1 mrg {
903 1.6 mrg if (!expr || expr == error_mark_node)
904 1.6 mrg return expr;
905 1.6 mrg ++processing_template_decl;
906 1.6 mrg norm_info info (diag ? tf_norm : tf_none);
907 1.6 mrg tree norm = get_normalized_constraints (expr, args, info);
908 1.6 mrg --processing_template_decl;
909 1.6 mrg return norm;
910 1.1 mrg }
911 1.1 mrg
912 1.6 mrg /* Normalize an EXPR as a constraint. */
913 1.1 mrg
914 1.6 mrg static tree
915 1.6 mrg normalize_constraint_expression (tree expr, bool diag = false)
916 1.1 mrg {
917 1.6 mrg if (!expr || expr == error_mark_node)
918 1.6 mrg return expr;
919 1.6 mrg
920 1.6 mrg /* For concept checks, use the supplied template arguments as those used
921 1.6 mrg for normalization. Otherwise, there are no template arguments. */
922 1.6 mrg tree args;
923 1.6 mrg if (concept_check_p (expr))
924 1.1 mrg {
925 1.6 mrg tree id = unpack_concept_check (expr);
926 1.6 mrg args = TREE_OPERAND (id, 1);
927 1.1 mrg }
928 1.6 mrg else
929 1.6 mrg args = NULL_TREE;
930 1.6 mrg
931 1.6 mrg return normalize_constraint_expression (expr, args, diag);
932 1.1 mrg }
933 1.1 mrg
934 1.6 mrg /* 17.4.1.2p2. Two constraints are identical if they are formed
935 1.6 mrg from the same expression and the targets of the parameter mapping
936 1.6 mrg are equivalent. */
937 1.1 mrg
938 1.6 mrg bool
939 1.6 mrg atomic_constraints_identical_p (tree t1, tree t2)
940 1.1 mrg {
941 1.6 mrg gcc_assert (TREE_CODE (t1) == ATOMIC_CONSTR);
942 1.6 mrg gcc_assert (TREE_CODE (t2) == ATOMIC_CONSTR);
943 1.1 mrg
944 1.6 mrg if (ATOMIC_CONSTR_EXPR (t1) != ATOMIC_CONSTR_EXPR (t2))
945 1.6 mrg return false;
946 1.1 mrg
947 1.6 mrg if (!parameter_mapping_equivalent_p (t1, t2))
948 1.6 mrg return false;
949 1.1 mrg
950 1.6 mrg return true;
951 1.1 mrg }
952 1.1 mrg
953 1.6 mrg /* True if T1 and T2 are equivalent, meaning they have the same syntactic
954 1.6 mrg structure and all corresponding constraints are identical. */
955 1.1 mrg
956 1.6 mrg bool
957 1.6 mrg constraints_equivalent_p (tree t1, tree t2)
958 1.6 mrg {
959 1.6 mrg gcc_assert (CONSTR_P (t1));
960 1.6 mrg gcc_assert (CONSTR_P (t2));
961 1.1 mrg
962 1.6 mrg if (TREE_CODE (t1) != TREE_CODE (t2))
963 1.6 mrg return false;
964 1.1 mrg
965 1.6 mrg switch (TREE_CODE (t1))
966 1.6 mrg {
967 1.6 mrg case CONJ_CONSTR:
968 1.6 mrg case DISJ_CONSTR:
969 1.6 mrg if (!constraints_equivalent_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
970 1.6 mrg return false;
971 1.6 mrg if (!constraints_equivalent_p (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
972 1.6 mrg return false;
973 1.6 mrg break;
974 1.6 mrg case ATOMIC_CONSTR:
975 1.6 mrg if (!atomic_constraints_identical_p(t1, t2))
976 1.6 mrg return false;
977 1.6 mrg break;
978 1.6 mrg default:
979 1.6 mrg gcc_unreachable ();
980 1.6 mrg }
981 1.6 mrg return true;
982 1.1 mrg }
983 1.1 mrg
984 1.6 mrg /* Compute the hash value for T. */
985 1.1 mrg
986 1.6 mrg hashval_t
987 1.6 mrg hash_atomic_constraint (tree t)
988 1.1 mrg {
989 1.6 mrg gcc_assert (TREE_CODE (t) == ATOMIC_CONSTR);
990 1.6 mrg
991 1.6 mrg /* Hash the identity of the expression. */
992 1.6 mrg hashval_t val = htab_hash_pointer (ATOMIC_CONSTR_EXPR (t));
993 1.1 mrg
994 1.6 mrg /* Hash the targets of the parameter map. */
995 1.6 mrg tree p = ATOMIC_CONSTR_MAP (t);
996 1.6 mrg while (p)
997 1.6 mrg {
998 1.6 mrg val = iterative_hash_template_arg (TREE_PURPOSE (p), val);
999 1.6 mrg p = TREE_CHAIN (p);
1000 1.6 mrg }
1001 1.1 mrg
1002 1.6 mrg return val;
1003 1.1 mrg }
1004 1.1 mrg
1005 1.6 mrg namespace inchash
1006 1.1 mrg {
1007 1.1 mrg
1008 1.6 mrg static void
1009 1.6 mrg add_constraint (tree t, hash& h)
1010 1.1 mrg {
1011 1.6 mrg h.add_int(TREE_CODE (t));
1012 1.1 mrg switch (TREE_CODE (t))
1013 1.6 mrg {
1014 1.6 mrg case CONJ_CONSTR:
1015 1.6 mrg case DISJ_CONSTR:
1016 1.6 mrg add_constraint (TREE_OPERAND (t, 0), h);
1017 1.6 mrg add_constraint (TREE_OPERAND (t, 1), h);
1018 1.6 mrg break;
1019 1.6 mrg case ATOMIC_CONSTR:
1020 1.6 mrg h.merge_hash (hash_atomic_constraint (t));
1021 1.6 mrg break;
1022 1.6 mrg default:
1023 1.6 mrg gcc_unreachable ();
1024 1.6 mrg }
1025 1.6 mrg }
1026 1.1 mrg
1027 1.6 mrg }
1028 1.1 mrg
1029 1.6 mrg /* Computes a hash code for the constraint T. */
1030 1.1 mrg
1031 1.6 mrg hashval_t
1032 1.6 mrg iterative_hash_constraint (tree t, hashval_t val)
1033 1.6 mrg {
1034 1.6 mrg gcc_assert (CONSTR_P (t));
1035 1.6 mrg inchash::hash h (val);
1036 1.6 mrg inchash::add_constraint (t, h);
1037 1.6 mrg return h.end ();
1038 1.1 mrg }
1039 1.1 mrg
1040 1.1 mrg // -------------------------------------------------------------------------- //
1041 1.1 mrg // Constraint Semantic Processing
1042 1.1 mrg //
1043 1.1 mrg // The following functions are called by the parser and substitution rules
1044 1.1 mrg // to create and evaluate constraint-related nodes.
1045 1.1 mrg
1046 1.1 mrg // The constraints associated with the current template parameters.
1047 1.1 mrg tree
1048 1.1 mrg current_template_constraints (void)
1049 1.1 mrg {
1050 1.1 mrg if (!current_template_parms)
1051 1.1 mrg return NULL_TREE;
1052 1.6 mrg tree tmpl_constr = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
1053 1.1 mrg return build_constraints (tmpl_constr, NULL_TREE);
1054 1.1 mrg }
1055 1.1 mrg
1056 1.6 mrg /* If the recently parsed TYPE declares or defines a template or
1057 1.6 mrg template specialization, get its corresponding constraints from the
1058 1.6 mrg current template parameters and bind them to TYPE's declaration. */
1059 1.6 mrg
1060 1.1 mrg tree
1061 1.1 mrg associate_classtype_constraints (tree type)
1062 1.1 mrg {
1063 1.6 mrg if (!type || type == error_mark_node || !CLASS_TYPE_P (type))
1064 1.1 mrg return type;
1065 1.1 mrg
1066 1.6 mrg /* An explicit class template specialization has no template parameters. */
1067 1.1 mrg if (!current_template_parms)
1068 1.1 mrg return type;
1069 1.1 mrg
1070 1.1 mrg if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1071 1.1 mrg {
1072 1.1 mrg tree decl = TYPE_STUB_DECL (type);
1073 1.1 mrg tree ci = current_template_constraints ();
1074 1.1 mrg
1075 1.6 mrg /* An implicitly instantiated member template declaration already
1076 1.6 mrg has associated constraints. If it is defined outside of its
1077 1.6 mrg class, then we need match these constraints against those of
1078 1.6 mrg original declaration. */
1079 1.1 mrg if (tree orig_ci = get_constraints (decl))
1080 1.1 mrg {
1081 1.6 mrg if (int extra_levels = (TMPL_PARMS_DEPTH (current_template_parms)
1082 1.6 mrg - TMPL_ARGS_DEPTH (TYPE_TI_ARGS (type))))
1083 1.6 mrg {
1084 1.6 mrg /* If there is a discrepancy between the current template depth
1085 1.6 mrg and the template depth of the original declaration, then we
1086 1.6 mrg must be redeclaring a class template as part of a friend
1087 1.6 mrg declaration within another class template. Before matching
1088 1.6 mrg constraints, we need to reduce the template parameter level
1089 1.6 mrg within the current constraints via substitution. */
1090 1.6 mrg tree outer_gtargs = template_parms_to_args (current_template_parms);
1091 1.6 mrg TREE_VEC_LENGTH (outer_gtargs) = extra_levels;
1092 1.6 mrg ci = tsubst_constraint_info (ci, outer_gtargs, tf_none, NULL_TREE);
1093 1.6 mrg }
1094 1.1 mrg if (!equivalent_constraints (ci, orig_ci))
1095 1.1 mrg {
1096 1.6 mrg error ("%qT does not match original declaration", type);
1097 1.6 mrg tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1098 1.6 mrg location_t loc = DECL_SOURCE_LOCATION (tmpl);
1099 1.6 mrg inform (loc, "original template declaration here");
1100 1.6 mrg /* Fall through, so that we define the type anyway. */
1101 1.1 mrg }
1102 1.1 mrg return type;
1103 1.1 mrg }
1104 1.1 mrg set_constraints (decl, ci);
1105 1.1 mrg }
1106 1.1 mrg return type;
1107 1.1 mrg }
1108 1.1 mrg
1109 1.6 mrg /* Create an empty constraint info block. */
1110 1.1 mrg
1111 1.6 mrg static inline tree_constraint_info*
1112 1.1 mrg build_constraint_info ()
1113 1.1 mrg {
1114 1.1 mrg return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
1115 1.1 mrg }
1116 1.1 mrg
1117 1.1 mrg /* Build a constraint-info object that contains the associated constraints
1118 1.1 mrg of a declaration. This also includes the declaration's template
1119 1.1 mrg requirements (TREQS) and any trailing requirements for a function
1120 1.1 mrg declarator (DREQS). Note that both TREQS and DREQS must be constraints.
1121 1.1 mrg
1122 1.1 mrg If the declaration has neither template nor declaration requirements
1123 1.1 mrg this returns NULL_TREE, indicating an unconstrained declaration. */
1124 1.1 mrg
1125 1.1 mrg tree
1126 1.6 mrg build_constraints (tree tr, tree dr)
1127 1.1 mrg {
1128 1.6 mrg if (!tr && !dr)
1129 1.1 mrg return NULL_TREE;
1130 1.1 mrg
1131 1.1 mrg tree_constraint_info* ci = build_constraint_info ();
1132 1.6 mrg ci->template_reqs = tr;
1133 1.6 mrg ci->declarator_reqs = dr;
1134 1.6 mrg ci->associated_constr = combine_constraint_expressions (tr, dr);
1135 1.1 mrg
1136 1.1 mrg return (tree)ci;
1137 1.1 mrg }
1138 1.1 mrg
1139 1.6 mrg /* Add constraint RHS to the end of CONSTRAINT_INFO ci. */
1140 1.1 mrg
1141 1.1 mrg tree
1142 1.6 mrg append_constraint (tree ci, tree rhs)
1143 1.1 mrg {
1144 1.6 mrg tree tr = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
1145 1.6 mrg tree dr = ci ? CI_DECLARATOR_REQS (ci) : NULL_TREE;
1146 1.6 mrg dr = combine_constraint_expressions (dr, rhs);
1147 1.6 mrg if (ci)
1148 1.1 mrg {
1149 1.6 mrg CI_DECLARATOR_REQS (ci) = dr;
1150 1.6 mrg tree ac = combine_constraint_expressions (tr, dr);
1151 1.6 mrg CI_ASSOCIATED_CONSTRAINTS (ci) = ac;
1152 1.1 mrg }
1153 1.1 mrg else
1154 1.6 mrg ci = build_constraints (tr, dr);
1155 1.6 mrg return ci;
1156 1.1 mrg }
1157 1.1 mrg
1158 1.6 mrg /* A mapping from declarations to constraint information. */
1159 1.1 mrg
1160 1.6 mrg static GTY ((cache)) decl_tree_cache_map *decl_constraints;
1161 1.1 mrg
1162 1.6 mrg /* Returns the template constraints of declaration T. If T is not
1163 1.6 mrg constrained, return NULL_TREE. Note that T must be non-null. */
1164 1.1 mrg
1165 1.1 mrg tree
1166 1.6 mrg get_constraints (const_tree t)
1167 1.1 mrg {
1168 1.6 mrg if (!flag_concepts)
1169 1.6 mrg return NULL_TREE;
1170 1.6 mrg if (!decl_constraints)
1171 1.6 mrg return NULL_TREE;
1172 1.6 mrg
1173 1.6 mrg gcc_assert (DECL_P (t));
1174 1.6 mrg if (TREE_CODE (t) == TEMPLATE_DECL)
1175 1.6 mrg t = DECL_TEMPLATE_RESULT (t);
1176 1.6 mrg tree* found = decl_constraints->get (CONST_CAST_TREE (t));
1177 1.6 mrg if (found)
1178 1.6 mrg return *found;
1179 1.1 mrg else
1180 1.6 mrg return NULL_TREE;
1181 1.1 mrg }
1182 1.1 mrg
1183 1.6 mrg /* Associate the given constraint information CI with the declaration
1184 1.6 mrg T. If T is a template, then the constraints are associated with
1185 1.6 mrg its underlying declaration. Don't build associations if CI is
1186 1.6 mrg NULL_TREE. */
1187 1.6 mrg
1188 1.6 mrg void
1189 1.6 mrg set_constraints (tree t, tree ci)
1190 1.6 mrg {
1191 1.6 mrg if (!ci)
1192 1.6 mrg return;
1193 1.6 mrg gcc_assert (t && flag_concepts);
1194 1.6 mrg if (TREE_CODE (t) == TEMPLATE_DECL)
1195 1.6 mrg t = DECL_TEMPLATE_RESULT (t);
1196 1.6 mrg bool found = hash_map_safe_put<hm_ggc> (decl_constraints, t, ci);
1197 1.6 mrg gcc_assert (!found);
1198 1.6 mrg }
1199 1.1 mrg
1200 1.6 mrg /* Remove the associated constraints of the declaration T. */
1201 1.1 mrg
1202 1.6 mrg void
1203 1.6 mrg remove_constraints (tree t)
1204 1.1 mrg {
1205 1.6 mrg gcc_assert (DECL_P (t));
1206 1.6 mrg if (TREE_CODE (t) == TEMPLATE_DECL)
1207 1.6 mrg t = DECL_TEMPLATE_RESULT (t);
1208 1.6 mrg
1209 1.6 mrg if (decl_constraints)
1210 1.6 mrg decl_constraints->remove (t);
1211 1.1 mrg }
1212 1.1 mrg
1213 1.6 mrg /* If DECL is a friend, substitute into REQS to produce requirements suitable
1214 1.6 mrg for declaration matching. */
1215 1.1 mrg
1216 1.1 mrg tree
1217 1.6 mrg maybe_substitute_reqs_for (tree reqs, const_tree decl_)
1218 1.1 mrg {
1219 1.6 mrg if (reqs == NULL_TREE)
1220 1.1 mrg return NULL_TREE;
1221 1.6 mrg tree decl = CONST_CAST_TREE (decl_);
1222 1.6 mrg tree result = STRIP_TEMPLATE (decl);
1223 1.6 mrg if (DECL_FRIEND_P (result))
1224 1.6 mrg {
1225 1.6 mrg tree tmpl = decl == result ? DECL_TI_TEMPLATE (result) : decl;
1226 1.6 mrg tree gargs = generic_targs_for (tmpl);
1227 1.6 mrg processing_template_decl_sentinel s;
1228 1.6 mrg if (uses_template_parms (gargs))
1229 1.6 mrg ++processing_template_decl;
1230 1.6 mrg reqs = tsubst_constraint (reqs, gargs,
1231 1.6 mrg tf_warning_or_error, NULL_TREE);
1232 1.6 mrg }
1233 1.6 mrg return reqs;
1234 1.6 mrg }
1235 1.6 mrg
1236 1.6 mrg /* Returns the template-head requires clause for the template
1237 1.6 mrg declaration T or NULL_TREE if none. */
1238 1.1 mrg
1239 1.6 mrg tree
1240 1.6 mrg get_template_head_requirements (tree t)
1241 1.6 mrg {
1242 1.6 mrg tree ci = get_constraints (t);
1243 1.6 mrg if (!ci)
1244 1.5 mrg return NULL_TREE;
1245 1.6 mrg return CI_TEMPLATE_REQS (ci);
1246 1.6 mrg }
1247 1.5 mrg
1248 1.6 mrg /* Returns the trailing requires clause of the declarator of
1249 1.6 mrg a template declaration T or NULL_TREE if none. */
1250 1.1 mrg
1251 1.6 mrg tree
1252 1.6 mrg get_trailing_function_requirements (tree t)
1253 1.6 mrg {
1254 1.6 mrg tree ci = get_constraints (t);
1255 1.6 mrg if (!ci)
1256 1.6 mrg return NULL_TREE;
1257 1.6 mrg return CI_DECLARATOR_REQS (ci);
1258 1.6 mrg }
1259 1.6 mrg
1260 1.6 mrg /* Construct a sequence of template arguments by prepending
1261 1.6 mrg ARG to REST. Either ARG or REST may be null. */
1262 1.6 mrg static tree
1263 1.6 mrg build_concept_check_arguments (tree arg, tree rest)
1264 1.6 mrg {
1265 1.6 mrg gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1266 1.6 mrg tree args;
1267 1.6 mrg if (arg)
1268 1.6 mrg {
1269 1.6 mrg int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1270 1.6 mrg args = make_tree_vec (n + 1);
1271 1.6 mrg TREE_VEC_ELT (args, 0) = arg;
1272 1.6 mrg if (rest)
1273 1.6 mrg for (int i = 0; i < n; ++i)
1274 1.6 mrg TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1275 1.6 mrg int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1276 1.6 mrg SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1277 1.6 mrg }
1278 1.6 mrg else
1279 1.6 mrg {
1280 1.6 mrg args = rest;
1281 1.6 mrg }
1282 1.6 mrg return args;
1283 1.6 mrg }
1284 1.6 mrg
1285 1.6 mrg /* Builds an id-expression of the form `C<Args...>()` where C is a function
1286 1.6 mrg concept. */
1287 1.6 mrg
1288 1.6 mrg static tree
1289 1.6 mrg build_function_check (tree tmpl, tree args, tsubst_flags_t /*complain*/)
1290 1.6 mrg {
1291 1.6 mrg if (TREE_CODE (tmpl) == TEMPLATE_DECL)
1292 1.6 mrg {
1293 1.6 mrg /* If we just got a template, wrap it in an overload so it looks like any
1294 1.6 mrg other template-id. */
1295 1.6 mrg tmpl = ovl_make (tmpl);
1296 1.6 mrg TREE_TYPE (tmpl) = boolean_type_node;
1297 1.6 mrg }
1298 1.6 mrg
1299 1.6 mrg /* Perform function concept resolution now so we always have a single
1300 1.6 mrg function of the overload set (even if we started with only one; the
1301 1.6 mrg resolution function converts template arguments). Note that we still
1302 1.6 mrg wrap this in an overload set so we don't upset other parts of the
1303 1.6 mrg compiler that expect template-ids referring to function concepts
1304 1.6 mrg to have an overload set. */
1305 1.6 mrg tree info = resolve_function_concept_overload (tmpl, args);
1306 1.6 mrg if (info == error_mark_node)
1307 1.6 mrg return error_mark_node;
1308 1.6 mrg if (!info)
1309 1.6 mrg {
1310 1.6 mrg error ("no matching concepts for %qE", tmpl);
1311 1.6 mrg return error_mark_node;
1312 1.6 mrg }
1313 1.6 mrg args = TREE_PURPOSE (info);
1314 1.6 mrg tmpl = DECL_TI_TEMPLATE (TREE_VALUE (info));
1315 1.6 mrg
1316 1.6 mrg /* Rebuild the singleton overload set; mark the type bool. */
1317 1.6 mrg tmpl = ovl_make (tmpl, NULL_TREE);
1318 1.6 mrg TREE_TYPE (tmpl) = boolean_type_node;
1319 1.6 mrg
1320 1.6 mrg /* Build the id-expression around the overload set. */
1321 1.6 mrg tree id = build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args);
1322 1.6 mrg
1323 1.6 mrg /* Finally, build the call expression around the overload. */
1324 1.6 mrg ++processing_template_decl;
1325 1.6 mrg vec<tree, va_gc> *fargs = make_tree_vector ();
1326 1.6 mrg tree call = build_min_nt_call_vec (id, fargs);
1327 1.6 mrg TREE_TYPE (call) = boolean_type_node;
1328 1.6 mrg release_tree_vector (fargs);
1329 1.6 mrg --processing_template_decl;
1330 1.6 mrg
1331 1.6 mrg return call;
1332 1.6 mrg }
1333 1.6 mrg
1334 1.6 mrg /* Builds an id-expression of the form `C<Args...>` where C is a variable
1335 1.6 mrg concept. */
1336 1.6 mrg
1337 1.6 mrg static tree
1338 1.6 mrg build_variable_check (tree tmpl, tree args, tsubst_flags_t complain)
1339 1.6 mrg {
1340 1.6 mrg gcc_assert (variable_concept_p (tmpl));
1341 1.6 mrg gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1342 1.6 mrg tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
1343 1.6 mrg args = coerce_template_parms (parms, args, tmpl, complain);
1344 1.6 mrg if (args == error_mark_node)
1345 1.6 mrg return error_mark_node;
1346 1.6 mrg return build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args);
1347 1.6 mrg }
1348 1.6 mrg
1349 1.6 mrg /* Builds an id-expression of the form `C<Args...>` where C is a standard
1350 1.6 mrg concept. */
1351 1.6 mrg
1352 1.6 mrg static tree
1353 1.6 mrg build_standard_check (tree tmpl, tree args, tsubst_flags_t complain)
1354 1.6 mrg {
1355 1.6 mrg gcc_assert (standard_concept_p (tmpl));
1356 1.6 mrg gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1357 1.6 mrg tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
1358 1.6 mrg args = coerce_template_parms (parms, args, tmpl, complain);
1359 1.6 mrg if (args == error_mark_node)
1360 1.6 mrg return error_mark_node;
1361 1.6 mrg return build2 (TEMPLATE_ID_EXPR, boolean_type_node, tmpl, args);
1362 1.6 mrg }
1363 1.6 mrg
1364 1.6 mrg /* Construct an expression that checks TARGET using ARGS. */
1365 1.6 mrg
1366 1.6 mrg tree
1367 1.6 mrg build_concept_check (tree target, tree args, tsubst_flags_t complain)
1368 1.6 mrg {
1369 1.6 mrg return build_concept_check (target, NULL_TREE, args, complain);
1370 1.6 mrg }
1371 1.6 mrg
1372 1.6 mrg /* Construct an expression that checks the concept given by DECL. If
1373 1.6 mrg concept_definition_p (DECL) is false, this returns null. */
1374 1.6 mrg
1375 1.6 mrg tree
1376 1.6 mrg build_concept_check (tree decl, tree arg, tree rest, tsubst_flags_t complain)
1377 1.6 mrg {
1378 1.6 mrg tree args = build_concept_check_arguments (arg, rest);
1379 1.6 mrg
1380 1.6 mrg if (standard_concept_p (decl))
1381 1.6 mrg return build_standard_check (decl, args, complain);
1382 1.6 mrg if (variable_concept_p (decl))
1383 1.6 mrg return build_variable_check (decl, args, complain);
1384 1.6 mrg if (function_concept_p (decl))
1385 1.6 mrg return build_function_check (decl, args, complain);
1386 1.6 mrg
1387 1.6 mrg return error_mark_node;
1388 1.6 mrg }
1389 1.6 mrg
1390 1.6 mrg /* Build a template-id that can participate in a concept check. */
1391 1.6 mrg
1392 1.6 mrg static tree
1393 1.6 mrg build_concept_id (tree decl, tree args)
1394 1.6 mrg {
1395 1.6 mrg tree check = build_concept_check (decl, args, tf_warning_or_error);
1396 1.6 mrg if (check == error_mark_node)
1397 1.6 mrg return error_mark_node;
1398 1.6 mrg return unpack_concept_check (check);
1399 1.6 mrg }
1400 1.6 mrg
1401 1.6 mrg /* Build a template-id that can participate in a concept check, preserving
1402 1.6 mrg the source location of the original template-id. */
1403 1.6 mrg
1404 1.6 mrg tree
1405 1.6 mrg build_concept_id (tree expr)
1406 1.6 mrg {
1407 1.6 mrg gcc_assert (TREE_CODE (expr) == TEMPLATE_ID_EXPR);
1408 1.6 mrg tree id = build_concept_id (TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
1409 1.6 mrg protected_set_expr_location (id, cp_expr_location (expr));
1410 1.6 mrg return id;
1411 1.6 mrg }
1412 1.6 mrg
1413 1.6 mrg /* Build as template-id with a placeholder that can be used as a
1414 1.6 mrg type constraint.
1415 1.6 mrg
1416 1.6 mrg Note that this will diagnose errors if the initial concept check
1417 1.6 mrg cannot be built. */
1418 1.6 mrg
1419 1.6 mrg tree
1420 1.6 mrg build_type_constraint (tree decl, tree args, tsubst_flags_t complain)
1421 1.6 mrg {
1422 1.6 mrg tree wildcard = build_nt (WILDCARD_DECL);
1423 1.6 mrg ++processing_template_decl;
1424 1.6 mrg tree check = build_concept_check (decl, wildcard, args, complain);
1425 1.6 mrg --processing_template_decl;
1426 1.6 mrg if (check == error_mark_node)
1427 1.6 mrg return error_mark_node;
1428 1.6 mrg return unpack_concept_check (check);
1429 1.6 mrg }
1430 1.6 mrg
1431 1.6 mrg /* Returns a TYPE_DECL that contains sufficient information to
1432 1.6 mrg build a template parameter of the same kind as PROTO and
1433 1.6 mrg constrained by the concept declaration CNC. Note that PROTO
1434 1.6 mrg is the first template parameter of CNC.
1435 1.6 mrg
1436 1.6 mrg If specified, ARGS provides additional arguments to the
1437 1.6 mrg constraint check. */
1438 1.6 mrg tree
1439 1.6 mrg build_constrained_parameter (tree cnc, tree proto, tree args)
1440 1.6 mrg {
1441 1.6 mrg tree name = DECL_NAME (cnc);
1442 1.6 mrg tree type = TREE_TYPE (proto);
1443 1.6 mrg tree decl = build_decl (input_location, TYPE_DECL, name, type);
1444 1.6 mrg CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1445 1.6 mrg CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1446 1.6 mrg CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1447 1.6 mrg return decl;
1448 1.6 mrg }
1449 1.6 mrg
1450 1.6 mrg /* Create a constraint expression for the given DECL that evaluates the
1451 1.6 mrg requirements specified by CONSTR, a TYPE_DECL that contains all the
1452 1.6 mrg information necessary to build the requirements (see finish_concept_name
1453 1.6 mrg for the layout of that TYPE_DECL).
1454 1.6 mrg
1455 1.6 mrg Note that the constraints are neither reduced nor decomposed. That is
1456 1.6 mrg done only after the requires clause has been parsed (or not). */
1457 1.6 mrg
1458 1.6 mrg tree
1459 1.6 mrg finish_shorthand_constraint (tree decl, tree constr)
1460 1.6 mrg {
1461 1.6 mrg /* No requirements means no constraints. */
1462 1.6 mrg if (!constr)
1463 1.6 mrg return NULL_TREE;
1464 1.6 mrg
1465 1.6 mrg if (error_operand_p (constr))
1466 1.6 mrg return NULL_TREE;
1467 1.6 mrg
1468 1.6 mrg tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1469 1.6 mrg tree con = CONSTRAINED_PARM_CONCEPT (constr);
1470 1.6 mrg tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1471 1.6 mrg
1472 1.6 mrg /* The TS lets use shorthand to constrain a pack of arguments, but the
1473 1.6 mrg standard does not.
1474 1.6 mrg
1475 1.6 mrg For the TS, consider:
1476 1.6 mrg
1477 1.6 mrg template<C... Ts> struct s;
1478 1.6 mrg
1479 1.6 mrg If C is variadic (and because Ts is a pack), we associate the
1480 1.6 mrg constraint C<Ts...>. In all other cases, we associate
1481 1.6 mrg the constraint (C<Ts> && ...).
1482 1.6 mrg
1483 1.6 mrg The standard behavior cannot be overridden by -fconcepts-ts. */
1484 1.6 mrg bool variadic_concept_p = template_parameter_pack_p (proto);
1485 1.6 mrg bool declared_pack_p = template_parameter_pack_p (decl);
1486 1.6 mrg bool apply_to_each_p = (cxx_dialect >= cxx2a) ? true : !variadic_concept_p;
1487 1.1 mrg
1488 1.1 mrg /* Get the argument and overload used for the requirement
1489 1.1 mrg and adjust it if we're going to expand later. */
1490 1.6 mrg tree arg = template_parm_to_arg (decl);
1491 1.6 mrg if (apply_to_each_p && declared_pack_p)
1492 1.1 mrg arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1493 1.1 mrg
1494 1.6 mrg /* Build the concept constraint-expression. */
1495 1.1 mrg tree tmpl = DECL_TI_TEMPLATE (con);
1496 1.6 mrg tree check = tmpl;
1497 1.6 mrg if (TREE_CODE (con) == FUNCTION_DECL)
1498 1.6 mrg check = ovl_make (tmpl);
1499 1.6 mrg check = build_concept_check (check, arg, args, tf_warning_or_error);
1500 1.6 mrg
1501 1.6 mrg /* Make the check a fold-expression if needed. */
1502 1.6 mrg if (apply_to_each_p && declared_pack_p)
1503 1.6 mrg check = finish_left_unary_fold_expr (check, TRUTH_ANDIF_EXPR);
1504 1.1 mrg
1505 1.6 mrg return check;
1506 1.1 mrg }
1507 1.1 mrg
1508 1.1 mrg /* Returns a conjunction of shorthand requirements for the template
1509 1.1 mrg parameter list PARMS. Note that the requirements are stored in
1510 1.1 mrg the TYPE of each tree node. */
1511 1.6 mrg
1512 1.1 mrg tree
1513 1.1 mrg get_shorthand_constraints (tree parms)
1514 1.1 mrg {
1515 1.1 mrg tree result = NULL_TREE;
1516 1.1 mrg parms = INNERMOST_TEMPLATE_PARMS (parms);
1517 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1518 1.1 mrg {
1519 1.1 mrg tree parm = TREE_VEC_ELT (parms, i);
1520 1.1 mrg tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1521 1.6 mrg result = combine_constraint_expressions (result, constr);
1522 1.1 mrg }
1523 1.1 mrg return result;
1524 1.1 mrg }
1525 1.1 mrg
1526 1.6 mrg /* Get the deduced wildcard from a DEDUCED placeholder. If the deduced
1527 1.6 mrg wildcard is a pack, return the first argument of that pack. */
1528 1.6 mrg
1529 1.6 mrg static tree
1530 1.6 mrg get_deduced_wildcard (tree wildcard)
1531 1.6 mrg {
1532 1.6 mrg if (ARGUMENT_PACK_P (wildcard))
1533 1.6 mrg wildcard = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (wildcard), 0);
1534 1.6 mrg gcc_assert (TREE_CODE (wildcard) == WILDCARD_DECL);
1535 1.6 mrg return wildcard;
1536 1.6 mrg }
1537 1.6 mrg
1538 1.6 mrg /* Returns the prototype parameter for the nth deduced wildcard. */
1539 1.6 mrg
1540 1.6 mrg static tree
1541 1.6 mrg get_introduction_prototype (tree wildcards, int index)
1542 1.6 mrg {
1543 1.6 mrg return TREE_TYPE (get_deduced_wildcard (TREE_VEC_ELT (wildcards, index)));
1544 1.6 mrg }
1545 1.6 mrg
1546 1.6 mrg /* Introduce a type template parameter. */
1547 1.6 mrg
1548 1.6 mrg static tree
1549 1.6 mrg introduce_type_template_parameter (tree wildcard, bool& non_type_p)
1550 1.6 mrg {
1551 1.6 mrg non_type_p = false;
1552 1.6 mrg return finish_template_type_parm (class_type_node, DECL_NAME (wildcard));
1553 1.6 mrg }
1554 1.6 mrg
1555 1.6 mrg /* Introduce a template template parameter. */
1556 1.6 mrg
1557 1.1 mrg static tree
1558 1.6 mrg introduce_template_template_parameter (tree wildcard, bool& non_type_p)
1559 1.1 mrg {
1560 1.6 mrg non_type_p = false;
1561 1.6 mrg begin_template_parm_list ();
1562 1.6 mrg current_template_parms = DECL_TEMPLATE_PARMS (TREE_TYPE (wildcard));
1563 1.6 mrg end_template_parm_list ();
1564 1.6 mrg return finish_template_template_parm (class_type_node, DECL_NAME (wildcard));
1565 1.6 mrg }
1566 1.1 mrg
1567 1.6 mrg /* Introduce a template non-type parameter. */
1568 1.1 mrg
1569 1.6 mrg static tree
1570 1.6 mrg introduce_nontype_template_parameter (tree wildcard, bool& non_type_p)
1571 1.6 mrg {
1572 1.6 mrg non_type_p = true;
1573 1.6 mrg tree parm = copy_decl (TREE_TYPE (wildcard));
1574 1.6 mrg DECL_NAME (parm) = DECL_NAME (wildcard);
1575 1.6 mrg return parm;
1576 1.6 mrg }
1577 1.1 mrg
1578 1.6 mrg /* Introduce a single template parameter. */
1579 1.1 mrg
1580 1.6 mrg static tree
1581 1.6 mrg build_introduced_template_parameter (tree wildcard, bool& non_type_p)
1582 1.6 mrg {
1583 1.6 mrg tree proto = TREE_TYPE (wildcard);
1584 1.1 mrg
1585 1.1 mrg tree parm;
1586 1.6 mrg if (TREE_CODE (proto) == TYPE_DECL)
1587 1.6 mrg parm = introduce_type_template_parameter (wildcard, non_type_p);
1588 1.6 mrg else if (TREE_CODE (proto) == TEMPLATE_DECL)
1589 1.6 mrg parm = introduce_template_template_parameter (wildcard, non_type_p);
1590 1.6 mrg else
1591 1.6 mrg parm = introduce_nontype_template_parameter (wildcard, non_type_p);
1592 1.6 mrg
1593 1.6 mrg /* Wrap in a TREE_LIST for process_template_parm. Note that introduced
1594 1.6 mrg parameters do not retain the defaults from the source parameter. */
1595 1.6 mrg return build_tree_list (NULL_TREE, parm);
1596 1.6 mrg }
1597 1.6 mrg
1598 1.6 mrg /* Introduce a single template parameter. */
1599 1.6 mrg
1600 1.6 mrg static tree
1601 1.6 mrg introduce_template_parameter (tree parms, tree wildcard)
1602 1.6 mrg {
1603 1.6 mrg gcc_assert (!ARGUMENT_PACK_P (wildcard));
1604 1.6 mrg tree proto = TREE_TYPE (wildcard);
1605 1.6 mrg location_t loc = DECL_SOURCE_LOCATION (wildcard);
1606 1.6 mrg
1607 1.6 mrg /* Diagnose the case where we have C{...Args}. */
1608 1.6 mrg if (WILDCARD_PACK_P (wildcard))
1609 1.1 mrg {
1610 1.6 mrg tree id = DECL_NAME (wildcard);
1611 1.6 mrg error_at (loc, "%qE cannot be introduced with an ellipsis %<...%>", id);
1612 1.6 mrg inform (DECL_SOURCE_LOCATION (proto), "prototype declared here");
1613 1.1 mrg }
1614 1.6 mrg
1615 1.6 mrg bool non_type_p;
1616 1.6 mrg tree parm = build_introduced_template_parameter (wildcard, non_type_p);
1617 1.6 mrg return process_template_parm (parms, loc, parm, non_type_p, false);
1618 1.6 mrg }
1619 1.6 mrg
1620 1.6 mrg /* Introduce a template parameter pack. */
1621 1.6 mrg
1622 1.6 mrg static tree
1623 1.6 mrg introduce_template_parameter_pack (tree parms, tree wildcard)
1624 1.6 mrg {
1625 1.6 mrg bool non_type_p;
1626 1.6 mrg tree parm = build_introduced_template_parameter (wildcard, non_type_p);
1627 1.6 mrg location_t loc = DECL_SOURCE_LOCATION (wildcard);
1628 1.6 mrg return process_template_parm (parms, loc, parm, non_type_p, true);
1629 1.6 mrg }
1630 1.6 mrg
1631 1.6 mrg /* Introduce the nth template parameter. */
1632 1.6 mrg
1633 1.6 mrg static tree
1634 1.6 mrg introduce_template_parameter (tree parms, tree wildcards, int& index)
1635 1.6 mrg {
1636 1.6 mrg tree deduced = TREE_VEC_ELT (wildcards, index++);
1637 1.6 mrg return introduce_template_parameter (parms, deduced);
1638 1.6 mrg }
1639 1.6 mrg
1640 1.6 mrg /* Introduce either a template parameter pack or a list of template
1641 1.6 mrg parameters. */
1642 1.6 mrg
1643 1.6 mrg static tree
1644 1.6 mrg introduce_template_parameters (tree parms, tree wildcards, int& index)
1645 1.6 mrg {
1646 1.6 mrg /* If the prototype was a parameter, we better have deduced an
1647 1.6 mrg argument pack, and that argument must be the last deduced value
1648 1.6 mrg in the wildcard vector. */
1649 1.6 mrg tree deduced = TREE_VEC_ELT (wildcards, index++);
1650 1.6 mrg gcc_assert (ARGUMENT_PACK_P (deduced));
1651 1.6 mrg gcc_assert (index == TREE_VEC_LENGTH (wildcards));
1652 1.6 mrg
1653 1.6 mrg /* Introduce each element in the pack. */
1654 1.6 mrg tree args = ARGUMENT_PACK_ARGS (deduced);
1655 1.6 mrg for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1656 1.1 mrg {
1657 1.6 mrg tree arg = TREE_VEC_ELT (args, i);
1658 1.6 mrg if (WILDCARD_PACK_P (arg))
1659 1.6 mrg parms = introduce_template_parameter_pack (parms, arg);
1660 1.6 mrg else
1661 1.6 mrg parms = introduce_template_parameter (parms, arg);
1662 1.1 mrg }
1663 1.6 mrg
1664 1.6 mrg return parms;
1665 1.6 mrg }
1666 1.6 mrg
1667 1.6 mrg /* Builds the template parameter list PARMS by chaining introduced
1668 1.6 mrg parameters from the WILDCARD vector. INDEX is the position of
1669 1.6 mrg the current parameter. */
1670 1.6 mrg
1671 1.6 mrg static tree
1672 1.6 mrg process_introduction_parms (tree parms, tree wildcards, int& index)
1673 1.6 mrg {
1674 1.6 mrg tree proto = get_introduction_prototype (wildcards, index);
1675 1.6 mrg if (template_parameter_pack_p (proto))
1676 1.6 mrg return introduce_template_parameters (parms, wildcards, index);
1677 1.1 mrg else
1678 1.6 mrg return introduce_template_parameter (parms, wildcards, index);
1679 1.6 mrg }
1680 1.6 mrg
1681 1.6 mrg /* Ensure that all template parameters have been introduced for the concept
1682 1.6 mrg named in CHECK. If not, emit a diagnostic.
1683 1.6 mrg
1684 1.6 mrg Note that implicitly introducing a parameter with a default argument
1685 1.6 mrg creates a case where a parameter is declared, but unnamed, making
1686 1.6 mrg it unusable in the definition. */
1687 1.6 mrg
1688 1.6 mrg static bool
1689 1.6 mrg check_introduction_list (tree intros, tree check)
1690 1.6 mrg {
1691 1.6 mrg check = unpack_concept_check (check);
1692 1.6 mrg tree tmpl = TREE_OPERAND (check, 0);
1693 1.6 mrg if (OVL_P (tmpl))
1694 1.6 mrg tmpl = OVL_FIRST (tmpl);
1695 1.6 mrg
1696 1.6 mrg tree parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
1697 1.6 mrg if (TREE_VEC_LENGTH (intros) < TREE_VEC_LENGTH (parms))
1698 1.1 mrg {
1699 1.6 mrg error_at (input_location, "all template parameters of %qD must "
1700 1.6 mrg "be introduced", tmpl);
1701 1.6 mrg return false;
1702 1.1 mrg }
1703 1.1 mrg
1704 1.6 mrg return true;
1705 1.6 mrg }
1706 1.1 mrg
1707 1.6 mrg /* Associates a constraint check to the current template based on the
1708 1.6 mrg introduction parameters. INTRO_LIST must be a TREE_VEC of WILDCARD_DECLs
1709 1.6 mrg containing a chained PARM_DECL which contains the identifier as well as
1710 1.6 mrg the source location. TMPL_DECL is the decl for the concept being used.
1711 1.6 mrg If we take a concept, C, this will form a check in the form of
1712 1.6 mrg C<INTRO_LIST> filling in any extra arguments needed by the defaults
1713 1.6 mrg deduced.
1714 1.1 mrg
1715 1.6 mrg Returns NULL_TREE if no concept could be matched and error_mark_node if
1716 1.6 mrg an error occurred when matching. */
1717 1.1 mrg
1718 1.1 mrg tree
1719 1.6 mrg finish_template_introduction (tree tmpl_decl,
1720 1.6 mrg tree intro_list,
1721 1.6 mrg location_t intro_loc)
1722 1.1 mrg {
1723 1.6 mrg /* Build a concept check to deduce the actual parameters. */
1724 1.6 mrg tree expr = build_concept_check (tmpl_decl, intro_list, tf_none);
1725 1.1 mrg if (expr == error_mark_node)
1726 1.6 mrg {
1727 1.6 mrg error_at (intro_loc, "cannot deduce template parameters from "
1728 1.6 mrg "introduction list");
1729 1.6 mrg return error_mark_node;
1730 1.6 mrg }
1731 1.6 mrg
1732 1.6 mrg if (!check_introduction_list (intro_list, expr))
1733 1.6 mrg return error_mark_node;
1734 1.1 mrg
1735 1.1 mrg tree parms = deduce_concept_introduction (expr);
1736 1.1 mrg if (!parms)
1737 1.1 mrg return NULL_TREE;
1738 1.1 mrg
1739 1.1 mrg /* Build template parameter scope for introduction. */
1740 1.1 mrg tree parm_list = NULL_TREE;
1741 1.1 mrg begin_template_parm_list ();
1742 1.1 mrg int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1743 1.6 mrg for (int n = 0; n < nargs; )
1744 1.6 mrg parm_list = process_introduction_parms (parm_list, parms, n);
1745 1.1 mrg parm_list = end_template_parm_list (parm_list);
1746 1.6 mrg
1747 1.6 mrg /* Update the number of arguments to reflect the number of deduced
1748 1.6 mrg template parameter introductions. */
1749 1.6 mrg nargs = TREE_VEC_LENGTH (parm_list);
1750 1.6 mrg
1751 1.6 mrg /* Determine if any errors occurred during matching. */
1752 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1753 1.1 mrg if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1754 1.1 mrg {
1755 1.1 mrg end_template_decl ();
1756 1.1 mrg return error_mark_node;
1757 1.1 mrg }
1758 1.1 mrg
1759 1.1 mrg /* Build a concept check for our constraint. */
1760 1.6 mrg tree check_args = make_tree_vec (nargs);
1761 1.1 mrg int n = 0;
1762 1.1 mrg for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1763 1.1 mrg {
1764 1.1 mrg tree parm = TREE_VEC_ELT (parm_list, n);
1765 1.1 mrg TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1766 1.1 mrg }
1767 1.1 mrg SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args, n);
1768 1.1 mrg
1769 1.1 mrg /* If the template expects more parameters we should be able
1770 1.1 mrg to use the defaults from our deduced concept. */
1771 1.1 mrg for (; n < TREE_VEC_LENGTH (parms); ++n)
1772 1.1 mrg TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1773 1.1 mrg
1774 1.6 mrg /* Associate the constraint. */
1775 1.6 mrg tree check = build_concept_check (tmpl_decl,
1776 1.6 mrg check_args,
1777 1.6 mrg tf_warning_or_error);
1778 1.6 mrg TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = check;
1779 1.1 mrg
1780 1.1 mrg return parm_list;
1781 1.1 mrg }
1782 1.1 mrg
1783 1.1 mrg
1784 1.6 mrg /* Given the concept check T from a constrained-type-specifier, extract
1785 1.1 mrg its TMPL and ARGS. FIXME why do we need two different forms of
1786 1.1 mrg constrained-type-specifier? */
1787 1.1 mrg
1788 1.1 mrg void
1789 1.1 mrg placeholder_extract_concept_and_args (tree t, tree &tmpl, tree &args)
1790 1.1 mrg {
1791 1.6 mrg if (concept_check_p (t))
1792 1.6 mrg {
1793 1.6 mrg t = unpack_concept_check (t);
1794 1.6 mrg tmpl = TREE_OPERAND (t, 0);
1795 1.6 mrg if (TREE_CODE (tmpl) == OVERLOAD)
1796 1.6 mrg tmpl = OVL_FIRST (tmpl);
1797 1.6 mrg args = TREE_OPERAND (t, 1);
1798 1.6 mrg return;
1799 1.6 mrg }
1800 1.6 mrg
1801 1.1 mrg if (TREE_CODE (t) == TYPE_DECL)
1802 1.1 mrg {
1803 1.1 mrg /* A constrained parameter. Build a constraint check
1804 1.1 mrg based on the prototype parameter and then extract the
1805 1.1 mrg arguments from that. */
1806 1.1 mrg tree proto = CONSTRAINED_PARM_PROTOTYPE (t);
1807 1.1 mrg tree check = finish_shorthand_constraint (proto, t);
1808 1.1 mrg placeholder_extract_concept_and_args (check, tmpl, args);
1809 1.1 mrg return;
1810 1.1 mrg }
1811 1.1 mrg }
1812 1.1 mrg
1813 1.1 mrg /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1814 1.6 mrg and C2 can be either TEMPLATE_TYPE_PARM or template-ids. */
1815 1.1 mrg
1816 1.1 mrg bool
1817 1.1 mrg equivalent_placeholder_constraints (tree c1, tree c2)
1818 1.1 mrg {
1819 1.1 mrg if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1820 1.1 mrg /* A constrained auto. */
1821 1.1 mrg c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1822 1.1 mrg if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1823 1.1 mrg c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1824 1.1 mrg
1825 1.1 mrg if (c1 == c2)
1826 1.1 mrg return true;
1827 1.1 mrg if (!c1 || !c2)
1828 1.1 mrg return false;
1829 1.1 mrg if (c1 == error_mark_node || c2 == error_mark_node)
1830 1.1 mrg /* We get here during satisfaction; when a deduction constraint
1831 1.1 mrg fails, substitution can produce an error_mark_node for the
1832 1.1 mrg placeholder constraints. */
1833 1.1 mrg return false;
1834 1.1 mrg
1835 1.1 mrg tree t1, t2, a1, a2;
1836 1.1 mrg placeholder_extract_concept_and_args (c1, t1, a1);
1837 1.1 mrg placeholder_extract_concept_and_args (c2, t2, a2);
1838 1.1 mrg
1839 1.1 mrg if (t1 != t2)
1840 1.1 mrg return false;
1841 1.1 mrg
1842 1.1 mrg int len1 = TREE_VEC_LENGTH (a1);
1843 1.1 mrg int len2 = TREE_VEC_LENGTH (a2);
1844 1.1 mrg if (len1 != len2)
1845 1.1 mrg return false;
1846 1.1 mrg
1847 1.1 mrg /* Skip the first argument so we don't infinitely recurse.
1848 1.1 mrg Also, they may differ in template parameter index. */
1849 1.1 mrg for (int i = 1; i < len1; ++i)
1850 1.1 mrg {
1851 1.1 mrg tree t1 = TREE_VEC_ELT (a1, i);
1852 1.1 mrg tree t2 = TREE_VEC_ELT (a2, i);
1853 1.1 mrg if (!template_args_equal (t1, t2))
1854 1.1 mrg return false;
1855 1.1 mrg }
1856 1.1 mrg return true;
1857 1.1 mrg }
1858 1.1 mrg
1859 1.6 mrg /* Return a hash value for the placeholder ATOMIC_CONSTR C. */
1860 1.1 mrg
1861 1.1 mrg hashval_t
1862 1.1 mrg hash_placeholder_constraint (tree c)
1863 1.1 mrg {
1864 1.1 mrg tree t, a;
1865 1.1 mrg placeholder_extract_concept_and_args (c, t, a);
1866 1.1 mrg
1867 1.1 mrg /* Like hash_tmpl_and_args, but skip the first argument. */
1868 1.1 mrg hashval_t val = iterative_hash_object (DECL_UID (t), 0);
1869 1.1 mrg
1870 1.1 mrg for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1871 1.1 mrg val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1872 1.1 mrg
1873 1.1 mrg return val;
1874 1.1 mrg }
1875 1.1 mrg
1876 1.6 mrg /* Substitute through the simple requirement. */
1877 1.1 mrg
1878 1.6 mrg static tree
1879 1.6 mrg tsubst_valid_expression_requirement (tree t, tree args, subst_info info)
1880 1.1 mrg {
1881 1.6 mrg tree r = tsubst_expr (t, args, info.complain, info.in_decl, false);
1882 1.6 mrg if (convert_to_void (r, ICV_STATEMENT, info.complain) == error_mark_node)
1883 1.6 mrg return error_mark_node;
1884 1.6 mrg return r;
1885 1.1 mrg }
1886 1.1 mrg
1887 1.1 mrg
1888 1.6 mrg /* Substitute through the simple requirement. */
1889 1.6 mrg
1890 1.6 mrg static tree
1891 1.6 mrg tsubst_simple_requirement (tree t, tree args, subst_info info)
1892 1.1 mrg {
1893 1.6 mrg tree t0 = TREE_OPERAND (t, 0);
1894 1.6 mrg tree expr = tsubst_valid_expression_requirement (t0, args, info);
1895 1.6 mrg if (expr == error_mark_node)
1896 1.1 mrg return error_mark_node;
1897 1.6 mrg return finish_simple_requirement (EXPR_LOCATION (t), expr);
1898 1.1 mrg }
1899 1.1 mrg
1900 1.6 mrg /* Substitute through the type requirement. */
1901 1.1 mrg
1902 1.6 mrg static tree
1903 1.6 mrg tsubst_type_requirement (tree t, tree args, subst_info info)
1904 1.1 mrg {
1905 1.1 mrg tree t0 = TREE_OPERAND (t, 0);
1906 1.6 mrg tree type = tsubst (t0, args, info.complain, info.in_decl);
1907 1.6 mrg if (type == error_mark_node)
1908 1.1 mrg return error_mark_node;
1909 1.6 mrg return finish_type_requirement (EXPR_LOCATION (t), type);
1910 1.1 mrg }
1911 1.1 mrg
1912 1.6 mrg /* True if TYPE can be deduced from EXPR. */
1913 1.1 mrg
1914 1.6 mrg static bool
1915 1.6 mrg type_deducible_p (tree expr, tree type, tree placeholder, tree args,
1916 1.6 mrg subst_info info)
1917 1.6 mrg {
1918 1.6 mrg /* Make sure deduction is performed against ( EXPR ), so that
1919 1.6 mrg references are preserved in the result. */
1920 1.6 mrg expr = force_paren_expr_uneval (expr);
1921 1.6 mrg
1922 1.6 mrg /* Replace the constraints with the instantiated constraints. This
1923 1.6 mrg substitutes args into any template parameters in the trailing
1924 1.6 mrg result type. */
1925 1.6 mrg tree saved_constr = PLACEHOLDER_TYPE_CONSTRAINTS (placeholder);
1926 1.6 mrg tree subst_constr
1927 1.6 mrg = tsubst_constraint (saved_constr,
1928 1.6 mrg args,
1929 1.6 mrg info.complain | tf_partial,
1930 1.6 mrg info.in_decl);
1931 1.1 mrg
1932 1.6 mrg if (subst_constr == error_mark_node)
1933 1.6 mrg return false;
1934 1.6 mrg
1935 1.6 mrg PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = subst_constr;
1936 1.6 mrg
1937 1.6 mrg /* Temporarily unlink the canonical type. */
1938 1.6 mrg tree saved_type = TYPE_CANONICAL (placeholder);
1939 1.6 mrg TYPE_CANONICAL (placeholder) = NULL_TREE;
1940 1.6 mrg
1941 1.6 mrg tree deduced_type
1942 1.6 mrg = do_auto_deduction (type,
1943 1.6 mrg expr,
1944 1.6 mrg placeholder,
1945 1.6 mrg info.complain,
1946 1.6 mrg adc_requirement);
1947 1.6 mrg
1948 1.6 mrg PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = saved_constr;
1949 1.6 mrg TYPE_CANONICAL (placeholder) = saved_type;
1950 1.6 mrg
1951 1.6 mrg if (deduced_type == error_mark_node)
1952 1.6 mrg return false;
1953 1.6 mrg
1954 1.6 mrg return true;
1955 1.1 mrg }
1956 1.1 mrg
1957 1.6 mrg /* True if EXPR can not be converted to TYPE. */
1958 1.1 mrg
1959 1.6 mrg static bool
1960 1.6 mrg expression_convertible_p (tree expr, tree type, subst_info info)
1961 1.1 mrg {
1962 1.6 mrg tree conv =
1963 1.6 mrg perform_direct_initialization_if_possible (type, expr, false,
1964 1.6 mrg info.complain);
1965 1.6 mrg if (conv == error_mark_node)
1966 1.6 mrg return false;
1967 1.6 mrg if (conv == NULL_TREE)
1968 1.6 mrg {
1969 1.6 mrg if (info.complain & tf_error)
1970 1.6 mrg {
1971 1.6 mrg location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
1972 1.6 mrg error_at (loc, "cannot convert %qE to %qT", expr, type);
1973 1.6 mrg }
1974 1.6 mrg return false;
1975 1.6 mrg }
1976 1.6 mrg return true;
1977 1.1 mrg }
1978 1.1 mrg
1979 1.1 mrg
1980 1.6 mrg /* Substitute through the compound requirement. */
1981 1.6 mrg
1982 1.6 mrg static tree
1983 1.6 mrg tsubst_compound_requirement (tree t, tree args, subst_info info)
1984 1.1 mrg {
1985 1.6 mrg tree t0 = TREE_OPERAND (t, 0);
1986 1.6 mrg tree t1 = TREE_OPERAND (t, 1);
1987 1.6 mrg tree expr = tsubst_valid_expression_requirement (t0, args, info);
1988 1.6 mrg if (expr == error_mark_node)
1989 1.1 mrg return error_mark_node;
1990 1.6 mrg
1991 1.6 mrg /* Check the noexcept condition. */
1992 1.6 mrg bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1993 1.6 mrg if (noexcept_p && !expr_noexcept_p (expr, tf_none))
1994 1.1 mrg return error_mark_node;
1995 1.6 mrg
1996 1.6 mrg /* Substitute through the type expression, if any. */
1997 1.6 mrg tree type = tsubst (t1, args, info.complain, info.in_decl);
1998 1.6 mrg if (type == error_mark_node)
1999 1.6 mrg return error_mark_node;
2000 1.6 mrg
2001 1.6 mrg subst_info quiet (tf_none, info.in_decl);
2002 1.6 mrg
2003 1.6 mrg /* Check expression against the result type. */
2004 1.6 mrg if (type)
2005 1.6 mrg {
2006 1.6 mrg if (tree placeholder = type_uses_auto (type))
2007 1.6 mrg {
2008 1.6 mrg if (!type_deducible_p (expr, type, placeholder, args, quiet))
2009 1.6 mrg return error_mark_node;
2010 1.6 mrg }
2011 1.6 mrg else if (!expression_convertible_p (expr, type, quiet))
2012 1.6 mrg return error_mark_node;
2013 1.6 mrg }
2014 1.6 mrg
2015 1.6 mrg return finish_compound_requirement (EXPR_LOCATION (t),
2016 1.6 mrg expr, type, noexcept_p);
2017 1.1 mrg }
2018 1.1 mrg
2019 1.6 mrg static tree
2020 1.6 mrg tsubst_nested_requirement (tree t, tree args, subst_info info)
2021 1.1 mrg {
2022 1.6 mrg /* Ensure that we're in an evaluation context prior to satisfaction. */
2023 1.6 mrg tree norm = TREE_VALUE (TREE_TYPE (t));
2024 1.6 mrg tree result = satisfy_constraint (norm, args, info);
2025 1.6 mrg if (result == error_mark_node && info.quiet ())
2026 1.6 mrg {
2027 1.6 mrg subst_info noisy (tf_warning_or_error, info.in_decl);
2028 1.6 mrg satisfy_constraint (norm, args, noisy);
2029 1.6 mrg }
2030 1.6 mrg if (result != boolean_true_node)
2031 1.1 mrg return error_mark_node;
2032 1.6 mrg return result;
2033 1.1 mrg }
2034 1.1 mrg
2035 1.6 mrg /* Substitute ARGS into the requirement T. */
2036 1.1 mrg
2037 1.6 mrg static tree
2038 1.6 mrg tsubst_requirement (tree t, tree args, subst_info info)
2039 1.1 mrg {
2040 1.6 mrg iloc_sentinel loc_s (cp_expr_location (t));
2041 1.6 mrg switch (TREE_CODE (t))
2042 1.6 mrg {
2043 1.6 mrg case SIMPLE_REQ:
2044 1.6 mrg return tsubst_simple_requirement (t, args, info);
2045 1.6 mrg case TYPE_REQ:
2046 1.6 mrg return tsubst_type_requirement (t, args, info);
2047 1.6 mrg case COMPOUND_REQ:
2048 1.6 mrg return tsubst_compound_requirement (t, args, info);
2049 1.6 mrg case NESTED_REQ:
2050 1.6 mrg return tsubst_nested_requirement (t, args, info);
2051 1.6 mrg default:
2052 1.6 mrg break;
2053 1.6 mrg }
2054 1.6 mrg gcc_unreachable ();
2055 1.1 mrg }
2056 1.1 mrg
2057 1.6 mrg /* Substitute ARGS into the list of requirements T. Note that
2058 1.6 mrg substitution failures here result in ill-formed programs. */
2059 1.6 mrg
2060 1.6 mrg static tree
2061 1.6 mrg tsubst_requirement_body (tree t, tree args, subst_info info)
2062 1.6 mrg {
2063 1.6 mrg tree result = NULL_TREE;
2064 1.6 mrg while (t)
2065 1.6 mrg {
2066 1.6 mrg tree req = tsubst_requirement (TREE_VALUE (t), args, info);
2067 1.6 mrg if (req == error_mark_node)
2068 1.6 mrg return error_mark_node;
2069 1.6 mrg result = tree_cons (NULL_TREE, req, result);
2070 1.6 mrg t = TREE_CHAIN (t);
2071 1.6 mrg }
2072 1.6 mrg return nreverse (result);
2073 1.6 mrg }
2074 1.1 mrg
2075 1.6 mrg static tree
2076 1.1 mrg declare_constraint_vars (tree parms, tree vars)
2077 1.1 mrg {
2078 1.1 mrg tree s = vars;
2079 1.1 mrg for (tree t = parms; t; t = DECL_CHAIN (t))
2080 1.1 mrg {
2081 1.1 mrg if (DECL_PACK_P (t))
2082 1.1 mrg {
2083 1.1 mrg tree pack = extract_fnparm_pack (t, &s);
2084 1.1 mrg register_local_specialization (pack, t);
2085 1.1 mrg }
2086 1.1 mrg else
2087 1.1 mrg {
2088 1.1 mrg register_local_specialization (s, t);
2089 1.1 mrg s = DECL_CHAIN (s);
2090 1.1 mrg }
2091 1.1 mrg }
2092 1.1 mrg return vars;
2093 1.1 mrg }
2094 1.1 mrg
2095 1.6 mrg /* Substitute through as if checking function parameter types. This
2096 1.6 mrg will diagnose common parameter type errors. Returns error_mark_node
2097 1.6 mrg if an error occurred. */
2098 1.6 mrg
2099 1.6 mrg static tree
2100 1.6 mrg check_constaint_variables (tree t, tree args, subst_info info)
2101 1.6 mrg {
2102 1.6 mrg tree types = NULL_TREE;
2103 1.6 mrg tree p = t;
2104 1.6 mrg while (p && !VOID_TYPE_P (p))
2105 1.6 mrg {
2106 1.6 mrg types = tree_cons (NULL_TREE, TREE_TYPE (p), types);
2107 1.6 mrg p = TREE_CHAIN (p);
2108 1.6 mrg }
2109 1.6 mrg types = chainon (nreverse (types), void_list_node);
2110 1.6 mrg return tsubst_function_parms (types, args, info.complain, info.in_decl);
2111 1.6 mrg }
2112 1.6 mrg
2113 1.1 mrg /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
2114 1.1 mrg into the parameter list T, producing a sequence of constraint
2115 1.1 mrg variables, declared in the current scope.
2116 1.1 mrg
2117 1.1 mrg Note that the caller must establish a local specialization stack
2118 1.1 mrg prior to calling this function since this substitution will
2119 1.1 mrg declare the substituted parameters. */
2120 1.1 mrg
2121 1.6 mrg static tree
2122 1.6 mrg tsubst_constraint_variables (tree t, tree args, subst_info info)
2123 1.1 mrg {
2124 1.6 mrg /* Perform a trial substitution to check for type errors. */
2125 1.6 mrg tree parms = check_constaint_variables (t, args, info);
2126 1.6 mrg if (parms == error_mark_node)
2127 1.6 mrg return error_mark_node;
2128 1.6 mrg
2129 1.1 mrg /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
2130 1.1 mrg of PARM_DECLs. */
2131 1.1 mrg int saved_unevaluated_operand = cp_unevaluated_operand;
2132 1.1 mrg cp_unevaluated_operand = 0;
2133 1.6 mrg tree vars = tsubst (t, args, info.complain, info.in_decl);
2134 1.1 mrg cp_unevaluated_operand = saved_unevaluated_operand;
2135 1.1 mrg if (vars == error_mark_node)
2136 1.1 mrg return error_mark_node;
2137 1.1 mrg return declare_constraint_vars (t, vars);
2138 1.1 mrg }
2139 1.1 mrg
2140 1.6 mrg /* Substitute ARGS into the requires-expression T. [8.4.7]p6. The
2141 1.6 mrg substitution of template arguments into a requires-expression
2142 1.6 mrg may result in the formation of invalid types or expressions
2143 1.6 mrg in its requirements ... In such cases, the expression evaluates
2144 1.6 mrg to false; it does not cause the program to be ill-formed.
2145 1.1 mrg
2146 1.6 mrg However, there are cases where substitution must produce a
2147 1.6 mrg new requires-expression, that is not a template constraint.
2148 1.6 mrg For example:
2149 1.1 mrg
2150 1.6 mrg template<typename T>
2151 1.6 mrg class X {
2152 1.6 mrg template<typename U>
2153 1.6 mrg static constexpr bool var = requires (U u) { T::fn(u); };
2154 1.6 mrg };
2155 1.6 mrg
2156 1.6 mrg In the instantiation of X<Y> (assuming Y defines fn), then the
2157 1.6 mrg instantiated requires-expression would include Y::fn(u). If any
2158 1.6 mrg substitution in the requires-expression fails, we can immediately
2159 1.6 mrg fold the expression to false, as would be the case e.g., when
2160 1.6 mrg instantiation X<int>. */
2161 1.1 mrg
2162 1.1 mrg tree
2163 1.6 mrg tsubst_requires_expr (tree t, tree args,
2164 1.6 mrg tsubst_flags_t complain, tree in_decl)
2165 1.1 mrg {
2166 1.6 mrg local_specialization_stack stack (lss_copy);
2167 1.1 mrg
2168 1.6 mrg subst_info info (complain, in_decl);
2169 1.1 mrg
2170 1.6 mrg /* A requires-expression is an unevaluated context. */
2171 1.6 mrg cp_unevaluated u;
2172 1.1 mrg
2173 1.1 mrg tree parms = TREE_OPERAND (t, 0);
2174 1.1 mrg if (parms)
2175 1.1 mrg {
2176 1.6 mrg parms = tsubst_constraint_variables (parms, args, info);
2177 1.1 mrg if (parms == error_mark_node)
2178 1.6 mrg return boolean_false_node;
2179 1.1 mrg }
2180 1.1 mrg
2181 1.1 mrg tree reqs = TREE_OPERAND (t, 1);
2182 1.6 mrg reqs = tsubst_requirement_body (reqs, args, info);
2183 1.1 mrg if (reqs == error_mark_node)
2184 1.6 mrg return boolean_false_node;
2185 1.6 mrg
2186 1.6 mrg if (processing_template_decl)
2187 1.6 mrg return finish_requires_expr (cp_expr_location (t), parms, reqs);
2188 1.1 mrg
2189 1.6 mrg return boolean_true_node;
2190 1.1 mrg }
2191 1.1 mrg
2192 1.1 mrg /* Substitute ARGS into the constraint information CI, producing a new
2193 1.6 mrg constraint record. */
2194 1.1 mrg
2195 1.1 mrg tree
2196 1.1 mrg tsubst_constraint_info (tree t, tree args,
2197 1.1 mrg tsubst_flags_t complain, tree in_decl)
2198 1.1 mrg {
2199 1.1 mrg if (!t || t == error_mark_node || !check_constraint_info (t))
2200 1.1 mrg return NULL_TREE;
2201 1.1 mrg
2202 1.6 mrg tree tr = tsubst_constraint (CI_TEMPLATE_REQS (t), args, complain, in_decl);
2203 1.6 mrg tree dr = tsubst_constraint (CI_DECLARATOR_REQS (t), args, complain, in_decl);
2204 1.6 mrg return build_constraints (tr, dr);
2205 1.6 mrg }
2206 1.1 mrg
2207 1.6 mrg /* Substitute through a parameter mapping, in order to get the actual
2208 1.6 mrg arguments used to instantiate an atomic constraint. This may fail
2209 1.6 mrg if the substitution into arguments produces something ill-formed. */
2210 1.1 mrg
2211 1.6 mrg static tree
2212 1.6 mrg tsubst_parameter_mapping (tree map, tree args, subst_info info)
2213 1.6 mrg {
2214 1.6 mrg if (!map)
2215 1.6 mrg return NULL_TREE;
2216 1.6 mrg
2217 1.6 mrg tsubst_flags_t complain = info.complain;
2218 1.6 mrg tree in_decl = info.in_decl;
2219 1.6 mrg
2220 1.6 mrg tree result = NULL_TREE;
2221 1.6 mrg for (tree p = map; p; p = TREE_CHAIN (p))
2222 1.6 mrg {
2223 1.6 mrg if (p == error_mark_node)
2224 1.6 mrg return error_mark_node;
2225 1.6 mrg tree parm = TREE_VALUE (p);
2226 1.6 mrg tree arg = TREE_PURPOSE (p);
2227 1.6 mrg tree new_arg = NULL_TREE;
2228 1.6 mrg if (TYPE_P (arg))
2229 1.6 mrg {
2230 1.6 mrg /* If a template parameter is declared with a placeholder, we can
2231 1.6 mrg get those in the argument list if decltype is applied to the
2232 1.6 mrg placeholder. For example:
2233 1.6 mrg
2234 1.6 mrg template<auto T>
2235 1.6 mrg requires C<decltype(T)>
2236 1.6 mrg void f() { }
2237 1.6 mrg
2238 1.6 mrg The normalized argument for C will be an auto type, so we'll
2239 1.6 mrg need to deduce the actual argument from the corresponding
2240 1.6 mrg initializer (whatever argument is provided for T), and use
2241 1.6 mrg that result in the instantiated parameter mapping. */
2242 1.6 mrg if (tree auto_node = type_uses_auto (arg))
2243 1.6 mrg {
2244 1.6 mrg int level;
2245 1.6 mrg int index;
2246 1.6 mrg template_parm_level_and_index (parm, &level, &index);
2247 1.6 mrg tree init = TMPL_ARG (args, level, index);
2248 1.6 mrg new_arg = do_auto_deduction (arg, init, auto_node,
2249 1.6 mrg complain, adc_variable_type,
2250 1.6 mrg make_tree_vec (0));
2251 1.6 mrg }
2252 1.6 mrg }
2253 1.6 mrg else if (ARGUMENT_PACK_P (arg))
2254 1.6 mrg new_arg = tsubst_argument_pack (arg, args, complain, in_decl);
2255 1.6 mrg if (!new_arg)
2256 1.6 mrg {
2257 1.6 mrg new_arg = tsubst_template_arg (arg, args, complain, in_decl);
2258 1.6 mrg if (TYPE_P (new_arg))
2259 1.6 mrg new_arg = canonicalize_type_argument (new_arg, complain);
2260 1.6 mrg }
2261 1.6 mrg if (new_arg == error_mark_node)
2262 1.6 mrg return error_mark_node;
2263 1.6 mrg
2264 1.6 mrg result = tree_cons (new_arg, parm, result);
2265 1.6 mrg }
2266 1.6 mrg return nreverse (result);
2267 1.1 mrg }
2268 1.1 mrg
2269 1.1 mrg tree
2270 1.6 mrg tsubst_parameter_mapping (tree map, tree args, tsubst_flags_t complain, tree in_decl)
2271 1.1 mrg {
2272 1.6 mrg return tsubst_parameter_mapping (map, args, subst_info (complain, in_decl));
2273 1.1 mrg }
2274 1.1 mrg
2275 1.1 mrg /*---------------------------------------------------------------------------
2276 1.1 mrg Constraint satisfaction
2277 1.1 mrg ---------------------------------------------------------------------------*/
2278 1.1 mrg
2279 1.6 mrg /* Hash functions for satisfaction entries. */
2280 1.1 mrg
2281 1.6 mrg struct GTY((for_user)) sat_entry
2282 1.6 mrg {
2283 1.6 mrg tree constr;
2284 1.6 mrg tree args;
2285 1.6 mrg tree result;
2286 1.6 mrg };
2287 1.1 mrg
2288 1.6 mrg struct sat_hasher : ggc_ptr_hash<sat_entry>
2289 1.6 mrg {
2290 1.6 mrg static hashval_t hash (sat_entry *e)
2291 1.6 mrg {
2292 1.6 mrg hashval_t value = hash_atomic_constraint (e->constr);
2293 1.6 mrg return iterative_hash_template_arg (e->args, value);
2294 1.6 mrg }
2295 1.1 mrg
2296 1.6 mrg static bool equal (sat_entry *e1, sat_entry *e2)
2297 1.6 mrg {
2298 1.6 mrg if (!atomic_constraints_identical_p (e1->constr, e2->constr))
2299 1.6 mrg return false;
2300 1.6 mrg return template_args_equal (e1->args, e2->args);
2301 1.6 mrg }
2302 1.6 mrg };
2303 1.1 mrg
2304 1.6 mrg /* Cache the result of satisfy_atom. */
2305 1.6 mrg static GTY((deletable)) hash_table<sat_hasher> *sat_cache;
2306 1.1 mrg
2307 1.6 mrg /* Cache the result of constraint_satisfaction_value. */
2308 1.6 mrg static GTY((deletable)) hash_map<tree, tree> *decl_satisfied_cache;
2309 1.1 mrg
2310 1.6 mrg static tree
2311 1.6 mrg get_satisfaction (tree constr, tree args)
2312 1.6 mrg {
2313 1.6 mrg if (!sat_cache)
2314 1.6 mrg return NULL_TREE;
2315 1.6 mrg sat_entry elt = { constr, args, NULL_TREE };
2316 1.6 mrg sat_entry* found = sat_cache->find (&elt);
2317 1.6 mrg if (found)
2318 1.6 mrg return found->result;
2319 1.6 mrg else
2320 1.6 mrg return NULL_TREE;
2321 1.1 mrg }
2322 1.1 mrg
2323 1.6 mrg static void
2324 1.6 mrg save_satisfaction (tree constr, tree args, tree result)
2325 1.6 mrg {
2326 1.6 mrg if (!sat_cache)
2327 1.6 mrg sat_cache = hash_table<sat_hasher>::create_ggc (31);
2328 1.6 mrg sat_entry elt = {constr, args, result};
2329 1.6 mrg sat_entry** slot = sat_cache->find_slot (&elt, INSERT);
2330 1.6 mrg sat_entry* entry = ggc_alloc<sat_entry> ();
2331 1.6 mrg *entry = elt;
2332 1.6 mrg *slot = entry;
2333 1.6 mrg }
2334 1.1 mrg
2335 1.6 mrg void
2336 1.6 mrg clear_satisfaction_cache ()
2337 1.1 mrg {
2338 1.6 mrg if (sat_cache)
2339 1.6 mrg sat_cache->empty ();
2340 1.6 mrg if (decl_satisfied_cache)
2341 1.6 mrg decl_satisfied_cache->empty ();
2342 1.6 mrg }
2343 1.1 mrg
2344 1.6 mrg /* A tool to help manage satisfaction caching in satisfy_constraint_r.
2345 1.6 mrg Note the cache is only used when not diagnosing errors. */
2346 1.1 mrg
2347 1.6 mrg struct satisfaction_cache
2348 1.6 mrg {
2349 1.6 mrg satisfaction_cache (tree constr, tree args, tsubst_flags_t complain)
2350 1.6 mrg : constr(constr), args(args), complain(complain)
2351 1.6 mrg { }
2352 1.1 mrg
2353 1.6 mrg tree get ()
2354 1.6 mrg {
2355 1.6 mrg if (complain == tf_none)
2356 1.6 mrg return get_satisfaction (constr, args);
2357 1.6 mrg return NULL_TREE;
2358 1.6 mrg }
2359 1.1 mrg
2360 1.6 mrg tree save (tree result)
2361 1.6 mrg {
2362 1.6 mrg if (complain == tf_none)
2363 1.6 mrg save_satisfaction (constr, args, result);
2364 1.6 mrg return result;
2365 1.6 mrg }
2366 1.1 mrg
2367 1.6 mrg tree constr;
2368 1.6 mrg tree args;
2369 1.6 mrg tsubst_flags_t complain;
2370 1.6 mrg };
2371 1.1 mrg
2372 1.6 mrg static int satisfying_constraint = 0;
2373 1.1 mrg
2374 1.6 mrg /* Returns true if we are currently satisfying a constraint.
2375 1.1 mrg
2376 1.6 mrg This is used to guard against recursive calls to evaluate_concept_check
2377 1.6 mrg during template argument substitution.
2378 1.1 mrg
2379 1.6 mrg TODO: Do we need this now that we fully normalize prior to evaluation?
2380 1.6 mrg I think not. */
2381 1.1 mrg
2382 1.6 mrg bool
2383 1.6 mrg satisfying_constraint_p ()
2384 1.6 mrg {
2385 1.6 mrg return satisfying_constraint;
2386 1.1 mrg }
2387 1.1 mrg
2388 1.6 mrg /* Substitute ARGS into constraint-expression T during instantiation of
2389 1.6 mrg a member of a class template. */
2390 1.1 mrg
2391 1.1 mrg tree
2392 1.6 mrg tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2393 1.1 mrg {
2394 1.6 mrg /* We also don't want to evaluate concept-checks when substituting the
2395 1.6 mrg constraint-expressions of a declaration. */
2396 1.6 mrg processing_constraint_expression_sentinel s;
2397 1.6 mrg tree expr = tsubst_expr (t, args, complain, in_decl, false);
2398 1.6 mrg return expr;
2399 1.1 mrg }
2400 1.1 mrg
2401 1.6 mrg static tree satisfy_constraint_r (tree, tree, subst_info info);
2402 1.1 mrg
2403 1.6 mrg /* Compute the satisfaction of a conjunction. */
2404 1.1 mrg
2405 1.6 mrg static tree
2406 1.6 mrg satisfy_conjunction (tree t, tree args, subst_info info)
2407 1.1 mrg {
2408 1.6 mrg tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, info);
2409 1.6 mrg if (lhs == error_mark_node || lhs == boolean_false_node)
2410 1.6 mrg return lhs;
2411 1.6 mrg return satisfy_constraint_r (TREE_OPERAND (t, 1), args, info);
2412 1.1 mrg }
2413 1.1 mrg
2414 1.6 mrg /* The current depth at which we're replaying an error during recursive
2415 1.6 mrg diagnosis of a constraint satisfaction failure. */
2416 1.1 mrg
2417 1.6 mrg static int current_constraint_diagnosis_depth;
2418 1.1 mrg
2419 1.6 mrg /* Whether CURRENT_CONSTRAINT_DIAGNOSIS_DEPTH has ever exceeded
2420 1.6 mrg CONCEPTS_DIAGNOSTICS_MAX_DEPTH during recursive diagnosis of a constraint
2421 1.6 mrg satisfaction error. */
2422 1.1 mrg
2423 1.6 mrg static bool concepts_diagnostics_max_depth_exceeded_p;
2424 1.1 mrg
2425 1.6 mrg /* Recursive subroutine of collect_operands_of_disjunction. T is a normalized
2426 1.6 mrg subexpression of a constraint (composed of CONJ_CONSTRs and DISJ_CONSTRs)
2427 1.6 mrg and E is the corresponding unnormalized subexpression (composed of
2428 1.6 mrg TRUTH_ANDIF_EXPRs and TRUTH_ORIF_EXPRs). */
2429 1.1 mrg
2430 1.6 mrg static void
2431 1.6 mrg collect_operands_of_disjunction_r (tree t, tree e,
2432 1.6 mrg auto_vec<tree_pair> *operands)
2433 1.1 mrg {
2434 1.6 mrg if (TREE_CODE (e) == TRUTH_ORIF_EXPR)
2435 1.6 mrg {
2436 1.6 mrg collect_operands_of_disjunction_r (TREE_OPERAND (t, 0),
2437 1.6 mrg TREE_OPERAND (e, 0), operands);
2438 1.6 mrg collect_operands_of_disjunction_r (TREE_OPERAND (t, 1),
2439 1.6 mrg TREE_OPERAND (e, 1), operands);
2440 1.6 mrg }
2441 1.1 mrg else
2442 1.6 mrg {
2443 1.6 mrg tree_pair p = std::make_pair (t, e);
2444 1.6 mrg operands->safe_push (p);
2445 1.6 mrg }
2446 1.1 mrg }
2447 1.1 mrg
2448 1.6 mrg /* Recursively collect the normalized and unnormalized operands of the
2449 1.6 mrg disjunction T and append them to OPERANDS in order. */
2450 1.1 mrg
2451 1.6 mrg static void
2452 1.6 mrg collect_operands_of_disjunction (tree t, auto_vec<tree_pair> *operands)
2453 1.1 mrg {
2454 1.6 mrg collect_operands_of_disjunction_r (t, CONSTR_EXPR (t), operands);
2455 1.1 mrg }
2456 1.1 mrg
2457 1.6 mrg /* Compute the satisfaction of a disjunction. */
2458 1.6 mrg
2459 1.6 mrg static tree
2460 1.6 mrg satisfy_disjunction (tree t, tree args, subst_info info)
2461 1.6 mrg {
2462 1.6 mrg /* Evaluate the operands quietly. */
2463 1.6 mrg subst_info quiet (tf_none, NULL_TREE);
2464 1.1 mrg
2465 1.6 mrg /* Register the constraint for diagnostics, if needed. */
2466 1.6 mrg diagnosing_failed_constraint failure (t, args, info.noisy ());
2467 1.1 mrg
2468 1.6 mrg tree lhs = satisfy_constraint_r (TREE_OPERAND (t, 0), args, quiet);
2469 1.6 mrg if (lhs == boolean_true_node)
2470 1.6 mrg return boolean_true_node;
2471 1.6 mrg tree rhs = satisfy_constraint_r (TREE_OPERAND (t, 1), args, quiet);
2472 1.6 mrg if (rhs != boolean_true_node && info.noisy ())
2473 1.6 mrg {
2474 1.6 mrg cp_expr disj_expr = CONSTR_EXPR (t);
2475 1.6 mrg inform (disj_expr.get_location (),
2476 1.6 mrg "no operand of the disjunction is satisfied");
2477 1.6 mrg if (diagnosing_failed_constraint::replay_errors_p ())
2478 1.6 mrg {
2479 1.6 mrg /* Replay the error in each branch of the disjunction. */
2480 1.6 mrg auto_vec<tree_pair> operands;
2481 1.6 mrg collect_operands_of_disjunction (t, &operands);
2482 1.6 mrg for (unsigned i = 0; i < operands.length (); i++)
2483 1.6 mrg {
2484 1.6 mrg tree norm_op = operands[i].first;
2485 1.6 mrg tree op = operands[i].second;
2486 1.6 mrg location_t loc = make_location (cp_expr_location (op),
2487 1.6 mrg disj_expr.get_start (),
2488 1.6 mrg disj_expr.get_finish ());
2489 1.6 mrg inform (loc, "the operand %qE is unsatisfied because", op);
2490 1.6 mrg satisfy_constraint_r (norm_op, args, info);
2491 1.6 mrg }
2492 1.6 mrg }
2493 1.6 mrg }
2494 1.6 mrg return rhs;
2495 1.1 mrg }
2496 1.1 mrg
2497 1.6 mrg /* Ensures that T is a truth value and not (accidentally, as sometimes
2498 1.6 mrg happens) an integer value. */
2499 1.1 mrg
2500 1.1 mrg tree
2501 1.6 mrg satisfaction_value (tree t)
2502 1.1 mrg {
2503 1.6 mrg if (t == error_mark_node || t == boolean_true_node || t == boolean_false_node)
2504 1.6 mrg return t;
2505 1.6 mrg gcc_assert (TREE_CODE (t) == INTEGER_CST);
2506 1.6 mrg if (integer_onep (t))
2507 1.1 mrg return boolean_true_node;
2508 1.6 mrg if (integer_zerop (t))
2509 1.6 mrg return boolean_false_node;
2510 1.6 mrg
2511 1.6 mrg /* Anything else should be invalid. */
2512 1.6 mrg gcc_assert (false);
2513 1.1 mrg }
2514 1.1 mrg
2515 1.6 mrg /* Build a new template argument list with template arguments corresponding
2516 1.6 mrg to the parameters used in an atomic constraint. */
2517 1.1 mrg
2518 1.1 mrg tree
2519 1.6 mrg get_mapped_args (tree map)
2520 1.1 mrg {
2521 1.6 mrg /* No map, no arguments. */
2522 1.6 mrg if (!map)
2523 1.6 mrg return NULL_TREE;
2524 1.1 mrg
2525 1.6 mrg /* Find the mapped parameter with the highest level. */
2526 1.6 mrg int count = 0;
2527 1.6 mrg for (tree p = map; p; p = TREE_CHAIN (p))
2528 1.6 mrg {
2529 1.6 mrg int level;
2530 1.6 mrg int index;
2531 1.6 mrg template_parm_level_and_index (TREE_VALUE (p), &level, &index);
2532 1.6 mrg if (level > count)
2533 1.6 mrg count = level;
2534 1.6 mrg }
2535 1.6 mrg
2536 1.6 mrg /* Place each argument at its corresponding position in the argument
2537 1.6 mrg list. Note that the list will be sparse (not all arguments supplied),
2538 1.6 mrg but instantiation is guaranteed to only use the parameters in the
2539 1.6 mrg mapping, so null arguments would never be used. */
2540 1.6 mrg auto_vec< vec<tree> > lists (count);
2541 1.6 mrg lists.quick_grow_cleared (count);
2542 1.6 mrg for (tree p = map; p; p = TREE_CHAIN (p))
2543 1.6 mrg {
2544 1.6 mrg int level;
2545 1.6 mrg int index;
2546 1.6 mrg template_parm_level_and_index (TREE_VALUE (p), &level, &index);
2547 1.6 mrg
2548 1.6 mrg /* Insert the argument into its corresponding position. */
2549 1.6 mrg vec<tree> &list = lists[level - 1];
2550 1.6 mrg if (index >= (int)list.length ())
2551 1.6 mrg list.safe_grow_cleared (index + 1);
2552 1.6 mrg list[index] = TREE_PURPOSE (p);
2553 1.6 mrg }
2554 1.6 mrg
2555 1.6 mrg /* Build the new argument list. */
2556 1.6 mrg tree args = make_tree_vec (lists.length ());
2557 1.6 mrg for (unsigned i = 0; i != lists.length (); ++i)
2558 1.6 mrg {
2559 1.6 mrg vec<tree> &list = lists[i];
2560 1.6 mrg tree level = make_tree_vec (list.length ());
2561 1.6 mrg for (unsigned j = 0; j < list.length(); ++j)
2562 1.6 mrg TREE_VEC_ELT (level, j) = list[j];
2563 1.6 mrg SET_TMPL_ARGS_LEVEL (args, i + 1, level);
2564 1.6 mrg list.release ();
2565 1.6 mrg }
2566 1.6 mrg SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, 0);
2567 1.1 mrg
2568 1.6 mrg return args;
2569 1.6 mrg }
2570 1.1 mrg
2571 1.6 mrg static void diagnose_atomic_constraint (tree, tree, tree, subst_info);
2572 1.1 mrg
2573 1.6 mrg /* Compute the satisfaction of an atomic constraint. */
2574 1.1 mrg
2575 1.6 mrg static tree
2576 1.6 mrg satisfy_atom (tree t, tree args, subst_info info)
2577 1.6 mrg {
2578 1.6 mrg satisfaction_cache cache (t, args, info.complain);
2579 1.6 mrg if (tree r = cache.get ())
2580 1.6 mrg return r;
2581 1.1 mrg
2582 1.6 mrg /* Perform substitution quietly. */
2583 1.6 mrg subst_info quiet (tf_none, NULL_TREE);
2584 1.1 mrg
2585 1.6 mrg /* In case there is a diagnostic, we want to establish the context
2586 1.6 mrg prior to printing errors. If no errors occur, this context is
2587 1.6 mrg removed before returning. */
2588 1.6 mrg diagnosing_failed_constraint failure (t, args, info.noisy ());
2589 1.1 mrg
2590 1.6 mrg /* Instantiate the parameter mapping. */
2591 1.6 mrg tree map = tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, quiet);
2592 1.6 mrg if (map == error_mark_node)
2593 1.6 mrg {
2594 1.6 mrg /* If instantiation of the parameter mapping fails, the program
2595 1.6 mrg is ill-formed. */
2596 1.6 mrg if (info.noisy())
2597 1.6 mrg tsubst_parameter_mapping (ATOMIC_CONSTR_MAP (t), args, info);
2598 1.6 mrg return cache.save (boolean_false_node);
2599 1.6 mrg }
2600 1.1 mrg
2601 1.6 mrg /* Rebuild the argument vector from the parameter mapping. */
2602 1.6 mrg args = get_mapped_args (map);
2603 1.1 mrg
2604 1.6 mrg /* Apply the parameter mapping (i.e., just substitute). */
2605 1.6 mrg tree expr = ATOMIC_CONSTR_EXPR (t);
2606 1.6 mrg tree result = tsubst_expr (expr, args, quiet.complain, quiet.in_decl, false);
2607 1.6 mrg if (result == error_mark_node)
2608 1.6 mrg {
2609 1.6 mrg /* If substitution results in an invalid type or expression, the constraint
2610 1.6 mrg is not satisfied. Replay the substitution. */
2611 1.6 mrg if (info.noisy ())
2612 1.6 mrg tsubst_expr (expr, args, info.complain, info.in_decl, false);
2613 1.6 mrg return cache.save (boolean_false_node);
2614 1.6 mrg }
2615 1.1 mrg
2616 1.6 mrg /* [17.4.1.2] ... lvalue-to-rvalue conversion is performed as necessary,
2617 1.6 mrg and EXPR shall be a constant expression of type bool. */
2618 1.6 mrg result = force_rvalue (result, info.complain);
2619 1.6 mrg if (result == error_mark_node)
2620 1.6 mrg return cache.save (error_mark_node);
2621 1.6 mrg if (!same_type_p (TREE_TYPE (result), boolean_type_node))
2622 1.6 mrg {
2623 1.6 mrg if (info.noisy ())
2624 1.6 mrg diagnose_atomic_constraint (t, map, result, info);
2625 1.6 mrg return cache.save (error_mark_node);
2626 1.6 mrg }
2627 1.1 mrg
2628 1.6 mrg /* Compute the value of the constraint. */
2629 1.6 mrg if (info.noisy ())
2630 1.6 mrg result = cxx_constant_value (result);
2631 1.6 mrg else
2632 1.6 mrg {
2633 1.6 mrg result = maybe_constant_value (result, NULL_TREE,
2634 1.6 mrg /*manifestly_const_eval=*/true);
2635 1.6 mrg if (!TREE_CONSTANT (result))
2636 1.6 mrg result = error_mark_node;
2637 1.6 mrg }
2638 1.6 mrg result = satisfaction_value (result);
2639 1.6 mrg if (result == boolean_false_node && info.noisy ())
2640 1.6 mrg diagnose_atomic_constraint (t, map, result, info);
2641 1.6 mrg
2642 1.6 mrg return cache.save (result);
2643 1.6 mrg }
2644 1.6 mrg
2645 1.6 mrg /* Determine if the normalized constraint T is satisfied.
2646 1.6 mrg Returns boolean_true_node if the expression/constraint is
2647 1.6 mrg satisfied, boolean_false_node if not, and error_mark_node
2648 1.6 mrg if the there was an error evaluating the constraint.
2649 1.6 mrg
2650 1.6 mrg The parameter mapping of atomic constraints is simply the
2651 1.6 mrg set of template arguments that will be substituted into
2652 1.6 mrg the expression, regardless of template parameters appearing
2653 1.6 mrg withing. Whether a template argument is used in the atomic
2654 1.6 mrg constraint only matters for subsumption. */
2655 1.1 mrg
2656 1.6 mrg static tree
2657 1.6 mrg satisfy_constraint_r (tree t, tree args, subst_info info)
2658 1.6 mrg {
2659 1.6 mrg if (t == error_mark_node)
2660 1.6 mrg return error_mark_node;
2661 1.1 mrg
2662 1.6 mrg switch (TREE_CODE (t))
2663 1.6 mrg {
2664 1.6 mrg case CONJ_CONSTR:
2665 1.6 mrg return satisfy_conjunction (t, args, info);
2666 1.6 mrg case DISJ_CONSTR:
2667 1.6 mrg return satisfy_disjunction (t, args, info);
2668 1.6 mrg case ATOMIC_CONSTR:
2669 1.6 mrg return satisfy_atom (t, args, info);
2670 1.6 mrg default:
2671 1.6 mrg gcc_unreachable ();
2672 1.6 mrg }
2673 1.1 mrg }
2674 1.1 mrg
2675 1.6 mrg /* Check that the normalized constraint T is satisfied for ARGS. */
2676 1.1 mrg
2677 1.6 mrg static tree
2678 1.6 mrg satisfy_constraint (tree t, tree args, subst_info info)
2679 1.1 mrg {
2680 1.1 mrg auto_timevar time (TV_CONSTRAINT_SAT);
2681 1.1 mrg
2682 1.1 mrg /* Turn off template processing. Constraint satisfaction only applies
2683 1.1 mrg to non-dependent terms, so we want to ensure full checking here. */
2684 1.1 mrg processing_template_decl_sentinel proc (true);
2685 1.1 mrg
2686 1.6 mrg /* We need to check access during satisfaction. */
2687 1.6 mrg deferring_access_check_sentinel acs (dk_no_deferred);
2688 1.6 mrg
2689 1.6 mrg return satisfy_constraint_r (t, args, info);
2690 1.6 mrg }
2691 1.6 mrg
2692 1.6 mrg /* Check the normalized constraints T against ARGS, returning a satisfaction
2693 1.6 mrg value (either true, false, or error). */
2694 1.6 mrg
2695 1.6 mrg static tree
2696 1.6 mrg satisfy_associated_constraints (tree t, tree args, subst_info info)
2697 1.6 mrg {
2698 1.6 mrg /* If there are no constraints then this is trivially satisfied. */
2699 1.6 mrg if (!t)
2700 1.6 mrg return boolean_true_node;
2701 1.6 mrg
2702 1.6 mrg /* If any arguments depend on template parameters, we can't
2703 1.6 mrg check constraints. Pretend they're satisfied for now. */
2704 1.6 mrg if (args && uses_template_parms (args))
2705 1.6 mrg return boolean_true_node;
2706 1.6 mrg
2707 1.6 mrg return satisfy_constraint (t, args, info);
2708 1.6 mrg }
2709 1.6 mrg
2710 1.6 mrg /* Evaluate EXPR as a constraint expression using ARGS, returning a
2711 1.6 mrg satisfaction value. */
2712 1.6 mrg
2713 1.6 mrg static tree
2714 1.6 mrg satisfy_constraint_expression (tree t, tree args, subst_info info)
2715 1.6 mrg {
2716 1.6 mrg if (t == error_mark_node)
2717 1.6 mrg return error_mark_node;
2718 1.6 mrg
2719 1.6 mrg gcc_assert (EXPR_P (t));
2720 1.6 mrg
2721 1.6 mrg /* Get the normalized constraints. */
2722 1.6 mrg tree norm;
2723 1.6 mrg if (args == NULL_TREE && concept_check_p (t))
2724 1.6 mrg {
2725 1.6 mrg tree id = unpack_concept_check (t);
2726 1.6 mrg args = TREE_OPERAND (id, 1);
2727 1.6 mrg tree tmpl = get_concept_check_template (id);
2728 1.6 mrg norm = normalize_concept_definition (tmpl, info.noisy ());
2729 1.6 mrg }
2730 1.6 mrg else
2731 1.6 mrg norm = normalize_constraint_expression (t, info.noisy ());
2732 1.6 mrg
2733 1.6 mrg /* Perform satisfaction. */
2734 1.6 mrg return satisfy_constraint (norm, args, info);
2735 1.1 mrg }
2736 1.1 mrg
2737 1.6 mrg /* Used only to evaluate requires-expressions during constant expression
2738 1.6 mrg evaluation. */
2739 1.1 mrg
2740 1.1 mrg tree
2741 1.6 mrg satisfy_constraint_expression (tree expr)
2742 1.6 mrg {
2743 1.6 mrg subst_info info (tf_none, NULL_TREE);
2744 1.6 mrg return satisfy_constraint_expression (expr, NULL_TREE, info);
2745 1.6 mrg }
2746 1.6 mrg
2747 1.6 mrg static tree
2748 1.6 mrg satisfy_declaration_constraints (tree t, subst_info info)
2749 1.1 mrg {
2750 1.6 mrg gcc_assert (DECL_P (t));
2751 1.6 mrg const tree saved_t = t;
2752 1.6 mrg
2753 1.6 mrg /* For inherited constructors, consider the original declaration;
2754 1.6 mrg it has the correct template information attached. */
2755 1.6 mrg t = strip_inheriting_ctors (t);
2756 1.6 mrg tree inh_ctor_targs = NULL_TREE;
2757 1.6 mrg if (t != saved_t)
2758 1.6 mrg if (tree ti = DECL_TEMPLATE_INFO (saved_t))
2759 1.6 mrg /* The inherited constructor points to an instantiation of a constructor
2760 1.6 mrg template; remember its template arguments. */
2761 1.6 mrg inh_ctor_targs = TI_ARGS (ti);
2762 1.6 mrg
2763 1.6 mrg /* Update the declaration for diagnostics. */
2764 1.6 mrg info.in_decl = t;
2765 1.6 mrg
2766 1.6 mrg if (info.quiet ())
2767 1.6 mrg if (tree *result = hash_map_safe_get (decl_satisfied_cache, saved_t))
2768 1.6 mrg return *result;
2769 1.1 mrg
2770 1.6 mrg /* Get the normalized constraints. */
2771 1.6 mrg tree norm = NULL_TREE;
2772 1.6 mrg tree args = NULL_TREE;
2773 1.6 mrg if (tree ti = DECL_TEMPLATE_INFO (t))
2774 1.6 mrg {
2775 1.6 mrg tree tmpl = TI_TEMPLATE (ti);
2776 1.6 mrg norm = normalize_template_requirements (tmpl, info.noisy ());
2777 1.1 mrg
2778 1.6 mrg /* The initial parameter mapping is the complete set of
2779 1.6 mrg template arguments substituted into the declaration. */
2780 1.6 mrg args = TI_ARGS (ti);
2781 1.6 mrg if (inh_ctor_targs)
2782 1.6 mrg args = add_outermost_template_args (args, inh_ctor_targs);
2783 1.6 mrg }
2784 1.6 mrg else
2785 1.6 mrg {
2786 1.6 mrg /* These should be empty until we allow constraints on non-templates. */
2787 1.6 mrg norm = normalize_nontemplate_requirements (t, info.noisy ());
2788 1.6 mrg }
2789 1.1 mrg
2790 1.6 mrg tree result = boolean_true_node;
2791 1.6 mrg if (norm)
2792 1.6 mrg {
2793 1.6 mrg if (!push_tinst_level (t))
2794 1.6 mrg return result;
2795 1.6 mrg push_access_scope (t);
2796 1.6 mrg result = satisfy_associated_constraints (norm, args, info);
2797 1.6 mrg pop_access_scope (t);
2798 1.6 mrg pop_tinst_level ();
2799 1.6 mrg }
2800 1.1 mrg
2801 1.6 mrg if (info.quiet ())
2802 1.6 mrg hash_map_safe_put<hm_ggc> (decl_satisfied_cache, saved_t, result);
2803 1.1 mrg
2804 1.6 mrg return result;
2805 1.6 mrg }
2806 1.1 mrg
2807 1.6 mrg static tree
2808 1.6 mrg satisfy_declaration_constraints (tree t, tree args, subst_info info)
2809 1.1 mrg {
2810 1.6 mrg /* Update the declaration for diagnostics. */
2811 1.6 mrg info.in_decl = t;
2812 1.1 mrg
2813 1.6 mrg gcc_assert (TREE_CODE (t) == TEMPLATE_DECL);
2814 1.6 mrg if (tree norm = normalize_template_requirements (t, info.noisy ()))
2815 1.6 mrg {
2816 1.6 mrg tree pattern = DECL_TEMPLATE_RESULT (t);
2817 1.6 mrg push_access_scope (pattern);
2818 1.6 mrg tree result = satisfy_associated_constraints (norm, args, info);
2819 1.6 mrg pop_access_scope (pattern);
2820 1.6 mrg return result;
2821 1.6 mrg }
2822 1.1 mrg
2823 1.6 mrg return boolean_true_node;
2824 1.1 mrg }
2825 1.1 mrg
2826 1.6 mrg static tree
2827 1.6 mrg constraint_satisfaction_value (tree t, tsubst_flags_t complain)
2828 1.1 mrg {
2829 1.6 mrg subst_info info (complain, NULL_TREE);
2830 1.6 mrg tree r;
2831 1.6 mrg if (DECL_P (t))
2832 1.6 mrg r = satisfy_declaration_constraints (t, info);
2833 1.6 mrg else
2834 1.6 mrg r = satisfy_constraint_expression (t, NULL_TREE, info);
2835 1.6 mrg if (r == error_mark_node && info.quiet ()
2836 1.6 mrg && !(DECL_P (t) && TREE_NO_WARNING (t)))
2837 1.6 mrg {
2838 1.6 mrg constraint_satisfaction_value (t, tf_warning_or_error);
2839 1.6 mrg if (DECL_P (t))
2840 1.6 mrg /* Avoid giving these errors again. */
2841 1.6 mrg TREE_NO_WARNING (t) = true;
2842 1.6 mrg }
2843 1.6 mrg return r;
2844 1.1 mrg }
2845 1.1 mrg
2846 1.6 mrg static tree
2847 1.6 mrg constraint_satisfaction_value (tree t, tree args, tsubst_flags_t complain)
2848 1.1 mrg {
2849 1.6 mrg subst_info info (complain, NULL_TREE);
2850 1.6 mrg tree r;
2851 1.6 mrg if (DECL_P (t))
2852 1.6 mrg r = satisfy_declaration_constraints (t, args, info);
2853 1.6 mrg else
2854 1.6 mrg r = satisfy_constraint_expression (t, args, info);
2855 1.6 mrg if (r == error_mark_node && info.quiet ())
2856 1.6 mrg constraint_satisfaction_value (t, args, tf_warning_or_error);
2857 1.6 mrg return r;
2858 1.1 mrg }
2859 1.1 mrg
2860 1.6 mrg /* True iff the result of satisfying T is BOOLEAN_TRUE_NODE and false
2861 1.6 mrg otherwise, even in the case of errors. */
2862 1.1 mrg
2863 1.1 mrg bool
2864 1.6 mrg constraints_satisfied_p (tree t)
2865 1.1 mrg {
2866 1.6 mrg if (!flag_concepts)
2867 1.5 mrg return true;
2868 1.5 mrg
2869 1.6 mrg return constraint_satisfaction_value (t, tf_none) == boolean_true_node;
2870 1.1 mrg }
2871 1.1 mrg
2872 1.6 mrg /* True iff the result of satisfying T with ARGS is BOOLEAN_TRUE_NODE
2873 1.6 mrg and false otherwise, even in the case of errors. */
2874 1.1 mrg
2875 1.1 mrg bool
2876 1.1 mrg constraints_satisfied_p (tree t, tree args)
2877 1.1 mrg {
2878 1.6 mrg if (!flag_concepts)
2879 1.6 mrg return true;
2880 1.6 mrg
2881 1.6 mrg return constraint_satisfaction_value (t, args, tf_none) == boolean_true_node;
2882 1.1 mrg }
2883 1.1 mrg
2884 1.6 mrg /* Evaluate a concept check of the form C<ARGS>. This is only used for the
2885 1.6 mrg evaluation of template-ids as id-expressions. */
2886 1.6 mrg
2887 1.6 mrg tree
2888 1.6 mrg evaluate_concept_check (tree check, tsubst_flags_t complain)
2889 1.1 mrg {
2890 1.6 mrg if (check == error_mark_node)
2891 1.6 mrg return error_mark_node;
2892 1.1 mrg
2893 1.6 mrg gcc_assert (concept_check_p (check));
2894 1.1 mrg
2895 1.6 mrg /* Check for satisfaction without diagnostics. */
2896 1.6 mrg subst_info quiet (tf_none, NULL_TREE);
2897 1.6 mrg tree result = satisfy_constraint_expression (check, NULL_TREE, quiet);
2898 1.6 mrg if (result == error_mark_node && (complain & tf_error))
2899 1.6 mrg {
2900 1.6 mrg /* Replay the error with re-normalized requirements. */
2901 1.6 mrg subst_info noisy (tf_warning_or_error, NULL_TREE);
2902 1.6 mrg satisfy_constraint_expression (check, NULL_TREE, noisy);
2903 1.6 mrg }
2904 1.6 mrg return result;
2905 1.1 mrg }
2906 1.1 mrg
2907 1.1 mrg /*---------------------------------------------------------------------------
2908 1.1 mrg Semantic analysis of requires-expressions
2909 1.1 mrg ---------------------------------------------------------------------------*/
2910 1.1 mrg
2911 1.1 mrg /* Finish a requires expression for the given PARMS (possibly
2912 1.6 mrg null) and the non-empty sequence of requirements. */
2913 1.6 mrg
2914 1.1 mrg tree
2915 1.6 mrg finish_requires_expr (location_t loc, tree parms, tree reqs)
2916 1.1 mrg {
2917 1.1 mrg /* Modify the declared parameters by removing their context
2918 1.1 mrg so they don't refer to the enclosing scope and explicitly
2919 1.1 mrg indicating that they are constraint variables. */
2920 1.1 mrg for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
2921 1.1 mrg {
2922 1.1 mrg DECL_CONTEXT (parm) = NULL_TREE;
2923 1.1 mrg CONSTRAINT_VAR_P (parm) = true;
2924 1.1 mrg }
2925 1.1 mrg
2926 1.1 mrg /* Build the node. */
2927 1.1 mrg tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
2928 1.1 mrg TREE_SIDE_EFFECTS (r) = false;
2929 1.1 mrg TREE_CONSTANT (r) = true;
2930 1.6 mrg SET_EXPR_LOCATION (r, loc);
2931 1.1 mrg return r;
2932 1.1 mrg }
2933 1.1 mrg
2934 1.6 mrg /* Construct a requirement for the validity of EXPR. */
2935 1.6 mrg
2936 1.1 mrg tree
2937 1.6 mrg finish_simple_requirement (location_t loc, tree expr)
2938 1.1 mrg {
2939 1.6 mrg tree r = build_nt (SIMPLE_REQ, expr);
2940 1.6 mrg SET_EXPR_LOCATION (r, loc);
2941 1.6 mrg return r;
2942 1.1 mrg }
2943 1.1 mrg
2944 1.6 mrg /* Construct a requirement for the validity of TYPE. */
2945 1.6 mrg
2946 1.1 mrg tree
2947 1.6 mrg finish_type_requirement (location_t loc, tree type)
2948 1.1 mrg {
2949 1.6 mrg tree r = build_nt (TYPE_REQ, type);
2950 1.6 mrg SET_EXPR_LOCATION (r, loc);
2951 1.6 mrg return r;
2952 1.1 mrg }
2953 1.1 mrg
2954 1.1 mrg /* Construct a requirement for the validity of EXPR, along with
2955 1.1 mrg its properties. if TYPE is non-null, then it specifies either
2956 1.1 mrg an implicit conversion or argument deduction constraint,
2957 1.1 mrg depending on whether any placeholders occur in the type name.
2958 1.6 mrg NOEXCEPT_P is true iff the noexcept keyword was specified. */
2959 1.6 mrg
2960 1.1 mrg tree
2961 1.6 mrg finish_compound_requirement (location_t loc, tree expr, tree type, bool noexcept_p)
2962 1.1 mrg {
2963 1.1 mrg tree req = build_nt (COMPOUND_REQ, expr, type);
2964 1.6 mrg SET_EXPR_LOCATION (req, loc);
2965 1.1 mrg COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2966 1.1 mrg return req;
2967 1.1 mrg }
2968 1.1 mrg
2969 1.6 mrg /* Finish a nested requirement. */
2970 1.6 mrg
2971 1.1 mrg tree
2972 1.6 mrg finish_nested_requirement (location_t loc, tree expr)
2973 1.1 mrg {
2974 1.6 mrg /* Currently open template headers have dummy arg vectors, so don't
2975 1.6 mrg pass into normalization. */
2976 1.6 mrg tree norm = normalize_constraint_expression (expr, NULL_TREE, false);
2977 1.6 mrg tree args = current_template_parms
2978 1.6 mrg ? template_parms_to_args (current_template_parms) : NULL_TREE;
2979 1.6 mrg
2980 1.6 mrg /* Save the normalized constraint and complete set of normalization
2981 1.6 mrg arguments with the requirement. We keep the complete set of arguments
2982 1.6 mrg around for re-normalization during diagnostics. */
2983 1.6 mrg tree info = build_tree_list (args, norm);
2984 1.6 mrg
2985 1.6 mrg /* Build the constraint, saving its normalization as its type. */
2986 1.6 mrg tree r = build1 (NESTED_REQ, info, expr);
2987 1.6 mrg SET_EXPR_LOCATION (r, loc);
2988 1.6 mrg return r;
2989 1.1 mrg }
2990 1.1 mrg
2991 1.6 mrg /* Check that FN satisfies the structural requirements of a
2992 1.6 mrg function concept definition. */
2993 1.1 mrg tree
2994 1.1 mrg check_function_concept (tree fn)
2995 1.1 mrg {
2996 1.6 mrg /* Check that the function is comprised of only a return statement. */
2997 1.1 mrg tree body = DECL_SAVED_TREE (fn);
2998 1.1 mrg if (TREE_CODE (body) == BIND_EXPR)
2999 1.1 mrg body = BIND_EXPR_BODY (body);
3000 1.1 mrg
3001 1.6 mrg /* Sometimes a function call results in the creation of clean up
3002 1.6 mrg points. Allow these to be preserved in the body of the
3003 1.6 mrg constraint, as we might actually need them for some constexpr
3004 1.6 mrg evaluations. */
3005 1.1 mrg if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
3006 1.1 mrg body = TREE_OPERAND (body, 0);
3007 1.1 mrg
3008 1.6 mrg /* Check that the definition is written correctly. */
3009 1.1 mrg if (TREE_CODE (body) != RETURN_EXPR)
3010 1.1 mrg {
3011 1.1 mrg location_t loc = DECL_SOURCE_LOCATION (fn);
3012 1.1 mrg if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
3013 1.4 mrg {
3014 1.4 mrg if (seen_error ())
3015 1.4 mrg /* The definition was probably erroneous, not empty. */;
3016 1.4 mrg else
3017 1.4 mrg error_at (loc, "definition of concept %qD is empty", fn);
3018 1.4 mrg }
3019 1.1 mrg else
3020 1.1 mrg error_at (loc, "definition of concept %qD has multiple statements", fn);
3021 1.1 mrg }
3022 1.1 mrg
3023 1.1 mrg return NULL_TREE;
3024 1.1 mrg }
3025 1.1 mrg
3026 1.1 mrg
3027 1.1 mrg // Check that a constrained friend declaration function declaration,
3028 1.1 mrg // FN, is admissible. This is the case only when the declaration depends
3029 1.1 mrg // on template parameters and does not declare a specialization.
3030 1.1 mrg void
3031 1.1 mrg check_constrained_friend (tree fn, tree reqs)
3032 1.1 mrg {
3033 1.1 mrg if (fn == error_mark_node)
3034 1.1 mrg return;
3035 1.1 mrg gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
3036 1.1 mrg
3037 1.1 mrg // If there are not constraints, this cannot be an error.
3038 1.1 mrg if (!reqs)
3039 1.1 mrg return;
3040 1.1 mrg
3041 1.1 mrg // Constrained friend functions that don't depend on template
3042 1.1 mrg // arguments are effectively meaningless.
3043 1.1 mrg if (!uses_template_parms (TREE_TYPE (fn)))
3044 1.1 mrg {
3045 1.1 mrg error_at (location_of (fn),
3046 1.1 mrg "constrained friend does not depend on template parameters");
3047 1.1 mrg return;
3048 1.1 mrg }
3049 1.1 mrg }
3050 1.1 mrg
3051 1.1 mrg /*---------------------------------------------------------------------------
3052 1.1 mrg Equivalence of constraints
3053 1.1 mrg ---------------------------------------------------------------------------*/
3054 1.1 mrg
3055 1.1 mrg /* Returns true when A and B are equivalent constraints. */
3056 1.1 mrg bool
3057 1.1 mrg equivalent_constraints (tree a, tree b)
3058 1.1 mrg {
3059 1.1 mrg gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
3060 1.1 mrg gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
3061 1.1 mrg return cp_tree_equal (a, b);
3062 1.1 mrg }
3063 1.1 mrg
3064 1.1 mrg /* Returns true if the template declarations A and B have equivalent
3065 1.1 mrg constraints. This is the case when A's constraints subsume B's and
3066 1.1 mrg when B's also constrain A's. */
3067 1.1 mrg bool
3068 1.1 mrg equivalently_constrained (tree d1, tree d2)
3069 1.1 mrg {
3070 1.1 mrg gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
3071 1.1 mrg return equivalent_constraints (get_constraints (d1), get_constraints (d2));
3072 1.1 mrg }
3073 1.1 mrg
3074 1.1 mrg /*---------------------------------------------------------------------------
3075 1.1 mrg Partial ordering of constraints
3076 1.1 mrg ---------------------------------------------------------------------------*/
3077 1.1 mrg
3078 1.6 mrg /* Returns true when the constraints in A subsume those in B. */
3079 1.1 mrg
3080 1.1 mrg bool
3081 1.1 mrg subsumes_constraints (tree a, tree b)
3082 1.1 mrg {
3083 1.1 mrg gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
3084 1.1 mrg gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
3085 1.1 mrg return subsumes (a, b);
3086 1.1 mrg }
3087 1.1 mrg
3088 1.6 mrg /* Returns true when the constraints in CI (with arguments
3089 1.6 mrg ARGS) strictly subsume the associated constraints of TMPL. */
3090 1.6 mrg
3091 1.6 mrg bool
3092 1.6 mrg strictly_subsumes (tree ci, tree args, tree tmpl)
3093 1.6 mrg {
3094 1.6 mrg tree n1 = get_normalized_constraints_from_info (ci, args, NULL_TREE);
3095 1.6 mrg tree n2 = get_normalized_constraints_from_decl (tmpl);
3096 1.6 mrg
3097 1.6 mrg return subsumes (n1, n2) && !subsumes (n2, n1);
3098 1.6 mrg }
3099 1.6 mrg
3100 1.6 mrg /* REturns true when the constraints in CI (with arguments ARGS) subsume
3101 1.6 mrg the associated constraints of TMPL. */
3102 1.1 mrg
3103 1.1 mrg bool
3104 1.6 mrg weakly_subsumes (tree ci, tree args, tree tmpl)
3105 1.1 mrg {
3106 1.6 mrg tree n1 = get_normalized_constraints_from_info (ci, args, NULL_TREE);
3107 1.6 mrg tree n2 = get_normalized_constraints_from_decl (tmpl);
3108 1.6 mrg
3109 1.6 mrg return subsumes (n1, n2);
3110 1.1 mrg }
3111 1.1 mrg
3112 1.1 mrg /* Determines which of the declarations, A or B, is more constrained.
3113 1.1 mrg That is, which declaration's constraints subsume but are not subsumed
3114 1.1 mrg by the other's?
3115 1.1 mrg
3116 1.6 mrg Returns 1 if D1 is more constrained than D2, -1 if D2 is more constrained
3117 1.6 mrg than D1, and 0 otherwise. */
3118 1.1 mrg
3119 1.1 mrg int
3120 1.1 mrg more_constrained (tree d1, tree d2)
3121 1.1 mrg {
3122 1.6 mrg tree n1 = get_normalized_constraints_from_decl (d1);
3123 1.6 mrg tree n2 = get_normalized_constraints_from_decl (d2);
3124 1.6 mrg
3125 1.1 mrg int winner = 0;
3126 1.6 mrg if (subsumes (n1, n2))
3127 1.1 mrg ++winner;
3128 1.6 mrg if (subsumes (n2, n1))
3129 1.1 mrg --winner;
3130 1.1 mrg return winner;
3131 1.1 mrg }
3132 1.1 mrg
3133 1.6 mrg /* Return whether D1 is at least as constrained as D2. */
3134 1.1 mrg
3135 1.1 mrg bool
3136 1.1 mrg at_least_as_constrained (tree d1, tree d2)
3137 1.1 mrg {
3138 1.6 mrg tree n1 = get_normalized_constraints_from_decl (d1);
3139 1.6 mrg tree n2 = get_normalized_constraints_from_decl (d2);
3140 1.6 mrg
3141 1.6 mrg return subsumes (n1, n2);
3142 1.1 mrg }
3143 1.1 mrg
3144 1.1 mrg /*---------------------------------------------------------------------------
3145 1.1 mrg Constraint diagnostics
3146 1.1 mrg ---------------------------------------------------------------------------*/
3147 1.1 mrg
3148 1.6 mrg /* Returns the best location to diagnose a constraint error. */
3149 1.1 mrg
3150 1.6 mrg static location_t
3151 1.6 mrg get_constraint_error_location (tree t)
3152 1.1 mrg {
3153 1.6 mrg if (location_t loc = cp_expr_location (t))
3154 1.6 mrg return loc;
3155 1.1 mrg
3156 1.6 mrg /* If we have a specific location give it. */
3157 1.6 mrg tree expr = CONSTR_EXPR (t);
3158 1.6 mrg if (location_t loc = cp_expr_location (expr))
3159 1.6 mrg return loc;
3160 1.6 mrg
3161 1.6 mrg /* If the constraint is normalized from a requires-clause, give
3162 1.6 mrg the location as that of the constrained declaration. */
3163 1.6 mrg tree cxt = CONSTR_CONTEXT (t);
3164 1.6 mrg tree src = cxt ? TREE_VALUE (cxt) : NULL_TREE;
3165 1.6 mrg if (!src)
3166 1.6 mrg /* TODO: This only happens for constrained non-template declarations. */
3167 1.6 mrg ;
3168 1.6 mrg else if (DECL_P (src))
3169 1.6 mrg return DECL_SOURCE_LOCATION (src);
3170 1.6 mrg /* Otherwise, give the location as the defining concept. */
3171 1.6 mrg else if (concept_check_p (src))
3172 1.6 mrg {
3173 1.6 mrg tree id = unpack_concept_check (src);
3174 1.6 mrg tree tmpl = TREE_OPERAND (id, 0);
3175 1.6 mrg if (OVL_P (tmpl))
3176 1.6 mrg tmpl = OVL_FIRST (tmpl);
3177 1.6 mrg return DECL_SOURCE_LOCATION (tmpl);
3178 1.6 mrg }
3179 1.1 mrg
3180 1.6 mrg return input_location;
3181 1.1 mrg }
3182 1.1 mrg
3183 1.6 mrg /* Emit a diagnostic for a failed trait. */
3184 1.1 mrg
3185 1.1 mrg void
3186 1.6 mrg diagnose_trait_expr (tree expr, tree map)
3187 1.1 mrg {
3188 1.6 mrg location_t loc = cp_expr_location (expr);
3189 1.6 mrg tree args = get_mapped_args (map);
3190 1.1 mrg
3191 1.6 mrg /* Build a "fake" version of the instantiated trait, so we can
3192 1.6 mrg get the instantiated types from result. */
3193 1.1 mrg ++processing_template_decl;
3194 1.1 mrg expr = tsubst_expr (expr, args, tf_none, NULL_TREE, false);
3195 1.1 mrg --processing_template_decl;
3196 1.1 mrg
3197 1.1 mrg tree t1 = TRAIT_EXPR_TYPE1 (expr);
3198 1.1 mrg tree t2 = TRAIT_EXPR_TYPE2 (expr);
3199 1.1 mrg switch (TRAIT_EXPR_KIND (expr))
3200 1.1 mrg {
3201 1.1 mrg case CPTK_HAS_NOTHROW_ASSIGN:
3202 1.6 mrg inform (loc, " %qT is not %<nothrow%> copy assignable", t1);
3203 1.1 mrg break;
3204 1.1 mrg case CPTK_HAS_NOTHROW_CONSTRUCTOR:
3205 1.6 mrg inform (loc, " %qT is not %<nothrow%> default constructible", t1);
3206 1.1 mrg break;
3207 1.1 mrg case CPTK_HAS_NOTHROW_COPY:
3208 1.6 mrg inform (loc, " %qT is not %<nothrow%> copy constructible", t1);
3209 1.1 mrg break;
3210 1.1 mrg case CPTK_HAS_TRIVIAL_ASSIGN:
3211 1.1 mrg inform (loc, " %qT is not trivially copy assignable", t1);
3212 1.1 mrg break;
3213 1.1 mrg case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
3214 1.1 mrg inform (loc, " %qT is not trivially default constructible", t1);
3215 1.1 mrg break;
3216 1.1 mrg case CPTK_HAS_TRIVIAL_COPY:
3217 1.1 mrg inform (loc, " %qT is not trivially copy constructible", t1);
3218 1.1 mrg break;
3219 1.1 mrg case CPTK_HAS_TRIVIAL_DESTRUCTOR:
3220 1.1 mrg inform (loc, " %qT is not trivially destructible", t1);
3221 1.1 mrg break;
3222 1.1 mrg case CPTK_HAS_VIRTUAL_DESTRUCTOR:
3223 1.1 mrg inform (loc, " %qT does not have a virtual destructor", t1);
3224 1.1 mrg break;
3225 1.1 mrg case CPTK_IS_ABSTRACT:
3226 1.1 mrg inform (loc, " %qT is not an abstract class", t1);
3227 1.1 mrg break;
3228 1.1 mrg case CPTK_IS_BASE_OF:
3229 1.1 mrg inform (loc, " %qT is not a base of %qT", t1, t2);
3230 1.1 mrg break;
3231 1.1 mrg case CPTK_IS_CLASS:
3232 1.1 mrg inform (loc, " %qT is not a class", t1);
3233 1.1 mrg break;
3234 1.1 mrg case CPTK_IS_EMPTY:
3235 1.1 mrg inform (loc, " %qT is not an empty class", t1);
3236 1.1 mrg break;
3237 1.1 mrg case CPTK_IS_ENUM:
3238 1.1 mrg inform (loc, " %qT is not an enum", t1);
3239 1.1 mrg break;
3240 1.1 mrg case CPTK_IS_FINAL:
3241 1.1 mrg inform (loc, " %qT is not a final class", t1);
3242 1.1 mrg break;
3243 1.1 mrg case CPTK_IS_LITERAL_TYPE:
3244 1.1 mrg inform (loc, " %qT is not a literal type", t1);
3245 1.1 mrg break;
3246 1.1 mrg case CPTK_IS_POD:
3247 1.1 mrg inform (loc, " %qT is not a POD type", t1);
3248 1.1 mrg break;
3249 1.1 mrg case CPTK_IS_POLYMORPHIC:
3250 1.1 mrg inform (loc, " %qT is not a polymorphic type", t1);
3251 1.1 mrg break;
3252 1.1 mrg case CPTK_IS_SAME_AS:
3253 1.1 mrg inform (loc, " %qT is not the same as %qT", t1, t2);
3254 1.1 mrg break;
3255 1.1 mrg case CPTK_IS_STD_LAYOUT:
3256 1.1 mrg inform (loc, " %qT is not an standard layout type", t1);
3257 1.1 mrg break;
3258 1.1 mrg case CPTK_IS_TRIVIAL:
3259 1.1 mrg inform (loc, " %qT is not a trivial type", t1);
3260 1.1 mrg break;
3261 1.1 mrg case CPTK_IS_UNION:
3262 1.1 mrg inform (loc, " %qT is not a union", t1);
3263 1.1 mrg break;
3264 1.1 mrg default:
3265 1.1 mrg gcc_unreachable ();
3266 1.1 mrg }
3267 1.1 mrg }
3268 1.1 mrg
3269 1.6 mrg static tree
3270 1.6 mrg diagnose_valid_expression (tree expr, tree args, tree in_decl)
3271 1.1 mrg {
3272 1.6 mrg tree result = tsubst_expr (expr, args, tf_none, in_decl, false);
3273 1.6 mrg if (result != error_mark_node
3274 1.6 mrg && convert_to_void (result, ICV_STATEMENT, tf_none) != error_mark_node)
3275 1.6 mrg return result;
3276 1.6 mrg
3277 1.6 mrg location_t loc = cp_expr_loc_or_input_loc (expr);
3278 1.6 mrg if (diagnosing_failed_constraint::replay_errors_p ())
3279 1.6 mrg {
3280 1.6 mrg /* Replay the substitution error. */
3281 1.6 mrg inform (loc, "the required expression %qE is invalid, because", expr);
3282 1.6 mrg if (result == error_mark_node)
3283 1.6 mrg tsubst_expr (expr, args, tf_error, in_decl, false);
3284 1.6 mrg else
3285 1.6 mrg convert_to_void (result, ICV_STATEMENT, tf_error);
3286 1.6 mrg }
3287 1.6 mrg else
3288 1.6 mrg inform (loc, "the required expression %qE is invalid", expr);
3289 1.1 mrg
3290 1.6 mrg return error_mark_node;
3291 1.1 mrg }
3292 1.1 mrg
3293 1.6 mrg static tree
3294 1.6 mrg diagnose_valid_type (tree type, tree args, tree in_decl)
3295 1.1 mrg {
3296 1.6 mrg tree result = tsubst (type, args, tf_none, in_decl);
3297 1.6 mrg if (result != error_mark_node)
3298 1.6 mrg return result;
3299 1.1 mrg
3300 1.6 mrg location_t loc = cp_expr_loc_or_input_loc (type);
3301 1.6 mrg if (diagnosing_failed_constraint::replay_errors_p ())
3302 1.1 mrg {
3303 1.6 mrg /* Replay the substitution error. */
3304 1.6 mrg inform (loc, "the required type %qT is invalid, because", type);
3305 1.6 mrg tsubst (type, args, tf_error, in_decl);
3306 1.1 mrg }
3307 1.6 mrg else
3308 1.6 mrg inform (loc, "the required type %qT is invalid", type);
3309 1.1 mrg
3310 1.6 mrg return error_mark_node;
3311 1.1 mrg }
3312 1.1 mrg
3313 1.6 mrg static void
3314 1.6 mrg diagnose_simple_requirement (tree req, tree args, tree in_decl)
3315 1.1 mrg {
3316 1.6 mrg diagnose_valid_expression (TREE_OPERAND (req, 0), args, in_decl);
3317 1.1 mrg }
3318 1.1 mrg
3319 1.6 mrg static void
3320 1.6 mrg diagnose_compound_requirement (tree req, tree args, tree in_decl)
3321 1.1 mrg {
3322 1.6 mrg tree expr = TREE_OPERAND (req, 0);
3323 1.6 mrg expr = diagnose_valid_expression (expr, args, in_decl);
3324 1.6 mrg if (expr == error_mark_node)
3325 1.1 mrg return;
3326 1.1 mrg
3327 1.6 mrg location_t loc = cp_expr_loc_or_input_loc (expr);
3328 1.6 mrg
3329 1.6 mrg /* Check the noexcept condition. */
3330 1.6 mrg if (COMPOUND_REQ_NOEXCEPT_P (req) && !expr_noexcept_p (expr, tf_none))
3331 1.6 mrg inform (loc, "%qE is not %<noexcept%>", expr);
3332 1.1 mrg
3333 1.6 mrg tree type = TREE_OPERAND (req, 1);
3334 1.6 mrg type = diagnose_valid_type (type, args, in_decl);
3335 1.6 mrg if (type == error_mark_node)
3336 1.1 mrg return;
3337 1.1 mrg
3338 1.6 mrg if (type)
3339 1.6 mrg {
3340 1.6 mrg subst_info quiet (tf_none, in_decl);
3341 1.6 mrg subst_info noisy (tf_error, in_decl);
3342 1.1 mrg
3343 1.6 mrg /* Check the expression against the result type. */
3344 1.6 mrg if (tree placeholder = type_uses_auto (type))
3345 1.6 mrg {
3346 1.6 mrg if (!type_deducible_p (expr, type, placeholder, args, quiet))
3347 1.6 mrg {
3348 1.6 mrg tree orig_expr = TREE_OPERAND (req, 0);
3349 1.6 mrg if (diagnosing_failed_constraint::replay_errors_p ())
3350 1.6 mrg {
3351 1.6 mrg inform (loc,
3352 1.6 mrg "%qE does not satisfy return-type-requirement, "
3353 1.6 mrg "because", orig_expr);
3354 1.6 mrg /* Further explain the reason for the error. */
3355 1.6 mrg type_deducible_p (expr, type, placeholder, args, noisy);
3356 1.6 mrg }
3357 1.6 mrg else
3358 1.6 mrg inform (loc, "%qE does not satisfy return-type-requirement",
3359 1.6 mrg orig_expr);
3360 1.6 mrg }
3361 1.6 mrg }
3362 1.6 mrg else if (!expression_convertible_p (expr, type, quiet))
3363 1.6 mrg {
3364 1.6 mrg tree orig_expr = TREE_OPERAND (req, 0);
3365 1.6 mrg if (diagnosing_failed_constraint::replay_errors_p ())
3366 1.6 mrg {
3367 1.6 mrg inform (loc, "cannot convert %qE to %qT because", orig_expr, type);
3368 1.6 mrg /* Further explain the reason for the error. */
3369 1.6 mrg expression_convertible_p (expr, type, noisy);
3370 1.6 mrg }
3371 1.6 mrg else
3372 1.6 mrg inform (loc, "cannot convert %qE to %qT", orig_expr, type);
3373 1.6 mrg }
3374 1.6 mrg }
3375 1.1 mrg }
3376 1.1 mrg
3377 1.6 mrg static void
3378 1.6 mrg diagnose_type_requirement (tree req, tree args, tree in_decl)
3379 1.1 mrg {
3380 1.6 mrg tree type = TREE_OPERAND (req, 0);
3381 1.6 mrg diagnose_valid_type (type, args, in_decl);
3382 1.1 mrg }
3383 1.1 mrg
3384 1.6 mrg static void
3385 1.6 mrg diagnose_nested_requirement (tree req, tree args)
3386 1.1 mrg {
3387 1.6 mrg /* Quietly check for satisfaction first. We can elaborate details
3388 1.6 mrg later if needed. */
3389 1.6 mrg tree norm = TREE_VALUE (TREE_TYPE (req));
3390 1.6 mrg subst_info info (tf_none, NULL_TREE);
3391 1.6 mrg tree result = satisfy_constraint (norm, args, info);
3392 1.6 mrg if (result == boolean_true_node)
3393 1.1 mrg return;
3394 1.1 mrg
3395 1.6 mrg tree expr = TREE_OPERAND (req, 0);
3396 1.6 mrg location_t loc = cp_expr_location (expr);
3397 1.6 mrg if (diagnosing_failed_constraint::replay_errors_p ())
3398 1.1 mrg {
3399 1.6 mrg /* Replay the substitution error. */
3400 1.6 mrg inform (loc, "nested requirement %qE is not satisfied, because", expr);
3401 1.6 mrg subst_info noisy (tf_warning_or_error, NULL_TREE);
3402 1.6 mrg satisfy_constraint_expression (expr, args, noisy);
3403 1.1 mrg }
3404 1.6 mrg else
3405 1.6 mrg inform (loc, "nested requirement %qE is not satisfied", expr);
3406 1.1 mrg
3407 1.1 mrg }
3408 1.1 mrg
3409 1.6 mrg static void
3410 1.6 mrg diagnose_requirement (tree req, tree args, tree in_decl)
3411 1.1 mrg {
3412 1.6 mrg iloc_sentinel loc_s (cp_expr_location (req));
3413 1.6 mrg switch (TREE_CODE (req))
3414 1.1 mrg {
3415 1.6 mrg case SIMPLE_REQ:
3416 1.6 mrg return diagnose_simple_requirement (req, args, in_decl);
3417 1.6 mrg case COMPOUND_REQ:
3418 1.6 mrg return diagnose_compound_requirement (req, args, in_decl);
3419 1.6 mrg case TYPE_REQ:
3420 1.6 mrg return diagnose_type_requirement (req, args, in_decl);
3421 1.6 mrg case NESTED_REQ:
3422 1.6 mrg return diagnose_nested_requirement (req, args);
3423 1.6 mrg default:
3424 1.6 mrg gcc_unreachable ();
3425 1.1 mrg }
3426 1.1 mrg }
3427 1.1 mrg
3428 1.6 mrg static void
3429 1.6 mrg diagnose_requires_expr (tree expr, tree map, tree in_decl)
3430 1.1 mrg {
3431 1.6 mrg local_specialization_stack stack (lss_copy);
3432 1.6 mrg tree parms = TREE_OPERAND (expr, 0);
3433 1.6 mrg tree body = TREE_OPERAND (expr, 1);
3434 1.6 mrg tree args = get_mapped_args (map);
3435 1.1 mrg
3436 1.6 mrg cp_unevaluated u;
3437 1.6 mrg subst_info info (tf_warning_or_error, NULL_TREE);
3438 1.6 mrg tree vars = tsubst_constraint_variables (parms, args, info);
3439 1.6 mrg if (vars == error_mark_node)
3440 1.1 mrg return;
3441 1.1 mrg
3442 1.6 mrg tree p = body;
3443 1.6 mrg while (p)
3444 1.6 mrg {
3445 1.6 mrg tree req = TREE_VALUE (p);
3446 1.6 mrg diagnose_requirement (req, args, in_decl);
3447 1.6 mrg p = TREE_CHAIN (p);
3448 1.6 mrg }
3449 1.1 mrg }
3450 1.1 mrg
3451 1.6 mrg /* Diagnose a substitution failure in the atomic constraint T when applied
3452 1.6 mrg with the instantiated parameter mapping MAP. */
3453 1.1 mrg
3454 1.6 mrg static void
3455 1.6 mrg diagnose_atomic_constraint (tree t, tree map, tree result, subst_info info)
3456 1.1 mrg {
3457 1.6 mrg /* If the constraint is already ill-formed, we've previously diagnosed
3458 1.6 mrg the reason. We should still say why the constraints aren't satisfied. */
3459 1.6 mrg if (t == error_mark_node)
3460 1.1 mrg {
3461 1.6 mrg location_t loc;
3462 1.6 mrg if (info.in_decl)
3463 1.6 mrg loc = DECL_SOURCE_LOCATION (info.in_decl);
3464 1.6 mrg else
3465 1.6 mrg loc = input_location;
3466 1.6 mrg inform (loc, "invalid constraints");
3467 1.1 mrg return;
3468 1.1 mrg }
3469 1.1 mrg
3470 1.6 mrg location_t loc = get_constraint_error_location (t);
3471 1.6 mrg iloc_sentinel loc_s (loc);
3472 1.1 mrg
3473 1.6 mrg /* Generate better diagnostics for certain kinds of expressions. */
3474 1.6 mrg tree expr = ATOMIC_CONSTR_EXPR (t);
3475 1.6 mrg STRIP_ANY_LOCATION_WRAPPER (expr);
3476 1.6 mrg switch (TREE_CODE (expr))
3477 1.1 mrg {
3478 1.6 mrg case TRAIT_EXPR:
3479 1.6 mrg diagnose_trait_expr (expr, map);
3480 1.1 mrg break;
3481 1.6 mrg case REQUIRES_EXPR:
3482 1.6 mrg diagnose_requires_expr (expr, map, info.in_decl);
3483 1.1 mrg break;
3484 1.1 mrg default:
3485 1.6 mrg tree a = copy_node (t);
3486 1.6 mrg ATOMIC_CONSTR_MAP (a) = map;
3487 1.6 mrg if (!same_type_p (TREE_TYPE (result), boolean_type_node))
3488 1.6 mrg error_at (loc, "constraint %qE has type %qT, not %<bool%>",
3489 1.6 mrg a, TREE_TYPE (result));
3490 1.6 mrg else
3491 1.6 mrg inform (loc, "the expression %qE evaluated to %<false%>", a);
3492 1.6 mrg ggc_free (a);
3493 1.1 mrg }
3494 1.1 mrg }
3495 1.1 mrg
3496 1.6 mrg GTY(()) tree current_failed_constraint;
3497 1.1 mrg
3498 1.6 mrg diagnosing_failed_constraint::
3499 1.6 mrg diagnosing_failed_constraint (tree t, tree args, bool diag)
3500 1.6 mrg : diagnosing_error (diag)
3501 1.1 mrg {
3502 1.6 mrg if (diagnosing_error)
3503 1.6 mrg {
3504 1.6 mrg current_failed_constraint
3505 1.6 mrg = tree_cons (args, t, current_failed_constraint);
3506 1.6 mrg ++current_constraint_diagnosis_depth;
3507 1.6 mrg }
3508 1.6 mrg }
3509 1.1 mrg
3510 1.6 mrg diagnosing_failed_constraint::
3511 1.6 mrg ~diagnosing_failed_constraint ()
3512 1.6 mrg {
3513 1.6 mrg if (diagnosing_error)
3514 1.1 mrg {
3515 1.6 mrg --current_constraint_diagnosis_depth;
3516 1.6 mrg if (current_failed_constraint)
3517 1.6 mrg current_failed_constraint = TREE_CHAIN (current_failed_constraint);
3518 1.1 mrg }
3519 1.1 mrg
3520 1.1 mrg }
3521 1.1 mrg
3522 1.6 mrg /* Whether we are allowed to replay an error that underlies a constraint failure
3523 1.6 mrg at the current diagnosis depth. */
3524 1.6 mrg
3525 1.6 mrg bool
3526 1.6 mrg diagnosing_failed_constraint::replay_errors_p ()
3527 1.6 mrg {
3528 1.6 mrg if (current_constraint_diagnosis_depth >= concepts_diagnostics_max_depth)
3529 1.6 mrg {
3530 1.6 mrg concepts_diagnostics_max_depth_exceeded_p = true;
3531 1.6 mrg return false;
3532 1.6 mrg }
3533 1.6 mrg else
3534 1.6 mrg return true;
3535 1.6 mrg }
3536 1.1 mrg
3537 1.6 mrg /* Emit diagnostics detailing the failure ARGS to satisfy the constraints
3538 1.6 mrg of T. Here, T can be either a constraint or a declaration. */
3539 1.1 mrg
3540 1.1 mrg void
3541 1.1 mrg diagnose_constraints (location_t loc, tree t, tree args)
3542 1.1 mrg {
3543 1.6 mrg inform (loc, "constraints not satisfied");
3544 1.6 mrg
3545 1.6 mrg if (concepts_diagnostics_max_depth == 0)
3546 1.6 mrg return;
3547 1.1 mrg
3548 1.6 mrg /* Replay satisfaction, but diagnose errors. */
3549 1.6 mrg if (!args)
3550 1.6 mrg constraint_satisfaction_value (t, tf_warning_or_error);
3551 1.1 mrg else
3552 1.6 mrg constraint_satisfaction_value (t, args, tf_warning_or_error);
3553 1.1 mrg
3554 1.6 mrg static bool suggested_p;
3555 1.6 mrg if (concepts_diagnostics_max_depth_exceeded_p
3556 1.6 mrg && current_constraint_diagnosis_depth == 0
3557 1.6 mrg && !suggested_p)
3558 1.6 mrg {
3559 1.6 mrg inform (UNKNOWN_LOCATION,
3560 1.6 mrg "set %qs to at least %d for more detail",
3561 1.6 mrg "-fconcepts-diagnostics-depth=",
3562 1.6 mrg concepts_diagnostics_max_depth + 1);
3563 1.6 mrg suggested_p = true;
3564 1.6 mrg }
3565 1.1 mrg }
3566 1.6 mrg
3567 1.6 mrg #include "gt-cp-constraint.h"
3568