This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Spurious error on convention of anonymous access-to-subprogram type


This patch ensures that Ada RM 6.3.1 13.1/3 which states

   The calling convention for an anonymous access-to-subprogram parameter or
   anonymous access-to-subprogram result is protected if the reserved word
   protected appears in its definition; otherwise, it is the convention of
   the subprogram that contains the parameter.

properly sets the convention of an anonymous access-to-subprogram type to that
of the related subprogram.

------------
-- Source --
------------

--  conventions.ads

package Conventions is

   -----------------
   -- Inherit Ada --
   -----------------

   procedure Ada_1 (Ptr : access procedure);
   function  Ada_2 return access procedure;
   function  Ada_3 return access procedure;
   function  Ada_4 return access procedure;

   ---------------
   -- Inherit C --
   ---------------

   procedure C_1 (Ptr : access procedure) with Convention => C;
   function  C_2 return access procedure  with Convention => C;
   function  C_3 return access procedure  with Convention => C;
   function  C_4 return access procedure  with Convention => C;

   -----------------------
   -- Inherit Protected --
   -----------------------

   protected IP is
      procedure Prot_1 (Ptr : access procedure);
      function  Prot_2 return access procedure;
      function  Prot_3 return access procedure;
      function  Prot_4 return access procedure;
   end IP;

   ------------------------
   -- Protected with Ada --
   ------------------------

   procedure Prot_Ada_1 (Ptr : access protected procedure);          --  OK
   function  Prot_Ada_2 return access protected procedure;           --  OK

   ----------------------
   -- Protected with C --
   ----------------------

   procedure Prot_C_1 (Ptr : access protected procedure)             --  OK
     with Convention => C;
   function  Prot_C_2 return access protected procedure              --  OK
     with Convention => C;

   ------------------------------
   -- Protected with Protected --
   ------------------------------

   protected PP is
      procedure Prot_1 (Ptr : access protected procedure);           --  OK
      function  Prot_2 return access protected procedure;            --  OK
      function  Prot_3 return access protected procedure;            --  OK
      function  Prot_4 return access protected procedure;            --  OK
   end PP;

   ---------------
   -- Renamings --
   ---------------

   procedure Ren_Ada_1 (Ptr : access procedure) renames Ada_1;
   function  Ren_Ada_2 return access procedure  renames Ada_2;

   procedure Ren_C_1 (Ptr : access procedure) renames C_1;
   function  Ren_C_2 return access procedure  renames C_2;

   procedure Ren_Prot_1 (Ptr : access procedure) renames IP.Prot_1;
   function  Ren_Prot_2 return access procedure  renames IP.Prot_2;

   --------------
   -- Nestings --
   --------------

   procedure Nest_Ada_1
     (Ptr_1 : access procedure (Ptr_2 : access procedure));
   function  Nest_Ada_2
     return access procedure (Ptr : access procedure);
   function  Nest_Ada_3
     return access procedure (Ptr : access procedure);
   function  Nest_Ada_4
     return access procedure (Ptr : access procedure);

   procedure Nest_C_1
     (Ptr_1 : access procedure (Ptr_2 : access procedure))
     with Convention => C;
   function Nest_C_2
     return access procedure (Ptr : access procedure)
     with Convention => C;
   function Nest_C_3
     return access procedure (Ptr : access procedure)
     with Convention => C;
   function Nest_C_4
     return access procedure (Ptr : access procedure)
     with Convention => C;

   protected NP is
      procedure Prot_1 (Ptr_1 : access procedure (Ptr_2 : access procedure));
      function  Prot_2  return  access procedure (Ptr   : access procedure);
      function  Prot_3  return  access procedure (Ptr   : access procedure);
      function  Prot_4  return  access procedure (Ptr   : access procedure);
   end NP;

   procedure Calls;
end Conventions;

--  conventions.adb

package body Conventions is

   --  Specs

   procedure Ada_Proc;
   procedure Ada_Proc_Access (Ptr : access procedure);

   procedure C_Proc with Convention => C;
   procedure C_Proc_Access (Ptr : access procedure) with Convention => C;

   protected P is
      procedure Proc;
   end P;

   protected P_Access is
      procedure Proc (Ptr : access procedure);
   end P_Access;

   --  Bodies

   procedure Ada_Proc is
   begin null; end Ada_Proc;

   procedure Ada_Proc_Access (Ptr : access procedure) is
   begin null; end Ada_Proc_Access;

   procedure C_Proc is
   begin null; end C_Proc;

   procedure C_Proc_Access (Ptr : access procedure) is
   begin null; end C_Proc_Access;

   protected body P is
      procedure Proc is begin null; end Proc;
   end P;

   protected body P_Access is
      procedure Proc (Ptr : access procedure) is
      begin null; end Proc;
   end P_Access;

   -----------------
   -- Inherit Ada --
   -----------------

   procedure Ada_1 (Ptr : access procedure) is
   begin null; end Ada_1;

   function Ada_2 return access procedure is
   begin
      return Ada_Proc'Access;                                        --  OK
   end Ada_2;

   function Ada_3 return access procedure is
   begin
      return C_Proc'Access;                                          --  Error
   end Ada_3;

   function Ada_4 return access procedure is
   begin
      return P.Proc'Access;                                          --  Error
   end Ada_4;

   ---------------
   -- Inherit C --
   ---------------

   procedure C_1 (Ptr : access procedure) is
   begin null; end C_1;

   function C_2 return access procedure is
   begin
      return Ada_Proc'Access;                                        --  Error
   end C_2;

   function C_3 return access procedure is
   begin
      return C_Proc'Access;                                          --  OK
   end C_3;

   function C_4 return access procedure is
   begin
      return P.Proc'Access;                                          --  Error
   end C_4;

   ----------------------
   -- Iherit Protected --
   ----------------------

   protected body IP is
      procedure Prot_1 (Ptr : access procedure) is
      begin null; end Prot_1;

      function Prot_2 return access procedure is
      begin
         return Ada_Proc'Access;                                     --  OK
      end Prot_2;

      function Prot_3 return access procedure is
      begin
         return C_Proc'Access;                                       --  Error
      end Prot_3;

      function Prot_4 return access procedure is
      begin
         return P.Proc'Access;                                       --  Error
      end Prot_4;
   end IP;

   ------------------------
   -- Protected with Ada --
   ------------------------

   procedure Prot_Ada_1 (Ptr : access protected procedure) is
   begin null; end Prot_Ada_1;

   function Prot_Ada_2 return access protected procedure is
   begin return null; end Prot_Ada_2;

   ----------------------
   -- Protected with C --
   ----------------------

   procedure Prot_C_1 (Ptr : access protected procedure) is
   begin null; end Prot_C_1;

   function Prot_C_2 return access protected procedure is
   begin return null; end Prot_C_2;

   ------------------------------
   -- Protected with Protected --
   ------------------------------

   protected body PP is
      procedure Prot_1 (Ptr : access protected procedure) is
      begin null; end Prot_1;

      function Prot_2 return access protected procedure is
      begin return null; end Prot_2;

      function Prot_3 return access protected procedure is
      begin return null; end Prot_3;

      function Prot_4 return access protected procedure is
      begin return null; end Prot_4;
   end PP;

   --------------
   -- Nestings --
   --------------

   procedure Nest_Ada_1
     (Ptr_1 : access procedure (Ptr_2 : access procedure)) is
   begin null; end Nest_Ada_1;

   function Nest_Ada_2
     return access procedure (Ptr : access procedure) is
   begin
      return Ada_Proc_Access'Access;                                 --  OK
   end Nest_Ada_2;

   function Nest_Ada_3
     return access procedure (Ptr : access procedure) is
   begin
      return C_Proc_Access'Access;                                   --  Error
   end Nest_Ada_3;

   function Nest_Ada_4
     return access procedure (Ptr : access procedure) is
   begin
      return P_Access.Proc'Access;                                   --  Error
   end Nest_Ada_4;

   procedure Nest_C_1
     (Ptr_1 : access procedure (Ptr_2 : access procedure)) is
   begin null; end Nest_C_1;

   function Nest_C_2
     return access procedure (Ptr : access procedure) is
   begin
      return Ada_Proc_Access'Access;                                 --  Error
   end Nest_C_2;

   function Nest_C_3
     return access procedure (Ptr : access procedure) is
   begin
      return C_Proc_Access'Access;                                   --  OK
   end Nest_C_3;

   function Nest_C_4
     return access procedure (Ptr : access procedure) is
   begin
      return P_Access.Proc'Access;                                   --  Error
   end Nest_C_4;

   protected body NP is
      procedure Prot_1 (Ptr_1 : access procedure (Ptr_2 : access procedure)) is
      begin null; end Prot_1;

      function Prot_2 return access procedure (Ptr : access procedure) is
      begin
         return Ada_Proc_Access'Access;                              --  OK
      end Prot_2;

      function Prot_3 return access procedure (Ptr : access procedure) is
      begin
         return C_Proc_Access'Access;                                --  Error
      end Prot_3;

      function Prot_4 return access procedure (Ptr : access procedure) is
      begin
         return P_Access.Proc'Access;                                --  Error
      end Prot_4;
   end NP;

   -----------
   -- Calls --
   -----------

   procedure Calls is
   begin
      Ada_1 (Ada_Proc'Access);                                       --  OK
      Ada_1 (C_Proc'Access);                                         --  Error
      Ada_1 (P.Proc'Access);                                         --  Error

      C_1 (Ada_Proc'Access);                                         --  Error
      C_1 (C_Proc'Access);                                           --  OK
      C_1 (P.Proc'Access);                                           --  Error

      IP.Prot_1 (Ada_Proc'Access);                                   --  OK
      IP.Prot_1 (C_Proc'Access);                                     --  Error
      IP.Prot_1 (P.Proc'Access);                                     --  Error

      Ren_Ada_1 (Ada_Proc'Access);                                   --  OK
      Ren_Ada_1 (C_Proc'Access);                                     --  Error
      Ren_Ada_1 (P.Proc'Access);                                     --  Error

      Ren_C_1 (Ada_Proc'Access);                                     --  Error
      Ren_C_1 (C_Proc'Access);                                       --  OK
      Ren_C_1 (P.Proc'Access);                                       --  Error

      Ren_Prot_1 (Ada_Proc'Access);                                  --  OK
      Ren_Prot_1 (C_Proc'Access);                                    --  Error
      Ren_Prot_1 (P.Proc'Access);                                    --  Error

      Nest_Ada_1 (Ada_Proc_Access'Access);                           --  OK
      Nest_Ada_1 (C_Proc_Access'Access);                             --  Error
      Nest_Ada_1 (P_Access.Proc'Access);                             --  Error

      Nest_C_1 (Ada_Proc_Access'Access);                             --  Error
      Nest_C_1 (C_Proc_Access'Access);                               --  OK
      Nest_C_1 (P_Access.Proc'Access);                               --  Error

      NP.Prot_1 (Ada_Proc_Access'Access);                            --  OK
      NP.Prot_1 (C_Proc_Access'Access);                              --  Error
      NP.Prot_1 (P_Access.Proc'Access);                              --  Error
   end Calls;
end Conventions;

----------------------------
-- Compilation and output --
----------------------------

$ gcc -c conventions.adb
conventions.adb:56:14: subprogram "C_Proc" has wrong convention
conventions.adb:56:14: does not match access to subprogram declared at
  conventions.ads:9
conventions.adb:61:14: context requires a non-protected subprogram
conventions.adb:73:14: subprogram "Ada_Proc" has wrong convention
conventions.adb:73:14: does not match access to subprogram declared at
  conventions.ads:17
conventions.adb:83:14: context requires a non-protected subprogram
conventions.adb:101:17: subprogram "C_Proc" has wrong convention
conventions.adb:101:17: does not match access to subprogram declared at
  conventions.ads:28
conventions.adb:106:17: context requires a non-protected subprogram
conventions.adb:165:14: subprogram "C_Proc_Access" has wrong convention
conventions.adb:165:14: does not match access to subprogram declared at
  conventions.ads:80
conventions.adb:171:14: context requires a non-protected subprogram
conventions.adb:181:14: subprogram "Ada_Proc_Access" has wrong convention
conventions.adb:181:14: does not match access to subprogram declared at
  conventions.ads:88
conventions.adb:193:14: context requires a non-protected subprogram
conventions.adb:207:17: subprogram "C_Proc_Access" has wrong convention
conventions.adb:207:17: does not match access to subprogram declared at
  conventions.ads:101
conventions.adb:212:17: context requires a non-protected subprogram
conventions.adb:223:14: subprogram "C_Proc" has wrong convention
conventions.adb:223:14: does not match access to subprogram declared at
  conventions.ads:7
conventions.adb:224:14: context requires a non-protected subprogram
conventions.adb:226:12: subprogram "Ada_Proc" has wrong convention
conventions.adb:226:12: does not match access to subprogram declared at
  conventions.ads:16
conventions.adb:228:12: context requires a non-protected subprogram
conventions.adb:231:18: subprogram "C_Proc" has wrong convention
conventions.adb:231:18: does not match access to subprogram declared at
  conventions.ads:26
conventions.adb:232:18: context requires a non-protected subprogram
conventions.adb:235:18: subprogram "C_Proc" has wrong convention
conventions.adb:235:18: does not match access to subprogram declared at
  conventions.ads:7
conventions.adb:236:18: context requires a non-protected subprogram
conventions.adb:238:16: subprogram "Ada_Proc" has wrong convention
conventions.adb:238:16: does not match access to subprogram declared at
  conventions.ads:16
conventions.adb:240:16: context requires a non-protected subprogram
conventions.adb:243:19: subprogram "C_Proc" has wrong convention
conventions.adb:243:19: does not match access to subprogram declared at
  conventions.ads:26
conventions.adb:244:19: context requires a non-protected subprogram
conventions.adb:247:19: subprogram "C_Proc_Access" has wrong convention
conventions.adb:247:19: does not match access to subprogram declared at
  conventions.ads:76
conventions.adb:248:19: context requires a non-protected subprogram
conventions.adb:250:17: subprogram "Ada_Proc_Access" has wrong convention
conventions.adb:250:17: does not match access to subprogram declared at
  conventions.ads:85
conventions.adb:252:17: context requires a non-protected subprogram
conventions.adb:255:18: subprogram "C_Proc_Access" has wrong convention
conventions.adb:255:18: does not match access to subprogram declared at
  conventions.ads:99
conventions.adb:256:18: context requires a non-protected subprogram

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

2017-01-23  Hristian Kirtchev  <kirtchev@adacore.com>

	* freeze.adb (Freeze_Subprogram): Ensure that all anonymous
	access-to-subprogram types inherit the convention of the
	associated subprogram.	(Set_Profile_Convention): New routine.
	* sem_ch6.adb (Check_Conformance): Do not compare the conventions
	of the two entities directly, use Conventions_Match to account
	for anonymous access-to-subprogram and subprogram types.
	(Conventions_Match): New routine.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]