/
TypedConstantExtensions.cs
217 lines (185 loc) · 8.47 KB
/
TypedConstantExtensions.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
// 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.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp
{
public static class TypedConstantExtensions
{
/// <summary>
/// Returns the System.String that represents the current TypedConstant.
/// </summary>
/// <returns>A System.String that represents the current TypedConstant.</returns>
public static string ToCSharpString(this TypedConstant constant)
{
if (constant.IsNull)
{
return "null";
}
if (constant.Kind == TypedConstantKind.Array)
{
return "{" + string.Join(", ", constant.Values.Select(v => v.ToCSharpString())) + "}";
}
if (constant.Kind == TypedConstantKind.Type || constant.Type.SpecialType == SpecialType.System_Object)
{
return "typeof(" + constant.Value.ToString() + ")";
}
if (constant.Kind == TypedConstantKind.Enum)
{
// TODO (tomat): replace with SymbolDisplay
return DisplayEnumConstant(constant);
}
return SymbolDisplay.FormatPrimitive(constant.Value, quoteStrings: true, useHexadecimalNumbers: false);
}
// Decode the value of enum constant
private static string DisplayEnumConstant(TypedConstant constant)
{
Debug.Assert(constant.Kind == TypedConstantKind.Enum);
// Create a ConstantValue of enum underlying type
SpecialType splType = ((INamedTypeSymbol)constant.Type).EnumUnderlyingType.SpecialType;
ConstantValue valueConstant = ConstantValue.Create(constant.Value, splType);
string typeName = constant.Type.ToDisplayString(SymbolDisplayFormat.QualifiedNameOnlyFormat);
if (valueConstant.IsUnsigned)
{
return DisplayUnsignedEnumConstant(constant, splType, valueConstant.UInt64Value, typeName);
}
else
{
return DisplaySignedEnumConstant(constant, splType, valueConstant.Int64Value, typeName);
}
}
private static string DisplayUnsignedEnumConstant(TypedConstant constant, SpecialType specialType, ulong constantToDecode, string typeName)
{
// Specified valueConstant might have an exact matching enum field
// or it might be a bitwise Or of multiple enum fields.
// For the later case, we keep track of the current value of
// bitwise Or of possible enum fields.
ulong curValue = 0;
// Initialize the value string to empty
PooledStringBuilder pooledBuilder = null;
StringBuilder valueStringBuilder = null;
// Iterate through all the constant members in the enum type
var members = constant.Type.GetMembers();
foreach (var member in members)
{
var field = member as IFieldSymbol;
if ((object)field != null && field.HasConstantValue)
{
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType);
ulong memberValue = memberConstant.UInt64Value;
// Do we have an exact matching enum field
if (memberValue == constantToDecode)
{
if (pooledBuilder != null)
{
pooledBuilder.Free();
}
return typeName + "." + field.Name;
}
// specifiedValue might be a bitwise Or of multiple enum fields
// Is the current member included in the specified value?
if ((memberValue & constantToDecode) == memberValue)
{
// update the current value
curValue = curValue | memberValue;
if (valueStringBuilder == null)
{
pooledBuilder = PooledStringBuilder.GetInstance();
valueStringBuilder = pooledBuilder.Builder;
}
else
{
valueStringBuilder.Append(" | ");
}
valueStringBuilder.Append(typeName);
valueStringBuilder.Append(".");
valueStringBuilder.Append(field.Name);
}
}
}
if (pooledBuilder != null)
{
if (curValue == constantToDecode)
{
// return decoded enum constant
return pooledBuilder.ToStringAndFree();
}
// Unable to decode the enum constant
pooledBuilder.Free();
}
// Unable to decode the enum constant, just display the integral value
return constant.Value.ToString();
}
private static string DisplaySignedEnumConstant(TypedConstant constant, SpecialType specialType, long constantToDecode, string typeName)
{
// Specified valueConstant might have an exact matching enum field
// or it might be a bitwise Or of multiple enum fields.
// For the later case, we keep track of the current value of
// bitwise Or of possible enum fields.
long curValue = 0;
// Initialize the value string to empty
PooledStringBuilder pooledBuilder = null;
StringBuilder valueStringBuilder = null;
// Iterate through all the constant members in the enum type
var members = constant.Type.GetMembers();
foreach (var member in members)
{
var field = member as IFieldSymbol;
if ((object)field != null && field.HasConstantValue)
{
ConstantValue memberConstant = ConstantValue.Create(field.ConstantValue, specialType);
long memberValue = memberConstant.Int64Value;
// Do we have an exact matching enum field
if (memberValue == constantToDecode)
{
if (pooledBuilder != null)
{
pooledBuilder.Free();
}
return typeName + "." + field.Name;
}
// specifiedValue might be a bitwise Or of multiple enum fields
// Is the current member included in the specified value?
if ((memberValue & constantToDecode) == memberValue)
{
// update the current value
curValue = curValue | memberValue;
if (valueStringBuilder == null)
{
pooledBuilder = PooledStringBuilder.GetInstance();
valueStringBuilder = pooledBuilder.Builder;
}
else
{
valueStringBuilder.Append(" | ");
}
valueStringBuilder.Append(typeName);
valueStringBuilder.Append(".");
valueStringBuilder.Append(field.Name);
}
}
}
if (pooledBuilder != null)
{
if (curValue == constantToDecode)
{
// return decoded enum constant
return pooledBuilder.ToStringAndFree();
}
// Unable to decode the enum constant
pooledBuilder.Free();
}
// Unable to decode the enum constant, just display the integral value
return constant.Value.ToString();
}
}
}