Merge pull request #8069 from random-internet-cat/set-scope

Move scope of Scopable into new ScopableAnnotation
This commit is contained in:
chriseth 2019-12-20 08:22:36 +01:00 committed by GitHub
commit ece6463f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 12 deletions

View File

@ -639,7 +639,7 @@ void DeclarationRegistrationHelper::endVisit(FunctionDefinition&)
bool DeclarationRegistrationHelper::visit(TryCatchClause& _tryCatchClause) bool DeclarationRegistrationHelper::visit(TryCatchClause& _tryCatchClause)
{ {
_tryCatchClause.setScope(m_currentScope); _tryCatchClause.annotation().scope = m_currentScope;
enterNewSubScope(_tryCatchClause); enterNewSubScope(_tryCatchClause);
return true; return true;
} }
@ -675,7 +675,7 @@ void DeclarationRegistrationHelper::endVisit(FunctionTypeName&)
bool DeclarationRegistrationHelper::visit(Block& _block) bool DeclarationRegistrationHelper::visit(Block& _block)
{ {
_block.setScope(m_currentScope); _block.annotation().scope = m_currentScope;
enterNewSubScope(_block); enterNewSubScope(_block);
return true; return true;
} }
@ -687,7 +687,7 @@ void DeclarationRegistrationHelper::endVisit(Block&)
bool DeclarationRegistrationHelper::visit(ForStatement& _for) bool DeclarationRegistrationHelper::visit(ForStatement& _for)
{ {
_for.setScope(m_currentScope); _for.annotation().scope = m_currentScope;
enterNewSubScope(_for); enterNewSubScope(_for);
return true; return true;
} }
@ -761,7 +761,7 @@ void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaratio
registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, inactive, m_errorReporter); registerDeclaration(*m_scopes[m_currentScope], _declaration, nullptr, nullptr, warnAboutShadowing, inactive, m_errorReporter);
_declaration.setScope(m_currentScope); _declaration.annotation().scope = m_currentScope;
if (_opensScope) if (_opensScope)
enterNewSubScope(_declaration); enterNewSubScope(_declaration);
} }

View File

@ -449,6 +449,13 @@ string Scopable::sourceUnitName() const
return sourceUnit().annotation().path; 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 bool VariableDeclaration::isLValue() const
{ {
// Constant declared variables are Read-Only // Constant declared variables are Read-Only
@ -653,6 +660,27 @@ InlineAssemblyAnnotation& InlineAssembly::annotation() const
return dynamic_cast<InlineAssemblyAnnotation&>(*m_annotation); 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 ReturnAnnotation& Return::annotation() const
{ {
if (!m_annotation) if (!m_annotation)

View File

@ -159,8 +159,7 @@ public:
virtual ~Scopable() = default; virtual ~Scopable() = default;
/// @returns the scope this declaration resides in. Can be nullptr if it is the global scope. /// @returns the scope this declaration resides in. Can be nullptr if it is the global scope.
/// Available only after name and type resolution step. /// Available only after name and type resolution step.
ASTNode const* scope() const { return m_scope; } ASTNode const* scope() const { return annotation().scope; }
void setScope(ASTNode const* _scope) { m_scope = _scope; }
/// @returns the source unit this scopable is present in. /// @returns the source unit this scopable is present in.
SourceUnit const& sourceUnit() const; SourceUnit const& sourceUnit() const;
@ -172,8 +171,7 @@ public:
/// Can be combined with annotation().canonicalName (if present) to form a globally unique name. /// Can be combined with annotation().canonicalName (if present) to form a globally unique name.
std::string sourceUnitName() const; std::string sourceUnitName() const;
protected: virtual ScopableAnnotation& annotation() const = 0;
ASTNode const* m_scope = nullptr;
}; };
/** /**
@ -231,6 +229,8 @@ public:
/// @returns null when it is not accessible as a function. /// @returns null when it is not accessible as a function.
virtual FunctionTypePointer functionType(bool /*_internal*/) const { return {}; } virtual FunctionTypePointer functionType(bool /*_internal*/) const { return {}; }
DeclarationAnnotation& annotation() const override;
protected: protected:
virtual Visibility defaultVisibility() const { return Visibility::Public; } virtual Visibility defaultVisibility() const { return Visibility::Public; }
@ -1169,6 +1169,8 @@ public:
std::vector<ASTPointer<Statement>> const& statements() const { return m_statements; } std::vector<ASTPointer<Statement>> const& statements() const { return m_statements; }
BlockAnnotation& annotation() const override;
private: private:
std::vector<ASTPointer<Statement>> m_statements; std::vector<ASTPointer<Statement>> m_statements;
}; };
@ -1248,6 +1250,8 @@ public:
ParameterList const* parameters() const { return m_parameters.get(); } ParameterList const* parameters() const { return m_parameters.get(); }
Block const& block() const { return *m_block; } Block const& block() const { return *m_block; }
TryCatchClauseAnnotation& annotation() const override;
private: private:
ASTPointer<ASTString> m_errorName; ASTPointer<ASTString> m_errorName;
ASTPointer<ParameterList> m_parameters; ASTPointer<ParameterList> m_parameters;
@ -1357,6 +1361,8 @@ public:
ExpressionStatement const* loopExpression() const { return m_loopExpression.get(); } ExpressionStatement const* loopExpression() const { return m_loopExpression.get(); }
Statement const& body() const { return *m_body; } Statement const& body() const { return *m_body; }
ForStatementAnnotation& annotation() const override;
private: private:
/// For statement's initialization expression. for (XXX; ; ). Can be empty /// For statement's initialization expression. for (XXX; ; ). Can be empty
ASTPointer<Statement> m_initExpression; ASTPointer<Statement> m_initExpression;

View File

@ -75,7 +75,18 @@ struct SourceUnitAnnotation: ASTAnnotation
std::set<ExperimentalFeature> experimentalFeatures; 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. /// The absolute path of the source unit to import.
std::string absolutePath; std::string absolutePath;
@ -83,7 +94,7 @@ struct ImportAnnotation: ASTAnnotation
SourceUnit const* sourceUnit = nullptr; SourceUnit const* sourceUnit = nullptr;
}; };
struct TypeDeclarationAnnotation: ASTAnnotation struct TypeDeclarationAnnotation: DeclarationAnnotation
{ {
/// The name of this type, prefixed by proper namespaces if globally accessible. /// The name of this type, prefixed by proper namespaces if globally accessible.
std::string canonicalName; std::string canonicalName;
@ -104,7 +115,7 @@ struct ContractDefinitionAnnotation: TypeDeclarationAnnotation, DocumentedAnnota
std::map<FunctionDefinition const*, ASTNode const*> baseConstructorArguments; std::map<FunctionDefinition const*, ASTNode const*> baseConstructorArguments;
}; };
struct CallableDeclarationAnnotation: ASTAnnotation struct CallableDeclarationAnnotation: DeclarationAnnotation
{ {
/// The set of functions/modifiers/events this callable overrides. /// The set of functions/modifiers/events this callable overrides.
std::set<CallableDeclaration const*> baseFunctions; std::set<CallableDeclaration const*> baseFunctions;
@ -124,7 +135,7 @@ struct ModifierDefinitionAnnotation: CallableDeclarationAnnotation, DocumentedAn
{ {
}; };
struct VariableDeclarationAnnotation: ASTAnnotation struct VariableDeclarationAnnotation: DeclarationAnnotation
{ {
/// Type of variable (type of identifier referencing this variable). /// Type of variable (type of identifier referencing this variable).
TypePointer type = nullptr; TypePointer type = nullptr;
@ -152,6 +163,18 @@ struct InlineAssemblyAnnotation: StatementAnnotation
std::shared_ptr<yul::AsmAnalysisInfo> analysisInfo; std::shared_ptr<yul::AsmAnalysisInfo> analysisInfo;
}; };
struct BlockAnnotation: StatementAnnotation, ScopableAnnotation
{
};
struct TryCatchClauseAnnotation: ASTAnnotation, ScopableAnnotation
{
};
struct ForStatementAnnotation: StatementAnnotation, ScopableAnnotation
{
};
struct ReturnAnnotation: StatementAnnotation struct ReturnAnnotation: StatementAnnotation
{ {
/// Reference to the return parameters of the function. /// Reference to the return parameters of the function.