Skip to content

Commit

Permalink
Merge pull request #8069 from random-internet-cat/set-scope
Browse files Browse the repository at this point in the history
Move scope of Scopable into new ScopableAnnotation
  • Loading branch information
chriseth authored Dec 20, 2019
2 parents 2b3a9d7 + 6679f92 commit ece6463
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
8 changes: 4 additions & 4 deletions libsolidity/analysis/NameAndTypeResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ void DeclarationRegistrationHelper::endVisit(FunctionDefinition&)

bool DeclarationRegistrationHelper::visit(TryCatchClause& _tryCatchClause)
{
_tryCatchClause.setScope(m_currentScope);
_tryCatchClause.annotation().scope = m_currentScope;
enterNewSubScope(_tryCatchClause);
return true;
}
Expand Down Expand Up @@ -675,7 +675,7 @@ void DeclarationRegistrationHelper::endVisit(FunctionTypeName&)

bool DeclarationRegistrationHelper::visit(Block& _block)
{
_block.setScope(m_currentScope);
_block.annotation().scope = m_currentScope;
enterNewSubScope(_block);
return true;
}
Expand All @@ -687,7 +687,7 @@ void DeclarationRegistrationHelper::endVisit(Block&)

bool DeclarationRegistrationHelper::visit(ForStatement& _for)
{
_for.setScope(m_currentScope);
_for.annotation().scope = m_currentScope;
enterNewSubScope(_for);
return true;
}
Expand Down Expand Up @@ -761,7 +761,7 @@ void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaratio

registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, inactive, m_errorReporter);

_declaration.setScope(m_currentScope);
_declaration.annotation().scope = m_currentScope;
if (_opensScope)
enterNewSubScope(_declaration);
}
Expand Down
28 changes: 28 additions & 0 deletions libsolidity/ast/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,13 @@ string Scopable::sourceUnitName() const
return sourceUnit().annotation().path;
}

DeclarationAnnotation& Declaration::annotation() const
{
if (!m_annotation)
m_annotation = make_unique<DeclarationAnnotation>();
return dynamic_cast<DeclarationAnnotation&>(*m_annotation);
}

bool VariableDeclaration::isLValue() const
{
// Constant declared variables are Read-Only
Expand Down Expand Up @@ -653,6 +660,27 @@ InlineAssemblyAnnotation& InlineAssembly::annotation() const
return dynamic_cast<InlineAssemblyAnnotation&>(*m_annotation);
}

BlockAnnotation& Block::annotation() const
{
if (!m_annotation)
m_annotation = make_unique<BlockAnnotation>();
return dynamic_cast<BlockAnnotation&>(*m_annotation);
}

TryCatchClauseAnnotation& TryCatchClause::annotation() const
{
if (!m_annotation)
m_annotation = make_unique<TryCatchClauseAnnotation>();
return dynamic_cast<TryCatchClauseAnnotation&>(*m_annotation);
}

ForStatementAnnotation& ForStatement::annotation() const
{
if (!m_annotation)
m_annotation = make_unique<ForStatementAnnotation>();
return dynamic_cast<ForStatementAnnotation&>(*m_annotation);
}

ReturnAnnotation& Return::annotation() const
{
if (!m_annotation)
Expand Down
14 changes: 10 additions & 4 deletions libsolidity/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ class Scopable
virtual ~Scopable() = default;
/// @returns the scope this declaration resides in. Can be nullptr if it is the global scope.
/// Available only after name and type resolution step.
ASTNode const* scope() const { return m_scope; }
void setScope(ASTNode const* _scope) { m_scope = _scope; }
ASTNode const* scope() const { return annotation().scope; }

/// @returns the source unit this scopable is present in.
SourceUnit const& sourceUnit() const;
Expand All @@ -172,8 +171,7 @@ class Scopable
/// Can be combined with annotation().canonicalName (if present) to form a globally unique name.
std::string sourceUnitName() const;

protected:
ASTNode const* m_scope = nullptr;
virtual ScopableAnnotation& annotation() const = 0;
};

/**
Expand Down Expand Up @@ -231,6 +229,8 @@ class Declaration: public ASTNode, public Scopable
/// @returns null when it is not accessible as a function.
virtual FunctionTypePointer functionType(bool /*_internal*/) const { return {}; }

DeclarationAnnotation& annotation() const override;

protected:
virtual Visibility defaultVisibility() const { return Visibility::Public; }

Expand Down Expand Up @@ -1169,6 +1169,8 @@ class Block: public Statement, public Scopable

std::vector<ASTPointer<Statement>> const& statements() const { return m_statements; }

BlockAnnotation& annotation() const override;

private:
std::vector<ASTPointer<Statement>> m_statements;
};
Expand Down Expand Up @@ -1248,6 +1250,8 @@ class TryCatchClause: public ASTNode, public Scopable
ParameterList const* parameters() const { return m_parameters.get(); }
Block const& block() const { return *m_block; }

TryCatchClauseAnnotation& annotation() const override;

private:
ASTPointer<ASTString> m_errorName;
ASTPointer<ParameterList> m_parameters;
Expand Down Expand Up @@ -1357,6 +1361,8 @@ class ForStatement: public BreakableStatement, public Scopable
ExpressionStatement const* loopExpression() const { return m_loopExpression.get(); }
Statement const& body() const { return *m_body; }

ForStatementAnnotation& annotation() const override;

private:
/// For statement's initialization expression. for (XXX; ; ). Can be empty
ASTPointer<Statement> m_initExpression;
Expand Down
31 changes: 27 additions & 4 deletions libsolidity/ast/ASTAnnotations.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,26 @@ struct SourceUnitAnnotation: ASTAnnotation
std::set<ExperimentalFeature> experimentalFeatures;
};

struct ImportAnnotation: ASTAnnotation
struct ScopableAnnotation
{
/// The scope this declaration resides in. Can be nullptr if it is the global scope.
/// Available only after name and type resolution step.
ASTNode const* scope = nullptr;
};

struct DeclarationAnnotation: ASTAnnotation, ScopableAnnotation
{
};

struct ImportAnnotation: DeclarationAnnotation
{
/// The absolute path of the source unit to import.
std::string absolutePath;
/// The actual source unit.
SourceUnit const* sourceUnit = nullptr;
};

struct TypeDeclarationAnnotation: ASTAnnotation
struct TypeDeclarationAnnotation: DeclarationAnnotation
{
/// The name of this type, prefixed by proper namespaces if globally accessible.
std::string canonicalName;
Expand All @@ -104,7 +115,7 @@ struct ContractDefinitionAnnotation: TypeDeclarationAnnotation, DocumentedAnnota
std::map<FunctionDefinition const*, ASTNode const*> baseConstructorArguments;
};

struct CallableDeclarationAnnotation: ASTAnnotation
struct CallableDeclarationAnnotation: DeclarationAnnotation
{
/// The set of functions/modifiers/events this callable overrides.
std::set<CallableDeclaration const*> baseFunctions;
Expand All @@ -124,7 +135,7 @@ struct ModifierDefinitionAnnotation: CallableDeclarationAnnotation, DocumentedAn
{
};

struct VariableDeclarationAnnotation: ASTAnnotation
struct VariableDeclarationAnnotation: DeclarationAnnotation
{
/// Type of variable (type of identifier referencing this variable).
TypePointer type = nullptr;
Expand Down Expand Up @@ -152,6 +163,18 @@ struct InlineAssemblyAnnotation: StatementAnnotation
std::shared_ptr<yul::AsmAnalysisInfo> analysisInfo;
};

struct BlockAnnotation: StatementAnnotation, ScopableAnnotation
{
};

struct TryCatchClauseAnnotation: ASTAnnotation, ScopableAnnotation
{
};

struct ForStatementAnnotation: StatementAnnotation, ScopableAnnotation
{
};

struct ReturnAnnotation: StatementAnnotation
{
/// Reference to the return parameters of the function.
Expand Down

0 comments on commit ece6463

Please sign in to comment.