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"
|
2023-03-18 03:20:37 +08:00
|
|
|
#include "Luau/Linter.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"
|
2024-07-27 01:47:49 +08:00
|
|
|
#include "Luau/AnyTypeSummary.h"
|
2024-10-19 01:27:15 +08:00
|
|
|
#include "Luau/DataFlowGraph.h"
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
|
|
|
struct Module;
|
2024-07-27 01:47:49 +08:00
|
|
|
struct AnyTypeSummary;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-04-22 06:14:26 +08:00
|
|
|
ModuleName name; // Module identifier or a filename
|
|
|
|
std::string humanReadableName;
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
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);
|
2023-04-15 02:06:22 +08:00
|
|
|
bool isWithinComment(const ParseResult& result, Position pos);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
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();
|
|
|
|
|
2023-04-22 06:14:26 +08:00
|
|
|
ModuleName name;
|
|
|
|
std::string humanReadableName;
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
TypeArena interfaceTypes;
|
|
|
|
TypeArena internalTypes;
|
|
|
|
|
2024-07-27 01:47:49 +08:00
|
|
|
// Summary of Ast Nodes that either contain
|
|
|
|
// user annotated anys or typechecker inferred anys
|
|
|
|
AnyTypeSummary ats{};
|
|
|
|
|
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};
|
2023-01-07 05:14:35 +08:00
|
|
|
|
2023-06-24 14:19:39 +08: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.
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-04 03:26:13 +08:00
|
|
|
DenseHashMap<const AstNode*, TypeId> astOriginalCallTypes{nullptr};
|
2023-06-24 14:19:39 +08: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.
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-04 03:26:13 +08:00
|
|
|
DenseHashMap<const AstNode*, TypeId> astOverloadResolvedTypes{nullptr};
|
2023-01-07 05:14:35 +08:00
|
|
|
|
2023-06-24 14:19:39 +08: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-24 09:56:00 +08:00
|
|
|
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};
|
|
|
|
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};
|
Sync to upstream/release/562 (#828)
* Fixed rare use-after-free in analysis during table unification
A lot of work these past months went into two new Luau components:
* A near full rewrite of the typechecker using a new deferred constraint
resolution system
* Native code generation for AoT/JiT compilation of VM bytecode into x64
(avx)/arm64 instructions
Both of these components are far from finished and we don't provide
documentation on building and using them at this point.
However, curious community members expressed interest in learning about
changes that go into these components each week, so we are now listing
them here in the 'sync' pull request descriptions.
---
New typechecker can be enabled by setting
DebugLuauDeferredConstraintResolution flag to 'true'.
It is considered unstable right now, so try it at your own risk.
Even though it already provides better type inference than the current
one in some cases, our main goal right now is to reach feature parity
with current typechecker.
Features which improve over the capabilities of the current typechecker
are marked as '(NEW)'.
Changes to new typechecker:
* Regular for loop index and parameters are now typechecked
* Invalid type annotations on local variables are ignored to improve
autocomplete
* Fixed missing autocomplete type suggestions for function arguments
* Type reduction is now performed to produce simpler types to be
presented to the user (error messages, custom LSPs)
* Internally, complex types like '((number | string) & ~(false?)) |
string' can be produced, which is just 'string | number' when simplified
* Fixed spots where support for unknown and never types was missing
* (NEW) Length operator '#' is now valid to use on top table type, this
type comes up when doing typeof(x) == "table" guards and isn't available
in current typechecker
---
Changes to native code generation:
* Additional math library fast calls are now lowered to x64: math.ldexp,
math.round, math.frexp, math.modf, math.sign and math.clamp
2023-02-04 03:26:13 +08:00
|
|
|
|
2024-06-15 04:21:20 +08: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-11 05:10:07 +08:00
|
|
|
DenseHashMap<TypeId, std::vector<std::pair<Location, TypeId>>> upperBoundContributors{nullptr};
|
|
|
|
|
2023-06-24 14:19:39 +08:00
|
|
|
// Map AST nodes to the scope they create. Cannot be NotNull<Scope> because
|
|
|
|
// we need a sentinel value for the map.
|
2022-08-19 05:32:08 +08:00
|
|
|
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;
|
2023-03-18 03:20:37 +08:00
|
|
|
LintResult lintResult;
|
2021-10-30 04:25:12 +08:00
|
|
|
Mode mode;
|
|
|
|
SourceCode::Type type;
|
2023-08-19 02:15:41 +08:00
|
|
|
double checkDurationSec = 0.0;
|
2022-04-08 05:29:01 +08:00
|
|
|
bool timeout = false;
|
2023-07-15 02:08:53 +08:00
|
|
|
bool cancelled = false;
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2023-01-07 05:14:35 +08:00
|
|
|
TypePackId returnType = nullptr;
|
|
|
|
std::unordered_map<Name, TypeFun> exportedTypeBindings;
|
2024-10-19 01:27:15 +08:00
|
|
|
// We also need to keep DFG data alive between runs
|
|
|
|
std::shared_ptr<DataFlowGraph> dataFlowGraph = nullptr;
|
|
|
|
std::vector<std::unique_ptr<DfgScope>> dfgScopes;
|
2023-01-07 05:14:35 +08:00
|
|
|
|
|
|
|
bool hasModuleScope() const;
|
2021-10-30 04:25:12 +08:00
|
|
|
ScopePtr getModuleScope() const;
|
|
|
|
|
2023-06-24 14:19:39 +08: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-05 04:53:17 +08:00
|
|
|
void clonePublicInterface(NotNull<BuiltinTypes> builtinTypes, InternalErrorReporter& ice);
|
2021-10-30 04:25:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Luau
|