1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-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 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 with Atree
; use Atree
;
37 with Casing
; use Casing
;
38 with Einfo
; use Einfo
;
41 with Namet
; use Namet
;
42 with Nlists
; use Nlists
;
43 with Output
; use Output
;
44 with Sinfo
; use Sinfo
;
45 with Sinput
; use Sinput
;
53 function Get_Body_Name
(N
: Unit_Name_Type
) return Unit_Name_Type
is
57 pragma Assert
(Name_Len
> 2
58 and then Name_Buffer
(Name_Len
- 1) = '%'
59 and then Name_Buffer
(Name_Len
) = 's');
61 Name_Buffer
(Name_Len
) := 'b';
65 -----------------------------------
66 -- Get_External_Unit_Name_String --
67 -----------------------------------
69 procedure Get_External_Unit_Name_String
(N
: Unit_Name_Type
) is
74 -- Get unit name and eliminate trailing %s or %b
77 Name_Len
:= Name_Len
- 2;
79 -- Find number of components
82 for J
in 1 .. Name_Len
loop
83 if Name_Buffer
(J
) = '.' then
88 -- If simple name, nothing to do
94 -- If name has multiple components, replace dots by double underscore
96 Newlen
:= Name_Len
+ Pcount
;
98 for J
in reverse 1 .. Name_Len
loop
99 if Name_Buffer
(J
) = '.' then
100 Name_Buffer
(Newlen
) := '_';
101 Name_Buffer
(Newlen
- 1) := '_';
102 Newlen
:= Newlen
- 2;
105 Name_Buffer
(Newlen
) := Name_Buffer
(J
);
106 Newlen
:= Newlen
- 1;
110 Name_Len
:= Name_Len
+ Pcount
;
111 end Get_External_Unit_Name_String
;
113 --------------------------
114 -- Get_Parent_Body_Name --
115 --------------------------
117 function Get_Parent_Body_Name
(N
: Unit_Name_Type
) return Unit_Name_Type
is
121 while Name_Buffer
(Name_Len
) /= '.' loop
122 pragma Assert
(Name_Len
> 1); -- not a child or subunit name
123 Name_Len
:= Name_Len
- 1;
126 Name_Buffer
(Name_Len
) := '%';
127 Name_Len
:= Name_Len
+ 1;
128 Name_Buffer
(Name_Len
) := 'b';
131 end Get_Parent_Body_Name
;
133 --------------------------
134 -- Get_Parent_Spec_Name --
135 --------------------------
137 function Get_Parent_Spec_Name
(N
: Unit_Name_Type
) return Unit_Name_Type
is
141 while Name_Buffer
(Name_Len
) /= '.' loop
143 return No_Name
; -- not a child or subunit name
145 Name_Len
:= Name_Len
- 1;
149 Name_Buffer
(Name_Len
) := '%';
150 Name_Len
:= Name_Len
+ 1;
151 Name_Buffer
(Name_Len
) := 's';
154 end Get_Parent_Spec_Name
;
160 function Get_Spec_Name
(N
: Unit_Name_Type
) return Unit_Name_Type
is
164 pragma Assert
(Name_Len
> 2
165 and then Name_Buffer
(Name_Len
- 1) = '%'
166 and then Name_Buffer
(Name_Len
) = 'b');
168 Name_Buffer
(Name_Len
) := 's';
176 function Get_Unit_Name
(N
: Node_Id
) return Unit_Name_Type
is
178 Unit_Name_Buffer
: String (1 .. Hostparm
.Max_Name_Length
);
179 -- Buffer used to build name of unit. Note that we cannot use the
180 -- Name_Buffer in package Name_Table because we use it to read
183 Unit_Name_Length
: Natural := 0;
184 -- Length of name stored in Unit_Name_Buffer
189 procedure Add_Char
(C
: Character);
190 -- Add a single character to stored unit name
192 procedure Add_Name
(Name
: Name_Id
);
193 -- Add the characters of a names table entry to stored unit name
195 procedure Add_Node_Name
(Node
: Node_Id
);
196 -- Recursive procedure adds characters associated with Node
198 function Get_Parent
(Node
: Node_Id
) return Node_Id
;
199 -- Get parent compilation unit of a stub
205 procedure Add_Char
(C
: Character) is
207 -- Should really check for max length exceeded here???
208 Unit_Name_Length
:= Unit_Name_Length
+ 1;
209 Unit_Name_Buffer
(Unit_Name_Length
) := C
;
216 procedure Add_Name
(Name
: Name_Id
) is
218 Get_Name_String
(Name
);
220 for J
in 1 .. Name_Len
loop
221 Add_Char
(Name_Buffer
(J
));
229 procedure Add_Node_Name
(Node
: Node_Id
) is
230 Kind
: Node_Kind
:= Nkind
(Node
);
233 -- Just ignore an error node (someone else will give a message)
238 -- Otherwise see what kind of node we have
244 N_Defining_Identifier |
245 N_Defining_Operator_Symbol
=>
247 -- Note: it is of course an error to have a defining
248 -- operator symbol at this point, but this is not where
249 -- the error is signalled, so we handle it nicely here!
251 Add_Name
(Chars
(Node
));
253 when N_Defining_Program_Unit_Name
=>
254 Add_Node_Name
(Name
(Node
));
256 Add_Node_Name
(Defining_Identifier
(Node
));
258 when N_Selected_Component |
260 Add_Node_Name
(Prefix
(Node
));
262 Add_Node_Name
(Selector_Name
(Node
));
264 when N_Subprogram_Specification |
265 N_Package_Specification
=>
266 Add_Node_Name
(Defining_Unit_Name
(Node
));
268 when N_Subprogram_Body |
269 N_Subprogram_Declaration |
270 N_Package_Declaration |
271 N_Generic_Declaration
=>
272 Add_Node_Name
(Specification
(Node
));
274 when N_Generic_Instantiation
=>
275 Add_Node_Name
(Defining_Unit_Name
(Node
));
277 when N_Package_Body
=>
278 Add_Node_Name
(Defining_Unit_Name
(Node
));
282 Add_Node_Name
(Defining_Identifier
(Node
));
284 when N_Package_Renaming_Declaration
=>
285 Add_Node_Name
(Defining_Unit_Name
(Node
));
287 when N_Subprogram_Renaming_Declaration
=>
288 Add_Node_Name
(Specification
(Node
));
290 when N_Generic_Renaming_Declaration
=>
291 Add_Node_Name
(Defining_Unit_Name
(Node
));
293 when N_Subprogram_Body_Stub
=>
294 Add_Node_Name
(Get_Parent
(Node
));
296 Add_Node_Name
(Specification
(Node
));
298 when N_Compilation_Unit
=>
299 Add_Node_Name
(Unit
(Node
));
301 when N_Package_Body_Stub
=>
302 Add_Node_Name
(Get_Parent
(Node
));
304 Add_Node_Name
(Defining_Identifier
(Node
));
306 when N_Task_Body_Stub |
307 N_Protected_Body_Stub
=>
308 Add_Node_Name
(Get_Parent
(Node
));
310 Add_Node_Name
(Defining_Identifier
(Node
));
313 Add_Node_Name
(Name
(Node
));
315 Add_Node_Name
(Proper_Body
(Node
));
317 when N_With_Clause
=>
318 Add_Node_Name
(Name
(Node
));
321 Add_Node_Name
(Expression
(First
322 (Pragma_Argument_Associations
(Node
))));
324 -- Tasks and protected stuff appear only in an error context,
325 -- but the error has been posted elsewhere, so we deal nicely
326 -- with these error situations here, and produce a reasonable
327 -- unit name using the defining identifier.
329 when N_Task_Type_Declaration |
330 N_Single_Task_Declaration |
331 N_Protected_Type_Declaration |
332 N_Single_Protected_Declaration
=>
333 Add_Node_Name
(Defining_Identifier
(Node
));
346 function Get_Parent
(Node
: Node_Id
) return Node_Id
is
350 while Nkind
(N
) /= N_Compilation_Unit
loop
357 --------------------------------------------
358 -- Start of Processing for Get_Unit_Name --
359 --------------------------------------------
364 -- If we have Defining_Identifier, find the associated unit node
366 if Nkind
(Node
) = N_Defining_Identifier
then
367 Node
:= Declaration_Node
(Node
);
369 -- If an expanded name, it is an already analyzed child unit, find
372 elsif Nkind
(Node
) = N_Expanded_Name
then
373 Node
:= Declaration_Node
(Entity
(Node
));
376 if Nkind
(Node
) = N_Package_Specification
377 or else Nkind
(Node
) in N_Subprogram_Specification
379 Node
:= Parent
(Node
);
382 -- Node points to the unit, so get its name and add proper suffix
384 Add_Node_Name
(Node
);
388 when N_Generic_Declaration |
389 N_Subprogram_Declaration |
390 N_Package_Declaration |
393 N_Generic_Instantiation |
394 N_Package_Renaming_Declaration |
395 N_Subprogram_Renaming_Declaration |
396 N_Generic_Renaming_Declaration |
397 N_Single_Task_Declaration |
398 N_Single_Protected_Declaration |
399 N_Task_Type_Declaration |
400 N_Protected_Type_Declaration
=>
404 when N_Subprogram_Body |
411 N_Selected_Component
=>
419 Name_Buffer
(1 .. Unit_Name_Length
) :=
420 Unit_Name_Buffer
(1 .. Unit_Name_Length
);
421 Name_Len
:= Unit_Name_Length
;
426 --------------------------
427 -- Get_Unit_Name_String --
428 --------------------------
430 procedure Get_Unit_Name_String
(N
: Unit_Name_Type
) is
431 Unit_Is_Body
: Boolean;
434 Get_Decoded_Name_String
(N
);
435 Unit_Is_Body
:= Name_Buffer
(Name_Len
) = 'b';
436 Set_Casing
(Identifier_Casing
(Source_Index
(Main_Unit
)), Mixed_Case
);
438 -- A special fudge, normally we don't have operator symbols present,
439 -- since it is always an error to do so. However, if we do, at this
440 -- stage it has the form:
444 -- and the %s or %b has already been eliminated so put 2 chars back
446 if Name_Buffer
(1) = '"' then
447 Name_Len
:= Name_Len
+ 2;
450 -- Now adjust the %s or %b to (spec) or (body)
453 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 5) := " (body)";
455 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 5) := " (spec)";
458 for J
in 1 .. Name_Len
loop
459 if Name_Buffer
(J
) = '-' then
460 Name_Buffer
(J
) := '.';
464 Name_Len
:= Name_Len
+ (7 - 2);
465 end Get_Unit_Name_String
;
471 function Is_Body_Name
(N
: Unit_Name_Type
) return Boolean is
475 and then Name_Buffer
(Name_Len
- 1) = '%'
476 and then Name_Buffer
(Name_Len
) = 'b';
483 function Is_Child_Name
(N
: Unit_Name_Type
) return Boolean is
490 while Name_Buffer
(J
) /= '.' loop
492 return False; -- not a child or subunit name
505 function Is_Spec_Name
(N
: Unit_Name_Type
) return Boolean is
509 and then Name_Buffer
(Name_Len
- 1) = '%'
510 and then Name_Buffer
(Name_Len
) = 's';
513 -----------------------
514 -- Name_To_Unit_Name --
515 -----------------------
517 function Name_To_Unit_Name
(N
: Name_Id
) return Unit_Name_Type
is
520 Name_Buffer
(Name_Len
+ 1) := '%';
521 Name_Buffer
(Name_Len
+ 2) := 's';
522 Name_Len
:= Name_Len
+ 2;
524 end Name_To_Unit_Name
;
531 (Old
: Unit_Name_Type
;
532 Newp
: Unit_Name_Type
)
533 return Unit_Name_Type
538 Get_Name_String
(Old
);
541 Child
: String := Name_Buffer
(1 .. Name_Len
);
544 Get_Name_String
(Newp
);
545 Name_Len
:= Name_Len
- 2;
548 while Child
(P
) /= '.' loop
552 while P
<= Child
'Last loop
553 Name_Len
:= Name_Len
+ 1;
554 Name_Buffer
(Name_Len
) := Child
(P
);
566 function Uname_Ge
(Left
, Right
: Unit_Name_Type
) return Boolean is
568 return Left
= Right
or else Uname_Gt
(Left
, Right
);
575 function Uname_Gt
(Left
, Right
: Unit_Name_Type
) return Boolean is
577 return Left
/= Right
and then not Uname_Lt
(Left
, Right
);
584 function Uname_Le
(Left
, Right
: Unit_Name_Type
) return Boolean is
586 return Left
= Right
or else Uname_Lt
(Left
, Right
);
593 function Uname_Lt
(Left
, Right
: Unit_Name_Type
) return Boolean is
594 Left_Name
: String (1 .. Hostparm
.Max_Name_Length
);
595 Left_Length
: Natural;
596 Right_Name
: String renames Name_Buffer
;
597 Right_Length
: Natural renames Name_Len
;
601 pragma Warnings
(Off
, Right_Length
);
602 -- Suppress warnings on Right_Length, used in pragma Assert
608 Get_Name_String
(Left
);
609 Left_Name
(1 .. Name_Len
+ 1) := Name_Buffer
(1 .. Name_Len
+ 1);
610 Left_Length
:= Name_Len
;
611 Get_Name_String
(Right
);
615 exit when Left_Name
(J
) = '%';
617 if Right_Name
(J
) = '%' then
618 return False; -- left name is longer
621 pragma Assert
(J
<= Left_Length
and then J
<= Right_Length
);
623 if Left_Name
(J
) /= Right_Name
(J
) then
624 return Left_Name
(J
) < Right_Name
(J
); -- parent names different
630 -- Come here pointing to % in left name
632 if Right_Name
(J
) /= '%' then
633 return True; -- right name is longer
636 -- Here the parent names are the same and specs sort low. If neither is
637 -- a spec, then we are comparing the same name and we want a result of
638 -- False in any case.
640 return Left_Name
(J
+ 1) = 's';
643 ---------------------
644 -- Write_Unit_Name --
645 ---------------------
647 procedure Write_Unit_Name
(N
: Unit_Name_Type
) is
649 Get_Unit_Name_String
(N
);
650 Write_Str
(Name_Buffer
(1 .. Name_Len
));