2021-10-30 04:25:12 +08: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-24 09:56:00 +08:00
|
|
|
#include "Luau/Error.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
#include "Luau/FileResolver.h"
|
|
|
|
#include "Luau/ParseOptions.h"
|
2022-02-18 09:18:01 +08:00
|
|
|
#include "Luau/ParseResult.h"
|
2022-06-24 09:56:00 +08:00
|
|
|
#include "Luau/Scope.h"
|
2022-05-20 08:02:24 +08:00
|
|
|
#include "Luau/TypeArena.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
struct Module;
|
|
|
|
|
|
|
|
using ScopePtr = std::shared_ptr<struct Scope>;
|
|
|
|
using ModulePtr = std::shared_ptr<Module>;
|
2022-06-24 09:56:00 +08:00
|
|
|
|
|
|
|
class AstType;
|
|
|
|
class AstTypePack;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
/// Root of the AST of a parsed source file
|
|
|
|
struct SourceModule
|
|
|
|
{
|
|
|
|
ModuleName name; // DataModel path if possible. Filename if not.
|
|
|
|
SourceCode::Type type = SourceCode::None;
|
|
|
|
std::optional<std::string> environmentName;
|
|
|
|
bool cyclic = false;
|
|
|
|
|
2022-04-08 05:29:01 +08:00
|
|
|
std::shared_ptr<Allocator> allocator;
|
|
|
|
std::shared_ptr<AstNameTable> names;
|
2021-10-30 04:25:12 +08:00
|
|
|
std::vector<ParseError> parseErrors;
|
|
|
|
|
|
|
|
AstStatBlock* root = nullptr;
|
|
|
|
std::optional<Mode> mode;
|
|
|
|
|
2022-02-18 09:18:01 +08:00
|
|
|
std::vector<HotComment> hotcomments;
|
2021-10-30 04:25:12 +08:00
|
|
|
std::vector<Comment> commentLocations;
|
|
|
|
|
|
|
|
SourceModule()
|
|
|
|
: allocator(new Allocator)
|
|
|
|
, names(new AstNameTable(*allocator))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool isWithinComment(const SourceModule& sourceModule, Position pos);
|
|
|
|
|
2022-04-08 05:29:01 +08: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-30 04:25:12 +08:00
|
|
|
struct Module
|
|
|
|
{
|
|
|
|
~Module();
|
|
|
|
|
|
|
|
TypeArena interfaceTypes;
|
|
|
|
TypeArena internalTypes;
|
|
|
|
|
2022-04-08 05:29:01 +08: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-05 06:35:33 +08:00
|
|
|
std::vector<std::pair<Location, ScopePtr>> scopes; // never empty
|
2021-11-05 10:34:35 +08:00
|
|
|
|
|
|
|
DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr};
|
2022-06-17 09:05:14 +08:00
|
|
|
DenseHashMap<const AstExpr*, TypePackId> astTypePacks{nullptr};
|
2021-11-05 10:34:35 +08:00
|
|
|
DenseHashMap<const AstExpr*, TypeId> astExpectedTypes{nullptr};
|
|
|
|
DenseHashMap<const AstExpr*, TypeId> astOriginalCallTypes{nullptr};
|
|
|
|
DenseHashMap<const AstExpr*, TypeId> astOverloadResolvedTypes{nullptr};
|
2022-06-24 09:56:00 +08:00
|
|
|
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};
|
|
|
|
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};
|
2022-08-19 05:32:08 +08:00
|
|
|
// Map AST nodes to the scope they create. Cannot be NotNull<Scope> because we need a sentinel value for the map.
|
|
|
|
DenseHashMap<const AstNode*, Scope*> astScopes{nullptr};
|
2021-11-05 10:34:35 +08:00
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
std::unordered_map<Name, TypeId> declaredGlobals;
|
|
|
|
ErrorVec errors;
|
|
|
|
Mode mode;
|
|
|
|
SourceCode::Type type;
|
2022-04-08 05:29:01 +08:00
|
|
|
bool timeout = false;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
ScopePtr getModuleScope() const;
|
|
|
|
|
|
|
|
// Once a module has been typechecked, we clone its public interface into a separate arena.
|
|
|
|
// This helps us to force TypeVar ownership into a DAG rather than a DCG.
|
2022-09-09 06:14:25 +08:00
|
|
|
void clonePublicInterface(NotNull<SingletonTypes> singletonTypes, InternalErrorReporter& ice);
|
2021-10-30 04:25:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|