1 ------------------------------------------------------------------------------
3 -- GNAT SYSTEM UTILITIES --
9 -- Copyright (C) 1992-2018, 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 the spec and body of the Nmake package
30 -- sinfo.ads Spec of Sinfo package
31 -- nmake.adt Template for Nmake package
35 -- nmake.ads Spec of Nmake package
36 -- nmake.adb Body of Nmake package
38 -- Note: this program assumes that sinfo.ads has passed the error checks that
39 -- are carried out by the csinfo utility, so it does not duplicate these
40 -- checks and assumes that sinfo.ads has the correct form.
42 -- In the absence of any switches, both the ads and adb files are output.
43 -- The switch -s or /s indicates that only the ads file is to be output.
44 -- The switch -b or /b indicates that only the adb file is to be output.
46 -- If a file name argument is given, then the output is written to this file
47 -- rather than to nmake.ads or nmake.adb. A file name can only be given if
48 -- exactly one of the -s or -b options is present.
50 with Ada
.Command_Line
; use Ada
.Command_Line
;
51 with Ada
.Strings
.Unbounded
; use Ada
.Strings
.Unbounded
;
52 with Ada
.Strings
.Unbounded
.Text_IO
; use Ada
.Strings
.Unbounded
.Text_IO
;
53 with Ada
.Strings
.Maps
; use Ada
.Strings
.Maps
;
54 with Ada
.Strings
.Maps
.Constants
; use Ada
.Strings
.Maps
.Constants
;
55 with Ada
.Streams
.Stream_IO
; use Ada
.Streams
.Stream_IO
;
56 with Ada
.Text_IO
; use Ada
.Text_IO
;
58 with GNAT
.Spitbol
; use GNAT
.Spitbol
;
59 with GNAT
.Spitbol
.Patterns
; use GNAT
.Spitbol
.Patterns
;
66 -- Raised to terminate execution
70 Arg_List
: VString
:= Nul
;
71 Comment
: VString
:= Nul
;
72 Default
: VString
:= Nul
;
73 Field
: VString
:= Nul
;
74 Line
: VString
:= Nul
;
75 Node
: VString
:= Nul
;
76 Op_Name
: VString
:= Nul
;
77 Prevl
: VString
:= Nul
;
78 Synonym
: VString
:= Nul
;
83 FileS
: VString
:= V
("nmake.ads");
84 FileB
: VString
:= V
("nmake.adb");
85 -- Set to null if corresponding file not to be generated
87 Given_File
: VString
:= Nul
;
88 -- File name given by command line argument
90 subtype Sfile
is Ada
.Streams
.Stream_IO
.File_Type
;
92 InS
, InT
: Ada
.Text_IO
.File_Type
;
95 wsp
: constant Pattern
:= Span
(' ' & ASCII
.HT
);
97 Body_Only
: constant Pattern
:= BreakX
(' ') * X
98 & Span
(' ') & "-- body only";
99 Spec_Only
: constant Pattern
:= BreakX
(' ') * X
100 & Span
(' ') & "-- spec only";
102 Node_Hdr
: constant Pattern
:= wsp
& "-- N_" & Rest
* Node
;
103 Punc
: constant Pattern
:= BreakX
(" .,");
105 Binop
: constant Pattern
:= wsp
106 & "-- plus fields for binary operator";
107 Unop
: constant Pattern
:= wsp
108 & "-- plus fields for unary operator";
109 Syn
: constant Pattern
:= wsp
& "-- " & Break
(' ') * Synonym
110 & " (" & Break
(')') * Field
113 Templ
: constant Pattern
:= BreakX
('T') * A
& "T e m p l a t e";
114 Spec
: constant Pattern
:= BreakX
('S') * A
& "S p e c";
116 Sem_Field
: constant Pattern
:= BreakX
('-') & "-Sem";
117 Lib_Field
: constant Pattern
:= BreakX
('-') & "-Lib";
119 Get_Field
: constant Pattern
:= BreakX
(Decimal_Digit_Set
) * Field
;
121 Get_Dflt
: constant Pattern
:= BreakX
('(') & "(set to "
122 & Break
(" ") * Default
& " if";
124 Next_Arg
: constant Pattern
:= Break
(',') * Arg
& ',';
126 Op_Node
: constant Pattern
:= "Op_" & Rest
* Op_Name
;
128 Shft_Rot
: constant Pattern
:= "Shift_" or "Rotate_";
130 No_Ent
: constant Pattern
:= "Or_Else" or "And_Then"
135 V_String_Id
: constant VString
:= V
("String_Id");
136 V_Node_Id
: constant VString
:= V
("Node_Id");
137 V_Name_Id
: constant VString
:= V
("Name_Id");
138 V_List_Id
: constant VString
:= V
("List_Id");
139 V_Elist_Id
: constant VString
:= V
("Elist_Id");
140 V_Boolean
: constant VString
:= V
("Boolean");
142 procedure Put_Line
(F
: Sfile
; S
: String) renames XUtil
.Put_Line
;
143 procedure Put_Line
(F
: Sfile
; S
: VString
) renames XUtil
.Put_Line
;
144 -- Local version of Put_Line ensures Unix style line endings
146 procedure WriteS
(S
: String);
147 procedure WriteB
(S
: String);
148 procedure WriteBS
(S
: String);
149 procedure WriteS
(S
: VString
);
150 procedure WriteB
(S
: VString
);
151 procedure WriteBS
(S
: VString
);
152 -- Write given line to spec or body file or both if active
154 procedure WriteB
(S
: String) is
161 procedure WriteB
(S
: VString
) is
168 procedure WriteBS
(S
: String) is
179 procedure WriteBS
(S
: VString
) is
190 procedure WriteS
(S
: String) is
197 procedure WriteS
(S
: VString
) is
204 -- Start of processing for XNmake
208 Anchored_Mode
:= True;
210 for ArgN
in 1 .. Argument_Count
loop
212 Arg
: constant String := Argument
(ArgN
);
215 if Arg
(1) = '-' then
217 and then (Arg
(2) = 'b' or else Arg
(2) = 'B')
222 and then (Arg
(2) = 's' or else Arg
(2) = 'S')
231 if Given_File
/= Nul
then
234 Given_File
:= V
(Arg
);
240 if FileS
= Nul
and then FileB
= Nul
then
243 elsif Given_File
/= Nul
then
247 elsif FileS
= Nul
then
255 Open
(InS
, In_File
, "sinfo.ads");
256 Open
(InT
, In_File
, "nmake.adt");
259 Create
(OutS
, Out_File
, S
(FileS
));
263 Create
(OutB
, Out_File
, S
(FileB
));
266 Anchored_Mode
:= True;
268 -- Copy initial part of template to spec and body
271 Line
:= Get_Line
(InT
);
273 -- Skip lines describing the template
275 if Match
(Line
, "-- This file is a template") then
277 Line
:= Get_Line
(InT
);
282 -- Loop keeps going until "package" keyword written
284 exit when Match
(Line
, "package");
286 -- Deal with WITH lines, writing to body or spec as appropriate
288 if Match
(Line
, Body_Only
, M
) then
292 elsif Match
(Line
, Spec_Only
, M
) then
296 -- Change header from Template to Spec and write to spec file
299 if Match
(Line
, Templ
, M
) then
300 Replace
(M
, A
& " S p e c ");
305 -- Write header line to body file
307 if Match
(Line
, Spec
, M
) then
308 Replace
(M
, A
& "B o d y");
315 -- Package line reached
317 WriteS
("package Nmake is");
318 WriteB
("package body Nmake is");
321 -- Copy rest of lines up to template insert point to spec only
324 Line
:= Get_Line
(InT
);
325 exit when Match
(Line
, "!!TEMPLATE INSERTION POINT");
329 -- Here we are doing the actual insertions, loop through node types
332 Line
:= Get_Line
(InS
);
334 if Match
(Line
, Node_Hdr
)
335 and then not Match
(Node
, Punc
)
336 and then Node
/= "Unused"
338 exit when Node
= "Empty";
339 Prevl
:= " function Make_" & Node
& " (Sloc : Source_Ptr";
342 -- Loop through fields of one node
345 Line
:= Get_Line
(InS
);
348 if Match
(Line
, Binop
) then
349 WriteBS
(Prevl
& ';');
350 Append
(Arg_List
, "Left_Opnd,Right_Opnd,");
352 " " & Rpad
("Left_Opnd", NWidth
) & " : Node_Id;");
354 " " & Rpad
("Right_Opnd", NWidth
) & " : Node_Id";
356 elsif Match
(Line
, Unop
) then
357 WriteBS
(Prevl
& ';');
358 Append
(Arg_List
, "Right_Opnd,");
359 Prevl
:= " " & Rpad
("Right_Opnd", NWidth
) & " : Node_Id";
361 elsif Match
(Line
, Syn
) then
362 if Synonym
/= "Prev_Ids"
363 and then Synonym
/= "More_Ids"
364 and then Synonym
/= "Comes_From_Source"
365 and then Synonym
/= "Paren_Count"
366 and then not Match
(Field
, Sem_Field
)
367 and then not Match
(Field
, Lib_Field
)
369 Match
(Field
, Get_Field
);
371 if Field
= "Str" then
372 Field
:= V_String_Id
;
373 elsif Field
= "Node" then
375 elsif Field
= "Name" then
377 elsif Field
= "List" then
379 elsif Field
= "Elist" then
381 elsif Field
= "Flag" then
385 if Field
= "Boolean" then
386 Default
:= V
("False");
391 Match
(Comment
, Get_Dflt
);
393 WriteBS
(Prevl
& ';');
394 Append
(Arg_List
, Synonym
& ',');
395 Rpad
(Synonym
, NWidth
);
398 Prevl
:= " " & Synonym
& " : " & Field
;
401 " " & Synonym
& " : " & Field
& " := " & Default
;
407 WriteBS
(Prevl
& ')');
408 WriteS
(" return Node_Id;");
409 WriteS
(" pragma Inline (Make_" & Node
& ");");
410 WriteB
(" return Node_Id");
412 WriteB
(" N : constant Node_Id :=");
414 if Match
(Node
, "Defining_Identifier") or else
415 Match
(Node
, "Defining_Character") or else
416 Match
(Node
, "Defining_Operator")
418 WriteB
(" New_Entity (N_" & Node
& ", Sloc);");
420 WriteB
(" New_Node (N_" & Node
& ", Sloc);");
425 while Match
(Arg_List
, Next_Arg
, "") loop
426 if Length
(Arg
) < NWidth
then
427 WriteB
(" Set_" & Arg
& " (N, " & Arg
& ");");
429 WriteB
(" Set_" & Arg
);
430 WriteB
(" (N, " & Arg
& ");");
434 if Match
(Node
, Op_Node
) then
435 if Node
= "Op_Plus" then
436 WriteB
(" Set_Chars (N, Name_Op_Add);");
438 elsif Node
= "Op_Minus" then
439 WriteB
(" Set_Chars (N, Name_Op_Subtract);");
441 elsif Match
(Op_Name
, Shft_Rot
) then
442 WriteB
(" Set_Chars (N, Name_" & Op_Name
& ");");
445 WriteB
(" Set_Chars (N, Name_" & Node
& ");");
448 if not Match
(Op_Name
, No_Ent
) then
449 WriteB
(" Set_Entity (N, Standard_" & Node
& ");");
453 WriteB
(" return N;");
454 WriteB
(" end Make_" & Node
& ';');
459 WriteBS
("end Nmake;");
464 Put_Line
(Standard_Error
, "usage: xnmake [-b] [-s] [filename]");