* crtstuff.c (__dso_handle): Set section from
[official-gcc.git] / gcc / ada / xtreeprs.adb
blob59116e76475b03c08c970d3bffa74bf98800518b
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 ------------------------------------------------------------------------------
27 -- Program to construct the spec of the Treeprs package
29 -- Input files:
31 -- sinfo.ads Spec of Sinfo package
32 -- treeprs.adt Template for Treeprs package
34 -- Output files:
36 -- treeprs.ads Spec of Treeprs package
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
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.
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;
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;
55 procedure XTreeprs is
57 package TB renames GNAT.Spitbol.Table_Boolean;
58 package TV renames GNAT.Spitbol.Table_VString;
60 Err : exception;
61 -- Raised on fatal error
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;
79 subtype Sfile is Ada.Streams.Stream_IO.File_Type;
81 OutS : Sfile;
82 -- Output file
84 InS : Ada.Text_IO.File_Type;
85 -- Read sinfo.ads
87 InT : Ada.Text_IO.File_Type;
88 -- Read treeprs.adt
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.
95 Names : array (1 .. 500) of VString;
96 -- Table of names of synonyms
98 Positions : array (1 .. 500) of Natural;
99 -- Table of starting positions in Pchars string for synonyms
101 Strings : TV.Table (300);
102 -- Contribution of each synonym to Pchars string, indexed by name
104 Count : Natural := 0;
105 -- Number of synonyms processed so far
107 Curpos : Natural := 1;
108 -- Number of characters generated in Pchars string so far
110 Lineno : Natural := 0;
111 -- Line number in sinfo.ads
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#)).
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#)).
121 Fieldch : Character;
122 -- Field character, as per above tables
124 Sp : aliased Natural;
125 -- Space left on line for Pchars output
127 wsp : Pattern := Span (' ' & ASCII.HT);
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;
141 M : Match_Result;
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
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;
153 procedure Put_Line (F : Sfile; S : VString) is
154 begin
155 Put_Line (F, To_String (S));
156 end Put_Line;
158 -- Start of processing for XTreeprs
160 begin
161 Anchored_Mode := True;
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;
169 Open (InS, In_File, "sinfo.ads");
170 Open (InT, In_File, "treeprs.adt");
172 -- Initialize special fields table
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);
191 -- Read template header and generate new header
193 loop
194 Line := Get_Line (InT);
196 -- Skip lines describing the template
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;
205 exit when Match (Line, "package");
207 if Match (Line, Is_Temp, M) then
208 Replace (M, A & " S p e c ");
209 end if;
211 Put_Line (OutS, Line);
212 end loop;
214 Put_Line (OutS, Line);
216 -- Copy rest of comments up to template insert point to spec
218 loop
219 Line := Get_Line (InT);
220 exit when Match (Line, "!!TEMPLATE INSERTION POINT");
221 Put_Line (OutS, Line);
222 end loop;
224 -- Here we are doing the actual insertions
226 Put_Line (OutS, " Pchars : constant String :=");
228 -- Loop through comments describing nodes, picking up fields
230 loop
231 Line := Get_Line (InS);
232 Lineno := Lineno + 1;
233 exit when Match (Line, " type Node_Kind");
235 if Match (Line, Get_Node)
236 and then not Match (Node, Tst_Punc)
237 then
238 Outstring := Node & ' ';
240 loop
241 Line := Get_Line (InS);
242 exit when Line = "";
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:
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'
275 if Match (Field, Brk_Min) then
276 Field := Ffield;
277 end if;
279 if Match (Field, Is_Flag) then
280 Fieldch := Char (Flag_Base - 1 + N (Flagno));
282 elsif Match (Field, Is_Field) then
283 Fieldch := Char (Field_Base - 1 + N (Fieldno));
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;
295 Append (Outstring, Fieldch & Synonym);
296 end if;
297 end loop;
299 Set (Strings, Node, Outstring);
300 end if;
301 end loop;
303 -- Loop through actual definitions of node kind enumeration literals
305 loop
306 loop
307 Line := Get_Line (InS);
308 Lineno := Lineno + 1;
309 exit when Match (Line, Is_Syn);
310 end loop;
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 = ")";
322 -- Loop to output the string literal for Pchars
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;
332 Put_Line (OutS, Prefix & '"' & S & """ &");
333 end loop;
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'(");
343 -- Output lines for Pchar_Pos_Array values
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;
350 Name := Rpad ("N_" & Names (Count), 40);
351 Put_Line (OutS, " " & Name & " => " & Positions (Count) & ");");
353 Put_Line (OutS, "");
354 Put_Line (OutS, "end Treeprs;");
356 exception
357 when Err =>
358 Put_Line (Standard_Error, "*** fatal error");
359 Set_Exit_Status (1);
361 end XTreeprs;