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
|
|
|
|
|
2022-07-01 00:29:02 +01:00
|
|
|
#include "Luau/Ast.h" // Used for some of the enumerations
|
2022-10-21 18:33:43 +01:00
|
|
|
#include "Luau/Def.h"
|
2022-12-09 18:07:25 +00:00
|
|
|
#include "Luau/DenseHash.h"
|
2022-06-17 01:54:42 +01:00
|
|
|
#include "Luau/NotNull.h"
|
2023-01-03 17:33:19 +00:00
|
|
|
#include "Luau/Type.h"
|
2022-10-21 18:33:43 +01:00
|
|
|
#include "Luau/Variant.h"
|
2022-06-17 01:54:42 +01:00
|
|
|
|
2022-06-24 02:44:07 +01:00
|
|
|
#include <string>
|
2022-06-17 01:54:42 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2022-07-29 04:41:13 +01:00
|
|
|
struct Scope;
|
|
|
|
|
2023-01-03 17:33:19 +00:00
|
|
|
struct Type;
|
|
|
|
using TypeId = const Type*;
|
2022-06-17 01:54:42 +01:00
|
|
|
|
|
|
|
struct TypePackVar;
|
|
|
|
using TypePackId = const TypePackVar*;
|
|
|
|
|
|
|
|
// subType <: superType
|
|
|
|
struct SubtypeConstraint
|
|
|
|
{
|
|
|
|
TypeId subType;
|
|
|
|
TypeId superType;
|
|
|
|
};
|
|
|
|
|
|
|
|
// subPack <: superPack
|
|
|
|
struct PackSubtypeConstraint
|
|
|
|
{
|
|
|
|
TypePackId subPack;
|
|
|
|
TypePackId superPack;
|
|
|
|
};
|
|
|
|
|
2022-08-25 21:55:08 +01:00
|
|
|
// generalizedType ~ gen sourceType
|
2022-06-17 01:54:42 +01:00
|
|
|
struct GeneralizationConstraint
|
|
|
|
{
|
|
|
|
TypeId generalizedType;
|
|
|
|
TypeId sourceType;
|
|
|
|
};
|
|
|
|
|
|
|
|
// subType ~ inst superType
|
|
|
|
struct InstantiationConstraint
|
|
|
|
{
|
|
|
|
TypeId subType;
|
|
|
|
TypeId superType;
|
|
|
|
};
|
|
|
|
|
2022-07-01 00:29:02 +01:00
|
|
|
struct UnaryConstraint
|
|
|
|
{
|
|
|
|
AstExprUnary::Op op;
|
|
|
|
TypeId operandType;
|
|
|
|
TypeId resultType;
|
|
|
|
};
|
|
|
|
|
2022-09-02 00:00:14 +01:00
|
|
|
// let L : leftType
|
|
|
|
// let R : rightType
|
|
|
|
// in
|
|
|
|
// L op R : resultType
|
2022-07-01 00:29:02 +01:00
|
|
|
struct BinaryConstraint
|
|
|
|
{
|
|
|
|
AstExprBinary::Op op;
|
|
|
|
TypeId leftType;
|
|
|
|
TypeId rightType;
|
|
|
|
TypeId resultType;
|
2022-12-09 18:07:25 +00:00
|
|
|
|
|
|
|
// When we dispatch this constraint, we update the key at this map to record
|
|
|
|
// the overload that we selected.
|
|
|
|
AstExpr* expr;
|
|
|
|
DenseHashMap<const AstExpr*, TypeId>* astOriginalCallTypes;
|
|
|
|
DenseHashMap<const AstExpr*, TypeId>* astOverloadResolvedTypes;
|
2022-07-01 00:29:02 +01:00
|
|
|
};
|
|
|
|
|
2022-09-02 00:00:14 +01:00
|
|
|
// iteratee is iterable
|
|
|
|
// iterators is the iteration types.
|
|
|
|
struct IterableConstraint
|
|
|
|
{
|
|
|
|
TypePackId iterator;
|
|
|
|
TypePackId variables;
|
|
|
|
};
|
|
|
|
|
2022-06-24 02:44:07 +01:00
|
|
|
// name(namedType) = name
|
|
|
|
struct NameConstraint
|
|
|
|
{
|
|
|
|
TypeId namedType;
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
|
2022-08-04 22:27:28 +01:00
|
|
|
// target ~ inst target
|
|
|
|
struct TypeAliasExpansionConstraint
|
|
|
|
{
|
2023-01-03 17:33:19 +00:00
|
|
|
// Must be a PendingExpansionType.
|
2022-08-04 22:27:28 +01:00
|
|
|
TypeId target;
|
|
|
|
};
|
|
|
|
|
2022-09-02 00:00:14 +01:00
|
|
|
struct FunctionCallConstraint
|
|
|
|
{
|
2022-09-23 19:32:10 +01:00
|
|
|
std::vector<NotNull<const struct Constraint>> innerConstraints;
|
2022-09-02 00:00:14 +01:00
|
|
|
TypeId fn;
|
2022-09-29 23:11:54 +01:00
|
|
|
TypePackId argsPack;
|
2022-09-02 00:00:14 +01:00
|
|
|
TypePackId result;
|
2022-09-29 23:11:54 +01:00
|
|
|
class AstExprCall* callSite;
|
2022-09-02 00:00:14 +01:00
|
|
|
};
|
|
|
|
|
2022-09-23 19:32:10 +01:00
|
|
|
// result ~ prim ExpectedType SomeSingletonType MultitonType
|
|
|
|
//
|
|
|
|
// If ExpectedType is potentially a singleton (an actual singleton or a union
|
|
|
|
// that contains a singleton), then result ~ SomeSingletonType
|
|
|
|
//
|
|
|
|
// else result ~ MultitonType
|
|
|
|
struct PrimitiveTypeConstraint
|
|
|
|
{
|
|
|
|
TypeId resultType;
|
|
|
|
TypeId expectedType;
|
|
|
|
TypeId singletonType;
|
|
|
|
TypeId multitonType;
|
|
|
|
};
|
|
|
|
|
|
|
|
// result ~ hasProp type "prop_name"
|
|
|
|
//
|
|
|
|
// If the subject is a table, bind the result to the named prop. If the table
|
|
|
|
// has an indexer, bind it to the index result type. If the subject is a union,
|
|
|
|
// bind the result to the union of its constituents' properties.
|
|
|
|
//
|
|
|
|
// It would be nice to get rid of this constraint and someday replace it with
|
|
|
|
//
|
|
|
|
// T <: {p: X}
|
|
|
|
//
|
|
|
|
// Where {} describes an inexact shape type.
|
|
|
|
struct HasPropConstraint
|
|
|
|
{
|
|
|
|
TypeId resultType;
|
|
|
|
TypeId subjectType;
|
|
|
|
std::string prop;
|
|
|
|
};
|
|
|
|
|
2022-11-18 18:45:14 +00:00
|
|
|
// result ~ setProp subjectType ["prop", "prop2", ...] propType
|
|
|
|
//
|
|
|
|
// If the subject is a table or table-like thing that already has the named
|
|
|
|
// property chain, we unify propType with that existing property type.
|
|
|
|
//
|
|
|
|
// If the subject is a free table, we augment it in place.
|
|
|
|
//
|
|
|
|
// If the subject is an unsealed table, result is an augmented table that
|
|
|
|
// includes that new prop.
|
|
|
|
struct SetPropConstraint
|
|
|
|
{
|
|
|
|
TypeId resultType;
|
|
|
|
TypeId subjectType;
|
|
|
|
std::vector<std::string> path;
|
|
|
|
TypeId propType;
|
|
|
|
};
|
|
|
|
|
2022-12-02 10:46:05 +00:00
|
|
|
// if negation:
|
|
|
|
// result ~ if isSingleton D then ~D else unknown where D = discriminantType
|
|
|
|
// if not negation:
|
|
|
|
// result ~ if isSingleton D then D else unknown where D = discriminantType
|
2022-11-04 17:02:37 +00:00
|
|
|
struct SingletonOrTopTypeConstraint
|
2022-10-21 18:33:43 +01:00
|
|
|
{
|
2022-11-04 17:02:37 +00:00
|
|
|
TypeId resultType;
|
2022-10-21 18:33:43 +01:00
|
|
|
TypeId discriminantType;
|
2022-12-02 10:46:05 +00:00
|
|
|
bool negated;
|
2022-10-21 18:33:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using ConstraintV = Variant<SubtypeConstraint, PackSubtypeConstraint, GeneralizationConstraint, InstantiationConstraint, UnaryConstraint,
|
|
|
|
BinaryConstraint, IterableConstraint, NameConstraint, TypeAliasExpansionConstraint, FunctionCallConstraint, PrimitiveTypeConstraint,
|
2022-11-18 18:45:14 +00:00
|
|
|
HasPropConstraint, SetPropConstraint, SingletonOrTopTypeConstraint>;
|
2022-09-02 00:00:14 +01:00
|
|
|
|
2022-06-17 01:54:42 +01:00
|
|
|
struct Constraint
|
|
|
|
{
|
2022-09-02 00:00:14 +01:00
|
|
|
Constraint(NotNull<Scope> scope, const Location& location, ConstraintV&& c);
|
2022-06-17 01:54:42 +01:00
|
|
|
|
|
|
|
Constraint(const Constraint&) = delete;
|
|
|
|
Constraint& operator=(const Constraint&) = delete;
|
|
|
|
|
2022-09-02 00:00:14 +01:00
|
|
|
NotNull<Scope> scope;
|
2022-10-21 18:33:43 +01:00
|
|
|
Location location; // TODO: Extract this out into only the constraints that needs a location. Not all constraints needs locations.
|
2022-06-17 01:54:42 +01:00
|
|
|
ConstraintV c;
|
2022-09-02 00:00:14 +01:00
|
|
|
|
2022-06-17 01:54:42 +01:00
|
|
|
std::vector<NotNull<Constraint>> dependencies;
|
|
|
|
};
|
|
|
|
|
2022-09-23 19:32:10 +01:00
|
|
|
using ConstraintPtr = std::unique_ptr<Constraint>;
|
|
|
|
|
2022-06-17 01:54:42 +01: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
|