1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2009, 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 with Ada
.Unchecked_Deallocation
;
28 with Errout
; use Errout
;
29 with Lib
.Writ
; use Lib
.Writ
;
31 with Osint
; use Osint
;
33 with Scans
; use Scans
;
35 with Sinput
.L
; use Sinput
.L
;
36 with Stringt
; use Stringt
;
38 with Types
; use Types
;
40 package body Prepcomp
is
42 No_Preprocessing
: Boolean := True;
43 -- Set to False if there is at least one source that needs to be
46 Source_Index_Of_Preproc_Data_File
: Source_File_Index
:= No_Source_File
;
48 -- The following variable should be a constant, but this is not possible
49 -- because its type GNAT.Dynamic_Tables.Instance has a component P of
50 -- unitialized private type GNAT.Dynamic_Tables.Table_Private and there
51 -- are no exported values for this private type. Warnings are Off because
52 -- it is never assigned a value.
54 pragma Warnings
(Off
);
55 No_Mapping
: Prep
.Symbol_Table
.Instance
;
58 type String_Ptr
is access String;
59 type String_Array
is array (Positive range <>) of String_Ptr
;
60 type String_Array_Ptr
is access String_Array
;
63 new Ada
.Unchecked_Deallocation
(String_Array
, String_Array_Ptr
);
65 Symbol_Definitions
: String_Array_Ptr
:= new String_Array
(1 .. 4);
66 -- An extensible array to temporarily stores symbol definitions specified
67 -- on the command line with -gnateD switches.
69 Last_Definition
: Natural := 0;
70 -- Index of last symbol definition in array Symbol_Definitions
72 type Preproc_Data
is record
73 Mapping
: Symbol_Table
.Instance
;
74 File_Name
: File_Name_Type
:= No_File
;
75 Deffile
: String_Id
:= No_String
;
76 Undef_False
: Boolean := False;
77 Always_Blank
: Boolean := False;
78 Comments
: Boolean := False;
79 List_Symbols
: Boolean := False;
80 Processed
: Boolean := False;
82 -- Structure to keep the preprocessing data for a file name or for the
83 -- default (when Name_Id = No_Name).
85 No_Preproc_Data
: constant Preproc_Data
:=
86 (Mapping
=> No_Mapping
,
90 Always_Blank
=> False,
92 List_Symbols
=> False,
95 Default_Data
: Preproc_Data
:= No_Preproc_Data
;
96 -- The preprocessing data to be used when no specific preprocessing data
97 -- is specified for a source.
99 Default_Data_Defined
: Boolean := False;
100 -- True if source for which no specific preprocessing is specified need to
101 -- be preprocess with the Default_Data.
103 Current_Data
: Preproc_Data
:= No_Preproc_Data
;
105 package Preproc_Data_Table
is new Table
.Table
106 (Table_Component_Type
=> Preproc_Data
,
107 Table_Index_Type
=> Int
,
108 Table_Low_Bound
=> 1,
110 Table_Increment
=> 100,
111 Table_Name
=> "Prepcomp.Preproc_Data_Table");
112 -- Table to store the specific preprocessing data
114 Command_Line_Symbols
: Symbol_Table
.Instance
;
115 -- A table to store symbol definitions specified on the command line with
118 package Dependencies
is new Table
.Table
119 (Table_Component_Type
=> Source_File_Index
,
120 Table_Index_Type
=> Int
,
121 Table_Low_Bound
=> 1,
123 Table_Increment
=> 100,
124 Table_Name
=> "Prepcomp.Dependencies");
125 -- Table to store the dependencies on preprocessing files
127 procedure Add_Command_Line_Symbols
;
128 -- Add the command line symbol definitions, if any, to Prep.Mapping table
130 procedure Skip_To_End_Of_Line
;
131 -- Ignore errors and scan up to the next end of line or the end of file
133 ------------------------------
134 -- Add_Command_Line_Symbols --
135 ------------------------------
137 procedure Add_Command_Line_Symbols
is
138 Symbol_Id
: Prep
.Symbol_Id
;
141 for J
in 1 .. Symbol_Table
.Last
(Command_Line_Symbols
) loop
142 Symbol_Id
:= Prep
.Index_Of
(Command_Line_Symbols
.Table
(J
).Symbol
);
144 if Symbol_Id
= No_Symbol
then
145 Symbol_Table
.Increment_Last
(Prep
.Mapping
);
146 Symbol_Id
:= Symbol_Table
.Last
(Prep
.Mapping
);
149 Prep
.Mapping
.Table
(Symbol_Id
) := Command_Line_Symbols
.Table
(J
);
151 end Add_Command_Line_Symbols
;
153 ----------------------
154 -- Add_Dependencies --
155 ----------------------
157 procedure Add_Dependencies
is
159 for Index
in 1 .. Dependencies
.Last
loop
160 Add_Preprocessing_Dependency
(Dependencies
.Table
(Index
));
162 end Add_Dependencies
;
164 ---------------------------
165 -- Add_Symbol_Definition --
166 ---------------------------
168 procedure Add_Symbol_Definition
(Def
: String) is
170 -- If Symbol_Definitions is not large enough, double it
172 if Last_Definition
= Symbol_Definitions
'Last then
174 New_Symbol_Definitions
: constant String_Array_Ptr
:=
175 new String_Array
(1 .. 2 * Last_Definition
);
178 New_Symbol_Definitions
(Symbol_Definitions
'Range) :=
179 Symbol_Definitions
.all;
180 Free
(Symbol_Definitions
);
181 Symbol_Definitions
:= New_Symbol_Definitions
;
185 Last_Definition
:= Last_Definition
+ 1;
186 Symbol_Definitions
(Last_Definition
) := new String'(Def);
187 end Add_Symbol_Definition;
193 procedure Check_Symbols is
195 -- If there is at least one switch -gnateD specified
197 if Symbol_Table.Last (Command_Line_Symbols) >= 1 then
198 Current_Data := No_Preproc_Data;
199 No_Preprocessing := False;
200 Current_Data.Processed := True;
202 -- Start with an empty, initialized mapping table; use Prep.Mapping,
203 -- because Prep.Index_Of uses Prep.Mapping.
205 Prep.Mapping := No_Mapping;
206 Symbol_Table.Init (Prep.Mapping);
208 -- Add the command line symbols
210 Add_Command_Line_Symbols;
212 -- Put the resulting Prep.Mapping in Current_Data, and immediately
213 -- set Prep.Mapping to nil.
215 Current_Data.Mapping := Prep.Mapping;
216 Prep.Mapping := No_Mapping;
218 -- Set the default data
220 Default_Data := Current_Data;
221 Default_Data_Defined := True;
225 ------------------------------
226 -- Parse_Preprocessing_Data --
227 ------------------------------
229 procedure Parse_Preprocessing_Data_File (N : File_Name_Type) is
230 OK : Boolean := False;
231 Dash_Location : Source_Ptr;
232 Symbol_Data : Prep.Symbol_Data;
233 Symbol_Id : Prep.Symbol_Id;
234 T : constant Nat := Total_Errors_Detected;
237 -- Load the preprocessing data file
239 Source_Index_Of_Preproc_Data_File := Load_Preprocessing_Data_File (N);
241 -- Fail if preprocessing data file cannot be found
243 if Source_Index_Of_Preproc_Data_File = No_Source_File then
245 Fail ("preprocessing data file """
246 & Name_Buffer (1 .. Name_Len)
250 -- Initialize scanner and set its behavior for processing a data file
252 Scn.Scanner.Initialize_Scanner (Source_Index_Of_Preproc_Data_File);
253 Scn.Scanner.Set_End_Of_Line_As_Token (True);
254 Scn.Scanner.Reset_Special_Characters;
260 exit For_Each_Line when Token = Tok_EOF;
262 if Token = Tok_End_Of_Line then
269 No_Preprocessing := False;
270 Current_Data := No_Preproc_Data;
277 if Default_Data_Defined then
279 ("multiple default preprocessing data", Token_Ptr);
283 Default_Data_Defined := True;
286 when Tok_String_Literal =>
290 String_To_Name_Buffer (String_Literal_Id);
291 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
292 Current_Data.File_Name := Name_Find;
295 for Index in 1 .. Preproc_Data_Table.Last loop
296 if Current_Data.File_Name =
297 Preproc_Data_Table.Table (Index).File_Name
299 Error_Msg_File_1 := Current_Data.File_Name;
301 ("multiple preprocessing data for{", Token_Ptr);
308 Error_Msg ("`'*`
or literal
string expected
", Token_Ptr);
311 -- If there is a problem, skip the line
318 -- Scan past the * or the literal string
322 -- A literal string in second position is a definition file
324 if Token = Tok_String_Literal then
325 Current_Data.Deffile := String_Literal_Id;
326 Current_Data.Processed := False;
330 -- If there is no definition file, set Processed to True now
332 Current_Data.Processed := True;
335 -- Start with an empty, initialized mapping table; use Prep.Mapping,
336 -- because Prep.Index_Of uses Prep.Mapping.
338 Prep.Mapping := No_Mapping;
339 Symbol_Table.Init (Prep.Mapping);
341 -- Check the switches that may follow
343 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
344 if Token /= Tok_Minus then
345 Error_Msg ("`
'-` expected", Token_Ptr);
350 -- Keep the location of the '-' for possible error reporting
352 Dash_Location := Token_Ptr;
358 Change_Reserved_Keyword_To_Symbol;
360 -- An identifier (or a reserved word converted to an
361 -- identifier) is expected and there must be no blank space
362 -- between the '-' and the identifier.
364 if Token = Tok_Identifier
365 and then Token_Ptr = Dash_Location + 1
367 Get_Name_String (Token_Name);
369 -- Check the character in the source, because the case is
372 case Sinput.Source (Token_Ptr) is
375 -- Undefined symbol are False
378 Current_Data.Undef_False := True;
387 Current_Data.Always_Blank := True;
393 -- Comment removed lines
396 Current_Data.Comments := True;
405 Current_Data.List_Symbols := True;
417 -- A symbol must be an Ada identifier; it cannot start
418 -- with an underline or a digit.
420 if Name_Buffer (2) = '_
'
421 or else Name_Buffer (2) in '0' .. '9'
423 Error_Msg ("symbol expected", Token_Ptr + 1);
428 -- Get the name id of the symbol
430 Symbol_Data.On_The_Command_Line := True;
431 Name_Buffer (1 .. Name_Len - 1) :=
432 Name_Buffer (2 .. Name_Len);
433 Name_Len := Name_Len - 1;
434 Symbol_Data.Symbol := Name_Find;
436 if Name_Buffer (1 .. Name_Len) = "if"
437 or else Name_Buffer (1 .. Name_Len) = "else"
438 or else Name_Buffer (1 .. Name_Len) = "elsif"
439 or else Name_Buffer (1 .. Name_Len) = "end"
440 or else Name_Buffer (1 .. Name_Len) = "not"
441 or else Name_Buffer (1 .. Name_Len) = "and"
442 or else Name_Buffer (1 .. Name_Len) = "then"
444 Error_Msg ("symbol expected", Token_Ptr + 1);
449 -- Get the name id of the original symbol, with
450 -- possibly capital letters.
452 Name_Len := Integer (Scan_Ptr - Token_Ptr - 1);
454 for J in 1 .. Name_Len loop
456 Sinput.Source (Token_Ptr + Text_Ptr (J));
459 Symbol_Data.Original := Name_Find;
461 -- Scan past D<symbol>
465 if Token /= Tok_Equal then
466 Error_Msg ("`=` expected", Token_Ptr);
475 -- Here any reserved word is OK
477 Change_Reserved_Keyword_To_Symbol
478 (All_Keywords => True);
480 -- Value can be an identifier (or a reserved word)
481 -- or a literal string.
484 when Tok_String_Literal =>
485 Symbol_Data.Is_A_String := True;
486 Symbol_Data.Value := String_Literal_Id;
488 when Tok_Identifier =>
489 Symbol_Data.Is_A_String := False;
492 for J in Token_Ptr .. Scan_Ptr - 1 loop
493 Store_String_Char (Sinput.Source (J));
496 Symbol_Data.Value := End_String;
500 ("literal string or identifier expected",
506 -- If symbol already exists, replace old definition
509 Symbol_Id := Prep.Index_Of (Symbol_Data.Symbol);
511 -- Otherwise, add a new entry in the table
513 if Symbol_Id = No_Symbol then
514 Symbol_Table.Increment_Last (Prep.Mapping);
515 Symbol_Id := Symbol_Table.Last (Mapping);
518 Prep.Mapping.Table (Symbol_Id) := Symbol_Data;
529 Error_Msg ("invalid switch", Dash_Location);
535 -- Add the command line symbols, if any, possibly replacing symbols
538 Add_Command_Line_Symbols;
540 -- Put the resulting Prep.Mapping in Current_Data, and immediately
541 -- set Prep.Mapping to nil.
543 Current_Data.Mapping := Prep.Mapping;
544 Prep.Mapping := No_Mapping;
546 -- Record Current_Data
548 if Current_Data.File_Name = No_File then
549 Default_Data := Current_Data;
552 Preproc_Data_Table.Increment_Last;
553 Preproc_Data_Table.Table (Preproc_Data_Table.Last) := Current_Data;
556 Current_Data := No_Preproc_Data;
557 end loop For_Each_Line;
559 Scn.Scanner.Set_End_Of_Line_As_Token (False);
561 -- Fail if there were errors in the preprocessing data file
563 if Total_Errors_Detected > T then
564 Errout.Finalize (Last_Call => True);
565 Errout.Output_Messages;
566 Fail ("errors found in preprocessing data file """
567 & Get_Name_String (N) & """");
570 -- Record the dependency on the preprocessor data file
572 Dependencies.Increment_Last;
573 Dependencies.Table (Dependencies.Last) :=
574 Source_Index_Of_Preproc_Data_File;
575 end Parse_Preprocessing_Data_File;
577 ---------------------------
578 -- Prepare_To_Preprocess --
579 ---------------------------
581 procedure Prepare_To_Preprocess
582 (Source : File_Name_Type;
583 Preprocessing_Needed : out Boolean)
585 Default : Boolean := False;
589 -- By default, preprocessing is not needed
591 Preprocessing_Needed := False;
593 if No_Preprocessing then
597 -- First, look for preprocessing data specific to the current source
599 for J in 1 .. Preproc_Data_Table.Last loop
600 if Preproc_Data_Table.Table (J).File_Name = Source then
602 Current_Data := Preproc_Data_Table.Table (J);
607 -- If no specific preprocessing data, then take the default
610 if Default_Data_Defined then
611 Current_Data := Default_Data;
615 -- If no default, then nothing to do
621 -- Set the preprocessing flags according to the preprocessing data
623 if Current_Data.Comments and then not Current_Data.Always_Blank then
624 Comment_Deleted_Lines := True;
625 Blank_Deleted_Lines := False;
628 Comment_Deleted_Lines := False;
629 Blank_Deleted_Lines := True;
632 Undefined_Symbols_Are_False := Current_Data.Undef_False;
633 List_Preprocessing_Symbols := Current_Data.List_Symbols;
635 -- If not already done it, process the definition file
637 if Current_Data.Processed then
641 Prep.Mapping := Current_Data.Mapping;
644 -- First put the mapping in Prep.Mapping, because Prep.Parse_Def_File
645 -- works on Prep.Mapping.
647 Prep.Mapping := Current_Data.Mapping;
649 String_To_Name_Buffer (Current_Data.Deffile);
652 N : constant File_Name_Type := Name_Find;
653 Deffile : constant Source_File_Index :=
654 Load_Definition_File (N);
655 Add_Deffile : Boolean := True;
656 T : constant Nat := Total_Errors_Detected;
659 if Deffile = No_Source_File then
660 Fail ("definition file """
661 & Get_Name_String (N)
665 -- Initialize the preprocessor and set the characteristics of the
666 -- scanner for a definition file.
669 (Error_Msg => Errout.Error_Msg'Access,
670 Scan => Scn.Scanner.Scan'Access,
671 Set_Ignore_Errors => Errout.Set_Ignore_Errors'Access,
675 Scn.Scanner.Set_End_Of_Line_As_Token (True);
676 Scn.Scanner.Reset_Special_Characters;
678 -- Initialize the scanner and process the definition file
680 Scn.Scanner.Initialize_Scanner (Deffile);
683 -- Reset the behaviour of the scanner to the default
685 Scn.Scanner.Set_End_Of_Line_As_Token (False);
687 -- Fail if errors were found while processing the definition file
689 if T /= Total_Errors_Detected then
690 Errout.Finalize (Last_Call => True);
691 Errout.Output_Messages;
692 Fail ("errors found in definition file """
693 & Get_Name_String (N)
697 for Index in 1 .. Dependencies.Last loop
698 if Dependencies.Table (Index) = Deffile then
699 Add_Deffile := False;
705 Dependencies.Increment_Last;
706 Dependencies.Table (Dependencies.Last) := Deffile;
710 -- Get back the mapping, indicate that the definition file is
711 -- processed and store back the preprocessing data.
713 Current_Data.Mapping := Prep.Mapping;
714 Current_Data.Processed := True;
717 Default_Data := Current_Data;
720 Preproc_Data_Table.Table (Index) := Current_Data;
724 Preprocessing_Needed := True;
725 end Prepare_To_Preprocess;
727 ---------------------------------------------
728 -- Process_Command_Line_Symbol_Definitions --
729 ---------------------------------------------
731 procedure Process_Command_Line_Symbol_Definitions is
732 Symbol_Data : Prep.Symbol_Data;
733 Found : Boolean := False;
736 Symbol_Table.Init (Command_Line_Symbols);
738 -- The command line definitions have been stored temporarily in
739 -- array Symbol_Definitions.
741 for Index in 1 .. Last_Definition loop
742 -- Check each symbol definition, fail immediately if syntax is not
745 Check_Command_Line_Symbol_Definition
746 (Definition => Symbol_Definitions (Index).all,
747 Data => Symbol_Data);
750 -- If there is already a definition for this symbol, replace the old
751 -- definition by this one.
753 for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
754 if Command_Line_Symbols.Table (J).Symbol = Symbol_Data.Symbol then
755 Command_Line_Symbols.Table (J) := Symbol_Data;
761 -- Otherwise, create a new entry in the table
764 Symbol_Table.Increment_Last (Command_Line_Symbols);
765 Command_Line_Symbols.Table
766 (Symbol_Table.Last (Command_Line_Symbols)) := Symbol_Data;
769 end Process_Command_Line_Symbol_Definitions;
771 -------------------------
772 -- Skip_To_End_Of_Line --
773 -------------------------
775 procedure Skip_To_End_Of_Line is
777 Set_Ignore_Errors (To => True);
779 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
783 Set_Ignore_Errors (To => False);
784 end Skip_To_End_Of_Line;