OSDN Git Service

Remove duplicate entries.
[pf3gnuchains/gcc-fork.git] / gcc / ada / ali-util.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             A L I . U T I L                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2011, 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 Debug;   use Debug;
27 with Binderr; use Binderr;
28 with Opt;     use Opt;
29 with Output;  use Output;
30 with Osint;   use Osint;
31 with Scans;   use Scans;
32 with Scng;
33 with Sinput.C;
34 with Snames;  use Snames;
35 with Styleg;
36
37 package body ALI.Util is
38
39    --  Empty procedures needed to instantiate Scng. Error procedures are
40    --  empty, because we don't want to report any errors when computing
41    --  a source checksum.
42
43    procedure Post_Scan;
44
45    procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr);
46
47    procedure Error_Msg_S (Msg : String);
48
49    procedure Error_Msg_SC (Msg : String);
50
51    procedure Error_Msg_SP (Msg : String);
52
53    --  Instantiation of Styleg, needed to instantiate Scng
54
55    package Style is new Styleg
56      (Error_Msg, Error_Msg_S, Error_Msg_SC, Error_Msg_SP);
57
58    --  A Scanner is needed to get checksum of a source (procedure
59    --  Get_File_Checksum).
60
61    package Scanner is new Scng
62      (Post_Scan, Error_Msg, Error_Msg_S, Error_Msg_SC, Error_Msg_SP, Style);
63
64    type Header_Num is range 0 .. 1_000;
65
66    function Hash (F : File_Name_Type) return Header_Num;
67    --  Function used to compute hash of ALI file name
68
69    package Interfaces is new Simple_HTable (
70      Header_Num => Header_Num,
71      Element    => Boolean,
72      No_Element => False,
73      Key        => File_Name_Type,
74      Hash       => Hash,
75      Equal      => "=");
76
77    ---------------------
78    -- Checksums_Match --
79    ---------------------
80
81    function Checksums_Match (Checksum1, Checksum2 : Word) return Boolean is
82    begin
83       return Checksum1 = Checksum2 and then Checksum1 /= Checksum_Error;
84    end Checksums_Match;
85
86    ---------------
87    -- Error_Msg --
88    ---------------
89
90    procedure Error_Msg (Msg : String; Flag_Location : Source_Ptr) is
91       pragma Warnings (Off, Msg);
92       pragma Warnings (Off, Flag_Location);
93    begin
94       null;
95    end Error_Msg;
96
97    -----------------
98    -- Error_Msg_S --
99    -----------------
100
101    procedure Error_Msg_S (Msg : String) is
102       pragma Warnings (Off, Msg);
103    begin
104       null;
105    end Error_Msg_S;
106
107    ------------------
108    -- Error_Msg_SC --
109    ------------------
110
111    procedure Error_Msg_SC (Msg : String) is
112       pragma Warnings (Off, Msg);
113    begin
114       null;
115    end Error_Msg_SC;
116
117    ------------------
118    -- Error_Msg_SP --
119    ------------------
120
121    procedure Error_Msg_SP (Msg : String) is
122       pragma Warnings (Off, Msg);
123    begin
124       null;
125    end Error_Msg_SP;
126
127    -----------------------
128    -- Get_File_Checksum --
129    -----------------------
130
131    function Get_File_Checksum (Fname : File_Name_Type) return Word is
132       Full_Name    : File_Name_Type;
133       Source_Index : Source_File_Index;
134
135    begin
136       Full_Name := Find_File (Fname, Osint.Source);
137
138       --  If we cannot find the file, then return an impossible checksum,
139       --  impossible because checksums have the high order bit zero, so
140       --  that checksums do not match.
141
142       if Full_Name = No_File then
143          return Checksum_Error;
144       end if;
145
146       Source_Index := Sinput.C.Load_File (Get_Name_String (Full_Name));
147
148       if Source_Index = No_Source_File then
149          return Checksum_Error;
150       end if;
151
152       Scanner.Initialize_Scanner (Source_Index);
153
154       --  Make sure that the project language reserved words are not
155       --  recognized as reserved words, but as identifiers. The byte info for
156       --  those names have been set if we are in gnatmake.
157
158       Set_Name_Table_Byte (Name_Project,          0);
159       Set_Name_Table_Byte (Name_Extends,          0);
160       Set_Name_Table_Byte (Name_External,         0);
161       Set_Name_Table_Byte (Name_External_As_List, 0);
162
163       --  Scan the complete file to compute its checksum
164
165       loop
166          Scanner.Scan;
167          exit when Token = Tok_EOF;
168       end loop;
169
170       return Scans.Checksum;
171    end Get_File_Checksum;
172
173    ----------
174    -- Hash --
175    ----------
176
177    function Hash (F : File_Name_Type) return Header_Num is
178    begin
179       return Header_Num (Int (F) rem Header_Num'Range_Length);
180    end Hash;
181
182    ---------------------------
183    -- Initialize_ALI_Source --
184    ---------------------------
185
186    procedure Initialize_ALI_Source is
187    begin
188       --  When (re)initializing ALI data structures the ALI user expects to
189       --  get a fresh set of data structures. Thus we first need to erase the
190       --  marks put in the name table by the previous set of ALI routine calls.
191       --  This loop is empty and harmless the first time in.
192
193       for J in Source.First .. Source.Last loop
194          Set_Name_Table_Info (Source.Table (J).Sfile, 0);
195          Source.Table (J).Source_Found := False;
196       end loop;
197
198       Source.Init;
199       Interfaces.Reset;
200    end Initialize_ALI_Source;
201
202    ---------------
203    -- Post_Scan --
204    ---------------
205
206    procedure Post_Scan is
207    begin
208       null;
209    end Post_Scan;
210
211    ----------------------
212    -- Read_Withed_ALIs --
213    ----------------------
214
215    procedure Read_Withed_ALIs
216      (Id            : ALI_Id;
217       Ignore_Errors : Boolean := False)
218    is
219       Afile  : File_Name_Type;
220       Text   : Text_Buffer_Ptr;
221       Idread : ALI_Id;
222
223    begin
224       --  Process all dependent units
225
226       for U in ALIs.Table (Id).First_Unit .. ALIs.Table (Id).Last_Unit loop
227          for
228            W in Units.Table (U).First_With .. Units.Table (U).Last_With
229          loop
230             Afile := Withs.Table (W).Afile;
231
232             --  Only process if not a generic (Afile /= No_File) and if
233             --  file has not been processed already.
234
235             if Afile /= No_File
236               and then Get_Name_Table_Info (Afile) = 0
237             then
238                Text := Read_Library_Info (Afile);
239
240                --  Unless Ignore_Errors is true, return with an error if source
241                --  cannot be found. We used to skip this check when we did not
242                --  compile library generics separately, but we now always do,
243                --  so there is no special case here anymore.
244
245                if Text = null then
246
247                   if not Ignore_Errors then
248                      Error_Msg_File_1 := Afile;
249                      Error_Msg_File_2 := Withs.Table (W).Sfile;
250                      Error_Msg ("{ not found, { must be compiled");
251                      Set_Name_Table_Info (Afile, Int (No_Unit_Id));
252                      return;
253                   end if;
254
255                else
256                   --  Enter in ALIs table
257
258                   Idread :=
259                     Scan_ALI
260                       (F         => Afile,
261                        T         => Text,
262                        Ignore_ED => False,
263                        Err       => False);
264
265                   Free (Text);
266
267                   if ALIs.Table (Idread).Compile_Errors
268                     and then not Ignore_Errors
269                   then
270                      Error_Msg_File_1 := Withs.Table (W).Sfile;
271                      Error_Msg ("{ had errors, must be fixed, and recompiled");
272                      Set_Name_Table_Info (Afile, Int (No_Unit_Id));
273
274                   elsif ALIs.Table (Idread).No_Object
275                     and then not Ignore_Errors
276                   then
277                      Error_Msg_File_1 := Withs.Table (W).Sfile;
278                      Error_Msg ("{ must be recompiled");
279                      Set_Name_Table_Info (Afile, Int (No_Unit_Id));
280                   end if;
281
282                   --  If the Unit is an Interface to a Stand-Alone Library,
283                   --  set the Interface flag in the Withs table, so that its
284                   --  dependant are not considered for elaboration order.
285
286                   if ALIs.Table (Idread).SAL_Interface then
287                      Withs.Table (W).SAL_Interface := True;
288                      Interface_Library_Unit := True;
289
290                      --  Set the entry in the Interfaces hash table, so that
291                      --  other units that import this unit will set the flag
292                      --  in their entry in the Withs table.
293
294                      Interfaces.Set (Afile, True);
295
296                   else
297                      --  Otherwise, recurse to get new dependents
298
299                      Read_Withed_ALIs (Idread);
300                   end if;
301                end if;
302
303             --  If the ALI file has already been processed and is an interface,
304             --  set the flag in the entry of the Withs table.
305
306             elsif Interface_Library_Unit and then Interfaces.Get (Afile) then
307                Withs.Table (W).SAL_Interface := True;
308             end if;
309          end loop;
310       end loop;
311    end Read_Withed_ALIs;
312
313    ----------------------
314    -- Set_Source_Table --
315    ----------------------
316
317    procedure Set_Source_Table (A : ALI_Id) is
318       F     : File_Name_Type;
319       S     : Source_Id;
320       Stamp : Time_Stamp_Type;
321
322    begin
323       Sdep_Loop : for D in
324         ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
325       loop
326          F := Sdep.Table (D).Sfile;
327
328          if F /= No_File then
329
330             --  If this is the first time we are seeing this source file,
331             --  then make a new entry in the source table.
332
333             if Get_Name_Table_Info (F) = 0 then
334                Source.Increment_Last;
335                S := Source.Last;
336                Set_Name_Table_Info (F, Int (S));
337                Source.Table (S).Sfile := F;
338                Source.Table (S).All_Timestamps_Match := True;
339
340                --  Initialize checksum fields
341
342                Source.Table (S).Checksum := Sdep.Table (D).Checksum;
343                Source.Table (S).All_Checksums_Match := True;
344
345                --  In check source files mode, try to get time stamp from file
346
347                if Opt.Check_Source_Files then
348                   Stamp := Source_File_Stamp (F);
349
350                   --  If we got the stamp, then set the stamp in the source
351                   --  table entry and mark it as set from the source so that
352                   --  it does not get subsequently changed.
353
354                   if Stamp (Stamp'First) /= ' ' then
355                      Source.Table (S).Stamp := Stamp;
356                      Source.Table (S).Source_Found := True;
357
358                   --  If we could not find the file, then the stamp is set
359                   --  from the dependency table entry (to be possibly reset
360                   --  if we find a later stamp in subsequent processing)
361
362                   else
363                      Source.Table (S).Stamp := Sdep.Table (D).Stamp;
364                      Source.Table (S).Source_Found := False;
365
366                      --  In All_Sources mode, flag error of file not found
367
368                      if Opt.All_Sources then
369                         Error_Msg_File_1 := F;
370                         Error_Msg ("cannot locate {");
371                      end if;
372                   end if;
373
374                --  First time for this source file, but Check_Source_Files
375                --  is off, so simply initialize the stamp from the Sdep entry
376
377                else
378                   Source.Table (S).Source_Found := False;
379                   Source.Table (S).Stamp := Sdep.Table (D).Stamp;
380                end if;
381
382             --  Here if this is not the first time for this source file,
383             --  so that the source table entry is already constructed.
384
385             else
386                S := Source_Id (Get_Name_Table_Info (F));
387
388                --  Update checksum flag
389
390                if not Checksums_Match
391                         (Sdep.Table (D).Checksum, Source.Table (S).Checksum)
392                then
393                   Source.Table (S).All_Checksums_Match := False;
394                end if;
395
396                --  Check for time stamp mismatch
397
398                if Sdep.Table (D).Stamp /= Source.Table (S).Stamp then
399                   Source.Table (S).All_Timestamps_Match := False;
400
401                   --  When we have a time stamp mismatch, we go look for the
402                   --  source file even if Check_Source_Files is false, since
403                   --  if we find it, then we can use it to resolve which of the
404                   --  two timestamps in the ALI files is likely to be correct.
405
406                   if not Check_Source_Files then
407                      Stamp := Source_File_Stamp (F);
408
409                      if Stamp (Stamp'First) /= ' ' then
410                         Source.Table (S).Stamp := Stamp;
411                         Source.Table (S).Source_Found := True;
412                      end if;
413                   end if;
414
415                   --  If the stamp in the source table entry was set from the
416                   --  source file, then we do not change it (the stamp in the
417                   --  source file is always taken as the "right" one).
418
419                   if Source.Table (S).Source_Found then
420                      null;
421
422                   --  Otherwise, we have no source file available, so we guess
423                   --  that the later of the two timestamps is the right one.
424                   --  Note that this guess only affects which error messages
425                   --  are issued later on, not correct functionality.
426
427                   else
428                      if Sdep.Table (D).Stamp > Source.Table (S).Stamp then
429                         Source.Table (S).Stamp := Sdep.Table (D).Stamp;
430                      end if;
431                   end if;
432                end if;
433             end if;
434
435             --  Set the checksum value in the source table
436
437             S := Source_Id (Get_Name_Table_Info (F));
438             Source.Table (S).Checksum := Sdep.Table (D).Checksum;
439          end if;
440
441       end loop Sdep_Loop;
442    end Set_Source_Table;
443
444    ----------------------
445    -- Set_Source_Table --
446    ----------------------
447
448    procedure Set_Source_Table is
449    begin
450       for A in ALIs.First .. ALIs.Last loop
451          Set_Source_Table (A);
452       end loop;
453    end Set_Source_Table;
454
455    -------------------------
456    -- Time_Stamp_Mismatch --
457    -------------------------
458
459    function Time_Stamp_Mismatch
460      (A         : ALI_Id;
461       Read_Only : Boolean := False) return File_Name_Type
462    is
463       Src : Source_Id;
464       --  Source file Id for the current Sdep entry
465
466    begin
467       for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
468          Src := Source_Id (Get_Name_Table_Info (Sdep.Table (D).Sfile));
469
470          if Opt.Minimal_Recompilation
471            and then Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
472          then
473             --  If minimal recompilation is in action, replace the stamp
474             --  of the source file in the table if checksums match.
475
476             --  ??? It is probably worth updating the ALI file with a new
477             --  field to avoid recomputing it each time.
478
479             if Checksums_Match
480                  (Get_File_Checksum (Sdep.Table (D).Sfile),
481                   Source.Table (Src).Checksum)
482             then
483                if Verbose_Mode then
484                   Write_Str ("   ");
485                   Write_Str (Get_Name_String (Sdep.Table (D).Sfile));
486                   Write_Str (": up to date, different timestamps " &
487                              "but same checksum");
488                   Write_Eol;
489                end if;
490
491                Sdep.Table (D).Stamp := Source.Table (Src).Stamp;
492             end if;
493
494          end if;
495
496          if (not Read_Only) or else Source.Table (Src).Source_Found then
497             if not Source.Table (Src).Source_Found
498               or else Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
499             then
500                --  If -dt debug flag set, output time stamp found/expected
501
502                if Source.Table (Src).Source_Found and then Debug_Flag_T then
503                   Write_Str ("Source: """);
504                   Get_Name_String (Sdep.Table (D).Sfile);
505                   Write_Str (Name_Buffer (1 .. Name_Len));
506                   Write_Line ("""");
507
508                   Write_Str ("   time stamp expected: ");
509                   Write_Line (String (Sdep.Table (D).Stamp));
510
511                   Write_Str ("      time stamp found: ");
512                   Write_Line (String (Source.Table (Src).Stamp));
513                end if;
514
515                --  Return the source file
516
517                return Source.Table (Src).Sfile;
518             end if;
519          end if;
520       end loop;
521
522       return No_File;
523    end Time_Stamp_Mismatch;
524
525 end ALI.Util;