public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Legality checks involving aspect Import
@ 2014-08-01 10:16 Arnaud Charlet
  2014-08-01 16:11 ` Mike Stump
  0 siblings, 1 reply; 2+ messages in thread
From: Arnaud Charlet @ 2014-08-01 10:16 UTC (permalink / raw)
  To: gcc-patches; +Cc: Ed Schonberg

[-- Attachment #1: Type: text/plain, Size: 1437 bytes --]

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  <schonberg@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.


[-- Attachment #2: difs --]
[-- Type: text/plain, Size: 3158 bytes --]

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;
 

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Ada] Legality checks involving aspect Import
  2014-08-01 10:16 [Ada] Legality checks involving aspect Import Arnaud Charlet
@ 2014-08-01 16:11 ` Mike Stump
  0 siblings, 0 replies; 2+ messages in thread
From: Mike Stump @ 2014-08-01 16:11 UTC (permalink / raw)
  To: Arnaud Charlet; +Cc: gcc-patches, Ed Schonberg

On Aug 1, 2014, at 3:16 AM, Arnaud Charlet <charlet@adacore.com> wrote:
> This patch adds some missing legality checks

No, GNU English doesn’t have us using legal/illegal unless the motivation is a law of some specific jurisdiction.

> 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))

There are ok, but…

> 	* sem_ch13.adb (Analyze_Aspect_Specifications, case Aspect_Import):
> 	Set Is_Imported flag at once, to simplify subsequent legality

Not this.

> 	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.

Nor this.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-08-01 16:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-01 10:16 [Ada] Legality checks involving aspect Import Arnaud Charlet
2014-08-01 16:11 ` Mike Stump

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).