From patchwork Wed May 11 19:20:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 53818 Return-Path: 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 274D9383D80C for ; Wed, 11 May 2022 19:20:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 274D9383D80C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652296843; bh=n07J1VB0gPg+oKhn+Wa9FPiMqJEuHwEF+rqsu56Gw3Q=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=wEkxSp6SccGPlthYaoGHE2BLQ3ZfzI85PEeCowW9Ui0CzY51sUVREOKLDL6c9D/lB 1W0XmTKim4oBrTac6BHgUu4yEUkWKbwWvxIjJCZacJvHqWh1x8hS+LzcRuOHlU1RPg pAkWwMG4nUatgOhXPnhbwxutxNNb1ypd81kMFaR0= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 29D77384F02D for ; Wed, 11 May 2022 19:20:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 29D77384F02D Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-522-k8KgyUlPPmG2J7VZeo8WyA-1; Wed, 11 May 2022 15:20:11 -0400 X-MC-Unique: k8KgyUlPPmG2J7VZeo8WyA-1 Received: by mail-qv1-f72.google.com with SMTP id a12-20020a056214062c00b0045a7b4a1a29so2747959qvx.10 for ; Wed, 11 May 2022 12:20:11 -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=n07J1VB0gPg+oKhn+Wa9FPiMqJEuHwEF+rqsu56Gw3Q=; b=ZLaW06v8B4Xd1CSY/41Vn17eWSsoGIOrbo9SW+PZY9r6jjr9Ac2BrAhfiAGFMRduKS xgPo67fwDfHk6xN4DgyurjUx77hlp+cJZ2dCRAs6Z61OZUdiJBHtTRQ14twX8qWScYUt WFrhk5EN4e7f1dm0FMCV+RaoXzT27N9XJqRb3GFdaLF7qSX2X930UHhj7OVMapYAL6eP KXvTs7yEqLpfEp7mdsTeVXeMuAVCXwwsy4z3d+hv6GLJY33kpNasIPuTwH26NqYHQBd0 jF+ok9aLjsPDR8xIDWoOoTWCSlTvQKWghSWjCVpsMMSsuVWQJXEjgmD70FSxpVK7+Zeq ZEag== X-Gm-Message-State: AOAM532wAEsg529mdQbelVgxuyVE+Fnyhee4Y2uEAmqokNAJkqa965Br FVNJAiA/LnHLQXHm8nSgDWNCF85zCztxTSg0jf+FgxT1JZ5RThkQ0OZP2k5iDXUzgNtpqKECGQm nv4gab090evdShNXMs0jtLwJUGyHn4ezMtvHftLHjj6a3O42Tf586RHtaDZXg7h/FMmg= X-Received: by 2002:ac8:7d08:0:b0:2f3:ebfa:e966 with SMTP id g8-20020ac87d08000000b002f3ebfae966mr6896238qtb.220.1652296810717; Wed, 11 May 2022 12:20:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+fr0jkHm3/NMETyqef60FxR9GmAlgX4n+LYvBRSb7mQugMOsBdoWomRqlD0LTexvxjk4CHg== X-Received: by 2002:ac8:7d08:0:b0:2f3:ebfa:e966 with SMTP id g8-20020ac87d08000000b002f3ebfae966mr6896199qtb.220.1652296810319; Wed, 11 May 2022 12:20:10 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id 191-20020a3703c8000000b0069fc13ce240sm1654110qkd.113.2022.05.11.12.20.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 12:20:10 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree: introduce range adaptor for TREE_VEC Date: Wed, 11 May 2022 15:20:08 -0400 Message-Id: <20220511192008.1917896-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.0.63.gf5aaf72f1b MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch implements a simple tree wrapper, named tree_vec_range, which lets us idiomatically iterate over all the elements of a TREE_VEC using a C++11 range-based for loop: // v is a TREE_VEC for (tree e : tree_vec_range (v)) ... This is similar to the existing tree-based range adaptors ovl_range and lkp_range added to the C++ frontend by r12-340-g3307b9a07a3c51. This patch also converts some existing loops over TREE_VEC within the C++ frontend to use tree_vec_range and range-for. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look useful? gcc/cp/ChangeLog: * constraint.cc (tsubst_parameter_mapping): Convert loop over TREE_VEC into a range-based for loop using tree_vec_range. * pt.cc (iterative_hash_template_arg): Likewise. (template_parms_level_to_args): Likewise (and simplify). (deducible_template_args): Likewise. (check_undeduced_parms): Likewise. (dependent_type_p_r): Likewise. (value_dependent_expression_p) : Likewise. (dependent_template_arg_p): Likewise. * tree.cc (cp_walk_subtrees) : Likewise. gcc/ChangeLog: * tree.h (TREE_VEC_BEGIN): Define. (TREE_VEC_END): Correct access of length member. (class tree_vec_range): Define. --- gcc/cp/constraint.cc | 9 +++---- gcc/cp/pt.cc | 57 +++++++++++++++++--------------------------- gcc/cp/tree.cc | 5 ++-- gcc/tree.h | 15 +++++++++++- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index 94f6222b436..591155cee22 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -2348,12 +2348,9 @@ tsubst_parameter_mapping (tree map, tree args, subst_info info) if (TREE_CODE (new_arg) == TYPE_ARGUMENT_PACK) { tree pack_args = ARGUMENT_PACK_ARGS (new_arg); - for (int i = 0; i < TREE_VEC_LENGTH (pack_args); i++) - { - tree& pack_arg = TREE_VEC_ELT (pack_args, i); - if (TYPE_P (pack_arg)) - pack_arg = canonicalize_type_argument (pack_arg, complain); - } + for (tree& pack_arg : tree_vec_range (pack_args)) + if (TYPE_P (pack_arg)) + pack_arg = canonicalize_type_argument (pack_arg, complain); } if (new_arg == error_mark_node) return error_mark_node; diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 9932d861af6..2c7c5f8bb5d 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -1811,8 +1811,8 @@ iterative_hash_template_arg (tree arg, hashval_t val) return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val); case TREE_VEC: - for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i) - val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val); + for (tree elt : tree_vec_range (arg)) + val = iterative_hash_template_arg (elt, val); return val; case TYPE_PACK_EXPANSION: @@ -4883,15 +4883,15 @@ template_parm_to_arg (tree t) tree template_parms_level_to_args (tree parms) { - tree a = copy_node (parms); - TREE_TYPE (a) = NULL_TREE; - for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i) - TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i)); + parms = copy_node (parms); + TREE_TYPE (parms) = NULL_TREE; + for (tree& parm : tree_vec_range (parms)) + parm = template_parm_to_arg (parm); if (CHECKING_P) - SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a)); + SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (parms, TREE_VEC_LENGTH (parms)); - return a; + return parms; } /* Given a set of template parameters, return them as a set of template @@ -22470,10 +22470,9 @@ deducible_array_bound (tree domain) static bool deducible_template_args (tree args) { - for (int i = 0; i < TREE_VEC_LENGTH (args); ++i) + for (tree elt : tree_vec_range (args)) { bool deducible; - tree elt = TREE_VEC_ELT (args, i); if (ARGUMENT_PACK_P (elt)) deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt)); else @@ -24844,12 +24843,11 @@ static bool check_undeduced_parms (tree targs, tree args, tree end) { bool found = false; - int i; - for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i) - if (TREE_VEC_ELT (targs, i) == NULL_TREE) + for (tree& targ : tree_vec_range (targs)) + if (targ == NULL_TREE) { found = true; - TREE_VEC_ELT (targs, i) = error_mark_node; + targ = error_mark_node; } if (found) { @@ -27288,10 +27286,9 @@ dependent_type_p_r (tree type) if (TREE_CODE (type) == TYPE_ARGUMENT_PACK) { tree args = ARGUMENT_PACK_ARGS (type); - int i, len = TREE_VEC_LENGTH (args); - for (i = 0; i < len; ++i) - if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) - return true; + for (tree arg : tree_vec_range (args)) + if (dependent_template_arg_p (arg)) + return true; } /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must @@ -27574,16 +27571,10 @@ value_dependent_expression_p (tree expression) case NONTYPE_ARGUMENT_PACK: /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument is value-dependent. */ - { - tree values = ARGUMENT_PACK_ARGS (expression); - int i, len = TREE_VEC_LENGTH (values); - - for (i = 0; i < len; ++i) - if (value_dependent_expression_p (TREE_VEC_ELT (values, i))) - return true; - - return false; - } + for (tree arg : tree_vec_range (ARGUMENT_PACK_ARGS (expression))) + if (value_dependent_expression_p (arg)) + return true; + return false; case TRAIT_EXPR: { @@ -28209,13 +28200,9 @@ dependent_template_arg_p (tree arg) else if (ARGUMENT_PACK_P (arg)) { tree args = ARGUMENT_PACK_ARGS (arg); - int i, len = TREE_VEC_LENGTH (args); - for (i = 0; i < len; ++i) - { - if (dependent_template_arg_p (TREE_VEC_ELT (args, i))) - return true; - } - + for (tree arg : tree_vec_range (args)) + if (dependent_template_arg_p (arg)) + return true; return false; } else if (TYPE_P (arg)) diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 633cc164a3a..bc38c8fbdbe 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -5414,9 +5414,8 @@ cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func, case NONTYPE_ARGUMENT_PACK: { tree args = ARGUMENT_PACK_ARGS (*tp); - int i, len = TREE_VEC_LENGTH (args); - for (i = 0; i < len; i++) - WALK_SUBTREE (TREE_VEC_ELT (args, i)); + for (tree arg : tree_vec_range (args)) + WALK_SUBTREE (arg); } break; diff --git a/gcc/tree.h b/gcc/tree.h index 82eb8ba39d2..0c12c60ee7e 100644 --- a/gcc/tree.h +++ b/gcc/tree.h @@ -1107,8 +1107,9 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int, /* In a TREE_VEC node. */ #define TREE_VEC_LENGTH(NODE) (TREE_VEC_CHECK (NODE)->base.u.length) +#define TREE_VEC_BEGIN(NODE) (&TREE_VEC_CHECK (NODE)->vec.a[0]) #define TREE_VEC_END(NODE) \ - ((void) TREE_VEC_CHECK (NODE), &((NODE)->vec.a[(NODE)->vec.base.u.length])) + ((void) TREE_VEC_CHECK (NODE), &((NODE)->vec.a[(NODE)->base.u.length])) #define TREE_VEC_ELT(NODE,I) TREE_VEC_ELT_CHECK (NODE, I) @@ -4481,6 +4482,18 @@ extern tree make_tree_vec (int CXX_MEM_STAT_INFO); extern tree grow_tree_vec (tree v, int CXX_MEM_STAT_INFO); +/* Treat a TREE_VEC as an range of trees, e.g. + for (tree e : tree_vec_range (v)) { ... } */ + +class tree_vec_range +{ + tree v; +public: + tree_vec_range(tree v) : v(v) { } + tree *begin() { return TREE_VEC_BEGIN (v); } + tree *end() { return TREE_VEC_END (v); } +}; + /* Construct various types of nodes. */ extern tree build_nt (enum tree_code, ...);