constraint.cc revision 1.1 1 1.1 mrg /* Processing rules for constraints.
2 1.1 mrg Copyright (C) 2013-2016 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.1 mrg /*---------------------------------------------------------------------------
50 1.1 mrg Operations on constraints
51 1.1 mrg ---------------------------------------------------------------------------*/
52 1.1 mrg
53 1.1 mrg /* Returns true if C is a constraint tree code. Note that ERROR_MARK
54 1.1 mrg is a valid constraint. */
55 1.1 mrg
56 1.1 mrg static inline bool
57 1.1 mrg constraint_p (tree_code c)
58 1.1 mrg {
59 1.1 mrg return ((PRED_CONSTR <= c && c <= DISJ_CONSTR)
60 1.1 mrg || c == EXPR_PACK_EXPANSION
61 1.1 mrg || c == ERROR_MARK);
62 1.1 mrg }
63 1.1 mrg
64 1.1 mrg /* Returns true if T is a constraint. Note that error_mark_node
65 1.1 mrg is a valid constraint. */
66 1.1 mrg
67 1.1 mrg bool
68 1.1 mrg constraint_p (tree t)
69 1.1 mrg {
70 1.1 mrg return constraint_p (TREE_CODE (t));
71 1.1 mrg }
72 1.1 mrg
73 1.1 mrg /* Returns the conjunction of two constraints A and B. Note that
74 1.1 mrg conjoining a non-null constraint with NULL_TREE is an identity
75 1.1 mrg operation. That is, for non-null A,
76 1.1 mrg
77 1.1 mrg conjoin_constraints(a, NULL_TREE) == a
78 1.1 mrg
79 1.1 mrg and
80 1.1 mrg
81 1.1 mrg conjoin_constraints (NULL_TREE, a) == a
82 1.1 mrg
83 1.1 mrg If both A and B are NULL_TREE, the result is also NULL_TREE. */
84 1.1 mrg
85 1.1 mrg tree
86 1.1 mrg conjoin_constraints (tree a, tree b)
87 1.1 mrg {
88 1.1 mrg gcc_assert (a ? constraint_p (a) : true);
89 1.1 mrg gcc_assert (b ? constraint_p (b) : true);
90 1.1 mrg if (a)
91 1.1 mrg return b ? build_nt (CONJ_CONSTR, a, b) : a;
92 1.1 mrg else if (b)
93 1.1 mrg return b;
94 1.1 mrg else
95 1.1 mrg return NULL_TREE;
96 1.1 mrg }
97 1.1 mrg
98 1.1 mrg /* Transform the vector of expressions in the T into a conjunction
99 1.1 mrg of requirements. T must be a TREE_VEC. */
100 1.1 mrg
101 1.1 mrg tree
102 1.1 mrg conjoin_constraints (tree t)
103 1.1 mrg {
104 1.1 mrg gcc_assert (TREE_CODE (t) == TREE_VEC);
105 1.1 mrg tree r = NULL_TREE;
106 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
107 1.1 mrg r = conjoin_constraints (r, TREE_VEC_ELT (t, i));
108 1.1 mrg return r;
109 1.1 mrg }
110 1.1 mrg
111 1.1 mrg /* Returns true if T is a call expression to a function
112 1.1 mrg concept. */
113 1.1 mrg
114 1.1 mrg bool
115 1.1 mrg function_concept_check_p (tree t)
116 1.1 mrg {
117 1.1 mrg gcc_assert (TREE_CODE (t) == CALL_EXPR);
118 1.1 mrg tree fn = CALL_EXPR_FN (t);
119 1.1 mrg if (TREE_CODE (fn) == TEMPLATE_ID_EXPR
120 1.1 mrg && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
121 1.1 mrg {
122 1.1 mrg tree f1 = get_first_fn (fn);
123 1.1 mrg if (TREE_CODE (f1) == TEMPLATE_DECL
124 1.1 mrg && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (f1)))
125 1.1 mrg return true;
126 1.1 mrg }
127 1.1 mrg return false;
128 1.1 mrg }
129 1.1 mrg
130 1.1 mrg /* Returns true if any of the arguments in the template
131 1.1 mrg argument list is a wildcard or wildcard pack. */
132 1.1 mrg
133 1.1 mrg bool
134 1.1 mrg contains_wildcard_p (tree args)
135 1.1 mrg {
136 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
137 1.1 mrg {
138 1.1 mrg tree arg = TREE_VEC_ELT (args, i);
139 1.1 mrg if (TREE_CODE (arg) == WILDCARD_DECL)
140 1.1 mrg return true;
141 1.1 mrg }
142 1.1 mrg return false;
143 1.1 mrg }
144 1.1 mrg
145 1.1 mrg /* Build a new call expression, but don't actually generate a
146 1.1 mrg new function call. We just want the tree, not the semantics. */
147 1.1 mrg
148 1.1 mrg inline tree
149 1.1 mrg build_call_check (tree id)
150 1.1 mrg {
151 1.1 mrg ++processing_template_decl;
152 1.1 mrg vec<tree, va_gc> *fargs = make_tree_vector();
153 1.1 mrg tree call = finish_call_expr (id, &fargs, false, false, tf_none);
154 1.1 mrg release_tree_vector (fargs);
155 1.1 mrg --processing_template_decl;
156 1.1 mrg return call;
157 1.1 mrg }
158 1.1 mrg
159 1.1 mrg /* Build an expression that will check a variable concept. If any
160 1.1 mrg argument contains a wildcard, don't try to finish the variable
161 1.1 mrg template because we can't substitute into a non-existent
162 1.1 mrg declaration. */
163 1.1 mrg
164 1.1 mrg tree
165 1.1 mrg build_variable_check (tree id)
166 1.1 mrg {
167 1.1 mrg gcc_assert (TREE_CODE (id) == TEMPLATE_ID_EXPR);
168 1.1 mrg if (contains_wildcard_p (TREE_OPERAND (id, 1)))
169 1.1 mrg return id;
170 1.1 mrg
171 1.1 mrg ++processing_template_decl;
172 1.1 mrg tree var = finish_template_variable (id);
173 1.1 mrg --processing_template_decl;
174 1.1 mrg return var;
175 1.1 mrg }
176 1.1 mrg
177 1.1 mrg /*---------------------------------------------------------------------------
178 1.1 mrg Resolution of qualified concept names
179 1.1 mrg ---------------------------------------------------------------------------*/
180 1.1 mrg
181 1.1 mrg /* This facility is used to resolve constraint checks from
182 1.1 mrg requirement expressions. A constraint check is a call to
183 1.1 mrg a function template declared with the keyword 'concept'.
184 1.1 mrg
185 1.1 mrg The result of resolution is a pair (a TREE_LIST) whose value
186 1.1 mrg is the matched declaration, and whose purpose contains the
187 1.1 mrg coerced template arguments that can be substituted into the
188 1.1 mrg call. */
189 1.1 mrg
190 1.1 mrg // Given an overload set OVL, try to find a unique definition that can be
191 1.1 mrg // instantiated by the template arguments ARGS.
192 1.1 mrg //
193 1.1 mrg // This function is not called for arbitrary call expressions. In particular,
194 1.1 mrg // the call expression must be written with explicit template arguments
195 1.1 mrg // and no function arguments. For example:
196 1.1 mrg //
197 1.1 mrg // f<T, U>()
198 1.1 mrg //
199 1.1 mrg // If a single match is found, this returns a TREE_LIST whose VALUE
200 1.1 mrg // is the constraint function (not the template), and its PURPOSE is
201 1.1 mrg // the complete set of arguments substituted into the parameter list.
202 1.1 mrg static tree
203 1.1 mrg resolve_constraint_check (tree ovl, tree args)
204 1.1 mrg {
205 1.1 mrg int nerrs = 0;
206 1.1 mrg tree cands = NULL_TREE;
207 1.1 mrg for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
208 1.1 mrg {
209 1.1 mrg // Get the next template overload.
210 1.1 mrg tree tmpl = OVL_CURRENT (p);
211 1.1 mrg if (TREE_CODE (tmpl) != TEMPLATE_DECL)
212 1.1 mrg continue;
213 1.1 mrg
214 1.1 mrg // Don't try to deduce checks for non-concepts. We often
215 1.1 mrg // end up trying to resolve constraints in functional casts
216 1.1 mrg // as part of a postfix-expression. We can save time and
217 1.1 mrg // headaches by not instantiating those declarations.
218 1.1 mrg //
219 1.1 mrg // NOTE: This masks a potential error, caused by instantiating
220 1.1 mrg // non-deduced contexts using placeholder arguments.
221 1.1 mrg tree fn = DECL_TEMPLATE_RESULT (tmpl);
222 1.1 mrg if (DECL_ARGUMENTS (fn))
223 1.1 mrg continue;
224 1.1 mrg if (!DECL_DECLARED_CONCEPT_P (fn))
225 1.1 mrg continue;
226 1.1 mrg
227 1.1 mrg // Remember the candidate if we can deduce a substitution.
228 1.1 mrg ++processing_template_decl;
229 1.1 mrg tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
230 1.1 mrg if (tree subst = coerce_template_parms (parms, args, tmpl))
231 1.1 mrg {
232 1.1 mrg if (subst == error_mark_node)
233 1.1 mrg ++nerrs;
234 1.1 mrg else
235 1.1 mrg cands = tree_cons (subst, fn, cands);
236 1.1 mrg }
237 1.1 mrg --processing_template_decl;
238 1.1 mrg }
239 1.1 mrg
240 1.1 mrg if (!cands)
241 1.1 mrg /* We either had no candidates or failed deductions. */
242 1.1 mrg return nerrs ? error_mark_node : NULL_TREE;
243 1.1 mrg else if (TREE_CHAIN (cands))
244 1.1 mrg /* There are multiple candidates. */
245 1.1 mrg return error_mark_node;
246 1.1 mrg
247 1.1 mrg return cands;
248 1.1 mrg }
249 1.1 mrg
250 1.1 mrg // Determine if the the call expression CALL is a constraint check, and
251 1.1 mrg // return the concept declaration and arguments being checked. If CALL
252 1.1 mrg // does not denote a constraint check, return NULL.
253 1.1 mrg tree
254 1.1 mrg resolve_constraint_check (tree call)
255 1.1 mrg {
256 1.1 mrg gcc_assert (TREE_CODE (call) == CALL_EXPR);
257 1.1 mrg
258 1.1 mrg // A constraint check must be only a template-id expression. If
259 1.1 mrg // it's a call to a base-link, its function(s) should be a
260 1.1 mrg // template-id expression. If this is not a template-id, then it
261 1.1 mrg // cannot be a concept-check.
262 1.1 mrg tree target = CALL_EXPR_FN (call);
263 1.1 mrg if (BASELINK_P (target))
264 1.1 mrg target = BASELINK_FUNCTIONS (target);
265 1.1 mrg if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
266 1.1 mrg return NULL_TREE;
267 1.1 mrg
268 1.1 mrg // Get the overload set and template arguments and try to
269 1.1 mrg // resolve the target.
270 1.1 mrg tree ovl = TREE_OPERAND (target, 0);
271 1.1 mrg
272 1.1 mrg /* This is a function call of a variable concept... ill-formed. */
273 1.1 mrg if (TREE_CODE (ovl) == TEMPLATE_DECL)
274 1.1 mrg {
275 1.1 mrg error_at (location_of (call),
276 1.1 mrg "function call of variable concept %qE", call);
277 1.1 mrg return error_mark_node;
278 1.1 mrg }
279 1.1 mrg
280 1.1 mrg tree args = TREE_OPERAND (target, 1);
281 1.1 mrg return resolve_constraint_check (ovl, args);
282 1.1 mrg }
283 1.1 mrg
284 1.1 mrg /* Returns a pair containing the checked variable concept
285 1.1 mrg and its associated prototype parameter. The result
286 1.1 mrg is a TREE_LIST whose TREE_VALUE is the variable concept
287 1.1 mrg and whose TREE_PURPOSE is the prototype parameter. */
288 1.1 mrg
289 1.1 mrg tree
290 1.1 mrg resolve_variable_concept_check (tree id)
291 1.1 mrg {
292 1.1 mrg tree tmpl = TREE_OPERAND (id, 0);
293 1.1 mrg tree args = TREE_OPERAND (id, 1);
294 1.1 mrg
295 1.1 mrg if (!variable_concept_p (tmpl))
296 1.1 mrg return NULL_TREE;
297 1.1 mrg
298 1.1 mrg /* Make sure that we have the right parameters before
299 1.1 mrg assuming that it works. Note that failing to deduce
300 1.1 mrg will result in diagnostics. */
301 1.1 mrg tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
302 1.1 mrg ++processing_template_decl;
303 1.1 mrg tree result = coerce_template_parms (parms, args, tmpl);
304 1.1 mrg --processing_template_decl;
305 1.1 mrg if (result != error_mark_node)
306 1.1 mrg {
307 1.1 mrg tree decl = DECL_TEMPLATE_RESULT (tmpl);
308 1.1 mrg return build_tree_list (result, decl);
309 1.1 mrg }
310 1.1 mrg else
311 1.1 mrg return error_mark_node;
312 1.1 mrg }
313 1.1 mrg
314 1.1 mrg
315 1.1 mrg /* Given a call expression or template-id expression to
316 1.1 mrg a concept EXPR possibly including a wildcard, deduce
317 1.1 mrg the concept being checked and the prototype parameter.
318 1.1 mrg Returns true if the constraint and prototype can be
319 1.1 mrg deduced and false otherwise. Note that the CHECK and
320 1.1 mrg PROTO arguments are set to NULL_TREE if this returns
321 1.1 mrg false. */
322 1.1 mrg
323 1.1 mrg bool
324 1.1 mrg deduce_constrained_parameter (tree expr, tree& check, tree& proto)
325 1.1 mrg {
326 1.1 mrg tree info = NULL_TREE;
327 1.1 mrg if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
328 1.1 mrg info = resolve_variable_concept_check (expr);
329 1.1 mrg else if (TREE_CODE (expr) == CALL_EXPR)
330 1.1 mrg info = resolve_constraint_check (expr);
331 1.1 mrg else
332 1.1 mrg gcc_unreachable ();
333 1.1 mrg
334 1.1 mrg if (info && info != error_mark_node)
335 1.1 mrg {
336 1.1 mrg check = TREE_VALUE (info);
337 1.1 mrg tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
338 1.1 mrg if (ARGUMENT_PACK_P (arg))
339 1.1 mrg arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
340 1.1 mrg proto = TREE_TYPE (arg);
341 1.1 mrg return true;
342 1.1 mrg }
343 1.1 mrg check = proto = NULL_TREE;
344 1.1 mrg return false;
345 1.1 mrg }
346 1.1 mrg
347 1.1 mrg // Given a call expression or template-id expression to a concept, EXPR,
348 1.1 mrg // deduce the concept being checked and return the template arguments.
349 1.1 mrg // Returns NULL_TREE if deduction fails.
350 1.1 mrg static tree
351 1.1 mrg deduce_concept_introduction (tree expr)
352 1.1 mrg {
353 1.1 mrg tree info = NULL_TREE;
354 1.1 mrg if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
355 1.1 mrg info = resolve_variable_concept_check (expr);
356 1.1 mrg else if (TREE_CODE (expr) == CALL_EXPR)
357 1.1 mrg info = resolve_constraint_check (expr);
358 1.1 mrg else
359 1.1 mrg gcc_unreachable ();
360 1.1 mrg
361 1.1 mrg if (info && info != error_mark_node)
362 1.1 mrg return TREE_PURPOSE (info);
363 1.1 mrg return NULL_TREE;
364 1.1 mrg }
365 1.1 mrg
366 1.1 mrg namespace {
367 1.1 mrg
368 1.1 mrg /*---------------------------------------------------------------------------
369 1.1 mrg Constraint implication learning
370 1.1 mrg ---------------------------------------------------------------------------*/
371 1.1 mrg
372 1.1 mrg /* The implication context determines how we memoize concept checks.
373 1.1 mrg Given two checks C1 and C2, the direction of implication depends
374 1.1 mrg on whether we are learning implications of a conjunction or disjunction.
375 1.1 mrg For example:
376 1.1 mrg
377 1.1 mrg template<typename T> concept bool C = ...;
378 1.1 mrg template<typenaem T> concept bool D = C<T> && true;
379 1.1 mrg
380 1.1 mrg From this, we can learn that D<T> implies C<T>. We cannot learn,
381 1.1 mrg without further testing, that C<T> does not imply D<T>. If, for
382 1.1 mrg example, C<T> were defined as true, then these constraints would
383 1.1 mrg be logically equivalent.
384 1.1 mrg
385 1.1 mrg In rare cases, we may start with a logical equivalence. For example:
386 1.1 mrg
387 1.1 mrg template<typename T> concept bool C = ...;
388 1.1 mrg template<typename T> concept bool D = C<T>;
389 1.1 mrg
390 1.1 mrg Here, we learn that C<T> implies D<T> and vice versa. */
391 1.1 mrg
392 1.1 mrg enum implication_context
393 1.1 mrg {
394 1.1 mrg conjunction_cxt, /* C1 implies C2. */
395 1.1 mrg disjunction_cxt, /* C2 implies C1. */
396 1.1 mrg equivalence_cxt /* C1 implies C2, C2 implies C1. */
397 1.1 mrg };
398 1.1 mrg
399 1.1 mrg void learn_implications(tree, tree, implication_context);
400 1.1 mrg
401 1.1 mrg void
402 1.1 mrg learn_implication (tree parent, tree child, implication_context cxt)
403 1.1 mrg {
404 1.1 mrg switch (cxt)
405 1.1 mrg {
406 1.1 mrg case conjunction_cxt:
407 1.1 mrg save_subsumption_result (parent, child, true);
408 1.1 mrg break;
409 1.1 mrg case disjunction_cxt:
410 1.1 mrg save_subsumption_result (child, parent, true);
411 1.1 mrg break;
412 1.1 mrg case equivalence_cxt:
413 1.1 mrg save_subsumption_result (parent, child, true);
414 1.1 mrg save_subsumption_result (child, parent, true);
415 1.1 mrg break;
416 1.1 mrg }
417 1.1 mrg }
418 1.1 mrg
419 1.1 mrg void
420 1.1 mrg learn_logical_operation (tree parent, tree constr, implication_context cxt)
421 1.1 mrg {
422 1.1 mrg learn_implications (parent, TREE_OPERAND (constr, 0), cxt);
423 1.1 mrg learn_implications (parent, TREE_OPERAND (constr, 1), cxt);
424 1.1 mrg }
425 1.1 mrg
426 1.1 mrg void
427 1.1 mrg learn_implications (tree parent, tree constr, implication_context cxt)
428 1.1 mrg {
429 1.1 mrg switch (TREE_CODE (constr))
430 1.1 mrg {
431 1.1 mrg case CHECK_CONSTR:
432 1.1 mrg return learn_implication (parent, constr, cxt);
433 1.1 mrg
434 1.1 mrg case CONJ_CONSTR:
435 1.1 mrg if (cxt == disjunction_cxt)
436 1.1 mrg return;
437 1.1 mrg return learn_logical_operation (parent, constr, cxt);
438 1.1 mrg
439 1.1 mrg case DISJ_CONSTR:
440 1.1 mrg if (cxt == conjunction_cxt)
441 1.1 mrg return;
442 1.1 mrg return learn_logical_operation (parent, constr, cxt);
443 1.1 mrg
444 1.1 mrg default:
445 1.1 mrg break;
446 1.1 mrg }
447 1.1 mrg }
448 1.1 mrg
449 1.1 mrg /* Quickly scan the top-level constraints of CONSTR to learn and
450 1.1 mrg cache logical relations between concepts. The search does not
451 1.1 mrg include conjunctions of disjunctions or vice versa. */
452 1.1 mrg
453 1.1 mrg void
454 1.1 mrg learn_implications (tree tmpl, tree args, tree constr)
455 1.1 mrg {
456 1.1 mrg /* Don't memoize relations between non-dependent arguemnts. It's not
457 1.1 mrg helpful. */
458 1.1 mrg if (!uses_template_parms (args))
459 1.1 mrg return;
460 1.1 mrg
461 1.1 mrg /* Build a check constraint for the purpose of caching. */
462 1.1 mrg tree parent = build_nt (CHECK_CONSTR, tmpl, args);
463 1.1 mrg
464 1.1 mrg /* Start learning based on the kind of the top-level contraint. */
465 1.1 mrg if (TREE_CODE (constr) == CONJ_CONSTR)
466 1.1 mrg return learn_logical_operation (parent, constr, conjunction_cxt);
467 1.1 mrg else if (TREE_CODE (constr) == DISJ_CONSTR)
468 1.1 mrg return learn_logical_operation (parent, constr, disjunction_cxt);
469 1.1 mrg else if (TREE_CODE (constr) == CHECK_CONSTR)
470 1.1 mrg /* This is the rare concept alias case. */
471 1.1 mrg return learn_implication (parent, constr, equivalence_cxt);
472 1.1 mrg }
473 1.1 mrg
474 1.1 mrg /*---------------------------------------------------------------------------
475 1.1 mrg Expansion of concept definitions
476 1.1 mrg ---------------------------------------------------------------------------*/
477 1.1 mrg
478 1.1 mrg /* Returns the expression of a function concept. */
479 1.1 mrg
480 1.1 mrg tree
481 1.1 mrg get_returned_expression (tree fn)
482 1.1 mrg {
483 1.1 mrg /* Extract the body of the function minus the return expression. */
484 1.1 mrg tree body = DECL_SAVED_TREE (fn);
485 1.1 mrg if (!body)
486 1.1 mrg return error_mark_node;
487 1.1 mrg if (TREE_CODE (body) == BIND_EXPR)
488 1.1 mrg body = BIND_EXPR_BODY (body);
489 1.1 mrg if (TREE_CODE (body) != RETURN_EXPR)
490 1.1 mrg return error_mark_node;
491 1.1 mrg
492 1.1 mrg return TREE_OPERAND (body, 0);
493 1.1 mrg }
494 1.1 mrg
495 1.1 mrg /* Returns the initializer of a variable concept. */
496 1.1 mrg
497 1.1 mrg tree
498 1.1 mrg get_variable_initializer (tree var)
499 1.1 mrg {
500 1.1 mrg tree init = DECL_INITIAL (var);
501 1.1 mrg if (!init)
502 1.1 mrg return error_mark_node;
503 1.1 mrg return init;
504 1.1 mrg }
505 1.1 mrg
506 1.1 mrg /* Returns the definition of a variable or function concept. */
507 1.1 mrg
508 1.1 mrg tree
509 1.1 mrg get_concept_definition (tree decl)
510 1.1 mrg {
511 1.1 mrg if (TREE_CODE (decl) == VAR_DECL)
512 1.1 mrg return get_variable_initializer (decl);
513 1.1 mrg else if (TREE_CODE (decl) == FUNCTION_DECL)
514 1.1 mrg return get_returned_expression (decl);
515 1.1 mrg gcc_unreachable ();
516 1.1 mrg }
517 1.1 mrg
518 1.1 mrg int expansion_level = 0;
519 1.1 mrg
520 1.1 mrg struct expanding_concept_sentinel
521 1.1 mrg {
522 1.1 mrg expanding_concept_sentinel ()
523 1.1 mrg {
524 1.1 mrg ++expansion_level;
525 1.1 mrg }
526 1.1 mrg
527 1.1 mrg ~expanding_concept_sentinel()
528 1.1 mrg {
529 1.1 mrg --expansion_level;
530 1.1 mrg }
531 1.1 mrg };
532 1.1 mrg
533 1.1 mrg
534 1.1 mrg } /* namespace */
535 1.1 mrg
536 1.1 mrg /* Returns true when a concept is being expanded. */
537 1.1 mrg
538 1.1 mrg bool
539 1.1 mrg expanding_concept()
540 1.1 mrg {
541 1.1 mrg return expansion_level > 0;
542 1.1 mrg }
543 1.1 mrg
544 1.1 mrg /* Expand a concept declaration (not a template) and its arguments to
545 1.1 mrg a constraint defined by the concept's initializer or definition. */
546 1.1 mrg
547 1.1 mrg tree
548 1.1 mrg expand_concept (tree decl, tree args)
549 1.1 mrg {
550 1.1 mrg expanding_concept_sentinel sentinel;
551 1.1 mrg
552 1.1 mrg if (TREE_CODE (decl) == TEMPLATE_DECL)
553 1.1 mrg decl = DECL_TEMPLATE_RESULT (decl);
554 1.1 mrg tree tmpl = DECL_TI_TEMPLATE (decl);
555 1.1 mrg
556 1.1 mrg /* Check for a previous specialization. */
557 1.1 mrg if (tree spec = get_concept_expansion (tmpl, args))
558 1.1 mrg return spec;
559 1.1 mrg
560 1.1 mrg /* Substitute the arguments to form a new definition expression. */
561 1.1 mrg tree def = get_concept_definition (decl);
562 1.1 mrg
563 1.1 mrg ++processing_template_decl;
564 1.1 mrg tree result = tsubst_expr (def, args, tf_none, NULL_TREE, true);
565 1.1 mrg --processing_template_decl;
566 1.1 mrg if (result == error_mark_node)
567 1.1 mrg return error_mark_node;
568 1.1 mrg
569 1.1 mrg /* And lastly, normalize it, check for implications, and save
570 1.1 mrg the specialization for later. */
571 1.1 mrg tree norm = normalize_expression (result);
572 1.1 mrg learn_implications (tmpl, args, norm);
573 1.1 mrg return save_concept_expansion (tmpl, args, norm);
574 1.1 mrg }
575 1.1 mrg
576 1.1 mrg
577 1.1 mrg /*---------------------------------------------------------------------------
578 1.1 mrg Stepwise normalization of expressions
579 1.1 mrg
580 1.1 mrg This set of functions will transform an expression into a constraint
581 1.1 mrg in a sequence of steps. Normalization does not not look into concept
582 1.1 mrg definitions.
583 1.1 mrg ---------------------------------------------------------------------------*/
584 1.1 mrg
585 1.1 mrg /* Transform a logical-or or logical-and expression into either
586 1.1 mrg a conjunction or disjunction. */
587 1.1 mrg
588 1.1 mrg tree
589 1.1 mrg normalize_logical_operation (tree t, tree_code c)
590 1.1 mrg {
591 1.1 mrg tree t0 = normalize_expression (TREE_OPERAND (t, 0));
592 1.1 mrg tree t1 = normalize_expression (TREE_OPERAND (t, 1));
593 1.1 mrg return build_nt (c, t0, t1);
594 1.1 mrg }
595 1.1 mrg
596 1.1 mrg /* A simple requirement T introduces an expression constraint
597 1.1 mrg for its expression. */
598 1.1 mrg
599 1.1 mrg inline tree
600 1.1 mrg normalize_simple_requirement (tree t)
601 1.1 mrg {
602 1.1 mrg return build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
603 1.1 mrg }
604 1.1 mrg
605 1.1 mrg /* A type requirement T introduce a type constraint for its type. */
606 1.1 mrg
607 1.1 mrg inline tree
608 1.1 mrg normalize_type_requirement (tree t)
609 1.1 mrg {
610 1.1 mrg return build_nt (TYPE_CONSTR, TREE_OPERAND (t, 0));
611 1.1 mrg }
612 1.1 mrg
613 1.1 mrg /* A compound requirement T introduces a conjunction of constraints
614 1.1 mrg depending on its form. The conjunction always includes an
615 1.1 mrg expression constraint for the expression of the requirement.
616 1.1 mrg If a trailing return type was specified, the conjunction includes
617 1.1 mrg either an implicit conversion constraint or an argument deduction
618 1.1 mrg constraint. If the noexcept specifier is present, the conjunction
619 1.1 mrg includes an exception constraint. */
620 1.1 mrg
621 1.1 mrg tree
622 1.1 mrg normalize_compound_requirement (tree t)
623 1.1 mrg {
624 1.1 mrg tree expr = TREE_OPERAND (t, 0);
625 1.1 mrg tree constr = build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
626 1.1 mrg
627 1.1 mrg /* If a type is given, append an implicit conversion or
628 1.1 mrg argument deduction constraint. */
629 1.1 mrg if (tree type = TREE_OPERAND (t, 1))
630 1.1 mrg {
631 1.1 mrg tree type_constr;
632 1.1 mrg /* TODO: We should be extracting a list of auto nodes
633 1.1 mrg from type_uses_auto, not a single node */
634 1.1 mrg if (tree placeholder = type_uses_auto (type))
635 1.1 mrg type_constr = build_nt (DEDUCT_CONSTR, expr, type, placeholder);
636 1.1 mrg else
637 1.1 mrg type_constr = build_nt (ICONV_CONSTR, expr, type);
638 1.1 mrg constr = conjoin_constraints (constr, type_constr);
639 1.1 mrg }
640 1.1 mrg
641 1.1 mrg /* If noexcept is present, append an exception constraint. */
642 1.1 mrg if (COMPOUND_REQ_NOEXCEPT_P (t))
643 1.1 mrg {
644 1.1 mrg tree except = build_nt (EXCEPT_CONSTR, expr);
645 1.1 mrg constr = conjoin_constraints (constr, except);
646 1.1 mrg }
647 1.1 mrg
648 1.1 mrg return constr;
649 1.1 mrg }
650 1.1 mrg
651 1.1 mrg /* A nested requirement T introduces a conjunction of constraints
652 1.1 mrg corresponding to its constraint-expression.
653 1.1 mrg
654 1.1 mrg If the result of transforming T is error_mark_node, the resulting
655 1.1 mrg constraint is a predicate constraint whose operand is also
656 1.1 mrg error_mark_node. This preserves the constraint structure, but
657 1.1 mrg will guarantee that the constraint is never satisfied. */
658 1.1 mrg
659 1.1 mrg inline tree
660 1.1 mrg normalize_nested_requirement (tree t)
661 1.1 mrg {
662 1.1 mrg return normalize_expression (TREE_OPERAND (t, 0));
663 1.1 mrg }
664 1.1 mrg
665 1.1 mrg /* Transform a requirement T into one or more constraints. */
666 1.1 mrg
667 1.1 mrg tree
668 1.1 mrg normalize_requirement (tree t)
669 1.1 mrg {
670 1.1 mrg switch (TREE_CODE (t))
671 1.1 mrg {
672 1.1 mrg case SIMPLE_REQ:
673 1.1 mrg return normalize_simple_requirement (t);
674 1.1 mrg
675 1.1 mrg case TYPE_REQ:
676 1.1 mrg return normalize_type_requirement (t);
677 1.1 mrg
678 1.1 mrg case COMPOUND_REQ:
679 1.1 mrg return normalize_compound_requirement (t);
680 1.1 mrg
681 1.1 mrg case NESTED_REQ:
682 1.1 mrg return normalize_nested_requirement (t);
683 1.1 mrg
684 1.1 mrg default:
685 1.1 mrg gcc_unreachable ();
686 1.1 mrg }
687 1.1 mrg return error_mark_node;
688 1.1 mrg }
689 1.1 mrg
690 1.1 mrg /* Transform a sequence of requirements into a conjunction of
691 1.1 mrg constraints. */
692 1.1 mrg
693 1.1 mrg tree
694 1.1 mrg normalize_requirements (tree t)
695 1.1 mrg {
696 1.1 mrg tree result = NULL_TREE;
697 1.1 mrg for (; t; t = TREE_CHAIN (t))
698 1.1 mrg {
699 1.1 mrg tree constr = normalize_requirement (TREE_VALUE (t));
700 1.1 mrg result = conjoin_constraints (result, constr);
701 1.1 mrg }
702 1.1 mrg return result;
703 1.1 mrg }
704 1.1 mrg
705 1.1 mrg /* The normal form of a requires-expression is a parameterized
706 1.1 mrg constraint having the same parameters and a conjunction of
707 1.1 mrg constraints representing the normal form of requirements. */
708 1.1 mrg
709 1.1 mrg tree
710 1.1 mrg normalize_requires_expression (tree t)
711 1.1 mrg {
712 1.1 mrg tree operand = normalize_requirements (TREE_OPERAND (t, 1));
713 1.1 mrg if (tree parms = TREE_OPERAND (t, 0))
714 1.1 mrg return build_nt (PARM_CONSTR, parms, operand);
715 1.1 mrg else
716 1.1 mrg return operand;
717 1.1 mrg }
718 1.1 mrg
719 1.1 mrg /* For a template-id referring to a variable concept, returns
720 1.1 mrg a check constraint. Otherwise, returns a predicate constraint. */
721 1.1 mrg
722 1.1 mrg tree
723 1.1 mrg normalize_template_id_expression (tree t)
724 1.1 mrg {
725 1.1 mrg if (tree info = resolve_variable_concept_check (t))
726 1.1 mrg {
727 1.1 mrg if (info == error_mark_node)
728 1.1 mrg {
729 1.1 mrg /* We get this when the template arguments don't match
730 1.1 mrg the variable concept. */
731 1.1 mrg error ("invalid reference to concept %qE", t);
732 1.1 mrg return error_mark_node;
733 1.1 mrg }
734 1.1 mrg
735 1.1 mrg tree decl = TREE_VALUE (info);
736 1.1 mrg tree args = TREE_PURPOSE (info);
737 1.1 mrg return build_nt (CHECK_CONSTR, decl, args);
738 1.1 mrg }
739 1.1 mrg
740 1.1 mrg /* Check that we didn't refer to a function concept like a variable. */
741 1.1 mrg tree tmpl = TREE_OPERAND (t, 0);
742 1.1 mrg if (TREE_CODE (tmpl) == OVERLOAD)
743 1.1 mrg {
744 1.1 mrg tree fn = OVL_FUNCTION (tmpl);
745 1.1 mrg if (TREE_CODE (fn) == TEMPLATE_DECL
746 1.1 mrg && DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn)))
747 1.1 mrg {
748 1.1 mrg error_at (location_of (t),
749 1.1 mrg "invalid reference to function concept %qD", fn);
750 1.1 mrg return error_mark_node;
751 1.1 mrg }
752 1.1 mrg }
753 1.1 mrg
754 1.1 mrg return build_nt (PRED_CONSTR, t);
755 1.1 mrg }
756 1.1 mrg
757 1.1 mrg /* For a call expression to a function concept, returns a check
758 1.1 mrg constraint. Otherwise, returns a predicate constraint. */
759 1.1 mrg
760 1.1 mrg tree
761 1.1 mrg normalize_call_expression (tree t)
762 1.1 mrg {
763 1.1 mrg /* Try to resolve this function call as a concept. If not, then
764 1.1 mrg it can be returned as a predicate constraint. */
765 1.1 mrg tree check = resolve_constraint_check (t);
766 1.1 mrg if (!check)
767 1.1 mrg return build_nt (PRED_CONSTR, t);
768 1.1 mrg if (check == error_mark_node)
769 1.1 mrg {
770 1.1 mrg /* TODO: Improve diagnostics. We could report why the reference
771 1.1 mrg is invalid. */
772 1.1 mrg error ("invalid reference to concept %qE", t);
773 1.1 mrg return error_mark_node;
774 1.1 mrg }
775 1.1 mrg
776 1.1 mrg tree fn = TREE_VALUE (check);
777 1.1 mrg tree args = TREE_PURPOSE (check);
778 1.1 mrg return build_nt (CHECK_CONSTR, fn, args);
779 1.1 mrg }
780 1.1 mrg
781 1.1 mrg /* If T is a call to an overloaded && or || operator, diagnose that
782 1.1 mrg as a non-SFINAEable error. Returns true if an error is emitted.
783 1.1 mrg
784 1.1 mrg TODO: It would be better to diagnose this at the point of definition,
785 1.1 mrg if possible. Perhaps we should immediately do a first-pass normalization
786 1.1 mrg of a concept definition to catch obvious non-dependent errors like
787 1.1 mrg this. */
788 1.1 mrg
789 1.1 mrg bool
790 1.1 mrg check_for_logical_overloads (tree t)
791 1.1 mrg {
792 1.1 mrg if (TREE_CODE (t) != CALL_EXPR)
793 1.1 mrg return false;
794 1.1 mrg
795 1.1 mrg tree fn = CALL_EXPR_FN (t);
796 1.1 mrg
797 1.1 mrg /* For member calls, try extracting the function from the
798 1.1 mrg component ref. */
799 1.1 mrg if (TREE_CODE (fn) == COMPONENT_REF)
800 1.1 mrg {
801 1.1 mrg fn = TREE_OPERAND (fn, 1);
802 1.1 mrg if (TREE_CODE (fn) == BASELINK)
803 1.1 mrg fn = BASELINK_FUNCTIONS (fn);
804 1.1 mrg }
805 1.1 mrg
806 1.1 mrg if (TREE_CODE (fn) != FUNCTION_DECL)
807 1.1 mrg return false;
808 1.1 mrg
809 1.1 mrg if (DECL_OVERLOADED_OPERATOR_P (fn))
810 1.1 mrg {
811 1.1 mrg location_t loc = EXPR_LOC_OR_LOC (t, input_location);
812 1.1 mrg error_at (loc, "constraint %qE, uses overloaded operator", t);
813 1.1 mrg return true;
814 1.1 mrg }
815 1.1 mrg
816 1.1 mrg return false;
817 1.1 mrg }
818 1.1 mrg
819 1.1 mrg /* The normal form of an atom depends on the expression. The normal
820 1.1 mrg form of a function call to a function concept is a check constraint
821 1.1 mrg for that concept. The normal form of a reference to a variable
822 1.1 mrg concept is a check constraint for that concept. Otherwise, the
823 1.1 mrg constraint is a predicate constraint. */
824 1.1 mrg
825 1.1 mrg tree
826 1.1 mrg normalize_atom (tree t)
827 1.1 mrg {
828 1.1 mrg /* We can get constraints pushed down through pack expansions, so
829 1.1 mrg just return them. */
830 1.1 mrg if (constraint_p (t))
831 1.1 mrg return t;
832 1.1 mrg
833 1.1 mrg tree type = TREE_TYPE (t);
834 1.1 mrg if (!type || type_unknown_p (t) || TREE_CODE (type) == TEMPLATE_TYPE_PARM)
835 1.1 mrg ;
836 1.1 mrg else if (!dependent_type_p (type))
837 1.1 mrg {
838 1.1 mrg if (check_for_logical_overloads (t))
839 1.1 mrg return error_mark_node;
840 1.1 mrg
841 1.1 mrg type = cv_unqualified (type);
842 1.1 mrg if (!same_type_p (type, boolean_type_node))
843 1.1 mrg {
844 1.1 mrg error ("predicate constraint %q+E does not have type %<bool%>", t);
845 1.1 mrg return error_mark_node;
846 1.1 mrg }
847 1.1 mrg }
848 1.1 mrg
849 1.1 mrg if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
850 1.1 mrg return normalize_template_id_expression (t);
851 1.1 mrg if (TREE_CODE (t) == CALL_EXPR)
852 1.1 mrg return normalize_call_expression (t);
853 1.1 mrg return build_nt (PRED_CONSTR, t);
854 1.1 mrg }
855 1.1 mrg
856 1.1 mrg /* Push down the pack expansion EXP into the leaves of the constraint PAT. */
857 1.1 mrg
858 1.1 mrg tree
859 1.1 mrg push_down_pack_expansion (tree exp, tree pat)
860 1.1 mrg {
861 1.1 mrg switch (TREE_CODE (pat))
862 1.1 mrg {
863 1.1 mrg case CONJ_CONSTR:
864 1.1 mrg case DISJ_CONSTR:
865 1.1 mrg {
866 1.1 mrg pat = copy_node (pat);
867 1.1 mrg TREE_OPERAND (pat, 0)
868 1.1 mrg = push_down_pack_expansion (exp, TREE_OPERAND (pat, 0));
869 1.1 mrg TREE_OPERAND (pat, 1)
870 1.1 mrg = push_down_pack_expansion (exp, TREE_OPERAND (pat, 1));
871 1.1 mrg return pat;
872 1.1 mrg }
873 1.1 mrg default:
874 1.1 mrg {
875 1.1 mrg exp = copy_node (exp);
876 1.1 mrg SET_PACK_EXPANSION_PATTERN (exp, pat);
877 1.1 mrg return exp;
878 1.1 mrg }
879 1.1 mrg }
880 1.1 mrg }
881 1.1 mrg
882 1.1 mrg /* Transform a pack expansion into a constraint. First we transform the
883 1.1 mrg pattern of the pack expansion, then we push the pack expansion down into the
884 1.1 mrg leaves of the constraint so that partial ordering will work. */
885 1.1 mrg
886 1.1 mrg tree
887 1.1 mrg normalize_pack_expansion (tree t)
888 1.1 mrg {
889 1.1 mrg tree pat = normalize_expression (PACK_EXPANSION_PATTERN (t));
890 1.1 mrg return push_down_pack_expansion (t, pat);
891 1.1 mrg }
892 1.1 mrg
893 1.1 mrg /* Transform an expression into a constraint. */
894 1.1 mrg
895 1.1 mrg tree
896 1.1 mrg normalize_any_expression (tree t)
897 1.1 mrg {
898 1.1 mrg switch (TREE_CODE (t))
899 1.1 mrg {
900 1.1 mrg case TRUTH_ANDIF_EXPR:
901 1.1 mrg return normalize_logical_operation (t, CONJ_CONSTR);
902 1.1 mrg
903 1.1 mrg case TRUTH_ORIF_EXPR:
904 1.1 mrg return normalize_logical_operation (t, DISJ_CONSTR);
905 1.1 mrg
906 1.1 mrg case REQUIRES_EXPR:
907 1.1 mrg return normalize_requires_expression (t);
908 1.1 mrg
909 1.1 mrg case BIND_EXPR:
910 1.1 mrg return normalize_expression (BIND_EXPR_BODY (t));
911 1.1 mrg
912 1.1 mrg case EXPR_PACK_EXPANSION:
913 1.1 mrg return normalize_pack_expansion (t);
914 1.1 mrg
915 1.1 mrg default:
916 1.1 mrg /* All other constraints are atomic. */
917 1.1 mrg return normalize_atom (t);
918 1.1 mrg }
919 1.1 mrg }
920 1.1 mrg
921 1.1 mrg /* Transform a statement into an expression. */
922 1.1 mrg tree
923 1.1 mrg normalize_any_statement (tree t)
924 1.1 mrg {
925 1.1 mrg switch (TREE_CODE (t))
926 1.1 mrg {
927 1.1 mrg case RETURN_EXPR:
928 1.1 mrg return normalize_expression (TREE_OPERAND (t, 0));
929 1.1 mrg default:
930 1.1 mrg gcc_unreachable ();
931 1.1 mrg }
932 1.1 mrg return error_mark_node;
933 1.1 mrg }
934 1.1 mrg
935 1.1 mrg /* Reduction rules for the declaration T. */
936 1.1 mrg
937 1.1 mrg tree
938 1.1 mrg normalize_any_declaration (tree t)
939 1.1 mrg {
940 1.1 mrg switch (TREE_CODE (t))
941 1.1 mrg {
942 1.1 mrg case VAR_DECL:
943 1.1 mrg return normalize_atom (t);
944 1.1 mrg default:
945 1.1 mrg gcc_unreachable ();
946 1.1 mrg }
947 1.1 mrg return error_mark_node;
948 1.1 mrg }
949 1.1 mrg
950 1.1 mrg /* Returns the normal form of a constraint expression. */
951 1.1 mrg
952 1.1 mrg tree
953 1.1 mrg normalize_expression (tree t)
954 1.1 mrg {
955 1.1 mrg if (!t)
956 1.1 mrg return NULL_TREE;
957 1.1 mrg
958 1.1 mrg if (t == error_mark_node)
959 1.1 mrg return error_mark_node;
960 1.1 mrg
961 1.1 mrg switch (TREE_CODE_CLASS (TREE_CODE (t)))
962 1.1 mrg {
963 1.1 mrg case tcc_unary:
964 1.1 mrg case tcc_binary:
965 1.1 mrg case tcc_expression:
966 1.1 mrg case tcc_vl_exp:
967 1.1 mrg return normalize_any_expression (t);
968 1.1 mrg
969 1.1 mrg case tcc_statement:
970 1.1 mrg return normalize_any_statement (t);
971 1.1 mrg
972 1.1 mrg case tcc_declaration:
973 1.1 mrg return normalize_any_declaration (t);
974 1.1 mrg
975 1.1 mrg case tcc_exceptional:
976 1.1 mrg case tcc_constant:
977 1.1 mrg case tcc_reference:
978 1.1 mrg case tcc_comparison:
979 1.1 mrg /* These are all atomic predicate constraints. */
980 1.1 mrg return normalize_atom (t);
981 1.1 mrg
982 1.1 mrg default:
983 1.1 mrg /* Unhandled node kind. */
984 1.1 mrg gcc_unreachable ();
985 1.1 mrg }
986 1.1 mrg return error_mark_node;
987 1.1 mrg }
988 1.1 mrg
989 1.1 mrg
990 1.1 mrg /*---------------------------------------------------------------------------
991 1.1 mrg Constraint normalization
992 1.1 mrg ---------------------------------------------------------------------------*/
993 1.1 mrg
994 1.1 mrg tree normalize_constraint (tree);
995 1.1 mrg
996 1.1 mrg /* The normal form of the disjunction T0 /\ T1 is the conjunction
997 1.1 mrg of the normal form of T0 and the normal form of T1. */
998 1.1 mrg
999 1.1 mrg inline tree
1000 1.1 mrg normalize_conjunction (tree t)
1001 1.1 mrg {
1002 1.1 mrg tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
1003 1.1 mrg tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
1004 1.1 mrg return build_nt (CONJ_CONSTR, t0, t1);
1005 1.1 mrg }
1006 1.1 mrg
1007 1.1 mrg /* The normal form of the disjunction T0 \/ T1 is the disjunction
1008 1.1 mrg of the normal form of T0 and the normal form of T1. */
1009 1.1 mrg
1010 1.1 mrg inline tree
1011 1.1 mrg normalize_disjunction (tree t)
1012 1.1 mrg {
1013 1.1 mrg tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
1014 1.1 mrg tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
1015 1.1 mrg return build_nt (DISJ_CONSTR, t0, t1);
1016 1.1 mrg }
1017 1.1 mrg
1018 1.1 mrg /* A predicate constraint is normalized in two stages. First all
1019 1.1 mrg references specializations of concepts are replaced by their
1020 1.1 mrg substituted definitions. Then, the resulting expression is
1021 1.1 mrg transformed into a constraint by transforming && expressions
1022 1.1 mrg into conjunctions and || into disjunctions. */
1023 1.1 mrg
1024 1.1 mrg tree
1025 1.1 mrg normalize_predicate_constraint (tree t)
1026 1.1 mrg {
1027 1.1 mrg ++processing_template_decl;
1028 1.1 mrg tree expr = PRED_CONSTR_EXPR (t);
1029 1.1 mrg tree constr = normalize_expression (expr);
1030 1.1 mrg --processing_template_decl;
1031 1.1 mrg return constr;
1032 1.1 mrg }
1033 1.1 mrg
1034 1.1 mrg /* The normal form of a parameterized constraint is the normal
1035 1.1 mrg form of its operand. */
1036 1.1 mrg
1037 1.1 mrg tree
1038 1.1 mrg normalize_parameterized_constraint (tree t)
1039 1.1 mrg {
1040 1.1 mrg tree parms = PARM_CONSTR_PARMS (t);
1041 1.1 mrg tree operand = normalize_constraint (PARM_CONSTR_OPERAND (t));
1042 1.1 mrg return build_nt (PARM_CONSTR, parms, operand);
1043 1.1 mrg }
1044 1.1 mrg
1045 1.1 mrg /* Normalize the constraint T by reducing it so that it is
1046 1.1 mrg comprised of only conjunctions and disjunctions of atomic
1047 1.1 mrg constraints. */
1048 1.1 mrg
1049 1.1 mrg tree
1050 1.1 mrg normalize_constraint (tree t)
1051 1.1 mrg {
1052 1.1 mrg if (!t)
1053 1.1 mrg return NULL_TREE;
1054 1.1 mrg
1055 1.1 mrg if (t == error_mark_node)
1056 1.1 mrg return t;
1057 1.1 mrg
1058 1.1 mrg switch (TREE_CODE (t))
1059 1.1 mrg {
1060 1.1 mrg case CONJ_CONSTR:
1061 1.1 mrg return normalize_conjunction (t);
1062 1.1 mrg
1063 1.1 mrg case DISJ_CONSTR:
1064 1.1 mrg return normalize_disjunction (t);
1065 1.1 mrg
1066 1.1 mrg case PRED_CONSTR:
1067 1.1 mrg return normalize_predicate_constraint (t);
1068 1.1 mrg
1069 1.1 mrg case PARM_CONSTR:
1070 1.1 mrg return normalize_parameterized_constraint (t);
1071 1.1 mrg
1072 1.1 mrg case EXPR_CONSTR:
1073 1.1 mrg case TYPE_CONSTR:
1074 1.1 mrg case ICONV_CONSTR:
1075 1.1 mrg case DEDUCT_CONSTR:
1076 1.1 mrg case EXCEPT_CONSTR:
1077 1.1 mrg /* These constraints are defined to be atomic. */
1078 1.1 mrg return t;
1079 1.1 mrg
1080 1.1 mrg default:
1081 1.1 mrg /* CONSTR was not a constraint. */
1082 1.1 mrg gcc_unreachable();
1083 1.1 mrg }
1084 1.1 mrg return error_mark_node;
1085 1.1 mrg }
1086 1.1 mrg
1087 1.1 mrg
1088 1.1 mrg
1089 1.1 mrg // -------------------------------------------------------------------------- //
1090 1.1 mrg // Constraint Semantic Processing
1091 1.1 mrg //
1092 1.1 mrg // The following functions are called by the parser and substitution rules
1093 1.1 mrg // to create and evaluate constraint-related nodes.
1094 1.1 mrg
1095 1.1 mrg // The constraints associated with the current template parameters.
1096 1.1 mrg tree
1097 1.1 mrg current_template_constraints (void)
1098 1.1 mrg {
1099 1.1 mrg if (!current_template_parms)
1100 1.1 mrg return NULL_TREE;
1101 1.1 mrg tree tmpl_constr = TEMPLATE_PARM_CONSTRAINTS (current_template_parms);
1102 1.1 mrg return build_constraints (tmpl_constr, NULL_TREE);
1103 1.1 mrg }
1104 1.1 mrg
1105 1.1 mrg // If the recently parsed TYPE declares or defines a template or template
1106 1.1 mrg // specialization, get its corresponding constraints from the current
1107 1.1 mrg // template parameters and bind them to TYPE's declaration.
1108 1.1 mrg tree
1109 1.1 mrg associate_classtype_constraints (tree type)
1110 1.1 mrg {
1111 1.1 mrg if (!type || type == error_mark_node || TREE_CODE (type) != RECORD_TYPE)
1112 1.1 mrg return type;
1113 1.1 mrg
1114 1.1 mrg // An explicit class template specialization has no template
1115 1.1 mrg // parameters.
1116 1.1 mrg if (!current_template_parms)
1117 1.1 mrg return type;
1118 1.1 mrg
1119 1.1 mrg if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1120 1.1 mrg {
1121 1.1 mrg tree decl = TYPE_STUB_DECL (type);
1122 1.1 mrg tree ci = current_template_constraints ();
1123 1.1 mrg
1124 1.1 mrg // An implicitly instantiated member template declaration already
1125 1.1 mrg // has associated constraints. If it is defined outside of its
1126 1.1 mrg // class, then we need match these constraints against those of
1127 1.1 mrg // original declaration.
1128 1.1 mrg if (tree orig_ci = get_constraints (decl))
1129 1.1 mrg {
1130 1.1 mrg if (!equivalent_constraints (ci, orig_ci))
1131 1.1 mrg {
1132 1.1 mrg // FIXME: Improve diagnostics.
1133 1.1 mrg error ("%qT does not match any declaration", type);
1134 1.1 mrg return error_mark_node;
1135 1.1 mrg }
1136 1.1 mrg return type;
1137 1.1 mrg }
1138 1.1 mrg set_constraints (decl, ci);
1139 1.1 mrg }
1140 1.1 mrg return type;
1141 1.1 mrg }
1142 1.1 mrg
1143 1.1 mrg namespace {
1144 1.1 mrg
1145 1.1 mrg // Create an empty constraint info block.
1146 1.1 mrg inline tree_constraint_info*
1147 1.1 mrg build_constraint_info ()
1148 1.1 mrg {
1149 1.1 mrg return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
1150 1.1 mrg }
1151 1.1 mrg
1152 1.1 mrg } // namespace
1153 1.1 mrg
1154 1.1 mrg /* Build a constraint-info object that contains the associated constraints
1155 1.1 mrg of a declaration. This also includes the declaration's template
1156 1.1 mrg requirements (TREQS) and any trailing requirements for a function
1157 1.1 mrg declarator (DREQS). Note that both TREQS and DREQS must be constraints.
1158 1.1 mrg
1159 1.1 mrg If the declaration has neither template nor declaration requirements
1160 1.1 mrg this returns NULL_TREE, indicating an unconstrained declaration. */
1161 1.1 mrg
1162 1.1 mrg tree
1163 1.1 mrg build_constraints (tree tmpl_reqs, tree decl_reqs)
1164 1.1 mrg {
1165 1.1 mrg gcc_assert (tmpl_reqs ? constraint_p (tmpl_reqs) : true);
1166 1.1 mrg gcc_assert (decl_reqs ? constraint_p (decl_reqs) : true);
1167 1.1 mrg
1168 1.1 mrg if (!tmpl_reqs && !decl_reqs)
1169 1.1 mrg return NULL_TREE;
1170 1.1 mrg
1171 1.1 mrg tree_constraint_info* ci = build_constraint_info ();
1172 1.1 mrg ci->template_reqs = tmpl_reqs;
1173 1.1 mrg ci->declarator_reqs = decl_reqs;
1174 1.1 mrg ci->associated_constr = conjoin_constraints (tmpl_reqs, decl_reqs);
1175 1.1 mrg
1176 1.1 mrg return (tree)ci;
1177 1.1 mrg }
1178 1.1 mrg
1179 1.1 mrg namespace {
1180 1.1 mrg
1181 1.1 mrg /* Construct a sequence of template arguments by prepending
1182 1.1 mrg ARG to REST. Either ARG or REST may be null. */
1183 1.1 mrg tree
1184 1.1 mrg build_concept_check_arguments (tree arg, tree rest)
1185 1.1 mrg {
1186 1.1 mrg gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
1187 1.1 mrg tree args;
1188 1.1 mrg if (arg)
1189 1.1 mrg {
1190 1.1 mrg int n = rest ? TREE_VEC_LENGTH (rest) : 0;
1191 1.1 mrg args = make_tree_vec (n + 1);
1192 1.1 mrg TREE_VEC_ELT (args, 0) = arg;
1193 1.1 mrg if (rest)
1194 1.1 mrg for (int i = 0; i < n; ++i)
1195 1.1 mrg TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
1196 1.1 mrg int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
1197 1.1 mrg SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
1198 1.1 mrg }
1199 1.1 mrg else
1200 1.1 mrg {
1201 1.1 mrg gcc_assert (rest != NULL_TREE);
1202 1.1 mrg args = rest;
1203 1.1 mrg }
1204 1.1 mrg return args;
1205 1.1 mrg }
1206 1.1 mrg
1207 1.1 mrg } // namespace
1208 1.1 mrg
1209 1.1 mrg /* Construct an expression that checks the concept given by
1210 1.1 mrg TARGET. The TARGET must be:
1211 1.1 mrg
1212 1.1 mrg - an OVERLOAD referring to one or more function concepts
1213 1.1 mrg - a BASELINK referring to an overload set of the above, or
1214 1.1 mrg - a TEMPLTATE_DECL referring to a variable concept.
1215 1.1 mrg
1216 1.1 mrg ARG and REST are the explicit template arguments for the
1217 1.1 mrg eventual concept check. */
1218 1.1 mrg tree
1219 1.1 mrg build_concept_check (tree target, tree arg, tree rest)
1220 1.1 mrg {
1221 1.1 mrg tree args = build_concept_check_arguments (arg, rest);
1222 1.1 mrg if (variable_template_p (target))
1223 1.1 mrg return build_variable_check (lookup_template_variable (target, args));
1224 1.1 mrg else
1225 1.1 mrg return build_call_check (lookup_template_function (target, args));
1226 1.1 mrg }
1227 1.1 mrg
1228 1.1 mrg
1229 1.1 mrg /* Returns a TYPE_DECL that contains sufficient information to
1230 1.1 mrg build a template parameter of the same kind as PROTO and
1231 1.1 mrg constrained by the concept declaration CNC. Note that PROTO
1232 1.1 mrg is the first template parameter of CNC.
1233 1.1 mrg
1234 1.1 mrg If specified, ARGS provides additional arguments to the
1235 1.1 mrg constraint check. */
1236 1.1 mrg tree
1237 1.1 mrg build_constrained_parameter (tree cnc, tree proto, tree args)
1238 1.1 mrg {
1239 1.1 mrg tree name = DECL_NAME (cnc);
1240 1.1 mrg tree type = TREE_TYPE (proto);
1241 1.1 mrg tree decl = build_decl (input_location, TYPE_DECL, name, type);
1242 1.1 mrg CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
1243 1.1 mrg CONSTRAINED_PARM_CONCEPT (decl) = cnc;
1244 1.1 mrg CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
1245 1.1 mrg return decl;
1246 1.1 mrg }
1247 1.1 mrg
1248 1.1 mrg /* Create a constraint expression for the given DECL that
1249 1.1 mrg evaluates the requirements specified by CONSTR, a TYPE_DECL
1250 1.1 mrg that contains all the information necessary to build the
1251 1.1 mrg requirements (see finish_concept_name for the layout of
1252 1.1 mrg that TYPE_DECL).
1253 1.1 mrg
1254 1.1 mrg Note that the constraints are neither reduced nor decomposed.
1255 1.1 mrg That is done only after the requires clause has been parsed
1256 1.1 mrg (or not).
1257 1.1 mrg
1258 1.1 mrg This will always return a CHECK_CONSTR. */
1259 1.1 mrg tree
1260 1.1 mrg finish_shorthand_constraint (tree decl, tree constr)
1261 1.1 mrg {
1262 1.1 mrg /* No requirements means no constraints. */
1263 1.1 mrg if (!constr)
1264 1.1 mrg return NULL_TREE;
1265 1.1 mrg
1266 1.1 mrg tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
1267 1.1 mrg tree con = CONSTRAINED_PARM_CONCEPT (constr);
1268 1.1 mrg tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
1269 1.1 mrg
1270 1.1 mrg /* If the parameter declaration is variadic, but the concept
1271 1.1 mrg is not then we need to apply the concept to every element
1272 1.1 mrg in the pack. */
1273 1.1 mrg bool is_proto_pack = template_parameter_pack_p (proto);
1274 1.1 mrg bool is_decl_pack = template_parameter_pack_p (decl);
1275 1.1 mrg bool apply_to_all_p = is_decl_pack && !is_proto_pack;
1276 1.1 mrg
1277 1.1 mrg /* Get the argument and overload used for the requirement
1278 1.1 mrg and adjust it if we're going to expand later. */
1279 1.1 mrg tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
1280 1.1 mrg if (apply_to_all_p)
1281 1.1 mrg arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
1282 1.1 mrg
1283 1.1 mrg /* Build the concept check. If it the constraint needs to be
1284 1.1 mrg applied to all elements of the parameter pack, then make
1285 1.1 mrg the constraint an expansion. */
1286 1.1 mrg tree check;
1287 1.1 mrg tree tmpl = DECL_TI_TEMPLATE (con);
1288 1.1 mrg if (TREE_CODE (con) == VAR_DECL)
1289 1.1 mrg {
1290 1.1 mrg check = build_concept_check (tmpl, arg, args);
1291 1.1 mrg }
1292 1.1 mrg else
1293 1.1 mrg {
1294 1.1 mrg tree ovl = build_overload (tmpl, NULL_TREE);
1295 1.1 mrg check = build_concept_check (ovl, arg, args);
1296 1.1 mrg }
1297 1.1 mrg
1298 1.1 mrg /* Make the check a pack expansion if needed.
1299 1.1 mrg
1300 1.1 mrg FIXME: We should be making a fold expression. */
1301 1.1 mrg if (apply_to_all_p)
1302 1.1 mrg {
1303 1.1 mrg check = make_pack_expansion (check);
1304 1.1 mrg TREE_TYPE (check) = boolean_type_node;
1305 1.1 mrg }
1306 1.1 mrg
1307 1.1 mrg return normalize_expression (check);
1308 1.1 mrg }
1309 1.1 mrg
1310 1.1 mrg /* Returns a conjunction of shorthand requirements for the template
1311 1.1 mrg parameter list PARMS. Note that the requirements are stored in
1312 1.1 mrg the TYPE of each tree node. */
1313 1.1 mrg tree
1314 1.1 mrg get_shorthand_constraints (tree parms)
1315 1.1 mrg {
1316 1.1 mrg tree result = NULL_TREE;
1317 1.1 mrg parms = INNERMOST_TEMPLATE_PARMS (parms);
1318 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
1319 1.1 mrg {
1320 1.1 mrg tree parm = TREE_VEC_ELT (parms, i);
1321 1.1 mrg tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
1322 1.1 mrg result = conjoin_constraints (result, constr);
1323 1.1 mrg }
1324 1.1 mrg return result;
1325 1.1 mrg }
1326 1.1 mrg
1327 1.1 mrg // Returns and chains a new parameter for PARAMETER_LIST which will conform
1328 1.1 mrg // to the prototype given by SRC_PARM. The new parameter will have its
1329 1.1 mrg // identifier and location set according to IDENT and PARM_LOC respectively.
1330 1.1 mrg static tree
1331 1.1 mrg process_introduction_parm (tree parameter_list, tree src_parm)
1332 1.1 mrg {
1333 1.1 mrg // If we have a pack, we should have a single pack argument which is the
1334 1.1 mrg // placeholder we want to look at.
1335 1.1 mrg bool is_parameter_pack = ARGUMENT_PACK_P (src_parm);
1336 1.1 mrg if (is_parameter_pack)
1337 1.1 mrg src_parm = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm), 0);
1338 1.1 mrg
1339 1.1 mrg // At this point we should have a wildcard, but we want to
1340 1.1 mrg // grab the associated decl from it. Also grab the stored
1341 1.1 mrg // identifier and location that should be chained to it in
1342 1.1 mrg // a PARM_DECL.
1343 1.1 mrg gcc_assert (TREE_CODE (src_parm) == WILDCARD_DECL);
1344 1.1 mrg
1345 1.1 mrg tree ident = DECL_NAME (src_parm);
1346 1.1 mrg location_t parm_loc = DECL_SOURCE_LOCATION (src_parm);
1347 1.1 mrg
1348 1.1 mrg // If we expect a pack and the deduced template is not a pack, or if the
1349 1.1 mrg // template is using a pack and we didn't declare a pack, throw an error.
1350 1.1 mrg if (is_parameter_pack != WILDCARD_PACK_P (src_parm))
1351 1.1 mrg {
1352 1.1 mrg error_at (parm_loc, "cannot match pack for introduced parameter");
1353 1.1 mrg tree err_parm = build_tree_list (error_mark_node, error_mark_node);
1354 1.1 mrg return chainon (parameter_list, err_parm);
1355 1.1 mrg }
1356 1.1 mrg
1357 1.1 mrg src_parm = TREE_TYPE (src_parm);
1358 1.1 mrg
1359 1.1 mrg tree parm;
1360 1.1 mrg bool is_non_type;
1361 1.1 mrg if (TREE_CODE (src_parm) == TYPE_DECL)
1362 1.1 mrg {
1363 1.1 mrg is_non_type = false;
1364 1.1 mrg parm = finish_template_type_parm (class_type_node, ident);
1365 1.1 mrg }
1366 1.1 mrg else if (TREE_CODE (src_parm) == TEMPLATE_DECL)
1367 1.1 mrg {
1368 1.1 mrg is_non_type = false;
1369 1.1 mrg begin_template_parm_list ();
1370 1.1 mrg current_template_parms = DECL_TEMPLATE_PARMS (src_parm);
1371 1.1 mrg end_template_parm_list ();
1372 1.1 mrg parm = finish_template_template_parm (class_type_node, ident);
1373 1.1 mrg }
1374 1.1 mrg else
1375 1.1 mrg {
1376 1.1 mrg is_non_type = true;
1377 1.1 mrg
1378 1.1 mrg // Since we don't have a declarator, so we can copy the source
1379 1.1 mrg // parameter and change the name and eventually the location.
1380 1.1 mrg parm = copy_decl (src_parm);
1381 1.1 mrg DECL_NAME (parm) = ident;
1382 1.1 mrg }
1383 1.1 mrg
1384 1.1 mrg // Wrap in a TREE_LIST for process_template_parm. Introductions do not
1385 1.1 mrg // retain the defaults from the source template.
1386 1.1 mrg parm = build_tree_list (NULL_TREE, parm);
1387 1.1 mrg
1388 1.1 mrg return process_template_parm (parameter_list, parm_loc, parm,
1389 1.1 mrg is_non_type, is_parameter_pack);
1390 1.1 mrg }
1391 1.1 mrg
1392 1.1 mrg /* Associates a constraint check to the current template based
1393 1.1 mrg on the introduction parameters. INTRO_LIST must be a TREE_VEC
1394 1.1 mrg of WILDCARD_DECLs containing a chained PARM_DECL which
1395 1.1 mrg contains the identifier as well as the source location.
1396 1.1 mrg TMPL_DECL is the decl for the concept being used. If we
1397 1.1 mrg take a concept, C, this will form a check in the form of
1398 1.1 mrg C<INTRO_LIST> filling in any extra arguments needed by the
1399 1.1 mrg defaults deduced.
1400 1.1 mrg
1401 1.1 mrg Returns NULL_TREE if no concept could be matched and
1402 1.1 mrg error_mark_node if an error occurred when matching. */
1403 1.1 mrg tree
1404 1.1 mrg finish_template_introduction (tree tmpl_decl, tree intro_list)
1405 1.1 mrg {
1406 1.1 mrg /* Deduce the concept check. */
1407 1.1 mrg tree expr = build_concept_check (tmpl_decl, NULL_TREE, intro_list);
1408 1.1 mrg if (expr == error_mark_node)
1409 1.1 mrg return NULL_TREE;
1410 1.1 mrg
1411 1.1 mrg tree parms = deduce_concept_introduction (expr);
1412 1.1 mrg if (!parms)
1413 1.1 mrg return NULL_TREE;
1414 1.1 mrg
1415 1.1 mrg /* Build template parameter scope for introduction. */
1416 1.1 mrg tree parm_list = NULL_TREE;
1417 1.1 mrg begin_template_parm_list ();
1418 1.1 mrg int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
1419 1.1 mrg for (int n = 0; n < nargs; ++n)
1420 1.1 mrg parm_list = process_introduction_parm (parm_list, TREE_VEC_ELT (parms, n));
1421 1.1 mrg parm_list = end_template_parm_list (parm_list);
1422 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
1423 1.1 mrg if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
1424 1.1 mrg {
1425 1.1 mrg end_template_decl ();
1426 1.1 mrg return error_mark_node;
1427 1.1 mrg }
1428 1.1 mrg
1429 1.1 mrg /* Build a concept check for our constraint. */
1430 1.1 mrg tree check_args = make_tree_vec (TREE_VEC_LENGTH (parms));
1431 1.1 mrg int n = 0;
1432 1.1 mrg for (; n < TREE_VEC_LENGTH (parm_list); ++n)
1433 1.1 mrg {
1434 1.1 mrg tree parm = TREE_VEC_ELT (parm_list, n);
1435 1.1 mrg TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
1436 1.1 mrg }
1437 1.1 mrg SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args, n);
1438 1.1 mrg
1439 1.1 mrg /* If the template expects more parameters we should be able
1440 1.1 mrg to use the defaults from our deduced concept. */
1441 1.1 mrg for (; n < TREE_VEC_LENGTH (parms); ++n)
1442 1.1 mrg TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
1443 1.1 mrg
1444 1.1 mrg /* Associate the constraint. */
1445 1.1 mrg tree check = build_concept_check (tmpl_decl, NULL_TREE, check_args);
1446 1.1 mrg tree constr = normalize_expression (check);
1447 1.1 mrg TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = constr;
1448 1.1 mrg
1449 1.1 mrg return parm_list;
1450 1.1 mrg }
1451 1.1 mrg
1452 1.1 mrg
1453 1.1 mrg /* Given the predicate constraint T from a constrained-type-specifier, extract
1454 1.1 mrg its TMPL and ARGS. FIXME why do we need two different forms of
1455 1.1 mrg constrained-type-specifier? */
1456 1.1 mrg
1457 1.1 mrg void
1458 1.1 mrg placeholder_extract_concept_and_args (tree t, tree &tmpl, tree &args)
1459 1.1 mrg {
1460 1.1 mrg if (TREE_CODE (t) == TYPE_DECL)
1461 1.1 mrg {
1462 1.1 mrg /* A constrained parameter. Build a constraint check
1463 1.1 mrg based on the prototype parameter and then extract the
1464 1.1 mrg arguments from that. */
1465 1.1 mrg tree proto = CONSTRAINED_PARM_PROTOTYPE (t);
1466 1.1 mrg tree check = finish_shorthand_constraint (proto, t);
1467 1.1 mrg placeholder_extract_concept_and_args (check, tmpl, args);
1468 1.1 mrg return;
1469 1.1 mrg }
1470 1.1 mrg
1471 1.1 mrg if (TREE_CODE (t) == CHECK_CONSTR)
1472 1.1 mrg {
1473 1.1 mrg tree decl = CHECK_CONSTR_CONCEPT (t);
1474 1.1 mrg tmpl = DECL_TI_TEMPLATE (decl);
1475 1.1 mrg args = CHECK_CONSTR_ARGS (t);
1476 1.1 mrg return;
1477 1.1 mrg }
1478 1.1 mrg
1479 1.1 mrg gcc_unreachable ();
1480 1.1 mrg }
1481 1.1 mrg
1482 1.1 mrg /* Returns true iff the placeholders C1 and C2 are equivalent. C1
1483 1.1 mrg and C2 can be either CHECK_CONSTR or TEMPLATE_TYPE_PARM. */
1484 1.1 mrg
1485 1.1 mrg bool
1486 1.1 mrg equivalent_placeholder_constraints (tree c1, tree c2)
1487 1.1 mrg {
1488 1.1 mrg if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
1489 1.1 mrg /* A constrained auto. */
1490 1.1 mrg c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
1491 1.1 mrg if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
1492 1.1 mrg c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
1493 1.1 mrg
1494 1.1 mrg if (c1 == c2)
1495 1.1 mrg return true;
1496 1.1 mrg if (!c1 || !c2)
1497 1.1 mrg return false;
1498 1.1 mrg if (c1 == error_mark_node || c2 == error_mark_node)
1499 1.1 mrg /* We get here during satisfaction; when a deduction constraint
1500 1.1 mrg fails, substitution can produce an error_mark_node for the
1501 1.1 mrg placeholder constraints. */
1502 1.1 mrg return false;
1503 1.1 mrg
1504 1.1 mrg tree t1, t2, a1, a2;
1505 1.1 mrg placeholder_extract_concept_and_args (c1, t1, a1);
1506 1.1 mrg placeholder_extract_concept_and_args (c2, t2, a2);
1507 1.1 mrg
1508 1.1 mrg if (t1 != t2)
1509 1.1 mrg return false;
1510 1.1 mrg
1511 1.1 mrg int len1 = TREE_VEC_LENGTH (a1);
1512 1.1 mrg int len2 = TREE_VEC_LENGTH (a2);
1513 1.1 mrg if (len1 != len2)
1514 1.1 mrg return false;
1515 1.1 mrg
1516 1.1 mrg /* Skip the first argument so we don't infinitely recurse.
1517 1.1 mrg Also, they may differ in template parameter index. */
1518 1.1 mrg for (int i = 1; i < len1; ++i)
1519 1.1 mrg {
1520 1.1 mrg tree t1 = TREE_VEC_ELT (a1, i);
1521 1.1 mrg tree t2 = TREE_VEC_ELT (a2, i);
1522 1.1 mrg if (!template_args_equal (t1, t2))
1523 1.1 mrg return false;
1524 1.1 mrg }
1525 1.1 mrg return true;
1526 1.1 mrg }
1527 1.1 mrg
1528 1.1 mrg /* Return a hash value for the placeholder PRED_CONSTR C. */
1529 1.1 mrg
1530 1.1 mrg hashval_t
1531 1.1 mrg hash_placeholder_constraint (tree c)
1532 1.1 mrg {
1533 1.1 mrg tree t, a;
1534 1.1 mrg placeholder_extract_concept_and_args (c, t, a);
1535 1.1 mrg
1536 1.1 mrg /* Like hash_tmpl_and_args, but skip the first argument. */
1537 1.1 mrg hashval_t val = iterative_hash_object (DECL_UID (t), 0);
1538 1.1 mrg
1539 1.1 mrg for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
1540 1.1 mrg val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
1541 1.1 mrg
1542 1.1 mrg return val;
1543 1.1 mrg }
1544 1.1 mrg
1545 1.1 mrg /*---------------------------------------------------------------------------
1546 1.1 mrg Constraint substitution
1547 1.1 mrg ---------------------------------------------------------------------------*/
1548 1.1 mrg
1549 1.1 mrg /* The following functions implement substitution rules for constraints.
1550 1.1 mrg Substitution without checking constraints happens only in the
1551 1.1 mrg instantiation of class templates. For example:
1552 1.1 mrg
1553 1.1 mrg template<C1 T> struct S {
1554 1.1 mrg void f(T) requires C2<T>;
1555 1.1 mrg void g(T) requires T::value;
1556 1.1 mrg };
1557 1.1 mrg
1558 1.1 mrg S<int> s; // error instantiating S<int>::g(T)
1559 1.1 mrg
1560 1.1 mrg When we instantiate S, we substitute into its member declarations,
1561 1.1 mrg including their constraints. However, those constraints are not
1562 1.1 mrg checked. Substituting int into C2<T> yields C2<int>, and substituting
1563 1.1 mrg into T::value yields a substitution failure, making the program
1564 1.1 mrg ill-formed.
1565 1.1 mrg
1566 1.1 mrg Note that we only ever substitute into the associated constraints
1567 1.1 mrg of a declaration. That is, substitution is defined only for predicate
1568 1.1 mrg constraints and conjunctions. */
1569 1.1 mrg
1570 1.1 mrg /* Substitute into the predicate constraints. Returns error_mark_node
1571 1.1 mrg if the substitution into the expression fails. */
1572 1.1 mrg tree
1573 1.1 mrg tsubst_predicate_constraint (tree t, tree args,
1574 1.1 mrg tsubst_flags_t complain, tree in_decl)
1575 1.1 mrg {
1576 1.1 mrg tree expr = PRED_CONSTR_EXPR (t);
1577 1.1 mrg ++processing_template_decl;
1578 1.1 mrg tree result = tsubst_expr (expr, args, complain, in_decl, false);
1579 1.1 mrg --processing_template_decl;
1580 1.1 mrg return build_nt (PRED_CONSTR, result);
1581 1.1 mrg }
1582 1.1 mrg
1583 1.1 mrg /* Substitute into a check constraint. */
1584 1.1 mrg
1585 1.1 mrg tree
1586 1.1 mrg tsubst_check_constraint (tree t, tree args,
1587 1.1 mrg tsubst_flags_t complain, tree in_decl)
1588 1.1 mrg {
1589 1.1 mrg tree decl = CHECK_CONSTR_CONCEPT (t);
1590 1.1 mrg tree tmpl = DECL_TI_TEMPLATE (decl);
1591 1.1 mrg tree targs = CHECK_CONSTR_ARGS (t);
1592 1.1 mrg
1593 1.1 mrg /* Substitute through by building an template-id expression
1594 1.1 mrg and then substituting into that. */
1595 1.1 mrg tree expr = build_nt(TEMPLATE_ID_EXPR, tmpl, targs);
1596 1.1 mrg ++processing_template_decl;
1597 1.1 mrg tree result = tsubst_expr (expr, args, complain, in_decl, false);
1598 1.1 mrg --processing_template_decl;
1599 1.1 mrg
1600 1.1 mrg if (result == error_mark_node)
1601 1.1 mrg return error_mark_node;
1602 1.1 mrg
1603 1.1 mrg /* Extract the results and rebuild the check constraint. */
1604 1.1 mrg decl = DECL_TEMPLATE_RESULT (TREE_OPERAND (result, 0));
1605 1.1 mrg args = TREE_OPERAND (result, 1);
1606 1.1 mrg
1607 1.1 mrg return build_nt (CHECK_CONSTR, decl, args);
1608 1.1 mrg }
1609 1.1 mrg
1610 1.1 mrg /* Substitute into the conjunction of constraints. Returns
1611 1.1 mrg error_mark_node if substitution into either operand fails. */
1612 1.1 mrg
1613 1.1 mrg tree
1614 1.1 mrg tsubst_logical_operator (tree t, tree args,
1615 1.1 mrg tsubst_flags_t complain, tree in_decl)
1616 1.1 mrg {
1617 1.1 mrg tree t0 = TREE_OPERAND (t, 0);
1618 1.1 mrg tree r0 = tsubst_constraint (t0, args, complain, in_decl);
1619 1.1 mrg if (r0 == error_mark_node)
1620 1.1 mrg return error_mark_node;
1621 1.1 mrg tree t1 = TREE_OPERAND (t, 1);
1622 1.1 mrg tree r1 = tsubst_constraint (t1, args, complain, in_decl);
1623 1.1 mrg if (r1 == error_mark_node)
1624 1.1 mrg return error_mark_node;
1625 1.1 mrg return build_nt (TREE_CODE (t), r0, r1);
1626 1.1 mrg }
1627 1.1 mrg
1628 1.1 mrg namespace {
1629 1.1 mrg
1630 1.1 mrg /* Substitute ARGS into the expression constraint T. */
1631 1.1 mrg
1632 1.1 mrg tree
1633 1.1 mrg tsubst_expr_constr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1634 1.1 mrg {
1635 1.1 mrg cp_unevaluated guard;
1636 1.1 mrg tree expr = EXPR_CONSTR_EXPR (t);
1637 1.1 mrg tree ret = tsubst_expr (expr, args, complain, in_decl, false);
1638 1.1 mrg if (ret == error_mark_node)
1639 1.1 mrg return error_mark_node;
1640 1.1 mrg return build_nt (EXPR_CONSTR, ret);
1641 1.1 mrg }
1642 1.1 mrg
1643 1.1 mrg /* Substitute ARGS into the type constraint T. */
1644 1.1 mrg
1645 1.1 mrg tree
1646 1.1 mrg tsubst_type_constr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1647 1.1 mrg {
1648 1.1 mrg tree type = TYPE_CONSTR_TYPE (t);
1649 1.1 mrg tree ret = tsubst (type, args, complain, in_decl);
1650 1.1 mrg if (ret == error_mark_node)
1651 1.1 mrg return error_mark_node;
1652 1.1 mrg return build_nt (TYPE_CONSTR, ret);
1653 1.1 mrg }
1654 1.1 mrg
1655 1.1 mrg /* Substitute ARGS into the implicit conversion constraint T. */
1656 1.1 mrg
1657 1.1 mrg tree
1658 1.1 mrg tsubst_implicit_conversion_constr (tree t, tree args, tsubst_flags_t complain,
1659 1.1 mrg tree in_decl)
1660 1.1 mrg {
1661 1.1 mrg cp_unevaluated guard;
1662 1.1 mrg tree expr = ICONV_CONSTR_EXPR (t);
1663 1.1 mrg tree type = ICONV_CONSTR_TYPE (t);
1664 1.1 mrg tree new_expr = tsubst_expr (expr, args, complain, in_decl, false);
1665 1.1 mrg if (new_expr == error_mark_node)
1666 1.1 mrg return error_mark_node;
1667 1.1 mrg tree new_type = tsubst (type, args, complain, in_decl);
1668 1.1 mrg if (new_type == error_mark_node)
1669 1.1 mrg return error_mark_node;
1670 1.1 mrg return build_nt (ICONV_CONSTR, new_expr, new_type);
1671 1.1 mrg }
1672 1.1 mrg
1673 1.1 mrg /* Substitute ARGS into the argument deduction constraint T. */
1674 1.1 mrg
1675 1.1 mrg tree
1676 1.1 mrg tsubst_argument_deduction_constr (tree t, tree args, tsubst_flags_t complain,
1677 1.1 mrg tree in_decl)
1678 1.1 mrg {
1679 1.1 mrg cp_unevaluated guard;
1680 1.1 mrg tree expr = DEDUCT_CONSTR_EXPR (t);
1681 1.1 mrg tree pattern = DEDUCT_CONSTR_PATTERN (t);
1682 1.1 mrg tree autos = DEDUCT_CONSTR_PLACEHOLDER(t);
1683 1.1 mrg tree new_expr = tsubst_expr (expr, args, complain, in_decl, false);
1684 1.1 mrg if (new_expr == error_mark_node)
1685 1.1 mrg return error_mark_node;
1686 1.1 mrg /* It seems like substituting through the pattern will not affect the
1687 1.1 mrg placeholders. We should (?) be able to reuse the existing list
1688 1.1 mrg without any problems. If not, then we probably want to create a
1689 1.1 mrg new list of placeholders and then instantiate the pattern using
1690 1.1 mrg those. */
1691 1.1 mrg tree new_pattern = tsubst (pattern, args, complain, in_decl);
1692 1.1 mrg if (new_pattern == error_mark_node)
1693 1.1 mrg return error_mark_node;
1694 1.1 mrg return build_nt (DEDUCT_CONSTR, new_expr, new_pattern, autos);
1695 1.1 mrg }
1696 1.1 mrg
1697 1.1 mrg /* Substitute ARGS into the exception constraint T. */
1698 1.1 mrg
1699 1.1 mrg tree
1700 1.1 mrg tsubst_exception_constr (tree t, tree args, tsubst_flags_t complain,
1701 1.1 mrg tree in_decl)
1702 1.1 mrg {
1703 1.1 mrg cp_unevaluated guard;
1704 1.1 mrg tree expr = EXCEPT_CONSTR_EXPR (t);
1705 1.1 mrg tree ret = tsubst_expr (expr, args, complain, in_decl, false);
1706 1.1 mrg if (ret == error_mark_node)
1707 1.1 mrg return error_mark_node;
1708 1.1 mrg return build_nt (EXCEPT_CONSTR, ret);
1709 1.1 mrg }
1710 1.1 mrg
1711 1.1 mrg /* A subroutine of tsubst_constraint_variables. Register local
1712 1.1 mrg specializations for each of parameter in PARMS and its
1713 1.1 mrg corresponding substituted constraint variable in VARS.
1714 1.1 mrg Returns VARS. */
1715 1.1 mrg
1716 1.1 mrg tree
1717 1.1 mrg declare_constraint_vars (tree parms, tree vars)
1718 1.1 mrg {
1719 1.1 mrg tree s = vars;
1720 1.1 mrg for (tree t = parms; t; t = DECL_CHAIN (t))
1721 1.1 mrg {
1722 1.1 mrg if (DECL_PACK_P (t))
1723 1.1 mrg {
1724 1.1 mrg tree pack = extract_fnparm_pack (t, &s);
1725 1.1 mrg register_local_specialization (pack, t);
1726 1.1 mrg }
1727 1.1 mrg else
1728 1.1 mrg {
1729 1.1 mrg register_local_specialization (s, t);
1730 1.1 mrg s = DECL_CHAIN (s);
1731 1.1 mrg }
1732 1.1 mrg }
1733 1.1 mrg return vars;
1734 1.1 mrg }
1735 1.1 mrg
1736 1.1 mrg /* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
1737 1.1 mrg into the parameter list T, producing a sequence of constraint
1738 1.1 mrg variables, declared in the current scope.
1739 1.1 mrg
1740 1.1 mrg Note that the caller must establish a local specialization stack
1741 1.1 mrg prior to calling this function since this substitution will
1742 1.1 mrg declare the substituted parameters. */
1743 1.1 mrg
1744 1.1 mrg tree
1745 1.1 mrg tsubst_constraint_variables (tree t, tree args,
1746 1.1 mrg tsubst_flags_t complain, tree in_decl)
1747 1.1 mrg {
1748 1.1 mrg /* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
1749 1.1 mrg of PARM_DECLs. */
1750 1.1 mrg int saved_unevaluated_operand = cp_unevaluated_operand;
1751 1.1 mrg cp_unevaluated_operand = 0;
1752 1.1 mrg tree vars = tsubst (t, args, complain, in_decl);
1753 1.1 mrg cp_unevaluated_operand = saved_unevaluated_operand;
1754 1.1 mrg if (vars == error_mark_node)
1755 1.1 mrg return error_mark_node;
1756 1.1 mrg return declare_constraint_vars (t, vars);
1757 1.1 mrg }
1758 1.1 mrg
1759 1.1 mrg /* Substitute ARGS into the parameterized constraint T. */
1760 1.1 mrg
1761 1.1 mrg tree
1762 1.1 mrg tsubst_parameterized_constraint (tree t, tree args,
1763 1.1 mrg tsubst_flags_t complain, tree in_decl)
1764 1.1 mrg {
1765 1.1 mrg local_specialization_stack stack;
1766 1.1 mrg tree vars = tsubst_constraint_variables (PARM_CONSTR_PARMS (t),
1767 1.1 mrg args, complain, in_decl);
1768 1.1 mrg if (vars == error_mark_node)
1769 1.1 mrg return error_mark_node;
1770 1.1 mrg tree expr = tsubst_constraint (PARM_CONSTR_OPERAND (t), args,
1771 1.1 mrg complain, in_decl);
1772 1.1 mrg if (expr == error_mark_node)
1773 1.1 mrg return error_mark_node;
1774 1.1 mrg return build_nt (PARM_CONSTR, vars, expr);
1775 1.1 mrg }
1776 1.1 mrg
1777 1.1 mrg /* Substitute ARGS into the simple requirement T. Note that
1778 1.1 mrg substitution may result in an ill-formed expression without
1779 1.1 mrg causing the program to be ill-formed. In such cases, the
1780 1.1 mrg requirement wraps an error_mark_node. */
1781 1.1 mrg
1782 1.1 mrg inline tree
1783 1.1 mrg tsubst_simple_requirement (tree t, tree args,
1784 1.1 mrg tsubst_flags_t complain, tree in_decl)
1785 1.1 mrg {
1786 1.1 mrg ++processing_template_decl;
1787 1.1 mrg tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1788 1.1 mrg --processing_template_decl;
1789 1.1 mrg return finish_simple_requirement (expr);
1790 1.1 mrg }
1791 1.1 mrg
1792 1.1 mrg /* Substitute ARGS into the type requirement T. Note that
1793 1.1 mrg substitution may result in an ill-formed type without
1794 1.1 mrg causing the program to be ill-formed. In such cases, the
1795 1.1 mrg requirement wraps an error_mark_node. */
1796 1.1 mrg
1797 1.1 mrg inline tree
1798 1.1 mrg tsubst_type_requirement (tree t, tree args,
1799 1.1 mrg tsubst_flags_t complain, tree in_decl)
1800 1.1 mrg {
1801 1.1 mrg ++processing_template_decl;
1802 1.1 mrg tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
1803 1.1 mrg --processing_template_decl;
1804 1.1 mrg return finish_type_requirement (type);
1805 1.1 mrg }
1806 1.1 mrg
1807 1.1 mrg /* Substitute args into the compound requirement T. If substituting
1808 1.1 mrg into either the expression or the type fails, the corresponding
1809 1.1 mrg operands in the resulting node will be error_mark_node. This
1810 1.1 mrg preserves a requirement for the purpose of partial ordering, but
1811 1.1 mrg it will never be satisfied. */
1812 1.1 mrg
1813 1.1 mrg tree
1814 1.1 mrg tsubst_compound_requirement (tree t, tree args,
1815 1.1 mrg tsubst_flags_t complain, tree in_decl)
1816 1.1 mrg {
1817 1.1 mrg ++processing_template_decl;
1818 1.1 mrg tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1819 1.1 mrg tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
1820 1.1 mrg --processing_template_decl;
1821 1.1 mrg bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
1822 1.1 mrg return finish_compound_requirement (expr, type, noexcept_p);
1823 1.1 mrg }
1824 1.1 mrg
1825 1.1 mrg /* Substitute ARGS into the nested requirement T. */
1826 1.1 mrg
1827 1.1 mrg tree
1828 1.1 mrg tsubst_nested_requirement (tree t, tree args,
1829 1.1 mrg tsubst_flags_t complain, tree in_decl)
1830 1.1 mrg {
1831 1.1 mrg ++processing_template_decl;
1832 1.1 mrg tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
1833 1.1 mrg --processing_template_decl;
1834 1.1 mrg return finish_nested_requirement (expr);
1835 1.1 mrg }
1836 1.1 mrg
1837 1.1 mrg /* Substitute ARGS into the requirement T. */
1838 1.1 mrg
1839 1.1 mrg inline tree
1840 1.1 mrg tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1841 1.1 mrg {
1842 1.1 mrg switch (TREE_CODE (t))
1843 1.1 mrg {
1844 1.1 mrg case SIMPLE_REQ:
1845 1.1 mrg return tsubst_simple_requirement (t, args, complain, in_decl);
1846 1.1 mrg case TYPE_REQ:
1847 1.1 mrg return tsubst_type_requirement (t, args, complain, in_decl);
1848 1.1 mrg case COMPOUND_REQ:
1849 1.1 mrg return tsubst_compound_requirement (t, args, complain, in_decl);
1850 1.1 mrg case NESTED_REQ:
1851 1.1 mrg return tsubst_nested_requirement (t, args, complain, in_decl);
1852 1.1 mrg default:
1853 1.1 mrg gcc_unreachable ();
1854 1.1 mrg }
1855 1.1 mrg return error_mark_node;
1856 1.1 mrg }
1857 1.1 mrg
1858 1.1 mrg /* Substitute ARGS into the list of requirements T. Note that
1859 1.1 mrg substitution failures here result in ill-formed programs. */
1860 1.1 mrg
1861 1.1 mrg tree
1862 1.1 mrg tsubst_requirement_body (tree t, tree args,
1863 1.1 mrg tsubst_flags_t complain, tree in_decl)
1864 1.1 mrg {
1865 1.1 mrg tree r = NULL_TREE;
1866 1.1 mrg while (t)
1867 1.1 mrg {
1868 1.1 mrg tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
1869 1.1 mrg if (e == error_mark_node)
1870 1.1 mrg return error_mark_node;
1871 1.1 mrg r = tree_cons (NULL_TREE, e, r);
1872 1.1 mrg t = TREE_CHAIN (t);
1873 1.1 mrg }
1874 1.1 mrg /* Ensure that the order of constraints is the same as the original. */
1875 1.1 mrg return nreverse (r);
1876 1.1 mrg }
1877 1.1 mrg
1878 1.1 mrg } /* namespace */
1879 1.1 mrg
1880 1.1 mrg /* Substitute ARGS into the requires expression T. Note that this
1881 1.1 mrg results in the re-declaration of local parameters when
1882 1.1 mrg substituting through the parameter list. If either substitution
1883 1.1 mrg fails, the program is ill-formed. */
1884 1.1 mrg
1885 1.1 mrg tree
1886 1.1 mrg tsubst_requires_expr (tree t, tree args,
1887 1.1 mrg tsubst_flags_t complain, tree in_decl)
1888 1.1 mrg {
1889 1.1 mrg local_specialization_stack stack;
1890 1.1 mrg
1891 1.1 mrg tree parms = TREE_OPERAND (t, 0);
1892 1.1 mrg if (parms)
1893 1.1 mrg {
1894 1.1 mrg parms = tsubst_constraint_variables (parms, args, complain, in_decl);
1895 1.1 mrg if (parms == error_mark_node)
1896 1.1 mrg return error_mark_node;
1897 1.1 mrg }
1898 1.1 mrg
1899 1.1 mrg tree reqs = TREE_OPERAND (t, 1);
1900 1.1 mrg reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
1901 1.1 mrg if (reqs == error_mark_node)
1902 1.1 mrg return error_mark_node;
1903 1.1 mrg
1904 1.1 mrg return finish_requires_expr (parms, reqs);
1905 1.1 mrg }
1906 1.1 mrg
1907 1.1 mrg /* Substitute ARGS into the constraint information CI, producing a new
1908 1.1 mrg constraint record. */
1909 1.1 mrg
1910 1.1 mrg tree
1911 1.1 mrg tsubst_constraint_info (tree t, tree args,
1912 1.1 mrg tsubst_flags_t complain, tree in_decl)
1913 1.1 mrg {
1914 1.1 mrg if (!t || t == error_mark_node || !check_constraint_info (t))
1915 1.1 mrg return NULL_TREE;
1916 1.1 mrg
1917 1.1 mrg tree tmpl_constr = NULL_TREE;
1918 1.1 mrg if (tree r = CI_TEMPLATE_REQS (t))
1919 1.1 mrg tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
1920 1.1 mrg
1921 1.1 mrg tree decl_constr = NULL_TREE;
1922 1.1 mrg if (tree r = CI_DECLARATOR_REQS (t))
1923 1.1 mrg decl_constr = tsubst_constraint (r, args, complain, in_decl);
1924 1.1 mrg
1925 1.1 mrg return build_constraints (tmpl_constr, decl_constr);
1926 1.1 mrg }
1927 1.1 mrg
1928 1.1 mrg /* Substitute ARGS into the constraint T. */
1929 1.1 mrg
1930 1.1 mrg tree
1931 1.1 mrg tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
1932 1.1 mrg {
1933 1.1 mrg if (t == NULL_TREE)
1934 1.1 mrg return t;
1935 1.1 mrg switch (TREE_CODE (t))
1936 1.1 mrg {
1937 1.1 mrg case PRED_CONSTR:
1938 1.1 mrg return tsubst_predicate_constraint (t, args, complain, in_decl);
1939 1.1 mrg case CHECK_CONSTR:
1940 1.1 mrg return tsubst_check_constraint (t, args, complain, in_decl);
1941 1.1 mrg case CONJ_CONSTR:
1942 1.1 mrg case DISJ_CONSTR:
1943 1.1 mrg return tsubst_logical_operator (t, args, complain, in_decl);
1944 1.1 mrg case PARM_CONSTR:
1945 1.1 mrg return tsubst_parameterized_constraint (t, args, complain, in_decl);
1946 1.1 mrg case EXPR_CONSTR:
1947 1.1 mrg return tsubst_expr_constr (t, args, complain, in_decl);
1948 1.1 mrg case TYPE_CONSTR:
1949 1.1 mrg return tsubst_type_constr (t, args, complain, in_decl);
1950 1.1 mrg case ICONV_CONSTR:
1951 1.1 mrg return tsubst_implicit_conversion_constr (t, args, complain, in_decl);
1952 1.1 mrg case DEDUCT_CONSTR:
1953 1.1 mrg return tsubst_argument_deduction_constr (t, args, complain, in_decl);
1954 1.1 mrg case EXCEPT_CONSTR:
1955 1.1 mrg return tsubst_exception_constr (t, args, complain, in_decl);
1956 1.1 mrg default:
1957 1.1 mrg gcc_unreachable ();
1958 1.1 mrg }
1959 1.1 mrg return error_mark_node;
1960 1.1 mrg }
1961 1.1 mrg
1962 1.1 mrg /*---------------------------------------------------------------------------
1963 1.1 mrg Constraint satisfaction
1964 1.1 mrg ---------------------------------------------------------------------------*/
1965 1.1 mrg
1966 1.1 mrg /* The following functions determine if a constraint, when
1967 1.1 mrg substituting template arguments, is satisfied. For convenience,
1968 1.1 mrg satisfaction reduces a constraint to either true or false (and
1969 1.1 mrg nothing else). */
1970 1.1 mrg
1971 1.1 mrg namespace {
1972 1.1 mrg
1973 1.1 mrg tree satisfy_constraint_1 (tree, tree, tsubst_flags_t, tree);
1974 1.1 mrg
1975 1.1 mrg /* Check the constraint pack expansion. */
1976 1.1 mrg
1977 1.1 mrg tree
1978 1.1 mrg satisfy_pack_expansion (tree t, tree args,
1979 1.1 mrg tsubst_flags_t complain, tree in_decl)
1980 1.1 mrg {
1981 1.1 mrg /* Get the vector of satisfaction results.
1982 1.1 mrg gen_elem_of_pack_expansion_instantiation will check that each element of
1983 1.1 mrg the expansion is satisfied. */
1984 1.1 mrg tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
1985 1.1 mrg
1986 1.1 mrg if (exprs == error_mark_node)
1987 1.1 mrg return boolean_false_node;
1988 1.1 mrg
1989 1.1 mrg /* TODO: It might be better to normalize each expanded term
1990 1.1 mrg and evaluate them separately. That would provide better
1991 1.1 mrg opportunities for diagnostics. */
1992 1.1 mrg for (int i = 0; i < TREE_VEC_LENGTH (exprs); ++i)
1993 1.1 mrg if (TREE_VEC_ELT (exprs, i) != boolean_true_node)
1994 1.1 mrg return boolean_false_node;
1995 1.1 mrg return boolean_true_node;
1996 1.1 mrg }
1997 1.1 mrg
1998 1.1 mrg /* A predicate constraint is satisfied if its expression evaluates
1999 1.1 mrg to true. If substitution into that node fails, the constraint
2000 1.1 mrg is not satisfied ([temp.constr.pred]).
2001 1.1 mrg
2002 1.1 mrg Note that a predicate constraint is a constraint expression
2003 1.1 mrg of type bool. If neither of those are true, the program is
2004 1.1 mrg ill-formed; they are not SFINAE'able errors. */
2005 1.1 mrg
2006 1.1 mrg tree
2007 1.1 mrg satisfy_predicate_constraint (tree t, tree args,
2008 1.1 mrg tsubst_flags_t complain, tree in_decl)
2009 1.1 mrg {
2010 1.1 mrg tree expr = TREE_OPERAND (t, 0);
2011 1.1 mrg
2012 1.1 mrg /* We should never have a naked pack expansion in a predicate constraint. */
2013 1.1 mrg gcc_assert (TREE_CODE (expr) != EXPR_PACK_EXPANSION);
2014 1.1 mrg
2015 1.1 mrg /* If substitution into the expression fails, the constraint
2016 1.1 mrg is not satisfied. */
2017 1.1 mrg expr = tsubst_expr (expr, args, complain, in_decl, false);
2018 1.1 mrg if (expr == error_mark_node)
2019 1.1 mrg return boolean_false_node;
2020 1.1 mrg
2021 1.1 mrg /* A predicate constraint shall have type bool. In some
2022 1.1 mrg cases, substitution gives us const-qualified bool, which
2023 1.1 mrg is also acceptable. */
2024 1.1 mrg tree type = cv_unqualified (TREE_TYPE (expr));
2025 1.1 mrg if (!same_type_p (type, boolean_type_node))
2026 1.1 mrg {
2027 1.1 mrg error_at (EXPR_LOC_OR_LOC (expr, input_location),
2028 1.1 mrg "constraint %qE does not have type %qT",
2029 1.1 mrg expr, boolean_type_node);
2030 1.1 mrg return boolean_false_node;
2031 1.1 mrg }
2032 1.1 mrg
2033 1.1 mrg return cxx_constant_value (expr);
2034 1.1 mrg }
2035 1.1 mrg
2036 1.1 mrg /* A concept check constraint like C<CARGS> is satisfied if substituting ARGS
2037 1.1 mrg into CARGS succeeds and C is satisfied for the resulting arguments. */
2038 1.1 mrg
2039 1.1 mrg tree
2040 1.1 mrg satisfy_check_constraint (tree t, tree args,
2041 1.1 mrg tsubst_flags_t complain, tree in_decl)
2042 1.1 mrg {
2043 1.1 mrg tree decl = CHECK_CONSTR_CONCEPT (t);
2044 1.1 mrg tree tmpl = DECL_TI_TEMPLATE (decl);
2045 1.1 mrg tree cargs = CHECK_CONSTR_ARGS (t);
2046 1.1 mrg
2047 1.1 mrg /* Instantiate the concept check arguments. */
2048 1.1 mrg tree targs = tsubst (cargs, args, tf_none, NULL_TREE);
2049 1.1 mrg if (targs == error_mark_node)
2050 1.1 mrg return boolean_false_node;
2051 1.1 mrg
2052 1.1 mrg /* Search for a previous value. */
2053 1.1 mrg if (tree prev = lookup_concept_satisfaction (tmpl, targs))
2054 1.1 mrg return prev;
2055 1.1 mrg
2056 1.1 mrg /* Expand the concept; failure here implies non-satisfaction. */
2057 1.1 mrg tree def = expand_concept (decl, targs);
2058 1.1 mrg if (def == error_mark_node)
2059 1.1 mrg return memoize_concept_satisfaction (tmpl, args, boolean_false_node);
2060 1.1 mrg
2061 1.1 mrg /* Recursively satisfy the constraint. */
2062 1.1 mrg tree result = satisfy_constraint_1 (def, targs, complain, in_decl);
2063 1.1 mrg return memoize_concept_satisfaction (tmpl, targs, result);
2064 1.1 mrg }
2065 1.1 mrg
2066 1.1 mrg /* Check an expression constraint. The constraint is satisfied if
2067 1.1 mrg substitution succeeds ([temp.constr.expr]).
2068 1.1 mrg
2069 1.1 mrg Note that the expression is unevaluated. */
2070 1.1 mrg
2071 1.1 mrg tree
2072 1.1 mrg satisfy_expression_constraint (tree t, tree args,
2073 1.1 mrg tsubst_flags_t complain, tree in_decl)
2074 1.1 mrg {
2075 1.1 mrg cp_unevaluated guard;
2076 1.1 mrg deferring_access_check_sentinel deferring;
2077 1.1 mrg
2078 1.1 mrg tree expr = EXPR_CONSTR_EXPR (t);
2079 1.1 mrg tree check = tsubst_expr (expr, args, complain, in_decl, false);
2080 1.1 mrg if (check == error_mark_node)
2081 1.1 mrg return boolean_false_node;
2082 1.1 mrg if (!perform_deferred_access_checks (tf_none))
2083 1.1 mrg return boolean_false_node;
2084 1.1 mrg return boolean_true_node;
2085 1.1 mrg }
2086 1.1 mrg
2087 1.1 mrg /* Check a type constraint. The constraint is satisfied if
2088 1.1 mrg substitution succeeds. */
2089 1.1 mrg
2090 1.1 mrg inline tree
2091 1.1 mrg satisfy_type_constraint (tree t, tree args,
2092 1.1 mrg tsubst_flags_t complain, tree in_decl)
2093 1.1 mrg {
2094 1.1 mrg deferring_access_check_sentinel deferring;
2095 1.1 mrg tree type = TYPE_CONSTR_TYPE (t);
2096 1.1 mrg gcc_assert (TYPE_P (type) || type == error_mark_node);
2097 1.1 mrg tree check = tsubst (type, args, complain, in_decl);
2098 1.1 mrg if (error_operand_p (check))
2099 1.1 mrg return boolean_false_node;
2100 1.1 mrg if (!perform_deferred_access_checks (complain))
2101 1.1 mrg return boolean_false_node;
2102 1.1 mrg return boolean_true_node;
2103 1.1 mrg }
2104 1.1 mrg
2105 1.1 mrg /* Check an implicit conversion constraint. */
2106 1.1 mrg
2107 1.1 mrg tree
2108 1.1 mrg satisfy_implicit_conversion_constraint (tree t, tree args,
2109 1.1 mrg tsubst_flags_t complain, tree in_decl)
2110 1.1 mrg {
2111 1.1 mrg /* Don't tsubst as if we're processing a template. If we try
2112 1.1 mrg to we can end up generating template-like expressions
2113 1.1 mrg (e.g., modop-exprs) that aren't properly typed. */
2114 1.1 mrg tree expr =
2115 1.1 mrg tsubst_expr (ICONV_CONSTR_EXPR (t), args, complain, in_decl, false);
2116 1.1 mrg if (expr == error_mark_node)
2117 1.1 mrg return boolean_false_node;
2118 1.1 mrg
2119 1.1 mrg /* Get the transformed target type. */
2120 1.1 mrg tree type = tsubst (ICONV_CONSTR_TYPE (t), args, complain, in_decl);
2121 1.1 mrg if (type == error_mark_node)
2122 1.1 mrg return boolean_false_node;
2123 1.1 mrg
2124 1.1 mrg /* Attempt the conversion as a direct initialization
2125 1.1 mrg of the form TYPE <unspecified> = EXPR. */
2126 1.1 mrg tree conv =
2127 1.1 mrg perform_direct_initialization_if_possible (type, expr, false, complain);
2128 1.1 mrg if (conv == NULL_TREE || conv == error_mark_node)
2129 1.1 mrg return boolean_false_node;
2130 1.1 mrg else
2131 1.1 mrg return boolean_true_node;
2132 1.1 mrg }
2133 1.1 mrg
2134 1.1 mrg /* Check an argument deduction constraint. */
2135 1.1 mrg
2136 1.1 mrg tree
2137 1.1 mrg satisfy_argument_deduction_constraint (tree t, tree args,
2138 1.1 mrg tsubst_flags_t complain, tree in_decl)
2139 1.1 mrg {
2140 1.1 mrg /* Substitute through the expression. */
2141 1.1 mrg tree expr = DEDUCT_CONSTR_EXPR (t);
2142 1.1 mrg tree init = tsubst_expr (expr, args, complain, in_decl, false);
2143 1.1 mrg if (expr == error_mark_node)
2144 1.1 mrg return boolean_false_node;
2145 1.1 mrg
2146 1.1 mrg /* Perform auto or decltype(auto) deduction to get the result. */
2147 1.1 mrg tree pattern = DEDUCT_CONSTR_PATTERN (t);
2148 1.1 mrg tree placeholder = DEDUCT_CONSTR_PLACEHOLDER (t);
2149 1.1 mrg tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (placeholder);
2150 1.1 mrg tree type_canonical = TYPE_CANONICAL (placeholder);
2151 1.1 mrg PLACEHOLDER_TYPE_CONSTRAINTS (placeholder)
2152 1.1 mrg = tsubst_constraint (constr, args, complain|tf_partial, in_decl);
2153 1.1 mrg TYPE_CANONICAL (placeholder) = NULL_TREE;
2154 1.1 mrg tree type = do_auto_deduction (pattern, init, placeholder,
2155 1.1 mrg complain, adc_requirement);
2156 1.1 mrg PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = constr;
2157 1.1 mrg TYPE_CANONICAL (placeholder) = type_canonical;
2158 1.1 mrg if (type == error_mark_node)
2159 1.1 mrg return boolean_false_node;
2160 1.1 mrg
2161 1.1 mrg return boolean_true_node;
2162 1.1 mrg }
2163 1.1 mrg
2164 1.1 mrg /* Check an exception constraint. An exception constraint for an
2165 1.1 mrg expression e is satisfied when noexcept(e) is true. */
2166 1.1 mrg
2167 1.1 mrg tree
2168 1.1 mrg satisfy_exception_constraint (tree t, tree args,
2169 1.1 mrg tsubst_flags_t complain, tree in_decl)
2170 1.1 mrg {
2171 1.1 mrg tree expr = EXCEPT_CONSTR_EXPR (t);
2172 1.1 mrg tree check = tsubst_expr (expr, args, complain, in_decl, false);
2173 1.1 mrg if (check == error_mark_node)
2174 1.1 mrg return boolean_false_node;
2175 1.1 mrg
2176 1.1 mrg if (expr_noexcept_p (check, complain))
2177 1.1 mrg return boolean_true_node;
2178 1.1 mrg else
2179 1.1 mrg return boolean_false_node;
2180 1.1 mrg }
2181 1.1 mrg
2182 1.1 mrg /* Check a parameterized constraint. */
2183 1.1 mrg
2184 1.1 mrg tree
2185 1.1 mrg satisfy_parameterized_constraint (tree t, tree args,
2186 1.1 mrg tsubst_flags_t complain, tree in_decl)
2187 1.1 mrg {
2188 1.1 mrg local_specialization_stack stack;
2189 1.1 mrg tree parms = PARM_CONSTR_PARMS (t);
2190 1.1 mrg tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
2191 1.1 mrg if (vars == error_mark_node)
2192 1.1 mrg return boolean_false_node;
2193 1.1 mrg tree constr = PARM_CONSTR_OPERAND (t);
2194 1.1 mrg return satisfy_constraint_1 (constr, args, complain, in_decl);
2195 1.1 mrg }
2196 1.1 mrg
2197 1.1 mrg /* Check that the conjunction of constraints is satisfied. Note
2198 1.1 mrg that if left operand is not satisfied, the right operand
2199 1.1 mrg is not checked.
2200 1.1 mrg
2201 1.1 mrg FIXME: Check that this wouldn't result in a user-defined
2202 1.1 mrg operator. Note that this error is partially diagnosed in
2203 1.1 mrg satisfy_predicate_constraint. It would be nice to diagnose
2204 1.1 mrg the overload, but I don't think it's strictly necessary. */
2205 1.1 mrg
2206 1.1 mrg tree
2207 1.1 mrg satisfy_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2208 1.1 mrg {
2209 1.1 mrg tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
2210 1.1 mrg if (t0 == boolean_false_node)
2211 1.1 mrg return boolean_false_node;
2212 1.1 mrg return satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
2213 1.1 mrg }
2214 1.1 mrg
2215 1.1 mrg /* Check that the disjunction of constraints is satisfied. Note
2216 1.1 mrg that if the left operand is satisfied, the right operand is not
2217 1.1 mrg checked. */
2218 1.1 mrg
2219 1.1 mrg tree
2220 1.1 mrg satisfy_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2221 1.1 mrg {
2222 1.1 mrg tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
2223 1.1 mrg if (t0 == boolean_true_node)
2224 1.1 mrg return boolean_true_node;
2225 1.1 mrg return satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
2226 1.1 mrg }
2227 1.1 mrg
2228 1.1 mrg /* Dispatch to an appropriate satisfaction routine depending on the
2229 1.1 mrg tree code of T. */
2230 1.1 mrg
2231 1.1 mrg tree
2232 1.1 mrg satisfy_constraint_1 (tree t, tree args, tsubst_flags_t complain, tree in_decl)
2233 1.1 mrg {
2234 1.1 mrg gcc_assert (!processing_template_decl);
2235 1.1 mrg
2236 1.1 mrg if (!t)
2237 1.1 mrg return boolean_false_node;
2238 1.1 mrg
2239 1.1 mrg if (t == error_mark_node)
2240 1.1 mrg return boolean_false_node;
2241 1.1 mrg
2242 1.1 mrg switch (TREE_CODE (t))
2243 1.1 mrg {
2244 1.1 mrg case PRED_CONSTR:
2245 1.1 mrg return satisfy_predicate_constraint (t, args, complain, in_decl);
2246 1.1 mrg
2247 1.1 mrg case CHECK_CONSTR:
2248 1.1 mrg return satisfy_check_constraint (t, args, complain, in_decl);
2249 1.1 mrg
2250 1.1 mrg case EXPR_CONSTR:
2251 1.1 mrg return satisfy_expression_constraint (t, args, complain, in_decl);
2252 1.1 mrg
2253 1.1 mrg case TYPE_CONSTR:
2254 1.1 mrg return satisfy_type_constraint (t, args, complain, in_decl);
2255 1.1 mrg
2256 1.1 mrg case ICONV_CONSTR:
2257 1.1 mrg return satisfy_implicit_conversion_constraint (t, args, complain, in_decl);
2258 1.1 mrg
2259 1.1 mrg case DEDUCT_CONSTR:
2260 1.1 mrg return satisfy_argument_deduction_constraint (t, args, complain, in_decl);
2261 1.1 mrg
2262 1.1 mrg case EXCEPT_CONSTR:
2263 1.1 mrg return satisfy_exception_constraint (t, args, complain, in_decl);
2264 1.1 mrg
2265 1.1 mrg case PARM_CONSTR:
2266 1.1 mrg return satisfy_parameterized_constraint (t, args, complain, in_decl);
2267 1.1 mrg
2268 1.1 mrg case CONJ_CONSTR:
2269 1.1 mrg return satisfy_conjunction (t, args, complain, in_decl);
2270 1.1 mrg
2271 1.1 mrg case DISJ_CONSTR:
2272 1.1 mrg return satisfy_disjunction (t, args, complain, in_decl);
2273 1.1 mrg
2274 1.1 mrg case EXPR_PACK_EXPANSION:
2275 1.1 mrg return satisfy_pack_expansion (t, args, complain, in_decl);
2276 1.1 mrg
2277 1.1 mrg default:
2278 1.1 mrg gcc_unreachable ();
2279 1.1 mrg }
2280 1.1 mrg return boolean_false_node;
2281 1.1 mrg }
2282 1.1 mrg
2283 1.1 mrg /* Check that the constraint is satisfied, according to the rules
2284 1.1 mrg for that constraint. Note that each satisfy_* function returns
2285 1.1 mrg true or false, depending on whether it is satisfied or not. */
2286 1.1 mrg
2287 1.1 mrg tree
2288 1.1 mrg satisfy_constraint (tree t, tree args)
2289 1.1 mrg {
2290 1.1 mrg auto_timevar time (TV_CONSTRAINT_SAT);
2291 1.1 mrg
2292 1.1 mrg /* Turn off template processing. Constraint satisfaction only applies
2293 1.1 mrg to non-dependent terms, so we want to ensure full checking here. */
2294 1.1 mrg processing_template_decl_sentinel proc (true);
2295 1.1 mrg
2296 1.1 mrg /* Avoid early exit in tsubst and tsubst_copy from null args; since earlier
2297 1.1 mrg substitution was done with processing_template_decl forced on, there will
2298 1.1 mrg be expressions that still need semantic processing, possibly buried in
2299 1.1 mrg decltype or a template argument. */
2300 1.1 mrg if (args == NULL_TREE)
2301 1.1 mrg args = make_tree_vec (1);
2302 1.1 mrg
2303 1.1 mrg return satisfy_constraint_1 (t, args, tf_none, NULL_TREE);
2304 1.1 mrg }
2305 1.1 mrg
2306 1.1 mrg /* Check the associated constraints in CI against the given
2307 1.1 mrg ARGS, returning true when the constraints are satisfied
2308 1.1 mrg and false otherwise. */
2309 1.1 mrg
2310 1.1 mrg tree
2311 1.1 mrg satisfy_associated_constraints (tree ci, tree args)
2312 1.1 mrg {
2313 1.1 mrg /* If there are no constraints then this is trivially satisfied. */
2314 1.1 mrg if (!ci)
2315 1.1 mrg return boolean_true_node;
2316 1.1 mrg
2317 1.1 mrg /* If any arguments depend on template parameters, we can't
2318 1.1 mrg check constraints. */
2319 1.1 mrg if (args && uses_template_parms (args))
2320 1.1 mrg return boolean_true_node;
2321 1.1 mrg
2322 1.1 mrg /* Check if we've seen a previous result. */
2323 1.1 mrg if (tree prev = lookup_constraint_satisfaction (ci, args))
2324 1.1 mrg return prev;
2325 1.1 mrg
2326 1.1 mrg /* Actually test for satisfaction. */
2327 1.1 mrg tree result = satisfy_constraint (CI_ASSOCIATED_CONSTRAINTS (ci), args);
2328 1.1 mrg return memoize_constraint_satisfaction (ci, args, result);
2329 1.1 mrg }
2330 1.1 mrg
2331 1.1 mrg } /* namespace */
2332 1.1 mrg
2333 1.1 mrg /* Evaluate the given constraint, returning boolean_true_node
2334 1.1 mrg if the constraint is satisfied and boolean_false_node
2335 1.1 mrg otherwise. */
2336 1.1 mrg
2337 1.1 mrg tree
2338 1.1 mrg evaluate_constraints (tree constr, tree args)
2339 1.1 mrg {
2340 1.1 mrg gcc_assert (constraint_p (constr));
2341 1.1 mrg return satisfy_constraint (constr, args);
2342 1.1 mrg }
2343 1.1 mrg
2344 1.1 mrg /* Evaluate the function concept FN by substituting its own args
2345 1.1 mrg into its definition and evaluating that as the result. Returns
2346 1.1 mrg boolean_true_node if the constraints are satisfied and
2347 1.1 mrg boolean_false_node otherwise. */
2348 1.1 mrg
2349 1.1 mrg tree
2350 1.1 mrg evaluate_function_concept (tree fn, tree args)
2351 1.1 mrg {
2352 1.1 mrg tree constr = build_nt (CHECK_CONSTR, fn, args);
2353 1.1 mrg return satisfy_constraint (constr, args);
2354 1.1 mrg }
2355 1.1 mrg
2356 1.1 mrg /* Evaluate the variable concept VAR by substituting its own args into
2357 1.1 mrg its initializer and checking the resulting constraint. Returns
2358 1.1 mrg boolean_true_node if the constraints are satisfied and
2359 1.1 mrg boolean_false_node otherwise. */
2360 1.1 mrg
2361 1.1 mrg tree
2362 1.1 mrg evaluate_variable_concept (tree var, tree args)
2363 1.1 mrg {
2364 1.1 mrg tree constr = build_nt (CHECK_CONSTR, var, args);
2365 1.1 mrg return satisfy_constraint (constr, args);
2366 1.1 mrg }
2367 1.1 mrg
2368 1.1 mrg /* Evaluate the given expression as if it were a predicate
2369 1.1 mrg constraint. Returns boolean_true_node if the constraint
2370 1.1 mrg is satisfied and boolean_false_node otherwise. */
2371 1.1 mrg
2372 1.1 mrg tree
2373 1.1 mrg evaluate_constraint_expression (tree expr, tree args)
2374 1.1 mrg {
2375 1.1 mrg tree constr = normalize_expression (expr);
2376 1.1 mrg return satisfy_constraint (constr, args);
2377 1.1 mrg }
2378 1.1 mrg
2379 1.1 mrg /* Returns true if the DECL's constraints are satisfied.
2380 1.1 mrg This is used in cases where a declaration is formed but
2381 1.1 mrg before it is used (e.g., overload resolution). */
2382 1.1 mrg
2383 1.1 mrg bool
2384 1.1 mrg constraints_satisfied_p (tree decl)
2385 1.1 mrg {
2386 1.1 mrg /* Get the constraints to check for satisfaction. This depends
2387 1.1 mrg on whether we're looking at a template specialization or not. */
2388 1.1 mrg tree ci;
2389 1.1 mrg tree args = NULL_TREE;
2390 1.1 mrg if (tree ti = DECL_TEMPLATE_INFO (decl))
2391 1.1 mrg {
2392 1.1 mrg ci = get_constraints (TI_TEMPLATE (ti));
2393 1.1 mrg args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
2394 1.1 mrg }
2395 1.1 mrg else
2396 1.1 mrg {
2397 1.1 mrg ci = get_constraints (decl);
2398 1.1 mrg }
2399 1.1 mrg
2400 1.1 mrg tree eval = satisfy_associated_constraints (ci, args);
2401 1.1 mrg return eval == boolean_true_node;
2402 1.1 mrg }
2403 1.1 mrg
2404 1.1 mrg /* Returns true if the constraints are satisfied by ARGS.
2405 1.1 mrg Here, T can be either a constraint or a constrained
2406 1.1 mrg declaration. */
2407 1.1 mrg
2408 1.1 mrg bool
2409 1.1 mrg constraints_satisfied_p (tree t, tree args)
2410 1.1 mrg {
2411 1.1 mrg tree eval;
2412 1.1 mrg if (constraint_p (t))
2413 1.1 mrg eval = evaluate_constraints (t, args);
2414 1.1 mrg else
2415 1.1 mrg eval = satisfy_associated_constraints (get_constraints (t), args);
2416 1.1 mrg return eval == boolean_true_node;
2417 1.1 mrg }
2418 1.1 mrg
2419 1.1 mrg namespace
2420 1.1 mrg {
2421 1.1 mrg
2422 1.1 mrg /* Normalize EXPR and determine if the resulting constraint is
2423 1.1 mrg satisfied by ARGS. Returns true if and only if the constraint
2424 1.1 mrg is satisfied. This is used extensively by diagnostics to
2425 1.1 mrg determine causes for failure. */
2426 1.1 mrg
2427 1.1 mrg inline bool
2428 1.1 mrg constraint_expression_satisfied_p (tree expr, tree args)
2429 1.1 mrg {
2430 1.1 mrg return evaluate_constraint_expression (expr, args) == boolean_true_node;
2431 1.1 mrg }
2432 1.1 mrg
2433 1.1 mrg } /* namespace */
2434 1.1 mrg
2435 1.1 mrg /*---------------------------------------------------------------------------
2436 1.1 mrg Semantic analysis of requires-expressions
2437 1.1 mrg ---------------------------------------------------------------------------*/
2438 1.1 mrg
2439 1.1 mrg /* Finish a requires expression for the given PARMS (possibly
2440 1.1 mrg null) and the non-empty sequence of requirements. */
2441 1.1 mrg tree
2442 1.1 mrg finish_requires_expr (tree parms, tree reqs)
2443 1.1 mrg {
2444 1.1 mrg /* Modify the declared parameters by removing their context
2445 1.1 mrg so they don't refer to the enclosing scope and explicitly
2446 1.1 mrg indicating that they are constraint variables. */
2447 1.1 mrg for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
2448 1.1 mrg {
2449 1.1 mrg DECL_CONTEXT (parm) = NULL_TREE;
2450 1.1 mrg CONSTRAINT_VAR_P (parm) = true;
2451 1.1 mrg }
2452 1.1 mrg
2453 1.1 mrg /* Build the node. */
2454 1.1 mrg tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
2455 1.1 mrg TREE_SIDE_EFFECTS (r) = false;
2456 1.1 mrg TREE_CONSTANT (r) = true;
2457 1.1 mrg return r;
2458 1.1 mrg }
2459 1.1 mrg
2460 1.1 mrg /* Construct a requirement for the validity of EXPR. */
2461 1.1 mrg tree
2462 1.1 mrg finish_simple_requirement (tree expr)
2463 1.1 mrg {
2464 1.1 mrg return build_nt (SIMPLE_REQ, expr);
2465 1.1 mrg }
2466 1.1 mrg
2467 1.1 mrg /* Construct a requirement for the validity of TYPE. */
2468 1.1 mrg tree
2469 1.1 mrg finish_type_requirement (tree type)
2470 1.1 mrg {
2471 1.1 mrg return build_nt (TYPE_REQ, type);
2472 1.1 mrg }
2473 1.1 mrg
2474 1.1 mrg /* Construct a requirement for the validity of EXPR, along with
2475 1.1 mrg its properties. if TYPE is non-null, then it specifies either
2476 1.1 mrg an implicit conversion or argument deduction constraint,
2477 1.1 mrg depending on whether any placeholders occur in the type name.
2478 1.1 mrg NOEXCEPT_P is true iff the noexcept keyword was specified. */
2479 1.1 mrg tree
2480 1.1 mrg finish_compound_requirement (tree expr, tree type, bool noexcept_p)
2481 1.1 mrg {
2482 1.1 mrg tree req = build_nt (COMPOUND_REQ, expr, type);
2483 1.1 mrg COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
2484 1.1 mrg return req;
2485 1.1 mrg }
2486 1.1 mrg
2487 1.1 mrg /* Finish a nested requirement. */
2488 1.1 mrg tree
2489 1.1 mrg finish_nested_requirement (tree expr)
2490 1.1 mrg {
2491 1.1 mrg return build_nt (NESTED_REQ, expr);
2492 1.1 mrg }
2493 1.1 mrg
2494 1.1 mrg // Check that FN satisfies the structural requirements of a
2495 1.1 mrg // function concept definition.
2496 1.1 mrg tree
2497 1.1 mrg check_function_concept (tree fn)
2498 1.1 mrg {
2499 1.1 mrg // Check that the function is comprised of only a single
2500 1.1 mrg // return statement.
2501 1.1 mrg tree body = DECL_SAVED_TREE (fn);
2502 1.1 mrg if (TREE_CODE (body) == BIND_EXPR)
2503 1.1 mrg body = BIND_EXPR_BODY (body);
2504 1.1 mrg
2505 1.1 mrg // Sometimes a function call results in the creation of clean up
2506 1.1 mrg // points. Allow these to be preserved in the body of the
2507 1.1 mrg // constraint, as we might actually need them for some constexpr
2508 1.1 mrg // evaluations.
2509 1.1 mrg if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
2510 1.1 mrg body = TREE_OPERAND (body, 0);
2511 1.1 mrg
2512 1.1 mrg /* Check that the definition is written correctly. */
2513 1.1 mrg if (TREE_CODE (body) != RETURN_EXPR)
2514 1.1 mrg {
2515 1.1 mrg location_t loc = DECL_SOURCE_LOCATION (fn);
2516 1.1 mrg if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
2517 1.1 mrg error_at (loc, "definition of concept %qD is empty", fn);
2518 1.1 mrg else
2519 1.1 mrg error_at (loc, "definition of concept %qD has multiple statements", fn);
2520 1.1 mrg }
2521 1.1 mrg
2522 1.1 mrg return NULL_TREE;
2523 1.1 mrg }
2524 1.1 mrg
2525 1.1 mrg
2526 1.1 mrg // Check that a constrained friend declaration function declaration,
2527 1.1 mrg // FN, is admissible. This is the case only when the declaration depends
2528 1.1 mrg // on template parameters and does not declare a specialization.
2529 1.1 mrg void
2530 1.1 mrg check_constrained_friend (tree fn, tree reqs)
2531 1.1 mrg {
2532 1.1 mrg if (fn == error_mark_node)
2533 1.1 mrg return;
2534 1.1 mrg gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
2535 1.1 mrg
2536 1.1 mrg // If there are not constraints, this cannot be an error.
2537 1.1 mrg if (!reqs)
2538 1.1 mrg return;
2539 1.1 mrg
2540 1.1 mrg // Constrained friend functions that don't depend on template
2541 1.1 mrg // arguments are effectively meaningless.
2542 1.1 mrg if (!uses_template_parms (TREE_TYPE (fn)))
2543 1.1 mrg {
2544 1.1 mrg error_at (location_of (fn),
2545 1.1 mrg "constrained friend does not depend on template parameters");
2546 1.1 mrg return;
2547 1.1 mrg }
2548 1.1 mrg }
2549 1.1 mrg
2550 1.1 mrg /*---------------------------------------------------------------------------
2551 1.1 mrg Equivalence of constraints
2552 1.1 mrg ---------------------------------------------------------------------------*/
2553 1.1 mrg
2554 1.1 mrg /* Returns true when A and B are equivalent constraints. */
2555 1.1 mrg bool
2556 1.1 mrg equivalent_constraints (tree a, tree b)
2557 1.1 mrg {
2558 1.1 mrg gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2559 1.1 mrg gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2560 1.1 mrg return cp_tree_equal (a, b);
2561 1.1 mrg }
2562 1.1 mrg
2563 1.1 mrg /* Returns true if the template declarations A and B have equivalent
2564 1.1 mrg constraints. This is the case when A's constraints subsume B's and
2565 1.1 mrg when B's also constrain A's. */
2566 1.1 mrg bool
2567 1.1 mrg equivalently_constrained (tree d1, tree d2)
2568 1.1 mrg {
2569 1.1 mrg gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
2570 1.1 mrg return equivalent_constraints (get_constraints (d1), get_constraints (d2));
2571 1.1 mrg }
2572 1.1 mrg
2573 1.1 mrg /*---------------------------------------------------------------------------
2574 1.1 mrg Partial ordering of constraints
2575 1.1 mrg ---------------------------------------------------------------------------*/
2576 1.1 mrg
2577 1.1 mrg /* Returns true when the the constraints in A subsume those in B. */
2578 1.1 mrg
2579 1.1 mrg bool
2580 1.1 mrg subsumes_constraints (tree a, tree b)
2581 1.1 mrg {
2582 1.1 mrg gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
2583 1.1 mrg gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
2584 1.1 mrg return subsumes (a, b);
2585 1.1 mrg }
2586 1.1 mrg
2587 1.1 mrg /* Returns true when the the constraints in A subsume those in B, but
2588 1.1 mrg the constraints in B do not subsume the constraints in A. */
2589 1.1 mrg
2590 1.1 mrg bool
2591 1.1 mrg strictly_subsumes (tree a, tree b)
2592 1.1 mrg {
2593 1.1 mrg return subsumes (a, b) && !subsumes (b, a);
2594 1.1 mrg }
2595 1.1 mrg
2596 1.1 mrg /* Determines which of the declarations, A or B, is more constrained.
2597 1.1 mrg That is, which declaration's constraints subsume but are not subsumed
2598 1.1 mrg by the other's?
2599 1.1 mrg
2600 1.1 mrg Returns 1 if A is more constrained than B, -1 if B is more constrained
2601 1.1 mrg than A, and 0 otherwise. */
2602 1.1 mrg
2603 1.1 mrg int
2604 1.1 mrg more_constrained (tree d1, tree d2)
2605 1.1 mrg {
2606 1.1 mrg tree c1 = get_constraints (d1);
2607 1.1 mrg tree c2 = get_constraints (d2);
2608 1.1 mrg int winner = 0;
2609 1.1 mrg if (subsumes_constraints (c1, c2))
2610 1.1 mrg ++winner;
2611 1.1 mrg if (subsumes_constraints (c2, c1))
2612 1.1 mrg --winner;
2613 1.1 mrg return winner;
2614 1.1 mrg }
2615 1.1 mrg
2616 1.1 mrg /* Returns true if D1 is at least as constrained as D2. That is, the
2617 1.1 mrg associated constraints of D1 subsume those of D2, or both declarations
2618 1.1 mrg are unconstrained. */
2619 1.1 mrg
2620 1.1 mrg bool
2621 1.1 mrg at_least_as_constrained (tree d1, tree d2)
2622 1.1 mrg {
2623 1.1 mrg tree c1 = get_constraints (d1);
2624 1.1 mrg tree c2 = get_constraints (d2);
2625 1.1 mrg return subsumes_constraints (c1, c2);
2626 1.1 mrg }
2627 1.1 mrg
2628 1.1 mrg
2629 1.1 mrg /*---------------------------------------------------------------------------
2630 1.1 mrg Constraint diagnostics
2631 1.1 mrg
2632 1.1 mrg FIXME: Normalize expressions into constraints before evaluating them.
2633 1.1 mrg This should be the general pattern for all such diagnostics.
2634 1.1 mrg ---------------------------------------------------------------------------*/
2635 1.1 mrg
2636 1.1 mrg /* The number of detailed constraint failures. */
2637 1.1 mrg
2638 1.1 mrg int constraint_errors = 0;
2639 1.1 mrg
2640 1.1 mrg /* Do not generate errors after diagnosing this number of constraint
2641 1.1 mrg failures.
2642 1.1 mrg
2643 1.1 mrg FIXME: This is a really arbitrary number. Provide better control of
2644 1.1 mrg constraint diagnostics with a command line option. */
2645 1.1 mrg
2646 1.1 mrg int constraint_thresh = 20;
2647 1.1 mrg
2648 1.1 mrg
2649 1.1 mrg /* Returns true if we should elide the diagnostic for a constraint failure.
2650 1.1 mrg This is the case when the number of errors has exceeded the pre-configured
2651 1.1 mrg threshold. */
2652 1.1 mrg
2653 1.1 mrg inline bool
2654 1.1 mrg elide_constraint_failure_p ()
2655 1.1 mrg {
2656 1.1 mrg bool ret = constraint_thresh <= constraint_errors;
2657 1.1 mrg ++constraint_errors;
2658 1.1 mrg return ret;
2659 1.1 mrg }
2660 1.1 mrg
2661 1.1 mrg /* Returns the number of undiagnosed errors. */
2662 1.1 mrg
2663 1.1 mrg inline int
2664 1.1 mrg undiagnosed_constraint_failures ()
2665 1.1 mrg {
2666 1.1 mrg return constraint_errors - constraint_thresh;
2667 1.1 mrg }
2668 1.1 mrg
2669 1.1 mrg /* The diagnosis of constraints performs a combination of normalization
2670 1.1 mrg and satisfaction testing. We recursively walk through the conjunction or
2671 1.1 mrg disjunction of associated constraints, testing each sub-constraint in
2672 1.1 mrg turn. */
2673 1.1 mrg
2674 1.1 mrg namespace {
2675 1.1 mrg
2676 1.1 mrg void diagnose_constraint (location_t, tree, tree, tree);
2677 1.1 mrg
2678 1.1 mrg /* Emit a specific diagnostics for a failed trait. */
2679 1.1 mrg
2680 1.1 mrg void
2681 1.1 mrg diagnose_trait_expression (location_t loc, tree, tree cur, tree args)
2682 1.1 mrg {
2683 1.1 mrg if (constraint_expression_satisfied_p (cur, args))
2684 1.1 mrg return;
2685 1.1 mrg if (elide_constraint_failure_p())
2686 1.1 mrg return;
2687 1.1 mrg
2688 1.1 mrg tree expr = PRED_CONSTR_EXPR (cur);
2689 1.1 mrg ++processing_template_decl;
2690 1.1 mrg expr = tsubst_expr (expr, args, tf_none, NULL_TREE, false);
2691 1.1 mrg --processing_template_decl;
2692 1.1 mrg
2693 1.1 mrg tree t1 = TRAIT_EXPR_TYPE1 (expr);
2694 1.1 mrg tree t2 = TRAIT_EXPR_TYPE2 (expr);
2695 1.1 mrg switch (TRAIT_EXPR_KIND (expr))
2696 1.1 mrg {
2697 1.1 mrg case CPTK_HAS_NOTHROW_ASSIGN:
2698 1.1 mrg inform (loc, " %qT is not nothrow copy assignable", t1);
2699 1.1 mrg break;
2700 1.1 mrg case CPTK_HAS_NOTHROW_CONSTRUCTOR:
2701 1.1 mrg inform (loc, " %qT is not nothrow default constructible", t1);
2702 1.1 mrg break;
2703 1.1 mrg case CPTK_HAS_NOTHROW_COPY:
2704 1.1 mrg inform (loc, " %qT is not nothrow copy constructible", t1);
2705 1.1 mrg break;
2706 1.1 mrg case CPTK_HAS_TRIVIAL_ASSIGN:
2707 1.1 mrg inform (loc, " %qT is not trivially copy assignable", t1);
2708 1.1 mrg break;
2709 1.1 mrg case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
2710 1.1 mrg inform (loc, " %qT is not trivially default constructible", t1);
2711 1.1 mrg break;
2712 1.1 mrg case CPTK_HAS_TRIVIAL_COPY:
2713 1.1 mrg inform (loc, " %qT is not trivially copy constructible", t1);
2714 1.1 mrg break;
2715 1.1 mrg case CPTK_HAS_TRIVIAL_DESTRUCTOR:
2716 1.1 mrg inform (loc, " %qT is not trivially destructible", t1);
2717 1.1 mrg break;
2718 1.1 mrg case CPTK_HAS_VIRTUAL_DESTRUCTOR:
2719 1.1 mrg inform (loc, " %qT does not have a virtual destructor", t1);
2720 1.1 mrg break;
2721 1.1 mrg case CPTK_IS_ABSTRACT:
2722 1.1 mrg inform (loc, " %qT is not an abstract class", t1);
2723 1.1 mrg break;
2724 1.1 mrg case CPTK_IS_BASE_OF:
2725 1.1 mrg inform (loc, " %qT is not a base of %qT", t1, t2);
2726 1.1 mrg break;
2727 1.1 mrg case CPTK_IS_CLASS:
2728 1.1 mrg inform (loc, " %qT is not a class", t1);
2729 1.1 mrg break;
2730 1.1 mrg case CPTK_IS_EMPTY:
2731 1.1 mrg inform (loc, " %qT is not an empty class", t1);
2732 1.1 mrg break;
2733 1.1 mrg case CPTK_IS_ENUM:
2734 1.1 mrg inform (loc, " %qT is not an enum", t1);
2735 1.1 mrg break;
2736 1.1 mrg case CPTK_IS_FINAL:
2737 1.1 mrg inform (loc, " %qT is not a final class", t1);
2738 1.1 mrg break;
2739 1.1 mrg case CPTK_IS_LITERAL_TYPE:
2740 1.1 mrg inform (loc, " %qT is not a literal type", t1);
2741 1.1 mrg break;
2742 1.1 mrg case CPTK_IS_POD:
2743 1.1 mrg inform (loc, " %qT is not a POD type", t1);
2744 1.1 mrg break;
2745 1.1 mrg case CPTK_IS_POLYMORPHIC:
2746 1.1 mrg inform (loc, " %qT is not a polymorphic type", t1);
2747 1.1 mrg break;
2748 1.1 mrg case CPTK_IS_SAME_AS:
2749 1.1 mrg inform (loc, " %qT is not the same as %qT", t1, t2);
2750 1.1 mrg break;
2751 1.1 mrg case CPTK_IS_STD_LAYOUT:
2752 1.1 mrg inform (loc, " %qT is not an standard layout type", t1);
2753 1.1 mrg break;
2754 1.1 mrg case CPTK_IS_TRIVIAL:
2755 1.1 mrg inform (loc, " %qT is not a trivial type", t1);
2756 1.1 mrg break;
2757 1.1 mrg case CPTK_IS_UNION:
2758 1.1 mrg inform (loc, " %qT is not a union", t1);
2759 1.1 mrg break;
2760 1.1 mrg default:
2761 1.1 mrg gcc_unreachable ();
2762 1.1 mrg }
2763 1.1 mrg }
2764 1.1 mrg
2765 1.1 mrg /* Diagnose the expression of a predicate constraint. */
2766 1.1 mrg
2767 1.1 mrg void
2768 1.1 mrg diagnose_other_expression (location_t loc, tree, tree cur, tree args)
2769 1.1 mrg {
2770 1.1 mrg if (constraint_expression_satisfied_p (cur, args))
2771 1.1 mrg return;
2772 1.1 mrg if (elide_constraint_failure_p())
2773 1.1 mrg return;
2774 1.1 mrg inform (loc, "%qE evaluated to false", cur);
2775 1.1 mrg }
2776 1.1 mrg
2777 1.1 mrg /* Do our best to infer meaning from predicates. */
2778 1.1 mrg
2779 1.1 mrg inline void
2780 1.1 mrg diagnose_predicate_constraint (location_t loc, tree orig, tree cur, tree args)
2781 1.1 mrg {
2782 1.1 mrg if (TREE_CODE (PRED_CONSTR_EXPR (cur)) == TRAIT_EXPR)
2783 1.1 mrg diagnose_trait_expression (loc, orig, cur, args);
2784 1.1 mrg else
2785 1.1 mrg diagnose_other_expression (loc, orig, cur, args);
2786 1.1 mrg }
2787 1.1 mrg
2788 1.1 mrg /* Diagnose a failed pack expansion, possibly containing constraints. */
2789 1.1 mrg
2790 1.1 mrg void
2791 1.1 mrg diagnose_pack_expansion (location_t loc, tree, tree cur, tree args)
2792 1.1 mrg {
2793 1.1 mrg if (constraint_expression_satisfied_p (cur, args))
2794 1.1 mrg return;
2795 1.1 mrg if (elide_constraint_failure_p())
2796 1.1 mrg return;
2797 1.1 mrg
2798 1.1 mrg /* Make sure that we don't have naked packs that we don't expect. */
2799 1.1 mrg if (!same_type_p (TREE_TYPE (cur), boolean_type_node))
2800 1.1 mrg {
2801 1.1 mrg inform (loc, "invalid pack expansion in constraint %qE", cur);
2802 1.1 mrg return;
2803 1.1 mrg }
2804 1.1 mrg
2805 1.1 mrg inform (loc, "in the expansion of %qE", cur);
2806 1.1 mrg
2807 1.1 mrg /* Get the vector of expanded arguments. Note that n must not
2808 1.1 mrg be 0 since this constraint is not satisfied. */
2809 1.1 mrg ++processing_template_decl;
2810 1.1 mrg tree exprs = tsubst_pack_expansion (cur, args, tf_none, NULL_TREE);
2811 1.1 mrg --processing_template_decl;
2812 1.1 mrg if (exprs == error_mark_node)
2813 1.1 mrg {
2814 1.1 mrg /* TODO: This error message could be better. */
2815 1.1 mrg inform (loc, " substitution failure occurred during expansion");
2816 1.1 mrg return;
2817 1.1 mrg }
2818 1.1 mrg
2819 1.1 mrg /* Check each expanded constraint separately. */
2820 1.1 mrg int n = TREE_VEC_LENGTH (exprs);
2821 1.1 mrg for (int i = 0; i < n; ++i)
2822 1.1 mrg {
2823 1.1 mrg tree expr = TREE_VEC_ELT (exprs, i);
2824 1.1 mrg if (!constraint_expression_satisfied_p (expr, args))
2825 1.1 mrg inform (loc, " %qE was not satisfied", expr);
2826 1.1 mrg }
2827 1.1 mrg }
2828 1.1 mrg
2829 1.1 mrg /* Diagnose a potentially unsatisfied concept check constraint DECL<CARGS>.
2830 1.1 mrg Parameters are as for diagnose_constraint. */
2831 1.1 mrg
2832 1.1 mrg void
2833 1.1 mrg diagnose_check_constraint (location_t loc, tree orig, tree cur, tree args)
2834 1.1 mrg {
2835 1.1 mrg if (constraints_satisfied_p (cur, args))
2836 1.1 mrg return;
2837 1.1 mrg
2838 1.1 mrg tree decl = CHECK_CONSTR_CONCEPT (cur);
2839 1.1 mrg tree cargs = CHECK_CONSTR_ARGS (cur);
2840 1.1 mrg tree tmpl = DECL_TI_TEMPLATE (decl);
2841 1.1 mrg tree check = build_nt (CHECK_CONSTR, decl, cargs);
2842 1.1 mrg
2843 1.1 mrg /* Instantiate the concept check arguments. */
2844 1.1 mrg tree targs = tsubst (cargs, args, tf_none, NULL_TREE);
2845 1.1 mrg if (targs == error_mark_node)
2846 1.1 mrg {
2847 1.1 mrg if (elide_constraint_failure_p ())
2848 1.1 mrg return;
2849 1.1 mrg inform (loc, "invalid use of the concept %qE", check);
2850 1.1 mrg tsubst (cargs, args, tf_warning_or_error, NULL_TREE);
2851 1.1 mrg return;
2852 1.1 mrg }
2853 1.1 mrg
2854 1.1 mrg tree sub = build_tree_list (tmpl, targs);
2855 1.1 mrg /* Update to the expanded definitions. */
2856 1.1 mrg cur = expand_concept (decl, targs);
2857 1.1 mrg if (cur == error_mark_node)
2858 1.1 mrg {
2859 1.1 mrg if (elide_constraint_failure_p ())
2860 1.1 mrg return;
2861 1.1 mrg inform (loc, "in the expansion of concept %qE %S", check, sub);
2862 1.1 mrg cur = get_concept_definition (decl);
2863 1.1 mrg tsubst_expr (cur, targs, tf_warning_or_error, NULL_TREE, false);
2864 1.1 mrg return;
2865 1.1 mrg }
2866 1.1 mrg
2867 1.1 mrg orig = get_concept_definition (CHECK_CONSTR_CONCEPT (orig));
2868 1.1 mrg orig = normalize_expression (orig);
2869 1.1 mrg
2870 1.1 mrg location_t dloc = DECL_SOURCE_LOCATION (decl);
2871 1.1 mrg inform (dloc, "within %qS", sub);
2872 1.1 mrg diagnose_constraint (dloc, orig, cur, targs);
2873 1.1 mrg }
2874 1.1 mrg
2875 1.1 mrg /* Diagnose a potentially unsatisfied conjunction or disjunction. Parameters
2876 1.1 mrg are as for diagnose_constraint. */
2877 1.1 mrg
2878 1.1 mrg void
2879 1.1 mrg diagnose_logical_constraint (location_t loc, tree orig, tree cur, tree args)
2880 1.1 mrg {
2881 1.1 mrg tree t0 = TREE_OPERAND (cur, 0);
2882 1.1 mrg tree t1 = TREE_OPERAND (cur, 1);
2883 1.1 mrg if (!constraints_satisfied_p (t0, args))
2884 1.1 mrg diagnose_constraint (loc, TREE_OPERAND (orig, 0), t0, args);
2885 1.1 mrg else if (TREE_CODE (orig) == TRUTH_ORIF_EXPR)
2886 1.1 mrg return;
2887 1.1 mrg if (!constraints_satisfied_p (t1, args))
2888 1.1 mrg diagnose_constraint (loc, TREE_OPERAND (orig, 1), t1, args);
2889 1.1 mrg }
2890 1.1 mrg
2891 1.1 mrg /* Diagnose a potential expression constraint failure. */
2892 1.1 mrg
2893 1.1 mrg void
2894 1.1 mrg diagnose_expression_constraint (location_t loc, tree orig, tree cur, tree args)
2895 1.1 mrg {
2896 1.1 mrg if (constraints_satisfied_p (cur, args))
2897 1.1 mrg return;
2898 1.1 mrg if (elide_constraint_failure_p())
2899 1.1 mrg return;
2900 1.1 mrg
2901 1.1 mrg tree expr = EXPR_CONSTR_EXPR (orig);
2902 1.1 mrg inform (loc, "the required expression %qE would be ill-formed", expr);
2903 1.1 mrg
2904 1.1 mrg // TODO: We should have a flag that controls this substitution.
2905 1.1 mrg // I'm finding it very useful for resolving concept check errors.
2906 1.1 mrg
2907 1.1 mrg // inform (input_location, "==== BEGIN DUMP ====");
2908 1.1 mrg // tsubst_expr (EXPR_CONSTR_EXPR (orig), args, tf_warning_or_error, NULL_TREE, false);
2909 1.1 mrg // inform (input_location, "==== END DUMP ====");
2910 1.1 mrg }
2911 1.1 mrg
2912 1.1 mrg /* Diagnose a potentially failed type constraint. */
2913 1.1 mrg
2914 1.1 mrg void
2915 1.1 mrg diagnose_type_constraint (location_t loc, tree orig, tree cur, tree args)
2916 1.1 mrg {
2917 1.1 mrg if (constraints_satisfied_p (cur, args))
2918 1.1 mrg return;
2919 1.1 mrg if (elide_constraint_failure_p())
2920 1.1 mrg return;
2921 1.1 mrg
2922 1.1 mrg tree type = TYPE_CONSTR_TYPE (orig);
2923 1.1 mrg inform (loc, "the required type %qT would be ill-formed", type);
2924 1.1 mrg }
2925 1.1 mrg
2926 1.1 mrg /* Diagnose a potentially unsatisfied conversion constraint. */
2927 1.1 mrg
2928 1.1 mrg void
2929 1.1 mrg diagnose_implicit_conversion_constraint (location_t loc, tree orig, tree cur,
2930 1.1 mrg tree args)
2931 1.1 mrg {
2932 1.1 mrg if (constraints_satisfied_p (cur, args))
2933 1.1 mrg return;
2934 1.1 mrg
2935 1.1 mrg /* The expression and type will previously have been substituted into,
2936 1.1 mrg and therefore may already be an error. Also, we will have already
2937 1.1 mrg diagnosed substitution failures into an expression since this must be
2938 1.1 mrg part of a compound requirement. */
2939 1.1 mrg tree expr = ICONV_CONSTR_EXPR (cur);
2940 1.1 mrg if (error_operand_p (expr))
2941 1.1 mrg return;
2942 1.1 mrg
2943 1.1 mrg /* Don't elide a previously diagnosed failure. */
2944 1.1 mrg if (elide_constraint_failure_p())
2945 1.1 mrg return;
2946 1.1 mrg
2947 1.1 mrg tree type = ICONV_CONSTR_TYPE (cur);
2948 1.1 mrg if (error_operand_p (type))
2949 1.1 mrg {
2950 1.1 mrg inform (loc, "substitution into type %qT failed",
2951 1.1 mrg ICONV_CONSTR_TYPE (orig));
2952 1.1 mrg return;
2953 1.1 mrg }
2954 1.1 mrg
2955 1.1 mrg inform(loc, "%qE is not implicitly convertible to %qT", expr, type);
2956 1.1 mrg }
2957 1.1 mrg
2958 1.1 mrg /* Diagnose an argument deduction constraint. */
2959 1.1 mrg
2960 1.1 mrg void
2961 1.1 mrg diagnose_argument_deduction_constraint (location_t loc, tree orig, tree cur,
2962 1.1 mrg tree args)
2963 1.1 mrg {
2964 1.1 mrg if (constraints_satisfied_p (cur, args))
2965 1.1 mrg return;
2966 1.1 mrg
2967 1.1 mrg /* The expression and type will previously have been substituted into,
2968 1.1 mrg and therefore may already be an error. Also, we will have already
2969 1.1 mrg diagnosed substution failures into an expression since this must be
2970 1.1 mrg part of a compound requirement. */
2971 1.1 mrg tree expr = DEDUCT_CONSTR_EXPR (cur);
2972 1.1 mrg if (error_operand_p (expr))
2973 1.1 mrg return;
2974 1.1 mrg
2975 1.1 mrg /* Don't elide a previously diagnosed failure. */
2976 1.1 mrg if (elide_constraint_failure_p ())
2977 1.1 mrg return;
2978 1.1 mrg
2979 1.1 mrg tree pattern = DEDUCT_CONSTR_PATTERN (cur);
2980 1.1 mrg if (error_operand_p (pattern))
2981 1.1 mrg {
2982 1.1 mrg inform (loc, "substitution into type %qT failed",
2983 1.1 mrg DEDUCT_CONSTR_PATTERN (orig));
2984 1.1 mrg return;
2985 1.1 mrg }
2986 1.1 mrg
2987 1.1 mrg inform (loc, "unable to deduce placeholder type %qT from %qE",
2988 1.1 mrg pattern, expr);
2989 1.1 mrg }
2990 1.1 mrg
2991 1.1 mrg /* Diagnose an exception constraint. */
2992 1.1 mrg
2993 1.1 mrg void
2994 1.1 mrg diagnose_exception_constraint (location_t loc, tree orig, tree cur, tree args)
2995 1.1 mrg {
2996 1.1 mrg if (constraints_satisfied_p (cur, args))
2997 1.1 mrg return;
2998 1.1 mrg if (elide_constraint_failure_p ())
2999 1.1 mrg return;
3000 1.1 mrg
3001 1.1 mrg /* Rebuild a noexcept expression. */
3002 1.1 mrg tree expr = EXCEPT_CONSTR_EXPR (cur);
3003 1.1 mrg if (error_operand_p (expr))
3004 1.1 mrg return;
3005 1.1 mrg
3006 1.1 mrg inform (loc, "%qE evaluated to false", EXCEPT_CONSTR_EXPR (orig));
3007 1.1 mrg }
3008 1.1 mrg
3009 1.1 mrg /* Diagnose a potentially unsatisfied parameterized constraint. */
3010 1.1 mrg
3011 1.1 mrg void
3012 1.1 mrg diagnose_parameterized_constraint (location_t loc, tree orig, tree cur,
3013 1.1 mrg tree args)
3014 1.1 mrg {
3015 1.1 mrg if (constraints_satisfied_p (cur, args))
3016 1.1 mrg return;
3017 1.1 mrg
3018 1.1 mrg local_specialization_stack stack;
3019 1.1 mrg tree parms = PARM_CONSTR_PARMS (cur);
3020 1.1 mrg tree vars = tsubst_constraint_variables (parms, args, tf_warning_or_error,
3021 1.1 mrg NULL_TREE);
3022 1.1 mrg if (vars == error_mark_node)
3023 1.1 mrg {
3024 1.1 mrg if (elide_constraint_failure_p ())
3025 1.1 mrg return;
3026 1.1 mrg
3027 1.1 mrg /* TODO: Check which variable failed and use orig to diagnose
3028 1.1 mrg that substitution error. */
3029 1.1 mrg inform (loc, "failed to instantiate constraint variables");
3030 1.1 mrg return;
3031 1.1 mrg }
3032 1.1 mrg
3033 1.1 mrg /* TODO: It would be better write these in a list. */
3034 1.1 mrg while (vars)
3035 1.1 mrg {
3036 1.1 mrg inform (loc, " with %q#D", vars);
3037 1.1 mrg vars = TREE_CHAIN (vars);
3038 1.1 mrg }
3039 1.1 mrg orig = PARM_CONSTR_OPERAND (orig);
3040 1.1 mrg cur = PARM_CONSTR_OPERAND (cur);
3041 1.1 mrg return diagnose_constraint (loc, orig, cur, args);
3042 1.1 mrg }
3043 1.1 mrg
3044 1.1 mrg /* Diagnose the constraint CUR for the given ARGS. This is only ever invoked
3045 1.1 mrg on the associated constraints, so we can only have conjunctions of
3046 1.1 mrg predicate constraints. The ORIGinal (dependent) constructs follow
3047 1.1 mrg the current constraints to enable better diagnostics. Note that ORIG
3048 1.1 mrg and CUR must be the same kinds of node, except when CUR is an error. */
3049 1.1 mrg
3050 1.1 mrg void
3051 1.1 mrg diagnose_constraint (location_t loc, tree orig, tree cur, tree args)
3052 1.1 mrg {
3053 1.1 mrg switch (TREE_CODE (cur))
3054 1.1 mrg {
3055 1.1 mrg case EXPR_CONSTR:
3056 1.1 mrg diagnose_expression_constraint (loc, orig, cur, args);
3057 1.1 mrg break;
3058 1.1 mrg
3059 1.1 mrg case TYPE_CONSTR:
3060 1.1 mrg diagnose_type_constraint (loc, orig, cur, args);
3061 1.1 mrg break;
3062 1.1 mrg
3063 1.1 mrg case ICONV_CONSTR:
3064 1.1 mrg diagnose_implicit_conversion_constraint (loc, orig, cur, args);
3065 1.1 mrg break;
3066 1.1 mrg
3067 1.1 mrg case DEDUCT_CONSTR:
3068 1.1 mrg diagnose_argument_deduction_constraint (loc, orig, cur, args);
3069 1.1 mrg break;
3070 1.1 mrg
3071 1.1 mrg case EXCEPT_CONSTR:
3072 1.1 mrg diagnose_exception_constraint (loc, orig, cur, args);
3073 1.1 mrg break;
3074 1.1 mrg
3075 1.1 mrg case CONJ_CONSTR:
3076 1.1 mrg case DISJ_CONSTR:
3077 1.1 mrg diagnose_logical_constraint (loc, orig, cur, args);
3078 1.1 mrg break;
3079 1.1 mrg
3080 1.1 mrg case PRED_CONSTR:
3081 1.1 mrg diagnose_predicate_constraint (loc, orig, cur, args);
3082 1.1 mrg break;
3083 1.1 mrg
3084 1.1 mrg case PARM_CONSTR:
3085 1.1 mrg diagnose_parameterized_constraint (loc, orig, cur, args);
3086 1.1 mrg break;
3087 1.1 mrg
3088 1.1 mrg case CHECK_CONSTR:
3089 1.1 mrg diagnose_check_constraint (loc, orig, cur, args);
3090 1.1 mrg break;
3091 1.1 mrg
3092 1.1 mrg case EXPR_PACK_EXPANSION:
3093 1.1 mrg diagnose_pack_expansion (loc, orig, cur, args);
3094 1.1 mrg break;
3095 1.1 mrg
3096 1.1 mrg case ERROR_MARK:
3097 1.1 mrg /* TODO: Can we improve the diagnostic with the original? */
3098 1.1 mrg inform (input_location, "ill-formed constraint");
3099 1.1 mrg break;
3100 1.1 mrg
3101 1.1 mrg default:
3102 1.1 mrg gcc_unreachable ();
3103 1.1 mrg break;
3104 1.1 mrg }
3105 1.1 mrg }
3106 1.1 mrg
3107 1.1 mrg /* Diagnose the reason(s) why ARGS do not satisfy the constraints
3108 1.1 mrg of declaration DECL. */
3109 1.1 mrg
3110 1.1 mrg void
3111 1.1 mrg diagnose_declaration_constraints (location_t loc, tree decl, tree args)
3112 1.1 mrg {
3113 1.1 mrg inform (loc, " constraints not satisfied");
3114 1.1 mrg
3115 1.1 mrg /* Constraints are attached to the template. */
3116 1.1 mrg if (tree ti = DECL_TEMPLATE_INFO (decl))
3117 1.1 mrg {
3118 1.1 mrg decl = TI_TEMPLATE (ti);
3119 1.1 mrg if (!args)
3120 1.1 mrg args = TI_ARGS (ti);
3121 1.1 mrg }
3122 1.1 mrg
3123 1.1 mrg /* Recursively diagnose the associated constraints. */
3124 1.1 mrg tree ci = get_constraints (decl);
3125 1.1 mrg tree t = CI_ASSOCIATED_CONSTRAINTS (ci);
3126 1.1 mrg diagnose_constraint (loc, t, t, args);
3127 1.1 mrg }
3128 1.1 mrg
3129 1.1 mrg } // namespace
3130 1.1 mrg
3131 1.1 mrg /* Emit diagnostics detailing the failure ARGS to satisfy the
3132 1.1 mrg constraints of T. Here, T can be either a constraint
3133 1.1 mrg or a declaration. */
3134 1.1 mrg
3135 1.1 mrg void
3136 1.1 mrg diagnose_constraints (location_t loc, tree t, tree args)
3137 1.1 mrg {
3138 1.1 mrg constraint_errors = 0;
3139 1.1 mrg
3140 1.1 mrg if (constraint_p (t))
3141 1.1 mrg diagnose_constraint (loc, t, t, args);
3142 1.1 mrg else if (DECL_P (t))
3143 1.1 mrg diagnose_declaration_constraints (loc, t, args);
3144 1.1 mrg else
3145 1.1 mrg gcc_unreachable ();
3146 1.1 mrg
3147 1.1 mrg /* Note the number of elided failures. */
3148 1.1 mrg int n = undiagnosed_constraint_failures ();
3149 1.1 mrg if (n > 0)
3150 1.1 mrg inform (loc, "... and %d more constraint errors not shown", n);
3151 1.1 mrg }
3152