Aspect Relaxed_Initialization has been prototyped for ordinary
subprograms and then SPARK RM 6.10 allowed it for generic subprograms as
well.
This is mostly straightforward to implement, except when 'Result appears
in the aspect expression for a generic function. When instantiated in a
wrapper package, the aspect gets attached to a subprogram with internal
name, while 'Result in the aspect is still prefixed with the name of a
generic function.
We already had a mechanism to correct this discrepancy, as it also
happens with Post, Depends and Refined_Depends aspects. However,
expressions of those aspects are first relocated to pragmas and then
analysed with renaming of the instantiated subprogram in scope. This
patch reuses this relaxes a guard for this mechanism, so that correction
applies to aspect Relaxed_Initialization, which is not translated to
pragma.
Tested on x86_64-pc-linux-gnu, committed on trunk
gcc/ada/
* sem_attr.adb (Analyze_Attribute): Correct prefix of 'Result
this prefix is a generic function but the enclosing aspect or
pragma is attached to its instance.
* sem_ch12.adb (Analyze_Generic_Subprogram_Declaration): Analyze
generic subprogram formal parameters (including the implicit
result of a generic function) and only then analyse its aspects,
because with Relaxed_Initialization the aspect expression might
refer to those formal parameters.
* sem_ch13.adb (Analyze_Aspect_Relaxed_Initialization): Accept
aspect on generic subprograms; install formal parameters of a
generic subprogram but not formal parameters of the generic unit
itself (the previous code was inspired by aspects Post and
Depends, where both kinds of formals are allowed).
* sem_util.ads (Enter_Name): Fix name of a subprogram referenced
in comment.
diff --git a/gcc/ada/sem_attr.adb b/gcc/ada/sem_attr.adb
--- a/gcc/ada/sem_attr.adb
+++ b/gcc/ada/sem_attr.adb
@@ -5512,8 +5512,16 @@ package body Sem_Attr is
if Is_Entity_Name (P) then
Pref_Id := Entity (P);
- if Ekind_In (Pref_Id, E_Function, E_Generic_Function)
- and then Ekind (Spec_Id) = Ekind (Pref_Id)
+ -- Either both the prefix and the annotated spec must be
+ -- generic functions, or they both must be non-generic
+ -- functions, or the prefix must be generic and the spec
+ -- must be non-generic (i.e. it must denote an instance).
+
+ if (Ekind_In (Pref_Id, E_Function, E_Generic_Function)
+ and then Ekind (Pref_Id) = Ekind (Spec_Id))
+ or else
+ (Ekind (Pref_Id) = E_Generic_Function
+ and then Ekind (Spec_Id) = E_Function)
then
if Denote_Same_Function (Pref_Id, Spec_Id) then
diff --git a/gcc/ada/sem_ch12.adb b/gcc/ada/sem_ch12.adb
--- a/gcc/ada/sem_ch12.adb
+++ b/gcc/ada/sem_ch12.adb
@@ -3873,13 +3873,6 @@ package body Sem_Ch12 is
Set_Ekind (Id, E_Generic_Procedure);
end if;
- -- Analyze the aspects of the generic copy to ensure that all generated
- -- pragmas (if any) perform their semantic effects.
-
- if Has_Aspects (N) then
- Analyze_Aspect_Specifications (N, Id);
- end if;
-
-- Set SPARK_Mode from context
Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
@@ -3951,6 +3944,13 @@ package body Sem_Ch12 is
Set_Etype (Id, Standard_Void_Type);
end if;
+ -- Analyze the aspects of the generic copy to ensure that all generated
+ -- pragmas (if any) perform their semantic effects.
+
+ if Has_Aspects (N) then
+ Analyze_Aspect_Specifications (N, Id);
+ end if;
+
-- For a library unit, we have reconstructed the entity for the unit,
-- and must reset it in the library tables. We also make sure that
-- Body_Required is set properly in the original compilation unit node.
diff --git a/gcc/ada/sem_ch13.adb b/gcc/ada/sem_ch13.adb
--- a/gcc/ada/sem_ch13.adb
+++ b/gcc/ada/sem_ch13.adb
@@ -2276,7 +2276,9 @@ package body Sem_Ch13 is
-- Annotation of a subprogram; aspect expression is required
- elsif Is_Subprogram_Or_Entry (E) then
+ elsif Is_Subprogram_Or_Entry (E)
+ or else Is_Generic_Subprogram (E)
+ then
if Present (Expr) then
-- If we analyze subprogram body that acts as its own
@@ -2291,11 +2293,13 @@ package body Sem_Ch13 is
Restore_Scope := True;
Push_Scope (E);
- if Is_Generic_Subprogram (E) then
- Install_Generic_Formals (E);
- else
- Install_Formals (E);
- end if;
+ -- Only formals of the subprogram itself can appear
+ -- in Relaxed_Initialization aspect expression, not
+ -- formals of the enclosing generic unit. (This is
+ -- different that in Precondition or Depends aspects,
+ -- where both kinds of formals are allowed.)
+
+ Install_Formals (E);
end if;
-- Aspect expression is either an aggregate with list of
diff --git a/gcc/ada/sem_util.ads b/gcc/ada/sem_util.ads
--- a/gcc/ada/sem_util.ads
+++ b/gcc/ada/sem_util.ads
@@ -741,7 +741,7 @@ package Sem_Util is
-- Insert new name in symbol table of current scope with check for
-- duplications (error message is issued if a conflict is found).
-- Note: Enter_Name is not used for overloadable entities, instead these
- -- are entered using Sem_Ch6.Enter_Overloadable_Entity.
+ -- are entered using Sem_Ch6.Enter_Overloaded_Entity.
function Entity_Of (N : Node_Id) return Entity_Id;
-- Obtain the entity of arbitrary node N. If N is a renaming, return the