From patchwork Tue Jan 11 13:32:34 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: 1578498 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=C78epPQc; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JYC9P09dgz9s1l for ; Wed, 12 Jan 2022 01:04:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E8B8038A941E for ; Tue, 11 Jan 2022 14:04:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8B8038A941E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641909846; bh=mBOkDV6Vb4loPT336MxhCyMtop82/gZuD7dVLI7aRso=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=C78epPQctGI9N20I+4C9AycfTbxmEzmRiTqQETDqSk9ZHZKQALxOxfOQb0T1UWGyF PuKAAVRzCgNdHCBrFpqxt44Cyj3hO+rQaZGx1UvzNlsu3mF2y8Vkkb2JuGSg5glGvU NfcAi9cUoOZKE1Q4pglyuBxmO7aFhkzX/CchYdv8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 8031E38A942F for ; Tue, 11 Jan 2022 13:32:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8031E38A942F Received: by mail-wr1-x435.google.com with SMTP id h10so22839359wrb.1 for ; Tue, 11 Jan 2022 05:32:36 -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=mBOkDV6Vb4loPT336MxhCyMtop82/gZuD7dVLI7aRso=; b=GboGkwr3YRDAphCIiEpAOU2Cx0KPt4wUARDudiZ77ErT2dNpXCMYU7lRAUhMgAh3e/ 9TMBP4di28whzKwFRkotndAcl5oyQaEz2pxieIUbl87PPvY+DJq4/ZIQfOz3yk9YjMqP Dg67PAHbtnzkO6yrN0/UUG+FfA6xrKmtDsYgfoXzJAXqhYc7M4eeZCSqmEtc+ySTOCsP s/YmWHHUMyR362WPeKe/3nKNrr4NpLOb5WxVptnISxspbTNYGXnrYoSphj+i/nBAGIsf Q02+PsKT2XTlcomyo+AGkE7w0eLarSL9myzCl1Ff7ZcfhYdl+FrJOgxPMEhi9nCQqmk9 e2CQ== X-Gm-Message-State: AOAM533LIqMlRdH7cF1f7lY9O94udXLSwUrfdUAIfUv8Snr/fOdJMYEO bFeC87Ny1EVKO8LZ4IiFV/rJN9dbGfl83w== X-Google-Smtp-Source: ABdhPJzAJBbs0Hu7zp5t51k7WEDE+PhBJ8ZyopGNg79gVpS0WAvXKx0rqxq5zTCOjxNI1TvMUIVGNQ== X-Received: by 2002:a5d:64cc:: with SMTP id f12mr3784058wri.679.1641907955611; Tue, 11 Jan 2022 05:32:35 -0800 (PST) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id w6sm9427732wrq.86.2022.01.11.05.32.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jan 2022 05:32:34 -0800 (PST) Date: Tue, 11 Jan 2022 13:32:34 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Reduce scope of declare block in analysis of allocators Message-ID: <20220111133234.GA748925@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-13.2 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: Piotr Trojanek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Cleanup related to handling of allocators in GNATprove; semantics is unaffected. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_ch4.adb (Analyze_Allocator): Move DECLARE block inside IF statement; refill code and comments. diff --git a/gcc/ada/sem_ch4.adb b/gcc/ada/sem_ch4.adb --- a/gcc/ada/sem_ch4.adb +++ b/gcc/ada/sem_ch4.adb @@ -588,59 +588,58 @@ package body Sem_Ch4 is -- Case where allocator has a subtype indication else - declare - Def_Id : Entity_Id; - Base_Typ : Entity_Id; - - begin - -- If the allocator includes a N_Subtype_Indication then a - -- constraint is present, otherwise the node is a subtype mark. - -- Introduce an explicit subtype declaration into the tree - -- defining some anonymous subtype and rewrite the allocator to - -- use this subtype rather than the subtype indication. - - -- It is important to introduce the explicit subtype declaration - -- so that the bounds of the subtype indication are attached to - -- the tree in case the allocator is inside a generic unit. - - -- Finally, if there is no subtype indication and the type is - -- a tagged unconstrained type with discriminants, the designated - -- object is constrained by their default values, and it is - -- simplest to introduce an explicit constraint now. In some cases - -- this is done during expansion, but freeze actions are certain - -- to be emitted in the proper order if constraint is explicit. - - if Is_Entity_Name (E) and then Expander_Active then - Find_Type (E); - Type_Id := Entity (E); - - if Is_Tagged_Type (Type_Id) - and then Has_Defaulted_Discriminants (Type_Id) - and then not Is_Constrained (Type_Id) - then - declare - Constr : constant List_Id := New_List; - Loc : constant Source_Ptr := Sloc (E); - Discr : Entity_Id := First_Discriminant (Type_Id); + -- If the allocator includes a N_Subtype_Indication then a + -- constraint is present, otherwise the node is a subtype mark. + -- Introduce an explicit subtype declaration into the tree + -- defining some anonymous subtype and rewrite the allocator to + -- use this subtype rather than the subtype indication. + + -- It is important to introduce the explicit subtype declaration + -- so that the bounds of the subtype indication are attached to + -- the tree in case the allocator is inside a generic unit. + + -- Finally, if there is no subtype indication and the type is + -- a tagged unconstrained type with discriminants, the designated + -- object is constrained by their default values, and it is + -- simplest to introduce an explicit constraint now. In some cases + -- this is done during expansion, but freeze actions are certain + -- to be emitted in the proper order if constraint is explicit. + + if Is_Entity_Name (E) and then Expander_Active then + Find_Type (E); + Type_Id := Entity (E); + + if Is_Tagged_Type (Type_Id) + and then Has_Defaulted_Discriminants (Type_Id) + and then not Is_Constrained (Type_Id) + then + declare + Constr : constant List_Id := New_List; + Loc : constant Source_Ptr := Sloc (E); + Discr : Entity_Id := First_Discriminant (Type_Id); - begin - while Present (Discr) loop - Append (Discriminant_Default_Value (Discr), Constr); - Next_Discriminant (Discr); - end loop; + begin + while Present (Discr) loop + Append (Discriminant_Default_Value (Discr), Constr); + Next_Discriminant (Discr); + end loop; - Rewrite (E, - Make_Subtype_Indication (Loc, - Subtype_Mark => New_Occurrence_Of (Type_Id, Loc), - Constraint => - Make_Index_Or_Discriminant_Constraint (Loc, - Constraints => Constr))); - end; - end if; + Rewrite (E, + Make_Subtype_Indication (Loc, + Subtype_Mark => New_Occurrence_Of (Type_Id, Loc), + Constraint => + Make_Index_Or_Discriminant_Constraint (Loc, + Constraints => Constr))); + end; end if; + end if; - if Nkind (E) = N_Subtype_Indication then + if Nkind (E) = N_Subtype_Indication then + declare + Def_Id : Entity_Id; + Base_Typ : Entity_Id; + begin -- A constraint is only allowed for a composite type in Ada -- 95. In Ada 83, a constraint is also allowed for an -- access-to-composite type, but the constraint is ignored. @@ -693,151 +692,151 @@ package body Sem_Ch4 is N_Index_Or_Discriminant_Constraint then Error_Msg_N -- CODEFIX - ("if qualified expression was meant, " - & "use apostrophe!", Constraint (E)); + ("if qualified expression was meant, use apostrophe!", + Constraint (E)); end if; E := New_Occurrence_Of (Def_Id, Loc); Rewrite (Expression (N), E); end if; - end if; + end; + end if; - Type_Id := Process_Subtype (E, N); - Acc_Type := Create_Itype (E_Allocator_Type, N); - Set_Etype (Acc_Type, Acc_Type); - Set_Directly_Designated_Type (Acc_Type, Type_Id); - Check_Fully_Declared (Type_Id, N); + Type_Id := Process_Subtype (E, N); + Acc_Type := Create_Itype (E_Allocator_Type, N); + Set_Etype (Acc_Type, Acc_Type); + Set_Directly_Designated_Type (Acc_Type, Type_Id); + Check_Fully_Declared (Type_Id, N); - -- Ada 2005 (AI-231): If the designated type is itself an access - -- type that excludes null, its default initialization will - -- be a null object, and we can insert an unconditional raise - -- before the allocator. + -- Ada 2005 (AI-231): If the designated type is itself an access + -- type that excludes null, its default initialization will + -- be a null object, and we can insert an unconditional raise + -- before the allocator. - -- Ada 2012 (AI-104): A not null indication here is altogether - -- illegal. + -- Ada 2012 (AI-104): A not null indication here is altogether + -- illegal. - if Can_Never_Be_Null (Type_Id) then - declare - Not_Null_Check : constant Node_Id := - Make_Raise_Constraint_Error (Sloc (E), - Reason => CE_Null_Not_Allowed); + if Can_Never_Be_Null (Type_Id) then + declare + Not_Null_Check : constant Node_Id := + Make_Raise_Constraint_Error (Sloc (E), + Reason => CE_Null_Not_Allowed); - begin - if Expander_Active then - Insert_Action (N, Not_Null_Check); - Analyze (Not_Null_Check); + begin + if Expander_Active then + Insert_Action (N, Not_Null_Check); + Analyze (Not_Null_Check); - elsif Warn_On_Ada_2012_Compatibility then - Error_Msg_N - ("null value not allowed here in Ada 2012?y?", E); - end if; - end; - end if; + elsif Warn_On_Ada_2012_Compatibility then + Error_Msg_N + ("null value not allowed here in Ada 2012?y?", E); + end if; + end; + end if; - -- Check for missing initialization. Skip this check if we already - -- had errors on analyzing the allocator, since in that case these - -- are probably cascaded errors. + -- Check for missing initialization. Skip this check if we already + -- had errors on analyzing the allocator, since in that case these + -- are probably cascaded errors. - if not Is_Definite_Subtype (Type_Id) - and then Serious_Errors_Detected = Sav_Errs + if not Is_Definite_Subtype (Type_Id) + and then Serious_Errors_Detected = Sav_Errs + then + -- The build-in-place machinery may produce an allocator when + -- the designated type is indefinite but the underlying type is + -- not. In this case the unknown discriminants are meaningless + -- and should not trigger error messages. Check the parent node + -- because the allocator is marked as coming from source. + + if Present (Underlying_Type (Type_Id)) + and then Is_Definite_Subtype (Underlying_Type (Type_Id)) + and then not Comes_From_Source (Parent (N)) then - -- The build-in-place machinery may produce an allocator when - -- the designated type is indefinite but the underlying type is - -- not. In this case the unknown discriminants are meaningless - -- and should not trigger error messages. Check the parent node - -- because the allocator is marked as coming from source. - - if Present (Underlying_Type (Type_Id)) - and then Is_Definite_Subtype (Underlying_Type (Type_Id)) - and then not Comes_From_Source (Parent (N)) - then - null; + null; - -- An unusual case arises when the parent of a derived type is - -- a limited record extension with unknown discriminants, and - -- its full view has no discriminants. - -- - -- A more general fix might be to create the proper underlying - -- type for such a derived type, but it is a record type with - -- no private attributes, so this required extending the - -- meaning of this attribute. ??? - - elsif Ekind (Etype (Type_Id)) = E_Record_Type_With_Private - and then Present (Underlying_Type (Etype (Type_Id))) - and then - not Has_Discriminants (Underlying_Type (Etype (Type_Id))) - and then not Comes_From_Source (Parent (N)) + -- An unusual case arises when the parent of a derived type is + -- a limited record extension with unknown discriminants, and + -- its full view has no discriminants. + -- + -- A more general fix might be to create the proper underlying + -- type for such a derived type, but it is a record type with + -- no private attributes, so this required extending the + -- meaning of this attribute. ??? + + elsif Ekind (Etype (Type_Id)) = E_Record_Type_With_Private + and then Present (Underlying_Type (Etype (Type_Id))) + and then + not Has_Discriminants (Underlying_Type (Etype (Type_Id))) + and then not Comes_From_Source (Parent (N)) + then + null; + + elsif Is_Class_Wide_Type (Type_Id) then + Error_Msg_N + ("initialization required in class-wide allocation", N); + + else + if Ada_Version < Ada_2005 + and then Is_Limited_Type (Type_Id) then - null; + Error_Msg_N ("unconstrained allocation not allowed", N); - elsif Is_Class_Wide_Type (Type_Id) then - Error_Msg_N - ("initialization required in class-wide allocation", N); + if Is_Array_Type (Type_Id) then + Error_Msg_N + ("\constraint with array bounds required", N); + + elsif Has_Unknown_Discriminants (Type_Id) then + null; + + else pragma Assert (Has_Discriminants (Type_Id)); + Error_Msg_N + ("\constraint with discriminant values required", N); + end if; + + -- Limited Ada 2005 and general nonlimited case. + -- This is an error, except in the case of an + -- uninitialized allocator that is generated + -- for a build-in-place function return of a + -- discriminated but compile-time-known-size + -- type. else - if Ada_Version < Ada_2005 - and then Is_Limited_Type (Type_Id) + if Original_Node (N) /= N + and then Nkind (Original_Node (N)) = N_Allocator then - Error_Msg_N ("unconstrained allocation not allowed", N); + declare + Qual : constant Node_Id := + Expression (Original_Node (N)); + pragma Assert + (Nkind (Qual) = N_Qualified_Expression); + Call : constant Node_Id := Expression (Qual); + pragma Assert + (Is_Expanded_Build_In_Place_Call (Call)); + begin + null; + end; + + else + Error_Msg_N + ("uninitialized unconstrained allocation not " + & "allowed", N); if Is_Array_Type (Type_Id) then Error_Msg_N - ("\constraint with array bounds required", N); + ("\qualified expression or constraint with " + & "array bounds required", N); elsif Has_Unknown_Discriminants (Type_Id) then - null; + Error_Msg_N ("\qualified expression required", N); else pragma Assert (Has_Discriminants (Type_Id)); Error_Msg_N - ("\constraint with discriminant values required", N); - end if; - - -- Limited Ada 2005 and general nonlimited case. - -- This is an error, except in the case of an - -- uninitialized allocator that is generated - -- for a build-in-place function return of a - -- discriminated but compile-time-known-size - -- type. - - else - if Original_Node (N) /= N - and then Nkind (Original_Node (N)) = N_Allocator - then - declare - Qual : constant Node_Id := - Expression (Original_Node (N)); - pragma Assert - (Nkind (Qual) = N_Qualified_Expression); - Call : constant Node_Id := Expression (Qual); - pragma Assert - (Is_Expanded_Build_In_Place_Call (Call)); - begin - null; - end; - - else - Error_Msg_N - ("uninitialized unconstrained allocation not " - & "allowed", N); - - if Is_Array_Type (Type_Id) then - Error_Msg_N - ("\qualified expression or constraint with " - & "array bounds required", N); - - elsif Has_Unknown_Discriminants (Type_Id) then - Error_Msg_N ("\qualified expression required", N); - - else pragma Assert (Has_Discriminants (Type_Id)); - Error_Msg_N - ("\qualified expression or constraint with " - & "discriminant values required", N); - end if; + ("\qualified expression or constraint with " + & "discriminant values required", N); end if; end if; end if; end if; - end; + end if; end if; if Is_Abstract_Type (Type_Id) then