comp.lang.ada
 help / color / mirror / Atom feed
* Little note on shared generics
@ 1997-04-26  0:00 Robert Dewar
  0 siblings, 0 replies; only message in thread
From: Robert Dewar @ 1997-04-26  0:00 UTC (permalink / raw)



Since we have discussed recently a little bit the advantages and 
disadvantages of macro-instantiated generics, I thought the following
little example from GNAT is entertaining. It shows how the use of
macro-instantiation rather cheaply means that the compiler can do a
lot for you, because it has all the code conveniently at hand.

In this case, we are compiling a case of a triply nested instantiation, and
GNAT says:

test1.adb:8:04: warning: in instantiation at type_liste_contigue_ordonnee.adb:8
test1.adb:8:04: warning: in instantiation at type_arbre_balance.ads:53
test1.adb:8:04: warning: in instantiation at balance_io.ads:41
test1.adb:8:04: warning: infinite recursion
test1.adb:8:04: warning: Storage_Error will be raised at runtime

What is going on in this triply nested instantiation is that we have
in the template:

   function "<" (U, V : in ELEMENT) return BOOLEAN is
   begin
      return LA_CLE (U) < LA_CLE (V);
   end "<";

But LA_CLE is a generi formal function, and in the instantiation for which
the warning is posted, the instantiation looks like:

with BALANCE_IO;
procedure TEST1 is
   function LA_CLE (E : INTEGER) return INTEGER is
   begin
      return E;
   end LA_CLE;
   package AB is new BALANCE_IO ( 1000, INTEGER, INTEGER);
begin
   null;
end TEST1;

LA_CLE is passed down to each level of the instantiation, and given that
it is a function from INTEGER to INTEGER, and the corresponding formal
type ELEMENT is also INTEGER, this is indeed an infinite recursion!

The nice thing about this is that the fact that GNAT does macro insertion
means we get this kind of processing free on generic instantiations.

Robert Dewar
Ada Core Technologies





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1997-04-26  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-26  0:00 Little note on shared generics Robert Dewar

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