How to set enum literal from command line

I got this trick problem.


string type_name = get_enum_type_name();

function string get_enum_type_name();
// this function will return a string containing the name of a enum type, say "enum_t"
endfunction

Say I already got the name of a user defined enum type as “enum_t”, stored in a string.
Now I want to create another variable, call it my_enum, with the type “enum_t”. But the type name is stored in string, how can I use it?
I want to have something like


var type( string2type(type_name) ) my_enum;

In which the string2type() function returns the type object. But how should I implement this?

In reply to Changyu:

SystemVerilog is a statically typed and compiled language. You cannot create types or variables from they dynamic content of a string.

You should read about the XY problem and try to explain the problem you are trying to solve.

In reply to dave_59:

Thanks Dave, I got what you mean.
Yes the question indeed is a “Y” part of a solution, so let me try to explain the original “X” problem.

In UVM command line, you can set enumeration value by “+uvm_set_config_int”, like below.


// trivial code omitted
typedef enum{MIN, LOW, MEDIUM, HIGH, MAX} level_t;
rand level_t traffic_level;
// then in command line, set traffic level as you wish
+uvm_set_config_int=uvm_test_top,traffic_level,4

My goal is to make it intuitive, when you set a enumeration type, you can use its literal name rather than a number. So in this case user won’t need to look up the enumeration definition to understand what the number stands for.
And here is my plan:

  1. instead of setting integer value, let’s set string value on command line, with the enum type explicitly typed out
  2. interpret the special strings, extract the component, field and target variable’s enum type and literal value
  3. use uvm_enum_wrapper to get the integer value from literal name, and then set it

So far I was able to achieve the first two steps, and stuck in step3, which was my previous “Y” problem.
And the code is something like this:


// in command line
+uvm_set_config_string=uvm_test_top,traffic_level,level_t::MAX

// a intermediate function to convert string to enum
function void string_to_enum();
    string comp, field, enum_type, value;
    // use UVM command line processor to get the special string and store each part
    // after processing the string, I get:
    // comp      = "uvm_test_top"
    // field     = "traffic_level"
    // enum_type = "level_t"
    // value     = "MAX"
endfunction : string_to_enum

Now I get everything I needed, but cannot use uvm_enum_wrapper#(type T)::from_name to convert the string to a integer, because I was unable to convert the string to a type as I originally described in the post.
Having said so, I suddenly realize my plan could be impossible because as you said, statically typed language cannot create variable on a dynamic string.
However there might be a way of doing this.