Fix class constructor chaining corner cases

There are some corner cases around class constructor chaining that can
result in chained constructors not being called, or being called multiple
times.

This is primarily related to that a class can have either an explicit
constructor called `new` and an implicit constructor called `new@` and how
the lookup of them is done.

Lookup is currently done independently for the implicit and explicit
constructor using the `method_from_name()` method. `method_from_name()`
will search the whole class hierarchy for a class method. If a class
doesn't have a method by that name it will look in the parent class and so
on.

As a result the lookup for the explicit constructor can return the explicit
constructor of a parent class if the class itself only has an implicit
constructor and vice versa.

E.g. in the following example the constructor of D will not be called
because the implicit constructor for C is found when looking for a implicit
constructor in D.

```
class C;
  int x = 10;
endclass

class D extends C;
  function new;
    $display("D");
  endfunction
endclass

class E extends D;
  int y;
  function new;
    y = 20;
  endfunction
endclass

E e = new;
```

There is a similar case where the constructor of a base class can be called
multiple times if the base class has an explicit constructor and the
derived class has an implicit constructor. In that case the derived class
constructor will call the base class constructor, but the code that is
emitted for the `new` statement will call both of them.

To mitigate this introduce a new method to lookup the constructor that will
search for either the explicit or implicit constructor in the current class
and only continue to search in the base class if neither is found.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
This commit is contained in:
Lars-Peter Clausen 2023-06-17 07:50:05 -07:00
parent 0651e0be17
commit 6ea01cbf7f
4 changed files with 22 additions and 40 deletions

View File

@ -6634,29 +6634,7 @@ NetExpr* PENewClass::elaborate_expr_constructor_(Design*des, NetScope*scope,
{ {
ivl_assert(*this, ctype); ivl_assert(*this, ctype);
// If there is an initializer function, then pass the object NetScope *new_scope = ctype->get_constructor();
// through that function first. Note that the initializer
// function has no arguments other than the object itself.
if (NetScope*new1_scope = ctype->method_from_name(perm_string::literal("new@"))) {
NetFuncDef*def1 = new1_scope->func_def();
ivl_assert(*this, def1);
ivl_assert(*this, def1->port_count()==1);
vector<NetExpr*> parms1 (1);
parms1[0] = obj;
// The return value of the initializer is the "this"
// variable, instead of the "new&" scope name.
NetNet*res1 = new1_scope->find_signal(perm_string::literal(THIS_TOKEN));
ivl_assert(*this, res1);
NetESignal*eres = new NetESignal(res1);
NetEUFunc*tmp = new NetEUFunc(scope, new1_scope, eres, parms1, true);
tmp->set_line(*this);
obj = tmp;
}
NetScope*new_scope = ctype->method_from_name(perm_string::literal("new"));
if (new_scope == 0) { if (new_scope == 0) {
// No constructor. // No constructor.
if (parms_.size() > 0) { if (parms_.size() > 0) {

View File

@ -3289,25 +3289,9 @@ NetProc* PChainConstructor::elaborate(Design*des, NetScope*scope) const
// going to pass this to the chained constructor. // going to pass this to the chained constructor.
NetNet*var_this = scope->find_signal(perm_string::literal(THIS_TOKEN)); NetNet*var_this = scope->find_signal(perm_string::literal(THIS_TOKEN));
// If super.new is an implicit constructor, then there are no
// arguments (other than "this" to worry about, so make a
// NetEUFunc and there we go.
if (NetScope*new_scope = class_super->method_from_name(perm_string::literal("new@"))) {
NetESignal*eres = new NetESignal(var_this);
vector<NetExpr*> parms(1);
parms[0] = eres;
NetEUFunc*tmp = new NetEUFunc(scope, new_scope, eres, parms, true);
tmp->set_line(*this);
NetAssign_*lval_this = new NetAssign_(var_this);
NetAssign*stmt = new NetAssign(lval_this, tmp);
stmt->set_line(*this);
return stmt;
}
// If super.new(...) is a user defined constructor, then call // If super.new(...) is a user defined constructor, then call
// it. This is a bit more complicated because there may be arguments. // it. This is a bit more complicated because there may be arguments.
if (NetScope*new_scope = class_super->method_from_name(perm_string::literal("new"))) { if (NetScope*new_scope = class_super->get_constructor()) {
int missing_parms = 0; int missing_parms = 0;
NetFuncDef*def = new_scope->func_def(); NetFuncDef*def = new_scope->func_def();

View File

@ -172,6 +172,22 @@ NetScope*netclass_t::method_from_name(perm_string name) const
} }
NetScope* netclass_t::get_constructor() const
{
auto task = class_scope_->child(hname_t(perm_string::literal("new")));
if (task)
return task;
task = class_scope_->child(hname_t(perm_string::literal("new@")));
if (task)
return task;
if (super_)
return super_->get_constructor();
return nullptr;
}
NetNet* netclass_t::find_static_property(perm_string name) const NetNet* netclass_t::find_static_property(perm_string name) const
{ {
NetNet *net = class_scope_->find_signal(name); NetNet *net = class_scope_->find_signal(name);

View File

@ -94,6 +94,10 @@ class netclass_t : public ivl_type_s {
// The task method scopes from the method name. // The task method scopes from the method name.
NetScope*method_from_name(perm_string mname) const; NetScope*method_from_name(perm_string mname) const;
// Returns the constructor task method of the class. Might be nullptr if
// there is nothing to do in the constructor.
NetScope* get_constructor() const;
// Find the elaborated signal (NetNet) for a static // Find the elaborated signal (NetNet) for a static
// property. Search by name. The signal is created by the // property. Search by name. The signal is created by the
// elaborate_sig pass. // elaborate_sig pass.