OSDN Git Service

* config/vax/vax.h (target_flags, MASK_UNIX_ASM, MASK_VAXC_ALIGNMENT)
[pf3gnuchains/gcc-fork.git] / gcc / ada / xtreeprs.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                          GNAT SYSTEM UTILITIES                           --
4 --                                                                          --
5 --                             X T R E E P R S                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005 Free Software Foundation, Inc.          --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 --  Program to construct the spec of the Treeprs package
28
29 --    Input files:
30
31 --       sinfo.ads     Spec of Sinfo package
32 --       treeprs.adt   Template for Treeprs package
33
34 --    Output files:
35
36 --       treeprs.ads   Spec of Treeprs package
37
38 --  Note: this program assumes that sinfo.ads has passed the error checks which
39 --  are carried out by the CSinfo utility so it does not duplicate these checks
40
41 --  An optional argument allows the specification of an output file name to
42 --  override the default treeprs.ads file name for the generated output file.
43
44 with Ada.Command_Line;              use Ada.Command_Line;
45 with Ada.Strings.Unbounded;         use Ada.Strings.Unbounded;
46 with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
47 with Ada.Text_IO;                   use Ada.Text_IO;
48 with Ada.Streams.Stream_IO;         use Ada.Streams.Stream_IO;
49
50 with GNAT.Spitbol;                  use GNAT.Spitbol;
51 with GNAT.Spitbol.Patterns;         use GNAT.Spitbol.Patterns;
52 with GNAT.Spitbol.Table_Boolean;    use GNAT.Spitbol.Table_Boolean;
53 with GNAT.Spitbol.Table_VString;    use GNAT.Spitbol.Table_VString;
54
55 procedure XTreeprs is
56
57    package TB renames GNAT.Spitbol.Table_Boolean;
58    package TV renames GNAT.Spitbol.Table_VString;
59
60    Err : exception;
61    --  Raised on fatal error
62
63    A          : VString := Nul;
64    Ffield     : VString := Nul;
65    Field      : VString := Nul;
66    Fieldno    : VString := Nul;
67    Flagno     : VString := Nul;
68    Line       : VString := Nul;
69    Name       : VString := Nul;
70    Node       : VString := Nul;
71    Outstring  : VString := Nul;
72    Prefix     : VString := Nul;
73    S          : VString := Nul;
74    S1         : VString := Nul;
75    Syn        : VString := Nul;
76    Synonym    : VString := Nul;
77    Term       : VString := Nul;
78
79    subtype Sfile is Ada.Streams.Stream_IO.File_Type;
80
81    OutS : Sfile;
82    --  Output file
83
84    InS : Ada.Text_IO.File_Type;
85    --  Read sinfo.ads
86
87    InT : Ada.Text_IO.File_Type;
88    --  Read treeprs.adt
89
90    Special : TB.Table (20);
91    --  Table of special fields. These fields are not included in the table
92    --  constructed by Xtreeprs, since they are specially handled in treeprs.
93    --  This means these field definitions are completely ignored.
94
95    Names : array (1 .. 500) of VString;
96    --  Table of names of synonyms
97
98    Positions : array (1 .. 500) of Natural;
99    --  Table of starting positions in Pchars string for synonyms
100
101    Strings : TV.Table (300);
102    --  Contribution of each synonym to Pchars string, indexed by name
103
104    Count  : Natural := 0;
105    --  Number of synonyms processed so far
106
107    Curpos : Natural := 1;
108    --  Number of characters generated in Pchars string so far
109
110    Lineno : Natural := 0;
111    --  Line number in sinfo.ads
112
113    Field_Base : constant := Character'Pos ('#');
114    --  Fields 1-5 are represented by the characters #$%&' (i.e. by five
115    --  contiguous characters starting at # (16#23#)).
116
117    Flag_Base : constant := Character'Pos ('(');
118    --  Flags 1-18 are represented by the characters ()*+,-./0123456789
119    --  (i.e. by 18 contiguous characters starting at (16#28#)).
120
121    Fieldch : Character;
122    --  Field character, as per above tables
123
124    Sp : aliased Natural;
125    --  Space left on line for Pchars output
126
127    wsp : Pattern := Span (' ' & ASCII.HT);
128
129    Is_Temp  : Pattern := BreakX ('T') * A & "T e m p l a t e";
130    Get_Node : Pattern := wsp & "--  N_" & Rest * Node;
131    Tst_Punc : Pattern := Break (" ,.");
132    Get_Syn  : Pattern := Span (' ') & "--  " & Break (' ') * Synonym
133                 & " (" & Break (')') * Field;
134    Brk_Min  : Pattern := Break ('-') * Ffield;
135    Is_Flag  : Pattern := "Flag" & Rest * Flagno;
136    Is_Field : Pattern := Rtab (1) & Len (1) * Fieldno;
137    Is_Syn   : Pattern := wsp & "N_" & Break (",)") * Syn & Len (1) * Term;
138    Brk_Node : Pattern := Break (' ') * Node & ' ';
139    Chop_SP  : Pattern := Len (Sp'Unrestricted_Access) * S1;
140
141    M : Match_Result;
142
143    procedure Put_Line (F : Sfile; S : String);
144    procedure Put_Line (F : Sfile; S : VString);
145    --  Local version of Put_Line ensures Unix style line endings
146
147    procedure Put_Line (F : Sfile; S : String) is
148    begin
149       String'Write (Stream (F), S);
150       Character'Write (Stream (F), ASCII.LF);
151    end Put_Line;
152
153    procedure Put_Line (F : Sfile; S : VString) is
154    begin
155       Put_Line (F, To_String (S));
156    end Put_Line;
157
158 --  Start of processing for XTreeprs
159
160 begin
161    Anchored_Mode := True;
162
163    if Argument_Count > 0 then
164       Create (OutS, Out_File, Argument (1));
165    else
166       Create (OutS, Out_File, "treeprs.ads");
167    end if;
168
169    Open (InS, In_File, "sinfo.ads");
170    Open (InT, In_File, "treeprs.adt");
171
172    --  Initialize special fields table
173
174    Set (Special, "Analyzed",                True);
175    Set (Special, "Cannot_Be_Constant",      True);
176    Set (Special, "Chars",                   True);
177    Set (Special, "Comes_From_Source",       True);
178    Set (Special, "Error_Posted",            True);
179    Set (Special, "Etype",                   True);
180    Set (Special, "Has_No_Side_Effects",     True);
181    Set (Special, "Is_Controlling_Actual",   True);
182    Set (Special, "Is_Overloaded",           True);
183    Set (Special, "Is_Static_Expression",    True);
184    Set (Special, "Left_Opnd",               True);
185    Set (Special, "Must_Check_Expr",         True);
186    Set (Special, "No_Overflow_Expr",        True);
187    Set (Special, "Paren_Count",             True);
188    Set (Special, "Raises_Constraint_Error", True);
189    Set (Special, "Right_Opnd",              True);
190
191    --  Read template header and generate new header
192
193    loop
194       Line := Get_Line (InT);
195
196       --  Skip lines describing the template
197
198       if Match (Line, "--  This file is a template") then
199          loop
200             Line := Get_Line (InT);
201             exit when Line = "";
202          end loop;
203       end if;
204
205       exit when Match (Line, "package");
206
207       if Match (Line, Is_Temp, M) then
208          Replace (M, A & "    S p e c    ");
209       end if;
210
211       Put_Line (OutS, Line);
212    end loop;
213
214    Put_Line (OutS, Line);
215
216    --  Copy rest of comments up to template insert point to spec
217
218    loop
219       Line := Get_Line (InT);
220       exit when Match (Line, "!!TEMPLATE INSERTION POINT");
221       Put_Line (OutS, Line);
222    end loop;
223
224    --  Here we are doing the actual insertions
225
226    Put_Line (OutS, "   Pchars : constant String :=");
227
228    --  Loop through comments describing nodes, picking up fields
229
230    loop
231       Line := Get_Line (InS);
232       Lineno := Lineno + 1;
233       exit when Match (Line, "   type Node_Kind");
234
235       if Match (Line, Get_Node)
236         and then not Match (Node, Tst_Punc)
237       then
238          Outstring := Node & ' ';
239
240          loop
241             Line := Get_Line (InS);
242             exit when Line = "";
243
244             if Match (Line, Get_Syn)
245               and then not Match (Synonym, "plus")
246               and then not Present (Special, Synonym)
247             then
248                --  Convert this field into the character used to
249                --  represent the field according to the table:
250
251                --    Field1       '#'
252                --    Field2       '$'
253                --    Field3       '%'
254                --    Field4       '&'
255                --    Field5       "'"
256                --    Flag1        "("
257                --    Flag2        ")"
258                --    Flag3        '*'
259                --    Flag4        '+'
260                --    Flag5        ','
261                --    Flag6        '-'
262                --    Flag7        '.'
263                --    Flag8        '/'
264                --    Flag9        '0'
265                --    Flag10       '1'
266                --    Flag11       '2'
267                --    Flag12       '3'
268                --    Flag13       '4'
269                --    Flag14       '5'
270                --    Flag15       '6'
271                --    Flag16       '7'
272                --    Flag17       '8'
273                --    Flag18       '9'
274
275                if Match (Field, Brk_Min) then
276                   Field := Ffield;
277                end if;
278
279                if Match (Field, Is_Flag) then
280                   Fieldch := Char (Flag_Base - 1 + N (Flagno));
281
282                elsif Match (Field, Is_Field) then
283                   Fieldch := Char (Field_Base - 1 + N (Fieldno));
284
285                else
286                   Put_Line
287                     (Standard_Error,
288                      "*** Line " &
289                       Lineno &
290                       " has unrecognized field name " &
291                       Field);
292                   raise Err;
293                end if;
294
295                Append (Outstring, Fieldch & Synonym);
296             end if;
297          end loop;
298
299          Set (Strings, Node, Outstring);
300       end if;
301    end loop;
302
303    --  Loop through actual definitions of node kind enumeration literals
304
305    loop
306       loop
307          Line := Get_Line (InS);
308          Lineno := Lineno + 1;
309          exit when Match (Line, Is_Syn);
310       end loop;
311
312       S := Get (Strings, Syn);
313       Match (S, Brk_Node, "");
314       Count := Count + 1;
315       Names (Count) := Syn;
316       Positions (Count) := Curpos;
317       Curpos := Curpos + Length (S);
318       Put_Line (OutS, "      --  " & Node);
319       Prefix := V ("      ");
320       exit when Term = ")";
321
322       --  Loop to output the string literal for Pchars
323
324       loop
325          Sp := 79 - 4 - Length (Prefix);
326          exit when Size (S) <= Sp;
327          Match (S, Chop_SP, "");
328          Put_Line (OutS, Prefix & '"' & S1 & """ &");
329          Prefix := V ("         ");
330       end loop;
331
332       Put_Line (OutS, Prefix & '"' & S & """ &");
333    end loop;
334
335    Put_Line (OutS, "      """";");
336    Put_Line (OutS, "");
337    Put_Line
338      (OutS, "   type Pchar_Pos_Array is array (Node_Kind) of Positive;");
339    Put_Line
340      (OutS,
341       "   Pchar_Pos : constant Pchar_Pos_Array := Pchar_Pos_Array'(");
342
343    --  Output lines for Pchar_Pos_Array values
344
345    for M in 1 .. Count - 1 loop
346       Name := Rpad ("N_" & Names (M), 40);
347       Put_Line (OutS, "      " & Name & " => " & Positions (M) & ',');
348    end loop;
349
350    Name := Rpad ("N_" & Names (Count), 40);
351    Put_Line (OutS, "      " & Name & " => " & Positions (Count) & ");");
352
353    Put_Line (OutS, "");
354    Put_Line (OutS, "end Treeprs;");
355
356 exception
357    when Err =>
358       Put_Line (Standard_Error, "*** fatal error");
359       Set_Exit_Status (1);
360
361 end XTreeprs;