1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             A L I . U T I L                              --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2013, 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--  This child unit provides utility data structures and procedures used
27--  for manipulation of ALI data by the gnatbind and gnatmake.
28
29package ALI.Util is
30
31   -----------------------
32   -- Source File Table --
33   -----------------------
34
35   --  A table entry is built for every source file that is in the source
36   --  dependency table of any ALI file that is part of the current program.
37
38   No_Source_Id : constant Source_Id := Source_Id'First;
39   --  Special value indicating no Source table entry
40
41   First_Source_Entry : constant Source_Id := No_Source_Id + 1;
42   --  Id of first actual entry in table
43
44   type Source_Record is record
45
46      Sfile : File_Name_Type;
47      --  Name of source file
48
49      Stamp : Time_Stamp_Type;
50      --  Time stamp value. If Check_Source_Files is set and the source
51      --  file is located, then Stamp is set from the source file. Otherwise
52      --  Stamp is set from the latest stamp value found in any of the
53      --  ALI files for the current program.
54
55      Source_Found : Boolean;
56      --  This flag is set to True if the corresponding source file was
57      --  located and the Stamp value was set from the actual source file.
58      --  It is always false if Check_Source_Files is not set.
59
60      Stamp_File : File_Name_Type;
61      --  File that Stamp came from. If Source_Found is True, then Stamp is the
62      --  timestamp of the source file, and this is the name of the source
63      --  file. If Source_Found is False, then Stamp comes from a dependency
64      --  line in an ALI file, this is the name of that ALI file. Used only in
65      --  verbose mode, for messages.
66
67      All_Timestamps_Match : Boolean;
68      --  This flag is set only if all files referencing this source file
69      --  have a matching time stamp, and also, if Source_Found is True,
70      --  then the stamp of the source file also matches. If this flag is
71      --  True, then checksums for this file are never referenced. We only
72      --  use checksums if there are time stamp mismatches.
73
74      All_Checksums_Match : Boolean;
75      --  This flag is set only if all files referencing this source file
76      --  have checksums, and if all these checksums match. If this flag
77      --  is set to True, then the binder will ignore a timestamp mismatch.
78      --  An absent checksum causes this flag to be set False, and a mismatch
79      --  of checksums also causes it to be set False. The checksum of the
80      --  actual source file (if Source_Found is True) is included only if
81      --  All_Timestamps_Match is False (since checksums are only interesting
82      --  if we have time stamp mismatches, and we want to avoid computing the
83      --  checksum of the source file if it is not needed.)
84
85      Checksum : Word;
86      --  If no dependency line has a checksum for this source file (i.e. the
87      --  corresponding entries in the source dependency records all have the
88      --  Checksum_Present flag set False), then this field is undefined. If
89      --  at least one dependency entry has a checksum present, then this
90      --  field contains one of the possible checksum values that has been
91      --  seen. This is used to set All_Checksums_Match properly.
92
93   end record;
94
95   package Source is new Table.Table (
96     Table_Component_Type => Source_Record,
97     Table_Index_Type     => Source_Id,
98     Table_Low_Bound      => First_Source_Entry,
99     Table_Initial        => 1000,
100     Table_Increment      => 200,
101     Table_Name           => "Source");
102
103   procedure Initialize_ALI_Source;
104   --  Initialize Source table
105
106   --------------------------------------------------
107   -- Subprograms for Manipulating ALI Information --
108   --------------------------------------------------
109
110   procedure Read_Withed_ALIs
111     (Id            : ALI_Id;
112      Ignore_Errors : Boolean := False);
113   --  Process an ALI file which has been read and scanned by looping through
114   --  all withed units in the ALI file, checking if they have been processed.
115   --  Each unit that has not yet been processed will be read, scanned, and
116   --  processed recursively. If Ignore_Errors is True, then failure to read an
117   --  ALI file is not reported as an error, and scanning continues with other
118   --  ALI files.
119
120   procedure Set_Source_Table (A : ALI_Id);
121   --  Build source table entry corresponding to the ALI file whose id is A
122
123   procedure Set_Source_Table;
124   --  Build the entire source table
125
126   function Time_Stamp_Mismatch
127     (A         : ALI_Id;
128      Read_Only : Boolean := False) return File_Name_Type;
129   --  Looks in the Source_Table and checks time stamp mismatches between
130   --  the sources there and the sources in the Sdep section of ali file whose
131   --  id is A. If no time stamp mismatches are found No_File is returned.
132   --  Otherwise return the first file for which there is a mismatch.
133   --  Note that in check source files mode (Check_Source_Files = True), the
134   --  time stamp in the Source_Table should be the actual time stamp of the
135   --  source files. In minimal recompilation mode (Minimal_Recompilation set
136   --  to True, no mismatch is found if the file's timestamp has not changed.
137   --  If Read_Only is True, missing sources are not considered.
138
139   --------------------------------------------
140   -- Subprograms for manipulating checksums --
141   --------------------------------------------
142
143   Checksum_Error : constant Word := 16#FFFF_FFFF#;
144   --  This value is used to indicate an error in computing the checksum.
145   --  When comparing checksums for smart recompilation, the CRC_Error
146   --  value is never considered to match. This could possibly result
147   --  in a false negative, but that is never harmful, it just means
148   --  that in unusual cases an unnecessary recompilation occurs.
149
150   function Get_File_Checksum (Fname : File_Name_Type) return Word;
151   --  Compute checksum for the given file. As far as possible, this circuit
152   --  computes exactly the same value computed by the compiler, but it does
153   --  not matter if it gets it wrong in marginal cases, since the only result
154   --  is to miss some smart recompilation cases, correct functioning is not
155   --  affected by a miscomputation. Returns Checksum_Error if the file is
156   --  missing or has an error.
157
158   function Checksums_Match (Checksum1, Checksum2 : Word) return Boolean;
159   pragma Inline (Checksums_Match);
160   --  Returns True if Checksum1 and Checksum2 have the same value and are
161   --  not equal to Checksum_Error, returns False in all other cases. This
162   --  routine must always be used to compare for checksum equality, to
163   --  ensure that the case of Checksum_Error is handled properly.
164
165end ALI.Util;
166