Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
370 changes: 276 additions & 94 deletions src/cp_gen_be.c

Large diffs are not rendered by default.

52 changes: 51 additions & 1 deletion src/declarator.c
Original file line number Diff line number Diff line change
Expand Up @@ -5677,6 +5677,44 @@ __w64 annotation, and __based variable specifiers). The given type must be a

#endif /* MICROSOFT_EXTENSIONS_ALLOWED */

#if BACK_END_IS_CP_GEN_BE

static a_type_ptr preserve_collapsed_reference_spelling(
a_type_ptr type,
a_type_ptr base_type,
a_boolean rvalue_ref,
a_decl_parse_state *state)
/*
Return type, the result of reference collapsing, with the base type spelling
and reference operator retained.
*/
{
a_type_ptr result = type;

if (state->qualifiers == TQ_NONE && !is_error_type(type)) {
a_type_ptr naming_type = skip_lexical_typerefs(base_type);
if (type_is(naming_type, tk_typeref) &&
(typeref_is_typedef(naming_type) ||
typeref_is_type_operator(naming_type, /*include_intrinsics=*/TRUE))) {
result = alloc_type(tk_typeref);
result->variant.typeref.kind =
rvalue_ref ? trk_collapsed_rvalue_reference
: trk_collapsed_lvalue_reference;
result->variant.typeref.type = type;
result->variant.typeref.extra_info->operator_type_arg = base_type;
result->variant.typeref.is_dependent =
naming_type->variant.typeref.is_dependent;
result->variant.typeref.is_nonreal =
naming_type->variant.typeref.is_nonreal;
result->variant.typeref.is_prototype_instantiation =
naming_type->variant.typeref.is_prototype_instantiation;
} /* if */
} /* if */
return result;
} /* preserve_collapsed_reference_spelling */

#endif /* BACK_END_IS_CP_GEN_BE */

a_type_ptr pointer_declarator(
a_type_ptr specifiers_type,
a_decl_parse_state *state,
Expand Down Expand Up @@ -5973,10 +6011,16 @@ Microsoft extended decl modifiers are also scanned, but they are ignored
a_source_position_ptr qual_pos =
(state->qualifiers == TQ_RESTRICT) ? &state->restrict_pos
: &state->qualifiers_pos;
complete_type = make_reference_to_reference(
a_type_ptr collapsed_type = make_reference_to_reference(
complete_type, rvalue_ref_case,
/*tracking_ref=*/FALSE,
state->qualifiers, qual_pos, (a_boolean*)NULL);
#if BACK_END_IS_CP_GEN_BE
collapsed_type = preserve_collapsed_reference_spelling(
collapsed_type, complete_type, rvalue_ref_case,
state);
#endif /* BACK_END_IS_CP_GEN_BE */
complete_type = collapsed_type;
state->unused_qualifiers = FALSE;
} else {
/* Type "reference to reference" is illegal. */
Expand Down Expand Up @@ -8534,6 +8578,12 @@ etc.).
make_reference_to_reference(complete_type, is_rvalue_ref,
tracking_ref, state->qualifiers,
qual_pos, (a_boolean*)NULL);
#if BACK_END_IS_CP_GEN_BE
if (!tracking_ref) {
derived_type = preserve_collapsed_reference_spelling(
derived_type, complete_type, is_rvalue_ref, state);
} /* if */
#endif /* BACK_END_IS_CP_GEN_BE */
} /* if */
state->unused_qualifiers = FALSE;
/* The second reference component is essentially ignored. We do not
Expand Down
6 changes: 3 additions & 3 deletions src/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -28916,7 +28916,7 @@ indication in *rcblock).
const_cast; mark it accordingly. */
expr->variant.operation.is_const_cast = TRUE;
} /* if */
record_cast_position_in_expr_rescan_info(expr,
record_cast_position_in_expr_rescan_info(expr, csf_const_cast,
&start_position,
&type_position,
cast_type);
Expand Down Expand Up @@ -29256,7 +29256,7 @@ if it's not valid).
expr->is_static_cast = TRUE;
} /* if */
} /* if */
record_cast_position_in_expr_rescan_info(expr,
record_cast_position_in_expr_rescan_info(expr, source_form,
start_position,
type_position,
type_cast_to);
Expand Down Expand Up @@ -29756,7 +29756,7 @@ indication in *rcblock).
} /* if */
#endif /* CHECKING */
} /* if */
record_cast_position_in_expr_rescan_info(expr,
record_cast_position_in_expr_rescan_info(expr, csf_reinterpret_cast,
&start_position,
&type_position,
type_cast_to);
Expand Down
66 changes: 57 additions & 9 deletions src/exprutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -4642,18 +4642,47 @@ and type is the specified type.


void record_cast_position_in_expr_rescan_info(
an_expr_node_ptr expr,
a_source_position *start_position,
a_source_position *type_position,
a_type_ptr cast_type)
an_expr_node_ptr expr,
a_cast_source_form source_form,
a_source_position *start_position,
a_source_position *type_position,
a_type_ptr cast_type)
/*
Record the source position of a cast in the rescan information
associated with that cast, allocating the rescan info if needed.
expr is the cast expression; start_position is the source position
of the start of the cast; type_position is the source position of the
type within the cast; and cast_type is the type cast to.
type within the cast; and cast_type is the type cast to. source_form
identifies an explicit cast whose type spelling can be used to regenerate
the expression.
*/
{
#if BACK_END_IS_CP_GEN_BE
if (source_form != csf_none && is_cast_operation_node(expr) &&
!expr->compiler_generated && expr->variant.operation.is_reference_cast &&
type_is(cast_type, tk_typeref)) {
a_type_ptr *type_slot =
is_glvalue_node(expr) ? &expr->type : &expr->orig_lvalue_type;
a_type_ptr type = skip_cast_type_typeref(*type_slot);
if (type != NULL) {
a_type_ptr spelling = alloc_type(tk_typeref);
spelling->variant.typeref.kind = trk_cast_type;
spelling->variant.typeref.type = type;
spelling->variant.typeref.extra_info->operator_type_arg = cast_type;
if (type_is(type, tk_typeref)) {
spelling->variant.typeref.is_dependent =
type->variant.typeref.is_dependent;
spelling->variant.typeref.is_nonreal =
type->variant.typeref.is_nonreal;
spelling->variant.typeref.is_prototype_instantiation =
type->variant.typeref.is_prototype_instantiation;
} /* if */
*type_slot = spelling;
} /* if */
} /* if */
#else /* !BACK_END_IS_CP_GEN_BE */
(void)source_form;
#endif /* BACK_END_IS_CP_GEN_BE */
if (expr_stack->possible_rescan_context &&
!is_error_node(expr)) {
if (is_operation_node(expr) &&
Expand Down Expand Up @@ -4715,12 +4744,18 @@ something, or when the original operand was not an expression (e.g.,
it was a constant), it can be NULL.
*/
{
if (expr_stack->possible_rescan_context) {
if (expr_stack->possible_rescan_context
#if BACK_END_IS_CP_GEN_BE
|| (source_form != csf_none && type_is(cast_type, tk_typeref) &&
is_any_reference_type(cast_type))
#endif /* BACK_END_IS_CP_GEN_BE */
) {
an_expr_node_ptr expr = find_primary_cast_node(orig_operand_expr,
source_form,
operand);
if (expr != NULL) {
record_cast_position_in_expr_rescan_info(expr, start_position,
record_cast_position_in_expr_rescan_info(expr, source_form,
start_position,
type_position, cast_type);
} /* if */
} /* if */
Expand Down Expand Up @@ -5637,7 +5672,9 @@ dynamic init that underlies the cast, or NULL if there isn't one.
orig_type = eriep->type;
} /* if */
} /* if */
/* Determine the result type for the cast. */
/* Substitute the semantic destination type. The collapsed-reference
spelling wrapper is dropped by type substitution, so a rescanned cast
cannot inherit an unsubstituted spelling from the prototype. */
check_assertion(eriep != NULL && eriep->type != NULL);
*cast_type = do_type_substitution_for_rescan(orig_type, rcblock, eriep);
if (actual_dip != NULL) *actual_dip = dip;
Expand Down Expand Up @@ -22394,7 +22431,12 @@ it might produce an error).
check_assertion(is_glvalue_node(node) || is_error_node(node));
/* Constant folding can be done only within the expression routines. */
check_assertion(constant_case == NULL || expr_stack != NULL);
/* Determine the type for the node after conversion to a prvalue. */
/* Determine the prvalue type. Save any cast spelling with the original
glvalue type, where it can still be used to reconstruct the cast. */
#if BACK_END_IS_CP_GEN_BE
a_type_ptr cast_type = is_cast_type_typeref(orig_type) ? orig_type : NULL;
orig_type = skip_cast_type_typeref(orig_type);
#endif /* BACK_END_IS_CP_GEN_BE */
if (is_function_type(orig_type)) {
/* Function designator (C) or function lvalue (C++): the conversion to
prvalue adds a "pointer to". */
Expand Down Expand Up @@ -23067,6 +23109,12 @@ it might produce an error).
node->orig_lvalue_type = orig_type;
} /* if */
} /* if */
#if BACK_END_IS_CP_GEN_BE
if (cast_type != NULL && node != NULL && is_cast_operation_node(node) &&
node->orig_lvalue_type == cast_type->variant.typeref.type) {
node->orig_lvalue_type = cast_type;
} /* if */
#endif /* BACK_END_IS_CP_GEN_BE */
release_local_constant(&result_con);
return node;
} /* conv_glvalue_expr_to_prvalue */
Expand Down
1 change: 1 addition & 0 deletions src/exprutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,7 @@ extern void record_typed_operator_position_in_expr_rescan_info(

extern void record_cast_position_in_expr_rescan_info(
an_expr_node_ptr expr,
a_cast_source_form source_form,
a_source_position *start_position,
a_source_position *type_position,
a_type_ptr cast_type);
Expand Down
5 changes: 4 additions & 1 deletion src/ifc_modules_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,10 @@ index for the type file.
a_type_ptr underlying_type = type->variant.typeref.type;

result = this->find_or_enter_type(underlying_type);
} else if (is_typeref_kind(type, trk_none)) {
} else if (is_typeref_kind(type, trk_none) ||
is_cast_type_typeref(type) ||
type_has_collapsed_reference_spelling(type)) {
/* The IFC needs only the underlying type for these typerefs. */
a_type_ptr underlying_type = type->variant.typeref.type;

result = this->find_or_enter_type(underlying_type);
Expand Down
16 changes: 15 additions & 1 deletion src/il.c
Original file line number Diff line number Diff line change
Expand Up @@ -21072,6 +21072,10 @@ options is a set of substitution options.
that preserves any incidental flags set on the operation, e.g.,
compiler_generated. */
expr_copy = copy_node(expr);
#if BACK_END_IS_CP_GEN_BE
/* The written destination can refer to the original scope. */
operation_type = skip_cast_type_typeref(operation_type);
#endif /* BACK_END_IS_CP_GEN_BE */
set_node_operator(expr_copy, op, operation_type,
expr->is_lvalue, new_operand_1);
copy_node_value_category(expr, expr_copy);
Expand Down Expand Up @@ -23462,6 +23466,16 @@ copied again.
} /* if */
/* Copy the top node. */
expr_copy = copy_node(expr);
#if BACK_END_IS_CP_GEN_BE
if ((options & (CE_COPYING_FROM_ONE_FUNC_TO_ANOTHER |
CE_DOING_INLINING_OF_FUNCTION_CALL)) ||
(in_file_scope(expr_copy) && !in_file_scope(expr))) {
/* The written destination can refer to entities in the original scope. */
expr_copy->type = skip_cast_type_typeref(expr_copy->type);
expr_copy->orig_lvalue_type =
skip_cast_type_typeref(expr_copy->orig_lvalue_type);
} /* if */
#endif /* BACK_END_IS_CP_GEN_BE */
if (options & (CE_COPYING_FOR_CONSTEXPR_FOLDING | CE_PRESERVE_RESCAN_INFO)) {
expr_copy->extra.rescan_info = expr->extra.rescan_info;
expr_copy->is_pack_expansion = expr->is_pack_expansion;
Expand Down Expand Up @@ -34145,7 +34159,7 @@ it can be used by back ends.
ref_type->variant.pointer.is_handle = TRUE;
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
} /* if */
ref_type->variant.pointer.type = dest_type;
ref_type->variant.pointer.type = skip_cast_type_typeref(dest_type);
} /* destination_type_for_reference_cast */


Expand Down
13 changes: 12 additions & 1 deletion src/il_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -9496,7 +9496,8 @@ typedef struct a_typeref_type_supplement {
use has been encountered. */
a_type_ptr operator_type_arg;
/* The type that originally appeared as the argument
to the operator. */
to the operator, or the written type recorded by a
cast or collapsed-reference typeref. */
int32_t min_template_arguments;
/* The minimum number of template arguments used to
refer to this instance in the source (using
Expand Down Expand Up @@ -9767,6 +9768,16 @@ enum a_typeref_kind : a_byte {
the type as written. */
trk_pack_index, /* A typeref representing a C++26 pack-index-specifier
(T...[N]). */
trk_collapsed_lvalue_reference,
trk_collapsed_rvalue_reference,
/* A type written with & or && on a
type that already denotes a reference. The
underlying type is the collapsed reference and
operator_type_arg records the written base type.
These typerefs do not represent type operators. */
trk_cast_type, /* A lexical typeref on a reference-cast expression.
The underlying type is the expression type and
operator_type_arg is the written destination. */
};

/*
Expand Down
7 changes: 7 additions & 0 deletions src/il_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -2262,7 +2262,14 @@ Display a typeref kind.
case trk_remove_volatile: str = "remove_volatile"; break;
case trk_template_arg_list: str = "template_arg_list"; break;
case trk_name_qualifier: str = "name_qualifier"; break;
case trk_cast_type: str = "cast_type"; break;
case trk_pack_index: str = "pack_index"; break;
case trk_collapsed_lvalue_reference:
str = "collapsed_lvalue_reference";
break;
case trk_collapsed_rvalue_reference:
str = "collapsed_rvalue_reference";
break;
default: str = "**BAD TYPEREF KIND**"; break;
} /* switch */
(void)fprintf(f_display, "%s\n", str);
Expand Down
Loading
Loading