/
SyntaxAnnotation.cs
104 lines (86 loc) · 3.19 KB
/
SyntaxAnnotation.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
// 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.Diagnostics;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// A SyntaxAnnotation is used to annotate syntax elements with additional information.
///
/// Since syntax elements are immutable, annotating them requires creating new instances of them
/// with the annotations attached.
/// </summary>
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
public sealed class SyntaxAnnotation : IObjectWritable, IEquatable<SyntaxAnnotation?>
{
static SyntaxAnnotation()
{
ObjectBinder.RegisterTypeReader(typeof(SyntaxAnnotation), r => new SyntaxAnnotation(r));
}
/// <summary>
/// A predefined syntax annotation that indicates whether the syntax element has elastic trivia.
/// </summary>
public static SyntaxAnnotation ElasticAnnotation { get; } = new SyntaxAnnotation();
// use a value identity instead of object identity so a deserialized instance matches the original instance.
private readonly long _id;
private static long s_nextId;
// use a value identity instead of object identity so a deserialized instance matches the original instance.
public string? Kind { get; }
public string? Data { get; }
public SyntaxAnnotation()
{
_id = System.Threading.Interlocked.Increment(ref s_nextId);
}
public SyntaxAnnotation(string? kind)
: this()
{
this.Kind = kind;
}
public SyntaxAnnotation(string? kind, string? data)
: this(kind)
{
this.Data = data;
}
private SyntaxAnnotation(ObjectReader reader)
{
_id = reader.ReadInt64();
this.Kind = reader.ReadString();
this.Data = reader.ReadString();
}
bool IObjectWritable.ShouldReuseInSerialization => true;
void IObjectWritable.WriteTo(ObjectWriter writer)
{
writer.WriteInt64(_id);
writer.WriteString(this.Kind);
writer.WriteString(this.Data);
}
private string GetDebuggerDisplay()
{
return string.Format("Annotation: Kind='{0}' Data='{1}'", this.Kind ?? "", this.Data ?? "");
}
public bool Equals(SyntaxAnnotation? other)
{
return other is object && _id == other._id;
}
public static bool operator ==(SyntaxAnnotation? left, SyntaxAnnotation? right)
{
if (left is null)
{
return right is null;
}
return left.Equals(right);
}
public static bool operator !=(SyntaxAnnotation? left, SyntaxAnnotation? right) =>
!(left == right);
public override bool Equals(object? obj)
{
return this.Equals(obj as SyntaxAnnotation);
}
public override int GetHashCode()
{
return _id.GetHashCode();
}
}
}