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