From patchwork Fri Jul 28 07:30:47 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: 127385 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp261288vqg; Fri, 28 Jul 2023 00:39:04 -0700 (PDT) X-Google-Smtp-Source: APBJJlEmqSY1/vLbSH4gOJDt5QhvsXRhybO3pL5UbRKZT9umc8cTEoTEhkqsBtuhT4y4hnh3Mdv+ X-Received: by 2002:aa7:cd6a:0:b0:51e:2282:e1fc with SMTP id ca10-20020aa7cd6a000000b0051e2282e1fcmr1093455edb.6.1690529943944; Fri, 28 Jul 2023 00:39:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690529943; cv=none; d=google.com; s=arc-20160816; b=GjwTGFC+hb6fz3MQ0xEetjTLS/z3BqePsgiqtNTUS1ZT/Vg3M6vepxMsIbHQ23jMB4 tpR4/2B8csPGfpyXTbVwhdNkXAVnB3sZeIry3q4aTWSzptmD8o/si4vy9yKUFMlq7EW/ 48EIC0WSfBmuiUXekNrjBqTJ23j9c6HcdvjQPFtxH2bt66M22ZcPoevx7TwU+So219Zr CbVzX20evL+shrhlCKf2YfiMdIFiAvowNi9d3bUlYPzV24C9bJo5hNqOkwQtxTjn3EYU qjLXQjIcEnN/HHwJLUZZhgxoGyiiFnj4+ZfBvQ9o5wlP6CllpnRdb+CQi3cS20t1IkWv 0/+w== 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=rSLtkIEUPGZAA0GVlpxyUVGOAhKOZ1mgztGGWwvMVeo=; fh=XugzLzSAfUMFtRI8ljBKsKm2X575N/wgCIOzZPd2BB4=; b=pCXRcQ2ZVyRvPmDyhIQjamt9CslgZB5gqeI4aVL3BZVqVCPbYjz9ODMv84ta6kMn9A D7LJXBamosIilavFij7f4pYGjdWhkBJlGFMX7ngOaHO/FvSzgevkCO81ZYiZ+GXFWbID tvxoZRsiu6AQhmCR6B/MkxdZHYDxaSBmhbACtRygLsGqtOEs4l5vSy22i6kzghgQ82DP mmmO59Btf4m7YEai/qnr9VCsOT6emqKkHjSyxIxeqjaDpLDGYI6CtOoaA3z0JRdKQlbo ThBuW7MbDcH/xmhKR2AEN8Lgo4LvUiw/g30DryfyWfW/M5V46oFH27hpfbpnocJINtkC 5Kfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=e7+zZr8T; 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 y14-20020aa7c24e000000b005227d7bfc18si2214853edo.619.2023.07.28.00.39.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jul 2023 00:39:03 -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=e7+zZr8T; 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 4E66D384233D for ; Fri, 28 Jul 2023 07:33:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E66D384233D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690529620; bh=rSLtkIEUPGZAA0GVlpxyUVGOAhKOZ1mgztGGWwvMVeo=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=e7+zZr8TgzyJYx+pqHwk55YJz0gUAN+usN4LVZY5golXNKtbXt5ZcN7fSkK5e+4Rz C+czBRrayXcMUsnNRknJf1GucFJuV+YFZK0GrjOg1UZOsD3JzipONzWmdmYlmcc6qt B/PKFvQywoHPo4L/eR/10LVu0wR3lR6aIOhrEgG4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by sourceware.org (Postfix) with ESMTPS id 544D7385AFBD for ; Fri, 28 Jul 2023 07:30:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 544D7385AFBD Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-4fdfefdf5abso3051628e87.1 for ; Fri, 28 Jul 2023 00:30:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690529450; x=1691134250; 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=rSLtkIEUPGZAA0GVlpxyUVGOAhKOZ1mgztGGWwvMVeo=; b=drhmlRNYSzEr+3uHNm1d1mzPqWI9PfRb2CzQw43/4o/9kiUJ12xilaTNjXAg2Oh3fO yTXI0PiQchZAucko8SKlpYRao9YZzXO6Tegje+c24Amr2opwLCBiNFKRLqRHdtph+aYd /MWoSfpyMmBFGQMTpQEF9iNodSiiLxuLHQNpi7fj8xs4bWvZR5r5AYXQsqRNAE+uJwbM iqtLM4Hua92v8TPLDYSuYkZMC3iM2ubumZ4Uo690iDZ0KUE8UxWH18SyGralk06EWeLK EBiiSIrIcyIkBPdOUUxLCxb75Q00rBK4QnOdOPS3a1lmCpWby9w9vlOxrse+rCUqV0I5 PgxA== X-Gm-Message-State: ABy/qLacwz+1I5J7lXcpTDTqJB284JBjqmf286HIZ89+7eBfNcy2j867 cZfeBi/uo35LzlySamSoAcyIgYDHsL6BBe2fGBDFYw== X-Received: by 2002:ac2:5b4e:0:b0:4f8:76ba:ad3c with SMTP id i14-20020ac25b4e000000b004f876baad3cmr1033429lfp.55.1690529450091; Fri, 28 Jul 2023 00:30:50 -0700 (PDT) Received: from localhost.localdomain ([2001:861:3382:1a90:792d:7d2b:d7e:14a3]) by smtp.gmail.com with ESMTPSA id y18-20020a5d6212000000b003143c6e09ccsm4102483wru.16.2023.07.28.00.30.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jul 2023 00:30:49 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Tom Tromey Subject: [COMMITTED] ada: Emit enums rather than defines for various constants Date: Fri, 28 Jul 2023 09:30:47 +0200 Message-Id: <20230728073047.1852369-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 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, 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: 1772649126221803120 X-GMAIL-MSGID: 1772649126221803120 From: Tom Tromey This patch changes xsnamest and gen_il-gen to emit various constants as enums rather than a sequence of preprocessor defines. This enables better debugging and somewhat better type safety. gcc/ada/ * fe.h (Convention): Now inline function. * gen_il-gen.adb (Put_C_Type_And_Subtypes.Put_Enum_Lit) (Put_C_Type_And_Subtypes.Put_Kind_Subtype, Put_C_Getter): Emit enum. * snames.h-tmpl (Name_Id, Name_, Attribute_Id, Attribute_) (Convention_Id, Convention_, Pragma_Id, Pragma_): Now enum. (Get_Attribute_Id, Get_Pragma_Id): Now inline functions. * types.h (Node_Kind, Entity_Kind, Convention_Id, Name_Id): Now enum. * xsnamest.adb (Output_Header_Line, Make_Value): Emit enum. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/fe.h | 8 ++++-- gcc/ada/gen_il-gen.adb | 11 ++++++--- gcc/ada/snames.h-tmpl | 56 +++++++++++++++++++++++++----------------- gcc/ada/types.h | 8 +++--- gcc/ada/xsnamest.adb | 30 +++++++++++++--------- 5 files changed, 69 insertions(+), 44 deletions(-) diff --git a/gcc/ada/fe.h b/gcc/ada/fe.h index f283064c728..ca77f433cfa 100644 --- a/gcc/ada/fe.h +++ b/gcc/ada/fe.h @@ -683,8 +683,12 @@ Entity_Kind Parameter_Mode (E Id); // The following is needed because Convention in Sem_Util is a renaming // of Basic_Convention. -#define Convention einfo__entities__basic_convention -Convention_Id Convention (N Node); +static inline Convention_Id +Convention (N Node) +{ + extern Byte einfo__entities__basic_convention (N Node); + return (Convention_Id) einfo__entities__basic_convention (Node); +} // See comments regarding Entity_Or_Associated_Node in Sinfo.Utils. diff --git a/gcc/ada/gen_il-gen.adb b/gcc/ada/gen_il-gen.adb index bf760f3d917..1cee17caf76 100644 --- a/gcc/ada/gen_il-gen.adb +++ b/gcc/ada/gen_il-gen.adb @@ -2957,9 +2957,9 @@ package body Gen_IL.Gen is -- Current Node_Kind'Pos or Entity_Kind'Pos to be printed procedure Put_Enum_Lit (T : Node_Or_Entity_Type); - -- Print out the #define corresponding to the Ada enumeration literal + -- Print out the enumerator corresponding to the Ada enumeration literal -- for T in Node_Kind and Entity_Kind (i.e. concrete types). - -- This looks like "#define Some_Kind ", where Some_Kind + -- This looks like "Some_Kind = ", where Some_Kind -- is the Node_Kind or Entity_Kind enumeration literal, and -- is Node_Kind'Pos or Entity_Kind'Pos of that literal. @@ -2970,7 +2970,7 @@ package body Gen_IL.Gen is procedure Put_Enum_Lit (T : Node_Or_Entity_Type) is begin if T in Concrete_Type then - Put (S, "#define " & Image (T) & " " & Image (Cur_Pos) & LF); + Put (S, " " & Image (T) & " = " & Image (Cur_Pos) & "," & LF); Cur_Pos := Cur_Pos + 1; end if; end Put_Enum_Lit; @@ -2990,7 +2990,9 @@ package body Gen_IL.Gen is begin Put_Union_Membership (S, Root, Only_Prototypes => True); + Put (S, "enum " & Node_Or_Entity (Root) & "_Kind : unsigned int {" & LF); Iterate_Types (Root, Pre => Put_Enum_Lit'Access); + Put (S, "};" & LF); Put (S, "#define Number_" & Node_Or_Entity (Root) & "_Kinds " & Image (Cur_Pos) & "" & LF & LF); @@ -3046,7 +3048,8 @@ package body Gen_IL.Gen is Put (S, "unsigned int Raw = slot;" & LF); end if; - Put (S, Get_Set_Id_Image (Rec.Field_Type) & " val = "); + Put (S, Get_Set_Id_Image (Rec.Field_Type) & " val = (" & + Get_Set_Id_Image (Rec.Field_Type) & ") "); if Field_Has_Special_Default (Rec.Field_Type) then Increase_Indent (S, 2); diff --git a/gcc/ada/snames.h-tmpl b/gcc/ada/snames.h-tmpl index 95b3c776197..f01642ffbff 100644 --- a/gcc/ada/snames.h-tmpl +++ b/gcc/ada/snames.h-tmpl @@ -28,43 +28,55 @@ /* Name_Id values */ -typedef Int Name_Id; -#define Name_ !! TEMPLATE INSERTION POINT +enum Name_Id : Int +{ + Name_ !! TEMPLATE INSERTION POINT +}; -/* Define the function to return one of the numeric values below. Note - that it actually returns a char since an enumeration value of less - than 256 entries is represented that way in Ada. The operand is a Chars - field value. */ +/* Define the numeric values for attributes. */ -typedef Byte Attribute_Id; -#define Get_Attribute_Id snames__get_attribute_id -extern Attribute_Id Get_Attribute_Id (int); +enum Attribute_Id : unsigned char +{ + Attr_ !! TEMPLATE INSERTION POINT +}; -/* Define the numeric values for attributes. */ +/* Define the function to return one of the numeric values above. The operand + is a Chars field value. */ -#define Attr_ !! TEMPLATE INSERTION POINT +static inline Attribute_Id +Get_Attribute_Id (int id) +{ + extern unsigned char snames__get_attribute_id (int); + return (Attribute_Id) snames__get_attribute_id (id); +} /* Define the numeric values for the conventions. */ -typedef Byte Convention_Id; -#define Convention_ !! TEMPLATE INSERTION POINT +enum Convention_Id : Byte +{ + Convention_ !! TEMPLATE INSERTION POINT +}; /* Define the function to check if a Name_Id value is a valid pragma */ #define Is_Pragma_Name snames__is_pragma_name extern Boolean Is_Pragma_Name (Name_Id); -/* Define the function to return one of the numeric values below. Note - that it actually returns a char since an enumeration value of less - than 256 entries is represented that way in Ada. The operand is a Chars - field value. */ +/* Define the numeric values for the pragmas. */ -typedef Byte Pragma_Id; -#define Get_Pragma_Id snames__get_pragma_id -extern Pragma_Id Get_Pragma_Id (int); +enum Pragma_Id : Byte +{ + Pragma_ !! TEMPLATE_INSERTION_POINT +}; -/* Define the numeric values for the pragmas. */ +/* Define the function to return one of the numeric values above. The operand + is a Chars field value. */ -#define Pragma_ !! TEMPLATE_INSERTION_POINT +static inline Pragma_Id +Get_Pragma_Id (int id) +{ + extern unsigned char snames__get_pragma_id (int); + return (Pragma_Id) snames__get_pragma_id (id); +} /* End of snames.h (C version of Snames package spec) */ diff --git a/gcc/ada/types.h b/gcc/ada/types.h index 3004de658aa..aa0b2a67b84 100644 --- a/gcc/ada/types.h +++ b/gcc/ada/types.h @@ -102,8 +102,8 @@ typedef struct { const char *Array; String_Template *Bounds; } once again, the annoying restriction on bit fields for some compilers bites us! */ -typedef unsigned int Node_Kind; -typedef unsigned int Entity_Kind; +enum Node_Kind : unsigned int; +enum Entity_Kind : unsigned int; /* Types used for Text Buffer Handling: */ @@ -140,7 +140,7 @@ typedef Text_Ptr Source_Ptr; #define Standard_Location -2 /* Convention identifiers. */ -typedef Byte Convention_Id; +enum Convention_Id : Byte; /* Instance identifiers. */ typedef Nat Instance_Id; @@ -188,7 +188,7 @@ SUBTYPE (Ureal_Range, Int, Ureal_Low_Bound, Ureal_High_Bound) /* Types for Names_Table Package: */ -typedef Int Name_Id; +enum Name_Id : Int; /* Name_Id value for no name present. */ #define No_Name Names_Low_Bound diff --git a/gcc/ada/xsnamest.adb b/gcc/ada/xsnamest.adb index 979750faf7e..576cf76c960 100644 --- a/gcc/ada/xsnamest.adb +++ b/gcc/ada/xsnamest.adb @@ -119,13 +119,17 @@ procedure XSnamesT is Header_Current_Symbol : Header_Symbol := None; Header_Pending_Line : VString := Nul; + -- Subtypes we will emit after an enum + + Generated_C_Subtypes : Unbounded_String; + ------------------------ -- Output_Header_Line -- ------------------------ procedure Output_Header_Line (S : Header_Symbol) is function Make_Value (V : Integer) return String; - -- Build the definition for the current macro (Names are integers + -- Build the definition for the current enumerator (Names are integers -- offset to N, while other items are enumeration values). ---------------- @@ -144,14 +148,14 @@ procedure XSnamesT is -- Start of processing for Output_Header_Line begin - -- Skip all the #define for S-prefixed symbols in the header. + -- Skip all the enumerator for S-prefixed symbols in the header. -- Of course we are making implicit assumptions: -- (1) No newline between symbols with the same prefix. -- (2) Prefix order is the same as in snames.ads. if Header_Current_Symbol /= S then declare - Pat : constant Pattern := "#define " + Pat : constant Pattern := " " & Header_Prefix (S).all & Break (' ') * Name2; In_Pat : Boolean := False; @@ -180,14 +184,12 @@ procedure XSnamesT is -- Now output the line - -- Note that we must ensure at least one space between macro name and - -- parens, otherwise the parenthesized value gets treated as an argument - -- specification. - - Put_Line (OutH, "#define " & Header_Prefix (S).all + Put_Line (OutH, " " & Header_Prefix (S).all & "_" & Name1 & (30 - Natural'Min (29, Length (Name1))) * ' ' - & Make_Value (Header_Counter (S))); + & " = " + & Make_Value (Header_Counter (S)) + & ","); Header_Counter (S) := Header_Counter (S) + 1; end Output_Header_Line; @@ -235,10 +237,12 @@ begin elsif Match (Line, Get_Prag) then Output_Header_Line (Prag); elsif Match (Line, Get_Subt1) and then Match (Name2, Is_Conv) then - New_Line (OutH); - Put_Line (OutH, "SUBTYPE (" & Name1 & ", " & Name2 & ", "); + Generated_C_Subtypes := Generated_C_Subtypes & ASCII.LF + & "SUBTYPE (" & Name1 & ", " & Name2 + & ", "; elsif Match (Line, Get_Subt2) and then Match (Name1, Is_Conv) then - Put_Line (OutH, " " & Name1 & ", " & Name2 & ')'); + Generated_C_Subtypes := Generated_C_Subtypes & ASCII.LF + & " " & Name1 & ", " & Name2 & ')'; end if; else @@ -297,6 +301,8 @@ begin Put_Line (OutH, Line); end loop; + Put_Line (OutH, Generated_C_Subtypes); + Put_Line (OutH, ""); Put_Line (OutH, "#ifdef __cplusplus"); Put_Line (OutH, "}"); Put_Line (OutH, "#endif");