1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 2001, Free Software Foundation, Inc. --
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. --
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). --
27 ------------------------------------------------------------------------------
29 with Namet; use Namet;
30 with Osint; use Osint;
31 with Output; use Output;
34 with Unchecked_Conversion;
40 subtype Big_String is String (Positive);
41 type Big_String_Ptr is access all Big_String;
43 function To_Big_String_Ptr is new Unchecked_Conversion
44 (Source_Buffer_Ptr, Big_String_Ptr);
46 package File_Mapping is new Table.Table (
47 Table_Component_Type => File_Name_Type,
48 Table_Index_Type => Int,
50 Table_Initial => 1_000,
51 Table_Increment => 1_000,
52 Table_Name => "Fmap.File_Mapping");
53 -- Mapping table to map unit names to file names.
55 package Path_Mapping is new Table.Table (
56 Table_Component_Type => File_Name_Type,
57 Table_Index_Type => Int,
59 Table_Initial => 1_000,
60 Table_Increment => 1_000,
61 Table_Name => "Fmap.Path_Mapping");
62 -- Mapping table to map file names to path names
64 type Header_Num is range 0 .. 1_000;
66 function Hash (F : Unit_Name_Type) return Header_Num;
67 -- Function used to compute hash of unit name
69 No_Entry : constant Int := -1;
70 -- Signals no entry in following table
72 package Unit_Hash_Table is new GNAT.HTable.Simple_HTable (
73 Header_Num => Header_Num,
75 No_Element => No_Entry,
76 Key => Unit_Name_Type,
79 -- Hash table to map unit names to file names. Used in conjunction with
80 -- table File_Mapping above.
82 package File_Hash_Table is new GNAT.HTable.Simple_HTable (
83 Header_Num => Header_Num,
85 No_Element => No_Entry,
86 Key => File_Name_Type,
89 -- Hash table to map file names to path names. Used in conjunction with
90 -- table Path_Mapping above.
96 procedure Add_To_File_Map
97 (Unit_Name : Unit_Name_Type;
98 File_Name : File_Name_Type;
99 Path_Name : File_Name_Type)
102 File_Mapping.Increment_Last;
103 Unit_Hash_Table.Set (Unit_Name, File_Mapping.Last);
104 File_Mapping.Table (File_Mapping.Last) := File_Name;
105 Path_Mapping.Increment_Last;
106 File_Hash_Table.Set (File_Name, Path_Mapping.Last);
107 Path_Mapping.Table (Path_Mapping.Last) := Path_Name;
114 function Hash (F : Unit_Name_Type) return Header_Num is
116 return Header_Num (Int (F) rem Header_Num'Range_Length);
123 procedure Initialize (File_Name : String) is
124 Src : Source_Buffer_Ptr;
132 Uname : Unit_Name_Type;
136 procedure Empty_Tables;
137 -- Remove all entries in case of incorrect mapping file
140 -- Get a line from the mapping file
142 procedure Report_Truncated;
143 -- Report a warning when the mapping file is truncated
144 -- (number of lines is not a multiple of 3).
150 procedure Empty_Tables is
152 Unit_Hash_Table.Reset;
153 File_Hash_Table.Reset;
154 Path_Mapping.Set_Last (0);
155 File_Mapping.Set_Last (0);
162 procedure Get_Line is
168 -- If not at the end of file, skip the end of line
171 and then (SP (Deb) = CR
172 or else SP (Deb) = LF
173 or else SP (Deb) = EOF)
178 -- If not at the end of line, find the end of this new line
180 if Deb < SP'Last and then SP (Deb) /= EOF then
184 and then SP (Fin + 1) /= CR
185 and then SP (Fin + 1) /= LF
186 and then SP (Fin + 1) /= EOF
194 ----------------------
195 -- Report_Truncated --
196 ----------------------
198 procedure Report_Truncated is
200 Write_Str ("warning: mapping file """);
201 Write_Str (File_Name);
202 Write_Line (""" is truncated");
203 end Report_Truncated;
205 -- Start of procedure Initialize
208 Name_Len := File_Name'Length;
209 Name_Buffer (1 .. Name_Len) := File_Name;
210 Read_Source_File (Name_Enter, 0, Hi, Src, Config);
213 Write_Str ("warning: could not read mapping file """);
214 Write_Str (File_Name);
218 BS := To_Big_String_Ptr (Src);
219 SP := BS (1 .. Natural (Hi))'Unrestricted_Access;
226 -- Exit if end of file has been reached
230 pragma Assert (Fin >= Deb + 2);
231 pragma Assert (SP (Fin - 1) = '%');
232 pragma Assert (SP (Fin) = 's' or else SP (Fin) = 'b');
234 Name_Len := Fin - Deb + 1;
235 Name_Buffer (1 .. Name_Len) := SP (Deb .. Fin);
242 -- If end of line has been reached, file is truncated
250 Name_Len := Fin - Deb + 1;
251 Name_Buffer (1 .. Name_Len) := SP (Deb .. Fin);
258 -- If end of line has been reached, file is truncated
266 Name_Len := Fin - Deb + 1;
267 Name_Buffer (1 .. Name_Len) := SP (Deb .. Fin);
270 -- Check for duplicate entries
272 if Unit_Hash_Table.Get (Uname) /= No_Entry then
273 Write_Str ("warning: duplicate entry """);
274 Write_Str (Get_Name_String (Uname));
275 Write_Str (""" in mapping file """);
276 Write_Str (File_Name);
282 if File_Hash_Table.Get (Fname) /= No_Entry then
283 Write_Str ("warning: duplicate entry """);
284 Write_Str (Get_Name_String (Fname));
285 Write_Str (""" in mapping file """);
286 Write_Str (File_Name);
292 -- Add the mappings for this unit name
294 Add_To_File_Map (Uname, Fname, Pname);
299 ----------------------
300 -- Mapped_File_Name --
301 ----------------------
303 function Mapped_File_Name (Unit : Unit_Name_Type) return File_Name_Type is
304 The_Index : constant Int := Unit_Hash_Table.Get (Unit);
307 if The_Index = No_Entry then
310 return File_Mapping.Table (The_Index);
312 end Mapped_File_Name;
314 ----------------------
315 -- Mapped_Path_Name --
316 ----------------------
318 function Mapped_Path_Name (File : File_Name_Type) return File_Name_Type is
319 Index : Int := No_Entry;
322 Index := File_Hash_Table.Get (File);
324 if Index = No_Entry then
327 return Path_Mapping.Table (Index);
329 end Mapped_Path_Name;