Skip to content

Commit e545e93

Browse files
cameelekpyron
authored andcommitted
TypeSystemHelper: substitute()
1 parent 1906cf1 commit e545e93

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

libsolidity/experimental/ast/TypeSystemHelper.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,34 @@ std::vector<experimental::Type> TypeEnvironmentHelpers::typeVars(Type _type) con
287287
};
288288
typeVarsImpl(_type, typeVarsImpl);
289289
return typeVars;
290-
291290
}
292291

292+
experimental::Type TypeEnvironmentHelpers::substitute(
293+
Type const& _type,
294+
Type const& _partToReplace,
295+
Type const& _replacement
296+
) const
297+
{
298+
using ranges::views::transform;
299+
using ranges::to;
300+
301+
if (env.typeEquals(_type, _partToReplace))
302+
return _replacement;
303+
304+
auto recurse = [&](Type const& _t) { return substitute(_t, _partToReplace, _replacement); };
305+
306+
return visit(util::GenericVisitor{
307+
[&](TypeConstant const& _typeConstant) -> Type {
308+
return TypeConstant{
309+
_typeConstant.constructor,
310+
_typeConstant.arguments | transform(recurse) | to<std::vector<Type>>,
311+
};
312+
},
313+
[&](auto const& _type) -> Type {
314+
return _type;
315+
},
316+
}, env.resolve(_type));
317+
}
293318

294319
std::string TypeSystemHelpers::sortToString(Sort _sort) const
295320
{

libsolidity/experimental/ast/TypeSystemHelper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct TypeEnvironmentHelpers
5555
std::string typeToString(Type const& _type) const;
5656
std::string canonicalTypeName(Type _type) const;
5757
std::vector<Type> typeVars(Type _type) const;
58+
59+
Type substitute(Type const& _type, Type const& _partToReplace, Type const& _replacement) const;
5860
};
5961

6062
}

0 commit comments

Comments
 (0)