Line data Source code
1 : /* C++ reflection code.
2 : Copyright (C) 2025-2026 Free Software Foundation, Inc.
3 : Written by Marek Polacek <polacek@redhat.com> and
4 : Jakub Jelinek <jakub@redhat.com>.
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GCC is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "target.h"
26 : #include "tm.h"
27 : #include "cp-tree.h"
28 : #include "stringpool.h" // for get_identifier
29 : #include "intl.h"
30 : #include "attribs.h"
31 : #include "c-family/c-pragma.h" // for parse_in
32 : #include "gimplify.h" // for unshare_expr
33 : #include "metafns.h"
34 :
35 : static tree eval_is_function_type (tree);
36 : static tree eval_is_object_type (location_t, const constexpr_ctx *, tree,
37 : bool *, tree);
38 : static tree eval_reflect_constant (location_t, const constexpr_ctx *, tree,
39 : tree, bool *, tree *, tree);
40 : static tree eval_is_array_type (location_t, const constexpr_ctx *, tree,
41 : bool *, tree);
42 : static tree eval_reflect_constant_array (location_t, const constexpr_ctx *,
43 : tree, bool *, bool *, tree *, tree);
44 : static tree eval_reflect_function (location_t, const constexpr_ctx *, tree,
45 : tree, bool *, tree *, tree);
46 : struct constexpr_ctx;
47 :
48 : /* Return the appropriate tsubst flags for processing a metafunction. */
49 :
50 : static tsubst_flags_t
51 480 : complain_flags (const constexpr_ctx *ctx)
52 : {
53 480 : return cxx_constexpr_quiet_p (ctx) ? tf_none : tf_warning_or_error;
54 : }
55 :
56 : /* Initialize state for reflection; e.g., initialize meta_info_type_node. */
57 :
58 : void
59 1394 : init_reflection ()
60 : {
61 : /* The type std::meta::info is a scalar type for which equality and
62 : inequality are meaningful, but for which no ordering relation is
63 : defined. */
64 1394 : meta_info_type_node = make_node (OPAQUE_TYPE);
65 : /* Make it a complete type. */
66 2788 : TYPE_SIZE (meta_info_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
67 2788 : TYPE_SIZE_UNIT (meta_info_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
68 1394 : TYPE_UNSIGNED (meta_info_type_node) = 1;
69 2788 : TYPE_PRECISION (meta_info_type_node) = GET_MODE_BITSIZE (ptr_mode);
70 1394 : SET_TYPE_MODE (meta_info_type_node, ptr_mode);
71 1394 : SET_TYPE_ALIGN (meta_info_type_node, GET_MODE_ALIGNMENT (ptr_mode));
72 : /* Name it. */
73 1394 : record_builtin_type (RID_MAX, "decltype(^^int)", meta_info_type_node);
74 :
75 : /* Create the `std::meta' namespace. */
76 1394 : push_namespace (get_identifier ("std"));
77 1394 : push_namespace (get_identifier ("meta"), /*inline*/false);
78 1394 : std_meta_node = current_namespace;
79 1394 : pop_namespace ();
80 1394 : pop_namespace ();
81 1394 : }
82 :
83 : /* Ensure the type of DECL is fully resolved by performing return
84 : type deduction and deferred noexcept instantiation. */
85 :
86 : static void
87 98838 : resolve_type_of_reflected_decl (tree decl)
88 : {
89 : /* Quietly calling mark_used in an unevaluated context will perform
90 : all necessary checks and instantiations while suppressing constraint
91 : unsatisfaction and deletedness diagnostics. */
92 98838 : cp_unevaluated u;
93 98838 : mark_used (decl, tf_none);
94 98838 : }
95 :
96 : /* Create a REFLECT_EXPR expression of kind KIND around T. */
97 :
98 : static tree
99 134544 : get_reflection_raw (location_t loc, tree t, reflect_kind kind = REFLECT_UNDEF)
100 : {
101 134544 : t = build1_loc (loc, REFLECT_EXPR, meta_info_type_node, t);
102 134544 : SET_REFLECT_EXPR_KIND (t, kind);
103 134544 : TREE_CONSTANT (t) = true;
104 134544 : TREE_READONLY (t) = true;
105 134544 : TREE_SIDE_EFFECTS (t) = false;
106 134544 : return t;
107 : }
108 :
109 : /* Return the reflection for T.
110 :
111 : [basic.fundamental]: A value of type std::meta::info is called a reflection.
112 : There exists a unique null reflection; every other reflection is
113 : a representation of
114 :
115 : -- a value of scalar type,
116 : -- an object with static storage duration,
117 : -- a variable,
118 : -- a structured binding,
119 : -- a function,
120 : -- a function parameter,
121 : -- an enumerator,
122 : -- an annotation,
123 : -- a type alias,
124 : -- a type,
125 : -- a class member,
126 : -- an unnamed bit-field,
127 : -- a class template,
128 : -- a function template,
129 : -- a variable template,
130 : -- an alias template,
131 : -- a concept,
132 : -- a namespace alias,
133 : -- a namespace,
134 : -- a direct base class relationship, or
135 : -- a data member description.
136 :
137 : KIND is used to distinguish between categories that are represented
138 : by the same handle. */
139 :
140 : tree
141 56660 : get_reflection (location_t loc, tree t, reflect_kind kind/*=REFLECT_UNDEF*/)
142 : {
143 56660 : STRIP_ANY_LOCATION_WRAPPER (t);
144 56660 : t = STRIP_REFERENCE_REF (t);
145 :
146 : /* Constant template parameters and pack-index-expressions cannot
147 : appear as operands of the reflection operator. */
148 56660 : if (TREE_CODE (t) == PACK_INDEX_EXPR)
149 : {
150 0 : error_at (loc, "%<^^%> cannot be applied to a pack index");
151 0 : return error_mark_node;
152 : }
153 56660 : else if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
154 : {
155 14 : error_at (loc, "%<^^%> cannot be applied to a non-type template "
156 : "parameter %qD", t);
157 14 : return error_mark_node;
158 : }
159 : /* If the id-expression denotes a variable declared by an init-capture,
160 : R is ill-formed. */
161 56646 : else if (is_capture_proxy (t))
162 : {
163 4 : error_at (loc, "%<^^%> cannot be applied to a capture %qD", t);
164 4 : return error_mark_node;
165 : }
166 : /* If the id-expression denotes a local parameter introduced by
167 : a requires-expression, R is ill-formed. */
168 56642 : else if (TREE_CODE (t) == PARM_DECL && CONSTRAINT_VAR_P (t))
169 : {
170 2 : error_at (loc, "%<^^%> cannot be applied to a local parameter of "
171 : "a requires-expression %qD", t);
172 2 : return error_mark_node;
173 : }
174 : /* If the id-expression denotes a local entity E for which there is
175 : a lambda scope that intervenes between R and the point at which E
176 : was introduced, R is ill-formed. */
177 56640 : else if (outer_automatic_var_p (t)
178 : /* Since outer_automatic_var_p is also true when we are in
179 : a local class member function, additionally check that
180 : we are in a lambda. */
181 56640 : && ((current_function_decl
182 36 : && LAMBDA_FUNCTION_P (current_function_decl))
183 12 : || parsing_lambda_declarator ()))
184 : {
185 14 : auto_diagnostic_group d;
186 14 : error_at (loc, "%<^^%> cannot be applied to a local entity for which "
187 : "there is an intervening lambda expression");
188 14 : inform (DECL_SOURCE_LOCATION (t), "%qD declared here", t);
189 14 : return error_mark_node;
190 14 : }
191 : /* If lookup finds a declaration that replaced a using-declarator during
192 : a single search, R is ill-formed. */
193 56626 : else if (TREE_CODE (t) == USING_DECL
194 56626 : || (TREE_CODE (t) == OVERLOAD && OVL_USING_P (t)))
195 : {
196 8 : error_at (loc, "%<^^%> cannot be applied to a using-declaration");
197 8 : return error_mark_node;
198 : }
199 : /* A concept is fine, but not Concept<arg>. */
200 56618 : else if (concept_check_p (t))
201 : {
202 2 : error_at (loc, "%<^^%> cannot be applied to a concept check");
203 2 : return error_mark_node;
204 : }
205 :
206 : /* Otherwise, if the template-name names a function template F,
207 : then the template-name interpreted as an id-expression shall
208 : denote an overload set containing only F. R represents F.
209 :
210 : When we have:
211 : template<typename T>
212 : void foo (T) {}
213 : constexpr auto a = ^^foo;
214 : we will get an OVERLOAD containing only one function. */
215 56616 : tree r = MAYBE_BASELINK_FUNCTIONS (t);
216 56616 : if (OVL_P (r))
217 : {
218 3983 : if (!OVL_SINGLE_P (r))
219 : {
220 32 : error_at (loc, "cannot take the reflection of an overload set");
221 32 : return error_mark_node;
222 : }
223 : }
224 : /* [expr.reflect] If the id-expression denotes an overload set S,
225 : overload resolution for the expression &S with no target shall
226 : select a unique function; R represents that function. */
227 52633 : else if (!processing_template_decl && t != unknown_type_node)
228 : {
229 : /* Resolve all TEMPLATE_ID_EXPRs here. */
230 47582 : t = resolve_nondeduced_context_or_error (t, tf_warning_or_error);
231 : /* The argument could have a deduced return type, so we need to
232 : instantiate it now to find out its type. */
233 47582 : resolve_type_of_reflected_decl (t);
234 : /* Avoid -Wunused-but-set* warnings when a variable or parameter
235 : is just set and reflected. */
236 47582 : if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
237 4388 : mark_exp_read (t);
238 : }
239 :
240 : /* For injected-class-name, use the main variant so that comparing
241 : reflections works (cf. compare3.C). */
242 56584 : if (RECORD_OR_UNION_TYPE_P (t)
243 13926 : && TYPE_NAME (t)
244 70016 : && DECL_SELF_REFERENCE_P (TYPE_NAME (t)))
245 110 : t = TYPE_MAIN_VARIANT (t);
246 :
247 : /* It's annoying to deal with BIT_NOT_EXPR in a reflection later, so
248 : look up the FUNCTION_DECL here. */
249 56584 : if (TREE_CODE (t) == BIT_NOT_EXPR
250 100 : && CLASS_TYPE_P (TREE_OPERAND (t, 0))
251 56684 : && COMPLETE_TYPE_P (TREE_OPERAND (t, 0)))
252 : {
253 100 : r = TREE_OPERAND (t, 0);
254 100 : if (CLASSTYPE_LAZY_DESTRUCTOR (r))
255 14 : lazily_declare_fn (sfk_destructor, r);
256 100 : if (tree dtor = CLASSTYPE_DESTRUCTOR (r))
257 56584 : t = dtor;
258 : }
259 :
260 : /* Block-scope externs are invalid here as per the proposed resolution
261 : of CWG 3065. */
262 56584 : if (VAR_OR_FUNCTION_DECL_P (t) && DECL_LOCAL_DECL_P (t))
263 : {
264 40 : error_at (loc, "cannot take the reflection of a block-scope extern %qE",
265 : t);
266 40 : return error_mark_node;
267 : }
268 :
269 56544 : if (t == error_mark_node)
270 : return error_mark_node;
271 :
272 56522 : return get_reflection_raw (loc, t, kind);
273 : }
274 :
275 : /* Null reflection shared tree. */
276 :
277 : static GTY(()) tree null_reflection;
278 :
279 : /* Return a null reflection value. */
280 :
281 : tree
282 17578 : get_null_reflection ()
283 : {
284 17578 : if (!null_reflection)
285 908 : null_reflection = get_reflection_raw (UNKNOWN_LOCATION, unknown_type_node);
286 17578 : return null_reflection;
287 : }
288 :
289 : /* True iff T is a null reflection. */
290 :
291 : bool
292 5247341 : null_reflection_p (const_tree t)
293 : {
294 5247341 : if (!t)
295 : return false;
296 :
297 5247341 : if (REFLECT_EXPR_P (t) && REFLECT_EXPR_HANDLE (t) == unknown_type_node)
298 : return true;
299 :
300 : /* This is the rewritten form. */
301 5245177 : return integer_zerop (t) && REFLECTION_TYPE_P (TREE_TYPE (t));
302 : }
303 :
304 : /* If R represents a null reflection, rewrite it with something the ME
305 : can process; that is, something that doesn't use REFLECT_EXPR. */
306 :
307 : void
308 2917008 : rewrite_null_reflection (tree &r)
309 : {
310 : /* A tree walk mostly only so that we recurse through CONSTRUCTORs. */
311 8141729 : auto walker = [](tree *tp, int *walk_subtrees, void *) -> tree
312 : {
313 5224721 : if (TYPE_P (*tp))
314 1012 : *walk_subtrees = 0;
315 5223709 : else if (null_reflection_p (*tp))
316 : {
317 729 : *tp = build_int_cst (meta_info_type_node, 0);
318 729 : *walk_subtrees = 0;
319 : }
320 5224721 : return NULL_TREE;
321 : };
322 :
323 2917008 : cp_walk_tree (&r, walker, nullptr, nullptr);
324 2917008 : }
325 :
326 : /* Do strip_typedefs on T, but only for types. */
327 :
328 : static tree
329 6368 : maybe_strip_typedefs (tree t)
330 : {
331 6368 : if (TYPE_P (t))
332 6094 : return strip_typedefs (t);
333 : return t;
334 : }
335 :
336 : /* If PARM_DECL comes from an earlier reflection of a function parameter
337 : and function definition is seen after that, DECL_ARGUMENTS is
338 : overwritten and so the old PARM_DECL is no longer present in the
339 : DECL_ARGUMENTS (DECL_CONTEXT (parm)) chain. Return corresponding
340 : PARM_DECL which is in the chain. */
341 :
342 : tree
343 1076 : maybe_update_function_parm (tree parm)
344 : {
345 1076 : if (!OLD_PARM_DECL_P (parm))
346 : return parm;
347 256 : tree fn = DECL_CONTEXT (parm);
348 256 : int oldlen = list_length (parm);
349 256 : int newlen = list_length (DECL_ARGUMENTS (fn));
350 256 : gcc_assert (newlen >= oldlen);
351 256 : tree ret = DECL_ARGUMENTS (fn);
352 256 : int n = newlen - oldlen;
353 704 : while (n)
354 : {
355 448 : ret = DECL_CHAIN (ret);
356 448 : --n;
357 : }
358 : return ret;
359 : }
360 :
361 : /* Build up const T &. */
362 :
363 : tree
364 1387 : build_const_lref (tree t)
365 : {
366 1387 : return build_stub_type (t, cp_type_quals (t) | TYPE_QUAL_CONST,
367 1387 : /*rval=*/false);
368 : }
369 :
370 : /* Return true if DECL comes from std::meta. */
371 :
372 : static bool
373 142435 : decl_in_std_meta_p (tree decl)
374 : {
375 0 : return decl_namespace_context (decl) == std_meta_node;
376 : }
377 :
378 : /* True if CTX is an instance of std::meta::NAME class. */
379 :
380 : static bool
381 8031 : is_std_meta_class (tree ctx, const char *name)
382 : {
383 8031 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
384 : return false;
385 :
386 8013 : tree decl = TYPE_MAIN_DECL (ctx);
387 8013 : tree dname = DECL_NAME (decl);
388 8013 : if (dname == NULL_TREE || !id_equal (dname, name))
389 : return false;
390 :
391 8013 : return decl_in_std_meta_p (decl);
392 : }
393 :
394 : /* Returns true if FNDECL, a FUNCTION_DECL, is a call to a metafunction
395 : declared in namespace std::meta. */
396 :
397 : bool
398 185502594 : metafunction_p (tree fndecl)
399 : {
400 185502594 : if (!flag_reflection)
401 : return false;
402 :
403 : /* Metafunctions are expected to be marked consteval. */
404 9459166 : if (!DECL_IMMEDIATE_FUNCTION_P (fndecl))
405 : return false;
406 :
407 160183 : if (special_function_p (fndecl))
408 : return false;
409 :
410 134422 : if (!decl_in_std_meta_p (fndecl))
411 : return false;
412 :
413 : /* They should be user provided and not defined. */
414 95346 : if (!user_provided_p (fndecl)
415 95346 : || (DECL_NAMESPACE_SCOPE_P (fndecl) && DECL_DELETED_FN (fndecl)))
416 : return false;
417 95346 : if (DECL_INITIAL (fndecl))
418 950 : return false;
419 :
420 : return true;
421 : }
422 :
423 : /* Extract the N-th reflection argument from a metafunction call CALL. */
424 :
425 : static tree
426 57920 : get_info (location_t loc, const constexpr_ctx *ctx, tree call, int n,
427 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
428 : {
429 57920 : gcc_checking_assert (call_expr_nargs (call) > n);
430 57920 : tree info = get_nth_callarg (call, n);
431 57920 : if (!REFLECTION_TYPE_P (TREE_TYPE (info)))
432 : {
433 12 : error_at (loc, "incorrect %qT type of argument %d, expected %qT",
434 12 : TREE_TYPE (info), n + 1, meta_info_type_node);
435 12 : *non_constant_p = true;
436 12 : return NULL_TREE;
437 : }
438 57908 : info = cxx_eval_constant_expression (ctx, info, vc_prvalue,
439 : non_constant_p, overflow_p,
440 : jump_target);
441 57908 : if (*jump_target)
442 : return NULL_TREE;
443 57896 : if (!REFLECT_EXPR_P (info))
444 : {
445 52 : *non_constant_p = true;
446 52 : return NULL_TREE;
447 : }
448 : return info;
449 : }
450 :
451 : /* Helper function for get_range_elts, called through cp_walk_tree. */
452 :
453 : static tree
454 175086 : replace_parm_r (tree *tp, int *walk_subtrees, void *data)
455 : {
456 175086 : tree *p = (tree *) data;
457 175086 : if (*tp == p[0])
458 15806 : *tp = p[1];
459 159280 : else if (TYPE_P (*tp))
460 0 : *walk_subtrees = 0;
461 175086 : return NULL_TREE;
462 : }
463 :
464 : static tree throw_exception (location_t, const constexpr_ctx *, const char *,
465 : tree, bool *, tree *);
466 :
467 : /* Helper function for get_range_elts, handle adjustment of ARRAY_TYPE elts
468 : of a retvec. */
469 :
470 : static tree
471 2622 : adjust_array_elt (location_t loc, const constexpr_ctx *ctx, tree valuet,
472 : tree expr, tree fun, bool *non_constant_p, tree *jump_target)
473 : {
474 2622 : if (TREE_CODE (valuet) == ARRAY_TYPE)
475 : {
476 816 : if (TREE_CODE (expr) != CONSTRUCTOR
477 816 : || TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
478 0 : return throw_exception (loc, ctx, "reflect_constant_array failed",
479 0 : fun, non_constant_p, jump_target);
480 : unsigned int i;
481 : tree val;
482 3216 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, val)
483 : {
484 2400 : CONSTRUCTOR_ELT (expr, i)->value
485 2400 : = adjust_array_elt (loc, ctx, TREE_TYPE (valuet), val, fun,
486 : non_constant_p, jump_target);
487 2400 : if (*jump_target || *non_constant_p)
488 : return NULL_TREE;
489 : }
490 : return expr;
491 : }
492 1806 : expr = convert_reflect_constant_arg (valuet, expr);
493 1806 : if (expr == error_mark_node)
494 0 : return throw_exception (loc, ctx, "reflect_constant failed",
495 0 : fun, non_constant_p, jump_target);
496 1806 : if (VAR_P (expr))
497 0 : expr = DECL_INITIAL (expr);
498 : return expr;
499 : }
500 :
501 : /* Kinds for get_range_elts. */
502 :
503 : enum get_range_elts_kind {
504 : GET_INFO_VEC,
505 : REFLECT_CONSTANT_STRING,
506 : REFLECT_CONSTANT_ARRAY
507 : };
508 :
509 : /* Extract the N-th input_range argument from a metafunction call CALL
510 : and return it as TREE_VEC or STRING_CST or CONSTRUCTOR. Helper function
511 : for get_info_vec, eval_reflect_constant_string and
512 : eval_reflect_constant_array. For GET_INFO_VEC kind, <meta> ensures
513 : the argument is reference to reflection_range concept and so both
514 : range_value_t is info and range_refernce_t is cv info or cv info & or
515 : cv info &&. If N is negative, CALL is the expression to extract
516 : values from rather than N-th argument from CALL. */
517 :
518 : static tree
519 8476 : get_range_elts (location_t loc, const constexpr_ctx *ctx, tree call, int n,
520 : bool *non_constant_p, bool *overflow_p, tree *jump_target,
521 : get_range_elts_kind kind, tree fun)
522 : {
523 8476 : tree arg, parm;
524 8476 : if (n < 0)
525 : arg = parm = call;
526 : else
527 : {
528 7881 : gcc_checking_assert (call_expr_nargs (call) > n);
529 7881 : arg = get_nth_callarg (call, n);
530 7881 : parm = DECL_ARGUMENTS (cp_get_callee_fndecl_nofold (call));
531 11468 : for (int i = 0; i < n; ++i)
532 3587 : parm = DECL_CHAIN (parm);
533 : }
534 8476 : tree type = TREE_TYPE (arg);
535 8476 : gcc_checking_assert (TYPE_REF_P (type));
536 8476 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue, non_constant_p,
537 : overflow_p, jump_target);
538 8476 : if (*jump_target || *non_constant_p)
539 : return NULL_TREE;
540 8472 : tree map[2] = { parm, arg };
541 : /* To speed things up, check
542 : if constexpr (std::ranges::contiguous_range <_R>). */
543 8472 : tree ranges_ns = lookup_qualified_name (std_node, "ranges");
544 8472 : if (TREE_CODE (ranges_ns) != NAMESPACE_DECL)
545 : {
546 0 : error_at (loc, "%<std::ranges%> is not a namespace");
547 0 : *non_constant_p = true;
548 0 : return NULL_TREE;
549 : }
550 8472 : tree contiguous_range
551 8472 : = lookup_qualified_name (ranges_ns, "contiguous_range");
552 8472 : if (TREE_CODE (contiguous_range) != TEMPLATE_DECL
553 8472 : || !concept_definition_p (contiguous_range))
554 : contiguous_range = NULL_TREE;
555 : else
556 : {
557 8472 : tree args = make_tree_vec (1);
558 8472 : TREE_VEC_ELT (args, 0) = TREE_TYPE (type);
559 8472 : contiguous_range = build2_loc (loc, TEMPLATE_ID_EXPR, boolean_type_node,
560 : contiguous_range, args);
561 8472 : if (!integer_nonzerop (maybe_constant_value (contiguous_range)))
562 164 : contiguous_range = NULL_TREE;
563 : }
564 8472 : tree valuet = meta_info_type_node;
565 8472 : tree ret = NULL_TREE;
566 8472 : if (kind != GET_INFO_VEC)
567 : {
568 4330 : tree args = make_tree_vec (1);
569 4330 : TREE_VEC_ELT (args, 0) = TREE_TYPE (type);
570 4330 : tree inst = lookup_template_class (get_identifier ("range_value_t"),
571 : args, /*in_decl*/NULL_TREE,
572 : /*context*/ranges_ns,
573 : tf_warning_or_error);
574 4330 : inst = complete_type (inst);
575 4330 : if (inst == error_mark_node)
576 : {
577 0 : *non_constant_p = true;
578 0 : return NULL_TREE;
579 : }
580 4330 : valuet = TYPE_MAIN_VARIANT (inst);
581 4330 : if (kind == REFLECT_CONSTANT_STRING
582 3582 : && valuet != char_type_node
583 194 : && valuet != wchar_type_node
584 172 : && valuet != char8_type_node
585 70 : && valuet != char16_type_node
586 48 : && valuet != char32_type_node)
587 : {
588 18 : if (!cxx_constexpr_quiet_p (ctx))
589 6 : error_at (loc, "%<reflect_constant_string%> called with %qT "
590 : "rather than %<char%>, %<wchar_t%>, %<char8_t%>, "
591 : "%<char16_t%> or %<char32_t%>", inst);
592 18 : *non_constant_p = true;
593 18 : return NULL_TREE;
594 : }
595 : /* Check for the reflect_object_string special-case, where r
596 : refers to a string literal. In that case CharT() should not
597 : be appended. */
598 3564 : if (kind == REFLECT_CONSTANT_STRING
599 3564 : && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
600 164 : && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) == valuet
601 164 : && TYPE_DOMAIN (TREE_TYPE (type)))
602 : {
603 164 : tree a = arg;
604 164 : tree maxv = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (type)));
605 164 : STRIP_NOPS (a);
606 164 : tree at;
607 164 : if (TREE_CODE (a) == ADDR_EXPR
608 164 : && TREE_CODE (TREE_OPERAND (a, 0)) == STRING_CST
609 160 : && tree_fits_uhwi_p (maxv)
610 160 : && ((unsigned) TREE_STRING_LENGTH (TREE_OPERAND (a, 0))
611 160 : == ((tree_to_uhwi (maxv) + 1)
612 160 : * tree_to_uhwi (TYPE_SIZE_UNIT (valuet))))
613 160 : && (at = TREE_TYPE (TREE_OPERAND (a, 0)))
614 324 : && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
615 : at))
616 160 : return TREE_OPERAND (a, 0);
617 : }
618 4152 : if (kind == REFLECT_CONSTANT_ARRAY)
619 : {
620 748 : tree valuete = strip_array_types (valuet);
621 748 : if (!structural_type_p (valuete))
622 : {
623 12 : if (!cxx_constexpr_quiet_p (ctx))
624 : {
625 4 : auto_diagnostic_group d;
626 4 : error_at (loc, "%<reflect_constant_array%> argument with "
627 : "%qT which is not a structural type", inst);
628 4 : structural_type_p (valuete, true);
629 4 : }
630 12 : *non_constant_p = true;
631 12 : return NULL_TREE;
632 : }
633 736 : TREE_VEC_ELT (args, 0) = build_const_lref (valuete);
634 736 : if (!is_xible (INIT_EXPR, valuete, args))
635 : {
636 12 : if (!cxx_constexpr_quiet_p (ctx))
637 4 : error_at (loc, "%<reflect_constant_array%> argument with %qT "
638 : "which is not copy constructible", inst);
639 12 : *non_constant_p = true;
640 12 : return NULL_TREE;
641 : }
642 724 : TREE_VEC_ELT (args, 0) = TREE_TYPE (type);
643 724 : tree instr
644 724 : = lookup_template_class (get_identifier ("range_reference_t"),
645 : args, /*in_decl*/NULL_TREE,
646 : /*context*/ranges_ns,
647 : tf_warning_or_error);
648 724 : instr = complete_type (instr);
649 724 : if (instr == error_mark_node)
650 : {
651 0 : *non_constant_p = true;
652 0 : return NULL_TREE;
653 : }
654 724 : tree referencet = TYPE_MAIN_VARIANT (instr);
655 724 : TREE_VEC_ELT (args, 0) = referencet;
656 724 : if (valuete != valuet)
657 : {
658 74 : tree rt = non_reference (referencet);
659 74 : if (!same_type_ignoring_top_level_qualifiers_p (valuet, rt))
660 : {
661 0 : if (!cxx_constexpr_quiet_p (ctx))
662 0 : error_at (loc, "%<reflect_constant_array%> argument with "
663 : "%qT which is not compatible with %qT "
664 : "%<std::ranges::range_reference_t%>",
665 : inst, referencet);
666 0 : *non_constant_p = true;
667 0 : return NULL_TREE;
668 : }
669 : }
670 650 : else if (!is_xible (INIT_EXPR, valuet, args))
671 : {
672 0 : if (!cxx_constexpr_quiet_p (ctx))
673 0 : error_at (loc, "%<reflect_constant_array%> argument with %qT "
674 : "which is not constructible from %qT "
675 : "%<std::ranges::range_reference_t%>",
676 : inst, referencet);
677 0 : *non_constant_p = true;
678 0 : return NULL_TREE;
679 : }
680 : }
681 : }
682 8270 : auto_vec<tree, 32> retvec;
683 8270 : tree p = convert_from_reference (parm);
684 25300 : auto obj_call = [=, &map] (tree obj, tsubst_flags_t complain) {
685 17030 : releasing_vec args;
686 17030 : vec_safe_push (args, p);
687 17030 : tree call = finish_call_expr (obj, &args, true, false, complain);
688 17030 : if (call == error_mark_node)
689 : return call;
690 16996 : if (n >= 0)
691 15806 : cp_walk_tree (&call, replace_parm_r, map, NULL);
692 16996 : if (complain != tf_none)
693 820 : return call;
694 16176 : call = cxx_eval_constant_expression (ctx, call, vc_prvalue, non_constant_p,
695 : overflow_p, jump_target);
696 16176 : if (*jump_target || *non_constant_p)
697 2 : return NULL_TREE;
698 : return call;
699 25300 : };
700 8840 : auto ret_retvec = [=, &retvec] () {
701 570 : unsigned HOST_WIDE_INT sz = retvec.length ();
702 2716 : for (size_t i = 0; i < sz; ++i)
703 : {
704 2146 : if (INTEGRAL_TYPE_P (valuet))
705 : {
706 1492 : if (TREE_CODE (retvec[i]) != INTEGER_CST)
707 0 : return throw_exception (loc, ctx,
708 : "array element not a constant integer",
709 0 : fun, non_constant_p, jump_target);
710 : }
711 : else
712 : {
713 654 : gcc_assert (kind == REFLECT_CONSTANT_ARRAY);
714 654 : if (TREE_CODE (valuet) == ARRAY_TYPE)
715 : {
716 222 : retvec[i]
717 222 : = adjust_array_elt (loc, ctx, valuet,
718 222 : unshare_expr (retvec[i]), fun,
719 : non_constant_p, jump_target);
720 222 : if (*jump_target || *non_constant_p)
721 : return NULL_TREE;
722 222 : continue;
723 : }
724 432 : tree expr = convert_reflect_constant_arg (valuet, retvec[i]);
725 432 : if (expr == error_mark_node)
726 0 : return throw_exception (loc, ctx, "reflect_constant failed",
727 0 : fun, non_constant_p, jump_target);
728 432 : if (VAR_P (expr))
729 154 : expr = DECL_INITIAL (expr);
730 432 : retvec[i] = expr;
731 : }
732 : }
733 570 : if (kind == REFLECT_CONSTANT_ARRAY && sz == 0)
734 : {
735 : /* Don't call finish_compound_literal in a template. */
736 22 : if (processing_template_decl)
737 : {
738 0 : *non_constant_p = true;
739 0 : return NULL_TREE;
740 : }
741 : /* Return std::array <valuet, 0> {}. */
742 22 : tree args = make_tree_vec (2);
743 22 : TREE_VEC_ELT (args, 0) = valuet;
744 22 : TREE_VEC_ELT (args, 1) = size_zero_node;
745 22 : tree inst = lookup_template_class (get_identifier ("array"), args,
746 : /*in_decl*/NULL_TREE,
747 : /*context*/std_node,
748 : tf_warning_or_error);
749 22 : tree type = complete_type (inst);
750 22 : if (type == error_mark_node)
751 : {
752 0 : *non_constant_p = true;
753 0 : return NULL_TREE;
754 : }
755 22 : tree ctor = build_constructor (init_list_type_node, nullptr);
756 22 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
757 22 : TREE_CONSTANT (ctor) = true;
758 22 : TREE_STATIC (ctor) = true;
759 22 : tree r = finish_compound_literal (type, ctor, tf_warning_or_error,
760 : fcl_functional);
761 : /* Here, we're evaluating an AGGR_INIT_EXPR, which is already
762 : embedded in a TARGET_EXPR, so we don't want to add another
763 : TARGET_EXPR inside it. Note that SIMPLE_TARGET_EXPR_P would
764 : always be false because the TARGET_EXPR_INITIAL is an
765 : AGGR_INIT_EXPR with void type. */
766 22 : if (TREE_CODE (r) == TARGET_EXPR)
767 22 : r = TARGET_EXPR_INITIAL (r);
768 : return r;
769 : }
770 548 : unsigned esz = tree_to_uhwi (TYPE_SIZE_UNIT (valuet));
771 548 : unsigned last = kind == REFLECT_CONSTANT_STRING ? esz : 0;
772 548 : tree index = build_index_type (size_int (last ? sz : sz - 1));
773 548 : tree at = build_array_type (valuet, index);
774 548 : at = cp_build_qualified_type (at, TYPE_QUAL_CONST);
775 548 : if (kind == REFLECT_CONSTANT_STRING
776 548 : || ((valuet == char_type_node
777 486 : || valuet == wchar_type_node
778 486 : || valuet == char8_type_node
779 486 : || valuet == char16_type_node
780 486 : || valuet == char32_type_node)
781 40 : && integer_zerop (retvec.last ())))
782 : {
783 54 : unsigned HOST_WIDE_INT szt = sz * esz;
784 54 : char *p;
785 54 : if (szt < 4096)
786 54 : p = XALLOCAVEC (char, szt + last);
787 : else
788 0 : p = XNEWVEC (char, szt + last);
789 552 : for (size_t i = 0; i < sz; ++i)
790 498 : native_encode_expr (retvec[i], (unsigned char *) p + i * esz,
791 : esz, 0);
792 54 : if (last)
793 42 : memset (p + szt, '\0', last);
794 54 : tree ret = build_string (szt + last, p);
795 54 : TREE_TYPE (ret) = at;
796 54 : TREE_CONSTANT (ret) = 1;
797 54 : TREE_READONLY (ret) = 1;
798 54 : TREE_STATIC (ret) = 1;
799 54 : if (szt >= 4096)
800 0 : XDELETEVEC (p);
801 : return ret;
802 : }
803 494 : vec<constructor_elt, va_gc> *elts = nullptr;
804 2142 : for (unsigned i = 0; i < sz; ++i)
805 1648 : CONSTRUCTOR_APPEND_ELT (elts, bitsize_int (i), retvec[i]);
806 494 : return build_constructor (at, elts);
807 8270 : };
808 : /* If true, call std::ranges::data (p) and std::ranges::size (p)
809 : and if that works out and what the former returns can be handled,
810 : grab the elements from the initializer of the decl pointed by the
811 : first expression. p has to be convert_from_reference (PARM_DECL)
812 : rather than its value, otherwise it is not considered lvalue. */
813 8270 : if (contiguous_range)
814 : {
815 8106 : tree data = lookup_qualified_name (ranges_ns, "data");
816 8106 : tree size = lookup_qualified_name (ranges_ns, "size");
817 8106 : if (TREE_CODE (data) != VAR_DECL || TREE_CODE (size) != VAR_DECL)
818 : {
819 0 : error_at (loc, "%<std::ranges::data%> or %<std::ranges::size%> "
820 : "are not customization point objects");
821 0 : *non_constant_p = true;
822 0 : return NULL_TREE;
823 : }
824 8106 : data = obj_call (data, tf_none);
825 8106 : if (error_operand_p (data))
826 0 : goto non_contiguous;
827 8106 : if (data == NULL_TREE)
828 : return NULL_TREE;
829 8104 : size = obj_call (size, tf_none);
830 8104 : if (error_operand_p (size))
831 34 : goto non_contiguous;
832 8070 : if (size == NULL_TREE)
833 : return NULL_TREE;
834 8070 : if (!tree_fits_uhwi_p (size) || tree_to_uhwi (size) > INT_MAX)
835 0 : goto non_contiguous;
836 8070 : if (integer_zerop (size))
837 : {
838 1277 : if (kind == GET_INFO_VEC)
839 1255 : return make_tree_vec (0);
840 22 : return ret_retvec ();
841 : }
842 6793 : STRIP_NOPS (data);
843 6793 : unsigned HOST_WIDE_INT minidx = 0, pplus = 0;
844 6793 : if (TREE_CODE (data) == POINTER_PLUS_EXPR
845 24 : && tree_fits_uhwi_p (TREE_OPERAND (data, 1))
846 6817 : && !wi::neg_p (wi::to_wide (TREE_OPERAND (data, 1))))
847 : {
848 24 : pplus = tree_to_uhwi (TREE_OPERAND (data, 1));
849 24 : data = TREE_OPERAND (data, 0);
850 24 : STRIP_NOPS (data);
851 : }
852 6793 : if (TREE_CODE (data) != ADDR_EXPR)
853 0 : goto non_contiguous;
854 6793 : data = TREE_OPERAND (data, 0);
855 6793 : if (TREE_CODE (data) == ARRAY_REF
856 6793 : && tree_fits_uhwi_p (TREE_OPERAND (data, 1)))
857 : {
858 26 : minidx = tree_to_uhwi (TREE_OPERAND (data, 1));
859 26 : data = TREE_OPERAND (data, 0);
860 : }
861 6793 : data = cxx_eval_constant_expression (ctx, data, vc_prvalue,
862 : non_constant_p, overflow_p,
863 : jump_target);
864 6793 : if (*jump_target || *non_constant_p)
865 : return NULL_TREE;
866 6793 : if (TREE_CODE (TREE_TYPE (data)) != ARRAY_TYPE
867 6793 : || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (data))) != valuet)
868 16 : goto non_contiguous;
869 6777 : if (pplus
870 6777 : && (pplus % tree_to_uhwi (TYPE_SIZE_UNIT (valuet))) != 0)
871 0 : goto non_contiguous;
872 6777 : minidx += pplus / tree_to_uhwi (TYPE_SIZE_UNIT (valuet));
873 6777 : if (kind != GET_INFO_VEC && TREE_CODE (data) == STRING_CST)
874 : {
875 3562 : unsigned esz = tree_to_uhwi (TYPE_SIZE_UNIT (valuet));
876 3562 : unsigned HOST_WIDE_INT sz = tree_to_uhwi (size) * esz;
877 3562 : if (minidx > INT_MAX
878 3562 : || (unsigned) TREE_STRING_LENGTH (data) < sz + minidx * esz)
879 0 : goto non_contiguous;
880 3562 : if (kind == REFLECT_CONSTANT_ARRAY && sz == 0)
881 0 : return ret_retvec ();
882 3562 : tree index
883 6924 : = build_index_type (size_int ((kind == REFLECT_CONSTANT_ARRAY
884 : ? -1 : 0) + tree_to_uhwi (size)));
885 3562 : tree at = build_array_type (valuet, index);
886 3562 : at = cp_build_qualified_type (at, TYPE_QUAL_CONST);
887 3562 : const unsigned char *q
888 3562 : = (const unsigned char *) TREE_STRING_POINTER (data);
889 3562 : q += minidx * esz;
890 3562 : if (kind == REFLECT_CONSTANT_ARRAY)
891 : {
892 : unsigned HOST_WIDE_INT i;
893 530 : for (i = 0; i < esz; ++i)
894 334 : if (q[sz - esz + i])
895 : break;
896 200 : if (i != esz)
897 : {
898 : /* Not a NUL terminated string. Build a CONSTRUCTOR
899 : instead. */
900 20 : for (i = 0; i < sz; i += esz)
901 : {
902 16 : tree t = native_interpret_expr (valuet, q + i, sz);
903 16 : retvec.safe_push (t);
904 : }
905 4 : return ret_retvec ();
906 : }
907 : }
908 3558 : char *p;
909 3558 : if (sz < 4096)
910 3558 : p = XALLOCAVEC (char, sz + esz);
911 : else
912 0 : p = XNEWVEC (char, sz + esz);
913 3558 : memcpy (p, q, sz);
914 3558 : memset (p + sz, '\0', esz);
915 3754 : ret = build_string (sz + (kind == REFLECT_CONSTANT_ARRAY
916 : ? 0 : esz), p);
917 3558 : TREE_TYPE (ret) = at;
918 3558 : TREE_CONSTANT (ret) = 1;
919 3558 : TREE_READONLY (ret) = 1;
920 3558 : TREE_STATIC (ret) = 1;
921 3558 : if (sz >= 4096)
922 0 : XDELETEVEC (p);
923 : return ret;
924 : }
925 3215 : if (TREE_CODE (data) != CONSTRUCTOR)
926 0 : goto non_contiguous;
927 3215 : unsigned sz = tree_to_uhwi (size), i;
928 3215 : unsigned HOST_WIDE_INT j = 0;
929 3215 : tree *r, null = NULL_TREE;
930 3215 : if (kind == GET_INFO_VEC)
931 : {
932 2761 : ret = make_tree_vec (sz);
933 2761 : r = TREE_VEC_BEGIN (ret);
934 2761 : null = get_null_reflection ();
935 : }
936 : else
937 : {
938 454 : retvec.safe_grow (sz, true);
939 454 : r = retvec.address ();
940 : }
941 8928 : for (i = 0; i < sz; ++i)
942 5713 : r[i] = null;
943 : tree field, value;
944 8982 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (data), i, field, value)
945 5767 : if (field == NULL_TREE)
946 : {
947 0 : if (j >= minidx && j - minidx < sz)
948 0 : r[j - minidx] = value;
949 0 : ++j;
950 : }
951 5767 : else if (TREE_CODE (field) == RANGE_EXPR)
952 : {
953 0 : tree lo = TREE_OPERAND (field, 0);
954 0 : tree hi = TREE_OPERAND (field, 1);
955 0 : if (!tree_fits_uhwi_p (lo) || !tree_fits_uhwi_p (hi))
956 0 : goto non_contiguous;
957 0 : unsigned HOST_WIDE_INT m = tree_to_uhwi (hi);
958 0 : for (j = tree_to_uhwi (lo); j <= m; ++j)
959 0 : if (j >= minidx && j - minidx < sz)
960 0 : r[j - minidx] = value;
961 : }
962 5767 : else if (tree_fits_uhwi_p (field))
963 : {
964 5767 : j = tree_to_uhwi (field);
965 5767 : if (j >= minidx && j - minidx < sz)
966 5665 : r[j - minidx] = value;
967 5767 : ++j;
968 : }
969 : else
970 0 : goto non_contiguous;
971 3215 : if (kind == GET_INFO_VEC)
972 : return ret;
973 1620 : for (i = 0; i < sz; ++i)
974 1362 : if (r[i] == NULL_TREE || !tree_fits_shwi_p (r[i]))
975 196 : goto non_contiguous;
976 258 : return ret_retvec ();
977 : }
978 164 : non_contiguous:
979 : /* Otherwise, do it the slower way. Initialize two temporaries,
980 : one to std::ranges::base (p) and another to std::ranges::end (p)
981 : and use a loop. */
982 410 : tree begin = lookup_qualified_name (ranges_ns, "begin");
983 410 : tree end = lookup_qualified_name (ranges_ns, "end");
984 410 : if (TREE_CODE (begin) != VAR_DECL || TREE_CODE (end) != VAR_DECL)
985 : {
986 0 : error_at (loc, "missing %<std::ranges::begin%> or %<std::ranges::end%>");
987 0 : *non_constant_p = true;
988 0 : return NULL_TREE;
989 : }
990 410 : begin = obj_call (begin, tf_warning_or_error);
991 410 : if (error_operand_p (begin))
992 : {
993 0 : *non_constant_p = true;
994 0 : return NULL_TREE;
995 : }
996 410 : end = obj_call (end, tf_warning_or_error);
997 410 : if (error_operand_p (end))
998 : {
999 0 : *non_constant_p = true;
1000 0 : return NULL_TREE;
1001 : }
1002 410 : if (!CLASS_TYPE_P (TREE_TYPE (begin)) && !POINTER_TYPE_P (TREE_TYPE (begin)))
1003 : {
1004 0 : error_at (loc, "incorrect type %qT of %<std::ranges::begin(arg)%>",
1005 0 : TREE_TYPE (begin));
1006 0 : *non_constant_p = true;
1007 0 : return NULL_TREE;
1008 : }
1009 410 : if (VOID_TYPE_P (TREE_TYPE (end)))
1010 : {
1011 0 : error_at (loc, "incorrect type %qT of %<std::ranges::end(arg)%>",
1012 0 : TREE_TYPE (end));
1013 0 : *non_constant_p = true;
1014 0 : return NULL_TREE;
1015 : }
1016 410 : begin = get_target_expr (begin);
1017 410 : end = get_target_expr (end);
1018 410 : begin = cxx_eval_constant_expression (ctx, begin, vc_glvalue, non_constant_p,
1019 : overflow_p, jump_target);
1020 410 : if (*jump_target || *non_constant_p)
1021 : return NULL_TREE;
1022 410 : end = cxx_eval_constant_expression (ctx, end, vc_glvalue, non_constant_p,
1023 : overflow_p, jump_target);
1024 410 : if (*jump_target || *non_constant_p)
1025 : return NULL_TREE;
1026 410 : tree cmp = build_new_op (loc, NE_EXPR, LOOKUP_NORMAL, begin, end,
1027 : tf_warning_or_error);
1028 410 : tree deref = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, begin,
1029 : NULL_TREE, tf_warning_or_error);
1030 410 : tree inc = build_new_op (loc, PREINCREMENT_EXPR, LOOKUP_NORMAL, begin,
1031 : NULL_TREE, tf_warning_or_error);
1032 410 : cmp = condition_conversion (cmp);
1033 410 : if (error_operand_p (cmp)
1034 410 : || error_operand_p (deref)
1035 820 : || error_operand_p (inc))
1036 : {
1037 0 : *non_constant_p = true;
1038 0 : return NULL_TREE;
1039 : }
1040 410 : if (TYPE_MAIN_VARIANT (TREE_TYPE (deref)) != valuet)
1041 : {
1042 6 : deref = perform_implicit_conversion (valuet, deref, tf_warning_or_error);
1043 6 : if (error_operand_p (deref))
1044 : {
1045 0 : *non_constant_p = true;
1046 0 : return NULL_TREE;
1047 : }
1048 6 : if (CLASS_TYPE_P (valuet))
1049 : {
1050 2 : deref = force_target_expr (valuet, deref, tf_warning_or_error);
1051 2 : if (error_operand_p (deref))
1052 : {
1053 0 : *non_constant_p = true;
1054 0 : return NULL_TREE;
1055 : }
1056 : }
1057 : }
1058 410 : deref = fold_build_cleanup_point_expr (TREE_TYPE (deref), deref);
1059 410 : inc = fold_build_cleanup_point_expr (void_type_node, inc);
1060 410 : retvec.truncate (0);
1061 : /* while (begin != end) { push (*begin); ++begin; } */
1062 3362 : do
1063 : {
1064 1886 : tree t = cxx_eval_constant_expression (ctx, cmp, vc_prvalue,
1065 : non_constant_p, overflow_p,
1066 : jump_target);
1067 1886 : if (*jump_target || *non_constant_p)
1068 0 : return NULL_TREE;
1069 1886 : if (integer_zerop (t))
1070 : break;
1071 1476 : t = cxx_eval_constant_expression (ctx, deref, vc_prvalue, non_constant_p,
1072 : overflow_p, jump_target);
1073 1476 : if (*jump_target || *non_constant_p)
1074 : return NULL_TREE;
1075 1476 : retvec.safe_push (t);
1076 1476 : cxx_eval_constant_expression (ctx, inc, vc_discard, non_constant_p,
1077 : overflow_p, jump_target);
1078 1476 : if (*jump_target || *non_constant_p)
1079 : return NULL_TREE;
1080 1476 : }
1081 : while (true);
1082 410 : if (kind != GET_INFO_VEC)
1083 286 : return ret_retvec ();
1084 124 : ret = make_tree_vec (retvec.length ());
1085 124 : tree v;
1086 124 : unsigned int i;
1087 9030 : FOR_EACH_VEC_ELT (retvec, i, v)
1088 512 : TREE_VEC_ELT (ret, i) = v;
1089 : return ret;
1090 8270 : }
1091 :
1092 : /* Extract the N-th reflection_range argument from a metafunction call CALL
1093 : and return it as TREE_VEC. */
1094 :
1095 : static tree
1096 4146 : get_info_vec (location_t loc, const constexpr_ctx *ctx, tree call, int n,
1097 : bool *non_constant_p, bool *overflow_p, tree *jump_target,
1098 : tree fun)
1099 : {
1100 0 : return get_range_elts (loc, ctx, call, n, non_constant_p, overflow_p,
1101 0 : jump_target, GET_INFO_VEC, fun);
1102 : }
1103 :
1104 : /* Create std::meta::exception{ what, from }. WHAT is the string for what(),
1105 : and FROM is the info for from(). */
1106 :
1107 : static tree
1108 3256 : get_meta_exception_object (location_t loc, const constexpr_ctx *ctx,
1109 : const char *what, tree from, bool *non_constant_p)
1110 : {
1111 : /* Don't throw in a template. */
1112 3256 : if (processing_template_decl)
1113 : {
1114 0 : *non_constant_p = true;
1115 0 : return NULL_TREE;
1116 : }
1117 :
1118 : /* Don't try to throw exceptions with -fno-exceptions. */
1119 3256 : if (!flag_exceptions)
1120 : {
1121 8 : if (!cxx_constexpr_quiet_p (ctx))
1122 : {
1123 2 : auto_diagnostic_group d;
1124 2 : error_at (loc, "%qD should throw %qs; %<what()%>: %qs",
1125 : from, "std::meta::exception", _(what));
1126 2 : inform (loc, "exceptions are disabled, treating as non-constant");
1127 2 : }
1128 8 : *non_constant_p = true;
1129 8 : return NULL_TREE;
1130 : }
1131 :
1132 3248 : tree type = lookup_qualified_name (std_meta_node, "exception",
1133 : LOOK_want::TYPE, /*complain*/true);
1134 3248 : if (TREE_CODE (type) != TYPE_DECL || !CLASS_TYPE_P (TREE_TYPE (type)))
1135 : {
1136 0 : error_at (loc, "couldn%'t throw %qs", "std::meta::exception");
1137 0 : *non_constant_p = true;
1138 0 : return NULL_TREE;
1139 : }
1140 3248 : type = TREE_TYPE (type);
1141 3248 : vec<constructor_elt, va_gc> *elts = nullptr;
1142 3248 : what = _(what);
1143 : /* Translate what from SOURCE_CHARSET to exec charset. */
1144 3248 : cpp_string istr, ostr;
1145 3248 : istr.len = strlen (what) + 1;
1146 3248 : istr.text = (const unsigned char *) what;
1147 3248 : if (!cpp_translate_string (parse_in, &istr, &ostr, CPP_STRING, false))
1148 : {
1149 0 : what = "";
1150 0 : ostr.text = NULL;
1151 : }
1152 : else
1153 3248 : what = (const char *) ostr.text;
1154 3248 : if (TREE_CODE (from) == FUNCTION_DECL && DECL_TEMPLATE_INFO (from))
1155 970 : from = DECL_TI_TEMPLATE (from);
1156 3248 : tree string_lit = build_string (strlen (what) + 1, what);
1157 3248 : free (const_cast <unsigned char *> (ostr.text));
1158 3248 : TREE_TYPE (string_lit) = char_array_type_node;
1159 3248 : string_lit = fix_string_type (string_lit);
1160 3248 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, string_lit);
1161 3248 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, get_reflection_raw (loc, from));
1162 3248 : tree ctor = build_constructor (init_list_type_node, elts);
1163 3248 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
1164 3248 : TREE_CONSTANT (ctor) = true;
1165 3248 : TREE_STATIC (ctor) = true;
1166 3248 : return finish_compound_literal (type, ctor, tf_warning_or_error,
1167 3248 : fcl_functional);
1168 : }
1169 :
1170 : /* Perform 'throw std::meta::exception{...}'. MSGID is the string for what(),
1171 : FROM is the reflection for from(). */
1172 :
1173 : static tree
1174 3256 : throw_exception (location_t loc, const constexpr_ctx *ctx, const char *msgid,
1175 : tree from, bool *non_constant_p, tree *jump_target)
1176 : {
1177 3256 : if (tree obj = get_meta_exception_object (loc, ctx, msgid, from,
1178 : non_constant_p))
1179 3248 : *jump_target = cxa_allocate_and_throw_exception (loc, ctx, obj);
1180 3256 : return NULL_TREE;
1181 : }
1182 :
1183 : /* Wrapper around throw_exception to complain that the reflection does not
1184 : represent a type. */
1185 :
1186 : static tree
1187 780 : throw_exception_nontype (location_t loc, const constexpr_ctx *ctx,
1188 : tree from, bool *non_constant_p, tree *jump_target)
1189 : {
1190 0 : return throw_exception (loc, ctx,
1191 : "reflection does not represent a type",
1192 0 : from, non_constant_p, jump_target);
1193 : }
1194 :
1195 : /* Wrapper around throw_exception to complain that the reflection does not
1196 : represent something that satisfies has_template_arguments. */
1197 :
1198 : static tree
1199 2 : throw_exception_notargs (location_t loc, const constexpr_ctx *ctx,
1200 : tree from, bool *non_constant_p, tree *jump_target)
1201 : {
1202 0 : return throw_exception (loc, ctx,
1203 : "reflection does not have template arguments",
1204 0 : from, non_constant_p, jump_target);
1205 : }
1206 :
1207 : /* Wrapper around throw_exception to complain that the reflection does not
1208 : represent a function or a function type. */
1209 :
1210 : static tree
1211 12 : throw_exception_nofn (location_t loc, const constexpr_ctx *ctx,
1212 : tree from, bool *non_constant_p, tree *jump_target)
1213 : {
1214 0 : return throw_exception (loc, ctx, "reflection does not represent a "
1215 : "function or function type",
1216 0 : from, non_constant_p, jump_target);
1217 : }
1218 :
1219 : /* The values of std::meta::operators enumerators corresponding to
1220 : the ovl_op_code and IDENTIFIER_ASSIGN_OP_P pair. */
1221 :
1222 : static unsigned char meta_operators[2][OVL_OP_MAX];
1223 :
1224 : /* Init the meta_operators table if not yet initialized. */
1225 :
1226 : static void
1227 592 : maybe_init_meta_operators (location_t loc)
1228 : {
1229 592 : if (meta_operators[0][OVL_OP_ERROR_MARK])
1230 576 : return;
1231 16 : meta_operators[0][OVL_OP_ERROR_MARK] = 1;
1232 16 : tree operators = lookup_qualified_name (std_meta_node, "operators");
1233 16 : if (TREE_CODE (operators) != TYPE_DECL
1234 16 : || TREE_CODE (TREE_TYPE (operators)) != ENUMERAL_TYPE)
1235 : {
1236 0 : fail:
1237 0 : error_at (loc, "unexpected %<std::meta::operators%>");
1238 0 : return;
1239 : }
1240 16 : char buf[sizeof "op_greater_greater_equals"];
1241 16 : memcpy (buf, "op_", 3);
1242 48 : for (int i = 0; i < 2; ++i)
1243 1856 : for (int j = OVL_OP_ERROR_MARK + 1; j < OVL_OP_MAX; ++j)
1244 1824 : if (ovl_op_info[i][j].meta_name)
1245 : {
1246 800 : strcpy (buf + 3, ovl_op_info[i][j].meta_name);
1247 800 : tree id = get_identifier (buf);
1248 800 : tree t = lookup_enumerator (TREE_TYPE (operators), id);
1249 800 : if (t == NULL_TREE || TREE_CODE (t) != CONST_DECL)
1250 0 : goto fail;
1251 800 : tree v = DECL_INITIAL (t);
1252 800 : if (!tree_fits_uhwi_p (v) || tree_to_uhwi (v) > UCHAR_MAX)
1253 0 : goto fail;
1254 800 : meta_operators[i][j] = tree_to_uhwi (v);
1255 : }
1256 : }
1257 :
1258 : /* Process std::meta::is_variable.
1259 : Returns: true if r represents a variable. Otherwise, false. */
1260 :
1261 : static tree
1262 11722 : eval_is_variable (const_tree r, reflect_kind kind)
1263 : {
1264 : /* ^^param is a variable but parameters_of(parent_of(^^param))[0] is not. */
1265 570 : if ((TREE_CODE (r) == PARM_DECL && kind != REFLECT_PARM)
1266 11412 : || (VAR_P (r)
1267 3780 : && kind == REFLECT_UNDEF
1268 : /* A structured binding is not a variable. */
1269 3516 : && !(DECL_DECOMPOSITION_P (r) && !DECL_DECOMP_IS_BASE (r)))
1270 19724 : || (VAR_P (r)
1271 : /* Underlying variable of tuple using structured binding is a
1272 : variable. */
1273 370 : && kind == REFLECT_VAR
1274 30 : && DECL_DECOMPOSITION_P (r)
1275 30 : && !DECL_DECOMP_IS_BASE (r)))
1276 3750 : return boolean_true_node;
1277 : else
1278 7972 : return boolean_false_node;
1279 : }
1280 :
1281 : /* Process std::meta::is_type.
1282 : Returns: true if r represents an entity whose underlying entity is
1283 : a type. Otherwise, false. */
1284 :
1285 : static tree
1286 25114 : eval_is_type (const_tree r)
1287 : {
1288 : /* Null reflection isn't a type. */
1289 25114 : if (TYPE_P (r) && r != unknown_type_node)
1290 20100 : return boolean_true_node;
1291 : else
1292 5014 : return boolean_false_node;
1293 : }
1294 :
1295 : /* Process std::meta::is_type_alias.
1296 : Returns: true if r represents a type alias. Otherwise, false. */
1297 :
1298 : static tree
1299 8874 : eval_is_type_alias (const_tree r)
1300 : {
1301 8874 : if (TYPE_P (r) && typedef_variant_p (r))
1302 1450 : return boolean_true_node;
1303 : else
1304 7424 : return boolean_false_node;
1305 : }
1306 :
1307 : /* Process std::meta::is_namespace.
1308 : Returns: true if r represents an entity whose underlying entity is
1309 : a namespace. Otherwise, false. */
1310 :
1311 : static tree
1312 1526 : eval_is_namespace (const_tree r)
1313 : {
1314 980 : if (TREE_CODE (r) == NAMESPACE_DECL)
1315 114 : return boolean_true_node;
1316 : else
1317 1034 : return boolean_false_node;
1318 : }
1319 :
1320 : /* Process std::meta::is_namespace_alias.
1321 : Returns: true if r represents a namespace alias. Otherwise, false. */
1322 :
1323 : static tree
1324 578 : eval_is_namespace_alias (const_tree r)
1325 : {
1326 578 : if (TREE_CODE (r) == NAMESPACE_DECL && DECL_NAMESPACE_ALIAS (r))
1327 46 : return boolean_true_node;
1328 : else
1329 532 : return boolean_false_node;
1330 : }
1331 :
1332 : /* Process std::meta::is_function.
1333 : Returns: true if r represents a function. Otherwise, false. */
1334 :
1335 : static tree
1336 11356 : eval_is_function (tree r)
1337 : {
1338 11356 : r = maybe_get_first_fn (r);
1339 :
1340 11356 : if (TREE_CODE (r) == FUNCTION_DECL)
1341 5496 : return boolean_true_node;
1342 : else
1343 5860 : return boolean_false_node;
1344 : }
1345 :
1346 : /* Process std::meta::is_function_template.
1347 : Returns: true if r represents a function template. Otherwise, false. */
1348 :
1349 : static tree
1350 5572 : eval_is_function_template (tree r)
1351 : {
1352 5572 : r = maybe_get_first_fn (r);
1353 :
1354 5572 : if (DECL_FUNCTION_TEMPLATE_P (r))
1355 200 : return boolean_true_node;
1356 :
1357 5372 : return boolean_false_node;
1358 : }
1359 :
1360 : /* Process std::meta::is_variable_template.
1361 : Returns: true if r represents a variable template. Otherwise, false. */
1362 :
1363 : static tree
1364 5048 : eval_is_variable_template (tree r)
1365 : {
1366 5048 : if (variable_template_p (r))
1367 120 : return boolean_true_node;
1368 : else
1369 4928 : return boolean_false_node;
1370 : }
1371 :
1372 : /* Process std::meta::is_class_template.
1373 : Returns: true if r represents a class template. Otherwise, false. */
1374 :
1375 : static tree
1376 5474 : eval_is_class_template (const_tree r)
1377 : {
1378 5474 : if (DECL_CLASS_TEMPLATE_P (r))
1379 380 : return boolean_true_node;
1380 : else
1381 5094 : return boolean_false_node;
1382 : }
1383 :
1384 : /* Process std::meta::is_alias_template.
1385 : Returns: true if r represents an alias template. Otherwise, false. */
1386 :
1387 : static tree
1388 4932 : eval_is_alias_template (const_tree r)
1389 : {
1390 4932 : if (DECL_ALIAS_TEMPLATE_P (r))
1391 86 : return boolean_true_node;
1392 : else
1393 4846 : return boolean_false_node;
1394 : }
1395 :
1396 : /* Process std::meta::is_concept.
1397 : Returns: true if r represents a concept. Otherwise, false. */
1398 :
1399 : static tree
1400 4914 : eval_is_concept (const_tree r)
1401 : {
1402 5076 : if (concept_definition_p (r))
1403 8 : return boolean_true_node;
1404 : else
1405 4722 : return boolean_false_node;
1406 : }
1407 :
1408 : /* Process std::meta::is_object.
1409 : Returns: true if r represents an object. Otherwise, false. */
1410 :
1411 : static tree
1412 3584 : eval_is_object (reflect_kind kind)
1413 : {
1414 1396 : if (kind == REFLECT_OBJECT)
1415 86 : return boolean_true_node;
1416 : else
1417 3240 : return boolean_false_node;
1418 : }
1419 :
1420 : /* Process std::meta::is_value.
1421 : Returns: true if r represents a value. Otherwise, false. */
1422 :
1423 : static tree
1424 2822 : eval_is_value (reflect_kind kind)
1425 : {
1426 934 : if (kind == REFLECT_VALUE)
1427 68 : return boolean_true_node;
1428 : else
1429 2670 : return boolean_false_node;
1430 : }
1431 :
1432 : /* Like get_info_vec, but throw exception if any of the elements aren't
1433 : eval_is_type reflections and change their content to the corresponding
1434 : REFLECT_EXPR_HANDLE. */
1435 :
1436 : static tree
1437 2458 : get_type_info_vec (location_t loc, const constexpr_ctx *ctx, tree call, int n,
1438 : bool *non_constant_p, bool *overflow_p, tree *jump_target,
1439 : tree fun)
1440 : {
1441 2458 : tree vec = get_info_vec (loc, ctx, call, n, non_constant_p, overflow_p,
1442 : jump_target, fun);
1443 2458 : if (*jump_target || *non_constant_p)
1444 : return NULL_TREE;
1445 5868 : for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
1446 : {
1447 3442 : tree type = REFLECT_EXPR_HANDLE (TREE_VEC_ELT (vec, i));
1448 3442 : if (eval_is_type (type) != boolean_true_node)
1449 32 : return throw_exception_nontype (loc, ctx, fun, non_constant_p,
1450 32 : jump_target);
1451 3410 : TREE_VEC_ELT (vec, i) = type;
1452 : }
1453 : return vec;
1454 : }
1455 :
1456 : /* Process std::meta::is_structured_binding.
1457 : Returns: true if r represents a structured binding. Otherwise, false. */
1458 :
1459 : static tree
1460 1836 : eval_is_structured_binding (const_tree r, reflect_kind kind)
1461 : {
1462 94 : if (DECL_DECOMPOSITION_P (r)
1463 86 : && !DECL_DECOMP_IS_BASE (r)
1464 1918 : && kind != REFLECT_VAR)
1465 74 : return boolean_true_node;
1466 : else
1467 1762 : return boolean_false_node;
1468 : }
1469 :
1470 : /* Process std::meta::is_class_member.
1471 : Returns: true if r represents a class member. Otherwise, false. */
1472 :
1473 : static tree
1474 59954 : eval_is_class_member (tree r)
1475 : {
1476 59954 : r = maybe_get_first_fn (r);
1477 59954 : if (TREE_CODE (r) == CONST_DECL)
1478 : {
1479 : /* [class.mem.general]/5 - The enumerators of an unscoped enumeration
1480 : defined in the class are members of the class. */
1481 186 : if (UNSCOPED_ENUM_P (DECL_CONTEXT (r)))
1482 138 : r = DECL_CONTEXT (r);
1483 : else
1484 48 : return boolean_false_node;
1485 : }
1486 59768 : else if (TYPE_P (r) && typedef_variant_p (r))
1487 2118 : r = TYPE_NAME (r);
1488 59906 : if (DECL_P (r) && DECL_CLASS_SCOPE_P (r))
1489 55270 : return boolean_true_node;
1490 4636 : else if (TYPE_P (r) && TYPE_CLASS_SCOPE_P (r))
1491 1622 : return boolean_true_node;
1492 : else
1493 3014 : return boolean_false_node;
1494 : }
1495 :
1496 : /* For direct base class relationship R return the binfo related
1497 : to the derived type. */
1498 :
1499 : static tree
1500 3274 : direct_base_derived_binfo (tree r)
1501 : {
1502 : /* Looping needed for multiple virtual inheritance. */
1503 6590 : while (BINFO_INHERITANCE_CHAIN (r))
1504 : r = BINFO_INHERITANCE_CHAIN (r);
1505 3274 : return r;
1506 : }
1507 :
1508 : /* For direct base class relationship R return the derived type
1509 : (i.e. when R is (D, B) it returns D). */
1510 :
1511 : tree
1512 3208 : direct_base_derived (tree r)
1513 : {
1514 3208 : return BINFO_TYPE (direct_base_derived_binfo (r));
1515 : }
1516 :
1517 : /* Helper function for eval_is_{public, protected, private}. */
1518 :
1519 : static tree
1520 438 : eval_is_expected_access (tree r, reflect_kind kind, tree expected_access)
1521 : {
1522 438 : if (eval_is_class_member (r) == boolean_true_node)
1523 : {
1524 386 : r = maybe_get_first_fn (r);
1525 :
1526 386 : if (TYPE_P (r))
1527 : {
1528 90 : if (TYPE_NAME (r) == NULL_TREE || !DECL_P (TYPE_NAME (r)))
1529 0 : return boolean_false_node;
1530 90 : r = TYPE_NAME (r);
1531 : }
1532 :
1533 386 : bool matches = false;
1534 386 : if (expected_access == access_private_node)
1535 110 : matches = TREE_PRIVATE (r);
1536 276 : else if (expected_access == access_protected_node)
1537 120 : matches = TREE_PROTECTED (r);
1538 156 : else if (expected_access == access_public_node)
1539 156 : matches = !(TREE_PRIVATE (r) || TREE_PROTECTED (r));
1540 : else
1541 0 : gcc_unreachable ();
1542 :
1543 230 : if (matches)
1544 : return boolean_true_node;
1545 : else
1546 212 : return boolean_false_node;
1547 : }
1548 :
1549 52 : if (kind == REFLECT_BASE)
1550 : {
1551 10 : gcc_assert (TREE_CODE (r) == TREE_BINFO);
1552 10 : tree c = direct_base_derived_binfo (r);
1553 :
1554 10 : tree base_binfo;
1555 40 : for (unsigned ix = 0; BINFO_BASE_ITERATE (c, ix, base_binfo); ix++)
1556 30 : if (base_binfo == r)
1557 : {
1558 10 : tree access = BINFO_BASE_ACCESS (c, ix);
1559 10 : if (access == expected_access)
1560 : return boolean_true_node;
1561 : else
1562 0 : return boolean_false_node;
1563 : }
1564 0 : gcc_unreachable ();
1565 : }
1566 :
1567 42 : return boolean_false_node;
1568 : }
1569 :
1570 : /* Process std::meta::is_public.
1571 : Returns: true if r represents either:
1572 : - a class member or unnamed bit-field that is public or
1573 : - a direct base class relationship (D, B) for which
1574 : B is a public base class of D.
1575 : Otherwise, false. */
1576 :
1577 : static tree
1578 172 : eval_is_public (tree r, reflect_kind kind)
1579 : {
1580 0 : return eval_is_expected_access (r, kind, access_public_node);
1581 : }
1582 :
1583 : /* Process std::meta::is_protected.
1584 : Returns: true if r represents either:
1585 : - a class member or unnamed bit-field that is protected, or
1586 : - a direct base class relationship (D, B) for which
1587 : B is a protected base class of D.
1588 : Otherwise, false. */
1589 :
1590 : static tree
1591 138 : eval_is_protected (tree r, reflect_kind kind)
1592 : {
1593 0 : return eval_is_expected_access (r, kind, access_protected_node);
1594 : }
1595 :
1596 : /* Process std::meta::is_private
1597 : Returns: true if r represents either:
1598 : - a class member or unnamed bit-field that is private, or
1599 : - a direct base class relationship (D, B) for which
1600 : B is a private base class of D.
1601 : Otherwise, false. */
1602 :
1603 : static tree
1604 128 : eval_is_private (tree r, reflect_kind kind)
1605 : {
1606 0 : return eval_is_expected_access (r, kind, access_private_node);
1607 : }
1608 :
1609 : /* Process std::meta::is_virtual.
1610 : Returns: true if r represents either a virtual member function or a direct
1611 : base class relationship (D,B) for which B is a virtual base class of D.
1612 : Otherwise, false. */
1613 :
1614 : static tree
1615 34 : eval_is_virtual (tree r, reflect_kind kind)
1616 : {
1617 34 : r = maybe_get_first_fn (r);
1618 34 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_VIRTUAL_P (r))
1619 22 : return boolean_true_node;
1620 :
1621 16 : if (kind == REFLECT_BASE && BINFO_VIRTUAL_P (r))
1622 2 : return boolean_true_node;
1623 :
1624 10 : return boolean_false_node;
1625 : }
1626 :
1627 : /* Process std::meta::is_pure_virtual.
1628 : Returns: true if r represents a member function that is pure virtual.
1629 : Otherwise, false. */
1630 :
1631 : static tree
1632 28 : eval_is_pure_virtual (tree r)
1633 : {
1634 28 : r = maybe_get_first_fn (r);
1635 28 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_PURE_VIRTUAL_P (r))
1636 6 : return boolean_true_node;
1637 : else
1638 22 : return boolean_false_node;
1639 : }
1640 :
1641 : /* Helper function for eval_is_override, return true if FNDECL in TYPE
1642 : overrides another function. */
1643 :
1644 : static bool
1645 42 : is_override (tree type, tree fndecl)
1646 : {
1647 42 : tree binfo = TYPE_BINFO (type), base_binfo;
1648 :
1649 44 : for (unsigned ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1650 : {
1651 20 : tree basetype = BINFO_TYPE (base_binfo);
1652 20 : if (TYPE_POLYMORPHIC_P (basetype))
1653 : {
1654 20 : if (look_for_overrides_here (basetype, fndecl))
1655 : return true;
1656 2 : if (is_override (basetype, fndecl))
1657 : return true;
1658 : }
1659 : }
1660 : return false;
1661 : }
1662 :
1663 : /* Process std::meta::is_override.
1664 : Returns: true if r represents a member function that overrides another
1665 : member function. Otherwise, false. */
1666 :
1667 : static tree
1668 140 : eval_is_override (tree r)
1669 : {
1670 140 : r = maybe_get_first_fn (r);
1671 140 : if (TREE_CODE (r) == FUNCTION_DECL
1672 54 : && DECL_VIRTUAL_P (r)
1673 40 : && !DECL_STATIC_FUNCTION_P (r)
1674 180 : && is_override (DECL_CONTEXT (r), r))
1675 18 : return boolean_true_node;
1676 122 : return boolean_false_node;
1677 : }
1678 :
1679 : /* Process std::meta::is_namespace_member.
1680 : Returns: true if r represents a namespace member. Otherwise, false. */
1681 :
1682 : static tree
1683 142 : eval_is_namespace_member (tree r)
1684 : {
1685 142 : r = maybe_get_first_fn (r);
1686 142 : if (TREE_CODE (r) == CONST_DECL)
1687 : {
1688 22 : if (UNSCOPED_ENUM_P (DECL_CONTEXT (r)))
1689 16 : r = DECL_CONTEXT (r);
1690 : else
1691 6 : return boolean_false_node;
1692 : }
1693 120 : else if (TYPE_P (r) && typedef_variant_p (r))
1694 6 : r = TYPE_NAME (r);
1695 136 : if (r == global_namespace || r == unknown_type_node)
1696 4 : return boolean_false_node;
1697 170 : if (DECL_P (r) && DECL_NAMESPACE_SCOPE_P (r))
1698 54 : return boolean_true_node;
1699 94 : else if (TYPE_P (r) && TYPE_NAMESPACE_SCOPE_P (r))
1700 20 : return boolean_true_node;
1701 : else
1702 58 : return boolean_false_node;
1703 : }
1704 :
1705 : /* Process std::meta::is_nonstatic_data_member.
1706 : Returns: true if r represents a non-static data member.
1707 : Otherwise, false. */
1708 :
1709 : static tree
1710 7180 : eval_is_nonstatic_data_member (const_tree r)
1711 : {
1712 7180 : if (TREE_CODE (r) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (r))
1713 1424 : return boolean_true_node;
1714 : else
1715 5756 : return boolean_false_node;
1716 : }
1717 :
1718 : /* Process std::meta::is_static_member.
1719 : Returns: true if r represents a static member.
1720 : Otherwise, false. */
1721 :
1722 : static tree
1723 136 : eval_is_static_member (tree r)
1724 : {
1725 136 : r = maybe_get_first_fn (r);
1726 136 : r = STRIP_TEMPLATE (r);
1727 136 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (r))
1728 8 : return boolean_true_node;
1729 128 : else if (VAR_P (r) && DECL_CLASS_SCOPE_P (r))
1730 2 : return boolean_true_node;
1731 : else
1732 126 : return boolean_false_node;
1733 : }
1734 :
1735 : /* Process std::meta::is_base.
1736 : Returns: true if r represents a direct base class relationship.
1737 : Otherwise, false. */
1738 :
1739 : static tree
1740 228 : eval_is_base (tree r, reflect_kind kind)
1741 : {
1742 228 : if (kind == REFLECT_BASE)
1743 : {
1744 112 : gcc_assert (TREE_CODE (r) == TREE_BINFO);
1745 112 : return boolean_true_node;
1746 : }
1747 : else
1748 116 : return boolean_false_node;
1749 : }
1750 :
1751 : /* Process std::meta::has_default_member_initializer.
1752 : Returns: true if r represents a non-static data member that has a default
1753 : member initializer. Otherwise, false. */
1754 :
1755 : static tree
1756 128 : eval_has_default_member_initializer (const_tree r)
1757 : {
1758 128 : if (TREE_CODE (r) == FIELD_DECL
1759 10 : && !DECL_UNNAMED_BIT_FIELD (r)
1760 138 : && DECL_INITIAL (r) != NULL_TREE)
1761 4 : return boolean_true_node;
1762 : else
1763 124 : return boolean_false_node;
1764 : }
1765 :
1766 : /* Process std::meta::has_static_storage_duration.
1767 : Returns: true if r represents an object or variable that has static
1768 : storage duration. Otherwise, false. */
1769 :
1770 : static tree
1771 300 : eval_has_static_storage_duration (const_tree r, reflect_kind kind)
1772 : {
1773 300 : if (eval_is_variable (r, kind) == boolean_true_node
1774 300 : && decl_storage_duration (const_cast<tree> (r)) == dk_static)
1775 120 : return boolean_true_node;
1776 : /* This includes DECL_NTTP_OBJECT_P objects. */
1777 360 : else if (eval_is_object (kind) == boolean_true_node)
1778 : return boolean_true_node;
1779 : else
1780 170 : return boolean_false_node;
1781 : }
1782 :
1783 : /* Process std::meta::has_thread_storage_duration.
1784 : Returns: true if r represents an object or variable that has thread
1785 : storage duration. Otherwise, false. */
1786 :
1787 : static tree
1788 140 : eval_has_thread_storage_duration (const_tree r, reflect_kind kind)
1789 : {
1790 140 : if (eval_is_variable (r, kind) == boolean_true_node
1791 140 : && decl_storage_duration (const_cast<tree> (r)) == dk_thread)
1792 8 : return boolean_true_node;
1793 : else
1794 132 : return boolean_false_node;
1795 : }
1796 :
1797 : /* Process std::meta::has_automatic_storage_duration.
1798 : Returns: true if r represents an object or variable that has automatic
1799 : storage duration. Otherwise, false. */
1800 :
1801 : static tree
1802 140 : eval_has_automatic_storage_duration (const_tree r, reflect_kind kind)
1803 : {
1804 140 : if (eval_is_variable (r, kind) == boolean_true_node
1805 140 : && decl_storage_duration (const_cast<tree> (r)) == dk_auto)
1806 8 : return boolean_true_node;
1807 : else
1808 132 : return boolean_false_node;
1809 : }
1810 :
1811 : /* Process std::meta::is_mutable_member.
1812 : Returns: true if r represents a mutable non-static data member.
1813 : Otherwise, false. */
1814 :
1815 : static tree
1816 128 : eval_is_mutable_member (tree r)
1817 : {
1818 128 : if (TREE_CODE (r) == FIELD_DECL
1819 12 : && !DECL_UNNAMED_BIT_FIELD (r)
1820 140 : && DECL_MUTABLE_P (r))
1821 6 : return boolean_true_node;
1822 : else
1823 122 : return boolean_false_node;
1824 : }
1825 :
1826 : /* Process std::meta::is_template.
1827 : Returns: true if r represents a function template, class template, variable
1828 : template, alias template, or concept. Otherwise, false. */
1829 :
1830 : static tree
1831 4922 : eval_is_template (tree r)
1832 : {
1833 4922 : if (eval_is_function_template (r) == boolean_true_node
1834 4784 : || eval_is_class_template (r) == boolean_true_node
1835 4466 : || eval_is_variable_template (r) == boolean_true_node
1836 4378 : || eval_is_alias_template (r) == boolean_true_node
1837 9244 : || eval_is_concept (r) == boolean_true_node)
1838 : return boolean_true_node;
1839 : else
1840 4160 : return boolean_false_node;
1841 : }
1842 :
1843 : /* Process std::meta::is_function_parameter.
1844 : Returns: true if r represents a function parameter. Otherwise, false. */
1845 :
1846 : static tree
1847 8810 : eval_is_function_parameter (const_tree r, reflect_kind kind)
1848 : {
1849 8810 : if (kind == REFLECT_PARM)
1850 : {
1851 778 : gcc_checking_assert (TREE_CODE (r) == PARM_DECL);
1852 778 : return boolean_true_node;
1853 : }
1854 : else
1855 8032 : return boolean_false_node;
1856 : }
1857 :
1858 : /* Process std::meta::is_data_member_spec.
1859 : Returns: true if r represents a data member description.
1860 : Otherwise, false. */
1861 :
1862 : static tree
1863 198 : eval_is_data_member_spec (const_tree r, reflect_kind kind)
1864 : {
1865 198 : if (kind == REFLECT_DATA_MEMBER_SPEC)
1866 : {
1867 100 : gcc_checking_assert (TREE_CODE (r) == TREE_VEC);
1868 100 : return boolean_true_node;
1869 : }
1870 : else
1871 98 : return boolean_false_node;
1872 : }
1873 :
1874 : /* Process std::meta::is_explicit_object_parameter.
1875 : Returns: true if r represents a function parameter that is an explicit
1876 : object parameter. Otherwise, false. */
1877 :
1878 : static tree
1879 148 : eval_is_explicit_object_parameter (tree r, reflect_kind kind)
1880 : {
1881 148 : if (eval_is_function_parameter (r, kind) == boolean_false_node)
1882 : return boolean_false_node;
1883 18 : r = maybe_update_function_parm (r);
1884 18 : tree fn = DECL_CONTEXT (r);
1885 18 : if (r == DECL_ARGUMENTS (fn) && DECL_XOBJ_MEMBER_FUNCTION_P (fn))
1886 6 : return boolean_true_node;
1887 : else
1888 12 : return boolean_false_node;
1889 : }
1890 :
1891 : /* Process std::meta::has_default_argument.
1892 : Returns: If r represents a parameter P of a function F, then:
1893 : -- If F is a specialization of a templated function T, then true if there
1894 : exists a declaration D of T that precedes some point in the evaluation
1895 : context and D specifies a default argument for the parameter of T
1896 : corresponding to P. Otherwise, false.
1897 : -- Otherwise, if there exists a declaration D of F that precedes some
1898 : point in the evaluation context and D specifies a default argument
1899 : for P, then true.
1900 : Otherwise, false. */
1901 :
1902 : static tree
1903 152 : eval_has_default_argument (tree r, reflect_kind kind)
1904 : {
1905 152 : if (eval_is_function_parameter (r, kind) == boolean_false_node)
1906 : return boolean_false_node;
1907 54 : r = maybe_update_function_parm (r);
1908 54 : if (DECL_HAS_DEFAULT_ARGUMENT_P (r))
1909 4 : return boolean_true_node;
1910 50 : tree fn = DECL_CONTEXT (r);
1911 50 : tree args = FUNCTION_FIRST_USER_PARM (fn);
1912 50 : tree types = FUNCTION_FIRST_USER_PARMTYPE (fn);
1913 128 : while (r != args)
1914 : {
1915 28 : args = DECL_CHAIN (args);
1916 28 : types = TREE_CHAIN (types);
1917 : }
1918 50 : if (TREE_PURPOSE (types))
1919 34 : return boolean_true_node;
1920 : else
1921 16 : return boolean_false_node;
1922 : }
1923 :
1924 : /* Process std::meta::is_vararg_function.
1925 : Returns: true if r represents a function or function type that
1926 : is a vararg function. Otherwise, false. */
1927 :
1928 : static tree
1929 144 : eval_is_vararg_function (tree r)
1930 : {
1931 144 : r = MAYBE_BASELINK_FUNCTIONS (r);
1932 144 : if (TREE_CODE (r) == FUNCTION_DECL)
1933 32 : r = TREE_TYPE (r);
1934 144 : if (FUNC_OR_METHOD_TYPE_P (r) && stdarg_p (r))
1935 24 : return boolean_true_node;
1936 : else
1937 120 : return boolean_false_node;
1938 : }
1939 :
1940 : /* Process std::meta::is_deleted.
1941 : Returns: true if r represents a function that is deleted.
1942 : Otherwise, false. */
1943 :
1944 : static tree
1945 252 : eval_is_deleted (tree r)
1946 : {
1947 252 : r = maybe_get_first_fn (r);
1948 252 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_MAYBE_DELETED (r))
1949 : {
1950 4 : ++function_depth;
1951 4 : maybe_synthesize_method (r);
1952 4 : --function_depth;
1953 : }
1954 252 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_DELETED_FN (r))
1955 58 : return boolean_true_node;
1956 : else
1957 194 : return boolean_false_node;
1958 : }
1959 :
1960 : /* Process std::meta::is_defaulted.
1961 : Returns: true if r represents a function that is defaulted.
1962 : Otherwise, false. */
1963 :
1964 : static tree
1965 718 : eval_is_defaulted (tree r)
1966 : {
1967 718 : r = maybe_get_first_fn (r);
1968 718 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_DEFAULTED_FN (r))
1969 548 : return boolean_true_node;
1970 : else
1971 170 : return boolean_false_node;
1972 : }
1973 :
1974 : /* Process std::meta::is_user_provided.
1975 : Returns: true if r represents a function that is user-provided.
1976 : Otherwise, false. */
1977 :
1978 : static tree
1979 212 : eval_is_user_provided (tree r)
1980 : {
1981 212 : r = maybe_get_first_fn (r);
1982 212 : if (TREE_CODE (r) == FUNCTION_DECL && user_provided_p (r))
1983 24 : return boolean_true_node;
1984 : else
1985 188 : return boolean_false_node;
1986 : }
1987 :
1988 : /* Process std::meta::is_user_declared.
1989 : Returns: true if r represents a function that is user-declared.
1990 : Otherwise, false. */
1991 :
1992 : static tree
1993 212 : eval_is_user_declared (tree r)
1994 : {
1995 212 : r = maybe_get_first_fn (r);
1996 212 : if (TREE_CODE (r) == FUNCTION_DECL && !DECL_ARTIFICIAL (r))
1997 80 : return boolean_true_node;
1998 : else
1999 132 : return boolean_false_node;
2000 : }
2001 :
2002 : /* Process std::meta::is_explicit.
2003 : Returns: true if r represents
2004 : a member function that is declared explicit.
2005 : Otherwise, false.
2006 : If r represents a member function template
2007 : that is declared explicit, is_explicit(r)
2008 : is still false because in general such queries
2009 : for templates cannot be answered. */
2010 :
2011 : static tree
2012 44 : eval_is_explicit (tree r)
2013 : {
2014 44 : r = maybe_get_first_fn (r);
2015 :
2016 44 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_NONCONVERTING_P (r))
2017 8 : return boolean_true_node;
2018 : else
2019 36 : return boolean_false_node;
2020 : }
2021 :
2022 : /* Process std::meta::is_bit_field.
2023 : Returns: true if r represents a bit-field, or if r represents a data member
2024 : description (T,N,A,W,NUA,ANN) for which W is not _|_. Otherwise, false. */
2025 :
2026 : static tree
2027 220 : eval_is_bit_field (const_tree r, reflect_kind kind)
2028 : {
2029 220 : if (TREE_CODE (r) == FIELD_DECL && DECL_C_BIT_FIELD (r))
2030 72 : return boolean_true_node;
2031 148 : else if (kind == REFLECT_DATA_MEMBER_SPEC && TREE_VEC_ELT (r, 3))
2032 4 : return boolean_true_node;
2033 : else
2034 144 : return boolean_false_node;
2035 : }
2036 :
2037 : /* Process std::meta::is_enumerator.
2038 : Returns: true if r represents an enumerator. Otherwise, false. */
2039 :
2040 : static tree
2041 3158 : eval_is_enumerator (const_tree r)
2042 : {
2043 : /* This doesn't check !DECL_TEMPLATE_PARM_P because such CONST_DECLs
2044 : would already have been rejected in get_reflection. */
2045 1604 : if (TREE_CODE (r) == CONST_DECL)
2046 8 : return boolean_true_node;
2047 : else
2048 3000 : return boolean_false_node;
2049 : }
2050 :
2051 : /* Get the linkage name for T, or NULL_TREE for types with no name
2052 : or for typedefs. */
2053 :
2054 : static tree
2055 104 : reflection_type_linkage_name (tree t)
2056 : {
2057 104 : if (OVERLOAD_TYPE_P (t) && !typedef_variant_p (t))
2058 72 : return TYPE_NAME (t);
2059 : return NULL_TREE;
2060 : }
2061 :
2062 : /* Process std::meta::has_internal_linkage.
2063 : Returns: true if r represents a variable, function, type, template, or
2064 : namespace whose name has internal linkage. Otherwise, false. */
2065 :
2066 : static tree
2067 126 : eval_has_internal_linkage (tree r, reflect_kind kind)
2068 : {
2069 126 : if (eval_is_variable (r, kind) == boolean_false_node
2070 88 : && eval_is_function (r) == boolean_false_node
2071 72 : && eval_is_type (r) == boolean_false_node
2072 46 : && eval_is_template (r) == boolean_false_node
2073 162 : && eval_is_namespace (r) == boolean_false_node)
2074 : return boolean_false_node;
2075 104 : r = maybe_get_first_fn (r);
2076 104 : r = STRIP_TEMPLATE (r);
2077 104 : if (TYPE_P (r))
2078 : {
2079 26 : r = reflection_type_linkage_name (r);
2080 26 : if (!r)
2081 : return boolean_false_node;
2082 : }
2083 96 : if (decl_linkage (r) == lk_internal)
2084 16 : return boolean_true_node;
2085 : else
2086 80 : return boolean_false_node;
2087 : }
2088 :
2089 : /* Process std::meta::has_module_linkage.
2090 : Returns: true if r represents a variable, function, type, template, or
2091 : namespace whose name has module linkage. Otherwise, false. */
2092 :
2093 : static tree
2094 126 : eval_has_module_linkage (tree r, reflect_kind kind)
2095 : {
2096 126 : if (eval_is_variable (r, kind) == boolean_false_node
2097 88 : && eval_is_function (r) == boolean_false_node
2098 72 : && eval_is_type (r) == boolean_false_node
2099 46 : && eval_is_template (r) == boolean_false_node
2100 162 : && eval_is_namespace (r) == boolean_false_node)
2101 : return boolean_false_node;
2102 104 : r = maybe_get_first_fn (r);
2103 104 : r = STRIP_TEMPLATE (r);
2104 104 : if (TYPE_P (r))
2105 : {
2106 26 : r = reflection_type_linkage_name (r);
2107 26 : if (!r)
2108 : return boolean_false_node;
2109 : }
2110 96 : if (decl_linkage (r) == lk_module)
2111 2 : return boolean_true_node;
2112 : else
2113 94 : return boolean_false_node;
2114 : }
2115 :
2116 : /* Process std::meta::has_external_linkage.
2117 : Returns: true if r represents a variable, function, type, template, or
2118 : namespace whose name has external linkage. Otherwise, false. */
2119 :
2120 : static tree
2121 126 : eval_has_external_linkage (tree r, reflect_kind kind)
2122 : {
2123 126 : if (eval_is_variable (r, kind) == boolean_false_node
2124 88 : && eval_is_function (r) == boolean_false_node
2125 72 : && eval_is_type (r) == boolean_false_node
2126 46 : && eval_is_template (r) == boolean_false_node
2127 162 : && eval_is_namespace (r) == boolean_false_node)
2128 : return boolean_false_node;
2129 104 : r = maybe_get_first_fn (r);
2130 104 : r = STRIP_TEMPLATE (r);
2131 104 : if (TYPE_P (r))
2132 : {
2133 26 : r = reflection_type_linkage_name (r);
2134 26 : if (!r)
2135 : return boolean_false_node;
2136 : }
2137 96 : if (DECL_EXTERNAL_LINKAGE_P (r))
2138 70 : return boolean_true_node;
2139 : else
2140 26 : return boolean_false_node;
2141 : }
2142 :
2143 : /* Process std::meta::has_c_language_linkage
2144 : Returns: true if r represents a variable, function, or function type with
2145 : C language linkage. Otherwise, false. */
2146 :
2147 : static tree
2148 132 : eval_has_c_language_linkage (tree r, reflect_kind kind)
2149 : {
2150 132 : if (eval_is_variable (r, kind) == boolean_false_node
2151 96 : && eval_is_function (r) == boolean_false_node
2152 206 : && eval_is_function_type (r) == boolean_false_node)
2153 : return boolean_false_node;
2154 58 : r = maybe_get_first_fn (r);
2155 58 : r = STRIP_TEMPLATE (r);
2156 58 : if (TYPE_P (r))
2157 : {
2158 0 : r = reflection_type_linkage_name (r);
2159 0 : if (!r)
2160 : return boolean_false_node;
2161 : }
2162 58 : if (decl_linkage (r) != lk_none && DECL_LANGUAGE (r) == lang_c)
2163 8 : return boolean_true_node;
2164 : else
2165 50 : return boolean_false_node;
2166 : }
2167 :
2168 : /* Process std::meta::has_linkage.
2169 : Returns: true if r represents a variable, function, type, template, or
2170 : namespace whose name has any linkage. Otherwise, false. */
2171 :
2172 : static tree
2173 124 : eval_has_linkage (tree r, reflect_kind kind)
2174 : {
2175 124 : if (eval_is_variable (r, kind) == boolean_false_node
2176 88 : && eval_is_function (r) == boolean_false_node
2177 72 : && eval_is_type (r) == boolean_false_node
2178 46 : && eval_is_template (r) == boolean_false_node
2179 160 : && eval_is_namespace (r) == boolean_false_node)
2180 : return boolean_false_node;
2181 102 : r = maybe_get_first_fn (r);
2182 102 : r = STRIP_TEMPLATE (r);
2183 102 : if (TYPE_P (r))
2184 : {
2185 26 : r = reflection_type_linkage_name (r);
2186 26 : if (!r)
2187 : return boolean_false_node;
2188 : }
2189 94 : if (decl_linkage (r) != lk_none)
2190 86 : return boolean_true_node;
2191 : else
2192 8 : return boolean_false_node;
2193 : }
2194 :
2195 : /* Process std::meta::is_complete_type.
2196 : Returns: true if is_type(r) is true and there is some point in the
2197 : evaluation context from which the type represented by dealias(r) is
2198 : not an incomplete type. Otherwise, false. */
2199 :
2200 : static tree
2201 395 : eval_is_complete_type (const_tree r)
2202 : {
2203 395 : if (eval_is_type (r) == boolean_true_node)
2204 : {
2205 323 : complete_type (const_cast<tree> (r));
2206 323 : if (COMPLETE_TYPE_P (r))
2207 263 : return boolean_true_node;
2208 : }
2209 132 : return boolean_false_node;
2210 : }
2211 :
2212 : /* Process std::meta::is_enumerable_type.
2213 : A type T is enumerable from a point P if either
2214 : -- T is a class type complete at point P or
2215 : -- T is an enumeration type defined by a declaration D such that D is
2216 : reachable from P but P does not occur within an enum-specifier of D.
2217 : Returns: true if dealias(r) represents a type that is enumerable from some
2218 : point in the evaluation context. Otherwise, false. */
2219 :
2220 : static tree
2221 304 : eval_is_enumerable_type (const_tree r)
2222 : {
2223 304 : if (CLASS_TYPE_P (r))
2224 : {
2225 14 : complete_type (const_cast<tree> (r));
2226 14 : if (COMPLETE_TYPE_P (r))
2227 10 : return boolean_true_node;
2228 : }
2229 290 : else if (TREE_CODE (r) == ENUMERAL_TYPE)
2230 : {
2231 278 : r = TYPE_MAIN_VARIANT (r);
2232 278 : if (!ENUM_IS_OPAQUE (r) && !ENUM_BEING_DEFINED_P (r))
2233 148 : return boolean_true_node;
2234 : }
2235 146 : return boolean_false_node;
2236 : }
2237 :
2238 : /* Process std::meta::is_annotation.
2239 : Returns: true if r represents an annotation. Otherwise, false. */
2240 :
2241 : static tree
2242 7138 : eval_is_annotation (const_tree r, reflect_kind kind)
2243 : {
2244 7138 : if (kind == REFLECT_ANNOTATION)
2245 : {
2246 1118 : gcc_assert (TREE_CODE (r) == TREE_LIST);
2247 1118 : return boolean_true_node;
2248 : }
2249 : else
2250 6020 : return boolean_false_node;
2251 : }
2252 :
2253 : /* Process std::meta::is_conversion_function.
2254 : Returns: true if r represents a function that is a conversion function.
2255 : Otherwise, false. */
2256 :
2257 : static tree
2258 3912 : eval_is_conversion_function (tree r)
2259 : {
2260 3912 : r = MAYBE_BASELINK_FUNCTIONS (r);
2261 3912 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_CONV_FN_P (r))
2262 10 : return boolean_true_node;
2263 : else
2264 3902 : return boolean_false_node;
2265 : }
2266 :
2267 : /* Process std::meta::is_operator_function.
2268 : Returns: true if r represents a function that is an operator function.
2269 : Otherwise, false. */
2270 :
2271 : static tree
2272 4700 : eval_is_operator_function (tree r)
2273 : {
2274 4700 : r = maybe_get_first_fn (r);
2275 :
2276 4700 : if (TREE_CODE (r) == FUNCTION_DECL
2277 4422 : && DECL_OVERLOADED_OPERATOR_P (r)
2278 5082 : && !DECL_CONV_FN_P (r))
2279 372 : return boolean_true_node;
2280 : else
2281 4328 : return boolean_false_node;
2282 : }
2283 :
2284 : /* Process std::meta::is_literal_operator.
2285 : Returns: true if r represents a function that is a literal operator.
2286 : Otherwise, false. */
2287 :
2288 : static tree
2289 30 : eval_is_literal_operator (const_tree r)
2290 : {
2291 : /* No MAYBE_BASELINK_FUNCTIONS here because a literal operator
2292 : must be a non-member function. */
2293 30 : if (TREE_CODE (r) == FUNCTION_DECL && UDLIT_OPER_P (DECL_NAME (r)))
2294 2 : return boolean_true_node;
2295 : else
2296 28 : return boolean_false_node;
2297 : }
2298 :
2299 : /* Process std::meta::is_special_member_function.
2300 : Returns: true if r represents a function that is a special member function.
2301 : Otherwise, false. */
2302 :
2303 : static tree
2304 1568 : eval_is_special_member_function (tree r)
2305 : {
2306 1568 : r = MAYBE_BASELINK_FUNCTIONS (r);
2307 1568 : if (TREE_CODE (r) == FUNCTION_DECL && special_memfn_p (r) != sfk_none)
2308 618 : return boolean_true_node;
2309 : else
2310 950 : return boolean_false_node;
2311 : }
2312 :
2313 : /* Process std::meta::is_constructor.
2314 : Returns: true if r represents a function that is a constructor.
2315 : Otherwise, false. */
2316 :
2317 : static tree
2318 5856 : eval_is_constructor (tree r)
2319 : {
2320 5856 : r = MAYBE_BASELINK_FUNCTIONS (r);
2321 10978 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (r))
2322 330 : return boolean_true_node;
2323 : else
2324 5526 : return boolean_false_node;
2325 : }
2326 :
2327 : /* Process std::meta::is_default_constructor.
2328 : Returns: true if r represents a function that is a default constructor.
2329 : Otherwise, false. */
2330 :
2331 : static tree
2332 1352 : eval_is_default_constructor (tree r)
2333 : {
2334 1352 : r = MAYBE_BASELINK_FUNCTIONS (r);
2335 1352 : if (TREE_CODE (r) == FUNCTION_DECL && default_ctor_p (r))
2336 124 : return boolean_true_node;
2337 : else
2338 1228 : return boolean_false_node;
2339 : }
2340 :
2341 : /* Process std::meta::is_copy_constructor.
2342 : Returns: true if r represents a function that is a copy constructor.
2343 : Otherwise, false. */
2344 :
2345 : static tree
2346 760 : eval_is_copy_constructor (tree r)
2347 : {
2348 760 : r = MAYBE_BASELINK_FUNCTIONS (r);
2349 1280 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_COPY_CONSTRUCTOR_P (r))
2350 86 : return boolean_true_node;
2351 : else
2352 674 : return boolean_false_node;
2353 : }
2354 :
2355 : /* Process std::meta::is_move_constructor.
2356 : Returns: true if r represents a function that is a move constructor.
2357 : Otherwise, false. */
2358 :
2359 : static tree
2360 718 : eval_is_move_constructor (tree r)
2361 : {
2362 718 : r = MAYBE_BASELINK_FUNCTIONS (r);
2363 1200 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_MOVE_CONSTRUCTOR_P (r))
2364 72 : return boolean_true_node;
2365 : else
2366 646 : return boolean_false_node;
2367 : }
2368 :
2369 : /* Process std::meta::is_assignment.
2370 : Returns: true if r represents a function that is an assignment operator.
2371 : Otherwise, false. */
2372 :
2373 : static tree
2374 20 : eval_is_assignment (tree r)
2375 : {
2376 20 : r = MAYBE_BASELINK_FUNCTIONS (r);
2377 20 : if (TREE_CODE (r) == FUNCTION_DECL
2378 14 : && DECL_ASSIGNMENT_OPERATOR_P (r)
2379 30 : && DECL_OVERLOADED_OPERATOR_IS (r, NOP_EXPR))
2380 6 : return boolean_true_node;
2381 : else
2382 14 : return boolean_false_node;
2383 : }
2384 :
2385 : /* Process std::meta::is_copy_assignment.
2386 : Returns: true if r represents a function that is a copy assignment
2387 : operator. Otherwise, false. */
2388 :
2389 : static tree
2390 764 : eval_is_copy_assignment (tree r)
2391 : {
2392 764 : r = MAYBE_BASELINK_FUNCTIONS (r);
2393 764 : if (TREE_CODE (r) == FUNCTION_DECL
2394 764 : && special_function_p (r) == sfk_copy_assignment)
2395 86 : return boolean_true_node;
2396 : else
2397 678 : return boolean_false_node;
2398 : }
2399 :
2400 : /* Process std::meta::is_move_assignment.
2401 : Returns: true if r represents a function that is a move assignment
2402 : operator. Otherwise, false. */
2403 :
2404 : static tree
2405 752 : eval_is_move_assignment (tree r)
2406 : {
2407 752 : r = MAYBE_BASELINK_FUNCTIONS (r);
2408 752 : if (TREE_CODE (r) == FUNCTION_DECL
2409 752 : && special_function_p (r) == sfk_move_assignment)
2410 72 : return boolean_true_node;
2411 : else
2412 680 : return boolean_false_node;
2413 : }
2414 :
2415 : /* Process std::meta::is_destructor.
2416 : Returns: true if r represents a function that is a destructor.
2417 : Otherwise, false. */
2418 :
2419 : static tree
2420 7020 : eval_is_destructor (tree r)
2421 : {
2422 7020 : r = maybe_get_first_fn (r);
2423 7020 : if (TREE_CODE (r) == FUNCTION_DECL
2424 7020 : && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (r))
2425 242 : return boolean_true_node;
2426 : else
2427 6778 : return boolean_false_node;
2428 : }
2429 :
2430 : /* Process std::meta::is_conversion_function_template.
2431 : Returns: true if r represents a conversion function template.
2432 : Otherwise, false. */
2433 :
2434 : static tree
2435 92 : eval_is_conversion_function_template (tree r)
2436 : {
2437 92 : r = maybe_get_first_fn (r);
2438 :
2439 92 : if (DECL_FUNCTION_TEMPLATE_P (r) && DECL_CONV_FN_P (r))
2440 8 : return boolean_true_node;
2441 : else
2442 84 : return boolean_false_node;
2443 : }
2444 :
2445 : /* Process std::meta::is_operator_function_template.
2446 : Returns: true if r represents an operator function template.
2447 : Otherwise, false. */
2448 :
2449 : static tree
2450 372 : eval_is_operator_function_template (tree r)
2451 : {
2452 372 : r = maybe_get_first_fn (r);
2453 :
2454 372 : if (DECL_FUNCTION_TEMPLATE_P (r))
2455 : {
2456 148 : r = STRIP_TEMPLATE (r);
2457 148 : if (DECL_OVERLOADED_OPERATOR_P (r) && !DECL_CONV_FN_P (r))
2458 112 : return boolean_true_node;
2459 : }
2460 :
2461 260 : return boolean_false_node;
2462 : }
2463 :
2464 : /* Process std::meta::is_literal_operator_template.
2465 : Returns: true if r represents a literal operator template.
2466 : Otherwise, false. */
2467 :
2468 : static tree
2469 30 : eval_is_literal_operator_template (tree r)
2470 : {
2471 : /* No MAYBE_BASELINK_FUNCTIONS here because a literal operator
2472 : template must be a non-member function template. */
2473 30 : r = OVL_FIRST (r);
2474 :
2475 30 : if (DECL_FUNCTION_TEMPLATE_P (r) && UDLIT_OPER_P (DECL_NAME (r)))
2476 2 : return boolean_true_node;
2477 : else
2478 28 : return boolean_false_node;
2479 : }
2480 :
2481 : /* Process std::meta::is_constructor_template.
2482 : Returns: true if r represents a function that is an operator function
2483 : template. Otherwise, false. */
2484 :
2485 : static tree
2486 144 : eval_is_constructor_template (tree r)
2487 : {
2488 144 : r = maybe_get_first_fn (r);
2489 :
2490 144 : if (DECL_FUNCTION_TEMPLATE_P (r) && DECL_CONSTRUCTOR_P (r))
2491 16 : return boolean_true_node;
2492 : else
2493 128 : return boolean_false_node;
2494 : }
2495 :
2496 : /* Process std::meta::operator_of.
2497 : Returns: The value of the enumerator from the operators whose corresponding
2498 : operator-function-id is the unqualified name of the entity represented by
2499 : r.
2500 : Throws: meta::exception unless r represents an operator function or
2501 : operator function template. */
2502 :
2503 : static tree
2504 480 : eval_operator_of (location_t loc, const constexpr_ctx *ctx, tree r,
2505 : bool *non_constant_p, tree *jump_target, tree ret_type,
2506 : tree fun)
2507 : {
2508 480 : if (eval_is_operator_function (r) == boolean_false_node
2509 480 : && eval_is_operator_function_template (r) == boolean_false_node)
2510 180 : return throw_exception (loc, ctx,
2511 : "reflection does not represent an operator "
2512 : "function or operator function template",
2513 180 : fun, non_constant_p, jump_target);
2514 300 : r = maybe_get_first_fn (r);
2515 300 : r = STRIP_TEMPLATE (r);
2516 300 : maybe_init_meta_operators (loc);
2517 300 : int i = IDENTIFIER_ASSIGN_OP_P (DECL_NAME (r)) ? 1 : 0;
2518 300 : int j = IDENTIFIER_CP_INDEX (DECL_NAME (r));
2519 300 : return build_int_cst (ret_type, meta_operators[i][j]);
2520 : }
2521 :
2522 : /* Helper to build a string literal containing '\0' terminated NAME.
2523 : ELT_TYPE must be either char_type_node or char8_type_node, and the
2524 : function takes care of converting the name from SOURCE_CHARSET
2525 : to ordinary literal charset resp. UTF-8. Returns the string
2526 : literal, or NULL_TREE if the conversion failed. */
2527 :
2528 : static tree
2529 8940 : get_string_literal (const char *name, tree elt_type)
2530 : {
2531 8940 : cpp_string istr, ostr;
2532 8940 : istr.len = strlen (name) + 1;
2533 8940 : istr.text = (const unsigned char *) name;
2534 8940 : if (!cpp_translate_string (parse_in, &istr, &ostr,
2535 8940 : elt_type == char_type_node
2536 : ? CPP_STRING : CPP_UTF8STRING, false))
2537 : return NULL_TREE;
2538 8936 : name = (const char *) ostr.text;
2539 8936 : tree ret = build_string_literal (strlen (name) + 1, name, elt_type);
2540 8936 : free (const_cast <char *> (name));
2541 8936 : return ret;
2542 : }
2543 :
2544 : /* Process std::meta::{,u8}symbol_of.
2545 : Returns: A string_view or u8string_view containing the characters of the
2546 : operator symbol name corresponding to op, respectively encoded with the
2547 : ordinary literal encoding or with UTF-8.
2548 : Throws: meta::exception unless the value of op corresponds to one of the
2549 : enumerators in operators. */
2550 :
2551 : static tree
2552 292 : eval_symbol_of (location_t loc, const constexpr_ctx *ctx, tree expr,
2553 : bool *non_constant_p, tree *jump_target, tree elt_type,
2554 : tree ret_type, tree fun)
2555 : {
2556 292 : maybe_init_meta_operators (loc);
2557 292 : if (!tree_fits_uhwi_p (expr))
2558 : {
2559 0 : fail:
2560 12 : return throw_exception (loc, ctx,
2561 : "operators argument is not a valid operator",
2562 12 : fun, non_constant_p, jump_target);
2563 : }
2564 292 : unsigned HOST_WIDE_INT val = tree_to_uhwi (expr);
2565 390 : for (int i = 0; i < 2; ++i)
2566 12028 : for (int j = OVL_OP_ERROR_MARK + 1; j < OVL_OP_MAX; ++j)
2567 11930 : if (ovl_op_info[i][j].meta_name && meta_operators[i][j] == val)
2568 : {
2569 280 : const char *name = ovl_op_info[i][j].name;
2570 280 : char buf[64];
2571 280 : if (const char *sp = strchr (name, ' '))
2572 : {
2573 12 : memcpy (buf, name, sp - name);
2574 12 : strcpy (buf + (sp - name), sp + 1);
2575 12 : name = buf;
2576 : }
2577 280 : tree str = get_string_literal (name, elt_type);
2578 : /* Basic character set ought to be better convertible
2579 : into ordinary literal character set and must be always
2580 : convertible into UTF-8. */
2581 280 : gcc_checking_assert (str);
2582 280 : releasing_vec args (make_tree_vector_single (str));
2583 280 : tree r = build_special_member_call (NULL_TREE,
2584 : complete_ctor_identifier,
2585 : &args, ret_type, LOOKUP_NORMAL,
2586 : tf_warning_or_error);
2587 280 : return build_cplus_new (ret_type, r, tf_warning_or_error);
2588 280 : }
2589 12 : goto fail;
2590 : }
2591 :
2592 : /* has-type (exposition only).
2593 : Returns: true if r represents a value, annotation, object, variable,
2594 : function whose type does not contain an undeduced placeholder type and
2595 : that is not a constructor or destructor, enumerator, non-static data
2596 : member, unnamed bit-field, direct base class relationship, data member
2597 : description, or function parameter. Otherwise, false. */
2598 :
2599 : static bool
2600 3560 : has_type (tree r, reflect_kind kind)
2601 : {
2602 3560 : r = MAYBE_BASELINK_FUNCTIONS (r);
2603 3560 : if (TREE_CODE (r) == FUNCTION_DECL)
2604 : {
2605 280 : if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
2606 : return false;
2607 244 : resolve_type_of_reflected_decl (r);
2608 244 : if (undeduced_auto_decl (r))
2609 : return false;
2610 228 : return true;
2611 : }
2612 3280 : if (CONSTANT_CLASS_P (r)
2613 2746 : || eval_is_variable (r, kind) == boolean_true_node
2614 1518 : || eval_is_enumerator (r) == boolean_true_node
2615 1406 : || TREE_CODE (r) == FIELD_DECL
2616 1260 : || eval_is_annotation (r, kind) == boolean_true_node
2617 854 : || eval_is_function_parameter (r, kind) == boolean_true_node
2618 772 : || eval_is_object (kind) == boolean_true_node
2619 608 : || eval_is_value (kind) == boolean_true_node
2620 : || kind == REFLECT_BASE
2621 3812 : || kind == REFLECT_DATA_MEMBER_SPEC)
2622 2910 : return true;
2623 : return false;
2624 : }
2625 :
2626 : /* Helper function for eval_type_of. Assuming has_type is true, return
2627 : the std::meta::type_of type (rather than reflection thereof). */
2628 :
2629 : static tree
2630 3044 : type_of (tree r, reflect_kind kind)
2631 : {
2632 3044 : r = MAYBE_BASELINK_FUNCTIONS (r);
2633 3044 : if (TREE_CODE (r) == PARM_DECL && kind == REFLECT_PARM)
2634 : {
2635 82 : r = maybe_update_function_parm (r);
2636 82 : tree fn = DECL_CONTEXT (r);
2637 82 : tree args = FUNCTION_FIRST_USER_PARM (fn);
2638 82 : tree type = FUNCTION_FIRST_USER_PARMTYPE (fn);
2639 274 : while (r != args)
2640 : {
2641 110 : args = DECL_CHAIN (args);
2642 110 : type = TREE_CHAIN (type);
2643 : }
2644 82 : r = TREE_VALUE (type);
2645 82 : }
2646 2962 : else if (kind == REFLECT_BASE)
2647 224 : r = BINFO_TYPE (r);
2648 2738 : else if (kind == REFLECT_DATA_MEMBER_SPEC)
2649 44 : r = TREE_VEC_ELT (r, 0);
2650 2694 : else if (eval_is_annotation (r, kind) == boolean_true_node)
2651 : {
2652 406 : r = TREE_TYPE (TREE_VALUE (TREE_VALUE (r)));
2653 406 : if (CLASS_TYPE_P (r))
2654 : {
2655 34 : auto quals = cp_type_quals (r);
2656 34 : quals |= TYPE_QUAL_CONST;
2657 34 : r = cp_build_qualified_type (r, quals);
2658 : }
2659 : }
2660 2288 : else if (TREE_CODE (r) == FIELD_DECL && DECL_BIT_FIELD_TYPE (r))
2661 : r = DECL_BIT_FIELD_TYPE (r);
2662 2246 : else if (TREE_CODE (r) == FUNCTION_DECL)
2663 212 : r = static_fn_type (r);
2664 : else
2665 2034 : r = TREE_TYPE (r);
2666 3044 : return strip_typedefs (r);
2667 : }
2668 :
2669 : /* Process std::meta::type_of. Returns:
2670 : -- If r represents the ith parameter of a function F, then the ith type
2671 : in the parameter-type-list of F.
2672 : -- Otherwise, if r represents a value, object, variable, function,
2673 : non-static data member, or unnamed bit-field, then the type of what is
2674 : represented by r.
2675 : -- Otherwise, if r represents an annotation, then type_of(constant_of(r)).
2676 : -- Otherwise, if r represents an enumerator N of an enumeration E, then:
2677 : -- If E is defined by a declaration D that precedes a point P in the
2678 : evaluation context and P does not occur within an enum-specifier of
2679 : D, then a reflection of E.
2680 : -- Otherwise, a reflection of the type of N prior to the closing brace
2681 : of the enum-specifier as specified in [dcl.enum].
2682 : -- Otherwise, if r represents a direct base class relationship (D,B), then
2683 : a reflection of B.
2684 : -- Otherwise, for a data member description (T,N,A,W,NUA,ANN), a reflection
2685 : of the type T. */
2686 :
2687 : static tree
2688 1336 : eval_type_of (location_t loc, const constexpr_ctx *ctx, tree r,
2689 : reflect_kind kind, bool *non_constant_p, tree *jump_target,
2690 : tree fun)
2691 : {
2692 1336 : if (!has_type (r, kind))
2693 52 : return throw_exception (loc, ctx, "reflection does not have a type",
2694 52 : fun, non_constant_p, jump_target);
2695 1284 : return get_reflection_raw (loc, type_of (r, kind));
2696 : }
2697 :
2698 : /* Process std::meta::source_location_of.
2699 : Returns: If r represents a value, a type other than a class type or an
2700 : enumeration type, the global namespace, or a data member description,
2701 : then source_location{}. Otherwise, an implementation-defined
2702 : source_location value. */
2703 :
2704 : static tree
2705 120 : eval_source_location_of (location_t loc, tree r, reflect_kind kind,
2706 : tree std_source_location)
2707 : {
2708 120 : if (!NON_UNION_CLASS_TYPE_P (std_source_location))
2709 : {
2710 0 : error_at (loc, "%qT is not a class type", std_source_location);
2711 0 : return error_mark_node;
2712 : }
2713 120 : location_t rloc = UNKNOWN_LOCATION;
2714 120 : if (kind == REFLECT_BASE)
2715 : /* We don't track location_t of the base specifiers, so at least
2716 : for now use location_t of the base parent (i.e. the derived
2717 : class). */
2718 8 : r = direct_base_derived (r);
2719 : else
2720 112 : r = maybe_get_first_fn (r);
2721 120 : if (OVERLOAD_TYPE_P (r) || (TYPE_P (r) && typedef_variant_p (r)))
2722 20 : rloc = DECL_SOURCE_LOCATION (TYPE_NAME (r));
2723 100 : else if (DECL_P (r) && r != global_namespace)
2724 66 : rloc = DECL_SOURCE_LOCATION (r);
2725 34 : else if (eval_is_annotation (r, kind) == boolean_true_node)
2726 26 : rloc = EXPR_LOCATION (TREE_VALUE (TREE_VALUE (r)));
2727 120 : tree decl = NULL_TREE, field = NULL_TREE;
2728 108 : if (rloc != UNKNOWN_LOCATION)
2729 : {
2730 : /* Make sure __builtin_source_location (which depends on
2731 : std::source_location::__impl) will work without errors. */
2732 108 : tree name = get_identifier ("__impl");
2733 108 : decl = lookup_qualified_name (std_source_location, name);
2734 108 : if (TREE_CODE (decl) != TYPE_DECL)
2735 : decl = NULL_TREE;
2736 : else
2737 : {
2738 108 : name = get_identifier ("__builtin_source_location");
2739 108 : decl = lookup_qualified_name (global_namespace, name);
2740 108 : if (TREE_CODE (decl) != FUNCTION_DECL
2741 108 : || !fndecl_built_in_p (decl, BUILT_IN_FRONTEND)
2742 108 : || DECL_FE_FUNCTION_CODE (decl) != CP_BUILT_IN_SOURCE_LOCATION
2743 216 : || !require_deduced_type (decl, tf_warning_or_error))
2744 : decl = NULL_TREE;
2745 : }
2746 : }
2747 108 : if (decl)
2748 : {
2749 108 : field = TYPE_FIELDS (std_source_location);
2750 108 : field = next_aggregate_field (field);
2751 : /* Make sure std::source_location has exactly a single non-static
2752 : data member (_M_impl in libstdc++, __ptr_ in libc++) with pointer
2753 : type. Return {._M_impl = &*.Lsrc_locN}. */
2754 108 : if (field != NULL_TREE
2755 108 : && POINTER_TYPE_P (TREE_TYPE (field))
2756 216 : && !next_aggregate_field (DECL_CHAIN (field)))
2757 : {
2758 108 : tree call = build_call_nary (TREE_TYPE (TREE_TYPE (decl)), decl, 0);
2759 108 : SET_EXPR_LOCATION (call, rloc);
2760 108 : call = fold_builtin_source_location (call);
2761 108 : return build_constructor_single (std_source_location, field, call);
2762 : }
2763 : }
2764 12 : return build_constructor (std_source_location, nullptr);
2765 : }
2766 :
2767 : /* If R is (const T &) &foo, get foo. */
2768 :
2769 : static tree
2770 786 : maybe_get_reference_referent (tree r)
2771 : {
2772 786 : if (TREE_CODE (r) == NOP_EXPR
2773 374 : && TYPE_REF_P (TREE_TYPE (r))
2774 1156 : && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
2775 : {
2776 370 : STRIP_NOPS (r);
2777 370 : r = TREE_OPERAND (r, 0);
2778 : }
2779 786 : return r;
2780 : }
2781 :
2782 : /* Process std::meta::object_of.
2783 : Returns:
2784 : -- If r represents an object, then r.
2785 : -- Otherwise, if r represents a reference, then a reflection of the object
2786 : referred to by that reference.
2787 : -- Otherwise, r represents a variable; a reflection of the object declared
2788 : by that variable.
2789 : Throws: meta::exception unless r is a reflection representing either
2790 : -- an object with static storage duration, or
2791 : -- a variable that either declares or refers to such an object, and if that
2792 : variable is a reference R, then either
2793 : -- R is usable in constant expressions, or
2794 : -- the lifetime of R began within the core constant expression currently
2795 : under evaluation. */
2796 :
2797 : static tree
2798 156 : eval_object_of (location_t loc, const constexpr_ctx *ctx, tree r,
2799 : reflect_kind kind, bool *non_constant_p, bool *overflow_p,
2800 : tree *jump_target, tree fun)
2801 : {
2802 156 : tree orig = r;
2803 156 : tree type = TREE_TYPE (r);
2804 156 : if (type && TYPE_REF_P (type))
2805 28 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue, non_constant_p,
2806 : overflow_p, jump_target);
2807 156 : r = maybe_get_reference_referent (r);
2808 156 : if (eval_has_static_storage_duration (orig, kind) == boolean_false_node
2809 156 : && (orig == r
2810 4 : || eval_has_static_storage_duration (r, kind) == boolean_false_node))
2811 66 : return throw_exception (loc, ctx, "reflection does not represent an"
2812 : " object with static storage duration,"
2813 : " or a reference to such an object",
2814 66 : fun, non_constant_p, jump_target);
2815 90 : return get_reflection_raw (loc, r, REFLECT_OBJECT);
2816 : }
2817 :
2818 : /* Process std::meta::constant_of.
2819 : Let R be a constant expression of type info such that R == r is true.
2820 : If r represents an annotation, then let C be its underlying constant.
2821 : Effects: Equivalent to:
2822 : if constexpr (is_annotation(R)) {
2823 : return C;
2824 : } else if constexpr (is_array_type(type_of(R)) {
2825 : return reflect_constant_array([: R :]);
2826 : } else if constexpr (is_function_type(type_of(R)) {
2827 : return reflect_function([: R :]);
2828 : } else {
2829 : return reflect_constant([: R :]);
2830 : }
2831 : Throws: meta::exception unless either r represents an annotation or
2832 : [: R :] is a valid splice-expression. */
2833 :
2834 : static tree
2835 1256 : eval_constant_of (location_t loc, const constexpr_ctx *ctx, tree r,
2836 : reflect_kind kind, bool *non_constant_p, bool *overflow_p,
2837 : tree *jump_target, tree fun)
2838 : {
2839 1256 : tree type;
2840 1256 : if (has_type (r, kind))
2841 1220 : type = type_of (r, kind);
2842 : else
2843 36 : type = maybe_strip_typedefs (r);
2844 :
2845 : /* So that outer_automatic_var_p works below in check_splice_expr. */
2846 1256 : temp_override<tree> ovr (current_function_decl);
2847 1256 : current_function_decl = cxx_constexpr_caller (ctx);
2848 :
2849 1256 : if (eval_is_annotation (r, kind) == boolean_true_node)
2850 240 : r = tree_strip_any_location_wrapper (TREE_VALUE (TREE_VALUE (r)));
2851 2032 : else if (eval_is_array_type (loc, ctx, type, non_constant_p, fun)
2852 1016 : == boolean_true_node)
2853 : {
2854 398 : const tsubst_flags_t complain = complain_flags (ctx);
2855 : /* Create a call to reflect_constant_array so that we can simply
2856 : let eval_reflect_constant_array do its job. */
2857 398 : tree name = get_identifier ("reflect_constant_array");
2858 398 : tree call = lookup_qualified_name (std_meta_node, name);
2859 398 : if (error_operand_p (call) || !is_overloaded_fn (call))
2860 : {
2861 0 : if (complain)
2862 0 : error_at (loc, "couldn%'t look up %<%D::%D%>", std_meta_node, name);
2863 0 : *non_constant_p = true;
2864 0 : return NULL_TREE;
2865 : }
2866 : /* We want the argument to be a CONSTRUCTOR or a STRING_CST. */
2867 398 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue, non_constant_p,
2868 : overflow_p, jump_target);
2869 398 : if (*jump_target || *non_constant_p)
2870 : return NULL_TREE;
2871 398 : releasing_vec args (make_tree_vector_single (r));
2872 398 : call = finish_call_expr (call, &args, /*disallow_virtual=*/true,
2873 : /*koenig_p=*/false, complain);
2874 398 : if (call == error_mark_node)
2875 : {
2876 0 : *non_constant_p = true;
2877 0 : return NULL_TREE;
2878 : }
2879 398 : return eval_reflect_constant_array (loc, ctx, call, non_constant_p,
2880 398 : overflow_p, jump_target, fun);
2881 398 : }
2882 618 : else if (eval_is_function_type (type) == boolean_true_node)
2883 4 : return eval_reflect_function (loc, ctx, type, r, non_constant_p,
2884 4 : jump_target, fun);
2885 614 : else if (!check_splice_expr (loc, UNKNOWN_LOCATION, r,
2886 : /*address_p=*/false,
2887 : /*member_access_p=*/false,
2888 : /*template_p=*/false,
2889 : /*targs_p=*/false,
2890 : /*complain_p=*/false)
2891 : /* One cannot query the value of a function template.
2892 : ??? But if [:^^X:] where X is a template is OK, should we
2893 : really throw? We need an LWG issue. */
2894 614 : || eval_is_template (r) == boolean_true_node)
2895 48 : return throw_exception (loc, ctx, "reflection does not represent an "
2896 : "annotation or a valid argument to "
2897 : "a splice-expression",
2898 48 : fun, non_constant_p, jump_target);
2899 :
2900 806 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue, non_constant_p,
2901 : overflow_p, jump_target);
2902 806 : if (*jump_target || *non_constant_p)
2903 : return NULL_TREE;
2904 : /* Figure out the type for reflect_constant. */
2905 792 : type = TREE_TYPE (convert_from_reference (r));
2906 792 : type = type_decays_to (type);
2907 792 : type = cv_unqualified (type);
2908 :
2909 792 : return eval_reflect_constant (loc, ctx, type, r, non_constant_p, jump_target,
2910 792 : fun);
2911 1256 : }
2912 :
2913 : /* Process std::meta::dealias.
2914 : Returns: If r represents an entity, then a reflection representing the
2915 : underlying entity of what r represents. Otherwise, r.
2916 : This implements LWG 4427 so we do not throw. */
2917 :
2918 : static tree
2919 198 : eval_dealias (location_t loc, tree r, reflect_kind kind)
2920 : {
2921 198 : r = maybe_strip_typedefs (r);
2922 198 : if (TREE_CODE (r) == NAMESPACE_DECL)
2923 8 : r = ORIGINAL_NAMESPACE (r);
2924 198 : return get_reflection_raw (loc, r, kind);
2925 : }
2926 :
2927 : /* Process std::meta::is_noexcept.
2928 : Returns: true if r represents a noexcept function type or a function
2929 : with a non-throwing exception specification ([except.spec]).
2930 : Otherwise, false.
2931 : Note: If r represents a function template that is declared noexcept,
2932 : is_noexcept (r) is still false because in general such queries
2933 : for templates cannot be answered. */
2934 :
2935 : static tree
2936 350 : eval_is_noexcept (tree r)
2937 : {
2938 350 : if (eval_is_function (r) == boolean_true_node)
2939 : {
2940 154 : r = maybe_get_first_fn (r);
2941 154 : maybe_instantiate_noexcept (r);
2942 154 : if (TYPE_NOTHROW_P (TREE_TYPE (r)))
2943 88 : return boolean_true_node;
2944 : else
2945 66 : return boolean_false_node;
2946 : }
2947 :
2948 392 : if (eval_is_function_type (r) == boolean_true_node
2949 196 : && TYPE_NOTHROW_P (r))
2950 16 : return boolean_true_node;
2951 :
2952 180 : return boolean_false_node;
2953 : }
2954 :
2955 : /* Process std::meta::is_const.
2956 : Let T be type_of(r) if has-type(r) is true. Otherwise, let T be dealias(r).
2957 : Returns: true if T represents a const type, or a const-qualified function
2958 : type. Otherwise, false. */
2959 :
2960 : static tree
2961 228 : eval_is_const (tree r, reflect_kind kind)
2962 : {
2963 228 : if (has_type (r, kind))
2964 100 : r = type_of (r, kind);
2965 : else
2966 128 : r = maybe_strip_typedefs (r);
2967 228 : r = strip_array_types (r);
2968 228 : if (TREE_CODE (r) == METHOD_TYPE)
2969 : {
2970 0 : if (type_memfn_quals (r) & TYPE_QUAL_CONST)
2971 0 : return boolean_true_node;
2972 : }
2973 228 : else if (TYPE_P (r) && TYPE_READONLY (r))
2974 56 : return boolean_true_node;
2975 172 : return boolean_false_node;
2976 : }
2977 :
2978 : /* Process std::meta::is_volatile.
2979 : Let T be type_of(r) if has-type(r) is true. Otherwise, let T be dealias(r).
2980 : Returns: true if T represents a volatile type, or a volatile-qualified
2981 : function type. Otherwise, false. */
2982 :
2983 : static tree
2984 180 : eval_is_volatile (tree r, reflect_kind kind)
2985 : {
2986 180 : if (has_type (r, kind))
2987 64 : r = type_of (r, kind);
2988 : else
2989 116 : r = maybe_strip_typedefs (r);
2990 180 : r = strip_array_types (r);
2991 180 : if (TREE_CODE (r) == METHOD_TYPE)
2992 : {
2993 0 : if (type_memfn_quals (r) & TYPE_QUAL_VOLATILE)
2994 0 : return boolean_true_node;
2995 : }
2996 180 : else if (TYPE_P (r) && TYPE_VOLATILE (r))
2997 38 : return boolean_true_node;
2998 142 : return boolean_false_node;
2999 : }
3000 :
3001 : /* Process std::meta::has_template_arguments.
3002 : Returns: true if r represents a specialization of a function template,
3003 : variable template, class template, or an alias template. Otherwise,
3004 : false. */
3005 :
3006 : static tree
3007 6776 : eval_has_template_arguments (tree r)
3008 : {
3009 6776 : r = MAYBE_BASELINK_FUNCTIONS (r);
3010 : /* For
3011 : typedef cls_tmpl<int> TYPE;
3012 : 'has_template_arguments (^^TYPE)' should be false. */
3013 6776 : if (TYPE_P (r) && typedef_variant_p (r))
3014 1430 : return (alias_template_specialization_p (r, nt_opaque)
3015 1430 : ? boolean_true_node : boolean_false_node);
3016 5346 : if (primary_template_specialization_p (r)
3017 5346 : || variable_template_specialization_p (r))
3018 270 : return boolean_true_node;
3019 : else
3020 5076 : return boolean_false_node;
3021 : }
3022 :
3023 : /* Process std::meta::template_of.
3024 : Returns: A reflection of the template of the specialization represented
3025 : by r.
3026 : Throws: meta::exception unless has_template_arguments(r) is true. */
3027 :
3028 : static tree
3029 78 : eval_template_of (location_t loc, const constexpr_ctx *ctx, tree r,
3030 : bool *non_constant_p, tree *jump_target, tree fun)
3031 : {
3032 78 : if (eval_has_template_arguments (r) != boolean_true_node)
3033 2 : return throw_exception_notargs (loc, ctx, fun, non_constant_p, jump_target);
3034 :
3035 76 : r = MAYBE_BASELINK_FUNCTIONS (r);
3036 76 : if (TYPE_P (r) && typedef_variant_p (r))
3037 28 : r = TI_TEMPLATE (TYPE_ALIAS_TEMPLATE_INFO (r));
3038 62 : else if (CLASS_TYPE_P (r) && CLASSTYPE_TEMPLATE_INFO (r))
3039 46 : r = CLASSTYPE_TI_TEMPLATE (r);
3040 16 : else if (VAR_OR_FUNCTION_DECL_P (r) && DECL_TEMPLATE_INFO (r))
3041 16 : r = DECL_TI_TEMPLATE (r);
3042 : else
3043 0 : gcc_unreachable ();
3044 :
3045 76 : gcc_assert (TREE_CODE (r) == TEMPLATE_DECL);
3046 76 : return get_reflection_raw (loc, r);
3047 : }
3048 :
3049 : /* Process std::meta::has_parent
3050 : Returns:
3051 : -- If r represents the global namespace, then false.
3052 : -- Otherwise, if r represents an entity that has C language linkage,
3053 : then false.
3054 : -- Otherwise, if r represents an entity that has a language linkage
3055 : other than C++ language linkage, then an implementation-defined value.
3056 : -- Otherwise, if r represents a type that is neither a class nor enumeration
3057 : type, then false.
3058 : -- Otherwise, if r represents an entity or direct base class relationship,
3059 : then true.
3060 : -- Otherwise, false. */
3061 :
3062 : static tree
3063 956 : eval_has_parent (tree r, reflect_kind kind)
3064 : {
3065 956 : if (kind == REFLECT_OBJECT
3066 956 : || CONSTANT_CLASS_P (r)
3067 944 : || r == global_namespace
3068 938 : || kind == REFLECT_DATA_MEMBER_SPEC)
3069 24 : return boolean_false_node;
3070 932 : if (TYPE_P (r))
3071 : {
3072 96 : if (TYPE_NAME (r)
3073 96 : && DECL_P (TYPE_NAME (r))
3074 192 : && DECL_LANGUAGE (TYPE_NAME (r)) == lang_c)
3075 0 : return boolean_false_node;
3076 96 : else if (OVERLOAD_TYPE_P (r) || typedef_variant_p (r))
3077 86 : return boolean_true_node;
3078 : else
3079 10 : return boolean_false_node;
3080 : }
3081 836 : r = maybe_get_first_fn (r);
3082 836 : if (kind == REFLECT_BASE)
3083 92 : return boolean_true_node;
3084 744 : if (!DECL_P (r))
3085 0 : return boolean_false_node;
3086 744 : if (TREE_CODE (r) != NAMESPACE_DECL && DECL_LANGUAGE (r) == lang_c)
3087 18 : return boolean_false_node;
3088 726 : return boolean_true_node;
3089 : }
3090 :
3091 : /* Process std::meta::parent_of.
3092 : Returns:
3093 : -- If r represents a non-static data member that is a direct member of an
3094 : anonymous union, or an unnamed bit-field declared within the
3095 : member-specification of such a union, then a reflection representing the
3096 : innermost enclosing anonymous union.
3097 : -- Otherwise, if r represents an enumerator, then a reflection representing
3098 : the corresponding enumeration type.
3099 : -- Otherwise, if r represents a direct base class relationship (D,B), then
3100 : a reflection representing D.
3101 : -- Otherwise, let E be a class, function, or namespace whose class scope,
3102 : function parameter scope, or namespace scope, respectively, is the
3103 : innermost such scope that either is, or encloses, the target scope of a
3104 : declaration of what is represented by r.
3105 : -- If E is the function call operator of a closure type for a
3106 : consteval-block-declaration, then parent_of(parent_of(^^E)).
3107 : -- Otherwise, ^^E. */
3108 :
3109 : static tree
3110 842 : eval_parent_of (location_t loc, const constexpr_ctx *ctx, tree r,
3111 : reflect_kind kind, bool *non_constant_p, tree *jump_target,
3112 : tree fun)
3113 : {
3114 842 : if (eval_has_parent (r, kind) != boolean_true_node)
3115 34 : return throw_exception (loc, ctx, "reflection does not represent an "
3116 : "entity with parent",
3117 34 : fun, non_constant_p, jump_target);
3118 808 : tree c;
3119 808 : r = maybe_get_first_fn (r);
3120 808 : if (TYPE_P (r))
3121 : {
3122 64 : if (TYPE_NAME (r) && DECL_P (TYPE_NAME (r)))
3123 808 : c = CP_DECL_CONTEXT (TYPE_NAME (r));
3124 : else
3125 0 : c = CP_TYPE_CONTEXT (r);
3126 : }
3127 744 : else if (kind == REFLECT_BASE)
3128 90 : c = direct_base_derived (r);
3129 : else
3130 654 : c = CP_DECL_CONTEXT (r);
3131 : tree lam;
3132 904 : while (LAMBDA_FUNCTION_P (c)
3133 60 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (c)))
3134 904 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
3135 36 : c = CP_TYPE_CONTEXT (CP_DECL_CONTEXT (c));
3136 808 : return get_reflection_raw (loc, c);
3137 : }
3138 :
3139 : /* Return std::vector<info>. */
3140 :
3141 : static tree
3142 7156 : get_vector_info ()
3143 : {
3144 7156 : tree args = make_tree_vec (1);
3145 7156 : TREE_VEC_ELT (args, 0) = meta_info_type_node;
3146 7156 : tree inst = lookup_template_class (get_identifier ("vector"), args,
3147 : /*in_decl*/NULL_TREE,
3148 : /*context*/std_node, tf_none);
3149 7156 : inst = complete_type (inst);
3150 7156 : if (inst == error_mark_node || !COMPLETE_TYPE_P (inst))
3151 : {
3152 0 : error ("couldn%'t look up %qs", "std::vector");
3153 0 : return NULL_TREE;
3154 : }
3155 :
3156 : return inst;
3157 : }
3158 :
3159 : /* Build std::vector<info>{ ELTS }. */
3160 :
3161 : static tree
3162 7156 : get_vector_of_info_elts (vec<constructor_elt, va_gc> *elts)
3163 : {
3164 7156 : tree ctor = build_constructor (init_list_type_node, elts);
3165 7156 : CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
3166 7156 : TREE_CONSTANT (ctor) = true;
3167 7156 : TREE_STATIC (ctor) = true;
3168 7156 : tree type = get_vector_info ();
3169 7156 : if (!type)
3170 0 : return error_mark_node;
3171 7156 : tree r = finish_compound_literal (type, ctor, tf_warning_or_error,
3172 : fcl_functional);
3173 : /* Here, we're evaluating an AGGR_INIT_EXPR, which is already
3174 : embedded in a TARGET_EXPR, so we don't want to add another
3175 : TARGET_EXPR inside it. Note that SIMPLE_TARGET_EXPR_P would
3176 : always be false because the TARGET_EXPR_INITIAL is an
3177 : AGGR_INIT_EXPR with void type. */
3178 7156 : if (TREE_CODE (r) == TARGET_EXPR)
3179 7156 : r = TARGET_EXPR_INITIAL (r);
3180 : return r;
3181 : }
3182 :
3183 : /* Process std::meta::parameters_of.
3184 : Returns:
3185 : -- If r represents a function F, then a vector containing reflections of
3186 : the parameters of F, in the order in which they appear in a declaration
3187 : of F.
3188 : -- Otherwise, r represents a function type T; a vector containing
3189 : reflections of the types in parameter-type-list of T, in the order in
3190 : which they appear in the parameter-type-list.
3191 :
3192 : Throws: meta::exception unless r represents a function or a function
3193 : type. */
3194 :
3195 : static tree
3196 702 : eval_parameters_of (location_t loc, const constexpr_ctx *ctx, tree r,
3197 : bool *non_constant_p, tree *jump_target, tree fun)
3198 : {
3199 702 : if (eval_is_function (r) != boolean_true_node
3200 758 : && eval_is_function_type (r) != boolean_true_node)
3201 12 : return throw_exception_nofn (loc, ctx, fun, non_constant_p, jump_target);
3202 :
3203 690 : r = maybe_get_first_fn (r);
3204 690 : vec<constructor_elt, va_gc> *elts = nullptr;
3205 690 : if (TREE_CODE (r) == FUNCTION_DECL)
3206 2734 : for (tree arg = FUNCTION_FIRST_USER_PARM (r); arg; arg = DECL_CHAIN (arg))
3207 2088 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
3208 : get_reflection_raw (loc, arg, REFLECT_PARM));
3209 : else
3210 178 : for (tree arg = TYPE_ARG_TYPES (r); arg && arg != void_list_node;
3211 134 : arg = TREE_CHAIN (arg))
3212 : {
3213 134 : tree type = maybe_strip_typedefs (TREE_VALUE (arg));
3214 134 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
3215 : get_reflection_raw (loc, type));
3216 : }
3217 690 : return get_vector_of_info_elts (elts);
3218 : }
3219 :
3220 : /* Process std::meta::variable_of.
3221 : Returns: The reflection of the parameter variable corresponding to r.
3222 :
3223 : Throws: meta::exception unless
3224 : -- r represents a parameter of a function F and
3225 : -- there is a point P in the evaluation context for which the innermost
3226 : non-block scope enclosing P is the function parameter scope associated
3227 : with F. */
3228 :
3229 : static tree
3230 240 : eval_variable_of (location_t loc, const constexpr_ctx *ctx, tree r,
3231 : reflect_kind kind, bool *non_constant_p, tree *jump_target,
3232 : tree fun)
3233 : {
3234 240 : if (eval_is_function_parameter (r, kind) == boolean_false_node
3235 : /* This doesn't consider the points corresponding to injected
3236 : declarations, but that doesn't seem needed. */
3237 240 : || DECL_CONTEXT (r) != current_function_decl)
3238 116 : return throw_exception (loc, ctx, "reflection does not represent "
3239 : "parameter of current function",
3240 116 : fun, non_constant_p, jump_target);
3241 124 : r = maybe_update_function_parm (r);
3242 124 : return get_reflection_raw (loc, r, REFLECT_UNDEF);
3243 : }
3244 :
3245 : /* Process std::meta::return_type_of.
3246 : Returns: The reflection of the return type of the function or function type
3247 : represented by r.
3248 :
3249 : Throws: meta::exception unless either r represents a function and
3250 : has-type(r) is true or r represents a function type. */
3251 :
3252 : static tree
3253 126 : eval_return_type_of (location_t loc, const constexpr_ctx *ctx, tree r,
3254 : reflect_kind kind, bool *non_constant_p, tree *jump_target,
3255 : tree fun)
3256 : {
3257 48 : if ((eval_is_function (r) != boolean_true_node || !has_type (r, kind))
3258 236 : && eval_is_function_type (r) != boolean_true_node)
3259 100 : return throw_exception (loc, ctx, "reflection does not represent a "
3260 : "function or function type with a return type",
3261 100 : fun, non_constant_p, jump_target);
3262 :
3263 26 : r = MAYBE_BASELINK_FUNCTIONS (r);
3264 26 : if (TREE_CODE (r) == FUNCTION_DECL)
3265 16 : r = TREE_TYPE (r);
3266 26 : r = TREE_TYPE (r);
3267 26 : return get_reflection_raw (loc, r, REFLECT_UNDEF);
3268 : }
3269 :
3270 : /* Process std::meta::offset_of.
3271 : Let V be the offset in bits from the beginning of a complete object of the
3272 : type represented by parent_of(r) to the subobject associated with the
3273 : entity represented by r.
3274 : Returns: {V / CHAR_BIT, V % CHAR_BIT}.
3275 : Throws: meta::exception unless r represents a non-static data member,
3276 : unnamed bit-field, or direct base class relationship (D,B) for which either
3277 : B is not a virtual base class or D is not an abstract class. */
3278 :
3279 : static tree
3280 246 : eval_offset_of (location_t loc, const constexpr_ctx *ctx, tree r,
3281 : reflect_kind kind, tree member_offset, bool *non_constant_p,
3282 : tree *jump_target, tree fun)
3283 : {
3284 246 : tree byte_off = NULL_TREE, bit_off = NULL_TREE;
3285 246 : if (kind == REFLECT_BASE)
3286 : {
3287 88 : tree d = direct_base_derived (r);
3288 88 : if (BINFO_VIRTUAL_P (r) && ABSTRACT_CLASS_TYPE_P (d))
3289 0 : return throw_exception (loc, ctx,
3290 : "reflection of virtual direct base "
3291 : "relationship with abstract derived "
3292 0 : "class", fun, non_constant_p, jump_target);
3293 88 : byte_off = BINFO_OFFSET (r);
3294 : }
3295 158 : else if (TREE_CODE (r) != FIELD_DECL)
3296 92 : return throw_exception (loc, ctx, "reflection unsuitable for offset_of",
3297 92 : fun, non_constant_p, jump_target);
3298 : else
3299 66 : bit_off = bit_position (r);
3300 154 : if (TREE_CODE (bit_off ? bit_off : byte_off) != INTEGER_CST)
3301 0 : return throw_exception (loc, ctx, "non-constant offset for offset_of",
3302 0 : fun, non_constant_p, jump_target);
3303 154 : if (TREE_CODE (member_offset) != RECORD_TYPE)
3304 : {
3305 0 : fail:
3306 0 : error_at (loc, "unexpected return type of %qs", "std::meta::offset_of");
3307 0 : return build_zero_cst (member_offset);
3308 : }
3309 154 : tree bytes = next_aggregate_field (TYPE_FIELDS (member_offset));
3310 154 : if (!bytes || !INTEGRAL_TYPE_P (TREE_TYPE (bytes)))
3311 0 : goto fail;
3312 154 : tree bits = next_aggregate_field (DECL_CHAIN (bytes));
3313 154 : if (!bits || !INTEGRAL_TYPE_P (TREE_TYPE (bits)))
3314 0 : goto fail;
3315 154 : if (next_aggregate_field (DECL_CHAIN (bits)))
3316 0 : goto fail;
3317 154 : tree bytesv;
3318 154 : if (byte_off)
3319 : bytesv = byte_off;
3320 : else
3321 66 : bytesv = size_binop (TRUNC_DIV_EXPR, bit_off, bitsize_unit_node);
3322 154 : bytesv = fold_convert (TREE_TYPE (bytes), bytesv);
3323 154 : tree bitsv;
3324 154 : if (byte_off)
3325 88 : bitsv = build_zero_cst (TREE_TYPE (bits));
3326 : else
3327 : {
3328 66 : bitsv = size_binop (TRUNC_MOD_EXPR, bit_off, bitsize_unit_node);
3329 66 : bitsv = fold_convert (TREE_TYPE (bits), bitsv);
3330 : }
3331 154 : vec<constructor_elt, va_gc> *elts = nullptr;
3332 154 : CONSTRUCTOR_APPEND_ELT (elts, bytes, bytesv);
3333 154 : CONSTRUCTOR_APPEND_ELT (elts, bits, bitsv);
3334 154 : return build_constructor (member_offset, elts);
3335 : }
3336 :
3337 : /* Process std::meta::size_of.
3338 : Returns: If
3339 : -- r represents a non-static data member of type T or a data member
3340 : description (T,N,A,W,NUA,ANN) or
3341 : -- dealias(r) represents a type T,
3342 : then sizeof(T) if T is not a reference type and size_of(add_pointer(^^T))
3343 : otherwise. Otherwise, size_of(type_of(r)).
3344 :
3345 : Throws: meta::exception unless all of the following conditions are met:
3346 : -- dealias(r) is a reflection of a type, object, value, variable of
3347 : non-reference type, non-static data member that is not a bit-field,
3348 : direct base class relationship, or data member description
3349 : (T,N,A,W,NUA,ANN) where W is not _|_.
3350 : -- If dealias(r) represents a type, then is_complete_type(r) is true. */
3351 :
3352 : static tree
3353 210 : eval_size_of (location_t loc, const constexpr_ctx *ctx, tree r,
3354 : reflect_kind kind, tree ret_type, bool *non_constant_p,
3355 : tree *jump_target, tree fun)
3356 : {
3357 210 : if (eval_is_type (r) != boolean_true_node
3358 160 : && eval_is_object (kind) != boolean_true_node
3359 156 : && eval_is_value (kind) != boolean_true_node
3360 152 : && (eval_is_variable (r, kind) != boolean_true_node
3361 36 : || TYPE_REF_P (TREE_TYPE (r)))
3362 124 : && (TREE_CODE (r) != FIELD_DECL || DECL_C_BIT_FIELD (r))
3363 114 : && kind != REFLECT_BASE
3364 290 : && (kind != REFLECT_DATA_MEMBER_SPEC || TREE_VEC_ELT (r, 3)))
3365 76 : return throw_exception (loc, ctx, "reflection not suitable for size_of",
3366 76 : fun, non_constant_p, jump_target);
3367 134 : if (!INTEGRAL_TYPE_P (ret_type))
3368 : {
3369 0 : error_at (loc, "unexpected return type of %qs", "std::meta::size_of");
3370 0 : return build_zero_cst (ret_type);
3371 : }
3372 134 : tree type;
3373 134 : if (TYPE_P (r))
3374 : type = r;
3375 : else
3376 84 : type = type_of (r, kind);
3377 134 : tree ret;
3378 134 : if (FUNC_OR_METHOD_TYPE_P (type))
3379 16 : return throw_exception (loc, ctx,
3380 : "reflection of function type in size_of",
3381 16 : fun, non_constant_p, jump_target);
3382 118 : else if (!complete_type_or_maybe_complain (type, NULL_TREE, tf_none)
3383 : /* No special casing of references needed, c_sizeof_or_alignof_type
3384 : returns the same size for POINTER_TYPE and REFERENCE_TYPE. */
3385 118 : || ((ret = c_sizeof_or_alignof_type (loc, type, true, false, 0))
3386 114 : == error_mark_node))
3387 4 : return throw_exception (loc, ctx,
3388 : "reflection with incomplete type in size_of",
3389 4 : fun, non_constant_p, jump_target);
3390 114 : return fold_convert (ret_type, ret);
3391 : }
3392 :
3393 : /* Process std::meta::alignment_of.
3394 : Returns:
3395 : -- If dealias(r) represents a type T, then alignment_of(add_pointer(r)) if
3396 : T is a reference type and the alignment requirement of T otherwise.
3397 : -- Otherwise, if dealias(r) represents a variable or object, then the
3398 : alignment requirement of the variable or object.
3399 : -- Otherwise, if r represents a direct base class relationship, then
3400 : alignment_of(type_of(r)).
3401 : -- Otherwise, if r represents a non-static data member M of a class C,
3402 : then the alignment of the direct member subobject corresponding to M of a
3403 : complete object of type C.
3404 : -- Otherwise, r represents a data member description (T,N,A,W,NUA,ANN).
3405 : If A is not _|_, then the value A. Otherwise, alignment_of(^^T).
3406 : Throws: meta::exception unless all of the following conditions are met:
3407 : -- dealias(r) is a reflection of a type, object, variable of non-reference
3408 : type, non-static data member that is not a bit-field, direct base class
3409 : relationship, or data member description (T,N,A,W,NUA,ANN) where W is
3410 : _|_.
3411 : -- If dealias(r) represents a type, then is_complete_type(r) is true. */
3412 :
3413 : static tree
3414 210 : eval_alignment_of (location_t loc, const constexpr_ctx *ctx, tree r,
3415 : reflect_kind kind, tree ret_type, bool *non_constant_p,
3416 : tree *jump_target, tree fun)
3417 : {
3418 210 : if (eval_is_type (r) != boolean_true_node
3419 178 : && eval_is_object (kind) != boolean_true_node
3420 166 : && (eval_is_variable (r, kind) != boolean_true_node
3421 50 : || TYPE_REF_P (TREE_TYPE (r)))
3422 124 : && (TREE_CODE (r) != FIELD_DECL || DECL_C_BIT_FIELD (r))
3423 88 : && kind != REFLECT_BASE
3424 294 : && (kind != REFLECT_DATA_MEMBER_SPEC || TREE_VEC_ELT (r, 3)))
3425 80 : return throw_exception (loc, ctx, "reflection not suitable for alignment_of",
3426 80 : fun, non_constant_p, jump_target);
3427 130 : if (!INTEGRAL_TYPE_P (ret_type))
3428 : {
3429 0 : error_at (loc, "unexpected return type of %qs", "std::meta::alignment_of");
3430 0 : return build_zero_cst (ret_type);
3431 : }
3432 130 : tree type;
3433 130 : if (kind == REFLECT_DATA_MEMBER_SPEC)
3434 : {
3435 4 : if (TREE_VEC_ELT (r, 2))
3436 0 : return fold_convert (ret_type, TREE_VEC_ELT (r, 2));
3437 : else
3438 4 : type = TREE_VEC_ELT (r, 0);
3439 : }
3440 126 : else if (kind == REFLECT_BASE)
3441 4 : type = BINFO_TYPE (r);
3442 122 : else if (TREE_CODE (r) == FIELD_DECL
3443 86 : || eval_is_variable (r, kind) == boolean_true_node
3444 166 : || (eval_is_object (kind) == boolean_true_node
3445 12 : && ((DECL_P (r) && TREE_CODE (r) != FUNCTION_DECL)
3446 8 : || TREE_CODE (r) == COMPONENT_REF)))
3447 : {
3448 86 : if (TREE_CODE (r) == COMPONENT_REF)
3449 4 : r = TREE_OPERAND (r, 1);
3450 86 : return build_int_cst (ret_type, MAX (DECL_ALIGN_UNIT (r), 1));
3451 : }
3452 36 : else if (TYPE_P (r))
3453 : type = r;
3454 4 : else if (eval_is_object (kind) == boolean_true_node)
3455 4 : type = TREE_TYPE (r);
3456 : else
3457 0 : gcc_unreachable ();
3458 44 : if (TYPE_REF_P (type))
3459 8 : type = ptr_type_node;
3460 44 : if (FUNC_OR_METHOD_TYPE_P (type))
3461 4 : return throw_exception (loc, ctx, "alignment_of on function type",
3462 4 : fun, non_constant_p, jump_target);
3463 40 : tree ret;
3464 40 : if (!complete_type_or_maybe_complain (type, NULL_TREE, tf_none)
3465 : /* No special casing of references needed, c_sizeof_or_alignof_type
3466 : returns the same alignment for POINTER_TYPE and REFERENCE_TYPE. */
3467 40 : || ((ret = c_sizeof_or_alignof_type (loc, type, false, true, 0))
3468 40 : == error_mark_node))
3469 0 : return throw_exception (loc, ctx,
3470 : "reflection with incomplete type in alignment_of",
3471 0 : fun, non_constant_p, jump_target);
3472 40 : return fold_convert (ret_type, ret);
3473 : }
3474 :
3475 : /* Process std::meta::bit_size_of.
3476 : Returns:
3477 : -- If r represents an unnamed bit-field or a non-static data member that
3478 : is a bit-field with width W, then W.
3479 : -- Otherwise, if r represents a data member description (T,N,A,W,NUA,ANN)
3480 : and W is not _|_, then W.
3481 : -- Otherwise, CHAR_BIT * size_of(r).
3482 :
3483 : Throws: meta::exception unless all of the following conditions are met:
3484 :
3485 : -- dealias(r) is a reflection of a type, object, value, variable of
3486 : non-reference type, non-static data member, unnamed bit-field, direct
3487 : base class relationship, or data member description.
3488 : -- If dealias(r) represents a type T, there is a point within the
3489 : evaluation context from which T is not incomplete. */
3490 :
3491 : static tree
3492 200 : eval_bit_size_of (location_t loc, const constexpr_ctx *ctx, tree r,
3493 : reflect_kind kind, tree ret_type, bool *non_constant_p,
3494 : tree *jump_target, tree fun)
3495 : {
3496 200 : if (eval_is_type (r) != boolean_true_node
3497 172 : && eval_is_object (kind) != boolean_true_node
3498 170 : && eval_is_value (kind) != boolean_true_node
3499 166 : && (eval_is_variable (r, kind) != boolean_true_node
3500 36 : || TYPE_REF_P (TREE_TYPE (r)))
3501 138 : && TREE_CODE (r) != FIELD_DECL
3502 : && kind != REFLECT_BASE
3503 272 : && kind != REFLECT_DATA_MEMBER_SPEC)
3504 60 : return throw_exception (loc, ctx,
3505 : "reflection not suitable for bit_size_of",
3506 60 : fun, non_constant_p, jump_target);
3507 140 : if (!INTEGRAL_TYPE_P (ret_type))
3508 : {
3509 0 : error_at (loc, "unexpected return type of %qs",
3510 : "std::meta::bit_size_of");
3511 0 : return build_zero_cst (ret_type);
3512 : }
3513 140 : tree type;
3514 140 : if (TREE_CODE (r) == FIELD_DECL && DECL_C_BIT_FIELD (r))
3515 56 : return fold_convert (ret_type, DECL_SIZE (r));
3516 84 : else if (TYPE_P (r))
3517 : type = r;
3518 56 : else if (kind == REFLECT_DATA_MEMBER_SPEC && TREE_VEC_ELT (r, 3))
3519 4 : return fold_convert (ret_type, TREE_VEC_ELT (r, 3));
3520 : else
3521 52 : type = type_of (r, kind);
3522 80 : tree ret;
3523 80 : if (!complete_type_or_maybe_complain (type, NULL_TREE, tf_none)
3524 : /* No special casing of references needed, c_sizeof_or_alignof_type
3525 : returns the same size for POINTER_TYPE and REFERENCE_TYPE. */
3526 80 : || ((ret = c_sizeof_or_alignof_type (loc, type, true, false, 0))
3527 76 : == error_mark_node))
3528 8 : return throw_exception (loc, ctx,
3529 : "reflection with incomplete type in bit_size_of",
3530 8 : fun, non_constant_p, jump_target);
3531 72 : ret = size_binop (MULT_EXPR, ret, size_int (BITS_PER_UNIT));
3532 72 : return fold_convert (ret_type, ret);
3533 : }
3534 :
3535 : /* Process std::meta::has_identifier.
3536 : Returns:
3537 : -- If r represents an entity that has a typedef name for linkage purposes,
3538 : then true.
3539 : -- Otherwise, if r represents an unnamed entity, then false.
3540 : -- Otherwise, if r represents a type alias, then !has_template_arguments(r).
3541 : -- Otherwise, if r represents a type, then true if
3542 : -- r represents a cv-unqualified class type and has_template_arguments(r)
3543 : is false, or
3544 : -- r represents a cv-unqualified enumeration type.
3545 : Otherwise, false.
3546 : -- Otherwise, if r represents a class type, then !has_template_arguments(r).
3547 : -- Otherwise, if r represents a function, then true if
3548 : has_template_arguments(r) is false and the function is not a constructor,
3549 : destructor, operator function, or conversion function. Otherwise, false.
3550 : -- Otherwise, if r represents a template, then true if r does not represent
3551 : a constructor template, operator function template, or conversion
3552 : function template. Otherwise, false.
3553 : -- Otherwise, if r represents the ith parameter of a function F that is an
3554 : (implicit or explicit) specialization of a templated function T and the
3555 : ith parameter of the instantiated declaration of T whose template
3556 : arguments are those of F would be instantiated from a pack, then false.
3557 : -- Otherwise, if r represents the parameter P of a function F, then let S
3558 : be the set of declarations, ignoring any explicit instantiations, that
3559 : precede some point in the evaluation context and that declare either F
3560 : or a templated function of which F is a specialization; true if
3561 : -- there is a declaration D in S that introduces a name N for either P
3562 : or the parameter corresponding to P in the templated function that
3563 : D declares and
3564 : -- no declaration in S does so using any name other than N.
3565 : Otherwise, false.
3566 : -- Otherwise, if r represents a variable, then false if the declaration of
3567 : that variable was instantiated from a function parameter pack.
3568 : Otherwise, !has_template_arguments(r).
3569 : -- Otherwise, if r represents a structured binding, then false if the
3570 : declaration of that structured binding was instantiated from a
3571 : structured binding pack. Otherwise, true.
3572 : -- Otherwise, if r represents an enumerator, non-static-data member,
3573 : namespace, or namespace alias, then true.
3574 : -- Otherwise, if r represents a direct base class relationship, then
3575 : has_identifier(type_of(r)).
3576 : -- Otherwise, r represents a data member description (T,N,A,W,NUA,ANN);
3577 : true if N is not _|_. Otherwise, false. */
3578 :
3579 : static tree
3580 7036 : eval_has_identifier (tree r, reflect_kind kind)
3581 : {
3582 7036 : r = maybe_get_first_fn (r);
3583 7036 : if (kind == REFLECT_BASE)
3584 : {
3585 30 : r = type_of (r, kind);
3586 30 : kind = REFLECT_UNDEF;
3587 : }
3588 7036 : if (DECL_P (r)
3589 4980 : && kind != REFLECT_PARM
3590 11794 : && (!DECL_NAME (r) || IDENTIFIER_ANON_P (DECL_NAME (r))))
3591 58 : return boolean_false_node;
3592 6978 : if (TYPE_P (r) && (!TYPE_NAME (r)
3593 3940 : || (TYPE_ANON_P (r) && !typedef_variant_p (r))
3594 2012 : || (DECL_P (TYPE_NAME (r))
3595 2012 : && !DECL_NAME (TYPE_NAME (r)))))
3596 22 : return boolean_false_node;
3597 6956 : if (eval_is_type_alias (r) == boolean_true_node
3598 6956 : || (CLASS_TYPE_P (r) && !cv_qualified_p (r)))
3599 : {
3600 1944 : if (eval_has_template_arguments (r) == boolean_true_node)
3601 0 : return boolean_false_node;
3602 : else
3603 : return boolean_true_node;
3604 : }
3605 5012 : if (TYPE_P (r))
3606 : {
3607 68 : if (TREE_CODE (r) == ENUMERAL_TYPE && !cv_qualified_p (r))
3608 36 : return boolean_true_node;
3609 : else
3610 32 : return boolean_false_node;
3611 : }
3612 4944 : if (eval_is_function (r) == boolean_true_node)
3613 : {
3614 3958 : if (eval_has_template_arguments (r) == boolean_true_node
3615 3954 : || eval_is_constructor (r) == boolean_true_node
3616 3946 : || eval_is_destructor (r) == boolean_true_node
3617 3944 : || eval_is_operator_function (r) == boolean_true_node
3618 7840 : || eval_is_conversion_function (r) == boolean_true_node)
3619 84 : return boolean_false_node;
3620 : else
3621 : return boolean_true_node;
3622 : }
3623 986 : if (eval_is_template (r) == boolean_true_node)
3624 : {
3625 56 : if (eval_is_constructor_template (r) == boolean_true_node
3626 56 : || eval_is_operator_function_template (r) == boolean_true_node
3627 110 : || eval_is_conversion_function_template (r) == boolean_true_node)
3628 2 : return boolean_false_node;
3629 : else
3630 : return boolean_true_node;
3631 : }
3632 930 : if (eval_is_function_parameter (r, kind) == boolean_true_node)
3633 : {
3634 222 : r = maybe_update_function_parm (r);
3635 222 : if (MULTIPLE_NAMES_PARM_P (r))
3636 30 : return boolean_false_node;
3637 192 : if (DECL_NAME (r))
3638 : {
3639 106 : if (strchr (IDENTIFIER_POINTER (DECL_NAME (r)), '#'))
3640 2 : return boolean_false_node;
3641 : else
3642 104 : return boolean_true_node;
3643 : }
3644 86 : if (lookup_attribute ("old parm name", DECL_ATTRIBUTES (r)))
3645 52 : return boolean_true_node;
3646 : else
3647 34 : return boolean_false_node;
3648 : }
3649 708 : if (eval_is_variable (r, kind) == boolean_true_node)
3650 : {
3651 444 : if (strchr (IDENTIFIER_POINTER (DECL_NAME (r)), '#'))
3652 0 : return boolean_false_node;
3653 444 : if (eval_has_template_arguments (r) == boolean_true_node)
3654 0 : return boolean_false_node;
3655 : else
3656 : return boolean_true_node;
3657 : }
3658 264 : if (eval_is_structured_binding (r, kind) == boolean_true_node)
3659 : {
3660 24 : if (strchr (IDENTIFIER_POINTER (DECL_NAME (r)), '#'))
3661 0 : return boolean_false_node;
3662 : else
3663 : return boolean_true_node;
3664 : }
3665 240 : if (eval_is_enumerator (r) == boolean_true_node
3666 232 : || TREE_CODE (r) == FIELD_DECL
3667 338 : || (TREE_CODE (r) == NAMESPACE_DECL && r != global_namespace))
3668 : return boolean_true_node;
3669 26 : if (kind == REFLECT_DATA_MEMBER_SPEC && TREE_VEC_ELT (r, 1))
3670 : return boolean_true_node;
3671 10 : return boolean_false_node;
3672 : }
3673 :
3674 : /* Process std::meta::{,u8}identifier_of.
3675 : Let E be UTF-8 for u8identifier_of, and otherwise the ordinary literal
3676 : encoding.
3677 : Returns: An NTMBS, encoded with E, determined as follows:
3678 : -- If r represents an entity with a typedef name for linkage purposes,
3679 : then that name.
3680 : -- Otherwise, if r represents a literal operator or literal operator
3681 : template, then the ud-suffix of the operator or operator template.
3682 : -- Otherwise, if r represents the parameter P of a function F, then let S
3683 : be the set of declarations, ignoring any explicit instantiations, that
3684 : precede some point in the evaluation context and that declare either F
3685 : or a templated function of which F is a specialization; the name that
3686 : was introduced by a declaration in S for the parameter corresponding
3687 : to P.
3688 : -- Otherwise, if r represents an entity, then the identifier introduced by
3689 : the declaration of that entity.
3690 : -- Otherwise, if r represents a direct base class relationship, then
3691 : identifier_of(type_of(r)) or u8identifier_of(type_of(r)), respectively.
3692 : -- Otherwise, r represents a data member description (T,N,A,W,NUA,ANN);
3693 : a string_view or u8string_view, respectively, containing the identifier
3694 : N.
3695 : Throws: meta::exception unless has_identifier(r) is true and the identifier
3696 : that would be returned (see above) is representable by E. */
3697 :
3698 : static tree
3699 4912 : eval_identifier_of (location_t loc, const constexpr_ctx *ctx, tree r,
3700 : reflect_kind kind, bool *non_constant_p, tree *jump_target,
3701 : tree elt_type, tree ret_type, tree fun)
3702 : {
3703 4912 : if (eval_has_identifier (r, kind) == boolean_false_node)
3704 12 : return throw_exception (loc, ctx,
3705 : "reflection with has_identifier false",
3706 12 : fun, non_constant_p, jump_target);
3707 4900 : r = maybe_get_first_fn (r);
3708 4900 : const char *name = NULL;
3709 4900 : if (kind == REFLECT_BASE)
3710 : {
3711 30 : r = type_of (r, kind);
3712 30 : kind = REFLECT_UNDEF;
3713 : }
3714 4900 : if (eval_is_function_parameter (r, kind) == boolean_true_node)
3715 : {
3716 102 : r = maybe_update_function_parm (r);
3717 102 : if (DECL_NAME (r))
3718 70 : name = IDENTIFIER_POINTER (DECL_NAME (r));
3719 : else
3720 : {
3721 32 : tree opn = lookup_attribute ("old parm name", DECL_ATTRIBUTES (r));
3722 32 : opn = TREE_VALUE (TREE_VALUE (opn));
3723 32 : name = IDENTIFIER_POINTER (opn);
3724 : }
3725 : }
3726 4798 : else if (DECL_P (r) && UDLIT_OPER_P (DECL_NAME (r)))
3727 6 : name = UDLIT_OP_SUFFIX (DECL_NAME (r));
3728 4792 : else if (DECL_P (r))
3729 3336 : name = IDENTIFIER_POINTER (DECL_NAME (r));
3730 1456 : else if (TYPE_P (r))
3731 : {
3732 1444 : if (DECL_P (TYPE_NAME (r)))
3733 1444 : name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (r)));
3734 : else
3735 0 : name = IDENTIFIER_POINTER (TYPE_NAME (r));
3736 : }
3737 12 : else if (kind == REFLECT_DATA_MEMBER_SPEC)
3738 12 : name = IDENTIFIER_POINTER (TREE_VEC_ELT (r, 1));
3739 : else
3740 0 : gcc_unreachable ();
3741 4900 : tree str = get_string_literal (name, elt_type);
3742 4900 : if (str == NULL_TREE)
3743 : {
3744 0 : if (elt_type == char_type_node)
3745 0 : return throw_exception (loc, ctx, "identifier_of not representable"
3746 : " in ordinary literal encoding",
3747 0 : fun, non_constant_p, jump_target);
3748 : else
3749 0 : return throw_exception (loc, ctx, "u8identifier_of not representable"
3750 : " in UTF-8",
3751 0 : fun, non_constant_p, jump_target);
3752 : }
3753 4900 : releasing_vec args (make_tree_vector_single (str));
3754 4900 : tree ret = build_special_member_call (NULL_TREE, complete_ctor_identifier,
3755 : &args, ret_type, LOOKUP_NORMAL,
3756 : tf_warning_or_error);
3757 4900 : return build_cplus_new (ret_type, ret, tf_warning_or_error);
3758 4900 : }
3759 :
3760 : /* Display R, which is a data member description. */
3761 :
3762 : void
3763 20 : dump_data_member_spec (pretty_printer *pp, tree r)
3764 : {
3765 : #if __GNUC__ >= 10
3766 20 : #pragma GCC diagnostic push
3767 20 : #pragma GCC diagnostic ignored "-Wformat"
3768 20 : #pragma GCC diagnostic ignored "-Wformat-diag"
3769 : #endif
3770 20 : pp_printf (pp, "(%T, %E, %E, %E, %s, {", TREE_VEC_ELT (r, 0),
3771 20 : TREE_VEC_ELT (r, 1), TREE_VEC_ELT (r, 2), TREE_VEC_ELT (r, 3),
3772 20 : TREE_VEC_ELT (r, 4) == boolean_true_node
3773 : ? "true" : "false");
3774 48 : for (int i = 5; i < TREE_VEC_LENGTH (r); ++i)
3775 12 : pp_printf (pp, "%s%E", i == 5 ? "" : ", ",
3776 8 : REFLECT_EXPR_HANDLE (TREE_VEC_ELT (r, i)));
3777 20 : pp_printf (pp, "})");
3778 : #if __GNUC__ >= 10
3779 20 : #pragma GCC diagnostic pop
3780 : #endif
3781 20 : }
3782 :
3783 : /* Process std::meta::{,u8}display_string_of.
3784 : Returns: An implementation-defined string_view or u8string_view,
3785 : respectively.
3786 : Recommended practice: Where possible, implementations should return a
3787 : string suitable for identifying the represented construct. */
3788 :
3789 : static tree
3790 454 : eval_display_string_of (location_t loc, const constexpr_ctx *ctx, tree r,
3791 : reflect_kind kind, bool *non_constant_p,
3792 : tree *jump_target, tree elt_type, tree ret_type,
3793 : tree fun)
3794 : {
3795 : #if __GNUC__ >= 10
3796 454 : #pragma GCC diagnostic push
3797 454 : #pragma GCC diagnostic ignored "-Wformat"
3798 454 : #pragma GCC diagnostic ignored "-Wformat-diag"
3799 : #endif
3800 454 : r = maybe_get_first_fn (r);
3801 454 : pretty_printer pp, *refpp = global_dc->get_reference_printer ();
3802 454 : pp_format_decoder (&pp) = pp_format_decoder (refpp);
3803 454 : pp.set_format_postprocessor (pp_format_postprocessor (refpp)->clone ());
3804 454 : if (r == unknown_type_node)
3805 4 : pp_printf (&pp, "<null reflection>");
3806 450 : else if (TYPE_P (r))
3807 70 : pp_printf (&pp, "%T", r);
3808 380 : else if (kind == REFLECT_PARM)
3809 : {
3810 72 : r = maybe_update_function_parm (r);
3811 72 : tree fn = DECL_CONTEXT (r);
3812 72 : if (DECL_NAME (r))
3813 56 : pp_printf (&pp, "<parameter %D of %D>", r, fn);
3814 : else
3815 : {
3816 16 : int idx = 1;
3817 16 : for (tree args = FUNCTION_FIRST_USER_PARM (fn);
3818 40 : r != args; args = DECL_CHAIN (args))
3819 24 : ++idx;
3820 16 : pp_printf (&pp, "<unnamed parameter %d of %D>", idx, fn);
3821 : }
3822 : }
3823 308 : else if (kind == REFLECT_VALUE || kind == REFLECT_OBJECT)
3824 24 : pp_printf (&pp, "%E", r);
3825 284 : else if (DECL_P (r) && (DECL_NAME (r) || TREE_CODE (r) == NAMESPACE_DECL))
3826 224 : pp_printf (&pp, "%D", r);
3827 60 : else if (TREE_CODE (r) == FIELD_DECL)
3828 : {
3829 16 : if (DECL_UNNAMED_BIT_FIELD (r))
3830 8 : pp_printf (&pp, "%T::<unnamed bit-field>", DECL_CONTEXT (r));
3831 8 : else if (ANON_UNION_TYPE_P (TREE_TYPE (r)))
3832 8 : pp_printf (&pp, "%T::<anonymous union>", DECL_CONTEXT (r));
3833 : else
3834 0 : pp_printf (&pp, "%T::<unnamed member>", DECL_CONTEXT (r));
3835 : }
3836 44 : else if (kind == REFLECT_BASE)
3837 : {
3838 8 : tree d = direct_base_derived (r);
3839 8 : pp_printf (&pp, "%T: %T", d, BINFO_TYPE (r));
3840 : }
3841 36 : else if (kind == REFLECT_DATA_MEMBER_SPEC)
3842 20 : dump_data_member_spec (&pp, r);
3843 16 : else if (eval_is_annotation (r, kind) == boolean_true_node)
3844 32 : pp_printf (&pp, "[[=%E]]",
3845 16 : tree_strip_any_location_wrapper (TREE_VALUE (TREE_VALUE (r))));
3846 : else
3847 0 : pp_string (&pp, "<unsupported reflection>");
3848 : #if __GNUC__ >= 10
3849 454 : #pragma GCC diagnostic pop
3850 : #endif
3851 454 : tree str = get_string_literal (pp_formatted_text (&pp), elt_type);
3852 454 : if (str == NULL_TREE)
3853 : {
3854 0 : if (elt_type == char_type_node)
3855 0 : return throw_exception (loc, ctx, "display_string_of not representable"
3856 : " in ordinary literal encoding",
3857 0 : fun, non_constant_p, jump_target);
3858 : else
3859 0 : return throw_exception (loc, ctx,
3860 : "u8display_string_of not representable"
3861 : " in UTF-8",
3862 0 : fun, non_constant_p, jump_target);
3863 : }
3864 454 : releasing_vec args (make_tree_vector_single (str));
3865 454 : tree ret = build_special_member_call (NULL_TREE, complete_ctor_identifier,
3866 : &args, ret_type, LOOKUP_NORMAL,
3867 : tf_warning_or_error);
3868 454 : return build_cplus_new (ret_type, ret, tf_warning_or_error);
3869 454 : }
3870 :
3871 : /* Determine the reflection kind for R. */
3872 :
3873 : static reflect_kind
3874 1988 : get_reflection_kind (tree r)
3875 : {
3876 1988 : if (eval_is_type (r) == boolean_true_node
3877 1896 : || eval_is_template (r) == boolean_true_node
3878 3834 : || eval_is_function (r) == boolean_true_node)
3879 : return REFLECT_UNDEF;
3880 1842 : return obvalue_p (r) ? REFLECT_OBJECT : REFLECT_VALUE;
3881 : }
3882 :
3883 : /* Get the reflection of template argument ARG as per
3884 : std::meta::template_arguments_of. */
3885 :
3886 : static tree
3887 324 : get_reflection_of_targ (tree arg)
3888 : {
3889 324 : const location_t loc = location_of (arg);
3890 : /* canonicalize_type_argument already strip_typedefs. */
3891 324 : arg = STRIP_REFERENCE_REF (arg);
3892 324 : arg = maybe_get_reference_referent (arg);
3893 324 : return get_reflection_raw (loc, arg, get_reflection_kind (arg));
3894 : }
3895 :
3896 : /* Process std::meta::template_arguments_of.
3897 : Returns: A vector containing reflections of the template arguments of the
3898 : template specialization represented by r, in the order in which they appear
3899 : in the corresponding template argument list.
3900 : For a given template argument A, its corresponding reflection R is
3901 : determined as follows:
3902 :
3903 : -- If A denotes a type or type alias, then R is a reflection representing
3904 : the underlying entity of A.
3905 : -- Otherwise, if A denotes a class template, variable template, concept,
3906 : or alias template, then R is a reflection representing A.
3907 : -- Otherwise, A is a constant template argument. Let P be the
3908 : corresponding template parameter.
3909 : -- If P has reference type, then R is a reflection representing the
3910 : object or function referred to by A.
3911 : -- Otherwise, if P has class type, then R represents the corresponding
3912 : template parameter object.
3913 : -- Otherwise, R is a reflection representing the value of A.
3914 :
3915 : Throws: meta::exception unless has_template_arguments(r) is true. */
3916 :
3917 : static tree
3918 160 : eval_template_arguments_of (location_t loc, const constexpr_ctx *ctx, tree r,
3919 : bool *non_constant_p, tree *jump_target, tree fun)
3920 : {
3921 160 : if (eval_has_template_arguments (r) != boolean_true_node)
3922 0 : return throw_exception_notargs (loc, ctx, fun, non_constant_p, jump_target);
3923 :
3924 160 : vec<constructor_elt, va_gc> *elts = nullptr;
3925 160 : tree args = NULL_TREE;
3926 160 : if (TYPE_P (r) && typedef_variant_p (r))
3927 : {
3928 16 : if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (r))
3929 16 : args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
3930 : }
3931 : else
3932 144 : args = get_template_innermost_arguments (r);
3933 160 : gcc_assert (args);
3934 476 : for (tree arg : tree_vec_range (args))
3935 : {
3936 316 : if (ARGUMENT_PACK_P (arg))
3937 : {
3938 18 : tree pargs = ARGUMENT_PACK_ARGS (arg);
3939 44 : for (tree a : tree_vec_range (pargs))
3940 26 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
3941 : get_reflection_of_targ (a));
3942 : }
3943 : else
3944 614 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, get_reflection_of_targ (arg));
3945 : }
3946 160 : return get_vector_of_info_elts (elts);
3947 : }
3948 :
3949 : /* Helper for eval_remove_const to build non-const type. */
3950 :
3951 : static tree
3952 200 : remove_const (tree type)
3953 : {
3954 200 : return cp_build_qualified_type (type,
3955 200 : cp_type_quals (type) & ~TYPE_QUAL_CONST);
3956 : }
3957 :
3958 : /* Process std::meta::annotations_of and annotations_of_with_type.
3959 : For a function F, let S(F) be the set of declarations, ignoring any explicit
3960 : instantiations, that declare either F or a templated function of which F is
3961 : a specialization.
3962 : Returns: A vector containing all of the reflections R representing each
3963 : annotation applying to:
3964 : -- if item represents a function parameter P of a function F, then the
3965 : declaration of P in each declaration of F in S(F),
3966 : -- otherwise, if item represents a function F, then each declaration of F
3967 : in S(F),
3968 : -- otherwise, if item represents a direct base class relationship (D,B),
3969 : then the corresponding base-specifier in the definition of D,
3970 : -- otherwise, each declaration of the entity represented by item,
3971 : such that precedes either some point in the evaluation context or a point
3972 : immediately following the class-specifier of the outermost class for which
3973 : such a point is in a complete-class context.
3974 : For any two reflections R1 and R2 in the returned vector, if the annotation
3975 : represented by R1 precedes the annotation represented by R2, then R1
3976 : appears before R2.
3977 : If R1 and R2 represent annotations from the same translation unit T, any
3978 : element in the returned vector between R1 and R2 represents an annotation
3979 : from T.
3980 :
3981 : Throws: meta::exception unless item represents a type, type alias,
3982 : variable, function, function parameter, namespace, enumerator, direct base
3983 : class relationship, or non-static data member. */
3984 :
3985 : static tree
3986 644 : eval_annotations_of (location_t loc, const constexpr_ctx *ctx, tree r,
3987 : reflect_kind kind, tree type, bool *non_constant_p,
3988 : tree *jump_target, tree fun)
3989 : {
3990 670 : if (!(eval_is_type (r) == boolean_true_node
3991 568 : || eval_is_type_alias (r) == boolean_true_node
3992 568 : || eval_is_variable (r, kind) == boolean_true_node
3993 332 : || eval_is_function (r) == boolean_true_node
3994 172 : || eval_is_function_parameter (r, kind) == boolean_true_node
3995 118 : || eval_is_namespace (r) == boolean_true_node
3996 86 : || eval_is_enumerator (r) == boolean_true_node
3997 82 : || eval_is_base (r, kind) == boolean_true_node
3998 26 : || eval_is_nonstatic_data_member (r) == boolean_true_node))
3999 0 : return throw_exception (loc, ctx,
4000 : "reflection does not represent a type,"
4001 : " type alias, variable, function, function"
4002 : " parameter, namespace, enumerator,"
4003 : " direct base class relationship,"
4004 : " or non-static data member",
4005 0 : fun, non_constant_p, jump_target);
4006 :
4007 644 : if (type)
4008 : {
4009 40 : type = maybe_strip_typedefs (type);
4010 40 : if (!TYPE_P (type)
4011 40 : || !complete_type_or_maybe_complain (type, NULL_TREE, tf_none))
4012 0 : return throw_exception (loc, ctx,
4013 : "reflection does not represent a complete"
4014 : " type or type alias", fun, non_constant_p,
4015 0 : jump_target);
4016 40 : type = remove_const (type);
4017 : }
4018 :
4019 644 : r = maybe_get_first_fn (r);
4020 644 : bool var_of = false;
4021 644 : if (kind == REFLECT_BASE)
4022 : {
4023 56 : gcc_assert (TREE_CODE (r) == TREE_BINFO);
4024 56 : tree c = direct_base_derived_binfo (r), binfo = r, base_binfo;
4025 :
4026 56 : r = NULL_TREE;
4027 200 : for (unsigned ix = 0; BINFO_BASE_ITERATE (c, ix, base_binfo); ix++)
4028 144 : if (base_binfo == binfo)
4029 : {
4030 112 : if (ix + BINFO_BASE_BINFOS (c)->length ()
4031 56 : < vec_safe_length (BINFO_BASE_ACCESSES (c)))
4032 52 : r = BINFO_BASE_ACCESS (c, ix + BINFO_BASE_BINFOS (c)->length ());
4033 : break;
4034 : }
4035 : }
4036 588 : else if (TYPE_P (r))
4037 : {
4038 76 : complete_type (r);
4039 76 : if (typedef_variant_p (r))
4040 8 : r = DECL_ATTRIBUTES (TYPE_NAME (r));
4041 : else
4042 68 : r = TYPE_ATTRIBUTES (r);
4043 : }
4044 512 : else if (DECL_P (r))
4045 : {
4046 512 : if (TREE_CODE (r) == PARM_DECL && kind != REFLECT_PARM)
4047 512 : var_of = true;
4048 512 : r = DECL_ATTRIBUTES (r);
4049 : }
4050 : else
4051 0 : gcc_unreachable ();
4052 644 : vec<constructor_elt, va_gc> *elts = nullptr;
4053 644 : for (tree a = r;
4054 3048 : (a = lookup_annotation (a));
4055 2404 : a = TREE_CHAIN (a))
4056 : {
4057 2404 : gcc_checking_assert (TREE_CODE (TREE_VALUE (a)) == TREE_LIST);
4058 2404 : tree val = TREE_VALUE (TREE_VALUE (a));
4059 2404 : tree purpose = TREE_PURPOSE (TREE_VALUE (a));
4060 2464 : if (var_of
4061 2404 : && (purpose == NULL_TREE
4062 40 : || (TREE_CODE (purpose) == INTEGER_CST
4063 0 : && !TYPE_UNSIGNED (TREE_TYPE (purpose)))))
4064 : /* For ^^fnparm or variable_of (parameters_of (^^fn)[N])
4065 : filter out annotations not specified on the function
4066 : definition. TREE_PURPOSE is set in grokfndecl and/or in
4067 : reflection_mangle_prefix. */
4068 60 : continue;
4069 2344 : if (type)
4070 : {
4071 182 : tree at = TREE_TYPE (val);
4072 182 : if (at != type && !same_type_p (remove_const (at), type))
4073 138 : continue;
4074 : }
4075 4610 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
4076 : get_reflection_raw (loc, a, REFLECT_ANNOTATION));
4077 : }
4078 644 : if (elts)
4079 : {
4080 : /* Reverse the order. */
4081 606 : unsigned l = elts->length ();
4082 606 : constructor_elt *ptr = elts->address ();
4083 :
4084 1560 : for (unsigned i = 0; i < l / 2; i++)
4085 954 : std::swap (ptr[i], ptr[l - i - 1]);
4086 : }
4087 644 : return get_vector_of_info_elts (elts);
4088 : }
4089 :
4090 : /* Process std::meta::reflect_constant.
4091 : Mandates: is_copy_constructible_v<T> is true and T is a cv-unqualified
4092 : structural type that is not a reference type.
4093 : Let V be:
4094 : -- if T is a class type, then an object that is template-argument-equivalent
4095 : to the value of expr;
4096 : -- otherwise, the value of expr.
4097 : Returns: template_arguments_of(^^TCls<V>)[0], with TCls as defined below.
4098 : Throws: meta::exception unless the template-id TCls<V> would be valid given
4099 : the invented template
4100 : template<T P> struct TCls; */
4101 :
4102 : static tree
4103 1726 : eval_reflect_constant (location_t loc, const constexpr_ctx *ctx, tree type,
4104 : tree expr, bool *non_constant_p, tree *jump_target,
4105 : tree fun)
4106 : {
4107 1726 : if (!structural_type_p (type)
4108 1702 : || CP_TYPE_VOLATILE_P (type)
4109 1702 : || CP_TYPE_CONST_P (type)
4110 3428 : || TYPE_REF_P (type))
4111 : {
4112 24 : if (!cxx_constexpr_quiet_p (ctx))
4113 8 : error_at (loc, "%qT must be a cv-unqualified structural type that is "
4114 : "not a reference type", type);
4115 24 : *non_constant_p = true;
4116 24 : return error_mark_node;
4117 : }
4118 1702 : expr = convert_reflect_constant_arg (type, convert_from_reference (expr));
4119 1702 : if (expr == error_mark_node)
4120 38 : return throw_exception (loc, ctx, "reflect_constant failed", fun,
4121 38 : non_constant_p, jump_target);
4122 1664 : return get_reflection_raw (loc, expr, get_reflection_kind (expr));
4123 : }
4124 :
4125 : /* Process std::meta::reflect_object.
4126 : Mandates: T is an object type.
4127 : Returns: A reflection of the object designated by expr.
4128 : Throws: meta::exception unless expr is suitable for use as a constant
4129 : template argument for a constant template parameter of type T&. */
4130 :
4131 : static tree
4132 256 : eval_reflect_object (location_t loc, const constexpr_ctx *ctx, tree type,
4133 : tree expr, bool *non_constant_p, tree *jump_target,
4134 : tree fun)
4135 : {
4136 512 : if (eval_is_object_type (loc, ctx, type, non_constant_p, fun)
4137 256 : != boolean_true_node)
4138 : {
4139 0 : if (!cxx_constexpr_quiet_p (ctx))
4140 0 : error_at (loc, "%qT must be an object type", type);
4141 0 : *non_constant_p = true;
4142 0 : return error_mark_node;
4143 : }
4144 256 : type = cp_build_reference_type (type, /*rval=*/false);
4145 256 : tree e = convert_reflect_constant_arg (type, convert_from_reference (expr));
4146 256 : if (e == error_mark_node)
4147 8 : return throw_exception (loc, ctx, "reflect_object failed", fun,
4148 8 : non_constant_p, jump_target);
4149 : /* We got (const T &) &foo. Get the referent, since we want the object
4150 : designated by EXPR. */
4151 248 : expr = maybe_get_reference_referent (expr);
4152 248 : return get_reflection_raw (loc, expr, REFLECT_OBJECT);
4153 : }
4154 :
4155 : /* Process std::meta::reflect_function.
4156 : Mandates: T is a function type.
4157 : Returns: A reflection of the function designated by fn.
4158 : Throws: meta::exception unless fn is suitable for use as a constant
4159 : template argument for a constant template parameter of type T&. */
4160 :
4161 : static tree
4162 62 : eval_reflect_function (location_t loc, const constexpr_ctx *ctx, tree type,
4163 : tree expr, bool *non_constant_p, tree *jump_target,
4164 : tree fun)
4165 : {
4166 62 : if (eval_is_function_type (type) != boolean_true_node)
4167 : {
4168 0 : if (!cxx_constexpr_quiet_p (ctx))
4169 0 : error_at (loc, "%qT must be a function type", type);
4170 0 : *non_constant_p = true;
4171 0 : return error_mark_node;
4172 : }
4173 62 : type = cp_build_reference_type (type, /*rval=*/false);
4174 62 : tree e = convert_reflect_constant_arg (type, convert_from_reference (expr));
4175 62 : if (e == error_mark_node)
4176 4 : return throw_exception (loc, ctx, "reflect_function failed", fun,
4177 4 : non_constant_p, jump_target);
4178 : /* We got (void (&<Ta885>) (void)) fn. Get the function. */
4179 58 : expr = maybe_get_reference_referent (expr);
4180 58 : return get_reflection_raw (loc, expr);
4181 : }
4182 :
4183 : /* Reflection type traits [meta.reflection.traits].
4184 :
4185 : Every function and function template declared in this subclause throws
4186 : an exception of type meta::exception unless the following conditions are
4187 : met:
4188 : -- For every parameter p of type info, is_type(p) is true.
4189 : -- For every parameter r whose type is constrained on reflection_range,
4190 : ranges::all_of(r, is_type) is true. */
4191 :
4192 : /* Evaluate reflection type traits for which we have corresponding built-in
4193 : traits. KIND says which trait we are interested in; TYPE1 and TYPE2 are
4194 : arguments to the trait. */
4195 :
4196 : static tree
4197 10180 : eval_type_trait (location_t loc, const constexpr_ctx *ctx, tree type1,
4198 : tree type2, cp_trait_kind kind, bool *non_constant_p,
4199 : tree fun)
4200 : {
4201 10180 : tree r = finish_trait_expr (loc, kind, type1, type2, tf_none);
4202 10180 : if (r == error_mark_node)
4203 : {
4204 : /* [meta.reflection.traits]/3.2: Otherwise, if the instantiation of S
4205 : would result in undefined behavior due to dependence on an incomplete
4206 : type, then the call is not a constant subexpression. */
4207 768 : if (!cxx_constexpr_quiet_p (ctx))
4208 : {
4209 256 : auto_diagnostic_group d;
4210 256 : error_at (loc, "type trait %qE preconditions not satisfied", fun);
4211 : /* See if we can figure out which argument was incomplete. */
4212 256 : tree inc = NULL_TREE;
4213 450 : if (!COMPLETE_OR_VOID_TYPE_P (type1)
4214 446 : && !array_of_unknown_bound_p (type1))
4215 : inc = type1;
4216 70 : else if (type2)
4217 : {
4218 70 : if (TYPE_P (type2)
4219 36 : && !COMPLETE_OR_VOID_TYPE_P (type2)
4220 106 : && !array_of_unknown_bound_p (type2))
4221 : inc = type2;
4222 34 : else if (TREE_CODE (type2) == TREE_VEC)
4223 34 : for (tree t : tree_vec_range (type2))
4224 68 : if (!COMPLETE_OR_VOID_TYPE_P (t)
4225 68 : && !array_of_unknown_bound_p (t))
4226 : {
4227 : inc = t;
4228 : break;
4229 : }
4230 : }
4231 256 : if (inc)
4232 256 : cxx_incomplete_type_diagnostic (loc, NULL_TREE, inc,
4233 : diagnostics::kind::note);
4234 256 : }
4235 768 : *non_constant_p = true;
4236 768 : return NULL_TREE;
4237 : }
4238 9412 : STRIP_ANY_LOCATION_WRAPPER (r);
4239 9412 : return r;
4240 : }
4241 :
4242 : /* Like above, but for type traits that take only one type. */
4243 :
4244 : static tree
4245 5060 : eval_type_trait (location_t loc, const constexpr_ctx *ctx, tree type,
4246 : cp_trait_kind kind, bool *non_constant_p, tree fun)
4247 : {
4248 0 : return eval_type_trait (loc, ctx, type, NULL_TREE, kind, non_constant_p,
4249 0 : fun);
4250 : }
4251 :
4252 : /* Process std::meta::is_function_type. */
4253 :
4254 : static tree
4255 1196 : eval_is_function_type (tree type)
4256 : {
4257 1116 : if (FUNC_OR_METHOD_TYPE_P (type))
4258 92 : return boolean_true_node;
4259 : else
4260 940 : return boolean_false_node;
4261 : }
4262 :
4263 : /* Process std::meta::is_void_type. */
4264 :
4265 : static tree
4266 58 : eval_is_void_type (tree type)
4267 : {
4268 0 : if (VOID_TYPE_P (type))
4269 6 : return boolean_true_node;
4270 : else
4271 52 : return boolean_false_node;
4272 : }
4273 :
4274 : /* Process std::meta::is_null_pointer_type. */
4275 :
4276 : static tree
4277 52 : eval_is_null_pointer_type (tree type)
4278 : {
4279 0 : if (NULLPTR_TYPE_P (type))
4280 2 : return boolean_true_node;
4281 : else
4282 50 : return boolean_false_node;
4283 : }
4284 :
4285 : /* Process std::meta::is_integral_type. */
4286 :
4287 : static tree
4288 308 : eval_is_integral_type (tree type)
4289 : {
4290 0 : if (CP_INTEGRAL_TYPE_P (type))
4291 144 : return boolean_true_node;
4292 : else
4293 164 : return boolean_false_node;
4294 : }
4295 :
4296 : /* Process std::meta::is_floating_point_type. */
4297 :
4298 : static tree
4299 56 : eval_is_floating_point_type (tree type)
4300 : {
4301 56 : if (FLOAT_TYPE_P (type))
4302 6 : return boolean_true_node;
4303 : else
4304 50 : return boolean_false_node;
4305 : }
4306 :
4307 : /* Process std::meta::is_array_type. */
4308 :
4309 : static tree
4310 1380 : eval_is_array_type (location_t loc, const constexpr_ctx *ctx, tree type,
4311 : bool *non_constant_p, tree fun)
4312 : {
4313 1016 : return eval_type_trait (loc, ctx, type, CPTK_IS_ARRAY, non_constant_p,
4314 0 : fun);
4315 : }
4316 :
4317 : /* Process std::meta::is_pointer_type. */
4318 :
4319 : static tree
4320 56 : eval_is_pointer_type (location_t loc, const constexpr_ctx *ctx, tree type,
4321 : bool *non_constant_p, tree fun)
4322 : {
4323 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_POINTER, non_constant_p,
4324 0 : fun);
4325 : }
4326 :
4327 : /* Process std::meta::is_lvalue_reference_type. */
4328 :
4329 : static tree
4330 52 : eval_is_lvalue_reference_type (tree type)
4331 : {
4332 4 : if (TYPE_REF_P (type) && !TYPE_REF_IS_RVALUE (type))
4333 2 : return boolean_true_node;
4334 : else
4335 50 : return boolean_false_node;
4336 : }
4337 :
4338 : /* Process std::meta::is_rvalue_reference_type. */
4339 :
4340 : static tree
4341 52 : eval_is_rvalue_reference_type (tree type)
4342 : {
4343 4 : if (TYPE_REF_P (type) && TYPE_REF_IS_RVALUE (type))
4344 2 : return boolean_true_node;
4345 : else
4346 50 : return boolean_false_node;
4347 : }
4348 :
4349 : /* Process std::meta::is_member_object_pointer_type. */
4350 :
4351 : static tree
4352 52 : eval_is_member_object_pointer_type (location_t loc, const constexpr_ctx *ctx,
4353 : tree type, bool *non_constant_p, tree fun)
4354 : {
4355 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_MEMBER_OBJECT_POINTER,
4356 0 : non_constant_p, fun);
4357 : }
4358 :
4359 : /* Process std::meta::is_member_function_pointer_type. */
4360 :
4361 : static tree
4362 52 : eval_is_member_function_pointer_type (location_t loc, const constexpr_ctx *ctx,
4363 : tree type, bool *non_constant_p, tree fun)
4364 : {
4365 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_MEMBER_FUNCTION_POINTER,
4366 0 : non_constant_p, fun);
4367 : }
4368 :
4369 : /* Process std::meta::is_enum_type. */
4370 :
4371 : static tree
4372 52 : eval_is_enum_type (location_t loc, const constexpr_ctx *ctx, tree type,
4373 : bool *non_constant_p, tree fun)
4374 : {
4375 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_ENUM, non_constant_p,
4376 0 : fun);
4377 : }
4378 :
4379 : /* Process std::meta::is_union_type. */
4380 :
4381 : static tree
4382 84 : eval_is_union_type (location_t loc, const constexpr_ctx *ctx, tree type,
4383 : bool *non_constant_p, tree fun)
4384 : {
4385 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_UNION, non_constant_p,
4386 0 : fun);
4387 : }
4388 :
4389 : /* Process std::meta::is_class_type. */
4390 :
4391 : static tree
4392 220 : eval_is_class_type (location_t loc, const constexpr_ctx *ctx, tree type,
4393 : bool *non_constant_p, tree fun)
4394 : {
4395 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_CLASS, non_constant_p,
4396 0 : fun);
4397 : }
4398 :
4399 : /* Process std::meta::is_reflection_type. */
4400 :
4401 : static tree
4402 52 : eval_is_reflection_type (tree type)
4403 : {
4404 52 : if (REFLECTION_TYPE_P (type))
4405 2 : return boolean_true_node;
4406 : else
4407 50 : return boolean_false_node;
4408 : }
4409 :
4410 : /* Process std::meta::is_reference_type. */
4411 :
4412 : static tree
4413 1210 : eval_is_reference_type (location_t loc, const constexpr_ctx *ctx, tree type,
4414 : bool *non_constant_p, tree fun)
4415 : {
4416 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_REFERENCE, non_constant_p,
4417 0 : fun);
4418 : }
4419 :
4420 : /* Process std::meta::is_arithmetic_type. */
4421 :
4422 : static tree
4423 56 : eval_is_arithmetic_type (tree type)
4424 : {
4425 56 : if (ARITHMETIC_TYPE_P (type))
4426 12 : return boolean_true_node;
4427 : else
4428 44 : return boolean_false_node;
4429 : }
4430 :
4431 : /* Process std::meta::is_object_type. */
4432 :
4433 : static tree
4434 448 : eval_is_object_type (location_t loc, const constexpr_ctx *ctx, tree type,
4435 : bool *non_constant_p, tree fun)
4436 : {
4437 392 : return eval_type_trait (loc, ctx, type, CPTK_IS_OBJECT, non_constant_p,
4438 0 : fun);
4439 : }
4440 :
4441 : /* Process std::meta::is_scalar_type. */
4442 :
4443 : static tree
4444 62 : eval_is_scalar_type (tree type)
4445 : {
4446 62 : if (SCALAR_TYPE_P (type))
4447 30 : return boolean_true_node;
4448 : else
4449 32 : return boolean_false_node;
4450 : }
4451 :
4452 : /* Process std::meta::is_fundamental_type. */
4453 :
4454 : static tree
4455 112 : eval_is_fundamental_type (tree type)
4456 : {
4457 112 : if (ARITHMETIC_TYPE_P (type)
4458 : || VOID_TYPE_P (type)
4459 : || NULLPTR_TYPE_P (type)
4460 80 : || REFLECTION_TYPE_P (type))
4461 36 : return boolean_true_node;
4462 : else
4463 76 : return boolean_false_node;
4464 : }
4465 :
4466 : /* Process std::meta::is_compound_type. */
4467 :
4468 : static tree
4469 56 : eval_is_compound_type (tree type)
4470 : {
4471 56 : if (eval_is_fundamental_type (type) == boolean_false_node)
4472 38 : return boolean_true_node;
4473 : else
4474 : return boolean_false_node;
4475 : }
4476 :
4477 : /* Process std::meta::is_member_pointer_type. */
4478 :
4479 : static tree
4480 56 : eval_is_member_pointer_type (location_t loc, const constexpr_ctx *ctx,
4481 : tree type, bool *non_constant_p, tree fun)
4482 : {
4483 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_MEMBER_POINTER,
4484 0 : non_constant_p, fun);
4485 : }
4486 :
4487 : /* Process std::meta::is_const_type. */
4488 :
4489 : static tree
4490 16 : eval_is_const_type (tree type)
4491 : {
4492 16 : if (CP_TYPE_CONST_P (type))
4493 8 : return boolean_true_node;
4494 : else
4495 8 : return boolean_false_node;
4496 : }
4497 :
4498 : /* Process std::meta::is_volatile_type. */
4499 :
4500 : static tree
4501 16 : eval_is_volatile_type (tree type)
4502 : {
4503 16 : if (CP_TYPE_VOLATILE_P (type))
4504 8 : return boolean_true_node;
4505 : else
4506 8 : return boolean_false_node;
4507 : }
4508 :
4509 : /* Process std::meta::is_trivially_copyable_type. */
4510 :
4511 : static tree
4512 112 : eval_is_trivially_copyable_type (location_t loc, const constexpr_ctx *ctx,
4513 : tree type, bool *non_constant_p, tree fun)
4514 : {
4515 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_TRIVIALLY_COPYABLE,
4516 0 : non_constant_p, fun);
4517 : }
4518 :
4519 : /* Process std::meta::is_standard_layout_type. */
4520 :
4521 : static tree
4522 38 : eval_is_standard_layout_type (location_t loc, const constexpr_ctx *ctx,
4523 : tree type, bool *non_constant_p, tree fun)
4524 : {
4525 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_STD_LAYOUT, non_constant_p,
4526 0 : fun);
4527 : }
4528 :
4529 : /* Process std::meta::is_empty_type. */
4530 :
4531 : static tree
4532 44 : eval_is_empty_type (location_t loc, const constexpr_ctx *ctx, tree type,
4533 : bool *non_constant_p, tree fun)
4534 : {
4535 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_EMPTY, non_constant_p,
4536 0 : fun);
4537 : }
4538 :
4539 : /* Process std::meta::is_polymorphic_type. */
4540 :
4541 : static tree
4542 34 : eval_is_polymorphic_type (location_t loc, const constexpr_ctx *ctx,
4543 : tree type, bool *non_constant_p, tree fun)
4544 : {
4545 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_POLYMORPHIC, non_constant_p,
4546 0 : fun);
4547 : }
4548 :
4549 : /* Process std::meta::is_abstract_type. */
4550 :
4551 : static tree
4552 24 : eval_is_abstract_type (location_t loc, const constexpr_ctx *ctx,
4553 : tree type, bool *non_constant_p, tree fun)
4554 : {
4555 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_ABSTRACT, non_constant_p,
4556 0 : fun);
4557 : }
4558 :
4559 : /* Process std::meta::is_final_type. */
4560 :
4561 : static tree
4562 24 : eval_is_final_type (location_t loc, const constexpr_ctx *ctx, tree type,
4563 : bool *non_constant_p, tree fun)
4564 : {
4565 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_FINAL, non_constant_p, fun);
4566 : }
4567 :
4568 : /* Process std::meta::is_final.
4569 : Returns: true if r represents a final class or a final member function.
4570 : Otherwise, false. */
4571 :
4572 : static tree
4573 40 : eval_is_final (tree r)
4574 : {
4575 40 : if (eval_is_function (r) == boolean_true_node)
4576 : {
4577 18 : r = maybe_get_first_fn (r);
4578 18 : if (TREE_CODE (r) == FUNCTION_DECL && DECL_FINAL_P (r))
4579 : return boolean_true_node;
4580 : else
4581 8 : return boolean_false_node;
4582 : }
4583 :
4584 22 : if (eval_is_type (r) == boolean_true_node
4585 14 : && CLASS_TYPE_P (r)
4586 36 : && CLASSTYPE_FINAL (r))
4587 : return boolean_true_node;
4588 :
4589 16 : return boolean_false_node;
4590 : }
4591 :
4592 : /* Process std::meta::is_aggregate_type. */
4593 :
4594 : static tree
4595 70 : eval_is_aggregate_type (location_t loc, const constexpr_ctx *ctx,
4596 : tree type, bool *non_constant_p, tree fun)
4597 : {
4598 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_AGGREGATE,
4599 0 : non_constant_p, fun);
4600 : }
4601 :
4602 : /* Process std::meta::is_structural_type. */
4603 :
4604 : static tree
4605 86 : eval_is_structural_type (location_t loc, const constexpr_ctx *ctx,
4606 : tree type, bool *non_constant_p, tree fun)
4607 : {
4608 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_STRUCTURAL,
4609 0 : non_constant_p, fun);
4610 : }
4611 :
4612 : /* Process std::meta::is_signed_type. */
4613 :
4614 : static tree
4615 34 : eval_is_signed_type (tree type)
4616 : {
4617 34 : if (ARITHMETIC_TYPE_P (type) && !TYPE_UNSIGNED (type))
4618 20 : return boolean_true_node;
4619 : else
4620 14 : return boolean_false_node;
4621 : }
4622 :
4623 : /* Process std::meta::is_unsigned_type. */
4624 :
4625 : static tree
4626 34 : eval_is_unsigned_type (tree type)
4627 : {
4628 34 : if (ARITHMETIC_TYPE_P (type) && TYPE_UNSIGNED (type))
4629 10 : return boolean_true_node;
4630 : else
4631 24 : return boolean_false_node;
4632 : }
4633 :
4634 : /* Process std::meta::is_bounded_array_type. */
4635 :
4636 : static tree
4637 252 : eval_is_bounded_array_type (location_t loc, const constexpr_ctx *ctx,
4638 : tree type, bool *non_constant_p, tree fun)
4639 : {
4640 216 : return eval_type_trait (loc, ctx, type, CPTK_IS_BOUNDED_ARRAY,
4641 0 : non_constant_p, fun);
4642 : }
4643 :
4644 : /* Process std::meta::is_unbounded_array_type. */
4645 :
4646 : static tree
4647 42 : eval_is_unbounded_array_type (tree type)
4648 : {
4649 42 : if (array_of_unknown_bound_p (type))
4650 14 : return boolean_true_node;
4651 : else
4652 28 : return boolean_false_node;
4653 : }
4654 :
4655 : /* Process std::meta::is_scoped_enum_type. */
4656 :
4657 : static tree
4658 36 : eval_is_scoped_enum_type (tree type)
4659 : {
4660 8 : if (SCOPED_ENUM_P (type))
4661 4 : return boolean_true_node;
4662 : else
4663 32 : return boolean_false_node;
4664 : }
4665 :
4666 : /* Process std::meta::is_constructible_type. */
4667 :
4668 : static tree
4669 1302 : eval_is_constructible_type (location_t loc, const constexpr_ctx *ctx,
4670 : tree type, tree tvec, bool *non_constant_p,
4671 : tree fun)
4672 : {
4673 0 : return eval_type_trait (loc, ctx, type, tvec, CPTK_IS_CONSTRUCTIBLE,
4674 0 : non_constant_p, fun);
4675 : }
4676 :
4677 : /* Process std::meta::is_default_constructible_type. */
4678 :
4679 : static tree
4680 288 : eval_is_default_constructible_type (location_t loc, const constexpr_ctx *ctx,
4681 : tree type, bool *non_constant_p, tree fun)
4682 : {
4683 288 : return eval_type_trait (loc, ctx, type, make_tree_vec (0),
4684 288 : CPTK_IS_CONSTRUCTIBLE, non_constant_p, fun);
4685 : }
4686 :
4687 : /* Process std::meta::is_copy_constructible_type. */
4688 :
4689 : static tree
4690 100 : eval_is_copy_constructible_type (location_t loc, const constexpr_ctx *ctx,
4691 : tree type, bool *non_constant_p, tree fun)
4692 : {
4693 100 : tree arg = make_tree_vec (1);
4694 100 : TREE_VEC_ELT (arg, 0) = build_const_lref (type);
4695 100 : return eval_type_trait (loc, ctx, type, arg, CPTK_IS_CONSTRUCTIBLE,
4696 100 : non_constant_p, fun);
4697 : }
4698 :
4699 : /* Process std::meta::is_move_constructible_type. */
4700 :
4701 : static tree
4702 80 : eval_is_move_constructible_type (location_t loc, const constexpr_ctx *ctx,
4703 : tree type, bool *non_constant_p, tree fun)
4704 : {
4705 80 : tree arg = make_tree_vec (1);
4706 80 : TREE_VEC_ELT (arg, 0) = cp_build_reference_type (type, /*rval=*/true);
4707 80 : return eval_type_trait (loc, ctx, type, arg, CPTK_IS_CONSTRUCTIBLE,
4708 80 : non_constant_p, fun);
4709 : }
4710 :
4711 : /* Process std::meta::is_assignable_type. */
4712 :
4713 : static tree
4714 1030 : eval_is_assignable_type (location_t loc, const constexpr_ctx *ctx,
4715 : tree type1, tree type2, bool *non_constant_p,
4716 : tree fun)
4717 : {
4718 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_ASSIGNABLE,
4719 0 : non_constant_p, fun);
4720 : }
4721 :
4722 : /* Process std::meta::is_copy_assignable_type. */
4723 :
4724 : static tree
4725 80 : eval_is_copy_assignable_type (location_t loc, const constexpr_ctx *ctx,
4726 : tree type, bool *non_constant_p, tree fun)
4727 : {
4728 80 : tree type1 = cp_build_reference_type (type, /*rval=*/false);
4729 80 : tree type2 = build_const_lref (type);
4730 80 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_ASSIGNABLE,
4731 80 : non_constant_p, fun);
4732 : }
4733 :
4734 : /* Process std::meta::is_move_assignable_type. */
4735 :
4736 : static tree
4737 60 : eval_is_move_assignable_type (location_t loc, const constexpr_ctx *ctx,
4738 : tree type, bool *non_constant_p, tree fun)
4739 : {
4740 60 : tree type1 = cp_build_reference_type (type, /*rval=*/false);
4741 60 : tree type2 = cp_build_reference_type (type, /*rval=*/true);
4742 60 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_ASSIGNABLE,
4743 60 : non_constant_p, fun);
4744 : }
4745 :
4746 : /* Process std::meta::is_destructible_type. */
4747 :
4748 : static tree
4749 206 : eval_is_destructible_type (location_t loc, const constexpr_ctx *ctx,
4750 : tree type, bool *non_constant_p, tree fun)
4751 : {
4752 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_DESTRUCTIBLE,
4753 0 : non_constant_p, fun);
4754 : }
4755 :
4756 : /* Process std::meta::is_trivially_constructible_type. */
4757 :
4758 : static tree
4759 166 : eval_is_trivially_constructible_type (location_t loc, const constexpr_ctx *ctx,
4760 : tree type, tree tvec,
4761 : bool *non_constant_p, tree fun)
4762 : {
4763 0 : return eval_type_trait (loc, ctx, type, tvec,
4764 : CPTK_IS_TRIVIALLY_CONSTRUCTIBLE,
4765 0 : non_constant_p, fun);
4766 : }
4767 :
4768 : /* Process std::meta::is_trivially_default_constructible_type. */
4769 :
4770 : static tree
4771 98 : eval_is_trivially_default_constructible_type (location_t loc,
4772 : const constexpr_ctx *ctx,
4773 : tree type, bool *non_constant_p,
4774 : tree fun)
4775 : {
4776 98 : return eval_type_trait (loc, ctx, type, make_tree_vec (0),
4777 : CPTK_IS_TRIVIALLY_CONSTRUCTIBLE,
4778 98 : non_constant_p, fun);
4779 : }
4780 :
4781 : /* Process std::meta::is_trivially_copy_constructible_type. */
4782 :
4783 : static tree
4784 78 : eval_is_trivially_copy_constructible_type (location_t loc,
4785 : const constexpr_ctx *ctx,
4786 : tree type, bool *non_constant_p,
4787 : tree fun)
4788 : {
4789 78 : tree arg = make_tree_vec (1);
4790 78 : TREE_VEC_ELT (arg, 0) = build_const_lref (type);
4791 78 : return eval_type_trait (loc, ctx, type, arg,
4792 : CPTK_IS_TRIVIALLY_CONSTRUCTIBLE,
4793 78 : non_constant_p, fun);
4794 : }
4795 :
4796 : /* Process std::meta::is_trivially_move_constructible_type. */
4797 :
4798 : static tree
4799 62 : eval_is_trivially_move_constructible_type (location_t loc,
4800 : const constexpr_ctx *ctx,
4801 : tree type, bool *non_constant_p,
4802 : tree fun)
4803 : {
4804 62 : tree arg = make_tree_vec (1);
4805 62 : TREE_VEC_ELT (arg, 0) = cp_build_reference_type (type, /*rval=*/true);
4806 62 : return eval_type_trait (loc, ctx, type, arg,
4807 : CPTK_IS_TRIVIALLY_CONSTRUCTIBLE,
4808 62 : non_constant_p, fun);
4809 : }
4810 :
4811 : /* Process std::meta::is_trivially_assignable_type. */
4812 :
4813 : static tree
4814 128 : eval_is_trivially_assignable_type (location_t loc, const constexpr_ctx *ctx,
4815 : tree type1, tree type2,
4816 : bool *non_constant_p, tree fun)
4817 : {
4818 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_TRIVIALLY_ASSIGNABLE,
4819 0 : non_constant_p, fun);
4820 : }
4821 :
4822 : /* Process std::meta::is_trivially_copy_assignable_type. */
4823 :
4824 : static tree
4825 72 : eval_is_trivially_copy_assignable_type (location_t loc,
4826 : const constexpr_ctx *ctx,
4827 : tree type, bool *non_constant_p,
4828 : tree fun)
4829 : {
4830 72 : tree type1 = cp_build_reference_type (type, /*rval=*/false);
4831 72 : tree type2 = build_const_lref (type);
4832 72 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_TRIVIALLY_ASSIGNABLE,
4833 72 : non_constant_p, fun);
4834 : }
4835 :
4836 : /* Process std::meta::is_trivially_move_assignable_type. */
4837 :
4838 : static tree
4839 56 : eval_is_trivially_move_assignable_type (location_t loc,
4840 : const constexpr_ctx *ctx,
4841 : tree type, bool *non_constant_p,
4842 : tree fun)
4843 : {
4844 56 : tree type1 = cp_build_reference_type (type, /*rval=*/false);
4845 56 : tree type2 = cp_build_reference_type (type, /*rval=*/true);
4846 56 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_TRIVIALLY_ASSIGNABLE,
4847 56 : non_constant_p, fun);
4848 : }
4849 :
4850 : /* Process std::meta::is_trivially_destructible_type. */
4851 :
4852 : static tree
4853 70 : eval_is_trivially_destructible_type (location_t loc, const constexpr_ctx *ctx,
4854 : tree type, bool *non_constant_p,
4855 : tree fun)
4856 : {
4857 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_TRIVIALLY_DESTRUCTIBLE,
4858 0 : non_constant_p, fun);
4859 : }
4860 :
4861 : /* Process std::meta::is_nothrow_constructible_type. */
4862 :
4863 : static tree
4864 158 : eval_is_nothrow_constructible_type (location_t loc, const constexpr_ctx *ctx,
4865 : tree type, tree tvec, bool *non_constant_p,
4866 : tree fun)
4867 : {
4868 0 : return eval_type_trait (loc, ctx, type, tvec, CPTK_IS_NOTHROW_CONSTRUCTIBLE,
4869 0 : non_constant_p, fun);
4870 : }
4871 :
4872 : /* Process std::meta::is_nothrow_default_constructible_type. */
4873 :
4874 : static tree
4875 70 : eval_is_nothrow_default_constructible_type (location_t loc,
4876 : const constexpr_ctx *ctx,
4877 : tree type, bool *non_constant_p,
4878 : tree fun)
4879 : {
4880 70 : return eval_type_trait (loc, ctx, type, make_tree_vec (0),
4881 : CPTK_IS_NOTHROW_CONSTRUCTIBLE,
4882 70 : non_constant_p, fun);
4883 : }
4884 :
4885 : /* Process std::meta::is_nothrow_copy_constructible_type. */
4886 :
4887 : static tree
4888 96 : eval_is_nothrow_copy_constructible_type (location_t loc,
4889 : const constexpr_ctx *ctx,
4890 : tree type, bool *non_constant_p,
4891 : tree fun)
4892 : {
4893 96 : tree arg = make_tree_vec (1);
4894 96 : TREE_VEC_ELT (arg, 0) = build_const_lref (type);
4895 96 : return eval_type_trait (loc, ctx, type, arg,
4896 : CPTK_IS_NOTHROW_CONSTRUCTIBLE,
4897 96 : non_constant_p, fun);
4898 : }
4899 :
4900 : /* Process std::meta::is_nothrow_move_constructible_type. */
4901 :
4902 : static tree
4903 80 : eval_is_nothrow_move_constructible_type (location_t loc,
4904 : const constexpr_ctx *ctx,
4905 : tree type, bool *non_constant_p,
4906 : tree fun)
4907 : {
4908 80 : tree arg = make_tree_vec (1);
4909 80 : TREE_VEC_ELT (arg, 0) = cp_build_reference_type (type, /*rval=*/true);
4910 80 : return eval_type_trait (loc, ctx, type, arg,
4911 : CPTK_IS_NOTHROW_CONSTRUCTIBLE,
4912 80 : non_constant_p, fun);
4913 : }
4914 :
4915 : /* Process std::meta::is_nothrow_assignable_type. */
4916 :
4917 : static tree
4918 40 : eval_is_nothrow_assignable_type (location_t loc, const constexpr_ctx *ctx,
4919 : tree type1, tree type2, bool *non_constant_p,
4920 : tree fun)
4921 : {
4922 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_NOTHROW_ASSIGNABLE,
4923 0 : non_constant_p, fun);
4924 : }
4925 :
4926 : /* Process std::meta::is_nothrow_copy_assignable_type. */
4927 :
4928 : static tree
4929 74 : eval_is_nothrow_copy_assignable_type (location_t loc, const constexpr_ctx *ctx,
4930 : tree type, bool *non_constant_p, tree fun)
4931 : {
4932 74 : tree type1 = cp_build_reference_type (type, /*rval=*/false);
4933 74 : tree type2 = build_const_lref (type);
4934 74 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_NOTHROW_ASSIGNABLE,
4935 74 : non_constant_p, fun);
4936 : }
4937 :
4938 : /* Process std::meta::is_nothrow_move_assignable_type. */
4939 :
4940 : static tree
4941 58 : eval_is_nothrow_move_assignable_type (location_t loc, const constexpr_ctx *ctx,
4942 : tree type, bool *non_constant_p, tree fun)
4943 : {
4944 58 : tree type1 = cp_build_reference_type (type, /*rval=*/false);
4945 58 : tree type2 = cp_build_reference_type (type, /*rval=*/true);
4946 58 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_NOTHROW_ASSIGNABLE,
4947 58 : non_constant_p, fun);
4948 : }
4949 :
4950 : /* Process std::meta::is_nothrow_destructible_type. */
4951 :
4952 : static tree
4953 184 : eval_is_nothrow_destructible_type (location_t loc, const constexpr_ctx *ctx,
4954 : tree type, bool *non_constant_p, tree fun)
4955 : {
4956 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_NOTHROW_DESTRUCTIBLE,
4957 0 : non_constant_p, fun);
4958 : }
4959 :
4960 : /* Process std::meta::is_implicit_lifetime_type. */
4961 :
4962 : static tree
4963 168 : eval_is_implicit_lifetime_type (location_t loc, const constexpr_ctx *ctx,
4964 : tree type, bool *non_constant_p, tree fun)
4965 : {
4966 0 : return eval_type_trait (loc, ctx, type, CPTK_IS_IMPLICIT_LIFETIME,
4967 0 : non_constant_p, fun);
4968 : }
4969 :
4970 : /* Process std::meta::has_virtual_destructor. */
4971 :
4972 : static tree
4973 38 : eval_has_virtual_destructor (location_t loc, const constexpr_ctx *ctx,
4974 : tree type, bool *non_constant_p, tree fun)
4975 : {
4976 0 : return eval_type_trait (loc, ctx, type, CPTK_HAS_VIRTUAL_DESTRUCTOR,
4977 0 : non_constant_p, fun);
4978 : }
4979 :
4980 : /* Process std::meta::has_unique_object_representations. */
4981 :
4982 : static tree
4983 100 : eval_has_unique_object_representations (location_t loc,
4984 : const constexpr_ctx *ctx,
4985 : tree type, bool *non_constant_p,
4986 : tree fun)
4987 : {
4988 0 : return eval_type_trait (loc, ctx, type, CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS,
4989 0 : non_constant_p, fun);
4990 : }
4991 :
4992 : /* Process std::meta::reference_constructs_from_temporary. */
4993 :
4994 : static tree
4995 82 : eval_reference_constructs_from_temporary (location_t loc,
4996 : const constexpr_ctx *ctx,
4997 : tree type1, tree type2,
4998 : bool *non_constant_p,
4999 : tree fun)
5000 : {
5001 0 : return eval_type_trait (loc, ctx, type1, type2,
5002 : CPTK_REF_CONSTRUCTS_FROM_TEMPORARY,
5003 0 : non_constant_p, fun);
5004 : }
5005 :
5006 : /* Process std::meta::reference_converts_from_temporary. */
5007 :
5008 : static tree
5009 82 : eval_reference_converts_from_temporary (location_t loc,
5010 : const constexpr_ctx *ctx,
5011 : tree type1, tree type2,
5012 : bool *non_constant_p,
5013 : tree fun)
5014 : {
5015 0 : return eval_type_trait (loc, ctx, type1, type2,
5016 : CPTK_REF_CONVERTS_FROM_TEMPORARY,
5017 0 : non_constant_p, fun);
5018 : }
5019 :
5020 : /* Process std::meta::rank. */
5021 :
5022 : static tree
5023 30 : eval_rank (tree type)
5024 : {
5025 30 : size_t rank = 0;
5026 86 : for (; TREE_CODE (type) == ARRAY_TYPE; type = TREE_TYPE (type))
5027 56 : ++rank;
5028 30 : return build_int_cst (size_type_node, rank);
5029 : }
5030 :
5031 : /* Process std::meta::extent. */
5032 :
5033 : static tree
5034 242 : eval_extent (location_t loc, const constexpr_ctx *ctx, tree type, tree i,
5035 : bool *non_constant_p, tree fun)
5036 : {
5037 242 : size_t rank = tree_to_uhwi (i);
5038 274 : while (rank && TREE_CODE (type) == ARRAY_TYPE)
5039 : {
5040 32 : --rank;
5041 32 : type = TREE_TYPE (type);
5042 : }
5043 242 : tree r;
5044 242 : if (rank
5045 234 : || TREE_CODE (type) != ARRAY_TYPE
5046 242 : || eval_is_bounded_array_type (loc, ctx, type, non_constant_p,
5047 216 : fun) != boolean_true_node)
5048 42 : r = size_zero_node;
5049 : else
5050 200 : r = size_binop (PLUS_EXPR, TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
5051 : size_one_node);
5052 : /* std::meta::extent returns a value of type size_t. */
5053 242 : return cp_fold_convert (size_type_node, r);
5054 : }
5055 :
5056 : /* Process std::meta::is_same_type. */
5057 :
5058 : static tree
5059 220 : eval_is_same_type (location_t loc, const constexpr_ctx *ctx, tree type1,
5060 : tree type2, bool *non_constant_p, tree fun)
5061 : {
5062 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_SAME,
5063 0 : non_constant_p, fun);
5064 : }
5065 :
5066 : /* Process std::meta::is_base_of_type. */
5067 :
5068 : static tree
5069 36 : eval_is_base_of_type (location_t loc, const constexpr_ctx *ctx,
5070 : tree type1, tree type2, bool *non_constant_p, tree fun)
5071 : {
5072 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_BASE_OF,
5073 0 : non_constant_p, fun);
5074 : }
5075 :
5076 : /* Process std::meta::is_virtual_base_of_type. */
5077 :
5078 : static tree
5079 66 : eval_is_virtual_base_of_type (location_t loc, const constexpr_ctx *ctx,
5080 : tree type1, tree type2, bool *non_constant_p,
5081 : tree fun)
5082 : {
5083 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_VIRTUAL_BASE_OF,
5084 0 : non_constant_p, fun);
5085 : }
5086 :
5087 : /* Process std::meta::is_convertible_type. */
5088 :
5089 : static tree
5090 50 : eval_is_convertible_type (location_t loc, const constexpr_ctx *ctx,
5091 : tree type1, tree type2, bool *non_constant_p,
5092 : tree fun)
5093 : {
5094 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_CONVERTIBLE,
5095 0 : non_constant_p, fun);
5096 : }
5097 :
5098 : /* Process std::meta::is_nothrow_convertible_type. */
5099 :
5100 : static tree
5101 54 : eval_is_nothrow_convertible_type (location_t loc, const constexpr_ctx *ctx,
5102 : tree type1, tree type2, bool *non_constant_p,
5103 : tree fun)
5104 : {
5105 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_NOTHROW_CONVERTIBLE,
5106 0 : non_constant_p, fun);
5107 : }
5108 :
5109 : /* Process std::meta::is_layout_compatible_type. */
5110 :
5111 : static tree
5112 56 : eval_is_layout_compatible_type (location_t loc, const constexpr_ctx *ctx,
5113 : tree type1, tree type2, bool *non_constant_p,
5114 : tree fun)
5115 : {
5116 0 : return eval_type_trait (loc, ctx, type1, type2, CPTK_IS_LAYOUT_COMPATIBLE,
5117 0 : non_constant_p, fun);
5118 : }
5119 :
5120 : /* Process std::meta::is_pointer_interconvertible_base_of_type. */
5121 :
5122 : static tree
5123 50 : eval_is_pointer_interconvertible_base_of_type (location_t loc,
5124 : const constexpr_ctx *ctx,
5125 : tree type1, tree type2,
5126 : bool *non_constant_p,
5127 : tree fun)
5128 : {
5129 0 : return eval_type_trait (loc, ctx, type1, type2,
5130 : CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF,
5131 0 : non_constant_p, fun);
5132 : }
5133 :
5134 : /* Process std::meta::is_invocable_type. */
5135 :
5136 : static tree
5137 134 : eval_is_invocable_type (location_t loc, const constexpr_ctx *ctx,
5138 : tree type, tree tvec, bool *non_constant_p,
5139 : tree fun)
5140 : {
5141 0 : return eval_type_trait (loc, ctx, type, tvec, CPTK_IS_INVOCABLE,
5142 0 : non_constant_p, fun);
5143 : }
5144 :
5145 : /* Helper for various eval_* type trait functions which can't use builtin
5146 : trait and have to instantiate std::NAME<ARGS>::value. */
5147 :
5148 : static tree
5149 1612 : finish_library_value_trait (location_t loc, const constexpr_ctx *ctx,
5150 : const char *name, tree args, tree call,
5151 : bool *non_constant_p, tree *jump_target, tree fun)
5152 : {
5153 1612 : tree inst = lookup_template_class (get_identifier (name), args,
5154 : /*in_decl*/NULL_TREE, /*context*/std_node,
5155 : tf_warning_or_error);
5156 1612 : inst = complete_type (inst);
5157 1612 : if (inst == error_mark_node
5158 1612 : || !COMPLETE_TYPE_P (inst)
5159 3212 : || !CLASS_TYPE_P (inst))
5160 : {
5161 12 : if (!cxx_constexpr_quiet_p (ctx))
5162 4 : error_at (loc, "couldn%'t instantiate %<std::%s<%T>%>",
5163 : name, args);
5164 12 : *non_constant_p = true;
5165 12 : return call;
5166 : }
5167 1600 : tree val = lookup_qualified_name (inst, value_identifier,
5168 : LOOK_want::NORMAL, /*complain*/false);
5169 1600 : if (val == error_mark_node)
5170 0 : return throw_exception (loc, ctx, "value member missing",
5171 0 : fun, non_constant_p, jump_target);
5172 1600 : if (VAR_P (val) || TREE_CODE (val) == CONST_DECL)
5173 1600 : val = maybe_constant_value (val, NULL_TREE, mce_true);
5174 1600 : if (TREE_CODE (TREE_TYPE (call)) == BOOLEAN_TYPE)
5175 : {
5176 1536 : if (integer_zerop (val))
5177 788 : return boolean_false_node;
5178 748 : else if (integer_nonzerop (val))
5179 748 : return boolean_true_node;
5180 : else
5181 0 : return throw_exception (loc, ctx, "unexpected value of value member",
5182 0 : fun, non_constant_p, jump_target);
5183 : }
5184 64 : else if (TREE_CODE (val) == INTEGER_CST)
5185 : {
5186 64 : val = build_converted_constant_expr (TREE_TYPE (call), val, tf_none);
5187 64 : if (TREE_CODE (val) == INTEGER_CST)
5188 : return val;
5189 : }
5190 0 : return throw_exception (loc, ctx, "unexpected value of value member",
5191 0 : fun, non_constant_p, jump_target);
5192 : }
5193 :
5194 : /* Process std::meta::is_{,nothrow_}invocable_r_type. */
5195 :
5196 : static tree
5197 290 : eval_is_invocable_r_type (location_t loc, const constexpr_ctx *ctx,
5198 : tree tres, tree type, tree tvec, tree call,
5199 : bool *non_constant_p, tree *jump_target, tree fun,
5200 : const char *name)
5201 : {
5202 : /* Create std::is_invocable_r<TYPE>::value. */
5203 290 : tree args = make_tree_vec (TREE_VEC_LENGTH (tvec) + 2);
5204 290 : TREE_VEC_ELT (args, 0) = tres;
5205 290 : TREE_VEC_ELT (args, 1) = type;
5206 580 : for (int i = 0; i < TREE_VEC_LENGTH (tvec); ++i)
5207 290 : TREE_VEC_ELT (args, i + 2) = TREE_VEC_ELT (tvec, i);
5208 290 : return finish_library_value_trait (loc, ctx, name, args, call,
5209 290 : non_constant_p, jump_target, fun);
5210 : }
5211 :
5212 : /* Process std::meta::is_nothrow_invocable_type. */
5213 :
5214 : static tree
5215 114 : eval_is_nothrow_invocable_type (location_t loc, const constexpr_ctx *ctx,
5216 : tree type, tree tvec, bool *non_constant_p,
5217 : tree fun)
5218 : {
5219 0 : return eval_type_trait (loc, ctx, type, tvec, CPTK_IS_NOTHROW_INVOCABLE,
5220 0 : non_constant_p, fun);
5221 : }
5222 :
5223 : /* Process std::meta::is_{,nothrow_}swappable_with_type. */
5224 :
5225 : static tree
5226 128 : eval_is_swappable_with_type (location_t loc, const constexpr_ctx *ctx,
5227 : tree type1, tree type2, tree call,
5228 : bool *non_constant_p, tree *jump_target, tree fun,
5229 : const char *name)
5230 : {
5231 : /* Create std::is_swappable_with<TYPE>::value. */
5232 128 : tree args = make_tree_vec (2);
5233 128 : TREE_VEC_ELT (args, 0) = type1;
5234 128 : TREE_VEC_ELT (args, 1) = type2;
5235 128 : return finish_library_value_trait (loc, ctx, name, args, call,
5236 128 : non_constant_p, jump_target, fun);
5237 : }
5238 :
5239 : /* Process std::meta::is_{,nothrow_}swappable_type. */
5240 :
5241 : static tree
5242 382 : eval_is_swappable_type (location_t loc, const constexpr_ctx *ctx,
5243 : tree type, tree call, bool *non_constant_p,
5244 : tree *jump_target, tree fun, const char *name)
5245 : {
5246 : /* Create std::is_swappable<TYPE>::value. */
5247 382 : tree args = make_tree_vec (1);
5248 382 : TREE_VEC_ELT (args, 0) = type;
5249 382 : return finish_library_value_trait (loc, ctx, name, args, call,
5250 382 : non_constant_p, jump_target, fun);
5251 : }
5252 :
5253 : /* Process std::meta::remove_cvref. */
5254 :
5255 : static tree
5256 34 : eval_remove_cvref (location_t loc, tree type)
5257 : {
5258 34 : if (TYPE_REF_P (type))
5259 16 : type = TREE_TYPE (type);
5260 34 : type = finish_trait_type (CPTK_REMOVE_CV, type, NULL_TREE, tf_none);
5261 34 : type = strip_typedefs (type);
5262 34 : return get_reflection_raw (loc, type);
5263 : }
5264 :
5265 : /* Process std::meta::decay. */
5266 :
5267 : static tree
5268 42 : eval_decay (location_t loc, tree type)
5269 : {
5270 42 : type = finish_trait_type (CPTK_DECAY, type, NULL_TREE, tf_none);
5271 42 : type = strip_typedefs (type);
5272 42 : return get_reflection_raw (loc, type);
5273 : }
5274 :
5275 : /* Helper for various eval_* type trait functions which can't use builtin
5276 : trait and have to instantiate std::NAME<ARGS>::type. */
5277 :
5278 : static tree
5279 610 : finish_library_type_trait (location_t loc, const constexpr_ctx *ctx,
5280 : const char *name, tree args, tree call,
5281 : bool *non_constant_p, tree *jump_target, tree fun)
5282 : {
5283 610 : tree inst = lookup_template_class (get_identifier (name), args,
5284 : /*in_decl*/NULL_TREE,
5285 : /*context*/std_node,
5286 : tf_warning_or_error);
5287 610 : if (inst == error_mark_node)
5288 : {
5289 0 : if (!cxx_constexpr_quiet_p (ctx))
5290 0 : error_at (loc, "couldn%'t instantiate %<std::%s<%T>%>",
5291 : name, args);
5292 0 : *non_constant_p = true;
5293 0 : return call;
5294 : }
5295 610 : tree type = make_typename_type (inst, type_identifier,
5296 : none_type, tf_none);
5297 610 : if (type == error_mark_node)
5298 18 : return throw_exception (loc, ctx, "type member missing",
5299 18 : fun, non_constant_p, jump_target);
5300 592 : type = strip_typedefs (type);
5301 592 : return get_reflection_raw (loc, type);
5302 : }
5303 :
5304 : /* Process std::meta::common_{type,reference}. */
5305 :
5306 : static tree
5307 242 : eval_common_type (location_t loc, const constexpr_ctx *ctx, tree tvec,
5308 : tree call, bool *non_constant_p, tree *jump_target, tree fun,
5309 : const char *name)
5310 : {
5311 0 : return finish_library_type_trait (loc, ctx, name, tvec, call,
5312 0 : non_constant_p, jump_target, fun);
5313 : }
5314 :
5315 : /* Process std::meta::underlying_type. */
5316 :
5317 : static tree
5318 24 : eval_underlying_type (location_t loc, const constexpr_ctx *ctx, tree type,
5319 : bool *non_constant_p, tree *jump_target, tree fun)
5320 : {
5321 24 : if (TREE_CODE (type) != ENUMERAL_TYPE || !COMPLETE_TYPE_P (type))
5322 8 : return throw_exception (loc, ctx, "reflection does not represent "
5323 : "a complete enumeration type",
5324 8 : fun, non_constant_p, jump_target);
5325 16 : type = finish_underlying_type (type);
5326 16 : type = strip_typedefs (type);
5327 16 : return get_reflection_raw (loc, type);
5328 : }
5329 :
5330 : /* Process std::meta::invoke_result. */
5331 :
5332 : static tree
5333 20 : eval_invoke_result (location_t loc, const constexpr_ctx *ctx, tree type,
5334 : tree tvec, tree call, bool *non_constant_p,
5335 : tree *jump_target, tree fun)
5336 : {
5337 20 : tree args = make_tree_vec (TREE_VEC_LENGTH (tvec) + 1);
5338 20 : TREE_VEC_ELT (args, 0) = type;
5339 64 : for (int i = 0; i < TREE_VEC_LENGTH (tvec); ++i)
5340 44 : TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (tvec, i);
5341 20 : return finish_library_type_trait (loc, ctx, "invoke_result", args, call,
5342 20 : non_constant_p, jump_target, fun);
5343 : }
5344 :
5345 : /* Process std::meta::unwrap_{reference,ref_decay}. */
5346 :
5347 : static tree
5348 52 : eval_unwrap_reference (location_t loc, const constexpr_ctx *ctx, tree type,
5349 : tree call, bool *non_constant_p, tree *jump_target,
5350 : tree fun, const char *name)
5351 : {
5352 52 : tree args = make_tree_vec (1);
5353 52 : TREE_VEC_ELT (args, 0) = type;
5354 52 : return finish_library_type_trait (loc, ctx, name, args, call,
5355 52 : non_constant_p, jump_target, fun);
5356 : }
5357 :
5358 : /* Process std::meta::type_order. */
5359 :
5360 : static tree
5361 116 : eval_type_order (tree type1, tree type2)
5362 : {
5363 116 : return type_order_value (strip_typedefs (type1), strip_typedefs (type2));
5364 : }
5365 :
5366 : /* Process std::meta::enumerators_of.
5367 : Returns: A vector containing the reflections of each enumerator of the
5368 : enumeration represented by dealias(type_enum), in the order in which they
5369 : are declared.
5370 : Throws: meta::exception unless dealias(type_enum) represents an enumeration
5371 : type, and is_enumerable_type(type_enum) is true. */
5372 :
5373 : static tree
5374 266 : eval_enumerators_of (location_t loc, const constexpr_ctx *ctx, tree r,
5375 : bool *non_constant_p, tree *jump_target, tree fun)
5376 : {
5377 266 : if (TREE_CODE (r) != ENUMERAL_TYPE
5378 266 : || eval_is_enumerable_type (r) == boolean_false_node)
5379 144 : return throw_exception (loc, ctx, "reflection does not represent an "
5380 : "enumerable enumeration type", fun,
5381 144 : non_constant_p, jump_target);
5382 122 : vec<constructor_elt, va_gc> *elts = nullptr;
5383 564 : for (tree t = TYPE_VALUES (r); t; t = TREE_CHAIN (t))
5384 : {
5385 442 : tree e = TREE_VALUE (t);
5386 442 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, get_reflection_raw (loc, e));
5387 : }
5388 122 : return get_vector_of_info_elts (elts);
5389 : }
5390 :
5391 : /* Process std::meta::remove_const.
5392 : Returns: a reflection representing the type denoted by
5393 : std::remove_const_t<T>, where T is the type or type alias
5394 : represented by type. */
5395 :
5396 : static tree
5397 22 : eval_remove_const (location_t loc, tree type)
5398 : {
5399 22 : return get_reflection_raw (loc, remove_const (strip_typedefs (type)));
5400 : }
5401 :
5402 : /* Process std::meta::remove_volatile.
5403 : Returns: a reflection representing the type denoted by
5404 : std::remove_volatile_t<T>, where T is the type or type alias
5405 : represented by type. */
5406 :
5407 : static tree
5408 24 : eval_remove_volatile (location_t loc, tree type)
5409 : {
5410 24 : type = strip_typedefs (type);
5411 24 : auto quals = cp_type_quals (type);
5412 24 : quals &= ~TYPE_QUAL_VOLATILE;
5413 24 : type = cp_build_qualified_type (type, quals);
5414 24 : return get_reflection_raw (loc, type);
5415 : }
5416 :
5417 : /* Process std::meta::remove_cv.
5418 : Returns: a reflection representing the type denoted by
5419 : std::remove_cv_t<T>, where T is the type or type alias
5420 : represented by type. */
5421 :
5422 : static tree
5423 22 : eval_remove_cv (location_t loc, tree type)
5424 : {
5425 22 : type = strip_typedefs (type);
5426 22 : type = finish_trait_type (CPTK_REMOVE_CV, type, NULL_TREE, tf_none);
5427 22 : return get_reflection_raw (loc, type);
5428 : }
5429 :
5430 : /* Process std::meta::add_const.
5431 : Returns: a reflection representing the type denoted by
5432 : std::add_const_t<T>, where T is the type or type alias
5433 : represented by type. */
5434 :
5435 : static tree
5436 26 : eval_add_const (location_t loc, tree type)
5437 : {
5438 26 : type = strip_typedefs (type);
5439 26 : if (!TYPE_REF_P (type) && !FUNC_OR_METHOD_TYPE_P (type))
5440 : {
5441 20 : auto quals = cp_type_quals (type);
5442 20 : quals |= TYPE_QUAL_CONST;
5443 20 : type = cp_build_qualified_type (type, quals);
5444 : }
5445 26 : return get_reflection_raw (loc, type);
5446 : }
5447 :
5448 : /* Process std::meta::add_volatile.
5449 : Returns: a reflection representing the type denoted by
5450 : std::add_volatile_t<T>, where T is the type or type alias
5451 : represented by type. */
5452 :
5453 : static tree
5454 26 : eval_add_volatile (location_t loc, tree type)
5455 : {
5456 26 : type = strip_typedefs (type);
5457 26 : if (!TYPE_REF_P (type) && !FUNC_OR_METHOD_TYPE_P (type))
5458 : {
5459 20 : auto quals = cp_type_quals (type);
5460 20 : quals |= TYPE_QUAL_VOLATILE;
5461 20 : type = cp_build_qualified_type (type, quals);
5462 : }
5463 26 : return get_reflection_raw (loc, type);
5464 : }
5465 :
5466 : /* Process std::meta::add_cv.
5467 : Returns: a reflection representing the type denoted by
5468 : std::add_cv_t<T>, where T is the type or type alias
5469 : represented by type. */
5470 :
5471 : static tree
5472 26 : eval_add_cv (location_t loc, tree type)
5473 : {
5474 26 : type = strip_typedefs (type);
5475 26 : if (!TYPE_REF_P (type) && !FUNC_OR_METHOD_TYPE_P (type))
5476 : {
5477 20 : auto quals = cp_type_quals (type);
5478 20 : quals |= (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
5479 20 : type = cp_build_qualified_type (type, quals);
5480 : }
5481 26 : return get_reflection_raw (loc, type);
5482 : }
5483 :
5484 : /* Process std::meta::remove_reference.
5485 : Returns: a reflection representing the type denoted by
5486 : std::remove_reference_t<T>, where T is the type or type alias
5487 : represented by type. */
5488 :
5489 : static tree
5490 36 : eval_remove_reference (location_t loc, tree type)
5491 : {
5492 36 : if (TYPE_REF_P (type))
5493 26 : type = TREE_TYPE (type);
5494 36 : type = strip_typedefs (type);
5495 36 : return get_reflection_raw (loc, type);
5496 : }
5497 :
5498 : /* Process std::meta::add_lvalue_reference.
5499 : Returns: a reflection representing the type denoted by
5500 : std::add_lvalue_reference_t<T>, where T is the type or type alias
5501 : represented by type. */
5502 :
5503 : static tree
5504 32 : eval_add_lvalue_reference (location_t loc, tree type)
5505 : {
5506 32 : type = strip_typedefs (type);
5507 32 : type = finish_trait_type (CPTK_ADD_LVALUE_REFERENCE, type, NULL_TREE, tf_none);
5508 32 : return get_reflection_raw (loc, type);
5509 : }
5510 :
5511 : /* Process std::meta::add_rvalue_reference.
5512 : Returns: a reflection representing the type denoted by
5513 : std::add_rvalue_reference_t<T>, where T is the type or type alias
5514 : represented by type. */
5515 :
5516 : static tree
5517 30 : eval_add_rvalue_reference (location_t loc, tree type)
5518 : {
5519 30 : type = strip_typedefs (type);
5520 30 : type = finish_trait_type (CPTK_ADD_RVALUE_REFERENCE, type, NULL_TREE, tf_none);
5521 30 : return get_reflection_raw (loc, type);
5522 : }
5523 :
5524 : /* Process std::meta::make_signed and std::meta::make_unsigned.
5525 : Returns: a reflection representing the type denoted by
5526 : std::make_signed_t<T> or std::make_unsigned_t<T>, respectively, where T is
5527 : the type or type alias represented by type. */
5528 :
5529 : static tree
5530 100 : eval_make_signed (location_t loc, const constexpr_ctx *ctx, tree type,
5531 : bool unsignedp, bool *non_constant_p, tree *jump_target,
5532 : tree fun)
5533 : {
5534 100 : if (!INTEGRAL_TYPE_P (type) || TREE_CODE (type) == BOOLEAN_TYPE)
5535 12 : return throw_exception (loc, ctx, "reflection represents non-integral "
5536 : "or bool type", fun, non_constant_p,
5537 12 : jump_target);
5538 88 : tree ret = type;
5539 88 : if (TREE_CODE (type) == ENUMERAL_TYPE
5540 72 : || TYPE_MAIN_VARIANT (type) == wchar_type_node
5541 68 : || TYPE_MAIN_VARIANT (type) == char8_type_node
5542 64 : || TYPE_MAIN_VARIANT (type) == char16_type_node
5543 148 : || TYPE_MAIN_VARIANT (type) == char32_type_node)
5544 : {
5545 32 : tree unit = TYPE_SIZE_UNIT (type);
5546 32 : tree types[] = {
5547 32 : signed_char_type_node,
5548 32 : short_integer_type_node,
5549 32 : integer_type_node,
5550 32 : long_integer_type_node,
5551 32 : long_long_integer_type_node };
5552 32 : ret = NULL_TREE;
5553 76 : for (unsigned i = 0; i < ARRAY_SIZE (types); ++i)
5554 76 : if (tree_int_cst_equal (TYPE_SIZE_UNIT (types[i]), unit))
5555 : {
5556 32 : ret = c_common_signed_or_unsigned_type (unsignedp, types[i]);
5557 32 : break;
5558 : }
5559 32 : if (!ret)
5560 0 : ret = c_common_type_for_size (TYPE_PRECISION (type), unsignedp);
5561 : }
5562 56 : else if (TYPE_MAIN_VARIANT (type) == char_type_node)
5563 4 : ret = unsignedp ? unsigned_char_type_node : signed_char_type_node;
5564 52 : else if (unsignedp ^ (!!TYPE_UNSIGNED (type)))
5565 24 : ret = c_common_signed_or_unsigned_type (unsignedp, type);
5566 88 : if (ret != type)
5567 : {
5568 60 : auto quals = cp_type_quals (type);
5569 60 : quals &= (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
5570 60 : ret = cp_build_qualified_type (ret, quals);
5571 : }
5572 : else
5573 28 : ret = strip_typedefs (type);
5574 88 : return get_reflection_raw (loc, ret);
5575 : }
5576 :
5577 : /* Process std::meta::remove_extent.
5578 : Returns: a reflection representing the type denoted by
5579 : std::remove_extent_t<T>, where T is the type or type alias
5580 : represented by type. */
5581 :
5582 : static tree
5583 26 : eval_remove_extent (location_t loc, tree type)
5584 : {
5585 26 : if (TREE_CODE (type) == ARRAY_TYPE)
5586 20 : type = TREE_TYPE (type);
5587 26 : type = strip_typedefs (type);
5588 26 : return get_reflection_raw (loc, type);
5589 : }
5590 :
5591 : /* Process std::meta::remove_all_extents.
5592 : Returns: a reflection representing the type denoted by
5593 : std::remove_all_extents_t<T>, where T is the type or type alias
5594 : represented by type. */
5595 :
5596 : static tree
5597 22 : eval_remove_all_extents (location_t loc, tree type)
5598 : {
5599 22 : type = strip_array_types (type);
5600 22 : type = strip_typedefs (type);
5601 22 : return get_reflection_raw (loc, type);
5602 : }
5603 :
5604 : /* Process std::meta::remove_pointer.
5605 : Returns: a reflection representing the type denoted by
5606 : std::remove_pointer_t<T>, where T is the type or type alias
5607 : represented by type. */
5608 :
5609 : static tree
5610 26 : eval_remove_pointer (location_t loc, tree type)
5611 : {
5612 26 : if (TYPE_PTR_P (type))
5613 18 : type = TREE_TYPE (type);
5614 26 : type = strip_typedefs (type);
5615 26 : return get_reflection_raw (loc, type);
5616 : }
5617 :
5618 : /* Process std::meta::add_pointer.
5619 : Returns: a reflection representing the type denoted by
5620 : std::add_pointer_t<T>, where T is the type or type alias
5621 : represented by type. */
5622 :
5623 : static tree
5624 34 : eval_add_pointer (location_t loc, tree type)
5625 : {
5626 34 : type = strip_typedefs (type);
5627 34 : type = finish_trait_type (CPTK_ADD_POINTER, type, NULL_TREE, tf_none);
5628 34 : return get_reflection_raw (loc, type);
5629 : }
5630 :
5631 : /* Process std::meta::is_lvalue_reference_qualified and
5632 : std::meta::is_rvalue_reference_qualified.
5633 : Let T be type_of(r) if has-type(r) is true. Otherwise, let T be
5634 : dealias(r).
5635 : Returns: true if T represents an lvalue- or rvalue-qualified
5636 : function type, respectively. Otherwise, false.
5637 : RVALUE_P is true if we're processing is_rvalue_*, false if we're
5638 : processing is_lvalue_*. */
5639 :
5640 : static tree
5641 90 : eval_is_lrvalue_reference_qualified (tree r, reflect_kind kind,
5642 : bool rvalue_p)
5643 : {
5644 90 : if (has_type (r, kind))
5645 36 : r = type_of (r, kind);
5646 : else
5647 54 : r = maybe_strip_typedefs (r);
5648 90 : if (FUNC_OR_METHOD_TYPE_P (r)
5649 84 : && FUNCTION_REF_QUALIFIED (r)
5650 130 : && rvalue_p == FUNCTION_RVALUE_QUALIFIED (r))
5651 20 : return boolean_true_node;
5652 :
5653 70 : return boolean_false_node;
5654 : }
5655 :
5656 : /* Process std::meta::can_substitute.
5657 : Let Z be the template represented by templ and let Args... be a sequence of
5658 : prvalue constant expressions that compute the reflections held by the
5659 : elements of arguments, in order.
5660 : Returns: true if Z<[:Args:]...> is a valid template-id that does not name
5661 : a function whose type contains an undeduced placeholder type.
5662 : Otherwise, false.
5663 : Throws: meta::exception unless templ represents a template, and every
5664 : reflection in arguments represents a construct usable as a template
5665 : argument. */
5666 :
5667 : static tree
5668 892 : eval_can_substitute (location_t loc, const constexpr_ctx *ctx,
5669 : tree r, tree rvec, bool *non_constant_p, tree *jump_target,
5670 : tree fun)
5671 : {
5672 892 : if (eval_is_template (r) != boolean_true_node)
5673 316 : return throw_exception (loc, ctx,
5674 : "reflection does not represent a template",
5675 316 : fun, non_constant_p, jump_target);
5676 1250 : for (int i = 0; i < TREE_VEC_LENGTH (rvec); ++i)
5677 : {
5678 734 : tree ra = TREE_VEC_ELT (rvec, i);
5679 734 : tree a = REFLECT_EXPR_HANDLE (ra);
5680 734 : reflect_kind kind = REFLECT_EXPR_KIND (ra);
5681 : // TODO: It is unclear on what kinds of reflections we should throw
5682 : // and what kinds of exceptions should merely result in can_substitute
5683 : // returning false.
5684 734 : if (a == unknown_type_node
5685 726 : || kind == REFLECT_PARM
5686 718 : || eval_is_namespace (a) == boolean_true_node
5687 690 : || eval_is_constructor (a) == boolean_true_node
5688 690 : || eval_is_destructor (a) == boolean_true_node
5689 682 : || eval_is_annotation (a, kind) == boolean_true_node
5690 674 : || (TREE_CODE (a) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (a))
5691 : || kind == REFLECT_DATA_MEMBER_SPEC
5692 674 : || kind == REFLECT_BASE
5693 1408 : || (!TYPE_P (a)
5694 282 : && eval_is_template (a) == boolean_false_node
5695 274 : && !has_type (a, kind)))
5696 60 : return throw_exception (loc, ctx,
5697 : "invalid argument to can_substitute",
5698 60 : fun, non_constant_p, jump_target);
5699 674 : if (!TYPE_P (a))
5700 282 : a = convert_from_reference (a);
5701 674 : TREE_VEC_ELT (rvec, i) = a;
5702 : }
5703 516 : if (DECL_TYPE_TEMPLATE_P (r) || DECL_TEMPLATE_TEMPLATE_PARM_P (r))
5704 : {
5705 216 : tree type = lookup_template_class (r, rvec, NULL_TREE, NULL_TREE,
5706 : tf_none);
5707 216 : if (type == error_mark_node)
5708 96 : return boolean_false_node;
5709 : else
5710 120 : return boolean_true_node;
5711 : }
5712 524 : else if (concept_definition_p (r))
5713 : {
5714 152 : tree c = build_concept_check (r, rvec, tf_none);
5715 152 : if (c == error_mark_node)
5716 30 : return boolean_false_node;
5717 : else
5718 122 : return boolean_true_node;
5719 : }
5720 148 : else if (variable_template_p (r))
5721 : {
5722 68 : tree var = lookup_template_variable (r, rvec, tf_none);
5723 68 : if (var == error_mark_node)
5724 24 : return boolean_false_node;
5725 44 : var = finish_template_variable (var, tf_none);
5726 44 : if (var == error_mark_node)
5727 0 : return boolean_false_node;
5728 : else
5729 44 : return boolean_true_node;
5730 : }
5731 : else
5732 : {
5733 80 : tree fn = lookup_template_function (r, rvec);
5734 80 : if (fn == error_mark_node)
5735 0 : return boolean_false_node;
5736 80 : fn = resolve_nondeduced_context_or_error (fn, tf_none);
5737 80 : fn = MAYBE_BASELINK_FUNCTIONS (fn);
5738 80 : resolve_type_of_reflected_decl (fn);
5739 80 : if (fn == error_mark_node || undeduced_auto_decl (fn))
5740 42 : return boolean_false_node;
5741 38 : return boolean_true_node;
5742 : }
5743 : }
5744 :
5745 : /* Process std::meta::substitute.
5746 : Let Z be the template represented by templ and let Args... be a sequence of
5747 : prvalue constant expressions that compute the reflections held by the
5748 : elements of arguments, in order.
5749 : Returns: ^^Z<[:Args:]...>.
5750 : Throws: meta::exception unless can_substitute(templ, arguments) is true. */
5751 :
5752 : static tree
5753 594 : eval_substitute (location_t loc, const constexpr_ctx *ctx,
5754 : tree r, tree rvec, bool *non_constant_p, tree *jump_target,
5755 : tree fun)
5756 : {
5757 594 : tree cs = eval_can_substitute (loc, ctx, r, rvec, non_constant_p, jump_target,
5758 : fun);
5759 594 : if (*jump_target)
5760 : return cs;
5761 408 : if (cs == boolean_false_node)
5762 128 : return throw_exception (loc, ctx, "can_substitute returned false",
5763 128 : fun, non_constant_p, jump_target);
5764 280 : tree ret = NULL_TREE;
5765 280 : if (DECL_TYPE_TEMPLATE_P (r) || DECL_TEMPLATE_TEMPLATE_PARM_P (r))
5766 92 : ret = lookup_template_class (r, rvec, NULL_TREE, NULL_TREE, tf_none);
5767 350 : else if (concept_definition_p (r))
5768 : {
5769 118 : ret = build_concept_check (r, rvec, tf_none);
5770 118 : ret = evaluate_concept_check (ret);
5771 118 : return get_reflection_raw (loc, ret, REFLECT_VALUE);
5772 : }
5773 70 : else if (variable_template_p (r))
5774 40 : ret = lookup_and_finish_template_variable (r, rvec, tf_none);
5775 : else
5776 : {
5777 30 : if (DECL_FUNCTION_TEMPLATE_P (r))
5778 4 : r = ovl_make (r, NULL_TREE);
5779 30 : ret = lookup_template_function (r, rvec);
5780 30 : ret = resolve_nondeduced_context (ret, tf_none);
5781 : }
5782 162 : return get_reflection_raw (loc, ret);
5783 : }
5784 :
5785 : /* Process std::meta::tuple_size.
5786 : Returns: tuple_size_v<T>, where T is the type represented by
5787 : dealias(type). */
5788 :
5789 : static tree
5790 56 : eval_tuple_size (location_t loc, const constexpr_ctx *ctx, tree type,
5791 : tree call, bool *non_constant_p, tree *jump_target,
5792 : tree fun)
5793 : {
5794 : /* Create std::tuple_size<TYPE>::value. */
5795 56 : tree args = make_tree_vec (1);
5796 56 : TREE_VEC_ELT (args, 0) = type;
5797 56 : return finish_library_value_trait (loc, ctx, "tuple_size", args, call,
5798 56 : non_constant_p, jump_target, fun);
5799 : }
5800 :
5801 : /* Process std::meta::tuple_element.
5802 : Returns: A reflection representing the type denoted by
5803 : tuple_element_t<I, T>, where T is the type represented by dealias(type)
5804 : and I is a constant equal to index. */
5805 :
5806 : static tree
5807 62 : eval_tuple_element (location_t loc, const constexpr_ctx *ctx, tree i,
5808 : tree type, tree call, bool *non_constant_p,
5809 : tree *jump_target, tree fun)
5810 : {
5811 : /* Create std::tuple_element<I,TYPE>::type. */
5812 62 : tree args = make_tree_vec (2);
5813 62 : TREE_VEC_ELT (args, 0) = i;
5814 62 : TREE_VEC_ELT (args, 1) = type;
5815 62 : return finish_library_type_trait (loc, ctx, "tuple_element",
5816 : args, call, non_constant_p, jump_target,
5817 62 : fun);
5818 : }
5819 :
5820 : /* Process std::meta::is_{,nothrow_}applicable_type. */
5821 :
5822 : static tree
5823 736 : eval_is_applicable_type (location_t loc, const constexpr_ctx *ctx,
5824 : tree fn, tree tuple, tree call,
5825 : bool *non_constant_p, tree *jump_target, tree fun,
5826 : const char *name)
5827 : {
5828 : /* Create std::is_applicable<FN, TUPLE>::value. */
5829 736 : tree args = make_tree_vec (2);
5830 736 : TREE_VEC_ELT (args, 0) = fn;
5831 736 : TREE_VEC_ELT (args, 1) = tuple;
5832 736 : return finish_library_value_trait (loc, ctx, name, args, call,
5833 736 : non_constant_p, jump_target, fun);
5834 : }
5835 :
5836 : /* Process std::meta::apply_result. */
5837 :
5838 : static tree
5839 200 : eval_apply_result (location_t loc, const constexpr_ctx *ctx, tree fn,
5840 : tree tuple, tree call, bool *non_constant_p,
5841 : tree *jump_target, tree fun)
5842 : {
5843 : /* Create std::apply_result<FN, TUPLE>::type. */
5844 200 : tree args = make_tree_vec (2);
5845 200 : TREE_VEC_ELT (args, 0) = fn;
5846 200 : TREE_VEC_ELT (args, 1) = tuple;
5847 200 : return finish_library_type_trait (loc, ctx, "apply_result",
5848 : args, call, non_constant_p, jump_target,
5849 200 : fun);
5850 : }
5851 :
5852 : /* Process std::meta::variant_size.
5853 : Returns: variant_size_v<T>, where T is the type represented by
5854 : dealias(type). */
5855 :
5856 : static tree
5857 20 : eval_variant_size (location_t loc, const constexpr_ctx *ctx, tree type,
5858 : tree call, bool *non_constant_p, tree *jump_target,
5859 : tree fun)
5860 : {
5861 : /* Create std::variant_size<TYPE>::value. */
5862 20 : tree args = make_tree_vec (1);
5863 20 : TREE_VEC_ELT (args, 0) = type;
5864 20 : return finish_library_value_trait (loc, ctx, "variant_size", args, call,
5865 20 : non_constant_p, jump_target, fun);
5866 : }
5867 :
5868 : /* Process std::meta::variant_alternative.
5869 : Returns: A reflection representing the type denoted by
5870 : variant_alternative_t<I, T>, where T is the type represented by
5871 : dealias(type) and I is a constant equal to index. */
5872 :
5873 : static tree
5874 34 : eval_variant_alternative (location_t loc, const constexpr_ctx *ctx, tree i,
5875 : tree type, tree call, bool *non_constant_p,
5876 : tree *jump_target, tree fun)
5877 : {
5878 : /* Create std::variant_alternative<I,TYPE>::type. */
5879 34 : tree args = make_tree_vec (2);
5880 34 : TREE_VEC_ELT (args, 0) = i;
5881 34 : TREE_VEC_ELT (args, 1) = type;
5882 34 : return finish_library_type_trait (loc, ctx, "variant_alternative",
5883 : args, call, non_constant_p, jump_target,
5884 34 : fun);
5885 : }
5886 :
5887 : /* Process std::meta::data_member_spec.
5888 : Returns: A reflection of a data member description (T,N,A,W,NUA,ANN) where
5889 : -- T is the type represented by dealias(type),
5890 : -- N is either the identifier encoded by options.name or _|_ if
5891 : options.name does not contain a value,
5892 : -- A is either the alignment value held by options.alignment or _|_ if
5893 : options.alignment does not contain a value,
5894 : -- W is either the value held by options.bit_width or _|_ if
5895 : options.bit_width does not contain a value,
5896 : -- NUA is the value held by options.no_unique_address, and
5897 : -- ANN is the sequence of values constant_of(r) for each r in
5898 : options.annotations.
5899 : Throws: meta::exception unless the following conditions are met:
5900 : -- dealias(type) represents either an object type or a reference type;
5901 : -- if options.name contains a value, then:
5902 : -- holds_alternative<u8string>(options.name->contents) is true and
5903 : get<u8string>(options.name->contents) contains a valid identifier
5904 : that is not a keyword when interpreted with UTF-8, or
5905 : -- holds_alternative<string>(options.name->contents) is true and
5906 : get<string>(options.name->contents) contains a valid identifier
5907 : that is not a keyword when interpreted with the ordinary literal
5908 : encoding;
5909 : -- if options.name does not contain a value, then options.bit_width
5910 : contains a value and options.annotations is empty;
5911 : -- if options.bit_width contains a value V, then
5912 : -- is_integral_type(type) || is_enum_type(type) is true,
5913 : -- options.alignment does not contain a value,
5914 : -- options.no_unique_address is false,
5915 : -- V is not negative, and
5916 : -- if V equals 0, then options.name does not contain a value;
5917 : -- if options.alignment contains a value, it is an alignment value not less
5918 : than alignment_of(type); and
5919 : -- for every reflection r in options.annotations, has-type(r) is true,
5920 : type_of(r) represents a non-array object type, and evaluation of
5921 : constant_of(r) does not exit via an exception. */
5922 :
5923 : static tree
5924 751 : eval_data_member_spec (location_t loc, const constexpr_ctx *ctx,
5925 : tree type, tree opts, bool *non_constant_p,
5926 : bool *overflow_p, tree *jump_target, tree fun)
5927 : {
5928 751 : type = strip_typedefs (type);
5929 751 : if (!TYPE_OBJ_P (type) && !TYPE_REF_P (type))
5930 8 : return throw_exception (loc, ctx, "type is not object or reference type",
5931 8 : fun, non_constant_p, jump_target);
5932 743 : opts = convert_from_reference (opts);
5933 743 : if (!CLASS_TYPE_P (TREE_TYPE (opts)))
5934 : {
5935 0 : fail:
5936 0 : error_at (loc, "unexpected %<data_member_options%> argument");
5937 0 : *non_constant_p = true;
5938 0 : return NULL_TREE;
5939 : }
5940 743 : enum { m_name = 1, m_alignment, m_bit_width, m_no_unique_address,
5941 : m_annotations, n_args };
5942 743 : tree args[n_args] = { type, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
5943 743 : NULL_TREE };
5944 743 : for (tree field = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (opts)));
5945 4458 : field; field = next_aggregate_field (DECL_CHAIN (field)))
5946 3715 : if (tree name = DECL_NAME (field))
5947 : {
5948 3715 : if (id_equal (name, "name"))
5949 743 : args[m_name] = field;
5950 2972 : else if (id_equal (name, "alignment"))
5951 743 : args[m_alignment] = field;
5952 2229 : else if (id_equal (name, "bit_width"))
5953 743 : args[m_bit_width] = field;
5954 1486 : else if (id_equal (name, "no_unique_address"))
5955 743 : args[m_no_unique_address] = field;
5956 743 : else if (id_equal (name, "annotations"))
5957 743 : args[m_annotations] = field;
5958 : }
5959 3718 : for (int i = m_name; i < n_args; ++i)
5960 : {
5961 3123 : if (args[i] == NULL_TREE)
5962 0 : goto fail;
5963 3123 : tree opt = build3 (COMPONENT_REF, TREE_TYPE (args[i]), opts, args[i],
5964 : NULL_TREE);
5965 3123 : if (i == m_no_unique_address)
5966 : {
5967 : /* The no_unique_address handling is simple. */
5968 595 : if (TREE_CODE (TREE_TYPE (opt)) != BOOLEAN_TYPE)
5969 0 : goto fail;
5970 595 : opt = cxx_eval_constant_expression (ctx, opt, vc_prvalue,
5971 : non_constant_p, overflow_p,
5972 : jump_target);
5973 595 : if (*jump_target || *non_constant_p)
5974 148 : return NULL_TREE;
5975 595 : if (TREE_CODE (opt) != INTEGER_CST)
5976 0 : goto fail;
5977 595 : if (integer_zerop (opt))
5978 561 : args[i] = boolean_false_node;
5979 : else
5980 34 : args[i] = boolean_true_node;
5981 2478 : continue;
5982 : }
5983 2528 : if (i == m_annotations)
5984 : {
5985 : /* To handle annotations, read it using input range from
5986 : std::vector<info>. */
5987 595 : tree rtype
5988 595 : = cp_build_reference_type (TREE_TYPE (opt), /*rval*/false);
5989 595 : opt = build_address (opt);
5990 595 : opt = fold_convert (rtype, opt);
5991 595 : opt = get_info_vec (loc, ctx, opt, -1, non_constant_p, overflow_p,
5992 : jump_target, fun);
5993 595 : if (*jump_target || *non_constant_p)
5994 : return NULL_TREE;
5995 595 : args[i] = opt;
5996 595 : continue;
5997 595 : }
5998 : /* Otherwise the member is optional<something>. */
5999 1933 : if (!CLASS_TYPE_P (TREE_TYPE (opt)))
6000 0 : goto fail;
6001 1933 : tree has_value = build_static_cast (loc, boolean_type_node, opt,
6002 : tf_warning_or_error);
6003 1933 : if (error_operand_p (has_value))
6004 0 : goto fail;
6005 1933 : has_value = cxx_eval_constant_expression (ctx, has_value, vc_prvalue,
6006 : non_constant_p, overflow_p,
6007 : jump_target);
6008 1933 : if (*jump_target || *non_constant_p)
6009 : return NULL_TREE;
6010 1933 : if (TREE_CODE (has_value) != INTEGER_CST)
6011 0 : goto fail;
6012 1933 : if (integer_zerop (has_value))
6013 : {
6014 : /* If it doesn't have value, store NULL_TREE. */
6015 1076 : args[i] = NULL_TREE;
6016 1076 : continue;
6017 : }
6018 857 : tree deref = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, opt,
6019 : NULL_TREE, tf_warning_or_error);
6020 857 : if (error_operand_p (deref))
6021 0 : goto fail;
6022 857 : if (i != m_name)
6023 : {
6024 : /* For alignment and bit_width otherwise it should be int. */
6025 212 : if (TYPE_MAIN_VARIANT (TREE_TYPE (deref)) != integer_type_node)
6026 0 : goto fail;
6027 212 : deref = cxx_eval_constant_expression (ctx, deref, vc_prvalue,
6028 : non_constant_p, overflow_p,
6029 : jump_target);
6030 212 : if (*jump_target || *non_constant_p)
6031 : return NULL_TREE;
6032 212 : if (TREE_CODE (deref) != INTEGER_CST)
6033 0 : goto fail;
6034 212 : args[i] = deref;
6035 212 : continue;
6036 : }
6037 : /* Otherwise it is a name. */
6038 645 : if (!CLASS_TYPE_P (TREE_TYPE (deref)))
6039 0 : goto fail;
6040 645 : enum { m_is_u8, m_u8s, m_s, n_fields };
6041 645 : tree fields[n_fields] = { NULL_TREE, NULL_TREE, NULL_TREE };
6042 645 : for (tree field = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (deref)));
6043 3225 : field; field = next_aggregate_field (DECL_CHAIN (field)))
6044 2580 : if (tree name = DECL_NAME (field))
6045 : {
6046 2580 : if (id_equal (name, "_M_is_u8"))
6047 645 : fields[m_is_u8] = field;
6048 1935 : else if (id_equal (name, "_M_u8s"))
6049 645 : fields[m_u8s] = field;
6050 1290 : else if (id_equal (name, "_M_s"))
6051 645 : fields[m_s] = field;
6052 : }
6053 2336 : for (int j = 0; j < n_fields; ++j)
6054 : {
6055 1839 : if (fields[j] == NULL_TREE)
6056 0 : goto fail;
6057 1839 : if (j != m_is_u8
6058 2160 : && j == (fields[m_is_u8] == boolean_true_node ? m_s : m_u8s))
6059 1194 : continue;
6060 1290 : tree f = build3 (COMPONENT_REF, TREE_TYPE (fields[j]), deref,
6061 : fields[j], NULL_TREE);
6062 1290 : if (j == m_is_u8)
6063 : {
6064 : /* The _M_is_u8 handling is simple. */
6065 645 : if (TREE_CODE (TREE_TYPE (f)) != BOOLEAN_TYPE)
6066 0 : goto fail;
6067 645 : f = cxx_eval_constant_expression (ctx, f, vc_prvalue,
6068 : non_constant_p, overflow_p,
6069 : jump_target);
6070 645 : if (*jump_target || *non_constant_p)
6071 148 : return NULL_TREE;
6072 645 : if (TREE_CODE (f) != INTEGER_CST)
6073 0 : goto fail;
6074 645 : if (integer_zerop (f))
6075 483 : fields[m_is_u8] = boolean_false_node;
6076 : else
6077 162 : fields[m_is_u8] = boolean_true_node;
6078 645 : continue;
6079 : }
6080 : /* _M_u8s/_M_s handling is the same except for encoding. */
6081 645 : if (!CLASS_TYPE_P (TREE_TYPE (f)))
6082 0 : goto fail;
6083 645 : tree fns = lookup_qualified_name (TREE_TYPE (f),
6084 : get_identifier ("c_str"));
6085 645 : if (error_operand_p (fns))
6086 0 : goto fail;
6087 645 : f = build_new_method_call (f, fns, NULL, NULL_TREE, LOOKUP_NORMAL,
6088 : NULL, tf_warning_or_error);
6089 645 : if (error_operand_p (f))
6090 0 : goto fail;
6091 645 : f = cxx_eval_constant_expression (ctx, f, vc_prvalue,
6092 : non_constant_p, overflow_p,
6093 : jump_target);
6094 645 : if (*jump_target || *non_constant_p)
6095 : return NULL_TREE;
6096 645 : STRIP_NOPS (f);
6097 645 : if (TREE_CODE (f) != ADDR_EXPR)
6098 0 : goto fail;
6099 645 : f = TREE_OPERAND (f, 0);
6100 645 : f = cxx_eval_constant_expression (ctx, f, vc_prvalue,
6101 : non_constant_p, overflow_p,
6102 : jump_target);
6103 645 : if (*jump_target || *non_constant_p)
6104 : return NULL_TREE;
6105 645 : if (TREE_CODE (f) != CONSTRUCTOR
6106 645 : || TREE_CODE (TREE_TYPE (f)) != ARRAY_TYPE)
6107 0 : goto fail;
6108 645 : tree eltt = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (f)));
6109 645 : if (eltt != (j == m_u8s ? char8_type_node : char_type_node))
6110 0 : goto fail;
6111 : tree field, value;
6112 : unsigned k;
6113 : unsigned HOST_WIDE_INT l = 0;
6114 3388 : bool ntmbs = false;
6115 3388 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (f), k, field, value)
6116 3388 : if (!tree_fits_shwi_p (value))
6117 0 : goto fail;
6118 3388 : else if (field == NULL_TREE)
6119 : {
6120 0 : if (integer_zerop (value))
6121 : {
6122 : ntmbs = true;
6123 : break;
6124 : }
6125 0 : ++l;
6126 : }
6127 3388 : else if (TREE_CODE (field) == RANGE_EXPR)
6128 : {
6129 0 : tree lo = TREE_OPERAND (field, 0);
6130 0 : tree hi = TREE_OPERAND (field, 1);
6131 0 : if (!tree_fits_uhwi_p (lo) || !tree_fits_uhwi_p (hi))
6132 0 : goto fail;
6133 0 : if (integer_zerop (value))
6134 : {
6135 0 : l = tree_to_uhwi (lo);
6136 0 : ntmbs = true;
6137 0 : break;
6138 : }
6139 0 : l = tree_to_uhwi (hi) + 1;
6140 : }
6141 3388 : else if (tree_fits_uhwi_p (field))
6142 : {
6143 3388 : l = tree_to_uhwi (field);
6144 3388 : if (integer_zerop (value))
6145 : {
6146 : ntmbs = true;
6147 : break;
6148 : }
6149 2743 : ++l;
6150 : }
6151 : else
6152 0 : goto fail;
6153 645 : if (!ntmbs || l > INT_MAX - 1)
6154 0 : goto fail;
6155 645 : char *namep;
6156 645 : unsigned len = l;
6157 645 : if (l < 64)
6158 645 : namep = XALLOCAVEC (char, l + 1);
6159 : else
6160 0 : namep = XNEWVEC (char, l + 1);
6161 645 : memset (namep, 0, l + 1);
6162 645 : l = 0;
6163 3388 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (f), k, field, value)
6164 3388 : if (integer_zerop (value))
6165 : break;
6166 2743 : else if (field == NULL_TREE)
6167 : {
6168 0 : namep[l] = tree_to_shwi (value);
6169 0 : ++l;
6170 : }
6171 2743 : else if (TREE_CODE (field) == RANGE_EXPR)
6172 : {
6173 0 : tree lo = TREE_OPERAND (field, 0);
6174 0 : tree hi = TREE_OPERAND (field, 1);
6175 0 : unsigned HOST_WIDE_INT m = tree_to_uhwi (hi);
6176 0 : for (l = tree_to_uhwi (lo); l <= m; ++l)
6177 0 : namep[l] = tree_to_shwi (value);
6178 : }
6179 : else
6180 : {
6181 2743 : l = tree_to_uhwi (field);
6182 2743 : namep[l++] = tree_to_shwi (value);
6183 : }
6184 645 : namep[len] = '\0';
6185 : /* Convert namep from execution charset to SOURCE_CHARSET. */
6186 645 : cpp_string istr, ostr;
6187 645 : istr.len = strlen (namep) + 1;
6188 645 : istr.text = (const unsigned char *) namep;
6189 807 : if (!cpp_translate_string (parse_in, &istr, &ostr,
6190 : j == m_s ? CPP_STRING : CPP_UTF8STRING,
6191 : true))
6192 : {
6193 0 : if (len >= 64)
6194 0 : XDELETEVEC (namep);
6195 0 : if (j == m_s)
6196 0 : return throw_exception (loc, ctx,
6197 : "conversion from ordinary literal "
6198 : "encoding to source charset "
6199 : "failed", fun, non_constant_p,
6200 0 : jump_target);
6201 : else
6202 0 : return throw_exception (loc, ctx,
6203 : "conversion from UTF-8 encoding to "
6204 : "source charset failed",
6205 0 : fun, non_constant_p, jump_target);
6206 : }
6207 645 : if (len >= 64)
6208 0 : XDELETEVEC (namep);
6209 645 : if (!cpp_valid_identifier (parse_in, ostr.text))
6210 : {
6211 116 : free (const_cast <unsigned char *> (ostr.text));
6212 116 : return throw_exception (loc, ctx,
6213 : "name is not a valid identifier",
6214 116 : fun, non_constant_p, jump_target);
6215 : }
6216 529 : args[i] = get_identifier ((const char *) ostr.text);
6217 529 : free (const_cast <unsigned char *> (ostr.text));
6218 529 : switch (get_identifier_kind (args[i]))
6219 : {
6220 24 : case cik_keyword:
6221 24 : return throw_exception (loc, ctx, "name is a keyword",
6222 24 : fun, non_constant_p, jump_target);
6223 8 : case cik_trait:
6224 8 : return throw_exception (loc, ctx, "name is a built-in trait",
6225 8 : fun, non_constant_p, jump_target);
6226 497 : default:
6227 497 : break;
6228 : }
6229 : }
6230 : }
6231 595 : if (args[m_name] == NULL_TREE && args[m_bit_width] == NULL_TREE)
6232 12 : return throw_exception (loc, ctx,
6233 : "neither name nor bit_width specified",
6234 12 : fun, non_constant_p, jump_target);
6235 583 : if (args[m_name] == NULL_TREE && TREE_VEC_LENGTH (args[m_annotations]))
6236 4 : return throw_exception (loc, ctx,
6237 : "no name and non-empty annotations specified",
6238 4 : fun, non_constant_p, jump_target);
6239 579 : if (args[m_bit_width])
6240 : {
6241 154 : if (!CP_INTEGRAL_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
6242 4 : return throw_exception (loc, ctx,
6243 : "bit_width specified with non-integral "
6244 : "and non-enumeration type",
6245 4 : fun, non_constant_p, jump_target);
6246 150 : if (args[m_alignment])
6247 4 : return throw_exception (loc, ctx,
6248 : "both alignment and bit_width specified",
6249 4 : fun, non_constant_p, jump_target);
6250 146 : if (args[m_no_unique_address] == boolean_true_node)
6251 4 : return throw_exception (loc, ctx,
6252 : "bit_width specified with "
6253 : "no_unique_address true",
6254 4 : fun, non_constant_p, jump_target);
6255 142 : if (integer_zerop (args[m_bit_width]) && args[m_name])
6256 4 : return throw_exception (loc, ctx,
6257 : "bit_width 0 with specified name",
6258 4 : fun, non_constant_p, jump_target);
6259 138 : if (tree_int_cst_sgn (args[m_bit_width]) < 0)
6260 4 : return throw_exception (loc, ctx, "bit_width is negative",
6261 4 : fun, non_constant_p, jump_target);
6262 134 : if (args[m_name] == NULL_TREE)
6263 : {
6264 82 : if (eval_is_const (type, REFLECT_UNDEF) == boolean_true_node)
6265 12 : return throw_exception (loc, ctx,
6266 : "name unspecified and type is const",
6267 12 : fun, non_constant_p, jump_target);
6268 70 : if (eval_is_volatile (type, REFLECT_UNDEF) == boolean_true_node)
6269 8 : return throw_exception (loc, ctx,
6270 : "name unspecified and type is volatile",
6271 8 : fun, non_constant_p, jump_target);
6272 : }
6273 : }
6274 539 : if (args[m_alignment])
6275 : {
6276 46 : if (!integer_pow2p (args[m_alignment]))
6277 4 : return throw_exception (loc, ctx,
6278 : "alignment is not power of two",
6279 4 : fun, non_constant_p, jump_target);
6280 42 : if (tree_int_cst_sgn (args[m_alignment]) < 0)
6281 4 : return throw_exception (loc, ctx, "alignment is negative",
6282 4 : fun, non_constant_p, jump_target);
6283 38 : tree al = cxx_sizeof_or_alignof_type (loc, type, ALIGNOF_EXPR, true,
6284 : tf_none);
6285 38 : if (TREE_CODE (al) == INTEGER_CST
6286 38 : && wi::to_widest (al) > wi::to_widest (args[m_alignment]))
6287 4 : return throw_exception (loc, ctx,
6288 : "alignment is smaller than alignment_of",
6289 4 : fun, non_constant_p, jump_target);
6290 : }
6291 659 : for (int i = 0; i < TREE_VEC_LENGTH (args[m_annotations]); ++i)
6292 : {
6293 148 : tree r = REFLECT_EXPR_HANDLE (TREE_VEC_ELT (args[m_annotations], i));
6294 148 : reflect_kind kind
6295 148 : = REFLECT_EXPR_KIND (TREE_VEC_ELT (args[m_annotations], i));
6296 148 : if (!has_type (r, kind))
6297 4 : return throw_exception (loc, ctx, "reflection does not have a type",
6298 4 : fun, non_constant_p, jump_target);
6299 144 : tree type = type_of (r, kind);
6300 144 : if ((eval_is_array_type (loc, ctx, type, non_constant_p, fun)
6301 144 : == boolean_true_node)
6302 144 : || (eval_is_object_type (loc, ctx, type, non_constant_p, fun)
6303 136 : != boolean_true_node))
6304 12 : return throw_exception (loc, ctx, "reflection does not have "
6305 : "non-array object type",
6306 12 : fun, non_constant_p, jump_target);
6307 132 : tree cst = eval_constant_of (loc, ctx, r, kind, non_constant_p,
6308 : overflow_p, jump_target, fun);
6309 132 : if (cst == NULL_TREE)
6310 : return NULL_TREE;
6311 132 : TREE_VEC_ELT (args[m_annotations], i) = cst;
6312 : }
6313 511 : tree ret = make_tree_vec (m_annotations
6314 511 : + TREE_VEC_LENGTH (args[m_annotations]));
6315 3577 : for (int i = 0; i < m_annotations; ++i)
6316 2555 : TREE_VEC_ELT (ret, i) = args[i];
6317 643 : for (int i = 0; i < TREE_VEC_LENGTH (args[m_annotations]); ++i)
6318 264 : TREE_VEC_ELT (ret, i + m_annotations)
6319 132 : = TREE_VEC_ELT (args[m_annotations], i);
6320 511 : return get_reflection_raw (loc, ret, REFLECT_DATA_MEMBER_SPEC);
6321 : }
6322 :
6323 : /* Process std::meta::define_aggregate.
6324 : Let C be the type represented by class_type and r_K be the Kth reflection
6325 : value in mdescrs.
6326 : For every r_K in mdescrs, let (T_K,N_K,A_K,W_K,NUA_K,ANN_K) be the
6327 : corresponding data member description represented by r_K.
6328 : Constant When:
6329 : -- class_type represents a cv-unqualified class type;
6330 : -- C is incomplete from every point in the evaluation context;
6331 : -- is_data_member_spec(r_K) is true for every r_K;
6332 : -- is_complete_type(T_K) is true for every r_K; and
6333 : -- for every pair (r_K,r_L) where K<L, if N_K is not _|_ and N_L is not
6334 : _|_, then either:
6335 : -- N_K is not the same identifier as N_L or
6336 : -- N_K is the identifier _ (U+005F LOW LINE).
6337 : Effects: Produces an injected declaration D that defines C and has
6338 : properties as follows:
6339 : -- The target scope of D is the scope to which C belongs.
6340 : -- The locus of D follows immediately after the core constant expression
6341 : currently under evaluation.
6342 : -- The characteristic sequence of D is the sequence of reflection values
6343 : r_K.
6344 : -- If C is a specialization of a templated class T, and C is not a local
6345 : class, then D is an explicit specialization of T.
6346 : -- For each r_K, there is a corresponding entity M_K with public access
6347 : belonging to the class scope of D with the following properties:
6348 : -- If N_K is _|_, M_K is an unnamed bit-field.
6349 : Otherwise, M_K is a non-static data member whose name is the
6350 : identifier N_K.
6351 : -- The type of M_K is T_K.
6352 : -- M_K is declared with the attribute [[no_unique_address]] if and only
6353 : if NUA_K is true.
6354 : -- If W_K is not _|_, M_K is a bit-field whose width is that value.
6355 : Otherwise, M_K is not a bit-field.
6356 : -- If A_K is not _|_, M_K has the alignment-specifier alignas(A_K).
6357 : Otherwise, M_K has no alignment-specifier.
6358 : -- M_K has an annotation whose underlying constant is r for every
6359 : reflection r in ANN_K.
6360 : -- For every r_L in mdescrs such that K<L, the declaration corresponding to
6361 : r_K precedes the declaration corresponding to r_L.
6362 : Returns: class_type.
6363 : Remarks: If C is a specialization of a templated class and it has not been
6364 : instantiated, C is treated as an explicit specialization. */
6365 :
6366 : static tree
6367 195 : eval_define_aggregate (location_t loc, const constexpr_ctx *ctx,
6368 : tree type, tree rvec, tree call, bool *non_constant_p)
6369 : {
6370 195 : tree orig_type = type;
6371 195 : if (!CLASS_TYPE_P (type))
6372 : {
6373 6 : if (!cxx_constexpr_quiet_p (ctx))
6374 6 : error_at (loc, "first %<define_aggregate%> argument is not a class "
6375 : "type reflection");
6376 6 : *non_constant_p = true;
6377 6 : return call;
6378 : }
6379 189 : if (typedef_variant_p (type))
6380 : {
6381 2 : if (!cxx_constexpr_quiet_p (ctx))
6382 2 : error_at (loc, "first %<define_aggregate%> argument is a reflection "
6383 : "of a type alias");
6384 2 : *non_constant_p = true;
6385 2 : return call;
6386 : }
6387 187 : if (cv_qualified_p (type))
6388 : {
6389 6 : if (!cxx_constexpr_quiet_p (ctx))
6390 6 : error_at (loc, "first %<define_aggregate%> argument is a "
6391 : "cv-qualified class type reflection");
6392 6 : *non_constant_p = true;
6393 6 : return call;
6394 : }
6395 181 : if (COMPLETE_TYPE_P (type))
6396 : {
6397 4 : if (!cxx_constexpr_quiet_p (ctx))
6398 4 : error_at (loc, "first %<define_aggregate%> argument is a complete "
6399 : "class type reflection");
6400 4 : *non_constant_p = true;
6401 4 : return call;
6402 : }
6403 177 : if (TYPE_BEING_DEFINED (type))
6404 : {
6405 10 : if (!cxx_constexpr_quiet_p (ctx))
6406 10 : error_at (loc, "first %<define_aggregate%> argument is a reflection "
6407 : "of a class type %qT being defined", type);
6408 10 : *non_constant_p = true;
6409 10 : return call;
6410 : }
6411 167 : hash_set<tree> nameset;
6412 479 : for (int i = 0; i < TREE_VEC_LENGTH (rvec); ++i)
6413 : {
6414 153 : tree ra = TREE_VEC_ELT (rvec, i);
6415 153 : tree a = REFLECT_EXPR_HANDLE (ra);
6416 153 : if (REFLECT_EXPR_KIND (ra) != REFLECT_DATA_MEMBER_SPEC)
6417 : {
6418 2 : if (!cxx_constexpr_quiet_p (ctx))
6419 2 : error_at (loc, "%<define_aggregate%> argument not a data member "
6420 : "description");
6421 2 : *non_constant_p = true;
6422 2 : return call;
6423 : }
6424 151 : if (eval_is_complete_type (TREE_VEC_ELT (a, 0)) != boolean_true_node)
6425 : {
6426 2 : if (!cxx_constexpr_quiet_p (ctx))
6427 2 : error_at (loc, "%<define_aggregate%> argument data member "
6428 : "description without complete type");
6429 2 : *non_constant_p = true;
6430 2 : return call;
6431 : }
6432 149 : if (TREE_VEC_ELT (a, 1)
6433 137 : && !id_equal (TREE_VEC_ELT (a, 1), "_")
6434 252 : && nameset.add (TREE_VEC_ELT (a, 1)))
6435 : {
6436 4 : if (!cxx_constexpr_quiet_p (ctx))
6437 4 : error_at (loc, "name %qD used in multiple data member "
6438 4 : "descriptions", TREE_VEC_ELT (a, 1));
6439 4 : *non_constant_p = true;
6440 4 : return call;
6441 : }
6442 145 : if (TYPE_WARN_IF_NOT_ALIGN (type)
6443 0 : && TREE_VEC_ELT (a, 3))
6444 : {
6445 0 : if (!cxx_constexpr_quiet_p (ctx))
6446 0 : error_at (loc, "cannot declare bit-field in "
6447 : "%<warn_if_not_aligned%> type");
6448 0 : *non_constant_p = true;
6449 0 : return call;
6450 : }
6451 : }
6452 159 : tree consteval_block = cxx_constexpr_consteval_block (ctx);
6453 159 : if (consteval_block == NULL_TREE)
6454 : {
6455 30 : if (!cxx_constexpr_quiet_p (ctx))
6456 8 : error_at (loc, "%<define_aggregate%> not evaluated from "
6457 : "%<consteval%> block");
6458 30 : *non_constant_p = true;
6459 30 : return call;
6460 : }
6461 129 : iloc_sentinel ils = loc;
6462 129 : type = TYPE_MAIN_VARIANT (type);
6463 129 : type = strip_typedefs (type);
6464 129 : tree cscope = NULL_TREE, tscope = NULL_TREE;
6465 199 : for (tree c = TYPE_CONTEXT (CP_DECL_CONTEXT (consteval_block)); c;
6466 70 : c = get_containing_scope (c))
6467 129 : if (TYPE_P (c) || TREE_CODE (c) == FUNCTION_DECL)
6468 : {
6469 : cscope = c;
6470 : break;
6471 : }
6472 321 : for (tree c = TYPE_CONTEXT (type); c; c = get_containing_scope (c))
6473 : {
6474 198 : if (c == consteval_block)
6475 : {
6476 6 : auto_diagnostic_group d;
6477 6 : error_at (loc, "%<define_aggregate%> evaluated from "
6478 : "%<consteval%> block which encloses %qT being "
6479 : "defined", type);
6480 6 : inform (DECL_SOURCE_LOCATION (consteval_block),
6481 : "%<consteval%> block defined here");
6482 6 : return get_reflection_raw (loc, orig_type);
6483 6 : }
6484 192 : if (tscope == NULL_TREE
6485 125 : && (TYPE_P (c) || TREE_CODE (c) == FUNCTION_DECL))
6486 55 : tscope = c;
6487 : }
6488 123 : if (cscope != tscope)
6489 : {
6490 32 : auto_diagnostic_group d;
6491 32 : if (cscope && tscope)
6492 : {
6493 50 : for (tree c = tscope; c; c = get_containing_scope (c))
6494 38 : if (c == cscope)
6495 : {
6496 6 : if (DECL_P (tscope))
6497 2 : error_at (loc, "%qD intervenes between %qT scope and "
6498 : "%<consteval%> block %<define_aggregate%> "
6499 : "is evaluated from", tscope, type);
6500 : else
6501 4 : error_at (loc, "%qT intervenes between %qT scope and "
6502 : "%<consteval%> block %<define_aggregate%> "
6503 : "is evaluated from", tscope, type);
6504 : cscope = NULL_TREE;
6505 : tscope = NULL_TREE;
6506 : break;
6507 : }
6508 42 : for (tree c = cscope; c; c = get_containing_scope (c))
6509 28 : if (c == tscope)
6510 : {
6511 4 : if (DECL_P (cscope))
6512 4 : error_at (loc, "%qD intervenes between %<consteval%> block "
6513 : "%<define_aggregate%> is evaluated from and "
6514 : "%qT scope", cscope, type);
6515 : else
6516 0 : error_at (loc, "%qT intervenes between %<consteval%> block "
6517 : "%<define_aggregate%> is evaluated from and "
6518 : "%qT scope", cscope, type);
6519 : cscope = NULL_TREE;
6520 : tscope = NULL_TREE;
6521 : break;
6522 : }
6523 18 : if (cscope && tscope)
6524 : {
6525 8 : if (DECL_P (cscope) && DECL_P (tscope))
6526 2 : error_at (loc, "%<define_aggregate%> evaluated from "
6527 : "%<consteval%> block enclosed by %qD while "
6528 : "%qT type being defined is enclosed by %qD",
6529 : cscope, type, tscope);
6530 6 : else if (DECL_P (cscope))
6531 2 : error_at (loc, "%<define_aggregate%> evaluated from "
6532 : "%<consteval%> block enclosed by %qD while "
6533 : "%qT type being defined is enclosed by %qT",
6534 : cscope, type, tscope);
6535 4 : else if (DECL_P (tscope))
6536 2 : error_at (loc, "%<define_aggregate%> evaluated from "
6537 : "%<consteval%> block enclosed by %qT while "
6538 : "%qT type being defined is enclosed by %qD",
6539 : cscope, type, tscope);
6540 2 : else if (tscope)
6541 2 : error_at (loc, "%<define_aggregate%> evaluated from "
6542 : "%<consteval%> block enclosed by %qT while "
6543 : "%qT type being defined is enclosed by %qT",
6544 : cscope, type, tscope);
6545 : }
6546 : }
6547 14 : else if (cscope && DECL_P (cscope))
6548 6 : error_at (loc, "%qD intervenes between %<consteval%> block "
6549 : "%<define_aggregate%> is evaluated from and %qT scope",
6550 : cscope, type);
6551 8 : else if (cscope)
6552 4 : error_at (loc, "%qT intervenes between %<consteval%> block "
6553 : "%<define_aggregate%> is evaluated from and %qT scope",
6554 : cscope, type);
6555 4 : else if (tscope && DECL_P (tscope))
6556 2 : error_at (loc, "%qD intervenes between %qT scope and %<consteval%> "
6557 : "block %<define_aggregate%> is evaluated from",
6558 : tscope, type);
6559 : else
6560 2 : error_at (loc, "%qT intervenes between %qT scope and %<consteval%> "
6561 : "block %<define_aggregate%> is evaluated from",
6562 : tscope, type);
6563 32 : inform (DECL_SOURCE_LOCATION (consteval_block),
6564 : "%<consteval%> block defined here");
6565 32 : return get_reflection_raw (loc, orig_type);
6566 32 : }
6567 91 : if (primary_template_specialization_p (type))
6568 : {
6569 24 : type = maybe_process_partial_specialization (type);
6570 24 : if (type == error_mark_node)
6571 : {
6572 0 : *non_constant_p = true;
6573 0 : return call;
6574 : }
6575 : }
6576 91 : if (!TYPE_BINFO (type))
6577 91 : xref_basetypes (type, NULL_TREE);
6578 91 : pushclass (type);
6579 91 : TYPE_BEING_DEFINED (type) = 1;
6580 91 : build_self_reference ();
6581 91 : tree fields = TYPE_FIELDS (type);
6582 232 : for (int i = 0; i < TREE_VEC_LENGTH (rvec); ++i)
6583 : {
6584 141 : tree ra = TREE_VEC_ELT (rvec, i);
6585 141 : tree a = REFLECT_EXPR_HANDLE (ra);
6586 141 : tree f = build_decl (cp_expr_loc_or_input_loc (ra), FIELD_DECL,
6587 141 : TREE_VEC_ELT (a, 1), TREE_VEC_ELT (a, 0));
6588 141 : DECL_CHAIN (f) = fields;
6589 141 : DECL_IN_AGGR_P (f) = 1;
6590 141 : DECL_CONTEXT (f) = type;
6591 141 : TREE_PUBLIC (f) = 1;
6592 : /* Bit-field. */
6593 141 : if (TREE_VEC_ELT (a, 3))
6594 : {
6595 : /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE.
6596 : check_bitfield_decl picks it from there later and sets DECL_SIZE
6597 : accordingly. */
6598 24 : DECL_BIT_FIELD_REPRESENTATIVE (f) = TREE_VEC_ELT (a, 3);
6599 24 : SET_DECL_C_BIT_FIELD (f);
6600 24 : DECL_NONADDRESSABLE_P (f) = 1;
6601 : /* If this bit-field is unnamed, it's padding. */
6602 24 : if (!TREE_VEC_ELT (a, 1))
6603 12 : DECL_PADDING_P (f) = 1;
6604 : }
6605 117 : else if (TREE_VEC_ELT (a, 2))
6606 : {
6607 10 : SET_DECL_ALIGN (f, tree_to_uhwi (TREE_VEC_ELT (a, 2))
6608 : * BITS_PER_UNIT);
6609 10 : DECL_USER_ALIGN (f) = 1;
6610 : }
6611 141 : if (TREE_VEC_ELT (a, 4) == boolean_true_node
6612 274 : || TREE_VEC_LENGTH (a) != 5)
6613 : {
6614 10 : tree attrs = NULL_TREE, attr;
6615 10 : if (TREE_VEC_ELT (a, 4) == boolean_true_node)
6616 : {
6617 8 : attr = build_tree_list (NULL_TREE,
6618 : get_identifier ("no_unique_address"));
6619 8 : attrs = build_tree_list (attr, NULL_TREE);
6620 : }
6621 20 : for (int i = TREE_VEC_LENGTH (a) - 1; i >= 5; --i)
6622 : {
6623 10 : attr = build_tree_list (internal_identifier,
6624 : annotation_identifier);
6625 10 : tree val = REFLECT_EXPR_HANDLE (TREE_VEC_ELT (a, i));
6626 10 : attrs = tree_cons (attr, build_tree_list (NULL_TREE, val), attrs);
6627 : }
6628 10 : cplus_decl_attributes (&f, attrs, 0);
6629 : }
6630 141 : fields = f;
6631 : }
6632 91 : TYPE_FIELDS (type) = fields;
6633 91 : finish_struct (type, NULL_TREE);
6634 91 : return get_reflection_raw (loc, orig_type);
6635 296 : }
6636 :
6637 : /* Implement std::meta::reflect_constant_string.
6638 : Let CharT be ranges::range_value_t<R>.
6639 : Mandates: CharT is one of char, wchar_t, char8_t, char16_t, char32_t.
6640 : Let V be the pack of values of type CharT whose elements are the
6641 : corresponding elements of r, except that if r refers to a string literal
6642 : object, then V does not include the trailing null terminator of r.
6643 : Let P be the template parameter object of type
6644 : const CharT[sizeof...(V) + 1] initialized with {V..., CharT()}.
6645 : Returns: ^^P. */
6646 :
6647 : static tree
6648 280 : eval_reflect_constant_string (location_t loc, const constexpr_ctx *ctx,
6649 : tree call, bool *non_constant_p,
6650 : bool *overflow_p, tree *jump_target, tree fun)
6651 : {
6652 280 : tree str = get_range_elts (loc, ctx, call, 0, non_constant_p, overflow_p,
6653 : jump_target, REFLECT_CONSTANT_STRING, fun);
6654 280 : if (*jump_target || *non_constant_p)
6655 : return NULL_TREE;
6656 262 : tree decl = get_template_parm_object (str,
6657 : mangle_template_parm_object (str));
6658 262 : DECL_MERGEABLE (decl) = 1;
6659 262 : return get_reflection_raw (loc, decl);
6660 : }
6661 :
6662 : /* Implement std::meta::reflect_constant_array.
6663 : Let T be ranges::range_value_t<R>.
6664 : Mandates: T is a structural type,
6665 : is_constructible_v<T, ranges::range_reference_t<R>> is true, and
6666 : is_copy_constructible_v<T> is true.
6667 : Let V be the pack of values of type info of the same size as r, where the
6668 : ith element is reflect_constant(e_i), where e_i is the ith element of r.
6669 : Let P be
6670 : -- If sizeof...(V) > 0 is true, then the template parameter object of type
6671 : const T[sizeof...(V)] initialized with {[:V:]...}.
6672 : -- Otherwise, the template parameter object of type array<T, 0> initialized
6673 : with {}.
6674 : Returns: ^^P. */
6675 :
6676 : static tree
6677 748 : eval_reflect_constant_array (location_t loc, const constexpr_ctx *ctx,
6678 : tree call, bool *non_constant_p,
6679 : bool *overflow_p, tree *jump_target, tree fun)
6680 : {
6681 748 : tree str = get_range_elts (loc, ctx, call, 0, non_constant_p, overflow_p,
6682 : jump_target, REFLECT_CONSTANT_ARRAY, fun);
6683 748 : if (*jump_target || *non_constant_p)
6684 : return NULL_TREE;
6685 724 : tree decl = get_template_parm_object (str,
6686 : mangle_template_parm_object (str));
6687 724 : DECL_MERGEABLE (decl) = 1;
6688 724 : return get_reflection_raw (loc, decl);
6689 : }
6690 :
6691 : /* Return CURRENT-SCOPE(P). */
6692 :
6693 : static tree
6694 880 : reflect_current_scope (location_t loc, const constexpr_ctx *ctx, tree call,
6695 : bool *non_constant_p, const char *name)
6696 : {
6697 880 : tree scope = cxx_constexpr_caller (ctx);
6698 : /* Ignore temporary current_function_decl changes caused by
6699 : push_access_scope. */
6700 880 : if (scope == NULL_TREE && current_function_decl)
6701 290 : scope = current_function_decl_without_access_scope ();
6702 : /* [meta.reflection.access.context]/(5.1.2): Otherwise, if an initialization
6703 : by an inherited constructor is using I, a point whose immediate scope is
6704 : the class scope corresponding to C. */
6705 1372 : if (scope && DECL_INHERITED_CTOR (scope))
6706 4 : scope = DECL_CONTEXT (scope);
6707 880 : if (scope == NULL_TREE)
6708 : {
6709 388 : if (cxx_constexpr_manifestly_const_eval (ctx) != mce_true)
6710 : {
6711 : /* Outside of functions limit this to manifestly constant-evaluation
6712 : so that we don't fold it prematurely. */
6713 0 : if (!cxx_constexpr_quiet_p (ctx))
6714 0 : error_at (loc, "%qs used outside of manifestly constant-evaluation",
6715 : name);
6716 0 : *non_constant_p = true;
6717 0 : return call;
6718 : }
6719 388 : if (current_class_type)
6720 880 : scope = current_class_type;
6721 : /* If we have been pushed into a different namespace, use it. */
6722 314 : else if (!vec_safe_is_empty (decl_namespace_list))
6723 48 : scope = decl_namespace_list->last ();
6724 266 : else if (current_namespace)
6725 : scope = current_namespace;
6726 : else
6727 0 : scope = global_namespace;
6728 : }
6729 : tree lam;
6730 1178 : while (LAMBDA_FUNCTION_P (scope)
6731 156 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (scope)))
6732 1178 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
6733 142 : scope = CP_TYPE_CONTEXT (CP_DECL_CONTEXT (scope));
6734 : return scope;
6735 : }
6736 :
6737 : /* Process std::meta::current_function. */
6738 :
6739 : static tree
6740 128 : eval_current_function (location_t loc, const constexpr_ctx *ctx,
6741 : tree call, bool *non_constant_p, tree *jump_target,
6742 : tree fun)
6743 : {
6744 128 : tree scope = reflect_current_scope (loc, ctx, call, non_constant_p,
6745 : "std::meta::current_function");
6746 128 : if (TREE_CODE (scope) != FUNCTION_DECL)
6747 78 : return throw_exception (loc, ctx,
6748 : "current scope does not represent a function",
6749 78 : fun, non_constant_p, jump_target);
6750 50 : return get_reflection_raw (loc, scope);
6751 : }
6752 :
6753 : /* Process std::meta::current_class. */
6754 :
6755 : static tree
6756 136 : eval_current_class (location_t loc, const constexpr_ctx *ctx,
6757 : tree call, bool *non_constant_p, tree *jump_target,
6758 : tree fun)
6759 : {
6760 136 : tree scope = reflect_current_scope (loc, ctx, call, non_constant_p,
6761 : "std::meta::current_class");
6762 136 : if (TREE_CODE (scope) == FUNCTION_DECL
6763 66 : && DECL_CONTEXT (scope)
6764 202 : && TYPE_P (DECL_CONTEXT (scope)))
6765 16 : scope = DECL_CONTEXT (scope);
6766 136 : if (!CLASS_TYPE_P (scope))
6767 88 : return throw_exception (loc, ctx,
6768 : "current scope does not represent a class"
6769 : " nor a member function",
6770 88 : fun, non_constant_p, jump_target);
6771 48 : return get_reflection_raw (loc, scope);
6772 : }
6773 :
6774 : /* Process std::meta::current_namespace. */
6775 :
6776 : static tree
6777 68 : eval_current_namespace (location_t loc, const constexpr_ctx *ctx,
6778 : tree call, bool *non_constant_p)
6779 : {
6780 68 : tree scope = reflect_current_scope (loc, ctx, call, non_constant_p,
6781 : "std::meta::current_namespace");
6782 68 : return get_reflection_raw (loc, decl_namespace_context (scope));
6783 : }
6784 :
6785 : /* Process std::meta::access_context::current. */
6786 :
6787 : static tree
6788 548 : eval_access_context_current (location_t loc, const constexpr_ctx *ctx,
6789 : tree call, bool *non_constant_p)
6790 : {
6791 548 : tree scope = reflect_current_scope (loc, ctx, call, non_constant_p,
6792 : "std::meta::access_context::current");
6793 548 : tree access_context = TREE_TYPE (call);
6794 548 : if (TREE_CODE (access_context) != RECORD_TYPE)
6795 : {
6796 0 : fail:
6797 0 : error_at (loc, "unexpected return type of %qs",
6798 : "std::meta::access_context::current");
6799 0 : return build_zero_cst (access_context);
6800 : }
6801 548 : tree scopef = next_aggregate_field (TYPE_FIELDS (access_context));
6802 548 : if (!scopef || !REFLECTION_TYPE_P (TREE_TYPE (scopef)))
6803 0 : goto fail;
6804 548 : tree classf = next_aggregate_field (DECL_CHAIN (scopef));
6805 548 : if (!classf || !REFLECTION_TYPE_P (TREE_TYPE (classf)))
6806 0 : goto fail;
6807 548 : if (next_aggregate_field (DECL_CHAIN (classf)))
6808 0 : goto fail;
6809 548 : vec<constructor_elt, va_gc> *elts = nullptr;
6810 548 : CONSTRUCTOR_APPEND_ELT (elts, scopef, get_reflection_raw (loc, scope));
6811 548 : CONSTRUCTOR_APPEND_ELT (elts, classf, get_null_reflection ());
6812 548 : return build_constructor (access_context, elts);
6813 : }
6814 :
6815 : /* Helper function to extract scope and designating class from
6816 : access_context ACTX. */
6817 :
6818 : static bool
6819 59392 : extract_access_context (location_t loc, tree actx, tree *scope,
6820 : tree *designating_class)
6821 : {
6822 59392 : if (TREE_CODE (actx) != CONSTRUCTOR
6823 59392 : || CONSTRUCTOR_NELTS (actx) != 2
6824 59392 : || !REFLECT_EXPR_P (CONSTRUCTOR_ELT (actx, 0)->value)
6825 118784 : || !REFLECT_EXPR_P (CONSTRUCTOR_ELT (actx, 1)->value))
6826 : {
6827 0 : error_at (loc, "invalid %<access_context%> argument");
6828 0 : return false;
6829 : }
6830 59392 : *scope = REFLECT_EXPR_HANDLE (CONSTRUCTOR_ELT (actx, 0)->value);
6831 59392 : *designating_class = REFLECT_EXPR_HANDLE (CONSTRUCTOR_ELT (actx, 1)->value);
6832 59392 : if (*scope == unknown_type_node)
6833 45494 : *scope = NULL_TREE;
6834 13898 : else if (TREE_CODE (*scope) != FUNCTION_DECL
6835 12630 : && TREE_CODE (*scope) != NAMESPACE_DECL
6836 19494 : && !CLASS_TYPE_P (*scope))
6837 : {
6838 0 : error_at (loc, "unexpected %<access_context::scope()%>");
6839 0 : return false;
6840 : }
6841 13898 : else if (CLASS_TYPE_P (*scope))
6842 5596 : *scope = TYPE_MAIN_VARIANT (*scope);
6843 59392 : if (*designating_class == unknown_type_node)
6844 53874 : *designating_class = NULL_TREE;
6845 5518 : else if (!CLASS_TYPE_P (*designating_class)
6846 11036 : || !COMPLETE_TYPE_P (*designating_class))
6847 : {
6848 0 : error_at (loc, "unexpected %<access_context::designating_class()%>");
6849 0 : return false;
6850 : }
6851 : else
6852 5518 : *designating_class = TYPE_MAIN_VARIANT (*designating_class);
6853 : return true;
6854 : }
6855 :
6856 : /* Process std::meta::is_accessible.
6857 : Let PARENT-CLS(r) be:
6858 : -- If parent_of(r) represents a class C, then C.
6859 : -- Otherwise, PARENT-CLS(parent_of(r)).
6860 : Let DESIGNATING-CLS(r, ctx) be:
6861 : -- If ctx.designating_class() represents a class C, then C.
6862 : -- Otherwise, PARENT-CLS(r).
6863 : Returns:
6864 : -- If r represents an unnamed bit-field F, then is_accessible(r_H, ctx),
6865 : where r_H represents a hypothetical non-static data member of the class
6866 : represented by PARENT-CLS(r) with the same access as F.
6867 : -- Otherwise, if r does not represent a class member or a direct base class
6868 : relationship, then true.
6869 : -- Otherwise, if r represents
6870 : -- a class member that is not a (possibly indirect or variant) member of
6871 : DESIGNATING-CLS(r, ctx) or
6872 : -- a direct base class relationship such that parent_of(r) does not
6873 : represent DESIGNATING-CLS(r, ctx) or a (direct or indirect) base
6874 : class thereof,
6875 : then false.
6876 : -- Otherwise, if ctx.scope() is the null reflection, then true.
6877 : -- Otherwise, letting P be a program point whose immediate scope is the
6878 : function parameter scope, class scope, or namespace scope corresponding
6879 : to the function, class, or namespace represented by ctx.scope():
6880 : -- If r represents a direct base class relationship (D,B), then true if
6881 : base class B of DESIGNATING-CLS(r, ctx) is accessible at P;
6882 : otherwise false.
6883 : -- Otherwise, r represents a class member M; true if M would be
6884 : accessible at P with the designating class (as DESIGNATING-CLS(r, ctx)
6885 : if the effect of any using-declarations were ignored. Otherwise,
6886 : false.
6887 : Throws: meta::exception if r represents a class member for which
6888 : PARENT-CLS(r) is an incomplete class. */
6889 :
6890 : static tree
6891 59392 : eval_is_accessible (location_t loc, const constexpr_ctx *ctx, tree r,
6892 : reflect_kind kind, tree actx, tree call,
6893 : bool *non_constant_p, tree *jump_target, tree fun)
6894 : {
6895 59392 : tree scope = NULL_TREE, designating_class = NULL_TREE, c;
6896 59392 : if (!extract_access_context (loc, actx, &scope, &designating_class))
6897 : {
6898 0 : *non_constant_p = true;
6899 0 : return call;
6900 : }
6901 :
6902 59392 : if (eval_is_class_member (r) == boolean_true_node)
6903 : {
6904 56468 : r = maybe_get_first_fn (r);
6905 56468 : c = r;
6906 56468 : if (TREE_CODE (r) == CONST_DECL && UNSCOPED_ENUM_P (DECL_CONTEXT (r)))
6907 78 : c = DECL_CONTEXT (r);
6908 56468 : if (TYPE_P (c))
6909 : {
6910 3584 : if (TYPE_NAME (c) && DECL_P (TYPE_NAME (c)))
6911 3584 : c = CP_DECL_CONTEXT (TYPE_NAME (c));
6912 : else
6913 0 : c = CP_TYPE_CONTEXT (c);
6914 : }
6915 : else
6916 52884 : c = CP_DECL_CONTEXT (r);
6917 : }
6918 2924 : else if (kind == REFLECT_BASE)
6919 : {
6920 2840 : c = direct_base_derived (r);
6921 2840 : r = BINFO_TYPE (r);
6922 : }
6923 : else
6924 : return boolean_true_node;
6925 59308 : if (!CLASS_TYPE_P (c) || !COMPLETE_TYPE_P (c))
6926 0 : return throw_exception (loc, ctx,
6927 : "incomplete parent class",
6928 0 : fun, non_constant_p, jump_target);
6929 59308 : if (designating_class)
6930 : {
6931 : tree p = c;
6932 5530 : while (ANON_AGGR_TYPE_P (p) && p != designating_class)
6933 12 : p = CP_TYPE_CONTEXT (p);
6934 5518 : if (p != designating_class
6935 5518 : && (!CLASS_TYPE_P (p)
6936 5512 : || !DERIVED_FROM_P (p, designating_class)))
6937 2 : return boolean_false_node;
6938 : }
6939 59306 : if (scope == NULL_TREE)
6940 45494 : return boolean_true_node;
6941 13812 : if (designating_class == NULL_TREE)
6942 8296 : designating_class = c;
6943 13812 : if (TREE_CODE (scope) == NAMESPACE_DECL)
6944 6950 : push_to_top_level ();
6945 6862 : else if (TYPE_P (scope))
6946 5594 : push_access_scope (TYPE_NAME (scope));
6947 : else
6948 1268 : push_access_scope (scope);
6949 13812 : tree ret = boolean_false_node;
6950 13812 : if (kind == REFLECT_BASE)
6951 : {
6952 646 : if (accessible_base_p (designating_class, r, /*consider_local_p=*/true))
6953 460 : ret = boolean_true_node;
6954 : }
6955 : else
6956 : {
6957 13166 : tree o = TYPE_P (r) ? TYPE_NAME (r) : r;
6958 13166 : if (accessible_p (TYPE_BINFO (designating_class), o,
6959 : /*consider_local_p=*/true))
6960 10056 : ret = boolean_true_node;
6961 : }
6962 13812 : if (TREE_CODE (scope) == NAMESPACE_DECL)
6963 6950 : pop_from_top_level ();
6964 6862 : else if (TYPE_P (scope))
6965 5594 : pop_access_scope (TYPE_NAME (scope));
6966 : else
6967 1268 : pop_access_scope (scope);
6968 : return ret;
6969 : }
6970 :
6971 : /* Returns true if R is C-members-of-representable from
6972 : current point P. */
6973 :
6974 : static bool
6975 83220 : members_of_representable_p (tree c, tree r)
6976 : {
6977 83220 : if (TREE_CODE (r) == CONST_DECL)
6978 : return false;
6979 145248 : if (LAMBDA_TYPE_P (c) && !LAMBDA_FUNCTION_P (r))
6980 : return false;
6981 81552 : if (TYPE_P (r))
6982 : {
6983 7166 : if (CP_DECL_CONTEXT (TYPE_NAME (r)) != c)
6984 : return false;
6985 8760 : if (LAMBDA_TYPE_P (r))
6986 : return false;
6987 6706 : if (OVERLOAD_TYPE_P (r))
6988 : return true;
6989 3798 : if (typedef_variant_p (r))
6990 : return true;
6991 : }
6992 74386 : else if (DECL_P (r))
6993 : {
6994 74386 : if (CP_DECL_CONTEXT (r) != c)
6995 : return false;
6996 14516 : if (DECL_CLASS_TEMPLATE_P (r)
6997 73078 : || DECL_FUNCTION_TEMPLATE_P (r)
6998 62598 : || variable_template_p (r)
6999 61200 : || DECL_ALIAS_TEMPLATE_P (r)
7000 59892 : || concept_definition_p (r)
7001 59870 : || TREE_CODE (r) == FIELD_DECL
7002 125408 : || TREE_CODE (r) == NAMESPACE_DECL)
7003 : return true;
7004 50932 : if (VAR_OR_FUNCTION_DECL_P (r))
7005 : {
7006 50932 : resolve_type_of_reflected_decl (r);
7007 50932 : if (!undeduced_auto_decl (r))
7008 49624 : return true;
7009 : }
7010 : }
7011 : return false;
7012 : }
7013 :
7014 : /* Callback for vector qsort to compare members by ascending DECL_UID. */
7015 :
7016 : static int
7017 702334 : members_cmp (const void *a, const void *b)
7018 : {
7019 702334 : const constructor_elt *ea = (const constructor_elt *) a;
7020 702334 : const constructor_elt *eb = (const constructor_elt *) b;
7021 702334 : tree vala = REFLECT_EXPR_HANDLE (ea->value);
7022 702334 : tree valb = REFLECT_EXPR_HANDLE (eb->value);
7023 702334 : if (TYPE_P (vala))
7024 144982 : vala = TYPE_NAME (vala);
7025 702334 : if (TYPE_P (valb))
7026 133514 : valb = TYPE_NAME (valb);
7027 702334 : if (DECL_UID (vala) < DECL_UID (valb))
7028 : return -1;
7029 355978 : if (DECL_UID (vala) > DECL_UID (valb))
7030 : return 1;
7031 0 : gcc_assert (ea == eb);
7032 : return 0;
7033 : }
7034 :
7035 : /* Enumerate members of namespace NS for eval_members_of. */
7036 :
7037 : static vec<constructor_elt, va_gc> *
7038 192 : namespace_members_of (location_t loc, tree ns)
7039 : {
7040 192 : struct data_t {
7041 : location_t loc = UNKNOWN_LOCATION;
7042 : tree ns = NULL_TREE;
7043 : vec<constructor_elt, va_gc> *elts = nullptr;
7044 : hash_set<tree> *seen = nullptr;
7045 :
7046 192 : data_t (location_t loc, tree ns) : loc (loc), ns (ns) {}
7047 240 : ~data_t () { delete seen; }
7048 : };
7049 :
7050 17158 : const auto walker = [](tree b, void *data_)
7051 : {
7052 16966 : auto *data = static_cast<data_t *>(data_);
7053 16966 : tree m = b;
7054 :
7055 16966 : if (VAR_P (b) && DECL_ANON_UNION_VAR_P (b))
7056 : {
7057 : /* TODO: This doesn't handle namespace N { static union {}; }
7058 : but we pedwarn on that, so perhaps it doesn't need to be
7059 : handled. */
7060 8 : tree v = DECL_VALUE_EXPR (b);
7061 8 : gcc_assert (v && TREE_CODE (v) == COMPONENT_REF);
7062 8 : tree var = TREE_OPERAND (v, 0);
7063 8 : tree type = TREE_TYPE (var);
7064 8 : if (!data->seen)
7065 8 : data->seen = new hash_set<tree>;
7066 8 : if (members_of_representable_p (data->ns, type)
7067 8 : && !data->seen->add (type))
7068 8 : CONSTRUCTOR_APPEND_ELT (data->elts, NULL_TREE,
7069 : get_reflection_raw (data->loc, type));
7070 8 : if (members_of_representable_p (data->ns, var)
7071 8 : && !data->seen->add (var))
7072 8 : CONSTRUCTOR_APPEND_ELT (data->elts, NULL_TREE,
7073 : get_reflection_raw (data->loc, var));
7074 8 : return;
7075 : }
7076 :
7077 16958 : if (TREE_CODE (b) == CONST_DECL
7078 216 : && enum_with_enumerator_for_linkage_p (CP_DECL_CONTEXT (b))
7079 17156 : && members_of_representable_p (data->ns, CP_DECL_CONTEXT (b)))
7080 : {
7081 : /* TODO: This doesn't handle namespace N { enum {}; }
7082 : but we pedwarn on that because it violates [dcl.pre]/6, so
7083 : perhaps it doesn't need to be handled. */
7084 198 : tree type = CP_DECL_CONTEXT (b);
7085 198 : if (!data->seen)
7086 20 : data->seen = new hash_set<tree>;
7087 198 : if (!data->seen->add (type))
7088 : {
7089 48 : CONSTRUCTOR_APPEND_ELT (data->elts, NULL_TREE,
7090 : get_reflection_raw (data->loc, type));
7091 48 : return;
7092 : }
7093 : }
7094 :
7095 16910 : if (TREE_CODE (b) == TYPE_DECL)
7096 1816 : m = TREE_TYPE (b);
7097 :
7098 16910 : if (!members_of_representable_p (data->ns, m))
7099 : return;
7100 :
7101 16424 : if (DECL_DECOMPOSITION_P (m) && !DECL_DECOMP_IS_BASE (m))
7102 : {
7103 78 : tree base = DECL_DECOMP_BASE (m);
7104 78 : if (!data->seen)
7105 20 : data->seen = new hash_set<tree>;
7106 78 : if (members_of_representable_p (data->ns, base)
7107 78 : && !data->seen->add (base))
7108 20 : CONSTRUCTOR_APPEND_ELT (data->elts, NULL_TREE,
7109 : get_reflection_raw (data->loc, base));
7110 78 : if (!DECL_HAS_VALUE_EXPR_P (m))
7111 32 : CONSTRUCTOR_APPEND_ELT (data->elts, NULL_TREE,
7112 : get_reflection_raw (data->loc, m,
7113 : REFLECT_VAR));
7114 78 : return;
7115 : }
7116 :
7117 : /* Skip GCC built-ins. */
7118 16346 : if (DECL_P (b) && DECL_IS_UNDECLARED_BUILTIN (b))
7119 : return;
7120 :
7121 : /* eval_is_accessible should be always true for namespace members,
7122 : so don't bother calling it here. */
7123 4820 : CONSTRUCTOR_APPEND_ELT (data->elts, NULL_TREE,
7124 : get_reflection_raw (data->loc, m));
7125 :
7126 : /* For typedef struct { ... } S; include both the S type
7127 : alias (added above) and dealias of that for the originally
7128 : unnamed type (added below). */
7129 4820 : if (TREE_CODE (b) == TYPE_DECL
7130 4820 : && TYPE_DECL_FOR_LINKAGE_PURPOSES_P (b))
7131 118 : CONSTRUCTOR_APPEND_ELT (data->elts, NULL_TREE,
7132 : get_reflection_raw (data->loc,
7133 : strip_typedefs (m)));
7134 : };
7135 :
7136 192 : data_t data (loc, ns);
7137 192 : walk_namespace_bindings (ns, walker, &data);
7138 :
7139 192 : if (data.elts)
7140 182 : data.elts->qsort (members_cmp);
7141 384 : return data.elts;
7142 192 : }
7143 :
7144 : /* Enumerate members of class R for eval_*members_of. KIND is
7145 : one of METAFN_{,{,NON}STATIC_DATA_}MEMBERS_OF or
7146 : METAFN_HAS_INACCESSIBLE_NONSTATIC_DATA_MEMBERS.
7147 : For the last kind don't append any elts except for the first one for
7148 : which is_accessible returned false. */
7149 :
7150 : static vec<constructor_elt, va_gc> *
7151 4398 : class_members_of (location_t loc, const constexpr_ctx *ctx, tree r,
7152 : tree actx, tree call, bool *non_constant_p,
7153 : tree *jump_target, enum metafn_code kind, tree fun)
7154 : {
7155 4398 : r = TYPE_MAIN_VARIANT (r);
7156 4398 : if (kind == METAFN_MEMBERS_OF)
7157 : {
7158 3748 : if (modules_p ())
7159 0 : lazy_load_pendings (TYPE_NAME (r));
7160 3748 : if (CLASSTYPE_LAZY_DEFAULT_CTOR (r))
7161 172 : lazily_declare_fn (sfk_constructor, r);
7162 3748 : if (CLASSTYPE_LAZY_COPY_CTOR (r))
7163 200 : lazily_declare_fn (sfk_copy_constructor, r);
7164 3748 : if (CLASSTYPE_LAZY_MOVE_CTOR (r))
7165 174 : lazily_declare_fn (sfk_move_constructor, r);
7166 3748 : if (CLASSTYPE_LAZY_DESTRUCTOR (r))
7167 194 : lazily_declare_fn (sfk_destructor, r);
7168 3748 : if (CLASSTYPE_LAZY_COPY_ASSIGN (r))
7169 274 : lazily_declare_fn (sfk_copy_assignment, r);
7170 3748 : if (CLASSTYPE_LAZY_MOVE_ASSIGN (r))
7171 244 : lazily_declare_fn (sfk_move_assignment, r);
7172 : }
7173 4398 : auto_vec <tree, 8> implicitly_declared;
7174 4398 : vec<constructor_elt, va_gc> *elts = nullptr;
7175 108336 : for (tree field = TYPE_FIELDS (r); field; field = DECL_CHAIN (field))
7176 : {
7177 103938 : tree m = field;
7178 103938 : if (TREE_CODE (field) == FIELD_DECL && DECL_ARTIFICIAL (field))
7179 : /* Ignore bases and the vptr. */
7180 66866 : continue;
7181 102384 : else if (DECL_CLONED_FUNCTION_P (field))
7182 : /* Ignore cloned cdtors. */
7183 31928 : continue;
7184 70456 : else if (DECL_SELF_REFERENCE_P (field))
7185 4398 : continue;
7186 66058 : else if (TREE_CODE (field) == TYPE_DECL)
7187 5144 : m = TREE_TYPE (field);
7188 60914 : else if (TREE_CODE (field) == FUNCTION_DECL)
7189 : {
7190 : /* Ignore functions with unsatisfied constraints. */
7191 31624 : if (!constraints_satisfied_p (field))
7192 40 : continue;
7193 31584 : if (DECL_MAYBE_DELETED (field))
7194 : {
7195 22 : ++function_depth;
7196 22 : maybe_synthesize_method (field);
7197 22 : --function_depth;
7198 : }
7199 : }
7200 66018 : if (members_of_representable_p (r, m))
7201 : {
7202 65696 : if (kind == METAFN_STATIC_DATA_MEMBERS_OF
7203 62750 : && eval_is_variable (m, REFLECT_UNDEF) != boolean_true_node)
7204 2946 : continue; /* For static_data_members_of only include
7205 : is_variable. */
7206 63830 : else if ((kind == METAFN_NONSTATIC_DATA_MEMBERS_OF
7207 59804 : || kind == METAFN_HAS_INACCESSIBLE_NONSTATIC_DATA_MEMBERS)
7208 59804 : && eval_is_nonstatic_data_member (m) != boolean_true_node)
7209 4026 : continue; /* For nonstatic_data_members_of only include
7210 : is_nonstatic_data_member. */
7211 55778 : tree a = eval_is_accessible (loc, ctx, m, REFLECT_UNDEF, actx, call,
7212 : non_constant_p, jump_target, fun);
7213 55778 : if (*jump_target || *non_constant_p)
7214 0 : return nullptr;
7215 55778 : if (a == boolean_false_node)
7216 : {
7217 2924 : if (kind == METAFN_HAS_INACCESSIBLE_NONSTATIC_DATA_MEMBERS
7218 14 : && elts == nullptr)
7219 6 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, boolean_true_node);
7220 2924 : continue;
7221 2924 : }
7222 52854 : gcc_assert (a == boolean_true_node);
7223 71830 : if (kind == METAFN_MEMBERS_OF
7224 51190 : && TREE_CODE (m) == FUNCTION_DECL
7225 80986 : && DECL_ARTIFICIAL (m))
7226 : {
7227 : /* Implicitly-declared special members or operator== members
7228 : appear after any user declared members. */
7229 18976 : implicitly_declared.safe_push (m);
7230 18976 : continue;
7231 : }
7232 33878 : else if (kind == METAFN_HAS_INACCESSIBLE_NONSTATIC_DATA_MEMBERS)
7233 74 : continue;
7234 33804 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7235 : get_reflection_raw (loc, m));
7236 : }
7237 : }
7238 : /* TYPE_DECLs in TYPE_FIELDS come after other decls due to the "struct
7239 : stat hack" (see finish_member_declaration), so for members_of the
7240 : declaration order is not preserved. */
7241 4398 : if (kind == METAFN_MEMBERS_OF && elts)
7242 3658 : elts->qsort (members_cmp);
7243 3748 : if (kind == METAFN_MEMBERS_OF && !implicitly_declared.is_empty ())
7244 : {
7245 3592 : gcc_assert (implicitly_declared.length () <= 8);
7246 21352 : for (tree m : implicitly_declared)
7247 18946 : if (default_ctor_p (m))
7248 : {
7249 1186 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7250 : get_reflection_raw (loc, m));
7251 1186 : break;
7252 : }
7253 24956 : for (tree m : implicitly_declared)
7254 35520 : if (DECL_COPY_CONSTRUCTOR_P (m))
7255 : {
7256 3580 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7257 : get_reflection_raw (loc, m));
7258 3580 : break;
7259 : }
7260 14348 : for (tree m : implicitly_declared)
7261 7144 : if (special_function_p (m) == sfk_copy_assignment)
7262 : {
7263 3572 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7264 : get_reflection_raw (loc, m));
7265 3572 : break;
7266 : }
7267 21546 : for (tree m : implicitly_declared)
7268 28580 : if (DECL_MOVE_CONSTRUCTOR_P (m))
7269 : {
7270 3520 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7271 : get_reflection_raw (loc, m));
7272 3520 : break;
7273 : }
7274 10990 : for (tree m : implicitly_declared)
7275 3734 : if (special_function_p (m) == sfk_move_assignment)
7276 : {
7277 3520 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7278 : get_reflection_raw (loc, m));
7279 3520 : break;
7280 : }
7281 17922 : for (tree m : implicitly_declared)
7282 10714 : if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (m))
7283 : {
7284 3568 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7285 : get_reflection_raw (loc, m));
7286 3568 : break;
7287 : }
7288 29726 : for (tree m : implicitly_declared)
7289 18976 : if (DECL_OVERLOADED_OPERATOR_IS (m, EQ_EXPR))
7290 : {
7291 26 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7292 : get_reflection_raw (loc, m));
7293 26 : break;
7294 : }
7295 29748 : for (tree m : implicitly_declared)
7296 18980 : if (LAMBDA_FUNCTION_P (m))
7297 : {
7298 4 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7299 : get_reflection_raw (loc, m));
7300 4 : break;
7301 : }
7302 : }
7303 4398 : return elts;
7304 4398 : }
7305 :
7306 : /* Enumerate bases of class R for eval_*of. KIND is METAFN_BASES_OF
7307 : or METAFN_HAS_INACCESSIBLE_BASES. */
7308 :
7309 : static vec<constructor_elt, va_gc> *
7310 1282 : class_bases_of (location_t loc, const constexpr_ctx *ctx, tree r,
7311 : tree actx, tree call, bool *non_constant_p,
7312 : tree *jump_target, enum metafn_code kind, tree fun)
7313 : {
7314 1282 : vec<constructor_elt, va_gc> *elts = nullptr;
7315 1282 : tree binfo = TYPE_BINFO (r), base_binfo;
7316 4060 : for (unsigned i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
7317 : {
7318 2778 : tree a = eval_is_accessible (loc, ctx, base_binfo, REFLECT_BASE, actx,
7319 : call, non_constant_p, jump_target, fun);
7320 2778 : if (*jump_target || *non_constant_p)
7321 : return nullptr;
7322 2778 : if (a == boolean_false_node)
7323 : {
7324 154 : if (kind == METAFN_HAS_INACCESSIBLE_BASES && elts == nullptr)
7325 16 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, boolean_true_node);
7326 154 : continue;
7327 154 : }
7328 2624 : gcc_assert (a == boolean_true_node);
7329 2624 : if (kind == METAFN_HAS_INACCESSIBLE_BASES)
7330 106 : continue;
7331 5296 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE,
7332 : get_reflection_raw (loc, base_binfo,
7333 : REFLECT_BASE));
7334 : }
7335 1282 : return elts;
7336 : }
7337 :
7338 : /* Implement std::meta::members_of.
7339 : A declaration D members-of-precedes a point P if D precedes either P or the
7340 : point immediately following the class-specifier of the outermost class for
7341 : which P is in a complete-class context.
7342 : A declaration D of a member M of a class or namespace Q is
7343 : Q-members-of-eligible if
7344 : -- the host scope of D is the class scope or namespace scope associated
7345 : with Q,
7346 : -- D is not a friend declaration,
7347 : -- M is not a closure type,
7348 : -- M is not a specialization of a template,
7349 : -- if Q is a class that is not a closure type, then M is a direct member of
7350 : Q that is not a variant member of a nested anonymous union of Q, and
7351 : -- if Q is a closure type, then M is a function call operator or function
7352 : call operator template.
7353 : It is implementation-defined whether declarations of other members of a
7354 : closure type Q are Q-members-of-eligible.
7355 : A member M of a class or namespace Q is Q-members-of-representable from a
7356 : point P if a Q-members-of-eligible declaration of M members-of-precedes P,
7357 : and M is
7358 : -- a class or enumeration type
7359 : -- a type alias
7360 : -- a class template, function template, variable template, alias template,
7361 : or concept,
7362 : -- a variable or reference V for which the type of V does not contain an
7363 : undeduced placeholder type,
7364 : -- a function F for which
7365 : -- the type of F does not contain an undeduced placeholder type,
7366 : -- the constraints (if any) of F are satisfied, and
7367 : -- if F is a prospective destructor, F is the selected destructor,
7368 : -- a non-static data member,
7369 : -- a namespace, or
7370 : -- a namespace alias.
7371 : Returns: A vector containing reflections of all members M of the entity Q
7372 : represented by dealias(r) for which
7373 : -- M is Q-members-of-representable from some point in the evaluation
7374 : context and
7375 : -- is_accessible(^^M, ctx) is true.
7376 : If dealias(r) represents a class C, then the vector also contains
7377 : reflections representing all unnamed bit-fields B whose declarations
7378 : inhabit the class scope corresponding to C for which
7379 : is_accessible(^^B, ctx) is true.
7380 : Reflections of class members and unnamed bit-fields that are declared
7381 : appear in the order in which they are declared.
7382 : Throws: meta::exception unless dealias(r) is a reflection representing
7383 : either a class type that is complete from some point in the evaluation
7384 : context or a namespace. */
7385 :
7386 : static tree
7387 3940 : eval_members_of (location_t loc, const constexpr_ctx *ctx, tree r,
7388 : tree actx, tree call, bool *non_constant_p,
7389 : tree *jump_target, tree fun)
7390 : {
7391 3940 : r = maybe_strip_typedefs (r);
7392 3940 : if (TREE_CODE (r) == NAMESPACE_DECL)
7393 192 : r = ORIGINAL_NAMESPACE (r);
7394 3940 : vec<constructor_elt, va_gc> *elts;
7395 3940 : if (TREE_CODE (r) == NAMESPACE_DECL)
7396 192 : elts = namespace_members_of (loc, r);
7397 3748 : else if (CLASS_TYPE_P (r)
7398 7496 : && complete_type_or_maybe_complain (r, NULL_TREE, tf_none))
7399 : {
7400 3748 : elts = class_members_of (loc, ctx, r, actx, call, non_constant_p,
7401 : jump_target, METAFN_MEMBERS_OF, fun);
7402 3748 : if (*jump_target || *non_constant_p)
7403 : return NULL_TREE;
7404 : }
7405 : else
7406 0 : return throw_exception (loc, ctx,
7407 : "neither complete class type nor namespace",
7408 0 : fun, non_constant_p, jump_target);
7409 3940 : return get_vector_of_info_elts (elts);
7410 : }
7411 :
7412 : /* Implement std::meta::bases_of.
7413 : Returns: Let C be the class represented by dealias(type).
7414 : A vector containing the reflections of all the direct base class
7415 : relationships B, if any, of C such that is_accessible(^^B, ctx) is true.
7416 : The direct base class relationships appear in the order in which the
7417 : corresponding base classes appear in the base-specifier-list of C.
7418 : Throws: meta::exception unless dealias(type) represents a class type that
7419 : is complete from some point in the evaluation context. */
7420 :
7421 : static tree
7422 1010 : eval_bases_of (location_t loc, const constexpr_ctx *ctx, tree r,
7423 : tree actx, tree call, bool *non_constant_p,
7424 : tree *jump_target, tree fun)
7425 : {
7426 1010 : r = maybe_strip_typedefs (r);
7427 1010 : vec<constructor_elt, va_gc> *elts = nullptr;
7428 1010 : if (CLASS_TYPE_P (r)
7429 2020 : && complete_type_or_maybe_complain (r, NULL_TREE, tf_none))
7430 : {
7431 1010 : elts = class_bases_of (loc, ctx, r, actx, call, non_constant_p,
7432 : jump_target, METAFN_BASES_OF, fun);
7433 1010 : if (*jump_target || *non_constant_p)
7434 : return NULL_TREE;
7435 : }
7436 : else
7437 0 : return throw_exception (loc, ctx, "not a complete class type",
7438 0 : fun, non_constant_p, jump_target);
7439 1010 : return get_vector_of_info_elts (elts);
7440 : }
7441 :
7442 : /* Implement std::meta::static_data_members_of.
7443 : Returns: A vector containing each element e of members_of(type, ctx) such
7444 : that is_variable(e) is true, preserving their order.
7445 : Throws: meta::exception unless dealias(type) represents a class type that
7446 : is complete from some point in the evaluation context. */
7447 :
7448 : static tree
7449 96 : eval_static_data_members_of (location_t loc, const constexpr_ctx *ctx, tree r,
7450 : tree actx, tree call, bool *non_constant_p,
7451 : tree *jump_target, tree fun)
7452 : {
7453 96 : r = maybe_strip_typedefs (r);
7454 96 : vec<constructor_elt, va_gc> *elts = nullptr;
7455 96 : if (CLASS_TYPE_P (r)
7456 192 : && complete_type_or_maybe_complain (r, NULL_TREE, tf_none))
7457 : {
7458 96 : elts = class_members_of (loc, ctx, r, actx, call, non_constant_p,
7459 : jump_target, METAFN_STATIC_DATA_MEMBERS_OF,
7460 : fun);
7461 96 : if (*jump_target || *non_constant_p)
7462 : return NULL_TREE;
7463 : }
7464 : else
7465 0 : return throw_exception (loc, ctx, "not a complete class type",
7466 0 : fun, non_constant_p, jump_target);
7467 96 : return get_vector_of_info_elts (elts);
7468 : }
7469 :
7470 : /* Implement std::meta::nonstatic_data_members_of.
7471 : Returns: A vector containing each element e of members_of(type, ctx) such
7472 : that is_nonstatic_data_member(e) is true, preserving their order.
7473 : Throws: meta::exception unless dealias(type) represents a class type that
7474 : is complete from some point in the evaluation context. */
7475 :
7476 : static tree
7477 284 : eval_nonstatic_data_members_of (location_t loc, const constexpr_ctx *ctx,
7478 : tree r, tree actx, tree call,
7479 : bool *non_constant_p, tree *jump_target,
7480 : tree fun)
7481 : {
7482 284 : r = maybe_strip_typedefs (r);
7483 284 : vec<constructor_elt, va_gc> *elts = nullptr;
7484 284 : if (CLASS_TYPE_P (r)
7485 568 : && complete_type_or_maybe_complain (r, NULL_TREE, tf_none))
7486 : {
7487 284 : elts = class_members_of (loc, ctx, r, actx, call, non_constant_p,
7488 : jump_target, METAFN_NONSTATIC_DATA_MEMBERS_OF,
7489 : fun);
7490 284 : if (*jump_target || *non_constant_p)
7491 : return NULL_TREE;
7492 : }
7493 : else
7494 0 : return throw_exception (loc, ctx, "not a complete class type",
7495 0 : fun, non_constant_p, jump_target);
7496 284 : return get_vector_of_info_elts (elts);
7497 : }
7498 :
7499 : /* Implement std::meta::subobjects_of.
7500 : Returns: A vector containing each element of bases_of(type, ctx) followed
7501 : by each element of nonstatic_data_members_of(type, ctx), preserving their
7502 : order.
7503 : Throws: meta::exception unless dealias(type) represents a class type that
7504 : is complete from some point in the evaluation context. */
7505 :
7506 : static tree
7507 210 : eval_subobjects_of (location_t loc, const constexpr_ctx *ctx, tree r,
7508 : tree actx, tree call, bool *non_constant_p,
7509 : tree *jump_target, tree fun)
7510 : {
7511 210 : r = maybe_strip_typedefs (r);
7512 210 : vec<constructor_elt, va_gc> *elts = nullptr;
7513 210 : if (CLASS_TYPE_P (r)
7514 420 : && complete_type_or_maybe_complain (r, NULL_TREE, tf_none))
7515 : {
7516 210 : elts = class_bases_of (loc, ctx, r, actx, call, non_constant_p,
7517 : jump_target, METAFN_BASES_OF, fun);
7518 210 : if (*jump_target || *non_constant_p)
7519 : return NULL_TREE;
7520 210 : vec<constructor_elt, va_gc> *elts2
7521 210 : = class_members_of (loc, ctx, r, actx, call, non_constant_p,
7522 : jump_target, METAFN_NONSTATIC_DATA_MEMBERS_OF,
7523 : fun);
7524 210 : if (*jump_target || *non_constant_p)
7525 : return NULL_TREE;
7526 210 : if (elts == nullptr)
7527 66 : elts = elts2;
7528 144 : else if (elts2)
7529 136 : vec_safe_splice (elts, elts2);
7530 : }
7531 : else
7532 0 : return throw_exception (loc, ctx, "not a complete class type",
7533 0 : fun, non_constant_p, jump_target);
7534 210 : return get_vector_of_info_elts (elts);
7535 : }
7536 :
7537 : /* Implement std::meta::has_inaccessible_nonstatic_data_members.
7538 : Returns: true if is_accessible(R, ctx) is false for any R in
7539 : nonstatic_data_members_of(r, access_context::unchecked()).
7540 : Otherwise, false.
7541 : Throws: meta::exception if
7542 : -- the evaluation of
7543 : nonstatic_data_members_of(r, access_context::unchecked()) would exit via
7544 : an exception and or
7545 : -- r represents a closure type. */
7546 :
7547 : static tree
7548 60 : eval_has_inaccessible_nonstatic_data_members (location_t loc,
7549 : const constexpr_ctx *ctx,
7550 : tree r, tree actx, tree call,
7551 : bool *non_constant_p,
7552 : tree *jump_target, tree fun)
7553 : {
7554 60 : r = maybe_strip_typedefs (r);
7555 60 : vec<constructor_elt, va_gc> *elts = nullptr;
7556 60 : if (CLASS_TYPE_P (r)
7557 120 : && complete_type_or_maybe_complain (r, NULL_TREE, tf_none))
7558 : {
7559 120 : if (LAMBDA_TYPE_P (r))
7560 0 : return throw_exception (loc, ctx, "closure type", fun,
7561 0 : non_constant_p, jump_target);
7562 60 : elts = class_members_of (loc, ctx, r, actx, call, non_constant_p,
7563 : jump_target,
7564 : METAFN_HAS_INACCESSIBLE_NONSTATIC_DATA_MEMBERS,
7565 : fun);
7566 60 : if (*jump_target || *non_constant_p)
7567 : return NULL_TREE;
7568 : }
7569 : else
7570 0 : return throw_exception (loc, ctx, "not a complete class type",
7571 0 : fun, non_constant_p, jump_target);
7572 60 : if (elts == nullptr)
7573 54 : return boolean_false_node;
7574 : else
7575 6 : return boolean_true_node;
7576 : }
7577 :
7578 : /* Implement std::meta::has_inaccessible_bases.
7579 : Returns: true if is_accessible(R, ctx) is false for any R in
7580 : bases_of(r, access_context::unchecked()). Otherwise, false.
7581 : Throws: meta::exception if the evaluation of
7582 : bases_of(r, access_context::unchecked()) would exit via an exception. */
7583 :
7584 : static tree
7585 62 : eval_has_inaccessible_bases (location_t loc, const constexpr_ctx *ctx,
7586 : tree r, tree actx, tree call,
7587 : bool *non_constant_p, tree *jump_target,
7588 : tree fun)
7589 : {
7590 62 : r = maybe_strip_typedefs (r);
7591 62 : vec<constructor_elt, va_gc> *elts = nullptr;
7592 62 : if (CLASS_TYPE_P (r)
7593 124 : && complete_type_or_maybe_complain (r, NULL_TREE, tf_none))
7594 : {
7595 62 : elts = class_bases_of (loc, ctx, r, actx, call, non_constant_p,
7596 : jump_target, METAFN_HAS_INACCESSIBLE_BASES, fun);
7597 62 : if (*jump_target || *non_constant_p)
7598 : return NULL_TREE;
7599 : }
7600 : else
7601 0 : return throw_exception (loc, ctx, "not a complete class type",
7602 0 : fun, non_constant_p, jump_target);
7603 62 : if (elts == nullptr)
7604 46 : return boolean_false_node;
7605 : else
7606 16 : return boolean_true_node;
7607 : }
7608 :
7609 : /* Implement std::meta::has_inaccessible_subobjects.
7610 : Effects: Equivalent to:
7611 : return has_inaccessible_bases(r, ctx)
7612 : || has_inaccessible_nonstatic_data_members(r, ctx); */
7613 :
7614 : static tree
7615 38 : eval_has_inaccessible_subobjects (location_t loc, const constexpr_ctx *ctx,
7616 : tree r, tree actx, tree call,
7617 : bool *non_constant_p, tree *jump_target,
7618 : tree fun)
7619 : {
7620 38 : tree b = eval_has_inaccessible_bases (loc, ctx, r, actx, call,
7621 : non_constant_p, jump_target, fun);
7622 38 : if (*jump_target || *non_constant_p)
7623 : return NULL_TREE;
7624 38 : if (b == boolean_true_node)
7625 : return b;
7626 30 : return eval_has_inaccessible_nonstatic_data_members (loc, ctx, r, actx,
7627 : call, non_constant_p,
7628 30 : jump_target, fun);
7629 : }
7630 :
7631 : /* Implement std::meta::exception::_S_exception_cvt_to_utf8 and
7632 : std::meta::exception::_S_exception_cvt_from_utf8. This is
7633 : an implementation specific metafunction which translates string_view
7634 : into u8string_view resp. u8string_view into string_view for use in
7635 : std::meta::exception constructors. On translation failure returns an empty
7636 : {u8,}string_view. TO_UTF8 is true for _S_exception_cvt_to_utf8 and false
7637 : for _S_exception_cvt_from_utf8. */
7638 :
7639 : static tree
7640 3302 : eval_exception__S_exception_cvt_tofrom_utf8 (location_t loc,
7641 : const constexpr_ctx *ctx,
7642 : tree call, bool *non_constant_p,
7643 : bool *overflow_p,
7644 : tree *jump_target, tree fun,
7645 : bool to_utf8)
7646 : {
7647 3302 : tree str = get_range_elts (loc, ctx, call, 0, non_constant_p, overflow_p,
7648 : jump_target, REFLECT_CONSTANT_STRING, fun);
7649 3302 : if (*jump_target || *non_constant_p)
7650 : return NULL_TREE;
7651 3302 : if (TREE_CODE (str) != STRING_CST
7652 6604 : || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (str)))
7653 3302 : != (to_utf8 ? char_type_node : char8_type_node)))
7654 : {
7655 0 : error_at (loc, "unexpected argument to %qs",
7656 : to_utf8 ? "_S_exception_cvt_to_utf8"
7657 : : "_S_exception_cvt_from_utf8");
7658 0 : *non_constant_p = true;
7659 0 : return call;
7660 : }
7661 : /* We need to translate the string twice for the theoretical case
7662 : of non-UTF8 SOURCE_CHARSET. First translate from {exec charset,UTF-8} to
7663 : SOURCE_CHARSET... */
7664 3302 : cpp_string istr, ostr;
7665 3302 : istr.len = TREE_STRING_LENGTH (str);
7666 3302 : istr.text = (const unsigned char *) TREE_STRING_POINTER (str);
7667 3302 : const char *name;
7668 3346 : if (!cpp_translate_string (parse_in, &istr, &ostr,
7669 : to_utf8 ? CPP_STRING : CPP_UTF8STRING, true))
7670 : {
7671 0 : ostr.text = NULL;
7672 0 : name = "";
7673 : }
7674 : else
7675 3302 : name = (const char *) ostr.text;
7676 : /* And then let get_string_literal translate from SOURCE_CHARSET to
7677 : {UTF-8,exec charset}. */
7678 3302 : tree dchar_type = to_utf8 ? char8_type_node : char_type_node;
7679 3302 : str = get_string_literal (name, dchar_type);
7680 3302 : free (const_cast <unsigned char *> (ostr.text));
7681 3302 : if (str == NULL_TREE)
7682 : {
7683 4 : str = get_string_literal ("", dchar_type);
7684 4 : gcc_assert (str);
7685 : }
7686 3302 : releasing_vec args (make_tree_vector_single (str));
7687 3302 : tree ret = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7688 3302 : &args, TREE_TYPE (call), LOOKUP_NORMAL,
7689 : tf_warning_or_error);
7690 3302 : return build_cplus_new (TREE_TYPE (call), ret, tf_warning_or_error);
7691 3302 : }
7692 :
7693 : /* Helper for eval_extract, extracting a reference type T.
7694 : Returns: If r represents an object O, then a reference to O.
7695 : Otherwise, a reference to the object declared, or referred to, by the
7696 : variable represented by r.
7697 : Throws: meta::exception unless
7698 : -- r represents a variable or object of type U,
7699 : -- is_convertible_v<remove_reference_t<U>(*)[],
7700 : remove_reference_t<T>(*)[]> is true, and
7701 : -- If r represents a variable, then either that variable is usable in
7702 : constant expressions or its lifetime began within the core constant
7703 : expression currently under evaluation. */
7704 :
7705 : static tree
7706 250 : extract_ref (location_t loc, const constexpr_ctx *ctx, tree T, tree r,
7707 : reflect_kind kind, bool *non_constant_p, tree *jump_target,
7708 : tree fun)
7709 : {
7710 718 : auto adjust_type = [](tree type) -> tree
7711 : {
7712 468 : if (TYPE_REF_P (type))
7713 292 : type = TREE_TYPE (type);
7714 468 : if (FUNC_OR_METHOD_TYPE_P (type)
7715 468 : || (TREE_CODE (type) == ARRAY_TYPE
7716 52 : && TYPE_DOMAIN (type) == NULL_TREE))
7717 24 : return error_mark_node;
7718 444 : type = build_cplus_array_type (type, NULL_TREE);
7719 444 : return build_pointer_type (type);
7720 : };
7721 :
7722 250 : const bool var_p = eval_is_variable (r, kind) == boolean_true_node;
7723 320 : if (var_p || eval_is_object (kind) == boolean_true_node)
7724 : {
7725 : /* The wording is saying that U is the type of r. */
7726 234 : tree U = TREE_TYPE (r);
7727 234 : tree adju = adjust_type (U);
7728 234 : tree adjt = adjust_type (T);
7729 234 : if (adju != error_mark_node
7730 222 : && adjt != error_mark_node
7731 222 : && is_convertible (adju, adjt)
7732 448 : && (!var_p
7733 160 : || decl_constant_var_p (r)
7734 108 : || cxx_constexpr_in_eval_lifetime_p (ctx, r)))
7735 : {
7736 146 : if (TYPE_REF_P (TREE_TYPE (r)))
7737 : {
7738 32 : bool nc = false, ovf = false;
7739 32 : tree jt = NULL_TREE;
7740 32 : r = cxx_eval_constant_expression (ctx, convert_from_reference (r),
7741 : vc_glvalue, &nc, &ovf, &jt);
7742 32 : if (nc || jt)
7743 0 : goto throw_;
7744 32 : gcc_checking_assert (!TYPE_P (r));
7745 : }
7746 146 : return build_nop (T, build_address (r));
7747 : }
7748 : }
7749 :
7750 104 : throw_:
7751 104 : return throw_exception (loc, ctx, "value cannot be extracted", fun,
7752 104 : non_constant_p, jump_target);
7753 : }
7754 :
7755 : /* Helper for extract_value. Return true iff we can extract value of
7756 : type U using type T. */
7757 :
7758 : static bool
7759 730 : can_extract_value_p (tree T, tree U)
7760 : {
7761 730 : if (POINTER_TYPE_P (U)
7762 46 : && (similar_type_p (T, U)
7763 16 : || (FUNCTION_POINTER_TYPE_P (T) && FUNCTION_POINTER_TYPE_P (U)))
7764 768 : && is_convertible (U, T))
7765 : return true;
7766 704 : else if (same_type_ignoring_top_level_qualifiers_p (T, U))
7767 : return true;
7768 552 : else if (TREE_CODE (U) == ARRAY_TYPE
7769 382 : && POINTER_TYPE_P (T)
7770 934 : && is_convertible (U, T))
7771 : {
7772 : /* remove_extent_t<U> */
7773 370 : U = TREE_TYPE (U);
7774 370 : U = strip_typedefs (U);
7775 : /* remove_extent_t<U>* */
7776 370 : U = build_pointer_type (U);
7777 370 : return similar_type_p (T, U);
7778 : }
7779 36 : else if (LAMBDA_TYPE_P (U)
7780 14 : && FUNCTION_POINTER_TYPE_P (T)
7781 196 : && is_convertible (U, T))
7782 : return true;
7783 : return false;
7784 : }
7785 :
7786 : /* Helper for eval_extract, extracting a value.
7787 : Let U be the type of the value or object that r represents.
7788 : Returns: static_cast<T>([:R:]), where R is a constant expression of
7789 : type info such that R == r is true.
7790 : Throws: meta::exception unless
7791 : -- U is a pointer type, T and U are either similar or both function pointer
7792 : types, and is_convertible_v<U, T> is true,
7793 : -- U is not a pointer type and the cv-unqualified types of T and U are the
7794 : same,
7795 : -- U is an array type, T is a pointer type, remove_extent_t<U>* and T are
7796 : similar types, and the value r represents is convertible to T, or
7797 : -- U is a closure type, T is a function pointer type, and the value that r
7798 : represents is convertible to T. */
7799 :
7800 : static tree
7801 730 : extract_value (location_t loc, const constexpr_ctx *ctx, tree T, tree r,
7802 : bool *non_constant_p, tree *jump_target, tree fun)
7803 : {
7804 730 : if (REFLECT_EXPR_P (r))
7805 : {
7806 730 : r = REFLECT_EXPR_HANDLE (r);
7807 730 : if (can_extract_value_p (T, TREE_TYPE (r)))
7808 554 : return build_static_cast (loc, T, r, tf_none);
7809 : }
7810 176 : return throw_exception (loc, ctx, "value cannot be extracted", fun,
7811 176 : non_constant_p, jump_target);
7812 : }
7813 :
7814 : /* Helper for extract_member_or_function. Return true iff we can
7815 : extract an NSDM or function R of kind KIND using type T. */
7816 :
7817 : static bool
7818 162 : can_extract_member_or_function_p (tree T, tree r, reflect_kind kind)
7819 : {
7820 162 : if (eval_is_nonstatic_data_member (r) == boolean_true_node)
7821 : {
7822 30 : if (eval_is_bit_field (r, kind) == boolean_true_node)
7823 : return false;
7824 : /* static union { int m; }; extract<int>(^^m); is invalid. */
7825 22 : if (TREE_CODE (r) == FIELD_DECL
7826 22 : && ANON_UNION_TYPE_P (DECL_CONTEXT (r)))
7827 : {
7828 4 : tree c = CP_TYPE_CONTEXT (DECL_CONTEXT (r));
7829 4 : while (ANON_UNION_TYPE_P (c))
7830 0 : c = CP_TYPE_CONTEXT (c);
7831 4 : if (!TYPE_P (c))
7832 : return false;
7833 : }
7834 : /* Create the X C::* type. */
7835 18 : tree type = build_offset_type (CP_DECL_CONTEXT (r), TREE_TYPE (r));
7836 18 : if (similar_type_p (type, T) && is_convertible (type, T))
7837 18 : return true;
7838 : return false;
7839 : }
7840 132 : else if (DECL_IOBJ_MEMBER_FUNCTION_P (r))
7841 : {
7842 86 : tree F = TREE_TYPE (r);
7843 86 : F = build_pointer_type (F);
7844 86 : F = build_ptrmemfunc_type (F);
7845 86 : if (same_type_p (T, F) || fnptr_conv_p (T, F))
7846 34 : return true;
7847 : return false;
7848 : }
7849 46 : else if (TREE_CODE (r) == FUNCTION_DECL)
7850 : {
7851 46 : tree F = TREE_TYPE (r);
7852 46 : F = build_pointer_type (F);
7853 46 : if (same_type_p (T, F) || fnptr_conv_p (T, F))
7854 30 : return true;
7855 : return false;
7856 : }
7857 :
7858 : return false;
7859 : }
7860 :
7861 : /* Helper for eval_extract, extracting a NSDM or function.
7862 : Returns:
7863 : -- If T is a pointer type, then a pointer value pointing to the function
7864 : represented by r.
7865 : -- Otherwise, a pointer-to-member value designating the non-static data
7866 : member or function represented by r.
7867 : Throws: meta::exception unless
7868 : -- r represents a non-static data member with type X, that is not
7869 : a bit-field, that is a direct member of class C, T and X C::*
7870 : are similar types, and is_convertible_v<X C::*, T> is true;
7871 : -- r represents an implicit object member function with type F or
7872 : F noexcept that is a direct member of a class C, and T is F C::*; or
7873 : -- r represents a non-member function, static member function, or
7874 : explicit object member function of function type F or F noexcept, and
7875 : T is F*. */
7876 :
7877 : static tree
7878 162 : extract_member_or_function (location_t loc, const constexpr_ctx *ctx,
7879 : tree T, tree r, reflect_kind kind,
7880 : bool *non_constant_p, tree *jump_target, tree fun)
7881 : {
7882 162 : r = MAYBE_BASELINK_FUNCTIONS (r);
7883 162 : if (!can_extract_member_or_function_p (T, r, kind))
7884 80 : return throw_exception (loc, ctx, "value cannot be extracted", fun,
7885 80 : non_constant_p, jump_target);
7886 :
7887 82 : const tsubst_flags_t complain = complain_flags (ctx);
7888 82 : if (POINTER_TYPE_P (T))
7889 : {
7890 30 : r = cp_build_addr_expr (r, complain);
7891 30 : return perform_implicit_conversion (T, r, complain);
7892 : }
7893 : else
7894 : {
7895 52 : if (!mark_used (r, complain))
7896 : {
7897 0 : *non_constant_p = true;
7898 0 : return NULL_TREE;
7899 : }
7900 52 : r = build_offset_ref (DECL_CONTEXT (r), r, /*address_p=*/true, complain);
7901 52 : r = cp_build_addr_expr (r, complain);
7902 52 : r = cp_convert (T, r, complain);
7903 52 : return r;
7904 : }
7905 : }
7906 :
7907 : /* Process std::meta::extract.
7908 : Let U be remove_cv_t<T>.
7909 : Effects: Equivalent to:
7910 : if constexpr (is_reference_type(^^T)) {
7911 : return extract-ref<T>(r);
7912 : } else if constexpr (is_nonstatic_data_member(r) || is_function(r)) {
7913 : return extract-member-or-function<U>(r);
7914 : } else {
7915 : return extract-value<U>(constant_of(r));
7916 : }
7917 : */
7918 :
7919 : static tree
7920 1158 : eval_extract (location_t loc, const constexpr_ctx *ctx, tree type, tree r,
7921 : reflect_kind kind, bool *non_constant_p, bool *overflow_p,
7922 : tree *jump_target, tree fun)
7923 : {
7924 1158 : if (eval_is_reference_type (loc, ctx, type, non_constant_p, fun)
7925 1158 : == boolean_true_node)
7926 250 : return extract_ref (loc, ctx, type, r, kind, non_constant_p, jump_target,
7927 250 : fun);
7928 908 : type = cv_unqualified (type);
7929 908 : if (eval_is_nonstatic_data_member (r) == boolean_true_node
7930 908 : || eval_is_function (r) == boolean_true_node)
7931 162 : return extract_member_or_function (loc, ctx, type, r, kind, non_constant_p,
7932 162 : jump_target, fun);
7933 : else
7934 : {
7935 746 : r = eval_constant_of (loc, ctx, r, kind, non_constant_p, overflow_p,
7936 : jump_target, fun);
7937 746 : if (*jump_target || *non_constant_p)
7938 : return NULL_TREE;
7939 730 : return extract_value (loc, ctx, type, r, non_constant_p, jump_target,
7940 730 : fun);
7941 : }
7942 : }
7943 :
7944 : /* Diagnose incorrect type of metafn argument and return true in that
7945 : case. */
7946 :
7947 : static bool
7948 9131 : check_metafn_arg_type (location_t loc, metafn_kind_arg kind, int n, tree type,
7949 : bool *non_constant_p)
7950 : {
7951 9131 : tree expected = NULL_TREE;
7952 9131 : const char *expected_str = NULL;
7953 9131 : switch ((metafn_kind_arg) kind)
7954 : {
7955 : case METAFN_KIND_ARG_VOID:
7956 : break;
7957 0 : case METAFN_KIND_ARG_INFO:
7958 0 : case METAFN_KIND_ARG_TINFO:
7959 0 : if (!REFLECTION_TYPE_P (type))
7960 0 : expected = meta_info_type_node;
7961 : break;
7962 : case METAFN_KIND_ARG_REFLECTION_RANGE:
7963 : case METAFN_KIND_ARG_REFLECTION_RANGET:
7964 : case METAFN_KIND_ARG_INPUT_RANGE:
7965 : break;
7966 108 : case METAFN_KIND_ARG_SIZE_T:
7967 108 : if (!same_type_ignoring_top_level_qualifiers_p (type, size_type_node))
7968 9131 : expected_str = "std::size_t";
7969 : break;
7970 248 : case METAFN_KIND_ARG_UNSIGNED:
7971 248 : if (!same_type_ignoring_top_level_qualifiers_p (type,
7972 : unsigned_type_node))
7973 6 : expected = unsigned_type_node;
7974 : break;
7975 298 : case METAFN_KIND_ARG_OPERATORS:
7976 298 : if (TREE_CODE (type) != ENUMERAL_TYPE
7977 292 : || TYPE_PRECISION (type) != TYPE_PRECISION (integer_type_node)
7978 590 : || TYPE_CONTEXT (type) != std_meta_node)
7979 : expected_str = "std::meta::operators";
7980 : break;
7981 6468 : case METAFN_KIND_ARG_ACCESS_CONTEXT:
7982 6468 : if (TYPE_REF_P (type))
7983 0 : type = TREE_TYPE (type);
7984 6468 : if (!is_std_meta_class (type, "access_context"))
7985 0 : expected_str = "std::meta::access_context";
7986 : break;
7987 757 : case METAFN_KIND_ARG_DATA_MEMBER_OPTIONS:
7988 757 : if (TYPE_REF_P (type))
7989 751 : type = TREE_TYPE (type);
7990 757 : if (!is_std_meta_class (type, "data_member_options"))
7991 6 : expected_str = "std::meta::data_member_options";
7992 : break;
7993 : case METAFN_KIND_ARG_TEMPLATE_PARM:
7994 : case METAFN_KIND_ARG_TEMPLATE_PARM_REF:
7995 : break;
7996 : }
7997 9131 : if (expected || expected_str)
7998 : {
7999 24 : if (expected_str)
8000 18 : error_at (loc, "incorrect %qT type of argument %d, expected %qs",
8001 : type, n + 1, expected_str);
8002 : else
8003 6 : error_at (loc, "incorrect %qT type of argument %d, expected %qT",
8004 : type, n + 1, expected);
8005 24 : *non_constant_p = true;
8006 24 : return true;
8007 : }
8008 : return false;
8009 : }
8010 :
8011 : /* Diagnose incorrect return type of metafn and return true in that case. */
8012 :
8013 : static bool
8014 61172 : check_metafn_return_type (location_t loc, metafn_kind_ret kind, tree type,
8015 : bool *non_constant_p)
8016 : {
8017 61172 : tree expected = NULL_TREE;
8018 61172 : const char *expected_str = NULL;
8019 61172 : switch (kind)
8020 : {
8021 32520 : case METAFN_KIND_RET_BOOL:
8022 32520 : if (TREE_CODE (type) != BOOLEAN_TYPE)
8023 6 : expected = boolean_type_node;
8024 : break;
8025 8598 : case METAFN_KIND_RET_INFO:
8026 8598 : if (!REFLECTION_TYPE_P (type))
8027 2 : expected = meta_info_type_node;
8028 : break;
8029 1010 : case METAFN_KIND_RET_SIZE_T:
8030 1010 : if (!same_type_ignoring_top_level_qualifiers_p (type, size_type_node))
8031 61172 : expected_str = "std::size_t";
8032 : break;
8033 252 : case METAFN_KIND_RET_MEMBER_OFFSET:
8034 252 : if (!is_std_meta_class (type, "member_offset"))
8035 61172 : expected_str = "std::meta::member_offset";
8036 : break;
8037 486 : case METAFN_KIND_RET_OPERATORS:
8038 486 : if (TREE_CODE (type) != ENUMERAL_TYPE
8039 480 : || TYPE_PRECISION (type) != TYPE_PRECISION (integer_type_node)
8040 966 : || TYPE_CONTEXT (type) != std_meta_node)
8041 : expected_str = "std::meta::operators";
8042 : break;
8043 126 : case METAFN_KIND_RET_SOURCE_LOCATION:
8044 126 : if (!is_std_class (type, "source_location"))
8045 61172 : expected_str = "std::source_location";
8046 : break;
8047 5280 : case METAFN_KIND_RET_STRING_VIEW:
8048 5280 : if (!CLASS_TYPE_P (type))
8049 : expected_str = "std::string_view";
8050 : break;
8051 3700 : case METAFN_KIND_RET_U8STRING_VIEW:
8052 3700 : if (!CLASS_TYPE_P (type))
8053 : expected_str = "std::u8string_view";
8054 : break;
8055 126 : case METAFN_KIND_RET_STRONG_ORDERING:
8056 126 : if (!is_std_class (type, "strong_ordering"))
8057 61172 : expected_str = "std::strong_ordering";
8058 : break;
8059 7360 : case METAFN_KIND_RET_VECTOR_INFO:
8060 7360 : if (!CLASS_TYPE_P (type))
8061 : expected_str = "std::vector<std::meta::info>";
8062 : break;
8063 554 : case METAFN_KIND_RET_ACCESS_CONTEXT:
8064 554 : if (!is_std_meta_class (type, "access_context"))
8065 61172 : expected_str = "std::meta::access_context";
8066 : break;
8067 : case METAFN_KIND_RET_TEMPLATE_PARM:
8068 : break;
8069 : }
8070 61172 : if (expected || expected_str)
8071 : {
8072 64 : if (expected_str)
8073 56 : error_at (loc, "incorrect %qT return type, expected %qs",
8074 : type, expected_str);
8075 : else
8076 8 : error_at (loc, "incorrect %qT return type, expected %qT",
8077 : type, expected);
8078 64 : *non_constant_p = true;
8079 64 : return true;
8080 : }
8081 : return false;
8082 : }
8083 :
8084 : /* Expand a call to a metafunction FUN. CALL is the CALL_EXPR.
8085 : JUMP_TARGET is set if we are throwing std::meta::exception. */
8086 :
8087 : tree
8088 61172 : process_metafunction (const constexpr_ctx *ctx, tree fun, tree call,
8089 : bool *non_constant_p, bool *overflow_p,
8090 : tree *jump_target)
8091 : {
8092 61172 : tree name = DECL_NAME (fun);
8093 61172 : const char *ident = IDENTIFIER_POINTER (name);
8094 61172 : const location_t loc = cp_expr_loc_or_input_loc (call);
8095 61172 : const metafn_info *minfo
8096 61172 : = metafn_lookup::find (ident, IDENTIFIER_LENGTH (name));
8097 61172 : if (minfo == NULL)
8098 : {
8099 0 : not_found:
8100 0 : error_at (loc, "unknown metafunction %qD", fun);
8101 0 : *non_constant_p = true;
8102 0 : return NULL_TREE;
8103 : }
8104 61172 : tree h = NULL_TREE, h1 = NULL_TREE, hvec = NULL_TREE, expr = NULL_TREE;
8105 61172 : tree type = NULL_TREE, ht, info;
8106 61172 : reflect_kind kind = REFLECT_UNDEF;
8107 61172 : tree rettype;
8108 61172 : if (TREE_CODE (call) == AGGR_INIT_EXPR)
8109 7348 : rettype = TREE_TYPE (AGGR_INIT_EXPR_SLOT (call));
8110 : else
8111 53824 : rettype = TREE_TYPE (call);
8112 61172 : if (check_metafn_return_type (loc, METAFN_KIND_RET (minfo), rettype,
8113 : non_constant_p))
8114 : return NULL_TREE;
8115 : /* Don't call get_vector_of_info_elts (for any metafn which returns
8116 : vector<info>) in a template. */
8117 61108 : if (processing_template_decl
8118 10 : && METAFN_KIND_RET (minfo) == METAFN_KIND_RET_VECTOR_INFO)
8119 : {
8120 4 : *non_constant_p = true;
8121 4 : return NULL_TREE;
8122 : }
8123 241818 : for (int argno = 0; argno < 3; ++argno)
8124 181606 : switch (METAFN_KIND_ARG (minfo, argno))
8125 : {
8126 : case METAFN_KIND_ARG_VOID:
8127 : break;
8128 57920 : case METAFN_KIND_ARG_INFO:
8129 57920 : case METAFN_KIND_ARG_TINFO:
8130 57920 : gcc_assert (argno < 2);
8131 57920 : info = get_info (loc, ctx, call, argno, non_constant_p, overflow_p,
8132 : jump_target);
8133 57920 : if (*jump_target || *non_constant_p)
8134 : return NULL_TREE;
8135 57844 : ht = REFLECT_EXPR_HANDLE (info);
8136 57844 : if (error_operand_p (ht))
8137 : {
8138 2 : *non_constant_p = true;
8139 2 : return NULL_TREE;
8140 : }
8141 57842 : if (METAFN_KIND_ARG (minfo, argno) == METAFN_KIND_ARG_TINFO
8142 57842 : && eval_is_type (ht) != boolean_true_node)
8143 748 : return throw_exception_nontype (loc, ctx, fun, non_constant_p,
8144 748 : jump_target);
8145 57094 : if (argno == 0)
8146 : {
8147 53590 : kind = REFLECT_EXPR_KIND (info);
8148 53590 : h = ht;
8149 : }
8150 : else
8151 : h1 = ht;
8152 : break;
8153 1093 : case METAFN_KIND_ARG_REFLECTION_RANGE:
8154 1093 : gcc_assert (argno == 1);
8155 1093 : hvec = get_info_vec (loc, ctx, call, argno, non_constant_p,
8156 : overflow_p, jump_target, fun);
8157 1093 : if (*jump_target || *non_constant_p)
8158 : return NULL_TREE;
8159 : break;
8160 2458 : case METAFN_KIND_ARG_REFLECTION_RANGET:
8161 2458 : hvec = get_type_info_vec (loc, ctx, call, argno, non_constant_p,
8162 : overflow_p, jump_target, fun);
8163 2458 : if (*jump_target || *non_constant_p)
8164 : return NULL_TREE;
8165 : break;
8166 3932 : case METAFN_KIND_ARG_INPUT_RANGE:
8167 : /* Handled in eval_reflect_constant_*. */
8168 3932 : gcc_assert (argno == 0);
8169 : break;
8170 1252 : case METAFN_KIND_ARG_TEMPLATE_PARM:
8171 1252 : case METAFN_KIND_ARG_TEMPLATE_PARM_REF:
8172 1252 : type = TREE_VEC_ELT (get_template_innermost_arguments (fun), 0);
8173 : /* FALLTHRU */
8174 1658 : case METAFN_KIND_ARG_SIZE_T:
8175 1658 : case METAFN_KIND_ARG_OPERATORS:
8176 1658 : gcc_assert (argno == 0);
8177 1658 : expr = get_nth_callarg (call, 0);
8178 1658 : if (check_metafn_arg_type (loc, METAFN_KIND_ARG (minfo, argno), 0,
8179 1658 : TREE_TYPE (expr), non_constant_p))
8180 : return NULL_TREE;
8181 1646 : expr = cxx_eval_constant_expression (ctx, expr, vc_prvalue,
8182 : non_constant_p, overflow_p,
8183 : jump_target);
8184 1646 : if (*jump_target || *non_constant_p)
8185 : return NULL_TREE;
8186 : break;
8187 7473 : case METAFN_KIND_ARG_UNSIGNED:
8188 7473 : case METAFN_KIND_ARG_ACCESS_CONTEXT:
8189 7473 : case METAFN_KIND_ARG_DATA_MEMBER_OPTIONS:
8190 7473 : gcc_assert (argno == 1);
8191 7473 : expr = get_nth_callarg (call, argno);
8192 7473 : if (check_metafn_arg_type (loc, METAFN_KIND_ARG (minfo, argno), 1,
8193 7473 : TREE_TYPE (expr), non_constant_p))
8194 : return NULL_TREE;
8195 7461 : expr = cxx_eval_constant_expression (ctx, expr, vc_prvalue,
8196 : non_constant_p, overflow_p,
8197 : jump_target);
8198 7461 : if (*jump_target || *non_constant_p)
8199 : return NULL_TREE;
8200 : break;
8201 0 : default:
8202 0 : gcc_unreachable ();
8203 : }
8204 :
8205 : /* Some of the eval_ functions build new trees and in a template
8206 : that could produce template trees. The subsequent evaluation
8207 : cannot process such trees, though, so force a non-template
8208 : context. This assumes that the call is not instantiation-dependent. */
8209 60212 : processing_template_decl_sentinel ptds;
8210 :
8211 60212 : switch (minfo->code)
8212 : {
8213 480 : case METAFN_OPERATOR_OF:
8214 480 : return eval_operator_of (loc, ctx, h, non_constant_p, jump_target,
8215 60212 : rettype, fun);
8216 192 : case METAFN_SYMBOL_OF:
8217 192 : return eval_symbol_of (loc, ctx, expr, non_constant_p, jump_target,
8218 192 : char_type_node, rettype, fun);
8219 100 : case METAFN_U8SYMBOL_OF:
8220 100 : return eval_symbol_of (loc, ctx, expr, non_constant_p, jump_target,
8221 100 : char8_type_node, rettype, fun);
8222 2124 : case METAFN_HAS_IDENTIFIER:
8223 2124 : return eval_has_identifier (h, kind);
8224 4794 : case METAFN_IDENTIFIER_OF:
8225 4794 : return eval_identifier_of (loc, ctx, h, kind, non_constant_p, jump_target,
8226 4794 : char_type_node, rettype, fun);
8227 118 : case METAFN_U8IDENTIFIER_OF:
8228 118 : return eval_identifier_of (loc, ctx, h, kind, non_constant_p, jump_target,
8229 118 : char8_type_node, rettype, fun);
8230 232 : case METAFN_DISPLAY_STRING_OF:
8231 232 : return eval_display_string_of (loc, ctx, h, kind, non_constant_p,
8232 : jump_target, char_type_node,
8233 232 : rettype, fun);
8234 222 : case METAFN_U8DISPLAY_STRING_OF:
8235 222 : return eval_display_string_of (loc, ctx, h, kind, non_constant_p,
8236 : jump_target, char8_type_node,
8237 222 : rettype, fun);
8238 120 : case METAFN_SOURCE_LOCATION_OF:
8239 120 : return eval_source_location_of (loc, h, kind, rettype);
8240 1336 : case METAFN_TYPE_OF:
8241 1336 : return eval_type_of (loc, ctx, h, kind, non_constant_p, jump_target, fun);
8242 156 : case METAFN_OBJECT_OF:
8243 156 : return eval_object_of (loc, ctx, h, kind, non_constant_p, overflow_p,
8244 156 : jump_target, fun);
8245 378 : case METAFN_CONSTANT_OF:
8246 378 : return eval_constant_of (loc, ctx, h, kind, non_constant_p, overflow_p,
8247 378 : jump_target, fun);
8248 172 : case METAFN_IS_PUBLIC:
8249 172 : return eval_is_public (h, kind);
8250 138 : case METAFN_IS_PROTECTED:
8251 138 : return eval_is_protected (h, kind);
8252 128 : case METAFN_IS_PRIVATE:
8253 128 : return eval_is_private (h, kind);
8254 34 : case METAFN_IS_VIRTUAL:
8255 34 : return eval_is_virtual (h, kind);
8256 28 : case METAFN_IS_PURE_VIRTUAL:
8257 28 : return eval_is_pure_virtual (h);
8258 140 : case METAFN_IS_OVERRIDE:
8259 140 : return eval_is_override (h);
8260 40 : case METAFN_IS_FINAL:
8261 40 : return eval_is_final (h);
8262 252 : case METAFN_IS_DELETED:
8263 252 : return eval_is_deleted (h);
8264 718 : case METAFN_IS_DEFAULTED:
8265 718 : return eval_is_defaulted (h);
8266 212 : case METAFN_IS_USER_PROVIDED:
8267 212 : return eval_is_user_provided (h);
8268 212 : case METAFN_IS_USER_DECLARED:
8269 212 : return eval_is_user_declared (h);
8270 44 : case METAFN_IS_EXPLICIT:
8271 44 : return eval_is_explicit (h);
8272 350 : case METAFN_IS_NOEXCEPT:
8273 350 : return eval_is_noexcept (h);
8274 190 : case METAFN_IS_BIT_FIELD:
8275 190 : return eval_is_bit_field (h, kind);
8276 98 : case METAFN_IS_ENUMERATOR:
8277 98 : return eval_is_enumerator (h);
8278 6 : case METAFN_IS_ANNOTATION:
8279 6 : return eval_is_annotation (h, kind);
8280 146 : case METAFN_IS_CONST:
8281 146 : return eval_is_const (h, kind);
8282 110 : case METAFN_IS_VOLATILE:
8283 110 : return eval_is_volatile (h, kind);
8284 128 : case METAFN_IS_MUTABLE_MEMBER:
8285 128 : return eval_is_mutable_member (h);
8286 48 : case METAFN_IS_LVALUE_REFERENCE_QUALIFIED:
8287 48 : return eval_is_lrvalue_reference_qualified (h, kind, /*rvalue_p=*/false);
8288 42 : case METAFN_IS_RVALUE_REFERENCE_QUALIFIED:
8289 42 : return eval_is_lrvalue_reference_qualified (h, kind, /*rvalue_p=*/true);
8290 140 : case METAFN_HAS_STATIC_STORAGE_DURATION:
8291 140 : return eval_has_static_storage_duration (h, kind);
8292 140 : case METAFN_HAS_THREAD_STORAGE_DURATION:
8293 140 : return eval_has_thread_storage_duration (h, kind);
8294 140 : case METAFN_HAS_AUTOMATIC_STORAGE_DURATION:
8295 140 : return eval_has_automatic_storage_duration (h, kind);
8296 126 : case METAFN_HAS_INTERNAL_LINKAGE:
8297 126 : return eval_has_internal_linkage (h, kind);
8298 126 : case METAFN_HAS_MODULE_LINKAGE:
8299 126 : return eval_has_module_linkage (h, kind);
8300 126 : case METAFN_HAS_EXTERNAL_LINKAGE:
8301 126 : return eval_has_external_linkage (h, kind);
8302 132 : case METAFN_HAS_C_LANGUAGE_LINKAGE:
8303 132 : return eval_has_c_language_linkage (h, kind);
8304 124 : case METAFN_HAS_LINKAGE:
8305 124 : return eval_has_linkage (h, kind);
8306 244 : case METAFN_IS_COMPLETE_TYPE:
8307 244 : return eval_is_complete_type (h);
8308 86 : case METAFN_IS_ENUMERABLE_TYPE:
8309 86 : return eval_is_enumerable_type (h);
8310 256 : case METAFN_IS_VARIABLE:
8311 256 : return eval_is_variable (h, kind);
8312 342 : case METAFN_IS_TYPE:
8313 342 : return eval_is_type (h);
8314 172 : case METAFN_IS_NAMESPACE:
8315 172 : return eval_is_namespace (h);
8316 170 : case METAFN_IS_TYPE_ALIAS:
8317 170 : return eval_is_type_alias (h);
8318 166 : case METAFN_IS_NAMESPACE_ALIAS:
8319 166 : return eval_is_namespace_alias (h);
8320 196 : case METAFN_IS_FUNCTION:
8321 196 : return eval_is_function (h);
8322 30 : case METAFN_IS_CONVERSION_FUNCTION:
8323 30 : return eval_is_conversion_function (h);
8324 276 : case METAFN_IS_OPERATOR_FUNCTION:
8325 276 : return eval_is_operator_function (h);
8326 30 : case METAFN_IS_LITERAL_OPERATOR:
8327 30 : return eval_is_literal_operator (h);
8328 1568 : case METAFN_IS_SPECIAL_MEMBER_FUNCTION:
8329 1568 : return eval_is_special_member_function (h);
8330 1212 : case METAFN_IS_CONSTRUCTOR:
8331 1212 : return eval_is_constructor (h);
8332 1352 : case METAFN_IS_DEFAULT_CONSTRUCTOR:
8333 1352 : return eval_is_default_constructor (h);
8334 760 : case METAFN_IS_COPY_CONSTRUCTOR:
8335 760 : return eval_is_copy_constructor (h);
8336 718 : case METAFN_IS_MOVE_CONSTRUCTOR:
8337 718 : return eval_is_move_constructor (h);
8338 20 : case METAFN_IS_ASSIGNMENT:
8339 20 : return eval_is_assignment (h);
8340 764 : case METAFN_IS_COPY_ASSIGNMENT:
8341 764 : return eval_is_copy_assignment (h);
8342 752 : case METAFN_IS_MOVE_ASSIGNMENT:
8343 752 : return eval_is_move_assignment (h);
8344 2384 : case METAFN_IS_DESTRUCTOR:
8345 2384 : return eval_is_destructor (h);
8346 116 : case METAFN_IS_FUNCTION_PARAMETER:
8347 116 : return eval_is_function_parameter (h, kind);
8348 148 : case METAFN_IS_EXPLICIT_OBJECT_PARAMETER:
8349 148 : return eval_is_explicit_object_parameter (h, kind);
8350 152 : case METAFN_HAS_DEFAULT_ARGUMENT:
8351 152 : return eval_has_default_argument (h, kind);
8352 144 : case METAFN_IS_VARARG_FUNCTION:
8353 144 : return eval_is_vararg_function (h);
8354 112 : case METAFN_IS_TEMPLATE:
8355 112 : return eval_is_template (h);
8356 112 : case METAFN_IS_FUNCTION_TEMPLATE:
8357 112 : return eval_is_function_template (h);
8358 94 : case METAFN_IS_VARIABLE_TEMPLATE:
8359 94 : return eval_is_variable_template (h);
8360 94 : case METAFN_IS_CLASS_TEMPLATE:
8361 94 : return eval_is_class_template (h);
8362 94 : case METAFN_IS_ALIAS_TEMPLATE:
8363 94 : return eval_is_alias_template (h);
8364 38 : case METAFN_IS_CONVERSION_FUNCTION_TEMPLATE:
8365 38 : return eval_is_conversion_function_template (h);
8366 36 : case METAFN_IS_OPERATOR_FUNCTION_TEMPLATE:
8367 36 : return eval_is_operator_function_template (h);
8368 30 : case METAFN_IS_LITERAL_OPERATOR_TEMPLATE:
8369 30 : return eval_is_literal_operator_template (h);
8370 88 : case METAFN_IS_CONSTRUCTOR_TEMPLATE:
8371 88 : return eval_is_constructor_template (h);
8372 158 : case METAFN_IS_CONCEPT:
8373 158 : return eval_is_concept (h);
8374 138 : case METAFN_IS_VALUE:
8375 138 : return eval_is_value (kind);
8376 270 : case METAFN_IS_OBJECT:
8377 270 : return eval_is_object (kind);
8378 70 : case METAFN_IS_STRUCTURED_BINDING:
8379 70 : return eval_is_structured_binding (h, kind);
8380 124 : case METAFN_IS_CLASS_MEMBER:
8381 124 : return eval_is_class_member (h);
8382 142 : case METAFN_IS_NAMESPACE_MEMBER:
8383 142 : return eval_is_namespace_member (h);
8384 124 : case METAFN_IS_NONSTATIC_DATA_MEMBER:
8385 124 : return eval_is_nonstatic_data_member (h);
8386 136 : case METAFN_IS_STATIC_MEMBER:
8387 136 : return eval_is_static_member (h);
8388 30 : case METAFN_IS_BASE:
8389 30 : return eval_is_base (h, kind);
8390 128 : case METAFN_HAS_DEFAULT_MEMBER_INITIALIZER:
8391 128 : return eval_has_default_member_initializer (h);
8392 114 : case METAFN_HAS_PARENT:
8393 114 : return eval_has_parent (h, kind);
8394 842 : case METAFN_PARENT_OF:
8395 842 : return eval_parent_of (loc, ctx, h, kind, non_constant_p, jump_target,
8396 842 : fun);
8397 198 : case METAFN_DEALIAS:
8398 198 : return eval_dealias (loc, h, kind);
8399 192 : case METAFN_HAS_TEMPLATE_ARGUMENTS:
8400 192 : return eval_has_template_arguments (h);
8401 78 : case METAFN_TEMPLATE_OF:
8402 78 : return eval_template_of (loc, ctx, h, non_constant_p, jump_target, fun);
8403 160 : case METAFN_TEMPLATE_ARGUMENTS_OF:
8404 160 : return eval_template_arguments_of (loc, ctx, h, non_constant_p,
8405 160 : jump_target, fun);
8406 702 : case METAFN_PARAMETERS_OF:
8407 702 : return eval_parameters_of (loc, ctx, h, non_constant_p, jump_target,
8408 702 : fun);
8409 240 : case METAFN_VARIABLE_OF:
8410 240 : return eval_variable_of (loc, ctx, h, kind, non_constant_p, jump_target,
8411 240 : fun);
8412 126 : case METAFN_RETURN_TYPE_OF:
8413 126 : return eval_return_type_of (loc, ctx, h, kind, non_constant_p,
8414 126 : jump_target, fun);
8415 836 : case METAFN_IS_ACCESSIBLE:
8416 836 : return eval_is_accessible (loc, ctx, h, kind, expr, call,
8417 836 : non_constant_p, jump_target, fun);
8418 30 : case METAFN_HAS_INACCESSIBLE_NONSTATIC_DATA_MEMBERS:
8419 30 : return eval_has_inaccessible_nonstatic_data_members (loc, ctx, h, expr,
8420 : call,
8421 : non_constant_p,
8422 30 : jump_target, fun);
8423 24 : case METAFN_HAS_INACCESSIBLE_BASES:
8424 24 : return eval_has_inaccessible_bases (loc, ctx, h, expr, call,
8425 24 : non_constant_p, jump_target, fun);
8426 38 : case METAFN_HAS_INACCESSIBLE_SUBOBJECTS:
8427 38 : return eval_has_inaccessible_subobjects (loc, ctx, h, expr, call,
8428 : non_constant_p, jump_target,
8429 38 : fun);
8430 128 : case METAFN_CURRENT_FUNCTION:
8431 128 : return eval_current_function (loc, ctx, call, non_constant_p,
8432 128 : jump_target, fun);
8433 136 : case METAFN_CURRENT_CLASS:
8434 136 : return eval_current_class (loc, ctx, call, non_constant_p,
8435 136 : jump_target, fun);
8436 68 : case METAFN_CURRENT_NAMESPACE:
8437 68 : return eval_current_namespace (loc, ctx, call, non_constant_p);
8438 3940 : case METAFN_MEMBERS_OF:
8439 3940 : return eval_members_of (loc, ctx, h, expr, call, non_constant_p,
8440 3940 : jump_target, fun);
8441 1010 : case METAFN_BASES_OF:
8442 1010 : return eval_bases_of (loc, ctx, h, expr, call, non_constant_p,
8443 1010 : jump_target, fun);
8444 96 : case METAFN_STATIC_DATA_MEMBERS_OF:
8445 96 : return eval_static_data_members_of (loc, ctx, h, expr, call,
8446 : non_constant_p, jump_target,
8447 96 : fun);
8448 284 : case METAFN_NONSTATIC_DATA_MEMBERS_OF:
8449 284 : return eval_nonstatic_data_members_of (loc, ctx, h, expr, call,
8450 : non_constant_p, jump_target,
8451 284 : fun);
8452 210 : case METAFN_SUBOBJECTS_OF:
8453 210 : return eval_subobjects_of (loc, ctx, h, expr, call, non_constant_p,
8454 210 : jump_target, fun);
8455 266 : case METAFN_ENUMERATORS_OF:
8456 266 : return eval_enumerators_of (loc, ctx, h, non_constant_p, jump_target,
8457 266 : fun);
8458 246 : case METAFN_OFFSET_OF:
8459 246 : return eval_offset_of (loc, ctx, h, kind, rettype,
8460 246 : non_constant_p, jump_target, fun);
8461 210 : case METAFN_SIZE_OF:
8462 210 : return eval_size_of (loc, ctx, h, kind, rettype, non_constant_p,
8463 210 : jump_target, fun);
8464 210 : case METAFN_ALIGNMENT_OF:
8465 210 : return eval_alignment_of (loc, ctx, h, kind, rettype,
8466 210 : non_constant_p, jump_target, fun);
8467 200 : case METAFN_BIT_SIZE_OF:
8468 200 : return eval_bit_size_of (loc, ctx, h, kind, rettype,
8469 200 : non_constant_p, jump_target, fun);
8470 1158 : case METAFN_EXTRACT:
8471 1158 : {
8472 1158 : type = TREE_VEC_ELT (get_template_innermost_arguments (fun), 0);
8473 1158 : return eval_extract (loc, ctx, type, h, kind, non_constant_p,
8474 1158 : overflow_p, jump_target, fun);
8475 : }
8476 298 : case METAFN_CAN_SUBSTITUTE:
8477 298 : return eval_can_substitute (loc, ctx, h, hvec, non_constant_p,
8478 298 : jump_target, fun);
8479 594 : case METAFN_SUBSTITUTE:
8480 594 : return eval_substitute (loc, ctx, h, hvec, non_constant_p, jump_target,
8481 594 : fun);
8482 934 : case METAFN_REFLECT_CONSTANT:
8483 934 : return eval_reflect_constant (loc, ctx, type, expr, non_constant_p,
8484 934 : jump_target, fun);
8485 256 : case METAFN_REFLECT_OBJECT:
8486 256 : return eval_reflect_object (loc, ctx, type, expr, non_constant_p,
8487 256 : jump_target, fun);
8488 58 : case METAFN_REFLECT_FUNCTION:
8489 58 : return eval_reflect_function (loc, ctx, type, expr, non_constant_p,
8490 58 : jump_target, fun);
8491 280 : case METAFN_REFLECT_CONSTANT_STRING:
8492 280 : return eval_reflect_constant_string (loc, ctx, call, non_constant_p,
8493 280 : overflow_p, jump_target, fun);
8494 350 : case METAFN_REFLECT_CONSTANT_ARRAY:
8495 350 : return eval_reflect_constant_array (loc, ctx, call, non_constant_p,
8496 350 : overflow_p, jump_target, fun);
8497 751 : case METAFN_DATA_MEMBER_SPEC:
8498 751 : return eval_data_member_spec (loc, ctx, h, expr, non_constant_p,
8499 751 : overflow_p, jump_target, fun);
8500 108 : case METAFN_IS_DATA_MEMBER_SPEC:
8501 108 : return eval_is_data_member_spec (h, kind);
8502 195 : case METAFN_DEFINE_AGGREGATE:
8503 195 : return eval_define_aggregate (loc, ctx, h, hvec, call, non_constant_p);
8504 58 : case METAFN_IS_VOID_TYPE:
8505 58 : return eval_is_void_type (h);
8506 52 : case METAFN_IS_NULL_POINTER_TYPE:
8507 52 : return eval_is_null_pointer_type (h);
8508 308 : case METAFN_IS_INTEGRAL_TYPE:
8509 308 : return eval_is_integral_type (h);
8510 56 : case METAFN_IS_FLOATING_POINT_TYPE:
8511 56 : return eval_is_floating_point_type (h);
8512 220 : case METAFN_IS_ARRAY_TYPE:
8513 220 : return eval_is_array_type (loc, ctx, h, non_constant_p, fun);
8514 56 : case METAFN_IS_POINTER_TYPE:
8515 56 : return eval_is_pointer_type (loc, ctx, h, non_constant_p, fun);
8516 52 : case METAFN_IS_LVALUE_REFERENCE_TYPE:
8517 52 : return eval_is_lvalue_reference_type (h);
8518 52 : case METAFN_IS_RVALUE_REFERENCE_TYPE:
8519 52 : return eval_is_rvalue_reference_type (h);
8520 52 : case METAFN_IS_MEMBER_OBJECT_POINTER_TYPE:
8521 52 : return eval_is_member_object_pointer_type (loc, ctx, h, non_constant_p,
8522 52 : fun);
8523 52 : case METAFN_IS_MEMBER_FUNCTION_POINTER_TYPE:
8524 52 : return eval_is_member_function_pointer_type (loc, ctx, h, non_constant_p,
8525 52 : fun);
8526 52 : case METAFN_IS_ENUM_TYPE:
8527 52 : return eval_is_enum_type (loc, ctx, h, non_constant_p, fun);
8528 84 : case METAFN_IS_UNION_TYPE:
8529 84 : return eval_is_union_type (loc, ctx, h, non_constant_p, fun);
8530 220 : case METAFN_IS_CLASS_TYPE:
8531 220 : return eval_is_class_type (loc, ctx, h, non_constant_p, fun);
8532 80 : case METAFN_IS_FUNCTION_TYPE:
8533 80 : return eval_is_function_type (h);
8534 52 : case METAFN_IS_REFLECTION_TYPE:
8535 52 : return eval_is_reflection_type (h);
8536 52 : case METAFN_IS_REFERENCE_TYPE:
8537 52 : return eval_is_reference_type (loc, ctx, h, non_constant_p, fun);
8538 56 : case METAFN_IS_ARITHMETIC_TYPE:
8539 56 : return eval_is_arithmetic_type (h);
8540 56 : case METAFN_IS_FUNDAMENTAL_TYPE:
8541 56 : return eval_is_fundamental_type (h);
8542 56 : case METAFN_IS_OBJECT_TYPE:
8543 56 : return eval_is_object_type (loc, ctx, h, non_constant_p, fun);
8544 62 : case METAFN_IS_SCALAR_TYPE:
8545 62 : return eval_is_scalar_type (h);
8546 56 : case METAFN_IS_COMPOUND_TYPE:
8547 56 : return eval_is_compound_type (h);
8548 56 : case METAFN_IS_MEMBER_POINTER_TYPE:
8549 56 : return eval_is_member_pointer_type (loc, ctx, h, non_constant_p, fun);
8550 16 : case METAFN_IS_CONST_TYPE:
8551 16 : return eval_is_const_type (h);
8552 16 : case METAFN_IS_VOLATILE_TYPE:
8553 16 : return eval_is_volatile_type (h);
8554 112 : case METAFN_IS_TRIVIALLY_COPYABLE_TYPE:
8555 112 : return eval_is_trivially_copyable_type (loc, ctx, h, non_constant_p,
8556 112 : fun);
8557 38 : case METAFN_IS_STANDARD_LAYOUT_TYPE:
8558 38 : return eval_is_standard_layout_type (loc, ctx, h, non_constant_p, fun);
8559 44 : case METAFN_IS_EMPTY_TYPE:
8560 44 : return eval_is_empty_type (loc, ctx, h, non_constant_p, fun);
8561 34 : case METAFN_IS_POLYMORPHIC_TYPE:
8562 34 : return eval_is_polymorphic_type (loc, ctx, h, non_constant_p, fun);
8563 24 : case METAFN_IS_ABSTRACT_TYPE:
8564 24 : return eval_is_abstract_type (loc, ctx, h, non_constant_p, fun);
8565 24 : case METAFN_IS_FINAL_TYPE:
8566 24 : return eval_is_final_type (loc, ctx, h, non_constant_p, fun);
8567 70 : case METAFN_IS_AGGREGATE_TYPE:
8568 70 : return eval_is_aggregate_type (loc, ctx, h, non_constant_p, fun);
8569 86 : case METAFN_IS_STRUCTURAL_TYPE:
8570 86 : return eval_is_structural_type (loc, ctx, h, non_constant_p, fun);
8571 34 : case METAFN_IS_SIGNED_TYPE:
8572 34 : return eval_is_signed_type (h);
8573 34 : case METAFN_IS_UNSIGNED_TYPE:
8574 34 : return eval_is_unsigned_type (h);
8575 36 : case METAFN_IS_BOUNDED_ARRAY_TYPE:
8576 36 : return eval_is_bounded_array_type (loc, ctx, h, non_constant_p, fun);
8577 42 : case METAFN_IS_UNBOUNDED_ARRAY_TYPE:
8578 42 : return eval_is_unbounded_array_type (h);
8579 36 : case METAFN_IS_SCOPED_ENUM_TYPE:
8580 60248 : return eval_is_scoped_enum_type (h);
8581 1302 : case METAFN_IS_CONSTRUCTIBLE_TYPE:
8582 1302 : return eval_is_constructible_type (loc, ctx, h, hvec, non_constant_p,
8583 1302 : fun);
8584 288 : case METAFN_IS_DEFAULT_CONSTRUCTIBLE_TYPE:
8585 288 : return eval_is_default_constructible_type (loc, ctx, h, non_constant_p,
8586 288 : fun);
8587 100 : case METAFN_IS_COPY_CONSTRUCTIBLE_TYPE:
8588 100 : return eval_is_copy_constructible_type (loc, ctx, h, non_constant_p, fun);
8589 80 : case METAFN_IS_MOVE_CONSTRUCTIBLE_TYPE:
8590 80 : return eval_is_move_constructible_type (loc, ctx, h, non_constant_p, fun);
8591 1030 : case METAFN_IS_ASSIGNABLE_TYPE:
8592 1030 : return eval_is_assignable_type (loc, ctx, h, h1, non_constant_p, fun);
8593 80 : case METAFN_IS_COPY_ASSIGNABLE_TYPE:
8594 80 : return eval_is_copy_assignable_type (loc, ctx, h, non_constant_p, fun);
8595 60 : case METAFN_IS_MOVE_ASSIGNABLE_TYPE:
8596 60 : return eval_is_move_assignable_type (loc, ctx, h, non_constant_p, fun);
8597 62 : case METAFN_IS_SWAPPABLE_WITH_TYPE:
8598 62 : return eval_is_swappable_with_type (loc, ctx, h, h1, call,
8599 : non_constant_p, jump_target, fun,
8600 62 : "is_swappable_with");
8601 196 : case METAFN_IS_SWAPPABLE_TYPE:
8602 196 : return eval_is_swappable_type (loc, ctx, h, call, non_constant_p,
8603 196 : jump_target, fun, "is_swappable");
8604 206 : case METAFN_IS_DESTRUCTIBLE_TYPE:
8605 206 : return eval_is_destructible_type (loc, ctx, h, non_constant_p, fun);
8606 166 : case METAFN_IS_TRIVIALLY_CONSTRUCTIBLE_TYPE:
8607 166 : return eval_is_trivially_constructible_type (loc, ctx, h, hvec,
8608 166 : non_constant_p, fun);
8609 98 : case METAFN_IS_TRIVIALLY_DEFAULT_CONSTRUCTIBLE_TYPE:
8610 98 : return eval_is_trivially_default_constructible_type (loc, ctx, h,
8611 : non_constant_p,
8612 98 : fun);
8613 78 : case METAFN_IS_TRIVIALLY_COPY_CONSTRUCTIBLE_TYPE:
8614 78 : return eval_is_trivially_copy_constructible_type (loc, ctx, h,
8615 78 : non_constant_p, fun);
8616 62 : case METAFN_IS_TRIVIALLY_MOVE_CONSTRUCTIBLE_TYPE:
8617 62 : return eval_is_trivially_move_constructible_type (loc, ctx, h,
8618 62 : non_constant_p, fun);
8619 128 : case METAFN_IS_TRIVIALLY_ASSIGNABLE_TYPE:
8620 128 : return eval_is_trivially_assignable_type (loc, ctx, h, h1,
8621 128 : non_constant_p, fun);
8622 72 : case METAFN_IS_TRIVIALLY_COPY_ASSIGNABLE_TYPE:
8623 72 : return eval_is_trivially_copy_assignable_type (loc, ctx, h,
8624 72 : non_constant_p, fun);
8625 56 : case METAFN_IS_TRIVIALLY_MOVE_ASSIGNABLE_TYPE:
8626 56 : return eval_is_trivially_move_assignable_type (loc, ctx, h,
8627 56 : non_constant_p, fun);
8628 70 : case METAFN_IS_TRIVIALLY_DESTRUCTIBLE_TYPE:
8629 70 : return eval_is_trivially_destructible_type (loc, ctx, h, non_constant_p,
8630 70 : fun);
8631 158 : case METAFN_IS_NOTHROW_CONSTRUCTIBLE_TYPE:
8632 158 : return eval_is_nothrow_constructible_type (loc, ctx, h, hvec,
8633 158 : non_constant_p, fun);
8634 70 : case METAFN_IS_NOTHROW_DEFAULT_CONSTRUCTIBLE_TYPE:
8635 70 : return eval_is_nothrow_default_constructible_type (loc, ctx, h,
8636 70 : non_constant_p, fun);
8637 96 : case METAFN_IS_NOTHROW_COPY_CONSTRUCTIBLE_TYPE:
8638 96 : return eval_is_nothrow_copy_constructible_type (loc, ctx, h,
8639 96 : non_constant_p, fun);
8640 80 : case METAFN_IS_NOTHROW_MOVE_CONSTRUCTIBLE_TYPE:
8641 80 : return eval_is_nothrow_move_constructible_type (loc, ctx, h,
8642 80 : non_constant_p, fun);
8643 40 : case METAFN_IS_NOTHROW_ASSIGNABLE_TYPE:
8644 40 : return eval_is_nothrow_assignable_type (loc, ctx, h, h1, non_constant_p,
8645 40 : fun);
8646 74 : case METAFN_IS_NOTHROW_COPY_ASSIGNABLE_TYPE:
8647 74 : return eval_is_nothrow_copy_assignable_type (loc, ctx, h, non_constant_p,
8648 74 : fun);
8649 58 : case METAFN_IS_NOTHROW_MOVE_ASSIGNABLE_TYPE:
8650 58 : return eval_is_nothrow_move_assignable_type (loc, ctx, h, non_constant_p,
8651 58 : fun);
8652 66 : case METAFN_IS_NOTHROW_SWAPPABLE_WITH_TYPE:
8653 66 : return eval_is_swappable_with_type (loc, ctx, h, h1, call,
8654 : non_constant_p, jump_target, fun,
8655 66 : "is_nothrow_swappable_with");
8656 186 : case METAFN_IS_NOTHROW_SWAPPABLE_TYPE:
8657 186 : return eval_is_swappable_type (loc, ctx, h, call, non_constant_p,
8658 186 : jump_target, fun, "is_nothrow_swappable");
8659 184 : case METAFN_IS_NOTHROW_DESTRUCTIBLE_TYPE:
8660 184 : return eval_is_nothrow_destructible_type (loc, ctx, h, non_constant_p,
8661 184 : fun);
8662 168 : case METAFN_IS_IMPLICIT_LIFETIME_TYPE:
8663 168 : return eval_is_implicit_lifetime_type (loc, ctx, h, non_constant_p, fun);
8664 38 : case METAFN_HAS_VIRTUAL_DESTRUCTOR:
8665 38 : return eval_has_virtual_destructor (loc, ctx, h, non_constant_p, fun);
8666 100 : case METAFN_HAS_UNIQUE_OBJECT_REPRESENTATIONS:
8667 100 : return eval_has_unique_object_representations (loc, ctx, h,
8668 100 : non_constant_p, fun);
8669 82 : case METAFN_REFERENCE_CONSTRUCTS_FROM_TEMPORARY:
8670 82 : return eval_reference_constructs_from_temporary (loc, ctx, h, h1,
8671 82 : non_constant_p, fun);
8672 82 : case METAFN_REFERENCE_CONVERTS_FROM_TEMPORARY:
8673 82 : return eval_reference_converts_from_temporary (loc, ctx, h, h1,
8674 82 : non_constant_p, fun);
8675 30 : case METAFN_RANK:
8676 30 : return eval_rank (h);
8677 242 : case METAFN_EXTENT:
8678 242 : return eval_extent (loc, ctx, h, expr, non_constant_p, fun);
8679 220 : case METAFN_IS_SAME_TYPE:
8680 220 : return eval_is_same_type (loc, ctx, h, h1, non_constant_p, fun);
8681 36 : case METAFN_IS_BASE_OF_TYPE:
8682 36 : return eval_is_base_of_type (loc, ctx, h, h1, non_constant_p, fun);
8683 66 : case METAFN_IS_VIRTUAL_BASE_OF_TYPE:
8684 66 : return eval_is_virtual_base_of_type (loc, ctx, h, h1, non_constant_p,
8685 66 : fun);
8686 50 : case METAFN_IS_CONVERTIBLE_TYPE:
8687 50 : return eval_is_convertible_type (loc, ctx, h, h1, non_constant_p, fun);
8688 54 : case METAFN_IS_NOTHROW_CONVERTIBLE_TYPE:
8689 54 : return eval_is_nothrow_convertible_type (loc, ctx, h, h1, non_constant_p,
8690 54 : fun);
8691 56 : case METAFN_IS_LAYOUT_COMPATIBLE_TYPE:
8692 56 : return eval_is_layout_compatible_type (loc, ctx, h, h1, non_constant_p,
8693 56 : fun);
8694 50 : case METAFN_IS_POINTER_INTERCONVERTIBLE_BASE_OF_TYPE:
8695 50 : return eval_is_pointer_interconvertible_base_of_type (loc, ctx, h, h1,
8696 : non_constant_p,
8697 50 : fun);
8698 134 : case METAFN_IS_INVOCABLE_TYPE:
8699 134 : return eval_is_invocable_type (loc, ctx, h, hvec, non_constant_p, fun);
8700 174 : case METAFN_IS_INVOCABLE_R_TYPE:
8701 174 : return eval_is_invocable_r_type (loc, ctx, h, h1, hvec, call,
8702 : non_constant_p, jump_target, fun,
8703 174 : "is_invocable_r");
8704 114 : case METAFN_IS_NOTHROW_INVOCABLE_TYPE:
8705 114 : return eval_is_nothrow_invocable_type (loc, ctx, h, hvec, non_constant_p,
8706 114 : fun);
8707 116 : case METAFN_IS_NOTHROW_INVOCABLE_R_TYPE:
8708 116 : return eval_is_invocable_r_type (loc, ctx, h, h1, hvec, call,
8709 : non_constant_p, jump_target, fun,
8710 116 : "is_nothrow_invocable_r");
8711 22 : case METAFN_REMOVE_CONST:
8712 22 : return eval_remove_const (loc, h);
8713 24 : case METAFN_REMOVE_VOLATILE:
8714 24 : return eval_remove_volatile (loc, h);
8715 22 : case METAFN_REMOVE_CV:
8716 22 : return eval_remove_cv (loc, h);
8717 26 : case METAFN_ADD_CONST:
8718 26 : return eval_add_const (loc, h);
8719 26 : case METAFN_ADD_VOLATILE:
8720 26 : return eval_add_volatile (loc, h);
8721 26 : case METAFN_ADD_CV:
8722 26 : return eval_add_cv (loc, h);
8723 36 : case METAFN_REMOVE_REFERENCE:
8724 36 : return eval_remove_reference (loc, h);
8725 32 : case METAFN_ADD_LVALUE_REFERENCE:
8726 32 : return eval_add_lvalue_reference (loc, h);
8727 30 : case METAFN_ADD_RVALUE_REFERENCE:
8728 30 : return eval_add_rvalue_reference (loc, h);
8729 50 : case METAFN_MAKE_SIGNED:
8730 50 : return eval_make_signed (loc, ctx, h, false, non_constant_p, jump_target,
8731 50 : fun);
8732 50 : case METAFN_MAKE_UNSIGNED:
8733 50 : return eval_make_signed (loc, ctx, h, true, non_constant_p, jump_target,
8734 50 : fun);
8735 26 : case METAFN_REMOVE_EXTENT:
8736 26 : return eval_remove_extent (loc, h);
8737 22 : case METAFN_REMOVE_ALL_EXTENTS:
8738 22 : return eval_remove_all_extents (loc, h);
8739 26 : case METAFN_REMOVE_POINTER:
8740 26 : return eval_remove_pointer (loc, h);
8741 34 : case METAFN_ADD_POINTER:
8742 34 : return eval_add_pointer (loc, h);
8743 34 : case METAFN_REMOVE_CVREF:
8744 34 : return eval_remove_cvref (loc, h);
8745 42 : case METAFN_DECAY:
8746 42 : return eval_decay (loc, h);
8747 178 : case METAFN_COMMON_TYPE:
8748 178 : return eval_common_type (loc, ctx, hvec, call, non_constant_p,
8749 178 : jump_target, fun, ident);
8750 64 : case METAFN_COMMON_REFERENCE:
8751 64 : return eval_common_type (loc, ctx, hvec, call, non_constant_p,
8752 64 : jump_target, fun, ident);
8753 24 : case METAFN_UNDERLYING_TYPE:
8754 24 : return eval_underlying_type (loc, ctx, h, non_constant_p, jump_target,
8755 24 : fun);
8756 20 : case METAFN_INVOKE_RESULT:
8757 20 : return eval_invoke_result (loc, ctx, h, hvec, call, non_constant_p,
8758 20 : jump_target, fun);
8759 28 : case METAFN_UNWRAP_REFERENCE:
8760 28 : return eval_unwrap_reference (loc, ctx, h, call, non_constant_p,
8761 28 : jump_target, fun, ident);
8762 24 : case METAFN_UNWRAP_REF_DECAY:
8763 24 : return eval_unwrap_reference (loc, ctx, h, call, non_constant_p,
8764 24 : jump_target, fun, ident);
8765 56 : case METAFN_TUPLE_SIZE:
8766 56 : return eval_tuple_size (loc, ctx, h, call, non_constant_p, jump_target,
8767 56 : fun);
8768 62 : case METAFN_TUPLE_ELEMENT:
8769 62 : return eval_tuple_element (loc, ctx, expr, h1, call,
8770 62 : non_constant_p, jump_target, fun);
8771 368 : case METAFN_IS_APPLICABLE_TYPE:
8772 368 : return eval_is_applicable_type (loc, ctx, h, h1, call,
8773 : non_constant_p, jump_target, fun,
8774 368 : "is_applicable");
8775 368 : case METAFN_IS_NOTHROW_APPLICABLE_TYPE:
8776 368 : return eval_is_applicable_type (loc, ctx, h, h1, call,
8777 : non_constant_p, jump_target, fun,
8778 368 : "is_nothrow_applicable");
8779 200 : case METAFN_APPLY_RESULT:
8780 200 : return eval_apply_result (loc, ctx, h, h1, call,
8781 200 : non_constant_p, jump_target, fun);
8782 20 : case METAFN_VARIANT_SIZE:
8783 20 : return eval_variant_size (loc, ctx, h, call, non_constant_p,
8784 20 : jump_target, fun);
8785 34 : case METAFN_VARIANT_ALTERNATIVE:
8786 34 : return eval_variant_alternative (loc, ctx, expr, h1, call,
8787 34 : non_constant_p, jump_target, fun);
8788 116 : case METAFN_TYPE_ORDER:
8789 116 : return eval_type_order (h, h1);
8790 604 : case METAFN_ANNOTATIONS_OF:
8791 604 : return eval_annotations_of (loc, ctx, h, kind, NULL_TREE, non_constant_p,
8792 604 : jump_target, fun);
8793 40 : case METAFN_ANNOTATIONS_OF_WITH_TYPE:
8794 40 : return eval_annotations_of (loc, ctx, h, kind, h1, non_constant_p,
8795 40 : jump_target, fun);
8796 : /* Special metafunctions. */
8797 548 : case METAFN_ACCESS_CONTEXT_CURRENT:
8798 1096 : if (DECL_CLASS_SCOPE_P (fun)
8799 548 : && TYPE_NAME (DECL_CONTEXT (fun))
8800 548 : && TREE_CODE (TYPE_NAME (DECL_CONTEXT (fun))) == TYPE_DECL
8801 548 : && DECL_NAME (TYPE_NAME (DECL_CONTEXT (fun)))
8802 1096 : && id_equal (DECL_NAME (TYPE_NAME (DECL_CONTEXT (fun))),
8803 : "access_context"))
8804 548 : return eval_access_context_current (loc, ctx, call, non_constant_p);
8805 0 : goto not_found;
8806 3302 : case METAFN_EXCEPTION__S_EXCEPTION_CVT_TO_UTF8:
8807 3302 : case METAFN_EXCEPTION__S_EXCEPTION_CVT_FROM_UTF8:
8808 6604 : if (DECL_CLASS_SCOPE_P (fun)
8809 3302 : && TYPE_NAME (DECL_CONTEXT (fun))
8810 3302 : && TREE_CODE (TYPE_NAME (DECL_CONTEXT (fun))) == TYPE_DECL
8811 3302 : && DECL_NAME (TYPE_NAME (DECL_CONTEXT (fun)))
8812 6604 : && id_equal (DECL_NAME (TYPE_NAME (DECL_CONTEXT (fun))),
8813 : "exception"))
8814 : {
8815 3302 : bool to_utf8
8816 : = minfo->code == METAFN_EXCEPTION__S_EXCEPTION_CVT_TO_UTF8;
8817 3302 : return eval_exception__S_exception_cvt_tofrom_utf8 (loc, ctx, call,
8818 : non_constant_p,
8819 : overflow_p,
8820 : jump_target,
8821 3302 : fun, to_utf8);
8822 : }
8823 0 : goto not_found;
8824 : }
8825 0 : goto not_found;
8826 : }
8827 :
8828 : /* Splice reflection REFL; i.e., return its entity. */
8829 :
8830 : tree
8831 6831 : splice (tree refl)
8832 : {
8833 6831 : if (refl == error_mark_node)
8834 : return error_mark_node;
8835 :
8836 : /* Who in the world am I? That's the great puzzle and we have to wait
8837 : until instantiation to find out. */
8838 6803 : if (instantiation_dependent_expression_p (refl))
8839 1614 : return build_nt (SPLICE_EXPR, refl);
8840 :
8841 : /* [basic.splice] "The constant-expression of a splice-specifier shall
8842 : be a converted constant expression of type std::meta::info." */
8843 5189 : refl = build_converted_constant_expr (meta_info_type_node, refl,
8844 : tf_warning_or_error);
8845 :
8846 5189 : if (processing_template_decl)
8847 920 : refl = fold_non_dependent_expr (refl, tf_warning_or_error, true);
8848 : else
8849 4269 : refl = cxx_constant_value (refl);
8850 5189 : if (refl == error_mark_node)
8851 : {
8852 14 : gcc_checking_assert (seen_error ());
8853 14 : return error_mark_node;
8854 : }
8855 5175 : location_t loc = cp_expr_loc_or_input_loc (refl);
8856 5175 : if (!REFLECT_EXPR_P (refl))
8857 : {
8858 2 : error_at (loc, "splice argument must be an "
8859 : "expression of type %qs", "std::meta::info");
8860 2 : return error_mark_node;
8861 : }
8862 :
8863 5173 : if (null_reflection_p (refl))
8864 : {
8865 6 : error_at (loc, "cannot splice a null reflection");
8866 6 : return error_mark_node;
8867 : }
8868 :
8869 : /* This isn't checked in check_splice_expr, because reflect_kind isn't
8870 : available there and variable_of (parameters_of (...)[...]) can be
8871 : spliced. */
8872 5167 : if (REFLECT_EXPR_KIND (refl) == REFLECT_PARM)
8873 : {
8874 6 : auto_diagnostic_group d;
8875 12 : error_at (loc, "cannot use %qD function parameter reflection in a "
8876 6 : "splice expression", REFLECT_EXPR_HANDLE (refl));
8877 6 : if (DECL_CONTEXT (REFLECT_EXPR_HANDLE (refl)) == current_function_decl)
8878 2 : inform (loc,
8879 : "apply %<std::meta::variable_of%> on it before splicing");
8880 6 : return error_mark_node;
8881 6 : }
8882 :
8883 5161 : reflect_kind kind = REFLECT_EXPR_KIND (refl);
8884 5161 : refl = REFLECT_EXPR_HANDLE (refl);
8885 :
8886 : /* We are bringing some entity from the unevaluated expressions world
8887 : to possibly outside of that, mark it used. */
8888 5161 : if (!mark_used (refl))
8889 0 : return error_mark_node;
8890 :
8891 : /* Function templates are wrapped in OVERLOAD from name lookup
8892 : and a lot of places assume that. Furthermore, if reflection comes
8893 : from ^^fntmpl, it is wrapped with OVERLOAD already, only when
8894 : it comes from e.g. members_of it is not. */
8895 5161 : if (DECL_FUNCTION_TEMPLATE_P (refl))
8896 1120 : refl = ovl_make (refl, NULL_TREE);
8897 : /* Also add a BASELINK so that we handle &[:R:]. Since R was already
8898 : resolved (e.g. via members_of), we don't want to consider the enclosing
8899 : class for the access path. */
8900 5161 : if (is_overloaded_fn (refl))
8901 2116 : refl = baselink_for_fns (refl, /*ignore_current_class_p=*/true);
8902 : /* For [:reflect_object(var):] wrap var into a VCE, so that it is an lvalue,
8903 : but not the var itself. */
8904 5161 : if (kind == REFLECT_OBJECT && DECL_P (refl))
8905 44 : refl = build1_loc (loc, VIEW_CONVERT_EXPR, TREE_TYPE (refl), refl);
8906 :
8907 : return refl;
8908 : }
8909 :
8910 : static bool consteval_only_p (tree, hash_set<tree> &);
8911 :
8912 : /* True if T is a consteval-only value as per [expr.const.const]/1:
8913 : A consteval-only value is either
8914 : -- a reflection value that is not the null reflection value or
8915 : -- a pointer or pointer-to-member that points to an immediate function
8916 : or to or past the end of an immediate object.
8917 :
8918 : This function doesn't look for an immediate function; for that, see
8919 : find_immediate_fndecl. */
8920 :
8921 : static bool
8922 34343521 : consteval_only_value_p (tree t, hash_set<tree> &seen)
8923 : {
8924 34343521 : STRIP_NOPS (t);
8925 :
8926 34343521 : if (REFLECT_EXPR_P (t) && !null_reflection_p (t))
8927 : return true;
8928 :
8929 34339357 : switch (TREE_CODE (t))
8930 : {
8931 8895713 : case ADDR_EXPR:
8932 8895713 : case POINTER_PLUS_EXPR:
8933 8895713 : case ARRAY_REF:
8934 8895713 : case COMPONENT_REF:
8935 8895713 : return consteval_only_p (TREE_OPERAND (t, 0), seen);
8936 : default:
8937 : break;
8938 : }
8939 : return false;
8940 : }
8941 :
8942 : /* Return true if T is an immediate object as per [expr.const.const]/2:
8943 : An object is an immediate object if its complete object has
8944 : -- a constituent value that is consteval-only or
8945 : -- a constituent reference that refers to an immediate object or
8946 : immediate function.
8947 :
8948 : Also return true for consteval-only values. This doesn't check for
8949 : immediate functions. */
8950 :
8951 : static bool
8952 183332301 : consteval_only_p (tree t, hash_set<tree> &seen)
8953 : {
8954 183332301 : if (!flag_reflection || !t || t == error_mark_node)
8955 : return false;
8956 :
8957 : /* Say that the wrapper itself is consteval-only so that
8958 : check_out_of_consteval_use_r returns the tree carrying the location. */
8959 44293729 : STRIP_ANY_LOCATION_WRAPPER (t);
8960 :
8961 : /* Walking COMPONENT_REFs can walk back into a CONSTRUCTOR that's still
8962 : under construction and we'd loop. */
8963 44293729 : if (seen.add (t))
8964 : return false;
8965 :
8966 40997979 : if (TREE_CODE (t) == TREE_VEC)
8967 : {
8968 0 : for (tree arg : tree_vec_range (t))
8969 0 : if (arg && consteval_only_p (arg, seen))
8970 0 : return true;
8971 0 : return false;
8972 : }
8973 :
8974 : /* Pull out the initializer. Don't call fold_non_dependent_expr and
8975 : similar here because that could prematurely instantiate things. */
8976 40997979 : if (VAR_P (t))
8977 6493713 : t = DECL_INITIAL (t);
8978 :
8979 40997979 : if (t && TREE_CODE (t) == TARGET_EXPR)
8980 320099 : t = TARGET_EXPR_INITIAL (t);
8981 :
8982 40997979 : if (t && TREE_CODE (t) == CONSTRUCTOR)
8983 : {
8984 34665459 : for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
8985 20261764 : if (consteval_only_p (elt.value, seen))
8986 : return true;
8987 : return false;
8988 : }
8989 :
8990 36114678 : return t && consteval_only_value_p (t, seen);
8991 : }
8992 :
8993 : /* Wrapper for consteval_only_p that sets up a hash set. */
8994 :
8995 : bool
8996 154174824 : consteval_only_p (tree t)
8997 : {
8998 154174824 : hash_set<tree> seen;
8999 154174824 : return consteval_only_p (t, seen);
9000 154174824 : }
9001 :
9002 : /* A walker for check_out_of_consteval_use_r. It cannot be a lambda, because
9003 : we have to call this recursively. */
9004 :
9005 : static tree
9006 9980375 : check_out_of_consteval_use_r (tree *tp, int *walk_subtrees, void *pset_)
9007 : {
9008 9980375 : tree t = *tp;
9009 9980375 : auto pset = static_cast<hash_set<tree> *>(pset_);
9010 :
9011 : /* No need to look into types or unevaluated operands. */
9012 9980375 : if (TYPE_P (t)
9013 9980041 : || (unevaluated_p (TREE_CODE (t)) && !REFLECT_EXPR_P (t))
9014 : /* Don't recurse on DECL_EXPRs. */
9015 9970053 : || TREE_CODE (t) == DECL_EXPR
9016 : /* Neither into USING_STMT. */
9017 9970033 : || TREE_CODE (t) == USING_STMT
9018 : /* The operand of a splice is a constant-expression, thus
9019 : manifestly constant-evaluated, so consteval-only values are permitted
9020 : here. */
9021 9970033 : || TREE_CODE (t) == SPLICE_EXPR
9022 : /* Blocks can appear in the TREE_VEC operand of OpenMP
9023 : depend/affinity/map/to/from OMP_CLAUSEs when using iterators. */
9024 19950408 : || TREE_CODE (t) == BLOCK)
9025 : {
9026 10342 : *walk_subtrees = false;
9027 10342 : return NULL_TREE;
9028 : }
9029 :
9030 : /* A subexpression of a manifestly constant-evaluated expression is
9031 : an immediate function context. For example,
9032 :
9033 : consteval void foo (std::meta::info) { }
9034 : void g() { foo (^^void); }
9035 :
9036 : is all good. */
9037 9970033 : if (tree decl = cp_get_callee_fndecl_nofold (t))
9038 251101 : if (immediate_invocation_p (decl))
9039 : {
9040 1832 : *walk_subtrees = false;
9041 1832 : return NULL_TREE;
9042 : }
9043 :
9044 9968201 : if (TREE_CODE (t) == EXPR_STMT)
9045 : {
9046 40 : if (tree r = cp_walk_tree (&EXPR_STMT_EXPR (t),
9047 : check_out_of_consteval_use_r, pset, pset))
9048 : {
9049 : /* If we can't give a precise location of the expression,
9050 : use the location of the whole statement. */
9051 16 : if (cp_expr_location (r) == UNKNOWN_LOCATION)
9052 : return t;
9053 16 : return r;
9054 : }
9055 24 : *walk_subtrees = false;
9056 24 : return NULL_TREE;
9057 : }
9058 :
9059 9968161 : if (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
9060 : {
9061 24747 : tree vexpr = DECL_VALUE_EXPR (t);
9062 24747 : if (tree ret = cp_walk_tree (&vexpr, check_out_of_consteval_use_r, pset,
9063 : pset))
9064 0 : return ret;
9065 : }
9066 :
9067 9968161 : if (TREE_CODE (t) == BIND_EXPR)
9068 : {
9069 26 : if (tree r = cp_walk_tree (&BIND_EXPR_BODY (t),
9070 : check_out_of_consteval_use_r, pset, pset))
9071 : return r;
9072 : /* Don't walk BIND_EXPR_VARS. */
9073 20 : *walk_subtrees = false;
9074 20 : return NULL_TREE;
9075 : }
9076 :
9077 9968135 : if (TREE_CODE (t) == IF_STMT)
9078 : {
9079 12 : if (IF_STMT_CONSTEVAL_P (t))
9080 : {
9081 4 : if (tree r = cp_walk_tree (&ELSE_CLAUSE (t),
9082 : check_out_of_consteval_use_r, pset, pset))
9083 : return r;
9084 : /* Don't walk the consteval branch. */
9085 2 : *walk_subtrees = false;
9086 2 : return NULL_TREE;
9087 : }
9088 8 : else if (IF_STMT_CONSTEXPR_P (t))
9089 : {
9090 4 : if (tree r = cp_walk_tree (&THEN_CLAUSE (t),
9091 : check_out_of_consteval_use_r, pset, pset))
9092 : return r;
9093 2 : if (tree r = cp_walk_tree (&ELSE_CLAUSE (t),
9094 : check_out_of_consteval_use_r, pset, pset))
9095 : return r;
9096 : /* Don't walk the condition -- it's a manifestly constant-evaluated
9097 : context. */
9098 2 : *walk_subtrees = false;
9099 2 : return NULL_TREE;
9100 : }
9101 : }
9102 :
9103 : /* Don't diagnose RETURN_EXPRs in cdtors on cdtor_returns_this
9104 : target. Those don't exist on other targets. */
9105 9968127 : if (TREE_CODE (t) == RETURN_EXPR
9106 0 : && targetm.cxx.cdtor_returns_this ()
9107 9968127 : && (DECL_CONSTRUCTOR_P (current_function_decl)
9108 0 : || DECL_DESTRUCTOR_P (current_function_decl)))
9109 : {
9110 0 : *walk_subtrees = false;
9111 0 : return NULL_TREE;
9112 : }
9113 :
9114 : /* If we don't find an immediate object, there's nothing to do. */
9115 9968127 : if (!consteval_only_p (t))
9116 : return NULL_TREE;
9117 :
9118 : /* Already escalated? */
9119 458 : if (current_function_decl
9120 816 : && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
9121 : {
9122 0 : *walk_subtrees = false;
9123 0 : return NULL_TREE;
9124 : }
9125 :
9126 : /* We might have to escalate if we are in an immediate-escalating
9127 : function. */
9128 458 : if (immediate_escalating_function_p (current_function_decl))
9129 : {
9130 70 : promote_function_to_consteval (current_function_decl);
9131 70 : *walk_subtrees = false;
9132 70 : return NULL_TREE;
9133 : }
9134 :
9135 388 : *walk_subtrees = false;
9136 388 : return t;
9137 : }
9138 :
9139 : /* Detect if a consteval-only expression EXPR or a consteval-only
9140 : variable EXPR not declared constexpr is used outside
9141 : a manifestly constant-evaluated context.
9142 :
9143 : [expr.const.const]/3: Every immediate object shall be
9144 : -- the object associated with a constexpr variable or a subobject thereof,
9145 : -- a template parameter object ([temp.param]) or a subject thereof, or
9146 : -- an object whose lifetime begins and ends during the evaluation of
9147 : a core constant expression.
9148 :
9149 : E.g.:
9150 :
9151 : void f() {
9152 : constexpr auto r = ^^int; // OK
9153 : [: r :] i = 42; // still OK
9154 : auto z = r; // bad
9155 : }
9156 :
9157 : But
9158 :
9159 : consteval void g() {
9160 : constexpr auto r = ^^int;
9161 : auto z = r;
9162 : }
9163 :
9164 : is OK. If COMPLAIN, emit an error; otherwise we're in the search-only
9165 : mode. Return true if we found a problematic expression. */
9166 :
9167 : bool
9168 122013436 : check_out_of_consteval_use (tree expr, bool complain/*=true*/)
9169 : {
9170 122013436 : if (!flag_reflection || in_immediate_context () || expr == NULL_TREE)
9171 : return false;
9172 :
9173 3085029 : if (VAR_P (expr) && DECL_DECLARED_CONSTEXPR_P (expr))
9174 : return false;
9175 :
9176 3085025 : hash_set<tree> pset;
9177 3085025 : if (tree t = cp_walk_tree (&expr, check_out_of_consteval_use_r, &pset, &pset))
9178 : {
9179 388 : if (complain)
9180 138 : error_at (cp_expr_loc_or_loc (t, cp_expr_loc_or_input_loc (expr)),
9181 : "consteval-only value outside an immediate function "
9182 : "context");
9183 : return true;
9184 : }
9185 :
9186 : return false;
9187 3085025 : }
9188 :
9189 : /* Return true if the reflections LHS and RHS are equal. */
9190 :
9191 : bool
9192 6254 : compare_reflections (tree lhs, tree rhs)
9193 : {
9194 6254 : if (null_reflection_p (lhs) || null_reflection_p (rhs))
9195 218 : return null_reflection_p (lhs) == null_reflection_p (rhs);
9196 :
9197 6038 : reflect_kind lkind;
9198 6038 : do
9199 : {
9200 6038 : lkind = REFLECT_EXPR_KIND (lhs);
9201 6038 : if (lkind != REFLECT_EXPR_KIND (rhs))
9202 : return false;
9203 5978 : lhs = REFLECT_EXPR_HANDLE (lhs);
9204 5978 : rhs = REFLECT_EXPR_HANDLE (rhs);
9205 : }
9206 5978 : while (REFLECT_EXPR_P (lhs) && REFLECT_EXPR_P (rhs));
9207 :
9208 5976 : lhs = resolve_nondeduced_context (lhs, tf_warning_or_error);
9209 5976 : rhs = resolve_nondeduced_context (rhs, tf_warning_or_error);
9210 :
9211 : /* TEMPLATE_DECLs are wrapped in an OVERLOAD. When we have
9212 :
9213 : template_of (^^fun_tmpl<int>) == ^^fun_tmpl
9214 :
9215 : the RHS will be OVERLOAD<TEMPLATE_DECL> but the LHS will
9216 : only be TEMPLATE_DECL. They should compare equal, though. */
9217 : // ??? Can we do something better?
9218 5976 : lhs = maybe_get_first_fn (lhs);
9219 5976 : rhs = maybe_get_first_fn (rhs);
9220 :
9221 : /* First handle reflection-specific comparisons, then fall back to
9222 : cp_tree_equal. */
9223 5976 : if (lkind == REFLECT_PARM)
9224 : {
9225 150 : lhs = maybe_update_function_parm (lhs);
9226 150 : rhs = maybe_update_function_parm (rhs);
9227 : }
9228 5826 : else if (lkind == REFLECT_DATA_MEMBER_SPEC)
9229 : {
9230 90 : if (typedef_variant_p (TREE_VEC_ELT (lhs, 0))
9231 90 : != typedef_variant_p (TREE_VEC_ELT (rhs, 0))
9232 90 : || !same_type_p (TREE_VEC_ELT (lhs, 0), TREE_VEC_ELT (rhs, 0))
9233 88 : || TREE_VEC_ELT (lhs, 1) != TREE_VEC_ELT (rhs, 1)
9234 64 : || !tree_int_cst_equal (TREE_VEC_ELT (lhs, 2),
9235 64 : TREE_VEC_ELT (rhs, 2))
9236 62 : || !tree_int_cst_equal (TREE_VEC_ELT (lhs, 3),
9237 62 : TREE_VEC_ELT (rhs, 3))
9238 60 : || TREE_VEC_ELT (lhs, 4) != TREE_VEC_ELT (rhs, 4)
9239 146 : || TREE_VEC_LENGTH (lhs) != TREE_VEC_LENGTH (rhs))
9240 : return false;
9241 90 : for (int i = 5; i < TREE_VEC_LENGTH (lhs); ++i)
9242 48 : if (!compare_reflections (TREE_VEC_ELT (lhs, i),
9243 48 : TREE_VEC_ELT (rhs, i)))
9244 : return false;
9245 : return true;
9246 : }
9247 5736 : else if (lkind == REFLECT_ANNOTATION)
9248 36 : return TREE_VALUE (lhs) == TREE_VALUE (rhs);
9249 5700 : else if (lkind == REFLECT_BASE)
9250 118 : return lhs == rhs;
9251 5582 : else if (TYPE_P (lhs) && TYPE_P (rhs))
9252 : {
9253 : /* Given
9254 : using A = int;
9255 : using B = int;
9256 : ^^int != ^^A and ^^A != ^^B. */
9257 3094 : if (typedef_variant_p (lhs) || typedef_variant_p (rhs))
9258 342 : return lhs == rhs;
9259 : /* This is for comparing function types. E.g.,
9260 : auto fn() -> int; type_of(^^fn) == ^^auto()->int; */
9261 2752 : return same_type_p (lhs, rhs);
9262 : }
9263 :
9264 2638 : return cp_tree_equal (lhs, rhs);
9265 : }
9266 :
9267 : /* Return true if T is a valid splice-type-specifier.
9268 : [dcl.type.splice]: For a splice-type-specifier of the form
9269 : "typename[opt] splice-specifier", the splice-specifier shall designate
9270 : a type, a class template, or an alias template.
9271 : For a splice-type-specifier of the form
9272 : "typename[opt] splice-specialization-specifier", the splice-specifier
9273 : of the splice-specialization-specifier shall designate a template T
9274 : that is either a class template or an alias template. */
9275 :
9276 : bool
9277 761 : valid_splice_type_p (const_tree t)
9278 : {
9279 761 : return TYPE_P (t);
9280 : }
9281 :
9282 : /* Return true if T is a valid splice-scope-specifier.
9283 : [basic.lookup.qual.general]: If a splice-scope-specifier is followed
9284 : by a ::, it shall either be a dependent splice-scope-specifier or it
9285 : shall designate a namespace, class, enumeration, or dependent type. */
9286 :
9287 : bool
9288 434 : valid_splice_scope_p (const_tree t)
9289 : {
9290 112 : return (CLASS_TYPE_P (t)
9291 322 : || TREE_CODE (t) == ENUMERAL_TYPE
9292 320 : || TREE_CODE (t) == NAMESPACE_DECL
9293 494 : || TREE_CODE (t) == SPLICE_SCOPE);
9294 : }
9295 :
9296 : /* Return true if T is a valid result of the splice in a class member access,
9297 : as in obj.[:R:]. If DECLS_ONLY_P, only certain decls are OK. */
9298 :
9299 : bool
9300 6816 : valid_splice_for_member_access_p (const_tree t, bool decls_only_p/*=true*/)
9301 : {
9302 6816 : if (TREE_CODE (t) == FIELD_DECL
9303 : || VAR_P (t)
9304 6816 : || TREE_CODE (t) == CONST_DECL
9305 4502 : || TREE_CODE (t) == FUNCTION_DECL
9306 3538 : || reflection_function_template_p (t)
9307 9176 : || variable_template_p (const_cast<tree> (t)))
9308 : return true;
9309 :
9310 2032 : if (decls_only_p)
9311 : return false;
9312 :
9313 1870 : return (BASELINK_P (t)
9314 700 : || TREE_CODE (t) == TEMPLATE_ID_EXPR
9315 2078 : || TREE_CODE (t) == TREE_BINFO);
9316 : }
9317 :
9318 : /* Check if T is a valid result of splice-expression. ADDRESS_P is true if
9319 : we are taking the address of the splice. MEMBER_ACCESS_P is true if this
9320 : splice is used in foo.[: bar :] or foo->[: bar :] context. TEMPLATE_P is
9321 : true if the splice-expression was preceded by 'template'. TARGS_P is true
9322 : if there were template arguments. COMPLAIN_P is true if any errors should
9323 : be emitted. Returns true if no problems are found, false otherwise. */
9324 :
9325 : bool
9326 4550 : check_splice_expr (location_t loc, location_t start_loc, tree t,
9327 : bool address_p, bool member_access_p, bool template_p,
9328 : bool targs_p, bool complain_p)
9329 : {
9330 4550 : t = MAYBE_BASELINK_FUNCTIONS (t);
9331 4550 : tree expr = t;
9332 4550 : if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
9333 420 : t = TREE_OPERAND (t, 0);
9334 4550 : t = OVL_FIRST (t);
9335 :
9336 : /* We may not have gotten an expression. */
9337 4550 : if (TREE_CODE (t) == TYPE_DECL
9338 4540 : || TREE_CODE (t) == NAMESPACE_DECL
9339 4522 : || TYPE_P (t))
9340 : {
9341 134 : if (complain_p)
9342 : {
9343 110 : if (TYPE_P (t))
9344 : {
9345 86 : auto_diagnostic_group d;
9346 86 : error_at (loc, "expected a reflection of an expression");
9347 86 : inform_tree_category (t);
9348 86 : if (start_loc != UNKNOWN_LOCATION)
9349 : {
9350 70 : rich_location richloc (line_table, start_loc);
9351 70 : richloc.add_fixit_insert_before (start_loc, "typename");
9352 70 : inform (&richloc, "add %<typename%> to denote a type "
9353 : "outside a type-only context");
9354 70 : }
9355 : else
9356 16 : inform (loc, "add %<typename%> to denote a type outside "
9357 : "a type-only context");
9358 86 : }
9359 : else
9360 : {
9361 24 : auto_diagnostic_group d;
9362 24 : error_at (loc, "expected a reflection of an expression");
9363 24 : inform_tree_category (t);
9364 24 : }
9365 : }
9366 : return false;
9367 : }
9368 : /* [expr.prim.splice]/2 For a splice-expression of the form
9369 : splice-specifier, the expression is ill-formed if it is: */
9370 : /* -- a constructor or a destructor */
9371 4416 : if (TREE_CODE (STRIP_TEMPLATE (t)) == FUNCTION_DECL
9372 4416 : && (DECL_CONSTRUCTOR_P (t) || DECL_DESTRUCTOR_P (t)))
9373 : {
9374 38 : if (complain_p)
9375 38 : error_at (loc, "cannot use constructor or destructor %qD in a splice "
9376 : "expression", t);
9377 : return false;
9378 : }
9379 : /* -- an unnamed bit-field */
9380 4378 : if (TREE_CODE (t) == FIELD_DECL && DECL_UNNAMED_BIT_FIELD (t))
9381 : {
9382 2 : if (complain_p)
9383 2 : error_at (loc, "cannot use an unnamed bit-field %qD in a splice "
9384 : "expression", t);
9385 : return false;
9386 : }
9387 : /* Class members may not be implicitly referenced through a splice.
9388 : But taking the address is fine, and so is class member access a la
9389 : foo.[: ^^S::bar :]. */
9390 4376 : if (!address_p
9391 : && !member_access_p
9392 1678 : && DECL_P (t)
9393 5532 : && DECL_NONSTATIC_MEMBER_P (t))
9394 : {
9395 72 : if (complain_p)
9396 60 : error_at (loc, "cannot implicitly reference a class member %qD "
9397 : "through a splice", t);
9398 : return false;
9399 : }
9400 :
9401 : /* One can't access a class template or alias template with . or ->. */
9402 4304 : if (member_access_p && DECL_TYPE_TEMPLATE_P (t))
9403 : {
9404 10 : if (complain_p)
9405 10 : error_at (loc, "invalid class member access of type template %qE", t);
9406 : return false;
9407 : }
9408 :
9409 4294 : if (member_access_p
9410 2486 : && !valid_splice_for_member_access_p (expr, /*decls_only_p=*/false))
9411 : {
9412 20 : if (complain_p)
9413 20 : error_at (loc, "cannot use %qE to access a class member", t);
9414 : return false;
9415 : }
9416 :
9417 : /* [expr.unary.op]/3.1 "If the operand [of unary &] is a qualified-id or
9418 : splice-expression designating a non-static member m, other than an
9419 : explicit object member function, m shall be a direct member of some
9420 : class C that is not an anonymous union." */
9421 4274 : if (address_p
9422 202 : && TREE_CODE (t) == FIELD_DECL
9423 50 : && ANON_UNION_TYPE_P (DECL_CONTEXT (t))
9424 4288 : && !TYPE_P (context_for_name_lookup (t)))
9425 : {
9426 10 : if (complain_p)
9427 10 : error_at (loc, "unary %<&%> applied to an anonymous union member "
9428 : "%qD that is not a direct member of a named class", t);
9429 : return false;
9430 : }
9431 :
9432 : /* [expr.prim.splice]/2 doesn't allow for the splice-expression of the form
9433 : splice-specifier to designate a template. But we accept a function
9434 : template here as an extension, because it would be valid if the 'template'
9435 : keyword were present. */
9436 4264 : if (!template_p
9437 : && !targs_p
9438 2816 : && TREE_CODE (t) == TEMPLATE_DECL
9439 4338 : && !DECL_FUNCTION_TEMPLATE_P (t))
9440 : {
9441 46 : if (complain_p)
9442 36 : error_at (loc, "cannot use template %qE in a splice expression", t);
9443 : return false;
9444 : }
9445 :
9446 : /* [expr.prim.splice]/4 allows only function and variable templates in the
9447 : template splice-specialization-specifier form. */
9448 4218 : if (targs_p
9449 788 : && TREE_CODE (t) == TEMPLATE_DECL
9450 406 : && !DECL_FUNCTION_TEMPLATE_P (t)
9451 4400 : && !variable_template_p (t))
9452 : {
9453 10 : if (complain_p)
9454 10 : error_at (loc, "expected a reflection of a function or variable "
9455 : "template");
9456 : return false;
9457 : }
9458 :
9459 : /* [expr.prim.splice]/2: "The expression is ill-formed if S [the construct
9460 : designated by splice-specifier] is
9461 : -- a local entity such that there is a lambda scope that intervenes
9462 : between the expression and the point at which S was introduced"
9463 : This also checks ODR violations (reflect/odr1.C). */
9464 4208 : if (outer_automatic_var_p (t))
9465 62 : if (tree r = process_outer_var_ref (t, tf_none))
9466 62 : if (r == error_mark_node || is_capture_proxy (r))
9467 : {
9468 : /* Not letting process_outer_var_ref emit the error so that we can
9469 : say "in a splice expression". */
9470 30 : if (complain_p)
9471 : {
9472 30 : auto_diagnostic_group d;
9473 30 : error_at (loc, "use of local variable with automatic storage "
9474 : "from containing function in a splice expression");
9475 30 : inform (DECL_SOURCE_LOCATION (t), "%q#D declared here", t);
9476 30 : }
9477 : return false;
9478 : }
9479 :
9480 : /* If we had a reflect_kind here, we could just check for
9481 : REFLECT_ANNOTATION and be done with it. But we don't have it yet (TODO),
9482 : so do it the suboptimal way. */
9483 4178 : if (TREE_CODE (t) == TREE_LIST && annotation_p (t))
9484 : {
9485 2 : if (complain_p)
9486 2 : error_at (loc, "cannot use an annotation %qE in a splice expression",
9487 : t);
9488 : return false;
9489 : }
9490 :
9491 : /* Same, but with REFLECT_DATA_MEMBER_SPEC. */
9492 4176 : if (TREE_CODE (t) == TREE_VEC)
9493 : {
9494 2 : if (complain_p)
9495 2 : error_at (loc, "cannot use a data member specification in a "
9496 : "splice expression");
9497 : return false;
9498 : }
9499 :
9500 : /* Like with NSDMs, a bare [:X:] designating a direct base class
9501 : relationship is ill-formed. */
9502 4174 : if (!member_access_p && TREE_CODE (t) == TREE_BINFO)
9503 : {
9504 2 : if (complain_p)
9505 2 : error_at (loc, "cannot use a base class in a splice expression "
9506 : "without an object");
9507 : return false;
9508 : }
9509 :
9510 4172 : if (template_p)
9511 : {
9512 : /* [expr.prim.splice] For a splice-expression of the form template
9513 : splice-specifier, the splice-specifier shall designate a function
9514 : template. */
9515 1408 : if (!targs_p)
9516 : {
9517 660 : if (!reflection_function_template_p (t) && !dependent_splice_p (t))
9518 : {
9519 38 : if (complain_p)
9520 : {
9521 38 : auto_diagnostic_group d;
9522 38 : error_at (loc, "expected a reflection of a function "
9523 : "template");
9524 38 : inform_tree_category (t);
9525 38 : }
9526 : return false;
9527 : }
9528 : }
9529 : /* [expr.prim.splice] For a splice-expression of the form
9530 : template splice-specialization-specifier, the splice-specifier of the
9531 : splice-specialization-specifier shall designate a template. The
9532 : template should be a function template or a variable template. */
9533 748 : else if (DECL_TYPE_TEMPLATE_P (t))
9534 : {
9535 0 : if (complain_p)
9536 : {
9537 0 : auto_diagnostic_group d;
9538 0 : error_at (loc, "expected a reflection of a function or variable "
9539 : "template");
9540 0 : inform_tree_category (t);
9541 0 : }
9542 : return false;
9543 : }
9544 1370 : gcc_checking_assert (reflection_function_template_p (t)
9545 : || get_template_info (expr)
9546 : || TREE_CODE (expr) == TEMPLATE_ID_EXPR
9547 : || variable_template_p (t)
9548 : || dependent_splice_p (t));
9549 : }
9550 :
9551 4134 : if (complain_p
9552 4134 : && !template_p
9553 : /* No 'template' but there were template arguments? */
9554 2142 : && (targs_p
9555 : /* No 'template' but the splice-specifier designates a function
9556 : template? */
9557 2112 : || reflection_function_template_p (t))
9558 6276 : && warning_enabled_at (loc, OPT_Wmissing_template_keyword))
9559 : /* Were 'template' present, this would be valid code, so keep going. */
9560 50 : missing_template_diag (loc, diagnostics::kind::pedwarn);
9561 :
9562 : return true;
9563 : }
9564 :
9565 : /* Create a new SPLICE_SCOPE tree. EXPR is its SPLICE_SCOPE_EXPR, and
9566 : TYPE_P says if it should have SPLICE_SCOPE_TYPE_P set. */
9567 :
9568 : tree
9569 506 : make_splice_scope (tree expr, bool type_p)
9570 : {
9571 506 : tree t = cxx_make_type (SPLICE_SCOPE);
9572 506 : SPLICE_SCOPE_EXPR (t) = expr;
9573 506 : SPLICE_SCOPE_TYPE_P (t) = type_p;
9574 506 : SET_TYPE_STRUCTURAL_EQUALITY (t);
9575 506 : return t;
9576 : }
9577 :
9578 : /* Return true if T is a splice expression; that is, it is either [:T:] or
9579 : [:T:]<arg>. */
9580 :
9581 : bool
9582 116950115 : dependent_splice_p (const_tree t)
9583 : {
9584 116950115 : return (TREE_CODE (t) == SPLICE_EXPR
9585 116950115 : || (TREE_CODE (t) == TEMPLATE_ID_EXPR
9586 4652119 : && TREE_CODE (TREE_OPERAND (t, 0)) == SPLICE_EXPR));
9587 : }
9588 :
9589 : /* Check if SPLICE is a valid namespace splice. Returns true if no problems
9590 : are found, false otherwise. */
9591 :
9592 : bool
9593 38 : check_namespace_splice (cp_expr splice)
9594 : {
9595 38 : if (TREE_CODE (splice) != NAMESPACE_DECL && !dependent_namespace_p (splice))
9596 : {
9597 6 : auto_diagnostic_group d;
9598 6 : error_at (splice.get_location (), "expected a reflection of "
9599 : "a namespace");
9600 6 : inform_tree_category (splice);
9601 6 : return false;
9602 6 : }
9603 32 : else if (splice == global_namespace)
9604 : {
9605 4 : error_at (splice.get_location (), "splice of the global namespace "
9606 : "can only appear in a nested-name-specifier");
9607 4 : return false;
9608 : }
9609 : return true;
9610 : }
9611 :
9612 : /* Annotation index for mangling. */
9613 :
9614 : static GTY(()) int annotation_idx;
9615 :
9616 : /* Helper function for mangle.cc (write_reflection).
9617 : Determine 2 letter mangling prefix and store it into prefix.
9618 : Additionally return the reflection handle possibly adjusted so that
9619 : write_reflection can mangle the operands of it if any are needed. */
9620 :
9621 : tree
9622 1758 : reflection_mangle_prefix (tree refl, char prefix[3])
9623 : {
9624 1758 : tree h = REFLECT_EXPR_HANDLE (refl);
9625 1758 : reflect_kind kind = REFLECT_EXPR_KIND (refl);
9626 1758 : if (h == unknown_type_node)
9627 : {
9628 8 : strcpy (prefix, "nu");
9629 8 : return NULL_TREE;
9630 : }
9631 3500 : if (eval_is_value (kind) == boolean_true_node)
9632 : {
9633 12 : strcpy (prefix, "vl");
9634 12 : if (VAR_P (h) && DECL_NTTP_OBJECT_P (h))
9635 0 : h = tparm_object_argument (h);
9636 : return h;
9637 : }
9638 3466 : if (eval_is_object (kind) == boolean_true_node)
9639 : {
9640 10 : strcpy (prefix, "ob");
9641 10 : return h;
9642 : }
9643 1728 : if (eval_is_variable (h, kind) == boolean_true_node)
9644 : {
9645 226 : strcpy (prefix, "vr");
9646 226 : return h;
9647 : }
9648 1502 : if (eval_is_structured_binding (h, kind) == boolean_true_node)
9649 : {
9650 8 : strcpy (prefix, "sb");
9651 8 : return h;
9652 : }
9653 1494 : if (eval_is_function (h) == boolean_true_node)
9654 : {
9655 196 : strcpy (prefix, "fn");
9656 196 : return maybe_get_first_fn (h);
9657 : }
9658 1298 : if (eval_is_function_parameter (h, kind) == boolean_true_node)
9659 : {
9660 82 : strcpy (prefix, "pa");
9661 82 : return maybe_update_function_parm (h);
9662 : }
9663 2406 : if (eval_is_enumerator (h) == boolean_true_node)
9664 : {
9665 26 : strcpy (prefix, "en");
9666 26 : return h;
9667 : }
9668 1190 : if (eval_is_annotation (h, kind) == boolean_true_node)
9669 : {
9670 10 : strcpy (prefix, "an");
9671 10 : if (TREE_PURPOSE (TREE_VALUE (h)) == NULL_TREE)
9672 8 : TREE_PURPOSE (TREE_VALUE (h))
9673 16 : = bitsize_int (annotation_idx++);
9674 : /* TREE_PURPOSE void_node or INTEGER_CST with signed type
9675 : means it is annotation which should appear in
9676 : variable_of list. */
9677 2 : else if (TREE_PURPOSE (TREE_VALUE (h)) == void_node)
9678 0 : TREE_PURPOSE (TREE_VALUE (h))
9679 0 : = sbitsize_int (annotation_idx++);
9680 10 : return TREE_PURPOSE (TREE_VALUE (h));
9681 : }
9682 1180 : if (eval_is_type_alias (h) == boolean_true_node)
9683 : {
9684 28 : strcpy (prefix, "ta");
9685 28 : return h;
9686 : }
9687 1152 : if (eval_is_type (h) == boolean_true_node)
9688 : {
9689 314 : strcpy (prefix, "ty");
9690 314 : return h;
9691 : }
9692 838 : if (eval_is_nonstatic_data_member (h) == boolean_true_node)
9693 : {
9694 234 : strcpy (prefix, "dm");
9695 234 : return h;
9696 : }
9697 604 : if (TREE_CODE (h) == FIELD_DECL && DECL_UNNAMED_BIT_FIELD (h))
9698 : {
9699 8 : strcpy (prefix, "un");
9700 8 : return h;
9701 : }
9702 596 : if (eval_is_class_template (h) == boolean_true_node)
9703 : {
9704 58 : strcpy (prefix, "ct");
9705 58 : return h;
9706 : }
9707 538 : if (eval_is_function_template (h) == boolean_true_node)
9708 : {
9709 50 : strcpy (prefix, "ft");
9710 50 : h = maybe_get_first_fn (h);
9711 50 : return h;
9712 : }
9713 488 : if (eval_is_variable_template (h) == boolean_true_node)
9714 : {
9715 28 : strcpy (prefix, "vt");
9716 28 : return h;
9717 : }
9718 460 : if (eval_is_alias_template (h) == boolean_true_node)
9719 : {
9720 26 : strcpy (prefix, "at");
9721 26 : return h;
9722 : }
9723 868 : if (eval_is_concept (h) == boolean_true_node)
9724 : {
9725 22 : strcpy (prefix, "co");
9726 22 : return h;
9727 : }
9728 412 : if (eval_is_namespace_alias (h) == boolean_true_node)
9729 : {
9730 38 : strcpy (prefix, "na");
9731 38 : return h;
9732 : }
9733 490 : if (eval_is_namespace (h) == boolean_true_node)
9734 : {
9735 258 : if (h == global_namespace)
9736 : {
9737 54 : strcpy (prefix, "gs");
9738 54 : return NULL_TREE;
9739 : }
9740 204 : strcpy (prefix, "ns");
9741 204 : return h;
9742 : }
9743 116 : if (eval_is_base (h, kind) == boolean_true_node)
9744 : {
9745 26 : strcpy (prefix, "ba");
9746 26 : return h;
9747 : }
9748 90 : if (eval_is_data_member_spec (h, kind) == boolean_true_node)
9749 : {
9750 84 : strcpy (prefix, "ds");
9751 84 : return h;
9752 : }
9753 : /* We don't have a metafunction for template template parameters. */
9754 6 : if (DECL_TEMPLATE_TEMPLATE_PARM_P (h))
9755 : {
9756 2 : strcpy (prefix, "tt");
9757 2 : return h;
9758 : }
9759 : /* For ^^T::x, we can't say what the reflection is going to be. Just say
9760 : it's something dependent. This is at the end rather than the beginning
9761 : because potential_constant_expression_1 doesn't handle codes like
9762 : TREE_BINFO. */
9763 4 : if (uses_template_parms (h))
9764 : {
9765 4 : strcpy (prefix, "de");
9766 4 : return h;
9767 : }
9768 0 : gcc_unreachable ();
9769 : }
9770 :
9771 : /* Returns true iff X is a reflection of a function template. */
9772 :
9773 : bool
9774 8376 : reflection_function_template_p (const_tree x)
9775 : {
9776 23920 : return (DECL_FUNCTION_TEMPLATE_P
9777 8376 : (OVL_FIRST (MAYBE_BASELINK_FUNCTIONS (const_cast<tree> (x)))));
9778 : }
9779 :
9780 : #include "gt-cp-reflect.h"
|