Patch ieee128-lib-patch002b
[official-gcc.git] / gcc / ada / exp_dbug.adb
blob5f650984fd0db397eb9fb62fe8044c3dd234a9a0
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-2019, 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;
27 with Atree; use Atree;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Exp_Util; use Exp_Util;
31 with Nlists; use Nlists;
32 with Nmake; use Nmake;
33 with Opt; use Opt;
34 with Output; use Output;
35 with Sem_Aux; use Sem_Aux;
36 with Sem_Eval; use Sem_Eval;
37 with Sem_Util; use Sem_Util;
38 with Sinfo; use Sinfo;
39 with Stand; use Stand;
40 with Stringt; use Stringt;
41 with Table;
42 with Tbuild; use Tbuild;
43 with Urealp; use Urealp;
45 package body Exp_Dbug is
47 -- The following table is used to queue up the entities passed as
48 -- arguments to Qualify_Entity_Names for later processing when
49 -- Qualify_All_Entity_Names is called.
51 package Name_Qualify_Units is new Table.Table (
52 Table_Component_Type => Node_Id,
53 Table_Index_Type => Nat,
54 Table_Low_Bound => 1,
55 Table_Initial => Alloc.Name_Qualify_Units_Initial,
56 Table_Increment => Alloc.Name_Qualify_Units_Increment,
57 Table_Name => "Name_Qualify_Units");
59 --------------------------------
60 -- Use of Qualification Flags --
61 --------------------------------
63 -- There are two flags used to keep track of qualification of entities
65 -- Has_Fully_Qualified_Name
66 -- Has_Qualified_Name
68 -- The difference between these is as follows. Has_Qualified_Name is
69 -- set to indicate that the name has been qualified as required by the
70 -- spec of this package. As described there, this may involve the full
71 -- qualification for the name, but for some entities, notably procedure
72 -- local variables, this full qualification is not required.
74 -- The flag Has_Fully_Qualified_Name is set if indeed the name has been
75 -- fully qualified in the Ada sense. If Has_Fully_Qualified_Name is set,
76 -- then Has_Qualified_Name is also set, but the other way round is not
77 -- the case.
79 -- Consider the following example:
81 -- with ...
82 -- procedure X is
83 -- B : Ddd.Ttt;
84 -- procedure Y is ..
86 -- Here B is a procedure local variable, so it does not need fully
87 -- qualification. The flag Has_Qualified_Name will be set on the
88 -- first attempt to qualify B, to indicate that the job is done
89 -- and need not be redone.
91 -- But Y is qualified as x__y, since procedures are always fully
92 -- qualified, so the first time that an attempt is made to qualify
93 -- the name y, it will be replaced by x__y, and both flags are set.
95 -- Why the two flags? Well there are cases where we derive type names
96 -- from object names. As noted in the spec, type names are always
97 -- fully qualified. Suppose for example that the backend has to build
98 -- a padded type for variable B. then it will construct the PAD name
99 -- from B, but it requires full qualification, so the fully qualified
100 -- type name will be x__b___PAD. The two flags allow the circuit for
101 -- building this name to realize efficiently that b needs further
102 -- qualification.
104 --------------------
105 -- Homonym_Suffix --
106 --------------------
108 -- The string defined here (and its associated length) is used to gather
109 -- the homonym string that will be appended to Name_Buffer when the name
110 -- is complete. Strip_Suffixes appends to this string as does
111 -- Append_Homonym_Number, and Output_Homonym_Numbers_Suffix appends the
112 -- string to the end of Name_Buffer.
114 Homonym_Numbers : String (1 .. 256);
115 Homonym_Len : Natural := 0;
117 ----------------------
118 -- Local Procedures --
119 ----------------------
121 procedure Add_Uint_To_Buffer (U : Uint);
122 -- Add image of universal integer to Name_Buffer, updating Name_Len
124 procedure Add_Real_To_Buffer (U : Ureal);
125 -- Add nnn_ddd to Name_Buffer, where nnn and ddd are integer values of
126 -- the normalized numerator and denominator of the given real value.
128 procedure Append_Homonym_Number (E : Entity_Id);
129 -- If the entity E has homonyms in the same scope, then make an entry
130 -- in the Homonym_Numbers array, bumping Homonym_Count accordingly.
132 function Bounds_Match_Size (E : Entity_Id) return Boolean;
133 -- Determine whether the bounds of E match the size of the type. This is
134 -- used to determine whether encoding is required for a discrete type.
136 function Is_Handled_Scale_Factor (U : Ureal) return Boolean;
137 -- The argument U is the Small_Value of a fixed-point type. This function
138 -- determines whether the back-end can handle this scale factor. When it
139 -- cannot, we have to output a GNAT encoding for the corresponding type.
141 procedure Output_Homonym_Numbers_Suffix;
142 -- If homonym numbers are stored, then output them into Name_Buffer
144 procedure Prepend_String_To_Buffer (S : String);
145 -- Prepend given string to the contents of the string buffer, updating
146 -- the value in Name_Len (i.e. string is added at start of buffer).
148 procedure Prepend_Uint_To_Buffer (U : Uint);
149 -- Prepend image of universal integer to Name_Buffer, updating Name_Len
151 procedure Qualify_Entity_Name (Ent : Entity_Id);
152 -- If not already done, replaces the Chars field of the given entity
153 -- with the appropriate fully qualified name.
155 procedure Reset_Buffers;
156 -- Reset the contents of Name_Buffer and Homonym_Numbers by setting their
157 -- respective lengths to zero.
159 procedure Strip_Suffixes (BNPE_Suffix_Found : in out Boolean);
160 -- Given an qualified entity name in Name_Buffer, remove any plain X or
161 -- X{nb} qualification suffix. The contents of Name_Buffer is not changed
162 -- but Name_Len may be adjusted on return to remove the suffix. If a
163 -- BNPE suffix is found and stripped, then BNPE_Suffix_Found is set to
164 -- True. If no suffix is found, then BNPE_Suffix_Found is not modified.
165 -- This routine also searches for a homonym suffix, and if one is found
166 -- it is also stripped, and the entries are added to the global homonym
167 -- list (Homonym_Numbers) so that they can later be put back.
169 ------------------------
170 -- Add_Real_To_Buffer --
171 ------------------------
173 procedure Add_Real_To_Buffer (U : Ureal) is
174 begin
175 Add_Uint_To_Buffer (Norm_Num (U));
176 Add_Str_To_Name_Buffer ("_");
177 Add_Uint_To_Buffer (Norm_Den (U));
178 end Add_Real_To_Buffer;
180 ------------------------
181 -- Add_Uint_To_Buffer --
182 ------------------------
184 procedure Add_Uint_To_Buffer (U : Uint) is
185 begin
186 if U < 0 then
187 Add_Uint_To_Buffer (-U);
188 Add_Char_To_Name_Buffer ('m');
189 else
190 UI_Image (U, Decimal);
191 Add_Str_To_Name_Buffer (UI_Image_Buffer (1 .. UI_Image_Length));
192 end if;
193 end Add_Uint_To_Buffer;
195 ---------------------------
196 -- Append_Homonym_Number --
197 ---------------------------
199 procedure Append_Homonym_Number (E : Entity_Id) is
201 procedure Add_Nat_To_H (Nr : Nat);
202 -- Little procedure to append Nr to Homonym_Numbers
204 ------------------
205 -- Add_Nat_To_H --
206 ------------------
208 procedure Add_Nat_To_H (Nr : Nat) is
209 begin
210 if Nr >= 10 then
211 Add_Nat_To_H (Nr / 10);
212 end if;
214 Homonym_Len := Homonym_Len + 1;
215 Homonym_Numbers (Homonym_Len) :=
216 Character'Val (Nr mod 10 + Character'Pos ('0'));
217 end Add_Nat_To_H;
219 -- Start of processing for Append_Homonym_Number
221 begin
222 if Has_Homonym (E) then
223 if Homonym_Len > 0 then
224 Homonym_Len := Homonym_Len + 1;
225 Homonym_Numbers (Homonym_Len) := '_';
226 end if;
228 Add_Nat_To_H (Homonym_Number (E));
229 end if;
230 end Append_Homonym_Number;
232 -----------------------
233 -- Bounds_Match_Size --
234 -----------------------
236 function Bounds_Match_Size (E : Entity_Id) return Boolean is
237 Siz : Uint;
239 begin
240 if not Is_OK_Static_Subtype (E) then
241 return False;
243 elsif Is_Integer_Type (E)
244 and then Subtypes_Statically_Match (E, Base_Type (E))
245 then
246 return True;
248 -- Here we check if the static bounds match the natural size, which is
249 -- the size passed through with the debugging information. This is the
250 -- Esize rounded up to 8, 16, 32 or 64 as appropriate.
252 else
253 declare
254 Umark : constant Uintp.Save_Mark := Uintp.Mark;
255 Result : Boolean;
257 begin
258 if Esize (E) <= 8 then
259 Siz := Uint_8;
260 elsif Esize (E) <= 16 then
261 Siz := Uint_16;
262 elsif Esize (E) <= 32 then
263 Siz := Uint_32;
264 else
265 Siz := Uint_64;
266 end if;
268 if Is_Modular_Integer_Type (E) or else Is_Enumeration_Type (E) then
269 Result :=
270 Expr_Rep_Value (Type_Low_Bound (E)) = 0
271 and then
272 2 ** Siz - Expr_Rep_Value (Type_High_Bound (E)) = 1;
274 else
275 Result :=
276 Expr_Rep_Value (Type_Low_Bound (E)) + 2 ** (Siz - 1) = 0
277 and then
278 2 ** (Siz - 1) - Expr_Rep_Value (Type_High_Bound (E)) = 1;
279 end if;
281 Release (Umark);
282 return Result;
283 end;
284 end if;
285 end Bounds_Match_Size;
287 --------------------------------
288 -- Debug_Renaming_Declaration --
289 --------------------------------
291 function Debug_Renaming_Declaration (N : Node_Id) return Node_Id is
292 Loc : constant Source_Ptr := Sloc (N);
293 Ent : constant Node_Id := Defining_Entity (N);
294 Nam : constant Node_Id := Name (N);
295 Ren : Node_Id;
296 Typ : Entity_Id;
297 Obj : Entity_Id;
298 Res : Node_Id;
300 Enable : Boolean := Nkind (N) = N_Package_Renaming_Declaration;
301 -- By default, we do not generate an encoding for renaming. This is
302 -- however done (in which case this is set to True) in a few cases:
303 -- - when a package is renamed,
304 -- - when the renaming involves a packed array,
305 -- - when the renaming involves a packed record.
307 Last_Is_Indexed_Comp : Boolean := False;
308 -- Whether the last subscript value was an indexed component access (XS)
310 procedure Enable_If_Packed_Array (N : Node_Id);
311 -- Enable encoding generation if N is a packed array
313 function Output_Subscript (N : Node_Id; S : String) return Boolean;
314 -- Outputs a single subscript value as ?nnn (subscript is compile time
315 -- known value with value nnn) or as ?e (subscript is local constant
316 -- with name e), where S supplies the proper string to use for ?.
317 -- Returns False if the subscript is not of an appropriate type to
318 -- output in one of these two forms. The result is prepended to the
319 -- name stored in Name_Buffer.
321 function Scope_Contains (Sc : Node_Id; Ent : Entity_Id) return Boolean;
322 -- Return whether Ent belong to the Sc scope
324 ----------------------------
325 -- Enable_If_Packed_Array --
326 ----------------------------
328 procedure Enable_If_Packed_Array (N : Node_Id) is
329 T : constant Entity_Id := Underlying_Type (Etype (N));
331 begin
332 Enable :=
333 Enable
334 or else
335 (Ekind (T) in Array_Kind
336 and then Present (Packed_Array_Impl_Type (T)));
337 end Enable_If_Packed_Array;
339 ----------------------
340 -- Output_Subscript --
341 ----------------------
343 function Output_Subscript (N : Node_Id; S : String) return Boolean is
344 begin
345 if Compile_Time_Known_Value (N) then
346 Prepend_Uint_To_Buffer (Expr_Value (N));
348 elsif Nkind (N) = N_Identifier
349 and then Scope_Contains (Scope (Entity (N)), Ent)
350 and then (Ekind (Entity (N)) = E_Constant
351 or else Ekind (Entity (N)) = E_In_Parameter)
352 then
353 Prepend_String_To_Buffer (Get_Name_String (Chars (Entity (N))));
355 else
356 return False;
357 end if;
359 Prepend_String_To_Buffer (S);
360 return True;
361 end Output_Subscript;
363 --------------------
364 -- Scope_Contains --
365 --------------------
367 function Scope_Contains (Sc : Node_Id; Ent : Entity_Id) return Boolean is
368 Cur : Node_Id := Scope (Ent);
370 begin
371 while Present (Cur) loop
372 if Cur = Sc then
373 return True;
374 end if;
376 Cur := Scope (Cur);
377 end loop;
379 return False;
380 end Scope_Contains;
382 -- Start of processing for Debug_Renaming_Declaration
384 begin
385 if not Comes_From_Source (N) and then not Needs_Debug_Info (Ent) then
386 return Empty;
387 end if;
389 -- Get renamed entity and compute suffix
391 Name_Len := 0;
392 Ren := Nam;
393 loop
394 -- The expression that designates the renamed object is sometimes
395 -- expanded into bit-wise operations. We want to work instead on
396 -- array/record components accesses, so try to analyze the unexpanded
397 -- forms.
399 Ren := Original_Node (Ren);
401 case Nkind (Ren) is
402 when N_Expanded_Name
403 | N_Identifier
405 if not Present (Renamed_Object (Entity (Ren))) then
406 exit;
407 end if;
409 -- This is a renaming of a renaming: traverse until the final
410 -- renaming to see if anything is packed along the way.
412 Ren := Renamed_Object (Entity (Ren));
414 when N_Selected_Component =>
415 declare
416 Sel_Id : constant Entity_Id :=
417 Entity (Selector_Name (Ren));
418 First_Bit : Uint;
420 begin
421 -- If the renaming involves a call to a primitive function,
422 -- we are out of the scope of renaming encodings. We will
423 -- very likely create a variable to hold the renamed value
424 -- anyway, so the renaming entity will be available in
425 -- debuggers.
427 exit when not Ekind_In (Sel_Id, E_Component, E_Discriminant);
429 First_Bit := Normalized_First_Bit (Sel_Id);
430 Enable :=
431 Enable
432 or else Is_Packed
433 (Underlying_Type (Etype (Prefix (Ren))))
434 or else (First_Bit /= No_Uint
435 and then First_Bit /= Uint_0);
436 end;
438 Prepend_String_To_Buffer
439 (Get_Name_String (Chars (Selector_Name (Ren))));
440 Prepend_String_To_Buffer ("XR");
441 Ren := Prefix (Ren);
442 Last_Is_Indexed_Comp := False;
444 when N_Indexed_Component =>
445 declare
446 X : Node_Id;
448 begin
449 Enable_If_Packed_Array (Prefix (Ren));
451 X := Last (Expressions (Ren));
452 while Present (X) loop
453 if not Output_Subscript (X, "XS") then
454 Set_Materialize_Entity (Ent);
455 return Empty;
456 end if;
458 Prev (X);
459 Last_Is_Indexed_Comp := True;
460 end loop;
461 end;
463 Ren := Prefix (Ren);
465 when N_Slice =>
467 -- Assuming X is an array:
468 -- X (Y1 .. Y2) (Y3)
470 -- is equivalent to:
471 -- X (Y3)
473 -- GDB cannot handle packed array slices, so avoid describing
474 -- the slice if we can avoid it.
476 if not Last_Is_Indexed_Comp then
477 Enable_If_Packed_Array (Prefix (Ren));
478 Typ := Etype (First_Index (Etype (Ren)));
480 if not Output_Subscript (Type_High_Bound (Typ), "XS") then
481 Set_Materialize_Entity (Ent);
482 return Empty;
483 end if;
485 if not Output_Subscript (Type_Low_Bound (Typ), "XL") then
486 Set_Materialize_Entity (Ent);
487 return Empty;
488 end if;
490 Last_Is_Indexed_Comp := False;
491 end if;
493 Ren := Prefix (Ren);
495 when N_Explicit_Dereference =>
496 Prepend_String_To_Buffer ("XA");
497 Ren := Prefix (Ren);
498 Last_Is_Indexed_Comp := False;
500 -- For now, anything else simply results in no translation
502 when others =>
503 Set_Materialize_Entity (Ent);
504 return Empty;
505 end case;
506 end loop;
508 -- If we found no reason here to emit an encoding, stop now
510 if not Enable then
511 Set_Materialize_Entity (Ent);
512 return Empty;
513 end if;
515 Prepend_String_To_Buffer ("___XE");
517 -- Include the designation of the form of renaming
519 case Nkind (N) is
520 when N_Object_Renaming_Declaration =>
521 Prepend_String_To_Buffer ("___XR");
523 when N_Exception_Renaming_Declaration =>
524 Prepend_String_To_Buffer ("___XRE");
526 when N_Package_Renaming_Declaration =>
527 Prepend_String_To_Buffer ("___XRP");
529 when others =>
530 return Empty;
531 end case;
533 -- Add the name of the renaming entity to the front
535 Prepend_String_To_Buffer (Get_Name_String (Chars (Ent)));
537 -- If it is a child unit create a fully qualified name, to disambiguate
538 -- multiple child units with the same name and different parents.
540 if Nkind (N) = N_Package_Renaming_Declaration
541 and then Is_Child_Unit (Ent)
542 then
543 Prepend_String_To_Buffer ("__");
544 Prepend_String_To_Buffer
545 (Get_Name_String (Chars (Scope (Ent))));
546 end if;
548 -- Create the special object whose name is the debug encoding for the
549 -- renaming declaration.
551 -- For now, the object name contains the suffix encoding for the renamed
552 -- object, but not the name of the leading entity. The object is linked
553 -- the renamed entity using the Debug_Renaming_Link field. Then the
554 -- Qualify_Entity_Name procedure uses this link to create the proper
555 -- fully qualified name.
557 -- The reason we do things this way is that we really need to copy the
558 -- qualification of the renamed entity, and it is really much easier to
559 -- do this after the renamed entity has itself been fully qualified.
561 Obj := Make_Defining_Identifier (Loc, Chars => Name_Enter);
562 Res :=
563 Make_Object_Declaration (Loc,
564 Defining_Identifier => Obj,
565 Object_Definition => New_Occurrence_Of
566 (Standard_Debug_Renaming_Type, Loc));
568 Set_Debug_Renaming_Link (Obj, Entity (Ren));
570 Set_Debug_Info_Needed (Obj);
572 -- The renamed entity may be a temporary, e.g. the result of an
573 -- implicit dereference in an iterator. Indicate that the temporary
574 -- itself requires debug information. If the renamed entity comes
575 -- from source this is a no-op.
577 Set_Debug_Info_Needed (Entity (Ren));
579 -- Mark the object as internal so that it won't be initialized when
580 -- pragma Initialize_Scalars or Normalize_Scalars is in use.
582 Set_Is_Internal (Obj);
584 return Res;
586 -- If we get an exception, just figure it is a case that we cannot
587 -- successfully handle using our current approach, since this is
588 -- only for debugging, no need to take the compilation with us.
590 exception
591 when others =>
592 return Make_Null_Statement (Loc);
593 end Debug_Renaming_Declaration;
595 -----------------------------
596 -- Is_Handled_Scale_Factor --
597 -----------------------------
599 function Is_Handled_Scale_Factor (U : Ureal) return Boolean is
600 begin
601 -- Keep in sync with gigi (see E_*_Fixed_Point_Type handling in
602 -- decl.c:gnat_to_gnu_entity).
604 if UI_Eq (Numerator (U), Uint_1) then
605 if Rbase (U) = 2 or else Rbase (U) = 10 then
606 return True;
607 end if;
608 end if;
610 return
611 (UI_Is_In_Int_Range (Norm_Num (U))
612 and then
613 UI_Is_In_Int_Range (Norm_Den (U)));
614 end Is_Handled_Scale_Factor;
616 ----------------------
617 -- Get_Encoded_Name --
618 ----------------------
620 -- Note: see spec for details on encodings
622 procedure Get_Encoded_Name (E : Entity_Id) is
623 Has_Suffix : Boolean;
625 begin
626 -- If not generating code, there is no need to create encoded names, and
627 -- problems when the back-end is called to annotate types without full
628 -- code generation. See comments in Get_External_Name for additional
629 -- details.
631 -- However we do create encoded names if the back end is active, even
632 -- if Operating_Mode got reset. Otherwise any serious error reported
633 -- by the backend calling Error_Msg changes the Compilation_Mode to
634 -- Check_Semantics, which disables the functionality of this routine,
635 -- causing the generation of spurious additional errors.
637 -- Couldn't we just test Original_Operating_Mode here? ???
639 if Operating_Mode /= Generate_Code and then not Generating_Code then
640 return;
641 end if;
643 Get_Name_String (Chars (E));
645 -- Nothing to do if we do not have a type
647 if not Is_Type (E)
649 -- Or if this is an enumeration base type
651 or else (Is_Enumeration_Type (E) and then Is_Base_Type (E))
653 -- Or if this is a dummy type for a renaming
655 or else (Name_Len >= 3 and then
656 Name_Buffer (Name_Len - 2 .. Name_Len) = "_XR")
658 or else (Name_Len >= 4 and then
659 (Name_Buffer (Name_Len - 3 .. Name_Len) = "_XRE"
660 or else
661 Name_Buffer (Name_Len - 3 .. Name_Len) = "_XRP"))
663 -- For all these cases, just return the name unchanged
665 then
666 Name_Buffer (Name_Len + 1) := ASCII.NUL;
667 return;
668 end if;
670 Has_Suffix := True;
672 -- Fixed-point case: generate GNAT encodings when asked to or when we
673 -- know the back-end will not be able to handle the scale factor.
675 if Is_Fixed_Point_Type (E)
676 and then (GNAT_Encodings /= DWARF_GNAT_Encodings_Minimal
677 or else not Is_Handled_Scale_Factor (Small_Value (E)))
678 then
679 Get_External_Name (E, True, "XF_");
680 Add_Real_To_Buffer (Delta_Value (E));
682 if Small_Value (E) /= Delta_Value (E) then
683 Add_Str_To_Name_Buffer ("_");
684 Add_Real_To_Buffer (Small_Value (E));
685 end if;
687 -- Discrete case where bounds do not match size. Not necessary if we can
688 -- emit standard DWARF.
690 elsif GNAT_Encodings /= DWARF_GNAT_Encodings_Minimal
691 and then Is_Discrete_Type (E)
692 and then not Bounds_Match_Size (E)
693 then
694 declare
695 Lo : constant Node_Id := Type_Low_Bound (E);
696 Hi : constant Node_Id := Type_High_Bound (E);
698 Lo_Con : constant Boolean := Compile_Time_Known_Value (Lo);
699 Hi_Con : constant Boolean := Compile_Time_Known_Value (Hi);
701 Lo_Discr : constant Boolean :=
702 Nkind (Lo) = N_Identifier
703 and then Ekind (Entity (Lo)) = E_Discriminant;
705 Hi_Discr : constant Boolean :=
706 Nkind (Hi) = N_Identifier
707 and then Ekind (Entity (Hi)) = E_Discriminant;
709 Lo_Encode : constant Boolean := Lo_Con or Lo_Discr;
710 Hi_Encode : constant Boolean := Hi_Con or Hi_Discr;
712 Biased : constant Boolean := Has_Biased_Representation (E);
714 begin
715 if Biased then
716 Get_External_Name (E, True, "XB");
717 else
718 Get_External_Name (E, True, "XD");
719 end if;
721 if Lo_Encode or Hi_Encode then
722 if Biased then
723 Add_Str_To_Name_Buffer ("_");
724 else
725 if Lo_Encode then
726 if Hi_Encode then
727 Add_Str_To_Name_Buffer ("LU_");
728 else
729 Add_Str_To_Name_Buffer ("L_");
730 end if;
731 else
732 Add_Str_To_Name_Buffer ("U_");
733 end if;
734 end if;
736 if Lo_Con then
737 Add_Uint_To_Buffer (Expr_Rep_Value (Lo));
738 elsif Lo_Discr then
739 Get_Name_String_And_Append (Chars (Entity (Lo)));
740 end if;
742 if Lo_Encode and Hi_Encode then
743 Add_Str_To_Name_Buffer ("__");
744 end if;
746 if Hi_Con then
747 Add_Uint_To_Buffer (Expr_Rep_Value (Hi));
748 elsif Hi_Discr then
749 Get_Name_String_And_Append (Chars (Entity (Hi)));
750 end if;
751 end if;
752 end;
754 -- For all other cases, the encoded name is the normal type name
756 else
757 Has_Suffix := False;
758 Get_External_Name (E);
759 end if;
761 if Debug_Flag_B and then Has_Suffix then
762 Write_Str ("**** type ");
763 Write_Name (Chars (E));
764 Write_Str (" is encoded as ");
765 Write_Str (Name_Buffer (1 .. Name_Len));
766 Write_Eol;
767 end if;
769 Name_Buffer (Name_Len + 1) := ASCII.NUL;
770 end Get_Encoded_Name;
772 -----------------------
773 -- Get_External_Name --
774 -----------------------
776 procedure Get_External_Name
777 (Entity : Entity_Id;
778 Has_Suffix : Boolean := False;
779 Suffix : String := "")
781 procedure Get_Qualified_Name_And_Append (Entity : Entity_Id);
782 -- Appends fully qualified name of given entity to Name_Buffer
784 -----------------------------------
785 -- Get_Qualified_Name_And_Append --
786 -----------------------------------
788 procedure Get_Qualified_Name_And_Append (Entity : Entity_Id) is
789 begin
790 -- If the entity is a compilation unit, its scope is Standard,
791 -- there is no outer scope, and the no further qualification
792 -- is required.
794 -- If the front end has already computed a fully qualified name,
795 -- then it is also the case that no further qualification is
796 -- required.
798 if Present (Scope (Scope (Entity)))
799 and then not Has_Fully_Qualified_Name (Entity)
800 then
801 Get_Qualified_Name_And_Append (Scope (Entity));
802 Add_Str_To_Name_Buffer ("__");
803 Get_Name_String_And_Append (Chars (Entity));
804 Append_Homonym_Number (Entity);
806 else
807 Get_Name_String_And_Append (Chars (Entity));
808 end if;
809 end Get_Qualified_Name_And_Append;
811 -- Local variables
813 E : Entity_Id := Entity;
815 -- Start of processing for Get_External_Name
817 begin
818 -- If we are not in code generation mode, this procedure may still be
819 -- called from Back_End (more specifically - from gigi for doing type
820 -- representation annotation or some representation-specific checks).
821 -- But in this mode there is no need to mess with external names.
823 -- Furthermore, the call causes difficulties in this case because the
824 -- string representing the homonym number is not correctly reset as a
825 -- part of the call to Output_Homonym_Numbers_Suffix (which is not
826 -- called in gigi).
828 if Operating_Mode /= Generate_Code then
829 return;
830 end if;
832 Reset_Buffers;
834 -- If this is a child unit, we want the child
836 if Nkind (E) = N_Defining_Program_Unit_Name then
837 E := Defining_Identifier (Entity);
838 end if;
840 -- Case of interface name being used
842 if Ekind_In (E, E_Constant,
843 E_Exception,
844 E_Function,
845 E_Procedure,
846 E_Variable)
847 and then Present (Interface_Name (E))
848 and then No (Address_Clause (E))
849 and then not Has_Suffix
850 then
851 Append (Global_Name_Buffer, Strval (Interface_Name (E)));
853 -- All other cases besides the interface name case
855 else
856 -- If this is a library level subprogram (i.e. a subprogram that is a
857 -- compilation unit other than a subunit), then we prepend _ada_ to
858 -- ensure distinctions required as described in the spec.
860 -- Check explicitly for child units, because those are not flagged
861 -- as Compilation_Units by lib. Should they be ???
863 if Is_Subprogram (E)
864 and then (Is_Compilation_Unit (E) or Is_Child_Unit (E))
865 and then not Has_Suffix
866 then
867 Add_Str_To_Name_Buffer ("_ada_");
868 end if;
870 -- If the entity is a subprogram instance that is not a compilation
871 -- unit, generate the name of the original Ada entity, which is the
872 -- one gdb needs.
874 if Is_Generic_Instance (E)
875 and then Is_Subprogram (E)
876 and then not Is_Compilation_Unit (Scope (E))
877 and then Ekind_In (Scope (E), E_Package, E_Package_Body)
878 and then Present (Related_Instance (Scope (E)))
879 then
880 E := Related_Instance (Scope (E));
881 end if;
883 Get_Qualified_Name_And_Append (E);
884 end if;
886 if Has_Suffix then
887 Add_Str_To_Name_Buffer ("___");
888 Add_Str_To_Name_Buffer (Suffix);
889 end if;
891 -- Add a special prefix to distinguish Ghost entities. In Ignored Ghost
892 -- mode, these entities should not leak in the "living" space and they
893 -- should be removed by the compiler in a post-processing pass. Thus,
894 -- the prefix allows anyone to check that the final executable indeed
895 -- does not contain such entities, in such a case. Do not insert this
896 -- prefix for compilation units, whose name is used as a basis for the
897 -- name of the generated elaboration procedure and (when appropriate)
898 -- the executable produced. Only insert this prefix once, for Ghost
899 -- entities declared inside other Ghost entities. Three leading
900 -- underscores are used so that "___ghost_" is a unique substring of
901 -- names produced for Ghost entities, while "__ghost_" can appear in
902 -- names of entities inside a child/local package called "Ghost".
904 -- The compiler-generated finalizer for an enabled Ghost unit is treated
905 -- specially, as its name must be known to the binder, which has no
906 -- knowledge of Ghost status. In that case, the finalizer is not marked
907 -- as Ghost so that no prefix is added. Note that the special ___ghost_
908 -- prefix is retained when the Ghost unit is ignored, which still allows
909 -- inspecting the final executable for the presence of an ignored Ghost
910 -- finalizer procedure.
912 if Is_Ghost_Entity (E)
913 and then not Is_Compilation_Unit (E)
914 and then (Name_Len < 9
915 or else Name_Buffer (1 .. 9) /= "___ghost_")
916 then
917 Insert_Str_In_Name_Buffer ("___ghost_", 1);
918 end if;
920 Name_Buffer (Name_Len + 1) := ASCII.NUL;
921 end Get_External_Name;
923 --------------------------
924 -- Get_Variant_Encoding --
925 --------------------------
927 procedure Get_Variant_Encoding (V : Node_Id) is
928 Choice : Node_Id;
930 procedure Choice_Val (Typ : Character; Choice : Node_Id);
931 -- Output encoded value for a single choice value. Typ is the key
932 -- character ('S', 'F', or 'T') that precedes the choice value.
934 ----------------
935 -- Choice_Val --
936 ----------------
938 procedure Choice_Val (Typ : Character; Choice : Node_Id) is
939 begin
940 if Nkind (Choice) = N_Integer_Literal then
941 Add_Char_To_Name_Buffer (Typ);
942 Add_Uint_To_Buffer (Intval (Choice));
944 -- Character literal with no entity present (this is the case
945 -- Standard.Character or Standard.Wide_Character as root type)
947 elsif Nkind (Choice) = N_Character_Literal
948 and then No (Entity (Choice))
949 then
950 Add_Char_To_Name_Buffer (Typ);
951 Add_Uint_To_Buffer (Char_Literal_Value (Choice));
953 else
954 declare
955 Ent : constant Entity_Id := Entity (Choice);
957 begin
958 if Ekind (Ent) = E_Enumeration_Literal then
959 Add_Char_To_Name_Buffer (Typ);
960 Add_Uint_To_Buffer (Enumeration_Rep (Ent));
962 else
963 pragma Assert (Ekind (Ent) = E_Constant);
964 Choice_Val (Typ, Constant_Value (Ent));
965 end if;
966 end;
967 end if;
968 end Choice_Val;
970 -- Start of processing for Get_Variant_Encoding
972 begin
973 Name_Len := 0;
975 Choice := First (Discrete_Choices (V));
976 while Present (Choice) loop
977 if Nkind (Choice) = N_Others_Choice then
978 Add_Char_To_Name_Buffer ('O');
980 elsif Nkind (Choice) = N_Range then
981 Choice_Val ('R', Low_Bound (Choice));
982 Choice_Val ('T', High_Bound (Choice));
984 elsif Is_Entity_Name (Choice)
985 and then Is_Type (Entity (Choice))
986 then
987 Choice_Val ('R', Type_Low_Bound (Entity (Choice)));
988 Choice_Val ('T', Type_High_Bound (Entity (Choice)));
990 elsif Nkind (Choice) = N_Subtype_Indication then
991 declare
992 Rang : constant Node_Id :=
993 Range_Expression (Constraint (Choice));
994 begin
995 Choice_Val ('R', Low_Bound (Rang));
996 Choice_Val ('T', High_Bound (Rang));
997 end;
999 else
1000 Choice_Val ('S', Choice);
1001 end if;
1003 Next (Choice);
1004 end loop;
1006 Name_Buffer (Name_Len + 1) := ASCII.NUL;
1008 if Debug_Flag_B then
1009 declare
1010 VP : constant Node_Id := Parent (V); -- Variant_Part
1011 CL : constant Node_Id := Parent (VP); -- Component_List
1012 RD : constant Node_Id := Parent (CL); -- Record_Definition
1013 FT : constant Node_Id := Parent (RD); -- Full_Type_Declaration
1015 begin
1016 Write_Str ("**** variant for type ");
1017 Write_Name (Chars (Defining_Identifier (FT)));
1018 Write_Str (" is encoded as ");
1019 Write_Str (Name_Buffer (1 .. Name_Len));
1020 Write_Eol;
1021 end;
1022 end if;
1023 end Get_Variant_Encoding;
1025 -----------------------------------------
1026 -- Build_Subprogram_Instance_Renamings --
1027 -----------------------------------------
1029 procedure Build_Subprogram_Instance_Renamings
1030 (N : Node_Id;
1031 Wrapper : Entity_Id)
1033 Loc : Source_Ptr;
1034 Decl : Node_Id;
1035 E : Entity_Id;
1037 begin
1038 E := First_Entity (Wrapper);
1039 while Present (E) loop
1040 if Nkind (Parent (E)) = N_Object_Declaration
1041 and then Is_Elementary_Type (Etype (E))
1042 then
1043 Loc := Sloc (Expression (Parent (E)));
1044 Decl := Make_Object_Renaming_Declaration (Loc,
1045 Defining_Identifier =>
1046 Make_Defining_Identifier (Loc, Chars (E)),
1047 Subtype_Mark => New_Occurrence_Of (Etype (E), Loc),
1048 Name => New_Occurrence_Of (E, Loc));
1050 Append (Decl, Declarations (N));
1051 Set_Debug_Info_Needed (Defining_Identifier (Decl));
1052 end if;
1054 Next_Entity (E);
1055 end loop;
1056 end Build_Subprogram_Instance_Renamings;
1058 ------------------------------------
1059 -- Get_Secondary_DT_External_Name --
1060 ------------------------------------
1062 procedure Get_Secondary_DT_External_Name
1063 (Typ : Entity_Id;
1064 Ancestor_Typ : Entity_Id;
1065 Suffix_Index : Int)
1067 begin
1068 Get_External_Name (Typ);
1070 if Ancestor_Typ /= Typ then
1071 declare
1072 Len : constant Natural := Name_Len;
1073 Save_Str : constant String (1 .. Name_Len)
1074 := Name_Buffer (1 .. Name_Len);
1075 begin
1076 Get_External_Name (Ancestor_Typ);
1078 -- Append the extended name of the ancestor to the
1079 -- extended name of Typ
1081 Name_Buffer (Len + 2 .. Len + Name_Len + 1) :=
1082 Name_Buffer (1 .. Name_Len);
1083 Name_Buffer (1 .. Len) := Save_Str;
1084 Name_Buffer (Len + 1) := '_';
1085 Name_Len := Len + Name_Len + 1;
1086 end;
1087 end if;
1089 Add_Nat_To_Name_Buffer (Suffix_Index);
1090 end Get_Secondary_DT_External_Name;
1092 ---------------------------------
1093 -- Make_Packed_Array_Impl_Type_Name --
1094 ---------------------------------
1096 function Make_Packed_Array_Impl_Type_Name
1097 (Typ : Entity_Id;
1098 Csize : Uint)
1099 return Name_Id
1101 begin
1102 Get_Name_String (Chars (Typ));
1103 Add_Str_To_Name_Buffer ("___XP");
1104 Add_Uint_To_Buffer (Csize);
1105 return Name_Find;
1106 end Make_Packed_Array_Impl_Type_Name;
1108 -----------------------------------
1109 -- Output_Homonym_Numbers_Suffix --
1110 -----------------------------------
1112 procedure Output_Homonym_Numbers_Suffix is
1113 J : Natural;
1115 begin
1116 if Homonym_Len > 0 then
1118 -- Check for all 1's, in which case we do not output
1120 J := 1;
1121 loop
1122 exit when Homonym_Numbers (J) /= '1';
1124 -- If we reached end of string we do not output
1126 if J = Homonym_Len then
1127 Homonym_Len := 0;
1128 return;
1129 end if;
1131 exit when Homonym_Numbers (J + 1) /= '_';
1132 J := J + 2;
1133 end loop;
1135 -- If we exit the loop then suffix must be output
1137 Add_Str_To_Name_Buffer ("__");
1138 Add_Str_To_Name_Buffer (Homonym_Numbers (1 .. Homonym_Len));
1139 Homonym_Len := 0;
1140 end if;
1141 end Output_Homonym_Numbers_Suffix;
1143 ------------------------------
1144 -- Prepend_String_To_Buffer --
1145 ------------------------------
1147 procedure Prepend_String_To_Buffer (S : String) is
1148 N : constant Integer := S'Length;
1149 begin
1150 Name_Buffer (1 + N .. Name_Len + N) := Name_Buffer (1 .. Name_Len);
1151 Name_Buffer (1 .. N) := S;
1152 Name_Len := Name_Len + N;
1153 end Prepend_String_To_Buffer;
1155 ----------------------------
1156 -- Prepend_Uint_To_Buffer --
1157 ----------------------------
1159 procedure Prepend_Uint_To_Buffer (U : Uint) is
1160 begin
1161 if U < 0 then
1162 Prepend_String_To_Buffer ("m");
1163 Prepend_Uint_To_Buffer (-U);
1164 else
1165 UI_Image (U, Decimal);
1166 Prepend_String_To_Buffer (UI_Image_Buffer (1 .. UI_Image_Length));
1167 end if;
1168 end Prepend_Uint_To_Buffer;
1170 ------------------------------
1171 -- Qualify_All_Entity_Names --
1172 ------------------------------
1174 procedure Qualify_All_Entity_Names is
1175 E : Entity_Id;
1176 Ent : Entity_Id;
1177 Nod : Node_Id;
1179 begin
1180 for J in Name_Qualify_Units.First .. Name_Qualify_Units.Last loop
1181 Nod := Name_Qualify_Units.Table (J);
1183 -- When a scoping construct is ignored Ghost, it is rewritten as
1184 -- a null statement. Skip such constructs as they no longer carry
1185 -- names.
1187 if Nkind (Nod) = N_Null_Statement then
1188 goto Continue;
1189 end if;
1191 E := Defining_Entity (Nod);
1192 Reset_Buffers;
1193 Qualify_Entity_Name (E);
1195 -- Normally entities in the qualification list are scopes, but in the
1196 -- case of a library-level package renaming there is an associated
1197 -- variable that encodes the debugger name and that variable is
1198 -- entered in the list since it occurs in the Aux_Decls list of the
1199 -- compilation and doesn't have a normal scope.
1201 if Ekind (E) /= E_Variable then
1202 Ent := First_Entity (E);
1203 while Present (Ent) loop
1204 Reset_Buffers;
1205 Qualify_Entity_Name (Ent);
1206 Next_Entity (Ent);
1208 -- There are odd cases where Last_Entity (E) = E. This happens
1209 -- in the case of renaming of packages. This test avoids
1210 -- getting stuck in such cases.
1212 exit when Ent = E;
1213 end loop;
1214 end if;
1216 <<Continue>>
1217 null;
1218 end loop;
1219 end Qualify_All_Entity_Names;
1221 -------------------------
1222 -- Qualify_Entity_Name --
1223 -------------------------
1225 procedure Qualify_Entity_Name (Ent : Entity_Id) is
1227 Full_Qualify_Name : String (1 .. Name_Buffer'Length);
1228 Full_Qualify_Len : Natural := 0;
1229 -- Used to accumulate fully qualified name of subprogram
1231 procedure Fully_Qualify_Name (E : Entity_Id);
1232 -- Used to qualify a subprogram or type name, where full
1233 -- qualification up to Standard is always used. Name is set
1234 -- in Full_Qualify_Name with the length in Full_Qualify_Len.
1235 -- Note that this routine does not prepend the _ada_ string
1236 -- required for library subprograms (this is done in the back end).
1238 function Is_BNPE (S : Entity_Id) return Boolean;
1239 -- Determines if S is a BNPE, i.e. Body-Nested Package Entity, which
1240 -- is defined to be a package which is immediately nested within a
1241 -- package body.
1243 function Qualify_Needed (S : Entity_Id) return Boolean;
1244 -- Given a scope, determines if the scope is to be included in the
1245 -- fully qualified name, True if so, False if not. Blocks and loops
1246 -- are excluded from a qualified name.
1248 procedure Set_BNPE_Suffix (E : Entity_Id);
1249 -- Recursive routine to append the BNPE qualification suffix. Works
1250 -- from right to left with E being the current entity in the list.
1251 -- The result does NOT have the trailing n's and trailing b stripped.
1252 -- The caller must do this required stripping.
1254 procedure Set_Entity_Name (E : Entity_Id);
1255 -- Internal recursive routine that does most of the work. This routine
1256 -- leaves the result sitting in Name_Buffer and Name_Len.
1258 BNPE_Suffix_Needed : Boolean := False;
1259 -- Set true if a body-nested package entity suffix is required
1261 Save_Chars : constant Name_Id := Chars (Ent);
1262 -- Save original name
1264 ------------------------
1265 -- Fully_Qualify_Name --
1266 ------------------------
1268 procedure Fully_Qualify_Name (E : Entity_Id) is
1269 Discard : Boolean := False;
1271 begin
1272 -- Ignore empty entry (can happen in error cases)
1274 if No (E) then
1275 return;
1277 -- If this we are qualifying entities local to a generic instance,
1278 -- use the name of the original instantiation, not that of the
1279 -- anonymous subprogram in the wrapper package, so that gdb doesn't
1280 -- have to know about these.
1282 elsif Is_Generic_Instance (E)
1283 and then Is_Subprogram (E)
1284 and then not Comes_From_Source (E)
1285 and then not Is_Compilation_Unit (Scope (E))
1286 then
1287 Fully_Qualify_Name (Related_Instance (Scope (E)));
1288 return;
1289 end if;
1291 -- If we reached fully qualified name, then just copy it
1293 if Has_Fully_Qualified_Name (E) then
1294 Get_Name_String (Chars (E));
1295 Strip_Suffixes (Discard);
1296 Full_Qualify_Name (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
1297 Full_Qualify_Len := Name_Len;
1298 Set_Has_Fully_Qualified_Name (Ent);
1300 -- Case of non-fully qualified name
1302 else
1303 if Scope (E) = Standard_Standard then
1304 Set_Has_Fully_Qualified_Name (Ent);
1305 else
1306 Fully_Qualify_Name (Scope (E));
1307 Full_Qualify_Name (Full_Qualify_Len + 1) := '_';
1308 Full_Qualify_Name (Full_Qualify_Len + 2) := '_';
1309 Full_Qualify_Len := Full_Qualify_Len + 2;
1310 end if;
1312 if Has_Qualified_Name (E) then
1313 Get_Unqualified_Name_String (Chars (E));
1314 else
1315 Get_Name_String (Chars (E));
1316 end if;
1318 -- Here we do one step of the qualification
1320 Full_Qualify_Name
1321 (Full_Qualify_Len + 1 .. Full_Qualify_Len + Name_Len) :=
1322 Name_Buffer (1 .. Name_Len);
1323 Full_Qualify_Len := Full_Qualify_Len + Name_Len;
1324 Append_Homonym_Number (E);
1325 end if;
1327 if Is_BNPE (E) then
1328 BNPE_Suffix_Needed := True;
1329 end if;
1330 end Fully_Qualify_Name;
1332 -------------
1333 -- Is_BNPE --
1334 -------------
1336 function Is_BNPE (S : Entity_Id) return Boolean is
1337 begin
1338 return Ekind (S) = E_Package and then Is_Package_Body_Entity (S);
1339 end Is_BNPE;
1341 --------------------
1342 -- Qualify_Needed --
1343 --------------------
1345 function Qualify_Needed (S : Entity_Id) return Boolean is
1346 begin
1347 -- If we got all the way to Standard, then we have certainly
1348 -- fully qualified the name, so set the flag appropriately,
1349 -- and then return False, since we are most certainly done.
1351 if S = Standard_Standard then
1352 Set_Has_Fully_Qualified_Name (Ent, True);
1353 return False;
1355 -- Otherwise figure out if further qualification is required
1357 else
1358 return Is_Subprogram (Ent)
1359 or else Ekind (Ent) = E_Subprogram_Body
1360 or else (Ekind (S) /= E_Block
1361 and then Ekind (S) /= E_Loop
1362 and then not Is_Dynamic_Scope (S));
1363 end if;
1364 end Qualify_Needed;
1366 ---------------------
1367 -- Set_BNPE_Suffix --
1368 ---------------------
1370 procedure Set_BNPE_Suffix (E : Entity_Id) is
1371 S : constant Entity_Id := Scope (E);
1373 begin
1374 if Qualify_Needed (S) then
1375 Set_BNPE_Suffix (S);
1377 if Is_BNPE (E) then
1378 Add_Char_To_Name_Buffer ('b');
1379 else
1380 Add_Char_To_Name_Buffer ('n');
1381 end if;
1383 else
1384 Add_Char_To_Name_Buffer ('X');
1385 end if;
1386 end Set_BNPE_Suffix;
1388 ---------------------
1389 -- Set_Entity_Name --
1390 ---------------------
1392 procedure Set_Entity_Name (E : Entity_Id) is
1393 S : constant Entity_Id := Scope (E);
1395 begin
1396 -- If we reach an already qualified name, just take the encoding
1397 -- except that we strip the package body suffixes, since these
1398 -- will be separately put on later.
1400 if Has_Qualified_Name (E) then
1401 Get_Name_String_And_Append (Chars (E));
1402 Strip_Suffixes (BNPE_Suffix_Needed);
1404 -- If the top level name we are adding is itself fully
1405 -- qualified, then that means that the name that we are
1406 -- preparing for the Fully_Qualify_Name call will also
1407 -- generate a fully qualified name.
1409 if Has_Fully_Qualified_Name (E) then
1410 Set_Has_Fully_Qualified_Name (Ent);
1411 end if;
1413 -- Case where upper level name is not encoded yet
1415 else
1416 -- Recurse if further qualification required
1418 if Qualify_Needed (S) then
1419 Set_Entity_Name (S);
1420 Add_Str_To_Name_Buffer ("__");
1421 end if;
1423 -- Otherwise get name and note if it is a BNPE
1425 Get_Name_String_And_Append (Chars (E));
1427 if Is_BNPE (E) then
1428 BNPE_Suffix_Needed := True;
1429 end if;
1431 Append_Homonym_Number (E);
1432 end if;
1433 end Set_Entity_Name;
1435 -- Start of processing for Qualify_Entity_Name
1437 begin
1438 if Has_Qualified_Name (Ent) then
1439 return;
1441 -- If the entity is a variable encoding the debug name for an object
1442 -- renaming, then the qualified name of the entity associated with the
1443 -- renamed object can now be incorporated in the debug name.
1445 elsif Ekind (Ent) = E_Variable
1446 and then Present (Debug_Renaming_Link (Ent))
1447 then
1448 Name_Len := 0;
1449 Qualify_Entity_Name (Debug_Renaming_Link (Ent));
1450 Get_Name_String (Chars (Ent));
1452 -- Retrieve the now-qualified name of the renamed entity and insert
1453 -- it in the middle of the name, just preceding the suffix encoding
1454 -- describing the renamed object.
1456 declare
1457 Renamed_Id : constant String :=
1458 Get_Name_String (Chars (Debug_Renaming_Link (Ent)));
1459 Insert_Len : constant Integer := Renamed_Id'Length + 1;
1460 Index : Natural := Name_Len - 3;
1462 begin
1463 -- Loop backwards through the name to find the start of the "___"
1464 -- sequence associated with the suffix.
1466 while Index >= Name_Buffer'First
1467 and then (Name_Buffer (Index + 1) /= '_'
1468 or else Name_Buffer (Index + 2) /= '_'
1469 or else Name_Buffer (Index + 3) /= '_')
1470 loop
1471 Index := Index - 1;
1472 end loop;
1474 pragma Assert (Name_Buffer (Index + 1 .. Index + 3) = "___");
1476 -- Insert an underscore separator and the entity name just in
1477 -- front of the suffix.
1479 Name_Buffer (Index + 1 + Insert_Len .. Name_Len + Insert_Len) :=
1480 Name_Buffer (Index + 1 .. Name_Len);
1481 Name_Buffer (Index + 1) := '_';
1482 Name_Buffer (Index + 2 .. Index + Insert_Len) := Renamed_Id;
1483 Name_Len := Name_Len + Insert_Len;
1484 end;
1486 -- Reset the name of the variable to the new name that includes the
1487 -- name of the renamed entity.
1489 Set_Chars (Ent, Name_Enter);
1491 -- If the entity needs qualification by its scope then develop it
1492 -- here, add the variable's name, and again reset the entity name.
1494 if Qualify_Needed (Scope (Ent)) then
1495 Name_Len := 0;
1496 Set_Entity_Name (Scope (Ent));
1497 Add_Str_To_Name_Buffer ("__");
1499 Get_Name_String_And_Append (Chars (Ent));
1501 Set_Chars (Ent, Name_Enter);
1502 end if;
1504 Set_Has_Qualified_Name (Ent);
1505 return;
1507 elsif Is_Subprogram (Ent)
1508 or else Ekind (Ent) = E_Subprogram_Body
1509 or else Is_Type (Ent)
1510 or else Ekind (Ent) = E_Exception
1511 then
1512 Fully_Qualify_Name (Ent);
1513 Name_Len := Full_Qualify_Len;
1514 Name_Buffer (1 .. Name_Len) := Full_Qualify_Name (1 .. Name_Len);
1516 -- Qualification needed for enumeration literals when generating C code
1517 -- (to simplify their management in the backend).
1519 elsif Modify_Tree_For_C
1520 and then Ekind (Ent) = E_Enumeration_Literal
1521 and then Scope (Ultimate_Alias (Ent)) /= Standard_Standard
1522 then
1523 Fully_Qualify_Name (Ent);
1524 Name_Len := Full_Qualify_Len;
1525 Name_Buffer (1 .. Name_Len) := Full_Qualify_Name (1 .. Name_Len);
1527 elsif Qualify_Needed (Scope (Ent)) then
1528 Name_Len := 0;
1529 Set_Entity_Name (Ent);
1531 else
1532 Set_Has_Qualified_Name (Ent);
1534 -- If a variable is hidden by a subsequent loop variable, qualify
1535 -- the name of that loop variable to prevent visibility issues when
1536 -- translating to C. Note that gdb probably never handled properly
1537 -- this accidental hiding, given that loops are not scopes at
1538 -- runtime. We also qualify a name if it hides an outer homonym,
1539 -- and both are declared in blocks.
1541 if Modify_Tree_For_C and then Ekind (Ent) = E_Variable then
1542 if Present (Hiding_Loop_Variable (Ent)) then
1543 declare
1544 Var : constant Entity_Id := Hiding_Loop_Variable (Ent);
1546 begin
1547 Set_Entity_Name (Var);
1548 Add_Str_To_Name_Buffer ("L");
1549 Set_Chars (Var, Name_Enter);
1550 end;
1552 elsif Present (Homonym (Ent))
1553 and then Ekind (Scope (Ent)) = E_Block
1554 and then Ekind (Scope (Homonym (Ent))) = E_Block
1555 then
1556 Set_Entity_Name (Ent);
1557 Add_Str_To_Name_Buffer ("B");
1558 Set_Chars (Ent, Name_Enter);
1559 end if;
1560 end if;
1562 return;
1563 end if;
1565 -- Fall through with a fully qualified name in Name_Buffer/Name_Len
1567 Output_Homonym_Numbers_Suffix;
1569 -- Add body-nested package suffix if required
1571 if BNPE_Suffix_Needed
1572 and then Ekind (Ent) /= E_Enumeration_Literal
1573 then
1574 Set_BNPE_Suffix (Ent);
1576 -- Strip trailing n's and last trailing b as required. note that
1577 -- we know there is at least one b, or no suffix would be generated.
1579 while Name_Buffer (Name_Len) = 'n' loop
1580 Name_Len := Name_Len - 1;
1581 end loop;
1583 Name_Len := Name_Len - 1;
1584 end if;
1586 Set_Chars (Ent, Name_Enter);
1587 Set_Has_Qualified_Name (Ent);
1589 if Debug_Flag_BB then
1590 Write_Str ("*** ");
1591 Write_Name (Save_Chars);
1592 Write_Str (" qualified as ");
1593 Write_Name (Chars (Ent));
1594 Write_Eol;
1595 end if;
1596 end Qualify_Entity_Name;
1598 --------------------------
1599 -- Qualify_Entity_Names --
1600 --------------------------
1602 procedure Qualify_Entity_Names (N : Node_Id) is
1603 begin
1604 Name_Qualify_Units.Append (N);
1605 end Qualify_Entity_Names;
1607 -------------------
1608 -- Reset_Buffers --
1609 -------------------
1611 procedure Reset_Buffers is
1612 begin
1613 Name_Len := 0;
1614 Homonym_Len := 0;
1615 end Reset_Buffers;
1617 --------------------
1618 -- Strip_Suffixes --
1619 --------------------
1621 procedure Strip_Suffixes (BNPE_Suffix_Found : in out Boolean) is
1622 SL : Natural;
1624 pragma Warnings (Off, BNPE_Suffix_Found);
1625 -- Since this procedure only ever sets the flag
1627 begin
1628 -- Search for and strip BNPE suffix
1630 for J in reverse 2 .. Name_Len loop
1631 if Name_Buffer (J) = 'X' then
1632 Name_Len := J - 1;
1633 BNPE_Suffix_Found := True;
1634 exit;
1635 end if;
1637 exit when Name_Buffer (J) /= 'b' and then Name_Buffer (J) /= 'n';
1638 end loop;
1640 -- Search for and strip homonym numbers suffix
1642 for J in reverse 2 .. Name_Len - 2 loop
1643 if Name_Buffer (J) = '_'
1644 and then Name_Buffer (J + 1) = '_'
1645 then
1646 if Name_Buffer (J + 2) in '0' .. '9' then
1647 if Homonym_Len > 0 then
1648 Homonym_Len := Homonym_Len + 1;
1649 Homonym_Numbers (Homonym_Len) := '-';
1650 end if;
1652 SL := Name_Len - (J + 1);
1654 Homonym_Numbers (Homonym_Len + 1 .. Homonym_Len + SL) :=
1655 Name_Buffer (J + 2 .. Name_Len);
1656 Name_Len := J - 1;
1657 Homonym_Len := Homonym_Len + SL;
1658 end if;
1660 exit;
1661 end if;
1662 end loop;
1663 end Strip_Suffixes;
1665 end Exp_Dbug;