1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2006, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Ada
.Unchecked_Deallocation
;
29 with Errout
; use Errout
;
30 with Namet
; use Namet
;
31 with Lib
.Writ
; use Lib
.Writ
;
33 with Osint
; use Osint
;
35 with Scans
; use Scans
;
37 with Sinput
.L
; use Sinput
.L
;
38 with Stringt
; use Stringt
;
41 package body Prepcomp
is
43 No_Preprocessing
: Boolean := True;
44 -- Set to True if there is at least one source that needs to be
47 Source_Index_Of_Preproc_Data_File
: Source_File_Index
:= No_Source_File
;
49 -- The following variable should be a constant, but this is not
50 -- possible. Warnings are Off because it is never assigned a value.
52 pragma Warnings
(Off
);
53 No_Mapping
: Prep
.Symbol_Table
.Instance
;
56 type String_Ptr
is access String;
57 type String_Array
is array (Positive range <>) of String_Ptr
;
58 type String_Array_Ptr
is access String_Array
;
61 new Ada
.Unchecked_Deallocation
(String_Array
, String_Array_Ptr
);
63 Symbol_Definitions
: String_Array_Ptr
:= new String_Array
(1 .. 4);
64 -- An extensible array to temporarily stores symbol definitions specified
65 -- on the command line with -gnateD switches.
67 Last_Definition
: Natural := 0;
68 -- Index of last symbol definition in array Symbol_Definitions
70 type Preproc_Data
is record
71 Mapping
: Symbol_Table
.Instance
;
72 File_Name
: Name_Id
:= No_Name
;
73 Deffile
: String_Id
:= No_String
;
74 Undef_False
: Boolean := False;
75 Always_Blank
: Boolean := False;
76 Comments
: Boolean := False;
77 List_Symbols
: Boolean := False;
78 Processed
: Boolean := False;
80 -- Structure to keep the preprocessing data for a file name or for the
81 -- default (when Name_Id = No_Name).
83 No_Preproc_Data
: constant Preproc_Data
:=
84 (Mapping
=> No_Mapping
,
88 Always_Blank
=> False,
90 List_Symbols
=> False,
93 Default_Data
: Preproc_Data
:= No_Preproc_Data
;
94 -- The preprocessing data to be used when no specific preprocessing data
95 -- is specified for a source.
97 Default_Data_Defined
: Boolean := False;
98 -- True if source for which no specific preprocessing is specified need to
99 -- be preprocess with the Default_Data.
101 Current_Data
: Preproc_Data
:= No_Preproc_Data
;
103 package Preproc_Data_Table
is new Table
.Table
104 (Table_Component_Type
=> Preproc_Data
,
105 Table_Index_Type
=> Int
,
106 Table_Low_Bound
=> 1,
108 Table_Increment
=> 5,
109 Table_Name
=> "Prepcomp.Preproc_Data_Table");
110 -- Table to store the specific preprocessing data
112 Command_Line_Symbols
: Symbol_Table
.Instance
;
113 -- A table to store symbol definitions specified on the command line with
116 package Dependencies
is new Table
.Table
117 (Table_Component_Type
=> Source_File_Index
,
118 Table_Index_Type
=> Int
,
119 Table_Low_Bound
=> 1,
121 Table_Increment
=> 5,
122 Table_Name
=> "Prepcomp.Dependencies");
123 -- Table to store the dependencies on preprocessing files
125 procedure Add_Command_Line_Symbols
;
126 -- Add the command line symbol definitions, if any, to the
127 -- Prep.Mapping table.
129 procedure Skip_To_End_Of_Line
;
130 -- Ignore errors and scan up to the next end of line or the end of file
132 ------------------------------
133 -- Add_Command_Line_Symbols --
134 ------------------------------
136 procedure Add_Command_Line_Symbols
is
137 Symbol_Id
: Prep
.Symbol_Id
;
140 for J
in 1 .. Symbol_Table
.Last
(Command_Line_Symbols
) loop
141 Symbol_Id
:= Prep
.Index_Of
(Command_Line_Symbols
.Table
(J
).Symbol
);
143 if Symbol_Id
= No_Symbol
then
144 Symbol_Table
.Increment_Last
(Prep
.Mapping
);
145 Symbol_Id
:= Symbol_Table
.Last
(Prep
.Mapping
);
148 Prep
.Mapping
.Table
(Symbol_Id
) := Command_Line_Symbols
.Table
(J
);
150 end Add_Command_Line_Symbols
;
152 ----------------------
153 -- Add_Dependencies --
154 ----------------------
156 procedure Add_Dependencies
is
158 for Index
in 1 .. Dependencies
.Last
loop
159 Add_Preprocessing_Dependency
(Dependencies
.Table
(Index
));
161 end Add_Dependencies
;
163 ---------------------------
164 -- Add_Symbol_Definition --
165 ---------------------------
167 procedure Add_Symbol_Definition
(Def
: String) is
169 -- If Symbol_Definitions is not large enough, double it
171 if Last_Definition
= Symbol_Definitions
'Last then
173 New_Symbol_Definitions
: constant String_Array_Ptr
:=
174 new String_Array
(1 .. 2 * Last_Definition
);
177 New_Symbol_Definitions
(Symbol_Definitions
'Range) :=
178 Symbol_Definitions
.all;
179 Free
(Symbol_Definitions
);
180 Symbol_Definitions
:= New_Symbol_Definitions
;
184 Last_Definition
:= Last_Definition
+ 1;
185 Symbol_Definitions
(Last_Definition
) := new String'(Def);
186 end Add_Symbol_Definition;
192 procedure Check_Symbols is
194 -- If there is at least one switch -gnateD specified
196 if Symbol_Table.Last (Command_Line_Symbols) >= 1 then
197 Current_Data := No_Preproc_Data;
198 No_Preprocessing := False;
199 Current_Data.Processed := True;
201 -- Start with an empty, initialized mapping table; use Prep.Mapping,
202 -- because Prep.Index_Of uses Prep.Mapping.
204 Prep.Mapping := No_Mapping;
205 Symbol_Table.Init (Prep.Mapping);
207 -- Add the command line symbols
209 Add_Command_Line_Symbols;
211 -- Put the resulting Prep.Mapping in Current_Data, and immediately
212 -- set Prep.Mapping to nil.
214 Current_Data.Mapping := Prep.Mapping;
215 Prep.Mapping := No_Mapping;
217 -- Set the default data
219 Default_Data := Current_Data;
220 Default_Data_Defined := True;
224 ------------------------------
225 -- Parse_Preprocessing_Data --
226 ------------------------------
228 procedure Parse_Preprocessing_Data_File (N : File_Name_Type) is
229 OK : Boolean := False;
230 Dash_Location : Source_Ptr;
231 Symbol_Data : Prep.Symbol_Data;
232 Symbol_Id : Prep.Symbol_Id;
233 T : constant Nat := Total_Errors_Detected;
236 -- Load the preprocessing data file
238 Source_Index_Of_Preproc_Data_File := Load_Preprocessing_Data_File (N);
240 -- Fail if preprocessing data file cannot be found
242 if Source_Index_Of_Preproc_Data_File = No_Source_File then
244 Fail ("preprocessing data file """,
245 Name_Buffer (1 .. Name_Len),
249 -- Initialize the sanner and set its behavior for a processing data file
251 Scn.Scanner.Initialize_Scanner (Source_Index_Of_Preproc_Data_File);
252 Scn.Scanner.Set_End_Of_Line_As_Token (True);
253 Scn.Scanner.Reset_Special_Characters;
259 exit For_Each_Line when Token = Tok_EOF;
261 if Token = Tok_End_Of_Line then
268 No_Preprocessing := False;
269 Current_Data := No_Preproc_Data;
276 if Default_Data_Defined then
278 ("multiple default preprocessing data", Token_Ptr);
282 Default_Data_Defined := True;
285 when Tok_String_Literal =>
289 String_To_Name_Buffer (String_Literal_Id);
290 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
291 Current_Data.File_Name := Name_Find;
294 for Index in 1 .. Preproc_Data_Table.Last loop
295 if Current_Data.File_Name =
296 Preproc_Data_Table.Table (Index).File_Name
298 Error_Msg_Name_1 := Current_Data.File_Name;
300 ("multiple preprocessing data for{", Token_Ptr);
307 Error_Msg ("`'*`
or literal
string expected
", Token_Ptr);
310 -- If there is a problem, skip the line
317 -- Scan past the * or the literal string
321 -- A literal string in second position is a definition file
323 if Token = Tok_String_Literal then
324 Current_Data.Deffile := String_Literal_Id;
325 Current_Data.Processed := False;
329 -- If there is no definition file, set Processed to True now
331 Current_Data.Processed := True;
334 -- Start with an empty, initialized mapping table; use Prep.Mapping,
335 -- because Prep.Index_Of uses Prep.Mapping.
337 Prep.Mapping := No_Mapping;
338 Symbol_Table.Init (Prep.Mapping);
340 -- Check the switches that may follow
342 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
343 if Token /= Tok_Minus then
344 Error_Msg ("`
'-` expected", Token_Ptr);
349 -- Keep the location of the '-' for possible error reporting
351 Dash_Location := Token_Ptr;
357 Change_Reserved_Keyword_To_Symbol;
359 -- An identifier (or a reserved word converted to an
360 -- identifier) is expected and there must be no blank space
361 -- between the '-' and the identifier.
363 if Token = Tok_Identifier
364 and then Token_Ptr = Dash_Location + 1
366 Get_Name_String (Token_Name);
368 -- Check the character in the source, because the case is
371 case Sinput.Source (Token_Ptr) is
374 -- Undefined symbol are False
377 Current_Data.Undef_False := True;
386 Current_Data.Always_Blank := True;
392 -- Comment removed lines
395 Current_Data.Comments := True;
404 Current_Data.List_Symbols := True;
416 -- A symbol must be an Ada identifier; it cannot start
417 -- with an underline or a digit.
419 if Name_Buffer (2) = '_
'
420 or Name_Buffer (2) in '0' .. '9'
422 Error_Msg ("symbol expected", Token_Ptr + 1);
427 -- Get the name id of the symbol
429 Symbol_Data.On_The_Command_Line := True;
430 Name_Buffer (1 .. Name_Len - 1) :=
431 Name_Buffer (2 .. Name_Len);
432 Name_Len := Name_Len - 1;
433 Symbol_Data.Symbol := Name_Find;
435 if Name_Buffer (1 .. Name_Len) = "if"
436 or else Name_Buffer (1 .. Name_Len) = "else"
437 or else Name_Buffer (1 .. Name_Len) = "elsif"
438 or else Name_Buffer (1 .. Name_Len) = "end"
439 or else Name_Buffer (1 .. Name_Len) = "not"
440 or else Name_Buffer (1 .. Name_Len) = "and"
441 or else Name_Buffer (1 .. Name_Len) = "then"
443 Error_Msg ("symbol expected", Token_Ptr + 1);
448 -- Get the name id of the original symbol, with
449 -- possibly capital letters.
451 Name_Len := Integer (Scan_Ptr - Token_Ptr - 1);
453 for J in 1 .. Name_Len loop
455 Sinput.Source (Token_Ptr + Text_Ptr (J));
458 Symbol_Data.Original := Name_Find;
460 -- Scan past D<symbol>
464 if Token /= Tok_Equal then
465 Error_Msg ("`=` expected", Token_Ptr);
474 -- Here any reserved word is OK
476 Change_Reserved_Keyword_To_Symbol
477 (All_Keywords => True);
479 -- Value can be an identifier (or a reserved word)
480 -- or a literal string.
483 when Tok_String_Literal =>
484 Symbol_Data.Is_A_String := True;
485 Symbol_Data.Value := String_Literal_Id;
487 when Tok_Identifier =>
488 Symbol_Data.Is_A_String := False;
491 for J in Token_Ptr .. Scan_Ptr - 1 loop
492 Store_String_Char (Sinput.Source (J));
495 Symbol_Data.Value := End_String;
499 ("literal string or identifier expected",
505 -- If symbol already exists, replace old definition
508 Symbol_Id := Prep.Index_Of (Symbol_Data.Symbol);
510 -- Otherwise, add a new entry in the table
512 if Symbol_Id = No_Symbol then
513 Symbol_Table.Increment_Last (Prep.Mapping);
514 Symbol_Id := Symbol_Table.Last (Mapping);
517 Prep.Mapping.Table (Symbol_Id) := Symbol_Data;
528 Error_Msg ("invalid switch", Dash_Location);
534 -- Add the command line symbols, if any, possibly replacing symbols
537 Add_Command_Line_Symbols;
539 -- Put the resulting Prep.Mapping in Current_Data, and immediately
540 -- set Prep.Mapping to nil.
542 Current_Data.Mapping := Prep.Mapping;
543 Prep.Mapping := No_Mapping;
545 -- Record Current_Data
547 if Current_Data.File_Name = No_Name then
548 Default_Data := Current_Data;
551 Preproc_Data_Table.Increment_Last;
552 Preproc_Data_Table.Table (Preproc_Data_Table.Last) := Current_Data;
555 Current_Data := No_Preproc_Data;
556 end loop For_Each_Line;
558 Scn.Scanner.Set_End_Of_Line_As_Token (False);
560 -- Fail if there were errors in the preprocessing data file
562 if Total_Errors_Detected > T then
564 Fail ("errors found in preprocessing data file """,
569 -- Record the dependency on the preprocessor data file
571 Dependencies.Increment_Last;
572 Dependencies.Table (Dependencies.Last) :=
573 Source_Index_Of_Preproc_Data_File;
574 end Parse_Preprocessing_Data_File;
576 ---------------------------
577 -- Prepare_To_Preprocess --
578 ---------------------------
580 procedure Prepare_To_Preprocess
581 (Source : File_Name_Type;
582 Preprocessing_Needed : out Boolean)
584 Default : Boolean := False;
588 -- By default, preprocessing is not needed
590 Preprocessing_Needed := False;
592 if No_Preprocessing then
596 -- First, look for preprocessing data specific to the current source
598 for J in 1 .. Preproc_Data_Table.Last loop
599 if Preproc_Data_Table.Table (J).File_Name = Source then
601 Current_Data := Preproc_Data_Table.Table (J);
606 -- If no specific preprocessing data, then take the default
609 if Default_Data_Defined then
610 Current_Data := Default_Data;
614 -- If no default, then nothing to do
620 -- Set the preprocessing flags according to the preprocessing data
622 if Current_Data.Comments and then not Current_Data.Always_Blank then
623 Comment_Deleted_Lines := True;
624 Blank_Deleted_Lines := False;
627 Comment_Deleted_Lines := False;
628 Blank_Deleted_Lines := True;
631 Undefined_Symbols_Are_False := Current_Data.Undef_False;
632 List_Preprocessing_Symbols := Current_Data.List_Symbols;
634 -- If not already done it, process the definition file
636 if Current_Data.Processed then
640 Prep.Mapping := Current_Data.Mapping;
643 -- First put the mapping in Prep.Mapping, because Prep.Parse_Def_File
644 -- works on Prep.Mapping.
646 Prep.Mapping := Current_Data.Mapping;
648 String_To_Name_Buffer (Current_Data.Deffile);
651 N : constant Name_Id := Name_Find;
652 Deffile : constant Source_File_Index := Load_Definition_File (N);
653 Add_Deffile : Boolean := True;
654 T : constant Nat := Total_Errors_Detected;
657 if Deffile = No_Source_File then
658 Fail ("definition file """,
660 """ cannot be found");
663 -- Initialize the preprocessor and set the characteristics of the
664 -- scanner for a definition file.
667 (Error_Msg => Errout.Error_Msg'Access,
668 Scan => Scn.Scanner.Scan'Access,
669 Set_Ignore_Errors => Errout.Set_Ignore_Errors'Access,
673 Scn.Scanner.Set_End_Of_Line_As_Token (True);
674 Scn.Scanner.Reset_Special_Characters;
676 -- Initialize the scanner and process the definition file
678 Scn.Scanner.Initialize_Scanner (Deffile);
681 -- Reset the behaviour of the scanner to the default
683 Scn.Scanner.Set_End_Of_Line_As_Token (False);
685 -- Fail if errors were found while processing the definition file
687 if T /= Total_Errors_Detected then
689 Fail ("errors found in definition file """,
694 for Index in 1 .. Dependencies.Last loop
695 if Dependencies.Table (Index) = Deffile then
696 Add_Deffile := False;
702 Dependencies.Increment_Last;
703 Dependencies.Table (Dependencies.Last) := Deffile;
707 -- Get back the mapping, indicate that the definition file is
708 -- processed and store back the preprocessing data.
710 Current_Data.Mapping := Prep.Mapping;
711 Current_Data.Processed := True;
714 Default_Data := Current_Data;
717 Preproc_Data_Table.Table (Index) := Current_Data;
721 Preprocessing_Needed := True;
722 end Prepare_To_Preprocess;
724 ---------------------------------------------
725 -- Process_Command_Line_Symbol_Definitions --
726 ---------------------------------------------
728 procedure Process_Command_Line_Symbol_Definitions is
729 Symbol_Data : Prep.Symbol_Data;
730 Found : Boolean := False;
733 Symbol_Table.Init (Command_Line_Symbols);
735 -- The command line definitions have been stored temporarily in
736 -- array Symbol_Definitions.
738 for Index in 1 .. Last_Definition loop
739 -- Check each symbol definition, fail immediately if syntax is not
742 Check_Command_Line_Symbol_Definition
743 (Definition => Symbol_Definitions (Index).all,
744 Data => Symbol_Data);
747 -- If there is already a definition for this symbol, replace the old
748 -- definition by this one.
750 for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
751 if Command_Line_Symbols.Table (J).Symbol = Symbol_Data.Symbol then
752 Command_Line_Symbols.Table (J) := Symbol_Data;
758 -- Otherwise, create a new entry in the table
761 Symbol_Table.Increment_Last (Command_Line_Symbols);
762 Command_Line_Symbols.Table
763 (Symbol_Table.Last (Command_Line_Symbols)) := Symbol_Data;
766 end Process_Command_Line_Symbol_Definitions;
768 -------------------------
769 -- Skip_To_End_Of_Line --
770 -------------------------
772 procedure Skip_To_End_Of_Line is
774 Set_Ignore_Errors (To => True);
776 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
780 Set_Ignore_Errors (To => False);
781 end Skip_To_End_Of_Line;