summaryrefslogtreecommitdiff
path: root/gcc/ada/xsnamest.adb
blob: 7dd3ca29883c8a5a3b398fefaaf8c03f011073f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
------------------------------------------------------------------------------
--                                                                          --
--                          GNAT SYSTEM UTILITIES                           --
--                                                                          --
--                             X S N A M E S T                              --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license.          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

--  This utility is used to make a new version of the Snames package when new
--  names are added. This version reads a template file from snames.ads-tmpl in
--  which the numbers are all written as $, and generates a new version of the
--  spec file snames.ads (written to snames.ns). It also reads snames.adb-tmpl
--  and generates an updated body (written to snames.nb), and snames.h-tmpl and
--  generates an updated C header file (written to snames.nh).

with Ada.Strings.Unbounded;         use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
with Ada.Strings.Maps;              use Ada.Strings.Maps;
with Ada.Strings.Maps.Constants;    use Ada.Strings.Maps.Constants;
with Ada.Text_IO;                   use Ada.Text_IO;
with Ada.Streams.Stream_IO;         use Ada.Streams.Stream_IO;

with GNAT.Spitbol;                  use GNAT.Spitbol;
with GNAT.Spitbol.Patterns;         use GNAT.Spitbol.Patterns;

with XUtil;                         use XUtil;

procedure XSnamesT is

   subtype VString is GNAT.Spitbol.VString;

   InS  : Ada.Text_IO.File_Type;
   InB  : Ada.Text_IO.File_Type;
   InH  : Ada.Text_IO.File_Type;

   OutS : Ada.Streams.Stream_IO.File_Type;
   OutB : Ada.Streams.Stream_IO.File_Type;
   OutH : Ada.Streams.Stream_IO.File_Type;

   A, B  : VString := Nul;
   Line  : VString := Nul;
   Name0 : VString := Nul;
   Name1 : VString := Nul;
   Oval  : VString := Nul;
   Restl : VString := Nul;

   Name_Ref : constant Pattern := Span (' ') * A & Break (' ') * Name0
                                  & Span (' ') * B
                                  & ": constant Name_Id := N + $;"
                                  & Rest * Restl;

   Get_Name : constant Pattern := "Name_" & Rest * Name1;
   Chk_Low  : constant Pattern := Pos (0) & Any (Lower_Set) & Rest & Pos (1);
   Findu    : constant Pattern := Span ('u') * A;

   Val : Natural;

   Xlate_U_Und : constant Character_Mapping := To_Mapping ("u", "_");

   M : Match_Result;

   type Header_Symbol is (None, Name, Attr, Conv, Prag);
   --  A symbol in the header file

   procedure Output_Header_Line (S : Header_Symbol);
   --  Output header line

   Header_Name : aliased String := "Name";
   Header_Attr : aliased String := "Attr";
   Header_Conv : aliased String := "Convention";
   Header_Prag : aliased String := "Pragma";
   --  Prefixes used in the header file

   type String_Ptr is access all String;
   Header_Prefix : constant array (Header_Symbol) of String_Ptr :=
                     (null,
                      Header_Name'Access,
                      Header_Attr'Access,
                      Header_Conv'Access,
                      Header_Prag'Access);

   --  Patterns used in the spec file

   Get_Attr : constant Pattern := Span (' ') & "Attribute_"
                                  & Break (",)") * Name1;
   Get_Conv : constant Pattern := Span (' ') & "Convention_"
                                  & Break (",)") * Name1;
   Get_Prag : constant Pattern := Span (' ') & "Pragma_"
                                  & Break (",)") * Name1;

   type Header_Symbol_Counter is array (Header_Symbol) of Natural;
   Header_Counter : Header_Symbol_Counter := (0, 0, 0, 0, 0);

   Header_Current_Symbol : Header_Symbol := None;
   Header_Pending_Line : VString := Nul;

   ------------------------
   -- 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
      --  offset to N, while other items are enumeration values).

      function Make_Value (V : Integer) return String is
      begin
         if S = Name then
            return "(First_Name_Id + 256 + " & V & ")";
         else
            return "" & V;
         end if;
      end Make_Value;

   begin
      --  Skip all the #define 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
            Name2 : VString;
            Pat : constant Pattern := "#define  "
                                       & Header_Prefix (S).all
                                       & Break (' ') * Name2;
            In_Pat : Boolean := False;

         begin
            if Header_Current_Symbol /= None then
               Put_Line (OutH, Header_Pending_Line);
            end if;

            loop
               Line := Get_Line (InH);

               if Match (Line, Pat) then
                  In_Pat := True;
               elsif In_Pat then
                  Header_Pending_Line := Line;
                  exit;
               else
                  Put_Line (OutH, Line);
               end if;
            end loop;

            Header_Current_Symbol := S;
         end;
      end if;

      --  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
                  & "_" & Name1
                  & (30 - Natural'Min (29, Length (Name1))) * ' '
                  & Make_Value (Header_Counter (S)));
      Header_Counter (S) := Header_Counter (S) + 1;
   end Output_Header_Line;

--  Start of processing for XSnames

begin
   Open (InS, In_File, "snames.ads-tmpl");
   Open (InB, In_File, "snames.adb-tmpl");
   Open (InH, In_File, "snames.h-tmpl");

   --  Note that we do not generate snames.{ads,adb,h} directly. Instead
   --  we output them to snames.n{s,b,h} so that Makefiles can use
   --  move-if-change to not touch previously generated files if the
   --  new ones are identical.

   Create (OutS, Out_File, "snames.ns");
   Create (OutB, Out_File, "snames.nb");
   Create (OutH, Out_File, "snames.nh");

   Anchored_Mode := True;
   Val := 0;

   loop
      Line := Get_Line (InB);
      exit when Match (Line, "   Preset_Names");
      Put_Line (OutB, Line);
   end loop;

   Put_Line (OutB, Line);

   LoopN : while not End_Of_File (InS) loop
      Line := Get_Line (InS);

      if not Match (Line, Name_Ref) then
         Put_Line (OutS, Line);

         if Match (Line, Get_Attr) then
            Output_Header_Line (Attr);
         elsif Match (Line, Get_Conv) then
            Output_Header_Line (Conv);
         elsif Match (Line, Get_Prag) then
            Output_Header_Line (Prag);
         end if;
      else
         Oval := Lpad (V (Val), 3, '0');

         if Match (Name0, "Last_") then
            Oval := Lpad (V (Val - 1), 3, '0');
         end if;

         Put_Line
           (OutS, A & Name0 & B & ": constant Name_Id := N + "
            & Oval & ';' & Restl);

         if Match (Name0, Get_Name) then
            Name0 := Name1;
            Val   := Val + 1;

            if Match (Name0, Findu, M) then
               Replace (M, Translate (A, Xlate_U_Und));
               Translate (Name0, Lower_Case_Map);

            elsif not Match (Name0, "Op_", "") then
               Translate (Name0, Lower_Case_Map);

            else
               Name0 := 'O' & Translate (Name0, Lower_Case_Map);
            end if;

            if Name0 = "error" then
               Name0 := V ("<error>");
            end if;

            if not Match (Name0, Chk_Low) then
               Put_Line (OutB, "     """ & Name0 & "#"" &");
            end if;

            Output_Header_Line (Name);
         end if;
      end if;
   end loop LoopN;

   loop
      Line := Get_Line (InB);
      exit when Match (Line, "     ""#"";");
   end loop;

   Put_Line (OutB, Line);

   while not End_Of_File (InB) loop
      Line := Get_Line (InB);
      Put_Line (OutB, Line);
   end loop;

   Put_Line (OutH, Header_Pending_Line);
   while not End_Of_File (InH) loop
      Line := Get_Line (InH);
      Put_Line (OutH, Line);
   end loop;
end XSnamesT;