c++: merge tsubst_copy into tsubst_copy_and_build

The relationship between tsubst_copy_and_build and tsubst_copy (two of
the main template argument substitution routines for expression trees)
is rather hazy.  The former is mostly a superset of the latter, with
some differences.

The main apparent difference is their handling of various tree codes,
but much of the tree code handling in tsubst_copy appears to be dead
code.  This is because tsubst_copy mostly gets (directly) called on
id-expressions rather than on arbitrary expressions.  The interesting
tree codes are PARM_DECL, VAR_DECL, BIT_NOT_EXPR, SCOPE_REF,
TEMPLATE_ID_EXPR and IDENTIFIER_NODE:

 * for PARM_DECL and VAR_DECL, tsubst_copy_and_build calls tsubst_copy
   followed by doing some extra handling of its own
 * for BIT_NOT_EXPR tsubst_copy implicitly handles unresolved destructor
   calls (i.e. the first operand is an identifier or a type)
 * for SCOPE_REF, TEMPLATE_ID_EXPR and IDENTIFIER_NODE tsubst_copy
   refrains from doing name lookup of the terminal name

Other more minor differences are that tsubst_copy exits early when
'args' is null, and it calls maybe_dependent_member_ref, and finally
it dispatches to tsubst for type trees.[1]

Thus tsubst_copy is similar enough to tsubst_copy_and_build that it
makes sense to merge the two functions, with the main difference we
want to preserve is tsubst_copy's lack of name lookup for id-expressions.
This patch achieves this via a new tsubst flag tf_no_name_lookup which
controls name lookup and resolution of a (top-level) id-expression.

[1]: Exiting early for null 'args' doesn't seem right since it means we
return templated trees even when !processing_template_decl.  And
dispatching to tsubst for type trees muddles the distinction between
type and expressions which makes things less clear at the call site.
So these properties of tsubst_copy don't seem worth preserving.

N.B. the diff for this patch looks much cleaner when generated using
the "patience diff" algorithm via Git's --patience flag.

gcc/cp/ChangeLog:

	* cp-tree.h (enum tsubst_flags): Add tf_no_name_lookup.
	* pt.cc (tsubst_pack_expansion): Use tsubst for substituting
	BASES_TYPE.
	(tsubst_decl) <case USING_DECL>: Use tsubst_name instead of
	tsubst_copy.
	(tsubst) <case TEMPLATE_TYPE_PARM>: Use tsubst_copy_and_build
	instead of tsubst_copy for substituting
	CLASS_PLACEHOLDER_TEMPLATE.
	<case TYPENAME_TYPE>: Use tsubst_name instead of tsubst_copy for
	substituting TYPENAME_TYPE_FULLNAME.
	(tsubst_name): Define.
	(tsubst_qualified_id): Use tsubst_name instead of tsubst_copy
	for substituting the component name of a SCOPE_REF.
	(tsubst_copy): Remove.
	(tsubst_copy_and_build): Clear tf_no_name_lookup at the start,
	and remember if it was set.  Call maybe_dependent_member_ref if
	tf_no_name_lookup was not set.
	<case IDENTIFIER_NODE>: Don't do name lookup if tf_no_name_lookup
	was set.
	<case TEMPLATE_ID_EXPR>: If tf_no_name_lookup was set, use
	tsubst_name instead of tsubst_copy_and_build to substitute the
	template and don't finish the template-id.
	<case BIT_NOT_EXPR>: Handle identifier and type operand (if
	tf_no_name_lookup was set).
	<case SCOPE_REF>: Avoid trying to resolve a SCOPE_REF if
	tf_no_name_lookup was set by calling build_qualified_name directly
	instead of tsubst_qualified_id.
	<case SIZEOF_EXPR>: Handling of sizeof...  copied from tsubst_copy.
	<case CALL_EXPR>: Use tsubst_name instead of tsubst_copy to
	substitute a TEMPLATE_ID_EXPR callee naming an unresolved template.
	<case COMPONENT_REF>: Likewise to substitute the member.
	<case FUNCTION_DECL>: Copied from tsubst_copy and merged with ...
	<case VAR_DECL, PARM_DECL>: ... these.  Initial handling copied
	from tsubst_copy.  Optimize local variable substitution by
	trying retrieve_local_specialization before checking
	uses_template_parms.
	<case CONST_DECL>: Copied from tsubst_copy.
	<case FIELD_DECL>: Likewise.
	<case NAMESPACE_DECL>: Likewise.
	<case OVERLOAD>: Likewise.
	<case TEMPLATE_DECL>: Likewise.
	<case TEMPLATE_PARM_INDEX>: Likewise.
	<case TYPE_DECL>: Likewise.
	<case CLEANUP_POINT_EXPR>: Likewise.
	<case OFFSET_REF>: Likewise.
	<case EXPR_PACK_EXPANSION>: Likewise.
	<case NONTYPE_ARGUMENT_PACK>: Likewise.
	<case *_CST>: Likewise.
	<case *_*_FOLD_EXPR>: Likewise.
	<case DEBUG_BEGIN_STMT>: Likewise.
	<case CO_AWAIT_EXPR>: Likewise.
	<case TRAIT_EXPR>: Use tsubst and tsubst_copy_and_build instead
	of tsubst_copy.
	<default>: Copied from tsubst_copy.
	(tsubst_initializer_list): Use tsubst and tsubst_copy_and_build
	instead of tsubst_copy.

Reviewed-by: Jason Merrill <jason@redhat.com>
This commit is contained in:
Patrick Palka 2023-10-20 11:21:54 -04:00
parent 909672f02f
commit 3e3d73ed5e
2 changed files with 444 additions and 743 deletions

View file

@ -5621,6 +5621,9 @@ enum tsubst_flags {
tf_qualifying_scope = 1 << 14, /* Substituting the LHS of the :: operator.
Affects TYPENAME_TYPE resolution from
make_typename_type. */
tf_no_name_lookup = 1 << 15, /* Don't look up the terminal name of an
outermost id-expression, or resolve its
constituent template-ids or qualified-ids. */
/* Convenient substitution flags combinations. */
tf_warning_or_error = tf_warning | tf_error
};

File diff suppressed because it is too large Load diff