From patchwork Wed Sep 6 13:17:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathan Sidwell X-Patchwork-Id: 810580 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-461609-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="F2YT6OJa"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3xnPK71zlqz9t43 for ; Wed, 6 Sep 2017 23:18:03 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=FVzl1Xbce6lctaezrcz/gvzyXxIZzeufUlhIac1CRjHekvaCEX AUv5/m/BvjuJXNHHDJ6/z1iWxyvy7z4ZFqOSdIaa5jdwmk6mQ2DdCgoPzprFGCmM mIoMpemxnDCBlOnr/hjHCsNTCU1akEc4t+QkDGG8uGdtptl+7DG8ALwXA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=cB3a4nsF9Cxd/w4uNL7crtcAVLw=; b=F2YT6OJatx0nadQ2OaiZ UXnJo0p9OCLXf6nIUT07Sm2EDrpDurmazkr5ZXMxkqU4ujnQquPZtpBesEEqZmlX 1KifO7DBfW52WbCtmi4OyhYVRvdZS5eqSZu7zE38aSQTm2/Pfr9o/jM80THe75jo IXZRMhok+HfTOVmRzmXTR34= Received: (qmail 8645 invoked by alias); 6 Sep 2017 13:17:35 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 8303 invoked by uid 89); 6 Sep 2017 13:17:35 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.4 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=Functions X-HELO: mail-yw0-f181.google.com Received: from mail-yw0-f181.google.com (HELO mail-yw0-f181.google.com) (209.85.161.181) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 06 Sep 2017 13:17:24 +0000 Received: by mail-yw0-f181.google.com with SMTP id r85so6413139ywg.1 for ; Wed, 06 Sep 2017 06:17:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:to:from:subject:message-id:date :user-agent:mime-version:content-language; bh=G94bSfZ9c/6CW72PHzd2k8UjtPy81fTwMEvPVHGfw2A=; b=i27JCekszwePSVSDU9DHyVT1cxeq83XQ+FNcWHs4v2Z5bGMhFDqRWA3d9WlMr1sG1X XLv9UqsUd6KApajs5LFb0jD5axqF2WU0ZPrQZ8n6DVk5PNKGGsR7fWu/Od9Qf0xMr31I npwybKfUvHTd37t0/A95y+t5XSJfUaSYHYjYi3OsG45yKinK7K7xq/BqxVliqXjPGByQ 01VWDo0VSvFnfb8nM4J9OQlXxvWc4Zp71J21YQl8e0L7oohe/9SKTDghM40dnPT/UwlI AJ7PE4d9tWklgE+Hme3oQQQ902JunK9xwrE0+Cc3AKT/vow8CgTS6CeYNY1DH+eVAUbR Su6g== X-Gm-Message-State: AHPjjUgNrupgMJ3MOqFFZnx8iD0jivmbZWEkE4Sg9ZqBPEUGL9Ez/Csx jxf9lW1+P2jUww== X-Google-Smtp-Source: ADKCNb6zyYzvOYtmOahvhN8UcRBHEBBMMwV6/BXUkkWbERlQcCr3ik9I3vgU6wX+eHS3A51/Vhh30A== X-Received: by 10.13.248.5 with SMTP id i5mr1928725ywf.130.1504703842937; Wed, 06 Sep 2017 06:17:22 -0700 (PDT) Received: from ?IPv6:2620:10d:c0a3:20fb:7500:e7fb:4a6f:2254? ([2620:10d:c091:200::16fd]) by smtp.googlemail.com with ESMTPSA id a81sm1089775ywc.6.2017.09.06.06.17.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Sep 2017 06:17:21 -0700 (PDT) To: GCC Patches From: Nathan Sidwell Subject: [C++ PATCH] class FIELD_VEC initialization Message-ID: <613620bc-e929-446c-f307-74daa97d6f24@acm.org> Date: Wed, 6 Sep 2017 09:17:20 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 Here's some cleanup of the SORTED_FIELDS vector initialization. Some function renaming, to be more specific. The functionality change is a minor bug in late enums. We only add them to the field vec, if there's already a field vec. But of course, their addition could have cause the class's TYPE_FIELD length to cross the threshold for wanting a field vector. Applied to trunk. nathan 2017-09-06 Nathan Sidwell * name-lookup.c (count_fields): Rename to ... (count_class_fields): ... here. Take a class, don't count NULL-named fields. (add_fields_to_record_type): Rename to ... (field_vec_append_class_fields): ... here. Take a class, don't add NULL-named fields. (add_enum_fields_to_record_type): Rename to ... (field_vec_append_enum_values): ... here. (set_class_bindings): Adjust, assert we added expected number. (insert_late_enum_def_bindings): Reimplement. Create vector if there are now sufficient entries. Index: name-lookup.c =================================================================== --- name-lookup.c (revision 251782) +++ name-lookup.c (working copy) @@ -1452,59 +1452,57 @@ sorted_fields_type_new (int n) return sft; } -/* Subroutine of insert_into_classtype_sorted_fields. Recursively - count the number of fields in TYPE, including anonymous union - members. */ +/* Recursively count the number of fields in KLASS, including anonymous + union members. */ -static int -count_fields (tree fields) +static unsigned +count_class_fields (tree klass) { - tree x; - int n_fields = 0; - for (x = fields; x; x = DECL_CHAIN (x)) - { - if (DECL_DECLARES_FUNCTION_P (x)) - /* Functions are dealt with separately. */; - else if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) - n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x))); - else - n_fields += 1; - } + unsigned n_fields = 0; + + for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) + if (DECL_DECLARES_FUNCTION_P (fields)) + /* Functions are dealt with separately. */; + else if (TREE_CODE (fields) == FIELD_DECL + && ANON_AGGR_TYPE_P (TREE_TYPE (fields))) + n_fields += count_class_fields (TREE_TYPE (fields)); + else if (DECL_NAME (fields)) + n_fields += 1; + return n_fields; } -/* Subroutine of insert_into_classtype_sorted_fields. Recursively add - all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE - elts, starting at offset IDX. */ - -static int -add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, - int idx) +/* Append all the nonfunction members fields of KLASS to FIELD_VEC + starting at IDX. Recurse for anonymous members. The array must + have space. Returns the next available index. */ + +static unsigned +field_vec_append_class_fields (struct sorted_fields_type *field_vec, + tree klass, unsigned idx) { - tree x; - for (x = fields; x; x = DECL_CHAIN (x)) - { - if (DECL_DECLARES_FUNCTION_P (x)) - /* Functions are handled separately. */; - else if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) - idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx); - else - field_vec->elts[idx++] = x; - } + for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) + if (DECL_DECLARES_FUNCTION_P (fields)) + /* Functions are handled separately. */; + else if (TREE_CODE (fields) == FIELD_DECL + && ANON_AGGR_TYPE_P (TREE_TYPE (fields))) + idx = field_vec_append_class_fields (field_vec, TREE_TYPE (fields), idx); + else if (DECL_NAME (fields)) + field_vec->elts[idx++] = fields; + return idx; } -/* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts, - starting at offset IDX. */ +/* Append all of the enum values of ENUMTYPE to FIELD_VEC starting at IDX. + FIELD_VEC must have space. */ -static int -add_enum_fields_to_record_type (tree enumtype, - struct sorted_fields_type *field_vec, - int idx) +static unsigned +field_vec_append_enum_values (struct sorted_fields_type *field_vec, + tree enumtype, unsigned idx) { - tree values; - for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) + for (tree values = TYPE_VALUES (enumtype); + values; values = TREE_CHAIN (values)) field_vec->elts[idx++] = TREE_VALUE (values); + return idx; } @@ -1518,12 +1516,12 @@ set_class_bindings (tree klass) qsort (method_vec->address (), method_vec->length (), sizeof (tree), method_name_cmp); - tree fields = TYPE_FIELDS (klass); - int n_fields = count_fields (fields); + int n_fields = count_class_fields (klass); if (n_fields >= 8) { struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields); - add_fields_to_record_type (fields, field_vec, 0); + unsigned idx = field_vec_append_class_fields (field_vec, klass, 0); + gcc_assert (idx == unsigned (field_vec->len)); qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp); CLASSTYPE_SORTED_FIELDS (klass) = field_vec; } @@ -1534,19 +1532,31 @@ set_class_bindings (tree klass) void insert_late_enum_def_bindings (tree klass, tree enumtype) { + unsigned n_fields; struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (klass); + + /* The enum values will already be on the TYPE_FIELDS, so don't + count them twice. */ if (sorted_fields) + n_fields = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len; + else + n_fields = count_class_fields (klass); + + if (n_fields >= 8) { - int i; - int n_fields - = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len; struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields); - - for (i = 0; i < sorted_fields->len; ++i) - field_vec->elts[i] = sorted_fields->elts[i]; + unsigned idx; + + if (sorted_fields) + { + for (idx = 0; idx < unsigned (sorted_fields->len); ++idx) + field_vec->elts[idx] = sorted_fields->elts[idx]; - add_enum_fields_to_record_type (enumtype, field_vec, - sorted_fields->len); + idx = field_vec_append_enum_values (field_vec, enumtype, idx); + } + else + idx = field_vec_append_class_fields (field_vec, klass, 0); + gcc_assert (idx == unsigned (field_vec->len)); qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp); CLASSTYPE_SORTED_FIELDS (klass) = field_vec; }