2022-06-17 09:05:14 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
2022-07-01 07:52:43 +08:00
|
|
|
#include "Luau/Ast.h" // Used for some of the enumerations
|
2022-06-17 09:05:14 +08:00
|
|
|
#include "Luau/NotNull.h"
|
|
|
|
#include "Luau/Variant.h"
|
2022-08-05 06:35:33 +08:00
|
|
|
#include "Luau/TypeVar.h"
|
2022-06-17 09:05:14 +08:00
|
|
|
|
2022-06-24 09:56:00 +08:00
|
|
|
#include <string>
|
2022-06-17 09:05:14 +08:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-07-29 12:24:07 +08:00
|
|
|
struct Scope;
|
|
|
|
|
2022-06-17 09:05:14 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-08-26 05:53:50 +08:00
|
|
|
// generalizedType ~ gen sourceType
|
2022-06-17 09:05:14 +08:00
|
|
|
struct GeneralizationConstraint
|
|
|
|
{
|
|
|
|
TypeId generalizedType;
|
|
|
|
TypeId sourceType;
|
|
|
|
};
|
|
|
|
|
|
|
|
// subType ~ inst superType
|
|
|
|
struct InstantiationConstraint
|
|
|
|
{
|
|
|
|
TypeId subType;
|
|
|
|
TypeId superType;
|
|
|
|
};
|
|
|
|
|
2022-07-01 07:52:43 +08:00
|
|
|
struct UnaryConstraint
|
|
|
|
{
|
|
|
|
AstExprUnary::Op op;
|
|
|
|
TypeId operandType;
|
|
|
|
TypeId resultType;
|
|
|
|
};
|
|
|
|
|
2022-09-02 07:14:03 +08:00
|
|
|
// let L : leftType
|
|
|
|
// let R : rightType
|
|
|
|
// in
|
|
|
|
// L op R : resultType
|
2022-07-01 07:52:43 +08:00
|
|
|
struct BinaryConstraint
|
|
|
|
{
|
|
|
|
AstExprBinary::Op op;
|
|
|
|
TypeId leftType;
|
|
|
|
TypeId rightType;
|
|
|
|
TypeId resultType;
|
|
|
|
};
|
|
|
|
|
2022-09-02 07:14:03 +08:00
|
|
|
// iteratee is iterable
|
|
|
|
// iterators is the iteration types.
|
|
|
|
struct IterableConstraint
|
|
|
|
{
|
|
|
|
TypePackId iterator;
|
|
|
|
TypePackId variables;
|
|
|
|
};
|
|
|
|
|
2022-06-24 09:56:00 +08:00
|
|
|
// name(namedType) = name
|
|
|
|
struct NameConstraint
|
|
|
|
{
|
|
|
|
TypeId namedType;
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
|
2022-08-05 06:35:33 +08:00
|
|
|
// target ~ inst target
|
|
|
|
struct TypeAliasExpansionConstraint
|
|
|
|
{
|
|
|
|
// Must be a PendingExpansionTypeVar.
|
|
|
|
TypeId target;
|
|
|
|
};
|
|
|
|
|
2022-06-17 09:05:14 +08:00
|
|
|
using ConstraintPtr = std::unique_ptr<struct Constraint>;
|
|
|
|
|
2022-09-02 07:14:03 +08:00
|
|
|
struct FunctionCallConstraint
|
|
|
|
{
|
|
|
|
std::vector<NotNull<const Constraint>> innerConstraints;
|
|
|
|
TypeId fn;
|
|
|
|
TypePackId result;
|
|
|
|
class AstExprCall* astFragment;
|
|
|
|
};
|
|
|
|
|
|
|
|
using ConstraintV = Variant<SubtypeConstraint, PackSubtypeConstraint, GeneralizationConstraint, InstantiationConstraint, UnaryConstraint,
|
|
|
|
BinaryConstraint, IterableConstraint, NameConstraint, TypeAliasExpansionConstraint, FunctionCallConstraint>;
|
|
|
|
|
2022-06-17 09:05:14 +08:00
|
|
|
struct Constraint
|
|
|
|
{
|
2022-09-02 07:14:03 +08:00
|
|
|
Constraint(NotNull<Scope> scope, const Location& location, ConstraintV&& c);
|
2022-06-17 09:05:14 +08:00
|
|
|
|
|
|
|
Constraint(const Constraint&) = delete;
|
|
|
|
Constraint& operator=(const Constraint&) = delete;
|
|
|
|
|
2022-09-02 07:14:03 +08:00
|
|
|
NotNull<Scope> scope;
|
|
|
|
Location location;
|
2022-06-17 09:05:14 +08:00
|
|
|
ConstraintV c;
|
2022-09-02 07:14:03 +08:00
|
|
|
|
2022-06-17 09:05:14 +08:00
|
|
|
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
|