comp.lang.ada
 help / color / mirror / Atom feed
From: gate.ready.com!taurus.cs.nps.navy.mil!erickson@decwrl.dec.com  (David Eri
Subject: compilation order of generic body & instantiation
Date: 18 Feb 93 23:52:27 GMT	[thread overview]
Message-ID: <C2o4zF.M7o@taurus.cs.nps.navy.mil> (raw)

I ran into a compilation problem that I don't understand: the package below 
compiles, but if I attempt to instantiate it in a procedure ("driver"), the 
compiler (Meridian Sparc Ada) will produce an error listing at the point of 
instantiation.  The error is:
"driver.a", 3: identifier not found "binary_tree" [LRM 4.1/3]

If the compilation order is changed (the original order is package spec,
body and driver) to spec, driver then body, the compilation produces no
errors, but the linking stage produces:
bamp: "driver" must be recompiled [LRM 10.3/2]
bamp: "driver" has unresolved generic instantiations
bamp: the body of "driver" must be compiled [LRM 10.3/2]

I found that if I move the definition of TRAVERSE so that it comes prior 
to function PARENT (which includes an instantiation of TRAVERSE), the error 
goes away. 

So what is the cause of the problem?  Does Ada require that the body of a 
generic procedure come before an instantiation?  Or is this a bug
in the Meridian compiler?

I tried compiling the same program using Verdix Ada, and had no problems
with either ordering of TRAVERSE and PARENT.

generic
  type ATOM is private;
package BIN_TREE is
  type BINARY_TREE is private;
  type TRAVERSAL_TYPE is (PRE_ORDER, IN_ORDER, POST_ORDER);
  TREE_ERROR: exception;

  function EMPTY(T : BINARY_TREE) return BOOLEAN;
  function PARENT(P, T : BINARY_TREE) return BINARY_TREE;

  generic
    with procedure PROCESS(T: in out BINARY_TREE) is <>;
  procedure TRAVERSE(T    : in out BINARY_TREE;
                     MODE : TRAVERSAL_TYPE);
private
  type NODE;
  type BINARY_TREE is access NODE;
end BIN_TREE;

package body BIN_TREE is
  type NODE is record
    A: ATOM;
    L,R: BINARY_TREE;
  end record;

  function EMPTY(T : BINARY_TREE) return BOOLEAN is
  begin
    return T = null;
  end EMPTY;

  function PARENT(P, T : BINARY_TREE) return BINARY_TREE is
    -- Returns the position in T of P's parent (or null if P is not in
    -- T). Precondition: P /= T and P is non-empty.
    ROOT : BINARY_TREE := T; -- local copy of T used as in out parameter
    RESULT : BINARY_TREE;

    procedure TEST_PARENT(T : in out BINARY_TREE) is
      -- uses non-local P and RESULT (which are declared in PARENT) in
      -- order to conform to the specifications for PROCESS.  Likewise,
      -- T is in out
    begin
      if (T.L = P) or (T.R = P) then
        RESULT := T;
      end if;
    end TEST_PARENT;

    procedure FIND_PARENT is
      new TRAVERSE(PROCESS => TEST_PARENT);
    -- FIND_PARENT does a traverse, and sets non-local variable RESULT

  begin       -- PARENT
    if P = ROOT or EMPTY(P) then
      raise TREE_ERROR;
    else
      FIND_PARENT(ROOT, PRE_ORDER);
      return RESULT;
    end if;
  end PARENT;

  procedure TRAVERSE(T    : in out BINARY_TREE;
                     MODE : TRAVERSAL_TYPE) is
    -- visits every node in T in preorder, inorder or postorder.
    -- T is an in out parameter, since Process requires an in out 
    -- parameter.
  begin
    if not EMPTY(T) then
      if MODE = PRE_ORDER then
        PROCESS(T);
        TRAVERSE(T.L, MODE);
        TRAVERSE(T.R, MODE);
      elsif MODE = IN_ORDER then
        TRAVERSE(T.L, MODE);
        PROCESS(T);
        TRAVERSE(T.R, MODE);
      else
        TRAVERSE(T.L, MODE);
        TRAVERSE(T.R, MODE);
        PROCESS(T);
      end if;
    end if;
  end TRAVERSE;
end BIN_TREE;

with BIN_TREE;
procedure DRIVER is
  package MY_TREE is new BIN_TREE(CHARACTER);  -- error occurs here
  use MY_TREE;
  T: BINARY_TREE;
begin
  if EMPTY(T) then
    return;
  end if;
end DRIVER;

             reply	other threads:[~1993-02-18 23:52 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-02-18 23:52 gate.ready.com!taurus.cs.nps.navy.mil!erickson [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-02-24 16:20 compilation order of generic body & instantiation agate!howland.reston.ans.net!newsserver.jvnc.net!netnews.upenn.edu!prijat
1993-02-25 16:05 emory!europa.eng.gtefsd.com!howland.reston.ans.net!usc!cs.utexas.edu!uwm.
replies disabled

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