1

I got vcs compile error when adding function in declaration of struct. The IEEE doc does not mention if function in struct is allowed.

I also got vcs compile error when trying to assign a default value to a field. But it is allowed in IEEE-1800-2012 7.2.2.

typedef struct {
        int a = 1; //compile error here
        int b;
        function void func();
                 b = a;
        endfunction
 } a_struct;

So I add the command line and error info as suggested:

vcs -sverilog a.sv

Error-[V2KIIAD] Invalid initialization at declaration
....
Struct or union member field 'a' cannot be initialized at declaration.

Error-[SE] Syntax error
  Following verilog source has syntax error :
  "a.sv", 4: token is 'function'
     function void func();
             ^

And my vcs version is 2013.06-SP1-10

AndresM
  • 1,293
  • 10
  • 19
awill
  • 135
  • 1
  • 4
  • 15
  • 1
    It is a good idea to include the errors your are receiving in the question. – Morgan Dec 19 '14 at 14:34
  • 1
    It would be helpful to know the vcs command you used as well (did you forget the -sverilog flag?) – Unn Dec 19 '14 at 16:07

2 Answers2

1

Functions declared inside structs are not supported as of IEEE Std 1800-2012. Looking over the syntax for structure declaration, a struct_union_member is a data_type_or_void and a function is not data_type.

§ 7.2 Structures

data_type ::= // from A.2.2.1
...
| struct_union [ packed [ signing ] ] {  struct_union_member { struct_union_member } }
{ packed_dimension }
struct_union_member ::=
{ attribute_instance } [random_qualifier] data_type_or_void list_of_variable_decl_assignments ;
data_type_or_void ::= data_type | void
struct_union ::= struct | union [ tagged ]

Expanding data_type from § A.2.2.1 Net and variable types

data_type ::=
integer_vector_type [ signing ] { packed_dimension }
| integer_atom_type [ signing ]
| non_integer_type
| struct_union [ packed [ signing ] ] {  struct_union_member { struct_union_member } }
{ packed_dimension }
| enum [ enum_base_type ] {  enum_name_declaration { ,  enum_name_declaration } }
{ packed_dimension }
| string
| chandle
| virtual [ interface ] interface_identifier [ parameter_value_assignment ] [ .  modport_identifier ]
| [ class_scope | package_scope ] type_identifier { packed_dimension }
| class_type
| event
| ps_covergroup_identifier
| type_reference
Greg
  • 18,111
  • 5
  • 46
  • 68
  • So system-verilog doesn't allow function in struct, which is different from C style. But the default value of field should be allowed. It might be synopsys issue.. – awill Dec 20 '14 at 04:54
0

As we know that structures are static and classes are dynamic data types. So in SV, while we are using class,we can assign values intially or in other conditions. But for structure we can't initialise any variable inside the declaration.