diff options
author | Eric Botcazou <ebotcazou@adacore.com> | 2022-06-03 10:27:33 +0200 |
---|---|---|
committer | Pierre-Marie de Rodat <derodat@adacore.com> | 2022-07-05 08:28:18 +0000 |
commit | 4844a259b41b4f31940b478216d6dc9faa2bcbca (patch) | |
tree | aeea40a56d2cf8ef46bab4833a8808775b3e269e /gcc/ada/exp_ch6.ads | |
parent | 9fde6c7114f079b38ebee420a541e14387bcc928 (diff) | |
download | gcc-4844a259b41b4f31940b478216d6dc9faa2bcbca.zip gcc-4844a259b41b4f31940b478216d6dc9faa2bcbca.tar.gz gcc-4844a259b41b4f31940b478216d6dc9faa2bcbca.tar.bz2 |
[Ada] Fix dangling bounds for array result of BIP functions
The implementation of the build-in-place return protocol for functions
whose result type is an unconstrained array type generates dangling
references to local bounds built on the stack for the result as soon as
these bounds are not static. The reason is that the implementation
treats the return object, either explicitly present in the source or
synthesized by the compiler, as a regular constrained object until very
late in the game, although it needs to be ultimately rewritten as the
renaming of the dereference of an allocator with unconstrained designated
type in order for the bounds to be part of the allocation.
Recently a partial fix was implemented for the case where the result is an
aggregate, by preventing the return object from being expanded after it has
been analyzed. However, it does not work for the general case of extended
return statements, because the statements therein are still analyzed with
the constrained version of the return object so, after it is changed into
the unconstrained renaming, this yields (sub)type mismatches.
Therefore this change goes the other way around: it rolls back the partial
fix and instead performs the transformation of the return object into the
unconstrained renaming during the expansion of its declaration, in other
words before statements referencing it, if any, are analyzed, thus ensuring
that they see the final version of the object.
gcc/ada/
* exp_aggr.adb (Expand_Array_Aggregate): Remove obsolete code.
Delay the expansion of aggregates initializing return objects of
build-in-place functions.
* exp_ch3.ads (Ensure_Activation_Chain_And_Master): Delete.
* exp_ch3.adb (Ensure_Activation_Chain_And_Master): Fold back to...
(Expand_N_Object_Declaration): ...here.
Perform the expansion of return objects of build-in-place functions
here instead of...
* exp_ch6.ads (Is_Build_In_Place_Return_Object): Declare.
* exp_ch6.adb (Expand_N_Extended_Return_Statement): ...here.
(Is_Build_In_Place_Result_Type): Alphabetize.
(Is_Build_In_Place_Return_Object): New predicate.
* exp_ch7.adb (Enclosing_Function): Delete.
(Process_Object_Declaration): Tidy up handling of return objects.
* sem_ch3.adb (Analyze_Object_Declaration): Do not decorate and
freeze the actual type if it is the same as the nominal type.
* sem_ch6.adb: Remove use and with clauses for Exp_Ch3.
(Analyze_Function_Return): Analyze again all return objects.
(Create_Extra_Formals): Do not force the definition of an Itype
if the subprogram is a compilation unit.
Diffstat (limited to 'gcc/ada/exp_ch6.ads')
-rw-r--r-- | gcc/ada/exp_ch6.ads | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/gcc/ada/exp_ch6.ads b/gcc/ada/exp_ch6.ads index f886eda..19d0bc3 100644 --- a/gcc/ada/exp_ch6.ads +++ b/gcc/ada/exp_ch6.ads @@ -127,22 +127,6 @@ package Exp_Ch6 is function Is_Build_In_Place_Entity (E : Entity_Id) return Boolean; -- Ada 2005 (AI-318-02): Returns True if E is a BIP entity. - function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean; - -- Ada 2005 (AI-318-02): Returns True if functions returning the type use - -- build-in-place protocols. For inherently limited types, this must be - -- True in >= Ada 2005, and must be False in Ada 95. For other types, it - -- can be True or False, and the decision should be based on efficiency, - -- and should be the same for all language versions, so that mixed-dialect - -- programs will work. - -- - -- For inherently limited types in Ada 2005, True means that calls will - -- actually be build-in-place in all cases. For other types, build-in-place - -- will be used when possible, but we need to make a copy in some - -- cases. For example, for "X := F(...);" if F can see X, or if F can - -- propagate exceptions, we need to store its result in a temp in general, - -- and copy the temp into X. Also, for "return Global_Var;" Global_Var - -- needs to be copied into the function result object. - function Is_Build_In_Place_Function (E : Entity_Id) return Boolean; -- Ada 2005 (AI-318-02): Returns True if E denotes a function, generic -- function, or access-to-function type for which @@ -155,6 +139,15 @@ package Exp_Ch6 is -- that requires handling as a build-in-place call (possibly qualified or -- converted). + function Is_Build_In_Place_Result_Type (Typ : Entity_Id) return Boolean; + -- Ada 2005 (AI-318-02): Returns True if functions returning the type use + -- build-in-place protocols. For inherently limited types, this must be + -- True in >= Ada 2005 and must be False in Ada 95. + + function Is_Build_In_Place_Return_Object (E : Entity_Id) return Boolean; + -- Ada 2005 (AI-318-02): Return True is E is a return object of a function + -- that uses build-in-place protocols. + function Is_Null_Procedure (Subp : Entity_Id) return Boolean; -- Predicate to recognize stubbed procedures and null procedures, which -- can be inlined unconditionally in all cases. @@ -272,4 +265,7 @@ package Exp_Ch6 is -- to reference the secondary dispatch table of an interface; otherwise -- return Empty. +private + pragma Inline (Is_Build_In_Place_Return_Object); + end Exp_Ch6; |