/
Visitor_AST.ml
1439 lines (1419 loc) · 43 KB
/
Visitor_AST.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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Yoann Padioleau
*
* Copyright (C) 2019, 2020 r2c
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file license.txt.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
* license.txt for more details.
*)
open OCaml
open AST_generic
module G = AST_generic
module H = AST_generic_helpers
module PI = Parse_info
(* Disable warnings against unused variables *)
[@@@warning "-26"]
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* hooks *)
type visitor_in = {
(* those are the one used by semgrep *)
kexpr : (expr -> unit) * visitor_out -> expr -> unit;
kstmt : (stmt -> unit) * visitor_out -> stmt -> unit;
kstmts : (stmt list -> unit) * visitor_out -> stmt list -> unit;
ktype_ : (type_ -> unit) * visitor_out -> type_ -> unit;
kpattern : (pattern -> unit) * visitor_out -> pattern -> unit;
kfield : (field -> unit) * visitor_out -> field -> unit;
kfields : (field list -> unit) * visitor_out -> field list -> unit;
kattr : (attribute -> unit) * visitor_out -> attribute -> unit;
kpartial : (partial -> unit) * visitor_out -> partial -> unit;
kdef : (definition -> unit) * visitor_out -> definition -> unit;
kdir : (directive -> unit) * visitor_out -> directive -> unit;
kparam : (parameter -> unit) * visitor_out -> parameter -> unit;
kcatch : (catch -> unit) * visitor_out -> catch -> unit;
kident : (ident -> unit) * visitor_out -> ident -> unit;
kname : (name -> unit) * visitor_out -> name -> unit;
kentity : (entity -> unit) * visitor_out -> entity -> unit;
kfunction_definition :
(function_definition -> unit) * visitor_out -> function_definition -> unit;
kclass_definition :
(class_definition -> unit) * visitor_out -> class_definition -> unit;
kinfo : (tok -> unit) * visitor_out -> tok -> unit;
kid_info : (id_info -> unit) * visitor_out -> id_info -> unit;
kconstness : (constness -> unit) * visitor_out -> constness -> unit;
}
and visitor_out = any -> unit
let default_visitor =
{
kexpr = (fun (k, _) x -> k x);
kstmt = (fun (k, _) x -> k x);
ktype_ = (fun (k, _) x -> k x);
kpattern = (fun (k, _) x -> k x);
kfield = (fun (k, _) x -> k x);
kfields = (fun (k, _) x -> k x);
kpartial = (fun (k, _) x -> k x);
kdef = (fun (k, _) x -> k x);
kdir = (fun (k, _) x -> k x);
kattr = (fun (k, _) x -> k x);
kparam = (fun (k, _) x -> k x);
kcatch = (fun (k, _) x -> k x);
kident = (fun (k, _) x -> k x);
kname = (fun (k, _) x -> k x);
kentity = (fun (k, _) x -> k x);
kstmts = (fun (k, _) x -> k x);
kfunction_definition = (fun (k, _) x -> k x);
kclass_definition = (fun (k, _) x -> k x);
kinfo = (fun (k, _) x -> k x);
(* By default, do not visit the refs in id_info *)
kid_info =
(fun (_k, _) x ->
let {
id_resolved = v_id_resolved;
id_type = v_id_type;
id_constness = _IGNORED;
id_hidden = _IGNORED2;
} =
x
in
let arg = v_ref_do_not_visit (v_option (fun _ -> ())) v_id_resolved in
let arg = v_ref_do_not_visit (v_option (fun _ -> ())) v_id_type in
());
kconstness = (fun (k, _) x -> k x);
}
let v_id _ = ()
let (mk_visitor :
?vardef_assign:bool ->
?flddef_assign:bool ->
?attr_expr:bool ->
visitor_in ->
visitor_out) =
fun ?(vardef_assign = false) ?(flddef_assign = false) ?(attr_expr = false) vin ->
(* start of auto generation *)
(* NOTE: we do a few subtle things at a few places now for semgrep
* to trigger a few more artificial visits:
* - we call vardef_to_assign (if `vardef_assign` is `true`)
* - we generate partial defs on the fly and call kpartial
* - we call v_expr on nested XmlXml to give the chance for an
* Xml pattern to also be matched against nested Xml elements
*
* old: We used to apply the VarDef-Assign equivalence by default, but
* this was error prone because visitors typically do side-effectful
* things and VarDefs were visited twice (as a VarDef and as an Assign),
* thus repeating side-effects, leading to surprises.
*)
(* generated by ocamltarzan with: camlp4o -o /tmp/yyy.ml -I pa/ pa_type_conv.cmo pa_visitor.cmo pr_o.cmo /tmp/xxx.ml *)
let rec v_info x =
let k x =
match x with
| { PI.token = _v_pinfox; transfo = _v_transfo } ->
(*
let arg = PI.v_pinfo v_pinfox in
let arg = v_unit v_comments in
let arg = PI.v_transformation v_transfo in
*)
()
in
vin.kinfo (k, all_functions) x
and v_tok v = v_info v
and v_wrap : 'a. ('a -> unit) -> 'a wrap -> unit =
fun _of_a (v1, v2) ->
let v1 = _of_a v1 and v2 = v_info v2 in
()
and v_bracket : 'a. ('a -> unit) -> 'a bracket -> unit =
fun of_a (v1, v2, v3) ->
let v1 = v_info v1 and v2 = of_a v2 and v3 = v_info v3 in
()
and v_ident v =
let k x = v_wrap v_string x in
vin.kident (k, all_functions) v
and v_dotted_ident v = v_list v_ident v
and v_ident_and_targs (v1, v2) =
v_ident v1;
v_option v_type_arguments v2
and v_qualifier = function
| QDots v -> v_list v_ident_and_targs v
| QExpr (e, t) ->
v_expr e;
v_tok t
and v_module_name = function
| FileName v1 ->
let v1 = v_wrap v_string v1 in
()
| DottedName v1 ->
let v1 = v_dotted_ident v1 in
()
and v_resolved_name (v1, v2) =
v_resolved_name_kind v1;
v_int v2
and v_resolved_name_kind = function
| Local -> ()
| Param -> ()
| EnclosedVar -> ()
| Global -> ()
| ImportedEntity v1 ->
let v1 = v_dotted_ident v1 in
()
| ImportedModule v1 ->
let v1 = v_module_name v1 in
()
| Macro -> ()
| EnumConstant -> ()
| TypeName -> ()
and v_name_info
{ name_middle = v4; name_top = v3; name_last = v1; name_info = v2 } =
let v1 = v_ident_and_targs v1 in
let v2 = v_id_info v2 in
let arg = v_option v_qualifier v4 in
let arg = v_option v_tok v3 in
()
and v_id_info x =
let k x =
let {
id_resolved = v_id_resolved;
id_type = v_id_type;
id_constness = v_id_constness;
id_hidden = v_id_hidden;
} =
x
in
let arg = v_ref_do_visit (v_option v_resolved_name) v_id_resolved in
let arg = v_ref_do_visit (v_option v_type_) v_id_type in
let arg = v_ref_do_visit (v_option v_constness) v_id_constness in
let arg = v_hidden v_id_hidden in
()
in
vin.kid_info (k, all_functions) x
and v_xml_attribute v =
match v with
| XmlAttr (v1, t, v2) ->
v_ident v1;
v_tok t;
v_xml_attr v2
| XmlAttrExpr v -> v_bracket v_expr v
| XmlEllipsis v -> v_tok v
and v_xml
{ xml_kind = v_xml_tag; xml_attrs = v_xml_attrs; xml_body = vv_xml_body }
=
let v_xml_tag = v_xml_kind v_xml_tag in
let v_xml_attrs = v_list v_xml_attribute v_xml_attrs in
let vv_xml_body = v_list v_xml_body vv_xml_body in
()
and v_xml_attr v = v_expr v
and v_xml_kind = function
| XmlClassic (v0, v1, v2, v3) ->
v_tok v0;
v_ident v1;
v_tok v2;
v_tok v3
| XmlSingleton (v0, v1, v2) ->
v_tok v0;
v_ident v1;
v_tok v2
| XmlFragment (v1, v2) ->
v_tok v1;
v_tok v2
and v_xml_body = function
| XmlText v1 ->
let v1 = v_wrap v_string v1 in
()
| XmlExpr v1 ->
let v1 = v_bracket (v_option v_expr) v1 in
()
| XmlXml v1 ->
(* subtle: old: let v1 = v_xml v1 in ()
* We want a simple Expr (Xml ...) pattern to also be matched
* against nested XmlXml elements *)
v_expr (Xml v1 |> G.e)
and v_name x =
let k x =
match x with
| Id (v1, v2) ->
let v1 = v_ident v1 and v2 = v_id_info v2 in
()
| IdQualified v1 ->
let v1 = v_name_info v1 in
()
in
vin.kname (k, all_functions) x
and v_expr x =
let k x =
match x.e with
| ParenExpr v1 -> v_bracket v_expr v1
| DotAccessEllipsis (v1, v2) ->
v_expr v1;
v_tok v2
| DisjExpr (v1, v2) ->
let v1 = v_expr v1 in
let v2 = v_expr v2 in
()
| L v1 ->
let v1 = v_literal v1 in
()
| Ellipsis v1 ->
let v1 = v_tok v1 in
()
| DeepEllipsis v1 ->
let v1 = v_bracket v_expr v1 in
()
| Container (v1, v2) ->
(match v1 with
| Dict ->
v2 |> unbracket
|> List.iter (fun e ->
match e.e with
| Container
(Tuple, (tok, [ { e = L (String id); _ }; e ], _)) ->
let t = PI.fake_info tok ":" in
v_partial ~recurse:false
(PartialSingleField (id, t, e))
| _ -> ())
(* for Go where we use List for composite literals *)
| List ->
v2 |> unbracket
|> List.iter (fun e ->
match e.e with
| Container
(Tuple, (tok, [ { e = N (Id (id, _)); _ }; e ], _)) ->
let t = PI.fake_info tok ":" in
v_partial ~recurse:false
(PartialSingleField (id, t, e))
| _ -> ())
| _ -> ());
let v1 = v_container_operator v1
and v2 = v_bracket (v_list v_expr) v2 in
()
| Comprehension (v1, v2) ->
let v1 = v_container_operator v1
and v2 = v_bracket v_comprehension v2 in
()
| Record v1 ->
let v1 = v_bracket v_fields v1 in
()
| Constructor (v1, v2) ->
let v1 = v_name v1 and v2 = v_bracket (v_list v_expr) v2 in
()
| Lambda v1 ->
let v1 = v_function_definition v1 in
()
| AnonClass v1 ->
let v1 = v_class_definition v1 in
()
| Xml v1 ->
let v1 = v_xml v1 in
()
| N v1 -> v_name v1
| IdSpecial v1 ->
let v1 = v_wrap v_special v1 in
()
| Call (v1, v2) ->
let v1 = v_expr v1 and v2 = v_arguments v2 in
()
| Assign (v1, v2, v3) ->
let v1 = v_expr v1 and v2 = v_tok v2 and v3 = v_expr v3 in
()
| AssignOp (v1, v2, v3) ->
let v1 = v_expr v1
and v2 = v_wrap v_arithmetic_operator v2
and v3 = v_expr v3 in
()
| LetPattern (v1, v2) ->
let v1 = v_pattern v1 and v2 = v_expr v2 in
()
| DotAccess (v1, t, v2) ->
let v1 = v_expr v1 and t = v_tok t and v2 = v_field_name v2 in
()
| ArrayAccess (v1, v2) ->
let v1 = v_expr v1 and v2 = v_bracket v_expr v2 in
()
| SliceAccess (v1, v2) ->
let v1 = v_expr v1
and v2 =
v_bracket
(fun (v1, v2, v3) ->
v_option v_expr v1;
v_option v_expr v2;
v_option v_expr v3)
v2
in
()
| Conditional (v1, v2, v3) ->
let v1 = v_expr v1 and v2 = v_expr v2 and v3 = v_expr v3 in
()
| TypedMetavar (v1, v2, v3) ->
let v1 = v_ident v1 and v2 = v_tok v2 and v3 = v_type_ v3 in
()
| Yield (t, v1, v2) ->
let t = v_tok t in
let v1 = v_option v_expr v1 and v2 = v_bool v2 in
()
| Await (t, v1) ->
let t = v_tok t in
let v1 = v_expr v1 in
()
| Cast (v1, t, v2) ->
let v1 = v_type_ v1 and t = v_tok t and v2 = v_expr v2 in
()
| Seq v1 ->
let v1 = v_list v_expr v1 in
()
| Ref (t, v1) ->
let t = v_tok t in
let v1 = v_expr v1 in
()
| DeRef (t, v1) ->
let t = v_tok t in
let v1 = v_expr v1 in
()
| StmtExpr v1 ->
let v1 = v_stmt v1 in
()
| OtherExpr (v1, v2) ->
let v1 = v_todo_kind v1 and v2 = v_list v_any v2 in
()
in
vin.kexpr (k, all_functions) x
and v_field_name = function
| FN v1 -> v_name v1
| FDynamic e -> v_expr e
and v_entity_name = function
| EN v1 -> v_name v1
| EDynamic e -> v_expr e
| EPattern x -> v_pattern x
| OtherEntity (v1, v2) ->
let v1 = v_todo_kind v1 and v2 = v_list v_any v2 in
()
and v_literal = function
| Unit v1 ->
let v1 = v_tok v1 in
()
| Bool v1 ->
let v1 = v_wrap v_bool v1 in
()
| Int v1 ->
let v1 = v_wrap v_id v1 in
()
| Float v1 ->
let v1 = v_wrap v_id v1 in
()
| Imag v1 ->
let v1 = v_wrap v_string v1 in
()
| Ratio v1 ->
let v1 = v_wrap v_string v1 in
()
| Atom (v0, v1) ->
let v0 = v_tok v0 in
let v1 = v_wrap v_string v1 in
()
| Char v1 ->
let v1 = v_wrap v_string v1 in
()
| String v1 ->
let v1 = v_wrap v_string v1 in
()
| Regexp (v1, v2) ->
let v1 = v_bracket (v_wrap v_string) v1 in
let v2 = v_option (v_wrap v_string) v2 in
()
| Null v1 ->
let v1 = v_tok v1 in
()
| Undefined v1 ->
let v1 = v_tok v1 in
()
and v_const_type = function
| Cbool -> ()
| Cint -> ()
| Cstr -> ()
| Cany -> ()
and v_constness x =
let k = function
| Lit v1 ->
let v1 = v_literal v1 in
()
| Cst v1 ->
let v1 = v_const_type v1 in
()
| NotCst -> ()
in
vin.kconstness (k, all_functions) x
and v_hidden _is_hidden = ()
and v_container_operator _x = ()
and v_comprehension (v1, v2) =
let v1 = v_expr v1 in
let v2 = v_list v_for_or_if_comp v2 in
()
and v_for_or_if_comp = function
| CompFor (v1, v2, v3, v4) ->
let v1 = v_tok v1 in
let v2 = v_pattern v2 in
let v3 = v_tok v3 in
let v4 = v_expr v4 in
()
| CompIf (v1, v2) ->
let v1 = v_tok v1 in
let v2 = v_expr v2 in
()
and v_special = function
| ForOf -> ()
| Defined -> ()
| This -> ()
| Super -> ()
| Self -> ()
| Parent -> ()
| Eval -> ()
| Typeof -> ()
| Instanceof -> ()
| Sizeof -> ()
| New -> ()
| Spread -> ()
| HashSplat -> ()
| NextArrayIndex -> ()
| EncodedString v1 ->
let v1 = v_string v1 in
()
| Op v1 ->
let v1 = v_arithmetic_operator v1 in
()
| IncrDecr (v1, v2) ->
let v1 = v_incr_decr v1 and v2 = v_prepost v2 in
()
| ConcatString v1 ->
let v1 = v_interpolated_kind v1 in
()
| InterpolatedElement -> ()
and v_interpolated_kind _ = ()
and v_incr_decr _ = ()
and v_prepost _ = ()
and v_arithmetic_operator _x = ()
and v_arguments v = v_bracket (v_list v_argument) v
and v_argument = function
| Arg v1 ->
let v1 = v_expr v1 in
()
| ArgType v1 ->
let v1 = v_type_ v1 in
()
| ArgKwd (v1, v2) ->
let v1 = v_ident v1 and v2 = v_expr v2 in
()
| OtherArg (v1, v2) ->
let v1 = v_todo_kind v1 and v2 = v_list v_any v2 in
()
and v_type_ x =
let k { t; t_attrs } =
v_list v_attribute t_attrs;
match t with
| TyEllipsis v1 -> v_tok v1
| TyRecordAnon (v0, v1) ->
v_class_kind v0;
let v1 = v_bracket v_fields v1 in
()
| TyOr (v1, v2, v3) ->
v_type_ v1;
v_tok v2;
v_type_ v3
| TyAnd (v1, v2, v3) ->
v_type_ v1;
v_tok v2;
v_type_ v3
| TyFun (v1, v2) ->
let v1 = v_list v_parameter v1 and v2 = v_type_ v2 in
()
| TyApply (v1, v2) ->
let v1 = v_type_ v1 and v2 = v_type_arguments v2 in
()
| TyN v1 -> v_name v1
| TyVar v1 ->
let v1 = v_ident v1 in
()
| TyAny v1 ->
let v1 = v_tok v1 in
()
| TyArray (v1, v2) ->
let v1 = v_bracket (v_option v_expr) v1 and v2 = v_type_ v2 in
()
| TyPointer (t, v1) ->
let t = v_tok t in
let v1 = v_type_ v1 in
()
| TyRef (t, v1) ->
let t = v_tok t in
let v1 = v_type_ v1 in
()
| TyTuple v1 ->
let v1 = v_bracket (v_list v_type_) v1 in
()
| TyQuestion (v1, t) ->
let t = v_tok t in
let v1 = v_type_ v1 in
()
| TyRest (t, v1) ->
let t = v_tok t in
let v1 = v_type_ v1 in
()
| TyExpr v1 ->
let v1 = v_expr v1 in
()
| OtherType (v1, v2) ->
let v1 = v_todo_kind v1 and v2 = v_list v_any v2 in
()
in
vin.ktype_ (k, all_functions) x
and v_type_arguments v = v_bracket (v_list v_type_argument) v
and v_type_argument = function
| TA v1 ->
let v1 = v_type_ v1 in
()
| TAWildcard (v1, v2) -> (
v_tok v1;
match v2 with
| None -> ()
| Some (v1, v2) ->
v_wrap v_bool v1;
v_type_ v2)
| TAExpr v1 ->
let v1 = v_expr v1 in
()
| OtherTypeArg (v1, v2) ->
let v1 = v_todo_kind v1 and v2 = v_list v_any v2 in
()
(* bugfix: do not call v_ident here, otherwise code like
* Analyze_pattern might consider the string for -filter_irrelevant_rules
*)
and v_todo_kind (_str, tok) = v_tok tok
and v_other_type_operator _ = ()
and v_type_parameter = function
| TParamEllipsis v1 -> v_tok v1
| TP v1 -> v_type_parameter_classic v1
| OtherTypeParam (t, xs) ->
let t = v_todo_kind t in
let xs = v_list v_any xs in
()
and v_type_parameter_classic
{
tp_id = v1;
tp_attrs = v2;
tp_bounds = v3;
tp_default = v4;
tp_variance = v5;
} =
v_ident v1;
v_list v_attribute v2;
v_list v_type_ v3;
v_option v_type_ v4;
v_option (v_wrap v_variance) v5;
()
and v_variance _ = ()
and v_attribute x =
let k x =
match x with
| KeywordAttr v1 ->
let v1 = v_wrap v_keyword_attribute v1 in
()
| NamedAttr (t, v1, v3) ->
let _ = v_named_attr_as_expr v1 v3 in
let t = v_tok t in
let v1 = v_name v1 and v3 = v_bracket (v_list v_argument) v3 in
()
| OtherAttribute (v1, v2) ->
let v1 = v_other_attribute_operator v1 and v2 = v_list v_any v2 in
()
in
vin.kattr (k, all_functions) x
and v_keyword_attribute _ = ()
and v_named_attr_as_expr name args =
(* A named attribute is essentially a function call, but this is not
* explicit in Generic so we cannot match expression patterns against
* attributes. This equivalence enables exactly that, and we can e.g.
* match `@f(a)` with `f($X)`. *)
if attr_expr then v_expr (e (Call (e (N name), args))) else ()
and v_other_attribute_operator _ = ()
and v_stmts xs =
let k xs =
match xs with
| [] -> ()
| x :: xs ->
v_stmt x;
(* we will call the visitor also on subsequences. This is useful
* for semgrep *)
v_stmts xs
in
vin.kstmts (k, all_functions) xs
and v_cases_and_body x =
match x with
| CasesAndBody (v1, v2) ->
let v1 = v_list v_case v1 and v2 = v_stmt v2 in
()
| CaseEllipsis v1 -> v_tok v1
and v_stmt x =
let k x =
(* todo? visit the s_id too? *)
match x.s with
| Match (v0, v1, v2) ->
v_partial ~recurse:false (PartialMatch (v0, v1));
let v0 = v_tok v0 in
let v1 = v_expr v1
and v2 =
v_list
(fun (v1, v2) ->
let v1 = v_pattern v1 and v2 = v_expr v2 in
())
v2
in
()
| DisjStmt (v1, v2) ->
let v1 = v_stmt v1 in
let v2 = v_stmt v2 in
()
| ExprStmt (v1, t) ->
let v1 = v_expr v1 in
let t = v_tok t in
()
| DefStmt v1 ->
let v1 = v_def v1 in
()
| DirectiveStmt v1 ->
let v1 = v_directive v1 in
()
| Block v1 ->
let v1 = v_bracket v_stmts v1 in
()
| If (t, Cond v1, v2, v3) ->
v_partial ~recurse:false (PartialIf (t, v1));
let t = v_tok t in
let v1 = v_expr v1 and v2 = v_stmt v2 and v3 = v_option v_stmt v3 in
()
| If (t, v1, v2, v3) ->
let t = v_tok t in
let v1 = v_condition v1
and v2 = v_stmt v2
and v3 = v_option v_stmt v3 in
()
| While (t, v1, v2) ->
let t = v_tok t in
let v1 = v_condition v1 and v2 = v_stmt v2 in
()
| DoWhile (t, v1, v2) ->
let t = v_tok t in
let v1 = v_stmt v1 and v2 = v_expr v2 in
()
| For (t, v1, v2) ->
let t = v_tok t in
let v1 = v_for_header v1 and v2 = v_stmt v2 in
()
| Switch (v0, v1, v2) ->
let v0 = v_tok v0 in
let v1 = v_option v_condition v1
and v2 = v_list v_cases_and_body v2 in
()
| Return (t, v1, sc) ->
let t = v_tok t in
let v1 = v_option v_expr v1 in
v_tok sc
| Continue (t, v1, sc) ->
let t = v_tok t in
let v1 = v_label_ident v1 in
v_tok sc
| Break (t, v1, sc) ->
let t = v_tok t in
let v1 = v_label_ident v1 in
v_tok sc
| Label (v1, v2) ->
let v1 = v_label v1 and v2 = v_stmt v2 in
()
| Goto (t, v1, sc) ->
let t = v_tok t in
let v1 = v_label v1 in
v_tok sc
| Throw (t, v1, sc) ->
let t = v_tok t in
let v1 = v_expr v1 in
v_tok sc
| Try (t, v1, v2, v3) ->
v_partial ~recurse:false (PartialTry (t, v1));
let t = v_tok t in
let v1 = v_stmt v1
and v2 = v_list v_catch v2
and v3 = v_option v_finally v3 in
()
| WithUsingResource (t, v1, v2) ->
let t = v_tok t in
let v1 = v_stmt v1 and v2 = v_stmt v2 in
()
| Assert (t, args, sc) ->
let t = v_tok t in
let _ = v_arguments args in
v_tok sc
| OtherStmtWithStmt (v1, v2, v3) ->
let v1 = v_other_stmt_with_stmt_operator v1
and v2 = v_list v_any v2
and v3 = v_stmt v3 in
()
| OtherStmt (v1, v2) ->
let v1 = v_other_stmt_operator v1 and v2 = v_list v_any v2 in
()
in
vin.kstmt (k, all_functions) x
and v_condition = function
| Cond e -> v_expr e
| OtherCond (v1, v2) ->
let v1 = v_todo_kind v1 and v2 = v_list v_any v2 in
()
and v_other_stmt_with_stmt_operator _ = ()
and v_label_ident = function
| LNone -> ()
| LId v1 ->
let v1 = v_label v1 in
()
| LInt v1 ->
let v1 = v_wrap v_int v1 in
()
| LDynamic v1 ->
let v1 = v_expr v1 in
()
and v_case = function
| OtherCase (v1, v2) ->
v_todo_kind v1;
v_list v_any v2
| Case (t, v1) ->
let t = v_tok t in
let v1 = v_pattern v1 in
()
| CaseEqualExpr (t, v1) ->
let t = v_tok t in
let v1 = v_expr v1 in
()
| Default t ->
let t = v_tok t in
()
and v_catch x =
let k (t, v1, v2) =
v_partial ~recurse:false (PartialCatch (t, v1, v2));
let t = v_tok t in
let v1 = v_catch_exn v1 and v2 = v_stmt v2 in
()
in
vin.kcatch (k, all_functions) x
and v_catch_exn = function
| OtherCatch (v1, v2) ->
v_todo_kind v1;
v_list v_any v2
| CatchPattern p -> v_pattern p
| CatchParam p -> v_parameter_classic p
and v_finally (t, v) =
v_partial ~recurse:false (PartialFinally (t, v));
let t = v_tok t in
v_stmt v
and v_label v = v_ident v
and v_for_header = function
| ForClassic (v1, v2, v3) ->
let v1 = v_list v_for_var_or_expr v1
and v2 = v_option v_expr v2
and v3 = v_option v_expr v3 in
()
| ForEach (v1, t, v2) ->
let t = v_tok t in
let v1 = v_pattern v1 and v2 = v_expr v2 in
()
| ForEllipsis t -> v_tok t
| ForIn (v1, v2) ->
let v1 = v_list v_for_var_or_expr v1 and v2 = v_list v_expr v2 in
()
and v_for_var_or_expr = function
| ForInitVar (v1, v2) ->
let v1 = v_entity v1 and v2 = v_variable_definition v2 in
()
| ForInitExpr v1 ->
let v1 = v_expr v1 in
()
and v_other_stmt_operator _x = ()
and v_pattern x =
let k x =
match x with
| PatEllipsis v1 -> v_tok v1
| PatRecord v1 ->
let v1 =
v_bracket
(v_list (fun (v1, v2) ->
let v1 = v_dotted_ident v1 and v2 = v_pattern v2 in
()))
v1
in
()
| PatId (v1, v2) ->
let v1 = v_ident v1 and v2 = v_id_info v2 in
()
| PatLiteral v1 ->
let v1 = v_literal v1 in
()
| PatType v1 ->
let v1 = v_type_ v1 in
()
| PatConstructor (v1, v2) ->
let v1 = v_name v1 and v2 = v_list v_pattern v2 in
()
| PatTuple (_, v1, _) ->
let v1 = v_list v_pattern v1 in
()
| PatList v1 ->
let v1 = v_bracket (v_list v_pattern) v1 in
()
| PatKeyVal (v1, v2) ->
let v1 = v_pattern v1 and v2 = v_pattern v2 in
()
| PatUnderscore v1 ->
let v1 = v_tok v1 in
()
| PatDisj (v1, v2) ->
let v1 = v_pattern v1 and v2 = v_pattern v2 in
()
| DisjPat (v1, v2) ->
let v1 = v_pattern v1 and v2 = v_pattern v2 in
()
| PatTyped (v1, v2) ->
let v1 = v_pattern v1 and v2 = v_type_ v2 in
()
| PatAs (v1, v2) ->
let v1 = v_pattern v1
and v2 =
match v2 with
| v1, v2 ->
let v1 = v_ident v1 and v2 = v_id_info v2 in
()
in
()
| PatWhen (v1, v2) ->
let v1 = v_pattern v1 and v2 = v_expr v2 in
()
| OtherPat (v1, v2) ->
let v1 = v_other_pattern_operator v1 and v2 = v_list v_any v2 in
()
in
vin.kpattern (k, all_functions) x
and v_other_pattern_operator _ = ()
and v_def x =
let k x =
let v1, v2 = x in
let _ = v_vardef_as_assign_expr v1 v2 in
let _ = v_def_as_partial v1 v2 in
let v1 = v_entity v1 and v2 = v_def_kind v2 in
()
in
vin.kdef (k, all_functions) x
(* WEIRD: not sure why, but using this code below instead of
* the v_def_as_partial above cause some regressions.
*
* (* calling kpartial with a modified def *)
* (match x with
* | ent, FuncDef def ->
* let partial_def = { def with fbody = empty_fbody } in
* v_partial (PartialDef (ent, FuncDef partial_def))
* | _ -> ()
* )
*)
and v_def_as_partial ent defkind =
(* calling kpartial with a modified def *)
match defkind with
| FuncDef def ->
let partial_def = { def with fbody = FBNothing } in
v_partial ~recurse:false (PartialDef (ent, FuncDef partial_def))
| ClassDef def ->
let partial_def = { def with cbody = empty_body } in
v_partial ~recurse:false (PartialDef (ent, ClassDef partial_def))
| _ -> ()
(* The recurse argument is subtle. It is needed because we
* want different behaviors depending on the context:
* - in some context we want to recurse, for example when
* we call ii_of_any (Partial ...), we want to get all the tokens in it
* - in other context we do not want to recurse, because that would mean
* we would visit two times the same function (one with a body, and one
* without a body), which can lead some code, e.g., Naming_AST, to generate
* intermediate sids which in turn lead to regressions in end-2-end tests
* (because the value of sid differ).
* This is why when we are called from v_any, we recurse (case 1), but
* when we are called from a v_def, we don't.
*)
and v_partial ~recurse x =
let k x =
match x with
| PartialDef (v1, v2) ->
(* Do not call v_def here, otherwise you'll get infinite loop *)
if recurse then (
v_entity v1;
v_def_kind v2);
()
| PartialIf (v1, v2)
| PartialMatch (v1, v2) ->
if recurse then (
v_tok v1;
v_expr v2)
| PartialTry (v1, v2) ->
if recurse then (
v_tok v1;
v_stmt v2)
| PartialCatch v1 -> if recurse then v_catch v1
| PartialFinally (v1, v2) ->
if recurse then (
v_tok v1;
v_stmt v2)
| PartialSingleField (v1, v2, v3) ->
if recurse then (
v_wrap v_string v1;
v_tok v2;
v_expr v3)
| PartialLambdaOrFuncDef v1 -> if recurse then v_function_definition v1
in
vin.kpartial (k, all_functions) x
and v_entity x =
let k x =
let { name = x_name; attrs = v_attrs; tparams = v_tparams } = x in
let arg = v_entity_name x_name in
let arg = v_list v_attribute v_attrs in
let arg = v_list v_type_parameter v_tparams in
()
in
vin.kentity (k, all_functions) x
and v_enum_entry_definition { ee_args; ee_body } =
v_option v_arguments ee_args;
v_option (v_bracket (v_list v_field)) ee_body
and v_def_kind = function
| EnumEntryDef v1 ->
let v1 = v_enum_entry_definition v1 in
()
| FuncDef v1 ->
let v1 = v_function_definition v1 in