Hi :
I would like to override my base PACKET( I cant modify it ) to MYPACKET, so I can add some more properties, such as NAME in below test case. I used SET_TYPE_OVERRIDE_BY_TYPE method, but I still get a compile error.
thanks
`include "uvm_macros.svh"
import uvm_pkg::* ;
class packet extends uvm_object;
`uvm_object_utils(packet);
function new ( string name ="packet");
super.new(name);
endfunction
endclass
class mypacket extends packet;
`uvm_object_utils(mypacket);
string name = " mypacket";
function new ( string name ="mypacket");
super.new(name);
endfunction
endclass
class test extends uvm_test;
`uvm_component_utils(test)
packet _packet;
mypacket _mypacket;
function new ( string name ="test", uvm_component p = null);
super.new(name, p);
endfunction
function void build_phase (uvm_phase phase);
super.build_phase(phase);
set_type_override_by_type(packet::get_type(), mypacket::get_type());
_packet = packet::type_id::create("_packet");
//_mypacket = mypacket::type_id::create("_mypacket");
`uvm_info ("TEST", $sformatf (" name is %0s", _packet.name), UVM_LOW);
endfunction
endclass
module top;
initial
run_test("test");
endmodule
the compile error is :
Error-[MFNF] Member not found
packet_override_at_compile.sv, 35
“this._packet.”
Could not find member ‘name’ in class ‘packet’, at
You’re doing the override correctly (although I believe set_type_override() would be sufficient), but you’re still assigning the result of the packet::type_id::create() call to an object of type packet, which doesn’t have a name field. You need to do
_mypacket = packet::type_id::create("_packet");
Then you’ll be able to access the name field. Since the test is predicated on you’re being able to access this field, it’s OK to code it such that the LHS is of type mypacket.
Thank Tom.
sorry. I am not sure what you mean Then you’ll be able to access the name field. Since the test is predicated on you’re being able to access this field, it’s OK to code it such that the LHS is of type mypacket.****
In my case, let us say PACKET and TEST is IP, I can not modify them. But I want to add some extra methods/properties on PACKET, so I declared a MYPACKET which extended PACKET, and added, for example, NAME. I expect that when I used the compile time override method set_type_override_by_type, the PACKET will be overridden by MYPACKET at compile time, so I can bypass the compile error ( cant find NAME in PACKET ).
Hi Aming,
Even if you create a mypacket type, when you assign it to a packet type, the compiler will treat it as a packet object, so it won’t see the name field. This is a basic part of OOP.
You have two choices:
Make the LHS of the create() statement of type mypacket, or
Keep the LHS of the create() statement of type packet and then cast the packet to mypacket
If you have the ability to modify the test to add the mypacket type, then you may as well use it correctly. ;-)