1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNAT was originally developed by the GNAT team at New York University. --
31 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with Atree; use Atree;
36 with Casing; use Casing;
37 with Einfo; use Einfo;
40 with Namet; use Namet;
41 with Nlists; use Nlists;
42 with Output; use Output;
43 with Sinfo; use Sinfo;
44 with Sinput; use Sinput;
52 function Get_Body_Name (N : Unit_Name_Type) return Unit_Name_Type is
56 pragma Assert (Name_Len > 2
57 and then Name_Buffer (Name_Len - 1) = '%'
58 and then Name_Buffer (Name_Len) = 's');
60 Name_Buffer (Name_Len) := 'b';
64 -----------------------------------
65 -- Get_External_Unit_Name_String --
66 -----------------------------------
68 procedure Get_External_Unit_Name_String (N : Unit_Name_Type) is
73 -- Get unit name and eliminate trailing %s or %b
76 Name_Len := Name_Len - 2;
78 -- Find number of components
81 for J in 1 .. Name_Len loop
82 if Name_Buffer (J) = '.' then
87 -- If simple name, nothing to do
93 -- If name has multiple components, replace dots by double underscore
95 Newlen := Name_Len + Pcount;
97 for J in reverse 1 .. Name_Len loop
98 if Name_Buffer (J) = '.' then
99 Name_Buffer (Newlen) := '_';
100 Name_Buffer (Newlen - 1) := '_';
101 Newlen := Newlen - 2;
104 Name_Buffer (Newlen) := Name_Buffer (J);
105 Newlen := Newlen - 1;
109 Name_Len := Name_Len + Pcount;
110 end Get_External_Unit_Name_String;
112 --------------------------
113 -- Get_Parent_Body_Name --
114 --------------------------
116 function Get_Parent_Body_Name (N : Unit_Name_Type) return Unit_Name_Type is
120 while Name_Buffer (Name_Len) /= '.' loop
121 pragma Assert (Name_Len > 1); -- not a child or subunit name
122 Name_Len := Name_Len - 1;
125 Name_Buffer (Name_Len) := '%';
126 Name_Len := Name_Len + 1;
127 Name_Buffer (Name_Len) := 'b';
130 end Get_Parent_Body_Name;
132 --------------------------
133 -- Get_Parent_Spec_Name --
134 --------------------------
136 function Get_Parent_Spec_Name (N : Unit_Name_Type) return Unit_Name_Type is
140 while Name_Buffer (Name_Len) /= '.' loop
142 return No_Name; -- not a child or subunit name
144 Name_Len := Name_Len - 1;
148 Name_Buffer (Name_Len) := '%';
149 Name_Len := Name_Len + 1;
150 Name_Buffer (Name_Len) := 's';
153 end Get_Parent_Spec_Name;
159 function Get_Spec_Name (N : Unit_Name_Type) return Unit_Name_Type is
163 pragma Assert (Name_Len > 2
164 and then Name_Buffer (Name_Len - 1) = '%'
165 and then Name_Buffer (Name_Len) = 'b');
167 Name_Buffer (Name_Len) := 's';
175 function Get_Unit_Name (N : Node_Id) return Unit_Name_Type is
177 Unit_Name_Buffer : String (1 .. Hostparm.Max_Name_Length);
178 -- Buffer used to build name of unit. Note that we cannot use the
179 -- Name_Buffer in package Name_Table because we use it to read
182 Unit_Name_Length : Natural := 0;
183 -- Length of name stored in Unit_Name_Buffer
188 procedure Add_Char (C : Character);
189 -- Add a single character to stored unit name
191 procedure Add_Name (Name : Name_Id);
192 -- Add the characters of a names table entry to stored unit name
194 procedure Add_Node_Name (Node : Node_Id);
195 -- Recursive procedure adds characters associated with Node
197 function Get_Parent (Node : Node_Id) return Node_Id;
198 -- Get parent compilation unit of a stub
204 procedure Add_Char (C : Character) is
206 -- Should really check for max length exceeded here???
207 Unit_Name_Length := Unit_Name_Length + 1;
208 Unit_Name_Buffer (Unit_Name_Length) := C;
215 procedure Add_Name (Name : Name_Id) is
217 Get_Name_String (Name);
219 for J in 1 .. Name_Len loop
220 Add_Char (Name_Buffer (J));
228 procedure Add_Node_Name (Node : Node_Id) is
229 Kind : Node_Kind := Nkind (Node);
232 -- Just ignore an error node (someone else will give a message)
237 -- Otherwise see what kind of node we have
243 N_Defining_Identifier |
244 N_Defining_Operator_Symbol =>
246 -- Note: it is of course an error to have a defining
247 -- operator symbol at this point, but this is not where
248 -- the error is signalled, so we handle it nicely here!
250 Add_Name (Chars (Node));
252 when N_Defining_Program_Unit_Name =>
253 Add_Node_Name (Name (Node));
255 Add_Node_Name (Defining_Identifier (Node));
257 when N_Selected_Component |
259 Add_Node_Name (Prefix (Node));
261 Add_Node_Name (Selector_Name (Node));
263 when N_Subprogram_Specification |
264 N_Package_Specification =>
265 Add_Node_Name (Defining_Unit_Name (Node));
267 when N_Subprogram_Body |
268 N_Subprogram_Declaration |
269 N_Package_Declaration |
270 N_Generic_Declaration =>
271 Add_Node_Name (Specification (Node));
273 when N_Generic_Instantiation =>
274 Add_Node_Name (Defining_Unit_Name (Node));
276 when N_Package_Body =>
277 Add_Node_Name (Defining_Unit_Name (Node));
281 Add_Node_Name (Defining_Identifier (Node));
283 when N_Package_Renaming_Declaration =>
284 Add_Node_Name (Defining_Unit_Name (Node));
286 when N_Subprogram_Renaming_Declaration =>
287 Add_Node_Name (Specification (Node));
289 when N_Generic_Renaming_Declaration =>
290 Add_Node_Name (Defining_Unit_Name (Node));
292 when N_Subprogram_Body_Stub =>
293 Add_Node_Name (Get_Parent (Node));
295 Add_Node_Name (Specification (Node));
297 when N_Compilation_Unit =>
298 Add_Node_Name (Unit (Node));
300 when N_Package_Body_Stub =>
301 Add_Node_Name (Get_Parent (Node));
303 Add_Node_Name (Defining_Identifier (Node));
305 when N_Task_Body_Stub |
306 N_Protected_Body_Stub =>
307 Add_Node_Name (Get_Parent (Node));
309 Add_Node_Name (Defining_Identifier (Node));
312 Add_Node_Name (Name (Node));
314 Add_Node_Name (Proper_Body (Node));
316 when N_With_Clause =>
317 Add_Node_Name (Name (Node));
320 Add_Node_Name (Expression (First
321 (Pragma_Argument_Associations (Node))));
323 -- Tasks and protected stuff appear only in an error context,
324 -- but the error has been posted elsewhere, so we deal nicely
325 -- with these error situations here, and produce a reasonable
326 -- unit name using the defining identifier.
328 when N_Task_Type_Declaration |
329 N_Single_Task_Declaration |
330 N_Protected_Type_Declaration |
331 N_Single_Protected_Declaration =>
332 Add_Node_Name (Defining_Identifier (Node));
345 function Get_Parent (Node : Node_Id) return Node_Id is
349 while Nkind (N) /= N_Compilation_Unit loop
356 --------------------------------------------
357 -- Start of Processing for Get_Unit_Name --
358 --------------------------------------------
363 -- If we have Defining_Identifier, find the associated unit node
365 if Nkind (Node) = N_Defining_Identifier then
366 Node := Declaration_Node (Node);
368 -- If an expanded name, it is an already analyzed child unit, find
371 elsif Nkind (Node) = N_Expanded_Name then
372 Node := Declaration_Node (Entity (Node));
375 if Nkind (Node) = N_Package_Specification
376 or else Nkind (Node) in N_Subprogram_Specification
378 Node := Parent (Node);
381 -- Node points to the unit, so get its name and add proper suffix
383 Add_Node_Name (Node);
387 when N_Generic_Declaration |
388 N_Subprogram_Declaration |
389 N_Package_Declaration |
392 N_Generic_Instantiation |
393 N_Package_Renaming_Declaration |
394 N_Subprogram_Renaming_Declaration |
395 N_Generic_Renaming_Declaration |
396 N_Single_Task_Declaration |
397 N_Single_Protected_Declaration |
398 N_Task_Type_Declaration |
399 N_Protected_Type_Declaration =>
403 when N_Subprogram_Body |
410 N_Selected_Component =>
418 Name_Buffer (1 .. Unit_Name_Length) :=
419 Unit_Name_Buffer (1 .. Unit_Name_Length);
420 Name_Len := Unit_Name_Length;
425 --------------------------
426 -- Get_Unit_Name_String --
427 --------------------------
429 procedure Get_Unit_Name_String (N : Unit_Name_Type) is
430 Unit_Is_Body : Boolean;
433 Get_Decoded_Name_String (N);
434 Unit_Is_Body := Name_Buffer (Name_Len) = 'b';
435 Set_Casing (Identifier_Casing (Source_Index (Main_Unit)), Mixed_Case);
437 -- A special fudge, normally we don't have operator symbols present,
438 -- since it is always an error to do so. However, if we do, at this
439 -- stage it has the form:
443 -- and the %s or %b has already been eliminated so put 2 chars back
445 if Name_Buffer (1) = '"' then
446 Name_Len := Name_Len + 2;
449 -- Now adjust the %s or %b to (spec) or (body)
452 Name_Buffer (Name_Len - 1 .. Name_Len + 5) := " (body)";
454 Name_Buffer (Name_Len - 1 .. Name_Len + 5) := " (spec)";
457 for J in 1 .. Name_Len loop
458 if Name_Buffer (J) = '-' then
459 Name_Buffer (J) := '.';
463 Name_Len := Name_Len + (7 - 2);
464 end Get_Unit_Name_String;
470 function Is_Body_Name (N : Unit_Name_Type) return Boolean is
474 and then Name_Buffer (Name_Len - 1) = '%'
475 and then Name_Buffer (Name_Len) = 'b';
482 function Is_Child_Name (N : Unit_Name_Type) return Boolean is
489 while Name_Buffer (J) /= '.' loop
491 return False; -- not a child or subunit name
504 function Is_Spec_Name (N : Unit_Name_Type) return Boolean is
508 and then Name_Buffer (Name_Len - 1) = '%'
509 and then Name_Buffer (Name_Len) = 's';
512 -----------------------
513 -- Name_To_Unit_Name --
514 -----------------------
516 function Name_To_Unit_Name (N : Name_Id) return Unit_Name_Type is
519 Name_Buffer (Name_Len + 1) := '%';
520 Name_Buffer (Name_Len + 2) := 's';
521 Name_Len := Name_Len + 2;
523 end Name_To_Unit_Name;
530 (Old : Unit_Name_Type;
531 Newp : Unit_Name_Type)
532 return Unit_Name_Type
537 Get_Name_String (Old);
540 Child : String := Name_Buffer (1 .. Name_Len);
543 Get_Name_String (Newp);
544 Name_Len := Name_Len - 2;
547 while Child (P) /= '.' loop
551 while P <= Child'Last loop
552 Name_Len := Name_Len + 1;
553 Name_Buffer (Name_Len) := Child (P);
565 function Uname_Ge (Left, Right : Unit_Name_Type) return Boolean is
567 return Left = Right or else Uname_Gt (Left, Right);
574 function Uname_Gt (Left, Right : Unit_Name_Type) return Boolean is
576 return Left /= Right and then not Uname_Lt (Left, Right);
583 function Uname_Le (Left, Right : Unit_Name_Type) return Boolean is
585 return Left = Right or else Uname_Lt (Left, Right);
592 function Uname_Lt (Left, Right : Unit_Name_Type) return Boolean is
593 Left_Name : String (1 .. Hostparm.Max_Name_Length);
594 Left_Length : Natural;
595 Right_Name : String renames Name_Buffer;
596 Right_Length : Natural renames Name_Len;
600 pragma Warnings (Off, Right_Length);
601 -- Suppress warnings on Right_Length, used in pragma Assert
607 Get_Name_String (Left);
608 Left_Name (1 .. Name_Len + 1) := Name_Buffer (1 .. Name_Len + 1);
609 Left_Length := Name_Len;
610 Get_Name_String (Right);
614 exit when Left_Name (J) = '%';
616 if Right_Name (J) = '%' then
617 return False; -- left name is longer
620 pragma Assert (J <= Left_Length and then J <= Right_Length);
622 if Left_Name (J) /= Right_Name (J) then
623 return Left_Name (J) < Right_Name (J); -- parent names different
629 -- Come here pointing to % in left name
631 if Right_Name (J) /= '%' then
632 return True; -- right name is longer
635 -- Here the parent names are the same and specs sort low. If neither is
636 -- a spec, then we are comparing the same name and we want a result of
637 -- False in any case.
639 return Left_Name (J + 1) = 's';
642 ---------------------
643 -- Write_Unit_Name --
644 ---------------------
646 procedure Write_Unit_Name (N : Unit_Name_Type) is
648 Get_Unit_Name_String (N);
649 Write_Str (Name_Buffer (1 .. Name_Len));