luau/Analysis/include/Luau/ConstraintSolver.h

227 lines
8.7 KiB
C
Raw Normal View History

2022-06-04 04:32:20 +08: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/Error.h"
#include "Luau/Variant.h"
2022-06-17 08:54:42 +08:00
#include "Luau/Constraint.h"
2022-06-04 04:32:20 +08:00
#include "Luau/TypeVar.h"
2022-09-09 05:44:50 +08:00
#include "Luau/ToString.h"
2022-10-07 07:55:58 +08:00
#include "Luau/Normalize.h"
2022-06-04 04:32:20 +08:00
#include <vector>
namespace Luau
{
2022-09-09 05:44:50 +08:00
struct DcrLogger;
2022-06-04 04:32:20 +08:00
// TypeId, TypePackId, or Constraint*. It is impossible to know which, but we
// never dereference this pointer.
using BlockedConstraintId = const void*;
2022-09-02 07:00:14 +08:00
struct ModuleResolver;
2022-08-05 05:27:28 +08:00
struct InstantiationSignature
{
TypeFun fn;
std::vector<TypeId> arguments;
std::vector<TypePackId> packArguments;
bool operator==(const InstantiationSignature& rhs) const;
bool operator!=(const InstantiationSignature& rhs) const
{
return !((*this) == rhs);
}
};
struct HashInstantiationSignature
{
size_t operator()(const InstantiationSignature& signature) const;
};
2022-06-04 04:32:20 +08:00
struct ConstraintSolver
{
TypeArena* arena;
2022-09-09 05:44:50 +08:00
NotNull<SingletonTypes> singletonTypes;
2022-06-04 04:32:20 +08:00
InternalErrorReporter iceReporter;
2022-10-07 07:55:58 +08:00
NotNull<Normalizer> normalizer;
2022-08-05 05:27:28 +08:00
// The entire set of constraints that the solver is trying to resolve.
std::vector<NotNull<Constraint>> constraints;
2022-07-29 11:41:13 +08:00
NotNull<Scope> rootScope;
2022-09-02 07:00:14 +08:00
ModuleName currentModuleName;
2022-06-04 04:32:20 +08:00
2022-08-05 05:27:28 +08:00
// Constraints that the solver has generated, rather than sourcing from the
// scope tree.
std::vector<std::unique_ptr<Constraint>> solverConstraints;
2022-06-04 04:32:20 +08:00
// This includes every constraint that has not been fully solved.
// A constraint can be both blocked and unsolved, for instance.
2022-06-17 08:54:42 +08:00
std::vector<NotNull<const Constraint>> unsolvedConstraints;
2022-06-04 04:32:20 +08:00
// A mapping of constraint pointer to how many things the constraint is
// blocked on. Can be empty or 0 for constraints that are not blocked on
// anything.
2022-06-17 08:54:42 +08:00
std::unordered_map<NotNull<const Constraint>, size_t> blockedConstraints;
2022-06-04 04:32:20 +08:00
// A mapping of type/pack pointers to the constraints they block.
2022-06-17 08:54:42 +08:00
std::unordered_map<BlockedConstraintId, std::vector<NotNull<const Constraint>>> blocked;
2022-08-05 05:27:28 +08:00
// Memoized instantiations of type aliases.
DenseHashMap<InstantiationSignature, TypeId, HashInstantiationSignature> instantiatedAliases{{}};
2022-06-17 08:54:42 +08:00
2022-08-19 05:04:33 +08:00
// Recorded errors that take place within the solver.
ErrorVec errors;
2022-09-02 07:00:14 +08:00
NotNull<ModuleResolver> moduleResolver;
std::vector<RequireCycle> requireCycles;
2022-09-09 05:44:50 +08:00
DcrLogger* logger;
2022-06-04 04:32:20 +08:00
2022-10-14 06:59:53 +08:00
explicit ConstraintSolver(NotNull<Normalizer> normalizer, NotNull<Scope> rootScope, ModuleName moduleName, NotNull<ModuleResolver> moduleResolver,
std::vector<RequireCycle> requireCycles, DcrLogger* logger);
2022-06-04 04:32:20 +08:00
2022-10-07 07:55:58 +08:00
// Randomize the order in which to dispatch constraints
void randomize(unsigned seed);
2022-06-04 04:32:20 +08:00
/**
* Attempts to dispatch all pending constraints and reach a type solution
2022-06-17 08:54:42 +08:00
* that satisfies all of the constraints.
2022-06-04 04:32:20 +08:00
**/
void run();
2022-10-14 06:59:53 +08:00
bool isDone();
void finalizeModule();
2022-06-04 04:32:20 +08:00
2022-10-07 07:55:58 +08:00
/** Attempt to dispatch a constraint. Returns true if it was successful. If
* tryDispatch() returns false, the constraint remains in the unsolved set
* and will be retried later.
2022-07-01 07:29:02 +08:00
*/
2022-06-17 08:54:42 +08:00
bool tryDispatch(NotNull<const Constraint> c, bool force);
2022-07-01 07:29:02 +08:00
2022-06-17 08:54:42 +08:00
bool tryDispatch(const SubtypeConstraint& c, NotNull<const Constraint> constraint, bool force);
bool tryDispatch(const PackSubtypeConstraint& c, NotNull<const Constraint> constraint, bool force);
bool tryDispatch(const GeneralizationConstraint& c, NotNull<const Constraint> constraint, bool force);
bool tryDispatch(const InstantiationConstraint& c, NotNull<const Constraint> constraint, bool force);
2022-07-01 07:29:02 +08:00
bool tryDispatch(const UnaryConstraint& c, NotNull<const Constraint> constraint, bool force);
bool tryDispatch(const BinaryConstraint& c, NotNull<const Constraint> constraint, bool force);
2022-09-02 07:00:14 +08:00
bool tryDispatch(const IterableConstraint& c, NotNull<const Constraint> constraint, bool force);
2022-06-24 09:44:07 +08:00
bool tryDispatch(const NameConstraint& c, NotNull<const Constraint> constraint);
2022-08-05 05:27:28 +08:00
bool tryDispatch(const TypeAliasExpansionConstraint& c, NotNull<const Constraint> constraint);
2022-09-02 07:00:14 +08:00
bool tryDispatch(const FunctionCallConstraint& c, NotNull<const Constraint> constraint);
2022-09-24 02:32:10 +08:00
bool tryDispatch(const PrimitiveTypeConstraint& c, NotNull<const Constraint> constraint);
bool tryDispatch(const HasPropConstraint& c, NotNull<const Constraint> constraint);
2022-10-22 01:33:43 +08:00
bool tryDispatch(const RefinementConstraint& c, NotNull<const Constraint> constraint);
2022-09-02 07:00:14 +08:00
// for a, ... in some_table do
2022-09-30 06:11:54 +08:00
// also handles __iter metamethod
2022-09-02 07:00:14 +08:00
bool tryDispatchIterableTable(TypeId iteratorTy, const IterableConstraint& c, NotNull<const Constraint> constraint, bool force);
// for a, ... in next_function, t, ... do
bool tryDispatchIterableFunction(
TypeId nextTy, TypeId tableTy, TypeId firstIndexTy, const IterableConstraint& c, NotNull<const Constraint> constraint, bool force);
2022-06-04 04:32:20 +08:00
2022-06-17 08:54:42 +08:00
void block(NotNull<const Constraint> target, NotNull<const Constraint> constraint);
2022-06-04 04:32:20 +08:00
/**
2022-06-17 08:54:42 +08:00
* Block a constraint on the resolution of a TypeVar.
* @returns false always. This is just to allow tryDispatch to return the result of block()
*/
bool block(TypeId target, NotNull<const Constraint> constraint);
bool block(TypePackId target, NotNull<const Constraint> constraint);
2022-06-04 04:32:20 +08:00
2022-09-24 02:32:10 +08:00
// Traverse the type. If any blocked or pending typevars are found, block
// the constraint on them.
//
// Returns false if a type blocks the constraint.
//
// FIXME: This use of a boolean for the return result is an appalling
// interface.
bool recursiveBlock(TypeId target, NotNull<const Constraint> constraint);
bool recursiveBlock(TypePackId target, NotNull<const Constraint> constraint);
2022-06-17 08:54:42 +08:00
void unblock(NotNull<const Constraint> progressed);
2022-06-04 04:32:20 +08:00
void unblock(TypeId progressed);
void unblock(TypePackId progressed);
2022-08-26 04:55:08 +08:00
void unblock(const std::vector<TypeId>& types);
void unblock(const std::vector<TypePackId>& packs);
2022-06-04 04:32:20 +08:00
2022-06-17 08:54:42 +08:00
/**
* @returns true if the TypeId is in a blocked state.
*/
bool isBlocked(TypeId ty);
2022-09-02 07:00:14 +08:00
/**
* @returns true if the TypePackId is in a blocked state.
*/
bool isBlocked(TypePackId tp);
2022-06-04 04:32:20 +08:00
/**
* Returns whether the constraint is blocked on anything.
* @param constraint the constraint to check.
*/
2022-06-17 08:54:42 +08:00
bool isBlocked(NotNull<const Constraint> constraint);
2022-06-04 04:32:20 +08:00
/**
* Creates a new Unifier and performs a single unification operation. Commits
2022-06-17 08:54:42 +08:00
* the result.
2022-06-04 04:32:20 +08:00
* @param subType the sub-type to unify.
* @param superType the super-type to unify.
*/
2022-08-19 05:04:33 +08:00
void unify(TypeId subType, TypeId superType, NotNull<Scope> scope);
2022-06-04 04:32:20 +08:00
/**
* Creates a new Unifier and performs a single unification operation. Commits
2022-06-17 08:54:42 +08:00
* the result.
2022-06-04 04:32:20 +08:00
* @param subPack the sub-type pack to unify.
* @param superPack the super-type pack to unify.
*/
2022-08-19 05:04:33 +08:00
void unify(TypePackId subPack, TypePackId superPack, NotNull<Scope> scope);
2022-06-17 08:54:42 +08:00
2022-08-05 05:27:28 +08:00
/** Pushes a new solver constraint to the solver.
* @param cv the body of the constraint.
**/
2022-09-02 07:00:14 +08:00
void pushConstraint(NotNull<Scope> scope, const Location& location, ConstraintV cv);
/**
* Attempts to resolve a module from its module information. Returns the
* module-level return type of the module, or the error type if one cannot
* be found. Reports errors to the solver if the module cannot be found or
* the require is illegal.
* @param module the module information to look up.
* @param location the location where the require is taking place; used for
* error locations.
**/
TypeId resolveModule(const ModuleInfo& module, const Location& location);
2022-08-05 05:27:28 +08:00
2022-08-19 05:04:33 +08:00
void reportError(TypeErrorData&& data, const Location& location);
void reportError(TypeError e);
2022-09-02 07:00:14 +08:00
2022-06-17 08:54:42 +08:00
private:
/**
* Marks a constraint as being blocked on a type or type pack. The constraint
* solver will not attempt to dispatch blocked constraints until their
* dependencies have made progress.
* @param target the type or type pack pointer that the constraint is blocked on.
* @param constraint the constraint to block.
**/
void block_(BlockedConstraintId target, NotNull<const Constraint> constraint);
/**
* Informs the solver that progress has been made on a type or type pack. The
* solver will wake up all constraints that are blocked on the type or type pack,
* and will resume attempting to dispatch them.
* @param progressed the type or type pack pointer that has progressed.
**/
void unblock_(BlockedConstraintId progressed);
2022-09-02 07:00:14 +08:00
2022-09-16 06:13:58 +08:00
TypeId errorRecoveryType() const;
TypePackId errorRecoveryTypePack() const;
2022-10-22 01:33:43 +08:00
TypeId unionOfTypes(TypeId a, TypeId b, NotNull<Scope> scope, bool unifyFreeTypes);
2022-09-02 07:00:14 +08:00
ToStringOptions opts;
2022-06-04 04:32:20 +08:00
};
2022-07-29 11:41:13 +08:00
void dump(NotNull<Scope> rootScope, struct ToStringOptions& opts);
2022-06-04 04:32:20 +08:00
} // namespace Luau