1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
21
namespace ICSharpCode.NRefactory.TypeSystem
24
/// Represents some well-known types.
26
public enum KnownTypeCode
28
// Note: DefaultResolvedTypeDefinition uses (KnownTypeCode)-1 as special value for "not yet calculated"
29
// The order of type codes at the beginning must correspond to those in System.TypeCode.
32
/// Not one of the known types.
35
/// <summary><c>object</c> (System.Object)</summary>
37
/// <summary><c>System.DBNull</c></summary>
39
/// <summary><c>bool</c> (System.Boolean)</summary>
41
/// <summary><c>char</c> (System.Char)</summary>
43
/// <summary><c>sbyte</c> (System.SByte)</summary>
45
/// <summary><c>byte</c> (System.Byte)</summary>
47
/// <summary><c>short</c> (System.Int16)</summary>
49
/// <summary><c>ushort</c> (System.UInt16)</summary>
51
/// <summary><c>int</c> (System.Int32)</summary>
53
/// <summary><c>uint</c> (System.UInt32)</summary>
55
/// <summary><c>long</c> (System.Int64)</summary>
57
/// <summary><c>ulong</c> (System.UInt64)</summary>
59
/// <summary><c>float</c> (System.Single)</summary>
61
/// <summary><c>double</c> (System.Double)</summary>
63
/// <summary><c>decimal</c> (System.Decimal)</summary>
65
/// <summary><c>System.DateTime</c></summary>
67
/// <summary><c>string</c> (System.String)</summary>
70
// String was the last element from System.TypeCode, now our additional known types start
72
/// <summary><c>void</c> (System.Void)</summary>
74
/// <summary><c>System.Type</c></summary>
76
/// <summary><c>System.Array</c></summary>
78
/// <summary><c>System.Attribute</c></summary>
80
/// <summary><c>System.ValueType</c></summary>
82
/// <summary><c>System.Enum</c></summary>
84
/// <summary><c>System.Delegate</c></summary>
86
/// <summary><c>System.MulticastDelegate</c></summary>
88
/// <summary><c>System.Exception</c></summary>
90
/// <summary><c>System.IntPtr</c></summary>
92
/// <summary><c>System.UIntPtr</c></summary>
94
/// <summary><c>System.Collections.IEnumerable</c></summary>
96
/// <summary><c>System.Collections.IEnumerator</c></summary>
98
/// <summary><c>System.Collections.Generic.IEnumerable{T}</c></summary>
100
/// <summary><c>System.Collections.Generic.IEnumerator{T}</c></summary>
102
/// <summary><c>System.Collections.Generic.ICollection</c></summary>
104
/// <summary><c>System.Collections.Generic.ICollection{T}</c></summary>
106
/// <summary><c>System.Collections.Generic.IList</c></summary>
108
/// <summary><c>System.Collections.Generic.IList{T}</c></summary>
110
/// <summary><c>System.Collections.Generic.IReadOnlyList{T}</c></summary>
112
/// <summary><c>System.Threading.Tasks.Task</c></summary>
114
/// <summary><c>System.Threading.Tasks.Task{T}</c></summary>
116
/// <summary><c>System.Nullable{T}</c></summary>
118
/// <summary><c>System.IDisposable</c></summary>
120
/// <summary><c>System.Runtime.CompilerServices.INotifyCompletion</c></summary>
122
/// <summary><c>System.Runtime.CompilerServices.ICriticalNotifyCompletion</c></summary>
123
ICriticalNotifyCompletion,
127
/// Contains well-known type references.
130
public sealed class KnownTypeReference : ITypeReference
132
internal const int KnownTypeCodeCount = (int)KnownTypeCode.ICriticalNotifyCompletion + 1;
134
static readonly KnownTypeReference[] knownTypeReferences = new KnownTypeReference[KnownTypeCodeCount] {
136
new KnownTypeReference(KnownTypeCode.Object, "System", "Object", baseType: KnownTypeCode.None),
137
new KnownTypeReference(KnownTypeCode.DBNull, "System", "DBNull"),
138
new KnownTypeReference(KnownTypeCode.Boolean, "System", "Boolean", baseType: KnownTypeCode.ValueType),
139
new KnownTypeReference(KnownTypeCode.Char, "System", "Char", baseType: KnownTypeCode.ValueType),
140
new KnownTypeReference(KnownTypeCode.SByte, "System", "SByte", baseType: KnownTypeCode.ValueType),
141
new KnownTypeReference(KnownTypeCode.Byte, "System", "Byte", baseType: KnownTypeCode.ValueType),
142
new KnownTypeReference(KnownTypeCode.Int16, "System", "Int16", baseType: KnownTypeCode.ValueType),
143
new KnownTypeReference(KnownTypeCode.UInt16, "System", "UInt16", baseType: KnownTypeCode.ValueType),
144
new KnownTypeReference(KnownTypeCode.Int32, "System", "Int32", baseType: KnownTypeCode.ValueType),
145
new KnownTypeReference(KnownTypeCode.UInt32, "System", "UInt32", baseType: KnownTypeCode.ValueType),
146
new KnownTypeReference(KnownTypeCode.Int64, "System", "Int64", baseType: KnownTypeCode.ValueType),
147
new KnownTypeReference(KnownTypeCode.UInt64, "System", "UInt64", baseType: KnownTypeCode.ValueType),
148
new KnownTypeReference(KnownTypeCode.Single, "System", "Single", baseType: KnownTypeCode.ValueType),
149
new KnownTypeReference(KnownTypeCode.Double, "System", "Double", baseType: KnownTypeCode.ValueType),
150
new KnownTypeReference(KnownTypeCode.Decimal, "System", "Decimal", baseType: KnownTypeCode.ValueType),
151
new KnownTypeReference(KnownTypeCode.DateTime, "System", "DateTime", baseType: KnownTypeCode.ValueType),
153
new KnownTypeReference(KnownTypeCode.String, "System", "String"),
154
new KnownTypeReference(KnownTypeCode.Void, "System", "Void"),
155
new KnownTypeReference(KnownTypeCode.Type, "System", "Type"),
156
new KnownTypeReference(KnownTypeCode.Array, "System", "Array"),
157
new KnownTypeReference(KnownTypeCode.Attribute, "System", "Attribute"),
158
new KnownTypeReference(KnownTypeCode.ValueType, "System", "ValueType"),
159
new KnownTypeReference(KnownTypeCode.Enum, "System", "Enum", baseType: KnownTypeCode.ValueType),
160
new KnownTypeReference(KnownTypeCode.Delegate, "System", "Delegate"),
161
new KnownTypeReference(KnownTypeCode.MulticastDelegate, "System", "MulticastDelegate", baseType: KnownTypeCode.Delegate),
162
new KnownTypeReference(KnownTypeCode.Exception, "System", "Exception"),
163
new KnownTypeReference(KnownTypeCode.IntPtr, "System", "IntPtr", baseType: KnownTypeCode.ValueType),
164
new KnownTypeReference(KnownTypeCode.UIntPtr, "System", "UIntPtr", baseType: KnownTypeCode.ValueType),
165
new KnownTypeReference(KnownTypeCode.IEnumerable, "System.Collections", "IEnumerable"),
166
new KnownTypeReference(KnownTypeCode.IEnumerator, "System.Collections", "IEnumerator"),
167
new KnownTypeReference(KnownTypeCode.IEnumerableOfT, "System.Collections.Generic", "IEnumerable", 1),
168
new KnownTypeReference(KnownTypeCode.IEnumeratorOfT, "System.Collections.Generic", "IEnumerator", 1),
169
new KnownTypeReference(KnownTypeCode.ICollection, "System.Collections", "ICollection"),
170
new KnownTypeReference(KnownTypeCode.ICollectionOfT, "System.Collections.Generic", "ICollection", 1),
171
new KnownTypeReference(KnownTypeCode.IList, "System.Collections", "IList"),
172
new KnownTypeReference(KnownTypeCode.IListOfT, "System.Collections.Generic", "IList", 1),
174
new KnownTypeReference(KnownTypeCode.IReadOnlyListOfT, "System.Collections.Generic", "IReadOnlyList", 1),
175
new KnownTypeReference(KnownTypeCode.Task, "System.Threading.Tasks", "Task"),
176
new KnownTypeReference(KnownTypeCode.TaskOfT, "System.Threading.Tasks", "Task", 1, baseType: KnownTypeCode.Task),
177
new KnownTypeReference(KnownTypeCode.NullableOfT, "System", "Nullable", 1, baseType: KnownTypeCode.ValueType),
178
new KnownTypeReference(KnownTypeCode.IDisposable, "System", "IDisposable"),
179
new KnownTypeReference(KnownTypeCode.INotifyCompletion, "System.Runtime.CompilerServices", "INotifyCompletion"),
180
new KnownTypeReference(KnownTypeCode.ICriticalNotifyCompletion, "System.Runtime.CompilerServices", "ICriticalNotifyCompletion"),
184
/// Gets the known type reference for the specified type code.
185
/// Returns null for KnownTypeCode.None.
187
public static KnownTypeReference Get(KnownTypeCode typeCode)
189
return knownTypeReferences[(int)typeCode];
193
/// Gets a type reference pointing to the <c>object</c> type.
195
public static readonly KnownTypeReference Object = Get(KnownTypeCode.Object);
198
/// Gets a type reference pointing to the <c>System.DBNull</c> type.
200
public static readonly KnownTypeReference DBNull = Get(KnownTypeCode.DBNull);
203
/// Gets a type reference pointing to the <c>bool</c> type.
205
public static readonly KnownTypeReference Boolean = Get(KnownTypeCode.Boolean);
208
/// Gets a type reference pointing to the <c>char</c> type.
210
public static readonly KnownTypeReference Char = Get(KnownTypeCode.Char);
213
/// Gets a type reference pointing to the <c>sbyte</c> type.
215
public static readonly KnownTypeReference SByte = Get(KnownTypeCode.SByte);
218
/// Gets a type reference pointing to the <c>byte</c> type.
220
public static readonly KnownTypeReference Byte = Get(KnownTypeCode.Byte);
223
/// Gets a type reference pointing to the <c>short</c> type.
225
public static readonly KnownTypeReference Int16 = Get(KnownTypeCode.Int16);
228
/// Gets a type reference pointing to the <c>ushort</c> type.
230
public static readonly KnownTypeReference UInt16 = Get(KnownTypeCode.UInt16);
233
/// Gets a type reference pointing to the <c>int</c> type.
235
public static readonly KnownTypeReference Int32 = Get(KnownTypeCode.Int32);
238
/// Gets a type reference pointing to the <c>uint</c> type.
240
public static readonly KnownTypeReference UInt32 = Get(KnownTypeCode.UInt32);
243
/// Gets a type reference pointing to the <c>long</c> type.
245
public static readonly KnownTypeReference Int64 = Get(KnownTypeCode.Int64);
248
/// Gets a type reference pointing to the <c>ulong</c> type.
250
public static readonly KnownTypeReference UInt64 = Get(KnownTypeCode.UInt64);
253
/// Gets a type reference pointing to the <c>float</c> type.
255
public static readonly KnownTypeReference Single = Get(KnownTypeCode.Single);
258
/// Gets a type reference pointing to the <c>double</c> type.
260
public static readonly KnownTypeReference Double = Get(KnownTypeCode.Double);
263
/// Gets a type reference pointing to the <c>decimal</c> type.
265
public static readonly KnownTypeReference Decimal = Get(KnownTypeCode.Decimal);
268
/// Gets a type reference pointing to the <c>System.DateTime</c> type.
270
public static readonly KnownTypeReference DateTime = Get(KnownTypeCode.DateTime);
273
/// Gets a type reference pointing to the <c>string</c> type.
275
public static readonly KnownTypeReference String = Get(KnownTypeCode.String);
278
/// Gets a type reference pointing to the <c>void</c> type.
280
public static readonly KnownTypeReference Void = Get(KnownTypeCode.Void);
283
/// Gets a type reference pointing to the <c>System.Type</c> type.
285
public static readonly KnownTypeReference Type = Get(KnownTypeCode.Type);
288
/// Gets a type reference pointing to the <c>System.Array</c> type.
290
public static readonly KnownTypeReference Array = Get(KnownTypeCode.Array);
293
/// Gets a type reference pointing to the <c>System.Attribute</c> type.
295
public static readonly KnownTypeReference Attribute = Get(KnownTypeCode.Attribute);
298
/// Gets a type reference pointing to the <c>System.ValueType</c> type.
300
public static readonly KnownTypeReference ValueType = Get(KnownTypeCode.ValueType);
303
/// Gets a type reference pointing to the <c>System.Enum</c> type.
305
public static readonly KnownTypeReference Enum = Get(KnownTypeCode.Enum);
308
/// Gets a type reference pointing to the <c>System.Delegate</c> type.
310
public static readonly KnownTypeReference Delegate = Get(KnownTypeCode.Delegate);
313
/// Gets a type reference pointing to the <c>System.MulticastDelegate</c> type.
315
public static readonly KnownTypeReference MulticastDelegate = Get(KnownTypeCode.MulticastDelegate);
318
/// Gets a type reference pointing to the <c>System.Exception</c> type.
320
public static readonly KnownTypeReference Exception = Get(KnownTypeCode.Exception);
323
/// Gets a type reference pointing to the <c>System.IntPtr</c> type.
325
public static readonly KnownTypeReference IntPtr = Get(KnownTypeCode.IntPtr);
328
/// Gets a type reference pointing to the <c>System.UIntPtr</c> type.
330
public static readonly KnownTypeReference UIntPtr = Get(KnownTypeCode.UIntPtr);
333
/// Gets a type reference pointing to the <c>System.Collections.IEnumerable</c> type.
335
public static readonly KnownTypeReference IEnumerable = Get(KnownTypeCode.IEnumerable);
338
/// Gets a type reference pointing to the <c>System.Collections.IEnumerator</c> type.
340
public static readonly KnownTypeReference IEnumerator = Get(KnownTypeCode.IEnumerator);
343
/// Gets a type reference pointing to the <c>System.Collections.Generic.IEnumerable{T}</c> type.
345
public static readonly KnownTypeReference IEnumerableOfT = Get(KnownTypeCode.IEnumerableOfT);
348
/// Gets a type reference pointing to the <c>System.Collections.Generic.IEnumerator{T}</c> type.
350
public static readonly KnownTypeReference IEnumeratorOfT = Get(KnownTypeCode.IEnumeratorOfT);
353
/// Gets a type reference pointing to the <c>System.Collections.ICollection</c> type.
355
public static readonly KnownTypeReference ICollection = Get(KnownTypeCode.ICollection);
358
/// Gets a type reference pointing to the <c>System.Collections.Generic.ICollection{T}</c> type.
360
public static readonly KnownTypeReference ICollectionOfT = Get(KnownTypeCode.ICollectionOfT);
363
/// Gets a type reference pointing to the <c>System.Collections.IList</c> type.
365
public static readonly KnownTypeReference IList = Get(KnownTypeCode.IList);
368
/// Gets a type reference pointing to the <c>System.Collections.Generic.IList{T}</c> type.
370
public static readonly KnownTypeReference IListOfT = Get(KnownTypeCode.IListOfT);
373
/// Gets a type reference pointing to the <c>System.Collections.Generic.IReadOnlyList{T}</c> type.
375
public static readonly KnownTypeReference IReadOnlyListOfT = Get(KnownTypeCode.IReadOnlyListOfT);
378
/// Gets a type reference pointing to the <c>System.Threading.Tasks.Task</c> type.
380
public static readonly KnownTypeReference Task = Get(KnownTypeCode.Task);
383
/// Gets a type reference pointing to the <c>System.Threading.Tasks.Task{T}</c> type.
385
public static readonly KnownTypeReference TaskOfT = Get(KnownTypeCode.TaskOfT);
388
/// Gets a type reference pointing to the <c>System.Nullable{T}</c> type.
390
public static readonly KnownTypeReference NullableOfT = Get(KnownTypeCode.NullableOfT);
393
/// Gets a type reference pointing to the <c>System.IDisposable</c> type.
395
public static readonly KnownTypeReference IDisposable = Get(KnownTypeCode.IDisposable);
398
/// Gets a type reference pointing to the <c>System.Runtime.CompilerServices.INotifyCompletion</c> type.
400
public static readonly KnownTypeReference INotifyCompletion = Get(KnownTypeCode.INotifyCompletion);
403
/// Gets a type reference pointing to the <c>System.Runtime.CompilerServices.ICriticalNotifyCompletion</c> type.
405
public static readonly KnownTypeReference ICriticalNotifyCompletion = Get(KnownTypeCode.ICriticalNotifyCompletion);
407
readonly KnownTypeCode knownTypeCode;
408
readonly string namespaceName;
409
readonly string name;
410
readonly int typeParameterCount;
411
internal readonly KnownTypeCode baseType;
413
private KnownTypeReference(KnownTypeCode knownTypeCode, string namespaceName, string name, int typeParameterCount = 0, KnownTypeCode baseType = KnownTypeCode.Object)
415
this.knownTypeCode = knownTypeCode;
416
this.namespaceName = namespaceName;
418
this.typeParameterCount = typeParameterCount;
419
this.baseType = baseType;
422
public KnownTypeCode KnownTypeCode {
423
get { return knownTypeCode; }
426
public string Namespace {
427
get { return namespaceName; }
434
public int TypeParameterCount {
435
get { return typeParameterCount; }
438
public IType Resolve(ITypeResolveContext context)
440
return context.Compilation.FindType(knownTypeCode);
443
public override string ToString()
445
return GetCSharpNameByTypeCode(knownTypeCode) ?? (this.Namespace + "." + this.Name);
449
/// Gets the C# primitive type name from the known type code.
450
/// Returns null if there is no primitive name for the specified type.
452
public static string GetCSharpNameByTypeCode(KnownTypeCode knownTypeCode)
454
switch (knownTypeCode) {
455
case KnownTypeCode.Object:
457
case KnownTypeCode.Boolean:
459
case KnownTypeCode.Char:
461
case KnownTypeCode.SByte:
463
case KnownTypeCode.Byte:
465
case KnownTypeCode.Int16:
467
case KnownTypeCode.UInt16:
469
case KnownTypeCode.Int32:
471
case KnownTypeCode.UInt32:
473
case KnownTypeCode.Int64:
475
case KnownTypeCode.UInt64:
477
case KnownTypeCode.Single:
479
case KnownTypeCode.Double:
481
case KnownTypeCode.Decimal:
483
case KnownTypeCode.String:
485
case KnownTypeCode.Void: