luau/Analysis/include/Luau/Module.h

112 lines
3.2 KiB
C
Raw Normal View History

// 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 02:44:07 +01:00
#include "Luau/Error.h"
2023-03-17 14:59:30 +00:00
#include "Luau/Linter.h"
#include "Luau/FileResolver.h"
#include "Luau/ParseOptions.h"
2022-02-18 00:41:20 +00:00
#include "Luau/ParseResult.h"
2022-06-24 02:44:07 +01:00
#include "Luau/Scope.h"
2022-05-20 00:46:52 +01:00
#include "Luau/TypeArena.h"
#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 02:44:07 +01:00
class AstType;
class AstTypePack;
/// Root of the AST of a parsed source file
struct SourceModule
{
2023-04-21 22:41:03 +01:00
ModuleName name; // Module identifier or a filename
std::string humanReadableName;
SourceCode::Type type = SourceCode::None;
std::optional<std::string> environmentName;
bool cyclic = false;
2022-04-07 21:53:47 +01:00
std::shared_ptr<Allocator> allocator;
std::shared_ptr<AstNameTable> names;
std::vector<ParseError> parseErrors;
AstStatBlock* root = nullptr;
std::optional<Mode> mode;
2022-02-18 00:41:20 +00:00
std::vector<HotComment> hotcomments;
std::vector<Comment> commentLocations;
SourceModule()
: allocator(new Allocator)
, names(new AstNameTable(*allocator))
{
}
};
bool isWithinComment(const SourceModule& sourceModule, Position pos);
2023-04-14 13:05:27 +01:00
bool isWithinComment(const ParseResult& result, Position pos);
2022-04-07 21:53:47 +01: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
};
struct Module
{
~Module();
2023-04-21 22:41:03 +01:00
ModuleName name;
std::string humanReadableName;
TypeArena interfaceTypes;
TypeArena internalTypes;
2022-04-07 21:53:47 +01: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 22:27:28 +01:00
std::vector<std::pair<Location, ScopePtr>> scopes; // never empty
DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr};
2022-06-17 01:54:42 +01:00
DenseHashMap<const AstExpr*, TypePackId> astTypePacks{nullptr};
DenseHashMap<const AstExpr*, TypeId> astExpectedTypes{nullptr};
2023-01-06 16:07:19 +00:00
2023-02-03 12:34:12 +00:00
DenseHashMap<const AstNode*, TypeId> astOriginalCallTypes{nullptr};
DenseHashMap<const AstNode*, TypeId> astOverloadResolvedTypes{nullptr};
2023-01-06 16:07:19 +00:00
2022-06-24 02:44:07 +01:00
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};
2023-02-03 12:34:12 +00:00
2022-08-18 22:04:33 +01: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};
std::unordered_map<Name, TypeId> declaredGlobals;
ErrorVec errors;
2023-03-17 14:59:30 +00:00
LintResult lintResult;
Mode mode;
SourceCode::Type type;
2022-04-07 21:53:47 +01:00
bool timeout = false;
2023-01-06 16:07:19 +00:00
TypePackId returnType = nullptr;
std::unordered_map<Name, TypeFun> exportedTypeBindings;
bool hasModuleScope() const;
ScopePtr getModuleScope() const;
// Once a module has been typechecked, we clone its public interface into a separate arena.
2023-01-03 17:33:19 +00:00
// This helps us to force Type ownership into a DAG rather than a DCG.
void clonePublicInterface(NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter& ice);
};
} // namespace Luau