OSDN Git Service

New Language: Ada
[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 --                            $Revision: 1.7 $
10 --                                                                          --
11 --          Copyright (C) 1992-2000 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 with Binderr; use Binderr;
30 with Namet;   use Namet;
31 with Opt;     use Opt;
32 with Osint;   use Osint;
33
34 package body ALI.Util is
35
36    -----------------------
37    -- Local Subprograms --
38    -----------------------
39
40    procedure Accumulate_Checksum (C : Character; Csum : in out Word);
41    pragma Inline (Accumulate_Checksum);
42    --  This routine accumulates the checksum given character C. During the
43    --  scanning of a source file, this routine is called with every character
44    --  in the source, excluding blanks, and all control characters (except
45    --  that ESC is included in the checksum). Upper case letters not in string
46    --  literals are folded by the caller. See Sinput spec for the documentation
47    --  of the checksum algorithm. Note: checksum values are only used if we
48    --  generate code, so it is not necessary to worry about making the right
49    --  sequence of calls in any error situation.
50
51    -------------------------
52    -- Accumulate_Checksum --
53    -------------------------
54
55    procedure Accumulate_Checksum (C : Character; Csum : in out Word) is
56    begin
57       Csum := Csum + Csum + Character'Pos (C);
58
59       if Csum > 16#8000_0000# then
60          Csum := (Csum + 1) and 16#7FFF_FFFF#;
61       end if;
62    end Accumulate_Checksum;
63
64    -----------------------
65    -- Get_File_Checksum --
66    -----------------------
67
68    function Get_File_Checksum (Fname : Name_Id) return Word is
69       Src  : Source_Buffer_Ptr;
70       Hi   : Source_Ptr;
71       Csum : Word;
72       Ptr  : Source_Ptr;
73
74       Bad : exception;
75       --  Raised if file not found, or file format error
76
77       use ASCII;
78       --  Make control characters visible
79
80       procedure Free_Source;
81       --  Free source file buffer
82
83       procedure Free_Source is
84          procedure free (Arg : Source_Buffer_Ptr);
85          pragma Import (C, free, "free");
86
87       begin
88          free (Src);
89       end Free_Source;
90
91    --  Start of processing for Get_File_Checksum
92
93    begin
94       Read_Source_File (Fname, 0, Hi, Src);
95
96       --  If we cannot find the file, then return an impossible checksum,
97       --  impossible becaues checksums have the high order bit zero, so
98       --  that checksums do not match.
99
100       if Src = null then
101          raise Bad;
102       end if;
103
104       Csum := 0;
105       Ptr := 0;
106
107       loop
108          case Src (Ptr) is
109
110             --  Spaces and formatting information are ignored in checksum
111
112             when ' ' | CR | LF | VT | FF | HT =>
113                Ptr := Ptr + 1;
114
115             --  EOF is ignored unless it is the last character
116
117             when EOF =>
118                if Ptr = Hi then
119                   Free_Source;
120                   return Csum;
121                else
122                   Ptr := Ptr + 1;
123                end if;
124
125             --  Non-blank characters that are included in the checksum
126
127             when '#' | '&' | '*' | ':' | '(' | ',' | '.' | '=' | '>' |
128                  '<' | ')' | '/' | ';' | '|' | '!' | '+' | '_' |
129                  '0' .. '9' | 'a' .. 'z'
130             =>
131                Accumulate_Checksum (Src (Ptr), Csum);
132                Ptr := Ptr + 1;
133
134             --  Upper case letters, fold to lower case
135
136             when 'A' .. 'Z' =>
137                Accumulate_Checksum
138                  (Character'Val (Character'Pos (Src (Ptr)) + 32), Csum);
139                Ptr := Ptr + 1;
140
141             --  Left bracket, really should do wide character thing here,
142             --  but for now, don't bother.
143
144             when '[' =>
145                raise Bad;
146
147             --  Minus, could be comment
148
149             when '-' =>
150                if Src (Ptr + 1) = '-' then
151                   Ptr := Ptr + 2;
152
153                   while Src (Ptr) >= ' ' or else Src (Ptr) = HT loop
154                      Ptr := Ptr + 1;
155                   end loop;
156
157                else
158                   Accumulate_Checksum ('-', Csum);
159                   Ptr := Ptr + 1;
160                end if;
161
162             --  String delimited by double quote
163
164             when '"' =>
165                Accumulate_Checksum ('"', Csum);
166
167                loop
168                   Ptr := Ptr + 1;
169                   exit when Src (Ptr) = '"';
170
171                   if Src (Ptr) < ' ' then
172                      raise Bad;
173                   end if;
174
175                   Accumulate_Checksum (Src (Ptr), Csum);
176                end loop;
177
178                Accumulate_Checksum ('"', Csum);
179                Ptr := Ptr + 1;
180
181             --  String delimited by percent
182
183             when '%' =>
184                Accumulate_Checksum ('%', Csum);
185
186                loop
187                   Ptr := Ptr + 1;
188                   exit when Src (Ptr) = '%';
189
190                   if Src (Ptr) < ' ' then
191                      raise Bad;
192                   end if;
193
194                   Accumulate_Checksum (Src (Ptr), Csum);
195                end loop;
196
197                Accumulate_Checksum ('%', Csum);
198                Ptr := Ptr + 1;
199
200             --  Quote, could be character constant
201
202             when ''' =>
203                Accumulate_Checksum (''', Csum);
204
205                if Src (Ptr + 2) = ''' then
206                   Accumulate_Checksum (Src (Ptr + 1), Csum);
207                   Accumulate_Checksum (''', Csum);
208                   Ptr := Ptr + 3;
209
210                --  Otherwise assume attribute char. We should deal with wide
211                --  character cases here, but that's hard, so forget it.
212
213                else
214                   Ptr := Ptr + 1;
215                end if;
216
217             --  Upper half character, more to be done here, we should worry
218             --  about folding Latin-1, folding other character sets, and
219             --  dealing with the nasty case of upper half wide encoding.
220
221             when Upper_Half_Character =>
222                Accumulate_Checksum (Src (Ptr), Csum);
223                Ptr := Ptr + 1;
224
225             --  Escape character, we should do the wide character thing here,
226             --  but for now, do not bother.
227
228             when ESC =>
229                raise Bad;
230
231             --  Invalid control characters
232
233             when NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS  | SO  |
234                  SI  | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN |
235                  EM  | FS  | GS  | RS  | US  | DEL
236             =>
237                raise Bad;
238
239             --  Invalid graphic characters
240
241             when '$' | '?' | '@' | '`' | '\' |
242                  '^' | '~' | ']' | '{' | '}'
243             =>
244                raise Bad;
245
246          end case;
247       end loop;
248
249    exception
250       when Bad =>
251          Free_Source;
252          return 16#FFFF_FFFF#;
253
254    end Get_File_Checksum;
255
256    ---------------------------
257    -- Initialize_ALI_Source --
258    ---------------------------
259
260    procedure Initialize_ALI_Source is
261    begin
262       --  When (re)initializing ALI data structures the ALI user expects to
263       --  get a fresh set of data structures. Thus we first need to erase the
264       --  marks put in the name table by the previous set of ALI routine calls.
265       --  This loop is empty and harmless the first time in.
266
267       for J in Source.First .. Source.Last loop
268          Set_Name_Table_Info (Source.Table (J).Sfile, 0);
269          Source.Table (J).Source_Found := False;
270       end loop;
271
272       Source.Init;
273    end Initialize_ALI_Source;
274
275    --------------
276    -- Read_ALI --
277    --------------
278
279    procedure Read_ALI (Id : ALI_Id) is
280       Afile  : File_Name_Type;
281       Text   : Text_Buffer_Ptr;
282       Idread : ALI_Id;
283
284    begin
285       for I in ALIs.Table (Id).First_Unit .. ALIs.Table (Id).Last_Unit loop
286          for J in Units.Table (I).First_With .. Units.Table (I).Last_With loop
287
288             Afile := Withs.Table (J).Afile;
289
290             --  Only process if not a generic (Afile /= No_File) and if
291             --  file has not been processed already.
292
293             if Afile /= No_File and then Get_Name_Table_Info (Afile) = 0 then
294
295                Text := Read_Library_Info (Afile);
296
297                if Text = null then
298                   Error_Msg_Name_1 := Afile;
299                   Error_Msg_Name_2 := Withs.Table (J).Sfile;
300                   Error_Msg ("% not found, % must be compiled");
301                   Set_Name_Table_Info (Afile, Int (No_Unit_Id));
302                   return;
303                end if;
304
305                Idread :=
306                  Scan_ALI
307                    (F         => Afile,
308                     T         => Text,
309                     Ignore_ED => Force_RM_Elaboration_Order,
310                     Err       => False);
311
312                Free (Text);
313
314                if ALIs.Table (Idread).Compile_Errors then
315                   Error_Msg_Name_1 := Withs.Table (J).Sfile;
316                   Error_Msg ("% had errors, must be fixed, and recompiled");
317                   Set_Name_Table_Info (Afile, Int (No_Unit_Id));
318
319                elsif ALIs.Table (Idread).No_Object then
320                   Error_Msg_Name_1 := Withs.Table (J).Sfile;
321                   Error_Msg ("% must be recompiled");
322                   Set_Name_Table_Info (Afile, Int (No_Unit_Id));
323                end if;
324
325                --  Recurse to get new dependents
326
327                Read_ALI (Idread);
328             end if;
329          end loop;
330       end loop;
331
332    end Read_ALI;
333
334    ----------------------
335    -- Set_Source_Table --
336    ----------------------
337
338    procedure Set_Source_Table (A : ALI_Id) is
339       F     : File_Name_Type;
340       S     : Source_Id;
341       Stamp : Time_Stamp_Type;
342
343    begin
344       Sdep_Loop : for D in
345         ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
346       loop
347          F := Sdep.Table (D).Sfile;
348
349          --  If this is the first time we are seeing this source file,
350          --  then make a new entry in the source table.
351
352          if Get_Name_Table_Info (F) = 0 then
353             Source.Increment_Last;
354             S := Source.Last;
355             Set_Name_Table_Info (F, Int (S));
356             Source.Table (S).Sfile := F;
357             Source.Table (S).All_Timestamps_Match := True;
358
359             --  Initialize checksum fields
360
361             Source.Table (S).Checksum := Sdep.Table (D).Checksum;
362             Source.Table (S).All_Checksums_Match := True;
363
364             --  In check source files mode, try to get time stamp from file
365
366             if Opt.Check_Source_Files then
367                Stamp := Source_File_Stamp (F);
368
369                --  If we got the stamp, then set the stamp in the source
370                --  table entry and mark it as set from the source so that
371                --  it does not get subsequently changed.
372
373                if Stamp (Stamp'First) /= ' ' then
374                   Source.Table (S).Stamp := Stamp;
375                   Source.Table (S).Source_Found := True;
376
377                --  If we could not find the file, then the stamp is set
378                --  from the dependency table entry (to be possibly reset
379                --  if we find a later stamp in subsequent processing)
380
381                else
382                   Source.Table (S).Stamp := Sdep.Table (D).Stamp;
383                   Source.Table (S).Source_Found := False;
384
385                   --  In All_Sources mode, flag error of file not found
386
387                   if Opt.All_Sources then
388                      Error_Msg_Name_1 := F;
389                      Error_Msg ("cannot locate %");
390                   end if;
391                end if;
392
393             --  First time for this source file, but Check_Source_Files
394             --  is off, so simply initialize the stamp from the Sdep entry
395
396             else
397                Source.Table (S).Source_Found := False;
398                Source.Table (S).Stamp := Sdep.Table (D).Stamp;
399             end if;
400
401          --  Here if this is not the first time for this source file,
402          --  so that the source table entry is already constructed.
403
404          else
405             S := Source_Id (Get_Name_Table_Info (F));
406
407             --  Update checksum flag
408
409             if Sdep.Table (D).Checksum /= Source.Table (S).Checksum then
410                Source.Table (S).All_Checksums_Match := False;
411             end if;
412
413             --  Check for time stamp mismatch
414
415             if Sdep.Table (D).Stamp /= Source.Table (S).Stamp then
416                Source.Table (S).All_Timestamps_Match := False;
417
418                --  When we have a time stamp mismatch, we go look for the
419                --  source file even if Check_Source_Files is false, since
420                --  if we find it, then we can use it to resolve which of the
421                --  two timestamps in the ALI files is likely to be correct.
422
423                if not Check_Source_Files then
424                   Stamp := Source_File_Stamp (F);
425
426                   if Stamp (Stamp'First) /= ' ' then
427                      Source.Table (S).Stamp := Stamp;
428                      Source.Table (S).Source_Found := True;
429                   end if;
430                end if;
431
432                --  If the stamp in the source table entry was set from the
433                --  source file, then we do not change it (the stamp in the
434                --  source file is always taken as the "right" one).
435
436                if Source.Table (S).Source_Found then
437                   null;
438
439                --  Otherwise, we have no source file available, so we guess
440                --  that the later of the two timestamps is the right one.
441                --  Note that this guess only affects which error messages
442                --  are issued later on, not correct functionality.
443
444                else
445                   if Sdep.Table (D).Stamp > Source.Table (S).Stamp then
446                      Source.Table (S).Stamp := Sdep.Table (D).Stamp;
447                   end if;
448                end if;
449             end if;
450          end if;
451
452          --  Set the checksum value in the source table
453
454          S := Source_Id (Get_Name_Table_Info (F));
455          Source.Table (S).Checksum := Sdep.Table (D).Checksum;
456
457       end loop Sdep_Loop;
458
459    end Set_Source_Table;
460
461    ----------------------
462    -- Set_Source_Table --
463    ----------------------
464
465    procedure Set_Source_Table is
466    begin
467       for A in ALIs.First .. ALIs.Last loop
468          Set_Source_Table (A);
469       end loop;
470
471    end Set_Source_Table;
472
473    -------------------------
474    -- Time_Stamp_Mismatch --
475    -------------------------
476
477    function Time_Stamp_Mismatch (A : ALI_Id) return File_Name_Type is
478       Src : Source_Id;
479       --  Source file Id for the current Sdep entry
480
481    begin
482       for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
483          Src := Source_Id (Get_Name_Table_Info (Sdep.Table (D).Sfile));
484
485          if Opt.Minimal_Recompilation
486            and then Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
487          then
488
489             --  If minimal recompilation is in action, replace the stamp
490             --  of the source file in the table if checksums match.
491
492             --  ??? It is probably worth updating the ALI file with a new
493             --  field to avoid recomputing it each time.
494
495             if Get_File_Checksum (Sdep.Table (D).Sfile) =
496                                              Source.Table (Src).Checksum
497             then
498                Sdep.Table (D).Stamp := Source.Table (Src).Stamp;
499             end if;
500
501          end if;
502
503          if not Source.Table (Src).Source_Found
504            or else Sdep.Table (D).Stamp /= Source.Table (Src).Stamp
505          then
506             return Source.Table (Src).Sfile;
507          end if;
508       end loop;
509
510       return No_File;
511
512    end Time_Stamp_Mismatch;
513
514 end ALI.Util;