Verilog: parse packages before the files that import them - #2082
Draft
kroening wants to merge 2 commits into
Draft
Verilog: parse packages before the files that import them#2082kroening wants to merge 2 commits into
kroening wants to merge 2 commits into
Conversation
kroening
marked this pull request as draft
August 11, 2026 18:28
IEEE 1800-2017 26.3 requires that the compilation of a package precedes the compilation of the scopes in which the package is imported, but the standard does not prescribe an order for the input files. We hence determine the order in which the input files are parsed and elaborated from the packages that they declare and reference, using a scanner-only pre-pass over the preprocessed input. The parse trees are still returned in the order in which the files were given, so that the choice of top-level modules is unaffected. Furthermore, the operand of an import can only ever be a package name, and hence the grammar now also accepts an identifier that is not classified as a package name. That yields a diagnostic that names the offending package instead of a syntax error about a token class.
kroening
force-pushed
the
kroening/frontend-issue5
branch
from
August 11, 2026 18:33
dbb8dc6 to
78ef0bd
Compare
Library files are parsed after the given files, and hence are appended to the list of parse trees once the given files have been parsed. Extend the elaboration order accordingly, as it would otherwise not cover them, and add an invariant that the elaboration order covers all parse trees.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
import some_pkg::*;was a syntax error unless the file declaringsome_pkghappened to be given earlier on the command line, and the diagnostic named a
token class rather than the package.
IEEE 1800-2017 26.3 requires that the compilation of a package precedes the
compilation of the scopes in which the package is imported, but the standard
does not prescribe an order for the input files, and command files produced by
other tools do not generally list a package ahead of its importers.
Example, which previously failed in one of the two orders:
Ordering
Identifiers are classified by the scanner, which consults the scope table, and
hence the members of an imported package must be in scope while the importing
file is scanned. Accepting the import and resolving it later is therefore not
sufficient:
some_typeabove would still not be classified as a type name.Instead, a scanner-only pre-pass over the preprocessed input records the
packages that each file declares and the packages that it references. A
topological sort then gives the order in which the files are parsed and
elaborated. Files without such a dependency retain their relative order, and
the parse trees are returned in the order in which the files were given, so
the choice of top-level modules is unaffected. Transitive chains, i.e., a
package that itself imports another package, are handled. A dependency cycle,
which is illegal, is broken arbitrarily, and the parser then reports the
offending reference.
Diagnostic
The operand of an import can only ever be a package name. The grammar now also
accepts an identifier that the scanner did not classify as a package name, and
reports
or, when the name denotes something other than a package,
There are no new grammar conflicts.
Tests
New tests in
regression/verilog/packages:two_files2,two_files3(awildcard import and an explicit import, with the package given after the
importing file),
three_files1(a chain of two packages, files given inreverse dependency order), and
import10,import11for the diagnostic on apackage that is not declared.