OSDN Git Service

2012-05-03 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / ada / prepcomp.adb
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-2010, 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 ------------------------------------------------------------------------------
25
26 with Errout;   use Errout;
27 with Lib.Writ; use Lib.Writ;
28 with Opt;      use Opt;
29 with Osint;    use Osint;
30 with Prep;     use Prep;
31 with Scans;    use Scans;
32 with Scn;      use Scn;
33 with Sinput.L; use Sinput.L;
34 with Stringt;  use Stringt;
35 with Table;
36 with Types;    use Types;
37
38 package body Prepcomp is
39
40    No_Preprocessing : Boolean := True;
41    --  Set to False if there is at least one source that needs to be
42    --  preprocessed.
43
44    Source_Index_Of_Preproc_Data_File : Source_File_Index := No_Source_File;
45
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.
51
52    pragma Warnings (Off);
53    No_Mapping : Prep.Symbol_Table.Instance;
54    pragma Warnings (On);
55
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;
65    end record;
66    --  Structure to keep the preprocessing data for a file name or for the
67    --  default (when Name_Id = No_Name).
68
69    No_Preproc_Data : constant Preproc_Data :=
70      (Mapping      => No_Mapping,
71       File_Name    => No_File,
72       Deffile      => No_String,
73       Undef_False  => False,
74       Always_Blank => False,
75       Comments     => False,
76       List_Symbols => False,
77       Processed    => False);
78
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.
82
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.
86
87    Current_Data : Preproc_Data := No_Preproc_Data;
88
89    package Preproc_Data_Table is new Table.Table
90      (Table_Component_Type => Preproc_Data,
91       Table_Index_Type     => Int,
92       Table_Low_Bound      => 1,
93       Table_Initial        => 5,
94       Table_Increment      => 100,
95       Table_Name           => "Prepcomp.Preproc_Data_Table");
96    --  Table to store the specific preprocessing data
97
98    Command_Line_Symbols : Symbol_Table.Instance;
99    --  A table to store symbol definitions specified on the command line with
100    --  -gnateD switches.
101
102    package Dependencies is new Table.Table
103      (Table_Component_Type => Source_File_Index,
104       Table_Index_Type     => Int,
105       Table_Low_Bound      => 1,
106       Table_Initial        => 10,
107       Table_Increment      => 100,
108       Table_Name           => "Prepcomp.Dependencies");
109    --  Table to store the dependencies on preprocessing files
110
111    procedure Add_Command_Line_Symbols;
112    --  Add the command line symbol definitions, if any, to Prep.Mapping table
113
114    procedure Skip_To_End_Of_Line;
115    --  Ignore errors and scan up to the next end of line or the end of file
116
117    ------------------------------
118    -- Add_Command_Line_Symbols --
119    ------------------------------
120
121    procedure Add_Command_Line_Symbols is
122       Symbol_Id : Prep.Symbol_Id;
123
124    begin
125       for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
126          Symbol_Id := Prep.Index_Of (Command_Line_Symbols.Table (J).Symbol);
127
128          if Symbol_Id = No_Symbol then
129             Symbol_Table.Increment_Last (Prep.Mapping);
130             Symbol_Id := Symbol_Table.Last (Prep.Mapping);
131          end if;
132
133          Prep.Mapping.Table (Symbol_Id) := Command_Line_Symbols.Table (J);
134       end loop;
135    end Add_Command_Line_Symbols;
136
137    ----------------------
138    -- Add_Dependencies --
139    ----------------------
140
141    procedure Add_Dependencies is
142    begin
143       for Index in 1 .. Dependencies.Last loop
144          Add_Preprocessing_Dependency (Dependencies.Table (Index));
145       end loop;
146    end Add_Dependencies;
147
148    -------------------
149    -- Check_Symbols --
150    -------------------
151
152    procedure Check_Symbols is
153    begin
154       --  If there is at least one switch -gnateD specified
155
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;
160
161          --  Start with an empty, initialized mapping table; use Prep.Mapping,
162          --  because Prep.Index_Of uses Prep.Mapping.
163
164          Prep.Mapping := No_Mapping;
165          Symbol_Table.Init (Prep.Mapping);
166
167          --  Add the command line symbols
168
169          Add_Command_Line_Symbols;
170
171          --  Put the resulting Prep.Mapping in Current_Data, and immediately
172          --  set Prep.Mapping to nil.
173
174          Current_Data.Mapping := Prep.Mapping;
175          Prep.Mapping := No_Mapping;
176
177          --  Set the default data
178
179          Default_Data := Current_Data;
180          Default_Data_Defined := True;
181       end if;
182    end Check_Symbols;
183
184    ------------------------------
185    -- Parse_Preprocessing_Data --
186    ------------------------------
187
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;
194
195    begin
196       --  Load the preprocessing data file
197
198       Source_Index_Of_Preproc_Data_File := Load_Preprocessing_Data_File (N);
199
200       --  Fail if preprocessing data file cannot be found
201
202       if Source_Index_Of_Preproc_Data_File = No_Source_File then
203          Get_Name_String (N);
204          Fail ("preprocessing data file """
205                & Name_Buffer (1 .. Name_Len)
206                & """ not found");
207       end if;
208
209       --  Initialize scanner and set its behavior for processing a data file
210
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;
214
215       For_Each_Line : loop
216          <<Scan_Line>>
217          Scan;
218
219          exit For_Each_Line when Token = Tok_EOF;
220
221          if Token = Tok_End_Of_Line then
222             goto Scan_Line;
223          end if;
224
225          --  Line is not empty
226
227          OK := False;
228          No_Preprocessing := False;
229          Current_Data := No_Preproc_Data;
230
231          case Token is
232             when Tok_Asterisk =>
233
234                --  Default data
235
236                if Default_Data_Defined then
237                   Error_Msg
238                     ("multiple default preprocessing data", Token_Ptr);
239
240                else
241                   OK := True;
242                   Default_Data_Defined := True;
243                end if;
244
245             when Tok_String_Literal =>
246
247                --  Specific data
248
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;
252                OK := True;
253
254                for Index in 1 .. Preproc_Data_Table.Last loop
255                   if Current_Data.File_Name =
256                        Preproc_Data_Table.Table (Index).File_Name
257                   then
258                      Error_Msg_File_1 := Current_Data.File_Name;
259                      Error_Msg
260                        ("multiple preprocessing data for{", Token_Ptr);
261                      OK := False;
262                      exit;
263                   end if;
264                end loop;
265
266             when others =>
267                Error_Msg ("`'*` or literal string expected", Token_Ptr);
268          end case;
269
270          --  If there is a problem, skip the line
271
272          if not OK then
273             Skip_To_End_Of_Line;
274             goto Scan_Line;
275          end if;
276
277          --  Scan past the * or the literal string
278
279          Scan;
280
281          --  A literal string in second position is a definition file
282
283          if Token = Tok_String_Literal then
284             Current_Data.Deffile := String_Literal_Id;
285             Current_Data.Processed := False;
286             Scan;
287
288          else
289             --  If there is no definition file, set Processed to True now
290
291             Current_Data.Processed := True;
292          end if;
293
294          --  Start with an empty, initialized mapping table; use Prep.Mapping,
295          --  because Prep.Index_Of uses Prep.Mapping.
296
297          Prep.Mapping := No_Mapping;
298          Symbol_Table.Init (Prep.Mapping);
299
300          --  Check the switches that may follow
301
302          while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
303             if Token /= Tok_Minus then
304                Error_Msg -- CODEFIX
305                  ("`'-` expected", Token_Ptr);
306                Skip_To_End_Of_Line;
307                goto Scan_Line;
308             end if;
309
310             --  Keep the location of the '-' for possible error reporting
311
312             Dash_Location := Token_Ptr;
313
314             --  Scan past the '-'
315
316             Scan;
317             OK := False;
318             Change_Reserved_Keyword_To_Symbol;
319
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.
323
324             if Token = Tok_Identifier
325               and then Token_Ptr = Dash_Location + 1
326             then
327                Get_Name_String (Token_Name);
328
329                --  Check the character in the source, because the case is
330                --  significant.
331
332                case Sinput.Source (Token_Ptr) is
333                   when 'u' =>
334
335                      --  Undefined symbol are False
336
337                      if Name_Len = 1 then
338                         Current_Data.Undef_False := True;
339                         OK := True;
340                      end if;
341
342                   when 'b' =>
343
344                      --  Blank lines
345
346                      if Name_Len = 1 then
347                         Current_Data.Always_Blank := True;
348                         OK := True;
349                      end if;
350
351                   when 'c' =>
352
353                      --  Comment removed lines
354
355                      if Name_Len = 1 then
356                         Current_Data.Comments := True;
357                         OK := True;
358                      end if;
359
360                   when 's' =>
361
362                      --  List symbols
363
364                      if Name_Len = 1 then
365                         Current_Data.List_Symbols := True;
366                         OK := True;
367                      end if;
368
369                   when 'D' =>
370
371                      --  Symbol definition
372
373                      OK := Name_Len > 1;
374
375                      if OK then
376
377                         --  A symbol must be an Ada identifier; it cannot start
378                         --  with an underline or a digit.
379
380                         if Name_Buffer (2) = '_'
381                           or else Name_Buffer (2) in '0' .. '9'
382                         then
383                            Error_Msg ("symbol expected", Token_Ptr + 1);
384                            Skip_To_End_Of_Line;
385                            goto Scan_Line;
386                         end if;
387
388                         --  Get the name id of the symbol
389
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;
395
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"
403                         then
404                            Error_Msg ("symbol expected", Token_Ptr + 1);
405                            Skip_To_End_Of_Line;
406                            goto Scan_Line;
407                         end if;
408
409                         --  Get the name id of the original symbol, with
410                         --  possibly capital letters.
411
412                         Name_Len := Integer (Scan_Ptr - Token_Ptr - 1);
413
414                         for J in 1 .. Name_Len loop
415                            Name_Buffer (J) :=
416                              Sinput.Source (Token_Ptr + Text_Ptr (J));
417                         end loop;
418
419                         Symbol_Data.Original := Name_Find;
420
421                         --  Scan past D<symbol>
422
423                         Scan;
424
425                         if Token /= Tok_Equal then
426                            Error_Msg -- CODEFIX
427                              ("`=` expected", Token_Ptr);
428                            Skip_To_End_Of_Line;
429                            goto Scan_Line;
430                         end if;
431
432                         --  Scan past '='
433
434                         Scan;
435
436                         --  Here any reserved word is OK
437
438                         Change_Reserved_Keyword_To_Symbol
439                           (All_Keywords => True);
440
441                         --  Value can be an identifier (or a reserved word)
442                         --  or a literal string.
443
444                         case Token is
445                            when Tok_String_Literal =>
446                               Symbol_Data.Is_A_String := True;
447                               Symbol_Data.Value := String_Literal_Id;
448
449                            when Tok_Identifier =>
450                               Symbol_Data.Is_A_String := False;
451                               Start_String;
452
453                               for J in Token_Ptr .. Scan_Ptr - 1 loop
454                                  Store_String_Char (Sinput.Source (J));
455                               end loop;
456
457                               Symbol_Data.Value := End_String;
458
459                            when others =>
460                               Error_Msg
461                                 ("literal string or identifier expected",
462                                  Token_Ptr);
463                               Skip_To_End_Of_Line;
464                               goto Scan_Line;
465                         end case;
466
467                         --  If symbol already exists, replace old definition
468                         --  by new one.
469
470                         Symbol_Id := Prep.Index_Of (Symbol_Data.Symbol);
471
472                         --  Otherwise, add a new entry in the table
473
474                         if Symbol_Id = No_Symbol then
475                            Symbol_Table.Increment_Last (Prep.Mapping);
476                            Symbol_Id := Symbol_Table.Last (Mapping);
477                         end if;
478
479                         Prep.Mapping.Table (Symbol_Id) := Symbol_Data;
480                      end if;
481
482                   when others =>
483                      null;
484                end case;
485
486                Scan;
487             end if;
488
489             if not OK then
490                Error_Msg ("invalid switch", Dash_Location);
491                Skip_To_End_Of_Line;
492                goto Scan_Line;
493             end if;
494          end loop;
495
496          --  Add the command line symbols, if any, possibly replacing symbols
497          --  just defined.
498
499          Add_Command_Line_Symbols;
500
501          --  Put the resulting Prep.Mapping in Current_Data, and immediately
502          --  set Prep.Mapping to nil.
503
504          Current_Data.Mapping := Prep.Mapping;
505          Prep.Mapping := No_Mapping;
506
507          --  Record Current_Data
508
509          if Current_Data.File_Name = No_File then
510             Default_Data := Current_Data;
511
512          else
513             Preproc_Data_Table.Increment_Last;
514             Preproc_Data_Table.Table (Preproc_Data_Table.Last) := Current_Data;
515          end if;
516
517          Current_Data := No_Preproc_Data;
518       end loop For_Each_Line;
519
520       Scn.Scanner.Set_End_Of_Line_As_Token (False);
521
522       --  Fail if there were errors in the preprocessing data file
523
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) & """");
529       end if;
530
531       --  Record the dependency on the preprocessor data file
532
533       Dependencies.Increment_Last;
534       Dependencies.Table (Dependencies.Last) :=
535         Source_Index_Of_Preproc_Data_File;
536    end Parse_Preprocessing_Data_File;
537
538    ---------------------------
539    -- Prepare_To_Preprocess --
540    ---------------------------
541
542    procedure Prepare_To_Preprocess
543      (Source               : File_Name_Type;
544       Preprocessing_Needed : out Boolean)
545    is
546       Default : Boolean := False;
547       Index   : Int := 0;
548
549    begin
550       --  By default, preprocessing is not needed
551
552       Preprocessing_Needed := False;
553
554       if No_Preprocessing then
555          return;
556       end if;
557
558       --  First, look for preprocessing data specific to the current source
559
560       for J in 1 .. Preproc_Data_Table.Last loop
561          if Preproc_Data_Table.Table (J).File_Name = Source then
562             Index := J;
563             Current_Data := Preproc_Data_Table.Table (J);
564             exit;
565          end if;
566       end loop;
567
568       --  If no specific preprocessing data, then take the default
569
570       if Index = 0 then
571          if Default_Data_Defined then
572             Current_Data := Default_Data;
573             Default := True;
574
575          else
576             --  If no default, then nothing to do
577
578             return;
579          end if;
580       end if;
581
582       --  Set the preprocessing flags according to the preprocessing data
583
584       if Current_Data.Comments and then not Current_Data.Always_Blank then
585          Comment_Deleted_Lines := True;
586          Blank_Deleted_Lines   := False;
587
588       else
589          Comment_Deleted_Lines := False;
590          Blank_Deleted_Lines   := True;
591       end if;
592
593       Undefined_Symbols_Are_False := Current_Data.Undef_False;
594       List_Preprocessing_Symbols  := Current_Data.List_Symbols;
595
596       --  If not already done it, process the definition file
597
598       if Current_Data.Processed then
599
600          --  Set Prep.Mapping
601
602          Prep.Mapping := Current_Data.Mapping;
603
604       else
605          --  First put the mapping in Prep.Mapping, because Prep.Parse_Def_File
606          --  works on Prep.Mapping.
607
608          Prep.Mapping := Current_Data.Mapping;
609
610          String_To_Name_Buffer (Current_Data.Deffile);
611
612          declare
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;
618
619          begin
620             if Deffile = No_Source_File then
621                Fail ("definition file """
622                      & Get_Name_String (N)
623                      & """ not found");
624             end if;
625
626             --  Initialize the preprocessor and set the characteristics of the
627             --  scanner for a definition file.
628
629             Prep.Setup_Hooks
630               (Error_Msg         => Errout.Error_Msg'Access,
631                Scan              => Scn.Scanner.Scan'Access,
632                Set_Ignore_Errors => Errout.Set_Ignore_Errors'Access,
633                Put_Char          => null,
634                New_EOL           => null);
635
636             Scn.Scanner.Set_End_Of_Line_As_Token (True);
637             Scn.Scanner.Reset_Special_Characters;
638
639             --  Initialize the scanner and process the definition file
640
641             Scn.Scanner.Initialize_Scanner (Deffile);
642             Prep.Parse_Def_File;
643
644             --  Reset the behaviour of the scanner to the default
645
646             Scn.Scanner.Set_End_Of_Line_As_Token (False);
647
648             --  Fail if errors were found while processing the definition file
649
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)
655                      & """");
656             end if;
657
658             for Index in 1 .. Dependencies.Last loop
659                if Dependencies.Table (Index) = Deffile then
660                   Add_Deffile := False;
661                   exit;
662                end if;
663             end loop;
664
665             if Add_Deffile then
666                Dependencies.Increment_Last;
667                Dependencies.Table (Dependencies.Last) := Deffile;
668             end if;
669          end;
670
671          --  Get back the mapping, indicate that the definition file is
672          --  processed and store back the preprocessing data.
673
674          Current_Data.Mapping := Prep.Mapping;
675          Current_Data.Processed := True;
676
677          if Default then
678             Default_Data := Current_Data;
679
680          else
681             Preproc_Data_Table.Table (Index) := Current_Data;
682          end if;
683       end if;
684
685       Preprocessing_Needed := True;
686    end Prepare_To_Preprocess;
687
688    ---------------------------------------------
689    -- Process_Command_Line_Symbol_Definitions --
690    ---------------------------------------------
691
692    procedure Process_Command_Line_Symbol_Definitions is
693       Symbol_Data : Prep.Symbol_Data;
694       Found : Boolean := False;
695
696    begin
697       Symbol_Table.Init (Command_Line_Symbols);
698
699       --  The command line definitions have been stored temporarily in
700       --  array Symbol_Definitions.
701
702       for Index in 1 .. Preprocessing_Symbol_Last loop
703          --  Check each symbol definition, fail immediately if syntax is not
704          --  correct.
705
706          Check_Command_Line_Symbol_Definition
707            (Definition => Preprocessing_Symbol_Defs (Index).all,
708             Data       => Symbol_Data);
709          Found := False;
710
711          --  If there is already a definition for this symbol, replace the old
712          --  definition by this one.
713
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;
717                Found := True;
718                exit;
719             end if;
720          end loop;
721
722          --  Otherwise, create a new entry in the table
723
724          if not Found then
725             Symbol_Table.Increment_Last (Command_Line_Symbols);
726             Command_Line_Symbols.Table
727               (Symbol_Table.Last (Command_Line_Symbols)) := Symbol_Data;
728          end if;
729       end loop;
730    end Process_Command_Line_Symbol_Definitions;
731
732    -------------------------
733    -- Skip_To_End_Of_Line --
734    -------------------------
735
736    procedure Skip_To_End_Of_Line is
737    begin
738       Set_Ignore_Errors (To => True);
739
740       while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
741          Scan;
742       end loop;
743
744       Set_Ignore_Errors (To => False);
745    end Skip_To_End_Of_Line;
746
747 end Prepcomp;