From dcd6cb94bf32326cc06ffd246f899002d3dc77aa Mon Sep 17 00:00:00 2001 From: Leonardo Alt Date: Tue, 7 May 2019 11:51:32 +0200 Subject: [PATCH] Add function that retrieves first FunctionDefinition up the scope path --- libsolidity/ast/AST.cpp | 15 +++++++++++++++ libsolidity/ast/AST.h | 3 +++ libsolidity/ast/ASTForward.h | 1 + 3 files changed, 19 insertions(+) diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 685b821d0..a6f7d3d2d 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -400,6 +400,21 @@ SourceUnit const& Scopable::sourceUnit() const return dynamic_cast(*s); } +CallableDeclaration const* Scopable::functionOrModifierDefinition() const +{ + ASTNode const* s = scope(); + solAssert(s, ""); + while (dynamic_cast(s)) + { + if (auto funDef = dynamic_cast(s)) + return funDef; + if (auto modDef = dynamic_cast(s)) + return modDef; + s = dynamic_cast(s)->scope(); + } + return nullptr; +} + string Scopable::sourceUnitName() const { return sourceUnit().annotation().path; diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index c5a13b8cd..d8229593e 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -161,6 +161,9 @@ public: /// @returns the source unit this scopable is present in. SourceUnit const& sourceUnit() const; + /// @returns the function or modifier definition this scopable is present in or nullptr. + CallableDeclaration const* functionOrModifierDefinition() const; + /// @returns the source name this scopable is present in. /// Can be combined with annotation().canonicalName (if present) to form a globally unique name. std::string sourceUnitName() const; diff --git a/libsolidity/ast/ASTForward.h b/libsolidity/ast/ASTForward.h index 23f0b471f..aad4ad558 100644 --- a/libsolidity/ast/ASTForward.h +++ b/libsolidity/ast/ASTForward.h @@ -38,6 +38,7 @@ class SourceUnit; class PragmaDirective; class ImportDirective; class Declaration; +class CallableDeclaration; class ContractDefinition; class InheritanceSpecifier; class UsingForDirective;