Skip to content

Commit

Permalink
SystemVerilog: class identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
kroening committed Feb 3, 2025
1 parent e042948 commit e9dbbcd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions regression/verilog/class/class1.sv
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class myClass;
endclass

module main;
myClass c = null;
endmodule
1 change: 1 addition & 0 deletions src/hw_cbmc_irep_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ IREP_ID_ONE(verilog_empty_item)
IREP_ID_ONE(verilog_import_item)
IREP_ID_ONE(verilog_interface)
IREP_ID_ONE(verilog_class)
IREP_ID_ONE(verilog_class_type)
IREP_ID_ONE(verilog_module)
IREP_ID_ONE(verilog_package)
IREP_ID_ONE(verilog_package_import)
Expand Down
4 changes: 4 additions & 0 deletions src/verilog/expr2verilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2052,10 +2052,14 @@ std::string expr2verilogt::convert(const typet &type)
}
else if(type.id() == ID_verilog_chandle)
return "chandle";
else if(type.id() == ID_verilog_class_type)
return "class";
else if(type.id() == ID_verilog_event)
return "event";
else if(type.id() == ID_verilog_genvar)
return "genvar";
else if(type.id() == ID_verilog_new)
return "new";
else if(type.id()==ID_integer)
return "integer";
else if(type.id()==ID_verilog_shortreal)
Expand Down
22 changes: 17 additions & 5 deletions src/verilog/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ int yyverilogerror(const char *error)
/* Others */
%token TOK_ENDOFFILE
%token TOK_NON_TYPE_IDENTIFIER
%token TOK_CLASS_IDENTIFIER
%token TOK_PACKAGE_IDENTIFIER
%token TOK_TYPE_IDENTIFIER
%token TOK_NUMBER // number, any base
Expand Down Expand Up @@ -819,12 +820,13 @@ checker_port_direction_opt:
;

class_declaration:
TOK_CLASS class_identifier
TOK_CLASS any_identifier
';'
{
init($$, ID_verilog_class);
stack_expr($$).set(ID_base_name, stack_expr($2).id());
push_scope(stack_expr($2).id(), "::", verilog_scopet::CLASS);
auto base_name = stack_expr($2).get(ID_base_name);
stack_expr($$).set(ID_base_name, base_name);
push_scope(base_name, "::", verilog_scopet::CLASS);
}
class_item_brace
TOK_ENDCLASS
Expand Down Expand Up @@ -1553,7 +1555,7 @@ data_type:
{ mto($1, $2);
add_as_subtype(stack_type($3), stack_type($2));
$$ = $3; }
// | class_type
| class_type
| TOK_EVENT
{ init($$, ID_verilog_event); }
/*
Expand Down Expand Up @@ -2033,6 +2035,9 @@ variable_decl_assignment:
addswap($$, ID_type, $2);
addswap($$, ID_value, $4); }
| variable_identifier variable_dimension_brace '=' class_new
{ $$ = $1; stack_expr($$).id(ID_declarator);
addswap($$, ID_type, $2);
addswap($$, ID_value, $4); }
;

class_new:
Expand Down Expand Up @@ -4383,7 +4388,14 @@ non_type_identifier: TOK_NON_TYPE_IDENTIFIER

block_identifier: TOK_NON_TYPE_IDENTIFIER;

class_identifier: TOK_NON_TYPE_IDENTIFIER;
class_identifier: TOK_CLASS_IDENTIFIER
{
init($$, ID_verilog_class_type);
auto base_name = stack_expr($1).id();
stack_expr($$).set(ID_base_name, base_name);
stack_expr($$).set(ID_identifier, PARSER.scopes.current_scope().prefix+id2string(base_name));
}
;

constraint_identifier: TOK_NON_TYPE_IDENTIFIER;

Expand Down
4 changes: 4 additions & 0 deletions src/verilog/verilog_elaborate_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ typet verilog_typecheck_exprt::elaborate_type(const typet &src)
{
return src;
}
else if(src.id() == ID_verilog_class_type)
{
return src;
}
else if(src.id() == ID_verilog_package_scope)
{
// package::typedef
Expand Down
2 changes: 1 addition & 1 deletion src/verilog/verilog_scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ unsigned verilog_scopest::identifier_token(irep_idt base_name) const
case verilog_scopet::FILE: return TOK_NON_TYPE_IDENTIFIER;
case verilog_scopet::PACKAGE: return TOK_PACKAGE_IDENTIFIER;
case verilog_scopet::MODULE: return TOK_NON_TYPE_IDENTIFIER;
case verilog_scopet::CLASS: return TOK_NON_TYPE_IDENTIFIER;
case verilog_scopet::CLASS: return TOK_CLASS_IDENTIFIER;
case verilog_scopet::BLOCK: return TOK_NON_TYPE_IDENTIFIER;
case verilog_scopet::ENUM_NAME: return TOK_NON_TYPE_IDENTIFIER;
case verilog_scopet::TASK: return TOK_NON_TYPE_IDENTIFIER;
Expand Down
6 changes: 6 additions & 0 deletions src/verilog/verilog_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2062,6 +2062,7 @@ void verilog_typecheck_exprt::implicit_typecast(
{
if(
dest_type.id() == ID_verilog_chandle ||
dest_type.id() == ID_verilog_class_type ||
dest_type.id() == ID_verilog_event)
{
if(expr.id() == ID_constant)
Expand Down Expand Up @@ -2594,6 +2595,11 @@ exprt verilog_typecheck_exprt::convert_unary_expr(unary_exprt expr)
convert_expr(expr.op());
expr.type() = expr.op().type();
}
else if(expr.id() == ID_verilog_new)
{
// The type of these expressions is determined by their context.
expr.type() = typet(ID_verilog_new);
}
else
{
throw errort() << "no conversion for unary expression " << expr.id();
Expand Down

0 comments on commit e9dbbcd

Please sign in to comment.