Merged with mainline at revision 128810.
[official-gcc.git] / gcc / ada / prepcomp.adb
blobd23c130323fda9634cbfe86f87a3923798aa650d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P R E P C O M P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2003-2007, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Ada.Unchecked_Deallocation;
28 with Errout; use Errout;
29 with Lib.Writ; use Lib.Writ;
30 with Opt; use Opt;
31 with Osint; use Osint;
32 with Prep; use Prep;
33 with Scans; use Scans;
34 with Scn; use Scn;
35 with Sinput.L; use Sinput.L;
36 with Stringt; use Stringt;
37 with Table;
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
44 -- preprocessed.
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
49 -- possible. Warnings are Off because it is never assigned a value.
51 pragma Warnings (Off);
52 No_Mapping : Prep.Symbol_Table.Instance;
53 pragma Warnings (On);
55 type String_Ptr is access String;
56 type String_Array is array (Positive range <>) of String_Ptr;
57 type String_Array_Ptr is access String_Array;
59 procedure Free is
60 new Ada.Unchecked_Deallocation (String_Array, String_Array_Ptr);
62 Symbol_Definitions : String_Array_Ptr := new String_Array (1 .. 4);
63 -- An extensible array to temporarily stores symbol definitions specified
64 -- on the command line with -gnateD switches.
66 Last_Definition : Natural := 0;
67 -- Index of last symbol definition in array Symbol_Definitions
69 type Preproc_Data is record
70 Mapping : Symbol_Table.Instance;
71 File_Name : File_Name_Type := No_File;
72 Deffile : String_Id := No_String;
73 Undef_False : Boolean := False;
74 Always_Blank : Boolean := False;
75 Comments : Boolean := False;
76 List_Symbols : Boolean := False;
77 Processed : Boolean := False;
78 end record;
79 -- Structure to keep the preprocessing data for a file name or for the
80 -- default (when Name_Id = No_Name).
82 No_Preproc_Data : constant Preproc_Data :=
83 (Mapping => No_Mapping,
84 File_Name => No_File,
85 Deffile => No_String,
86 Undef_False => False,
87 Always_Blank => False,
88 Comments => False,
89 List_Symbols => False,
90 Processed => False);
92 Default_Data : Preproc_Data := No_Preproc_Data;
93 -- The preprocessing data to be used when no specific preprocessing data
94 -- is specified for a source.
96 Default_Data_Defined : Boolean := False;
97 -- True if source for which no specific preprocessing is specified need to
98 -- be preprocess with the Default_Data.
100 Current_Data : Preproc_Data := No_Preproc_Data;
102 package Preproc_Data_Table is new Table.Table
103 (Table_Component_Type => Preproc_Data,
104 Table_Index_Type => Int,
105 Table_Low_Bound => 1,
106 Table_Initial => 5,
107 Table_Increment => 100,
108 Table_Name => "Prepcomp.Preproc_Data_Table");
109 -- Table to store the specific preprocessing data
111 Command_Line_Symbols : Symbol_Table.Instance;
112 -- A table to store symbol definitions specified on the command line with
113 -- -gnateD switches.
115 package Dependencies is new Table.Table
116 (Table_Component_Type => Source_File_Index,
117 Table_Index_Type => Int,
118 Table_Low_Bound => 1,
119 Table_Initial => 10,
120 Table_Increment => 100,
121 Table_Name => "Prepcomp.Dependencies");
122 -- Table to store the dependencies on preprocessing files
124 procedure Add_Command_Line_Symbols;
125 -- Add the command line symbol definitions, if any, to the
126 -- Prep.Mapping table.
128 procedure Skip_To_End_Of_Line;
129 -- Ignore errors and scan up to the next end of line or the end of file
131 ------------------------------
132 -- Add_Command_Line_Symbols --
133 ------------------------------
135 procedure Add_Command_Line_Symbols is
136 Symbol_Id : Prep.Symbol_Id;
138 begin
139 for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
140 Symbol_Id := Prep.Index_Of (Command_Line_Symbols.Table (J).Symbol);
142 if Symbol_Id = No_Symbol then
143 Symbol_Table.Increment_Last (Prep.Mapping);
144 Symbol_Id := Symbol_Table.Last (Prep.Mapping);
145 end if;
147 Prep.Mapping.Table (Symbol_Id) := Command_Line_Symbols.Table (J);
148 end loop;
149 end Add_Command_Line_Symbols;
151 ----------------------
152 -- Add_Dependencies --
153 ----------------------
155 procedure Add_Dependencies is
156 begin
157 for Index in 1 .. Dependencies.Last loop
158 Add_Preprocessing_Dependency (Dependencies.Table (Index));
159 end loop;
160 end Add_Dependencies;
162 ---------------------------
163 -- Add_Symbol_Definition --
164 ---------------------------
166 procedure Add_Symbol_Definition (Def : String) is
167 begin
168 -- If Symbol_Definitions is not large enough, double it
170 if Last_Definition = Symbol_Definitions'Last then
171 declare
172 New_Symbol_Definitions : constant String_Array_Ptr :=
173 new String_Array (1 .. 2 * Last_Definition);
175 begin
176 New_Symbol_Definitions (Symbol_Definitions'Range) :=
177 Symbol_Definitions.all;
178 Free (Symbol_Definitions);
179 Symbol_Definitions := New_Symbol_Definitions;
180 end;
181 end if;
183 Last_Definition := Last_Definition + 1;
184 Symbol_Definitions (Last_Definition) := new String'(Def);
185 end Add_Symbol_Definition;
187 -------------------
188 -- Check_Symbols --
189 -------------------
191 procedure Check_Symbols is
192 begin
193 -- If there is at least one switch -gnateD specified
195 if Symbol_Table.Last (Command_Line_Symbols) >= 1 then
196 Current_Data := No_Preproc_Data;
197 No_Preprocessing := False;
198 Current_Data.Processed := True;
200 -- Start with an empty, initialized mapping table; use Prep.Mapping,
201 -- because Prep.Index_Of uses Prep.Mapping.
203 Prep.Mapping := No_Mapping;
204 Symbol_Table.Init (Prep.Mapping);
206 -- Add the command line symbols
208 Add_Command_Line_Symbols;
210 -- Put the resulting Prep.Mapping in Current_Data, and immediately
211 -- set Prep.Mapping to nil.
213 Current_Data.Mapping := Prep.Mapping;
214 Prep.Mapping := No_Mapping;
216 -- Set the default data
218 Default_Data := Current_Data;
219 Default_Data_Defined := True;
220 end if;
221 end Check_Symbols;
223 ------------------------------
224 -- Parse_Preprocessing_Data --
225 ------------------------------
227 procedure Parse_Preprocessing_Data_File (N : File_Name_Type) is
228 OK : Boolean := False;
229 Dash_Location : Source_Ptr;
230 Symbol_Data : Prep.Symbol_Data;
231 Symbol_Id : Prep.Symbol_Id;
232 T : constant Nat := Total_Errors_Detected;
234 begin
235 -- Load the preprocessing data file
237 Source_Index_Of_Preproc_Data_File := Load_Preprocessing_Data_File (N);
239 -- Fail if preprocessing data file cannot be found
241 if Source_Index_Of_Preproc_Data_File = No_Source_File then
242 Get_Name_String (N);
243 Fail ("preprocessing data file """,
244 Name_Buffer (1 .. Name_Len),
245 """ not found");
246 end if;
248 -- Initialize the sanner and set its behavior for a processing data file
250 Scn.Scanner.Initialize_Scanner (Source_Index_Of_Preproc_Data_File);
251 Scn.Scanner.Set_End_Of_Line_As_Token (True);
252 Scn.Scanner.Reset_Special_Characters;
254 For_Each_Line : loop
255 <<Scan_Line>>
256 Scan;
258 exit For_Each_Line when Token = Tok_EOF;
260 if Token = Tok_End_Of_Line then
261 goto Scan_Line;
262 end if;
264 -- Line is not empty
266 OK := False;
267 No_Preprocessing := False;
268 Current_Data := No_Preproc_Data;
270 case Token is
271 when Tok_Asterisk =>
273 -- Default data
275 if Default_Data_Defined then
276 Error_Msg
277 ("multiple default preprocessing data", Token_Ptr);
279 else
280 OK := True;
281 Default_Data_Defined := True;
282 end if;
284 when Tok_String_Literal =>
286 -- Specific data
288 String_To_Name_Buffer (String_Literal_Id);
289 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
290 Current_Data.File_Name := Name_Find;
291 OK := True;
293 for Index in 1 .. Preproc_Data_Table.Last loop
294 if Current_Data.File_Name =
295 Preproc_Data_Table.Table (Index).File_Name
296 then
297 Error_Msg_File_1 := Current_Data.File_Name;
298 Error_Msg
299 ("multiple preprocessing data for{", Token_Ptr);
300 OK := False;
301 exit;
302 end if;
303 end loop;
305 when others =>
306 Error_Msg ("`'*` or literal string expected", Token_Ptr);
307 end case;
309 -- If there is a problem, skip the line
311 if not OK then
312 Skip_To_End_Of_Line;
313 goto Scan_Line;
314 end if;
316 -- Scan past the * or the literal string
318 Scan;
320 -- A literal string in second position is a definition file
322 if Token = Tok_String_Literal then
323 Current_Data.Deffile := String_Literal_Id;
324 Current_Data.Processed := False;
325 Scan;
327 else
328 -- If there is no definition file, set Processed to True now
330 Current_Data.Processed := True;
331 end if;
333 -- Start with an empty, initialized mapping table; use Prep.Mapping,
334 -- because Prep.Index_Of uses Prep.Mapping.
336 Prep.Mapping := No_Mapping;
337 Symbol_Table.Init (Prep.Mapping);
339 -- Check the switches that may follow
341 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
342 if Token /= Tok_Minus then
343 Error_Msg ("`'-` expected", Token_Ptr);
344 Skip_To_End_Of_Line;
345 goto Scan_Line;
346 end if;
348 -- Keep the location of the '-' for possible error reporting
350 Dash_Location := Token_Ptr;
352 -- Scan past the '-'
354 Scan;
355 OK := False;
356 Change_Reserved_Keyword_To_Symbol;
358 -- An identifier (or a reserved word converted to an
359 -- identifier) is expected and there must be no blank space
360 -- between the '-' and the identifier.
362 if Token = Tok_Identifier
363 and then Token_Ptr = Dash_Location + 1
364 then
365 Get_Name_String (Token_Name);
367 -- Check the character in the source, because the case is
368 -- significant.
370 case Sinput.Source (Token_Ptr) is
371 when 'u' =>
373 -- Undefined symbol are False
375 if Name_Len = 1 then
376 Current_Data.Undef_False := True;
377 OK := True;
378 end if;
380 when 'b' =>
382 -- Blank lines
384 if Name_Len = 1 then
385 Current_Data.Always_Blank := True;
386 OK := True;
387 end if;
389 when 'c' =>
391 -- Comment removed lines
393 if Name_Len = 1 then
394 Current_Data.Comments := True;
395 OK := True;
396 end if;
398 when 's' =>
400 -- List symbols
402 if Name_Len = 1 then
403 Current_Data.List_Symbols := True;
404 OK := True;
405 end if;
407 when 'D' =>
409 -- Symbol definition
411 OK := Name_Len > 1;
413 if OK then
415 -- A symbol must be an Ada identifier; it cannot start
416 -- with an underline or a digit.
418 if Name_Buffer (2) = '_'
419 or Name_Buffer (2) in '0' .. '9'
420 then
421 Error_Msg ("symbol expected", Token_Ptr + 1);
422 Skip_To_End_Of_Line;
423 goto Scan_Line;
424 end if;
426 -- Get the name id of the symbol
428 Symbol_Data.On_The_Command_Line := True;
429 Name_Buffer (1 .. Name_Len - 1) :=
430 Name_Buffer (2 .. Name_Len);
431 Name_Len := Name_Len - 1;
432 Symbol_Data.Symbol := Name_Find;
434 if Name_Buffer (1 .. Name_Len) = "if"
435 or else Name_Buffer (1 .. Name_Len) = "else"
436 or else Name_Buffer (1 .. Name_Len) = "elsif"
437 or else Name_Buffer (1 .. Name_Len) = "end"
438 or else Name_Buffer (1 .. Name_Len) = "not"
439 or else Name_Buffer (1 .. Name_Len) = "and"
440 or else Name_Buffer (1 .. Name_Len) = "then"
441 then
442 Error_Msg ("symbol expected", Token_Ptr + 1);
443 Skip_To_End_Of_Line;
444 goto Scan_Line;
445 end if;
447 -- Get the name id of the original symbol, with
448 -- possibly capital letters.
450 Name_Len := Integer (Scan_Ptr - Token_Ptr - 1);
452 for J in 1 .. Name_Len loop
453 Name_Buffer (J) :=
454 Sinput.Source (Token_Ptr + Text_Ptr (J));
455 end loop;
457 Symbol_Data.Original := Name_Find;
459 -- Scan past D<symbol>
461 Scan;
463 if Token /= Tok_Equal then
464 Error_Msg ("`=` expected", Token_Ptr);
465 Skip_To_End_Of_Line;
466 goto Scan_Line;
467 end if;
469 -- Scan past '='
471 Scan;
473 -- Here any reserved word is OK
475 Change_Reserved_Keyword_To_Symbol
476 (All_Keywords => True);
478 -- Value can be an identifier (or a reserved word)
479 -- or a literal string.
481 case Token is
482 when Tok_String_Literal =>
483 Symbol_Data.Is_A_String := True;
484 Symbol_Data.Value := String_Literal_Id;
486 when Tok_Identifier =>
487 Symbol_Data.Is_A_String := False;
488 Start_String;
490 for J in Token_Ptr .. Scan_Ptr - 1 loop
491 Store_String_Char (Sinput.Source (J));
492 end loop;
494 Symbol_Data.Value := End_String;
496 when others =>
497 Error_Msg
498 ("literal string or identifier expected",
499 Token_Ptr);
500 Skip_To_End_Of_Line;
501 goto Scan_Line;
502 end case;
504 -- If symbol already exists, replace old definition
505 -- by new one.
507 Symbol_Id := Prep.Index_Of (Symbol_Data.Symbol);
509 -- Otherwise, add a new entry in the table
511 if Symbol_Id = No_Symbol then
512 Symbol_Table.Increment_Last (Prep.Mapping);
513 Symbol_Id := Symbol_Table.Last (Mapping);
514 end if;
516 Prep.Mapping.Table (Symbol_Id) := Symbol_Data;
517 end if;
519 when others =>
520 null;
521 end case;
523 Scan;
524 end if;
526 if not OK then
527 Error_Msg ("invalid switch", Dash_Location);
528 Skip_To_End_Of_Line;
529 goto Scan_Line;
530 end if;
531 end loop;
533 -- Add the command line symbols, if any, possibly replacing symbols
534 -- just defined.
536 Add_Command_Line_Symbols;
538 -- Put the resulting Prep.Mapping in Current_Data, and immediately
539 -- set Prep.Mapping to nil.
541 Current_Data.Mapping := Prep.Mapping;
542 Prep.Mapping := No_Mapping;
544 -- Record Current_Data
546 if Current_Data.File_Name = No_File then
547 Default_Data := Current_Data;
549 else
550 Preproc_Data_Table.Increment_Last;
551 Preproc_Data_Table.Table (Preproc_Data_Table.Last) := Current_Data;
552 end if;
554 Current_Data := No_Preproc_Data;
555 end loop For_Each_Line;
557 Scn.Scanner.Set_End_Of_Line_As_Token (False);
559 -- Fail if there were errors in the preprocessing data file
561 if Total_Errors_Detected > T then
562 Errout.Finalize (Last_Call => True);
563 Errout.Output_Messages;
564 Fail ("errors found in preprocessing data file """,
565 Get_Name_String (N),
566 """");
567 end if;
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;
585 Index : Int := 0;
587 begin
588 -- By default, preprocessing is not needed
590 Preprocessing_Needed := False;
592 if No_Preprocessing then
593 return;
594 end if;
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
600 Index := J;
601 Current_Data := Preproc_Data_Table.Table (J);
602 exit;
603 end if;
604 end loop;
606 -- If no specific preprocessing data, then take the default
608 if Index = 0 then
609 if Default_Data_Defined then
610 Current_Data := Default_Data;
611 Default := True;
613 else
614 -- If no default, then nothing to do
616 return;
617 end if;
618 end if;
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;
626 else
627 Comment_Deleted_Lines := False;
628 Blank_Deleted_Lines := True;
629 end if;
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
638 -- Set Prep.Mapping
640 Prep.Mapping := Current_Data.Mapping;
642 else
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);
650 declare
651 N : constant File_Name_Type := Name_Find;
652 Deffile : constant Source_File_Index :=
653 Load_Definition_File (N);
654 Add_Deffile : Boolean := True;
655 T : constant Nat := Total_Errors_Detected;
657 begin
658 if Deffile = No_Source_File then
659 Fail ("definition file """,
660 Get_Name_String (N),
661 """ cannot be found");
662 end if;
664 -- Initialize the preprocessor and set the characteristics of the
665 -- scanner for a definition file.
667 Prep.Initialize
668 (Error_Msg => Errout.Error_Msg'Access,
669 Scan => Scn.Scanner.Scan'Access,
670 Set_Ignore_Errors => Errout.Set_Ignore_Errors'Access,
671 Put_Char => null,
672 New_EOL => null);
674 Scn.Scanner.Set_End_Of_Line_As_Token (True);
675 Scn.Scanner.Reset_Special_Characters;
677 -- Initialize the scanner and process the definition file
679 Scn.Scanner.Initialize_Scanner (Deffile);
680 Prep.Parse_Def_File;
682 -- Reset the behaviour of the scanner to the default
684 Scn.Scanner.Set_End_Of_Line_As_Token (False);
686 -- Fail if errors were found while processing the definition file
688 if T /= Total_Errors_Detected then
689 Errout.Finalize (Last_Call => True);
690 Errout.Output_Messages;
691 Fail ("errors found in definition file """,
692 Get_Name_String (N),
693 """");
694 end if;
696 for Index in 1 .. Dependencies.Last loop
697 if Dependencies.Table (Index) = Deffile then
698 Add_Deffile := False;
699 exit;
700 end if;
701 end loop;
703 if Add_Deffile then
704 Dependencies.Increment_Last;
705 Dependencies.Table (Dependencies.Last) := Deffile;
706 end if;
707 end;
709 -- Get back the mapping, indicate that the definition file is
710 -- processed and store back the preprocessing data.
712 Current_Data.Mapping := Prep.Mapping;
713 Current_Data.Processed := True;
715 if Default then
716 Default_Data := Current_Data;
718 else
719 Preproc_Data_Table.Table (Index) := Current_Data;
720 end if;
721 end if;
723 Preprocessing_Needed := True;
724 end Prepare_To_Preprocess;
726 ---------------------------------------------
727 -- Process_Command_Line_Symbol_Definitions --
728 ---------------------------------------------
730 procedure Process_Command_Line_Symbol_Definitions is
731 Symbol_Data : Prep.Symbol_Data;
732 Found : Boolean := False;
734 begin
735 Symbol_Table.Init (Command_Line_Symbols);
737 -- The command line definitions have been stored temporarily in
738 -- array Symbol_Definitions.
740 for Index in 1 .. Last_Definition loop
741 -- Check each symbol definition, fail immediately if syntax is not
742 -- correct.
744 Check_Command_Line_Symbol_Definition
745 (Definition => Symbol_Definitions (Index).all,
746 Data => Symbol_Data);
747 Found := False;
749 -- If there is already a definition for this symbol, replace the old
750 -- definition by this one.
752 for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
753 if Command_Line_Symbols.Table (J).Symbol = Symbol_Data.Symbol then
754 Command_Line_Symbols.Table (J) := Symbol_Data;
755 Found := True;
756 exit;
757 end if;
758 end loop;
760 -- Otherwise, create a new entry in the table
762 if not Found then
763 Symbol_Table.Increment_Last (Command_Line_Symbols);
764 Command_Line_Symbols.Table
765 (Symbol_Table.Last (Command_Line_Symbols)) := Symbol_Data;
766 end if;
767 end loop;
768 end Process_Command_Line_Symbol_Definitions;
770 -------------------------
771 -- Skip_To_End_Of_Line --
772 -------------------------
774 procedure Skip_To_End_Of_Line is
775 begin
776 Set_Ignore_Errors (To => True);
778 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
779 Scan;
780 end loop;
782 Set_Ignore_Errors (To => False);
783 end Skip_To_End_Of_Line;
785 end Prepcomp;