1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Debug
; use Debug
;
31 with Osint
; use Osint
;
33 with Targparm
; use Targparm
;
34 with Uname
; use Uname
;
35 with Widechar
; use Widechar
;
39 package body Fname
.UF
is
41 --------------------------------------------------------
42 -- Declarations for Handling Source_File_Name pragmas --
43 --------------------------------------------------------
45 type SFN_Entry
is record
46 U
: Unit_Name_Type
; -- Unit name
47 F
: File_Name_Type
; -- Spec/Body file name
48 Index
: Nat
; -- Index from SFN pragma (0 if none)
50 -- Record single Unit_Name type call to Set_File_Name
52 package SFN_Table
is new Table
.Table
(
53 Table_Component_Type
=> SFN_Entry
,
54 Table_Index_Type
=> Int
,
56 Table_Initial
=> Alloc
.SFN_Table_Initial
,
57 Table_Increment
=> Alloc
.SFN_Table_Increment
,
58 Table_Name
=> "SFN_Table");
59 -- Table recording all Unit_Name calls to Set_File_Name
61 type SFN_Header_Num
is range 0 .. 100;
63 function SFN_Hash
(F
: Unit_Name_Type
) return SFN_Header_Num
;
64 -- Compute hash index for use by Simple_HTable
66 No_Entry
: constant Int
:= -1;
67 -- Signals no entry in following table
69 package SFN_HTable
is new GNAT
.HTable
.Simple_HTable
(
70 Header_Num
=> SFN_Header_Num
,
72 No_Element
=> No_Entry
,
73 Key
=> Unit_Name_Type
,
76 -- Hash table allowing rapid access to SFN_Table, the element value
77 -- is an index into this table.
79 type SFN_Pattern_Entry
is record
80 Pat
: String_Ptr
; -- File name pattern (with asterisk in it)
81 Typ
: Character; -- 'S'/'B'/'U' for spec/body/subunit
82 Dot
: String_Ptr
; -- Dot_Separator string
83 Cas
: Casing_Type
; -- Upper/Lower/Mixed
85 -- Records single call to Set_File_Name_Patterm
87 package SFN_Patterns
is new Table
.Table
(
88 Table_Component_Type
=> SFN_Pattern_Entry
,
89 Table_Index_Type
=> Int
,
92 Table_Increment
=> 100,
93 Table_Name
=> "SFN_Patterns");
94 -- Table recording all calls to Set_File_Name_Pattern. Note that the
95 -- first two entries are set to represent the standard GNAT rules
98 -----------------------
99 -- File_Name_Of_Body --
100 -----------------------
102 function File_Name_Of_Body
(Name
: Name_Id
) return File_Name_Type
is
104 Get_Name_String
(Name
);
105 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 2) := "%b";
106 Name_Len
:= Name_Len
+ 2;
107 return Get_File_Name
(Name_Enter
, Subunit
=> False);
108 end File_Name_Of_Body
;
110 -----------------------
111 -- File_Name_Of_Spec --
112 -----------------------
114 function File_Name_Of_Spec
(Name
: Name_Id
) return File_Name_Type
is
116 Get_Name_String
(Name
);
117 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 2) := "%s";
118 Name_Len
:= Name_Len
+ 2;
119 return Get_File_Name
(Name_Enter
, Subunit
=> False);
120 end File_Name_Of_Spec
;
122 ----------------------------
123 -- Get_Expected_Unit_Type --
124 ----------------------------
126 function Get_Expected_Unit_Type
127 (Fname
: File_Name_Type
) return Expected_Unit_Type
130 -- In syntax checking only mode or in multiple unit per file mode,
131 -- there can be more than one unit in a file, so the file name is
132 -- not a useful guide to the nature of the unit.
134 if Operating_Mode
= Check_Syntax
135 or else Multiple_Unit_Index
/= 0
140 -- Search the file mapping table, if we find an entry for this
141 -- file we know whether it is a spec or a body.
143 for J
in SFN_Table
.First
.. SFN_Table
.Last
loop
144 if Fname
= SFN_Table
.Table
(J
).F
then
145 if Is_Body_Name
(SFN_Table
.Table
(J
).U
) then
153 -- If no entry in file naming table, assume .ads/.adb for spec/body
154 -- and return unknown if we have neither of these two cases.
156 Get_Name_String
(Fname
);
159 if Name_Buffer
(Name_Len
- 3 .. Name_Len
) = ".ads" then
161 elsif Name_Buffer
(Name_Len
- 3 .. Name_Len
) = ".adb" then
167 end Get_Expected_Unit_Type
;
173 function Get_File_Name
174 (Uname
: Unit_Name_Type
;
176 May_Fail
: Boolean := False) return File_Name_Type
178 Unit_Char
: Character;
179 -- Set to 's' or 'b' for spec or body or to 'u' for a subunit
181 Unit_Char_Search
: Character;
182 -- Same as Unit_Char, except that in the case of 'u' for a subunit,
183 -- we set Unit_Char_Search to 'b' if we do not find a subunit match.
187 Pname
: File_Name_Type
:= No_File
;
188 Fname
: File_Name_Type
:= No_File
;
189 -- Path name and File name for mapping
192 -- Null or error name means that some previous error occurred
193 -- This is an unrecoverable error, so signal it.
195 if Uname
in Error_Unit_Name_Or_No_Unit_Name
then
196 raise Unrecoverable_Error
;
199 -- Look in the map from unit names to file names
201 Fname
:= Mapped_File_Name
(Uname
);
203 -- If the unit name is already mapped, return the corresponding
204 -- file name from the map.
206 if Fname
/= No_File
then
210 -- If there is a specific SFN pragma, return the corresponding file name
212 N
:= SFN_HTable
.Get
(Uname
);
214 if N
/= No_Entry
then
215 return SFN_Table
.Table
(N
).F
;
218 -- Here for the case where the name was not found in the table
220 Get_Decoded_Name_String
(Uname
);
222 -- A special fudge, normally we don't have operator symbols present,
223 -- since it is always an error to do so. However, if we do, at this
224 -- stage it has a leading double quote.
226 -- What we do in this case is to go back to the undecoded name, which
227 -- is of the form, for example:
231 -- and build a file name that looks like:
235 -- which is bit peculiar, but we keep it that way. This means that
236 -- we avoid bombs due to writing a bad file name, and w get expected
237 -- error processing downstream, e.g. a compilation following gnatchop.
239 if Name_Buffer
(1) = '"' then
240 Get_Name_String
(Uname
);
241 Name_Len
:= Name_Len
+ 1;
242 Name_Buffer
(Name_Len
) := Name_Buffer
(Name_Len
- 1);
243 Name_Buffer
(Name_Len
- 1) := Name_Buffer
(Name_Len
- 2);
244 Name_Buffer
(Name_Len
- 2) := '_';
245 Name_Buffer
(1) := '_';
248 -- Deal with spec or body suffix
250 Unit_Char
:= Name_Buffer
(Name_Len
);
251 pragma Assert
(Unit_Char
= 'b' or else Unit_Char
= 's');
252 pragma Assert
(Name_Len
>= 3 and then Name_Buffer
(Name_Len
- 1) = '%');
253 Name_Len
:= Name_Len
- 2;
259 -- Now we need to find the proper translation of the name
262 Uname
: constant String (1 .. Name_Len
) :=
263 Name_Buffer
(1 .. Name_Len
);
267 Fnam
: File_Name_Type
:= No_File
;
273 -- Set True for predefined file
275 function C
(N
: Natural) return Character;
276 -- Return N'th character of pattern
278 function C
(N
: Natural) return Character is
280 return SFN_Patterns
.Table
(Pent
).Pat
(N
);
283 -- Start of search through pattern table
286 -- Search pattern table to find a matching entry. In the general
287 -- case we do two complete searches. The first time through we
288 -- stop only if a matching file is found, the second time through
289 -- we accept the first match regardless. Note that there will
290 -- always be a match the second time around, because of the
291 -- default entries at the end of the table.
293 for No_File_Check
in False .. True loop
294 Unit_Char_Search
:= Unit_Char
;
297 -- The search is repeated with Unit_Char_Search set to b, if an
298 -- initial search for the subunit case fails to find any match.
300 Pent
:= SFN_Patterns
.First
;
301 while Pent
<= SFN_Patterns
.Last
loop
302 if SFN_Patterns
.Table
(Pent
).Typ
= Unit_Char_Search
then
305 -- Determine if we have a predefined file name
307 Name_Len
:= Uname
'Length;
308 Name_Buffer
(1 .. Name_Len
) := Uname
;
310 Is_Predefined_File_Name
(Renamings_Included
=> True);
312 -- Found a match, execute the pattern
314 Name_Len
:= Uname
'Length;
315 Name_Buffer
(1 .. Name_Len
) := Uname
;
317 -- Apply casing, except that we do not do this for the case
318 -- of a predefined library file. For the latter, we always
319 -- use the all lower case name, regardless of the setting.
321 if not Is_Predef
then
322 Set_Casing
(SFN_Patterns
.Table
(Pent
).Cas
);
325 -- If dot translation required do it
327 Dot
:= SFN_Patterns
.Table
(Pent
).Dot
;
328 Dotl
:= Dot
.all'Length;
330 if Dot
.all /= "." then
333 while J
<= Name_Len
loop
334 if Name_Buffer
(J
) = '.' then
337 Name_Buffer
(J
) := Dot
(Dot
'First);
340 Name_Buffer
(J
+ Dotl
.. Name_Len
+ Dotl
- 1) :=
341 Name_Buffer
(J
+ 1 .. Name_Len
);
342 Name_Buffer
(J
.. J
+ Dotl
- 1) := Dot
.all;
343 Name_Len
:= Name_Len
+ Dotl
- 1;
348 -- Skip past wide char sequences to avoid messing
349 -- with dot characters that are part of a sequence.
351 elsif Name_Buffer
(J
) = ASCII
.ESC
352 or else (Upper_Half_Encoding
354 Name_Buffer
(J
) in Upper_Half_Character
)
356 Skip_Wide
(Name_Buffer
, J
);
363 -- Here move result to right if preinsertion before *
365 Plen
:= SFN_Patterns
.Table
(Pent
).Pat
'Length;
366 for K
in 1 .. Plen
loop
369 Name_Buffer
(1 + K
- 1 .. Name_Len
+ K
- 1) :=
370 Name_Buffer
(1 .. Name_Len
);
372 for L
in 1 .. K
- 1 loop
373 Name_Buffer
(L
) := C
(L
);
376 Name_Len
:= Name_Len
+ K
- 1;
379 for L
in K
+ 1 .. Plen
loop
380 Name_Len
:= Name_Len
+ 1;
381 Name_Buffer
(Name_Len
) := C
(L
);
388 -- Execute possible crunch on constructed name. The krunch
389 -- operation excludes any extension that may be present.
393 exit when Name_Buffer
(J
) = '.';
397 -- Case of extension present
401 Ext
: constant String := Name_Buffer
(J
.. Name_Len
);
408 -- Krunch what's left
413 Integer (Maximum_File_Name_Length
),
420 (Name_Len
+ 1 .. Name_Len
+ Ext
'Length) := Ext
;
421 Name_Len
:= Name_Len
+ Ext
'Length;
424 -- Case of no extension present, straight krunch on
425 -- the entire file name.
431 Integer (Maximum_File_Name_Length
),
437 -- If we are in the second search of the table, we accept
438 -- the file name without checking, because we know that
439 -- the file does not exist, except when May_Fail is True,
440 -- in which case we return No_File.
442 if No_File_Check
then
449 -- Otherwise we check if the file exists
452 Pname
:= Find_File
(Fnam
, Source
);
454 -- If it does exist, we add it to the mappings and
455 -- return the file name.
457 if Pname
/= No_File
then
459 -- Add to mapping, so that we don't do another
460 -- path search in Find_File for this file name
461 -- and, if we use a mapping file, we are ready
462 -- to update it at the end of this compilation
463 -- for the benefit of other compilation processes.
465 Add_To_File_Map
(Get_File_Name
.Uname
, Fnam
, Pname
);
468 -- If there are only two entries, they are those of
469 -- the default GNAT naming scheme. The file does
470 -- not exist, but there is no point doing the
471 -- second search, because we will end up with the
472 -- same file name. Just return the file name.
474 elsif SFN_Patterns
.Last
= 2 then
477 -- The file does not exist, but there may be other
478 -- naming scheme. Keep on searching.
489 -- If search failed, and was for a subunit, repeat the search
490 -- with Unit_Char_Search reset to 'b', since in the normal case
491 -- we simply treat subunits as bodies.
493 if Fnam
= No_File
and then Unit_Char_Search
= 'u' then
494 Unit_Char_Search
:= 'b';
498 -- Repeat entire search in No_File_Check mode if necessary
502 -- Something is wrong if search fails completely, since the
503 -- default entries should catch all possibilities at this stage.
513 function Get_Unit_Index
(Uname
: Unit_Name_Type
) return Nat
is
514 N
: constant Int
:= SFN_HTable
.Get
(Uname
);
516 if N
/= No_Entry
then
517 return SFN_Table
.Table
(N
).Index
;
527 procedure Initialize
is
532 -- Add default entries to SFN_Patterns.Table to represent the
533 -- standard default GNAT rules for file name translation.
535 SFN_Patterns
.Append
(New_Val
=>
536 (Pat
=> new String'("*.ads"),
538 Dot => new String'("-"),
539 Cas
=> All_Lower_Case
));
541 SFN_Patterns
.Append
(New_Val
=>
542 (Pat
=> new String'("*.adb"),
544 Dot => new String'("-"),
545 Cas
=> All_Lower_Case
));
554 SFN_Table
.Locked
:= True;
562 procedure Set_File_Name
568 SFN_Table
.Increment_Last
;
569 SFN_Table
.Table
(SFN_Table
.Last
) := (U
, F
, Index
);
570 SFN_HTable
.Set
(U
, SFN_Table
.Last
);
573 ---------------------------
574 -- Set_File_Name_Pattern --
575 ---------------------------
577 procedure Set_File_Name_Pattern
583 L
: constant Nat
:= SFN_Patterns
.Last
;
586 SFN_Patterns
.Increment_Last
;
588 -- Move up the last two entries (the default ones) and then
589 -- put the new entry into the table just before them (we
590 -- always have the default entries be the last ones).
592 SFN_Patterns
.Table
(L
+ 1) := SFN_Patterns
.Table
(L
);
593 SFN_Patterns
.Table
(L
) := SFN_Patterns
.Table
(L
- 1);
594 SFN_Patterns
.Table
(L
- 1) := (Pat
, Typ
, Dot
, Cas
);
595 end Set_File_Name_Pattern
;
601 function SFN_Hash
(F
: Unit_Name_Type
) return SFN_Header_Num
is
603 return SFN_Header_Num
(Int
(F
) rem SFN_Header_Num
'Range_Length);
608 -- We call the initialization routine from the package body, so that
609 -- Fname.Init only needs to be called explicitly to reinitialize.