Version 1.1 of acs/ac-00292.txt

Unformatted version of acs/ac-00292.txt version 1.1
Other versions for file acs/ac-00292.txt

!standard 4.6(24.12/2)          17-09-05 AC95-00292/00
!class Amendment 17-09-05
!status received no action 17-09-05
!status received 17-08-05
!subject Need a way to convert a constant to a variable
!summary
!appendix

!topic Need a way to convert a constant to a variable
!reference Ada 2012 RM
!from Victor Porton 17-08-05
!keywords constant, variable, view, conversion
!discussion

Sometimes one needs to convert a constant view into variable view (I am
fully conscious that after this the programmer should take care not to
change the object of the view).

In the following (not compilable with GNAT 7.1.0) code I present my
best attempt to solve the following problem:

Write a function with an "in" indefinite holder with a string, return
chars_ptr corresponding to the string.

It looks like there is no solution of this in Ada 2012 :-(

with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with Ada.Containers.Indefinite_Holders;

procedure Conv is

   package Char_Array_Holders is
      new Ada.Containers.Indefinite_Holders(char_array);

   type C_String_Holder is new Char_Array_Holders.Holder
      with null record;
   
   function To_C_String (Object: C_String_Holder) return chars_ptr is
      Value: char_array renames Constant_Reference(Object).Element.all;
      Value2: aliased Char_Array(Value'Range) with Import;
      for Value2'Address use Value'Address;
   begin
      return To_Chars_Ptr(Char_Array_Access'(Value2'Access));
   end;
begin
   null;
end;

$ gnatgcc -c conv.adb -c conv.adb 
conv.adb:13:07: warning: aliased object has explicit bounds
conv.adb:13:07: warning: declare without bounds (and with explicit
initialization)
conv.adb:13:07: warning: for use with unconstrained access
conv.adb:16:46: object subtype must statically match designated subtype

My current workaround is to define my own "indefinite holder" type to
use it in my software instead of Ada.Containers.Indefinite_Holders.

I propose the following language change:

Please add 'Unchecked_Variable attribute.

There are two possible meanings (of which we should choose one) of the
attribute:

1. C'Unchecked_Variable returns a variable view of a constant C.

2. A'Unchecked_Variable returns the corresponding access to a variable
for an access A to constant.

Both variants seem to solve the trouble.

Can any problem with different representation of constant and variables
appear?

***************************************************************

From: Martin Dowie
Sent: Monday, August 7, 2017  7:29 AM

with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with Ada.Containers.Indefinite_Holders;

procedure Conv is

   package Char_Array_Holders is
     new Ada.Containers.Indefinite_Holders(char_array);

   type C_String_Holder is new Char_Array_Holders.Holder
     with null record;
   
   function To_C_String (Object: C_String_Holder) return chars_ptr is
   begin
      return To_Chars_Ptr (Constant_Reference(Object).Element);
   end To_C_String;
begin
   null;
end;

***************************************************************

From: Victor Porton
Sent: Monday, August 7, 2017  3:19 PM

You are right, your code does what I need. I've rewritten my actual free
software project at which I encountered the trouble with your advice.

This time I was a fool. I tried a few hours and found no solution of my
particular problem.

However, my idea that sometimes one may need to change a constant view into
a variable view, it may be nevertheless worth.

It is dangerous, but I proposed the attribute started from "Unchecked_"
for it, so the user should know what he does.

***************************************************************

Questions? Ask the ACAA Technical Agent