RE: [sv-ec] sub-class constructor

From: Arturo Salz <Arturo.Salz_at_.....>
Date: Thu Feb 12 2009 - 11:02:59 PST
Jonathan,

Strictly, the constructor using the if form is illegal while the one using the conditional operator is legal. This is true even though both forms are equivalent (in this case). The LRM requires super.new() to be the first *statement*. The same goes for the calling a function, or initializing constructor arguments in their declaration.

I don't believe the LRM currently forbids calling member functions, but that should probably be the case, particularly virtual methods - the LRM does not clearly define the semantics of virtual method calls in constructors for which there are (at least) two mechanisms: the Java model, which always calls the same (final) virtual methods and the C++ model, which adjusts the virtual method during the derivation chain. I believe implementations may already differ on this respect and it would be wise to explicitly spell this out in the LRM - either forbid them or warn users that the semantics are undefined.

As for Shalom's previous question, there are two forms to specify the base class constructor call, through the an explicit call to super.new() in the derived class constructor, or via the special syntax class extension syntax:
        class Derived extends Base(arg1, arg2);
In the second case, the class extension informs the compiler how to call the base class constructor, otherwise the compiler has no way of knowing which arguments to pass to the base class constructor, hence, requiring an explicit call to super.new with the required arguments - I believe this is the situation the Doulos guide was attempting to cover.

        Arturo

-----Original Message-----
From: owner-sv-ec@eda.org [mailto:owner-sv-ec@eda.org] On Behalf Of jonathan.bromley@doulos.com
Sent: Thursday, February 12, 2009 6:28 AM
To: Bresticker, Shalom
Cc: sv-ec@server.eda.org; Feldman, Yulik
Subject: Re: [sv-ec] sub-class constructor

Shalom,

> The Doulos SV Golden Reference Guide says the following,
> "If the new() function of a subclass is overridden, it does not call
> the parent class's constructor by default. This will almost always
> need to be called explicitly as the first statement in the
> overloaded constructor."
> Is this correct? It is certainly not clear from the LRM.

If this is the case, it is at best misleading and at worst
wrong, and we (Doulos) need to fix it.

First off, I'm not sure "override" is the right term to use
for a user-written constructor in a derived class.  I was
under the impression that "override" strictly applies only
to virtual methods.

Second, there is no doubt that SV implicitly calls super.new(),
with no argument, unless the first executable statement of
a derived class's constructor is an explicit call to super.new().
We take some care to explain how a defaulted call to super.new()
will go wrong if super.new has one or more mandatory arguments,
and that's probably where the misleading text springs from.

For my part, I'll take an action to get that cleaned-up in
our GRG.  However, the SV LRM could perhaps be a little
more helpful than it is in clarifying this behaviour. There
are some corner cases that don't seem to me to be too
clear.  In particular, any attempt to compute the arguments
to super.new() will seriously muddy the waters:

  class C;
    function new(int x);
    ...
  endclass : C

  class D extends C;
    function new(bit b);
      if (b)
        super.new(-1);
      else
        super.new(1);
   ...

Is that legal?  If not, could it be made legal thus?

   function D::new(bit b);
     super.new(b? -1: 1);

Would it be legal to call a function to compute
the arguments? (Evidently, not a *member* function.)

In all such cases, super.new() is not the first
executable code in D::new() even if it is
lexically the first code to appear.

Thanks
--
Jonathan Bromley
Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, Hampshire, BH24 1AW,
UK
Tel: +44 (0)1425 471223                   Email:
jonathan.bromley@doulos.com
Fax: +44 (0)1425 471573                           Web:
http://www.doulos.com

This message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Thu Feb 12 11:03:58 2009

This archive was generated by hypermail 2.1.8 : Thu Feb 12 2009 - 11:04:26 PST