I'm trying to understand about case between same extended class in uvm as example below,

 module test_module (); class A; endclass class B extends A; endclass class C extends A; endclass A a_h; B b_h; C c_h; initial begin c_h = new(); b_h = c_h; $cast(c_h, b_h); end endmodule : test_module 

I got xrun: 20.09-s003: (c) Copyright 1995-2020 Cadence Design Systems, Inc. b_h = c_h; | xmvlog: *E,TYCMPAT (testbench.sv,20|12): assignment operator type check failed (expecting datatype compatible with 'class test_module::B' but found 'class test_module::C' instead). Error message.

I thought b_h and c_h are extended by same class.

But b_h and c_h are not same class type so.

I assign b_h = c_h;

But why does assign make error?

0

2 Answers

It would be easier to understand your problem if you added class properties to your example.

module test_module (); class A; int p1; endclass class B extends A; int p2; endclass class C extends A; int p3; endclass A a_h; B b_h; C c_h; initial begin c_h = new(); b_h = c_h; $cast(c_h, b_h); end endmodule : test_module 

The statement c_h = new(); constructs a class object with the properties p1 and p3. So you should be able to reference c_h.p1 and c_h.p3. You are allowed to reference any property in a class object based on the type of the class variable.

The class variable b_h allows you to access b_h.p1 and b_h.p2. However, if you were allowed to execute the statement b_h = c_h; and then try to reference b_h.p2, that would be a problem because the object you placed in b_h never constructed a p2 property.

It does not matter that your example never makes a reference to p2; the compiler will never allow the assignment. You can only make direct assignments from one class variable to another if they are the same class type, or going from an extended to a base type (upcast).

you have the following scheme:

 /--->B A---> \--->C 

There is direct dependency between A and B or A and C (A is a superclass for B and C). There is no direct dependency between B and C.

Therefore you can easily do:

a_h = b_h; // no casting needed to assign to superclass-A-type $cast(b_h, a_h); // you need a cast to upcast the base pointer. 

But casting $cast(b_h, c_h) is not possible because they are not related.

more formal:

When $cast is applied to class handles, it succeeds in only three cases:

  1. The source expression and the destination type are assignment compatible, that is, the destination is the same type or a superclass of the source expression.
  2. The type of the source expression is cast compatible with the destination type, that is, either: — the type of the source expression is a superclass of the destination type, or — the type of the source expression is an interface class (see 8.26) and the source is an object that is assignment compatible with the destination type. This type of assignment requires a run-time check as provided by $cast.
  3. The source expression is the literal constant null.
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.