Re: [sv-ec] reference to outer scope name from nested class

From: Steven Sharp <sharp_at_.....>
Date: Mon Dec 17 2007 - 18:36:07 PST
>From: "danielm" <danielm@aldec.com.pl>

>1. Problem with classes declaration  nested in modules
> 
>module sub;
>    reg a;
>    parameter P=1;
>    class C;
>        bit [P:0] p;
>        function new ();
>            $display("test>>>>>>>>>>>>",a);  //reference to outter scope
>variable a;
>        endfunction
>    endclass
>    
>    C c=new;
>endmodule
> 
>In the LRM (chapter 7. classses) there is no single example of nesting
>classes inside modules/programs/interfaces and resolving issues like above.

While an example would not hurt, it works like any other nested scope in
the language.

>The main issues for above are:
>    - how to deal with multi intantation of module sub - there will be multi
>class declaration each with separate set of static variables? what about
>parametrized p declaration in each instance?

The class type in each instantiation of module sub is a different type
from the one in any other instantiation of module sub.  Each one will
use the parameter value P from its own instance, and its new() function
will print the 'a' from its own instance.


>    - what happen if we use a class from module which is not instantiated in
>the project? is it possible

There is no way to do this, since it is only possible to reference the
class type from inside the module itself.


>2. Problem with classes declaration nested in classes:
> 
> 
>    class C;
>        int i;
>        static int si;
>        class CC;
>                function new();
>                    $display(i,,si);
>                endfunction
>        endclass
>    endclass
>    
>    C:CC cc=new;
> 
>Should non-static properties and function declared in outer class be visible
>in inner class? 
>IMHO  in above only the static property 'si'  should be accesible (like i C
>language) -  this should be clearly pointed out by LRM.
>If we allow reference to 'i' what will be the value if C::i not exists?

The LRM has a problem here.  It implies that properties and methods
declared in the outer class will be visible in the inner class, since
these are nested scopes.  However, this cannot work for non-static
members.  Accessing a non-static property of the class requires having
a handle to an object of a class type.  Inside the non-static methods
of the class, this is provided by the 'this' handle with which the method
was invoked.  But inside a method of the nested class, we do not have a
'this' handle of the outer class.  So we cannot allow access to the
non-static properties of the outer class from the inner class.

Similarly, a call to a non-static method of the outer class from another
non-static method of the outer class is done via the 'this' handle.
Since a method of the inner class does not have a 'this' handle of the
outer class type, it should not call a non-static method of the outer
class.

As you say, the inner class methods should only have access to the static
members of the outer class, and this should be made clear in the LRM.


Steven Sharp
sharp@cadence.com


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Mon Dec 17 18:36:31 2007

This archive was generated by hypermail 2.1.8 : Mon Dec 17 2007 - 18:37:06 PST