From 5f20129e65f5b8b714189145d177067152a21ac1 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 9 Feb 2018 16:53:25 +0100 Subject: Scopes do not have to be declarations. --- libsolidity/ast/AST.cpp | 11 ++++++----- libsolidity/ast/AST.h | 26 ++++++++++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) (limited to 'libsolidity/ast') diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 8da6964e..af007908 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -98,11 +98,12 @@ set SourceUnit::referencedSourceUnits(bool _recurse, set(scope) && dynamic_cast(scope)->m_scope) - scope = dynamic_cast(scope)->m_scope; - return dynamic_cast(*scope); + ASTNode const* s = scope(); + solAssert(s, ""); + // will not always be a declaratoion + while (dynamic_cast(s) && dynamic_cast(s)->scope()) + s = dynamic_cast(s)->scope(); + return dynamic_cast(*s); } string Declaration::sourceUnitName() const diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index c0d55aec..a0089d64 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -139,10 +139,26 @@ private: std::vector> m_nodes; }; +/** + * Abstract class that is added to each AST node that is stored inside a scope + * (including scopes). + */ +class Scopable +{ +public: + /// @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; } + +protected: + ASTNode const* m_scope = nullptr; +}; + /** * Abstract AST class for a declaration (contract, function, struct, variable, import directive). */ -class Declaration: public ASTNode +class Declaration: public ASTNode, public Scopable { public: /// Visibility ordered from restricted to unrestricted. @@ -171,7 +187,7 @@ public: ASTPointer const& _name, Visibility _visibility = Visibility::Default ): - ASTNode(_location), m_name(_name), m_visibility(_visibility), m_scope(nullptr) {} + ASTNode(_location), m_name(_name), m_visibility(_visibility) {} /// @returns the declared name. ASTString const& name() const { return *m_name; } @@ -181,11 +197,6 @@ public: virtual bool isVisibleInContract() const { return visibility() != Visibility::External; } bool isVisibleInDerivedContracts() const { return isVisibleInContract() && visibility() >= Visibility::Internal; } - /// @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; } - /// @returns the source unit this declaration is present in. SourceUnit const& sourceUnit() const; @@ -213,7 +224,6 @@ protected: private: ASTPointer m_name; Visibility m_visibility; - ASTNode const* m_scope; }; /** -- cgit v1.2.3