comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.lhota@adarose.com>
Subject: Re: Enumeration literals and their associated functions
Date: Thu, 20 Oct 2005 15:35:47 GMT
Date: 2005-10-20T15:35:47+00:00	[thread overview]
Message-ID: <nPO5f.6112$9N.3063@trndny07> (raw)
In-Reply-To: <dj8955$epg$1@sunnews.cern.ch>

Maciej Sobczak wrote:
> Hi,
> 
> <http://www.adaic.org/standards/95aarm/html/AA-3-5-1.html>, paragraph 6 
> states that for each enumeration literal specification there is an 
> appropriate parameterless function declated.
 >
>    type Color is (RED, GREEN, BLUE);
> 
>    type Function_Access is access function return Color;
> 
>    Fa : Function_Access := RED'Access;
> 
> The compiler reports an error in the last line:
> 
> "prefix of "Access" attribute cannot be enumeration literal"

The problem here is that enumeration literals are Intrinsic functions 
(See ARM 6.3.1). Intrinsic subprograms are "built in" to the compiler. 
When an intrinsic subprogram is called, the compiler will typically 
in-line some pre-defined code for the call. In other words, Intrinsic 
subprograms are always inlined.

The basic arithmetic operations declared in Standard, e.g.

	function "+" (Left : Integer; Right : Integer) return Integer;

are intrinsic functions. The compiler simply uses the one assembler 
instruction for calls to these functions. Enumeration literals are 
always intrinsic functions: the compiler simply uses the internal 
representation value as the result of the call.

The 'Access attribute, when applied to a subprogram, returns the 
location of the code for that subprogram in memory. For intrinsic 
subprograms, however, this code does not exist! Therefore, the 'Access 
attribute cannot be applied to an intrinsic function. If I declare

    function Add (Left, Right : in Integer) return Integer
       renames Standard."+";

then I cannot use Add'Access either.

If you really do need a pointer to a function that always returns a 
certain enumeration value, you can always write you own function, as follows

    function Always_Red return Color is
    begin
       return RED;
    end Always_Red;

    Fa : Function_Access := Always_Red'Access;

> I understand that the use of "RED" in this line is not resolved to be a 
> name of the function. If that is the case, then how can I use this 
> function?

Basically, the only use of the function notation for enumeration 
literals is in renaming declarations.

> Another example:
> 
> Some_Function_That_Expects_Color(RED);
> 
> Is the use of "RED" above considered to be a function call? If yes, then 
> why in RED'Access it is resolved to be a literal?
> If instead "RED" above is considered to be a literal, then what's the 
> sense of having an associated function at all?
> 
> 


-- 
"All things extant in this world,
Gods of Heaven, gods of Earth,
Let everything be as it should be;
Thus shall it be!"
- Magical chant from "Magical Shopping Arcade Abenobashi"

"Drizzle, Drazzle, Drozzle, Drome,
Time for the this one to come home!"
- Mr. Lizard from "Tutor Turtle"



  reply	other threads:[~2005-10-20 15:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-20 14:21 Enumeration literals and their associated functions Maciej Sobczak
2005-10-20 15:35 ` Frank J. Lhota [this message]
2005-10-20 19:26   ` Simon Wright
2005-10-20 16:17 ` Dmitry A. Kazakov
2005-10-20 17:03 ` Niklas Holsti
2005-10-20 22:10   ` Randy Brukardt
2005-10-20 22:59     ` Robert A Duff
2005-10-21  3:10       ` Larry Kilgallen
2005-10-21  4:57       ` Jeffrey R. Carter
2005-11-30  6:07         ` adaworks
replies disabled

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