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