Skip to content

Commit

Permalink
Rework pattern variable creation to fix various issues with scoping a…
Browse files Browse the repository at this point in the history
…nd lookup
  • Loading branch information
MikePopoloski committed Dec 20, 2024
1 parent a864e10 commit be0428d
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 110 deletions.
33 changes: 24 additions & 9 deletions include/slang/ast/Patterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,17 @@ class SLANG_EXPORT Pattern {
/// Returns true if the pattern had an error and is therefore invalid.
bool bad() const { return kind == PatternKind::Invalid; }

using VarMap = SmallMap<std::string_view, const PatternVarSymbol*, 4>;
static bool createPatternVars(const ASTContext& context,
const syntax::PatternSyntax& patternSyntax,
const syntax::ExpressionSyntax& condSyntax,
SmallVector<const PatternVarSymbol*>& results);

static Pattern& bind(const syntax::PatternSyntax& syntax, const Type& targetType,
VarMap& varMap, ASTContext& context);
static bool createPatternVars(const ASTContext& context, const syntax::PatternSyntax& syntax,
const Type& targetType,
SmallVector<const PatternVarSymbol*>& results);

static Pattern& bind(const ASTContext& context, const syntax::PatternSyntax& syntax,
const Type& targetType);

/// Evaluates the pattern under the given evaluation context. Any errors that occur
/// will be stored in the evaluation context instead of issued to the compilation.
Expand Down Expand Up @@ -104,8 +111,10 @@ class SLANG_EXPORT Pattern {
Pattern(PatternKind kind, SourceRange sourceRange) : kind(kind), sourceRange(sourceRange) {}

static Pattern& badPattern(Compilation& compilation, const Pattern* child);
static void createPlaceholderVars(const syntax::PatternSyntax& syntax, VarMap& varMap,
ASTContext& context);

static void createPlaceholderVars(const ASTContext& context,
const syntax::PatternSyntax& syntax,
SmallVector<const PatternVarSymbol*>& results);
};

/// @brief Represents an invalid pattern
Expand Down Expand Up @@ -180,8 +189,8 @@ class SLANG_EXPORT VariablePattern : public Pattern {
VariablePattern(const PatternVarSymbol& variable, SourceRange sourceRange) :
Pattern(PatternKind::Variable, sourceRange), variable(variable) {}

static Pattern& fromSyntax(const syntax::VariablePatternSyntax& syntax, const Type& targetType,
VarMap& varMap, ASTContext& context);
static Pattern& fromSyntax(const syntax::VariablePatternSyntax& syntax,
const ASTContext& context);

ConstantValue evalImpl(EvalContext& context, const ConstantValue& value,
CaseStatementCondition conditionKind) const;
Expand All @@ -203,8 +212,11 @@ class SLANG_EXPORT TaggedPattern : public Pattern {
TaggedPattern(const FieldSymbol& member, const Pattern* valuePattern, SourceRange sourceRange) :
Pattern(PatternKind::Tagged, sourceRange), member(member), valuePattern(valuePattern) {}

static bool createVars(const ASTContext& context, const syntax::TaggedPatternSyntax& syntax,
const Type& targetType, SmallVector<const PatternVarSymbol*>& results);

static Pattern& fromSyntax(const syntax::TaggedPatternSyntax& syntax, const Type& targetType,
VarMap& varMap, ASTContext& context);
const ASTContext& context);

ConstantValue evalImpl(EvalContext& context, const ConstantValue& value,
CaseStatementCondition conditionKind) const;
Expand Down Expand Up @@ -238,8 +250,11 @@ class SLANG_EXPORT StructurePattern : public Pattern {
StructurePattern(std::span<const FieldPattern> patterns, SourceRange sourceRange) :
Pattern(PatternKind::Structure, sourceRange), patterns(patterns) {}

static bool createVars(const ASTContext& context, const syntax::StructurePatternSyntax& syntax,
const Type& targetType, SmallVector<const PatternVarSymbol*>& results);

static Pattern& fromSyntax(const syntax::StructurePatternSyntax& syntax, const Type& targetType,
VarMap& varMap, ASTContext& context);
const ASTContext& context);

ConstantValue evalImpl(EvalContext& context, const ConstantValue& value,
CaseStatementCondition conditionKind) const;
Expand Down
2 changes: 1 addition & 1 deletion include/slang/ast/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class SLANG_EXPORT BlockStatement : public Statement {
const ASTContext& context, StatementContext& stmtCtx,
bool addInitializers = false);

static Statement& makeEmpty(Compilation& compilation);
static BlockStatement& makeEmpty(Compilation& compilation);

static bool isKind(StatementKind kind) { return kind == StatementKind::Block; }

Expand Down
6 changes: 6 additions & 0 deletions include/slang/ast/symbols/BlockSymbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SLANG_EXPORT StatementBlockSymbol : public Symbol, public Scope {
const Statement& getStatement(const ASTContext& context,
Statement::StatementContext& stmtCtx) const;

bool isKnownBad() const { return stmt && stmt->bad(); }

void serializeTo(ASTSerializer&) const {}

static StatementBlockSymbol& fromSyntax(const Scope& scope,
Expand All @@ -37,6 +39,10 @@ class SLANG_EXPORT StatementBlockSymbol : public Symbol, public Scope {
const syntax::ForLoopStatementSyntax& syntax);
static StatementBlockSymbol& fromSyntax(const Scope& scope,
const syntax::ForeachLoopStatementSyntax& syntax);
static StatementBlockSymbol& fromSyntax(const Scope& scope,
const syntax::ConditionalStatementSyntax& syntax);
static StatementBlockSymbol& fromSyntax(const Scope& scope,
const syntax::PatternCaseItemSyntax& syntax);
static StatementBlockSymbol& fromSyntax(const Scope& scope,
const syntax::RandSequenceStatementSyntax& syntax);
static StatementBlockSymbol& fromSyntax(const Scope& scope, const syntax::RsRuleSyntax& syntax);
Expand Down
Loading

0 comments on commit be0428d

Please sign in to comment.