2021-10-29 21:25:12 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/Config.h"
|
2023-09-08 01:13:49 +01:00
|
|
|
#include "Luau/GlobalTypes.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "Luau/Module.h"
|
|
|
|
#include "Luau/ModuleResolver.h"
|
|
|
|
#include "Luau/RequireTracer.h"
|
2022-07-01 00:52:43 +01:00
|
|
|
#include "Luau/Scope.h"
|
2023-07-28 16:13:53 +01:00
|
|
|
#include "Luau/TypeCheckLimits.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "Luau/Variant.h"
|
2023-04-21 23:14:26 +01:00
|
|
|
|
|
|
|
#include <mutex>
|
2021-10-29 21:25:12 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
class AstStat;
|
|
|
|
class ParseError;
|
|
|
|
struct Frontend;
|
|
|
|
struct TypeError;
|
|
|
|
struct LintWarning;
|
2023-03-10 20:21:07 +00:00
|
|
|
struct GlobalTypes;
|
2021-10-29 21:25:12 +01:00
|
|
|
struct TypeChecker;
|
|
|
|
struct FileResolver;
|
|
|
|
struct ModuleResolver;
|
|
|
|
struct ParseResult;
|
2022-02-18 01:18:01 +00:00
|
|
|
struct HotComment;
|
2023-05-05 22:52:49 +01:00
|
|
|
struct BuildQueueItem;
|
2023-07-14 19:08:53 +01:00
|
|
|
struct FrontendCancellationToken;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
struct LoadDefinitionFileResult
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
ParseResult parseResult;
|
2023-03-10 20:21:07 +00:00
|
|
|
SourceModule sourceModule;
|
2021-10-29 21:25:12 +01:00
|
|
|
ModulePtr module;
|
|
|
|
};
|
|
|
|
|
2022-02-18 01:18:01 +00:00
|
|
|
std::optional<Mode> parseMode(const std::vector<HotComment>& hotcomments);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
std::vector<std::string_view> parsePathExpr(const AstExpr& pathExpr);
|
|
|
|
|
|
|
|
// Exported only for convenient testing.
|
|
|
|
std::optional<ModuleName> pathExprToModuleName(const ModuleName& currentModuleName, const std::vector<std::string_view>& expr);
|
|
|
|
|
|
|
|
/** Try to convert an AST fragment into a ModuleName.
|
|
|
|
* Returns std::nullopt if the expression cannot be resolved. This will most likely happen in cases where
|
|
|
|
* the import path involves some dynamic computation that we cannot see into at typechecking time.
|
|
|
|
*
|
|
|
|
* Unintuitively, weirdly-formulated modules (like game.Parent.Parent.Parent.Foo) will successfully produce a ModuleName
|
|
|
|
* as long as it falls within the permitted syntax. This is ok because we will fail to find the module and produce an
|
|
|
|
* error when we try during typechecking.
|
|
|
|
*/
|
|
|
|
std::optional<ModuleName> pathExprToModuleName(const ModuleName& currentModuleName, const AstExpr& expr);
|
2023-04-28 20:55:13 +01:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
struct SourceNode
|
|
|
|
{
|
2022-04-21 22:44:27 +01:00
|
|
|
bool hasDirtySourceModule() const
|
|
|
|
{
|
|
|
|
return dirtySourceModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasDirtyModule(bool forAutocomplete) const
|
2022-04-07 22:29:01 +01:00
|
|
|
{
|
2022-05-26 23:08:16 +01:00
|
|
|
return forAutocomplete ? dirtyModuleForAutocomplete : dirtyModule;
|
2022-04-07 22:29:01 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
ModuleName name;
|
2023-04-21 23:14:26 +01:00
|
|
|
std::string humanReadableName;
|
2023-11-10 21:10:07 +00:00
|
|
|
DenseHashSet<ModuleName> requireSet{{}};
|
2021-10-29 21:25:12 +01:00
|
|
|
std::vector<std::pair<ModuleName, Location>> requireLocations;
|
2022-04-21 22:44:27 +01:00
|
|
|
bool dirtySourceModule = true;
|
|
|
|
bool dirtyModule = true;
|
|
|
|
bool dirtyModuleForAutocomplete = true;
|
2022-04-15 00:57:43 +01:00
|
|
|
double autocompleteLimitsMult = 1.0;
|
2021-10-29 21:25:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FrontendOptions
|
|
|
|
{
|
|
|
|
// When true, we retain full type information about every term in the AST.
|
|
|
|
// Setting this to false cuts back on RAM and is a good idea for batch
|
|
|
|
// jobs where the type graph is not deeply inspected after typechecking
|
|
|
|
// is complete.
|
|
|
|
bool retainFullTypeGraphs = false;
|
|
|
|
|
2022-10-07 01:23:29 +01:00
|
|
|
// Run typechecking only in mode required for autocomplete (strict mode in
|
|
|
|
// order to get more precise type information)
|
2022-04-07 22:29:01 +01:00
|
|
|
bool forAutocomplete = false;
|
2022-10-07 01:23:29 +01:00
|
|
|
|
2023-03-17 19:20:37 +00:00
|
|
|
bool runLintChecks = false;
|
|
|
|
|
2022-10-07 01:23:29 +01:00
|
|
|
// If not empty, randomly shuffle the constraint set before attempting to
|
|
|
|
// solve. Use this value to seed the random number generator.
|
|
|
|
std::optional<unsigned> randomizeConstraintResolutionSeed;
|
2023-03-17 19:20:37 +00:00
|
|
|
|
|
|
|
std::optional<LintOptions> enabledLintWarnings;
|
2023-07-14 19:08:53 +01:00
|
|
|
|
|
|
|
std::shared_ptr<FrontendCancellationToken> cancellationToken;
|
2023-08-18 19:15:41 +01:00
|
|
|
|
|
|
|
// Time limit for typechecking a single module
|
|
|
|
std::optional<double> moduleTimeLimitSec;
|
|
|
|
|
|
|
|
// When true, some internal complexity limits will be scaled down for modules that miss the limit set by moduleTimeLimitSec
|
|
|
|
bool applyInternalLimitScaling = false;
|
2021-10-29 21:25:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CheckResult
|
|
|
|
{
|
|
|
|
std::vector<TypeError> errors;
|
2023-03-17 19:20:37 +00:00
|
|
|
|
|
|
|
LintResult lintResult;
|
|
|
|
|
2022-04-07 22:29:01 +01:00
|
|
|
std::vector<ModuleName> timeoutHits;
|
2021-10-29 21:25:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FrontendModuleResolver : ModuleResolver
|
|
|
|
{
|
|
|
|
FrontendModuleResolver(Frontend* frontend);
|
|
|
|
|
|
|
|
const ModulePtr getModule(const ModuleName& moduleName) const override;
|
|
|
|
bool moduleExists(const ModuleName& moduleName) const override;
|
|
|
|
std::optional<ModuleInfo> resolveModuleInfo(const ModuleName& currentModuleName, const AstExpr& pathExpr) override;
|
|
|
|
std::string getHumanReadableModuleName(const ModuleName& moduleName) const override;
|
|
|
|
|
2023-04-21 23:14:26 +01:00
|
|
|
void setModule(const ModuleName& moduleName, ModulePtr module);
|
|
|
|
void clearModules();
|
|
|
|
|
|
|
|
private:
|
2021-10-29 21:25:12 +01:00
|
|
|
Frontend* frontend;
|
2023-04-21 23:14:26 +01:00
|
|
|
|
|
|
|
mutable std::mutex moduleMutex;
|
2021-10-29 21:25:12 +01:00
|
|
|
std::unordered_map<ModuleName, ModulePtr> modules;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Frontend
|
|
|
|
{
|
|
|
|
struct Stats
|
|
|
|
{
|
|
|
|
size_t files = 0;
|
|
|
|
size_t lines = 0;
|
|
|
|
|
|
|
|
size_t filesStrict = 0;
|
|
|
|
size_t filesNonstrict = 0;
|
|
|
|
|
|
|
|
double timeRead = 0;
|
|
|
|
double timeParse = 0;
|
|
|
|
double timeCheck = 0;
|
|
|
|
double timeLint = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
Frontend(FileResolver* fileResolver, ConfigResolver* configResolver, const FrontendOptions& options = {});
|
|
|
|
|
2023-07-07 21:10:48 +01:00
|
|
|
// Parse module graph and prepare SourceNode/SourceModule data, including required dependencies without running typechecking
|
|
|
|
void parse(const ModuleName& name);
|
|
|
|
|
|
|
|
// Parse and typecheck module graph
|
2022-01-07 01:46:53 +00:00
|
|
|
CheckResult check(const ModuleName& name, std::optional<FrontendOptions> optionOverride = {}); // new shininess
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-04-07 22:29:01 +01:00
|
|
|
bool isDirty(const ModuleName& name, bool forAutocomplete = false) const;
|
2021-10-29 21:25:12 +01:00
|
|
|
void markDirty(const ModuleName& name, std::vector<ModuleName>* markedDirty = nullptr);
|
|
|
|
|
|
|
|
/** Borrow a pointer into the SourceModule cache.
|
|
|
|
*
|
|
|
|
* Returns nullptr if we don't have it. This could mean that the script
|
|
|
|
* doesn't exist, or simply that its contents have changed since the previous
|
|
|
|
* check, in which case we do not have its AST.
|
|
|
|
*
|
|
|
|
* IMPORTANT: this pointer is only valid until the next call to markDirty. Do not retain it.
|
|
|
|
*/
|
|
|
|
SourceModule* getSourceModule(const ModuleName& name);
|
|
|
|
const SourceModule* getSourceModule(const ModuleName& name) const;
|
|
|
|
|
|
|
|
void clearStats();
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
ScopePtr addEnvironment(const std::string& environmentName);
|
2023-03-10 20:21:07 +00:00
|
|
|
ScopePtr getEnvironmentScope(const std::string& environmentName) const;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
void registerBuiltinDefinition(const std::string& name, std::function<void(Frontend&, GlobalTypes&, ScopePtr)>);
|
2021-10-29 21:25:12 +01:00
|
|
|
void applyBuiltinDefinitionToEnvironment(const std::string& environmentName, const std::string& definitionName);
|
|
|
|
|
2023-04-07 22:01:29 +01:00
|
|
|
LoadDefinitionFileResult loadDefinitionFile(GlobalTypes& globals, ScopePtr targetScope, std::string_view source, const std::string& packageName,
|
|
|
|
bool captureComments, bool typeCheckForAutocomplete = false);
|
2022-07-01 00:52:43 +01:00
|
|
|
|
2023-05-05 22:52:49 +01:00
|
|
|
// Batch module checking. Queue modules and check them together, retrieve results with 'getCheckResult'
|
|
|
|
// If provided, 'executeTask' function is allowed to call the 'task' function on any thread and return without waiting for 'task' to complete
|
|
|
|
void queueModuleCheck(const std::vector<ModuleName>& names);
|
|
|
|
void queueModuleCheck(const ModuleName& name);
|
|
|
|
std::vector<ModuleName> checkQueuedModules(std::optional<FrontendOptions> optionOverride = {},
|
|
|
|
std::function<void(std::function<void()> task)> executeTask = {}, std::function<void(size_t done, size_t total)> progress = {});
|
|
|
|
|
|
|
|
std::optional<CheckResult> getCheckResult(const ModuleName& name, bool accumulateNested, bool forAutocomplete = false);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
private:
|
2023-04-14 19:06:22 +01:00
|
|
|
ModulePtr check(const SourceModule& sourceModule, Mode mode, std::vector<RequireCycle> requireCycles, std::optional<ScopePtr> environmentScope,
|
|
|
|
bool forAutocomplete, bool recordJsonLog, TypeCheckLimits typeCheckLimits);
|
2022-06-03 23:15:45 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
std::pair<SourceNode*, SourceModule*> getSourceNode(const ModuleName& name);
|
2021-10-29 21:25:12 +01:00
|
|
|
SourceModule parse(const ModuleName& name, std::string_view src, const ParseOptions& parseOptions);
|
|
|
|
|
2023-05-05 22:52:49 +01:00
|
|
|
bool parseGraph(
|
|
|
|
std::vector<ModuleName>& buildQueue, const ModuleName& root, bool forAutocomplete, std::function<bool(const ModuleName&)> canSkip = {});
|
|
|
|
|
|
|
|
void addBuildQueueItems(std::vector<BuildQueueItem>& items, std::vector<ModuleName>& buildQueue, bool cycleDetected,
|
2023-11-10 21:10:07 +00:00
|
|
|
DenseHashSet<Luau::ModuleName>& seen, const FrontendOptions& frontendOptions);
|
2023-05-05 22:52:49 +01:00
|
|
|
void checkBuildQueueItem(BuildQueueItem& item);
|
|
|
|
void checkBuildQueueItems(std::vector<BuildQueueItem>& items);
|
|
|
|
void recordItemResult(const BuildQueueItem& item);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
static LintResult classifyLints(const std::vector<LintWarning>& warnings, const Config& config);
|
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
ScopePtr getModuleEnvironment(const SourceModule& module, const Config& config, bool forAutocomplete) const;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
std::unordered_map<std::string, ScopePtr> environments;
|
2023-04-07 22:01:29 +01:00
|
|
|
std::unordered_map<std::string, std::function<void(Frontend&, GlobalTypes&, ScopePtr)>> builtinDefinitions;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2023-01-04 20:53:17 +00:00
|
|
|
BuiltinTypes builtinTypes_;
|
2022-07-01 00:52:43 +01:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
public:
|
2023-01-04 20:53:17 +00:00
|
|
|
const NotNull<BuiltinTypes> builtinTypes;
|
2022-09-08 23:14:25 +01:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
FileResolver* fileResolver;
|
2023-04-14 19:06:22 +01:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
FrontendModuleResolver moduleResolver;
|
|
|
|
FrontendModuleResolver moduleResolverForAutocomplete;
|
2023-04-14 19:06:22 +01:00
|
|
|
|
2023-03-10 20:21:07 +00:00
|
|
|
GlobalTypes globals;
|
|
|
|
GlobalTypes globalsForAutocomplete;
|
2023-04-14 19:06:22 +01:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
ConfigResolver* configResolver;
|
|
|
|
FrontendOptions options;
|
|
|
|
InternalErrorReporter iceHandler;
|
2023-04-14 19:06:22 +01:00
|
|
|
std::function<void(const ModuleName& name, const ScopePtr& scope, bool forAutocomplete)> prepareModuleScope;
|
2024-01-19 18:04:46 +00:00
|
|
|
std::function<void(const ModuleName& name, std::string log)> writeJsonLog = {};
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2023-05-05 22:52:49 +01:00
|
|
|
std::unordered_map<ModuleName, std::shared_ptr<SourceNode>> sourceNodes;
|
|
|
|
std::unordered_map<ModuleName, std::shared_ptr<SourceModule>> sourceModules;
|
2022-06-17 02:05:14 +01:00
|
|
|
std::unordered_map<ModuleName, RequireTraceResult> requireTrace;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
Stats stats = {};
|
2023-05-05 22:52:49 +01:00
|
|
|
|
|
|
|
std::vector<ModuleName> moduleQueue;
|
2021-10-29 21:25:12 +01:00
|
|
|
};
|
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
ModulePtr check(const SourceModule& sourceModule, Mode mode, const std::vector<RequireCycle>& requireCycles, NotNull<BuiltinTypes> builtinTypes,
|
2023-03-03 20:21:14 +00:00
|
|
|
NotNull<InternalErrorReporter> iceHandler, NotNull<ModuleResolver> moduleResolver, NotNull<FileResolver> fileResolver,
|
2023-07-28 16:13:53 +01:00
|
|
|
const ScopePtr& globalScope, std::function<void(const ModuleName&, const ScopePtr&)> prepareModuleScope, FrontendOptions options,
|
|
|
|
TypeCheckLimits limits);
|
2023-03-03 20:21:14 +00:00
|
|
|
|
2023-10-13 21:20:12 +01:00
|
|
|
ModulePtr check(const SourceModule& sourceModule, Mode mode, const std::vector<RequireCycle>& requireCycles, NotNull<BuiltinTypes> builtinTypes,
|
2023-03-03 20:21:14 +00:00
|
|
|
NotNull<InternalErrorReporter> iceHandler, NotNull<ModuleResolver> moduleResolver, NotNull<FileResolver> fileResolver,
|
2023-04-28 20:55:13 +01:00
|
|
|
const ScopePtr& globalScope, std::function<void(const ModuleName&, const ScopePtr&)> prepareModuleScope, FrontendOptions options,
|
2024-01-19 18:04:46 +00:00
|
|
|
TypeCheckLimits limits, bool recordJsonLog, std::function<void(const ModuleName&, std::string)> writeJsonLog);
|
2023-02-17 23:41:51 +00:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
} // namespace Luau
|