luau/Analysis/include/Luau/Constraint.h

89 lines
1.6 KiB
C
Raw Normal View History

2022-06-17 01:54:42 +01:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
#include "Luau/NotNull.h"
#include "Luau/Variant.h"
2022-06-24 02:44:07 +01:00
#include <string>
2022-06-17 01:54:42 +01:00
#include <memory>
#include <vector>
namespace Luau
{
struct Scope2;
struct TypeVar;
using TypeId = const TypeVar*;
struct TypePackVar;
using TypePackId = const TypePackVar*;
// subType <: superType
struct SubtypeConstraint
{
TypeId subType;
TypeId superType;
};
// subPack <: superPack
struct PackSubtypeConstraint
{
TypePackId subPack;
TypePackId superPack;
};
// subType ~ gen superType
struct GeneralizationConstraint
{
TypeId generalizedType;
TypeId sourceType;
Scope2* scope;
};
// subType ~ inst superType
struct InstantiationConstraint
{
TypeId subType;
TypeId superType;
};
2022-06-24 02:44:07 +01:00
// name(namedType) = name
struct NameConstraint
{
TypeId namedType;
std::string name;
};
using ConstraintV = Variant<SubtypeConstraint, PackSubtypeConstraint, GeneralizationConstraint, InstantiationConstraint, NameConstraint>;
2022-06-17 01:54:42 +01:00
using ConstraintPtr = std::unique_ptr<struct Constraint>;
struct Constraint
{
2022-06-24 02:44:07 +01:00
explicit Constraint(ConstraintV&& c);
2022-06-17 01:54:42 +01:00
Constraint(const Constraint&) = delete;
Constraint& operator=(const Constraint&) = delete;
ConstraintV c;
std::vector<NotNull<Constraint>> dependencies;
};
inline Constraint& asMutable(const Constraint& c)
{
return const_cast<Constraint&>(c);
}
template<typename T>
T* getMutable(Constraint& c)
{
return ::Luau::get_if<T>(&c.c);
}
template<typename T>
const T* get(const Constraint& c)
{
return getMutable<T>(asMutable(c));
}
} // namespace Luau