-
Notifications
You must be signed in to change notification settings - Fork 4k
/
TypedConstant.vb
483 lines (390 loc) · 20.5 KB
/
TypedConstant.vb
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
' 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.
Imports System
Imports System.Collections.Generic
Imports System.Collections.Immutable
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Threading
Imports Microsoft.CodeAnalysis.CodeGen
Imports Microsoft.CodeAnalysis.Collections
Imports Microsoft.CodeAnalysis.PooledObjects
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports TypeKind = Microsoft.CodeAnalysis.TypeKind
Namespace Microsoft.CodeAnalysis.VisualBasic
Public Module TypedConstantExtensions
''' <summary>
''' Returns the System.String that represents the current TypedConstant.
''' </summary>
''' <returns>A System.String that represents the current TypedConstant.</returns>
<Extension>
Public Function ToVisualBasicString(constant As TypedConstant) As String
If constant.IsNull Then
Return "Nothing"
End If
If constant.Kind = TypedConstantKind.Array Then
Return "{" & String.Join(", ", constant.Values.Select(Function(v) v.ToVisualBasicString())) & "}"
End If
If constant.Kind = TypedConstantKind.Type OrElse constant.TypeInternal.SpecialType = SpecialType.System_Object Then
Return "GetType(" & constant.Value.ToString() & ")"
End If
If constant.Kind = TypedConstantKind.Enum Then
' TODO (tomat): use SymbolDisplay instead
Return DisplayEnumConstant(constant)
End If
Return SymbolDisplay.FormatPrimitive(constant.ValueInternal, quoteStrings:=True, useHexadecimalNumbers:=False)
End Function
' Decode the value of enum constant
Private Function DisplayEnumConstant(constant As TypedConstant) As String
Debug.Assert(constant.Kind = TypedConstantKind.Enum)
' Create a ConstantValue of enum underlying type
Dim splType As SpecialType = DirectCast(constant.TypeInternal, NamedTypeSymbol).EnumUnderlyingType.SpecialType
Dim valueConstant As ConstantValue = ConstantValue.Create(constant.ValueInternal, splType)
Dim typeName As String = constant.Type.ToDisplayString(SymbolDisplayFormat.QualifiedNameOnlyFormat)
If valueConstant.IsUnsigned Then
Return DisplayUnsignedEnumConstant(constant, splType, valueConstant.UInt64Value, typeName)
Else
Return DisplaySignedEnumConstant(constant, splType, valueConstant.Int64Value, typeName)
End If
End Function
Private Function DisplayUnsignedEnumConstant(constant As TypedConstant, splType As SpecialType, ByVal constantToDecode As ULong, ByVal typeName As String) As String
' 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.
Dim curValue As ULong = 0
' Initialize the value string to empty
Dim pooledBuilder As PooledStringBuilder = Nothing
Dim valueStringBuilder As StringBuilder = Nothing
' Iterate through all the constant members in the enum type
Dim members = constant.Type.GetMembers()
For Each member In members
Dim field = TryCast(member, IFieldSymbol)
If field IsNot Nothing AndAlso field.HasConstantValue Then
Dim memberConstant = ConstantValue.Create(field.ConstantValue, splType)
Dim memberValue = memberConstant.UInt64Value
' Do we have an exact matching enum field
If memberValue = constantToDecode Then
If Not pooledBuilder Is Nothing Then
pooledBuilder.Free()
End If
Return typeName & "." & field.Name
End If
' specifiedValue might be a bitwise Or of multiple enum fields
' Is the current member included in the specified value?
If (memberValue And constantToDecode) = memberValue Then
' update the current value
curValue = curValue Or memberValue
If valueStringBuilder Is Nothing Then
pooledBuilder = PooledStringBuilder.GetInstance()
valueStringBuilder = pooledBuilder.Builder
Else
valueStringBuilder.Append(" Or ")
End If
valueStringBuilder.Append(typeName)
valueStringBuilder.Append(".")
valueStringBuilder.Append(field.Name)
End If
End If
Next
If Not pooledBuilder Is Nothing Then
If curValue = constantToDecode Then
' return decoded enum constant
Return pooledBuilder.ToStringAndFree()
End If
' Unable to decode the enum constant
pooledBuilder.Free()
End If
' Unable to decode the enum constant, just display the integral value
Return constant.ValueInternal.ToString()
End Function
Private Function DisplaySignedEnumConstant(constant As TypedConstant, ByVal splType As SpecialType, ByVal constantToDecode As Long, ByVal typeName As String) As String
' 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.
Dim curValue As Long = 0
' Initialize the value string to empty
Dim pooledBuilder As PooledStringBuilder = Nothing
Dim valueStringBuilder As StringBuilder = Nothing
' Iterate through all the constant members in the enum type
Dim members = constant.Type.GetMembers()
For Each member In members
Dim field = TryCast(member, IFieldSymbol)
If field IsNot Nothing AndAlso field.HasConstantValue Then
Dim memberConstant = ConstantValue.Create(field.ConstantValue, splType)
Dim memberValue = memberConstant.Int64Value
' Do we have an exact matching enum field
If memberValue = constantToDecode Then
If Not pooledBuilder Is Nothing Then
pooledBuilder.Free()
End If
Return typeName & "." & field.Name
End If
' specifiedValue might be a bitwise Or of multiple enum fields
' Is the current member included in the specified value?
If (memberValue And constantToDecode) = memberValue Then
' update the current value
curValue = curValue Or memberValue
If valueStringBuilder Is Nothing Then
pooledBuilder = PooledStringBuilder.GetInstance()
valueStringBuilder = pooledBuilder.Builder
Else
valueStringBuilder.Append(" Or ")
End If
valueStringBuilder.Append(typeName)
valueStringBuilder.Append(".")
valueStringBuilder.Append(field.Name)
End If
End If
Next
If Not pooledBuilder Is Nothing Then
If curValue = constantToDecode Then
' return decoded enum constant
Return pooledBuilder.ToStringAndFree()
End If
' Unable to decode the enum constant
pooledBuilder.Free()
End If
' Unable to decode the enum constant, just display the integral value
Return constant.ValueInternal.ToString()
End Function
End Module
#If False Then
''' <summary>
''' TypedConstant represents a constant value used as an argument to an Attribute. The Typed constant can represent
''' a primitive type, an enum type, a system.type or an array of TypedConstants.
'''
''' Kind _value _type
''' Primitive Boxed value, string or nothing TypeSymbol for the boxed value possibly nothing for nothing literal
''' Enum Boxed value of the underlying type TypeSymbol for enum
''' Type TypeSymbol or nothing TypeSymbol for System.Type
''' Array TypeConstant() or nothing ArrayTypeSymbol
''' Error nothing TypeSymbol or ErrorTypeSymbol
'''
''' </summary>
Public Structure TypedConstant
Private ReadOnly _kind As TypedConstantKind
Private ReadOnly _type As TypeSymbol
Private ReadOnly _value As TypedConstantValue
Private Sub New(type As TypeSymbol, kind As TypedConstantKind, value As TypedConstantValue)
Debug.Assert(type IsNot Nothing OrElse kind = TypedConstantKind.Error AndAlso value.IsNull)
_value = value
_kind = kind
_type = type
End Sub
Friend Sub New(constant As TypedConstant)
MyClass.New(DirectCast(constant.Type, TypeSymbol), constant.Kind, constant.RawValue)
End Sub
Friend Sub New(type As TypeSymbol, array As ImmutableArray(Of TypedConstant))
MyClass.New(type, TypedConstantKind.Array, array)
End Sub
Friend Sub New(type As TypeSymbol, kind As TypedConstantKind, value As Object)
MyClass.New(type, kind, New TypedConstantValue(value))
Debug.Assert(kind <> TypedConstantKind.Array)
End Sub
Public Shared Widening Operator CType(constant As TypedConstant) As TypedConstant
Return New TypedConstant(constant._type, constant._kind, constant._value)
End Operator
Public Shared Narrowing Operator CType(constant As TypedConstant) As TypedConstant
Return New TypedConstant(constant)
End Operator
''' <summary>
''' The TypedConstant's kind. Can be one of Primitive, Enum, Type, Array or Error
''' </summary>
Public ReadOnly Property Kind As TypedConstantKind
Get
Return _kind
End Get
End Property
''' <summary>
''' True if the constant represents a null literal.
''' </summary>
Public ReadOnly Property IsNull As Boolean
Get
Return _value.IsNull
End Get
End Property
''' <summary>
''' Returns the value of a non-array <see cref="TypedConstant"/>,
''' for primitive constants a boxed value, string, or Nothing,
''' for enum constants a boxed value of the underlying type,
''' for type constants a <see cref="TypeSymbol"/>.
''' </summary>
''' <exception cref="InvalidOperationException">Constant represents an array, use <see cref="Values"/> to read its value.</exception>
Public ReadOnly Property Value As Object
Get
If _kind = TypedConstantKind.Array Then
Throw New InvalidOperationException("TypedConstant is an array. Use Values property.")
End If
Return _value.Object
End Get
End Property
''' <summary>
''' Returns the value for array constants.
''' </summary>
''' <exception cref="InvalidOperationException">Constant represents an array, use <see cref="Values"/> to read its value.</exception>
Public ReadOnly Property Values As IEnumerable(Of TypedConstant)
Get
If _kind <> TypedConstantKind.Array Then
Throw New InvalidOperationException("TypedConstant is not an array. Use Value property.")
End If
If IsNull Then
Return Nothing
End If
Return _value.Array.Select(Function(constant) New TypedConstant(constant))
End Get
End Property
''' <summary>
''' The TypedConstant's type. This is either a TypeSymbol of one of the primitive types, a TypeSymbol of an enum
''' type, a TypeSymbol for System.Type or an ArrayTypeSymbol.
''' </summary>
Public ReadOnly Property Type As TypeSymbol
Get
Return _type
End Get
End Property
#Region "Testing & Debugging"
''' <summary>
''' Returns the System.String that represents the current TypedConstant.
''' </summary>
''' <returns>A System.String that represents the current TypedConstant.</returns>
Public Overrides Function ToString() As String
If _value.IsNull Then
Return "Nothing"
End If
If _kind = TypedConstantKind.Array Then
Return "{" & String.Join(", ", _value.Array.Select(Function(v) New TypedConstant(v).ToString())) & "}"
End If
If _kind = TypedConstantKind.Type OrElse _type.SpecialType = SpecialType.System_Object Then
Return "GetType(" & _value.Object.ToString() & ")"
End If
If _kind = TypedConstantKind.Enum Then
' TODO (tomat): use SymbolDisplay instead
Return DisplayEnumConstant()
End If
Return SymbolDisplay.FormatPrimitive(_value.Object, quoteStrings:=True, useHexadecimalNumbers:=False)
End Function
' Decode the value of enum constant
Private Function DisplayEnumConstant() As String
Debug.Assert(Kind = TypedConstantKind.Enum)
' Create a ConstantValue of enum underlying type
Dim splType As SpecialType = Me.Type.GetEnumUnderlyingTypeOrSelf().SpecialType
Dim valueConstant As ConstantValue = ConstantValue.Create(Me.Value, splType)
Dim typeName As String = Me.Type.ToDisplayString(SymbolDisplayFormat.QualifiedNameOnlyFormat)
If valueConstant.IsUnsigned Then
Return DisplayUnsignedEnumConstant(splType, valueConstant.UInt64Value, typeName)
Else
Return DisplaySignedEnumConstant(splType, valueConstant.Int64Value, typeName)
End If
End Function
Private Function DisplayUnsignedEnumConstant(ByVal splType As SpecialType, ByVal constantToDecode As ULong, ByVal typeName As String) As String
' 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.
Dim curValue As ULong = 0
' Initialize the value string to empty
Dim pooledBuilder As PooledStringBuilder = Nothing
Dim valueStringBuilder As StringBuilder = Nothing
' Iterate through all the constant members in the enum type
Dim members As ImmutableArray(Of Symbol) = Me.Type.GetMembers()
For Each member As Symbol In members
Dim field = TryCast(member, FieldSymbol)
If field IsNot Nothing AndAlso field.HasConstantValue Then
Dim memberConstant = ConstantValue.Create(field.ConstantValue, splType)
Dim memberValue = memberConstant.UInt64Value
' Do we have an exact matching enum field
If memberValue = constantToDecode Then
If Not pooledBuilder Is Nothing Then
pooledBuilder.Free()
End If
Return typeName & "." & field.Name
End If
' specifiedValue might be a bitwise Or of multiple enum fields
' Is the current member included in the specified value?
If (memberValue And constantToDecode) = memberValue Then
' update the current value
curValue = curValue Or memberValue
If valueStringBuilder Is Nothing Then
pooledBuilder = PooledStringBuilder.GetInstance()
valueStringBuilder = pooledBuilder.Builder
Else
valueStringBuilder.Append(" Or ")
End If
valueStringBuilder.Append(typeName)
valueStringBuilder.Append(".")
valueStringBuilder.Append(field.Name)
End If
End If
Next
If Not pooledBuilder Is Nothing Then
If curValue = constantToDecode Then
' return decoded enum constant
Return pooledBuilder.ToStringAndFree()
End If
' Unable to decode the enum constant
pooledBuilder.Free()
End If
' Unable to decode the enum constant, just display the integral value
Return Value.ToString()
End Function
Private Function DisplaySignedEnumConstant(ByVal splType As SpecialType, ByVal constantToDecode As Long, ByVal typeName As String) As String
' 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.
Dim curValue As Long = 0
' Initialize the value string to empty
Dim pooledBuilder As PooledStringBuilder = Nothing
Dim valueStringBuilder As StringBuilder = Nothing
' Iterate through all the constant members in the enum type
Dim members As ImmutableArray(Of Symbol) = Me.Type.GetMembers()
For Each member As Symbol In members
Dim field = TryCast(member, FieldSymbol)
If field IsNot Nothing AndAlso field.HasConstantValue Then
Dim memberConstant = ConstantValue.Create(field.ConstantValue, splType)
Dim memberValue = memberConstant.Int64Value
' Do we have an exact matching enum field
If memberValue = constantToDecode Then
If Not pooledBuilder Is Nothing Then
pooledBuilder.Free()
End If
Return typeName & "." & field.Name
End If
' specifiedValue might be a bitwise Or of multiple enum fields
' Is the current member included in the specified value?
If (memberValue And constantToDecode) = memberValue Then
' update the current value
curValue = curValue Or memberValue
If valueStringBuilder Is Nothing Then
pooledBuilder = PooledStringBuilder.GetInstance()
valueStringBuilder = pooledBuilder.Builder
Else
valueStringBuilder.Append(" Or ")
End If
valueStringBuilder.Append(typeName)
valueStringBuilder.Append(".")
valueStringBuilder.Append(field.Name)
End If
End If
Next
If Not pooledBuilder Is Nothing Then
If curValue = constantToDecode Then
' return decoded enum constant
Return pooledBuilder.ToStringAndFree()
End If
' Unable to decode the enum constant
pooledBuilder.Free()
End If
' Unable to decode the enum constant, just display the integral value
Return Value.ToString()
End Function
#End Region
End Structure
#End If
End Namespace