fixup! Type inference draft.

This commit is contained in:
Kamil Śliwak 2023-09-26 16:37:17 +02:00
parent 1ffd65f089
commit e3caed0ea4
3 changed files with 49 additions and 9 deletions

View File

@ -149,7 +149,13 @@ std::tuple<std::integral_constant<size_t, Is>...> makeIndexTuple(std::index_sequ
bool Analysis::check(std::vector<std::shared_ptr<SourceUnit const>> const& _sourceUnits)
{
using AnalysisSteps = std::tuple<SyntaxRestrictor, TypeClassRegistration, TypeRegistration, TypeInference, DebugWarner>;
using AnalysisSteps = std::tuple<
SyntaxRestrictor,
TypeClassRegistration,
TypeRegistration,
TypeInference,
DebugWarner
>;
return std::apply([&](auto... _indexTuple) {
return ([&](auto&& _step) {

View File

@ -1088,7 +1088,11 @@ TypeRegistration::TypeClassInstantiations const& typeClassInstantiations(Analysi
// TODO: better mechanism than fetching by name.
auto& annotation = _analysis.annotation<TypeRegistration>();
auto& inferenceAnnotation = _analysis.annotation<TypeInference>();
return annotation.builtinClassInstantiations.at(inferenceAnnotation.builtinClassesByName.at(_analysis.typeSystem().typeClassName(_class)));
return annotation.builtinClassInstantiations.at(
inferenceAnnotation.builtinClassesByName.at(
_analysis.typeSystem().typeClassName(_class)
)
);
}
}
@ -1180,7 +1184,8 @@ void TypeInference::unify(Type _a, Type _b, langutil::SourceLocation _location)
fmt::format(
"Cannot unify {} and {}.",
envHelper.typeToString(_typeMismatch.a),
envHelper.typeToString(_typeMismatch.b))
envHelper.typeToString(_typeMismatch.b)
)
);
},
[&](TypeEnvironment::SortMismatch _sortMismatch) {
@ -1197,7 +1202,8 @@ void TypeInference::unify(Type _a, Type _b, langutil::SourceLocation _location)
fmt::format(
"Recursive unification: {} occurs in {}.",
envHelper.typeToString(_recursiveUnification.var),
envHelper.typeToString(_recursiveUnification.type))
envHelper.typeToString(_recursiveUnification.type)
)
);
}
}, failure);

View File

@ -29,31 +29,59 @@ namespace solidity::frontend
{
class Declaration;
}
namespace solidity::frontend::experimental
{
class TypeEnvironment
{
public:
struct TypeMismatch
{
Type a;
Type b;
};
struct SortMismatch {
Type type;
Sort sort;
};
struct RecursiveUnification
{
Type var;
Type type;
};
using UnificationFailure = std::variant<
TypeMismatch,
SortMismatch,
RecursiveUnification
>;
TypeEnvironment(TypeSystem& _typeSystem): m_typeSystem(_typeSystem) {}
TypeEnvironment(TypeEnvironment const&) = delete;
TypeEnvironment& operator=(TypeEnvironment const&) = delete;
TypeEnvironment clone() const;
Type resolve(Type _type) const;
Type resolveRecursive(Type _type) const;
Type fresh(Type _type);
struct TypeMismatch { Type a; Type b; };
struct SortMismatch { Type type; Sort sort; };
struct RecursiveUnification { Type var; Type type; };
using UnificationFailure = std::variant<TypeMismatch, SortMismatch, RecursiveUnification>;
[[nodiscard]] std::vector<UnificationFailure> unify(Type _a, Type _b);
Sort sort(Type _type) const;
bool typeEquals(Type _lhs, Type _rhs) const;
TypeSystem& typeSystem() { return m_typeSystem; }
TypeSystem const& typeSystem() const { return m_typeSystem; }
private:
TypeEnvironment(TypeEnvironment&& _env): m_typeSystem(_env.m_typeSystem), m_typeVariables(std::move(_env.m_typeVariables)) {}
TypeEnvironment(TypeEnvironment&& _env):
m_typeSystem(_env.m_typeSystem),
m_typeVariables(std::move(_env.m_typeVariables))
{}
[[nodiscard]] std::vector<TypeEnvironment::UnificationFailure> instantiate(TypeVariable _variable, Type _type);
TypeSystem& m_typeSystem;
std::map<size_t, Type> m_typeVariables;
};