luau/Analysis/include/Luau/Normalize.h

392 lines
13 KiB
C
Raw Normal View History

2022-04-15 05:57:15 +08:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2022-09-09 05:44:50 +08:00
#pragma once
2022-04-15 05:57:15 +08:00
2022-08-19 05:04:33 +08:00
#include "Luau/NotNull.h"
2023-01-04 01:33:19 +08:00
#include "Luau/Type.h"
2022-10-07 07:55:58 +08:00
#include "Luau/UnifierSharedState.h"
2022-08-19 05:04:33 +08:00
#include <memory>
2022-04-15 05:57:15 +08:00
namespace Luau
{
struct InternalErrorReporter;
2022-08-19 05:04:33 +08:00
struct Module;
struct Scope;
2023-01-04 01:33:19 +08:00
struct BuiltinTypes;
2022-08-19 05:04:33 +08:00
using ModulePtr = std::shared_ptr<Module>;
2022-04-15 05:57:15 +08:00
2023-01-04 01:33:19 +08:00
bool isSubtype(TypeId subTy, TypeId superTy, NotNull<Scope> scope, NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter& ice);
bool isSubtype(TypePackId subTy, TypePackId superTy, NotNull<Scope> scope, NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter& ice);
2023-03-17 22:59:30 +08:00
bool isConsistentSubtype(TypeId subTy, TypeId superTy, NotNull<Scope> scope, NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter& ice);
bool isConsistentSubtype(TypePackId subTy, TypePackId superTy, NotNull<Scope> scope, NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter& ice);
2022-09-09 05:44:50 +08:00
2022-10-07 07:55:58 +08:00
class TypeIds
{
private:
std::unordered_set<TypeId> types;
std::vector<TypeId> order;
std::size_t hash = 0;
public:
using iterator = std::vector<TypeId>::iterator;
using const_iterator = std::vector<TypeId>::const_iterator;
2023-01-04 01:33:19 +08:00
TypeIds(const TypeIds&) = default;
2022-10-07 07:55:58 +08:00
TypeIds(TypeIds&&) = default;
TypeIds() = default;
~TypeIds() = default;
TypeIds& operator=(TypeIds&&) = default;
void insert(TypeId ty);
/// Erase every element that does not also occur in tys
void retain(const TypeIds& tys);
void clear();
2022-12-10 02:07:25 +08:00
TypeId front() const;
2022-10-07 07:55:58 +08:00
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
iterator erase(const_iterator it);
size_t size() const;
bool empty() const;
size_t count(TypeId ty) const;
template<class Iterator>
void insert(Iterator begin, Iterator end)
{
for (Iterator it = begin; it != end; ++it)
insert(*it);
}
2022-10-14 06:59:53 +08:00
bool operator==(const TypeIds& there) const;
2022-10-07 07:55:58 +08:00
size_t getHash() const;
2023-06-09 20:20:36 +08:00
bool isNever() const;
2022-10-07 07:55:58 +08:00
};
} // namespace Luau
2022-10-14 06:59:53 +08:00
template<>
struct std::hash<Luau::TypeIds>
2022-10-07 07:55:58 +08:00
{
std::size_t operator()(const Luau::TypeIds& tys) const
{
return tys.getHash();
}
};
2022-10-14 06:59:53 +08:00
template<>
struct std::hash<const Luau::TypeIds*>
2022-10-07 07:55:58 +08:00
{
std::size_t operator()(const Luau::TypeIds* tys) const
{
return tys->getHash();
}
};
2022-10-14 06:59:53 +08:00
template<>
struct std::equal_to<Luau::TypeIds>
2022-10-07 07:55:58 +08:00
{
bool operator()(const Luau::TypeIds& here, const Luau::TypeIds& there) const
{
return here == there;
}
};
2022-10-14 06:59:53 +08:00
template<>
struct std::equal_to<const Luau::TypeIds*>
2022-10-07 07:55:58 +08:00
{
bool operator()(const Luau::TypeIds* here, const Luau::TypeIds* there) const
{
return *here == *there;
}
};
namespace Luau
{
2022-10-28 06:22:49 +08:00
/** A normalized string type is either `string` (represented by `nullopt`) or a
* union of string singletons.
*
2022-12-10 02:07:25 +08:00
* The representation is as follows:
2022-10-28 06:22:49 +08:00
*
* * A union of string singletons is finite and includes the singletons named by
* the `singletons` field.
* * An intersection of negated string singletons is cofinite and includes the
* singletons excluded by the `singletons` field. It is implied that cofinite
* values are exclusions from `string` itself.
* * The `string` data type is a cofinite set minus zero elements.
* * The `never` data type is a finite set plus zero elements.
*/
struct NormalizedStringType
{
// When false, this type represents a union of singleton string types.
// eg "a" | "b" | "c"
//
// When true, this type represents string intersected with negated string
// singleton types.
// eg string & ~"a" & ~"b" & ...
bool isCofinite = false;
2022-12-10 02:07:25 +08:00
std::map<std::string, TypeId> singletons;
2022-10-28 06:22:49 +08:00
void resetToString();
void resetToNever();
bool isNever() const;
bool isString() const;
/// Returns true if the string has finite domain.
///
/// Important subtlety: This method returns true for `never`. The empty set
/// is indeed an empty set.
bool isUnion() const;
/// Returns true if the string has infinite domain.
bool isIntersection() const;
bool includes(const std::string& str) const;
static const NormalizedStringType never;
2022-12-10 02:07:25 +08:00
NormalizedStringType();
NormalizedStringType(bool isCofinite, std::map<std::string, TypeId> singletons);
2022-10-28 06:22:49 +08:00
};
bool isSubtype(const NormalizedStringType& subStr, const NormalizedStringType& superStr);
2022-10-07 07:55:58 +08:00
2023-01-04 01:33:19 +08:00
struct NormalizedClassType
{
/** Has the following structure:
*
* (C1 & ~N11 & ... & ~Nn) | (C2 & ~N21 & ... & ~N2n) | ...
*
* C2 is either not a subtype of any other Cm, or it is and is also a
* subtype of one of Nmn types within the same cluster.
*
* Each TypeId is a class type.
*/
std::unordered_map<TypeId, TypeIds> classes;
/**
* In order to maintain a consistent insertion order, we use this vector to
* keep track of it. An ordered std::map will sort by pointer identity,
* which is undesirable.
*/
std::vector<TypeId> ordering;
void pushPair(TypeId ty, TypeIds negations);
void resetToNever();
bool isNever() const;
};
2022-11-05 01:02:37 +08:00
// A normalized function type can be `never`, the top function type `function`,
2022-10-07 07:55:58 +08:00
// or an intersection of function types.
2022-11-05 01:02:37 +08:00
//
// NOTE: type normalization can fail on function types with generics (e.g.
// because we do not support unions and intersections of generic type packs), so
// this type may contain `error`.
struct NormalizedFunctionType
{
bool isTop = false;
2023-03-31 20:21:14 +08:00
TypeIds parts;
2022-11-05 01:02:37 +08:00
void resetToNever();
void resetToTop();
bool isNever() const;
};
2022-10-07 07:55:58 +08:00
// A normalized generic/free type is a union, where each option is of the form (X & T) where
2023-03-17 22:59:30 +08:00
// * X is either a free type, a generic or a blocked type.
2022-10-07 07:55:58 +08:00
// * T is a normalized type.
struct NormalizedType;
using NormalizedTyvars = std::unordered_map<TypeId, std::unique_ptr<NormalizedType>>;
// A normalized type is either any, unknown, or one of the form P | T | F | G where
// * P is a union of primitive types (including singletons, classes and the error type)
// * T is a union of table types
// * F is a union of an intersection of function types
2023-03-17 22:59:30 +08:00
// * G is a union of generic/free/blocked types, intersected with a normalized type
2022-10-07 07:55:58 +08:00
struct NormalizedType
{
// The top part of the type.
// This type is either never, unknown, or any.
// If this type is not never, all the other fields are null.
TypeId tops;
// The boolean part of the type.
// This type is either never, boolean type, or a boolean singleton.
TypeId booleans;
2023-01-04 01:33:19 +08:00
NormalizedClassType classes;
2022-10-07 07:55:58 +08:00
// The error part of the type.
// This type is either never or the error type.
TypeId errors;
// The nil part of the type.
// This type is either never or nil.
TypeId nils;
// The number part of the type.
// This type is either never or number.
TypeId numbers;
// The string part of the type.
// This may be the `string` type, or a union of singletons.
2022-10-28 06:22:49 +08:00
NormalizedStringType strings;
2022-10-07 07:55:58 +08:00
// The thread part of the type.
// This type is either never or thread.
TypeId threads;
// The (meta)table part of the type.
2023-01-28 05:28:45 +08:00
// Each element of this set is a (meta)table type, or the top `table` type.
// An empty set denotes never.
2022-10-07 07:55:58 +08:00
TypeIds tables;
// The function part of the type.
NormalizedFunctionType functions;
// The generic/free part of the type.
NormalizedTyvars tyvars;
2023-01-04 01:33:19 +08:00
NormalizedType(NotNull<BuiltinTypes> builtinTypes);
2022-10-07 07:55:58 +08:00
NormalizedType() = delete;
~NormalizedType() = default;
2022-11-05 01:02:37 +08:00
NormalizedType(const NormalizedType&) = delete;
NormalizedType& operator=(const NormalizedType&) = delete;
NormalizedType(NormalizedType&&) = default;
2022-10-07 07:55:58 +08:00
NormalizedType& operator=(NormalizedType&&) = default;
2023-05-20 02:59:59 +08:00
// IsType functions
2023-06-09 20:20:36 +08:00
/// Returns true if the type is exactly a number. Behaves like Type::isNumber()
bool isExactlyNumber() const;
/// Returns true if the type is a subtype of string(it could be a singleton). Behaves like Type::isString()
bool isSubtypeOfString() const;
2023-07-08 01:14:35 +08:00
/// Returns true if this type should result in error suppressing behavior.
bool shouldSuppressErrors() const;
2023-07-28 19:37:00 +08:00
/// Returns true if this type contains the primitve top table type, `table`.
bool hasTopTable() const;
2023-06-09 20:20:36 +08:00
// Helpers that improve readability of the above (they just say if the component is present)
bool hasTops() const;
bool hasBooleans() const;
bool hasClasses() const;
bool hasErrors() const;
bool hasNils() const;
bool hasNumbers() const;
bool hasStrings() const;
bool hasThreads() const;
bool hasTables() const;
bool hasFunctions() const;
bool hasTyvars() const;
2022-10-07 07:55:58 +08:00
};
2023-05-20 02:59:59 +08:00
2022-10-07 07:55:58 +08:00
class Normalizer
{
std::unordered_map<TypeId, std::unique_ptr<NormalizedType>> cachedNormals;
std::unordered_map<const TypeIds*, TypeId> cachedIntersections;
std::unordered_map<const TypeIds*, TypeId> cachedUnions;
std::unordered_map<const TypeIds*, std::unique_ptr<TypeIds>> cachedTypeIds;
2023-05-26 04:46:51 +08:00
DenseHashMap<TypeId, bool> cachedIsInhabited{nullptr};
DenseHashMap<std::pair<TypeId, TypeId>, bool, TypeIdPairHash> cachedIsInhabitedIntersection{{nullptr, nullptr}};
2022-10-07 07:55:58 +08:00
bool withinResourceLimits();
public:
TypeArena* arena;
2023-01-04 01:33:19 +08:00
NotNull<BuiltinTypes> builtinTypes;
2022-10-07 07:55:58 +08:00
NotNull<UnifierSharedState> sharedState;
2023-05-26 04:46:51 +08:00
bool cacheInhabitance = false;
2022-10-07 07:55:58 +08:00
2023-05-26 04:46:51 +08:00
Normalizer(TypeArena* arena, NotNull<BuiltinTypes> builtinTypes, NotNull<UnifierSharedState> sharedState, bool cacheInhabitance = false);
2022-10-07 07:55:58 +08:00
Normalizer(const Normalizer&) = delete;
Normalizer(Normalizer&&) = delete;
Normalizer() = delete;
~Normalizer() = default;
Normalizer& operator=(Normalizer&&) = delete;
Normalizer& operator=(Normalizer&) = delete;
// If this returns null, the typechecker should emit a "too complex" error
const NormalizedType* normalize(TypeId ty);
void clearNormal(NormalizedType& norm);
// ------- Cached TypeIds
TypeId unionType(TypeId here, TypeId there);
TypeId intersectionType(TypeId here, TypeId there);
const TypeIds* cacheTypeIds(TypeIds tys);
void clearCaches();
// ------- Normalizing unions
void unionTysWithTy(TypeIds& here, TypeId there);
TypeId unionOfTops(TypeId here, TypeId there);
TypeId unionOfBools(TypeId here, TypeId there);
void unionClassesWithClass(TypeIds& heres, TypeId there);
void unionClasses(TypeIds& heres, const TypeIds& theres);
2023-01-04 01:33:19 +08:00
void unionClassesWithClass(NormalizedClassType& heres, TypeId there);
void unionClasses(NormalizedClassType& heres, const NormalizedClassType& theres);
2022-10-07 07:55:58 +08:00
void unionStrings(NormalizedStringType& here, const NormalizedStringType& there);
std::optional<TypePackId> unionOfTypePacks(TypePackId here, TypePackId there);
std::optional<TypeId> unionOfFunctions(TypeId here, TypeId there);
std::optional<TypeId> unionSaturatedFunctions(TypeId here, TypeId there);
void unionFunctionsWithFunction(NormalizedFunctionType& heress, TypeId there);
void unionFunctions(NormalizedFunctionType& heress, const NormalizedFunctionType& theress);
void unionTablesWithTable(TypeIds& heres, TypeId there);
void unionTables(TypeIds& heres, const TypeIds& theres);
bool unionNormals(NormalizedType& here, const NormalizedType& there, int ignoreSmallerTyvars = -1);
2023-07-08 01:14:35 +08:00
bool unionNormalWithTy(NormalizedType& here, TypeId there, std::unordered_set<TypeId>& seenSetTypes, int ignoreSmallerTyvars = -1);
2022-10-07 07:55:58 +08:00
2022-10-28 06:22:49 +08:00
// ------- Negations
2022-11-05 01:02:37 +08:00
std::optional<NormalizedType> negateNormal(const NormalizedType& here);
2022-10-28 06:22:49 +08:00
TypeIds negateAll(const TypeIds& theres);
TypeId negate(TypeId there);
void subtractPrimitive(NormalizedType& here, TypeId ty);
void subtractSingleton(NormalizedType& here, TypeId ty);
2022-10-07 07:55:58 +08:00
// ------- Normalizing intersections
TypeId intersectionOfTops(TypeId here, TypeId there);
TypeId intersectionOfBools(TypeId here, TypeId there);
2023-01-04 01:33:19 +08:00
void intersectClasses(NormalizedClassType& heres, const NormalizedClassType& theres);
void intersectClassesWithClass(NormalizedClassType& heres, TypeId there);
2022-10-07 07:55:58 +08:00
void intersectStrings(NormalizedStringType& here, const NormalizedStringType& there);
std::optional<TypePackId> intersectionOfTypePacks(TypePackId here, TypePackId there);
std::optional<TypeId> intersectionOfTables(TypeId here, TypeId there);
void intersectTablesWithTable(TypeIds& heres, TypeId there);
void intersectTables(TypeIds& heres, const TypeIds& theres);
std::optional<TypeId> intersectionOfFunctions(TypeId here, TypeId there);
void intersectFunctionsWithFunction(NormalizedFunctionType& heress, TypeId there);
void intersectFunctions(NormalizedFunctionType& heress, const NormalizedFunctionType& theress);
2023-07-08 01:14:35 +08:00
bool intersectTyvarsWithTy(NormalizedTyvars& here, TypeId there, std::unordered_set<TypeId>& seenSetTypes);
2022-10-07 07:55:58 +08:00
bool intersectNormals(NormalizedType& here, const NormalizedType& there, int ignoreSmallerTyvars = -1);
2023-07-08 01:14:35 +08:00
bool intersectNormalWithTy(NormalizedType& here, TypeId there, std::unordered_set<TypeId>& seenSetTypes);
2023-04-28 19:55:55 +08:00
bool normalizeIntersections(const std::vector<TypeId>& intersections, NormalizedType& outType);
2022-10-07 07:55:58 +08:00
2022-12-02 18:46:05 +08:00
// Check for inhabitance
2023-05-26 04:46:51 +08:00
bool isInhabited(TypeId ty);
bool isInhabited(TypeId ty, std::unordered_set<TypeId> seen);
2022-12-02 18:46:05 +08:00
bool isInhabited(const NormalizedType* norm, std::unordered_set<TypeId> seen = {});
2023-05-26 04:46:51 +08:00
2023-04-28 19:55:55 +08:00
// Check for intersections being inhabited
bool isIntersectionInhabited(TypeId left, TypeId right);
2022-12-02 18:46:05 +08:00
2022-10-07 07:55:58 +08:00
// -------- Convert back from a normalized type to a type
TypeId typeFromNormal(const NormalizedType& norm);
};
2022-04-15 05:57:15 +08:00
} // namespace Luau