Dynamic type casting Error

Hi All,

I have env called testenv #(`a) extends uvm_env this is created inside agentenv and agentenv is created inside pkgenv and pkgenv is created inside socenv,

here I want to typcast testenv to another class of type uvm_env I am trying it as bellow but getting runtime typecast error

testenv #(`a) test;
uvm_component c;
c=uvm_top.find("uvm_test_top.socenv.pkgenv.agentenv.testenv")
c.print();
if(!$cast(test,uvm_top.find("uvm_test_top.socenv.pkgenv.agentenv.testenv")))
`uvm_error("typecast failed");
else
test.print();

output :
typecast failed

In reply to siddu Angadi:

I guess your testenv is extended from uvm_component. Right?
And the type testenv is parameterized with a parameter `a, i.e testenv is parameterized but uvm_component is not parameterzied. This causes the cast error.

In reply to chr_sue:

NO, That is also extended from uvm_env.

Thanks,
Sid

In reply to siddu Angadi:

Does c.print() work correctly? Can you try the following?

 assert(testenv#(`a)::get_type == c.get_object_type());

If that fails, then you have incompatible types that cannot be cast.

We need to see the code used to declare and construct “testenv” inside your agent. The parameter types need to match coming from the same package).

In reply to dave_59:

Hi Deve,

Yes, c.print() is working properly I am able to get the all the details of testenv.

Thanks,
Sid

In reply to siddu Angadi:

Two questions :

  1. What’s the value of macro `a ?
  2. Code for the handle declaration and it’s instantiation for the Env component named “testenv”

In reply to ABD_91:

macro a has 56,0,56,0,24,12,4,8,7

class socenv extends uvm_env;
///
pkgenv pkgenv;

virtual function void build_phase(uvm_phase phase);
pkgenv=pkgenv(`a)::type_id::create(“pkgenv”, this);
endfunction:build_phase

class pkgenv extends uvm_env;
///
agentenv agentenv;

virtual function void build_phase(uvm_phase phase);
agentenv=agentenv::type_id::create(“agentenv”, this);
endfunction:build_phase

class agentenv extends uvm_env;
///
testenv #(`a) testenv;

virtual function void build_phase(uvm_phase phase);
testenv=testenv::type_id::create(“testenv”, this);
endfunction:build_phase

In reply to dave_59:

In reply to siddu Angadi:
Does c.print() work correctly?
yes
Can you try the following?

 assert(testenv#(`a)::get_type == c.get_object_type());

If that fails, then you have incompatible types that cannot be cast.
Yes, here it is failing
We need to see the code used to declare and construct “testenv” inside your agent. The parameter types need to match coming from the same package).

parameters are matching too. if packages are different is that matters here ?

In reply to siddu Angadi:

If you have included the same class definition in multiple packages, you have multiple incompatible classes.

In reply to dave_59:
Hi Dave Rich,

Thanks, for the information, I think issue is because of same class present as part of package and in agent we are using same package to create but when it comes to the testenv #(`a) test, it is using different class instead from package let me try and get back on this.

Thanks,
Sid