1 ------------------------------------------------------------------------------
3 -- GNAT SYSTEM UTILITIES --
9 -- Copyright (C) 1992-2011, 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;
64 package TB renames GNAT.Spitbol.Table_Boolean;
71 Expr : VString := Nul;
72 Filler : VString := Nul;
73 Fline : VString := Nul;
74 Formal : VString := Nul;
75 Formaltyp : VString := Nul;
77 Line : VString := Nul;
83 Name : VString := Nul;
84 NewS : VString := Nul;
85 Nextlin : VString := Nul;
86 OldS : VString := Nul;
88 Term : VString := Nul;
91 -- Used to read initial header from body
94 -- Used to read full text of both spec and body
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 &
114 Get_Nam : constant Pattern := wsp * A & Break (",)") * Nam
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
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 & ')';
141 Lineno : Natural := 0;
142 -- Line number in spec
147 Inlined : TB.Table (200);
148 -- Inlined<N> = True for inlined function, False otherwise
150 Lastinlined : Boolean;
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
173 "Body for function " & FN & " does not meet requirements");
181 function Getlin return VString is
186 Lin := Get_Line (InF);
187 Lineno := Lineno + 1;
190 and then not Match (Lin, Comment)
191 and then not Match (Lin, For_Rep)
202 procedure Must (B : Boolean) is
213 procedure Sethead (Line : in out VString; Term : String) is
217 Must (Match (Line, Get_Func, ""));
220 if Match (Line, To_Paren, "") then
221 Args := Filler & '(';
224 Must (Match (Line, Get_Fml, ""));
225 Append (Args, Formaltyp & ' ' & Formal);
226 exit when not Match (Line, Nxt_Fml);
230 Match (Line, Rem_Prn, "");
234 Must (Match (Line, Get_Rtn));
236 if Present (Inlined, Name) then
237 Put_Line (Ofile, A & "INLINE " & Rtn & ' ' & Name & Args & Term);
239 Put_Line (Ofile, A & Rtn & ' ' & Name & Args & Term);
243 -- Start of processing for XEinfo
246 -- First run CEinfo to check for errors. Note that CEinfo is also a
247 -- stand-alone program that can be run separately.
251 Anchored_Mode := True;
253 if Argument_Count > 0 then
254 Create (Ofile, Out_File, Argument (1));
256 Create (Ofile, Out_File, "a-einfo.h");
259 Open (InB, In_File, "einfo.adb");
260 Open (InF, In_File, "einfo.ads");
264 Line := Get_Line (InF);
265 Lineno := Lineno + 1;
270 "-- C Header File ");
271 Match (Line, "--", "/*");
272 Match (Line, Rtab (2) * A & "--", M);
273 Replace (M, A & "*/");
274 Put_Line (Ofile, Line);
277 Put_Line (Ofile, "");
279 Put_Line (Ofile, "#ifdef __cplusplus");
280 Put_Line (Ofile, "extern ""C"" {");
281 Put_Line (Ofile, "#endif");
283 -- Find and record pragma Inlines
286 Line := Get_Line (InF);
287 exit when Match (Line, " -- END XEINFO INLINES");
289 if Match (Line, Inline) then
290 Set (Inlined, Name, True);
294 -- Skip to package line
296 Reset (InF, In_File);
301 exit when Match (Line, Get_Pack);
306 Must (Match (Line, wsp & "type Entity_Kind"));
308 -- Process entity kind code definitions
312 exit when not Match (Line, Get_Enam);
313 Put_Line (Ofile, " #define " & Rpad (N, 32) & " " & V);
317 Must (Match (Line, wsp & Rest * N));
318 Put_Line (Ofile, " #define " & Rpad (N, 32) & ' ' & V);
321 Must (Match (Line, wsp & ");"));
322 Put_Line (Ofile, "");
324 -- Loop through subtype and type declarations
328 exit when Match (Line, Find_Fun);
330 -- Case of a subtype declaration
332 if Match (Line, F_Subtyp) then
334 -- Case of a subtype declaration that is an abbreviation of the
335 -- form subtype x is y, and if so generate the appropriate typedef
337 if Match (Line, G_Subtyp) then
338 Put_Line (Ofile, A & "typedef " & OldS & ' ' & NewS & ';');
340 -- Otherwise the subtype must be declaring a subrange of Entity_Id
343 Must (Match (Line, Get_Styp));
345 Must (Match (Line, Get_N1));
348 Line := Get_Line (InF);
349 Lineno := Lineno + 1;
350 exit when not Match (Line, Get_N2);
353 Must (Match (Line, Get_N3));
354 Put_Line (Ofile, A & "SUBTYPE (" & N & ", Entity_Kind, ");
355 Put_Line (Ofile, A & " " & N1 & ", " & N3 & ')');
356 Put_Line (Ofile, "");
359 -- Case of type declaration
361 elsif Match (Line, F_Typ) then
363 -- Process type declaration (must be enumeration type)
366 Put_Line (Ofile, A & "typedef char " & N & ';');
370 Must (Match (Line, Get_Nam));
371 Put_Line (Ofile, A & "#define " & Rpad (Nam, 25) & Ctr);
373 exit when Term /= ",";
376 Put_Line (Ofile, "");
378 -- Neither subtype nor type declaration
385 -- Process function declarations
387 -- Note: Lastinlined used to control blank lines
389 Put_Line (Ofile, "");
392 -- Loop through function declarations
394 while Match (Line, Get_FN) loop
396 -- Non-inlined function
398 if not Present (Inlined, FN) then
399 Put_Line (Ofile, "");
402 " #define " & FN & " einfo__" & Translate (FN, Lower_Case_Map));
407 if not Lastinlined then
408 Put_Line (Ofile, "");
412 -- Merge here to output spec
415 Lastinlined := Get (Inlined, FN);
419 Put_Line (Ofile, "");
421 -- Read body to find inlined functions
425 Open (InF, In_File, "einfo.adb");
428 -- Loop through input lines to find bodies of inlined functions
430 while not End_Of_File (InF) loop
431 Fline := Get_Line (InF);
433 if Match (Fline, Get_FN)
434 and then Get (Inlined, FN)
436 -- Here we have an inlined function
438 if not Match (Fline, Is_Rturn) then
445 if not Match (Line, Is_Begin) then
449 -- Skip past pragma Asserts
453 exit when not Match (Line, Get_Asrt);
455 -- Pragma assert found, get its continuation lines
458 exit when Match (Line, Semicoln);
463 -- Process return statement
465 Match (Line, Get_Cmnt, M);
468 -- Get continuations of return statement
470 while not Match (Line, Semicoln) loop
472 Match (Nextlin, wsp, " ");
473 Append (Line, Nextlin);
476 if not Match (Line, Get_Expr) then
482 if not Match (Line, Chek_End) then
486 Match (Expr, Get_B1, M);
487 Replace (M, "IN (" & A & ", " & B & ')');
488 Match (Expr, Get_B2, M);
489 Replace (M, A & " == " & B);
490 Match (Expr, Get_B3, M);
491 Replace (M, A & " != " & B);
492 Put_Line (Ofile, "");
494 Put_Line (Ofile, C & " { return " & Expr & "; }");
498 Put_Line (Ofile, "");
500 Put_Line (Ofile, "#ifdef __cplusplus");
501 Put_Line (Ofile, "}");
502 Put_Line (Ofile, "#endif");
506 "/* End of einfo.h (C version of Einfo package specification) */");
513 Put_Line (Standard_Error, Lineno & ". " & Line);
514 Put_Line (Standard_Error, "**** fatal error ****");
518 Put_Line (Standard_Error, "unexpected end of file");
519 Put_Line (Standard_Error, "**** fatal error ****");