ch7-local

2019-11-12

SystemVerilog local

A member declared as local is available only to the methods of the same class, and are not accessible by child classes. However, nonlocal methods that access local members can be inherited and overridden by child class.

Example

​ In the following example, we will declare two variables - one public and another local. We expect to see an error when a local member of the class is accessed from somewhere outside the class. This is because the keyword local is used to keep members local and visible only within the same class.

When accessed from outside the class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ABC;
// By default, all variables are public and for this example,
// let's create two variables - one public and the other "local"
byte public_var;
local byte local_var;

// This function simply prints these variable contents
function void display();
$display ("public_var=0x%0h, local_var=0x%0h", public_var, local_var);
endfunction
endclass

module tb;
initial begin

// Create a new class object, and call display method
ABC abc = new();
abc.display();

// Public variables can be accessed via the class handle
$display ("public_var = 0x%0h", abc.public_var);

// However, local variables cannot be accessed from outside
$display ("local_var = 0x%0h", abc.local_var);
end
endmodule

​ As expected, the compiler gives out a compilation error pointing to the line where a local member is accessed from outside the class.

Simulation Log

1
2
3
4
    $display ("local_var = 0x%0h", abc.local_var);
|
ncvlog: *E,CLSNLO (testbench.sv,24|47): Access to local member 'local_var' in class 'ABC' is not allowed here.
irun: *E,VLGERR: An error occurred during parsing. Review the log file for errors with the code *E and fix those identified problems to proceed. Exiting with code (status 1).

Click to try this example in a simulator! img

​ In the above example, we can remove the line that causes a compilation error and see that we get a good output. The only other function that accesses the local member is the display() function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module tb;
initial begin

ABC abc = new();

// This should be able to print local members of class ABC
// because display() is a member of ABC also
abc.display();

// Public variables can always be accessed via the class handle
$display ("public_var = 0x%0h", abc.public_var);
end
endmodule

Simulation Log

1
2
3
4
ncsim> run
public_var=0x0, local_var=0x0
public_var = 0x0
ncsim: *W,RNQUIE: Simulation is complete.

Click to try this example in a simulator! img

When accessed by child classes

​ In this example, let us try to access the local member from within a child class. We expect to see an error here also because local is not visible to child classes either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Define a base class and let the variable be "local" to this class
class ABC;
local byte local_var;
endclass

// Define another class that extends ABC and have a function that tries
// to access the local variable in ABC
class DEF extends ABC;
function show();
$display ("local_var = 0x%0h", local_var);
endfunction
endclass

module tb;
initial begin

// Create a new object of the child class, and call the show method
// This will give a compile time error because child classes cannot access
// base class "local" variables and methods
DEF def = new();
def.show();

end
endmodule

As expected, child classes cannot access the local members of their parent class.

Simulation Log

1
2
3
4
    $display ("local_var = 0x%0h", local_var);
|
ncvlog: *E,CLSNLO (testbench.sv,10|43): Access to local member 'local_var' in class 'ABC' is not allowed here.
irun: *E,VLGERR: An error occurred during parsing. Review the log file for errors with the code *E and fix those identified problems to proceed. Exiting with code (status 1).

Click to try this example in a simulator! img

Prev Article


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!