Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/include/Luau/Instantiation2.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/NotNull.h"
5
#include "Luau/Substitution.h"
6
#include "Luau/Subtyping.h"
7
#include "Luau/TxnLog.h"
8
#include "Luau/TypeFwd.h"
9
#include "Luau/Unifiable.h"
10
11
namespace Luau
12
{
13
14
struct TypeArena;
15
struct TypeCheckLimits;
16
17
struct Replacer_DEPRECATED : Substitution
18
{
19
DenseHashMap<TypeId, TypeId> replacements;
20
DenseHashMap<TypePackId, TypePackId> replacementPacks;
21
22
Replacer_DEPRECATED(NotNull<TypeArena> arena, DenseHashMap<TypeId, TypeId> replacements, DenseHashMap<TypePackId, TypePackId> replacementPacks)
23
: Substitution(TxnLog::empty(), arena)
24
, replacements(std::move(replacements))
25
, replacementPacks(std::move(replacementPacks))
26
{
27
}
28
29
bool isDirty(TypeId ty) override
30
{
31
return replacements.find(ty) != nullptr;
32
}
33
34
bool isDirty(TypePackId tp) override
35
{
36
return replacementPacks.find(tp) != nullptr;
37
}
38
39
TypeId clean(TypeId ty) override
40
{
41
TypeId res = replacements[ty];
42
LUAU_ASSERT(res);
43
dontTraverseInto(res);
44
return res;
45
}
46
47
TypePackId clean(TypePackId tp) override
48
{
49
TypePackId res = replacementPacks[tp];
50
LUAU_ASSERT(res);
51
dontTraverseInto(res);
52
return res;
53
}
54
};
55
56
struct Replacer : Substitution
57
{
58
NotNull<DenseHashMap<TypeId, TypeId>> replacements;
59
NotNull<DenseHashMap<TypePackId, TypePackId>> replacementPacks;
60
61
Replacer(
62
NotNull<TypeArena> arena,
63
NotNull<DenseHashMap<TypeId, TypeId>> replacements,
64
NotNull<DenseHashMap<TypePackId, TypePackId>> replacementPacks
65
);
66
67
bool isDirty(TypeId ty) override;
68
69
bool isDirty(TypePackId tp) override;
70
71
TypeId clean(TypeId ty) override;
72
73
TypePackId clean(TypePackId tp) override;
74
75
bool ignoreChildren(TypeId ty) override;
76
77
private:
78
/**
79
* It is *very* easy to create the world's worst bug by using a bound type
80
* as key: this is a helper function we run in debug mode to confirm this
81
* isn't the case.
82
*/
83
bool checkReplacementKeys() const;
84
};
85
86
// A substitution which replaces generic functions by monomorphic functions
87
struct Instantiation2 final : Substitution
88
{
89
// Mapping from generic types to free types to be used in instantiation.
90
DenseHashMap<TypeId, TypeId> genericSubstitutions{nullptr};
91
// Mapping from generic type packs to `TypePack`s of free types to be used in instantiation.
92
DenseHashMap<TypePackId, TypePackId> genericPackSubstitutions{nullptr};
93
94
// Make `NotNull` with LuauInstantiationUsesGenericPolarity
95
Subtyping* subtyping = nullptr;
96
Scope* scope = nullptr;
97
98
Instantiation2(TypeArena* arena, DenseHashMap<TypeId, TypeId> genericSubstitutions, DenseHashMap<TypePackId, TypePackId> genericPackSubstitutions)
99
: Substitution(TxnLog::empty(), arena)
100
, genericSubstitutions(std::move(genericSubstitutions))
101
, genericPackSubstitutions(std::move(genericPackSubstitutions))
102
{
103
}
104
105
Instantiation2(
106
TypeArena* arena,
107
DenseHashMap<TypeId, TypeId> genericSubstitutions,
108
DenseHashMap<TypePackId, TypePackId> genericPackSubstitutions,
109
NotNull<Subtyping> subtyping,
110
NotNull<Scope> scope
111
)
112
: Substitution(TxnLog::empty(), arena)
113
, genericSubstitutions(std::move(genericSubstitutions))
114
, genericPackSubstitutions(std::move(genericPackSubstitutions))
115
, subtyping(subtyping)
116
, scope(scope)
117
{
118
}
119
120
bool ignoreChildren(TypeId ty) override;
121
bool isDirty(TypeId ty) override;
122
bool isDirty(TypePackId tp) override;
123
TypeId clean(TypeId ty) override;
124
TypePackId clean(TypePackId tp) override;
125
};
126
127
std::optional<TypeId> instantiate2(
128
TypeArena* arena,
129
DenseHashMap<TypeId, TypeId> genericSubstitutions,
130
DenseHashMap<TypePackId, TypePackId> genericPackSubstitutions,
131
NotNull<Subtyping> subtyping,
132
NotNull<Scope> scope,
133
TypeId ty
134
);
135
136
std::optional<TypePackId> instantiate2(
137
TypeArena* arena,
138
DenseHashMap<TypeId, TypeId> genericSubstitutions,
139
DenseHashMap<TypePackId, TypePackId> genericPackSubstitutions,
140
NotNull<Subtyping> subtyping,
141
NotNull<Scope> scope,
142
TypePackId tp
143
);
144
145
} // namespace Luau
146
147