PageRenderTime 23ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/NRefactory/ICSharpCode.NRefactory/TypeSystem/SharedTypes.cs

http://github.com/icsharpcode/ILSpy
C# | 111 lines | 47 code | 13 blank | 51 comment | 3 complexity | eef75226749bfe5fbd421d08bf570a61 MD5 | raw file
  1// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2// 
  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:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 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.
 18
 19using System;
 20using ICSharpCode.NRefactory.TypeSystem.Implementation;
 21
 22namespace ICSharpCode.NRefactory.TypeSystem
 23{
 24	/// <summary>
 25	/// Contains static implementations of well-known types.
 26	/// </summary>
 27	public static class SharedTypes
 28	{
 29		/// <summary>
 30		/// Gets the type representing resolve errors.
 31		/// </summary>
 32		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")]
 33		public readonly static IType UnknownType = new SharedTypeImpl(TypeKind.Unknown, "?", isReferenceType: null);
 34		
 35		/// <summary>
 36		/// The null type is used as type of the null literal. It is a reference type without any members; and it is a subtype of all reference types.
 37		/// </summary>
 38		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")]
 39		public readonly static IType Null = new SharedTypeImpl(TypeKind.Null, "null", isReferenceType: true);
 40		
 41		/// <summary>
 42		/// Type representing the C# 'dynamic' type.
 43		/// </summary>
 44		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")]
 45		public readonly static IType Dynamic = new SharedTypeImpl(TypeKind.Dynamic, "dynamic", isReferenceType: true);
 46		
 47		/// <summary>
 48		/// A type used for unbound type arguments in partially parameterized types.
 49		/// </summary>
 50		/// <see cref="IType.GetNestedTypes"/>
 51		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "It's immutable")]
 52		public readonly static IType UnboundTypeArgument = new SharedTypeImpl(TypeKind.UnboundTypeArgument, "", isReferenceType: null);
 53		
 54		/*
 55		 * I'd like to define static instances for common types like
 56		 * void, int, etc.; but there are two problems with this:
 57		 * 
 58		 * SharedTypes.Void.GetDefinition().ProjectContent should return mscorlib, but
 59		 * we can't do that without providing a context.
 60		 * 
 61		 * Assuming we add a context parameter to GetDefinition():
 62		 * 
 63		 * SharedType.Void.Equals(SharedType.Void.GetDefinition(x))
 64		 * SharedType.Void.GetDefinition(y).Equals(SharedType.Void)
 65		 * should both return true.
 66		 * But if the type can have multiple definitions (multiple mscorlib versions loaded),
 67		 * then this is not possible without violating transitivity of Equals():
 68		 * 
 69		 * SharedType.Void.GetDefinition(x).Equals(SharedType.Void.GetDefinition(y))
 70		 * would have to return true even though these are two distinct definitions.
 71		 */
 72		
 73		[Serializable]
 74		sealed class SharedTypeImpl : AbstractType
 75		{
 76			readonly TypeKind kind;
 77			readonly string name;
 78			readonly bool? isReferenceType;
 79			
 80			public SharedTypeImpl(TypeKind kind, string name, bool? isReferenceType)
 81			{
 82				this.kind = kind;
 83				this.name = name;
 84				this.isReferenceType = isReferenceType;
 85			}
 86			
 87			public override TypeKind Kind {
 88				get { return kind; }
 89			}
 90			
 91			public override string Name {
 92				get { return name; }
 93			}
 94			
 95			public override bool? IsReferenceType(ITypeResolveContext context)
 96			{
 97				return isReferenceType;
 98			}
 99			
100			public override bool Equals(IType other)
101			{
102				return other != null && other.Kind == kind;
103			}
104			
105			public override int GetHashCode()
106			{
107				return (int)kind;
108			}
109		}
110	}
111}