support using previously declared types/localparams/params in package
authorJeff Wang <jjj11x@gmail.com>
Tue, 7 Apr 2020 04:37:44 +0000 (00:37 -0400)
committerJeff Wang <jeffrey.wang@ll.mit.edu>
Tue, 7 Apr 2020 04:38:15 +0000 (00:38 -0400)
(parameters in systemverilog packages can't actually be overridden, so
allowing parameters in addition to localparams doesn't actually add any
new functionality, but it's useful to be able to use the parameter
keyword also)

frontends/ast/simplify.cc
frontends/verilog/verilog_parser.y
tests/svtypes/typedef_package.sv

index b87af0f8c645b2c7986b329cf1240e9ae8f8635a..bdb48e82fad694a88e71a89929e3a5ff224f762e 100644 (file)
@@ -441,6 +441,29 @@ bool AstNode::simplify(bool const_fold, bool at_zero, bool in_lvalue, int stage,
                }
        }
 
+       // create name resolution entries for all objects with names
+       if (type == AST_PACKAGE) {
+               //add names to package scope
+               for (size_t i = 0; i < children.size(); i++) {
+                       AstNode *node = children[i];
+                       // these nodes appear at the top level in a package and can define names
+                       if (node->type == AST_PARAMETER || node->type == AST_LOCALPARAM || node->type == AST_TYPEDEF) {
+                               current_scope[node->str] = node;
+                       }
+                       if (node->type == AST_ENUM) {
+                               current_scope[node->str] = node;
+                               for (auto enode : node->children) {
+                                       log_assert(enode->type==AST_ENUM_ITEM);
+                                       if (current_scope.count(enode->str) == 0)
+                                               current_scope[enode->str] = enode;
+                                       else
+                                               log_file_error(filename, location.first_line, "enum item %s already exists in package\n", enode->str.c_str());
+                               }
+                       }
+               }
+       }
+
+
        auto backup_current_block = current_block;
        auto backup_current_block_child = current_block_child;
        auto backup_current_top_block = current_top_block;
index 3bffa3986a781988cbcca752e6f2769976645e13..f850daacbaa6964b9c35610277b1980d9597c412 100644 (file)
@@ -521,7 +521,8 @@ package_body:
 
 package_body_stmt:
        typedef_decl |
-       localparam_decl;
+       localparam_decl |
+       param_decl;
 
 interface:
        TOK_INTERFACE {
index 57a78c53a5b4c4291c9c4441e85770c33b2125e0..2d83742c58e592bc5e2af917a20fe0f4bcb0b300 100644 (file)
@@ -1,6 +1,9 @@
 package pkg;
        typedef logic [7:0] uint8_t;
-       typedef enum logic [7:0] {bb=8'hBB} enum8_t;
+       typedef enum logic [7:0] {bb=8'hBB, cc=8'hCC} enum8_t;
+
+       localparam uint8_t PCONST = cc;
+       parameter uint8_t PCONST_COPY = PCONST;
 endpackage
 
 module top;
@@ -8,7 +11,9 @@ module top;
        (* keep *) pkg::uint8_t a = 8'hAA;
        (* keep *) pkg::enum8_t b_enum = pkg::bb;
 
-       always @* assert(a == 8'hAA);
-       always @* assert(b_enum == 8'hBB);
+       always_comb assert(a == 8'hAA);
+       always_comb assert(b_enum == 8'hBB);
+       always_comb assert(pkg::PCONST == pkg::cc);
+       always_comb assert(pkg::PCONST_COPY == pkg::cc);
 
 endmodule