1 ------------------------------------------------------------------------------
3 -- GNAT SYSTEM UTILITIES --
9 -- Copyright (C) 1992-2007, 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;
66 pragma Warnings (Off);
67 -- These seem not to be referenced, but they are (by * operator)
72 Expr : VString := Nul;
73 Filler : VString := Nul;
74 Fline : VString := Nul;
75 Formal : VString := Nul;
76 Formaltyp : VString := Nul;
78 Line : VString := Nul;
84 Name : VString := Nul;
85 NewS : VString := Nul;
86 Nextlin : VString := Nul;
87 OldS : VString := Nul;
89 Term : VString := Nul;
94 -- Used to read initial header from body
97 -- Used to read full text of both spec and body
100 -- Used to write output file
102 wsp : constant Pattern := NSpan (' ' & ASCII.HT);
103 Comment : constant Pattern := wsp & "--";
104 For_Rep : constant Pattern := wsp & "for";
105 Get_Func : constant Pattern := wsp * A & "function" & wsp
106 & Break (' ') * Name;
107 Inline : constant Pattern := wsp & "pragma Inline (" & Break (')') * Name;
108 Get_Pack : constant Pattern := wsp & "package ";
109 Get_Enam : constant Pattern := wsp & Break (',') * N & ',';
110 Find_Fun : constant Pattern := wsp & "function";
111 F_Subtyp : constant Pattern := wsp * A & "subtype " & Break (' ') * N;
112 G_Subtyp : constant Pattern := wsp & "subtype" & wsp & Break (' ') * NewS
113 & wsp & "is" & wsp & Break (" ;") * OldS
114 & wsp & ';' & wsp & Rtab (0);
115 F_Typ : constant Pattern := wsp * A & "type " & Break (' ') * N &
117 Get_Nam : constant Pattern := wsp * A & Break (",)") * Nam
119 Get_Styp : constant Pattern := wsp * A & "subtype " & Break (' ') * N;
120 Get_N1 : constant Pattern := wsp & Break (' ') * N1;
121 Get_N2 : constant Pattern := wsp & "-- " & Rest * N2;
122 Get_N3 : constant Pattern := wsp & Break (';') * N3;
123 Get_FN : constant Pattern := wsp * C & "function" & wsp
125 Is_Rturn : constant Pattern := BreakX ('r') & "return";
126 Is_Begin : constant Pattern := wsp & "begin";
127 Get_Asrt : constant Pattern := wsp & "pragma Assert";
128 Semicoln : constant Pattern := BreakX (';');
129 Get_Cmnt : constant Pattern := BreakX ('-') * A & "--";
130 Get_Expr : constant Pattern := wsp & "return " & Break (';') * Expr;
131 Chek_End : constant Pattern := wsp & "end" & BreakX (';') & ';';
132 Get_B1 : constant Pattern := BreakX (' ') * A & " in " & Rest * B;
133 Get_B2 : constant Pattern := BreakX (' ') * A & " = " & Rest * B;
134 Get_B3 : constant Pattern := BreakX (' ') * A & " /= " & Rest * B;
135 To_Paren : constant Pattern := wsp * Filler & '(';
136 Get_Fml : constant Pattern := Break (" :") * Formal & wsp & ':' & wsp
137 & BreakX (" );") * Formaltyp;
138 Nxt_Fml : constant Pattern := wsp & "; ";
139 Get_Rtn : constant Pattern := wsp & "return" & wsp & BreakX (" ;") * Rtn;
140 Rem_Prn : constant Pattern := wsp & ')';
144 Lineno : Natural := 0;
145 -- Line number in spec
150 Inlined : TB.Table (200);
151 -- Inlined<N> = True for inlined function, False otherwise
153 Lastinlined : Boolean;
156 -- Signal bad function in body
158 function Getlin return VString;
159 -- Get non-comment line (comment lines skipped, also skips FOR rep clauses)
160 -- Fatal error (raises End_Error exception) if end of file encountered
162 procedure Must (B : Boolean);
163 -- Raises Err if the argument (a Match) call, returns False
165 procedure Sethead (Line : in out VString; Term : String);
166 -- Process function header into C
176 "Body for function " & FN & " does not meet requirements");
184 function Getlin return VString is
189 Lin := Get_Line (InF);
190 Lineno := Lineno + 1;
193 and then not Match (Lin, Comment)
194 and then not Match (Lin, For_Rep)
205 procedure Must (B : Boolean) is
216 procedure Sethead (Line : in out VString; Term : String) is
220 Must (Match (Line, Get_Func, ""));
223 if Match (Line, To_Paren, "") then
224 Args := Filler & '(';
227 Must (Match (Line, Get_Fml, ""));
228 Append (Args, Formaltyp & ' ' & Formal);
229 exit when not Match (Line, Nxt_Fml);
233 Match (Line, Rem_Prn, "");
237 Must (Match (Line, Get_Rtn));
239 if Present (Inlined, Name) then
240 Put_Line (Ofile, A & "INLINE " & Rtn & ' ' & Name & Args & Term);
242 Put_Line (Ofile, A & Rtn & ' ' & Name & Args & Term);
246 -- Start of processing for XEinfo
249 Anchored_Mode := True;
251 if Argument_Count > 0 then
252 Create (Ofile, Out_File, Argument (1));
254 Create (Ofile, Out_File, "a-einfo.h");
257 Open (InB, In_File, "einfo.adb");
258 Open (InF, In_File, "einfo.ads");
262 Line := Get_Line (InF);
263 Lineno := Lineno + 1;
268 "-- C Header File ");
269 Match (Line, "--", "/*");
270 Match (Line, Rtab (2) * A & "--", M);
271 Replace (M, A & "*/");
272 Put_Line (Ofile, Line);
275 Put_Line (Ofile, "");
277 -- Find and record pragma Inlines
280 Line := Get_Line (InF);
281 exit when Match (Line, " -- END XEINFO INLINES");
283 if Match (Line, Inline) then
284 Set (Inlined, Name, True);
288 -- Skip to package line
290 Reset (InF, In_File);
295 exit when Match (Line, Get_Pack);
300 Must (Match (Line, wsp & "type Entity_Kind"));
302 -- Process entity kind code definitions
306 exit when not Match (Line, Get_Enam);
307 Put_Line (Ofile, " #define " & Rpad (N, 32) & " " & V);
311 Must (Match (Line, wsp & Rest * N));
312 Put_Line (Ofile, " #define " & Rpad (N, 32) & ' ' & V);
315 Must (Match (Line, wsp & ");"));
316 Put_Line (Ofile, "");
318 -- Loop through subtype and type declarations
322 exit when Match (Line, Find_Fun);
324 -- Case of a subtype declaration
326 if Match (Line, F_Subtyp) then
328 -- Case of a subtype declaration that is an abbreviation of the
329 -- form subtype x is y, and if so generate the appropriate typedef
331 if Match (Line, G_Subtyp) then
332 Put_Line (Ofile, A & "typedef " & OldS & ' ' & NewS & ';');
334 -- Otherwise the subtype must be declaring a subrange of Entity_Id
337 Must (Match (Line, Get_Styp));
339 Must (Match (Line, Get_N1));
342 Line := Get_Line (InF);
343 Lineno := Lineno + 1;
344 exit when not Match (Line, Get_N2);
347 Must (Match (Line, Get_N3));
348 Put_Line (Ofile, A & "SUBTYPE (" & N & ", Entity_Kind, ");
349 Put_Line (Ofile, A & " " & N1 & ", " & N3 & ')');
350 Put_Line (Ofile, "");
353 -- Case of type declaration
355 elsif Match (Line, F_Typ) then
356 -- Process type declaration (must be enumeration type)
359 Put_Line (Ofile, A & "typedef char " & N & ';');
363 Must (Match (Line, Get_Nam));
364 Put_Line (Ofile, A & "#define " & Rpad (Nam, 25) & Ctr);
366 exit when Term /= ",";
369 Put_Line (Ofile, "");
371 -- Neither subtype nor type declaration
378 -- Process function declarations
379 -- Note: Lastinlined used to control blank lines
381 Put_Line (Ofile, "");
384 -- Loop through function declarations
386 while Match (Line, Get_FN) loop
388 -- Non-inlined funcion
390 if not Present (Inlined, FN) then
391 Put_Line (Ofile, "");
394 " #define " & FN & " einfo__" & Translate (FN, Lower_Case_Map));
399 if not Lastinlined then
400 Put_Line (Ofile, "");
404 -- Merge here to output spec
407 Lastinlined := Get (Inlined, FN);
411 Put_Line (Ofile, "");
413 -- Read body to find inlined functions
417 Open (InF, In_File, "einfo.adb");
420 -- Loop through input lines to find bodies of inlined functions
422 while not End_Of_File (InF) loop
423 Fline := Get_Line (InF);
425 if Match (Fline, Get_FN)
426 and then Get (Inlined, FN)
428 -- Here we have an inlined function
430 if not Match (Fline, Is_Rturn) then
437 if not Match (Line, Is_Begin) then
441 -- Skip past pragma Asserts
445 exit when not Match (Line, Get_Asrt);
447 -- Pragma asser found, get its continuation lines
450 exit when Match (Line, Semicoln);
455 -- Process return statement
457 Match (Line, Get_Cmnt, M);
460 -- Get continuations of return statemnt
462 while not Match (Line, Semicoln) loop
464 Match (Nextlin, wsp, " ");
465 Append (Line, Nextlin);
468 if not Match (Line, Get_Expr) then
474 if not Match (Line, Chek_End) then
478 Match (Expr, Get_B1, M);
479 Replace (M, "IN (" & A & ", " & B & ')');
480 Match (Expr, Get_B2, M);
481 Replace (M, A & " == " & B);
482 Match (Expr, Get_B3, M);
483 Replace (M, A & " != " & B);
484 Put_Line (Ofile, "");
486 Put_Line (Ofile, C & " { return " & Expr & "; }");
490 Put_Line (Ofile, "");
493 "/* End of einfo.h (C version of Einfo package specification) */");
497 Put_Line (Standard_Error, Lineno & ". " & Line);
498 Put_Line (Standard_Error, "**** fatal error ****");
502 Put_Line (Standard_Error, "unexpected end of file");
503 Put_Line (Standard_Error, "**** fatal error ****");