2021-10-29 16:25:12 -04:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
2022-01-21 11:23:02 -05:00
|
|
|
#include "Luau/DenseHash.h"
|
2021-10-29 16:25:12 -04:00
|
|
|
#include "Luau/Predicate.h"
|
|
|
|
#include "Luau/Unifiable.h"
|
|
|
|
#include "Luau/Variant.h"
|
|
|
|
#include "Luau/Common.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
#include <deque>
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
LUAU_FASTINT(LuauTableTypeMaximumStringifierLength)
|
|
|
|
LUAU_FASTINT(LuauTypeMaximumStringifierLength)
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
struct TypeArena;
|
2022-06-03 16:32:20 -04:00
|
|
|
struct Scope2;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* There are three kinds of type variables:
|
|
|
|
* - `Free` variables are metavariables, which stand for unconstrained types.
|
|
|
|
* - `Bound` variables are metavariables that have an equality constraint.
|
|
|
|
* - `Generic` variables are type variables that are bound by generic functions.
|
|
|
|
*
|
|
|
|
* For example, consider the program:
|
|
|
|
* ```
|
|
|
|
* function(x, y) x.f = y end
|
|
|
|
* ```
|
|
|
|
* To typecheck this, we first introduce free metavariables for the types of `x` and `y`:
|
|
|
|
* ```
|
|
|
|
* function(x: X, y: Y) x.f = y end
|
|
|
|
* ```
|
|
|
|
* Type inference for the function body then produces the constraint:
|
|
|
|
* ```
|
|
|
|
* X = { f: Y }
|
|
|
|
* ```
|
|
|
|
* so `X` is now a bound metavariable. We can then quantify the metavariables,
|
|
|
|
* which replaces any bound metavariables by their binding, and free metavariables
|
|
|
|
* by bound generic variables:
|
|
|
|
* ```
|
|
|
|
* function<a>(x: { f: a }, y: a) x.f = y end
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
|
|
|
|
// So... why `const T*` here rather than `T*`?
|
|
|
|
// It's because we've had problems caused by the type graph being mutated
|
|
|
|
// in ways it shouldn't be, for example mutating types from other modules.
|
|
|
|
// To try to control this, we make the use of types immutable by default,
|
|
|
|
// then provide explicit mutable access via getMutable and asMutable.
|
|
|
|
// This means we can grep for all the places we're mutating the type graph,
|
|
|
|
// and it makes it possible to provide other APIs (e.g. the txn log)
|
|
|
|
// which control mutable access to the type graph.
|
|
|
|
struct TypePackVar;
|
|
|
|
using TypePackId = const TypePackVar*;
|
|
|
|
|
|
|
|
// TODO: rename to Type? CLI-39100
|
|
|
|
struct TypeVar;
|
|
|
|
|
|
|
|
// Should never be null
|
|
|
|
using TypeId = const TypeVar*;
|
|
|
|
|
|
|
|
using Name = std::string;
|
|
|
|
|
|
|
|
// A free type var is one whose exact shape has yet to be fully determined.
|
|
|
|
using FreeTypeVar = Unifiable::Free;
|
|
|
|
|
|
|
|
// When a free type var is unified with any other, it is then "bound"
|
|
|
|
// to that type var, indicating that the two types are actually the same type.
|
|
|
|
using BoundTypeVar = Unifiable::Bound<TypeId>;
|
|
|
|
|
|
|
|
using GenericTypeVar = Unifiable::Generic;
|
|
|
|
|
|
|
|
using Tags = std::vector<std::string>;
|
|
|
|
|
|
|
|
using ModuleName = std::string;
|
|
|
|
|
2022-06-16 20:54:42 -04:00
|
|
|
/** A TypeVar that cannot be computed.
|
|
|
|
*
|
|
|
|
* BlockedTypeVars essentially serve as a way to encode partial ordering on the
|
|
|
|
* constraint graph. Until a BlockedTypeVar is unblocked by its owning
|
|
|
|
* constraint, nothing at all can be said about it. Constraints that need to
|
|
|
|
* process a BlockedTypeVar cannot be dispatched.
|
|
|
|
*
|
|
|
|
* Whenever a BlockedTypeVar is added to the graph, we also record a constraint
|
|
|
|
* that will eventually unblock it.
|
|
|
|
*/
|
|
|
|
struct BlockedTypeVar
|
|
|
|
{
|
|
|
|
BlockedTypeVar();
|
|
|
|
int index;
|
|
|
|
|
|
|
|
static int nextIndex;
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
struct PrimitiveTypeVar
|
|
|
|
{
|
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
NilType, // ObjC #defines Nil :(
|
|
|
|
Boolean,
|
|
|
|
Number,
|
|
|
|
String,
|
|
|
|
Thread,
|
|
|
|
};
|
|
|
|
|
|
|
|
Type type;
|
|
|
|
std::optional<TypeId> metatable; // string has a metatable
|
|
|
|
|
|
|
|
explicit PrimitiveTypeVar(Type type)
|
|
|
|
: type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit PrimitiveTypeVar(Type type, TypeId metatable)
|
|
|
|
: type(type)
|
|
|
|
, metatable(metatable)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
struct ConstrainedTypeVar
|
|
|
|
{
|
|
|
|
explicit ConstrainedTypeVar(TypeLevel level)
|
|
|
|
: level(level)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit ConstrainedTypeVar(TypeLevel level, const std::vector<TypeId>& parts)
|
|
|
|
: parts(parts)
|
|
|
|
, level(level)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<TypeId> parts;
|
|
|
|
TypeLevel level;
|
2022-06-03 16:32:20 -04:00
|
|
|
Scope2* scope = nullptr;
|
2022-04-14 17:57:15 -04:00
|
|
|
};
|
|
|
|
|
2021-11-18 17:21:07 -05:00
|
|
|
// Singleton types https://github.com/Roblox/luau/blob/master/rfcs/syntax-singleton-types.md
|
|
|
|
// Types for true and false
|
2022-01-27 16:29:34 -05:00
|
|
|
struct BooleanSingleton
|
2021-11-18 17:21:07 -05:00
|
|
|
{
|
|
|
|
bool value;
|
|
|
|
|
2022-01-27 16:29:34 -05:00
|
|
|
bool operator==(const BooleanSingleton& rhs) const
|
2021-11-18 17:21:07 -05:00
|
|
|
{
|
|
|
|
return value == rhs.value;
|
|
|
|
}
|
|
|
|
|
2022-01-27 16:29:34 -05:00
|
|
|
bool operator!=(const BooleanSingleton& rhs) const
|
2021-11-18 17:21:07 -05:00
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Types for "foo", "bar" etc.
|
|
|
|
struct StringSingleton
|
|
|
|
{
|
|
|
|
std::string value;
|
|
|
|
|
|
|
|
bool operator==(const StringSingleton& rhs) const
|
|
|
|
{
|
|
|
|
return value == rhs.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const StringSingleton& rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// No type for float singletons, partly because === isn't any equalivalence on floats
|
|
|
|
// (NaN != NaN).
|
|
|
|
|
2022-01-27 16:29:34 -05:00
|
|
|
using SingletonVariant = Luau::Variant<BooleanSingleton, StringSingleton>;
|
2021-11-18 17:21:07 -05:00
|
|
|
|
|
|
|
struct SingletonTypeVar
|
|
|
|
{
|
|
|
|
explicit SingletonTypeVar(const SingletonVariant& variant)
|
|
|
|
: variant(variant)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit SingletonTypeVar(SingletonVariant&& variant)
|
|
|
|
: variant(std::move(variant))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default operator== is C++20.
|
|
|
|
bool operator==(const SingletonTypeVar& rhs) const
|
|
|
|
{
|
|
|
|
return variant == rhs.variant;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const SingletonTypeVar& rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
SingletonVariant variant;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T* get(const SingletonTypeVar* stv)
|
|
|
|
{
|
|
|
|
if (stv)
|
|
|
|
return get_if<T>(&stv->variant);
|
|
|
|
else
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:06:31 -05:00
|
|
|
struct GenericTypeDefinition
|
|
|
|
{
|
|
|
|
TypeId ty;
|
|
|
|
std::optional<TypeId> defaultValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GenericTypePackDefinition
|
|
|
|
{
|
|
|
|
TypePackId tp;
|
|
|
|
std::optional<TypePackId> defaultValue;
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
struct FunctionArgument
|
|
|
|
{
|
|
|
|
Name name;
|
|
|
|
Location location;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FunctionDefinition
|
|
|
|
{
|
|
|
|
std::optional<ModuleName> definitionModuleName;
|
|
|
|
Location definitionLocation;
|
|
|
|
std::optional<Location> varargLocation;
|
|
|
|
Location originalNameLocation;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Come up with a better name.
|
|
|
|
// TODO: Do we actually need this? We'll find out later if we can delete this.
|
|
|
|
// Does not exactly belong in TypeVar.h, but this is the only way to appease the compiler.
|
|
|
|
template<typename T>
|
2022-06-16 20:54:42 -04:00
|
|
|
struct WithPredicate
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
|
|
|
T type;
|
|
|
|
PredicateVec predicates;
|
|
|
|
};
|
|
|
|
|
2022-06-16 20:54:42 -04:00
|
|
|
using MagicFunction = std::function<std::optional<WithPredicate<TypePackId>>(
|
|
|
|
struct TypeChecker&, const std::shared_ptr<struct Scope>&, const class AstExprCall&, WithPredicate<TypePackId>)>;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
struct FunctionTypeVar
|
|
|
|
{
|
|
|
|
// Global monomorphic function
|
2022-06-16 20:54:42 -04:00
|
|
|
FunctionTypeVar(TypePackId argTypes, TypePackId retTypes, std::optional<FunctionDefinition> defn = {}, bool hasSelf = false);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
// Global polymorphic function
|
2022-06-16 20:54:42 -04:00
|
|
|
FunctionTypeVar(std::vector<TypeId> generics, std::vector<TypePackId> genericPacks, TypePackId argTypes, TypePackId retTypes,
|
2021-10-29 16:25:12 -04:00
|
|
|
std::optional<FunctionDefinition> defn = {}, bool hasSelf = false);
|
|
|
|
|
|
|
|
// Local monomorphic function
|
2022-06-16 20:54:42 -04:00
|
|
|
FunctionTypeVar(TypeLevel level, TypePackId argTypes, TypePackId retTypes, std::optional<FunctionDefinition> defn = {}, bool hasSelf = false);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
// Local polymorphic function
|
2022-06-16 20:54:42 -04:00
|
|
|
FunctionTypeVar(TypeLevel level, std::vector<TypeId> generics, std::vector<TypePackId> genericPacks, TypePackId argTypes, TypePackId retTypes,
|
2021-10-29 16:25:12 -04:00
|
|
|
std::optional<FunctionDefinition> defn = {}, bool hasSelf = false);
|
|
|
|
|
|
|
|
TypeLevel level;
|
2022-06-03 16:32:20 -04:00
|
|
|
Scope2* scope = nullptr;
|
2021-10-29 16:25:12 -04:00
|
|
|
/// These should all be generic
|
|
|
|
std::vector<TypeId> generics;
|
|
|
|
std::vector<TypePackId> genericPacks;
|
|
|
|
TypePackId argTypes;
|
|
|
|
std::vector<std::optional<FunctionArgument>> argNames;
|
2022-06-16 20:54:42 -04:00
|
|
|
TypePackId retTypes;
|
2021-10-29 16:25:12 -04:00
|
|
|
std::optional<FunctionDefinition> definition;
|
|
|
|
MagicFunction magicFunction = nullptr; // Function pointer, can be nullptr.
|
|
|
|
bool hasSelf;
|
|
|
|
Tags tags;
|
2022-04-14 17:57:15 -04:00
|
|
|
bool hasNoGenerics = false;
|
2021-10-29 16:25:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class TableState
|
|
|
|
{
|
|
|
|
// Sealed tables have an exact, known shape
|
|
|
|
Sealed,
|
|
|
|
|
|
|
|
// An unsealed table can have extra properties added to it
|
|
|
|
Unsealed,
|
|
|
|
|
|
|
|
// Tables which are not yet fully understood. We are still in the process of learning its shape.
|
|
|
|
Free,
|
|
|
|
|
|
|
|
// A table which is a generic parameter to a function. We know that certain properties are required,
|
|
|
|
// but we don't care about the full shape.
|
|
|
|
Generic,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TableIndexer
|
|
|
|
{
|
|
|
|
TableIndexer(TypeId indexType, TypeId indexResultType)
|
|
|
|
: indexType(indexType)
|
|
|
|
, indexResultType(indexResultType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeId indexType;
|
|
|
|
TypeId indexResultType;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Property
|
|
|
|
{
|
|
|
|
TypeId type;
|
|
|
|
bool deprecated = false;
|
|
|
|
std::string deprecatedSuggestion;
|
|
|
|
std::optional<Location> location = std::nullopt;
|
|
|
|
Tags tags;
|
|
|
|
std::optional<std::string> documentationSymbol;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TableTypeVar
|
|
|
|
{
|
|
|
|
// We choose std::map over unordered_map here just because we have unit tests that compare
|
|
|
|
// textual outputs. I don't want to spend the effort making them resilient in the case where
|
|
|
|
// random events cause the iteration order of the map elements to change.
|
|
|
|
// If this shows up in a profile, we can revisit it.
|
|
|
|
using Props = std::map<Name, Property>;
|
|
|
|
|
|
|
|
TableTypeVar() = default;
|
|
|
|
explicit TableTypeVar(TableState state, TypeLevel level);
|
2022-03-04 11:19:20 -05:00
|
|
|
TableTypeVar(const Props& props, const std::optional<TableIndexer>& indexer, TypeLevel level, TableState state);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
Props props;
|
|
|
|
std::optional<TableIndexer> indexer;
|
|
|
|
|
|
|
|
TableState state = TableState::Unsealed;
|
|
|
|
TypeLevel level;
|
2022-06-03 16:32:20 -04:00
|
|
|
Scope2* scope = nullptr;
|
2021-10-29 16:25:12 -04:00
|
|
|
std::optional<std::string> name;
|
|
|
|
|
|
|
|
// Sometimes we throw a type on a name to make for nicer error messages, but without creating any entry in the type namespace
|
|
|
|
// We need to know which is which when we stringify types.
|
|
|
|
std::optional<std::string> syntheticName;
|
|
|
|
|
|
|
|
std::vector<TypeId> instantiatedTypeParams;
|
2021-11-04 22:07:18 -04:00
|
|
|
std::vector<TypePackId> instantiatedTypePackParams;
|
2021-10-29 16:25:12 -04:00
|
|
|
ModuleName definitionModuleName;
|
|
|
|
|
|
|
|
std::optional<TypeId> boundTo;
|
|
|
|
Tags tags;
|
2022-06-30 19:29:02 -04:00
|
|
|
|
|
|
|
// Methods of this table that have an untyped self will use the same shared self type.
|
|
|
|
std::optional<TypeId> selfTy;
|
2021-10-29 16:25:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Represents a metatable attached to a table typevar. Somewhat analogous to a bound typevar.
|
|
|
|
struct MetatableTypeVar
|
|
|
|
{
|
|
|
|
// Always points to a TableTypeVar.
|
|
|
|
TypeId table;
|
|
|
|
// Always points to either a TableTypeVar or a MetatableTypeVar.
|
|
|
|
TypeId metatable;
|
|
|
|
|
|
|
|
std::optional<std::string> syntheticName;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Custom userdata of a class type
|
|
|
|
struct ClassUserData
|
|
|
|
{
|
|
|
|
virtual ~ClassUserData() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** The type of a class.
|
|
|
|
*
|
|
|
|
* Classes behave like tables in many ways, but there are some important differences:
|
|
|
|
*
|
|
|
|
* The properties of a class are always exactly known.
|
|
|
|
* Classes optionally have a parent class.
|
|
|
|
* Two different classes that share the same properties are nevertheless distinct and mutually incompatible.
|
|
|
|
*/
|
|
|
|
struct ClassTypeVar
|
|
|
|
{
|
|
|
|
using Props = TableTypeVar::Props;
|
|
|
|
|
|
|
|
Name name;
|
|
|
|
Props props;
|
|
|
|
std::optional<TypeId> parent;
|
|
|
|
std::optional<TypeId> metatable; // metaclass?
|
|
|
|
Tags tags;
|
|
|
|
std::shared_ptr<ClassUserData> userData;
|
2022-04-21 17:04:22 -04:00
|
|
|
ModuleName definitionModuleName;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-04-21 17:04:22 -04:00
|
|
|
ClassTypeVar(Name name, Props props, std::optional<TypeId> parent, std::optional<TypeId> metatable, Tags tags,
|
|
|
|
std::shared_ptr<ClassUserData> userData, ModuleName definitionModuleName)
|
2021-10-29 16:25:12 -04:00
|
|
|
: name(name)
|
|
|
|
, props(props)
|
|
|
|
, parent(parent)
|
|
|
|
, metatable(metatable)
|
|
|
|
, tags(tags)
|
|
|
|
, userData(userData)
|
2022-04-21 17:04:22 -04:00
|
|
|
, definitionModuleName(definitionModuleName)
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TypeFun
|
|
|
|
{
|
2021-11-04 22:07:18 -04:00
|
|
|
// These should all be generic
|
2022-01-14 11:06:31 -05:00
|
|
|
std::vector<GenericTypeDefinition> typeParams;
|
|
|
|
std::vector<GenericTypePackDefinition> typePackParams;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
/** The underlying type.
|
|
|
|
*
|
|
|
|
* WARNING! This is not safe to use as a type if typeParams is not empty!!
|
|
|
|
* You must first use TypeChecker::instantiateTypeFun to turn it into a real type.
|
|
|
|
*/
|
|
|
|
TypeId type;
|
2021-11-04 22:07:18 -04:00
|
|
|
|
|
|
|
TypeFun() = default;
|
2022-01-14 11:06:31 -05:00
|
|
|
TypeFun(std::vector<GenericTypeDefinition> typeParams, TypeId type)
|
2021-11-04 22:07:18 -04:00
|
|
|
: typeParams(std::move(typeParams))
|
|
|
|
, type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:06:31 -05:00
|
|
|
TypeFun(std::vector<GenericTypeDefinition> typeParams, std::vector<GenericTypePackDefinition> typePackParams, TypeId type)
|
2021-11-04 22:07:18 -04:00
|
|
|
: typeParams(std::move(typeParams))
|
|
|
|
, typePackParams(std::move(typePackParams))
|
|
|
|
, type(type)
|
|
|
|
{
|
|
|
|
}
|
2021-10-29 16:25:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Anything! All static checking is off.
|
|
|
|
struct AnyTypeVar
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UnionTypeVar
|
|
|
|
{
|
|
|
|
std::vector<TypeId> options;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IntersectionTypeVar
|
|
|
|
{
|
|
|
|
std::vector<TypeId> parts;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LazyTypeVar
|
|
|
|
{
|
|
|
|
std::function<TypeId()> thunk;
|
|
|
|
};
|
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
struct UnknownTypeVar
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NeverTypeVar
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
using ErrorTypeVar = Unifiable::Error;
|
|
|
|
|
2022-06-16 20:54:42 -04:00
|
|
|
using TypeVariant = Unifiable::Variant<TypeId, PrimitiveTypeVar, ConstrainedTypeVar, BlockedTypeVar, SingletonTypeVar, FunctionTypeVar, TableTypeVar,
|
2022-07-07 21:05:31 -04:00
|
|
|
MetatableTypeVar, ClassTypeVar, AnyTypeVar, UnionTypeVar, IntersectionTypeVar, LazyTypeVar, UnknownTypeVar, NeverTypeVar>;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
struct TypeVar final
|
|
|
|
{
|
|
|
|
explicit TypeVar(const TypeVariant& ty)
|
|
|
|
: ty(ty)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit TypeVar(TypeVariant&& ty)
|
|
|
|
: ty(std::move(ty))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeVar(const TypeVariant& ty, bool persistent)
|
|
|
|
: ty(ty)
|
|
|
|
, persistent(persistent)
|
2022-04-14 17:57:15 -04:00
|
|
|
, normal(persistent) // We assume that all persistent types are irreducable.
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:52:23 -04:00
|
|
|
// Re-assignes the content of the type, but doesn't change the owning arena and can't make type persistent.
|
|
|
|
void reassign(const TypeVar& rhs)
|
|
|
|
{
|
|
|
|
ty = rhs.ty;
|
|
|
|
normal = rhs.normal;
|
|
|
|
documentationSymbol = rhs.documentationSymbol;
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
TypeVariant ty;
|
|
|
|
|
|
|
|
// Kludge: A persistent TypeVar is one that belongs to the global scope.
|
|
|
|
// Global type bindings are immutable but are reused many times.
|
|
|
|
// Persistent TypeVars do not get cloned.
|
|
|
|
bool persistent = false;
|
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
// Normalization sets this for types that are fully normalized.
|
|
|
|
// This implies that they are transitively immutable.
|
|
|
|
bool normal = false;
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
std::optional<std::string> documentationSymbol;
|
|
|
|
|
|
|
|
// Pointer to the type arena that allocated this type.
|
|
|
|
TypeArena* owningArena = nullptr;
|
|
|
|
|
|
|
|
bool operator==(const TypeVar& rhs) const;
|
|
|
|
bool operator!=(const TypeVar& rhs) const;
|
|
|
|
|
|
|
|
TypeVar& operator=(const TypeVariant& rhs);
|
|
|
|
TypeVar& operator=(TypeVariant&& rhs);
|
2022-06-16 20:52:23 -04:00
|
|
|
|
|
|
|
TypeVar& operator=(const TypeVar& rhs);
|
2021-10-29 16:25:12 -04:00
|
|
|
};
|
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
using SeenSet = std::set<std::pair<const void*, const void*>>;
|
2021-10-29 16:25:12 -04:00
|
|
|
bool areEqual(SeenSet& seen, const TypeVar& lhs, const TypeVar& rhs);
|
|
|
|
|
|
|
|
// Follow BoundTypeVars until we get to something real
|
|
|
|
TypeId follow(TypeId t);
|
2022-01-06 17:10:07 -05:00
|
|
|
TypeId follow(TypeId t, std::function<TypeId(TypeId)> mapper);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
std::vector<TypeId> flattenIntersection(TypeId ty);
|
|
|
|
|
|
|
|
bool isPrim(TypeId ty, PrimitiveTypeVar::Type primType);
|
|
|
|
bool isNil(TypeId ty);
|
|
|
|
bool isBoolean(TypeId ty);
|
|
|
|
bool isNumber(TypeId ty);
|
|
|
|
bool isString(TypeId ty);
|
|
|
|
bool isThread(TypeId ty);
|
|
|
|
bool isOptional(TypeId ty);
|
|
|
|
bool isTableIntersection(TypeId ty);
|
|
|
|
bool isOverloadedFunction(TypeId ty);
|
|
|
|
|
2022-03-04 11:19:20 -05:00
|
|
|
// True when string is a subtype of ty
|
|
|
|
bool maybeString(TypeId ty);
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
std::optional<TypeId> getMetatable(TypeId type);
|
|
|
|
TableTypeVar* getMutableTableType(TypeId type);
|
|
|
|
const TableTypeVar* getTableType(TypeId type);
|
|
|
|
|
|
|
|
// If the type has a name, return that. Else if it has a synthetic name, return that.
|
|
|
|
// Returns nullptr if the type has no name.
|
|
|
|
const std::string* getName(TypeId type);
|
|
|
|
|
2022-03-24 17:49:08 -04:00
|
|
|
// Returns name of the module where type was defined if type has that information
|
|
|
|
std::optional<ModuleName> getDefinitionModuleName(TypeId type);
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
// Checks whether a union contains all types of another union.
|
|
|
|
bool isSubset(const UnionTypeVar& super, const UnionTypeVar& sub);
|
|
|
|
|
2021-11-04 22:42:00 -04:00
|
|
|
// Checks if a type contains generic type binders
|
2021-10-29 16:25:12 -04:00
|
|
|
bool isGeneric(const TypeId ty);
|
|
|
|
|
|
|
|
// Checks if a type may be instantiated to one containing generic type binders
|
|
|
|
bool maybeGeneric(const TypeId ty);
|
|
|
|
|
2021-11-18 17:21:07 -05:00
|
|
|
// Checks if a type is of the form T1|...|Tn where one of the Ti is a singleton
|
|
|
|
bool maybeSingleton(TypeId ty);
|
|
|
|
|
2022-01-21 11:23:02 -05:00
|
|
|
// Checks if the length operator can be applied on the value of type
|
|
|
|
bool hasLength(TypeId ty, DenseHashSet<TypeId>& seen, int* recursionCount);
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
struct SingletonTypes
|
|
|
|
{
|
2021-11-11 21:12:39 -05:00
|
|
|
const TypeId nilType;
|
|
|
|
const TypeId numberType;
|
|
|
|
const TypeId stringType;
|
|
|
|
const TypeId booleanType;
|
|
|
|
const TypeId threadType;
|
2022-04-07 16:53:47 -04:00
|
|
|
const TypeId trueType;
|
|
|
|
const TypeId falseType;
|
2021-11-11 21:12:39 -05:00
|
|
|
const TypeId anyType;
|
2022-07-07 21:05:31 -04:00
|
|
|
const TypeId unknownType;
|
|
|
|
const TypeId neverType;
|
2021-11-11 21:12:39 -05:00
|
|
|
|
|
|
|
const TypePackId anyTypePack;
|
2022-07-07 21:05:31 -04:00
|
|
|
const TypePackId neverTypePack;
|
|
|
|
const TypePackId uninhabitableTypePack;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
SingletonTypes();
|
2021-12-10 16:17:10 -05:00
|
|
|
~SingletonTypes();
|
2021-10-29 16:25:12 -04:00
|
|
|
SingletonTypes(const SingletonTypes&) = delete;
|
|
|
|
void operator=(const SingletonTypes&) = delete;
|
|
|
|
|
2021-11-18 17:21:07 -05:00
|
|
|
TypeId errorRecoveryType(TypeId guess);
|
|
|
|
TypePackId errorRecoveryTypePack(TypePackId guess);
|
|
|
|
TypeId errorRecoveryType();
|
|
|
|
TypePackId errorRecoveryTypePack();
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
private:
|
|
|
|
std::unique_ptr<struct TypeArena> arena;
|
2021-12-10 16:17:10 -05:00
|
|
|
bool debugFreezeArena = false;
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
TypeId makeStringMetatable();
|
|
|
|
};
|
|
|
|
|
2021-12-10 16:17:10 -05:00
|
|
|
SingletonTypes& getSingletonTypes();
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
void persist(TypeId ty);
|
|
|
|
void persist(TypePackId tp);
|
|
|
|
|
|
|
|
const TypeLevel* getLevel(TypeId ty);
|
|
|
|
TypeLevel* getMutableLevel(TypeId ty);
|
|
|
|
|
2022-04-14 17:57:15 -04:00
|
|
|
std::optional<TypeLevel> getLevel(TypePackId tp);
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
const Property* lookupClassProp(const ClassTypeVar* cls, const Name& name);
|
|
|
|
bool isSubclass(const ClassTypeVar* cls, const ClassTypeVar* parent);
|
|
|
|
|
|
|
|
TypeVar* asMutable(TypeId ty);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
const T* get(TypeId tv)
|
|
|
|
{
|
2021-11-11 21:12:39 -05:00
|
|
|
LUAU_ASSERT(tv);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2021-11-11 21:12:39 -05:00
|
|
|
if constexpr (!std::is_same_v<T, BoundTypeVar>)
|
|
|
|
LUAU_ASSERT(get_if<BoundTypeVar>(&tv->ty) == nullptr);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
return get_if<T>(&tv->ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T* getMutable(TypeId tv)
|
|
|
|
{
|
2021-11-11 21:12:39 -05:00
|
|
|
LUAU_ASSERT(tv);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2021-11-11 21:12:39 -05:00
|
|
|
if constexpr (!std::is_same_v<T, BoundTypeVar>)
|
|
|
|
LUAU_ASSERT(get_if<BoundTypeVar>(&tv->ty) == nullptr);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
return get_if<T>(&asMutable(tv)->ty);
|
|
|
|
}
|
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
const std::vector<TypeId>& getTypes(const UnionTypeVar* utv);
|
|
|
|
const std::vector<TypeId>& getTypes(const IntersectionTypeVar* itv);
|
|
|
|
const std::vector<TypeId>& getTypes(const ConstrainedTypeVar* ctv);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct TypeIterator;
|
|
|
|
|
|
|
|
using UnionTypeVarIterator = TypeIterator<UnionTypeVar>;
|
|
|
|
UnionTypeVarIterator begin(const UnionTypeVar* utv);
|
|
|
|
UnionTypeVarIterator end(const UnionTypeVar* utv);
|
|
|
|
|
|
|
|
using IntersectionTypeVarIterator = TypeIterator<IntersectionTypeVar>;
|
|
|
|
IntersectionTypeVarIterator begin(const IntersectionTypeVar* itv);
|
|
|
|
IntersectionTypeVarIterator end(const IntersectionTypeVar* itv);
|
|
|
|
|
|
|
|
using ConstrainedTypeVarIterator = TypeIterator<ConstrainedTypeVar>;
|
|
|
|
ConstrainedTypeVarIterator begin(const ConstrainedTypeVar* ctv);
|
|
|
|
ConstrainedTypeVarIterator end(const ConstrainedTypeVar* ctv);
|
|
|
|
|
|
|
|
/* Traverses the type T yielding each TypeId.
|
|
|
|
* If the iterator encounters a nested type T, it will instead yield each TypeId within.
|
2021-10-29 16:25:12 -04:00
|
|
|
*/
|
2022-07-07 21:05:31 -04:00
|
|
|
template<typename T>
|
|
|
|
struct TypeIterator
|
2021-10-29 16:25:12 -04:00
|
|
|
{
|
|
|
|
using value_type = Luau::TypeId;
|
|
|
|
using pointer = value_type*;
|
|
|
|
using reference = value_type&;
|
|
|
|
using difference_type = size_t;
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
explicit TypeIterator(const T* t)
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(t);
|
|
|
|
|
|
|
|
const std::vector<TypeId>& types = getTypes(t);
|
|
|
|
if (!types.empty())
|
|
|
|
stack.push_front({t, 0});
|
|
|
|
|
|
|
|
seen.insert(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeIterator<T>& operator++()
|
|
|
|
{
|
|
|
|
advance();
|
|
|
|
descend();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeIterator<T> operator++(int)
|
|
|
|
{
|
|
|
|
TypeIterator<T> copy = *this;
|
|
|
|
++copy;
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const TypeIterator<T>& rhs) const
|
|
|
|
{
|
|
|
|
if (!stack.empty() && !rhs.stack.empty())
|
|
|
|
return stack.front() == rhs.stack.front();
|
|
|
|
|
|
|
|
return stack.empty() && rhs.stack.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const TypeIterator<T>& rhs) const
|
|
|
|
{
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
const TypeId& operator*()
|
|
|
|
{
|
|
|
|
LUAU_ASSERT(!stack.empty());
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
descend();
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
auto [t, currentIndex] = stack.front();
|
|
|
|
LUAU_ASSERT(t);
|
|
|
|
const std::vector<TypeId>& types = getTypes(t);
|
|
|
|
LUAU_ASSERT(currentIndex < types.size());
|
|
|
|
|
|
|
|
const TypeId& ty = types[currentIndex];
|
|
|
|
LUAU_ASSERT(!get<T>(follow(ty)));
|
|
|
|
return ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normally, we'd have `begin` and `end` be a template but there's too much trouble
|
|
|
|
// with templates portability in this area, so not worth it. Thanks MSVC.
|
|
|
|
friend UnionTypeVarIterator end(const UnionTypeVar*);
|
|
|
|
friend IntersectionTypeVarIterator end(const IntersectionTypeVar*);
|
|
|
|
friend ConstrainedTypeVarIterator end(const ConstrainedTypeVar*);
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
private:
|
2022-07-07 21:05:31 -04:00
|
|
|
TypeIterator() = default;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
// (T* t, size_t currentIndex)
|
|
|
|
using SavedIterInfo = std::pair<const T*, size_t>;
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
std::deque<SavedIterInfo> stack;
|
2022-07-07 21:05:31 -04:00
|
|
|
std::unordered_set<const T*> seen; // Only needed to protect the iterator from hanging the thread.
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
void advance()
|
|
|
|
{
|
|
|
|
while (!stack.empty())
|
|
|
|
{
|
|
|
|
auto& [t, currentIndex] = stack.front();
|
|
|
|
++currentIndex;
|
|
|
|
|
|
|
|
const std::vector<TypeId>& types = getTypes(t);
|
|
|
|
if (currentIndex >= types.size())
|
|
|
|
stack.pop_front();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 16:25:12 -04:00
|
|
|
|
2022-07-07 21:05:31 -04:00
|
|
|
void descend()
|
|
|
|
{
|
|
|
|
while (!stack.empty())
|
|
|
|
{
|
|
|
|
auto [current, currentIndex] = stack.front();
|
|
|
|
const std::vector<TypeId>& types = getTypes(current);
|
|
|
|
if (auto inner = get<T>(follow(types[currentIndex])))
|
|
|
|
{
|
|
|
|
// If we're about to descend into a cyclic type, we should skip over this.
|
|
|
|
// Ideally this should never happen, but alas it does from time to time. :(
|
|
|
|
if (seen.find(inner) != seen.end())
|
|
|
|
advance();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
seen.insert(inner);
|
|
|
|
stack.push_front({inner, 0});
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-10-29 16:25:12 -04:00
|
|
|
|
|
|
|
using TypeIdPredicate = std::function<std::optional<TypeId>(TypeId)>;
|
|
|
|
std::vector<TypeId> filterMap(TypeId type, TypeIdPredicate predicate);
|
|
|
|
|
2021-11-04 22:42:00 -04:00
|
|
|
void attachTag(TypeId ty, const std::string& tagName);
|
|
|
|
void attachTag(Property& prop, const std::string& tagName);
|
|
|
|
|
|
|
|
bool hasTag(TypeId ty, const std::string& tagName);
|
|
|
|
bool hasTag(const Property& prop, const std::string& tagName);
|
|
|
|
bool hasTag(const Tags& tags, const std::string& tagName); // Do not use in new work.
|
|
|
|
|
2021-10-29 16:25:12 -04:00
|
|
|
} // namespace Luau
|