1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2010, 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 Errout; use Errout;
27 with Lib.Writ; use Lib.Writ;
29 with Osint; use Osint;
31 with Scans; use Scans;
33 with Sinput.L; use Sinput.L;
34 with Stringt; use Stringt;
36 with Types; use Types;
38 package body Prepcomp is
40 No_Preprocessing : Boolean := True;
41 -- Set to False if there is at least one source that needs to be
44 Source_Index_Of_Preproc_Data_File : Source_File_Index := No_Source_File;
46 -- The following variable should be a constant, but this is not possible
47 -- because its type GNAT.Dynamic_Tables.Instance has a component P of
48 -- uninitialized private type GNAT.Dynamic_Tables.Table_Private and there
49 -- are no exported values for this private type. Warnings are Off because
50 -- it is never assigned a value.
52 pragma Warnings (Off);
53 No_Mapping : Prep.Symbol_Table.Instance;
56 type Preproc_Data is record
57 Mapping : Symbol_Table.Instance;
58 File_Name : File_Name_Type := No_File;
59 Deffile : String_Id := No_String;
60 Undef_False : Boolean := False;
61 Always_Blank : Boolean := False;
62 Comments : Boolean := False;
63 List_Symbols : Boolean := False;
64 Processed : Boolean := False;
66 -- Structure to keep the preprocessing data for a file name or for the
67 -- default (when Name_Id = No_Name).
69 No_Preproc_Data : constant Preproc_Data :=
70 (Mapping => No_Mapping,
74 Always_Blank => False,
76 List_Symbols => False,
79 Default_Data : Preproc_Data := No_Preproc_Data;
80 -- The preprocessing data to be used when no specific preprocessing data
81 -- is specified for a source.
83 Default_Data_Defined : Boolean := False;
84 -- True if source for which no specific preprocessing is specified need to
85 -- be preprocess with the Default_Data.
87 Current_Data : Preproc_Data := No_Preproc_Data;
89 package Preproc_Data_Table is new Table.Table
90 (Table_Component_Type => Preproc_Data,
91 Table_Index_Type => Int,
94 Table_Increment => 100,
95 Table_Name => "Prepcomp.Preproc_Data_Table");
96 -- Table to store the specific preprocessing data
98 Command_Line_Symbols : Symbol_Table.Instance;
99 -- A table to store symbol definitions specified on the command line with
102 package Dependencies is new Table.Table
103 (Table_Component_Type => Source_File_Index,
104 Table_Index_Type => Int,
105 Table_Low_Bound => 1,
107 Table_Increment => 100,
108 Table_Name => "Prepcomp.Dependencies");
109 -- Table to store the dependencies on preprocessing files
111 procedure Add_Command_Line_Symbols;
112 -- Add the command line symbol definitions, if any, to Prep.Mapping table
114 procedure Skip_To_End_Of_Line;
115 -- Ignore errors and scan up to the next end of line or the end of file
117 ------------------------------
118 -- Add_Command_Line_Symbols --
119 ------------------------------
121 procedure Add_Command_Line_Symbols is
122 Symbol_Id : Prep.Symbol_Id;
125 for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
126 Symbol_Id := Prep.Index_Of (Command_Line_Symbols.Table (J).Symbol);
128 if Symbol_Id = No_Symbol then
129 Symbol_Table.Increment_Last (Prep.Mapping);
130 Symbol_Id := Symbol_Table.Last (Prep.Mapping);
133 Prep.Mapping.Table (Symbol_Id) := Command_Line_Symbols.Table (J);
135 end Add_Command_Line_Symbols;
137 ----------------------
138 -- Add_Dependencies --
139 ----------------------
141 procedure Add_Dependencies is
143 for Index in 1 .. Dependencies.Last loop
144 Add_Preprocessing_Dependency (Dependencies.Table (Index));
146 end Add_Dependencies;
152 procedure Check_Symbols is
154 -- If there is at least one switch -gnateD specified
156 if Symbol_Table.Last (Command_Line_Symbols) >= 1 then
157 Current_Data := No_Preproc_Data;
158 No_Preprocessing := False;
159 Current_Data.Processed := True;
161 -- Start with an empty, initialized mapping table; use Prep.Mapping,
162 -- because Prep.Index_Of uses Prep.Mapping.
164 Prep.Mapping := No_Mapping;
165 Symbol_Table.Init (Prep.Mapping);
167 -- Add the command line symbols
169 Add_Command_Line_Symbols;
171 -- Put the resulting Prep.Mapping in Current_Data, and immediately
172 -- set Prep.Mapping to nil.
174 Current_Data.Mapping := Prep.Mapping;
175 Prep.Mapping := No_Mapping;
177 -- Set the default data
179 Default_Data := Current_Data;
180 Default_Data_Defined := True;
184 ------------------------------
185 -- Parse_Preprocessing_Data --
186 ------------------------------
188 procedure Parse_Preprocessing_Data_File (N : File_Name_Type) is
189 OK : Boolean := False;
190 Dash_Location : Source_Ptr;
191 Symbol_Data : Prep.Symbol_Data;
192 Symbol_Id : Prep.Symbol_Id;
193 T : constant Nat := Total_Errors_Detected;
196 -- Load the preprocessing data file
198 Source_Index_Of_Preproc_Data_File := Load_Preprocessing_Data_File (N);
200 -- Fail if preprocessing data file cannot be found
202 if Source_Index_Of_Preproc_Data_File = No_Source_File then
204 Fail ("preprocessing data file """
205 & Name_Buffer (1 .. Name_Len)
209 -- Initialize scanner and set its behavior for processing a data file
211 Scn.Scanner.Initialize_Scanner (Source_Index_Of_Preproc_Data_File);
212 Scn.Scanner.Set_End_Of_Line_As_Token (True);
213 Scn.Scanner.Reset_Special_Characters;
219 exit For_Each_Line when Token = Tok_EOF;
221 if Token = Tok_End_Of_Line then
228 No_Preprocessing := False;
229 Current_Data := No_Preproc_Data;
236 if Default_Data_Defined then
238 ("multiple default preprocessing data", Token_Ptr);
242 Default_Data_Defined := True;
245 when Tok_String_Literal =>
249 String_To_Name_Buffer (String_Literal_Id);
250 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
251 Current_Data.File_Name := Name_Find;
254 for Index in 1 .. Preproc_Data_Table.Last loop
255 if Current_Data.File_Name =
256 Preproc_Data_Table.Table (Index).File_Name
258 Error_Msg_File_1 := Current_Data.File_Name;
260 ("multiple preprocessing data for{", Token_Ptr);
267 Error_Msg ("`'*` or literal string expected", Token_Ptr);
270 -- If there is a problem, skip the line
277 -- Scan past the * or the literal string
281 -- A literal string in second position is a definition file
283 if Token = Tok_String_Literal then
284 Current_Data.Deffile := String_Literal_Id;
285 Current_Data.Processed := False;
289 -- If there is no definition file, set Processed to True now
291 Current_Data.Processed := True;
294 -- Start with an empty, initialized mapping table; use Prep.Mapping,
295 -- because Prep.Index_Of uses Prep.Mapping.
297 Prep.Mapping := No_Mapping;
298 Symbol_Table.Init (Prep.Mapping);
300 -- Check the switches that may follow
302 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
303 if Token /= Tok_Minus then
305 ("`'-` expected", Token_Ptr);
310 -- Keep the location of the '-' for possible error reporting
312 Dash_Location := Token_Ptr;
318 Change_Reserved_Keyword_To_Symbol;
320 -- An identifier (or a reserved word converted to an
321 -- identifier) is expected and there must be no blank space
322 -- between the '-' and the identifier.
324 if Token = Tok_Identifier
325 and then Token_Ptr = Dash_Location + 1
327 Get_Name_String (Token_Name);
329 -- Check the character in the source, because the case is
332 case Sinput.Source (Token_Ptr) is
335 -- Undefined symbol are False
338 Current_Data.Undef_False := True;
347 Current_Data.Always_Blank := True;
353 -- Comment removed lines
356 Current_Data.Comments := True;
365 Current_Data.List_Symbols := True;
377 -- A symbol must be an Ada identifier; it cannot start
378 -- with an underline or a digit.
380 if Name_Buffer (2) = '_'
381 or else Name_Buffer (2) in '0' .. '9'
383 Error_Msg ("symbol expected", Token_Ptr + 1);
388 -- Get the name id of the symbol
390 Symbol_Data.On_The_Command_Line := True;
391 Name_Buffer (1 .. Name_Len - 1) :=
392 Name_Buffer (2 .. Name_Len);
393 Name_Len := Name_Len - 1;
394 Symbol_Data.Symbol := Name_Find;
396 if Name_Buffer (1 .. Name_Len) = "if"
397 or else Name_Buffer (1 .. Name_Len) = "else"
398 or else Name_Buffer (1 .. Name_Len) = "elsif"
399 or else Name_Buffer (1 .. Name_Len) = "end"
400 or else Name_Buffer (1 .. Name_Len) = "not"
401 or else Name_Buffer (1 .. Name_Len) = "and"
402 or else Name_Buffer (1 .. Name_Len) = "then"
404 Error_Msg ("symbol expected", Token_Ptr + 1);
409 -- Get the name id of the original symbol, with
410 -- possibly capital letters.
412 Name_Len := Integer (Scan_Ptr - Token_Ptr - 1);
414 for J in 1 .. Name_Len loop
416 Sinput.Source (Token_Ptr + Text_Ptr (J));
419 Symbol_Data.Original := Name_Find;
421 -- Scan past D<symbol>
425 if Token /= Tok_Equal then
427 ("`=` expected", Token_Ptr);
436 -- Here any reserved word is OK
438 Change_Reserved_Keyword_To_Symbol
439 (All_Keywords => True);
441 -- Value can be an identifier (or a reserved word)
442 -- or a literal string.
445 when Tok_String_Literal =>
446 Symbol_Data.Is_A_String := True;
447 Symbol_Data.Value := String_Literal_Id;
449 when Tok_Identifier =>
450 Symbol_Data.Is_A_String := False;
453 for J in Token_Ptr .. Scan_Ptr - 1 loop
454 Store_String_Char (Sinput.Source (J));
457 Symbol_Data.Value := End_String;
461 ("literal string or identifier expected",
467 -- If symbol already exists, replace old definition
470 Symbol_Id := Prep.Index_Of (Symbol_Data.Symbol);
472 -- Otherwise, add a new entry in the table
474 if Symbol_Id = No_Symbol then
475 Symbol_Table.Increment_Last (Prep.Mapping);
476 Symbol_Id := Symbol_Table.Last (Mapping);
479 Prep.Mapping.Table (Symbol_Id) := Symbol_Data;
490 Error_Msg ("invalid switch", Dash_Location);
496 -- Add the command line symbols, if any, possibly replacing symbols
499 Add_Command_Line_Symbols;
501 -- Put the resulting Prep.Mapping in Current_Data, and immediately
502 -- set Prep.Mapping to nil.
504 Current_Data.Mapping := Prep.Mapping;
505 Prep.Mapping := No_Mapping;
507 -- Record Current_Data
509 if Current_Data.File_Name = No_File then
510 Default_Data := Current_Data;
513 Preproc_Data_Table.Increment_Last;
514 Preproc_Data_Table.Table (Preproc_Data_Table.Last) := Current_Data;
517 Current_Data := No_Preproc_Data;
518 end loop For_Each_Line;
520 Scn.Scanner.Set_End_Of_Line_As_Token (False);
522 -- Fail if there were errors in the preprocessing data file
524 if Total_Errors_Detected > T then
525 Errout.Finalize (Last_Call => True);
526 Errout.Output_Messages;
527 Fail ("errors found in preprocessing data file """
528 & Get_Name_String (N) & """");
531 -- Record the dependency on the preprocessor data file
533 Dependencies.Increment_Last;
534 Dependencies.Table (Dependencies.Last) :=
535 Source_Index_Of_Preproc_Data_File;
536 end Parse_Preprocessing_Data_File;
538 ---------------------------
539 -- Prepare_To_Preprocess --
540 ---------------------------
542 procedure Prepare_To_Preprocess
543 (Source : File_Name_Type;
544 Preprocessing_Needed : out Boolean)
546 Default : Boolean := False;
550 -- By default, preprocessing is not needed
552 Preprocessing_Needed := False;
554 if No_Preprocessing then
558 -- First, look for preprocessing data specific to the current source
560 for J in 1 .. Preproc_Data_Table.Last loop
561 if Preproc_Data_Table.Table (J).File_Name = Source then
563 Current_Data := Preproc_Data_Table.Table (J);
568 -- If no specific preprocessing data, then take the default
571 if Default_Data_Defined then
572 Current_Data := Default_Data;
576 -- If no default, then nothing to do
582 -- Set the preprocessing flags according to the preprocessing data
584 if Current_Data.Comments and then not Current_Data.Always_Blank then
585 Comment_Deleted_Lines := True;
586 Blank_Deleted_Lines := False;
589 Comment_Deleted_Lines := False;
590 Blank_Deleted_Lines := True;
593 Undefined_Symbols_Are_False := Current_Data.Undef_False;
594 List_Preprocessing_Symbols := Current_Data.List_Symbols;
596 -- If not already done it, process the definition file
598 if Current_Data.Processed then
602 Prep.Mapping := Current_Data.Mapping;
605 -- First put the mapping in Prep.Mapping, because Prep.Parse_Def_File
606 -- works on Prep.Mapping.
608 Prep.Mapping := Current_Data.Mapping;
610 String_To_Name_Buffer (Current_Data.Deffile);
613 N : constant File_Name_Type := Name_Find;
614 Deffile : constant Source_File_Index :=
615 Load_Definition_File (N);
616 Add_Deffile : Boolean := True;
617 T : constant Nat := Total_Errors_Detected;
620 if Deffile = No_Source_File then
621 Fail ("definition file """
622 & Get_Name_String (N)
626 -- Initialize the preprocessor and set the characteristics of the
627 -- scanner for a definition file.
630 (Error_Msg => Errout.Error_Msg'Access,
631 Scan => Scn.Scanner.Scan'Access,
632 Set_Ignore_Errors => Errout.Set_Ignore_Errors'Access,
636 Scn.Scanner.Set_End_Of_Line_As_Token (True);
637 Scn.Scanner.Reset_Special_Characters;
639 -- Initialize the scanner and process the definition file
641 Scn.Scanner.Initialize_Scanner (Deffile);
644 -- Reset the behaviour of the scanner to the default
646 Scn.Scanner.Set_End_Of_Line_As_Token (False);
648 -- Fail if errors were found while processing the definition file
650 if T /= Total_Errors_Detected then
651 Errout.Finalize (Last_Call => True);
652 Errout.Output_Messages;
653 Fail ("errors found in definition file """
654 & Get_Name_String (N)
658 for Index in 1 .. Dependencies.Last loop
659 if Dependencies.Table (Index) = Deffile then
660 Add_Deffile := False;
666 Dependencies.Increment_Last;
667 Dependencies.Table (Dependencies.Last) := Deffile;
671 -- Get back the mapping, indicate that the definition file is
672 -- processed and store back the preprocessing data.
674 Current_Data.Mapping := Prep.Mapping;
675 Current_Data.Processed := True;
678 Default_Data := Current_Data;
681 Preproc_Data_Table.Table (Index) := Current_Data;
685 Preprocessing_Needed := True;
686 end Prepare_To_Preprocess;
688 ---------------------------------------------
689 -- Process_Command_Line_Symbol_Definitions --
690 ---------------------------------------------
692 procedure Process_Command_Line_Symbol_Definitions is
693 Symbol_Data : Prep.Symbol_Data;
694 Found : Boolean := False;
697 Symbol_Table.Init (Command_Line_Symbols);
699 -- The command line definitions have been stored temporarily in
700 -- array Symbol_Definitions.
702 for Index in 1 .. Preprocessing_Symbol_Last loop
703 -- Check each symbol definition, fail immediately if syntax is not
706 Check_Command_Line_Symbol_Definition
707 (Definition => Preprocessing_Symbol_Defs (Index).all,
708 Data => Symbol_Data);
711 -- If there is already a definition for this symbol, replace the old
712 -- definition by this one.
714 for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
715 if Command_Line_Symbols.Table (J).Symbol = Symbol_Data.Symbol then
716 Command_Line_Symbols.Table (J) := Symbol_Data;
722 -- Otherwise, create a new entry in the table
725 Symbol_Table.Increment_Last (Command_Line_Symbols);
726 Command_Line_Symbols.Table
727 (Symbol_Table.Last (Command_Line_Symbols)) := Symbol_Data;
730 end Process_Command_Line_Symbol_Definitions;
732 -------------------------
733 -- Skip_To_End_Of_Line --
734 -------------------------
736 procedure Skip_To_End_Of_Line is
738 Set_Ignore_Errors (To => True);
740 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
744 Set_Ignore_Errors (To => False);
745 end Skip_To_End_Of_Line;