Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / ada / exp_dbug.adb
blob2f1137b41cf5ab1db2a6b8892a0dde1c85f2134d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ D B U G --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1996-2007, 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 ------------------------------------------------------------------------------
26 with Alloc; use Alloc;
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Nlists; use Nlists;
31 with Nmake; use Nmake;
32 with Opt; use Opt;
33 with Output; use Output;
34 with Sem_Eval; use Sem_Eval;
35 with Sem_Util; use Sem_Util;
36 with Sinfo; use Sinfo;
37 with Stand; use Stand;
38 with Stringt; use Stringt;
39 with Table;
40 with Tbuild; use Tbuild;
41 with Urealp; use Urealp;
43 package body Exp_Dbug is
45 -- The following table is used to queue up the entities passed as
46 -- arguments to Qualify_Entity_Names for later processing when
47 -- Qualify_All_Entity_Names is called.
49 package Name_Qualify_Units is new Table.Table (
50 Table_Component_Type => Node_Id,
51 Table_Index_Type => Nat,
52 Table_Low_Bound => 1,
53 Table_Initial => Alloc.Name_Qualify_Units_Initial,
54 Table_Increment => Alloc.Name_Qualify_Units_Increment,
55 Table_Name => "Name_Qualify_Units");
57 --------------------------------
58 -- Use of Qualification Flags --
59 --------------------------------
61 -- There are two flags used to keep track of qualification of entities
63 -- Has_Fully_Qualified_Name
64 -- Has_Qualified_Name
66 -- The difference between these is as follows. Has_Qualified_Name is
67 -- set to indicate that the name has been qualified as required by the
68 -- spec of this package. As described there, this may involve the full
69 -- qualification for the name, but for some entities, notably procedure
70 -- local variables, this full qualification is not required.
72 -- The flag Has_Fully_Qualified_Name is set if indeed the name has been
73 -- fully qualified in the Ada sense. If Has_Fully_Qualified_Name is set,
74 -- then Has_Qualified_Name is also set, but the other way round is not
75 -- the case.
77 -- Consider the following example:
79 -- with ...
80 -- procedure X is
81 -- B : Ddd.Ttt;
82 -- procedure Y is ..
84 -- Here B is a procedure local variable, so it does not need fully
85 -- qualification. The flag Has_Qualified_Name will be set on the
86 -- first attempt to qualify B, to indicate that the job is done
87 -- and need not be redone.
89 -- But Y is qualified as x__y, since procedures are always fully
90 -- qualified, so the first time that an attempt is made to qualify
91 -- the name y, it will be replaced by x__y, and both flags are set.
93 -- Why the two flags? Well there are cases where we derive type names
94 -- from object names. As noted in the spec, type names are always
95 -- fully qualified. Suppose for example that the backend has to build
96 -- a padded type for variable B. then it will construct the PAD name
97 -- from B, but it requires full qualification, so the fully qualified
98 -- type name will be x__b___PAD. The two flags allow the circuit for
99 -- building this name to realize efficiently that b needs further
100 -- qualification.
102 --------------------
103 -- Homonym_Suffix --
104 --------------------
106 -- The string defined here (and its associated length) is used to
107 -- gather the homonym string that will be appended to Name_Buffer
108 -- when the name is complete. Strip_Suffixes appends to this string
109 -- as does Append_Homonym_Number, and Output_Homonym_Numbers_Suffix
110 -- appends the string to the end of Name_Buffer.
112 Homonym_Numbers : String (1 .. 256);
113 Homonym_Len : Natural := 0;
115 ----------------------
116 -- Local Procedures --
117 ----------------------
119 procedure Add_Uint_To_Buffer (U : Uint);
120 -- Add image of universal integer to Name_Buffer, updating Name_Len
122 procedure Add_Real_To_Buffer (U : Ureal);
123 -- Add nnn_ddd to Name_Buffer, where nnn and ddd are integer values of
124 -- the normalized numerator and denominator of the given real value.
126 procedure Append_Homonym_Number (E : Entity_Id);
127 -- If the entity E has homonyms in the same scope, then make an entry
128 -- in the Homonym_Numbers array, bumping Homonym_Count accordingly.
130 function Bounds_Match_Size (E : Entity_Id) return Boolean;
131 -- Determine whether the bounds of E match the size of the type. This is
132 -- used to determine whether encoding is required for a discrete type.
134 procedure Output_Homonym_Numbers_Suffix;
135 -- If homonym numbers are stored, then output them into Name_Buffer
137 procedure Prepend_String_To_Buffer (S : String);
138 -- Prepend given string to the contents of the string buffer, updating
139 -- the value in Name_Len (i.e. string is added at start of buffer).
141 procedure Prepend_Uint_To_Buffer (U : Uint);
142 -- Prepend image of universal integer to Name_Buffer, updating Name_Len
144 procedure Qualify_Entity_Name (Ent : Entity_Id);
145 -- If not already done, replaces the Chars field of the given entity
146 -- with the appropriate fully qualified name.
148 procedure Strip_Suffixes (BNPE_Suffix_Found : in out Boolean);
149 -- Given an qualified entity name in Name_Buffer, remove any plain X or
150 -- X{nb} qualification suffix. The contents of Name_Buffer is not changed
151 -- but Name_Len may be adjusted on return to remove the suffix. If a
152 -- BNPE suffix is found and stripped, then BNPE_Suffix_Found is set to
153 -- True. If no suffix is found, then BNPE_Suffix_Found is not modified.
154 -- This routine also searches for a homonym suffix, and if one is found
155 -- it is also stripped, and the entries are added to the global homonym
156 -- list (Homonym_Numbers) so that they can later be put back.
158 ------------------------
159 -- Add_Real_To_Buffer --
160 ------------------------
162 procedure Add_Real_To_Buffer (U : Ureal) is
163 begin
164 Add_Uint_To_Buffer (Norm_Num (U));
165 Add_Str_To_Name_Buffer ("_");
166 Add_Uint_To_Buffer (Norm_Den (U));
167 end Add_Real_To_Buffer;
169 ------------------------
170 -- Add_Uint_To_Buffer --
171 ------------------------
173 procedure Add_Uint_To_Buffer (U : Uint) is
174 begin
175 if U < 0 then
176 Add_Uint_To_Buffer (-U);
177 Add_Char_To_Name_Buffer ('m');
178 else
179 UI_Image (U, Decimal);
180 Add_Str_To_Name_Buffer (UI_Image_Buffer (1 .. UI_Image_Length));
181 end if;
182 end Add_Uint_To_Buffer;
184 ---------------------------
185 -- Append_Homonym_Number --
186 ---------------------------
188 procedure Append_Homonym_Number (E : Entity_Id) is
190 procedure Add_Nat_To_H (Nr : Nat);
191 -- Little procedure to append Nr to Homonym_Numbers
193 ------------------
194 -- Add_Nat_To_H --
195 ------------------
197 procedure Add_Nat_To_H (Nr : Nat) is
198 begin
199 if Nr >= 10 then
200 Add_Nat_To_H (Nr / 10);
201 end if;
203 Homonym_Len := Homonym_Len + 1;
204 Homonym_Numbers (Homonym_Len) :=
205 Character'Val (Nr mod 10 + Character'Pos ('0'));
206 end Add_Nat_To_H;
208 -- Start of processing for Append_Homonym_Number
210 begin
211 if Has_Homonym (E) then
212 declare
213 H : Entity_Id := Homonym (E);
214 Nr : Nat := 1;
216 begin
217 while Present (H) loop
218 if Scope (H) = Scope (E) then
219 Nr := Nr + 1;
220 end if;
222 H := Homonym (H);
223 end loop;
225 if Homonym_Len > 0 then
226 Homonym_Len := Homonym_Len + 1;
227 Homonym_Numbers (Homonym_Len) := '_';
228 end if;
230 Add_Nat_To_H (Nr);
231 end;
232 end if;
233 end Append_Homonym_Number;
235 -----------------------
236 -- Bounds_Match_Size --
237 -----------------------
239 function Bounds_Match_Size (E : Entity_Id) return Boolean is
240 Siz : Uint;
242 begin
243 if not Is_OK_Static_Subtype (E) then
244 return False;
246 elsif Is_Integer_Type (E)
247 and then Subtypes_Statically_Match (E, Base_Type (E))
248 then
249 return True;
251 -- Here we check if the static bounds match the natural size, which is
252 -- the size passed through with the debugging information. This is the
253 -- Esize rounded up to 8, 16, 32 or 64 as appropriate.
255 else
256 declare
257 Umark : constant Uintp.Save_Mark := Uintp.Mark;
258 Result : Boolean;
260 begin
261 if Esize (E) <= 8 then
262 Siz := Uint_8;
263 elsif Esize (E) <= 16 then
264 Siz := Uint_16;
265 elsif Esize (E) <= 32 then
266 Siz := Uint_32;
267 else
268 Siz := Uint_64;
269 end if;
271 if Is_Modular_Integer_Type (E) or else Is_Enumeration_Type (E) then
272 Result :=
273 Expr_Rep_Value (Type_Low_Bound (E)) = 0
274 and then
275 2 ** Siz - Expr_Rep_Value (Type_High_Bound (E)) = 1;
277 else
278 Result :=
279 Expr_Rep_Value (Type_Low_Bound (E)) + 2 ** (Siz - 1) = 0
280 and then
281 2 ** (Siz - 1) - Expr_Rep_Value (Type_High_Bound (E)) = 1;
282 end if;
284 Release (Umark);
285 return Result;
286 end;
287 end if;
288 end Bounds_Match_Size;
290 --------------------------------
291 -- Debug_Renaming_Declaration --
292 --------------------------------
294 function Debug_Renaming_Declaration (N : Node_Id) return Node_Id is
295 Loc : constant Source_Ptr := Sloc (N);
296 Ent : constant Node_Id := Defining_Entity (N);
297 Nam : constant Node_Id := Name (N);
298 Ren : Node_Id;
299 Typ : Entity_Id;
300 Obj : Entity_Id;
301 Res : Node_Id;
303 function Output_Subscript (N : Node_Id; S : String) return Boolean;
304 -- Outputs a single subscript value as ?nnn (subscript is compile time
305 -- known value with value nnn) or as ?e (subscript is local constant
306 -- with name e), where S supplies the proper string to use for ?.
307 -- Returns False if the subscript is not of an appropriate type to
308 -- output in one of these two forms. The result is prepended to the
309 -- name stored in Name_Buffer.
311 ----------------------
312 -- Output_Subscript --
313 ----------------------
315 function Output_Subscript (N : Node_Id; S : String) return Boolean is
316 begin
317 if Compile_Time_Known_Value (N) then
318 Prepend_Uint_To_Buffer (Expr_Value (N));
320 elsif Nkind (N) = N_Identifier
321 and then Scope (Entity (N)) = Scope (Ent)
322 and then Ekind (Entity (N)) = E_Constant
323 then
324 Prepend_String_To_Buffer (Get_Name_String (Chars (Entity (N))));
326 else
327 return False;
328 end if;
330 Prepend_String_To_Buffer (S);
331 return True;
332 end Output_Subscript;
334 -- Start of processing for Debug_Renaming_Declaration
336 begin
337 if not Comes_From_Source (N)
338 and then not Needs_Debug_Info (Ent)
339 then
340 return Empty;
341 end if;
343 -- Get renamed entity and compute suffix
345 Name_Len := 0;
346 Ren := Nam;
347 loop
348 case Nkind (Ren) is
350 when N_Identifier =>
351 exit;
353 when N_Expanded_Name =>
355 -- The entity field for an N_Expanded_Name is on the expanded
356 -- name node itself, so we are done here too.
358 exit;
360 when N_Selected_Component =>
361 Prepend_String_To_Buffer
362 (Get_Name_String (Chars (Selector_Name (Ren))));
363 Prepend_String_To_Buffer ("XR");
364 Ren := Prefix (Ren);
366 when N_Indexed_Component =>
367 declare
368 X : Node_Id := Last (Expressions (Ren));
370 begin
371 while Present (X) loop
372 if not Output_Subscript (X, "XS") then
373 Set_Materialize_Entity (Ent);
374 return Empty;
375 end if;
377 Prev (X);
378 end loop;
379 end;
381 Ren := Prefix (Ren);
383 when N_Slice =>
385 Typ := Etype (First_Index (Etype (Nam)));
387 if not Output_Subscript (Type_High_Bound (Typ), "XS") then
388 Set_Materialize_Entity (Ent);
389 return Empty;
390 end if;
392 if not Output_Subscript (Type_Low_Bound (Typ), "XL") then
393 Set_Materialize_Entity (Ent);
394 return Empty;
395 end if;
397 Ren := Prefix (Ren);
399 when N_Explicit_Dereference =>
400 Set_Materialize_Entity (Ent);
401 Prepend_String_To_Buffer ("XA");
402 Ren := Prefix (Ren);
404 -- For now, anything else simply results in no translation
406 when others =>
407 Set_Materialize_Entity (Ent);
408 return Empty;
409 end case;
410 end loop;
412 Prepend_String_To_Buffer ("___XE");
414 -- Include the designation of the form of renaming
416 case Nkind (N) is
417 when N_Object_Renaming_Declaration =>
418 Prepend_String_To_Buffer ("___XR");
420 when N_Exception_Renaming_Declaration =>
421 Prepend_String_To_Buffer ("___XRE");
423 when N_Package_Renaming_Declaration =>
424 Prepend_String_To_Buffer ("___XRP");
426 when others =>
427 return Empty;
428 end case;
430 -- Add the name of the renaming entity to the front
432 Prepend_String_To_Buffer (Get_Name_String (Chars (Ent)));
434 -- If it is a child unit create a fully qualified name, to disambiguate
435 -- multiple child units with the same name and different parents.
437 if Nkind (N) = N_Package_Renaming_Declaration
438 and then Is_Child_Unit (Ent)
439 then
440 Prepend_String_To_Buffer ("__");
441 Prepend_String_To_Buffer
442 (Get_Name_String (Chars (Scope (Ent))));
443 end if;
445 -- Create the special object whose name is the debug encoding for the
446 -- renaming declaration.
448 -- For now, the object name contains the suffix encoding for the renamed
449 -- object, but not the name of the leading entity. The object is linked
450 -- the renamed entity using the Debug_Renaming_Link field. Then the
451 -- Qualify_Entity_Name procedure uses this link to create the proper
452 -- fully qualified name.
454 -- The reason we do things this way is that we really need to copy the
455 -- qualification of the renamed entity, and it is really much easier to
456 -- do this after the renamed entity has itself been fully qualified.
458 Obj := Make_Defining_Identifier (Loc, Chars => Name_Enter);
459 Res :=
460 Make_Object_Declaration (Loc,
461 Defining_Identifier => Obj,
462 Object_Definition => New_Reference_To
463 (Standard_Debug_Renaming_Type, Loc));
465 Set_Debug_Renaming_Link (Obj, Entity (Ren));
467 Set_Needs_Debug_Info (Obj);
469 -- Mark the object as internal so that it won't be initialized when
470 -- pragma Initialize_Scalars or Normalize_Scalars is in use.
472 Set_Is_Internal (Obj);
474 return Res;
476 -- If we get an exception, just figure it is a case that we cannot
477 -- successfully handle using our current approach, since this is
478 -- only for debugging, no need to take the compilation with us!
480 exception
481 when others =>
482 return Make_Null_Statement (Loc);
483 end Debug_Renaming_Declaration;
485 ----------------------
486 -- Get_Encoded_Name --
487 ----------------------
489 -- Note: see spec for details on encodings
491 procedure Get_Encoded_Name (E : Entity_Id) is
492 Has_Suffix : Boolean;
494 begin
495 -- If not generating code, there is no need to create encoded names, and
496 -- problems when the back-end is called to annotate types without full
497 -- code generation. See comments in Get_External_Name_With_Suffix for
498 -- additional details.
500 -- However we do create encoded names if the back end is active, even
501 -- if Operating_Mode got reset. Otherwise any serious error reported
502 -- by the backend calling Error_Msg changes the Compilation_Mode to
503 -- Check_Semantics, which disables the functionality of this routine,
504 -- causing the generation of spurious additional errors.
506 -- Couldn't we just test Original_Operating_Mode here? ???
508 if Operating_Mode /= Generate_Code
509 and then not Generating_Code
510 then
511 return;
512 end if;
514 Get_Name_String (Chars (E));
516 -- Nothing to do if we do not have a type
518 if not Is_Type (E)
520 -- Or if this is an enumeration base type
522 or else (Is_Enumeration_Type (E)
523 and then E = Base_Type (E))
525 -- Or if this is a dummy type for a renaming
527 or else (Name_Len >= 3 and then
528 Name_Buffer (Name_Len - 2 .. Name_Len) = "_XR")
530 or else (Name_Len >= 4 and then
531 (Name_Buffer (Name_Len - 3 .. Name_Len) = "_XRE"
532 or else
533 Name_Buffer (Name_Len - 3 .. Name_Len) = "_XRP"))
535 -- For all these cases, just return the name unchanged
537 then
538 Name_Buffer (Name_Len + 1) := ASCII.Nul;
539 return;
540 end if;
542 Has_Suffix := True;
544 -- Fixed-point case
546 if Is_Fixed_Point_Type (E) then
547 Get_External_Name_With_Suffix (E, "XF_");
548 Add_Real_To_Buffer (Delta_Value (E));
550 if Small_Value (E) /= Delta_Value (E) then
551 Add_Str_To_Name_Buffer ("_");
552 Add_Real_To_Buffer (Small_Value (E));
553 end if;
555 -- Vax floating-point case
557 elsif Vax_Float (E) then
558 if Digits_Value (Base_Type (E)) = 6 then
559 Get_External_Name_With_Suffix (E, "XFF");
561 elsif Digits_Value (Base_Type (E)) = 9 then
562 Get_External_Name_With_Suffix (E, "XFF");
564 else
565 pragma Assert (Digits_Value (Base_Type (E)) = 15);
566 Get_External_Name_With_Suffix (E, "XFG");
567 end if;
569 -- Discrete case where bounds do not match size
571 elsif Is_Discrete_Type (E)
572 and then not Bounds_Match_Size (E)
573 then
574 declare
575 Lo : constant Node_Id := Type_Low_Bound (E);
576 Hi : constant Node_Id := Type_High_Bound (E);
578 Lo_Con : constant Boolean := Compile_Time_Known_Value (Lo);
579 Hi_Con : constant Boolean := Compile_Time_Known_Value (Hi);
581 Lo_Discr : constant Boolean :=
582 Nkind (Lo) = N_Identifier
583 and then
584 Ekind (Entity (Lo)) = E_Discriminant;
586 Hi_Discr : constant Boolean :=
587 Nkind (Hi) = N_Identifier
588 and then
589 Ekind (Entity (Hi)) = E_Discriminant;
591 Lo_Encode : constant Boolean := Lo_Con or Lo_Discr;
592 Hi_Encode : constant Boolean := Hi_Con or Hi_Discr;
594 Biased : constant Boolean := Has_Biased_Representation (E);
596 begin
597 if Biased then
598 Get_External_Name_With_Suffix (E, "XB");
599 else
600 Get_External_Name_With_Suffix (E, "XD");
601 end if;
603 if Lo_Encode or Hi_Encode then
604 if Biased then
605 Add_Str_To_Name_Buffer ("_");
606 else
607 if Lo_Encode then
608 if Hi_Encode then
609 Add_Str_To_Name_Buffer ("LU_");
610 else
611 Add_Str_To_Name_Buffer ("L_");
612 end if;
613 else
614 Add_Str_To_Name_Buffer ("U_");
615 end if;
616 end if;
618 if Lo_Con then
619 Add_Uint_To_Buffer (Expr_Rep_Value (Lo));
620 elsif Lo_Discr then
621 Get_Name_String_And_Append (Chars (Entity (Lo)));
622 end if;
624 if Lo_Encode and Hi_Encode then
625 Add_Str_To_Name_Buffer ("__");
626 end if;
628 if Hi_Con then
629 Add_Uint_To_Buffer (Expr_Rep_Value (Hi));
630 elsif Hi_Discr then
631 Get_Name_String_And_Append (Chars (Entity (Hi)));
632 end if;
633 end if;
634 end;
636 -- For all other cases, the encoded name is the normal type name
638 else
639 Has_Suffix := False;
640 Get_External_Name (E, Has_Suffix);
641 end if;
643 if Debug_Flag_B and then Has_Suffix then
644 Write_Str ("**** type ");
645 Write_Name (Chars (E));
646 Write_Str (" is encoded as ");
647 Write_Str (Name_Buffer (1 .. Name_Len));
648 Write_Eol;
649 end if;
651 Name_Buffer (Name_Len + 1) := ASCII.NUL;
652 end Get_Encoded_Name;
654 -----------------------
655 -- Get_External_Name --
656 -----------------------
658 procedure Get_External_Name (Entity : Entity_Id; Has_Suffix : Boolean) is
659 E : Entity_Id := Entity;
660 Kind : Entity_Kind;
662 procedure Get_Qualified_Name_And_Append (Entity : Entity_Id);
663 -- Appends fully qualified name of given entity to Name_Buffer
665 -----------------------------------
666 -- Get_Qualified_Name_And_Append --
667 -----------------------------------
669 procedure Get_Qualified_Name_And_Append (Entity : Entity_Id) is
670 begin
671 -- If the entity is a compilation unit, its scope is Standard,
672 -- there is no outer scope, and the no further qualification
673 -- is required.
675 -- If the front end has already computed a fully qualified name,
676 -- then it is also the case that no further qualification is
677 -- required.
679 if Present (Scope (Scope (Entity)))
680 and then not Has_Fully_Qualified_Name (Entity)
681 then
682 Get_Qualified_Name_And_Append (Scope (Entity));
683 Add_Str_To_Name_Buffer ("__");
684 Get_Name_String_And_Append (Chars (Entity));
685 Append_Homonym_Number (Entity);
687 else
688 Get_Name_String_And_Append (Chars (Entity));
689 end if;
690 end Get_Qualified_Name_And_Append;
692 -- Start of processing for Get_External_Name
694 begin
695 Name_Len := 0;
696 Homonym_Len := 0;
698 -- If this is a child unit, we want the child
700 if Nkind (E) = N_Defining_Program_Unit_Name then
701 E := Defining_Identifier (Entity);
702 end if;
704 Kind := Ekind (E);
706 -- Case of interface name being used
708 if (Kind = E_Procedure or else
709 Kind = E_Function or else
710 Kind = E_Constant or else
711 Kind = E_Variable or else
712 Kind = E_Exception)
713 and then Present (Interface_Name (E))
714 and then No (Address_Clause (E))
715 and then not Has_Suffix
716 then
717 Add_String_To_Name_Buffer (Strval (Interface_Name (E)));
719 -- All other cases besides the interface name case
721 else
722 -- If this is a library level subprogram (i.e. a subprogram that is a
723 -- compilation unit other than a subunit), then we prepend _ada_ to
724 -- ensure distinctions required as described in the spec.
726 -- Check explicitly for child units, because those are not flagged
727 -- as Compilation_Units by lib. Should they be ???
729 if Is_Subprogram (E)
730 and then (Is_Compilation_Unit (E) or Is_Child_Unit (E))
731 and then not Has_Suffix
732 then
733 Add_Str_To_Name_Buffer ("_ada_");
734 end if;
736 -- If the entity is a subprogram instance that is not a compilation
737 -- unit, generate the name of the original Ada entity, which is the
738 -- one gdb needs.
740 if Is_Generic_Instance (E)
741 and then Is_Subprogram (E)
742 and then not Is_Compilation_Unit (Scope (E))
743 and then (Ekind (Scope (E)) = E_Package
744 or else
745 Ekind (Scope (E)) = E_Package_Body)
746 and then Present (Related_Instance (Scope (E)))
747 then
748 E := Related_Instance (Scope (E));
749 end if;
751 Get_Qualified_Name_And_Append (E);
752 end if;
754 Name_Buffer (Name_Len + 1) := ASCII.Nul;
755 end Get_External_Name;
757 -----------------------------------
758 -- Get_External_Name_With_Suffix --
759 -----------------------------------
761 procedure Get_External_Name_With_Suffix
762 (Entity : Entity_Id;
763 Suffix : String)
765 Has_Suffix : constant Boolean := (Suffix /= "");
767 begin
768 -- If we are not in code generation mode, this procedure may still be
769 -- called from Back_End (more specifically - from gigi for doing type
770 -- representation annotation or some representation-specific checks).
771 -- But in this mode there is no need to mess with external names.
773 -- Furthermore, the call causes difficulties in this case because the
774 -- string representing the homonym number is not correctly reset as a
775 -- part of the call to Output_Homonym_Numbers_Suffix (which is not
776 -- called in gigi).
778 if Operating_Mode /= Generate_Code then
779 return;
780 end if;
782 Get_External_Name (Entity, Has_Suffix);
784 if Has_Suffix then
785 Add_Str_To_Name_Buffer ("___");
786 Add_Str_To_Name_Buffer (Suffix);
787 Name_Buffer (Name_Len + 1) := ASCII.Nul;
788 end if;
789 end Get_External_Name_With_Suffix;
791 --------------------------
792 -- Get_Variant_Encoding --
793 --------------------------
795 procedure Get_Variant_Encoding (V : Node_Id) is
796 Choice : Node_Id;
798 procedure Choice_Val (Typ : Character; Choice : Node_Id);
799 -- Output encoded value for a single choice value. Typ is the key
800 -- character ('S', 'F', or 'T') that precedes the choice value.
802 ----------------
803 -- Choice_Val --
804 ----------------
806 procedure Choice_Val (Typ : Character; Choice : Node_Id) is
807 begin
808 if Nkind (Choice) = N_Integer_Literal then
809 Add_Char_To_Name_Buffer (Typ);
810 Add_Uint_To_Buffer (Intval (Choice));
812 -- Character literal with no entity present (this is the case
813 -- Standard.Character or Standard.Wide_Character as root type)
815 elsif Nkind (Choice) = N_Character_Literal
816 and then No (Entity (Choice))
817 then
818 Add_Char_To_Name_Buffer (Typ);
819 Add_Uint_To_Buffer (Char_Literal_Value (Choice));
821 else
822 declare
823 Ent : constant Entity_Id := Entity (Choice);
825 begin
826 if Ekind (Ent) = E_Enumeration_Literal then
827 Add_Char_To_Name_Buffer (Typ);
828 Add_Uint_To_Buffer (Enumeration_Rep (Ent));
830 else
831 pragma Assert (Ekind (Ent) = E_Constant);
832 Choice_Val (Typ, Constant_Value (Ent));
833 end if;
834 end;
835 end if;
836 end Choice_Val;
838 -- Start of processing for Get_Variant_Encoding
840 begin
841 Name_Len := 0;
843 Choice := First (Discrete_Choices (V));
844 while Present (Choice) loop
845 if Nkind (Choice) = N_Others_Choice then
846 Add_Char_To_Name_Buffer ('O');
848 elsif Nkind (Choice) = N_Range then
849 Choice_Val ('R', Low_Bound (Choice));
850 Choice_Val ('T', High_Bound (Choice));
852 elsif Is_Entity_Name (Choice)
853 and then Is_Type (Entity (Choice))
854 then
855 Choice_Val ('R', Type_Low_Bound (Entity (Choice)));
856 Choice_Val ('T', Type_High_Bound (Entity (Choice)));
858 elsif Nkind (Choice) = N_Subtype_Indication then
859 declare
860 Rang : constant Node_Id :=
861 Range_Expression (Constraint (Choice));
862 begin
863 Choice_Val ('R', Low_Bound (Rang));
864 Choice_Val ('T', High_Bound (Rang));
865 end;
867 else
868 Choice_Val ('S', Choice);
869 end if;
871 Next (Choice);
872 end loop;
874 Name_Buffer (Name_Len + 1) := ASCII.NUL;
876 if Debug_Flag_B then
877 declare
878 VP : constant Node_Id := Parent (V); -- Variant_Part
879 CL : constant Node_Id := Parent (VP); -- Component_List
880 RD : constant Node_Id := Parent (CL); -- Record_Definition
881 FT : constant Node_Id := Parent (RD); -- Full_Type_Declaration
883 begin
884 Write_Str ("**** variant for type ");
885 Write_Name (Chars (Defining_Identifier (FT)));
886 Write_Str (" is encoded as ");
887 Write_Str (Name_Buffer (1 .. Name_Len));
888 Write_Eol;
889 end;
890 end if;
891 end Get_Variant_Encoding;
893 ------------------------------------
894 -- Get_Secondary_DT_External_Name --
895 ------------------------------------
897 procedure Get_Secondary_DT_External_Name
898 (Typ : Entity_Id;
899 Ancestor_Typ : Entity_Id;
900 Suffix_Index : Int)
902 begin
903 Get_External_Name (Typ, Has_Suffix => False);
905 if Ancestor_Typ /= Typ then
906 declare
907 Len : constant Natural := Name_Len;
908 Save_Str : constant String (1 .. Name_Len)
909 := Name_Buffer (1 .. Name_Len);
910 begin
911 Get_External_Name (Ancestor_Typ, Has_Suffix => False);
913 -- Append the extended name of the ancestor to the
914 -- extended name of Typ
916 Name_Buffer (Len + 2 .. Len + Name_Len + 1) :=
917 Name_Buffer (1 .. Name_Len);
918 Name_Buffer (1 .. Len) := Save_Str;
919 Name_Buffer (Len + 1) := '_';
920 Name_Len := Len + Name_Len + 1;
921 end;
922 end if;
924 Add_Nat_To_Name_Buffer (Suffix_Index);
925 end Get_Secondary_DT_External_Name;
927 ---------------------------------
928 -- Make_Packed_Array_Type_Name --
929 ---------------------------------
931 function Make_Packed_Array_Type_Name
932 (Typ : Entity_Id;
933 Csize : Uint)
934 return Name_Id
936 begin
937 Get_Name_String (Chars (Typ));
938 Add_Str_To_Name_Buffer ("___XP");
939 Add_Uint_To_Buffer (Csize);
940 return Name_Find;
941 end Make_Packed_Array_Type_Name;
943 -----------------------------------
944 -- Output_Homonym_Numbers_Suffix --
945 -----------------------------------
947 procedure Output_Homonym_Numbers_Suffix is
948 J : Natural;
950 begin
951 if Homonym_Len > 0 then
953 -- Check for all 1's, in which case we do not output
955 J := 1;
956 loop
957 exit when Homonym_Numbers (J) /= '1';
959 -- If we reached end of string we do not output
961 if J = Homonym_Len then
962 Homonym_Len := 0;
963 return;
964 end if;
966 exit when Homonym_Numbers (J + 1) /= '_';
967 J := J + 2;
968 end loop;
970 -- If we exit the loop then suffix must be output
972 Add_Str_To_Name_Buffer ("__");
973 Add_Str_To_Name_Buffer (Homonym_Numbers (1 .. Homonym_Len));
974 Homonym_Len := 0;
975 end if;
976 end Output_Homonym_Numbers_Suffix;
978 ------------------------------
979 -- Prepend_String_To_Buffer --
980 ------------------------------
982 procedure Prepend_String_To_Buffer (S : String) is
983 N : constant Integer := S'Length;
984 begin
985 Name_Buffer (1 + N .. Name_Len + N) := Name_Buffer (1 .. Name_Len);
986 Name_Buffer (1 .. N) := S;
987 Name_Len := Name_Len + N;
988 end Prepend_String_To_Buffer;
990 ----------------------------
991 -- Prepend_Uint_To_Buffer --
992 ----------------------------
994 procedure Prepend_Uint_To_Buffer (U : Uint) is
995 begin
996 if U < 0 then
997 Prepend_String_To_Buffer ("m");
998 Prepend_Uint_To_Buffer (-U);
999 else
1000 UI_Image (U, Decimal);
1001 Prepend_String_To_Buffer (UI_Image_Buffer (1 .. UI_Image_Length));
1002 end if;
1003 end Prepend_Uint_To_Buffer;
1005 ------------------------------
1006 -- Qualify_All_Entity_Names --
1007 ------------------------------
1009 procedure Qualify_All_Entity_Names is
1010 E : Entity_Id;
1011 Ent : Entity_Id;
1013 begin
1014 for J in Name_Qualify_Units.First .. Name_Qualify_Units.Last loop
1015 E := Defining_Entity (Name_Qualify_Units.Table (J));
1016 Qualify_Entity_Name (E);
1018 -- Normally entities in the qualification list are scopes, but in the
1019 -- case of a library-level package renaming there is an associated
1020 -- variable that encodes the debugger name and that variable is
1021 -- entered in the list since it occurs in the Aux_Decls list of the
1022 -- compilation and doesn't have a normal scope.
1024 if Ekind (E) /= E_Variable then
1025 Ent := First_Entity (E);
1026 while Present (Ent) loop
1027 Qualify_Entity_Name (Ent);
1028 Next_Entity (Ent);
1030 -- There are odd cases where Last_Entity (E) = E. This happens
1031 -- in the case of renaming of packages. This test avoids
1032 -- getting stuck in such cases.
1034 exit when Ent = E;
1035 end loop;
1036 end if;
1037 end loop;
1038 end Qualify_All_Entity_Names;
1040 -------------------------
1041 -- Qualify_Entity_Name --
1042 -------------------------
1044 procedure Qualify_Entity_Name (Ent : Entity_Id) is
1046 Full_Qualify_Name : String (1 .. Name_Buffer'Length);
1047 Full_Qualify_Len : Natural := 0;
1048 -- Used to accumulate fully qualified name of subprogram
1050 procedure Fully_Qualify_Name (E : Entity_Id);
1051 -- Used to qualify a subprogram or type name, where full
1052 -- qualification up to Standard is always used. Name is set
1053 -- in Full_Qualify_Name with the length in Full_Qualify_Len.
1054 -- Note that this routine does not prepend the _ada_ string
1055 -- required for library subprograms (this is done in the back end).
1057 function Is_BNPE (S : Entity_Id) return Boolean;
1058 -- Determines if S is a BNPE, i.e. Body-Nested Package Entity, which
1059 -- is defined to be a package which is immediately nested within a
1060 -- package body.
1062 function Qualify_Needed (S : Entity_Id) return Boolean;
1063 -- Given a scope, determines if the scope is to be included in the
1064 -- fully qualified name, True if so, False if not.
1066 procedure Set_BNPE_Suffix (E : Entity_Id);
1067 -- Recursive routine to append the BNPE qualification suffix. Works
1068 -- from right to left with E being the current entity in the list.
1069 -- The result does NOT have the trailing n's and trailing b stripped.
1070 -- The caller must do this required stripping.
1072 procedure Set_Entity_Name (E : Entity_Id);
1073 -- Internal recursive routine that does most of the work. This routine
1074 -- leaves the result sitting in Name_Buffer and Name_Len.
1076 BNPE_Suffix_Needed : Boolean := False;
1077 -- Set true if a body-nested package entity suffix is required
1079 Save_Chars : constant Name_Id := Chars (Ent);
1080 -- Save original name
1082 ------------------------
1083 -- Fully_Qualify_Name --
1084 ------------------------
1086 procedure Fully_Qualify_Name (E : Entity_Id) is
1087 Discard : Boolean := False;
1089 begin
1090 -- Ignore empty entry (can happen in error cases)
1092 if No (E) then
1093 return;
1095 -- If this we are qualifying entities local to a generic
1096 -- instance, use the name of the original instantiation,
1097 -- not that of the anonymous subprogram in the wrapper
1098 -- package, so that gdb doesn't have to know about these.
1100 elsif Is_Generic_Instance (E)
1101 and then Is_Subprogram (E)
1102 and then not Comes_From_Source (E)
1103 and then not Is_Compilation_Unit (Scope (E))
1104 then
1105 Fully_Qualify_Name (Related_Instance (Scope (E)));
1106 return;
1107 end if;
1109 -- If we reached fully qualified name, then just copy it
1111 if Has_Fully_Qualified_Name (E) then
1112 Get_Name_String (Chars (E));
1113 Strip_Suffixes (Discard);
1114 Full_Qualify_Name (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
1115 Full_Qualify_Len := Name_Len;
1116 Set_Has_Fully_Qualified_Name (Ent);
1118 -- Case of non-fully qualified name
1120 else
1121 if Scope (E) = Standard_Standard then
1122 Set_Has_Fully_Qualified_Name (Ent);
1123 else
1124 Fully_Qualify_Name (Scope (E));
1125 Full_Qualify_Name (Full_Qualify_Len + 1) := '_';
1126 Full_Qualify_Name (Full_Qualify_Len + 2) := '_';
1127 Full_Qualify_Len := Full_Qualify_Len + 2;
1128 end if;
1130 if Has_Qualified_Name (E) then
1131 Get_Unqualified_Name_String (Chars (E));
1132 else
1133 Get_Name_String (Chars (E));
1134 end if;
1136 -- Here we do one step of the qualification
1138 Full_Qualify_Name
1139 (Full_Qualify_Len + 1 .. Full_Qualify_Len + Name_Len) :=
1140 Name_Buffer (1 .. Name_Len);
1141 Full_Qualify_Len := Full_Qualify_Len + Name_Len;
1142 Append_Homonym_Number (E);
1143 end if;
1145 if Is_BNPE (E) then
1146 BNPE_Suffix_Needed := True;
1147 end if;
1148 end Fully_Qualify_Name;
1150 -------------
1151 -- Is_BNPE --
1152 -------------
1154 function Is_BNPE (S : Entity_Id) return Boolean is
1155 begin
1156 return
1157 Ekind (S) = E_Package
1158 and then Is_Package_Body_Entity (S);
1159 end Is_BNPE;
1161 --------------------
1162 -- Qualify_Needed --
1163 --------------------
1165 function Qualify_Needed (S : Entity_Id) return Boolean is
1166 begin
1167 -- If we got all the way to Standard, then we have certainly
1168 -- fully qualified the name, so set the flag appropriately,
1169 -- and then return False, since we are most certainly done!
1171 if S = Standard_Standard then
1172 Set_Has_Fully_Qualified_Name (Ent, True);
1173 return False;
1175 -- Otherwise figure out if further qualification is required
1177 else
1178 return
1179 Is_Subprogram (Ent)
1180 or else
1181 Ekind (Ent) = E_Subprogram_Body
1182 or else
1183 (Ekind (S) /= E_Block
1184 and then not Is_Dynamic_Scope (S));
1185 end if;
1186 end Qualify_Needed;
1188 ---------------------
1189 -- Set_BNPE_Suffix --
1190 ---------------------
1192 procedure Set_BNPE_Suffix (E : Entity_Id) is
1193 S : constant Entity_Id := Scope (E);
1195 begin
1196 if Qualify_Needed (S) then
1197 Set_BNPE_Suffix (S);
1199 if Is_BNPE (E) then
1200 Add_Char_To_Name_Buffer ('b');
1201 else
1202 Add_Char_To_Name_Buffer ('n');
1203 end if;
1205 else
1206 Add_Char_To_Name_Buffer ('X');
1207 end if;
1208 end Set_BNPE_Suffix;
1210 ---------------------
1211 -- Set_Entity_Name --
1212 ---------------------
1214 procedure Set_Entity_Name (E : Entity_Id) is
1215 S : constant Entity_Id := Scope (E);
1217 begin
1218 -- If we reach an already qualified name, just take the encoding
1219 -- except that we strip the package body suffixes, since these
1220 -- will be separately put on later.
1222 if Has_Qualified_Name (E) then
1223 Get_Name_String_And_Append (Chars (E));
1224 Strip_Suffixes (BNPE_Suffix_Needed);
1226 -- If the top level name we are adding is itself fully
1227 -- qualified, then that means that the name that we are
1228 -- preparing for the Fully_Qualify_Name call will also
1229 -- generate a fully qualified name.
1231 if Has_Fully_Qualified_Name (E) then
1232 Set_Has_Fully_Qualified_Name (Ent);
1233 end if;
1235 -- Case where upper level name is not encoded yet
1237 else
1238 -- Recurse if further qualification required
1240 if Qualify_Needed (S) then
1241 Set_Entity_Name (S);
1242 Add_Str_To_Name_Buffer ("__");
1243 end if;
1245 -- Otherwise get name and note if it is a NPBE
1247 Get_Name_String_And_Append (Chars (E));
1249 if Is_BNPE (E) then
1250 BNPE_Suffix_Needed := True;
1251 end if;
1253 Append_Homonym_Number (E);
1254 end if;
1255 end Set_Entity_Name;
1257 -- Start of processing for Qualify_Entity_Name
1259 begin
1260 if Has_Qualified_Name (Ent) then
1261 return;
1263 -- If the entity is a variable encoding the debug name for an object
1264 -- renaming, then the qualified name of the entity associated with the
1265 -- renamed object can now be incorporated in the debug name.
1267 elsif Ekind (Ent) = E_Variable
1268 and then Present (Debug_Renaming_Link (Ent))
1269 then
1270 Name_Len := 0;
1271 Qualify_Entity_Name (Debug_Renaming_Link (Ent));
1272 Get_Name_String (Chars (Ent));
1274 -- Retrieve the now-qualified name of the renamed entity and insert
1275 -- it in the middle of the name, just preceding the suffix encoding
1276 -- describing the renamed object.
1278 declare
1279 Renamed_Id : constant String :=
1280 Get_Name_String (Chars (Debug_Renaming_Link (Ent)));
1281 Insert_Len : constant Integer := Renamed_Id'Length + 1;
1282 Index : Natural := Name_Len - 3;
1284 begin
1285 -- Loop backwards through the name to find the start of the "___"
1286 -- sequence associated with the suffix.
1288 while Index >= Name_Buffer'First
1289 and then (Name_Buffer (Index + 1) /= '_'
1290 or else Name_Buffer (Index + 2) /= '_'
1291 or else Name_Buffer (Index + 3) /= '_')
1292 loop
1293 Index := Index - 1;
1294 end loop;
1296 pragma Assert (Name_Buffer (Index + 1 .. Index + 3) = "___");
1298 -- Insert an underscore separator and the entity name just in
1299 -- front of the suffix.
1301 Name_Buffer (Index + 1 + Insert_Len .. Name_Len + Insert_Len) :=
1302 Name_Buffer (Index + 1 .. Name_Len);
1303 Name_Buffer (Index + 1) := '_';
1304 Name_Buffer (Index + 2 .. Index + Insert_Len) := Renamed_Id;
1305 Name_Len := Name_Len + Insert_Len;
1306 end;
1308 -- Reset the name of the variable to the new name that includes the
1309 -- name of the renamed entity.
1311 Set_Chars (Ent, Name_Enter);
1313 -- If the entity needs qualification by its scope then develop it
1314 -- here, add the variable's name, and again reset the entity name.
1316 if Qualify_Needed (Scope (Ent)) then
1317 Name_Len := 0;
1318 Set_Entity_Name (Scope (Ent));
1319 Add_Str_To_Name_Buffer ("__");
1321 Get_Name_String_And_Append (Chars (Ent));
1323 Set_Chars (Ent, Name_Enter);
1324 end if;
1326 Set_Has_Qualified_Name (Ent);
1327 return;
1329 elsif Is_Subprogram (Ent)
1330 or else Ekind (Ent) = E_Subprogram_Body
1331 or else Is_Type (Ent)
1332 then
1333 Fully_Qualify_Name (Ent);
1334 Name_Len := Full_Qualify_Len;
1335 Name_Buffer (1 .. Name_Len) := Full_Qualify_Name (1 .. Name_Len);
1337 elsif Qualify_Needed (Scope (Ent)) then
1338 Name_Len := 0;
1339 Set_Entity_Name (Ent);
1341 else
1342 Set_Has_Qualified_Name (Ent);
1343 return;
1344 end if;
1346 -- Fall through with a fully qualified name in Name_Buffer/Name_Len
1348 Output_Homonym_Numbers_Suffix;
1350 -- Add body-nested package suffix if required
1352 if BNPE_Suffix_Needed
1353 and then Ekind (Ent) /= E_Enumeration_Literal
1354 then
1355 Set_BNPE_Suffix (Ent);
1357 -- Strip trailing n's and last trailing b as required. note that
1358 -- we know there is at least one b, or no suffix would be generated.
1360 while Name_Buffer (Name_Len) = 'n' loop
1361 Name_Len := Name_Len - 1;
1362 end loop;
1364 Name_Len := Name_Len - 1;
1365 end if;
1367 Set_Chars (Ent, Name_Enter);
1368 Set_Has_Qualified_Name (Ent);
1370 if Debug_Flag_BB then
1371 Write_Str ("*** ");
1372 Write_Name (Save_Chars);
1373 Write_Str (" qualified as ");
1374 Write_Name (Chars (Ent));
1375 Write_Eol;
1376 end if;
1377 end Qualify_Entity_Name;
1379 --------------------------
1380 -- Qualify_Entity_Names --
1381 --------------------------
1383 procedure Qualify_Entity_Names (N : Node_Id) is
1384 begin
1385 Name_Qualify_Units.Append (N);
1386 end Qualify_Entity_Names;
1388 --------------------
1389 -- Strip_Suffixes --
1390 --------------------
1392 procedure Strip_Suffixes (BNPE_Suffix_Found : in out Boolean) is
1393 SL : Natural;
1395 pragma Warnings (Off, BNPE_Suffix_Found);
1396 -- Since this procedure only ever sets the flag
1398 begin
1399 -- Search for and strip BNPE suffix
1401 for J in reverse 2 .. Name_Len loop
1402 if Name_Buffer (J) = 'X' then
1403 Name_Len := J - 1;
1404 BNPE_Suffix_Found := True;
1405 exit;
1406 end if;
1408 exit when Name_Buffer (J) /= 'b' and then Name_Buffer (J) /= 'n';
1409 end loop;
1411 -- Search for and strip homonym numbers suffix
1413 for J in reverse 2 .. Name_Len - 2 loop
1414 if Name_Buffer (J) = '_'
1415 and then Name_Buffer (J + 1) = '_'
1416 then
1417 if Name_Buffer (J + 2) in '0' .. '9' then
1418 if Homonym_Len > 0 then
1419 Homonym_Len := Homonym_Len + 1;
1420 Homonym_Numbers (Homonym_Len) := '-';
1421 end if;
1423 SL := Name_Len - (J + 1);
1425 Homonym_Numbers (Homonym_Len + 1 .. Homonym_Len + SL) :=
1426 Name_Buffer (J + 2 .. Name_Len);
1427 Name_Len := J - 1;
1428 Homonym_Len := Homonym_Len + SL;
1429 end if;
1431 exit;
1432 end if;
1433 end loop;
1434 end Strip_Suffixes;
1436 end Exp_Dbug;