Skip to content

Commit 7bd6cba

Browse files
committed
Provide generic access to type definition for user-defined types.
1 parent 145186f commit 7bd6cba

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

libsolidity/ast/Types.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,11 @@ TypeResult StructType::interfaceType(bool _inLibrary) const
23412341
return *m_interfaceType_library;
23422342
}
23432343

2344+
Declaration const* StructType::typeDefinition() const
2345+
{
2346+
return &structDefinition();
2347+
}
2348+
23442349
BoolResult StructType::validForLocation(DataLocation _loc) const
23452350
{
23462351
for (auto const& member: m_struct.members())
@@ -2473,6 +2478,11 @@ Type const* EnumType::encodingType() const
24732478
return TypeProvider::uint(8);
24742479
}
24752480

2481+
Declaration const* EnumType::typeDefinition() const
2482+
{
2483+
return &enumDefinition();
2484+
}
2485+
24762486
TypeResult EnumType::unaryOperatorResult(Token _operator) const
24772487
{
24782488
return _operator == Token::Delete ? TypeProvider::emptyTuple() : nullptr;
@@ -2541,6 +2551,11 @@ Type const& UserDefinedValueType::underlyingType() const
25412551
return *type;
25422552
}
25432553

2554+
Declaration const* UserDefinedValueType::typeDefinition() const
2555+
{
2556+
return &m_definition;
2557+
}
2558+
25442559
string UserDefinedValueType::richIdentifier() const
25452560
{
25462561
return "t_userDefinedValueType" + parenthesizeIdentifier(m_definition.name()) + to_string(m_definition.id());

libsolidity/ast/Types.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ class Type
369369
/// are returned without modification.
370370
virtual TypeResult interfaceType(bool /*_inLibrary*/) const { return nullptr; }
371371

372+
/// @returns the declaration of a user defined type (enum, struct, user defined value type).
373+
/// Returns nullptr otherwise.
374+
virtual Declaration const* typeDefinition() const { return nullptr; }
375+
372376
/// Clears all internally cached values (if any).
373377
virtual void clearCache() const;
374378

@@ -1004,6 +1008,8 @@ class StructType: public ReferenceType
10041008
Type const* encodingType() const override;
10051009
TypeResult interfaceType(bool _inLibrary) const override;
10061010

1011+
Declaration const* typeDefinition() const override;
1012+
10071013
BoolResult validForLocation(DataLocation _loc) const override;
10081014

10091015
bool recursive() const;
@@ -1069,6 +1075,8 @@ class EnumType: public Type
10691075
return _inLibrary ? this : encodingType();
10701076
}
10711077

1078+
Declaration const* typeDefinition() const override;
1079+
10721080
EnumDefinition const& enumDefinition() const { return m_enum; }
10731081
/// @returns the value that the string has in the Enum
10741082
unsigned int memberValue(ASTString const& _member) const;
@@ -1101,6 +1109,9 @@ class UserDefinedValueType: public Type
11011109
TypeResult binaryOperatorResult(Token, Type const*) const override { return nullptr; }
11021110
Type const* encodingType() const override { return &underlyingType(); }
11031111
TypeResult interfaceType(bool /* _inLibrary */) const override {return &underlyingType(); }
1112+
1113+
Declaration const* typeDefinition() const override;
1114+
11041115
std::string richIdentifier() const override;
11051116
bool operator==(Type const& _other) const override;
11061117

0 commit comments

Comments
 (0)