/
CSharpExtensions.cs
1441 lines (1308 loc) · 63.7 KB
/
CSharpExtensions.cs
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.Syntax;
using Microsoft.CodeAnalysis.Operations;
namespace Microsoft.CodeAnalysis
{
public static class CSharpExtensions
{
public static bool IsKind(this SyntaxToken token, SyntaxKind kind)
{
return token.RawKind == (int)kind;
}
public static bool IsKind(this SyntaxTrivia trivia, SyntaxKind kind)
{
return trivia.RawKind == (int)kind;
}
public static bool IsKind(this SyntaxNode node, SyntaxKind kind)
{
return node?.RawKind == (int)kind;
}
public static bool IsKind(this SyntaxNodeOrToken nodeOrToken, SyntaxKind kind)
{
return nodeOrToken.RawKind == (int)kind;
}
internal static SyntaxKind ContextualKind(this SyntaxToken token)
{
return (object)token.Language == (object)LanguageNames.CSharp ? (SyntaxKind)token.RawContextualKind : SyntaxKind.None;
}
/// <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>
public static int IndexOf<TNode>(this SyntaxList<TNode> list, SyntaxKind kind)
where TNode : SyntaxNode
{
return list.IndexOf((int)kind);
}
/// <summary>
/// True if the list has at least one node of the specified kind.
/// </summary>
public static bool Any<TNode>(this SyntaxList<TNode> list, SyntaxKind kind)
where TNode : SyntaxNode
{
return list.IndexOf(kind) >= 0;
}
/// <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>
public static int IndexOf<TNode>(this SeparatedSyntaxList<TNode> list, SyntaxKind kind)
where TNode : SyntaxNode
{
return list.IndexOf((int)kind);
}
/// <summary>
/// True if the list has at least one node of the specified kind.
/// </summary>
public static bool Any<TNode>(this SeparatedSyntaxList<TNode> list, SyntaxKind kind)
where TNode : SyntaxNode
{
return list.IndexOf(kind) >= 0;
}
/// <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>
public static int IndexOf(this SyntaxTriviaList list, SyntaxKind kind)
{
return list.IndexOf((int)kind);
}
/// <summary>
/// True if the list has at least one trivia of the specified kind.
/// </summary>
public static bool Any(this SyntaxTriviaList list, SyntaxKind kind)
{
return list.IndexOf(kind) >= 0;
}
/// <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>
public static int IndexOf(this SyntaxTokenList list, SyntaxKind kind)
{
return list.IndexOf((int)kind);
}
/// <summary>
/// Tests whether a list contains a token of a particular kind.
/// </summary>
/// <param name="list"></param>
/// <param name="kind">The <see cref="CSharp.SyntaxKind"/> to test for.</param>
/// <returns>Returns true if the list contains a token which matches <paramref name="kind"/></returns>
public static bool Any(this SyntaxTokenList list, SyntaxKind kind)
{
return list.IndexOf(kind) >= 0;
}
internal static SyntaxToken FirstOrDefault(this SyntaxTokenList list, SyntaxKind kind)
{
int index = list.IndexOf(kind);
return (index >= 0) ? list[index] : default(SyntaxToken);
}
}
}
namespace Microsoft.CodeAnalysis.CSharp
{
public static class CSharpExtensions
{
internal static bool IsCSharpKind(int rawKind)
{
const int FirstVisualBasicKind = (int)SyntaxKind.List + 1;
const int FirstCSharpKind = (int)SyntaxKind.TildeToken;
// not in the range [FirstVisualBasicKind, FirstCSharpKind)
return unchecked((uint)(rawKind - FirstVisualBasicKind)) > (FirstCSharpKind - 1 - FirstVisualBasicKind);
}
public static SyntaxKind Kind(this SyntaxToken token)
{
var rawKind = token.RawKind;
return IsCSharpKind(rawKind) ? (SyntaxKind)rawKind : SyntaxKind.None;
}
public static SyntaxKind Kind(this SyntaxTrivia trivia)
{
var rawKind = trivia.RawKind;
return IsCSharpKind(rawKind) ? (SyntaxKind)rawKind : SyntaxKind.None;
}
public static SyntaxKind Kind(this SyntaxNode node)
{
var rawKind = node.RawKind;
return IsCSharpKind(rawKind) ? (SyntaxKind)rawKind : SyntaxKind.None;
}
public static SyntaxKind Kind(this SyntaxNodeOrToken nodeOrToken)
{
var rawKind = nodeOrToken.RawKind;
return IsCSharpKind(rawKind) ? (SyntaxKind)rawKind : SyntaxKind.None;
}
public static bool IsKeyword(this SyntaxToken token)
{
return SyntaxFacts.IsKeywordKind(token.Kind());
}
public static bool IsContextualKeyword(this SyntaxToken token)
{
return SyntaxFacts.IsContextualKeyword(token.Kind());
}
public static bool IsReservedKeyword(this SyntaxToken token)
{
return SyntaxFacts.IsReservedKeyword(token.Kind());
}
public static bool IsVerbatimStringLiteral(this SyntaxToken token)
{
return token.IsKind(SyntaxKind.StringLiteralToken) && token.Text.Length > 0 && token.Text[0] == '@';
}
public static bool IsVerbatimIdentifier(this SyntaxToken token)
{
return token.IsKind(SyntaxKind.IdentifierToken) && token.Text.Length > 0 && token.Text[0] == '@';
}
public static VarianceKind VarianceKindFromToken(this SyntaxToken node)
{
switch (node.Kind())
{
case SyntaxKind.OutKeyword: return VarianceKind.Out;
case SyntaxKind.InKeyword: return VarianceKind.In;
default: return VarianceKind.None;
}
}
/// <summary>
/// Insert one or more tokens in the list at the specified index.
/// </summary>
/// <returns>A new list with the tokens inserted.</returns>
public static SyntaxTokenList Insert(this SyntaxTokenList list, int index, params SyntaxToken[] items)
{
if (index < 0 || index > list.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
if (list.Count == 0)
{
return SyntaxFactory.TokenList(items);
}
else
{
var builder = new SyntaxTokenListBuilder(list.Count + items.Length);
if (index > 0)
{
builder.Add(list, 0, index);
}
builder.Add(items);
if (index < list.Count)
{
builder.Add(list, index, list.Count - index);
}
return builder.ToList();
}
}
/// <summary>
/// Creates a new token with the specified old trivia replaced with computed new trivia.
/// </summary>
/// <param name="token"></param>
/// <param name="trivia">The trivia to be replaced; descendants of the root token.</param>
/// <param name="computeReplacementTrivia">A function that computes a replacement trivia for
/// the argument trivia. The first argument is the original trivia. The second argument is
/// the same trivia rewritten with replaced structure.</param>
public static SyntaxToken ReplaceTrivia(this SyntaxToken token, IEnumerable<SyntaxTrivia> trivia, Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia> computeReplacementTrivia)
{
return Syntax.SyntaxReplacer.Replace(token, trivia: trivia, computeReplacementTrivia: computeReplacementTrivia);
}
/// <summary>
/// Creates a new token with the specified old trivia replaced with a new trivia. The old trivia may appear in
/// the token's leading or trailing trivia.
/// </summary>
/// <param name="token"></param>
/// <param name="oldTrivia">The trivia to be replaced.</param>
/// <param name="newTrivia">The new trivia to use in the new tree in place of the old
/// trivia.</param>
public static SyntaxToken ReplaceTrivia(this SyntaxToken token, SyntaxTrivia oldTrivia, SyntaxTrivia newTrivia)
{
return Syntax.SyntaxReplacer.Replace(token, trivia: new[] { oldTrivia }, computeReplacementTrivia: (o, r) => newTrivia);
}
internal static Syntax.InternalSyntax.DirectiveStack ApplyDirectives(this SyntaxNode node, Syntax.InternalSyntax.DirectiveStack stack)
{
return ((Syntax.InternalSyntax.CSharpSyntaxNode)node.Green).ApplyDirectives(stack);
}
internal static Syntax.InternalSyntax.DirectiveStack ApplyDirectives(this SyntaxToken token, Syntax.InternalSyntax.DirectiveStack stack)
{
return ((Syntax.InternalSyntax.CSharpSyntaxNode)token.Node).ApplyDirectives(stack);
}
internal static Syntax.InternalSyntax.DirectiveStack ApplyDirectives(this SyntaxNodeOrToken nodeOrToken, Syntax.InternalSyntax.DirectiveStack stack)
{
if (nodeOrToken.IsToken)
{
return nodeOrToken.AsToken().ApplyDirectives(stack);
}
if (nodeOrToken.IsNode)
{
return nodeOrToken.AsNode().ApplyDirectives(stack);
}
return stack;
}
/// <summary>
/// Returns this list as a <see cref="Microsoft.CodeAnalysis.SeparatedSyntaxList<TNode>"/>.
/// </summary>
/// <typeparam name="TOther">The type of the list elements in the separated list.</typeparam>
/// <returns></returns>
internal static SeparatedSyntaxList<TOther> AsSeparatedList<TOther>(this SyntaxNodeOrTokenList list) where TOther : SyntaxNode
{
var builder = SeparatedSyntaxListBuilder<TOther>.Create();
foreach (var i in list)
{
var node = i.AsNode();
if (node != null)
{
builder.Add((TOther)node);
}
else
{
builder.AddSeparator(i.AsToken());
}
}
return builder.ToList();
}
#region SyntaxNode
internal static IList<DirectiveTriviaSyntax> GetDirectives(this SyntaxNode node, Func<DirectiveTriviaSyntax, bool> filter = null)
{
return ((CSharpSyntaxNode)node).GetDirectives(filter);
}
/// <summary>
/// Gets the first directive of the tree rooted by this node.
/// </summary>
public static DirectiveTriviaSyntax GetFirstDirective(this SyntaxNode node, Func<DirectiveTriviaSyntax, bool> predicate = null)
{
return ((CSharpSyntaxNode)node).GetFirstDirective(predicate);
}
/// <summary>
/// Gets the last directive of the tree rooted by this node.
/// </summary>
public static DirectiveTriviaSyntax GetLastDirective(this SyntaxNode node, Func<DirectiveTriviaSyntax, bool> predicate = null)
{
return ((CSharpSyntaxNode)node).GetLastDirective(predicate);
}
#endregion
#region SyntaxTree
public static CompilationUnitSyntax GetCompilationUnitRoot(this SyntaxTree tree, CancellationToken cancellationToken = default(CancellationToken))
{
return (CompilationUnitSyntax)tree.GetRoot(cancellationToken);
}
internal static bool HasReferenceDirectives(this SyntaxTree tree)
{
var csharpTree = tree as CSharpSyntaxTree;
return csharpTree != null && csharpTree.HasReferenceDirectives;
}
internal static bool HasReferenceOrLoadDirectives(this SyntaxTree tree)
{
var csharpTree = tree as CSharpSyntaxTree;
return csharpTree != null && csharpTree.HasReferenceOrLoadDirectives;
}
internal static bool IsAnyPreprocessorSymbolDefined(this SyntaxTree tree, ImmutableArray<string> conditionalSymbols)
{
var csharpTree = tree as CSharpSyntaxTree;
return csharpTree != null && csharpTree.IsAnyPreprocessorSymbolDefined(conditionalSymbols);
}
internal static bool IsPreprocessorSymbolDefined(this SyntaxTree tree, string symbolName, int position)
{
var csharpTree = tree as CSharpSyntaxTree;
return csharpTree != null && csharpTree.IsPreprocessorSymbolDefined(symbolName, position);
}
// Given the error code and the source location, get the warning state based on pragma warning directives.
internal static PragmaWarningState GetPragmaDirectiveWarningState(this SyntaxTree tree, string id, int position)
{
return ((CSharpSyntaxTree)tree).GetPragmaDirectiveWarningState(id, position);
}
#endregion
#region Compilation
// NOTE(cyrusn): There is a bit of a discoverability problem with this method and the same
// named method in SyntaxTreeSemanticModel. Technically, i believe these are the appropriate
// locations for these methods. This method has no dependencies on anything but the
// compilation, while the other method needs a bindings object to determine what bound node
// an expression syntax binds to. Perhaps when we document these methods we should explain
// where a user can find the other.
public static Conversion ClassifyConversion(this Compilation compilation, ITypeSymbol source, ITypeSymbol destination)
{
var cscomp = compilation as CSharpCompilation;
if (cscomp != null)
{
return cscomp.ClassifyConversion(source, destination);
}
else
{
return Conversion.NoConversion;
}
}
#endregion
#region SemanticModel
/// <summary>
/// Gets the semantic information for an ordering clause in an orderby query clause.
/// </summary>
public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, OrderingSyntax node, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSymbolInfo(node, cancellationToken);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Gets the semantic information associated with a select or group clause.
/// </summary>
public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, SelectOrGroupClauseSyntax node, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSymbolInfo(node, cancellationToken);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Returns what symbol(s), if any, the given expression syntax bound to in the program.
///
/// An AliasSymbol will never be returned by this method. What the alias refers to will be
/// returned instead. To get information about aliases, call GetAliasInfo.
///
/// If binding the type name C in the expression "new C(...)" the actual constructor bound to
/// will be returned (or all constructor if overload resolution failed). This occurs as long as C
/// unambiguously binds to a single type that has a constructor. If C ambiguously binds to multiple
/// types, or C binds to a static class, then type(s) are returned.
/// </summary>
public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSymbolInfo(expression, cancellationToken);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Returns what 'Add' method symbol(s), if any, corresponds to the given expression syntax
/// within <see cref="ObjectCreationExpressionSyntax.Initializer"/>.
/// </summary>
public static SymbolInfo GetCollectionInitializerSymbolInfo(this SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetCollectionInitializerSymbolInfo(expression, cancellationToken);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Returns what symbol(s), if any, the given constructor initializer syntax bound to in the program.
/// </summary>
public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, ConstructorInitializerSyntax constructorInitializer, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSymbolInfo(constructorInitializer, cancellationToken);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Returns what symbol(s), if any, the given attribute syntax bound to in the program.
/// </summary>
public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, AttributeSyntax attributeSyntax, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSymbolInfo(attributeSyntax, cancellationToken);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Gets the semantic information associated with a documentation comment cref.
/// </summary>
public static SymbolInfo GetSymbolInfo(this SemanticModel semanticModel, CrefSyntax crefSyntax, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSymbolInfo(crefSyntax, cancellationToken);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Binds the expression in the context of the specified location and gets symbol information.
/// This method is used to get symbol information about an expression that did not actually
/// appear in the source code.
/// </summary>
public static SymbolInfo GetSpeculativeSymbolInfo(this SemanticModel semanticModel, int position, ExpressionSyntax expression, SpeculativeBindingOption bindingOption)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSpeculativeSymbolInfo(position, expression, bindingOption);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Binds the CrefSyntax expression in the context of the specified location and gets symbol information.
/// This method is used to get symbol information about an expression that did not actually
/// appear in the source code.
/// </summary>
public static SymbolInfo GetSpeculativeSymbolInfo(this SemanticModel semanticModel, int position, CrefSyntax expression, SpeculativeBindingOption bindingOption)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSpeculativeSymbolInfo(position, expression, bindingOption);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Bind the attribute in the context of the specified location and get semantic information
/// such as type, symbols and diagnostics. This method is used to get semantic information about an attribute
/// that did not actually appear in the source code.
/// </summary>
public static SymbolInfo GetSpeculativeSymbolInfo(this SemanticModel semanticModel, int position, AttributeSyntax attribute)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSpeculativeSymbolInfo(position, attribute);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Bind the constructor initializer in the context of the specified location and get semantic information
/// such as type, symbols and diagnostics. This method is used to get semantic information about a constructor
/// initializer that did not actually appear in the source code.
///
/// NOTE: This will only work in locations where there is already a constructor initializer.
/// </summary>
public static SymbolInfo GetSpeculativeSymbolInfo(this SemanticModel semanticModel, int position, ConstructorInitializerSyntax constructorInitializer)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSpeculativeSymbolInfo(position, constructorInitializer);
}
else
{
return SymbolInfo.None;
}
}
/// <summary>
/// Gets type information about a constructor initializer.
/// </summary>
public static TypeInfo GetTypeInfo(this SemanticModel semanticModel, ConstructorInitializerSyntax constructorInitializer, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetTypeInfo(constructorInitializer, cancellationToken);
}
else
{
return CSharpTypeInfo.None;
}
}
public static TypeInfo GetTypeInfo(this SemanticModel semanticModel, SelectOrGroupClauseSyntax node, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetTypeInfo(node, cancellationToken);
}
else
{
return CSharpTypeInfo.None;
}
}
/// <summary>
/// Gets type information about an expression.
/// </summary>
public static TypeInfo GetTypeInfo(this SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetTypeInfo(expression, cancellationToken);
}
else
{
return CSharpTypeInfo.None;
}
}
/// <summary>
/// Gets type information about an attribute.
/// </summary>
public static TypeInfo GetTypeInfo(this SemanticModel semanticModel, AttributeSyntax attributeSyntax, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetTypeInfo(attributeSyntax, cancellationToken);
}
else
{
return CSharpTypeInfo.None;
}
}
/// <summary>
/// Binds the expression in the context of the specified location and gets type information.
/// This method is used to get type information about an expression that did not actually
/// appear in the source code.
/// </summary>
public static TypeInfo GetSpeculativeTypeInfo(this SemanticModel semanticModel, int position, ExpressionSyntax expression, SpeculativeBindingOption bindingOption)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSpeculativeTypeInfo(position, expression, bindingOption);
}
else
{
return CSharpTypeInfo.None;
}
}
public static Conversion GetConversion(this SemanticModel semanticModel, SyntaxNode expression, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetConversion(expression, cancellationToken);
}
else
{
return Conversion.NoConversion;
}
}
/// <summary>
/// Gets the underlying <see cref="Conversion"/> information from this <see cref="IConversionOperation"/>. This
/// <see cref="IConversionOperation"/> must have been created from CSharp code.
/// </summary>
/// <param name="conversionExpression">The conversion expression to get original info from.</param>
/// <returns>The underlying <see cref="Conversion"/>.</returns>
/// <exception cref="InvalidCastException">If the <see cref="IConversionOperation"/> was not created from CSharp code.</exception>
public static Conversion GetConversion(this IConversionOperation conversionExpression)
{
if (conversionExpression.Language == LanguageNames.CSharp)
{
return (Conversion)((BaseConversionOperation)conversionExpression).ConvertibleConversion;
}
else
{
throw new ArgumentException(string.Format(CSharpResources.IConversionExpressionIsNotCSharpConversion,
nameof(IConversionOperation)),
nameof(conversionExpression));
}
}
/// <summary>
/// Gets the underlying <see cref="Conversion"/> information from this <see cref="ICompoundAssignmentOperation"/>. This
/// conversion is applied before the operator is applied to the result of this conversion and <see cref="IAssignmentOperation.Value"/>.
/// </summary>
/// <remarks>
/// This compound assignment must have been created from C# code.
/// </remarks>
public static Conversion GetInConversion(this ICompoundAssignmentOperation compoundAssignment)
{
if (compoundAssignment == null)
{
throw new ArgumentNullException(nameof(compoundAssignment));
}
if (compoundAssignment.Language == LanguageNames.CSharp)
{
return (Conversion)((BaseCompoundAssignmentOperation)compoundAssignment).InConversionConvertible;
}
else
{
throw new ArgumentException(string.Format(CSharpResources.ICompoundAssignmentOperationIsNotCSharpCompoundAssignment,
nameof(compoundAssignment)),
nameof(compoundAssignment));
}
}
/// <summary>
/// Gets the underlying <see cref="Conversion"/> information from this <see cref="ICompoundAssignmentOperation"/>. This
/// conversion is applied after the operator is applied, before the result is assigned to <see cref="IAssignmentOperation.Target"/>.
/// </summary>
/// <remarks>
/// This compound assignment must have been created from C# code.
/// </remarks>
public static Conversion GetOutConversion(this ICompoundAssignmentOperation compoundAssignment)
{
if (compoundAssignment == null)
{
throw new ArgumentNullException(nameof(compoundAssignment));
}
if (compoundAssignment.Language == LanguageNames.CSharp)
{
return (Conversion)((BaseCompoundAssignmentOperation)compoundAssignment).OutConversionConvertible;
}
else
{
throw new ArgumentException(string.Format(CSharpResources.ICompoundAssignmentOperationIsNotCSharpCompoundAssignment,
nameof(compoundAssignment)),
nameof(compoundAssignment));
}
}
public static Conversion GetSpeculativeConversion(this SemanticModel semanticModel, int position, ExpressionSyntax expression, SpeculativeBindingOption bindingOption)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetSpeculativeConversion(position, expression, bindingOption);
}
else
{
return Conversion.NoConversion;
}
}
public static ForEachStatementInfo GetForEachStatementInfo(this SemanticModel semanticModel, ForEachStatementSyntax forEachStatement)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetForEachStatementInfo(forEachStatement);
}
else
{
return default(ForEachStatementInfo);
}
}
public static ForEachStatementInfo GetForEachStatementInfo(this SemanticModel semanticModel, CommonForEachStatementSyntax forEachStatement)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetForEachStatementInfo(forEachStatement);
}
else
{
return default(ForEachStatementInfo);
}
}
public static DeconstructionInfo GetDeconstructionInfo(this SemanticModel semanticModel, AssignmentExpressionSyntax assignment)
{
return semanticModel is CSharpSemanticModel csmodel ? csmodel.GetDeconstructionInfo(assignment) : default;
}
public static DeconstructionInfo GetDeconstructionInfo(this SemanticModel semanticModel, ForEachVariableStatementSyntax @foreach)
{
return semanticModel is CSharpSemanticModel csmodel ? csmodel.GetDeconstructionInfo(@foreach) : default;
}
public static AwaitExpressionInfo GetAwaitExpressionInfo(this SemanticModel semanticModel, AwaitExpressionSyntax awaitExpression)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetAwaitExpressionInfo(awaitExpression);
}
else
{
return default(AwaitExpressionInfo);
}
}
public static ImmutableArray<ISymbol> GetMemberGroup(this SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetMemberGroup(expression, cancellationToken);
}
else
{
return ImmutableArray.Create<ISymbol>();
}
}
public static ImmutableArray<ISymbol> GetMemberGroup(this SemanticModel semanticModel, AttributeSyntax attribute, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetMemberGroup(attribute, cancellationToken);
}
else
{
return ImmutableArray.Create<ISymbol>();
}
}
public static ImmutableArray<ISymbol> GetMemberGroup(this SemanticModel semanticModel, ConstructorInitializerSyntax initializer, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetMemberGroup(initializer, cancellationToken);
}
else
{
return ImmutableArray.Create<ISymbol>();
}
}
/// <summary>
/// Returns the list of accessible, non-hidden indexers that could be invoked with the given expression as receiver.
/// </summary>
public static ImmutableArray<IPropertySymbol> GetIndexerGroup(this SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetIndexerGroup(expression, cancellationToken);
}
else
{
return ImmutableArray.Create<IPropertySymbol>();
}
}
public static Optional<object> GetConstantValue(this SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetConstantValue(expression, cancellationToken);
}
else
{
return default(Optional<object>);
}
}
/// <summary>
/// Gets the semantic information associated with a query clause.
/// </summary>
public static QueryClauseInfo GetQueryClauseInfo(this SemanticModel semanticModel, QueryClauseSyntax node, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.GetQueryClauseInfo(node, cancellationToken);
}
else
{
return default(QueryClauseInfo);
}
}
/// <summary>
/// If <paramref name="nameSyntax"/> resolves to an alias name, return the AliasSymbol corresponding
/// to A. Otherwise return null.
/// </summary>
public static IAliasSymbol GetAliasInfo(this SemanticModel semanticModel, IdentifierNameSyntax nameSyntax, CancellationToken cancellationToken = default(CancellationToken))
{
var csmodel = semanticModel as CSharpSemanticModel;
return csmodel?.GetAliasInfo(nameSyntax, cancellationToken);
}
/// <summary>
/// Binds the name in the context of the specified location and sees if it resolves to an
/// alias name. If it does, return the AliasSymbol corresponding to it. Otherwise, return null.
/// </summary>
public static IAliasSymbol GetSpeculativeAliasInfo(this SemanticModel semanticModel, int position, IdentifierNameSyntax nameSyntax, SpeculativeBindingOption bindingOption)
{
var csmodel = semanticModel as CSharpSemanticModel;
return csmodel?.GetSpeculativeAliasInfo(position, nameSyntax, bindingOption);
}
/// <summary>
/// Analyze control-flow within a part of a method body.
/// </summary>
public static ControlFlowAnalysis AnalyzeControlFlow(this SemanticModel semanticModel, StatementSyntax firstStatement, StatementSyntax lastStatement)
{
var csmodel = semanticModel as CSharpSemanticModel;
return csmodel?.AnalyzeControlFlow(firstStatement, lastStatement);
}
/// <summary>
/// Analyze control-flow within a part of a method body.
/// </summary>
public static ControlFlowAnalysis AnalyzeControlFlow(this SemanticModel semanticModel, StatementSyntax statement)
{
var csmodel = semanticModel as CSharpSemanticModel;
return csmodel?.AnalyzeControlFlow(statement);
}
/// <summary>
/// Analyze data-flow within an expression.
/// </summary>
public static DataFlowAnalysis AnalyzeDataFlow(this SemanticModel semanticModel, ExpressionSyntax expression)
{
var csmodel = semanticModel as CSharpSemanticModel;
return csmodel?.AnalyzeDataFlow(expression);
}
/// <summary>
/// Analyze data-flow within a part of a method body.
/// </summary>
public static DataFlowAnalysis AnalyzeDataFlow(this SemanticModel semanticModel, StatementSyntax firstStatement, StatementSyntax lastStatement)
{
var csmodel = semanticModel as CSharpSemanticModel;
return csmodel?.AnalyzeDataFlow(firstStatement, lastStatement);
}
/// <summary>
/// Analyze data-flow within a part of a method body.
/// </summary>
public static DataFlowAnalysis AnalyzeDataFlow(this SemanticModel semanticModel, StatementSyntax statement)
{
var csmodel = semanticModel as CSharpSemanticModel;
return csmodel?.AnalyzeDataFlow(statement);
}
/// <summary>
/// Get a SemanticModel object that is associated with a method body that did not appear in this source code.
/// Given <paramref name="position"/> must lie within an existing method body of the Root syntax node for this SemanticModel.
/// Locals and labels declared within this existing method body are not considered to be in scope of the speculated method body.
/// </summary>
public static bool TryGetSpeculativeSemanticModelForMethodBody(this SemanticModel semanticModel, int position, BaseMethodDeclarationSyntax method, out SemanticModel speculativeModel)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{
return csmodel.TryGetSpeculativeSemanticModelForMethodBody(position, method, out speculativeModel);
}
else
{
speculativeModel = null;
return false;
}
}
/// <summary>
/// Get a SemanticModel object that is associated with a method body that did not appear in this source code.
/// Given <paramref name="position"/> must lie within an existing method body of the Root syntax node for this SemanticModel.
/// Locals and labels declared within this existing method body are not considered to be in scope of the speculated method body.
/// </summary>
public static bool TryGetSpeculativeSemanticModelForMethodBody(this SemanticModel semanticModel, int position, AccessorDeclarationSyntax accessor, out SemanticModel speculativeModel)
{
var csmodel = semanticModel as CSharpSemanticModel;
if (csmodel != null)
{