tree build_capture_proxy (tree member) { tree var, object, fn, closure, name, lam, type; if (PACK_EXPANSION_P (member)) member = PACK_EXPANSION_PATTERN (member); closure = DECL_CONTEXT (member); fn = lambda_function (closure); lam = CLASSTYPE_LAMBDA_EXPR (closure); /* The proxy variable forwards to the capture field. */ object = build_fold_indirect_ref (DECL_ARGUMENTS (fn)); object = finish_non_static_data_member (member, object, NULL_TREE); if (REFERENCE_REF_P (object)) object = TREE_OPERAND (object, 0); /* Remove the __ inserted by add_capture. */ if (DECL_NORMAL_CAPTURE_P (member)) name = get_identifier (IDENTIFIER_POINTER (DECL_NAME (member)) + 2); else name = DECL_NAME (member); type = lambda_proxy_type (object); if (DECL_VLA_CAPTURE_P (member)) { /* Rebuild the VLA type from the pointer and maxindex. */ tree field = next_initializable_field (TYPE_FIELDS (type)); tree ptr = build_simple_component_ref (object, field); field = next_initializable_field (DECL_CHAIN (field)); tree max = build_simple_component_ref (object, field); type = build_cplus_array_type (TREE_TYPE (TREE_TYPE (ptr)), build_index_type (max)); type = build_reference_type (type); REFERENCE_VLA_OK (type) = true; object = convert (type, ptr); } var = build_decl (input_location, VAR_DECL, name, type); SET_DECL_VALUE_EXPR (var, object); DECL_HAS_VALUE_EXPR_P (var) = 1; DECL_ARTIFICIAL (var) = 1; TREE_USED (var) = 1; DECL_CONTEXT (var) = fn; if (name == this_identifier) { gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (lam) == member); LAMBDA_EXPR_THIS_CAPTURE (lam) = var; } if (fn == current_function_decl) insert_capture_proxy (var); else vec_safe_push (LAMBDA_EXPR_PENDING_PROXIES (lam), var); return var; }
void cxx_print_lambda_node (FILE *file, tree node, int indent) { if (LAMBDA_EXPR_MUTABLE_P (node)) fprintf (file, " /mutable"); fprintf (file, " default_capture_mode=["); switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (node)) { case CPLD_NONE: fprintf (file, "NONE"); break; case CPLD_COPY: fprintf (file, "COPY"); break; case CPLD_REFERENCE: fprintf (file, "CPLD_REFERENCE"); break; default: fprintf (file, "??"); break; } fprintf (file, "] "); print_node (file, "capture_list", LAMBDA_EXPR_CAPTURE_LIST (node), indent + 4); print_node (file, "this_capture", LAMBDA_EXPR_THIS_CAPTURE (node), indent + 4); print_node (file, "return_type", LAMBDA_EXPR_RETURN_TYPE (node), indent + 4); print_node (file, "closure", LAMBDA_EXPR_CLOSURE (node), indent + 4); }
tree build_lambda_expr (void) { tree lambda = make_node (LAMBDA_EXPR); LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) = CPLD_NONE; LAMBDA_EXPR_CAPTURE_LIST (lambda) = NULL_TREE; LAMBDA_EXPR_THIS_CAPTURE (lambda) = NULL_TREE; LAMBDA_EXPR_PENDING_PROXIES (lambda) = NULL; LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE; LAMBDA_EXPR_MUTABLE_P (lambda) = false; return lambda; }
tree lambda_expr_this_capture (tree lambda, bool add_capture_p) { tree result; tree this_capture = LAMBDA_EXPR_THIS_CAPTURE (lambda); /* In unevaluated context this isn't an odr-use, so don't capture. */ if (cp_unevaluated_operand) add_capture_p = false; /* Try to default capture 'this' if we can. */ if (!this_capture && (!add_capture_p || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda) != CPLD_NONE)) { tree lambda_stack = NULL_TREE; tree init = NULL_TREE; /* If we are in a lambda function, we can move out until we hit: 1. a non-lambda function or NSDMI, 2. a lambda function capturing 'this', or 3. a non-default capturing lambda function. */ for (tree tlambda = lambda; ;) { lambda_stack = tree_cons (NULL_TREE, tlambda, lambda_stack); if (LAMBDA_EXPR_EXTRA_SCOPE (tlambda) && TREE_CODE (LAMBDA_EXPR_EXTRA_SCOPE (tlambda)) == FIELD_DECL) { /* In an NSDMI, we don't have a function to look up the decl in, but the fake 'this' pointer that we're using for parsing is in scope_chain. */ init = scope_chain->x_current_class_ptr; gcc_checking_assert (init && (TREE_TYPE (TREE_TYPE (init)) == current_nonlambda_class_type ())); break; } tree closure_decl = TYPE_NAME (LAMBDA_EXPR_CLOSURE (tlambda)); tree containing_function = decl_function_context (closure_decl); if (containing_function == NULL_TREE) /* We ran out of scopes; there's no 'this' to capture. */ break; if (!LAMBDA_FUNCTION_P (containing_function)) { /* We found a non-lambda function. */ if (DECL_NONSTATIC_MEMBER_FUNCTION_P (containing_function)) /* First parameter is 'this'. */ init = DECL_ARGUMENTS (containing_function); break; } tlambda = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (containing_function)); if (LAMBDA_EXPR_THIS_CAPTURE (tlambda)) { /* An outer lambda has already captured 'this'. */ init = LAMBDA_EXPR_THIS_CAPTURE (tlambda); break; } if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (tlambda) == CPLD_NONE) /* An outer lambda won't let us capture 'this'. */ break; } if (init) { if (add_capture_p) this_capture = add_default_capture (lambda_stack, /*id=*/this_identifier, init); else this_capture = init; } } if (cp_unevaluated_operand) result = this_capture; else if (!this_capture) { if (add_capture_p) { error ("%<this%> was not captured for this lambda function"); result = error_mark_node; } else result = NULL_TREE; } else { /* To make sure that current_class_ref is for the lambda. */ gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref)) == LAMBDA_EXPR_CLOSURE (lambda)); result = this_capture; /* If 'this' is captured, each use of 'this' is transformed into an access to the corresponding unnamed data member of the closure type cast (_expr.cast_ 5.4) to the type of 'this'. [ The cast ensures that the transformed expression is an rvalue. ] */ result = rvalue (result); } return result; }
tree add_capture (tree lambda, tree id, tree orig_init, bool by_reference_p, bool explicit_init_p) { char *buf; tree type, member, name; bool vla = false; bool variadic = false; tree initializer = orig_init; if (PACK_EXPANSION_P (initializer)) { initializer = PACK_EXPANSION_PATTERN (initializer); variadic = true; } if (TREE_CODE (initializer) == TREE_LIST) initializer = build_x_compound_expr_from_list (initializer, ELK_INIT, tf_warning_or_error); type = TREE_TYPE (initializer); if (type == error_mark_node) return error_mark_node; if (array_of_runtime_bound_p (type)) { vla = true; if (!by_reference_p) error ("array of runtime bound cannot be captured by copy, " "only by reference"); /* For a VLA, we capture the address of the first element and the maximum index, and then reconstruct the VLA for the proxy. */ tree elt = cp_build_array_ref (input_location, initializer, integer_zero_node, tf_warning_or_error); initializer = build_constructor_va (init_list_type_node, 2, NULL_TREE, build_address (elt), NULL_TREE, array_type_nelts (type)); type = vla_capture_type (type); } else if (!dependent_type_p (type) && variably_modified_type_p (type, NULL_TREE)) { error ("capture of variable-size type %qT that is not an N3639 array " "of runtime bound", type); if (TREE_CODE (type) == ARRAY_TYPE && variably_modified_type_p (TREE_TYPE (type), NULL_TREE)) inform (input_location, "because the array element type %qT has " "variable size", TREE_TYPE (type)); type = error_mark_node; } else { type = lambda_capture_field_type (initializer, explicit_init_p); if (by_reference_p) { type = build_reference_type (type); if (!dependent_type_p (type) && !real_lvalue_p (initializer)) error ("cannot capture %qE by reference", initializer); } else { /* Capture by copy requires a complete type. */ type = complete_type (type); if (!dependent_type_p (type) && !COMPLETE_TYPE_P (type)) { error ("capture by copy of incomplete type %qT", type); cxx_incomplete_type_inform (type); return error_mark_node; } } } /* Add __ to the beginning of the field name so that user code won't find the field with name lookup. We can't just leave the name unset because template instantiation uses the name to find instantiated fields. */ buf = (char *) alloca (IDENTIFIER_LENGTH (id) + 3); buf[1] = buf[0] = '_'; memcpy (buf + 2, IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id) + 1); name = get_identifier (buf); /* If TREE_TYPE isn't set, we're still in the introducer, so check for duplicates. */ if (!LAMBDA_EXPR_CLOSURE (lambda)) { if (IDENTIFIER_MARKED (name)) { pedwarn (input_location, 0, "already captured %qD in lambda expression", id); return NULL_TREE; } IDENTIFIER_MARKED (name) = true; } if (variadic) type = make_pack_expansion (type); /* Make member variable. */ member = build_decl (input_location, FIELD_DECL, name, type); DECL_VLA_CAPTURE_P (member) = vla; if (!explicit_init_p) /* Normal captures are invisible to name lookup but uses are replaced with references to the capture field; we implement this by only really making them invisible in unevaluated context; see qualify_lookup. For now, let's make explicitly initialized captures always visible. */ DECL_NORMAL_CAPTURE_P (member) = true; if (id == this_identifier) LAMBDA_EXPR_THIS_CAPTURE (lambda) = member; /* Add it to the appropriate closure class if we've started it. */ if (current_class_type && current_class_type == LAMBDA_EXPR_CLOSURE (lambda)) finish_member_declaration (member); tree listmem = member; if (variadic) { listmem = make_pack_expansion (member); initializer = orig_init; } LAMBDA_EXPR_CAPTURE_LIST (lambda) = tree_cons (listmem, initializer, LAMBDA_EXPR_CAPTURE_LIST (lambda)); if (LAMBDA_EXPR_CLOSURE (lambda)) return build_capture_proxy (member); /* For explicit captures we haven't started the function yet, so we wait and build the proxy from cp_parser_lambda_body. */ return NULL_TREE; }