luau/Analysis/include/Luau/ConstraintGraphBuilder.h

218 lines
8.9 KiB
C
Raw Normal View History

2022-06-04 04:32:20 +08:00
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#pragma once
#include <memory>
#include <vector>
2022-06-17 08:54:42 +08:00
#include <unordered_map>
2022-06-04 04:32:20 +08:00
#include "Luau/Ast.h"
2022-06-17 08:54:42 +08:00
#include "Luau/Constraint.h"
2022-06-04 04:32:20 +08:00
#include "Luau/Module.h"
2022-09-02 07:00:14 +08:00
#include "Luau/ModuleResolver.h"
2022-06-17 08:54:42 +08:00
#include "Luau/NotNull.h"
2022-06-04 04:32:20 +08:00
#include "Luau/Symbol.h"
#include "Luau/TypeVar.h"
#include "Luau/Variant.h"
namespace Luau
{
2022-07-29 11:41:13 +08:00
struct Scope;
using ScopePtr = std::shared_ptr<Scope>;
2022-06-04 04:32:20 +08:00
2022-09-09 05:44:50 +08:00
struct DcrLogger;
2022-06-04 04:32:20 +08:00
struct ConstraintGraphBuilder
{
// A list of all the scopes in the module. This vector holds ownership of the
// scope pointers; the scopes themselves borrow pointers to other scopes to
// define the scope hierarchy.
2022-07-29 11:41:13 +08:00
std::vector<std::pair<Location, ScopePtr>> scopes;
2022-07-01 07:29:02 +08:00
ModuleName moduleName;
2022-08-12 04:42:54 +08:00
ModulePtr module;
2022-09-09 05:44:50 +08:00
NotNull<SingletonTypes> singletonTypes;
2022-07-01 07:29:02 +08:00
const NotNull<TypeArena> arena;
2022-06-04 04:32:20 +08:00
// The root scope of the module we're generating constraints for.
2022-07-01 07:29:02 +08:00
// This is null when the CGB is initially constructed.
2022-07-29 11:41:13 +08:00
Scope* rootScope;
2022-06-17 08:54:42 +08:00
// A mapping of AST node to TypeId.
DenseHashMap<const AstExpr*, TypeId> astTypes{nullptr};
// A mapping of AST node to TypePackId.
DenseHashMap<const AstExpr*, TypePackId> astTypePacks{nullptr};
DenseHashMap<const AstExpr*, TypeId> astOriginalCallTypes{nullptr};
2022-06-24 09:44:07 +08:00
// Types resolved from type annotations. Analogous to astTypes.
DenseHashMap<const AstType*, TypeId> astResolvedTypes{nullptr};
// Type packs resolved from type annotations. Analogous to astTypePacks.
DenseHashMap<const AstTypePack*, TypePackId> astResolvedTypePacks{nullptr};
2022-08-05 05:27:28 +08:00
// Defining scopes for AST nodes.
DenseHashMap<const AstStatTypeAlias*, ScopePtr> astTypeAliasDefiningScopes{nullptr};
2022-06-04 04:32:20 +08:00
2022-07-01 07:29:02 +08:00
int recursionCount = 0;
// It is pretty uncommon for constraint generation to itself produce errors, but it can happen.
std::vector<TypeError> errors;
2022-09-02 07:00:14 +08:00
// Needed to resolve modules to make 'require' import types properly.
NotNull<ModuleResolver> moduleResolver;
2022-07-01 07:29:02 +08:00
// Occasionally constraint generation needs to produce an ICE.
const NotNull<InternalErrorReporter> ice;
2022-08-12 04:42:54 +08:00
ScopePtr globalScope;
2022-09-09 05:44:50 +08:00
DcrLogger* logger;
2022-07-01 07:29:02 +08:00
2022-09-02 07:00:14 +08:00
ConstraintGraphBuilder(const ModuleName& moduleName, ModulePtr module, TypeArena* arena, NotNull<ModuleResolver> moduleResolver,
2022-09-09 05:44:50 +08:00
NotNull<SingletonTypes> singletonTypes, NotNull<InternalErrorReporter> ice, const ScopePtr& globalScope, DcrLogger* logger);
2022-06-04 04:32:20 +08:00
/**
* Fabricates a new free type belonging to a given scope.
2022-07-01 07:29:02 +08:00
* @param scope the scope the free type belongs to.
2022-06-04 04:32:20 +08:00
*/
2022-07-29 11:41:13 +08:00
TypeId freshType(const ScopePtr& scope);
2022-06-04 04:32:20 +08:00
/**
* Fabricates a new free type pack belonging to a given scope.
2022-07-01 07:29:02 +08:00
* @param scope the scope the free type pack belongs to.
2022-06-04 04:32:20 +08:00
*/
2022-07-29 11:41:13 +08:00
TypePackId freshTypePack(const ScopePtr& scope);
2022-06-04 04:32:20 +08:00
/**
* Fabricates a scope that is a child of another scope.
2022-08-19 05:04:33 +08:00
* @param node the lexical node that the scope belongs to.
2022-06-04 04:32:20 +08:00
* @param parent the parent scope of the new scope. Must not be null.
*/
2022-08-19 05:04:33 +08:00
ScopePtr childScope(AstNode* node, const ScopePtr& parent);
2022-06-04 04:32:20 +08:00
/**
* Adds a new constraint with no dependencies to a given scope.
2022-07-01 07:29:02 +08:00
* @param scope the scope to add the constraint to.
2022-06-04 04:32:20 +08:00
* @param cv the constraint variant to add.
*/
2022-09-02 07:00:14 +08:00
void addConstraint(const ScopePtr& scope, const Location& location, ConstraintV cv);
2022-06-04 04:32:20 +08:00
/**
* Adds a constraint to a given scope.
* @param scope the scope to add the constraint to. Must not be null.
* @param c the constraint to add.
*/
2022-07-29 11:41:13 +08:00
void addConstraint(const ScopePtr& scope, std::unique_ptr<Constraint> c);
2022-06-04 04:32:20 +08:00
/**
* The entry point to the ConstraintGraphBuilder. This will construct a set
* of scopes, constraints, and free types that can be solved later.
* @param block the root block to generate constraints for.
*/
void visit(AstStatBlock* block);
2022-07-29 11:41:13 +08:00
void visitBlockWithoutChildScope(const ScopePtr& scope, AstStatBlock* block);
2022-07-01 07:29:02 +08:00
2022-07-29 11:41:13 +08:00
void visit(const ScopePtr& scope, AstStat* stat);
void visit(const ScopePtr& scope, AstStatBlock* block);
void visit(const ScopePtr& scope, AstStatLocal* local);
void visit(const ScopePtr& scope, AstStatFor* for_);
2022-09-02 07:00:14 +08:00
void visit(const ScopePtr& scope, AstStatForIn* forIn);
2022-08-12 04:42:54 +08:00
void visit(const ScopePtr& scope, AstStatWhile* while_);
2022-08-19 05:04:33 +08:00
void visit(const ScopePtr& scope, AstStatRepeat* repeat);
2022-07-29 11:41:13 +08:00
void visit(const ScopePtr& scope, AstStatLocalFunction* function);
void visit(const ScopePtr& scope, AstStatFunction* function);
void visit(const ScopePtr& scope, AstStatReturn* ret);
void visit(const ScopePtr& scope, AstStatAssign* assign);
2022-08-19 05:04:33 +08:00
void visit(const ScopePtr& scope, AstStatCompoundAssign* assign);
2022-07-29 11:41:13 +08:00
void visit(const ScopePtr& scope, AstStatIf* ifStatement);
void visit(const ScopePtr& scope, AstStatTypeAlias* alias);
2022-08-05 05:27:28 +08:00
void visit(const ScopePtr& scope, AstStatDeclareGlobal* declareGlobal);
void visit(const ScopePtr& scope, AstStatDeclareClass* declareClass);
void visit(const ScopePtr& scope, AstStatDeclareFunction* declareFunction);
2022-06-17 08:54:42 +08:00
2022-07-29 11:41:13 +08:00
TypePackId checkPack(const ScopePtr& scope, AstArray<AstExpr*> exprs);
TypePackId checkPack(const ScopePtr& scope, AstExpr* expr);
2022-06-04 04:32:20 +08:00
2022-06-17 08:54:42 +08:00
/**
* Checks an expression that is expected to evaluate to one type.
* @param scope the scope the expression is contained within.
* @param expr the expression to check.
* @return the type of the expression.
*/
2022-07-29 11:41:13 +08:00
TypeId check(const ScopePtr& scope, AstExpr* expr);
2022-07-01 07:29:02 +08:00
2022-07-29 11:41:13 +08:00
TypeId checkExprTable(const ScopePtr& scope, AstExprTable* expr);
TypeId check(const ScopePtr& scope, AstExprIndexName* indexName);
TypeId check(const ScopePtr& scope, AstExprIndexExpr* indexExpr);
TypeId check(const ScopePtr& scope, AstExprUnary* unary);
TypeId check(const ScopePtr& scope, AstExprBinary* binary);
2022-08-19 05:04:33 +08:00
TypeId check(const ScopePtr& scope, AstExprIfElse* ifElse);
2022-08-12 04:42:54 +08:00
TypeId check(const ScopePtr& scope, AstExprTypeAssertion* typeAssert);
2022-07-01 07:29:02 +08:00
struct FunctionSignature
{
// The type of the function.
TypeId signature;
// The scope that encompasses the function's signature. May be nullptr
// if there was no need for a signature scope (the function has no
// generics).
2022-07-29 11:41:13 +08:00
ScopePtr signatureScope;
2022-07-01 07:29:02 +08:00
// The scope that encompasses the function's body. Is a child scope of
// signatureScope, if present.
2022-07-29 11:41:13 +08:00
ScopePtr bodyScope;
2022-07-01 07:29:02 +08:00
};
2022-07-29 11:41:13 +08:00
FunctionSignature checkFunctionSignature(const ScopePtr& parent, AstExprFunction* fn);
2022-06-17 08:54:42 +08:00
/**
* Checks the body of a function expression.
* @param scope the interior scope of the body of the function.
* @param fn the function expression to check.
*/
2022-07-29 11:41:13 +08:00
void checkFunctionBody(const ScopePtr& scope, AstExprFunction* fn);
2022-06-24 09:44:07 +08:00
/**
* Resolves a type from its AST annotation.
* @param scope the scope that the type annotation appears within.
* @param ty the AST annotation to resolve.
2022-08-05 05:27:28 +08:00
* @param topLevel whether the annotation is a "top-level" annotation.
2022-06-24 09:44:07 +08:00
* @return the type of the AST annotation.
**/
2022-08-05 05:27:28 +08:00
TypeId resolveType(const ScopePtr& scope, AstType* ty, bool topLevel = false);
2022-06-24 09:44:07 +08:00
/**
* Resolves a type pack from its AST annotation.
* @param scope the scope that the type annotation appears within.
* @param tp the AST annotation to resolve.
* @return the type pack of the AST annotation.
**/
2022-07-29 11:41:13 +08:00
TypePackId resolveTypePack(const ScopePtr& scope, AstTypePack* tp);
2022-07-01 07:29:02 +08:00
2022-07-29 11:41:13 +08:00
TypePackId resolveTypePack(const ScopePtr& scope, const AstTypeList& list);
2022-07-01 07:29:02 +08:00
2022-07-29 11:41:13 +08:00
std::vector<std::pair<Name, GenericTypeDefinition>> createGenerics(const ScopePtr& scope, AstArray<AstGenericType> generics);
std::vector<std::pair<Name, GenericTypePackDefinition>> createGenericPacks(const ScopePtr& scope, AstArray<AstGenericTypePack> packs);
2022-06-24 09:44:07 +08:00
2022-07-29 11:41:13 +08:00
TypeId flattenPack(const ScopePtr& scope, Location location, TypePackId tp);
2022-07-01 07:29:02 +08:00
void reportError(Location location, TypeErrorData err);
void reportCodeTooComplex(Location location);
/** Scan the program for global definitions.
*
* ConstraintGraphBuilder needs to differentiate between globals and accesses to undefined symbols. Doing this "for
* real" in a general way is going to be pretty hard, so we are choosing not to tackle that yet. For now, we do an
* initial scan of the AST and note what globals are defined.
*/
2022-07-29 11:41:13 +08:00
void prepopulateGlobalScope(const ScopePtr& globalScope, AstStatBlock* program);
2022-06-04 04:32:20 +08:00
};
2022-06-17 08:54:42 +08:00
/**
* Collects a vector of borrowed constraints from the scope and all its child
* scopes. It is important to only call this function when you're done adding
* constraints to the scope or its descendants, lest the borrowed pointers
* become invalid due to a container reallocation.
* @param rootScope the root scope of the scope graph to collect constraints
* from.
* @return a list of pointers to constraints contained within the scope graph.
* None of these pointers should be null.
*/
2022-07-29 11:41:13 +08:00
std::vector<NotNull<Constraint>> collectConstraints(NotNull<Scope> rootScope);
2022-06-04 04:32:20 +08:00
} // namespace Luau