Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/include/Luau/ModuleResolver.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/FileResolver.h"
5
6
#include <memory>
7
#include <optional>
8
#include <string>
9
10
namespace Luau
11
{
12
13
class AstExpr;
14
struct Module;
15
16
using ModulePtr = std::shared_ptr<Module>;
17
18
struct ModuleResolver
19
{
20
virtual ~ModuleResolver() {}
21
22
/** Compute a ModuleName from an AST fragment. This AST fragment is generally the argument to the require() function.
23
*
24
* @returns The ModuleInfo if the expression is a syntactically legal path.
25
* @returns std::nullopt if we are unable to determine whether or not the expression is a valid path. Type inference will
26
* silently assume that it could succeed in this case.
27
*
28
* FIXME: This is clearly not the right behaviour longterm. We'll want to adust this interface to be able to signal
29
* a) success,
30
* b) Definitive failure (this expression will absolutely cause require() to fail at runtime), and
31
* c) uncertainty
32
*/
33
virtual std::optional<ModuleInfo> resolveModuleInfo(const ModuleName& currentModuleName, const AstExpr& pathExpr) = 0;
34
35
/** Get a typechecked module from its name.
36
*
37
* This can return null under two circumstances: the module is unknown at compile time,
38
* or there's a cycle, and we are still in the middle of typechecking the module.
39
*/
40
virtual const ModulePtr getModule(const ModuleName& moduleName) const = 0;
41
42
/** Is a module known at compile time?
43
*
44
* This function can be used to distinguish the above two cases.
45
*/
46
virtual bool moduleExists(const ModuleName& moduleName) const = 0;
47
48
virtual std::string getHumanReadableModuleName(const ModuleName& moduleName) const = 0;
49
};
50
51
struct NullModuleResolver : ModuleResolver
52
{
53
std::optional<ModuleInfo> resolveModuleInfo(const ModuleName& currentModuleName, const AstExpr& pathExpr) override
54
{
55
return std::nullopt;
56
}
57
const ModulePtr getModule(const ModuleName& moduleName) const override
58
{
59
return nullptr;
60
}
61
bool moduleExists(const ModuleName& moduleName) const override
62
{
63
return false;
64
}
65
std::string getHumanReadableModuleName(const ModuleName& moduleName) const override
66
{
67
return moduleName;
68
}
69
};
70
71
} // namespace Luau
72
73