diff options
author | chriseth <c@ethdev.com> | 2017-02-01 06:12:40 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2017-02-14 20:23:44 +0800 |
commit | b1bb228ab3a8ca0548ed5f08fdc5fda1fcb71b1a (patch) | |
tree | 246e38dffc35acbaa7a3039f90375f34a9be05cb /libsolidity | |
parent | e67faa9839ebd0dadef2adf3ed1ef69fac6f65e1 (diff) | |
download | dexon-solidity-b1bb228ab3a8ca0548ed5f08fdc5fda1fcb71b1a.tar.gz dexon-solidity-b1bb228ab3a8ca0548ed5f08fdc5fda1fcb71b1a.tar.zst dexon-solidity-b1bb228ab3a8ca0548ed5f08fdc5fda1fcb71b1a.zip |
Allow different entry scope for registerDeclarations.
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/analysis/NameAndTypeResolver.cpp | 13 | ||||
-rw-r--r-- | libsolidity/analysis/NameAndTypeResolver.h | 11 | ||||
-rw-r--r-- | libsolidity/analysis/TypeChecker.cpp | 7 |
3 files changed, 22 insertions, 9 deletions
diff --git a/libsolidity/analysis/NameAndTypeResolver.cpp b/libsolidity/analysis/NameAndTypeResolver.cpp index ed1bd1d3..fd6fc058 100644 --- a/libsolidity/analysis/NameAndTypeResolver.cpp +++ b/libsolidity/analysis/NameAndTypeResolver.cpp @@ -46,12 +46,12 @@ NameAndTypeResolver::NameAndTypeResolver( m_scopes[nullptr]->registerDeclaration(*declaration); } -bool NameAndTypeResolver::registerDeclarations(ASTNode& _sourceUnit) +bool NameAndTypeResolver::registerDeclarations(ASTNode& _sourceUnit, ASTNode const* _currentScope) { // The helper registers all declarations in m_scopes as a side-effect of its construction. try { - DeclarationRegistrationHelper registrar(m_scopes, _sourceUnit, m_errors); + DeclarationRegistrationHelper registrar(m_scopes, _sourceUnit, m_errors, _currentScope); } catch (FatalError const&) { @@ -451,21 +451,22 @@ void NameAndTypeResolver::reportFatalTypeError(Error const& _e) DeclarationRegistrationHelper::DeclarationRegistrationHelper( map<ASTNode const*, shared_ptr<DeclarationContainer>>& _scopes, ASTNode& _astRoot, - ErrorList& _errors + ErrorList& _errors, + ASTNode const* _currentScope ): m_scopes(_scopes), - m_currentScope(nullptr), + m_currentScope(_currentScope), m_errors(_errors) { _astRoot.accept(*this); - solAssert(m_currentScope == nullptr, "Scopes not correctly closed."); + solAssert(m_currentScope == _currentScope, "Scopes not correctly closed."); } bool DeclarationRegistrationHelper::visit(SourceUnit& _sourceUnit) { if (!m_scopes[&_sourceUnit]) // By importing, it is possible that the container already exists. - m_scopes[&_sourceUnit].reset(new DeclarationContainer(nullptr, m_scopes[nullptr].get())); + m_scopes[&_sourceUnit].reset(new DeclarationContainer(m_currentScope, m_scopes[m_currentScope].get())); m_currentScope = &_sourceUnit; return true; } diff --git a/libsolidity/analysis/NameAndTypeResolver.h b/libsolidity/analysis/NameAndTypeResolver.h index 1c7af0c9..4de40e87 100644 --- a/libsolidity/analysis/NameAndTypeResolver.h +++ b/libsolidity/analysis/NameAndTypeResolver.h @@ -49,7 +49,9 @@ public: ); /// Registers all declarations found in the AST node, usually a source unit. /// @returns false in case of error. - bool registerDeclarations(ASTNode& _sourceUnit); + /// @param _currentScope should be nullptr but can be used to inject new declarations into + /// existing scopes, used by the snippets feature. + bool registerDeclarations(ASTNode& _sourceUnit, ASTNode const* _currentScope = nullptr); /// Applies the effect of import directives. bool performImports(SourceUnit& _sourceUnit, std::map<std::string, SourceUnit const*> const& _sourceUnits); /// Resolves all names and types referenced from the given AST Node. @@ -130,10 +132,15 @@ private: class DeclarationRegistrationHelper: private ASTVisitor { public: + /// Registers declarations in their scopes and creates new scopes as a side-effect + /// of construction. + /// @param _currentScope should be nullptr if we start at SourceUnit, but can be different + /// to inject new declarations into an existing scope, used by snippets. DeclarationRegistrationHelper( std::map<ASTNode const*, std::shared_ptr<DeclarationContainer>>& _scopes, ASTNode& _astRoot, - ErrorList& _errors + ErrorList& _errors, + ASTNode const* _currentScope = nullptr ); private: diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index be59d3d2..06a9e1ce 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -427,7 +427,12 @@ bool TypeChecker::visit(StructDefinition const& _struct) bool TypeChecker::visit(FunctionDefinition const& _function) { - bool isLibraryFunction = dynamic_cast<ContractDefinition const&>(*_function.scope()).isLibrary(); + bool isLibraryFunction = false; + if ( + dynamic_cast<ContractDefinition const*>(_function.scope()) && + dynamic_cast<ContractDefinition const*>(_function.scope())->isLibrary() + ) + isLibraryFunction = true; if (_function.isPayable()) { if (isLibraryFunction) |