Debian Codegraph @

gcc/ada/fname-uf.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- F N A M E . U F --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 Alloc;
27 with Debug; use Debug;
28 with Fmap; use Fmap;
29 with Krunch;
30 with Opt; use Opt;
31 with Osint; use Osint;
32 with Table;
33 with Uname; use Uname;
34 with Widechar; use Widechar;
35
36 with GNAT.HTable;
37
38 package body Fname.UF is
39
40 --------------------------------------------------------
41 -- Declarations for Handling Source_File_Name pragmas --
42 --------------------------------------------------------
43
44 type SFN_Entry is record
45 U : Unit_Name_Type; -- Unit name
46 F : File_Name_Type; -- Spec/Body file name
47 Index : Nat; -- Index from SFN pragma (0 if none)
48 end record;
49 -- Record single Unit_Name type call to Set_File_Name
50
51 package SFN_Table is new Table.Table (
52 Table_Component_Type => SFN_Entry,
53 Table_Index_Type => Int,
54 Table_Low_Bound => 0,
55 Table_Initial => Alloc.SFN_Table_Initial,
56 Table_Increment => Alloc.SFN_Table_Increment,
57 Table_Name => "SFN_Table");
58 -- Table recording all Unit_Name calls to Set_File_Name
59
60 type SFN_Header_Num is range 0 .. 100;
61
62 function SFN_Hash (F : Unit_Name_Type) return SFN_Header_Num;
63 -- Compute hash index for use by Simple_HTable
64
65 No_Entry : constant Int := -1;
66 -- Signals no entry in following table
67
68 package SFN_HTable is new GNAT.HTable.Simple_HTable (
69 Header_Num => SFN_Header_Num,
70 Element => Int,
71 No_Element => No_Entry,
72 Key => Unit_Name_Type,
73 Hash => SFN_Hash,
74 Equal => "=");
75 -- Hash table allowing rapid access to SFN_Table, the element value is an
76 -- index into this table.
77
78 type SFN_Pattern_Entry is record
79 Pat : String_Ptr; -- File name pattern (with asterisk in it)
80 Typ : Character; -- 'S'/'B'/'U' for spec/body/subunit
81 Dot : String_Ptr; -- Dot_Separator string
82 Cas : Casing_Type; -- Upper/Lower/Mixed
83 end record;
84 -- Records single call to Set_File_Name_Patterm
85
86 package SFN_Patterns is new Table.Table (
87 Table_Component_Type => SFN_Pattern_Entry,
88 Table_Index_Type => Int,
89 Table_Low_Bound => 1,
90 Table_Initial => 10,
91 Table_Increment => 100,
92 Table_Name => "SFN_Patterns");
93 -- Table recording calls to Set_File_Name_Pattern. Note that the first two
94 -- entries are set to represent the standard GNAT rules for file naming.
95
96 procedure Instantiate_SFN_Pattern
97 (Pattern : SFN_Pattern_Entry;
98 Buf : in out Bounded_String;
99 Is_Predef : Boolean := False);
100 -- On entry, Buf must contain a unit name. After returning, Buf contains
101 -- the file name corresponding to the unit following the naming pattern
102 -- described by Pattern. Is_Predef must be whether the unit name in Buf
103 -- is a predefined unit name as defined by Is_Predefined_Unit_Name.
104
105 -----------------------
106 -- File_Name_Of_Body --
107 -----------------------
108
109 function File_Name_Of_Body (Name : Name_Id) return File_Name_Type is
110 begin
111 Get_Name_String (Name);
112 Name_Buffer (Name_Len + 1 .. Name_Len + 2) := "%b";
113 Name_Len := Name_Len + 2;
114 return Get_File_Name (Name_Enter, Subunit => False);
115 end File_Name_Of_Body;
116
117 -----------------------
118 -- File_Name_Of_Spec --
119 -----------------------
120
121 function File_Name_Of_Spec (Name : Name_Id) return File_Name_Type is
122 begin
123 Get_Name_String (Name);
124 Name_Buffer (Name_Len + 1 .. Name_Len + 2) := "%s";
125 Name_Len := Name_Len + 2;
126 return Get_File_Name (Name_Enter, Subunit => False);
127 end File_Name_Of_Spec;
128
129 ----------------------------
130 -- Get_Expected_Unit_Type --
131 ----------------------------
132
133 function Get_Expected_Unit_Type
134 (Fname : File_Name_Type) return Expected_Unit_Type
135 is
136 begin
137 -- In syntax checking only mode or in multiple unit per file mode, there
138 -- can be more than one unit in a file, so the file name is not a useful
139 -- guide to the nature of the unit.
140
141 if Operating_Mode = Check_Syntax
142 or else Multiple_Unit_Index /= 0
143 then
144 return Unknown;
145 end if;
146
147 -- Search the file mapping table, if we find an entry for this file we
148 -- know whether it is a spec or a body.
149
150 for J in SFN_Table.First .. SFN_Table.Last loop
151 if Fname = SFN_Table.Table (J).F then
152 if Is_Body_Name (SFN_Table.Table (J).U) then
153 return Expect_Body;
154 else
155 return Expect_Spec;
156 end if;
157 end if;
158 end loop;
159
160 -- If no entry in file naming table, assume .ads/.adb for spec/body and
161 -- return unknown if we have neither of these two cases.
162
163 Get_Name_String (Fname);
164
165 if Name_Len > 4 then
166 if Name_Buffer (Name_Len - 3 .. Name_Len) = ".ads" then
167 return Expect_Spec;
168 elsif Name_Buffer (Name_Len - 3 .. Name_Len) = ".adb" then
169 return Expect_Body;
170 end if;
171 end if;
172
173 return Unknown;
174 end Get_Expected_Unit_Type;
175
176 ---------------------------
177 -- Get_Default_File_Name --
178 ---------------------------
179
180 function Get_Default_File_Name (Uname : Unit_Name_Type) return String is
181 Buf : Bounded_String;
182
183 Pattern : SFN_Pattern_Entry;
184 begin
185 Get_Unit_Name_String (Buf, Uname, False);
186
187 if Is_Spec_Name (Uname) then
188 Pattern := SFN_Patterns.Table (1);
189 else
190 pragma Assert (Is_Body_Name (Uname));
191 Pattern := SFN_Patterns.Table (2);
192 end if;
193
194 Instantiate_SFN_Pattern (Pattern, Buf);
195
196 return To_String (Buf);
197 end Get_Default_File_Name;
198
199 -------------------
200 -- Get_File_Name --
201 -------------------
202
203 function Get_File_Name
204 (Uname : Unit_Name_Type;
205 Subunit : Boolean;
206 May_Fail : Boolean := False) return File_Name_Type
207 is
208 Unit_Char : Character;
209 -- Set to 's' or 'b' for spec or body or to 'u' for a subunit
210
211 Unit_Char_Search : Character;
212 -- Same as Unit_Char, except that in the case of 'u' for a subunit, we
213 -- set Unit_Char_Search to 'b' if we do not find a subunit match.
214
215 N : Int;
216
217 Pname : File_Name_Type := No_File;
218 Fname : File_Name_Type := No_File;
219 -- Path name and File name for mapping
220
221 Unit_Buf : Bounded_String;
222
223 begin
224 -- Null or error name means that some previous error occurred. This is
225 -- an unrecoverable error, so signal it.
226
227 if Uname in Error_Unit_Name_Or_No_Unit_Name then
228 raise Unrecoverable_Error;
229 end if;
230
231 -- Look in the map from unit names to file names
232
233 Fname := Mapped_File_Name (Uname);
234
235 -- If the unit name is already mapped, return the corresponding file
236 -- name from the map.
237
238 if Fname /= No_File then
239 return Fname;
240 end if;
241
242 -- If there is a specific SFN pragma, return the corresponding file name
243
244 N := SFN_HTable.Get (Uname);
245
246 if N /= No_Entry then
247 return SFN_Table.Table (N).F;
248 end if;
249
250 -- Here for the case where the name was not found in the table
251
252 Append_Decoded (Unit_Buf, Uname);
253
254 -- A special fudge, normally we don't have operator symbols present,
255 -- since it is always an error to do so. However, if we do, at this
256 -- stage it has a leading double quote.
257
258 -- What we do in this case is to go back to the undecoded name, which
259 -- is of the form, for example:
260
261 -- Oand%s
262
263 -- and build a file name that looks like:
264
265 -- _and_.ads
266
267 -- which is bit peculiar, but we keep it that way. This means that we
268 -- avoid bombs due to writing a bad file name, and we get expected error
269 -- processing downstream, e.g. a compilation following gnatchop.
270
271 if Unit_Buf.Chars (1) = '"' then
272 Unit_Buf.Length := 0;
273 Append (Unit_Buf, Uname);
274 Unit_Buf.Length := Unit_Buf.Length + 1;
275 Unit_Buf.Chars (Unit_Buf.Length) :=
276 Unit_Buf.Chars (Unit_Buf.Length - 1);
277 Unit_Buf.Chars (Unit_Buf.Length - 1) :=
278 Unit_Buf.Chars (Unit_Buf.Length - 2);
279 Unit_Buf.Chars (Unit_Buf.Length - 2) := '_';
280 Unit_Buf.Chars (1) := '_';
281 end if;
282
283 -- Deal with spec or body suffix
284
285 Unit_Char := Unit_Buf.Chars (Unit_Buf.Length);
286 pragma Assert (Unit_Char = 'b' or else Unit_Char = 's');
287 pragma
288 Assert
289 (Unit_Buf.Length >= 3
290 and then Unit_Buf.Chars (Unit_Buf.Length - 1) = '%');
291 Unit_Buf.Length := Unit_Buf.Length - 2;
292
293 if Subunit then
294 Unit_Char := 'u';
295 end if;
296
297 -- Now we need to find the proper translation of the name
298
299 declare
300 Pent : Nat;
301 Fnam : File_Name_Type := No_File;
302
303 -- Start of search through pattern table
304
305 begin
306 -- Search pattern table to find a matching entry. In the general case
307 -- we do two complete searches. The first time through we stop only
308 -- if a matching file is found, the second time through we accept the
309 -- first match regardless. Note that there will always be a match the
310 -- second time around, because of the default entries at the end of
311 -- the table.
312
313 for No_File_Check in False .. True loop
314 Unit_Char_Search := Unit_Char;
315
316 <<Repeat_Search>>
317 -- The search is repeated with Unit_Char_Search set to b, if an
318 -- initial search for the subunit case fails to find any match.
319
320 Pent := SFN_Patterns.First;
321 while Pent <= SFN_Patterns.Last loop
322 if SFN_Patterns.Table (Pent).Typ = Unit_Char_Search then
323 -- Found a match, execute the pattern
324 declare
325 Is_Predef : constant Boolean :=
326 Is_Predefined_Unit_Name
327 (Uname, Renamings_Included => True);
328
329 Buf : Bounded_String;
330 begin
331 Append (Buf, Unit_Buf);
332
333 Instantiate_SFN_Pattern
334 (SFN_Patterns.Table (Pent), Buf, Is_Predef);
335
336 Fnam := Name_Find (Buf);
337 end;
338
339 -- If we are in the second search of the table, we accept
340 -- the file name without checking, because we know that the
341 -- file does not exist, except when May_Fail is True, in
342 -- which case we return No_File.
343
344 if No_File_Check then
345 if May_Fail then
346 return No_File;
347 else
348 return Fnam;
349 end if;
350
351 -- Otherwise we check if the file exists
352
353 else
354 Pname := Find_File (Fnam, Source);
355
356 -- If it does exist, we add it to the mappings and return
357 -- the file name.
358
359 if Pname /= No_File then
360
361 -- Add to mapping, so that we don't do another path
362 -- search in Find_File for this file name and, if we
363 -- use a mapping file, we are ready to update it at
364 -- the end of this compilation for the benefit of
365 -- other compilation processes.
366
367 Add_To_File_Map (Get_File_Name.Uname, Fnam, Pname);
368 return Fnam;
369
370 -- If there are only two entries, they are those of the
371 -- default GNAT naming scheme. The file does not exist,
372 -- but there is no point doing the second search, because
373 -- we will end up with the same file name. Just return
374 -- the file name, or No_File if May_Fail is True.
375
376 elsif SFN_Patterns.Last = 2 then
377 if May_Fail then
378 return No_File;
379 else
380 return Fnam;
381 end if;
382
383 -- The file does not exist, but there may be other naming
384 -- scheme. Keep on searching.
385
386 else
387 Fnam := No_File;
388 end if;
389 end if;
390 end if;
391
392 Pent := Pent + 1;
393 end loop;
394
395 -- If search failed, and was for a subunit, repeat the search with
396 -- Unit_Char_Search reset to 'b', since in the normal case we
397 -- simply treat subunits as bodies.
398
399 if Fnam = No_File and then Unit_Char_Search = 'u' then
400 Unit_Char_Search := 'b';
401 goto Repeat_Search;
402 end if;
403
404 -- Repeat entire search in No_File_Check mode if necessary
405
406 end loop;
407
408 -- Something is wrong if search fails completely, since the default
409 -- entries should catch all possibilities at this stage.
410
411 raise Program_Error;
412 end;
413 end Get_File_Name;
414
415 --------------------
416 -- Get_Unit_Index --
417 --------------------
418
419 function Get_Unit_Index (Uname : Unit_Name_Type) return Nat is
420 N : constant Int := SFN_HTable.Get (Uname);
421 begin
422 if N /= No_Entry then
423 return SFN_Table.Table (N).Index;
424 else
425 return 0;
426 end if;
427 end Get_Unit_Index;
428
429 ----------------
430 -- Initialize --
431 ----------------
432
433 procedure Initialize is
434 begin
435 SFN_Table.Init;
436 SFN_Patterns.Init;
437
438 -- Add default entries to SFN_Patterns.Table to represent the standard
439 -- default GNAT rules for file name translation.
440
441 SFN_Patterns.Append (New_Val =>
442 (Pat => new String'("*.ads"),
443 Typ => 's',
444 Dot => new String'("-"),
445 Cas => All_Lower_Case));
446
447 SFN_Patterns.Append (New_Val =>
448 (Pat => new String'("*.adb"),
449 Typ => 'b',
450 Dot => new String'("-"),
451 Cas => All_Lower_Case));
452 end Initialize;
453
454 -----------------------------
455 -- Instantiate_SFN_Pattern --
456 -----------------------------
457
458 procedure Instantiate_SFN_Pattern
459 (Pattern : SFN_Pattern_Entry;
460 Buf : in out Bounded_String;
461 Is_Predef : Boolean := False)
462 is
463 function C (N : Natural) return Character;
464 -- Return N'th character of pattern
465
466 function C (N : Natural) return Character is
467 begin
468 return Pattern.Pat (N);
469 end C;
470
471 Dot : constant String_Ptr := Pattern.Dot;
472
473 Dotl : constant Natural := Dot.all'Length;
474
475 Plen : constant Natural := Pattern.Pat'Length;
476
477 J : Natural;
478 begin
479 -- Apply casing, except that we do not do this for the case
480 -- of a predefined library file. For the latter, we always
481 -- use the all lower case name, regardless of the setting.
482
483 if not Is_Predef then
484 Set_Casing (Buf, Pattern.Cas);
485 end if;
486
487 -- If dot translation required do it
488
489 if Dot.all /= "." then
490 J := 1;
491
492 while J <= Buf.Length loop
493 if Buf.Chars (J) = '.' then
494
495 if Dotl = 1 then
496 Buf.Chars (J) := Dot (Dot'First);
497
498 else
499 Buf.Chars (J + Dotl .. Buf.Length + Dotl - 1) :=
500 Buf.Chars (J + 1 .. Buf.Length);
501 Buf.Chars (J .. J + Dotl - 1) := Dot.all;
502 Buf.Length := Buf.Length + Dotl - 1;
503 end if;
504
505 J := J + Dotl;
506
507 -- Skip past wide char sequences to avoid messing with
508 -- dot characters that are part of a sequence.
509
510 elsif Buf.Chars (J) = ASCII.ESC
511 or else (Upper_Half_Encoding
512 and then
513 Buf.Chars (J) in Upper_Half_Character)
514 then
515 Skip_Wide (Buf.Chars, J);
516 else
517 J := J + 1;
518 end if;
519 end loop;
520 end if;
521
522 -- Here move result to right if preinsertion before *
523
524 for K in 1 .. Plen loop
525 if C (K) = '*' then
526 if K /= 1 then
527 Buf.Chars (1 + K - 1 .. Buf.Length + K - 1) :=
528 Buf.Chars (1 .. Buf.Length);
529
530 for L in 1 .. K - 1 loop
531 Buf.Chars (L) := C (L);
532 end loop;
533
534 Buf.Length := Buf.Length + K - 1;
535 end if;
536
537 for L in K + 1 .. Plen loop
538 Buf.Length := Buf.Length + 1;
539 Buf.Chars (Buf.Length) := C (L);
540 end loop;
541
542 exit;
543 end if;
544 end loop;
545
546 -- Execute possible crunch on constructed name. The krunch
547 -- operation excludes any extension that may be present.
548
549 J := Buf.Length;
550 while J > 1 loop
551 exit when Buf.Chars (J) = '.';
552 J := J - 1;
553 end loop;
554
555 -- Case of extension present
556
557 if J > 1 then
558 declare
559 Ext : constant String := Buf.Chars (J .. Buf.Length);
560
561 begin
562 -- Remove extension
563
564 Buf.Length := J - 1;
565
566 -- Krunch what's left
567
568 Krunch
569 (Buf.Chars,
570 Buf.Length,
571 Integer (Maximum_File_Name_Length),
572 Debug_Flag_4);
573
574 -- Replace extension
575
576 Buf.Chars
577 (Buf.Length + 1 .. Buf.Length + Ext'Length) := Ext;
578 Buf.Length := Buf.Length + Ext'Length;
579 end;
580
581 -- Case of no extension present, straight krunch on the
582 -- entire file name.
583
584 else
585 Krunch
586 (Buf.Chars,
587 Buf.Length,
588 Integer (Maximum_File_Name_Length),
589 Debug_Flag_4);
590 end if;
591 end Instantiate_SFN_Pattern;
592
593 ----------
594 -- Lock --
595 ----------
596
597 procedure Lock is
598 begin
599 SFN_Table.Release;
600 SFN_Table.Locked := True;
601 end Lock;
602
603 -------------------
604 -- Set_File_Name --
605 -------------------
606
607 procedure Set_File_Name
608 (U : Unit_Name_Type;
609 F : File_Name_Type;
610 Index : Nat)
611 is
612 begin
613 SFN_Table.Increment_Last;
614 SFN_Table.Table (SFN_Table.Last) := (U, F, Index);
615 SFN_HTable.Set (U, SFN_Table.Last);
616 end Set_File_Name;
617
618 ---------------------------
619 -- Set_File_Name_Pattern --
620 ---------------------------
621
622 procedure Set_File_Name_Pattern
623 (Pat : String_Ptr;
624 Typ : Character;
625 Dot : String_Ptr;
626 Cas : Casing_Type)
627 is
628 L : constant Nat := SFN_Patterns.Last;
629
630 begin
631 SFN_Patterns.Increment_Last;
632
633 -- Move up the last two entries (the default ones) and then put the new
634 -- entry into the table just before them (we always have the default
635 -- entries be the last ones).
636
637 SFN_Patterns.Table (L + 1) := SFN_Patterns.Table (L);
638 SFN_Patterns.Table (L) := SFN_Patterns.Table (L - 1);
639 SFN_Patterns.Table (L - 1) := (Pat, Typ, Dot, Cas);
640 end Set_File_Name_Pattern;
641
642 --------------
643 -- SFN_Hash --
644 --------------
645
646 function SFN_Hash (F : Unit_Name_Type) return SFN_Header_Num is
647 begin
648 return SFN_Header_Num (Int (F) mod SFN_Header_Num'Range_Length);
649 end SFN_Hash;
650
651 begin
652
653 -- We call the initialization routine from the package body, so that
654 -- Fname.Init only needs to be called explicitly to reinitialize.
655
656 Fname.UF.Initialize;
657 end Fname.UF;