This patch adds some missing legality checks on uses of aspect Import. These
checks were performed properly in the presence of pragmas, but were incomplete
when using aspects.

Compiling q.ads must yield:

q.ads:2:28: imported entities cannot have explicit initialization (RM 8.1 (24))
q.ads:6:24: no initialization allowed for declaration of "J" at line 5
q.ads:6:24: imported entities cannot be initialized (RM B.1(24))
q.ads:9:24: no initialization allowed for declaration of "K" at line 8
q.ads:9:24: imported entities cannot be initialized (RM B.1(24))

---
package Q is
   R : constant Integer := 42 with Import, Volatile;
   Q1 : constant := R*R + R*R;

   J : constant Integer := -999 with Volatile;
   pragma Import (Ada, J);

   K : Integer := 1123;
   pragma Import (Ada, K);
end;

Tested on x86_64-pc-linux-gnu, committed on trunk

2014-08-01  Ed Schonberg  <schonb...@adacore.com>

        * sem_ch13.adb (Analyze_Aspect_Specifications, case Aspect_Import):
        Set Is_Imported flag at once, to simplify subsequent legality
        checks. Reject the aspect on an object whose declaration has an
        explicit initial value.
        * sem_prag.adb (Process_Import_Or_Interface): Use original node
        to check legality of an initial value for an imported entity.
        Set Is_Imported flag in case of error to prevent cascaded errors.
        Do not set the Is_Imported flag if the pragma comes from an
        aspect, because it is already done when analyzing the aspect.

Index: sem_prag.adb
===================================================================
--- sem_prag.adb        (revision 213440)
+++ sem_prag.adb        (working copy)
@@ -7838,8 +7838,14 @@
             --  the code generator making an implicit initialization explicit.
 
             elsif Present (Expression (Parent (Def_Id)))
-              and then Comes_From_Source (Expression (Parent (Def_Id)))
+              and then Comes_From_Source
+                         (Original_Node (Expression (Parent (Def_Id))))
             then
+
+               --  Set imported flag to prevent cascaded errors.
+
+               Set_Is_Imported (Def_Id);
+
                Error_Msg_Sloc := Sloc (Def_Id);
                Error_Pragma_Arg
                  ("no initialization allowed for declaration of& #",
@@ -7847,7 +7853,13 @@
                   Arg2);
 
             else
-               Set_Imported (Def_Id);
+               --  If the pragma comes from an aspect specification the
+               --  Is_Imported flag has already been set.
+
+               if not From_Aspect_Specification (N) then
+                  Set_Imported (Def_Id);
+               end if;
+
                Process_Interface_Name (Def_Id, Arg3, Arg4);
 
                --  Note that we do not set Is_Public here. That's because we
@@ -7922,8 +7934,13 @@
                   exit;
 
                else
-                  Set_Imported (Def_Id);
+                  --  If the pragma comes from an aspect specification the
+                  --  Is_Imported flag has already been set.
 
+                  if not From_Aspect_Specification (N) then
+                     Set_Imported (Def_Id);
+                  end if;
+
                   --  Reject an Import applied to an abstract subprogram
 
                   if Is_Subprogram (Def_Id)
Index: sem_ch13.adb
===================================================================
--- sem_ch13.adb        (revision 213446)
+++ sem_ch13.adb        (working copy)
@@ -2915,6 +2915,21 @@
                      --  that verifed that there was a matching convention
                      --  is now obsolete.
 
+                     if A_Id = Aspect_Import then
+                        Set_Is_Imported (E);
+
+                        --  An imported entity cannot have an explicit
+                        --  initialization.
+
+                        if Nkind (N) = N_Object_Declaration
+                          and then Present (Expression (N))
+                        then
+                           Error_Msg_N
+                             ("imported entities cannot be initialized "
+                              & "(RM B.1(24))", Expression (N));
+                        end if;
+                     end if;
+
                      goto Continue;
                   end if;
 
@@ -2930,7 +2945,7 @@
                     and then Nkind (Parent (N)) /= N_Compilation_Unit
                   then
                      Error_Msg_N
-                        ("incorrect context for library unit aspect&", Id);
+                       ("incorrect context for library unit aspect&", Id);
                      goto Continue;
                   end if;
 

Reply via email to