1 ------------------------------------------------------------------------------
3 -- GNAT SYSTEM UTILITIES --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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)
33 -- einfo.ads spec of Einfo package
34 -- einfo.adb body of Einfo package
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;
62 package TB renames GNAT.Spitbol.Table_Boolean;
69 Expr : VString := Nul;
70 Filler : VString := Nul;
71 Fline : VString := Nul;
72 Formal : VString := Nul;
73 Formaltyp : VString := Nul;
75 Line : VString := Nul;
81 Name : VString := Nul;
82 NewS : VString := Nul;
83 Nextlin : VString := Nul;
84 OldS : VString := Nul;
86 Term : VString := Nul;
89 -- Used to read initial header from body
92 -- Used to read full text of both spec and body
95 -- Used to write output file
97 wsp : constant Pattern := NSpan (' ' & ASCII.HT);
98 Comment : constant Pattern := wsp & "--";
99 For_Rep : constant Pattern := wsp & "for";
100 Get_Func : constant Pattern := wsp * A & "function" & wsp
101 & Break (' ') * Name;
102 Inline : constant Pattern := wsp & "pragma Inline (" & Break (')') * Name;
103 Get_Pack : constant Pattern := wsp & "package ";
104 Get_Enam : constant Pattern := wsp & Break (',') * N & ',';
105 Find_Fun : constant Pattern := wsp & "function";
106 F_Subtyp : constant Pattern := wsp * A & "subtype " & Break (' ') * N;
107 G_Subtyp : constant Pattern := wsp & "subtype" & wsp & Break (' ') * NewS
108 & wsp & "is" & wsp & Break (" ;") * OldS
109 & wsp & ';' & wsp & Rtab (0);
110 F_Typ : constant Pattern := wsp * A & "type " & Break (' ') * N &
112 Get_Nam : constant Pattern := wsp * A & Break (",)") * Nam
114 Get_Styp : constant Pattern := wsp * A & "subtype " & Break (' ') * N;
115 Get_N1 : constant Pattern := wsp & Break (' ') * N1;
116 Get_N2 : constant Pattern := wsp & "-- " & Rest * N2;
117 Get_N3 : constant Pattern := wsp & Break (';') * N3;
118 Get_FN : constant Pattern := wsp * C & "function" & wsp
120 Is_Rturn : constant Pattern := BreakX ('r') & "return";
121 Is_Begin : constant Pattern := wsp & "begin";
122 Get_Asrt : constant Pattern := wsp & "pragma Assert";
123 Semicoln : constant Pattern := BreakX (';');
124 Get_Cmnt : constant Pattern := BreakX ('-') * A & "--";
125 Get_Expr : constant Pattern := wsp & "return " & Break (';') * Expr;
126 Chek_End : constant Pattern := wsp & "end" & BreakX (';') & ';';
127 Get_B1 : constant Pattern := BreakX (' ') * A & " in " & Rest * B;
128 Get_B2 : constant Pattern := BreakX (' ') * A & " = " & Rest * B;
129 Get_B3 : constant Pattern := BreakX (' ') * A & " /= " & Rest * B;
130 To_Paren : constant Pattern := wsp * Filler & '(';
131 Get_Fml : constant Pattern := Break (" :") * Formal & wsp & ':' & wsp
132 & BreakX (" );") * Formaltyp;
133 Nxt_Fml : constant Pattern := wsp & "; ";
134 Get_Rtn : constant Pattern := wsp & "return" & wsp & BreakX (" ;") * Rtn;
135 Rem_Prn : constant Pattern := wsp & ')';
139 Lineno : Natural := 0;
140 -- Line number in spec
145 Inlined : TB.Table (200);
146 -- Inlined<N> = True for inlined function, False otherwise
148 Lastinlined : Boolean;
151 -- Signal bad function in body
153 function Getlin return VString;
154 -- Get non-comment line (comment lines skipped, also skips FOR rep clauses)
155 -- Fatal error (raises End_Error exception) if end of file encountered
157 procedure Must (B : Boolean);
158 -- Raises Err if the argument (a Match) call, returns False
160 procedure Sethead (Line : in out VString; Term : String);
161 -- Process function header into C
171 "Body for function " & FN & " does not meet requirements");
179 function Getlin return VString is
184 Lin := Get_Line (InF);
185 Lineno := Lineno + 1;
188 and then not Match (Lin, Comment)
189 and then not Match (Lin, For_Rep)
200 procedure Must (B : Boolean) is
211 procedure Sethead (Line : in out VString; Term : String) is
215 Must (Match (Line, Get_Func, ""));
218 if Match (Line, To_Paren, "") then
219 Args := Filler & '(';
222 Must (Match (Line, Get_Fml, ""));
223 Append (Args, Formaltyp & ' ' & Formal);
224 exit when not Match (Line, Nxt_Fml);
228 Match (Line, Rem_Prn, "");
232 Must (Match (Line, Get_Rtn));
234 if Present (Inlined, Name) then
235 Put_Line (Ofile, A & "INLINE " & Rtn & ' ' & Name & Args & Term);
237 Put_Line (Ofile, A & Rtn & ' ' & Name & Args & Term);
241 -- Start of processing for XEinfo
244 Anchored_Mode := True;
246 if Argument_Count > 0 then
247 Create (Ofile, Out_File, Argument (1));
249 Create (Ofile, Out_File, "a-einfo.h");
252 Open (InB, In_File, "einfo.adb");
253 Open (InF, In_File, "einfo.ads");
257 Line := Get_Line (InF);
258 Lineno := Lineno + 1;
263 "-- C Header File ");
264 Match (Line, "--", "/*");
265 Match (Line, Rtab (2) * A & "--", M);
266 Replace (M, A & "*/");
267 Put_Line (Ofile, Line);
270 Put_Line (Ofile, "");
272 -- Find and record pragma Inlines
275 Line := Get_Line (InF);
276 exit when Match (Line, " -- END XEINFO INLINES");
278 if Match (Line, Inline) then
279 Set (Inlined, Name, True);
283 -- Skip to package line
285 Reset (InF, In_File);
290 exit when Match (Line, Get_Pack);
295 Must (Match (Line, wsp & "type Entity_Kind"));
297 -- Process entity kind code definitions
301 exit when not Match (Line, Get_Enam);
302 Put_Line (Ofile, " #define " & Rpad (N, 32) & " " & V);
306 Must (Match (Line, wsp & Rest * N));
307 Put_Line (Ofile, " #define " & Rpad (N, 32) & ' ' & V);
310 Must (Match (Line, wsp & ");"));
311 Put_Line (Ofile, "");
313 -- Loop through subtype and type declarations
317 exit when Match (Line, Find_Fun);
319 -- Case of a subtype declaration
321 if Match (Line, F_Subtyp) then
323 -- Case of a subtype declaration that is an abbreviation of the
324 -- form subtype x is y, and if so generate the appropriate typedef
326 if Match (Line, G_Subtyp) then
327 Put_Line (Ofile, A & "typedef " & OldS & ' ' & NewS & ';');
329 -- Otherwise the subtype must be declaring a subrange of Entity_Id
332 Must (Match (Line, Get_Styp));
334 Must (Match (Line, Get_N1));
337 Line := Get_Line (InF);
338 Lineno := Lineno + 1;
339 exit when not Match (Line, Get_N2);
342 Must (Match (Line, Get_N3));
343 Put_Line (Ofile, A & "SUBTYPE (" & N & ", Entity_Kind, ");
344 Put_Line (Ofile, A & " " & N1 & ", " & N3 & ')');
345 Put_Line (Ofile, "");
348 -- Case of type declaration
350 elsif Match (Line, F_Typ) then
352 -- Process type declaration (must be enumeration type)
355 Put_Line (Ofile, A & "typedef char " & N & ';');
359 Must (Match (Line, Get_Nam));
360 Put_Line (Ofile, A & "#define " & Rpad (Nam, 25) & Ctr);
362 exit when Term /= ",";
365 Put_Line (Ofile, "");
367 -- Neither subtype nor type declaration
374 -- Process function declarations
376 -- Note: Lastinlined used to control blank lines
378 Put_Line (Ofile, "");
381 -- Loop through function declarations
383 while Match (Line, Get_FN) loop
385 -- Non-inlined function
387 if not Present (Inlined, FN) then
388 Put_Line (Ofile, "");
391 " #define " & FN & " einfo__" & Translate (FN, Lower_Case_Map));
396 if not Lastinlined then
397 Put_Line (Ofile, "");
401 -- Merge here to output spec
404 Lastinlined := Get (Inlined, FN);
408 Put_Line (Ofile, "");
410 -- Read body to find inlined functions
414 Open (InF, In_File, "einfo.adb");
417 -- Loop through input lines to find bodies of inlined functions
419 while not End_Of_File (InF) loop
420 Fline := Get_Line (InF);
422 if Match (Fline, Get_FN)
423 and then Get (Inlined, FN)
425 -- Here we have an inlined function
427 if not Match (Fline, Is_Rturn) then
434 if not Match (Line, Is_Begin) then
438 -- Skip past pragma Asserts
442 exit when not Match (Line, Get_Asrt);
444 -- Pragma assert found, get its continuation lines
447 exit when Match (Line, Semicoln);
452 -- Process return statement
454 Match (Line, Get_Cmnt, M);
457 -- Get continuations of return statement
459 while not Match (Line, Semicoln) loop
461 Match (Nextlin, wsp, " ");
462 Append (Line, Nextlin);
465 if not Match (Line, Get_Expr) then
471 if not Match (Line, Chek_End) then
475 Match (Expr, Get_B1, M);
476 Replace (M, "IN (" & A & ", " & B & ')');
477 Match (Expr, Get_B2, M);
478 Replace (M, A & " == " & B);
479 Match (Expr, Get_B3, M);
480 Replace (M, A & " != " & B);
481 Put_Line (Ofile, "");
483 Put_Line (Ofile, C & " { return " & Expr & "; }");
487 Put_Line (Ofile, "");
490 "/* End of einfo.h (C version of Einfo package specification) */");
494 Put_Line (Standard_Error, Lineno & ". " & Line);
495 Put_Line (Standard_Error, "**** fatal error ****");
499 Put_Line (Standard_Error, "unexpected end of file");
500 Put_Line (Standard_Error, "**** fatal error ****");