Skip to content

Commit

Permalink
Ensure all lookups that can return hierarchical references go through…
Browse files Browse the repository at this point in the history
… an expression
  • Loading branch information
MikePopoloski committed Dec 21, 2024
1 parent fe0e2c0 commit 84a584f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 50 deletions.
2 changes: 1 addition & 1 deletion include/slang/ast/TimingControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class SLANG_EXPORT BlockEventListControl : public TimingControl {
/// A single block event.
struct Event {
/// The target block.
const Symbol* target = nullptr;
const Expression* target = nullptr;

/// True if this is a `begin` event and false if it's an `end` event.
bool isBegin = false;
Expand Down
10 changes: 3 additions & 7 deletions include/slang/ast/statements/MiscStatements.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@ class SLANG_EXPORT EmptyStatement : public Statement {
class SLANG_EXPORT DisableStatement : public Statement {
public:
/// The target of the disable.
const Symbol& target;

/// True if the target name was a hierarchical name.
bool isHierarchical;
const Expression& target;

DisableStatement(const Symbol& target, bool isHierarchical, SourceRange sourceRange) :
Statement(StatementKind::Disable, sourceRange), target(target),
isHierarchical(isHierarchical) {}
DisableStatement(const Expression& target, SourceRange sourceRange) :
Statement(StatementKind::Disable, sourceRange), target(target) {}

EvalResult evalImpl(EvalContext& context) const;

Expand Down
2 changes: 2 additions & 0 deletions source/ast/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,8 @@ void Expression::findPotentiallyImplicitNets(
ASTContext ctx(*context.scope, LookupLocation::max);
Lookup::name(nameSyntax, ctx, flags, result);

SLANG_ASSERT(!result.flags.has(LookupResultFlags::IsHierarchical));

if (!result.found && !result.hasError())
results.push_back(&nameSyntax.as<IdentifierNameSyntax>());
}
Expand Down
26 changes: 12 additions & 14 deletions source/ast/TimingControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "slang/ast/ASTSerializer.h"
#include "slang/ast/Compilation.h"
#include "slang/ast/Expression.h"
#include "slang/ast/expressions/MiscExpressions.h"
#include "slang/ast/types/Type.h"
#include "slang/diagnostics/ExpressionsDiags.h"
#include "slang/diagnostics/StatementsDiags.h"
Expand Down Expand Up @@ -510,22 +511,19 @@ TimingControl& BlockEventListControl::fromSyntax(const BlockEventExpressionSynta
SmallVector<Event, 4> events;

auto addEvent = [&](const PrimaryBlockEventExpressionSyntax& evSyntax) {
LookupResult result;
Lookup::name(*evSyntax.name, context,
LookupFlags::ForceHierarchical | LookupFlags::NoSelectors, result);
result.reportDiags(context);

const Symbol* symbol = result.found;
if (!symbol)
return false;
auto& expr = ArbitrarySymbolExpression::fromSyntax(comp, *evSyntax.name, context);
if (auto symbol = expr.getSymbolReference()) {
if (symbol->kind != SymbolKind::StatementBlock &&
symbol->kind != SymbolKind::Subroutine) {
context.addDiag(diag::InvalidBlockEventTarget, evSyntax.name->sourceRange());
return false;
}

if (symbol->kind != SymbolKind::StatementBlock && symbol->kind != SymbolKind::Subroutine) {
context.addDiag(diag::InvalidBlockEventTarget, evSyntax.name->sourceRange());
return false;
events.push_back({&expr, evSyntax.keyword.kind == TokenKind::BeginKeyword});
return true;
}

events.push_back({nullptr, evSyntax.keyword.kind == TokenKind::BeginKeyword});
return true;
return false;
};

const BlockEventExpressionSyntax* curr = &syntax;
Expand All @@ -548,7 +546,7 @@ void BlockEventListControl::serializeTo(ASTSerializer& serializer) const {
for (auto& event : events) {
SLANG_ASSERT(event.target);
serializer.startObject();
serializer.writeLink("target", *event.target);
serializer.write("target", *event.target);
serializer.write("isBegin", event.isBegin);
serializer.endObject();
}
Expand Down
28 changes: 13 additions & 15 deletions source/ast/statements/MiscStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,41 @@ using namespace parsing;

using ER = Statement::EvalResult;

Statement& DisableStatement::fromSyntax(Compilation& compilation,
const DisableStatementSyntax& syntax,
Statement& DisableStatement::fromSyntax(Compilation& comp, const DisableStatementSyntax& syntax,
const ASTContext& context) {
LookupResult result;
Lookup::name(*syntax.name, context, LookupFlags::ForceHierarchical | LookupFlags::NoSelectors,
result);
result.reportDiags(context);
auto& targetExpr = ArbitrarySymbolExpression::fromSyntax(comp, *syntax.name, context);
if (targetExpr.bad())
return badStmt(comp, nullptr);

const Symbol* symbol = result.found;
if (!symbol)
return badStmt(compilation, nullptr);
auto symbol = targetExpr.getSymbolReference();
SLANG_ASSERT(symbol);

if (symbol->kind != SymbolKind::StatementBlock &&
(symbol->kind != SymbolKind::Subroutine ||
symbol->as<SubroutineSymbol>().subroutineKind != SubroutineKind::Task)) {
context.addDiag(diag::InvalidDisableTarget, syntax.name->sourceRange());
return badStmt(compilation, nullptr);
return badStmt(comp, nullptr);
}

return *compilation.emplace<DisableStatement>(
*symbol, result.flags.has(LookupResultFlags::IsHierarchical), syntax.sourceRange());
return *comp.emplace<DisableStatement>(targetExpr, syntax.sourceRange());
}

ER DisableStatement::evalImpl(EvalContext& context) const {
// Hierarchical names are disallowed in constant expressions and constant functions
auto& ase = target.as<ArbitrarySymbolExpression>();
const bool isHierarchical = ase.hierRef.target != nullptr;
if (isHierarchical) {
context.addDiag(diag::ConstEvalHierarchicalName, sourceRange) << target.name;
context.addDiag(diag::ConstEvalHierarchicalName, sourceRange) << ase.symbol->name;
return ER::Fail;
}

SLANG_ASSERT(!context.getDisableTarget());
context.setDisableTarget(&target, sourceRange);
context.setDisableTarget(ase.symbol, sourceRange);
return ER::Disable;
}

void DisableStatement::serializeTo(ASTSerializer& serializer) const {
serializer.writeLink("target", target);
serializer.write("target", target);
}

ER VariableDeclStatement::evalImpl(EvalContext& context) const {
Expand Down
13 changes: 6 additions & 7 deletions source/ast/symbols/InstanceSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,15 +1954,14 @@ void CheckerInstanceSymbol::fromSyntax(const CheckerInstantiationSyntax& syntax,
return;
}

LookupResult lookupResult;
Lookup::name(*syntax.type, context, LookupFlags::AllowDeclaredAfter | LookupFlags::NoSelectors,
lookupResult);

lookupResult.reportDiags(context);
if (!lookupResult.found)
auto& comp = context.getCompilation();
auto& typeExpr = ArbitrarySymbolExpression::fromSyntax(comp, *syntax.type, context);
if (typeExpr.bad())
return;

auto symbol = lookupResult.found;
auto symbol = typeExpr.getSymbolReference();
SLANG_ASSERT(symbol);

if (symbol->kind != SymbolKind::Checker) {
if (symbol->kind == SymbolKind::ClassType) {
context.addDiag(diag::CheckerClassBadInstantiation, syntax.sourceRange())
Expand Down
10 changes: 4 additions & 6 deletions source/ast/types/NetType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "slang/ast/ASTSerializer.h"
#include "slang/ast/Compilation.h"
#include "slang/ast/Scope.h"
#include "slang/ast/expressions/MiscExpressions.h"
#include "slang/ast/symbols/SubroutineSymbols.h"
#include "slang/ast/symbols/VariableSymbols.h"
#include "slang/ast/types/Type.h"
Expand Down Expand Up @@ -109,12 +110,9 @@ const SubroutineSymbol* NetType::getResolutionFunction() const {
auto& declSyntax = syntax->as<NetTypeDeclarationSyntax>();
if (declSyntax.withFunction) {
ASTContext context(*scope, LookupLocation::after(*this));
LookupResult result;
Lookup::name(*declSyntax.withFunction->name, context,
LookupFlags::ForceHierarchical | LookupFlags::NoSelectors, result);
result.reportDiags(context);

auto symbol = result.found;
auto& expr = ArbitrarySymbolExpression::fromSyntax(context.getCompilation(),
*declSyntax.withFunction->name, context);
auto symbol = expr.getSymbolReference();
if (symbol) {
SourceRange range = declSyntax.withFunction->name->sourceRange();
if (symbol->kind != SymbolKind::Subroutine) {
Expand Down

0 comments on commit 84a584f

Please sign in to comment.