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] Expression functions as fully private protected operations


An expression function that is not a completion can appear in a protected body
and requires the same renaming declarations as other protected operations.

The following must compile quietly:

---
with Private_Types;
procedure Main is

   package Integer_Buffer is new
     Private_Types (Element => Integer, Size => 10);

   Buffer : Integer_Buffer.Buffer;

begin
   Buffer.Put (0);
end Main;
---
generic
   type Element is private;
   Size: Positive;
package Private_Types is

   subtype Storage_Range is Positive range 1 .. Size;
   type Storage is array (Storage_Range) of Element;

   protected type Buffer is

      entry Put (Item : in Element);
      entry Get (Item : out Element);

   private

      Item_Buffer : Storage := (others => <>);
      Put_Item_Index, Get_Item_Index : Storage_Range := Storage_Range'First;
      Item_Count : Natural range 0 .. Size := 0;
   end Buffer;
end Private_Types;
---
package body Private_Types is

   protected body Buffer is
      function Is_Full return Boolean is (Item_Count = Size);

      function Is_Empty return Boolean is (Item_Count = 0);

      entry Put (Item : in Element) when not Is_Full is
      begin
         Item_Buffer (Put_Item_Index) := Item;
         Put_Item_Index :=
           (if Put_Item_Index = Storage_Range'Last then
              Storage_Range'First
            else
              Storage_Range'Succ (Put_Item_Index));
         Item_Count := Item_Count + 1;
      end Put;

      entry Get (Item : out Element) when not Is_Empty is
      begin
         Item := Item_Buffer (Get_Item_Index);
         Get_Item_Index :=
           (if Get_Item_Index = Storage_Range'Last then
              Storage_Range'First
            else
              Storage_Range'Succ (Get_Item_Index));
         Item_Count := Item_Count - 1;
      end Get;
   end Buffer;
end Private_Types;

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

2014-01-22  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch6.adb (Analyze_Subprogram_Body_Helper): A subprogram
	body generated for an expression function within a protected body
	needs a set of renaming declarations if the expression function
	comes from source.

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]