Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Analysis/include/Luau/DcrLogger.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/Constraint.h"
5
#include "Luau/NotNull.h"
6
#include "Luau/Scope.h"
7
#include "Luau/Module.h"
8
#include "Luau/ToString.h"
9
#include "Luau/Error.h"
10
#include "Luau/Variant.h"
11
12
#include <optional>
13
#include <string>
14
#include <vector>
15
16
namespace Luau
17
{
18
19
struct ErrorSnapshot
20
{
21
std::string message;
22
Location location;
23
};
24
25
struct BindingSnapshot
26
{
27
std::string typeId;
28
std::string typeString;
29
Location location;
30
};
31
32
struct TypeBindingSnapshot
33
{
34
std::string typeId;
35
std::string typeString;
36
};
37
38
struct ExprTypesAtLocation
39
{
40
Location location;
41
TypeId ty;
42
std::optional<TypeId> expectedTy;
43
};
44
45
struct AnnotationTypesAtLocation
46
{
47
Location location;
48
TypeId resolvedTy;
49
};
50
51
struct ConstraintGenerationLog
52
{
53
std::string source;
54
std::vector<ErrorSnapshot> errors;
55
56
std::vector<ExprTypesAtLocation> exprTypeLocations;
57
std::vector<AnnotationTypesAtLocation> annotationTypeLocations;
58
};
59
60
struct ScopeSnapshot
61
{
62
std::unordered_map<Name, BindingSnapshot> bindings;
63
std::unordered_map<Name, TypeBindingSnapshot> typeBindings;
64
std::unordered_map<Name, TypeBindingSnapshot> typePackBindings;
65
std::vector<ScopeSnapshot> children;
66
};
67
68
using ConstraintBlockTarget = Variant<TypeId, TypePackId, NotNull<const Constraint>>;
69
70
struct ConstraintBlock
71
{
72
ConstraintBlockTarget target;
73
std::string stringification;
74
};
75
76
struct ConstraintSnapshot
77
{
78
std::string stringification;
79
Location location;
80
std::vector<ConstraintBlock> blocks;
81
};
82
83
struct BoundarySnapshot
84
{
85
DenseHashMap<const Constraint*, ConstraintSnapshot> unsolvedConstraints{nullptr};
86
ScopeSnapshot rootScope;
87
DenseHashMap<const void*, std::string> typeStrings{nullptr};
88
};
89
90
struct StepSnapshot
91
{
92
const Constraint* currentConstraint;
93
bool forced;
94
DenseHashMap<const Constraint*, ConstraintSnapshot> unsolvedConstraints{nullptr};
95
ScopeSnapshot rootScope;
96
DenseHashMap<const void*, std::string> typeStrings{nullptr};
97
};
98
99
struct TypeSolveLog
100
{
101
BoundarySnapshot initialState;
102
std::vector<StepSnapshot> stepStates;
103
BoundarySnapshot finalState;
104
};
105
106
struct TypeCheckLog
107
{
108
std::vector<ErrorSnapshot> errors;
109
};
110
111
struct DcrLogger
112
{
113
std::string compileOutput();
114
115
void captureSource(std::string source);
116
void captureGenerationError(const TypeError& error);
117
void captureConstraintLocation(NotNull<const Constraint> constraint, Location location);
118
void captureGenerationModule(const ModulePtr& module);
119
120
void pushBlock(NotNull<const Constraint> constraint, TypeId block);
121
void pushBlock(NotNull<const Constraint> constraint, TypePackId block);
122
void pushBlock(NotNull<const Constraint> constraint, NotNull<const Constraint> block);
123
void popBlock(TypeId block);
124
void popBlock(TypePackId block);
125
void popBlock(NotNull<const Constraint> block);
126
127
void captureInitialSolverState(const Scope* rootScope, const std::vector<NotNull<const Constraint>>& unsolvedConstraints);
128
StepSnapshot prepareStepSnapshot(
129
const Scope* rootScope,
130
NotNull<const Constraint> current,
131
bool force,
132
const std::vector<NotNull<const Constraint>>& unsolvedConstraints
133
);
134
void commitStepSnapshot(StepSnapshot snapshot);
135
void captureFinalSolverState(const Scope* rootScope, const std::vector<NotNull<const Constraint>>& unsolvedConstraints);
136
137
void captureTypeCheckError(const TypeError& error);
138
139
private:
140
ConstraintGenerationLog generationLog;
141
std::unordered_map<NotNull<const Constraint>, std::vector<ConstraintBlockTarget>> constraintBlocks;
142
TypeSolveLog solveLog;
143
TypeCheckLog checkLog;
144
145
ToStringOptions opts{true};
146
147
std::vector<ConstraintBlock> snapshotBlocks(NotNull<const Constraint> constraint);
148
void captureBoundaryState(BoundarySnapshot& target, const Scope* rootScope, const std::vector<NotNull<const Constraint>>& unsolvedConstraints);
149
};
150
151
} // namespace Luau
152
153