From 3e3d73ed5e85e7f467c366e9ad219d558ef9cb79 Mon Sep 17 00:00:00 2001 From: Patrick Palka Date: Fri, 20 Oct 2023 11:21:54 -0400 Subject: 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) : Use tsubst_name instead of tsubst_copy. (tsubst) : Use tsubst_copy_and_build instead of tsubst_copy for substituting CLASS_PLACEHOLDER_TEMPLATE. : 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. : Don't do name lookup if tf_no_name_lookup was set. : 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. : Handle identifier and type operand (if tf_no_name_lookup was set). : 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. : Handling of sizeof... copied from tsubst_copy. : Use tsubst_name instead of tsubst_copy to substitute a TEMPLATE_ID_EXPR callee naming an unresolved template. : Likewise to substitute the member. : Copied from tsubst_copy and merged with ... : ... these. Initial handling copied from tsubst_copy. Optimize local variable substitution by trying retrieve_local_specialization before checking uses_template_parms. : Copied from tsubst_copy. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Likewise. : Use tsubst and tsubst_copy_and_build instead of tsubst_copy. : Copied from tsubst_copy. (tsubst_initializer_list): Use tsubst and tsubst_copy_and_build instead of tsubst_copy. Reviewed-by: Jason Merrill --- gcc/cp/cp-tree.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'gcc/cp/cp-tree.h') diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index ff3323b..d6abd15 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -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 }; -- cgit v1.1