From patchwork Mon Jan 10 09:41:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 49791 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 321423891C05 for ; Mon, 10 Jan 2022 09:49:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 321423891C05 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641808185; bh=8l7rhbgDULnm0x0ooIM5uLy+S12exVB2X+HBpGj9NsY=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=JcBgw4axnXg4RDhgkRx6CsVCJjb9uQOjNllbfjGASfrYX2MeUBG6v5YEjTGgETBky Kj6NK4Pr7n+vOdJlMwqK/IC8om0OKRZM73K57x+IREb/KHIJyYUQOS9iBj9S+Y/Dal 1FYclM5udoXD5ybxGHYcZSuYgdYkYqH7MmEOJO10= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id 6350C3891C01 for ; Mon, 10 Jan 2022 09:41:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6350C3891C01 Received: by mail-wr1-x433.google.com with SMTP id e9so24059812wra.2 for ; Mon, 10 Jan 2022 01:41:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=8l7rhbgDULnm0x0ooIM5uLy+S12exVB2X+HBpGj9NsY=; b=P+nv1tPBbWTn9H8K99b62+c37UkEf6RQhrhkdWF36btfwfafFKyDXFsy1jqBu3vvX0 TtKM8CQ9qfas8kBFVK2zRSKDP7YXWj0k7lVOYz0bZeYZbTOnCCmxopxPk1p+uwHH71cW 1Ze9dvNrqDbjcyXUg9DXx/ppRKSjqwMogWfQLzuvAYujoOulJ6Azqr9+wK38J5WasQqT qK3J4VAMC8aDas+C7t5L1MgrwV/mJiIUwD+T7lYwHWqhGo6gKv2MygtYjfvQx8AkKRpf YAjL8W3iGuE+Rwv30/iLIvRUAukIWrelyLeXLPgXhzo+9EoATorAW1wT2JYH5O3ZgsA+ QHNw== X-Gm-Message-State: AOAM531Ev1R6HAkHcmZzK4ngL0k6M/Xwo5NWAaRom3Tn35UpluYI3AKH BE0wd1W8mFf3SEvFIrVaj3N0awbEJrCf0w== X-Google-Smtp-Source: ABdhPJyQLj549g9IdSibdBKmoNR3ygnx13IBAsJaD6yDzqoOhEUbhihv7IMp1y59inyNPb5NNylYlA== X-Received: by 2002:adf:c10e:: with SMTP id r14mr65087828wre.558.1641807701498; Mon, 10 Jan 2022 01:41:41 -0800 (PST) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id n14sm6519562wrf.107.2022.01.10.01.41.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jan 2022 01:41:40 -0800 (PST) Date: Mon, 10 Jan 2022 09:41:40 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix internal error on unchecked union with component clauses Message-ID: <20220110094140.GA586181@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Eric Botcazou Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The issue arises when the unchecked union contains nested variants, i.e. variants containing themselves a variant part, and is subject to a full representation clause covering all the components in all the variants, when the component clauses do not align the variant boundaries with byte boundaries consistently. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * gcc-interface/decl.c (gnat_to_gnu_entity) : Fix computation of boolean result in the unchecked union case. (components_to_record): Rename MAYBE_UNUSED parameter to IN_VARIANT and remove local variable of the same name. Pass NULL recursively as P_GNU_REP_LIST for nested variants in the unchecked union case. diff --git a/gcc/ada/gcc-interface/decl.c b/gcc/ada/gcc-interface/decl.c --- a/gcc/ada/gcc-interface/decl.c +++ b/gcc/ada/gcc-interface/decl.c @@ -3059,7 +3059,8 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition) Present (gnat_field); gnat_field = Next_Entity (gnat_field)) if ((Ekind (gnat_field) == E_Component - || Ekind (gnat_field) == E_Discriminant) + || (Ekind (gnat_field) == E_Discriminant + && !is_unchecked_union)) && No (Component_Clause (gnat_field))) { all_rep = false; @@ -7874,8 +7875,7 @@ typedef struct vinfo DEBUG_INFO is true if we need to write debug information about the type. - MAYBE_UNUSED is true if this type may be unused in the end; this doesn't - mean that its contents may be unused as well, only the container itself. + IN_VARIANT is true if the componennt list is that of a variant. FIRST_FREE_POS, if nonzero, is the first (lowest) free field position in the outer record type down to this variant level. It is nonzero only if @@ -7890,7 +7890,7 @@ components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type, tree gnu_field_list, tree gnu_record_type, int packed, bool definition, bool cancel_alignment, bool all_rep, bool unchecked_union, bool artificial, bool debug_info, - bool maybe_unused, tree first_free_pos, + bool in_variant, tree first_free_pos, tree *p_gnu_rep_list) { const bool needs_xv_encodings @@ -8075,15 +8075,21 @@ components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type, = TYPE_SIZE_UNIT (gnu_record_type); } - /* Add the fields into the record type for the variant. Note that - we aren't sure to really use it at this point, see below. */ + /* Add the fields into the record type for the variant but note that + we aren't sure to really use it at this point, see below. In the + case of an unchecked union, we force the fields with a rep clause + present in a nested variant to be moved to the outermost variant, + so as to flatten the rep-ed layout as much as possible, the reason + being that we cannot do any flattening when a subtype statically + selects a variant later on, for example for an aggregate. */ has_rep = components_to_record (Component_List (variant), gnat_record_type, NULL_TREE, gnu_variant_type, packed, definition, !all_rep_and_size, all_rep, unchecked_union, true, needs_xv_encodings, true, this_first_free_pos, - all_rep || this_first_free_pos + (all_rep || this_first_free_pos) + && !(in_variant && unchecked_union) ? NULL : &gnu_rep_list); /* Translate the qualifier and annotate the GNAT node. */ @@ -8206,9 +8212,9 @@ components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type, finish_record_type (gnu_union_type, nreverse (gnu_variant_list), all_rep_and_size ? 1 : 0, needs_xv_encodings); - /* If GNU_UNION_TYPE is our record type, it means we must have an - Unchecked_Union with no fields. Verify that and, if so, just - return. */ + /* If GNU_UNION_TYPE is our record type, this means that we must have + an Unchecked_Union whose fields are all in the variant part. Now + verify that and, if so, just return. */ if (gnu_union_type == gnu_record_type) { gcc_assert (unchecked_union @@ -8275,7 +8281,6 @@ components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type, = (Convention (gnat_record_type) == Convention_Ada && Warn_On_Questionable_Layout && !(No_Reordering (gnat_record_type) && GNAT_Mode)); - const bool in_variant = (p_gnu_rep_list != NULL); tree gnu_zero_list = NULL_TREE; tree gnu_self_list = NULL_TREE; tree gnu_var_list = NULL_TREE; @@ -8640,7 +8645,7 @@ components_to_record (Node_Id gnat_component_list, Entity_Id gnat_record_type, TYPE_ARTIFICIAL (gnu_record_type) = artificial; finish_record_type (gnu_record_type, gnu_field_list, layout_with_rep ? 1 : 0, - debug_info && !maybe_unused); + debug_info && !in_variant); /* Chain the fields with zero size at the beginning of the field list. */ if (gnu_zero_list)