/
VisualBasicExtensions.vb
1902 lines (1735 loc) · 90.8 KB
/
VisualBasicExtensions.vb
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
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
' See the LICENSE file in the project root for more information.
Imports System.Collections.Immutable
Imports System.Collections.ObjectModel
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Imports System.Threading
Imports Microsoft.CodeAnalysis.Operations
Imports Microsoft.CodeAnalysis.Syntax
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis
''' <summary>
''' Addition Visual Basic syntax extension methods.
''' </summary>
Public Module VisualBasicExtensions
''' <summary>
''' Determines if <see cref="SyntaxTrivia"/> is of a specified kind.
''' </summary>
''' <param name="trivia">The source trivia.</param>
''' <param name="kind">The syntax kind to test for.</param>
''' <returns><see langword="true"/> if the trivia is of the specified kind; otherwise, <see langword="false"/>.</returns>
<Extension>
Public Function IsKind(trivia As SyntaxTrivia, kind As SyntaxKind) As Boolean
Return trivia.RawKind = kind
End Function
''' <summary>
''' Determines if <see cref="SyntaxToken"/> is of a specified kind.
''' </summary>
''' <param name="token">The source token.</param>
''' <param name="kind">The syntax kind to test for.</param>
''' <returns><see langword="true"/> if the token is of the specified kind; otherwise, <see langword="false"/>.</returns>
<Extension>
Public Function IsKind(token As SyntaxToken, kind As SyntaxKind) As Boolean
Return token.RawKind = kind
End Function
''' <summary>
''' Determines if <see cref="SyntaxNode"/> is of a specified kind.
''' </summary>
''' <param name="node">The Source node.</param>
''' <param name="kind">The syntax kind to test for.</param>
''' <returns><see langword="true"/> if the node is of the specified kind; otherwise, <see langword="false"/>.</returns>
<Extension>
Public Function IsKind(node As SyntaxNode, kind As SyntaxKind) As Boolean
Return node IsNot Nothing AndAlso node.RawKind = kind
End Function
''' <summary>
''' Determines if <see cref="SyntaxNodeOrToken"/> is of a specified kind.
''' </summary>
''' <param name="nodeOrToken">The source node or token.</param>
''' <param name="kind">The syntax kind to test for.</param>
''' <returns><see langword="true"/> if the node or token is of the specified kind; otherwise, <see langword="false"/>.</returns>
<Extension>
Public Function IsKind(nodeOrToken As SyntaxNodeOrToken, kind As SyntaxKind) As Boolean
Return nodeOrToken.RawKind = kind
End Function
''' <summary>
''' Returns the index of the first node of a specified kind in the node list.
''' </summary>
''' <param name="list">Node list.</param>
''' <param name="kind">The <see cref="SyntaxKind"/> to find.</param>
''' <returns>Returns non-negative index if the list contains a node which matches <paramref name="kind"/>, -1 otherwise.</returns>
<Extension>
Public Function IndexOf(Of TNode As SyntaxNode)(list As SyntaxList(Of TNode), kind As SyntaxKind) As Integer
Return list.IndexOf(CType(kind, Integer))
End Function
''' <summary>
''' Tests whether a list contains node of a particular kind.
''' </summary>
''' <param name="kind">The <see cref="SyntaxKind"/> to test for.</param>
''' <returns>Returns true if the list contains a token which matches <paramref name="kind"/></returns>
<Extension>
Public Function Any(Of TNode As SyntaxNode)(list As SyntaxList(Of TNode), kind As SyntaxKind) As Boolean
Return list.IndexOf(kind) >= 0
End Function
''' <summary>
''' Returns the index of the first node of a specified kind in the node list.
''' </summary>
''' <param name="list">Node list.</param>
''' <param name="kind">The <see cref="SyntaxKind"/> to find.</param>
''' <returns>Returns non-negative index if the list contains a node which matches <paramref name="kind"/>, -1 otherwise.</returns>
<Extension>
Public Function IndexOf(Of TNode As SyntaxNode)(list As SeparatedSyntaxList(Of TNode), kind As SyntaxKind) As Integer
Return list.IndexOf(CType(kind, Integer))
End Function
''' <summary>
''' Tests whether a list contains node of a particular kind.
''' </summary>
''' <param name="kind">The <see cref="SyntaxKind"/> to test for.</param>
''' <returns>Returns true if the list contains a token which matches <paramref name="kind"/></returns>
<Extension>
Public Function Any(Of TNode As SyntaxNode)(list As SeparatedSyntaxList(Of TNode), kind As SyntaxKind) As Boolean
Return list.IndexOf(kind) >= 0
End Function
''' <summary>
''' Returns the index of the first trivia of a specified kind in the trivia list.
''' </summary>
''' <param name="list">Trivia list.</param>
''' <param name="kind">The <see cref="SyntaxKind"/> to find.</param>
''' <returns>Returns non-negative index if the list contains a trivia which matches <paramref name="kind"/>, -1 otherwise.</returns>
<Extension>
Public Function IndexOf(list As SyntaxTriviaList, kind As SyntaxKind) As Integer
Return list.IndexOf(CType(kind, Integer))
End Function
''' <summary>
''' Tests whether a list contains trivia of a particular kind.
''' </summary>
''' <param name="kind">The <see cref="SyntaxKind"/> to test for.</param>
''' <returns>Returns true if the list contains a trivia which matches <paramref name="kind"/></returns>
<Extension>
Public Function Any(list As SyntaxTriviaList, kind As SyntaxKind) As Boolean
Return list.IndexOf(kind) >= 0
End Function
''' <summary>
''' Returns the index of the first token of a specified kind in the token list.
''' </summary>
''' <param name="list">Token list.</param>
''' <param name="kind">The <see cref="SyntaxKind"/> to find.</param>
''' <returns>Returns non-negative index if the list contains a token which matches <paramref name="kind"/>, -1 otherwise.</returns>
<Extension>
Public Function IndexOf(list As SyntaxTokenList, kind As SyntaxKind) As Integer
Return list.IndexOf(CType(kind, Integer))
End Function
''' <summary>
''' Tests whether a list contains token of a particular kind.
''' </summary>
''' <param name="kind">The <see cref="SyntaxKind"/> to test for.</param>
''' <returns>Returns true if the list contains a token which matches <paramref name="kind"/></returns>
<Extension>
Public Function Any(list As SyntaxTokenList, kind As SyntaxKind) As Boolean
Return list.IndexOf(kind) >= 0
End Function
<Extension>
Friend Function FirstOrDefault(list As SyntaxTokenList, kind As SyntaxKind) As SyntaxToken
Dim index = list.IndexOf(kind)
Return If(index >= 0, list(index), Nothing)
End Function
<Extension>
Friend Function First(list As SyntaxTokenList, kind As SyntaxKind) As SyntaxToken
Dim index = list.IndexOf(kind)
If index < 0 Then
Throw New InvalidOperationException()
End If
Return list(index)
End Function
End Module
End Namespace
Namespace Microsoft.CodeAnalysis.VisualBasic
Public Module VisualBasicExtensions
''' <summary>
''' Determines if the given raw kind value belongs to the Visual Basic <see cref="SyntaxKind"/> enumeration.
''' </summary>
''' <param name="rawKind">The raw value to test.</param>
''' <returns><see langword="true"/> when the raw value belongs to the Visual Basic syntax kind; otherwise, <see langword="false"/>.</returns>
Friend Function IsVisualBasicKind(rawKind As Integer) As Boolean
Const LastPossibleVisualBasicKind As Integer = 8192
Return rawKind <= LastPossibleVisualBasicKind
End Function
''' <summary>
''' Returns <see cref="SyntaxKind"/> for <see cref="SyntaxTrivia"/> from <see cref="SyntaxTrivia.RawKind"/> property.
''' </summary>
<Extension>
Public Function Kind(trivia As SyntaxTrivia) As SyntaxKind
Dim rawKind = trivia.RawKind
Return If(IsVisualBasicKind(rawKind), CType(rawKind, SyntaxKind), SyntaxKind.None)
End Function
''' <summary>
''' Returns <see cref="SyntaxKind"/> for <see cref="SyntaxToken"/> from <see cref="SyntaxToken.RawKind"/> property.
''' </summary>
<Extension>
Public Function Kind(token As SyntaxToken) As SyntaxKind
Dim rawKind = token.RawKind
Return If(IsVisualBasicKind(rawKind), CType(rawKind, SyntaxKind), SyntaxKind.None)
End Function
''' <summary>
''' Returns <see cref="SyntaxKind"/> for <see cref="SyntaxNode"/> from <see cref="SyntaxNode.RawKind"/> property.
''' </summary>
<Extension>
Public Function Kind(node As SyntaxNode) As SyntaxKind
Dim rawKind = node.RawKind
Return If(IsVisualBasicKind(rawKind), CType(rawKind, SyntaxKind), SyntaxKind.None)
End Function
''' <summary>
''' Returns <see cref="SyntaxKind"/> for <see cref="SyntaxNodeOrToken"/> from <see cref="SyntaxNodeOrToken.RawKind"/> property.
''' </summary>
<Extension>
Public Function Kind(nodeOrToken As SyntaxNodeOrToken) As SyntaxKind
Dim rawKind = nodeOrToken.RawKind
Return If(IsVisualBasicKind(rawKind), CType(rawKind, SyntaxKind), SyntaxKind.None)
End Function
<Extension>
Friend Function GetLocation(syntaxReference As SyntaxReference) As Location
Dim tree = TryCast(syntaxReference.SyntaxTree, VisualBasicSyntaxTree)
If syntaxReference.SyntaxTree IsNot Nothing Then
If tree.IsEmbeddedSyntaxTree Then
Return New EmbeddedTreeLocation(tree.GetEmbeddedKind, syntaxReference.Span)
ElseIf tree.IsMyTemplate Then
Return New MyTemplateLocation(tree, syntaxReference.Span)
End If
End If
Return New SourceLocation(syntaxReference)
End Function
<Extension>
Friend Function IsMyTemplate(syntaxTree As SyntaxTree) As Boolean
Dim vbTree = TryCast(syntaxTree, VisualBasicSyntaxTree)
Return vbTree IsNot Nothing AndAlso vbTree.IsMyTemplate
End Function
<Extension>
Friend Function HasReferenceDirectives(syntaxTree As SyntaxTree) As Boolean
Dim vbTree = TryCast(syntaxTree, VisualBasicSyntaxTree)
Return vbTree IsNot Nothing AndAlso vbTree.HasReferenceDirectives
End Function
<Extension>
Friend Function IsAnyPreprocessorSymbolDefined(syntaxTree As SyntaxTree, conditionalSymbolNames As IEnumerable(Of String), atNode As SyntaxNodeOrToken) As Boolean
Dim vbTree = TryCast(syntaxTree, VisualBasicSyntaxTree)
Return vbTree IsNot Nothing AndAlso vbTree.IsAnyPreprocessorSymbolDefined(conditionalSymbolNames, atNode)
End Function
<Extension>
Friend Function GetVisualBasicSyntax(syntaxReference As SyntaxReference, Optional cancellationToken As CancellationToken = Nothing) As VisualBasicSyntaxNode
Return DirectCast(syntaxReference.GetSyntax(cancellationToken), VisualBasicSyntaxNode)
End Function
<Extension>
Friend Function GetVisualBasicRoot(syntaxTree As SyntaxTree, Optional cancellationToken As CancellationToken = Nothing) As VisualBasicSyntaxNode
Return DirectCast(syntaxTree.GetRoot(cancellationToken), VisualBasicSyntaxNode)
End Function
<Extension>
Friend Function GetPreprocessingSymbolInfo(syntaxTree As SyntaxTree, identifierNode As IdentifierNameSyntax) As VisualBasicPreprocessingSymbolInfo
Dim vbTree = DirectCast(syntaxTree, VisualBasicSyntaxTree)
Return vbTree.GetPreprocessingSymbolInfo(identifierNode)
End Function
<Extension>
Friend Function Errors(trivia As SyntaxTrivia) As InternalSyntax.SyntaxDiagnosticInfoList
Return New InternalSyntax.SyntaxDiagnosticInfoList(DirectCast(trivia.UnderlyingNode, InternalSyntax.VisualBasicSyntaxNode))
End Function
<Extension>
Friend Function Errors(token As SyntaxToken) As InternalSyntax.SyntaxDiagnosticInfoList
Return New InternalSyntax.SyntaxDiagnosticInfoList(DirectCast(token.Node, InternalSyntax.VisualBasicSyntaxNode))
End Function
<Extension>
Friend Function GetSyntaxErrors(token As SyntaxToken, tree As SyntaxTree) As ReadOnlyCollection(Of Diagnostic)
Return VisualBasicSyntaxNode.DoGetSyntaxErrors(tree, token)
End Function
''' <summary>
''' Checks to see if SyntaxToken is a bracketed identifier.
''' </summary>
''' <param name="token">The source SyntaxToken.</param>
''' <returns>A boolean value, True if token represents a bracketed Identifier.</returns>
<Extension>
Public Function IsBracketed(token As SyntaxToken) As Boolean
If token.IsKind(SyntaxKind.IdentifierToken) Then
Return DirectCast(token.Node, InternalSyntax.IdentifierTokenSyntax).IsBracketed
End If
Return False
End Function
''' <summary>
''' Returns the Type character for a given syntax token. This returns type character for Identifiers or Integer, Floating Point or Decimal Literals.
''' Examples: Dim a$ or Dim l1 = 1L
''' </summary>
''' <param name="token">The source SyntaxToken.</param>
''' <returns>A type character used for the specific Internal Syntax Token Types.</returns>
<Extension>
Public Function GetTypeCharacter(token As SyntaxToken) As TypeCharacter
Select Case token.Kind()
Case SyntaxKind.IdentifierToken
Dim id = DirectCast(token.Node, InternalSyntax.IdentifierTokenSyntax)
Return id.TypeCharacter
Case SyntaxKind.IntegerLiteralToken
Dim literal = DirectCast(token.Node, InternalSyntax.IntegerLiteralTokenSyntax)
Return literal.TypeSuffix
Case SyntaxKind.FloatingLiteralToken
Dim literal = DirectCast(token.Node, InternalSyntax.FloatingLiteralTokenSyntax)
Return literal.TypeSuffix
Case SyntaxKind.DecimalLiteralToken
Dim literal = DirectCast(token.Node, InternalSyntax.DecimalLiteralTokenSyntax)
Return literal.TypeSuffix
End Select
Return Nothing
End Function
''' <summary>
''' The source token base for Integer literals. Base can be Decimal, Hex or Octal.
''' </summary>
''' <param name="token">The source SyntaxToken.</param>
''' <returns>An instance representing the integer literal base.</returns>
<Extension>
Public Function GetBase(token As SyntaxToken) As LiteralBase?
If token.IsKind(SyntaxKind.IntegerLiteralToken) Then
Dim tk = DirectCast(token.Node, InternalSyntax.IntegerLiteralTokenSyntax)
Return tk.Base
End If
Return Nothing
End Function
''' <summary>
''' Determines if the token represents a reserved or contextual keyword
''' </summary>
''' <param name="token">The source SyntaxToken.</param>
''' <returns>A boolean value True if token is a keyword.</returns>
<Extension>
Public Function IsKeyword(token As SyntaxToken) As Boolean
Return SyntaxFacts.IsKeywordKind(token.Kind())
End Function
''' <summary>
''' Determines if the token represents a reserved keyword
''' </summary>
''' <param name="token">The source SyntaxToken.</param>
''' <returns>A boolean value True if token is a reserved keyword.</returns>
<Extension>
Public Function IsReservedKeyword(token As SyntaxToken) As Boolean
Return SyntaxFacts.IsReservedKeyword(token.Kind())
End Function
''' <summary>
''' Determines if the token represents a contextual keyword
''' </summary>
''' <returns>A boolean value True if token is a contextual keyword.</returns>
<Extension>
Public Function IsContextualKeyword(token As SyntaxToken) As Boolean
Return SyntaxFacts.IsContextualKeyword(token.Kind())
End Function
''' <summary>
''' Determines if the token represents a preprocessor keyword
''' </summary>
''' <param name="token">The source SyntaxToken.</param>
''' <returns> A boolean value True if token is a pre processor keyword.</returns>
<Extension>
Public Function IsPreprocessorKeyword(token As SyntaxToken) As Boolean
Return SyntaxFacts.IsPreprocessorKeyword(token.Kind())
End Function
''' <summary>
''' Returns the Identifiertext for a specified SyntaxToken.
''' </summary>
<Extension>
Public Function GetIdentifierText(token As SyntaxToken) As String
Return If(token.Node IsNot Nothing,
If(token.IsKind(SyntaxKind.IdentifierToken),
DirectCast(token.Node, InternalSyntax.IdentifierTokenSyntax).IdentifierText,
token.ToString()),
String.Empty)
End Function
''' <summary>
''' Insert one or more tokens in the list at the specified index.
''' </summary>
''' <returns>A new list with the tokens inserted.</returns>
<Extension>
Public Function Insert(list As SyntaxTokenList, index As Integer, ParamArray items As SyntaxToken()) As SyntaxTokenList
If index < 0 OrElse index > list.Count Then
Throw New ArgumentOutOfRangeException(NameOf(index))
End If
If items Is Nothing Then
Throw New ArgumentNullException(NameOf(items))
End If
If list.Count = 0 Then
Return SyntaxFactory.TokenList(items)
Else
Dim builder = New SyntaxTokenListBuilder(list.Count + items.Length)
If index > 0 Then
builder.Add(list, 0, index)
End If
builder.Add(items)
If index < list.Count Then
builder.Add(list, index, list.Count - index)
End If
Return builder.ToList()
End If
End Function
''' <summary>
''' Add one or more tokens to the end of the list.
''' </summary>
''' <returns>A new list with the tokens added.</returns>
<Extension>
Public Function Add(list As SyntaxTokenList, ParamArray items As SyntaxToken()) As SyntaxTokenList
Return Insert(list, list.Count, items)
End Function
''' <summary>
''' Replaces trivia on a specified SyntaxToken.
''' </summary>
''' <param name="token">The source SyntaxToken to change trivia on.</param>
''' <param name="oldTrivia">The original Trivia.</param>
''' <param name="newTrivia">The updated Trivia.</param>
''' <returns>The updated SyntaxToken with replaced trivia.</returns>
<Extension>
Public Function ReplaceTrivia(token As SyntaxToken, oldTrivia As SyntaxTrivia, newTrivia As SyntaxTrivia) As SyntaxToken
Return SyntaxReplacer.Replace(token, trivia:={oldTrivia}, computeReplacementTrivia:=Function(o, r) newTrivia)
End Function
''' <summary>
''' Replaces trivia on a specified SyntaxToken.
''' </summary>
<Extension>
Public Function ReplaceTrivia(token As SyntaxToken, trivia As IEnumerable(Of SyntaxTrivia), computeReplacementTrivia As Func(Of SyntaxTrivia, SyntaxTrivia, SyntaxTrivia)) As SyntaxToken
Return SyntaxReplacer.Replace(token, trivia:=trivia, computeReplacementTrivia:=computeReplacementTrivia)
End Function
<Extension>
Friend Function AsSeparatedList(Of TOther As SyntaxNode)(list As SyntaxNodeOrTokenList) As SeparatedSyntaxList(Of TOther)
Dim builder = SeparatedSyntaxListBuilder(Of TOther).Create
For Each i In list
Dim node = i.AsNode
If node IsNot Nothing Then
builder.Add(DirectCast(DirectCast(node, SyntaxNode), TOther))
Else
builder.AddSeparator(i.AsToken)
End If
Next
Return builder.ToList
End Function
''' <summary>
''' Gets the DirectiveTriviaSyntax items for a specified SyntaxNode with optional filtering.
''' </summary>
''' <param name="node">The source SyntaxNode.</param>
''' <param name="filter">The optional DirectiveTrivia Syntax filter predicate.</param>
''' <returns>A list of DirectiveTriviaSyntax items</returns>
<Extension>
Public Function GetDirectives(node As SyntaxNode, Optional filter As Func(Of DirectiveTriviaSyntax, Boolean) = Nothing) As IList(Of DirectiveTriviaSyntax)
Return DirectCast(node, VisualBasicSyntaxNode).GetDirectives(filter)
End Function
''' <summary>
''' Gets the first DirectiveTriviaSyntax item for a specified SyntaxNode.
''' </summary>
''' <param name="node">The source SyntaxNode.</param>
''' <param name="predicate">The optional DirectiveTriviaSyntax filter predicate.</param>
''' <returns>The first DirectiveSyntaxTrivia item.</returns>
<Extension> Public Function GetFirstDirective(node As SyntaxNode, Optional predicate As Func(Of DirectiveTriviaSyntax, Boolean) = Nothing) As DirectiveTriviaSyntax
Return DirectCast(node, VisualBasicSyntaxNode).GetFirstDirective(predicate)
End Function
''' <summary>
''' Gets the last DirectiveTriviaSyntax item for a specified SyntaxNode.
''' </summary>
''' <param name="node">The source node</param>
''' <param name="predicate">The optional DirectiveTriviaSyntax filter predicate.</param>
''' <returns>The last DirectiveSyntaxTrivia item.</returns>
<Extension>
Public Function GetLastDirective(node As SyntaxNode, Optional predicate As Func(Of DirectiveTriviaSyntax, Boolean) = Nothing) As DirectiveTriviaSyntax
Return DirectCast(node, VisualBasicSyntaxNode).GetLastDirective(predicate)
End Function
''' <summary>
''' Gets the root CompilationUnitSyntax for a specified SyntaxTree.
''' </summary>
''' <param name="tree">The source SyntaxTree.</param>
''' <returns>A CompilationUnitSyntax.</returns>
<Extension>
Public Function GetCompilationUnitRoot(tree As SyntaxTree) As CompilationUnitSyntax
Return DirectCast(tree.GetRoot(), CompilationUnitSyntax)
End Function
''' <summary>
''' Gets the reporting state for a warning at a given source location based on warning directives.
''' </summary>
<Extension>
Friend Function GetWarningState(tree As SyntaxTree, id As String, position As Integer) As ReportDiagnostic
Return DirectCast(tree, VisualBasicSyntaxTree).GetWarningState(id, position)
End Function
#Region "Symbols"
''' <summary>
''' Determines if symbol is Shared.
''' </summary>
''' <param name="symbol">The source symbol.</param>
''' <returns>A boolean value, True if symbol is Shared; otherwise False.</returns>
<Extension>
Public Function IsShared(symbol As ISymbol) As Boolean
Return symbol.IsStatic
End Function
<Extension>
Public Function IsOverrides(symbol As ISymbol) As Boolean
Return symbol.IsOverride
End Function
<Extension>
Public Function IsOverridable(symbol As ISymbol) As Boolean
Return symbol.IsVirtual
End Function
<Extension>
Public Function IsNotOverridable(symbol As ISymbol) As Boolean
Return symbol.IsSealed
End Function
<Extension>
Public Function IsMustOverride(symbol As ISymbol) As Boolean
Return symbol.IsAbstract
End Function
<Extension>
Public Function IsMe(parameterSymbol As IParameterSymbol) As Boolean
Return parameterSymbol.IsThis
End Function
<Extension>
Public Function IsOverloads(methodSymbol As IMethodSymbol) As Boolean
Dim vbmethod = TryCast(methodSymbol, MethodSymbol)
Return vbmethod IsNot Nothing AndAlso vbmethod.IsOverloads
End Function
<Extension>
Public Function IsOverloads(propertySymbol As IPropertySymbol) As Boolean
Dim vbprop = TryCast(propertySymbol, PropertySymbol)
Return vbprop IsNot Nothing AndAlso vbprop.IsOverloads
End Function
<Extension>
Public Function IsDefault(propertySymbol As IPropertySymbol) As Boolean
Dim vbprop = TryCast(propertySymbol, PropertySymbol)
Return vbprop IsNot Nothing AndAlso vbprop.IsDefault
End Function
<Extension>
Public Function HandledEvents(methodSymbol As IMethodSymbol) As ImmutableArray(Of HandledEvent)
Dim vbmethod = TryCast(methodSymbol, MethodSymbol)
If vbmethod IsNot Nothing Then
Return vbmethod.HandledEvents
Else
Return ImmutableArray(Of HandledEvent).Empty
End If
End Function
<Extension>
Public Function IsFor(localSymbol As ILocalSymbol) As Boolean
Dim vblocal = TryCast(localSymbol, LocalSymbol)
Return vblocal IsNot Nothing AndAlso vblocal.IsFor
End Function
<Extension>
Public Function IsForEach(localSymbol As ILocalSymbol) As Boolean
Dim vblocal = TryCast(localSymbol, LocalSymbol)
Return vblocal IsNot Nothing AndAlso vblocal.IsForEach
End Function
<Extension>
Public Function IsCatch(localSymbol As ILocalSymbol) As Boolean
Dim vblocal = TryCast(localSymbol, LocalSymbol)
Return vblocal IsNot Nothing AndAlso vblocal.IsCatch
End Function
<Extension>
Public Function AssociatedField(eventSymbol As IEventSymbol) As IFieldSymbol
Dim vbevent = TryCast(eventSymbol, EventSymbol)
Return If(vbevent IsNot Nothing, vbevent.AssociatedField, Nothing)
End Function
<Extension>
Public Function HasAssociatedField(eventSymbol As IEventSymbol) As Boolean
Dim vbevent = TryCast(eventSymbol, EventSymbol)
Return vbevent IsNot Nothing AndAlso vbevent.HasAssociatedField
End Function
<Extension>
Public Function GetFieldAttributes(eventSymbol As IEventSymbol) As ImmutableArray(Of AttributeData)
Dim vbevent = TryCast(eventSymbol, EventSymbol)
If vbevent IsNot Nothing Then
Return StaticCast(Of AttributeData).From(vbevent.GetFieldAttributes())
Else
Return ImmutableArray(Of AttributeData).Empty
End If
End Function
<Extension>
Public Function IsImplicitlyDeclared(eventSymbol As IEventSymbol) As Boolean
Dim vbevent = TryCast(eventSymbol, EventSymbol)
Return vbevent IsNot Nothing AndAlso vbevent.IsImplicitlyDeclared
End Function
''' <summary>
''' Gets all module members in a namespace.
''' </summary>
''' <param name="[namespace]">The source namespace symbol.</param>
''' <returns>An ImmutableArray of NamedTypeSymbol for all module members in namespace.</returns>
<Extension>
Public Function GetModuleMembers([namespace] As INamespaceSymbol) As ImmutableArray(Of INamedTypeSymbol)
Dim symbol = TryCast([namespace], NamespaceSymbol)
If symbol IsNot Nothing Then
Return StaticCast(Of INamedTypeSymbol).From(symbol.GetModuleMembers())
Else
Return ImmutableArray.Create(Of INamedTypeSymbol)()
End If
End Function
''' <summary>
''' Gets all module members in a specified namespace.
''' </summary>
''' <param name="[namespace]">The source namespace symbol.</param>
''' <param name="name">The name of the namespace.</param>
''' <returns>An ImmutableArray of NamedTypeSymbol for all module members in namespace.</returns>
<Extension>
Public Function GetModuleMembers([namespace] As INamespaceSymbol, name As String) As ImmutableArray(Of INamedTypeSymbol)
Dim symbol = TryCast([namespace], NamespaceSymbol)
If symbol IsNot Nothing Then
Return StaticCast(Of INamedTypeSymbol).From(symbol.GetModuleMembers(name))
Else
Return ImmutableArray.Create(Of INamedTypeSymbol)()
End If
End Function
#End Region
#Region "Compilation"
''' <summary>
''' Gets the Semantic Model OptionStrict property.
''' </summary>
''' <param name="semanticModel">A source Semantic model object.</param>
''' <returns>The OptionStrict object for the semantic model instance OptionStrict property, otherwise Null if semantic model is Null. </returns>
<Extension>
Public Function OptionStrict(semanticModel As SemanticModel) As OptionStrict
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.OptionStrict
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the Semantic Model OptionInfer property.
''' </summary>
''' <param name="semanticModel">A source Semantic model object.</param>
''' <returns>A boolean values, for the semantic model instance OptionInfer property. otherwise Null if semantic model is Null. </returns>
<Extension>
Public Function OptionInfer(semanticModel As SemanticModel) As Boolean
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.OptionInfer
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the Semantic Model OptionExplicit property.
''' </summary>
''' <param name="semanticModel">A source Semantic model object.</param>
''' <returns>A boolean values, for the semantic model instance OptionExplicit property. otherwise Null if semantic model is Null. </returns>
<Extension>
Public Function OptionExplicit(semanticModel As SemanticModel) As Boolean
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.OptionExplicit
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the Semantic Model OptionCompareText property.
''' </summary>
''' <param name="semanticModel">A source Semantic model object.</param>
''' <returns>A boolean values, for the semantic model instance OptionCompareText property. otherwise Null if semantic model is Null. </returns>
<Extension>
Public Function OptionCompareText(semanticModel As SemanticModel) As Boolean
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.OptionCompareText
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the compilation RootNamespace property.
''' </summary>
''' <param name="compilation">A source Compilation object.</param>
''' <returns>A NamespaceSymbol instance, for the compilation instance RootNamespace property. otherwise Null if compilation instance is Null. </returns>
<Extension>
Public Function RootNamespace(compilation As Compilation) As INamespaceSymbol
Dim vbcomp = TryCast(compilation, VisualBasicCompilation)
If vbcomp IsNot Nothing Then
Return vbcomp.RootNamespace
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the compilation AliasImports property.
''' </summary>
''' <param name="compilation">A source Compilation object.</param>
''' <returns>An ImmutableArray of AliasSymbol, from the compilation instance AliasImports property; otherwise an empty ImmutableArray if compilation instance is Null.</returns>
<Extension>
Public Function AliasImports(compilation As Compilation) As ImmutableArray(Of IAliasSymbol)
Dim vbcomp = TryCast(compilation, VisualBasicCompilation)
If vbcomp IsNot Nothing Then
Return StaticCast(Of IAliasSymbol).From(vbcomp.AliasImports)
Else
Return ImmutableArray.Create(Of IAliasSymbol)()
End If
End Function
''' <summary>
''' Gets the compilation MemberImports property.
''' </summary>
''' <param name="compilation">A source Compilation object.</param>
''' <returns>An ImmutableArray of NamespaceOrTypeSymbol, from the compilation instance MemberImports property; otherwise an empty ImmutableArray if compilation instance is Null.</returns>
<Extension>
Public Function MemberImports(compilation As Compilation) As ImmutableArray(Of INamespaceOrTypeSymbol)
Dim vbcomp = TryCast(compilation, VisualBasicCompilation)
If vbcomp IsNot Nothing Then
Return StaticCast(Of INamespaceOrTypeSymbol).From(vbcomp.MemberImports)
Else
Return ImmutableArray.Create(Of INamespaceOrTypeSymbol)()
End If
End Function
''' <summary>
''' Determines what kind of conversion there is between the specified types.
''' </summary>
''' <param name="compilation">A source Compilation object.</param>
''' <param name="source">A source Typesymbol</param>
''' <param name="destination">A destination Typesymbol</param>
''' <returns>A Conversion instance, representing the kind of conversion between the two type symbols; otherwise Null if compilation instance is Null.</returns>
<Extension>
Public Function ClassifyConversion(compilation As Compilation, source As ITypeSymbol, destination As ITypeSymbol) As Conversion
Dim vbcomp = TryCast(compilation, VisualBasicCompilation)
If vbcomp IsNot Nothing Then
Return vbcomp.ClassifyConversion(DirectCast(source, TypeSymbol), DirectCast(destination, TypeSymbol))
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the special type symbol in current compilation.
''' </summary>
''' <param name="compilation">A source Compilation object.</param>
''' <param name="typeId">The SpecialType to get.</param>
''' <returns>A NamedTypeSymbol for the specified type in compilation; Null if compilation is Null.</returns>
<Extension>
Public Function GetSpecialType(compilation As Compilation, typeId As SpecialType) As INamedTypeSymbol
Dim vbcomp = TryCast(compilation, VisualBasicCompilation)
If vbcomp IsNot Nothing Then
Return vbcomp.GetSpecialType(typeId)
Else
Return Nothing
End If
End Function
#End Region
#Region "SemanticModel"
''' <summary>
''' Determines what kind of conversion there is between the expression syntax and a specified type.
''' </summary>
''' <param name="semanticModel">A source semantic model.</param>
''' <param name="expression">A source expression syntax.</param>
''' <param name="destination">A destination TypeSymbol.</param>
''' <returns>A Conversion instance, representing the kind of conversion between the expression and type symbol; otherwise Null if semantic model instance is Null.</returns>
<Extension>
Public Function ClassifyConversion(semanticModel As SemanticModel, expression As ExpressionSyntax, destination As ITypeSymbol) As Conversion
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.ClassifyConversion(expression, DirectCast(destination, TypeSymbol))
Else
Return Nothing
End If
End Function
''' <summary>
''' Determines what kind of conversion there is between the expression syntax and a specified type.
''' </summary>
''' <param name="semanticModel">A source semantic model.</param>
''' <param name="position">A position within the expression syntax.</param>
''' <param name="expression">A source expression syntax.</param>
''' <param name="destination">A destination TypeSymbol.</param>
''' <returns>A Conversion instance, representing the kind of conversion between the expression and type symbol; otherwise Null if semantic model instance is Null.</returns>
<Extension>
Public Function ClassifyConversion(semanticModel As SemanticModel, position As Integer, expression As ExpressionSyntax, destination As ITypeSymbol) As Conversion
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.ClassifyConversion(position, expression, DirectCast(destination, TypeSymbol))
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding symbol for a specified identifier.
''' </summary>
''' <param name="semanticModel">A source semantic model.</param>
''' <param name="identifierSyntax">A IdentifierSyntax object.</param>
''' <param name="cancellationToken">A cancellation token.</param>
''' <returns>A symbol, for the specified identifier; otherwise Null if semantic model is Null. </returns>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, identifierSyntax As ModifiedIdentifierSyntax, Optional cancellationToken As CancellationToken = Nothing) As ISymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(identifierSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding symbol for a specified tuple element.
''' </summary>
''' <param name="semanticModel">A source semantic model.</param>
''' <param name="elementSyntax">A TupleElementSyntax object.</param>
''' <param name="cancellationToken">A cancellation token.</param>
''' <returns>A symbol, for the specified element; otherwise Nothing. </returns>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, elementSyntax As TupleElementSyntax, Optional cancellationToken As CancellationToken = Nothing) As ISymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(elementSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding PropertySymbol for a specified FieldInitializerSyntax.
''' </summary>
''' <param name="semanticModel">A source semantic model.</param>
''' <param name="fieldInitializerSyntax">A FieldInitializerSyntax object.</param>
''' <param name="cancellationToken">A cancellation token.</param>
''' <returns>A PropertySymbol. Null if semantic model is null.</returns>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, fieldInitializerSyntax As FieldInitializerSyntax, Optional cancellationToken As CancellationToken = Nothing) As IPropertySymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(fieldInitializerSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding NamedTypeSymbol for a specified AnonymousObjectCreationExpressionSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, anonymousObjectCreationExpressionSyntax As AnonymousObjectCreationExpressionSyntax, Optional cancellationToken As CancellationToken = Nothing) As INamedTypeSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(anonymousObjectCreationExpressionSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding RangeVariableSymbol for a specified ExpressionRangeVariableSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, rangeVariableSyntax As ExpressionRangeVariableSyntax, Optional cancellationToken As CancellationToken = Nothing) As IRangeVariableSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(rangeVariableSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding RangeVariableSymbol for a specified CollectionRangeVariableSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, rangeVariableSyntax As CollectionRangeVariableSyntax, Optional cancellationToken As CancellationToken = Nothing) As IRangeVariableSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(rangeVariableSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding RangeVariableSymbol for a specified AggregationRangeVariableSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, rangeVariableSyntax As AggregationRangeVariableSyntax, Optional cancellationToken As CancellationToken = Nothing) As IRangeVariableSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(rangeVariableSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding LabelSymbol for a specified LabelStatementSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, declarationSyntax As LabelStatementSyntax, Optional cancellationToken As CancellationToken = Nothing) As ILabelSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(declarationSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding FieldSymbol for a specified EnumMemberDeclarationSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, declarationSyntax As EnumMemberDeclarationSyntax, Optional cancellationToken As CancellationToken = Nothing) As IFieldSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(declarationSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding NamedTypeSymbol for a specified TypeStatementSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, declarationSyntax As TypeStatementSyntax, Optional cancellationToken As CancellationToken = Nothing) As INamedTypeSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(declarationSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding NamedTypeSymbol for a specified TypeBlockSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, declarationSyntax As TypeBlockSyntax, Optional cancellationToken As CancellationToken = Nothing) As INamedTypeSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(declarationSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding NamedTypeSymbol for a specified EnumStatementSyntax.
''' </summary>
<Extension>
Public Function GetDeclaredSymbol(semanticModel As SemanticModel, declarationSyntax As EnumStatementSyntax, Optional cancellationToken As CancellationToken = Nothing) As INamedTypeSymbol
Dim vbmodel = TryCast(semanticModel, VBSemanticModel)
If vbmodel IsNot Nothing Then
Return vbmodel.GetDeclaredSymbol(declarationSyntax, cancellationToken)
Else
Return Nothing
End If
End Function
''' <summary>
''' Gets the corresponding NamedTypeSymbol for a specified EnumBlockSyntax.
''' </summary>
<Extension>