comp.lang.ada
 help / color / mirror / Atom feed
* In mode parameter in an assignment statement
@ 2018-10-13  0:48 Anh Vo
  2018-10-13  5:52 ` Jacob Sparre Andersen
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Anh Vo @ 2018-10-13  0:48 UTC (permalink / raw)


   -- ...

   ------------------
   -- Compute_Data --
   ------------------

   procedure Compute_Data (Region : Mapped_Region) is
      Data_Shift : constant Storage_Offset :=
         Storage_Offset (Region.User_Offset - Region.System_Offset);
   begin
      if Region.User_Size = 0 then
         Region.Data := Empty_String'Address;
      elsif Region.Mapped then
         Region.Data := Region.Mapping.Address + Data_Shift;
      else
         Region.Data := Region.Buffer.all'Address + Data_Shift;
      end if;
   end Compute_Data;

end GNATCOLL.Mmap;

The above code is the tail end of package gnatcoll-mmap.adb.

I thought it should be rejected during compilation because in mode parameter is not allowed in an assignment statement. But, it was compiled successfully under GNAT obviously.

What did I miss? Thanks in advance.

Anh Vo


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

* Re: In mode parameter in an assignment statement
  2018-10-13  0:48 In mode parameter in an assignment statement Anh Vo
@ 2018-10-13  5:52 ` Jacob Sparre Andersen
  2018-10-13  6:03 ` Per Sandberg
  2018-10-13  8:38 ` Jeffrey R. Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Jacob Sparre Andersen @ 2018-10-13  5:52 UTC (permalink / raw)


Anh Vo <anhvofrcaus@gmail.com> writes:

>    -- ...
>
>    ------------------
>    -- Compute_Data --
>    ------------------
>
>    procedure Compute_Data (Region : Mapped_Region) is
>       Data_Shift : constant Storage_Offset :=
>          Storage_Offset (Region.User_Offset - Region.System_Offset);
>    begin
>       if Region.User_Size = 0 then
>          Region.Data := Empty_String'Address;
>       elsif Region.Mapped then
>          Region.Data := Region.Mapping.Address + Data_Shift;
>       else
>          Region.Data := Region.Buffer.all'Address + Data_Shift;
>       end if;
>    end Compute_Data;
>
> end GNATCOLL.Mmap;
>
> The above code is the tail end of package gnatcoll-mmap.adb.
>
> I thought it should be rejected during compilation because in mode
> parameter is not allowed in an assignment statement. But, it was
> compiled successfully under GNAT obviously.
>
> What did I miss? Thanks in advance.

Probably that there is an access type involved.

If "Mapped_Region" is an access type, then "Region.Data" could be a
short-hand for "Region.all.Data".

Greetings,

Jacob
-- 
"I've got _plenty_ of common sense!"
"I just choose to ignore it."


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

* Re: In mode parameter in an assignment statement
  2018-10-13  0:48 In mode parameter in an assignment statement Anh Vo
  2018-10-13  5:52 ` Jacob Sparre Andersen
@ 2018-10-13  6:03 ` Per Sandberg
  2018-10-13  8:38 ` Jeffrey R. Carter
  2 siblings, 0 replies; 5+ messages in thread
From: Per Sandberg @ 2018-10-13  6:03 UTC (permalink / raw)


package GNATCOLL.Mmap is
....
private
    type Mapped_Region_Record;
    type Mapped_Region is access Mapped_Region_Record;
...
end GNATCOLL.Mmap;

If you look in the spec you will see that Mapped_Region is an access 
type referencing the incomplete Mapped_Region_Record that is completed 
in the body, any yes the implicit deference makes it look a bit strange.
/P


On 10/13/18 2:48 AM, Anh Vo wrote:
>     -- ...
> 
>     ------------------
>     -- Compute_Data --
>     ------------------
> 
>     procedure Compute_Data (Region : Mapped_Region) is
>        Data_Shift : constant Storage_Offset :=
>           Storage_Offset (Region.User_Offset - Region.System_Offset);
>     begin
>        if Region.User_Size = 0 then
>           Region.Data := Empty_String'Address;
>        elsif Region.Mapped then
>           Region.Data := Region.Mapping.Address + Data_Shift;
>        else
>           Region.Data := Region.Buffer.all'Address + Data_Shift;
>        end if;
>     end Compute_Data;
> 
> end GNATCOLL.Mmap;
> 
> The above code is the tail end of package gnatcoll-mmap.adb.
> 
> I thought it should be rejected during compilation because in mode parameter is not allowed in an assignment statement. But, it was compiled successfully under GNAT obviously.
> 
> What did I miss? Thanks in advance.
> 
> Anh Vo
> 

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

* Re: In mode parameter in an assignment statement
  2018-10-13  0:48 In mode parameter in an assignment statement Anh Vo
  2018-10-13  5:52 ` Jacob Sparre Andersen
  2018-10-13  6:03 ` Per Sandberg
@ 2018-10-13  8:38 ` Jeffrey R. Carter
  2018-10-15 16:05   ` Anh Vo
  2 siblings, 1 reply; 5+ messages in thread
From: Jeffrey R. Carter @ 2018-10-13  8:38 UTC (permalink / raw)


On 10/13/2018 02:48 AM, Anh Vo wrote:
> 
> I thought it should be rejected during compilation because in mode parameter is not allowed in an assignment statement. But, it was compiled successfully under GNAT obviously.
> 
> What did I miss? Thanks in advance.

Others have pointed out that the in-mode parameter, Region, is of an access 
type. What may not be clear is why this makes a difference. It's important to 
distinguish the access value (stored in Region) from its designated value 
(Region.all). The access value is constant within the procedure; any attempt to 
change it

Region := new Mapped_Region_Record;

would result in a compiler error. However, the access value is the only thing 
that is an in-mode parameter and constant within the procedure. The designated 
value, Region.all, is not constant and may be modified. That is what's happening 
in the procedure. That Ada access types allow for implicit dereference and can 
usually be used exactly the same way as the designated value can be confusing if 
one is not aware that the type is an access type.

-- 
Jeff Carter
It's better to be root than to reboot.
119

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

* Re: In mode parameter in an assignment statement
  2018-10-13  8:38 ` Jeffrey R. Carter
@ 2018-10-15 16:05   ` Anh Vo
  0 siblings, 0 replies; 5+ messages in thread
From: Anh Vo @ 2018-10-15 16:05 UTC (permalink / raw)


On Saturday, October 13, 2018 at 1:38:03 AM UTC-7, Jeffrey R. Carter wrote:
> On 10/13/2018 02:48 AM, Anh Vo wrote:
> > 
> > I thought it should be rejected during compilation because in mode parameter is not allowed in an assignment statement. But, it was compiled successfully under GNAT obviously.
> > 
> > What did I miss? Thanks in advance.
> 
> Others have pointed out that the in-mode parameter, Region, is of an access 
> type. What may not be clear is why this makes a difference. It's important to 
> distinguish the access value (stored in Region) from its designated value 
> (Region.all). The access value is constant within the procedure; any attempt to 
> change it
> 
> Region := new Mapped_Region_Record;
> 
> would result in a compiler error. However, the access value is the only thing 
> that is an in-mode parameter and constant within the procedure. The designated 
> value, Region.all, is not constant and may be modified. That is what's happening 
> in the procedure. That Ada access types allow for implicit dereference and can 
> usually be used exactly the same way as the designated value can be confusing if 
> one is not aware that the type is an access type.
>

I got it. Thank you all for your replies specially for Jeffrey's detailed explanation.

Anh Vo

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

end of thread, other threads:[~2018-10-15 16:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-13  0:48 In mode parameter in an assignment statement Anh Vo
2018-10-13  5:52 ` Jacob Sparre Andersen
2018-10-13  6:03 ` Per Sandberg
2018-10-13  8:38 ` Jeffrey R. Carter
2018-10-15 16:05   ` Anh Vo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox