Message ID | 20220512135523.2899969-1-ppalka@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13B663836427 for <patchwork@sourceware.org>; Thu, 12 May 2022 13:56:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 13B663836427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652363761; bh=Hc455fUYzVwV8riB9GNoyPaA739LBG6/74DA0yumjYA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=hfiJAVroNMmLTAZafHZsS6MxskutPNFr+p47+Az1TgZuLzen6qHlX/JrtdJJI669E 8tQWrEsauFCEyls5Xrc+l9/xiL6W+7NBcu6hVzn+RHupzQTA4L8yDwZwxKcjuohZWh WwHyYAH4Ayz8i5FcKfy7+tyZl4gComL6A65/aSbY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 6BD46384B0CC for <gcc-patches@gcc.gnu.org>; Thu, 12 May 2022 13:55:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6BD46384B0CC Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-649-vTUK4WmgNdW5notrMmENvA-1; Thu, 12 May 2022 09:55:27 -0400 X-MC-Unique: vTUK4WmgNdW5notrMmENvA-1 Received: by mail-qv1-f71.google.com with SMTP id s19-20020ad44b33000000b00456107e1120so4596583qvw.0 for <gcc-patches@gcc.gnu.org>; Thu, 12 May 2022 06:55:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Hc455fUYzVwV8riB9GNoyPaA739LBG6/74DA0yumjYA=; b=YrV1qRmfcZ2IRJHzlZGFAKbHT9Lj1dUADV+VTiKLIbLaqqncMeWoFD12Vz8jKI+G0d niif9LFUK7pBOkZ1fmWZn0LLTIFNfWsUTOQmHpycZk0bo2hPBCBnhGNHFb2agiHT9nvW /VoEyCvIblFIeHn6OiXXAW+XKazS28gy2juUByfrUsQWBZKQfCSq/mmLkIrRMjNgHfxq /ocYOO/IcQ0u1vTtDXJLSiRYLTA+Q/pKqideEZmiePoHrwJDpBgvdqOYMUVuzdYMN15+ cikgqLbzfvNk4SwoenLLuxgeOt1erSE5ntMYUUwm++GDHTBdVRWyAUzh7oETrctgDOJk oaSw== X-Gm-Message-State: AOAM5304lmSlHkSZivCVVR74mXw6dSKLZpj+0eZLRuA5E3fWZtb3xZJM VtfE1oBw+2DdpbjXDLOAHNwjZbb7lA/rgUZ4Q2UyIr/53A1t8QRklbxtPjkMeHzYWYzjYJG01Wz 2rOgj1mssmb+f4h9yq+1cwgFXHHULIGY+xOrrkrdsMb5vqPgkOK8HVWov7TKU67jYwHY= X-Received: by 2002:a37:68d7:0:b0:69f:fd74:7cbf with SMTP id d206-20020a3768d7000000b0069ffd747cbfmr22210632qkc.91.1652363726198; Thu, 12 May 2022 06:55:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx5cIHPN9b5ngYH0I1plL1ps1wJLFhuZ8klLlXu0wt3r15fHw1qqqmc3D8lp9BsvMpHuRKLQA== X-Received: by 2002:a37:68d7:0:b0:69f:fd74:7cbf with SMTP id d206-20020a3768d7000000b0069ffd747cbfmr22210591qkc.91.1652363725603; Thu, 12 May 2022 06:55:25 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id b64-20020a378043000000b0069fc13ce1eesm3313381qkd.31.2022.05.12.06.55.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 May 2022 06:55:25 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros Date: Thu, 12 May 2022 09:55:23 -0400 Message-Id: <20220512135523.2899969-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.1.63.gef9b086d95 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Patrick Palka via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Patrick Palka <ppalka@redhat.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
c++: improve a couple of TMPL_ARGS_* accessor macros
|
|
Commit Message
Patrick Palka
May 12, 2022, 1:55 p.m. UTC
After r13-332-g88459c3965e2a2, it looks like we can safely remove the NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its semantics. And TMPL_ARGS_LEVEL should verify the level argument is sane in the one-dimensional vector case. This change uncovered a couple of latent bugs: in try_class_unification, we weren't correctly copying multidimensional targs, and in unify_pack_expansion it seems an inequality test needs to be reversed (not sure about this latter fix, the test was added in r8-6178-g2625472ffa519e FWIW). Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? Also tested on cmcstl2 and range-v3. gcc/cp/ChangeLog: * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test. (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. * pt.cc (try_class_unification): Correctly copy multidimensional targs. (unify_pack_expansion): Fix level comparison. --- gcc/cp/cp-tree.h | 5 +++-- gcc/cp/pt.cc | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-)
Comments
On Thu, 12 May 2022, Patrick Palka wrote: > After r13-332-g88459c3965e2a2, it looks like we can safely remove the > NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its > semantics. > > And TMPL_ARGS_LEVEL should verify the level argument is sane in the > one-dimensional vector case. This change uncovered a couple of latent > bugs: in try_class_unification, we weren't correctly copying > multidimensional targs, and in unify_pack_expansion it seems an > inequality test needs to be reversed (not sure about this latter fix, > the test was added in r8-6178-g2625472ffa519e FWIW). > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? Also tested on cmcstl2 and range-v3. > > gcc/cp/ChangeLog: > > * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test. > (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when > TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. > * pt.cc (try_class_unification): Correctly copy multidimensional > targs. > (unify_pack_expansion): Fix level comparison. > --- > gcc/cp/cp-tree.h | 5 +++-- > gcc/cp/pt.cc | 12 ++++++------ > 2 files changed, 9 insertions(+), 8 deletions(-) > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index b6961a796af..f681d32ac93 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl { > /* Nonzero if the template arguments is actually a vector of vectors, > rather than just a vector. */ > #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ > - (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ > + (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ > && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) > > /* The depth of a template argument vector. When called directly by > @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl { > args is level 1, not level 0. */ > #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ > (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ > - ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS)) > + ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) \ > + : (gcc_checking_assert (LEVEL == 1), (ARGS))) > > /* Set the LEVELth level of the template ARGS to VAL. This macro does > not work with single-level argument vectors. */ > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 2c7c5f8bb5d..75b21e5c88a 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -23398,8 +23398,6 @@ static tree > try_class_unification (tree tparms, tree targs, tree parm, tree arg, > bool explain_p) > { > - tree copy_of_targs; > - > if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) > return NULL_TREE; > else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) > @@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg, > because there are two ways to unify base classes of S<0, 1, 2> > with S<I, I, I>. If we kept the already deduced knowledge, we > would reject the possibility I=1. */ > - copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); > + targs = copy_template_args (targs); > + for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) > + targ = NULL_TREE; It occurred to me that we don't need to make a copy of (the TREE_VECs for) the outer template arguments, so it might be better to use copy_node manually instead of copy_template_args. But this redundant copying shouldn't matter much if we make sure to ggc_free it afterwards. So here's v2 which additionally makes try_class_unification ggc_free this copy of targs (bootstrap and regtest in progress): -- >8 -- Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros After r13-332-g88459c3965e2a2, it looks like we can safely remove the NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which should make its uses easier to reason about. And TMPL_ARGS_LEVEL should verify the level argument is valid in the one-dimensional vector case. This change uncovered a couple of latent issues: in try_class_unification, we weren't correctly copying multidimensional targs, and in unify_pack_expansion it seems an inequality test needs to be reversed. This patch fixes both these issues, and in passing makes the former function ggc_free the copy of targs. gcc/cp/ChangeLog: * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test. (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. * pt.cc (try_class_unification): Correctly copy multidimensional targs. Free the copy of targs. (unify_pack_expansion): Fix level comparison. --- gcc/cp/cp-tree.h | 5 +++-- gcc/cp/pt.cc | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index b6961a796af..c28a3311dde 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl { /* Nonzero if the template arguments is actually a vector of vectors, rather than just a vector. */ #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ - (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ + (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) /* The depth of a template argument vector. When called directly by @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl { args is level 1, not level 0. */ #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ - ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS)) + ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) \ + : (gcc_checking_assert ((LEVEL) == 1), (ARGS))) /* Set the LEVELth level of the template ARGS to VAL. This macro does not work with single-level argument vectors. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 2c7c5f8bb5d..3df40f0d22f 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -23398,8 +23398,6 @@ static tree try_class_unification (tree tparms, tree targs, tree parm, tree arg, bool explain_p) { - tree copy_of_targs; - if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) return NULL_TREE; else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) @@ -23438,21 +23436,23 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg, because there are two ways to unify base classes of S<0, 1, 2> with S<I, I, I>. If we kept the already deduced knowledge, we would reject the possibility I=1. */ - copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); + targs = copy_template_args (targs); + for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) + targ = NULL_TREE; + int err; if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) - { - if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p)) - return NULL_TREE; - return arg; - } + err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p); + else + err = unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), + CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p); - /* If unification failed, we're done. */ - if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), - CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p)) - return NULL_TREE; + if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) + for (tree level : tree_vec_range (targs)) + ggc_free (level); + ggc_free (targs); - return arg; + return err ? NULL_TREE : arg; } /* Given a template type PARM and a class type ARG, find the unique @@ -23649,7 +23649,7 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms, /* Determine the index and level of this parameter pack. */ template_parm_level_and_index (parm_pack, &level, &idx); - if (level < levels) + if (level > levels) continue; /* Keep track of the parameter packs and their corresponding
On 5/12/22 14:14, Patrick Palka wrote: > On Thu, 12 May 2022, Patrick Palka wrote: > >> After r13-332-g88459c3965e2a2, it looks like we can safely remove the >> NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its >> semantics. >> >> And TMPL_ARGS_LEVEL should verify the level argument is sane in the >> one-dimensional vector case. This change uncovered a couple of latent >> bugs: in try_class_unification, we weren't correctly copying >> multidimensional targs, and in unify_pack_expansion it seems an >> inequality test needs to be reversed (not sure about this latter fix, >> the test was added in r8-6178-g2625472ffa519e FWIW). >> >> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for >> trunk? Also tested on cmcstl2 and range-v3. >> >> gcc/cp/ChangeLog: >> >> * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test. >> (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when >> TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. >> * pt.cc (try_class_unification): Correctly copy multidimensional >> targs. >> (unify_pack_expansion): Fix level comparison. >> --- >> gcc/cp/cp-tree.h | 5 +++-- >> gcc/cp/pt.cc | 12 ++++++------ >> 2 files changed, 9 insertions(+), 8 deletions(-) >> >> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h >> index b6961a796af..f681d32ac93 100644 >> --- a/gcc/cp/cp-tree.h >> +++ b/gcc/cp/cp-tree.h >> @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl { >> /* Nonzero if the template arguments is actually a vector of vectors, >> rather than just a vector. */ >> #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ >> - (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ >> + (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ >> && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) >> >> /* The depth of a template argument vector. When called directly by >> @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl { >> args is level 1, not level 0. */ >> #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ >> (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ >> - ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS)) >> + ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) \ >> + : (gcc_checking_assert (LEVEL == 1), (ARGS))) >> >> /* Set the LEVELth level of the template ARGS to VAL. This macro does >> not work with single-level argument vectors. */ >> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc >> index 2c7c5f8bb5d..75b21e5c88a 100644 >> --- a/gcc/cp/pt.cc >> +++ b/gcc/cp/pt.cc >> @@ -23398,8 +23398,6 @@ static tree >> try_class_unification (tree tparms, tree targs, tree parm, tree arg, >> bool explain_p) >> { >> - tree copy_of_targs; >> - >> if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) >> return NULL_TREE; >> else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) >> @@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg, >> because there are two ways to unify base classes of S<0, 1, 2> >> with S<I, I, I>. If we kept the already deduced knowledge, we >> would reject the possibility I=1. */ >> - copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); >> + targs = copy_template_args (targs); >> + for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) >> + targ = NULL_TREE; > > It occurred to me that we don't need to make a copy of (the TREE_VECs > for) the outer template arguments, so it might be better to use > copy_node manually instead of copy_template_args. But this redundant > copying shouldn't matter much if we make sure to ggc_free it afterwards. > So here's v2 which additionally makes try_class_unification ggc_free > this copy of targs (bootstrap and regtest in progress): > > -- >8 -- > > Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros > > After r13-332-g88459c3965e2a2, it looks like we can safely remove the > NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which should make its > uses easier to reason about. because we should have given up before trying to check that? > And TMPL_ARGS_LEVEL should verify the level argument is valid in the > one-dimensional vector case. This change uncovered a couple of latent > issues: in try_class_unification, we weren't correctly copying > multidimensional targs, and in unify_pack_expansion it seems an > inequality test needs to be reversed. This patch fixes both these > issues, and in passing makes the former function ggc_free the copy of > targs. OK. > gcc/cp/ChangeLog: > > * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test. > (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when > TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. > * pt.cc (try_class_unification): Correctly copy multidimensional > targs. Free the copy of targs. > (unify_pack_expansion): Fix level comparison. > --- > gcc/cp/cp-tree.h | 5 +++-- > gcc/cp/pt.cc | 28 ++++++++++++++-------------- > 2 files changed, 17 insertions(+), 16 deletions(-) > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index b6961a796af..c28a3311dde 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl { > /* Nonzero if the template arguments is actually a vector of vectors, > rather than just a vector. */ > #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ > - (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ > + (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ > && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) > > /* The depth of a template argument vector. When called directly by > @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl { > args is level 1, not level 0. */ > #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ > (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ > - ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS)) > + ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) \ > + : (gcc_checking_assert ((LEVEL) == 1), (ARGS))) > > /* Set the LEVELth level of the template ARGS to VAL. This macro does > not work with single-level argument vectors. */ > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 2c7c5f8bb5d..3df40f0d22f 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -23398,8 +23398,6 @@ static tree > try_class_unification (tree tparms, tree targs, tree parm, tree arg, > bool explain_p) > { > - tree copy_of_targs; > - > if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) > return NULL_TREE; > else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) > @@ -23438,21 +23436,23 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg, > because there are two ways to unify base classes of S<0, 1, 2> > with S<I, I, I>. If we kept the already deduced knowledge, we > would reject the possibility I=1. */ > - copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); > + targs = copy_template_args (targs); > + for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) > + targ = NULL_TREE; > > + int err; > if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) > - { > - if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p)) > - return NULL_TREE; > - return arg; > - } > + err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p); > + else > + err = unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), > + CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p); > > - /* If unification failed, we're done. */ > - if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), > - CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p)) > - return NULL_TREE; > + if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) > + for (tree level : tree_vec_range (targs)) > + ggc_free (level); > + ggc_free (targs); > > - return arg; > + return err ? NULL_TREE : arg; > } > > /* Given a template type PARM and a class type ARG, find the unique > @@ -23649,7 +23649,7 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms, > > /* Determine the index and level of this parameter pack. */ > template_parm_level_and_index (parm_pack, &level, &idx); > - if (level < levels) > + if (level > levels) > continue; > > /* Keep track of the parameter packs and their corresponding
On Thu, 12 May 2022, Jason Merrill wrote: > On 5/12/22 14:14, Patrick Palka wrote: > > On Thu, 12 May 2022, Patrick Palka wrote: > > > > > After r13-332-g88459c3965e2a2, it looks like we can safely remove the > > > NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its > > > semantics. > > > > > > And TMPL_ARGS_LEVEL should verify the level argument is sane in the > > > one-dimensional vector case. This change uncovered a couple of latent > > > bugs: in try_class_unification, we weren't correctly copying > > > multidimensional targs, and in unify_pack_expansion it seems an > > > inequality test needs to be reversed (not sure about this latter fix, > > > the test was added in r8-6178-g2625472ffa519e FWIW). > > > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > > > trunk? Also tested on cmcstl2 and range-v3. > > > > > > gcc/cp/ChangeLog: > > > > > > * cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test. > > > (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when > > > TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. > > > * pt.cc (try_class_unification): Correctly copy multidimensional > > > targs. > > > (unify_pack_expansion): Fix level comparison. > > > --- > > > gcc/cp/cp-tree.h | 5 +++-- > > > gcc/cp/pt.cc | 12 ++++++------ > > > 2 files changed, 9 insertions(+), 8 deletions(-) > > > > > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > > > index b6961a796af..f681d32ac93 100644 > > > --- a/gcc/cp/cp-tree.h > > > +++ b/gcc/cp/cp-tree.h > > > @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl { > > > /* Nonzero if the template arguments is actually a vector of vectors, > > > rather than just a vector. */ > > > #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ > > > - (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ > > > + (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ > > > && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) > > > /* The depth of a template argument vector. When called directly by > > > @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl { > > > args is level 1, not level 0. */ > > > #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ > > > (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ > > > - ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS)) > > > + ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) \ > > > + : (gcc_checking_assert (LEVEL == 1), (ARGS))) > > > /* Set the LEVELth level of the template ARGS to VAL. This macro does > > > not work with single-level argument vectors. */ > > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > > > index 2c7c5f8bb5d..75b21e5c88a 100644 > > > --- a/gcc/cp/pt.cc > > > +++ b/gcc/cp/pt.cc > > > @@ -23398,8 +23398,6 @@ static tree > > > try_class_unification (tree tparms, tree targs, tree parm, tree arg, > > > bool explain_p) > > > { > > > - tree copy_of_targs; > > > - > > > if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) > > > return NULL_TREE; > > > else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) > > > @@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, > > > tree parm, tree arg, > > > because there are two ways to unify base classes of S<0, 1, 2> > > > with S<I, I, I>. If we kept the already deduced knowledge, we > > > would reject the possibility I=1. */ > > > - copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); > > > + targs = copy_template_args (targs); > > > + for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) > > > + targ = NULL_TREE; > > > > It occurred to me that we don't need to make a copy of (the TREE_VECs > > for) the outer template arguments, so it might be better to use > > copy_node manually instead of copy_template_args. But this redundant > > copying shouldn't matter much if we make sure to ggc_free it afterwards. > > So here's v2 which additionally makes try_class_unification ggc_free > > this copy of targs (bootstrap and regtest in progress): > > > > -- >8 -- > > > > Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros > > > > After r13-332-g88459c3965e2a2, it looks like we can safely remove the > > NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which should make its > > uses easier to reason about. > > because we should have given up before trying to check that? *nod* I was going to mention that in particular it makes TMPL_ARGS_LEVEL (NULL, 1) invalid instead of yielding NULL, but on second thought yielding NULL kind of makes sense -- in some other places we already treat NULL args like an empty TREE_VEC, so it seems reasonable that TMPL_ARGS_LEVEL acts like the identity function for both NULL and an empty TREE_VEC. So I'm not too sure anymore that removing the NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS is the way to go; I'm going to remove this part of the patch, which is a non-functional change anyway. > > > And TMPL_ARGS_LEVEL should verify the level argument is valid in the > > one-dimensional vector case. This change uncovered a couple of latent > > issues: in try_class_unification, we weren't correctly copying > > multidimensional targs, and in unify_pack_expansion it seems an > > inequality test needs to be reversed. This patch fixes both these > > issues, and in passing makes the former function ggc_free the copy of > > targs. > > OK. Thanks, here's what I ultimately committed: -- >8 -- Subject: [PATCH] c++: tighten TMPL_ARGS_LEVEL macro This patch makes TMPL_ARGS_LEVEL verify the level argument is valid in the one-dimensional vector case. Doing so uncovered a couple of latent issues: in try_class_unification, we weren't correctly copying targs when it's two-dimensional, and in unify_pack_expansion it seems an inequality test needs to be reversed. This patch fixes both issues, and in passing makes the former function free the temporary copy of targs. gcc/cp/ChangeLog: * cp-tree.h (TMPL_ARGS_LEVEL): Assert LEVEL is 1 when TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false. * pt.cc (try_class_unification): Correctly copy multidimensional targs. Free the copy of targs. (unify_pack_expansion): Fix level comparison. --- gcc/cp/cp-tree.h | 3 ++- gcc/cp/pt.cc | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index b6961a796af..b2df6fc0ad4 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl { args is level 1, not level 0. */ #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ - ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS)) + ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) \ + : (gcc_checking_assert ((LEVEL) == 1), (ARGS))) /* Set the LEVELth level of the template ARGS to VAL. This macro does not work with single-level argument vectors. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 2c7c5f8bb5d..3df40f0d22f 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -23398,8 +23398,6 @@ static tree try_class_unification (tree tparms, tree targs, tree parm, tree arg, bool explain_p) { - tree copy_of_targs; - if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) return NULL_TREE; else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) @@ -23438,21 +23436,23 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg, because there are two ways to unify base classes of S<0, 1, 2> with S<I, I, I>. If we kept the already deduced knowledge, we would reject the possibility I=1. */ - copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); + targs = copy_template_args (targs); + for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) + targ = NULL_TREE; + int err; if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) - { - if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p)) - return NULL_TREE; - return arg; - } + err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p); + else + err = unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), + CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p); - /* If unification failed, we're done. */ - if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), - CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p)) - return NULL_TREE; + if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs)) + for (tree level : tree_vec_range (targs)) + ggc_free (level); + ggc_free (targs); - return arg; + return err ? NULL_TREE : arg; } /* Given a template type PARM and a class type ARG, find the unique @@ -23649,7 +23649,7 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms, /* Determine the index and level of this parameter pack. */ template_parm_level_and_index (parm_pack, &level, &idx); - if (level < levels) + if (level > levels) continue; /* Keep track of the parameter packs and their corresponding
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index b6961a796af..f681d32ac93 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl { /* Nonzero if the template arguments is actually a vector of vectors, rather than just a vector. */ #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \ - (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ + (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0) \ && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC) /* The depth of a template argument vector. When called directly by @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl { args is level 1, not level 0. */ #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \ (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \ - ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS)) + ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) \ + : (gcc_checking_assert (LEVEL == 1), (ARGS))) /* Set the LEVELth level of the template ARGS to VAL. This macro does not work with single-level argument vectors. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 2c7c5f8bb5d..75b21e5c88a 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -23398,8 +23398,6 @@ static tree try_class_unification (tree tparms, tree targs, tree parm, tree arg, bool explain_p) { - tree copy_of_targs; - if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg)) return NULL_TREE; else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) @@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg, because there are two ways to unify base classes of S<0, 1, 2> with S<I, I, I>. If we kept the already deduced knowledge, we would reject the possibility I=1. */ - copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs)); + targs = copy_template_args (targs); + for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs))) + targ = NULL_TREE; if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM) { - if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p)) + if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p)) return NULL_TREE; return arg; } /* If unification failed, we're done. */ - if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm), + if (unify (tparms, targs, CLASSTYPE_TI_ARGS (parm), CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p)) return NULL_TREE; @@ -23649,7 +23649,7 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms, /* Determine the index and level of this parameter pack. */ template_parm_level_and_index (parm_pack, &level, &idx); - if (level < levels) + if (level > levels) continue; /* Keep track of the parameter packs and their corresponding