Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/include/Luau/Module.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/Error.h"
5
#include "Luau/Linter.h"
6
#include "Luau/FileResolver.h"
7
#include "Luau/ParseOptions.h"
8
#include "Luau/ParseResult.h"
9
#include "Luau/Scope.h"
10
#include "Luau/TypeArena.h"
11
#include "Luau/DataFlowGraph.h"
12
13
#include <memory>
14
#include <vector>
15
#include <unordered_map>
16
#include <optional>
17
18
namespace Luau
19
{
20
21
using LogLuauProc = void (*)(std::string_view, std::string_view);
22
extern LogLuauProc logLuau;
23
24
void setLogLuau(LogLuauProc ll);
25
void resetLogLuauProc();
26
27
struct Module;
28
29
using ScopePtr = std::shared_ptr<struct Scope>;
30
using ModulePtr = std::shared_ptr<Module>;
31
32
class AstType;
33
class AstTypePack;
34
35
/// Root of the AST of a parsed source file
36
struct SourceModule
37
{
38
ModuleName name; // Module identifier or a filename
39
std::string humanReadableName;
40
41
SourceCode::Type type = SourceCode::None;
42
std::optional<std::string> environmentName;
43
bool cyclic = false;
44
45
std::shared_ptr<Allocator> allocator;
46
std::shared_ptr<AstNameTable> names;
47
std::vector<ParseError> parseErrors;
48
49
AstStatBlock* root = nullptr;
50
std::optional<Mode> mode;
51
52
std::vector<HotComment> hotcomments;
53
std::vector<Comment> commentLocations;
54
55
SourceModule()
56
: allocator(new Allocator)
57
, names(new AstNameTable(*allocator))
58
{
59
}
60
};
61
62
bool isWithinComment(const std::vector<Comment>& commentLocations, Position pos);
63
bool isWithinComment(const SourceModule& sourceModule, Position pos);
64
bool isWithinComment(const ParseResult& result, Position pos);
65
66
bool isWithinHotComment(const std::vector<HotComment>& hotComments, Position pos);
67
bool isWithinHotComment(const SourceModule& sourceModule, Position pos);
68
bool isWithinHotComment(const ParseResult& result, Position pos);
69
70
struct RequireCycle
71
{
72
Location location;
73
std::vector<ModuleName> path; // one of the paths for a require() to go all the way back to the originating module
74
};
75
76
struct Module
77
{
78
~Module();
79
80
// TODO: Clip this when we clip FFlagLuauSolverV2
81
bool checkedInNewSolver = false;
82
83
ModuleName name;
84
std::string humanReadableName;
85
86
TypeArena interfaceTypes;
87
TypeArena internalTypes;
88
89
// Scopes and AST types refer to parse data, so we need to keep that alive
90
std::shared_ptr<Allocator> allocator;
91
std::shared_ptr<AstNameTable> names;
92
AstStatBlock* root = nullptr;
93
94
std::vector<std::pair<Location, ScopePtr>> scopes; // never empty
95
96
DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr};
97
DenseHashMap<const AstExpr*, TypePackId> astTypePacks{nullptr};
98
DenseHashMap<const AstExpr*, TypeId> astExpectedTypes{nullptr};
99
100
// For AST nodes that are function calls, this map provides the
101
// unspecialized type of the function that was called. If a function call
102
// resolves to a __call metamethod application, this map will point at that
103
// metamethod.
104
//
105
// This is useful for type checking and Signature Help.
106
DenseHashMap<const AstNode*, TypeId> astOriginalCallTypes{nullptr};
107
108
// The specialization of a function that was selected. If the function is
109
// generic, those generic type parameters will be replaced with the actual
110
// types that were passed. If the function is an overload, this map will
111
// point at the specific overloads that were selected.
112
DenseHashMap<const AstNode*, TypeId> astOverloadResolvedTypes{nullptr};
113
114
// Only used with for...in loops. The computed type of the next() function
115
// is kept here for type checking.
116
DenseHashMap<const AstNode*, TypeId> astForInNextTypes{nullptr};
117
118
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};
119
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};
120
121
// The computed result type of a compound assignment. (eg foo += 1)
122
//
123
// Type checking uses this to check that the result of such an operation is
124
// actually compatible with the left-side operand.
125
DenseHashMap<const AstStat*, TypeId> astCompoundAssignResultTypes{nullptr};
126
127
DenseHashMap<TypeId, std::vector<std::pair<Location, TypeId>>> upperBoundContributors{nullptr};
128
129
// Map AST nodes to the scope they create. Cannot be NotNull<Scope> because
130
// we need a sentinel value for the map.
131
DenseHashMap<const AstNode*, Scope*> astScopes{nullptr};
132
133
// Stable references for type aliases registered in the environment
134
std::vector<std::unique_ptr<TypeFun>> typeFunctionAliases;
135
136
std::unordered_map<Name, TypeId> declaredGlobals;
137
ErrorVec errors;
138
LintResult lintResult;
139
Mode mode;
140
SourceCode::Type type;
141
double checkDurationSec = 0.0;
142
bool timeout = false;
143
bool cancelled = false;
144
145
TypePackId returnType = nullptr;
146
std::unordered_map<Name, TypeFun> exportedTypeBindings;
147
148
// Arenas related to the DFG must persist after the DFG no longer exists, as
149
// Module objects maintain raw pointers to objects in these arenas.
150
DefArena defArena;
151
RefinementKeyArena keyArena;
152
153
bool hasModuleScope() const;
154
ScopePtr getModuleScope() const;
155
156
// Once a module has been typechecked, we clone its public interface into a
157
// separate arena. This helps us to force Type ownership into a DAG rather
158
// than a DCG.
159
void clonePublicInterface(NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter& ice, SolverMode mode);
160
161
bool constraintGenerationDidNotComplete = true;
162
};
163
164
} // namespace Luau
165
166