2010-12-20 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / ada / xeinfo.adb
blobba9ded9d5cc1892d9e332798fb46e783aafca1cf
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT SYSTEM UTILITIES --
4 -- --
5 -- X E I N F O --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 -- Program to construct C header file a-einfo.h (C version of einfo.ads spec)
27 -- for use by Gigi. This header file contains all definitions and access
28 -- functions, but does not contain set procedures, since Gigi is not allowed
29 -- to modify the GNAT tree)
31 -- Input files:
33 -- einfo.ads spec of Einfo package
34 -- einfo.adb body of Einfo package
36 -- Output files:
38 -- a-einfo.h Corresponding c header file
40 -- Note: It is assumed that the input files have been compiled without errors
42 -- An optional argument allows the specification of an output file name to
43 -- override the default a-einfo.h file name for the generated output file.
45 -- Most, but not all of the functions in Einfo can be inlined in the C header.
46 -- They are the functions identified by pragma Inline in the spec. Functions
47 -- that cannot be inlined are simply defined in the header.
49 with Ada.Command_Line; use Ada.Command_Line;
50 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
51 with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
52 with Ada.Strings.Maps; use Ada.Strings.Maps;
53 with Ada.Strings.Maps.Constants; use Ada.Strings.Maps.Constants;
54 with Ada.Text_IO; use Ada.Text_IO;
56 with GNAT.Spitbol; use GNAT.Spitbol;
57 with GNAT.Spitbol.Patterns; use GNAT.Spitbol.Patterns;
58 with GNAT.Spitbol.Table_Boolean; use GNAT.Spitbol.Table_Boolean;
60 with CEinfo;
62 procedure XEinfo is
64 package TB renames GNAT.Spitbol.Table_Boolean;
66 Err : exception;
68 A : VString := Nul;
69 B : VString := Nul;
70 C : VString := Nul;
71 Expr : VString := Nul;
72 Filler : VString := Nul;
73 Fline : VString := Nul;
74 Formal : VString := Nul;
75 Formaltyp : VString := Nul;
76 FN : VString := Nul;
77 Line : VString := Nul;
78 N : VString := Nul;
79 N1 : VString := Nul;
80 N2 : VString := Nul;
81 N3 : VString := Nul;
82 Nam : VString := Nul;
83 Name : VString := Nul;
84 NewS : VString := Nul;
85 Nextlin : VString := Nul;
86 OldS : VString := Nul;
87 Rtn : VString := Nul;
88 Term : VString := Nul;
90 InB : File_Type;
91 -- Used to read initial header from body
93 InF : File_Type;
94 -- Used to read full text of both spec and body
96 Ofile : File_Type;
97 -- Used to write output file
99 wsp : constant Pattern := NSpan (' ' & ASCII.HT);
100 Comment : constant Pattern := wsp & "--";
101 For_Rep : constant Pattern := wsp & "for";
102 Get_Func : constant Pattern := wsp * A & "function" & wsp
103 & Break (' ') * Name;
104 Inline : constant Pattern := wsp & "pragma Inline (" & Break (')') * Name;
105 Get_Pack : constant Pattern := wsp & "package ";
106 Get_Enam : constant Pattern := wsp & Break (',') * N & ',';
107 Find_Fun : constant Pattern := wsp & "function";
108 F_Subtyp : constant Pattern := wsp * A & "subtype " & Break (' ') * N;
109 G_Subtyp : constant Pattern := wsp & "subtype" & wsp & Break (' ') * NewS
110 & wsp & "is" & wsp & Break (" ;") * OldS
111 & wsp & ';' & wsp & Rtab (0);
112 F_Typ : constant Pattern := wsp * A & "type " & Break (' ') * N &
113 " is (";
114 Get_Nam : constant Pattern := wsp * A & Break (",)") * Nam
115 & Len (1) * Term;
116 Get_Styp : constant Pattern := wsp * A & "subtype " & Break (' ') * N;
117 Get_N1 : constant Pattern := wsp & Break (' ') * N1;
118 Get_N2 : constant Pattern := wsp & "-- " & Rest * N2;
119 Get_N3 : constant Pattern := wsp & Break (';') * N3;
120 Get_FN : constant Pattern := wsp * C & "function" & wsp
121 & Break (" (") * FN;
122 Is_Rturn : constant Pattern := BreakX ('r') & "return";
123 Is_Begin : constant Pattern := wsp & "begin";
124 Get_Asrt : constant Pattern := wsp & "pragma Assert";
125 Semicoln : constant Pattern := BreakX (';');
126 Get_Cmnt : constant Pattern := BreakX ('-') * A & "--";
127 Get_Expr : constant Pattern := wsp & "return " & Break (';') * Expr;
128 Chek_End : constant Pattern := wsp & "end" & BreakX (';') & ';';
129 Get_B1 : constant Pattern := BreakX (' ') * A & " in " & Rest * B;
130 Get_B2 : constant Pattern := BreakX (' ') * A & " = " & Rest * B;
131 Get_B3 : constant Pattern := BreakX (' ') * A & " /= " & Rest * B;
132 To_Paren : constant Pattern := wsp * Filler & '(';
133 Get_Fml : constant Pattern := Break (" :") * Formal & wsp & ':' & wsp
134 & BreakX (" );") * Formaltyp;
135 Nxt_Fml : constant Pattern := wsp & "; ";
136 Get_Rtn : constant Pattern := wsp & "return" & wsp & BreakX (" ;") * Rtn;
137 Rem_Prn : constant Pattern := wsp & ')';
139 M : Match_Result;
141 Lineno : Natural := 0;
142 -- Line number in spec
144 V : Natural;
145 Ctr : Natural;
147 Inlined : TB.Table (200);
148 -- Inlined<N> = True for inlined function, False otherwise
150 Lastinlined : Boolean;
152 procedure Badfunc;
153 -- Signal bad function in body
155 function Getlin return VString;
156 -- Get non-comment line (comment lines skipped, also skips FOR rep clauses)
157 -- Fatal error (raises End_Error exception) if end of file encountered
159 procedure Must (B : Boolean);
160 -- Raises Err if the argument (a Match) call, returns False
162 procedure Sethead (Line : in out VString; Term : String);
163 -- Process function header into C
165 -------------
166 -- Badfunc --
167 -------------
169 procedure Badfunc is
170 begin
171 Put_Line
172 (Standard_Error,
173 "Body for function " & FN & " does not meet requirements");
174 raise Err;
175 end Badfunc;
177 -------------
178 -- Getlin --
179 -------------
181 function Getlin return VString is
182 Lin : VString;
184 begin
185 loop
186 Lin := Get_Line (InF);
187 Lineno := Lineno + 1;
189 if Lin /= ""
190 and then not Match (Lin, Comment)
191 and then not Match (Lin, For_Rep)
192 then
193 return Lin;
194 end if;
195 end loop;
196 end Getlin;
198 ----------
199 -- Must --
200 ----------
202 procedure Must (B : Boolean) is
203 begin
204 if not B then
205 raise Err;
206 end if;
207 end Must;
209 -------------
210 -- Sethead --
211 -------------
213 procedure Sethead (Line : in out VString; Term : String) is
214 Args : VString;
216 begin
217 Must (Match (Line, Get_Func, ""));
218 Args := Nul;
220 if Match (Line, To_Paren, "") then
221 Args := Filler & '(';
223 loop
224 Must (Match (Line, Get_Fml, ""));
225 Append (Args, Formaltyp & ' ' & Formal);
226 exit when not Match (Line, Nxt_Fml);
227 Append (Args, ",");
228 end loop;
230 Match (Line, Rem_Prn, "");
231 Append (Args, ')');
232 end if;
234 Must (Match (Line, Get_Rtn));
236 if Present (Inlined, Name) then
237 Put_Line (Ofile, A & "INLINE " & Rtn & ' ' & Name & Args & Term);
238 else
239 Put_Line (Ofile, A & Rtn & ' ' & Name & Args & Term);
240 end if;
241 end Sethead;
243 -- Start of processing for XEinfo
245 begin
246 -- First run CEinfo to check for errors. Note that CEinfo is also a
247 -- stand-alone program that can be run separately.
249 CEinfo;
251 Anchored_Mode := True;
253 if Argument_Count > 0 then
254 Create (Ofile, Out_File, Argument (1));
255 else
256 Create (Ofile, Out_File, "a-einfo.h");
257 end if;
259 Open (InB, In_File, "einfo.adb");
260 Open (InF, In_File, "einfo.ads");
262 Lineno := 0;
263 loop
264 Line := Get_Line (InF);
265 Lineno := Lineno + 1;
266 exit when Line = "";
268 Match (Line,
269 "-- S p e c ",
270 "-- C Header File ");
271 Match (Line, "--", "/*");
272 Match (Line, Rtab (2) * A & "--", M);
273 Replace (M, A & "*/");
274 Put_Line (Ofile, Line);
275 end loop;
277 Put_Line (Ofile, "");
279 -- Find and record pragma Inlines
281 loop
282 Line := Get_Line (InF);
283 exit when Match (Line, " -- END XEINFO INLINES");
285 if Match (Line, Inline) then
286 Set (Inlined, Name, True);
287 end if;
288 end loop;
290 -- Skip to package line
292 Reset (InF, In_File);
293 Lineno := 0;
295 loop
296 Line := Getlin;
297 exit when Match (Line, Get_Pack);
298 end loop;
300 V := 0;
301 Line := Getlin;
302 Must (Match (Line, wsp & "type Entity_Kind"));
304 -- Process entity kind code definitions
306 loop
307 Line := Getlin;
308 exit when not Match (Line, Get_Enam);
309 Put_Line (Ofile, " #define " & Rpad (N, 32) & " " & V);
310 V := V + 1;
311 end loop;
313 Must (Match (Line, wsp & Rest * N));
314 Put_Line (Ofile, " #define " & Rpad (N, 32) & ' ' & V);
315 Line := Getlin;
317 Must (Match (Line, wsp & ");"));
318 Put_Line (Ofile, "");
320 -- Loop through subtype and type declarations
322 loop
323 Line := Getlin;
324 exit when Match (Line, Find_Fun);
326 -- Case of a subtype declaration
328 if Match (Line, F_Subtyp) then
330 -- Case of a subtype declaration that is an abbreviation of the
331 -- form subtype x is y, and if so generate the appropriate typedef
333 if Match (Line, G_Subtyp) then
334 Put_Line (Ofile, A & "typedef " & OldS & ' ' & NewS & ';');
336 -- Otherwise the subtype must be declaring a subrange of Entity_Id
338 else
339 Must (Match (Line, Get_Styp));
340 Line := Getlin;
341 Must (Match (Line, Get_N1));
343 loop
344 Line := Get_Line (InF);
345 Lineno := Lineno + 1;
346 exit when not Match (Line, Get_N2);
347 end loop;
349 Must (Match (Line, Get_N3));
350 Put_Line (Ofile, A & "SUBTYPE (" & N & ", Entity_Kind, ");
351 Put_Line (Ofile, A & " " & N1 & ", " & N3 & ')');
352 Put_Line (Ofile, "");
353 end if;
355 -- Case of type declaration
357 elsif Match (Line, F_Typ) then
359 -- Process type declaration (must be enumeration type)
361 Ctr := 0;
362 Put_Line (Ofile, A & "typedef char " & N & ';');
364 loop
365 Line := Getlin;
366 Must (Match (Line, Get_Nam));
367 Put_Line (Ofile, A & "#define " & Rpad (Nam, 25) & Ctr);
368 Ctr := Ctr + 1;
369 exit when Term /= ",";
370 end loop;
372 Put_Line (Ofile, "");
374 -- Neither subtype nor type declaration
376 else
377 raise Err;
378 end if;
379 end loop;
381 -- Process function declarations
383 -- Note: Lastinlined used to control blank lines
385 Put_Line (Ofile, "");
386 Lastinlined := True;
388 -- Loop through function declarations
390 while Match (Line, Get_FN) loop
392 -- Non-inlined function
394 if not Present (Inlined, FN) then
395 Put_Line (Ofile, "");
396 Put_Line
397 (Ofile,
398 " #define " & FN & " einfo__" & Translate (FN, Lower_Case_Map));
400 -- Inlined function
402 else
403 if not Lastinlined then
404 Put_Line (Ofile, "");
405 end if;
406 end if;
408 -- Merge here to output spec
410 Sethead (Line, ";");
411 Lastinlined := Get (Inlined, FN);
412 Line := Getlin;
413 end loop;
415 Put_Line (Ofile, "");
417 -- Read body to find inlined functions
419 Close (InB);
420 Close (InF);
421 Open (InF, In_File, "einfo.adb");
422 Lineno := 0;
424 -- Loop through input lines to find bodies of inlined functions
426 while not End_Of_File (InF) loop
427 Fline := Get_Line (InF);
429 if Match (Fline, Get_FN)
430 and then Get (Inlined, FN)
431 then
432 -- Here we have an inlined function
434 if not Match (Fline, Is_Rturn) then
435 Line := Fline;
436 Badfunc;
437 end if;
439 Line := Getlin;
441 if not Match (Line, Is_Begin) then
442 Badfunc;
443 end if;
445 -- Skip past pragma Asserts
447 loop
448 Line := Getlin;
449 exit when not Match (Line, Get_Asrt);
451 -- Pragma assert found, get its continuation lines
453 loop
454 exit when Match (Line, Semicoln);
455 Line := Getlin;
456 end loop;
457 end loop;
459 -- Process return statement
461 Match (Line, Get_Cmnt, M);
462 Replace (M, A);
464 -- Get continuations of return statement
466 while not Match (Line, Semicoln) loop
467 Nextlin := Getlin;
468 Match (Nextlin, wsp, " ");
469 Append (Line, Nextlin);
470 end loop;
472 if not Match (Line, Get_Expr) then
473 Badfunc;
474 end if;
476 Line := Getlin;
478 if not Match (Line, Chek_End) then
479 Badfunc;
480 end if;
482 Match (Expr, Get_B1, M);
483 Replace (M, "IN (" & A & ", " & B & ')');
484 Match (Expr, Get_B2, M);
485 Replace (M, A & " == " & B);
486 Match (Expr, Get_B3, M);
487 Replace (M, A & " != " & B);
488 Put_Line (Ofile, "");
489 Sethead (Fline, "");
490 Put_Line (Ofile, C & " { return " & Expr & "; }");
491 end if;
492 end loop;
494 Put_Line (Ofile, "");
495 Put_Line
496 (Ofile,
497 "/* End of einfo.h (C version of Einfo package specification) */");
499 Close (InF);
500 Close (Ofile);
502 exception
503 when Err =>
504 Put_Line (Standard_Error, Lineno & ". " & Line);
505 Put_Line (Standard_Error, "**** fatal error ****");
506 Set_Exit_Status (1);
508 when End_Error =>
509 Put_Line (Standard_Error, "unexpected end of file");
510 Put_Line (Standard_Error, "**** fatal error ****");
512 end XEinfo;