comp.lang.ada
 help / color / mirror / Atom feed
* Placement of pragma Convention
@ 1997-06-20  0:00 Christopher Green
  1997-06-23  0:00 ` Tucker Taft
  0 siblings, 1 reply; 4+ messages in thread
From: Christopher Green @ 1997-06-20  0:00 UTC (permalink / raw)



I have a question concerning the correct placement of pragma Convention;
in particular, whether GNAT and other Ada 95 compilers are properly inter-
preting the applicable rules.

Given the following code:

package Callbacks is

    procedure OrdinaryProc;

end Callbacks;

package body Callbacks is

    procedure OrdinaryProc is
    begin
        null;
    end OrdinaryProc;

    procedure CallbackProc is
    begin
        null;
    end CallbackProc;
    pragma Convention (C, CallbackProc);

end Callbacks;

I get the following different results when it is compiled on different Ada 95
compilers.  All compilers are self-hosted on a SPARC/Solaris 2.5 host.

    Apex 2.2.3A gives no errors and no warnings.

    ObjectAda V7.1 gives no errors and no warnings.

    GNAT 3.09 gives the following error:

        callbacks.adb:12:05: pragma "Convention" requires separate spec and 
        must come before body

It would appear to me that the Apex and ObjectAda compilers are correct and
the GNAT compiler is incorrect, at least by the following reasoning:

    B.1(4): An interfacing pragma is a representation pragma that is one of
    the pragmas Import, Export, or Convention...

    B.1(30): An interfacing pragma is a program unit pragma when applied to
    a program unit (see 10.1.5).

    10.1.5(3-4): A program unit pragma shall appear in one of these places:
    At the place of a compilation_unit, in which case the pragma shall im-
    mediately follow in the same compilation (except for other pragmas) a
    library_unit_declaration that is a subprogram_declaration... and the
    pragma shall have an argument that is a name denoting that declaration.

    10.1.1(5): library_unit_declaration ::= subprogram_declaration ...
    10.1.1(9): A library unit is a program unit that is declared by a 
    library_item.

    6.1(2): subprogram_declaration ::= subprogram_specification;
    6.1(4): subprogram_specification ::= 
                procedure defining_program_unit_name parameter_profile
                | function defining_designator parameter_and_result_profile

So far, GNAT seems to be correct.  Pragma Convention is an interfacing
pragma, an interfacing pragma is a program unit pragma, a program unit
pragma immediately follows a subprogram declaration, and a subprogram
declaration is a subprogram specification.

However, consider the legality rule for a subprogram body:

    6.3(4): In contrast to other bodies, a subprogram_body need not be the
    completion of a previous declaration, in which case the body declares
    the subprogram...

Thus, a subprogram body that completes no other declaration is itself both
body and declaration.  The pragma Convention in the example therefore ful-
fills the entire chain of legality rules:

    The body of CallbackProc in the package body of Callbacks is the
    declaration of CallbackProc, by 6.3(4).

    The body of CallbackProc (that is also the declaration) is a declaration 
    of a library unit, by 10.1.1(9).

    The correct position of a program unit pragma pertaining to CallbackProc
    is immediately following the declaration, thus immediately following the
    body of CallbackProc, by 10.1.5(3-4).

    An interfacing pragma pertaining to CallbackProc is a program unit
    pragma, by B.1(30).

    Pragma Convention is an interfacing pragma, by B.1(4).

    Therefore, pragma Convention is correctly positioned following the
    body of CallbackProc.

Although B.1(43) allows an implementation to place restrictions on inter-
facing pragmas, since other compilers support the use of pragma Convention
as shown, the restriction in the case of GNAT appears to be gratuitous and
does compromise portability of Ada 95 code.

Any "language lawyers" interested in commenting on the above, please respond
by posting to this newsgroup or by e-mail.

Chris Green                                  Email cgreen@atc.com
Advanced Technology Center                   Phone (714) 583-9119
22982 Mill Creek Drive                                   ext. 220
Laguna Hills, CA 92653                       Fax   (714) 583-9213




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

* Re: Placement of pragma Convention
  1997-06-20  0:00 Placement of pragma Convention Christopher Green
@ 1997-06-23  0:00 ` Tucker Taft
  1997-06-23  0:00   ` Christopher Green
  0 siblings, 1 reply; 4+ messages in thread
From: Tucker Taft @ 1997-06-23  0:00 UTC (permalink / raw)



Christopher Green (cgreen@yosemite.atc.com) wrote:

: I have a question concerning the correct placement of pragma Convention;
: in particular, whether GNAT and other Ada 95 compilers are properly inter-
: preting the applicable rules.

: Given the following code:

: package Callbacks is

:     procedure OrdinaryProc;

: end Callbacks;

: package body Callbacks is

:     procedure OrdinaryProc is
:     begin
:         null;
:     end OrdinaryProc;

:     procedure CallbackProc is
:     begin
:         null;
:     end CallbackProc;
:     pragma Convention (C, CallbackProc);

: end Callbacks;

: I get the following different results when it is compiled on different Ada 95
: compilers.  All compilers are self-hosted on a SPARC/Solaris 2.5 host.

:     Apex 2.2.3A gives no errors and no warnings.

:     ObjectAda V7.1 gives no errors and no warnings.

:     GNAT 3.09 gives the following error:

:         callbacks.adb:12:05: pragma "Convention" requires separate spec and 
:         must come before body

: It would appear to me that the Apex and ObjectAda compilers are correct and
: the GNAT compiler is incorrect, ...

Despite my affiliation, GNAT is in the right on this one.
A program unit pragma may follow the program unit only if the
program unit is a subprogram_declaration, generic_subprogram_declaration,
or generic instantiation (10.1.5(4,6)).  In all other cases, it needs to be
immediately inside the program unit.  Although a "subprogram_body"
is considered to be a "declaration," it is *not* within the category 
"subprogram_declaration," and so any program unit pragma must
be immediately inside it, rather than following it.

Amusingly enough, at least in the recent past, Object Ada enforced
this rule for pragma Inline (which is also a program unit pragma)
and GNAT did not.

: ...
: Any "language lawyers" interested in commenting on the above, please respond
: by posting to this newsgroup or by e-mail.

See above.

: Chris Green                                  Email cgreen@atc.com
: Advanced Technology Center                   Phone (714) 583-9119
: 22982 Mill Creek Drive                                   ext. 220
: Laguna Hills, CA 92653                       Fax   (714) 583-9213

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




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

* Re: Placement of pragma Convention
  1997-06-23  0:00 ` Tucker Taft
@ 1997-06-23  0:00   ` Christopher Green
  1997-06-24  0:00     ` Robert Dewar
  0 siblings, 1 reply; 4+ messages in thread
From: Christopher Green @ 1997-06-23  0:00 UTC (permalink / raw)



In article <EC8x58.2uJ.0.-s@inmet.camb.inmet.com>,
Tucker Taft <stt@houdini.camb.inmet.com> wrote:
>Despite my affiliation, GNAT is in the right on this one.
>A program unit pragma may follow the program unit only if the
>program unit is a subprogram_declaration, generic_subprogram_declaration,
>or generic instantiation (10.1.5(4,6)).  In all other cases, it needs to be
>immediately inside the program unit.  Although a "subprogram_body"
>is considered to be a "declaration," it is *not* within the category 
>"subprogram_declaration," and so any program unit pragma must
>be immediately inside it, rather than following it.

Thank you, Tucker, for your clear and complete explanation.

I see the point; however, the distinction between a declaration
of a subprogram and a subprogram_declaration seems to me a fine
one.

Chris Green                                  Email cgreen@atc.com
Advanced Technology Center                   Phone (714) 583-9119
22982 Mill Creek Drive                                   ext. 220
Laguna Hills, CA 92653                       Fax   (714) 583-9213




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

* Re: Placement of pragma Convention
  1997-06-23  0:00   ` Christopher Green
@ 1997-06-24  0:00     ` Robert Dewar
  0 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1997-06-24  0:00 UTC (permalink / raw)



Chris said

<<Thank you, Tucker, for your clear and complete explanation.

I see the point; however, the distinction between a declaration
of a subprogram and a subprogram_declaration seems to me a fine
one.

>>


One thing that will help guide your intuition here is to think of a one
pass compiler. There is no requirement that Ada compilers only have one
pass, but the semantics of the language is basically designed as though
they do, with only a few exceptions (this is because that which can be
compiled in one pass can be read by a human in one pass!)

Obviously if you give a convention for a subprogram, you need to know
the convention *before* you compile code for the body, hence the perfectly
reasonable restriction that the pragma must appear before the body.





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

end of thread, other threads:[~1997-06-24  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-06-20  0:00 Placement of pragma Convention Christopher Green
1997-06-23  0:00 ` Tucker Taft
1997-06-23  0:00   ` Christopher Green
1997-06-24  0:00     ` Robert Dewar

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