130 lines
3.3 KiB
C#
130 lines
3.3 KiB
C#
using System.Threading;
|
|
using Microsoft.CodeAnalysis.Text;
|
|
|
|
namespace Microsoft.CodeAnalysis.CSharp.Symbols;
|
|
|
|
internal struct LexicalSortKey
|
|
{
|
|
private int _treeOrdinal;
|
|
|
|
private int _position;
|
|
|
|
public static readonly LexicalSortKey NotInSource;
|
|
|
|
public static readonly LexicalSortKey NotInitialized;
|
|
|
|
public static readonly LexicalSortKey SynthesizedCtor;
|
|
|
|
public static readonly LexicalSortKey SynthesizedCCtor;
|
|
|
|
public int TreeOrdinal => _treeOrdinal;
|
|
|
|
public int Position => _position;
|
|
|
|
public bool IsInitialized => Volatile.Read(in _position) >= 0;
|
|
|
|
public static LexicalSortKey GetSynthesizedMemberKey(int offset)
|
|
{
|
|
return new LexicalSortKey
|
|
{
|
|
_treeOrdinal = int.MaxValue,
|
|
_position = 2147483645 - offset
|
|
};
|
|
}
|
|
|
|
private LexicalSortKey(int treeOrdinal, int position)
|
|
{
|
|
_treeOrdinal = treeOrdinal;
|
|
_position = position;
|
|
}
|
|
|
|
private LexicalSortKey(SyntaxTree tree, int position, CSharpCompilation compilation)
|
|
{
|
|
this = new LexicalSortKey((tree == null) ? (-1) : ((Compilation)compilation).GetSyntaxTreeOrdinal(tree), position);
|
|
}
|
|
|
|
public LexicalSortKey(SyntaxReference syntaxRef, CSharpCompilation compilation)
|
|
{
|
|
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
|
|
SyntaxTree syntaxTree = syntaxRef.SyntaxTree;
|
|
TextSpan span = syntaxRef.Span;
|
|
this = new LexicalSortKey(syntaxTree, ((TextSpan)(ref span)).Start, compilation);
|
|
}
|
|
|
|
public LexicalSortKey(Location location, CSharpCompilation compilation)
|
|
{
|
|
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
|
|
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
|
|
SyntaxTree sourceTree = location.SourceTree;
|
|
TextSpan sourceSpan = location.SourceSpan;
|
|
this = new LexicalSortKey(sourceTree, ((TextSpan)(ref sourceSpan)).Start, compilation);
|
|
}
|
|
|
|
public LexicalSortKey(CSharpSyntaxNode node, CSharpCompilation compilation)
|
|
{
|
|
this = new LexicalSortKey(node.SyntaxTree, ((SyntaxNode)node).SpanStart, compilation);
|
|
}
|
|
|
|
public LexicalSortKey(SyntaxToken token, CSharpCompilation compilation)
|
|
{
|
|
this = new LexicalSortKey(((SyntaxToken)(ref token)).SyntaxTree, ((SyntaxToken)(ref token)).SpanStart, compilation);
|
|
}
|
|
|
|
public static int Compare(LexicalSortKey xSortKey, LexicalSortKey ySortKey)
|
|
{
|
|
if (xSortKey.TreeOrdinal != ySortKey.TreeOrdinal)
|
|
{
|
|
if (xSortKey.TreeOrdinal < 0)
|
|
{
|
|
return 1;
|
|
}
|
|
if (ySortKey.TreeOrdinal < 0)
|
|
{
|
|
return -1;
|
|
}
|
|
return xSortKey.TreeOrdinal - ySortKey.TreeOrdinal;
|
|
}
|
|
return xSortKey.Position - ySortKey.Position;
|
|
}
|
|
|
|
public static LexicalSortKey First(LexicalSortKey xSortKey, LexicalSortKey ySortKey)
|
|
{
|
|
if (Compare(xSortKey, ySortKey) <= 0)
|
|
{
|
|
return xSortKey;
|
|
}
|
|
return ySortKey;
|
|
}
|
|
|
|
public void SetFrom(LexicalSortKey other)
|
|
{
|
|
_treeOrdinal = other._treeOrdinal;
|
|
Volatile.Write(ref _position, other._position);
|
|
}
|
|
|
|
static LexicalSortKey()
|
|
{
|
|
NotInSource = new LexicalSortKey
|
|
{
|
|
_treeOrdinal = -1,
|
|
_position = 0
|
|
};
|
|
NotInitialized = new LexicalSortKey
|
|
{
|
|
_treeOrdinal = -1,
|
|
_position = -1
|
|
};
|
|
SynthesizedCtor = new LexicalSortKey
|
|
{
|
|
_treeOrdinal = int.MaxValue,
|
|
_position = 2147483646
|
|
};
|
|
SynthesizedCCtor = new LexicalSortKey
|
|
{
|
|
_treeOrdinal = int.MaxValue,
|
|
_position = int.MaxValue
|
|
};
|
|
}
|
|
}
|