Constraint_Check_Needed : constant Boolean :=
(Has_Discriminants (Obj_Typ) or else Is_Array_Type (Obj_Typ))
and then Is_Tagged_Type (Obj_Typ)
+ and then Nkind (Original_Node (Obj_Decl)) /=
+ N_Object_Renaming_Declaration
and then Is_Constrained (Obj_Typ);
-- We are processing a call in the context of something like
-- "X : T := F (...);". This is True if we need to do a constraint
-- which is possible only in the callee-allocates case,
-- which is why we have Is_Tagged_Type above.
-- ???The check is missing in the untagged caller-allocates case.
+ -- ???The check for renaming declarations above is needed because
+ -- Sem_Ch8.Analyze_Object_Renaming sometimes changes a renaming
+ -- into an object declaration. We probably shouldn't do that,
+ -- but for now, we need this check.
-- Start of processing for Make_Build_In_Place_Call_In_Object_Declaration
Resolve (Nam, T);
+ -- If the renamed object is a function call of a limited type,
+ -- the expansion of the renaming is complicated by the presence
+ -- of various temporaries and subtypes that capture constraints
+ -- of the renamed object. Rewrite node as an object declaration,
+ -- whose expansion is simpler. Given that the object is limited
+ -- there is no copy involved and no performance hit.
+
+ if Nkind (Nam) = N_Function_Call
+ and then Is_Inherently_Limited_Type (Etype (Nam))
+ and then not Is_Constrained (Etype (Nam))
+ and then Comes_From_Source (N)
+ then
+ Set_Etype (Id, T);
+ Mutate_Ekind (Id, E_Constant);
+ Rewrite (N,
+ Make_Object_Declaration (Loc,
+ Defining_Identifier => Id,
+ Constant_Present => True,
+ Object_Definition => New_Occurrence_Of (Etype (Nam), Loc),
+ Expression => Relocate_Node (Nam)));
+ return;
+ end if;
+
-- Ada 2012 (AI05-149): Reject renaming of an anonymous access object
-- when renaming declaration has a named access type. The Ada 2012
-- coverage rules allow an anonymous access type in the context of