Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/include/Luau/Generalization.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/Scope.h"
5
#include "Luau/NotNull.h"
6
#include "Luau/TypeFwd.h"
7
8
namespace Luau
9
{
10
11
template<typename TID>
12
struct GeneralizationParams
13
{
14
bool foundOutsideFunctions = false;
15
size_t useCount = 0;
16
Polarity polarity = Polarity::None;
17
};
18
19
template<typename TID>
20
struct GeneralizationResult
21
{
22
std::optional<TID> result;
23
24
// True if the provided type was replaced with a generic.
25
bool wasReplacedByGeneric = false;
26
27
bool resourceLimitsExceeded = false;
28
29
explicit operator bool() const
30
{
31
return bool(result);
32
}
33
};
34
35
// Replace a single free type by its bounds according to the polarity provided.
36
GeneralizationResult<TypeId> generalizeType(
37
NotNull<TypeArena> arena,
38
NotNull<BuiltinTypes> builtinTypes,
39
NotNull<Scope> scope,
40
TypeId freeTy,
41
const GeneralizationParams<TypeId>& params
42
);
43
44
// Generalize one type pack
45
GeneralizationResult<TypePackId> generalizeTypePack(
46
NotNull<TypeArena> arena,
47
NotNull<BuiltinTypes> builtinTypes,
48
NotNull<Scope> scope,
49
TypePackId tp,
50
const GeneralizationParams<TypePackId>& params
51
);
52
53
void sealTable(NotNull<Scope> scope, TypeId ty);
54
55
56
/** Attempt to generalize a type.
57
*
58
* If generalizationTarget is set, then only that type will be replaced by its
59
* bounds. The way this is intended to be used is that ty is some function that
60
* is not fully generalized, and generalizationTarget is a type within its
61
* signature. There should be no further constraints that could affect the
62
* bounds of generalizationTarget.
63
*
64
* Returns nullopt if generalization failed due to resources limits.
65
*/
66
std::optional<TypeId> generalize(
67
NotNull<TypeArena> arena,
68
NotNull<BuiltinTypes> builtinTypes,
69
NotNull<Scope> scope,
70
NotNull<DenseHashSet<TypeId>> cachedTypes,
71
TypeId ty,
72
std::optional<TypeId> generalizationTarget = {}
73
);
74
75
void pruneUnnecessaryGenerics(
76
NotNull<TypeArena> arena,
77
NotNull<BuiltinTypes> builtinTypes,
78
NotNull<Scope> scope,
79
NotNull<DenseHashSet<TypeId>> cachedTypes,
80
TypeId ty
81
);
82
83
} // namespace Luau
84
85