Enum of same names

Hi,
Let us say I have same enum defined in two pkgs but under different files of different directories
Like

blk1/sv/file1_pkg.sv (Inside it say I have “typedef enum {A=0, B=1} ab_e;”
blk2/sv/file1_pkg.sv (Inside it say I have “typedef enum {A=0, B=1} ab_e;”

These blocks when running at block level (within blk1 or blk2 scope) it works fine.
But if I include both files when running at top level there is enum conflict when the enum gets used at top level files. How to resolve this efficiently?
Is there any way to use the enums of different name in file1_pkg.sv and assign it to the old name to be backward compatible with say A, B enum values so that I can avoid changing the A and B reference inside all files in blk1 or blk2.

Thanks

In reply to raj1150:

If you have the same enumeration in two packages, you can prevent conflicts by:

  • Create unique enumerations by pre-pending the package name (or other identifier) to each enumeration element.
  • Defining the enumeration once in a package, then importing that package into the two block packages.
  • Use the package scope when referencing the enum to select which version you want to use. Note that this prevents you from importing all symbols from each package using the ‘*’ import.

In reply to cgales:

Thanks for the response cgales.

The option1 requires replacement of changing the enum name in the files used within blk1/blk2
The option2 assumes the decoding of the enum is same for both blk1 and blk2 whereas let us say only the enum name is same and not decoding. This will not work right?
The option3 again requires changing the files where enum is used and prefix them with pkg name and scope resolution operator.

What I would ideally prefer is, say I want to go with option1 but dont want to change the enum names in files where it is getting referenced. so I will add something like below in blk1/sv/file1_pkg.sv → typedef enum {A_blk1=0, B_blk1=1 } ab_e;

I would like the files in blk1 where “A” is getting referenced should automatically be interpreted as “A_blk1”. Is that possible?

something like const A = A_blk1; in file1_pkg.sv works?

In reply to raj1150:

You don’t need to change any code in blk1 or blk2 as each package has a unique namespace, so there will be no confusion. It is only when you need to use both blk1 and blk2 in the same namespace that you will run into errors.

Option 2 is the easiest since there is no code changes other than removing the enum definition from each package and importing the enum_package.


package enum_pkg;
  typedef enum {A=0, B=1} ab_e;
endpackage

package blk1_pkg;
  import enum_pkg::*;
endpackage

package blk2_pkg;
  import enum_pkg::*;
endpackage