Illegal hierarchical reference through a let construct

65 Views Asked by At

I have a package that I wanted to put some useful helper lets in to do some common calculations that I use all over the place. I thought I could just use them in a class, but when I do I get an error. Here is a basic example that shows the issue:

package let_pkg;

   let sum(b,c) = b+c;

endpackage

class class_that_uses_let;

   int a,b,c;

   task use_let;
      a = let_pkg::sum(b,c);
   endtask

endclass

First: Why is this illegal?

Second: What is a better way to do this if I want these let constructs to be useable in various parts of my testbench? In the past I have used macros, but 'let' seems so much cleaner so I want to make it work.

1

There are 1 best solutions below

0
Mikef On

First: Why is this illegal?

It might be a tool bug.

Using import let_pkg::*; works for me on eda playground in multiple simulators based on comments by @dave_59

You can optionally remove the scope resolution operator let_pkg:: when import::* is used.

Example using import

package let_pkg;
   let sum(b,c) = b+c;

  function int pkgFunct(input int a,b);
    return a + b;
  endfunction

endpackage
import let_pkg::*;
class class_that_uses_let;

   int a,b,c;
  
  function new();
    $display("Foo");
  endfunction

  function int myFunc(input int a,b);
    return a + b;
  endfunction
  
   task use_let;
      a = let_pkg::sum(b,c);
   endtask

endclass

Testbench

module tb();

  class_that_uses_let foo;
  
  initial begin
   foo = new;
    $display( foo.myFunc(1,1)        );
    $display( let_pkg::pkgFunct(2,2) );
   end
 
endmodule

Produces

# run -all
# Foo
#           2
#           4
# exit

What is a better way to do this if I want these let constructs to be usable in various parts of my testbench?

Putting the 'let' construct in a package, then importing the package as needed works to build a library.

Small math or logic operations can be done in tasks or functions in a package. Using tasks & functions may be more familiar to Verilog coders than let (important if you are going to share the library).

Yet another way would be to put tasks, functions, let, etc in another class, and have instances of that class where needed, rather than using a package as the library.


Can I import a package into a class?

No
Here is the full explanation by @dave_59
package-contents-into-class-scope