/
debug.ml
935 lines (810 loc) · 25.6 KB
/
debug.ml
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
(**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "flow" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
module Json = Hh_json
open Reason_js
open Type
open Utils
let string_of_pred_ctor = function
| AndP _ -> "AndP"
| OrP _ -> "OrP"
| NotP _ -> "NotP"
| LeftP _ -> "LeftP"
| RightP _ -> "RightP"
| ExistsP -> "ExistsP"
| TrueP -> "TrueP"
| FalseP -> "FalseP"
| VoidP -> "VoidP"
| NullP -> "NullP"
| MaybeP -> "MaybeP"
| BoolP -> "BoolP"
| StrP -> "StrP"
| NumP -> "NumP"
| FunP -> "FunP"
| ObjP -> "ObjP"
| ArrP -> "ArrP"
let string_of_binary_test_ctor = function
| Instanceof -> "Instanceof"
| SentinelProp _ -> "SentinelProp"
type json_cx = {
stack: ISet.t;
depth: int;
cx: Context.t;
}
let check_depth continuation json_cx =
let depth = json_cx.depth - 1 in
if depth < 0
then fun _ -> Json.JNull
else continuation { json_cx with depth; }
let rec _json_of_t json_cx = check_depth _json_of_t_impl json_cx
and _json_of_t_impl json_cx t = Json.(
JAssoc ([
"reason", json_of_reason (reason_of_t t);
"kind", JString (string_of_ctor t)
] @
match t with
| OpenT (_, id) -> [
"id", JInt id
] @
if ISet.mem id json_cx.stack then []
else [
"node", json_of_node json_cx id
]
| NumT (_, lit) ->
begin match lit with
| Literal (_, raw) -> ["literal", JString raw]
| Truthy -> ["refinement", JString "Truthy"]
| Falsy -> ["refinement", JString "Falsy"]
| AnyLiteral -> []
end
| StrT (_, lit) ->
begin match lit with
| Literal s -> ["literal", JString s]
| Truthy -> ["refinement", JString "Truthy"]
| Falsy -> ["refinement", JString "Falsy"]
| AnyLiteral -> []
end
| BoolT (_, b) ->
(match b with
| Some b -> ["literal", JBool b]
| None -> [])
| UndefT _
| MixedT _
| AnyT _
| NullT _
| VoidT _ ->
[]
| FunT (_, static, proto, funtype) -> [
"static", _json_of_t json_cx static;
"prototype", _json_of_t json_cx proto;
"funType", json_of_funtype json_cx funtype
]
| ObjT (_, objtype) -> [
"type", json_of_objtype json_cx objtype
]
| ArrT (_, elemt, tuplet) -> [
"elemType", _json_of_t json_cx elemt;
"tupleType", JList (List.map (_json_of_t json_cx) tuplet)
]
| ClassT t -> [
"type", _json_of_t json_cx t
]
| InstanceT (_, static, super, instance) -> [
"static", _json_of_t json_cx static;
"super", _json_of_t json_cx super;
"instance", json_of_insttype json_cx instance
]
| OptionalT t
| RestT t -> [
"type", _json_of_t json_cx t
]
| PolyT (tparams, t) -> [
"typeParams", JList (List.map (json_of_typeparam json_cx) tparams);
"type", _json_of_t json_cx t
]
| TypeAppT (t, targs) -> [
"typeArgs", JList (List.map (_json_of_t json_cx) targs);
"type", _json_of_t json_cx t
]
| BoundT tparam -> [
"typeParam", json_of_typeparam json_cx tparam
]
| ExistsT tparam ->
[]
| MaybeT t -> [
"type", _json_of_t json_cx t
]
| TaintedT (_,t) -> [
"type", _json_of_t json_cx t
]
| WrapTaintT (_,t) -> [
"type", _json_of_t json_cx t
]
| IntersectionT (_, ts)
| UnionT (_, ts) -> [
"types", JList (List.map (_json_of_t json_cx) ts)
]
| UpperBoundT t
| LowerBoundT t -> [
"type", _json_of_t json_cx t
]
| AnyObjT _
| AnyFunT _ ->
[]
| ShapeT t -> [
"type", _json_of_t json_cx t
]
| DiffT (t1, t2) -> [
"type1", _json_of_t json_cx t1;
"type2", _json_of_t json_cx t2
]
| KeysT (_, t) -> [
"type", _json_of_t json_cx t
]
| SingletonStrT (_, s) -> [
"literal", JString s
]
| SingletonNumT (_, (_, raw)) -> [
"literal", JString raw
]
| SingletonBoolT (_, b) -> [
"literal", JBool b
]
| TypeT (_, t) -> [
"result", _json_of_t json_cx t
]
| AnnotT (t1, t2) -> [
"assert", _json_of_t json_cx t1;
"assume", _json_of_t json_cx t2
]
| BecomeT (_, t) -> [
"result", _json_of_t json_cx t
]
| SpeculativeMatchFailureT (_, attempt, target) -> [
"attemptType", _json_of_t json_cx attempt;
"targetType", _json_of_t json_cx target
]
| ModuleT (_, {exports_tmap; cjs_export;}) ->
let property_maps = Context.property_maps json_cx.cx in
let tmap = IMap.find_unsafe exports_tmap property_maps in
let cjs_export = match cjs_export with
| Some(t) -> _json_of_t json_cx t
| None -> JNull
in
[
"namedExports", json_of_tmap json_cx tmap;
"cjsExport", cjs_export;
]
| SummarizeT (_, t) -> [
"type", _json_of_t json_cx t
]
| CallT (_, funtype) -> [
"funType", json_of_funtype json_cx funtype
]
| MethodT (_, name, funtype) -> [
"name", json_of_propname json_cx name;
"funType", json_of_funtype json_cx funtype
]
| ReposLowerT (_, t)
| ReposUpperT (_, t) -> [
"type", _json_of_t json_cx t
]
| SetPropT (_, name, t)
| GetPropT (_, name, t) -> [
"propName", json_of_propname json_cx name;
"propType", _json_of_t json_cx t
]
| SetElemT (_, indext, elemt)
| GetElemT (_, indext, elemt) -> [
"indexType", _json_of_t json_cx indext;
"elemType", _json_of_t json_cx elemt
]
| ConstructorT (_, tparams, t) -> [
"typeParams", JList (List.map (_json_of_t json_cx) tparams);
"type", _json_of_t json_cx t
]
| SuperT (_, instance) -> [
"instance", json_of_insttype json_cx instance
]
| ExtendsT (_, t1, t2) -> [
"type1", _json_of_t json_cx t1;
"type2", _json_of_t json_cx t2
]
| AdderT (_, l, r) -> [
"leftType", _json_of_t json_cx l;
"rightType", _json_of_t json_cx r
]
| ComparatorT (_, t) -> [
"type", _json_of_t json_cx t
]
| UnaryMinusT (_, t) -> [
"type", _json_of_t json_cx t
]
| PredicateT (p, t) -> [
"pred", json_of_pred json_cx p;
"type", _json_of_t json_cx t
]
| EqT (_, t) -> [
"type", _json_of_t json_cx t
]
| AndT (_, right, res)
| OrT (_, right, res) -> [
"rightType", _json_of_t json_cx right;
"resultType", _json_of_t json_cx res
]
| NotT (_, t) -> [
"type", _json_of_t json_cx t
]
| SpecializeT (_, cache, targs, tvar) -> [
"cache", JBool cache;
"types", JList (List.map (_json_of_t json_cx) targs);
"tvar", _json_of_t json_cx tvar
]
| LookupT (_, rstrict, _, name, t) ->
(match rstrict with
| None -> []
| Some r -> ["strictReason", json_of_reason r]
) @ [
"name", JString name;
"type", _json_of_t json_cx t
]
| ObjAssignT (_, assignee, tvar, prop_names, flag) -> [
"assigneeType", _json_of_t json_cx assignee;
"resultType", _json_of_t json_cx tvar;
"propNames", JList (List.map (fun s -> JString s) prop_names);
"flag", JBool flag
]
| ObjFreezeT (_, t) -> [
"type", _json_of_t json_cx t
]
| ObjRestT (_, excludes, tvar) -> [
"excludedProps", JList (List.map (fun s -> JString s) excludes);
"resultType", _json_of_t json_cx tvar;
]
| ObjSealT (_, t) -> [
"type", _json_of_t json_cx t
]
| ObjTestT (_, default, res) -> [
"defaultType", _json_of_t json_cx default;
"resultType", _json_of_t json_cx res
]
| UnifyT (t1, t2) -> [
"type1", _json_of_t json_cx t1;
"type2", _json_of_t json_cx t2
]
| ConcretizeT (l, todo_list, done_list, u) -> [
"inType", _json_of_t json_cx l;
"todoTypes", JList (List.map (_json_of_t json_cx) todo_list);
"doneTypes", JList (List.map (_json_of_t json_cx) done_list);
"absType", _json_of_t json_cx u
]
| ConcreteT t
| GetKeysT (_, t) -> [
"type", _json_of_t json_cx t
]
| HasKeyT (_, key) -> [
"key", JString key
]
| ElemT (_, base, elem) -> [
"baseType", _json_of_t json_cx base;
"elemType", _json_of_t json_cx elem
]
| CJSRequireT (_, export) -> [
"export",
_json_of_t json_cx export
]
| ImportModuleNsT (_, t)
| ImportTypeT (_, t)
| ImportTypeofT (_, t)
-> ["type", _json_of_t json_cx t]
| CJSExtractNamedExportsT (_, module_t, t_out) -> [
"module", _json_of_t json_cx module_t;
"t_out", _json_of_t json_cx t_out;
]
| SetNamedExportsT (_, tmap, t_out) -> [
"tmap", json_of_tmap json_cx tmap;
"t_out", _json_of_t json_cx t_out;
]
| SetCJSExportT (_, t, t_out) -> [
"cjsExportType", _json_of_t json_cx t;
"t_out", _json_of_t json_cx t_out;
]
))
and json_of_polarity json_cx = check_depth json_of_polarity_impl json_cx
and json_of_polarity_impl json_cx polarity =
Json.JString (match polarity with
| Negative -> "Negative"
| Neutral -> "Neutral"
| Positive -> "Positive"
)
and json_of_typeparam json_cx = check_depth json_of_typeparam_impl json_cx
and json_of_typeparam_impl json_cx tparam = Json.(
JAssoc [
"reason", json_of_reason tparam.reason;
"name", JString tparam.name;
"bound", _json_of_t json_cx tparam.bound;
"polarity", json_of_polarity json_cx tparam.polarity;
]
)
and json_of_objtype json_cx = check_depth json_of_objtype_impl json_cx
and json_of_objtype_impl json_cx objtype = Json.(
let property_maps = Context.property_maps json_cx.cx in
let tmap = IMap.find_unsafe objtype.props_tmap property_maps in
JAssoc ([
"flags", json_of_flags json_cx objtype.flags;
] @ (match objtype.dict_t with
| None -> []
| Some d -> ["dictType", json_of_dicttype json_cx d]
) @ [
"propTypes", json_of_tmap json_cx tmap;
"prototype", _json_of_t json_cx objtype.proto_t
])
)
and json_of_dicttype json_cx = check_depth json_of_dicttype_impl json_cx
and json_of_dicttype_impl json_cx dicttype = Json.(
JAssoc (
(match dicttype.dict_name with
| None -> []
| Some name -> ["name", JString name]
) @ [
"keyType", _json_of_t json_cx dicttype.key;
"valueType", _json_of_t json_cx dicttype.value
])
)
and json_of_flags json_cx = check_depth json_of_flags_impl json_cx
and json_of_flags_impl json_cx flags = Json.(
JAssoc [
"frozen", JBool flags.frozen;
"sealed", JBool (match flags.sealed with
| Sealed -> true
| UnsealedInFile _ -> false);
"exact", JBool flags.exact;
]
)
and json_of_changeset json_cx = check_depth json_of_changeset_impl json_cx
and json_of_changeset_impl json_cx = Json.(
let json_of_entry_ref (scope_id, name, op) =
JAssoc [
"scope_id", JInt scope_id;
"name", JString name;
"op", JString (Changeset.string_of_op op)
]
in
let json_of_changed_vars changed_vars =
JList (List.rev (Changeset.EntryRefSet.fold
(fun entry_ref acc -> json_of_entry_ref entry_ref :: acc)
changed_vars []
))
in
let json_of_refi_ref (scope_id, key, op) =
JAssoc [
"scope_id", JInt scope_id;
"key", JString (Key.string_of_key key);
"op", JString (Changeset.string_of_op op)
]
in
let json_of_changed_refis changed_refis =
JList (List.rev (Changeset.RefiRefSet.fold
(fun refi_ref acc -> json_of_refi_ref refi_ref :: acc)
changed_refis []
))
in
fun (changed_vars, changed_refis) ->
JAssoc [
"vars", json_of_changed_vars changed_vars;
"refis", json_of_changed_refis changed_refis
]
)
and json_of_funtype json_cx = check_depth json_of_funtype_impl json_cx
and json_of_funtype_impl json_cx {
this_t;
params_tlist;
params_names;
return_t;
closure_t;
changeset
} = Json.(
JAssoc ([
"thisType", _json_of_t json_cx this_t;
"paramTypes", JList (List.map (_json_of_t json_cx) params_tlist)
] @ (match params_names with
| None -> []
| Some names -> ["paramNames", JList (List.map (fun s -> JString s) names)]
) @ [
"returnType", _json_of_t json_cx return_t;
"closureIndex", JInt closure_t;
"changeset", json_of_changeset json_cx changeset
])
)
and json_of_insttype json_cx = check_depth json_of_insttype_impl json_cx
and json_of_insttype_impl json_cx insttype = Json.(
let property_maps = Context.property_maps json_cx.cx in
let field_tmap = IMap.find_unsafe insttype.fields_tmap property_maps in
let method_tmap = IMap.find_unsafe insttype.methods_tmap property_maps in
JAssoc [
"classId", JInt insttype.class_id;
"typeArgs", json_of_tmap json_cx insttype.type_args;
"argPolarities", json_of_polarity_map json_cx insttype.arg_polarities;
"fieldTypes", json_of_tmap json_cx field_tmap;
"methodTypes", json_of_tmap json_cx method_tmap;
"mixins", JBool insttype.mixins;
"structural", JBool insttype.structural;
]
)
and json_of_polarity_map json_cx = check_depth json_of_polarity_map_impl json_cx
and json_of_polarity_map_impl json_cx pmap = Json.(
let lst = SMap.fold (fun name pol acc ->
JAssoc ["name", JString name; "polarity", json_of_polarity json_cx pol] :: acc
) pmap [] in
JList (List.rev lst)
)
and json_of_propname json_cx = check_depth json_of_propname_impl json_cx
and json_of_propname_impl json_cx (reason, literal) = Json.(
JAssoc [
"reason", json_of_reason reason;
"literal", JString literal;
]
)
and json_of_tmap json_cx = check_depth json_of_tmap_impl json_cx
and json_of_tmap_impl json_cx bindings = Json.(
let lst = SMap.fold (fun name t acc ->
json_of_type_binding json_cx (name, t) :: acc
) bindings [] in
JList (List.rev lst)
)
and json_of_type_binding json_cx = check_depth json_of_type_binding_impl json_cx
and json_of_type_binding_impl json_cx (name, t) = Json.(
JAssoc ["name", JString name; "type", _json_of_t json_cx t]
)
and json_of_pred json_cx = check_depth json_of_pred_impl json_cx
and json_of_pred_impl json_cx p = Json.(
JAssoc ([
"kind", JString (string_of_pred_ctor p)
] @
match p with
| AndP (l, r)
| OrP (l, r) -> [
"left", json_of_pred json_cx l;
"right", json_of_pred json_cx r
]
| NotP p -> ["pred", json_of_pred json_cx p]
| LeftP (b, t)
| RightP (b, t) -> [
"binaryTest", json_of_binary_test json_cx b;
"type", _json_of_t json_cx t
]
| TrueP
| FalseP
| ExistsP
| VoidP
| NullP
| MaybeP
| BoolP
| StrP
| NumP
| FunP
| ObjP
| ArrP
-> []
))
and json_of_binary_test json_cx = check_depth json_of_binary_test_impl json_cx
and json_of_binary_test_impl json_cx b = Json.(
JAssoc ([
"kind", JString (string_of_binary_test_ctor b)
] @
match b with
| Instanceof -> []
| SentinelProp s -> ["key", JString s]
))
and json_of_node json_cx = check_depth json_of_node_impl json_cx
and json_of_node_impl json_cx id = Json.(
JAssoc (
let json_cx = { json_cx with stack = ISet.add id json_cx.stack } in
match IMap.find_unsafe id (Context.graph json_cx.cx) with
| Constraint_js.Goto id ->
["kind", JString "Goto"]
@ ["id", JInt id]
| Constraint_js.Root root ->
["kind", JString "Root"]
@ ["root", json_of_root json_cx root]
)
)
and json_of_root json_cx = check_depth json_of_root_impl json_cx
and json_of_root_impl json_cx root = Json.(Constraint_js.(
JAssoc ([
"rank", JInt root.rank;
"constraints", json_of_constraints json_cx root.constraints
])
))
and json_of_constraints json_cx = check_depth json_of_constraints_impl json_cx
and json_of_constraints_impl json_cx constraints = Json.(
JAssoc (
match constraints with
| Constraint_js.Resolved t ->
["kind", JString "Resolved"]
@ ["type", _json_of_t json_cx t]
| Constraint_js.Unresolved bounds ->
["kind", JString "Unresolved"]
@ ["bounds", json_of_bounds json_cx bounds]
)
)
and json_of_bounds json_cx = check_depth json_of_bounds_impl json_cx
and json_of_bounds_impl json_cx bounds = Json.(
match bounds with
| { Constraint_js.lower; upper; lowertvars; uppertvars; } -> JAssoc ([
"lower", json_of_tkeys json_cx lower;
"upper", json_of_tkeys json_cx upper;
"lowertvars", json_of_tvarkeys json_cx lowertvars;
"uppertvars", json_of_tvarkeys json_cx uppertvars;
])
)
and json_of_tkeys json_cx = check_depth json_of_tkeys_impl json_cx
and json_of_tkeys_impl json_cx tmap = Json.(
JList (TypeMap.fold (fun t _ acc -> _json_of_t json_cx t :: acc) tmap [])
)
and json_of_tvarkeys json_cx = check_depth json_of_tvarkeys_impl json_cx
and json_of_tvarkeys_impl json_cx imap = Json.(
JList (IMap.fold (fun i _ acc -> JInt i :: acc) imap [])
)
let json_of_t ?(depth=1000) cx t =
let json_cx = { cx; depth; stack = ISet.empty; } in
_json_of_t json_cx t
let jstr_of_t ?(depth=1000) cx t =
Json.json_to_multiline (json_of_t ~depth cx t)
let json_of_graph ?(depth=1000) cx = Json.(
let entries = IMap.fold (fun id _ entries ->
let json_cx = { cx; depth; stack = ISet.empty; } in
(spf "%d" id, json_of_node json_cx id) :: entries
) (Context.graph cx) [] in
JAssoc (List.rev entries)
)
let jstr_of_graph ?(depth=1000) cx =
Json.json_to_multiline (json_of_graph ~depth cx)
(* scopes *)
let json_of_scope = Scope.(
let open Hh_json in
let json_of_value_impl json_cx {
Entry.kind; value_state; value_loc; specific; general
} =
JAssoc [
"entry_type", JString "Value";
"kind", JString (Entry.string_of_value_kind kind);
"value_state", JString (Entry.string_of_state value_state);
"value_loc", json_of_loc value_loc;
"specific", _json_of_t json_cx specific;
"general", _json_of_t json_cx general;
]
in
let json_of_value json_cx = check_depth json_of_value_impl json_cx in
let json_of_type_impl json_cx { Entry.type_state; type_loc; _type } =
JAssoc [
"entry_type", JString "Type";
"type_state", JString (Entry.string_of_state type_state);
"type_loc", json_of_loc type_loc;
"_type", _json_of_t json_cx _type;
]
in
let json_of_type json_cx = check_depth json_of_type_impl json_cx in
let json_of_entry_impl json_cx = Entry.(function
| Value r -> json_of_value json_cx r
| Type r -> json_of_type json_cx r
) in
let json_of_entry json_cx = check_depth json_of_entry_impl json_cx in
let json_of_entries_impl json_cx entries =
let props = SMap.fold (fun name entry acc ->
(name, json_of_entry json_cx entry) :: acc
) entries []
|> List.rev
in
JAssoc props
in
let json_of_entries json_cx = check_depth json_of_entries_impl json_cx in
let json_of_refi_impl json_cx { refi_loc; refined; original } =
JAssoc [
"refi_loc", json_of_loc refi_loc;
"refined", _json_of_t json_cx refined;
"original", _json_of_t json_cx original;
]
in
let json_of_refi json_cx = check_depth json_of_refi_impl json_cx in
let json_of_refis_impl json_cx refis =
let props = KeyMap.fold (fun key refi acc ->
(Key.string_of_key key, json_of_refi json_cx refi) :: acc
) refis []
|> List.rev
in
JAssoc props
in
let json_of_refis json_cx = check_depth json_of_refis_impl json_cx in
fun ?(depth=1000) cx scope ->
let json_cx = { cx; depth; stack = ISet.empty; } in
JAssoc [
"kind", JString (string_of_kind scope.kind);
"entries", json_of_entries json_cx scope.entries;
"refis", json_of_refis json_cx scope.refis;
]
)
let json_of_env ?(depth=1000) cx env =
Json.JList (List.map (json_of_scope ~depth cx) env)
(*****************************************************************)
(* old debug type printer - ad-hoc middle ground between the public
printer (no internal types, no literal refinements, etc.) and
json *)
let rec dump_t cx t =
dump_t_ ISet.empty cx t
and dump_t_ =
(* we'll want to add more here *)
let override stack cx t = match t with
| OpenT (r, id) -> Some (dump_tvar stack cx r id)
| NumT (r, lit) -> Some (match lit with
| Literal (_, raw) -> spf "NumT(%s)" raw
| Truthy -> spf "NumT(truthy)"
| Falsy -> spf "NumT(0)"
| AnyLiteral -> "NumT")
| StrT (r, c) -> Some (match c with
| Literal s -> spf "StrT(%S)" s
| Truthy -> spf "StrT(truthy)"
| Falsy -> spf "StrT(falsy)"
| AnyLiteral -> "StrT")
| BoolT (r, c) -> Some (match c with
| Some b -> spf "BoolT(%B)" b
| None -> "BoolT")
| UndefT _
| MixedT _
| AnyT _
| NullT _ -> Some (string_of_ctor t)
| SetPropT (_, (_, n), t) ->
Some (spf "SetPropT(%s: %s)" n (dump_t_ stack cx t))
| GetPropT (_, (_, n), t) ->
Some (spf "GetPropT(%s: %s)" n (dump_t_ stack cx t))
| LookupT (_, _, ts, n, t) ->
Some (spf "LookupT(%s: %s)" n (dump_t_ stack cx t))
| PredicateT (p, t) -> Some (spf "PredicateT(%s | %s)"
(string_of_predicate p) (dump_t_ stack cx t))
| _ -> None
in
fun stack cx t -> Type_printer.(
type_printer (override stack) string_of_ctor EnclosureNone cx t
)
(* type variable dumper. abbreviates a few simple cases for readability.
note: if we turn the tvar record into a datatype, these will give a
sense of some of the obvious data constructors *)
and dump_tvar stack cx r id = Constraint_js.(
let sbounds = if ISet.mem id stack then "(...)" else (
let stack = ISet.add id stack in
match IMap.find_unsafe id (Context.graph cx) with
| Goto id -> spf "Goto TYPE_%d" id
| Root { rank; constraints = Resolved t } ->
spf "Root (rank = %d, resolved = %s)"
rank (dump_t_ stack cx t)
| Root { rank; constraints = Unresolved bounds } ->
spf "Root (rank = %d, unresolved = %s)"
rank (dump_bounds stack cx id bounds)
) in
(spf "TYPE_%d: " id) ^ sbounds
)
and dump_bounds stack cx id bounds = Constraint_js.(match bounds with
| { lower; upper; lowertvars; uppertvars; }
when lower = TypeMap.empty && upper = TypeMap.empty
&& IMap.cardinal lowertvars = 1 && IMap.cardinal uppertvars = 1 ->
(* no inflows or outflows *)
"(free)"
| { lower; upper; lowertvars; uppertvars; }
when upper = TypeMap.empty
&& IMap.cardinal lowertvars = 1 && IMap.cardinal uppertvars = 1 ->
(* only concrete inflows *)
spf "L %s" (dump_tkeys stack cx lower)
| { lower; upper; lowertvars; uppertvars; }
when lower = TypeMap.empty && upper = TypeMap.empty
&& IMap.cardinal uppertvars = 1 ->
(* only tvar inflows *)
spf "LV %s" (dump_tvarkeys cx id lowertvars)
| { lower; upper; lowertvars; uppertvars; }
when lower = TypeMap.empty
&& IMap.cardinal lowertvars = 1 && IMap.cardinal uppertvars = 1 ->
(* only concrete outflows *)
spf "U %s" (dump_tkeys stack cx upper)
| { lower; upper; lowertvars; uppertvars; }
when lower = TypeMap.empty && upper = TypeMap.empty
&& IMap.cardinal lowertvars = 1 ->
(* only tvar outflows *)
spf "UV %s" (dump_tvarkeys cx id uppertvars)
| { lower; upper; lowertvars; uppertvars; }
when IMap.cardinal lowertvars = 1 && IMap.cardinal uppertvars = 1 ->
(* only concrete inflows/outflows *)
let l = dump_tkeys stack cx lower in
let u = dump_tkeys stack cx upper in
if l = u then "= " ^ l
else "L " ^ l ^ " U " ^ u
| { lower; upper; lowertvars; uppertvars; } ->
let slower = if lower = TypeMap.empty then "" else
spf " lower = %s;" (dump_tkeys stack cx lower) in
let supper = if upper = TypeMap.empty then "" else
spf " upper = %s;" (dump_tkeys stack cx upper) in
let sltvars = if IMap.cardinal lowertvars <= 1 then "" else
spf " lowertvars = %s;" (dump_tvarkeys cx id lowertvars) in
let sutvars = if IMap.cardinal uppertvars <= 1 then "" else
spf " uppertvars = %s;" (dump_tvarkeys cx id uppertvars) in
"{" ^ slower ^ supper ^ sltvars ^ sutvars ^ " }"
)
(* dump the keys of a type map as a list *)
and dump_tkeys stack cx tmap =
"[" ^ (
String.concat "," (
List.rev (
TypeMap.fold (
fun t _ acc -> dump_t_ stack cx t :: acc
) tmap []
)
)
) ^ "]"
(* dump the keys of a tvar map as a list *)
and dump_tvarkeys cx self imap =
"[" ^ (
String.concat "," (
List.rev (
IMap.fold (
fun id _ acc ->
if id = self then acc else spf "TYPE_%d" id :: acc
) imap []
)
)
) ^ "]"
(* scopes and types *)
let string_of_scope_entry = Scope.(
let string_of_value_binding cx {
Entry.kind; value_state; value_loc; specific; general
} =
spf "{ kind: %s; value_state: %s; value_loc: %S; \
specific: %s; general: %s }"
(Entry.string_of_value_kind kind)
(Entry.string_of_state value_state)
(string_of_loc value_loc)
(dump_t cx specific)
(dump_t cx general)
in
let string_of_type_binding cx { Entry.type_state; type_loc; _type } =
spf "{ type_state: %s; type_loc: %S; _type: %s }"
(Entry.string_of_state type_state)
(string_of_loc type_loc)
(dump_t cx _type)
in
fun cx -> Entry.(function
| Value r -> spf "Value %s" (string_of_value_binding cx r)
| Type r -> spf "Type %s" (string_of_type_binding cx r)
)
)
let string_of_scope_entries cx entries =
let strings = SMap.fold (fun name entry acc ->
(spf "%s: %s" name (string_of_scope_entry cx entry))
:: acc
) entries []
|> String.concat "; \n"
in spf "[ %s ]" strings
let string_of_scope_refi cx { Scope.refi_loc; refined; original } =
spf "{ refi_loc: %S; refined: %s; original: %s }"
(string_of_loc refi_loc)
(dump_t cx refined)
(dump_t cx original)
let string_of_scope_refis cx refis = Scope.(
let strings = KeyMap.fold (fun key refi acc ->
(spf "%s: %s"
(Key.string_of_key key)
(string_of_scope_refi cx refi))
:: acc
) refis []
|> String.concat ";\n"
in spf "[ %s ]" strings
)
let string_of_scope cx scope = Scope.(
Utils.spf "{ kind: %s;\nentries:\n%s\nrefis:\n%s\n}"
(string_of_kind scope.kind)
(string_of_scope_entries cx scope.entries)
(string_of_scope_refis cx scope.refis)
)