From patchwork Tue Aug 1 08:08:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 129061 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp2516495vqg; Tue, 1 Aug 2023 01:10:29 -0700 (PDT) X-Google-Smtp-Source: APBJJlGvQP6Rfbx3LQj0/ARKMMfR8nWYu3k4ROfEZxNxLZ6nSqQ8de9uMd550Gt4Nf+h2Drce4wm X-Received: by 2002:a17:906:244:b0:99c:1ea4:350 with SMTP id 4-20020a170906024400b0099c1ea40350mr1507178ejl.77.1690877429443; Tue, 01 Aug 2023 01:10:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690877429; cv=none; d=google.com; s=arc-20160816; b=AQlzFuOHtK8F6ratqmMAYssxqHfYbhgk5aukbUMx4H3PZBp9WoVBmN1qYPgcb4pS7X hkFdS5sF9Ts5hzFYSHQ5B/3xQChUQK2epFTyPB8xKMNPbI1fU9Q9chYECzu7DfQV2qpk XPh1iFLhZiWUNEaeEv1FG7Bd/JhriHP0QZ+kZofCyOdBAUErCZEkP+iW0LQZP8fGU73e Xwy5CpioHciXpdokZKkPBQSedmrB1IteG7d2z6iWrlSQjjECxNWPclJeFnzX6uPsohnf KWscKOgnvW0+oxVURqssN59/Xxsi3foUstoOcIrn4E7+H3+FSQd9Vy9H3TVzzey5awJA IzFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=sQO6GRH+/c8vQj/MZqCIvXA/xG68JGe2tiGCQPcsJ/s=; fh=beF+Pc8ynQ3wE1qqgmK6CL56KsYHzqLAU03xLjdxjP0=; b=bhvItiUN5sH2Rd85rx+ec8OXCInP7b2MpUPkBmNYYTcYMleoMOGMuK73qVGUbB+c60 +cnHJ0s+FQxxhEUb3aVpRN5VEdsN2JP8BKcAjTiMkRfrRqpEKv9JMKQnuiUcngFoFuKy feXAf4tHOUsNenv+T2NZ0R7jc43T6BAHk3VYb5/F7yxTi4FqX627xddITkQOWPV5zmf7 4FZE0ABv7bwq+cTh+i/ChZF312K6jOFAQygdDQLyE/9AIrIqu/8lwQ/7ABEjScbxe6qa DsGkzxnL0eaAUbq5hK51TIG4KuP/j2ZpSS9j/q0ItkBWoTsfdho8BnfbCMswgvKP4Sx7 OOTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=A77+wQgu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id gy19-20020a170906f25300b0099307f52a2esi2995225ejb.1003.2023.08.01.01.10.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 01:10:29 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=A77+wQgu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3F9A8385DC11 for ; Tue, 1 Aug 2023 08:09:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3F9A8385DC11 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690877371; bh=sQO6GRH+/c8vQj/MZqCIvXA/xG68JGe2tiGCQPcsJ/s=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=A77+wQguyMZMqu14CoWEFNW53HP1WwBsPnTsdzXP5pONe9Qm6ViuCP3159M0tPCcE wPAUfjrUX1cUgjnWRgzpGr2soBFIp5jONzgbnwy7eePgEN1z4Dz4UC22ldT3V4XvPA LxygQeHP/SnlPkHTAqVGuGVL4Mt6krlmMl0rdyDk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 20DE8385841A for ; Tue, 1 Aug 2023 08:08:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 20DE8385841A Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-31763b2c5a4so5457174f8f.3 for ; Tue, 01 Aug 2023 01:08:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690877298; x=1691482098; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=sQO6GRH+/c8vQj/MZqCIvXA/xG68JGe2tiGCQPcsJ/s=; b=XrYsDXupW5MfFJ5r4R9bkCVu8xFqQMbJwfELGHaiHTLJj0ktzp4jAs/Ch5YxSZV4k3 1A0c3GvLOglNuVtmiH+3LXVN2U1qN6aTCwtFj5MzUyHHGEmRs4RLAKTWL8wNoq/CyyS1 PaBL5edLnj4+ijmpfR8o+l49RTDBwQsBMCvYOcWQajougCP/LDzLpuX1qpBcYcSE3a95 6oMEiNJ1gd3yBSqcYPnxZA0dUqCLItUJTDw7VozURRW454gU4iS3RjG1NjQapGn/TZBV hZwLU7sIZfQsdd2TP16Rzzz4O03vorhhRtQTxw4iui5OKUbZzpVm3xvftrq07GSZUcMm 7f2A== X-Gm-Message-State: ABy/qLaLAUKyg+2Dx7IgluXkRnuc856DrBcsOIulIrgrPkxZqg95bA+x 85PoZL0l33JcQ7nJp2FUJAa0Jfn//TnhnPYY5D3poQ== X-Received: by 2002:adf:e9c8:0:b0:314:2faa:a23 with SMTP id l8-20020adfe9c8000000b003142faa0a23mr1798718wrn.43.1690877298368; Tue, 01 Aug 2023 01:08:18 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id s7-20020adff807000000b00313e2abfb8dsm15290587wrp.92.2023.08.01.01.08.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Aug 2023 01:08:17 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Pascal Obry Subject: [COMMITTED] ada: Default Put_Image for composite derived types is missing information Date: Tue, 1 Aug 2023 10:08:16 +0200 Message-Id: <20230801080816.2271563-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773013491309876722 X-GMAIL-MSGID: 1773013491309876722 From: Pascal Obry The output generated by a call to Some_Derived_Composite_Type'Put_Image (in Ada2022 code) is incomplete in some cases, notably for a type derived from a container type (i.e., from the Set/Map/List/Vector type declared in an instance of one of Ada's predefined container generics) with no user-specified Put_Image procedure. gcc/ada/ * aspects.ads (Find_Aspect): Add Boolean parameter Or_Rep_Item (defaulted to False). * aspects.adb (Find_Aspect): If new Boolean parameter Or_Rep_Item is True, then instead of returning an empty result if no appropriate N_Aspect_Specification node is found, return an appropriate N_Attribute_Definition_Clause if one is found. * exp_put_image.ads: Change name of Enable_Put_Image function to Put_Image_Enabled. * exp_put_image.adb (Build_Record_Put_Image_Procedure): Detect the case where a call to the Put_Image procedure of a derived type can be transformed into a call to the parent type's Put_Image procedure (with a type conversion to the parent type as the actual parameter). (Put_Image_Enabled): Change name of function (previously Enable_Put_Image). Return True in more cases. In particular, return True for a type with an explicitly specified Put_Image aspect even if the type is declared in a predefined unit (or in an instance of a predefined generic unit). * exp_attr.adb: Changes due to Put_Image_Enabled function name change. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/aspects.adb | 30 +++++++++++++++++------- gcc/ada/aspects.ads | 12 ++++++++-- gcc/ada/exp_attr.adb | 4 ++-- gcc/ada/exp_put_image.adb | 48 +++++++++++++++++++++++++++++++++------ gcc/ada/exp_put_image.ads | 2 +- 5 files changed, 76 insertions(+), 20 deletions(-) diff --git a/gcc/ada/aspects.adb b/gcc/ada/aspects.adb index c14769c640c..86dbd183565 100644 --- a/gcc/ada/aspects.adb +++ b/gcc/ada/aspects.adb @@ -193,13 +193,14 @@ package body Aspects is function Find_Aspect (Id : Entity_Id; A : Aspect_Id; - Class_Present : Boolean := False) return Node_Id + Class_Present : Boolean := False; + Or_Rep_Item : Boolean := False) return Node_Id is - Decl : Node_Id; - Item : Node_Id; - Owner : Entity_Id; - Spec : Node_Id; - + Decl : Node_Id; + Item : Node_Id; + Owner : Entity_Id; + Spec : Node_Id; + Alternative_Rep_Item : Node_Id := Empty; begin Owner := Id; @@ -231,6 +232,18 @@ package body Aspects is and then Class_Present = Sinfo.Nodes.Class_Present (Item) then return Item; + + -- We could do something similar here for an N_Pragma node + -- when Get_Aspect_Id (Pragma_Name (Item)) = A, but let's + -- wait for a demonstrated need. + + elsif Or_Rep_Item + and then not Class_Present + and then Nkind (Item) = N_Attribute_Definition_Clause + and then Get_Aspect_Id (Chars (Item)) = A + then + -- Remember this candidate in case we don't find anything better + Alternative_Rep_Item := Item; end if; Next_Rep_Item (Item); @@ -266,9 +279,10 @@ package body Aspects is end if; -- The entity does not carry any aspects or the desired aspect was not - -- found. + -- found. We have no N_Aspect_Specification node to return, but + -- Alternative_Rep_Item may have been set (if Or_Rep_Item is True). - return Empty; + return Alternative_Rep_Item; end Find_Aspect; -------------------------- diff --git a/gcc/ada/aspects.ads b/gcc/ada/aspects.ads index 05677978037..f718227a7af 100644 --- a/gcc/ada/aspects.ads +++ b/gcc/ada/aspects.ads @@ -1156,10 +1156,18 @@ package Aspects is function Find_Aspect (Id : Entity_Id; A : Aspect_Id; - Class_Present : Boolean := False) return Node_Id; + Class_Present : Boolean := False; + Or_Rep_Item : Boolean := False) return Node_Id; -- Find the aspect specification of aspect A (or A'Class if Class_Present) -- associated with entity I. - -- Return Empty if Id does not have the requested aspect. + -- If found, then return the aspect specification. + -- If not found and Or_Rep_Item is true, then look for a representation + -- item (as opposed to an N_Aspect_Specification node) which specifies + -- the given aspect; if found, then return the representation item. + -- [Currently only N_Attribute_Definition_Clause representation items + -- are checked for, but support for detecting N_Pragma representation + -- items could easily be added in the future if there is a need.] + -- Otherwise, return Empty. function Find_Value_Of_Aspect (Id : Entity_Id; diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb index 6b498eb2d77..dddc05437b4 100644 --- a/gcc/ada/exp_attr.adb +++ b/gcc/ada/exp_attr.adb @@ -5906,7 +5906,7 @@ package body Exp_Attr is if No (Pname) then -- If Put_Image is disabled, call the "unknown" version - if not Enable_Put_Image (U_Type) then + if not Put_Image_Enabled (U_Type) then Rewrite (N, Build_Unknown_Put_Image_Call (N)); Analyze (N); return; @@ -5937,7 +5937,7 @@ package body Exp_Attr is -- ????Need Find_Optional_Prim_Op instead of Find_Prim_Op, -- because we might be deriving from a predefined type, which - -- currently has Enable_Put_Image False. + -- currently has Put_Image_Enabled False. if No (Pname) then Rewrite (N, Build_Unknown_Put_Image_Call (N)); diff --git a/gcc/ada/exp_put_image.adb b/gcc/ada/exp_put_image.adb index 0c357f1c547..6684d4178e6 100644 --- a/gcc/ada/exp_put_image.adb +++ b/gcc/ada/exp_put_image.adb @@ -815,7 +815,7 @@ package body Exp_Put_Image is begin if Ada_Version < Ada_2022 - or else not Enable_Put_Image (Btyp) + or else not Put_Image_Enabled (Btyp) then -- generate a very simple Put_Image implementation @@ -845,6 +845,26 @@ package body Exp_Put_Image is Parameter_Associations => New_List (Make_Identifier (Loc, Name_S), Make_String_Literal (Loc, "(NULL RECORD)")))); + + elsif Is_Derived_Type (Btyp) + and then (not Is_Tagged_Type (Btyp) or else Is_Null_Extension (Btyp)) + then + declare + Parent_Type : constant Entity_Id := Base_Type (Etype (Btyp)); + begin + Append_To (Stms, + Make_Attribute_Reference (Loc, + Prefix => New_Occurrence_Of (Parent_Type, Loc), + Attribute_Name => Name_Put_Image, + Expressions => New_List ( + Make_Identifier (Loc, Name_S), + Make_Type_Conversion (Loc, + Subtype_Mark => New_Occurrence_Of + (Parent_Type, Loc), + Expression => Make_Identifier + (Loc, Name_V))))); + end; + else Append_To (Stms, Make_Procedure_Call_Statement (Loc, @@ -951,11 +971,11 @@ package body Exp_Put_Image is Entity (Prefix (N)), Append_NUL => False)))); end Build_Unknown_Put_Image_Call; - ---------------------- - -- Enable_Put_Image -- - ---------------------- + ----------------------- + -- Put_Image_Enabled -- + ----------------------- - function Enable_Put_Image (Typ : Entity_Id) return Boolean is + function Put_Image_Enabled (Typ : Entity_Id) return Boolean is begin -- If this function returns False for a non-scalar type Typ, then -- a) calls to Typ'Image will result in calls to @@ -969,13 +989,13 @@ package body Exp_Put_Image is -- The name "Sink" here is a short nickname for -- "Ada.Strings.Text_Buffers.Root_Buffer_Type". -- + -- Put_Image does not work for Remote_Types. We check the containing -- package, rather than the type itself, because we want to include -- types in the private part of a Remote_Types package. if Is_Remote_Types (Scope (Typ)) or else Is_Remote_Call_Interface (Typ) - or else (Is_Tagged_Type (Typ) and then In_Predefined_Unit (Typ)) then return False; end if; @@ -994,6 +1014,20 @@ package body Exp_Put_Image is return False; end if; + if Is_Tagged_Type (Typ) then + if Is_Class_Wide_Type (Typ) then + return Put_Image_Enabled (Find_Specific_Type (Base_Type (Typ))); + elsif Present (Find_Aspect (Typ, Aspect_Put_Image, + Or_Rep_Item => True)) + then + null; + elsif Is_Derived_Type (Typ) then + return Put_Image_Enabled (Etype (Base_Type (Typ))); + elsif In_Predefined_Unit (Typ) then + return False; + end if; + end if; + -- ???Disable Put_Image on type Root_Buffer_Type declared in -- Ada.Strings.Text_Buffers. Note that we can't call Is_RTU on -- Ada_Strings_Text_Buffers, because it's not known yet (we might be @@ -1030,7 +1064,7 @@ package body Exp_Put_Image is end if; return True; - end Enable_Put_Image; + end Put_Image_Enabled; ------------------------- -- Make_Put_Image_Name -- diff --git a/gcc/ada/exp_put_image.ads b/gcc/ada/exp_put_image.ads index a4c94120154..9af4d9e5b23 100644 --- a/gcc/ada/exp_put_image.ads +++ b/gcc/ada/exp_put_image.ads @@ -39,7 +39,7 @@ package Exp_Put_Image is -- are calls to T'Put_Image in different units, there will be duplicates; -- each unit will get a copy of the T'Put_Image procedure. - function Enable_Put_Image (Typ : Entity_Id) return Boolean; + function Put_Image_Enabled (Typ : Entity_Id) return Boolean; -- True if the predefined Put_Image should be enabled for type T. Put_Image -- is always enabled if there is a user-specified one.