2021-10-29 13:25:12 -07:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
2022-06-23 18:44:07 -07:00
|
|
|
#include "Luau/Error.h"
|
2023-03-17 16:59:30 +02:00
|
|
|
#include "Luau/Linter.h"
|
2021-10-29 13:25:12 -07:00
|
|
|
#include "Luau/FileResolver.h"
|
|
|
|
#include "Luau/ParseOptions.h"
|
2022-02-17 16:41:20 -08:00
|
|
|
#include "Luau/ParseResult.h"
|
2022-06-23 18:44:07 -07:00
|
|
|
#include "Luau/Scope.h"
|
2022-05-19 16:46:52 -07:00
|
|
|
#include "Luau/TypeArena.h"
|
2024-07-25 17:10:42 -07:00
|
|
|
#include "Luau/AnyTypeSummary.h"
|
2024-10-18 18:08:01 +03:00
|
|
|
#include "Luau/DataFlowGraph.h"
|
2021-10-29 13:25:12 -07:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
struct Module;
|
2024-07-25 17:10:42 -07:00
|
|
|
struct AnyTypeSummary;
|
2021-10-29 13:25:12 -07:00
|
|
|
|
|
|
|
using ScopePtr = std::shared_ptr<struct Scope>;
|
|
|
|
using ModulePtr = std::shared_ptr<Module>;
|
2022-06-23 18:44:07 -07:00
|
|
|
|
|
|
|
class AstType;
|
|
|
|
class AstTypePack;
|
2021-10-29 13:25:12 -07:00
|
|
|
|
|
|
|
/// Root of the AST of a parsed source file
|
|
|
|
struct SourceModule
|
|
|
|
{
|
2023-04-21 14:41:03 -07:00
|
|
|
ModuleName name; // Module identifier or a filename
|
|
|
|
std::string humanReadableName;
|
|
|
|
|
2021-10-29 13:25:12 -07:00
|
|
|
SourceCode::Type type = SourceCode::None;
|
|
|
|
std::optional<std::string> environmentName;
|
|
|
|
bool cyclic = false;
|
|
|
|
|
2022-04-07 13:53:47 -07:00
|
|
|
std::shared_ptr<Allocator> allocator;
|
|
|
|
std::shared_ptr<AstNameTable> names;
|
2021-10-29 13:25:12 -07:00
|
|
|
std::vector<ParseError> parseErrors;
|
|
|
|
|
|
|
|
AstStatBlock* root = nullptr;
|
|
|
|
std::optional<Mode> mode;
|
|
|
|
|
2022-02-17 16:41:20 -08:00
|
|
|
std::vector<HotComment> hotcomments;
|
2021-10-29 13:25:12 -07:00
|
|
|
std::vector<Comment> commentLocations;
|
|
|
|
|
|
|
|
SourceModule()
|
|
|
|
: allocator(new Allocator)
|
|
|
|
, names(new AstNameTable(*allocator))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool isWithinComment(const SourceModule& sourceModule, Position pos);
|
2023-04-14 15:05:27 +03:00
|
|
|
bool isWithinComment(const ParseResult& result, Position pos);
|
2021-10-29 13:25:12 -07:00
|
|
|
|
2022-04-07 13:53:47 -07:00
|
|
|
struct RequireCycle
|
|
|
|
{
|
|
|
|
Location location;
|
|
|
|
std::vector<ModuleName> path; // one of the paths for a require() to go all the way back to the originating module
|
|
|
|
};
|
|
|
|
|
2021-10-29 13:25:12 -07:00
|
|
|
struct Module
|
|
|
|
{
|
|
|
|
~Module();
|
|
|
|
|
2024-11-15 11:37:29 -08:00
|
|
|
// TODO: Clip this when we clip FFlagLuauSolverV2
|
|
|
|
bool checkedInNewSolver = false;
|
|
|
|
|
2023-04-21 14:41:03 -07:00
|
|
|
ModuleName name;
|
|
|
|
std::string humanReadableName;
|
|
|
|
|
2021-10-29 13:25:12 -07:00
|
|
|
TypeArena interfaceTypes;
|
|
|
|
TypeArena internalTypes;
|
|
|
|
|
2024-07-25 17:10:42 -07:00
|
|
|
// Summary of Ast Nodes that either contain
|
|
|
|
// user annotated anys or typechecker inferred anys
|
|
|
|
AnyTypeSummary ats{};
|
|
|
|
|
2022-04-07 13:53:47 -07:00
|
|
|
// Scopes and AST types refer to parse data, so we need to keep that alive
|
|
|
|
std::shared_ptr<Allocator> allocator;
|
|
|
|
std::shared_ptr<AstNameTable> names;
|
|
|
|
|
2022-08-04 14:27:28 -07:00
|
|
|
std::vector<std::pair<Location, ScopePtr>> scopes; // never empty
|
2021-11-04 19:07:18 -07:00
|
|
|
|
|
|
|
DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr};
|
2022-06-16 17:54:42 -07:00
|
|
|
DenseHashMap<const AstExpr*, TypePackId> astTypePacks{nullptr};
|
2021-11-04 19:07:18 -07:00
|
|
|
DenseHashMap<const AstExpr*, TypeId> astExpectedTypes{nullptr};
|
2023-01-06 18:07:19 +02:00
|
|
|
|
2023-06-24 08:33:44 +03:00
|
|
|
// For AST nodes that are function calls, this map provides the
|
|
|
|
// unspecialized type of the function that was called. If a function call
|
|
|
|
// resolves to a __call metamethod application, this map will point at that
|
|
|
|
// metamethod.
|
|
|
|
//
|
|
|
|
// This is useful for type checking and Signature Help.
|
2023-02-03 14:34:12 +02:00
|
|
|
DenseHashMap<const AstNode*, TypeId> astOriginalCallTypes{nullptr};
|
2023-06-24 08:33:44 +03:00
|
|
|
|
|
|
|
// The specialization of a function that was selected. If the function is
|
|
|
|
// generic, those generic type parameters will be replaced with the actual
|
|
|
|
// types that were passed. If the function is an overload, this map will
|
|
|
|
// point at the specific overloads that were selected.
|
2023-02-03 14:34:12 +02:00
|
|
|
DenseHashMap<const AstNode*, TypeId> astOverloadResolvedTypes{nullptr};
|
2023-01-06 18:07:19 +02:00
|
|
|
|
2023-06-24 08:33:44 +03:00
|
|
|
// Only used with for...in loops. The computed type of the next() function
|
|
|
|
// is kept here for type checking.
|
|
|
|
DenseHashMap<const AstNode*, TypeId> astForInNextTypes{nullptr};
|
|
|
|
|
2022-06-23 18:44:07 -07:00
|
|
|
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};
|
|
|
|
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};
|
2023-02-03 14:34:12 +02:00
|
|
|
|
2024-06-14 09:38:56 -07:00
|
|
|
// The computed result type of a compound assignment. (eg foo += 1)
|
|
|
|
//
|
|
|
|
// Type checking uses this to check that the result of such an operation is
|
|
|
|
// actually compatible with the left-side operand.
|
|
|
|
DenseHashMap<const AstStat*, TypeId> astCompoundAssignResultTypes{nullptr};
|
|
|
|
|
2023-11-10 10:05:48 -08:00
|
|
|
DenseHashMap<TypeId, std::vector<std::pair<Location, TypeId>>> upperBoundContributors{nullptr};
|
|
|
|
|
2023-06-24 08:33:44 +03:00
|
|
|
// Map AST nodes to the scope they create. Cannot be NotNull<Scope> because
|
|
|
|
// we need a sentinel value for the map.
|
2022-08-18 14:04:33 -07:00
|
|
|
DenseHashMap<const AstNode*, Scope*> astScopes{nullptr};
|
2021-11-04 19:07:18 -07:00
|
|
|
|
2021-10-29 13:25:12 -07:00
|
|
|
std::unordered_map<Name, TypeId> declaredGlobals;
|
|
|
|
ErrorVec errors;
|
2023-03-17 16:59:30 +02:00
|
|
|
LintResult lintResult;
|
2021-10-29 13:25:12 -07:00
|
|
|
Mode mode;
|
|
|
|
SourceCode::Type type;
|
2023-08-18 10:06:29 -07:00
|
|
|
double checkDurationSec = 0.0;
|
2022-04-07 13:53:47 -07:00
|
|
|
bool timeout = false;
|
2023-07-14 08:57:16 -07:00
|
|
|
bool cancelled = false;
|
2021-10-29 13:25:12 -07:00
|
|
|
|
2023-01-06 18:07:19 +02:00
|
|
|
TypePackId returnType = nullptr;
|
|
|
|
std::unordered_map<Name, TypeFun> exportedTypeBindings;
|
|
|
|
|
|
|
|
bool hasModuleScope() const;
|
2021-10-29 13:25:12 -07:00
|
|
|
ScopePtr getModuleScope() const;
|
|
|
|
|
2023-06-24 08:33:44 +03:00
|
|
|
// Once a module has been typechecked, we clone its public interface into a
|
|
|
|
// separate arena. This helps us to force Type ownership into a DAG rather
|
|
|
|
// than a DCG.
|
2023-01-03 19:33:19 +02:00
|
|
|
void clonePublicInterface(NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter& ice);
|
2021-10-29 13:25:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|