Skip to content

Commit 63cc0d8

Browse files
committed
SystemVerilog: non-wildcard imports
This adds 1800 2017 26.3 non-wildcard import declarations.
1 parent 605626e commit 63cc0d8

File tree

9 files changed

+78
-6
lines changed

9 files changed

+78
-6
lines changed
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
KNOWNBUG
1+
CORE
22
import2.sv
33
--bound 0
44
^EXIT=0$
55
^SIGNAL=0$
66
--
77
--
8-
The identifier is not found.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
import3.sv
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
--
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package my_pkg;
2+
typedef int my_type;
3+
endpackage
4+
5+
module main;
6+
import my_pkg::my_type;
7+
assert final($typename(my_type) == "int");
8+
endmodule

src/verilog/parser.y

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,8 +1488,10 @@ package_import_item:
14881488
package_identifier "::" identifier
14891489
{ init($$, ID_verilog_import_item);
14901490
auto package_base_name = stack_expr($1).get(ID_base_name);
1491+
auto identifier_base_name = stack_expr($3).get(ID_base_name);
14911492
stack_expr($$).set(ID_verilog_package, package_base_name);
1492-
stack_expr($$).set(ID_base_name, stack_expr($3).id()); }
1493+
stack_expr($$).set(ID_base_name, identifier_base_name);
1494+
PARSER.scopes.import(package_base_name, identifier_base_name); }
14931495
| package_identifier "::" "*"
14941496
{ init($$, ID_verilog_import_item);
14951497
auto package_base_name = stack_expr($1).get(ID_base_name);
@@ -2010,6 +2012,7 @@ list_of_param_assignments:
20102012
param_assignment: param_identifier '=' constant_param_expression
20112013
{ init($$, ID_parameter);
20122014
auto base_name = stack_expr($1).get(ID_base_name);
2015+
PARSER.scopes.add_name(base_name, "", verilog_scopet::PARAMETER);
20132016
stack_expr($$).set(ID_base_name, base_name);
20142017
addswap($$, ID_value, $3); }
20152018
;

src/verilog/scanner.l

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ YYSTYPE make_identifier(irep_idt base_name, exprt &dest)
7777
else
7878
{
7979
dest.set(ID_scope, scope_ptr->prefix);
80+
if(!scope_ptr->import.empty())
81+
dest.set("import", scope_ptr->import);
8082
return scope_ptr->identifier_token();
8183
}
8284
}
8385

8486
#define IDENTIFIER(text) \
8587
{ newstack(yyveriloglval); \
86-
auto base_name = text; /* hashing here */ \
88+
irep_idt base_name = text; /* hashing here */ \
8789
return make_identifier(base_name, stack_expr(yyveriloglval)); \
8890
}
8991
#define KEYWORD(s, x) \

src/verilog/verilog_elaborate_type.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,21 @@ typet verilog_typecheck_exprt::elaborate_type(const typet &src)
343343
{
344344
// Look it up!
345345
auto base_name = to_verilog_typedef_type(src).base_name();
346-
const auto *symbol_ptr = resolve(base_name);
346+
const symbolt *symbol_ptr;
347+
auto import = src.get("import");
348+
if(import != irep_idt{})
349+
{
350+
auto full_identifier = "Verilog::package::" + id2string(import);
351+
352+
if(ns.lookup(full_identifier, symbol_ptr))
353+
{
354+
DATA_INVARIANT(false, "failed to find imported typedef identifier");
355+
}
356+
}
357+
else
358+
{
359+
symbol_ptr = resolve(base_name);
360+
}
347361

348362
if(symbol_ptr == nullptr)
349363
throw errort().with_location(source_location)

src/verilog/verilog_scope.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ unsigned verilog_scopet::identifier_token() const
5252
case verilog_scopet::TASK: return TOK_NON_TYPE_IDENTIFIER;
5353
case verilog_scopet::FUNCTION: return TOK_NON_TYPE_IDENTIFIER;
5454
case verilog_scopet::TYPEDEF: return TOK_TYPE_IDENTIFIER;
55+
case verilog_scopet::PARAMETER: return TOK_NON_TYPE_IDENTIFIER;
5556
case verilog_scopet::PROPERTY: return TOK_NON_TYPE_IDENTIFIER;
5657
case verilog_scopet::SEQUENCE: return TOK_NON_TYPE_IDENTIFIER;
5758
case verilog_scopet::OTHER: return TOK_NON_TYPE_IDENTIFIER;
@@ -61,6 +62,25 @@ unsigned verilog_scopet::identifier_token() const
6162
UNREACHABLE;
6263
}
6364

65+
void verilog_scopest::import(irep_idt package, irep_idt base_name)
66+
{
67+
// find the package in the global scope
68+
auto package_it = top_scope.scope_map.find(package);
69+
if(package_it == top_scope.scope_map.end())
70+
return;
71+
72+
// find the identifier in the package
73+
auto name_it = package_it->second.scope_map.find(base_name);
74+
if(name_it != package_it->second.scope_map.end())
75+
{
76+
auto &scope = add_name(base_name, "", name_it->second.kind);
77+
scope.import = name_it->second.identifier();
78+
}
79+
else
80+
{
81+
}
82+
}
83+
6484
void verilog_scopest::enter_package_scope(irep_idt base_name)
6585
{
6686
// look in the global scope

src/verilog/verilog_scope.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct verilog_scopet
2929
FUNCTION,
3030
BLOCK,
3131
TYPEDEF,
32+
PARAMETER,
3233
PROPERTY,
3334
SEQUENCE,
3435
OTHER
@@ -54,6 +55,7 @@ struct verilog_scopet
5455
irep_idt __base_name;
5556
std::string prefix;
5657
kindt kind;
58+
irep_idt import;
5759

5860
irep_idt identifier() const
5961
{
@@ -132,6 +134,8 @@ class verilog_scopest
132134
scope_stack.pop_back();
133135
}
134136

137+
void import(irep_idt package, irep_idt base_name);
138+
135139
// Look up an identifier, starting from the current scope,
136140
// going upwards until found. Returns nullptr when not found.
137141
const scopet *lookup(irep_idt base_name) const;

src/verilog/verilog_typecheck_expr.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,8 +1383,23 @@ exprt verilog_typecheck_exprt::convert_verilog_identifier(
13831383
verilog_identifier_exprt expr,
13841384
const std::optional<typet> &implicit_net_type)
13851385
{
1386+
const symbolt *symbol;
13861387
auto base_name = expr.base_name();
1387-
auto symbol = resolve(base_name);
1388+
auto import = expr.get("import");
1389+
1390+
if(import != irep_idt{})
1391+
{
1392+
auto full_identifier = "Verilog::package::" + id2string(import);
1393+
1394+
if(ns.lookup(full_identifier, symbol))
1395+
{
1396+
DATA_INVARIANT(false, "failed to find imported identifier");
1397+
}
1398+
}
1399+
else
1400+
{
1401+
symbol = resolve(base_name);
1402+
}
13881403

13891404
if(symbol != nullptr)
13901405
{

0 commit comments

Comments
 (0)