slang::parsing::Preprocessor class

Preprocessor - Interface between lexer and parser

This class handles the messy interface between various source file lexers, include directives, and macro expansion, and the actual SystemVerilog parser that wants a nice coherent stream of tokens to consume.

Public functions

auto next() -> Token
Gets the next token in the stream, after applying preprocessor rules.
void pushSource(std::string_view source, std::string_view name = "source")
Push a new source file onto the stack.
void predefine(const std::string& definition, std::string_view name = "<api>")
auto undefine(std::string_view name) -> bool
void undefineAll()
Undefines all currently defined macros.
auto isDefined(std::string_view name) -> bool
void setKeywordVersion(KeywordVersion version)
void resetAllDirectives()
void pushDesignElementStack()
void popDesignElementStack()
auto getTimeScale() const -> const std::optional<TimeScale>&
Gets the currently active time scale value, if any has been set by the user.
auto getDefaultNetType() const -> TokenKind
auto getUnconnectedDrive() const -> TokenKind
auto getCurrentKeywordVersion() const -> KeywordVersion
Gets the currently active keyword version in use by the preprocessor.
auto getDefinedMacros() const -> std::vector<const syntax::DefineDirectiveSyntax*>
Gets all macros that have been defined thus far in the preprocessor.

Function documentation

void slang::parsing::Preprocessor::predefine(const std::string& definition, std::string_view name = "<api>")

Predefines the given macro definition. The given definition string is lexed as if it were source text immediately following a `define directive. If any diagnostics are printed for the created text, they will be marked as coming from name.

bool slang::parsing::Preprocessor::undefine(std::string_view name)

Undefines a previously defined macro. If the macro is not defined, or if you pass the name of an intrinsic macro, this call returns false and does not undefine anything.

bool slang::parsing::Preprocessor::isDefined(std::string_view name)

Checks whether the given macro is defined. This does not check built-in directives except for the intrinsic macros (LINE, etc).

void slang::parsing::Preprocessor::setKeywordVersion(KeywordVersion version)

Sets the base keyword version for the current compilation unit. Note that this does not affect the keyword version if the user has explicitly requested a different version via the begin_keywords directive.

void slang::parsing::Preprocessor::resetAllDirectives()

Resets the state of all compiler directives; this is equivalent to encountering the `resetall directive in source. Note that this does not include the state of any macros that have been defined.

void slang::parsing::Preprocessor::pushDesignElementStack()

Increases the preprocessor's view of the depth of parsed design elements, such as modules or interfaces. A parser calls this whenever starting to parse a new design element so that the preprocessor can enforce rules about where directives may appear.

void slang::parsing::Preprocessor::popDesignElementStack()

Decreases the preprocessor's view of the depth of parsed design elements, such as modules or interfaces. A parser calls this whenever finishing parsing a design element so that the preprocessor can enforce rules about where directives may appear.

TokenKind slang::parsing::Preprocessor::getDefaultNetType() const

Gets the default net type to use if none is specified. This is set via the `default_nettype directive. If it is set to "none" by the user, this will return TokenKind::Unknown.

TokenKind slang::parsing::Preprocessor::getUnconnectedDrive() const

Gets the currently active drive strength to apply to unconnected nets, if any has been set by the user. If none is set, this returns TokenKind::Unknown.