This repository was archived by the owner on Oct 15, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathCodeModel.fs
More file actions
331 lines (300 loc) · 8.98 KB
/
Copy pathCodeModel.fs
File metadata and controls
331 lines (300 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2018 Fabulous contributors. See LICENSE.md for license.
module FSharp.Compiler.PortaCode.CodeModel
open MessagePack
open MessagePack.FSharp
open MessagePack.Resolvers
[<MessagePackObject>]
type DRange =
{
[<Key(0)>]
File: string
[<Key(1)>]
StartLine: int
[<Key(2)>]
StartColumn: int
[<Key(3)>]
EndLine: int
[<Key(4)>]
EndColumn: int
}
override range.ToString() =
sprintf "%s: (%d,%d)-(%d-%d)" range.File range.StartLine range.StartColumn range.EndLine range.EndColumn
[<MessagePackObject>]
type DDiagnostic =
{
[<Key(0)>]
Severity: int
[<Key(1)>]
Number: int
[<Key(2)>]
Message: string
[<Key(3)>]
LocationStack: DRange []
}
[<Key(4)>]
member diag.Location = Array.last diag.LocationStack
override diag.ToString() =
let sev =
match diag.Severity with
| 0 -> "info"
| 1 -> "warning"
| _ -> "error"
match List.ofArray (Array.rev diag.LocationStack) with
| [] -> sprintf "%s LC%d: %O" sev diag.Number diag.Message
| loc :: t ->
[
sprintf "%O: %s LC%d: %O" loc sev diag.Number diag.Message
for loc in t do
sprintf " stack: %O" loc
]
|> String.concat "\n"
/// A representation of resolved F# expressions that can be serialized
[<MessagePackObject>]
type DExpr =
| Value of DLocalRef
| ThisValue of DType
| BaseValue of DType
| Application of DExpr * DType [] * DExpr [] * DRange option
| Lambda of DType * DType * DLocalDef * DExpr
| TypeLambda of DGenericParameterDef [] * DExpr
| Quote of DExpr
| IfThenElse of DExpr * DExpr * DExpr
| DecisionTree of DExpr * (DLocalDef [] * DExpr) []
| DecisionTreeSuccess of int * DExpr []
| Call of DExpr option * DMemberRef * DType [] * DType [] * DExpr [] * DRange option
| NewObject of DMemberRef * DType [] * DExpr [] * DRange option
| LetRec of (DLocalDef * DExpr) [] * DExpr
| Let of (DLocalDef * DExpr) * DExpr
| NewRecord of DType * DExpr [] * DRange option
| NewAnonRecord of DFieldRef [] * DExpr [] * DRange option
| ObjectExpr of DType * DExpr * DObjectExprOverrideDef [] * (DType * DObjectExprOverrideDef []) []
| AnonRecordGet of DExpr * DFieldRef * DRange option
| FSharpFieldGet of DExpr option * DType * DFieldRef * DRange option
| FSharpFieldSet of DExpr option * DType * DFieldRef * DExpr * DRange option
| NewUnionCase of DType * DUnionCaseRef * DExpr [] * DRange option
| UnionCaseGet of DExpr * DType * DUnionCaseRef * DFieldRef
| UnionCaseSet of DExpr * DType * DUnionCaseRef * DFieldRef * DExpr
| UnionCaseTag of DExpr * DType
| UnionCaseTest of DExpr * DType * DUnionCaseRef
| TraitCall of DType [] * string * isInstance: bool * DType [] * DType [] * DExpr [] * DRange option
| NewTuple of DType * DExpr []
| TupleGet of DType * int * DExpr
| Coerce of DType * DExpr
| NewArray of DType * DExpr []
| TypeTest of DType * DExpr
| AddressSet of DExpr * DExpr
| ValueSet of Choice<DLocalRef, DMemberRef> * DExpr * DRange option
| Unused
| DefaultValue of DType
| Const of obj * DType
| AddressOf of DExpr
| Sequential of DExpr * DExpr * DRange option
| FastIntegerForLoop of DExpr * DExpr * DExpr * bool
| WhileLoop of DExpr * DExpr
| TryFinally of DExpr * DExpr
| TryWith of DExpr * DLocalDef * DExpr * DLocalDef * DExpr
| NewDelegate of DType * DExpr
| ILFieldGet of DExpr option * DType * string
| ILFieldSet of DExpr option * DType * string * DExpr
| ILAsm of string * DType [] * DExpr []
and [<MessagePackObject>] DType =
| DNamedType of DEntityRef * DType []
| DFunctionType of DType * DType
| DTupleType of bool * DType []
| DAnonRecdType of bool * string [] * DType []
| DArrayType of int * DType
| DByRefType of DType
| DVariableType of string
and [<MessagePackObject>] DLocalDef =
{
[<Key(0)>]
Name: string
[<Key(1)>]
IsMutable: bool
[<Key(2)>]
LocalType: DType
[<Key(3)>]
Range: DRange option
[<Key(4)>]
IsCompilerGenerated: bool
}
and [<MessagePackObject>] DFieldDef =
{
[<Key(0)>]
Name: string
[<Key(1)>]
IsStatic: bool
[<Key(2)>]
IsMutable: bool
[<Key(3)>]
FieldType: DType
[<Key(4)>]
Range: DRange option
[<Key(5)>]
IsCompilerGenerated: bool
}
and [<MessagePackObject>] DSlotRef =
{
[<Key 0>]
Member: DMemberRef
[<Key 1>]
DeclaringType: DType
}
and [<MessagePackObject>] DMemberDef =
{
[<Key(0)>]
EnclosingEntity: DEntityRef
[<Key(1)>]
Name: string
[<Key(2)>]
GenericParameters: DGenericParameterDef []
[<Key(3)>]
ImplementedSlots: DSlotRef []
[<Key(4)>]
IsInstance: bool
[<Key(5)>]
IsValue: bool
[<Key(6)>]
IsCompilerGenerated: bool
[<Key(7)>]
CustomAttributes: DCustomAttributeDef []
[<Key(8)>]
Parameters: DLocalDef []
[<Key(9)>]
ReturnType: DType
[<Key(10)>]
Range: DRange option
}
[<Key(11)>]
member x.Ref =
{
Entity = x.EnclosingEntity
Name = x.Name
GenericArity = x.GenericParameters.Length
ArgTypes = (x.Parameters |> Array.map (fun p -> p.LocalType))
ReturnType = x.ReturnType
}
and [<MessagePackObject>] DGenericParameterDef =
{
[<Key(0)>]
Name: string
[<Key(1)>]
InterfaceConstraints: DType []
[<Key(2)>]
BaseTypeConstraint: DType option
[<Key(3)>]
DefaultConstructorConstraint: bool
[<Key(4)>]
NotNullableValueTypeConstraint: bool
[<Key(5)>]
ReferenceTypeConstraint: bool
}
and [<MessagePackObject>] DEntityDef =
{
[<Key(0)>]
QualifiedName: string
[<Key(1)>]
Name: string
[<Key(2)>]
GenericParameters: DGenericParameterDef []
[<Key(3)>]
BaseType: DType option
[<Key(4)>]
DeclaredInterfaces: DType []
[<Key(5)>]
DeclaredFields: DFieldDef []
[<Key(6)>]
DeclaredDispatchSlots: DMemberDef []
[<Key(7)>]
IsUnion: bool
[<Key(8)>]
IsRecord: bool
[<Key(9)>]
IsStruct: bool
[<Key(10)>]
IsInterface: bool
[<Key(11)>]
CustomAttributes: DCustomAttributeDef []
//IsAbstractClass: bool
[<Key(12)>]
UnionCases: string []
[<Key(13)>]
Range: DRange option
}
[<Key(14)>]
member x.Ref = DEntityRef x.QualifiedName
and [<MessagePackObject>] DCustomAttributeDef =
{
[<Key(0)>]
AttributeType: DEntityRef
[<Key(1)>]
ConstructorArguments: (DType * obj) []
[<Key(2)>]
NamedArguments: (DType * string * bool * obj) []
}
and [<MessagePackObject>] DEntityRef = | DEntityRef of string
and [<MessagePackObject>] DMemberRef =
{
[<Key(0)>]
Entity: DEntityRef
[<Key(1)>]
Name: string
[<Key(2)>]
GenericArity: int
[<Key(3)>]
ArgTypes: DType []
[<Key(4)>]
ReturnType: DType
}
and [<MessagePackObject>] DLocalRef =
{
[<Key(0)>]
Name: string
[<Key(1)>]
IsThisValue: bool
[<Key(2)>]
IsMutable: bool
[<Key(3)>]
IsCompilerGenerated: bool
[<Key(4)>]
Range: DRange option
}
and [<MessagePackObject>] DFieldRef = | DFieldRef of int * string
and [<MessagePackObject>] DUnionCaseRef = | DUnionCaseRef of string
and [<MessagePackObject>] DObjectExprOverrideDef =
{
[<Key(0)>]
Name: string
[<Key(1)>]
Slot: DSlotRef
[<Key(2)>]
GenericParameters: DGenericParameterDef []
[<Key(3)>]
Parameters: DLocalDef []
[<Key(4)>]
Body: DExpr
}
[<MessagePackObject>]
type DDecl =
| DDeclEntity of DEntityDef * DDecl []
| DDeclMember of DMemberDef * DExpr * isLiveCheck: bool
| InitAction of DExpr * DRange option
[<MessagePackObject>]
type DFile =
{
[<Key(0)>]
Code: DDecl []
}
let CodeChangesPackOptions =
MessagePackSerializerOptions.Standard.WithResolver(Resolvers.CompositeResolver.Create(FSharpResolver.Instance, StandardResolver.Instance))
[<MessagePackObject>]
type CodeChangesPack =
{
[<Key(0)>]
Changes: (string * DFile) []
}
static member FromBytes (bytes: byte[]) =
use stream = new System.IO.MemoryStream(bytes)
MessagePackSerializer.Deserialize<CodeChangesPack>(stream, CodeChangesPackOptions)
member this.ToBytes () =
MessagePackSerializer.Serialize(this, CodeChangesPackOptions)