1
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
2
#pragma once
3
3
4
+ #include " Luau/AstQuery.h"
4
5
#include " Luau/Config.h"
5
6
#include " Luau/ModuleResolver.h"
6
7
#include " Luau/Scope.h"
@@ -36,6 +37,7 @@ struct AnyTypeSummary
36
37
{
37
38
TypeArena arena;
38
39
40
+ AstStatBlock* rootSrc = nullptr ;
39
41
DenseHashSet<TypeId> seenTypeFamilyInstances{nullptr };
40
42
41
43
int recursionCount = 0 ;
@@ -47,33 +49,30 @@ struct AnyTypeSummary
47
49
48
50
AnyTypeSummary ();
49
51
50
- void traverse (Module* module, AstStat* src, NotNull<BuiltinTypes> builtinTypes);
52
+ void traverse (const Module* module, AstStat* src, NotNull<BuiltinTypes> builtinTypes);
51
53
52
- std::pair<bool , TypeId> checkForAnyCast (Scope* scope, AstExprTypeAssertion* expr);
53
-
54
- // Todo: errors resolved by anys
55
- void reportError (Location location, TypeErrorData err);
54
+ std::pair<bool , TypeId> checkForAnyCast (const Scope* scope, AstExprTypeAssertion* expr);
56
55
57
56
bool containsAny (TypePackId typ);
58
57
bool containsAny (TypeId typ);
59
58
60
- bool isAnyCast (Scope* scope, AstExpr* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
61
- bool isAnyCall (Scope* scope, AstExpr* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
59
+ bool isAnyCast (const Scope* scope, AstExpr* expr, const Module* module, NotNull<BuiltinTypes> builtinTypes);
60
+ bool isAnyCall (const Scope* scope, AstExpr* expr, const Module* module, NotNull<BuiltinTypes> builtinTypes);
62
61
63
- bool hasVariadicAnys (Scope* scope, AstExprFunction* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
64
- bool hasArgAnys (Scope* scope, AstExprFunction* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
65
- bool hasAnyReturns (Scope* scope, AstExprFunction* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
62
+ bool hasVariadicAnys (const Scope* scope, AstExprFunction* expr, const Module* module, NotNull<BuiltinTypes> builtinTypes);
63
+ bool hasArgAnys (const Scope* scope, AstExprFunction* expr, const Module* module, NotNull<BuiltinTypes> builtinTypes);
64
+ bool hasAnyReturns (const Scope* scope, AstExprFunction* expr, const Module* module, NotNull<BuiltinTypes> builtinTypes);
66
65
67
- TypeId checkForFamilyInhabitance (TypeId instance, Location location);
68
- TypeId lookupType (AstExpr* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
69
- TypePackId reconstructTypePack (AstArray<AstExpr*> exprs, Module* module, NotNull<BuiltinTypes> builtinTypes);
66
+ TypeId checkForFamilyInhabitance (const TypeId instance, Location location);
67
+ TypeId lookupType (const AstExpr* expr, const Module* module, NotNull<BuiltinTypes> builtinTypes);
68
+ TypePackId reconstructTypePack (const AstArray<AstExpr*> exprs, const Module* module, NotNull<BuiltinTypes> builtinTypes);
70
69
71
70
DenseHashSet<TypeId> seenTypeFunctionInstances{nullptr };
72
- TypeId lookupAnnotation (AstType* annotation, Module* module, NotNull<BuiltinTypes> builtintypes);
73
- std::optional<TypePackId> lookupPackAnnotation (AstTypePack* annotation, Module* module);
74
- TypeId checkForTypeFunctionInhabitance (TypeId instance, Location location);
71
+ TypeId lookupAnnotation (AstType* annotation, const Module* module, NotNull<BuiltinTypes> builtintypes);
72
+ std::optional<TypePackId> lookupPackAnnotation (AstTypePack* annotation, const Module* module);
73
+ TypeId checkForTypeFunctionInhabitance (const TypeId instance, const Location location);
75
74
76
- enum Pattern : uint64_t
75
+ enum Pattern: uint64_t
77
76
{
78
77
Casts,
79
78
FuncArg,
@@ -91,41 +90,58 @@ struct AnyTypeSummary
91
90
Pattern code;
92
91
std::string node;
93
92
TelemetryTypePair type;
94
- std::string debug;
95
93
96
94
explicit TypeInfo (Pattern code, std::string node, TelemetryTypePair type);
97
95
};
98
96
97
+ struct FindReturnAncestry final : public AstVisitor
98
+ {
99
+ AstNode* currNode{nullptr };
100
+ AstNode* stat{nullptr };
101
+ Position rootEnd;
102
+ bool found = false ;
103
+
104
+ explicit FindReturnAncestry (AstNode* stat, Position rootEnd);
105
+
106
+ bool visit (AstType* node) override ;
107
+ bool visit (AstNode* node) override ;
108
+ bool visit (AstStatFunction* node) override ;
109
+ bool visit (AstStatLocalFunction* node) override ;
110
+ };
111
+
99
112
std::vector<TypeInfo> typeInfo;
100
113
101
114
/* *
102
115
* Fabricates a scope that is a child of another scope.
103
116
* @param node the lexical node that the scope belongs to.
104
117
* @param parent the parent scope of the new scope. Must not be null.
105
118
*/
106
- Scope* childScope (AstNode* node, const Scope* parent);
107
-
108
- Scope* findInnerMostScope (Location location, Module* module);
109
-
110
- void visit (Scope* scope, AstStat* stat, Module* module, NotNull<BuiltinTypes> builtinTypes);
111
- void visit (Scope* scope, AstStatBlock* block, Module* module, NotNull<BuiltinTypes> builtinTypes);
112
- void visit (Scope* scope, AstStatIf* ifStatement, Module* module, NotNull<BuiltinTypes> builtinTypes);
113
- void visit (Scope* scope, AstStatWhile* while_, Module* module, NotNull<BuiltinTypes> builtinTypes);
114
- void visit (Scope* scope, AstStatRepeat* repeat, Module* module, NotNull<BuiltinTypes> builtinTypes);
115
- void visit (Scope* scope, AstStatReturn* ret, Module* module, NotNull<BuiltinTypes> builtinTypes);
116
- void visit (Scope* scope, AstStatLocal* local, Module* module, NotNull<BuiltinTypes> builtinTypes);
117
- void visit (Scope* scope, AstStatFor* for_, Module* module, NotNull<BuiltinTypes> builtinTypes);
118
- void visit (Scope* scope, AstStatForIn* forIn, Module* module, NotNull<BuiltinTypes> builtinTypes);
119
- void visit (Scope* scope, AstStatAssign* assign, Module* module, NotNull<BuiltinTypes> builtinTypes);
120
- void visit (Scope* scope, AstStatCompoundAssign* assign, Module* module, NotNull<BuiltinTypes> builtinTypes);
121
- void visit (Scope* scope, AstStatFunction* function, Module* module, NotNull<BuiltinTypes> builtinTypes);
122
- void visit (Scope* scope, AstStatLocalFunction* function, Module* module, NotNull<BuiltinTypes> builtinTypes);
123
- void visit (Scope* scope, AstStatTypeAlias* alias, Module* module, NotNull<BuiltinTypes> builtinTypes);
124
- void visit (Scope* scope, AstStatExpr* expr, Module* module, NotNull<BuiltinTypes> builtinTypes);
125
- void visit (Scope* scope, AstStatDeclareGlobal* declareGlobal, Module* module, NotNull<BuiltinTypes> builtinTypes);
126
- void visit (Scope* scope, AstStatDeclareClass* declareClass, Module* module, NotNull<BuiltinTypes> builtinTypes);
127
- void visit (Scope* scope, AstStatDeclareFunction* declareFunction, Module* module, NotNull<BuiltinTypes> builtinTypes);
128
- void visit (Scope* scope, AstStatError* error, Module* module, NotNull<BuiltinTypes> builtinTypes);
119
+ const Scope* childScope (const AstNode* node, const Scope* parent);
120
+
121
+ std::optional<AstExpr*> matchRequire (const AstExprCall& call);
122
+ AstNode* getNode (AstStatBlock* root, AstNode* node);
123
+ const Scope* findInnerMostScope (const Location location, const Module* module);
124
+ const AstNode* findAstAncestryAtLocation (const AstStatBlock* root, AstNode* node);
125
+
126
+ void visit (const Scope* scope, AstStat* stat, const Module* module, NotNull<BuiltinTypes> builtinTypes);
127
+ void visit (const Scope* scope, AstStatBlock* block, const Module* module, NotNull<BuiltinTypes> builtinTypes);
128
+ void visit (const Scope* scope, AstStatIf* ifStatement, const Module* module, NotNull<BuiltinTypes> builtinTypes);
129
+ void visit (const Scope* scope, AstStatWhile* while_, const Module* module, NotNull<BuiltinTypes> builtinTypes);
130
+ void visit (const Scope* scope, AstStatRepeat* repeat, const Module* module, NotNull<BuiltinTypes> builtinTypes);
131
+ void visit (const Scope* scope, AstStatReturn* ret, const Module* module, NotNull<BuiltinTypes> builtinTypes);
132
+ void visit (const Scope* scope, AstStatLocal* local, const Module* module, NotNull<BuiltinTypes> builtinTypes);
133
+ void visit (const Scope* scope, AstStatFor* for_, const Module* module, NotNull<BuiltinTypes> builtinTypes);
134
+ void visit (const Scope* scope, AstStatForIn* forIn, const Module* module, NotNull<BuiltinTypes> builtinTypes);
135
+ void visit (const Scope* scope, AstStatAssign* assign, const Module* module, NotNull<BuiltinTypes> builtinTypes);
136
+ void visit (const Scope* scope, AstStatCompoundAssign* assign, const Module* module, NotNull<BuiltinTypes> builtinTypes);
137
+ void visit (const Scope* scope, AstStatFunction* function, const Module* module, NotNull<BuiltinTypes> builtinTypes);
138
+ void visit (const Scope* scope, AstStatLocalFunction* function, const Module* module, NotNull<BuiltinTypes> builtinTypes);
139
+ void visit (const Scope* scope, AstStatTypeAlias* alias, const Module* module, NotNull<BuiltinTypes> builtinTypes);
140
+ void visit (const Scope* scope, AstStatExpr* expr, const Module* module, NotNull<BuiltinTypes> builtinTypes);
141
+ void visit (const Scope* scope, AstStatDeclareGlobal* declareGlobal, const Module* module, NotNull<BuiltinTypes> builtinTypes);
142
+ void visit (const Scope* scope, AstStatDeclareClass* declareClass, const Module* module, NotNull<BuiltinTypes> builtinTypes);
143
+ void visit (const Scope* scope, AstStatDeclareFunction* declareFunction, const Module* module, NotNull<BuiltinTypes> builtinTypes);
144
+ void visit (const Scope* scope, AstStatError* error, const Module* module, NotNull<BuiltinTypes> builtinTypes);
129
145
};
130
146
131
147
} // namespace Luau
0 commit comments