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