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