Add function that retrieves first FunctionDefinition up the scope path

This commit is contained in:
Leonardo Alt 2019-05-07 11:51:32 +02:00
parent 8f2c8daf22
commit dcd6cb94bf
3 changed files with 19 additions and 0 deletions

View File

@ -400,6 +400,21 @@ SourceUnit const& Scopable::sourceUnit() const
return dynamic_cast<SourceUnit const&>(*s);
}
CallableDeclaration const* Scopable::functionOrModifierDefinition() const
{
ASTNode const* s = scope();
solAssert(s, "");
while (dynamic_cast<Scopable const*>(s))
{
if (auto funDef = dynamic_cast<FunctionDefinition const*>(s))
return funDef;
if (auto modDef = dynamic_cast<ModifierDefinition const*>(s))
return modDef;
s = dynamic_cast<Scopable const*>(s)->scope();
}
return nullptr;
}
string Scopable::sourceUnitName() const
{
return sourceUnit().annotation().path;

View File

@ -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;

View File

@ -38,6 +38,7 @@ class SourceUnit;
class PragmaDirective;
class ImportDirective;
class Declaration;
class CallableDeclaration;
class ContractDefinition;
class InheritanceSpecifier;
class UsingForDirective;