luau/Analysis/include/Luau/Constraint.h

115 lines
2.1 KiB
C
Raw Normal View History

2022-06-17 08:54:42 +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:29:02 +08:00
#include "Luau/Ast.h" // Used for some of the enumerations
2022-06-17 08:54:42 +08:00
#include "Luau/NotNull.h"
#include "Luau/Variant.h"
2022-08-05 05:27:28 +08:00
#include "Luau/TypeVar.h"
2022-06-17 08:54:42 +08:00
2022-06-24 09:44:07 +08:00
#include <string>
2022-06-17 08:54:42 +08:00
#include <memory>
#include <vector>
namespace Luau
{
2022-07-29 11:41:13 +08:00
struct Scope;
2022-06-17 08:54:42 +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;
};
// subType ~ gen superType
struct GeneralizationConstraint
{
TypeId generalizedType;
TypeId sourceType;
};
// subType ~ inst superType
struct InstantiationConstraint
{
TypeId subType;
TypeId superType;
};
2022-07-01 07:29:02 +08:00
struct UnaryConstraint
{
AstExprUnary::Op op;
TypeId operandType;
TypeId resultType;
};
struct BinaryConstraint
{
AstExprBinary::Op op;
TypeId leftType;
TypeId rightType;
TypeId resultType;
};
2022-06-24 09:44:07 +08:00
// name(namedType) = name
struct NameConstraint
{
TypeId namedType;
std::string name;
};
2022-08-05 05:27:28 +08:00
// target ~ inst target
struct TypeAliasExpansionConstraint
{
// Must be a PendingExpansionTypeVar.
TypeId target;
};
2022-07-01 07:29:02 +08:00
using ConstraintV = Variant<SubtypeConstraint, PackSubtypeConstraint, GeneralizationConstraint, InstantiationConstraint, UnaryConstraint,
2022-08-05 05:27:28 +08:00
BinaryConstraint, NameConstraint, TypeAliasExpansionConstraint>;
2022-06-17 08:54:42 +08:00
using ConstraintPtr = std::unique_ptr<struct Constraint>;
struct Constraint
{
2022-08-19 05:04:33 +08:00
Constraint(ConstraintV&& c, NotNull<Scope> scope);
2022-06-17 08:54:42 +08:00
Constraint(const Constraint&) = delete;
Constraint& operator=(const Constraint&) = delete;
ConstraintV c;
std::vector<NotNull<Constraint>> dependencies;
2022-08-19 05:04:33 +08:00
NotNull<Scope> scope;
2022-06-17 08:54:42 +08:00
};
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