Accessing enum type of data

Hi,

Could anyone please help to enlighten me on which part of the latest LRM that allows the following code of accessing enum type of data (line 1 and 2) ? I am unable to find the section of LRM that allows enum type being accessed like an array.

module top();
  enum bit[31:0] {apple, banana, tomato} fruits;
  initial begin
    fruits[apple] = 1'b1; // --- 1
    furits[tomato] = 1'b0; // --- 2
  end
endmodule

It is not explicitly mentioned in LRM.

Did you notice that enum type has few built-in-methods. (First(), Last(), Next(), Prev(), Num(), Name()).

They are similar to assoc-array. It means enums are built upon assoc-arrays. That inherently supports accessing array with named indexes.

I don’t understand what is being asked. You can certainly declare an associative array indexed by an eunum type.

module top;
 typedef enum {apple, banana, tomato} fruit_e;
 bit fruits[fruit_e]; // associate array of a bit.
  initial begin
    fruits[apple] = 1'b1; // --- 1
    furits[tomato] = 1'b0; // --- 2
  end
endmodule

In reply to dave_59:

Hi Dave,

Pardon me for having vague questions. No, I am not asking about declaring an associative array indexed by an enum. I want to understand whether SV LRM do mentioned that the following code of accessing and assigning enum variables are allowed or not. Do take note that the code is not using typedef. It is declaring enum fruits and directly assigning it as shown in the line (1) and line (2).

module top();
  enum bit[31:0] {apple, banana, tomato} fruits;
  initial begin
    fruits[apple] = 1'b1; // --- 1
    furits[tomato] = 1'b0; // --- 2
  end
endmodule.

In reply to kolopipo:

OK, you are asking if the above code is legal(after fixing typos). My answer is unfortunately, yes.

An enum variable is an integrally packed type, and you can always select a bit or range of a packet type variable. And the type of a select of a packed type is always a simple integral type, even if you select the entire enumerated value. In your example, you are selecting a bit of a enum variable with a constant that just happens to be an enum label of the same variable. The code is interpreted as

fruits[32'd0] = 1'b1;
fruits[32'd2] = 1'b0;

At the end of your initial block, fruits will have the value banana.

So the following is legal syntax, even though it puts an out-of-range value into an enum variable without a cast, and that is very unfortunately legal.

fruits[31:0] = 6; // legal
fruits = 6; // illegal without a cast

In reply to dave_59:

Thanks Dave for the information.