-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SymbolInfo.cs
125 lines (107 loc) · 4.58 KB
/
SymbolInfo.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
// 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.
using System;
using System.Collections.Immutable;
using System.Linq;
using Roslyn.Utilities;
using System.Diagnostics;
namespace Microsoft.CodeAnalysis
{
public struct SymbolInfo : IEquatable<SymbolInfo>
{
internal static readonly SymbolInfo None = new SymbolInfo(null, ImmutableArray<ISymbol>.Empty, CandidateReason.None);
private readonly ImmutableArray<ISymbol> _candidateSymbols;
/// <summary>
/// The symbol that was referred to by the syntax node, if any. Returns null if the given
/// expression did not bind successfully to a single symbol. If null is returned, it may
/// still be that case that we have one or more "best guesses" as to what symbol was
/// intended. These best guesses are available via the CandidateSymbols property.
/// </summary>
public ISymbol? Symbol { get; }
/// <summary>
/// If the expression did not successfully resolve to a symbol, but there were one or more
/// symbols that may have been considered but discarded, this property returns those
/// symbols. The reason that the symbols did not successfully resolve to a symbol are
/// available in the CandidateReason property. For example, if the symbol was inaccessible,
/// ambiguous, or used in the wrong context.
/// </summary>
public ImmutableArray<ISymbol> CandidateSymbols
{
get
{
return _candidateSymbols.NullToEmpty();
}
}
internal ImmutableArray<ISymbol> GetAllSymbols()
{
if (this.Symbol != null)
{
return ImmutableArray.Create<ISymbol>(this.Symbol);
}
else
{
return _candidateSymbols;
}
}
///<summary>
/// If the expression did not successfully resolve to a symbol, but there were one or more
/// symbols that may have been considered but discarded, this property describes why those
/// symbol or symbols were not considered suitable.
/// </summary>
public CandidateReason CandidateReason { get; }
internal SymbolInfo(ISymbol symbol)
: this(symbol, ImmutableArray<ISymbol>.Empty, CandidateReason.None)
{
}
internal SymbolInfo(ISymbol symbol, CandidateReason reason)
: this(symbol, ImmutableArray<ISymbol>.Empty, reason)
{
}
internal SymbolInfo(ImmutableArray<ISymbol> candidateSymbols, CandidateReason candidateReason)
: this(null, candidateSymbols, candidateReason)
{
}
internal SymbolInfo(ISymbol? symbol, ImmutableArray<ISymbol> candidateSymbols, CandidateReason candidateReason)
: this()
{
this.Symbol = symbol;
_candidateSymbols = candidateSymbols.IsDefault ? ImmutableArray.Create<ISymbol>() : candidateSymbols;
#if DEBUG
const NamespaceKind NamespaceKindNamespaceGroup = (NamespaceKind)0;
Debug.Assert(symbol is null || symbol.Kind != SymbolKind.Namespace || ((INamespaceSymbol)symbol).NamespaceKind != NamespaceKindNamespaceGroup);
foreach (var item in _candidateSymbols)
{
Debug.Assert(item.Kind != SymbolKind.Namespace || ((INamespaceSymbol)item).NamespaceKind != NamespaceKindNamespaceGroup);
}
#endif
this.CandidateReason = candidateReason;
}
public override bool Equals(object? obj)
{
return obj is SymbolInfo && Equals((SymbolInfo)obj);
}
public bool Equals(SymbolInfo other)
{
if (!object.Equals(this.Symbol, other.Symbol) ||
_candidateSymbols.IsDefault != other._candidateSymbols.IsDefault ||
this.CandidateReason != other.CandidateReason)
{
return false;
}
return _candidateSymbols.IsDefault || _candidateSymbols.SequenceEqual(other._candidateSymbols);
}
public override int GetHashCode()
{
return Hash.Combine(this.Symbol, Hash.Combine(Hash.CombineValues(_candidateSymbols, 4), (int)this.CandidateReason));
}
internal bool IsEmpty
{
get
{
return this.Symbol == null
&& this.CandidateSymbols.Length == 0;
}
}
}
}