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.
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).
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