Build_Anonymous_Collection (Ptr_Typ);
end if;
- -- Access-to-controlled types should always have a collection
+ -- Named access-to-controlled types must have a collection, but
+ -- anonymous access-to-controlled types need not.
- pragma Assert (Present (Finalization_Collection (Ptr_Typ)));
+ if Present (Finalization_Collection (Ptr_Typ)) then
+ Actual :=
+ Make_Attribute_Reference (Loc,
+ Prefix =>
+ New_Occurrence_Of
+ (Finalization_Collection (Ptr_Typ), Loc),
+ Attribute_Name => Name_Unrestricted_Access);
- Actual :=
- Make_Attribute_Reference (Loc,
- Prefix =>
- New_Occurrence_Of (Finalization_Collection (Ptr_Typ), Loc),
- Attribute_Name => Name_Unrestricted_Access);
+ else pragma Assert (Ekind (Ptr_Typ) = E_Anonymous_Access_Type);
+ Actual := Make_Null (Loc);
+ end if;
-- Tagged types
return;
end if;
+ -- For the access result type of a function that is a library unit,
+ -- we cannot create a finalization collection attached to the unit as
+ -- this would cause premature finalization of objects created through
+ -- the access result type, which may be returned from the function.
+
+ if Is_Local_Anonymous_Access (Ptr_Typ)
+ and then Ekind (Unit_Id) = E_Function
+ and then Parent (Ptr_Typ) =
+ Result_Definition (Subprogram_Specification (Unit_Id))
+ then
+ return;
+ end if;
+
-- Determine whether the current semantic unit already has an anonymous
-- collection which services the designated type.