comp.lang.ada
 help / color / mirror / Atom feed
* Re: conditional compiles ?
@ 1992-09-22 20:22 Dave Marshall
  0 siblings, 0 replies; 2+ messages in thread
From: Dave Marshall @ 1992-09-22 20:22 UTC (permalink / raw)


In article <1992Sep22.115353.27412@umbc3.umbc.edu>, dipto@umbc4.umbc.edu (Dipto
 Chakravarty) writes:
> 
> I am new to Ada, and was wondering how to do conditional compiles under
> an Ada program. Conditional compiles using #ifdef and #endif, etc.,
> allows us to maintain a large software which has several system specific 
> routines using C. 
> 
> Please reply by email, as I am not a regular "rn" reader. Thanx in advance.
> 
> .


It's not possible to use conditional compilation in Ada like you can in C.

However, by using a Boolean parameter to a function, procedure, or generic
package or subprogram, you can achieve the same effect.  (Examples below.)

Paragraph 2 of Section 10.6 of the LRM states:

A compiler may find that some statements or subprograms will never be executed,
 
for example, if their execution depends on a condition known to be FALSE.  The
corresponding object machine code can then be omitted.  This rule permits the
effect of conditional compilation within the language.

Examples:

(I didn't compile these, so don't sue me if they're not exactly right.)

function MY_FUNCTION ( A : in INTEGER, DEBUG: in BOOLEAN) is

begin

  if DEBUG then

-- some debug code

  end if;

-- and so on

end MY_FUNCTION;

The procedure example is equally trivial.

I think that the way to give a compiler the best chance to optimize the
code away is to use the conditional parameter as a parameter for
instantiating a generic package or subprogram.

generic
  DEBUG : BOOLEAN;
package MY_PACKAGE is

  procedure DO_WAH;

end MY_PACKAGE;

Now implement the body of MY_PACKAGE and surround your conditional code
with "if"s on DEBUG.  When you instantiate the package, the compiler
should be able to do what's right.

package MY_DEBUG_TEST is new MY_PACKAGE ( DEBUG => TRUE);

OR

package MY_FINAL_PRODUCT is new MY_PACKAGE ( DEBUG => FALSE);

You can do the same thing with procedures.

generic
  DEBUG : BOOLEAN;
procedure MY_TEST_PROCEDURE;

-- 
                                           Dave Marshall
                                           dmarshal@stars.reston.unisys.com

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

* Re: conditional compiles ?
@ 1992-09-29 16:37 Robert I. Eachus
  0 siblings, 0 replies; 2+ messages in thread
From: Robert I. Eachus @ 1992-09-29 16:37 UTC (permalink / raw)


    The other replies to this message are barking up the wrong tree.
We all know that in Ada it is almost always possible to code in an
implementation independant manner so a first order answer is that you
don't use anything like #ifdef.  (Or #include, a lot of early Ada
compilers implemented pragma INCLUDE, but no one ever used it.)  If
you do have to have hardware specific code, the usual approach is to
put all that code in one or two packages and if necessary maintain
several versions of it.

--

					Robert I. Eachus

with STANDARD_DISCLAIMER;
use  STANDARD_DISCLAIMER;
function MESSAGE (TEXT: in CLEVER_IDEAS) return BETTER_IDEAS is...

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

end of thread, other threads:[~1992-09-29 16:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-09-29 16:37 conditional compiles ? Robert I. Eachus
  -- strict thread matches above, loose matches on Subject: below --
1992-09-22 20:22 Dave Marshall

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