Daily bump.
[official-gcc.git] / gcc / ada / exp_dbug.adb
blobdc1f884d52517460054a8d9cd0468a33abc16d86
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-2017, 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_Aux; use Sem_Aux;
35 with Sem_Eval; use Sem_Eval;
36 with Sem_Util; use Sem_Util;
37 with Sinfo; use Sinfo;
38 with Stand; use Stand;
39 with Stringt; use Stringt;
40 with Table;
41 with Tbuild; use Tbuild;
42 with Urealp; use Urealp;
44 package body Exp_Dbug is
46 -- The following table is used to queue up the entities passed as
47 -- arguments to Qualify_Entity_Names for later processing when
48 -- Qualify_All_Entity_Names is called.
50 package Name_Qualify_Units is new Table.Table (
51 Table_Component_Type => Node_Id,
52 Table_Index_Type => Nat,
53 Table_Low_Bound => 1,
54 Table_Initial => Alloc.Name_Qualify_Units_Initial,
55 Table_Increment => Alloc.Name_Qualify_Units_Increment,
56 Table_Name => "Name_Qualify_Units");
58 --------------------------------
59 -- Use of Qualification Flags --
60 --------------------------------
62 -- There are two flags used to keep track of qualification of entities
64 -- Has_Fully_Qualified_Name
65 -- Has_Qualified_Name
67 -- The difference between these is as follows. Has_Qualified_Name is
68 -- set to indicate that the name has been qualified as required by the
69 -- spec of this package. As described there, this may involve the full
70 -- qualification for the name, but for some entities, notably procedure
71 -- local variables, this full qualification is not required.
73 -- The flag Has_Fully_Qualified_Name is set if indeed the name has been
74 -- fully qualified in the Ada sense. If Has_Fully_Qualified_Name is set,
75 -- then Has_Qualified_Name is also set, but the other way round is not
76 -- the case.
78 -- Consider the following example:
80 -- with ...
81 -- procedure X is
82 -- B : Ddd.Ttt;
83 -- procedure Y is ..
85 -- Here B is a procedure local variable, so it does not need fully
86 -- qualification. The flag Has_Qualified_Name will be set on the
87 -- first attempt to qualify B, to indicate that the job is done
88 -- and need not be redone.
90 -- But Y is qualified as x__y, since procedures are always fully
91 -- qualified, so the first time that an attempt is made to qualify
92 -- the name y, it will be replaced by x__y, and both flags are set.
94 -- Why the two flags? Well there are cases where we derive type names
95 -- from object names. As noted in the spec, type names are always
96 -- fully qualified. Suppose for example that the backend has to build
97 -- a padded type for variable B. then it will construct the PAD name
98 -- from B, but it requires full qualification, so the fully qualified
99 -- type name will be x__b___PAD. The two flags allow the circuit for
100 -- building this name to realize efficiently that b needs further
101 -- qualification.
103 --------------------
104 -- Homonym_Suffix --
105 --------------------
107 -- The string defined here (and its associated length) is used to gather
108 -- the homonym string that will be appended to Name_Buffer when the name
109 -- is complete. Strip_Suffixes appends to this string as does
110 -- Append_Homonym_Number, and Output_Homonym_Numbers_Suffix appends the
111 -- string to the end of Name_Buffer.
113 Homonym_Numbers : String (1 .. 256);
114 Homonym_Len : Natural := 0;
116 ----------------------
117 -- Local Procedures --
118 ----------------------
120 procedure Add_Uint_To_Buffer (U : Uint);
121 -- Add image of universal integer to Name_Buffer, updating Name_Len
123 procedure Add_Real_To_Buffer (U : Ureal);
124 -- Add nnn_ddd to Name_Buffer, where nnn and ddd are integer values of
125 -- the normalized numerator and denominator of the given real value.
127 procedure Append_Homonym_Number (E : Entity_Id);
128 -- If the entity E has homonyms in the same scope, then make an entry
129 -- in the Homonym_Numbers array, bumping Homonym_Count accordingly.
131 function Bounds_Match_Size (E : Entity_Id) return Boolean;
132 -- Determine whether the bounds of E match the size of the type. This is
133 -- used to determine whether encoding is required for a discrete type.
135 function Is_Handled_Scale_Factor (U : Ureal) return Boolean;
136 -- The argument U is the Small_Value of a fixed-point type. This function
137 -- determines whether the back-end can handle this scale factor. When it
138 -- cannot, we have to output a GNAT encoding for the corresponding type.
140 procedure Output_Homonym_Numbers_Suffix;
141 -- If homonym numbers are stored, then output them into Name_Buffer
143 procedure Prepend_String_To_Buffer (S : String);
144 -- Prepend given string to the contents of the string buffer, updating
145 -- the value in Name_Len (i.e. string is added at start of buffer).
147 procedure Prepend_Uint_To_Buffer (U : Uint);
148 -- Prepend image of universal integer to Name_Buffer, updating Name_Len
150 procedure Qualify_Entity_Name (Ent : Entity_Id);
151 -- If not already done, replaces the Chars field of the given entity
152 -- with the appropriate fully qualified name.
154 procedure Reset_Buffers;
155 -- Reset the contents of Name_Buffer and Homonym_Numbers by setting their
156 -- respective lengths to zero.
158 procedure Strip_Suffixes (BNPE_Suffix_Found : in out Boolean);
159 -- Given an qualified entity name in Name_Buffer, remove any plain X or
160 -- X{nb} qualification suffix. The contents of Name_Buffer is not changed
161 -- but Name_Len may be adjusted on return to remove the suffix. If a
162 -- BNPE suffix is found and stripped, then BNPE_Suffix_Found is set to
163 -- True. If no suffix is found, then BNPE_Suffix_Found is not modified.
164 -- This routine also searches for a homonym suffix, and if one is found
165 -- it is also stripped, and the entries are added to the global homonym
166 -- list (Homonym_Numbers) so that they can later be put back.
168 ------------------------
169 -- Add_Real_To_Buffer --
170 ------------------------
172 procedure Add_Real_To_Buffer (U : Ureal) is
173 begin
174 Add_Uint_To_Buffer (Norm_Num (U));
175 Add_Str_To_Name_Buffer ("_");
176 Add_Uint_To_Buffer (Norm_Den (U));
177 end Add_Real_To_Buffer;
179 ------------------------
180 -- Add_Uint_To_Buffer --
181 ------------------------
183 procedure Add_Uint_To_Buffer (U : Uint) is
184 begin
185 if U < 0 then
186 Add_Uint_To_Buffer (-U);
187 Add_Char_To_Name_Buffer ('m');
188 else
189 UI_Image (U, Decimal);
190 Add_Str_To_Name_Buffer (UI_Image_Buffer (1 .. UI_Image_Length));
191 end if;
192 end Add_Uint_To_Buffer;
194 ---------------------------
195 -- Append_Homonym_Number --
196 ---------------------------
198 procedure Append_Homonym_Number (E : Entity_Id) is
200 procedure Add_Nat_To_H (Nr : Nat);
201 -- Little procedure to append Nr to Homonym_Numbers
203 ------------------
204 -- Add_Nat_To_H --
205 ------------------
207 procedure Add_Nat_To_H (Nr : Nat) is
208 begin
209 if Nr >= 10 then
210 Add_Nat_To_H (Nr / 10);
211 end if;
213 Homonym_Len := Homonym_Len + 1;
214 Homonym_Numbers (Homonym_Len) :=
215 Character'Val (Nr mod 10 + Character'Pos ('0'));
216 end Add_Nat_To_H;
218 -- Start of processing for Append_Homonym_Number
220 begin
221 if Has_Homonym (E) then
222 declare
223 H : Entity_Id := Homonym (E);
224 Nr : Nat := 1;
226 begin
227 while Present (H) loop
228 if Scope (H) = Scope (E) then
229 Nr := Nr + 1;
230 end if;
232 H := Homonym (H);
233 end loop;
235 if Homonym_Len > 0 then
236 Homonym_Len := Homonym_Len + 1;
237 Homonym_Numbers (Homonym_Len) := '_';
238 end if;
240 Add_Nat_To_H (Nr);
241 end;
242 end if;
243 end Append_Homonym_Number;
245 -----------------------
246 -- Bounds_Match_Size --
247 -----------------------
249 function Bounds_Match_Size (E : Entity_Id) return Boolean is
250 Siz : Uint;
252 begin
253 if not Is_OK_Static_Subtype (E) then
254 return False;
256 elsif Is_Integer_Type (E)
257 and then Subtypes_Statically_Match (E, Base_Type (E))
258 then
259 return True;
261 -- Here we check if the static bounds match the natural size, which is
262 -- the size passed through with the debugging information. This is the
263 -- Esize rounded up to 8, 16, 32 or 64 as appropriate.
265 else
266 declare
267 Umark : constant Uintp.Save_Mark := Uintp.Mark;
268 Result : Boolean;
270 begin
271 if Esize (E) <= 8 then
272 Siz := Uint_8;
273 elsif Esize (E) <= 16 then
274 Siz := Uint_16;
275 elsif Esize (E) <= 32 then
276 Siz := Uint_32;
277 else
278 Siz := Uint_64;
279 end if;
281 if Is_Modular_Integer_Type (E) or else Is_Enumeration_Type (E) then
282 Result :=
283 Expr_Rep_Value (Type_Low_Bound (E)) = 0
284 and then
285 2 ** Siz - Expr_Rep_Value (Type_High_Bound (E)) = 1;
287 else
288 Result :=
289 Expr_Rep_Value (Type_Low_Bound (E)) + 2 ** (Siz - 1) = 0
290 and then
291 2 ** (Siz - 1) - Expr_Rep_Value (Type_High_Bound (E)) = 1;
292 end if;
294 Release (Umark);
295 return Result;
296 end;
297 end if;
298 end Bounds_Match_Size;
300 --------------------------------
301 -- Debug_Renaming_Declaration --
302 --------------------------------
304 function Debug_Renaming_Declaration (N : Node_Id) return Node_Id is
305 Loc : constant Source_Ptr := Sloc (N);
306 Ent : constant Node_Id := Defining_Entity (N);
307 Nam : constant Node_Id := Name (N);
308 Ren : Node_Id;
309 Typ : Entity_Id;
310 Obj : Entity_Id;
311 Res : Node_Id;
313 Enable : Boolean := Nkind (N) = N_Package_Renaming_Declaration;
314 -- By default, we do not generate an encoding for renaming. This is
315 -- however done (in which case this is set to True) in a few cases:
316 -- - when a package is renamed,
317 -- - when the renaming involves a packed array,
318 -- - when the renaming involves a packed record.
320 Last_Is_Indexed_Comp : Boolean := False;
321 -- Whether the last subscript value was an indexed component access (XS)
323 procedure Enable_If_Packed_Array (N : Node_Id);
324 -- Enable encoding generation if N is a packed array
326 function Output_Subscript (N : Node_Id; S : String) return Boolean;
327 -- Outputs a single subscript value as ?nnn (subscript is compile time
328 -- known value with value nnn) or as ?e (subscript is local constant
329 -- with name e), where S supplies the proper string to use for ?.
330 -- Returns False if the subscript is not of an appropriate type to
331 -- output in one of these two forms. The result is prepended to the
332 -- name stored in Name_Buffer.
334 function Scope_Contains (Sc : Node_Id; Ent : Entity_Id) return Boolean;
335 -- Return whether Ent belong to the Sc scope
337 ----------------------------
338 -- Enable_If_Packed_Array --
339 ----------------------------
341 procedure Enable_If_Packed_Array (N : Node_Id) is
342 T : constant Entity_Id := Underlying_Type (Etype (N));
344 begin
345 Enable :=
346 Enable
347 or else
348 (Ekind (T) in Array_Kind
349 and then Present (Packed_Array_Impl_Type (T)));
350 end Enable_If_Packed_Array;
352 ----------------------
353 -- Output_Subscript --
354 ----------------------
356 function Output_Subscript (N : Node_Id; S : String) return Boolean is
357 begin
358 if Compile_Time_Known_Value (N) then
359 Prepend_Uint_To_Buffer (Expr_Value (N));
361 elsif Nkind (N) = N_Identifier
362 and then Scope_Contains (Scope (Entity (N)), Ent)
363 and then (Ekind (Entity (N)) = E_Constant
364 or else Ekind (Entity (N)) = E_In_Parameter)
365 then
366 Prepend_String_To_Buffer (Get_Name_String (Chars (Entity (N))));
368 else
369 return False;
370 end if;
372 Prepend_String_To_Buffer (S);
373 return True;
374 end Output_Subscript;
376 --------------------
377 -- Scope_Contains --
378 --------------------
380 function Scope_Contains (Sc : Node_Id; Ent : Entity_Id) return Boolean is
381 Cur : Node_Id := Scope (Ent);
383 begin
384 while Present (Cur) loop
385 if Cur = Sc then
386 return True;
387 end if;
389 Cur := Scope (Cur);
390 end loop;
392 return False;
393 end Scope_Contains;
395 -- Start of processing for Debug_Renaming_Declaration
397 begin
398 if not Comes_From_Source (N) and then not Needs_Debug_Info (Ent) then
399 return Empty;
400 end if;
402 -- Get renamed entity and compute suffix
404 Name_Len := 0;
405 Ren := Nam;
406 loop
407 -- The expression that designates the renamed object is sometimes
408 -- expanded into bit-wise operations. We want to work instead on
409 -- array/record components accesses, so try to analyze the unexpanded
410 -- forms.
412 Ren := Original_Node (Ren);
414 case Nkind (Ren) is
415 when N_Expanded_Name
416 | N_Identifier
418 if not Present (Renamed_Object (Entity (Ren))) then
419 exit;
420 end if;
422 -- This is a renaming of a renaming: traverse until the final
423 -- renaming to see if anything is packed along the way.
425 Ren := Renamed_Object (Entity (Ren));
427 when N_Selected_Component =>
428 declare
429 First_Bit : constant Uint :=
430 Normalized_First_Bit
431 (Entity (Selector_Name (Ren)));
433 begin
434 Enable :=
435 Enable
436 or else Is_Packed
437 (Underlying_Type (Etype (Prefix (Ren))))
438 or else (First_Bit /= No_Uint
439 and then First_Bit /= Uint_0);
440 end;
442 Prepend_String_To_Buffer
443 (Get_Name_String (Chars (Selector_Name (Ren))));
444 Prepend_String_To_Buffer ("XR");
445 Ren := Prefix (Ren);
446 Last_Is_Indexed_Comp := False;
448 when N_Indexed_Component =>
449 declare
450 X : Node_Id;
452 begin
453 Enable_If_Packed_Array (Prefix (Ren));
455 X := Last (Expressions (Ren));
456 while Present (X) loop
457 if not Output_Subscript (X, "XS") then
458 Set_Materialize_Entity (Ent);
459 return Empty;
460 end if;
462 Prev (X);
463 Last_Is_Indexed_Comp := True;
464 end loop;
465 end;
467 Ren := Prefix (Ren);
469 when N_Slice =>
471 -- Assuming X is an array:
472 -- X (Y1 .. Y2) (Y3)
474 -- is equivalent to:
475 -- X (Y3)
477 -- GDB cannot handle packed array slices, so avoid describing
478 -- the slice if we can avoid it.
480 if not Last_Is_Indexed_Comp then
481 Enable_If_Packed_Array (Prefix (Ren));
482 Typ := Etype (First_Index (Etype (Ren)));
484 if not Output_Subscript (Type_High_Bound (Typ), "XS") then
485 Set_Materialize_Entity (Ent);
486 return Empty;
487 end if;
489 if not Output_Subscript (Type_Low_Bound (Typ), "XL") then
490 Set_Materialize_Entity (Ent);
491 return Empty;
492 end if;
494 Last_Is_Indexed_Comp := False;
495 end if;
497 Ren := Prefix (Ren);
499 when N_Explicit_Dereference =>
500 Prepend_String_To_Buffer ("XA");
501 Ren := Prefix (Ren);
502 Last_Is_Indexed_Comp := False;
504 -- For now, anything else simply results in no translation
506 when others =>
507 Set_Materialize_Entity (Ent);
508 return Empty;
509 end case;
510 end loop;
512 -- If we found no reason here to emit an encoding, stop now
514 if not Enable then
515 Set_Materialize_Entity (Ent);
516 return Empty;
517 end if;
519 Prepend_String_To_Buffer ("___XE");
521 -- Include the designation of the form of renaming
523 case Nkind (N) is
524 when N_Object_Renaming_Declaration =>
525 Prepend_String_To_Buffer ("___XR");
527 when N_Exception_Renaming_Declaration =>
528 Prepend_String_To_Buffer ("___XRE");
530 when N_Package_Renaming_Declaration =>
531 Prepend_String_To_Buffer ("___XRP");
533 when others =>
534 return Empty;
535 end case;
537 -- Add the name of the renaming entity to the front
539 Prepend_String_To_Buffer (Get_Name_String (Chars (Ent)));
541 -- If it is a child unit create a fully qualified name, to disambiguate
542 -- multiple child units with the same name and different parents.
544 if Nkind (N) = N_Package_Renaming_Declaration
545 and then Is_Child_Unit (Ent)
546 then
547 Prepend_String_To_Buffer ("__");
548 Prepend_String_To_Buffer
549 (Get_Name_String (Chars (Scope (Ent))));
550 end if;
552 -- Create the special object whose name is the debug encoding for the
553 -- renaming declaration.
555 -- For now, the object name contains the suffix encoding for the renamed
556 -- object, but not the name of the leading entity. The object is linked
557 -- the renamed entity using the Debug_Renaming_Link field. Then the
558 -- Qualify_Entity_Name procedure uses this link to create the proper
559 -- fully qualified name.
561 -- The reason we do things this way is that we really need to copy the
562 -- qualification of the renamed entity, and it is really much easier to
563 -- do this after the renamed entity has itself been fully qualified.
565 Obj := Make_Defining_Identifier (Loc, Chars => Name_Enter);
566 Res :=
567 Make_Object_Declaration (Loc,
568 Defining_Identifier => Obj,
569 Object_Definition => New_Occurrence_Of
570 (Standard_Debug_Renaming_Type, Loc));
572 Set_Debug_Renaming_Link (Obj, Entity (Ren));
574 Set_Debug_Info_Needed (Obj);
576 -- The renamed entity may be a temporary, e.g. the result of an
577 -- implicit dereference in an iterator. Indicate that the temporary
578 -- itself requires debug information. If the renamed entity comes
579 -- from source this is a no-op.
581 Set_Debug_Info_Needed (Entity (Ren));
583 -- Mark the object as internal so that it won't be initialized when
584 -- pragma Initialize_Scalars or Normalize_Scalars is in use.
586 Set_Is_Internal (Obj);
588 return Res;
590 -- If we get an exception, just figure it is a case that we cannot
591 -- successfully handle using our current approach, since this is
592 -- only for debugging, no need to take the compilation with us.
594 exception
595 when others =>
596 return Make_Null_Statement (Loc);
597 end Debug_Renaming_Declaration;
599 -----------------------------
600 -- Is_Handled_Scale_Factor --
601 -----------------------------
603 function Is_Handled_Scale_Factor (U : Ureal) return Boolean is
604 begin
605 -- Keep in sync with gigi (see E_*_Fixed_Point_Type handling in
606 -- decl.c:gnat_to_gnu_entity).
608 if UI_Eq (Numerator (U), Uint_1) then
609 if Rbase (U) = 2 or else Rbase (U) = 10 then
610 return True;
611 end if;
612 end if;
614 return
615 (UI_Is_In_Int_Range (Norm_Num (U))
616 and then
617 UI_Is_In_Int_Range (Norm_Den (U)));
618 end Is_Handled_Scale_Factor;
620 ----------------------
621 -- Get_Encoded_Name --
622 ----------------------
624 -- Note: see spec for details on encodings
626 procedure Get_Encoded_Name (E : Entity_Id) is
627 Has_Suffix : Boolean;
629 begin
630 -- If not generating code, there is no need to create encoded names, and
631 -- problems when the back-end is called to annotate types without full
632 -- code generation. See comments in Get_External_Name for additional
633 -- details.
635 -- However we do create encoded names if the back end is active, even
636 -- if Operating_Mode got reset. Otherwise any serious error reported
637 -- by the backend calling Error_Msg changes the Compilation_Mode to
638 -- Check_Semantics, which disables the functionality of this routine,
639 -- causing the generation of spurious additional errors.
641 -- Couldn't we just test Original_Operating_Mode here? ???
643 if Operating_Mode /= Generate_Code and then not Generating_Code then
644 return;
645 end if;
647 Get_Name_String (Chars (E));
649 -- Nothing to do if we do not have a type
651 if not Is_Type (E)
653 -- Or if this is an enumeration base type
655 or else (Is_Enumeration_Type (E) and then Is_Base_Type (E))
657 -- Or if this is a dummy type for a renaming
659 or else (Name_Len >= 3 and then
660 Name_Buffer (Name_Len - 2 .. Name_Len) = "_XR")
662 or else (Name_Len >= 4 and then
663 (Name_Buffer (Name_Len - 3 .. Name_Len) = "_XRE"
664 or else
665 Name_Buffer (Name_Len - 3 .. Name_Len) = "_XRP"))
667 -- For all these cases, just return the name unchanged
669 then
670 Name_Buffer (Name_Len + 1) := ASCII.NUL;
671 return;
672 end if;
674 Has_Suffix := True;
676 -- Fixed-point case: generate GNAT encodings when asked to or when we
677 -- know the back-end will not be able to handle the scale factor.
679 if Is_Fixed_Point_Type (E)
680 and then (GNAT_Encodings /= DWARF_GNAT_Encodings_Minimal
681 or else not Is_Handled_Scale_Factor (Small_Value (E)))
682 then
683 Get_External_Name (E, True, "XF_");
684 Add_Real_To_Buffer (Delta_Value (E));
686 if Small_Value (E) /= Delta_Value (E) then
687 Add_Str_To_Name_Buffer ("_");
688 Add_Real_To_Buffer (Small_Value (E));
689 end if;
691 -- Discrete case where bounds do not match size. Not necessary if we can
692 -- emit standard DWARF.
694 elsif GNAT_Encodings /= DWARF_GNAT_Encodings_Minimal
695 and then Is_Discrete_Type (E)
696 and then not Bounds_Match_Size (E)
697 then
698 declare
699 Lo : constant Node_Id := Type_Low_Bound (E);
700 Hi : constant Node_Id := Type_High_Bound (E);
702 Lo_Con : constant Boolean := Compile_Time_Known_Value (Lo);
703 Hi_Con : constant Boolean := Compile_Time_Known_Value (Hi);
705 Lo_Discr : constant Boolean :=
706 Nkind (Lo) = N_Identifier
707 and then Ekind (Entity (Lo)) = E_Discriminant;
709 Hi_Discr : constant Boolean :=
710 Nkind (Hi) = N_Identifier
711 and then Ekind (Entity (Hi)) = E_Discriminant;
713 Lo_Encode : constant Boolean := Lo_Con or Lo_Discr;
714 Hi_Encode : constant Boolean := Hi_Con or Hi_Discr;
716 Biased : constant Boolean := Has_Biased_Representation (E);
718 begin
719 if Biased then
720 Get_External_Name (E, True, "XB");
721 else
722 Get_External_Name (E, True, "XD");
723 end if;
725 if Lo_Encode or Hi_Encode then
726 if Biased then
727 Add_Str_To_Name_Buffer ("_");
728 else
729 if Lo_Encode then
730 if Hi_Encode then
731 Add_Str_To_Name_Buffer ("LU_");
732 else
733 Add_Str_To_Name_Buffer ("L_");
734 end if;
735 else
736 Add_Str_To_Name_Buffer ("U_");
737 end if;
738 end if;
740 if Lo_Con then
741 Add_Uint_To_Buffer (Expr_Rep_Value (Lo));
742 elsif Lo_Discr then
743 Get_Name_String_And_Append (Chars (Entity (Lo)));
744 end if;
746 if Lo_Encode and Hi_Encode then
747 Add_Str_To_Name_Buffer ("__");
748 end if;
750 if Hi_Con then
751 Add_Uint_To_Buffer (Expr_Rep_Value (Hi));
752 elsif Hi_Discr then
753 Get_Name_String_And_Append (Chars (Entity (Hi)));
754 end if;
755 end if;
756 end;
758 -- For all other cases, the encoded name is the normal type name
760 else
761 Has_Suffix := False;
762 Get_External_Name (E);
763 end if;
765 if Debug_Flag_B and then Has_Suffix then
766 Write_Str ("**** type ");
767 Write_Name (Chars (E));
768 Write_Str (" is encoded as ");
769 Write_Str (Name_Buffer (1 .. Name_Len));
770 Write_Eol;
771 end if;
773 Name_Buffer (Name_Len + 1) := ASCII.NUL;
774 end Get_Encoded_Name;
776 -----------------------
777 -- Get_External_Name --
778 -----------------------
780 procedure Get_External_Name
781 (Entity : Entity_Id;
782 Has_Suffix : Boolean := False;
783 Suffix : String := "")
785 procedure Get_Qualified_Name_And_Append (Entity : Entity_Id);
786 -- Appends fully qualified name of given entity to Name_Buffer
788 -----------------------------------
789 -- Get_Qualified_Name_And_Append --
790 -----------------------------------
792 procedure Get_Qualified_Name_And_Append (Entity : Entity_Id) is
793 begin
794 -- If the entity is a compilation unit, its scope is Standard,
795 -- there is no outer scope, and the no further qualification
796 -- is required.
798 -- If the front end has already computed a fully qualified name,
799 -- then it is also the case that no further qualification is
800 -- required.
802 if Present (Scope (Scope (Entity)))
803 and then not Has_Fully_Qualified_Name (Entity)
804 then
805 Get_Qualified_Name_And_Append (Scope (Entity));
806 Add_Str_To_Name_Buffer ("__");
807 Get_Name_String_And_Append (Chars (Entity));
808 Append_Homonym_Number (Entity);
810 else
811 Get_Name_String_And_Append (Chars (Entity));
812 end if;
813 end Get_Qualified_Name_And_Append;
815 -- Local variables
817 E : Entity_Id := Entity;
819 -- Start of processing for Get_External_Name
821 begin
822 -- If we are not in code generation mode, this procedure may still be
823 -- called from Back_End (more specifically - from gigi for doing type
824 -- representation annotation or some representation-specific checks).
825 -- But in this mode there is no need to mess with external names.
827 -- Furthermore, the call causes difficulties in this case because the
828 -- string representing the homonym number is not correctly reset as a
829 -- part of the call to Output_Homonym_Numbers_Suffix (which is not
830 -- called in gigi).
832 if Operating_Mode /= Generate_Code then
833 return;
834 end if;
836 Reset_Buffers;
838 -- If this is a child unit, we want the child
840 if Nkind (E) = N_Defining_Program_Unit_Name then
841 E := Defining_Identifier (Entity);
842 end if;
844 -- Case of interface name being used
846 if Ekind_In (E, E_Constant,
847 E_Exception,
848 E_Function,
849 E_Procedure,
850 E_Variable)
851 and then Present (Interface_Name (E))
852 and then No (Address_Clause (E))
853 and then not Has_Suffix
854 then
855 Append (Global_Name_Buffer, Strval (Interface_Name (E)));
857 -- All other cases besides the interface name case
859 else
860 -- If this is a library level subprogram (i.e. a subprogram that is a
861 -- compilation unit other than a subunit), then we prepend _ada_ to
862 -- ensure distinctions required as described in the spec.
864 -- Check explicitly for child units, because those are not flagged
865 -- as Compilation_Units by lib. Should they be ???
867 if Is_Subprogram (E)
868 and then (Is_Compilation_Unit (E) or Is_Child_Unit (E))
869 and then not Has_Suffix
870 then
871 Add_Str_To_Name_Buffer ("_ada_");
872 end if;
874 -- If the entity is a subprogram instance that is not a compilation
875 -- unit, generate the name of the original Ada entity, which is the
876 -- one gdb needs.
878 if Is_Generic_Instance (E)
879 and then Is_Subprogram (E)
880 and then not Is_Compilation_Unit (Scope (E))
881 and then Ekind_In (Scope (E), E_Package, E_Package_Body)
882 and then Present (Related_Instance (Scope (E)))
883 then
884 E := Related_Instance (Scope (E));
885 end if;
887 Get_Qualified_Name_And_Append (E);
888 end if;
890 if Has_Suffix then
891 Add_Str_To_Name_Buffer ("___");
892 Add_Str_To_Name_Buffer (Suffix);
893 end if;
895 -- Add a special prefix to distinguish Ghost entities. In Ignored Ghost
896 -- mode, these entities should not leak in the "living" space and they
897 -- should be removed by the compiler in a post-processing pass. Thus,
898 -- the prefix allows anyone to check that the final executable indeed
899 -- does not contain such entities, in such a case. Do not insert this
900 -- prefix for compilation units, whose name is used as a basis for the
901 -- name of the generated elaboration procedure and (when appropriate)
902 -- the executable produced. Only insert this prefix once, for Ghost
903 -- entities declared inside other Ghost entities. Three leading
904 -- underscores are used so that "___ghost_" is a unique substring of
905 -- names produced for Ghost entities, while "__ghost_" can appear in
906 -- names of entities inside a child/local package called "Ghost".
908 if Is_Ghost_Entity (E)
909 and then not Is_Compilation_Unit (E)
910 and then (Name_Len < 9
911 or else Name_Buffer (1 .. 9) /= "___ghost_")
912 then
913 Insert_Str_In_Name_Buffer ("___ghost_", 1);
914 end if;
916 Name_Buffer (Name_Len + 1) := ASCII.NUL;
917 end Get_External_Name;
919 --------------------------
920 -- Get_Variant_Encoding --
921 --------------------------
923 procedure Get_Variant_Encoding (V : Node_Id) is
924 Choice : Node_Id;
926 procedure Choice_Val (Typ : Character; Choice : Node_Id);
927 -- Output encoded value for a single choice value. Typ is the key
928 -- character ('S', 'F', or 'T') that precedes the choice value.
930 ----------------
931 -- Choice_Val --
932 ----------------
934 procedure Choice_Val (Typ : Character; Choice : Node_Id) is
935 begin
936 if Nkind (Choice) = N_Integer_Literal then
937 Add_Char_To_Name_Buffer (Typ);
938 Add_Uint_To_Buffer (Intval (Choice));
940 -- Character literal with no entity present (this is the case
941 -- Standard.Character or Standard.Wide_Character as root type)
943 elsif Nkind (Choice) = N_Character_Literal
944 and then No (Entity (Choice))
945 then
946 Add_Char_To_Name_Buffer (Typ);
947 Add_Uint_To_Buffer (Char_Literal_Value (Choice));
949 else
950 declare
951 Ent : constant Entity_Id := Entity (Choice);
953 begin
954 if Ekind (Ent) = E_Enumeration_Literal then
955 Add_Char_To_Name_Buffer (Typ);
956 Add_Uint_To_Buffer (Enumeration_Rep (Ent));
958 else
959 pragma Assert (Ekind (Ent) = E_Constant);
960 Choice_Val (Typ, Constant_Value (Ent));
961 end if;
962 end;
963 end if;
964 end Choice_Val;
966 -- Start of processing for Get_Variant_Encoding
968 begin
969 Name_Len := 0;
971 Choice := First (Discrete_Choices (V));
972 while Present (Choice) loop
973 if Nkind (Choice) = N_Others_Choice then
974 Add_Char_To_Name_Buffer ('O');
976 elsif Nkind (Choice) = N_Range then
977 Choice_Val ('R', Low_Bound (Choice));
978 Choice_Val ('T', High_Bound (Choice));
980 elsif Is_Entity_Name (Choice)
981 and then Is_Type (Entity (Choice))
982 then
983 Choice_Val ('R', Type_Low_Bound (Entity (Choice)));
984 Choice_Val ('T', Type_High_Bound (Entity (Choice)));
986 elsif Nkind (Choice) = N_Subtype_Indication then
987 declare
988 Rang : constant Node_Id :=
989 Range_Expression (Constraint (Choice));
990 begin
991 Choice_Val ('R', Low_Bound (Rang));
992 Choice_Val ('T', High_Bound (Rang));
993 end;
995 else
996 Choice_Val ('S', Choice);
997 end if;
999 Next (Choice);
1000 end loop;
1002 Name_Buffer (Name_Len + 1) := ASCII.NUL;
1004 if Debug_Flag_B then
1005 declare
1006 VP : constant Node_Id := Parent (V); -- Variant_Part
1007 CL : constant Node_Id := Parent (VP); -- Component_List
1008 RD : constant Node_Id := Parent (CL); -- Record_Definition
1009 FT : constant Node_Id := Parent (RD); -- Full_Type_Declaration
1011 begin
1012 Write_Str ("**** variant for type ");
1013 Write_Name (Chars (Defining_Identifier (FT)));
1014 Write_Str (" is encoded as ");
1015 Write_Str (Name_Buffer (1 .. Name_Len));
1016 Write_Eol;
1017 end;
1018 end if;
1019 end Get_Variant_Encoding;
1021 -----------------------------------------
1022 -- Build_Subprogram_Instance_Renamings --
1023 -----------------------------------------
1025 procedure Build_Subprogram_Instance_Renamings
1026 (N : Node_Id;
1027 Wrapper : Entity_Id)
1029 Loc : Source_Ptr;
1030 Decl : Node_Id;
1031 E : Entity_Id;
1033 begin
1034 E := First_Entity (Wrapper);
1035 while Present (E) loop
1036 if Nkind (Parent (E)) = N_Object_Declaration
1037 and then Is_Elementary_Type (Etype (E))
1038 then
1039 Loc := Sloc (Expression (Parent (E)));
1040 Decl := Make_Object_Renaming_Declaration (Loc,
1041 Defining_Identifier =>
1042 Make_Defining_Identifier (Loc, Chars (E)),
1043 Subtype_Mark => New_Occurrence_Of (Etype (E), Loc),
1044 Name => New_Occurrence_Of (E, Loc));
1046 Append (Decl, Declarations (N));
1047 Set_Needs_Debug_Info (Defining_Identifier (Decl));
1048 end if;
1050 Next_Entity (E);
1051 end loop;
1052 end Build_Subprogram_Instance_Renamings;
1054 ------------------------------------
1055 -- Get_Secondary_DT_External_Name --
1056 ------------------------------------
1058 procedure Get_Secondary_DT_External_Name
1059 (Typ : Entity_Id;
1060 Ancestor_Typ : Entity_Id;
1061 Suffix_Index : Int)
1063 begin
1064 Get_External_Name (Typ);
1066 if Ancestor_Typ /= Typ then
1067 declare
1068 Len : constant Natural := Name_Len;
1069 Save_Str : constant String (1 .. Name_Len)
1070 := Name_Buffer (1 .. Name_Len);
1071 begin
1072 Get_External_Name (Ancestor_Typ);
1074 -- Append the extended name of the ancestor to the
1075 -- extended name of Typ
1077 Name_Buffer (Len + 2 .. Len + Name_Len + 1) :=
1078 Name_Buffer (1 .. Name_Len);
1079 Name_Buffer (1 .. Len) := Save_Str;
1080 Name_Buffer (Len + 1) := '_';
1081 Name_Len := Len + Name_Len + 1;
1082 end;
1083 end if;
1085 Add_Nat_To_Name_Buffer (Suffix_Index);
1086 end Get_Secondary_DT_External_Name;
1088 ---------------------------------
1089 -- Make_Packed_Array_Impl_Type_Name --
1090 ---------------------------------
1092 function Make_Packed_Array_Impl_Type_Name
1093 (Typ : Entity_Id;
1094 Csize : Uint)
1095 return Name_Id
1097 begin
1098 Get_Name_String (Chars (Typ));
1099 Add_Str_To_Name_Buffer ("___XP");
1100 Add_Uint_To_Buffer (Csize);
1101 return Name_Find;
1102 end Make_Packed_Array_Impl_Type_Name;
1104 -----------------------------------
1105 -- Output_Homonym_Numbers_Suffix --
1106 -----------------------------------
1108 procedure Output_Homonym_Numbers_Suffix is
1109 J : Natural;
1111 begin
1112 if Homonym_Len > 0 then
1114 -- Check for all 1's, in which case we do not output
1116 J := 1;
1117 loop
1118 exit when Homonym_Numbers (J) /= '1';
1120 -- If we reached end of string we do not output
1122 if J = Homonym_Len then
1123 Homonym_Len := 0;
1124 return;
1125 end if;
1127 exit when Homonym_Numbers (J + 1) /= '_';
1128 J := J + 2;
1129 end loop;
1131 -- If we exit the loop then suffix must be output
1133 Add_Str_To_Name_Buffer ("__");
1134 Add_Str_To_Name_Buffer (Homonym_Numbers (1 .. Homonym_Len));
1135 Homonym_Len := 0;
1136 end if;
1137 end Output_Homonym_Numbers_Suffix;
1139 ------------------------------
1140 -- Prepend_String_To_Buffer --
1141 ------------------------------
1143 procedure Prepend_String_To_Buffer (S : String) is
1144 N : constant Integer := S'Length;
1145 begin
1146 Name_Buffer (1 + N .. Name_Len + N) := Name_Buffer (1 .. Name_Len);
1147 Name_Buffer (1 .. N) := S;
1148 Name_Len := Name_Len + N;
1149 end Prepend_String_To_Buffer;
1151 ----------------------------
1152 -- Prepend_Uint_To_Buffer --
1153 ----------------------------
1155 procedure Prepend_Uint_To_Buffer (U : Uint) is
1156 begin
1157 if U < 0 then
1158 Prepend_String_To_Buffer ("m");
1159 Prepend_Uint_To_Buffer (-U);
1160 else
1161 UI_Image (U, Decimal);
1162 Prepend_String_To_Buffer (UI_Image_Buffer (1 .. UI_Image_Length));
1163 end if;
1164 end Prepend_Uint_To_Buffer;
1166 ------------------------------
1167 -- Qualify_All_Entity_Names --
1168 ------------------------------
1170 procedure Qualify_All_Entity_Names is
1171 E : Entity_Id;
1172 Ent : Entity_Id;
1173 Nod : Node_Id;
1175 begin
1176 for J in Name_Qualify_Units.First .. Name_Qualify_Units.Last loop
1177 Nod := Name_Qualify_Units.Table (J);
1179 -- When a scoping construct is ignored Ghost, it is rewritten as
1180 -- a null statement. Skip such constructs as they no longer carry
1181 -- names.
1183 if Nkind (Nod) = N_Null_Statement then
1184 goto Continue;
1185 end if;
1187 E := Defining_Entity (Nod);
1188 Reset_Buffers;
1189 Qualify_Entity_Name (E);
1191 -- Normally entities in the qualification list are scopes, but in the
1192 -- case of a library-level package renaming there is an associated
1193 -- variable that encodes the debugger name and that variable is
1194 -- entered in the list since it occurs in the Aux_Decls list of the
1195 -- compilation and doesn't have a normal scope.
1197 if Ekind (E) /= E_Variable then
1198 Ent := First_Entity (E);
1199 while Present (Ent) loop
1200 Reset_Buffers;
1201 Qualify_Entity_Name (Ent);
1202 Next_Entity (Ent);
1204 -- There are odd cases where Last_Entity (E) = E. This happens
1205 -- in the case of renaming of packages. This test avoids
1206 -- getting stuck in such cases.
1208 exit when Ent = E;
1209 end loop;
1210 end if;
1212 <<Continue>>
1213 null;
1214 end loop;
1215 end Qualify_All_Entity_Names;
1217 -------------------------
1218 -- Qualify_Entity_Name --
1219 -------------------------
1221 procedure Qualify_Entity_Name (Ent : Entity_Id) is
1223 Full_Qualify_Name : String (1 .. Name_Buffer'Length);
1224 Full_Qualify_Len : Natural := 0;
1225 -- Used to accumulate fully qualified name of subprogram
1227 procedure Fully_Qualify_Name (E : Entity_Id);
1228 -- Used to qualify a subprogram or type name, where full
1229 -- qualification up to Standard is always used. Name is set
1230 -- in Full_Qualify_Name with the length in Full_Qualify_Len.
1231 -- Note that this routine does not prepend the _ada_ string
1232 -- required for library subprograms (this is done in the back end).
1234 function Is_BNPE (S : Entity_Id) return Boolean;
1235 -- Determines if S is a BNPE, i.e. Body-Nested Package Entity, which
1236 -- is defined to be a package which is immediately nested within a
1237 -- package body.
1239 function Qualify_Needed (S : Entity_Id) return Boolean;
1240 -- Given a scope, determines if the scope is to be included in the
1241 -- fully qualified name, True if so, False if not. Blocks and loops
1242 -- are excluded from a qualified name.
1244 procedure Set_BNPE_Suffix (E : Entity_Id);
1245 -- Recursive routine to append the BNPE qualification suffix. Works
1246 -- from right to left with E being the current entity in the list.
1247 -- The result does NOT have the trailing n's and trailing b stripped.
1248 -- The caller must do this required stripping.
1250 procedure Set_Entity_Name (E : Entity_Id);
1251 -- Internal recursive routine that does most of the work. This routine
1252 -- leaves the result sitting in Name_Buffer and Name_Len.
1254 BNPE_Suffix_Needed : Boolean := False;
1255 -- Set true if a body-nested package entity suffix is required
1257 Save_Chars : constant Name_Id := Chars (Ent);
1258 -- Save original name
1260 ------------------------
1261 -- Fully_Qualify_Name --
1262 ------------------------
1264 procedure Fully_Qualify_Name (E : Entity_Id) is
1265 Discard : Boolean := False;
1267 begin
1268 -- Ignore empty entry (can happen in error cases)
1270 if No (E) then
1271 return;
1273 -- If this we are qualifying entities local to a generic instance,
1274 -- use the name of the original instantiation, not that of the
1275 -- anonymous subprogram in the wrapper package, so that gdb doesn't
1276 -- have to know about these.
1278 elsif Is_Generic_Instance (E)
1279 and then Is_Subprogram (E)
1280 and then not Comes_From_Source (E)
1281 and then not Is_Compilation_Unit (Scope (E))
1282 then
1283 Fully_Qualify_Name (Related_Instance (Scope (E)));
1284 return;
1285 end if;
1287 -- If we reached fully qualified name, then just copy it
1289 if Has_Fully_Qualified_Name (E) then
1290 Get_Name_String (Chars (E));
1291 Strip_Suffixes (Discard);
1292 Full_Qualify_Name (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
1293 Full_Qualify_Len := Name_Len;
1294 Set_Has_Fully_Qualified_Name (Ent);
1296 -- Case of non-fully qualified name
1298 else
1299 if Scope (E) = Standard_Standard then
1300 Set_Has_Fully_Qualified_Name (Ent);
1301 else
1302 Fully_Qualify_Name (Scope (E));
1303 Full_Qualify_Name (Full_Qualify_Len + 1) := '_';
1304 Full_Qualify_Name (Full_Qualify_Len + 2) := '_';
1305 Full_Qualify_Len := Full_Qualify_Len + 2;
1306 end if;
1308 if Has_Qualified_Name (E) then
1309 Get_Unqualified_Name_String (Chars (E));
1310 else
1311 Get_Name_String (Chars (E));
1312 end if;
1314 -- Here we do one step of the qualification
1316 Full_Qualify_Name
1317 (Full_Qualify_Len + 1 .. Full_Qualify_Len + Name_Len) :=
1318 Name_Buffer (1 .. Name_Len);
1319 Full_Qualify_Len := Full_Qualify_Len + Name_Len;
1320 Append_Homonym_Number (E);
1321 end if;
1323 if Is_BNPE (E) then
1324 BNPE_Suffix_Needed := True;
1325 end if;
1326 end Fully_Qualify_Name;
1328 -------------
1329 -- Is_BNPE --
1330 -------------
1332 function Is_BNPE (S : Entity_Id) return Boolean is
1333 begin
1334 return Ekind (S) = E_Package and then Is_Package_Body_Entity (S);
1335 end Is_BNPE;
1337 --------------------
1338 -- Qualify_Needed --
1339 --------------------
1341 function Qualify_Needed (S : Entity_Id) return Boolean is
1342 begin
1343 -- If we got all the way to Standard, then we have certainly
1344 -- fully qualified the name, so set the flag appropriately,
1345 -- and then return False, since we are most certainly done.
1347 if S = Standard_Standard then
1348 Set_Has_Fully_Qualified_Name (Ent, True);
1349 return False;
1351 -- Otherwise figure out if further qualification is required
1353 else
1354 return Is_Subprogram (Ent)
1355 or else Ekind (Ent) = E_Subprogram_Body
1356 or else (Ekind (S) /= E_Block
1357 and then Ekind (S) /= E_Loop
1358 and then not Is_Dynamic_Scope (S));
1359 end if;
1360 end Qualify_Needed;
1362 ---------------------
1363 -- Set_BNPE_Suffix --
1364 ---------------------
1366 procedure Set_BNPE_Suffix (E : Entity_Id) is
1367 S : constant Entity_Id := Scope (E);
1369 begin
1370 if Qualify_Needed (S) then
1371 Set_BNPE_Suffix (S);
1373 if Is_BNPE (E) then
1374 Add_Char_To_Name_Buffer ('b');
1375 else
1376 Add_Char_To_Name_Buffer ('n');
1377 end if;
1379 else
1380 Add_Char_To_Name_Buffer ('X');
1381 end if;
1382 end Set_BNPE_Suffix;
1384 ---------------------
1385 -- Set_Entity_Name --
1386 ---------------------
1388 procedure Set_Entity_Name (E : Entity_Id) is
1389 S : constant Entity_Id := Scope (E);
1391 begin
1392 -- If we reach an already qualified name, just take the encoding
1393 -- except that we strip the package body suffixes, since these
1394 -- will be separately put on later.
1396 if Has_Qualified_Name (E) then
1397 Get_Name_String_And_Append (Chars (E));
1398 Strip_Suffixes (BNPE_Suffix_Needed);
1400 -- If the top level name we are adding is itself fully
1401 -- qualified, then that means that the name that we are
1402 -- preparing for the Fully_Qualify_Name call will also
1403 -- generate a fully qualified name.
1405 if Has_Fully_Qualified_Name (E) then
1406 Set_Has_Fully_Qualified_Name (Ent);
1407 end if;
1409 -- Case where upper level name is not encoded yet
1411 else
1412 -- Recurse if further qualification required
1414 if Qualify_Needed (S) then
1415 Set_Entity_Name (S);
1416 Add_Str_To_Name_Buffer ("__");
1417 end if;
1419 -- Otherwise get name and note if it is a BNPE
1421 Get_Name_String_And_Append (Chars (E));
1423 if Is_BNPE (E) then
1424 BNPE_Suffix_Needed := True;
1425 end if;
1427 Append_Homonym_Number (E);
1428 end if;
1429 end Set_Entity_Name;
1431 -- Start of processing for Qualify_Entity_Name
1433 begin
1434 if Has_Qualified_Name (Ent) then
1435 return;
1437 -- In formal verification mode, simply append a suffix for homonyms.
1438 -- We used to qualify entity names as full expansion does, but this was
1439 -- removed as this prevents the verification back-end from using a short
1440 -- name for debugging and user interaction. The verification back-end
1441 -- already takes care of qualifying names when needed. Still mark the
1442 -- name as being qualified, as Qualify_Entity_Name may be called more
1443 -- than once on the same entity.
1445 elsif GNATprove_Mode then
1446 if Has_Homonym (Ent) then
1447 Get_Name_String (Chars (Ent));
1448 Append_Homonym_Number (Ent);
1449 Output_Homonym_Numbers_Suffix;
1450 Set_Chars (Ent, Name_Enter);
1451 end if;
1453 Set_Has_Qualified_Name (Ent);
1454 return;
1456 -- If the entity is a variable encoding the debug name for an object
1457 -- renaming, then the qualified name of the entity associated with the
1458 -- renamed object can now be incorporated in the debug name.
1460 elsif Ekind (Ent) = E_Variable
1461 and then Present (Debug_Renaming_Link (Ent))
1462 then
1463 Name_Len := 0;
1464 Qualify_Entity_Name (Debug_Renaming_Link (Ent));
1465 Get_Name_String (Chars (Ent));
1467 -- Retrieve the now-qualified name of the renamed entity and insert
1468 -- it in the middle of the name, just preceding the suffix encoding
1469 -- describing the renamed object.
1471 declare
1472 Renamed_Id : constant String :=
1473 Get_Name_String (Chars (Debug_Renaming_Link (Ent)));
1474 Insert_Len : constant Integer := Renamed_Id'Length + 1;
1475 Index : Natural := Name_Len - 3;
1477 begin
1478 -- Loop backwards through the name to find the start of the "___"
1479 -- sequence associated with the suffix.
1481 while Index >= Name_Buffer'First
1482 and then (Name_Buffer (Index + 1) /= '_'
1483 or else Name_Buffer (Index + 2) /= '_'
1484 or else Name_Buffer (Index + 3) /= '_')
1485 loop
1486 Index := Index - 1;
1487 end loop;
1489 pragma Assert (Name_Buffer (Index + 1 .. Index + 3) = "___");
1491 -- Insert an underscore separator and the entity name just in
1492 -- front of the suffix.
1494 Name_Buffer (Index + 1 + Insert_Len .. Name_Len + Insert_Len) :=
1495 Name_Buffer (Index + 1 .. Name_Len);
1496 Name_Buffer (Index + 1) := '_';
1497 Name_Buffer (Index + 2 .. Index + Insert_Len) := Renamed_Id;
1498 Name_Len := Name_Len + Insert_Len;
1499 end;
1501 -- Reset the name of the variable to the new name that includes the
1502 -- name of the renamed entity.
1504 Set_Chars (Ent, Name_Enter);
1506 -- If the entity needs qualification by its scope then develop it
1507 -- here, add the variable's name, and again reset the entity name.
1509 if Qualify_Needed (Scope (Ent)) then
1510 Name_Len := 0;
1511 Set_Entity_Name (Scope (Ent));
1512 Add_Str_To_Name_Buffer ("__");
1514 Get_Name_String_And_Append (Chars (Ent));
1516 Set_Chars (Ent, Name_Enter);
1517 end if;
1519 Set_Has_Qualified_Name (Ent);
1520 return;
1522 elsif Is_Subprogram (Ent)
1523 or else Ekind (Ent) = E_Subprogram_Body
1524 or else Is_Type (Ent)
1525 then
1526 Fully_Qualify_Name (Ent);
1527 Name_Len := Full_Qualify_Len;
1528 Name_Buffer (1 .. Name_Len) := Full_Qualify_Name (1 .. Name_Len);
1530 -- Qualification needed for enumeration literals when generating C code
1531 -- (to simplify their management in the backend).
1533 elsif Modify_Tree_For_C
1534 and then Ekind (Ent) = E_Enumeration_Literal
1535 and then Scope (Ultimate_Alias (Ent)) /= Standard_Standard
1536 then
1537 Fully_Qualify_Name (Ent);
1538 Name_Len := Full_Qualify_Len;
1539 Name_Buffer (1 .. Name_Len) := Full_Qualify_Name (1 .. Name_Len);
1541 elsif Qualify_Needed (Scope (Ent)) then
1542 Name_Len := 0;
1543 Set_Entity_Name (Ent);
1545 else
1546 Set_Has_Qualified_Name (Ent);
1548 -- If a variable is hidden by a subsequent loop variable, qualify
1549 -- the name of that loop variable to prevent visibility issues when
1550 -- translating to C. Note that gdb probably never handled properly
1551 -- this accidental hiding, given that loops are not scopes at
1552 -- runtime. We also qualify a name if it hides an outer homonym,
1553 -- and both are declared in blocks.
1555 if Modify_Tree_For_C and then Ekind (Ent) = E_Variable then
1556 if Present (Hiding_Loop_Variable (Ent)) then
1557 declare
1558 Var : constant Entity_Id := Hiding_Loop_Variable (Ent);
1560 begin
1561 Set_Entity_Name (Var);
1562 Add_Str_To_Name_Buffer ("L");
1563 Set_Chars (Var, Name_Enter);
1564 end;
1566 elsif Present (Homonym (Ent))
1567 and then Ekind (Scope (Ent)) = E_Block
1568 and then Ekind (Scope (Homonym (Ent))) = E_Block
1569 then
1570 Set_Entity_Name (Ent);
1571 Add_Str_To_Name_Buffer ("B");
1572 Set_Chars (Ent, Name_Enter);
1573 end if;
1574 end if;
1576 return;
1577 end if;
1579 -- Fall through with a fully qualified name in Name_Buffer/Name_Len
1581 Output_Homonym_Numbers_Suffix;
1583 -- Add body-nested package suffix if required
1585 if BNPE_Suffix_Needed
1586 and then Ekind (Ent) /= E_Enumeration_Literal
1587 then
1588 Set_BNPE_Suffix (Ent);
1590 -- Strip trailing n's and last trailing b as required. note that
1591 -- we know there is at least one b, or no suffix would be generated.
1593 while Name_Buffer (Name_Len) = 'n' loop
1594 Name_Len := Name_Len - 1;
1595 end loop;
1597 Name_Len := Name_Len - 1;
1598 end if;
1600 Set_Chars (Ent, Name_Enter);
1601 Set_Has_Qualified_Name (Ent);
1603 if Debug_Flag_BB then
1604 Write_Str ("*** ");
1605 Write_Name (Save_Chars);
1606 Write_Str (" qualified as ");
1607 Write_Name (Chars (Ent));
1608 Write_Eol;
1609 end if;
1610 end Qualify_Entity_Name;
1612 --------------------------
1613 -- Qualify_Entity_Names --
1614 --------------------------
1616 procedure Qualify_Entity_Names (N : Node_Id) is
1617 begin
1618 Name_Qualify_Units.Append (N);
1619 end Qualify_Entity_Names;
1621 -------------------
1622 -- Reset_Buffers --
1623 -------------------
1625 procedure Reset_Buffers is
1626 begin
1627 Name_Len := 0;
1628 Homonym_Len := 0;
1629 end Reset_Buffers;
1631 --------------------
1632 -- Strip_Suffixes --
1633 --------------------
1635 procedure Strip_Suffixes (BNPE_Suffix_Found : in out Boolean) is
1636 SL : Natural;
1638 pragma Warnings (Off, BNPE_Suffix_Found);
1639 -- Since this procedure only ever sets the flag
1641 begin
1642 -- Search for and strip BNPE suffix
1644 for J in reverse 2 .. Name_Len loop
1645 if Name_Buffer (J) = 'X' then
1646 Name_Len := J - 1;
1647 BNPE_Suffix_Found := True;
1648 exit;
1649 end if;
1651 exit when Name_Buffer (J) /= 'b' and then Name_Buffer (J) /= 'n';
1652 end loop;
1654 -- Search for and strip homonym numbers suffix
1656 for J in reverse 2 .. Name_Len - 2 loop
1657 if Name_Buffer (J) = '_'
1658 and then Name_Buffer (J + 1) = '_'
1659 then
1660 if Name_Buffer (J + 2) in '0' .. '9' then
1661 if Homonym_Len > 0 then
1662 Homonym_Len := Homonym_Len + 1;
1663 Homonym_Numbers (Homonym_Len) := '-';
1664 end if;
1666 SL := Name_Len - (J + 1);
1668 Homonym_Numbers (Homonym_Len + 1 .. Homonym_Len + SL) :=
1669 Name_Buffer (J + 2 .. Name_Len);
1670 Name_Len := J - 1;
1671 Homonym_Len := Homonym_Len + SL;
1672 end if;
1674 exit;
1675 end if;
1676 end loop;
1677 end Strip_Suffixes;
1679 end Exp_Dbug;