Using Class Scope Resolution operator within in-line Constraint

I was wondering if class scope resolution operator can help to resolve an identifier within an inline constraint.


class trans;
   rand bit [31:0] addr;
 endclass

 class seq;

 rand bit [31:0] addr;
 trans t;

 function void body();
   int addr ;
 
     addr = 10 ;
        t = new();

   if(t.randomize() with { t.addr == seq::addr ; } )
     $display("Success with %p",t);
   else
     $display("Fails");


 endfunction

 endclass

 seq obj ;


 initial begin

     obj = new() ;

     obj.body();


 end


This works and I observe ::

Success with '{addr:'h0}

LRM explains about local::qualifier . So I was wondering whether Class Scope Resolution Operator Could be used too ?

In reply to MICRO_91:

Yes, it works in this. I can think of a contrived example where local:: is still needed. For example, when the classes trans and seq are defined in different packages as extensions to different classes with the same base class name.

The search rules are quite simple: the in-line constraint searches identifiers in the object being randomized, if not found, it searches from the scope of the call to randomize.

In your example the reference to t.addr is searching for t in the object being randomized and does not find it. It then searches from the scope of the body() task and finds t a member of the seq class. Then it resolves addr as a member of t. You could have just written addr == seq::addr.

In reply to dave_59:

Hi Dave ,

Could you please elaborate where local:: is definitely needed over class scoped identifier

Based on your comments I could figure out code to be ::


  // Within PKG1
     class  Base ;
        ....
     endclass
 
     class Intermediate1 extends Base ;
         ...
     endclass

     class  trans  extends Intermediate1 ;
  
       rand bit [31:0]  addr ;

     endclass      
 
 // Within PKG2
     // Import PKG1
     class  Base ;
        ....
     endclass
 
     class Intermediate2 extends Base ;
         ...
     endclass

     class  seq  extends Intermediate2 ;
  
       rand bit [31:0] addr;
       trans t;
 
 function void body();
   int addr ;
 
     addr = 10 ;
        t = new();
 
   if(t.randomize() with { addr == seq::addr ; } ) // This would still work , I believe 
     $display("Success with %p",t);
   else
     $display("Fails");
 
 
 endfunction


     endclass



In reply to MICRO_91:

The issue would be if addr was also declared inside the Base class and you tried to use Base::addr. But this is too convoluted to pursue.

You still need local:: to reference the addr declared inside the body method.