Skip to content

Commit c036dd5

Browse files
authored
Merge pull request #958 from diffblue/class_identifier
SystemVerilog: class identifiers
2 parents 867f995 + e9dbbcd commit c036dd5

File tree

7 files changed

+34
-6
lines changed

7 files changed

+34
-6
lines changed

regression/verilog/class/class1.sv

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ class myClass;
33
endclass
44

55
module main;
6+
myClass c = null;
67
endmodule

src/hw_cbmc_irep_ids.h

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ IREP_ID_ONE(verilog_empty_item)
251251
IREP_ID_ONE(verilog_import_item)
252252
IREP_ID_ONE(verilog_interface)
253253
IREP_ID_ONE(verilog_class)
254+
IREP_ID_ONE(verilog_class_type)
254255
IREP_ID_ONE(verilog_module)
255256
IREP_ID_ONE(verilog_package)
256257
IREP_ID_ONE(verilog_package_import)

src/verilog/expr2verilog.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2052,10 +2052,14 @@ std::string expr2verilogt::convert(const typet &type)
20522052
}
20532053
else if(type.id() == ID_verilog_chandle)
20542054
return "chandle";
2055+
else if(type.id() == ID_verilog_class_type)
2056+
return "class";
20552057
else if(type.id() == ID_verilog_event)
20562058
return "event";
20572059
else if(type.id() == ID_verilog_genvar)
20582060
return "genvar";
2061+
else if(type.id() == ID_verilog_new)
2062+
return "new";
20592063
else if(type.id()==ID_integer)
20602064
return "integer";
20612065
else if(type.id()==ID_verilog_shortreal)

src/verilog/parser.y

+17-5
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ int yyverilogerror(const char *error)
537537
/* Others */
538538
%token TOK_ENDOFFILE
539539
%token TOK_NON_TYPE_IDENTIFIER
540+
%token TOK_CLASS_IDENTIFIER
540541
%token TOK_PACKAGE_IDENTIFIER
541542
%token TOK_TYPE_IDENTIFIER
542543
%token TOK_NUMBER // number, any base
@@ -819,12 +820,13 @@ checker_port_direction_opt:
819820
;
820821

821822
class_declaration:
822-
TOK_CLASS class_identifier
823+
TOK_CLASS any_identifier
823824
';'
824825
{
825826
init($$, ID_verilog_class);
826-
stack_expr($$).set(ID_base_name, stack_expr($2).id());
827-
push_scope(stack_expr($2).id(), "::", verilog_scopet::CLASS);
827+
auto base_name = stack_expr($2).get(ID_base_name);
828+
stack_expr($$).set(ID_base_name, base_name);
829+
push_scope(base_name, "::", verilog_scopet::CLASS);
828830
}
829831
class_item_brace
830832
TOK_ENDCLASS
@@ -1553,7 +1555,7 @@ data_type:
15531555
{ mto($1, $2);
15541556
add_as_subtype(stack_type($3), stack_type($1));
15551557
$$ = $3; }
1556-
// | class_type
1558+
| class_type
15571559
| TOK_EVENT
15581560
{ init($$, ID_verilog_event); }
15591561
/*
@@ -2033,6 +2035,9 @@ variable_decl_assignment:
20332035
addswap($$, ID_type, $2);
20342036
addswap($$, ID_value, $4); }
20352037
| variable_identifier variable_dimension_brace '=' class_new
2038+
{ $$ = $1; stack_expr($$).id(ID_declarator);
2039+
addswap($$, ID_type, $2);
2040+
addswap($$, ID_value, $4); }
20362041
;
20372042

20382043
class_new:
@@ -4383,7 +4388,14 @@ non_type_identifier: TOK_NON_TYPE_IDENTIFIER
43834388

43844389
block_identifier: TOK_NON_TYPE_IDENTIFIER;
43854390

4386-
class_identifier: TOK_NON_TYPE_IDENTIFIER;
4391+
class_identifier: TOK_CLASS_IDENTIFIER
4392+
{
4393+
init($$, ID_verilog_class_type);
4394+
auto base_name = stack_expr($1).id();
4395+
stack_expr($$).set(ID_base_name, base_name);
4396+
stack_expr($$).set(ID_identifier, PARSER.scopes.current_scope().prefix+id2string(base_name));
4397+
}
4398+
;
43874399

43884400
constraint_identifier: TOK_NON_TYPE_IDENTIFIER;
43894401

src/verilog/verilog_elaborate_type.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ typet verilog_typecheck_exprt::elaborate_type(const typet &src)
438438
{
439439
return src;
440440
}
441+
else if(src.id() == ID_verilog_class_type)
442+
{
443+
return src;
444+
}
441445
else if(src.id() == ID_verilog_package_scope)
442446
{
443447
// package::typedef

src/verilog/verilog_scope.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ unsigned verilog_scopest::identifier_token(irep_idt base_name) const
6262
case verilog_scopet::FILE: return TOK_NON_TYPE_IDENTIFIER;
6363
case verilog_scopet::PACKAGE: return TOK_PACKAGE_IDENTIFIER;
6464
case verilog_scopet::MODULE: return TOK_NON_TYPE_IDENTIFIER;
65-
case verilog_scopet::CLASS: return TOK_NON_TYPE_IDENTIFIER;
65+
case verilog_scopet::CLASS: return TOK_CLASS_IDENTIFIER;
6666
case verilog_scopet::BLOCK: return TOK_NON_TYPE_IDENTIFIER;
6767
case verilog_scopet::ENUM_NAME: return TOK_NON_TYPE_IDENTIFIER;
6868
case verilog_scopet::TASK: return TOK_NON_TYPE_IDENTIFIER;

src/verilog/verilog_typecheck_expr.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -2062,6 +2062,7 @@ void verilog_typecheck_exprt::implicit_typecast(
20622062
{
20632063
if(
20642064
dest_type.id() == ID_verilog_chandle ||
2065+
dest_type.id() == ID_verilog_class_type ||
20652066
dest_type.id() == ID_verilog_event)
20662067
{
20672068
if(expr.id() == ID_constant)
@@ -2594,6 +2595,11 @@ exprt verilog_typecheck_exprt::convert_unary_expr(unary_exprt expr)
25942595
convert_expr(expr.op());
25952596
expr.type() = expr.op().type();
25962597
}
2598+
else if(expr.id() == ID_verilog_new)
2599+
{
2600+
// The type of these expressions is determined by their context.
2601+
expr.type() = typet(ID_verilog_new);
2602+
}
25972603
else
25982604
{
25992605
throw errort() << "no conversion for unary expression " << expr.id();

0 commit comments

Comments
 (0)