with Atree; use Atree;
with Einfo; use Einfo;
-with Einfo.Entities; use Einfo.Entities;
with Einfo.Utils; use Einfo.Utils;
with Nlists; use Nlists;
with Sinfo; use Sinfo;
return E;
end Ultimate_Alias;
+ ---------------------------
+ -- Unique_Component_Name --
+ ---------------------------
+
+ function Unique_Component_Name
+ (Component : Record_Field_Kind_Id) return Name_Id
+ is
+ Homographic_Component_Count : Pos := 1;
+ Hcc : Pos renames Homographic_Component_Count;
+ Enclosing_Type : Entity_Id :=
+ Underlying_Type (Base_Type (Scope (Component)));
+ begin
+ if Ekind (Enclosing_Type) = E_Record_Type
+ and then Is_Tagged_Type (Enclosing_Type)
+ and then Has_Private_Ancestor (Enclosing_Type)
+ then
+ -- traverse ancestors to determine Hcc value
+ loop
+ declare
+ Type_Decl : constant Node_Id :=
+ Parent (Underlying_Type (Base_Type (Enclosing_Type)));
+ Type_Def : constant Node_Id := Type_Definition (Type_Decl);
+ begin
+ exit when Nkind (Type_Def) /= N_Derived_Type_Definition;
+ Enclosing_Type :=
+ Underlying_Type (Base_Type (Etype (Enclosing_Type)));
+
+ declare
+ Ancestor_Comp : Opt_Record_Field_Kind_Id :=
+ First_Component_Or_Discriminant (Enclosing_Type);
+ begin
+ while Present (Ancestor_Comp) loop
+ if Chars (Ancestor_Comp) = Chars (Component) then
+ Hcc := Hcc + 1;
+ exit; -- exit not required, but might as well
+ end if;
+ Next_Component_Or_Discriminant (Ancestor_Comp);
+ end loop;
+ end;
+ end;
+ end loop;
+ end if;
+
+ if Hcc = 1 then
+ -- the usual case
+ return Chars (Component);
+ else
+ declare
+ Buff : Bounded_String;
+ begin
+ Append (Buff, Chars (Component));
+
+ Append (Buff, "__");
+ -- A double underscore in an identifier is legal in C, not in Ada.
+ -- Returning a result that is not a legal Ada identifier
+ -- ensures that we won't have problems with collisions.
+ -- If we have a component named Foo and we just append a
+ -- number (without any underscores), that new name might match
+ -- the name of another component (which would be bad).
+ -- The result of this function is intended for use as an
+ -- identifier in generated C code, so it needs to be a
+ -- legal C identifer.
+
+ Append (Buff, Hcc);
+ -- Should we instead append Hcc - 1 here? This is a human
+ -- readability question. If parent type and extension each
+ -- have a Foo component, do we want the name returned for the
+ -- second Foo to be "foo__2" or "foo__1" ? Does it matter?
+ -- Either way, the name returned for the first Foo will be "foo".
+
+ return Name_Find (Buff);
+ end;
+ end if;
+ end Unique_Component_Name;
+
--------------------------
-- Unit_Declaration_Node --
--------------------------
-- require more than minimal semantic knowledge.
with Alloc;
+with Einfo.Entities; use Einfo.Entities;
with Namet; use Namet;
with Table;
with Types; use Types;
-- Return the last entity in the chain of aliased entities of Prim. If Prim
-- has no alias return Prim.
+ function Unique_Component_Name
+ (Component : Record_Field_Kind_Id) return Name_Id;
+ -- Usually, a record type cannot have two components with the same name.
+ -- But in the case of a component declared in an extension of a tagged
+ -- private (or private extension) parent type, it is possible that some
+ -- ancestor type also has a (non-visible) component with the same name.
+ -- In the common case, this function simply returns the Chars attribute
+ -- of its argument.
+ -- But in the multiple-components-with-the-same-name case, it appends
+ -- a uniquifying suffix. The result in this case will not be a
+ -- syntactically valid Ada identifier, but it will be a syntactically
+ -- valid C identifier.
+
function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id;
-- Unit_Id is the simple name of a program unit, this function returns the
-- corresponding xxx_Declaration node for the entity. Also applies to the