Debian Codegraph @

gcc/ada/uname.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- U N A M E --
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 Atree; use Atree;
27 with Casing; use Casing;
28 with Einfo; use Einfo;
29 with Einfo.Utils; use Einfo.Utils;
30 with Hostparm;
31 with Lib; use Lib;
32 with Nlists; use Nlists;
33 with Output; use Output;
34 with Sinfo; use Sinfo;
35 with Sinfo.Nodes; use Sinfo.Nodes;
36 with Sinfo.Utils; use Sinfo.Utils;
37 with Sinput; use Sinput;
38
39 package body Uname is
40
41 function Has_Prefix (X, Prefix : String) return Boolean;
42 -- True if Prefix is at the beginning of X. For example,
43 -- Has_Prefix("a-filename.ads", Prefix => "a-") is True.
44
45 function Encoded_Library_Unit_Name (N : Unit_Name_Type) return String;
46 -- Returns the name of the library unit N as a string without a %s or %b
47 -- suffix.
48
49 function Is_Predefined_Unit_Name
50 (Name : String; Renamings_Included : Boolean := True) return Boolean;
51 -- Same as Fname.Is_Predefined_File_Name, except it works with the name of
52 -- the unit, rather than the file name.
53
54 -------------------
55 -- Get_Body_Name --
56 -------------------
57
58 function Get_Body_Name (N : Unit_Name_Type) return Unit_Name_Type is
59 Buffer : Bounded_String;
60 begin
61 Append (Buffer, N);
62 pragma Assert (Is_Spec_Name (N));
63 Buffer.Chars (Buffer.Length) := 'b';
64 return Name_Find (Buffer);
65 end Get_Body_Name;
66
67 -------------------------------
68 -- Encoded_Library_Unit_Name --
69 -------------------------------
70
71 function Encoded_Library_Unit_Name (N : Unit_Name_Type) return String is
72 S : constant String := Get_Name_String (N);
73 begin
74 return S (S'First .. S'Last - 2);
75 end Encoded_Library_Unit_Name;
76
77 -----------------------------------
78 -- Get_External_Unit_Name_String --
79 -----------------------------------
80
81 procedure Get_External_Unit_Name_String (N : Unit_Name_Type) is
82 Pcount : Natural;
83 Newlen : Natural;
84
85 begin
86 Name_Len := 0;
87 Add_Str_To_Name_Buffer (Encoded_Library_Unit_Name (N));
88
89 -- Find number of components
90
91 Pcount := 0;
92 for J in 1 .. Name_Len loop
93 if Name_Buffer (J) = '.' then
94 Pcount := Pcount + 1;
95 end if;
96 end loop;
97
98 -- If simple name, nothing to do
99
100 if Pcount = 0 then
101 return;
102 end if;
103
104 -- If name has multiple components, replace dots by double underscore
105
106 Newlen := Name_Len + Pcount;
107
108 for J in reverse 1 .. Name_Len loop
109 if Name_Buffer (J) = '.' then
110 Name_Buffer (Newlen) := '_';
111 Name_Buffer (Newlen - 1) := '_';
112 Newlen := Newlen - 2;
113
114 else
115 Name_Buffer (Newlen) := Name_Buffer (J);
116 Newlen := Newlen - 1;
117 end if;
118 end loop;
119
120 Name_Len := Name_Len + Pcount;
121 end Get_External_Unit_Name_String;
122
123 --------------------------
124 -- Get_Parent_Body_Name --
125 --------------------------
126
127 function Get_Parent_Body_Name (N : Unit_Name_Type) return Unit_Name_Type is
128 Buffer : Bounded_String;
129 begin
130 Append (Buffer, N);
131
132 while Buffer.Chars (Buffer.Length) /= '.' loop
133 pragma Assert (Buffer.Length > 1); -- not a child or subunit name
134 Buffer.Length := Buffer.Length - 1;
135 end loop;
136
137 Buffer.Chars (Buffer.Length) := '%';
138 Append (Buffer, 'b');
139
140 return Name_Find (Buffer);
141 end Get_Parent_Body_Name;
142
143 --------------------------
144 -- Get_Parent_Spec_Name --
145 --------------------------
146
147 function Get_Parent_Spec_Name (N : Unit_Name_Type) return Unit_Name_Type is
148 Buffer : Bounded_String;
149 begin
150 Append (Buffer, N);
151
152 while Buffer.Chars (Buffer.Length) /= '.' loop
153 if Buffer.Length = 1 then
154 return No_Unit_Name;
155 else
156 Buffer.Length := Buffer.Length - 1;
157 end if;
158 end loop;
159
160 Buffer.Chars (Buffer.Length) := '%';
161 Append (Buffer, 's');
162
163 return Name_Find (Buffer);
164 end Get_Parent_Spec_Name;
165
166 -------------------
167 -- Get_Spec_Name --
168 -------------------
169
170 function Get_Spec_Name (N : Unit_Name_Type) return Unit_Name_Type is
171 Buffer : Bounded_String;
172 begin
173 Append (Buffer, N);
174 pragma Assert (Is_Body_Name (N));
175 Buffer.Chars (Buffer.Length) := 's';
176 return Name_Find (Buffer);
177 end Get_Spec_Name;
178
179 -------------------
180 -- Get_Unit_Name --
181 -------------------
182
183 function Get_Unit_Name (N : Node_Id) return Unit_Name_Type is
184
185 Unit_Name_Buffer : Bounded_String;
186 -- Buffer used to build name of unit
187
188 Node : Node_Id;
189 -- Program unit node
190
191 procedure Add_Char (C : Character);
192 -- Add a single character to stored unit name
193
194 procedure Add_Name (Name : Name_Id);
195 -- Add the characters of a names table entry to stored unit name
196
197 procedure Add_Node_Name (Node : Node_Id);
198 -- Recursive procedure adds characters associated with Node
199
200 function Get_Parent (Node : Node_Id) return Node_Id;
201 -- Get parent compilation unit of a stub
202
203 --------------
204 -- Add_Char --
205 --------------
206
207 procedure Add_Char (C : Character) is
208 begin
209 Append (Unit_Name_Buffer, C);
210 end Add_Char;
211
212 --------------
213 -- Add_Name --
214 --------------
215
216 procedure Add_Name (Name : Name_Id) is
217 begin
218 Append (Unit_Name_Buffer, Name);
219 end Add_Name;
220
221 -------------------
222 -- Add_Node_Name --
223 -------------------
224
225 procedure Add_Node_Name (Node : Node_Id) is
226 begin
227 -- Just ignore an error node (someone else will give a message)
228
229 if Node = Error then
230 return;
231
232 -- Otherwise see what kind of node we have
233
234 else
235 case Nkind (Node) is
236 when N_Defining_Identifier
237 | N_Defining_Operator_Symbol
238 | N_Identifier
239 =>
240 -- Note: it is of course an error to have a defining
241 -- operator symbol at this point, but this is not where
242 -- the error is signalled, so we handle it nicely here.
243
244 Add_Name (Chars (Node));
245
246 when N_Defining_Program_Unit_Name =>
247 Add_Node_Name (Name (Node));
248 Add_Char ('.');
249 Add_Node_Name (Defining_Identifier (Node));
250
251 when N_Expanded_Name
252 | N_Selected_Component
253 =>
254 Add_Node_Name (Prefix (Node));
255 Add_Char ('.');
256 Add_Node_Name (Selector_Name (Node));
257
258 when N_Package_Specification
259 | N_Subprogram_Specification
260 =>
261 Add_Node_Name (Defining_Unit_Name (Node));
262
263 when N_Generic_Declaration
264 | N_Package_Declaration
265 | N_Subprogram_Body
266 | N_Subprogram_Declaration
267 =>
268 Add_Node_Name (Specification (Node));
269
270 when N_Generic_Instantiation =>
271 Add_Node_Name (Defining_Unit_Name (Node));
272
273 when N_Package_Body =>
274 Add_Node_Name (Defining_Unit_Name (Node));
275
276 when N_Protected_Body
277 | N_Task_Body
278 =>
279 Add_Node_Name (Defining_Identifier (Node));
280
281 when N_Package_Renaming_Declaration =>
282 Add_Node_Name (Defining_Unit_Name (Node));
283
284 when N_Subprogram_Renaming_Declaration =>
285 Add_Node_Name (Specification (Node));
286
287 when N_Generic_Renaming_Declaration =>
288 Add_Node_Name (Defining_Unit_Name (Node));
289
290 when N_Subprogram_Body_Stub =>
291 Add_Node_Name (Get_Parent (Node));
292 Add_Char ('.');
293 Add_Node_Name (Specification (Node));
294
295 when N_Compilation_Unit =>
296 Add_Node_Name (Unit (Node));
297
298 when N_Package_Body_Stub
299 | N_Protected_Body_Stub
300 | N_Task_Body_Stub
301 =>
302 Add_Node_Name (Get_Parent (Node));
303 Add_Char ('.');
304 Add_Node_Name (Defining_Identifier (Node));
305
306 when N_Subunit =>
307 Add_Node_Name (Name (Node));
308 Add_Char ('.');
309 Add_Node_Name (Proper_Body (Node));
310
311 when N_With_Clause =>
312 Add_Node_Name (Name (Node));
313
314 when N_Pragma =>
315 Add_Node_Name (Expression (First
316 (Pragma_Argument_Associations (Node))));
317
318 -- Tasks and protected stuff appear only in an error context,
319 -- but the error has been posted elsewhere, so we deal nicely
320 -- with these error situations here, and produce a reasonable
321 -- unit name using the defining identifier.
322
323 when N_Protected_Type_Declaration
324 | N_Single_Protected_Declaration
325 | N_Single_Task_Declaration
326 | N_Task_Type_Declaration
327 =>
328 Add_Node_Name (Defining_Identifier (Node));
329
330 when others =>
331 raise Program_Error;
332 end case;
333 end if;
334 end Add_Node_Name;
335
336 ----------------
337 -- Get_Parent --
338 ----------------
339
340 function Get_Parent (Node : Node_Id) return Node_Id is
341 N : Node_Id := Node;
342
343 begin
344 while Nkind (N) /= N_Compilation_Unit loop
345 N := Parent (N);
346 end loop;
347
348 return N;
349 end Get_Parent;
350
351 -- Start of processing for Get_Unit_Name
352
353 begin
354 Node := N;
355
356 -- If we have Defining_Identifier, find the associated unit node
357
358 if Nkind (Node) = N_Defining_Identifier then
359 Node := Declaration_Node (Node);
360
361 -- If an expanded name, it is an already analyzed child unit, find
362 -- unit node.
363
364 elsif Nkind (Node) = N_Expanded_Name then
365 Node := Declaration_Node (Entity (Node));
366 end if;
367
368 if Nkind (Node) in N_Package_Specification
369 | N_Subprogram_Specification
370 then
371 Node := Parent (Node);
372 end if;
373
374 -- Node points to the unit, so get its name and add proper suffix
375
376 Add_Node_Name (Node);
377 Add_Char ('%');
378
379 case Nkind (Node) is
380 when N_Generic_Declaration
381 | N_Generic_Instantiation
382 | N_Generic_Renaming_Declaration
383 | N_Package_Declaration
384 | N_Package_Renaming_Declaration
385 | N_Pragma
386 | N_Protected_Type_Declaration
387 | N_Single_Protected_Declaration
388 | N_Single_Task_Declaration
389 | N_Subprogram_Declaration
390 | N_Subprogram_Renaming_Declaration
391 | N_Task_Type_Declaration
392 | N_With_Clause
393 =>
394 Add_Char ('s');
395
396 when N_Body_Stub
397 | N_Identifier
398 | N_Package_Body
399 | N_Protected_Body
400 | N_Selected_Component
401 | N_Subprogram_Body
402 | N_Subunit
403 | N_Task_Body
404 =>
405 Add_Char ('b');
406
407 when others =>
408 raise Program_Error;
409 end case;
410
411 return Name_Find (Unit_Name_Buffer);
412 end Get_Unit_Name;
413
414 --------------------------
415 -- Get_Unit_Name_String --
416 --------------------------
417
418 procedure Get_Unit_Name_String
419 (Buf : in out Bounded_String;
420 N : Unit_Name_Type;
421 Suffix : Boolean := True)
422 is
423 begin
424 Buf.Length := 0;
425 Append_Decoded (Buf, N);
426 pragma Assert (Buf.Chars (1) /= '"');
427 pragma Assert (Is_Body_Name (N) or else Is_Spec_Name (N));
428
429 -- Buf always ends with "%s" or "%b", which we either remove, or replace
430 -- with " (spec)" or " (body)". Set_Casing of Buf after checking for
431 -- (lower case) 's'/'b', and before appending (lower case) "spec" or
432 -- "body".
433
434 declare
435 S : constant String :=
436 (if Buf.Chars (Buf.Length) = 's' then " (spec)" else " (body)");
437 begin
438 Buf.Length := Buf.Length - 2; -- remove "%s" or "%b"
439 Set_Casing (Buf, Identifier_Casing (Source_Index (Main_Unit)));
440
441 if Suffix then
442 Append (Buf, S);
443 end if;
444 end;
445
446 for J in 1 .. Buf.Length loop
447 if Buf.Chars (J) = '-' then
448 Buf.Chars (J) := '.';
449 end if;
450 end loop;
451 end Get_Unit_Name_String;
452
453 ----------------
454 -- Has_Prefix --
455 ----------------
456
457 function Has_Prefix (X, Prefix : String) return Boolean is
458 begin
459 if X'Length >= Prefix'Length then
460 declare
461 Slice : String renames
462 X (X'First .. X'First + Prefix'Length - 1);
463 begin
464 return Slice = Prefix;
465 end;
466 end if;
467 return False;
468 end Has_Prefix;
469
470 ------------------
471 -- Is_Body_Name --
472 ------------------
473
474 function Is_Body_Name (N : Unit_Name_Type) return Boolean is
475 Buffer : Bounded_String;
476 begin
477 Append (Buffer, N);
478 pragma Assert
479 (Buffer.Length > 2 and then Buffer.Chars (Buffer.Length - 1) = '%');
480 return Buffer.Chars (Buffer.Length) = 'b';
481 end Is_Body_Name;
482
483 -------------------
484 -- Is_Child_Name --
485 -------------------
486
487 function Is_Child_Name (N : Unit_Name_Type) return Boolean is
488 Buffer : Bounded_String;
489
490 begin
491 Append (Buffer, N);
492
493 while Buffer.Chars (Buffer.Length) /= '.' loop
494 if Buffer.Length = 1 then
495 return False; -- not a child or subunit name
496 else
497 Buffer.Length := Buffer.Length - 1;
498 end if;
499 end loop;
500
501 return True;
502 end Is_Child_Name;
503
504 ---------------------------
505 -- Is_Internal_Unit_Name --
506 ---------------------------
507
508 function Is_Internal_Unit_Name
509 (Name : Unit_Name_Type; Renamings_Included : Boolean := True)
510 return Boolean
511 is
512 Gnat : constant String := "gnat";
513
514 Lib_Unit_Name : constant String := Encoded_Library_Unit_Name (Name);
515 begin
516 if Lib_Unit_Name = Gnat then
517 return True;
518 end if;
519
520 if Has_Prefix (Lib_Unit_Name, Prefix => Gnat & ".") then
521 return True;
522 end if;
523
524 return Is_Predefined_Unit_Name (Lib_Unit_Name, Renamings_Included);
525 end Is_Internal_Unit_Name;
526
527 -----------------------------
528 -- Is_Predefined_Unit_Name --
529 -----------------------------
530
531 function Is_Predefined_Unit_Name
532 (Name : Unit_Name_Type; Renamings_Included : Boolean := True)
533 return Boolean is
534 begin
535 return
536 Is_Predefined_Unit_Name
537 (Encoded_Library_Unit_Name (Name), Renamings_Included);
538 end Is_Predefined_Unit_Name;
539
540 function Is_Predefined_Unit_Name
541 (Name : String; Renamings_Included : Boolean := True) return Boolean
542 is
543 Ada : constant String := "ada";
544 Interfaces : constant String := "interfaces";
545 System : constant String := "system";
546 begin
547 if Name in Ada | Interfaces | System then
548 return True;
549 end if;
550
551 if Has_Prefix (Name, Prefix => Ada & ".")
552 or else Has_Prefix (Name, Prefix => Interfaces & ".")
553 or else Has_Prefix (Name, Prefix => System & ".")
554 then
555 return True;
556 end if;
557
558 if not Renamings_Included then
559 return False;
560 end if;
561
562 -- The following are the predefined renamings
563
564 return
565 Name in
566 "calendar"
567 | "machine_code"
568 | "unchecked_conversion"
569 | "unchecked_deallocation"
570 | "direct_io"
571 | "io_exceptions"
572 | "sequential_io"
573 | "text_io";
574 end Is_Predefined_Unit_Name;
575
576 ------------------
577 -- Is_Spec_Name --
578 ------------------
579
580 function Is_Spec_Name (N : Unit_Name_Type) return Boolean is
581 Buffer : Bounded_String;
582 begin
583 Append (Buffer, N);
584 pragma Assert
585 (Buffer.Length > 2 and then Buffer.Chars (Buffer.Length - 1) = '%');
586 return Buffer.Chars (Buffer.Length) = 's';
587 end Is_Spec_Name;
588
589 -----------------------
590 -- Name_To_Unit_Name --
591 -----------------------
592
593 function Name_To_Unit_Name (N : Name_Id) return Unit_Name_Type is
594 Buffer : Bounded_String;
595 begin
596 Append (Buffer, N);
597 Append (Buffer, "%s");
598 return Name_Find (Buffer);
599 end Name_To_Unit_Name;
600
601 ---------------
602 -- New_Child --
603 ---------------
604
605 function New_Child
606 (Old : Unit_Name_Type;
607 Newp : Unit_Name_Type) return Unit_Name_Type
608 is
609 P : Natural;
610
611 begin
612 Get_Name_String (Old);
613
614 declare
615 Child : constant String := Name_Buffer (1 .. Name_Len);
616
617 begin
618 Get_Name_String (Newp);
619 Name_Len := Name_Len - 2;
620
621 P := Child'Last;
622 while Child (P) /= '.' loop
623 P := P - 1;
624 end loop;
625
626 while P <= Child'Last loop
627 Name_Len := Name_Len + 1;
628 Name_Buffer (Name_Len) := Child (P);
629 P := P + 1;
630 end loop;
631
632 return Name_Find;
633 end;
634 end New_Child;
635
636 --------------
637 -- Uname_Ge --
638 --------------
639
640 function Uname_Ge (Left, Right : Unit_Name_Type) return Boolean is
641 begin
642 return Left = Right or else Uname_Gt (Left, Right);
643 end Uname_Ge;
644
645 --------------
646 -- Uname_Gt --
647 --------------
648
649 function Uname_Gt (Left, Right : Unit_Name_Type) return Boolean is
650 begin
651 return Left /= Right and then not Uname_Lt (Left, Right);
652 end Uname_Gt;
653
654 --------------
655 -- Uname_Le --
656 --------------
657
658 function Uname_Le (Left, Right : Unit_Name_Type) return Boolean is
659 begin
660 return Left = Right or else Uname_Lt (Left, Right);
661 end Uname_Le;
662
663 --------------
664 -- Uname_Lt --
665 --------------
666
667 function Uname_Lt (Left, Right : Unit_Name_Type) return Boolean is
668 Left_Name : String (1 .. Hostparm.Max_Name_Length);
669 Left_Length : Natural;
670 Right_Name : String renames Name_Buffer;
671 Right_Length : Natural renames Name_Len;
672 J : Natural;
673
674 begin
675 pragma Warnings (Off, Right_Length);
676 -- Suppress warnings on Right_Length, used in pragma Assert
677
678 if Left = Right then
679 return False;
680 end if;
681
682 Get_Name_String (Left);
683 Left_Name (1 .. Name_Len + 1) := Name_Buffer (1 .. Name_Len + 1);
684 Left_Length := Name_Len;
685 Get_Name_String (Right);
686 J := 1;
687
688 loop
689 exit when Left_Name (J) = '%';
690
691 if Right_Name (J) = '%' then
692 return False; -- left name is longer
693 end if;
694
695 pragma Assert (J <= Left_Length and then J <= Right_Length);
696
697 if Left_Name (J) /= Right_Name (J) then
698 return Left_Name (J) < Right_Name (J); -- parent names different
699 end if;
700
701 J := J + 1;
702 end loop;
703
704 -- Come here pointing to % in left name
705
706 if Right_Name (J) /= '%' then
707 return True; -- right name is longer
708 end if;
709
710 -- Here the parent names are the same and specs sort low. If neither is
711 -- a spec, then we are comparing the same name and we want a result of
712 -- False in any case.
713
714 return Left_Name (J + 1) = 's';
715 end Uname_Lt;
716
717 ---------------------
718 -- Write_Unit_Name --
719 ---------------------
720
721 procedure Write_Unit_Name (N : Unit_Name_Type) is
722 Buf : Bounded_String;
723 begin
724 Get_Unit_Name_String (Buf, N);
725 Write_Str (Buf.Chars (1 .. Buf.Length));
726 end Write_Unit_Name;
727
728 -------------------------------
729 -- Write_Unit_Name_For_Debug --
730 -------------------------------
731
732 procedure Write_Unit_Name_For_Debug (N : Unit_Name_Type) is
733 begin
734 if Is_Valid_Name (N) then
735 Write_Unit_Name (N);
736 else
737 Write_Name_For_Debug (N);
738 end if;
739 end Write_Unit_Name_For_Debug;
740
741 end Uname;