comp.lang.ada
 help / color / mirror / Atom feed
* Using renaming declarations to make private subprograms visible
@ 2001-08-25 13:13 Matthew Woodcraft
  2001-08-25 18:27 ` Jeffrey Carter
  0 siblings, 1 reply; 5+ messages in thread
From: Matthew Woodcraft @ 2001-08-25 13:13 UTC (permalink / raw)



Suppose I want a private type that I can convert to a string and back:


package Foo is

   type Token_Type is private;

   function To_Token (Source : String) return Token_Type;

   function To_String (Source : Token_Type) return String;

   [...]

private

   [...]	

end Foo;



Now suppose that (for the moment), I want to implement this type using
an unbounded string. I can make Token_Type a derived type of
Unbounded_String, and use renaming declarations to make some of the
'inherited' subprograms publicly visible:

private

   type Token_Type is new Ada.Strings.Unbounded.Unbounded_String;

   function To_Token (Source : String) return Token_Type
     renames To_Unbounded_String;

    -- No good
   function To_String (Source : Token_Type) return String
     renames To_String;

   [...]


The trouble is, this doesn't work when I want the public name of a
subprogram to be the same as the private name (eg, To_String above), as
my new definition overrides the inherited one, and I end up trying
renaming the function to itself.

Is there any way to make the inherited subprogram publicly visible with
the same name? Or am I better off making Token_Type a record with one
component, rather than a derived type?


-M-



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

* Re: Using renaming declarations to make private subprograms visible
  2001-08-25 13:13 Using renaming declarations to make private subprograms visible Matthew Woodcraft
@ 2001-08-25 18:27 ` Jeffrey Carter
  2001-08-26 10:59   ` Matthew Woodcraft
  0 siblings, 1 reply; 5+ messages in thread
From: Jeffrey Carter @ 2001-08-25 18:27 UTC (permalink / raw)


Matthew Woodcraft wrote:
> 
> Now suppose that (for the moment), I want to implement this type using
> an unbounded string. I can make Token_Type a derived type of
> Unbounded_String, and use renaming declarations to make some of the
> 'inherited' subprograms publicly visible:
> 
> private
> 
>    type Token_Type is new Ada.Strings.Unbounded.Unbounded_String;
> 
>    function To_Token (Source : String) return Token_Type
>      renames To_Unbounded_String;
> 
>     -- No good
>    function To_String (Source : Token_Type) return String
>      renames To_String;

This compiles with GNAT 3.13p:

   function Convert_To_String (Source : Token_Type) return String
      renames To_String;

   function To_String (Source : Token_Type) return String
      renames Convert_To_String;

-- 
Jeff Carter
"I wave my private parts at your aunties."
Monty Python & the Holy Grail



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

* Re: Using renaming declarations to make private subprograms visible
  2001-08-25 18:27 ` Jeffrey Carter
@ 2001-08-26 10:59   ` Matthew Woodcraft
  2001-08-26 12:29     ` Marc A. Criley
  2001-08-27  5:31     ` Jeffrey Carter
  0 siblings, 2 replies; 5+ messages in thread
From: Matthew Woodcraft @ 2001-08-26 10:59 UTC (permalink / raw)


Jeffrey Carter <jrcarter@acm.org> writes:

> This compiles with GNAT 3.13p:
> 
>    function Convert_To_String (Source : Token_Type) return String
>       renames To_String;
> 
>    function To_String (Source : Token_Type) return String
>       renames Convert_To_String;

Yes, it compiles. I tried this before, but I didn't actually run it: it
seems to me that it's just a circular renaming that the compiler doesn't
catch (though I'm surprised the compiler isn't required to catch it).

At any rate, I've tried now, and the code below doesn't run for me with
GNAT 3.13p (segmentation fault, with a stack backtrace containing many
thousands of identical entries).

As an aside, if I want to use -fstack-check, do I have to recompile the
entire runtime?

-M-

===

with Ada.Strings.Unbounded;
package Renaming is

   type Token_Type is private;

   function To_Token (Source : String) return Token_Type;

   function To_String (Source : Token_Type) return String;

private

   type Token_Type is new Ada.Strings.Unbounded.Unbounded_String;

   function To_Token (Source : String) return Token_Type
     renames To_Unbounded_String;

   function Convert_To_String (Source : Token_Type) return String
      renames To_String;

   function To_String (Source : Token_Type) return String
      renames Convert_To_String;

end Renaming;



with Renaming; use Renaming;
with Ada.Text_IO; use Ada.Text_IO;
procedure Try_Renaming is

   S  : constant String := "Hello World";
   T  : Token_Type;

begin
   T := To_Token (S);

   declare
      New_S : constant String := To_String (T);
   begin
      Put (S);
      Put (New_S);
   end;

end Try_Renaming;





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

* Re: Using renaming declarations to make private subprograms visible
  2001-08-26 10:59   ` Matthew Woodcraft
@ 2001-08-26 12:29     ` Marc A. Criley
  2001-08-27  5:31     ` Jeffrey Carter
  1 sibling, 0 replies; 5+ messages in thread
From: Marc A. Criley @ 2001-08-26 12:29 UTC (permalink / raw)


Matthew Woodcraft wrote:
>
> As an aside, if I want to use -fstack-check, do I have to recompile the
> entire runtime?

No, though obviously stack checks will be performed only on the
(application) software that was recompiled with that option.  Which has
always been sufficient for me.

Marc A. Criley
Senior Staff Engineer
Quadrus Corporation
www.quadruscorp.com



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

* Re: Using renaming declarations to make private subprograms visible
  2001-08-26 10:59   ` Matthew Woodcraft
  2001-08-26 12:29     ` Marc A. Criley
@ 2001-08-27  5:31     ` Jeffrey Carter
  1 sibling, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2001-08-27  5:31 UTC (permalink / raw)


Matthew Woodcraft wrote:
> 
> Jeffrey Carter <jrcarter@acm.org> writes:
> 
> > This compiles with GNAT 3.13p:
> >
> >    function Convert_To_String (Source : Token_Type) return String
> >       renames To_String;
> >
> >    function To_String (Source : Token_Type) return String
> >       renames Convert_To_String;

I was rather surprised that it compiles. Someday I'll have time to check
the ARM and see what it says on the matter.

In the meantime, you probably want to make the full definition of
Token_Type a record with a component of Unbounded_String, and provide a
body for your package.

-- 
Jeff Carter
"You empty-headed animal-food-trough wiper."
Monty Python & the Holy Grail



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

end of thread, other threads:[~2001-08-27  5:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-25 13:13 Using renaming declarations to make private subprograms visible Matthew Woodcraft
2001-08-25 18:27 ` Jeffrey Carter
2001-08-26 10:59   ` Matthew Woodcraft
2001-08-26 12:29     ` Marc A. Criley
2001-08-27  5:31     ` Jeffrey Carter

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