// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
namespace ICSharpCode.NRefactory.TypeSystem
{
///
/// Represents some well-known types.
///
public enum KnownTypeCode
{
// Note: DefaultResolvedTypeDefinition uses (KnownTypeCode)-1 as special value for "not yet calculated".
// The order of type codes at the beginning must correspond to those in System.TypeCode.
///
/// Not one of the known types.
///
None,
/// object (System.Object)
Object,
/// System.DBNull
DBNull,
/// bool (System.Boolean)
Boolean,
/// char (System.Char)
Char,
/// sbyte (System.SByte)
SByte,
/// byte (System.Byte)
Byte,
/// short (System.Int16)
Int16,
/// ushort (System.UInt16)
UInt16,
/// int (System.Int32)
Int32,
/// uint (System.UInt32)
UInt32,
/// long (System.Int64)
Int64,
/// ulong (System.UInt64)
UInt64,
/// float (System.Single)
Single,
/// double (System.Double)
Double,
/// decimal (System.Decimal)
Decimal,
/// System.DateTime
DateTime,
/// string (System.String)
String = 18,
// String was the last element from System.TypeCode, now our additional known types start
/// void (System.Void)
Void,
/// System.Type
Type,
/// System.Array
Array,
/// System.Attribute
Attribute,
/// System.ValueType
ValueType,
/// System.Enum
Enum,
/// System.Delegate
Delegate,
/// System.MulticastDelegate
MulticastDelegate,
/// System.Exception
Exception,
/// System.IntPtr
IntPtr,
/// System.UIntPtr
UIntPtr,
/// System.Collections.IEnumerable
IEnumerable,
/// System.Collections.IEnumerator
IEnumerator,
/// System.Collections.Generic.IEnumerable{T}
IEnumerableOfT,
/// System.Collections.Generic.IEnumerator{T}
IEnumeratorOfT,
/// System.Collections.Generic.ICollection
ICollection,
/// System.Collections.Generic.ICollection{T}
ICollectionOfT,
/// System.Collections.Generic.IList
IList,
/// System.Collections.Generic.IList{T}
IListOfT,
/// System.Collections.Generic.IReadOnlyCollection{T}
IReadOnlyCollectionOfT,
/// System.Collections.Generic.IReadOnlyList{T}
IReadOnlyListOfT,
/// System.Threading.Tasks.Task
Task,
/// System.Threading.Tasks.Task{T}
TaskOfT,
/// System.Nullable{T}
NullableOfT,
/// System.IDisposable
IDisposable,
/// System.Runtime.CompilerServices.INotifyCompletion
INotifyCompletion,
/// System.Runtime.CompilerServices.ICriticalNotifyCompletion
ICriticalNotifyCompletion,
}
///
/// Contains well-known type references.
///
[Serializable]
public sealed class KnownTypeReference : ITypeReference
{
internal const int KnownTypeCodeCount = (int)KnownTypeCode.ICriticalNotifyCompletion + 1;
static readonly KnownTypeReference[] knownTypeReferences = new KnownTypeReference[KnownTypeCodeCount] {
null, // None
new KnownTypeReference(KnownTypeCode.Object, "System", "Object", baseType: KnownTypeCode.None),
new KnownTypeReference(KnownTypeCode.DBNull, "System", "DBNull"),
new KnownTypeReference(KnownTypeCode.Boolean, "System", "Boolean", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Char, "System", "Char", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.SByte, "System", "SByte", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Byte, "System", "Byte", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Int16, "System", "Int16", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.UInt16, "System", "UInt16", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Int32, "System", "Int32", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.UInt32, "System", "UInt32", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Int64, "System", "Int64", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.UInt64, "System", "UInt64", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Single, "System", "Single", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Double, "System", "Double", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Decimal, "System", "Decimal", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.DateTime, "System", "DateTime", baseType: KnownTypeCode.ValueType),
null,
new KnownTypeReference(KnownTypeCode.String, "System", "String"),
new KnownTypeReference(KnownTypeCode.Void, "System", "Void"),
new KnownTypeReference(KnownTypeCode.Type, "System", "Type"),
new KnownTypeReference(KnownTypeCode.Array, "System", "Array"),
new KnownTypeReference(KnownTypeCode.Attribute, "System", "Attribute"),
new KnownTypeReference(KnownTypeCode.ValueType, "System", "ValueType"),
new KnownTypeReference(KnownTypeCode.Enum, "System", "Enum", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.Delegate, "System", "Delegate"),
new KnownTypeReference(KnownTypeCode.MulticastDelegate, "System", "MulticastDelegate", baseType: KnownTypeCode.Delegate),
new KnownTypeReference(KnownTypeCode.Exception, "System", "Exception"),
new KnownTypeReference(KnownTypeCode.IntPtr, "System", "IntPtr", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.UIntPtr, "System", "UIntPtr", baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.IEnumerable, "System.Collections", "IEnumerable"),
new KnownTypeReference(KnownTypeCode.IEnumerator, "System.Collections", "IEnumerator"),
new KnownTypeReference(KnownTypeCode.IEnumerableOfT, "System.Collections.Generic", "IEnumerable", 1),
new KnownTypeReference(KnownTypeCode.IEnumeratorOfT, "System.Collections.Generic", "IEnumerator", 1),
new KnownTypeReference(KnownTypeCode.ICollection, "System.Collections", "ICollection"),
new KnownTypeReference(KnownTypeCode.ICollectionOfT, "System.Collections.Generic", "ICollection", 1),
new KnownTypeReference(KnownTypeCode.IList, "System.Collections", "IList"),
new KnownTypeReference(KnownTypeCode.IListOfT, "System.Collections.Generic", "IList", 1),
new KnownTypeReference(KnownTypeCode.IReadOnlyCollectionOfT, "System.Collections.Generic", "IReadOnlyCollection", 1),
new KnownTypeReference(KnownTypeCode.IReadOnlyListOfT, "System.Collections.Generic", "IReadOnlyList", 1),
new KnownTypeReference(KnownTypeCode.Task, "System.Threading.Tasks", "Task"),
new KnownTypeReference(KnownTypeCode.TaskOfT, "System.Threading.Tasks", "Task", 1, baseType: KnownTypeCode.Task),
new KnownTypeReference(KnownTypeCode.NullableOfT, "System", "Nullable", 1, baseType: KnownTypeCode.ValueType),
new KnownTypeReference(KnownTypeCode.IDisposable, "System", "IDisposable"),
new KnownTypeReference(KnownTypeCode.INotifyCompletion, "System.Runtime.CompilerServices", "INotifyCompletion"),
new KnownTypeReference(KnownTypeCode.ICriticalNotifyCompletion, "System.Runtime.CompilerServices", "ICriticalNotifyCompletion"),
};
///
/// Gets the known type reference for the specified type code.
/// Returns null for KnownTypeCode.None.
///
public static KnownTypeReference Get(KnownTypeCode typeCode)
{
return knownTypeReferences[(int)typeCode];
}
///
/// Gets a type reference pointing to the object type.
///
public static readonly KnownTypeReference Object = Get(KnownTypeCode.Object);
///
/// Gets a type reference pointing to the System.DBNull type.
///
public static readonly KnownTypeReference DBNull = Get(KnownTypeCode.DBNull);
///
/// Gets a type reference pointing to the bool type.
///
public static readonly KnownTypeReference Boolean = Get(KnownTypeCode.Boolean);
///
/// Gets a type reference pointing to the char type.
///
public static readonly KnownTypeReference Char = Get(KnownTypeCode.Char);
///
/// Gets a type reference pointing to the sbyte type.
///
public static readonly KnownTypeReference SByte = Get(KnownTypeCode.SByte);
///
/// Gets a type reference pointing to the byte type.
///
public static readonly KnownTypeReference Byte = Get(KnownTypeCode.Byte);
///
/// Gets a type reference pointing to the short type.
///
public static readonly KnownTypeReference Int16 = Get(KnownTypeCode.Int16);
///
/// Gets a type reference pointing to the ushort type.
///
public static readonly KnownTypeReference UInt16 = Get(KnownTypeCode.UInt16);
///
/// Gets a type reference pointing to the int type.
///
public static readonly KnownTypeReference Int32 = Get(KnownTypeCode.Int32);
///
/// Gets a type reference pointing to the uint type.
///
public static readonly KnownTypeReference UInt32 = Get(KnownTypeCode.UInt32);
///
/// Gets a type reference pointing to the long type.
///
public static readonly KnownTypeReference Int64 = Get(KnownTypeCode.Int64);
///
/// Gets a type reference pointing to the ulong type.
///
public static readonly KnownTypeReference UInt64 = Get(KnownTypeCode.UInt64);
///
/// Gets a type reference pointing to the float type.
///
public static readonly KnownTypeReference Single = Get(KnownTypeCode.Single);
///
/// Gets a type reference pointing to the double type.
///
public static readonly KnownTypeReference Double = Get(KnownTypeCode.Double);
///
/// Gets a type reference pointing to the decimal type.
///
public static readonly KnownTypeReference Decimal = Get(KnownTypeCode.Decimal);
///
/// Gets a type reference pointing to the System.DateTime type.
///
public static readonly KnownTypeReference DateTime = Get(KnownTypeCode.DateTime);
///
/// Gets a type reference pointing to the string type.
///
public static readonly KnownTypeReference String = Get(KnownTypeCode.String);
///
/// Gets a type reference pointing to the void type.
///
public static readonly KnownTypeReference Void = Get(KnownTypeCode.Void);
///
/// Gets a type reference pointing to the System.Type type.
///
public static readonly KnownTypeReference Type = Get(KnownTypeCode.Type);
///
/// Gets a type reference pointing to the System.Array type.
///
public static readonly KnownTypeReference Array = Get(KnownTypeCode.Array);
///
/// Gets a type reference pointing to the System.Attribute type.
///
public static readonly KnownTypeReference Attribute = Get(KnownTypeCode.Attribute);
///
/// Gets a type reference pointing to the System.ValueType type.
///
public static readonly KnownTypeReference ValueType = Get(KnownTypeCode.ValueType);
///
/// Gets a type reference pointing to the System.Enum type.
///
public static readonly KnownTypeReference Enum = Get(KnownTypeCode.Enum);
///
/// Gets a type reference pointing to the System.Delegate type.
///
public static readonly KnownTypeReference Delegate = Get(KnownTypeCode.Delegate);
///
/// Gets a type reference pointing to the System.MulticastDelegate type.
///
public static readonly KnownTypeReference MulticastDelegate = Get(KnownTypeCode.MulticastDelegate);
///
/// Gets a type reference pointing to the System.Exception type.
///
public static readonly KnownTypeReference Exception = Get(KnownTypeCode.Exception);
///
/// Gets a type reference pointing to the System.IntPtr type.
///
public static readonly KnownTypeReference IntPtr = Get(KnownTypeCode.IntPtr);
///
/// Gets a type reference pointing to the System.UIntPtr type.
///
public static readonly KnownTypeReference UIntPtr = Get(KnownTypeCode.UIntPtr);
///
/// Gets a type reference pointing to the System.Collections.IEnumerable type.
///
public static readonly KnownTypeReference IEnumerable = Get(KnownTypeCode.IEnumerable);
///
/// Gets a type reference pointing to the System.Collections.IEnumerator type.
///
public static readonly KnownTypeReference IEnumerator = Get(KnownTypeCode.IEnumerator);
///
/// Gets a type reference pointing to the System.Collections.Generic.IEnumerable{T} type.
///
public static readonly KnownTypeReference IEnumerableOfT = Get(KnownTypeCode.IEnumerableOfT);
///
/// Gets a type reference pointing to the System.Collections.Generic.IEnumerator{T} type.
///
public static readonly KnownTypeReference IEnumeratorOfT = Get(KnownTypeCode.IEnumeratorOfT);
///
/// Gets a type reference pointing to the System.Collections.ICollection type.
///
public static readonly KnownTypeReference ICollection = Get(KnownTypeCode.ICollection);
///
/// Gets a type reference pointing to the System.Collections.Generic.ICollection{T} type.
///
public static readonly KnownTypeReference ICollectionOfT = Get(KnownTypeCode.ICollectionOfT);
///
/// Gets a type reference pointing to the System.Collections.IList type.
///
public static readonly KnownTypeReference IList = Get(KnownTypeCode.IList);
///
/// Gets a type reference pointing to the System.Collections.Generic.IList{T} type.
///
public static readonly KnownTypeReference IListOfT = Get(KnownTypeCode.IListOfT);
///
/// Gets a type reference pointing to the System.Collections.Generic.IReadOnlyCollection{T} type.
///
public static readonly KnownTypeReference IReadOnlyCollectionOfT = Get(KnownTypeCode.IReadOnlyCollectionOfT);
///
/// Gets a type reference pointing to the System.Collections.Generic.IReadOnlyList{T} type.
///
public static readonly KnownTypeReference IReadOnlyListOfT = Get(KnownTypeCode.IReadOnlyListOfT);
///
/// Gets a type reference pointing to the System.Threading.Tasks.Task type.
///
public static readonly KnownTypeReference Task = Get(KnownTypeCode.Task);
///
/// Gets a type reference pointing to the System.Threading.Tasks.Task{T} type.
///
public static readonly KnownTypeReference TaskOfT = Get(KnownTypeCode.TaskOfT);
///
/// Gets a type reference pointing to the System.Nullable{T} type.
///
public static readonly KnownTypeReference NullableOfT = Get(KnownTypeCode.NullableOfT);
///
/// Gets a type reference pointing to the System.IDisposable type.
///
public static readonly KnownTypeReference IDisposable = Get(KnownTypeCode.IDisposable);
///
/// Gets a type reference pointing to the System.Runtime.CompilerServices.INotifyCompletion type.
///
public static readonly KnownTypeReference INotifyCompletion = Get(KnownTypeCode.INotifyCompletion);
///
/// Gets a type reference pointing to the System.Runtime.CompilerServices.ICriticalNotifyCompletion type.
///
public static readonly KnownTypeReference ICriticalNotifyCompletion = Get(KnownTypeCode.ICriticalNotifyCompletion);
readonly KnownTypeCode knownTypeCode;
readonly string namespaceName;
readonly string name;
readonly int typeParameterCount;
internal readonly KnownTypeCode baseType;
private KnownTypeReference(KnownTypeCode knownTypeCode, string namespaceName, string name, int typeParameterCount = 0, KnownTypeCode baseType = KnownTypeCode.Object)
{
this.knownTypeCode = knownTypeCode;
this.namespaceName = namespaceName;
this.name = name;
this.typeParameterCount = typeParameterCount;
this.baseType = baseType;
}
public KnownTypeCode KnownTypeCode {
get { return knownTypeCode; }
}
public string Namespace {
get { return namespaceName; }
}
public string Name {
get { return name; }
}
public int TypeParameterCount {
get { return typeParameterCount; }
}
public IType Resolve(ITypeResolveContext context)
{
return context.Compilation.FindType(knownTypeCode);
}
public override string ToString()
{
return GetCSharpNameByTypeCode(knownTypeCode) ?? (this.Namespace + "." + this.Name);
}
///
/// Gets the C# primitive type name from the known type code.
/// Returns null if there is no primitive name for the specified type.
///
public static string GetCSharpNameByTypeCode(KnownTypeCode knownTypeCode)
{
switch (knownTypeCode) {
case KnownTypeCode.Object:
return "object";
case KnownTypeCode.Boolean:
return "bool";
case KnownTypeCode.Char:
return "char";
case KnownTypeCode.SByte:
return "sbyte";
case KnownTypeCode.Byte:
return "byte";
case KnownTypeCode.Int16:
return "short";
case KnownTypeCode.UInt16:
return "ushort";
case KnownTypeCode.Int32:
return "int";
case KnownTypeCode.UInt32:
return "uint";
case KnownTypeCode.Int64:
return "long";
case KnownTypeCode.UInt64:
return "ulong";
case KnownTypeCode.Single:
return "float";
case KnownTypeCode.Double:
return "double";
case KnownTypeCode.Decimal:
return "decimal";
case KnownTypeCode.String:
return "string";
case KnownTypeCode.Void:
return "void";
default:
return null;
}
}
}
}